[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 | |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 8 | #include <memory> |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 13 | #include "base/callback.h" |
Min Qin | 7f0cff2 | 2018-05-24 20:06:57 | [diff] [blame] | 14 | #include "components/leveldb_proto/leveldb_database.h" |
Chris Mumford | 328d3be | 2017-09-12 17:44:24 | [diff] [blame] | 15 | #include "third_party/leveldatabase/env_chromium.h" |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 16 | |
peter | 7cdc93c | 2015-07-30 14:03:14 | [diff] [blame] | 17 | namespace base { |
| 18 | class FilePath; |
| 19 | } |
| 20 | |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 21 | namespace leveldb_proto { |
| 22 | |
| 23 | // Interface for classes providing persistent storage of Protocol Buffer |
| 24 | // entries (T must be a Proto type extending MessageLite). |
| 25 | template <typename T> |
| 26 | class ProtoDatabase { |
| 27 | public: |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 28 | using InitCallback = base::OnceCallback<void(bool success)>; |
| 29 | using UpdateCallback = base::OnceCallback<void(bool success)>; |
peter | 7cdc93c | 2015-07-30 14:03:14 | [diff] [blame] | 30 | using LoadCallback = |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 31 | base::OnceCallback<void(bool success, std::unique_ptr<std::vector<T>>)>; |
tschumann | 108e9c2 | 2016-10-04 13:34:57 | [diff] [blame] | 32 | using LoadKeysCallback = |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 33 | base::OnceCallback<void(bool success, |
| 34 | std::unique_ptr<std::vector<std::string>>)>; |
| 35 | using GetCallback = |
| 36 | base::OnceCallback<void(bool success, std::unique_ptr<T>)>; |
| 37 | using DestroyCallback = base::OnceCallback<void(bool success)>; |
peter | 7cdc93c | 2015-07-30 14:03:14 | [diff] [blame] | 38 | |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 39 | // A list of key-value (string, T) tuples. |
peter | 7cdc93c | 2015-07-30 14:03:14 | [diff] [blame] | 40 | using KeyEntryVector = std::vector<std::pair<std::string, T>>; |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 41 | |
| 42 | virtual ~ProtoDatabase() {} |
| 43 | |
Chris Mumford | 29f2cf5 | 2017-09-21 00:35:40 | [diff] [blame] | 44 | // Asynchronously initializes the object with the specified |options|. |
| 45 | // |callback| will be invoked on the calling thread when complete. |
| 46 | virtual void Init(const char* client_name, |
| 47 | const base::FilePath& database_dir, |
| 48 | const leveldb_env::Options& options, |
| 49 | InitCallback callback) = 0; |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 50 | |
| 51 | // Asynchronously saves |entries_to_save| and deletes entries from |
| 52 | // |keys_to_remove| from the database. |callback| will be invoked on the |
| 53 | // calling thread when complete. |
| 54 | virtual void UpdateEntries( |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 55 | std::unique_ptr<KeyEntryVector> entries_to_save, |
| 56 | std::unique_ptr<std::vector<std::string>> keys_to_remove, |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 57 | UpdateCallback callback) = 0; |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 58 | |
Gang Wu | b29235f | 2018-06-11 19:24:30 | [diff] [blame^] | 59 | // Asynchronously saves |entries_to_save| and deletes entries that satisfies |
| 60 | // the |delete_key_filter| from the database. |callback| will be invoked on |
| 61 | // the calling thread when complete. The filter will be called on |
| 62 | // ProtoDatabase's taskrunner. |
| 63 | virtual void UpdateEntriesWithRemoveFilter( |
| 64 | std::unique_ptr<KeyEntryVector> entries_to_save, |
| 65 | const LevelDB::KeyFilter& delete_key_filter, |
| 66 | UpdateCallback callback) = 0; |
| 67 | |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 68 | // Asynchronously loads all entries from the database and invokes |callback| |
| 69 | // when complete. |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 70 | virtual void LoadEntries(LoadCallback callback) = 0; |
jianli | 23248b4 | 2015-10-27 23:56:34 | [diff] [blame] | 71 | |
Min Qin | 7f0cff2 | 2018-05-24 20:06:57 | [diff] [blame] | 72 | // Asynchronously loads entries that satisfies the |filter| from the database |
| 73 | // and invokes |callback| when complete. The filter will be called on |
| 74 | // ProtoDatabase's taskrunner. |
| 75 | virtual void LoadEntriesWithFilter(const LevelDB::KeyFilter& filter, |
| 76 | LoadCallback callback) = 0; |
| 77 | |
tschumann | 108e9c2 | 2016-10-04 13:34:57 | [diff] [blame] | 78 | // Asynchronously loads all keys from the database and invokes |callback| with |
| 79 | // those keys when complete. |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 80 | virtual void LoadKeys(LoadKeysCallback callback) = 0; |
tschumann | 108e9c2 | 2016-10-04 13:34:57 | [diff] [blame] | 81 | |
treib | 4fe73e3 | 2016-06-16 15:36:15 | [diff] [blame] | 82 | // Asynchronously loads a single entry, identified by |key|, from the database |
| 83 | // and invokes |callback| when complete. If no entry with |key| is found, |
| 84 | // a nullptr is passed to the callback, but the success flag is still true. |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 85 | virtual void GetEntry(const std::string& key, GetCallback callback) = 0; |
treib | 4fe73e3 | 2016-06-16 15:36:15 | [diff] [blame] | 86 | |
jianli | 23248b4 | 2015-10-27 23:56:34 | [diff] [blame] | 87 | // Asynchronously destroys the database. |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame] | 88 | virtual void Destroy(DestroyCallback callback) = 0; |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 89 | }; |
| 90 | |
Chris Mumford | 29f2cf5 | 2017-09-21 00:35:40 | [diff] [blame] | 91 | // Return a new instance of Options, but with two additions: |
| 92 | // 1) create_if_missing = true |
| 93 | // 2) max_open_files = 0 |
| 94 | leveldb_env::Options CreateSimpleOptions(); |
| 95 | |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 96 | } // namespace leveldb_proto |
| 97 | |
| 98 | #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_H_ |