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