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 I_KV_DB_CONNECTION_H
17 #define I_KV_DB_CONNECTION_H
18 
19 #include <string>
20 #include <functional>
21 
22 #include "cloud/cloud_store_types.h"
23 #include "cloud/icloud_db.h"
24 #include "db_types.h"
25 #include "iconnection.h"
26 #include "intercepted_data.h"
27 #include "kv_store_observer.h"
28 #include "macro_utils.h"
29 #include "query.h"
30 #include "store_types.h"
31 
32 namespace DistributedDB {
33 class IKvDB;
34 class IKvDBSnapshot;
35 class KvDBObserverHandle;
36 class KvDBCommitNotifyData;
37 class IKvDBResultSet;
38 
39 using KvDBObserverAction = std::function<void(const KvDBCommitNotifyData &data)>;
40 using KvDBConflictAction = std::function<void(const KvDBCommitNotifyData &data)>;
41 
42 class IKvDBConnection : public IConnection {
43 public:
44     IKvDBConnection() = default;
~IKvDBConnection()45     virtual ~IKvDBConnection() {};
46 
47     DISABLE_COPY_ASSIGN_MOVE(IKvDBConnection);
48 
49     // Get the value from the database.
50     virtual int Get(const IOption &option, const Key &key, Value &value) const = 0;
51 
52     // Put the value to the database.
53     virtual int Put(const IOption &option, const Key &key, const Value &value) = 0;
54 
55     // Delete the value from the database.
56     virtual int Delete(const IOption &option, const Key &key) = 0;
57 
58     // Clear all the data from the database.
59     virtual int Clear(const IOption &option) = 0;
60 
61     // Get all the data from the database.
62     virtual int GetEntries(const IOption &option, const Key &keyPrefix, std::vector<Entry> &entries) const = 0;
63 
64     virtual int GetEntries(const IOption &option, const Query &query, std::vector<Entry> &entries) const = 0;
65 
66     virtual int GetCount(const IOption &option, const Query &query, int &count) const = 0;
67 
68     // Put the batch values to the database.
69     virtual int PutBatch(const IOption &option, const std::vector<Entry> &entries) = 0;
70 
71     // Delete the batch values from the database.
72     virtual int DeleteBatch(const IOption &option, const std::vector<Key> &keys) = 0;
73 
74     // Get the snapshot.
75     virtual int GetSnapshot(IKvDBSnapshot *&snapshot) const = 0;
76 
77     // Release the created snapshot.
78     virtual void ReleaseSnapshot(IKvDBSnapshot *&snapshot) = 0;
79 
80     // Start the transaction.
81     virtual int StartTransaction() = 0;
82 
83     // Commit the transaction.
84     virtual int Commit() = 0;
85 
86     // Roll back the transaction.
87     virtual int RollBack() = 0;
88 
89     // Check if the transaction already started manually
90     virtual bool IsTransactionStarted() const = 0;
91 
92     // Register observer.
93     virtual KvDBObserverHandle *RegisterObserver(unsigned mode, const Key &key,
94         const KvDBObserverAction &action, int &errCode) = 0;
95 
96     // Unregister observer.
97     virtual int UnRegisterObserver(const KvDBObserverHandle *observerHandle) = 0;
98 
99     // Register a conflict notifier.
100     virtual int SetConflictNotifier(int conflictType, const KvDBConflictAction &action) = 0;
101 
102     // Close and release the connection.
103     virtual int Close() = 0;
104 
105     virtual std::string GetIdentifier() const = 0;
106 
107     // Pragma interface.
108     virtual int Pragma(int cmd, void *parameter) = 0;
109 
110     // Rekey the database.
111     virtual int Rekey(const CipherPassword &passwd) = 0;
112 
113     // Empty passwords represent non-encrypted files.
114     // Export existing database files to a specified database file in the specified directory.
115     virtual int Export(const std::string &filePath, const CipherPassword &passwd) = 0;
116 
117     // Import the existing database files to the specified database file in the specified directory.
118     virtual int Import(const std::string &filePath, const CipherPassword &passwd) = 0;
119 
120     // Get the result set
121     virtual int GetResultSet(const IOption &option, const Key &keyPrefix, IKvDBResultSet *&resultSet) const = 0;
122 
123     virtual int GetResultSet(const IOption &option, const Query &query, IKvDBResultSet *&resultSet) const = 0;
124 
125     // Release the result set
126     virtual void ReleaseResultSet(IKvDBResultSet *&resultSet) = 0;
127 
128     virtual int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier &notifier) = 0;
129 
130     // Get the securityLabel and securityFlag
131     virtual int GetSecurityOption(int &securityLabel, int &securityFlag) const = 0;
132 
133     virtual int CheckIntegrity() const = 0;
134 
135     virtual int GetKeys(const IOption &option, const Key &keyPrefix, std::vector<Key> &keys) const = 0;
136 
137     virtual int GetSyncDataSize(const std::string &device, size_t &size) const = 0;
138 
139     virtual int UpdateKey(const UpdateKeyCallback &callback) = 0;
140 
141     virtual int GetWatermarkInfo(const std::string &device, WatermarkInfo &info) = 0;
142 
143     virtual int Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess) = 0;
144 
145     virtual int SetCloudDB(const std::map<std::string, std::shared_ptr<ICloudDb>> &cloudDBs) = 0;
146 
147     virtual int SetCloudDbSchema(const std::map<std::string, DataBaseSchema> &schema) = 0;
148 
149     virtual int RemoveDeviceData(const std::string &device, ClearMode mode) = 0;
150 
151     virtual int RemoveDeviceData(const std::string &device, const std::string &user, ClearMode mode) = 0;
152 
153     virtual int32_t GetTaskCount() = 0;
154 
155     virtual int RegisterObserverAction(const KvStoreObserver *observer, const ObserverAction &action) = 0;
156 
157     virtual int UnRegisterObserverAction(const KvStoreObserver *observer) = 0;
158 
159     virtual void SetGenCloudVersionCallback(const GenerateCloudVersionCallback &callback) = 0;
160 
161     virtual int GetCloudVersion(const std::string &device, std::map<std::string, std::string> &versionMap) = 0;
162 
163     virtual int SetReceiveDataInterceptor(const DataInterceptor &interceptor) = 0;
164 
165     virtual int SetCloudSyncConfig(const CloudSyncConfig &config) = 0;
166 
167     virtual int GetEntries(const std::string &device, std::vector<Entry> &entries) const = 0;
168 
169     virtual void MarkRebuild() = 0;
170 
171     virtual bool IsRebuild() const = 0;
172 };
173 } // namespace DistributedDB
174 
175 #endif // I_KV_DB_CONNECTION_H
176