| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "blimp/helium/version_vector.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 | |
| 11 namespace blimp { | |
| 12 namespace helium { | |
| 13 | |
| 14 VersionVector::VersionVector() {} | |
| 15 | |
| 16 VersionVector::VersionVector(Revision local_revision, Revision remote_revision) | |
| 17 : local_revision_(local_revision), remote_revision_(remote_revision) {} | |
| 18 | |
| 19 VersionVector::Comparison VersionVector::CompareTo( | |
| 20 const VersionVector& other) const { | |
| 21 DCHECK_GE(local_revision_, other.local_revision()); | |
| 22 | |
| 23 if (local_revision_ == other.local_revision()) { | |
| 24 if (remote_revision_ == other.remote_revision()) { | |
| 25 return VersionVector::Comparison::EqualTo; | |
| 26 } else if (remote_revision_ < other.remote_revision()) { | |
| 27 return VersionVector::Comparison::LessThan; | |
| 28 } else { | |
| 29 return VersionVector::Comparison::GreaterThan; | |
| 30 } | |
| 31 } else { | |
| 32 if (local_revision_ > other.local_revision()) { | |
| 33 if (remote_revision_ == other.remote_revision()) { | |
| 34 return VersionVector::Comparison::GreaterThan; | |
| 35 } else { | |
| 36 return VersionVector::Comparison::Conflict; | |
| 37 } | |
| 38 } else { // We know its not equal or greater, so its smaller | |
| 39 if (remote_revision_ == other.remote_revision()) { | |
| 40 return VersionVector::Comparison::LessThan; | |
| 41 } else { | |
| 42 LOG(FATAL) << "Local revision should always be greater or equal."; | |
| 43 return VersionVector::Comparison::Conflict; | |
| 44 } | |
| 45 } | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 VersionVector::Comparison VersionVector::CompareWithBias( | |
| 50 const VersionVector& other, | |
| 51 Bias bias) const { | |
| 52 Comparison cmp = CompareTo(other); | |
| 53 if (cmp == Comparison::Conflict) { | |
| 54 if (bias == Bias::REMOTE) { | |
| 55 return Comparison::LessThan; | |
| 56 } else if (bias == Bias::LOCAL) { | |
| 57 return Comparison::GreaterThan; | |
| 58 } | |
| 59 } | |
| 60 return cmp; | |
| 61 } | |
| 62 | |
| 63 VersionVector VersionVector::MergeWith(const VersionVector& other) const { | |
| 64 VersionVector result(std::max(local_revision_, other.local_revision()), | |
| 65 std::max(remote_revision_, other.remote_revision())); | |
| 66 return result; | |
| 67 } | |
| 68 | |
| 69 void VersionVector::IncrementLocal() { | |
| 70 local_revision_++; | |
| 71 } | |
| 72 | |
| 73 proto::VersionVectorMessage VersionVector::ToProto() const { | |
| 74 proto::VersionVectorMessage result; | |
| 75 result.set_local_revision(local_revision_); | |
| 76 result.set_remote_revision(remote_revision_); | |
| 77 return result; | |
| 78 } | |
| 79 | |
| 80 VersionVector VersionVector::Invert() const { | |
| 81 return VersionVector(remote_revision_, local_revision_); | |
| 82 } | |
| 83 | |
| 84 } // namespace helium | |
| 85 } // namespace blimp | |
| OLD | NEW |