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

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

Issue 2382533002: Helium: Initial proto and Syncable interface definition (Closed)
Patch Set: addressing 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_NET_HELIUM_SYNCABLE_H_
6 #define BLIMP_NET_HELIUM_SYNCABLE_H_
7
8 #include <stdint.h>
9 #include <memory>
10
11 #include "base/macros.h"
12 #include "blimp/common/proto/helium.pb.h"
Kevin M 2016/10/03 22:30:35 Use forward declaration for ChangesetMessage?
scf 2016/10/03 22:52:33 Done.
13 #include "blimp/net/helium/vector_clock.h"
14
15 namespace blimp {
16
17 // Syncable is a base class interface that is used for both SyncableObject and
18 // SyncableMember classes.
19 //
20 // Syncable is something that supports creating and restoring Changesets.
21 // Changesets are state that is propagated between client and server to
22 // keep corresponding objects eventually synchronized.
23 //
24 // 1. There is a one-to-one relationship between instances on the client and
Kevin M 2016/10/03 22:30:35 Give this ordered list a quick sentence for contex
scf 2016/10/03 22:52:33 Done.
25 // instances on the engine.
26 // 2. The values stored in client-engine pairs should eventually be equal.
27 //
28 // The main difference is that SyncableObject owns its lifetime, whereas
29 // SyncableMember depends on its SyncableObject parent.
30 //
31 // An example for GeoLocation would be:
32 // GeoLocation : SyncableObject {
33 // * Frequency : SyncableMember
34 // * Position : SyncableMember
35 // }
36 //
37 // VectorClocks from a SyncableMember can be compared with the VectorClock from
38 // the SyncableObject. This reduces the amount of state that needs to be kept.
39
40 template <class ChangesetType>
41 class Syncable {
42 public:
43 virtual ~Syncable() {}
44
45 // Constructs a changeset between the |from| revision and its current state.
46 // The Sync layer will encapsulate the changeset with details since |from|,
47 // but the Object is responsible for including any revision information
48 // additional to that expressed by the VectorClocks, that is necessary to
49 // detect and resolve conflicts.
50 // The computed changeset is returned asynchronously as a return parameter.
51 virtual std::unique_ptr<ChangesetType> CreateChangesetToCurrent(
52 const VectorClock& from) = 0;
53
54 // Applies a |changeset| given as parameter to the contents of the
55 // Syncable.
56 // The VectorClocks |from| and |to| can be used to detect and resolve
57 // concurrent change conflicts.
58 virtual void ApplyChangeset(const VectorClock& from,
59 const VectorClock& to,
60 std::unique_ptr<ChangesetType> changeset) = 0;
61
62 // Gives a chance for the Syncable to delete any old data previous to the
63 // |checkpoint|. This is a pretty important method that will remove some
64 // memory pressure for example from the UniqueSet CRDTs. They need to keep
65 // a growing list of added/removed elements over time. With this checkpoint
66 // info they can delete those elements prior to the vector clock specified in
67 // |checkpoint|.
68 virtual void ReleaseCheckpointsBefore(const VectorClock& checkpoint) = 0;
69 };
70
71 class SyncableObject : public Syncable<helium::ChangesetMessage> {
72 public:
73 explicit SyncableObject(const VectorClock& clock);
74 ~SyncableObject() override {}
75
76 const VectorClock& clock() const { return clock_; }
77
78 VectorClock* mutable_clock() { return &clock_; }
79
80 protected:
81 // The clock is needed in order for the |SyncableMember| objects whenever
82 // they modify their state they update their |last_modified_| value.
83 // The rationale here is use a "global" per SyncableObject clock which makes
84 // state management a lot simpler.
85 VectorClock clock_;
86
87 private:
88 DISALLOW_COPY_AND_ASSIGN(SyncableObject);
89 };
90
91 template <class ChangesetType>
92 class SyncableMember : public Syncable<ChangesetType> {
93 public:
94 explicit SyncableMember(SyncableObject* parent);
95 ~SyncableMember() override {}
96
97 // Returns true if the object have been modified since |from|.
98 virtual bool ModifiedSince(const VectorClock& from) = 0;
99
100 protected:
101 // Increments the parent clock and returns the new value. Should be used
102 // whenever a SyncableMember updates its state.
103 VectorClock IncrementParentClock();
104
105 // Returns the current value of the vector clock of the parent object (aka
106 // SyncableObject).
107 const VectorClock& parent_clock() const;
108
109 private:
110 SyncableObject* parent_;
111 DISALLOW_COPY_AND_ASSIGN(SyncableMember);
112 };
113
114 // Actual implementations are inline because of template instantiation.
Kevin M 2016/10/03 22:30:35 No need to write this
scf 2016/10/03 22:52:33 Done.
115
116 SyncableObject::SyncableObject(const VectorClock& clock) : clock_(clock) {}
117
118 template <class ChangesetType>
119 SyncableMember<ChangesetType>::SyncableMember(SyncableObject* parent)
120 : parent_(parent) {}
121
122 template <class ChangesetType>
123 VectorClock SyncableMember<ChangesetType>::IncrementParentClock() {
124 parent_->mutable_clock()->IncrementLocal();
125 return parent_->clock();
126 }
127
128 template <class ChangesetType>
129 const VectorClock& SyncableMember<ChangesetType>::parent_clock() const {
130 return parent_->clock();
131 }
132
133 } // namespace blimp
134
135 #endif // BLIMP_NET_HELIUM_SYNCABLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698