From 9c5b515100050f1839cd6fb60a21660911156d21 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 17 May 2023 16:06:51 +0800 Subject: [PATCH 01/14] refactor:Decoupling syncer and meta Signed-off-by: htt1997 --- .../service/rdb/rdb_service_impl.cpp | 103 ++++++++++++------ .../service/rdb/rdb_service_impl.h | 13 ++- .../service/rdb/rdb_service_stub.cpp | 12 +- .../service/rdb/rdb_syncer.cpp | 83 +------------- .../service/rdb/rdb_syncer.h | 7 +- .../service/test/cloud_data_test.cpp | 58 +++++----- 6 files changed, 116 insertions(+), 160 deletions(-) diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 525aed434..f7a9b2c79 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -24,6 +24,7 @@ #include "eventcenter/event_center.h" #include "ipc_skeleton.h" #include "log_print.h" +#include "metadata/appid_meta_data.h" #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" #include "permission/permission_validator.h" @@ -45,14 +46,18 @@ using namespace OHOS::DistributedData; using namespace OHOS::Security::AccessToken; using DistributedDB::RelationalStoreManager; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; +using system_clock = std::chrono::system_clock; constexpr uint32_t ITERATE_TIMES = 10000; namespace OHOS::DistributedRdb { __attribute__((used)) RdbServiceImpl::Factory RdbServiceImpl::factory_; RdbServiceImpl::Factory::Factory() { - FeatureSystem::GetInstance().RegisterCreator("relational_store", []() { - return std::make_shared(); + FeatureSystem::GetInstance().RegisterCreator(RdbServiceImpl::SERVICE_NAME, [this]() { + if (product_ == nullptr) { + product_ = std::make_shared(); + } + return product_; }); AutoCache::GetInstance().RegCreator(RDB_DEVICE_COLLABORATION, [](const StoreMetaData &metaData) -> GeneralStore* { return new (std::nothrow) RdbGeneralStore(metaData); @@ -284,12 +289,8 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa } syncers.erase(storeId); } - if (syncers.size() >= MAX_SYNCER_PER_PROCESS) { - ZLOGE("%{public}d exceed MAX_PROCESS_SYNCER_NUM", pid); - return !syncers.empty(); - } - if (syncerNum_ >= MAX_SYNCER_NUM) { - ZLOGE("no available syncer"); + if (syncers.size() >= MAX_SYNCER_PER_PROCESS || syncerNum_ >= MAX_SYNCER_NUM) { + ZLOGE("pid: %{public}d, syncers size: %{public}zu. syncerNum: %{public}d", pid, syncers.size(), syncerNum_); return !syncers.empty(); } auto rdbObserver = new (std::nothrow) RdbStoreObserverImpl(this, pid); @@ -297,7 +298,9 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa return !syncers.empty(); } auto syncer_ = std::make_shared(param, rdbObserver); - if (syncer_->Init(pid, uid, tokenId) != 0) { + StoreMetaData storeMetaData = GetStoreMetaData(param); + MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData); + if (syncer_->Init(pid, uid, tokenId, storeMetaData) != 0) { return !syncers.empty(); } syncers[storeId] = syncer_; @@ -492,8 +495,7 @@ int32_t RdbServiceImpl::RemoteQuery(const RdbSyncerParam& param, const std::stri return syncer->RemoteQuery(device, sql, selectionArgs, resultSet); } -int32_t RdbServiceImpl::CreateRDBTable( - const RdbSyncerParam ¶m, const std::string &writePermission, const std::string &readPermission) +int32_t RdbServiceImpl::CreateRDBTable(const RdbSyncerParam ¶m) { if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); @@ -512,7 +514,9 @@ int32_t RdbServiceImpl::CreateRDBTable( } auto uid = IPCSkeleton::GetCallingUid(); auto tokenId = IPCSkeleton::GetCallingTokenID(); - if (syncer->Init(pid, uid, tokenId, writePermission, readPermission) != RDB_OK) { + StoreMetaData storeMetaData = GetStoreMetaData(param); + MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData); + if (syncer->Init(pid, uid, tokenId, storeMetaData) != RDB_OK) { ZLOGE("Init error"); delete syncer; return RDB_ERROR; @@ -527,25 +531,8 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) ZLOGE("permission error"); return RDB_ERROR; } - pid_t pid = IPCSkeleton::GetCallingPid(); - auto rdbObserver = new (std::nothrow) RdbStoreObserverImpl(this, pid); - if (rdbObserver == nullptr) { - return RDB_ERROR; - } - auto syncer = new (std::nothrow) RdbSyncer(param, rdbObserver); - if (syncer == nullptr) { - ZLOGE("new syncer error"); - return RDB_ERROR; - } - - StoreMetaData meta; - if (syncer->DestroyMetaData(meta) != RDB_OK) { - ZLOGE("destroy meta data error"); - delete syncer; - return RDB_ERROR; - } - delete syncer; - return RDB_OK; + auto meta = GetStoreMetaData(param); + return MetaDataManager::GetInstance().DelMeta(meta.GetKey()) ? RDB_OK : RDB_ERROR; } int32_t RdbServiceImpl::OnInitialize() @@ -559,10 +546,12 @@ int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) ZLOGE("permission error"); return RDB_ERROR; } - auto syncer = GetRdbSyncer(param); - if (syncer == nullptr) { + StoreMetaData storeMeta; + if (CreateMetaData(param, storeMeta) != RDB_OK) { return RDB_ERROR; } + + EventCenter::Defer defer; CloudEvent::StoreInfo storeInfo { IPCSkeleton::GetCallingTokenID(), param.bundleName_, RdbSyncer::RemoveSuffix(param.storeName_), RdbSyncer::GetInstIndex(IPCSkeleton::GetCallingTokenID(), param.bundleName_) }; @@ -593,9 +582,53 @@ StoreMetaData RdbServiceImpl::GetStoreMetaData(const RdbSyncerParam ¶m) return metaData; } +int32_t RdbServiceImpl::CreateMetaData(const RdbSyncerParam ¶m, StoreMetaData &old) +{ + auto meta = GetStoreMetaData(param); + bool isCreated = MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), old); + if (isCreated && (old.storeType != meta.storeType || Constant::NotEqual(old.isEncrypt, meta.isEncrypt) || + old.area != meta.area)) { + ZLOGE("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " + "area:%{public}d->%{public}d", + meta.bundleName.c_str(), meta.storeId.c_str(), old.storeType, meta.storeType, old.isEncrypt, + meta.isEncrypt, old.area, meta.area); + return RDB_ERROR; + } + + auto saved = MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta); + if (!saved) { + return RDB_ERROR; + } + AppIDMetaData appIdMeta; + appIdMeta.bundleName = meta.bundleName; + appIdMeta.appId = meta.appId; + saved = MetaDataManager::GetInstance().SaveMeta(appIdMeta.GetKey(), appIdMeta, true); + if (!saved) { + return RDB_ERROR; + } + if (!param.isEncrypt_ || param.password_.empty()) { + return RDB_OK; + } + return SetSecretKey(param, meta); +} + +bool RdbServiceImpl::SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta) +{ + SecretKeyMetaData newSecretKey; + newSecretKey.storeType = meta.storeType; + newSecretKey.sKey = CryptoManager::GetInstance().Encrypt(param.password_); + if (newSecretKey.sKey.empty()) { + ZLOGE("encrypt work key error."); + return RDB_ERROR; + } + auto time = system_clock::to_time_t(system_clock::now()); + newSecretKey.time = { reinterpret_cast(&time), reinterpret_cast(&time) + sizeof(time) }; + return MetaDataManager::GetInstance().SaveMeta(meta.GetSecretKey(), newSecretKey, true) ? RDB_OK : RDB_ERROR; +} + int32_t RdbServiceImpl::OnExecutor(std::shared_ptr executors) { - executors_ = executors; - return 0; + executors_ = std::move(executors); + return RDB_OK; } } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index af2d788d6..c9f3eb546 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -52,16 +52,17 @@ public: 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 CreateRDBTable(const RdbSyncerParam ¶m) override; + int32_t DestroyRDBTable(const RdbSyncerParam ¶m) override; int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; + int32_t OnExecutor(std::shared_ptr executors) override; + int32_t OnInitialize() override; int32_t GetSchema(const RdbSyncerParam ¶m) override; - int32_t OnExecutor(std::shared_ptr executors) override; protected: int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option, @@ -96,7 +97,9 @@ private: void OnAsyncComplete(pid_t pid, uint32_t seqNum, const SyncResult& result); - StoreMetaData GetStoreMetaData(const RdbSyncerParam& param); + int32_t CreateMetaData(const RdbSyncerParam ¶m, StoreMetaData &old); + StoreMetaData GetStoreMetaData(const RdbSyncerParam ¶m); + bool SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta); class DeathRecipientImpl : public IRemoteObject::DeathRecipient { public: @@ -111,6 +114,8 @@ private: public: Factory(); ~Factory(); + private: + std::shared_ptr product_; }; using StoreSyncersType = std::map>; diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index 52ae78517..e879c5234 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -215,17 +215,13 @@ int RdbServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageP int32_t RdbServiceStub::OnRemoteDoCreateTable(MessageParcel &data, MessageParcel &reply) { RdbSyncerParam param; - std::string writePermission; - std::string readPermission; - if (!ITypesUtil::Unmarshal(data, param, writePermission, readPermission)) { - ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s writePermission:%{public}s " - "readPermission:%{public}s", param.bundleName_.c_str(), param.storeName_.c_str(), - DistributedData::Anonymous::Change(writePermission).c_str(), - DistributedData::Anonymous::Change(readPermission).c_str()); + 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; } - int32_t status = CreateRDBTable(param, writePermission, readPermission); + int32_t status = CreateRDBTable(param); 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.cpp b/services/distributeddataservice/service/rdb/rdb_syncer.cpp index f8f6192a1..1d1d1c35e 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -102,26 +102,19 @@ std::string RdbSyncer::GetStoreId() const return RemoveSuffix(param_.storeName_); } -int32_t RdbSyncer::Init( - pid_t pid, pid_t uid, uint32_t token, const std::string &writePermission, const std::string &readPermission) +int32_t RdbSyncer::Init(pid_t pid, pid_t uid, uint32_t token, const StoreMetaData &meta) { ZLOGI("enter"); pid_ = pid; uid_ = uid; token_ = token; - StoreMetaData oldMeta; - StoreMetaData meta; - if (CreateMetaData(meta, oldMeta) != RDB_OK) { - ZLOGE("create meta data failed"); - return RDB_ERROR; - } if (InitDBDelegate(meta) != RDB_OK) { ZLOGE("delegate is nullptr"); return RDB_ERROR; } - if (oldMeta.storeType == RDB_DEVICE_COLLABORATION && oldMeta.version < StoreMetaData::UUID_CHANGED_TAG) { + if (meta.storeType == RDB_DEVICE_COLLABORATION && meta.version < StoreMetaData::UUID_CHANGED_TAG) { delegate_->RemoveDeviceData(); } @@ -129,78 +122,6 @@ int32_t RdbSyncer::Init( return RDB_OK; } -int32_t RdbSyncer::DestroyMetaData(StoreMetaData &meta) -{ - FillMetaData(meta); - auto deleted = MetaDataManager::GetInstance().DelMeta(meta.GetKey(), true); - return deleted ? RDB_OK : RDB_ERROR; -} - -void RdbSyncer::FillMetaData(StoreMetaData &meta) -{ - meta.uid = uid_; - meta.tokenId = token_; - meta.instanceId = GetInstIndex(token_, param_.bundleName_); - meta.bundleName = param_.bundleName_; - meta.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; - meta.storeId = RemoveSuffix(param_.storeName_); - meta.user = std::to_string(AccountDelegate::GetInstance()->GetUserByToken(token_)); - meta.storeType = param_.type_; - meta.securityLevel = param_.level_; - meta.area = param_.area_; - meta.appId = CheckerManager::GetInstance().GetAppId(Converter::ConvertToStoreInfo(meta)); - meta.appType = "harmony"; - meta.hapName = param_.hapName_; - meta.dataDir = DirectoryManager::GetInstance().GetStorePath(meta) + "/" + param_.storeName_; - meta.account = AccountDelegate::GetInstance()->GetCurrentAccountId(); - meta.isEncrypt = param_.isEncrypt_; -} - -int32_t RdbSyncer::CreateMetaData(StoreMetaData &meta, StoreMetaData &old) -{ - FillMetaData(meta); - bool isCreated = MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), old); - if (isCreated && (old.storeType != meta.storeType || Constant::NotEqual(old.isEncrypt, meta.isEncrypt) || - old.area != meta.area)) { - ZLOGE("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " - "area:%{public}d->%{public}d", - meta.bundleName.c_str(), meta.storeId.c_str(), old.storeType, meta.storeType, old.isEncrypt, - meta.isEncrypt, old.area, meta.area); - return RDB_ERROR; - } - - auto saved = MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta); - if (!saved) { - return RDB_ERROR; - } - AppIDMetaData appIdMeta; - appIdMeta.bundleName = meta.bundleName; - appIdMeta.appId = meta.appId; - saved = MetaDataManager::GetInstance().SaveMeta(appIdMeta.GetKey(), appIdMeta, true); - if (!saved) { - return RDB_ERROR; - } - if (!param_.isEncrypt_ || param_.password_.empty()) { - return RDB_OK; - } - return SetSecretKey(meta); -} - -bool RdbSyncer::SetSecretKey(const StoreMetaData &meta) -{ - SecretKeyMetaData newSecretKey; - newSecretKey.storeType = meta.storeType; - newSecretKey.sKey = CryptoManager::GetInstance().Encrypt(param_.password_); - if (newSecretKey.sKey.empty()) { - ZLOGE("encrypt work key error."); - return RDB_ERROR; - } - param_.password_.assign(param_.password_.size(), 0); - auto time = system_clock::to_time_t(system_clock::now()); - newSecretKey.time = { reinterpret_cast(&time), reinterpret_cast(&time) + sizeof(time) }; - return MetaDataManager::GetInstance().SaveMeta(meta.GetSecretKey(), newSecretKey, true) ? RDB_OK : RDB_ERROR; -} - bool RdbSyncer::GetPassword(const StoreMetaData &metaData, DistributedDB::CipherPassword &password) { if (!metaData.isEncrypt) { diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.h b/services/distributeddataservice/service/rdb/rdb_syncer.h index f5f238851..68d1e44fb 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -34,8 +34,7 @@ public: RdbSyncer(const RdbSyncerParam& param, RdbStoreObserverImpl* observer); ~RdbSyncer() noexcept; - int32_t Init(pid_t pid, pid_t uid, uint32_t token, const std::string &writePermission = "", - const std::string &readPermission = ""); + int32_t Init(pid_t pid, pid_t uid, uint32_t token, const StoreMetaData &meta); pid_t GetPid() const; @@ -56,7 +55,6 @@ public: int32_t RemoteQuery(const std::string& device, const std::string& sql, const std::vector& selectionArgs, sptr& resultSet); - int32_t DestroyMetaData(StoreMetaData &meta); static std::string RemoveSuffix(const std::string& name); static int32_t GetInstIndex(uint32_t tokenId, const std::string &bundleName); @@ -72,10 +70,7 @@ private: std::string GetAppId() const; - int32_t CreateMetaData(StoreMetaData &meta, StoreMetaData &old); - void FillMetaData(StoreMetaData &meta); int32_t InitDBDelegate(const StoreMetaData &meta); - bool SetSecretKey(const StoreMetaData &meta); DistributedDB::RelationalStoreDelegate* GetDelegate(); diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index 031ea345f..ddee440ef 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -44,11 +44,13 @@ public: void SetUp(); void TearDown(); + static SchemaMeta schemaMeta_; protected: static constexpr const char *TEST_DISTRIBUTEDDATA_BUNDLE = "test_distributeddata"; static constexpr const char *TEST_DISTRIBUTEDDATA_STORE = "test_service_meta"; void InitMetaData(); + void InitSchemaMeta(); static std::shared_ptr dbStoreMock_; StoreMetaData metaData_; }; @@ -83,29 +85,7 @@ CloudInfo CloudServerMock::GetServerInfo(int32_t userId) 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_STORE; - database.alias = "test_cloud_database_alias"; - database.tables.emplace_back(table); - - SchemaMeta schemaMeta; - schemaMeta.version = 1; - schemaMeta.databases.emplace_back(database); - - return schemaMeta; + return CloudDataTest::schemaMeta_; } std::shared_ptr CloudDataTest::dbStoreMock_ = std::make_shared(); @@ -126,6 +106,30 @@ void CloudDataTest::InitMetaData() value.type = OHOS::DistributedKv::PolicyType::IMMEDIATE_SYNC_ON_ONLINE; } +void CloudDataTest::InitSchemaMeta() +{ + 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_STORE; + database.alias = "test_cloud_database_alias"; + database.tables.emplace_back(table); + + schemaMeta_.version = 1; + schemaMeta_.databases.emplace_back(database); +} + void CloudDataTest::SetUpTestCase(void) { MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, [](const auto &, auto) { @@ -157,6 +161,7 @@ void CloudDataTest::SetUp() storeMetaData.user = std::to_string(DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(storeMetaData.tokenId)); MetaDataManager::GetInstance().SaveMeta(storeMetaData.GetKey(), storeMetaData); + InitSchemaMeta(); } void CloudDataTest::TearDown() {} @@ -175,14 +180,15 @@ HWTEST_F(CloudDataTest, GetSchema, TestSize.Level0) auto cloudInfo = cloudServerMock->GetServerInfo( DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(OHOS::IPCSkeleton::GetCallingTokenID())); ASSERT_TRUE(MetaDataManager::GetInstance().DelMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), true)); - StoreMetaData storeMetaData; + SchemaMeta schemaMeta; ASSERT_FALSE( - MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), storeMetaData, true)); + MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), schemaMeta, true)); 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( - MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), storeMetaData, true)); + MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), schemaMeta, true)); + ASSERT_EQ(to_string(schemaMeta.Marshall()), to_string(schemaMeta_.Marshall())); } } // namespace DistributedDataTest } // namespace OHOS -- Gitee From 3bdbe182391e07a64447f81a8116d5e65ec8c85c Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 17 May 2023 18:01:13 +0800 Subject: [PATCH 02/14] refactor:optimize per and fix:test Signed-off-by: htt1997 --- .../framework/cloud/cloud_event.cpp | 2 +- .../framework/include/cloud/cloud_event.h | 2 +- .../framework/include/store/general_store.h | 4 +-- .../service/cloud/cloud_service_impl.cpp | 36 +++++++++++-------- .../service/rdb/rdb_general_store.cpp | 8 +---- .../service/rdb/rdb_general_store.h | 3 +- .../service/test/cloud_data_test.cpp | 1 + 7 files changed, 27 insertions(+), 29 deletions(-) diff --git a/services/distributeddataservice/framework/cloud/cloud_event.cpp b/services/distributeddataservice/framework/cloud/cloud_event.cpp index 11f43a392..cb2604bd0 100644 --- a/services/distributeddataservice/framework/cloud/cloud_event.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_event.cpp @@ -26,7 +26,7 @@ std::string CloudEvent::GetFeatureName() const return featureName_; } -CloudEvent::StoreInfo CloudEvent::GetStoreInfo() const +const CloudEvent::StoreInfo& CloudEvent::GetStoreInfo() const { return storeInfo_; } diff --git a/services/distributeddataservice/framework/include/cloud/cloud_event.h b/services/distributeddataservice/framework/include/cloud/cloud_event.h index 47c873c99..e1f04f41a 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -40,7 +40,7 @@ public: CloudEvent(int32_t evtId, StoreInfo storeInfo, const std::string &featureName = "relational_store"); ~CloudEvent() = default; std::string GetFeatureName() const; - StoreInfo GetStoreInfo() const; + const StoreInfo& GetStoreInfo() const; private: std::string featureName_; diff --git a/services/distributeddataservice/framework/include/store/general_store.h b/services/distributeddataservice/framework/include/store/general_store.h index 097f8d9a7..e31eb8899 100644 --- a/services/distributeddataservice/framework/include/store/general_store.h +++ b/services/distributeddataservice/framework/include/store/general_store.h @@ -32,9 +32,7 @@ public: virtual ~GeneralStore() = default; - virtual int32_t Bind(std::shared_ptr cloudDb) = 0; - - virtual int32_t SetSchema(const SchemaMeta &schemaMeta) = 0; + virtual int32_t Bind(const SchemaMeta &schemaMeta, std::shared_ptr cloudDb) = 0; virtual int32_t Execute(const std::string &table, const std::string &sql) = 0; diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 0163dfb8c..f8fc003c3 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -397,28 +397,34 @@ void CloudServiceImpl::GetSchema(const Event &event) } auto storeMeta = GetStoreMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, rdbEvent.GetStoreInfo().instanceId); + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + ZLOGE("instance is nullptr"); + return; + } + auto database = std::find_if(schemaMeta.databases.begin(),schemaMeta.databases.end(), + [&rdbEvent](const auto &database){ + return database.name == rdbEvent.GetStoreInfo().storeName; + }); + if (database == schemaMeta.databases.end()) { + return; + } + ZLOGD("database: %{public}s sync start", database->name.c_str()); + auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, *database); + if (cloudDB == nullptr) { + ZLOGE("cloudDB is nullptr"); + return; + } AutoCache::Watchers watchers; auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers); 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; - } - ZLOGD("database: %{public}s sync start", database.name.c_str()); - // ConnectCloudDB and Bind to store - for (auto &table : database.tables) { - ZLOGD("table: %{public}s sync start", table.name.c_str()); - } + store->Bind(schemaMeta, cloudDB); + for (const auto &table : database->tables) { + ZLOGD("table: %{public}s sync start", table.name.c_str()); // do sync } return; diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index 496878bcd..bd1f118c5 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -138,15 +138,9 @@ int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &qu return status == DistributedDB::OK ? GeneralError::E_OK : GeneralError::E_ERROR; } -int32_t RdbGeneralStore::Bind(std::shared_ptr cloudDb) +int32_t RdbGeneralStore::Bind(const SchemaMeta &schemaMeta, std::shared_ptr cloudDb) { cloudDb_ = std::move(cloudDb); return 0; } - -int32_t RdbGeneralStore::SetSchema(const SchemaMeta &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 f1bd92cdd..3d2fda304 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -39,8 +39,7 @@ public: using RdbManager = DistributedDB::RelationalStoreManager; explicit RdbGeneralStore(const StoreMetaData &metaData); - int32_t Bind(std::shared_ptr cloudDb) override; - int32_t SetSchema(const SchemaMeta &schemaMeta) override; + int32_t Bind(const SchemaMeta &schemaMeta, 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; diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index ddee440ef..781ed1582 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -19,6 +19,7 @@ #include "account/account_delegate.h" #include "cloud/cloud_event.h" #include "cloud/cloud_server.h" +#include "cloud/schema_meta.h" #include "communicator/device_manager_adapter.h" #include "device_matrix.h" #include "eventcenter/event_center.h" -- Gitee From f5ae3f6fc8ca2175552b1183e982f084566575c8 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Thu, 18 May 2023 10:03:41 +0800 Subject: [PATCH 03/14] fix:ut test Signed-off-by: htt1997 --- .../distributeddataservice/service/test/cloud_data_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index 781ed1582..32057e30b 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -90,6 +90,7 @@ SchemaMeta CloudServerMock::GetAppSchema(int32_t userId, const std::string &bund } std::shared_ptr CloudDataTest::dbStoreMock_ = std::make_shared(); +SchemaMeta CloudDataTest::schemaMeta_; void CloudDataTest::InitMetaData() { @@ -148,6 +149,7 @@ void CloudDataTest::TearDownTestCase() {} void CloudDataTest::SetUp() { InitMetaData(); + InitSchemaMeta(); MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_); StoreMetaData storeMetaData; @@ -162,7 +164,6 @@ void CloudDataTest::SetUp() storeMetaData.user = std::to_string(DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(storeMetaData.tokenId)); MetaDataManager::GetInstance().SaveMeta(storeMetaData.GetKey(), storeMetaData); - InitSchemaMeta(); } void CloudDataTest::TearDown() {} -- Gitee From c429575805d7941d1c27754162bcf3dbb18d9985 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Thu, 18 May 2023 10:07:02 +0800 Subject: [PATCH 04/14] style:code check Signed-off-by: htt1997 --- .../service/cloud/cloud_service_impl.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index f8fc003c3..cd3c8d160 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -402,10 +402,10 @@ void CloudServiceImpl::GetSchema(const Event &event) ZLOGE("instance is nullptr"); return; } - auto database = std::find_if(schemaMeta.databases.begin(),schemaMeta.databases.end(), - [&rdbEvent](const auto &database){ - return database.name == rdbEvent.GetStoreInfo().storeName; - }); + auto database = + std::find_if(schemaMeta.databases.begin(), schemaMeta.databases.end(), [&rdbEvent](const auto &database) { + return database.name == rdbEvent.GetStoreInfo().storeName; + }); if (database == schemaMeta.databases.end()) { return; } -- Gitee From f9e8a5693ebbf14e68283c503573166fe3fc4bee Mon Sep 17 00:00:00 2001 From: htt1997 Date: Thu, 18 May 2023 20:51:52 +0800 Subject: [PATCH 05/14] style:Review comments modification Signed-off-by: htt1997 --- .../service/cloud/cloud_service_impl.cpp | 10 ++++++---- .../service/cloud/cloud_service_impl.h | 4 ++-- .../service/rdb/rdb_service_impl.cpp | 4 ++-- .../service/rdb/rdb_service_impl.h | 4 +++- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index cd3c8d160..dc263d784 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -317,7 +317,7 @@ ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry, int32_t user) }; } -SchemaMeta CloudServiceImpl::GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId) +SchemaMeta CloudServiceImpl::GetSchemaMeta(int32_t userId, const std::string &bundleName, int32_t instanceId) { SchemaMeta schemaMeta; auto instance = CloudServer::GetInstance(); @@ -350,7 +350,7 @@ SchemaMeta CloudServiceImpl::GetSchemaMata(int32_t userId, const std::string &bu return schemaMeta; } -StoreMetaData CloudServiceImpl::GetStoreMata(int32_t userId, const std::string &bundleName, +StoreMetaData CloudServiceImpl::GetStoreMeta(int32_t userId, const std::string &bundleName, const std::string &storeName, int32_t instanceId) { StoreMetaData storeMetaData; @@ -391,11 +391,12 @@ void CloudServiceImpl::GetSchema(const Event &event) 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 schemaMeta = GetSchemaMeta(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().instanceId); if (schemaMeta.databases.empty()) { + ZLOGD("bundleName:%{public}s no cloud database", rdbEvent.GetStoreInfo().bundleName.c_str()); return; } - auto storeMeta = GetStoreMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, + auto storeMeta = GetStoreMeta(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, rdbEvent.GetStoreInfo().instanceId); auto instance = CloudServer::GetInstance(); if (instance == nullptr) { @@ -407,6 +408,7 @@ void CloudServiceImpl::GetSchema(const Event &event) return database.name == rdbEvent.GetStoreInfo().storeName; }); if (database == schemaMeta.databases.end()) { + ZLOGD("database: %{public}s is not cloud database", database->name.c_str()); return; } diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 35121c576..d78ec9a4c 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -60,8 +60,8 @@ 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, + SchemaMeta GetSchemaMeta(int32_t userId, const std::string &bundleName, int32_t instanceId); + StoreMetaData GetStoreMeta(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 GetCloudInfoFromMeta(CloudInfo &cloudInfo); diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index f7a9b2c79..1dfcc49b7 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -300,7 +300,7 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa auto syncer_ = std::make_shared(param, rdbObserver); StoreMetaData storeMetaData = GetStoreMetaData(param); MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData); - if (syncer_->Init(pid, uid, tokenId, storeMetaData) != 0) { + if (syncer_->Init(pid, uid, tokenId, storeMetaData) != RDB_OK) { return !syncers.empty(); } syncers[storeId] = syncer_; @@ -612,7 +612,7 @@ int32_t RdbServiceImpl::CreateMetaData(const RdbSyncerParam ¶m, StoreMetaDat return SetSecretKey(param, meta); } -bool RdbServiceImpl::SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta) +int32_t RdbServiceImpl::SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta) { SecretKeyMetaData newSecretKey; newSecretKey.storeType = meta.storeType; diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index c9f3eb546..18dd29b26 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -98,8 +98,10 @@ private: void OnAsyncComplete(pid_t pid, uint32_t seqNum, const SyncResult& result); int32_t CreateMetaData(const RdbSyncerParam ¶m, StoreMetaData &old); + StoreMetaData GetStoreMetaData(const RdbSyncerParam ¶m); - bool SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta); + + int32_t SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta); class DeathRecipientImpl : public IRemoteObject::DeathRecipient { public: -- Gitee From 1e67af684ad8fa3c8b89ef8f101742a996e56d9d Mon Sep 17 00:00:00 2001 From: htt1997 Date: Mon, 22 May 2023 22:10:41 +0800 Subject: [PATCH 06/14] refactor:delete unused code Signed-off-by: htt1997 --- .../framework/cloud/cloud_event.cpp | 2 +- .../framework/include/cloud/cloud_event.h | 2 +- .../service/rdb/rdb_service_impl.cpp | 40 ------------------- .../service/rdb/rdb_service_impl.h | 4 -- .../service/rdb/rdb_service_stub.cpp | 33 --------------- .../service/rdb/rdb_service_stub.h | 6 --- 6 files changed, 2 insertions(+), 85 deletions(-) diff --git a/services/distributeddataservice/framework/cloud/cloud_event.cpp b/services/distributeddataservice/framework/cloud/cloud_event.cpp index cb2604bd0..cdd0e02ea 100644 --- a/services/distributeddataservice/framework/cloud/cloud_event.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_event.cpp @@ -21,7 +21,7 @@ CloudEvent::CloudEvent(int32_t evtId, CloudEvent::StoreInfo storeInfo, const std { } -std::string CloudEvent::GetFeatureName() const +const std::string& CloudEvent::GetFeatureName() const { return featureName_; } diff --git a/services/distributeddataservice/framework/include/cloud/cloud_event.h b/services/distributeddataservice/framework/include/cloud/cloud_event.h index e1f04f41a..fbb09716e 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -39,7 +39,7 @@ public: CloudEvent(int32_t evtId, StoreInfo storeInfo, const std::string &featureName = "relational_store"); ~CloudEvent() = default; - std::string GetFeatureName() const; + const std::string& GetFeatureName() const; const StoreInfo& GetStoreInfo() const; private: diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 1dfcc49b7..d1a9d782f 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -495,46 +495,6 @@ int32_t RdbServiceImpl::RemoteQuery(const RdbSyncerParam& param, const std::stri return syncer->RemoteQuery(device, sql, selectionArgs, resultSet); } -int32_t RdbServiceImpl::CreateRDBTable(const RdbSyncerParam ¶m) -{ - if (!CheckAccess(param.bundleName_, param.storeName_)) { - ZLOGE("permission error"); - return RDB_ERROR; - } - - pid_t pid = IPCSkeleton::GetCallingPid(); - auto rdbObserver = new (std::nothrow) RdbStoreObserverImpl(this, pid); - if (rdbObserver == nullptr) { - return RDB_ERROR; - } - auto syncer = new (std::nothrow) RdbSyncer(param, rdbObserver); - if (syncer == nullptr) { - ZLOGE("new syncer error"); - return RDB_ERROR; - } - auto uid = IPCSkeleton::GetCallingUid(); - auto tokenId = IPCSkeleton::GetCallingTokenID(); - StoreMetaData storeMetaData = GetStoreMetaData(param); - MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData); - if (syncer->Init(pid, uid, tokenId, storeMetaData) != RDB_OK) { - ZLOGE("Init error"); - delete syncer; - return RDB_ERROR; - } - delete syncer; - return RDB_OK; -} - -int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) -{ - if (!CheckAccess(param.bundleName_, param.storeName_)) { - ZLOGE("permission error"); - return RDB_ERROR; - } - auto meta = GetStoreMetaData(param); - return MetaDataManager::GetInstance().DelMeta(meta.GetKey()) ? RDB_OK : RDB_ERROR; -} - int32_t RdbServiceImpl::OnInitialize() { return RDB_OK; diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index 18dd29b26..244726776 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -52,10 +52,6 @@ public: void OnChange(uint32_t tokenId, const std::string &storeName); - int32_t CreateRDBTable(const RdbSyncerParam ¶m) override; - - int32_t DestroyRDBTable(const RdbSyncerParam ¶m) override; - int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; int32_t OnExecutor(std::shared_ptr executors) override; diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index e879c5234..224109f0d 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -212,37 +212,4 @@ int RdbServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageP return RDB_ERROR; } -int32_t RdbServiceStub::OnRemoteDoCreateTable(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; - } - - int32_t status = CreateRDBTable(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::OnRemoteDoDestroyTable(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; - } - - int32_t status = DestroyRDBTable(param); - if (!ITypesUtil::Marshal(reply, status)) { - ZLOGE("Marshal status:0x%{public}x", status); - return IPC_STUB_WRITE_PARCEL_ERR; - } - return RDB_OK; -} } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.h b/services/distributeddataservice/service/rdb/rdb_service_stub.h index 16f4c169f..04c57e577 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.h +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.h @@ -66,10 +66,6 @@ private: int32_t OnRemoteDoRemoteQuery(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteDoCreateTable(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteDoDestroyTable(MessageParcel& data, MessageParcel& reply); - using RequestHandle = int (RdbServiceStub::*)(MessageParcel &, MessageParcel &); static constexpr RequestHandle HANDLERS[RDB_SERVICE_CMD_MAX] = { [RDB_SERVICE_CMD_OBTAIN_TABLE] = &RdbServiceStub::OnRemoteObtainDistributedTableName, @@ -80,8 +76,6 @@ private: [RDB_SERVICE_CMD_SUBSCRIBE] = &RdbServiceStub::OnRemoteDoSubscribe, [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_CMD_GET_SCHEMA] = &RdbServiceStub::OnGetSchema }; }; -- Gitee From 0b1520f5602cd5a1410a4fe3c52624232505fdcd Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 23 May 2023 10:54:55 +0800 Subject: [PATCH 07/14] style:code check Signed-off-by: htt1997 --- .../distributeddataservice/service/rdb/rdb_service_impl.cpp | 2 +- .../distributeddataservice/service/rdb/rdb_service_stub.cpp | 1 - services/distributeddataservice/service/rdb/rdb_syncer.cpp | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 46955dc6f..b4980b4a6 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -126,7 +126,7 @@ int32_t RdbServiceImpl::ResolveAutoLaunch(const std::string &identifier, Distrib ZLOGI("%{public}.6s", identifierHex.c_str()); std::vector entries; auto localId = DmAdapter::GetInstance().GetLocalDevice().uuid; - if (!MetaDataManager::GetInstance().LoadMeta(StoreMetaData::GetPrefix({ localId }), entries)) { + if (!MetaDataManager::GetInstance().LoadMeta(StoreMetaData::GetPrefix( { localId } ), entries)) { ZLOGE("get meta failed"); return false; } diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index 224109f0d..034e98220 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -211,5 +211,4 @@ int RdbServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageP } return RDB_ERROR; } - } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/services/distributeddataservice/service/rdb/rdb_syncer.cpp index 96318c068..55f44b5a7 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -94,7 +94,7 @@ std::string RdbSyncer::GetBundleName() const std::string RdbSyncer::GetAppId() const { - return DistributedData::CheckerManager::GetInstance().GetAppId({ uid_, token_, param_.bundleName_ }); + return DistributedData::CheckerManager::GetInstance().GetAppId( { uid_, token_, param_.bundleName_ } ); } std::string RdbSyncer::GetStoreId() const -- Gitee From 49f0dfb80579887f86b0b0708f623094fbcfec79 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 23 May 2023 15:42:47 +0800 Subject: [PATCH 08/14] fix:Review comments modification Signed-off-by: htt1997 --- .../framework/cloud/schema_meta.cpp | 14 ++-- .../framework/include/cloud/schema_meta.h | 56 +++++++------ .../framework/include/store/general_store.h | 21 ++++- .../framework/include/store/general_value.h | 24 ++++++ .../service/cloud/cloud_service_impl.cpp | 2 +- .../service/rdb/rdb_general_store.cpp | 84 ++++++++++++------- .../service/rdb/rdb_general_store.h | 12 +-- 7 files changed, 140 insertions(+), 73 deletions(-) diff --git a/services/distributeddataservice/framework/cloud/schema_meta.cpp b/services/distributeddataservice/framework/cloud/schema_meta.cpp index b6a8cd4b3..d54543613 100644 --- a/services/distributeddataservice/framework/cloud/schema_meta.cpp +++ b/services/distributeddataservice/framework/cloud/schema_meta.cpp @@ -29,7 +29,7 @@ bool SchemaMeta::Unmarshal(const Serializable::json &node) return true; } -bool SchemaMeta::Database::Marshal(Serializable::json &node) const +bool Database::Marshal(Serializable::json &node) const { SetValue(node[GET_NAME(name)], name); SetValue(node[GET_NAME(alias)], alias); @@ -37,7 +37,7 @@ bool SchemaMeta::Database::Marshal(Serializable::json &node) const return true; } -bool SchemaMeta::Database::Unmarshal(const Serializable::json &node) +bool Database::Unmarshal(const Serializable::json &node) { GetValue(node, GET_NAME(name), name); GetValue(node, GET_NAME(alias), alias); @@ -45,7 +45,7 @@ bool SchemaMeta::Database::Unmarshal(const Serializable::json &node) return true; } -bool SchemaMeta::Table::Marshal(Serializable::json &node) const +bool Table::Marshal(Serializable::json &node) const { SetValue(node[GET_NAME(name)], name); SetValue(node[GET_NAME(alias)], alias); @@ -53,7 +53,7 @@ bool SchemaMeta::Table::Marshal(Serializable::json &node) const return true; } -bool SchemaMeta::Table::Unmarshal(const Serializable::json &node) +bool Table::Unmarshal(const Serializable::json &node) { GetValue(node, GET_NAME(name), name); GetValue(node, GET_NAME(alias), alias); @@ -61,7 +61,7 @@ bool SchemaMeta::Table::Unmarshal(const Serializable::json &node) return true; } -bool SchemaMeta::Field::Marshal(Serializable::json &node) const +bool Field::Marshal(Serializable::json &node) const { SetValue(node[GET_NAME(colName)], colName); SetValue(node[GET_NAME(alias)], alias); @@ -71,7 +71,7 @@ bool SchemaMeta::Field::Marshal(Serializable::json &node) const return true; } -bool SchemaMeta::Field::Unmarshal(const Serializable::json &node) +bool Field::Unmarshal(const Serializable::json &node) { GetValue(node, GET_NAME(colName), colName); GetValue(node, GET_NAME(alias), alias); @@ -81,7 +81,7 @@ bool SchemaMeta::Field::Unmarshal(const Serializable::json &node) return true; } -SchemaMeta::Database SchemaMeta::GetDataBase(const std::string &storeId) +Database SchemaMeta::GetDataBase(const std::string &storeId) { for (const auto &database : databases) { if (database.name == storeId) { diff --git a/services/distributeddataservice/framework/include/cloud/schema_meta.h b/services/distributeddataservice/framework/include/cloud/schema_meta.h index 188a01326..d2064961e 100644 --- a/services/distributeddataservice/framework/include/cloud/schema_meta.h +++ b/services/distributeddataservice/framework/include/cloud/schema_meta.h @@ -17,39 +17,43 @@ #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SCHEMA_META_H #include "serializable/serializable.h" namespace OHOS::DistributedData { +struct API_EXPORT Field final : public Serializable { + std::string colName; + std::string alias; + int32_t type = 0; + bool primary = false; + bool nullable = true; + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; +}; + +struct API_EXPORT Table final : public Serializable { + std::string name; + std::string alias; + std::vector fields; + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; +}; + +struct API_EXPORT Database final : public Serializable { + std::string name = ""; + std::string alias; + std::vector tables; + + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; +}; + class API_EXPORT SchemaMeta final : public Serializable { public: + using Database = Database; + using Table = Table; + using Field = Field; static constexpr const char *DELETE_FIELD = "#_deleted"; static constexpr const char *GID_FIELD = "#_gid"; static constexpr const char *CREATE_FIELD = "#_createTime"; static constexpr const char *MODIFY_FIELD = "#_modifyTime"; static constexpr const char *CURSOR_FIELD = "#_cursor"; - struct API_EXPORT Field final : public Serializable { - std::string colName; - std::string alias; - int32_t type = 0; - bool primary = false; - bool nullable = true; - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; - }; - - struct API_EXPORT Table final : public Serializable { - std::string name; - std::string alias; - std::vector fields; - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; - }; - - struct API_EXPORT Database final : public Serializable { - std::string name = ""; - std::string alias; - std::vector
tables; - - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; - }; int32_t version = 0; std::vector databases; diff --git a/services/distributeddataservice/framework/include/store/general_store.h b/services/distributeddataservice/framework/include/store/general_store.h index e31eb8899..579634d28 100644 --- a/services/distributeddataservice/framework/include/store/general_store.h +++ b/services/distributeddataservice/framework/include/store/general_store.h @@ -18,21 +18,32 @@ #include #include +#include "cloud/schema_meta.h" #include "store/cursor.h" #include "store/general_value.h" #include "store/general_watcher.h" namespace OHOS::DistributedData { class CloudDB; -class SchemaMeta; +class AssetLoader; +struct Database; class GeneralStore { public: using Watcher = GeneralWatcher; - using Async = std::function>)>; + using AsyncDetail = std::function; + using AsyncStatus = std::function>)>; using Devices = std::vector; + struct BindInfo { + BindInfo(std::shared_ptr db = nullptr, std::shared_ptr loader = nullptr) + : db_(std::move(db)), loader_(std::move(loader)) + { + } + std::shared_ptr db_; + std::shared_ptr loader_; + }; virtual ~GeneralStore() = default; - virtual int32_t Bind(const SchemaMeta &schemaMeta, std::shared_ptr cloudDb) = 0; + virtual int32_t Bind(const Database &database, BindInfo bindInfo) = 0; virtual int32_t Execute(const std::string &table, const std::string &sql) = 0; @@ -46,7 +57,9 @@ public: virtual std::shared_ptr Query(const std::string &table, GenQuery &query) = 0; - virtual int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) = 0; + virtual int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, AsyncDetail async, int32_t wait) = 0; + + virtual int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, AsyncStatus async, int32_t wait) = 0; virtual int32_t Watch(int32_t origin, Watcher &watcher) = 0; diff --git a/services/distributeddataservice/framework/include/store/general_value.h b/services/distributeddataservice/framework/include/store/general_value.h index 05d4766d3..9ec687945 100644 --- a/services/distributeddataservice/framework/include/store/general_value.h +++ b/services/distributeddataservice/framework/include/store/general_value.h @@ -24,6 +24,30 @@ #include "error/general_error.h" #include "traits.h" namespace OHOS::DistributedData { +enum Progress { + SYNC_BEGIN, + SYNC_IN_PROGRESS, + SYNC_FINISH, +}; + +struct Statistic { + int32_t total; + int32_t success; + int32_t failed; + int32_t untreated; +}; + +struct TableDetails { + Statistic upload; + Statistic download; +}; + +struct ProgressDetails { + int32_t progress; + int32_t code; + std::map details; +}; + struct Asset { uint32_t version; std::string name; diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index f6d8c2241..18ce34373 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -424,7 +424,7 @@ void CloudServiceImpl::GetSchema(const Event &event) ZLOGE("store is nullptr"); return; } - store->Bind(schemaMeta, cloudDB); + store->Bind(*database, cloudDB); for (const auto &table : database->tables) { ZLOGD("table: %{public}s sync start", table.name.c_str()); // do sync diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index bd1f118c5..c5bf2b394 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -15,6 +15,9 @@ #define LOG_TAG "RdbGeneralStore" #include "rdb_general_store.h" +#include "cloud/asset_loader.h" +#include "cloud/cloud_db.h" +#include "cloud/schema_meta.h" #include "crypto_manager.h" #include "log_print.h" #include "metadata/meta_data_manager.h" @@ -23,11 +26,13 @@ #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; +using namespace NativeRdb; +using DBField = DistributedDB::Field; +using DBTable = DistributedDB::TableSchema; +using DBSchema = DistributedDB::DataBaseSchema; class RdbOpenCallbackImpl : public RdbOpenCallback { public: int OnCreate(RdbStore &rdbStore) override @@ -69,8 +74,47 @@ RdbGeneralStore::RdbGeneralStore(const StoreMetaData &meta) : manager_(meta.appI } } +RdbGeneralStore::~RdbGeneralStore() +{ + manager_.CloseStore(delegate_); + delegate_ = nullptr; + store_ = nullptr; + bindInfo_.loader_ = nullptr; + bindInfo_.db_->Close(); + bindInfo_.db_ = nullptr; +} + +int32_t RdbGeneralStore::Bind(const Database &database, BindInfo bindInfo) +{ + bindInfo_ = std::move(bindInfo); + // SetCloudDB + DBSchema schema; + schema.tables.resize(database.tables.size()); + for (size_t i = 0; i < database.tables.size(); i++) { + const Table &table = database.tables[i]; + DBTable &dbTable = schema.tables[i]; + dbTable.name = table.name; + for (auto &field : table.fields) { + DBField dbField; + dbField.colName = field.colName; + dbField.type = field.type; + dbField.primary = field.primary; + dbField.nullable = field.nullable; + dbTable.fields.push_back(std::move(dbField)); + } + } + // SetCloudDbSchema + return GeneralError::E_NOT_SUPPORT; +} + int32_t RdbGeneralStore::Close() { + manager_.CloseStore(delegate_); + delegate_ = nullptr; + store_ = nullptr; + bindInfo_.loader_ = nullptr; + bindInfo_.db_->Close(); + bindInfo_.db_ = nullptr; return 0; } @@ -104,43 +148,25 @@ std::shared_ptr RdbGeneralStore::Query(const std::string &table, GenQuer return std::shared_ptr(); } -int32_t RdbGeneralStore::Watch(int32_t origin, Watcher &watcher) +int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &query, AsyncDetail async, int32_t wait) { - watcher_ = &watcher; return GeneralError::E_NOT_SUPPORT; + } -int32_t RdbGeneralStore::Unwatch(int32_t origin, Watcher &watcher) +int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &query, AsyncStatus async, int32_t wait) { - return 0; + return GeneralError::E_NOT_SUPPORT; + } -int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) +int32_t RdbGeneralStore::Watch(int32_t origin, Watcher &watcher) { - 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; + return GeneralError::E_NOT_SUPPORT; } -int32_t RdbGeneralStore::Bind(const SchemaMeta &schemaMeta, std::shared_ptr cloudDb) +int32_t RdbGeneralStore::Unwatch(int32_t origin, Watcher &watcher) { - cloudDb_ = std::move(cloudDb); - return 0; + return GeneralError::E_NOT_SUPPORT; } } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h index 3d2fda304..f337fe564 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -32,21 +32,22 @@ public: using Value = DistributedData::Value; using Values = DistributedData::Values; using StoreMetaData = DistributedData::StoreMetaData; - using SchemaMeta = DistributedData::SchemaMeta; - using CloudDB = DistributedData::CloudDB; + using Database = DistributedData::Database; using RdbStore = OHOS::NativeRdb::RdbStore; using RdbDelegate = DistributedDB::RelationalStoreDelegate; using RdbManager = DistributedDB::RelationalStoreManager; explicit RdbGeneralStore(const StoreMetaData &metaData); - int32_t Bind(const SchemaMeta &schemaMeta, std::shared_ptr cloudDb) override; + ~RdbGeneralStore(); + int32_t Bind(const Database &database, BindInfo bindInfo) 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 Sync(const Devices &devices, int32_t mode, GenQuery &query, AsyncDetail async, int32_t wait) override; + int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, AsyncStatus 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 Close() override; @@ -57,8 +58,7 @@ private: RdbManager manager_; RdbDelegate *delegate_ = nullptr; std::shared_ptr store_; - std::shared_ptr cloudDb_; - Watcher *watcher_; + BindInfo bindInfo_; }; } // namespace OHOS::DistributedRdb #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_STORE_H -- Gitee From a030f394b02dba1d8a92b7c3e51f9640e2a6010e Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 23 May 2023 21:17:59 +0800 Subject: [PATCH 09/14] fix:Review comments modification Signed-off-by: htt1997 --- .../service/cloud/cloud_service_impl.cpp | 3 +- .../service/rdb/rdb_general_store.cpp | 2 - .../service/rdb/rdb_service_impl.cpp | 44 ++++++++++++++++--- .../service/rdb/rdb_service_impl.h | 2 + .../service/rdb/rdb_syncer.cpp | 19 ++++++-- .../service/rdb/rdb_syncer.h | 2 + 6 files changed, 59 insertions(+), 13 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 18ce34373..35fdbccfc 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -415,7 +415,8 @@ void CloudServiceImpl::GetSchema(const Event &event) ZLOGD("database: %{public}s sync start", database->name.c_str()); auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, *database); if (cloudDB == nullptr) { - ZLOGE("cloudDB is nullptr"); + ZLOGE("cloudDB is nullptr, bundleName:%{public}s user:%{public}d database:%{public}s", + rdbEvent.GetStoreInfo().bundleName.c_str(), rdbEvent.GetStoreInfo().user, database->name.c_str()); return; } AutoCache::Watchers watchers; diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index c5bf2b394..8ccc8d1df 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -151,13 +151,11 @@ std::shared_ptr RdbGeneralStore::Query(const std::string &table, GenQuer int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &query, AsyncDetail async, int32_t wait) { return GeneralError::E_NOT_SUPPORT; - } int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &query, AsyncStatus async, int32_t wait) { return GeneralError::E_NOT_SUPPORT; - } int32_t RdbGeneralStore::Watch(int32_t origin, Watcher &watcher) diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index b4980b4a6..4e38a5aab 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -554,16 +554,22 @@ int32_t RdbServiceImpl::CreateMetaData(const RdbSyncerParam ¶m, StoreMetaDat meta.isEncrypt, old.area, meta.area); return RDB_ERROR; } - - auto saved = MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta); - if (!saved) { - return RDB_ERROR; + if (!isCreated || meta != old) { + Upgrade(param, meta, old); + ZLOGE("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " + "area:%{public}d->%{public}d", + meta.bundleName.c_str(), meta.storeId.c_str(), old.storeType, meta.storeType, old.isEncrypt, + meta.isEncrypt, old.area, meta.area); + MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta); } AppIDMetaData appIdMeta; appIdMeta.bundleName = meta.bundleName; appIdMeta.appId = meta.appId; - saved = MetaDataManager::GetInstance().SaveMeta(appIdMeta.GetKey(), appIdMeta, true); - if (!saved) { + if (!MetaDataManager::GetInstance().SaveMeta(appIdMeta.GetKey(), appIdMeta, true)) { + ZLOGE("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " + "area:%{public}d->%{public}d", + meta.bundleName.c_str(), meta.storeId.c_str(), old.storeType, meta.storeType, old.isEncrypt, + meta.isEncrypt, old.area, meta.area); return RDB_ERROR; } if (!param.isEncrypt_ || param.password_.empty()) { @@ -586,6 +592,32 @@ int32_t RdbServiceImpl::SetSecretKey(const RdbSyncerParam ¶m, const StoreMet return MetaDataManager::GetInstance().SaveMeta(meta.GetSecretKey(), newSecretKey, true) ? RDB_OK : RDB_ERROR; } +int32_t RdbServiceImpl::Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &meta, const StoreMetaData &old) +{ + if (old.storeType == RDB_DEVICE_COLLABORATION && old.version < StoreMetaData::UUID_CHANGED_TAG) { + pid_t pid = IPCSkeleton::GetCallingPid(); + auto rdbObserver = new (std::nothrow) RdbStoreObserverImpl(this, pid); + if (rdbObserver == nullptr) { + return RDB_ERROR; + } + auto syncer = new (std::nothrow) RdbSyncer(param, rdbObserver); + if (syncer == nullptr) { + ZLOGE("new syncer error"); + return RDB_ERROR; + } + auto uid = IPCSkeleton::GetCallingUid(); + auto tokenId = IPCSkeleton::GetCallingTokenID(); + if (syncer->Init(pid, uid, tokenId, meta) != RDB_OK) { + ZLOGE("Init error"); + delete syncer; + return RDB_ERROR; + } + syncer->RemoveDeviceData(); + delete syncer; + } + return RDB_OK; +} + int32_t RdbServiceImpl::OnBind(const BindInfo &bindInfo) { executors_ = bindInfo.executors; diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index 75e0cbabf..b3de8bba2 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -99,6 +99,8 @@ private: int32_t SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta); + int32_t Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &meta, const StoreMetaData &old); + class DeathRecipientImpl : public IRemoteObject::DeathRecipient { public: using DeathCallback = std::function; diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/services/distributeddataservice/service/rdb/rdb_syncer.cpp index 55f44b5a7..f969ce434 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -114,10 +114,6 @@ int32_t RdbSyncer::Init(pid_t pid, pid_t uid, uint32_t token, const StoreMetaDat return RDB_ERROR; } - if (meta.storeType == RDB_DEVICE_COLLABORATION && meta.version < StoreMetaData::UUID_CHANGED_TAG) { - delegate_->RemoveDeviceData(); - } - ZLOGI("success"); return RDB_OK; } @@ -409,4 +405,19 @@ int32_t RdbSyncer::RemoteQuery(const std::string& device, const std::string& sql } return RDB_OK; } + +int32_t RdbSyncer::RemoveDeviceData() +{ + auto* delegate = GetDelegate(); + if (delegate == nullptr) { + ZLOGE("delegate is nullptr"); + return RDB_ERROR; + } + DistributedDB::DBStatus status = delegate->RemoveDeviceData(); + if (status != DistributedDB::DBStatus::OK) { + ZLOGE("DistributedDB RemoveDeviceData failed, status is %{public}d.", status); + return RDB_ERROR; + } + return RDB_OK; +} } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.h b/services/distributeddataservice/service/rdb/rdb_syncer.h index 68d1e44fb..30dc84021 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -55,6 +55,8 @@ public: int32_t RemoteQuery(const std::string& device, const std::string& sql, const std::vector& selectionArgs, sptr& resultSet); + int32_t RemoveDeviceData(); + static std::string RemoveSuffix(const std::string& name); static int32_t GetInstIndex(uint32_t tokenId, const std::string &bundleName); -- Gitee From 122b48e7a26b893df5c38f42bb85a5f4ca8c7267 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 24 May 2023 15:26:16 +0800 Subject: [PATCH 10/14] fix:Review comments modification Signed-off-by: htt1997 --- .../service/rdb/rdb_service_impl.cpp | 26 +++++-------------- .../service/rdb/rdb_service_impl.h | 2 +- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 4e38a5aab..3b5baedb5 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -555,8 +555,8 @@ int32_t RdbServiceImpl::CreateMetaData(const RdbSyncerParam ¶m, StoreMetaDat return RDB_ERROR; } if (!isCreated || meta != old) { - Upgrade(param, meta, old); - ZLOGE("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " + Upgrade(param, old); + ZLOGD("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " "area:%{public}d->%{public}d", meta.bundleName.c_str(), meta.storeId.c_str(), old.storeType, meta.storeType, old.isEncrypt, meta.isEncrypt, old.area, meta.area); @@ -592,28 +592,16 @@ int32_t RdbServiceImpl::SetSecretKey(const RdbSyncerParam ¶m, const StoreMet return MetaDataManager::GetInstance().SaveMeta(meta.GetSecretKey(), newSecretKey, true) ? RDB_OK : RDB_ERROR; } -int32_t RdbServiceImpl::Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &meta, const StoreMetaData &old) +int32_t RdbServiceImpl::Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &old) { if (old.storeType == RDB_DEVICE_COLLABORATION && old.version < StoreMetaData::UUID_CHANGED_TAG) { - pid_t pid = IPCSkeleton::GetCallingPid(); - auto rdbObserver = new (std::nothrow) RdbStoreObserverImpl(this, pid); - if (rdbObserver == nullptr) { - return RDB_ERROR; - } - auto syncer = new (std::nothrow) RdbSyncer(param, rdbObserver); + auto syncer = GetRdbSyncer(param); if (syncer == nullptr) { - ZLOGE("new syncer error"); - return RDB_ERROR; - } - auto uid = IPCSkeleton::GetCallingUid(); - auto tokenId = IPCSkeleton::GetCallingTokenID(); - if (syncer->Init(pid, uid, tokenId, meta) != RDB_OK) { - ZLOGE("Init error"); - delete syncer; + ZLOGE("syncer is null, bundleName:%{public}s storeName:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str()); return RDB_ERROR; } - syncer->RemoveDeviceData(); - delete syncer; + return syncer->RemoveDeviceData(); } return RDB_OK; } diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index b3de8bba2..01831def7 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -99,7 +99,7 @@ private: int32_t SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta); - int32_t Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &meta, const StoreMetaData &old); + int32_t Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &old); class DeathRecipientImpl : public IRemoteObject::DeathRecipient { public: -- Gitee From 9d44fe7985c88dd1330839febb914a7b435f9677 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 24 May 2023 22:34:25 +0800 Subject: [PATCH 11/14] fix:check cloudInfo From server 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 f3984e45b..fca27edb3 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -247,7 +247,7 @@ int32_t CloudServiceImpl::GetCloudInfoFromServer(CloudInfo &cloudInfo) return NOT_SUPPORT; } cloudInfo = instance->GetServerInfo(cloudInfo.user); - return SUCCESS; + return cloudInfo.IsValid() ? SUCCESS : ERROR; } void CloudServiceImpl::UpdateCloudInfo(CloudInfo &cloudInfo) -- Gitee From c35435b7b3716f52a3299678735be5cc60ed5321 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Fri, 26 May 2023 10:02:47 +0800 Subject: [PATCH 12/14] style:code check Signed-off-by: htt1997 --- services/distributeddataservice/framework/cloud/cloud_event.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/framework/cloud/cloud_event.cpp b/services/distributeddataservice/framework/cloud/cloud_event.cpp index cdd0e02ea..4cc139bd1 100644 --- a/services/distributeddataservice/framework/cloud/cloud_event.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_event.cpp @@ -21,7 +21,7 @@ CloudEvent::CloudEvent(int32_t evtId, CloudEvent::StoreInfo storeInfo, const std { } -const std::string& CloudEvent::GetFeatureName() const +const std::string& CloudEvent::GetFeatureName() const { return featureName_; } -- Gitee From a69875d17dd0e1e2ac6f9a3434a7028d291b5d93 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Fri, 26 May 2023 17:09:32 +0800 Subject: [PATCH 13/14] refactor:Getschema switch async Signed-off-by: htt1997 --- .../service/cloud/cloud_service_impl.cpp | 30 +++++++++++-------- .../service/rdb/rdb_service_impl.cpp | 8 +++-- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index fca27edb3..5e554b82e 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -320,14 +320,14 @@ ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry, int32_t user) SchemaMeta CloudServiceImpl::GetSchemaMeta(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; - } CloudInfo cloudInfo; cloudInfo.user = userId; if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + ZLOGE("instance is nullptr"); + return schemaMeta; + } cloudInfo = instance->GetServerInfo(userId); if (!cloudInfo.IsValid()) { ZLOGE("cloudInfo is invalid"); @@ -344,6 +344,11 @@ SchemaMeta CloudServiceImpl::GetSchemaMeta(int32_t userId, const std::string &bu } std::string schemaKey = cloudInfo.GetSchemaKey(bundleName, instanceId); if (!MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true)) { + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + ZLOGE("instance is nullptr"); + return schemaMeta; + } schemaMeta = instance->GetAppSchema(cloudInfo.user, bundleName); MetaDataManager::GetInstance().SaveMeta(schemaKey, schemaMeta, true); } @@ -396,13 +401,6 @@ void CloudServiceImpl::GetSchema(const Event &event) ZLOGD("bundleName:%{public}s no cloud database", rdbEvent.GetStoreInfo().bundleName.c_str()); return; } - auto storeMeta = GetStoreMeta(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, - rdbEvent.GetStoreInfo().instanceId); - auto instance = CloudServer::GetInstance(); - if (instance == nullptr) { - ZLOGE("instance is nullptr"); - return; - } auto database = std::find_if(schemaMeta.databases.begin(), schemaMeta.databases.end(), [&rdbEvent](const auto &database) { return database.name == rdbEvent.GetStoreInfo().storeName; @@ -412,6 +410,11 @@ void CloudServiceImpl::GetSchema(const Event &event) return; } + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + ZLOGE("instance is nullptr"); + return; + } ZLOGD("database: %{public}s sync start", database->name.c_str()); auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, *database); if (cloudDB == nullptr) { @@ -419,6 +422,9 @@ void CloudServiceImpl::GetSchema(const Event &event) rdbEvent.GetStoreInfo().bundleName.c_str(), rdbEvent.GetStoreInfo().user, database->name.c_str()); return; } + + auto storeMeta = GetStoreMeta(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, + rdbEvent.GetStoreInfo().instanceId); AutoCache::Watchers watchers; auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers); if (store == nullptr) { diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 3b5baedb5..e9c3fda3e 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -511,12 +511,14 @@ int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) return RDB_ERROR; } - EventCenter::Defer defer; 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)); + executors_->Execute([storeInfo]() { + auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "relational_store"); + EventCenter::GetInstance().PostEvent(move(event)); + }); + return RDB_OK; } -- Gitee From 2f35ee013ae1470562fc2bed07361e9fbb384c87 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sat, 27 May 2023 11:23:46 +0800 Subject: [PATCH 14/14] refactor:refactor Signed-off-by: htt1997 --- .../service/rdb/rdb_service_impl.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index e9c3fda3e..24b656daf 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -511,13 +511,16 @@ int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) return RDB_ERROR; } - CloudEvent::StoreInfo storeInfo { IPCSkeleton::GetCallingTokenID(), param.bundleName_, + if (executors_ != nullptr) { + CloudEvent::StoreInfo storeInfo { IPCSkeleton::GetCallingTokenID(), param.bundleName_, RdbSyncer::RemoveSuffix(param.storeName_), RdbSyncer::GetInstIndex(IPCSkeleton::GetCallingTokenID(), param.bundleName_) }; - executors_->Execute([storeInfo]() { - auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "relational_store"); - EventCenter::GetInstance().PostEvent(move(event)); - }); + executors_->Execute([storeInfo]() { + auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), + "relational_store"); + EventCenter::GetInstance().PostEvent(move(event)); + }); + } return RDB_OK; } -- Gitee