[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" |
treib | d0124d40 | 2016-11-24 21:54:35 | [diff] [blame] | 14 | #include "components/leveldb_proto/options.h" |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 15 | |
peter | 7cdc93c | 2015-07-30 14:03:14 | [diff] [blame] | 16 | namespace base { |
| 17 | class FilePath; |
| 18 | } |
| 19 | |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 20 | namespace leveldb_proto { |
| 21 | |
| 22 | // Interface for classes providing persistent storage of Protocol Buffer |
| 23 | // entries (T must be a Proto type extending MessageLite). |
| 24 | template <typename T> |
| 25 | class ProtoDatabase { |
| 26 | public: |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame^] | 27 | using InitCallback = base::OnceCallback<void(bool success)>; |
| 28 | using UpdateCallback = base::OnceCallback<void(bool success)>; |
peter | 7cdc93c | 2015-07-30 14:03:14 | [diff] [blame] | 29 | using LoadCallback = |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame^] | 30 | base::OnceCallback<void(bool success, std::unique_ptr<std::vector<T>>)>; |
tschumann | 108e9c2 | 2016-10-04 13:34:57 | [diff] [blame] | 31 | using LoadKeysCallback = |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame^] | 32 | base::OnceCallback<void(bool success, |
| 33 | std::unique_ptr<std::vector<std::string>>)>; |
| 34 | using GetCallback = |
| 35 | base::OnceCallback<void(bool success, std::unique_ptr<T>)>; |
| 36 | using DestroyCallback = base::OnceCallback<void(bool success)>; |
peter | 7cdc93c | 2015-07-30 14:03:14 | [diff] [blame] | 37 | |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 38 | // A list of key-value (string, T) tuples. |
peter | 7cdc93c | 2015-07-30 14:03:14 | [diff] [blame] | 39 | using KeyEntryVector = std::vector<std::pair<std::string, T>>; |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 40 | |
| 41 | virtual ~ProtoDatabase() {} |
| 42 | |
treib | d0124d40 | 2016-11-24 21:54:35 | [diff] [blame] | 43 | // Asynchronously initializes the object with default options. |callback| will |
| 44 | // be invoked on the calling thread when complete. |
| 45 | void Init(const char* client_name, |
| 46 | const base::FilePath& database_dir, |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame^] | 47 | InitCallback callback) { |
| 48 | InitWithOptions(client_name, Options(database_dir), std::move(callback)); |
treib | d0124d40 | 2016-11-24 21:54:35 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Similar to Init, but takes additional options. |
| 52 | virtual void InitWithOptions(const char* client_name, |
| 53 | const Options& options, |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame^] | 54 | InitCallback callback) = 0; |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 55 | |
| 56 | // Asynchronously saves |entries_to_save| and deletes entries from |
| 57 | // |keys_to_remove| from the database. |callback| will be invoked on the |
| 58 | // calling thread when complete. |
| 59 | virtual void UpdateEntries( |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 60 | std::unique_ptr<KeyEntryVector> entries_to_save, |
| 61 | std::unique_ptr<std::vector<std::string>> keys_to_remove, |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame^] | 62 | UpdateCallback callback) = 0; |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 63 | |
| 64 | // Asynchronously loads all entries from the database and invokes |callback| |
| 65 | // when complete. |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame^] | 66 | virtual void LoadEntries(LoadCallback callback) = 0; |
jianli | 23248b4 | 2015-10-27 23:56:34 | [diff] [blame] | 67 | |
tschumann | 108e9c2 | 2016-10-04 13:34:57 | [diff] [blame] | 68 | // Asynchronously loads all keys from the database and invokes |callback| with |
| 69 | // those keys when complete. |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame^] | 70 | virtual void LoadKeys(LoadKeysCallback callback) = 0; |
tschumann | 108e9c2 | 2016-10-04 13:34:57 | [diff] [blame] | 71 | |
treib | 4fe73e3 | 2016-06-16 15:36:15 | [diff] [blame] | 72 | // Asynchronously loads a single entry, identified by |key|, from the database |
| 73 | // and invokes |callback| when complete. If no entry with |key| is found, |
| 74 | // 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^] | 75 | virtual void GetEntry(const std::string& key, GetCallback callback) = 0; |
treib | 4fe73e3 | 2016-06-16 15:36:15 | [diff] [blame] | 76 | |
jianli | 23248b4 | 2015-10-27 23:56:34 | [diff] [blame] | 77 | // Asynchronously destroys the database. |
Tommy Nyquist | ddffa7d | 2017-05-24 08:49:03 | [diff] [blame^] | 78 | virtual void Destroy(DestroyCallback callback) = 0; |
[email protected] | 2b894b8 | 2014-06-18 16:22:30 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | } // namespace leveldb_proto |
| 82 | |
| 83 | #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_H_ |