• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "sqlite_single_ver_natural_store_connection.h"
17 #include <algorithm>
18 
19 #include "db_common.h"
20 #include "db_constant.h"
21 #include "db_dfx_adapter.h"
22 #include "db_errno.h"
23 #include "kvdb_observer_handle.h"
24 #include "kvdb_pragma.h"
25 #include "log_print.h"
26 #include "single_ver_natural_store_connection.h"
27 #include "sqlite_single_ver_natural_store.h"
28 #include "sqlite_single_ver_result_set.h"
29 #include "store_types.h"
30 #include "time_helper.h"
31 
32 namespace DistributedDB {
33 namespace {
34     enum class LocalOperType {
35         LOCAL_OPR_NONE = 0,
36         LOCAL_OPR_DEL = 1,
37         LOCAL_OPR_PUT = 2
38     };
39     const uint32_t MAX_AUTO_LIFE_CYCLE = 1800000; // half an hour.
40     const uint32_t MIN_AUTO_LIFE_CYCLE = 5000; // 5s.
41 }
42 
SQLiteSingleVerNaturalStoreConnection(SQLiteSingleVerNaturalStore * kvDB)43 SQLiteSingleVerNaturalStoreConnection::SQLiteSingleVerNaturalStoreConnection(SQLiteSingleVerNaturalStore *kvDB)
44     : SingleVerNaturalStoreConnection(kvDB),
45       cacheMaxSizeForNewResultSet_(DEFAULT_RESULT_SET_CACHE_MAX_SIZE),
46       conflictType_(0),
47       transactionEntryLen_(0),
48       currentMaxTimestamp_(0),
49       committedData_(nullptr),
50       localCommittedData_(nullptr),
51       transactionExeFlag_(false),
52       conflictListener_(nullptr),
53       writeHandle_(nullptr)
54 {}
55 
~SQLiteSingleVerNaturalStoreConnection()56 SQLiteSingleVerNaturalStoreConnection::~SQLiteSingleVerNaturalStoreConnection()
57 {
58     if (conflictListener_ != nullptr) {
59         conflictListener_->Drop(true);
60         conflictListener_ = nullptr;
61     }
62 }
63 
IsFileAccessControlled() const64 inline bool SQLiteSingleVerNaturalStoreConnection::IsFileAccessControlled() const
65 {
66     return RuntimeContext::GetInstance()->IsAccessControlled() &&
67         kvDB_->GetMyProperties().GetSecLabel() > SecurityLabel::S2;
68 }
69 
CheckReadDataControlled() const70 int SQLiteSingleVerNaturalStoreConnection::CheckReadDataControlled() const
71 {
72     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
73     if (naturalStore == nullptr) {
74         LOGE("[SingleVerConnection] natural store is nullptr in CheckReadDataControlled.");
75         return E_OK;
76     }
77     return naturalStore->CheckReadDataControlled();
78 }
79 
Get(const IOption & option,const Key & key,Value & value) const80 int SQLiteSingleVerNaturalStoreConnection::Get(const IOption &option, const Key &key, Value &value) const
81 {
82     if (key.size() > DBConstant::MAX_KEY_SIZE || key.empty()) {
83         return -E_INVALID_ARGS;
84     }
85 
86     SingleVerDataType dataType;
87     if (option.dataType == IOption::LOCAL_DATA) {
88         dataType = SingleVerDataType::LOCAL_TYPE_SQLITE;
89     } else if (option.dataType == IOption::SYNC_DATA) {
90         dataType = SingleVerDataType::SYNC_TYPE;
91     } else {
92         return -E_NOT_SUPPORT;
93     }
94 
95     int errCode = CheckReadDataControlled();
96     if (errCode != E_OK) {
97         LOGE("[Get] Existed cache database can not read data, errCode = [%d]!", errCode);
98         return errCode;
99     }
100 
101     DBDfxAdapter::StartTracing();
102     bool isInWhitelist = IsInWhitelist();
103     // need to check if the transaction started
104     if (!isInWhitelist || (isInWhitelist && transactionExeFlag_.load())) {
105         {
106             std::lock_guard<std::mutex> lock(transactionMutex_);
107             if (writeHandle_ != nullptr) {
108                 LOGD("Transaction started already.");
109                 Timestamp recordTimestamp;
110                 errCode = writeHandle_->GetKvData(dataType, key, value, recordTimestamp);
111                 DBDfxAdapter::FinishTracing();
112                 return errCode;
113             }
114         }
115     }
116 
117     SQLiteSingleVerStorageExecutor *handle = GetExecutor(false, errCode);
118     if (handle == nullptr) {
119         DBDfxAdapter::FinishTracing();
120         return errCode;
121     }
122 
123     Timestamp timestamp;
124     errCode = handle->GetKvData(dataType, key, value, timestamp);
125     ReleaseExecutor(handle);
126     DBDfxAdapter::FinishTracing();
127     return errCode;
128 }
129 
Clear(const IOption & option)130 int SQLiteSingleVerNaturalStoreConnection::Clear(const IOption &option)
131 {
132     return -E_NOT_SUPPORT;
133 }
134 
GetEntries(const IOption & option,const Key & keyPrefix,std::vector<Entry> & entries) const135 int SQLiteSingleVerNaturalStoreConnection::GetEntries(const IOption &option, const Key &keyPrefix,
136     std::vector<Entry> &entries) const
137 {
138     return GetEntriesInner(true, option, keyPrefix, entries);
139 }
140 
GetEntries(const IOption & option,const Query & query,std::vector<Entry> & entries) const141 int SQLiteSingleVerNaturalStoreConnection::GetEntries(const IOption &option, const Query &query,
142     std::vector<Entry> &entries) const
143 {
144     if (option.dataType != IOption::SYNC_DATA) {
145         return -E_NOT_SUPPORT;
146     }
147     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
148     if (naturalStore == nullptr) {
149         return -E_INVALID_DB;
150     }
151     int errCode = CheckReadDataControlled();
152     if (errCode != E_OK) {
153         LOGE("[GetEntries] Existed cache database can not read data, errCode = [%d]!", errCode);
154         return errCode;
155     }
156     QueryObject queryObj(query);
157     if (((queryObj.GetSortType() != SortType::NONE) && !queryObj.IsQueryOnlyByKey())
158         || queryObj.IsQueryByRange()) {
159         LOGE("[GetEntries][query] timestamp sort only support prefixKey and not support Range search");
160         return -E_NOT_SUPPORT;
161     }
162 
163     // In readOnly mode, forbidden all schema related query
164     if (CheckWritePermission() == E_OK) {
165         const SchemaObject &schemaObjRef = naturalStore->GetSchemaObjectConstRef();
166         queryObj.SetSchema(schemaObjRef);
167     }
168     DBDfxAdapter::StartTracing();
169     {
170         std::lock_guard<std::mutex> lock(transactionMutex_);
171         if (writeHandle_ != nullptr) {
172             LOGD("Transaction started already.");
173             errCode = writeHandle_->GetEntries(queryObj, entries);
174             DBDfxAdapter::FinishTracing();
175             return errCode;
176         }
177     }
178 
179     SQLiteSingleVerStorageExecutor *handle = GetExecutor(false, errCode);
180     if (handle == nullptr) {
181         DBDfxAdapter::FinishTracing();
182         return errCode;
183     }
184 
185     errCode = handle->GetEntries(queryObj, entries);
186     ReleaseExecutor(handle);
187     DBDfxAdapter::FinishTracing();
188     return errCode;
189 }
190 
GetCount(const IOption & option,const Query & query,int & count) const191 int SQLiteSingleVerNaturalStoreConnection::GetCount(const IOption &option, const Query &query, int &count) const
192 {
193     if (option.dataType != IOption::SYNC_DATA) {
194         return -E_NOT_SUPPORT;
195     }
196     int errCode = CheckReadDataControlled();
197     if (errCode != E_OK) {
198         LOGE("[GetCount] Existed cache database can not read data, errCode = [%d]!", errCode);
199         return errCode;
200     }
201 
202     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
203     if (naturalStore == nullptr) {
204         return -E_INVALID_DB;
205     }
206     QueryObject queryObj(query);
207     if (((queryObj.GetSortType() != SortType::NONE) && !queryObj.IsQueryOnlyByKey())
208         || queryObj.IsQueryByRange()) {
209         LOGE("[GetCount] get count query invalid");
210         return -E_NOT_SUPPORT;
211     }
212     // In readOnly mode, forbidden all schema related query
213     if (CheckWritePermission() == E_OK) {
214         const SchemaObject &schemaObjRef = naturalStore->GetSchemaObjectConstRef();
215         queryObj.SetSchema(schemaObjRef);
216     }
217     DBDfxAdapter::StartTracing();
218     {
219         std::lock_guard<std::mutex> lock(transactionMutex_);
220         if (writeHandle_ != nullptr) {
221             LOGD("Transaction started already.");
222             errCode = writeHandle_->GetCount(queryObj, count);
223             DBDfxAdapter::FinishTracing();
224             return errCode;
225         }
226     }
227 
228     SQLiteSingleVerStorageExecutor *handle = GetExecutor(false, errCode);
229     if (handle == nullptr) {
230         DBDfxAdapter::FinishTracing();
231         return errCode;
232     }
233     errCode = handle->GetCount(queryObj, count);
234     ReleaseExecutor(handle);
235     DBDfxAdapter::FinishTracing();
236     return errCode;
237 }
238 
PutBatch(const IOption & option,const std::vector<Entry> & entries)239 int SQLiteSingleVerNaturalStoreConnection::PutBatch(const IOption &option, const std::vector<Entry> &entries)
240 {
241     LOGD("[PutBatch] entries size is : %zu, dataType : %d", entries.size(), option.dataType);
242     if (option.dataType == IOption::LOCAL_DATA) {
243         int retCode = CheckLocalEntriesValid(entries);
244         if (retCode != E_OK) {
245             return retCode;
246         }
247         return PutBatchInner(option, entries);
248     }
249 
250     return SingleVerNaturalStoreConnection::PutBatch(option, entries);
251 }
252 
DeleteBatch(const IOption & option,const std::vector<Key> & keys)253 int SQLiteSingleVerNaturalStoreConnection::DeleteBatch(const IOption &option, const std::vector<Key> &keys)
254 {
255     if (option.dataType == IOption::LOCAL_DATA) {
256         LOGD("[DeleteBatch] keys size is : %zu, dataType : %d", keys.size(), option.dataType);
257         int retCode = CheckLocalKeysValid(keys);
258         if (retCode != E_OK) {
259             return retCode;
260         }
261         return DeleteBatchInner(option, keys);
262     }
263 
264     return SingleVerNaturalStoreConnection::DeleteBatch(option, keys);
265 }
266 
GetSnapshot(IKvDBSnapshot * & snapshot) const267 int SQLiteSingleVerNaturalStoreConnection::GetSnapshot(IKvDBSnapshot *&snapshot) const
268 {
269     return -E_NOT_SUPPORT;
270 }
271 
ReleaseSnapshot(IKvDBSnapshot * & snapshot)272 void SQLiteSingleVerNaturalStoreConnection::ReleaseSnapshot(IKvDBSnapshot *&snapshot)
273 {}
274 
StartTransaction()275 int SQLiteSingleVerNaturalStoreConnection::StartTransaction()
276 {
277     std::lock_guard<std::mutex> lock(transactionMutex_);
278     if (writeHandle_ != nullptr) {
279         LOGD("Transaction started already.");
280         return -E_TRANSACT_STATE;
281     }
282 
283     int errCode = StartTransactionInner();
284     if (errCode == E_OK) {
285         transactionExeFlag_.store(true);
286     }
287     return errCode;
288 }
289 
Commit()290 int SQLiteSingleVerNaturalStoreConnection::Commit()
291 {
292     std::lock_guard<std::mutex> lock(transactionMutex_);
293     if (writeHandle_ == nullptr) {
294         LOGE("single version database is null or the transaction has not been started");
295         return -E_INVALID_DB;
296     }
297 
298     int errCode = CommitInner();
299     if (errCode == E_OK) {
300         transactionExeFlag_.store(false);
301     }
302     return errCode;
303 }
304 
RollBack()305 int SQLiteSingleVerNaturalStoreConnection::RollBack()
306 {
307     std::lock_guard<std::mutex> lock(transactionMutex_);
308     if (writeHandle_ == nullptr) {
309         LOGE("Invalid handle for rollback or the transaction has not been started.");
310         return -E_INVALID_DB;
311     }
312 
313     int errCode = RollbackInner();
314     if (errCode == E_OK) {
315         transactionExeFlag_.store(false);
316     }
317     return errCode;
318 }
319 
IsTransactionStarted() const320 bool SQLiteSingleVerNaturalStoreConnection::IsTransactionStarted() const
321 {
322     return transactionExeFlag_.load();
323 }
324 
Pragma(int cmd,void * parameter)325 int SQLiteSingleVerNaturalStoreConnection::Pragma(int cmd, void *parameter)
326 {
327     int errCode = E_OK;
328     switch (cmd) {
329         case PRAGMA_RM_DEVICE_DATA: {
330             return RemoveDeviceDataByCmd(parameter);
331         }
332         case PRAGMA_GET_IDENTIFIER_OF_DEVICE: {
333             if (parameter == nullptr) {
334                 return -E_INVALID_ARGS;
335             }
336             return CalcHashDevID(*(static_cast<PragmaDeviceIdentifier *>(parameter)));
337         }
338         case PRAGMA_GET_DEVICE_IDENTIFIER_OF_ENTRY:
339             return GetDeviceIdentifier(static_cast<PragmaEntryDeviceIdentifier *>(parameter));
340         case PRAGMA_PUBLISH_LOCAL:
341             return PragmaPublish(parameter);
342         case PRAGMA_UNPUBLISH_SYNC:
343             errCode = PragmaUnpublish(parameter);
344             break;
345         case PRAGMA_SET_AUTO_LIFE_CYCLE:
346             return PragmaSetAutoLifeCycle(static_cast<uint32_t *>(parameter));
347         case PRAGMA_RESULT_SET_CACHE_MODE:
348             return PragmaResultSetCacheMode(parameter);
349         case PRAGMA_RESULT_SET_CACHE_MAX_SIZE:
350             return PragmaResultSetCacheMaxSize(parameter);
351         case PRAGMA_TRIGGER_TO_MIGRATE_DATA:
352             return PragmaTriggerToMigrateData(*static_cast<SecurityOption *>(parameter));
353         case PRAGMA_SET_MAX_LOG_LIMIT:
354             return PragmaSetMaxLogSize(static_cast<uint64_t *>(parameter));
355         case PRAGMA_EXEC_CHECKPOINT:
356             return ForceCheckPoint();
357         default:
358             // Call of others.
359             errCode = PragmaNext(cmd, parameter);
360             break;
361     }
362 
363     return errCode;
364 }
365 
PragmaNext(int cmd,void * parameter)366 int SQLiteSingleVerNaturalStoreConnection::PragmaNext(int cmd, void *parameter)
367 {
368     int errCode = E_OK;
369     switch (cmd) {
370         case PRAGMA_SET_MAX_VALUE_SIZE:
371             return SetMaxValueSize(*static_cast<uint32_t *>(parameter));
372         default:
373             // Call Pragma() of super class.
374             errCode = SyncAbleKvDBConnection::Pragma(cmd, parameter);
375             break;
376     }
377 
378     return errCode;
379 }
380 
TranslateObserverModeToEventTypes(unsigned mode,std::list<int> & eventTypes) const381 int SQLiteSingleVerNaturalStoreConnection::TranslateObserverModeToEventTypes(unsigned mode,
382     std::list<int> &eventTypes) const
383 {
384     int errCode = E_OK;
385     switch (mode) {
386         case static_cast<unsigned>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_PUT_EVENT):
387             eventTypes.push_back(static_cast<int>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_PUT_EVENT));
388             break;
389         case static_cast<unsigned>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_SYNC_EVENT):
390             eventTypes.push_back(static_cast<int>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_SYNC_EVENT));
391             break;
392         case (static_cast<unsigned>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_PUT_EVENT) |
393             static_cast<unsigned>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_SYNC_EVENT)):
394             eventTypes.push_back(static_cast<int>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_PUT_EVENT));
395             eventTypes.push_back(static_cast<int>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_SYNC_EVENT));
396             break;
397         case static_cast<unsigned>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_LOCAL_PUT_EVENT):
398             eventTypes.push_back(
399                 static_cast<int>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_LOCAL_PUT_EVENT));
400             break;
401         default:
402             errCode = -E_NOT_SUPPORT;
403             break;
404     }
405     return errCode;
406 }
407 
ClearConflictNotifierCount()408 void SQLiteSingleVerNaturalStoreConnection::ClearConflictNotifierCount()
409 {
410     uint32_t conflictType = static_cast<unsigned>(conflictType_);
411     if ((conflictType & static_cast<unsigned>(SQLiteGeneralNSConflictType::SQLITE_GENERAL_NS_FOREIGN_KEY_ONLY)) != 0) {
412         (void)kvDB_->UnregisterFunction(RegisterFuncType::CONFLICT_SINGLE_VERSION_NS_FOREIGN_KEY_ONLY);
413     }
414     if ((conflictType & static_cast<unsigned>(SQLiteGeneralNSConflictType::SQLITE_GENERAL_NS_FOREIGN_KEY_ORIG)) != 0) {
415         (void)kvDB_->UnregisterFunction(RegisterFuncType::CONFLICT_SINGLE_VERSION_NS_FOREIGN_KEY_ORIG);
416     }
417     if ((conflictType & static_cast<unsigned>(SQLiteGeneralNSConflictType::SQLITE_GENERAL_NS_NATIVE_ALL)) != 0) {
418         (void)kvDB_->UnregisterFunction(RegisterFuncType::CONFLICT_SINGLE_VERSION_NS_NATIVE_ALL);
419     }
420     return;
421 }
422 
ResetConflictNotifierCount(int target)423 void SQLiteSingleVerNaturalStoreConnection::ResetConflictNotifierCount(int target)
424 {
425     // Clear the old conflict type function.
426     ClearConflictNotifierCount();
427 
428     LOGD("Conflict type:%d to %d", conflictType_, target);
429     // Add the new conflict type function.
430     AddConflictNotifierCount(target);
431     conflictType_ = target;
432 }
433 
AddConflictNotifierCount(int target)434 void SQLiteSingleVerNaturalStoreConnection::AddConflictNotifierCount(int target)
435 {
436     LOGD("Conflict type:%u vs %u", conflictType_, target);
437     // Add the new conflict type function.
438     uint32_t targetTemp = static_cast<uint32_t>(target);
439     if ((targetTemp & static_cast<uint32_t>(SQLiteGeneralNSConflictType::SQLITE_GENERAL_NS_FOREIGN_KEY_ONLY)) != 0) {
440         (void)kvDB_->RegisterFunction(RegisterFuncType::CONFLICT_SINGLE_VERSION_NS_FOREIGN_KEY_ONLY);
441     }
442     if ((targetTemp & static_cast<uint32_t>(SQLiteGeneralNSConflictType::SQLITE_GENERAL_NS_FOREIGN_KEY_ORIG)) != 0) {
443         (void)kvDB_->RegisterFunction(RegisterFuncType::CONFLICT_SINGLE_VERSION_NS_FOREIGN_KEY_ORIG);
444     }
445     if ((targetTemp & static_cast<uint32_t>(SQLiteGeneralNSConflictType::SQLITE_GENERAL_NS_NATIVE_ALL)) != 0) {
446         (void)kvDB_->RegisterFunction(RegisterFuncType::CONFLICT_SINGLE_VERSION_NS_NATIVE_ALL);
447     }
448 }
449 
SetConflictNotifier(int conflictType,const KvDBConflictAction & action)450 int SQLiteSingleVerNaturalStoreConnection::SetConflictNotifier(int conflictType,
451     const KvDBConflictAction &action)
452 {
453     std::lock_guard<std::mutex> lock(conflictMutex_);
454     if (!action && conflictListener_ == nullptr) {
455         return -E_INVALID_ARGS;
456     }
457     if (kvDB_ == nullptr) {
458         return -E_INVALID_DB;
459     }
460 
461     // prevent the rekey operation.
462     if (isExclusive_.load()) {
463         return -E_BUSY;
464     }
465 
466     int targetType = 0;
467     NotificationChain::Listener *listener = nullptr;
468     if (action) {
469         int errCode = E_OK;
470         Key key;
471         listener = RegisterSpecialListener(
472             static_cast<int>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_CONFLICT_EVENT), key, action, true,
473             errCode);
474         if (listener == nullptr) {
475             LOGE("Register Conflict listener failed:'%d'.", errCode);
476             return errCode;
477         }
478         targetType = conflictType;
479     }
480 
481     ResetConflictNotifierCount(targetType);
482     // drop the old listener.
483     if (conflictListener_ != nullptr) {
484         conflictListener_->Drop(true);
485     }
486     conflictListener_ = listener;
487     return E_OK;
488 }
489 
Rekey(const CipherPassword & passwd)490 int SQLiteSingleVerNaturalStoreConnection::Rekey(const CipherPassword &passwd)
491 {
492     if (IsFileAccessControlled()) {
493         LOGE("Forbid Rekey when screen locked and security label [%d]!", kvDB_->GetMyProperties().GetSecLabel());
494         return -E_NOT_SUPPORT;
495     }
496     std::lock_guard<std::mutex> lock(rekeyMutex_);
497     int errCode = CheckMonoStatus(OperatePerm::REKEY_MONOPOLIZE_PERM);
498     if (errCode != E_OK) {
499         return errCode;
500     }
501     LOGI("Begin rekey operation");
502     errCode = kvDB_->Rekey(passwd);
503     GenericKvDBConnection::ResetExclusiveStatus();
504     kvDB_->ReEnableConnection(OperatePerm::REKEY_MONOPOLIZE_PERM);
505     EnableManualSync();
506     LOGI("End rekey operation errCode = [%d]", errCode);
507     return errCode;
508 }
509 
Export(const std::string & filePath,const CipherPassword & passwd)510 int SQLiteSingleVerNaturalStoreConnection::Export(const std::string &filePath, const CipherPassword &passwd)
511 {
512     if (kvDB_ == nullptr) {
513         return -E_INVALID_DB;
514     }
515 
516     if (IsFileAccessControlled()) {
517         LOGE("Forbid Export when screen locked and security label [%d] file lock state [%d]",
518             kvDB_->GetMyProperties().GetSecLabel(), RuntimeContext::GetInstance()->IsAccessControlled());
519         return -E_NOT_SUPPORT;
520     } // Avoid abnormal branch handling without affecting the business
521     return kvDB_->Export(filePath, passwd);
522 }
523 
Import(const std::string & filePath,const CipherPassword & passwd,bool isNeedIntegrityCheck)524 int SQLiteSingleVerNaturalStoreConnection::Import(const std::string &filePath, const CipherPassword &passwd,
525     [[gnu::unused]] bool isNeedIntegrityCheck)
526 {
527     if (IsFileAccessControlled()) {
528         LOGE("Forbid Import when screen locked and security label [%d]!", kvDB_->GetMyProperties().GetSecLabel());
529         return -E_NOT_SUPPORT;
530     }
531 
532     std::lock_guard<std::mutex> lock(importMutex_);
533     int errCode = CheckMonoStatus(OperatePerm::IMPORT_MONOPOLIZE_PERM);
534     if (errCode != E_OK) {
535         return errCode;
536     }
537     errCode = kvDB_->Import(filePath, passwd);
538     GenericKvDBConnection::ResetExclusiveStatus();
539     kvDB_->ReEnableConnection(OperatePerm::IMPORT_MONOPOLIZE_PERM);
540     EnableManualSync();
541     if (errCode == E_OK) {
542         kvDB_->ResetSyncStatus();
543     }
544     return errCode;
545 }
546 
GetResultSet(const IOption & option,const Key & keyPrefix,IKvDBResultSet * & resultSet) const547 int SQLiteSingleVerNaturalStoreConnection::GetResultSet(const IOption &option, const Key &keyPrefix,
548     IKvDBResultSet *&resultSet) const
549 {
550     // need to check if the transaction started
551     if (transactionExeFlag_.load()) {
552         LOGD("Transaction started already.");
553         return -E_BUSY;
554     }
555 
556     // maximum of result set size is 4
557     std::lock_guard<std::mutex> lock(kvDbResultSetsMutex_);
558     if (kvDbResultSets_.size() >= MAX_RESULT_SET_SIZE) {
559         LOGE("Over max result set size");
560         return -E_MAX_LIMITS;
561     }
562 
563     int errCode = CheckReadDataControlled();
564     if (errCode != E_OK) {
565         LOGE("[GetResultSet][keyPrefix] Existed cache database can not read data, errCode = [%d]!", errCode);
566         return errCode;
567     }
568 
569     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
570     if (naturalStore == nullptr) {
571         return -E_INVALID_DB;
572     }
573     bool isMemDb = naturalStore->GetMyProperties().GetBoolProp(KvDBProperties::MEMORY_MODE, false);
574     resultSet = new (std::nothrow) SQLiteSingleVerResultSet(naturalStore, keyPrefix,
575         SQLiteSingleVerResultSet::Option{cacheModeForNewResultSet_.load(), cacheMaxSizeForNewResultSet_.load()});
576     if (resultSet == nullptr) {
577         LOGE("Create single version result set failed.");
578         return -E_OUT_OF_MEMORY;
579     }
580     errCode = resultSet->Open(isMemDb);
581     if (errCode != E_OK) {
582         delete resultSet;
583         resultSet = nullptr;
584         LOGE("Open result set failed.");
585         return errCode;
586     }
587     kvDbResultSets_.insert(resultSet);
588     return E_OK;
589 }
590 
GetResultSet(const IOption & option,const Query & query,IKvDBResultSet * & resultSet) const591 int SQLiteSingleVerNaturalStoreConnection::GetResultSet(const IOption &option, const Query &query,
592     IKvDBResultSet *&resultSet) const
593 {
594     // need to check if the transaction started
595     if (transactionExeFlag_.load()) {
596         LOGD("Transaction started already.");
597         return -E_BUSY;
598     }
599 
600     // maximum of result set size is 4
601     std::lock_guard<std::mutex> lock(kvDbResultSetsMutex_);
602     if (kvDbResultSets_.size() >= MAX_RESULT_SET_SIZE) {
603         LOGE("Over max result set size");
604         return -E_MAX_LIMITS;
605     }
606 
607     int errCode = CheckReadDataControlled();
608     if (errCode != E_OK) {
609         LOGE("[GetResultSet][query] Existed cache database can not read data, errCode = [%d]!", errCode);
610         return errCode;
611     }
612 
613     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>(); // Guarantee not nullptr
614     QueryObject queryObj(query);
615     // In readOnly mode, forbidden all schema related query
616     if (CheckWritePermission() == E_OK) {
617         const SchemaObject &schemaObjRef = naturalStore->GetSchemaObjectConstRef();
618         queryObj.SetSchema(schemaObjRef);
619     }
620     if (((queryObj.GetSortType() != SortType::NONE) && !queryObj.IsQueryOnlyByKey())
621         || queryObj.IsQueryByRange()) {
622         LOGE("[GetResultSet][query] timestamp sort only support prefixKey and not support range search");
623         return -E_NOT_SUPPORT;
624     }
625     bool isMemDb = naturalStore->GetMyProperties().GetBoolProp(KvDBProperties::MEMORY_MODE, false);
626     resultSet = new (std::nothrow) SQLiteSingleVerResultSet(naturalStore, queryObj,
627         SQLiteSingleVerResultSet::Option{cacheModeForNewResultSet_.load(), cacheMaxSizeForNewResultSet_.load()});
628     if (resultSet == nullptr) {
629         LOGE("Create single version result set failed.");
630         return -E_OUT_OF_MEMORY;
631     }
632     errCode = resultSet->Open(isMemDb);
633     if (errCode != E_OK) {
634         delete resultSet;
635         resultSet = nullptr;
636         LOGE("Open result set failed.");
637         return errCode;
638     }
639     kvDbResultSets_.insert(resultSet);
640     return E_OK;
641 }
642 
ReleaseResultSet(IKvDBResultSet * & resultSet)643 void SQLiteSingleVerNaturalStoreConnection::ReleaseResultSet(IKvDBResultSet *&resultSet)
644 {
645     std::lock_guard<std::mutex> lock(kvDbResultSetsMutex_);
646     if (resultSet == nullptr) {
647         return;
648     }
649     resultSet->Close();
650     kvDbResultSets_.erase(resultSet);
651     delete resultSet;
652     resultSet = nullptr;
653     return;
654 }
655 
RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier & notifier)656 int SQLiteSingleVerNaturalStoreConnection::RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier &notifier)
657 {
658     if (kvDB_ == nullptr) {
659         return -E_INVALID_DB;
660     }
661     return static_cast<SQLiteSingleVerNaturalStore *>(kvDB_)->RegisterLifeCycleCallback(notifier);
662 }
663 
PreClose(bool isCloseImmediately)664 int SQLiteSingleVerNaturalStoreConnection::PreClose(bool isCloseImmediately)
665 {
666     // check if result set closed
667     {
668         std::lock_guard<std::mutex> kvDbResultLock(kvDbResultSetsMutex_);
669         if (kvDbResultSets_.size() > 0) {
670             LOGE("The connection have [%zu] active result set, can not close.", kvDbResultSets_.size());
671             return -E_BUSY;
672         }
673     }
674 
675     // check if sync task finish
676     if (!isCloseImmediately) {
677         int ret = kvDB_->PreClose();
678         if (ret != E_OK) {
679             return ret;
680         }
681     }
682 
683     // check if transaction closed
684     {
685         std::lock_guard<std::mutex> transactionLock(transactionMutex_);
686         if (writeHandle_ != nullptr) {
687             LOGW("Transaction started, need to rollback before close.");
688             int errCode = RollbackInner();
689             if (errCode != E_OK) {
690                 LOGE("Rollback transaction failed, %d.", errCode);
691             }
692             ReleaseExecutor(writeHandle_);
693         }
694     }
695 
696     // Clear the conflict type function.
697     {
698         std::lock_guard<std::mutex> lock(conflictMutex_);
699         ClearConflictNotifierCount();
700         conflictType_ = 0;
701     }
702     return E_OK;
703 }
704 
CheckIntegrity() const705 int SQLiteSingleVerNaturalStoreConnection::CheckIntegrity() const
706 {
707     int errCode = E_OK;
708     SQLiteSingleVerStorageExecutor *handle = GetExecutor(true, errCode);
709     if (handle == nullptr) {
710         LOGW("Failed to get the executor for the integrity check.");
711         return errCode;
712     }
713 
714     errCode = handle->CheckIntegrity();
715     ReleaseExecutor(handle);
716     return errCode;
717 }
718 
PragmaSetMaxLogSize(uint64_t * limit)719 int SQLiteSingleVerNaturalStoreConnection::PragmaSetMaxLogSize(uint64_t *limit)
720 {
721     if (limit == nullptr) {
722         return -E_INVALID_ARGS;
723     }
724     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
725     if (naturalStore == nullptr) {
726         LOGE("[SingleVerConnection] db is nullptr for max log limit set.");
727         return -E_INVALID_DB;
728     }
729     if (*limit > DBConstant::MAX_LOG_SIZE_HIGH || *limit < DBConstant::MAX_LOG_SIZE_LOW) {
730         return -E_INVALID_ARGS;
731     }
732     return naturalStore->SetMaxLogSize(*limit);
733 }
734 
SetMaxValueSize(uint32_t maxValueSize)735 int SQLiteSingleVerNaturalStoreConnection::SetMaxValueSize(uint32_t maxValueSize)
736 {
737     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
738     if (naturalStore == nullptr) {
739         LOGE("[SingleVerConnection] db is nullptr for max value size set.");
740         return -E_INVALID_DB;
741     }
742     if (maxValueSize > DBConstant::MAX_SET_VALUE_SIZE || maxValueSize < DBConstant::MAX_VALUE_SIZE) {
743         return -E_INVALID_ARGS;
744     }
745     return naturalStore->SetMaxValueSize(maxValueSize);
746 }
747 
ForceCheckPoint() const748 int SQLiteSingleVerNaturalStoreConnection::ForceCheckPoint() const
749 {
750     int errCode = E_OK;
751     SQLiteSingleVerStorageExecutor *handle = GetExecutor(true, errCode);
752     if (handle == nullptr) {
753         LOGW("Failed to get the executor for the checkpoint.");
754         return errCode;
755     }
756 
757     errCode = handle->ForceCheckPoint();
758     ReleaseExecutor(handle);
759     return errCode;
760 }
761 
CheckMonoStatus(OperatePerm perm)762 int SQLiteSingleVerNaturalStoreConnection::CheckMonoStatus(OperatePerm perm)
763 {
764     // 1. Get the connection number
765     if (kvDB_ == nullptr) {
766         return -E_INVALID_DB;
767     }
768     int errCode = DisableManualSync();
769     if (errCode != E_OK) {
770         LOGE("In manual sync");
771         return -E_BUSY;
772     }
773 
774     // 2. check the result set number
775     {
776         std::lock_guard<std::mutex> kvDbResultLock(kvDbResultSetsMutex_);
777         if (kvDbResultSets_.size() > 0) {
778             LOGE("Active result set exist.");
779             EnableManualSync();
780             return -E_BUSY;
781         }
782     }
783     // 1. Get the connection number, and get the right to do the rekey operation.
784     errCode = kvDB_->TryToDisableConnection(perm);
785     if (errCode != E_OK) {
786         // If precheck failed, it means that there are more than one connection.
787         // No need reset the condition for the scene.
788         LOGE("More than one connection");
789         EnableManualSync();
790         return errCode;
791     }
792     // 2. Check the observer list.
793     errCode = GenericKvDBConnection::PreCheckExclusiveStatus();
794     if (errCode != E_OK) {
795         kvDB_->ReEnableConnection(perm);
796         LOGE("Observer prevents.");
797         EnableManualSync();
798         return errCode;
799     }
800 
801     // 3. Check the conflict notifier.
802     {
803         std::lock_guard<std::mutex> conflictLock(conflictMutex_);
804         if (conflictListener_ != nullptr) {
805             errCode = -E_BUSY;
806             GenericKvDBConnection::ResetExclusiveStatus();
807             kvDB_->ReEnableConnection(perm);
808             LOGE("Conflict notifier prevents");
809             EnableManualSync();
810             return errCode;
811         }
812     }
813     return E_OK;
814 }
815 
GetDeviceIdentifier(PragmaEntryDeviceIdentifier * identifier)816 int SQLiteSingleVerNaturalStoreConnection::GetDeviceIdentifier(PragmaEntryDeviceIdentifier *identifier)
817 {
818     if (identifier == nullptr) {
819         return -E_INVALID_ARGS;
820     }
821 
822     if (identifier->key.empty() || identifier->key.size() > DBConstant::MAX_VALUE_SIZE) {
823         return -E_INVALID_ARGS;
824     }
825 
826     int errCode = E_OK;
827     SQLiteSingleVerStorageExecutor *handle = GetExecutor(false, errCode);
828     if (handle == nullptr) {
829         return errCode;
830     }
831 
832     errCode = handle->GetDeviceIdentifier(identifier);
833     ReleaseExecutor(handle);
834     return errCode;
835 }
836 
PutBatchInner(const IOption & option,const std::vector<Entry> & entries)837 int SQLiteSingleVerNaturalStoreConnection::PutBatchInner(const IOption &option, const std::vector<Entry> &entries)
838 {
839     DBDfxAdapter::StartTracing();
840     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
841     if (naturalStore == nullptr) {
842         return -E_INVALID_DB;
843     }
844     // check whether sync started to get SystemTime
845     naturalStore->WakeUpSyncer();
846     std::lock_guard<std::mutex> lock(transactionMutex_);
847     bool isAuto = false;
848     int errCode = E_OK;
849     if (writeHandle_ == nullptr) {
850         isAuto = true;
851         errCode = StartTransactionInner(TransactType::IMMEDIATE);
852         if (errCode != E_OK) {
853             DBDfxAdapter::FinishTracing();
854             return errCode;
855         }
856     }
857     uint32_t len = 0;
858     if (!CheckAndGetEntryLen(entries, (DBConstant::MAX_TRANSACTION_KEY_VALUE_LENS - transactionEntryLen_), len)) {
859         DBDfxAdapter::FinishTracing();
860         return -E_MAX_LIMITS;
861     }
862 
863     if (option.dataType == IOption::SYNC_DATA) {
864         errCode = SaveSyncEntries(entries);
865     } else {
866         errCode = SaveLocalEntries(entries);
867     }
868     if (errCode == E_OK) {
869         transactionEntryLen_ += len;
870     }
871 
872     if (isAuto) {
873         if (errCode == E_OK) {
874             errCode = CommitInner();
875         } else {
876             int innerCode = RollbackInner();
877             errCode = (innerCode != E_OK) ? innerCode : errCode;
878         }
879     }
880     DBDfxAdapter::FinishTracing();
881     return errCode;
882 }
883 
DeleteBatchInner(const IOption & option,const std::vector<Key> & keys)884 int SQLiteSingleVerNaturalStoreConnection::DeleteBatchInner(const IOption &option, const std::vector<Key> &keys)
885 {
886     DBDfxAdapter::StartTracing();
887     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
888     if (naturalStore == nullptr) {
889         return -E_INVALID_DB;
890     }
891     // check whether sync started to get SystemTime
892     naturalStore->WakeUpSyncer();
893     std::lock_guard<std::mutex> lock(transactionMutex_);
894     bool isAuto = false;
895     int errCode = E_OK;
896 
897     if (writeHandle_ == nullptr) {
898         isAuto = true;
899         errCode = StartTransactionInner(TransactType::IMMEDIATE);
900         if (errCode != E_OK) {
901             DBDfxAdapter::FinishTracing();
902             return errCode;
903         }
904     }
905     uint32_t len = 0;
906     if (!CheckAndGetKeyLen(keys, (DBConstant::MAX_TRANSACTION_KEY_VALUE_LENS - transactionEntryLen_), len)) {
907         DBDfxAdapter::FinishTracing();
908         return -E_MAX_LIMITS;
909     }
910 
911     if (option.dataType == IOption::SYNC_DATA) {
912         errCode = DeleteSyncEntries(keys);
913     } else {
914         errCode = DeleteLocalEntries(keys);
915     }
916     if (errCode == E_OK) {
917         transactionEntryLen_ += len;
918     }
919 
920     if (isAuto) {
921         if (errCode == E_OK) {
922             errCode = CommitInner();
923         } else {
924             int innerCode = RollbackInner();
925             errCode = (innerCode != E_OK) ? innerCode : errCode;
926         }
927     }
928     DBDfxAdapter::FinishTracing();
929     return errCode;
930 }
931 
SaveSyncEntries(const std::vector<Entry> & entries)932 int SQLiteSingleVerNaturalStoreConnection::SaveSyncEntries(const std::vector<Entry> &entries)
933 {
934     int errCode = E_OK;
935     for (const auto &entry : entries) {
936         errCode = SaveEntry(entry, false);
937         if (errCode != E_OK) {
938             break;
939         }
940     }
941     return errCode;
942 }
943 
SaveLocalEntries(const std::vector<Entry> & entries)944 int SQLiteSingleVerNaturalStoreConnection::SaveLocalEntries(const std::vector<Entry> &entries)
945 {
946     int errCode = E_OK;
947     for (const auto &entry : entries) {
948         errCode = SaveLocalEntry(entry, false);
949         if (errCode != E_OK) {
950             break;
951         }
952     }
953     return errCode;
954 }
955 
DeleteSyncEntries(const std::vector<Key> & keys)956 int SQLiteSingleVerNaturalStoreConnection::DeleteSyncEntries(const std::vector<Key> &keys)
957 {
958     int errCode = E_OK;
959     for (const auto &key : keys) {
960         Entry entry;
961         DBCommon::CalcValueHash(key, entry.key);
962         errCode = SaveEntry(entry, true);
963         if ((errCode != E_OK) && (errCode != -E_NOT_FOUND)) {
964             LOGE("[DeleteSyncEntries] Delete data err:%d", errCode);
965             break;
966         }
967     }
968     return (errCode == -E_NOT_FOUND) ? E_OK : errCode;
969 }
970 
DeleteLocalEntries(const std::vector<Key> & keys)971 int SQLiteSingleVerNaturalStoreConnection::DeleteLocalEntries(const std::vector<Key> &keys)
972 {
973     int errCode = E_OK;
974     for (const auto &key : keys) {
975         Entry entry = {key, Value()};
976         errCode = SaveLocalEntry(entry, true);
977         if ((errCode != E_OK) && (errCode != -E_NOT_FOUND)) {
978             LOGE("[DeleteLocalEntries] Delete data err:%d", errCode);
979             break;
980         }
981     }
982     return (errCode == -E_NOT_FOUND) ? E_OK : errCode;
983 }
984 
985 // This function currently only be called in local procedure to change sync_data table, do not use in sync procedure.
986 // It will check and amend value when need if it is a schema database. return error if some value disagree with the
987 // schema. But in sync procedure, we just neglect the value that disagree with schema.
SaveEntry(const Entry & entry,bool isDelete,Timestamp timestamp)988 int SQLiteSingleVerNaturalStoreConnection::SaveEntry(const Entry &entry, bool isDelete, Timestamp timestamp)
989 {
990     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
991     if (naturalStore == nullptr) {
992         return -E_INVALID_DB;
993     }
994 
995     DataItem dataItem;
996     dataItem.key = entry.key;
997     dataItem.value = entry.value;
998     dataItem.flag = DataItem::LOCAL_FLAG;
999     if (isDelete) {
1000         dataItem.flag |= DataItem::DELETE_FLAG;
1001     } else {
1002         int errCode = CheckAmendValueContentForLocalProcedure(dataItem.value, dataItem.value);
1003         if (errCode != E_OK) {
1004             LOGE("[SqlSinCon][SaveEntry] CheckAmendValue fail, errCode=%d.", errCode);
1005             return errCode;
1006         }
1007     }
1008     RecordTimeIntoDataItem(timestamp, dataItem, *naturalStore);
1009     if (IsExtendedCacheDBMode()) {
1010         uint64_t recordVersion = naturalStore->GetCacheRecordVersion();
1011         return SaveEntryInCacheMode(dataItem, recordVersion);
1012     } else {
1013         return SaveEntryNormally(dataItem);
1014     }
1015 }
1016 
SaveLocalEntry(const Entry & entry,bool isDelete)1017 int SQLiteSingleVerNaturalStoreConnection::SaveLocalEntry(const Entry &entry, bool isDelete)
1018 {
1019     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1020     if (naturalStore == nullptr) {
1021         return -E_INVALID_DB;
1022     }
1023 
1024     LocalDataItem dataItem;
1025     dataItem.key = entry.key;
1026     dataItem.value = entry.value;
1027     (void)DBCommon::CalcValueHash(entry.key, dataItem.hashKey);
1028     if (isDelete) {
1029         dataItem.flag = DataItem::DELETE_FLAG;
1030     }
1031     dataItem.timestamp = naturalStore->GetCurrentTimestamp();
1032     LOGD("Timestamp is %" PRIu64, dataItem.timestamp);
1033 
1034     if (IsCacheDBMode()) {
1035         return SaveLocalItemInCacheMode(dataItem);
1036     } else {
1037         return SaveLocalItem(dataItem);
1038     }
1039 }
1040 
SaveLocalItem(const LocalDataItem & dataItem) const1041 int SQLiteSingleVerNaturalStoreConnection::SaveLocalItem(const LocalDataItem &dataItem) const
1042 {
1043     int errCode = E_OK;
1044     if ((dataItem.flag & DataItem::DELETE_FLAG) == 0) {
1045         errCode = writeHandle_->PutKvData(SingleVerDataType::LOCAL_TYPE_SQLITE, dataItem.key, dataItem.value,
1046             dataItem.timestamp, localCommittedData_);
1047     } else {
1048         Value value;
1049         Timestamp localTimestamp = 0;
1050         errCode = writeHandle_->DeleteLocalKvData(dataItem.key, localCommittedData_, value, localTimestamp);
1051     }
1052     return errCode;
1053 }
1054 
SaveLocalItemInCacheMode(const LocalDataItem & dataItem) const1055 int SQLiteSingleVerNaturalStoreConnection::SaveLocalItemInCacheMode(const LocalDataItem &dataItem) const
1056 {
1057     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1058     if (naturalStore == nullptr) {
1059         return -E_INVALID_DB;
1060     }
1061 
1062     int errCode = writeHandle_->PutLocalDataToCacheDB(dataItem);
1063     if (errCode != E_OK) {
1064         LOGE("[PutLocalEntries] Put local data to cacheDB err:%d", errCode);
1065     }
1066     return errCode;
1067 }
1068 
SaveEntryNormally(DataItem & dataItem)1069 int SQLiteSingleVerNaturalStoreConnection::SaveEntryNormally(DataItem &dataItem)
1070 {
1071     int errCode = writeHandle_->PrepareForSavingData(SingleVerDataType::SYNC_TYPE);
1072     if (errCode != E_OK) {
1073         LOGE("Prepare the saving sync data failed:%d", errCode);
1074         return errCode;
1075     }
1076 
1077     Timestamp maxTimestamp = 0;
1078     DeviceInfo deviceInfo = {true, ""};
1079     errCode = writeHandle_->SaveSyncDataItem(dataItem, deviceInfo, maxTimestamp, committedData_);
1080     if (errCode == E_OK) {
1081         if (maxTimestamp > currentMaxTimestamp_) {
1082             currentMaxTimestamp_ = maxTimestamp;
1083         }
1084     } else if (errCode != -E_NOT_FOUND) {
1085         LOGE("[Normal] Save err:%d", errCode);
1086     }
1087     return errCode;
1088 }
1089 
SaveEntryInCacheMode(DataItem & dataItem,uint64_t recordVersion)1090 int SQLiteSingleVerNaturalStoreConnection::SaveEntryInCacheMode(DataItem &dataItem, uint64_t recordVersion)
1091 {
1092     int errCode = writeHandle_->PrepareForSavingCacheData(SingleVerDataType::SYNC_TYPE);
1093     if (errCode != E_OK) {
1094         LOGE("Prepare the saving sync data failed:%d", errCode);
1095         return errCode;
1096     }
1097 
1098     Timestamp maxTimestamp = 0;
1099     DeviceInfo deviceInfo = {true, ""};
1100     QueryObject query(Query::Select());
1101     errCode = writeHandle_->SaveSyncDataItemInCacheMode(dataItem, deviceInfo, maxTimestamp, recordVersion, query);
1102     if (errCode == E_OK) {
1103         if (maxTimestamp > currentMaxTimestamp_) {
1104             currentMaxTimestamp_ = maxTimestamp;
1105         }
1106     } else {
1107         LOGE("Save entry failed, err:%d", errCode);
1108     }
1109     return errCode;
1110 }
1111 
CheckDataStatus(const Key & key,const Value & value,bool isDelete) const1112 int SQLiteSingleVerNaturalStoreConnection::CheckDataStatus(const Key &key, const Value &value, bool isDelete) const
1113 {
1114     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1115     if (naturalStore == nullptr) {
1116         return -E_INVALID_DB;
1117     }
1118 
1119     return naturalStore->CheckDataStatus(key, value, isDelete);
1120 }
1121 
CheckWritePermission() const1122 int SQLiteSingleVerNaturalStoreConnection::CheckWritePermission() const
1123 {
1124     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1125     if (naturalStore == nullptr) {
1126         return -E_INVALID_DB;
1127     }
1128 
1129     if (!naturalStore->CheckWritePermission()) {
1130         return -E_READ_ONLY;
1131     }
1132     return E_OK;
1133 }
1134 
CheckSyncEntriesValid(const std::vector<Entry> & entries) const1135 int SQLiteSingleVerNaturalStoreConnection::CheckSyncEntriesValid(const std::vector<Entry> &entries) const
1136 {
1137     uint32_t len = 0;
1138     if (!CheckAndGetEntryLen(entries, DBConstant::MAX_TRANSACTION_KEY_VALUE_LENS, len)) {
1139         return -E_INVALID_ARGS;
1140     }
1141 
1142     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1143     if (naturalStore == nullptr) {
1144         return -E_INVALID_DB;
1145     }
1146 
1147     if (!naturalStore->CheckWritePermission()) {
1148         return -E_READ_ONLY;
1149     }
1150 
1151     for (const auto &entry : entries) {
1152         int errCode = naturalStore->CheckDataStatus(entry.key, entry.value, false);
1153         if (errCode != E_OK) {
1154             return errCode;
1155         }
1156     }
1157     return E_OK;
1158 }
1159 
CheckSyncKeysValid(const std::vector<Key> & keys) const1160 int SQLiteSingleVerNaturalStoreConnection::CheckSyncKeysValid(const std::vector<Key> &keys) const
1161 {
1162     uint32_t len = 0;
1163     if (!CheckAndGetKeyLen(keys, DBConstant::MAX_TRANSACTION_KEY_VALUE_LENS, len)) {
1164         return -E_INVALID_ARGS;
1165     }
1166     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1167     if (naturalStore == nullptr) {
1168         return -E_INVALID_DB;
1169     }
1170 
1171     if (!naturalStore->CheckWritePermission()) {
1172         return -E_READ_ONLY;
1173     }
1174 
1175     for (const auto &key : keys) {
1176         int errCode = naturalStore->CheckDataStatus(key, {}, true);
1177         if (errCode != E_OK) {
1178             return errCode;
1179         }
1180     }
1181     return E_OK;
1182 }
1183 
CheckLocalEntriesValid(const std::vector<Entry> & entries) const1184 int SQLiteSingleVerNaturalStoreConnection::CheckLocalEntriesValid(const std::vector<Entry> &entries) const
1185 {
1186     uint32_t len = 0;
1187     if (!CheckAndGetEntryLen(entries, DBConstant::MAX_TRANSACTION_KEY_VALUE_LENS, len)) {
1188         return -E_INVALID_ARGS;
1189     }
1190 
1191     GenericKvDB *naturalStore = GetDB<GenericKvDB>();
1192     if (naturalStore == nullptr) {
1193         return -E_INVALID_DB;
1194     }
1195 
1196     if (!naturalStore->GenericKvDB::CheckWritePermission()) {
1197         return -E_READ_ONLY;
1198     }
1199 
1200     for (const auto &entry : entries) {
1201         int errCode = naturalStore->GenericKvDB::CheckDataStatus(entry.key, entry.value, false);
1202         if (errCode != E_OK) {
1203             return errCode;
1204         }
1205     }
1206     return E_OK;
1207 }
1208 
CheckLocalKeysValid(const std::vector<Key> & keys) const1209 int SQLiteSingleVerNaturalStoreConnection::CheckLocalKeysValid(const std::vector<Key> &keys) const
1210 {
1211     uint32_t len = 0;
1212     if (!CheckAndGetKeyLen(keys, DBConstant::MAX_TRANSACTION_KEY_VALUE_LENS, len)) {
1213         return -E_INVALID_ARGS;
1214     }
1215     GenericKvDB *naturalStore = GetDB<GenericKvDB>();
1216     if (naturalStore == nullptr) {
1217         return -E_INVALID_DB;
1218     }
1219 
1220     if (!naturalStore->GenericKvDB::CheckWritePermission()) {
1221         return -E_READ_ONLY;
1222     }
1223 
1224     for (const auto &key : keys) {
1225         int errCode = naturalStore->GenericKvDB::CheckDataStatus(key, {}, true);
1226         if (errCode != E_OK) {
1227             return errCode;
1228         }
1229     }
1230     return E_OK;
1231 }
1232 
CommitAndReleaseNotifyData(SingleVerNaturalStoreCommitNotifyData * & committedData,bool isNeedCommit,int eventType)1233 void SQLiteSingleVerNaturalStoreConnection::CommitAndReleaseNotifyData(
1234     SingleVerNaturalStoreCommitNotifyData *&committedData, bool isNeedCommit, int eventType)
1235 {
1236     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1237     if ((naturalStore != nullptr) && (committedData != nullptr)) {
1238         if (isNeedCommit) {
1239             if (!committedData->IsChangedDataEmpty()) {
1240                 naturalStore->CommitNotify(eventType, committedData);
1241             }
1242             if (!committedData->IsConflictedDataEmpty()) {
1243                 naturalStore->CommitNotify(
1244                     static_cast<int>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_CONFLICT_EVENT),
1245                     committedData);
1246             }
1247         }
1248     }
1249     ReleaseCommitData(committedData);
1250 }
1251 
StartTransactionInner(TransactType transType)1252 int SQLiteSingleVerNaturalStoreConnection::StartTransactionInner(TransactType transType)
1253 {
1254     if (IsExtendedCacheDBMode()) {
1255         return StartTransactionInCacheMode(transType);
1256     } else {
1257         return StartTransactionNormally(transType);
1258     }
1259 }
1260 
StartTransactionInCacheMode(TransactType transType)1261 int SQLiteSingleVerNaturalStoreConnection::StartTransactionInCacheMode(TransactType transType)
1262 {
1263     int errCode = E_OK;
1264     SQLiteSingleVerStorageExecutor *handle = GetExecutor(true, errCode);
1265     if (handle == nullptr) {
1266         return errCode;
1267     }
1268     if (CheckLogOverLimit(handle)) {
1269         LOGW("Over the log limit");
1270         ReleaseExecutor(handle);
1271         return -E_LOG_OVER_LIMITS;
1272     }
1273     errCode = handle->StartTransaction(transType);
1274     if (errCode != E_OK) {
1275         ReleaseExecutor(handle);
1276         return errCode;
1277     }
1278 
1279     writeHandle_ = handle;
1280     transactionEntryLen_ = 0;
1281     return E_OK;
1282 }
1283 
StartTransactionNormally(TransactType transType)1284 int SQLiteSingleVerNaturalStoreConnection::StartTransactionNormally(TransactType transType)
1285 {
1286     int errCode = E_OK;
1287     SQLiteSingleVerStorageExecutor *handle = GetExecutor(true, errCode);
1288     if (handle == nullptr) {
1289         return errCode;
1290     }
1291 
1292     errCode = kvDB_->TryToDisableConnection(OperatePerm::NORMAL_WRITE);
1293     if (errCode != E_OK) { // on operate rekey or import
1294         ReleaseExecutor(handle);
1295         LOGE("Start transaction failed, %d perm not normal", errCode);
1296         return errCode;
1297     }
1298 
1299     if (CheckLogOverLimit(handle)) {
1300         LOGW("Over the log limit");
1301         ReleaseExecutor(handle);
1302         return -E_LOG_OVER_LIMITS;
1303     }
1304 
1305     if (committedData_ == nullptr) {
1306         committedData_ = new (std::nothrow) SingleVerNaturalStoreCommitNotifyData;
1307         if (committedData_ == nullptr) {
1308             ReleaseExecutor(handle);
1309             return -E_OUT_OF_MEMORY;
1310         }
1311         InitConflictNotifiedFlag();
1312     }
1313     if (localCommittedData_ == nullptr) {
1314         localCommittedData_ = new (std::nothrow) SingleVerNaturalStoreCommitNotifyData;
1315         if (localCommittedData_ == nullptr) {
1316             ReleaseExecutor(handle);
1317             ReleaseCommitData(committedData_);
1318             return -E_OUT_OF_MEMORY;
1319         }
1320     }
1321     errCode = handle->StartTransaction(transType);
1322     if (errCode != E_OK) {
1323         ReleaseExecutor(handle);
1324         ReleaseCommitData(committedData_);
1325         ReleaseCommitData(localCommittedData_);
1326         return errCode;
1327     }
1328 
1329     writeHandle_ = handle;
1330     transactionEntryLen_ = 0;
1331     return E_OK;
1332 }
1333 
InitConflictNotifiedFlag()1334 void SQLiteSingleVerNaturalStoreConnection::InitConflictNotifiedFlag()
1335 {
1336     unsigned int conflictFlag = 0;
1337     if (kvDB_->GetRegisterFunctionCount(RegisterFuncType::CONFLICT_SINGLE_VERSION_NS_FOREIGN_KEY_ONLY) != 0) {
1338         conflictFlag |= static_cast<unsigned>(SQLiteGeneralNSConflictType::SQLITE_GENERAL_NS_FOREIGN_KEY_ONLY);
1339     }
1340     if (kvDB_->GetRegisterFunctionCount(RegisterFuncType::CONFLICT_SINGLE_VERSION_NS_FOREIGN_KEY_ORIG) != 0) {
1341         conflictFlag |= static_cast<unsigned>(SQLiteGeneralNSConflictType::SQLITE_GENERAL_NS_FOREIGN_KEY_ORIG);
1342     }
1343     if (kvDB_->GetRegisterFunctionCount(RegisterFuncType::CONFLICT_SINGLE_VERSION_NS_NATIVE_ALL) != 0) {
1344         conflictFlag |= static_cast<unsigned>(SQLiteGeneralNSConflictType::SQLITE_GENERAL_NS_NATIVE_ALL);
1345     }
1346 
1347     committedData_->SetConflictedNotifiedFlag(static_cast<int>(conflictFlag));
1348 }
1349 
CommitInner()1350 int SQLiteSingleVerNaturalStoreConnection::CommitInner()
1351 {
1352     bool isCacheOrMigrating = IsExtendedCacheDBMode();
1353 
1354     int errCode = writeHandle_->Commit();
1355     ReleaseExecutor(writeHandle_);
1356     transactionEntryLen_ = 0;
1357 
1358     if (!isCacheOrMigrating) {
1359         CommitAndReleaseNotifyData(committedData_, true,
1360             static_cast<int>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_PUT_EVENT));
1361         CommitAndReleaseNotifyData(localCommittedData_, true,
1362             static_cast<int>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_LOCAL_PUT_EVENT));
1363     }
1364     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1365     if (naturalStore == nullptr) {
1366         return -E_INVALID_DB;
1367     }
1368 
1369     if (isCacheOrMigrating) {
1370         naturalStore->IncreaseCacheRecordVersion();
1371     }
1372     return errCode;
1373 }
1374 
RollbackInner()1375 int SQLiteSingleVerNaturalStoreConnection::RollbackInner()
1376 {
1377     int errCode = writeHandle_->Rollback();
1378     transactionEntryLen_ = 0;
1379     currentMaxTimestamp_ = 0;
1380     if (!IsExtendedCacheDBMode()) {
1381         ReleaseCommitData(committedData_);
1382         ReleaseCommitData(localCommittedData_);
1383     }
1384     ReleaseExecutor(writeHandle_);
1385     return errCode;
1386 }
1387 
GetExecutor(bool isWrite,int & errCode) const1388 SQLiteSingleVerStorageExecutor *SQLiteSingleVerNaturalStoreConnection::GetExecutor(bool isWrite, int &errCode) const
1389 {
1390     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1391     if (naturalStore == nullptr) {
1392         errCode = -E_NOT_INIT;
1393         LOGE("[SingleVerConnection] the store is null");
1394         return nullptr;
1395     }
1396     errCode = naturalStore->TryHandle();
1397     if (errCode != E_OK) {
1398         return nullptr;
1399     }
1400     return naturalStore->GetHandle(isWrite, errCode);
1401 }
1402 
IsCacheDBMode() const1403 bool SQLiteSingleVerNaturalStoreConnection::IsCacheDBMode() const
1404 {
1405     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1406     if (naturalStore == nullptr) {
1407         LOGE("[SingleVerConnection] the store is null");
1408         return false;
1409     }
1410     return naturalStore->IsCacheDBMode();
1411 }
1412 
IsExtendedCacheDBMode() const1413 bool SQLiteSingleVerNaturalStoreConnection::IsExtendedCacheDBMode() const
1414 {
1415     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1416     if (naturalStore == nullptr) {
1417         LOGE("[SingleVerConnection] the store is null");
1418         return false;
1419     }
1420     return naturalStore->IsExtendedCacheDBMode();
1421 }
1422 
ReleaseExecutor(SQLiteSingleVerStorageExecutor * & executor) const1423 void SQLiteSingleVerNaturalStoreConnection::ReleaseExecutor(SQLiteSingleVerStorageExecutor *&executor) const
1424 {
1425     kvDB_->ReEnableConnection(OperatePerm::NORMAL_WRITE);
1426     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1427     if (naturalStore != nullptr) {
1428         naturalStore->ReleaseHandle(executor);
1429     }
1430 }
1431 
PublishLocal(const Key & key,bool deleteLocal,bool updateTimestamp,const KvStoreNbPublishAction & onConflict)1432 int SQLiteSingleVerNaturalStoreConnection::PublishLocal(const Key &key, bool deleteLocal, bool updateTimestamp,
1433     const KvStoreNbPublishAction &onConflict)
1434 {
1435     int errCode = CheckWritePermission();
1436     if (errCode != E_OK) {
1437         return errCode;
1438     }
1439 
1440     bool isNeedCallback = (onConflict != nullptr);
1441     SingleVerRecord localRecord;
1442     localRecord.key = key;
1443     SingleVerRecord syncRecord;
1444     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1445     if (naturalStore == nullptr) {
1446         return -E_INVALID_DB;
1447     }
1448     // check whether sync started to get SystemTime
1449     naturalStore->WakeUpSyncer();
1450     {
1451         if (IsTransactionStarted()) {
1452             return -E_NOT_SUPPORT;
1453         }
1454         std::lock_guard<std::mutex> lock(transactionMutex_);
1455         errCode = StartTransactionInner(TransactType::IMMEDIATE);
1456         if (errCode != E_OK) {
1457             return errCode;
1458         }
1459 
1460         SingleVerNaturalStoreCommitNotifyData *localCommittedData = nullptr;
1461         if (deleteLocal) {
1462             localCommittedData = new (std::nothrow) SingleVerNaturalStoreCommitNotifyData;
1463             if (localCommittedData == nullptr) {
1464                 errCode = -E_OUT_OF_MEMORY;
1465             }
1466         }
1467         if (errCode == E_OK) {
1468             errCode = PublishInner(localCommittedData, updateTimestamp, localRecord, syncRecord, isNeedCallback);
1469         }
1470 
1471         if (errCode != E_OK || isNeedCallback) {
1472             int innerCode = RollbackInner();
1473             errCode = (innerCode != E_OK) ? innerCode : errCode;
1474         } else {
1475             errCode = CommitInner();
1476             if (errCode == E_OK) {
1477                 CommitAndReleaseNotifyData(localCommittedData, true,
1478                     static_cast<int>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_LOCAL_PUT_EVENT));
1479             }
1480         }
1481         ReleaseCommitData(localCommittedData);
1482     }
1483 
1484     // need to release the handle lock before callback invoked
1485     if (isNeedCallback) {
1486         return PublishLocalCallback(updateTimestamp, localRecord, syncRecord, onConflict);
1487     }
1488 
1489     return errCode;
1490 }
1491 
PublishLocalCallback(bool updateTimestamp,const SingleVerRecord & localRecord,const SingleVerRecord & syncRecord,const KvStoreNbPublishAction & onConflict)1492 int SQLiteSingleVerNaturalStoreConnection::PublishLocalCallback(bool updateTimestamp,
1493     const SingleVerRecord &localRecord, const SingleVerRecord &syncRecord, const KvStoreNbPublishAction &onConflict)
1494 {
1495     bool isLocalLastest = updateTimestamp ? true : (localRecord.timestamp > syncRecord.writeTimestamp);
1496     if ((syncRecord.flag & DataItem::DELETE_FLAG) == DataItem::DELETE_FLAG) {
1497         onConflict({localRecord.key, localRecord.value}, nullptr, isLocalLastest);
1498     } else {
1499         Entry syncEntry = {syncRecord.key, syncRecord.value};
1500         onConflict({localRecord.key, localRecord.value}, &syncEntry, isLocalLastest);
1501     }
1502     return E_OK;
1503 }
1504 
PublishInner(SingleVerNaturalStoreCommitNotifyData * committedData,bool updateTimestamp,SingleVerRecord & localRecord,SingleVerRecord & syncRecord,bool & isNeedCallback)1505 int SQLiteSingleVerNaturalStoreConnection::PublishInner(SingleVerNaturalStoreCommitNotifyData *committedData,
1506     bool updateTimestamp, SingleVerRecord &localRecord, SingleVerRecord &syncRecord, bool &isNeedCallback)
1507 {
1508     Key hashKey;
1509     int errCode = DBCommon::CalcValueHash(localRecord.key, hashKey);
1510     if (errCode != E_OK) {
1511         return errCode;
1512     }
1513 
1514     if (committedData != nullptr) {
1515         errCode = writeHandle_->DeleteLocalKvData(localRecord.key, committedData, localRecord.value,
1516             localRecord.timestamp);
1517         if (errCode != E_OK) {
1518             LOGE("Delete local kv data err:%d", errCode);
1519             return errCode;
1520         }
1521     } else {
1522         if (!writeHandle_->CheckIfKeyExisted(localRecord.key, true, localRecord.value, localRecord.timestamp)) {
1523             LOGE("Record not found.");
1524             return -E_NOT_FOUND;
1525         }
1526     }
1527 
1528     // begin to insert entry to sync table, no more than 4M
1529     errCode = CheckDataStatus(localRecord.key, localRecord.value, false);
1530     if (errCode != E_OK) {
1531         return errCode;
1532     }
1533 
1534     errCode = writeHandle_->GetKvDataByHashKey(hashKey, syncRecord);
1535     if (errCode == E_OK) { // has conflict record
1536         if (isNeedCallback) {
1537             return errCode;
1538         }
1539         // fix conflict with LAST_WIN policy
1540         if (updateTimestamp) { // local win
1541             errCode = SaveEntry({localRecord.key, localRecord.value}, false);
1542         } else {
1543             if (localRecord.timestamp <= syncRecord.writeTimestamp) { // sync win
1544                 errCode = -E_STALE;
1545             } else {
1546                 errCode = SaveEntry({localRecord.key, localRecord.value}, false, localRecord.timestamp);
1547             }
1548         }
1549     } else {
1550         isNeedCallback = false;
1551         if (errCode == -E_NOT_FOUND) {
1552             errCode = SaveEntry({localRecord.key, localRecord.value}, false, localRecord.timestamp);
1553         }
1554     }
1555     return errCode;
1556 }
1557 
UnpublishToLocal(const Key & key,bool deletePublic,bool updateTimestamp)1558 int SQLiteSingleVerNaturalStoreConnection::UnpublishToLocal(const Key &key, bool deletePublic, bool updateTimestamp)
1559 {
1560     int errCode = CheckWritePermission();
1561     if (errCode != E_OK) {
1562         return errCode;
1563     }
1564 
1565     if (IsTransactionStarted()) {
1566         return -E_NOT_SUPPORT;
1567     }
1568     // check whether sync started to get SystemTime
1569     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1570     if (naturalStore != nullptr) {
1571         naturalStore->WakeUpSyncer();
1572     }
1573 
1574     std::lock_guard<std::mutex> lock(transactionMutex_);
1575 
1576     errCode = StartTransactionInner(TransactType::IMMEDIATE);
1577     if (errCode != E_OK) {
1578         return errCode;
1579     }
1580 
1581     Key hashKey;
1582     int innerErrCode = E_OK;
1583     SingleVerRecord syncRecord;
1584     std::pair<Key, Key> keyPair = { key, hashKey };
1585     SingleVerNaturalStoreCommitNotifyData *localCommittedData = nullptr;
1586     errCode = UnpublishInner(keyPair, localCommittedData, syncRecord, updateTimestamp, innerErrCode);
1587     if (errCode != E_OK) {
1588         goto END;
1589     }
1590 
1591     if (deletePublic && (syncRecord.flag & DataItem::DELETE_FLAG) != DataItem::DELETE_FLAG) {
1592         errCode = SaveEntry({keyPair.second, {}}, true);
1593     }
1594 
1595 END:
1596     // finalize
1597     if (errCode != E_OK) {
1598         int rollbackRet = RollbackInner();
1599         errCode = (rollbackRet != E_OK) ? rollbackRet : errCode;
1600     } else {
1601         errCode = CommitInner();
1602         if (errCode == E_OK) {
1603             CommitAndReleaseNotifyData(localCommittedData, true,
1604                 static_cast<int>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_LOCAL_PUT_EVENT));
1605         }
1606     }
1607     ReleaseCommitData(localCommittedData);
1608 
1609     return (errCode == E_OK) ? innerErrCode : errCode;
1610 }
1611 
UnpublishInner(std::pair<Key,Key> & keyPair,SingleVerNaturalStoreCommitNotifyData * & committedData,SingleVerRecord & syncRecord,bool updateTimestamp,int & innerErrCode)1612 int SQLiteSingleVerNaturalStoreConnection::UnpublishInner(std::pair<Key, Key> &keyPair,
1613     SingleVerNaturalStoreCommitNotifyData *&committedData, SingleVerRecord &syncRecord, bool updateTimestamp,
1614     int &innerErrCode)
1615 {
1616     int errCode = E_OK;
1617     auto &[key, hashKey] = keyPair;
1618     errCode = DBCommon::CalcValueHash(key, hashKey);
1619     if (errCode != E_OK) {
1620         return errCode;
1621     }
1622     errCode = writeHandle_->GetKvDataByHashKey(hashKey, syncRecord);
1623     if (errCode != E_OK) {
1624         return errCode;
1625     }
1626     syncRecord.key = key;
1627     int localOperation = static_cast<int>(LocalOperType::LOCAL_OPR_NONE);
1628     SingleVerRecord localRecord;
1629 
1630     innerErrCode = -E_LOCAL_DEFEAT;
1631     if (writeHandle_->CheckIfKeyExisted(syncRecord.key, true, localRecord.value, localRecord.timestamp)) {
1632         if ((syncRecord.flag & DataItem::DELETE_FLAG) == DataItem::DELETE_FLAG) {
1633             if (updateTimestamp || localRecord.timestamp <= syncRecord.writeTimestamp) { // sync win
1634                 innerErrCode = -E_LOCAL_DELETED;
1635                 localOperation = static_cast<int>(LocalOperType::LOCAL_OPR_DEL);
1636             }
1637         } else if (updateTimestamp || localRecord.timestamp <= syncRecord.writeTimestamp) { // sync win
1638             innerErrCode = -E_LOCAL_COVERED;
1639             localOperation = static_cast<int>(LocalOperType::LOCAL_OPR_PUT);
1640         }
1641     } else { // no conflict entry in local
1642         innerErrCode = E_OK;
1643         if ((syncRecord.flag & DataItem::DELETE_FLAG) != DataItem::DELETE_FLAG) {
1644             localOperation = static_cast<int>(LocalOperType::LOCAL_OPR_PUT);
1645         }
1646     }
1647 
1648     if (localOperation != static_cast<int>(LocalOperType::LOCAL_OPR_NONE)) {
1649         errCode = UnpublishOper(committedData, syncRecord, updateTimestamp, localOperation);
1650     }
1651 
1652     return errCode;
1653 }
1654 
UnpublishOper(SingleVerNaturalStoreCommitNotifyData * & committedData,const SingleVerRecord & syncRecord,bool updateTimestamp,int operType)1655 int SQLiteSingleVerNaturalStoreConnection::UnpublishOper(SingleVerNaturalStoreCommitNotifyData *&committedData,
1656     const SingleVerRecord &syncRecord, bool updateTimestamp, int operType)
1657 {
1658     committedData = new (std::nothrow) SingleVerNaturalStoreCommitNotifyData;
1659     if (committedData == nullptr) {
1660         return -E_OUT_OF_MEMORY;
1661     }
1662 
1663     int errCode = E_OK;
1664     if (operType == static_cast<int>(LocalOperType::LOCAL_OPR_PUT)) {
1665         SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1666         if (naturalStore == nullptr) {
1667             return -E_INVALID_DB;
1668         }
1669 
1670         errCode = CheckDataStatus(syncRecord.key, syncRecord.value, false);
1671         if (errCode != E_OK) {
1672             return errCode;
1673         }
1674 
1675         Timestamp time = updateTimestamp ? naturalStore->GetCurrentTimestamp() : syncRecord.writeTimestamp;
1676         errCode = writeHandle_->PutKvData(SingleVerDataType::LOCAL_TYPE_SQLITE, syncRecord.key, syncRecord.value, time,
1677             committedData);
1678     } else if (operType == static_cast<int>(LocalOperType::LOCAL_OPR_DEL)) {
1679         Timestamp localTimestamp = 0;
1680         Value value;
1681         errCode = writeHandle_->DeleteLocalKvData(syncRecord.key, committedData, value, localTimestamp);
1682     }
1683 
1684     return errCode;
1685 }
1686 
ReleaseCommitData(SingleVerNaturalStoreCommitNotifyData * & committedData)1687 void SQLiteSingleVerNaturalStoreConnection::ReleaseCommitData(SingleVerNaturalStoreCommitNotifyData *&committedData)
1688 {
1689     if (committedData != nullptr) {
1690         RefObject::DecObjRef(committedData);
1691         committedData = nullptr;
1692     }
1693 }
1694 
PragmaPublish(void * parameter)1695 int SQLiteSingleVerNaturalStoreConnection::PragmaPublish(void *parameter)
1696 {
1697     PragmaPublishInfo *info = static_cast<PragmaPublishInfo *>(parameter);
1698     if (info == nullptr) {
1699         return -E_INVALID_ARGS;
1700     }
1701     if (IsExtendedCacheDBMode()) {
1702         int err = IsCacheDBMode() ? -E_EKEYREVOKED : -E_BUSY;
1703         LOGE("[PragmaPublish]Existed cache database can not read data, errCode = [%d]!", err);
1704         return err;
1705     }
1706     return PublishLocal(info->key, info->deleteLocal, info->updateTimestamp, info->action);
1707 }
1708 
PragmaUnpublish(void * parameter)1709 int SQLiteSingleVerNaturalStoreConnection::PragmaUnpublish(void *parameter)
1710 {
1711     PragmaUnpublishInfo *info = static_cast<PragmaUnpublishInfo *>(parameter);
1712     if (info == nullptr) {
1713         return -E_INVALID_ARGS;
1714     }
1715     if (IsExtendedCacheDBMode()) {
1716         int err = IsCacheDBMode() ? -E_EKEYREVOKED : -E_BUSY;
1717         LOGE("[PragmaUnpublish]Existed cache database can not read data, errCode = [%d]!", err);
1718         return err;
1719     }
1720     return UnpublishToLocal(info->key, info->isDeleteSync, info->isUpdateTime);
1721 }
1722 
PragmaSetAutoLifeCycle(const uint32_t * lifeTime)1723 int SQLiteSingleVerNaturalStoreConnection::PragmaSetAutoLifeCycle(const uint32_t *lifeTime)
1724 {
1725     if (lifeTime == nullptr || *lifeTime > MAX_AUTO_LIFE_CYCLE || *lifeTime < MIN_AUTO_LIFE_CYCLE) {
1726         return -E_INVALID_ARGS;
1727     }
1728     if (kvDB_ == nullptr) {
1729         return -E_INVALID_DB;
1730     }
1731     return static_cast<SQLiteSingleVerNaturalStore *>(kvDB_)->SetAutoLifeCycleTime(*lifeTime);
1732 }
1733 
PragmaResultSetCacheMode(PragmaData inMode)1734 int SQLiteSingleVerNaturalStoreConnection::PragmaResultSetCacheMode(PragmaData inMode)
1735 {
1736     if (inMode == nullptr) {
1737         return -E_INVALID_ARGS;
1738     }
1739     auto mode = *(static_cast<ResultSetCacheMode *>(inMode));
1740     if (mode != ResultSetCacheMode::CACHE_FULL_ENTRY && mode != ResultSetCacheMode::CACHE_ENTRY_ID_ONLY) {
1741         return -E_INVALID_ARGS;
1742     }
1743     cacheModeForNewResultSet_.store(mode);
1744     return E_OK;
1745 }
1746 
PragmaResultSetCacheMaxSize(PragmaData inSize)1747 int SQLiteSingleVerNaturalStoreConnection::PragmaResultSetCacheMaxSize(PragmaData inSize)
1748 {
1749     if (inSize == nullptr) {
1750         return -E_INVALID_ARGS;
1751     }
1752     int size = *(static_cast<int *>(inSize));
1753     if (size < RESULT_SET_CACHE_MAX_SIZE_MIN || size > RESULT_SET_CACHE_MAX_SIZE_MAX) {
1754         return -E_INVALID_ARGS;
1755     }
1756     cacheMaxSizeForNewResultSet_.store(size);
1757     return E_OK;
1758 }
1759 
1760 // use for getkvstore migrating cache data
PragmaTriggerToMigrateData(const SecurityOption & secOption) const1761 int SQLiteSingleVerNaturalStoreConnection::PragmaTriggerToMigrateData(const SecurityOption &secOption) const
1762 {
1763     if ((secOption.securityLabel != S3) || (secOption.securityFlag != SECE)) {
1764         LOGD("Only S3 SECE data need migrate data!");
1765         return E_OK;
1766     }
1767 
1768     LOGI("Begin trigger the migration data while open the database!");
1769     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1770     if (naturalStore == nullptr) {
1771         return -E_INVALID_CONNECTION;
1772     }
1773     return naturalStore->TriggerToMigrateData();
1774 }
1775 
CheckAmendValueContentForLocalProcedure(const Value & oriValue,Value & amendValue) const1776 int SQLiteSingleVerNaturalStoreConnection::CheckAmendValueContentForLocalProcedure(const Value &oriValue,
1777     Value &amendValue) const
1778 {
1779     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1780     if (naturalStore == nullptr) { // Not Likely
1781         return -E_INVALID_DB;
1782     }
1783     bool useAmendValue = false;
1784     return naturalStore->CheckValueAndAmendIfNeed(ValueSource::FROM_LOCAL, oriValue, amendValue, useAmendValue);
1785 }
1786 
CheckLogOverLimit(SQLiteSingleVerStorageExecutor * executor) const1787 bool SQLiteSingleVerNaturalStoreConnection::CheckLogOverLimit(SQLiteSingleVerStorageExecutor *executor) const
1788 {
1789     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1790     if (naturalStore == nullptr || executor == nullptr) { // Not Likely
1791         return false;
1792     }
1793     uint64_t logFileSize = executor->GetLogFileSize();
1794     bool result = logFileSize > naturalStore->GetMaxLogSize();
1795     if (result) {
1796         LOGW("Log size[%" PRIu64 "] over the limit", logFileSize);
1797     }
1798     return result;
1799 }
1800 
CalcHashDevID(PragmaDeviceIdentifier & pragmaDev)1801 int SQLiteSingleVerNaturalStoreConnection::CalcHashDevID(PragmaDeviceIdentifier &pragmaDev)
1802 {
1803     if (pragmaDev.deviceID.empty()) {
1804         return -E_INVALID_ARGS;
1805     }
1806     pragmaDev.deviceIdentifier = DBCommon::TransferHashString(pragmaDev.deviceID);
1807     return E_OK;
1808 }
1809 
GetKeys(const IOption & option,const Key & keyPrefix,std::vector<Key> & keys) const1810 int SQLiteSingleVerNaturalStoreConnection::GetKeys(const IOption &option,
1811     const Key &keyPrefix, std::vector<Key> &keys) const
1812 {
1813     keys.clear();
1814     std::vector<Entry> entries;
1815     int errCode = GetEntriesInner(false, option, keyPrefix, entries);
1816     if (errCode == E_OK) {
1817         keys.resize(entries.size());
1818         std::transform(entries.begin(), entries.end(), keys.begin(), [](auto &entry) {
1819             return std::move(entry.key);
1820         });
1821     }
1822     keys.shrink_to_fit();
1823     return errCode;
1824 }
1825 
GetEntriesInner(bool isGetValue,const IOption & option,const Key & keyPrefix,std::vector<Entry> & entries) const1826 int SQLiteSingleVerNaturalStoreConnection::GetEntriesInner(bool isGetValue, const IOption &option,
1827     const Key &keyPrefix, std::vector<Entry> &entries) const
1828 {
1829     if (keyPrefix.size() > DBConstant::MAX_KEY_SIZE) {
1830         return -E_INVALID_ARGS;
1831     }
1832 
1833     SingleVerDataType type;
1834     if (option.dataType == IOption::LOCAL_DATA) {
1835         type = SingleVerDataType::LOCAL_TYPE_SQLITE;
1836     } else if (option.dataType == IOption::SYNC_DATA) {
1837         type = SingleVerDataType::SYNC_TYPE;
1838     } else {
1839         return -E_INVALID_ARGS;
1840     }
1841 
1842     int errCode = CheckReadDataControlled();
1843     if (errCode != E_OK) {
1844         LOGE("[GetEntries] Existed cache database can not read data, errCode = [%d]!", errCode);
1845         return errCode;
1846     }
1847 
1848     DBDfxAdapter::StartTracing();
1849     {
1850         std::lock_guard<std::mutex> lock(transactionMutex_);
1851         if (writeHandle_ != nullptr) {
1852             LOGD("[SQLiteSingleVerNaturalStoreConnection] Transaction started already.");
1853             errCode = writeHandle_->GetEntries(isGetValue, type, keyPrefix, entries);
1854             DBDfxAdapter::FinishTracing();
1855             return errCode;
1856         }
1857     }
1858 
1859     SQLiteSingleVerStorageExecutor *handle = GetExecutor(false, errCode);
1860     if (handle == nullptr) {
1861         LOGE("[SQLiteSingleVerNaturalStoreConnection]::[GetEntries] Get executor failed, errCode = [%d]", errCode);
1862         DBDfxAdapter::FinishTracing();
1863         return errCode;
1864     }
1865 
1866     errCode = handle->GetEntries(isGetValue, type, keyPrefix, entries);
1867     ReleaseExecutor(handle);
1868     DBDfxAdapter::FinishTracing();
1869     return errCode;
1870 }
1871 
UpdateKey(const DistributedDB::UpdateKeyCallback & callback)1872 int SQLiteSingleVerNaturalStoreConnection::UpdateKey(const DistributedDB::UpdateKeyCallback &callback)
1873 {
1874     if (IsExtendedCacheDBMode()) {
1875         LOGE("[Connection] Not support update key in cache mode");
1876         return -E_NOT_SUPPORT;
1877     }
1878     int errCode = E_OK;
1879     {
1880         std::lock_guard<std::mutex> lock(transactionMutex_);
1881         if (writeHandle_ != nullptr) {
1882             LOGD("[Connection] Transaction started already.");
1883             errCode = writeHandle_->UpdateKey(callback);
1884             return errCode;
1885         }
1886     }
1887 
1888     SQLiteSingleVerStorageExecutor *handle = GetExecutor(true, errCode);
1889     if (handle == nullptr) {
1890         LOGE("[Connection]::[UpdateKey] Get executor failed, errCode = [%d]", errCode);
1891         return errCode;
1892     }
1893 
1894     errCode = handle->UpdateKey(callback);
1895     ReleaseExecutor(handle);
1896     return errCode;
1897 }
1898 
1899 #ifdef USE_DISTRIBUTEDDB_CLOUD
SetCloudDbSchema(const std::map<std::string,DataBaseSchema> & schema)1900 int SQLiteSingleVerNaturalStoreConnection::SetCloudDbSchema(const std::map<std::string, DataBaseSchema> &schema)
1901 {
1902     int errCode = E_OK;
1903     SQLiteSingleVerStorageExecutor *handle = GetExecutor(true, errCode);
1904     if (handle == nullptr) {
1905         LOGE("[Connection]::[UpdateKey] Get executor failed, errCode = [%d]", errCode);
1906         return errCode;
1907     }
1908     errCode = handle->StartTransaction(TransactType::IMMEDIATE);
1909     if (errCode != E_OK) {
1910         ReleaseExecutor(handle);
1911         return errCode;
1912     }
1913     errCode = handle->CreateCloudLogTable();
1914     if (errCode != E_OK) {
1915         (void)handle->Rollback();
1916         ReleaseExecutor(handle);
1917         LOGE("[SingleVerConnection] create cloud log table failed, errCode = [%d]", errCode);
1918         return errCode;
1919     }
1920     errCode = handle->Commit();
1921     ReleaseExecutor(handle);
1922     if (errCode != E_OK) {
1923         LOGE("[SingleVerConnection] commit create cloud log table failed, errCode = [%d]", errCode);
1924         return errCode;
1925     }
1926 
1927     auto naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1928     if (naturalStore == nullptr) {
1929         LOGE("[SingleVerConnection] the store is null");
1930         return -E_NOT_INIT;
1931     }
1932     return naturalStore->SetCloudDbSchema(schema);
1933 }
1934 #endif
1935 
RegisterObserverAction(const KvStoreObserver * observer,const ObserverAction & action)1936 int SQLiteSingleVerNaturalStoreConnection::RegisterObserverAction(const KvStoreObserver *observer,
1937     const ObserverAction &action)
1938 {
1939     auto naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1940     if (naturalStore == nullptr) {
1941         LOGE("[SingleVerConnection] the store is null");
1942         return -E_NOT_INIT;
1943     }
1944     return naturalStore->RegisterObserverAction(observer, action);
1945 }
1946 
UnRegisterObserverAction(const KvStoreObserver * observer)1947 int SQLiteSingleVerNaturalStoreConnection::UnRegisterObserverAction(const KvStoreObserver *observer)
1948 {
1949     auto naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1950     if (naturalStore == nullptr) {
1951         LOGW("[SingleVerConnection] unregister observer but store is null");
1952         return E_OK;
1953     }
1954     return naturalStore->UnRegisterObserverAction(observer);
1955 }
1956 
RemoveDeviceData(const std::string & device,ClearMode mode)1957 int SQLiteSingleVerNaturalStoreConnection::RemoveDeviceData(const std::string &device, ClearMode mode)
1958 {
1959     if (device.length() > DBConstant::MAX_DEV_LENGTH) {
1960         return -E_INVALID_ARGS;
1961     }
1962     if (mode == ClearMode::CLEAR_SHARED_TABLE) {
1963         return -E_NOT_SUPPORT;
1964     }
1965     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1966     if (naturalStore == nullptr) {
1967         return -E_INVALID_DB;
1968     }
1969     return naturalStore->RemoveDeviceData(device, mode);
1970 }
1971 
RemoveDeviceData(const std::string & device,const std::string & user,ClearMode mode)1972 int SQLiteSingleVerNaturalStoreConnection::RemoveDeviceData(const std::string &device, const std::string &user,
1973     ClearMode mode)
1974 {
1975     if (device.length() > DBConstant::MAX_DEV_LENGTH) {
1976         return -E_INVALID_ARGS;
1977     }
1978     if (mode == ClearMode::CLEAR_SHARED_TABLE) {
1979         return -E_NOT_SUPPORT;
1980     }
1981     SQLiteSingleVerNaturalStore *naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1982     if (naturalStore == nullptr) {
1983         return -E_INVALID_DB;
1984     }
1985     return naturalStore->RemoveDeviceData(device, user, mode);
1986 }
1987 
1988 #ifdef USE_DISTRIBUTEDDB_CLOUD
GetCloudVersion(const std::string & device,std::map<std::string,std::string> & versionMap)1989 int SQLiteSingleVerNaturalStoreConnection::GetCloudVersion(const std::string &device,
1990     std::map<std::string, std::string> &versionMap)
1991 {
1992     auto naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
1993     if (naturalStore == nullptr) {
1994         return -E_INVALID_DB;
1995     }
1996     return naturalStore->GetCloudVersion(device, versionMap);
1997 }
1998 
SetCloudSyncConfig(const CloudSyncConfig & config)1999 int SQLiteSingleVerNaturalStoreConnection::SetCloudSyncConfig(const CloudSyncConfig &config)
2000 {
2001     auto naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
2002     if (naturalStore == nullptr) {
2003         LOGE("[SingleVerConnection] DB is null when set config");
2004         return -E_INVALID_DB;
2005     }
2006     return naturalStore->SetCloudSyncConfig(config);
2007 }
2008 
ClearCloudWatermark()2009 int SQLiteSingleVerNaturalStoreConnection::ClearCloudWatermark()
2010 {
2011     auto naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
2012     if (naturalStore == nullptr) {
2013         LOGE("[SingleVerConnection] DB is null when clear cloud watermark");
2014         return -E_INVALID_DB;
2015     }
2016     return naturalStore->ClearCloudWatermark();
2017 }
2018 #endif
2019 
RecordTimeIntoDataItem(Timestamp existCreateTime,DataItem & dataItem,SQLiteSingleVerNaturalStore & naturalStore)2020 void SQLiteSingleVerNaturalStoreConnection::RecordTimeIntoDataItem(Timestamp existCreateTime, DataItem &dataItem,
2021     SQLiteSingleVerNaturalStore &naturalStore)
2022 {
2023     dataItem.timestamp = naturalStore.GetCurrentTimestamp(false);
2024     if (currentMaxTimestamp_ > dataItem.timestamp) {
2025         dataItem.timestamp = currentMaxTimestamp_;
2026     }
2027 
2028     auto currentRawTime = TimeHelper::GetSysCurrentTime();
2029     if (existCreateTime != 0) {
2030         dataItem.writeTimestamp = existCreateTime;
2031     } else {
2032         dataItem.writeTimestamp = dataItem.timestamp;
2033     }
2034     dataItem.createTime = currentRawTime;
2035     dataItem.modifyTime = currentRawTime;
2036 }
2037 
GetEntries(const std::string & device,std::vector<Entry> & entries) const2038 int SQLiteSingleVerNaturalStoreConnection::GetEntries(const std::string &device, std::vector<Entry> &entries) const
2039 {
2040     int errCode = CheckReadDataControlled();
2041     if (errCode != E_OK) {
2042         LOGE("[GetEntries] Existed cache database can not read data, errCode = [%d]!", errCode);
2043         return errCode;
2044     }
2045     std::string localId;
2046     errCode = RuntimeContext::GetInstance()->GetLocalIdentity(localId);
2047     std::string getDevice;
2048     // if device is local, just search with empty string
2049     if (errCode != E_OK || localId != device) {
2050         getDevice = device;
2051     }
2052     {
2053         std::lock_guard<std::mutex> lock(transactionMutex_);
2054         if (writeHandle_ != nullptr) {
2055             LOGD("[SQLiteSingleVerNaturalStoreConnection] Transaction started already.");
2056             return writeHandle_->GetEntries(getDevice, entries);
2057         }
2058     }
2059 
2060     SQLiteSingleVerStorageExecutor *handle = GetExecutor(false, errCode);
2061     if (handle == nullptr) {
2062         LOGE("[SQLiteSingleVerNaturalStoreConnection]::[GetEntries] Get executor failed, errCode = [%d]", errCode);
2063         return errCode;
2064     }
2065 
2066     errCode = handle->GetEntries(getDevice, entries);
2067     ReleaseExecutor(handle);
2068     return errCode;
2069 }
2070 
OperateDataStatus(uint32_t dataOperator)2071 int SQLiteSingleVerNaturalStoreConnection::OperateDataStatus(uint32_t dataOperator)
2072 {
2073     auto naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
2074     if (naturalStore == nullptr) {
2075         LOGE("[SingleVerConnection] DB is null when operate data status");
2076         return -E_INVALID_DB;
2077     }
2078     return naturalStore->OperateDataStatus(dataOperator);
2079 }
2080 
RemoveDeviceDataByCmd(void * parameter)2081 int SQLiteSingleVerNaturalStoreConnection::RemoveDeviceDataByCmd(void *parameter)
2082 {
2083     auto naturalStore = GetDB<SQLiteSingleVerNaturalStore>();
2084     if (naturalStore == nullptr || parameter == nullptr) {
2085         return -E_INVALID_DB;
2086     }
2087     auto deviceName = static_cast<std::string *>(parameter);
2088     return naturalStore->RemoveDeviceData(*deviceName, false, false);
2089 }
2090 
IsInWhitelist() const2091 bool SQLiteSingleVerNaturalStoreConnection::IsInWhitelist() const
2092 {
2093     if (kvDB_ == nullptr) {
2094         return false;
2095     }
2096 
2097     std::string appId = kvDB_->GetMyProperties().GetStringProp(DBProperties::APP_ID, "");
2098     return appId == DBConstant::DISTRIBUTED_DEFAULT_APP_ID;
2099 }
2100 DEFINE_OBJECT_TAG_FACILITIES(SQLiteSingleVerNaturalStoreConnection)
2101 }