blob: a43b20deaa8e9e1c59e4f7ac60b17a4afb97c192 [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"
treibd0124d402016-11-24 21:54:3514#include "components/leveldb_proto/options.h"
[email protected]2b894b82014-06-18 16:22:3015
peter7cdc93c2015-07-30 14:03:1416namespace base {
17class FilePath;
18}
19
[email protected]2b894b82014-06-18 16:22:3020namespace leveldb_proto {
21
22// Interface for classes providing persistent storage of Protocol Buffer
23// entries (T must be a Proto type extending MessageLite).
24template <typename T>
25class ProtoDatabase {
26 public:
Tommy Nyquistddffa7d2017-05-24 08:49:0327 using InitCallback = base::OnceCallback<void(bool success)>;
28 using UpdateCallback = base::OnceCallback<void(bool success)>;
peter7cdc93c2015-07-30 14:03:1429 using LoadCallback =
Tommy Nyquistddffa7d2017-05-24 08:49:0330 base::OnceCallback<void(bool success, std::unique_ptr<std::vector<T>>)>;
tschumann108e9c22016-10-04 13:34:5731 using LoadKeysCallback =
Tommy Nyquistddffa7d2017-05-24 08:49:0332 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)>;
peter7cdc93c2015-07-30 14:03:1437
[email protected]2b894b82014-06-18 16:22:3038 // A list of key-value (string, T) tuples.
peter7cdc93c2015-07-30 14:03:1439 using KeyEntryVector = std::vector<std::pair<std::string, T>>;
[email protected]2b894b82014-06-18 16:22:3040
41 virtual ~ProtoDatabase() {}
42
treibd0124d402016-11-24 21:54:3543 // 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 Nyquistddffa7d2017-05-24 08:49:0347 InitCallback callback) {
48 InitWithOptions(client_name, Options(database_dir), std::move(callback));
treibd0124d402016-11-24 21:54:3549 }
50
51 // Similar to Init, but takes additional options.
52 virtual void InitWithOptions(const char* client_name,
53 const Options& options,
Tommy Nyquistddffa7d2017-05-24 08:49:0354 InitCallback callback) = 0;
[email protected]2b894b82014-06-18 16:22:3055
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(
dcheng84c358e2016-04-26 07:05:5360 std::unique_ptr<KeyEntryVector> entries_to_save,
61 std::unique_ptr<std::vector<std::string>> keys_to_remove,
Tommy Nyquistddffa7d2017-05-24 08:49:0362 UpdateCallback callback) = 0;
[email protected]2b894b82014-06-18 16:22:3063
64 // Asynchronously loads all entries from the database and invokes |callback|
65 // when complete.
Tommy Nyquistddffa7d2017-05-24 08:49:0366 virtual void LoadEntries(LoadCallback callback) = 0;
jianli23248b42015-10-27 23:56:3467
tschumann108e9c22016-10-04 13:34:5768 // Asynchronously loads all keys from the database and invokes |callback| with
69 // those keys when complete.
Tommy Nyquistddffa7d2017-05-24 08:49:0370 virtual void LoadKeys(LoadKeysCallback callback) = 0;
tschumann108e9c22016-10-04 13:34:5771
treib4fe73e32016-06-16 15:36:1572 // 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 Nyquistddffa7d2017-05-24 08:49:0375 virtual void GetEntry(const std::string& key, GetCallback callback) = 0;
treib4fe73e32016-06-16 15:36:1576
jianli23248b42015-10-27 23:56:3477 // Asynchronously destroys the database.
Tommy Nyquistddffa7d2017-05-24 08:49:0378 virtual void Destroy(DestroyCallback callback) = 0;
[email protected]2b894b82014-06-18 16:22:3079};
80
81} // namespace leveldb_proto
82
83#endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_H_