| 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 #ifndef BLIMP_HELIUM_VERSION_VECTOR_H_ | |
| 6 #define BLIMP_HELIUM_VERSION_VECTOR_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include "blimp/common/proto/helium.pb.h" | |
| 11 #include "blimp/helium/blimp_helium_export.h" | |
| 12 #include "blimp/helium/syncable_common.h" | |
| 13 | |
| 14 namespace blimp { | |
| 15 namespace helium { | |
| 16 | |
| 17 // Version Vectors are used to store revisions of both client and engine | |
| 18 // objects. It will allow determining partial ordering between 2 VersionVectors. | |
| 19 // | |
| 20 // It's similar to a vector clock, but it different according to the Wikipedia | |
| 21 // definition specially when handling merge. Vector Clocks specifies you should | |
| 22 // advance your local version, whereas VersionVectors you won't advance your | |
| 23 // local version (Unless you have to handle a merge conflict and requires to | |
| 24 // send data to the other side). | |
| 25 | |
| 26 typedef uint64_t Revision; | |
| 27 | |
| 28 class BLIMP_HELIUM_EXPORT VersionVector { | |
| 29 public: | |
| 30 enum class Comparison { LessThan, EqualTo, GreaterThan, Conflict }; | |
| 31 | |
| 32 VersionVector(Revision local_revision, Revision remote_revision); | |
| 33 VersionVector(); | |
| 34 VersionVector(const VersionVector&) = default; | |
| 35 | |
| 36 // Compares two vector clocks. There are 4 possibilities for the result: | |
| 37 // * LessThan: One revision is equal and for the other is smaller. | |
| 38 // (1,0).CompareTo((2, 0)); | |
| 39 // * EqualTo: Both revisions are the same. | |
| 40 // * GreaterThan: One revision is equal and for the other is greater. | |
| 41 // (2,0).CompareTo((1, 0)); | |
| 42 // * Conflict: Both revisions are different. (1,0).CompareTo(0,1) | |
| 43 Comparison CompareTo(const VersionVector& other) const; | |
| 44 | |
| 45 // Compares two vector clocks with special handling for conflict cases. | |
| 46 // If |this| and |other| are in conflict, then the value of |bias| determines | |
| 47 // whether the local VersionVector wins (returned as GreaterThan), or loses | |
| 48 // (returned as LessThan). | |
| 49 Comparison CompareWithBias(const VersionVector& other, Bias bias) const; | |
| 50 | |
| 51 // Merges two vector clocks. This function should be used at synchronization | |
| 52 // points. i.e. when client receives data from the server or vice versa. | |
| 53 VersionVector MergeWith(const VersionVector& other) const; | |
| 54 | |
| 55 // Increments local_revision_ by one. This is used when something changes | |
| 56 // in the local state like setting a property or applying a change set with | |
| 57 // a conflict that requires sending state to the other peer. | |
| 58 void IncrementLocal(); | |
| 59 | |
| 60 Revision local_revision() const { return local_revision_; } | |
| 61 | |
| 62 void set_local_revision(Revision local_revision) { | |
| 63 local_revision_ = local_revision; | |
| 64 } | |
| 65 | |
| 66 Revision remote_revision() const { return remote_revision_; } | |
| 67 | |
| 68 void set_remote_revision(Revision remote_revision) { | |
| 69 remote_revision_ = remote_revision; | |
| 70 } | |
| 71 | |
| 72 // Create the proto message corresponding to this object. | |
| 73 proto::VersionVectorMessage ToProto() const; | |
| 74 | |
| 75 // Inverts the local and remote components respectively. | |
| 76 // Used when we send VersionVector across the wire. The local becomes | |
| 77 // remote and vice versa. | |
| 78 VersionVector Invert() const; | |
| 79 | |
| 80 private: | |
| 81 Revision local_revision_ = 0; | |
| 82 Revision remote_revision_ = 0; | |
| 83 }; | |
| 84 | |
| 85 } // namespace helium | |
| 86 } // namespace blimp | |
| 87 | |
| 88 #endif // BLIMP_HELIUM_VERSION_VECTOR_H_ | |
| OLD | NEW |