blob: 9c708dff20d209ca09730212241679817611eb72 [file] [log] [blame]
[email protected]2b894b82014-06-18 16:22:301// 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 Hildebrandtd3c43f672018-09-28 19:20:128#include <map>
[email protected]2b894b82014-06-18 16:22:309#include <vector>
10
Troy Hildebrandtc7f0b95d2018-10-11 15:53:2211#include "base/sequenced_task_runner.h"
12#include "base/threading/thread_checker.h"
Min Qin7f0cff22018-05-24 20:06:5713#include "components/leveldb_proto/leveldb_database.h"
Troy Hildebrandtc7f0b95d2018-10-11 15:53:2214#include "components/leveldb_proto/proto_leveldb_wrapper.h"
peter7cdc93c2015-07-30 14:03:1415
[email protected]2b894b82014-06-18 16:22:3016namespace leveldb_proto {
17
18// Interface for classes providing persistent storage of Protocol Buffer
19// entries (T must be a Proto type extending MessageLite).
20template <typename T>
21class ProtoDatabase {
22 public:
Tommy Nyquistddffa7d2017-05-24 08:49:0323 using InitCallback = base::OnceCallback<void(bool success)>;
24 using UpdateCallback = base::OnceCallback<void(bool success)>;
peter7cdc93c2015-07-30 14:03:1425 using LoadCallback =
Tommy Nyquistddffa7d2017-05-24 08:49:0326 base::OnceCallback<void(bool success, std::unique_ptr<std::vector<T>>)>;
tschumann108e9c22016-10-04 13:34:5727 using LoadKeysCallback =
Tommy Nyquistddffa7d2017-05-24 08:49:0328 base::OnceCallback<void(bool success,
29 std::unique_ptr<std::vector<std::string>>)>;
Troy Hildebrandtd3c43f672018-09-28 19:20:1230 using LoadKeysAndEntriesCallback =
31 base::OnceCallback<void(bool success,
32 std::unique_ptr<std::map<std::string, T>>)>;
Tommy Nyquistddffa7d2017-05-24 08:49:0333 using GetCallback =
34 base::OnceCallback<void(bool success, std::unique_ptr<T>)>;
35 using DestroyCallback = base::OnceCallback<void(bool success)>;
peter7cdc93c2015-07-30 14:03:1436
[email protected]2b894b82014-06-18 16:22:3037 // A list of key-value (string, T) tuples.
peter7cdc93c2015-07-30 14:03:1438 using KeyEntryVector = std::vector<std::pair<std::string, T>>;
[email protected]2b894b82014-06-18 16:22:3039
Troy Hildebrandtc7f0b95d2018-10-11 15:53:2240 explicit ProtoDatabase(
41 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
42 : db_wrapper_(std::make_unique<ProtoLevelDBWrapper>(task_runner)) {}
[email protected]2b894b82014-06-18 16:22:3043 virtual ~ProtoDatabase() {}
44
Chris Mumford29f2cf52017-09-21 00:35:4045 // Asynchronously initializes the object with the specified |options|.
46 // |callback| will be invoked on the calling thread when complete.
Troy Hildebrandt78e6d8702018-11-05 17:09:4747 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 Mumford29f2cf52017-09-21 00:35:4051 virtual void Init(const char* client_name,
52 const base::FilePath& database_dir,
53 const leveldb_env::Options& options,
Troy Hildebrandtc7f0b95d2018-10-11 15:53:2254 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]2b894b82014-06-18 16:22:3063
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(
dcheng84c358e2016-04-26 07:05:5368 std::unique_ptr<KeyEntryVector> entries_to_save,
69 std::unique_ptr<std::vector<std::string>> keys_to_remove,
Troy Hildebrandtc7f0b95d2018-10-11 15:53:2270 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]2b894b82014-06-18 16:22:3075
Gang Wub29235f2018-06-11 19:24:3076 // 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 Hildebrandtc7f0b95d2018-10-11 15:53:2283 UpdateCallback callback) {
84 db_wrapper_->template UpdateEntriesWithRemoveFilter<T>(
85 std::move(entries_to_save), delete_key_filter, std::move(callback));
86 }
Gang Wub29235f2018-06-11 19:24:3087
[email protected]2b894b82014-06-18 16:22:3088 // Asynchronously loads all entries from the database and invokes |callback|
89 // when complete.
Troy Hildebrandtc7f0b95d2018-10-11 15:53:2290 virtual void LoadEntries(LoadCallback callback) {
91 db_wrapper_->template LoadEntries<T>(std::move(callback));
92 }
jianli23248b42015-10-27 23:56:3493
Min Qin7f0cff22018-05-24 20:06:5794 // 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 Hildebrandtc7f0b95d2018-10-11 15:53:2298 LoadCallback callback) {
99 db_wrapper_->template LoadEntriesWithFilter<T>(filter, std::move(callback));
100 }
101
102 virtual void LoadEntriesWithFilter(const LevelDB::KeyFilter& key_filter,
Troy Hildebrandt952e31b2018-08-23 15:51:13103 const leveldb::ReadOptions& options,
104 const std::string& target_prefix,
Troy Hildebrandtc7f0b95d2018-10-11 15:53:22105 LoadCallback callback) {
106 db_wrapper_->template LoadEntriesWithFilter<T>(
107 key_filter, options, target_prefix, std::move(callback));
108 }
Min Qin7f0cff22018-05-24 20:06:57109
Troy Hildebrandtc7f0b95d2018-10-11 15:53:22110 virtual void LoadKeysAndEntries(LoadKeysAndEntriesCallback callback) {
111 db_wrapper_->template LoadKeysAndEntries<T>(std::move(callback));
112 }
113
Troy Hildebrandtd3c43f672018-09-28 19:20:12114 virtual void LoadKeysAndEntriesWithFilter(
115 const LevelDB::KeyFilter& filter,
Troy Hildebrandtc7f0b95d2018-10-11 15:53:22116 typename ProtoDatabase<T>::LoadKeysAndEntriesCallback callback) {
117 db_wrapper_->template LoadKeysAndEntriesWithFilter<T>(filter,
118 std::move(callback));
119 }
Troy Hildebrandtd3c43f672018-09-28 19:20:12120 virtual void LoadKeysAndEntriesWithFilter(
121 const LevelDB::KeyFilter& filter,
122 const leveldb::ReadOptions& options,
123 const std::string& target_prefix,
Troy Hildebrandtc7f0b95d2018-10-11 15:53:22124 typename ProtoDatabase<T>::LoadKeysAndEntriesCallback callback) {
125 db_wrapper_->template LoadKeysAndEntriesWithFilter<T>(
126 filter, options, target_prefix, std::move(callback));
127 }
Troy Hildebrandtd3c43f672018-09-28 19:20:12128
tschumann108e9c22016-10-04 13:34:57129 // Asynchronously loads all keys from the database and invokes |callback| with
130 // those keys when complete.
Troy Hildebrandtc7f0b95d2018-10-11 15:53:22131 virtual void LoadKeys(LoadKeysCallback callback) {
132 db_wrapper_->LoadKeys(std::move(callback));
133 }
tschumann108e9c22016-10-04 13:34:57134
treib4fe73e32016-06-16 15:36:15135 // 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 Hildebrandtc7f0b95d2018-10-11 15:53:22138 virtual void GetEntry(const std::string& key, GetCallback callback) {
139 db_wrapper_->template GetEntry<T>(key, std::move(callback));
140 }
treib4fe73e32016-06-16 15:36:15141
jianli23248b42015-10-27 23:56:34142 // Asynchronously destroys the database.
Troy Hildebrandtc7f0b95d2018-10-11 15:53:22143 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]2b894b82014-06-18 16:22:30153};
154
Chris Mumford29f2cf52017-09-21 00:35:40155// Return a new instance of Options, but with two additions:
156// 1) create_if_missing = true
157// 2) max_open_files = 0
158leveldb_env::Options CreateSimpleOptions();
159
[email protected]2b894b82014-06-18 16:22:30160} // namespace leveldb_proto
161
Troy Hildebrandtc7f0b95d2018-10-11 15:53:22162#endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_H_