153c3577eSopenharmony_ci/* 253c3577eSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 353c3577eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 453c3577eSopenharmony_ci * you may not use this file except in compliance with the License. 553c3577eSopenharmony_ci * You may obtain a copy of the License at 653c3577eSopenharmony_ci * 753c3577eSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 853c3577eSopenharmony_ci * 953c3577eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1053c3577eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1153c3577eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1253c3577eSopenharmony_ci * See the License for the specific language governing permissions and 1353c3577eSopenharmony_ci * limitations under the License. 1453c3577eSopenharmony_ci */ 1553c3577eSopenharmony_ci#define LOG_TAG "ObjectStoreManager" 1653c3577eSopenharmony_ci 1753c3577eSopenharmony_ci#include "object_manager.h" 1853c3577eSopenharmony_ci 1953c3577eSopenharmony_ci#include <regex> 2053c3577eSopenharmony_ci 2153c3577eSopenharmony_ci#include "accesstoken_kit.h" 2253c3577eSopenharmony_ci#include "account/account_delegate.h" 2353c3577eSopenharmony_ci#include "block_data.h" 2453c3577eSopenharmony_ci#include "bootstrap.h" 2553c3577eSopenharmony_ci#include "common/bytes.h" 2653c3577eSopenharmony_ci#include "common/string_utils.h" 2753c3577eSopenharmony_ci#include "datetime_ex.h" 2853c3577eSopenharmony_ci#include "distributed_file_daemon_manager.h" 2953c3577eSopenharmony_ci#include "kvstore_utils.h" 3053c3577eSopenharmony_ci#include "log_print.h" 3153c3577eSopenharmony_ci#include "metadata/meta_data_manager.h" 3253c3577eSopenharmony_ci#include "metadata/store_meta_data.h" 3353c3577eSopenharmony_ci#include "object_dms_handler.h" 3453c3577eSopenharmony_ci#include "object_radar_reporter.h" 3553c3577eSopenharmony_ci#include "utils/anonymous.h" 3653c3577eSopenharmony_ci 3753c3577eSopenharmony_cinamespace OHOS { 3853c3577eSopenharmony_cinamespace DistributedObject { 3953c3577eSopenharmony_ciusing namespace OHOS::DistributedKv; 4053c3577eSopenharmony_ciusing namespace Security::AccessToken; 4153c3577eSopenharmony_ciusing StoreMetaData = OHOS::DistributedData::StoreMetaData; 4253c3577eSopenharmony_ciusing AccountDelegate = DistributedKv::AccountDelegate; 4353c3577eSopenharmony_ciusing Account = OHOS::DistributedKv::AccountDelegate; 4453c3577eSopenharmony_ciusing AccessTokenKit = Security::AccessToken::AccessTokenKit; 4553c3577eSopenharmony_ciusing ValueProxy = OHOS::DistributedData::ValueProxy; 4653c3577eSopenharmony_ciusing DistributedFileDaemonManager = Storage::DistributedFile::DistributedFileDaemonManager; 4753c3577eSopenharmony_ciconstexpr const char *SAVE_INFO = "p_###SAVEINFO###"; 4853c3577eSopenharmony_ciObjectStoreManager::ObjectStoreManager() 4953c3577eSopenharmony_ci{ 5053c3577eSopenharmony_ci ZLOGI("ObjectStoreManager construct"); 5153c3577eSopenharmony_ci RegisterAssetsLister(); 5253c3577eSopenharmony_ci} 5353c3577eSopenharmony_ci 5453c3577eSopenharmony_ciObjectStoreManager::~ObjectStoreManager() 5553c3577eSopenharmony_ci{ 5653c3577eSopenharmony_ci ZLOGI("ObjectStoreManager destroy"); 5753c3577eSopenharmony_ci if (objectAssetsRecvListener_ != nullptr) { 5853c3577eSopenharmony_ci auto status = DistributedFileDaemonManager::GetInstance().UnRegisterAssetCallback(objectAssetsRecvListener_); 5953c3577eSopenharmony_ci if (status != DistributedDB::DBStatus::OK) { 6053c3577eSopenharmony_ci ZLOGE("UnRegister assetsRecvListener err %{public}d", status); 6153c3577eSopenharmony_ci } 6253c3577eSopenharmony_ci } 6353c3577eSopenharmony_ci} 6453c3577eSopenharmony_ci 6553c3577eSopenharmony_ciDistributedDB::KvStoreNbDelegate *ObjectStoreManager::OpenObjectKvStore() 6653c3577eSopenharmony_ci{ 6753c3577eSopenharmony_ci DistributedDB::KvStoreNbDelegate *store = nullptr; 6853c3577eSopenharmony_ci DistributedDB::KvStoreNbDelegate::Option option; 6953c3577eSopenharmony_ci option.createDirByStoreIdOnly = true; 7053c3577eSopenharmony_ci option.syncDualTupleMode = true; 7153c3577eSopenharmony_ci option.secOption = { DistributedDB::S1, DistributedDB::ECE }; 7253c3577eSopenharmony_ci if (objectDataListener_ == nullptr) { 7353c3577eSopenharmony_ci objectDataListener_ = new ObjectDataListener(); 7453c3577eSopenharmony_ci } 7553c3577eSopenharmony_ci ZLOGD("start GetKvStore"); 7653c3577eSopenharmony_ci kvStoreDelegateManager_->GetKvStore(ObjectCommon::OBJECTSTORE_DB_STOREID, option, 7753c3577eSopenharmony_ci [&store, this](DistributedDB::DBStatus dbStatus, DistributedDB::KvStoreNbDelegate *kvStoreNbDelegate) { 7853c3577eSopenharmony_ci if (dbStatus != DistributedDB::DBStatus::OK) { 7953c3577eSopenharmony_ci ZLOGE("GetKvStore fail %{public}d", dbStatus); 8053c3577eSopenharmony_ci return; 8153c3577eSopenharmony_ci } 8253c3577eSopenharmony_ci ZLOGI("GetKvStore successsfully"); 8353c3577eSopenharmony_ci store = kvStoreNbDelegate; 8453c3577eSopenharmony_ci std::vector<uint8_t> tmpKey; 8553c3577eSopenharmony_ci DistributedDB::DBStatus status = store->RegisterObserver(tmpKey, 8653c3577eSopenharmony_ci DistributedDB::ObserverMode::OBSERVER_CHANGES_FOREIGN, 8753c3577eSopenharmony_ci objectDataListener_); 8853c3577eSopenharmony_ci if (status != DistributedDB::DBStatus::OK) { 8953c3577eSopenharmony_ci ZLOGE("RegisterObserver err %{public}d", status); 9053c3577eSopenharmony_ci } 9153c3577eSopenharmony_ci }); 9253c3577eSopenharmony_ci return store; 9353c3577eSopenharmony_ci} 9453c3577eSopenharmony_ci 9553c3577eSopenharmony_cibool ObjectStoreManager::RegisterAssetsLister() 9653c3577eSopenharmony_ci{ 9753c3577eSopenharmony_ci if (objectAssetsSendListener_ == nullptr) { 9853c3577eSopenharmony_ci objectAssetsSendListener_ = new ObjectAssetsSendListener(); 9953c3577eSopenharmony_ci } 10053c3577eSopenharmony_ci if (objectAssetsRecvListener_ == nullptr) { 10153c3577eSopenharmony_ci objectAssetsRecvListener_ = new ObjectAssetsRecvListener(); 10253c3577eSopenharmony_ci } 10353c3577eSopenharmony_ci auto status = DistributedFileDaemonManager::GetInstance().RegisterAssetCallback(objectAssetsRecvListener_); 10453c3577eSopenharmony_ci if (status != DistributedDB::DBStatus::OK) { 10553c3577eSopenharmony_ci ZLOGE("Register assetsRecvListener err %{public}d", status); 10653c3577eSopenharmony_ci return false; 10753c3577eSopenharmony_ci } 10853c3577eSopenharmony_ci return true; 10953c3577eSopenharmony_ci} 11053c3577eSopenharmony_ci 11153c3577eSopenharmony_civoid ObjectStoreManager::ProcessSyncCallback(const std::map<std::string, int32_t> &results, const std::string &appId, 11253c3577eSopenharmony_ci const std::string &sessionId, const std::string &deviceId) 11353c3577eSopenharmony_ci{ 11453c3577eSopenharmony_ci if (results.empty() || results.find(LOCAL_DEVICE) != results.end()) { 11553c3577eSopenharmony_ci return; 11653c3577eSopenharmony_ci } 11753c3577eSopenharmony_ci int32_t result = Open(); 11853c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 11953c3577eSopenharmony_ci ZLOGE("Open failed, errCode = %{public}d", result); 12053c3577eSopenharmony_ci return; 12153c3577eSopenharmony_ci } 12253c3577eSopenharmony_ci // delete local data 12353c3577eSopenharmony_ci result = RevokeSaveToStore(GetPropertyPrefix(appId, sessionId, deviceId)); 12453c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 12553c3577eSopenharmony_ci ZLOGE("Save failed, status = %{public}d", result); 12653c3577eSopenharmony_ci } 12753c3577eSopenharmony_ci Close(); 12853c3577eSopenharmony_ci return; 12953c3577eSopenharmony_ci} 13053c3577eSopenharmony_ci 13153c3577eSopenharmony_ciint32_t ObjectStoreManager::Save(const std::string &appId, const std::string &sessionId, 13253c3577eSopenharmony_ci const ObjectRecord &data, const std::string &deviceId, sptr<IRemoteObject> callback) 13353c3577eSopenharmony_ci{ 13453c3577eSopenharmony_ci auto proxy = iface_cast<ObjectSaveCallbackProxy>(callback); 13553c3577eSopenharmony_ci if (deviceId.size() == 0) { 13653c3577eSopenharmony_ci ZLOGE("DeviceId empty, appId: %{public}s, sessionId: %{public}s", appId.c_str(), sessionId.c_str()); 13753c3577eSopenharmony_ci proxy->Completed(std::map<std::string, int32_t>()); 13853c3577eSopenharmony_ci return INVALID_ARGUMENT; 13953c3577eSopenharmony_ci } 14053c3577eSopenharmony_ci int32_t result = Open(); 14153c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 14253c3577eSopenharmony_ci ZLOGE("Open object kvstore failed, result: %{public}d", result); 14353c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStateError(std::string(__FUNCTION__), ObjectStore::SAVE, 14453c3577eSopenharmony_ci ObjectStore::SAVE_TO_STORE, ObjectStore::RADAR_FAILED, ObjectStore::GETKV_FAILED, ObjectStore::FINISHED); 14553c3577eSopenharmony_ci proxy->Completed(std::map<std::string, int32_t>()); 14653c3577eSopenharmony_ci return STORE_NOT_OPEN; 14753c3577eSopenharmony_ci } 14853c3577eSopenharmony_ci SaveUserToMeta(); 14953c3577eSopenharmony_ci std::string dstBundleName = ObjectDmsHandler::GetInstance().GetDstBundleName(appId, deviceId); 15053c3577eSopenharmony_ci result = SaveToStore(dstBundleName, sessionId, deviceId, data); 15153c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 15253c3577eSopenharmony_ci ZLOGE("Save to store failed, result: %{public}d", result); 15353c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStateError(std::string(__FUNCTION__), ObjectStore::SAVE, 15453c3577eSopenharmony_ci ObjectStore::SAVE_TO_STORE, ObjectStore::RADAR_FAILED, result, ObjectStore::FINISHED); 15553c3577eSopenharmony_ci Close(); 15653c3577eSopenharmony_ci proxy->Completed(std::map<std::string, int32_t>()); 15753c3577eSopenharmony_ci return result; 15853c3577eSopenharmony_ci } 15953c3577eSopenharmony_ci ZLOGI("Sync data, bundleName: %{public}s, sessionId: %{public}s, deviceId: %{public}s", dstBundleName.c_str(), 16053c3577eSopenharmony_ci sessionId.c_str(), Anonymous::Change(deviceId).c_str()); 16153c3577eSopenharmony_ci SyncCallBack syncCallback = 16253c3577eSopenharmony_ci [proxy, dstBundleName, sessionId, deviceId, this](const std::map<std::string, int32_t> &results) { 16353c3577eSopenharmony_ci ProcessSyncCallback(results, dstBundleName, sessionId, deviceId); 16453c3577eSopenharmony_ci proxy->Completed(results); 16553c3577eSopenharmony_ci }; 16653c3577eSopenharmony_ci result = SyncOnStore(GetPropertyPrefix(dstBundleName, sessionId, deviceId), {deviceId}, syncCallback); 16753c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 16853c3577eSopenharmony_ci ZLOGE("Sync data failed, result: %{public}d", result); 16953c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStateError(std::string(__FUNCTION__), ObjectStore::SAVE, 17053c3577eSopenharmony_ci ObjectStore::SYNC_DATA, ObjectStore::RADAR_FAILED, result, ObjectStore::FINISHED); 17153c3577eSopenharmony_ci Close(); 17253c3577eSopenharmony_ci proxy->Completed(std::map<std::string, int32_t>()); 17353c3577eSopenharmony_ci return result; 17453c3577eSopenharmony_ci } 17553c3577eSopenharmony_ci Close(); 17653c3577eSopenharmony_ci return PushAssets(appId, dstBundleName, sessionId, data, deviceId); 17753c3577eSopenharmony_ci} 17853c3577eSopenharmony_ci 17953c3577eSopenharmony_ciint32_t ObjectStoreManager::PushAssets(const std::string &srcBundleName, const std::string &dstBundleName, 18053c3577eSopenharmony_ci const std::string &sessionId, const ObjectRecord &data, const std::string &deviceId) 18153c3577eSopenharmony_ci{ 18253c3577eSopenharmony_ci Assets assets = GetAssetsFromDBRecords(data); 18353c3577eSopenharmony_ci if (assets.empty() || data.find(ObjectStore::FIELDS_PREFIX + ObjectStore::DEVICEID_KEY) == data.end()) { 18453c3577eSopenharmony_ci return OBJECT_SUCCESS; 18553c3577eSopenharmony_ci } 18653c3577eSopenharmony_ci sptr<AssetObj> assetObj = new AssetObj(); 18753c3577eSopenharmony_ci assetObj->dstBundleName_ = dstBundleName; 18853c3577eSopenharmony_ci assetObj->srcBundleName_ = srcBundleName; 18953c3577eSopenharmony_ci assetObj->dstNetworkId_ = deviceId; 19053c3577eSopenharmony_ci assetObj->sessionId_ = sessionId; 19153c3577eSopenharmony_ci for (const auto& asset : assets) { 19253c3577eSopenharmony_ci assetObj->uris_.push_back(asset.uri); 19353c3577eSopenharmony_ci } 19453c3577eSopenharmony_ci if (objectAssetsSendListener_ == nullptr) { 19553c3577eSopenharmony_ci objectAssetsSendListener_ = new ObjectAssetsSendListener(); 19653c3577eSopenharmony_ci } 19753c3577eSopenharmony_ci int userId = std::atoi(GetCurrentUser().c_str()); 19853c3577eSopenharmony_ci auto status = ObjectAssetLoader::GetInstance()->PushAsset(userId, assetObj, objectAssetsSendListener_); 19953c3577eSopenharmony_ci return status; 20053c3577eSopenharmony_ci} 20153c3577eSopenharmony_ci 20253c3577eSopenharmony_ciint32_t ObjectStoreManager::RevokeSave( 20353c3577eSopenharmony_ci const std::string &appId, const std::string &sessionId, sptr<IRemoteObject> callback) 20453c3577eSopenharmony_ci{ 20553c3577eSopenharmony_ci auto proxy = iface_cast<ObjectRevokeSaveCallbackProxy>(callback); 20653c3577eSopenharmony_ci int32_t result = Open(); 20753c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 20853c3577eSopenharmony_ci ZLOGE("Open failed, errCode = %{public}d", result); 20953c3577eSopenharmony_ci proxy->Completed(STORE_NOT_OPEN); 21053c3577eSopenharmony_ci return STORE_NOT_OPEN; 21153c3577eSopenharmony_ci } 21253c3577eSopenharmony_ci 21353c3577eSopenharmony_ci result = RevokeSaveToStore(GetPrefixWithoutDeviceId(appId, sessionId)); 21453c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 21553c3577eSopenharmony_ci ZLOGE("RevokeSave failed, errCode = %{public}d", result); 21653c3577eSopenharmony_ci Close(); 21753c3577eSopenharmony_ci proxy->Completed(result); 21853c3577eSopenharmony_ci return result; 21953c3577eSopenharmony_ci } 22053c3577eSopenharmony_ci std::vector<std::string> deviceList; 22153c3577eSopenharmony_ci auto deviceInfos = DmAdaper::GetInstance().GetRemoteDevices(); 22253c3577eSopenharmony_ci std::for_each(deviceInfos.begin(), deviceInfos.end(), 22353c3577eSopenharmony_ci [&deviceList](AppDistributedKv::DeviceInfo info) { deviceList.emplace_back(info.networkId); }); 22453c3577eSopenharmony_ci if (!deviceList.empty()) { 22553c3577eSopenharmony_ci SyncCallBack tmp = [proxy](const std::map<std::string, int32_t> &results) { 22653c3577eSopenharmony_ci ZLOGI("revoke save finished"); 22753c3577eSopenharmony_ci proxy->Completed(OBJECT_SUCCESS); 22853c3577eSopenharmony_ci }; 22953c3577eSopenharmony_ci result = SyncOnStore(GetPropertyPrefix(appId, sessionId), deviceList, tmp); 23053c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 23153c3577eSopenharmony_ci ZLOGE("sync failed, errCode = %{public}d", result); 23253c3577eSopenharmony_ci proxy->Completed(result); 23353c3577eSopenharmony_ci } 23453c3577eSopenharmony_ci } else { 23553c3577eSopenharmony_ci proxy->Completed(OBJECT_SUCCESS); 23653c3577eSopenharmony_ci }; 23753c3577eSopenharmony_ci Close(); 23853c3577eSopenharmony_ci return result; 23953c3577eSopenharmony_ci} 24053c3577eSopenharmony_ci 24153c3577eSopenharmony_ciint32_t ObjectStoreManager::Retrieve( 24253c3577eSopenharmony_ci const std::string &bundleName, const std::string &sessionId, sptr<IRemoteObject> callback, uint32_t tokenId) 24353c3577eSopenharmony_ci{ 24453c3577eSopenharmony_ci auto proxy = iface_cast<ObjectRetrieveCallbackProxy>(callback); 24553c3577eSopenharmony_ci int32_t result = Open(); 24653c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 24753c3577eSopenharmony_ci ZLOGE("Open object kvstore failed, result: %{public}d", result); 24853c3577eSopenharmony_ci proxy->Completed(ObjectRecord(), false); 24953c3577eSopenharmony_ci return ObjectStore::GETKV_FAILED; 25053c3577eSopenharmony_ci } 25153c3577eSopenharmony_ci ObjectRecord results{}; 25253c3577eSopenharmony_ci int32_t status = RetrieveFromStore(bundleName, sessionId, results); 25353c3577eSopenharmony_ci if (status != OBJECT_SUCCESS) { 25453c3577eSopenharmony_ci ZLOGI("Retrieve from store failed, status: %{public}d", status); 25553c3577eSopenharmony_ci Close(); 25653c3577eSopenharmony_ci proxy->Completed(ObjectRecord(), false); 25753c3577eSopenharmony_ci return status; 25853c3577eSopenharmony_ci } 25953c3577eSopenharmony_ci bool allReady = false; 26053c3577eSopenharmony_ci Assets assets = GetAssetsFromDBRecords(results); 26153c3577eSopenharmony_ci if (assets.empty() || results.find(ObjectStore::FIELDS_PREFIX + ObjectStore::DEVICEID_KEY) == results.end()) { 26253c3577eSopenharmony_ci allReady = true; 26353c3577eSopenharmony_ci } else { 26453c3577eSopenharmony_ci auto objectKey = bundleName + sessionId; 26553c3577eSopenharmony_ci restoreStatus_.ComputeIfPresent(objectKey, [&allReady](const auto &key, auto &value) { 26653c3577eSopenharmony_ci if (value == RestoreStatus::ALL_READY) { 26753c3577eSopenharmony_ci allReady = true; 26853c3577eSopenharmony_ci return false; 26953c3577eSopenharmony_ci } 27053c3577eSopenharmony_ci if (value == RestoreStatus::DATA_READY) { 27153c3577eSopenharmony_ci value = RestoreStatus::DATA_NOTIFIED; 27253c3577eSopenharmony_ci } 27353c3577eSopenharmony_ci return true; 27453c3577eSopenharmony_ci }); 27553c3577eSopenharmony_ci } 27653c3577eSopenharmony_ci status = RevokeSaveToStore(GetPrefixWithoutDeviceId(bundleName, sessionId)); 27753c3577eSopenharmony_ci if (status != OBJECT_SUCCESS) { 27853c3577eSopenharmony_ci ZLOGE("Revoke save failed, status: %{public}d", status); 27953c3577eSopenharmony_ci Close(); 28053c3577eSopenharmony_ci proxy->Completed(ObjectRecord(), false); 28153c3577eSopenharmony_ci return status; 28253c3577eSopenharmony_ci } 28353c3577eSopenharmony_ci Close(); 28453c3577eSopenharmony_ci proxy->Completed(results, allReady); 28553c3577eSopenharmony_ci if (allReady) { 28653c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStateFinished(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE, 28753c3577eSopenharmony_ci ObjectStore::NOTIFY, ObjectStore::RADAR_SUCCESS, ObjectStore::FINISHED); 28853c3577eSopenharmony_ci } 28953c3577eSopenharmony_ci return status; 29053c3577eSopenharmony_ci} 29153c3577eSopenharmony_ci 29253c3577eSopenharmony_ciint32_t ObjectStoreManager::Clear() 29353c3577eSopenharmony_ci{ 29453c3577eSopenharmony_ci ZLOGI("enter"); 29553c3577eSopenharmony_ci std::string userId = GetCurrentUser(); 29653c3577eSopenharmony_ci if (userId.empty()) { 29753c3577eSopenharmony_ci return OBJECT_INNER_ERROR; 29853c3577eSopenharmony_ci } 29953c3577eSopenharmony_ci std::vector<StoreMetaData> metaData; 30053c3577eSopenharmony_ci std::string appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); 30153c3577eSopenharmony_ci std::string metaKey = GetMetaUserIdKey(userId, appId); 30253c3577eSopenharmony_ci if (!DistributedData::MetaDataManager::GetInstance().LoadMeta(metaKey, metaData, true)) { 30353c3577eSopenharmony_ci ZLOGE("no store of %{public}s", appId.c_str()); 30453c3577eSopenharmony_ci return OBJECT_STORE_NOT_FOUND; 30553c3577eSopenharmony_ci } 30653c3577eSopenharmony_ci for (const auto &storeMeta : metaData) { 30753c3577eSopenharmony_ci if (storeMeta.storeType < StoreMetaData::StoreType::STORE_OBJECT_BEGIN 30853c3577eSopenharmony_ci || storeMeta.storeType > StoreMetaData::StoreType::STORE_OBJECT_END) { 30953c3577eSopenharmony_ci continue; 31053c3577eSopenharmony_ci } 31153c3577eSopenharmony_ci if (storeMeta.user == userId) { 31253c3577eSopenharmony_ci ZLOGI("user is same, not need to change, mate user:%{public}s::user:%{public}s.", 31353c3577eSopenharmony_ci storeMeta.user.c_str(), userId.c_str()); 31453c3577eSopenharmony_ci return OBJECT_SUCCESS; 31553c3577eSopenharmony_ci } 31653c3577eSopenharmony_ci } 31753c3577eSopenharmony_ci ZLOGD("user is change, need to change"); 31853c3577eSopenharmony_ci int32_t result = Open(); 31953c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 32053c3577eSopenharmony_ci ZLOGE("Open failed, errCode = %{public}d", result); 32153c3577eSopenharmony_ci return STORE_NOT_OPEN; 32253c3577eSopenharmony_ci } 32353c3577eSopenharmony_ci result = RevokeSaveToStore(""); 32453c3577eSopenharmony_ci Close(); 32553c3577eSopenharmony_ci return result; 32653c3577eSopenharmony_ci} 32753c3577eSopenharmony_ci 32853c3577eSopenharmony_ciint32_t ObjectStoreManager::DeleteByAppId(const std::string &appId, int32_t user) 32953c3577eSopenharmony_ci{ 33053c3577eSopenharmony_ci int32_t result = Open(); 33153c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 33253c3577eSopenharmony_ci ZLOGE("Open store failed, result: %{public}d, appId: %{public}s, user: %{public}d", result, 33353c3577eSopenharmony_ci appId.c_str(), user); 33453c3577eSopenharmony_ci return STORE_NOT_OPEN; 33553c3577eSopenharmony_ci } 33653c3577eSopenharmony_ci result = RevokeSaveToStore(appId); 33753c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 33853c3577eSopenharmony_ci ZLOGE("Revoke save failed, result: %{public}d, appId: %{public}s, user: %{public}d", result, 33953c3577eSopenharmony_ci appId.c_str(), user); 34053c3577eSopenharmony_ci } 34153c3577eSopenharmony_ci Close(); 34253c3577eSopenharmony_ci std::string userId = std::to_string(user); 34353c3577eSopenharmony_ci std::string metaKey = GetMetaUserIdKey(userId, appId); 34453c3577eSopenharmony_ci auto status = DistributedData::MetaDataManager::GetInstance().DelMeta(metaKey, true); 34553c3577eSopenharmony_ci if (!status) { 34653c3577eSopenharmony_ci ZLOGE("Delete meta failed, userId: %{public}s, appId: %{public}s", userId.c_str(), appId.c_str()); 34753c3577eSopenharmony_ci } 34853c3577eSopenharmony_ci return result; 34953c3577eSopenharmony_ci} 35053c3577eSopenharmony_ci 35153c3577eSopenharmony_civoid ObjectStoreManager::RegisterRemoteCallback(const std::string &bundleName, const std::string &sessionId, 35253c3577eSopenharmony_ci pid_t pid, uint32_t tokenId, 35353c3577eSopenharmony_ci sptr<IRemoteObject> callback) 35453c3577eSopenharmony_ci{ 35553c3577eSopenharmony_ci if (bundleName.empty() || sessionId.empty()) { 35653c3577eSopenharmony_ci ZLOGD("ObjectStoreManager::RegisterRemoteCallback empty"); 35753c3577eSopenharmony_ci return; 35853c3577eSopenharmony_ci } 35953c3577eSopenharmony_ci ZLOGD("ObjectStoreManager::RegisterRemoteCallback start"); 36053c3577eSopenharmony_ci auto proxy = iface_cast<ObjectChangeCallbackProxy>(callback); 36153c3577eSopenharmony_ci std::string prefix = bundleName + sessionId; 36253c3577eSopenharmony_ci callbacks_.Compute(tokenId, ([pid, &proxy, &prefix](const uint32_t key, CallbackInfo &value) { 36353c3577eSopenharmony_ci if (value.pid != pid) { 36453c3577eSopenharmony_ci value = CallbackInfo { pid }; 36553c3577eSopenharmony_ci } 36653c3577eSopenharmony_ci value.observers_.insert_or_assign(prefix, proxy); 36753c3577eSopenharmony_ci return !value.observers_.empty(); 36853c3577eSopenharmony_ci })); 36953c3577eSopenharmony_ci} 37053c3577eSopenharmony_ci 37153c3577eSopenharmony_civoid ObjectStoreManager::UnregisterRemoteCallback(const std::string &bundleName, pid_t pid, uint32_t tokenId, 37253c3577eSopenharmony_ci const std::string &sessionId) 37353c3577eSopenharmony_ci{ 37453c3577eSopenharmony_ci if (bundleName.empty()) { 37553c3577eSopenharmony_ci ZLOGD("bundleName is empty"); 37653c3577eSopenharmony_ci return; 37753c3577eSopenharmony_ci } 37853c3577eSopenharmony_ci callbacks_.Compute(tokenId, ([pid, &sessionId, &bundleName](const uint32_t key, CallbackInfo &value) { 37953c3577eSopenharmony_ci if (value.pid != pid) { 38053c3577eSopenharmony_ci return true; 38153c3577eSopenharmony_ci } 38253c3577eSopenharmony_ci if (sessionId.empty()) { 38353c3577eSopenharmony_ci return false; 38453c3577eSopenharmony_ci } 38553c3577eSopenharmony_ci std::string prefix = bundleName + sessionId; 38653c3577eSopenharmony_ci for (auto it = value.observers_.begin(); it != value.observers_.end();) { 38753c3577eSopenharmony_ci if ((*it).first == prefix) { 38853c3577eSopenharmony_ci it = value.observers_.erase(it); 38953c3577eSopenharmony_ci } else { 39053c3577eSopenharmony_ci ++it; 39153c3577eSopenharmony_ci } 39253c3577eSopenharmony_ci } 39353c3577eSopenharmony_ci return true; 39453c3577eSopenharmony_ci })); 39553c3577eSopenharmony_ci} 39653c3577eSopenharmony_ci 39753c3577eSopenharmony_civoid ObjectStoreManager::NotifyChange(ObjectRecord &changedData) 39853c3577eSopenharmony_ci{ 39953c3577eSopenharmony_ci ZLOGI("OnChange start, size:%{public}zu", changedData.size()); 40053c3577eSopenharmony_ci bool hasAsset = false; 40153c3577eSopenharmony_ci SaveInfo saveInfo; 40253c3577eSopenharmony_ci for (const auto &[key, value] : changedData) { 40353c3577eSopenharmony_ci if (key.find(SAVE_INFO) != std::string::npos) { 40453c3577eSopenharmony_ci DistributedData::Serializable::Unmarshall(std::string(value.begin(), value.end()), saveInfo); 40553c3577eSopenharmony_ci break; 40653c3577eSopenharmony_ci } 40753c3577eSopenharmony_ci } 40853c3577eSopenharmony_ci auto data = GetObjectData(changedData, saveInfo, hasAsset); 40953c3577eSopenharmony_ci if (!hasAsset) { 41053c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStateStart(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE, 41153c3577eSopenharmony_ci ObjectStore::DATA_RECV, ObjectStore::RADAR_SUCCESS, ObjectStore::START, saveInfo.bundleName); 41253c3577eSopenharmony_ci callbacks_.ForEach([this, &data](uint32_t tokenId, const CallbackInfo& value) { 41353c3577eSopenharmony_ci DoNotify(tokenId, value, data, true); // no asset, data ready means all ready 41453c3577eSopenharmony_ci return false; 41553c3577eSopenharmony_ci }); 41653c3577eSopenharmony_ci return; 41753c3577eSopenharmony_ci } 41853c3577eSopenharmony_ci NotifyDataChanged(data, saveInfo); 41953c3577eSopenharmony_ci SaveUserToMeta(); 42053c3577eSopenharmony_ci} 42153c3577eSopenharmony_ci 42253c3577eSopenharmony_cistd::map<std::string, ObjectRecord> ObjectStoreManager::GetObjectData(const ObjectRecord& changedData, 42353c3577eSopenharmony_ci SaveInfo& saveInfo, bool& hasAsset) 42453c3577eSopenharmony_ci{ 42553c3577eSopenharmony_ci std::map<std::string, ObjectRecord> data; 42653c3577eSopenharmony_ci std::string keyPrefix = saveInfo.ToPropertyPrefix(); 42753c3577eSopenharmony_ci if (!keyPrefix.empty()) { 42853c3577eSopenharmony_ci std::string observerKey = saveInfo.bundleName + saveInfo.sessionId; 42953c3577eSopenharmony_ci for (const auto &[key, value] : changedData) { 43053c3577eSopenharmony_ci if (key.size() < keyPrefix.size() || key.find(SAVE_INFO) != std::string::npos) { 43153c3577eSopenharmony_ci continue; 43253c3577eSopenharmony_ci } 43353c3577eSopenharmony_ci std::string propertyName = key.substr(keyPrefix.size()); 43453c3577eSopenharmony_ci data[observerKey].insert_or_assign(propertyName, value); 43553c3577eSopenharmony_ci if (!hasAsset && IsAssetKey(propertyName)) { 43653c3577eSopenharmony_ci hasAsset = true; 43753c3577eSopenharmony_ci } 43853c3577eSopenharmony_ci } 43953c3577eSopenharmony_ci } else { 44053c3577eSopenharmony_ci for (const auto &item : changedData) { 44153c3577eSopenharmony_ci std::vector<std::string> splitKeys = SplitEntryKey(item.first); 44253c3577eSopenharmony_ci if (splitKeys.size() <= PROPERTY_NAME_INDEX) { 44353c3577eSopenharmony_ci continue; 44453c3577eSopenharmony_ci } 44553c3577eSopenharmony_ci if (saveInfo.sourceDeviceId.empty() || saveInfo.bundleName.empty()) { 44653c3577eSopenharmony_ci saveInfo.sourceDeviceId = splitKeys[SOURCE_DEVICE_UDID_INDEX]; 44753c3577eSopenharmony_ci saveInfo.bundleName = splitKeys[BUNDLE_NAME_INDEX]; 44853c3577eSopenharmony_ci saveInfo.sessionId = splitKeys[SESSION_ID_INDEX]; 44953c3577eSopenharmony_ci saveInfo.timestamp = splitKeys[TIME_INDEX]; 45053c3577eSopenharmony_ci } 45153c3577eSopenharmony_ci std::string prefix = splitKeys[BUNDLE_NAME_INDEX] + splitKeys[SESSION_ID_INDEX]; 45253c3577eSopenharmony_ci std::string propertyName = splitKeys[PROPERTY_NAME_INDEX]; 45353c3577eSopenharmony_ci data[prefix].insert_or_assign(propertyName, item.second); 45453c3577eSopenharmony_ci if (IsAssetKey(propertyName)) { 45553c3577eSopenharmony_ci hasAsset = true; 45653c3577eSopenharmony_ci } 45753c3577eSopenharmony_ci } 45853c3577eSopenharmony_ci } 45953c3577eSopenharmony_ci return data; 46053c3577eSopenharmony_ci} 46153c3577eSopenharmony_ci 46253c3577eSopenharmony_civoid ObjectStoreManager::ComputeStatus(const std::string& objectKey, const SaveInfo& saveInfo, 46353c3577eSopenharmony_ci const std::map<std::string, ObjectRecord>& data) 46453c3577eSopenharmony_ci{ 46553c3577eSopenharmony_ci restoreStatus_.Compute(objectKey, [this, &data, saveInfo] (const auto &key, auto &value) { 46653c3577eSopenharmony_ci if (value == RestoreStatus::ASSETS_READY) { 46753c3577eSopenharmony_ci value = RestoreStatus::ALL_READY; 46853c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStage(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE, 46953c3577eSopenharmony_ci ObjectStore::DATA_RECV, ObjectStore::RADAR_SUCCESS); 47053c3577eSopenharmony_ci callbacks_.ForEach([this, &data](uint32_t tokenId, const CallbackInfo& value) { 47153c3577eSopenharmony_ci DoNotify(tokenId, value, data, true); 47253c3577eSopenharmony_ci return false; 47353c3577eSopenharmony_ci }); 47453c3577eSopenharmony_ci } else { 47553c3577eSopenharmony_ci value = RestoreStatus::DATA_READY; 47653c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStateStart(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE, 47753c3577eSopenharmony_ci ObjectStore::DATA_RECV, ObjectStore::RADAR_SUCCESS, ObjectStore::START, saveInfo.bundleName); 47853c3577eSopenharmony_ci callbacks_.ForEach([this, &data](uint32_t tokenId, const CallbackInfo& value) { 47953c3577eSopenharmony_ci DoNotify(tokenId, value, data, false); 48053c3577eSopenharmony_ci return false; 48153c3577eSopenharmony_ci }); 48253c3577eSopenharmony_ci WaitAssets(key, saveInfo, data); 48353c3577eSopenharmony_ci } 48453c3577eSopenharmony_ci return true; 48553c3577eSopenharmony_ci }); 48653c3577eSopenharmony_ci} 48753c3577eSopenharmony_ci 48853c3577eSopenharmony_civoid ObjectStoreManager::NotifyDataChanged(const std::map<std::string, ObjectRecord>& data, const SaveInfo& saveInfo) 48953c3577eSopenharmony_ci{ 49053c3577eSopenharmony_ci for (auto const& [objectKey, results] : data) { 49153c3577eSopenharmony_ci restoreStatus_.ComputeIfAbsent( 49253c3577eSopenharmony_ci objectKey, [](const std::string& key) -> auto { 49353c3577eSopenharmony_ci return RestoreStatus::NONE; 49453c3577eSopenharmony_ci }); 49553c3577eSopenharmony_ci ComputeStatus(objectKey, saveInfo, data); 49653c3577eSopenharmony_ci } 49753c3577eSopenharmony_ci} 49853c3577eSopenharmony_ci 49953c3577eSopenharmony_ciint32_t ObjectStoreManager::WaitAssets(const std::string& objectKey, const SaveInfo& saveInfo, 50053c3577eSopenharmony_ci const std::map<std::string, ObjectRecord>& data) 50153c3577eSopenharmony_ci{ 50253c3577eSopenharmony_ci auto taskId = executors_->Schedule(std::chrono::seconds(WAIT_TIME), [this, objectKey, data, saveInfo]() { 50353c3577eSopenharmony_ci ZLOGE("wait assets finisehd timeout, try pull assets, objectKey:%{public}s", objectKey.c_str()); 50453c3577eSopenharmony_ci PullAssets(data, saveInfo); 50553c3577eSopenharmony_ci DoNotifyWaitAssetTimeout(objectKey); 50653c3577eSopenharmony_ci }); 50753c3577eSopenharmony_ci 50853c3577eSopenharmony_ci objectTimer_.ComputeIfAbsent( 50953c3577eSopenharmony_ci objectKey, [taskId](const std::string& key) -> auto { 51053c3577eSopenharmony_ci return taskId; 51153c3577eSopenharmony_ci }); 51253c3577eSopenharmony_ci return OBJECT_SUCCESS; 51353c3577eSopenharmony_ci} 51453c3577eSopenharmony_ci 51553c3577eSopenharmony_civoid ObjectStoreManager::PullAssets(const std::map<std::string, ObjectRecord>& data, const SaveInfo& saveInfo) 51653c3577eSopenharmony_ci{ 51753c3577eSopenharmony_ci std::map<std::string, Assets> changedAssets; 51853c3577eSopenharmony_ci for (auto const& [objectId, result] : data) { 51953c3577eSopenharmony_ci changedAssets[objectId] = GetAssetsFromDBRecords(result); 52053c3577eSopenharmony_ci } 52153c3577eSopenharmony_ci for (const auto& [objectId, assets] : changedAssets) { 52253c3577eSopenharmony_ci std::string networkId = DmAdaper::GetInstance().ToNetworkID(saveInfo.sourceDeviceId); 52353c3577eSopenharmony_ci auto block = std::make_shared<BlockData<std::tuple<bool, bool>>>(WAIT_TIME, std::tuple{ true, true }); 52453c3577eSopenharmony_ci ObjectAssetLoader::GetInstance()->TransferAssetsAsync(std::stoi(GetCurrentUser()), 52553c3577eSopenharmony_ci saveInfo.bundleName, networkId, assets, [this, block](bool success) { 52653c3577eSopenharmony_ci block->SetValue({ false, success }); 52753c3577eSopenharmony_ci }); 52853c3577eSopenharmony_ci auto [timeout, success] = block->GetValue(); 52953c3577eSopenharmony_ci ZLOGI("Pull assets end, timeout: %{public}d, success: %{public}d, size:%{public}zu, deviceId: %{public}s", 53053c3577eSopenharmony_ci timeout, success, assets.size(), DistributedData::Anonymous::Change(networkId).c_str()); 53153c3577eSopenharmony_ci } 53253c3577eSopenharmony_ci} 53353c3577eSopenharmony_ci 53453c3577eSopenharmony_civoid ObjectStoreManager::NotifyAssetsReady(const std::string& objectKey, const std::string& bundleName, 53553c3577eSopenharmony_ci const std::string& srcNetworkId) 53653c3577eSopenharmony_ci{ 53753c3577eSopenharmony_ci restoreStatus_.ComputeIfAbsent( 53853c3577eSopenharmony_ci objectKey, [](const std::string& key) -> auto { 53953c3577eSopenharmony_ci return RestoreStatus::NONE; 54053c3577eSopenharmony_ci }); 54153c3577eSopenharmony_ci restoreStatus_.Compute(objectKey, [this, &bundleName] (const auto &key, auto &value) { 54253c3577eSopenharmony_ci if (value == RestoreStatus::DATA_NOTIFIED) { 54353c3577eSopenharmony_ci value = RestoreStatus::ALL_READY; 54453c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStage(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE, 54553c3577eSopenharmony_ci ObjectStore::ASSETS_RECV, ObjectStore::RADAR_SUCCESS); 54653c3577eSopenharmony_ci callbacks_.ForEach([this, key](uint32_t tokenId, const CallbackInfo& value) { 54753c3577eSopenharmony_ci DoNotifyAssetsReady(tokenId, value, key, true); 54853c3577eSopenharmony_ci return false; 54953c3577eSopenharmony_ci }); 55053c3577eSopenharmony_ci } else if (value == RestoreStatus::DATA_READY) { 55153c3577eSopenharmony_ci value = RestoreStatus::ALL_READY; 55253c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStage(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE, 55353c3577eSopenharmony_ci ObjectStore::ASSETS_RECV, ObjectStore::RADAR_SUCCESS); 55453c3577eSopenharmony_ci auto [has, taskId] = objectTimer_.Find(key); 55553c3577eSopenharmony_ci if (has) { 55653c3577eSopenharmony_ci executors_->Remove(taskId); 55753c3577eSopenharmony_ci objectTimer_.Erase(key); 55853c3577eSopenharmony_ci } 55953c3577eSopenharmony_ci } else { 56053c3577eSopenharmony_ci value = RestoreStatus::ASSETS_READY; 56153c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStateStart(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE, 56253c3577eSopenharmony_ci ObjectStore::ASSETS_RECV, ObjectStore::RADAR_SUCCESS, ObjectStore::START, bundleName); 56353c3577eSopenharmony_ci } 56453c3577eSopenharmony_ci return true; 56553c3577eSopenharmony_ci }); 56653c3577eSopenharmony_ci} 56753c3577eSopenharmony_ci 56853c3577eSopenharmony_civoid ObjectStoreManager::NotifyAssetsStart(const std::string& objectKey, const std::string& srcNetworkId) 56953c3577eSopenharmony_ci{ 57053c3577eSopenharmony_ci restoreStatus_.ComputeIfAbsent( 57153c3577eSopenharmony_ci objectKey, [](const std::string& key) -> auto { 57253c3577eSopenharmony_ci return RestoreStatus::NONE; 57353c3577eSopenharmony_ci }); 57453c3577eSopenharmony_ci} 57553c3577eSopenharmony_ci 57653c3577eSopenharmony_cibool ObjectStoreManager::IsAssetKey(const std::string& key) 57753c3577eSopenharmony_ci{ 57853c3577eSopenharmony_ci return key.find(ObjectStore::ASSET_DOT) != std::string::npos; 57953c3577eSopenharmony_ci} 58053c3577eSopenharmony_ci 58153c3577eSopenharmony_cibool ObjectStoreManager::IsAssetComplete(const ObjectRecord& result, const std::string& assetPrefix) 58253c3577eSopenharmony_ci{ 58353c3577eSopenharmony_ci if (result.find(assetPrefix + ObjectStore::NAME_SUFFIX) == result.end() || 58453c3577eSopenharmony_ci result.find(assetPrefix + ObjectStore::URI_SUFFIX) == result.end() || 58553c3577eSopenharmony_ci result.find(assetPrefix + ObjectStore::PATH_SUFFIX) == result.end() || 58653c3577eSopenharmony_ci result.find(assetPrefix + ObjectStore::CREATE_TIME_SUFFIX) == result.end() || 58753c3577eSopenharmony_ci result.find(assetPrefix + ObjectStore::MODIFY_TIME_SUFFIX) == result.end() || 58853c3577eSopenharmony_ci result.find(assetPrefix + ObjectStore::SIZE_SUFFIX) == result.end()) { 58953c3577eSopenharmony_ci return false; 59053c3577eSopenharmony_ci } 59153c3577eSopenharmony_ci return true; 59253c3577eSopenharmony_ci} 59353c3577eSopenharmony_ci 59453c3577eSopenharmony_ciAssets ObjectStoreManager::GetAssetsFromDBRecords(const ObjectRecord& result) 59553c3577eSopenharmony_ci{ 59653c3577eSopenharmony_ci Assets assets{}; 59753c3577eSopenharmony_ci std::set<std::string> assetKey; 59853c3577eSopenharmony_ci for (const auto& [key, value] : result) { 59953c3577eSopenharmony_ci std::string assetPrefix = key.substr(0, key.find(ObjectStore::ASSET_DOT)); 60053c3577eSopenharmony_ci if (!IsAssetKey(key) || assetKey.find(assetPrefix) != assetKey.end() || 60153c3577eSopenharmony_ci result.find(assetPrefix + ObjectStore::NAME_SUFFIX) == result.end() || 60253c3577eSopenharmony_ci result.find(assetPrefix + ObjectStore::URI_SUFFIX) == result.end()) { 60353c3577eSopenharmony_ci continue; 60453c3577eSopenharmony_ci } 60553c3577eSopenharmony_ci Asset asset; 60653c3577eSopenharmony_ci ObjectStore::StringUtils::BytesToStrWithType( 60753c3577eSopenharmony_ci result.find(assetPrefix + ObjectStore::NAME_SUFFIX)->second, asset.name); 60853c3577eSopenharmony_ci if (asset.name.find(ObjectStore::STRING_PREFIX) != std::string::npos) { 60953c3577eSopenharmony_ci asset.name = asset.name.substr(ObjectStore::STRING_PREFIX_LEN); 61053c3577eSopenharmony_ci } 61153c3577eSopenharmony_ci ObjectStore::StringUtils::BytesToStrWithType( 61253c3577eSopenharmony_ci result.find(assetPrefix + ObjectStore::URI_SUFFIX)->second, asset.uri); 61353c3577eSopenharmony_ci if (asset.uri.find(ObjectStore::STRING_PREFIX) != std::string::npos) { 61453c3577eSopenharmony_ci asset.uri = asset.uri.substr(ObjectStore::STRING_PREFIX_LEN); 61553c3577eSopenharmony_ci } 61653c3577eSopenharmony_ci ObjectStore::StringUtils::BytesToStrWithType( 61753c3577eSopenharmony_ci result.find(assetPrefix + ObjectStore::MODIFY_TIME_SUFFIX)->second, asset.modifyTime); 61853c3577eSopenharmony_ci if (asset.modifyTime.find(ObjectStore::STRING_PREFIX) != std::string::npos) { 61953c3577eSopenharmony_ci asset.modifyTime = asset.modifyTime.substr(ObjectStore::STRING_PREFIX_LEN); 62053c3577eSopenharmony_ci } 62153c3577eSopenharmony_ci ObjectStore::StringUtils::BytesToStrWithType( 62253c3577eSopenharmony_ci result.find(assetPrefix + ObjectStore::SIZE_SUFFIX)->second, asset.size); 62353c3577eSopenharmony_ci if (asset.size.find(ObjectStore::STRING_PREFIX) != std::string::npos) { 62453c3577eSopenharmony_ci asset.size = asset.size.substr(ObjectStore::STRING_PREFIX_LEN); 62553c3577eSopenharmony_ci } 62653c3577eSopenharmony_ci asset.hash = asset.modifyTime + "_" + asset.size; 62753c3577eSopenharmony_ci assets.push_back(asset); 62853c3577eSopenharmony_ci assetKey.insert(assetPrefix); 62953c3577eSopenharmony_ci } 63053c3577eSopenharmony_ci return assets; 63153c3577eSopenharmony_ci} 63253c3577eSopenharmony_ci 63353c3577eSopenharmony_civoid ObjectStoreManager::DoNotify(uint32_t tokenId, const CallbackInfo& value, 63453c3577eSopenharmony_ci const std::map<std::string, ObjectRecord>& data, bool allReady) 63553c3577eSopenharmony_ci{ 63653c3577eSopenharmony_ci for (const auto& observer : value.observers_) { 63753c3577eSopenharmony_ci auto it = data.find(observer.first); 63853c3577eSopenharmony_ci if (it == data.end()) { 63953c3577eSopenharmony_ci continue; 64053c3577eSopenharmony_ci } 64153c3577eSopenharmony_ci observer.second->Completed((*it).second, allReady); 64253c3577eSopenharmony_ci if (allReady) { 64353c3577eSopenharmony_ci restoreStatus_.Erase(observer.first); 64453c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStateFinished(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE, 64553c3577eSopenharmony_ci ObjectStore::NOTIFY, ObjectStore::RADAR_SUCCESS, ObjectStore::FINISHED); 64653c3577eSopenharmony_ci } else { 64753c3577eSopenharmony_ci restoreStatus_.ComputeIfPresent(observer.first, [](const auto &key, auto &value) { 64853c3577eSopenharmony_ci value = RestoreStatus::DATA_NOTIFIED; 64953c3577eSopenharmony_ci return true; 65053c3577eSopenharmony_ci }); 65153c3577eSopenharmony_ci } 65253c3577eSopenharmony_ci } 65353c3577eSopenharmony_ci} 65453c3577eSopenharmony_ci 65553c3577eSopenharmony_civoid ObjectStoreManager::DoNotifyAssetsReady(uint32_t tokenId, const CallbackInfo& value, 65653c3577eSopenharmony_ci const std::string& objectKey, bool allReady) 65753c3577eSopenharmony_ci{ 65853c3577eSopenharmony_ci for (const auto& observer : value.observers_) { 65953c3577eSopenharmony_ci if (objectKey != observer.first) { 66053c3577eSopenharmony_ci continue; 66153c3577eSopenharmony_ci } 66253c3577eSopenharmony_ci observer.second->Completed(ObjectRecord(), allReady); 66353c3577eSopenharmony_ci if (allReady) { 66453c3577eSopenharmony_ci restoreStatus_.Erase(objectKey); 66553c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStateFinished(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE, 66653c3577eSopenharmony_ci ObjectStore::NOTIFY, ObjectStore::RADAR_SUCCESS, ObjectStore::FINISHED); 66753c3577eSopenharmony_ci } 66853c3577eSopenharmony_ci auto [has, taskId] = objectTimer_.Find(objectKey); 66953c3577eSopenharmony_ci if (has) { 67053c3577eSopenharmony_ci executors_->Remove(taskId); 67153c3577eSopenharmony_ci objectTimer_.Erase(objectKey); 67253c3577eSopenharmony_ci } 67353c3577eSopenharmony_ci } 67453c3577eSopenharmony_ci} 67553c3577eSopenharmony_ci 67653c3577eSopenharmony_civoid ObjectStoreManager::DoNotifyWaitAssetTimeout(const std::string &objectKey) 67753c3577eSopenharmony_ci{ 67853c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStageError(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE, 67953c3577eSopenharmony_ci ObjectStore::ASSETS_RECV, ObjectStore::RADAR_FAILED, ObjectStore::TIMEOUT); 68053c3577eSopenharmony_ci callbacks_.ForEach([this, &objectKey](uint32_t tokenId, const CallbackInfo &value) { 68153c3577eSopenharmony_ci for (const auto& observer : value.observers_) { 68253c3577eSopenharmony_ci if (objectKey != observer.first) { 68353c3577eSopenharmony_ci continue; 68453c3577eSopenharmony_ci } 68553c3577eSopenharmony_ci observer.second->Completed(ObjectRecord(), true); 68653c3577eSopenharmony_ci restoreStatus_.Erase(objectKey); 68753c3577eSopenharmony_ci auto [has, taskId] = objectTimer_.Find(objectKey); 68853c3577eSopenharmony_ci if (has) { 68953c3577eSopenharmony_ci executors_->Remove(taskId); 69053c3577eSopenharmony_ci objectTimer_.Erase(objectKey); 69153c3577eSopenharmony_ci } 69253c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStateError(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE, 69353c3577eSopenharmony_ci ObjectStore::NOTIFY, ObjectStore::RADAR_FAILED, ObjectStore::TIMEOUT, ObjectStore::FINISHED); 69453c3577eSopenharmony_ci } 69553c3577eSopenharmony_ci return false; 69653c3577eSopenharmony_ci }); 69753c3577eSopenharmony_ci} 69853c3577eSopenharmony_ci 69953c3577eSopenharmony_civoid ObjectStoreManager::SetData(const std::string &dataDir, const std::string &userId) 70053c3577eSopenharmony_ci{ 70153c3577eSopenharmony_ci ZLOGI("enter %{public}s", dataDir.c_str()); 70253c3577eSopenharmony_ci kvStoreDelegateManager_ = 70353c3577eSopenharmony_ci new DistributedDB::KvStoreDelegateManager(DistributedData::Bootstrap::GetInstance().GetProcessLabel(), userId); 70453c3577eSopenharmony_ci DistributedDB::KvStoreConfig kvStoreConfig { dataDir }; 70553c3577eSopenharmony_ci kvStoreDelegateManager_->SetKvStoreConfig(kvStoreConfig); 70653c3577eSopenharmony_ci userId_ = userId; 70753c3577eSopenharmony_ci} 70853c3577eSopenharmony_ci 70953c3577eSopenharmony_ciint32_t ObjectStoreManager::Open() 71053c3577eSopenharmony_ci{ 71153c3577eSopenharmony_ci if (kvStoreDelegateManager_ == nullptr) { 71253c3577eSopenharmony_ci ZLOGE("Kvstore delegate manager not init"); 71353c3577eSopenharmony_ci return OBJECT_INNER_ERROR; 71453c3577eSopenharmony_ci } 71553c3577eSopenharmony_ci std::lock_guard<std::recursive_mutex> lock(kvStoreMutex_); 71653c3577eSopenharmony_ci if (delegate_ == nullptr) { 71753c3577eSopenharmony_ci delegate_ = OpenObjectKvStore(); 71853c3577eSopenharmony_ci if (delegate_ == nullptr) { 71953c3577eSopenharmony_ci ZLOGE("Open object kvstore failed"); 72053c3577eSopenharmony_ci return OBJECT_DBSTATUS_ERROR; 72153c3577eSopenharmony_ci } 72253c3577eSopenharmony_ci syncCount_ = 1; 72353c3577eSopenharmony_ci ZLOGI("Open object kvstore success"); 72453c3577eSopenharmony_ci } else { 72553c3577eSopenharmony_ci syncCount_++; 72653c3577eSopenharmony_ci ZLOGI("Object kvstore syncCount: %{public}d", syncCount_); 72753c3577eSopenharmony_ci } 72853c3577eSopenharmony_ci return OBJECT_SUCCESS; 72953c3577eSopenharmony_ci} 73053c3577eSopenharmony_ci 73153c3577eSopenharmony_civoid ObjectStoreManager::Close() 73253c3577eSopenharmony_ci{ 73353c3577eSopenharmony_ci std::lock_guard<std::recursive_mutex> lock(kvStoreMutex_); 73453c3577eSopenharmony_ci if (delegate_ == nullptr) { 73553c3577eSopenharmony_ci return; 73653c3577eSopenharmony_ci } 73753c3577eSopenharmony_ci int32_t taskCount = delegate_->GetTaskCount(); 73853c3577eSopenharmony_ci if (taskCount > 0 && syncCount_ == 1) { 73953c3577eSopenharmony_ci CloseAfterMinute(); 74053c3577eSopenharmony_ci ZLOGW("Store is busy, close after a minute, task count: %{public}d", taskCount); 74153c3577eSopenharmony_ci return; 74253c3577eSopenharmony_ci } 74353c3577eSopenharmony_ci syncCount_--; 74453c3577eSopenharmony_ci ZLOGI("closed a store, syncCount = %{public}d", syncCount_); 74553c3577eSopenharmony_ci FlushClosedStore(); 74653c3577eSopenharmony_ci} 74753c3577eSopenharmony_ci 74853c3577eSopenharmony_civoid ObjectStoreManager::SyncCompleted( 74953c3577eSopenharmony_ci const std::map<std::string, DistributedDB::DBStatus> &results, uint64_t sequenceId) 75053c3577eSopenharmony_ci{ 75153c3577eSopenharmony_ci std::string userId; 75253c3577eSopenharmony_ci SequenceSyncManager::Result result = SequenceSyncManager::GetInstance()->Process(sequenceId, results, userId); 75353c3577eSopenharmony_ci if (result == SequenceSyncManager::SUCCESS_USER_HAS_FINISHED && userId == userId_) { 75453c3577eSopenharmony_ci std::lock_guard<std::recursive_mutex> lock(kvStoreMutex_); 75553c3577eSopenharmony_ci SetSyncStatus(false); 75653c3577eSopenharmony_ci FlushClosedStore(); 75753c3577eSopenharmony_ci } 75853c3577eSopenharmony_ci for (const auto &item : results) { 75953c3577eSopenharmony_ci if (item.second == DistributedDB::DBStatus::OK) { 76053c3577eSopenharmony_ci ZLOGI("Sync data success, sequenceId: 0x%{public}" PRIx64 "", sequenceId); 76153c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStateFinished(std::string(__FUNCTION__), ObjectStore::SAVE, 76253c3577eSopenharmony_ci ObjectStore::SYNC_DATA, ObjectStore::RADAR_SUCCESS, ObjectStore::FINISHED); 76353c3577eSopenharmony_ci } else { 76453c3577eSopenharmony_ci ZLOGE("Sync data failed, sequenceId: 0x%{public}" PRIx64 ", status: %{public}d", sequenceId, item.second); 76553c3577eSopenharmony_ci ObjectStore::RadarReporter::ReportStateError(std::string(__FUNCTION__), ObjectStore::SAVE, 76653c3577eSopenharmony_ci ObjectStore::SYNC_DATA, ObjectStore::RADAR_FAILED, item.second, ObjectStore::FINISHED); 76753c3577eSopenharmony_ci } 76853c3577eSopenharmony_ci } 76953c3577eSopenharmony_ci} 77053c3577eSopenharmony_ci 77153c3577eSopenharmony_civoid ObjectStoreManager::FlushClosedStore() 77253c3577eSopenharmony_ci{ 77353c3577eSopenharmony_ci std::lock_guard<std::recursive_mutex> lock(kvStoreMutex_); 77453c3577eSopenharmony_ci if (!isSyncing_ && syncCount_ == 0 && delegate_ != nullptr) { 77553c3577eSopenharmony_ci ZLOGD("close store"); 77653c3577eSopenharmony_ci auto status = kvStoreDelegateManager_->CloseKvStore(delegate_); 77753c3577eSopenharmony_ci if (status != DistributedDB::DBStatus::OK) { 77853c3577eSopenharmony_ci int timeOut = 1000; 77953c3577eSopenharmony_ci executors_->Schedule(std::chrono::milliseconds(timeOut), [this]() { 78053c3577eSopenharmony_ci FlushClosedStore(); 78153c3577eSopenharmony_ci }); 78253c3577eSopenharmony_ci ZLOGE("GetEntries fail %{public}d", status); 78353c3577eSopenharmony_ci return; 78453c3577eSopenharmony_ci } 78553c3577eSopenharmony_ci delegate_ = nullptr; 78653c3577eSopenharmony_ci if (objectDataListener_ != nullptr) { 78753c3577eSopenharmony_ci delete objectDataListener_; 78853c3577eSopenharmony_ci objectDataListener_ = nullptr; 78953c3577eSopenharmony_ci } 79053c3577eSopenharmony_ci } 79153c3577eSopenharmony_ci} 79253c3577eSopenharmony_ci 79353c3577eSopenharmony_civoid ObjectStoreManager::ProcessOldEntry(const std::string &appId) 79453c3577eSopenharmony_ci{ 79553c3577eSopenharmony_ci std::vector<DistributedDB::Entry> entries; 79653c3577eSopenharmony_ci auto status = delegate_->GetEntries(std::vector<uint8_t>(appId.begin(), appId.end()), entries); 79753c3577eSopenharmony_ci if (status != DistributedDB::DBStatus::NOT_FOUND) { 79853c3577eSopenharmony_ci ZLOGI("Get old entries empty, bundleName: %{public}s", appId.c_str()); 79953c3577eSopenharmony_ci return; 80053c3577eSopenharmony_ci } 80153c3577eSopenharmony_ci if (status != DistributedDB::DBStatus::OK) { 80253c3577eSopenharmony_ci ZLOGE("Get old entries failed, bundleName: %{public}s, status %{public}d", appId.c_str(), status); 80353c3577eSopenharmony_ci return; 80453c3577eSopenharmony_ci } 80553c3577eSopenharmony_ci std::map<std::string, int64_t> sessionIds; 80653c3577eSopenharmony_ci int64_t oldestTime = 0; 80753c3577eSopenharmony_ci std::string deleteKey; 80853c3577eSopenharmony_ci for (auto &item : entries) { 80953c3577eSopenharmony_ci std::string key(item.key.begin(), item.key.end()); 81053c3577eSopenharmony_ci std::vector<std::string> splitKeys = SplitEntryKey(key); 81153c3577eSopenharmony_ci if (splitKeys.empty()) { 81253c3577eSopenharmony_ci continue; 81353c3577eSopenharmony_ci } 81453c3577eSopenharmony_ci std::string bundleName = splitKeys[BUNDLE_NAME_INDEX]; 81553c3577eSopenharmony_ci std::string sessionId = splitKeys[SESSION_ID_INDEX]; 81653c3577eSopenharmony_ci if (sessionIds.count(sessionId) == 0) { 81753c3577eSopenharmony_ci char *end = nullptr; 81853c3577eSopenharmony_ci sessionIds[sessionId] = strtol(splitKeys[TIME_INDEX].c_str(), &end, DECIMAL_BASE); 81953c3577eSopenharmony_ci } 82053c3577eSopenharmony_ci if (oldestTime == 0 || oldestTime > sessionIds[sessionId]) { 82153c3577eSopenharmony_ci oldestTime = sessionIds[sessionId]; 82253c3577eSopenharmony_ci deleteKey = GetPrefixWithoutDeviceId(bundleName, sessionId); 82353c3577eSopenharmony_ci } 82453c3577eSopenharmony_ci } 82553c3577eSopenharmony_ci if (sessionIds.size() < MAX_OBJECT_SIZE_PER_APP) { 82653c3577eSopenharmony_ci return; 82753c3577eSopenharmony_ci } 82853c3577eSopenharmony_ci int32_t result = RevokeSaveToStore(deleteKey); 82953c3577eSopenharmony_ci if (result != OBJECT_SUCCESS) { 83053c3577eSopenharmony_ci ZLOGE("Delete old entries failed, deleteKey: %{public}s, status: %{public}d", deleteKey.c_str(), result); 83153c3577eSopenharmony_ci return; 83253c3577eSopenharmony_ci } 83353c3577eSopenharmony_ci ZLOGI("Delete old entries success, deleteKey: %{public}s", deleteKey.c_str()); 83453c3577eSopenharmony_ci} 83553c3577eSopenharmony_ci 83653c3577eSopenharmony_ciint32_t ObjectStoreManager::SaveToStore(const std::string &appId, const std::string &sessionId, 83753c3577eSopenharmony_ci const std::string &toDeviceId, const ObjectRecord &data) 83853c3577eSopenharmony_ci{ 83953c3577eSopenharmony_ci ProcessOldEntry(appId); 84053c3577eSopenharmony_ci RevokeSaveToStore(GetPropertyPrefix(appId, sessionId, toDeviceId)); 84153c3577eSopenharmony_ci std::string timestamp = std::to_string(GetSecondsSince1970ToNow()); 84253c3577eSopenharmony_ci std::string prefix = GetPropertyPrefix(appId, sessionId, toDeviceId) + timestamp + SEPERATOR; 84353c3577eSopenharmony_ci DistributedDB::Entry saveInfoEntry; 84453c3577eSopenharmony_ci std::string saveInfoKey = prefix + SAVE_INFO; 84553c3577eSopenharmony_ci saveInfoEntry.key = std::vector<uint8_t>(saveInfoKey.begin(), saveInfoKey.end()); 84653c3577eSopenharmony_ci SaveInfo saveInfo(appId, sessionId, DmAdaper::GetInstance().GetLocalDevice().udid, toDeviceId, timestamp); 84753c3577eSopenharmony_ci std::string saveInfoValue = DistributedData::Serializable::Marshall(saveInfo); 84853c3577eSopenharmony_ci saveInfoEntry.value = std::vector<uint8_t>(saveInfoValue.begin(), saveInfoValue.end()); 84953c3577eSopenharmony_ci std::vector<DistributedDB::Entry> entries; 85053c3577eSopenharmony_ci entries.emplace_back(saveInfoEntry); 85153c3577eSopenharmony_ci for (auto &item : data) { 85253c3577eSopenharmony_ci DistributedDB::Entry entry; 85353c3577eSopenharmony_ci std::string key = GetPropertyPrefix(appId, sessionId, toDeviceId) + timestamp + SEPERATOR + item.first; 85453c3577eSopenharmony_ci entry.key = std::vector<uint8_t>(key.begin(), key.end()); 85553c3577eSopenharmony_ci entry.value = item.second; 85653c3577eSopenharmony_ci entries.emplace_back(entry); 85753c3577eSopenharmony_ci } 85853c3577eSopenharmony_ci auto status = delegate_->PutBatch(entries); 85953c3577eSopenharmony_ci if (status != DistributedDB::DBStatus::OK) { 86053c3577eSopenharmony_ci ZLOGE("PutBatch failed, bundleName: %{public}s, sessionId: %{public}s, dstNetworkId: %{public}s, " 86153c3577eSopenharmony_ci "status: %{public}d", appId.c_str(), sessionId.c_str(), Anonymous::Change(toDeviceId).c_str(), status); 86253c3577eSopenharmony_ci return status; 86353c3577eSopenharmony_ci } 86453c3577eSopenharmony_ci ZLOGI("PutBatch success, bundleName: %{public}s, sessionId: %{public}s, dstNetworkId: %{public}s, " 86553c3577eSopenharmony_ci "count: %{public}zu", appId.c_str(), sessionId.c_str(), Anonymous::Change(toDeviceId).c_str(), entries.size()); 86653c3577eSopenharmony_ci return OBJECT_SUCCESS; 86753c3577eSopenharmony_ci} 86853c3577eSopenharmony_ci 86953c3577eSopenharmony_ciint32_t ObjectStoreManager::SyncOnStore( 87053c3577eSopenharmony_ci const std::string &prefix, const std::vector<std::string> &deviceList, SyncCallBack &callback) 87153c3577eSopenharmony_ci{ 87253c3577eSopenharmony_ci std::vector<std::string> syncDevices; 87353c3577eSopenharmony_ci for (auto &device : deviceList) { 87453c3577eSopenharmony_ci if (device == LOCAL_DEVICE) { 87553c3577eSopenharmony_ci ZLOGI("Save to local, do not need sync, prefix: %{public}s", prefix.c_str()); 87653c3577eSopenharmony_ci callback({{LOCAL_DEVICE, OBJECT_SUCCESS}}); 87753c3577eSopenharmony_ci return OBJECT_SUCCESS; 87853c3577eSopenharmony_ci } 87953c3577eSopenharmony_ci syncDevices.emplace_back(DmAdaper::GetInstance().GetUuidByNetworkId(device)); 88053c3577eSopenharmony_ci } 88153c3577eSopenharmony_ci if (syncDevices.empty()) { 88253c3577eSopenharmony_ci ZLOGI("Device list is empty, prefix: %{public}s", Anonymous::Change(prefix).c_str()); 88353c3577eSopenharmony_ci callback(std::map<std::string, int32_t>()); 88453c3577eSopenharmony_ci return OBJECT_SUCCESS; 88553c3577eSopenharmony_ci } 88653c3577eSopenharmony_ci uint64_t sequenceId = SequenceSyncManager::GetInstance()->AddNotifier(userId_, callback); 88753c3577eSopenharmony_ci DistributedDB::Query dbQuery = DistributedDB::Query::Select(); 88853c3577eSopenharmony_ci dbQuery.PrefixKey(std::vector<uint8_t>(prefix.begin(), prefix.end())); 88953c3577eSopenharmony_ci ZLOGI("Start sync data, sequenceId: 0x%{public}" PRIx64 "", sequenceId); 89053c3577eSopenharmony_ci auto status = delegate_->Sync(syncDevices, DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY, 89153c3577eSopenharmony_ci [this, sequenceId](const std::map<std::string, DistributedDB::DBStatus> &devicesMap) { 89253c3577eSopenharmony_ci ZLOGI("Sync data finished, sequenceId: 0x%{public}" PRIx64 "", sequenceId); 89353c3577eSopenharmony_ci std::map<std::string, DistributedDB::DBStatus> result; 89453c3577eSopenharmony_ci for (auto &item : devicesMap) { 89553c3577eSopenharmony_ci result[DmAdaper::GetInstance().ToNetworkID(item.first)] = item.second; 89653c3577eSopenharmony_ci } 89753c3577eSopenharmony_ci SyncCompleted(result, sequenceId); 89853c3577eSopenharmony_ci }, dbQuery, false); 89953c3577eSopenharmony_ci if (status != DistributedDB::DBStatus::OK) { 90053c3577eSopenharmony_ci ZLOGE("Sync data failed, prefix: %{public}s, sequenceId: 0x%{public}" PRIx64 ", status: %{public}d", 90153c3577eSopenharmony_ci Anonymous::Change(prefix).c_str(), sequenceId, status); 90253c3577eSopenharmony_ci std::string tmp; 90353c3577eSopenharmony_ci SequenceSyncManager::GetInstance()->DeleteNotifier(sequenceId, tmp); 90453c3577eSopenharmony_ci return status; 90553c3577eSopenharmony_ci } 90653c3577eSopenharmony_ci SetSyncStatus(true); 90753c3577eSopenharmony_ci return OBJECT_SUCCESS; 90853c3577eSopenharmony_ci} 90953c3577eSopenharmony_ci 91053c3577eSopenharmony_ciint32_t ObjectStoreManager::SetSyncStatus(bool status) 91153c3577eSopenharmony_ci{ 91253c3577eSopenharmony_ci std::lock_guard<std::recursive_mutex> lock(kvStoreMutex_); 91353c3577eSopenharmony_ci isSyncing_ = status; 91453c3577eSopenharmony_ci return OBJECT_SUCCESS; 91553c3577eSopenharmony_ci} 91653c3577eSopenharmony_ci 91753c3577eSopenharmony_ciint32_t ObjectStoreManager::RevokeSaveToStore(const std::string &prefix) 91853c3577eSopenharmony_ci{ 91953c3577eSopenharmony_ci std::vector<DistributedDB::Entry> entries; 92053c3577eSopenharmony_ci auto status = delegate_->GetEntries(std::vector<uint8_t>(prefix.begin(), prefix.end()), entries); 92153c3577eSopenharmony_ci if (status == DistributedDB::DBStatus::NOT_FOUND) { 92253c3577eSopenharmony_ci ZLOGI("Get entries empty, prefix: %{public}s", Anonymous::Change(prefix).c_str()); 92353c3577eSopenharmony_ci return OBJECT_SUCCESS; 92453c3577eSopenharmony_ci } 92553c3577eSopenharmony_ci if (status != DistributedDB::DBStatus::OK) { 92653c3577eSopenharmony_ci ZLOGE("Get entries failed, prefix: %{public}s, status: %{public}d", Anonymous::Change(prefix).c_str(), status); 92753c3577eSopenharmony_ci return DB_ERROR; 92853c3577eSopenharmony_ci } 92953c3577eSopenharmony_ci std::vector<std::vector<uint8_t>> keys; 93053c3577eSopenharmony_ci std::for_each(entries.begin(), entries.end(), [&keys](const DistributedDB::Entry &entry) { 93153c3577eSopenharmony_ci keys.emplace_back(entry.key); 93253c3577eSopenharmony_ci }); 93353c3577eSopenharmony_ci if (keys.empty()) { 93453c3577eSopenharmony_ci return OBJECT_SUCCESS; 93553c3577eSopenharmony_ci } 93653c3577eSopenharmony_ci status = delegate_->DeleteBatch(keys); 93753c3577eSopenharmony_ci if (status != DistributedDB::DBStatus::OK) { 93853c3577eSopenharmony_ci ZLOGE("Delete entries failed, prefix: %{public}s, status: %{public}d", Anonymous::Change(prefix).c_str(), 93953c3577eSopenharmony_ci status); 94053c3577eSopenharmony_ci return DB_ERROR; 94153c3577eSopenharmony_ci } 94253c3577eSopenharmony_ci ZLOGI("Delete entries success, prefix: %{public}s, count: %{public}zu", Anonymous::Change(prefix).c_str(), 94353c3577eSopenharmony_ci keys.size()); 94453c3577eSopenharmony_ci return OBJECT_SUCCESS; 94553c3577eSopenharmony_ci} 94653c3577eSopenharmony_ci 94753c3577eSopenharmony_ciint32_t ObjectStoreManager::RetrieveFromStore(const std::string &appId, const std::string &sessionId, 94853c3577eSopenharmony_ci ObjectRecord &results) 94953c3577eSopenharmony_ci{ 95053c3577eSopenharmony_ci std::vector<DistributedDB::Entry> entries; 95153c3577eSopenharmony_ci std::string prefix = GetPrefixWithoutDeviceId(appId, sessionId); 95253c3577eSopenharmony_ci auto status = delegate_->GetEntries(std::vector<uint8_t>(prefix.begin(), prefix.end()), entries); 95353c3577eSopenharmony_ci if (status == DistributedDB::DBStatus::NOT_FOUND) { 95453c3577eSopenharmony_ci ZLOGI("Get entries empty, prefix: %{public}s, status: %{public}d", prefix.c_str(), status); 95553c3577eSopenharmony_ci return KEY_NOT_FOUND; 95653c3577eSopenharmony_ci } 95753c3577eSopenharmony_ci if (status != DistributedDB::DBStatus::OK) { 95853c3577eSopenharmony_ci ZLOGE("Get entries failed, prefix: %{public}s, status: %{public}d", prefix.c_str(), status); 95953c3577eSopenharmony_ci return DB_ERROR; 96053c3577eSopenharmony_ci } 96153c3577eSopenharmony_ci ZLOGI("Get entries success, prefix: %{public}s, count: %{public}zu", prefix.c_str(), entries.size()); 96253c3577eSopenharmony_ci for (const auto &entry : entries) { 96353c3577eSopenharmony_ci std::string key(entry.key.begin(), entry.key.end()); 96453c3577eSopenharmony_ci if (key.find(SAVE_INFO) != std::string::npos) { 96553c3577eSopenharmony_ci continue; 96653c3577eSopenharmony_ci } 96753c3577eSopenharmony_ci auto splitKeys = SplitEntryKey(key); 96853c3577eSopenharmony_ci if (!splitKeys.empty()) { 96953c3577eSopenharmony_ci results[splitKeys[PROPERTY_NAME_INDEX]] = entry.value; 97053c3577eSopenharmony_ci } 97153c3577eSopenharmony_ci } 97253c3577eSopenharmony_ci return OBJECT_SUCCESS; 97353c3577eSopenharmony_ci} 97453c3577eSopenharmony_ci 97553c3577eSopenharmony_ciObjectStoreManager::SaveInfo::SaveInfo(const std::string &bundleName, const std::string &sessionId, 97653c3577eSopenharmony_ci const std::string &sourceDeviceId, const std::string &targetDeviceId, const std::string ×tamp) 97753c3577eSopenharmony_ci : bundleName(bundleName), sessionId(sessionId), sourceDeviceId(sourceDeviceId), targetDeviceId(targetDeviceId), 97853c3577eSopenharmony_ci timestamp(timestamp) {} 97953c3577eSopenharmony_ci 98053c3577eSopenharmony_cibool ObjectStoreManager::SaveInfo::Marshal(json &node) const 98153c3577eSopenharmony_ci{ 98253c3577eSopenharmony_ci SetValue(node[GET_NAME(bundleName)], bundleName); 98353c3577eSopenharmony_ci SetValue(node[GET_NAME(sessionId)], sessionId); 98453c3577eSopenharmony_ci SetValue(node[GET_NAME(sourceDeviceId)], sourceDeviceId); 98553c3577eSopenharmony_ci SetValue(node[GET_NAME(targetDeviceId)], targetDeviceId); 98653c3577eSopenharmony_ci SetValue(node[GET_NAME(timestamp)], timestamp); 98753c3577eSopenharmony_ci return true; 98853c3577eSopenharmony_ci} 98953c3577eSopenharmony_ci 99053c3577eSopenharmony_cibool ObjectStoreManager::SaveInfo::Unmarshal(const json &node) 99153c3577eSopenharmony_ci{ 99253c3577eSopenharmony_ci GetValue(node, GET_NAME(bundleName), bundleName); 99353c3577eSopenharmony_ci GetValue(node, GET_NAME(sessionId), sessionId); 99453c3577eSopenharmony_ci GetValue(node, GET_NAME(sourceDeviceId), sourceDeviceId); 99553c3577eSopenharmony_ci GetValue(node, GET_NAME(targetDeviceId), targetDeviceId); 99653c3577eSopenharmony_ci GetValue(node, GET_NAME(timestamp), timestamp); 99753c3577eSopenharmony_ci return true; 99853c3577eSopenharmony_ci} 99953c3577eSopenharmony_ci 100053c3577eSopenharmony_cistd::string ObjectStoreManager::SaveInfo::ToPropertyPrefix() 100153c3577eSopenharmony_ci{ 100253c3577eSopenharmony_ci if (bundleName.empty() || sessionId.empty() || sourceDeviceId.empty() || targetDeviceId.empty() || 100353c3577eSopenharmony_ci timestamp.empty()) { 100453c3577eSopenharmony_ci return ""; 100553c3577eSopenharmony_ci } 100653c3577eSopenharmony_ci return bundleName + SEPERATOR + sessionId + SEPERATOR + sourceDeviceId + SEPERATOR + targetDeviceId + SEPERATOR + 100753c3577eSopenharmony_ci timestamp + SEPERATOR; 100853c3577eSopenharmony_ci} 100953c3577eSopenharmony_ci 101053c3577eSopenharmony_cistd::vector<std::string> ObjectStoreManager::SplitEntryKey(const std::string &key) 101153c3577eSopenharmony_ci{ 101253c3577eSopenharmony_ci std::smatch match; 101353c3577eSopenharmony_ci std::regex timeRegex(TIME_REGEX); 101453c3577eSopenharmony_ci if (!std::regex_search(key, match, timeRegex)) { 101553c3577eSopenharmony_ci ZLOGW("Format error, key.size = %{public}zu", key.size()); 101653c3577eSopenharmony_ci return {}; 101753c3577eSopenharmony_ci } 101853c3577eSopenharmony_ci auto timePos = match.position(); 101953c3577eSopenharmony_ci std::string fromTime = key.substr(timePos + 1); 102053c3577eSopenharmony_ci std::string beforeTime = key.substr(0, timePos); 102153c3577eSopenharmony_ci 102253c3577eSopenharmony_ci size_t targetDevicePos = beforeTime.find_last_of(SEPERATOR); 102353c3577eSopenharmony_ci if (targetDevicePos == std::string::npos) { 102453c3577eSopenharmony_ci ZLOGW("Format error, key.size = %{public}zu", key.size()); 102553c3577eSopenharmony_ci return {}; 102653c3577eSopenharmony_ci } 102753c3577eSopenharmony_ci std::string targetDevice = beforeTime.substr(targetDevicePos + 1); 102853c3577eSopenharmony_ci std::string beforeTargetDevice = beforeTime.substr(0, targetDevicePos); 102953c3577eSopenharmony_ci 103053c3577eSopenharmony_ci size_t sourceDeviceUdidPos = beforeTargetDevice.find_last_of(SEPERATOR); 103153c3577eSopenharmony_ci if (sourceDeviceUdidPos == std::string::npos) { 103253c3577eSopenharmony_ci ZLOGW("Format error, key.size = %{public}zu", key.size()); 103353c3577eSopenharmony_ci return {}; 103453c3577eSopenharmony_ci } 103553c3577eSopenharmony_ci std::string sourceDeviceUdid = beforeTargetDevice.substr(sourceDeviceUdidPos + 1); 103653c3577eSopenharmony_ci std::string beforeSourceDeviceUdid = beforeTargetDevice.substr(0, sourceDeviceUdidPos); 103753c3577eSopenharmony_ci 103853c3577eSopenharmony_ci size_t sessionIdPos = beforeSourceDeviceUdid.find_last_of(SEPERATOR); 103953c3577eSopenharmony_ci if (sessionIdPos == std::string::npos) { 104053c3577eSopenharmony_ci ZLOGW("Format error, key.size = %{public}zu", key.size()); 104153c3577eSopenharmony_ci return {}; 104253c3577eSopenharmony_ci } 104353c3577eSopenharmony_ci std::string sessionId = beforeSourceDeviceUdid.substr(sessionIdPos + 1); 104453c3577eSopenharmony_ci std::string bundleName = beforeSourceDeviceUdid.substr(0, sessionIdPos); 104553c3577eSopenharmony_ci 104653c3577eSopenharmony_ci size_t propertyNamePos = fromTime.find_first_of(SEPERATOR); 104753c3577eSopenharmony_ci if (propertyNamePos == std::string::npos) { 104853c3577eSopenharmony_ci ZLOGW("Format error, key.size = %{public}zu", key.size()); 104953c3577eSopenharmony_ci return {}; 105053c3577eSopenharmony_ci } 105153c3577eSopenharmony_ci std::string propertyName = fromTime.substr(propertyNamePos + 1); 105253c3577eSopenharmony_ci std::string time = fromTime.substr(0, propertyNamePos); 105353c3577eSopenharmony_ci 105453c3577eSopenharmony_ci return { bundleName, sessionId, sourceDeviceUdid, targetDevice, time, propertyName }; 105553c3577eSopenharmony_ci} 105653c3577eSopenharmony_ci 105753c3577eSopenharmony_cistd::string ObjectStoreManager::GetCurrentUser() 105853c3577eSopenharmony_ci{ 105953c3577eSopenharmony_ci std::vector<int> users; 106053c3577eSopenharmony_ci AccountDelegate::GetInstance()->QueryUsers(users); 106153c3577eSopenharmony_ci if (users.empty()) { 106253c3577eSopenharmony_ci return ""; 106353c3577eSopenharmony_ci } 106453c3577eSopenharmony_ci return std::to_string(users[0]); 106553c3577eSopenharmony_ci} 106653c3577eSopenharmony_ci 106753c3577eSopenharmony_civoid ObjectStoreManager::SaveUserToMeta() 106853c3577eSopenharmony_ci{ 106953c3577eSopenharmony_ci ZLOGD("start."); 107053c3577eSopenharmony_ci std::string userId = GetCurrentUser(); 107153c3577eSopenharmony_ci if (userId.empty()) { 107253c3577eSopenharmony_ci return; 107353c3577eSopenharmony_ci } 107453c3577eSopenharmony_ci std::string appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); 107553c3577eSopenharmony_ci StoreMetaData userMeta; 107653c3577eSopenharmony_ci userMeta.storeId = DistributedObject::ObjectCommon::OBJECTSTORE_DB_STOREID; 107753c3577eSopenharmony_ci userMeta.user = userId; 107853c3577eSopenharmony_ci userMeta.storeType = ObjectDistributedType::OBJECT_SINGLE_VERSION; 107953c3577eSopenharmony_ci std::string userMetaKey = GetMetaUserIdKey(userId, appId); 108053c3577eSopenharmony_ci auto saved = DistributedData::MetaDataManager::GetInstance().SaveMeta(userMetaKey, userMeta, true); 108153c3577eSopenharmony_ci if (!saved) { 108253c3577eSopenharmony_ci ZLOGE("userMeta save failed"); 108353c3577eSopenharmony_ci } 108453c3577eSopenharmony_ci} 108553c3577eSopenharmony_ci 108653c3577eSopenharmony_civoid ObjectStoreManager::CloseAfterMinute() 108753c3577eSopenharmony_ci{ 108853c3577eSopenharmony_ci executors_->Schedule(std::chrono::minutes(INTERVAL), std::bind(&ObjectStoreManager::Close, this)); 108953c3577eSopenharmony_ci} 109053c3577eSopenharmony_ci 109153c3577eSopenharmony_civoid ObjectStoreManager::SetThreadPool(std::shared_ptr<ExecutorPool> executors) 109253c3577eSopenharmony_ci{ 109353c3577eSopenharmony_ci executors_ = executors; 109453c3577eSopenharmony_ci} 109553c3577eSopenharmony_ci 109653c3577eSopenharmony_ciuint64_t SequenceSyncManager::AddNotifier(const std::string &userId, SyncCallBack &callback) 109753c3577eSopenharmony_ci{ 109853c3577eSopenharmony_ci std::lock_guard<std::mutex> lock(notifierLock_); 109953c3577eSopenharmony_ci uint64_t sequenceId = KvStoreUtils::GenerateSequenceId(); 110053c3577eSopenharmony_ci userIdSeqIdRelations_[userId].emplace_back(sequenceId); 110153c3577eSopenharmony_ci seqIdCallbackRelations_[sequenceId] = callback; 110253c3577eSopenharmony_ci return sequenceId; 110353c3577eSopenharmony_ci} 110453c3577eSopenharmony_ci 110553c3577eSopenharmony_ciSequenceSyncManager::Result SequenceSyncManager::Process( 110653c3577eSopenharmony_ci uint64_t sequenceId, const std::map<std::string, DistributedDB::DBStatus> &results, std::string &userId) 110753c3577eSopenharmony_ci{ 110853c3577eSopenharmony_ci std::lock_guard<std::mutex> lock(notifierLock_); 110953c3577eSopenharmony_ci if (seqIdCallbackRelations_.count(sequenceId) == 0) { 111053c3577eSopenharmony_ci ZLOGE("not exist"); 111153c3577eSopenharmony_ci return ERR_SID_NOT_EXIST; 111253c3577eSopenharmony_ci } 111353c3577eSopenharmony_ci std::map<std::string, int32_t> syncResults; 111453c3577eSopenharmony_ci for (auto &item : results) { 111553c3577eSopenharmony_ci syncResults[item.first] = item.second == DistributedDB::DBStatus::OK ? 0 : -1; 111653c3577eSopenharmony_ci } 111753c3577eSopenharmony_ci seqIdCallbackRelations_[sequenceId](syncResults); 111853c3577eSopenharmony_ci ZLOGD("end complete"); 111953c3577eSopenharmony_ci return DeleteNotifierNoLock(sequenceId, userId); 112053c3577eSopenharmony_ci} 112153c3577eSopenharmony_ci 112253c3577eSopenharmony_ciSequenceSyncManager::Result SequenceSyncManager::DeleteNotifier(uint64_t sequenceId, std::string &userId) 112353c3577eSopenharmony_ci{ 112453c3577eSopenharmony_ci std::lock_guard<std::mutex> lock(notifierLock_); 112553c3577eSopenharmony_ci if (seqIdCallbackRelations_.count(sequenceId) == 0) { 112653c3577eSopenharmony_ci ZLOGE("not exist"); 112753c3577eSopenharmony_ci return ERR_SID_NOT_EXIST; 112853c3577eSopenharmony_ci } 112953c3577eSopenharmony_ci return DeleteNotifierNoLock(sequenceId, userId); 113053c3577eSopenharmony_ci} 113153c3577eSopenharmony_ci 113253c3577eSopenharmony_ciSequenceSyncManager::Result SequenceSyncManager::DeleteNotifierNoLock(uint64_t sequenceId, std::string &userId) 113353c3577eSopenharmony_ci{ 113453c3577eSopenharmony_ci seqIdCallbackRelations_.erase(sequenceId); 113553c3577eSopenharmony_ci auto userIdIter = userIdSeqIdRelations_.begin(); 113653c3577eSopenharmony_ci while (userIdIter != userIdSeqIdRelations_.end()) { 113753c3577eSopenharmony_ci auto sIdIter = std::find(userIdIter->second.begin(), userIdIter->second.end(), sequenceId); 113853c3577eSopenharmony_ci if (sIdIter != userIdIter->second.end()) { 113953c3577eSopenharmony_ci userIdIter->second.erase(sIdIter); 114053c3577eSopenharmony_ci if (userIdIter->second.empty()) { 114153c3577eSopenharmony_ci ZLOGD("finished user callback, userId = %{public}s", userIdIter->first.c_str()); 114253c3577eSopenharmony_ci userId = userIdIter->first; 114353c3577eSopenharmony_ci userIdSeqIdRelations_.erase(userIdIter); 114453c3577eSopenharmony_ci return SUCCESS_USER_HAS_FINISHED; 114553c3577eSopenharmony_ci } else { 114653c3577eSopenharmony_ci ZLOGD(" finished a callback but user not finished, userId = %{public}s", userIdIter->first.c_str()); 114753c3577eSopenharmony_ci return SUCCESS_USER_IN_USE; 114853c3577eSopenharmony_ci } 114953c3577eSopenharmony_ci } 115053c3577eSopenharmony_ci userIdIter++; 115153c3577eSopenharmony_ci } 115253c3577eSopenharmony_ci return SUCCESS_USER_HAS_FINISHED; 115353c3577eSopenharmony_ci} 115453c3577eSopenharmony_ci 115553c3577eSopenharmony_ciint32_t ObjectStoreManager::BindAsset(const uint32_t tokenId, const std::string& appId, const std::string& sessionId, 115653c3577eSopenharmony_ci ObjectStore::Asset& asset, ObjectStore::AssetBindInfo& bindInfo) 115753c3577eSopenharmony_ci{ 115853c3577eSopenharmony_ci auto snapshotKey = appId + SEPERATOR + sessionId; 115953c3577eSopenharmony_ci snapshots_.ComputeIfAbsent( 116053c3577eSopenharmony_ci snapshotKey, [](const std::string& key) -> auto { 116153c3577eSopenharmony_ci return std::make_shared<ObjectSnapshot>(); 116253c3577eSopenharmony_ci }); 116353c3577eSopenharmony_ci auto storeKey = appId + SEPERATOR + bindInfo.storeName; 116453c3577eSopenharmony_ci bindSnapshots_.ComputeIfAbsent( 116553c3577eSopenharmony_ci storeKey, [](const std::string& key) -> auto { 116653c3577eSopenharmony_ci return std::make_shared<std::map<std::string, std::shared_ptr<Snapshot>>>(); 116753c3577eSopenharmony_ci }); 116853c3577eSopenharmony_ci auto snapshots = snapshots_.Find(snapshotKey).second; 116953c3577eSopenharmony_ci bindSnapshots_.Compute(storeKey, [this, &asset, snapshots] (const auto &key, auto &value) { 117053c3577eSopenharmony_ci value->emplace(std::pair{asset.uri, snapshots}); 117153c3577eSopenharmony_ci return true; 117253c3577eSopenharmony_ci }); 117353c3577eSopenharmony_ci 117453c3577eSopenharmony_ci HapTokenInfo tokenInfo; 117553c3577eSopenharmony_ci auto status = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); 117653c3577eSopenharmony_ci if (status != RET_SUCCESS) { 117753c3577eSopenharmony_ci ZLOGE("token:0x%{public}x, result:%{public}d, bundleName:%{public}s", tokenId, status, appId.c_str()); 117853c3577eSopenharmony_ci return GeneralError::E_ERROR; 117953c3577eSopenharmony_ci } 118053c3577eSopenharmony_ci StoreInfo storeInfo; 118153c3577eSopenharmony_ci storeInfo.bundleName = appId; 118253c3577eSopenharmony_ci storeInfo.tokenId = tokenId; 118353c3577eSopenharmony_ci storeInfo.instanceId = tokenInfo.instIndex; 118453c3577eSopenharmony_ci storeInfo.user = tokenInfo.userID; 118553c3577eSopenharmony_ci storeInfo.storeName = bindInfo.storeName; 118653c3577eSopenharmony_ci 118753c3577eSopenharmony_ci snapshots_.Compute(snapshotKey, [this, &asset, &bindInfo, &storeInfo] (const auto &key, auto &value) { 118853c3577eSopenharmony_ci value->BindAsset(ValueProxy::Convert(std::move(asset)), ConvertBindInfo(bindInfo), storeInfo); 118953c3577eSopenharmony_ci return true; 119053c3577eSopenharmony_ci }); 119153c3577eSopenharmony_ci return OBJECT_SUCCESS; 119253c3577eSopenharmony_ci} 119353c3577eSopenharmony_ci 119453c3577eSopenharmony_ciDistributedData::AssetBindInfo ObjectStoreManager::ConvertBindInfo(ObjectStore::AssetBindInfo& bindInfo) 119553c3577eSopenharmony_ci{ 119653c3577eSopenharmony_ci return DistributedData::AssetBindInfo{ 119753c3577eSopenharmony_ci .storeName = std::move(bindInfo.storeName), 119853c3577eSopenharmony_ci .tableName = std::move(bindInfo.tableName), 119953c3577eSopenharmony_ci .primaryKey = ValueProxy::Convert(std::move(bindInfo.primaryKey)), 120053c3577eSopenharmony_ci .field = std::move(bindInfo.field), 120153c3577eSopenharmony_ci .assetName = std::move(bindInfo.assetName), 120253c3577eSopenharmony_ci }; 120353c3577eSopenharmony_ci} 120453c3577eSopenharmony_ci 120553c3577eSopenharmony_ciint32_t ObjectStoreManager::OnAssetChanged(const uint32_t tokenId, const std::string& appId, 120653c3577eSopenharmony_ci const std::string& sessionId, const std::string& deviceId, const ObjectStore::Asset& asset) 120753c3577eSopenharmony_ci{ 120853c3577eSopenharmony_ci const int32_t userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); 120953c3577eSopenharmony_ci auto objectAsset = asset; 121053c3577eSopenharmony_ci Asset dataAsset = ValueProxy::Convert(std::move(objectAsset)); 121153c3577eSopenharmony_ci auto snapshotKey = appId + SEPERATOR + sessionId; 121253c3577eSopenharmony_ci int32_t res = OBJECT_SUCCESS; 121353c3577eSopenharmony_ci bool exist = snapshots_.ComputeIfPresent(snapshotKey, 121453c3577eSopenharmony_ci [&res, &dataAsset, &deviceId](const std::string &key, std::shared_ptr<Snapshot> snapshot) { 121553c3577eSopenharmony_ci if (snapshot != nullptr) { 121653c3577eSopenharmony_ci res = snapshot->OnDataChanged(dataAsset, deviceId); // needChange 121753c3577eSopenharmony_ci } 121853c3577eSopenharmony_ci return snapshot != nullptr; 121953c3577eSopenharmony_ci }); 122053c3577eSopenharmony_ci if (exist) { 122153c3577eSopenharmony_ci return res; 122253c3577eSopenharmony_ci } 122353c3577eSopenharmony_ci 122453c3577eSopenharmony_ci auto block = std::make_shared<BlockData<std::tuple<bool, bool>>>(WAIT_TIME, std::tuple{ true, true }); 122553c3577eSopenharmony_ci ObjectAssetLoader::GetInstance()->TransferAssetsAsync(userId, appId, deviceId, { dataAsset }, [block](bool ret) { 122653c3577eSopenharmony_ci block->SetValue({ false, ret }); 122753c3577eSopenharmony_ci }); 122853c3577eSopenharmony_ci auto [timeout, success] = block->GetValue(); 122953c3577eSopenharmony_ci if (timeout || !success) { 123053c3577eSopenharmony_ci ZLOGE("transfer failed, timeout: %{public}d, success: %{public}d, name: %{public}s, deviceId: %{public}s ", 123153c3577eSopenharmony_ci timeout, success, asset.name.c_str(), DistributedData::Anonymous::Change(deviceId).c_str()); 123253c3577eSopenharmony_ci return OBJECT_INNER_ERROR; 123353c3577eSopenharmony_ci } 123453c3577eSopenharmony_ci return OBJECT_SUCCESS; 123553c3577eSopenharmony_ci} 123653c3577eSopenharmony_ci 123753c3577eSopenharmony_ciObjectStoreManager::UriToSnapshot ObjectStoreManager::GetSnapShots(const std::string& bundleName, 123853c3577eSopenharmony_ci const std::string& storeName) 123953c3577eSopenharmony_ci{ 124053c3577eSopenharmony_ci auto storeKey = bundleName + SEPERATOR + storeName; 124153c3577eSopenharmony_ci bindSnapshots_.ComputeIfAbsent( 124253c3577eSopenharmony_ci storeKey, [](const std::string& key) -> auto { 124353c3577eSopenharmony_ci return std::make_shared<std::map<std::string, std::shared_ptr<Snapshot>>>(); 124453c3577eSopenharmony_ci }); 124553c3577eSopenharmony_ci return bindSnapshots_.Find(storeKey).second; 124653c3577eSopenharmony_ci} 124753c3577eSopenharmony_ci 124853c3577eSopenharmony_civoid ObjectStoreManager::DeleteSnapshot(const std::string& bundleName, const std::string& sessionId) 124953c3577eSopenharmony_ci{ 125053c3577eSopenharmony_ci auto snapshotKey = bundleName + SEPERATOR + sessionId; 125153c3577eSopenharmony_ci auto snapshot = snapshots_.Find(snapshotKey).second; 125253c3577eSopenharmony_ci if (snapshot == nullptr) { 125353c3577eSopenharmony_ci ZLOGD("Not find snapshot, don't need delete"); 125453c3577eSopenharmony_ci return; 125553c3577eSopenharmony_ci } 125653c3577eSopenharmony_ci bindSnapshots_.ForEach([snapshot](auto& key, auto& value) { 125753c3577eSopenharmony_ci for (auto pos = value->begin(); pos != value->end();) { 125853c3577eSopenharmony_ci if (pos->second == snapshot) { 125953c3577eSopenharmony_ci pos = value->erase(pos); 126053c3577eSopenharmony_ci } else { 126153c3577eSopenharmony_ci ++pos; 126253c3577eSopenharmony_ci } 126353c3577eSopenharmony_ci } 126453c3577eSopenharmony_ci return true; 126553c3577eSopenharmony_ci }); 126653c3577eSopenharmony_ci snapshots_.Erase(snapshotKey); 126753c3577eSopenharmony_ci} 126853c3577eSopenharmony_ci} // namespace DistributedObject 126953c3577eSopenharmony_ci} // namespace OHOS 1270