diff --git a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.cpp b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.cpp index 77883180ee0db191b7be2aefe072e88b4b1ab141..a49978acefac969c97b5b0397c5ab6a10baf9301 100755 --- a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.cpp @@ -457,11 +457,8 @@ int RdSingleVerNaturalStoreConnection::CheckSyncEntriesValid(const std::vector &entries) +int RdSingleVerNaturalStoreConnection::PreparePutData(const IOption &option, bool &isAuto) { - LOGD("PutBatchInner"); - std::lock_guard lock(transactionMutex_); - bool isAuto = false; int errCode = E_OK; if (option.dataType != IOption::SYNC_DATA) { LOGE("LOCAL_DATA TYPE NOT SUPPORT in RD executor"); @@ -470,38 +467,58 @@ int RdSingleVerNaturalStoreConnection::PutBatchInner(const IOption &option, cons if (writeHandle_ == nullptr) { isAuto = true; errCode = StartTransactionInner(TransactType::IMMEDIATE); - if (errCode != E_OK) { - return errCode; - } } + return errCode; +} - errCode = SaveSyncEntries(entries, false); - +void RdSingleVerNaturalStoreConnection::FinishOperate(bool &isAuto, const bool isSetObserver, int &errCode) +{ if (isAuto) { if (errCode == E_OK) { - errCode = CommitInner(); + errCode = CommitInner(isSetObserver); } else { int innerCode = RollbackInner(); errCode = (innerCode != E_OK) ? innerCode : errCode; } } +} + +int RdSingleVerNaturalStoreConnection::PutInner(const IOption &option, const Key &key, const Value &value) +{ + std::lock_guard lock(transactionMutex_); + bool isSetObserver = (!IsObserverListEmpty()); + bool isAuto = false; + int errCode = E_OK; + errCode = PreparePutData(option, isAuto); + if (errCode != E_OK) { + return errCode; + } + Entry entry{key, value}; + errCode = SaveEntry(entry, false, isSetObserver); + FinishOperate(isAuto, isSetObserver, errCode); return errCode; } -int RdSingleVerNaturalStoreConnection::SaveSyncEntries(const std::vector &entries, bool isDelete) +int RdSingleVerNaturalStoreConnection::PutBatchInner(const IOption &option, const std::vector &entries) { - if (IsSinglePutOrDelete(entries)) { - return SaveEntry(entries[0], isDelete); + std::lock_guard lock(transactionMutex_); + bool isAuto = false; + int errCode = E_OK; + errCode = PreparePutData(option, isAuto); + if (errCode != E_OK) { + return errCode; } - return writeHandle_->BatchSaveEntries(entries, isDelete, committedData_); + errCode = writeHandle_->BatchSaveEntries(entries, false, committedData_); + FinishOperate(isAuto, true, errCode); + return errCode; } // This function currently only be called in local procedure to change sync_data table, do not use in sync procedure. // It will check and amend value when need if it is a schema database. return error if some value disagree with the // schema. But in sync procedure, we just neglect the value that disagree with schema. -int RdSingleVerNaturalStoreConnection::SaveEntry(const Entry &entry, bool isDelete, Timestamp timestamp) +int RdSingleVerNaturalStoreConnection::SaveEntry(const Entry &entry, bool isDelete, bool isSetObserver, + Timestamp timestamp) { - LOGD("Saving Entry"); RdSingleVerNaturalStore *naturalStore = GetDB(); if (naturalStore == nullptr) { LOGE("[RdSingleVerNaturalStoreConnection][SaveEntry] the store is null"); @@ -511,13 +528,18 @@ int RdSingleVerNaturalStoreConnection::SaveEntry(const Entry &entry, bool isDele if (IsExtendedCacheDBMode()) { return -E_NOT_SUPPORT; } else { - return SaveEntryNormally(entry, isDelete); + return SaveEntryNormally(entry, isDelete, isSetObserver); } } -int RdSingleVerNaturalStoreConnection::SaveEntryNormally(const Entry &entry, bool isDelete) +int RdSingleVerNaturalStoreConnection::SaveEntryNormally(const Entry &entry, bool isDelete, bool isSetObserver) { - int errCode = writeHandle_->SaveSyncDataItem(entry, committedData_, isDelete); + int errCode = E_OK; + if (isSetObserver) { + errCode = writeHandle_->SaveSyncDataItem(entry, committedData_, isDelete); + } else { + errCode = writeHandle_->SaveDataToDatabase(entry, isDelete); + } if (errCode != E_OK) { LOGE("Save entry failed, err:%d", errCode); } @@ -596,14 +618,18 @@ int RdSingleVerNaturalStoreConnection::StartTransactionNormally(TransactType tra return E_OK; } -int RdSingleVerNaturalStoreConnection::CommitInner() +int RdSingleVerNaturalStoreConnection::CommitInner(const bool isSetObserver) { int errCode = writeHandle_->Commit(); ReleaseExecutor(writeHandle_); writeHandle_ = nullptr; - CommitAndReleaseNotifyData(committedData_, true, - static_cast(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_PUT_EVENT)); + if (isSetObserver) { + CommitAndReleaseNotifyData(committedData_, true, + static_cast(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_NS_PUT_EVENT)); + } else { + ReleaseCommitData(committedData_); + } return errCode; } @@ -649,6 +675,7 @@ int RdSingleVerNaturalStoreConnection::CheckSyncKeysValid(const std::vector int RdSingleVerNaturalStoreConnection::DeleteBatchInner(const IOption &option, const std::vector &keys) { DBDfxAdapter::StartTracing(); + bool isSetObserver = (!IsObserverListEmpty()); bool isAuto = false; int errCode = E_OK; if (option.dataType != IOption::SYNC_DATA) { @@ -666,30 +693,29 @@ int RdSingleVerNaturalStoreConnection::DeleteBatchInner(const IOption &option, c } } - errCode = DeleteSyncEntries(keys); + errCode = DeleteSyncEntries(keys, isSetObserver); - if (isAuto) { - if (errCode == E_OK) { - errCode = CommitInner(); - } else { - int innerCode = RollbackInner(); - errCode = (innerCode != E_OK) ? innerCode : errCode; - } - } + FinishOperate(isAuto, isSetObserver, errCode); DBDfxAdapter::FinishTracing(); return errCode; } -int RdSingleVerNaturalStoreConnection::DeleteSyncEntries(const std::vector &keys) +int RdSingleVerNaturalStoreConnection::DeleteSyncEntries(const std::vector &keys, bool isSetObserver) { std::vector entries; + int keyCount = 0; // Record keys vector size. for (const auto &key : keys) { Entry entry; entry.key = std::move(key); entries.emplace_back(std::move(entry)); + keyCount++; + } + int errCode = E_OK; + if (keyCount == 1) { // 1 means only delete one data. + errCode = SaveEntry(entries[0], true, isSetObserver); + } else { + errCode = writeHandle_->BatchSaveEntries(entries, true, committedData_); } - - int errCode = SaveSyncEntries(entries, true); if ((errCode != E_OK) && (errCode != -E_NOT_FOUND)) { LOGE("[DeleteSyncEntries] Delete data err:%d", errCode); } diff --git a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.h b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.h index 39680da2f03cbcb4d71b9f4b7879e97c44096626..6a0356b824edaf7ab0307ec9d91c63e5fc39b78d 100644 --- a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.h +++ b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.h @@ -117,16 +117,16 @@ private: int CheckSyncEntriesValid(const std::vector &entries) const override; - int PutBatchInner(const IOption &option, const std::vector &entries) override; + int PutInner(const IOption &option, const Key &key, const Value &value) override; - int SaveSyncEntries(const std::vector &entries, bool isDelete); + int PutBatchInner(const IOption &option, const std::vector &entries) override; // This func currently only be called in local procedure to change sync_data table, do not use in sync procedure. // It will check and amend value when need if it is a schema database. return error if some value disagree with the // schema. But in sync procedure, we just neglect the value that disagree with schema. - int SaveEntry(const Entry &entry, bool isDelete, Timestamp timestamp = 0); + int SaveEntry(const Entry &entry, bool isDelete, bool isSetObserver, Timestamp timestamp = 0); - int SaveEntryNormally(const Entry &entry, bool isDelete); + int SaveEntryNormally(const Entry &entry, bool isDelete, bool isSetObserver); RdSingleVerStorageExecutor *GetExecutor(bool isWrite, int &errCode) const; @@ -138,10 +138,14 @@ private: int StartTransactionNormally(TransactType transType = TransactType::DEFERRED); - int CommitInner(); + int CommitInner(const bool isSetObserver); int RollbackInner(); + int PreparePutData(const IOption &option, bool &isAuto); + + void FinishOperate(bool &isAuto, const bool isSetObserver, int &errCode); + void CommitAndReleaseNotifyData(SingleVerNaturalStoreCommitNotifyData *&committedData, bool isNeedCommit, int eventType); @@ -151,12 +155,7 @@ private: int DeleteBatchInner(const IOption &option, const std::vector &keys) override; - int DeleteSyncEntries(const std::vector &keys); - - bool IsSinglePutOrDelete(const std::vector &entries) - { - return entries.size() == 1; - } + int DeleteSyncEntries(const std::vector &keys, bool isSetObserver); SingleVerNaturalStoreCommitNotifyData *committedData_; // used for transaction diff --git a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_storage_executor.cpp b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_storage_executor.cpp index 031f6fbea866090f7d2834ddc7e3014d58bf1cb9..09e4c864174073d6330b5ecbe1c1ff69ac5749da 100755 --- a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_storage_executor.cpp +++ b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_storage_executor.cpp @@ -463,7 +463,7 @@ int RdSingleVerStorageExecutor::SaveSyncDataItem(const Entry &entry, return errCode; } - errCode = SaveSyncDataToDatabase(entry, isDelete); + errCode = SaveDataToDatabase(entry, isDelete); if (errCode == E_OK) { PutIntoCommittedData(entry.key, entry.value, notify); } else { @@ -769,7 +769,7 @@ int RdSingleVerStorageExecutor::PrepareForNotifyConflictAndObserver(const Entry return E_OK; } -int RdSingleVerStorageExecutor::SaveSyncDataToDatabase(const Entry &entry, bool isDelete) +int RdSingleVerStorageExecutor::SaveDataToDatabase(const Entry &entry, bool isDelete) { if (isDelete) { return DelKvData(entry.key); diff --git a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_storage_executor.h b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_storage_executor.h index b0dc861a5a20c8023a6d40e4dee112027a0ae978..e426863a6cdc690912770d1d632e893a9db287ec 100755 --- a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_storage_executor.h +++ b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_storage_executor.h @@ -193,6 +193,8 @@ public: int GetExistsDevicesFromMeta(std::set &devices); int UpdateKey(const UpdateKeyCallback &callback); + + int SaveDataToDatabase(const Entry &entry, bool isDelete); protected: int SaveKvData(SingleVerDataType type, const Key &key, const Value &value); @@ -208,8 +210,6 @@ private: int DelKvData(const Key &key); - int SaveSyncDataToDatabase(const Entry &entry, bool isDelete); - int InnerMoveToHead(const int position, GRD_ResultSet *resultSet, int &currPosition); static int ClearEntriesAndFreeResultSet(std::vector &entries, GRD_ResultSet *resultSet); diff --git a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.cpp b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.cpp index 6d8e198538e44dea38fe40967c9d73220ac8d9ae..10d19105d453086bc0128242c1244604268fed8b 100644 --- a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.cpp @@ -167,6 +167,12 @@ int GenericKvDBConnection::UnRegisterObserver(const KvDBObserverHandle *observer return E_OK; } +bool GenericKvDBConnection::IsObserverListEmpty() +{ + std::lock_guard lockGuard(observerListLock_); + return observerList_.empty(); +} + int GenericKvDBConnection::SetConflictNotifier(int conflictType, const KvDBConflictAction &action) { (void)conflictType; diff --git a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.h b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.h index 05a8c8533073e27cc4fe28ad9d4c69a6b93610a5..e140e1405260ba9a9f076af0fad533b3418b31b7 100644 --- a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.h +++ b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.h @@ -96,6 +96,8 @@ protected: void ResetExclusiveStatus(); + bool IsObserverListEmpty(); + // Called in Close(), overriding of Close() is forbidden. virtual int PreClose(); diff --git a/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_connection.cpp b/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_connection.cpp index c7fd1dc028753ca18d6507433dda4985450875f0..105a6146889b4da245874ee8a37c6acdd6314b08 100644 --- a/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_connection.cpp @@ -30,11 +30,7 @@ SingleVerNaturalStoreConnection::~SingleVerNaturalStoreConnection() int SingleVerNaturalStoreConnection::Put(const IOption &option, const Key &key, const Value &value) { - std::vector entries; - Entry entry{key, value}; - entries.emplace_back(std::move(entry)); - - return PutBatch(option, entries); + return PutInner(option, key, value); } int SingleVerNaturalStoreConnection::PutBatch(const IOption &option, const std::vector &entries) diff --git a/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_connection.h b/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_connection.h index 6c0f72f3f4fbab518199345261e62cfa4e178bc1..4a4b33da2a2d7cdfdc2e0149a6ec5883fff36647 100644 --- a/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_connection.h +++ b/frameworks/libs/distributeddb/storage/src/single_ver_natural_store_connection.h @@ -43,6 +43,7 @@ protected: virtual int CheckWritePermission() const; + virtual int PutInner(const IOption &option, const Key &key, const Value &value) = 0; virtual int PutBatchInner(const IOption &option, const std::vector &entries) = 0; virtual int CheckSyncKeysValid(const std::vector &keys) const = 0; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp index a4891d86f191a1208f8abac95a2856f623702d1c..a2af230f58468de67154160bad56665011a21ace 100755 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp @@ -232,6 +232,14 @@ int SQLiteSingleVerNaturalStoreConnection::GetCount(const IOption &option, const return errCode; } +int SQLiteSingleVerNaturalStoreConnection::PutInner(const IOption &option, const Key &key, const Value &value) +{ + std::vector entries; + Entry entry{key, value}; + entries.emplace_back(std::move(entry)); + return PutBatch(option, entries); +} + int SQLiteSingleVerNaturalStoreConnection::PutBatch(const IOption &option, const std::vector &entries) { LOGD("[PutBatch] entries size is : %zu, dataType : %d", entries.size(), option.dataType); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h index da101f89684651979fae788992508e67d644a3bb..d40b38c9ab8aea72b54e38b06870eb9faf87064c 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h @@ -110,6 +110,7 @@ private: void ClearConflictNotifierCount(); + int PutInner(const IOption &option, const Key &key, const Value &value) override; int PutBatchInner(const IOption &option, const std::vector &entries) override; int DeleteBatchInner(const IOption &option, const std::vector &keys) override;