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_NB_DELEGATE_IMPL_H
17 #define KV_STORE_NB_DELEGATE_IMPL_H
18 
19 #include <functional>
20 #include <map>
21 #include <mutex>
22 #include <string>
23 
24 #include "db_types.h"
25 #include "store_types.h"
26 #include "ikvdb_connection.h"
27 #include "kv_store_nb_conflict_data.h"
28 #include "kv_store_nb_delegate.h"
29 
30 namespace DistributedDB {
31 class KvStoreNbDelegateImpl final : public KvStoreNbDelegate {
32 public:
33     KvStoreNbDelegateImpl(IKvDBConnection *conn, const std::string &storeId);
34     ~KvStoreNbDelegateImpl() override;
35 
36     DISABLE_COPY_ASSIGN_MOVE(KvStoreNbDelegateImpl);
37 
38     // Public zone interfaces
39     DBStatus Get(const Key &key, Value &value) const override;
40 
41     DBStatus GetEntries(const Key &keyPrefix, std::vector<Entry> &entries) const override;
42 
43     DBStatus GetEntries(const Key &keyPrefix, KvStoreResultSet *&resultSet) const override;
44 
45     DBStatus GetEntries(const Query &query, std::vector<Entry> &entries) const override;
46 
47     DBStatus GetEntries(const Query &query, KvStoreResultSet *&resultSet) const override;
48 
49     DBStatus GetCount(const Query &query, int &count) const override;
50 
51     DBStatus CloseResultSet(KvStoreResultSet *&resultSet) override;
52 
53     DBStatus Put(const Key &key, const Value &value) override;
54 
55     DBStatus PutBatch(const std::vector<Entry> &entries) override;
56 
57     DBStatus DeleteBatch(const std::vector<Key> &keys) override;
58 
59     DBStatus Delete(const Key &key) override;
60 
61     // Local zone interfaces
62     DBStatus GetLocal(const Key &key, Value &value) const override;
63 
64     DBStatus GetLocalEntries(const Key &keyPrefix, std::vector<Entry> &entries) const override;
65 
66     DBStatus PutLocal(const Key &key, const Value &value) override;
67 
68     DBStatus DeleteLocal(const Key &key) override;
69 
70     DBStatus PublishLocal(const Key &key, bool deleteLocal, bool updateTimestamp,
71         const KvStoreNbPublishOnConflict &onConflict) override;
72 
73     DBStatus UnpublishToLocal(const Key &key, bool deletePublic, bool updateTimestamp) override;
74 
75     // Observer interfaces
76     DBStatus RegisterObserver(const Key &key, unsigned int mode, KvStoreObserver *observer) override;
77 
78     DBStatus UnRegisterObserver(const KvStoreObserver *observer) override;
79 
80     DBStatus RemoveDeviceData(const std::string &device) override;
81 
82     // Other interfaces
83     std::string GetStoreId() const override;
84 
85     // Sync function interface, if wait set true, this function will be blocked until sync finished
86     DBStatus Sync(const std::vector<std::string> &devices, SyncMode mode,
87         const std::function<void(const std::map<std::string, DBStatus> &devicesMap)> &onComplete,
88         bool wait) override;
89 
90     // Special pragma interface, see PragmaCmd and PragmaData,
91     DBStatus Pragma(PragmaCmd cmd, PragmaData &paramData) override;
92 
93     // Set the conflict notifier for getting the specified type conflict data.
94     DBStatus SetConflictNotifier(int conflictType, const KvStoreNbConflictNotifier &notifier) override;
95 
96     // Rekey the database.
97     DBStatus Rekey(const CipherPassword &password) override;
98 
99     // Empty passwords represent non-encrypted files.
100     // Export existing database files to a specified database file in the specified directory.
101     DBStatus Export(const std::string &filePath, const CipherPassword &passwd, bool force) override;
102 
103     // Import the existing database files to the specified database file in the specified directory.
104     DBStatus Import(const std::string &filePath, const CipherPassword &passwd) override;
105 
106     // Start a transaction
107     DBStatus StartTransaction() override;
108 
109     // Commit a transaction
110     DBStatus Commit() override;
111 
112     // Rollback a transaction
113     DBStatus Rollback() override;
114 
115     DBStatus PutLocalBatch(const std::vector<Entry> &entries) override;
116 
117     DBStatus DeleteLocalBatch(const std::vector<Key> &keys) override;
118 
119     // Get the SecurityOption of this kvStore.
120     DBStatus GetSecurityOption(SecurityOption &option) const override;
121 
122     DBStatus SetRemotePushFinishedNotify(const RemotePushFinishedNotifier &notifier) override;
123 
124     void SetReleaseFlag(bool flag);
125 
126     DBStatus Close();
127 
128     // Sync function interface, if wait set true, this function will be blocked until sync finished.
129     // Param query used to filter the records to be synchronized.
130     // Now just support push mode and query by prefixKey.
131     DBStatus Sync(const std::vector<std::string> &devices, SyncMode mode,
132         const std::function<void(const std::map<std::string, DBStatus> &devicesMap)> &onComplete,
133         const Query &query, bool wait) override;
134 
135     DBStatus CheckIntegrity() const override;
136 
137     // Set an equal identifier for this database, After this called, send msg to the target will use this identifier
138     DBStatus SetEqualIdentifier(const std::string &identifier, const std::vector<std::string> &targets) override;
139 
140     DBStatus SetPushDataInterceptor(const PushDataInterceptor &interceptor) override;
141 
142     // Register a subscriber query on peer devices. The data in the peer device meets the subscriber query condition
143     // will automatically push to the local device when it's changed.
144     DBStatus SubscribeRemoteQuery(const std::vector<std::string> &devices,
145         const std::function<void(const std::map<std::string, DBStatus> &devicesMap)> &onComplete,
146         const Query &query, bool wait) override;
147 
148     // Unregister a subscriber query on peer devices.
149     DBStatus UnSubscribeRemoteQuery(const std::vector<std::string> &devices,
150         const std::function<void(const std::map<std::string, DBStatus> &devicesMap)> &onComplete,
151         const Query &query, bool wait) override;
152 
153     DBStatus RemoveDeviceData() override;
154 
155     DBStatus GetKeys(const Key &keyPrefix, std::vector<Key> &keys) const override;
156 
157     size_t GetSyncDataSize(const std::string &device) const override;
158 
159     // update all key in sync_data which is not deleted data
160     DBStatus UpdateKey(const UpdateKeyCallback &callback) override;
161 
162     std::pair<DBStatus, WatermarkInfo> GetWatermarkInfo(const std::string &device) override;
163 
164     DBStatus Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess) override;
165 
166     DBStatus SetCloudDB(const std::map<std::string, std::shared_ptr<ICloudDb>> &cloudDBs) override;
167 
168     DBStatus SetCloudDbSchema(const std::map<std::string, DataBaseSchema> &schema) override;
169 
170     DBStatus RemoveDeviceData(const std::string &device, ClearMode mode) override;
171 
172     DBStatus RemoveDeviceData(const std::string &device, const std::string &user, ClearMode mode) override;
173 
174     int32_t GetTaskCount() override;
175 
176     void SetGenCloudVersionCallback(const GenerateCloudVersionCallback &callback) override;
177 
178     std::pair<DBStatus, std::map<std::string, std::string>> GetCloudVersion(const std::string &device) override;
179 
180     DBStatus SetReceiveDataInterceptor(const DataInterceptor &interceptor) override;
181 
182     DBStatus SetCloudSyncConfig(const CloudSyncConfig &config) override;
183 
184     DBStatus GetDeviceEntries(const std::string &device, std::vector<Entry> &entries) const override;
185 
186     DatabaseStatus GetDatabaseStatus() const override;
187 private:
188     DBStatus GetInner(const IOption &option, const Key &key, Value &value) const;
189     DBStatus PutInner(const IOption &option, const Key &key, const Value &value);
190     DBStatus DeleteInner(const IOption &option, const Key &key);
191     DBStatus GetEntriesInner(const IOption &option, const Key &keyPrefix, std::vector<Entry> &entries) const;
192 
193     void OnSyncComplete(const std::map<std::string, int> &statuses,
194         const std::function<void(const std::map<std::string, DBStatus> &devicesMap)> &onComplete) const;
195 
196     DBStatus RegisterDeviceObserver(const Key &key, unsigned int mode, KvStoreObserver *observer);
197 
198     DBStatus RegisterCloudObserver(const Key &key, unsigned int mode, KvStoreObserver *observer);
199 
200     DBStatus UnRegisterDeviceObserver(const KvStoreObserver *observer);
201 
202     DBStatus UnRegisterCloudObserver(const KvStoreObserver *observer);
203 
204     IKvDBConnection *conn_;
205     std::string storeId_;
206     bool releaseFlag_;
207     std::mutex observerMapLock_;
208     std::map<const KvStoreObserver *, const KvDBObserverHandle *> observerMap_;
209     std::map<const KvStoreObserver *, unsigned int> cloudObserverMap_;
210 };
211 } // namespace DistributedDB
212 
213 #endif // KV_STORE_NB_DELEGATE_IMPL_H
214