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