1 /*
2  * Copyright (c) 2022 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 #include "db_store_mock.h"
16 #include "db_change_data_mock.h"
17 namespace OHOS {
18 namespace DistributedData {
19 using namespace DistributedDB;
Get(const Key & key,Value & value) const20 DBStatus DBStoreMock::Get(const Key &key, Value &value) const
21 {
22     return Get(entries_, key, value);
23 }
24 
GetEntries(const Key & keyPrefix,std::vector<Entry> & entries) const25 DBStatus DBStoreMock::GetEntries(const Key &keyPrefix, std::vector<Entry> &entries) const
26 {
27     return GetEntries(entries_, keyPrefix, entries);
28 }
29 
GetEntries(const Key & keyPrefix,KvStoreResultSet * & resultSet) const30 DBStatus DBStoreMock::GetEntries(const Key &keyPrefix, KvStoreResultSet *&resultSet) const
31 {
32     resultSet = nullptr;
33     return NOT_SUPPORT;
34 }
35 
GetEntries(const Query & query,std::vector<Entry> & entries) const36 DBStatus DBStoreMock::GetEntries(const Query &query, std::vector<Entry> &entries) const
37 {
38     return NOT_SUPPORT;
39 }
40 
GetEntries(const Query & query,KvStoreResultSet * & resultSet) const41 DBStatus DBStoreMock::GetEntries(const Query &query, KvStoreResultSet *&resultSet) const
42 {
43     resultSet = nullptr;
44     return NOT_SUPPORT;
45 }
46 
GetCount(const Query & query,int & count) const47 DBStatus DBStoreMock::GetCount(const Query &query, int &count) const
48 {
49     count = 0;
50     return NOT_SUPPORT;
51 }
52 
CloseResultSet(KvStoreResultSet * & resultSet)53 DBStatus DBStoreMock::CloseResultSet(KvStoreResultSet *&resultSet)
54 {
55     return NOT_SUPPORT;
56 }
57 
Put(const Key & key,const Value & value)58 DBStatus DBStoreMock::Put(const Key &key, const Value &value)
59 {
60     return PutBatch(entries_, { { key, value } });
61 }
62 
PutBatch(const std::vector<Entry> & entries)63 DBStatus DBStoreMock::PutBatch(const std::vector<Entry> &entries)
64 {
65     return PutBatch(entries_, entries);
66 }
67 
DeleteBatch(const std::vector<Key> & keys)68 DBStatus DBStoreMock::DeleteBatch(const std::vector<Key> &keys)
69 {
70     return DeleteBatch(entries_, keys);
71 }
72 
Delete(const Key & key)73 DBStatus DBStoreMock::Delete(const Key &key)
74 {
75     return DeleteBatch(entries_, { key });
76 }
77 
GetLocal(const Key & key,Value & value) const78 DBStatus DBStoreMock::GetLocal(const Key &key, Value &value) const
79 {
80     return Get(localEntries_, key, value);
81 }
82 
GetLocalEntries(const Key & keyPrefix,std::vector<Entry> & entries) const83 DBStatus DBStoreMock::GetLocalEntries(const Key &keyPrefix, std::vector<Entry> &entries) const
84 {
85     return GetEntries(localEntries_, keyPrefix, entries);
86 }
87 
PutLocal(const Key & key,const Value & value)88 DBStatus DBStoreMock::PutLocal(const Key &key, const Value &value)
89 {
90     return PutBatch(localEntries_, { { key, value } });
91 }
92 
DeleteLocal(const Key & key)93 DBStatus DBStoreMock::DeleteLocal(const Key &key)
94 {
95     return DeleteBatch(localEntries_, { key });
96 }
97 
PublishLocal(const Key & key,bool deleteLocal,bool updateTimestamp,const KvStoreNbPublishOnConflict & onConflict)98 DBStatus DBStoreMock::PublishLocal(const Key &key, bool deleteLocal, bool updateTimestamp,
99     const KvStoreNbPublishOnConflict &onConflict)
100 {
101     return NOT_SUPPORT;
102 }
103 
UnpublishToLocal(const Key & key,bool deletePublic,bool updateTimestamp)104 DBStatus DBStoreMock::UnpublishToLocal(const Key &key, bool deletePublic, bool updateTimestamp)
105 {
106     return NOT_SUPPORT;
107 }
108 
RegisterObserver(const Key & key,unsigned int mode,KvStoreObserver * observer)109 DBStatus DBStoreMock::RegisterObserver(const Key &key, unsigned int mode, KvStoreObserver *observer)
110 {
111     observers_.Compute(observer, [key](auto &, std::set<Key> &prefixes) {
112         prefixes.insert(key);
113         return !prefixes.empty();
114     });
115     return OK;
116 }
117 
UnRegisterObserver(const KvStoreObserver * observer)118 DBStatus DBStoreMock::UnRegisterObserver(const KvStoreObserver *observer)
119 {
120     KvStoreObserver *key = const_cast<KvStoreObserver *>(observer);
121     observers_.Erase(key);
122     return OK;
123 }
124 
RemoveDeviceData(const std::string & device)125 DBStatus DBStoreMock::RemoveDeviceData(const std::string &device)
126 {
127     return NOT_SUPPORT;
128 }
129 
GetStoreId() const130 std::string DBStoreMock::GetStoreId() const
131 {
132     return std::string();
133 }
134 
Sync(const std::vector<std::string> & devices,SyncMode mode,const std::function<void (const std::map<std::string,DBStatus> &)> & onComplete,bool wait)135 DBStatus DBStoreMock::Sync(const std::vector<std::string> &devices, SyncMode mode,
136     const std::function<void(const std::map<std::string, DBStatus> &)> &onComplete, bool wait)
137 {
138     std::map<std::string, DBStatus> result;
139     for (const auto &device : devices) {
140         result[device] = OK;
141     }
142     onComplete(result);
143     return OK;
144 }
145 
Pragma(PragmaCmd cmd,PragmaData & paramData)146 DBStatus DBStoreMock::Pragma(PragmaCmd cmd, PragmaData &paramData)
147 {
148     return NOT_SUPPORT;
149 }
150 
SetConflictNotifier(int conflictType,const KvStoreNbConflictNotifier & notifier)151 DBStatus DBStoreMock::SetConflictNotifier(int conflictType, const KvStoreNbConflictNotifier &notifier)
152 {
153     return NOT_SUPPORT;
154 }
155 
Rekey(const CipherPassword & password)156 DBStatus DBStoreMock::Rekey(const CipherPassword &password)
157 {
158     return NOT_SUPPORT;
159 }
160 
Export(const std::string & filePath,const CipherPassword & passwd,bool force)161 DBStatus DBStoreMock::Export(const std::string &filePath, const CipherPassword &passwd, bool force)
162 {
163     return NOT_SUPPORT;
164 }
165 
Import(const std::string & filePath,const CipherPassword & passwd)166 DBStatus DBStoreMock::Import(const std::string &filePath, const CipherPassword &passwd)
167 {
168     return NOT_SUPPORT;
169 }
170 
StartTransaction()171 DBStatus DBStoreMock::StartTransaction()
172 {
173     return NOT_SUPPORT;
174 }
175 
Commit()176 DBStatus DBStoreMock::Commit()
177 {
178     return NOT_SUPPORT;
179 }
180 
Rollback()181 DBStatus DBStoreMock::Rollback()
182 {
183     return NOT_SUPPORT;
184 }
185 
PutLocalBatch(const std::vector<Entry> & entries)186 DBStatus DBStoreMock::PutLocalBatch(const std::vector<Entry> &entries)
187 {
188     return PutBatch(localEntries_, entries);
189 }
190 
DeleteLocalBatch(const std::vector<Key> & keys)191 DBStatus DBStoreMock::DeleteLocalBatch(const std::vector<Key> &keys)
192 {
193     return DeleteBatch(localEntries_, keys);
194 }
195 
GetSecurityOption(SecurityOption & option) const196 DBStatus DBStoreMock::GetSecurityOption(SecurityOption &option) const
197 {
198     return NOT_SUPPORT;
199 }
200 
SetRemotePushFinishedNotify(const RemotePushFinishedNotifier & notifier)201 DBStatus DBStoreMock::SetRemotePushFinishedNotify(const RemotePushFinishedNotifier &notifier)
202 {
203     return NOT_SUPPORT;
204 }
205 
Sync(const std::vector<std::string> & devices,SyncMode mode,const std::function<void (const std::map<std::string,DBStatus> &)> & onComplete,const Query & query,bool wait)206 DBStatus DBStoreMock::Sync(const std::vector<std::string> &devices, SyncMode mode,
207     const std::function<void(const std::map<std::string, DBStatus> &)> &onComplete, const Query &query, bool wait)
208 {
209     return NOT_SUPPORT;
210 }
211 
CheckIntegrity() const212 DBStatus DBStoreMock::CheckIntegrity() const
213 {
214     return NOT_SUPPORT;
215 }
216 
SetEqualIdentifier(const std::string & identifier,const std::vector<std::string> & targets)217 DBStatus DBStoreMock::SetEqualIdentifier(const std::string &identifier, const std::vector<std::string> &targets)
218 {
219     return NOT_SUPPORT;
220 }
221 
SetPushDataInterceptor(const PushDataInterceptor & interceptor)222 DBStatus DBStoreMock::SetPushDataInterceptor(const PushDataInterceptor &interceptor)
223 {
224     return NOT_SUPPORT;
225 }
SubscribeRemoteQuery(const std::vector<std::string> & devices,const std::function<void (const std::map<std::string,DBStatus> &)> & onComplete,const Query & query,bool wait)226 DBStatus DBStoreMock::SubscribeRemoteQuery(const std::vector<std::string> &devices,
227     const std::function<void(const std::map<std::string, DBStatus> &)> &onComplete, const Query &query, bool wait)
228 {
229     return NOT_SUPPORT;
230 }
231 
UnSubscribeRemoteQuery(const std::vector<std::string> & devices,const std::function<void (const std::map<std::string,DBStatus> &)> & onComplete,const Query & query,bool wait)232 DBStatus DBStoreMock::UnSubscribeRemoteQuery(const std::vector<std::string> &devices,
233     const std::function<void(const std::map<std::string, DBStatus> &)> &onComplete, const Query &query, bool wait)
234 {
235     return NOT_SUPPORT;
236 }
237 
RemoveDeviceData()238 DBStatus DBStoreMock::RemoveDeviceData()
239 {
240     return NOT_SUPPORT;
241 }
242 
GetKeys(const Key & keyPrefix,std::vector<Key> & keys) const243 DBStatus DBStoreMock::GetKeys(const Key &keyPrefix, std::vector<Key> &keys) const
244 {
245     return NOT_SUPPORT;
246 }
247 
GetSyncDataSize(const std::string & device) const248 size_t DBStoreMock::GetSyncDataSize(const std::string &device) const
249 {
250     return DEFAULT_SIZE;
251 }
252 
Get(ConcurrentMap<Key,Value> & store,const Key & key,Value & value) const253 DBStatus DBStoreMock::Get(ConcurrentMap<Key, Value> &store, const Key &key, Value &value) const
254 {
255     auto it = store.Find(key);
256     value = std::move(it.second);
257     return it.first ? OK : NOT_FOUND;
258 }
259 
GetEntries(ConcurrentMap<Key,Value> & store,const Key & keyPrefix,std::vector<Entry> & entries) const260 DBStatus DBStoreMock::GetEntries(ConcurrentMap<Key, Value> &store, const Key &keyPrefix,
261     std::vector<Entry> &entries) const
262 {
263     store.ForEach([&entries, &keyPrefix](const Key &key, Value &value) {
264         auto it = std::search(key.begin(), key.end(), keyPrefix.begin(), keyPrefix.end());
265         if (it == key.begin()) {
266             entries.push_back({key, value});
267         }
268         return false;
269     });
270     return entries.size() > 0 ? OK : NOT_FOUND;
271 }
272 
PutBatch(ConcurrentMap<Key,Value> & store,const std::vector<Entry> & entries)273 DBStatus DBStoreMock::PutBatch(ConcurrentMap<Key, Value> &store, const std::vector<Entry> &entries)
274 {
275     for (auto &entry : entries) {
276         store.InsertOrAssign(entry.key, entry.value);
277     }
278     DBChangeDataMock changeData({}, entries, {});
279     observers_.ForEachCopies([&changeData](const auto &observer, auto &keys) mutable {
280         if (observer) {
281             observer->OnChange(changeData);
282         }
283         return false;
284     });
285     return OK;
286 }
287 
DeleteBatch(ConcurrentMap<Key,Value> & store,const std::vector<Key> & keys)288 DBStatus DBStoreMock::DeleteBatch(ConcurrentMap<Key, Value> &store, const std::vector<Key> &keys)
289 {
290     DBChangeDataMock changeData({}, {}, {});
291     for (auto &key : keys) {
292         auto it = store.Find(key);
293         if (!it.first) {
294             continue;
295         }
296         changeData.AddEntry({key, std::move(it.second)});
297         store.Erase(key);
298     }
299 
300     observers_.ForEachCopies([&changeData](const auto &observer, auto &keys) {
301         if (observer) {
302             observer->OnChange(changeData);
303         }
304         return false;
305     });
306     return OK;
307 }
308 
UpdateKey(const UpdateKeyCallback & callback)309 DBStatus DBStoreMock::UpdateKey(const UpdateKeyCallback &callback)
310 {
311     return NOT_SUPPORT;
312 }
313 
GetWatermarkInfo(const std::string & device)314 std::pair<DBStatus, WatermarkInfo> DBStoreMock::GetWatermarkInfo(const std::string &device)
315 {
316     WatermarkInfo mark;
317     return { DBStatus::OK, mark };
318 }
319 
Sync(const CloudSyncOption & option,const SyncProcessCallback & onProcess)320 DBStatus DBStoreMock::Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess)
321 {
322     return NOT_SUPPORT;
323 }
324 
SetCloudDB(const std::map<std::string,std::shared_ptr<ICloudDb>> & cloudDBs)325 DBStatus DBStoreMock::SetCloudDB(const std::map<std::string, std::shared_ptr<ICloudDb>> &cloudDBs)
326 {
327     return NOT_SUPPORT;
328 }
329 
SetCloudDbSchema(const std::map<std::string,DataBaseSchema> & schema)330 DBStatus DBStoreMock::SetCloudDbSchema(const std::map<std::string, DataBaseSchema> &schema)
331 {
332     return NOT_SUPPORT;
333 }
334 
RemoveDeviceData(const std::string & device,ClearMode mode)335 DBStatus DBStoreMock::RemoveDeviceData(const std::string &device, ClearMode mode)
336 {
337     return NOT_SUPPORT;
338 }
339 
RemoveDeviceData(const std::string & device,const std::string & user,ClearMode mode)340 DBStatus DBStoreMock::RemoveDeviceData(const std::string &device, const std::string &user,
341     ClearMode mode)
342 {
343     return NOT_SUPPORT;
344 }
345 
GetTaskCount()346 int32_t DBStoreMock::GetTaskCount()
347 {
348     return 0;
349 }
350 
SetGenCloudVersionCallback(const GenerateCloudVersionCallback & callback)351 void DBStoreMock::SetGenCloudVersionCallback(const GenerateCloudVersionCallback &callback)
352 {
353 }
354 
GetCloudVersion(const std::string & device)355 std::pair<DBStatus, std::map<std::string, std::string>> DBStoreMock::GetCloudVersion(const std::string &device)
356 {
357     return { NOT_SUPPORT, {} };
358 }
359 
SetCloudSyncConfig(const CloudSyncConfig & config)360 DBStatus DBStoreMock::SetCloudSyncConfig(const CloudSyncConfig &config)
361 {
362     return NOT_SUPPORT;
363 }
364 
SetReceiveDataInterceptor(const DataInterceptor & interceptor)365 DBStatus DBStoreMock::SetReceiveDataInterceptor(const DataInterceptor &interceptor)
366 {
367     return NOT_SUPPORT;
368 }
369 
GetDeviceEntries(const std::string & device,std::vector<Entry> & entries) const370 DBStatus DBStoreMock::GetDeviceEntries(const std::string &device, std::vector<Entry> &entries) const
371 {
372     return NOT_SUPPORT;
373 }
374 } // namespace DistributedData
375 } // namespace OHOS