diff --git a/datamgr_service/services/distributeddataservice/app/src/task_manager.cpp b/datamgr_service/services/distributeddataservice/app/src/task_manager.cpp index 1332fc203d99d6a8634ee8e5f2a237c4670cf245..ec06928edbcd050b2e9112248b7f242cf4c8504d 100644 --- a/datamgr_service/services/distributeddataservice/app/src/task_manager.cpp +++ b/datamgr_service/services/distributeddataservice/app/src/task_manager.cpp @@ -15,9 +15,8 @@ #include "task_manager.h" namespace OHOS::DistributedData { -TaskManager::TaskManager(std::shared_ptr executors) +TaskManager::TaskManager(std::shared_ptr executors) : executors_(executors) { - executors_ = executors; } TaskManager::~TaskManager() diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 1b32c37a6db100ddba2cf2bc481d83dc3f3df070..09495b84d2d2c788f09b1c5d3667a9521053d01f 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -354,7 +354,7 @@ ExecutorPool::Task CloudServiceImpl::GenTask(int32_t retry, int32_t user, AsyncW }; } -std::vector CloudServiceImpl::GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId) +std::vector CloudServiceImpl::GetSchemaMeta(int32_t userId, const std::string &bundleName, int32_t instanceId) { CloudInfo cloudInfo = GetCloudInfo(userId); if (!cloudInfo.IsValid()) { @@ -424,7 +424,7 @@ void CloudServiceImpl::GetSchema(const Event &event) auto &storeInfo = rdbEvent.GetStoreInfo(); ZLOGD("Start GetSchema, bundleName:%{public}s, storeName:%{public}s, instanceId:%{public}d", storeInfo.bundleName.c_str(), storeInfo.storeName.c_str(), storeInfo.instanceId); - GetSchemaMata(storeInfo.user, storeInfo.bundleName, storeInfo.instanceId); + GetSchemaMeta(storeInfo.user, storeInfo.bundleName, storeInfo.instanceId); } bool CloudServiceImpl::DoSubscribe(int32_t user) diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h index 451c792300c870048b44e1d5f17533cfbf0f3969..adc9a6648b7ddc7dfb8f727f9352989285e9b087 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -68,7 +68,7 @@ private: bool UpdateCloudInfo(int32_t user); bool UpdateSchema(int32_t user); - std::vector GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId); + std::vector GetSchemaMeta(int32_t userId, const std::string &bundleName, int32_t instanceId); CloudInfo GetCloudInfo(int32_t userId); int32_t GetCloudInfo(uint32_t tokenId, const std::string &id, CloudInfo &cloudInfo); int32_t GetCloudInfoFromMeta(CloudInfo &cloudInfo); 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 be98c6aa1f16d0c5f2e782dad3ae836bf617fa68..88cad34f054520898e6a407436f4d368c8822519 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -243,9 +243,10 @@ void RdbServiceImpl::SyncerTimeout(std::shared_ptr syncer) if (syncer == nullptr) { return; } - ZLOGI("%{public}s", syncer->GetStoreId().c_str()); - syncers_.ComputeIfPresent(syncer->GetPid(), [this, &syncer](const auto& key, StoreSyncersType& syncers) { - syncers.erase(syncer->GetStoreId()); + auto storeId = syncer->GetStoreId(); + ZLOGI("%{public}s", storeId.c_str()); + syncers_.ComputeIfPresent(syncer->GetPid(), [this, storeId](const auto& key, StoreSyncersType& syncers) { + syncers.erase(storeId); syncerNum_--; return true; }); @@ -283,7 +284,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_; @@ -304,7 +305,8 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa return syncer; } -int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables) +int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables, + int32_t type) { ZLOGI("enter"); if (!CheckAccess(param.bundleName_, param.storeName_)) { @@ -315,7 +317,7 @@ int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const if (syncer == nullptr) { return RDB_ERROR; } - return syncer->SetDistributedTables(tables); + return syncer->SetDistributedTables(tables, type); } std::pair RdbServiceImpl::DoSync(const RdbSyncerParam ¶m, const Option &option, @@ -512,17 +514,20 @@ int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) if (CreateMetaData(param, storeMeta) != RDB_OK) { return RDB_ERROR; } - - EventCenter::Defer defer; - CloudEvent::StoreInfo storeInfo; - storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); - storeInfo.bundleName = param.bundleName_; - storeInfo.storeName = RdbSyncer::RemoveSuffix(param.storeName_); - auto [instanceId, user]= RdbSyncer::GetInstIndexAndUser(storeInfo.tokenId, param.bundleName_); - storeInfo.instanceId = instanceId; - storeInfo.user = user; - auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo)); - EventCenter::GetInstance().PostEvent(move(event)); + if (executors_ != nullptr) { + CloudEvent::StoreInfo storeInfo; + storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + storeInfo.bundleName = param.bundleName_; + storeInfo.storeName = RdbSyncer::RemoveSuffix(param.storeName_); + auto [instanceId, user]= RdbSyncer::GetInstIndexAndUser(storeInfo.tokenId, param.bundleName_); + storeInfo.instanceId = instanceId; + storeInfo.user = user; + executors_->Execute([storeInfo]() { + auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo)); + EventCenter::GetInstance().PostEvent(move(event)); + return; + }); + } return RDB_OK; } @@ -561,16 +566,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, 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); + 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()) { @@ -579,7 +590,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; @@ -593,6 +604,20 @@ bool RdbServiceImpl::SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaDa return MetaDataManager::GetInstance().SaveMeta(meta.GetSecretKey(), newSecretKey, true) ? RDB_OK : RDB_ERROR; } +int32_t RdbServiceImpl::Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &old) +{ + if (old.storeType == RDB_DEVICE_COLLABORATION && old.version < StoreMetaData::UUID_CHANGED_TAG) { + auto syncer = GetRdbSyncer(param); + if (syncer == nullptr) { + ZLOGE("syncer is null, bundleName:%{public}s storeName:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str()); + return RDB_ERROR; + } + return syncer->RemoveDeviceData(); + } + return RDB_OK; +} + int32_t RdbServiceImpl::OnBind(const BindInfo &bindInfo) { executors_ = bindInfo.executors; 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 b8150b5205fd0cfefef30dd174301a17b8885519..3007d191da6b8e297109acbae184d5fdf6506690 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -42,7 +42,8 @@ public: int32_t InitNotifier(const RdbSyncerParam ¶m, sptr notifier) override; - int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector& tables) override; + int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables, + int32_t type = DISTRIBUTED_DEVICE) override; int32_t RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql, const std::vector& selectionArgs, sptr& resultSet) override; @@ -63,6 +64,7 @@ public: int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) override; int32_t GetSchema(const RdbSyncerParam ¶m) override; + int32_t OnBind(const BindInfo &bindInfo) override; private: @@ -108,8 +110,13 @@ private: void OnAsyncComplete(uint32_t tokenId, uint32_t seqNum, Details&& 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); + + int32_t Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &old); + static std::string TransferStringToHex(const std::string& origStr); static Factory factory_; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp index 0934069c4fb085aa3df4f094b922467ecf5a1323..ab45024546790bb4f745c31eb6b47c77d971198c 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -117,10 +117,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; } @@ -212,17 +208,17 @@ DistributedDB::RelationalStoreDelegate* RdbSyncer::GetDelegate() return delegate_; } -int32_t RdbSyncer::SetDistributedTables(const std::vector &tables) +int32_t RdbSyncer::SetDistributedTables(const std::vector &tables, int32_t type) { - auto* delegate = GetDelegate(); + auto *delegate = GetDelegate(); if (delegate == nullptr) { ZLOGE("delegate is nullptr"); return RDB_ERROR; } - for (const auto& table : tables) { + for (const auto &table : tables) { ZLOGI("%{public}s", table.c_str()); - auto dBStatus = delegate->CreateDistributedTable(table); + auto dBStatus = delegate->CreateDistributedTable(table, static_cast(type)); if (dBStatus != DistributedDB::DBStatus::OK) { ZLOGE("create distributed table failed, table:%{public}s, err:%{public}d", table.c_str(), dBStatus); return RDB_ERROR; @@ -423,4 +419,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/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h index d2ae68bd2858e47bcd82e3f11cdbe7e30fc8bd60..e3c6cc13867a9ca5249aeb5c226215c7670d6e87 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -51,13 +51,15 @@ public: std::string GetIdentifier() const; - int32_t SetDistributedTables(const std::vector &tables); + int32_t SetDistributedTables(const std::vector &tables, int32_t type); int32_t DoSync(const Option &option, const RdbPredicates &predicates, const AsyncDetail &async); 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 std::pair GetInstIndexAndUser(uint32_t tokenId, const std::string &bundleName); diff --git a/relational_store/frameworks/js/napi/common/include/js_utils.h b/relational_store/frameworks/js/napi/common/include/js_utils.h index 91708d67b1c5facf6cbd357879b0bd235328f627..d003529d5a02482096827ab08309233299ad63c8 100644 --- a/relational_store/frameworks/js/napi/common/include/js_utils.h +++ b/relational_store/frameworks/js/napi/common/include/js_utils.h @@ -27,6 +27,7 @@ #include "napi/native_api.h" #include "napi/native_common.h" #include "napi/native_node_api.h" +#include "rdb_types.h" namespace OHOS { namespace AppDataMgrJsKit { @@ -96,6 +97,11 @@ napi_value Convert2JSValue(napi_env env, int64_t value); napi_value Convert2JSValue(napi_env env, double value); napi_value Convert2JSValue(napi_env env, bool value); napi_value Convert2JSValue(napi_env env, const std::map &value); +napi_value Convert2JSValue(napi_env env, const DistributedRdb::Statistic &statistic); +napi_value Convert2JSValue(napi_env env, const DistributedRdb::TableDetail &tableDetail); +napi_value Convert2JSValue(napi_env env, const DistributedRdb::TableDetails &tableDetails); +napi_value Convert2JSValue(napi_env env, const DistributedRdb::ProgressDetail &progressDetail); +napi_value Convert2JSValue(napi_env env, const DistributedRdb::Details &details); napi_value Convert2JSValue(napi_env env, const std::monostate &value); template diff --git a/relational_store/frameworks/js/napi/common/src/js_utils.cpp b/relational_store/frameworks/js/napi/common/src/js_utils.cpp index dd9c384ee537056ea55364921cc36e7958ac21df..aaaef0ed3f985a488bba9aaa94493022afa0519d 100644 --- a/relational_store/frameworks/js/napi/common/src/js_utils.cpp +++ b/relational_store/frameworks/js/napi/common/src/js_utils.cpp @@ -441,6 +441,82 @@ napi_value JSUtils::Convert2JSValue(napi_env env, const std::map rdbPredicates = nullptr; @@ -266,7 +269,21 @@ int ParseSyncModeArg(const napi_env &env, const napi_value &arg, std::shared_ptr napi_status status = napi_get_value_int32(env, arg, &context->enumArg); CHECK_RETURN_SET(status == napi_ok, std::make_shared("mode", "a SyncMode Type.")); bool checked = context->enumArg == 0 || context->enumArg == 1; - CHECK_RETURN_SET(checked, std::make_shared("mode", "a SyncMode.")); + CHECK_RETURN_SET(checked, std::make_shared("mode", "a SyncMode of device.")); + + LOG_DEBUG("ParseSyncModeArg end"); + return OK; +} + +int ParseCloudSyncModeArg(const napi_env &env, const napi_value &arg, std::shared_ptr context) +{ + napi_valuetype type = napi_undefined; + napi_typeof(env, arg, &type); + CHECK_RETURN_SET(type == napi_number, std::make_shared("mode", "a SyncMode Type.")); + napi_status status = napi_get_value_int32(env, arg, &context->enumArg); + CHECK_RETURN_SET(status == napi_ok, std::make_shared("mode", "a SyncMode Type.")); + bool checked = (context->enumArg > 1 && context->enumArg <= 4); + CHECK_RETURN_SET(checked, std::make_shared("mode", "a SyncMode of cloud.")); LOG_DEBUG("ParseSyncModeArg end"); return OK; @@ -1012,14 +1029,24 @@ napi_value RdbStoreProxy::SetDistributedTables(napi_env env, napi_callback_info LOG_DEBUG("RdbStoreProxy::SetDistributedTables start"); auto context = std::make_shared(); auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) { - CHECK_RETURN_SET_E(argc == 1, std::make_shared("1 or 2")); + CHECK_RETURN_SET_E(argc == 1 || argc == 2, std::make_shared("1 - 3")); CHECK_RETURN(OK == ParserThis(env, self, context)); CHECK_RETURN(OK == ParseTablesName(env, argv[0], context)); + if (argc == 2) { + napi_valuetype type = napi_undefined; + napi_typeof(env, argv[1], &type); + CHECK_RETURN_SET_E(type == napi_number && napi_get_value_int32(env, argv[1], &context->enumArg) == napi_ok, + std::make_shared("mode", "a SyncMode Type.")); + bool checked = context->enumArg == 0 || context->enumArg == 1; + CHECK_RETURN_SET_E(checked, std::make_shared("mode", "a SyncMode of device.")); + } else { + context->enumArg = 0; + } }; auto exec = [context]() -> int { LOG_DEBUG("RdbStoreProxy::SetDistributedTables Async"); RdbStoreProxy *obj = reinterpret_cast(context->boundObj); - return obj->rdbStore_->SetDistributedTables(context->tablesNames); + return obj->rdbStore_->SetDistributedTables(context->tablesNames, context->enumArg); }; auto output = [context](napi_env env, napi_value &result) { napi_status status = napi_get_undefined(env, &result); @@ -1093,7 +1120,47 @@ napi_value RdbStoreProxy::Sync(napi_env env, napi_callback_info info) napi_value RdbStoreProxy::CloudSync(napi_env env, napi_callback_info info) { - return nullptr; + LOG_DEBUG("RdbStoreProxy::CloudSync start"); + auto context = std::make_shared(); + auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) { + CHECK_RETURN_SET_E(argc == 2 || argc == 3, std::make_shared("2 - 4")); + CHECK_RETURN(OK == ParserThis(env, self, context)); + CHECK_RETURN(OK == ParseCloudSyncModeArg(env, argv[0], context)); + uint32_t index = 1; + bool isArray = false; + napi_is_array(env, argv[index], &isArray); + if (isArray) { + CHECK_RETURN(OK == ParseTablesName(env, argv[index], context)); + index++; + } + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv[index], &valueType); + CHECK_RETURN_SET_E(valueType == napi_function, std::make_shared("a callback type")); + context->cloudSyncCallback = argv[index]; + }; + auto exec = [context]() -> int { + LOG_DEBUG("RdbStoreProxy::CloudSync Async"); + auto *obj = reinterpret_cast(context->boundObj); + SyncOption option; + option.mode = static_cast(context->enumArg); + option.isBlock = true; + + return obj->rdbStore_->Sync(option, context->tablesNames, [context](const Details &details) { + auto callback = std::make_shared(context->env_, context->cloudSyncCallback); + callback->OnSyncCompelete(details); + }); + }; + + auto output = [context](napi_env env, napi_value &result) { + LOG_DEBUG("RdbStoreProxy::CloudSync output"); + napi_status status = napi_get_undefined(env, &result); + CHECK_RETURN_SET_E(status == napi_ok, std::make_shared(E_ERROR)); + }; + + context->SetAction(env, info, input, exec, output); + + CHECK_RETURN_NULL(context->error == nullptr || context->error->GetCode() == OK); + return AsyncCall::Call(env, context); } napi_value RdbStoreProxy::OnDataChangeEvent(napi_env env, size_t argc, napi_value *argv) @@ -1193,6 +1260,15 @@ napi_value RdbStoreProxy::OffEvent(napi_env env, napi_callback_info info) } return nullptr; } + +void RdbStoreProxy::NapiCoudSyncCallback::OnSyncCompelete(const DistributedRdb::Details &details) +{ + LOG_DEBUG("NapiCoudSyncCallback::OnSyncCompelete begin"); + CallFunction([details](napi_env env, int &argc, napi_value *argv) { + argc = 1; + argv[0] = details.empty() ? nullptr : JSUtils::Convert2JSValue(env, details.begin()->second); + }); +} #endif } // namespace RelationalStoreJsKit } // namespace OHOS 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 cb6712cd66829c074a87955dc819a9322f0afc93..4f8a91c2292348091654cb84e0a37f47f14d208a 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h +++ b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h @@ -34,10 +34,12 @@ public: std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; int32_t InitNotifier(const RdbSyncerParam ¶m); + int32_t InitNotifier(const RdbSyncerParam ¶m, sptr notifier) override; - - int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector& tables) override; - + + int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables, + int32_t type = DISTRIBUTED_DEVICE) override; + int32_t Sync(const RdbSyncerParam& param, const Option& option, const RdbPredicates& predicates, const AsyncDetail &async) override; diff --git a/relational_store/frameworks/native/rdb/include/rdb_store_impl.h b/relational_store/frameworks/native/rdb/include/rdb_store_impl.h index 2c579e85a3045cd1ceb83d71d1b6d605ca10213d..5d7df88e1aee136e3ed0679dc929b5fecd5eb901 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_store_impl.h +++ b/relational_store/frameworks/native/rdb/include/rdb_store_impl.h @@ -28,6 +28,9 @@ #include "sqlite_connection_pool.h" #include "sqlite_statement.h" #include "transaction_observer.h" +namespace OHOS { +class ExecutorPool; +} namespace OHOS::NativeRdb { class RdbStoreImpl : public RdbStore, public std::enable_shared_from_this { public: @@ -99,7 +102,7 @@ public: std::shared_ptr RemoteQuery(const std::string &device, const AbsRdbPredicates &predicates, const std::vector &columns, int &errCode) override; - int SetDistributedTables(const std::vector& tables) override; + int SetDistributedTables(const std::vector& tables, int32_t type) override; std::string ObtainDistributedTableName(const std::string& device, const std::string& table, int &errCode) override; @@ -136,7 +139,6 @@ private: std::string fileType; DistributedRdb::RdbSyncerParam syncerParam_; bool isEncrypt_; - ExecutorPool::TaskId taskId_ = ExecutorPool::INVALID_TASK_ID; std::shared_ptr pool_; }; } // namespace OHOS::NativeRdb diff --git a/relational_store/frameworks/native/rdb/mock/include/task_executor.h b/relational_store/frameworks/native/rdb/mock/include/task_executor.h index ebd6b51aa3b637b2fae74b5b88585da74444acb4..9ed654833209d3933e245698a7f20b7ab706b273 100644 --- a/relational_store/frameworks/native/rdb/mock/include/task_executor.h +++ b/relational_store/frameworks/native/rdb/mock/include/task_executor.h @@ -23,9 +23,7 @@ public: using TaskId = uint64_t; using Task = std::function; using Duration = std::chrono::steady_clock::duration; - static constexpr Duration INVALID_DURATION = std::chrono::milliseconds(0); static constexpr TaskId INVALID_TASK_ID = static_cast(0l); - static constexpr uint64_t UNLIMITED_TIMES = std::numeric_limits::max(); class ExecutorPool { public: ExecutorPool(size_t max, size_t min) diff --git a/relational_store/frameworks/native/rdb/mock/src/timer.cpp b/relational_store/frameworks/native/rdb/mock/src/timer.cpp deleted file mode 100644 index 95b46f26710d5539c7b4e0dcc9c8cc4bc104e186..0000000000000000000000000000000000000000 --- a/relational_store/frameworks/native/rdb/mock/src/timer.cpp +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2021 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 "timer.h" - -namespace OHOS { -namespace Utils { - -Timer::Timer(const std::string& name, int timeoutMs) : name_(name), timeoutMs_(timeoutMs), - reactor_(nullptr) -{ -} - -uint32_t Timer::Setup() -{ - name_ = ""; - reactor_ = nullptr; - return 0; -} - -void Timer::Shutdown(bool useJoin) -{ -} - -uint32_t Timer::Register(const TimerCallback& callback, uint32_t interval /* ms */, bool once) -{ - return 0; -} - -void Timer::Unregister(uint32_t timerId) -{ - return; -} - -void Timer::MainLoop() -{ -} - -uint32_t Timer::DoRegister(const TimerListCallback& callback, uint32_t interval, bool once, int &timerFd) -{ - return 0; -} - -void Timer::DoUnregister(uint32_t interval) -{ -} - -void Timer::OnTimer(int timerFd) -{ -} - -void Timer::DoTimerListCallback(const TimerListCallback& callback, int timerFd) -{ -} - -/* valid range: [1, UINT32_MAX], but not TIMER_ERR_DEAL_FAILED */ -uint32_t Timer::GetValidId(uint32_t timerId) const -{ - return 0; -} - -int Timer::GetTimerFd(uint32_t interval /* ms */) -{ - return timeoutMs_; -} - -void Timer::EraseUnusedTimerId(uint32_t interval, const std::vector& unusedIds) -{ -} - -} // namespace Utils -} // namespace OHOS 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 4286211ac4998c35fc84deae67f778172a7cdd88..7a2e1a2e89a4c14d1dd58c88e05485c2d1f5d7cf 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp @@ -196,7 +196,7 @@ int32_t RdbServiceProxy::DoAsync(const RdbSyncerParam& param, const Option &opti return RDB_OK; } -int32_t RdbServiceProxy::SetDistributedTables(const RdbSyncerParam& param, const std::vector &tables) +int32_t RdbServiceProxy::SetDistributedTables(const RdbSyncerParam& param, const std::vector &tables, int32_t type) { MessageParcel reply; int32_t status = IPC_SEND(RDB_SERVICE_CMD_SET_DIST_TABLE, reply, param, tables); @@ -328,8 +328,22 @@ void RdbServiceProxy::ImportObservers(ObserverMap &observers) int32_t RdbServiceProxy::GetSchema(const RdbSyncerParam ¶m) { + MessageParcel request; + if (!request.WriteInterfaceToken(GetDescriptor())) { + return RDB_ERROR; + } + if (!ITypesUtil::Marshal(request, param)) { + return RDB_ERROR; + } MessageParcel reply; - int32_t status = IPC_SEND(RDB_SERVICE_CMD_GET_SCHEMA, reply, param); + MessageOption option; + option.SetFlags(option.TF_ASYNC); + auto result = remote_->SendRequest((RDB_SERVICE_CMD_GET_SCHEMA), request, reply, option); + if (result != 0) { + return RDB_ERROR; + } + int32_t status = RDB_OK; + ITypesUtil::Unmarshal(reply, status); if (status != RDB_OK) { ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", status, param.bundleName_.c_str(), param.storeName_.c_str()); 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 7db7459e6e05d118102b3b82909ee70fb2c25d39..4566f94de6f453f35403b7a5dc4277abcf9cfd7d 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -97,15 +97,16 @@ int RdbStoreImpl::InnerOpen(const RdbStoreConfig &config) pool_ = TaskExecutor::GetInstance().GetExecutor(); } if (pool_ != nullptr) { - taskId_ = pool_->Execute([this]() { - auto [errCode, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_); - if (errCode != E_OK || service == nullptr) { - LOG_ERROR("GetRdbService failed, err is %{public}d.", errCode); + auto param = syncerParam_; + pool_->Execute([param]() { + auto [err, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(param); + if (err != E_OK || service == nullptr) { + LOG_WARN("GetRdbService failed, err is %{public}d.", err); return; } - errCode = service->GetSchema(syncerParam_); - if (errCode != E_OK) { - LOG_ERROR("GetSchema failed, err is %{public}d.", errCode); + err = service->GetSchema(param); + if (err != E_OK) { + LOG_ERROR("GetSchema failed, err is %{public}d.", err); } }); } @@ -122,9 +123,6 @@ RdbStoreImpl::RdbStoreImpl(const RdbStoreConfig &config) RdbStoreImpl::~RdbStoreImpl() { delete connectionPool; - if (pool_ == nullptr && taskId_ != ExecutorPool::INVALID_TASK_ID) { - pool_->Remove(taskId_, true); - } } #ifdef WINDOWS_PLATFORM @@ -1121,8 +1119,8 @@ std::shared_ptr RdbStoreImpl::QueryByStep(const std::string &sql, std return std::make_shared(connectionPool, sql, std::move(args)); } -#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) -int RdbStoreImpl::SetDistributedTables(const std::vector &tables) +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) +int RdbStoreImpl::SetDistributedTables(const std::vector &tables, int32_t type) { DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__)); if (tables.empty()) { @@ -1143,7 +1141,7 @@ int RdbStoreImpl::SetDistributedTables(const std::vector &tables) if (errCode != E_OK) { return errCode; } - int32_t errorCode = service->SetDistributedTables(syncerParam_, tables); + int32_t errorCode = service->SetDistributedTables(syncerParam_, tables, type); if (errorCode != E_OK) { LOG_ERROR("Fail to set distributed tables, error=%{public}d", errorCode); syncerParam_.password_.assign(syncerParam_.password_.size(), 0); 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 689631eaf386f8c79e4ba3e62fc7297c93ac3687..f10acceac73468766b79032f70f7d76023c4766a 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp @@ -85,18 +85,18 @@ bool Marshalling(const ValueObject &input, MessageParcel &data) { return Marshal(data, input.value); } -template<> +template<> bool Unmarshalling(ValueObject &output, MessageParcel &data) { return Unmarshal(data, output.value); } -template<> +template<> bool Marshalling(const ValuesBucket &input, MessageParcel &data) { return Marshal(data, input.values_); } -template<> +template<> bool Unmarshalling(ValuesBucket &output, MessageParcel &data) { return Unmarshal(data, output.values_); diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_service.h b/relational_store/interfaces/inner_api/rdb/include/rdb_service.h index 94ab415ef0dbed316697833d55ab2901fd2a26ac..91dde3bfc20023fbbed09d8059bc4fae2f8a9672 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_service.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_service.h @@ -49,7 +49,8 @@ public: virtual std::string ObtainDistributedTableName(const std::string &device, const std::string &table) = 0; - virtual int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables) = 0; + virtual int32_t SetDistributedTables( + const RdbSyncerParam ¶m, const std::vector &tables, int32_t type = DISTRIBUTED_DEVICE) = 0; virtual int32_t Sync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &predicates, const AsyncDetail &async) = 0; diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_store.h b/relational_store/interfaces/inner_api/rdb/include/rdb_store.h index 42e74f72800f1cc5af516658bb01618be034f26d..513b1016d0bc917b9829dd4401dcc2f9b3de3e79 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_store.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_store.h @@ -385,7 +385,8 @@ public: * * @param tables Indicates the tables name you want to set. */ - virtual int SetDistributedTables(const std::vector& tables) = 0; + virtual int SetDistributedTables(const std::vector &tables, + int32_t type = DistributedRdb::DistributedTableType::DISTRIBUTED_DEVICE) = 0; /** * @brief Obtain distributed table name of specified remote device according to local table name. diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_types.h b/relational_store/interfaces/inner_api/rdb/include/rdb_types.h index b4f1b9cd5ce3fab561387676af9dcaba2031b917..373e29326b97001f488dc8dfcc618f179e7461b2 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_types.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_types.h @@ -65,6 +65,11 @@ struct SyncOption { bool isBlock; }; +enum DistributedTableType { + DISTRIBUTED_DEVICE = 0, + DISTRIBUTED_CLOUD +}; + enum Progress { SYNC_BEGIN, SYNC_IN_PROGRESS, @@ -83,14 +88,15 @@ struct TableDetail { Statistic download; }; +using TableDetails = std::map; + struct ProgressDetail { int32_t progress; int32_t code; - std::map details; + TableDetails details; }; using SyncResult = std::map; -using SyncCallback = std::function; using Briefs = std::map; using Details = std::map; using AsyncBrief = std::function;