Chromium Code Reviews| Index: sync/internal_api/public/model_type_store_backend.h |
| diff --git a/sync/internal_api/public/model_type_store_backend.h b/sync/internal_api/public/model_type_store_backend.h |
| index e0244a41e19865ed4ca882581f02485eb6f668e3..d93d8cbda9192be76678be644f791ee8970f2a1d 100644 |
| --- a/sync/internal_api/public/model_type_store_backend.h |
| +++ b/sync/internal_api/public/model_type_store_backend.h |
| @@ -7,8 +7,12 @@ |
| #include <memory> |
| #include <string> |
| +#include <unordered_map> |
| +#include "base/gtest_prod_util.h" |
| +#include "base/lazy_instance.h" |
| #include "base/macros.h" |
| +#include "base/synchronization/lock.h" |
| #include "base/threading/thread_collision_warner.h" |
| #include "sync/api/model_type_store.h" |
| #include "sync/base/sync_export.h" |
| @@ -24,26 +28,26 @@ namespace syncer_v2 { |
| // ModelTypeStoreBackend handles operations with leveldb. It is oblivious of the |
| // fact that it is called from separate thread (with the exception of ctor), |
| // meaning it shouldn't deal with callbacks and task_runners. |
| -class SYNC_EXPORT ModelTypeStoreBackend { |
| +class SYNC_EXPORT ModelTypeStoreBackend |
| + : public base::RefCountedThreadSafe<ModelTypeStoreBackend> { |
| public: |
| - ModelTypeStoreBackend(); |
| - ~ModelTypeStoreBackend(); |
| + typedef std::unordered_map<std::string, scoped_refptr<ModelTypeStoreBackend>> |
| + BackendMap; |
| // Helper function to create in memory environment for leveldb. |
| static std::unique_ptr<leveldb::Env> CreateInMemoryEnv(); |
| + static scoped_refptr<ModelTypeStoreBackend> GetOrCreateBackend( |
| + const std::string& path, |
| + leveldb::Env* env, |
| + scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); |
| + static void Disconnect(scoped_refptr<ModelTypeStoreBackend>& backend); |
| + |
| // Take ownership of env from consumer of ModelTypeStoreBackend object. env |
| // will be deleted right after db_ is deleted. This function allows tests to |
| // create in-memory store without requiring them to manage env ownership. |
| void TakeEnvOwnership(std::unique_ptr<leveldb::Env> env); |
| - // Init opens database at |path|. If database doesn't exist it creates one. |
| - // Normally |env| should be nullptr, this causes leveldb to use default disk |
| - // based environment from leveldb::Env::Default(). |
| - // Providing |env| allows to override environment used by leveldb for tests |
| - // with in-memory or faulty environment. |
| - ModelTypeStore::Result Init(const std::string& path, leveldb::Env* env); |
| - |
| // Reads records with keys formed by prepending ids from |id_list| with |
| // |prefix|. If the record is found its id (without prefix) and value is |
| // appended to record_list. If record is not found its id is appended to |
| @@ -65,7 +69,16 @@ class SYNC_EXPORT ModelTypeStoreBackend { |
| ModelTypeStore::Result WriteModifications( |
| std::unique_ptr<leveldb::WriteBatch> write_batch); |
| + ModelTypeStore::Result InitResult() const; |
|
maxbogue
2016/06/23 18:22:22
It might be cleaner to just pass an init callback
Gang Wu
2016/06/27 23:19:39
Now, the callback will return backend object, so t
|
| + |
| private: |
| + friend class base::RefCountedThreadSafe<ModelTypeStoreBackend>; |
|
maxbogue
2016/06/23 18:22:22
Why is this needed? The base class shouldn't need
Gang Wu
2016/06/27 23:19:39
Since dtor is private, this can let scoped_refptr
|
| + friend class ModelTypeStoreBackendTest; |
| + FRIEND_TEST_ALL_PREFIXES(ModelTypeStoreBackendTest, TwoBeckendTest); |
| + |
| + ModelTypeStoreBackend(const std::string& path); |
| + ~ModelTypeStoreBackend(); |
| + |
| // In some scenarios ModelTypeStoreBackend holds ownership of env. Typical |
| // example is when test creates in memory environment with CreateInMemoryEnv |
| // and wants it to be destroyed along with backend. This is achieved by |
| @@ -77,6 +90,21 @@ class SYNC_EXPORT ModelTypeStoreBackend { |
| std::unique_ptr<leveldb::DB> db_; |
| + std::string path_; |
| + |
| + ModelTypeStore::Result init_result_; |
| + |
| + static base::LazyInstance<BackendMap> backend_map_; |
|
maxbogue
2016/06/23 18:22:22
Can you explain why LazyInstance is necessary? Is
Gang Wu
2016/06/27 23:19:39
if not, the compiler will say "declaration require
|
| + |
| + static base::LazyInstance<base::Lock>::Leaky backend_lock_; |
| + |
| + // Init opens database at |path|. If database doesn't exist it creates one. |
| + // Normally |env| should be nullptr, this causes leveldb to use default disk |
| + // based environment from leveldb::Env::Default(). |
| + // Providing |env| allows to override environment used by leveldb for tests |
| + // with in-memory or faulty environment. |
| + void Init(const std::string& path, leveldb::Env* env); |
| + |
| // Macro wrapped mutex to guard against concurrent calls in debug builds. |
| DFAKE_MUTEX(push_pop_); |