| 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/callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "blimp/common/mandatory_callback.h" |
| 14 #include "blimp/net/helium/vector_clock.h" |
| 15 #include "blimp/net/helium/vector_clock_generator.h" |
| 16 |
| 17 namespace blimp { |
| 18 |
| 19 namespace proto { |
| 20 class ChangesetMessage; |
| 21 } |
| 22 |
| 23 // Syncable is something that supports creating and restoring Changesets. |
| 24 // These objects exchange Changesets between the client and server to keep |
| 25 // their peer counterparts eventually synchronized. |
| 26 // |
| 27 // It provides the following things: |
| 28 // 1. Mapping: There is a one-to-one relationship between instances on the |
| 29 // client and instances on the engine. |
| 30 // 2. Consistency: The values stored in client-engine pairs should eventually be |
| 31 // equal. |
| 32 // |
| 33 // Syncable is a base interface that is used for both self contained |
| 34 // objects (i.e. Simple register) and objects which are disconnected replicas |
| 35 // of external state. |
| 36 // |
| 37 template <class ChangesetType> |
| 38 class Syncable { |
| 39 public: |
| 40 virtual ~Syncable() {} |
| 41 |
| 42 // Constructs a changeset between the |from| revision and its current state. |
| 43 // The Sync layer will encapsulate the changeset with details since |from|, |
| 44 // but the Syncable is responsible for including any revision information |
| 45 // additional to that expressed by the VectorClocks, that is necessary to |
| 46 // detect and resolve conflicts. |
| 47 // The changeset is returned as a return value. |
| 48 virtual std::unique_ptr<ChangesetType> CreateChangesetToCurrent( |
| 49 const VectorClock& from) = 0; |
| 50 |
| 51 // Applies a |changeset| given as parameter to the contents of the |
| 52 // Syncable. |
| 53 // The VectorClocks |from| and |to| can be used to detect and resolve |
| 54 // concurrent change conflicts. |
| 55 virtual void ApplyChangeset(const VectorClock& from, |
| 56 const VectorClock& to, |
| 57 std::unique_ptr<ChangesetType> changeset) = 0; |
| 58 |
| 59 // Gives a chance for the Syncable to delete any old data previous to the |
| 60 // |checkpoint|. |
| 61 virtual void ReleaseCheckpointsBefore(const VectorClock& checkpoint) = 0; |
| 62 |
| 63 // Returns true if the object has been modified since |from|. |
| 64 virtual bool ModifiedSince(const VectorClock& from) const = 0; |
| 65 }; |
| 66 |
| 67 // Extends the Syncable interface by adding support to asynchronously replicate |
| 68 // state with some |
| 69 // external entity. |
| 70 // |
| 71 // TwoPhaseSyncable name derives from the fact that the state is both created |
| 72 // and applied in two stages: |
| 73 // |
| 74 // 1. Creation |
| 75 // 1.1) PreCreateChangesetToCurrent is called which retrieves the state |
| 76 // from an external state and saves locally. |
| 77 // 1.2) CreateChangesetToCurrent is called to actually create the changeset. |
| 78 // |
| 79 // 2. Updating |
| 80 // 2.1) ApplyChangeset is called which updates the local state. |
| 81 // 2.2) PostApplyChangeset is called to apply the state from the local |
| 82 // object into the external state. |
| 83 class TwoPhaseSyncable : public Syncable<proto::ChangesetMessage> { |
| 84 public: |
| 85 ~TwoPhaseSyncable() override {} |
| 86 |
| 87 // This is before calling CreateChangesetToCurrent to give a change for the |
| 88 // TwoPhaseSyncable to pull the latest changes into its local state. |
| 89 // |
| 90 // The callback |done| should be called once the local instance is ready |
| 91 // to accept the call to CreateChangesetToCurrent. |
| 92 virtual void PreCreateChangesetToCurrent(const VectorClock& from, |
| 93 MandatoryClosure&& done) = 0; |
| 94 |
| 95 // This is called after calling ApplyChangeset to allow the changes to |
| 96 // propagate to the actual external object. |
| 97 // |
| 98 // The callback |done| should be called once the external world object is |
| 99 // updated. |
| 100 virtual void PostApplyChangeset(const VectorClock& from, |
| 101 const VectorClock& to, |
| 102 MandatoryClosure&& done) = 0; |
| 103 }; |
| 104 |
| 105 } // namespace blimp |
| 106 |
| 107 #endif // BLIMP_NET_HELIUM_SYNCABLE_H_ |
| OLD | NEW |