diff --git a/services/distributeddataservice/service/udmf/store/runtime_store.cpp b/services/distributeddataservice/service/udmf/store/runtime_store.cpp index cd57d8801d9a695fc876059c3e9430c89458f1d4..8badccd4a2b591925b4c7045d6a1e1eb183aa189 100644 --- a/services/distributeddataservice/service/udmf/store/runtime_store.cpp +++ b/services/distributeddataservice/service/udmf/store/runtime_store.cpp @@ -435,24 +435,17 @@ bool RuntimeStore::Init() }); if (status != DBStatus::OK) { ZLOGE("GetKvStore fail, status: %{public}d.", static_cast(status)); - if (status == INVALID_PASSWD_OR_CORRUPTED_DB && - (status = delegateManager_->DeleteKvStore(storeId_)) != DBStatus::OK) { - ZLOGE("DeleteKvStore fail, status: %{public}d.", static_cast(status)); + if (status == INVALID_PASSWD_OR_CORRUPTED_DB) { + status = delegateManager_->DeleteKvStore(storeId_); + if (status != DBStatus::OK) { + ZLOGE("DeleteKvStore fail, status: %{public}d.", static_cast(status)); + } } return false; } auto release = [this](KvStoreNbDelegate *delegate) { - ZLOGI("Release runtime kvStore."); - if (delegate == nullptr) { - return; - } - auto retStatus = delegateManager_->CloseKvStore(delegate); - if (retStatus != DBStatus::OK) { - ZLOGE("CloseKvStore fail, status: %{public}d.", static_cast(retStatus)); - } - if (isCorrupted_ && (retStatus = delegateManager_->DeleteKvStore(storeId_)) != DBStatus::OK) { - ZLOGE("DeleteKvStore fail, status: %{public}d.", static_cast(retStatus)); - } + ZLOGI("Release runtime kvStore, db is corrupted: %{public}d.", isCorrupted_); + ReleaseStore(delegate); }; kvStore_ = std::shared_ptr(delegate, release); uint32_t pragmData = 16 * 1024 * 1024; @@ -541,6 +534,24 @@ bool RuntimeStore::SaveMetaData() return true; } +void RuntimeStore::ReleaseStore(DistributedDB::KvStoreNbDelegate *delegate) +{ + if (delegate == nullptr) { + return; + } + auto retStatus = delegateManager_->CloseKvStore(delegate); + if (retStatus != DBStatus::OK) { + ZLOGE("CloseKvStore fail, status: %{public}d.", static_cast(retStatus)); + return; + } + if (isCorrupted_) { + retStatus = delegateManager_->DeleteKvStore(storeId_); + if (retStatus != DBStatus::OK) { + ZLOGE("DeleteKvStore fail, status: %{public}d.", static_cast(retStatus)); + } + } +} + void RuntimeStore::SetDelegateManager(const std::string &dataDir, const std::string &appId, const std::string &userId) { delegateManager_ = std::make_shared(appId, userId); diff --git a/services/distributeddataservice/service/udmf/store/runtime_store.h b/services/distributeddataservice/service/udmf/store/runtime_store.h index 214c6f15c5f522958c98ccc7417eeb7f7a9a7817..cfa06e62dc12bebee9b13fcad7c9a6c4932f3082 100644 --- a/services/distributeddataservice/service/udmf/store/runtime_store.h +++ b/services/distributeddataservice/service/udmf/store/runtime_store.h @@ -65,6 +65,8 @@ private: const DevNameMap &deviceNameMap); Status PutSummary(const UnifiedData &data, std::vector &entries); Status MarkWhenCorrupted(DistributedDB::DBStatus status); + void ReleaseStore(DistributedDB::KvStoreNbDelegate *delegate); + bool isCorrupted_ = false; }; } // namespace UDMF