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_BACKEND_H_ | |
6 #define SYNC_INTERNAL_API_PUBLIC_MODEL_TYPE_STORE_BACKEND_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 #include <unordered_map> | |
11 | |
12 #include "base/lazy_instance.h" | |
13 #include "base/macros.h" | |
14 #include "base/threading/thread_collision_warner.h" | |
15 #include "sync/api/model_type_store.h" | |
16 #include "sync/base/sync_export.h" | |
17 | |
18 namespace leveldb { | |
19 class DB; | |
20 class Env; | |
21 class WriteBatch; | |
22 } // namespace leveldb | |
23 | |
24 namespace syncer_v2 { | |
25 | |
26 // ModelTypeStoreBackend handles operations with leveldb. It is oblivious of the | |
27 // fact that it is called from separate thread (with the exception of ctor), | |
28 // meaning it shouldn't deal with callbacks and task_runners. | |
29 class SYNC_EXPORT ModelTypeStoreBackend | |
30 : public base::RefCountedThreadSafe<ModelTypeStoreBackend> { | |
31 public: | |
32 typedef std::unordered_map<std::string, ModelTypeStoreBackend*> BackendMap; | |
33 | |
34 // Helper function to create in memory environment for leveldb. | |
35 static std::unique_ptr<leveldb::Env> CreateInMemoryEnv(); | |
36 | |
37 // GetOrCreateBackend will check if |backend_map_| has a backend with same | |
38 // |path|. If |backend_map_| has one, wrap that backend to scoped_refptr and | |
39 // return. If |backend_map_| does not have, create a backend and store it into | |
40 // |backend_map_|. | |
41 static scoped_refptr<ModelTypeStoreBackend> GetOrCreateBackend( | |
42 const std::string& path, | |
43 std::unique_ptr<leveldb::Env> env, | |
44 ModelTypeStore::Result* result); | |
45 | |
46 // Reads records with keys formed by prepending ids from |id_list| with | |
47 // |prefix|. If the record is found its id (without prefix) and value is | |
48 // appended to record_list. If record is not found its id is appended to | |
49 // |missing_id_list|. It is not an error that records for ids are not found so | |
50 // function will still return success in this case. | |
51 ModelTypeStore::Result ReadRecordsWithPrefix( | |
52 const std::string& prefix, | |
53 const ModelTypeStore::IdList& id_list, | |
54 ModelTypeStore::RecordList* record_list, | |
55 ModelTypeStore::IdList* missing_id_list); | |
56 | |
57 // Reads all records with keys starting with |prefix|. Prefix is removed from | |
58 // key before it is added to |record_list|. | |
59 ModelTypeStore::Result ReadAllRecordsWithPrefix( | |
60 const std::string& prefix, | |
61 ModelTypeStore::RecordList* record_list); | |
62 | |
63 // Writes modifications accumulated in |write_batch| to database. | |
64 ModelTypeStore::Result WriteModifications( | |
65 std::unique_ptr<leveldb::WriteBatch> write_batch); | |
66 | |
67 private: | |
68 friend class base::RefCountedThreadSafe<ModelTypeStoreBackend>; | |
69 friend class ModelTypeStoreBackendTest; | |
70 | |
71 ModelTypeStoreBackend(const std::string& path); | |
72 ~ModelTypeStoreBackend(); | |
73 | |
74 // In some scenarios ModelTypeStoreBackend holds ownership of env. Typical | |
75 // example is when test creates in memory environment with CreateInMemoryEnv | |
76 // and wants it to be destroyed along with backend. This is achieved by | |
77 // passing ownership of env to TakeEnvOwnership function. | |
78 // | |
79 // env_ declaration should appear before declaration of db_ because | |
80 // environment object should still be valid when db_'s destructor is called. | |
81 std::unique_ptr<leveldb::Env> env_; | |
82 | |
83 std::unique_ptr<leveldb::DB> db_; | |
84 | |
85 std::string path_; | |
86 | |
87 // backend_map_ holds raw pointer of backend, and when stores ask for backend, | |
88 // GetOrCreateBackend will return scoped_refptr of backend. backend_map_ | |
89 // doesn't take reference to backend, therefore doesn't block backend | |
90 // destruction. | |
91 static base::LazyInstance<BackendMap> backend_map_; | |
92 | |
93 // Init opens database at |path|. If database doesn't exist it creates one. | |
94 // Normally |env| should be nullptr, this causes leveldb to use default disk | |
95 // based environment from leveldb::Env::Default(). | |
96 // Providing |env| allows to override environment used by leveldb for tests | |
97 // with in-memory or faulty environment. | |
98 ModelTypeStore::Result Init(const std::string& path, | |
99 std::unique_ptr<leveldb::Env> env); | |
100 | |
101 // Macro wrapped mutex to guard against concurrent calls in debug builds. | |
102 DFAKE_MUTEX(push_pop_); | |
103 | |
104 DISALLOW_COPY_AND_ASSIGN(ModelTypeStoreBackend); | |
105 }; | |
106 | |
107 } // namespace syncer_v2 | |
108 | |
109 #endif // SYNC_INTERNAL_API_PUBLIC_MODEL_TYPE_STORE_BACKEND_H_ | |
OLD | NEW |