| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BLIMP_NET_HELIUM_VECTOR_CLOCK_H_ | 5 #ifndef BLIMP_NET_HELIUM_VECTOR_CLOCK_H_ |
| 6 #define BLIMP_NET_HELIUM_VECTOR_CLOCK_H_ | 6 #define BLIMP_NET_HELIUM_VECTOR_CLOCK_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "blimp/common/proto/helium.pb.h" |
| 10 #include "blimp/net/blimp_net_export.h" | 11 #include "blimp/net/blimp_net_export.h" |
| 11 | 12 |
| 12 namespace blimp { | 13 namespace blimp { |
| 13 | 14 |
| 14 // From wikipedia: | 15 // From wikipedia: |
| 15 // A vector clock is an algorithm for generating a partial ordering of events | 16 // A vector clock is an algorithm for generating a partial ordering of events |
| 16 // in a distributed system and detecting causality violations. This is used | 17 // in a distributed system and detecting causality violations. This is used |
| 17 // in Blimp to allow client and server modify a local copy of an object and | 18 // in Blimp to allow client and server modify a local copy of an object and |
| 18 // later be able to detect ordering or conflicts if any. | 19 // later be able to detect ordering or conflicts if any. |
| 19 // | 20 // |
| 20 // For more info see: | 21 // For more info see: |
| 21 // https://en.wikipedia.org/wiki/Vector_clock | 22 // https://en.wikipedia.org/wiki/Vector_clock |
| 22 | 23 |
| 23 typedef uint32_t Revision; | 24 typedef uint64_t Revision; |
| 24 | 25 |
| 25 class BLIMP_NET_EXPORT VectorClock { | 26 class BLIMP_NET_EXPORT VectorClock { |
| 26 public: | 27 public: |
| 27 enum class Comparison { LessThan, EqualTo, GreaterThan, Conflict }; | 28 enum class Comparison { LessThan, EqualTo, GreaterThan, Conflict }; |
| 28 | 29 |
| 29 VectorClock(Revision local_revision, Revision remote_revision); | 30 VectorClock(Revision local_revision, Revision remote_revision); |
| 30 VectorClock(); | 31 VectorClock(); |
| 32 VectorClock(const VectorClock&) = default; |
| 31 | 33 |
| 32 // Compares two vector clocks. There are 4 possibilities for the result: | 34 // Compares two vector clocks. There are 4 possibilities for the result: |
| 33 // * LessThan: One revision is equal and for the other is smaller. | 35 // * LessThan: One revision is equal and for the other is smaller. |
| 34 // (1,0).CompareTo((2, 0)); | 36 // (1,0).CompareTo((2, 0)); |
| 35 // * EqualTo: Both revisions are the same. | 37 // * EqualTo: Both revisions are the same. |
| 36 // * GreaterThan: One revision is equal and for the other is greater. | 38 // * GreaterThan: One revision is equal and for the other is greater. |
| 37 // (2,0).CompareTo((1, 0)); | 39 // (2,0).CompareTo((1, 0)); |
| 38 // * Conflict: Both revisions are different. (1,0).CompareTo(0,1) | 40 // * Conflict: Both revisions are different. (1,0).CompareTo(0,1) |
| 39 Comparison CompareTo(const VectorClock& other) const; | 41 Comparison CompareTo(const VectorClock& other) const; |
| 40 | 42 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 51 void set_local_revision(Revision local_revision) { | 53 void set_local_revision(Revision local_revision) { |
| 52 local_revision_ = local_revision; | 54 local_revision_ = local_revision; |
| 53 } | 55 } |
| 54 | 56 |
| 55 Revision remote_revision() const { return remote_revision_; } | 57 Revision remote_revision() const { return remote_revision_; } |
| 56 | 58 |
| 57 void set_remote_revision(Revision remote_revision) { | 59 void set_remote_revision(Revision remote_revision) { |
| 58 remote_revision_ = remote_revision; | 60 remote_revision_ = remote_revision; |
| 59 } | 61 } |
| 60 | 62 |
| 63 // Create the proto message corresponding to this object. |
| 64 proto::VectorClockMessage ToProto() const; |
| 65 |
| 66 // Inverts the local and remote components respectively. |
| 67 // Used when we send VectorClock across the wire. The local becomes |
| 68 // remote and vice versa. |
| 69 VectorClock Invert() const; |
| 70 |
| 61 private: | 71 private: |
| 62 Revision local_revision_ = 0; | 72 Revision local_revision_ = 0; |
| 63 Revision remote_revision_ = 0; | 73 Revision remote_revision_ = 0; |
| 64 }; | 74 }; |
| 65 | 75 |
| 66 } // namespace blimp | 76 } // namespace blimp |
| 67 | 77 |
| 68 #endif // BLIMP_NET_HELIUM_VECTOR_CLOCK_H_ | 78 #endif // BLIMP_NET_HELIUM_VECTOR_CLOCK_H_ |
| OLD | NEW |