| Index: sync/internal_api/model_type_store_backend.cc
|
| diff --git a/sync/internal_api/model_type_store_backend.cc b/sync/internal_api/model_type_store_backend.cc
|
| index cba6731d98c9ecff9a1ecbfdeb7307d55d32a4e9..80bf1ad4aaecc3f84aa9c8f18427fe8df776af42 100644
|
| --- a/sync/internal_api/model_type_store_backend.cc
|
| +++ b/sync/internal_api/model_type_store_backend.cc
|
| @@ -6,8 +6,11 @@
|
|
|
| #include <utility>
|
|
|
| +#include "base/bind.h"
|
| #include "base/files/file_path.h"
|
| +#include "base/location.h"
|
| #include "base/memory/ptr_util.h"
|
| +#include "base/sequenced_task_runner.h"
|
| #include "third_party/leveldatabase/env_chromium.h"
|
| #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
|
| #include "third_party/leveldatabase/src/include/leveldb/db.h"
|
| @@ -20,8 +23,16 @@
|
|
|
| namespace syncer_v2 {
|
|
|
| -ModelTypeStoreBackend::ModelTypeStoreBackend() {
|
| -}
|
| +// static
|
| +base::LazyInstance<ModelTypeStoreBackend::BackendMap>
|
| + ModelTypeStoreBackend::backend_map_ = LAZY_INSTANCE_INITIALIZER;
|
| +
|
| +// static
|
| +base::LazyInstance<base::Lock>::Leaky ModelTypeStoreBackend::backend_lock_ =
|
| + LAZY_INSTANCE_INITIALIZER;
|
| +
|
| +ModelTypeStoreBackend::ModelTypeStoreBackend(const std::string& path)
|
| + : path_(path), init_result_(ModelTypeStore::Result::UNINITIALIZED) {}
|
|
|
| ModelTypeStoreBackend::~ModelTypeStoreBackend() {
|
| }
|
| @@ -30,13 +41,46 @@ std::unique_ptr<leveldb::Env> ModelTypeStoreBackend::CreateInMemoryEnv() {
|
| return base::WrapUnique(leveldb::NewMemEnv(leveldb::Env::Default()));
|
| }
|
|
|
| +// static
|
| +scoped_refptr<ModelTypeStoreBackend> ModelTypeStoreBackend::GetOrCreateBackend(
|
| + const std::string& path,
|
| + leveldb::Env* env,
|
| + scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) {
|
| + base::AutoLock lock(backend_lock_.Get());
|
| + if (backend_map_.Get().find(path) != backend_map_.Get().end()) {
|
| + return backend_map_.Get()[path];
|
| + }
|
| +
|
| + backend_map_.Get()[path] = new ModelTypeStoreBackend(path);
|
| +
|
| + DCHECK(blocking_task_runner);
|
| + blocking_task_runner->PostTask(
|
| + FROM_HERE, base::Bind(&ModelTypeStoreBackend::Init,
|
| + backend_map_.Get()[path], path, env));
|
| +
|
| + return backend_map_.Get()[path];
|
| +}
|
| +
|
| +// static
|
| +void ModelTypeStoreBackend::Disconnect(
|
| + scoped_refptr<ModelTypeStoreBackend>& backend) {
|
| + base::AutoLock lock(backend_lock_.Get());
|
| + if (backend == NULL)
|
| + return;
|
| + std::string path = backend->path_;
|
| + backend = NULL;
|
| + if (backend_map_.Get().find(path) != backend_map_.Get().end() &&
|
| + backend_map_.Get()[path]->HasOneRef()) {
|
| + backend_map_.Get().erase(path);
|
| + }
|
| +}
|
| +
|
| void ModelTypeStoreBackend::TakeEnvOwnership(
|
| std::unique_ptr<leveldb::Env> env) {
|
| env_ = std::move(env);
|
| }
|
|
|
| -ModelTypeStore::Result ModelTypeStoreBackend::Init(const std::string& path,
|
| - leveldb::Env* env) {
|
| +void ModelTypeStoreBackend::Init(const std::string& path, leveldb::Env* env) {
|
| DFAKE_SCOPED_LOCK(push_pop_);
|
| leveldb::DB* db_raw = nullptr;
|
|
|
| @@ -49,10 +93,11 @@ ModelTypeStore::Result ModelTypeStoreBackend::Init(const std::string& path,
|
| leveldb::Status status = leveldb::DB::Open(options, path, &db_raw);
|
| if (!status.ok()) {
|
| DCHECK(db_raw == nullptr);
|
| - return ModelTypeStore::Result::UNSPECIFIED_ERROR;
|
| + init_result_ = ModelTypeStore::Result::UNSPECIFIED_ERROR;
|
| + } else {
|
| + db_.reset(db_raw);
|
| + init_result_ = ModelTypeStore::Result::SUCCESS;
|
| }
|
| - db_.reset(db_raw);
|
| - return ModelTypeStore::Result::SUCCESS;
|
| }
|
|
|
| ModelTypeStore::Result ModelTypeStoreBackend::ReadRecordsWithPrefix(
|
| @@ -114,4 +159,8 @@ ModelTypeStore::Result ModelTypeStoreBackend::WriteModifications(
|
| : ModelTypeStore::Result::UNSPECIFIED_ERROR;
|
| }
|
|
|
| +ModelTypeStore::Result ModelTypeStoreBackend::InitResult() const {
|
| + return init_result_;
|
| +}
|
| +
|
| } // namespace syncer_v2
|
|
|