[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 1 | // Copyright 2014 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 COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_H_ |
| 6 | #define COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_H_ |
| 7 | |
Troy Hildebrandt | d3c43f67 | 2018-09-28 19:20:12 | [diff] [blame] | 8 | #include <map> |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 11 | #include "base/sequenced_task_runner.h" |
| 12 | #include "base/threading/thread_checker.h" |
Min Qin | 7f0cff2 | 2018-05-24 20:06:57 | [diff] [blame] | 13 | #include "components/leveldb_proto/leveldb_database.h" |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 14 | #include "components/leveldb_proto/proto_leveldb_wrapper.h" |
peter | 7cdc93c | 2015-07-30 14:03:14 | [diff] [blame] | 15 | |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 16 | namespace leveldb_proto { |
| 17 | |
| 18 | // Interface for classes providing persistent storage of Protocol Buffer |
| 19 | // entries (T must be a Proto type extending MessageLite). |
| 20 | template <typename T> |
| 21 | class ProtoDatabase { |
| 22 | public: |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 23 | using InitCallback = base::OnceCallback<void(bool success)>; |
| 24 | using UpdateCallback = base::OnceCallback<void(bool success)>; |
peter | 7cdc93c | 2015-07-30 14:03:14 | [diff] [blame] | 25 | using LoadCallback = |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 26 | base::OnceCallback<void(bool success, std::unique_ptr<std::vector<T>>)>; |
tschumann | 108e9c2 | 2016-10-04 13:34:57 | [diff] [blame] | 27 | using LoadKeysCallback = |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 28 | base::OnceCallback<void(bool success, |
| 29 | std::unique_ptr<std::vector<std::string>>)>; |
Troy Hildebrandt | d3c43f67 | 2018-09-28 19:20:12 | [diff] [blame] | 30 | using LoadKeysAndEntriesCallback = |
| 31 | base::OnceCallback<void(bool success, |
| 32 | std::unique_ptr<std::map<std::string, T>>)>; |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 33 | using GetCallback = |
| 34 | base::OnceCallback<void(bool success, std::unique_ptr<T>)>; |
| 35 | using DestroyCallback = base::OnceCallback<void(bool success)>; |
peter | 7cdc93c | 2015-07-30 14:03:14 | [diff] [blame] | 36 | |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 37 | // A list of key-value (string, T) tuples. |
peter | 7cdc93c | 2015-07-30 14:03:14 | [diff] [blame] | 38 | using KeyEntryVector = std::vector<std::pair<std::string, T>>; |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 39 | |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 40 | explicit ProtoDatabase( |
| 41 | const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 42 | : db_wrapper_(std::make_unique<ProtoLevelDBWrapper>(task_runner)) {} |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 43 | virtual ~ProtoDatabase() {} |
| 44 | |
Chris Mumford | 29f2cf5 | 2017-09-21 00:35:40 | [diff] [blame] | 45 | // Asynchronously initializes the object with the specified |options|. |
| 46 | // |callback| will be invoked on the calling thread when complete. |
Troy Hildebrandt | 78e6d870 | 2018-11-05 17:09:47 | [diff] [blame] | 47 | virtual void Init(const std::string& client_name, |
| 48 | typename ProtoDatabase<T>::InitCallback callback) = 0; |
| 49 | // This version of Init is for compatibility, since many of the current |
| 50 | // proto database clients still use this. |
Chris Mumford | 29f2cf5 | 2017-09-21 00:35:40 | [diff] [blame] | 51 | virtual void Init(const char* client_name, |
| 52 | const base::FilePath& database_dir, |
| 53 | const leveldb_env::Options& options, |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 54 | typename ProtoDatabase<T>::InitCallback callback) = 0; |
| 55 | |
| 56 | virtual void InitWithDatabase(LevelDB* database, |
| 57 | const base::FilePath& database_dir, |
| 58 | const leveldb_env::Options& options, |
| 59 | InitCallback callback) { |
| 60 | db_wrapper_->InitWithDatabase(database, database_dir, options, |
| 61 | std::move(callback)); |
| 62 | } |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 63 | |
| 64 | // Asynchronously saves |entries_to_save| and deletes entries from |
| 65 | // |keys_to_remove| from the database. |callback| will be invoked on the |
| 66 | // calling thread when complete. |
| 67 | virtual void UpdateEntries( |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 68 | std::unique_ptr<KeyEntryVector> entries_to_save, |
| 69 | std::unique_ptr<std::vector<std::string>> keys_to_remove, |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 70 | UpdateCallback callback) { |
| 71 | db_wrapper_->template UpdateEntries<T>(std::move(entries_to_save), |
| 72 | std::move(keys_to_remove), |
| 73 | std::move(callback)); |
| 74 | } |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 75 | |
Gang Wu | b29235f | 2018-06-11 19:24:30 | [diff] [blame] | 76 | // Asynchronously saves |entries_to_save| and deletes entries that satisfies |
| 77 | // the |delete_key_filter| from the database. |callback| will be invoked on |
| 78 | // the calling thread when complete. The filter will be called on |
| 79 | // ProtoDatabase's taskrunner. |
| 80 | virtual void UpdateEntriesWithRemoveFilter( |
| 81 | std::unique_ptr<KeyEntryVector> entries_to_save, |
| 82 | const LevelDB::KeyFilter& delete_key_filter, |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 83 | UpdateCallback callback) { |
| 84 | db_wrapper_->template UpdateEntriesWithRemoveFilter<T>( |
| 85 | std::move(entries_to_save), delete_key_filter, std::move(callback)); |
| 86 | } |
Gang Wu | b29235f | 2018-06-11 19:24:30 | [diff] [blame] | 87 | |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 88 | // Asynchronously loads all entries from the database and invokes |callback| |
| 89 | // when complete. |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 90 | virtual void LoadEntries(LoadCallback callback) { |
| 91 | db_wrapper_->template LoadEntries<T>(std::move(callback)); |
| 92 | } |
jianli | 23248b4 | 2015-10-27 23:56:34 | [diff] [blame] | 93 | |
Min Qin | 7f0cff2 | 2018-05-24 20:06:57 | [diff] [blame] | 94 | // Asynchronously loads entries that satisfies the |filter| from the database |
| 95 | // and invokes |callback| when complete. The filter will be called on |
| 96 | // ProtoDatabase's taskrunner. |
| 97 | virtual void LoadEntriesWithFilter(const LevelDB::KeyFilter& filter, |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 98 | LoadCallback callback) { |
| 99 | db_wrapper_->template LoadEntriesWithFilter<T>(filter, std::move(callback)); |
| 100 | } |
| 101 | |
| 102 | virtual void LoadEntriesWithFilter(const LevelDB::KeyFilter& key_filter, |
Troy Hildebrandt | 952e31b | 2018-08-23 15:51:13 | [diff] [blame] | 103 | const leveldb::ReadOptions& options, |
| 104 | const std::string& target_prefix, |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 105 | LoadCallback callback) { |
| 106 | db_wrapper_->template LoadEntriesWithFilter<T>( |
| 107 | key_filter, options, target_prefix, std::move(callback)); |
| 108 | } |
Min Qin | 7f0cff2 | 2018-05-24 20:06:57 | [diff] [blame] | 109 | |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 110 | virtual void LoadKeysAndEntries(LoadKeysAndEntriesCallback callback) { |
| 111 | db_wrapper_->template LoadKeysAndEntries<T>(std::move(callback)); |
| 112 | } |
| 113 | |
Troy Hildebrandt | d3c43f67 | 2018-09-28 19:20:12 | [diff] [blame] | 114 | virtual void LoadKeysAndEntriesWithFilter( |
| 115 | const LevelDB::KeyFilter& filter, |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 116 | typename ProtoDatabase<T>::LoadKeysAndEntriesCallback callback) { |
| 117 | db_wrapper_->template LoadKeysAndEntriesWithFilter<T>(filter, |
| 118 | std::move(callback)); |
| 119 | } |
Troy Hildebrandt | d3c43f67 | 2018-09-28 19:20:12 | [diff] [blame] | 120 | virtual void LoadKeysAndEntriesWithFilter( |
| 121 | const LevelDB::KeyFilter& filter, |
| 122 | const leveldb::ReadOptions& options, |
| 123 | const std::string& target_prefix, |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 124 | typename ProtoDatabase<T>::LoadKeysAndEntriesCallback callback) { |
| 125 | db_wrapper_->template LoadKeysAndEntriesWithFilter<T>( |
| 126 | filter, options, target_prefix, std::move(callback)); |
| 127 | } |
Troy Hildebrandt | d3c43f67 | 2018-09-28 19:20:12 | [diff] [blame] | 128 | |
tschumann | 108e9c2 | 2016-10-04 13:34:57 | [diff] [blame] | 129 | // Asynchronously loads all keys from the database and invokes |callback| with |
| 130 | // those keys when complete. |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 131 | virtual void LoadKeys(LoadKeysCallback callback) { |
| 132 | db_wrapper_->LoadKeys(std::move(callback)); |
| 133 | } |
tschumann | 108e9c2 | 2016-10-04 13:34:57 | [diff] [blame] | 134 | |
treib | 4fe73e3 | 2016-06-16 15:36:15 | [diff] [blame] | 135 | // Asynchronously loads a single entry, identified by |key|, from the database |
| 136 | // and invokes |callback| when complete. If no entry with |key| is found, |
| 137 | // a nullptr is passed to the callback, but the success flag is still true. |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 138 | virtual void GetEntry(const std::string& key, GetCallback callback) { |
| 139 | db_wrapper_->template GetEntry<T>(key, std::move(callback)); |
| 140 | } |
treib | 4fe73e3 | 2016-06-16 15:36:15 | [diff] [blame] | 141 | |
jianli | 23248b4 | 2015-10-27 23:56:34 | [diff] [blame] | 142 | // Asynchronously destroys the database. |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 143 | virtual void Destroy(DestroyCallback callback) { |
| 144 | db_wrapper_->Destroy(std::move(callback)); |
| 145 | } |
| 146 | |
| 147 | bool GetApproximateMemoryUse(uint64_t* approx_mem_use) { |
| 148 | return db_wrapper_->GetApproximateMemoryUse(approx_mem_use); |
| 149 | } |
| 150 | |
| 151 | protected: |
| 152 | std::unique_ptr<ProtoLevelDBWrapper> db_wrapper_; |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 153 | }; |
| 154 | |
Chris Mumford | 29f2cf5 | 2017-09-21 00:35:40 | [diff] [blame] | 155 | // Return a new instance of Options, but with two additions: |
| 156 | // 1) create_if_missing = true |
| 157 | // 2) max_open_files = 0 |
| 158 | leveldb_env::Options CreateSimpleOptions(); |
| 159 | |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 160 | } // namespace leveldb_proto |
| 161 | |
Troy Hildebrandt | c7f0b95d | 2018-10-11 15:53:22 | [diff] [blame] | 162 | #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_H_ |