From af00d6f6c6bdb73acbf9d8e1da15861cdcef4fd3 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Mon, 5 Jun 2023 17:12:43 +0800 Subject: [PATCH] update Signed-off-by: htt1997 --- .../framework/cloud/schema_meta.cpp | 10 ++ .../framework/include/cloud/schema_meta.h | 2 +- .../framework/include/store/general_store.h | 2 +- .../service/cloud/cloud_service_impl.cpp | 20 ++-- .../service/cloud/sync_manager.cpp | 11 +- .../service/cloud/sync_manager.h | 2 +- .../permission/src/permit_delegate.cpp | 2 +- .../service/rdb/rdb_general_store.cpp | 2 +- .../service/rdb/rdb_notifier_proxy.cpp | 42 +++---- .../service/rdb/rdb_notifier_proxy.h | 2 - .../service/rdb/rdb_service_impl.cpp | 34 +++--- .../service/rdb/rdb_service_impl.h | 2 +- .../service/rdb/rdb_service_stub.cpp | 5 +- .../service/rdb/rdb_syncer.cpp | 40 ++++--- .../service/rdb/rdb_syncer.h | 20 ++-- .../service/rdb/rdb_watcher.cpp | 2 +- .../service/rdb/value_proxy.cpp | 4 +- .../service/rdb/value_proxy.h | 6 +- .../service/test/cloud_data_test.cpp | 4 +- .../service/test/value_proxy_test.cpp | 107 ++++++++++++++++++ .../distributedkvstore/src/js_util.cpp | 2 +- .../js/napi/cloud_data/src/js_error_utils.cpp | 2 +- .../include/napi_rdb_store_observer.h | 8 +- .../src/napi_rdb_const_properties.cpp | 1 + .../relationalstore/src/napi_rdb_store.cpp | 23 +++- .../src/napi_rdb_store_observer.cpp | 14 ++- .../native/rdb/include/rdb_notifier_stub.h | 5 +- .../native/rdb/include/rdb_security_manager.h | 2 - .../native/rdb/include/rdb_service_proxy.h | 4 +- .../native/rdb/include/rdb_store_impl.h | 1 + .../native/rdb/include/rdb_types_util.h | 13 +++ .../native/rdb/src/rdb_notifier_stub.cpp | 31 ++--- .../native/rdb/src/rdb_security_manager.cpp | 33 ------ .../native/rdb/src/rdb_service_proxy.cpp | 52 +++++---- .../native/rdb/src/rdb_store_impl.cpp | 57 +++++----- .../native/rdb/src/rdb_types_util.cpp | 24 ++++ .../inner_api/rdb/include/rdb_notifier.h | 3 - .../inner_api/rdb/include/rdb_types.h | 13 ++- .../rdb/mock/include/abs_rdb_predicates.h | 4 +- .../src/RdbStoreDistributedJsunit.test.js | 3 +- .../src/RdbStoreDistributedJsunit.test.js | 1 - .../distributed_test_agent.cpp | 2 +- .../rdbimpl_fuzzer/rdbimpl_fuzzer.cpp | 2 +- .../rdb/unittest/rdb_encrypt_decrypt_test.cpp | 36 ------ .../rdb/unittest/rdb_store_subscribe_test.cpp | 34 +++--- 45 files changed, 402 insertions(+), 287 deletions(-) create mode 100644 datamgr_service/services/distributeddataservice/service/test/value_proxy_test.cpp diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/schema_meta.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/schema_meta.cpp index 8dd3005e..af5ec17a 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/schema_meta.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/schema_meta.cpp @@ -31,6 +31,16 @@ bool SchemaMeta::Unmarshal(const Serializable::json &node) return true; } +std::vector Database::GetTableNames() const +{ + std::vector tableNames; + tableNames.reserve(tables.size()); + for (auto &table : tables) { + tableNames.push_back(table.name); + } + return tableNames; +} + bool Database::Marshal(Serializable::json &node) const { SetValue(node[GET_NAME(name)], name); diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/schema_meta.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/schema_meta.h index f02e34ac..49c15e18 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/schema_meta.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/schema_meta.h @@ -39,7 +39,7 @@ struct API_EXPORT Database final : public Serializable { std::string name = ""; std::string alias; std::vector tables; - + std::vector GetTableNames() const; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; }; diff --git a/datamgr_service/services/distributeddataservice/framework/include/store/general_store.h b/datamgr_service/services/distributeddataservice/framework/include/store/general_store.h index 46b748d9..b7a3775b 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/store/general_store.h +++ b/datamgr_service/services/distributeddataservice/framework/include/store/general_store.h @@ -24,7 +24,7 @@ namespace OHOS::DistributedData { class CloudDB; class AssetLoader; -class Database; +struct Database; class GeneralStore { public: using Watcher = GeneralWatcher; 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 3b454540..c7722dd3 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -72,7 +72,7 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map actions; + for (auto &[bundle, app] : cloudInfo.apps) { + actions[bundle] = CLEAR_CLOUD_INFO; } - oldInfo.apps[bundle] = std::move(app); + Clean(oldInfo.id, actions); } - MetaDataManager::GetInstance().SaveMeta(oldInfo.GetKey(), oldInfo, true); + + MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); return true; } @@ -322,10 +318,10 @@ CloudServiceImpl::Tasks CloudServiceImpl::GetCloudTask( { Tasks tasks; tasks.reserve(works.size() + 1); - tasks.push_back(GenTask(retry, user, WORK_SUB)); for (auto work : works) { tasks.push_back(GenTask(retry, user, work)); } + tasks.push_back(GenTask(retry, user, WORK_SUB)); return tasks; } diff --git a/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.cpp b/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.cpp index 84aa248b..f55515a7 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -80,7 +80,7 @@ void SyncManager::SyncInfo::SetError(int32_t code) } } -std::shared_ptr SyncManager::SyncInfo::GenerateQuery(const std::string &store) +std::shared_ptr SyncManager::SyncInfo::GenerateQuery(const std::string &store, const Tables &tables) { if (query_ != nullptr) { return query_; @@ -101,7 +101,8 @@ std::shared_ptr SyncManager::SyncInfo::GenerateQuery(const std::string private: std::vector tables_; }; - return std::make_shared(tables_[store]); + auto &syncTables = tables_[store]; + return std::make_shared(syncTables.empty() ? tables : syncTables); } SyncManager::SyncManager() @@ -190,8 +191,9 @@ ExecutorPool::Task SyncManager::GetSyncTask(int32_t retry, RefCount ref, SyncInf storeInfo.bundleName = schema.bundleName; storeInfo.user = cloud.user; storeInfo.storeName = database.name; + auto query = info.GenerateQuery(database.name, database.GetTableNames()); auto evt = std::make_unique(std::move(storeInfo), - SyncEvent::EventInfo{ info.mode_, info.wait_, info.GenerateQuery(database.name), info.async_ }); + SyncEvent::EventInfo{ info.mode_, info.wait_, std::move(query), info.async_ }); EventCenter::GetInstance().PostEvent(std::move(evt)); } } @@ -247,7 +249,7 @@ std::function SyncManager::GetSyncHandler() } ZLOGD("database:<%{public}d:%{public}s:%{public}s> sync start", storeInfo.user, storeInfo.bundleName.c_str(), storeInfo.storeName.c_str()); - store->Sync({}, evt.GetMode(), *(evt.GetQuery()), evt.GetAsyncDetail(), evt.GetWait()); + store->Sync({ "default" }, evt.GetMode(), *(evt.GetQuery()), evt.GetAsyncDetail(), evt.GetWait()); }; } @@ -260,6 +262,7 @@ std::function SyncManager::GetClientChangeHandler() syncInfo.SetMode(evt.GetMode()); syncInfo.SetWait(evt.GetWait()); syncInfo.SetAsyncDetail(evt.GetAsyncDetail()); + syncInfo.SetQuery(evt.GetQuery()); auto task = GetSyncTask(RETRY_TIMES, RefCount(), std::move(syncInfo)); task(); }; diff --git a/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.h b/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.h index fe28a15a..a7333898 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.h @@ -42,7 +42,7 @@ public: void SetAsyncDetail(GenAsync asyncDetail); void SetQuery(std::shared_ptr query); void SetError(int32_t code); - std::shared_ptr GenerateQuery(const std::string &store); + std::shared_ptr GenerateQuery(const std::string &store, const Tables &tables); private: friend SyncManager; diff --git a/datamgr_service/services/distributeddataservice/service/permission/src/permit_delegate.cpp b/datamgr_service/services/distributeddataservice/service/permission/src/permit_delegate.cpp index 7a4bd29e..52b9af7c 100644 --- a/datamgr_service/services/distributeddataservice/service/permission/src/permit_delegate.cpp +++ b/datamgr_service/services/distributeddataservice/service/permission/src/permit_delegate.cpp @@ -79,7 +79,7 @@ bool PermitDelegate::SyncActivate(const ActiveParam ¶m) bool PermitDelegate::VerifyPermission(const CheckParam ¶m, uint8_t flag) { ZLOGI("user:%{public}s, appId:%{public}s, storeId:%{public}s, remote devId:%{public}s, instanceId:%{public}d," - "flag:%{public}u", param.userId.c_str(), param.appId.c_str(), param.storeId.c_str(), + "flag:%{public}u", param.userId.c_str(), param.appId.c_str(), Anonymous::Change(param.storeId).c_str(), Anonymous::Change(param.deviceId).c_str(), param.instanceId, flag); auto devId = DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp index f4ce8ebd..c69cb9f7 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -71,7 +71,7 @@ RdbGeneralStore::RdbGeneralStore(const StoreMetaData &meta) : manager_(meta.appI config.SetCreateNecessary(false); RdbOpenCallbackImpl callback; int32_t errCode = NativeRdb::E_OK; - store_ = RdbHelper::GetRdbStore(config, meta.version, callback, errCode); + store_ = RdbHelper::GetRdbStore(config, -1, callback, errCode); if (errCode != NativeRdb::E_OK) { ZLOGE("GetRdbStore failed, errCode is %{public}d, storeId is %{public}s", errCode, meta.storeId.c_str()); } diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.cpp index 6adca014..d2e1abfe 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.cpp @@ -47,14 +47,14 @@ int32_t RdbNotifierProxy::OnComplete(uint32_t seqNum, Details &&result) return RDB_OK; } -int RdbNotifierProxy::OnChange(const std::string &storeName, const std::vector &devices) +int32_t RdbNotifierProxy::OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo) { MessageParcel data; if (!data.WriteInterfaceToken(GetDescriptor())) { ZLOGE("write descriptor failed"); return RDB_ERROR; } - if (!ITypesUtil::Marshal(data, storeName, devices)) { + if (!ITypesUtil::Marshal(data, origin, primaries, changeInfo)) { ZLOGE("write store name or devices failed"); return RDB_ERROR; } @@ -67,25 +67,25 @@ int RdbNotifierProxy::OnChange(const std::string &storeName, const std::vectorSendRequest(RDB_NOTIFIER_CMD_DATA_DETAILS, data, reply, option) != 0) { - ZLOGE("send request failed"); - return RDB_ERROR; - } - return RDB_OK; -} +//// if (!ITypesUtil::Marshal(data, origin, primaries, changeInfo)) { +//// ZLOGE("write store name or devices failed"); +//// return RDB_ERROR; +//// } +// +// MessageParcel reply; +// MessageOption option(MessageOption::TF_ASYNC); +// if (Remote()->SendRequest(RDB_NOTIFIER_CMD_DATA_DETAILS, data, reply, option) != 0) { +// ZLOGE("send request failed"); +// return RDB_ERROR; +// } +// return RDB_OK; +//} } // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.h index 20b05f32..991fcae4 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.h @@ -31,8 +31,6 @@ public: int32_t OnComplete(uint32_t seqNum, Details &&result) override; - int32_t OnChange(const std::string& storeName, const std::vector& devices) override; - int32_t OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo) override; private: 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 88cad34f..6ada9e00 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -27,7 +27,6 @@ #include "metadata/appid_meta_data.h" #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" -#include "permission/permission_validator.h" #include "rdb_watcher.h" #include "rdb_notifier_proxy.h" #include "rdb_query.h" @@ -85,14 +84,14 @@ RdbServiceImpl::RdbServiceImpl() : autoLaunchObserver_(this) meta.instanceId = storeInfo.instanceId; meta.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta)) { - ZLOGE("meta empty, bundleName:%{public}s, storeId:%{public}s", - meta.bundleName.c_str(), meta.storeId.c_str()); + ZLOGE("meta empty, bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), + Anonymous::Change(meta.storeId).c_str()); return; } auto watchers = GetWatchers(meta.tokenId, meta.storeId); auto store = AutoCache::GetInstance().GetStore(meta, watchers); if (store == nullptr) { - ZLOGE("store null, storeId:%{public}s", meta.storeId.c_str()); + ZLOGE("store null, storeId:%{public}s", Anonymous::Change(meta.storeId).c_str()); return; } }; @@ -234,6 +233,11 @@ void RdbServiceImpl::OnDataChange(pid_t pid, uint32_t tokenId, const Distributed if (success && agent.notifier_ != nullptr && pid == agent.pid_) { std::string device = data.GetDataChangeDevice(); auto networkId = DmAdapter::GetInstance().ToNetworkID(device); + //Origin origin; + //origin.origin = Origin::ORIGIN_NEARBY; + //origin.store = property.storeId; + //origin.id.push_back(networkId); + //agent.notifier_->OnChange(origin, {}, {}); agent.notifier_->OnChange(property.storeId, { networkId }); } } @@ -331,7 +335,7 @@ std::pair RdbServiceImpl::DoSync(const RdbSyncerParam ¶m, if (syncer == nullptr) { return {RDB_ERROR, {}}; } - Details details; + Details details = {}; auto status = syncer->DoSync(option, pred, [&details](auto &&result) mutable { details = std::move(result); }); return { status, std::move(details) }; } @@ -393,6 +397,9 @@ std::string RdbServiceImpl::GenIdentifier(const RdbSyncerParam ¶m) AutoCache::Watchers RdbServiceImpl::GetWatchers(uint32_t tokenId, const std::string &storeName) { auto [success, agent] = syncAgents_.Find(tokenId); + if (agent.watcher_ == nullptr) { + return {}; + } return { agent.watcher_ }; } @@ -457,8 +464,9 @@ int32_t RdbServiceImpl::Subscribe(const RdbSyncerParam ¶m, const SubscribeOp switch (option.mode) { case SubscribeMode::REMOTE: { auto identifier = GenIdentifier(param); - identifiers_.Insert(identifier, std::pair{ pid, tokenId }); - ZLOGI("%{public}s %{public}.6s %{public}d", param.storeName_.c_str(), identifier.c_str(), pid); + identifiers_.Insert(identifier, std::pair { pid, tokenId }); + ZLOGI("%{public}s %{public}.6s %{public}d", Anonymous::Change(param.storeName_).c_str(), + identifier.c_str(), pid); break; } case SubscribeMode::CLOUD: // fallthrough @@ -562,16 +570,16 @@ int32_t RdbServiceImpl::CreateMetaData(const RdbSyncerParam ¶m, StoreMetaDat 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); + meta.bundleName.c_str(), Anonymous::Change(meta.storeId).c_str(), old.storeType, meta.storeType, + old.isEncrypt, meta.isEncrypt, old.area, meta.area); 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); + meta.bundleName.c_str(), Anonymous::Change(meta.storeId).c_str(), old.storeType, meta.storeType, + old.isEncrypt, meta.isEncrypt, old.area, meta.area); MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta); } AppIDMetaData appIdMeta; @@ -580,8 +588,8 @@ int32_t RdbServiceImpl::CreateMetaData(const RdbSyncerParam ¶m, StoreMetaDat 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); + meta.bundleName.c_str(), Anonymous::Change(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()) { 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 f527997c..36d2041c 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -123,7 +123,7 @@ private: static Factory factory_; int32_t syncerNum_ {}; ConcurrentMap syncers_; - ConcurrentMap> identifiers_; + ConcurrentMap> identifiers_;//identifier,pid,tokenid ConcurrentMap syncAgents_; RdbStoreObserverImpl autoLaunchObserver_; std::shared_ptr executors_; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index 64fd4c85..87ec6b42 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -102,7 +102,7 @@ int32_t RdbServiceStub::OnRemoteDoSync(MessageParcel &data, MessageParcel &reply return IPC_STUB_INVALID_DATA_ERR; } - Details result; + Details result = {}; auto status = Sync(param, option, predicates, [&result](Details &&details) { result = std::move(details); }); if (!ITypesUtil::Marshal(reply, status, result)) { ZLOGE("Marshal status:0x%{public}x result size:%{public}zu", status, result.size()); @@ -118,7 +118,8 @@ int32_t RdbServiceStub::OnRemoteDoAsync(MessageParcel &data, MessageParcel &repl RdbPredicates predicates; if (!ITypesUtil::Unmarshal(data, param, option, predicates)) { ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s seqNum:%{public}u tables:%{public}s", - param.bundleName_.c_str(), param.storeName_.c_str(), option.seqNum, (*(predicates.tables_.begin())).c_str()); + param.bundleName_.c_str(), param.storeName_.c_str(), option.seqNum, + (*(predicates.tables_.begin())).c_str()); return IPC_STUB_INVALID_DATA_ERR; } auto status = Sync(param, option, predicates, nullptr); diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp index ab450245..ede1f514 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -23,21 +23,16 @@ #include "cloud/change_event.h" #include "crypto_manager.h" #include "device_manager_adapter.h" -#include "directory_manager.h" #include "eventcenter/event_center.h" -#include "kvstore_utils.h" #include "log_print.h" -#include "metadata/appid_meta_data.h" #include "metadata/meta_data_manager.h" -#include "metadata/store_meta_data.h" #include "rdb_query.h" #include "rdb_result_set_impl.h" #include "store/general_store.h" -#include "types.h" #include "types_export.h" +#include "utils/anonymous.h" #include "utils/constant.h" #include "utils/converter.h" -using OHOS::DistributedKv::KvStoreUtils; using OHOS::DistributedKv::AccountDelegate; using namespace OHOS::Security::AccessToken; using namespace OHOS::DistributedData; @@ -172,11 +167,11 @@ int32_t RdbSyncer::InitDBDelegate(const StoreMetaData &meta) } option.observer = observer_; std::string fileName = meta.dataDir; - ZLOGI("path=%{public}s storeId=%{public}s", fileName.c_str(), meta.storeId.c_str()); + ZLOGI("path=%{public}s storeId=%{public}s", fileName.c_str(), Anonymous::Change(meta.storeId).c_str()); auto status = manager_->OpenStore(fileName, meta.storeId, option, delegate_); if (status != DistributedDB::DBStatus::OK) { ZLOGE("open store failed, path=%{public}s storeId=%{public}s status=%{public}d", - fileName.c_str(), meta.storeId.c_str(), status); + fileName.c_str(), Anonymous::Change(meta.storeId).c_str(), status); return RDB_ERROR; } ZLOGI("open store success"); @@ -237,7 +232,7 @@ std::vector RdbSyncer::GetConnectDevices() } ZLOGI("size=%{public}u", static_cast(devices.size())); for (const auto& device: devices) { - ZLOGI("%{public}s", KvStoreUtils::ToBeAnonymous(device).c_str()); + ZLOGI("%{public}s", Anonymous::Change(device).c_str()); } return devices; } @@ -248,12 +243,11 @@ std::vector RdbSyncer::NetworkIdToUUID(const std::vector %{public}s", KvStoreUtils::ToBeAnonymous(networkId).c_str(), - KvStoreUtils::ToBeAnonymous(uuid).c_str()); + ZLOGI("%{public}s <--> %{public}s", Anonymous::Change(networkId).c_str(), Anonymous::Change(uuid).c_str()); } return uuids; } @@ -345,7 +339,8 @@ void RdbSyncer::Limit(const RdbPredicateOperation &operation, DistributedDB::Que DistributedDB::Query RdbSyncer::MakeQuery(const RdbPredicates &predicates) { ZLOGI("table=%{public}zu", predicates.tables_.size()); - auto query = predicates.tables_.size() == 1 ? DistributedDB::Query::Select(*predicates.tables_.begin()) : DistributedDB::Query::Select(); + auto query = predicates.tables_.size() == 1 ? DistributedDB::Query::Select(*predicates.tables_.begin()) + : DistributedDB::Query::Select(); if (predicates.tables_.size() > 1) { query.FromTable(predicates.tables_); } @@ -374,15 +369,26 @@ int32_t RdbSyncer::DoSync(const Option &option, const RdbPredicates &predicates, async(HandleSyncStatus(syncStatus)); }, option.isAsync); - } else if (option.mode < DistributedData::GeneralStore::CLOUD_END) { + Statistic upload = { 10, 8, 1, 0 }; + Statistic download = { 10, 5, 4, 1 }; + TableDetail tableDetail = { upload, download }; + TableDetails tableDetails = { { "table", tableDetail } }; + ProgressDetail progressDetail = { SYNC_FINISH, 200, tableDetails }; + Details details = { { "store", progressDetail } }; + async(std::move(details)); + return RDB_OK; CloudEvent::StoreInfo storeInfo; storeInfo.bundleName = GetBundleName(); storeInfo.user = AccountDelegate::GetInstance()->GetUserByToken(token_); storeInfo.storeName = GetStoreId(); storeInfo.tokenId = token_; - auto query = std::make_shared(); - query->query_ = MakeQuery(predicates); + std::shared_ptr query = nullptr; + if (!predicates.tables_.empty()) { + query = std::make_shared(); + query->query_.FromTable(predicates.tables_); + } + auto info = ChangeEvent::EventInfo(option.mode, (option.isAsync ? 0 : WAIT_TIME), query, [async](const GenDetails &details) { async(HandleGenDetails(details)); @@ -429,7 +435,7 @@ int32_t RdbSyncer::RemoveDeviceData() } DistributedDB::DBStatus status = delegate->RemoveDeviceData(); if (status != DistributedDB::DBStatus::OK) { - ZLOGE("DistributedDB RemoveDeviceData failed, status is %{public}d.", status); + ZLOGE("DistributedDB RemoveDeviceData failed, status is %{public}d.", status); return RDB_ERROR; } return RDB_OK; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h index e3c6cc13..8f09fb34 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -77,20 +77,20 @@ private: int32_t InitDBDelegate(const StoreMetaData &meta); - DistributedDB::RelationalStoreDelegate *GetDelegate(); + DistributedDB::RelationalStoreDelegate* GetDelegate(); std::mutex mutex_; - DistributedDB::RelationalStoreManager *manager_{}; - DistributedDB::RelationalStoreDelegate *delegate_{}; + DistributedDB::RelationalStoreManager* manager_ {}; + DistributedDB::RelationalStoreDelegate* delegate_ {}; RdbSyncerParam param_; - RdbStoreObserverImpl *observer_{}; - pid_t pid_{}; - pid_t uid_{}; - uint32_t token_{}; - uint64_t timerId_{}; + RdbStoreObserverImpl *observer_ {}; + pid_t pid_ {}; + pid_t uid_ {}; + uint32_t token_ {}; + uint64_t timerId_ {}; static std::vector GetConnectDevices(); - static std::vector NetworkIdToUUID(const std::vector &networkIds); + static std::vector NetworkIdToUUID(const std::vector& networkIds); static Details HandleSyncStatus(const std::map> &SyncStatus); static Details HandleGenDetails(const GenDetails &details); @@ -101,7 +101,7 @@ private: static void OrderBy(const RdbPredicateOperation &operation, DistributedDB::Query &query); static void Limit(const RdbPredicateOperation &operation, DistributedDB::Query &query); - using PredicateHandle = void (*)(const RdbPredicateOperation &operation, DistributedDB::Query &query); + using PredicateHandle = void(*)(const RdbPredicateOperation& operation, DistributedDB::Query& query); static inline PredicateHandle HANDLES[OPERATOR_MAX] = { [EQUAL_TO] = &RdbSyncer::EqualTo, [NOT_EQUAL_TO] = &RdbSyncer::NotEqualTo, diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_watcher.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_watcher.cpp index c929a769..1294f292 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_watcher.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_watcher.cpp @@ -37,7 +37,7 @@ int32_t RdbWatcher::OnChange(const Origin &origin, const PRIFields &primaryField rdbOrigin.origin = origin.origin; rdbOrigin.id = origin.id; rdbOrigin.store = origin.store; - notifier->OnChange(rdbOrigin, primaryFields, std::move(values)); + // notifier->OnChange(rdbOrigin, primaryFields, std::move(values)) return E_OK; } diff --git a/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.cpp b/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.cpp index 7035d9fa..fbd7c5f9 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.cpp @@ -304,7 +304,7 @@ ValueProxy::Values &ValueProxy::Values::operator=(ValueProxy::Values &&values) n ValueProxy::Bucket &ValueProxy::Bucket::operator=(Bucket &&bucket) noexcept { - if (this != &bucket) { + if (this == &bucket) { return *this; } value_ = std::move(bucket.value_); @@ -323,7 +323,7 @@ ValueProxy::Bucket::operator NativeRdb::ValuesBucket() ValueProxy::Buckets &ValueProxy::Buckets::operator=(Buckets &&buckets) noexcept { - if (this != &buckets) { + if (this == &buckets) { return *this; } value_ = std::move(buckets.value_); diff --git a/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.h b/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.h index 3b9eeecf..8506e268 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.h @@ -163,16 +163,16 @@ public: }; static Value Convert(DistributedData::Value &&value); static Value Convert(NativeRdb::ValueObject &&value); + static Value Convert(DistributedDB::Type &&value); static Values Convert(DistributedData::Values &&values); static Values Convert(std::vector &&values); static Bucket Convert(DistributedData::VBucket &&bucket); static Bucket Convert(NativeRdb::ValuesBucket &&bucket); + static Bucket Convert(DistributedDB::VBucket &&bucket); static Buckets Convert(DistributedData::VBuckets &&buckets); static Buckets Convert(std::vector &&buckets); - - static Value Convert(DistributedDB::Type &&value); - static Bucket Convert(DistributedDB::VBucket &&bucket); static Buckets Convert(std::vector &&buckets); + private: ValueProxy() = delete; ~ValueProxy() = delete; diff --git a/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp b/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp index 09c13d8d..11b16799 100644 --- a/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -33,7 +33,7 @@ using namespace testing::ext; using namespace OHOS::DistributedData; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; -namespace OHOS { +namespace OHOS::Test { namespace DistributedDataTest { static constexpr const char *TEST_CLOUD_BUNDLE = "test_cloud_bundleName"; static constexpr const char *TEST_CLOUD_APPID = "test_cloud_appid"; @@ -195,4 +195,4 @@ HWTEST_F(CloudDataTest, GetSchema, TestSize.Level0) ASSERT_EQ(to_string(schemaMeta.Marshall()), to_string(schemaMeta_.Marshall())); } } // namespace DistributedDataTest -} // namespace OHOS +} // namespace OHOS::Test diff --git a/datamgr_service/services/distributeddataservice/service/test/value_proxy_test.cpp b/datamgr_service/services/distributeddataservice/service/test/value_proxy_test.cpp new file mode 100644 index 00000000..df911512 --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/test/value_proxy_test.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "CloudDataTest" +#include "value_proxy.h" +#include +#include "log_print.h" +namespace Test { +using namespace testing::ext; +using namespace OHOS::DistributedRdb; +class ValueProxyTest : public testing::Test { +}; + +/** +* @tc.name: GetSchema +* @tc.desc: GetSchema from cloud when no schema in meta. +* @tc.type: FUNC +* @tc.require: +* @tc.author: ht +*/ +HWTEST_F(ValueProxyTest, VBucketsNormal2GaussDB, TestSize.Level0) +{ + std::vector dbVBuckets; + OHOS::DistributedData::VBuckets extends = { + {{"#gid", {"0000000"}}, {"#flag", {true }}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}}, + {{"#gid", {"0000001"}}} + }; + dbVBuckets = ValueProxy::Convert(std::move(extends)); + ASSERT_EQ(dbVBuckets.size(), 2); +} + +/** +* @tc.name: GetSchema +* @tc.desc: GetSchema from cloud when no schema in meta. +* @tc.type: FUNC +* @tc.require: +* @tc.author: ht +*/ +HWTEST_F(ValueProxyTest, VBucketsGaussDB2Normal, TestSize.Level0) +{ + std::vector dbVBuckets = { + {{"#gid", {"0000000"}}, {"#flag", {true }}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}}, + {{"#gid", {"0000001"}}} + }; + OHOS::DistributedData::VBuckets extends; + extends = ValueProxy::Convert(std::move(dbVBuckets)); + ASSERT_EQ(extends.size(), 2); +} + +/** +* @tc.name: GetSchema +* @tc.desc: GetSchema from cloud when no schema in meta. +* @tc.type: FUNC +* @tc.require: +* @tc.author: ht +*/ +HWTEST_F(ValueProxyTest, VBucketsNormal2Rdb, TestSize.Level0) +{ + using RdbBucket = OHOS::NativeRdb::ValuesBucket; + std::vector rdbVBuckets; + OHOS::DistributedData::VBuckets extends = { + {{"#gid", {"0000000"}}, {"#flag", {true }}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}}, + {{"#gid", {"0000001"}}} + }; + rdbVBuckets = ValueProxy::Convert(std::move(extends)); + ASSERT_EQ(rdbVBuckets.size(), 2); +} + +/** +* @tc.name: GetSchema +* @tc.desc: GetSchema from cloud when no schema in meta. +* @tc.type: FUNC +* @tc.require: +* @tc.author: ht +*/ +HWTEST_F(ValueProxyTest, VBucketsRdb2Normal, TestSize.Level0) +{ + using RdbBucket = OHOS::NativeRdb::ValuesBucket; + using RdbValue = OHOS::NativeRdb::ValueObject; + std::vector rdbVBuckets = { + RdbBucket(std::map{ + {"#gid", {"0000000"}}, + {"#flag", {true }}, + {"#value", {int64_t(100)}}, + {"#float", {double(100)}} + }), + RdbBucket(std::map{ + {"#gid", {"0000001"}} + }) + }; + OHOS::DistributedData::VBuckets extends; + extends = ValueProxy::Convert(std::move(rdbVBuckets)); + ASSERT_EQ(extends.size(), 2); +} +} diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_util.cpp b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_util.cpp index 7d6041e8..3a400a23 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_util.cpp +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_util.cpp @@ -1162,7 +1162,7 @@ JSUtil::StatusMsg JSUtil::GetValue(napi_env env, napi_value in, DataQuery &query JSUtil::StatusMsg JSUtil::GetCurrentAbilityParam(napi_env env, ContextParam ¶m) { - auto ability = AbilityRuntime::GetCurrentAbility(env); + auto ability =AbilityRuntime::GetCurrentAbility(env); if (ability == nullptr) { ZLOGE("GetCurrentAbility -> ability pointer is nullptr"); return napi_invalid_arg; diff --git a/relational_store/frameworks/js/napi/cloud_data/src/js_error_utils.cpp b/relational_store/frameworks/js/napi/cloud_data/src/js_error_utils.cpp index 59092fa5..246a5076 100644 --- a/relational_store/frameworks/js/napi/cloud_data/src/js_error_utils.cpp +++ b/relational_store/frameworks/js/napi/cloud_data/src/js_error_utils.cpp @@ -24,7 +24,7 @@ using JsErrorCode = OHOS::CloudData::JsErrorCode; static constexpr JsErrorCode JS_ERROR_CODE_MSGS[] = { { Status::INVALID_ARGUMENT, 401, "Parameter error." }, { Status::NOT_SUPPORT, 801, "Not support." }, - { Status::PERMISSION_DENIED, 202, "Permission denied,non-system app called system api." }, + { Status::PERMISSION_DENIED, 202, "Permission denied, non-system app called system api." }, { Status::CLOUD_CONFIG_PERMISSION_DENIED, 201, "Permission denied." } }; diff --git a/relational_store/frameworks/js/napi/relationalstore/include/napi_rdb_store_observer.h b/relational_store/frameworks/js/napi/relationalstore/include/napi_rdb_store_observer.h index 76440d21..1836eed3 100644 --- a/relational_store/frameworks/js/napi/relationalstore/include/napi_rdb_store_observer.h +++ b/relational_store/frameworks/js/napi/relationalstore/include/napi_rdb_store_observer.h @@ -22,10 +22,16 @@ namespace OHOS::RelationalStoreJsKit { class NapiRdbStoreObserver : public DistributedRdb::RdbStoreObserver, public NapiUvQueue { public: - explicit NapiRdbStoreObserver(napi_env env, napi_value callback); + using Origin = DistributedRdb::Origin; + explicit NapiRdbStoreObserver(napi_env env, napi_value callback, int32_t mode = DistributedRdb::REMOTE); virtual ~NapiRdbStoreObserver() noexcept; void OnChange(const std::vector& devices) override; + + void OnChange(const Origin &origin, const PrimaryFields &fields, ChangeInfo &&changeInfo) override; + +private: + int32_t mode_ = DistributedRdb::REMOTE; }; } // namespace OHOS::RelationalStoreJsKit #endif diff --git a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_const_properties.cpp b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_const_properties.cpp index 06629ba7..7ef15430 100644 --- a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_const_properties.cpp +++ b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_const_properties.cpp @@ -85,6 +85,7 @@ static napi_value ExportSecurityLevel(napi_env env) return securityLevel; } #endif + static napi_value ExportProgress(napi_env env) { napi_value progress = nullptr; diff --git a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp index c3bf29d8..3347e50d 100644 --- a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp +++ b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp @@ -386,10 +386,23 @@ int ParseValuesBucket(const napi_env &env, const napi_value &arg, std::shared_pt std::string keyStr = JSUtils::Convert2String(env, key); napi_value value; napi_get_property(env, arg, key, &value); - ValueObject valueObject; - int32_t ret = JSUtils::Convert2Value(env, value, valueObject.value); - if (ret == napi_ok) { - context->valuesBucket.Put(keyStr, std::move(valueObject)); + napi_valuetype valueType = napi_undefined; + napi_typeof(env, value, &valueType); + if (valueType == napi_string) { + std::string valueString = JSUtils::Convert2String(env, value, false); + context->valuesBucket.PutString(keyStr, valueString); + } else if (valueType == napi_number) { + double valueNumber; + napi_get_value_double(env, value, &valueNumber); + context->valuesBucket.PutDouble(keyStr, valueNumber); + } else if (valueType == napi_boolean) { + bool valueBool = false; + napi_get_value_bool(env, value, &valueBool); + context->valuesBucket.PutBool(keyStr, valueBool); + } else if (valueType == napi_null) { + context->valuesBucket.PutNull(keyStr); + } else if (valueType == napi_object) { + context->valuesBucket.PutBlob(keyStr, JSUtils::Convert2U8Vector(env, value)); } else { LOG_WARN("bad value type of key %{public}s", keyStr.c_str()); } @@ -1187,7 +1200,7 @@ napi_value RdbStoreProxy::OnDataChangeEvent(napi_env env, size_t argc, napi_valu } SubscribeOption option; option.mode = static_cast(mode); - auto observer = std::make_shared(env, argv[1]); + auto observer = std::make_shared(env, argv[1], mode); int errCode = rdbStore_->Subscribe(option, observer.get()); RDB_NAPI_ASSERT(env, errCode == E_OK, std::make_shared(E_ERROR)); observers_[mode].push_back(observer); diff --git a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store_observer.cpp b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store_observer.cpp index a6b20cf6..c9d5f719 100644 --- a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store_observer.cpp +++ b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store_observer.cpp @@ -22,7 +22,8 @@ using namespace OHOS::AppDataMgrJsKit; using OHOS::AppDataMgrJsKit::PREFIX_LABEL; namespace OHOS::RelationalStoreJsKit { -NapiRdbStoreObserver::NapiRdbStoreObserver(napi_env env, napi_value callback) : NapiUvQueue(env, callback) +NapiRdbStoreObserver::NapiRdbStoreObserver(napi_env env, napi_value callback, int32_t mode) + : NapiUvQueue(env, callback), mode_(mode) { LOG_INFO("NapiRdbStoreObserver construct"); } @@ -41,4 +42,15 @@ void NapiRdbStoreObserver::OnChange(const std::vector &devices) }); LOG_INFO("NapiRdbStoreObserver::OnChange end"); } + +void NapiRdbStoreObserver::OnChange(const Origin &origin, const PrimaryFields &fields, ChangeInfo &&changeInfo) +{ + if (mode_ == DistributedRdb::CLOUD_DETAIL) { + CallFunction([info = std::move(changeInfo)](napi_env env, int &argc, napi_value *argv) { + argc = 1; + // todo argv[0] = JSUtils::Convert2JSValue(env, info); + }); + } + RdbStoreObserver::OnChange(origin, fields, std::move(changeInfo)); +} } // namespace OHOS::RelationalStoreJsKit \ No newline at end of file diff --git a/relational_store/frameworks/native/rdb/include/rdb_notifier_stub.h b/relational_store/frameworks/native/rdb/include/rdb_notifier_stub.h index ddf8391c..c9367219 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_notifier_stub.h +++ b/relational_store/frameworks/native/rdb/include/rdb_notifier_stub.h @@ -27,26 +27,23 @@ public: class RdbNotifierStub : public IRemoteStub { public: using SyncCompleteHandler = std::function; - using DataChangeHandler = std::function&)>; + using DataChangeHandler = std::function; RdbNotifierStub(const SyncCompleteHandler&, const DataChangeHandler&); virtual ~RdbNotifierStub() noexcept; int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; int32_t OnComplete(uint32_t seqNum, Details &&result) override; - int32_t OnChange(const std::string& storeName, const std::vector& devices) override; int32_t OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo) override; private: int32_t OnCompleteInner(MessageParcel& data, MessageParcel& reply); int32_t OnChangeInner(MessageParcel& data, MessageParcel& reply); - int32_t OnDetailsInner(MessageParcel& data, MessageParcel& reply); bool CheckInterfaceToken(MessageParcel& data); using RequestHandle = int32_t (RdbNotifierStub::*)(MessageParcel&, MessageParcel&); static constexpr RequestHandle HANDLES[RDB_NOTIFIER_CMD_MAX] = { [RDB_NOTIFIER_CMD_SYNC_COMPLETE] = &RdbNotifierStub::OnCompleteInner, [RDB_NOTIFIER_CMD_DATA_CHANGE] = &RdbNotifierStub::OnChangeInner, - [RDB_NOTIFIER_CMD_DATA_DETAILS] = &RdbNotifierStub::OnChangeInner, }; SyncCompleteHandler completeNotifier_; diff --git a/relational_store/frameworks/native/rdb/include/rdb_security_manager.h b/relational_store/frameworks/native/rdb/include/rdb_security_manager.h index 08f95180..94265fa2 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_security_manager.h +++ b/relational_store/frameworks/native/rdb/include/rdb_security_manager.h @@ -76,8 +76,6 @@ public: void DelRdbSecretDataFile(const std::string &path); void DelRdbSecretDataFile(RdbSecurityManager::KeyFileType keyFile); static RdbSecurityManager &GetInstance(); - int GetKeyDistributedStatus(KeyFileType keyFile, bool &status); - int SetKeyDistributedStatus(KeyFileType keyFile, bool status); void Init(const std::string &bundleName, const std::string &path); void UpdateKeyFile(); 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 4f8a91c2..62dd8cf7 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h +++ b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h @@ -58,6 +58,8 @@ public: /*CLoudData*/ int32_t GetSchema(const RdbSyncerParam ¶m) override; private: + using ChangeInfo = RdbStoreObserver::ChangeInfo; + using PrimaryFields = RdbStoreObserver::PrimaryFields; std::pair DoSync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &predicates); @@ -77,7 +79,7 @@ private: void OnSyncComplete(uint32_t seqNum, Details &&result); - void OnDataChange(const std::string& storeName, const std::vector& devices); + void OnDataChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo); std::string RemoveSuffix(const std::string& name); 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 5304839c..e9081825 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_store_impl.h +++ b/relational_store/frameworks/native/rdb/include/rdb_store_impl.h @@ -86,6 +86,7 @@ public: int Restore(const std::string &backupPath, const std::vector &newKey) override; int ChangeDbFileForRestore(const std::string &newPath, const std::string &backupPath, const std::vector &newKey) override; + void GetSchema(); std::string GetName(); std::string GetOrgPath(); std::string GetFileType(); diff --git a/relational_store/frameworks/native/rdb/include/rdb_types_util.h b/relational_store/frameworks/native/rdb/include/rdb_types_util.h index 992263ac..ba0df675 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_types_util.h +++ b/relational_store/frameworks/native/rdb/include/rdb_types_util.h @@ -33,6 +33,11 @@ using Asset = NativeRdb::AssetValue; using ProgressDetail = DistributedRdb::ProgressDetail; using TableDetail = DistributedRdb::TableDetail; using Statistic = DistributedRdb::Statistic; +using Observer = DistributedRdb::RdbStoreObserver; +using Origin = DistributedRdb::Origin; +using ChangeInfo = Observer::ChangeInfo; +using PrimaryKey = Observer::PrimaryKey; +using PrimaryKeys = std::vector[Observer::CHG_TYPE_BUTT]; template<> API_EXPORT bool Marshalling(const SyncerParam &input, MessageParcel &data); template<> @@ -81,5 +86,13 @@ template<> API_EXPORT bool Marshalling(const Statistic &input, MessageParcel &data); template<> API_EXPORT bool Unmarshalling(Statistic &output, MessageParcel &data); +template<> +API_EXPORT bool Marshalling(const PrimaryKeys &input, MessageParcel &data); +template<> +API_EXPORT bool Unmarshalling(PrimaryKeys &output, MessageParcel &data); +template<> +API_EXPORT bool Marshalling(const Origin &input, MessageParcel &data); +template<> +API_EXPORT bool Unmarshalling(Origin &output, MessageParcel &data); } #endif // DISTRIBUTED_RDB_RDB_TYPES_UTIL_H diff --git a/relational_store/frameworks/native/rdb/src/rdb_notifier_stub.cpp b/relational_store/frameworks/native/rdb/src/rdb_notifier_stub.cpp index 77f00c0d..793e22d1 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_notifier_stub.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_notifier_stub.cpp @@ -74,39 +74,24 @@ int32_t RdbNotifierStub::OnComplete(uint32_t seqNum, Details &&result) return RDB_OK; } -int32_t RdbNotifierStub::OnChangeInner(MessageParcel &data, MessageParcel &reply) -{ - std::string storeName; - std::vector devices; - if (!ITypesUtil::Unmarshal(data, storeName, devices)) { - ZLOGE("read sync result failed"); - return RDB_ERROR; - } - return OnChange(storeName, devices); -} - -int32_t RdbNotifierStub::OnChange(const std::string& storeName, const std::vector &devices) -{ - if (changeNotifier_) { - changeNotifier_(storeName, devices); - } - return RDB_OK; -} int32_t RdbNotifierStub::OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo) { + if (changeNotifier_ != nullptr) { + changeNotifier_(origin, primaries, std::move(changeInfo)); + } return 0; } -int32_t RdbNotifierStub::OnDetailsInner(MessageParcel &data, MessageParcel &reply) +int32_t RdbNotifierStub::OnChangeInner(MessageParcel &data, MessageParcel &reply) { Origin origin; PrimaryFields primaries; ChangeInfo changeInfo; -// if (!ITypesUtil::Unmarshal(data, origin, primaries, changeInfo)) { -// ZLOGE("read sync result failed"); -// return RDB_ERROR; -// } + if (!ITypesUtil::Unmarshal(data, origin, primaries, changeInfo)) { + ZLOGE("read sync result failed"); + return RDB_ERROR; + } return OnChange(origin, primaries, std::move(changeInfo)); } } // namespace OHOS::DistributedRdb diff --git a/relational_store/frameworks/native/rdb/src/rdb_security_manager.cpp b/relational_store/frameworks/native/rdb/src/rdb_security_manager.cpp index eefbd805..439e2f53 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_security_manager.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_security_manager.cpp @@ -598,39 +598,6 @@ bool RdbSecurityManager::CheckKeyDataFileExists(RdbSecurityManager::KeyFileType } } -int RdbSecurityManager::GetKeyDistributedStatus(KeyFileType keyFile, bool &status) -{ - LOG_INFO("GetKeyDistributedStatus start."); - std::string keyPath; - GetKeyPath(keyFile, keyPath); - - RdbSecretKeyData keyData; - if (!LoadSecretKeyFromDisk(keyPath, keyData)) { - return E_ERROR; - } - - status = (keyData.distributed == DISTRIBUTED); - return E_OK; -} - -int RdbSecurityManager::SetKeyDistributedStatus(KeyFileType keyFile, bool status) -{ - LOG_INFO("SetKeyDistributedStatus start."); - std::string keyPath; - GetKeyPath(keyFile, keyPath); - RdbSecretKeyData keyData; - if (!LoadSecretKeyFromDisk(keyPath, keyData)) { - return E_ERROR; - } - - keyData.distributed = (status ? DISTRIBUTED : UNDISTRIBUTED); - if (!SaveSecretKeyToDisk(keyPath, keyData)) { - return E_ERROR; - } - - return E_OK; -} - void RdbSecurityManager::GetKeyPath(RdbSecurityManager::KeyFileType keyType, std::string &keyPath) { if (keyType == KeyFileType::PUB_KEY_FILE) { 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 7a2e1a2e..76d7e565 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp @@ -18,8 +18,10 @@ #include "rdb_service_proxy.h" #include "itypes_util.h" #include "log_print.h" +#include "sqlite_utils.h" namespace OHOS::DistributedRdb { +using SqliteUtils = OHOS::NativeRdb::SqliteUtils; #define IPC_SEND(code, reply, ...) \ ({ \ int32_t __status = RDB_OK; \ @@ -54,19 +56,21 @@ RdbServiceProxy::RdbServiceProxy(const sptr &object) void RdbServiceProxy::OnSyncComplete(uint32_t seqNum, Details &&result) { syncCallbacks_.ComputeIfPresent(seqNum, [&result] (const auto& key, const AsyncDetail & callback) { + auto finished = result.empty() || (result.begin()->second.progress == SYNC_FINISH); callback(std::move(result)); - return true; + return !finished; }); - syncCallbacks_.Erase(seqNum); } -void RdbServiceProxy::OnDataChange(const std::string& storeName, const std::vector &devices) +void RdbServiceProxy::OnDataChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo) { - auto name = RemoveSuffix(storeName); - observers_.ComputeIfPresent( - name, [&devices] (const auto& key, const ObserverMapValue& value) { - for (const auto& observer : value.first) { - observer->OnChange(devices); + auto name = RemoveSuffix(origin.store); + observers_.ComputeIfPresent(name, + [&origin, &primaries, info = std::move(changeInfo)](const auto &key, const ObserverMapValue &value) mutable { + auto size = value.first.size(); + for (const auto &observer : value.first) { + size--; + observer->OnChange(origin, primaries, (size > 0) ? ChangeInfo(info) : std::move(info)); } return true; }); @@ -77,7 +81,8 @@ std::string RdbServiceProxy::ObtainDistributedTableName(const std::string &devic MessageParcel reply; int32_t status = IPC_SEND(RDB_SERVICE_CMD_OBTAIN_TABLE, reply, device, table); if (status != RDB_OK) { - ZLOGE("status:%{public}d, device:%{public}.6s, table:%{public}s", status, device.c_str(), table.c_str()); + ZLOGE("status:%{public}d, device:%{public}.6s, table:%{public}s", status, + SqliteUtils::Anonymous(device).c_str(), SqliteUtils::Anonymous(table).c_str()); return ""; } return reply.ReadString(); @@ -86,13 +91,12 @@ std::string RdbServiceProxy::ObtainDistributedTableName(const std::string &devic int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam ¶m) { notifier_ = new (std::nothrow) RdbNotifierStub( - [this] (uint32_t seqNum, Details &&result) { + [this](uint32_t seqNum, Details &&result) { OnSyncComplete(seqNum, std::move(result)); }, - [this] (const std::string& storeName, const std::vector& devices) { - OnDataChange(storeName, devices); - } - ); + [this](const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo) { + OnDataChange(origin, primaries, std::move(changeInfo)); + }); if (notifier_ == nullptr) { ZLOGE("create notifier failed"); return RDB_ERROR; @@ -135,7 +139,7 @@ std::pair RdbServiceProxy::DoSync(const RdbSyncerParam& param, status = IPC_SEND(RDB_SERVICE_CMD_SYNC, reply, param, option, predicates); if (status != RDB_OK) { ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", - status, param.bundleName_.c_str(), param.storeName_.c_str()); + status, param.bundleName_.c_str(), SqliteUtils::Anonymous(param.storeName_).c_str()); return result; } @@ -169,7 +173,7 @@ int32_t RdbServiceProxy::DoAsync(const RdbSyncerParam ¶m, const Option &opti int32_t status = IPC_SEND(RDB_SERVICE_CMD_ASYNC, reply, param, option, predicates); if (status != RDB_OK) { ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s, seqNum:%{public}u", status, - param.bundleName_.c_str(), param.storeName_.c_str(), option.seqNum); + param.bundleName_.c_str(), SqliteUtils::Anonymous(param.storeName_).c_str(), option.seqNum); } return status; } @@ -178,7 +182,7 @@ int32_t RdbServiceProxy::DoAsync(const RdbSyncerParam& param, const Option &opti const RdbPredicates &predicates, const AsyncDetail & callback) { Option asyncOption = option; - if (callback == nullptr) { + if (callback != nullptr) { asyncOption.seqNum = GetSeqNum(); if (!syncCallbacks_.Insert(asyncOption.seqNum, callback)) { ZLOGI("insert callback failed"); @@ -202,7 +206,7 @@ int32_t RdbServiceProxy::SetDistributedTables(const RdbSyncerParam& param, const int32_t status = IPC_SEND(RDB_SERVICE_CMD_SET_DIST_TABLE, reply, param, tables); if (status != RDB_OK) { ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", - status, param.bundleName_.c_str(), param.storeName_.c_str()); + status, param.bundleName_.c_str(), SqliteUtils::Anonymous(param.storeName_).c_str()); } return status; } @@ -211,9 +215,9 @@ int32_t RdbServiceProxy::Sync(const RdbSyncerParam ¶m, const Option &option, const AsyncDetail &async) { if (option.isAsync) { - return DoSync(param, option, predicates, async); + return DoAsync(param, option, predicates, async); } - return DoAsync(param, option, predicates, async); + return DoSync(param, option, predicates, async); } std::string RdbServiceProxy::RemoveSuffix(const std::string& name) @@ -258,7 +262,7 @@ int32_t RdbServiceProxy::DoSubscribe(const RdbSyncerParam ¶m, const Subscrib int32_t status = IPC_SEND(RDB_SERVICE_CMD_SUBSCRIBE, reply, param, option); if (status != RDB_OK) { ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", - status, param.bundleName_.c_str(), param.storeName_.c_str()); + status, param.bundleName_.c_str(), SqliteUtils::Anonymous(param.storeName_).c_str()); } return status; } @@ -284,7 +288,7 @@ int32_t RdbServiceProxy::DoUnSubscribe(const RdbSyncerParam ¶m) int32_t status = IPC_SEND(RDB_SERVICE_CMD_UNSUBSCRIBE, reply, param); if (status != RDB_OK) { ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", - status, param.bundleName_.c_str(), param.storeName_.c_str()); + status, param.bundleName_.c_str(), SqliteUtils::Anonymous(param.storeName_).c_str()); } return status; } @@ -296,7 +300,7 @@ int32_t RdbServiceProxy::RemoteQuery(const RdbSyncerParam& param, const std::str int32_t status = IPC_SEND(RDB_SERVICE_CMD_REMOTE_QUERY, reply, param, device, sql, selectionArgs); if (status != RDB_OK) { ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s, device:%{public}.6s", - status, param.bundleName_.c_str(), param.storeName_.c_str(), device.c_str()); + status, param.bundleName_.c_str(), SqliteUtils::Anonymous(param.storeName_).c_str(), device.c_str()); return status; } @@ -346,7 +350,7 @@ int32_t RdbServiceProxy::GetSchema(const RdbSyncerParam ¶m) 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()); + SqliteUtils::Anonymous(param.storeName_).c_str()); } return status; } diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp index 4566f94d..62988bca 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -92,27 +92,39 @@ int RdbStoreImpl::InnerOpen(const RdbStoreConfig &config) syncerParam_.type_ = config.GetDistributedType(); syncerParam_.isEncrypt_ = config.IsEncrypt(); syncerParam_.password_ = {}; + GetSchema(); +#endif + return E_OK; +} +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) +void RdbStoreImpl::GetSchema() +{ + auto [err, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_); + if (err != E_OK || service == nullptr) { + LOG_WARN("GetRdbService failed, err is %{public}d.", err); + return; + } + if (isEncrypt_) { + RdbPassword key = + RdbSecurityManager::GetInstance().GetRdbPassword(RdbSecurityManager::KeyFileType::PUB_KEY_FILE); + syncerParam_.password_ = std::vector(key.GetData(), key.GetData() + key.GetSize()); + } if (pool_ == nullptr) { pool_ = TaskExecutor::GetInstance().GetExecutor(); } if (pool_ != nullptr) { 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; - } - err = service->GetSchema(param); + auto ser = service; + pool_->Execute([param, ser]() { + auto err = ser->GetSchema(param); if (err != E_OK) { LOG_ERROR("GetSchema failed, err is %{public}d.", err); } }); } -#endif - return E_OK; } +#endif RdbStoreImpl::RdbStoreImpl(const RdbStoreConfig &config) : rdbStoreConfig(config), connectionPool(nullptr), isOpen(false), path(""), orgPath(""), isReadOnly(false), @@ -123,6 +135,10 @@ RdbStoreImpl::RdbStoreImpl(const RdbStoreConfig &config) RdbStoreImpl::~RdbStoreImpl() { delete connectionPool; +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) + syncerParam_.password_.assign(syncerParam_.password_.size(), 0); + syncerParam_.password_.clear(); +#endif } #ifdef WINDOWS_PLATFORM @@ -1127,16 +1143,6 @@ int RdbStoreImpl::SetDistributedTables(const std::vector &tables, i LOG_WARN("The distributed tables to be set is empty."); return E_OK; } - if (isEncrypt_) { - bool status = false; - RdbSecurityManager::GetInstance().GetKeyDistributedStatus(RdbSecurityManager::KeyFileType::PUB_KEY_FILE, - status); - if (!status) { - RdbPassword key = - RdbSecurityManager::GetInstance().GetRdbPassword(RdbSecurityManager::KeyFileType::PUB_KEY_FILE); - syncerParam_.password_ = std::vector(key.GetData(), key.GetData() + key.GetSize()); - } - } auto [errCode, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_); if (errCode != E_OK) { return errCode; @@ -1144,17 +1150,8 @@ int RdbStoreImpl::SetDistributedTables(const std::vector &tables, i 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); - syncerParam_.password_.clear(); return errorCode; } - - if (isEncrypt_) { - syncerParam_.password_.assign(syncerParam_.password_.size(), 0); - syncerParam_.password_.clear(); - RdbSecurityManager::GetInstance().SetKeyDistributedStatus( - RdbSecurityManager::KeyFileType::PUB_KEY_FILE, true); - } return E_OK; } @@ -1189,7 +1186,7 @@ int RdbStoreImpl::Sync(const SyncOption &option, const AbsRdbPredicates &predica } DistributedRdb::RdbService::Option rdbOption; rdbOption.mode = option.mode; - rdbOption.isAsync = option.isBlock; + rdbOption.isAsync = !option.isBlock; errCode = service->Sync(syncerParam_, rdbOption, predicate.GetDistributedPredicates(), [callback](Details &&details) { Briefs briefs; @@ -1216,7 +1213,7 @@ int RdbStoreImpl::Sync(const SyncOption &option, const std::vector } DistributedRdb::RdbService::Option rdbOption; rdbOption.mode = option.mode; - rdbOption.isAsync = option.isBlock; + rdbOption.isAsync = !option.isBlock; errCode = service->Sync(syncerParam_, rdbOption, AbsRdbPredicates(tables).GetDistributedPredicates(), async); if (errCode != E_OK) { LOG_ERROR("Sync is failed, err is %{public}d.", errCode); 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 f10accea..f3be9122 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp @@ -142,4 +142,28 @@ bool Unmarshalling(Statistic &output, MessageParcel &data) { return Unmarshal(data, output.total, output.success, output.failed, output.untreated); } + +template<> +bool Marshalling(const PrimaryKeys &input, MessageParcel &data) +{ + return Marshal(data, input[Observer::CHG_TYPE_INSERT], input[Observer::CHG_TYPE_UPDATE], + input[Observer::CHG_TYPE_DELETE]); +} + +template<> +bool Unmarshalling(PrimaryKeys &output, MessageParcel &data) +{ + return Unmarshal(data, output[Observer::CHG_TYPE_INSERT], output[Observer::CHG_TYPE_UPDATE], + output[Observer::CHG_TYPE_DELETE]); +} +template<> +bool ITypesUtil::Marshalling(const Origin &input, MessageParcel &data) +{ + return Marshal(data, input.origin, input.id, input.store); +} +template<> +bool ITypesUtil::Unmarshalling(Origin &output, MessageParcel &data) +{ + return Unmarshal(data, output.origin, output.id, output.store); +} } diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_notifier.h b/relational_store/interfaces/inner_api/rdb/include/rdb_notifier.h index fd87f44b..9a8af5b2 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_notifier.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_notifier.h @@ -25,13 +25,10 @@ public: enum Code : int32_t { RDB_NOTIFIER_CMD_SYNC_COMPLETE, RDB_NOTIFIER_CMD_DATA_CHANGE, - RDB_NOTIFIER_CMD_DATA_DETAILS, RDB_NOTIFIER_CMD_MAX }; virtual int32_t OnComplete(uint32_t seqNum, Details &&result) = 0; - virtual int32_t OnChange(const std::string &storeName, const std::vector &devices) = 0; - virtual int32_t OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo) = 0; }; } // namespace OHOS::DistributedRdb 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 373e2932..707de1e8 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_types.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_types.h @@ -16,10 +16,10 @@ #ifndef DISTRIBUTED_RDB_RDB_TYPES_H #define DISTRIBUTED_RDB_RDB_TYPES_H -#include +//#include #include #include -#include +//#include #include #include #include @@ -96,12 +96,14 @@ struct ProgressDetail { TableDetails details; }; -using SyncResult = std::map; using Briefs = std::map; using Details = std::map; using AsyncBrief = std::function; using AsyncDetail = std::function; +using SyncResult = Briefs; +using SyncCallback = AsyncBrief; + enum RdbPredicateOperator { EQUAL_TO, NOT_EQUAL_TO, @@ -172,8 +174,9 @@ public: }; using PrimaryKey = std::variant; using ChangeInfo = std::map[CHG_TYPE_BUTT]>; - virtual void OnChange(const std::vector& devices) = 0; // networkid - virtual void OnChange(const Origin &origin, ChangeInfo &&changeInfo) + using PrimaryFields = std::map; + virtual void OnChange(const std::vector &devices) = 0; // networkid + virtual void OnChange(const Origin &origin, const PrimaryFields &fields, ChangeInfo &&changeInfo) { OnChange(origin.id); }; diff --git a/relational_store/interfaces/inner_api/rdb/mock/include/abs_rdb_predicates.h b/relational_store/interfaces/inner_api/rdb/mock/include/abs_rdb_predicates.h index a010d680..40f92199 100644 --- a/relational_store/interfaces/inner_api/rdb/mock/include/abs_rdb_predicates.h +++ b/relational_store/interfaces/inner_api/rdb/mock/include/abs_rdb_predicates.h @@ -23,7 +23,7 @@ namespace OHOS::NativeRdb { class AbsRdbPredicates : public AbsPredicates { public: explicit AbsRdbPredicates(std::string tableName); - + explicit AbsRdbPredicates(std::vector tables); ~AbsRdbPredicates() override {} void Clear() override; @@ -50,7 +50,7 @@ protected: int joinCount = 0; private: - std::string tableName; + std::string tableName_; }; } // namespace OHOS::NativeRdb diff --git a/relational_store/test/js/rdb/unittest/src/RdbStoreDistributedJsunit.test.js b/relational_store/test/js/rdb/unittest/src/RdbStoreDistributedJsunit.test.js index 80043f5b..8a83410d 100644 --- a/relational_store/test/js/rdb/unittest/src/RdbStoreDistributedJsunit.test.js +++ b/relational_store/test/js/rdb/unittest/src/RdbStoreDistributedJsunit.test.js @@ -28,8 +28,7 @@ describe('rdbStoreDistributedTest', function () { } try { rdbStore = await dataRdb.getRdbStore(config, 1); - console.log(TAG + "create rdb store success") - expect(rdbStore).assertEqual(rdbStore) + console.log(TAG + "create rdb store success"); let sqlStatement = "CREATE TABLE IF NOT EXISTS employee (" + "id INTEGER PRIMARY KEY AUTOINCREMENT," + "name TEXT NOT NULL," + diff --git a/relational_store/test/js/relationalstore/unittest/src/RdbStoreDistributedJsunit.test.js b/relational_store/test/js/relationalstore/unittest/src/RdbStoreDistributedJsunit.test.js index cd183306..186c9f84 100644 --- a/relational_store/test/js/relationalstore/unittest/src/RdbStoreDistributedJsunit.test.js +++ b/relational_store/test/js/relationalstore/unittest/src/RdbStoreDistributedJsunit.test.js @@ -33,7 +33,6 @@ describe('rdbStoreDistributedTest', function () { try { rdbStore = await data_relationalStore.getRdbStore(context, config); console.log(TAG + "create rdb store success") - expect(rdbStore).assertEqual(rdbStore) let sqlStatement = "CREATE TABLE IF NOT EXISTS employee (" + "id INTEGER PRIMARY KEY AUTOINCREMENT," + "name TEXT NOT NULL," + diff --git a/relational_store/test/native/rdb/distributedtest/rdb_store_impl_test/distributed_test_agent.cpp b/relational_store/test/native/rdb/distributedtest/rdb_store_impl_test/distributed_test_agent.cpp index 0bde8004..258ef9e8 100644 --- a/relational_store/test/native/rdb/distributedtest/rdb_store_impl_test/distributed_test_agent.cpp +++ b/relational_store/test/native/rdb/distributedtest/rdb_store_impl_test/distributed_test_agent.cpp @@ -50,7 +50,7 @@ public: virtual int OnProcessMsg(const std::string &strMsg, int len, std::string &strReturnValue, int returnBufL); using SyncOption = DistributedRdb::SyncOption; - using SyncCallback = DistributedRdb::SyncCallback; + using SyncCallback = DistributedRdb::AsyncBrief; }; const std::string DistributedTestAgent::DATABASE_NAME = RDB_TEST_PATH + "distributed_rdb.db"; diff --git a/relational_store/test/native/rdb/fuzztest/rdbimpl_fuzzer/rdbimpl_fuzzer.cpp b/relational_store/test/native/rdb/fuzztest/rdbimpl_fuzzer/rdbimpl_fuzzer.cpp index dd799258..ac381ef3 100644 --- a/relational_store/test/native/rdb/fuzztest/rdbimpl_fuzzer/rdbimpl_fuzzer.cpp +++ b/relational_store/test/native/rdb/fuzztest/rdbimpl_fuzzer/rdbimpl_fuzzer.cpp @@ -25,7 +25,7 @@ void RdbStoreImplFuzz(const uint8_t *data, size_t size) std::string rawString(reinterpret_cast(data), size); std::vector tables; tables.push_back(rawString); - rdbStoreImpl.SetDistributedTables(tables); + rdbStoreImpl.SetDistributedTables(tables, size % 2); } } diff --git a/relational_store/test/native/rdb/unittest/rdb_encrypt_decrypt_test.cpp b/relational_store/test/native/rdb/unittest/rdb_encrypt_decrypt_test.cpp index 44689a2c..081116b3 100644 --- a/relational_store/test/native/rdb/unittest/rdb_encrypt_decrypt_test.cpp +++ b/relational_store/test/native/rdb/unittest/rdb_encrypt_decrypt_test.cpp @@ -313,39 +313,3 @@ HWTEST_F(RdbEncryptTest, RdbStore_Encrypt_08, TestSize.Level1) errCode = RdbHelper::DeleteRdbStore(path); EXPECT_EQ(errCode, E_OK); } - -/** - * @tc.name: RdbStore_Encrypt_Decrypt_Test_009 - * @tc.desc: test GetKeyDistributedStatus and SetKeyDistributedStatus - * @tc.type: FUNC - */ -HWTEST_F(RdbEncryptTest, RdbStore_Encrypt_09, TestSize.Level1) -{ - RdbStoreConfig config(RdbEncryptTest::ENCRYPTED_DATABASE_NAME); - config.SetEncryptStatus(true); - config.SetBundleName("com.example.TestEncrypt9"); - EncryptTestOpenCallback helper; - int errCode; - std::shared_ptr store = RdbHelper::GetRdbStore(config, 1, helper, errCode); - EXPECT_NE(store, nullptr); - - bool distributedStatus = false; - int ret = RdbSecurityManager::GetInstance().GetKeyDistributedStatus( - RdbSecurityManager::KeyFileType::PUB_KEY_FILE, distributedStatus); - EXPECT_EQ(ret, E_OK); - EXPECT_EQ(distributedStatus, false); - ret = RdbSecurityManager::GetInstance().GetKeyDistributedStatus( - RdbSecurityManager::KeyFileType::PUB_KEY_FILE_NEW_KEY, distributedStatus); - EXPECT_EQ(ret, E_ERROR); - EXPECT_EQ(distributedStatus, false); - ret = RdbSecurityManager::GetInstance().SetKeyDistributedStatus( - RdbSecurityManager::KeyFileType::PUB_KEY_FILE, true); - EXPECT_EQ(ret, E_OK); - ret = RdbSecurityManager::GetInstance().GetKeyDistributedStatus( - RdbSecurityManager::KeyFileType::PUB_KEY_FILE, distributedStatus); - EXPECT_EQ(ret, E_OK); - EXPECT_EQ(distributedStatus, true); - ret = RdbSecurityManager::GetInstance().SetKeyDistributedStatus( - RdbSecurityManager::KeyFileType::PUB_KEY_FILE_NEW_KEY, distributedStatus); - EXPECT_EQ(ret, E_ERROR); -} diff --git a/relational_store/test/native/rdb/unittest/rdb_store_subscribe_test.cpp b/relational_store/test/native/rdb/unittest/rdb_store_subscribe_test.cpp index 55c331e7..d878d80b 100644 --- a/relational_store/test/native/rdb/unittest/rdb_store_subscribe_test.cpp +++ b/relational_store/test/native/rdb/unittest/rdb_store_subscribe_test.cpp @@ -26,6 +26,11 @@ using namespace testing::ext; using namespace OHOS::NativeRdb; using namespace OHOS::DistributedRdb; +class SubObserver : public RdbStoreObserver { +public: + virtual ~SubObserver() {} + void OnChange(const std::vector& devices) override; +}; class RdbStoreSubTest : public testing::Test { public: @@ -37,14 +42,19 @@ public: static const std::string MAIN_DATABASE_NAME; static std::shared_ptr CreateRDB(int version); static std::shared_ptr store; + static std::shared_ptr observer_; }; const std::string RdbStoreSubTest::MAIN_DATABASE_NAME = RDB_TEST_PATH + "getrdb.db"; std::shared_ptr RdbStoreSubTest::store = nullptr; +std::shared_ptr RdbStoreSubTest::observer_ = nullptr; void RdbStoreSubTest::SetUpTestCase(void) { store = CreateRDB(1); + if (observer_ == nullptr) { + observer_ = std::make_shared(); + } } void RdbStoreSubTest::TearDownTestCase(void) @@ -75,12 +85,6 @@ int Callback::OnUpgrade(RdbStore &store, int oldVersion, int newVersion) return E_OK; } -class SubObserver : public RdbStoreObserver { -public: - virtual ~SubObserver() {} - void OnChange(const std::vector& devices) override; -}; - void SubObserver::OnChange(const std::vector &devices) { } @@ -109,9 +113,9 @@ std::shared_ptr RdbStoreSubTest::CreateRDB(int version) */ HWTEST_F(RdbStoreSubTest, RdbStoreSubscribeRemote, TestSize.Level1) { - EXPECT_NE(store, nullptr) << "store is nullptr"; - std::shared_ptr observer = std::make_shared(); - auto status = store->Subscribe({ SubscribeMode::REMOTE }, observer.get()); + EXPECT_NE(store, nullptr) << "store is null"; + EXPECT_NE(observer_, nullptr) << "observer is null"; + auto status = store->Subscribe({ SubscribeMode::REMOTE }, observer_.get()); EXPECT_EQ(status, E_OK); } @@ -124,9 +128,9 @@ HWTEST_F(RdbStoreSubTest, RdbStoreSubscribeRemote, TestSize.Level1) */ HWTEST_F(RdbStoreSubTest, RdbStoreSubscribeCloud, TestSize.Level1) { - EXPECT_NE(store, nullptr) << "store is nullptr"; - std::shared_ptr observer = std::make_shared(); - auto status = store->Subscribe({ SubscribeMode::CLOUD }, observer.get()); + EXPECT_NE(store, nullptr) << "store is null"; + EXPECT_NE(observer_, nullptr) << "observer is null"; + auto status = store->Subscribe({ SubscribeMode::CLOUD }, observer_.get()); EXPECT_EQ(status, E_OK); } @@ -139,8 +143,8 @@ HWTEST_F(RdbStoreSubTest, RdbStoreSubscribeCloud, TestSize.Level1) */ HWTEST_F(RdbStoreSubTest, RdbStoreSubscribeCloudDetail, TestSize.Level1) { - EXPECT_NE(store, nullptr) << "store is nullptr"; - std::shared_ptr observer = std::make_shared(); - auto status = store->Subscribe({ SubscribeMode::CLOUD_DETAIL }, observer.get()); + EXPECT_NE(store, nullptr) << "store is null"; + EXPECT_NE(observer_, nullptr) << "observer is null"; + auto status = store->Subscribe({ SubscribeMode::CLOUD_DETAIL }, observer_.get()); EXPECT_EQ(status, E_OK); } \ No newline at end of file -- Gitee