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 #include "kv_store_nb_delegate_impl.h"
17
18 #include <functional>
19 #include <string>
20
21 #include "db_common.h"
22 #include "db_constant.h"
23 #include "db_errno.h"
24 #include "db_types.h"
25 #include "kv_store_changed_data_impl.h"
26 #include "kv_store_errno.h"
27 #include "kv_store_nb_conflict_data_impl.h"
28 #include "kv_store_observer.h"
29 #include "kv_store_result_set_impl.h"
30 #include "kvdb_manager.h"
31 #include "kvdb_pragma.h"
32 #include "log_print.h"
33 #include "param_check_utils.h"
34 #include "performance_analysis.h"
35 #include "platform_specific.h"
36 #include "store_types.h"
37 #include "sync_operation.h"
38
39 namespace DistributedDB {
40 namespace {
41 struct PragmaCmdPair {
42 int externCmd = 0;
43 int innerCmd = 0;
44 };
45
46 const PragmaCmdPair g_pragmaMap[] = {
47 {GET_DEVICE_IDENTIFIER_OF_ENTRY, PRAGMA_GET_DEVICE_IDENTIFIER_OF_ENTRY},
48 {AUTO_SYNC, PRAGMA_AUTO_SYNC},
49 {PERFORMANCE_ANALYSIS_GET_REPORT, PRAGMA_PERFORMANCE_ANALYSIS_GET_REPORT},
50 {PERFORMANCE_ANALYSIS_OPEN, PRAGMA_PERFORMANCE_ANALYSIS_OPEN},
51 {PERFORMANCE_ANALYSIS_CLOSE, PRAGMA_PERFORMANCE_ANALYSIS_CLOSE},
52 {PERFORMANCE_ANALYSIS_SET_REPORTFILENAME, PRAGMA_PERFORMANCE_ANALYSIS_SET_REPORTFILENAME},
53 {GET_IDENTIFIER_OF_DEVICE, PRAGMA_GET_IDENTIFIER_OF_DEVICE},
54 {GET_QUEUED_SYNC_SIZE, PRAGMA_GET_QUEUED_SYNC_SIZE},
55 {SET_QUEUED_SYNC_LIMIT, PRAGMA_SET_QUEUED_SYNC_LIMIT},
56 {GET_QUEUED_SYNC_LIMIT, PRAGMA_GET_QUEUED_SYNC_LIMIT},
57 {SET_WIPE_POLICY, PRAGMA_SET_WIPE_POLICY},
58 {RESULT_SET_CACHE_MODE, PRAGMA_RESULT_SET_CACHE_MODE},
59 {RESULT_SET_CACHE_MAX_SIZE, PRAGMA_RESULT_SET_CACHE_MAX_SIZE},
60 {SET_SYNC_RETRY, PRAGMA_SET_SYNC_RETRY},
61 {SET_MAX_LOG_LIMIT, PRAGMA_SET_MAX_LOG_LIMIT},
62 {EXEC_CHECKPOINT, PRAGMA_EXEC_CHECKPOINT},
63 {SET_MAX_VALUE_SIZE, PRAGMA_SET_MAX_VALUE_SIZE},
64 };
65
66 constexpr const char *INVALID_CONNECTION = "[KvStoreNbDelegate] Invalid connection for operation";
67 }
68
KvStoreNbDelegateImpl(IKvDBConnection * conn,const std::string & storeId)69 KvStoreNbDelegateImpl::KvStoreNbDelegateImpl(IKvDBConnection *conn, const std::string &storeId)
70 : conn_(conn),
71 storeId_(storeId),
72 releaseFlag_(false)
73 {}
74
~KvStoreNbDelegateImpl()75 KvStoreNbDelegateImpl::~KvStoreNbDelegateImpl()
76 {
77 if (!releaseFlag_) {
78 LOGF("[KvStoreNbDelegate] Can't release directly");
79 return;
80 }
81
82 conn_ = nullptr;
83 }
84
Get(const Key & key,Value & value) const85 DBStatus KvStoreNbDelegateImpl::Get(const Key &key, Value &value) const
86 {
87 IOption option;
88 option.dataType = IOption::SYNC_DATA;
89 return GetInner(option, key, value);
90 }
91
GetEntries(const Key & keyPrefix,std::vector<Entry> & entries) const92 DBStatus KvStoreNbDelegateImpl::GetEntries(const Key &keyPrefix, std::vector<Entry> &entries) const
93 {
94 IOption option;
95 option.dataType = IOption::SYNC_DATA;
96 return GetEntriesInner(option, keyPrefix, entries);
97 }
98
GetEntries(const Key & keyPrefix,KvStoreResultSet * & resultSet) const99 DBStatus KvStoreNbDelegateImpl::GetEntries(const Key &keyPrefix, KvStoreResultSet *&resultSet) const
100 {
101 if (conn_ == nullptr) {
102 LOGE("%s", INVALID_CONNECTION);
103 return DB_ERROR;
104 }
105
106 IOption option;
107 option.dataType = IOption::SYNC_DATA;
108 IKvDBResultSet *kvDbResultSet = nullptr;
109 int errCode = conn_->GetResultSet(option, keyPrefix, kvDbResultSet);
110 if (errCode == E_OK) {
111 resultSet = new (std::nothrow) KvStoreResultSetImpl(kvDbResultSet);
112 if (resultSet != nullptr) {
113 return OK;
114 }
115
116 LOGE("[KvStoreNbDelegate] Alloc result set failed.");
117 conn_->ReleaseResultSet(kvDbResultSet);
118 kvDbResultSet = nullptr;
119 return DB_ERROR;
120 }
121
122 LOGE("[KvStoreNbDelegate] Get result set failed: %d", errCode);
123 return TransferDBErrno(errCode);
124 }
125
GetEntries(const Query & query,std::vector<Entry> & entries) const126 DBStatus KvStoreNbDelegateImpl::GetEntries(const Query &query, std::vector<Entry> &entries) const
127 {
128 IOption option;
129 option.dataType = IOption::SYNC_DATA;
130 if (conn_ != nullptr) {
131 int errCode = conn_->GetEntries(option, query, entries);
132 if (errCode == E_OK) {
133 return OK;
134 } else if (errCode == -E_NOT_FOUND) {
135 LOGD("[KvStoreNbDelegate] Not found the data by query");
136 return NOT_FOUND;
137 }
138
139 LOGE("[KvStoreNbDelegate] Get the batch data by query err:%d", errCode);
140 return TransferDBErrno(errCode);
141 }
142
143 LOGE("%s", INVALID_CONNECTION);
144 return DB_ERROR;
145 }
146
GetEntries(const Query & query,KvStoreResultSet * & resultSet) const147 DBStatus KvStoreNbDelegateImpl::GetEntries(const Query &query, KvStoreResultSet *&resultSet) const
148 {
149 if (conn_ == nullptr) {
150 LOGE("%s", INVALID_CONNECTION);
151 return DB_ERROR;
152 }
153
154 IOption option;
155 option.dataType = IOption::SYNC_DATA;
156 IKvDBResultSet *kvDbResultSet = nullptr;
157 int errCode = conn_->GetResultSet(option, query, kvDbResultSet);
158 if (errCode == E_OK) {
159 resultSet = new (std::nothrow) KvStoreResultSetImpl(kvDbResultSet);
160 if (resultSet != nullptr) {
161 return OK;
162 }
163
164 LOGE("[KvStoreNbDelegate] Alloc result set failed.");
165 conn_->ReleaseResultSet(kvDbResultSet);
166 kvDbResultSet = nullptr;
167 return DB_ERROR;
168 }
169
170 LOGE("[KvStoreNbDelegate] Get result set for query failed: %d", errCode);
171 return TransferDBErrno(errCode);
172 }
173
GetCount(const Query & query,int & count) const174 DBStatus KvStoreNbDelegateImpl::GetCount(const Query &query, int &count) const
175 {
176 if (conn_ == nullptr) {
177 LOGE("%s", INVALID_CONNECTION);
178 return DB_ERROR;
179 }
180
181 IOption option;
182 option.dataType = IOption::SYNC_DATA;
183 int errCode = conn_->GetCount(option, query, count);
184 if (errCode == E_OK) {
185 if (count == 0) {
186 return NOT_FOUND;
187 }
188 return OK;
189 }
190
191 LOGE("[KvStoreNbDelegate] Get count for query failed: %d", errCode);
192 return TransferDBErrno(errCode);
193 }
194
CloseResultSet(KvStoreResultSet * & resultSet)195 DBStatus KvStoreNbDelegateImpl::CloseResultSet(KvStoreResultSet *&resultSet)
196 {
197 if (resultSet == nullptr) {
198 return INVALID_ARGS;
199 }
200
201 if (conn_ == nullptr) {
202 LOGE("%s", INVALID_CONNECTION);
203 return DB_ERROR;
204 }
205
206 // release inner result set
207 IKvDBResultSet *kvDbResultSet = nullptr;
208 (static_cast<KvStoreResultSetImpl *>(resultSet))->GetResultSet(kvDbResultSet);
209 conn_->ReleaseResultSet(kvDbResultSet);
210 // release external result set
211 delete resultSet;
212 resultSet = nullptr;
213 return OK;
214 }
215
Put(const Key & key,const Value & value)216 DBStatus KvStoreNbDelegateImpl::Put(const Key &key, const Value &value)
217 {
218 IOption option;
219 option.dataType = IOption::SYNC_DATA;
220 return PutInner(option, key, value);
221 }
222
PutBatch(const std::vector<Entry> & entries)223 DBStatus KvStoreNbDelegateImpl::PutBatch(const std::vector<Entry> &entries)
224 {
225 if (conn_ != nullptr) {
226 IOption option;
227 option.dataType = IOption::SYNC_DATA;
228 int errCode = conn_->PutBatch(option, entries);
229 if (errCode == E_OK) {
230 return OK;
231 }
232
233 LOGE("[KvStoreNbDelegate] Put batch data failed:%d", errCode);
234 return TransferDBErrno(errCode);
235 }
236
237 LOGE("%s", INVALID_CONNECTION);
238 return DB_ERROR;
239 }
240
DeleteBatch(const std::vector<Key> & keys)241 DBStatus KvStoreNbDelegateImpl::DeleteBatch(const std::vector<Key> &keys)
242 {
243 if (conn_ == nullptr) {
244 LOGE("%s", INVALID_CONNECTION);
245 return DB_ERROR;
246 }
247
248 IOption option;
249 option.dataType = IOption::SYNC_DATA;
250 int errCode = conn_->DeleteBatch(option, keys);
251 if (errCode == E_OK || errCode == -E_NOT_FOUND) {
252 return OK;
253 }
254
255 LOGE("[KvStoreNbDelegate] Delete batch data failed:%d", errCode);
256 return TransferDBErrno(errCode);
257 }
258
Delete(const Key & key)259 DBStatus KvStoreNbDelegateImpl::Delete(const Key &key)
260 {
261 IOption option;
262 option.dataType = IOption::SYNC_DATA;
263 return DeleteInner(option, key);
264 }
265
GetLocal(const Key & key,Value & value) const266 DBStatus KvStoreNbDelegateImpl::GetLocal(const Key &key, Value &value) const
267 {
268 IOption option;
269 option.dataType = IOption::LOCAL_DATA;
270 return GetInner(option, key, value);
271 }
272
GetLocalEntries(const Key & keyPrefix,std::vector<Entry> & entries) const273 DBStatus KvStoreNbDelegateImpl::GetLocalEntries(const Key &keyPrefix, std::vector<Entry> &entries) const
274 {
275 IOption option;
276 option.dataType = IOption::LOCAL_DATA;
277 return GetEntriesInner(option, keyPrefix, entries);
278 }
279
PutLocal(const Key & key,const Value & value)280 DBStatus KvStoreNbDelegateImpl::PutLocal(const Key &key, const Value &value)
281 {
282 IOption option;
283 option.dataType = IOption::LOCAL_DATA;
284 return PutInner(option, key, value);
285 }
286
DeleteLocal(const Key & key)287 DBStatus KvStoreNbDelegateImpl::DeleteLocal(const Key &key)
288 {
289 IOption option;
290 option.dataType = IOption::LOCAL_DATA;
291 return DeleteInner(option, key);
292 }
293
PublishLocal(const Key & key,bool deleteLocal,bool updateTimestamp,const KvStoreNbPublishOnConflict & onConflict)294 DBStatus KvStoreNbDelegateImpl::PublishLocal(const Key &key, bool deleteLocal, bool updateTimestamp,
295 const KvStoreNbPublishOnConflict &onConflict)
296 {
297 if (key.empty() || key.size() > DBConstant::MAX_KEY_SIZE) {
298 LOGW("[KvStoreNbDelegate][Publish] Invalid para");
299 return INVALID_ARGS;
300 }
301
302 if (conn_ != nullptr) {
303 PragmaPublishInfo publishInfo{ key, deleteLocal, updateTimestamp, onConflict };
304 int errCode = conn_->Pragma(PRAGMA_PUBLISH_LOCAL, static_cast<PragmaData>(&publishInfo));
305 if (errCode != E_OK) {
306 LOGE("[KvStoreNbDelegate] Publish local err:%d", errCode);
307 }
308 return TransferDBErrno(errCode);
309 }
310
311 LOGE("%s", INVALID_CONNECTION);
312 return DB_ERROR;
313 }
314
UnpublishToLocal(const Key & key,bool deletePublic,bool updateTimestamp)315 DBStatus KvStoreNbDelegateImpl::UnpublishToLocal(const Key &key, bool deletePublic, bool updateTimestamp)
316 {
317 if (key.empty() || key.size() > DBConstant::MAX_KEY_SIZE) {
318 LOGW("[KvStoreNbDelegate][Unpublish] Invalid para");
319 return INVALID_ARGS;
320 }
321
322 if (conn_ != nullptr) {
323 PragmaUnpublishInfo unpublishInfo{ key, deletePublic, updateTimestamp };
324 int errCode = conn_->Pragma(PRAGMA_UNPUBLISH_SYNC, static_cast<PragmaData>(&unpublishInfo));
325 if (errCode != E_OK) {
326 LOGE("[KvStoreNbDelegate] Unpublish result:%d", errCode);
327 }
328 return TransferDBErrno(errCode);
329 }
330
331 LOGE("%s", INVALID_CONNECTION);
332 return DB_ERROR;
333 }
334
PutLocalBatch(const std::vector<Entry> & entries)335 DBStatus KvStoreNbDelegateImpl::PutLocalBatch(const std::vector<Entry> &entries)
336 {
337 if (conn_ == nullptr) {
338 LOGE("%s", INVALID_CONNECTION);
339 return DB_ERROR;
340 }
341
342 IOption option;
343 option.dataType = IOption::LOCAL_DATA;
344 int errCode = conn_->PutBatch(option, entries);
345 if (errCode != E_OK) {
346 LOGE("[KvStoreNbDelegate] Put local batch data failed:%d", errCode);
347 }
348 return TransferDBErrno(errCode);
349 }
350
DeleteLocalBatch(const std::vector<Key> & keys)351 DBStatus KvStoreNbDelegateImpl::DeleteLocalBatch(const std::vector<Key> &keys)
352 {
353 if (conn_ == nullptr) {
354 LOGE("%s", INVALID_CONNECTION);
355 return DB_ERROR;
356 }
357
358 IOption option;
359 option.dataType = IOption::LOCAL_DATA;
360 int errCode = conn_->DeleteBatch(option, keys);
361 if (errCode == E_OK || errCode == -E_NOT_FOUND) {
362 return OK;
363 }
364
365 LOGE("[KvStoreNbDelegate] Delete local batch data failed:%d", errCode);
366 return TransferDBErrno(errCode);
367 }
368
RegisterObserver(const Key & key,unsigned int mode,KvStoreObserver * observer)369 DBStatus KvStoreNbDelegateImpl::RegisterObserver(const Key &key, unsigned int mode, KvStoreObserver *observer)
370 {
371 if (key.size() > DBConstant::MAX_KEY_SIZE) {
372 return INVALID_ARGS;
373 }
374 uint64_t rawMode = DBCommon::EraseBit(mode, DBConstant::OBSERVER_CHANGES_MASK);
375 if (rawMode == static_cast<uint64_t>(ObserverMode::OBSERVER_CHANGES_CLOUD)) {
376 return RegisterCloudObserver(key, mode, observer);
377 }
378 return RegisterDeviceObserver(key, static_cast<unsigned int>(rawMode), observer);
379 }
380
RegisterDeviceObserver(const Key & key,unsigned int mode,KvStoreObserver * observer)381 DBStatus KvStoreNbDelegateImpl::RegisterDeviceObserver(const Key &key, unsigned int mode, KvStoreObserver *observer)
382 {
383 if (!ParamCheckUtils::CheckObserver(key, mode)) {
384 LOGE("Register nb observer by illegal mode or key size!");
385 return INVALID_ARGS;
386 }
387
388 if (observer == nullptr) {
389 return INVALID_ARGS;
390 }
391
392 std::lock_guard<std::mutex> lockGuard(observerMapLock_);
393 if (observerMap_.size() >= DBConstant::MAX_OBSERVER_COUNT) {
394 LOGE("[KvStoreNbDelegate] The number of kv observers has been over limit, storeId[%.3s]", storeId_.c_str());
395 return OVER_MAX_LIMITS;
396 }
397 if (observerMap_.find(observer) != observerMap_.end()) {
398 LOGE("[KvStoreNbDelegate] Observer has been already registered!");
399 return ALREADY_SET;
400 }
401
402 if (conn_ == nullptr) {
403 LOGE("%s", INVALID_CONNECTION);
404 return DB_ERROR;
405 }
406
407 if (conn_->IsTransactionStarted()) {
408 return BUSY;
409 }
410
411 int errCode = E_OK;
412 auto storeId = storeId_;
413 KvDBObserverHandle *observerHandle = conn_->RegisterObserver(
414 mode, key,
415 [observer, storeId](const KvDBCommitNotifyData ¬ifyData) {
416 KvStoreChangedDataImpl data(¬ifyData);
417 LOGD("[KvStoreNbDelegate] Trigger [%s] on change", storeId.c_str());
418 observer->OnChange(data);
419 },
420 errCode);
421
422 if (errCode != E_OK || observerHandle == nullptr) {
423 LOGE("[KvStoreNbDelegate] RegisterListener failed:%d!", errCode);
424 return DB_ERROR;
425 }
426
427 observerMap_.insert(std::pair<const KvStoreObserver *, const KvDBObserverHandle *>(observer, observerHandle));
428 LOGI("[KvStoreNbDelegate] RegisterDeviceObserver ok mode:%u", mode);
429 return OK;
430 }
431
RegisterCloudObserver(const Key & key,unsigned int mode,KvStoreObserver * observer)432 DBStatus KvStoreNbDelegateImpl::RegisterCloudObserver(const Key &key, unsigned int mode,
433 KvStoreObserver *observer)
434 {
435 if (conn_ == nullptr) {
436 LOGE("%s", INVALID_CONNECTION);
437 return DB_ERROR;
438 }
439
440 std::lock_guard<std::mutex> lockGuard(observerMapLock_);
441 if (cloudObserverMap_.size() >= DBConstant::MAX_OBSERVER_COUNT) {
442 LOGE("[KvStoreNbDelegate] The number of kv cloud observers has been over limit, storeId[%.3s]",
443 storeId_.c_str());
444 return OVER_MAX_LIMITS;
445 }
446 if (cloudObserverMap_.find(observer) != cloudObserverMap_.end() && cloudObserverMap_[observer] == mode) {
447 LOGE("[KvStoreNbDelegate] Cloud observer has been already registered!");
448 return ALREADY_SET;
449 }
450
451 auto storeId = storeId_;
452 ObserverAction action = [observer, storeId](
453 const std::string &device, ChangedData &&changedData, bool isChangedData) {
454 if (isChangedData) {
455 LOGD("[KvStoreNbDelegate] Trigger [%s] on change", storeId.c_str());
456 observer->OnChange(Origin::ORIGIN_CLOUD, device, std::move(changedData));
457 }
458 };
459 int errCode = conn_->RegisterObserverAction(observer, action);
460 if (errCode != E_OK) {
461 LOGE("[KvStoreNbDelegate] RegisterCloudObserver failed:%d!", errCode);
462 return DB_ERROR;
463 }
464 cloudObserverMap_[observer] = mode;
465 LOGI("[KvStoreNbDelegate] RegisterCloudObserver ok mode:%u", mode);
466 return OK;
467 }
468
UnRegisterObserver(const KvStoreObserver * observer)469 DBStatus KvStoreNbDelegateImpl::UnRegisterObserver(const KvStoreObserver *observer)
470 {
471 if (observer == nullptr) {
472 return INVALID_ARGS;
473 }
474
475 if (conn_ == nullptr) {
476 LOGE("%s", INVALID_CONNECTION);
477 return DB_ERROR;
478 }
479
480 DBStatus cloudRet = UnRegisterCloudObserver(observer);
481 DBStatus devRet = UnRegisterDeviceObserver(observer);
482 if (cloudRet == OK || devRet == OK) {
483 return OK;
484 }
485 return devRet;
486 }
487
UnRegisterDeviceObserver(const KvStoreObserver * observer)488 DBStatus KvStoreNbDelegateImpl::UnRegisterDeviceObserver(const KvStoreObserver *observer)
489 {
490 std::lock_guard<std::mutex> lockGuard(observerMapLock_);
491 auto iter = observerMap_.find(observer);
492 if (iter == observerMap_.end()) {
493 LOGE("[KvStoreNbDelegate] [%s] Observer has not been registered!", storeId_.c_str());
494 return NOT_FOUND;
495 }
496
497 const KvDBObserverHandle *observerHandle = iter->second;
498 int errCode = conn_->UnRegisterObserver(observerHandle);
499 if (errCode != E_OK) {
500 LOGE("[KvStoreNbDelegate] UnRegistObserver failed:%d!", errCode);
501 return DB_ERROR;
502 }
503 observerMap_.erase(iter);
504 return OK;
505 }
506
UnRegisterCloudObserver(const KvStoreObserver * observer)507 DBStatus KvStoreNbDelegateImpl::UnRegisterCloudObserver(const KvStoreObserver *observer)
508 {
509 std::lock_guard<std::mutex> lockGuard(observerMapLock_);
510 auto iter = cloudObserverMap_.find(observer);
511 if (iter == cloudObserverMap_.end()) {
512 LOGW("[KvStoreNbDelegate] [%s] CloudObserver has not been registered!", storeId_.c_str());
513 return NOT_FOUND;
514 }
515 int errCode = conn_->UnRegisterObserverAction(observer);
516 if (errCode != E_OK) {
517 LOGE("[KvStoreNbDelegate] UnRegisterCloudObserver failed:%d!", errCode);
518 return DB_ERROR;
519 }
520 cloudObserverMap_.erase(iter);
521 return OK;
522 }
523
RemoveDeviceData(const std::string & device)524 DBStatus KvStoreNbDelegateImpl::RemoveDeviceData(const std::string &device)
525 {
526 if (conn_ == nullptr) {
527 LOGE("%s", INVALID_CONNECTION);
528 return DB_ERROR;
529 }
530 if (device.empty() || device.length() > DBConstant::MAX_DEV_LENGTH) {
531 return INVALID_ARGS;
532 }
533 int errCode = conn_->Pragma(PRAGMA_RM_DEVICE_DATA,
534 const_cast<void *>(static_cast<const void *>(&device)));
535 if (errCode != E_OK) {
536 LOGE("[KvStoreNbDelegate] Remove device data failed:%d", errCode);
537 }
538 return TransferDBErrno(errCode);
539 }
540
GetStoreId() const541 std::string KvStoreNbDelegateImpl::GetStoreId() const
542 {
543 return storeId_;
544 }
545
Sync(const std::vector<std::string> & devices,SyncMode mode,const std::function<void (const std::map<std::string,DBStatus> & devicesMap)> & onComplete,bool wait=false)546 DBStatus KvStoreNbDelegateImpl::Sync(const std::vector<std::string> &devices, SyncMode mode,
547 const std::function<void(const std::map<std::string, DBStatus> &devicesMap)> &onComplete,
548 bool wait = false)
549 {
550 if (conn_ == nullptr) {
551 LOGE("%s", INVALID_CONNECTION);
552 return DB_ERROR;
553 }
554 if (mode > SYNC_MODE_PUSH_PULL) {
555 LOGE("not support other mode");
556 return NOT_SUPPORT;
557 }
558
559 PragmaSync pragmaData(
560 devices, mode, [this, onComplete](const std::map<std::string, int> &statuses) {
561 OnSyncComplete(statuses, onComplete);
562 }, wait);
563 int errCode = conn_->Pragma(PRAGMA_SYNC_DEVICES, &pragmaData);
564 if (errCode < E_OK) {
565 LOGE("[KvStoreNbDelegate] Sync data failed:%d", errCode);
566 return TransferDBErrno(errCode);
567 }
568 return OK;
569 }
570
Sync(const std::vector<std::string> & devices,SyncMode mode,const std::function<void (const std::map<std::string,DBStatus> & devicesMap)> & onComplete,const Query & query,bool wait)571 DBStatus KvStoreNbDelegateImpl::Sync(const std::vector<std::string> &devices, SyncMode mode,
572 const std::function<void(const std::map<std::string, DBStatus> &devicesMap)> &onComplete,
573 const Query &query, bool wait)
574 {
575 if (conn_ == nullptr) {
576 LOGE("%s", INVALID_CONNECTION);
577 return DB_ERROR;
578 }
579 if (mode > SYNC_MODE_PUSH_PULL) {
580 LOGE("not support other mode");
581 return NOT_SUPPORT;
582 }
583
584 if (!DBCommon::CheckQueryWithoutMultiTable(query)) {
585 LOGE("not support for invalid query");
586 return NOT_SUPPORT;
587 }
588 QuerySyncObject querySyncObj(query);
589 if (querySyncObj.GetSortType() != SortType::NONE || querySyncObj.IsQueryByRange()) {
590 LOGE("not support order by timestamp and query by range");
591 return NOT_SUPPORT;
592 }
593 PragmaSync pragmaData(
594 devices, mode, querySyncObj, [this, onComplete](const std::map<std::string, int> &statuses) {
595 OnSyncComplete(statuses, onComplete);
596 }, wait);
597 int errCode = conn_->Pragma(PRAGMA_SYNC_DEVICES, &pragmaData);
598 if (errCode < E_OK) {
599 LOGE("[KvStoreNbDelegate] QuerySync data failed:%d", errCode);
600 return TransferDBErrno(errCode);
601 }
602 return OK;
603 }
604
OnDeviceSyncProcess(const std::map<std::string,DeviceSyncProcess> & processMap,const DeviceSyncProcessCallback & onProcess) const605 void KvStoreNbDelegateImpl::OnDeviceSyncProcess(const std::map<std::string, DeviceSyncProcess> &processMap,
606 const DeviceSyncProcessCallback &onProcess) const
607 {
608 std::map<std::string, DeviceSyncProcess> result;
609 for (const auto &pair : processMap) {
610 DeviceSyncProcess info = pair.second;
611 int status = info.errCode;
612 info.errCode = SyncOperation::DBStatusTrans(status);
613 info.process = SyncOperation::DBStatusTransProcess(status);
614 result.insert(std::pair<std::string, DeviceSyncProcess>(pair.first, info));
615 }
616 if (onProcess) {
617 onProcess(result);
618 }
619 }
620
Sync(const DeviceSyncOption & option,const DeviceSyncProcessCallback & onProcess)621 DBStatus KvStoreNbDelegateImpl::Sync(const DeviceSyncOption &option, const DeviceSyncProcessCallback &onProcess)
622 {
623 if (conn_ == nullptr) {
624 LOGE("%s", INVALID_CONNECTION);
625 return DB_ERROR;
626 }
627 if (option.mode != SYNC_MODE_PULL_ONLY) {
628 LOGE("Not support other mode");
629 return NOT_SUPPORT;
630 }
631 DeviceSyncProcessCallback onSyncProcess = [this, onProcess](const std::map<std::string, DeviceSyncProcess> &map) {
632 OnDeviceSyncProcess(map, onProcess);
633 };
634 int errCode = E_OK;
635 if (option.isQuery) {
636 QuerySyncObject querySyncObj(option.query);
637 if (!DBCommon::CheckQueryWithoutMultiTable(option.query)) {
638 LOGE("Not support for invalid query");
639 return NOT_SUPPORT;
640 }
641
642 if (querySyncObj.GetSortType() != SortType::NONE || querySyncObj.IsQueryByRange()) {
643 LOGE("Not support order by timestamp and query by range");
644 return NOT_SUPPORT;
645 }
646 PragmaSync pragmaData(option, querySyncObj, onSyncProcess);
647 errCode = conn_->Pragma(PRAGMA_SYNC_DEVICES, &pragmaData);
648 } else {
649 PragmaSync pragmaData(option, onSyncProcess);
650 errCode = conn_->Pragma(PRAGMA_SYNC_DEVICES, &pragmaData);
651 }
652
653 if (errCode != E_OK) {
654 LOGE("[KvStoreNbDelegate] DeviceSync data failed:%d", errCode);
655 return TransferDBErrno(errCode);
656 }
657 return OK;
658 }
659
CancelSync(uint32_t syncId)660 DBStatus KvStoreNbDelegateImpl::CancelSync(uint32_t syncId)
661 {
662 if (conn_ == nullptr) {
663 LOGE("%s", INVALID_CONNECTION);
664 return DB_ERROR;
665 }
666 uint32_t tempSyncId = syncId;
667 int errCode = conn_->Pragma(PRAGMA_CANCEL_SYNC_DEVICES, &tempSyncId);
668 if (errCode != E_OK) {
669 LOGE("[KvStoreNbDelegate] CancelSync failed:%d", errCode);
670 return TransferDBErrno(errCode);
671 }
672 return OK;
673 }
674
Pragma(PragmaCmd cmd,PragmaData & paramData)675 DBStatus KvStoreNbDelegateImpl::Pragma(PragmaCmd cmd, PragmaData ¶mData)
676 {
677 if (conn_ == nullptr) {
678 LOGE("%s", INVALID_CONNECTION);
679 return DB_ERROR;
680 }
681
682 int errCode = -E_NOT_SUPPORT;
683 for (const auto &item : g_pragmaMap) {
684 if (item.externCmd == cmd) {
685 errCode = conn_->Pragma(item.innerCmd, paramData);
686 break;
687 }
688 }
689
690 if (errCode != E_OK) {
691 LOGE("[KvStoreNbDelegate] Pragma failed:%d", errCode);
692 }
693 return TransferDBErrno(errCode);
694 }
695
SetConflictNotifier(int conflictType,const KvStoreNbConflictNotifier & notifier)696 DBStatus KvStoreNbDelegateImpl::SetConflictNotifier(int conflictType, const KvStoreNbConflictNotifier ¬ifier)
697 {
698 if (conn_ == nullptr) {
699 LOGE("%s", INVALID_CONNECTION);
700 return DB_ERROR;
701 }
702
703 if (!ParamCheckUtils::CheckConflictNotifierType(conflictType)) {
704 LOGE("%s", INVALID_CONNECTION);
705 return INVALID_ARGS;
706 }
707
708 int errCode;
709 if (!notifier) {
710 errCode = conn_->SetConflictNotifier(conflictType, nullptr);
711 goto END;
712 }
713
714 errCode = conn_->SetConflictNotifier(conflictType,
715 [conflictType, notifier](const KvDBCommitNotifyData &data) {
716 int resultCode;
717 const std::list<KvDBConflictEntry> entries = data.GetCommitConflicts(resultCode);
718 if (resultCode != E_OK) {
719 LOGE("Get commit conflicted entries failed:%d!", resultCode);
720 return;
721 }
722
723 for (const auto &entry : entries) {
724 // Prohibit signed numbers to perform bit operations
725 uint32_t entryType = static_cast<uint32_t>(entry.type);
726 uint32_t type = static_cast<uint32_t>(conflictType);
727 if (entryType & type) {
728 KvStoreNbConflictDataImpl dataImpl;
729 dataImpl.SetConflictData(entry);
730 notifier(dataImpl);
731 }
732 }
733 });
734
735 END:
736 if (errCode != E_OK) {
737 LOGE("[KvStoreNbDelegate] Register conflict failed:%d!", errCode);
738 }
739 return TransferDBErrno(errCode);
740 }
741
Rekey(const CipherPassword & password)742 DBStatus KvStoreNbDelegateImpl::Rekey(const CipherPassword &password)
743 {
744 if (conn_ == nullptr) {
745 LOGE("%s", INVALID_CONNECTION);
746 return DB_ERROR;
747 }
748
749 int errCode = conn_->Rekey(password);
750 if (errCode == E_OK) {
751 return OK;
752 }
753
754 LOGE("[KvStoreNbDelegate] Rekey failed:%d", errCode);
755 return TransferDBErrno(errCode);
756 }
757
Export(const std::string & filePath,const CipherPassword & passwd,bool force)758 DBStatus KvStoreNbDelegateImpl::Export(const std::string &filePath, const CipherPassword &passwd, bool force)
759 {
760 if (conn_ == nullptr) {
761 LOGE("%s", INVALID_CONNECTION);
762 return DB_ERROR;
763 }
764
765 std::string fileDir;
766 std::string fileName;
767 OS::SplitFilePath(filePath, fileDir, fileName);
768
769 std::string canonicalUrl;
770 if (!ParamCheckUtils::CheckDataDir(fileDir, canonicalUrl)) {
771 return INVALID_ARGS;
772 }
773
774 if (!OS::CheckPathExistence(canonicalUrl)) {
775 return NO_PERMISSION;
776 }
777
778 canonicalUrl = canonicalUrl + "/" + fileName;
779 if (!force && OS::CheckPathExistence(canonicalUrl)) {
780 return FILE_ALREADY_EXISTED;
781 }
782
783 int errCode = conn_->Export(canonicalUrl, passwd);
784 if (errCode == E_OK) {
785 return OK;
786 }
787 LOGE("[KvStoreNbDelegate] Export failed:%d", errCode);
788 return TransferDBErrno(errCode);
789 }
790
Import(const std::string & filePath,const CipherPassword & passwd)791 DBStatus KvStoreNbDelegateImpl::Import(const std::string &filePath, const CipherPassword &passwd)
792 {
793 if (conn_ == nullptr) {
794 LOGE("%s", INVALID_CONNECTION);
795 return DB_ERROR;
796 }
797
798 std::string fileDir;
799 std::string fileName;
800 OS::SplitFilePath(filePath, fileDir, fileName);
801
802 std::string canonicalUrl;
803 if (!ParamCheckUtils::CheckDataDir(fileDir, canonicalUrl)) {
804 return INVALID_ARGS;
805 }
806
807 canonicalUrl = canonicalUrl + "/" + fileName;
808 if (!OS::CheckPathExistence(canonicalUrl)) {
809 LOGE("Import file path err, DBStatus = INVALID_FILE errno = [%d]", errno);
810 return INVALID_FILE;
811 }
812
813 int errCode = conn_->Import(canonicalUrl, passwd);
814 if (errCode == E_OK) {
815 LOGI("[KvStoreNbDelegate] Import ok");
816 return OK;
817 }
818
819 LOGE("[KvStoreNbDelegate] Import failed:%d", errCode);
820 return TransferDBErrno(errCode);
821 }
822
StartTransaction()823 DBStatus KvStoreNbDelegateImpl::StartTransaction()
824 {
825 if (conn_ == nullptr) {
826 LOGE("%s", INVALID_CONNECTION);
827 return DB_ERROR;
828 }
829
830 int errCode = conn_->StartTransaction();
831 if (errCode != E_OK) {
832 LOGE("[KvStoreNbDelegate] StartTransaction failed:%d", errCode);
833 }
834 return TransferDBErrno(errCode);
835 }
836
Commit()837 DBStatus KvStoreNbDelegateImpl::Commit()
838 {
839 if (conn_ == nullptr) {
840 LOGE("%s", INVALID_CONNECTION);
841 return DB_ERROR;
842 }
843
844 int errCode = conn_->Commit();
845 if (errCode != E_OK) {
846 LOGE("[KvStoreNbDelegate] Commit failed:%d", errCode);
847 }
848 return TransferDBErrno(errCode);
849 }
850
Rollback()851 DBStatus KvStoreNbDelegateImpl::Rollback()
852 {
853 if (conn_ == nullptr) {
854 LOGE("%s", INVALID_CONNECTION);
855 return DB_ERROR;
856 }
857
858 int errCode = conn_->RollBack();
859 if (errCode != E_OK) {
860 LOGE("[KvStoreNbDelegate] Rollback failed:%d", errCode);
861 }
862 return TransferDBErrno(errCode);
863 }
864
SetReleaseFlag(bool flag)865 void KvStoreNbDelegateImpl::SetReleaseFlag(bool flag)
866 {
867 releaseFlag_ = flag;
868 }
869
Close()870 DBStatus KvStoreNbDelegateImpl::Close()
871 {
872 if (conn_ != nullptr) {
873 int errCode = KvDBManager::ReleaseDatabaseConnection(conn_);
874 if (errCode == -E_BUSY) {
875 LOGI("[KvStoreNbDelegate] Busy for close");
876 return BUSY;
877 }
878 conn_ = nullptr;
879 }
880 return OK;
881 }
882
CheckIntegrity() const883 DBStatus KvStoreNbDelegateImpl::CheckIntegrity() const
884 {
885 if (conn_ == nullptr) {
886 LOGE("%s", INVALID_CONNECTION);
887 return DB_ERROR;
888 }
889
890 return TransferDBErrno(conn_->CheckIntegrity());
891 }
892
GetSecurityOption(SecurityOption & option) const893 DBStatus KvStoreNbDelegateImpl::GetSecurityOption(SecurityOption &option) const
894 {
895 if (conn_ == nullptr) {
896 LOGE("%s", INVALID_CONNECTION);
897 return DB_ERROR;
898 }
899 return TransferDBErrno(conn_->GetSecurityOption(option.securityLabel, option.securityFlag));
900 }
901
SetRemotePushFinishedNotify(const RemotePushFinishedNotifier & notifier)902 DBStatus KvStoreNbDelegateImpl::SetRemotePushFinishedNotify(const RemotePushFinishedNotifier ¬ifier)
903 {
904 if (conn_ == nullptr) {
905 LOGE("%s", INVALID_CONNECTION);
906 return DB_ERROR;
907 }
908
909 PragmaRemotePushNotify notify(notifier);
910 int errCode = conn_->Pragma(PRAGMA_REMOTE_PUSH_FINISHED_NOTIFY, reinterpret_cast<void *>(¬ify));
911 if (errCode != E_OK) {
912 LOGE("[KvStoreNbDelegate] Set remote push finished notify failed : %d", errCode);
913 }
914 return TransferDBErrno(errCode);
915 }
916
GetInner(const IOption & option,const Key & key,Value & value) const917 DBStatus KvStoreNbDelegateImpl::GetInner(const IOption &option, const Key &key, Value &value) const
918 {
919 if (conn_ == nullptr) {
920 LOGE("%s", INVALID_CONNECTION);
921 return DB_ERROR;
922 }
923
924 int errCode = conn_->Get(option, key, value);
925 if (errCode == E_OK) {
926 return OK;
927 }
928
929 if (errCode != -E_NOT_FOUND) {
930 LOGE("[KvStoreNbDelegate] [%s] Get the data failed:%d", storeId_.c_str(), errCode);
931 }
932 return TransferDBErrno(errCode);
933 }
934
GetEntriesInner(const IOption & option,const Key & keyPrefix,std::vector<Entry> & entries) const935 DBStatus KvStoreNbDelegateImpl::GetEntriesInner(const IOption &option,
936 const Key &keyPrefix, std::vector<Entry> &entries) const
937 {
938 if (conn_ == nullptr) {
939 LOGE("%s", INVALID_CONNECTION);
940 return DB_ERROR;
941 }
942
943 int errCode = conn_->GetEntries(option, keyPrefix, entries);
944 if (errCode == E_OK) {
945 return OK;
946 }
947 LOGE("[KvStoreNbDelegate] Get the batch data failed:%d", errCode);
948 return TransferDBErrno(errCode);
949 }
950
PutInner(const IOption & option,const Key & key,const Value & value)951 DBStatus KvStoreNbDelegateImpl::PutInner(const IOption &option, const Key &key, const Value &value)
952 {
953 if (conn_ == nullptr) {
954 LOGE("%s", INVALID_CONNECTION);
955 return DB_ERROR;
956 }
957
958 PerformanceAnalysis *performance = PerformanceAnalysis::GetInstance();
959 if (performance != nullptr) {
960 performance->StepTimeRecordStart(PT_TEST_RECORDS::RECORD_PUT_DATA);
961 }
962
963 int errCode = conn_->Put(option, key, value);
964 if (performance != nullptr) {
965 performance->StepTimeRecordEnd(PT_TEST_RECORDS::RECORD_PUT_DATA);
966 }
967
968 if (errCode == E_OK) {
969 return OK;
970 }
971 LOGE("[KvStoreNbDelegate] Put the data failed:%d", errCode);
972 return TransferDBErrno(errCode);
973 }
974
DeleteInner(const IOption & option,const Key & key)975 DBStatus KvStoreNbDelegateImpl::DeleteInner(const IOption &option, const Key &key)
976 {
977 if (conn_ == nullptr) {
978 LOGE("%s", INVALID_CONNECTION);
979 return DB_ERROR;
980 }
981
982 int errCode = conn_->Delete(option, key);
983 if (errCode == E_OK || errCode == -E_NOT_FOUND) {
984 return OK;
985 }
986
987 LOGE("[KvStoreNbDelegate] Delete the data failed:%d", errCode);
988 return TransferDBErrno(errCode);
989 }
990
OnSyncComplete(const std::map<std::string,int> & statuses,const std::function<void (const std::map<std::string,DBStatus> & devicesMap)> & onComplete) const991 void KvStoreNbDelegateImpl::OnSyncComplete(const std::map<std::string, int> &statuses,
992 const std::function<void(const std::map<std::string, DBStatus> &devicesMap)> &onComplete) const
993 {
994 std::map<std::string, DBStatus> result;
995 for (const auto &pair : statuses) {
996 DBStatus status = SyncOperation::DBStatusTrans(pair.second);
997 result.insert(std::pair<std::string, DBStatus>(pair.first, status));
998 }
999 if (onComplete) {
1000 onComplete(result);
1001 }
1002 }
1003
SetEqualIdentifier(const std::string & identifier,const std::vector<std::string> & targets)1004 DBStatus KvStoreNbDelegateImpl::SetEqualIdentifier(const std::string &identifier,
1005 const std::vector<std::string> &targets)
1006 {
1007 if (conn_ == nullptr) {
1008 LOGE("%s", INVALID_CONNECTION);
1009 return DB_ERROR;
1010 }
1011
1012 PragmaSetEqualIdentifier pragma(identifier, targets);
1013 int errCode = conn_->Pragma(PRAGMA_ADD_EQUAL_IDENTIFIER, reinterpret_cast<void *>(&pragma));
1014 if (errCode != E_OK) {
1015 LOGE("[KvStoreNbDelegate] Set store equal identifier failed : %d", errCode);
1016 }
1017
1018 return TransferDBErrno(errCode);
1019 }
1020
SetPushDataInterceptor(const PushDataInterceptor & interceptor)1021 DBStatus KvStoreNbDelegateImpl::SetPushDataInterceptor(const PushDataInterceptor &interceptor)
1022 {
1023 if (conn_ == nullptr) {
1024 LOGE("%s", INVALID_CONNECTION);
1025 return DB_ERROR;
1026 }
1027
1028 PushDataInterceptor notify = interceptor;
1029 int errCode = conn_->Pragma(PRAGMA_INTERCEPT_SYNC_DATA, static_cast<void *>(¬ify));
1030 if (errCode != E_OK) {
1031 LOGE("[KvStoreNbDelegate] Set data interceptor notify failed : %d", errCode);
1032 }
1033 return TransferDBErrno(errCode);
1034 }
1035
SubscribeRemoteQuery(const std::vector<std::string> & devices,const std::function<void (const std::map<std::string,DBStatus> & devicesMap)> & onComplete,const Query & query,bool wait)1036 DBStatus KvStoreNbDelegateImpl::SubscribeRemoteQuery(const std::vector<std::string> &devices,
1037 const std::function<void(const std::map<std::string, DBStatus> &devicesMap)> &onComplete,
1038 const Query &query, bool wait)
1039 {
1040 if (conn_ == nullptr) {
1041 LOGE("%s", INVALID_CONNECTION);
1042 return DB_ERROR;
1043 }
1044
1045 QuerySyncObject querySyncObj(query);
1046 if (querySyncObj.GetSortType() != SortType::NONE || querySyncObj.IsQueryByRange()) {
1047 LOGE("not support order by timestamp and query by range");
1048 return NOT_SUPPORT;
1049 }
1050 PragmaSync pragmaData(devices, SyncModeType::SUBSCRIBE_QUERY, querySyncObj,
1051 [this, onComplete](const std::map<std::string, int> &statuses) { OnSyncComplete(statuses, onComplete); }, wait);
1052 int errCode = conn_->Pragma(PRAGMA_SUBSCRIBE_QUERY, &pragmaData);
1053 if (errCode < E_OK) {
1054 LOGE("[KvStoreNbDelegate] Subscribe remote data with query failed:%d", errCode);
1055 return TransferDBErrno(errCode);
1056 }
1057 return OK;
1058 }
1059
UnSubscribeRemoteQuery(const std::vector<std::string> & devices,const std::function<void (const std::map<std::string,DBStatus> & devicesMap)> & onComplete,const Query & query,bool wait)1060 DBStatus KvStoreNbDelegateImpl::UnSubscribeRemoteQuery(const std::vector<std::string> &devices,
1061 const std::function<void(const std::map<std::string, DBStatus> &devicesMap)> &onComplete,
1062 const Query &query, bool wait)
1063 {
1064 if (conn_ == nullptr) {
1065 LOGE("%s", INVALID_CONNECTION);
1066 return DB_ERROR;
1067 }
1068
1069 QuerySyncObject querySyncObj(query);
1070 if (querySyncObj.GetSortType() != SortType::NONE || querySyncObj.IsQueryByRange()) {
1071 LOGE("not support order by timestamp and query by range");
1072 return NOT_SUPPORT;
1073 }
1074 PragmaSync pragmaData(devices, SyncModeType::UNSUBSCRIBE_QUERY, querySyncObj,
1075 [this, onComplete](const std::map<std::string, int> &statuses) { OnSyncComplete(statuses, onComplete); }, wait);
1076 int errCode = conn_->Pragma(PRAGMA_SUBSCRIBE_QUERY, &pragmaData);
1077 if (errCode < E_OK) {
1078 LOGE("[KvStoreNbDelegate] Unsubscribe remote data with query failed:%d", errCode);
1079 return TransferDBErrno(errCode);
1080 }
1081 return OK;
1082 }
1083
RemoveDeviceData()1084 DBStatus KvStoreNbDelegateImpl::RemoveDeviceData()
1085 {
1086 if (conn_ == nullptr) {
1087 LOGE("%s", INVALID_CONNECTION);
1088 return DB_ERROR;
1089 }
1090
1091 std::string device; // Empty device for remove all device data
1092 int errCode = conn_->Pragma(PRAGMA_RM_DEVICE_DATA,
1093 const_cast<void *>(static_cast<const void *>(&device)));
1094 if (errCode != E_OK) {
1095 LOGE("[KvStoreNbDelegate] Remove device data failed:%d", errCode);
1096 }
1097 return TransferDBErrno(errCode);
1098 }
1099
GetKeys(const Key & keyPrefix,std::vector<Key> & keys) const1100 DBStatus KvStoreNbDelegateImpl::GetKeys(const Key &keyPrefix, std::vector<Key> &keys) const
1101 {
1102 if (conn_ == nullptr) {
1103 LOGE("%s", INVALID_CONNECTION);
1104 return DB_ERROR;
1105 }
1106 IOption option;
1107 option.dataType = IOption::SYNC_DATA;
1108 int errCode = conn_->GetKeys(option, keyPrefix, keys);
1109 if (errCode == E_OK) {
1110 return OK;
1111 }
1112 LOGE("[KvStoreNbDelegate] Get the keys failed:%d", errCode);
1113 return TransferDBErrno(errCode);
1114 }
1115
GetSyncDataSize(const std::string & device) const1116 size_t KvStoreNbDelegateImpl::GetSyncDataSize(const std::string &device) const
1117 {
1118 if (conn_ == nullptr) {
1119 LOGE("%s", INVALID_CONNECTION);
1120 return 0;
1121 }
1122 if (device.empty()) {
1123 LOGE("device len is invalid");
1124 return 0;
1125 }
1126 size_t size = 0;
1127 int errCode = conn_->GetSyncDataSize(device, size);
1128 if (errCode != E_OK) {
1129 LOGE("[KvStoreNbDelegate] calculate sync data size failed : %d", errCode);
1130 return 0;
1131 }
1132 return size;
1133 }
1134
UpdateKey(const UpdateKeyCallback & callback)1135 DBStatus KvStoreNbDelegateImpl::UpdateKey(const UpdateKeyCallback &callback)
1136 {
1137 if (conn_ == nullptr) {
1138 LOGE("%s", INVALID_CONNECTION);
1139 return DB_ERROR;
1140 }
1141 if (callback == nullptr) {
1142 LOGE("[KvStoreNbDelegate] Invalid callback for operation");
1143 return INVALID_ARGS;
1144 }
1145 int errCode = conn_->UpdateKey(callback);
1146 if (errCode == E_OK) {
1147 LOGI("[KvStoreNbDelegate] update keys success");
1148 return OK;
1149 }
1150 LOGE("[KvStoreNbDelegate] update keys failed:%d", errCode);
1151 return TransferDBErrno(errCode);
1152 }
1153
GetWatermarkInfo(const std::string & device)1154 std::pair<DBStatus, WatermarkInfo> KvStoreNbDelegateImpl::GetWatermarkInfo(const std::string &device)
1155 {
1156 std::pair<DBStatus, WatermarkInfo> res;
1157 if (device.empty() || device.size() > DBConstant::MAX_DEV_LENGTH) {
1158 LOGE("[KvStoreNbDelegate] device invalid length %zu", device.size());
1159 res.first = INVALID_ARGS;
1160 return res;
1161 }
1162 if (conn_ == nullptr) {
1163 LOGE("%s", INVALID_CONNECTION);
1164 res.first = DB_ERROR;
1165 return res;
1166 }
1167 int errCode = conn_->GetWatermarkInfo(device, res.second);
1168 if (errCode == E_OK) {
1169 LOGI("[KvStoreNbDelegate] get watermark info success");
1170 } else {
1171 LOGE("[KvStoreNbDelegate] get watermark info failed:%d", errCode);
1172 }
1173 res.first = TransferDBErrno(errCode);
1174 return res;
1175 }
1176
Sync(const CloudSyncOption & option,const SyncProcessCallback & onProcess)1177 DBStatus KvStoreNbDelegateImpl::Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess)
1178 {
1179 if (conn_ == nullptr) {
1180 LOGE("%s", INVALID_CONNECTION);
1181 return DB_ERROR;
1182 }
1183 return TransferDBErrno(conn_->Sync(option, onProcess));
1184 }
1185
SetCloudDB(const std::map<std::string,std::shared_ptr<ICloudDb>> & cloudDBs)1186 DBStatus KvStoreNbDelegateImpl::SetCloudDB(const std::map<std::string, std::shared_ptr<ICloudDb>> &cloudDBs)
1187 {
1188 if (conn_ == nullptr) {
1189 LOGE("%s", INVALID_CONNECTION);
1190 return DB_ERROR;
1191 }
1192 if (cloudDBs.empty()) {
1193 LOGE("[KvStoreNbDelegate] no cloud db");
1194 return INVALID_ARGS;
1195 }
1196 return TransferDBErrno(conn_->SetCloudDB(cloudDBs));
1197 }
1198
SetCloudDbSchema(const std::map<std::string,DataBaseSchema> & schema)1199 DBStatus KvStoreNbDelegateImpl::SetCloudDbSchema(const std::map<std::string, DataBaseSchema> &schema)
1200 {
1201 if (conn_ == nullptr) {
1202 LOGE("%s", INVALID_CONNECTION);
1203 return DB_ERROR;
1204 }
1205 return TransferDBErrno(conn_->SetCloudDbSchema(schema));
1206 }
1207
RemoveDeviceData(const std::string & device,ClearMode mode)1208 DBStatus KvStoreNbDelegateImpl::RemoveDeviceData(const std::string &device, ClearMode mode)
1209 {
1210 if (conn_ == nullptr) {
1211 LOGE("%s", INVALID_CONNECTION);
1212 return DB_ERROR;
1213 }
1214 int errCode = conn_->RemoveDeviceData(device, mode);
1215 if (errCode != E_OK) {
1216 LOGE("[KvStoreNbDelegate] remove device data res %d", errCode);
1217 }
1218 LOGI("[KvStoreNbDelegate] remove device data res");
1219 return TransferDBErrno(errCode);
1220 }
1221
RemoveDeviceData(const std::string & device,const std::string & user,ClearMode mode)1222 DBStatus KvStoreNbDelegateImpl::RemoveDeviceData(const std::string &device, const std::string &user,
1223 ClearMode mode)
1224 {
1225 if (conn_ == nullptr) {
1226 LOGE("%s", INVALID_CONNECTION);
1227 return DB_ERROR;
1228 }
1229 if (user.empty() && mode != ClearMode::DEFAULT) {
1230 LOGE("[KvStoreNbDelegate] remove device data with empty user!");
1231 return INVALID_ARGS;
1232 }
1233 int errCode = conn_->RemoveDeviceData(device, user, mode);
1234 if (errCode != E_OK) {
1235 LOGE("[KvStoreNbDelegate] remove device data with user res %d", errCode);
1236 }
1237 LOGI("[KvStoreNbDelegate] remove device data with user res");
1238 return TransferDBErrno(errCode);
1239 }
1240
GetTaskCount()1241 int32_t KvStoreNbDelegateImpl::GetTaskCount()
1242 {
1243 if (conn_ == nullptr) {
1244 LOGE("%s", INVALID_CONNECTION);
1245 return DB_ERROR;
1246 }
1247 return conn_->GetTaskCount();
1248 }
1249
SetGenCloudVersionCallback(const GenerateCloudVersionCallback & callback)1250 void KvStoreNbDelegateImpl::SetGenCloudVersionCallback(const GenerateCloudVersionCallback &callback)
1251 {
1252 if (conn_ == nullptr || callback == nullptr) {
1253 LOGD("[KvStoreNbDelegate] Invalid connection or callback for operation");
1254 return;
1255 }
1256 conn_->SetGenCloudVersionCallback(callback);
1257 }
1258
GetCloudVersion(const std::string & device)1259 std::pair<DBStatus, std::map<std::string, std::string>> KvStoreNbDelegateImpl::GetCloudVersion(
1260 const std::string &device)
1261 {
1262 std::pair<DBStatus, std::map<std::string, std::string>> res;
1263 if (device.size() > DBConstant::MAX_DEV_LENGTH) {
1264 LOGE("[KvStoreNbDelegate] device invalid length %zu", device.size());
1265 res.first = INVALID_ARGS;
1266 return res;
1267 }
1268 if (conn_ == nullptr) {
1269 LOGE("%s", INVALID_CONNECTION);
1270 res.first = DB_ERROR;
1271 return res;
1272 }
1273 int errCode = conn_->GetCloudVersion(device, res.second);
1274 if (errCode == E_OK) {
1275 LOGI("[KvStoreNbDelegate] get cloudVersion success");
1276 } else {
1277 LOGE("[KvStoreNbDelegate] get cloudVersion failed:%d", errCode);
1278 }
1279 if (errCode == E_OK && res.second.empty()) {
1280 errCode = -E_NOT_FOUND;
1281 }
1282 res.first = TransferDBErrno(errCode);
1283 return res;
1284 }
1285
SetReceiveDataInterceptor(const DataInterceptor & interceptor)1286 DBStatus KvStoreNbDelegateImpl::SetReceiveDataInterceptor(const DataInterceptor &interceptor)
1287 {
1288 if (conn_ == nullptr) {
1289 LOGE("%s", INVALID_CONNECTION);
1290 return DB_ERROR;
1291 }
1292 int errCode = conn_->SetReceiveDataInterceptor(interceptor);
1293 if (errCode != E_OK) {
1294 LOGE("[KvStoreNbDelegate] Set receive data interceptor errCode:%d", errCode);
1295 }
1296 LOGI("[KvStoreNbDelegate] Set receive data interceptor");
1297 return TransferDBErrno(errCode);
1298 }
1299
SetCloudSyncConfig(const CloudSyncConfig & config)1300 DBStatus KvStoreNbDelegateImpl::SetCloudSyncConfig(const CloudSyncConfig &config)
1301 {
1302 if (conn_ == nullptr) {
1303 LOGE("%s", INVALID_CONNECTION);
1304 return DB_ERROR;
1305 }
1306 if (!DBCommon::CheckCloudSyncConfigValid(config)) {
1307 return INVALID_ARGS;
1308 }
1309 int errCode = conn_->SetCloudSyncConfig(config);
1310 if (errCode != E_OK) {
1311 LOGE("[KvStoreNbDelegate] Set cloud sync config errCode:%d", errCode);
1312 }
1313 LOGI("[KvStoreNbDelegate] Set cloud sync config");
1314 return TransferDBErrno(errCode);
1315 }
1316
GetDeviceEntries(const std::string & device,std::vector<Entry> & entries) const1317 DBStatus KvStoreNbDelegateImpl::GetDeviceEntries(const std::string &device, std::vector<Entry> &entries) const
1318 {
1319 if (conn_ == nullptr) {
1320 LOGE("%s", INVALID_CONNECTION);
1321 return DB_ERROR;
1322 }
1323 int errCode = conn_->GetEntries(device, entries);
1324 if (errCode == E_OK) {
1325 return OK;
1326 }
1327 LOGE("[KvStoreNbDelegate] Get the entries failed:%d", errCode);
1328 return TransferDBErrno(errCode);
1329 }
1330
GetDatabaseStatus() const1331 KvStoreNbDelegate::DatabaseStatus KvStoreNbDelegateImpl::GetDatabaseStatus() const
1332 {
1333 KvStoreNbDelegate::DatabaseStatus status;
1334 if (conn_ == nullptr) {
1335 LOGE("%s", INVALID_CONNECTION);
1336 return status;
1337 }
1338 status.isRebuild = conn_->IsRebuild();
1339 LOGI("[KvStoreNbDelegate] rebuild %d", static_cast<int>(status.isRebuild));
1340 return status;
1341 }
1342 } // namespace DistributedDB
1343