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

Side by Side Diff: blimp/net/helium/vector_clock.h

Issue 2382533002: Helium: Initial proto and Syncable interface definition (Closed)
Patch Set: remainder stufft Created 4 years, 2 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 unified diff | Download patch
OLDNEW
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"
11
10 namespace blimp { 12 namespace blimp {
11 13
12 // From wikipedia: 14 // From wikipedia:
13 // A vector clock is an algorithm for generating a partial ordering of events 15 // A vector clock is an algorithm for generating a partial ordering of events
14 // in a distributed system and detecting causality violations. This is used 16 // in a distributed system and detecting causality violations. This is used
15 // in Blimp to allow client and server modify a local copy of an object and 17 // in Blimp to allow client and server modify a local copy of an object and
16 // later be able to detect ordering or conflicts if any. 18 // later be able to detect ordering or conflicts if any.
17 // 19 //
18 // For more info see: 20 // For more info see:
19 // https://en.wikipedia.org/wiki/Vector_clock 21 // https://en.wikipedia.org/wiki/Vector_clock
20 22
21 typedef uint32_t Revision; 23 typedef uint64_t Revision;
22 24
23 class VectorClock { 25 class VectorClock {
24 public: 26 public:
25 enum class Comparison { LessThan, EqualTo, GreaterThan, Conflict }; 27 enum class Comparison { LessThan, EqualTo, GreaterThan, Conflict };
26 28
27 VectorClock(Revision local_revision, Revision remote_revision); 29 VectorClock(Revision local_revision, Revision remote_revision);
28 VectorClock(); 30 VectorClock();
31 VectorClock(const VectorClock&) = default;
29 32
30 // Compares two vector clocks. There are 4 possibilities for the result: 33 // Compares two vector clocks. There are 4 possibilities for the result:
31 // * LessThan: One revision is equal and for the other is smaller. 34 // * LessThan: One revision is equal and for the other is smaller.
32 // (1,0).CompareTo((2, 0)); 35 // (1,0).CompareTo((2, 0));
33 // * EqualTo: Both revisions are the same. 36 // * EqualTo: Both revisions are the same.
34 // * GreaterThan: One revision is equal and for the other is greater. 37 // * GreaterThan: One revision is equal and for the other is greater.
35 // (2,0).CompareTo((1, 0)); 38 // (2,0).CompareTo((1, 0));
36 // * Conflict: Both revisions are different. (1,0).CompareTo(0,1) 39 // * Conflict: Both revisions are different. (1,0).CompareTo(0,1)
37 Comparison CompareTo(const VectorClock& other) const; 40 Comparison CompareTo(const VectorClock& other) const;
38 41
(...skipping 10 matching lines...) Expand all
49 void set_local_revision(Revision local_revision) { 52 void set_local_revision(Revision local_revision) {
50 local_revision_ = local_revision; 53 local_revision_ = local_revision;
51 } 54 }
52 55
53 Revision remote_revision() const { return remote_revision_; } 56 Revision remote_revision() const { return remote_revision_; }
54 57
55 void set_remote_revision(Revision remote_revision) { 58 void set_remote_revision(Revision remote_revision) {
56 remote_revision_ = remote_revision; 59 remote_revision_ = remote_revision;
57 } 60 }
58 61
62 // Create the proto message corresponding to this object.
63 proto::VectorClockMessage ToProto() const;
64
65 // Inverts the local and remote components respectively.
66 // Used when we send VectorClock across the wire. The local becomes
67 // remote and vice versa.
68 VectorClock Invert() const;
69
59 private: 70 private:
60 Revision local_revision_ = 0; 71 Revision local_revision_ = 0;
61 Revision remote_revision_ = 0; 72 Revision remote_revision_ = 0;
62 }; 73 };
63 74
64 } // namespace blimp 75 } // namespace blimp
65 76
66 #endif // BLIMP_NET_HELIUM_VECTOR_CLOCK_H_ 77 #endif // BLIMP_NET_HELIUM_VECTOR_CLOCK_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698