| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 COMPONENTS_SYNC_API_MODEL_TYPE_STORE_H_ | |
| 6 #define COMPONENTS_SYNC_API_MODEL_TYPE_STORE_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "components/sync/base/model_type.h" | |
| 15 | |
| 16 namespace base { | |
| 17 class SequencedTaskRunner; | |
| 18 } // namespace base | |
| 19 | |
| 20 namespace syncer { | |
| 21 | |
| 22 // ModelTypeStore is leveldb backed store for model type's data, metadata and | |
| 23 // global metadata. | |
| 24 // | |
| 25 // Store keeps records for entries identified by ids. For each entry store keeps | |
| 26 // data and metadata. Also store keeps one record for global metadata. | |
| 27 // | |
| 28 // To create store call one of Create*Store static factory functions. Model type | |
| 29 // controls store's lifetime with returned unique_ptr. Call to Create*Store | |
| 30 // function triggers asynchronous store backend initialization, callback will be | |
| 31 // called with results when initialization is done. | |
| 32 // | |
| 33 // Read operations are asynchronous, initiated with one of Read* functions, | |
| 34 // provided callback will be called with result code and output of read | |
| 35 // operation. | |
| 36 // | |
| 37 // Write operations are done in context of write batch. To get one call | |
| 38 // CreateWriteBatch(). After that pass write batch object to Write/Delete | |
| 39 // functions. WriteBatch only accumulates pending changes, doesn't actually do | |
| 40 // data modification. Calling CommitWriteBatch writes all accumulated changes to | |
| 41 // disk atomically. Callback passed to CommitWriteBatch will be called with | |
| 42 // result of write operation. If write batch object is destroyed without | |
| 43 // comitting accumulated write operations will not be persisted. | |
| 44 // | |
| 45 // Destroying store object doesn't necessarily cancel asynchronous operations | |
| 46 // issued previously. You should be prepared to handle callbacks from those | |
| 47 // operations. | |
| 48 class ModelTypeStore { | |
| 49 public: | |
| 50 // Result of store operations. | |
| 51 enum class Result { | |
| 52 SUCCESS, | |
| 53 UNSPECIFIED_ERROR, | |
| 54 }; | |
| 55 | |
| 56 // Output of read operations is passed back as list of Record structures. | |
| 57 struct Record { | |
| 58 Record(const std::string& id, const std::string& value) | |
| 59 : id(id), value(value) {} | |
| 60 | |
| 61 std::string id; | |
| 62 std::string value; | |
| 63 }; | |
| 64 | |
| 65 // WriteBatch object is used in all modification operations. | |
| 66 class WriteBatch { | |
| 67 public: | |
| 68 virtual ~WriteBatch(); | |
| 69 | |
| 70 protected: | |
| 71 friend class MockModelTypeStore; | |
| 72 WriteBatch(); | |
| 73 }; | |
| 74 | |
| 75 typedef std::vector<Record> RecordList; | |
| 76 typedef std::vector<std::string> IdList; | |
| 77 | |
| 78 typedef base::Callback<void(Result result, | |
| 79 std::unique_ptr<ModelTypeStore> store)> | |
| 80 InitCallback; | |
| 81 typedef base::Callback<void(Result result)> CallbackWithResult; | |
| 82 typedef base::Callback<void(Result result, | |
| 83 std::unique_ptr<RecordList> data_records, | |
| 84 std::unique_ptr<IdList> missing_id_list)> | |
| 85 ReadDataCallback; | |
| 86 typedef base::Callback<void(Result result, | |
| 87 std::unique_ptr<RecordList> data_records)> | |
| 88 ReadAllDataCallback; | |
| 89 typedef base::Callback<void(Result result, | |
| 90 std::unique_ptr<RecordList> metadata_records, | |
| 91 const std::string& global_metadata)> | |
| 92 ReadMetadataCallback; | |
| 93 | |
| 94 // CreateStore takes |path| and |blocking_task_runner|. Here is how to get | |
| 95 // task runner in production code: | |
| 96 // | |
| 97 // base::SequencedWorkerPool* worker_pool = | |
| 98 // content::BrowserThread::GetBlockingPool(); | |
| 99 // scoped_refptr<base::SequencedTaskRunner> blocking_task_runner( | |
| 100 // worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 101 // worker_pool->GetSequenceToken(), | |
| 102 // base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); | |
| 103 // | |
| 104 // In test get task runner from MessageLoop::task_runner(). | |
| 105 static void CreateStore( | |
| 106 const ModelType type, | |
| 107 const std::string& path, | |
| 108 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, | |
| 109 const InitCallback& callback); | |
| 110 // Creates store object backed by in-memory leveldb database. It is used in | |
| 111 // tests. | |
| 112 static void CreateInMemoryStoreForTest(const InitCallback& callback); | |
| 113 | |
| 114 virtual ~ModelTypeStore(); | |
| 115 | |
| 116 // Read operations return records either for all entries or only for ones | |
| 117 // identified in |id_list|. Result is SUCCESS if all records were read | |
| 118 // successfully. If reading any of records fails result is UNSPECIFIED_ERROR | |
| 119 // and RecordList contains some records that were read successfully. There is | |
| 120 // no guarantee that RecordList will contain all successfully read records in | |
| 121 // this case. | |
| 122 // Callback for ReadData (ReadDataCallback) in addition receives list of ids | |
| 123 // that were not found in store (missing_id_list). | |
| 124 virtual void ReadData(const IdList& id_list, | |
| 125 const ReadDataCallback& callback) = 0; | |
| 126 virtual void ReadAllData(const ReadAllDataCallback& callback) = 0; | |
| 127 // ReadMetadataCallback will be invoked with three parameters: result of | |
| 128 // operation, list of metadata records and global metadata. | |
| 129 virtual void ReadAllMetadata(const ReadMetadataCallback& callback) = 0; | |
| 130 | |
| 131 // Creates write batch for write operations. | |
| 132 virtual std::unique_ptr<WriteBatch> CreateWriteBatch() = 0; | |
| 133 | |
| 134 // Commits write operations accumulated in write batch. If write operation | |
| 135 // fails result is UNSPECIFIED_ERROR and write operations will not be | |
| 136 // reflected in the store. | |
| 137 virtual void CommitWriteBatch(std::unique_ptr<WriteBatch> write_batch, | |
| 138 const CallbackWithResult& callback) = 0; | |
| 139 | |
| 140 // Write operations. | |
| 141 virtual void WriteData(WriteBatch* write_batch, | |
| 142 const std::string& id, | |
| 143 const std::string& value) = 0; | |
| 144 virtual void WriteMetadata(WriteBatch* write_batch, | |
| 145 const std::string& id, | |
| 146 const std::string& value) = 0; | |
| 147 virtual void WriteGlobalMetadata(WriteBatch* write_batch, | |
| 148 const std::string& value) = 0; | |
| 149 virtual void DeleteData(WriteBatch* write_batch, const std::string& id) = 0; | |
| 150 virtual void DeleteMetadata(WriteBatch* write_batch, | |
| 151 const std::string& id) = 0; | |
| 152 virtual void DeleteGlobalMetadata(WriteBatch* write_batch) = 0; | |
| 153 // TODO(pavely): Consider implementing DeleteAllMetadata with following | |
| 154 // signature: | |
| 155 // virtual void DeleteAllMetadata(const CallbackWithResult& callback) = 0. | |
| 156 // It will delete all metadata records and global metadata record. | |
| 157 }; | |
| 158 | |
| 159 } // namespace syncer | |
| 160 | |
| 161 #endif // COMPONENTS_SYNC_API_MODEL_TYPE_STORE_H_ | |
| OLD | NEW |