diff --git a/services/dbms/include/distributed_data_storage.h b/services/dbms/include/distributed_data_storage.h index fd2840f6ac48ad7405f2e862e0b4166b1917b125..fdc2eee34cd5b6294982dd51823b98a6a4e28078 100644 --- a/services/dbms/include/distributed_data_storage.h +++ b/services/dbms/include/distributed_data_storage.h @@ -53,6 +53,8 @@ private: DistributedBundleInfo ConvertToDistributedBundleInfo(const BundleInfo &bundleInfo); int32_t GetUdidByNetworkId(const std::string &networkId, std::string &udid); bool InnerSaveStorageDistributeInfo(const DistributedBundleInfo &distributedBundleInfo); + std::map GetAllOldDistributionBundleInfo(); + private: static std::mutex mutex_; static std::shared_ptr instance_; diff --git a/services/dbms/src/distributed_data_storage.cpp b/services/dbms/src/distributed_data_storage.cpp index add8daddc2d12ac86567ab67e4b5cb25cf5ecd01..a44cd6b63c7f636fdf1a90005ebefbe18c81e991 100644 --- a/services/dbms/src/distributed_data_storage.cpp +++ b/services/dbms/src/distributed_data_storage.cpp @@ -358,56 +358,74 @@ DistributedBundleInfo DistributedDataStorage::ConvertToDistributedBundleInfo(con } distributedBundleInfo.enabled = bundleInfo.applicationInfo.enabled; distributedBundleInfo.accessTokenId = bundleInfo.applicationInfo.accessTokenId; + distributedBundleInfo.updateTime = bundleInfo.updateTime; return distributedBundleInfo; } void DistributedDataStorage::UpdateDistributedData(int32_t userId) { APP_LOGI("UpdateDistributedData"); + auto bundleMgr = DelayedSingleton::GetInstance()->GetBundleMgr(); + if (bundleMgr == nullptr) { + APP_LOGE("Get bundleMgr shared_ptr nullptr"); + return; + } + std::map oldDistributedBundleInfos = GetAllOldDistributionBundleInfo(); + std::vector bundleInfos; + if (!bundleMgr->GetBundleInfos(FLAGS, bundleInfos, userId)) { + APP_LOGE("get bundleInfos failed"); + return; + } + for (const auto &bundleInfo : bundleInfos) { + if (bundleInfo.singleton) { + continue; + } + if (oldDistributedBundleInfos.find(bundleInfo.name) != oldDistributedBundleInfos.end()) { + if (oldDistributedBundleInfos[bundleInfo.name].updateTime == bundleInfo.updateTime) { + APP_LOGW("bundleName:%{public}s no need to update", bundleInfo.name.c_str()); + continue; + } + } + if (!InnerSaveStorageDistributeInfo(ConvertToDistributedBundleInfo(bundleInfo))) { + APP_LOGW("UpdateDistributedData SaveStorageDistributeInfo:%{public}s failed", bundleInfo.name.c_str()); + } + } +} + +std::map DistributedDataStorage::GetAllOldDistributionBundleInfo() +{ + APP_LOGD("start"); + std::map oldDistributedBundleInfos; if (kvStorePtr_ == nullptr) { APP_LOGE("kvStorePtr_ is null"); - return; + return oldDistributedBundleInfos; } std::string udid; if (!GetLocalUdid(udid)) { APP_LOGE("GetLocalUdid failed"); - return; + return oldDistributedBundleInfos; } Key allEntryKeyPrefix(""); std::vector allEntries; Status status = kvStorePtr_->GetEntries(allEntryKeyPrefix, allEntries); if (status != Status::SUCCESS) { APP_LOGE("dataManager_ GetEntries error: %{public}d", status); - return; + return oldDistributedBundleInfos; } - for (auto entry : allEntries) { + for (const auto &entry : allEntries) { std::string key = entry.key.ToString(); if (key.find(udid) == std::string::npos) { continue; } - status = kvStorePtr_->Delete(entry.key); - if (status != Status::SUCCESS) { - APP_LOGE("Delete key:%{public}s failed", key.c_str()); - } - } - auto bundleMgr = DelayedSingleton::GetInstance()->GetBundleMgr(); - if (bundleMgr == nullptr) { - APP_LOGE("Get bundleMgr shared_ptr nullptr"); - return; - } - std::vector bundleInfos; - if (!bundleMgr->GetBundleInfos(FLAGS, bundleInfos, userId)) { - APP_LOGE("get bundleInfos failed"); - return; - } - for (auto bundleInfo : bundleInfos) { - if (bundleInfo.singleton) { - continue; - } - if (!InnerSaveStorageDistributeInfo(ConvertToDistributedBundleInfo(bundleInfo))) { - APP_LOGW("UpdateDistributedData SaveStorageDistributeInfo:%{public}s failed", bundleInfo.name.c_str()); + std::string value = entry.value.ToString(); + DistributedBundleInfo distributedBundleInfo; + if (distributedBundleInfo.FromJsonString(value)) { + oldDistributedBundleInfos.emplace(distributedBundleInfo.bundleName, distributedBundleInfo); + } else { + APP_LOGE("DistributionInfo FromJsonString key:%{public}s failed", key.c_str()); } } + return oldDistributedBundleInfos; } } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file