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

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

Issue 2372903002: Helium: Adding vector clock (Closed)
Patch Set: Missed to update one revision 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..8481e7aa527622e82bee537c37e58ccdcffa510c
--- /dev/null
+++ b/blimp/net/helium/vector_clock.cc
@@ -0,0 +1,57 @@
+// 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) {}
Kevin M 2016/09/28 01:23:15 Put the zeroes in the .h as inline initializers
scf 2016/09/28 16:40:37 Done.
+
+VectorClock::VectorClock(Revision local_revision, Revision remote_revision)
+ : local_revision_(local_revision), remote_revision_(remote_revision) {}
+
+VectorClock::Comparison VectorClock::CompareTo(const VectorClock& other) const {
+ DCHECK(local_revision_ >= other.local_revision());
+
+ 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 {
+ DLOG(FATAL) << "Local revision should always be greater or equal.";
Kevin M 2016/09/28 01:23:15 This state is pretty messed up, right? Maybe we sh
scf 2016/09/28 16:40:37 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() {
+ local_revision_++;
+}
+
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698