| 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_HELIUM_SYNCABLE_H_ | |
| 6 #define BLIMP_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/helium/result.h" | |
| 14 #include "blimp/helium/version_vector.h" | |
| 15 | |
| 16 namespace blimp { | |
| 17 namespace helium { | |
| 18 | |
| 19 // LazySyncable is something that supports creating and restoring Changesets. | |
| 20 // These objects exchange Changesets between the client and server to keep | |
| 21 // their peer counterparts eventually synchronized. | |
| 22 // | |
| 23 // It provides the following things: | |
| 24 // 1. Mapping: There is a one-to-one relationship between instances on the | |
| 25 // client and instances on the engine. | |
| 26 // 2. Consistency: The values stored in client-engine pairs should eventually be | |
| 27 // equal. | |
| 28 // | |
| 29 // LazySyncable is a base interface that is used for both self contained objects | |
| 30 // (i.e. simple register) and objects which are disconnected replicas | |
| 31 // of external state. In the latter case, the method PrepareToCreateChangeset() | |
| 32 // is used to tell the LazySyncable to replicate its state asynchronously. | |
| 33 template <typename ChangesetType> | |
| 34 class LazySyncable { | |
| 35 public: | |
| 36 // Easy access to the type through which Changesets are serialized for this | |
| 37 // Syncable. | |
| 38 using Changeset = ChangesetType; | |
| 39 | |
| 40 virtual ~LazySyncable() = default; | |
| 41 | |
| 42 // Serializes a description of changes between |from| and the current state | |
| 43 // into the specified |output| changeset. Note that the Sync layer will use | |
| 44 // GetRevision() to determine the end-revision for the Changeset, so the | |
| 45 // Syncable must ensure that GetRevision() is incremented at least on the next | |
| 46 // modification after this call. | |
| 47 virtual std::unique_ptr<ChangesetType> CreateChangeset( | |
| 48 Revision from) const = 0; | |
| 49 | |
| 50 // Reads and applies a Changeset originating from the peer counterpart of | |
| 51 // this Syncable. Failure to apply the changeset should result in a CHECK | |
| 52 // failure. | |
| 53 virtual void ApplyChangeset(const ChangesetType& changeset) = 0; | |
| 54 | |
| 55 // Sets the callback that the Syncable should invoke immediately after being | |
| 56 // modified | |
| 57 // locally (e.g. LwwRegister::Set() will call it before returning). | |
| 58 virtual void SetLocalUpdateCallback( | |
| 59 const base::Closure& local_update_callback) = 0; | |
| 60 | |
| 61 // Returns true if |changeset| is semantically correct and can be | |
| 62 // Applied() to the state of |this|. | |
| 63 virtual bool ValidateChangeset(const ChangesetType& changeset) const { | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 // Allows the Syncable to release book-keeping state related to Revisions | |
| 68 // prior to |before|. State relating to Revisions |before| and later must be | |
| 69 // retained. | |
| 70 virtual void ReleaseBefore(Revision checkpoint) = 0; | |
| 71 | |
| 72 // Returns the current Revision of this Syncable. If the returned Revision is | |
| 73 // zero then the Syncable is newly-created and has not yet been modified. | |
| 74 virtual Revision GetRevision() const = 0; | |
| 75 | |
| 76 // Called when the Sync layer is ready to request a Changeset for this | |
| 77 // Syncable, to allow for external state to be pulled into the Syncable ready | |
| 78 // for synchronous serialization. | |
| 79 virtual void PrepareToCreateChangeset(Revision from, base::Closure done) = 0; | |
| 80 }; | |
| 81 | |
| 82 template <typename ChangesetType> | |
| 83 class Syncable : public LazySyncable<ChangesetType> { | |
| 84 public: | |
| 85 // LazySyncable implementation. | |
| 86 void PrepareToCreateChangeset(Revision from, base::Closure done) override { | |
| 87 // Default no-op implementation. | |
| 88 done.Run(); | |
| 89 } | |
| 90 }; | |
| 91 | |
| 92 } // namespace helium | |
| 93 } // namespace blimp | |
| 94 | |
| 95 #endif // BLIMP_HELIUM_SYNCABLE_H_ | |
| OLD | NEW |