blob: d8adc5f6f2f4faa68c7d5f27abce29dc87db8029 [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
dcheng84c358e2016-04-26 07:05:538#include <memory>
[email protected]2b894b82014-06-18 16:22:309#include <string>
10#include <utility>
11#include <vector>
12
Tommy Nyquistddffa7d2017-05-24 08:49:0313#include "base/callback.h"
Min Qin7f0cff22018-05-24 20:06:5714#include "components/leveldb_proto/leveldb_database.h"
Chris Mumford328d3be2017-09-12 17:44:2415#include "third_party/leveldatabase/env_chromium.h"
[email protected]2b894b82014-06-18 16:22:3016
peter7cdc93c2015-07-30 14:03:1417namespace base {
18class FilePath;
19}
20
[email protected]2b894b82014-06-18 16:22:3021namespace leveldb_proto {
22
23// Interface for classes providing persistent storage of Protocol Buffer
24// entries (T must be a Proto type extending MessageLite).
25template <typename T>
26class ProtoDatabase {
27 public:
Tommy Nyquistddffa7d2017-05-24 08:49:0328 using InitCallback = base::OnceCallback<void(bool success)>;
29 using UpdateCallback = base::OnceCallback<void(bool success)>;
peter7cdc93c2015-07-30 14:03:1430 using LoadCallback =
Tommy Nyquistddffa7d2017-05-24 08:49:0331 base::OnceCallback<void(bool success, std::unique_ptr<std::vector<T>>)>;
tschumann108e9c22016-10-04 13:34:5732 using LoadKeysCallback =
Tommy Nyquistddffa7d2017-05-24 08:49:0333 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)>;
peter7cdc93c2015-07-30 14:03:1438
[email protected]2b894b82014-06-18 16:22:3039 // A list of key-value (string, T) tuples.
peter7cdc93c2015-07-30 14:03:1440 using KeyEntryVector = std::vector<std::pair<std::string, T>>;
[email protected]2b894b82014-06-18 16:22:3041
42 virtual ~ProtoDatabase() {}
43
Chris Mumford29f2cf52017-09-21 00:35:4044 // 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]2b894b82014-06-18 16:22:3050
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(
dcheng84c358e2016-04-26 07:05:5355 std::unique_ptr<KeyEntryVector> entries_to_save,
56 std::unique_ptr<std::vector<std::string>> keys_to_remove,
Tommy Nyquistddffa7d2017-05-24 08:49:0357 UpdateCallback callback) = 0;
[email protected]2b894b82014-06-18 16:22:3058
Gang Wub29235f2018-06-11 19:24:3059 // 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]2b894b82014-06-18 16:22:3068 // Asynchronously loads all entries from the database and invokes |callback|
69 // when complete.
Tommy Nyquistddffa7d2017-05-24 08:49:0370 virtual void LoadEntries(LoadCallback callback) = 0;
jianli23248b42015-10-27 23:56:3471
Min Qin7f0cff22018-05-24 20:06:5772 // 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
tschumann108e9c22016-10-04 13:34:5778 // Asynchronously loads all keys from the database and invokes |callback| with
79 // those keys when complete.
Tommy Nyquistddffa7d2017-05-24 08:49:0380 virtual void LoadKeys(LoadKeysCallback callback) = 0;
tschumann108e9c22016-10-04 13:34:5781
treib4fe73e32016-06-16 15:36:1582 // 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 Nyquistddffa7d2017-05-24 08:49:0385 virtual void GetEntry(const std::string& key, GetCallback callback) = 0;
treib4fe73e32016-06-16 15:36:1586
jianli23248b42015-10-27 23:56:3487 // Asynchronously destroys the database.
Tommy Nyquistddffa7d2017-05-24 08:49:0388 virtual void Destroy(DestroyCallback callback) = 0;
[email protected]2b894b82014-06-18 16:22:3089};
90
Chris Mumford29f2cf52017-09-21 00:35:4091// Return a new instance of Options, but with two additions:
92// 1) create_if_missing = true
93// 2) max_open_files = 0
94leveldb_env::Options CreateSimpleOptions();
95
[email protected]2b894b82014-06-18 16:22:3096} // namespace leveldb_proto
97
98#endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_H_