diff --git a/services/distributeddataservice/framework/include/cloud/schema_meta.h b/services/distributeddataservice/framework/include/cloud/schema_meta.h index bc74d210b11676ad6a4835461066605d599ca8d8..33dc0cb41946842ab8e357ee76d2baa23fde2325 100644 --- a/services/distributeddataservice/framework/include/cloud/schema_meta.h +++ b/services/distributeddataservice/framework/include/cloud/schema_meta.h @@ -73,7 +73,8 @@ public: static constexpr const char *SHARING_RESOURCE = "#_sharing_resource"; static constexpr const char *HASH_KEY = "#_hash_key"; - static constexpr uint32_t CURRENT_VERSION = 0x10000; + static constexpr uint32_t CURRENT_VERSION = 0x10001; + static constexpr uint32_t CLEAN_WATER_VERSION = 0x10001; static inline uint32_t GetLowVersion(uint32_t metaVersion = CURRENT_VERSION) { return metaVersion & 0xFFFF; @@ -104,4 +105,4 @@ enum AutoSyncType { }; } // namespace OHOS::DistributedData -#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SCHEMA_META_H +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SCHEMA_META_H \ No newline at end of file diff --git a/services/distributeddataservice/framework/include/store/general_store.h b/services/distributeddataservice/framework/include/store/general_store.h index dd4e27e7d1b9c710a12731279d81add230b00909..dc109463a23646ff36efa977381f77d7c0694f63 100644 --- a/services/distributeddataservice/framework/include/store/general_store.h +++ b/services/distributeddataservice/framework/include/store/general_store.h @@ -61,6 +61,7 @@ public: CLOUD_DATA, CLOUD_INFO, LOCAL_DATA, + CLEAN_WATER, CLEAN_MODE_BUTT }; diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index cf3f5d1d7865206227d2337874e910ac7f425c2d..fe9aa097bb835088cb0917cf0a5549a2e33e97b9 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -275,7 +275,9 @@ void CloudServiceImpl::DoClean(int32_t user, const SchemaMeta &schemaMeta, int32 storeInfo.bundleName = meta.bundleName; storeInfo.user = atoi(meta.user.c_str()); storeInfo.storeName = meta.storeId; - EventCenter::GetInstance().PostEvent(std::make_unique(CloudEvent::CLEAN_DATA, storeInfo)); + if (action != GeneralStore::CLEAN_WATER) { + EventCenter::GetInstance().PostEvent(std::make_unique(CloudEvent::CLEAN_DATA, storeInfo)); + } auto status = store->Clean({}, action, ""); if (status != E_OK) { ZLOGW("remove device data status:%{public}d, user:%{public}d, bundleName:%{public}s, " @@ -669,10 +671,16 @@ int32_t CloudServiceImpl::OnInitialize() { XCollie xcollie(__FUNCTION__, XCollie::XCOLLIE_LOG | XCollie::XCOLLIE_RECOVERY); DistributedDB::RuntimeConfig::SetCloudTranslate(std::make_shared()); - Execute(GenTask(0, 0, CloudSyncScene::SERVICE_INIT, - { WORK_CLOUD_INFO_UPDATE, WORK_SCHEMA_UPDATE, WORK_DO_CLOUD_SYNC, WORK_SUB })); std::vector users; Account::GetInstance()->QueryUsers(users); + for (auto user : users) { + if (user == DEFAULT_USER) { + continue; + } + CleanWaterVersion(user); + } + Execute(GenTask(0, 0, CloudSyncScene::SERVICE_INIT, + { WORK_CLOUD_INFO_UPDATE, WORK_SCHEMA_UPDATE, WORK_DO_CLOUD_SYNC, WORK_SUB })); for (auto user : users) { if (user == DEFAULT_USER) { continue; @@ -687,6 +695,35 @@ int32_t CloudServiceImpl::OnInitialize() return E_OK; } +bool CloudServiceImpl::CleanWaterVersion(int32_t user) +{ + auto [status, cloudInfo] = GetCloudInfoFromMeta(user); + if (status != SUCCESS) { + return false; + } + auto stores = CheckerManager::GetInstance().GetDynamicStores(); + auto staticStores = CheckerManager::GetInstance().GetStaticStores(); + stores.insert(stores.end(), staticStores.begin(), staticStores.end()); + auto keys = cloudInfo.GetSchemaKey(); + for (const auto &[bundle, key] : keys) { + SchemaMeta schemaMeta; + if (MetaDataManager::GetInstance().LoadMeta(key, schemaMeta, true) && + schemaMeta.metaVersion < SchemaMeta::CLEAN_WATER_VERSION) { + bool found = std::any_of(stores.begin(), stores.end(), + [&schemaMeta](const CheckerManager::StoreInfo &storeInfo) { + return storeInfo.bundleName == schemaMeta.bundleName; + }); + if (!found) { + continue; + } + DoClean(user, schemaMeta, GeneralStore::CleanMode::CLEAN_WATER); + schemaMeta.metaVersion = SchemaMeta::CLEAN_WATER_VERSION; + } + MetaDataManager::GetInstance().SaveMeta(key, schemaMeta, true); + } + return true; +} + int32_t CloudServiceImpl::OnBind(const BindInfo &info) { if (executor_ != nullptr || info.executors == nullptr) { diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index c1ec1dd099cb44a31eabce1bd26ef50a685672ad..21283817f210f1f06cb0611f7a7fc4bff8aec9b3 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -145,6 +145,7 @@ private: bool ReleaseUserInfo(int32_t user, CloudSyncScene scene); bool DoCloudSync(int32_t user, CloudSyncScene scene); bool StopCloudSync(int32_t user, CloudSyncScene scene); + bool CleanWaterVersion(int32_t user); static std::pair GetCloudInfo(int32_t userId); static std::pair GetCloudInfoFromMeta(int32_t userId); diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index 358468d153fcdf1ddd851e7193ed210b9146f578..05a49cda23ebb8c09c8e1007fbf5d82c78d3e795 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -521,6 +521,11 @@ int32_t KVDBGeneralStore::Clean(const std::vector &devices, int32_t case CLOUD_DATA: status = delegate_->RemoveDeviceData("", static_cast(CLOUD_DATA)); break; + case CLEAN_WATER: + ClearKvMetaDataOption option; + option.type = ClearKvMetaOpType::CLEAN_CLOUD_WATERMARK; + status = delegate_->ClearMetaData(option); + break; case NEARBY_DATA: if (devices.empty()) { status = delegate_->RemoveDeviceData(); diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index 6da7e1d416e751d92b0ce2ee2ed87f452df334bc..57ca2a4151c1c89d58b79ea4fa83b305beac9e36 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -567,7 +567,7 @@ HWTEST_F(KVDBGeneralStoreTest, Clean, TestSize.Level0) std::string tableName = "tableName"; auto ret = store->Clean(devices, -1, tableName); EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - ret = store->Clean(devices, 5, tableName); + ret = store->Clean(devices, 6, tableName); EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); ret = store->Clean(devices, GeneralStore::CleanMode::NEARBY_DATA, tableName); EXPECT_EQ(ret, GeneralError::E_ALREADY_CLOSED); @@ -587,6 +587,9 @@ HWTEST_F(KVDBGeneralStoreTest, Clean, TestSize.Level0) ret = store->Clean(devices, GeneralStore::CleanMode::LOCAL_DATA, tableName); EXPECT_EQ(ret, GeneralError::E_ERROR); + + ret = store->Clean(devices, GeneralStore::CleanMode::CLEAN_WATER, tableName); + EXPECT_EQ(ret, GeneralError::E_OK); } /** diff --git a/services/distributeddataservice/service/test/mock/kv_store_nb_delegate_mock.cpp b/services/distributeddataservice/service/test/mock/kv_store_nb_delegate_mock.cpp index 09b0962e854924d719eeb07d55c0fc37c04b3091..fd5dab19db96d5b04be1fa3509c60f512bb20475 100644 --- a/services/distributeddataservice/service/test/mock/kv_store_nb_delegate_mock.cpp +++ b/services/distributeddataservice/service/test/mock/kv_store_nb_delegate_mock.cpp @@ -337,4 +337,9 @@ KvStoreNbDelegate::DatabaseStatus KvStoreNbDelegateMock::GetDatabaseStatus() con { return {}; } + +DBStatus KvStoreNbDelegateMock::ClearMetaData(ClearKvMetaDataOption option) +{ + return DBStatus::OK; +} } // namespace DistributedDB \ No newline at end of file diff --git a/services/distributeddataservice/service/test/mock/kv_store_nb_delegate_mock.h b/services/distributeddataservice/service/test/mock/kv_store_nb_delegate_mock.h index 7f3d77c8d184b2d02877307072caf3024eff713e..8a97092b82cb5b7fc6f57fea11f33aec4cbf0c53 100644 --- a/services/distributeddataservice/service/test/mock/kv_store_nb_delegate_mock.h +++ b/services/distributeddataservice/service/test/mock/kv_store_nb_delegate_mock.h @@ -107,6 +107,7 @@ public: DBStatus Sync(const DeviceSyncOption &option, const DeviceSyncProcessCallback &onProcess); DBStatus CancelSync(uint32_t syncId); DatabaseStatus GetDatabaseStatus() const; + DBStatus ClearMetaData(ClearKvMetaDataOption option); }; } // namespace DistributedDB #endif // KV_STORE_NB_DELEGATE_H_MOCK \ No newline at end of file