From 512d642d6901b7b3f7c0c39cff345c591bf0c741 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Thu, 4 May 2023 21:07:20 +0800 Subject: [PATCH 01/95] feat:Support cloud based data schema settings Signed-off-by: htt1997 --- .../distributeddataservice/framework/BUILD.gn | 1 + .../framework/cloud/cloud_event.cpp | 33 +++ .../framework/cloud/cloud_info.cpp | 2 + .../framework/include/cloud/cloud_event.h | 50 ++++ .../framework/include/cloud/cloud_info.h | 1 + .../framework/include/eventcenter/event.h | 3 +- .../framework/include/store/auto_cache.h | 5 +- .../framework/store/auto_cache.cpp | 18 ++ .../distributeddataservice/service/BUILD.gn | 8 +- .../service/cloud/cloud_service_impl.cpp | 252 ++++++++++++++++++ .../service/cloud/cloud_service_impl.h | 56 ++++ .../service/cloud/cloud_service_stub.cpp | 104 ++++++++ .../service/cloud/cloud_service_stub.h | 37 +++ .../service/cloud/cloud_syncer.cpp | 28 ++ .../service/cloud/cloud_syncer.h | 30 +++ .../service/rdb/rdb_general_store.cpp | 110 ++++++++ .../service/rdb/rdb_general_store.h | 58 ++++ .../service/rdb/rdb_service_impl.cpp | 135 ++++++++-- .../service/rdb/rdb_service_impl.h | 12 +- .../service/rdb/rdb_service_stub.cpp | 22 +- .../service/rdb/rdb_service_stub.h | 9 +- .../service/test/cloud_data_test.cpp | 113 ++++++++ 22 files changed, 1057 insertions(+), 30 deletions(-) create mode 100644 services/distributeddataservice/framework/cloud/cloud_event.cpp create mode 100644 services/distributeddataservice/framework/include/cloud/cloud_event.h create mode 100644 services/distributeddataservice/service/cloud/cloud_service_impl.cpp create mode 100644 services/distributeddataservice/service/cloud/cloud_service_impl.h create mode 100644 services/distributeddataservice/service/cloud/cloud_service_stub.cpp create mode 100644 services/distributeddataservice/service/cloud/cloud_service_stub.h create mode 100644 services/distributeddataservice/service/cloud/cloud_syncer.cpp create mode 100644 services/distributeddataservice/service/cloud/cloud_syncer.h create mode 100644 services/distributeddataservice/service/rdb/rdb_general_store.cpp create mode 100644 services/distributeddataservice/service/rdb/rdb_general_store.h create mode 100644 services/distributeddataservice/service/test/cloud_data_test.cpp diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index 4ce626b7..ea3b510f 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -36,6 +36,7 @@ ohos_shared_library("distributeddatasvcfwk") { "checker/checker_manager.cpp", "cloud/asset_loader.cpp", "cloud/cloud_db.cpp", + "cloud/cloud_event.cpp", "cloud/cloud_info.cpp", "cloud/cloud_server.cpp", "cloud/schema_meta.cpp", diff --git a/services/distributeddataservice/framework/cloud/cloud_event.cpp b/services/distributeddataservice/framework/cloud/cloud_event.cpp new file mode 100644 index 00000000..11f43a39 --- /dev/null +++ b/services/distributeddataservice/framework/cloud/cloud_event.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 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 "cloud/cloud_event.h" + +namespace OHOS::DistributedData { +CloudEvent::CloudEvent(int32_t evtId, CloudEvent::StoreInfo storeInfo, const std::string &featureName) + : Event(evtId), featureName_(featureName), storeInfo_(storeInfo) +{ +} + +std::string CloudEvent::GetFeatureName() const +{ + return featureName_; +} + +CloudEvent::StoreInfo CloudEvent::GetStoreInfo() const +{ + return storeInfo_; +} +} \ No newline at end of file diff --git a/services/distributeddataservice/framework/cloud/cloud_info.cpp b/services/distributeddataservice/framework/cloud/cloud_info.cpp index f3f141eb..d3560bb8 100644 --- a/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -44,6 +44,7 @@ bool CloudInfo::AppInfo::Marshal(Serializable::json &node) const SetValue(node[GET_NAME(bundleName)], bundleName); SetValue(node[GET_NAME(appId)], appId); SetValue(node[GET_NAME(version)], version); + SetValue(node[GET_NAME(instanceId)], instanceId); SetValue(node[GET_NAME(cloudSwitch)], cloudSwitch); return true; } @@ -53,6 +54,7 @@ bool CloudInfo::AppInfo::Unmarshal(const Serializable::json &node) GetValue(node, GET_NAME(bundleName), bundleName); GetValue(node, GET_NAME(appId), appId); GetValue(node, GET_NAME(version), version); + GetValue(node, GET_NAME(instanceId), instanceId); GetValue(node, GET_NAME(cloudSwitch), cloudSwitch); return true; } diff --git a/services/distributeddataservice/framework/include/cloud/cloud_event.h b/services/distributeddataservice/framework/include/cloud/cloud_event.h new file mode 100644 index 00000000..d01ab0f2 --- /dev/null +++ b/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 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_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_EVENT_H + +#include +#include "eventcenter/event.h" + +namespace OHOS::DistributedData { +class API_EXPORT CloudEvent : public Event { +public: + enum : int32_t { + FEATURE_INIT = EVT_CLOUD, + GET_SCHEMA, + NEED_CREATE, + CLOUD_BUTT + }; + + struct StoreInfo{ + uint32_t tokenId = 0; + std::string bundleName; + std::string storeName; + int32_t instanceId = 0; + int32_t schemaVersion = -1; + }; + + CloudEvent(int32_t evtId, StoreInfo storeInfo, const std::string &featureName = "relational_store"); + ~CloudEvent() = default; + std::string GetFeatureName() const; + StoreInfo GetStoreInfo() const; + +private: + std::string featureName_; + StoreInfo storeInfo_; +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_EVENT_H diff --git a/services/distributeddataservice/framework/include/cloud/cloud_info.h b/services/distributeddataservice/framework/include/cloud/cloud_info.h index 5576bca8..d16c8cfc 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_info.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -23,6 +23,7 @@ public: std::string bundleName = ""; std::string appId = ""; uint64_t version = 0; + int32_t instanceId = 0; bool cloudSwitch = false; bool Marshal(json &node) const override; diff --git a/services/distributeddataservice/framework/include/eventcenter/event.h b/services/distributeddataservice/framework/include/eventcenter/event.h index d574e0f5..27bf9df2 100644 --- a/services/distributeddataservice/framework/include/eventcenter/event.h +++ b/services/distributeddataservice/framework/include/eventcenter/event.h @@ -27,7 +27,8 @@ public: EVT_INVALID, EVT_INITED, EVT_UPDATE, - EVT_CUSTOM = 0x1000 + EVT_CUSTOM = 0x1000, + EVT_CLOUD = 0x2000 }; API_EXPORT Event(int32_t evtId); API_EXPORT Event(Event &&) noexcept = delete; diff --git a/services/distributeddataservice/framework/include/store/auto_cache.h b/services/distributeddataservice/framework/include/store/auto_cache.h index ffed302f..0dd4e15d 100644 --- a/services/distributeddataservice/framework/include/store/auto_cache.h +++ b/services/distributeddataservice/framework/include/store/auto_cache.h @@ -27,6 +27,7 @@ #include "store/general_watcher.h" #include "visibility.h" namespace OHOS::DistributedData { +class SchemaMeta; class AutoCache { public: using Error = GeneralError; @@ -44,7 +45,9 @@ public: API_EXPORT void Bind(std::shared_ptr executor); API_EXPORT Store GetStore(const StoreMetaData &meta, const Watchers &watchers); - + + API_EXPORT int32_t CreateTable(const StoreMetaData &storeMetaData, const SchemaMeta &schemaMeta); + API_EXPORT void CloseStore(uint32_t tokenId, const std::string &storeId); API_EXPORT void CloseExcept(const std::set &users); diff --git a/services/distributeddataservice/framework/store/auto_cache.cpp b/services/distributeddataservice/framework/store/auto_cache.cpp index 212f6c1b..76ec3966 100644 --- a/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/services/distributeddataservice/framework/store/auto_cache.cpp @@ -79,6 +79,24 @@ AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers & return store; } +int32_t AutoCache::CreateTable(const StoreMetaData &storeMetaData, const SchemaMeta &schemaMeta) +{ + stores_.Compute(storeMetaData.tokenId, + [this, &storeMetaData, &schemaMeta](auto &, std::map &stores) -> bool { + auto it = stores.find(storeMetaData.storeId); + if (it != stores.end()) { + //it->second->CreateTable(schemaMeta) + return true; + } + auto *dbStore = creators_[storeMetaData.storeType](storeMetaData); + if (dbStore != nullptr) { + //dbStore->CreateTable(schemaMeta) + } + return false; + }); + return 0; +} + void AutoCache::CloseStore(uint32_t tokenId, const std::string &storeId) { stores_.ComputeIfPresent(tokenId, [&storeId](auto &key, std::map &delegates) { diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index d05b54c5..ee18efff 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -21,6 +21,7 @@ config("module_public_config") { include_dirs = [ "backup/include", "bootstrap/include", + "cloud", "config/include", "crypto/include", "datashare", @@ -51,12 +52,15 @@ config("module_public_config") { ohos_shared_library("distributeddatasvc") { include_dirs = [ "../../../../data_object/frameworks/innerkitsimpl/include", + "../../../../relational_store/interfaces/inner_api/cloud_data/include", "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/rdb/include", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/include/distributeddb", ] sources = [ "backup/src/backup_manager.cpp", "bootstrap/src/bootstrap.cpp", + "cloud/cloud_service_impl.cpp", + "cloud/cloud_service_stub.cpp", + "cloud/cloud_syncer.cpp", "config/src/config_factory.cpp", "config/src/model/backup_config.cpp", "config/src/model/checker_config.cpp", @@ -94,6 +98,7 @@ ohos_shared_library("distributeddatasvc") { "object/object_service_impl.cpp", "object/object_service_stub.cpp", "permission/src/permit_delegate.cpp", + "rdb/rdb_general_store.cpp", "rdb/rdb_notifier_proxy.cpp", "rdb/rdb_result_set_impl.cpp", "rdb/rdb_result_set_stub.cpp", @@ -129,6 +134,7 @@ ohos_shared_library("distributeddatasvc") { "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", + "relational_store:cloud_data", "relational_store:native_rdb", "relational_store:rdb_data_share_adapter", "resource_management:global_resmgr", diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp new file mode 100644 index 00000000..fafc5c9e --- /dev/null +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2023 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 "CloudServiceImpl" + +#include "cloud_service_impl.h" + +#include "account/account_delegate.h" +#include "checker/checker_manager.h" +#include "cloud/cloud_event.h" +#include "cloud/cloud_server.h" +#include "cloud_syncer.h" +#include "communicator/device_manager_adapter.h" +#include "eventcenter/event_center.h" +#include "feature/feature_system.h" +#include "ipc_skeleton.h" +#include "log_print.h" +#include "metadata/meta_data_manager.h" +#include "metadata/store_meta_data.h" +#include "utils/anonymous.h" +#include "store/auto_cache.h" + +namespace OHOS::CloudData { +using namespace DistributedData; +using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; +__attribute__((used)) CloudServiceImpl::Factory CloudServiceImpl::factory_; +CloudServiceImpl::Factory::Factory() +{ + FeatureSystem::GetInstance().RegisterCreator(CloudServiceImpl::SERVICE_NAME, [this]() { + if (product_ == nullptr) { + product_ = std::make_shared(); + } + return product_; + }); +} + +CloudServiceImpl::Factory::~Factory() {} + +CloudServiceImpl::CloudServiceImpl() +{ + EventCenter::GetInstance().Subscribe(CloudEvent::FEATURE_INIT, [this](const Event &event) { + auto &rdbEvent = static_cast(event); + CloudInfo cloudInfo; + cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); + if (GetServerInfo(cloudInfo) != SUCCESS) { + ZLOGE("failed, user:%{public}d", cloudInfo.user); + return; + } + UpdateCloudInfo(cloudInfo); + AddSchema(cloudInfo); + }); + + EventCenter::GetInstance().Subscribe(CloudEvent::GET_SCHEMA, [this](const Event &event) { + auto &rdbEvent = static_cast(event); + CloudInfo cloudInfo; + cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); + if (GetServerInfo(cloudInfo) != SUCCESS) { + ZLOGE("failed, user:%{public}d", cloudInfo.user); + return; + } + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + return; + } + SchemaMeta schemaMeta; + std::string schemaKey = cloudInfo.GetSchemaKey(rdbEvent.GetStoreInfo().bundleName); + if (!MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true) || + schemaMeta.version != rdbEvent.GetStoreInfo().schemaVersion) { + schemaMeta = instance->GetAppSchema(cloudInfo.user, rdbEvent.GetStoreInfo().bundleName); + MetaDataManager::GetInstance().SaveMeta(schemaKey, schemaMeta, true); + } + StoreMetaData storeMetaData; + storeMetaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + storeMetaData.user = std::to_string(cloudInfo.user); + storeMetaData.bundleName = rdbEvent.GetStoreInfo().bundleName; + storeMetaData.storeId = rdbEvent.GetStoreInfo().storeName; + storeMetaData.instanceId = rdbEvent.GetStoreInfo().instanceId; + if (!MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData, true)) { + return; + } + AutoCache::GetInstance().CreateTable(storeMetaData, schemaMeta); + for (auto &database : schemaMeta.databases) { + if (database.name != rdbEvent.GetStoreInfo().storeName /* || don't need sync*/) { + continue; + } + auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, database); + //do sync + } + }); +} + +int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &switches) +{ + CloudInfo cloudInfo; + cloudInfo.id = id; + if (GetCloudInfo(cloudInfo) != SUCCESS && GetServerInfo(cloudInfo) != SUCCESS) { + return INVALID_ARGUMENT; + } + cloudInfo.enableCloud = true; + for (const auto &item : switches) { + auto it = std::find_if(cloudInfo.apps.begin(), cloudInfo.apps.end(), + [&item](const CloudInfo::AppInfo &appInfo) -> bool { + return appInfo.bundleName == item.first; + }); + if (it == cloudInfo.apps.end()) { + continue; + } + (*it).cloudSwitch = item.second; + } + if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { + return ERROR; + } + return SUCCESS; +} + +int32_t CloudServiceImpl::DisableCloud(const std::string &id) +{ + CloudInfo cloudInfo; + cloudInfo.id = id; + if (GetCloudInfo(cloudInfo) != SUCCESS && GetServerInfo(cloudInfo) != SUCCESS) { + return INVALID_ARGUMENT; + } + cloudInfo.enableCloud = false; + if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { + return ERROR; + } + return SUCCESS; +} + +int32_t CloudServiceImpl::ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) +{ + CloudInfo cloudInfo; + cloudInfo.id = id; + if (GetCloudInfo(cloudInfo) != SUCCESS && GetServerInfo(cloudInfo) != SUCCESS) { + return INVALID_ARGUMENT; + } + auto it = std::find_if(cloudInfo.apps.begin(), cloudInfo.apps.end(), + [&bundleName](const CloudInfo::AppInfo &appInfo) -> bool { + return appInfo.bundleName == bundleName; + }); + if (it == cloudInfo.apps.end()) { + ZLOGE("bundleName:%{public}s", bundleName.c_str()); + return INVALID_ARGUMENT; + } + (*it).cloudSwitch = appSwitch; + if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { + return ERROR; + } + return SUCCESS; +} + +int32_t CloudServiceImpl::Clean(const std::string &id, const std::map &actions) +{ + CloudInfo cloudInfo; + cloudInfo.id = id; + if (GetCloudInfo(cloudInfo) != SUCCESS && GetServerInfo(cloudInfo) != SUCCESS) { + ZLOGE("id:%{public}s", Anonymous::Change(id).c_str()); + return INVALID_ARGUMENT; + } + auto keys = cloudInfo.GetSchemaKey(); + for (const auto &action : actions) { + if (!cloudInfo.IsExist(action.first)) { + continue; + } + SchemaMeta schemaMeta; + if (MetaDataManager::GetInstance().LoadMeta(keys[action.first], schemaMeta, true)) { + } + } + return SUCCESS; +} + +int32_t CloudServiceImpl::NotifyDataChange(const std::string &id, const std::string &bundleName) +{ + return 0; +} + +int32_t CloudServiceImpl::GetCloudInfo(CloudInfo &cloudInfo) +{ + auto tokenId = IPCSkeleton::GetCallingTokenID(); + cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { + ZLOGE("invalid argument id:%{public}s, user:%{public}d", Anonymous::Change(cloudInfo.id).c_str(), + cloudInfo.user); + return ERROR; + } + return SUCCESS; +} + +int32_t CloudServiceImpl::GetServerInfo(CloudInfo &cloudInfo) +{ + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + return SERVER_UNAVAILABLE; + } + cloudInfo = instance->GetServerInfo(cloudInfo.user); + if (!cloudInfo.IsValid()) { + return ERROR; + } + return SUCCESS; +} + +void CloudServiceImpl::UpdateCloudInfo(CloudInfo &cloudInfo) +{ + CloudInfo oldInfo; + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), oldInfo, true)) { + MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); + return; + } + oldInfo.totalSpace = cloudInfo.totalSpace; + oldInfo.remainSpace = cloudInfo.remainSpace; + oldInfo.apps = std::move(cloudInfo.apps); + cloudInfo = oldInfo; + MetaDataManager::GetInstance().SaveMeta(oldInfo.GetKey(), oldInfo, true); +} + +void CloudServiceImpl::AddSchema(CloudInfo &cloudInfo) +{ + auto keys = cloudInfo.GetSchemaKey(); + for (const auto &key : keys) { + SchemaMeta schemaMeta; + if (MetaDataManager::GetInstance().LoadMeta(key.second, schemaMeta, true)) { + continue; + } + if (GetAppSchema(cloudInfo.user, key.first, schemaMeta) != SUCCESS) { + continue; + } + MetaDataManager::GetInstance().SaveMeta(key.second, schemaMeta, true); + } +} + +int32_t CloudServiceImpl::GetAppSchema(int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta) +{ + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + return SERVER_UNAVAILABLE; + } + schemaMeta = instance->GetAppSchema(user, bundleName); + return SUCCESS; +} +} // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h new file mode 100644 index 00000000..9fa2ec68 --- /dev/null +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 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_SERVICE_IMPL_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SERVICE_IMPL_H + +#include +#include "cloud_service_stub.h" +#include "cloud/cloud_info.h" +#include "cloud/schema_meta.h" + +namespace OHOS::CloudData { +class CloudServiceImpl : public CloudServiceStub { +public: + CloudServiceImpl(); + ~CloudServiceImpl() = default; + int32_t EnableCloud(const std::string &id, const std::map &switches) override; + int32_t DisableCloud(const std::string &id) override; + int32_t ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) override; + int32_t Clean(const std::string &id, const std::map &actions) override; + int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) override; + +private: + class Factory { + public: + Factory(); + ~Factory(); + private: + std::shared_ptr product_; + }; + static Factory factory_; + + using CloudInfo = DistributedData::CloudInfo; + using SchemaMeta = DistributedData::SchemaMeta; + + void UpdateCloudInfo(CloudInfo &cloudInfo); + void AddSchema(CloudInfo &cloudInfo); + int32_t GetCloudInfo(CloudInfo &cloudInfo); + int32_t GetServerInfo(CloudInfo &cloudInfo); + int32_t GetAppSchema(int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta); +}; +} // namespace OHOS::DistributedData + +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SERVICE_IMPL_H diff --git a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp new file mode 100644 index 00000000..2ccf869e --- /dev/null +++ b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2023 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 "CloudServiceStub" +#include "cloud_service_stub.h" + +#include "ipc_skeleton.h" +#include "itypes_util.h" +#include "log_print.h" +#include "utils/anonymous.h" +namespace OHOS::CloudData { +using namespace DistributedData; +const CloudServiceStub::Handler CloudServiceStub::HANDLERS[TRANS_BUTT] = { + &CloudServiceStub::OnEnableCloud, + &CloudServiceStub::OnDisableCloud, + &CloudServiceStub::OnChangeAppSwitch, + &CloudServiceStub::OnClean, + &CloudServiceStub::OnNotifyDataChange, +}; + +int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply) +{ + ZLOGI("code:%{public}u callingPid:%{public}u", code, IPCSkeleton::GetCallingPid()); + std::u16string local = CloudServiceStub::GetDescriptor(); + std::u16string remote = data.ReadInterfaceToken(); + if (local != remote) { + ZLOGE("local is not equal to remote"); + return -1; + } + + if (TRANS_HEAD > code || code >= TRANS_BUTT || HANDLERS[code] == nullptr) { + ZLOGE("not support code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); + return -1; + } + std::string id; + if (!ITypesUtil::Unmarshal(data, id)) { + ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + return (this->*HANDLERS[code])(id, data, reply); +} + +int32_t CloudServiceStub::OnEnableCloud(const std::string &id, MessageParcel &data, MessageParcel &reply) +{ + std::map switches; + if (!ITypesUtil::Unmarshal(data, switches)) { + ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + auto result = EnableCloud(id, switches); + return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnDisableCloud(const std::string &id, MessageParcel &data, MessageParcel &reply) +{ + auto result = DisableCloud(id); + return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnChangeAppSwitch(const std::string &id, MessageParcel &data, MessageParcel &reply) +{ + std::string bundleName; + int32_t appSwitch = SWITCH_OFF; + if (!ITypesUtil::Unmarshal(data, bundleName, appSwitch)) { + ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + auto result = ChangeAppSwitch(id, bundleName, appSwitch); + return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnClean(const std::string &id, MessageParcel &data, MessageParcel &reply) +{ + std::map actions; + if (!ITypesUtil::Unmarshal(data, actions)) { + ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + auto result = Clean(id, actions); + return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnNotifyDataChange(const std::string &id, MessageParcel &data, MessageParcel &reply) +{ + std::string bundleName; + if (!ITypesUtil::Unmarshal(data, bundleName)) { + ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + auto result = NotifyDataChange(id, bundleName); + return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} +} // namespace OHOS::CloudData diff --git a/services/distributeddataservice/service/cloud/cloud_service_stub.h b/services/distributeddataservice/service/cloud/cloud_service_stub.h new file mode 100644 index 00000000..4175015d --- /dev/null +++ b/services/distributeddataservice/service/cloud/cloud_service_stub.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 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_SERVICE_STUB_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SERVICE_STUB_H +#include "cloud_service.h" +#include "feature/feature_system.h" +#include "iremote_broker.h" +namespace OHOS::CloudData { +class CloudServiceStub : public CloudService, public DistributedData::FeatureSystem::Feature { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.CloudData.CloudServer"); + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) override; + +private: + using Handler = int32_t (CloudServiceStub::*)(const std::string &id, MessageParcel &data, MessageParcel &reply); + int32_t OnEnableCloud(const std::string &id, MessageParcel &data, MessageParcel &reply); + int32_t OnDisableCloud(const std::string &id, MessageParcel &data, MessageParcel &reply); + int32_t OnChangeAppSwitch(const std::string &id, MessageParcel &data, MessageParcel &reply); + int32_t OnClean(const std::string &id, MessageParcel &data, MessageParcel &reply); + int32_t OnNotifyDataChange(const std::string &id, MessageParcel &data, MessageParcel &reply); + static const Handler HANDLERS[TRANS_BUTT]; +}; +} // namespace OHOS::CloudData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SERVICE_STUB_H diff --git a/services/distributeddataservice/service/cloud/cloud_syncer.cpp b/services/distributeddataservice/service/cloud/cloud_syncer.cpp new file mode 100644 index 00000000..f920c5ec --- /dev/null +++ b/services/distributeddataservice/service/cloud/cloud_syncer.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 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 "cloud_syncer.h" + +namespace OHOS::CloudData { +CloudSyncer &CloudSyncer::GetInstance() +{ + static CloudSyncer instance; + return instance; +} + +void CloudSyncer::Sync(const DistributedData::CloudInfo &cloudInfo) +{ +} +} // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_syncer.h b/services/distributeddataservice/service/cloud/cloud_syncer.h new file mode 100644 index 00000000..c9dd7971 --- /dev/null +++ b/services/distributeddataservice/service/cloud/cloud_syncer.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 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_SYNCER_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SYNCER_H + +#include "cloud/cloud_info.h" + +namespace OHOS::CloudData { +class CloudSyncer { +public: + static CloudSyncer &GetInstance(); + void Sync(const DistributedData::CloudInfo &cloudInfo); + +private: +}; +} // namespace OHOS::CloudData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SYNCER_H diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp new file mode 100644 index 00000000..ab5f74e1 --- /dev/null +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2023 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 "RdbGeneralStore" +#include "rdb_general_store.h" + +#include "crypto_manager.h" +#include "log_print.h" +#include "metadata/meta_data_manager.h" +#include "metadata/secret_key_meta_data.h" +#include "rdb_helper.h" +#include "relational_store_manager.h" +namespace OHOS::DistributedRdb { +using namespace DistributedData; +using namespace OHOS::NativeRdb; +class RdbOpenCallbackImpl : public RdbOpenCallback { +public: + int OnCreate(RdbStore &rdbStore) override + { + return NativeRdb::E_OK; + } + int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override + { + return NativeRdb::E_OK; + } +}; +RdbGeneralStore::RdbGeneralStore(const StoreMetaData &meta) : manager_(meta.appId, meta.user, meta.instanceId) +{ +} + +int32_t RdbGeneralStore::Close() +{ + return 0; +} + +int32_t RdbGeneralStore::Execute(const std::string &table, const std::string &sql) +{ + return GeneralError::E_OK; +} + +int32_t RdbGeneralStore::BatchInsert(const std::string &table, VBuckets &&values) +{ + return 0; +} + +int32_t RdbGeneralStore::BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) +{ + return 0; +} + +int32_t RdbGeneralStore::Delete(const std::string &table, const std::string &sql, Values &&args) +{ + return 0; +} + +std::shared_ptr RdbGeneralStore::Query(const std::string &table, const std::string &sql, Values &&args) +{ + return std::shared_ptr(); +} + +std::shared_ptr RdbGeneralStore::Query(const std::string &table, const GenQuery &query) +{ + return std::shared_ptr(); +} + +int32_t RdbGeneralStore::Watch(int32_t origin, Watcher &watcher) +{ + return 0; +} + +int32_t RdbGeneralStore::Unwatch(int32_t origin, Watcher &watcher) +{ + return 0; +} + +int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait) +{ + return 0; +} + +NativeRdb::ValuesBucket RdbGeneralStore::Convert(VBucket &&bucket) +{ + ValuesBucket rdbVBucket; + return rdbVBucket; +} + +NativeRdb::ValueObject RdbGeneralStore::Convert(Value &&value) +{ + ValueObject rdbValue; + return rdbValue; +} + +DistributedData::Value RdbGeneralStore::Convert(ValueObject &&rdbValue) +{ + DistributedData::Value value; + return value; +} + +} // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h new file mode 100644 index 00000000..272f7fcd --- /dev/null +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 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_DATAMGR_SERVICE_RDB_RDB_GENERAL_STORE_H +#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_RDB_GENERAL_STORE_H +#include +#include "relational_store_delegate.h" +#include "relational_store_manager.h" + +#include "rdb_store.h" +#include "store/general_store.h" +#include "metadata/store_meta_data.h" +namespace OHOS::DistributedRdb { +class API_EXPORT RdbGeneralStore : public DistributedData::GeneralStore { +public: + using Cursor = DistributedData::Cursor; + using GenQuery = DistributedData::GenQuery; + using VBucket = DistributedData::VBucket; + using VBuckets = DistributedData::VBuckets; + using Values = DistributedData::Values; + using StoreMetaData = DistributedData::StoreMetaData; + using RdbStore = OHOS::NativeRdb::RdbStore; + using RdbDelegate = DistributedDB::RelationalStoreDelegate; + using RdbManager = DistributedDB::RelationalStoreManager; + RdbGeneralStore(const StoreMetaData &metaData); + int32_t Close() override; + int32_t Execute(const std::string &table, const std::string &sql) override; + int32_t BatchInsert(const std::string &table, VBuckets &&values) override; + int32_t BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) override; + int32_t Delete(const std::string &table, const std::string &sql, Values &&args) override; + std::shared_ptr Query(const std::string &table, const std::string &sql, Values &&args) override; + std::shared_ptr Query(const std::string &table, const GenQuery &query) override; + int32_t Watch(int32_t origin, Watcher &watcher) override; + int32_t Unwatch(int32_t origin, Watcher &watcher) override; + int32_t Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait) override; + +private: + NativeRdb::ValuesBucket Convert(DistributedData::VBucket &&bucket); + NativeRdb::ValueObject Convert(DistributedData::Value &&value); + DistributedData::Value Convert(NativeRdb::ValueObject &&rdbValue); + + RdbManager manager_; + std::shared_ptr store_; +}; +} // namespace OHOS::DistributedRdb +#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_RDB_GENERAL_STORE_H diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 6f7eb43c..69181184 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -17,22 +17,31 @@ #include "accesstoken_kit.h" #include "account/account_delegate.h" #include "checker/checker_manager.h" +#include "cloud/cloud_event.h" #include "communicator/device_manager_adapter.h" #include "crypto_manager.h" +#include "directory_manager.h" +#include "eventcenter/event_center.h" #include "ipc_skeleton.h" #include "log_print.h" #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" #include "permission/permission_validator.h" #include "rdb_notifier_proxy.h" +#include "store/auto_cache.h" #include "types_export.h" #include "utils/anonymous.h" +#include "utils/constant.h" +#include "utils/converter.h" +#include "cloud/schema_meta.h" +#include "rdb_general_store.h" using OHOS::DistributedKv::AccountDelegate; using OHOS::DistributedData::CheckerManager; using OHOS::DistributedData::MetaDataManager; using OHOS::DistributedData::StoreMetaData; using OHOS::DistributedData::Anonymous; using namespace OHOS::DistributedData; +using namespace OHOS::Security::AccessToken; using DistributedDB::RelationalStoreManager; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; @@ -41,8 +50,12 @@ namespace OHOS::DistributedRdb { __attribute__((used)) RdbServiceImpl::Factory RdbServiceImpl::factory_; RdbServiceImpl::Factory::Factory() { - FeatureSystem::GetInstance().RegisterCreator("relational_store", - []() { return std::make_shared(); }); + FeatureSystem::GetInstance().RegisterCreator("relational_store", []() { + return std::make_shared(); + }); + AutoCache::GetInstance().RegCreator(RDB_DEVICE_COLLABORATION, [](const StoreMetaData &metaData) -> GeneralStore* { + return new RdbGeneralStore(metaData); + }); } RdbServiceImpl::Factory::~Factory() @@ -136,13 +149,13 @@ void RdbServiceImpl::OnClientDied(pid_t pid) }); } -bool RdbServiceImpl::CheckAccess(const RdbSyncerParam ¶m) +bool RdbServiceImpl::CheckAccess(const std::string& bundleName, const std::string& storeName) { CheckerManager::StoreInfo storeInfo; storeInfo.uid = IPCSkeleton::GetCallingUid(); storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); - storeInfo.bundleName = param.bundleName_; - storeInfo.storeId = RdbSyncer::RemoveSuffix(param.storeName_); + storeInfo.bundleName = bundleName; + storeInfo.storeId = RdbSyncer::RemoveSuffix(storeName); auto instanceId = RdbSyncer::GetInstIndex(storeInfo.tokenId, storeInfo.bundleName); if (instanceId != 0) { return false; @@ -161,15 +174,18 @@ std::string RdbServiceImpl::ObtainDistributedTableName(const std::string &device return DistributedDB::RelationalStoreManager::GetDistributedTableName(uuid, table); } -int32_t RdbServiceImpl::InitNotifier(const RdbSyncerParam& param, const sptr notifier) +int32_t RdbServiceImpl::InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_, "")) { ZLOGE("permission error"); return RDB_ERROR; } - + if (notifier == nullptr) { + ZLOGE("notifier is null"); + return RDB_ERROR; + } pid_t pid = IPCSkeleton::GetCallingPid(); - auto recipient = new(std::nothrow) DeathRecipientImpl([this, pid] { + auto recipient = new (std::nothrow) DeathRecipientImpl([this, pid] { OnClientDied(pid); }); if (recipient == nullptr) { @@ -278,7 +294,7 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables) { ZLOGI("enter"); - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -292,7 +308,7 @@ int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const int32_t RdbServiceImpl::DoSync(const RdbSyncerParam ¶m, const SyncOption &option, const RdbPredicates &predicates, SyncResult &result) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -315,7 +331,7 @@ void RdbServiceImpl::OnAsyncComplete(pid_t pid, uint32_t seqNum, const SyncResul int32_t RdbServiceImpl::DoAsync(const RdbSyncerParam ¶m, uint32_t seqNum, const SyncOption &option, const RdbPredicates &predicates) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -325,10 +341,9 @@ int32_t RdbServiceImpl::DoAsync(const RdbSyncerParam ¶m, uint32_t seqNum, co if (syncer == nullptr) { return RDB_ERROR; } - return syncer->DoAsync(option, predicates, - [this, pid, seqNum] (const SyncResult& result) { - OnAsyncComplete(pid, seqNum, result); - }); + return syncer->DoAsync(option, predicates, [this, pid, seqNum](const SyncResult &result) { + OnAsyncComplete(pid, seqNum, result); + }); } std::string RdbServiceImpl::TransferStringToHex(const std::string &origStr) @@ -379,7 +394,7 @@ int32_t RdbServiceImpl::DoUnSubscribe(const RdbSyncerParam& param) int32_t RdbServiceImpl::RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql, const std::vector& selectionArgs, sptr& resultSet) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -394,7 +409,7 @@ int32_t RdbServiceImpl::RemoteQuery(const RdbSyncerParam& param, const std::stri int32_t RdbServiceImpl::CreateRDBTable( const RdbSyncerParam ¶m, const std::string &writePermission, const std::string &readPermission) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -422,7 +437,7 @@ int32_t RdbServiceImpl::CreateRDBTable( int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) { - if (!CheckAccess(param)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } @@ -446,4 +461,86 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) delete syncer; return RDB_OK; } + +int32_t RdbServiceImpl::OnInitialize() +{ + CloudEvent::StoreInfo storeInfo = { IPCSkeleton::GetCallingTokenID() }; + auto initEvt = std::make_unique(CloudEvent::FEATURE_INIT, storeInfo); + EventCenter::GetInstance().PostEvent(std::move(initEvt)); + return RDB_OK; +} + +int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) +{ + if (!CheckAccess(param.bundleName_, param.storeName_)) { + ZLOGE("permission error"); + return RDB_ERROR; + } + + if (param.schemaVersion == -1) { + return RDB_OK; + } + + auto storeMeta = GetStoreMetaData(param); + StoreMetaData oldMeta; + bool isCreated = MetaDataManager::GetInstance().LoadMeta(storeMeta.GetKey(), oldMeta, true); + if (isCreated && (oldMeta.storeType != storeMeta.storeType || + Constant::NotEqual(oldMeta.isEncrypt, storeMeta.isEncrypt) || + oldMeta.area != storeMeta.area)) { + ZLOGE("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " + "area:%{public}d->%{public}d", + storeMeta.bundleName.c_str(), storeMeta.storeId.c_str(), oldMeta.storeType, storeMeta.storeType, + oldMeta.isEncrypt, storeMeta.isEncrypt, oldMeta.area, storeMeta.area); + return RDB_ERROR; + } + auto saved = MetaDataManager::GetInstance().SaveMeta(storeMeta.GetKey(), storeMeta, true); + if (!saved) { + return RDB_ERROR; + } + CloudEvent::StoreInfo storeInfo{ IPCSkeleton::GetCallingTokenID(), param.bundleName_, param.storeName_, + storeMeta.instanceId, param.schemaVersion }; + auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "relational_store"); + EventCenter::GetInstance().PostEvent(move(event)); + return RDB_OK; +} + +StoreMetaData RdbServiceImpl::GetStoreMetaData(const RdbSyncerParam ¶m) +{ + StoreMetaData metaData; + metaData.uid = IPCSkeleton::GetCallingUid(); + metaData.tokenId = IPCSkeleton::GetCallingTokenID(); + metaData.instanceId = GetInstIndex(metaData.tokenId, param.bundleName_, param.storeName_); + metaData.bundleName = param.bundleName_; + metaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + metaData.storeId = param.storeName_; + metaData.user = std::to_string(AccountDelegate::GetInstance()->GetUserByToken(metaData.tokenId)); + metaData.storeType = param.type_; + metaData.securityLevel = param.level_; + metaData.area = param.area_; + metaData.appId = CheckerManager::GetInstance().GetAppId(Converter::ConvertToStoreInfo(metaData)); + metaData.appType = "harmony"; + metaData.hapName = param.hapName_; + metaData.dataDir = DirectoryManager::GetInstance().GetStorePath(metaData) + "/" + param.storeName_; + metaData.account = AccountDelegate::GetInstance()->GetCurrentAccountId(); + metaData.isEncrypt = param.isEncrypt_; + return metaData; +} + +int32_t RdbServiceImpl::GetInstIndex(uint32_t tokenId, const std::string &bundleName, const std::string &storeName) +{ + if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { + return 0; + } + + HapTokenInfo tokenInfo; + tokenInfo.instIndex = -1; + int errCode = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); + if (errCode != RET_SUCCESS) { + ZLOGE("GetHapTokenInfo error:%{public}d, tokenId:0x%{public}x bundleName:%{public}s storeName:%{public}s", + errCode, tokenId, bundleName.c_str(), storeName.c_str()); + return -1; + } + return tokenInfo.instIndex; +} + } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index 35f93b7c..ae939cd9 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -41,7 +41,7 @@ public: /* IPC interface */ std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; - int32_t InitNotifier(const RdbSyncerParam& param, const sptr notifier) override; + int32_t InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) override; int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector& tables) override; @@ -56,6 +56,10 @@ public: int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; + int32_t OnInitialize() override; + + int32_t GetSchema(const RdbSyncerParam ¶m) override; + protected: int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option, const RdbPredicates& predicates, SyncResult& result) override; @@ -70,7 +74,7 @@ protected: private: std::string GenIdentifier(const RdbSyncerParam& param); - bool CheckAccess(const RdbSyncerParam& param); + bool CheckAccess(const std::string& bundleName, const std::string& storeName); void SyncerTimeout(std::shared_ptr syncer); @@ -78,6 +82,10 @@ private: void OnAsyncComplete(pid_t pid, uint32_t seqNum, const SyncResult& result); + StoreMetaData GetStoreMetaData(const RdbSyncerParam& param); + + int32_t GetInstIndex(uint32_t tokenId, const std::string &appId, const std::string &storeName); + class DeathRecipientImpl : public IRemoteObject::DeathRecipient { public: using DeathCallback = std::function; diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index 931792ac..39fd62d4 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -40,15 +40,31 @@ int32_t RdbServiceStub::OnRemoteObtainDistributedTableName(MessageParcel &data, return RDB_OK; } -int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel &reply) +int32_t RdbServiceStub::OnGetSchema(MessageParcel &data, MessageParcel &reply) { RdbSyncerParam param; - sptr notifier; - if (!ITypesUtil::Unmarshal(data, param, notifier) || notifier == nullptr) { + if (!ITypesUtil::Unmarshal(data, param)) { ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), param.storeName_.c_str()); return IPC_STUB_INVALID_DATA_ERR; } + auto status = GetSchema(param); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return RDB_OK; +} + +int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel &reply) +{ + RdbSyncerParam param; + sptr notifier; + if (!ITypesUtil::Unmarshal(data, param, notifier) || param.bundleName_.empty() || notifier == nullptr) { + ZLOGE("Unmarshal bundleName:%{public}s notifier is nullptr:%{public}d", param.bundleName_.c_str(), + notifier == nullptr); + return IPC_STUB_INVALID_DATA_ERR; + } auto status = InitNotifier(param, notifier); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x", status); diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.h b/services/distributeddataservice/service/rdb/rdb_service_stub.h index 901a7d6e..16f4c169 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.h +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.h @@ -50,9 +50,11 @@ private: int32_t OnRemoteObtainDistributedTableName(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteInitNotifier(MessageParcel&data, MessageParcel& reply); + int32_t OnGetSchema(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply); + int32_t OnRemoteInitNotifier(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteSetDistributedTables(MessageParcel& data, MessageParcel& reply); int32_t OnRemoteDoSync(MessageParcel& data, MessageParcel& reply); @@ -79,7 +81,8 @@ private: [RDB_SERVICE_CMD_UNSUBSCRIBE] = &RdbServiceStub::OnRemoteDoUnSubscribe, [RDB_SERVICE_CMD_REMOTE_QUERY] = &RdbServiceStub::OnRemoteDoRemoteQuery, [RDB_SERVICE_CREATE_RDB_TABLE] = &RdbServiceStub::OnRemoteDoCreateTable, - [RDB_SERVICE_DESTROY_RDB_TABLE] = &RdbServiceStub::OnRemoteDoDestroyTable + [RDB_SERVICE_DESTROY_RDB_TABLE] = &RdbServiceStub::OnRemoteDoDestroyTable, + [RDB_SERVICE_CMD_GET_SCHEMA] = &RdbServiceStub::OnGetSchema }; }; } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp new file mode 100644 index 00000000..5794d6d8 --- /dev/null +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -0,0 +1,113 @@ +/* +* Copyright (c) 2022 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 "account/account_delegate.h" +#include "metadata/meta_data_manager.h" +#include "metadata/store_meta_data.h" +#include "cloud/cloud_server.h" +#include "gtest/gtest.h" +#include "cloud/cloud_event.h" +#include "eventcenter/event_center.h" +#include "communicator/device_manager_adapter.h" +using namespace testing::ext; +using namespace OHOS::DistributedData; +using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; +class CloudServerMock : public CloudServer { +public: + CloudInfo GetServerInfo(int32_t userId) override; + SchemaMeta GetAppSchema(int32_t userId, const std::string &bundleName) override; + static constexpr uint64_t REMAINSPACE = 1000; + static constexpr uint64_t TATALSPACE = 2000; +}; + +class CloudDataTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + auto cloudServerMock = new CloudServerMock(); + ASSERT_TRUE(CloudServer::RegisterCloudInstance(cloudServerMock)); + } + static void TearDownTestCase(void) + { + } + void SetUp() + { + } + void TearDown() + { + } +}; + + +CloudInfo CloudServerMock::GetServerInfo(int32_t userId) +{ + CloudInfo cloudInfo; + cloudInfo.user = userId; + cloudInfo.id = "test_cloud_id"; + cloudInfo.remainSpace = REMAINSPACE; + cloudInfo.totalSpace = TATALSPACE; + cloudInfo.enableCloud = true; + + CloudInfo::AppInfo appInfo; + appInfo.bundleName = "test_cloud_bundleName"; + appInfo.appId = "test_cloud_appid"; + appInfo.version = 1; + appInfo.cloudSwitch = true; + + cloudInfo.apps.emplace_back(std::move(appInfo)); + return cloudInfo; +} + +SchemaMeta CloudServerMock::GetAppSchema(int32_t userId, const std::string &bundleName) +{ + SchemaMeta::Field field1; + field1.colName = "test_cloud_field_name1"; + field1.alias = "test_cloud_field_alias1"; + SchemaMeta::Field field2; + field2.colName = "test_cloud_field_name2"; + field2.alias = "test_cloud_field_alias2"; + + SchemaMeta::Table table; + table.name = "test_cloud_table_name"; + table.alias = "test_cloud_table_alias"; + table.fields.emplace_back(field1); + table.fields.emplace_back(field2); + + SchemaMeta::Database database; + database.name = "test_cloud_database_name"; + database.alias = "test_cloud_database_alias"; + database.tables.emplace_back(table); + + SchemaMeta schemaMeta; + schemaMeta.version = 1; + schemaMeta.databases.emplace_back(database); + + return schemaMeta; +} + +HWTEST_F(CloudDataTest, GlobalConfig, TestSize.Level0) +{ + CloudEvent::StoreInfo storeInfo{ 0, "test_cloud_bundleName", "test_cloud_database_name", 0, 1 }; + auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "test_service"); + EventCenter::GetInstance().PostEvent(move(event)); + StoreMetaData storeMetaData; + storeMetaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + storeMetaData.user = OHOS::DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(0); + storeMetaData.bundleName = "test_cloud_bundleName"; + storeMetaData.storeId = "test_cloud_database_name"; + storeMetaData.instanceId = 0; + ASSERT_TRUE(MetaDataManager::GetInstance().DelMeta(storeMetaData.GetKey(), true)); + ASSERT_TRUE(MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData, true)); +} + -- Gitee From 52d1ad07278e118ab633628fd611ff26bb8b79e1 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 9 May 2023 09:29:15 +0800 Subject: [PATCH 02/95] test:add ut Signed-off-by: htt1997 --- .../framework/cloud/cloud_db.cpp | 4 +- .../framework/include/cloud/cloud_db.h | 4 +- .../framework/include/store/general_store.h | 8 +- .../service/rdb/rdb_general_store.cpp | 4 +- .../service/rdb/rdb_general_store.h | 4 +- .../service/test/BUILD.gn | 30 +++++ .../service/test/cloud_data_test.cpp | 126 +++++++++++++----- .../service/test/mock/db_store_mock.cpp | 2 +- 8 files changed, 137 insertions(+), 45 deletions(-) diff --git a/services/distributeddataservice/framework/cloud/cloud_db.cpp b/services/distributeddataservice/framework/cloud/cloud_db.cpp index ea4d041d..d80a6d36 100644 --- a/services/distributeddataservice/framework/cloud/cloud_db.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_db.cpp @@ -45,12 +45,12 @@ std::shared_ptr CloudDB::Query(const std::string &table, const std::stri return nullptr; } -std::shared_ptr CloudDB::Query(const std::string &table, const GenQuery &query) +std::shared_ptr CloudDB::Query(const std::string &table, GenQuery &query) { return nullptr; } -int32_t CloudDB::Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait) +int32_t CloudDB::Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) { return E_NOT_SUPPORT; } diff --git a/services/distributeddataservice/framework/include/cloud/cloud_db.h b/services/distributeddataservice/framework/include/cloud/cloud_db.h index 9b67c9a4..6d963661 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_db.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_db.h @@ -26,8 +26,8 @@ public: int32_t BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) override; int32_t Delete(const std::string &table, const std::string &sql, Values &&args) override; std::shared_ptr Query(const std::string &table, const std::string &sql, Values &&args) override; - std::shared_ptr Query(const std::string &table, const GenQuery &query) override; - int32_t Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait) override; + std::shared_ptr Query(const std::string &table, GenQuery &query) override; + int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) override; int32_t Watch(int32_t origin, Watcher &watcher) override; int32_t Unwatch(int32_t origin, Watcher &watcher) override; diff --git a/services/distributeddataservice/framework/include/store/general_store.h b/services/distributeddataservice/framework/include/store/general_store.h index 3b88c4f4..3c12f288 100644 --- a/services/distributeddataservice/framework/include/store/general_store.h +++ b/services/distributeddataservice/framework/include/store/general_store.h @@ -29,8 +29,6 @@ public: virtual ~GeneralStore() = default; - virtual int32_t Close() = 0; - virtual int32_t Execute(const std::string &table, const std::string &sql) = 0; virtual int32_t BatchInsert(const std::string &table, VBuckets &&values) = 0; @@ -41,13 +39,15 @@ public: virtual std::shared_ptr Query(const std::string &table, const std::string &sql, Values &&args) = 0; - virtual std::shared_ptr Query(const std::string &table, const GenQuery &query) = 0; + virtual std::shared_ptr Query(const std::string &table, GenQuery &query) = 0; - virtual int32_t Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait) = 0; + virtual int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) = 0; virtual int32_t Watch(int32_t origin, Watcher &watcher) = 0; virtual int32_t Unwatch(int32_t origin, Watcher &watcher) = 0; + + virtual int32_t Close() = 0; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_STORE_H diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index ab5f74e1..decce6c9 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -69,7 +69,7 @@ std::shared_ptr RdbGeneralStore::Query(const std::string &table, const s return std::shared_ptr(); } -std::shared_ptr RdbGeneralStore::Query(const std::string &table, const GenQuery &query) +std::shared_ptr RdbGeneralStore::Query(const std::string &table, GenQuery &query) { return std::shared_ptr(); } @@ -84,7 +84,7 @@ int32_t RdbGeneralStore::Unwatch(int32_t origin, Watcher &watcher) return 0; } -int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait) +int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) { return 0; } diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h index 272f7fcd..7f10e2ce 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -41,10 +41,10 @@ public: int32_t BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) override; int32_t Delete(const std::string &table, const std::string &sql, Values &&args) override; std::shared_ptr Query(const std::string &table, const std::string &sql, Values &&args) override; - std::shared_ptr Query(const std::string &table, const GenQuery &query) override; + std::shared_ptr Query(const std::string &table, GenQuery &query) override; int32_t Watch(int32_t origin, Watcher &watcher) override; int32_t Unwatch(int32_t origin, Watcher &watcher) override; - int32_t Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait) override; + int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) override; private: NativeRdb::ValuesBucket Convert(DistributedData::VBucket &&bucket); diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 70ad6fdd..478ac4ad 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/ohos_var.gni") import("//build/test.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") module_output_path = "datamgr_service/distributeddatafwk" @@ -35,6 +36,34 @@ config("module_private_config") { ] } +ohos_unittest("CloudDataTest") { + module_out_path = module_output_path + sources = [ + "cloud_data_test.cpp", + "mock/db_change_data_mock.cpp", + "mock/db_store_mock.cpp", + ] + + configs = [ ":module_private_config" ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + ] + + deps = [ + "../../adapter:distributeddata_adapter", + "../../framework:distributeddatasvcfwk", + "../../service:distributeddatasvc", + "${kv_store_distributeddb_path}:distributeddb", + "//third_party/googletest:gtest_main", + ] +} + ohos_unittest("ConfigFactoryTest") { module_out_path = module_output_path sources = [ "config_factory_test.cpp" ] @@ -146,6 +175,7 @@ group("unittest") { deps = [] deps += [ + ":CloudDataTest", ":ConfigFactoryTest", ":CryptoManagerTest", ":DeviceMatrixTest", diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index 5794d6d8..82b84b73 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -12,44 +12,38 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#define LOG_TAG "CloudDataTest" #include "account/account_delegate.h" #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" +#include "feature/feature_system.h" #include "cloud/cloud_server.h" #include "gtest/gtest.h" #include "cloud/cloud_event.h" #include "eventcenter/event_center.h" #include "communicator/device_manager_adapter.h" +#include "metadata/store_meta_data_local.h" +#include "ipc_skeleton.h" +#include "log_print.h" +#include "mock/db_store_mock.h" +#include "device_matrix.h" using namespace testing::ext; using namespace OHOS::DistributedData; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; + +static constexpr const char *TEST_CLOUD_BUNDLE = "test_cloud_bundleName"; +static constexpr const char *TEST_CLOUD_APPID = "test_cloud_appid"; +static constexpr const char *TEST_CLOUD_STORE = "test_cloud_database_name"; + class CloudServerMock : public CloudServer { public: CloudInfo GetServerInfo(int32_t userId) override; SchemaMeta GetAppSchema(int32_t userId, const std::string &bundleName) override; + virtual ~CloudServerMock() = default; static constexpr uint64_t REMAINSPACE = 1000; static constexpr uint64_t TATALSPACE = 2000; }; -class CloudDataTest : public testing::Test { -public: - static void SetUpTestCase(void) - { - auto cloudServerMock = new CloudServerMock(); - ASSERT_TRUE(CloudServer::RegisterCloudInstance(cloudServerMock)); - } - static void TearDownTestCase(void) - { - } - void SetUp() - { - } - void TearDown() - { - } -}; - - CloudInfo CloudServerMock::GetServerInfo(int32_t userId) { CloudInfo cloudInfo; @@ -60,8 +54,8 @@ CloudInfo CloudServerMock::GetServerInfo(int32_t userId) cloudInfo.enableCloud = true; CloudInfo::AppInfo appInfo; - appInfo.bundleName = "test_cloud_bundleName"; - appInfo.appId = "test_cloud_appid"; + appInfo.bundleName = TEST_CLOUD_BUNDLE; + appInfo.appId = TEST_CLOUD_APPID; appInfo.version = 1; appInfo.cloudSwitch = true; @@ -76,7 +70,7 @@ SchemaMeta CloudServerMock::GetAppSchema(int32_t userId, const std::string &bund field1.alias = "test_cloud_field_alias1"; SchemaMeta::Field field2; field2.colName = "test_cloud_field_name2"; - field2.alias = "test_cloud_field_alias2"; + field2.alias = "test_cloud_field_alias2"; SchemaMeta::Table table; table.name = "test_cloud_table_name"; @@ -85,7 +79,7 @@ SchemaMeta CloudServerMock::GetAppSchema(int32_t userId, const std::string &bund table.fields.emplace_back(field2); SchemaMeta::Database database; - database.name = "test_cloud_database_name"; + database.name = TEST_CLOUD_STORE; database.alias = "test_cloud_database_alias"; database.tables.emplace_back(table); @@ -96,18 +90,86 @@ SchemaMeta CloudServerMock::GetAppSchema(int32_t userId, const std::string &bund return schemaMeta; } -HWTEST_F(CloudDataTest, GlobalConfig, TestSize.Level0) +class CloudDataTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + +protected: + static constexpr const char *TEST_DISTRIBUTEDDATA_BUNDLE = "test_distributeddata"; + static constexpr const char *TEST_DISTRIBUTEDDATA_STORE = "test_service_meta"; + static constexpr const char *TEST_DISTRIBUTEDDATA_USER = "-1"; + + void InitMetaData(); + static std::shared_ptr dbStoreMock_; + StoreMetaData metaData_; +}; + +std::shared_ptr CloudDataTest::dbStoreMock_ = std::make_shared(); + +void CloudDataTest::InitMetaData() { - CloudEvent::StoreInfo storeInfo{ 0, "test_cloud_bundleName", "test_cloud_database_name", 0, 1 }; - auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "test_service"); - EventCenter::GetInstance().PostEvent(move(event)); + metaData_.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + metaData_.appId = TEST_DISTRIBUTEDDATA_BUNDLE; + metaData_.bundleName = TEST_DISTRIBUTEDDATA_BUNDLE; + metaData_.user = TEST_DISTRIBUTEDDATA_USER; + metaData_.area = OHOS::DistributedKv::EL1; + metaData_.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + metaData_.instanceId = 0; + metaData_.isAutoSync = true; + metaData_.storeType = 1; + metaData_.storeId = TEST_DISTRIBUTEDDATA_STORE; + PolicyValue value; + value.type = OHOS::DistributedKv::PolicyType::IMMEDIATE_SYNC_ON_ONLINE; +} + +void CloudDataTest::SetUpTestCase(void) +{ + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, [](const auto &, auto) { + DeviceMatrix::GetInstance().OnChanged(DeviceMatrix::META_STORE_MASK); + }); + + auto cloudServerMock = new CloudServerMock(); + ASSERT_TRUE(CloudServer::RegisterCloudInstance(cloudServerMock)); + FeatureSystem::GetInstance().GetCreator("cloud")(); +} + +void CloudDataTest::TearDownTestCase() {} + +void CloudDataTest::SetUp() +{ + InitMetaData(); + MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_); + StoreMetaData storeMetaData; storeMetaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; - storeMetaData.user = OHOS::DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(0); - storeMetaData.bundleName = "test_cloud_bundleName"; - storeMetaData.storeId = "test_cloud_database_name"; + storeMetaData.user = -1; + storeMetaData.bundleName = TEST_CLOUD_BUNDLE; + storeMetaData.storeId = TEST_CLOUD_STORE; storeMetaData.instanceId = 0; - ASSERT_TRUE(MetaDataManager::GetInstance().DelMeta(storeMetaData.GetKey(), true)); - ASSERT_TRUE(MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData, true)); + storeMetaData.isAutoSync = true; + storeMetaData.storeType = 1; + storeMetaData.area = OHOS::DistributedKv::EL1; + storeMetaData.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + MetaDataManager::GetInstance().SaveMeta(storeMetaData.GetKey(), storeMetaData); } +void CloudDataTest::TearDown() {} + +HWTEST_F(CloudDataTest, GlobalConfig, TestSize.Level0) +{ + ZLOGI("CloudDataTest start"); + std::shared_ptr cloudServerMock = std::make_shared(); + auto cloudInfo = cloudServerMock->GetServerInfo(-1); + ASSERT_TRUE(MetaDataManager::GetInstance().DelMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), true)); + StoreMetaData storeMetaData; + ASSERT_FALSE( + MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), storeMetaData, true)); + CloudEvent::StoreInfo storeInfo{ 0, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, 0, 1 }; + auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "test_service"); + EventCenter::GetInstance().PostEvent(move(event)); + ASSERT_TRUE( + MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), storeMetaData, true)); +} diff --git a/services/distributeddataservice/service/test/mock/db_store_mock.cpp b/services/distributeddataservice/service/test/mock/db_store_mock.cpp index 87639965..edc1cb3b 100644 --- a/services/distributeddataservice/service/test/mock/db_store_mock.cpp +++ b/services/distributeddataservice/service/test/mock/db_store_mock.cpp @@ -273,7 +273,7 @@ DBStatus DBStoreMock::GetEntries(ConcurrentMap &store, const Key &ke DBStatus DBStoreMock::PutBatch(ConcurrentMap &store, const std::vector &entries) { for (auto &entry : entries) { - entries_.InsertOrAssign(entry.key, entry.value); + store.InsertOrAssign(entry.key, entry.value); } DBChangeDataMock changeData({}, entries, {}); observers_.ForEachCopies([&changeData](const auto &observer, auto &keys) mutable { -- Gitee From 966aa02b3b57bce56cf6b8cb00460a861f5c0d26 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 9 May 2023 14:30:01 +0800 Subject: [PATCH 03/95] fix:add bind Signed-off-by: htt1997 --- datamgr_service.gni | 1 + .../distributeddataservice/service/BUILD.gn | 2 +- .../service/rdb/rdb_general_store.cpp | 19 +++------------ .../service/rdb/rdb_general_store.h | 23 +++++++++++-------- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/datamgr_service.gni b/datamgr_service.gni index 6ec402a1..c3fa0d49 100644 --- a/datamgr_service.gni +++ b/datamgr_service.gni @@ -26,6 +26,7 @@ datashare_path = "//foundation/distributeddatamgr/data_share" ipc_core_path = "//foundation/communication/ipc/interfaces/innerkits/ipc_core" device_manager_path = "//foundation/distributedhardware/device_manager" + declare_args() { datamgr_service_power = true if (!defined(global_parts_info.power_manager_native_powermgr_client) || diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 62317e78..d60d41f0 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -55,7 +55,7 @@ ohos_shared_library("distributeddatasvc") { include_dirs = [ "../../../../data_object/frameworks/innerkitsimpl/include", "../../../../relational_store/interfaces/inner_api/cloud_data/include", - "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/rdb/include", + "../../../../relational_store/interfaces/inner_api/rdb/include", "${kv_store_distributeddb_path}", ] sources = [ diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index decce6c9..53cfb7d5 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -89,22 +89,9 @@ int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &qu return 0; } -NativeRdb::ValuesBucket RdbGeneralStore::Convert(VBucket &&bucket) +int32_t RdbGeneralStore::Bind(std::shared_ptr cloudDb) { - ValuesBucket rdbVBucket; - return rdbVBucket; -} - -NativeRdb::ValueObject RdbGeneralStore::Convert(Value &&value) -{ - ValueObject rdbValue; - return rdbValue; -} - -DistributedData::Value RdbGeneralStore::Convert(ValueObject &&rdbValue) -{ - DistributedData::Value value; - return value; + cloudDb_ = std::move(cloudDb); + return 0; } - } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h index 7f10e2ce..8970e5ca 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_RDB_GENERAL_STORE_H -#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_RDB_GENERAL_STORE_H +#ifndef OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_STORE_H +#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_STORE_H #include #include "relational_store_delegate.h" #include "relational_store_manager.h" @@ -29,30 +29,33 @@ public: using GenQuery = DistributedData::GenQuery; using VBucket = DistributedData::VBucket; using VBuckets = DistributedData::VBuckets; + using Value = DistributedData::Value; using Values = DistributedData::Values; using StoreMetaData = DistributedData::StoreMetaData; + using CloudDB = DistributedData::CloudDB; using RdbStore = OHOS::NativeRdb::RdbStore; - using RdbDelegate = DistributedDB::RelationalStoreDelegate; - using RdbManager = DistributedDB::RelationalStoreManager; + using RdbDelegate = DistributedDB::RelationalStoreDelegate; + using RdbManager = DistributedDB::RelationalStoreManager; + RdbGeneralStore(const StoreMetaData &metaData); - int32_t Close() override; + int32_t Bind(std::shared_ptr cloudDb) override; int32_t Execute(const std::string &table, const std::string &sql) override; int32_t BatchInsert(const std::string &table, VBuckets &&values) override; int32_t BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) override; int32_t Delete(const std::string &table, const std::string &sql, Values &&args) override; std::shared_ptr Query(const std::string &table, const std::string &sql, Values &&args) override; std::shared_ptr Query(const std::string &table, GenQuery &query) override; + int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) override; int32_t Watch(int32_t origin, Watcher &watcher) override; int32_t Unwatch(int32_t origin, Watcher &watcher) override; - int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) override; + int32_t Close() override; private: - NativeRdb::ValuesBucket Convert(DistributedData::VBucket &&bucket); - NativeRdb::ValueObject Convert(DistributedData::Value &&value); - DistributedData::Value Convert(NativeRdb::ValueObject &&rdbValue); + static constexpr uint32_t ITERATE_TIMES = 10000; RdbManager manager_; std::shared_ptr store_; + std::shared_ptr cloudDb_; }; } // namespace OHOS::DistributedRdb -#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_RDB_GENERAL_STORE_H +#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_STORE_H -- Gitee From 5593ba7eac5bec8424018e811d67ec9d549bf3a5 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 9 May 2023 16:02:19 +0800 Subject: [PATCH 04/95] fix:codecheck Signed-off-by: htt1997 --- services/distributeddataservice/service/BUILD.gn | 2 +- .../distributeddataservice/service/test/cloud_data_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index d60d41f0..0e136c63 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -159,8 +159,8 @@ ohos_shared_library("distributeddatasvc") { "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", - "relational_store:cloud_data", "kv_store:distributeddata_inner", + "relational_store:cloud_data", "relational_store:native_rdb", "relational_store:rdb_bms_adapter", "relational_store:rdb_data_share_adapter", diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index 82b84b73..b40cbe17 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -158,7 +158,7 @@ void CloudDataTest::SetUp() void CloudDataTest::TearDown() {} -HWTEST_F(CloudDataTest, GlobalConfig, TestSize.Level0) +HWTEST_F(CloudDataTest, GetSchema, TestSize.Level0) { ZLOGI("CloudDataTest start"); std::shared_ptr cloudServerMock = std::make_shared(); -- Gitee From eb4b5f2cf4bb1bb21b4f229c1939eb84c04171fe Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 9 May 2023 16:15:24 +0800 Subject: [PATCH 05/95] fix:storeMeta load from sync table Signed-off-by: htt1997 --- .../distributeddataservice/service/cloud/cloud_service_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index fafc5c9e..7c6a67c1 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -87,7 +87,7 @@ CloudServiceImpl::CloudServiceImpl() storeMetaData.bundleName = rdbEvent.GetStoreInfo().bundleName; storeMetaData.storeId = rdbEvent.GetStoreInfo().storeName; storeMetaData.instanceId = rdbEvent.GetStoreInfo().instanceId; - if (!MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData, true)) { + if (!MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData)) { return; } AutoCache::GetInstance().CreateTable(storeMetaData, schemaMeta); -- Gitee From cb577fc375c003d2f55f51e4347fb8752c0928dc Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 9 May 2023 16:17:05 +0800 Subject: [PATCH 06/95] fix:format check Signed-off-by: htt1997 --- services/distributeddataservice/service/test/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 478ac4ad..0c5173a0 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -56,10 +56,10 @@ ohos_unittest("CloudDataTest") { ] deps = [ + "${kv_store_distributeddb_path}:distributeddb", "../../adapter:distributeddata_adapter", "../../framework:distributeddatasvcfwk", "../../service:distributeddatasvc", - "${kv_store_distributeddb_path}:distributeddb", "//third_party/googletest:gtest_main", ] } -- Gitee From df20a1638e2729bb5b55e78e43e0983319c68f8a Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 9 May 2023 22:33:52 +0800 Subject: [PATCH 07/95] fix:change Signed-off-by: htt1997 --- .../framework/cloud/cloud_info.cpp | 12 +++- .../framework/include/cloud/cloud_db.h | 2 + .../framework/include/cloud/cloud_event.h | 1 - .../framework/include/cloud/cloud_info.h | 4 +- .../framework/include/store/auto_cache.h | 4 +- .../framework/include/store/general_store.h | 3 + .../framework/store/auto_cache.cpp | 26 ++----- .../distributeddataservice/service/BUILD.gn | 2 +- .../service/cloud/cloud_service_impl.cpp | 70 +++++++++++++------ .../service/cloud/cloud_service_impl.h | 4 ++ .../service/rdb/rdb_general_store.cpp | 6 ++ .../service/rdb/rdb_general_store.h | 4 +- .../service/rdb/rdb_service_impl.cpp | 2 +- .../service/test/cloud_data_test.cpp | 23 +++--- 14 files changed, 100 insertions(+), 63 deletions(-) diff --git a/services/distributeddataservice/framework/cloud/cloud_info.cpp b/services/distributeddataservice/framework/cloud/cloud_info.cpp index d3560bb8..2f44ff85 100644 --- a/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -68,15 +68,21 @@ std::map CloudInfo::GetSchemaKey() const { std::map keys; for (const auto &app : apps) { - const auto key = GetKey(SCHEMA_PREFIX, { std::to_string(user), id, app.bundleName }); + const auto key = GetKey( + SCHEMA_PREFIX, { std::to_string(user), app.bundleName, std::to_string(app.instanceId) }); keys.insert_or_assign(app.bundleName, key); } return keys; } -std::string CloudInfo::GetSchemaKey(std::string bundleName) const +std::string CloudInfo::GetSchemaKey(const std::string &bundleName, const int32_t instanceId) const { - return GetKey(SCHEMA_PREFIX, { std::to_string(user), id, bundleName }); + return GetKey(SCHEMA_PREFIX, { std::to_string(user), bundleName, std::to_string(instanceId) }); +} + +std::string CloudInfo::GetSchemaKey(const StoreMetaData &meta) +{ + return GetKey(SCHEMA_PREFIX, { meta.user, meta.bundleName, std::to_string(meta.instanceId) }); } bool CloudInfo::IsValid() const diff --git a/services/distributeddataservice/framework/include/cloud/cloud_db.h b/services/distributeddataservice/framework/include/cloud/cloud_db.h index 57d15f5c..c7599bda 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_db.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_db.h @@ -28,6 +28,8 @@ public: using Watcher = GeneralWatcher; using Async = std::function>)>; using Devices = std::vector; + virtual ~CloudDB() = default; + virtual int32_t Execute(const std::string &table, const std::string &sql, const VBucket &extend); virtual int32_t BatchInsert(const std::string &table, VBuckets &&values, VBuckets &extends); diff --git a/services/distributeddataservice/framework/include/cloud/cloud_event.h b/services/distributeddataservice/framework/include/cloud/cloud_event.h index d01ab0f2..5a5efc87 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -34,7 +34,6 @@ public: std::string bundleName; std::string storeName; int32_t instanceId = 0; - int32_t schemaVersion = -1; }; CloudEvent(int32_t evtId, StoreInfo storeInfo, const std::string &featureName = "relational_store"); diff --git a/services/distributeddataservice/framework/include/cloud/cloud_info.h b/services/distributeddataservice/framework/include/cloud/cloud_info.h index d16c8cfc..dc732027 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_info.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -15,6 +15,7 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_INFO_H #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_INFO_H +#include "metadata/store_meta_data.h" #include "serializable/serializable.h" namespace OHOS::DistributedData { class API_EXPORT CloudInfo final : public Serializable { @@ -38,7 +39,8 @@ public: std::string GetKey() const; std::map GetSchemaKey() const; - std::string GetSchemaKey(std::string bundleName) const; + std::string GetSchemaKey(const std::string &bundleName, const int32_t instanceId = 0) const; + static std::string GetSchemaKey(const StoreMetaData &meta); bool IsValid() const; bool IsExist(const std::string &bundleName) const; static std::string GetPrefix(const std::initializer_list &field); diff --git a/services/distributeddataservice/framework/include/store/auto_cache.h b/services/distributeddataservice/framework/include/store/auto_cache.h index 3a957e2a..8884a789 100644 --- a/services/distributeddataservice/framework/include/store/auto_cache.h +++ b/services/distributeddataservice/framework/include/store/auto_cache.h @@ -44,9 +44,7 @@ public: API_EXPORT void Bind(std::shared_ptr executor); - API_EXPORT Store GetStore(const StoreMetaData &meta, const Watchers &watchers); - - API_EXPORT int32_t CreateTable(const StoreMetaData &storeMetaData, const SchemaMeta &schemaMeta); + API_EXPORT Store GetStore(const StoreMetaData &meta, const Watchers &watchers, bool setWatchers = true); API_EXPORT void CloseStore(uint32_t tokenId, const std::string &storeId); diff --git a/services/distributeddataservice/framework/include/store/general_store.h b/services/distributeddataservice/framework/include/store/general_store.h index 56cdcf87..097f8d9a 100644 --- a/services/distributeddataservice/framework/include/store/general_store.h +++ b/services/distributeddataservice/framework/include/store/general_store.h @@ -23,6 +23,7 @@ #include "store/general_watcher.h" namespace OHOS::DistributedData { class CloudDB; +class SchemaMeta; class GeneralStore { public: using Watcher = GeneralWatcher; @@ -33,6 +34,8 @@ public: virtual int32_t Bind(std::shared_ptr cloudDb) = 0; + virtual int32_t SetSchema(const SchemaMeta &schemaMeta) = 0; + virtual int32_t Execute(const std::string &table, const std::string &sql) = 0; virtual int32_t BatchInsert(const std::string &table, VBuckets &&values) = 0; diff --git a/services/distributeddataservice/framework/store/auto_cache.cpp b/services/distributeddataservice/framework/store/auto_cache.cpp index 11dedf24..bb22477f 100644 --- a/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/services/distributeddataservice/framework/store/auto_cache.cpp @@ -52,7 +52,7 @@ AutoCache::~AutoCache() } } -AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers &watchers) +AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers &watchers, bool setWatchers) { Store store; if (meta.storeType >= MAX_CREATOR_NUM || !creators_[meta.storeType]) { @@ -60,10 +60,12 @@ AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers & } stores_.Compute(meta.tokenId, - [this, &meta, &watchers, &store](auto &, std::map &stores) -> bool { + [this, &meta, &watchers, &store, setWatchers](auto &, std::map &stores) -> bool { auto it = stores.find(meta.storeId); if (it != stores.end()) { - it->second.SetObservers(watchers); + if (setWatchers) { + it->second.SetObservers(watchers); + } store = it->second; return !stores.empty(); } @@ -79,24 +81,6 @@ AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers & return store; } -int32_t AutoCache::CreateTable(const StoreMetaData &storeMetaData, const SchemaMeta &schemaMeta) -{ - stores_.Compute(storeMetaData.tokenId, - [this, &storeMetaData, &schemaMeta](auto &, std::map &stores) -> bool { - auto it = stores.find(storeMetaData.storeId); - if (it != stores.end()) { - //it->second->CreateTable(schemaMeta) - return true; - } - auto *dbStore = creators_[storeMetaData.storeType](storeMetaData); - if (dbStore != nullptr) { - //dbStore->CreateTable(schemaMeta) - } - return false; - }); - return 0; -} - void AutoCache::CloseStore(uint32_t tokenId, const std::string &storeId) { stores_.ComputeIfPresent(tokenId, [&storeId](auto &key, std::map &delegates) { diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 0e136c63..59998168 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -142,6 +142,7 @@ ohos_shared_library("distributeddatasvc") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", + "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/cloud_data:cloud_data_inner", ] external_deps = [ @@ -160,7 +161,6 @@ ohos_shared_library("distributeddatasvc") { "huks:libhukssdk", "ipc:ipc_core", "kv_store:distributeddata_inner", - "relational_store:cloud_data", "relational_store:native_rdb", "relational_store:rdb_bms_adapter", "relational_store:rdb_data_share_adapter", diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 7c6a67c1..e1474659 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -64,40 +64,30 @@ CloudServiceImpl::CloudServiceImpl() EventCenter::GetInstance().Subscribe(CloudEvent::GET_SCHEMA, [this](const Event &event) { auto &rdbEvent = static_cast(event); - CloudInfo cloudInfo; - cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); - if (GetServerInfo(cloudInfo) != SUCCESS) { - ZLOGE("failed, user:%{public}d", cloudInfo.user); - return; - } + auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); + auto schemaMeta = GetSchemaMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().instanceId); + auto storeMeta = GetStoreMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, + rdbEvent.GetStoreInfo().instanceId); + + AutoCache::Watchers watchers; + auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers, false); + store->SetSchema(schemaMeta); auto instance = CloudServer::GetInstance(); if (instance == nullptr) { + ZLOGE("instance is nullptr"); return; } - SchemaMeta schemaMeta; - std::string schemaKey = cloudInfo.GetSchemaKey(rdbEvent.GetStoreInfo().bundleName); - if (!MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true) || - schemaMeta.version != rdbEvent.GetStoreInfo().schemaVersion) { - schemaMeta = instance->GetAppSchema(cloudInfo.user, rdbEvent.GetStoreInfo().bundleName); - MetaDataManager::GetInstance().SaveMeta(schemaKey, schemaMeta, true); - } - StoreMetaData storeMetaData; - storeMetaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; - storeMetaData.user = std::to_string(cloudInfo.user); - storeMetaData.bundleName = rdbEvent.GetStoreInfo().bundleName; - storeMetaData.storeId = rdbEvent.GetStoreInfo().storeName; - storeMetaData.instanceId = rdbEvent.GetStoreInfo().instanceId; - if (!MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData)) { - return; - } - AutoCache::GetInstance().CreateTable(storeMetaData, schemaMeta); for (auto &database : schemaMeta.databases) { if (database.name != rdbEvent.GetStoreInfo().storeName /* || don't need sync*/) { continue; } auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, database); + if (cloudDB != nullptr) { + store->Bind(cloudDB); + } //do sync } + return; }); } @@ -249,4 +239,38 @@ int32_t CloudServiceImpl::GetAppSchema(int32_t user, const std::string &bundleNa schemaMeta = instance->GetAppSchema(user, bundleName); return SUCCESS; } + +SchemaMeta CloudServiceImpl::GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId) +{ + SchemaMeta schemaMeta; + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + ZLOGE("instance is nullptr"); + return schemaMeta; + } + auto cloudInfo = instance->GetServerInfo(userId); + if (!cloudInfo.IsValid()) { + ZLOGE("cloudInfo is invalid"); + return schemaMeta; + } + std::string schemaKey = cloudInfo.GetSchemaKey(bundleName, instanceId); + if (!MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true)) { + schemaMeta = instance->GetAppSchema(cloudInfo.user, bundleName); + MetaDataManager::GetInstance().SaveMeta(schemaKey, schemaMeta, true); + } + return schemaMeta; +} + +StoreMetaData CloudServiceImpl::GetStoreMata(int32_t userId, const std::string &bundleName, + const std::string &storeName, int32_t instanceId) +{ + StoreMetaData storeMetaData; + storeMetaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + storeMetaData.user = std::to_string(userId); + storeMetaData.bundleName = bundleName; + storeMetaData.storeId = storeName; + storeMetaData.instanceId = instanceId; + MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData); + return storeMetaData; +} } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 9fa2ec68..d4086f5f 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -24,6 +24,7 @@ namespace OHOS::CloudData { class CloudServiceImpl : public CloudServiceStub { public: + using StoreMetaData = DistributedData::StoreMetaData; CloudServiceImpl(); ~CloudServiceImpl() = default; int32_t EnableCloud(const std::string &id, const std::map &switches) override; @@ -47,6 +48,9 @@ private: void UpdateCloudInfo(CloudInfo &cloudInfo); void AddSchema(CloudInfo &cloudInfo); + SchemaMeta GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId); + StoreMetaData GetStoreMata(int32_t userId, const std::string &bundleName, const std::string &storeName, + int32_t instanceId); int32_t GetCloudInfo(CloudInfo &cloudInfo); int32_t GetServerInfo(CloudInfo &cloudInfo); int32_t GetAppSchema(int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta); diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index 53cfb7d5..a6745e3d 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -94,4 +94,10 @@ int32_t RdbGeneralStore::Bind(std::shared_ptr cloudDb) cloudDb_ = std::move(cloudDb); return 0; } + +int32_t RdbGeneralStore::SetSchema(const SchemaMeta &schemaMeta) +{ + //delegate_->SetSchema(schemaMeta); + return GeneralError::E_OK; +} } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h index 8970e5ca..c0051fab 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -23,7 +23,7 @@ #include "store/general_store.h" #include "metadata/store_meta_data.h" namespace OHOS::DistributedRdb { -class API_EXPORT RdbGeneralStore : public DistributedData::GeneralStore { +class RdbGeneralStore : public DistributedData::GeneralStore { public: using Cursor = DistributedData::Cursor; using GenQuery = DistributedData::GenQuery; @@ -32,6 +32,7 @@ public: using Value = DistributedData::Value; using Values = DistributedData::Values; using StoreMetaData = DistributedData::StoreMetaData; + using SchemaMeta = DistributedData::SchemaMeta; using CloudDB = DistributedData::CloudDB; using RdbStore = OHOS::NativeRdb::RdbStore; using RdbDelegate = DistributedDB::RelationalStoreDelegate; @@ -39,6 +40,7 @@ public: RdbGeneralStore(const StoreMetaData &metaData); int32_t Bind(std::shared_ptr cloudDb) override; + int32_t SetSchema(const SchemaMeta &schemaMeta) override; int32_t Execute(const std::string &table, const std::string &sql) override; int32_t BatchInsert(const std::string &table, VBuckets &&values) override; int32_t BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) override; diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index c2dddd0f..1b38797b 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -500,7 +500,7 @@ int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) return RDB_ERROR; } CloudEvent::StoreInfo storeInfo{ IPCSkeleton::GetCallingTokenID(), param.bundleName_, param.storeName_, - storeMeta.instanceId, param.schemaVersion }; + storeMeta.instanceId }; auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "relational_store"); EventCenter::GetInstance().PostEvent(move(event)); return RDB_OK; diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index b40cbe17..988cee18 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -14,19 +14,19 @@ */ #define LOG_TAG "CloudDataTest" #include "account/account_delegate.h" -#include "metadata/meta_data_manager.h" -#include "metadata/store_meta_data.h" -#include "feature/feature_system.h" -#include "cloud/cloud_server.h" -#include "gtest/gtest.h" #include "cloud/cloud_event.h" -#include "eventcenter/event_center.h" +#include "cloud/cloud_server.h" #include "communicator/device_manager_adapter.h" -#include "metadata/store_meta_data_local.h" +#include "device_matrix.h" +#include "eventcenter/event_center.h" +#include "feature/feature_system.h" +#include "gtest/gtest.h" #include "ipc_skeleton.h" #include "log_print.h" +#include "metadata/meta_data_manager.h" +#include "metadata/store_meta_data.h" +#include "metadata/store_meta_data_local.h" #include "mock/db_store_mock.h" -#include "device_matrix.h" using namespace testing::ext; using namespace OHOS::DistributedData; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; @@ -158,6 +158,13 @@ void CloudDataTest::SetUp() void CloudDataTest::TearDown() {} +/** +* @tc.name: GetSchema +* @tc.desc: GetSchema from cloud. +* @tc.type: FUNC +* @tc.require: +* @tc.author: ht +*/ HWTEST_F(CloudDataTest, GetSchema, TestSize.Level0) { ZLOGI("CloudDataTest start"); -- Gitee From c7c7a00c7fa3f2e6e99bde302a3afc808906ce15 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 10 May 2023 10:01:55 +0800 Subject: [PATCH 08/95] fix:code check Signed-off-by: htt1997 --- datamgr_service.gni | 2 ++ services/distributeddataservice/service/BUILD.gn | 2 +- .../distributeddataservice/service/rdb/rdb_general_store.cpp | 2 +- .../distributeddataservice/service/rdb/rdb_general_store.h | 2 +- .../distributeddataservice/service/rdb/rdb_service_impl.cpp | 4 ---- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/datamgr_service.gni b/datamgr_service.gni index c3fa0d49..15f8ddfc 100644 --- a/datamgr_service.gni +++ b/datamgr_service.gni @@ -15,6 +15,8 @@ distributedfilejs_path = "//foundation/distributeddatamgr/distributedfile" kv_store_path = "//foundation/distributeddatamgr/kv_store" +relational_store_path = "//foundation/distributeddatamgr/relational_store" + kv_store_common_path = "${kv_store_path}/frameworks/common" kv_store_distributeddb_path = "${kv_store_path}/frameworks/libs/distributeddb" diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 59998168..49e56b7d 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -142,7 +142,7 @@ ohos_shared_library("distributeddatasvc") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", - "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/cloud_data:cloud_data_inner", + "${relational_store_path}/interfaces/inner_api/cloud_data:cloud_data_inner", ] external_deps = [ diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index a6745e3d..15debe6e 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -97,7 +97,7 @@ int32_t RdbGeneralStore::Bind(std::shared_ptr cloudDb) int32_t RdbGeneralStore::SetSchema(const SchemaMeta &schemaMeta) { - //delegate_->SetSchema(schemaMeta); + //SetSchema; return GeneralError::E_OK; } } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h index c0051fab..af8ca7d9 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -38,7 +38,7 @@ public: using RdbDelegate = DistributedDB::RelationalStoreDelegate; using RdbManager = DistributedDB::RelationalStoreManager; - RdbGeneralStore(const StoreMetaData &metaData); + explicit RdbGeneralStore(const StoreMetaData &metaData); int32_t Bind(std::shared_ptr cloudDb) override; int32_t SetSchema(const SchemaMeta &schemaMeta) override; int32_t Execute(const std::string &table, const std::string &sql) override; diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 1b38797b..209ee447 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -479,10 +479,6 @@ int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) return RDB_ERROR; } - if (param.schemaVersion == -1) { - return RDB_OK; - } - auto storeMeta = GetStoreMetaData(param); StoreMetaData oldMeta; bool isCreated = MetaDataManager::GetInstance().LoadMeta(storeMeta.GetKey(), oldMeta, true); -- Gitee From 00db61da3384f7a23a43cd4cc6ff06786d8e6ef1 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 10 May 2023 10:29:03 +0800 Subject: [PATCH 09/95] fix:code check Signed-off-by: htt1997 --- .../distributeddataservice/service/rdb/rdb_general_store.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index 15debe6e..bb69635c 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -97,7 +97,7 @@ int32_t RdbGeneralStore::Bind(std::shared_ptr cloudDb) int32_t RdbGeneralStore::SetSchema(const SchemaMeta &schemaMeta) { - //SetSchema; + //SetSchema return GeneralError::E_OK; } } // namespace OHOS::DistributedRdb -- Gitee From 45d3e2a47b097706ed59b728a626b7192f206d8d Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 10 May 2023 11:27:41 +0800 Subject: [PATCH 10/95] fix:code check Signed-off-by: htt1997 --- .../service/cloud/cloud_service_impl.cpp | 5 +++-- .../service/rdb/rdb_service_impl.cpp | 4 ++-- .../service/test/cloud_data_test.cpp | 8 ++++---- .../service/test/mock/db_store_mock.cpp | 8 ++++---- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index e1474659..d45c8808 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -78,14 +78,14 @@ CloudServiceImpl::CloudServiceImpl() return; } for (auto &database : schemaMeta.databases) { - if (database.name != rdbEvent.GetStoreInfo().storeName /* || don't need sync*/) { + if (database.name != rdbEvent.GetStoreInfo().storeName /* || don't need sync */) { continue; } auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, database); if (cloudDB != nullptr) { store->Bind(cloudDB); } - //do sync + // do sync } return; }); @@ -166,6 +166,7 @@ int32_t CloudServiceImpl::Clean(const std::string &id, const std::mapGetUserByToken(token); std::string appId = CheckerManager::GetInstance().GetAppId(storeInfo); std::string identifier = RelationalStoreManager::GetRelationalStoreIdentifier( @@ -495,7 +495,7 @@ int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) if (!saved) { return RDB_ERROR; } - CloudEvent::StoreInfo storeInfo{ IPCSkeleton::GetCallingTokenID(), param.bundleName_, param.storeName_, + CloudEvent::StoreInfo storeInfo { IPCSkeleton::GetCallingTokenID(), param.bundleName_, param.storeName_, storeMeta.instanceId }; auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "relational_store"); EventCenter::GetInstance().PostEvent(move(event)); diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index 988cee18..ffce5e23 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -13,6 +13,9 @@ * limitations under the License. */ #define LOG_TAG "CloudDataTest" +#include "gtest/gtest.h" +#include "log_print.h" +#include "ipc_skeleton.h" #include "account/account_delegate.h" #include "cloud/cloud_event.h" #include "cloud/cloud_server.h" @@ -20,9 +23,6 @@ #include "device_matrix.h" #include "eventcenter/event_center.h" #include "feature/feature_system.h" -#include "gtest/gtest.h" -#include "ipc_skeleton.h" -#include "log_print.h" #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" #include "metadata/store_meta_data_local.h" @@ -174,7 +174,7 @@ HWTEST_F(CloudDataTest, GetSchema, TestSize.Level0) StoreMetaData storeMetaData; ASSERT_FALSE( MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), storeMetaData, true)); - CloudEvent::StoreInfo storeInfo{ 0, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, 0, 1 }; + CloudEvent::StoreInfo storeInfo { 0, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, 0, 1 }; auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "test_service"); EventCenter::GetInstance().PostEvent(move(event)); ASSERT_TRUE( diff --git a/services/distributeddataservice/service/test/mock/db_store_mock.cpp b/services/distributeddataservice/service/test/mock/db_store_mock.cpp index edc1cb3b..e4f0f3b2 100644 --- a/services/distributeddataservice/service/test/mock/db_store_mock.cpp +++ b/services/distributeddataservice/service/test/mock/db_store_mock.cpp @@ -263,7 +263,7 @@ DBStatus DBStoreMock::GetEntries(ConcurrentMap &store, const Key &ke store.ForEach([&entries, &keyPrefix](const Key &key, Value &value) { auto it = std::search(key.begin(), key.end(), keyPrefix.begin(), keyPrefix.end()); if (it == key.begin()) { - entries.push_back({ key, value }); + entries.push_back( { key, value } ); } return false; }); @@ -275,7 +275,7 @@ DBStatus DBStoreMock::PutBatch(ConcurrentMap &store, const std::vect for (auto &entry : entries) { store.InsertOrAssign(entry.key, entry.value); } - DBChangeDataMock changeData({}, entries, {}); + DBChangeDataMock changeData( {}, entries, {} ); observers_.ForEachCopies([&changeData](const auto &observer, auto &keys) mutable { if (observer) { observer->OnChange(changeData); @@ -287,13 +287,13 @@ DBStatus DBStoreMock::PutBatch(ConcurrentMap &store, const std::vect DBStatus DBStoreMock::DeleteBatch(ConcurrentMap &store, const std::vector &keys) { - DBChangeDataMock changeData({}, {}, {}); + DBChangeDataMock changeData( {}, {}, {} ); for (auto &key : keys) { auto it = store.Find(key); if (!it.first) { continue; } - changeData.AddEntry({key, std::move(it.second)}); + changeData.AddEntry( {key, std::move(it.second)} ); store.Erase(key); } -- Gitee From d3a8bbd4492df2837646adead545146b8e19463a Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 10 May 2023 14:47:51 +0800 Subject: [PATCH 11/95] fix:code check Signed-off-by: htt1997 --- .../framework/include/cloud/cloud_event.h | 2 +- .../service/rdb/rdb_general_store.cpp | 13 +------ .../service/test/cloud_data_test.cpp | 36 +++++++++---------- 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/services/distributeddataservice/framework/include/cloud/cloud_event.h b/services/distributeddataservice/framework/include/cloud/cloud_event.h index 5a5efc87..e6cff13a 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -29,7 +29,7 @@ public: CLOUD_BUTT }; - struct StoreInfo{ + struct StoreInfo { uint32_t tokenId = 0; std::string bundleName; std::string storeName; diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index bb69635c..e57cc2ea 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -24,17 +24,6 @@ namespace OHOS::DistributedRdb { using namespace DistributedData; using namespace OHOS::NativeRdb; -class RdbOpenCallbackImpl : public RdbOpenCallback { -public: - int OnCreate(RdbStore &rdbStore) override - { - return NativeRdb::E_OK; - } - int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override - { - return NativeRdb::E_OK; - } -}; RdbGeneralStore::RdbGeneralStore(const StoreMetaData &meta) : manager_(meta.appId, meta.user, meta.instanceId) { } @@ -97,7 +86,7 @@ int32_t RdbGeneralStore::Bind(std::shared_ptr cloudDb) int32_t RdbGeneralStore::SetSchema(const SchemaMeta &schemaMeta) { - //SetSchema + // SetSchema return GeneralError::E_OK; } } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index ffce5e23..6830b375 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -35,6 +35,23 @@ static constexpr const char *TEST_CLOUD_BUNDLE = "test_cloud_bundleName"; static constexpr const char *TEST_CLOUD_APPID = "test_cloud_appid"; static constexpr const char *TEST_CLOUD_STORE = "test_cloud_database_name"; +class CloudDataTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + +protected: + static constexpr const char *TEST_DISTRIBUTEDDATA_BUNDLE = "test_distributeddata"; + static constexpr const char *TEST_DISTRIBUTEDDATA_STORE = "test_service_meta"; + static constexpr const char *TEST_DISTRIBUTEDDATA_USER = "-1"; + + void InitMetaData(); + static std::shared_ptr dbStoreMock_; + StoreMetaData metaData_; +}; + class CloudServerMock : public CloudServer { public: CloudInfo GetServerInfo(int32_t userId) override; @@ -90,23 +107,6 @@ SchemaMeta CloudServerMock::GetAppSchema(int32_t userId, const std::string &bund return schemaMeta; } -class CloudDataTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); - -protected: - static constexpr const char *TEST_DISTRIBUTEDDATA_BUNDLE = "test_distributeddata"; - static constexpr const char *TEST_DISTRIBUTEDDATA_STORE = "test_service_meta"; - static constexpr const char *TEST_DISTRIBUTEDDATA_USER = "-1"; - - void InitMetaData(); - static std::shared_ptr dbStoreMock_; - StoreMetaData metaData_; -}; - std::shared_ptr CloudDataTest::dbStoreMock_ = std::make_shared(); void CloudDataTest::InitMetaData() @@ -174,7 +174,7 @@ HWTEST_F(CloudDataTest, GetSchema, TestSize.Level0) StoreMetaData storeMetaData; ASSERT_FALSE( MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), storeMetaData, true)); - CloudEvent::StoreInfo storeInfo { 0, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, 0, 1 }; + CloudEvent::StoreInfo storeInfo { 0, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, 0 }; auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "test_service"); EventCenter::GetInstance().PostEvent(move(event)); ASSERT_TRUE( -- Gitee From 6c7fd3069cf9ba9ae9afb182d6f2d8607bffe281 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 10 May 2023 16:18:50 +0800 Subject: [PATCH 12/95] fix:clouddata test Signed-off-by: htt1997 --- .../service/cloud/cloud_service_impl.cpp | 5 ++++- .../service/cloud/cloud_service_impl.h | 2 +- .../distributeddataservice/service/test/cloud_data_test.cpp | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index d45c8808..d75c0ac3 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -36,7 +36,7 @@ namespace OHOS::CloudData { using namespace DistributedData; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; __attribute__((used)) CloudServiceImpl::Factory CloudServiceImpl::factory_; -CloudServiceImpl::Factory::Factory() +CloudServiceImpl::Factory::Factory() noexcept { FeatureSystem::GetInstance().RegisterCreator(CloudServiceImpl::SERVICE_NAME, [this]() { if (product_ == nullptr) { @@ -71,6 +71,9 @@ CloudServiceImpl::CloudServiceImpl() AutoCache::Watchers watchers; auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers, false); + if (store == nullptr) { + ZLOGE("store is nullptr"); + } store->SetSchema(schemaMeta); auto instance = CloudServer::GetInstance(); if (instance == nullptr) { diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index d4086f5f..b71bbf4d 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -36,7 +36,7 @@ public: private: class Factory { public: - Factory(); + Factory() noexcept; ~Factory(); private: std::shared_ptr product_; diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index 6830b375..1fda4965 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ #define LOG_TAG "CloudDataTest" -#include "gtest/gtest.h" +#include #include "log_print.h" #include "ipc_skeleton.h" #include "account/account_delegate.h" @@ -134,6 +134,7 @@ void CloudDataTest::SetUpTestCase(void) auto cloudServerMock = new CloudServerMock(); ASSERT_TRUE(CloudServer::RegisterCloudInstance(cloudServerMock)); FeatureSystem::GetInstance().GetCreator("cloud")(); + FeatureSystem::GetInstance().GetCreator("relational_store")(); } void CloudDataTest::TearDownTestCase() {} -- Gitee From 52e2a894b5a6f8ea1b5567ccce464335b818cf10 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 10 May 2023 18:15:29 +0800 Subject: [PATCH 13/95] fix:code check Signed-off-by: htt1997 --- .../distributeddataservice/framework/store/auto_cache.cpp | 2 +- .../service/cloud/cloud_service_impl.cpp | 1 + services/distributeddataservice/service/test/BUILD.gn | 1 + .../service/test/cloud_data_test.cpp | 8 +++++++- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/framework/store/auto_cache.cpp b/services/distributeddataservice/framework/store/auto_cache.cpp index bb22477f..37aff132 100644 --- a/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/services/distributeddataservice/framework/store/auto_cache.cpp @@ -55,7 +55,7 @@ AutoCache::~AutoCache() AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers &watchers, bool setWatchers) { Store store; - if (meta.storeType >= MAX_CREATOR_NUM || !creators_[meta.storeType]) { + if (meta.storeType >= MAX_CREATOR_NUM || meta.storeType < 0 || !creators_[meta.storeType]) { return store; } diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index d75c0ac3..70efa85e 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -73,6 +73,7 @@ CloudServiceImpl::CloudServiceImpl() auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers, false); if (store == nullptr) { ZLOGE("store is nullptr"); + return; } store->SetSchema(schemaMeta); auto instance = CloudServer::GetInstance(); diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 0c5173a0..cb562215 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -43,6 +43,7 @@ ohos_unittest("CloudDataTest") { "mock/db_change_data_mock.cpp", "mock/db_store_mock.cpp", ] + include_dirs = [ "../../../../../relational_store/interfaces/inner_api/rdb/include" ] configs = [ ":module_private_config" ] diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index 1fda4965..f640631a 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -27,10 +27,14 @@ #include "metadata/store_meta_data.h" #include "metadata/store_meta_data_local.h" #include "mock/db_store_mock.h" +#include "rdb_types.h" using namespace testing::ext; using namespace OHOS::DistributedData; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; +namespace OHOS { +namespace DistributedDataTest { + static constexpr const char *TEST_CLOUD_BUNDLE = "test_cloud_bundleName"; static constexpr const char *TEST_CLOUD_APPID = "test_cloud_appid"; static constexpr const char *TEST_CLOUD_STORE = "test_cloud_database_name"; @@ -151,7 +155,7 @@ void CloudDataTest::SetUp() storeMetaData.storeId = TEST_CLOUD_STORE; storeMetaData.instanceId = 0; storeMetaData.isAutoSync = true; - storeMetaData.storeType = 1; + storeMetaData.storeType = DistributedRdb::RDB_DEVICE_COLLABORATION; storeMetaData.area = OHOS::DistributedKv::EL1; storeMetaData.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); MetaDataManager::GetInstance().SaveMeta(storeMetaData.GetKey(), storeMetaData); @@ -181,3 +185,5 @@ HWTEST_F(CloudDataTest, GetSchema, TestSize.Level0) ASSERT_TRUE( MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), storeMetaData, true)); } +} // namespace DistributedDataTest +} // namespace OHOS -- Gitee From d219683c1807c5d058bc7554ebd2d9ea10b2c1d0 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 10 May 2023 19:19:07 +0800 Subject: [PATCH 14/95] support map and pointer in serializable Signed-off-by: Sven Wang --- .../include/serializable/serializable.h | 139 ++++++++++++------ .../framework/serializable/serializable.cpp | 18 ++- .../framework/test/serializable_test.cpp | 74 ++++++++++ 3 files changed, 184 insertions(+), 47 deletions(-) diff --git a/services/distributeddataservice/framework/include/serializable/serializable.h b/services/distributeddataservice/framework/include/serializable/serializable.h index 4b16f8dd..b2e3ac87 100644 --- a/services/distributeddataservice/framework/include/serializable/serializable.h +++ b/services/distributeddataservice/framework/include/serializable/serializable.h @@ -32,6 +32,7 @@ public: using json = nlohmann::json; using size_type = nlohmann::json::size_type; using error_handler_t = nlohmann::detail::error_handler_t; + API_EXPORT json Marshall() const; template static std::string Marshall(T &values) @@ -64,60 +65,114 @@ public: API_EXPORT static bool SetValue(json &node, const int32_t &value); API_EXPORT static bool SetValue(json &node, const int64_t &value); API_EXPORT static bool SetValue(json &node, const uint64_t &value); - API_EXPORT static bool SetValue(json &node, const bool &value); + // Use bool & to forbid the const T * auto convert to bool, const bool will convert to const uint32_t &value; + API_EXPORT static bool SetValue(json &node, bool &value); API_EXPORT static bool SetValue(json &node, const std::vector &value); API_EXPORT static bool SetValue(json &node, const Serializable &value); protected: API_EXPORT ~Serializable() = default; template - static bool GetValue(const json &node, const std::string &name, T *&value) - { - auto &subNode = GetSubNode(node, name); - if (subNode.is_null()) { - return false; - } - value = new(std::nothrow) T(); - if (value == nullptr) { - return false; - } - bool result = GetValue(subNode, "", *value); - if (!result) { - delete value; - value = nullptr; - } - return result; - } + static bool GetValue(const json &node, const std::string &name, std::vector &values); + template - static bool GetValue(const json &node, const std::string &name, std::vector &values) - { - auto &subNode = GetSubNode(node, name); - if (subNode.is_null() || !subNode.is_array()) { - return false; - } - bool result = true; - values.resize(subNode.size()); - for (size_type i = 0; i < subNode.size(); ++i) { - result = GetValue(subNode[i], "", values[i]) && result; - } - return result; - } + static bool SetValue(json &node, const std::vector &values); template - static bool SetValue(json &node, const std::vector &values) - { - bool result = true; - size_type i = 0; - node = json::value_t::array; - for (const auto &value : values) { - result = SetValue(node[i], value) && result; - i++; - } - return result; - } + static bool GetValue(const json &node, const std::string &name, std::map &values); + + template + static bool SetValue(json &node, const std::map &values); + + template + static bool GetValue(const json &node, const std::string &name, T *&value); + + template + static bool SetValue(json &node, const T *value); API_EXPORT static const json &GetSubNode(const json &node, const std::string &name); }; + +template +bool Serializable::GetValue(const json &node, const std::string &name, std::vector &values) +{ + auto &subNode = GetSubNode(node, name); + if (subNode.is_null() || !subNode.is_array()) { + return false; + } + bool result = true; + values.resize(subNode.size()); + for (size_type i = 0; i < subNode.size(); ++i) { + result = GetValue(subNode[i], "", values[i]) && result; + } + return result; +} + +template +bool Serializable::SetValue(json &node, const std::vector &values) +{ + bool result = true; + size_type i = 0; + node = json::value_t::array; + for (const auto &value : values) { + result = SetValue(node[i], value) && result; + i++; + } + return result; +} + +template +bool Serializable::GetValue(const json &node, const std::string &name, std::map &values) +{ + auto &subNode = GetSubNode(node, name); + if (subNode.is_null() || !subNode.is_object()) { + return false; + } + bool result = true; + for (auto object = subNode.begin(); object != subNode.end(); ++object) { + result = GetValue(object.value(), "", values[object.key()]) && result; + } + return result; +} + +template +bool Serializable::SetValue(json &node, const std::map &values) +{ + bool result = true; + node = json::value_t::object; + for (const auto &[key, value] : values) { + result = SetValue(node[key], value) && result; + } + return result; +} + +template +bool Serializable::GetValue(const json &node, const std::string &name, T *&value) +{ + auto &subNode = GetSubNode(node, name); + if (subNode.is_null()) { + return false; + } + value = new(std::nothrow) T(); + if (value == nullptr) { + return false; + } + bool result = GetValue(subNode, "", *value); + if (!result) { + delete value; + value = nullptr; + } + return result; +} + +template +bool Serializable::SetValue(json &node, const T *value) +{ + if (value == nullptr) { + return false; + } + return SetValue(node, *value); +} } // namespace DistributedData } // namespace OHOS #endif // OHOS_DISTRIBUTED_DATA_FRAMEWORKS_COMMON_SERIALIZABLE_H diff --git a/services/distributeddataservice/framework/serializable/serializable.cpp b/services/distributeddataservice/framework/serializable/serializable.cpp index 509880a8..c1c7ca8b 100644 --- a/services/distributeddataservice/framework/serializable/serializable.cpp +++ b/services/distributeddataservice/framework/serializable/serializable.cpp @@ -116,11 +116,19 @@ bool Serializable::GetValue(const json &node, const std::string &name, uint64_t bool Serializable::GetValue(const json &node, const std::string &name, bool &value) { auto &subNode = GetSubNode(node, name); - if (subNode.is_null() || !subNode.is_boolean()) { - return false; + if (subNode.is_boolean()) { + subNode.get_to(value); + return true; } - subNode.get_to(value); - return true; + + if (subNode.is_number_unsigned()) { + uint32_t number = 0; + subNode.get_to(number); + value = number != 0; + return true; + } + + return false; } bool Serializable::GetValue(const json &node, const std::string &name, std::vector &value) @@ -172,7 +180,7 @@ bool Serializable::SetValue(json &node, const uint64_t &value) return true; } -bool Serializable::SetValue(json &node, const bool &value) +bool Serializable::SetValue(json &node, bool &value) { node = value; return true; diff --git a/services/distributeddataservice/framework/test/serializable_test.cpp b/services/distributeddataservice/framework/test/serializable_test.cpp index 95c0154b..07199d39 100644 --- a/services/distributeddataservice/framework/test/serializable_test.cpp +++ b/services/distributeddataservice/framework/test/serializable_test.cpp @@ -159,3 +159,77 @@ HWTEST_F(SerializableTest, GetMutilVal, TestSize.Level2) normal1.Unmarshall(jstr); ASSERT_TRUE(normalEx == normal1) << normal1.name; } + +/** +* @tc.name: GetMap +* @tc.desc: mutil value case. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Sven Wang +*/ +HWTEST_F(SerializableTest, GetMap, TestSize.Level2) +{ + ZLOGI("SerializableSuite GetMapVals begin."); + std::map marshData; + NormalEx normalEx; + normalEx.normals = {Normal()}; + normalEx.name = "normalEx"; + marshData.insert(std::pair{"test1", normalEx}); + auto jsonData = NormalEx::Marshall(marshData); + + std::map unmarshData; + NormalEx::Unmarshall(jsonData, unmarshData); + ASSERT_TRUE((marshData["test1"] == unmarshData["test1"])) << jsonData; +} + +/** +* @tc.name: GetMapInStruct +* @tc.desc: mutil value case. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Sven Wang +*/ +HWTEST_F(SerializableTest, GetMapInStruct, TestSize.Level2) +{ + struct TestMeta : public Serializable { + std::map data; + std::map *index = nullptr; + std::vector> others; + ~TestMeta() { + delete index; + } + bool Marshal(json &node) const + { + SetValue(node[GET_NAME(data)], data); + SetValue(node[GET_NAME(index)], index); + SetValue(node[GET_NAME(others)], others); + return true; + } + + bool Unmarshal(const json &node) + { + GetValue(node, GET_NAME(data), data); + GetValue(node, GET_NAME(index), index); + GetValue(node, GET_NAME(others), others); + return true; + } + }; + ZLOGI("SerializableSuite GetMapVals begin."); + TestMeta marData; + NormalEx normalEx; + normalEx.normals = {Normal()}; + normalEx.name = "normalEx"; + marData.data.insert(std::pair{"test1", normalEx}); + marData.others.push_back({std::pair{"test2", normalEx}}); + marData.index = new (std::nothrow) std::map; + ASSERT_NE(marData.index, nullptr); + marData.index->insert(std::pair{"test1", true}); + marData.index->insert(std::pair{"test2", true}); + auto jsonData = NormalEx::Marshall(marData); + TestMeta unmarData; + NormalEx::Unmarshall(jsonData, unmarData); + ASSERT_TRUE((marData.data == unmarData.data)) << jsonData; + ASSERT_TRUE((marData.others == unmarData.others)) << jsonData; + ASSERT_NE(unmarData.index, nullptr); + ASSERT_TRUE((*marData.index == *unmarData.index)) << jsonData; +} \ No newline at end of file -- Gitee From 60ef71efe5306cce8a613e3f888c58e46fb70cb0 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 10 May 2023 20:30:42 +0800 Subject: [PATCH 15/95] fix:ut Signed-off-by: htt1997 --- .../service/test/cloud_data_test.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index f640631a..031ea345 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -34,11 +34,9 @@ using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; namespace OHOS { namespace DistributedDataTest { - static constexpr const char *TEST_CLOUD_BUNDLE = "test_cloud_bundleName"; static constexpr const char *TEST_CLOUD_APPID = "test_cloud_appid"; static constexpr const char *TEST_CLOUD_STORE = "test_cloud_database_name"; - class CloudDataTest : public testing::Test { public: static void SetUpTestCase(void); @@ -49,7 +47,6 @@ public: protected: static constexpr const char *TEST_DISTRIBUTEDDATA_BUNDLE = "test_distributeddata"; static constexpr const char *TEST_DISTRIBUTEDDATA_STORE = "test_service_meta"; - static constexpr const char *TEST_DISTRIBUTEDDATA_USER = "-1"; void InitMetaData(); static std::shared_ptr dbStoreMock_; @@ -118,9 +115,9 @@ void CloudDataTest::InitMetaData() metaData_.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; metaData_.appId = TEST_DISTRIBUTEDDATA_BUNDLE; metaData_.bundleName = TEST_DISTRIBUTEDDATA_BUNDLE; - metaData_.user = TEST_DISTRIBUTEDDATA_USER; - metaData_.area = OHOS::DistributedKv::EL1; metaData_.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + metaData_.user = std::to_string(DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(metaData_.tokenId)); + metaData_.area = OHOS::DistributedKv::EL1; metaData_.instanceId = 0; metaData_.isAutoSync = true; metaData_.storeType = 1; @@ -150,7 +147,6 @@ void CloudDataTest::SetUp() StoreMetaData storeMetaData; storeMetaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; - storeMetaData.user = -1; storeMetaData.bundleName = TEST_CLOUD_BUNDLE; storeMetaData.storeId = TEST_CLOUD_STORE; storeMetaData.instanceId = 0; @@ -158,6 +154,8 @@ void CloudDataTest::SetUp() storeMetaData.storeType = DistributedRdb::RDB_DEVICE_COLLABORATION; storeMetaData.area = OHOS::DistributedKv::EL1; storeMetaData.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + storeMetaData.user = + std::to_string(DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(storeMetaData.tokenId)); MetaDataManager::GetInstance().SaveMeta(storeMetaData.GetKey(), storeMetaData); } @@ -174,12 +172,13 @@ HWTEST_F(CloudDataTest, GetSchema, TestSize.Level0) { ZLOGI("CloudDataTest start"); std::shared_ptr cloudServerMock = std::make_shared(); - auto cloudInfo = cloudServerMock->GetServerInfo(-1); + auto cloudInfo = cloudServerMock->GetServerInfo( + DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(OHOS::IPCSkeleton::GetCallingTokenID())); ASSERT_TRUE(MetaDataManager::GetInstance().DelMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), true)); StoreMetaData storeMetaData; ASSERT_FALSE( MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), storeMetaData, true)); - CloudEvent::StoreInfo storeInfo { 0, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, 0 }; + CloudEvent::StoreInfo storeInfo { OHOS::IPCSkeleton::GetCallingTokenID(), TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, 0 }; auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "test_service"); EventCenter::GetInstance().PostEvent(move(event)); ASSERT_TRUE( -- Gitee From 0153e063364f43661a24ff9b544a5e153e855702 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 10 May 2023 20:37:46 +0800 Subject: [PATCH 16/95] fixed code style Signed-off-by: Sven Wang --- .../framework/test/serializable_test.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/framework/test/serializable_test.cpp b/services/distributeddataservice/framework/test/serializable_test.cpp index 07199d39..697b33b5 100644 --- a/services/distributeddataservice/framework/test/serializable_test.cpp +++ b/services/distributeddataservice/framework/test/serializable_test.cpp @@ -172,9 +172,9 @@ HWTEST_F(SerializableTest, GetMap, TestSize.Level2) ZLOGI("SerializableSuite GetMapVals begin."); std::map marshData; NormalEx normalEx; - normalEx.normals = {Normal()}; + normalEx.normals = { Normal() }; normalEx.name = "normalEx"; - marshData.insert(std::pair{"test1", normalEx}); + marshData.insert(std::pair{ "test1", normalEx }); auto jsonData = NormalEx::Marshall(marshData); std::map unmarshData; @@ -195,7 +195,8 @@ HWTEST_F(SerializableTest, GetMapInStruct, TestSize.Level2) std::map data; std::map *index = nullptr; std::vector> others; - ~TestMeta() { + ~TestMeta() + { delete index; } bool Marshal(json &node) const @@ -217,14 +218,14 @@ HWTEST_F(SerializableTest, GetMapInStruct, TestSize.Level2) ZLOGI("SerializableSuite GetMapVals begin."); TestMeta marData; NormalEx normalEx; - normalEx.normals = {Normal()}; + normalEx.normals = { Normal() }; normalEx.name = "normalEx"; - marData.data.insert(std::pair{"test1", normalEx}); - marData.others.push_back({std::pair{"test2", normalEx}}); + marData.data.insert(std::pair{ "test1", normalEx }); + marData.others.push_back({ std::pair{ "test2", normalEx } }); marData.index = new (std::nothrow) std::map; ASSERT_NE(marData.index, nullptr); - marData.index->insert(std::pair{"test1", true}); - marData.index->insert(std::pair{"test2", true}); + marData.index->insert(std::pair{ "test1", true }); + marData.index->insert(std::pair{ "test2", true }); auto jsonData = NormalEx::Marshall(marData); TestMeta unmarData; NormalEx::Unmarshall(jsonData, unmarData); -- Gitee From ab284a0ea8933201a66a483a45175d83ea82bfaf Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 10 May 2023 20:55:04 +0800 Subject: [PATCH 17/95] add map Signed-off-by: Sven Wang --- .../framework/include/serializable/serializable.h | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/framework/include/serializable/serializable.h b/services/distributeddataservice/framework/include/serializable/serializable.h index b2e3ac87..e358decc 100644 --- a/services/distributeddataservice/framework/include/serializable/serializable.h +++ b/services/distributeddataservice/framework/include/serializable/serializable.h @@ -15,6 +15,7 @@ #ifndef OHOS_DISTRIBUTED_DATA_FRAMEWORKS_COMMON_SERIALIZABLE_H #define OHOS_DISTRIBUTED_DATA_FRAMEWORKS_COMMON_SERIALIZABLE_H +#include #include #include #include "visibility.h" -- Gitee From b8581ae7a2d2173e4349bbf22d71e120eaede79e Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 10 May 2023 21:51:20 +0800 Subject: [PATCH 18/95] fix:format check Signed-off-by: htt1997 --- .../framework/include/store/auto_cache.h | 2 +- .../distributeddataservice/service/BUILD.gn | 3 +- .../service/cloud/cloud_service_impl.cpp | 1 - .../service/cloud/cloud_syncer.cpp | 28 ----------------- .../service/cloud/cloud_syncer.h | 30 ------------------- .../service/test/BUILD.gn | 3 +- 6 files changed, 4 insertions(+), 63 deletions(-) delete mode 100644 services/distributeddataservice/service/cloud/cloud_syncer.cpp delete mode 100644 services/distributeddataservice/service/cloud/cloud_syncer.h diff --git a/services/distributeddataservice/framework/include/store/auto_cache.h b/services/distributeddataservice/framework/include/store/auto_cache.h index 8884a789..b01834a5 100644 --- a/services/distributeddataservice/framework/include/store/auto_cache.h +++ b/services/distributeddataservice/framework/include/store/auto_cache.h @@ -45,7 +45,7 @@ public: API_EXPORT void Bind(std::shared_ptr executor); API_EXPORT Store GetStore(const StoreMetaData &meta, const Watchers &watchers, bool setWatchers = true); - + API_EXPORT void CloseStore(uint32_t tokenId, const std::string &storeId); API_EXPORT void CloseExcept(const std::set &users); diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 49e56b7d..0aaaf8f6 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -63,7 +63,6 @@ ohos_shared_library("distributeddatasvc") { "bootstrap/src/bootstrap.cpp", "cloud/cloud_service_impl.cpp", "cloud/cloud_service_stub.cpp", - "cloud/cloud_syncer.cpp", "config/src/config_factory.cpp", "config/src/model/backup_config.cpp", "config/src/model/checker_config.cpp", @@ -139,10 +138,10 @@ ohos_shared_library("distributeddatasvc") { deps = [ "${kv_store_distributeddb_path}:distributeddb", + "${relational_store_path}/interfaces/inner_api/cloud_data:cloud_data_inner", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", - "${relational_store_path}/interfaces/inner_api/cloud_data:cloud_data_inner", ] external_deps = [ diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 70efa85e..8b45fd18 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -21,7 +21,6 @@ #include "checker/checker_manager.h" #include "cloud/cloud_event.h" #include "cloud/cloud_server.h" -#include "cloud_syncer.h" #include "communicator/device_manager_adapter.h" #include "eventcenter/event_center.h" #include "feature/feature_system.h" diff --git a/services/distributeddataservice/service/cloud/cloud_syncer.cpp b/services/distributeddataservice/service/cloud/cloud_syncer.cpp deleted file mode 100644 index f920c5ec..00000000 --- a/services/distributeddataservice/service/cloud/cloud_syncer.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2023 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 "cloud_syncer.h" - -namespace OHOS::CloudData { -CloudSyncer &CloudSyncer::GetInstance() -{ - static CloudSyncer instance; - return instance; -} - -void CloudSyncer::Sync(const DistributedData::CloudInfo &cloudInfo) -{ -} -} // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_syncer.h b/services/distributeddataservice/service/cloud/cloud_syncer.h deleted file mode 100644 index c9dd7971..00000000 --- a/services/distributeddataservice/service/cloud/cloud_syncer.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2023 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_SYNCER_H -#define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SYNCER_H - -#include "cloud/cloud_info.h" - -namespace OHOS::CloudData { -class CloudSyncer { -public: - static CloudSyncer &GetInstance(); - void Sync(const DistributedData::CloudInfo &cloudInfo); - -private: -}; -} // namespace OHOS::CloudData -#endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SYNCER_H diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index cb562215..7cda59fa 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -43,7 +43,8 @@ ohos_unittest("CloudDataTest") { "mock/db_change_data_mock.cpp", "mock/db_store_mock.cpp", ] - include_dirs = [ "../../../../../relational_store/interfaces/inner_api/rdb/include" ] + include_dirs = + [ "../../../../../relational_store/interfaces/inner_api/rdb/include" ] configs = [ ":module_private_config" ] -- Gitee From aff16fba7a15c4603d19ecd1ced846dc340cde98 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Thu, 11 May 2023 10:12:10 +0800 Subject: [PATCH 19/95] fix dfx test Signed-off-by: renjiecui --- .../adapter/dfx/test/BUILD.gn | 5 ++++- .../unittest/distributeddata_dfx_ut_test.cpp | 16 ++++++++++------ .../adapter/include/dfx/reporter.h | 11 +++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/adapter/dfx/test/BUILD.gn b/services/distributeddataservice/adapter/dfx/test/BUILD.gn index de03ed06..0c2a6050 100755 --- a/services/distributeddataservice/adapter/dfx/test/BUILD.gn +++ b/services/distributeddataservice/adapter/dfx/test/BUILD.gn @@ -118,6 +118,9 @@ group("unittest") { testonly = true deps = [] - deps += [ ":DistributeddataDfxMSTTest" ] + deps += [ + ":DistributeddataDfxMSTTest", + ":DistributeddataDfxUTTest", + ] } ############################################################################### diff --git a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp index b93b5632..f4887d3c 100644 --- a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp +++ b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp @@ -35,21 +35,25 @@ public: void DistributedataDfxUTTest::SetUpTestCase() { - size_t max = 12; - size_t min = 5; - Reporter::GetInstance()->SetThreadPool(std::make_shared(max, min)); FakeHivew::Clear(); } void DistributedataDfxUTTest::TearDownTestCase() { - Reporter::GetInstance()->SetThreadPool(nullptr); FakeHivew::Clear(); } -void DistributedataDfxUTTest::SetUp() {} +void DistributedataDfxUTTest::SetUp() +{ + size_t max = 12; + size_t min = 5; + Reporter::GetInstance()->SetThreadPool(std::make_shared(max, min)); +} -void DistributedataDfxUTTest::TearDown() {} +void DistributedataDfxUTTest::TearDown() +{ + Reporter::GetInstance()->SetThreadPool(nullptr); +} /** * @tc.name: Dfx001 diff --git a/services/distributeddataservice/adapter/include/dfx/reporter.h b/services/distributeddataservice/adapter/include/dfx/reporter.h index f84af98a..c2537b57 100644 --- a/services/distributeddataservice/adapter/include/dfx/reporter.h +++ b/services/distributeddataservice/adapter/include/dfx/reporter.h @@ -43,6 +43,17 @@ public: void SetThreadPool(std::shared_ptr executors) { executors_ = executors; + if (executors == nullptr) { + ServiceFault(); + RuntimeFault(); + DatabaseFault(); + CommunicationFault(); + DatabaseStatistic(); + VisitStatistic(); + TrafficStatistic(); + ApiPerformanceStatistic(); + BehaviourReporter(); + } }; private: -- Gitee From aee94dab5f248d8299f16bd42bb8cdb9398eda0d Mon Sep 17 00:00:00 2001 From: htt1997 Date: Fri, 12 May 2023 10:49:18 +0800 Subject: [PATCH 20/95] fix:Review comments modification Signed-off-by: htt1997 --- .../service/cloud/cloud_service_impl.cpp | 119 ++++++++++-------- .../service/cloud/cloud_service_impl.h | 6 +- .../service/cloud/cloud_service_stub.cpp | 8 ++ .../service/rdb/rdb_service_impl.cpp | 48 ++----- .../service/rdb/rdb_service_impl.h | 2 - .../service/test/BUILD.gn | 1 + 6 files changed, 89 insertions(+), 95 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 8b45fd18..55bcde15 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -50,46 +50,12 @@ CloudServiceImpl::Factory::~Factory() {} CloudServiceImpl::CloudServiceImpl() { EventCenter::GetInstance().Subscribe(CloudEvent::FEATURE_INIT, [this](const Event &event) { - auto &rdbEvent = static_cast(event); - CloudInfo cloudInfo; - cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); - if (GetServerInfo(cloudInfo) != SUCCESS) { - ZLOGE("failed, user:%{public}d", cloudInfo.user); - return; - } - UpdateCloudInfo(cloudInfo); - AddSchema(cloudInfo); + FeatureInit(event); + return; }); EventCenter::GetInstance().Subscribe(CloudEvent::GET_SCHEMA, [this](const Event &event) { - auto &rdbEvent = static_cast(event); - auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); - auto schemaMeta = GetSchemaMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().instanceId); - auto storeMeta = GetStoreMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, - rdbEvent.GetStoreInfo().instanceId); - - AutoCache::Watchers watchers; - auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers, false); - if (store == nullptr) { - ZLOGE("store is nullptr"); - return; - } - store->SetSchema(schemaMeta); - auto instance = CloudServer::GetInstance(); - if (instance == nullptr) { - ZLOGE("instance is nullptr"); - return; - } - for (auto &database : schemaMeta.databases) { - if (database.name != rdbEvent.GetStoreInfo().storeName /* || don't need sync */) { - continue; - } - auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, database); - if (cloudDB != nullptr) { - store->Bind(cloudDB); - } - // do sync - } + GetSchema(event); return; }); } @@ -97,8 +63,7 @@ CloudServiceImpl::CloudServiceImpl() int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &switches) { CloudInfo cloudInfo; - cloudInfo.id = id; - if (GetCloudInfo(cloudInfo) != SUCCESS && GetServerInfo(cloudInfo) != SUCCESS) { + if (GetCloudInfo(IPCSkeleton::GetCallingTokenID(), id, cloudInfo) != SUCCESS) { return INVALID_ARGUMENT; } cloudInfo.enableCloud = true; @@ -110,7 +75,7 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::mapcloudSwitch = item.second; } if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { return ERROR; @@ -121,8 +86,7 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &actions) { CloudInfo cloudInfo; - cloudInfo.id = id; - if (GetCloudInfo(cloudInfo) != SUCCESS && GetServerInfo(cloudInfo) != SUCCESS) { - ZLOGE("id:%{public}s", Anonymous::Change(id).c_str()); + if (GetCloudInfo(IPCSkeleton::GetCallingTokenID(), id, cloudInfo) != SUCCESS) { return INVALID_ARGUMENT; } auto keys = cloudInfo.GetSchemaKey(); @@ -180,14 +141,18 @@ int32_t CloudServiceImpl::NotifyDataChange(const std::string &id, const std::str return 0; } -int32_t CloudServiceImpl::GetCloudInfo(CloudInfo &cloudInfo) +int32_t CloudServiceImpl::GetCloudInfo(uint32_t tokenId, const std::string &id, CloudInfo &cloudInfo) { - auto tokenId = IPCSkeleton::GetCallingTokenID(); cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); - if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { - ZLOGE("invalid argument id:%{public}s, user:%{public}d", Anonymous::Change(cloudInfo.id).c_str(), - cloudInfo.user); - return ERROR; + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true) && + GetServerInfo(cloudInfo ) != SUCCESS) { + ZLOGE("invalid args, user:%{public}d", cloudInfo.user); + return INVALID_ARGUMENT; + } + if (cloudInfo.id != id) { + ZLOGE("invalid args, [input] id:%{public}s, [exist] id:%{public}s", + Anonymous::Change(id).c_str(), Anonymous::Change(cloudInfo.id).c_str()); + return INVALID_ARGUMENT; } return SUCCESS; } @@ -277,4 +242,52 @@ StoreMetaData CloudServiceImpl::GetStoreMata(int32_t userId, const std::string & MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData); return storeMetaData; } + +void CloudServiceImpl::FeatureInit(const Event &event) +{ + CloudInfo cloudInfo; + std::vector users; + if (!DistributedKv::AccountDelegate::GetInstance()->QueryUsers(users) || users.empty()) { + return; + } + cloudInfo.user = *users.begin(); + if (GetServerInfo(cloudInfo) != SUCCESS) { + ZLOGE("failed, user:%{public}d", cloudInfo.user); + return; + } + UpdateCloudInfo(cloudInfo); + AddSchema(cloudInfo); +} + +void CloudServiceImpl::GetSchema(const Event &event) { + auto &rdbEvent = static_cast(event); + auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); + auto schemaMeta = GetSchemaMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().instanceId); + auto storeMeta = GetStoreMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, + rdbEvent.GetStoreInfo().instanceId); + + AutoCache::Watchers watchers; + auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers, false); + if (store == nullptr) { + ZLOGE("store is nullptr"); + return; + } + store->SetSchema(schemaMeta); + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + ZLOGE("instance is nullptr"); + return; + } + for (auto &database : schemaMeta.databases) { + if (database.name != rdbEvent.GetStoreInfo().storeName /* || don't need sync */) { + continue; + } + auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, database); + if (cloudDB != nullptr) { + store->Bind(cloudDB); + } + // do sync + } + return; +} } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index b71bbf4d..26ed17e7 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -20,6 +20,7 @@ #include "cloud_service_stub.h" #include "cloud/cloud_info.h" #include "cloud/schema_meta.h" +#include "cloud/cloud_event.h" namespace OHOS::CloudData { class CloudServiceImpl : public CloudServiceStub { @@ -45,15 +46,18 @@ private: using CloudInfo = DistributedData::CloudInfo; using SchemaMeta = DistributedData::SchemaMeta; + using Event = DistributedData::Event; void UpdateCloudInfo(CloudInfo &cloudInfo); void AddSchema(CloudInfo &cloudInfo); SchemaMeta GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId); StoreMetaData GetStoreMata(int32_t userId, const std::string &bundleName, const std::string &storeName, int32_t instanceId); - int32_t GetCloudInfo(CloudInfo &cloudInfo); + int32_t GetCloudInfo(uint32_t tokenId, const std::string &id, CloudInfo &cloudInfo); int32_t GetServerInfo(CloudInfo &cloudInfo); int32_t GetAppSchema(int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta); + void FeatureInit(const Event &event); + void GetSchema(const Event &event); }; } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp index 2ccf869e..15556684 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp @@ -19,8 +19,10 @@ #include "itypes_util.h" #include "log_print.h" #include "utils/anonymous.h" +#include "tokenid_kit.h" namespace OHOS::CloudData { using namespace DistributedData; +using namespace OHOS::Security::AccessToken; const CloudServiceStub::Handler CloudServiceStub::HANDLERS[TRANS_BUTT] = { &CloudServiceStub::OnEnableCloud, &CloudServiceStub::OnDisableCloud, @@ -43,6 +45,12 @@ int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, ZLOGE("not support code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); return -1; } + + if(!TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID())){ + ZLOGE("permission denied! code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); + return -1; + } + std::string id; if (!ITypesUtil::Unmarshal(data, id)) { ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str()); diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index ab255d06..1f6722eb 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -54,7 +54,7 @@ RdbServiceImpl::Factory::Factory() return std::make_shared(); }); AutoCache::GetInstance().RegCreator(RDB_DEVICE_COLLABORATION, [](const StoreMetaData &metaData) -> GeneralStore* { - return new RdbGeneralStore(metaData); + return new (std::nothrow) RdbGeneralStore(metaData); }); } @@ -466,8 +466,7 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) int32_t RdbServiceImpl::OnInitialize() { - CloudEvent::StoreInfo storeInfo = { IPCSkeleton::GetCallingTokenID() }; - auto initEvt = std::make_unique(CloudEvent::FEATURE_INIT, storeInfo); + auto initEvt = std::make_unique(CloudEvent::FEATURE_INIT, CloudEvent::StoreInfo()); EventCenter::GetInstance().PostEvent(std::move(initEvt)); return RDB_OK; } @@ -478,25 +477,13 @@ int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) ZLOGE("permission error"); return RDB_ERROR; } - - auto storeMeta = GetStoreMetaData(param); - StoreMetaData oldMeta; - bool isCreated = MetaDataManager::GetInstance().LoadMeta(storeMeta.GetKey(), oldMeta, true); - if (isCreated && (oldMeta.storeType != storeMeta.storeType || - Constant::NotEqual(oldMeta.isEncrypt, storeMeta.isEncrypt) || - oldMeta.area != storeMeta.area)) { - ZLOGE("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " - "area:%{public}d->%{public}d", - storeMeta.bundleName.c_str(), storeMeta.storeId.c_str(), oldMeta.storeType, storeMeta.storeType, - oldMeta.isEncrypt, storeMeta.isEncrypt, oldMeta.area, storeMeta.area); - return RDB_ERROR; - } - auto saved = MetaDataManager::GetInstance().SaveMeta(storeMeta.GetKey(), storeMeta, true); - if (!saved) { + auto syncer = GetRdbSyncer(param); + if (syncer == nullptr) { return RDB_ERROR; } - CloudEvent::StoreInfo storeInfo { IPCSkeleton::GetCallingTokenID(), param.bundleName_, param.storeName_, - storeMeta.instanceId }; + CloudEvent::StoreInfo storeInfo{ IPCSkeleton::GetCallingTokenID(), param.bundleName_, + RdbSyncer::RemoveSuffix(param.storeName_), + RdbSyncer::GetInstIndex(IPCSkeleton::GetCallingTokenID(), param.bundleName_) }; auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "relational_store"); EventCenter::GetInstance().PostEvent(move(event)); return RDB_OK; @@ -507,10 +494,10 @@ StoreMetaData RdbServiceImpl::GetStoreMetaData(const RdbSyncerParam ¶m) StoreMetaData metaData; metaData.uid = IPCSkeleton::GetCallingUid(); metaData.tokenId = IPCSkeleton::GetCallingTokenID(); - metaData.instanceId = GetInstIndex(metaData.tokenId, param.bundleName_, param.storeName_); + metaData.instanceId = RdbSyncer::GetInstIndex(metaData.tokenId, param.bundleName_); metaData.bundleName = param.bundleName_; metaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; - metaData.storeId = param.storeName_; + metaData.storeId = RdbSyncer::RemoveSuffix(param.storeName_); metaData.user = std::to_string(AccountDelegate::GetInstance()->GetUserByToken(metaData.tokenId)); metaData.storeType = param.type_; metaData.securityLevel = param.level_; @@ -524,23 +511,6 @@ StoreMetaData RdbServiceImpl::GetStoreMetaData(const RdbSyncerParam ¶m) return metaData; } -int32_t RdbServiceImpl::GetInstIndex(uint32_t tokenId, const std::string &bundleName, const std::string &storeName) -{ - if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { - return 0; - } - - HapTokenInfo tokenInfo; - tokenInfo.instIndex = -1; - int errCode = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); - if (errCode != RET_SUCCESS) { - ZLOGE("GetHapTokenInfo error:%{public}d, tokenId:0x%{public}x bundleName:%{public}s storeName:%{public}s", - errCode, tokenId, bundleName.c_str(), storeName.c_str()); - return -1; - } - return tokenInfo.instIndex; -} - int32_t RdbServiceImpl::OnExecutor(std::shared_ptr executors) { executors_ = executors; diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index c5af1b9d..9d3b22b2 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -84,8 +84,6 @@ private: StoreMetaData GetStoreMetaData(const RdbSyncerParam& param); - int32_t GetInstIndex(uint32_t tokenId, const std::string &appId, const std::string &storeName); - class DeathRecipientImpl : public IRemoteObject::DeathRecipient { public: using DeathCallback = std::function; diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 7cda59fa..5a7ff143 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -43,6 +43,7 @@ ohos_unittest("CloudDataTest") { "mock/db_change_data_mock.cpp", "mock/db_store_mock.cpp", ] + include_dirs = [ "../../../../../relational_store/interfaces/inner_api/rdb/include" ] -- Gitee From d2bb6eaae76fc7956cd8de6ba3f551216bacf8fb Mon Sep 17 00:00:00 2001 From: htt1997 Date: Fri, 12 May 2023 11:09:58 +0800 Subject: [PATCH 21/95] fix:code check Signed-off-by: htt1997 --- services/distributeddataservice/service/BUILD.gn | 1 + .../service/cloud/cloud_service_impl.cpp | 9 +++++---- .../service/cloud/cloud_service_stub.cpp | 6 +++--- .../service/rdb/rdb_service_impl.cpp | 2 +- services/distributeddataservice/service/test/BUILD.gn | 2 +- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 0aaaf8f6..1d29c1ef 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -150,6 +150,7 @@ ohos_shared_library("distributeddatasvc") { "ability_runtime:ability_manager", "ability_runtime:dataobs_manager", "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 55bcde15..e9e932fc 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -145,13 +145,13 @@ int32_t CloudServiceImpl::GetCloudInfo(uint32_t tokenId, const std::string &id, { cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true) && - GetServerInfo(cloudInfo ) != SUCCESS) { + GetServerInfo(cloudInfo) != SUCCESS) { ZLOGE("invalid args, user:%{public}d", cloudInfo.user); return INVALID_ARGUMENT; } if (cloudInfo.id != id) { - ZLOGE("invalid args, [input] id:%{public}s, [exist] id:%{public}s", - Anonymous::Change(id).c_str(), Anonymous::Change(cloudInfo.id).c_str()); + ZLOGE("invalid args, [input] id:%{public}s, [exist] id:%{public}s", Anonymous::Change(id).c_str(), + Anonymous::Change(cloudInfo.id).c_str()); return INVALID_ARGUMENT; } return SUCCESS; @@ -259,7 +259,8 @@ void CloudServiceImpl::FeatureInit(const Event &event) AddSchema(cloudInfo); } -void CloudServiceImpl::GetSchema(const Event &event) { +void CloudServiceImpl::GetSchema(const Event &event) +{ auto &rdbEvent = static_cast(event); auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); auto schemaMeta = GetSchemaMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().instanceId); diff --git a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp index 15556684..61e9dc63 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp @@ -45,12 +45,12 @@ int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, ZLOGE("not support code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); return -1; } - - if(!TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID())){ + + if (!TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID())) { ZLOGE("permission denied! code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); return -1; } - + std::string id; if (!ITypesUtil::Unmarshal(data, id)) { ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str()); diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 1f6722eb..1938c504 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -481,7 +481,7 @@ int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) if (syncer == nullptr) { return RDB_ERROR; } - CloudEvent::StoreInfo storeInfo{ IPCSkeleton::GetCallingTokenID(), param.bundleName_, + CloudEvent::StoreInfo storeInfo { IPCSkeleton::GetCallingTokenID(), param.bundleName_, RdbSyncer::RemoveSuffix(param.storeName_), RdbSyncer::GetInstIndex(IPCSkeleton::GetCallingTokenID(), param.bundleName_) }; auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "relational_store"); diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 5a7ff143..97d837b7 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -43,7 +43,7 @@ ohos_unittest("CloudDataTest") { "mock/db_change_data_mock.cpp", "mock/db_store_mock.cpp", ] - + include_dirs = [ "../../../../../relational_store/interfaces/inner_api/rdb/include" ] -- Gitee From 259eebf63053d97fad1b945a12c85c69fbf61e7d Mon Sep 17 00:00:00 2001 From: srr101 Date: Fri, 12 May 2023 11:29:33 +0800 Subject: [PATCH 22/95] bug fix Signed-off-by: srr101 --- .../service/data_share/strategies/query_strategy.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp index c2e2852a..6e16f1a7 100644 --- a/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp @@ -34,6 +34,7 @@ std::shared_ptr QueryStrategy::Execute( ZLOGE("get strategy fail, maybe memory not enough"); return nullptr; } + context->isRead = true; if (!(*preProcess)(context)) { errCode = context->errCode; ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); -- Gitee From 22476a1cca9c10626689816141ee6f2d2356cbe7 Mon Sep 17 00:00:00 2001 From: srr101 Date: Fri, 12 May 2023 16:33:33 +0800 Subject: [PATCH 23/95] =?UTF-8?q?=E7=BB=99errorcode=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: srr101 --- .../service/data_share/common/rdb_delegate.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.h b/services/distributeddataservice/service/data_share/common/rdb_delegate.h index b8fdcd92..c30ceac8 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.h +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.h @@ -45,7 +45,7 @@ public: private: static std::atomic resultSetCount; std::shared_ptr store_; - int errCode_; + int errCode_ = E_OK; }; class DefaultOpenCallback : public RdbOpenCallback { public: -- Gitee From 285d9f41e8647a2d4391f33eb72aa57f1f7a5df1 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Fri, 12 May 2023 16:34:10 +0800 Subject: [PATCH 24/95] modify executors_ initializing position Signed-off-by: renjiecui --- .../app/src/kvstore_data_service.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index ee5ef9e7..a1853289 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -68,12 +68,24 @@ KvStoreDataService::KvStoreDataService(bool runOnCreate) : SystemAbility(runOnCreate), mutex_(), clients_() { ZLOGI("begin."); + if (executors_ == nullptr) { + constexpr size_t MAX = 12; + constexpr size_t MIN = 5; + executors_ = std::make_shared(MAX, MIN); + DistributedDB::RuntimeConfig::SetThreadPool(std::make_shared(executors_)); + } } KvStoreDataService::KvStoreDataService(int32_t systemAbilityId, bool runOnCreate) : SystemAbility(systemAbilityId, runOnCreate), mutex_(), clients_() { ZLOGI("begin"); + if (executors_ == nullptr) { + constexpr size_t MAX = 12; + constexpr size_t MIN = 5; + executors_ = std::make_shared(MAX, MIN); + DistributedDB::RuntimeConfig::SetThreadPool(std::make_shared(executors_)); + } } KvStoreDataService::~KvStoreDataService() @@ -91,7 +103,6 @@ void KvStoreDataService::Initialize() #endif auto communicator = std::make_shared(RouteHeadHandlerImpl::Create); auto ret = KvStoreDelegateManager::SetProcessCommunicator(communicator); - DistributedDB::RuntimeConfig::SetThreadPool(std::make_shared(executors_)); ZLOGI("set communicator ret:%{public}d.", static_cast(ret)); AppDistributedKv::CommunicationProvider::GetInstance(); @@ -233,9 +244,6 @@ void KvStoreDataService::OnStart() { ZLOGI("distributeddata service onStart"); EventCenter::Defer defer; - constexpr size_t MAX = 12; - constexpr size_t MIN = 5; - executors_ = std::make_shared(MAX, MIN); Reporter::GetInstance()->SetThreadPool(executors_); AccountDelegate::GetInstance()->BindExecutor(executors_); AccountDelegate::GetInstance()->RegisterHashFunc(Crypto::Sha256); -- Gitee From fbcff23d662d8250584b0660fe67dfe751e666cd Mon Sep 17 00:00:00 2001 From: ylq121 Date: Sat, 13 May 2023 15:46:20 +0800 Subject: [PATCH 25/95] system api Signed-off-by: ylq121 --- .../service/cloud/cloud_service_stub.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp index 61e9dc63..b14485f4 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_stub.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_stub.cpp @@ -48,7 +48,8 @@ int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, if (!TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID())) { ZLOGE("permission denied! code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); - return -1; + auto result = static_cast(PERMISSION_DENIED); + return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; } std::string id; -- Gitee From ef5b92e6731bf9efe365c700bad77a93b40091c2 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sat, 13 May 2023 16:56:16 +0800 Subject: [PATCH 26/95] feat:Delete schemata during HAP update Signed-off-by: htt1997 --- .../app/src/feature_stub_impl.cpp | 8 ++ .../app/src/feature_stub_impl.h | 1 + .../app/src/kvstore_data_service.cpp | 10 ++ .../app/src/kvstore_data_service.h | 2 + .../app/src/uninstaller/uninstaller_impl.cpp | 109 ++++++++++++------ .../app/src/uninstaller/uninstaller_impl.h | 8 +- .../framework/feature/feature_system.cpp | 6 + .../include/feature/feature_system.h | 1 + .../service/cloud/cloud_service_impl.cpp | 6 + 9 files changed, 113 insertions(+), 38 deletions(-) diff --git a/services/distributeddataservice/app/src/feature_stub_impl.cpp b/services/distributeddataservice/app/src/feature_stub_impl.cpp index 01171f95..0e640edd 100644 --- a/services/distributeddataservice/app/src/feature_stub_impl.cpp +++ b/services/distributeddataservice/app/src/feature_stub_impl.cpp @@ -57,6 +57,14 @@ int32_t FeatureStubImpl::OnAppUninstall(const std::string &bundleName, int32_t u return featureImpl_->OnAppUninstall(bundleName, user, index, tokenId); } +int32_t FeatureStubImpl::OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId) +{ + if (featureImpl_ == nullptr) { + return -1; + } + return featureImpl_->OnAppUpdate(bundleName, user, index, tokenId); +} + int32_t FeatureStubImpl::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) { if (featureImpl_ == nullptr) { diff --git a/services/distributeddataservice/app/src/feature_stub_impl.h b/services/distributeddataservice/app/src/feature_stub_impl.h index 6fc9c824..b00646bc 100644 --- a/services/distributeddataservice/app/src/feature_stub_impl.h +++ b/services/distributeddataservice/app/src/feature_stub_impl.h @@ -33,6 +33,7 @@ public: int32_t OnInitialize(std::shared_ptr executor); int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName); int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); + int32_t OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m); int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account); int32_t Online(const std::string &device); diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index ee5ef9e7..2e190156 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -716,4 +716,14 @@ int32_t KvStoreDataService::OnUninstall(const std::string &bundleName, int32_t u }); return 0; } + +int32_t KvStoreDataService::OnUpdate(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId) +{ + features_.ForEachCopies( + [bundleName, user, index, tokenId](const auto &, sptr &value) { + value->OnAppUpdate(bundleName, user, index, tokenId); + return false; + }); + return 0; +} } // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index fae1dbac..60aac352 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -76,6 +76,8 @@ public: int32_t OnUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); + int32_t OnUpdate(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); + private: void NotifyAccountEvent(const AccountEventInfo &eventInfo); class KvStoreClientDeathObserverImpl { diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index 81072e46..12aa99db 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -27,6 +27,7 @@ #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" #include "permit_delegate.h" +#include "cloud/cloud_info.h" #include "utils/block_integer.h" namespace OHOS::DistributedKv { @@ -36,26 +37,28 @@ using namespace OHOS::AppExecFwk; using namespace OHOS::DistributedData; using namespace OHOS::EventFwk; -UninstallEventSubscriber::UninstallEventSubscriber(const CommonEventSubscribeInfo &info, - UninstallEventCallback callback) - : CommonEventSubscriber(info), callback_(callback) -{} +UninstallEventSubscriber::UninstallEventSubscriber(const CommonEventSubscribeInfo &info) : CommonEventSubscriber(info) +{ +} void UninstallEventSubscriber::OnReceiveEvent(const CommonEventData &event) { ZLOGI("Intent Action Rec"); Want want = event.GetWant(); std::string action = want.GetAction(); - if (action != CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED && - action != OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) { - return; - } - - std::string bundleName = want.GetElement().GetBundleName(); - int32_t userId = want.GetIntParam(USER_ID, -1); - int32_t appIndex = want.GetIntParam(SANDBOX_APP_INDEX, 0); - ZLOGI("bundleName:%s, user:%d, appIndex:%d", bundleName.c_str(), userId, appIndex); - callback_(bundleName, userId, appIndex); + callbacks_.ComputeIfPresent(action, [&want](const auto& key, auto &callback) { + std::string bundleName = want.GetElement().GetBundleName(); + int32_t userId = want.GetIntParam(USER_ID, -1); + int32_t appIndex = want.GetIntParam(SANDBOX_APP_INDEX, 0); + ZLOGI("bundleName:%{public}s, user:%{public}d, appIndex:%{public}d", bundleName.c_str(), userId, appIndex); + callback(bundleName, userId, appIndex); + return true; + }); +} +int32_t UninstallEventSubscriber::RegisterCallback(const std::string &action, UninstallEventCallback callback) +{ + callbacks_.InsertOrAssign(action, std::move(callback)); + return Status::SUCCESS; } UninstallerImpl::~UninstallerImpl() @@ -84,34 +87,30 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared MatchingSkills matchingSkills; matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); matchingSkills.AddEvent(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); CommonEventSubscribeInfo info(matchingSkills); - auto callback = [kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { + + auto subscriber = std::make_shared(info); + auto removedCallback = [this, kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { kvStoreDataService->OnUninstall(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID()); - std::string prefix = StoreMetaData::GetPrefix({ DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, - std::to_string(userId), "default", bundleName }); - std::vector storeMetaData; - if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData)) { - ZLOGE("load meta failed!"); - return; - } - for (auto &meta : storeMetaData) { - if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) { - ZLOGI("uninstalled bundleName:%s, stordId:%s", bundleName.c_str(), meta.storeId.c_str()); - MetaDataManager::GetInstance().DelMeta(meta.GetKey()); - MetaDataManager::GetInstance().DelMeta(meta.GetSecretKey(), true); - MetaDataManager::GetInstance().DelMeta(meta.GetStrategyKey()); - MetaDataManager::GetInstance().DelMeta(meta.appId, true); - MetaDataManager::GetInstance().DelMeta(meta.GetKeyLocal(), true); - PermitDelegate::GetInstance().DelCache(meta.GetKey()); - } - } + OnUninstall(bundleName, userId, appIndex); }; - auto subscriber = std::make_shared(info, callback); + + auto updatedCallback = [this, kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { + kvStoreDataService->OnUpdate(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID()); + OnUpdate(bundleName, userId, appIndex); + }; + + subscriber->RegisterCallback(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED, removedCallback); + subscriber->RegisterCallback(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED, removedCallback); + subscriber->RegisterCallback(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED, updatedCallback); + subscriber_ = subscriber; executors_ = executors; executors_->Execute(GetTask()); return Status::SUCCESS; } + ExecutorPool::Task UninstallerImpl::GetTask() { return [this] { @@ -120,11 +119,51 @@ ExecutorPool::Task UninstallerImpl::GetTask() ZLOGI("subscribe uninstall event success"); return; } - ZLOGE("subscribe uninstall event fail, try times:%d", retryTime_); + ZLOGE("subscribe common event fail, try times:%{public}d", retryTime_); if (retryTime_++ >= RETRY_TIME) { return; } executors_->Schedule(std::chrono::milliseconds(RETRY_INTERVAL), GetTask()); }; } + +void UninstallerImpl::OnUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex) +{ + std::string prefix = StoreMetaData::GetPrefix( + { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName }); + std::vector storeMetaData; + if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData)) { + ZLOGE("load meta failed!"); + return; + } + for (auto &meta : storeMetaData) { + if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) { + ZLOGI("uninstalled bundleName:%{public}s stordId:%{public}s", bundleName.c_str(), meta.storeId.c_str()); + MetaDataManager::GetInstance().DelMeta(meta.GetKey()); + MetaDataManager::GetInstance().DelMeta(meta.GetSecretKey(), true); + MetaDataManager::GetInstance().DelMeta(meta.GetStrategyKey()); + MetaDataManager::GetInstance().DelMeta(meta.appId, true); + MetaDataManager::GetInstance().DelMeta(meta.GetKeyLocal(), true); + MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true); + PermitDelegate::GetInstance().DelCache(meta.GetKey()); + } + } +} + +void UninstallerImpl::OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex) +{ + std::string prefix = StoreMetaData::GetPrefix( + { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName }); + std::vector storeMetaData; + if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData)) { + ZLOGE("load meta failed!"); + return; + } + for (auto &meta : storeMetaData) { + if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) { + ZLOGI("updated bundleName:%{public}s, stordId:%{public}s", bundleName.c_str(), meta.storeId.c_str()); + MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true); + } + } +} } // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h index 50ec5fe3..9607f58b 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h @@ -25,8 +25,8 @@ using UninstallEventCallback = std::function callbacks_; }; class UninstallerImpl : public Uninstaller { public: @@ -47,6 +47,8 @@ public: private: static constexpr int32_t RETRY_TIME = 300; static constexpr int32_t RETRY_INTERVAL = 100; + void OnUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex); + void OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex); int32_t retryTime_; ExecutorPool::Task GetTask(); std::shared_ptr subscriber_ {}; diff --git a/services/distributeddataservice/framework/feature/feature_system.cpp b/services/distributeddataservice/framework/feature/feature_system.cpp index 6bfd3d23..49b69e8c 100644 --- a/services/distributeddataservice/framework/feature/feature_system.cpp +++ b/services/distributeddataservice/framework/feature/feature_system.cpp @@ -76,6 +76,12 @@ int32_t FeatureSystem::Feature::OnAppUninstall(const std::string &bundleName, in return E_OK; } +int32_t FeatureSystem::Feature::OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index, + uint32_t tokenId) +{ + return E_OK; +} + int32_t FeatureSystem::Feature::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) { return E_OK; diff --git a/services/distributeddataservice/framework/include/feature/feature_system.h b/services/distributeddataservice/framework/include/feature/feature_system.h index 8aa732cf..6bb34530 100644 --- a/services/distributeddataservice/framework/include/feature/feature_system.h +++ b/services/distributeddataservice/framework/include/feature/feature_system.h @@ -42,6 +42,7 @@ public: virtual int32_t OnExecutor(std::shared_ptr executors); virtual int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName); virtual int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); + virtual int32_t OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); virtual int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m); virtual int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account); virtual int32_t Online(const std::string &device); diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index e9e932fc..e6875348 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -262,6 +262,9 @@ void CloudServiceImpl::FeatureInit(const Event &event) void CloudServiceImpl::GetSchema(const Event &event) { auto &rdbEvent = static_cast(event); + ZLOGD("Start GetSchema, bundleName:%{public}s, storeName:%{public}s, instanceId:%{public}d", + rdbEvent.GetStoreInfo().bundleName.c_str(), rdbEvent.GetStoreInfo().storeName.c_str(), + rdbEvent.GetStoreInfo().instanceId); auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); auto schemaMeta = GetSchemaMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().instanceId); auto storeMeta = GetStoreMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, @@ -287,6 +290,9 @@ void CloudServiceImpl::GetSchema(const Event &event) if (cloudDB != nullptr) { store->Bind(cloudDB); } + for (auto &table : database.tables) { + ZLOGD("table: %{public}s sync start", table.name.c_str()); + } // do sync } return; -- Gitee From de830f45ca2435849b41e95de09272936f38679f Mon Sep 17 00:00:00 2001 From: hanlu Date: Sat, 13 May 2023 18:13:13 +0800 Subject: [PATCH 27/95] f Signed-off-by: hanlu --- .../include/serializable/serializable.h | 68 ++- .../distributeddataservice/service/BUILD.gn | 6 + .../service/data_share/common/db_delegate.cpp | 16 +- .../service/data_share/common/db_delegate.h | 28 +- .../service/data_share/common/kv_delegate.cpp | 121 ++++- .../service/data_share/common/kv_delegate.h | 4 + .../data_share/common/rdb_delegate.cpp | 5 +- .../data_share/common/template_manager.cpp | 480 ++++++++++++++++++ .../data_share/common/template_manager.h | 118 +++++ .../service/data_share/common/uri_utils.h | 2 +- .../data_share/data/json_formatter.cpp | 38 ++ .../service/data_share/data/json_formatter.h | 36 ++ .../data_share/data/published_data.cpp | 126 +++++ .../service/data_share/data/published_data.h | 54 ++ .../data/resultset_json_formatter.cpp | 48 ++ .../data/resultset_json_formatter.h | 35 ++ .../service/data_share/data/template_data.cpp | 140 +++++ .../service/data_share/data/template_data.h | 67 +++ .../data_share/data_share_service_impl.cpp | 132 ++++- .../data_share/data_share_service_impl.h | 10 +- .../data_share/strategies/get_data_strategy.h | 2 + .../strategies/publish_strategy.cpp | 28 +- .../data_share/strategies/publish_strategy.h | 6 +- .../strategies/subscribe_strategy.cpp | 1 + 24 files changed, 1516 insertions(+), 55 deletions(-) create mode 100644 services/distributeddataservice/service/data_share/common/template_manager.cpp create mode 100644 services/distributeddataservice/service/data_share/common/template_manager.h create mode 100644 services/distributeddataservice/service/data_share/data/json_formatter.cpp create mode 100644 services/distributeddataservice/service/data_share/data/json_formatter.h create mode 100644 services/distributeddataservice/service/data_share/data/published_data.cpp create mode 100644 services/distributeddataservice/service/data_share/data/published_data.h create mode 100644 services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp create mode 100644 services/distributeddataservice/service/data_share/data/resultset_json_formatter.h create mode 100644 services/distributeddataservice/service/data_share/data/template_data.cpp create mode 100644 services/distributeddataservice/service/data_share/data/template_data.h diff --git a/services/distributeddataservice/framework/include/serializable/serializable.h b/services/distributeddataservice/framework/include/serializable/serializable.h index e358decc..47d1805e 100644 --- a/services/distributeddataservice/framework/include/serializable/serializable.h +++ b/services/distributeddataservice/framework/include/serializable/serializable.h @@ -15,13 +15,13 @@ #ifndef OHOS_DISTRIBUTED_DATA_FRAMEWORKS_COMMON_SERIALIZABLE_H #define OHOS_DISTRIBUTED_DATA_FRAMEWORKS_COMMON_SERIALIZABLE_H -#include #include #include #include "visibility.h" #ifndef JSON_NOEXCEPTION #define JSON_NOEXCEPTION #endif +#include #include namespace OHOS { namespace DistributedData { @@ -70,6 +70,12 @@ public: API_EXPORT static bool SetValue(json &node, bool &value); API_EXPORT static bool SetValue(json &node, const std::vector &value); API_EXPORT static bool SetValue(json &node, const Serializable &value); + + template + API_EXPORT static bool SetValue(json &node, const std::variant<_Types...> &input); + + template + API_EXPORT static bool GetValue(const json &node, const std::string &name, std::variant<_Types...> &value); protected: API_EXPORT ~Serializable() = default; @@ -91,6 +97,17 @@ protected: template static bool SetValue(json &node, const T *value); + template + static bool ReadVariant(const json &node, const std::string &name, uint32_t step, uint32_t index, _OutTp &output); + + template + static bool ReadVariant(const json &node, const std::string &name, uint32_t step, uint32_t index, _OutTp &output); + + template + static bool WriteVariant(json &node, uint32_t step, const _InTp &input); + + template + static bool WriteVariant(json &node, uint32_t step, const _InTp &input); API_EXPORT static const json &GetSubNode(const json &node, const std::string &name); }; @@ -174,6 +191,55 @@ bool Serializable::SetValue(json &node, const T *value) } return SetValue(node, *value); } + +template +bool Serializable::SetValue(json &node, const std::variant<_Types...> &input) +{ + node[GET_NAME(type)] = input.index(); + return WriteVariant(node, 0, input); +} + +template +bool Serializable::GetValue(const json &node, const std::string &name, std::variant<_Types...> &value) +{ + uint32_t index; + bool ret = GetValue(node, GET_NAME(type), index); + if (!ret) { + return ret; + } + + return Serializable::ReadVariant(node, name, 0, index, value); +} + +template +bool Serializable::WriteVariant(json &node, uint32_t step, const _InTp &input) +{ + return false; +} + +template +bool Serializable::ReadVariant(const json &node, const std::string &name, uint32_t step, uint32_t index, _OutTp &output) +{ + if (step == index) { + return Serializable::GetValue(node, name, std::get<_First>(output)); + } + return Serializable::ReadVariant<_OutTp, _Rest...>(node, name, step + 1, index, output); +} + +template +bool Serializable::WriteVariant(json &node, uint32_t step, const _InTp &input) +{ + if (step == input.index()) { + return Serializable::SetValue(node, std::get<_First>(input)); + } + return WriteVariant<_InTp, _Rest...>(node, step + 1, input); +} + +template +bool Serializable::ReadVariant(const json &node, const std::string &name, uint32_t step, uint32_t index, _OutTp &output) +{ + return false; +} } // namespace DistributedData } // namespace OHOS #endif // OHOS_DISTRIBUTED_DATA_FRAMEWORKS_COMMON_SERIALIZABLE_H diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 1d29c1ef..62ccc155 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -78,7 +78,12 @@ ohos_shared_library("distributeddatasvc") { "data_share/common/kv_delegate.cpp", "data_share/common/rdb_delegate.cpp", "data_share/common/seq_strategy.cpp", + "data_share/common/template_manager.cpp", "data_share/common/uri_utils.cpp", + "data_share/data/json_formatter.cpp", + "data_share/data/published_data.cpp", + "data_share/data/resultset_json_formatter.cpp", + "data_share/data/template_data.cpp", "data_share/data_share_obs_proxy.cpp", "data_share/data_share_service_impl.cpp", "data_share/data_share_service_stub.cpp", @@ -142,6 +147,7 @@ ohos_shared_library("distributeddatasvc") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", + "data_share/gaussdb_rd:gaussdb_rd", ] external_deps = [ diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp index 97e451bc..7008e5fd 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -34,15 +34,6 @@ std::shared_ptr KvDBDelegate::GetInstance(bool reInit, const std:: return delegate; } -bool KvData::Marshal(DistributedData::Serializable::json &node) const -{ - auto ret = SetValue(node, *GetId()); - if (HasVersion()) { - ret &= SetValue(node, GetVersion()); - } - return ret & SetValue(node, GetValue()); -} - bool Id::Marshal(DistributedData::Serializable::json &node) const { return SetValue(node[GET_NAME(_id)], _id); @@ -66,4 +57,11 @@ bool VersionData::Marshal(DistributedData::Serializable::json &node) const { return SetValue(node[GET_NAME(version)], version); } + +const std::string &KvData::GetId() const +{ + return id; +} + +KvData::KvData(const Id &id) : id(DistributedData::Serializable::Marshall(id)) {} } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.h b/services/distributeddataservice/service/data_share/common/db_delegate.h index e6ec2721..716c4f32 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.h +++ b/services/distributeddataservice/service/data_share/common/db_delegate.h @@ -46,11 +46,16 @@ struct RdbStoreContext { int version; }; -struct Id final: public DistributedData::Serializable { +class Id : public DistributedData::Serializable { +public: explicit Id(const std::string &id); ~Id() = default; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; + operator std::string() + { + return DistributedData::Serializable::Marshall(*this); + } private: std::string _id; @@ -61,12 +66,11 @@ public: explicit VersionData(int version); bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; - VersionData &operator=(int inputVersion) + virtual void SetVersion(int ver) { - version = inputVersion; - return *this; + version = ver; }; - operator int() + virtual int GetVersion() const { return version; }; @@ -75,12 +79,16 @@ private: int version; }; -struct KvData : public DistributedData::Serializable { - virtual std::shared_ptr GetId() const = 0; +class KvData { +public: + explicit KvData(const Id &id); + const std::string &GetId() const; virtual bool HasVersion() const = 0; - virtual VersionData GetVersion() const = 0; - virtual const Serializable &GetValue() const = 0; - bool Marshal(json &node) const override; + virtual int GetVersion() const = 0; + virtual std::string GetValue() const = 0; + +private: + std::string id; }; class KvDBDelegate { diff --git a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp index cbbb3fcd..f75a3d6d 100644 --- a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp @@ -16,17 +16,41 @@ #include "kv_delegate.h" #include "datashare_errno.h" #include "directory_manager.h" +#include "grd_document/grd_document_api.h" +#include "grd_base/grd_error.h" #include "ipc_skeleton.h" #include "log_print.h" namespace OHOS::DataShare { int64_t KvDelegate::Upsert(const std::string &collectionName, const std::string &filter, const std::string &value) { + std::lock_guard lock(mutex_); + if (!Init()) { + ZLOGE("init failed, %{public}s", collectionName.c_str()); + return E_ERROR; + } + int count = GRD_UpsertDoc(db_, collectionName.c_str(), filter.c_str(), value.c_str(), 0); + if (count <= 0) { + ZLOGE("GRD_UpSertDoc failed,status %{public}d", count); + return count; + } + Flush(); return E_OK; } int64_t KvDelegate::Delete(const std::string &collectionName, const std::string &filter) { + std::lock_guard lock(mutex_); + if (!Init()) { + ZLOGE("init failed, %{public}s", collectionName.c_str()); + return E_ERROR; + } + int count = GRD_DeleteDoc(db_, collectionName.c_str(), filter.c_str(), 0); + if (count <= 0) { + ZLOGE("GRD_UpSertDoc failed,status %{public}d", count); + return count; + } + Flush(); return E_OK; } @@ -35,24 +59,53 @@ bool KvDelegate::Init() if (isInitDone_) { return true; } + ZLOGE("hanlu init %{public}s", path_.c_str()); + int status = GRD_DBOpen((path_ + "/dataShare.db").c_str(), nullptr, GRD_DB_OPEN_CREATE, &db_); + if (status != GRD_OK || db_ == nullptr) { + ZLOGE("GRD_DBOpen failed,status %{public}d", status); + return false; + } + + status = GRD_CreateCollection(db_, TEMPLATE_TABLE, nullptr, 0); + if (status != GRD_OK) { + ZLOGE("GRD_CreateCollection template table failed,status %{public}d", status); + return false; + } + + status = GRD_CreateCollection(db_, DATA_TABLE, nullptr, 0); + if (status != GRD_OK) { + ZLOGE("GRD_CreateCollection data table failed,status %{public}d", status); + return false; + } isInitDone_ = true; return true; } +KvDelegate::~KvDelegate() +{ + std::lock_guard lock(mutex_); + if (isInitDone_) { + int status = GRD_DBClose(db_, 0); + if (status != GRD_OK) { + ZLOGE("GRD_DBClose failed,status %{public}d", status); + } + } +} + int32_t KvDelegate::Upsert(const std::string &collectionName, const KvData &value) { - std::string id = DistributedData::Serializable::Marshall(*value.GetId()); + std::string id = value.GetId(); if (value.HasVersion() && value.GetVersion() != 0) { int version = -1; if (GetVersion(collectionName, id, version)) { if (value.GetVersion() <= version) { ZLOGE("GetVersion failed,%{public}s id %{private}s %{public}d %{public}d", collectionName.c_str(), - id.c_str(), static_cast(value.GetVersion()), version); + id.c_str(), value.GetVersion(), version); return E_VERSION_NOT_NEWER; } } } - return Upsert(collectionName, id, DistributedData::Serializable::Marshall(value.GetValue())); + return Upsert(collectionName, id, value.GetValue()); } int32_t KvDelegate::DeleteById(const std::string &collectionName, const Id &id) @@ -82,23 +135,83 @@ bool KvDelegate::GetVersion(const std::string &collectionName, const std::string ZLOGE("Unmarshall failed,data %{public}s", value.c_str()); return false; } - version = data; + version = data.GetVersion(); return true; } int32_t KvDelegate::Get( const std::string &collectionName, const std::string &filter, const std::string &projection, std::string &result) { + std::lock_guard lock(mutex_); + if (!Init()) { + ZLOGE("init failed, %{public}s", collectionName.c_str()); + return E_ERROR; + } + Query query; + query.filter = filter.c_str(); + query.projection = projection.c_str(); + GRD_ResultSet *resultSet = nullptr; + int status = GRD_FindDoc(db_, collectionName.c_str(), query, 0, &resultSet); + if (status != GRD_OK || resultSet == nullptr) { + ZLOGE("GRD_FindDoc failed,status %{public}d", status); + return status; + } + status = GRD_Next(resultSet); + if (status != GRD_OK) { + GRD_FreeResultSet(resultSet); + ZLOGE("GRD_Next failed,status %{public}d", status); + return status; + } + char *value = nullptr; + status = GRD_GetValue(resultSet, &value); + if (status != GRD_OK || value == nullptr) { + GRD_FreeResultSet(resultSet); + ZLOGE("GRD_GetValue failed,status %{public}d", status); + return status; + } + result = value; + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); return E_OK; } void KvDelegate::Flush() { + int status = GRD_Flush(db_, GRD_DB_FLUSH_ASYNC); + if (status != GRD_OK) { + ZLOGE("GRD_Flush failed,status %{public}d", status); + } } int32_t KvDelegate::GetBatch(const std::string &collectionName, const std::string &filter, const std::string &projection, std::vector &result) { + std::lock_guard lock(mutex_); + if (!Init()) { + ZLOGE("init failed, %{public}s", collectionName.c_str()); + return E_ERROR; + } + Query query; + query.filter = filter.c_str(); + query.projection = projection.c_str(); + GRD_ResultSet *resultSet; + int status = GRD_FindDoc(db_, collectionName.c_str(), query, 0, &resultSet); + if (status != GRD_OK || resultSet == nullptr) { + ZLOGE("GRD_UpSertDoc failed,status %{public}d", status); + return status; + } + char *value = nullptr; + while (GRD_Next(resultSet) == GRD_OK) { + status = GRD_GetValue(resultSet, &value); + if (status != GRD_OK || value == nullptr) { + GRD_FreeResultSet(resultSet); + ZLOGE("GRD_GetValue failed,status %{public}d", status); + return status; + } + result.emplace_back(value); + GRD_FreeValue(value); + } + GRD_FreeResultSet(resultSet); return E_OK; } diff --git a/services/distributeddataservice/service/data_share/common/kv_delegate.h b/services/distributeddataservice/service/data_share/common/kv_delegate.h index fe111337..e490a249 100644 --- a/services/distributeddataservice/service/data_share/common/kv_delegate.h +++ b/services/distributeddataservice/service/data_share/common/kv_delegate.h @@ -20,11 +20,13 @@ #include #include "db_delegate.h" +#include "grd_base/grd_db_api.h" namespace OHOS::DataShare { class KvDelegate final : public KvDBDelegate { public: explicit KvDelegate(const std::string &path); + ~KvDelegate() override; int32_t Upsert(const std::string &collectionName, const KvData &value) override; int32_t DeleteById(const std::string &collectionName, const Id &id) override; int32_t Get(const std::string &collectionName, const Id &id, std::string &value) override; @@ -40,7 +42,9 @@ private: int64_t Upsert(const std::string &collectionName, const std::string &filter, const std::string &value); int64_t Delete(const std::string &collectionName, const std::string &filter); void Flush(); + std::mutex mutex_; std::string path_; + GRD_DB *db_ = nullptr; bool isInitDone_ = false; }; } // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index a5e6ad21..e4c9b2be 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -15,7 +15,9 @@ #define LOG_TAG "RdbAdaptor" #include "rdb_delegate.h" +#include "data/resultset_json_formatter.h" #include "log_print.h" +#include "rdb_utils.h" #include "utils/anonymous.h" namespace OHOS::DataShare { @@ -120,7 +122,8 @@ std::shared_ptr RdbDelegate::Query( ZLOGE("Query failed %{private}s", sql.c_str()); return nullptr; } - return nullptr; + + return std::make_shared(resultSet); } int RdbDelegate::ExecuteSql(const std::string &sql) diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp new file mode 100644 index 00000000..9a22d067 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -0,0 +1,480 @@ +/* + * Copyright (c) 2023 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 "TemplateManager" + +#include "template_manager.h" + +#include "db_delegate.h" +#include "json_formatter.h" +#include "log_print.h" +#include "published_data.h" +#include "template_data.h" +#include "uri_utils.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +bool TemplateManager::GetTemplate( + const std::string &uri, const int64_t subscriberId, const std::string &bundleName, Template &tpl) +{ + return TemplateData::Query(Id(TemplateData::GenId(uri, bundleName, subscriberId)), tpl) == E_OK; +} + +bool TemplateManager::AddTemplate(const std::string &uri, const TemplateId &tplId, const Template &tpl) +{ + auto delegate = KvDBDelegate::GetInstance(); + if (delegate == nullptr) { + ZLOGE("db open failed"); + return false; + } + TemplateData data(uri, tplId.bundleName_, tplId.subscriberId_, tpl); + auto status = delegate->Upsert(KvDBDelegate::TEMPLATE_TABLE, data); + if (status != E_OK) { + ZLOGE("db Upsert failed, %{public}d", status); + return false; + } + return true; +} + +bool TemplateManager::DelTemplate(const std::string &uri, const TemplateId &tplId) +{ + auto delegate = KvDBDelegate::GetInstance(); + if (delegate == nullptr) { + ZLOGE("db open failed"); + return false; + } + auto status = delegate->DeleteById( + KvDBDelegate::TEMPLATE_TABLE, Id(TemplateData::GenId(uri, tplId.bundleName_, tplId.subscriberId_))); + if (status != E_OK) { + ZLOGE("db DeleteById failed, %{public}d", status); + return false; + } + return true; +} + +Key::Key(const std::string &uri, const int64_t subscriberId, const std::string &bundleName) + : uri(uri), subscriberId(subscriberId), bundleName(bundleName) +{ +} + +bool Key::operator==(const Key &rhs) const +{ + return uri == rhs.uri && subscriberId == rhs.subscriberId && bundleName == rhs.bundleName; +} + +bool Key::operator!=(const Key &rhs) const +{ + return !(rhs == *this); +} +bool Key::operator<(const Key &rhs) const +{ + if (uri < rhs.uri) { + return true; + } + if (rhs.uri < uri) { + return false; + } + if (subscriberId < rhs.subscriberId) { + return true; + } + if (rhs.subscriberId < subscriberId) { + return false; + } + return bundleName < rhs.bundleName; +} +bool Key::operator>(const Key &rhs) const +{ + return rhs < *this; +} +bool Key::operator<=(const Key &rhs) const +{ + return !(rhs < *this); +} +bool Key::operator>=(const Key &rhs) const +{ + return !(*this < rhs); +} + +TemplateManager::TemplateManager() {} + +TemplateManager &TemplateManager::GetInstance() +{ + static TemplateManager manager; + return manager; +} + +RdbSubscriberManager &RdbSubscriberManager::GetInstance() +{ + static RdbSubscriberManager manager; + return manager; +} + +int RdbSubscriberManager::AddRdbSubscriber(const std::string &uri, const TemplateId &tplId, + const sptr observer, std::shared_ptr context) +{ + int result = E_OK; + Key key(uri, tplId.subscriberId_, tplId.bundleName_); + rdbCache_.Compute(key, [&observer, &context, &result, this](const auto &key, std::vector &value) { + ZLOGI("add subscriber, uri %{private}s tokenId %{public}d", key.uri.c_str(), context->callerTokenId); + std::vector node; + node.emplace_back(observer, context->callerTokenId); + result = Notify(key, node, context->calledSourceDir, context->version); + if (result != E_OK) { + return false; + } + value.emplace_back(observer, context->callerTokenId); + return true; + }); + return result; +} + +int RdbSubscriberManager::DelRdbSubscriber( + const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId) +{ + Key key(uri, tplId.subscriberId_, tplId.bundleName_); + auto result = + rdbCache_.ComputeIfPresent(key, [&callerTokenId, this](const auto &key, std::vector &value) { + ZLOGI("delete subscriber, uri %{private}s tokenId %{public}d", key.uri.c_str(), callerTokenId); + for (auto it = value.begin(); it != value.end();) { + if (it->callerTokenId == callerTokenId) { + ZLOGI("erase start"); + it = value.erase(it); + } else { + it++; + } + } + return !value.empty(); + }); + return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; +} + +int RdbSubscriberManager::DisableRdbSubscriber( + const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId) +{ + Key key(uri, tplId.subscriberId_, tplId.bundleName_); + auto result = + rdbCache_.ComputeIfPresent(key, [&callerTokenId, this](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end(); it++) { + if (it->callerTokenId == callerTokenId) { + it->enabled = false; + } + } + return true; + }); + return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; +} + +int RdbSubscriberManager::EnableRdbSubscriber( + const std::string &uri, const TemplateId &tplId, std::shared_ptr context) +{ + Key key(uri, tplId.subscriberId_, tplId.bundleName_); + auto result = rdbCache_.ComputeIfPresent(key, [&context, this](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end(); it++) { + if (it->callerTokenId == context->callerTokenId) { + it->enabled = true; + std::vector node; + node.emplace_back(it->observer, context->callerTokenId); + Notify(key, node, context->calledSourceDir, context->version); + } + } + return true; + }); + return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; +} + +void RdbSubscriberManager::Emit(const std::string &uri, std::shared_ptr context) +{ + if (!URIUtils::IsDataProxyURI(uri)) { + return; + } + rdbCache_.ForEach([&uri, &context, this](const Key &key, std::vector &val) { + if (key.uri != uri) { + return false; + } + Notify(key, val, context->calledSourceDir, context->version); + return false; + }); +} + +std::vector RdbSubscriberManager::GetKeysByUri(const std::string &uri) +{ + std::vector results; + rdbCache_.ForEach([&uri, &results](const Key &key, std::vector &val) { + if (key.uri != uri) { + return false; + } + results.emplace_back(key); + return false; + }); + return results; +} + +void RdbSubscriberManager::EmitByKey(const Key &key, const std::string &rdbPath, int version) +{ + if (!URIUtils::IsDataProxyURI(key.uri)) { + return; + } + rdbCache_.ComputeIfPresent(key, [&rdbPath, &version, this](const Key &key, std::vector &val) { + Notify(key, val, rdbPath, version); + return true; + }); +} + +int RdbSubscriberManager::GetObserverCount(const Key &key) +{ + auto pair = rdbCache_.Find(key); + if (!pair.first) { + return 0; + } + return pair.second.size(); +} + +int RdbSubscriberManager::GetEnableObserverCount(const Key &key) +{ + auto pair = rdbCache_.Find(key); + if (!pair.first) { + return 0; + } + int count = 0; + for (const auto &observer : pair.second) { + if (observer.enabled) { + count++; + } + } + return count; +} + +int RdbSubscriberManager::Notify( + const Key &key, std::vector &val, const std::string &rdbDir, int rdbVersion) +{ + Template tpl; + if (!TemplateManager::GetInstance().GetTemplate(key.uri, key.subscriberId, key.bundleName, tpl)) { + ZLOGE("template undefined, %{public}s, %{public}" PRId64 ", %{public}s", + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); + return E_TEMPLATE_NOT_EXIST; + } + auto delegate = DBDelegate::Create(rdbDir, rdbVersion); + if (delegate == nullptr) { + ZLOGE("Create fail %{public}s %{public}s", DistributedData::Anonymous::Change(key.uri).c_str(), + key.bundleName.c_str()); + return E_ERROR; + } + RdbChangeNode changeNode; + changeNode.uri_ = key.uri; + changeNode.templateId_.subscriberId_ = key.subscriberId; + changeNode.templateId_.bundleName_ = key.bundleName; + for (const auto &predicate : tpl.predicates_) { + JsonFormatter formatter(predicate.key_, delegate->Query(predicate.selectSql_)); + changeNode.data_.emplace_back(DistributedData::Serializable::Marshall(formatter)); + } + + ZLOGI("emit, size %{public}d %{private}s", val.size(), changeNode.uri_.c_str()); + for (auto &callback : val) { + if (callback.enabled && callback.observer != nullptr) { + callback.observer->OnChangeFromRdb(changeNode); + } + } + return E_OK; +} + +PublishedDataSubscriberManager &PublishedDataSubscriberManager::GetInstance() +{ + static PublishedDataSubscriberManager manager; + return manager; +} + +int PublishedDataSubscriberManager::AddSubscriber(const std::string &key, const std::string &callerBundleName, + const int64_t subscriberId, const sptr observer, const uint32_t callerTokenId) +{ + PublishedDataKey publishedDataKey(key, callerBundleName, subscriberId); + publishedDataCache.Compute(publishedDataKey, + [&observer, &callerTokenId, this](const PublishedDataKey &key, std::vector &value) { + ZLOGI("add publish subscriber, uri %{private}s tokenId %{public}d", key.key.c_str(), callerTokenId); + value.emplace_back(observer, callerTokenId); + return true; + }); + return E_OK; +} + +int PublishedDataSubscriberManager::DelSubscriber(const std::string &uri, const std::string &callerBundleName, + const int64_t subscriberId, const uint32_t callerTokenId) +{ + PublishedDataKey key(uri, callerBundleName, subscriberId); + auto result = + publishedDataCache.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end();) { + if (it->callerTokenId == callerTokenId) { + it = value.erase(it); + } else { + it++; + } + } + return !value.empty(); + }); + return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; +} + +int PublishedDataSubscriberManager::DisableSubscriber(const std::string &uri, const std::string &callerBundleName, + const int64_t subscriberId, const uint32_t callerTokenId) +{ + PublishedDataKey key(uri, callerBundleName, subscriberId); + auto result = + publishedDataCache.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end(); it++) { + if (it->callerTokenId == callerTokenId) { + it->enabled = false; + } + } + return true; + }); + return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; +} + +int PublishedDataSubscriberManager::EnableSubscriber(const std::string &uri, const std::string &callerBundleName, + const int64_t subscriberId, const uint32_t callerTokenId) +{ + PublishedDataKey key(uri, callerBundleName, subscriberId); + auto result = + publishedDataCache.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end(); it++) { + if (it->callerTokenId == callerTokenId) { + it->enabled = true; + } + } + return true; + }); + return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; +} + +void PublishedDataSubscriberManager::Emit(const std::vector &keys, + const std::string &ownerBundleName, const sptr observer) +{ + int32_t status; + // key is bundleName, value is change node + std::map, std::string>> publishedResult; + std::map, std::vector> callbacks; + publishedDataCache.ForEach([&keys, &status, &observer, &publishedResult, &callbacks, this]( + const PublishedDataKey &key, std::vector &val) { + for (auto &data : keys) { + if (key != data || publishedResult.count(key) != 0) { + continue; + } + status = PublishedData::Query( + Id(PublishedData::GenId(key.key, key.bundleName, key.subscriberId)), publishedResult[key]); + if (status != E_OK) { + ZLOGE("query fail %{public}s %{public}s %{public}" PRId64, data.bundleName.c_str(), data.key.c_str(), + data.subscriberId); + publishedResult.erase(key); + continue; + } + PutInto(callbacks, val, key, observer); + break; + } + return false; + }); + PublishedDataChangeNode result; + for (auto &[callback, keys] : callbacks) { + result.datas_.clear(); + for (auto &key : keys) { + if (publishedResult.count(key) != 0) { + result.datas_.emplace_back(key.key, key.subscriberId, publishedResult[key]); + } + } + if (result.datas_.empty()) { + continue; + } + result.ownerBundleName_ = ownerBundleName; + callback->OnChangeFromPublishedData(result); + } +} + +void PublishedDataSubscriberManager::PutInto( + std::map, std::vector> &callbacks, + std::vector &val, const PublishedDataKey &key, const sptr observer) +{ + for (auto &callback : val) { + if (callback.enabled && callback.observer != nullptr) { + // callback the observer, others do not call + if (observer != nullptr && callback.observer != observer) { + continue; + } + callbacks[callback.observer].emplace_back(key); + } + } +} + +PublishedDataKey::PublishedDataKey(const std::string &key, const std::string &bundle, const int64_t subscriberId) + : key(key), bundleName(bundle), subscriberId(subscriberId) +{ + /* private published data can use key as simple uri */ + /* etc: datashareproxy://{bundleName}/meeting can use meeting replaced */ + /* if key is normal uri, bundleName is from uri */ + if (URIUtils::IsDataProxyURI(key)) { + URIUtils::GetBundleNameFromProxyURI(key, bundleName); + } +} + +bool PublishedDataKey::operator<(const PublishedDataKey &rhs) const +{ + if (key < rhs.key) { + return true; + } + if (rhs.key < key) { + return false; + } + if (bundleName < rhs.bundleName) { + return true; + } + if (rhs.bundleName < bundleName) { + return false; + } + return subscriberId < rhs.subscriberId; +} + +bool PublishedDataKey::operator>(const PublishedDataKey &rhs) const +{ + return rhs < *this; +} + +bool PublishedDataKey::operator<=(const PublishedDataKey &rhs) const +{ + return !(rhs < *this); +} + +bool PublishedDataKey::operator>=(const PublishedDataKey &rhs) const +{ + return !(*this < rhs); +} + +bool PublishedDataKey::operator==(const PublishedDataKey &rhs) const +{ + return key == rhs.key && bundleName == rhs.bundleName && subscriberId == rhs.subscriberId; +} + +bool PublishedDataKey::operator!=(const PublishedDataKey &rhs) const +{ + return !(rhs == *this); +} + +RdbSubscriberManager::ObserverNode::ObserverNode(const sptr &observer, uint32_t callerTokenId) + : observer(observer), callerTokenId(callerTokenId) +{ +} + +PublishedDataSubscriberManager::ObserverNode::ObserverNode( + const sptr &observer, uint32_t callerTokenId) + : observer(observer), callerTokenId(callerTokenId) +{ +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/template_manager.h b/services/distributeddataservice/service/data_share/common/template_manager.h new file mode 100644 index 00000000..f6b4e5a5 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/template_manager.h @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2023 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 DATASHARESERVICE_TEMPLATE_MANAGER_H +#define DATASHARESERVICE_TEMPLATE_MANAGER_H + +#include +#include + +#include "concurrent_map.h" +#include "datashare_template.h" +#include "data_proxy_observer.h" +#include "context.h" +namespace OHOS::DataShare { +struct Key { + Key(const std::string &uri, const int64_t subscriberId, const std::string &bundleName); + bool operator==(const Key &rhs) const; + bool operator!=(const Key &rhs) const; + bool operator<(const Key &rhs) const; + bool operator>(const Key &rhs) const; + bool operator<=(const Key &rhs) const; + bool operator>=(const Key &rhs) const; + const std::string uri; + const int64_t subscriberId; + const std::string bundleName; +}; +class TemplateManager { +public: + static TemplateManager &GetInstance(); + bool AddTemplate(const std::string &uri, const TemplateId &tplId, const Template &tpl); + bool DelTemplate(const std::string &uri, const TemplateId &tplId); + bool GetTemplate(const std::string &uri, int64_t subscriberId, const std::string &bundleName, Template &tpl); + +private: + TemplateManager(); + friend class RdbSubscriberManager; +}; + +class RdbSubscriberManager { +public: + static RdbSubscriberManager &GetInstance(); + int AddRdbSubscriber(const std::string &uri, const TemplateId &tplId, const sptr observer, + std::shared_ptr context); + int DelRdbSubscriber(const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId); + int DisableRdbSubscriber( + const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId); + int EnableRdbSubscriber(const std::string &uri, const TemplateId &tplId, std::shared_ptr context); + void Emit(const std::string &uri, std::shared_ptr context); + void EmitByKey(const Key &key, const std::string &rdbPath, int version); + int GetObserverCount(const Key &key); + std::vector GetKeysByUri(const std::string &uri); + +private: + struct ObserverNode { + ObserverNode(const sptr &observer, uint32_t callerTokenId); + sptr observer; + uint32_t callerTokenId; + bool enabled = true; + }; + + RdbSubscriberManager() = default; + ConcurrentMap> rdbCache_; + int Notify(const Key &key, std::vector &val, const std::string &rdbDir, int rdbVersion); + int GetEnableObserverCount(const Key &key); +}; + +struct PublishedDataKey { + PublishedDataKey(const std::string &key, const std::string &bundleName, const int64_t subscriberId); + bool operator<(const PublishedDataKey &rhs) const; + bool operator>(const PublishedDataKey &rhs) const; + bool operator<=(const PublishedDataKey &rhs) const; + bool operator>=(const PublishedDataKey &rhs) const; + bool operator==(const PublishedDataKey &rhs) const; + bool operator!=(const PublishedDataKey &rhs) const; + std::string key; + std::string bundleName; + int64_t subscriberId; +}; + +class PublishedDataSubscriberManager { +public: + static PublishedDataSubscriberManager &GetInstance(); + int AddSubscriber(const std::string &key, const std::string &callerBundleName, const int64_t subscriberId, + const sptr observer, const uint32_t callerTokenId); + int DelSubscriber(const std::string &uri, const std::string &callerBundleName, const int64_t subscriberId, + const uint32_t callerTokenId); + int DisableSubscriber(const std::string &uri, const std::string &callerBundleName, const int64_t subscriberId, + const uint32_t callerTokenId); + int EnableSubscriber(const std::string &uri, const std::string &callerBundleName, const int64_t subscriberId, + const uint32_t callerTokenId); + void Emit(const std::vector &keys, const std::string &ownerBundleName, + const sptr observer = nullptr); +private: + struct ObserverNode { + ObserverNode(const sptr &observer, uint32_t callerTokenId); + sptr observer; + uint32_t callerTokenId; + bool enabled = true; + }; + PublishedDataSubscriberManager() = default; + void PutInto(std::map, std::vector> &, + std::vector &, const PublishedDataKey &, const sptr); + ConcurrentMap> publishedDataCache; +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/common/uri_utils.h b/services/distributeddataservice/service/data_share/common/uri_utils.h index 0bf62d16..17ee8698 100644 --- a/services/distributeddataservice/service/data_share/common/uri_utils.h +++ b/services/distributeddataservice/service/data_share/common/uri_utils.h @@ -33,7 +33,7 @@ public: static bool IsDataProxyURI(const std::string &uri); static constexpr const char *DATA_SHARE_SCHEMA = "datashare:///";; static constexpr const char DATA_PROXY_SCHEMA[] = "datashareproxy://"; - static constexpr int DATA_PROXY_SCHEMA_LEN = sizeof(DATA_PROXY_SCHEMA); + static constexpr int DATA_PROXY_SCHEMA_LEN = sizeof(DATA_PROXY_SCHEMA) - 1; private: enum PATH_PARAM : int32_t { diff --git a/services/distributeddataservice/service/data_share/data/json_formatter.cpp b/services/distributeddataservice/service/data_share/data/json_formatter.cpp new file mode 100644 index 00000000..db190824 --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/json_formatter.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 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 "JsonFormatter" +#include "json_formatter.h" + +#include "log_print.h" + +namespace OHOS::DataShare { +bool JsonFormatter::Marshal(json &node) const +{ + if (value_ == nullptr) { + ZLOGE("null value %{public}s", key_.c_str()); + return false; + } + return SetValue(node[key_], *value_); +} + +bool JsonFormatter::Unmarshal(const json &node) +{ + if (value_ == nullptr) { + ZLOGE("null value %{public}s", key_.c_str()); + return false; + } + return GetValue(node, key_, *value_); +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/json_formatter.h b/services/distributeddataservice/service/data_share/data/json_formatter.h new file mode 100644 index 00000000..7c41e197 --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/json_formatter.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 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 DATASHARESERVICE_JSON_FORMATTER_H +#define DATASHARESERVICE_JSON_FORMATTER_H + +#include "serializable/serializable.h" + +namespace OHOS::DataShare { +class JsonFormatter : public DistributedData::Serializable { +public: + JsonFormatter(const std::string &key, const std::shared_ptr &value) + : key_(key), value_(value) + { + } + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + +private: + std::string key_; + std::shared_ptr value_; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/data/published_data.cpp b/services/distributeddataservice/service/data_share/data/published_data.cpp new file mode 100644 index 00000000..4dd329be --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/published_data.cpp @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2023 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 "PublishedData" +#include "published_data.h" + +#include "log_print.h" + +namespace OHOS::DataShare { +bool PublishedData::HasVersion() const +{ + return true; +} + +int PublishedData::GetVersion() const +{ + return value.GetVersion(); +} + +std::string PublishedData::GetValue() const +{ + return DistributedData::Serializable::Marshall(value); +} + +PublishedData::PublishedData(const std::string &key, const std::string &bundleName, int64_t subscriberId, + const std::variant, std::string> &inputValue, const int version) + : KvData(Id(GenId(key, bundleName, subscriberId))), + value(key, bundleName, subscriberId, inputValue) +{ + value.SetVersion(version); +} +/* +std::vector PublishedData::Query(const std::string &bundleName) +{ + auto delegate = KvDBDelegate::GetInstance(); + if (delegate == nullptr) { + ZLOGE("db open failed"); + return std::vector(); + } + std::vector queryResults; + json filter; + filter["bundleName"] = bundleName; + int32_t status = delegate->GetBatch(KvDBDelegate::DATA_TABLE, filter.dump(), "{}", queryResults); + if (status != E_OK) { + ZLOGE("db Upsert failed, %{public}s %{public}d", bundleName.c_str(), status); + return std::vector(); + } + std::vector results; + for (auto &result : queryResults) { + PublishedData data; + if (data.Unmarshall(result)) { + results.push_back(std::move(data)); + } + } + return results; +} */ + +bool PublishedDataNode::Marshal(DistributedData::Serializable::json &node) const +{ + bool ret = SetValue(node[GET_NAME(key)], key); + ret = ret && SetValue(node[GET_NAME(bundleName)], bundleName); + ret = ret && SetValue(node[GET_NAME(subscriberId)], subscriberId); + ret = ret && SetValue(node[GET_NAME(value)], value); + ret = ret && SetValue(node[GET_NAME(timestamp)], timestamp); + return ret && VersionData::Marshal(node); +} + +bool PublishedDataNode::Unmarshal(const DistributedData::Serializable::json &node) +{ + bool ret = GetValue(node, GET_NAME(key), key); + ret = ret && GetValue(node, GET_NAME(bundleName), bundleName); + ret = ret && GetValue(node, GET_NAME(subscriberId), subscriberId); + ret = ret && GetValue(node, GET_NAME(value), value); + ret = ret && GetValue(node, GET_NAME(timestamp), timestamp); + return ret && VersionData::Unmarshal(node); +} + +PublishedDataNode::PublishedDataNode(const std::string &key, const std::string &bundleName, + int64_t subscriberId, const std::variant, std::string> &value) + : VersionData(-1), key(key), bundleName(bundleName), subscriberId(subscriberId), value(std::move(value)) +{ + auto now = time(nullptr); + if (now > 0) { + timestamp = now; + } +} + +PublishedDataNode::PublishedDataNode() : VersionData(-1) {} + +int32_t PublishedData::Query(const std::string &filter, std::variant, std::string> &publishedData) +{ + auto delegate = KvDBDelegate::GetInstance(); + if (delegate == nullptr) { + ZLOGE("db open failed"); + return E_ERROR; + } + std::string queryResult; + int32_t status = delegate->Get(KvDBDelegate::DATA_TABLE, filter, "{}", queryResult); + if (status != E_OK) { + ZLOGE("db Get failed, %{public}s %{public}d", filter.c_str(), status); + return status; + } + PublishedDataNode data; + if (!PublishedDataNode::Unmarshall(queryResult, data)) { + ZLOGE("Unmarshall failed, %{private}s", queryResult.c_str()); + return E_ERROR; + } + publishedData = std::move(data.value); + return E_OK; +} +std::string PublishedData::GenId(const std::string &key, const std::string &bundleName, int64_t subscriberId) +{ + return key + "_" + std::to_string(subscriberId) + "_" + bundleName; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/published_data.h b/services/distributeddataservice/service/data_share/data/published_data.h new file mode 100644 index 00000000..daa7d667 --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/published_data.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 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 DATASHARESERVICE_PUBLISHED_DATA_H +#define DATASHARESERVICE_PUBLISHED_DATA_H + +#include "db_delegate.h" +#include "serializable/serializable.h" + +namespace OHOS::DataShare { +class PublishedDataNode final : public VersionData { +public: + PublishedDataNode(); + PublishedDataNode(const std::string &key, const std::string &bundleName, int64_t subscriberId, + const std::variant, std::string> &value); + ~PublishedDataNode() = default; + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + std::string key; + std::string bundleName; + int64_t subscriberId; + std::variant, std::string> value; + std::time_t timestamp = 0; +}; + +class PublishedData final : public KvData { +public: + // static std::vector Query(const std::string &bundleName); + static int32_t Query(const std::string &filter, std::variant, std::string> &publishedData); + static std::string GenId(const std::string &key, const std::string &bundleName, int64_t subscriberId); + PublishedData(const std::string &key, const std::string &bundleName, int64_t subscriberId, + const std::variant, std::string> &value, const int version); + ~PublishedData() = default; + bool HasVersion() const override; + int GetVersion() const override; + std::string GetValue() const override; + +private: + PublishedDataNode value; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp new file mode 100644 index 00000000..931a4d54 --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 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 "ResultSetJsonFormatter" +#include "resultset_json_formatter.h" + +#include "rdb_errno.h" +#include "log_print.h" + +namespace OHOS::DataShare { +bool ResultSetJsonFormatter::Marshal(json &node) const +{ + int columnCount = 0; + auto result = resultSet->GetColumnCount(columnCount); + if (result != NativeRdb::E_OK) { + ZLOGE("GetColumnCount err, %{public}d", result); + return false; + } + while (resultSet->GoToNextRow() == NativeRdb::E_OK) { + json result; + for (int i = 0; i < columnCount; i++) { + std::string columnName; + std::string value; + resultSet->GetColumnName(i, columnName); + resultSet->GetString(i, value); + SetValue(result[columnName], value); + } + node.push_back(result); + } + return true; +} + +bool ResultSetJsonFormatter::Unmarshal(const DistributedData::Serializable::json &node) +{ + return false; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h new file mode 100644 index 00000000..0dfcabdf --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 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 DATASHARESERVICE_RESULTSET_JSON_FORMATTER_H +#define DATASHARESERVICE_RESULTSET_JSON_FORMATTER_H + +#include "datashare_template.h" +#include "rdb_utils.h" +#include "serializable/serializable.h" + +namespace OHOS::DataShare { +class ResultSetJsonFormatter final : public DistributedData::Serializable { +public: + explicit ResultSetJsonFormatter(const std::shared_ptr &resultSet) : resultSet(resultSet) {} + ~ResultSetJsonFormatter() {} + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + +private: + std::shared_ptr resultSet; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_RESULTSET_JSON_FORMATTER_H diff --git a/services/distributeddataservice/service/data_share/data/template_data.cpp b/services/distributeddataservice/service/data_share/data/template_data.cpp new file mode 100644 index 00000000..ca52ba1a --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/template_data.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2023 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 "TemplateData" +#include "template_data.h" +#include "log_print.h" +namespace OHOS::DataShare { +bool TemplateNode::Marshal(DistributedData::Serializable::json &node) const +{ + bool ret = SetValue(node[GET_NAME(predicates)], predicates); + ret = ret && SetValue(node[GET_NAME(scheduler)], scheduler); + return ret; +} + +bool TemplateNode::Unmarshal(const DistributedData::Serializable::json &node) +{ + bool ret = GetValue(node, GET_NAME(predicates), predicates); + return ret && GetValue(node, GET_NAME(scheduler), scheduler); +} + +TemplateNode::TemplateNode(const Template &tpl) : scheduler(tpl.scheduler_) +{ + for (auto &item:tpl.predicates_) { + predicates.emplace_back(item.key_, item.selectSql_); + } +} + +Template TemplateNode::ToTemplate() const +{ + std::vector nodes; + for (const auto &predicate: predicates) { + nodes.emplace_back(predicate.key, predicate.selectSql); + } + return Template(nodes, scheduler); +} + +bool TemplateRootNode::Marshal(DistributedData::Serializable::json &node) const +{ + bool ret = SetValue(node[GET_NAME(uri)], uri); + ret = ret && SetValue(node[GET_NAME(bundleName)], bundleName); + ret = ret && SetValue(node[GET_NAME(subscriberId)], subscriberId); + ret = ret && SetValue(node[GET_NAME(templat)], tpl); + return ret; +} + +bool TemplateRootNode::Unmarshal(const DistributedData::Serializable::json &node) +{ + bool ret = GetValue(node, GET_NAME(uri), uri); + ret = ret && GetValue(node, GET_NAME(bundleName), bundleName); + ret = ret && GetValue(node, GET_NAME(subscriberId), subscriberId); + ret = ret && GetValue(node, GET_NAME(templat), tpl); + return ret; +} + +TemplateRootNode::TemplateRootNode( + const std::string &uri, const std::string &bundleName, int64_t subscriberId, const Template &tpl) + : uri(uri), bundleName(bundleName), subscriberId(subscriberId), tpl(tpl) +{ +} + +bool TemplateData::HasVersion() const +{ + return false; +} + +std::string TemplateData::GetValue() const +{ + return DistributedData::Serializable::Marshall(value); +} + +TemplateData::TemplateData( + const std::string &uri, const std::string &bundleName, int64_t subscriberId, const Template &tpl) + :KvData(Id(GenId(uri, bundleName, subscriberId))), value(uri, bundleName, subscriberId, tpl) +{ +} + +int TemplateData::GetVersion() const +{ + return 0; +} + +std::string TemplateData::GenId(const std::string &uri, const std::string &bundleName, int64_t subscriberId) +{ + return uri + "_" + std::to_string(subscriberId) + "_" + bundleName; +} + +int32_t TemplateData::Query(const std::string &filter, Template &aTemplate) +{ + auto delegate = KvDBDelegate::GetInstance(); + if (delegate == nullptr) { + ZLOGE("db open failed"); + return E_ERROR; + } + std::string queryResult; + int32_t status = delegate->Get(KvDBDelegate::TEMPLATE_TABLE, filter, "{}", queryResult); + if (status != E_OK) { + ZLOGE("db Get failed, %{public}s %{public}d", filter.c_str(), status); + return status; + } + TemplateRootNode data; + if (!DistributedData::Serializable::Unmarshall(queryResult, data)) { + ZLOGE("Unmarshall failed, %{private}s", queryResult.c_str()); + return E_ERROR; + } + aTemplate = data.ToTemplate(); + return E_OK; +} + +Template TemplateRootNode::ToTemplate() const +{ + return tpl.ToTemplate(); +} + +PredicatesNode::PredicatesNode(const std::string &key, const std::string &selectSql) : key(key), selectSql(selectSql) +{ +} +bool PredicatesNode::Marshal(DistributedData::Serializable::json &node) const +{ + bool ret = SetValue(node[GET_NAME(key)], key); + ret = ret && SetValue(node[GET_NAME(selectSql)], selectSql); + return ret; +} +bool PredicatesNode::Unmarshal(const DistributedData::Serializable::json &node) +{ + bool ret = GetValue(node, GET_NAME(key), key); + ret = ret && GetValue(node, GET_NAME(selectSql), selectSql); + return ret; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/template_data.h b/services/distributeddataservice/service/data_share/data/template_data.h new file mode 100644 index 00000000..2de90d8f --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/template_data.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 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 DATASHARESERVICE_TEMPLATE_DATA_H +#define DATASHARESERVICE_TEMPLATE_DATA_H + +#include "datashare_template.h" +#include "db_delegate.h" +#include "serializable/serializable.h" + +namespace OHOS::DataShare { +struct PredicatesNode final: public DistributedData::Serializable { + PredicatesNode() = default; + PredicatesNode(const std::string &key, const std::string &selectSql); + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + std::string key; + std::string selectSql; +}; +struct TemplateNode final: public DistributedData::Serializable { + TemplateNode() = default; + explicit TemplateNode(const Template &tpl); + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + Template ToTemplate() const; +private: + std::vector predicates; + std::string scheduler; +}; + +struct TemplateRootNode final: public DistributedData::Serializable { + TemplateRootNode() = default; + TemplateRootNode(const std::string &uri, const std::string &bundleName, int64_t subscriberId, const Template &tpl); + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + Template ToTemplate() const; +private: + std::string uri; + std::string bundleName; + int64_t subscriberId; + TemplateNode tpl; +}; + +struct TemplateData final : public KvData { + TemplateData(const std::string &uri, const std::string &bundleName, int64_t subscriberId, const Template &tpl); + static int32_t Query(const std::string &filter, Template &aTemplate); + static std::string GenId(const std::string &uri, const std::string &bundleName, int64_t subscriberId); + bool HasVersion() const override; + int GetVersion() const override; + std::string GetValue() const override; +private: + TemplateRootNode value; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 833de07f..6c198e7b 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -20,6 +20,7 @@ #include "accesstoken_kit.h" #include "account/account_delegate.h" #include "bootstrap.h" +#include "common/kv_delegate.h" #include "dataobs_mgr_client.h" #include "datashare_errno.h" #include "datashare_template.h" @@ -33,6 +34,7 @@ #include "publish_strategy.h" #include "query_strategy.h" #include "subscribe_strategy.h" +#include "template_manager.h" #include "update_strategy.h" #include "utils/anonymous.h" @@ -55,6 +57,7 @@ int32_t DataShareServiceImpl::Insert(const std::string &uri, const DataShareValu auto ret = InsertStrategy::Execute(context, valuesBucket); if (ret) { NotifyChange(uri); + RdbSubscriberManager::GetInstance().Emit(uri, context); return ret; } return ret; @@ -84,6 +87,7 @@ int32_t DataShareServiceImpl::Update(const std::string &uri, const DataSharePred auto ret = UpdateStrategy::Execute(context, predicate, valuesBucket); if (ret) { NotifyChange(uri); + RdbSubscriberManager::GetInstance().Emit(uri, context); return ret; } return ret; @@ -96,6 +100,7 @@ int32_t DataShareServiceImpl::Delete(const std::string &uri, const DataSharePred auto ret = DeleteStrategy::Execute(context, predicate); if (ret) { NotifyChange(uri); + RdbSubscriberManager::GetInstance().Emit(uri, context); return ret; } return ret; @@ -117,7 +122,7 @@ int32_t DataShareServiceImpl::AddTemplate(const std::string &uri, const int64_t ZLOGE("get bundleName error, %{public}s", DistributedData::Anonymous::Change(uri).c_str()); return ERROR; } - return ERROR; + return TemplateManager::GetInstance().AddTemplate(uri, tpltId, tplt); } int32_t DataShareServiceImpl::DelTemplate(const std::string &uri, const int64_t subscriberId) @@ -128,7 +133,7 @@ int32_t DataShareServiceImpl::DelTemplate(const std::string &uri, const int64_t ZLOGE("get bundleName error, %{public}s", DistributedData::Anonymous::Change(uri).c_str()); return ERROR; } - return ERROR; + return TemplateManager::GetInstance().DelTemplate(uri, tpltId); } bool DataShareServiceImpl::GetCallerBundleName(std::string &bundleName) @@ -149,18 +154,48 @@ bool DataShareServiceImpl::GetCallerBundleName(std::string &bundleName) std::vector DataShareServiceImpl::Publish(const Data &data, const std::string &bundleNameOfProvider) { - return std::vector(); + std::vector results; + std::vector publishedData; + std::string callerBundleName; + GetCallerBundleName(callerBundleName); + for (const auto &item : data.datas_) { + auto context = std::make_shared(item.key_); + context->version = data.version_; + context->callerBundleName = callerBundleName; + context->calledBundleName = bundleNameOfProvider; + int32_t result = publishStrategy_.Execute(context, item); + results.emplace_back(item.key_, result); + if (result != EOK) { + ZLOGE("publish error, key is %{public}s", DistributedData::Anonymous::Change(item.key_).c_str()); + continue; + } + publishedData.emplace_back(context->uri, callerBundleName, item.subscriberId_); + } + PublishedDataSubscriberManager::GetInstance().Emit(publishedData, callerBundleName); + return results; } Data DataShareServiceImpl::GetData(const std::string &bundleNameOfProvider) { - return Data(); + std::string callerBundleName; + GetCallerBundleName(callerBundleName); + auto context = std::make_shared(); + context->callerBundleName = callerBundleName; + context->calledBundleName = bundleNameOfProvider; + return GetDataStrategy::Execute(context); } std::vector DataShareServiceImpl::SubscribeRdbData( const std::vector &uris, const TemplateId &id, const sptr observer) { - return std::vector(); + std::vector results; + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + results.emplace_back(uri, SubscribeStrategy::Execute(context, [&id, &observer, &context]() -> bool { + return RdbSubscriberManager::GetInstance().AddRdbSubscriber(context->uri, id, observer, context); + })); + } + return results; } std::vector DataShareServiceImpl::UnsubscribeRdbData( @@ -170,7 +205,7 @@ std::vector DataShareServiceImpl::UnsubscribeRdbData( for (const auto &uri : uris) { auto context = std::make_shared(uri); results.emplace_back(uri, SubscribeStrategy::Execute(context, [&id, &context]() -> bool { - return true; + return RdbSubscriberManager::GetInstance().DelRdbSubscriber(context->uri, id, context->callerTokenId); })); } return results; @@ -183,7 +218,7 @@ std::vector DataShareServiceImpl::EnableRdbSubs( for (const auto &uri : uris) { auto context = std::make_shared(uri); results.emplace_back(uri, SubscribeStrategy::Execute(context, [&id, &context]() -> bool { - return true; + return RdbSubscriberManager::GetInstance().EnableRdbSubscriber(context->uri, id, context); })); } return results; @@ -196,7 +231,7 @@ std::vector DataShareServiceImpl::DisableRdbSubs( for (const auto &uri : uris) { auto context = std::make_shared(uri); results.emplace_back(uri, SubscribeStrategy::Execute(context, [&id, &context]() -> bool { - return true; + return RdbSubscriberManager::GetInstance().DisableRdbSubscriber(context->uri, id, context->callerTokenId); })); } return results; @@ -205,25 +240,94 @@ std::vector DataShareServiceImpl::DisableRdbSubs( std::vector DataShareServiceImpl::SubscribePublishedData(const std::vector &uris, const int64_t subscriberId, const sptr observer) { - return std::vector(); + std::vector results; + std::string callerBundleName; + GetCallerBundleName(callerBundleName); + std::vector publishedKeys; + int32_t result; + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + PublishedDataKey key(uri, callerBundleName, subscriberId); + context->callerBundleName = callerBundleName; + context->calledBundleName = key.bundleName; + result = SubscribeStrategy::Execute( + context, [&subscriberId, &observer, &callerBundleName, &context]() -> bool { + return PublishedDataSubscriberManager::GetInstance().AddSubscriber( + context->uri, callerBundleName, subscriberId, observer, context->callerTokenId); + }); + results.emplace_back(uri, result); + if (result == E_OK) { + publishedKeys.emplace_back(key); + } + } + PublishedDataSubscriberManager::GetInstance().Emit(publishedKeys, callerBundleName, observer); + return results; } std::vector DataShareServiceImpl::UnsubscribePublishedData(const std::vector &uris, const int64_t subscriberId) { - return std::vector(); + std::vector results; + std::string callerBundleName; + GetCallerBundleName(callerBundleName); + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + PublishedDataKey key(uri, callerBundleName, subscriberId); + context->callerBundleName = callerBundleName; + context->calledBundleName = key.bundleName; + results.emplace_back( + uri, SubscribeStrategy::Execute(context, [&subscriberId, &callerBundleName, &context]() -> bool { + return PublishedDataSubscriberManager::GetInstance().DelSubscriber( + context->uri, callerBundleName, subscriberId, context->callerTokenId); + })); + } + return results; } std::vector DataShareServiceImpl::EnablePubSubs(const std::vector &uris, const int64_t subscriberId) { - return std::vector(); + std::vector results; + std::string callerBundleName; + GetCallerBundleName(callerBundleName); + std::vector publishedKeys; + int32_t result; + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + PublishedDataKey key(uri, callerBundleName, subscriberId); + context->callerBundleName = callerBundleName; + context->calledBundleName = key.bundleName; + result = SubscribeStrategy::Execute(context, [&subscriberId, &callerBundleName, &context]() -> bool { + return PublishedDataSubscriberManager::GetInstance().EnableSubscriber( + context->uri, callerBundleName, subscriberId, context->callerTokenId); + }); + results.emplace_back(uri, result); + if (result == E_OK) { + publishedKeys.emplace_back(key); + } + } + PublishedDataSubscriberManager::GetInstance().Emit(publishedKeys, callerBundleName); + return results; } std::vector DataShareServiceImpl::DisablePubSubs(const std::vector &uris, const int64_t subscriberId) { - return std::vector(); + std::vector results; + std::string callerBundleName; + GetCallerBundleName(callerBundleName); + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + PublishedDataKey key(uri, callerBundleName, subscriberId); + context->callerBundleName = callerBundleName; + context->calledBundleName = key.bundleName; + results.emplace_back( + uri, SubscribeStrategy::Execute(context, [&subscriberId, &callerBundleName, &context]() -> bool { + return PublishedDataSubscriberManager::GetInstance().DisableSubscriber( + context->uri, callerBundleName, subscriberId, context->callerTokenId); + })); + } + return results; } enum DataShareKvStoreType : int32_t { @@ -252,6 +356,7 @@ int32_t DataShareServiceImpl::OnInitialize() saveMeta.uid = IPCSkeleton::GetCallingUid(); saveMeta.storeType = DATA_SHARE_SINGLE_VERSION; saveMeta.dataDir = DistributedData::DirectoryManager::GetInstance().GetStorePath(saveMeta); + ZLOGE("hanlu init %{public}s", saveMeta.dataDir.c_str()); KvDBDelegate::GetInstance(false, saveMeta.dataDir); return EOK; } @@ -276,7 +381,8 @@ int32_t DataShareServiceImpl::OnUserChange(uint32_t code, const std::string &use saveMeta.uid = IPCSkeleton::GetCallingUid(); saveMeta.storeType = DATA_SHARE_SINGLE_VERSION; saveMeta.dataDir = DistributedData::DirectoryManager::GetInstance().GetStorePath(saveMeta); - KvDBDelegate::GetInstance(true, saveMeta.dataDir); + ZLOGE("hanlu init2 %{public}s", saveMeta.dataDir.c_str()); + KvDBDelegate::GetInstance(false, saveMeta.dataDir); return EOK; } } // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.h b/services/distributeddataservice/service/data_share/data_share_service_impl.h index 6cf14030..6ae6a0ae 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -18,11 +18,13 @@ #include +#include "data_proxy_observer.h" #include "data_share_service_stub.h" #include "datashare_template.h" -#include "data_proxy_observer.h" +#include "db_delegate.h" #include "uri_utils.h" #include "visibility.h" +#include "publish_strategy.h" namespace OHOS::DataShare { class API_EXPORT DataShareServiceImpl : public DataShareServiceStub { @@ -64,15 +66,11 @@ private: ~Factory(); }; - enum class PermissionType { - READ_PERMISSION = 1, - WRITE_PERMISSION - }; - bool NotifyChange(const std::string &uri); bool GetCallerBundleName(std::string &bundleName); static Factory factory_; static constexpr int32_t ERROR = -1; + PublishStrategy publishStrategy_; }; } // namespace OHOS::DataShare #endif diff --git a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.h b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.h index 062f2a79..9200ad9b 100644 --- a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.h +++ b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.h @@ -19,6 +19,8 @@ #include #include "data_proxy_observer.h" +#include "datashare_template.h" +#include "published_data.h" #include "seq_strategy.h" namespace OHOS::DataShare { diff --git a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp index d658954c..db4a1c63 100644 --- a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp @@ -20,6 +20,7 @@ #include "general/load_config_common_strategy.h" #include "general/permission_strategy.h" #include "log_print.h" +#include "published_data.h" #include "utils/anonymous.h" namespace OHOS::DataShare { @@ -30,33 +31,44 @@ int32_t PublishStrategy::Execute(std::shared_ptr context, const Publish ZLOGE("get strategy fail, maybe memory not enough"); return -1; } + if (!(*preProcess)(context)) { ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); return -1; } - return 0; + auto delegate = KvDBDelegate::GetInstance(); + if (delegate == nullptr) { + ZLOGE("db open failed"); + return -1; + } + PublishedData data(context->uri, context->calledBundleName, item.subscriberId_, item.GetData(), context->version); + int32_t status = delegate->Upsert(KvDBDelegate::DATA_TABLE, data); + if (status != E_OK) { + ZLOGE("db Upsert failed, %{public}s %{public}s %{public}d", context->calledBundleName.c_str(), + DistributedData::Anonymous::Change(context->uri).c_str(), status); + return -1; + } + return E_OK; } Strategy *PublishStrategy::GetStrategy() { - static std::mutex mutex; - static SeqStrategy strategies; - std::lock_guard lock(mutex); - if (!strategies.IsEmpty()) { - return &strategies; + std::lock_guard lock(mutex_); + if (!strategies_.IsEmpty()) { + return &strategies_; } std::initializer_list list = { new (std::nothrow) LoadConfigCommonStrategy(), new (std::nothrow) LoadConfigFromDataProxyNodeStrategy(), new (std::nothrow) PermissionStrategy() }; - auto ret = strategies.Init(list); + auto ret = strategies_.Init(list); if (!ret) { std::for_each(list.begin(), list.end(), [](Strategy *item) { delete item; }); return nullptr; } - return &strategies; + return &strategies_; } } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/publish_strategy.h b/services/distributeddataservice/service/data_share/strategies/publish_strategy.h index 70acdf7c..c518f722 100644 --- a/services/distributeddataservice/service/data_share/strategies/publish_strategy.h +++ b/services/distributeddataservice/service/data_share/strategies/publish_strategy.h @@ -24,10 +24,12 @@ namespace OHOS::DataShare { class PublishStrategy final { public: - static int32_t Execute(std::shared_ptr context, const PublishedDataItem &item); + int32_t Execute(std::shared_ptr context, const PublishedDataItem &item); private: - static Strategy *GetStrategy(); + Strategy *GetStrategy(); + std::mutex mutex_; + SeqStrategy strategies_; }; } // namespace OHOS::DataShare #endif diff --git a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp index 3835435c..cdad8087 100644 --- a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp @@ -31,6 +31,7 @@ int32_t SubscribeStrategy::Execute(std::shared_ptr context, std::functi ZLOGE("get strategy fail, maybe memory not enough"); return -1; } + context->isRead = true; if (!(*preProcess)(context)) { ZLOGE("pre process fail, uri_: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); return context->errCode; -- Gitee From c894e900a386673fbac564832941a95311fe2246 Mon Sep 17 00:00:00 2001 From: niudongyao Date: Sat, 13 May 2023 18:20:56 +0800 Subject: [PATCH 28/95] add scheduler Signed-off-by: niudongyao --- .../distributeddataservice/service/BUILD.gn | 1 + .../service/data_share/common/db_delegate.cpp | 4 +- .../service/data_share/common/db_delegate.h | 11 +- .../data_share/common/rdb_delegate.cpp | 27 ++- .../service/data_share/common/rdb_delegate.h | 14 +- .../data_share/common/scheduler_manager.cpp | 164 ++++++++++++++++++ .../data_share/common/scheduler_manager.h | 49 ++++++ .../data_share/common/template_manager.cpp | 16 +- .../data_share/data_share_service_impl.cpp | 6 +- 9 files changed, 272 insertions(+), 20 deletions(-) create mode 100644 services/distributeddataservice/service/data_share/common/scheduler_manager.cpp create mode 100644 services/distributeddataservice/service/data_share/common/scheduler_manager.h diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 62ccc155..cd798b5c 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -77,6 +77,7 @@ ohos_shared_library("distributeddatasvc") { "data_share/common/div_strategy.cpp", "data_share/common/kv_delegate.cpp", "data_share/common/rdb_delegate.cpp", + "data_share/common/scheduler_manager.cpp", "data_share/common/seq_strategy.cpp", "data_share/common/template_manager.cpp", "data_share/common/uri_utils.cpp", diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp index 7008e5fd..fb80825d 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -18,9 +18,9 @@ #include "kv_delegate.h" #include "rdb_delegate.h" namespace OHOS::DataShare { -std::shared_ptr DBDelegate::Create(const std::string &dir, int version) +std::shared_ptr DBDelegate::Create(const std::string &dir, int version, bool registerFunction) { - return std::make_shared(dir, version); + return std::make_shared(dir, version, registerFunction); } std::shared_ptr KvDBDelegate::GetInstance(bool reInit, const std::string &dir) diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.h b/services/distributeddataservice/service/data_share/common/db_delegate.h index 716c4f32..b28b5a58 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.h +++ b/services/distributeddataservice/service/data_share/common/db_delegate.h @@ -18,6 +18,7 @@ #include +#include "abs_shared_result_set.h" #include "concurrent_map.h" #include "datashare_predicates.h" #include "datashare_result_set.h" @@ -28,7 +29,7 @@ namespace OHOS::DataShare { class DBDelegate { public: - static std::shared_ptr Create(const std::string &dir, int version); + static std::shared_ptr Create(const std::string &dir, int version, bool registerFunction = false); virtual int64_t Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket) = 0; virtual int64_t Update(const std::string &tableName, const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket) = 0; @@ -37,13 +38,7 @@ public: const DataSharePredicates &predicates, const std::vector &columns, int &errCode) = 0; virtual std::shared_ptr Query( const std::string &sql, const std::vector &selectionArgs = std::vector()) = 0; - virtual int ExecuteSql(const std::string &sql) = 0; -}; - -struct RdbStoreContext { - RdbStoreContext(const std::string &dir, int version) : dir(dir), version(version) {} - std::string dir; - int version; + virtual std::unique_ptr QuerySql(const std::string &sql) = 0; }; class Id : public DistributedData::Serializable { diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index e4c9b2be..6859b3aa 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -23,10 +23,29 @@ namespace OHOS::DataShare { constexpr static int32_t MAX_RESULTSET_COUNT = 16; std::atomic RdbDelegate::resultSetCount = 0; -RdbDelegate::RdbDelegate(const std::string &dir, int version) +std::string RemindTimerFunc(const std::vector &args) +{ + int size = args.size(); + if (size != ARGS_SIZE) { + ZLOGE("RemindTimerFunc args size error, %{public}d", size); + return ""; + } + std::string dbPath = args[ARG_DB_PATH]; + int version = std::strtol(args[ARG_VERSION].c_str(), nullptr, 0); + Key key(args[ARG_URI], std::strtoll(args[ARG_SUBSCRIBER_ID].c_str(), nullptr, 0), args[ARG_BUNDLE_NAME]); + int64_t reminderTime = std::strtoll(args[ARG_TIME].c_str(), nullptr, 0); + + SchedulerManager::GetInstance().SetTimer(dbPath, version, key, reminderTime); + return args[ARG_TIME]; +} + +RdbDelegate::RdbDelegate(const std::string &dir, int version, bool registerFunction) { RdbStoreConfig config(dir); config.SetCreateNecessary(false); + if (registerFunction) { + config.SetScalarFunction("remindTimer", ARGS_SIZE, RemindTimerFunc); + } DefaultOpenCallback callback; store_ = RdbHelper::GetRdbStore(config, version, callback, errCode_); if (errCode_ != E_OK) { @@ -126,12 +145,12 @@ std::shared_ptr RdbDelegate::Query( return std::make_shared(resultSet); } -int RdbDelegate::ExecuteSql(const std::string &sql) +std::unique_ptr RdbDelegate::QuerySql(const std::string &sql) { if (store_ == nullptr) { ZLOGE("store is null"); - return -1; + return nullptr; } - return store_->ExecuteSql(sql); + return store_->QuerySql(sql); } } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.h b/services/distributeddataservice/service/data_share/common/rdb_delegate.h index c30ceac8..836eb03c 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.h +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.h @@ -29,9 +29,19 @@ namespace OHOS::DataShare { using namespace OHOS::NativeRdb; +enum REMIND_TIMER_ARGS : int32_t { + ARG_DB_PATH = 0, + ARG_VERSION, + ARG_URI, + ARG_SUBSCRIBER_ID, + ARG_BUNDLE_NAME, + ARG_TIME, + ARGS_SIZE +}; + class RdbDelegate final : public DBDelegate { public: - explicit RdbDelegate(const std::string &dir, int version); + explicit RdbDelegate(const std::string &dir, int version, bool registerFunction); int64_t Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket) override; int64_t Update(const std::string &tableName, const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket) override; @@ -40,7 +50,7 @@ public: const std::vector &columns, int &errCode) override; std::shared_ptr Query( const std::string &sql, const std::vector &selectionArgs) override; - int ExecuteSql(const std::string &sql) override; + std::unique_ptr QuerySql(const std::string &sql) override; private: static std::atomic resultSetCount; diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp new file mode 100644 index 00000000..d1f134e8 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2023 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 "SchedulerManager" + +#include "scheduler_manager.h" + +#include "log_print.h" +#include "uri_utils.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +SchedulerManager &SchedulerManager::GetInstance() +{ + static SchedulerManager instance; + return instance; +} + +void SchedulerManager::Execute(const std::string &uri, const std::string &rdbDir, int version) +{ + if (!URIUtils::IsDataProxyURI(uri)) { + return; + } + auto delegate = DBDelegate::Create(rdbDir, version, true); + if (delegate == nullptr) { + ZLOGE("malloc fail %{public}s", DistributedData::Anonymous::Change(uri).c_str()); + return; + } + std::vector keys = RdbSubscriberManager::GetInstance().GetKeysByUri(uri); + for (auto &key : keys) { + if (RdbSubscriberManager::GetInstance().GetObserverCount(key) == 0) { + continue; + } + ExecuteSchedulerSQL(rdbDir, version, key, delegate); + } +} + +void SchedulerManager::Execute(const Key &key, const std::string &rdbDir, int version) +{ + auto delegate = DBDelegate::Create(rdbDir, version, true); + if (delegate == nullptr) { + ZLOGE("malloc fail %{public}s", DistributedData::Anonymous::Change(key.uri).c_str()); + return; + } + ExecuteSchedulerSQL(rdbDir, version, key, delegate); +} + +void SchedulerManager::SetTimer(const std::string &dbPath, int version, const Key &key, int64_t reminderTime) +{ + std::lock_guard lock(mutex_); + if (scheduler_ == nullptr) { + scheduler_ = std::make_shared(TIME_TASK_NUM, "remind_timer"); + } + auto it = timerCache_.find(key); + if (it != timerCache_.end()) { + // has current timer, reset time + ZLOGD("has current taskId, uri is %{public}s, subscriberId is %{public}" PRId64 ", bundleName is %{public}s", + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); + // scheduler_->Reset(it->second, std::chrono::seconds(reminderTime - time(nullptr))); + scheduler_->Reset(it->second, std::chrono::seconds(7)); + return; + } + // not find task in map, create new timer + // auto taskId = scheduler_->At(TaskScheduler::Clock::now() + std::chrono::seconds(reminderTime - time(nullptr)), + auto taskId = scheduler_->At(TaskScheduler::Clock::now() + std::chrono::seconds(7), + [key, dbPath, version, this]() { + timerCache_.erase(key); + // 1. execute schedulerSQL in next time + Execute(key, dbPath, version); + // 2. notify + RdbSubscriberManager::GetInstance().EmitByKey(key, dbPath, version); + }); + if (taskId == TaskScheduler::INVALID_TASK_ID) { + ZLOGE("create timer failed, over the max capacity"); + return; + } + ZLOGI("create new task success, uri is %{public}s, subscriberId is %{public}" PRId64 ", bundleName is %{public}s", + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); + timerCache_.emplace(key, taskId); +} + +void SchedulerManager::ExecuteSchedulerSQL(const std::string &rdbDir, int version, const Key &key, + std::shared_ptr delegate) +{ + Template tpl; + if (!TemplateManager::GetInstance().GetTemplate(key.uri, key.subscriberId, key.bundleName, tpl)) { + ZLOGE("template undefined, %{public}s, %{public}" PRId64 ", %{public}s", + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); + return; + } + if (tpl.scheduler_.empty()) { + ZLOGW("template scheduler_ empty, %{public}s, %{public}" PRId64 ", %{public}s", + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); + return; + } + GenRemindTimerFuncParams(rdbDir, version, key, tpl.scheduler_); + auto resultSet = delegate->QuerySql(tpl.scheduler_); + if (resultSet == nullptr) { + ZLOGE("resultSet is nullptr, %{public}s, %{public}" PRId64 ", %{public}s", + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); + return; + } + int count; + int errCode = resultSet->GetRowCount(count); + if (errCode != E_OK || count == 0) { + ZLOGE("GetRowCount error, %{public}s, %{public}" PRId64 ", %{public}s, errorCode is %{public}d, count is " + "%{public}d", + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str(), errCode, + count); + return; + } + errCode = resultSet->GoToFirstRow(); + if (errCode != E_OK) { + ZLOGE("GoToFirstRow error, %{public}s, %{public}" PRId64 ", %{public}s, errCode is %{public}d", + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str(), errCode); + } +} + +void SchedulerManager::GenRemindTimerFuncParams(const std::string &rdbDir, int version, const Key &key, + std::string &schedulerSQL) +{ + auto index = schedulerSQL.find(REMIND_TIMER_FUNC); + if (index == -1) { + ZLOGW("not find remindTimer, sql is %{public}s", schedulerSQL.c_str()); + return; + } + index += REMIND_TIMER_FUNC_LEN; + std::string keyStr = "'" + rdbDir + "', " + std::to_string(version) + ", '" + key.uri + "', " + + std::to_string(key.subscriberId) + ", '" + key.bundleName + "', "; + schedulerSQL.insert(index, keyStr); + return; +} + +void SchedulerManager::RemoveTimer(const Key &key) +{ + std::lock_guard lock(mutex_); + if (scheduler_ == nullptr) { + ZLOGD("scheduler_ is nullptr"); + return; + } + auto it = timerCache_.find(key); + if (it != timerCache_.end()) { + ZLOGD("RemoveTimer %{public}s %{public}s %{public}" PRId64, + DistributedData::Anonymous::Change(key.uri).c_str(), key.bundleName.c_str(), key.subscriberId); + scheduler_->Remove(it->second); + timerCache_.erase(key); + if (timerCache_.empty()) { + scheduler_ = nullptr; + } + } +} +} // namespace OHOS::DataShare + diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.h b/services/distributeddataservice/service/data_share/common/scheduler_manager.h new file mode 100644 index 00000000..546d77be --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 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 SCHEDULER_MANAGER_H +#define SCHEDULER_MANAGER_H + +#include + +#include "db_delegate.h" +#include "task_scheduler.h" +#include "template_manager.h" + +namespace OHOS::DataShare { +class SchedulerManager { +public: + static SchedulerManager &GetInstance(); + void Execute(const std::string &uri, const std::string &rdbDir, int version); + void Execute(const Key &key, const std::string &rdbDir, int version); + void SetTimer(const std::string &dbPath, int version, const Key &key, int64_t reminderTime); + void RemoveTimer(const Key &key); + +private: + static constexpr const char *REMIND_TIMER_FUNC = "remindTimer("; + static constexpr int REMIND_TIMER_FUNC_LEN = 12; + static constexpr size_t TIME_TASK_NUM = 10; + SchedulerManager() = default; + ~SchedulerManager() = default; + void GenRemindTimerFuncParams(const std::string &rdbDir, int version, const Key &key, std::string &schedulerSQL); + void ExecuteSchedulerSQL(const std::string &rdbDir, int version, const Key &key, + std::shared_ptr delegate); + + std::mutex mutex_; + std::map timerCache_; + std::shared_ptr scheduler_ = nullptr; +}; +} // namespace OHOS::DataShare +#endif // SCHEDULER_MANAGER_H diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index 9a22d067..9833fe2f 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -23,6 +23,7 @@ #include "template_data.h" #include "uri_utils.h" #include "utils/anonymous.h" +#include "scheduler_manager.h" namespace OHOS::DataShare { bool TemplateManager::GetTemplate( @@ -60,6 +61,7 @@ bool TemplateManager::DelTemplate(const std::string &uri, const TemplateId &tplI ZLOGE("db DeleteById failed, %{public}d", status); return false; } + SchedulerManager::GetInstance().RemoveTimer(Key(uri, tplId.subscriberId_, tplId.bundleName_)); return true; } @@ -134,6 +136,9 @@ int RdbSubscriberManager::AddRdbSubscriber(const std::string &uri, const Templat return false; } value.emplace_back(observer, context->callerTokenId); + if (GetEnableObserverCount(key) == 1) { + SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); + } return true; }); return result; @@ -154,6 +159,9 @@ int RdbSubscriberManager::DelRdbSubscriber( it++; } } + if (GetEnableObserverCount(key) == 0) { + SchedulerManager::GetInstance().RemoveTimer(key); + } return !value.empty(); }); return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; @@ -170,6 +178,9 @@ int RdbSubscriberManager::DisableRdbSubscriber( it->enabled = false; } } + if (GetEnableObserverCount(key) == 0) { + SchedulerManager::GetInstance().RemoveTimer(key); + } return true; }); return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; @@ -187,6 +198,9 @@ int RdbSubscriberManager::EnableRdbSubscriber( node.emplace_back(it->observer, context->callerTokenId); Notify(key, node, context->calledSourceDir, context->version); } + if (GetEnableObserverCount(key) == 1) { + SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); + } } return true; }); @@ -264,7 +278,7 @@ int RdbSubscriberManager::Notify( DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); return E_TEMPLATE_NOT_EXIST; } - auto delegate = DBDelegate::Create(rdbDir, rdbVersion); + auto delegate = DBDelegate::Create(rdbDir, rdbVersion, true); if (delegate == nullptr) { ZLOGE("Create fail %{public}s %{public}s", DistributedData::Anonymous::Change(key.uri).c_str(), key.bundleName.c_str()); diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 6c198e7b..d277f2e4 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -58,7 +58,7 @@ int32_t DataShareServiceImpl::Insert(const std::string &uri, const DataShareValu if (ret) { NotifyChange(uri); RdbSubscriberManager::GetInstance().Emit(uri, context); - return ret; + SchedulerManager::GetInstance().Execute(uri, context->calledSourceDir, context->version); } return ret; } @@ -88,7 +88,7 @@ int32_t DataShareServiceImpl::Update(const std::string &uri, const DataSharePred if (ret) { NotifyChange(uri); RdbSubscriberManager::GetInstance().Emit(uri, context); - return ret; + SchedulerManager::GetInstance().Execute(uri, context->calledSourceDir, context->version); } return ret; } @@ -101,7 +101,7 @@ int32_t DataShareServiceImpl::Delete(const std::string &uri, const DataSharePred if (ret) { NotifyChange(uri); RdbSubscriberManager::GetInstance().Emit(uri, context); - return ret; + SchedulerManager::GetInstance().Execute(uri, context->calledSourceDir, context->version); } return ret; } -- Gitee From 94d6509080da1c104bc4fad93560544877faeefa Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sat, 13 May 2023 18:31:58 +0800 Subject: [PATCH 29/95] fix:change to static Signed-off-by: htt1997 --- .../app/src/uninstaller/uninstaller_impl.cpp | 4 ++-- .../app/src/uninstaller/uninstaller_impl.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index 12aa99db..80f0686a 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -91,12 +91,12 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared CommonEventSubscribeInfo info(matchingSkills); auto subscriber = std::make_shared(info); - auto removedCallback = [this, kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { + auto removedCallback = [kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { kvStoreDataService->OnUninstall(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID()); OnUninstall(bundleName, userId, appIndex); }; - auto updatedCallback = [this, kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { + auto updatedCallback = [kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { kvStoreDataService->OnUpdate(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID()); OnUpdate(bundleName, userId, appIndex); }; diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h index 9607f58b..4de54f49 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h @@ -47,8 +47,8 @@ public: private: static constexpr int32_t RETRY_TIME = 300; static constexpr int32_t RETRY_INTERVAL = 100; - void OnUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex); - void OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex); + static void OnUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex); + static void OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex); int32_t retryTime_; ExecutorPool::Task GetTask(); std::shared_ptr subscriber_ {}; -- Gitee From a649fe849b63c1a3bad70febac80abf946b06a7a Mon Sep 17 00:00:00 2001 From: hanlu Date: Sat, 13 May 2023 18:46:18 +0800 Subject: [PATCH 30/95] f Signed-off-by: hanlu --- services/distributeddataservice/service/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 62ccc155..9254d279 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -144,10 +144,10 @@ ohos_shared_library("distributeddatasvc") { deps = [ "${kv_store_distributeddb_path}:distributeddb", "${relational_store_path}/interfaces/inner_api/cloud_data:cloud_data_inner", + "data_share/gaussdb_rd:gaussdb_rd", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", - "data_share/gaussdb_rd:gaussdb_rd", ] external_deps = [ -- Gitee From 2db5e5056ea10561179eb61019d63f7ab0ac692f Mon Sep 17 00:00:00 2001 From: hanlu Date: Sat, 13 May 2023 18:47:45 +0800 Subject: [PATCH 31/95] f Signed-off-by: hanlu --- .../service/data_share/common/scheduler_manager.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp index d1f134e8..52501eee 100644 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp @@ -67,8 +67,7 @@ void SchedulerManager::SetTimer(const std::string &dbPath, int version, const Ke // has current timer, reset time ZLOGD("has current taskId, uri is %{public}s, subscriberId is %{public}" PRId64 ", bundleName is %{public}s", DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); - // scheduler_->Reset(it->second, std::chrono::seconds(reminderTime - time(nullptr))); - scheduler_->Reset(it->second, std::chrono::seconds(7)); + scheduler_->Reset(it->second, std::chrono::seconds(reminderTime - time(nullptr))); return; } // not find task in map, create new timer -- Gitee From e58a88da9fd89d93a16b04ab655b94349889209a Mon Sep 17 00:00:00 2001 From: hanlu Date: Sat, 13 May 2023 19:12:12 +0800 Subject: [PATCH 32/95] f Signed-off-by: hanlu --- .../include/serializable/serializable.h | 15 +++++-- .../service/data_share/common/db_delegate.h | 2 +- .../data_share/common/rdb_delegate.cpp | 45 +++++++++++-------- .../service/data_share/common/rdb_delegate.h | 13 +----- .../data_share/common/template_manager.cpp | 4 +- .../data_share/data/json_formatter.cpp | 12 +---- .../service/data_share/data/json_formatter.h | 4 +- .../data_share/data_share_service_impl.cpp | 3 +- 8 files changed, 47 insertions(+), 51 deletions(-) diff --git a/services/distributeddataservice/framework/include/serializable/serializable.h b/services/distributeddataservice/framework/include/serializable/serializable.h index 47d1805e..3dda5678 100644 --- a/services/distributeddataservice/framework/include/serializable/serializable.h +++ b/services/distributeddataservice/framework/include/serializable/serializable.h @@ -195,20 +195,27 @@ bool Serializable::SetValue(json &node, const T *value) template bool Serializable::SetValue(json &node, const std::variant<_Types...> &input) { - node[GET_NAME(type)] = input.index(); - return WriteVariant(node, 0, input); + bool ret = SetValue(node[GET_NAME(type)], input.index()); + if (!ret) { + return ret; + } + return WriteVariant(node[GET_NAME(value)], 0, input); } template bool Serializable::GetValue(const json &node, const std::string &name, std::variant<_Types...> &value) { + auto &subNode = GetSubNode(node, name); + if (subNode.is_null()) { + return false; + } uint32_t index; - bool ret = GetValue(node, GET_NAME(type), index); + bool ret = GetValue(subNode, GET_NAME(type), index); if (!ret) { return ret; } - return Serializable::ReadVariant(node, name, 0, index, value); + return Serializable::ReadVariant(subNode, GET_NAME(value), 0, index, value); } template diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.h b/services/distributeddataservice/service/data_share/common/db_delegate.h index b28b5a58..d8b9df90 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.h +++ b/services/distributeddataservice/service/data_share/common/db_delegate.h @@ -36,7 +36,7 @@ public: virtual int64_t Delete(const std::string &tableName, const DataSharePredicates &predicate) = 0; virtual std::shared_ptr Query(const std::string &tableName, const DataSharePredicates &predicates, const std::vector &columns, int &errCode) = 0; - virtual std::shared_ptr Query( + virtual std::string Query( const std::string &sql, const std::vector &selectionArgs = std::vector()) = 0; virtual std::unique_ptr QuerySql(const std::string &sql) = 0; }; diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 6859b3aa..0e8a5b95 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -18,25 +18,36 @@ #include "data/resultset_json_formatter.h" #include "log_print.h" #include "rdb_utils.h" +#include "scheduler_manager.h" #include "utils/anonymous.h" namespace OHOS::DataShare { constexpr static int32_t MAX_RESULTSET_COUNT = 16; std::atomic RdbDelegate::resultSetCount = 0; +enum REMIND_TIMER_ARGS : int32_t +{ + ARG_DB_PATH = 0, + ARG_VERSION, + ARG_URI, + ARG_SUBSCRIBER_ID, + ARG_BUNDLE_NAME, + ARG_TIME, + ARGS_SIZE +}; std::string RemindTimerFunc(const std::vector &args) { - int size = args.size(); - if (size != ARGS_SIZE) { - ZLOGE("RemindTimerFunc args size error, %{public}d", size); - return ""; - } - std::string dbPath = args[ARG_DB_PATH]; - int version = std::strtol(args[ARG_VERSION].c_str(), nullptr, 0); - Key key(args[ARG_URI], std::strtoll(args[ARG_SUBSCRIBER_ID].c_str(), nullptr, 0), args[ARG_BUNDLE_NAME]); - int64_t reminderTime = std::strtoll(args[ARG_TIME].c_str(), nullptr, 0); + int size = args.size(); + if (size != ARGS_SIZE) { + ZLOGE("RemindTimerFunc args size error, %{public}d", size); + return ""; + } + std::string dbPath = args[ARG_DB_PATH]; + int version = std::strtol(args[ARG_VERSION].c_str(), nullptr, 0); + Key key(args[ARG_URI], std::strtoll(args[ARG_SUBSCRIBER_ID].c_str(), nullptr, 0), args[ARG_BUNDLE_NAME]); + int64_t reminderTime = std::strtoll(args[ARG_TIME].c_str(), nullptr, 0); - SchedulerManager::GetInstance().SetTimer(dbPath, version, key, reminderTime); - return args[ARG_TIME]; + SchedulerManager::GetInstance().SetTimer(dbPath, version, key, reminderTime); + return args[ARG_TIME]; } RdbDelegate::RdbDelegate(const std::string &dir, int version, bool registerFunction) @@ -98,9 +109,8 @@ int64_t RdbDelegate::Delete(const std::string &tableName, const DataSharePredica } return changeCount; } -std::shared_ptr RdbDelegate::Query( - const std::string &tableName, const DataSharePredicates &predicates, - const std::vector &columns, int &errCode) +std::shared_ptr RdbDelegate::Query(const std::string &tableName, + const DataSharePredicates &predicates, const std::vector &columns, int &errCode) { if (store_ == nullptr) { ZLOGE("store is null"); @@ -129,8 +139,7 @@ std::shared_ptr RdbDelegate::Query( }); } -std::shared_ptr RdbDelegate::Query( - const std::string &sql, const std::vector &selectionArgs) +std::string RdbDelegate::Query(const std::string &sql, const std::vector &selectionArgs) { if (store_ == nullptr) { ZLOGE("store is null"); @@ -141,8 +150,8 @@ std::shared_ptr RdbDelegate::Query( ZLOGE("Query failed %{private}s", sql.c_str()); return nullptr; } - - return std::make_shared(resultSet); + ResultSetJsonFormatter formatter(resultSet); + return DistributedData::Serializable::Marshall(formatter); } std::unique_ptr RdbDelegate::QuerySql(const std::string &sql) diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.h b/services/distributeddataservice/service/data_share/common/rdb_delegate.h index 836eb03c..2a68d15b 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.h +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.h @@ -29,16 +29,6 @@ namespace OHOS::DataShare { using namespace OHOS::NativeRdb; -enum REMIND_TIMER_ARGS : int32_t { - ARG_DB_PATH = 0, - ARG_VERSION, - ARG_URI, - ARG_SUBSCRIBER_ID, - ARG_BUNDLE_NAME, - ARG_TIME, - ARGS_SIZE -}; - class RdbDelegate final : public DBDelegate { public: explicit RdbDelegate(const std::string &dir, int version, bool registerFunction); @@ -48,8 +38,7 @@ public: int64_t Delete(const std::string &tableName, const DataSharePredicates &predicate) override; std::shared_ptr Query(const std::string &tableName, const DataSharePredicates &predicates, const std::vector &columns, int &errCode) override; - std::shared_ptr Query( - const std::string &sql, const std::vector &selectionArgs) override; + std::string Query(const std::string &sql, const std::vector &selectionArgs) override; std::unique_ptr QuerySql(const std::string &sql) override; private: diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index 9833fe2f..e876a77e 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -20,10 +20,10 @@ #include "json_formatter.h" #include "log_print.h" #include "published_data.h" +#include "scheduler_manager.h" #include "template_data.h" #include "uri_utils.h" #include "utils/anonymous.h" -#include "scheduler_manager.h" namespace OHOS::DataShare { bool TemplateManager::GetTemplate( @@ -199,7 +199,7 @@ int RdbSubscriberManager::EnableRdbSubscriber( Notify(key, node, context->calledSourceDir, context->version); } if (GetEnableObserverCount(key) == 1) { - SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); + SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); } } return true; diff --git a/services/distributeddataservice/service/data_share/data/json_formatter.cpp b/services/distributeddataservice/service/data_share/data/json_formatter.cpp index db190824..b3c38832 100644 --- a/services/distributeddataservice/service/data_share/data/json_formatter.cpp +++ b/services/distributeddataservice/service/data_share/data/json_formatter.cpp @@ -20,19 +20,11 @@ namespace OHOS::DataShare { bool JsonFormatter::Marshal(json &node) const { - if (value_ == nullptr) { - ZLOGE("null value %{public}s", key_.c_str()); - return false; - } - return SetValue(node[key_], *value_); + return SetValue(node[key_], value_); } bool JsonFormatter::Unmarshal(const json &node) { - if (value_ == nullptr) { - ZLOGE("null value %{public}s", key_.c_str()); - return false; - } - return GetValue(node, key_, *value_); + return GetValue(node, key_, value_); } } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/json_formatter.h b/services/distributeddataservice/service/data_share/data/json_formatter.h index 7c41e197..0b718493 100644 --- a/services/distributeddataservice/service/data_share/data/json_formatter.h +++ b/services/distributeddataservice/service/data_share/data/json_formatter.h @@ -21,7 +21,7 @@ namespace OHOS::DataShare { class JsonFormatter : public DistributedData::Serializable { public: - JsonFormatter(const std::string &key, const std::shared_ptr &value) + JsonFormatter(const std::string &key, const std::string &value) : key_(key), value_(value) { } @@ -30,7 +30,7 @@ public: private: std::string key_; - std::shared_ptr value_; + std::string value_; }; } // namespace OHOS::DataShare #endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index d277f2e4..14b957db 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -20,7 +20,6 @@ #include "accesstoken_kit.h" #include "account/account_delegate.h" #include "bootstrap.h" -#include "common/kv_delegate.h" #include "dataobs_mgr_client.h" #include "datashare_errno.h" #include "datashare_template.h" @@ -31,8 +30,8 @@ #include "insert_strategy.h" #include "ipc_skeleton.h" #include "log_print.h" -#include "publish_strategy.h" #include "query_strategy.h" +#include "scheduler_manager.h" #include "subscribe_strategy.h" #include "template_manager.h" #include "update_strategy.h" -- Gitee From 1aa7433994ccf49d8d24bb77802404456cded8ab Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sat, 13 May 2023 20:38:03 +0800 Subject: [PATCH 33/95] on cloud datachange Signed-off-by: zuojiangjiang --- .../framework/cloud/cloud_info.cpp | 2 +- .../framework/include/cloud/cloud_event.h | 3 +- .../distributeddataservice/service/BUILD.gn | 2 + .../service/cloud/cloud_service_impl.cpp | 132 +++++++++++++----- .../service/cloud/cloud_service_impl.h | 6 +- .../service/rdb/rdb_general_store.cpp | 64 ++++++++- .../service/rdb/rdb_general_store.h | 2 + .../service/rdb/rdb_query.cpp | 23 +++ .../service/rdb/rdb_query.h | 34 +++++ .../service/rdb/rdb_service_impl.cpp | 97 ++++++++++++- .../service/rdb/rdb_service_stub.cpp | 5 +- .../service/rdb/rdb_syncer.h | 3 +- .../service/rdb/rdb_watcher.cpp | 43 ++++++ .../service/rdb/rdb_watcher.h | 37 +++++ 14 files changed, 401 insertions(+), 52 deletions(-) create mode 100644 services/distributeddataservice/service/rdb/rdb_query.cpp create mode 100644 services/distributeddataservice/service/rdb/rdb_query.h create mode 100644 services/distributeddataservice/service/rdb/rdb_watcher.cpp create mode 100644 services/distributeddataservice/service/rdb/rdb_watcher.h diff --git a/services/distributeddataservice/framework/cloud/cloud_info.cpp b/services/distributeddataservice/framework/cloud/cloud_info.cpp index 2f44ff85..a50f4d69 100644 --- a/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -61,7 +61,7 @@ bool CloudInfo::AppInfo::Unmarshal(const Serializable::json &node) std::string CloudInfo::GetKey() const { - return GetKey(INFO_PREFIX, { std::to_string(user), id }); + return GetKey(INFO_PREFIX, { std::to_string(user) }); } std::map CloudInfo::GetSchemaKey() const diff --git a/services/distributeddataservice/framework/include/cloud/cloud_event.h b/services/distributeddataservice/framework/include/cloud/cloud_event.h index e6cff13a..47c873c9 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -25,7 +25,7 @@ public: enum : int32_t { FEATURE_INIT = EVT_CLOUD, GET_SCHEMA, - NEED_CREATE, + DATA_CHANGE, CLOUD_BUTT }; @@ -34,6 +34,7 @@ public: std::string bundleName; std::string storeName; int32_t instanceId = 0; + int32_t user = 0; }; CloudEvent(int32_t evtId, StoreInfo storeInfo, const std::string &featureName = "relational_store"); diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 1d29c1ef..46cae1d6 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -123,12 +123,14 @@ ohos_shared_library("distributeddatasvc") { "permission/src/permit_delegate.cpp", "rdb/rdb_general_store.cpp", "rdb/rdb_notifier_proxy.cpp", + "rdb/rdb_query.cpp", "rdb/rdb_result_set_impl.cpp", "rdb/rdb_result_set_stub.cpp", "rdb/rdb_service_impl.cpp", "rdb/rdb_service_stub.cpp", "rdb/rdb_store_observer_impl.cpp", "rdb/rdb_syncer.cpp", + "rdb/rdb_watcher.cpp", ] cflags = [ "-Wno-multichar" ] diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index e9e932fc..5a131f5c 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -37,23 +37,22 @@ using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; __attribute__((used)) CloudServiceImpl::Factory CloudServiceImpl::factory_; CloudServiceImpl::Factory::Factory() noexcept { - FeatureSystem::GetInstance().RegisterCreator(CloudServiceImpl::SERVICE_NAME, [this]() { - if (product_ == nullptr) { - product_ = std::make_shared(); - } - return product_; - }); + FeatureSystem::GetInstance().RegisterCreator( + CloudServiceImpl::SERVICE_NAME, + [this]() { + if (product_ == nullptr) { + product_ = std::make_shared(); + } + return product_; + }, + FeatureSystem::BIND_NOW); } CloudServiceImpl::Factory::~Factory() {} CloudServiceImpl::CloudServiceImpl() { - EventCenter::GetInstance().Subscribe(CloudEvent::FEATURE_INIT, [this](const Event &event) { - FeatureInit(event); - return; - }); - + FeatureInit(); EventCenter::GetInstance().Subscribe(CloudEvent::GET_SCHEMA, [this](const Event &event) { GetSchema(event); return; @@ -63,8 +62,9 @@ CloudServiceImpl::CloudServiceImpl() int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &switches) { CloudInfo cloudInfo; - if (GetCloudInfo(IPCSkeleton::GetCallingTokenID(), id, cloudInfo) != SUCCESS) { - return INVALID_ARGUMENT; + auto status = GetCloudInfo(IPCSkeleton::GetCallingTokenID(), id, cloudInfo); + if (status != SUCCESS) { + return status; } cloudInfo.enableCloud = true; for (const auto &item : switches) { @@ -86,8 +86,9 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map bool { @@ -120,8 +122,10 @@ int32_t CloudServiceImpl::ChangeAppSwitch(const std::string &id, const std::stri int32_t CloudServiceImpl::Clean(const std::string &id, const std::map &actions) { CloudInfo cloudInfo; - if (GetCloudInfo(IPCSkeleton::GetCallingTokenID(), id, cloudInfo) != SUCCESS) { - return INVALID_ARGUMENT; + auto tokenId = IPCSkeleton::GetCallingTokenID(); + cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); + if (GetCloudInfoFromMeta(cloudInfo) != SUCCESS) { + return ERROR; } auto keys = cloudInfo.GetSchemaKey(); for (const auto &action : actions) { @@ -138,16 +142,60 @@ int32_t CloudServiceImpl::Clean(const std::string &id, const std::mapGetUserByToken(tokenId); + if (GetCloudInfoFromMeta(cloudInfo) != SUCCESS) { + return ERROR; + } + if (cloudInfo.id != id) { + ZLOGE("invalid args, [input] id:%{public}s, [exist] id:%{public}s", Anonymous::Change(id).c_str(), + Anonymous::Change(cloudInfo.id).c_str()); + return INVALID_ARGUMENT; + } + if (!cloudInfo.enableCloud) { + return CLOUD_DISABLE; + } + auto it = std::find_if(cloudInfo.apps.begin(), cloudInfo.apps.end(), + [&bundleName](const CloudInfo::AppInfo &appInfo) -> bool { + return appInfo.bundleName == bundleName; + }); + if (it == cloudInfo.apps.end()) { + ZLOGE("bundleName:%{public}s", bundleName.c_str()); + return INVALID_ARGUMENT; + } + if (!it->cloudSwitch) { + return CLOUD_DISABLE_SWITCH; + } + + auto key = cloudInfo.GetSchemaKey(bundleName); + SchemaMeta schemaMeta; + if (!MetaDataManager::GetInstance().LoadMeta(key, schemaMeta, true)) { + ZLOGE("bundleName:%{public}s", bundleName.c_str()); + return INVALID_ARGUMENT; + } + for (const auto &database : schemaMeta.databases) { + EventCenter::Defer defer; + CloudEvent::StoreInfo storeInfo; + storeInfo.bundleName = it->bundleName; + storeInfo.instanceId = it->instanceId; + storeInfo.user = cloudInfo.user; + storeInfo.storeName = database.name; + auto evt = std::make_unique(CloudEvent::DATA_CHANGE, storeInfo); + EventCenter::GetInstance().PostEvent(std::move(evt)); + } + return SUCCESS; } int32_t CloudServiceImpl::GetCloudInfo(uint32_t tokenId, const std::string &id, CloudInfo &cloudInfo) { cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); - if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true) && - GetServerInfo(cloudInfo) != SUCCESS) { - ZLOGE("invalid args, user:%{public}d", cloudInfo.user); - return INVALID_ARGUMENT; + if (GetCloudInfoFromMeta(cloudInfo) != SUCCESS) { + auto status = GetCloudInfoFromServer(cloudInfo); + if (status != SUCCESS) { + ZLOGE("user:%{public}d", cloudInfo.user); + return status; + } } if (cloudInfo.id != id) { ZLOGE("invalid args, [input] id:%{public}s, [exist] id:%{public}s", Anonymous::Change(id).c_str(), @@ -157,16 +205,23 @@ int32_t CloudServiceImpl::GetCloudInfo(uint32_t tokenId, const std::string &id, return SUCCESS; } -int32_t CloudServiceImpl::GetServerInfo(CloudInfo &cloudInfo) +int32_t CloudServiceImpl::GetCloudInfoFromMeta(CloudInfo &cloudInfo) +{ + cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { + ZLOGE("no exist meta, user:%{public}d", cloudInfo.user); + return ERROR; + } + return SUCCESS; +} + +int32_t CloudServiceImpl::GetCloudInfoFromServer(CloudInfo &cloudInfo) { auto instance = CloudServer::GetInstance(); if (instance == nullptr) { - return SERVER_UNAVAILABLE; + return NOT_SUPPORT; } cloudInfo = instance->GetServerInfo(cloudInfo.user); - if (!cloudInfo.IsValid()) { - return ERROR; - } return SUCCESS; } @@ -243,20 +298,25 @@ StoreMetaData CloudServiceImpl::GetStoreMata(int32_t userId, const std::string & return storeMetaData; } -void CloudServiceImpl::FeatureInit(const Event &event) +void CloudServiceImpl::FeatureInit() { CloudInfo cloudInfo; std::vector users; if (!DistributedKv::AccountDelegate::GetInstance()->QueryUsers(users) || users.empty()) { return; } - cloudInfo.user = *users.begin(); - if (GetServerInfo(cloudInfo) != SUCCESS) { - ZLOGE("failed, user:%{public}d", cloudInfo.user); - return; + for (const auot &user : users) { + if (user == USER_ID) { + continue; + } + cloudInfo.user = user; + if (GetCloudInfoFromServer(cloudInfo) != SUCCESS) { + ZLOGE("failed, user:%{public}d", user); + continue; + } + UpdateCloudInfo(cloudInfo); + AddSchema(cloudInfo); } - UpdateCloudInfo(cloudInfo); - AddSchema(cloudInfo); } void CloudServiceImpl::GetSchema(const Event &event) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 26ed17e7..8b79e73b 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -35,6 +35,7 @@ public: int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) override; private: + static const inline int USER_ID = 0; class Factory { public: Factory() noexcept; @@ -54,9 +55,10 @@ private: StoreMetaData GetStoreMata(int32_t userId, const std::string &bundleName, const std::string &storeName, int32_t instanceId); int32_t GetCloudInfo(uint32_t tokenId, const std::string &id, CloudInfo &cloudInfo); - int32_t GetServerInfo(CloudInfo &cloudInfo); + int32_t GetCloudInfoFromMeta(CloudInfo &cloudInfo); + int32_t GetCloudInfoFromServer(CloudInfo &cloudInfo); int32_t GetAppSchema(int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta); - void FeatureInit(const Event &event); + void FeatureInit(); void GetSchema(const Event &event); }; } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index e57cc2ea..496878bc 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -20,12 +20,53 @@ #include "metadata/meta_data_manager.h" #include "metadata/secret_key_meta_data.h" #include "rdb_helper.h" +#include "rdb_query.h" +#include "rdb_syncer.h" #include "relational_store_manager.h" +#include "store/general_watcher.h" namespace OHOS::DistributedRdb { using namespace DistributedData; +using namespace DistributedDB; using namespace OHOS::NativeRdb; +class RdbOpenCallbackImpl : public RdbOpenCallback { +public: + int OnCreate(RdbStore &rdbStore) override + { + return NativeRdb::E_OK; + } + int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override + { + return NativeRdb::E_OK; + } +}; + RdbGeneralStore::RdbGeneralStore(const StoreMetaData &meta) : manager_(meta.appId, meta.user, meta.instanceId) { + RelationalStoreDelegate::Option option; + if (meta.isEncrypt) { + std::string key = meta.GetSecretKey(); + SecretKeyMetaData secretKeyMeta; + MetaDataManager::GetInstance().LoadMeta(key, secretKeyMeta, true); + std::vector decryptKey; + CryptoManager::GetInstance().Decrypt(secretKeyMeta.sKey, decryptKey); + if (option.passwd.SetValue(decryptKey.data(), decryptKey.size()) != CipherPassword::OK) { + std::fill(decryptKey.begin(), decryptKey.end(), 0); + } + std::fill(decryptKey.begin(), decryptKey.end(), 0); + option.isEncryptedDb = meta.isEncrypt; + option.iterateTimes = ITERATE_TIMES; + option.cipher = CipherType::AES_256_GCM; + } + option.observer = nullptr; + manager_.OpenStore(meta.dataDir, meta.storeId, option, delegate_); + RdbStoreConfig config(meta.dataDir); + config.SetCreateNecessary(false); + RdbOpenCallbackImpl callback; + int32_t errCode = NativeRdb::E_OK; + store_ = RdbHelper::GetRdbStore(config, meta.version, callback, errCode); + if (errCode != NativeRdb::E_OK) { + ZLOGE("GetRdbStore failed, errCode is %{public}d, storeId is %{public}s", errCode, meta.storeId.c_str()); + } } int32_t RdbGeneralStore::Close() @@ -65,7 +106,8 @@ std::shared_ptr RdbGeneralStore::Query(const std::string &table, GenQuer int32_t RdbGeneralStore::Watch(int32_t origin, Watcher &watcher) { - return 0; + watcher_ = &watcher; + return GeneralError::E_NOT_SUPPORT; } int32_t RdbGeneralStore::Unwatch(int32_t origin, Watcher &watcher) @@ -75,7 +117,25 @@ int32_t RdbGeneralStore::Unwatch(int32_t origin, Watcher &watcher) int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) { - return 0; + RdbQuery *rdbQuery = nullptr; + auto ret = query.QueryInterface(rdbQuery); + if (ret != GeneralError::E_OK || rdbQuery == nullptr) { + return GeneralError::E_OK; + } + auto status = delegate_->Sync( + devices, DistributedDB::SyncMode(mode), RdbSyncer::MakeQuery(rdbQuery->predicates_.GetDistributedPredicates()), + [async](const std::map> &result) { + std::map> detail; + for (auto &[key, tables] : result) { + auto value = detail[key]; + for (auto &table : tables) { + value[std::move(table.tableName)] = table.status; + } + } + async(std::move(detail)); + }, + wait); + return status == DistributedDB::OK ? GeneralError::E_OK : GeneralError::E_ERROR; } int32_t RdbGeneralStore::Bind(std::shared_ptr cloudDb) diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h index af8ca7d9..f1bd92cd 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -56,8 +56,10 @@ private: static constexpr uint32_t ITERATE_TIMES = 10000; RdbManager manager_; + RdbDelegate *delegate_ = nullptr; std::shared_ptr store_; std::shared_ptr cloudDb_; + Watcher *watcher_; }; } // namespace OHOS::DistributedRdb #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_STORE_H diff --git a/services/distributeddataservice/service/rdb/rdb_query.cpp b/services/distributeddataservice/service/rdb/rdb_query.cpp new file mode 100644 index 00000000..fc990eb1 --- /dev/null +++ b/services/distributeddataservice/service/rdb/rdb_query.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 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 "rdb_query.h" +namespace OHOS::DistributedRdb { +using namespace DistributedData; +bool RdbQuery::IsEqual(uint64_t tid) +{ + return tid == TYPE_ID; +} +} // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_query.h b/services/distributeddataservice/service/rdb/rdb_query.h new file mode 100644 index 00000000..fdeed511 --- /dev/null +++ b/services/distributeddataservice/service/rdb/rdb_query.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 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_DATAMGR_SERVICE_RDB_QUERY_H +#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_QUERY_H +#include "rdb_predicates.h" +#include "store/general_value.h" +namespace OHOS::DistributedRdb { +class RdbQuery : public DistributedData::GenQuery { +public: + using Predicates = NativeRdb::RdbPredicates; + static constexpr uint64_t TYPE_ID = 0; + RdbQuery() = default; + + ~RdbQuery() override = default; + + bool IsEqual(uint64_t tid) override; + + NativeRdb::RdbPredicates predicates_ {""}; +}; +} // namespace OHOS::DistributedRdb +#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_QUERY_H diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 1938c504..1250c43f 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -27,8 +27,9 @@ #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" #include "permission/permission_validator.h" +#include "rdb_watcher.h" #include "rdb_notifier_proxy.h" -#include "store/auto_cache.h" +#include "rdb_query.h" #include "types_export.h" #include "utils/anonymous.h" #include "utils/constant.h" @@ -88,6 +89,31 @@ RdbServiceImpl::RdbServiceImpl() : autoLaunchObserver_(this) [this](const std::string& identifier, DistributedDB::AutoLaunchParam ¶m) { return ResolveAutoLaunch(identifier, param); }); + EventCenter::GetInstance().Subscribe(CloudEvent::DATA_CHANGE, [this](const Event &event) { + auto &evt = static_cast(event); + auto storeInfo = evt.GetStoreInfo(); + StoreMetaData meta; + meta.storeId = storeInfo.storeName; + meta.bundleName = storeInfo.bundleName; + meta.user = std::to_string(storeInfo.user); + meta.instanceId = storeInfo.instanceId; + meta.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + ZLOGE("meta key:%{public}s", meta.GetKey().c_str()); + if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta)) { + ZLOGE("meta empty, bundleName:%{public}s, storeId:%{public}s", + meta.bundleName.c_str(), meta.storeId.c_str()); + return; + } + auto watchers = GetWatchers(meta.tokenId, meta.storeId); + auto store = AutoCache::GetInstance().GetStore(meta, watchers); + if (store == nullptr) { + ZLOGE("store null, storeId:%{public}s", meta.storeId.c_str()); + return; + } + for (const auto &watcher : watchers) { // mock for datachange + watcher->OnChange(GeneralWatcher::Origin::ORIGIN_CLOUD, {}); + } + }); } int32_t RdbServiceImpl::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) @@ -376,15 +402,74 @@ std::string RdbServiceImpl::GenIdentifier(const RdbSyncerParam ¶m) return TransferStringToHex(identifier); } -int32_t RdbServiceImpl::DoSubscribe(const RdbSyncerParam& param) +int32_t RdbServiceImpl::DoSubscribe(const RdbSyncerParam& param, const SubscribeOption &option) { pid_t pid = IPCSkeleton::GetCallingPid(); - auto identifier = GenIdentifier(param); - ZLOGI("%{public}s %{public}.6s %{public}d", param.storeName_.c_str(), identifier.c_str(), pid); - identifiers_.Insert(identifier, pid); + auto tokenId = IPCSkeleton::GetCallingTokenID(); + switch (option.mode) { + case SubscribeMode::REMOTE: { + auto identifier = GenIdentifier(param); + ZLOGI("%{public}s %{public}.6s %{public}d", param.storeName_.c_str(), identifier.c_str(), pid); + identifiers_.Insert(identifier, pid); + break; + } + case SubscribeMode::CLOUD: // fallthrough + case SubscribeMode::CLOUD_DETAIL: { + syncAgents_.Compute(tokenId, [this, pid, tokenId, ¶m, &option](auto &key, SyncAgent &value) { + if (pid != value.pid_) { + value.ReInit(pid, param.bundleName_); + } + auto storeName = RdbSyncer::RemoveSuffix(param.storeName_); + auto it = value.watchers_.find(storeName); + if (it == value.watchers_.end()) { + auto watcher = std::make_shared(this, tokenId, storeName); + value.watchers_[storeName] = { watcher }; + value.mode_[storeName] = option.mode; + } + return true; + }); + break; + } + default: + return RDB_ERROR; + } return RDB_OK; } +void RdbServiceImpl::OnChange(uint32_t tokenId, const std::string &storeName) +{ + pid_t pid = 0; + syncAgents_.ComputeIfPresent(tokenId, [&pid, &storeName](auto &key, SyncAgent &value) { + pid = value.pid_; + return true; + }); + notifiers_.ComputeIfPresent(pid, [&storeName](const auto& key, const sptr& value) { + value->OnChange(storeName, { storeName }); + return true; + }); +} + +AutoCache::Watchers RdbServiceImpl::GetWatchers(uint32_t tokenId, const std::string &storeName) +{ + AutoCache::Watchers watchers; + syncAgents_.ComputeIfPresent(tokenId, [&storeName, &watchers](auto, SyncAgent &agent) { + auto it = agent.watchers_.find(storeName); + if (it != agent.watchers_.end()) { + watchers = it->second; + } + return true; + }); + return watchers; +} + +void RdbServiceImpl::SyncAgent::ReInit(pid_t pid, const std::string &bundleName) +{ + pid_ = pid; + bundleName_ = bundleName; + watchers_.clear(); + mode_.clear(); +} + int32_t RdbServiceImpl::DoUnSubscribe(const RdbSyncerParam& param) { auto identifier = GenIdentifier(param); @@ -466,8 +551,6 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) int32_t RdbServiceImpl::OnInitialize() { - auto initEvt = std::make_unique(CloudEvent::FEATURE_INIT, CloudEvent::StoreInfo()); - EventCenter::GetInstance().PostEvent(std::move(initEvt)); return RDB_OK; } diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index 39fd62d4..ccc864ae 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -134,13 +134,14 @@ int32_t RdbServiceStub::OnRemoteDoAsync(MessageParcel &data, MessageParcel &repl int32_t RdbServiceStub::OnRemoteDoSubscribe(MessageParcel &data, MessageParcel &reply) { RdbSyncerParam param; - if (!ITypesUtil::Unmarshal(data, param)) { + SubscribeOption option; + if (!ITypesUtil::Unmarshal(data, param, option)) { ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), param.storeName_.c_str()); return IPC_STUB_INVALID_DATA_ERR; } - auto status = DoSubscribe(param); + auto status = DoSubscribe(param, option); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x", status); return IPC_STUB_WRITE_PARCEL_ERR; diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.h b/services/distributeddataservice/service/rdb/rdb_syncer.h index 42602bdb..f5f23885 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -63,6 +63,8 @@ public: static bool GetPassword(const StoreMetaData &metaData, DistributedDB::CipherPassword &password); + static DistributedDB::Query MakeQuery(const RdbPredicates& predicates); + private: std::string GetUserId() const; @@ -92,7 +94,6 @@ private: static void HandleSyncStatus(const std::map>& SyncStatus, SyncResult& result); - static DistributedDB::Query MakeQuery(const RdbPredicates& predicates); static void EqualTo(const RdbPredicateOperation& operation, DistributedDB::Query& query); static void NotEqualTo(const RdbPredicateOperation& operation, DistributedDB::Query& query); static void And(const RdbPredicateOperation& operation, DistributedDB::Query& query); diff --git a/services/distributeddataservice/service/rdb/rdb_watcher.cpp b/services/distributeddataservice/service/rdb/rdb_watcher.cpp new file mode 100644 index 00000000..01572668 --- /dev/null +++ b/services/distributeddataservice/service/rdb/rdb_watcher.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 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 "RdbWatcher" + +#include "rdb_watcher.h" +#include "log_print.h" +#include "error/general_error.h" + +namespace OHOS::DistributedRdb { +using namespace DistributedData; +using Err = DistributedData::GeneralError; +RdbWatcher::RdbWatcher(RdbServiceImpl *rdbService, uint32_t tokenId, const std::string &storeName) + : rdbService_(rdbService), tokenId_(tokenId), storeName_(storeName) +{ +} + +int32_t RdbWatcher::OnChange(Origin origin, const std::string &id) +{ + if (rdbService_ == nullptr) { + return Err::E_ERROR; + } + rdbService_->OnChange(tokenId_, storeName_); + return Err::E_OK; +} + +int32_t RdbWatcher::OnChange(Origin origin, const std::string &id, const std::vector &values) +{ + return Err::E_NOT_SUPPORT; +} +} // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_watcher.h b/services/distributeddataservice/service/rdb/rdb_watcher.h new file mode 100644 index 00000000..65af0216 --- /dev/null +++ b/services/distributeddataservice/service/rdb/rdb_watcher.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 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_DATAMGR_SERVICE_RDB_GENERAL_WATCHER_H +#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_WATCHER_H +#include "rdb_service_impl.h" +#include "store/general_value.h" +#include "store/general_watcher.h" + +namespace OHOS::DistributedRdb { +class RdbServiceImpl; +class RdbWatcher : public DistributedData::GeneralWatcher { +public: + explicit RdbWatcher(RdbServiceImpl *rdbService, uint32_t tokenId, const std::string &storeName); + int32_t OnChange(Origin origin, const std::string &id) override; + int32_t OnChange(Origin origin, const std::string &id, + const std::vector &values) override; + +private: + RdbServiceImpl* rdbService_ {}; + uint32_t tokenId_ = 0; + std::string storeName_ {}; +}; +} // namespace OHOS::DistributedRdb +#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_WATCHER_H -- Gitee From f622f78c81bc34532c6d2a7ff98c6cf61e68a560 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sat, 13 May 2023 20:49:39 +0800 Subject: [PATCH 34/95] fix:change Signed-off-by: htt1997 --- .../app/src/uninstaller/uninstaller_impl.cpp | 112 ++++++++---------- .../app/src/uninstaller/uninstaller_impl.h | 14 ++- 2 files changed, 58 insertions(+), 68 deletions(-) diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index 80f0686a..e8d815a4 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -37,8 +37,13 @@ using namespace OHOS::AppExecFwk; using namespace OHOS::DistributedData; using namespace OHOS::EventFwk; -UninstallEventSubscriber::UninstallEventSubscriber(const CommonEventSubscribeInfo &info) : CommonEventSubscriber(info) +UninstallEventSubscriber::UninstallEventSubscriber(const CommonEventSubscribeInfo &info, + KvStoreDataService *kvStoreDataService) + : CommonEventSubscriber(info), kvStoreDataService_(kvStoreDataService) { + callbacks_ = { { CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED, &UninstallEventSubscriber::OnUninstall }, + { OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED, &UninstallEventSubscriber::OnUninstall }, + { CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED, &UninstallEventSubscriber::OnUpdate } }; } void UninstallEventSubscriber::OnReceiveEvent(const CommonEventData &event) @@ -46,21 +51,57 @@ void UninstallEventSubscriber::OnReceiveEvent(const CommonEventData &event) ZLOGI("Intent Action Rec"); Want want = event.GetWant(); std::string action = want.GetAction(); - callbacks_.ComputeIfPresent(action, [&want](const auto& key, auto &callback) { + auto it = callbacks_.find(action); + if (it != callbacks_.end()) { std::string bundleName = want.GetElement().GetBundleName(); int32_t userId = want.GetIntParam(USER_ID, -1); int32_t appIndex = want.GetIntParam(SANDBOX_APP_INDEX, 0); ZLOGI("bundleName:%{public}s, user:%{public}d, appIndex:%{public}d", bundleName.c_str(), userId, appIndex); - callback(bundleName, userId, appIndex); - return true; - }); + (this->*it->second)(bundleName, userId, appIndex); + } } -int32_t UninstallEventSubscriber::RegisterCallback(const std::string &action, UninstallEventCallback callback) + +void UninstallEventSubscriber::OnUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex) { - callbacks_.InsertOrAssign(action, std::move(callback)); - return Status::SUCCESS; + kvStoreDataService_->OnUninstall(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID()); + std::string prefix = StoreMetaData::GetPrefix( + { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName }); + std::vector storeMetaData; + if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData)) { + ZLOGE("load meta failed!"); + return; + } + for (auto &meta : storeMetaData) { + if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) { + ZLOGI("uninstalled bundleName:%{public}s stordId:%{public}s", bundleName.c_str(), meta.storeId.c_str()); + MetaDataManager::GetInstance().DelMeta(meta.GetKey()); + MetaDataManager::GetInstance().DelMeta(meta.GetSecretKey(), true); + MetaDataManager::GetInstance().DelMeta(meta.GetStrategyKey()); + MetaDataManager::GetInstance().DelMeta(meta.appId, true); + MetaDataManager::GetInstance().DelMeta(meta.GetKeyLocal(), true); + MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true); + PermitDelegate::GetInstance().DelCache(meta.GetKey()); + } + } } +void UninstallEventSubscriber::OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex) +{ + kvStoreDataService_->OnUpdate(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID()); + std::string prefix = StoreMetaData::GetPrefix( + { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName }); + std::vector storeMetaData; + if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData)) { + ZLOGE("load meta failed!"); + return; + } + for (auto &meta : storeMetaData) { + if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) { + ZLOGI("updated bundleName:%{public}s, stordId:%{public}s", bundleName.c_str(), meta.storeId.c_str()); + MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true); + } + } +} UninstallerImpl::~UninstallerImpl() { ZLOGD("destruct"); @@ -90,21 +131,7 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); CommonEventSubscribeInfo info(matchingSkills); - auto subscriber = std::make_shared(info); - auto removedCallback = [kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { - kvStoreDataService->OnUninstall(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID()); - OnUninstall(bundleName, userId, appIndex); - }; - - auto updatedCallback = [kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { - kvStoreDataService->OnUpdate(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID()); - OnUpdate(bundleName, userId, appIndex); - }; - - subscriber->RegisterCallback(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED, removedCallback); - subscriber->RegisterCallback(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED, removedCallback); - subscriber->RegisterCallback(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED, updatedCallback); - + auto subscriber = std::make_shared(info, kvStoreDataService); subscriber_ = subscriber; executors_ = executors; executors_->Execute(GetTask()); @@ -127,43 +154,4 @@ ExecutorPool::Task UninstallerImpl::GetTask() }; } -void UninstallerImpl::OnUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex) -{ - std::string prefix = StoreMetaData::GetPrefix( - { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName }); - std::vector storeMetaData; - if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData)) { - ZLOGE("load meta failed!"); - return; - } - for (auto &meta : storeMetaData) { - if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) { - ZLOGI("uninstalled bundleName:%{public}s stordId:%{public}s", bundleName.c_str(), meta.storeId.c_str()); - MetaDataManager::GetInstance().DelMeta(meta.GetKey()); - MetaDataManager::GetInstance().DelMeta(meta.GetSecretKey(), true); - MetaDataManager::GetInstance().DelMeta(meta.GetStrategyKey()); - MetaDataManager::GetInstance().DelMeta(meta.appId, true); - MetaDataManager::GetInstance().DelMeta(meta.GetKeyLocal(), true); - MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true); - PermitDelegate::GetInstance().DelCache(meta.GetKey()); - } - } -} - -void UninstallerImpl::OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex) -{ - std::string prefix = StoreMetaData::GetPrefix( - { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName }); - std::vector storeMetaData; - if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData)) { - ZLOGE("load meta failed!"); - return; - } - for (auto &meta : storeMetaData) { - if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) { - ZLOGI("updated bundleName:%{public}s, stordId:%{public}s", bundleName.c_str(), meta.storeId.c_str()); - MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true); - } - } -} } // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h index 4de54f49..da7a498b 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h @@ -21,12 +21,12 @@ #include "uninstaller.h" namespace OHOS::DistributedKv { -using UninstallEventCallback = std::function; class UninstallEventSubscriber : public EventFwk::CommonEventSubscriber { public: - explicit UninstallEventSubscriber(const EventFwk::CommonEventSubscribeInfo &info); - int32_t RegisterCallback(const std::string &action, UninstallEventCallback callback); +using UninstallEventCallback = void (UninstallEventSubscriber::*) + (const std::string &bundleName, int32_t userId, int32_t appIndex); + UninstallEventSubscriber(const EventFwk::CommonEventSubscribeInfo &info, KvStoreDataService *kvStoreDataService); ~UninstallEventSubscriber() {} void OnReceiveEvent(const EventFwk::CommonEventData &event) override; @@ -34,8 +34,12 @@ public: private: static constexpr const char *USER_ID = "userId"; static constexpr const char *SANDBOX_APP_INDEX = "sandbox_app_index"; - ConcurrentMap callbacks_; + void OnUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex); + void OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex); + std::map callbacks_; + KvStoreDataService *kvStoreDataService_; }; + class UninstallerImpl : public Uninstaller { public: ~UninstallerImpl(); @@ -47,8 +51,6 @@ public: private: static constexpr int32_t RETRY_TIME = 300; static constexpr int32_t RETRY_INTERVAL = 100; - static void OnUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex); - static void OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex); int32_t retryTime_; ExecutorPool::Task GetTask(); std::shared_ptr subscriber_ {}; -- Gitee From abc4be15a34c17fae611c1c4268b347c26c5b71c Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sat, 13 May 2023 21:50:58 +0800 Subject: [PATCH 35/95] update Signed-off-by: zuojiangjiang --- .../service/cloud/cloud_service_impl.cpp | 3 +-- .../service/rdb/rdb_service_impl.h | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 5a131f5c..4af50389 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -207,7 +207,6 @@ int32_t CloudServiceImpl::GetCloudInfo(uint32_t tokenId, const std::string &id, int32_t CloudServiceImpl::GetCloudInfoFromMeta(CloudInfo &cloudInfo) { - cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { ZLOGE("no exist meta, user:%{public}d", cloudInfo.user); return ERROR; @@ -305,7 +304,7 @@ void CloudServiceImpl::FeatureInit() if (!DistributedKv::AccountDelegate::GetInstance()->QueryUsers(users) || users.empty()) { return; } - for (const auot &user : users) { + for (const auto &user : users) { if (user == USER_ID) { continue; } diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index 9d3b22b2..af2d788d 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -27,6 +27,7 @@ #include "rdb_notifier_proxy.h" #include "rdb_syncer.h" #include "store_observer.h" +#include "store/auto_cache.h" #include "visibility.h" namespace OHOS::DistributedRdb { class API_EXPORT RdbServiceImpl : public RdbServiceStub { @@ -49,6 +50,8 @@ public: void OnDataChange(pid_t pid, const DistributedDB::StoreChangedData& data); + void OnChange(uint32_t tokenId, const std::string &storeName); + int32_t CreateRDBTable( const RdbSyncerParam ¶m, const std::string &writePermission, const std::string &readPermission) override; int32_t DestroyRDBTable(const RdbSyncerParam ¶m) override; @@ -67,11 +70,22 @@ protected: int32_t DoAsync(const RdbSyncerParam& param, uint32_t seqNum, const SyncOption& option, const RdbPredicates& predicates) override; - int32_t DoSubscribe(const RdbSyncerParam& param) override; + int32_t DoSubscribe(const RdbSyncerParam& param, const SubscribeOption &option) override; int32_t DoUnSubscribe(const RdbSyncerParam& param) override; private: + using Watchers = DistributedData::AutoCache::Watchers; + struct SyncAgent { + pid_t pid_ = 0; + std::string bundleName_; + std::map mode_; + std::map watchers_; + void ReInit(pid_t pid, const std::string &bundleName); + }; + + Watchers GetWatchers(uint32_t tokenId, const std::string &storeName); + std::string GenIdentifier(const RdbSyncerParam& param); bool CheckAccess(const std::string& bundleName, const std::string& storeName); @@ -114,6 +128,7 @@ private: static constexpr int32_t MAX_SYNCER_PER_PROCESS = 10; static constexpr int32_t SYNCER_TIMEOUT = 60 * 1000; // ms std::shared_ptr executors_; + ConcurrentMap syncAgents_; }; } // namespace OHOS::DistributedRdb #endif -- Gitee From 0a503385483d2a12a54efc6ffe7e12597724b841 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sat, 13 May 2023 22:02:07 +0800 Subject: [PATCH 36/95] fit:some change Signed-off-by: htt1997 --- .../app/src/uninstaller/uninstaller_impl.cpp | 2 -- .../app/src/uninstaller/uninstaller_impl.h | 1 - .../framework/include/store/auto_cache.h | 2 +- .../distributeddataservice/framework/store/auto_cache.cpp | 6 +++--- .../distributeddataservice/service/rdb/rdb_service_stub.cpp | 6 +++--- 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index e8d815a4..f0f570f5 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -28,7 +28,6 @@ #include "metadata/store_meta_data.h" #include "permit_delegate.h" #include "cloud/cloud_info.h" -#include "utils/block_integer.h" namespace OHOS::DistributedKv { using namespace OHOS::AppDistributedKv; @@ -153,5 +152,4 @@ ExecutorPool::Task UninstallerImpl::GetTask() executors_->Schedule(std::chrono::milliseconds(RETRY_INTERVAL), GetTask()); }; } - } // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h index da7a498b..3922fbad 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h @@ -21,7 +21,6 @@ #include "uninstaller.h" namespace OHOS::DistributedKv { - class UninstallEventSubscriber : public EventFwk::CommonEventSubscriber { public: using UninstallEventCallback = void (UninstallEventSubscriber::*) diff --git a/services/distributeddataservice/framework/include/store/auto_cache.h b/services/distributeddataservice/framework/include/store/auto_cache.h index b01834a5..ca1a5e80 100644 --- a/services/distributeddataservice/framework/include/store/auto_cache.h +++ b/services/distributeddataservice/framework/include/store/auto_cache.h @@ -44,7 +44,7 @@ public: API_EXPORT void Bind(std::shared_ptr executor); - API_EXPORT Store GetStore(const StoreMetaData &meta, const Watchers &watchers, bool setWatchers = true); + API_EXPORT Store GetStore(const StoreMetaData &meta, const Watchers &watchers); API_EXPORT void CloseStore(uint32_t tokenId, const std::string &storeId); diff --git a/services/distributeddataservice/framework/store/auto_cache.cpp b/services/distributeddataservice/framework/store/auto_cache.cpp index 37aff132..db51ad5c 100644 --- a/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/services/distributeddataservice/framework/store/auto_cache.cpp @@ -52,7 +52,7 @@ AutoCache::~AutoCache() } } -AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers &watchers, bool setWatchers) +AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers &watchers) { Store store; if (meta.storeType >= MAX_CREATOR_NUM || meta.storeType < 0 || !creators_[meta.storeType]) { @@ -60,10 +60,10 @@ AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers & } stores_.Compute(meta.tokenId, - [this, &meta, &watchers, &store, setWatchers](auto &, std::map &stores) -> bool { + [this, &meta, &watchers, &store](auto &, std::map &stores) -> bool { auto it = stores.find(meta.storeId); if (it != stores.end()) { - if (setWatchers) { + if (!watchers.empty()) { it->second.SetObservers(watchers); } store = it->second; diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index 39fd62d4..7e1bcdb4 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -60,9 +60,9 @@ int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel { RdbSyncerParam param; sptr notifier; - if (!ITypesUtil::Unmarshal(data, param, notifier) || param.bundleName_.empty() || notifier == nullptr) { - ZLOGE("Unmarshal bundleName:%{public}s notifier is nullptr:%{public}d", param.bundleName_.c_str(), - notifier == nullptr); + if (!ITypesUtil::Unmarshal(data, param, notifier) || notifier == nullptr) { + ZLOGE("Unmarshal bundleName:%{public}s storeName_:%{public}s notifier is nullptr:%{public}d", + param.bundleName_.c_str(), param.storeName_.c_str(), notifier == nullptr); return IPC_STUB_INVALID_DATA_ERR; } auto status = InitNotifier(param, notifier); -- Gitee From 1fab541689be5d696b042ad92eca68a1813b54fe Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sat, 13 May 2023 22:20:57 +0800 Subject: [PATCH 37/95] fit:some change Signed-off-by: htt1997 --- .../distributeddataservice/service/cloud/cloud_service_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index e6875348..a77ef04a 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -271,7 +271,7 @@ void CloudServiceImpl::GetSchema(const Event &event) rdbEvent.GetStoreInfo().instanceId); AutoCache::Watchers watchers; - auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers, false); + auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers); if (store == nullptr) { ZLOGE("store is nullptr"); return; -- Gitee From 728009dedd1de229de3634fe27c063a1fa370b80 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sat, 13 May 2023 22:25:59 +0800 Subject: [PATCH 38/95] fit:some change Signed-off-by: htt1997 --- .../app/src/uninstaller/uninstaller_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index f0f570f5..8e45606d 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -56,7 +56,7 @@ void UninstallEventSubscriber::OnReceiveEvent(const CommonEventData &event) int32_t userId = want.GetIntParam(USER_ID, -1); int32_t appIndex = want.GetIntParam(SANDBOX_APP_INDEX, 0); ZLOGI("bundleName:%{public}s, user:%{public}d, appIndex:%{public}d", bundleName.c_str(), userId, appIndex); - (this->*it->second)(bundleName, userId, appIndex); + (this->(*it->second))(bundleName, userId, appIndex); } } -- Gitee From 7367fdd0cb25bf5049f222b86290e9c4747c9d24 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sat, 13 May 2023 22:34:55 +0800 Subject: [PATCH 39/95] fit:some change Signed-off-by: htt1997 --- .../app/src/uninstaller/uninstaller_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index 8e45606d..cb6142eb 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -56,7 +56,7 @@ void UninstallEventSubscriber::OnReceiveEvent(const CommonEventData &event) int32_t userId = want.GetIntParam(USER_ID, -1); int32_t appIndex = want.GetIntParam(SANDBOX_APP_INDEX, 0); ZLOGI("bundleName:%{public}s, user:%{public}d, appIndex:%{public}d", bundleName.c_str(), userId, appIndex); - (this->(*it->second))(bundleName, userId, appIndex); + (this->*(it->second))(bundleName, userId, appIndex); } } -- Gitee From ff8ac6419a717fb05a7a1f44a13d1e384ce75ed3 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sun, 14 May 2023 10:39:31 +0800 Subject: [PATCH 40/95] update Signed-off-by: zuojiangjiang --- .../service/cloud/cloud_service_impl.cpp | 7 ++++++- .../service/cloud/cloud_service_impl.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 4af50389..4d239517 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -52,13 +52,18 @@ CloudServiceImpl::Factory::~Factory() {} CloudServiceImpl::CloudServiceImpl() { - FeatureInit(); EventCenter::GetInstance().Subscribe(CloudEvent::GET_SCHEMA, [this](const Event &event) { GetSchema(event); return; }); } +int32_t CloudServiceImpl::OnInitialize() +{ + FeatureInit(); + return SUCCESS; +} + int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &switches) { CloudInfo cloudInfo; diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 8b79e73b..ec5bc9ce 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -33,6 +33,7 @@ public: int32_t ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) override; int32_t Clean(const std::string &id, const std::map &actions) override; int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) override; + int32_t OnInitialize() override; private: static const inline int USER_ID = 0; -- Gitee From 65607b7295dd4fee72ccad0f5c5593f48b9d6479 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sun, 14 May 2023 10:42:28 +0800 Subject: [PATCH 41/95] update Signed-off-by: zuojiangjiang --- services/distributeddataservice/service/rdb/rdb_service_impl.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 1250c43f..525aed43 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -98,7 +98,6 @@ RdbServiceImpl::RdbServiceImpl() : autoLaunchObserver_(this) meta.user = std::to_string(storeInfo.user); meta.instanceId = storeInfo.instanceId; meta.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; - ZLOGE("meta key:%{public}s", meta.GetKey().c_str()); if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta)) { ZLOGE("meta empty, bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), meta.storeId.c_str()); -- Gitee From 1654fd0002be56ceca257ba68e0b9249d2c64ad4 Mon Sep 17 00:00:00 2001 From: hanlu Date: Sun, 14 May 2023 10:46:35 +0800 Subject: [PATCH 42/95] f Signed-off-by: hanlu --- .../service/data_share/common/rdb_delegate.cpp | 3 +-- .../service/data_share/common/scheduler_manager.cpp | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 0e8a5b95..8a0287a4 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -24,8 +24,7 @@ namespace OHOS::DataShare { constexpr static int32_t MAX_RESULTSET_COUNT = 16; std::atomic RdbDelegate::resultSetCount = 0; -enum REMIND_TIMER_ARGS : int32_t -{ +enum REMIND_TIMER_ARGS : int32_t { ARG_DB_PATH = 0, ARG_VERSION, ARG_URI, diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp index 52501eee..85d7e530 100644 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp @@ -71,8 +71,7 @@ void SchedulerManager::SetTimer(const std::string &dbPath, int version, const Ke return; } // not find task in map, create new timer - // auto taskId = scheduler_->At(TaskScheduler::Clock::now() + std::chrono::seconds(reminderTime - time(nullptr)), - auto taskId = scheduler_->At(TaskScheduler::Clock::now() + std::chrono::seconds(7), + auto taskId = scheduler_->At(TaskScheduler::Clock::now() + std::chrono::seconds(reminderTime - time(nullptr)), [key, dbPath, version, this]() { timerCache_.erase(key); // 1. execute schedulerSQL in next time -- Gitee From 20d8bf350337594e8dfc6deeea4e488ac5d2c971 Mon Sep 17 00:00:00 2001 From: hanlu Date: Sun, 14 May 2023 14:06:47 +0800 Subject: [PATCH 43/95] f Signed-off-by: hanlu --- .../framework/include/serializable/serializable.h | 7 ++++++- .../service/data_share/data_share_service_impl.cpp | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/framework/include/serializable/serializable.h b/services/distributeddataservice/framework/include/serializable/serializable.h index 3dda5678..8a729354 100644 --- a/services/distributeddataservice/framework/include/serializable/serializable.h +++ b/services/distributeddataservice/framework/include/serializable/serializable.h @@ -228,7 +228,12 @@ template bool Serializable::ReadVariant(const json &node, const std::string &name, uint32_t step, uint32_t index, _OutTp &output) { if (step == index) { - return Serializable::GetValue(node, name, std::get<_First>(output)); + _First result; + if(!Serializable::GetValue(node, name, result)) { + return false; + } + output = result; + return true; } return Serializable::ReadVariant<_OutTp, _Rest...>(node, name, step + 1, index, output); } diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 14b957db..8cc30e87 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -337,6 +337,11 @@ enum DataShareKvStoreType : int32_t { int32_t DataShareServiceImpl::OnInitialize() { auto token = IPCSkeleton::GetCallingTokenID(); + auto type = OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(token); + if (type != OHOS::Security::AccessToken::TOKEN_NATIVE && type != OHOS::Security::AccessToken::TOKEN_SHELL) { + ZLOGE("hanlu init app"); + return EOK; + } const std::string accountId = DistributedKv::AccountDelegate::GetInstance()->GetCurrentAccountId(); const auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(token); DistributedData::StoreMetaData saveMeta; -- Gitee From 71bc84f26b077e3f41dda8e6fb4e223a9f2daff9 Mon Sep 17 00:00:00 2001 From: hanlu Date: Sun, 14 May 2023 15:03:43 +0800 Subject: [PATCH 44/95] f Signed-off-by: hanlu --- .../framework/include/serializable/serializable.h | 2 +- .../service/data_share/common/rdb_delegate.cpp | 8 ++++---- .../service/data_share/common/rdb_delegate.h | 2 +- .../service/data_share/common/template_manager.cpp | 3 ++- .../service/data_share/data_share_service_impl.cpp | 5 +++++ 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/framework/include/serializable/serializable.h b/services/distributeddataservice/framework/include/serializable/serializable.h index 8a729354..f652a684 100644 --- a/services/distributeddataservice/framework/include/serializable/serializable.h +++ b/services/distributeddataservice/framework/include/serializable/serializable.h @@ -229,7 +229,7 @@ bool Serializable::ReadVariant(const json &node, const std::string &name, uint32 { if (step == index) { _First result; - if(!Serializable::GetValue(node, name, result)) { + if (!Serializable::GetValue(node, name, result)) { return false; } output = result; diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 8a0287a4..174e2343 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -144,21 +144,21 @@ std::string RdbDelegate::Query(const std::string &sql, const std::vector resultSet = store_->QueryByStep(sql, selectionArgs); + auto resultSet = store_->QueryByStep(sql, selectionArgs); if (resultSet == nullptr) { ZLOGE("Query failed %{private}s", sql.c_str()); return nullptr; } - ResultSetJsonFormatter formatter(resultSet); + ResultSetJsonFormatter formatter(std::move(resultSet)); return DistributedData::Serializable::Marshall(formatter); } -std::unique_ptr RdbDelegate::QuerySql(const std::string &sql) +std::shared_ptr RdbDelegate::QuerySql(const std::string &sql) { if (store_ == nullptr) { ZLOGE("store is null"); return nullptr; } - return store_->QuerySql(sql); + return std::move(store_->QuerySql(sql)); } } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.h b/services/distributeddataservice/service/data_share/common/rdb_delegate.h index 2a68d15b..39e2c119 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.h +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.h @@ -39,7 +39,7 @@ public: std::shared_ptr Query(const std::string &tableName, const DataSharePredicates &predicates, const std::vector &columns, int &errCode) override; std::string Query(const std::string &sql, const std::vector &selectionArgs) override; - std::unique_ptr QuerySql(const std::string &sql) override; + std::shared_ptr QuerySql(const std::string &sql) override; private: static std::atomic resultSetCount; diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index e876a77e..7cd18c7d 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -289,7 +289,8 @@ int RdbSubscriberManager::Notify( changeNode.templateId_.subscriberId_ = key.subscriberId; changeNode.templateId_.bundleName_ = key.bundleName; for (const auto &predicate : tpl.predicates_) { - JsonFormatter formatter(predicate.key_, delegate->Query(predicate.selectSql_)); + std::string result = delegate->Query(predicate.selectSql_); + JsonFormatter formatter(predicate.key_, result); changeNode.data_.emplace_back(DistributedData::Serializable::Marshall(formatter)); } diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 8cc30e87..5b7b716d 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -368,6 +368,11 @@ int32_t DataShareServiceImpl::OnInitialize() int32_t DataShareServiceImpl::OnUserChange(uint32_t code, const std::string &user, const std::string &account) { auto token = IPCSkeleton::GetCallingTokenID(); + auto type = OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(token); + if (type != OHOS::Security::AccessToken::TOKEN_NATIVE && type != OHOS::Security::AccessToken::TOKEN_SHELL) { + ZLOGE("hanlu init app"); + return EOK; + } const std::string accountId = DistributedKv::AccountDelegate::GetInstance()->GetCurrentAccountId(); DistributedData::StoreMetaData saveMeta; saveMeta.appType = "default"; -- Gitee From d3999e6f4f6ad9fffe0fdeeb75b6453cd9c5a356 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Sun, 14 May 2023 15:24:20 +0800 Subject: [PATCH 45/95] do subscribe and print the details Signed-off-by: Sven Wang --- .../distributeddataservice/framework/BUILD.gn | 1 + .../framework/cloud/subscription.cpp | 49 ++++++++ .../framework/include/cloud/subscription.h | 35 ++++++ .../service/cloud/cloud_service_impl.cpp | 108 ++++++++++++++++-- .../service/cloud/cloud_service_impl.h | 11 ++ 5 files changed, 196 insertions(+), 8 deletions(-) create mode 100644 services/distributeddataservice/framework/cloud/subscription.cpp create mode 100644 services/distributeddataservice/framework/include/cloud/subscription.h diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index 56855ff8..09a242ba 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -50,6 +50,7 @@ ohos_shared_library("distributeddatasvcfwk") { "cloud/cloud_info.cpp", "cloud/cloud_server.cpp", "cloud/schema_meta.cpp", + "cloud/subscription.cpp", "eventcenter/event.cpp", "eventcenter/event_center.cpp", "feature/feature_system.cpp", diff --git a/services/distributeddataservice/framework/cloud/subscription.cpp b/services/distributeddataservice/framework/cloud/subscription.cpp new file mode 100644 index 00000000..635c8571 --- /dev/null +++ b/services/distributeddataservice/framework/cloud/subscription.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 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 "cloud/subscription.h" +#include "utils/constant.h" +namespace OHOS::DistributedData { +bool Subscription::Marshal(json &node) const +{ + SetValue(node[GET_NAME(userId)], userId); + SetValue(node[GET_NAME(id)], id); + SetValue(node[GET_NAME(expiresTime)], expiresTime); + return true; +} + +bool Subscription::Unmarshal(const json &node) +{ + GetValue(node, GET_NAME(userId), userId); + GetValue(node, GET_NAME(id), id); + GetValue(node, GET_NAME(expiresTime), expiresTime); + return true; +} + +std::string Subscription::GetKey() +{ + return GetKey(userId); +} + +std::string Subscription::GetKey(int32_t userId) +{ + return Constant::Join(PREFIX, Constant::KEY_SEPARATOR, { std::to_string(userId) }); +} + +std::string Subscription::GetPrefix(const std::initializer_list &fields) +{ + return Constant::Join(PREFIX, Constant::KEY_SEPARATOR, fields); +} +} // namespace OHOS::DistributedData \ No newline at end of file diff --git a/services/distributeddataservice/framework/include/cloud/subscription.h b/services/distributeddataservice/framework/include/cloud/subscription.h new file mode 100644 index 00000000..cd86b9de --- /dev/null +++ b/services/distributeddataservice/framework/include/cloud/subscription.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 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_SUBSCRIPTION_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SUBSCRIPTION_H +#include "serializable/serializable.h" +namespace OHOS::DistributedData { +struct API_EXPORT Subscription final : public Serializable { + int32_t userId = 0; + std::string id; + std::map expiresTime; + + bool Marshal(Serializable::json &node) const; + bool Unmarshal(const Serializable::json &node); + std::string GetKey(); + static std::string GetKey(int32_t userId); + static std::string GetPrefix(const std::initializer_list &fields); +private: + static constexpr const char *PREFIX = "CLOUD_SUBSCRIPTION"; + static constexpr uint64_t INVALID_TIME = 0; +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SUBSCRIPTION_H diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index e9e932fc..40d7b712 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -18,9 +18,11 @@ #include "cloud_service_impl.h" #include "account/account_delegate.h" +#include "account_delegate.h" #include "checker/checker_manager.h" #include "cloud/cloud_event.h" #include "cloud/cloud_server.h" +#include "cloud/subscription.h" #include "communicator/device_manager_adapter.h" #include "eventcenter/event_center.h" #include "feature/feature_system.h" @@ -28,21 +30,24 @@ #include "log_print.h" #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" -#include "utils/anonymous.h" #include "store/auto_cache.h" - +#include "utils/anonymous.h" namespace OHOS::CloudData { using namespace DistributedData; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; +using Account = OHOS::DistributedKv::AccountDelegate; __attribute__((used)) CloudServiceImpl::Factory CloudServiceImpl::factory_; CloudServiceImpl::Factory::Factory() noexcept { - FeatureSystem::GetInstance().RegisterCreator(CloudServiceImpl::SERVICE_NAME, [this]() { - if (product_ == nullptr) { - product_ = std::make_shared(); - } - return product_; - }); + FeatureSystem::GetInstance().RegisterCreator( + CloudServiceImpl::SERVICE_NAME, + [this]() { + if (product_ == nullptr) { + product_ = std::make_shared(); + } + return product_; + }, + FeatureSystem::BIND_NOW); } CloudServiceImpl::Factory::~Factory() {} @@ -141,6 +146,28 @@ int32_t CloudServiceImpl::NotifyDataChange(const std::string &id, const std::str return 0; } +int32_t CloudServiceImpl::OnInitialize() +{ + executor_->Execute(GetCloudTask(0, 0)); + return E_OK; +} + +int32_t CloudServiceImpl::OnExecutor(std::shared_ptr executor) +{ + if (executor_ != nullptr || executor == nullptr) { + return E_INVALID_ARGS; + } + + executor_ = std::move(executor); + return E_OK; +} + +int32_t CloudServiceImpl::OnUserChange(uint32_t code, const std::string &user, const std::string &account) +{ + executor_->Execute(GetCloudTask(0, atoi(user.c_str()))); + return E_OK; +} + int32_t CloudServiceImpl::GetCloudInfo(uint32_t tokenId, const std::string &id, CloudInfo &cloudInfo) { cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); @@ -209,6 +236,33 @@ int32_t CloudServiceImpl::GetAppSchema(int32_t user, const std::string &bundleNa return SUCCESS; } +ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry, int32_t user) +{ + return [this, retry, user]() -> void { + if (retry >= RETRY_TIMES) { + return; + } + + bool finished = true; + std::vector users; + if (user == 0) { + finished = Account::GetInstance()->QueryUsers(users); + } else { + users.push_back(user); + } + + for (auto user : users) { + Subscription subscription; + subscription.userId = user; + MetaDataManager::GetInstance().LoadMeta(subscription.GetKey(), subscription, true); + finished = Subscribe(subscription) && finished; + } + if (!finished) { + executor_->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetCloudTask(retry + 1, user)); + } + }; +} + SchemaMeta CloudServiceImpl::GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId) { SchemaMeta schemaMeta; @@ -291,4 +345,42 @@ void CloudServiceImpl::GetSchema(const Event &event) } return; } + +bool CloudServiceImpl::Subscribe(const Subscription &subscription) +{ + CloudInfo cloudInfo; + cloudInfo.user = subscription.userId; + auto exits = MetaDataManager::GetInstance().LoadMeta(cloudInfo.id, cloudInfo, true); + if (!exits) { + ZLOGW("error, there is no cloud info for user(%{public}d)", subscription.userId); + return false; + } + + ZLOGD("begin to subscribe user:%{public}d apps:%{public}zu", subscription.userId, cloudInfo.apps.size()); + if (CloudServer::GetInstance() == nullptr || cloudInfo.apps.empty()) { + return true; + } + + bool finished = true; + auto now = std::chrono::system_clock::now() + std::chrono::hours(7 * 24); + std::map> dbs; + for (auto &app : cloudInfo.apps) { + auto it = subscription.expiresTime.find(app.bundleName); + if (it != subscription.expiresTime.end() && it->second <= now.time_since_epoch().count()) { + continue; + } + + SchemaMeta schemaMeta; + exits = MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(app.bundleName), schemaMeta, true); + if (exits) { + dbs[it->first] = std::move(schemaMeta.databases); + continue; + } + finished = false; + } + ZLOGI("Subscribe user%{public}d, size:%{public}zu", subscription.userId, dbs.size()); + ZLOGD("Subscribe user%{public}d, details:%{public}s", subscription.userId, Serializable::Marshall(dbs).c_str()); + auto ret = CloudServer::GetInstance()->Subscribe(subscription.userId, dbs); + return (ret == E_OK && finished); +} } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 26ed17e7..67d33612 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -21,6 +21,7 @@ #include "cloud/cloud_info.h" #include "cloud/schema_meta.h" #include "cloud/cloud_event.h" +#include "cloud/subscription.h" namespace OHOS::CloudData { class CloudServiceImpl : public CloudServiceStub { @@ -33,6 +34,9 @@ public: int32_t ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) override; int32_t Clean(const std::string &id, const std::map &actions) override; int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) override; + int32_t OnInitialize() override; + int32_t OnExecutor(std::shared_ptr executor) override; + int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; private: class Factory { @@ -47,6 +51,10 @@ private: using CloudInfo = DistributedData::CloudInfo; using SchemaMeta = DistributedData::SchemaMeta; using Event = DistributedData::Event; + using Subscription = DistributedData::Subscription; + + static constexpr int32_t RETRY_TIMES = 10; + static constexpr int32_t RETRY_INTERVAL = 30; void UpdateCloudInfo(CloudInfo &cloudInfo); void AddSchema(CloudInfo &cloudInfo); @@ -58,6 +66,9 @@ private: int32_t GetAppSchema(int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta); void FeatureInit(const Event &event); void GetSchema(const Event &event); + ExecutorPool::Task GetCloudTask(int32_t retry, int32_t user); + bool Subscribe(const Subscription &subscription); + std::shared_ptr executor_; }; } // namespace OHOS::DistributedData -- Gitee From 4511389f446928b869274ba2c0f3f28e3c27e8b1 Mon Sep 17 00:00:00 2001 From: hanlu Date: Sun, 14 May 2023 15:30:05 +0800 Subject: [PATCH 46/95] f Signed-off-by: hanlu --- .../service/data_share/common/db_delegate.h | 2 +- .../service/data_share/common/rdb_delegate.cpp | 7 ++++--- .../strategies/general/load_config_data_info_strategy.cpp | 2 +- .../data_share/strategies/general/permission_strategy.cpp | 1 + .../service/data_share/strategies/subscribe_strategy.cpp | 3 ++- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.h b/services/distributeddataservice/service/data_share/common/db_delegate.h index d8b9df90..fd81af57 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.h +++ b/services/distributeddataservice/service/data_share/common/db_delegate.h @@ -38,7 +38,7 @@ public: const DataSharePredicates &predicates, const std::vector &columns, int &errCode) = 0; virtual std::string Query( const std::string &sql, const std::vector &selectionArgs = std::vector()) = 0; - virtual std::unique_ptr QuerySql(const std::string &sql) = 0; + virtual std::shared_ptr QuerySql(const std::string &sql) = 0; }; class Id : public DistributedData::Serializable { diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 174e2343..383fac2a 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -142,12 +142,12 @@ std::string RdbDelegate::Query(const std::string &sql, const std::vectorQueryByStep(sql, selectionArgs); if (resultSet == nullptr) { ZLOGE("Query failed %{private}s", sql.c_str()); - return nullptr; + return ""; } ResultSetJsonFormatter formatter(std::move(resultSet)); return DistributedData::Serializable::Marshall(formatter); @@ -159,6 +159,7 @@ std::shared_ptr RdbDelegate::QuerySql(const std:: ZLOGE("store is null"); return nullptr; } - return std::move(store_->QuerySql(sql)); + auto result = store_->QuerySql(sql); + return std::move(result); } } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp index 39285621..0aab4f83 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp @@ -49,7 +49,7 @@ static bool QueryMetaData(const std::string &bundleName, const std::string &stor bool LoadConfigNormalDataInfoStrategy::operator()(std::shared_ptr context) { if (context->type != "rdb") { - return false; + return true; } DistributedData::StoreMetaData metaData; if (!QueryMetaData(context->calledBundleName, context->calledStoreName, metaData, context->currentUserId)) { diff --git a/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.cpp index 86a4ffba..49823bd8 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.cpp @@ -23,6 +23,7 @@ namespace OHOS::DataShare { bool PermissionStrategy::operator()(std::shared_ptr context) { if (context->permission == "reject") { + ZLOGE("reject permission"); return false; } if (!context->permission.empty()) { diff --git a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp index cdad8087..ef9ad6b0 100644 --- a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp @@ -49,7 +49,8 @@ Strategy *SubscribeStrategy::GetStrategy() std::initializer_list list = { new (std::nothrow)LoadConfigCommonStrategy(), new (std::nothrow)LoadConfigFromDataProxyNodeStrategy(), - new (std::nothrow)PermissionStrategy() + new (std::nothrow)PermissionStrategy(), + new (std::nothrow)LoadConfigDataInfoStrategy() }; auto ret = strategies.Init(list); if (!ret) { -- Gitee From e75d999b0c156f2a264b059bba6761ea974fa1a8 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Sun, 14 May 2023 15:38:19 +0800 Subject: [PATCH 47/95] fixed the not support cloud server scene Signed-off-by: Sven Wang --- .../service/cloud/cloud_service_impl.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 40d7b712..95f89d4c 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -348,6 +348,11 @@ void CloudServiceImpl::GetSchema(const Event &event) bool CloudServiceImpl::Subscribe(const Subscription &subscription) { + if (CloudServer::GetInstance() == nullptr) { + ZLOGI("not support cloud server"); + return true; + } + CloudInfo cloudInfo; cloudInfo.user = subscription.userId; auto exits = MetaDataManager::GetInstance().LoadMeta(cloudInfo.id, cloudInfo, true); @@ -357,10 +362,6 @@ bool CloudServiceImpl::Subscribe(const Subscription &subscription) } ZLOGD("begin to subscribe user:%{public}d apps:%{public}zu", subscription.userId, cloudInfo.apps.size()); - if (CloudServer::GetInstance() == nullptr || cloudInfo.apps.empty()) { - return true; - } - bool finished = true; auto now = std::chrono::system_clock::now() + std::chrono::hours(7 * 24); std::map> dbs; @@ -380,7 +381,10 @@ bool CloudServiceImpl::Subscribe(const Subscription &subscription) } ZLOGI("Subscribe user%{public}d, size:%{public}zu", subscription.userId, dbs.size()); ZLOGD("Subscribe user%{public}d, details:%{public}s", subscription.userId, Serializable::Marshall(dbs).c_str()); - auto ret = CloudServer::GetInstance()->Subscribe(subscription.userId, dbs); - return (ret == E_OK && finished); + if (!dbs.empty()) { + auto ret = CloudServer::GetInstance()->Subscribe(subscription.userId, dbs); + finished = (ret == E_OK) ? finished : false; + } + return finished; } } // namespace OHOS::CloudData \ No newline at end of file -- Gitee From b8602dd0272e8bd44c724a0be859b6792785632b Mon Sep 17 00:00:00 2001 From: hanlu Date: Sun, 14 May 2023 16:18:00 +0800 Subject: [PATCH 48/95] y Signed-off-by: hanlu --- .../service/data_share/common/kv_delegate.cpp | 1 - .../service/data_share/data_share_service_impl.cpp | 4 ---- 2 files changed, 5 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp index f75a3d6d..fe645eba 100644 --- a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp @@ -59,7 +59,6 @@ bool KvDelegate::Init() if (isInitDone_) { return true; } - ZLOGE("hanlu init %{public}s", path_.c_str()); int status = GRD_DBOpen((path_ + "/dataShare.db").c_str(), nullptr, GRD_DB_OPEN_CREATE, &db_); if (status != GRD_OK || db_ == nullptr) { ZLOGE("GRD_DBOpen failed,status %{public}d", status); diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 5b7b716d..d4bb77ee 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -339,7 +339,6 @@ int32_t DataShareServiceImpl::OnInitialize() auto token = IPCSkeleton::GetCallingTokenID(); auto type = OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(token); if (type != OHOS::Security::AccessToken::TOKEN_NATIVE && type != OHOS::Security::AccessToken::TOKEN_SHELL) { - ZLOGE("hanlu init app"); return EOK; } const std::string accountId = DistributedKv::AccountDelegate::GetInstance()->GetCurrentAccountId(); @@ -360,7 +359,6 @@ int32_t DataShareServiceImpl::OnInitialize() saveMeta.uid = IPCSkeleton::GetCallingUid(); saveMeta.storeType = DATA_SHARE_SINGLE_VERSION; saveMeta.dataDir = DistributedData::DirectoryManager::GetInstance().GetStorePath(saveMeta); - ZLOGE("hanlu init %{public}s", saveMeta.dataDir.c_str()); KvDBDelegate::GetInstance(false, saveMeta.dataDir); return EOK; } @@ -370,7 +368,6 @@ int32_t DataShareServiceImpl::OnUserChange(uint32_t code, const std::string &use auto token = IPCSkeleton::GetCallingTokenID(); auto type = OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(token); if (type != OHOS::Security::AccessToken::TOKEN_NATIVE && type != OHOS::Security::AccessToken::TOKEN_SHELL) { - ZLOGE("hanlu init app"); return EOK; } const std::string accountId = DistributedKv::AccountDelegate::GetInstance()->GetCurrentAccountId(); @@ -390,7 +387,6 @@ int32_t DataShareServiceImpl::OnUserChange(uint32_t code, const std::string &use saveMeta.uid = IPCSkeleton::GetCallingUid(); saveMeta.storeType = DATA_SHARE_SINGLE_VERSION; saveMeta.dataDir = DistributedData::DirectoryManager::GetInstance().GetStorePath(saveMeta); - ZLOGE("hanlu init2 %{public}s", saveMeta.dataDir.c_str()); KvDBDelegate::GetInstance(false, saveMeta.dataDir); return EOK; } -- Gitee From 830743e4c430a49f5e47563b1690b51d415ce954 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Sun, 14 May 2023 16:48:34 +0800 Subject: [PATCH 49/95] add unsubscribe scene Signed-off-by: Sven Wang --- .../service/cloud/cloud_service_impl.cpp | 50 +++++++++++++------ .../service/cloud/cloud_service_impl.h | 3 +- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 95f89d4c..23a74da9 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -85,6 +85,7 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::mapExecute(GetCloudTask(0, cloudInfo.user)); return SUCCESS; } @@ -98,6 +99,7 @@ int32_t CloudServiceImpl::DisableCloud(const std::string &id) if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { return ERROR; } + executor_->Execute(GetCloudTask(0, cloudInfo.user)); return SUCCESS; } @@ -119,6 +121,7 @@ int32_t CloudServiceImpl::ChangeAppSwitch(const std::string &id, const std::stri if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { return ERROR; } + executor_->Execute(GetCloudTask(0, cloudInfo.user)); return SUCCESS; } @@ -255,7 +258,7 @@ ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry, int32_t user) Subscription subscription; subscription.userId = user; MetaDataManager::GetInstance().LoadMeta(subscription.GetKey(), subscription, true); - finished = Subscribe(subscription) && finished; + finished = DoSubscribe(subscription) && finished; } if (!finished) { executor_->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetCloudTask(retry + 1, user)); @@ -346,7 +349,7 @@ void CloudServiceImpl::GetSchema(const Event &event) return; } -bool CloudServiceImpl::Subscribe(const Subscription &subscription) +bool CloudServiceImpl::DoSubscribe(const Subscription &subscription) { if (CloudServer::GetInstance() == nullptr) { ZLOGI("not support cloud server"); @@ -361,30 +364,45 @@ bool CloudServiceImpl::Subscribe(const Subscription &subscription) return false; } - ZLOGD("begin to subscribe user:%{public}d apps:%{public}zu", subscription.userId, cloudInfo.apps.size()); - bool finished = true; - auto now = std::chrono::system_clock::now() + std::chrono::hours(7 * 24); - std::map> dbs; + ZLOGD("begin cloud:%{public}d user:%{public}d apps:%{public}zu", cloudInfo.enableCloud, subscription.userId, + cloudInfo.apps.size()); + auto onThreshold = (std::chrono::system_clock::now() + std::chrono::hours(EXPIRE_INTERVAL)).time_since_epoch(); + auto offThreshold = std::chrono::system_clock::now().time_since_epoch(); + std::map> subDbs; + std::map> unsubDbs; for (auto &app : cloudInfo.apps) { + auto enabled = cloudInfo.enableCloud && app.cloudSwitch; + auto &dbs = enabled ? subDbs : unsubDbs; auto it = subscription.expiresTime.find(app.bundleName); - if (it != subscription.expiresTime.end() && it->second <= now.time_since_epoch().count()) { + + // cloud is enabled, but the subscription won't expire + if (enabled && (it != subscription.expiresTime.end() && it->second >= onThreshold.count())) { + continue; + } + // cloud is disabled, we don't care the subscription which was expired or didn't subscribe. + if (!enabled && (it == subscription.expiresTime.end() || it->second <= offThreshold.count())) { continue; } SchemaMeta schemaMeta; exits = MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(app.bundleName), schemaMeta, true); - if (exits) { - dbs[it->first] = std::move(schemaMeta.databases); + if (!exits) { continue; } - finished = false; + dbs[it->first] = std::move(schemaMeta.databases); + } + + ZLOGI("cloud switch:%{public}d user%{public}d, sub:%{public}zu, unsub:%{public}zu", cloudInfo.enableCloud, + subscription.userId, subDbs.size(), unsubDbs.size()); + ZLOGD("Subscribe user%{public}d, details:%{public}s", subscription.userId, Serializable::Marshall(subDbs).c_str()); + ZLOGD("Unsubscribe user%{public}d, details:%{public}s", subscription.userId, + Serializable::Marshall(unsubDbs).c_str()); + if (!subDbs.empty()) { + CloudServer::GetInstance()->Subscribe(subscription.userId, subDbs); } - ZLOGI("Subscribe user%{public}d, size:%{public}zu", subscription.userId, dbs.size()); - ZLOGD("Subscribe user%{public}d, details:%{public}s", subscription.userId, Serializable::Marshall(dbs).c_str()); - if (!dbs.empty()) { - auto ret = CloudServer::GetInstance()->Subscribe(subscription.userId, dbs); - finished = (ret == E_OK) ? finished : false; + if (!unsubDbs.empty()) { + CloudServer::GetInstance()->Subscribe(subscription.userId, unsubDbs); } - return finished; + return subDbs.empty() && unsubDbs.empty(); } } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 67d33612..b43655ff 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -55,6 +55,7 @@ private: static constexpr int32_t RETRY_TIMES = 10; static constexpr int32_t RETRY_INTERVAL = 30; + static constexpr int32_t EXPIRE_INTERVAL = 7 * 24; // 7 day void UpdateCloudInfo(CloudInfo &cloudInfo); void AddSchema(CloudInfo &cloudInfo); @@ -67,7 +68,7 @@ private: void FeatureInit(const Event &event); void GetSchema(const Event &event); ExecutorPool::Task GetCloudTask(int32_t retry, int32_t user); - bool Subscribe(const Subscription &subscription); + bool DoSubscribe(const Subscription &subscription); std::shared_ptr executor_; }; } // namespace OHOS::DistributedData -- Gitee From 07b1cb2d21911cd952764fd44f8bdebbe3920a82 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Sun, 14 May 2023 16:55:04 +0800 Subject: [PATCH 50/95] fixed code style Signed-off-by: Sven Wang --- .../distributeddataservice/service/cloud/cloud_service_impl.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 23a74da9..0a894588 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -374,7 +374,6 @@ bool CloudServiceImpl::DoSubscribe(const Subscription &subscription) auto enabled = cloudInfo.enableCloud && app.cloudSwitch; auto &dbs = enabled ? subDbs : unsubDbs; auto it = subscription.expiresTime.find(app.bundleName); - // cloud is enabled, but the subscription won't expire if (enabled && (it != subscription.expiresTime.end() && it->second >= onThreshold.count())) { continue; -- Gitee From cfe3a02855fc7dae6daa756d9bd093a3fd024f37 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sun, 14 May 2023 17:29:39 +0800 Subject: [PATCH 51/95] update Signed-off-by: zuojiangjiang --- .../distributeddataservice/service/cloud/cloud_service_impl.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index a5527354..26fcf7f3 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -18,7 +18,6 @@ #include "cloud_service_impl.h" #include "account/account_delegate.h" -#include "account_delegate.h" #include "checker/checker_manager.h" #include "cloud/cloud_event.h" #include "cloud/cloud_server.h" -- Gitee From 2b6384b986c49cc6744543cce7f0115b87ef4ac2 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Sun, 14 May 2023 17:30:27 +0800 Subject: [PATCH 52/95] remove unused header Signed-off-by: Sven Wang --- .../distributeddataservice/service/cloud/cloud_service_impl.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 0a894588..89093551 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -18,7 +18,6 @@ #include "cloud_service_impl.h" #include "account/account_delegate.h" -#include "account_delegate.h" #include "checker/checker_manager.h" #include "cloud/cloud_event.h" #include "cloud/cloud_server.h" -- Gitee From 3d1aadb7fcf61b85523f70edc2b17e123809c536 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Sun, 14 May 2023 17:51:07 +0800 Subject: [PATCH 53/95] add nullptr check Signed-off-by: Sven Wang --- .../service/cloud/cloud_service_impl.cpp | 23 +++++++++++++------ .../service/cloud/cloud_service_impl.h | 1 + 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 89093551..cf4a5b25 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -84,7 +84,7 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::mapExecute(GetCloudTask(0, cloudInfo.user)); + Execute(GetCloudTask(0, cloudInfo.user)); return SUCCESS; } @@ -98,7 +98,7 @@ int32_t CloudServiceImpl::DisableCloud(const std::string &id) if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { return ERROR; } - executor_->Execute(GetCloudTask(0, cloudInfo.user)); + Execute(GetCloudTask(0, cloudInfo.user)); return SUCCESS; } @@ -120,7 +120,7 @@ int32_t CloudServiceImpl::ChangeAppSwitch(const std::string &id, const std::stri if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { return ERROR; } - executor_->Execute(GetCloudTask(0, cloudInfo.user)); + Execute(GetCloudTask(0, cloudInfo.user)); return SUCCESS; } @@ -150,7 +150,7 @@ int32_t CloudServiceImpl::NotifyDataChange(const std::string &id, const std::str int32_t CloudServiceImpl::OnInitialize() { - executor_->Execute(GetCloudTask(0, 0)); + Execute(GetCloudTask(0, 0)); return E_OK; } @@ -166,7 +166,7 @@ int32_t CloudServiceImpl::OnExecutor(std::shared_ptr executor) int32_t CloudServiceImpl::OnUserChange(uint32_t code, const std::string &user, const std::string &account) { - executor_->Execute(GetCloudTask(0, atoi(user.c_str()))); + Execute(GetCloudTask(0, atoi(user.c_str()))); return E_OK; } @@ -241,7 +241,8 @@ int32_t CloudServiceImpl::GetAppSchema(int32_t user, const std::string &bundleNa ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry, int32_t user) { return [this, retry, user]() -> void { - if (retry >= RETRY_TIMES) { + auto executor = executor_; + if (retry >= RETRY_TIMES || executor == nullptr) { return; } @@ -260,7 +261,7 @@ ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry, int32_t user) finished = DoSubscribe(subscription) && finished; } if (!finished) { - executor_->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetCloudTask(retry + 1, user)); + executor->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetCloudTask(retry + 1, user)); } }; } @@ -403,4 +404,12 @@ bool CloudServiceImpl::DoSubscribe(const Subscription &subscription) } return subDbs.empty() && unsubDbs.empty(); } +void CloudServiceImpl::Execute(ExecutorPool::Task task) +{ + auto executor = executor_; + if (executor == nullptr) { + return; + } + executor->Execute(std::move(task)); +} } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index b43655ff..2e8b7c9b 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -68,6 +68,7 @@ private: void FeatureInit(const Event &event); void GetSchema(const Event &event); ExecutorPool::Task GetCloudTask(int32_t retry, int32_t user); + void Execute(ExecutorPool::Task task); bool DoSubscribe(const Subscription &subscription); std::shared_ptr executor_; }; -- Gitee From c10da293dbe972902882d8439fa452db4e59c82b Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sun, 14 May 2023 18:03:05 +0800 Subject: [PATCH 54/95] update Signed-off-by: zuojiangjiang --- .../service/cloud/cloud_service_impl.cpp | 24 +++++++++++++------ .../service/cloud/cloud_service_impl.h | 1 + 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 26fcf7f3..ee924220 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -80,7 +80,7 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::mapExecute(GetCloudTask(0, cloudInfo.user)); + Execute(GetCloudTask(0, cloudInfo.user)); return SUCCESS; } @@ -95,7 +95,7 @@ int32_t CloudServiceImpl::DisableCloud(const std::string &id) if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { return ERROR; } - executor_->Execute(GetCloudTask(0, cloudInfo.user)); + Execute(GetCloudTask(0, cloudInfo.user)); return SUCCESS; } @@ -118,7 +118,7 @@ int32_t CloudServiceImpl::ChangeAppSwitch(const std::string &id, const std::stri if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { return ERROR; } - executor_->Execute(GetCloudTask(0, cloudInfo.user)); + Execute(GetCloudTask(0, cloudInfo.user)); return SUCCESS; } @@ -193,7 +193,7 @@ int32_t CloudServiceImpl::NotifyDataChange(const std::string &id, const std::str int32_t CloudServiceImpl::OnInitialize() { FeatureInit(); - executor_->Execute(GetCloudTask(0, 0)); + Execute(GetCloudTask(0, 0)); return E_OK; } @@ -209,7 +209,7 @@ int32_t CloudServiceImpl::OnExecutor(std::shared_ptr executor) int32_t CloudServiceImpl::OnUserChange(uint32_t code, const std::string &user, const std::string &account) { - executor_->Execute(GetCloudTask(0, atoi(user.c_str()))); + Execute(GetCloudTask(0, atoi(user.c_str()))); return E_OK; } @@ -292,7 +292,8 @@ int32_t CloudServiceImpl::GetAppSchema(int32_t user, const std::string &bundleNa ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry, int32_t user) { return [this, retry, user]() -> void { - if (retry >= RETRY_TIMES) { + auto executor = executor_; + if (retry >= RETRY_TIMES || executor == nullptr) { return; } @@ -311,7 +312,7 @@ ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry, int32_t user) finished = DoSubscribe(subscription) && finished; } if (!finished) { - executor_->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetCloudTask(retry + 1, user)); + executor->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetCloudTask(retry + 1, user)); } }; } @@ -459,4 +460,13 @@ bool CloudServiceImpl::DoSubscribe(const Subscription &subscription) } return subDbs.empty() && unsubDbs.empty(); } + +void CloudServiceImpl::Execute(ExecutorPool::Task task) +{ + auto executor = executor_; + if (executor == nullptr) { + return; + } + executor->Execute(std::move(task)); +} } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index e9e3e50c..116a32ce 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -70,6 +70,7 @@ private: void FeatureInit(); void GetSchema(const Event &event); ExecutorPool::Task GetCloudTask(int32_t retry, int32_t user); + void Execute(ExecutorPool::Task task); bool DoSubscribe(const Subscription &subscription); std::shared_ptr executor_; }; -- Gitee From 6cc5839d94004a7a4861414d0a670a44946f5c2d Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Sun, 14 May 2023 20:22:44 +0800 Subject: [PATCH 55/95] add relation info Signed-off-by: Sven Wang --- .../framework/cloud/subscription.cpp | 26 +++++++++++++++++++ .../framework/include/cloud/subscription.h | 15 +++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/framework/cloud/subscription.cpp b/services/distributeddataservice/framework/cloud/subscription.cpp index 635c8571..7bddb52c 100644 --- a/services/distributeddataservice/framework/cloud/subscription.cpp +++ b/services/distributeddataservice/framework/cloud/subscription.cpp @@ -16,6 +16,22 @@ #include "cloud/subscription.h" #include "utils/constant.h" namespace OHOS::DistributedData { +bool Subscription::Relation::Marshal(json &node) const +{ + SetValue(node[GET_NAME(id)], id); + SetValue(node[GET_NAME(bundleName)], bundleName); + SetValue(node[GET_NAME(relations)], relations); + return true; +} + +bool Subscription::Relation::Unmarshal(const json &node) +{ + GetValue(node, GET_NAME(id), id); + GetValue(node, GET_NAME(bundleName), bundleName); + GetValue(node, GET_NAME(relations), relations); + return true; +} + bool Subscription::Marshal(json &node) const { SetValue(node[GET_NAME(userId)], userId); @@ -37,11 +53,21 @@ std::string Subscription::GetKey() return GetKey(userId); } +std::string Subscription::GetRelationKey(const std::string &bundleName) +{ + return GetRelationKey(userId, bundleName); +} + std::string Subscription::GetKey(int32_t userId) { return Constant::Join(PREFIX, Constant::KEY_SEPARATOR, { std::to_string(userId) }); } +std::string Subscription::GetRelationKey(int32_t userId, const std::string &bundleName) +{ + return Constant::Join(RELATION_PREFIX, Constant::KEY_SEPARATOR, { std::to_string(userId), bundleName }); +} + std::string Subscription::GetPrefix(const std::initializer_list &fields) { return Constant::Join(PREFIX, Constant::KEY_SEPARATOR, fields); diff --git a/services/distributeddataservice/framework/include/cloud/subscription.h b/services/distributeddataservice/framework/include/cloud/subscription.h index cd86b9de..c0e8dec5 100644 --- a/services/distributeddataservice/framework/include/cloud/subscription.h +++ b/services/distributeddataservice/framework/include/cloud/subscription.h @@ -22,13 +22,24 @@ struct API_EXPORT Subscription final : public Serializable { std::string id; std::map expiresTime; - bool Marshal(Serializable::json &node) const; - bool Unmarshal(const Serializable::json &node); + struct API_EXPORT Relation final : public Serializable { + std::string id; + std::string bundleName; + std::map relations; + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + }; + + bool Marshal(json &node) const; + bool Unmarshal(const json &node); std::string GetKey(); + std::string GetRelationKey(const std::string &bundleName); static std::string GetKey(int32_t userId); + static std::string GetRelationKey(int32_t userId, const std::string &bundleName); static std::string GetPrefix(const std::initializer_list &fields); private: static constexpr const char *PREFIX = "CLOUD_SUBSCRIPTION"; + static constexpr const char *RELATION_PREFIX = "CLOUD_RELATION"; static constexpr uint64_t INVALID_TIME = 0; }; } // namespace OHOS::DistributedData -- Gitee From 25b4f26d12fe12a94ad7b09fcc4ef5961c0c437f Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Sun, 14 May 2023 20:42:37 +0800 Subject: [PATCH 56/95] update Signed-off-by: Sven Wang --- .../service/cloud/cloud_service_impl.cpp | 37 ++++++++----------- .../service/cloud/cloud_service_impl.h | 2 +- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index cf4a5b25..4fcf3fd6 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -349,7 +349,7 @@ void CloudServiceImpl::GetSchema(const Event &event) return; } -bool CloudServiceImpl::DoSubscribe(const Subscription &subscription) +bool CloudServiceImpl::DoSubscribe(const Subscription &sub) { if (CloudServer::GetInstance() == nullptr) { ZLOGI("not support cloud server"); @@ -357,14 +357,14 @@ bool CloudServiceImpl::DoSubscribe(const Subscription &subscription) } CloudInfo cloudInfo; - cloudInfo.user = subscription.userId; - auto exits = MetaDataManager::GetInstance().LoadMeta(cloudInfo.id, cloudInfo, true); + cloudInfo.user = sub.userId; + auto exits = MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true); if (!exits) { - ZLOGW("error, there is no cloud info for user(%{public}d)", subscription.userId); + ZLOGW("error, there is no cloud info for user(%{public}d)", sub.userId); return false; } - ZLOGD("begin cloud:%{public}d user:%{public}d apps:%{public}zu", cloudInfo.enableCloud, subscription.userId, + ZLOGD("begin cloud:%{public}d user:%{public}d apps:%{public}zu", cloudInfo.enableCloud, sub.userId, cloudInfo.apps.size()); auto onThreshold = (std::chrono::system_clock::now() + std::chrono::hours(EXPIRE_INTERVAL)).time_since_epoch(); auto offThreshold = std::chrono::system_clock::now().time_since_epoch(); @@ -373,37 +373,32 @@ bool CloudServiceImpl::DoSubscribe(const Subscription &subscription) for (auto &app : cloudInfo.apps) { auto enabled = cloudInfo.enableCloud && app.cloudSwitch; auto &dbs = enabled ? subDbs : unsubDbs; - auto it = subscription.expiresTime.find(app.bundleName); + auto it = sub.expiresTime.find(app.bundleName); // cloud is enabled, but the subscription won't expire - if (enabled && (it != subscription.expiresTime.end() && it->second >= onThreshold.count())) { + if (enabled && (it != sub.expiresTime.end() && it->second >= onThreshold.count())) { continue; } // cloud is disabled, we don't care the subscription which was expired or didn't subscribe. - if (!enabled && (it == subscription.expiresTime.end() || it->second <= offThreshold.count())) { + if (!enabled && (it == sub.expiresTime.end() || it->second <= offThreshold.count())) { continue; } SchemaMeta schemaMeta; exits = MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(app.bundleName), schemaMeta, true); - if (!exits) { - continue; + if (exits) { + dbs[it->first] = std::move(schemaMeta.databases); } - dbs[it->first] = std::move(schemaMeta.databases); } ZLOGI("cloud switch:%{public}d user%{public}d, sub:%{public}zu, unsub:%{public}zu", cloudInfo.enableCloud, - subscription.userId, subDbs.size(), unsubDbs.size()); - ZLOGD("Subscribe user%{public}d, details:%{public}s", subscription.userId, Serializable::Marshall(subDbs).c_str()); - ZLOGD("Unsubscribe user%{public}d, details:%{public}s", subscription.userId, - Serializable::Marshall(unsubDbs).c_str()); - if (!subDbs.empty()) { - CloudServer::GetInstance()->Subscribe(subscription.userId, subDbs); - } - if (!unsubDbs.empty()) { - CloudServer::GetInstance()->Subscribe(subscription.userId, unsubDbs); - } + sub.userId, subDbs.size(), unsubDbs.size()); + ZLOGD("Subscribe user%{public}d details:%{public}s", sub.userId, Serializable::Marshall(subDbs).c_str()); + ZLOGD("Unsubscribe user%{public}d details:%{public}s", sub.userId, Serializable::Marshall(unsubDbs).c_str()); + CloudServer::GetInstance()->Subscribe(sub.userId, subDbs); + CloudServer::GetInstance()->Unsubscribe(sub.userId, unsubDbs); return subDbs.empty() && unsubDbs.empty(); } + void CloudServiceImpl::Execute(ExecutorPool::Task task) { auto executor = executor_; diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 2e8b7c9b..638d2db9 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -69,7 +69,7 @@ private: void GetSchema(const Event &event); ExecutorPool::Task GetCloudTask(int32_t retry, int32_t user); void Execute(ExecutorPool::Task task); - bool DoSubscribe(const Subscription &subscription); + bool DoSubscribe(const Subscription &sub); std::shared_ptr executor_; }; } // namespace OHOS::DistributedData -- Gitee From 8359157ec5ac07f9597ad5cb1388dc5becefd17b Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sun, 14 May 2023 22:32:11 +0800 Subject: [PATCH 57/95] gix:save cloudInfo to meta Signed-off-by: htt1997 --- .../service/cloud/cloud_service_impl.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index a77ef04a..52f1a56b 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -217,9 +217,21 @@ SchemaMeta CloudServiceImpl::GetSchemaMata(int32_t userId, const std::string &bu ZLOGE("instance is nullptr"); return schemaMeta; } - auto cloudInfo = instance->GetServerInfo(userId); - if (!cloudInfo.IsValid()) { - ZLOGE("cloudInfo is invalid"); + CloudInfo cloudInfo; + cloudInfo.user = userId; + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { + cloudInfo = instance->GetServerInfo(userId); + if (!cloudInfo.IsValid()) { + ZLOGE("cloudInfo is invalid"); + return schemaMeta; + } + MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); + } + if (std::find_if(cloudInfo.apps.begin(), cloudInfo.apps.end(), + [&bundleName, &instanceId](const CloudInfo::AppInfo &appInfo) { + return appInfo.bundleName == bundleName && appInfo.instanceId == instanceId; + }) == cloudInfo.apps.end()) { + ZLOGE("bundleName:%{public}s instanceId:%{public}d", bundleName.c_str(), instanceId); return schemaMeta; } std::string schemaKey = cloudInfo.GetSchemaKey(bundleName, instanceId); @@ -286,6 +298,7 @@ void CloudServiceImpl::GetSchema(const Event &event) if (database.name != rdbEvent.GetStoreInfo().storeName /* || don't need sync */) { continue; } + ZLOGD("database: %{public}s sync start", database.name.c_str()); auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, database); if (cloudDB != nullptr) { store->Bind(cloudDB); -- Gitee From 4bdce19355f6c73cfb353f3d965e803c8a0e9f51 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sun, 14 May 2023 23:07:23 +0800 Subject: [PATCH 58/95] style:code check Signed-off-by: htt1997 --- .../service/cloud/cloud_service_impl.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 52f1a56b..c6f60a6e 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -228,9 +228,9 @@ SchemaMeta CloudServiceImpl::GetSchemaMata(int32_t userId, const std::string &bu MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); } if (std::find_if(cloudInfo.apps.begin(), cloudInfo.apps.end(), - [&bundleName, &instanceId](const CloudInfo::AppInfo &appInfo) { - return appInfo.bundleName == bundleName && appInfo.instanceId == instanceId; - }) == cloudInfo.apps.end()) { + [&bundleName, &instanceId](const CloudInfo::AppInfo &appInfo) { + return appInfo.bundleName == bundleName && appInfo.instanceId == instanceId; + }) == cloudInfo.apps.end()) { ZLOGE("bundleName:%{public}s instanceId:%{public}d", bundleName.c_str(), instanceId); return schemaMeta; } @@ -279,6 +279,9 @@ void CloudServiceImpl::GetSchema(const Event &event) rdbEvent.GetStoreInfo().instanceId); auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); auto schemaMeta = GetSchemaMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().instanceId); + if (schemaMeta.databases.empty()) { + return; + } auto storeMeta = GetStoreMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, rdbEvent.GetStoreInfo().instanceId); @@ -299,10 +302,7 @@ void CloudServiceImpl::GetSchema(const Event &event) continue; } ZLOGD("database: %{public}s sync start", database.name.c_str()); - auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, database); - if (cloudDB != nullptr) { - store->Bind(cloudDB); - } + //ConnectCloudDB and Bind to store for (auto &table : database.tables) { ZLOGD("table: %{public}s sync start", table.name.c_str()); } -- Gitee From 5aa2bb6994ee0bcd95bf5b745dda4a01ccb1b83e Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sun, 14 May 2023 23:25:54 +0800 Subject: [PATCH 59/95] style:code check Signed-off-by: htt1997 --- .../distributeddataservice/service/cloud/cloud_service_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index c6f60a6e..11c7d183 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -302,7 +302,7 @@ void CloudServiceImpl::GetSchema(const Event &event) continue; } ZLOGD("database: %{public}s sync start", database.name.c_str()); - //ConnectCloudDB and Bind to store + // ConnectCloudDB and Bind to store for (auto &table : database.tables) { ZLOGD("table: %{public}s sync start", table.name.c_str()); } -- Gitee From 795f7783aad855faa1dd45ad72d8b11c10e3f335 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Mon, 15 May 2023 11:09:15 +0800 Subject: [PATCH 60/95] fixed bundleName bugs Signed-off-by: Sven Wang --- .../distributeddataservice/service/cloud/cloud_service_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 4fcf3fd6..3a6b07d0 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -386,7 +386,7 @@ bool CloudServiceImpl::DoSubscribe(const Subscription &sub) SchemaMeta schemaMeta; exits = MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(app.bundleName), schemaMeta, true); if (exits) { - dbs[it->first] = std::move(schemaMeta.databases); + dbs[app.bundleName] = std::move(schemaMeta.databases); } } -- Gitee From 723f63707dd1b4bfb18b6521d066192ee9250ff3 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Mon, 15 May 2023 19:25:41 +0800 Subject: [PATCH 61/95] Fix attribute Signed-off-by: lianhuix --- .../data_share/gaussdb_rd/include/grd_base/grd_type_export.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_type_export.h b/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_type_export.h index baa98144..d116ea22 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_type_export.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_type_export.h @@ -21,7 +21,7 @@ extern "C" { #endif // __cplusplus #ifndef _WIN32 -#define GRD_API __attribute__((visibility("default"), weak)) +#define GRD_API __attribute__((visibility("default"))) #endif typedef struct GRD_DB GRD_DB; -- Gitee From d6419d96ad52cf5f6f9c749a1ce79788901a8f32 Mon Sep 17 00:00:00 2001 From: niudongyao Date: Wed, 17 May 2023 15:38:44 +0800 Subject: [PATCH 62/95] fix warn Signed-off-by: niudongyao --- .../service/data_share/data_share_types_util.h | 4 ++-- .../load_config_from_data_share_bundle_info_strategy.cpp | 2 +- .../strategies/general/connect_extension_strategy.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/data_share/data_share_types_util.h b/services/distributeddataservice/service/data_share/data_share_types_util.h index 4f4ebf9b..b1e0a476 100644 --- a/services/distributeddataservice/service/data_share/data_share_types_util.h +++ b/services/distributeddataservice/service/data_share/data_share_types_util.h @@ -42,7 +42,7 @@ template<> bool Unmarshalling(PublishedDataItem &dataItem, MessageParcel &parcel); template<> -bool Marshalling(const PublishedDataItem &templateId, MessageParcel &parcel); +bool Marshalling(const PublishedDataItem &dataItem, MessageParcel &parcel); template<> bool Unmarshalling(Data &data, MessageParcel &parcel); @@ -63,7 +63,7 @@ template<> bool Marshalling(const OperationResult &operationResult, MessageParcel &parcel); template<> -bool Marshalling(const TemplateId &changeNode, MessageParcel &parcel); +bool Marshalling(const TemplateId &templateId, MessageParcel &parcel); }; #endif // DATASHARESERVICE_DATA_SHARE_TYPES_UTIL_H diff --git a/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp index 5df4f1bb..9ff57d27 100644 --- a/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp @@ -53,7 +53,7 @@ bool LoadConfigFromDataShareBundleInfoStrategy::LoadConfigFromProfile( "/" + context->calledStoreName; std::string tableUri = storeUri + "/" + context->calledTableName; ConfigData result; - for (auto &item : profileInfo.tableConfig) { + for (auto const &item : profileInfo.tableConfig) { if (item.uri == tableUri) { result.SetCrossUserMode(ConfigData::TABLE_MATCH_PRIORITY, item.crossUserMode); continue; diff --git a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp index eecd0c38..1561140c 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp @@ -23,7 +23,7 @@ namespace OHOS::DataShare { bool ConnectExtensionStrategy::operator()(std::shared_ptr context) { - for (auto &extension: context->bundleInfo.extensionInfos) { + for (auto const &extension: context->bundleInfo.extensionInfos) { if (extension.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { return Connect(context); } -- Gitee From 31e0148bdcc8abf61172f4faa7a2f9a6da47b179 Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 18 May 2023 21:47:02 +0800 Subject: [PATCH 63/95] f Signed-off-by: hanlu --- conf/config.json | 3 + services/distributeddataservice/app/BUILD.gn | 1 + .../app/src/feature_stub_impl.cpp | 6 +- .../framework/feature/feature_system.cpp | 2 +- .../include/feature/feature_system.h | 6 +- .../distributeddataservice/service/BUILD.gn | 49 +------- .../service/data_share/BUILD.gn | 110 ++++++++++++++++++ .../data_share/common/rdb_delegate.cpp | 2 +- .../data_share/common/template_manager.cpp | 2 +- .../data_share/data_share_service_impl.cpp | 21 ++-- .../data_share/data_share_service_impl.h | 3 +- .../service/directory/BUILD.gn | 52 +++++++++ .../service/object/object_service_impl.cpp | 2 +- .../service/object/object_service_impl.h | 2 +- 14 files changed, 192 insertions(+), 69 deletions(-) create mode 100644 services/distributeddataservice/service/data_share/BUILD.gn create mode 100644 services/distributeddataservice/service/directory/BUILD.gn diff --git a/conf/config.json b/conf/config.json index 6f13c2cf..cf89a760 100644 --- a/conf/config.json +++ b/conf/config.json @@ -20,6 +20,9 @@ }, { "lib": "libudmf_server.z.so" + }, + { + "lib": "libdata_share_service.z.so" } ], "bundleChecker": { diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 6fa9ba6a..70ac16dd 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -108,6 +108,7 @@ ohos_shared_library("distributeddataservice") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src/uninstaller:distributeddata_uninstaller_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:distributeddatasvc", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/directory:distributeddata_directory", ] external_deps = [ diff --git a/services/distributeddataservice/app/src/feature_stub_impl.cpp b/services/distributeddataservice/app/src/feature_stub_impl.cpp index 0e640edd..ec203bea 100644 --- a/services/distributeddataservice/app/src/feature_stub_impl.cpp +++ b/services/distributeddataservice/app/src/feature_stub_impl.cpp @@ -13,6 +13,9 @@ * limitations under the License. */ #include "feature_stub_impl.h" + +#include "bootstrap.h" +#include "ipc_skeleton.h" namespace OHOS::DistributedData { FeatureStubImpl::FeatureStubImpl(std::shared_ptr feature) : featureImpl_(std::move(feature)) @@ -38,7 +41,8 @@ int32_t FeatureStubImpl::OnInitialize(std::shared_ptr executor) return -1; } featureImpl_->OnExecutor(std::move(executor)); - return featureImpl_->OnInitialize(); + return featureImpl_->OnInitialize( + { Bootstrap::GetInstance().GetProcessLabel(), static_cast(IPCSkeleton::GetSelfTokenID()) }); } int32_t FeatureStubImpl::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) diff --git a/services/distributeddataservice/framework/feature/feature_system.cpp b/services/distributeddataservice/framework/feature/feature_system.cpp index 49b69e8c..c1acd0ec 100644 --- a/services/distributeddataservice/framework/feature/feature_system.cpp +++ b/services/distributeddataservice/framework/feature/feature_system.cpp @@ -55,7 +55,7 @@ FeatureSystem::Feature::~Feature() { } -int32_t FeatureSystem::Feature::OnInitialize() +int32_t FeatureSystem::Feature::OnInitialize(const BinderInfo &binderInfo) { return E_OK; } diff --git a/services/distributeddataservice/framework/include/feature/feature_system.h b/services/distributeddataservice/framework/include/feature/feature_system.h index 6bb34530..2863d0ab 100644 --- a/services/distributeddataservice/framework/include/feature/feature_system.h +++ b/services/distributeddataservice/framework/include/feature/feature_system.h @@ -36,9 +36,13 @@ public: }; class API_EXPORT Feature { public: + struct BinderInfo { + std::string bundleName; + uint32_t localTokenId; + }; virtual ~Feature(); virtual int OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply) = 0; - virtual int32_t OnInitialize(); + virtual int32_t OnInitialize(const BinderInfo &binderInfo); virtual int32_t OnExecutor(std::shared_ptr executors); virtual int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName); virtual int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 8225d0a7..f2edaa30 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -25,10 +25,6 @@ config("module_public_config") { "cloud", "config/include", "crypto/include", - "data_share", - "data_share/strategies", - "data_share/common", - "data_share/data", "directory/include", "kvdb", "matrix/include", @@ -72,42 +68,6 @@ ohos_shared_library("distributeddatasvc") { "config/src/model/network_config.cpp", "config/src/model/protocol_config.cpp", "crypto/src/crypto_manager.cpp", - "data_share/common/bundle_mgr_proxy.cpp", - "data_share/common/db_delegate.cpp", - "data_share/common/div_strategy.cpp", - "data_share/common/kv_delegate.cpp", - "data_share/common/rdb_delegate.cpp", - "data_share/common/scheduler_manager.cpp", - "data_share/common/seq_strategy.cpp", - "data_share/common/template_manager.cpp", - "data_share/common/uri_utils.cpp", - "data_share/data/json_formatter.cpp", - "data_share/data/published_data.cpp", - "data_share/data/resultset_json_formatter.cpp", - "data_share/data/template_data.cpp", - "data_share/data_share_obs_proxy.cpp", - "data_share/data_share_service_impl.cpp", - "data_share/data_share_service_stub.cpp", - "data_share/data_share_types_util.cpp", - "data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp", - "data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp", - "data_share/strategies/delete_strategy.cpp", - "data_share/strategies/general/check_is_data_proxy_strategy.cpp", - "data_share/strategies/general/check_is_single_app_strategy.cpp", - "data_share/strategies/general/connect_extension_strategy.cpp", - "data_share/strategies/general/empty_strategy.cpp", - "data_share/strategies/general/load_config_common_strategy.cpp", - "data_share/strategies/general/load_config_data_info_strategy.cpp", - "data_share/strategies/general/load_config_from_bundle_info_strategy.cpp", - "data_share/strategies/general/permission_strategy.cpp", - "data_share/strategies/general/process_single_app_user_cross_strategy.cpp", - "data_share/strategies/get_data_strategy.cpp", - "data_share/strategies/insert_strategy.cpp", - "data_share/strategies/publish_strategy.cpp", - "data_share/strategies/query_strategy.cpp", - "data_share/strategies/subscribe_strategy.cpp", - "data_share/strategies/update_strategy.cpp", - "directory/src/directory_manager.cpp", "kvdb/auth_delegate.cpp", "kvdb/executor_factory.cpp", "kvdb/kvdb_exporter.cpp", @@ -147,7 +107,7 @@ ohos_shared_library("distributeddatasvc") { deps = [ "${kv_store_distributeddb_path}:distributeddb", "${relational_store_path}/interfaces/inner_api/cloud_data:cloud_data_inner", - "data_share/gaussdb_rd:gaussdb_rd", + "directory:distributeddata_directory", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", @@ -160,10 +120,7 @@ ohos_shared_library("distributeddatasvc") { "ability_runtime:dataobs_manager", "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", "c_utils:utils", - "data_share:datashare_common", "device_auth:deviceauth_sdk", "device_manager:devicemanagersdk", "hiviewdfx_hilog_native:libhilog", @@ -171,10 +128,6 @@ ohos_shared_library("distributeddatasvc") { "ipc:ipc_core", "kv_store:distributeddata_inner", "relational_store:native_rdb", - "relational_store:rdb_bms_adapter", - "relational_store:rdb_data_share_adapter", - "resource_management:global_resmgr", - "samgr:samgr_proxy", ] subsystem_name = "distributeddatamgr" diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn new file mode 100644 index 00000000..c192973b --- /dev/null +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -0,0 +1,110 @@ +# Copyright (c) 2023 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. +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") +config("module_public_config") { + visibility = [ ":*" ] + include_dirs = [ + ".", + "strategies", + "common", + "data", + "../directory/include", + "//third_party/json/single_include", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/include", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", + "${datashare_path}/frameworks/native/common/include", + "${datashare_path}/interfaces/inner_api/common/include", + "${datashare_path}/interfaces/inner_api/consumer/include", + ] +} +group("build_module") { + deps = [ ":data_share_service" ] +} +ohos_shared_library("data_share_service") { + sources = [ + "common/bundle_mgr_proxy.cpp", + "common/db_delegate.cpp", + "common/div_strategy.cpp", + "common/kv_delegate.cpp", + "common/rdb_delegate.cpp", + "common/scheduler_manager.cpp", + "common/seq_strategy.cpp", + "common/template_manager.cpp", + "common/uri_utils.cpp", + "data/json_formatter.cpp", + "data/published_data.cpp", + "data/resultset_json_formatter.cpp", + "data/template_data.cpp", + "data_share_obs_proxy.cpp", + "data_share_service_impl.cpp", + "data_share_service_stub.cpp", + "data_share_types_util.cpp", + "strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp", + "strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp", + "strategies/delete_strategy.cpp", + "strategies/general/check_is_data_proxy_strategy.cpp", + "strategies/general/check_is_single_app_strategy.cpp", + "strategies/general/connect_extension_strategy.cpp", + "strategies/general/empty_strategy.cpp", + "strategies/general/load_config_common_strategy.cpp", + "strategies/general/load_config_data_info_strategy.cpp", + "strategies/general/load_config_from_bundle_info_strategy.cpp", + "strategies/general/permission_strategy.cpp", + "strategies/general/process_single_app_user_cross_strategy.cpp", + "strategies/get_data_strategy.cpp", + "strategies/insert_strategy.cpp", + "strategies/publish_strategy.cpp", + "strategies/query_strategy.cpp", + "strategies/subscribe_strategy.cpp", + "strategies/update_strategy.cpp", + ] + cflags = [ "-Wno-multichar" ] + + cflags_cc = [ "-fvisibility=hidden" ] + + configs = [ ":module_public_config" ] + + deps = [ + "gaussdb_rd:gaussdb_rd", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/directory:distributeddata_directory", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:dataobs_manager", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "data_share:datashare_common", + "device_manager:devicemanagersdk", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "relational_store:native_rdb", + "relational_store:rdb_bms_adapter", + "relational_store:rdb_data_share_adapter", + "samgr:samgr_proxy", + ] + subsystem_name = "distributeddatamgr" + + part_name = "datamgr_service" +} diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 383fac2a..3965a58f 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -15,7 +15,7 @@ #define LOG_TAG "RdbAdaptor" #include "rdb_delegate.h" -#include "data/resultset_json_formatter.h" +#include "resultset_json_formatter.h" #include "log_print.h" #include "rdb_utils.h" #include "scheduler_manager.h" diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index 7cd18c7d..f7541ef5 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -294,7 +294,7 @@ int RdbSubscriberManager::Notify( changeNode.data_.emplace_back(DistributedData::Serializable::Marshall(formatter)); } - ZLOGI("emit, size %{public}d %{private}s", val.size(), changeNode.uri_.c_str()); + ZLOGI("emit, size %{public}ul %{private}s", val.size(), changeNode.uri_.c_str()); for (auto &callback : val) { if (callback.enabled && callback.observer != nullptr) { callback.observer->OnChangeFromRdb(changeNode); diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index d4bb77ee..921618e7 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -19,7 +19,6 @@ #include "accesstoken_kit.h" #include "account/account_delegate.h" -#include "bootstrap.h" #include "dataobs_mgr_client.h" #include "datashare_errno.h" #include "datashare_template.h" @@ -334,26 +333,22 @@ enum DataShareKvStoreType : int32_t { DISTRIBUTED_TYPE_BUTT }; -int32_t DataShareServiceImpl::OnInitialize() +int32_t DataShareServiceImpl::OnInitialize(const BinderInfo &binderInfo) { - auto token = IPCSkeleton::GetCallingTokenID(); - auto type = OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(token); - if (type != OHOS::Security::AccessToken::TOKEN_NATIVE && type != OHOS::Security::AccessToken::TOKEN_SHELL) { - return EOK; - } + binderInfo_ = binderInfo; const std::string accountId = DistributedKv::AccountDelegate::GetInstance()->GetCurrentAccountId(); - const auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(token); + const auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(binderInfo.localTokenId); DistributedData::StoreMetaData saveMeta; saveMeta.appType = "default"; saveMeta.storeId = "data_share_data_"; saveMeta.isAutoSync = false; saveMeta.isBackup = false; saveMeta.isEncrypt = false; - saveMeta.bundleName = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); - saveMeta.appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); + saveMeta.bundleName = binderInfo.bundleName; + saveMeta.appId = binderInfo.bundleName; saveMeta.user = std::to_string(userId); saveMeta.account = accountId; - saveMeta.tokenId = token; + saveMeta.tokenId = binderInfo.localTokenId; saveMeta.securityLevel = DistributedKv::SecurityLevel::S1; saveMeta.area = 1; saveMeta.uid = IPCSkeleton::GetCallingUid(); @@ -377,8 +372,8 @@ int32_t DataShareServiceImpl::OnUserChange(uint32_t code, const std::string &use saveMeta.isAutoSync = false; saveMeta.isBackup = false; saveMeta.isEncrypt = false; - saveMeta.bundleName = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); - saveMeta.appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); + saveMeta.bundleName = binderInfo_.bundleName; + saveMeta.appId = binderInfo_.bundleName; saveMeta.user = user; saveMeta.account = account; saveMeta.tokenId = token; diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.h b/services/distributeddataservice/service/data_share/data_share_service_impl.h index 6ae6a0ae..2033008e 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -56,7 +56,7 @@ public: const int64_t subscriberId) override; std::vector DisablePubSubs(const std::vector &uris, const int64_t subscriberId) override; - int32_t OnInitialize() override; + int32_t OnInitialize(const BinderInfo &binderInfo) override; int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; private: @@ -71,6 +71,7 @@ private: static Factory factory_; static constexpr int32_t ERROR = -1; PublishStrategy publishStrategy_; + BinderInfo binderInfo_; }; } // namespace OHOS::DataShare #endif diff --git a/services/distributeddataservice/service/directory/BUILD.gn b/services/distributeddataservice/service/directory/BUILD.gn new file mode 100644 index 00000000..aa216d78 --- /dev/null +++ b/services/distributeddataservice/service/directory/BUILD.gn @@ -0,0 +1,52 @@ +# Copyright (c) 2023 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. +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") + +config("module_public_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/include", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", + ] +} + +group("build_module") { + deps = [ ":distributeddata_directory" ] +} + +ohos_shared_library("distributeddata_directory") { + sources = [ "src/directory_manager.cpp" ] + cflags = [ "-Wno-multichar" ] + + cflags_cc = [ "-fvisibility=hidden" ] + + configs = [ ":module_public_config" ] + + deps = [ + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + subsystem_name = "distributeddatamgr" + part_name = "datamgr_service" +} diff --git a/services/distributeddataservice/service/object/object_service_impl.cpp b/services/distributeddataservice/service/object/object_service_impl.cpp index 7ae417c3..b2f98214 100644 --- a/services/distributeddataservice/service/object/object_service_impl.cpp +++ b/services/distributeddataservice/service/object/object_service_impl.cpp @@ -71,7 +71,7 @@ int32_t ObjectServiceImpl::ObjectStoreSave(const std::string &bundleName, const return status; } -int32_t ObjectServiceImpl::OnInitialize() +int32_t ObjectServiceImpl::OnInitialize(const BinderInfo &binderInfo) { ZLOGI("Initialize"); auto localDeviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; diff --git a/services/distributeddataservice/service/object/object_service_impl.h b/services/distributeddataservice/service/object/object_service_impl.h index 932ea29a..2a49e78e 100644 --- a/services/distributeddataservice/service/object/object_service_impl.h +++ b/services/distributeddataservice/service/object/object_service_impl.h @@ -41,7 +41,7 @@ public: void Clear(); int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &appId) override; - int32_t OnInitialize() override; + int32_t OnInitialize(const BinderInfo &binderInfo) override; int32_t OnExecutor(std::shared_ptr executors) override; int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId) override; -- Gitee From ddf038473d88335927e7f0286ecd9376ea2c4e04 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 19 May 2023 09:16:36 +0800 Subject: [PATCH 64/95] f Signed-off-by: hanlu --- .../distributeddataservice/service/rdb/rdb_service_impl.cpp | 2 +- services/distributeddataservice/service/rdb/rdb_service_impl.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 525aed43..0190060d 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -548,7 +548,7 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) return RDB_OK; } -int32_t RdbServiceImpl::OnInitialize() +int32_t RdbServiceImpl::OnInitialize(const BinderInfo &binderInfo) { return RDB_OK; } diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index af2d788d..b4619846 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -58,7 +58,7 @@ public: int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; - int32_t OnInitialize() override; + int32_t OnInitialize(const BinderInfo &binderInfo) override; int32_t GetSchema(const RdbSyncerParam ¶m) override; int32_t OnExecutor(std::shared_ptr executors) override; -- Gitee From dc82e209c635f1aced721715770c1823f971f437 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 19 May 2023 09:26:07 +0800 Subject: [PATCH 65/95] f Signed-off-by: hanlu --- services/distributeddataservice/app/BUILD.gn | 2 +- .../distributeddataservice/service/data_share/BUILD.gn | 8 ++++---- .../distributeddataservice/service/directory/BUILD.gn | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 70ac16dd..8dfef646 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -108,7 +108,7 @@ ohos_shared_library("distributeddataservice") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src/uninstaller:distributeddata_uninstaller_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:distributeddatasvc", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/directory:distributeddata_directory", + "../service/directory:distributeddata_directory", ] external_deps = [ diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn index c192973b..1daed73b 100644 --- a/services/distributeddataservice/service/data_share/BUILD.gn +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -79,10 +79,10 @@ ohos_shared_library("data_share_service") { deps = [ "gaussdb_rd:gaussdb_rd", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/directory:distributeddata_directory", + "../../adapter:distributeddata_adapter", + "../../adapter/utils:distributeddata_utils_static", + "../../framework:distributeddatasvcfwk", + "../directory:distributeddata_directory", ] external_deps = [ diff --git a/services/distributeddataservice/service/directory/BUILD.gn b/services/distributeddataservice/service/directory/BUILD.gn index aa216d78..db1b14c6 100644 --- a/services/distributeddataservice/service/directory/BUILD.gn +++ b/services/distributeddataservice/service/directory/BUILD.gn @@ -37,9 +37,9 @@ ohos_shared_library("distributeddata_directory") { configs = [ ":module_public_config" ] deps = [ - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", + "../../adapter:distributeddata_adapter", + "../../adapter/utils:distributeddata_utils_static", + "../../framework:distributeddatasvcfwk", ] external_deps = [ -- Gitee From eb4e86a87a648bdc5d84cb9053aeae2693d830f9 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 19 May 2023 09:34:30 +0800 Subject: [PATCH 66/95] f Signed-off-by: hanlu --- services/distributeddataservice/service/data_share/BUILD.gn | 6 +++--- services/distributeddataservice/service/directory/BUILD.gn | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn index 1daed73b..caa9c27b 100644 --- a/services/distributeddataservice/service/data_share/BUILD.gn +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -22,9 +22,9 @@ config("module_public_config") { "data", "../directory/include", "//third_party/json/single_include", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/include", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", + "../../adapter/include", + "../../app/src", + "../../framework/include", "${datashare_path}/frameworks/native/common/include", "${datashare_path}/interfaces/inner_api/common/include", "${datashare_path}/interfaces/inner_api/consumer/include", diff --git a/services/distributeddataservice/service/directory/BUILD.gn b/services/distributeddataservice/service/directory/BUILD.gn index db1b14c6..7e2477fa 100644 --- a/services/distributeddataservice/service/directory/BUILD.gn +++ b/services/distributeddataservice/service/directory/BUILD.gn @@ -18,9 +18,9 @@ config("module_public_config") { visibility = [ ":*" ] include_dirs = [ "include", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/include", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", + "../../adapter/include", + "../../app/src", + "../../framework/include", ] } -- Gitee From 571b921b971a0dd76ec89bda60c7ff4821931074 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 19 May 2023 09:49:54 +0800 Subject: [PATCH 67/95] f Signed-off-by: hanlu --- bundle.json | 4 +++- services/distributeddataservice/service/data_share/BUILD.gn | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bundle.json b/bundle.json index a5d2bd57..caee502d 100644 --- a/bundle.json +++ b/bundle.json @@ -90,7 +90,9 @@ "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:build_module", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:build_module", "//foundation/distributeddatamgr/datamgr_service/conf:build_module", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share/gaussdb_rd:build_module" + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share:build_module", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/directory:build_module", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share/gaussdb_rd:build_module" ], "inner_kits": [], "test": [ diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn index caa9c27b..4dafa8e0 100644 --- a/services/distributeddataservice/service/data_share/BUILD.gn +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -78,11 +78,11 @@ ohos_shared_library("data_share_service") { configs = [ ":module_public_config" ] deps = [ - "gaussdb_rd:gaussdb_rd", "../../adapter:distributeddata_adapter", "../../adapter/utils:distributeddata_utils_static", "../../framework:distributeddatasvcfwk", "../directory:distributeddata_directory", + "gaussdb_rd:gaussdb_rd", ] external_deps = [ -- Gitee From 3054e8b79084933adeb9f812529e1a2b25028a0e Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 19 May 2023 10:00:19 +0800 Subject: [PATCH 68/95] f Signed-off-by: hanlu --- services/distributeddataservice/app/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 8dfef646..53f2884e 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -100,6 +100,7 @@ ohos_shared_library("distributeddataservice") { configs = [ ":module_private_config" ] deps = [ "${kv_store_distributeddb_path}:distributeddb", + "../service/directory:distributeddata_directory", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", @@ -108,7 +109,6 @@ ohos_shared_library("distributeddataservice") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src/uninstaller:distributeddata_uninstaller_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:distributeddatasvc", - "../service/directory:distributeddata_directory", ] external_deps = [ -- Gitee From 96c33757a17e9bb26a5d3149db49ae2444d56269 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 19 May 2023 10:36:58 +0800 Subject: [PATCH 69/95] f Signed-off-by: hanlu --- .../distributeddataservice/service/cloud/cloud_service_impl.cpp | 2 +- .../distributeddataservice/service/cloud/cloud_service_impl.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 0163dfb8..5744ed0e 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -190,7 +190,7 @@ int32_t CloudServiceImpl::NotifyDataChange(const std::string &id, const std::str return SUCCESS; } -int32_t CloudServiceImpl::OnInitialize() +int32_t CloudServiceImpl::OnInitialize(const BinderInfo &binderInfo) { FeatureInit(); Execute(GetCloudTask(0, 0)); diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 35121c57..2d94fcc8 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -34,7 +34,7 @@ public: int32_t ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) override; int32_t Clean(const std::string &id, const std::map &actions) override; int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) override; - int32_t OnInitialize() override; + int32_t OnInitialize(const BinderInfo &binderInfo) override; int32_t OnExecutor(std::shared_ptr executor) override; int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; -- Gitee From 184f05d4e9296d807d19678bba005d73daa5a4e4 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 19 May 2023 11:31:33 +0800 Subject: [PATCH 70/95] f Signed-off-by: hanlu --- services/distributeddataservice/service/test/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 97d837b7..beb45336 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -111,6 +111,7 @@ ohos_unittest("DirectoryManagerTest") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:distributeddatasvc", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/directory:distributeddata_directory", "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata:distributeddata_inner", "//third_party/googletest:gtest_main", ] -- Gitee From 845124732d1ff10aaee9565b967f1cd2f8cf0a34 Mon Sep 17 00:00:00 2001 From: niudongyao Date: Fri, 19 May 2023 11:42:15 +0800 Subject: [PATCH 71/95] warn fix Signed-off-by: niudongyao --- .../service/data_share/common/template_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index 7cd18c7d..9c069076 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -294,7 +294,7 @@ int RdbSubscriberManager::Notify( changeNode.data_.emplace_back(DistributedData::Serializable::Marshall(formatter)); } - ZLOGI("emit, size %{public}d %{private}s", val.size(), changeNode.uri_.c_str()); + ZLOGI("emit, size %{public}lu %{private}s", val.size(), changeNode.uri_.c_str()); for (auto &callback : val) { if (callback.enabled && callback.observer != nullptr) { callback.observer->OnChangeFromRdb(changeNode); -- Gitee From a1f407977ad94a9eacceff838f82e0a6f8206544 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 19 May 2023 14:19:21 +0800 Subject: [PATCH 72/95] f Signed-off-by: hanlu --- bundle.json | 1 - services/distributeddataservice/app/BUILD.gn | 1 - .../app/src/kvstore_meta_manager.cpp | 2 +- .../distributeddataservice/framework/BUILD.gn | 6 ++- .../directory}/directory_manager.cpp | 5 +- .../include/directory}/directory_manager.h | 0 .../distributeddataservice/service/BUILD.gn | 2 - .../service/backup/src/backup_manager.cpp | 5 +- .../service/bootstrap/src/bootstrap.cpp | 5 +- .../config/include/model/directory_config.h | 2 +- .../service/data_share/BUILD.gn | 1 - .../service/data_share/common/kv_delegate.cpp | 5 +- .../data_share/common/template_manager.cpp | 2 +- .../data_share/data_share_service_impl.cpp | 2 +- .../service/directory/BUILD.gn | 52 ------------------- .../service/kvdb/kvdb_exporter.cpp | 2 +- .../service/kvdb/kvdb_service_impl.cpp | 4 +- .../service/kvdb/store_cache.cpp | 3 +- .../service/kvdb/upgrade.cpp | 6 +-- .../service/object/object_service_impl.cpp | 2 +- .../service/rdb/rdb_service_impl.cpp | 2 +- .../service/rdb/rdb_syncer.cpp | 2 +- .../service/test/BUILD.gn | 1 - .../service/test/directory_manager_test.cpp | 2 +- 24 files changed, 32 insertions(+), 83 deletions(-) rename services/distributeddataservice/{service/directory/src => framework/directory}/directory_manager.cpp (99%) rename services/distributeddataservice/{service/directory/include => framework/include/directory}/directory_manager.h (100%) delete mode 100644 services/distributeddataservice/service/directory/BUILD.gn diff --git a/bundle.json b/bundle.json index caee502d..2c97e047 100644 --- a/bundle.json +++ b/bundle.json @@ -91,7 +91,6 @@ "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:build_module", "//foundation/distributeddatamgr/datamgr_service/conf:build_module", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share:build_module", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/directory:build_module", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share/gaussdb_rd:build_module" ], "inner_kits": [], diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 53f2884e..6fa9ba6a 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -100,7 +100,6 @@ ohos_shared_library("distributeddataservice") { configs = [ ":module_private_config" ] deps = [ "${kv_store_distributeddb_path}:distributeddb", - "../service/directory:distributeddata_directory", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index 7fb54c86..c281c7a2 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -28,7 +28,7 @@ #include "crypto_manager.h" #include "device_manager_adapter.h" #include "device_matrix.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "dump_helper.h" #include "eventcenter/event_center.h" #include "kvstore_data_service.h" diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index 09a242ba..d9293018 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -51,6 +51,7 @@ ohos_shared_library("distributeddatasvcfwk") { "cloud/cloud_server.cpp", "cloud/schema_meta.cpp", "cloud/subscription.cpp", + "directory/directory_manager.cpp", "eventcenter/event.cpp", "eventcenter/event_center.cpp", "feature/feature_system.cpp", @@ -84,7 +85,10 @@ ohos_shared_library("distributeddatasvcfwk") { deps = [ "//third_party/openssl:libcrypto_shared" ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "access_token:libaccesstoken_sdk", + ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/services/distributeddataservice/service/directory/src/directory_manager.cpp b/services/distributeddataservice/framework/directory/directory_manager.cpp similarity index 99% rename from services/distributeddataservice/service/directory/src/directory_manager.cpp rename to services/distributeddataservice/framework/directory/directory_manager.cpp index bba79049..a3b0458b 100644 --- a/services/distributeddataservice/service/directory/src/directory_manager.cpp +++ b/services/distributeddataservice/framework/directory/directory_manager.cpp @@ -13,12 +13,11 @@ * limitations under the License. */ #define LOG_TAG "DirectoryManager" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include -#include - #include +#include #include "accesstoken_kit.h" #include "log_print.h" diff --git a/services/distributeddataservice/service/directory/include/directory_manager.h b/services/distributeddataservice/framework/include/directory/directory_manager.h similarity index 100% rename from services/distributeddataservice/service/directory/include/directory_manager.h rename to services/distributeddataservice/framework/include/directory/directory_manager.h diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index f2edaa30..77641b8d 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -25,7 +25,6 @@ config("module_public_config") { "cloud", "config/include", "crypto/include", - "directory/include", "kvdb", "matrix/include", "object", @@ -107,7 +106,6 @@ ohos_shared_library("distributeddatasvc") { deps = [ "${kv_store_distributeddb_path}:distributeddb", "${relational_store_path}/interfaces/inner_api/cloud_data:cloud_data_inner", - "directory:distributeddata_directory", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", diff --git a/services/distributeddataservice/service/backup/src/backup_manager.cpp b/services/distributeddataservice/service/backup/src/backup_manager.cpp index 5c52cbd7..0a904e1b 100644 --- a/services/distributeddataservice/service/backup/src/backup_manager.cpp +++ b/services/distributeddataservice/service/backup/src/backup_manager.cpp @@ -18,10 +18,11 @@ #include #include #include + #include "backuprule/backup_rule_manager.h" -#include "device_manager_adapter.h" #include "crypto_manager.h" -#include "directory_manager.h" +#include "device_manager_adapter.h" +#include "directory/directory_manager.h" #include "log_print.h" #include "metadata/meta_data_manager.h" #include "types.h" diff --git a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp index 6cdd57ab..8f958cf3 100644 --- a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp +++ b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp @@ -16,11 +16,12 @@ #include "bootstrap.h" #include -#include "backuprule/backup_rule_manager.h" + #include "backup_manager.h" +#include "backuprule/backup_rule_manager.h" #include "checker/checker_manager.h" #include "config_factory.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "log_print.h" namespace OHOS { namespace DistributedData { diff --git a/services/distributeddataservice/service/config/include/model/directory_config.h b/services/distributeddataservice/service/config/include/model/directory_config.h index baa7b05c..03422499 100644 --- a/services/distributeddataservice/service/config/include/model/directory_config.h +++ b/services/distributeddataservice/service/config/include/model/directory_config.h @@ -15,7 +15,7 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_CONFIG_MODEL_DIRECTORY_CONFIG_H #define OHOS_DISTRIBUTED_DATA_SERVICES_CONFIG_MODEL_DIRECTORY_CONFIG_H -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "serializable/serializable.h" namespace OHOS { namespace DistributedData { diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn index 4dafa8e0..b8efaf61 100644 --- a/services/distributeddataservice/service/data_share/BUILD.gn +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -81,7 +81,6 @@ ohos_shared_library("data_share_service") { "../../adapter:distributeddata_adapter", "../../adapter/utils:distributeddata_utils_static", "../../framework:distributeddatasvcfwk", - "../directory:distributeddata_directory", "gaussdb_rd:gaussdb_rd", ] diff --git a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp index fe645eba..c17166cd 100644 --- a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp @@ -14,10 +14,11 @@ */ #define LOG_TAG "KvAdaptor" #include "kv_delegate.h" + #include "datashare_errno.h" -#include "directory_manager.h" -#include "grd_document/grd_document_api.h" +#include "directory/directory_manager.h" #include "grd_base/grd_error.h" +#include "grd_document/grd_document_api.h" #include "ipc_skeleton.h" #include "log_print.h" diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index f7541ef5..4b573349 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -294,7 +294,7 @@ int RdbSubscriberManager::Notify( changeNode.data_.emplace_back(DistributedData::Serializable::Marshall(formatter)); } - ZLOGI("emit, size %{public}ul %{private}s", val.size(), changeNode.uri_.c_str()); + ZLOGI("emit, size %{public}zu %{private}s", val.size(), changeNode.uri_.c_str()); for (auto &callback : val) { if (callback.enabled && callback.observer != nullptr) { callback.observer->OnChangeFromRdb(changeNode); diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 921618e7..7604b8a3 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -23,7 +23,7 @@ #include "datashare_errno.h" #include "datashare_template.h" #include "delete_strategy.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "get_data_strategy.h" #include "hap_token_info.h" #include "insert_strategy.h" diff --git a/services/distributeddataservice/service/directory/BUILD.gn b/services/distributeddataservice/service/directory/BUILD.gn deleted file mode 100644 index 7e2477fa..00000000 --- a/services/distributeddataservice/service/directory/BUILD.gn +++ /dev/null @@ -1,52 +0,0 @@ -# Copyright (c) 2023 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. -import("//build/ohos.gni") -import("//build/ohos_var.gni") -import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") - -config("module_public_config") { - visibility = [ ":*" ] - include_dirs = [ - "include", - "../../adapter/include", - "../../app/src", - "../../framework/include", - ] -} - -group("build_module") { - deps = [ ":distributeddata_directory" ] -} - -ohos_shared_library("distributeddata_directory") { - sources = [ "src/directory_manager.cpp" ] - cflags = [ "-Wno-multichar" ] - - cflags_cc = [ "-fvisibility=hidden" ] - - configs = [ ":module_public_config" ] - - deps = [ - "../../adapter:distributeddata_adapter", - "../../adapter/utils:distributeddata_utils_static", - "../../framework:distributeddatasvcfwk", - ] - - external_deps = [ - "access_token:libaccesstoken_sdk", - "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", - ] - subsystem_name = "distributeddatamgr" - part_name = "datamgr_service" -} diff --git a/services/distributeddataservice/service/kvdb/kvdb_exporter.cpp b/services/distributeddataservice/service/kvdb/kvdb_exporter.cpp index 4b0c2e7f..22532d53 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_exporter.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_exporter.cpp @@ -16,7 +16,7 @@ #include "kvdb_exporter.h" #include "backup_manager.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "log_print.h" #include "reporter.h" #include "store_cache.h" diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 8428cb58..ccb683f7 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -26,15 +26,15 @@ #include "communication_strategy.h" #include "crypto_manager.h" #include "device_manager_adapter.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "eventcenter/event_center.h" #include "ipc_skeleton.h" #include "log_print.h" #include "matrix_event.h" #include "metadata/appid_meta_data.h" #include "metadata/meta_data_manager.h" -#include "query_helper.h" #include "permit_delegate.h" +#include "query_helper.h" #include "upgrade.h" #include "utils/anonymous.h" #include "utils/constant.h" diff --git a/services/distributeddataservice/service/kvdb/store_cache.cpp b/services/distributeddataservice/service/kvdb/store_cache.cpp index 093e8e5a..620c5e9e 100644 --- a/services/distributeddataservice/service/kvdb/store_cache.cpp +++ b/services/distributeddataservice/service/kvdb/store_cache.cpp @@ -14,10 +14,11 @@ */ #define LOG_TAG "StoreCache" #include "store_cache.h" + #include "account/account_delegate.h" #include "crypto_manager.h" #include "device_matrix.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "log_print.h" #include "metadata/meta_data_manager.h" #include "metadata/secret_key_meta_data.h" diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index ea40a6bd..38a370e8 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -18,14 +18,14 @@ #include #include +#include "accesstoken_kit.h" #include "crypto_manager.h" -#include "metadata/secret_key_meta_data.h" #include "device_manager_adapter.h" +#include "directory/directory_manager.h" #include "log_print.h" #include "metadata/meta_data_manager.h" +#include "metadata/secret_key_meta_data.h" #include "store_cache.h" -#include "accesstoken_kit.h" -#include "directory_manager.h" namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; using system_clock = std::chrono::system_clock; diff --git a/services/distributeddataservice/service/object/object_service_impl.cpp b/services/distributeddataservice/service/object/object_service_impl.cpp index b2f98214..c17e1d03 100644 --- a/services/distributeddataservice/service/object/object_service_impl.cpp +++ b/services/distributeddataservice/service/object/object_service_impl.cpp @@ -23,7 +23,7 @@ #include "bootstrap.h" #include "checker/checker_manager.h" #include "device_manager_adapter.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "log_print.h" #include "metadata/appid_meta_data.h" #include "metadata/meta_data_manager.h" diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 0190060d..5a73ff78 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -20,7 +20,7 @@ #include "cloud/cloud_event.h" #include "communicator/device_manager_adapter.h" #include "crypto_manager.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "eventcenter/event_center.h" #include "ipc_skeleton.h" #include "log_print.h" diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/services/distributeddataservice/service/rdb/rdb_syncer.cpp index f8f6192a..b03c65ca 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -22,7 +22,7 @@ #include "checker/checker_manager.h" #include "crypto_manager.h" #include "device_manager_adapter.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "kvstore_utils.h" #include "log_print.h" #include "metadata/appid_meta_data.h" diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index beb45336..97d837b7 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -111,7 +111,6 @@ ohos_unittest("DirectoryManagerTest") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:distributeddatasvc", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/directory:distributeddata_directory", "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata:distributeddata_inner", "//third_party/googletest:gtest_main", ] diff --git a/services/distributeddataservice/service/test/directory_manager_test.cpp b/services/distributeddataservice/service/test/directory_manager_test.cpp index ae9cb648..d9eed29a 100644 --- a/services/distributeddataservice/service/test/directory_manager_test.cpp +++ b/services/distributeddataservice/service/test/directory_manager_test.cpp @@ -16,7 +16,7 @@ #include #include "accesstoken_kit.h" #include "bootstrap.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "nativetoken_kit.h" #include "types.h" -- Gitee From 4932d3bbe288b682649486e006e8dbc5f0c6c79a Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 19 May 2023 14:23:05 +0800 Subject: [PATCH 73/95] f Signed-off-by: hanlu --- services/distributeddataservice/framework/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index d9293018..c051f1eb 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -86,8 +86,8 @@ ohos_shared_library("distributeddatasvcfwk") { deps = [ "//third_party/openssl:libcrypto_shared" ] external_deps = [ - "hiviewdfx_hilog_native:libhilog", "access_token:libaccesstoken_sdk", + "hiviewdfx_hilog_native:libhilog", ] subsystem_name = "distributeddatamgr" -- Gitee From 5d3fcfbf7cc407d293122cb4e513126725490c2e Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 19 May 2023 16:09:28 +0800 Subject: [PATCH 74/95] f Signed-off-by: hanlu --- .../app/src/feature_stub_impl.cpp | 6 +++--- .../framework/feature/feature_system.cpp | 12 ++++++------ .../framework/include/feature/feature_system.h | 11 ++++++----- .../service/cloud/cloud_service_impl.cpp | 8 ++++---- .../service/cloud/cloud_service_impl.h | 4 ++-- .../service/data_share/BUILD.gn | 1 - .../service/data_share/common/template_manager.cpp | 7 ++----- .../service/data_share/data_share_service_impl.cpp | 14 +++++++------- .../service/data_share/data_share_service_impl.h | 4 ++-- .../service/kvdb/kvdb_service_impl.cpp | 10 +++++----- .../service/kvdb/kvdb_service_impl.h | 2 +- .../service/object/object_service_impl.cpp | 7 +------ .../service/object/object_service_impl.h | 4 +--- .../service/rdb/rdb_service_impl.cpp | 6 +++--- .../service/rdb/rdb_service_impl.h | 4 ++-- 15 files changed, 45 insertions(+), 55 deletions(-) diff --git a/services/distributeddataservice/app/src/feature_stub_impl.cpp b/services/distributeddataservice/app/src/feature_stub_impl.cpp index ec203bea..f1f52b91 100644 --- a/services/distributeddataservice/app/src/feature_stub_impl.cpp +++ b/services/distributeddataservice/app/src/feature_stub_impl.cpp @@ -40,9 +40,9 @@ int32_t FeatureStubImpl::OnInitialize(std::shared_ptr executor) if (featureImpl_ == nullptr) { return -1; } - featureImpl_->OnExecutor(std::move(executor)); - return featureImpl_->OnInitialize( - { Bootstrap::GetInstance().GetProcessLabel(), static_cast(IPCSkeleton::GetSelfTokenID()) }); + featureImpl_->OnBind({ Bootstrap::GetInstance().GetProcessLabel(), + static_cast(IPCSkeleton::GetSelfTokenID()), std::move(executor)}); + return featureImpl_->OnInitialize(); } int32_t FeatureStubImpl::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) diff --git a/services/distributeddataservice/framework/feature/feature_system.cpp b/services/distributeddataservice/framework/feature/feature_system.cpp index c1acd0ec..74907ec8 100644 --- a/services/distributeddataservice/framework/feature/feature_system.cpp +++ b/services/distributeddataservice/framework/feature/feature_system.cpp @@ -55,12 +55,7 @@ FeatureSystem::Feature::~Feature() { } -int32_t FeatureSystem::Feature::OnInitialize(const BinderInfo &binderInfo) -{ - return E_OK; -} - -int32_t FeatureSystem::Feature::OnExecutor(std::shared_ptr executors) +int32_t FeatureSystem::Feature::OnInitialize() { return E_OK; } @@ -106,5 +101,10 @@ int32_t FeatureSystem::Feature::OnReady(const std::string &device) { return E_OK; } + +int32_t FeatureSystem::Feature::OnBind(const FeatureSystem::Feature::BindInfo &bindInfo) +{ + return E_OK; +} } // namespace DistributedData } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/framework/include/feature/feature_system.h b/services/distributeddataservice/framework/include/feature/feature_system.h index 2863d0ab..d549cc6d 100644 --- a/services/distributeddataservice/framework/include/feature/feature_system.h +++ b/services/distributeddataservice/framework/include/feature/feature_system.h @@ -36,14 +36,15 @@ public: }; class API_EXPORT Feature { public: - struct BinderInfo { - std::string bundleName; - uint32_t localTokenId; + struct BindInfo { + std::string selfName; + uint32_t selfTokenId; + std::shared_ptr executors; }; virtual ~Feature(); virtual int OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply) = 0; - virtual int32_t OnInitialize(const BinderInfo &binderInfo); - virtual int32_t OnExecutor(std::shared_ptr executors); + virtual int32_t OnInitialize(); + virtual int32_t OnBind(const BindInfo &bindInfo); virtual int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName); virtual int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); virtual int32_t OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 5744ed0e..b605daf5 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -190,20 +190,20 @@ int32_t CloudServiceImpl::NotifyDataChange(const std::string &id, const std::str return SUCCESS; } -int32_t CloudServiceImpl::OnInitialize(const BinderInfo &binderInfo) +int32_t CloudServiceImpl::OnInitialize() { FeatureInit(); Execute(GetCloudTask(0, 0)); return E_OK; } -int32_t CloudServiceImpl::OnExecutor(std::shared_ptr executor) +int32_t CloudServiceImpl::OnBind(const BindInfo &info) { - if (executor_ != nullptr || executor == nullptr) { + if (executor_ != nullptr || info.executors == nullptr) { return E_INVALID_ARGS; } - executor_ = std::move(executor); + executor_ = std::move(info.executors); return E_OK; } diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 2d94fcc8..5bbc2507 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -34,8 +34,8 @@ public: int32_t ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) override; int32_t Clean(const std::string &id, const std::map &actions) override; int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) override; - int32_t OnInitialize(const BinderInfo &binderInfo) override; - int32_t OnExecutor(std::shared_ptr executor) override; + int32_t OnInitialize() override; + int32_t OnBind(const BindInfo &info) override; int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; private: diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn index b8efaf61..b9490bbc 100644 --- a/services/distributeddataservice/service/data_share/BUILD.gn +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -20,7 +20,6 @@ config("module_public_config") { "strategies", "common", "data", - "../directory/include", "//third_party/json/single_include", "../../adapter/include", "../../app/src", diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index 4b573349..fabed5f1 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -127,14 +127,11 @@ int RdbSubscriberManager::AddRdbSubscriber(const std::string &uri, const Templat { int result = E_OK; Key key(uri, tplId.subscriberId_, tplId.bundleName_); - rdbCache_.Compute(key, [&observer, &context, &result, this](const auto &key, std::vector &value) { + rdbCache_.Compute(key, [&observer, &context, this](const auto &key, std::vector &value) { ZLOGI("add subscriber, uri %{private}s tokenId %{public}d", key.uri.c_str(), context->callerTokenId); std::vector node; node.emplace_back(observer, context->callerTokenId); - result = Notify(key, node, context->calledSourceDir, context->version); - if (result != E_OK) { - return false; - } + Notify(key, node, context->calledSourceDir, context->version); value.emplace_back(observer, context->callerTokenId); if (GetEnableObserverCount(key) == 1) { SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 7604b8a3..3209b7cc 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -333,22 +333,22 @@ enum DataShareKvStoreType : int32_t { DISTRIBUTED_TYPE_BUTT }; -int32_t DataShareServiceImpl::OnInitialize(const BinderInfo &binderInfo) +int32_t DataShareServiceImpl::OnBind(const BindInfo &binderInfo) { binderInfo_ = binderInfo; const std::string accountId = DistributedKv::AccountDelegate::GetInstance()->GetCurrentAccountId(); - const auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(binderInfo.localTokenId); + const auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(binderInfo.selfTokenId); DistributedData::StoreMetaData saveMeta; saveMeta.appType = "default"; saveMeta.storeId = "data_share_data_"; saveMeta.isAutoSync = false; saveMeta.isBackup = false; saveMeta.isEncrypt = false; - saveMeta.bundleName = binderInfo.bundleName; - saveMeta.appId = binderInfo.bundleName; + saveMeta.bundleName = binderInfo.selfName; + saveMeta.appId = binderInfo.selfName; saveMeta.user = std::to_string(userId); saveMeta.account = accountId; - saveMeta.tokenId = binderInfo.localTokenId; + saveMeta.tokenId = binderInfo.selfTokenId; saveMeta.securityLevel = DistributedKv::SecurityLevel::S1; saveMeta.area = 1; saveMeta.uid = IPCSkeleton::GetCallingUid(); @@ -372,8 +372,8 @@ int32_t DataShareServiceImpl::OnUserChange(uint32_t code, const std::string &use saveMeta.isAutoSync = false; saveMeta.isBackup = false; saveMeta.isEncrypt = false; - saveMeta.bundleName = binderInfo_.bundleName; - saveMeta.appId = binderInfo_.bundleName; + saveMeta.bundleName = binderInfo_.selfName; + saveMeta.appId = binderInfo_.selfName; saveMeta.user = user; saveMeta.account = account; saveMeta.tokenId = token; diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.h b/services/distributeddataservice/service/data_share/data_share_service_impl.h index 2033008e..74dc44b3 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -56,7 +56,7 @@ public: const int64_t subscriberId) override; std::vector DisablePubSubs(const std::vector &uris, const int64_t subscriberId) override; - int32_t OnInitialize(const BinderInfo &binderInfo) override; + int32_t OnBind(const BindInfo &binderInfo) override; int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; private: @@ -71,7 +71,7 @@ private: static Factory factory_; static constexpr int32_t ERROR = -1; PublishStrategy publishStrategy_; - BinderInfo binderInfo_; + BindInfo binderInfo_; }; } // namespace OHOS::DataShare #endif diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index ccb683f7..e63f2dcd 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -790,12 +790,12 @@ size_t KVDBServiceImpl::GetSyncDataSize(const std::string &deviceId) return totalSize; } -int32_t KVDBServiceImpl::OnExecutor(std::shared_ptr executors) +int32_t KVDBServiceImpl::OnBind(const BindInfo &bindInfo) { - executors_ = executors; - storeCache_.SetThreadPool(executors); - KvStoreSyncManager::GetInstance()->SetThreadPool(executors); - ZLOGE("onexecutor:%{public}p", executors.get()); + executors_ = bindInfo.executors; + storeCache_.SetThreadPool(bindInfo.executors); + KvStoreSyncManager::GetInstance()->SetThreadPool(bindInfo.executors); + ZLOGE("onexecutor:%{public}p", bindInfo.executors.get()); return 0; } } // namespace OHOS::DistributedKv \ No newline at end of file diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h index b1c3e5fd..861cd068 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h @@ -53,7 +53,7 @@ public: Status Subscribe(const AppId &appId, const StoreId &storeId, sptr observer) override; Status Unsubscribe(const AppId &appId, const StoreId &storeId, sptr observer) override; Status GetBackupPassword(const AppId &appId, const StoreId &storeId, std::vector &password) override; - int32_t OnExecutor(std::shared_ptr executors) override; + int32_t OnBind(const BindInfo &bindInfo) override; int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &appId) override; int32_t ResolveAutoLaunch(const std::string &identifier, DBLaunchParam ¶m) override; int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; diff --git a/services/distributeddataservice/service/object/object_service_impl.cpp b/services/distributeddataservice/service/object/object_service_impl.cpp index c17e1d03..ccd1e57a 100644 --- a/services/distributeddataservice/service/object/object_service_impl.cpp +++ b/services/distributeddataservice/service/object/object_service_impl.cpp @@ -71,7 +71,7 @@ int32_t ObjectServiceImpl::ObjectStoreSave(const std::string &bundleName, const return status; } -int32_t ObjectServiceImpl::OnInitialize(const BinderInfo &binderInfo) +int32_t ObjectServiceImpl::OnInitialize() { ZLOGI("Initialize"); auto localDeviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; @@ -277,9 +277,4 @@ int32_t ObjectServiceImpl::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, con ObjectServiceImpl::ObjectServiceImpl() { } -int32_t ObjectServiceImpl::OnExecutor(std::shared_ptr executors) -{ - executors_ = executors; - return 0; -} } // namespace OHOS::DistributedObject diff --git a/services/distributeddataservice/service/object/object_service_impl.h b/services/distributeddataservice/service/object/object_service_impl.h index 2a49e78e..a7f40e51 100644 --- a/services/distributeddataservice/service/object/object_service_impl.h +++ b/services/distributeddataservice/service/object/object_service_impl.h @@ -41,8 +41,7 @@ public: void Clear(); int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &appId) override; - int32_t OnInitialize(const BinderInfo &binderInfo) override; - int32_t OnExecutor(std::shared_ptr executors) override; + int32_t OnInitialize() override; int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId) override; @@ -53,7 +52,6 @@ private: ~Factory(); }; static Factory factory_; - std::shared_ptr executors_; }; } // namespace OHOS::DistributedObject #endif diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 5a73ff78..88f79bc6 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -548,7 +548,7 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) return RDB_OK; } -int32_t RdbServiceImpl::OnInitialize(const BinderInfo &binderInfo) +int32_t RdbServiceImpl::OnInitialize() { return RDB_OK; } @@ -593,9 +593,9 @@ StoreMetaData RdbServiceImpl::GetStoreMetaData(const RdbSyncerParam ¶m) return metaData; } -int32_t RdbServiceImpl::OnExecutor(std::shared_ptr executors) +int32_t RdbServiceImpl::OnBind(const BindInfo &bindInfo) { - executors_ = executors; + executors_ = bindInfo.executors; return 0; } } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index b4619846..8a34c712 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -58,10 +58,10 @@ public: int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; - int32_t OnInitialize(const BinderInfo &binderInfo) override; + int32_t OnInitialize() override; int32_t GetSchema(const RdbSyncerParam ¶m) override; - int32_t OnExecutor(std::shared_ptr executors) override; + int32_t OnBind(const BindInfo &bindInfo) override; protected: int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option, -- Gitee From 0513a3b19256119be35af7649e0ab9b46c8109c1 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 19 May 2023 18:41:30 +0800 Subject: [PATCH 75/95] f Signed-off-by: hanlu --- .../distributeddataservice/service/test/config_factory_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/test/config_factory_test.cpp b/services/distributeddataservice/service/test/config_factory_test.cpp index 70a6a5ad..02106109 100644 --- a/services/distributeddataservice/service/test/config_factory_test.cpp +++ b/services/distributeddataservice/service/test/config_factory_test.cpp @@ -61,7 +61,7 @@ HWTEST_F(ConfigFactoryTest, ComponentConfig, TestSize.Level0) { auto *components = ConfigFactory::GetInstance().GetComponentConfig(); ASSERT_NE(components, nullptr); - ASSERT_EQ(components->size(), 3); + ASSERT_EQ(components->size(), 4); const ComponentConfig &config = (*components)[0]; ASSERT_EQ(config.description, "3rd party adapter"); ASSERT_EQ(config.lib, "libconfigdemo.z.so"); -- Gitee From 97b9299b301c77645086f0c59a9500c8df483a45 Mon Sep 17 00:00:00 2001 From: wellinleo Date: Sun, 21 May 2023 10:26:28 +0800 Subject: [PATCH 76/95] modify codecheck Signed-off-by: wellinleo --- .../app/src/dump_helper.cpp | 38 +++++++++---------- .../data_share/common/bundle_mgr_proxy.cpp | 2 +- .../data_share/common/rdb_delegate.cpp | 2 +- .../test/unittest/api/documentdb_api_test.cpp | 14 +++---- .../api/documentdb_collection_test.cpp | 2 +- .../unittest/api/documentdb_data_test.cpp | 12 +++--- .../unittest/api/documentdb_find_test.cpp | 2 +- .../unittest/api/documentdb_insert_test.cpp | 8 ++-- .../documentdb_json_common_test.cpp | 38 +++++++++---------- .../oh_adapter/documentdb_jsonobject_test.cpp | 2 +- .../service/kvdb/ref_count.h | 2 +- .../service/kvdb/store_cache.h | 4 +- .../service/test/mock/db_store_mock.cpp | 8 ++-- 13 files changed, 67 insertions(+), 67 deletions(-) diff --git a/services/distributeddataservice/app/src/dump_helper.cpp b/services/distributeddataservice/app/src/dump_helper.cpp index f718164e..225a0e17 100644 --- a/services/distributeddataservice/app/src/dump_helper.cpp +++ b/services/distributeddataservice/app/src/dump_helper.cpp @@ -112,25 +112,25 @@ void DumpHelper::ShowHelp(int fd) { std::string result; result.append("Usage:dump [options]\n") - .append("Description:\n") - .append(CMD_USER_INFO) - .append(" ") - .append("dump all user information in the system\n") - .append(CMD_APP_INFO) - .append(" ") - .append("dump list of all app information in the system\n") - .append(CMD_APP_INFO) - .append(" [appID] ") - .append("dump information about the specified app in the system\n") - .append(CMD_STORE_INFO) - .append(" ") - .append("dump list of all store information in the system\n") - .append(CMD_STORE_INFO) - .append(" [storeID] ") - .append("dump information about the specified store in the system\n") - .append(CMD_ERROR_INFO) - .append(" ") - .append("dump the recent errors information in the system\n"); + .append("Description:\n") + .append(CMD_USER_INFO) + .append(" ") + .append("dump all user information in the system\n") + .append(CMD_APP_INFO) + .append(" ") + .append("dump list of all app information in the system\n") + .append(CMD_APP_INFO) + .append(" [appID] ") + .append("dump information about the specified app in the system\n") + .append(CMD_STORE_INFO) + .append(" ") + .append("dump list of all store information in the system\n") + .append(CMD_STORE_INFO) + .append(" [storeID] ") + .append("dump information about the specified store in the system\n") + .append(CMD_ERROR_INFO) + .append(" ") + .append("dump the recent errors information in the system\n"); dprintf(fd, "%s\n", result.c_str()); } diff --git a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp index 4519382f..65319772 100644 --- a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp @@ -85,7 +85,7 @@ BundleMgrProxy::~BundleMgrProxy() std::shared_ptr BundleMgrProxy::GetInstance() { - static std::shared_ptr proxy(new BundleMgrProxy()); + static std::shared_ptr proxy(std::make_shared()); return proxy; } } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 383fac2a..2d3dd071 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -131,7 +131,7 @@ std::shared_ptr RdbDelegate::Query(const std::string &tableN return nullptr; } auto bridge = RdbDataShareAdapter::RdbUtils::ToResultSetBridge(resultSet); - return std::shared_ptr(new DataShareResultSet(bridge), [](auto p) { + return std::shared_ptr(std::make_shared(bridge), [](auto p) { ZLOGD("release resultset"); resultSetCount--; delete p; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp index 3e0c86f7..22fe15ee 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp @@ -65,7 +65,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBTest001, TestSize.Level0) EXPECT_EQ(GRD_CreateCollection(db, "student", "", 0), GRD_OK); - EXPECT_EQ(GRD_UpsertDoc(db, "student", R""({"_id":"10001"})"", R""({"name":"Tom","age":23})"", 0), 1); + EXPECT_EQ(GRD_UpsertDoc(db, "student", R""({"_id":"10001"})"", R""({"name":"Tom", "age":23})"", 0), 1); EXPECT_EQ(GRD_DropCollection(db, "student", 0), GRD_OK); @@ -245,7 +245,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest001, TestSize.Level0) R""({"maxConnNum":1000000007})"", R""({"maxConnNum":"16"})"", R""({"maxConnNum":{"value":17}})"", - R""({"maxConnNum":[16,17,18]})"", + R""({"maxConnNum":[16, 17, 18]})"", }; for (const auto &config : configList) { GLOGD("OpenDBConfigMaxConnNumTest001: test with config:%s", config.c_str()); @@ -363,7 +363,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest001, TestSize.Level0) R""({"pageSize":1000000007})"", R""({"pageSize":"4"})"", R""({"pageSize":{"value":8}})"", - R""({"pageSize":[16,32,64]})"", + R""({"pageSize":[16, 32, 64]})"", }; for (const auto &config : configList) { GLOGD("OpenDBConfigPageSizeTest001: test with config:%s", config.c_str()); @@ -500,11 +500,11 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigBufferPoolTest001, TestSize.Level0) GRD_DB *db = nullptr; std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), R""({"pageSize":64,"bufferPoolSize":4096})"", GRD_DB_OPEN_CREATE, &db); + int status = GRD_DBOpen(path.c_str(), R""({"pageSize":64, "bufferPoolSize":4096})"", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OK); EXPECT_EQ(GRD_DBClose(db, 0), GRD_OK); - status = GRD_DBOpen(path.c_str(), R""({"pageSize":64,"bufferPoolSize":4095})"", GRD_DB_OPEN_CREATE, &db); + status = GRD_DBOpen(path.c_str(), R""({"pageSize":64, "bufferPoolSize":4095})"", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_INVALID_ARGS); status = GRD_DBOpen(path.c_str(), R""({"bufferPoolSize":1023})"", GRD_DB_OPEN_CREATE, &db); @@ -526,11 +526,11 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigPubBuffTest001, TestSize.Level0) GRD_DB *db = nullptr; std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), R""({"pageSize":64,"redopubbufsize":4033})"", GRD_DB_OPEN_CREATE, &db); + int status = GRD_DBOpen(path.c_str(), R""({"pageSize":64, "redopubbufsize":4033})"", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OK); EXPECT_EQ(GRD_DBClose(db, 0), GRD_OK); - status = GRD_DBOpen(path.c_str(), R""({"pageSize":64,"redopubbufsize":4032})"", GRD_DB_OPEN_CREATE, &db); + status = GRD_DBOpen(path.c_str(), R""({"pageSize":64, "redopubbufsize":4032})"", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_INVALID_ARGS); status = GRD_DBOpen(path.c_str(), R""({"redopubbufsize":255})"", GRD_DB_OPEN_CREATE, &db); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_collection_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_collection_test.cpp index 92aedcd5..04431be4 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_collection_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_collection_test.cpp @@ -157,7 +157,7 @@ HWTEST_F(DocumentDBCollectionTest, CollectionTest005, TestSize.Level0) R""({"maxDoc":0})"", R""({"maxDoc":"123"})"", R""({"maxDoc":{"value":1024}})"", - R""({"maxDoc":[1,2,4,8]})"", + R""({"maxDoc":[1, 2, 4, 8]})"", R""({"minDoc":1024})"", }; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp index d43519a7..064f8004 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp @@ -149,7 +149,7 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest006, TestSize.Level0) HWTEST_F(DocumentDBDataTest, UpsertDataTest007, TestSize.Level0) { std::string filter = R""({"_id":"1234"})""; - std::string val = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::string val = R""({"name":"Tmono", "age":18, "addr":{"city":"shanghai", "postal":200001}})""; EXPECT_EQ(GRD_UpsertDoc(g_db, "collection_not_exists", filter.c_str(), val.c_str(), GRD_DOC_REPLACE), GRD_INVALID_ARGS); } @@ -164,10 +164,10 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest007, TestSize.Level0) HWTEST_F(DocumentDBDataTest, UpsertDataTest008, TestSize.Level0) { std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmn","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::string document = R""({"name":"Tmn", "age":18, "addr":{"city":"shanghai", "postal":200001}})""; EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), GRD_DOC_REPLACE), 1); - std::string updateDoc = R""({"name":"Xue","case":2,"age":28,"addr":{"city":"shenzhen","postal":518000}})""; + std::string updateDoc = R""({"name":"Xue", "case":2, "age":28, "addr":{"city":"shenzhen", "postal":518000}})""; EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), updateDoc.c_str(), GRD_DOC_APPEND), 1); } @@ -272,7 +272,7 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest005, TestSize.Level0) {} HWTEST_F(DocumentDBDataTest, UpdateDataTest006, TestSize.Level0) { std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::string document = R""({"name":"Tmono", "age":18, "addr":{"city":"shanghai", "postal":200001}})""; std::vector invalidFlags = { 2, 4, 8, 1024, UINT32_MAX }; for (auto flag : invalidFlags) { GLOGD("UpdateDataTest006: update data with flag: %u", flag); @@ -293,7 +293,7 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest007, TestSize.Level0) HWTEST_F(DocumentDBDataTest, UpdateDataTest008, TestSize.Level0) { const char *updateStr = - R""({"field2":{"c_field":{"cc_field":{"ccc_field":{"ccc_field":[1,false,1.234e2,["hello world!"]]}}}}})""; + R""({"field2":{"c_field":{"cc_field":{"ccc_field":{"ccc_field":[1, false, 1.234e2, ["hello world!"]]}}}}})""; int result = GRD_UpdateDoc(g_db, g_coll, "{\"field\" : 2}", updateStr, 0); int result2 = GRD_UpsertDoc(g_db, g_coll, "{\"field\" : 2}", updateStr, 0); EXPECT_EQ(result, GRD_INVALID_ARGS); @@ -318,7 +318,7 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest009, TestSize.Level0) char *value = nullptr; EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); string valueStr = value; - string repectStr = R""({"_id":"1234","field1":1,"field2":2,"FIELD1":[1,true,1.23456789,"hello world!",null]})""; + string repectStr = R""({"_id":"1234", "field1":1, "field2":2, "FIELD1":[1, true, 1.23456789, "hello world!", null]})""; EXPECT_EQ((valueStr == repectStr), 1); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp index 3fa8c92d..f773add4 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp @@ -49,7 +49,7 @@ static const char *g_document4 = "{\"_id\" : \"4\", \"name\":\"doc4\",\"item\":\ static const char *g_document5 = "{\"_id\" : \"5\", \"name\":\"doc5\",\"item\":\"journal\",\"personInfo\":\ [{\"sex\" : \"woma\", \"school\" : \"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; static const char *g_document6 = "{\"_id\" : \"6\", \"name\":\"doc6\",\"item\":false,\"personInfo\":\ - [{\"school\":\"B\", \"teacher\" : \"mike\",\"age\" : 15}, {\"school\":\"C\", \"teacher\" : \"moon\",\"age\":20}]}"; + [{\"school\":\"B\", \"teacher\" : \"mike\", \"age\" : 15}, {\"school\":\"C\", \"teacher\" : \"moon\", \"age\":20}]}"; static const char *g_document7 = "{\"_id\" : \"7\", \"name\":\"doc7\",\"item\":\"fruit\",\"other_Info\":\ [{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; static const char *g_document8 = "{\"_id\" : \"8\", \"name\":\"doc8\",\"item\":true,\"personInfo\":\ diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp index 54be32da..7ce58327 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp @@ -330,14 +330,14 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest014, TestSize.Level1) * @tc.expected:step1.GRD_INVALID_ARGS */ const char *document2 = R""({"level1" : {"level2" : {"level3" : [{ "level5" : "level5_1val", - "level5_2":"level5_2_val"}, "level4_val1","level4_val2"], "level3_2" : "level3_2_val"}},"_id":"14"})""; + "level5_2":"level5_2_val"}, "level4_val1","level4_val2"], "level3_2" : "level3_2_val"}}, "_id":"14"})""; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); /** * @tc.steps:step1.document's JSON depth is 4 * @tc.expected:step1.GRD_OK */ const char *document3 = R""({"level1" : {"level2" : {"level3" : { "level4" : "level5_1val"}, - "level3_2" : "level3_2_val"}},"_id":"14"})""; + "level3_2" : "level3_2_val"}}, "_id":"14"})""; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_OK); } @@ -563,8 +563,8 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest025, TestSize.Level1) * @tc.steps:step1.documents JSON depth is 4, which is allowed. * @tc.expected:step1.GRD_OK. */ - const char *document1 = "{\"_id\" : \"25_0\", \"level1\" : { \"level2\" : {\"level3\" :\ - {\"level4\" : \"level4Val\" } } } , \"level1_2\" : \"level1_2Val\" }"; + const char *document1 = "{\"_id\" : \"25_0\", \"level1\" : {\"level2\" : {\"level3\" :\ + {\"level4\" : \"level4Val\"}}} , \"level1_2\" : \"level1_2Val\" }"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); /** * @tc.steps:step2.documents JSON depth is exactly 4. diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp index aca10555..9b9a892c 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp @@ -44,9 +44,9 @@ void DocumentDBJsonCommonTest::TearDown(void) {} HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest001, TestSize.Level0) { - std::string document = R""({"name":"Tmn","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::string updateDoc = R""({"name":"Xue","case":{"field1":1,"field2":"string","field3":[1,2,3]}, - "age":28,"addr":{"city":"shenzhen","postal":518000}})""; + std::string document = R""({"name":"Tmn", "age":18, "addr":{"city":"shanghai", "postal":200001}})""; + std::string updateDoc = R""({"name":"Xue", "case":{"field1":1, "field2":"string", "field3":[1, 2, 3]}, + "age":28, "addr":{"city":"shenzhen", "postal":518000}})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -68,9 +68,9 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest001, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest002, TestSize.Level0) { - std::string document = R""({"name":"Tmn","case":2,"age":[1,2,3],"addr":{"city":"shanghai","postal":200001}})""; - std::string updateDoc = R""({"name":["Xue","Neco","Lip"],"grade":99,"age":18,"addr": - [{"city":"shanghai","postal":200001},{"city":"beijing","postal":100000}]})""; + std::string document = R""({"name":"Tmn", "case":2, "age":[1, 2, 3], "addr":{"city":"shanghai", "postal":200001}})""; + std::string updateDoc = R""({"name":["Xue", "Neco", "Lip"], "grade":99, "age":18, "addr": + [{"city":"shanghai", "postal":200001}, {"city":"beijing", "postal":100000}]})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -91,10 +91,10 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest002, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest003, TestSize.Level0) { - std::string document = R""({"name":["Tmn","BB","Alice"],"age":[1,2,3],"addr":[{"city":"shanghai","postal":200001}, - {"city":"wuhan","postal":430000}]})""; - std::string updateDoc = R""({"name":["Xue","Neco","Lip"],"age":18,"addr":[{"city":"shanghai","postal":200001}, - {"city":"beijing","postal":100000}]})""; + std::string document = R""({"name":["Tmn", "BB", "Alice"], "age":[1, 2, 3], "addr":[{"city":"shanghai", "postal":200001}, + {"city":"wuhan", "postal":430000}]})""; + std::string updateDoc = R""({"name":["Xue", "Neco", "Lip"], "age":18, "addr":[{"city":"shanghai", "postal":200001}, + {"city":"beijing", "postal":100000}]})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -129,7 +129,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest004, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest005, TestSize.Level0) { - std::string document = R""({"name":["Tmn","BB","Alice"]})""; + std::string document = R""({"name":["Tmn", "BB", "Alice"]})""; std::string updateDoc = R""({"name.2":"GG"})""; int errCode = E_OK; @@ -148,7 +148,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest005, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest006, TestSize.Level0) { - std::string document = R""({"name":{"first":"Tno","last":"moray"}})""; + std::string document = R""({"name":{"first":"Tno", "last":"moray"}})""; std::string updateDoc = R""({"name":{"midle.AA":"GG"}})""; int errCode = E_OK; @@ -167,7 +167,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest006, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest007, TestSize.Level0) { - std::string document = R""({"name":{"first":["XX","CC"],"last":"moray"}})""; + std::string document = R""({"name":{"first":["XX", "CC"], "last":"moray"}})""; std::string updateDoc = R""({"name.first.0":"LL"})""; int errCode = E_OK; @@ -186,8 +186,8 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest007, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest008, TestSize.Level0) { - std::string document = R""({"name":{"first":"XX","last":"moray"}})""; - std::string updateDoc = R""({"name":{"first":["XXX","BBB","CCC"]}})""; + std::string document = R""({"name":{"first":"XX", "last":"moray"}})""; + std::string updateDoc = R""({"name":{"first":["XXX", "BBB", "CCC"]}})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -205,7 +205,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest008, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest009, TestSize.Level0) { - std::string document = R""({"name":{"first":["XXX","BBB","CCC"],"last":"moray"}})""; + std::string document = R""({"name":{"first":["XXX", "BBB", "CCC"], "last":"moray"}})""; std::string updateDoc = R""({"name":{"first":"XX"}})""; int errCode = E_OK; @@ -286,7 +286,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest013, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest014, TestSize.Level0) { - std::string document = R""({"name":{"first":"Xue","second":"Lang"}})""; + std::string document = R""({"name":{"first":"Xue", "second":"Lang"}})""; std::string updateDoc = R""({"name.0":"GG"})""; int errCode = E_OK; @@ -301,7 +301,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest014, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest015, TestSize.Level0) { std::string document = R""({"name":{"first":"Xue","second":"Lang"}})""; - std::string updateDoc = R""({"name.first":["GG","MM"]})""; + std::string updateDoc = R""({"name.first":["GG", "MM"]})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -459,7 +459,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest012, TestSize.Leve { std::string document = R""({"item" : "journal", "instock" : [{"warehose" : "A", "qty" : 5}, {"warehose" : "C", "qty" : 15}]})""; - std::string filter = R""({"instock" : {"warehose" : "A", "bad" : "2" ,"qty" : 5}})""; + std::string filter = R""({"instock" : {"warehose" : "A", "bad" : "2" , "qty" : 5}})""; int errCode = E_OK; JsonObject srcObj = JsonObject::Parse(document, errCode); EXPECT_EQ(errCode, E_OK); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp index 93d46974..604f2c38 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp @@ -49,7 +49,7 @@ void DocumentDBJsonObjectTest::TearDown(void) {} */ HWTEST_F(DocumentDBJsonObjectTest, JsonObjectTest001, TestSize.Level0) { - const std::string config = R""({"a":123,"b":{"c":234,"d":"12345"}})""; + const std::string config = R""({"a":123, "b":{"c":234, "d":"12345"}})""; int ret = E_OK; JsonObject conf = JsonObject::Parse(config, ret); diff --git a/services/distributeddataservice/service/kvdb/ref_count.h b/services/distributeddataservice/service/kvdb/ref_count.h index 89ee9dd6..3ec72fe4 100644 --- a/services/distributeddataservice/service/kvdb/ref_count.h +++ b/services/distributeddataservice/service/kvdb/ref_count.h @@ -27,7 +27,7 @@ public: RefCount(RefCount &&other) noexcept; RefCount &operator=(const RefCount &other); RefCount &operator=(RefCount &&other) noexcept; - operator bool () const; + operator bool() const; private: std::shared_ptr ref_; diff --git a/services/distributeddataservice/service/kvdb/store_cache.h b/services/distributeddataservice/service/kvdb/store_cache.h index 2e4d726c..37e5133d 100644 --- a/services/distributeddataservice/service/kvdb/store_cache.h +++ b/services/distributeddataservice/service/kvdb/store_cache.h @@ -33,9 +33,9 @@ public: template struct Less { public: - bool operator()(const sptr &__x, const sptr &__y) const + bool operator()(const sptr &x, const sptr &y) const { - return __x.GetRefPtr() < __y.GetRefPtr(); + return x.GetRefPtr() < y.GetRefPtr(); } }; using DBStatus = DistributedDB::DBStatus; diff --git a/services/distributeddataservice/service/test/mock/db_store_mock.cpp b/services/distributeddataservice/service/test/mock/db_store_mock.cpp index e4f0f3b2..55d9ca88 100644 --- a/services/distributeddataservice/service/test/mock/db_store_mock.cpp +++ b/services/distributeddataservice/service/test/mock/db_store_mock.cpp @@ -263,7 +263,7 @@ DBStatus DBStoreMock::GetEntries(ConcurrentMap &store, const Key &ke store.ForEach([&entries, &keyPrefix](const Key &key, Value &value) { auto it = std::search(key.begin(), key.end(), keyPrefix.begin(), keyPrefix.end()); if (it == key.begin()) { - entries.push_back( { key, value } ); + entries.push_back({key, value}); } return false; }); @@ -275,7 +275,7 @@ DBStatus DBStoreMock::PutBatch(ConcurrentMap &store, const std::vect for (auto &entry : entries) { store.InsertOrAssign(entry.key, entry.value); } - DBChangeDataMock changeData( {}, entries, {} ); + DBChangeDataMock changeData({}, entries, {}); observers_.ForEachCopies([&changeData](const auto &observer, auto &keys) mutable { if (observer) { observer->OnChange(changeData); @@ -287,13 +287,13 @@ DBStatus DBStoreMock::PutBatch(ConcurrentMap &store, const std::vect DBStatus DBStoreMock::DeleteBatch(ConcurrentMap &store, const std::vector &keys) { - DBChangeDataMock changeData( {}, {}, {} ); + DBChangeDataMock changeData({}, {}, {}); for (auto &key : keys) { auto it = store.Find(key); if (!it.first) { continue; } - changeData.AddEntry( {key, std::move(it.second)} ); + changeData.AddEntry({key, std::move(it.second)}); store.Erase(key); } -- Gitee From 005f04042ab14c76abdab0b5b0b63bb531b3e99a Mon Sep 17 00:00:00 2001 From: wellinleo Date: Sun, 21 May 2023 10:40:00 +0800 Subject: [PATCH 77/95] modify codecheck Signed-off-by: wellinleo --- .../distributeddataservice/app/src/dump_helper.cpp | 12 ++++++------ .../service/data_share/common/bundle_mgr_proxy.cpp | 2 +- .../service/data_share/common/rdb_delegate.cpp | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/app/src/dump_helper.cpp b/services/distributeddataservice/app/src/dump_helper.cpp index 225a0e17..0c5f77a5 100644 --- a/services/distributeddataservice/app/src/dump_helper.cpp +++ b/services/distributeddataservice/app/src/dump_helper.cpp @@ -114,22 +114,22 @@ void DumpHelper::ShowHelp(int fd) result.append("Usage:dump [options]\n") .append("Description:\n") .append(CMD_USER_INFO) - .append(" ") + .append(" ") .append("dump all user information in the system\n") .append(CMD_APP_INFO) - .append(" ") + .append(" ") .append("dump list of all app information in the system\n") .append(CMD_APP_INFO) - .append(" [appID] ") + .append(" [appID] ") .append("dump information about the specified app in the system\n") .append(CMD_STORE_INFO) - .append(" ") + .append(" ") .append("dump list of all store information in the system\n") .append(CMD_STORE_INFO) - .append(" [storeID] ") + .append(" [storeID] ") .append("dump information about the specified store in the system\n") .append(CMD_ERROR_INFO) - .append(" ") + .append(" ") .append("dump the recent errors information in the system\n"); dprintf(fd, "%s\n", result.c_str()); } diff --git a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp index 65319772..58da8094 100644 --- a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp @@ -85,7 +85,7 @@ BundleMgrProxy::~BundleMgrProxy() std::shared_ptr BundleMgrProxy::GetInstance() { - static std::shared_ptr proxy(std::make_shared()); + static std::shared_ptr proxy = std::make_shared(); return proxy; } } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 2d3dd071..383fac2a 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -131,7 +131,7 @@ std::shared_ptr RdbDelegate::Query(const std::string &tableN return nullptr; } auto bridge = RdbDataShareAdapter::RdbUtils::ToResultSetBridge(resultSet); - return std::shared_ptr(std::make_shared(bridge), [](auto p) { + return std::shared_ptr(new DataShareResultSet(bridge), [](auto p) { ZLOGD("release resultset"); resultSetCount--; delete p; -- Gitee From 58d5c083cbf14d3d214d4ff1ee022f0a85a38ca3 Mon Sep 17 00:00:00 2001 From: wellinleo Date: Sun, 21 May 2023 11:01:54 +0800 Subject: [PATCH 78/95] update Signed-off-by: wellinleo --- .../gaussdb_rd/test/unittest/api/documentdb_data_test.cpp | 3 ++- .../gaussdb_rd/test/unittest/api/documentdb_find_test.cpp | 3 ++- .../unittest/oh_adapter/documentdb_json_common_test.cpp | 7 ++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp index 064f8004..1ea370b5 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp @@ -318,7 +318,8 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest009, TestSize.Level0) char *value = nullptr; EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); string valueStr = value; - string repectStr = R""({"_id":"1234", "field1":1, "field2":2, "FIELD1":[1, true, 1.23456789, "hello world!", null]})""; + string repectStr = R""({"_id":"1234", "field1":1, "field2":2, + "FIELD1":[1, true, 1.23456789, "hello world!", null]})""; EXPECT_EQ((valueStr == repectStr), 1); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp index f773add4..0530f99c 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp @@ -49,7 +49,8 @@ static const char *g_document4 = "{\"_id\" : \"4\", \"name\":\"doc4\",\"item\":\ static const char *g_document5 = "{\"_id\" : \"5\", \"name\":\"doc5\",\"item\":\"journal\",\"personInfo\":\ [{\"sex\" : \"woma\", \"school\" : \"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; static const char *g_document6 = "{\"_id\" : \"6\", \"name\":\"doc6\",\"item\":false,\"personInfo\":\ - [{\"school\":\"B\", \"teacher\" : \"mike\", \"age\" : 15}, {\"school\":\"C\", \"teacher\" : \"moon\", \"age\":20}]}"; + [{\"school\":\"B\", \"teacher\" : \"mike\", \"age\" : 15}, + {\"school\":\"C\", \"teacher\" : \"moon\", \"age\" : 20}]}"; static const char *g_document7 = "{\"_id\" : \"7\", \"name\":\"doc7\",\"item\":\"fruit\",\"other_Info\":\ [{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; static const char *g_document8 = "{\"_id\" : \"8\", \"name\":\"doc8\",\"item\":true,\"personInfo\":\ diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp index 9b9a892c..bec7c70a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp @@ -68,7 +68,8 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest001, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest002, TestSize.Level0) { - std::string document = R""({"name":"Tmn", "case":2, "age":[1, 2, 3], "addr":{"city":"shanghai", "postal":200001}})""; + std::string document = R""({"name":"Tmn", "case":2, "age":[1, 2, 3], + "addr":{"city":"shanghai", "postal":200001}})""; std::string updateDoc = R""({"name":["Xue", "Neco", "Lip"], "grade":99, "age":18, "addr": [{"city":"shanghai", "postal":200001}, {"city":"beijing", "postal":100000}]})""; @@ -91,8 +92,8 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest002, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest003, TestSize.Level0) { - std::string document = R""({"name":["Tmn", "BB", "Alice"], "age":[1, 2, 3], "addr":[{"city":"shanghai", "postal":200001}, - {"city":"wuhan", "postal":430000}]})""; + std::string document = R""({"name":["Tmn", "BB", "Alice"], "age":[1, 2, 3], + "addr":[{"city":"shanghai", "postal":200001}, {"city":"wuhan", "postal":430000}]})""; std::string updateDoc = R""({"name":["Xue", "Neco", "Lip"], "age":18, "addr":[{"city":"shanghai", "postal":200001}, {"city":"beijing", "postal":100000}]})""; -- Gitee From 723296216632cf4baa9ac6211e9cade47aafc47c Mon Sep 17 00:00:00 2001 From: wellinleo Date: Sun, 21 May 2023 11:10:03 +0800 Subject: [PATCH 79/95] update Signed-off-by: wellinleo --- .../service/data_share/common/bundle_mgr_proxy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp index 58da8094..4519382f 100644 --- a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp @@ -85,7 +85,7 @@ BundleMgrProxy::~BundleMgrProxy() std::shared_ptr BundleMgrProxy::GetInstance() { - static std::shared_ptr proxy = std::make_shared(); + static std::shared_ptr proxy(new BundleMgrProxy()); return proxy; } } // namespace OHOS::DataShare \ No newline at end of file -- Gitee From 0d19568e68879ec1ae69e87e022980785f201562 Mon Sep 17 00:00:00 2001 From: wellinleo Date: Sun, 21 May 2023 11:26:03 +0800 Subject: [PATCH 80/95] update Signed-off-by: wellinleo --- .../gaussdb_rd/test/unittest/api/documentdb_find_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp index 0530f99c..f77761d8 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp @@ -49,8 +49,9 @@ static const char *g_document4 = "{\"_id\" : \"4\", \"name\":\"doc4\",\"item\":\ static const char *g_document5 = "{\"_id\" : \"5\", \"name\":\"doc5\",\"item\":\"journal\",\"personInfo\":\ [{\"sex\" : \"woma\", \"school\" : \"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; static const char *g_document6 = "{\"_id\" : \"6\", \"name\":\"doc6\",\"item\":false,\"personInfo\":\ - [{\"school\":\"B\", \"teacher\" : \"mike\", \"age\" : 15}, + [{\"school\":\"B\", \"teacher\" : \"mike\", \"age\" : 15},\ {\"school\":\"C\", \"teacher\" : \"moon\", \"age\" : 20}]}"; + static const char *g_document7 = "{\"_id\" : \"7\", \"name\":\"doc7\",\"item\":\"fruit\",\"other_Info\":\ [{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; static const char *g_document8 = "{\"_id\" : \"8\", \"name\":\"doc8\",\"item\":true,\"personInfo\":\ -- Gitee From b38ddba9780facb86f5e8b06428b4f7866215b9d Mon Sep 17 00:00:00 2001 From: e Date: Sun, 21 May 2023 15:23:36 +0800 Subject: [PATCH 81/95] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: e --- .../distributeddataservice/app/src/kvstore_data_service.h | 2 +- services/distributeddataservice/app/src/task_manager.cpp | 3 +-- .../app/src/uninstaller/uninstaller_impl.h | 2 +- .../distributeddataservice/service/rdb/rdb_general_store.h | 4 ++-- .../distributeddataservice/service/rdb/rdb_service_impl.cpp | 2 +- .../distributeddataservice/service/test/cloud_data_test.cpp | 4 ++-- 6 files changed, 8 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index 60aac352..cbb4ae01 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -121,7 +121,7 @@ private: Status AppExit(pid_t uid, pid_t pid, uint32_t token, const AppId &appId); bool ResolveAutoLaunchParamByIdentifier(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m); - void ResolveAutoLaunchCompatible(const StoreMetaData &meta, const std::string &identifier); + void ResolveAutoLaunchCompatible(const StoreMetaData &storeMeta, const std::string &identifier); static DistributedDB::SecurityOption ConvertSecurity(int securityLevel); static Status InitNbDbOption(const Options &options, const std::vector &cipherKey, DistributedDB::KvStoreNbDelegate::Option &dbOption); diff --git a/services/distributeddataservice/app/src/task_manager.cpp b/services/distributeddataservice/app/src/task_manager.cpp index 1332fc20..ec06928e 100644 --- a/services/distributeddataservice/app/src/task_manager.cpp +++ b/services/distributeddataservice/app/src/task_manager.cpp @@ -15,9 +15,8 @@ #include "task_manager.h" namespace OHOS::DistributedData { -TaskManager::TaskManager(std::shared_ptr executors) +TaskManager::TaskManager(std::shared_ptr executors) : executors_(executors) { - executors_ = executors; } TaskManager::~TaskManager() diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h index 3922fbad..e6498a67 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h @@ -50,7 +50,7 @@ public: private: static constexpr int32_t RETRY_TIME = 300; static constexpr int32_t RETRY_INTERVAL = 100; - int32_t retryTime_; + int32_t retryTime_ = 0; ExecutorPool::Task GetTask(); std::shared_ptr subscriber_ {}; std::shared_ptr executors_; diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h index f1bd92cd..c85014cc 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -38,7 +38,7 @@ public: using RdbDelegate = DistributedDB::RelationalStoreDelegate; using RdbManager = DistributedDB::RelationalStoreManager; - explicit RdbGeneralStore(const StoreMetaData &metaData); + explicit RdbGeneralStore(const StoreMetaData &meta); int32_t Bind(std::shared_ptr cloudDb) override; int32_t SetSchema(const SchemaMeta &schemaMeta) override; int32_t Execute(const std::string &table, const std::string &sql) override; @@ -59,7 +59,7 @@ private: RdbDelegate *delegate_ = nullptr; std::shared_ptr store_; std::shared_ptr cloudDb_; - Watcher *watcher_; + Watcher *watcher_ = nullptr; }; } // namespace OHOS::DistributedRdb #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_STORE_H diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 525aed43..96c30eda 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -438,7 +438,7 @@ int32_t RdbServiceImpl::DoSubscribe(const RdbSyncerParam& param, const Subscribe void RdbServiceImpl::OnChange(uint32_t tokenId, const std::string &storeName) { pid_t pid = 0; - syncAgents_.ComputeIfPresent(tokenId, [&pid, &storeName](auto &key, SyncAgent &value) { + syncAgents_.ComputeIfPresent(tokenId, [&pid, &storeName](auto &key, const SyncAgent &value) { pid = value.pid_; return true; }); diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index 031ea345..79f12f85 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -32,7 +32,7 @@ using namespace testing::ext; using namespace OHOS::DistributedData; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; -namespace OHOS { +namespace OHOS::Test { namespace DistributedDataTest { static constexpr const char *TEST_CLOUD_BUNDLE = "test_cloud_bundleName"; static constexpr const char *TEST_CLOUD_APPID = "test_cloud_appid"; @@ -185,4 +185,4 @@ HWTEST_F(CloudDataTest, GetSchema, TestSize.Level0) MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), storeMetaData, true)); } } // namespace DistributedDataTest -} // namespace OHOS +} // namespace OHOS::Test -- Gitee From 4a89dc3b04ba0baff80d618bef39e020faa53f2d Mon Sep 17 00:00:00 2001 From: e Date: Mon, 22 May 2023 10:30:05 +0800 Subject: [PATCH 82/95] =?UTF-8?q?=E9=83=A8=E4=BB=B6=E5=8C=96=E9=81=97?= =?UTF-8?q?=E7=95=99=E9=97=AE=E9=A2=98=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: e --- bundle.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bundle.json b/bundle.json index d7325b51..98a7a3bc 100644 --- a/bundle.json +++ b/bundle.json @@ -1,6 +1,6 @@ { - "name": "@ohos/distributeddatamgr_datamgr_service", - "version": "1.0.0", + "name": "@ohos/datamgr_service", + "version": "3.2.0", "description": "Distributed data manager that provides the capability to store data in the databases of different devices", "homePage": "https://gitee.com/openharmony", "license": "Apache V2", @@ -46,8 +46,8 @@ "adapted_system_type": [ "standard" ], - "rom": "", - "ram": "", + "rom": "5120KB", + "ram": "8192KB", "hisysevent_config": [ "//foundation/distributeddatamgr/datamgr_service/hisysevent.yaml" ], -- Gitee From c82d4084d740da6b8133451f39a0fa5d0b7c75ee Mon Sep 17 00:00:00 2001 From: lianhuix Date: Mon, 22 May 2023 10:41:39 +0800 Subject: [PATCH 83/95] Fix compile issues Signed-off-by: lianhuix --- .../service/data_share/BUILD.gn | 3 ++- .../data_share/datamgr_service_data_share.gni | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 services/distributeddataservice/service/data_share/datamgr_service_data_share.gni diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn index b9490bbc..be7efa51 100644 --- a/services/distributeddataservice/service/data_share/BUILD.gn +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -13,6 +13,7 @@ import("//build/ohos.gni") import("//build/ohos_var.gni") import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") +import("datamgr_service_data_share.gni") config("module_public_config") { visibility = [ ":*" ] include_dirs = [ @@ -77,10 +78,10 @@ ohos_shared_library("data_share_service") { configs = [ ":module_public_config" ] deps = [ + "${gaussrd_path}:gaussdb_rd", "../../adapter:distributeddata_adapter", "../../adapter/utils:distributeddata_utils_static", "../../framework:distributeddatasvcfwk", - "gaussdb_rd:gaussdb_rd", ] external_deps = [ diff --git a/services/distributeddataservice/service/data_share/datamgr_service_data_share.gni b/services/distributeddataservice/service/data_share/datamgr_service_data_share.gni new file mode 100644 index 00000000..5ac6d628 --- /dev/null +++ b/services/distributeddataservice/service/data_share/datamgr_service_data_share.gni @@ -0,0 +1,23 @@ +# Copyright (c) 2023 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. + +declare_args() { + gaussrd_path = "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share/gaussdb_rd" + if (defined(global_parts_info) && + defined(global_parts_info.distributeddatamgr_kv_store_lite)) { + gaussrd_path = "//foundation/distributeddatamgr/kv_store_lite/gaussdb_rd" + gaussrd_enchance = true + } else { + gaussrd_enchance = false + } +} -- Gitee From baad2794739deecead51c7b0f4b0d09f9f70e0d1 Mon Sep 17 00:00:00 2001 From: niudongyao Date: Mon, 22 May 2023 15:41:49 +0800 Subject: [PATCH 84/95] fix warn Signed-off-by: niudongyao --- .../service/data_share/common/scheduler_manager.cpp | 2 +- .../service/data_share/common/scheduler_manager.h | 3 ++- .../service/data_share/common/template_manager.cpp | 4 ++-- .../service/data_share/common/template_manager.h | 2 +- .../service/data_share/data/published_data.h | 2 +- .../service/data_share/data_share_service_stub.cpp | 2 +- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp index 85d7e530..86032a1b 100644 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp @@ -38,7 +38,7 @@ void SchedulerManager::Execute(const std::string &uri, const std::string &rdbDir return; } std::vector keys = RdbSubscriberManager::GetInstance().GetKeysByUri(uri); - for (auto &key : keys) { + for (auto const &key : keys) { if (RdbSubscriberManager::GetInstance().GetObserverCount(key) == 0) { continue; } diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.h b/services/distributeddataservice/service/data_share/common/scheduler_manager.h index 546d77be..d5e5479b 100644 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.h +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.h @@ -37,7 +37,8 @@ private: static constexpr size_t TIME_TASK_NUM = 10; SchedulerManager() = default; ~SchedulerManager() = default; - void GenRemindTimerFuncParams(const std::string &rdbDir, int version, const Key &key, std::string &schedulerSQL); + static void GenRemindTimerFuncParams(const std::string &rdbDir, int version, const Key &key, + std::string &schedulerSQL); void ExecuteSchedulerSQL(const std::string &rdbDir, int version, const Key &key, std::shared_ptr delegate); diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index fabed5f1..67e435e8 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -413,9 +413,9 @@ void PublishedDataSubscriberManager::Emit(const std::vector &k void PublishedDataSubscriberManager::PutInto( std::map, std::vector> &callbacks, - std::vector &val, const PublishedDataKey &key, const sptr observer) + const std::vector &val, const PublishedDataKey &key, const sptr observer) { - for (auto &callback : val) { + for (auto const &callback : val) { if (callback.enabled && callback.observer != nullptr) { // callback the observer, others do not call if (observer != nullptr && callback.observer != observer) { diff --git a/services/distributeddataservice/service/data_share/common/template_manager.h b/services/distributeddataservice/service/data_share/common/template_manager.h index f6b4e5a5..72af2ca4 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.h +++ b/services/distributeddataservice/service/data_share/common/template_manager.h @@ -111,7 +111,7 @@ private: }; PublishedDataSubscriberManager() = default; void PutInto(std::map, std::vector> &, - std::vector &, const PublishedDataKey &, const sptr); + const std::vector &, const PublishedDataKey &, const sptr); ConcurrentMap> publishedDataCache; }; } // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/data/published_data.h b/services/distributeddataservice/service/data_share/data/published_data.h index daa7d667..ebc633bf 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.h +++ b/services/distributeddataservice/service/data_share/data/published_data.h @@ -41,7 +41,7 @@ public: static int32_t Query(const std::string &filter, std::variant, std::string> &publishedData); static std::string GenId(const std::string &key, const std::string &bundleName, int64_t subscriberId); PublishedData(const std::string &key, const std::string &bundleName, int64_t subscriberId, - const std::variant, std::string> &value, const int version); + const std::variant, std::string> &inputValue, const int version); ~PublishedData() = default; bool HasVersion() const override; int GetVersion() const override; diff --git a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp index 023f4e75..edfa7b88 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp @@ -317,7 +317,7 @@ int DataShareServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Me if (!CheckInterfaceToken(data)) { return DATA_SHARE_ERROR; } - if (code >= 0 && code < DATA_SHARE_SERVICE_CMD_MAX) { + if (code < DATA_SHARE_SERVICE_CMD_MAX) { return (this->*HANDLERS[code])(data, reply); } return -1; -- Gitee From d8c9e2941e1af1e7f301b7182109f46c0e55af0c Mon Sep 17 00:00:00 2001 From: niudongyao Date: Mon, 22 May 2023 16:03:58 +0800 Subject: [PATCH 85/95] fix warn Signed-off-by: niudongyao --- .../service/data_share/common/template_manager.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index 67e435e8..f47ad59a 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -413,7 +413,8 @@ void PublishedDataSubscriberManager::Emit(const std::vector &k void PublishedDataSubscriberManager::PutInto( std::map, std::vector> &callbacks, - const std::vector &val, const PublishedDataKey &key, const sptr observer) + const std::vector &val, const PublishedDataKey &key, + const sptr observer) { for (auto const &callback : val) { if (callback.enabled && callback.observer != nullptr) { -- Gitee From 48fc64136c67e30220df961442db657db7f1e498 Mon Sep 17 00:00:00 2001 From: hanlu Date: Mon, 22 May 2023 21:21:24 +0800 Subject: [PATCH 86/95] f Signed-off-by: hanlu --- .../service/data_share/BUILD.gn | 1 - .../data_share/common/template_manager.cpp | 4 +-- .../data_share/data/json_formatter.cpp | 30 ---------------- .../service/data_share/data/json_formatter.h | 36 ------------------- 4 files changed, 1 insertion(+), 70 deletions(-) delete mode 100644 services/distributeddataservice/service/data_share/data/json_formatter.cpp delete mode 100644 services/distributeddataservice/service/data_share/data/json_formatter.h diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn index b9490bbc..f47d5b04 100644 --- a/services/distributeddataservice/service/data_share/BUILD.gn +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -43,7 +43,6 @@ ohos_shared_library("data_share_service") { "common/seq_strategy.cpp", "common/template_manager.cpp", "common/uri_utils.cpp", - "data/json_formatter.cpp", "data/published_data.cpp", "data/resultset_json_formatter.cpp", "data/template_data.cpp", diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index fabed5f1..d2514c15 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -17,7 +17,6 @@ #include "template_manager.h" #include "db_delegate.h" -#include "json_formatter.h" #include "log_print.h" #include "published_data.h" #include "scheduler_manager.h" @@ -287,8 +286,7 @@ int RdbSubscriberManager::Notify( changeNode.templateId_.bundleName_ = key.bundleName; for (const auto &predicate : tpl.predicates_) { std::string result = delegate->Query(predicate.selectSql_); - JsonFormatter formatter(predicate.key_, result); - changeNode.data_.emplace_back(DistributedData::Serializable::Marshall(formatter)); + changeNode.data_.emplace_back("{\"" + predicate.key_ + "\":" + result + "}"); } ZLOGI("emit, size %{public}zu %{private}s", val.size(), changeNode.uri_.c_str()); diff --git a/services/distributeddataservice/service/data_share/data/json_formatter.cpp b/services/distributeddataservice/service/data_share/data/json_formatter.cpp deleted file mode 100644 index b3c38832..00000000 --- a/services/distributeddataservice/service/data_share/data/json_formatter.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2023 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 "JsonFormatter" -#include "json_formatter.h" - -#include "log_print.h" - -namespace OHOS::DataShare { -bool JsonFormatter::Marshal(json &node) const -{ - return SetValue(node[key_], value_); -} - -bool JsonFormatter::Unmarshal(const json &node) -{ - return GetValue(node, key_, value_); -} -} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/json_formatter.h b/services/distributeddataservice/service/data_share/data/json_formatter.h deleted file mode 100644 index 0b718493..00000000 --- a/services/distributeddataservice/service/data_share/data/json_formatter.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2023 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 DATASHARESERVICE_JSON_FORMATTER_H -#define DATASHARESERVICE_JSON_FORMATTER_H - -#include "serializable/serializable.h" - -namespace OHOS::DataShare { -class JsonFormatter : public DistributedData::Serializable { -public: - JsonFormatter(const std::string &key, const std::string &value) - : key_(key), value_(value) - { - } - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; - -private: - std::string key_; - std::string value_; -}; -} // namespace OHOS::DataShare -#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H -- Gitee From 0e8fc8f6fad92c6fdeea4139d4c3fe3fe3aca7d7 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Tue, 23 May 2023 10:49:11 +0800 Subject: [PATCH 87/95] user specific declaration Signed-off-by: zwtmichael --- services/distributeddataservice/service/rdb/rdb_syncer.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/services/distributeddataservice/service/rdb/rdb_syncer.cpp index b03c65ca..9a38687a 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -434,7 +434,8 @@ int32_t RdbSyncer::DoSync(const SyncOption &option, const RdbPredicates &predica ZLOGI("delegate sync"); return delegate->Sync(devices, static_cast(option.mode), - MakeQuery(predicates), [&result] (const auto& syncStatus) { + MakeQuery(predicates), [&result] (const std::map> &syncStatus) { HandleSyncStatus(syncStatus, result); }, true); } @@ -456,7 +457,8 @@ int32_t RdbSyncer::DoAsync(const SyncOption &option, const RdbPredicates &predic ZLOGI("delegate sync"); return delegate->Sync(devices, static_cast(option.mode), - MakeQuery(predicates), [callback] (const auto& syncStatus) { + MakeQuery(predicates), [callback] (const std::map> &syncStatus) { SyncResult result; HandleSyncStatus(syncStatus, result); callback(result); -- Gitee From a10ec873d3edf6d916d6c8ee7b4ebb8c94b4f79c Mon Sep 17 00:00:00 2001 From: niudongyao Date: Tue, 23 May 2023 14:35:29 +0800 Subject: [PATCH 88/95] task_scheduler -> executor_pool Signed-off-by: niudongyao --- .../data_share/common/scheduler_manager.cpp | 25 +++++++++++-------- .../data_share/common/scheduler_manager.h | 8 +++--- .../data_share/data_share_service_impl.cpp | 2 ++ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp index 86032a1b..d694b011 100644 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp @@ -59,19 +59,20 @@ void SchedulerManager::Execute(const Key &key, const std::string &rdbDir, int ve void SchedulerManager::SetTimer(const std::string &dbPath, int version, const Key &key, int64_t reminderTime) { std::lock_guard lock(mutex_); - if (scheduler_ == nullptr) { - scheduler_ = std::make_shared(TIME_TASK_NUM, "remind_timer"); + if (executor_ == nullptr) { + ZLOGE("executor_ is nullptr"); + return; } auto it = timerCache_.find(key); if (it != timerCache_.end()) { // has current timer, reset time ZLOGD("has current taskId, uri is %{public}s, subscriberId is %{public}" PRId64 ", bundleName is %{public}s", DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); - scheduler_->Reset(it->second, std::chrono::seconds(reminderTime - time(nullptr))); + executor_->Reset(it->second, std::chrono::seconds(reminderTime - time(nullptr))); return; } // not find task in map, create new timer - auto taskId = scheduler_->At(TaskScheduler::Clock::now() + std::chrono::seconds(reminderTime - time(nullptr)), + auto taskId = executor_->Schedule(std::chrono::seconds(reminderTime - time(nullptr)), [key, dbPath, version, this]() { timerCache_.erase(key); // 1. execute schedulerSQL in next time @@ -79,7 +80,7 @@ void SchedulerManager::SetTimer(const std::string &dbPath, int version, const Ke // 2. notify RdbSubscriberManager::GetInstance().EmitByKey(key, dbPath, version); }); - if (taskId == TaskScheduler::INVALID_TASK_ID) { + if (taskId == ExecutorPool::INVALID_TASK_ID) { ZLOGE("create timer failed, over the max capacity"); return; } @@ -143,20 +144,22 @@ void SchedulerManager::GenRemindTimerFuncParams(const std::string &rdbDir, int v void SchedulerManager::RemoveTimer(const Key &key) { std::lock_guard lock(mutex_); - if (scheduler_ == nullptr) { - ZLOGD("scheduler_ is nullptr"); + if (executor_ == nullptr) { + ZLOGE("executor_ is nullptr"); return; } auto it = timerCache_.find(key); if (it != timerCache_.end()) { ZLOGD("RemoveTimer %{public}s %{public}s %{public}" PRId64, DistributedData::Anonymous::Change(key.uri).c_str(), key.bundleName.c_str(), key.subscriberId); - scheduler_->Remove(it->second); + executor_->Remove(it->second); timerCache_.erase(key); - if (timerCache_.empty()) { - scheduler_ = nullptr; - } } } + +void SchedulerManager::SetExecutorPool(std::shared_ptr executor) +{ + executor_ = executor; +} } // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.h b/services/distributeddataservice/service/data_share/common/scheduler_manager.h index d5e5479b..05a75fa3 100644 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.h +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.h @@ -19,7 +19,7 @@ #include #include "db_delegate.h" -#include "task_scheduler.h" +#include "executor_pool.h" #include "template_manager.h" namespace OHOS::DataShare { @@ -30,11 +30,11 @@ public: void Execute(const Key &key, const std::string &rdbDir, int version); void SetTimer(const std::string &dbPath, int version, const Key &key, int64_t reminderTime); void RemoveTimer(const Key &key); + void SetExecutorPool(std::shared_ptr executor); private: static constexpr const char *REMIND_TIMER_FUNC = "remindTimer("; static constexpr int REMIND_TIMER_FUNC_LEN = 12; - static constexpr size_t TIME_TASK_NUM = 10; SchedulerManager() = default; ~SchedulerManager() = default; static void GenRemindTimerFuncParams(const std::string &rdbDir, int version, const Key &key, @@ -43,8 +43,8 @@ private: std::shared_ptr delegate); std::mutex mutex_; - std::map timerCache_; - std::shared_ptr scheduler_ = nullptr; + std::map timerCache_; + std::shared_ptr executor_ = nullptr; }; } // namespace OHOS::DataShare #endif // SCHEDULER_MANAGER_H diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 3209b7cc..dedfcb72 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -336,6 +336,7 @@ enum DataShareKvStoreType : int32_t { int32_t DataShareServiceImpl::OnBind(const BindInfo &binderInfo) { binderInfo_ = binderInfo; + SchedulerManager::GetInstance().SetExecutorPool(binderInfo.executors); const std::string accountId = DistributedKv::AccountDelegate::GetInstance()->GetCurrentAccountId(); const auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(binderInfo.selfTokenId); DistributedData::StoreMetaData saveMeta; @@ -355,6 +356,7 @@ int32_t DataShareServiceImpl::OnBind(const BindInfo &binderInfo) saveMeta.storeType = DATA_SHARE_SINGLE_VERSION; saveMeta.dataDir = DistributedData::DirectoryManager::GetInstance().GetStorePath(saveMeta); KvDBDelegate::GetInstance(false, saveMeta.dataDir); + SchedulerManager::GetInstance().SetExecutorPool(binderInfo.executors); return EOK; } -- Gitee From a95a8ac2a68f5bddec1ff424a5174b1fd1263f56 Mon Sep 17 00:00:00 2001 From: niudongyao Date: Tue, 23 May 2023 14:36:36 +0800 Subject: [PATCH 89/95] task_scheduler -> executor_pool Signed-off-by: niudongyao --- .../service/data_share/data_share_service_impl.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index dedfcb72..c947256a 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -336,7 +336,6 @@ enum DataShareKvStoreType : int32_t { int32_t DataShareServiceImpl::OnBind(const BindInfo &binderInfo) { binderInfo_ = binderInfo; - SchedulerManager::GetInstance().SetExecutorPool(binderInfo.executors); const std::string accountId = DistributedKv::AccountDelegate::GetInstance()->GetCurrentAccountId(); const auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(binderInfo.selfTokenId); DistributedData::StoreMetaData saveMeta; -- Gitee From da5fdf1f6fd907082ccce1bdcdf7e3c92014dbcb Mon Sep 17 00:00:00 2001 From: e Date: Tue, 23 May 2023 15:11:11 +0800 Subject: [PATCH 90/95] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: e --- .../app/src/kvstore_data_service.cpp | 2 +- .../framework/store/auto_cache.cpp | 11 +++++------ .../service/kvdb/kvdb_service_impl.cpp | 1 - .../service/kvdb/store_cache.cpp | 11 +++++------ 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 4c5dc06f..8138acdc 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -161,7 +161,7 @@ void KvStoreDataService::LoadFeatures() { ZLOGI("begin."); auto features = FeatureSystem::GetInstance().GetFeatureName(FeatureSystem::BIND_NOW); - for (auto &feature : features) { + for (auto const &feature : features) { GetFeatureInterface(feature); } } diff --git a/services/distributeddataservice/framework/store/auto_cache.cpp b/services/distributeddataservice/framework/store/auto_cache.cpp index db51ad5c..53f671ee 100644 --- a/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/services/distributeddataservice/framework/store/auto_cache.cpp @@ -174,13 +174,12 @@ bool AutoCache::Delegate::Close() std::unique_lock lock(mutex_); if (store_ != nullptr) { store_->Unwatch(ORIGIN_ALL, *this); + auto status = store_->Close(); + if (status == Error::E_BUSY) { + return false; + } + store_ = nullptr; } - - auto status = store_->Close(); - if (status == Error::E_BUSY) { - return false; - } - store_ = nullptr; return true; } diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 8428cb58..3a51a06f 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -795,7 +795,6 @@ int32_t KVDBServiceImpl::OnExecutor(std::shared_ptr executors) executors_ = executors; storeCache_.SetThreadPool(executors); KvStoreSyncManager::GetInstance()->SetThreadPool(executors); - ZLOGE("onexecutor:%{public}p", executors.get()); return 0; } } // namespace OHOS::DistributedKv \ No newline at end of file diff --git a/services/distributeddataservice/service/kvdb/store_cache.cpp b/services/distributeddataservice/service/kvdb/store_cache.cpp index 093e8e5a..786f528d 100644 --- a/services/distributeddataservice/service/kvdb/store_cache.cpp +++ b/services/distributeddataservice/service/kvdb/store_cache.cpp @@ -234,13 +234,12 @@ bool StoreCache::DBStoreDelegate::Close(DBManager &manager) std::unique_lock lock(mutex_); if (delegate_ != nullptr) { delegate_->UnRegisterObserver(this); + auto status = manager.CloseKvStore(delegate_); + if (status == DBStatus::BUSY) { + return false; + } + delegate_ = nullptr; } - - auto status = manager.CloseKvStore(delegate_); - if (status == DBStatus::BUSY) { - return false; - } - delegate_ = nullptr; return true; } -- Gitee From 4df4f4d96f456a1ac3b36404b43c8127297da463 Mon Sep 17 00:00:00 2001 From: e Date: Tue, 23 May 2023 16:11:48 +0800 Subject: [PATCH 91/95] change4 Signed-off-by: e --- .../distributeddataservice/app/src/flowctrl_manager/BUILD.gn | 1 - .../distributeddataservice/service/cloud/cloud_service_impl.cpp | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn b/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn index 61f30728..eae2ee83 100755 --- a/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn +++ b/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn @@ -23,7 +23,6 @@ ohos_static_library("distributeddata_flowctrl_static") { "${kv_store_path}/frameworks/innerkitsimpl/distributeddatafwk/include", "${kv_store_path}/interfaces/innerkits/distributeddata/include", "//third_party/json/single_include", - "//commonlibrary/c_utils/base/include", ] cflags_cc = [ "-fvisibility=hidden" ] diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index b605daf5..9ec87a5e 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -447,7 +447,6 @@ bool CloudServiceImpl::DoSubscribe(const Subscription &sub) std::map> unsubDbs; for (auto &app : cloudInfo.apps) { auto enabled = cloudInfo.enableCloud && app.cloudSwitch; - auto &dbs = enabled ? subDbs : unsubDbs; auto it = sub.expiresTime.find(app.bundleName); // cloud is enabled, but the subscription won't expire if (enabled && (it != sub.expiresTime.end() && it->second >= onThreshold.count())) { @@ -461,6 +460,7 @@ bool CloudServiceImpl::DoSubscribe(const Subscription &sub) SchemaMeta schemaMeta; exits = MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(app.bundleName), schemaMeta, true); if (exits) { + auto &dbs = enabled ? subDbs : unsubDbs; dbs[app.bundleName] = std::move(schemaMeta.databases); } } -- Gitee From 7720440fe9e92f578d8ba6f8220f331099c1ba13 Mon Sep 17 00:00:00 2001 From: niudongyao Date: Tue, 23 May 2023 17:13:38 +0800 Subject: [PATCH 92/95] fix warn Signed-off-by: niudongyao --- .../distributeddataservice/service/data_share/common/context.h | 2 +- .../service/data_share/common/rdb_delegate.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/context.h b/services/distributeddataservice/service/data_share/common/context.h index d506e4b1..062319c3 100644 --- a/services/distributeddataservice/service/data_share/common/context.h +++ b/services/distributeddataservice/service/data_share/common/context.h @@ -38,7 +38,7 @@ public: std::string uri; int32_t currentUserId = -1; std::string permission; - uint32_t callerTokenId = -1; + uint32_t callerTokenId = 0; std::string callerBundleName; std::string calledBundleName; std::string calledModuleName; diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 3965a58f..7a331e2a 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -35,7 +35,7 @@ enum REMIND_TIMER_ARGS : int32_t { }; std::string RemindTimerFunc(const std::vector &args) { - int size = args.size(); + size_t size = args.size(); if (size != ARGS_SIZE) { ZLOGE("RemindTimerFunc args size error, %{public}d", size); return ""; -- Gitee From 420a7e1c20aefd6db3c526ad6d0fdc896467b635 Mon Sep 17 00:00:00 2001 From: niudongyao Date: Tue, 23 May 2023 17:16:33 +0800 Subject: [PATCH 93/95] fix warn Signed-off-by: niudongyao --- .../service/data_share/common/rdb_delegate.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 7a331e2a..8dcd025d 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -37,7 +37,7 @@ std::string RemindTimerFunc(const std::vector &args) { size_t size = args.size(); if (size != ARGS_SIZE) { - ZLOGE("RemindTimerFunc args size error, %{public}d", size); + ZLOGE("RemindTimerFunc args size error, %{public}zu", size); return ""; } std::string dbPath = args[ARG_DB_PATH]; -- Gitee From b66cc3a2ccc99aa40e0108623541aed775a4d71a Mon Sep 17 00:00:00 2001 From: niudongyao Date: Tue, 23 May 2023 17:21:21 +0800 Subject: [PATCH 94/95] fix warn Signed-off-by: niudongyao --- .../strategies/general/load_config_data_info_strategy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp index 0aab4f83..4ad67991 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp @@ -64,7 +64,7 @@ bool LoadConfigNormalDataInfoStrategy::operator()(std::shared_ptr conte } } context->calledSourceDir = metaData.dataDir; - context->version = metaData.version; + context->version = static_cast(metaData.version); return true; } -- Gitee From ef23c76641ebadbf916a5a31db1d27c7b4027d23 Mon Sep 17 00:00:00 2001 From: e Date: Tue, 23 May 2023 20:01:11 +0800 Subject: [PATCH 95/95] change5 Signed-off-by: e --- .../distributeddataservice/service/rdb/rdb_service_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 42ff8350..88f79bc6 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -438,7 +438,7 @@ int32_t RdbServiceImpl::DoSubscribe(const RdbSyncerParam& param, const Subscribe void RdbServiceImpl::OnChange(uint32_t tokenId, const std::string &storeName) { pid_t pid = 0; - syncAgents_.ComputeIfPresent(tokenId, [&pid, &storeName](auto &key, const SyncAgent &value) { + syncAgents_.ComputeIfPresent(tokenId, [&pid, &storeName](auto &key, SyncAgent &value) { pid = value.pid_; return true; }); -- Gitee