diff --git a/services/distributeddataservice/framework/include/cloud/cloud_event.h b/services/distributeddataservice/framework/include/cloud/cloud_event.h index 7c347470981f5e540afddcab8f10363df5420e20..089d97b9711c57702f1e7d045a6f49a3fa6c2b01 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -40,6 +40,7 @@ public: UNLOCK_CLOUD_CONTAINER, SET_SEARCH_TRIGGER, UPGRADE_SCHEMA, + CLOUD_SYNC_BEFORE, CLOUD_BUTT }; diff --git a/services/distributeddataservice/framework/include/store/store_event.h b/services/distributeddataservice/framework/include/store/store_event.h new file mode 100644 index 0000000000000000000000000000000000000000..6462d6d7adcf4d861b5a3fa2553deefd6735a5ef --- /dev/null +++ b/services/distributeddataservice/framework/include/store/store_event.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_EVENT_H + +#include + +#include "eventcenter/event.h" +#include "store/store_info.h" + +namespace OHOS::DistributedData { +class API_EXPORT StoreEvent : public Event { +public: + enum : int32_t { FEATURE_INIT = EVT_CLOUD, OPEN_STORE, SET_DISTRIBUTED_TABLE, STORE_BUTT }; + + StoreEvent(int32_t evtId, StoreInfo storeInfo); + ~StoreEvent() = default; + const StoreInfo &GetStoreInfo() const; + int32_t GetEventId() const; + +private: + int32_t eventId_; + StoreInfo storeInfo_; +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_EVENT_H \ No newline at end of file diff --git a/services/distributeddataservice/framework/store/store_event.cpp b/services/distributeddataservice/framework/store/store_event.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a15eecb59a23fdc1ad98a3191d036cbf57ef1924 --- /dev/null +++ b/services/distributeddataservice/framework/store/store_event.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "store/store_event.h" + +namespace OHOS::DistributedData { +StoreEvent::StoreEvent(int32_t evtId, StoreInfo storeInfo) + : Event(evtId), eventId_(evtId), storeInfo_(std::move(storeInfo)) +{ +} + +const StoreEvent &StoreEvent::GetStoreInfo() const +{ + return storeInfo_; +} + +int32_t StoreEvent::GetEventId() const +{ + return eventId_; +} +} // namespace OHOS::DistributedData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index a286b810c841e70a093285af889b592a55e73452..6de110b449c7143e6576d08a61de24b20415fe44 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -97,8 +97,13 @@ CloudServiceImpl::Factory::~Factory() {} CloudServiceImpl::CloudServiceImpl() { - EventCenter::GetInstance().Subscribe(CloudEvent::GET_SCHEMA, [this](const Event &event) { - GetSchema(event); + EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_SYNC_BEFORE, [this](const Event &event) { + auto &cloudEvent = static_cast(event); + auto &storeInfo = cloudEvent.GetStoreInfo(); + auto status = GetCloudInfo(storeInfo.user); + if (status == SUCCESS) { + GetSchema(event); + } }); EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_SHARE, [this](const Event &event) { CloudShare(event); @@ -117,6 +122,9 @@ CloudServiceImpl::CloudServiceImpl() InitSubTask(sub, SUBSCRIPTION_INTERVAL); return true; }, true); + EventCenter::GetInstance().Subscribe(StoreEvent::SET_DISTRIBUTED_TABLE, [this](const Event &event) { + GetSchema(event); + }); } int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &switches) diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 1391796224bf894a550a943e687400803348fb65..5a8372d40befb5233044b44b9a22ac336d95a13e 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -556,7 +556,7 @@ void SyncManager::UpdateSchema(const SyncManager::SyncInfo &syncInfo) StoreInfo storeInfo; storeInfo.user = syncInfo.user_; storeInfo.bundleName = syncInfo.bundleName_; - EventCenter::GetInstance().PostEvent(std::make_unique(CloudEvent::GET_SCHEMA, storeInfo)); + EventCenter::GetInstance().PostEvent(std::make_unique(CloudEvent::CLOUD_SYNC_BEFORE, storeInfo)); } std::map SyncManager::GetBindInfos(const StoreMetaData &meta, @@ -691,19 +691,10 @@ std::vector> SyncManager::GetCloudSyncInfo(const std::vector> cloudSyncInfos; if (NeedGetCloudInfo(cloud)) { ZLOGI("get cloud info from server, user: %{public}d.", cloud.user); - auto instance = CloudServer::GetInstance(); - if (instance == nullptr) { - return cloudSyncInfos; - } - int32_t errCode = SUCCESS; - std::tie(errCode, cloud) = instance->GetServerInfo(cloud.user, false); - if (!cloud.IsValid()) { - ZLOGE("cloud is empty, user: %{public}d", cloud.user); - return cloudSyncInfos; - } - if (!MetaDataManager::GetInstance().SaveMeta(cloud.GetKey(), cloud, true)) { - ZLOGW("save cloud info fail, user: %{public}d", cloud.user); - } + StoreInfo storeInfo; + storeInfo.user = cloud.user; + storeInfo.bundleName = info.bundleName_; + EventCenter::GetInstance().PostEvent(std::make_unique(CloudEvent::CLOUD_SYNC_BEFORE, storeInfo)); } auto schemaKey = CloudInfo::GetSchemaKey(cloud.user, info.bundleName_); SchemaMeta schemaMeta; diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index f04980ccc167301f48916a6a907e72f745330e5e..f8dccdc5bdefa6216c7482c52ffb764629ef9035 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -705,7 +705,7 @@ Status KVDBServiceImpl::BeforeCreate(const AppId &appId, const StoreId &storeId, storeInfo.instanceId = GetInstIndex(storeInfo.tokenId, appId); storeInfo.user = std::atoi(meta.user.c_str()); executors_->Execute([storeInfo]() { - auto event = std::make_unique(CloudEvent::GET_SCHEMA, storeInfo); + auto event = std::make_unique(StoreEvent::OPEN_STORE, storeInfo); EventCenter::GetInstance().PostEvent(move(event)); }); } diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 61bff0d7681858adc3ccef8ecbb863a16e5348ee..2a6124bce70cf8750c03026020bf1e622d32042b 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -339,6 +339,7 @@ int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const localMeta.enableCloud = param.enableCloud_; MetaDataManager::GetInstance().SaveMeta(localMeta.GetKey(), localMeta, true); } + PostEvent(param, StoreEvent::SET_DISTRIBTED_TABLE); } std::vector relationships; for (const auto &reference : references) { @@ -862,7 +863,7 @@ int32_t RdbServiceImpl::AfterOpen(const RdbSyncerParam ¶m) } UpgradeCloneSecretKey(meta); } - GetSchema(param); + PostEvent(param, StoreEvent::OPEN_STORE); return RDB_OK; } @@ -877,7 +878,7 @@ int32_t RdbServiceImpl::ReportStatistic(const RdbSyncerParam& param, const RdbSt return RDB_OK; } -void RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) +void RdbServiceImpl::PostEvent(const RdbSyncerParam ¶m, int32_t evtId) { if (executors_ != nullptr) { StoreInfo storeInfo; @@ -888,8 +889,8 @@ void RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) storeInfo.instanceId = instanceId; storeInfo.user = user; storeInfo.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; - executors_->Execute([storeInfo]() { - auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo)); + executors_->Execute([storeInfo, evtId]() { + auto event = std::make_unique(evtId, std::move(storeInfo)); EventCenter::GetInstance().PostEvent(move(event)); return; });