From 51870d2b80df3851a45a87e637f46cb1ab289d1f Mon Sep 17 00:00:00 2001 From: shaoyuanzhao Date: Wed, 5 Jun 2024 22:46:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=9B=E5=87=BA=E7=AB=AF=E4=BA=91=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E5=BC=80=E5=A7=8B=E5=92=8C=E7=BB=93=E6=9D=9F=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: shaoyuanzhao --- .../framework/include/cloud/cloud_event.h | 1 + .../include/cloud/cloud_sync_status_event.h | 45 +++++++++++ .../distributeddataservice/service/BUILD.gn | 1 + .../service/cloud/cloud_sync_report.cpp | 75 +++++++++++++++++++ .../service/cloud/cloud_sync_report.h | 34 +++++++++ .../service/cloud/sync_manager.cpp | 6 ++ 6 files changed, 162 insertions(+) create mode 100644 services/distributeddataservice/framework/include/cloud/cloud_sync_status_event.h create mode 100644 services/distributeddataservice/service/cloud/cloud_sync_report.cpp create mode 100644 services/distributeddataservice/service/cloud/cloud_sync_report.h diff --git a/services/distributeddataservice/framework/include/cloud/cloud_event.h b/services/distributeddataservice/framework/include/cloud/cloud_event.h index 6d33219c5..c540197e0 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -32,6 +32,7 @@ public: CLOUD_SHARE, MAKE_QUERY, CLOUD_SYNC_FINISHED, + CLOUD_SYNC_STATUS, CLOUD_BUTT }; diff --git a/services/distributeddataservice/framework/include/cloud/cloud_sync_status_event.h b/services/distributeddataservice/framework/include/cloud/cloud_sync_status_event.h new file mode 100644 index 000000000..f75f2e4c6 --- /dev/null +++ b/services/distributeddataservice/framework/include/cloud/cloud_sync_status_event.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2024 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_CLOUD_CLOUD_SYNC_STATUS_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_SYNC_STATUS_EVENT_H + +#include +#include "cloud_event.h" + +namespace OHOS::DistributedData { +class API_EXPORT CloudSyncStatusEvent : public CloudEvent { +public: + enum SyncStatus : int32_t { + SATAR, + FINISH, + }; + + CloudSyncStatusEvent(StoreInfo storeInfo, SyncStatus status) + : CloudEvent(CLOUD_SYNC_STATUS, std::move(storeInfo)), status_(std::move(status)) + { + } + + ~CloudSyncStatusEvent() override = default; + + SyncStatus GetCloudSyncStatus() const + { + return status_ ; + } +private: + SyncStatus status_; +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_SYNC_STATUS_EVENT_H diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 051297cd7..b826ff779 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -72,6 +72,7 @@ ohos_shared_library("distributeddatasvc") { "bootstrap/src/bootstrap.cpp", "cloud/cloud_service_impl.cpp", "cloud/cloud_service_stub.cpp", + "cloud/cloud_sync_report.cpp", "cloud/cloud_types_util.cpp", "cloud/cloud_value_util.cpp", "cloud/sync_manager.cpp", diff --git a/services/distributeddataservice/service/cloud/cloud_sync_report.cpp b/services/distributeddataservice/service/cloud/cloud_sync_report.cpp new file mode 100644 index 000000000..97e0c3fda --- /dev/null +++ b/services/distributeddataservice/service/cloud/cloud_sync_report.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2024 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. + */ +#define LOG_TAG "CloudSyncReport" + +#include "cloud_sync_report.h" +#include "cloud/cloud_sync_status_event.h" +#include "eventcenter/event_center.h" +#include "log_print.h" + +namespace OHOS::CloudData { +using namespace DistributedData; +CloudSyncReport &CloudSyncReport::GetInstance() +{ + static CloudSyncReport instance; + return instance; +} + +void CloudSyncReport::CloudSyncStart(const StoreInfo &storeInfo) +{ + bool postEvent = false; + syncDbInfo_.Compute(storeInfo.bundleName, [&storeInfo, postEvent](const std::string &key, + std::map &value) { + auto it = value.find(storeInfo.storeName); + if (it == value.end()) { + value.insert_or_assign(storeInfo.storeName, 1); + postEvent = true; + } else { + it->second++; + } + return true; + }); + if (postEvent) { + StoreInfo info = storeInfo; + auto evt = std::make_unique(std::move(info), CloudSyncStatusEvent::SyncStatus::SATAR); + EventCenter::GetInstance().PostEvent(std::move(evt)); + } +} + +void CloudSyncReport::CloudSyncFinish(const StoreInfo &storeInfo) +{ + bool postEvent = false; + syncDbInfo_.EraseIf([&storeInfo, postEvent](const std::string &key, std::map &value) { + if (key != storeInfo.bundleName) { + return false; + } + auto it = value.find(storeInfo.storeName); + if (it == value.end()) { + return false; + } + it->second--; + if (it->second == 0) { + value.erase(storeInfo.storeName); + postEvent = true; + } + return value.empty(); + }); + if (postEvent) { + StoreInfo info = storeInfo; + auto evt = std::make_unique(std::move(info), CloudSyncStatusEvent::SyncStatus::FINISH); + EventCenter::GetInstance().PostEvent(std::move(evt)); + } +} +} // OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_sync_report.h b/services/distributeddataservice/service/cloud/cloud_sync_report.h new file mode 100644 index 000000000..6ff2111bf --- /dev/null +++ b/services/distributeddataservice/service/cloud/cloud_sync_report.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2024 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_CLOUD_CLOUD_SYNC_REPORT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SYNC_REPORT_H +#include "concurrent_map.h" +#include "store/store_info.h" + +namespace OHOS::CloudData { +class CloudSyncReport { +public: + using StoreInfo = DistributedData::StoreInfo; + static CloudSyncReport &GetInstance(); + void CloudSyncStart(const StoreInfo &storeInfo); + void CloudSyncFinish(const StoreInfo &storeInfo); + +private: + // bundle, + ConcurrentMap> syncDbInfo_; +}; +} // namespace OHOS::CloudData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SYNC_REPORT_H \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index f9c069584..64913fb8c 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -21,6 +21,7 @@ #include "cloud/cloud_server.h" #include "cloud/schema_meta.h" #include "cloud/sync_event.h" +#include "cloud_sync_report.h" #include "cloud_value_util.h" #include "device_manager_adapter.h" #include "dfx/radar_reporter.h" @@ -334,12 +335,16 @@ std::function SyncManager::GetSyncHandler(Retryer retryer) } int32_t code = details.begin()->second.code; if (details.begin()->second.progress == GenProgress::SYNC_FINISH) { + CloudSyncReport::GetInstance().CloudSyncFinish(storeInfo); QueryKey queryKey{ GetAccountId(storeInfo.user), storeInfo.bundleName, "" }; UpdateFinishSyncInfo(queryKey, storeInfo.syncId, code); } retryer(GetInterval(code), code); } : GetCallback(evt.GetAsyncDetail(), storeInfo), syncParam); + if (status == E_OK) { + CloudSyncReport::GetInstance().CloudSyncStart(storeInfo); + } if (status != E_OK && async) { detail.code = status; async(std::move(details)); @@ -606,6 +611,7 @@ std::function SyncManager::GetCallback(const Gen if (result.begin()->second.progress != GenProgress::SYNC_FINISH) { return; } + CloudSyncReport::GetInstance().CloudSyncFinish(storeInfo); auto id = GetAccountId(storeInfo.user); if (id.empty()) { -- Gitee