diff --git a/services/core/include/persistenceadapter/kvadapter/kv_adapter.h b/services/core/include/persistenceadapter/kvadapter/kv_adapter.h index c1fb4a7231018d86f960da0512798aab1d2d0217..a278c865984c00c00cc65104e837e6871030c210 100644 --- a/services/core/include/persistenceadapter/kvadapter/kv_adapter.h +++ b/services/core/include/persistenceadapter/kvadapter/kv_adapter.h @@ -51,7 +51,7 @@ public: std::map& values) override; int32_t Sync(const std::vector& deviceList, SyncMode syncMode) override; int32_t DeleteKvStore(); - void TriggerDynamicQuery(const std::string& udid); + void TriggerOnDemandQuery(const std::string& udid); private: DistributedKv::Status GetKvStorePtr(DistributedKv::DataType dataType); @@ -65,7 +65,6 @@ private: int32_t RegisterDeathListener(); int32_t UnRegisterDeathListener(); int32_t DeleteDeathListener(); - void SyncDeviceProfile(const std::string& udid); private: DistributedKv::AppId appId_; diff --git a/services/core/src/persistenceadapter/kvadapter/kv_adapter.cpp b/services/core/src/persistenceadapter/kvadapter/kv_adapter.cpp index 0981027d3325dcca75eba69e409b984d641caa21..e2c93d7dc4926af0152f860a99ff25f71412fa83 100644 --- a/services/core/src/persistenceadapter/kvadapter/kv_adapter.cpp +++ b/services/core/src/persistenceadapter/kvadapter/kv_adapter.cpp @@ -457,15 +457,15 @@ int32_t KVAdapter::DeleteDeathListener() return DP_SUCCESS; } -void KVAdapter::TriggerDynamicQuery(const std::string& udid) +void KVAdapter::TriggerOnDemandQuery(const std::string& udid) { - HILOGI("Trigger DynamicQuery, key :%{public}s", ProfileUtils::GetAnonyString(udid).c_str()); + HILOGI("Trigger ondemand, key :%{public}s", ProfileUtils::GetAnonyString(udid).c_str()); std::string networkId = ""; if (ProfileCache::GetInstance().GetNetWorkIdByUdid(udid, networkId) != DP_SUCCESS) { HILOGE("Can not find networkId by udid"); return; } - HILOGI("Try Sync Dynamic data with remote dev, networkId: %{public}s", + HILOGI("Try sync data with remote dev, networkId: %{public}s", ProfileUtils::GetAnonyString(networkId).c_str()); std::function call = [](DistributedKv::Status status, DistributedKv::Value &&value) { @@ -503,9 +503,8 @@ int32_t KVAdapter::GetByPrefix(const std::string& udid, const std::string& keyPr HILOGE("kvStoragePtr_ is null"); return DP_KV_DB_PTR_NULL; } - if ((this->dataType_ == DistributedKv::DataType::TYPE_DYNAMICAL) && - (ProfileCache::GetInstance().GetLocalUdid() != udid)) { - TriggerDynamicQuery(udid); + if (ProfileCache::GetInstance().GetLocalUdid() != udid) { + TriggerOnDemandQuery(udid); } // if prefix is empty, get all entries. DistributedKv::Key allEntryKeyPrefix(keyPrefix); @@ -517,13 +516,8 @@ int32_t KVAdapter::GetByPrefix(const std::string& udid, const std::string& keyPr HILOGE("Query data by keyPrefix failed, prefix: %{public}s", ProfileUtils::GetAnonyString(keyPrefix).c_str()); return DP_GET_KV_DB_FAIL; } - // data to be queried must exist, if not, try to sync. - if (allEntries.size() == 0) { - HILOGE("AllEntries is empty!"); - SyncDeviceProfile(udid); - return DP_INVALID_PARAMS; - } - if (allEntries.size() > MAX_DB_SIZE) { + + if (allEntries.size() == 0 || allEntries.size() > MAX_DB_SIZE) { HILOGE("AllEntries size is invalid!"); return DP_INVALID_PARAMS; } @@ -549,14 +543,13 @@ int32_t KVAdapter::Get(const std::string& udid, const std::string& key, std::str HILOGE("kvStoragePtr_ is null"); return DP_KV_DB_PTR_NULL; } - if ((this->dataType_ == DistributedKv::DataType::TYPE_DYNAMICAL) && - (ProfileCache::GetInstance().GetLocalUdid() != udid)) { - TriggerDynamicQuery(udid); + if (ProfileCache::GetInstance().GetLocalUdid() != udid) { + TriggerOnDemandQuery(udid); } status = kvStorePtr_->Get(kvKey, kvValue); } if (status == DistributedKv::Status::NOT_FOUND) { - SyncDeviceProfile(udid); + HILOGE("Not found key from kv."); return DP_GET_KV_DB_FAIL; } if (status != DistributedKv::Status::SUCCESS) { @@ -566,19 +559,5 @@ int32_t KVAdapter::Get(const std::string& udid, const std::string& key, std::str value = kvValue.ToString(); return DP_SUCCESS; } - -void KVAdapter::SyncDeviceProfile(const std::string& udid) -{ - HILOGI("call!"); - if (udid.empty()) { - HILOGE("udid is invalid."); - return; - } - std::vector device; - device.push_back(udid); - SyncMode syncMode{ SyncMode::PUSH_PULL }; - int32_t syncResult = Sync(device, syncMode); - HILOGI("SyncDeviceProfile res: %{public}d!", syncResult); -} } // namespace DeviceProfile } // namespace OHOS diff --git a/services/core/test/unittest/kv_adapter_test.cpp b/services/core/test/unittest/kv_adapter_test.cpp index 49ba7607bab72527250bb5166297955a9f2b673e..ec16af859481cf5ece08111d65e85ffb1920c3c9 100644 --- a/services/core/test/unittest/kv_adapter_test.cpp +++ b/services/core/test/unittest/kv_adapter_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2024 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 @@ -466,18 +466,13 @@ HWTEST_F(KVAdapterTest, Get005, TestSize.Level1) */ HWTEST_F(KVAdapterTest, Get006, TestSize.Level1) { - std::string peerUdid = "11111"; shared_ptr kvStore_; kvStore_= make_shared(APP_ID, STORE_ID, make_shared(), make_shared(), make_shared(), DistributedKv::TYPE_DYNAMICAL); - kvStore_->SyncDeviceProfile(peerUdid); - - peerUdid = ""; - kvStore_->SyncDeviceProfile(peerUdid); - + std::string udid = "11111"; std::string key = "11111"; std::string value = "";