From 353c5d6b4ab5ff64691b07ff50869ac4a8ae4f7d Mon Sep 17 00:00:00 2001 From: z30053452 Date: Mon, 30 Jun 2025 10:42:39 +0800 Subject: [PATCH 1/4] onchange fix Signed-off-by: z30053452 --- .../app/src/kvstore_meta_manager.cpp | 4 +- .../service/kvdb/kvdb_observer_proxy.cpp | 50 +++++++++++++++---- .../service/kvdb/kvdb_observer_proxy.h | 1 + .../service/test/kvdb_service_test.cpp | 25 ++++++++++ 4 files changed, 67 insertions(+), 13 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index 1472fdd59..337d571be 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -652,8 +652,8 @@ void KvStoreMetaManager::CheckMetaDeviceId() if (deviceMeta.newUuid != localUuid) { UpdateStoreMetaData(localUuid, deviceMeta.newUuid); UpdateMetaDatas(localUuid, deviceMeta.newUuid); - ZLOGI("meta changed! cur uuid:%{public}s, old uuid:%{public}s", Anonymous::Change(localUuid).c_str(), - Anonymous::Change(deviceMeta.newUuid).c_str()); + ZLOGI("meta changed! curruuid:%{public}s, olduuid:%{public}s", Anonymous::Change(deviceMeta.newUuid).c_str(), + Anonymous::Change(deviceMeta.oldUuid).c_str()); deviceMeta.newUuid = localUuid; MetaDataManager::GetInstance().SaveMeta(deviceMeta.GetKey(), deviceMeta, true); } diff --git a/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.cpp b/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.cpp index 3eb5413ef..e2c6ebfb4 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.cpp @@ -36,6 +36,22 @@ KVDBObserverProxy::KVDBObserverProxy(const sptr &impl) : IRemoteP { } +int64_t KVDBObserverProxy::CalTotalSize(const ChangeNotification &changeNotification, MessageParcel &data) +{ + const int errorResult = -1; + int64_t insertSize = ITypesUtil::GetTotalSize(changeNotification.GetInsertEntries()); + int64_t updateSize = ITypesUtil::GetTotalSize(changeNotification.GetUpdateEntries()); + int64_t deleteSize = ITypesUtil::GetTotalSize(changeNotification.GetDeleteEntries()); + int64_t totalSize = insertSize + updateSize + deleteSize; + if (insertSize < 0 || updateSize < 0 || deleteSize < 0 || !data.WriteInt64(totalSize)) { + ZLOGE("Write ChangeNotification buffer size to parcel failed. I(%" PRId64 ") U(%" PRId64 ") D(%" PRId64 ")", + insertSize, updateSize, deleteSize); + return errorResult; + } + ZLOGD("I(%" PRId64 ") U(%" PRId64 ") D(%" PRId64 ") T(%" PRId64 ")", insertSize, updateSize, deleteSize, totalSize); + return totalSize; +} + void KVDBObserverProxy::OnChange(const ChangeNotification &changeNotification) { MessageParcel data; @@ -44,25 +60,37 @@ void KVDBObserverProxy::OnChange(const ChangeNotification &changeNotification) ZLOGE("Write descriptor failed"); return; } - int64_t insertSize = ITypesUtil::GetTotalSize(changeNotification.GetInsertEntries()); - int64_t updateSize = ITypesUtil::GetTotalSize(changeNotification.GetUpdateEntries()); - int64_t deleteSize = ITypesUtil::GetTotalSize(changeNotification.GetDeleteEntries()); - int64_t totalSize = insertSize + updateSize + deleteSize + sizeof(uint32_t); - if (insertSize < 0 || updateSize < 0 || deleteSize < 0 || !data.WriteInt32(totalSize)) { - ZLOGE("Write ChangeNotification buffer size to parcel failed."); + int64_t totalSize = CalTotalSize(changeNotification, data); + if (totalSize == -1) { return; } - ZLOGD("I(%" PRId64 ") U(%" PRId64 ") D(%" PRId64 ") T(%" PRId64 ")", insertSize, updateSize, deleteSize, totalSize); if (totalSize < SWITCH_RAW_DATA_SIZE) { if (!ITypesUtil::Marshal(data, changeNotification)) { ZLOGW("Write ChangeNotification to parcel failed."); return; } } else { - if (!ITypesUtil::Marshal(data, changeNotification.GetDeviceId(), uint32_t(changeNotification.IsClear())) || - !ITypesUtil::MarshalToBuffer(changeNotification.GetInsertEntries(), insertSize, data) || - !ITypesUtil::MarshalToBuffer(changeNotification.GetUpdateEntries(), updateSize, data) || - !ITypesUtil::MarshalToBuffer(changeNotification.GetDeleteEntries(), deleteSize, data)) { + if (!ITypesUtil::Marshal(data, changeNotification.GetDeviceId(), uint32_t(changeNotification.IsClear()))) { + ZLOGE("write deviceId to parcel failed, devId:%{public}s, clear:%{public}d", + changeNotification.GetDeviceId().c_str(), changeNotification.IsClear()); + return; + } + if (!data.WriteInt32(changeNotification.GetInsertEntries().size()) || + !data.WriteInt32(changeNotification.GetUpdateEntries().size()) || + !data.WriteInt32(changeNotification.GetDeleteEntries().size())) { + ZLOGE("write change size to parcel failed, insert:%{public}zu, update:%{public}zu, delete:%{public}zu", + changeNotification.GetInsertEntries().size(), changeNotification.GetUpdateEntries().size(), + changeNotification.GetDeleteEntries().size()); + return; + } + std::vector totalEntries; + totalEntries.insert(totalEntries.end(), changeNotification.GetInsertEntries().begin(), + changeNotification.GetInsertEntries().end()); + totalEntries.insert(totalEntries.end(), changeNotification.GetUpdateEntries().begin(), + changeNotification.GetUpdateEntries().end()); + totalEntries.insert(totalEntries.end(), changeNotification.GetDeleteEntries().begin(), + changeNotification.GetDeleteEntries().end()); + if (!ITypesUtil::MarshalToBuffer(totalEntries, totalSize, data)) { ZLOGE("WriteChangeList to Parcel by buffer failed"); return; } diff --git a/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.h b/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.h index 9a706fd26..35d619dbc 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.h +++ b/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.h @@ -32,6 +32,7 @@ public: void OnChange(const DataOrigin &origin, Keys &&keys) override; private: static inline BrokerDelegator delegator_; + int64_t CalTotalSize(const ChangeNotification &changeNotification, MessageParcel &data); }; } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/service/test/kvdb_service_test.cpp b/services/distributeddataservice/service/test/kvdb_service_test.cpp index 22689b6d9..f0375321c 100644 --- a/services/distributeddataservice/service/test/kvdb_service_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_service_test.cpp @@ -441,6 +441,31 @@ HWTEST_F(KVDBWatcherTest, OnChange002, TestSize.Level0) EXPECT_EQ(result, GeneralError::E_NOT_INIT); } +/** +* @tc.name: OnChange003 +* @tc.desc: OnChange test function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: zhiyihang + */ +HWTEST_F(KVDBWatcherTest, OnChange003, TestSize.Level0) +{ + GeneralWatcher::Origin origin; + origin.store = "store"; + GeneralWatcher::Fields fields; + GeneralWatcher::ChangeData datas; + std::string testStr = "bQ6#xq@L9!zP7m$sV1%kY*4nC&jF(0lR)oU2^aH5-iT3+eW8=gD9;hG:0bJ1,K4?lN6.M7|pO8\\qS9/rA" + + "0~tB1\uX2'vZ3[wE4]yI5{zF6`cK7Q6#xq@L9!zP7m$sV1%kY*4nC&jF(0lR)oU2^aH5-iT3+eW8=gD9;hG:0bJ1,K4?lN6.M7|s"; + datas["store"][OP_DELETE].push_back({std::string(testStr)}); + std::shared_ptr watcher = std::make_shared(); + sptr observer = nullptr; + watcher->SetObserver(observer); + EXPECT_EQ(watcher->observer_, nullptr); + auto result = watcher->OnChange(origin, fields, std::move(datas)); + EXPECT_EQ(result, GeneralError::E_NOT_INIT); +} + + /** * @tc.name: ConvertToEntries * @tc.desc: ConvertToEntries test the return result of input with different values. -- Gitee From 47316e11b3a9d6bb6a641e36a17cb72adf8e59da Mon Sep 17 00:00:00 2001 From: z30053452 Date: Mon, 30 Jun 2025 11:39:38 +0800 Subject: [PATCH 2/4] code fix Signed-off-by: z30053452 --- .../distributeddataservice/app/src/kvstore_meta_manager.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index 337d571be..005cfc6da 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -652,8 +652,8 @@ void KvStoreMetaManager::CheckMetaDeviceId() if (deviceMeta.newUuid != localUuid) { UpdateStoreMetaData(localUuid, deviceMeta.newUuid); UpdateMetaDatas(localUuid, deviceMeta.newUuid); - ZLOGI("meta changed! curruuid:%{public}s, olduuid:%{public}s", Anonymous::Change(deviceMeta.newUuid).c_str(), - Anonymous::Change(deviceMeta.oldUuid).c_str()); + ZLOGI("meta changed! curruuid:%{public}s, localUuid:%{public}s", Anonymous::Change(deviceMeta.newUuid).c_str(), + localUuid.c_str()); deviceMeta.newUuid = localUuid; MetaDataManager::GetInstance().SaveMeta(deviceMeta.GetKey(), deviceMeta, true); } -- Gitee From 4e891ae11faf08aa2620bcdf7e7df7e560da6126 Mon Sep 17 00:00:00 2001 From: z30053452 Date: Mon, 30 Jun 2025 14:20:43 +0800 Subject: [PATCH 3/4] code fix Signed-off-by: z30053452 --- .../distributeddataservice/service/test/kvdb_service_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/test/kvdb_service_test.cpp b/services/distributeddataservice/service/test/kvdb_service_test.cpp index f0375321c..b2cbf6771 100644 --- a/services/distributeddataservice/service/test/kvdb_service_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_service_test.cpp @@ -455,7 +455,7 @@ HWTEST_F(KVDBWatcherTest, OnChange003, TestSize.Level0) GeneralWatcher::Fields fields; GeneralWatcher::ChangeData datas; std::string testStr = "bQ6#xq@L9!zP7m$sV1%kY*4nC&jF(0lR)oU2^aH5-iT3+eW8=gD9;hG:0bJ1,K4?lN6.M7|pO8\\qS9/rA" + - "0~tB1\uX2'vZ3[wE4]yI5{zF6`cK7Q6#xq@L9!zP7m$sV1%kY*4nC&jF(0lR)oU2^aH5-iT3+eW8=gD9;hG:0bJ1,K4?lN6.M7|s"; + "0~tB1bQ6#xq@L9!zP7m$sV1%kY*4nC&jF(0lR)oU2^aH5-iT3+eW8=gD9;hG:0bJ1,K4?lN6.M7|pO8\\qS9/rAtesttesttesttesttest"; datas["store"][OP_DELETE].push_back({std::string(testStr)}); std::shared_ptr watcher = std::make_shared(); sptr observer = nullptr; -- Gitee From be4d9dc095817a3a059219ad030b092a609aa08e Mon Sep 17 00:00:00 2001 From: z30053452 Date: Mon, 30 Jun 2025 15:36:19 +0800 Subject: [PATCH 4/4] code fix Signed-off-by: z30053452 --- .../distributeddataservice/service/test/kvdb_service_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/test/kvdb_service_test.cpp b/services/distributeddataservice/service/test/kvdb_service_test.cpp index b2cbf6771..face05a54 100644 --- a/services/distributeddataservice/service/test/kvdb_service_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_service_test.cpp @@ -454,7 +454,7 @@ HWTEST_F(KVDBWatcherTest, OnChange003, TestSize.Level0) origin.store = "store"; GeneralWatcher::Fields fields; GeneralWatcher::ChangeData datas; - std::string testStr = "bQ6#xq@L9!zP7m$sV1%kY*4nC&jF(0lR)oU2^aH5-iT3+eW8=gD9;hG:0bJ1,K4?lN6.M7|pO8\\qS9/rA" + + std::string testStr = "bQ6#xq@L9!zP7m$sV1%kY*4nC&jF(0lR)oU2^aH5-iT3+eW8=gD9;hG:0bJ1,K4?lN6.M7|pO8\\qS9/rA" "0~tB1bQ6#xq@L9!zP7m$sV1%kY*4nC&jF(0lR)oU2^aH5-iT3+eW8=gD9;hG:0bJ1,K4?lN6.M7|pO8\\qS9/rAtesttesttesttesttest"; datas["store"][OP_DELETE].push_back({std::string(testStr)}); std::shared_ptr watcher = std::make_shared(); -- Gitee