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_INTERNAL_API_PUBLIC_MODEL_TYPE_STORE_IMPL_H_ | |
6 #define SYNC_INTERNAL_API_PUBLIC_MODEL_TYPE_STORE_IMPL_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/threading/non_thread_safe.h" | |
14 #include "sync/api/model_type_store.h" | |
15 #include "sync/internal_api/public/base/model_type.h" | |
16 | |
17 namespace leveldb { | |
18 class WriteBatch; | |
19 } // namespace leveldb | |
20 | |
21 namespace syncer_v2 { | |
22 | |
23 class ModelTypeStoreBackend; | |
24 | |
25 // ModelTypeStoreImpl handles details of store initialization, threading and | |
26 // leveldb key formatting. Actual leveldb IO calls are performed by | |
27 // ModelTypeStoreBackend. | |
28 class ModelTypeStoreImpl : public ModelTypeStore, public base::NonThreadSafe { | |
29 public: | |
30 ~ModelTypeStoreImpl() override; | |
31 | |
32 static void CreateStore( | |
33 const syncer::ModelType type, | |
34 const std::string& path, | |
35 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, | |
36 const InitCallback& callback); | |
37 static void CreateInMemoryStoreForTest(const InitCallback& callback); | |
38 | |
39 // ModelTypeStore implementation. | |
40 void ReadData(const IdList& id_list, | |
41 const ReadDataCallback& callback) override; | |
42 void ReadAllData(const ReadAllDataCallback& callback) override; | |
43 void ReadAllMetadata(const ReadMetadataCallback& callback) override; | |
44 std::unique_ptr<WriteBatch> CreateWriteBatch() override; | |
45 void CommitWriteBatch(std::unique_ptr<WriteBatch> write_batch, | |
46 const CallbackWithResult& callback) override; | |
47 void WriteData(WriteBatch* write_batch, | |
48 const std::string& id, | |
49 const std::string& value) override; | |
50 void WriteMetadata(WriteBatch* write_batch, | |
51 const std::string& id, | |
52 const std::string& value) override; | |
53 void WriteGlobalMetadata(WriteBatch* write_batch, | |
54 const std::string& value) override; | |
55 void DeleteData(WriteBatch* write_batch, const std::string& id) override; | |
56 void DeleteMetadata(WriteBatch* write_batch, const std::string& id) override; | |
57 void DeleteGlobalMetadata(WriteBatch* write_batch) override; | |
58 | |
59 private: | |
60 class WriteBatchImpl : public WriteBatch { | |
61 public: | |
62 WriteBatchImpl(); | |
63 ~WriteBatchImpl() override; | |
64 std::unique_ptr<leveldb::WriteBatch> leveldb_write_batch_; | |
65 }; | |
66 | |
67 static void BackendInitDone( | |
68 const syncer::ModelType type, | |
69 std::unique_ptr<Result> result, | |
70 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, | |
71 const InitCallback& callback, | |
72 scoped_refptr<ModelTypeStoreBackend> backend); | |
73 | |
74 // Format prefix key for data/metadata records with |type|. | |
75 static std::string FormatDataPrefix(const syncer::ModelType type); | |
76 static std::string FormatMetaPrefix(const syncer::ModelType type); | |
77 | |
78 static leveldb::WriteBatch* GetLeveldbWriteBatch(WriteBatch* write_batch); | |
79 | |
80 // Format key for data/metadata records with given id. | |
81 std::string FormatDataKey(const std::string& id); | |
82 std::string FormatMetadataKey(const std::string& id); | |
83 | |
84 ModelTypeStoreImpl( | |
85 const syncer::ModelType type, | |
86 scoped_refptr<ModelTypeStoreBackend> backend, | |
87 scoped_refptr<base::SequencedTaskRunner> backend_task_runner); | |
88 | |
89 // Callbacks for different calls to ModelTypeStoreBackend. | |
90 void ReadDataDone(const ReadDataCallback& callback, | |
91 std::unique_ptr<RecordList> record_list, | |
92 std::unique_ptr<IdList> missing_id_list, | |
93 Result result); | |
94 void ReadAllDataDone(const ReadAllDataCallback& callback, | |
95 std::unique_ptr<RecordList> record_list, | |
96 Result result); | |
97 void ReadMetadataRecordsDone(const ReadMetadataCallback& callback, | |
98 std::unique_ptr<RecordList> metadata_records, | |
99 Result result); | |
100 void ReadAllMetadataDone(const ReadMetadataCallback& callback, | |
101 std::unique_ptr<RecordList> metadata_records, | |
102 std::unique_ptr<RecordList> global_metadata_records, | |
103 std::unique_ptr<IdList> missing_id_list, | |
104 Result result); | |
105 void WriteModificationsDone(const CallbackWithResult& callback, | |
106 Result result); | |
107 | |
108 // Backend should be deleted on backend thread. | |
109 // To accomplish this store's dtor posts task to backend thread passing | |
110 // backend ownership to task parameter. | |
111 scoped_refptr<ModelTypeStoreBackend> backend_; | |
112 scoped_refptr<base::SequencedTaskRunner> backend_task_runner_; | |
113 | |
114 // Key prefix for data/metadata records of this model type. | |
115 const std::string data_prefix_; | |
116 const std::string metadata_prefix_; | |
117 | |
118 base::WeakPtrFactory<ModelTypeStoreImpl> weak_ptr_factory_; | |
119 }; | |
120 | |
121 } // namespace syncer_v2 | |
122 | |
123 #endif // SYNC_INTERNAL_API_PUBLIC_MODEL_TYPE_STORE_IMPL_H_ | |
OLD | NEW |