OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/leveldb/leveldb_service_impl.h" | 5 #include "components/leveldb/leveldb_service_impl.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "components/leveldb/env_mojo.h" | 10 #include "components/leveldb/env_mojo.h" |
11 #include "components/leveldb/leveldb_database_impl.h" | 11 #include "components/leveldb/leveldb_database_impl.h" |
12 #include "components/leveldb/public/cpp/util.h" | 12 #include "components/leveldb/public/cpp/util.h" |
13 #include "mojo/public/cpp/bindings/strong_associated_binding.h" | 13 #include "mojo/public/cpp/bindings/strong_associated_binding.h" |
14 #include "third_party/leveldatabase/env_chromium.h" | 14 #include "third_party/leveldatabase/env_chromium.h" |
15 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 15 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
16 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 16 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
17 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 17 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
18 #include "third_party/leveldatabase/src/include/leveldb/filter_policy.h" | 18 #include "third_party/leveldatabase/src/include/leveldb/filter_policy.h" |
19 #include "third_party/leveldatabase/src/include/leveldb/slice.h" | 19 #include "third_party/leveldatabase/src/include/leveldb/slice.h" |
20 | 20 |
21 namespace leveldb { | 21 namespace leveldb { |
22 | 22 |
23 LevelDBServiceImpl::LevelDBServiceImpl( | 23 LevelDBServiceImpl::LevelDBServiceImpl( |
24 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) | 24 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) |
25 : thread_(new LevelDBMojoProxy(std::move(file_task_runner))), | 25 : thread_(new LevelDBMojoProxy(std::move(file_task_runner))) {} |
26 environment_name_("LevelDBEnv") {} | |
27 | 26 |
28 LevelDBServiceImpl::~LevelDBServiceImpl() {} | 27 LevelDBServiceImpl::~LevelDBServiceImpl() {} |
29 | 28 |
30 void LevelDBServiceImpl::SetEnvironmentName(const std::string& name) { | |
31 environment_name_ = name; | |
32 } | |
33 | |
34 void LevelDBServiceImpl::Open( | 29 void LevelDBServiceImpl::Open( |
35 filesystem::mojom::DirectoryPtr directory, | 30 filesystem::mojom::DirectoryPtr directory, |
36 const std::string& dbname, | 31 const std::string& dbname, |
37 leveldb::mojom::LevelDBDatabaseAssociatedRequest database, | 32 leveldb::mojom::LevelDBDatabaseAssociatedRequest database, |
38 const OpenCallback& callback) { | 33 const OpenCallback& callback) { |
39 OpenWithOptions(leveldb::mojom::OpenOptions::New(), std::move(directory), | 34 OpenWithOptions(leveldb::mojom::OpenOptions::New(), std::move(directory), |
40 dbname, std::move(database), callback); | 35 dbname, std::move(database), callback); |
41 } | 36 } |
42 | 37 |
43 void LevelDBServiceImpl::OpenWithOptions( | 38 void LevelDBServiceImpl::OpenWithOptions( |
44 leveldb::mojom::OpenOptionsPtr open_options, | 39 leveldb::mojom::OpenOptionsPtr open_options, |
45 filesystem::mojom::DirectoryPtr directory, | 40 filesystem::mojom::DirectoryPtr directory, |
46 const std::string& dbname, | 41 const std::string& dbname, |
47 leveldb::mojom::LevelDBDatabaseAssociatedRequest database, | 42 leveldb::mojom::LevelDBDatabaseAssociatedRequest database, |
48 const OpenCallback& callback) { | 43 const OpenCallback& callback) { |
49 leveldb::Options options; | 44 leveldb::Options options; |
50 options.create_if_missing = open_options->create_if_missing; | 45 options.create_if_missing = open_options->create_if_missing; |
51 options.error_if_exists = open_options->error_if_exists; | 46 options.error_if_exists = open_options->error_if_exists; |
52 options.paranoid_checks = open_options->paranoid_checks; | 47 options.paranoid_checks = open_options->paranoid_checks; |
53 options.write_buffer_size = open_options->write_buffer_size; | 48 options.write_buffer_size = open_options->write_buffer_size; |
54 options.max_open_files = open_options->max_open_files; | 49 options.max_open_files = open_options->max_open_files; |
55 | 50 |
56 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 51 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
57 options.compression = leveldb::kSnappyCompression; | 52 options.compression = leveldb::kSnappyCompression; |
58 | 53 |
59 // Register our directory with the file thread. | 54 // Register our directory with the file thread. |
60 LevelDBMojoProxy::OpaqueDir* dir = | 55 LevelDBMojoProxy::OpaqueDir* dir = |
61 thread_->RegisterDirectory(std::move(directory)); | 56 thread_->RegisterDirectory(std::move(directory)); |
62 | 57 |
63 std::unique_ptr<MojoEnv> env_mojo( | 58 std::unique_ptr<MojoEnv> env_mojo(new MojoEnv(thread_, dir)); |
64 new MojoEnv(environment_name_, thread_, dir)); | |
65 options.env = env_mojo.get(); | 59 options.env = env_mojo.get(); |
66 | 60 |
67 leveldb::DB* db = nullptr; | 61 leveldb::DB* db = nullptr; |
68 leveldb::Status s = leveldb::DB::Open(options, dbname, &db); | 62 leveldb::Status s = leveldb::DB::Open(options, dbname, &db); |
69 | 63 |
70 if (s.ok()) { | 64 if (s.ok()) { |
71 mojo::MakeStrongAssociatedBinding( | 65 mojo::MakeStrongAssociatedBinding( |
72 base::MakeUnique<LevelDBDatabaseImpl>(std::move(env_mojo), | 66 base::MakeUnique<LevelDBDatabaseImpl>(std::move(env_mojo), |
73 base::WrapUnique(db)), | 67 base::WrapUnique(db)), |
74 std::move(database)); | 68 std::move(database)); |
(...skipping 25 matching lines...) Expand all Loading... |
100 callback.Run(LeveldbStatusToError(s)); | 94 callback.Run(LeveldbStatusToError(s)); |
101 } | 95 } |
102 | 96 |
103 void LevelDBServiceImpl::Destroy(filesystem::mojom::DirectoryPtr directory, | 97 void LevelDBServiceImpl::Destroy(filesystem::mojom::DirectoryPtr directory, |
104 const std::string& dbname, | 98 const std::string& dbname, |
105 const DestroyCallback& callback) { | 99 const DestroyCallback& callback) { |
106 leveldb::Options options; | 100 leveldb::Options options; |
107 // Register our directory with the file thread. | 101 // Register our directory with the file thread. |
108 LevelDBMojoProxy::OpaqueDir* dir = | 102 LevelDBMojoProxy::OpaqueDir* dir = |
109 thread_->RegisterDirectory(std::move(directory)); | 103 thread_->RegisterDirectory(std::move(directory)); |
110 std::unique_ptr<MojoEnv> env_mojo( | 104 std::unique_ptr<MojoEnv> env_mojo(new MojoEnv(thread_, dir)); |
111 new MojoEnv(environment_name_, thread_, dir)); | |
112 options.env = env_mojo.get(); | 105 options.env = env_mojo.get(); |
113 callback.Run(LeveldbStatusToError(leveldb::DestroyDB(dbname, options))); | 106 callback.Run(LeveldbStatusToError(leveldb::DestroyDB(dbname, options))); |
114 } | 107 } |
115 | 108 |
116 } // namespace leveldb | 109 } // namespace leveldb |
OLD | NEW |