From 88cf3609d6a673c9028633f34f6aacfbf88c2c59 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Thu, 27 Apr 2023 16:10:14 +0800 Subject: [PATCH] update Signed-off-by: htt1997 --- .../framework/cloud/cloud_event.cpp | 40 +------ .../framework/include/cloud/cloud_event.h | 26 ++--- .../framework/include/store/auto_cache.h | 4 +- .../framework/store/auto_cache.cpp | 18 +++ .../distributeddataservice/service/BUILD.gn | 5 +- .../service/cloud/cloud_service_impl.cpp | 33 ++++-- .../service/rdb/rdb_general_store.h | 2 +- .../service/rdb/rdb_service_impl.cpp | 88 +++++++++++--- .../service/rdb/rdb_service_impl.h | 6 +- .../service/rdb/rdb_service_stub.cpp | 13 ++- .../service/rdb/rdb_service_stub.h | 6 +- .../service/test/cloud_data_test.cpp | 110 ++++++++++++++++++ .../native/cloud_data/src/cloud_manager.cpp | 2 +- .../cloud_data/src/cloud_service_proxy.cpp | 1 - .../native/rdb/include/rdb_service_proxy.h | 2 +- .../native/rdb/include/rdb_types_util.h | 17 +-- .../native/rdb/src/rdb_service_proxy.cpp | 8 +- .../native/rdb/src/rdb_store_impl.cpp | 12 +- .../native/rdb/src/rdb_types_util.cpp | 30 ++--- 19 files changed, 286 insertions(+), 137 deletions(-) create mode 100644 datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp index 0212fcc7..11f43a39 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp @@ -16,15 +16,8 @@ #include "cloud/cloud_event.h" namespace OHOS::DistributedData { -CloudEvent::CloudEvent(int32_t evtId, uint32_t tokenId, const std::string &storeName, const std::string &bundleName, - const std::string &featureName, const std::string &schemaKey) - : Event(evtId), featureName_(featureName), tokenId_(tokenId), storeName_(storeName), bundleName_(bundleName), - schemaKey_(schemaKey) -{ -} -CloudEvent::CloudEvent(int32_t evtId, const CloudEvent &cloudEvent) - : Event(evtId), featureName_(cloudEvent.GetFeatureName()), tokenId_(cloudEvent.GetTokenId()), - storeName_(cloudEvent.GetStoreName()), bundleName_(cloudEvent.GetBundleName()) +CloudEvent::CloudEvent(int32_t evtId, CloudEvent::StoreInfo storeInfo, const std::string &featureName) + : Event(evtId), featureName_(featureName), storeInfo_(storeInfo) { } @@ -33,33 +26,8 @@ std::string CloudEvent::GetFeatureName() const return featureName_; } -void CloudEvent::SetSchemaKey(std::string schemaKey) -{ - schemaKey_ = schemaKey; -} - -std::string CloudEvent::GetSchemaKey() const -{ - return schemaKey_; -} - -std::string CloudEvent::GetBundleName() const -{ - return bundleName_; -} - -std::string CloudEvent::GetStoreName() const -{ - return storeName_; -} - -bool CloudEvent::Equals(const Event &event) const -{ - return false; -} - -uint32_t CloudEvent::GetTokenId() const +CloudEvent::StoreInfo CloudEvent::GetStoreInfo() const { - return tokenId_; + return storeInfo_; } } \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h index 469e50fc..d01ab0f2 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -29,26 +29,22 @@ public: CLOUD_BUTT }; - CloudEvent(int32_t evtId, uint32_t tokenId = 0, const std::string &storeName = "", - const std::string &bundleName = "", const std::string &featureName = "relational_store", - const std::string &schemaKey = ""); - CloudEvent(int32_t evtId, const CloudEvent &cloudEvent); + 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; - void SetSchemaKey(std::string bundleName); - std::string GetSchemaKey() const; - std::string GetBundleName() const; - std::string GetStoreName() const; - uint32_t GetTokenId() const; - bool Equals(const DistributedData::Event &event) const override; + StoreInfo GetStoreInfo() const; private: std::string featureName_; - uint32_t tokenId_; - std::string storeName_; - std::string bundleName_; - std::string schemaKey_; - + StoreInfo storeInfo_; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_EVENT_H diff --git a/datamgr_service/services/distributeddataservice/framework/include/store/auto_cache.h b/datamgr_service/services/distributeddataservice/framework/include/store/auto_cache.h index ffed302f..9f0d6340 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/store/auto_cache.h +++ b/datamgr_service/services/distributeddataservice/framework/include/store/auto_cache.h @@ -44,7 +44,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/datamgr_service/services/distributeddataservice/framework/store/auto_cache.cpp b/datamgr_service/services/distributeddataservice/framework/store/auto_cache.cpp index 212f6c1b..76ec3966 100644 --- a/datamgr_service/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/datamgr_service/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/datamgr_service/services/distributeddataservice/service/BUILD.gn b/datamgr_service/services/distributeddataservice/service/BUILD.gn index 17f18018..ee18efff 100644 --- a/datamgr_service/services/distributeddataservice/service/BUILD.gn +++ b/datamgr_service/services/distributeddataservice/service/BUILD.gn @@ -52,9 +52,8 @@ 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/relational_store/interfaces/inner_api/cloud_data/include", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/include/distributeddb", ] sources = [ "backup/src/backup_manager.cpp", @@ -135,8 +134,8 @@ ohos_shared_library("distributeddatasvc") { "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", - "relational_store:native_rdb", "relational_store:cloud_data", + "relational_store:native_rdb", "relational_store:rdb_data_share_adapter", "resource_management:global_resmgr", "samgr:samgr_proxy", diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 88b532a5..2f03d93f 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -22,15 +22,19 @@ #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() { @@ -49,7 +53,7 @@ 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.GetTokenId()); + cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); if (GetServerInfo(cloudInfo) != SUCCESS) { ZLOGE("failed, user:%{public}d", cloudInfo.user); return; @@ -61,7 +65,7 @@ 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.GetTokenId()); + cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); if (GetServerInfo(cloudInfo) != SUCCESS) { ZLOGE("failed, user:%{public}d", cloudInfo.user); return; @@ -71,20 +75,27 @@ CloudServiceImpl::CloudServiceImpl() return; } SchemaMeta schemaMeta; - std::string schemaKey = cloudInfo.GetSchemaKey(rdbEvent.GetBundleName()); - if (!MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true)) { - schemaMeta = instance->GetAppSchema(cloudInfo.user, rdbEvent.GetBundleName()); + 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); - auto finishedEvent = std::make_unique(CloudEvent::NEED_CREATE, rdbEvent); - finishedEvent->SetSchemaKey(schemaKey); - EventCenter::GetInstance().PostEvent(move(finishedEvent)); } - + 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.GetStoreName() /* ||TODO:不需要同步*/) { + if (database.name != rdbEvent.GetStoreInfo().storeName /* ||TODO:不需要同步*/) { continue; } - auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetTokenId(), database); + auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, database); //TODO:同步 } }); diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.h index dc7aa0d6..d93391b2 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/datamgr_service/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 RdbGeneralStore : public DistributedData::GeneralStore { +class API_EXPORT RdbGeneralStore : public DistributedData::GeneralStore { public: using Cursor = DistributedData::Cursor; using GenQuery = DistributedData::GenQuery; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 8f98b6d4..d06af712 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -20,6 +20,7 @@ #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" @@ -30,6 +31,8 @@ #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; @@ -38,6 +41,7 @@ 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; @@ -86,16 +90,6 @@ RdbServiceImpl::RdbServiceImpl() [this](const std::string& identifier, DistributedDB::AutoLaunchParam ¶m) { return ResolveAutoLaunch(identifier, param); }); - - EventCenter::GetInstance().Subscribe(CloudEvent::NEED_CREATE, [this](const Event &event) { - auto &cloudEvent = static_cast(event); - if (cloudEvent.GetFeatureName() != "relation_store") { - return; - } - DistributedData::SchemaMeta schemaMeta; - MetaDataManager::GetInstance().LoadMeta(cloudEvent.GetSchemaKey(), schemaMeta); - //CreateDatabase(schemaMeta)TODO:根据schema创建表和trigger - }); } int32_t RdbServiceImpl::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) @@ -470,22 +464,82 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) int32_t RdbServiceImpl::OnInitialize() { - auto tokenId = IPCSkeleton::GetCallingTokenID(); - auto initEvt = std::make_unique(CloudEvent::FEATURE_INIT, tokenId); + 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 std::string &bundleName, const std::string &storeName) +int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) { - if (!CheckAccess(bundleName, storeName)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } - auto event = std::make_unique(CloudEvent::GET_SCHEMA, IPCSkeleton::GetCallingTokenID(), - storeName, bundleName); - EventCenter::GetInstance().PostEvent(std::move(event)); + + 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), SERVICE_NAME); + 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/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h index b2e32eb2..ac5395ad 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -58,7 +58,7 @@ public: int32_t OnInitialize() override; - int32_t GetSchema(const std::string &bundleName, const std::string &storeName) override; + int32_t GetSchema(const RdbSyncerParam ¶m) override; protected: int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option, @@ -82,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/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index 85e3719e..ba5aeb21 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -40,14 +40,15 @@ int32_t RdbServiceStub::OnRemoteObtainDistributedTableName(MessageParcel &data, return RDB_OK; } -int32_t RdbServiceStub::OnGetSchema(MessageParcel &data, MessageParcel &reply) { - std::string bundleName; - std::string storeName; - if (!ITypesUtil::Unmarshal(data, bundleName, storeName)) { - ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", bundleName.c_str(), storeName.c_str()); +int32_t RdbServiceStub::OnGetSchema(MessageParcel &data, MessageParcel &reply) +{ + RdbSyncerParam param; + 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(bundleName, storeName); + auto status = GetSchema(param); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x", status); return IPC_STUB_WRITE_PARCEL_ERR; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h index 2e1af17a..16f4c169 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h @@ -50,11 +50,11 @@ private: int32_t OnRemoteObtainDistributedTableName(MessageParcel& data, MessageParcel& reply); - int32_t OnGetSchema(MessageParcel&data, MessageParcel& reply); + int32_t OnGetSchema(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteInitNotifier(MessageParcel&data, MessageParcel& reply); + int32_t OnRemoteInitNotifier(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply); + int32_t OnRemoteSetDistributedTables(MessageParcel& data, MessageParcel& reply); int32_t OnRemoteDoSync(MessageParcel& data, MessageParcel& reply); diff --git a/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp b/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp new file mode 100644 index 00000000..f7aa6c5e --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -0,0 +1,110 @@ +/* +* 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; +}; + +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 = 1000; + cloudInfo.totalSpace = 2000; + 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)); +} + diff --git a/relational_store/frameworks/native/cloud_data/src/cloud_manager.cpp b/relational_store/frameworks/native/cloud_data/src/cloud_manager.cpp index 78823f7f..5f0e290d 100644 --- a/relational_store/frameworks/native/cloud_data/src/cloud_manager.cpp +++ b/relational_store/frameworks/native/cloud_data/src/cloud_manager.cpp @@ -33,7 +33,7 @@ public: class CloudDeath : public IRemoteObject::DeathRecipient { public: - CloudDeath(std::function action) : action_(std::move(action)){}; + explicit CloudDeath(std::function action) : action_(std::move(action)){}; void OnRemoteDied(const wptr &object) override { if (action_) { diff --git a/relational_store/frameworks/native/cloud_data/src/cloud_service_proxy.cpp b/relational_store/frameworks/native/cloud_data/src/cloud_service_proxy.cpp index fd295595..a379d059 100644 --- a/relational_store/frameworks/native/cloud_data/src/cloud_service_proxy.cpp +++ b/relational_store/frameworks/native/cloud_data/src/cloud_service_proxy.cpp @@ -90,7 +90,6 @@ int32_t CloudServiceProxy::Clean(const std::string &id, const std::map(status); - } int32_t CloudServiceProxy::NotifyDataChange(const std::string &id, const std::string &bundleName) diff --git a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h index c7387314..17f819df 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h +++ b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h @@ -57,7 +57,7 @@ public: void ImportObservers(ObserverMap& observers); /*CLoudData*/ - int32_t GetSchema(const std::string &bundleName, const std::string &storeName) override; + int32_t GetSchema(const RdbSyncerParam ¶m) override; protected: int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option, const RdbPredicates& predicates, SyncResult& result) override; diff --git a/relational_store/frameworks/native/rdb/include/rdb_types_util.h b/relational_store/frameworks/native/rdb/include/rdb_types_util.h index ca4465ae..5acb5c25 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_types_util.h +++ b/relational_store/frameworks/native/rdb/include/rdb_types_util.h @@ -22,7 +22,6 @@ #include "rdb_visibility.h" namespace OHOS::ITypesUtil { using SyncerParam = DistributedRdb::RdbSyncerParam; -using CloudParam = DistributedRdb::CloudParam; using SyncOption = DistributedRdb::SyncOption; using RdbPredicates = DistributedRdb::RdbPredicates; using RdbOperation = DistributedRdb::RdbPredicateOperation; @@ -34,10 +33,6 @@ RDB_API_EXPORT bool Marshalling(const SyncerParam &input, MessageParcel &data); template<> RDB_API_EXPORT bool Unmarshalling(SyncerParam &output, MessageParcel &data); template<> -RDB_API_EXPORT bool Marshalling(const CloudParam &input, MessageParcel &data); -template<> -RDB_API_EXPORT bool Unmarshalling(CloudParam &output, MessageParcel &data); -template<> RDB_API_EXPORT bool Marshalling(const SyncOption &input, MessageParcel &data); template<> RDB_API_EXPORT bool Unmarshalling(SyncOption &output, MessageParcel &data); @@ -50,16 +45,16 @@ RDB_API_EXPORT bool Marshalling(const RdbOperation &input, MessageParcel &data); template<> RDB_API_EXPORT bool Unmarshalling(RdbOperation &output, MessageParcel &data); template<> -bool Marshalling(const ValueObject &input, MessageParcel &data); +RDB_API_EXPORT bool Marshalling(const ValueObject &input, MessageParcel &data); template<> -bool Unmarshalling(ValueObject &output, MessageParcel &data); +RDB_API_EXPORT bool Unmarshalling(ValueObject &output, MessageParcel &data); template<> -bool Marshalling(const ValuesBucket &input, MessageParcel &data); +RDB_API_EXPORT bool Marshalling(const ValuesBucket &input, MessageParcel &data); template<> -bool Unmarshalling(ValuesBucket &output, MessageParcel &data); +RDB_API_EXPORT bool Unmarshalling(ValuesBucket &output, MessageParcel &data); template<> -bool Marshalling(const Asset &input, MessageParcel &data); +RDB_API_EXPORT bool Marshalling(const Asset &input, MessageParcel &data); template<> -bool Unmarshalling(Asset &output, MessageParcel &data); +RDB_API_EXPORT bool Unmarshalling(Asset &output, MessageParcel &data); } #endif // DISTRIBUTED_RDB_RDB_TYPES_UTIL_H diff --git a/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp b/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp index 42bfe9a9..757ec5f0 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp @@ -339,13 +339,13 @@ int32_t RdbServiceProxy::DestroyRDBTable(const RdbSyncerParam ¶m) return status; } -int32_t RdbServiceProxy::GetSchema(const std::string &bundleName, const std::string &storeName) +int32_t RdbServiceProxy::GetSchema(const RdbSyncerParam ¶m) { MessageParcel reply; - int32_t status = IPC_SEND(RDB_SERVICE_CMD_GET_SCHEMA, reply, bundleName, storeName); + int32_t status = IPC_SEND(RDB_SERVICE_CMD_GET_SCHEMA, reply, param); if (status != RDB_OK) { - ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", status, bundleName.c_str(), - storeName.c_str()); + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", status, param.bundleName_.c_str(), + param.storeName_.c_str()); } return status; } diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp index 5c09002f..beb7f3f7 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -95,11 +95,12 @@ int RdbStoreImpl::InnerOpen(const RdbStoreConfig &config) LOG_ERROR("RdbStoreImpl::InnerOpen get service failed, err is %{public}d.", errCode); return E_OK; } - - errCode = service->GetSchema(config.GetBundleName(), config.GetName()); - if (errCode != E_OK) { - LOG_ERROR("RdbStoreImpl::InnerOpen GetSchema failed, err is %{public}d.", errCode); - return E_OK; + if (syncerParam_.schemaVersion != -1) { + errCode = service->GetSchema(syncerParam_); + if (errCode != E_OK) { + LOG_ERROR("RdbStoreImpl::InnerOpen GetSchema failed, err is %{public}d.", errCode); + return E_OK; + } } // open uri share @@ -111,7 +112,6 @@ int RdbStoreImpl::InnerOpen(const RdbStoreConfig &config) } isShared_ = true; } - #endif return E_OK; } diff --git a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp index bc423e6f..f0190b62 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp @@ -19,22 +19,13 @@ template<> bool Marshalling(const SyncerParam &input, MessageParcel &data) { return ITypesUtil::Marshal(data, input.bundleName_, input.hapName_, input.storeName_, input.area_, - input.level_, input.type_, input.isAutoSync_, input.isEncrypt_, input.password_); + input.level_, input.type_, input.isAutoSync_, input.isEncrypt_, input.password_, input.schemaVersion); } template<> bool Unmarshalling(SyncerParam &output, MessageParcel &data) { return ITypesUtil::Unmarshal(data, output.bundleName_, output.hapName_, output.storeName_, output.area_, - output.level_, output.type_, output.isAutoSync_, output.isEncrypt_, output.password_); -} - -template<> bool Marshalling(const CloudParam &input, MessageParcel &data) -{ - return ITypesUtil::Marshal(data, input.bundleName, input.storeName); -} -template<> bool Unmarshalling(CloudParam &output, MessageParcel &data) -{ - return ITypesUtil::Unmarshal(data, output.bundleName, output.storeName); + output.level_, output.type_, output.isAutoSync_, output.isEncrypt_, output.password_, output.schemaVersion); } template<> @@ -42,7 +33,6 @@ bool Marshalling(const SyncOption &input, MessageParcel &data) { return ITypesUtil::Marshal(data, static_cast(input.mode), input.isBlock); } - template<> bool Unmarshalling(SyncOption &output, MessageParcel &data) { @@ -68,7 +58,6 @@ bool Marshalling(const RdbOperation &input, MessageParcel &data) { return ITypesUtil::Marshal(data, static_cast(input.operator_), input.field_, input.values_); } - template<> bool Unmarshalling(RdbOperation &output, MessageParcel &data) { @@ -78,25 +67,28 @@ bool Unmarshalling(RdbOperation &output, MessageParcel &data) return ret; } -template<> bool ITypesUtil::Marshalling(const ValueObject &input, MessageParcel &data) +template<> +bool ITypesUtil::Marshalling(const ValueObject &input, MessageParcel &data) { return ITypesUtil::Marshal(data, input.value); } - -template<> bool ITypesUtil::Unmarshalling(ValueObject &output, MessageParcel &data) +template<> +bool ITypesUtil::Unmarshalling(ValueObject &output, MessageParcel &data) { return ITypesUtil::Unmarshal(data, output.value); } -template<> bool ITypesUtil::Marshalling(const ValuesBucket &input, MessageParcel &data) +template<> +bool ITypesUtil::Marshalling(const ValuesBucket &input, MessageParcel &data) { return ITypesUtil::Marshal(data, input.values_); } - -template<> bool ITypesUtil::Unmarshalling(ValuesBucket &output, MessageParcel &data) +template<> +bool ITypesUtil::Unmarshalling(ValuesBucket &output, MessageParcel &data) { return ITypesUtil::Unmarshal(data, output.values_); } + template<> bool ITypesUtil::Marshalling(const Asset &input, MessageParcel &data) { -- Gitee