From 72e608119fd2bd035db1e31edf27a5982adfdeb2 Mon Sep 17 00:00:00 2001 From: wangtong Date: Tue, 25 Mar 2025 11:18:30 +0800 Subject: [PATCH] fix Signed-off-by: wangtong --- .../framework/include/cloud/cloud_event.h | 1 + .../framework/include/store/store_event.h | 39 +++++++++++++++++++ .../framework/store/store_event.cpp | 33 ++++++++++++++++ .../service/cloud/cloud_service_impl.cpp | 12 +++++- .../service/cloud/sync_manager.cpp | 19 +++------ .../service/kvdb/kvdb_service_impl.cpp | 2 +- .../service/rdb/rdb_service_impl.cpp | 9 +++-- 7 files changed, 94 insertions(+), 21 deletions(-) create mode 100644 services/distributeddataservice/framework/include/store/store_event.h create mode 100644 services/distributeddataservice/framework/store/store_event.cpp diff --git a/services/distributeddataservice/framework/include/cloud/cloud_event.h b/services/distributeddataservice/framework/include/cloud/cloud_event.h index 7c3474709..089d97b97 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 000000000..6462d6d7a --- /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 000000000..a15eecb59 --- /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 a286b810c..6de110b44 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 139179622..5a8372d40 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 f04980ccc..f8dccdc5b 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 61bff0d76..2a6124bce 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; }); -- Gitee