Refactored the protobuf message comparison logic. Review: https://reviews.apache.org/r/24631
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/2d4eb0ef Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/2d4eb0ef Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/2d4eb0ef Branch: refs/heads/master Commit: 2d4eb0ef3c162502595f135408b0fe42cf29ebfa Parents: 038d833 Author: Jie Yu <[email protected]> Authored: Tue Aug 12 17:15:39 2014 -0700 Committer: Jie Yu <[email protected]> Committed: Tue Aug 12 23:28:29 2014 -0700 ---------------------------------------------------------------------- src/common/type_utils.cpp | 88 ++++++++++++------------------------------ 1 file changed, 25 insertions(+), 63 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/2d4eb0ef/src/common/type_utils.cpp ---------------------------------------------------------------------- diff --git a/src/common/type_utils.cpp b/src/common/type_utils.cpp index c8fc7b3..af0e3a6 100644 --- a/src/common/type_utils.cpp +++ b/src/common/type_utils.cpp @@ -16,79 +16,54 @@ * limitations under the License. */ +#include <string> + #include <mesos/mesos.hpp> #include <mesos/resources.hpp> #include "common/attributes.hpp" #include "common/type_utils.hpp" +using std::string; + namespace mesos { -bool operator == (const Environment& left, const Environment& right) +static bool equals( + const google::protobuf::Message& left, + const google::protobuf::Message& right) { - if (left.variables().size() != right.variables().size()) { + string _left; + string _right; + + // NOTE: If either of the two messages is not initialized, we will + // treat them as not equal. + if (!left.SerializeToString(&_left)) { return false; } - for (int i = 0; i < left.variables().size(); i++) { - const std::string& name = left.variables().Get(i).name(); - const std::string& value = left.variables().Get(i).value(); - bool found = false; - for (int j = 0; j < right.variables().size(); j++) { - if (name == right.variables().Get(j).name() && - value == right.variables().Get(j).value()) { - found = true; - break; - } - } - if (!found) { - return false; - } + if (!right.SerializeToString(&_right)) { + return false; } - return true; + return _left == _right; } -bool operator == (const CommandInfo& left, const CommandInfo& right) +bool operator == (const Environment& left, const Environment& right) { - if (left.uris().size() != right.uris().size()) { - return false; - } + return equals(left, right); +} - for (int i=0; i<left.uris().size(); i++) { - bool found = false; - for (int j=0; j<right.uris().size(); j++) { - if (left.uris().Get(i) == right.uris().Get(j)) { - found = true; - break; - } - } - if (!found) { - return false; - } - } - return left.has_environment() == right.has_environment() && - (!left.has_environment() || (left.environment() == right.environment())) && - left.value() == right.value(); +bool operator == (const CommandInfo& left, const CommandInfo& right) +{ + return equals(left, right); } bool operator == (const ExecutorInfo& left, const ExecutorInfo& right) { - return left.executor_id() == right.executor_id() && - left.has_framework_id() == right.has_framework_id() && - (!left.has_framework_id() || - (left.framework_id() == right.framework_id())) && - left.command() == right.command() && - Resources(left.resources()) == Resources(right.resources()) && - left.has_name() == right.has_name() && - (!left.has_name() || (left.name() == right.name())) && - left.has_source() == right.has_source() && - (!left.has_source() || (left.source() == right.source())) && - left.has_data() == right.has_data() && - (!left.has_data() || (left.data() == right.data())); + return equals(left, right); } @@ -109,13 +84,7 @@ bool operator == (const SlaveInfo& left, const SlaveInfo& right) bool operator == (const MasterInfo& left, const MasterInfo& right) { - return left.id() == right.id() && - left.ip() == right.ip() && - left.port() == right.port() && - left.has_pid() == right.has_pid() && - (!left.has_pid() || (left.pid() == right.pid())) && - left.has_hostname() == right.has_hostname() && - (!left.has_hostname() || (left.hostname() == right.hostname())); + return equals(left, right); } @@ -123,14 +92,7 @@ namespace internal { bool operator == (const Task& left, const Task& right) { - return left.name() == right.name() && - left.task_id() == right.task_id() && - left.framework_id() == right.framework_id() && - left.slave_id() == right.slave_id() && - left.state() == right.state() && - Resources(left.resources()) == Resources(right.resources()) && - left.has_executor_id() == right.has_executor_id() && - (!left.has_executor_id() || (left.executor_id() == right.executor_id())); + return equals(left, right); }
