Chromium Code Reviews| 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_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" | |
| 13 #include "blimp/net/helium/vector_clock.h" | |
| 14 | |
| 15 namespace blimp { | |
| 16 | |
| 17 // SyncableObject and SyncableMember are conceptually very similar. | |
|
Kevin M
2016/10/03 21:44:26
Include a description about what a Syncable<> is h
scf
2016/10/03 22:20:17
Done.
| |
| 18 // Both are objects that will be synchronized between client and engine. | |
|
Kevin M
2016/10/03 21:44:26
This seems to pigeonhole in the differences and si
scf
2016/10/03 22:20:17
Done.
| |
| 19 // | |
| 20 // 1. There is a one-to-one relationship between instances on the client and | |
| 21 // instances on the engine. | |
| 22 // 2. The values stored in client-engine pairs should eventually be equal. | |
| 23 // | |
| 24 // The main difference is that SyncableObject owns its lifetime, whereas | |
| 25 // SyncableMember depends on its SyncableObject parent. | |
| 26 // | |
| 27 // An example for GeoLocation would be: | |
| 28 // GeoLocation : SyncableObject { | |
| 29 // * Frequency : SyncableMember | |
| 30 // * Position : SyncableMember | |
| 31 // } | |
| 32 // | |
| 33 // VectorClocks from a SyncableMember can be compared with the VectorClock from | |
| 34 // the SyncableObject. This reduces the amount of state that needs to be kept. | |
| 35 | |
| 36 template <class ChangesetType> | |
| 37 class Syncable { | |
| 38 public: | |
| 39 virtual ~Syncable() {} | |
| 40 | |
| 41 // Constructs a changeset between the |from| revision and its current state. | |
| 42 // The Sync layer will encapsulate the changeset with details since |from|, | |
| 43 // but the Object is responsible for including any revision information | |
| 44 // additional to that expressed by the VectorClocks, that is necessary to | |
| 45 // detect and resolve conflicts. | |
| 46 // The computed changeset is returned asynchronously as a return parameter | |
| 47 virtual std::unique_ptr<ChangesetType> CreateChangesetToCurrent( | |
| 48 const VectorClock& from) = 0; | |
| 49 | |
| 50 // Applies a |changeset| given as parameter to the contents of the | |
| 51 // Syncable. | |
| 52 // The VectorClocks |from| and |to| can be used to detect and resolve | |
| 53 // concurrent change conflicts. | |
| 54 virtual void ApplyChangeset(const VectorClock& from, | |
| 55 const VectorClock& to, | |
| 56 std::unique_ptr<ChangesetType> changeset) = 0; | |
| 57 | |
| 58 // Gives a chance for the Syncable to delete any old data previous to the | |
| 59 // |checkpoint|. This is a pretty important method that will remove some | |
| 60 // memory pressure for example from the UniqueSet CRDTs. They need to keep | |
| 61 // a growing list of added/removed elements over time. With this checkpoint | |
| 62 // info they can delete those elements prior to the vector clock specified in | |
| 63 // |checkpoint|. | |
| 64 virtual void ReleaseCheckpointsBefore(const VectorClock& checkpoint) = 0; | |
| 65 }; | |
| 66 | |
| 67 class SyncableObject : public Syncable<helium::ChangesetMessage> { | |
| 68 public: | |
| 69 SyncableObject(); | |
|
Kevin M
2016/10/03 21:44:26
What will this be used for?
scf
2016/10/03 22:20:17
Done.
| |
| 70 explicit SyncableObject(const VectorClock& clock); | |
| 71 ~SyncableObject() override {} | |
| 72 | |
| 73 const VectorClock& clock() { return clock_; } | |
|
Kevin M
2016/10/03 21:44:26
Method should be const.
scf
2016/10/03 22:20:17
Done.
| |
| 74 | |
| 75 VectorClock* mutable_clock() { return &clock_; } | |
|
Kevin M
2016/10/03 21:44:26
Method should be const.
scf
2016/10/03 22:20:18
cannot return pointer of member in a const method
| |
| 76 | |
| 77 protected: | |
| 78 // The clock is needed in order for the |SyncableMember| objects whenever | |
| 79 // they modify their state they update their |last_modified_| value. | |
| 80 // The rationale here is use a "global" per SyncableObject clock which makes | |
| 81 // state management a lot simpler. | |
| 82 VectorClock clock_; | |
|
Kevin M
2016/10/03 21:44:26
If we are using a global clock, then we should tak
scf
2016/10/03 22:20:17
yes it will be done in a separate cl as discussed
| |
| 83 | |
| 84 private: | |
| 85 DISALLOW_COPY_AND_ASSIGN(SyncableObject); | |
| 86 }; | |
| 87 | |
| 88 template <class ChangesetType> | |
| 89 class SyncableMember : public Syncable<ChangesetType> { | |
| 90 public: | |
| 91 explicit SyncableMember(SyncableObject* parent); | |
| 92 ~SyncableMember() override {} | |
| 93 | |
| 94 // Returns true if the object have been modified since |from| | |
|
Kevin M
2016/10/03 21:44:26
trailing period.
scf
2016/10/03 22:20:17
Done.
| |
| 95 virtual bool ModifiedSince(const VectorClock& from) = 0; | |
| 96 | |
| 97 protected: | |
| 98 // Increments the parent clock and returns the new value. Should be used | |
| 99 // whenever a SyncableMember updates its state. | |
| 100 VectorClock IncrementParentClock(); | |
|
Kevin M
2016/10/03 21:44:26
Should we also have a method for getting the paren
scf
2016/10/03 22:20:18
Done.
| |
| 101 | |
| 102 SyncableObject* parent_; | |
| 103 | |
| 104 private: | |
| 105 DISALLOW_COPY_AND_ASSIGN(SyncableMember); | |
| 106 }; | |
| 107 | |
| 108 // Actual implementations are inline because of template instantiation | |
| 109 | |
| 110 SyncableObject::SyncableObject() : clock_() {} | |
| 111 | |
| 112 SyncableObject::SyncableObject(const VectorClock& clock) : clock_(clock) {} | |
| 113 | |
| 114 template <class ChangesetType> | |
| 115 SyncableMember<ChangesetType>::SyncableMember(SyncableObject* parent) | |
| 116 : parent_(parent) {} | |
| 117 | |
| 118 template <class ChangesetType> | |
| 119 VectorClock SyncableMember<ChangesetType>::IncrementParentClock() { | |
| 120 parent_->mutable_clock()->IncrementLocal(); | |
| 121 return parent_->clock(); | |
| 122 } | |
| 123 | |
| 124 } // namespace blimp | |
| 125 | |
| 126 #endif // BLIMP_NET_HELIUM_SYNCABLE_H_ | |
| OLD | NEW |