From 32b7cb46c944d2c9520ccb6ff483e9f996b6f2f0 Mon Sep 17 00:00:00 2001 From: wanghuajian-6 Date: Tue, 15 Apr 2025 14:51:49 +0800 Subject: [PATCH 01/11] fix clear bug Signed-off-by: wanghuajian-6 --- .../service/object/include/object_manager.h | 5 ++--- .../service/object/src/object_manager.cpp | 9 ++++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/service/object/include/object_manager.h b/services/distributeddataservice/service/object/include/object_manager.h index 8fdaea708..b1e69d0c1 100644 --- a/services/distributeddataservice/service/object/include/object_manager.h +++ b/services/distributeddataservice/service/object/include/object_manager.h @@ -196,10 +196,9 @@ private: { return appId + SEPERATOR + sessionId + SEPERATOR; }; - inline std::string GetMetaUserIdKey(const std::string &userId, const std::string &appId) + inline std::string GetMetaUserIdKey(const std::string &appId) { - return std::string(USERID) + SEPERATOR + userId + SEPERATOR + appId + SEPERATOR - + DmAdaper::GetInstance().GetLocalDevice().udid; + return std::string(USERID) + SEPERATOR + appId + SEPERATOR + DmAdaper::GetInstance().GetLocalDevice().udid; }; std::recursive_mutex kvStoreMutex_; std::shared_ptr kvStoreDelegateManager_ = nullptr; diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index c86d28e1b..00bf53435 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -297,7 +297,7 @@ int32_t ObjectStoreManager::Clear() } std::vector metaData; std::string appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); - std::string metaKey = GetMetaUserIdKey(userId, appId); + std::string metaKey = GetMetaUserIdKey(appId); if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(metaKey, metaData, true)) { ZLOGE("no store of %{public}s", appId.c_str()); return OBJECT_STORE_NOT_FOUND; @@ -338,11 +338,10 @@ int32_t ObjectStoreManager::DeleteByAppId(const std::string &appId, int32_t user appId.c_str(), user); } Close(); - std::string userId = std::to_string(user); - std::string metaKey = GetMetaUserIdKey(userId, appId); + std::string metaKey = GetMetaUserIdKey(appId); auto status = DistributedData::MetaDataManager::GetInstance().DelMeta(metaKey, true); if (!status) { - ZLOGE("Delete meta failed, userId: %{public}s, appId: %{public}s", userId.c_str(), appId.c_str()); + ZLOGE("Delete meta failed, userId: %{public}d, appId: %{public}s", user, appId.c_str()); } return result; } @@ -1104,7 +1103,7 @@ void ObjectStoreManager::SaveUserToMeta() userMeta.storeId = DistributedObject::ObjectCommon::OBJECTSTORE_DB_STOREID; userMeta.user = userId; userMeta.storeType = ObjectDistributedType::OBJECT_SINGLE_VERSION; - std::string userMetaKey = GetMetaUserIdKey(userId, appId); + std::string userMetaKey = GetMetaUserIdKey(appId); auto saved = DistributedData::MetaDataManager::GetInstance().SaveMeta(userMetaKey, userMeta, true); if (!saved) { ZLOGE("userMeta save failed"); -- Gitee From c22d8ee255f8c498a93b6825233e07b9bf8cdaa2 Mon Sep 17 00:00:00 2001 From: wanghuajian-6 Date: Tue, 15 Apr 2025 20:16:01 +0800 Subject: [PATCH 02/11] fix clear bug Signed-off-by: wanghuajian-6 --- .../distributeddataservice/framework/BUILD.gn | 1 + .../include/metadata/object_user_meta_data.h | 37 ++++++++++++ .../metadata/object_user_meta_data.cpp | 38 ++++++++++++ .../service/object/include/object_manager.h | 6 +- .../service/object/src/object_manager.cpp | 60 +++++++++++-------- .../object/src/object_service_impl.cpp | 1 + 6 files changed, 116 insertions(+), 27 deletions(-) create mode 100644 services/distributeddataservice/framework/include/metadata/object_user_meta_data.h create mode 100644 services/distributeddataservice/framework/metadata/object_user_meta_data.cpp diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index 344f08426..a1ba24c67 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -92,6 +92,7 @@ ohos_shared_library("distributeddatasvcfwk") { "metadata/matrix_meta_data.cpp", "metadata/meta_data.cpp", "metadata/meta_data_manager.cpp", + "metadata/object_user_meta_data.cpp", "metadata/secret_key_meta_data.cpp", "metadata/store_debug_info.cpp", "metadata/store_meta_data.cpp", diff --git a/services/distributeddataservice/framework/include/metadata/object_user_meta_data.h b/services/distributeddataservice/framework/include/metadata/object_user_meta_data.h new file mode 100644 index 000000000..91e81b868 --- /dev/null +++ b/services/distributeddataservice/framework/include/metadata/object_user_meta_data.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISTRIBUTEDDATAMGR_OBJECT_USER_META_DATA_H +#define DISTRIBUTEDDATAMGR_OBJECT_USER_META_DATA_H +#include +#include + +#include "serializable/serializable.h" +namespace OHOS::DistributedData { +class API_EXPORT ObjectUserMetaData final : public Serializable { +public: + std::string userId; + + API_EXPORT bool Marshal(json &node) const override; + API_EXPORT bool Unmarshal(const json &node) override; + API_EXPORT ObjectUserMetaData(); + + API_EXPORT static std::string GetKey(); + +private: + static constexpr const char *KEY_PREFIX = "ObjectUserMetaData"; +}; +} // namespace OHOS::DistributedData +#endif // DISTRIBUTEDDATAMGR_OBJECT_USER_META_DATA_H \ No newline at end of file diff --git a/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp b/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp new file mode 100644 index 000000000..9324ebc28 --- /dev/null +++ b/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "metadata/object_user_meta_data.h" + +#include "utils/constant.h" + +namespace OHOS::DistributedData { + +ObjectUserMetaData::ObjectUserMetaData() +{ +} +bool ObjectUserMetaData::Marshal(json &node) const +{ + return SetValue(node[GET_NAME(userId)], userId); +} + +bool ObjectUserMetaData::Unmarshal(const json &node) +{ + return GetValue(node, GET_NAME(userId), userId); +} + +std::string ObjectUserMetaData::GetKey() +{ + return KEY_PREFIX; +} +} \ No newline at end of file diff --git a/services/distributeddataservice/service/object/include/object_manager.h b/services/distributeddataservice/service/object/include/object_manager.h index b1e69d0c1..f1e38aa20 100644 --- a/services/distributeddataservice/service/object/include/object_manager.h +++ b/services/distributeddataservice/service/object/include/object_manager.h @@ -89,6 +89,7 @@ public: sptr callback, uint32_t tokenId); void SetData(const std::string &dataDir, const std::string &userId); int32_t Clear(); + int32_t ClearOldUserMeta(); int32_t DeleteByAppId(const std::string &appId, int32_t user); void RegisterRemoteCallback(const std::string &bundleName, const std::string &sessionId, pid_t pid, uint32_t tokenId, @@ -196,9 +197,10 @@ private: { return appId + SEPERATOR + sessionId + SEPERATOR; }; - inline std::string GetMetaUserIdKey(const std::string &appId) + inline std::string GetMetaUserIdKey(const std::string &userId, const std::string &appId) { - return std::string(USERID) + SEPERATOR + appId + SEPERATOR + DmAdaper::GetInstance().GetLocalDevice().udid; + return std::string(USERID) + SEPERATOR + userId + SEPERATOR + appId + SEPERATOR + + DmAdaper::GetInstance().GetLocalDevice().udid; }; std::recursive_mutex kvStoreMutex_; std::shared_ptr kvStoreDelegateManager_ = nullptr; diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 00bf53435..6c4987a8c 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -29,6 +29,7 @@ #include "kvstore_utils.h" #include "log_print.h" #include "metadata/meta_data_manager.h" +#include "metadata/object_user_meta_data.h" #include "metadata/store_meta_data.h" #include "object_dms_handler.h" #include "object_radar_reporter.h" @@ -295,23 +296,15 @@ int32_t ObjectStoreManager::Clear() if (userId.empty()) { return OBJECT_INNER_ERROR; } - std::vector metaData; - std::string appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); - std::string metaKey = GetMetaUserIdKey(appId); - if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(metaKey, metaData, true)) { - ZLOGE("no store of %{public}s", appId.c_str()); + DistributedData::ObjectUserMetaData userMeta; + std::string userMetaKey = DistributedData::ObjectUserMetaData::GetKey(); + if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(userMetaKey, userMeta, true)) { + ZLOGE("no meta of userId:%{public}s", userId.c_str()); return OBJECT_STORE_NOT_FOUND; } - for (const auto &storeMeta : metaData) { - if (storeMeta.storeType < StoreMetaData::StoreType::STORE_OBJECT_BEGIN - || storeMeta.storeType > StoreMetaData::StoreType::STORE_OBJECT_END) { - continue; - } - if (storeMeta.user == userId) { - ZLOGI("user is same, not need to change, mate user:%{public}s::user:%{public}s.", - storeMeta.user.c_str(), userId.c_str()); - return OBJECT_SUCCESS; - } + if (userMeta.userId == userId) { + ZLOGI("user is same, not need to change, user:%{public}s.", userId.c_str()); + return OBJECT_SUCCESS; } ZLOGI("user changed, need to clear, userId:%{public}s", userId.c_str()); int32_t result = Open(); @@ -324,6 +317,30 @@ int32_t ObjectStoreManager::Clear() return result; } +int32_t ObjectStoreManager::ClearOldUserMeta() +{ + std::string userId = GetCurrentUser(); + if (userId.empty()) { + ZLOGI("get userId error, one minute again"); + executors_->Schedule(std::chrono::minutes(INTERVAL), std::bind(&ObjectStoreManager::ClearOldUserMeta, this)); + return OBJECT_INNER_ERROR; + } + ObjectUserMetaData userMetaData; + userMetaData.userId = userId; + if (!DistributedData::MetaDataManager::GetInstance().SaveMeta(DistributedData::ObjectUserMetaData::GetKey(), + userMetaData, true)) { + ZLOGE("save meta error, userId:%{public}s", userId.c_str()); + return OBJECT_INNER_ERROR; + } + std::string appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); + std::string metaKey = GetMetaUserIdKey(userId, appId); + if (!DistributedData::MetaDataManager::GetInstance().DelMeta(metaKey, true)) { + ZLOGE("delete old meta error, userId:%{public}s", userId.c_str()); + return OBJECT_INNER_ERROR; + } + return OBJECT_SUCCESS; +} + int32_t ObjectStoreManager::DeleteByAppId(const std::string &appId, int32_t user) { int32_t result = Open(); @@ -338,11 +355,6 @@ int32_t ObjectStoreManager::DeleteByAppId(const std::string &appId, int32_t user appId.c_str(), user); } Close(); - std::string metaKey = GetMetaUserIdKey(appId); - auto status = DistributedData::MetaDataManager::GetInstance().DelMeta(metaKey, true); - if (!status) { - ZLOGE("Delete meta failed, userId: %{public}d, appId: %{public}s", user, appId.c_str()); - } return result; } @@ -1099,11 +1111,9 @@ void ObjectStoreManager::SaveUserToMeta() return; } std::string appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); - StoreMetaData userMeta; - userMeta.storeId = DistributedObject::ObjectCommon::OBJECTSTORE_DB_STOREID; - userMeta.user = userId; - userMeta.storeType = ObjectDistributedType::OBJECT_SINGLE_VERSION; - std::string userMetaKey = GetMetaUserIdKey(appId); + DistributedData::ObjectUserMetaData userMeta; + userMeta.userId = userId; + auto userMetaKey = DistributedData::ObjectUserMetaData::GetKey(); auto saved = DistributedData::MetaDataManager::GetInstance().SaveMeta(userMetaKey, userMeta, true); if (!saved) { ZLOGE("userMeta save failed"); diff --git a/services/distributeddataservice/service/object/src/object_service_impl.cpp b/services/distributeddataservice/service/object/src/object_service_impl.cpp index c09b58286..553d1ace8 100644 --- a/services/distributeddataservice/service/object/src/object_service_impl.cpp +++ b/services/distributeddataservice/service/object/src/object_service_impl.cpp @@ -139,6 +139,7 @@ int32_t ObjectServiceImpl::OnInitialize() StoreMetaData saveMeta; SaveMetaData(saveMeta, std::to_string(userId), accountId); ObjectStoreManager::GetInstance()->SetData(saveMeta.dataDir, std::to_string(userId)); + ObjectStoreManager::GetInstance()->ClearOldUserMeta(); RegisterObjectServiceInfo(); RegisterHandler(); ObjectDmsHandler::GetInstance().RegisterDmsEvent(); -- Gitee From f706ae0e4bf0e97862b1e570fdb5da3ba15a3d10 Mon Sep 17 00:00:00 2001 From: wanghuajian-6 Date: Tue, 15 Apr 2025 21:20:24 +0800 Subject: [PATCH 03/11] fix clear bug Signed-off-by: wanghuajian-6 --- .../framework/test/meta_data_test.cpp | 14 ++++++++++++++ .../service/object/src/object_manager.cpp | 3 +-- .../service/test/object_manager_test.cpp | 12 ++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/framework/test/meta_data_test.cpp b/services/distributeddataservice/framework/test/meta_data_test.cpp index 952990503..58359051e 100644 --- a/services/distributeddataservice/framework/test/meta_data_test.cpp +++ b/services/distributeddataservice/framework/test/meta_data_test.cpp @@ -25,6 +25,7 @@ #include "metadata/matrix_meta_data.h" #include "metadata/meta_data.h" #include "metadata/meta_data_manager.h" +#include "metadata/object_user_meta_data.h" #include "metadata/secret_key_meta_data.h" #include "metadata/store_meta_data.h" #include "metadata/store_meta_data_local.h" @@ -994,4 +995,17 @@ HWTEST_F(ServiceMetaDataTest, GetKey, TestSize.Level1) std::string prefix = metaData.GetKey(); EXPECT_EQ(prefix, expectedPrefix); } + +/** +* @tc.name: ObjectUserMetaDataGetKey +* @tc.desc: ObjectUserMetaDataGetKey +* @tc.type: FUNC +*/ +HWTEST_F(ServiceMetaDataTest, ObjectUserMetaDataGetKey, TestSize.Level1) +{ + ObjectUserMetaData metaData; + std::string expectedPrefix = "ObjectUserMetaData"; + std::string prefix = metaData.GetKey(); + EXPECT_EQ(prefix, expectedPrefix); +} } // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 6c4987a8c..049be5d40 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -297,8 +297,7 @@ int32_t ObjectStoreManager::Clear() return OBJECT_INNER_ERROR; } DistributedData::ObjectUserMetaData userMeta; - std::string userMetaKey = DistributedData::ObjectUserMetaData::GetKey(); - if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(userMetaKey, userMeta, true)) { + if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(userMeta.GetKey(), userMeta, true)) { ZLOGE("no meta of userId:%{public}s", userId.c_str()); return OBJECT_STORE_NOT_FOUND; } diff --git a/services/distributeddataservice/service/test/object_manager_test.cpp b/services/distributeddataservice/service/test/object_manager_test.cpp index dfc4bf24a..cfed2eacf 100644 --- a/services/distributeddataservice/service/test/object_manager_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_test.cpp @@ -1086,4 +1086,16 @@ HWTEST_F(ObjectManagerTest, GetObjectData002, TestSize.Level1) EXPECT_NE(saveInfo.bundleName, bundleName); EXPECT_FALSE(hasAsset); } + +/** +* @tc.name: ClearOldUserMeta001 +* @tc.desc: test clear old user meta. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerTest, ClearOldUserMeta001, TestSize.Level1) +{ + auto manager = ObjectStoreManager::GetInstance(); + auto status = manager->ClearOldUserMeta(); + ASSERT_EQ(status, DistributedObject::OBJECT_SUCCESS); +} } // namespace OHOS::Test -- Gitee From 92470f30136ecea4df7b549465338cf44a139f50 Mon Sep 17 00:00:00 2001 From: wanghuajian-6 Date: Thu, 17 Apr 2025 20:07:36 +0800 Subject: [PATCH 04/11] fix clear bug and std::bind check Signed-off-by: wanghuajian-6 --- .../include/metadata/object_user_meta_data.h | 2 -- .../metadata/object_user_meta_data.cpp | 2 -- .../service/object/src/object_manager.cpp | 23 +++++++++++-------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/services/distributeddataservice/framework/include/metadata/object_user_meta_data.h b/services/distributeddataservice/framework/include/metadata/object_user_meta_data.h index 91e81b868..cd2dd38e4 100644 --- a/services/distributeddataservice/framework/include/metadata/object_user_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/object_user_meta_data.h @@ -15,8 +15,6 @@ #ifndef DISTRIBUTEDDATAMGR_OBJECT_USER_META_DATA_H #define DISTRIBUTEDDATAMGR_OBJECT_USER_META_DATA_H -#include -#include #include "serializable/serializable.h" namespace OHOS::DistributedData { diff --git a/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp b/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp index 9324ebc28..e4471f745 100644 --- a/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp +++ b/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp @@ -14,8 +14,6 @@ */ #include "metadata/object_user_meta_data.h" -#include "utils/constant.h" - namespace OHOS::DistributedData { ObjectUserMetaData::ObjectUserMetaData() diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 049be5d40..797a7cace 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -292,20 +292,21 @@ int32_t ObjectStoreManager::Retrieve( int32_t ObjectStoreManager::Clear() { ZLOGI("enter"); + DistributedData::ObjectUserMetaData userMeta; + if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(userMeta.GetKey(), userMeta, true)) { + ZLOGI("no object user meta. don't need clear"); + return OBJECT_SUCCESS; + } std::string userId = GetCurrentUser(); if (userId.empty()) { + ZLOGE("no user error"); return OBJECT_INNER_ERROR; } - DistributedData::ObjectUserMetaData userMeta; - if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(userMeta.GetKey(), userMeta, true)) { - ZLOGE("no meta of userId:%{public}s", userId.c_str()); - return OBJECT_STORE_NOT_FOUND; - } if (userMeta.userId == userId) { - ZLOGI("user is same, not need to change, user:%{public}s.", userId.c_str()); + ZLOGI("user is same, don't need clear, user:%{public}s.", userId.c_str()); return OBJECT_SUCCESS; } - ZLOGI("user changed, need to clear, userId:%{public}s", userId.c_str()); + ZLOGI("user changed, need clear, userId:%{public}s", userId.c_str()); int32_t result = Open(); if (result != OBJECT_SUCCESS) { ZLOGE("Open failed, errCode = %{public}d", result); @@ -321,7 +322,9 @@ int32_t ObjectStoreManager::ClearOldUserMeta() std::string userId = GetCurrentUser(); if (userId.empty()) { ZLOGI("get userId error, one minute again"); - executors_->Schedule(std::chrono::minutes(INTERVAL), std::bind(&ObjectStoreManager::ClearOldUserMeta, this)); + executors_->Schedule(std::chrono::minutes(INTERVAL), [this]() { + ClearOldUserMeta(); + }); return OBJECT_INNER_ERROR; } ObjectUserMetaData userMetaData; @@ -1121,7 +1124,9 @@ void ObjectStoreManager::SaveUserToMeta() void ObjectStoreManager::CloseAfterMinute() { - executors_->Schedule(std::chrono::minutes(INTERVAL), std::bind(&ObjectStoreManager::Close, this)); + executors_->Schedule(std::chrono::minutes(INTERVAL), [this]() { + Close(); + }); } void ObjectStoreManager::SetThreadPool(std::shared_ptr executors) -- Gitee From 9c80066d5a32ab6a6ff8527f5a902d6e1fe747af Mon Sep 17 00:00:00 2001 From: wanghuajian-6 Date: Thu, 17 Apr 2025 20:10:35 +0800 Subject: [PATCH 05/11] fix clear bug and std::bind check Signed-off-by: wanghuajian-6 --- .../service/object/src/object_manager.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 797a7cace..6dfceec2c 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -1115,10 +1115,10 @@ void ObjectStoreManager::SaveUserToMeta() std::string appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); DistributedData::ObjectUserMetaData userMeta; userMeta.userId = userId; - auto userMetaKey = DistributedData::ObjectUserMetaData::GetKey(); - auto saved = DistributedData::MetaDataManager::GetInstance().SaveMeta(userMetaKey, userMeta, true); + auto saved = DistributedData::MetaDataManager::GetInstance().SaveMeta( + DistributedData::ObjectUserMetaData::GetKey(), userMeta, true); if (!saved) { - ZLOGE("userMeta save failed"); + ZLOGE("userMeta save failed, userId:%{public}s", userId.c_str()); } } -- Gitee From e75128c5a233351e8f2e11e0ef6dd2c6a271232f Mon Sep 17 00:00:00 2001 From: wanghuajian-6 Date: Thu, 17 Apr 2025 20:49:50 +0800 Subject: [PATCH 06/11] fix clear bug and std::bind check Signed-off-by: wanghuajian-6 --- .../service/object/src/object_manager.cpp | 4 ++-- .../service/object/src/object_service_impl.cpp | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 6dfceec2c..de0d9fe6a 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -1098,8 +1098,8 @@ std::vector ObjectStoreManager::SplitEntryKey(const std::string &ke std::string ObjectStoreManager::GetCurrentUser() { std::vector users; - AccountDelegate::GetInstance()->QueryUsers(users); - if (users.empty()) { + if (!AccountDelegate::GetInstance()->QueryUsers(users)) { + ZLOGE("QueryUsers failed."); return ""; } return std::to_string(users[0]); diff --git a/services/distributeddataservice/service/object/src/object_service_impl.cpp b/services/distributeddataservice/service/object/src/object_service_impl.cpp index 553d1ace8..517f1458b 100644 --- a/services/distributeddataservice/service/object/src/object_service_impl.cpp +++ b/services/distributeddataservice/service/object/src/object_service_impl.cpp @@ -383,8 +383,9 @@ void ObjectServiceImpl::RegisterObjectServiceInfo() void ObjectServiceImpl::RegisterHandler() { - Handler handler = - std::bind(&ObjectServiceImpl::DumpObjectServiceInfo, this, std::placeholders::_1, std::placeholders::_2); + Handler handler = [this](int fd, std::map> ¶ms) { + DumpObjectServiceInfo(fd, params); + }; DumpManager::GetInstance().AddHandler("FEATURE_INFO", uintptr_t(this), handler); } -- Gitee From edb3cac325e4f5b436f897b3d968d9166932b95c Mon Sep 17 00:00:00 2001 From: wanghuajian-6 Date: Fri, 18 Apr 2025 10:56:00 +0800 Subject: [PATCH 07/11] fix clear bug and std::bind check Signed-off-by: wanghuajian-6 --- .../framework/metadata/object_user_meta_data.cpp | 1 - .../service/object/include/object_manager.h | 2 +- .../service/object/src/object_manager.cpp | 7 ++----- .../service/object/src/object_service_impl.cpp | 2 +- .../service/test/object_manager_test.cpp | 2 +- 5 files changed, 5 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp b/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp index e4471f745..c5d5d9e07 100644 --- a/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp +++ b/services/distributeddataservice/framework/metadata/object_user_meta_data.cpp @@ -15,7 +15,6 @@ #include "metadata/object_user_meta_data.h" namespace OHOS::DistributedData { - ObjectUserMetaData::ObjectUserMetaData() { } diff --git a/services/distributeddataservice/service/object/include/object_manager.h b/services/distributeddataservice/service/object/include/object_manager.h index f1e38aa20..6ce04d901 100644 --- a/services/distributeddataservice/service/object/include/object_manager.h +++ b/services/distributeddataservice/service/object/include/object_manager.h @@ -89,7 +89,7 @@ public: sptr callback, uint32_t tokenId); void SetData(const std::string &dataDir, const std::string &userId); int32_t Clear(); - int32_t ClearOldUserMeta(); + int32_t CleanOldUserMeta(); int32_t DeleteByAppId(const std::string &appId, int32_t user); void RegisterRemoteCallback(const std::string &bundleName, const std::string &sessionId, pid_t pid, uint32_t tokenId, diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index de0d9fe6a..e121bb10e 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -317,14 +317,11 @@ int32_t ObjectStoreManager::Clear() return result; } -int32_t ObjectStoreManager::ClearOldUserMeta() +int32_t ObjectStoreManager::CleanOldUserMeta() { std::string userId = GetCurrentUser(); if (userId.empty()) { - ZLOGI("get userId error, one minute again"); - executors_->Schedule(std::chrono::minutes(INTERVAL), [this]() { - ClearOldUserMeta(); - }); + ZLOGI("get userId error"); return OBJECT_INNER_ERROR; } ObjectUserMetaData userMetaData; diff --git a/services/distributeddataservice/service/object/src/object_service_impl.cpp b/services/distributeddataservice/service/object/src/object_service_impl.cpp index 517f1458b..a207bf00f 100644 --- a/services/distributeddataservice/service/object/src/object_service_impl.cpp +++ b/services/distributeddataservice/service/object/src/object_service_impl.cpp @@ -139,7 +139,7 @@ int32_t ObjectServiceImpl::OnInitialize() StoreMetaData saveMeta; SaveMetaData(saveMeta, std::to_string(userId), accountId); ObjectStoreManager::GetInstance()->SetData(saveMeta.dataDir, std::to_string(userId)); - ObjectStoreManager::GetInstance()->ClearOldUserMeta(); + ObjectStoreManager::GetInstance()->CleanOldUserMeta(); RegisterObjectServiceInfo(); RegisterHandler(); ObjectDmsHandler::GetInstance().RegisterDmsEvent(); diff --git a/services/distributeddataservice/service/test/object_manager_test.cpp b/services/distributeddataservice/service/test/object_manager_test.cpp index cfed2eacf..b795e592a 100644 --- a/services/distributeddataservice/service/test/object_manager_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_test.cpp @@ -1095,7 +1095,7 @@ HWTEST_F(ObjectManagerTest, GetObjectData002, TestSize.Level1) HWTEST_F(ObjectManagerTest, ClearOldUserMeta001, TestSize.Level1) { auto manager = ObjectStoreManager::GetInstance(); - auto status = manager->ClearOldUserMeta(); + auto status = manager->CleanOldUserMeta(); ASSERT_EQ(status, DistributedObject::OBJECT_SUCCESS); } } // namespace OHOS::Test -- Gitee From 721fcefa6506acfbc2c496708bd0a80bbd21aa6d Mon Sep 17 00:00:00 2001 From: wanghuajian-6 Date: Sat, 19 Apr 2025 17:14:02 +0800 Subject: [PATCH 08/11] fix clear bug Signed-off-by: wanghuajian-6 --- services/distributeddataservice/service/test/BUILD.gn | 4 ++++ .../service/test/object_manager_test.cpp | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 9c0b1d91a..9b1b757b3 100755 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -828,6 +828,7 @@ ohos_unittest("ObjectDmsHandlerTest") { ohos_unittest("ObjectManagerTest") { module_out_path = module_output_path sources = [ + "${data_service_path}/app/src/kvstore_meta_manager.cpp", "${data_service_path}/service/common/common_types_utils.cpp", "${data_service_path}/service/common/value_proxy.cpp", "../object/src/object_asset_loader.cpp", @@ -849,6 +850,7 @@ ohos_unittest("ObjectManagerTest") { include_dirs = [ "${dataobject_path}/frameworks/innerkitsimpl/include", + "${data_service_path}/app/src", "${data_service_path}/service/common", "${dataobject_path}/frameworks/innerkitsimpl/include/common", "${dataobject_path}/interfaces/innerkits", @@ -864,6 +866,7 @@ ohos_unittest("ObjectManagerTest") { "access_token:libtokenid_sdk", "c_utils:utils", "data_object:distributeddataobject_impl", + "dataclassification:data_transit_mgr", "dfs_service:cloudsync_asset_kit_inner", "dfs_service:distributed_file_daemon_kit_inner", "dmsfwk:distributed_sdk", @@ -874,6 +877,7 @@ ohos_unittest("ObjectManagerTest") { "ipc:ipc_core", "json:nlohmann_json_static", "kv_store:distributeddata_inner", + "kv_store:distributeddata_mgr", "kv_store:distributeddb", "relational_store:native_rdb", ] diff --git a/services/distributeddataservice/service/test/object_manager_test.cpp b/services/distributeddataservice/service/test/object_manager_test.cpp index b795e592a..9d5abb512 100644 --- a/services/distributeddataservice/service/test/object_manager_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_test.cpp @@ -20,14 +20,17 @@ #include #include +#include "bootstrap.h" #include "device_manager_adapter_mock.h" #include "executor_pool.h" +#include "kvstore_meta_manager.h" #include "kv_store_nb_delegate_mock.h" #include "object_types.h" #include "snapshot/machine_status.h" using namespace testing::ext; using namespace OHOS::DistributedObject; +using namespace OHOS::DistributedData; using namespace std; using namespace testing; using AssetValue = OHOS::CommonType::AssetValue; @@ -1094,6 +1097,13 @@ HWTEST_F(ObjectManagerTest, GetObjectData002, TestSize.Level1) */ HWTEST_F(ObjectManagerTest, ClearOldUserMeta001, TestSize.Level1) { + std::shared_ptr executors = std::make_shared(1, 0); + Bootstrap::GetInstance().LoadDirectory(); + Bootstrap::GetInstance().LoadCheckers(); + DistributedKv::KvStoreMetaManager::GetInstance().BindExecutor(executors); + DistributedKv::KvStoreMetaManager::GetInstance().InitMetaParameter(); + DistributedKv::KvStoreMetaManager::GetInstance().InitMetaListener(); + auto manager = ObjectStoreManager::GetInstance(); auto status = manager->CleanOldUserMeta(); ASSERT_EQ(status, DistributedObject::OBJECT_SUCCESS); -- Gitee From c53266dd12f462415fc41eaa8f28261b38265038 Mon Sep 17 00:00:00 2001 From: wanghuajian-6 Date: Sat, 19 Apr 2025 19:27:03 +0800 Subject: [PATCH 09/11] fix clear bug Signed-off-by: wanghuajian-6 --- .../distributeddataservice/service/test/object_manager_test.cpp | 2 +- .../service/test/object_service_impl_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/test/object_manager_test.cpp b/services/distributeddataservice/service/test/object_manager_test.cpp index 9d5abb512..e9001c69e 100644 --- a/services/distributeddataservice/service/test/object_manager_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_test.cpp @@ -187,7 +187,7 @@ HWTEST_F(ObjectManagerTest, Clear001, TestSize.Level0) { auto manager = ObjectStoreManager::GetInstance(); auto result = manager->Clear(); - ASSERT_EQ(result, OHOS::DistributedObject::OBJECT_STORE_NOT_FOUND); + ASSERT_EQ(result, OHOS::DistributedObject::OBJECT_SUCCESS); } /** diff --git a/services/distributeddataservice/service/test/object_service_impl_test.cpp b/services/distributeddataservice/service/test/object_service_impl_test.cpp index e67f493ab..4a0a0b2f3 100644 --- a/services/distributeddataservice/service/test/object_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/object_service_impl_test.cpp @@ -149,6 +149,6 @@ HWTEST_F(ObjectServiceImplTest, ResolveAutoLaunch001, TestSize.Level1) std::string identifier = "identifier"; std::shared_ptr objectServiceImpl = std::make_shared(); int32_t ret = objectServiceImpl->ResolveAutoLaunch(identifier, param); - EXPECT_EQ(ret, OBJECT_STORE_NOT_FOUND); + EXPECT_EQ(ret, OBJECT_SUCCESS); } } \ No newline at end of file -- Gitee From 926c0bf092d08e5de639a0cee931fb830887ce1b Mon Sep 17 00:00:00 2001 From: wanghuajian-6 Date: Sat, 19 Apr 2025 20:27:43 +0800 Subject: [PATCH 10/11] fix clear bug Signed-off-by: wanghuajian-6 --- .../service/object/include/object_manager.h | 2 +- .../service/object/src/object_manager.cpp | 22 ++++++++++++++----- .../object/src/object_service_impl.cpp | 2 +- .../service/test/object_manager_test.cpp | 19 ++++++++-------- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/services/distributeddataservice/service/object/include/object_manager.h b/services/distributeddataservice/service/object/include/object_manager.h index 6ce04d901..158b5c50a 100644 --- a/services/distributeddataservice/service/object/include/object_manager.h +++ b/services/distributeddataservice/service/object/include/object_manager.h @@ -89,7 +89,7 @@ public: sptr callback, uint32_t tokenId); void SetData(const std::string &dataDir, const std::string &userId); int32_t Clear(); - int32_t CleanOldUserMeta(); + int32_t InitUserMeta(); int32_t DeleteByAppId(const std::string &appId, int32_t user); void RegisterRemoteCallback(const std::string &bundleName, const std::string &sessionId, pid_t pid, uint32_t tokenId, diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index e121bb10e..176ed3786 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -294,7 +294,11 @@ int32_t ObjectStoreManager::Clear() ZLOGI("enter"); DistributedData::ObjectUserMetaData userMeta; if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(userMeta.GetKey(), userMeta, true)) { - ZLOGI("no object user meta. don't need clear"); + ZLOGE("load meta error"); + return OBJECT_INNER_ERROR; + } + if (userMeta.userId.empty()) { + ZLOGI("no object user meta, don't need clean"); return OBJECT_SUCCESS; } std::string userId = GetCurrentUser(); @@ -317,17 +321,25 @@ int32_t ObjectStoreManager::Clear() return result; } -int32_t ObjectStoreManager::CleanOldUserMeta() +int32_t ObjectStoreManager::InitUserMeta() { + ObjectUserMetaData userMeta; + if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(userMeta.GetKey(), userMeta, true)) { + ZLOGE("load meta error"); + return OBJECT_INNER_ERROR; + } + if (!userMeta.userId.empty()) { + ZLOGI("userId has been set, don't need clean"); + return OBJECT_SUCCESS; + } std::string userId = GetCurrentUser(); if (userId.empty()) { ZLOGI("get userId error"); return OBJECT_INNER_ERROR; } - ObjectUserMetaData userMetaData; - userMetaData.userId = userId; + userMeta.userId = userId; if (!DistributedData::MetaDataManager::GetInstance().SaveMeta(DistributedData::ObjectUserMetaData::GetKey(), - userMetaData, true)) { + userMeta, true)) { ZLOGE("save meta error, userId:%{public}s", userId.c_str()); return OBJECT_INNER_ERROR; } diff --git a/services/distributeddataservice/service/object/src/object_service_impl.cpp b/services/distributeddataservice/service/object/src/object_service_impl.cpp index a207bf00f..71f6195e7 100644 --- a/services/distributeddataservice/service/object/src/object_service_impl.cpp +++ b/services/distributeddataservice/service/object/src/object_service_impl.cpp @@ -139,7 +139,7 @@ int32_t ObjectServiceImpl::OnInitialize() StoreMetaData saveMeta; SaveMetaData(saveMeta, std::to_string(userId), accountId); ObjectStoreManager::GetInstance()->SetData(saveMeta.dataDir, std::to_string(userId)); - ObjectStoreManager::GetInstance()->CleanOldUserMeta(); + ObjectStoreManager::GetInstance()->InitUserMeta(); RegisterObjectServiceInfo(); RegisterHandler(); ObjectDmsHandler::GetInstance().RegisterDmsEvent(); diff --git a/services/distributeddataservice/service/test/object_manager_test.cpp b/services/distributeddataservice/service/test/object_manager_test.cpp index e9001c69e..4250b4c64 100644 --- a/services/distributeddataservice/service/test/object_manager_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_test.cpp @@ -104,6 +104,12 @@ void ObjectManagerTest::SetUpTestCase(void) { devMgrAdapterMock = make_shared(); BDeviceManagerAdapter::deviceManagerAdapter = devMgrAdapterMock; + std::shared_ptr executors = std::make_shared(1, 0); + Bootstrap::GetInstance().LoadDirectory(); + Bootstrap::GetInstance().LoadCheckers(); + DistributedKv::KvStoreMetaManager::GetInstance().BindExecutor(executors); + DistributedKv::KvStoreMetaManager::GetInstance().InitMetaParameter(); + DistributedKv::KvStoreMetaManager::GetInstance().InitMetaListener(); } void ObjectManagerTest::TearDownTestCase(void) @@ -1091,21 +1097,14 @@ HWTEST_F(ObjectManagerTest, GetObjectData002, TestSize.Level1) } /** -* @tc.name: ClearOldUserMeta001 +* @tc.name: InitUserMeta001 * @tc.desc: test clear old user meta. * @tc.type: FUNC */ -HWTEST_F(ObjectManagerTest, ClearOldUserMeta001, TestSize.Level1) +HWTEST_F(ObjectManagerTest, InitUserMeta001, TestSize.Level1) { - std::shared_ptr executors = std::make_shared(1, 0); - Bootstrap::GetInstance().LoadDirectory(); - Bootstrap::GetInstance().LoadCheckers(); - DistributedKv::KvStoreMetaManager::GetInstance().BindExecutor(executors); - DistributedKv::KvStoreMetaManager::GetInstance().InitMetaParameter(); - DistributedKv::KvStoreMetaManager::GetInstance().InitMetaListener(); - auto manager = ObjectStoreManager::GetInstance(); - auto status = manager->CleanOldUserMeta(); + auto status = manager->InitUserMeta(); ASSERT_EQ(status, DistributedObject::OBJECT_SUCCESS); } } // namespace OHOS::Test -- Gitee From 592857f5dcefd2e923204994768e0f66ff707800 Mon Sep 17 00:00:00 2001 From: wanghuajian-6 Date: Mon, 21 Apr 2025 11:49:01 +0800 Subject: [PATCH 11/11] fix clear bug Signed-off-by: wanghuajian-6 --- .../service/object/src/object_manager.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 176ed3786..51635cf04 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -324,11 +324,7 @@ int32_t ObjectStoreManager::Clear() int32_t ObjectStoreManager::InitUserMeta() { ObjectUserMetaData userMeta; - if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(userMeta.GetKey(), userMeta, true)) { - ZLOGE("load meta error"); - return OBJECT_INNER_ERROR; - } - if (!userMeta.userId.empty()) { + if (DistributedData::MetaDataManager::GetInstance().LoadMeta(userMeta.GetKey(), userMeta, true)) { ZLOGI("userId has been set, don't need clean"); return OBJECT_SUCCESS; } @@ -338,17 +334,17 @@ int32_t ObjectStoreManager::InitUserMeta() return OBJECT_INNER_ERROR; } userMeta.userId = userId; - if (!DistributedData::MetaDataManager::GetInstance().SaveMeta(DistributedData::ObjectUserMetaData::GetKey(), - userMeta, true)) { - ZLOGE("save meta error, userId:%{public}s", userId.c_str()); - return OBJECT_INNER_ERROR; - } std::string appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); std::string metaKey = GetMetaUserIdKey(userId, appId); if (!DistributedData::MetaDataManager::GetInstance().DelMeta(metaKey, true)) { ZLOGE("delete old meta error, userId:%{public}s", userId.c_str()); return OBJECT_INNER_ERROR; } + if (!DistributedData::MetaDataManager::GetInstance().SaveMeta(DistributedData::ObjectUserMetaData::GetKey(), + userMeta, true)) { + ZLOGE("save meta error, userId:%{public}s", userId.c_str()); + return OBJECT_INNER_ERROR; + } return OBJECT_SUCCESS; } -- Gitee