Chromium Code Reviews| 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 |