From 2d6616fcbf82e681e3a7730785d07a0d7784c385 Mon Sep 17 00:00:00 2001 From: wddhw Date: Mon, 29 Jul 2024 20:13:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=9E=8D=E5=90=88=E6=B0=B4=E4=BD=8D=E4=BD=BF?= =?UTF-8?q?=E7=94=A8UDID=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wddhw --- .../kvdb/src/process_communication_impl.cpp | 10 ++ .../common/include/runtime_context.h | 1 + .../common/src/runtime_context_impl.cpp | 10 ++ .../common/src/runtime_context_impl.h | 1 + .../include/communicator_aggregator.h | 3 +- .../communicator/include/iadapter.h | 1 + .../include/icommunicator_aggregator.h | 1 + .../communicator/include/network_adapter.h | 3 +- .../src/communicator_aggregator.cpp | 5 + .../communicator/src/network_adapter.cpp | 12 ++ .../include/iprocess_communicator.h | 4 + .../relational/relational_sync_able_storage.h | 5 + .../include/icloud_sync_storage_interface.h | 9 +- .../storage/include/storage_proxy.h | 4 +- .../relational_sync_able_storage.cpp | 27 ++++ ...e_single_ver_relational_storage_executor.h | 2 + ...ver_relational_storage_executor_extend.cpp | 17 +++ .../sqlite/sqlite_cloud_kv_executor_utils.cpp | 92 +++++++++++++- .../sqlite/sqlite_cloud_kv_executor_utils.h | 13 +- .../src/sqlite/sqlite_cloud_kv_store.cpp | 35 +++++- .../src/sqlite/sqlite_cloud_kv_store.h | 4 +- .../sqlite_single_ver_storage_executor_sql.h | 6 + .../storage/src/storage_proxy.cpp | 17 ++- .../syncer/src/cloud/cloud_syncer.cpp | 1 - .../syncer/src/cloud/cloud_syncer.h | 4 +- .../syncer/src/cloud/cloud_syncer_extend.cpp | 43 ++++++- .../common/process_communicator_test_stub.h | 7 ++ .../common/communicator/adapter_stub.cpp | 6 + .../common/communicator/adapter_stub.h | 2 + .../communicator/mock_process_communicator.h | 1 + ...tributeddb_interfaces_auto_launch_test.cpp | 5 + .../cloud/distributeddb_cloud_kv_test.cpp | 118 ++++++++++++++++++ .../mock_icloud_sync_storage_interface.h | 1 + .../virtual_communicator_aggregator.cpp | 7 ++ .../syncer/virtual_communicator_aggregator.h | 1 + 35 files changed, 450 insertions(+), 28 deletions(-) diff --git a/frameworks/innerkitsimpl/kvdb/src/process_communication_impl.cpp b/frameworks/innerkitsimpl/kvdb/src/process_communication_impl.cpp index fb60a394b2c..e1b08455c1b 100644 --- a/frameworks/innerkitsimpl/kvdb/src/process_communication_impl.cpp +++ b/frameworks/innerkitsimpl/kvdb/src/process_communication_impl.cpp @@ -108,6 +108,16 @@ DistributedDB::DeviceInfos ProcessCommunicationImpl::GetLocalDeviceInfos() return devInfos; } +DistributedDB::DeviceInfos ProcessCommunicationImpl::GetImmutableLocalDeviceInfos() +{ + // get identifier generated by hardware identifier, need to be replaced by new api + std::string identifier = endpoint_->GetLocalDeviceInfos(); + DistributedDB::DeviceInfos devInfos = { + identifier + }; + return devInfos; +} + std::vector ProcessCommunicationImpl::GetRemoteOnlineDeviceInfosList() { return {}; diff --git a/frameworks/libs/distributeddb/common/include/runtime_context.h b/frameworks/libs/distributeddb/common/include/runtime_context.h index d3f8d9e1753..2dd0e5a5999 100644 --- a/frameworks/libs/distributeddb/common/include/runtime_context.h +++ b/frameworks/libs/distributeddb/common/include/runtime_context.h @@ -58,6 +58,7 @@ public: virtual int GetCommunicatorAggregator(ICommunicatorAggregator *&outAggregator) = 0; virtual void SetCommunicatorAggregator(ICommunicatorAggregator *inAggregator) = 0; virtual int GetLocalIdentity(std::string &outTarget) = 0; + virtual int GetImmutableLocalIdentity(std::string &outTarget) = 0; // Timer interfaces. virtual int SetTimer(int milliSeconds, const TimerAction &action, diff --git a/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp b/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp index bea56caf454..34498234536 100644 --- a/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp +++ b/frameworks/libs/distributeddb/common/src/runtime_context_impl.cpp @@ -157,6 +157,16 @@ int RuntimeContextImpl::GetLocalIdentity(std::string &outTarget) return -E_NOT_INIT; } +int RuntimeContextImpl::GetImmutableLocalIdentity(std::string &outTarget) +{ + std::lock_guard autoLock(communicatorLock_); + if (communicatorAggregator_ != nullptr) { + return communicatorAggregator_->GetImmutableLocalIdentity(outTarget); + } + LOGW("[RuntimeContextImpl] Get immutable local id without communicatorAggregator"); + return -E_NOT_INIT; +} + // Add and start a timer. int RuntimeContextImpl::SetTimer(int milliSeconds, const TimerAction &action, const TimerFinalizer &finalizer, TimerId &timerId) diff --git a/frameworks/libs/distributeddb/common/src/runtime_context_impl.h b/frameworks/libs/distributeddb/common/src/runtime_context_impl.h index a9f40a298f0..5c18f4309be 100644 --- a/frameworks/libs/distributeddb/common/src/runtime_context_impl.h +++ b/frameworks/libs/distributeddb/common/src/runtime_context_impl.h @@ -44,6 +44,7 @@ public: int GetCommunicatorAggregator(ICommunicatorAggregator *&outAggregator) override; void SetCommunicatorAggregator(ICommunicatorAggregator *inAggregator) override; int GetLocalIdentity(std::string &outTarget) override; + int GetImmutableLocalIdentity(std::string &outTarget) override; // Add and start a timer. int SetTimer(int milliSeconds, const TimerAction &action, const TimerFinalizer &finalizer, TimerId &timerId) override; diff --git a/frameworks/libs/distributeddb/communicator/include/communicator_aggregator.h b/frameworks/libs/distributeddb/communicator/include/communicator_aggregator.h index a552ccb4a83..390c370c4a3 100644 --- a/frameworks/libs/distributeddb/communicator/include/communicator_aggregator.h +++ b/frameworks/libs/distributeddb/communicator/include/communicator_aggregator.h @@ -77,7 +77,8 @@ public: uint32_t GetCommunicatorAggregatorTimeout(const std::string &target) const; bool IsDeviceOnline(const std::string &device) const; int GetLocalIdentity(std::string &outTarget) const override; - + int GetImmutableLocalIdentity(std::string &outTarget) const override; + // Get the protocol version of remote target. Return -E_NOT_FOUND if no record. int GetRemoteCommunicatorVersion(const std::string &target, uint16_t &outVersion) const; diff --git a/frameworks/libs/distributeddb/communicator/include/iadapter.h b/frameworks/libs/distributeddb/communicator/include/iadapter.h index 9945c8d449f..5a065991d52 100644 --- a/frameworks/libs/distributeddb/communicator/include/iadapter.h +++ b/frameworks/libs/distributeddb/communicator/include/iadapter.h @@ -51,6 +51,7 @@ public: // Get local target name for identify self virtual int GetLocalIdentity(std::string &outTarget) = 0; + virtual int GetImmutableLocalIdentity(std::string &outTarget) = 0; // Not assume bytes to be heap memory. Not assume SendBytes to be not blocking // Return 0 as success. Return negative as error diff --git a/frameworks/libs/distributeddb/communicator/include/icommunicator_aggregator.h b/frameworks/libs/distributeddb/communicator/include/icommunicator_aggregator.h index 656e165bced..3af390b725c 100644 --- a/frameworks/libs/distributeddb/communicator/include/icommunicator_aggregator.h +++ b/frameworks/libs/distributeddb/communicator/include/icommunicator_aggregator.h @@ -49,6 +49,7 @@ public: virtual int RegCommunicatorLackCallback(const CommunicatorLackCallback &onCommLack, const Finalizer &inOper) = 0; virtual int RegOnConnectCallback(const OnConnectCallback &onConnect, const Finalizer &inOper) = 0; virtual int GetLocalIdentity(std::string &outTarget) const = 0; + virtual int GetImmutableLocalIdentity(std::string &outTarget) const = 0; virtual ~ICommunicatorAggregator() {}; }; } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/communicator/include/network_adapter.h b/frameworks/libs/distributeddb/communicator/include/network_adapter.h index 06ebd559bd4..868dc47df27 100644 --- a/frameworks/libs/distributeddb/communicator/include/network_adapter.h +++ b/frameworks/libs/distributeddb/communicator/include/network_adapter.h @@ -43,7 +43,8 @@ public: uint32_t GetTimeout() override; uint32_t GetTimeout(const std::string &target) override; int GetLocalIdentity(std::string &outTarget) override; - + int GetImmutableLocalIdentity(std::string &outTarget) override; + int SendBytes(const std::string &dstTarget, const uint8_t *bytes, uint32_t length, uint32_t totalLength) override; diff --git a/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp b/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp index cd2952d8cb8..a75b8bb564c 100644 --- a/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp +++ b/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp @@ -248,6 +248,11 @@ int CommunicatorAggregator::GetLocalIdentity(std::string &outTarget) const return adapterHandle_->GetLocalIdentity(outTarget); } +int CommunicatorAggregator::GetImmutableLocalIdentity(std::string &outTarget) const +{ + return adapterHandle_->GetImmutableLocalIdentity(outTarget); +} + void CommunicatorAggregator::ActivateCommunicator(const LabelType &commLabel, const std::string &userId) { std::lock_guard commMapLockGuard(commMapMutex_); diff --git a/frameworks/libs/distributeddb/communicator/src/network_adapter.cpp b/frameworks/libs/distributeddb/communicator/src/network_adapter.cpp index c2d7066e7b6..1fbd43bf7b8 100644 --- a/frameworks/libs/distributeddb/communicator/src/network_adapter.cpp +++ b/frameworks/libs/distributeddb/communicator/src/network_adapter.cpp @@ -212,6 +212,18 @@ int NetworkAdapter::GetLocalIdentity(std::string &outTarget) return E_OK; } +int NetworkAdapter::GetImmutableLocalIdentity(std::string &outTarget) +{ + std::lock_guard identityLockGuard(identityMutex_); + DeviceInfos devInfo = processCommunicator_->GetImmutableLocalDeviceInfos(); + if (devInfo.identifier.empty()) { + LOGE("[NetworkAdapter] Get empty immutable local id"); + return -E_PERIPHERAL_INTERFACE_FAIL; + } + outTarget = devInfo.identifier; + return E_OK; +} + int NetworkAdapter::SendBytes(const std::string &dstTarget, const uint8_t *bytes, uint32_t length, uint32_t totalLength) { if (bytes == nullptr || length == 0) { diff --git a/frameworks/libs/distributeddb/interfaces/include/iprocess_communicator.h b/frameworks/libs/distributeddb/interfaces/include/iprocess_communicator.h index 5e4faf79dcc..4cc47e7aeff 100644 --- a/frameworks/libs/distributeddb/interfaces/include/iprocess_communicator.h +++ b/frameworks/libs/distributeddb/interfaces/include/iprocess_communicator.h @@ -112,6 +112,10 @@ public: // All field of returned DeviceInfos must be valid, the identifier must not be empty and changed between time. virtual DeviceInfos GetLocalDeviceInfos() = 0; + // The GetImmutableLocalDeviceInfos function should only be called after Start successfully + // All field of returned DeviceInfos must be valid, the identifier must not be empty and immutable哦那 between time. + virtual DeviceInfos GetImmutableLocalDeviceInfos() = 0; + // The GetRemoteOnlineDeviceInfosList function should only be called after Start successfully // All field of returned DeviceInfos must be valid, should not contain duplicate device or local device virtual std::vector GetRemoteOnlineDeviceInfosList() = 0; diff --git a/frameworks/libs/distributeddb/interfaces/src/relational/relational_sync_able_storage.h b/frameworks/libs/distributeddb/interfaces/src/relational/relational_sync_able_storage.h index af865f8e7c3..ead448ea53d 100644 --- a/frameworks/libs/distributeddb/interfaces/src/relational/relational_sync_able_storage.h +++ b/frameworks/libs/distributeddb/interfaces/src/relational/relational_sync_able_storage.h @@ -168,6 +168,8 @@ public: DataInfoWithLog &dataInfoWithLog, VBucket &assetInfo) override; int PutCloudSyncData(const std::string &tableName, DownloadData &downloadData) override; + + int DeleteLocalCloudVersionData(const std::string &tableName, const std::string &user) override; int CleanCloudData(ClearMode mode, const std::vector &tableNameList, const RelationalSchemaObject &localSchema, std::vector &assets) override; @@ -230,6 +232,9 @@ protected: int PutCloudSyncDataInner(SQLiteSingleVerRelationalStorageExecutor *handle, const std::string &tableName, DownloadData &downloadData); + + int DeleteLocalCloudVersionDataInner(SQLiteSingleVerRelationalStorageExecutor *handle, const std::string &tableName, + const std::string &user); virtual int GetReferenceGid(const std::string &tableName, const CloudSyncBatch &syncBatch, std::map &referenceGid); diff --git a/frameworks/libs/distributeddb/storage/include/icloud_sync_storage_interface.h b/frameworks/libs/distributeddb/storage/include/icloud_sync_storage_interface.h index 3e441e2abec..aed2cfd5cc0 100644 --- a/frameworks/libs/distributeddb/storage/include/icloud_sync_storage_interface.h +++ b/frameworks/libs/distributeddb/storage/include/icloud_sync_storage_interface.h @@ -44,6 +44,12 @@ enum class OpType : uint8_t { NOT_HANDLE }; +enum DeviceType : uint8_t { + UUID = 1, + UDID, + NOT_VALID +}; + typedef struct DownloadData { std::vector data; std::vector opType; @@ -129,6 +135,7 @@ public: DataInfoWithLog &dataInfoWithLog, VBucket &assetInfo) = 0; virtual int PutCloudSyncData(const std::string &tableName, DownloadData &downloadData) = 0; + virtual int DeleteLocalCloudVersionData(const std::string &tableName, const std::string &user) = 0; virtual int CleanCloudData(ClearMode mode, const std::vector &tableNameList, const RelationalSchemaObject &localSchema, std::vector &assets) @@ -202,7 +209,7 @@ public: { } - virtual std::pair GetLocalCloudVersion() + virtual std::pair GetLocalCloudVersion(const DeviceType deviceType) { return {E_OK, {}}; } diff --git a/frameworks/libs/distributeddb/storage/include/storage_proxy.h b/frameworks/libs/distributeddb/storage/include/storage_proxy.h index 1f702c2ec62..2342ebb86e4 100644 --- a/frameworks/libs/distributeddb/storage/include/storage_proxy.h +++ b/frameworks/libs/distributeddb/storage/include/storage_proxy.h @@ -82,6 +82,8 @@ public: int PutCloudSyncData(const std::string &tableName, DownloadData &downloadData); + int DeleteLocalCloudVersionData(const std::string &tableName); + int CleanCloudData(ClearMode mode, const std::vector &tableNameList, const RelationalSchemaObject &localSchema, std::vector &assets); @@ -142,7 +144,7 @@ public: int GetCloudDbSchema(std::shared_ptr &cloudSchema); - std::pair GetLocalCloudVersion(); + std::pair GetLocalCloudVersion(const DeviceType deviceType); CloudSyncConfig GetCloudSyncConfig() const; diff --git a/frameworks/libs/distributeddb/storage/src/relational/relational_sync_able_storage.cpp b/frameworks/libs/distributeddb/storage/src/relational/relational_sync_able_storage.cpp index df69b83156a..4553e90fafc 100644 --- a/frameworks/libs/distributeddb/storage/src/relational/relational_sync_able_storage.cpp +++ b/frameworks/libs/distributeddb/storage/src/relational/relational_sync_able_storage.cpp @@ -1258,6 +1258,33 @@ int RelationalSyncAbleStorage::PutCloudSyncDataInner(SQLiteSingleVerRelationalSt return errCode; } +int RelationalSyncAbleStorage::DeleteLocalCloudVersionData(const std::string &tableName, const std::string &user) +{ + if (transactionHandle_ == nullptr) { + LOGE(" the transaction has not been started"); + return -E_INVALID_DB; + } + return DeleteLocalCloudVersionDataInner(transactionHandle_, tableName, user); +} + +int RelationalSyncAbleStorage::DeleteLocalCloudVersionDataInner(SQLiteSingleVerRelationalStorageExecutor *handle, + const std::string &tableName, const std::string &user) +{ + TableSchema tableSchema; + int errCode = GetCloudTableSchema(tableName, tableSchema); + if (errCode != E_OK) { + LOGE("Get cloud schema failed when delete cloudVersion data, %d", errCode); + return errCode; + } + RelationalSchemaObject localSchema = GetSchemaInfo(); + handle->SetLocalSchema(localSchema); + TrackerTable trackerTable = storageEngine_->GetTrackerSchema().GetTrackerTable(tableName); + handle->SetLogicDelete(IsCurrentLogicDelete()); + errCode = handle->DeleteLocalCloudVersionData(tableName, tableSchema, trackerTable, user); + handle->SetLogicDelete(false); + return errCode; +} + int RelationalSyncAbleStorage::GetCloudDbSchema(std::shared_ptr &cloudSchema) { std::shared_lock readLock(schemaMgrMutex_); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor.h b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor.h index 085b1e5d7b4..5ef758e88c1 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor.h @@ -128,6 +128,8 @@ public: int PutCloudSyncData(const std::string &tableName, const TableSchema &tableSchema, const TrackerTable &trackerTable, DownloadData &downloadData); + int DeleteLocalCloudVersionData(const std::string &tableName, const TableSchema &tableSchema, + const TrackerTable &trackerTable, const std::string &user); int FillCloudAssetForDownload(const TableSchema &tableSchema, VBucket &vBucket, bool isDownloadSuccess); int DoCleanInner(ClearMode mode, const std::vector &tableNameList, const RelationalSchemaObject &localSchema, std::vector &assets); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor_extend.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor_extend.cpp index 7a095e01173..56297cd0c2e 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor_extend.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor_extend.cpp @@ -516,6 +516,23 @@ int SQLiteSingleVerRelationalStorageExecutor::PutCloudSyncData(const std::string return errCode == E_OK ? ret : errCode; } +int SQLiteSingleVerRelationalStorageExecutor::DeleteLocalCloudVersionData(const std::string &tableName, + const TableSchema &tableSchema, const TrackerTable &trackerTable, const std::string &user) +{ + int errCode = SetLogTriggerStatus(false); + if (errCode != E_OK) { + LOGE("Fail to set log trigger off, %d", errCode); + return errCode; + } + + errCode = DeleteLocalCloudVersionData(tableName, tableSchema, trackerTable, user); + int ret = SetLogTriggerStatus(true); + if (ret != E_OK) { + LOGE("Fail to set log trigger on, %d", ret); + } + return errCode == E_OK ? ret : errCode; +} + int SQLiteSingleVerRelationalStorageExecutor::InsertCloudData(VBucket &vBucket, const TableSchema &tableSchema, const TrackerTable &trackerTable, int64_t dataKey) { diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_executor_utils.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_executor_utils.cpp index 09717e1cf9c..13dba8f9077 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_executor_utils.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_executor_utils.cpp @@ -224,8 +224,8 @@ std::pair SqliteCloudKvExecutorUtils::GetLogInfo(sqlite3 * std::pair res; int &errCode = res.first; std::string keyStr; - errCode = CloudStorageUtils::GetValueFromVBucket(CloudDbConstant::CLOUD_KV_FIELD_KEY, cloudData, keyStr); if (errCode == -E_NOT_FOUND) { + errCode = CloudStorageUtils::GetValueFromVBucket(CloudDbConstant::CLOUD_KV_FIELD_KEY, cloudData, keyStr); errCode = E_OK; } if (errCode != E_OK) { @@ -353,6 +353,12 @@ int SqliteCloudKvExecutorUtils::PutCloudData(sqlite3 *db, bool isMemory, Downloa return errCode; } +int SqliteCloudKvExecutorUtils::DeleteLocalCloudVersionData(sqlite3 *db, bool isMemory, const std::string &user) +{ + int errCode = DeleteCloudVersionData(db, isMemory, user); + return errCode; +} + int SqliteCloudKvExecutorUtils::ExecutePutCloudData(sqlite3 *db, bool isMemory, DownloadData &downloadData, std::map &statisticMap) { @@ -429,6 +435,52 @@ int SqliteCloudKvExecutorUtils::OperateCloudData(sqlite3 *db, bool isMemory, int return StepStmt(logStmt, dataStmt, isMemory); } +int SqliteCloudKvExecutorUtils::DeleteCloudVersionData(sqlite3 *db, bool isMemory, const std::string &user) +{ + sqlite3_stmt *logStmt = nullptr; + int errCode = SQLiteUtils::GetStatement(db, DELETE_CLOUD_SYNC_DATA_LOG, logStmt); + if (errCode != E_OK) { + LOGE("[SqliteCloudKvExecutorUtils] Get cloudVersion delete log statement failed %d", errCode); + return errCode; + } + sqlite3_stmt *dataStmt = nullptr; + errCode = SQLiteUtils::GetStatement(db, DELETE_SYNC_SQL, dataStmt); + if (errCode != E_OK) { + int ret = E_OK; + SQLiteUtils::ResetStatement(logStmt, true, ret); + LOGE("[SqliteCloudKvExecutorUtils] Get cloudVersion delete data statement failed %d reset %d", errCode, ret); + return errCode; + } + ResFinalizer finalizerData([logStmt, dataStmt]() { + sqlite3_stmt *statement = logStmt; + int ret = E_OK; + SQLiteUtils::ResetStatement(statement, true, ret); + if (ret != E_OK) { + LOGW("[SqliteCloudKvExecutorUtils] Reset log stmt failed %d ", ret); + } + statement = dataStmt; + SQLiteUtils::ResetStatement(statement, true, ret); + if (ret != E_OK) { + LOGW("[SqliteCloudKvExecutorUtils] Reset data stmt failed ", ret); + } + }); + std::string hashDev; + errCode = RuntimeContext::GetInstance()->GetLocalIdentity(hashDev); + if (errCode != E_OK) { + return errCode; + } + std::string tempDev = DBCommon::TransferHashString(hashDev); + hashDev = DBCommon::TransferStringToHex(tempDev); + std::string key = CloudDbConstant::CLOUD_VERSION_RECORD_PREFIX_KEY + hashDev; + Key keyVec; + DBCommon::StringToVector(key, keyVec); + errCode = BindDeleteCloudVersionStmt(logStmt, dataStmt, user, keyVec); + if (errCode != E_OK) { + return errCode; + } + return StepStmt(logStmt, dataStmt, isMemory); +} + std::string SqliteCloudKvExecutorUtils::GetOperateDataSql(OpType opType) { switch (opType) { @@ -642,6 +694,29 @@ int SqliteCloudKvExecutorUtils::BindDeleteStmt(sqlite3_stmt *logStmt, sqlite3_st return E_OK; } +int SqliteCloudKvExecutorUtils::BindDeleteCloudVersionStmt(sqlite3_stmt *logStmt, sqlite3_stmt *dataStmt, const std::string &user, + const Bytes &hashKey) +{ + int index = 1; + int errCode = SQLiteUtils::BindTextToStatement(logStmt, index++, user); + + if (errCode != E_OK) { + LOGE("[SqliteCloudKvExecutorUtils] Bind user failed %d when delete cloudVersion log", errCode); + return errCode; + } + errCode = SQLiteUtils::BindBlobToStatement(logStmt, index--, hashKey); + if (errCode != E_OK) { + LOGE("[SqliteCloudKvExecutorUtils] Bind key failed %d when delete cloudVersion log", errCode); + return errCode; + } + errCode = SQLiteUtils::BindBlobToStatement(dataStmt, index, hashKey); + if (errCode != E_OK) { + LOGE("[SqliteCloudKvExecutorUtils] Bind key failed %d when delete cloudVersion data", errCode); + return errCode; + } + return E_OK; +} + int SqliteCloudKvExecutorUtils::BindDataStmt(sqlite3_stmt *dataStmt, const DataItem &dataItem, bool isInsert) { int index = 1; @@ -1085,9 +1160,9 @@ int SqliteCloudKvExecutorUtils::FillCloudVersionRecord(sqlite3 *db, OpType opTyp } std::pair SqliteCloudKvExecutorUtils::GetLocalCloudVersion(sqlite3 *db, bool isMemory, - const std::string &user) + const std::string &user, const DeviceType deviceType) { - auto res = GetLocalCloudVersionInner(db, isMemory, user); + auto res = GetLocalCloudVersionInner(db, isMemory, user, deviceType); if (res.first != E_OK) { LOGE("[SqliteCloudKvExecutorUtils] Get local cloud version failed %d", res.first); } @@ -1095,7 +1170,7 @@ std::pair SqliteCloudKvExecutorUtils::GetLocalCloudVersion(s } std::pair SqliteCloudKvExecutorUtils::GetLocalCloudVersionInner(sqlite3 *db, bool isMemory, - const std::string &user) + const std::string &user, const DeviceType deviceType) { std::pair res; auto &[errCode, syncData] = res; @@ -1114,7 +1189,11 @@ std::pair SqliteCloudKvExecutorUtils::GetLocalCloudVersionIn } }); std::string hashDev; - errCode = RuntimeContext::GetInstance()->GetLocalIdentity(hashDev); + if(deviceType == DeviceType::UUID){ + errCode = RuntimeContext::GetInstance()->GetLocalIdentity(hashDev); + } else if (deviceType == DeviceType::UDID){ + errCode = RuntimeContext::GetInstance()->GetImmutableLocalIdentity(hashDev); + } if (errCode != E_OK) { return res; } @@ -1132,7 +1211,8 @@ std::pair SqliteCloudKvExecutorUtils::GetLocalCloudVersionIn return res; } errCode = GetCloudVersionRecord(isMemory, stmt, syncData); - if (errCode == -E_NOT_FOUND) { + // udid not found, init the default udid version + if (errCode == -E_NOT_FOUND && deviceType == DeviceType::UDID) { InitDefaultCloudVersionRecord(key, tempDev, syncData); errCode = E_OK; } diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_executor_utils.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_executor_utils.h index 017bad5b2d9..5e98c93b354 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_executor_utils.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_executor_utils.h @@ -36,17 +36,17 @@ public: static std::pair GetLogInfo(sqlite3 *db, bool isMemory, const VBucket &cloudData); static int PutCloudData(sqlite3 *db, bool isMemory, DownloadData &downloadData); - + static int DeleteLocalCloudVersionData(sqlite3 *db, bool isMemory, const std::string &user); static int FillCloudLog(const FillGidParam ¶m, OpType opType, const CloudSyncData &data, const std::string &user, CloudUploadRecorder &recorder); - + static std::pair CountCloudData(sqlite3 *db, bool isMemory, const Timestamp ×tamp, const std::string &user, bool forcePush); static std::pair CountAllCloudData(const DBParam ¶m, const std::vector ×tampVec, const std::string &user, bool forcePush, QuerySyncObject &querySyncObject); - static std::pair GetLocalCloudVersion(sqlite3 *db, bool isMemory, const std::string &user); + static std::pair GetLocalCloudVersion(sqlite3 *db, bool isMemory, const std::string &user, const DeviceType deviceType); static int GetCloudVersionFromCloud(sqlite3 *db, bool isMemory, const std::string &user, const std::string &device, std::vector &dataVector); @@ -78,6 +78,8 @@ private: static int ExecutePutCloudData(sqlite3 *db, bool isMemory, DownloadData &downloadData, std::map &statisticMap); + + static int DeleteCloudVersionData(sqlite3 *db, bool isMemory, const std::string &user); static int OperateCloudData(sqlite3 *db, bool isMemory, int index, OpType opType, DownloadData &downloadData); @@ -107,6 +109,9 @@ private: static int BindDeleteStmt(sqlite3_stmt *logStmt, sqlite3_stmt *dataStmt, const std::string &user, DataItem &dataItem); + static int BindDeleteCloudVersionStmt(sqlite3_stmt *logStmt, sqlite3_stmt *dataStmt, const std::string &user, + const Bytes &hashKey); + static int BindDataStmt(sqlite3_stmt *dataStmt, const DataItem &dataItem, bool isInsert); static int BindSyncDataStmt(sqlite3_stmt *dataStmt, const DataItem &dataItem, bool isInsert, int &index); @@ -134,7 +139,7 @@ private: static int FillCloudVersionRecord(sqlite3 *db, OpType opType, const CloudSyncData &data); static std::pair GetLocalCloudVersionInner(sqlite3 *db, bool isMemory, - const std::string &user); + const std::string &user, const DeviceType deviceType); static int GetCloudVersionRecord(bool isMemory, sqlite3_stmt *stmt, CloudSyncData &syncData); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.cpp index ef439b3cea6..ad2537f29f1 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.cpp @@ -260,6 +260,35 @@ int SqliteCloudKvStore::PutCloudSyncData([[gnu::unused]] const std::string &tabl return SqliteCloudKvExecutorUtils::PutCloudData(db, isMemory, downloadData); } +int SqliteCloudKvStore::DeleteLocalCloudVersionData([[gnu::unused]] const std::string &tableName, const std::string &user) +{ + auto [errCode, handle] = storageHandle_->GetStorageExecutor(true); + if (errCode != E_OK) { + LOGE("[SqliteCloudKvStore] get handle failed %d when delete cloud version", errCode); + return errCode; + } + if (handle->IsMemory()) { + errCode = Commit(); + if (errCode != E_OK) { + LOGE("[SqliteCloudKvStore] commit failed %d before delete cloud version", errCode); + storageHandle_->RecycleStorageExecutor(handle); + return errCode; + } + } + sqlite3 *db = nullptr; + (void)handle->GetDbHandle(db); + errCode = SqliteCloudKvExecutorUtils::DeleteLocalCloudVersionData(db, handle->IsMemory(), user); + int ret = E_OK; + if (handle->IsMemory()) { + ret = StartTransaction(TransactType::DEFERRED); + if (ret != E_OK) { + LOGE("[SqliteCloudKvStore] restart transaction failed %d", ret); + } + } + storageHandle_->RecycleStorageExecutor(handle); + return errCode == E_OK ? ret : errCode; +} + int SqliteCloudKvStore::FillCloudLogAndAsset(OpType opType, const CloudSyncData &data, bool fillAsset, bool ignoreEmptyGid) { @@ -443,7 +472,7 @@ int SqliteCloudKvStore::GetCloudVersion(const std::string &device, std::map SqliteCloudKvStore::GetLocalCloudVersion() +std::pair SqliteCloudKvStore::GetLocalCloudVersion(const DeviceType deviceType) { std::pair res; auto &[errCode, data] = res; @@ -458,7 +487,7 @@ std::pair SqliteCloudKvStore::GetLocalCloudVersion() } sqlite3 *db = nullptr; (void)handle->GetDbHandle(db); - std::tie(errCode, data) = SqliteCloudKvExecutorUtils::GetLocalCloudVersion(db, handle->IsMemory(), user_); + std::tie(errCode, data) = SqliteCloudKvExecutorUtils::GetLocalCloudVersion(db, handle->IsMemory(), user_, deviceType); data.isCloudVersionRecord = true; storageHandle_->RecycleStorageExecutor(handle); FillTimestamp(rawSysTime, currentTime, data.insData); @@ -594,4 +623,4 @@ int SqliteCloudKvStore::GetCompensatedSyncQuery(std::vector &sy } return Commit(); } -} \ No newline at end of file +} diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.h index b0b2284974e..e7b7d052559 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.h @@ -64,6 +64,8 @@ public: int PutCloudSyncData(const std::string &tableName, DownloadData &downloadData) override; + int DeleteLocalCloudVersionData(const std::string &tableName, const std::string &user) override; + void TriggerObserverAction(const std::string &deviceName, ChangedData &&changedData, bool isChangedData) override; int FillCloudAssetForDownload(const std::string &tableName, VBucket &asset, bool isDownloadSuccess) override; @@ -88,7 +90,7 @@ public: int GetCloudVersion(const std::string &device, std::map &versionMap); - std::pair GetLocalCloudVersion() override; + std::pair GetLocalCloudVersion(const DeviceType deviceType) override; void SetCloudSyncConfig(const CloudSyncConfig &config); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_sql.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_sql.h index 9b2d1d40c9b..f86d79ad3d9 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_sql.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor_sql.h @@ -48,6 +48,9 @@ namespace DistributedDB { const std::string UPDATE_SYNC_SQL = "UPDATE sync_data SET key=?,value=?,timestamp=?,flag=?,device=?,ori_device=?,w_timestamp=?," \ "modify_time=?,create_time=? WHERE hash_key=?;"; + + const std::string DELETE_SYNC_SQL = + "DELETE FROM sync_data WHERE hash_key=?;"; const std::string INSERT_CACHE_SYNC_SQL = "INSERT OR REPLACE INTO sync_data VALUES(?,?,?,?,?,?,?,?,?);"; @@ -343,6 +346,9 @@ namespace DistributedDB { constexpr const char *UPDATE_CLOUD_SYNC_DATA_LOG = "UPDATE naturalbase_kv_aux_sync_data_log SET cloud_gid=?, " "version=?,cloud_flag=? WHERE userid=? AND hash_key=?"; + constexpr const char *DELETE_CLOUD_SYNC_DATA_LOG = "DELETE FROM naturalbase_kv_aux_sync_data_log WHERE userid=? " + "AND hash_key=?"; + constexpr const char *SET_SYNC_DATA_NO_FORCE_PUSH = "UPDATE sync_data SET flag=flag|0x40 WHERE hash_key=?"; constexpr const char *SET_SYNC_DATA_FORCE_PUSH = "UPDATE sync_data SET flag=flag&(~0x40) WHERE hash_key=?"; diff --git a/frameworks/libs/distributeddb/storage/src/storage_proxy.cpp b/frameworks/libs/distributeddb/storage/src/storage_proxy.cpp index 9c888dadb48..b7a27caef2e 100644 --- a/frameworks/libs/distributeddb/storage/src/storage_proxy.cpp +++ b/frameworks/libs/distributeddb/storage/src/storage_proxy.cpp @@ -311,6 +311,19 @@ int StorageProxy::PutCloudSyncData(const std::string &tableName, DownloadData &d return store_->PutCloudSyncData(tableName, downloadData); } +int StorageProxy::DeleteLocalCloudVersionData(const std::string &tableName) +{ + std::shared_lock readLock(storeMutex_); + if (store_ == nullptr) { + return -E_INVALID_DB; + } + if (!transactionExeFlag_.load()) { + LOGE("the transaction has not been started"); + return -E_TRANSACT_STATE; + } + + return store_->DeleteLocalCloudVersionData(tableName, user_); +} int StorageProxy::CleanCloudData(ClearMode mode, const std::vector &tableNameList, const RelationalSchemaObject &localSchema, std::vector &assets) { @@ -628,13 +641,13 @@ int StorageProxy::GetCloudDbSchema(std::shared_ptr &cloudSchema) return store_->GetCloudDbSchema(cloudSchema); } -std::pair StorageProxy::GetLocalCloudVersion() +std::pair StorageProxy::GetLocalCloudVersion(const DeviceType deviceType) { std::shared_lock readLock(storeMutex_); if (store_ == nullptr) { return {-E_INTERNAL_ERROR, {}}; } - return store_->GetLocalCloudVersion(); + return store_->GetLocalCloudVersion(deviceType); } CloudSyncConfig StorageProxy::GetCloudSyncConfig() const diff --git a/frameworks/libs/distributeddb/syncer/src/cloud/cloud_syncer.cpp b/frameworks/libs/distributeddb/syncer/src/cloud/cloud_syncer.cpp index 9c44bea368d..7fa0e223420 100644 --- a/frameworks/libs/distributeddb/syncer/src/cloud/cloud_syncer.cpp +++ b/frameworks/libs/distributeddb/syncer/src/cloud/cloud_syncer.cpp @@ -1374,7 +1374,6 @@ int CloudSyncer::PreHandleData(VBucket &datum, const std::vector &p std::make_tuple(CloudDbConstant::CURSOR_FIELD, TYPE_INDEX, true), std::make_tuple(CloudDbConstant::SHARING_RESOURCE_FIELD, TYPE_INDEX, false) }; - for (const auto &fieldIndex : fieldAndIndex) { if (datum.find(std::get<0>(fieldIndex)) == datum.end()) { if (!std::get<2>(fieldIndex)) { // 2 is index of mandatory flag diff --git a/frameworks/libs/distributeddb/syncer/src/cloud/cloud_syncer.h b/frameworks/libs/distributeddb/syncer/src/cloud/cloud_syncer.h index 1a0246061f6..11ce65b9bd9 100644 --- a/frameworks/libs/distributeddb/syncer/src/cloud/cloud_syncer.h +++ b/frameworks/libs/distributeddb/syncer/src/cloud/cloud_syncer.h @@ -407,7 +407,9 @@ protected: bool IsNeedLock(const UploadParam ¶m); - int UploadVersionRecordIfNeed(const UploadParam &uploadParam); + int UploadVersionRecordIfNeed(const UploadParam &uploadParam, const std::string &tableName); + + std::pair DeleteLocalCloudVersionIfNeed(const std::string &tableName); std::vector CopyAndClearTaskInfos(); diff --git a/frameworks/libs/distributeddb/syncer/src/cloud/cloud_syncer_extend.cpp b/frameworks/libs/distributeddb/syncer/src/cloud/cloud_syncer_extend.cpp index 81e4e1f16a3..ccfeca05078 100644 --- a/frameworks/libs/distributeddb/syncer/src/cloud/cloud_syncer_extend.cpp +++ b/frameworks/libs/distributeddb/syncer/src/cloud/cloud_syncer_extend.cpp @@ -925,11 +925,11 @@ int CloudSyncer::DoUploadInner(const std::string &tableName, UploadParam &upload break; } } - int ret = UploadVersionRecordIfNeed(uploadParam); + int ret = UploadVersionRecordIfNeed(uploadParam, tableName); return errCode != E_OK ? errCode : ret; } -int CloudSyncer::UploadVersionRecordIfNeed(const UploadParam &uploadParam) +int CloudSyncer::UploadVersionRecordIfNeed(const UploadParam &uploadParam, const std::string &tableName) { if (uploadParam.count == 0) { // no record upload @@ -938,10 +938,9 @@ int CloudSyncer::UploadVersionRecordIfNeed(const UploadParam &uploadParam) if (!cloudDB_.IsExistCloudVersionCallback()) { return E_OK; } - auto [errCode, uploadData] = storageProxy_->GetLocalCloudVersion(); - if (errCode != E_OK) { - return errCode; - } + // read uuid localCloudVersion, if it exist, delete uuid data and log data include local and cloud + // then insert udid data with cloudVersion; if not exist insert udid data with new cloudVersion directly. + auto [errCode, uploadData] = DeleteLocalCloudVersionIfNeed(tableName); bool isInsert = !uploadData.insData.record.empty(); CloudSyncBatch &batchData = isInsert ? uploadData.insData : uploadData.updData; if (batchData.record.empty()) { @@ -949,6 +948,7 @@ int CloudSyncer::UploadVersionRecordIfNeed(const UploadParam &uploadParam) return -E_INTERNAL_ERROR; } std::string oriVersion; + // use udid cloudVersion to get latest version CloudStorageUtils::GetStringFromCloudData(CloudDbConstant::CLOUD_KV_FIELD_VALUE, batchData.record[0], oriVersion); std::string newVersion; std::tie(errCode, newVersion) = cloudDB_.GetCloudVersion(oriVersion); @@ -969,6 +969,37 @@ int CloudSyncer::UploadVersionRecordIfNeed(const UploadParam &uploadParam) return errCode != E_OK ? errCode : ret; } +std::pair CloudSyncer::DeleteLocalCloudVersionIfNeed(const std::string &tableName){ + std::pair res; + auto &[errCode, uploadData] = res; + std::tie(errCode, uploadData) = storageProxy_->GetLocalCloudVersion(DeviceType::UDID); + if (errCode == -E_NOT_FOUND){ + int errCodeUUID; + CloudSyncData uploadDataUUID; + std::tie(errCodeUUID, uploadDataUUID) = storageProxy_->GetLocalCloudVersion(DeviceType::UUID); + if (errCodeUUID == -E_NOT_FOUND) { + errCode = E_OK; + return res; + } + InnerProcessInfo processInfo; + Info info; + uploadData.insData.record[0][CloudDbConstant::CLOUD_KV_FIELD_VALUE] = uploadDataUUID.updData.record[0][CloudDbConstant::CLOUD_KV_FIELD_VALUE]; + + // remove local data and log data + int errCode = storageProxy_->DeleteLocalCloudVersionData(tableName); + if (errCode != E_OK){ + return res; + } + // remove cloud data + errCode = BatchDelete(info, uploadDataUUID, processInfo); + if (errCode != E_OK){ + return res; + } + } + errCode = E_OK; + return res; +} + void CloudSyncer::TagUploadAssets(CloudSyncData &uploadData) { if (!IsDataContainAssets()) { diff --git a/frameworks/libs/distributeddb/test/unittest/common/common/process_communicator_test_stub.h b/frameworks/libs/distributeddb/test/unittest/common/common/process_communicator_test_stub.h index 1a31ba62bc2..b7d834850fa 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/common/process_communicator_test_stub.h +++ b/frameworks/libs/distributeddb/test/unittest/common/common/process_communicator_test_stub.h @@ -70,6 +70,13 @@ public: return info; } + DeviceInfos GetImmutableLocalDeviceInfos() override + { + DeviceInfos info; + info.identifier = "default_udid"; + return info; + } + std::vector GetRemoteOnlineDeviceInfosList() override { std::vector info; diff --git a/frameworks/libs/distributeddb/test/unittest/common/communicator/adapter_stub.cpp b/frameworks/libs/distributeddb/test/unittest/common/communicator/adapter_stub.cpp index 474427efa39..d058153f318 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/communicator/adapter_stub.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/communicator/adapter_stub.cpp @@ -75,6 +75,12 @@ int AdapterStub::GetLocalIdentity(std::string &outTarget) return E_OK; } +int AdapterStub::GetImmutableLocalIdentity(std::string &outTarget) +{ + outTarget = localTarget_; + return E_OK; +} + int AdapterStub::SendBytes(const std::string &dstTarget, const uint8_t *bytes, uint32_t length, uint32_t totalLength) { LOGI("[UT][Stub][Send] Send length=%" PRIu32 " to dstTarget=%s begin.", length, dstTarget.c_str()); diff --git a/frameworks/libs/distributeddb/test/unittest/common/communicator/adapter_stub.h b/frameworks/libs/distributeddb/test/unittest/common/communicator/adapter_stub.h index 9dc785f0cb8..da52bf02970 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/communicator/adapter_stub.h +++ b/frameworks/libs/distributeddb/test/unittest/common/communicator/adapter_stub.h @@ -41,6 +41,8 @@ public: uint32_t GetTimeout(const std::string &target) override; int GetLocalIdentity(std::string &outTarget) override; + int GetImmutableLocalIdentity(std::string &outTarget) override; + int SendBytes(const std::string &dstTarget, const uint8_t *bytes, uint32_t length, uint32_t totalLength) override; int RegBytesReceiveCallback(const BytesReceiveCallback &onReceive, const Finalizer &inOper) override; diff --git a/frameworks/libs/distributeddb/test/unittest/common/communicator/mock_process_communicator.h b/frameworks/libs/distributeddb/test/unittest/common/communicator/mock_process_communicator.h index 3f0e2771a97..6e0bf334b12 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/communicator/mock_process_communicator.h +++ b/frameworks/libs/distributeddb/test/unittest/common/communicator/mock_process_communicator.h @@ -28,6 +28,7 @@ public: MOCK_METHOD0(GetMtuSize, uint32_t()); MOCK_METHOD0(GetTimeout, uint32_t()); MOCK_METHOD0(GetLocalDeviceInfos, DeviceInfos()); + MOCK_METHOD0(GetImmutableLocalDeviceInfos, DeviceInfos()); MOCK_METHOD0(GetRemoteOnlineDeviceInfosList, std::vector()); MOCK_METHOD1(IsSameProcessLabelStartedOnPeerDevice, bool(const DeviceInfos &)); MOCK_METHOD4(CheckAndGetDataHeadInfo, DBStatus(const uint8_t *, uint32_t, uint32_t &, diff --git a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_auto_launch_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_auto_launch_test.cpp index 49a7fc40c3d..e717a3c1634 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_auto_launch_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_auto_launch_test.cpp @@ -111,6 +111,11 @@ namespace { { return E_OK; } + + int GetImmutableLocalIdentity(std::string &outTarget) const override + { + return E_OK; + } private: CommunicatorLackCallback lackCallback_; }; diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/cloud/distributeddb_cloud_kv_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/cloud/distributeddb_cloud_kv_test.cpp index fd42f96e3f8..3137756c143 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/cloud/distributeddb_cloud_kv_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/cloud/distributeddb_cloud_kv_test.cpp @@ -1759,6 +1759,124 @@ HWTEST_F(DistributedDBCloudKvTest, NormalSync041, TestSize.Level1) syncThread.join(); } +/** + * @tc.name: NormalSync042 + * @tc.desc: Test normal sync change uuid key to udid key. + * @tc.type: FUNC + * @tc.require: + * @tc.author: wangxiangdong + */ +HWTEST_F(DistributedDBCloudKvTest, NormalSync042, TestSize.Level0) +{ + std::vector record; + std::vector extend; + VBucket data; + data.insert_or_assign("cur_device", std::string("6p5yJNRXW8xUVPvyJbWDCW4w9TPSP2X6bW7MnPQmJFM=")); + data.insert_or_assign("key", std::string("naturalbase_cloud_version_5c7d3efaab31dafb2b83bc96e46ba07ebc1a83526abda5a9250efc5f5a528180")); + data.insert_or_assign("ori_device", std::string("6p5yJNRXW8xUVPvyJbWDCW4w9TPSP2X6bW7MnPQmJFM=")); + data.insert_or_assign("value", std::string("1")); + record.push_back(data); + VBucket log; + Timestamp now = TimeHelper::GetSysCurrentTime(); + log.insert_or_assign(CloudDbConstant::CREATE_FIELD, + static_cast(now / CloudDbConstant::TEN_THOUSAND)); + log.insert_or_assign(CloudDbConstant::MODIFY_FIELD, + static_cast(now / CloudDbConstant::TEN_THOUSAND)); + log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); + extend.push_back(log); + + ASSERT_EQ(virtualCloudDb_->BatchInsert(CloudDbConstant::CLOUD_KV_TABLE_NAME, std::move(record), extend), DBStatus::OK); + + Key key = {'k'}; + Value expectValue = {'v'}; + ASSERT_EQ(kvDelegatePtrS1_->Put(key, expectValue), OK); + kvDelegatePtrS1_->SetGenCloudVersionCallback([](const std::string &origin) { + LOGW("origin is %s", origin.c_str()); + return origin + "1"; + }); + BlockSync(kvDelegatePtrS1_, OK, g_CloudSyncoption); + for (const auto &table : lastProcess_.tableProcess) { + EXPECT_EQ(table.second.upLoadInfo.total, 1u); + EXPECT_EQ(table.second.upLoadInfo.insertCount, 1u); + } + BlockSync(kvDelegatePtrS2_, OK, g_CloudSyncoption); + for (const auto &table : lastProcess_.tableProcess) { + EXPECT_EQ(table.second.downLoadInfo.total, 3u); // download 3 records + EXPECT_EQ(table.second.downLoadInfo.insertCount, 3u); // download 3 records + } + Value actualValue; + EXPECT_EQ(kvDelegatePtrS2_->Get(key, actualValue), OK); + EXPECT_EQ(actualValue, expectValue); + kvDelegatePtrS1_->SetGenCloudVersionCallback(nullptr); + auto result = kvDelegatePtrS2_->GetCloudVersion(""); + EXPECT_EQ(result.first, OK); + for (const auto &item : result.second) { + EXPECT_EQ(item.second, "1"); + } +} + +/** + * @tc.name: NormalSync043 + * @tc.desc: Test normal sync change uuid key to udid key with 2 old uuid key record. + * @tc.type: FUNC + * @tc.require: + * @tc.author: wangxiangdong + */ +HWTEST_F(DistributedDBCloudKvTest, NormalSync043, TestSize.Level0) +{ + std::vector record; + std::vector extend; + VBucket data; + data.insert_or_assign("cur_device", std::string("6p5yJNRXW8xUVPvyJbWDCW4w9TPSP2X6bW7MnPQmJFM=")); + data.insert_or_assign("key", std::string("naturalbase_cloud_version_5c7d3efaab31dafb2b83bc96e46ba07ebc1a83526abda5a9250efc5f5a528180")); + data.insert_or_assign("ori_device", std::string("6p5yJNRXW8xUVPvyJbWDCW4w9TPSP2X6bW7MnPQmJFM=")); + data.insert_or_assign("value", std::string("1")); + record.push_back(data); + data.insert_or_assign("cur_device", std::string("6p5yJNRXW8xUVPvyJbWDCW4w9TPSP2X6bW7MnPQmJF22")); + data.insert_or_assign("key", std::string("naturalbase_cloud_version_2c7d3efaab31dafb2b83bc96e46ba07ebc1a83526abda5a9250efc5f5a528180")); + data.insert_or_assign("ori_device", std::string("6p5yJNRXW8xUVPvyJbWDCW4w9TPSP2X6bW7MnPQmJF22")); + data.insert_or_assign("value", std::string("1")); + record.push_back(data); + VBucket log; + Timestamp now = TimeHelper::GetSysCurrentTime(); + log.insert_or_assign(CloudDbConstant::CREATE_FIELD, + static_cast(now / CloudDbConstant::TEN_THOUSAND)); + log.insert_or_assign(CloudDbConstant::MODIFY_FIELD, + static_cast(now / CloudDbConstant::TEN_THOUSAND)); + log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); + extend.push_back(log); + extend.push_back(log); + + ASSERT_EQ(virtualCloudDb_->BatchInsert(CloudDbConstant::CLOUD_KV_TABLE_NAME, std::move(record), extend), DBStatus::OK); + + Key key = {'k'}; + Value expectValue = {'v'}; + ASSERT_EQ(kvDelegatePtrS1_->Put(key, expectValue), OK); + kvDelegatePtrS1_->SetGenCloudVersionCallback([](const std::string &origin) { + LOGW("origin is %s", origin.c_str()); + return origin + "1"; + }); + BlockSync(kvDelegatePtrS1_, OK, g_CloudSyncoption); + for (const auto &table : lastProcess_.tableProcess) { + EXPECT_EQ(table.second.upLoadInfo.total, 1u); + EXPECT_EQ(table.second.upLoadInfo.insertCount, 1u); + } + BlockSync(kvDelegatePtrS2_, OK, g_CloudSyncoption); + for (const auto &table : lastProcess_.tableProcess) { + EXPECT_EQ(table.second.downLoadInfo.total, 4u); // download 4 records + EXPECT_EQ(table.second.downLoadInfo.insertCount, 4u); // download 4 records + } + Value actualValue; + EXPECT_EQ(kvDelegatePtrS2_->Get(key, actualValue), OK); + EXPECT_EQ(actualValue, expectValue); + kvDelegatePtrS1_->SetGenCloudVersionCallback(nullptr); + auto result = kvDelegatePtrS2_->GetCloudVersion(""); + EXPECT_EQ(result.first, OK); + for (const auto &item : result.second) { + EXPECT_EQ(item.second, "1"); + } +} + /** * @tc.name: SyncOptionCheck001 * @tc.desc: Test sync without user. diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/cloud/mock_icloud_sync_storage_interface.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/cloud/mock_icloud_sync_storage_interface.h index ec34ffe9f53..367e0e1dccd 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/cloud/mock_icloud_sync_storage_interface.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/cloud/mock_icloud_sync_storage_interface.h @@ -41,6 +41,7 @@ public: MOCK_METHOD1(ReleaseCloudDataToken, int(ContinueToken &)); MOCK_METHOD4(GetInfoByPrimaryKeyOrGid, int(const std::string &, const VBucket &, DataInfoWithLog &, VBucket &)); MOCK_METHOD2(PutCloudSyncData, int(const std::string &, DownloadData &)); + MOCK_METHOD2(DeleteLocalCloudVersionData, int(const std::string &, const std::string &)); MOCK_METHOD3(TriggerObserverAction, void(const std::string &, ChangedData &&, bool)); MOCK_METHOD4(CleanCloudData, int(ClearMode mode, const std::vector &tableNameList, const RelationalSchemaObject &localSchema, std::vector &assets)); diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp index 3d7ed225322..385c6879701 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp @@ -114,6 +114,13 @@ int VirtualCommunicatorAggregator::GetLocalIdentity(std::string &outTarget) cons return getLocalDeviceRet_; } +int VirtualCommunicatorAggregator::GetImmutableLocalIdentity(std::string &outTarget) const +{ + std::lock_guard lock(localDeviceIdMutex_); + outTarget = "DEVICES_B"; + return getLocalDeviceRet_; +} + void VirtualCommunicatorAggregator::OnlineDevice(const std::string &deviceId) const { if (!isEnable_) { diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.h b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.h index 92eff191711..0de5979d9ba 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.h +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.h @@ -45,6 +45,7 @@ public: void RunOnConnectCallback(const std::string &target, bool isConnect); int GetLocalIdentity(std::string &outTarget) const override; + int GetImmutableLocalIdentity(std::string &outTarget) const override; // online a virtual device to the VirtualCommunicator, should call in main thread void OnlineDevice(const std::string &deviceId) const; -- Gitee