From 32b25d8f94a5145218b7afbe2ca386066abc2b56 Mon Sep 17 00:00:00 2001 From: wuchunbo Date: Thu, 24 Feb 2022 19:42:46 +0800 Subject: [PATCH 1/2] optimize inAllDevices of predicate Signed-off-by: wuchunbo --- .../innerkitsimpl/rdb/include/irdb_service.h | 3 -- .../innerkitsimpl/rdb/include/rdb_manager.h | 2 -- .../innerkitsimpl/rdb/src/rdb_manager.cpp | 5 --- .../rdb/src/rdb_manager_impl.cpp | 12 ------- .../innerkitsimpl/rdb/src/rdb_manager_impl.h | 2 -- .../rdb/src/rdb_service_proxy.cpp | 25 ------------- .../innerkitsimpl/rdb/src/rdb_service_proxy.h | 2 -- .../service/rdb/rdb_service_impl.cpp | 23 +++--------- .../service/rdb/rdb_service_impl.h | 2 -- .../service/rdb/rdb_service_stub.cpp | 10 ------ .../service/rdb/rdb_service_stub.h | 3 -- .../service/rdb/rdb_syncer.cpp | 35 +++++++++++++++---- .../service/rdb/rdb_syncer.h | 5 +-- 13 files changed, 37 insertions(+), 92 deletions(-) diff --git a/frameworks/innerkitsimpl/rdb/include/irdb_service.h b/frameworks/innerkitsimpl/rdb/include/irdb_service.h index 399be4fe0..e6b945411 100644 --- a/frameworks/innerkitsimpl/rdb/include/irdb_service.h +++ b/frameworks/innerkitsimpl/rdb/include/irdb_service.h @@ -27,7 +27,6 @@ class IRdbService : public RdbService, public IRemoteBroker { public: enum { RDB_SERVICE_CMD_OBTAIN_TABLE, - RDB_SERVICE_CMD_GET_DEVICES, RDB_SERVICE_CMD_INIT_NOTIFIER, RDB_SERVICE_CMD_SET_DIST_TABLE, RDB_SERVICE_CMD_SYNC, @@ -39,8 +38,6 @@ public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.IRdbService"); - virtual std::vector GetConnectDevices() = 0; - virtual int32_t InitNotifier(const RdbSyncerParam& param, const sptr notifier) = 0; protected: diff --git a/frameworks/innerkitsimpl/rdb/include/rdb_manager.h b/frameworks/innerkitsimpl/rdb/include/rdb_manager.h index fe1d95307..ad02e41e6 100644 --- a/frameworks/innerkitsimpl/rdb/include/rdb_manager.h +++ b/frameworks/innerkitsimpl/rdb/include/rdb_manager.h @@ -26,8 +26,6 @@ namespace OHOS::DistributedRdb { class RdbManager { public: - static std::vector GetConnectDevices(); - static std::shared_ptr GetRdbService(const RdbSyncerParam& param); }; } diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_manager.cpp b/frameworks/innerkitsimpl/rdb/src/rdb_manager.cpp index 7a438892c..bf0fdb212 100644 --- a/frameworks/innerkitsimpl/rdb/src/rdb_manager.cpp +++ b/frameworks/innerkitsimpl/rdb/src/rdb_manager.cpp @@ -17,11 +17,6 @@ #include "rdb_manager_impl.h" namespace OHOS::DistributedRdb { -std::vector RdbManager::GetConnectDevices() -{ - return RdbManagerImpl::GetInstance().GetConnectDevices(); -} - std::shared_ptr RdbManager::GetRdbService(const RdbSyncerParam& param) { return RdbManagerImpl::GetInstance().GetRdbService(param); diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp index b0c55b370..1be079979 100644 --- a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp +++ b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.cpp @@ -78,18 +78,6 @@ RdbManagerImpl& RdbManagerImpl::GetInstance() return manager; } -std::vector RdbManagerImpl::GetConnectDevices() -{ - std::lock_guard lock(mutex_); - auto service = GetRdbService(); - if (service == nullptr) { - ZLOGE("get service failed"); - return std::vector(); - } - - return ((RdbServiceProxy *)service.GetRefPtr())->GetConnectDevices(); -} - sptr RdbManagerImpl::GetRdbService() { if (distributedDataMgr_ == nullptr) { diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h index 95ea174c5..5285e1a96 100644 --- a/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h +++ b/frameworks/innerkitsimpl/rdb/src/rdb_manager_impl.h @@ -40,8 +40,6 @@ public: static RdbManagerImpl &GetInstance(); - std::vector GetConnectDevices(); - std::shared_ptr GetRdbService(const RdbSyncerParam& param); void OnRemoteDied(); diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.cpp b/frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.cpp index 3f1e9ec24..899882932 100644 --- a/frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.cpp +++ b/frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.cpp @@ -231,31 +231,6 @@ int32_t RdbServiceProxy::DoAsync(const RdbSyncerParam& param, const SyncOption & return RDB_OK; } -std::vector RdbServiceProxy::GetConnectDevices() -{ - MessageParcel data; - if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { - ZLOGE("write descriptor failed"); - return {}; - } - - MessageParcel reply; - MessageOption option; - if (Remote()->SendRequest(RDB_SERVICE_CMD_GET_DEVICES, data, reply, option) != 0) { - ZLOGE("send request failed"); - return {}; - } - - std::vector devices; - if (!reply.ReadStringVector(&devices)) { - ZLOGE("read devices failed"); - return {}; - } - - ZLOGI("success"); - return devices; -} - int32_t RdbServiceProxy::SetDistributedTables(const RdbSyncerParam& param, const std::vector &tables) { MessageParcel data; diff --git a/frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.h b/frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.h index 29dab88f9..468fb5a13 100644 --- a/frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.h +++ b/frameworks/innerkitsimpl/rdb/src/rdb_service_proxy.h @@ -33,8 +33,6 @@ public: std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; - std::vector GetConnectDevices() override; - int32_t InitNotifier(const RdbSyncerParam& param); int32_t InitNotifier(const RdbSyncerParam& param, const sptr notifier) override; diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index eb74d2077..034f5d613 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -125,20 +125,6 @@ std::string RdbServiceImpl::ObtainDistributedTableName(const std::string &device return DistributedDB::RelationalStoreManager::GetDistributedTableName(uuid, table); } -std::vector RdbServiceImpl::GetConnectDevices() -{ - auto deviceInfos = AppDistributedKv::CommunicationProvider::GetInstance().GetRemoteNodesBasicInfo(); - std::vector devices; - for (const auto& deviceInfo : deviceInfos) { - devices.push_back(deviceInfo.deviceId); - } - ZLOGI("size=%{public}u", static_cast(devices.size())); - for (const auto& device: devices) { - ZLOGI("%{public}.6s", device.c_str()); - } - return devices; -} - int32_t RdbServiceImpl::InitNotifier(const RdbSyncerParam& param, const sptr notifier) { if (!CheckAccess(param)) { @@ -262,7 +248,7 @@ int32_t RdbServiceImpl::DoSync(const RdbSyncerParam ¶m, const SyncOption &op if (syncer == nullptr) { return RDB_ERROR; } - return syncer->DoSync(option, predicates, result); + return syncer->DoSync(option, const_cast(predicates), result); } void RdbServiceImpl::OnAsyncComplete(pid_t pid, uint32_t seqNum, const SyncResult &result) @@ -282,9 +268,10 @@ int32_t RdbServiceImpl::DoAsync(const RdbSyncerParam ¶m, uint32_t seqNum, co if (syncer == nullptr) { return RDB_ERROR; } - return syncer->DoAsync(option, predicates, [this, pid, seqNum] (const SyncResult& result) { - OnAsyncComplete(pid, seqNum, result); - }); + return syncer->DoAsync(option, const_cast(predicates), + [this, pid, seqNum] (const SyncResult& result) { + OnAsyncComplete(pid, seqNum, result); + }); } std::string RdbServiceImpl::TransferStringToHex(const std::string &origStr) diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index 31a96de64..1014ae330 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -36,8 +36,6 @@ public: /* IPC interface */ std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; - std::vector GetConnectDevices() override; - int32_t InitNotifier(const RdbSyncerParam& param, const sptr notifier) override; int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector& tables) override; diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index 797d532f8..1de876eea 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -40,16 +40,6 @@ int32_t RdbServiceStub::OnRemoteObtainDistributedTableName(MessageParcel &data, return RDB_OK; } -int32_t RdbServiceStub::OnRemoteGetConnectDevices(MessageParcel& data, MessageParcel& reply) -{ - std::vector devices = GetConnectDevices(); - if (!reply.WriteStringVector(devices)) { - ZLOGE("write devices failed"); - } - - return RDB_OK; -} - int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel &reply) { int32_t error = RDB_ERROR; diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.h b/services/distributeddataservice/service/rdb/rdb_service_stub.h index c520bc490..a6c1e6217 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.h +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.h @@ -48,8 +48,6 @@ private: int32_t OnRemoteObtainDistributedTableName(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteGetConnectDevices(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteInitNotifier(MessageParcel&data, MessageParcel& reply); int32_t OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply); @@ -65,7 +63,6 @@ private: using RequestHandle = int (RdbServiceStub::*)(MessageParcel &, MessageParcel &); static constexpr RequestHandle HANDLERS[RDB_SERVICE_CMD_MAX] = { [RDB_SERVICE_CMD_OBTAIN_TABLE] = &RdbServiceStub::OnRemoteObtainDistributedTableName, - [RDB_SERVICE_CMD_GET_DEVICES] = &RdbServiceStub::OnRemoteGetConnectDevices, [RDB_SERVICE_CMD_INIT_NOTIFIER] = &RdbServiceStub::OnRemoteInitNotifier, [RDB_SERVICE_CMD_SET_DIST_TABLE] = &RdbServiceStub::OnRemoteSetDistributedTables, [RDB_SERVICE_CMD_SYNC] = &RdbServiceStub::OnRemoteDoSync, diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/services/distributeddataservice/service/rdb/rdb_syncer.cpp index 1c48d44cb..fc804baaa 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -23,6 +23,7 @@ #include "kvstore_utils.h" #include "kvstore_meta_manager.h" +using OHOS::DistributedKv::KvStoreUtils; using OHOS::DistributedKv::AccountDelegate; using OHOS::DistributedKv::KvStoreMetaManager; using OHOS::AppDistributedKv::CommunicationProvider; @@ -177,16 +178,32 @@ int32_t RdbSyncer::SetDistributedTables(const std::vector &tables) return RDB_OK; } +std::vector RdbSyncer::GetConnectDevices() +{ + auto deviceInfos = AppDistributedKv::CommunicationProvider::GetInstance().GetRemoteNodesBasicInfo(); + std::vector devices; + for (const auto& deviceInfo : deviceInfos) { + devices.push_back(deviceInfo.deviceId); + } + ZLOGI("size=%{public}u", static_cast(devices.size())); + for (const auto& device: devices) { + ZLOGI("%{public}s", KvStoreUtils::ToBeAnonymous(device).c_str()); + } + return devices; +} + std::vector RdbSyncer::NetworkIdToUUID(const std::vector &networkIds) { std::vector uuids; for (const auto& networkId : networkIds) { auto uuid = CommunicationProvider::GetInstance().GetUuidByNodeId(networkId); if (uuid.empty()) { - ZLOGE("%{public}.6s failed", networkId.c_str()); + ZLOGE("%{public}s failed", KvStoreUtils::ToBeAnonymous(networkId).c_str()); continue; } uuids.push_back(uuid); + ZLOGI("%{public}s <--> %{public}s", KvStoreUtils::ToBeAnonymous(networkId).c_str(), + KvStoreUtils::ToBeAnonymous(uuid).c_str()); } return uuids; } @@ -261,9 +278,6 @@ DistributedDB::Query RdbSyncer::MakeQuery(const RdbPredicates &predicates) { ZLOGI("table=%{public}s", predicates.table_.c_str()); auto query = DistributedDB::Query::Select(predicates.table_); - for (const auto& device : predicates.devices_) { - ZLOGI("device=%{public}.6s", device.c_str()); - } for (const auto& operation : predicates.operations_) { if (operation.operator_ >= 0 && operation.operator_ < OPERATOR_MAX) { HANDLES[operation.operator_](operation, query); @@ -272,7 +286,7 @@ DistributedDB::Query RdbSyncer::MakeQuery(const RdbPredicates &predicates) return query; } -int32_t RdbSyncer::DoSync(const SyncOption &option, const RdbPredicates &predicates, SyncResult &result) +int32_t RdbSyncer::DoSync(const SyncOption &option, RdbPredicates &predicates, SyncResult &result) { ZLOGI("enter"); auto* delegate = GetDelegate(); @@ -281,7 +295,11 @@ int32_t RdbSyncer::DoSync(const SyncOption &option, const RdbPredicates &predica return RDB_ERROR; } + if (predicates.devices_.empty()) { + predicates.devices_ = GetConnectDevices(); + } auto devices = NetworkIdToUUID(predicates.devices_); + ZLOGI("delegate sync"); return delegate->Sync(devices, static_cast(option.mode), MakeQuery(predicates), [&result] (const auto& syncStatus) { @@ -289,14 +307,19 @@ int32_t RdbSyncer::DoSync(const SyncOption &option, const RdbPredicates &predica }, true); } -int32_t RdbSyncer::DoAsync(const SyncOption &option, const RdbPredicates &predicates, const SyncCallback& callback) +int32_t RdbSyncer::DoAsync(const SyncOption &option, RdbPredicates &predicates, const SyncCallback& callback) { auto* delegate = GetDelegate(); if (delegate == nullptr) { ZLOGE("delegate is nullptr"); return RDB_ERROR; } + + if (predicates.devices_.empty()) { + predicates.devices_ = GetConnectDevices(); + } auto devices = NetworkIdToUUID(predicates.devices_); + ZLOGI("delegate sync"); return delegate->Sync(devices, static_cast(option.mode), MakeQuery(predicates), [callback] (const auto& syncStatus) { diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.h b/services/distributeddataservice/service/rdb/rdb_syncer.h index b31adfb86..53ccc319e 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -45,9 +45,9 @@ public: int32_t SetDistributedTables(const std::vector& tables); - int32_t DoSync(const SyncOption& option, const RdbPredicates& predicates, SyncResult& result); + int32_t DoSync(const SyncOption& option, RdbPredicates& predicates, SyncResult& result); - int32_t DoAsync(const SyncOption& option, const RdbPredicates& predicates, const SyncCallback& callback); + int32_t DoAsync(const SyncOption& option, RdbPredicates& predicates, const SyncCallback& callback); private: std::string GetUserId() const; @@ -71,6 +71,7 @@ private: pid_t uid_ {}; uint32_t timerId_ {}; + static std::vector GetConnectDevices(); static std::vector NetworkIdToUUID(const std::vector& networkIds); static void HandleSyncStatus(const std::map>& SyncStatus, -- Gitee From e18aed611f009cae7b4703e30ec7a014943fe32f Mon Sep 17 00:00:00 2001 From: wuchunbo Date: Thu, 24 Feb 2022 21:30:23 +0800 Subject: [PATCH 2/2] optimize inAllDevice of rdb predicate Signed-off-by: wuchunbo --- .../service/rdb/rdb_syncer.cpp | 16 ++++++++++------ .../service/rdb/rdb_syncer.h | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/services/distributeddataservice/service/rdb/rdb_syncer.cpp index fc804baaa..a1813e3b2 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -286,7 +286,7 @@ DistributedDB::Query RdbSyncer::MakeQuery(const RdbPredicates &predicates) return query; } -int32_t RdbSyncer::DoSync(const SyncOption &option, RdbPredicates &predicates, SyncResult &result) +int32_t RdbSyncer::DoSync(const SyncOption &option, const RdbPredicates &predicates, SyncResult &result) { ZLOGI("enter"); auto* delegate = GetDelegate(); @@ -295,10 +295,12 @@ int32_t RdbSyncer::DoSync(const SyncOption &option, RdbPredicates &predicates, S return RDB_ERROR; } + std::vector devices; if (predicates.devices_.empty()) { - predicates.devices_ = GetConnectDevices(); + devices = NetworkIdToUUID(GetConnectDevices()); + } else { + devices = NetworkIdToUUID(predicates.devices_); } - auto devices = NetworkIdToUUID(predicates.devices_); ZLOGI("delegate sync"); return delegate->Sync(devices, static_cast(option.mode), @@ -307,7 +309,7 @@ int32_t RdbSyncer::DoSync(const SyncOption &option, RdbPredicates &predicates, S }, true); } -int32_t RdbSyncer::DoAsync(const SyncOption &option, RdbPredicates &predicates, const SyncCallback& callback) +int32_t RdbSyncer::DoAsync(const SyncOption &option, const RdbPredicates &predicates, const SyncCallback& callback) { auto* delegate = GetDelegate(); if (delegate == nullptr) { @@ -315,10 +317,12 @@ int32_t RdbSyncer::DoAsync(const SyncOption &option, RdbPredicates &predicates, return RDB_ERROR; } + std::vector devices; if (predicates.devices_.empty()) { - predicates.devices_ = GetConnectDevices(); + devices = NetworkIdToUUID(GetConnectDevices()); + } else { + devices = NetworkIdToUUID(predicates.devices_); } - auto devices = NetworkIdToUUID(predicates.devices_); ZLOGI("delegate sync"); return delegate->Sync(devices, static_cast(option.mode), diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.h b/services/distributeddataservice/service/rdb/rdb_syncer.h index 53ccc319e..23039ed1a 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -45,9 +45,9 @@ public: int32_t SetDistributedTables(const std::vector& tables); - int32_t DoSync(const SyncOption& option, RdbPredicates& predicates, SyncResult& result); + int32_t DoSync(const SyncOption& option, const RdbPredicates& predicates, SyncResult& result); - int32_t DoAsync(const SyncOption& option, RdbPredicates& predicates, const SyncCallback& callback); + int32_t DoAsync(const SyncOption& option, const RdbPredicates& predicates, const SyncCallback& callback); private: std::string GetUserId() const; -- Gitee