Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2821)

Unified Diff: blimp/net/helium/vector_clock.cc

Issue 2372903002: Helium: Adding vector clock (Closed)
Patch Set: Missed one feedback (s/r/Conflicts/Conflict) Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: blimp/net/helium/vector_clock.cc
diff --git a/blimp/net/helium/vector_clock.cc b/blimp/net/helium/vector_clock.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f31181086e80f1be9030a3e61011c3d681c7ee3e
--- /dev/null
+++ b/blimp/net/helium/vector_clock.cc
@@ -0,0 +1,56 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "blimp/net/helium/vector_clock.h"
+
+#include <algorithm>
+
+#include "base/logging.h"
+
+namespace blimp {
+
+VectorClock::VectorClock() : local_revision_(0), remote_revision_(0) {}
+
+VectorClock::VectorClock(int32_t local_revision, int32_t remote_revision)
+ : local_revision_(local_revision), remote_revision_(remote_revision) {}
+
+VectorClock::Comparison VectorClock::CompareTo(const VectorClock& other) const {
+ DCHECK(local_revision() >= other.local_revision());
Kevin M 2016/09/27 21:36:09 add blank line below dchecks
scf 2016/09/27 23:23:10 Done.
+ if (local_revision() == other.local_revision()) {
+ if (remote_revision() == other.remote_revision()) {
+ return VectorClock::Comparison::EqualTo;
+ } else if (remote_revision() < other.remote_revision()) {
+ return VectorClock::Comparison::LessThan;
+ } else {
+ return VectorClock::Comparison::GreaterThan;
+ }
+ } else {
+ if (local_revision() > other.local_revision()) {
+ if (remote_revision() == other.remote_revision()) {
+ return VectorClock::Comparison::GreaterThan;
+ } else {
+ return VectorClock::Comparison::Conflict;
+ }
+ } else { // We know its not equal or greater, so its smaller
+ if (remote_revision() == other.remote_revision()) {
+ return VectorClock::Comparison::LessThan;
+ } else {
+ DCHECK(false) << "Local revision should always be greater or equal.";
Kevin M 2016/09/27 21:36:09 * Already checked in the top of CompareTo. * No ne
scf 2016/09/27 23:23:10 Done.
+ return VectorClock::Comparison::Conflict;
+ }
+ }
+ }
+}
+
+VectorClock VectorClock::MergeWith(const VectorClock& other) const {
+ VectorClock result(std::max(local_revision(), other.local_revision()),
+ std::max(remote_revision(), other.remote_revision()));
+ return result;
+}
+
+void VectorClock::IncrementLocal() {
+ set_local_revision(local_revision() + 1);
Kevin M 2016/09/27 21:36:09 Accesses to members of |this| needn't be mediated
scf 2016/09/27 23:23:10 Done.
+}
+
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698