From ecd2dd6ab8bebd70062f18a387ae48985acf3b61 Mon Sep 17 00:00:00 2001 From: ding_dong_dong Date: Fri, 28 Feb 2025 11:27:44 +0800 Subject: [PATCH] modify autocache Signed-off-by: ding_dong_dong --- .../framework/include/store/auto_cache.h | 9 ++-- .../framework/store/auto_cache.cpp | 44 +++++++++++++------ 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/services/distributeddataservice/framework/include/store/auto_cache.h b/services/distributeddataservice/framework/include/store/auto_cache.h index cc9e6c79b..2f0f8346c 100644 --- a/services/distributeddataservice/framework/include/store/auto_cache.h +++ b/services/distributeddataservice/framework/include/store/auto_cache.h @@ -52,13 +52,15 @@ public: API_EXPORT std::pair GetDBStore(const StoreMetaData &meta, const Watchers &watchers); - API_EXPORT Stores GetStoresIfPresent(uint32_t tokenId, const std::string &storeName = ""); + API_EXPORT Stores GetStoresIfPresent(uint32_t tokenId, const std::string &storeName = "", + const std::string &userId = ""); - API_EXPORT void CloseStore(uint32_t tokenId, const std::string &storeId = ""); + API_EXPORT void CloseStore(uint32_t tokenId, const std::string &storeId = "", const std::string &userId = ""); API_EXPORT void CloseStore(const Filter &filter); - API_EXPORT void SetObserver(uint32_t tokenId, const std::string &storeId, const Watchers &watchers); + API_EXPORT void SetObserver(uint32_t tokenId, const std::string &storeId, const Watchers &watchers, + const std::string &userId = ""); API_EXPORT void Enable(uint32_t tokenId, const std::string &storeId = ""); @@ -67,6 +69,7 @@ public: private: AutoCache(); ~AutoCache(); + std::string GenerateKey(const std::string &userId, const std::string &storeId) const; void GarbageCollect(bool isForce); void StartTimer(); struct Delegate : public GeneralWatcher { diff --git a/services/distributeddataservice/framework/store/auto_cache.cpp b/services/distributeddataservice/framework/store/auto_cache.cpp index ad799d6ec..3aa93e9e8 100644 --- a/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/services/distributeddataservice/framework/store/auto_cache.cpp @@ -25,6 +25,7 @@ #include "utils/anonymous.h" namespace OHOS::DistributedData { using Account = AccountDelegate; +static constexpr const char *KEY_SEPARATOR = "###"; AutoCache &AutoCache::GetInstance() { static AutoCache cache; @@ -60,9 +61,19 @@ AutoCache::~AutoCache() } } +std::string AutoCache::GenerateKey(const std::string &userId, const std::string &storeId) const +{ + std::string key = ""; + if (userId.empty() || storeId.empty()) { + return key; + } + return key.append(userId).append(KEY_SEPARATOR).append(storeId); +} + std::pair AutoCache::GetDBStore(const StoreMetaData &meta, const Watchers &watchers) { Store store; + auto storeKey = GenerateKey(meta.user, meta.storeId); if (meta.storeType >= MAX_CREATOR_NUM || meta.storeType < 0 || !creators_[meta.storeType] || disables_.ContainIf(meta.tokenId, [&meta](const std::set &stores) -> bool { return stores.count(meta.storeId) != 0; })) { @@ -82,14 +93,14 @@ std::pair AutoCache::GetDBStore(const StoreMetaData & return { E_USER_DEACTIVATING, store }; } stores_.Compute(meta.tokenId, - [this, &meta, &watchers, &store](auto &, std::map &stores) -> bool { + [this, &meta, &watchers, &store, &storeKey](auto &, std::map &stores) -> bool { if (disableStores_.count(meta.dataDir) != 0) { ZLOGW("store is closing, tokenId:0x%{public}x user:%{public}s" "bundleName:%{public}s storeName:%{public}s", meta.tokenId, meta.user.c_str(), meta.bundleName.c_str(), meta.GetStoreAlias().c_str()); return !stores.empty(); } - auto it = stores.find(meta.storeId); + auto it = stores.find(storeKey); if (it != stores.end()) { if (!watchers.empty()) { it->second.SetObservers(watchers); @@ -103,7 +114,7 @@ std::pair AutoCache::GetDBStore(const StoreMetaData & return !stores.empty(); } dbStore->SetExecutor(executor_); - auto result = stores.emplace(std::piecewise_construct, std::forward_as_tuple(meta.storeId), + auto result = stores.emplace(std::piecewise_construct, std::forward_as_tuple(storeKey), std::forward_as_tuple(dbStore, watchers, atoi(meta.user.c_str()), meta)); store = result.first->second; StartTimer(); @@ -117,17 +128,19 @@ AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers & return GetDBStore(meta, watchers).second; } -AutoCache::Stores AutoCache::GetStoresIfPresent(uint32_t tokenId, const std::string &storeName) +AutoCache::Stores AutoCache::GetStoresIfPresent(uint32_t tokenId, const std::string &storeName, + const std::string &userId) { Stores stores; + auto storeKey = GenerateKey(userId, storeName); stores_.ComputeIfPresent( - tokenId, [&stores, &storeName](auto &, std::map &delegates) -> bool { - if (storeName.empty()) { + tokenId, [&stores, &storeKey](auto &, std::map &delegates) -> bool { + if (storeKey.empty()) { for (auto &[_, delegate] : delegates) { stores.push_back(delegate); } } else { - auto it = delegates.find(storeName); + auto it = delegates.find(storeKey); if (it != delegates.end()) { stores.push_back(it->second); } @@ -159,17 +172,18 @@ void AutoCache::StartTimer() ZLOGD("start timer,taskId: %{public}" PRIu64, taskId_); } -void AutoCache::CloseStore(uint32_t tokenId, const std::string &storeId) +void AutoCache::CloseStore(uint32_t tokenId, const std::string &storeId, const std::string &userId) { ZLOGD("close store start, store:%{public}s, token:%{public}u", Anonymous::Change(storeId).c_str(), tokenId); std::set storeIds; std::list closeStores; bool isScreenLocked = ScreenManager::GetInstance()->IsLocked(); + auto storeKey = GenerateKey(userId, storeId); stores_.ComputeIfPresent(tokenId, - [this, &storeId, isScreenLocked, &storeIds, &closeStores](auto &key, auto &delegates) { + [this, &storeKey, isScreenLocked, &storeIds, &closeStores](auto &, auto &delegates) { auto it = delegates.begin(); while (it != delegates.end()) { - if ((storeId == it->first || storeId.empty()) && + if ((it->first == storeKey || storeKey.empty()) && (!isScreenLocked || it->second.GetArea() != GeneralStore::EL4) && disableStores_.count(it->second.GetDataDir()) == 0) { disableStores_.insert(it->second.GetDataDir()); @@ -229,12 +243,14 @@ void AutoCache::CloseStore(const AutoCache::Filter &filter) }); } -void AutoCache::SetObserver(uint32_t tokenId, const std::string &storeId, const AutoCache::Watchers &watchers) +void AutoCache::SetObserver(uint32_t tokenId, const std::string &storeId, const AutoCache::Watchers &watchers, + const std::string &userId) { - stores_.ComputeIfPresent(tokenId, [&storeId, &watchers](auto &key, auto &stores) { + auto storeKey = GenerateKey(userId, storeId); + stores_.ComputeIfPresent(tokenId, [&storeKey, &watchers](auto &key, auto &stores) { ZLOGD("tokenId:0x%{public}x storeId:%{public}s observers:%{public}zu", key, - Anonymous::Change(storeId).c_str(), watchers.size()); - auto it = stores.find(storeId); + Anonymous::Change(storeKey).c_str(), watchers.size()); + auto it = stores.find(storeKey); if (it != stores.end()) { it->second.SetObservers(watchers); } -- Gitee