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

Side by Side Diff: blimp/helium/lww_register.h

Issue 2400303002: Add LwwRegister CRDT (Closed)
Patch Set: Combine Bias and RunningAs enums, plus other code review feedback. 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
(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_LWW_REGISTER_H_
6 #define BLIMP_HELIUM_LWW_REGISTER_H_
7
8 #include "base/memory/ptr_util.h"
9 #include "blimp/helium/blimp_helium_export.h"
10 #include "blimp/helium/syncable.h"
11 #include "blimp/helium/syncable_common.h"
12 #include "blimp/helium/syncable_primitive_serializer.h"
13 #include "blimp/helium/version_vector.h"
14 #include "blimp/helium/version_vector_generator.h"
15 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
16
17 namespace blimp {
18 namespace helium {
19
20 // Provides a simple syncable and atomically-writable "register" holding
21 // contents of type |RegisterType|. When there is a write conflict, it is
22 // resolved by assuming the writer indicated by |bias| has the correct value.
23 template <class RegisterType>
24 class BLIMP_HELIUM_EXPORT LwwRegister : public Syncable {
25 public:
26 LwwRegister(VersionVectorGenerator* version_gen, Peer bias, Peer running_as);
steimel 2016/10/19 16:42:49 Since I end up just storing whether bias == runnin
Kevin M 2016/10/19 17:49:11 That's up to you, but IMO explicitly breaking out
steimel 2016/10/20 00:30:07 Agreed. I'll leave it
27 ~LwwRegister() = default;
28
29 void Set(const RegisterType& value);
30
31 const RegisterType& Get() const;
32
33 // Syncable implementation.
34 bool ModifiedSince(const VersionVector& from) const override;
35 void CreateChangesetToCurrent(
36 const VersionVector& from,
37 google::protobuf::io::CodedOutputStream* output_stream) override;
38 void ApplyChangeset(
39 const VersionVector& from,
40 const VersionVector& to,
41 google::protobuf::io::CodedInputStream* input_stream) override;
42 void ReleaseCheckpointsBefore(const VersionVector& checkpoint) override;
43
44 private:
45 VersionVectorGenerator* version_gen_;
CJ 2016/10/20 00:20:16 Does the LWW_Register take ownership over the Vers
steimel 2016/10/20 00:30:06 I believe a single, global generator will be used
46 VersionVector last_modified_;
47 bool locally_owned_;
48 RegisterType value_;
49 bool value_set_ = false;
50
51 DISALLOW_COPY_AND_ASSIGN(LwwRegister);
52 };
53
54 template <class RegisterType>
55 LwwRegister<RegisterType>::LwwRegister(VersionVectorGenerator* version_gen,
56 Peer bias,
57 Peer running_as)
58 : version_gen_(version_gen),
59 last_modified_(version_gen->current()),
60 locally_owned_(bias == running_as) {
61 DCHECK(version_gen_);
62 }
63
64 template <class RegisterType>
65 void LwwRegister<RegisterType>::Set(const RegisterType& value) {
66 value_ = value;
67 value_set_ = true;
68 version_gen_->Increment();
69 last_modified_ = last_modified_.MergeWith(version_gen_->current());
70 }
71
72 template <class RegisterType>
73 const RegisterType& LwwRegister<RegisterType>::Get() const {
74 DCHECK(value_set_);
75 return value_;
76 }
77
78 template <class RegisterType>
79 bool LwwRegister<RegisterType>::ModifiedSince(const VersionVector& from) const {
80 return from.local_revision() < last_modified_.local_revision();
81 }
82
83 template <class RegisterType>
84 void LwwRegister<RegisterType>::CreateChangesetToCurrent(
85 const VersionVector& from,
86 google::protobuf::io::CodedOutputStream* output_stream) {
87 SyncablePrimitiveSerializer::Serialize(value_, output_stream);
88 }
89
90 template <class RegisterType>
91 void LwwRegister<RegisterType>::ApplyChangeset(
92 const VersionVector& from,
93 const VersionVector& to,
94 google::protobuf::io::CodedInputStream* input_stream) {
95 VersionVector::Comparison cmp = last_modified_.CompareTo(to);
96 if (cmp == VersionVector::Comparison::LessThan ||
97 (cmp == VersionVector::Comparison::Conflict && !locally_owned_)) {
98 // TODO(steimel): deal with Deserialize returning false once ApplyChangeset
99 // returns a HeliumResult.
100 SyncablePrimitiveSerializer::Deserialize(input_stream, &value_);
101 value_set_ = true;
102 }
103 last_modified_ = last_modified_.MergeWith(to);
104 }
105
106 template <class RegisterType>
107 void LwwRegister<RegisterType>::ReleaseCheckpointsBefore(
108 const VersionVector& checkpoint) {
109 // no-op
110 }
111
112 } // namespace helium
113 } // namespace blimp
114
115 #endif // BLIMP_HELIUM_LWW_REGISTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698