1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef KV_STORE_DELEGATE_H
17 #define KV_STORE_DELEGATE_H
18 
19 #include <functional>
20 #include <string>
21 
22 #include "kv_store_observer.h"
23 #include "kv_store_snapshot_delegate.h"
24 #include "store_types.h"
25 
26 namespace DistributedDB {
27 class KvStoreDelegate {
28 public:
29     using ConflictResolution = std::function<void(void)>;
30 
31     struct Option {
32         bool createIfNecessary = true;
33         bool localOnly = false;
34         bool isEncryptedDb = false;
35         CipherType cipher = CipherType::DEFAULT;
36         CipherPassword passwd;
37         bool createDirByStoreIdOnly = false;
38     };
39 
~KvStoreDelegate()40     DB_API virtual ~KvStoreDelegate() {}
41 
42     // Used to Put a k-v pair to the kvstore.
43     // Return OK if operation is successful.
44     DB_API virtual DBStatus Put(const Key &key, const Value &value) = 0;
45 
46     // Used to Put a vector<Entry> contains k-v pairs to the kvstore.
47     // Return OK if operation is successful..
48     DB_API virtual DBStatus PutBatch(const std::vector<Entry> &entries) = 0;
49 
50     // Delete a record with the given key.
51     // Return OK if operation is successful.
52     DB_API virtual DBStatus Delete(const Key &key) = 0;
53 
54     // Batch delete records with the given keys.
55     // Return OK if operation is successful.
56     DB_API virtual DBStatus DeleteBatch(const std::vector<Key> &keys) = 0;
57 
58     // Delete all record of the kvstore.
59     // Return OK if operation is successful.
60     DB_API virtual DBStatus Clear() = 0;
61 
62     // Return a storeId of the KvStore instance
63     DB_API virtual std::string GetStoreId() const = 0;
64 
65     // Get a snapshot of the kvstore. The observer is used to notify data changed, it can be null.
66     // Return value is DBStatus and KvStoreSnapshotDelegate*, these values will be passed to the callback.
67     DB_API virtual void GetKvStoreSnapshot(KvStoreObserver *observer,
68         const std::function<void(DBStatus, KvStoreSnapshotDelegate *)> &callback) = 0;
69 
70     // Release a snapshot, it will return OK if operation is successful.
71     DB_API virtual DBStatus ReleaseKvStoreSnapshot(KvStoreSnapshotDelegate *&snapshotDelegate) = 0;
72 
73     // Register a data change observer
74     DB_API virtual DBStatus RegisterObserver(KvStoreObserver *observer) = 0;
75 
76     // Unregister a data change observer
77     DB_API virtual DBStatus UnRegisterObserver(const KvStoreObserver *observer) = 0;
78 
79     // Start a transaction
80     DB_API virtual DBStatus StartTransaction() = 0;
81 
82     // Commit a transaction
83     DB_API virtual DBStatus Commit() = 0;
84 
85     // Rollback a transaction
86     DB_API virtual DBStatus Rollback() = 0;
87 
88     // Used to set the resolution policy for conflicts.
89     // Return OK if operation is successful.
90     DB_API virtual DBStatus SetConflictResolutionPolicy(ResolutionPolicyType type,
91         const ConflictResolution &resolution) = 0;
92 
93     // Used to rekey the database.
94     DB_API virtual DBStatus Rekey(const CipherPassword &password) = 0;
95 
96     // Special pragma interface, see PragmaCmd and PragmaData,
97     DB_API virtual DBStatus Pragma(PragmaCmd cmd, PragmaData &paramData) = 0;
98 
99     // Empty passwords represent non-encrypted files.
100     // Export existing database files to a specified database file in the specified directory.
101     DB_API virtual DBStatus Export(const std::string &filePath, const CipherPassword &passwd) = 0;
102 
103     // Import the existing database files to the specified database file in the specified directory.
104     DB_API virtual DBStatus Import(const std::string &filePath, const CipherPassword &passwd) = 0;
105 };
106 } // namespace DistributedDB
107 
108 #endif // KV_STORE_DELEGATE_H