From f9b256b9b92c59594369cc767844074a71a12b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=B7?= Date: Tue, 9 Sep 2025 10:40:55 +0800 Subject: [PATCH] add service kv MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张雷 --- common/BUILD.gn | 1 + .../distributed_device_profile_constants.h | 23 ++ .../interfaces/service_info_profile_new.h | 92 +++++ .../distributed_device_profile_constants.cpp | 24 ++ .../interfaces/service_info_profile_new.cpp | 191 ++++++++++ services/core/BUILD.gn | 2 + .../kvadapter/service_info_kv_adapter.h | 72 ++++ .../kvadapter/service_info_kv_adapter.cpp | 344 ++++++++++++++++++ 8 files changed, 749 insertions(+) create mode 100644 common/include/interfaces/service_info_profile_new.h create mode 100644 common/src/interfaces/service_info_profile_new.cpp create mode 100644 services/core/include/persistenceadapter/kvadapter/service_info_kv_adapter.h create mode 100644 services/core/src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp diff --git a/common/BUILD.gn b/common/BUILD.gn index 03c9c474..157e9195 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -105,6 +105,7 @@ ohos_shared_library("distributed_device_profile_common") { "src/interfaces/accessee.cpp", "src/interfaces/accesser.cpp", "src/interfaces/business_event.cpp", + "src/interfaces/service_info_profile_new.cpp", "src/interfaces/characteristic_profile.cpp", "src/interfaces/device_icon_info.cpp", "src/interfaces/device_icon_info_filter_options.cpp", diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index 8d067f8c..035859a4 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -121,6 +121,10 @@ extern const std::string GET_SERVICE_INFO_PROFILE_BY_UNIQUE_KEY; extern const std::string GET_SERVICE_INFO_PROFILE_LIST_BY_TOKEN_ID; extern const std::string GET_ALL_SERVICE_INFO_PROFILE_LIST; extern const std::string GET_SERVICE_INFO_PROFILE_LIST_BY_BUNDLE_NAME; +extern const std::string REG_SERVICE_ID; +extern const std::string PUBLISH_STATE; +extern const std::string SERVICE_INFO; +extern const std::string SERVICE_IS_MULTI_USER; /* LocalServiceInfo Attribute */ extern const std::string PUT_LOCAL_SERVICE_INFO; extern const std::string UPDATE_LOCAL_SERVICE_INFO; @@ -318,6 +322,25 @@ extern const std::string LNN_ACL_TRUE; extern const std::string BUSINESS_KEY; extern const std::string BUSINESS_VALUE; +extern const std::string SERVICE_ID_KEY; +extern const std::string SERVICE_ID_VALUE; +extern const std::string SERVICE_TYPE_KEY; +extern const std::string SERVICE_TYPE_VALUE; +extern const std::string SERVICE_NAME_KEY; +extern const std::string SERVICE_NAME_VALUE; +extern const std::string SERVICE_DISPLAY_NAME_KEY; +extern const std::string SERVICE_DISPLAY_NAME_VALUE; +extern const std::string REG_SERVICE_ID_KEY; +extern const std::string REG_SERVICE_ID_VALUE; +extern const std::string SER_PUB_STATE_KEY; +extern const std::string SER_PUB_STATE_VALUE; +extern const std::string DEVICE_ID_KEY; +extern const std::string DEVICE_ID_VALUE; +extern const std::string SER_USER_ID_KEY; +extern const std::string SER_USER_ID_VALUE; +extern const std::string SER_TOKEN_ID_KEY; +extern const std::string SER_TOKEN_ID_VALUE; + constexpr int32_t ROWCNT_INIT = -1; constexpr int32_t RET_INIT = -1; constexpr int32_t ROWCOUNT_INIT = -1; diff --git a/common/include/interfaces/service_info_profile_new.h b/common/include/interfaces/service_info_profile_new.h new file mode 100644 index 00000000..0199838c --- /dev/null +++ b/common/include/interfaces/service_info_profile_new.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2025 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. + */ + +#ifndef OHOS_DP_SERVICE_INFO_H +#define OHOS_DP_SERVICE_INFO_H + +#include +#include + +#include "distributed_device_profile_constants.h" +#include "dp_parcel.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class ServiceInfoProfileNew : public DpParcel { +public: + ServiceInfoProfileNew() + : regServiceId_(0), + deviceId_(""), + isMultiUser_(false), + userId_(0), + tokenId_(0), + publishState_(0), + serviceId_(0), + serviceType_(""), + serviceName_(""), + serviceDisplayName_("") + {}; + + ~ServiceInfoProfileNew(); + + int32_t GetRegServiceId() const; + void SetRegServiceId(const int32_t regServiceIdValue); + + std::string GetDeviceId() const; + void SetDeviceId(const std::string &deviceIdValue); + + bool GetIsMultiUser() const; + void SetIsMultiUser(bool isMultiUser); + + int32_t GetUserId() const; + void SetUserId(const int32_t userIdValue); + + int64_t GetTokenId() const; + void SetTokenId(const int64_t tokenIdValue); + + int8_t GetSerPubState() const; + void SetSerPubState(const int8_t serPubStateValue); + + int64_t GetServiceId() const; + void SetServiceId(const int64_t serviceIdValue); + + std::string GetServiceType() const; + void SetServiceType(const std::string &serviceTypeValue); + + std::string GetServiceName() const; + void SetServiceName(const std::string &serviceNameValue); + + std::string GetServiceDisplayName() const; + void SetServiceDisplayName(const std::string &serviceDisplayNameValue); + + bool Marshalling(MessageParcel& parcel) const override; + bool UnMarshalling(MessageParcel& parcel) override; + std::string dump() const override; + +private: + int32_t regServiceId_; + std::string deviceId_; + bool isMultiUser_; + int32_t userId_; + int64_t tokenId_; + int8_t publishState_; + int64_t serviceId_; + std::string serviceType_; + std::string serviceName_; + std::string serviceDisplayName_; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif //OHOS_DP_SERVICE_INFO_H \ No newline at end of file diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index e86ea9c0..72ed6f4c 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -168,6 +168,10 @@ const std::string GET_SERVICE_INFO_PROFILE_BY_UNIQUE_KEY = "GetServiceInfoProfil const std::string GET_SERVICE_INFO_PROFILE_LIST_BY_TOKEN_ID = "GetServiceInfoProfileListByTokenId"; const std::string GET_ALL_SERVICE_INFO_PROFILE_LIST = "GetAllServiceInfoProfileList"; const std::string GET_SERVICE_INFO_PROFILE_LIST_BY_BUNDLE_NAME = "GetServiceInfoProfileListByBundleName"; +const std::string REG_SERVICE_ID = "regServiceId"; +const std::string PUBLISH_STATE = "publishState"; +const std::string SERVICE_INFO = "serviceInfo"; +const std::string SERVICE_IS_MULTI_USER = "isMultiUser"; /* LocalServiceInfo Attribute */ const std::string PUT_LOCAL_SERVICE_INFO = "PutLocalServiceInfo"; const std::string UPDATE_LOCAL_SERVICE_INFO = "UpdateLocalServiceInfo"; @@ -246,6 +250,26 @@ const std::string LNN_ACL_TRUE = "true"; const std::string BUSINESS_KEY = "businessKey"; const std::string BUSINESS_VALUE = "businessValue"; +//service info profile keys value +const std::string SERVICE_ID_KEY = "serviceIdKey"; +const std::string SERVICE_ID_VALUE = "serviceIdValue"; +const std::string SERVICE_TYPE_KEY = "serviceTypeKey"; +const std::string SERVICE_TYPE_VALUE = "serviceTypeValue"; +const std::string SERVICE_NAME_KEY = "serviceNameKey"; +const std::string SERVICE_NAME_VALUE = "serviceNameValue_"; +const std::string SERVICE_DISPLAY_NAME_KEY = "serviceDisplayNameKey"; +const std::string SERVICE_DISPLAY_NAME_VALUE = "serviceDisplayNameValue"; +const std::string REG_SERVICE_ID_KEY = "regServiceIdKey"; +const std::string REG_SERVICE_ID_VALUE = "regServiceIdValue"; +const std::string SER_PUB_STATE_KEY = "serPubStateKey"; +const std::string SER_PUB_STATE_VALUE = "serPubStateValue"; +const std::string DEVICE_ID_KEY = "deviceIdKey"; +const std::string DEVICE_ID_VALUE = "deviceIdValue"; +const std::string SER_USER_ID_KEY = "userIdKey"; +const std::string SER_USER_ID_VALUE = "userIdValue"; +const std::string SER_TOKEN_ID_KEY = "tokenIdKey"; +const std::string SER_TOKEN_ID_VALUE = "tokenIdValue"; + const std::string CREATE_TURST_DEVICE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS trust_device_table\ (\ deviceId TEXT PRIMARY KEY,\ diff --git a/common/src/interfaces/service_info_profile_new.cpp b/common/src/interfaces/service_info_profile_new.cpp new file mode 100644 index 00000000..15a16bf7 --- /dev/null +++ b/common/src/interfaces/service_info_profile_new.cpp @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "service_info_profile_new.h" +#include "cJSON.h" +#include "distributed_device_profile_constants.h" +#include "macro_utils.h" +#include "profile_utils.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "ServiceInfoProfileNew"; +} + +ServiceInfoProfileNew::~ServiceInfoProfileNew() +{ +} + +int64_t ServiceInfoProfileNew::GetServiceId() const +{ + return serviceId_; +} + +void ServiceInfoProfileNew::SetServiceId(const int64_t serviceId) +{ + serviceId_ = serviceId; +} + +std::string ServiceInfoProfileNew::GetServiceType() const +{ + return serviceType_; +} + +void ServiceInfoProfileNew::SetServiceType(const std::string& serviceType) +{ + serviceType_ = serviceType; +} + +std::string ServiceInfoProfileNew::GetServiceName() const +{ + return serviceName_; +} + +void ServiceInfoProfileNew::SetServiceName(const std::string &serviceName) +{ + serviceName_ = serviceName; +} + +std::string ServiceInfoProfileNew::GetServiceDisplayName() const +{ + return serviceDisplayName_; +} + +void ServiceInfoProfileNew::SetServiceDisplayName(const std::string &serviceDisplayName) +{ + serviceDisplayName_ = serviceDisplayName; +} + +int32_t ServiceInfoProfileNew::GetRegServiceId() const +{ + return regServiceId_; +} + +void ServiceInfoProfileNew::SetRegServiceId(const int32_t regServiceId) +{ + regServiceId_ = regServiceId; +} + +int8_t ServiceInfoProfileNew::GetSerPubState() const +{ + return publishState_; +} + +void ServiceInfoProfileNew::SetSerPubState(const int8_t serPubState) +{ + publishState_ = serPubState; +} + +std::string ServiceInfoProfileNew::GetDeviceId() const +{ + return deviceId_; +} +void ServiceInfoProfileNew::SetDeviceId(const std::string &deviceId) +{ + deviceId_ = deviceId; +} + +int32_t ServiceInfoProfileNew::GetUserId() const +{ + return userId_; +} +void ServiceInfoProfileNew::SetUserId(const int32_t userId) +{ + userId_ = userId; +} + +int64_t ServiceInfoProfileNew::GetTokenId() const +{ + return tokenId_; +} +void ServiceInfoProfileNew::SetTokenId(const int64_t tokenId) +{ + tokenId_ = tokenId; +} + +bool ServiceInfoProfileNew::GetIsMultiUser() const +{ + return isMultiUser_; +} + +void ServiceInfoProfileNew::SetIsMultiUser(bool isMultiUser) +{ + isMultiUser_ = isMultiUser; +} + +bool ServiceInfoProfileNew::Marshalling(MessageParcel& parcel) const +{ + WRITE_HELPER(parcel, Int32, regServiceId_); + WRITE_HELPER(parcel, String, deviceId_); + WRITE_HELPER(parcel, Bool, isMultiUser_); + WRITE_HELPER(parcel, Int32, userId_); + WRITE_HELPER(parcel, Int64, tokenId_); + WRITE_HELPER(parcel, Int8, publishState_); + WRITE_HELPER(parcel, Int64, serviceId_); + WRITE_HELPER(parcel, String, serviceType_); + WRITE_HELPER(parcel, String, serviceName_); + WRITE_HELPER(parcel, String, serviceDisplayName_); + + return true; +} + +bool ServiceInfoProfileNew::UnMarshalling(MessageParcel& parcel) +{ + READ_HELPER_RET(parcel, Int32, regServiceId_, false); + READ_HELPER_RET(parcel, String, deviceId_, false); + READ_HELPER_RET(parcel, Bool, isMultiUser_, false); + READ_HELPER_RET(parcel, Int32, userId_, false); + READ_HELPER_RET(parcel, Int64, tokenId_, false); + READ_HELPER_RET(parcel, Int8, publishState_, false); + READ_HELPER_RET(parcel, Int64, serviceId_, false); + READ_HELPER_RET(parcel, String, serviceType_, false); + READ_HELPER_RET(parcel, String, serviceName_, false); + READ_HELPER_RET(parcel, String, serviceDisplayName_, false); + + return true; +} + +std::string ServiceInfoProfileNew::dump() const +{ + cJSON* json = cJSON_CreateObject(); + if (!cJSON_IsObject(json)) { + cJSON_Delete(json); + HILOGE("cJSON CreateObject error!"); + return EMPTY_STRING; + } + cJSON_AddStringToObject(json, DEVICE_ID.c_str(), deviceId_.c_str()); + cJSON_AddStringToObject(json, SERVICE_IS_MULTI_USER.c_str(), std::to_string(isMultiUser_).c_str()); + cJSON_AddStringToObject(json, USERID.c_str(), std::to_string(userId_).c_str()); + cJSON_AddStringToObject(json, TOKENID.c_str(), std::to_string(tokenId_).c_str()); + cJSON_AddStringToObject(json, PUBLISH_STATE.c_str(), std::to_string(publishState_).c_str()); + cJSON_AddStringToObject(json, SISERVICE_ID.c_str(), std::to_string(serviceId_).c_str()); + cJSON_AddStringToObject(json, SERVICE_TYPE.c_str(), serviceType_.c_str()); + cJSON_AddStringToObject(json, SERVICE_NAME.c_str(), serviceName_.c_str()); + cJSON_AddStringToObject(json, SERVICE_DISPLAY_NAME.c_str(), serviceDisplayName_.c_str()); + + char* jsonChars = cJSON_PrintUnformatted(json); + if (jsonChars == NULL) { + cJSON_Delete(json); + HILOGE("cJSON formatted to string failed!"); + return EMPTY_STRING; + } + std::string jsonStr = jsonChars; + cJSON_Delete(json); + cJSON_free(jsonChars); + return jsonStr; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index 18ec16e8..cbe9e5fc 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -38,6 +38,7 @@ config("device_info_manager_config") { "include/profiledatamanager", "include/publishcommonevent", "include/serviceinfoprofile_manager", + "include/serviceinfo_manager", "include/sessionkeymanager", "include/subscribeprofilemanager", "include/staticcapabilitycollector", @@ -112,6 +113,7 @@ ohos_shared_library("distributed_device_profile_svr") { "src/persistenceadapter/assetadapter/asset_adapter.cpp", "src/persistenceadapter/kvadapter/business_event_adapter.cpp", "src/persistenceadapter/kvadapter/kv_adapter.cpp", + "src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp", "src/persistenceadapter/kvadapter/switch_adapter.cpp", "src/persistenceadapter/rdbadapter/local_service_info_rdb_adapter.cpp", "src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp", diff --git a/services/core/include/persistenceadapter/kvadapter/service_info_kv_adapter.h b/services/core/include/persistenceadapter/kvadapter/service_info_kv_adapter.h new file mode 100644 index 00000000..0385b479 --- /dev/null +++ b/services/core/include/persistenceadapter/kvadapter/service_info_kv_adapter.h @@ -0,0 +1,72 @@ +/* +* Copyright (c) 2025 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. +*/ + +#ifndef OHOS_DM_SERVICE_KV_ADAPTER_H +#define OHOS_DM_SERVICE_KV_ADAPTER_H + +#include +#include +#include +#include +#include +#include +#include + +#include "distributed_kv_data_manager.h" +#include "ikv_adapter.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class ServiceInfoKvAdapter : public DistributedDeviceProfile::IKVAdapter { +public: + ServiceInfoKvAdapter( + const std::shared_ptr &deathListener, DistributedKv::DataType dataType); + + int32_t Init() override; + int32_t UnInit() override; + int32_t ReInit(); + int32_t Put(const std::string& key, const std::string& value) override; + int32_t Get(const std::string& key, std::string& value) override; + int32_t DeleteBatch(const std::vector& keys) override; + int32_t PutBatch(const std::map& values) override; + int32_t Delete(const std::string& key) override; + int32_t DeleteByPrefix(const std::string& keyPrefix) override; + int32_t GetByPrefix(const std::string& keyPrefix, std::map& values) override; + int32_t Sync(const std::vector& deviceList, SyncMode syncMode) override; + int32_t GetDeviceEntries(const std::string& udid, std::map& values) override; + int32_t RemoveDeviceData(const std::string& uuid) override; + void OnRemoteDied(); + +private: + DistributedKv::Status GetKvStorePtr(); + int32_t RegisterKvStoreDeathListener(); + int32_t UnregisterKvStoreDeathListener(); + +private: + DistributedKv::AppId appId_; + DistributedKv::StoreId storeId_; + DistributedKv::DistributedKvDataManager kvDataMgr_; + DistributedKv::DataType dataType_ = DistributedKv::DataType::TYPE_STATICS; + std::shared_ptr kvStorePtr_ = nullptr; + std::shared_ptr deathRecipient_ = nullptr; + + std::mutex serviceInfoAdapterMutex_; + std::atomic isInited_ = false; + std::mutex kvAdapterMutex_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_SERVICE_KV_ADAPTER_H \ No newline at end of file diff --git a/services/core/src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp b/services/core/src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp new file mode 100644 index 00000000..4f759a88 --- /dev/null +++ b/services/core/src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp @@ -0,0 +1,344 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "service_info_kv_adapter.h" + +#include +#include + +#include "datetime_ex.h" +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "ffrt.h" +#include "profile_cache.h" +#include "string_ex.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace OHOS::DistributedKv; +namespace { + constexpr int32_t MAX_INIT_RETRY_TIMES = 30; + constexpr int32_t INIT_RETRY_SLEEP_INTERVAL = 200 * 1000; + constexpr uint32_t MAX_BATCH_SIZE = 128; + const std::string DATABASE_DIR = "/data/service/el1/public/database/distributed_device_profile_service"; + const std::string TAG = "ServiceInfoKvAdapter"; + const std::string APP_ID = "distributed_device_profile_service"; + const std::string STORE_ID = "dp_kv_store_service_info_profile"; +} + +ServiceInfoKvAdapter::ServiceInfoKvAdapter( + const std::shared_ptr &deathListener, DistributedKv::DataType dataType) +{ + this->deathRecipient_ = deathListener; + this->dataType_ = dataType; + HILOGD("ServiceInfoKvAdapter Constructor Success"); +} + +int32_t ServiceInfoKvAdapter::Init() +{ + HILOGI("ServiceInfoKvAdapter Init local DB, dataType: %{public}d", static_cast(dataType_)); + if (isInited_.load()) { + HILOGI("Local DB already inited."); + return DP_SUCCESS; + } + this->appId_.appId = APP_ID; + this->storeId_.storeId = STORE_ID; + int32_t tryTimes = MAX_INIT_RETRY_TIMES; + int64_t beginTime = GetTickCount(); + DistributedKv::Status status = GetKvStorePtr(); + while (tryTimes > 0) { + if (status == DistributedKv::Status::SUCCESS && kvStorePtr_ != nullptr) { + HILOGI("Init KvStorePtr Success"); + RegisterKvStoreDeathListener(); + isInited_.store(true); + return DP_SUCCESS; + } + HILOGI("CheckKvStore, left times: %{public}d, status: %{public}d", tryTimes, status); + usleep(INIT_RETRY_SLEEP_INTERVAL); + tryTimes--; + } + if ((kvStorePtr_ == nullptr) || (status != DistributedKv::Status::SUCCESS)) { + HILOGE("kvStorePtr is nullptr!"); + return DP_KV_DB_PTR_NULL; + } + isInited_.store(true); + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::UnInit() +{ + HILOGI("ServiceInfoKvAdapter UnInit"); + if (isInited_.load()) { + if (kvStorePtr_ == nullptr) { + HILOGE("kvStorePtr is nullptr!"); + return DP_KV_DB_PTR_NULL; + } + UnregisterKvStoreDeathListener(); + kvStorePtr_.reset(); + isInited_.store(false); + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::ReInit() +{ + HILOGI("ServiceInfoKvAdapter ReInit"); + UnInit(); + return Init(); +} + +int32_t ServiceInfoKvAdapter::Put(const std::string& key, const std::string& value) +{ + HILOGI("ServiceInfoKvAdapter::Put"); + if (key.empty() || key.size() > MAX_STRING_LEN || value.empty() || value.size() > MAX_STRING_LEN) { + HILOGE("Param is invalid: key or value is empty or too long"); + return DP_INVALID_PARAMS; + } + + DistributedKv::Status status; + { + std::lock_guard lock(kvAdapterMutex_); + if (kvStorePtr_ == nullptr) { + HILOGE("kvDBPtr is null!"); + return DP_KV_DB_PTR_NULL; + } + DistributedKv::Key kvKey(key); + DistributedKv::Value kvValue(value); + status = kvStorePtr_->Put(kvKey, kvValue); + } + + if (status != DistributedKv::Status::SUCCESS) { + HILOGE("Put kv to db failed, ret: %{public}d", status); + return DP_PUT_KV_DB_FAIL; + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::Get(const std::string& key, std::string& value) +{ + DistributedKv::Key kvKey(key); + DistributedKv::Value kvValue; + DistributedKv::Status status; + { + std::lock_guard lock(kvAdapterMutex_); + if (kvStorePtr_ == nullptr) { + HILOGE("kvStoragePtr_ is null"); + return DP_KV_DB_PTR_NULL; + } + status = kvStorePtr_->Get(kvKey, kvValue); + } + if (status != DistributedKv::Status::SUCCESS) { + HILOGE("Get data from kv failed, key: %{public}s", ProfileUtils::GetDbKeyAnonyString(key).c_str()); + return DP_GET_KV_DB_FAIL; + } + value = kvValue.ToString(); + return DP_SUCCESS; +} + +void ServiceInfoKvAdapter::OnRemoteDied() +{ + HILOGI("ServiceInfoKvAdapter OnRemoteDied, recover db begin"); + auto reInitTask = [this]() { + HILOGI("ReInit, storeId:%{public}s", storeId_.storeId.c_str()); + ReInit(); + }; + ffrt::submit(reInitTask); +} + +DistributedKv::Status ServiceInfoKvAdapter::GetKvStorePtr() +{ + HILOGI("ServiceInfoKvAdapter::GetKvStorePtr"); + DistributedKv::Options options = { + .createIfMissing = true, + .encrypt = false, + .autoSync = false, + .securityLevel = DistributedKv::SecurityLevel::S1, + .area = 1, + .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION, + .baseDir = DATABASE_DIR + }; + DistributedKv::Status status; + { + std::lock_guard lock(kvAdapterMutex_); + status = kvDataMgr_.GetSingleKvStore(options, appId_, storeId_, kvStorePtr_); + if (status == DistributedKv::Status::SUCCESS && kvStorePtr_ == nullptr) { + status = DistributedKv::Status::ERROR; + } + } + return status; +} + +int32_t ServiceInfoKvAdapter::RegisterKvStoreDeathListener() +{ + HILOGI("ServiceInfoKvAdapter::RegisterKvStoreDeathListener Start."); + { + std::lock_guard lock(kvAdapterMutex_); + kvDataMgr_.RegisterKvStoreServiceDeathRecipient(deathRecipient_); + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::UnregisterKvStoreDeathListener() +{ + HILOGI("ServiceInfoKvAdapter::UnregisterKvStoreDeathListener Start."); + { + std::lock_guard lock(kvAdapterMutex_); + kvDataMgr_.UnRegisterKvStoreServiceDeathRecipient(deathRecipient_); + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::Delete(const std::string& key) +{ + DistributedKv::Status status; + { + std::lock_guard lock(kvAdapterMutex_); + if (kvStorePtr_ == nullptr) { + HILOGE("kvDBPtr is null!"); + return DP_KV_DB_PTR_NULL; + } + DistributedKv::Key kvKey(key); + status = kvStorePtr_->Delete(kvKey); + } + if (status != DistributedKv::Status::SUCCESS) { + HILOGE("Delete kv by key failed!"); + return DP_DEL_KV_DB_FAIL; + } + HILOGD("Delete kv by key success!"); + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::DeleteBatch(const std::vector& keys) +{ + if (keys.empty() || keys.size() > MAX_PROFILE_SIZE) { + HILOGE("keys size(%{public}zu) is invalid!", keys.size()); + return DP_INVALID_PARAMS; + } + + uint32_t keysSize = static_cast(keys.size()); + std::vector> delKeyBatches; + for (uint32_t i = 0; i < keysSize; i += MAX_BATCH_SIZE) { + uint32_t end = (i + MAX_BATCH_SIZE) > keysSize ? keysSize : (i + MAX_BATCH_SIZE); + auto batch = std::vector(keys.begin() + i, keys.begin() + end); + std::vector delKeys; + for (auto item : batch) { + DistributedKv::Key key(item); + delKeys.emplace_back(key); + } + delKeyBatches.emplace_back(delKeys); + } + { + std::lock_guard lock(kvAdapterMutex_); + if (kvStorePtr_ == nullptr) { + HILOGE("kvStorePtr is nullptr!"); + return DP_KV_DB_PTR_NULL; + } + for (auto delKeys : delKeyBatches) { + DistributedKv::Status status = kvStorePtr_->DeleteBatch(delKeys); + if (status != DistributedKv::Status::SUCCESS) { + HILOGE("DeleteBatch failed!"); + return DP_DEL_KV_DB_FAIL; + } + } + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::PutBatch(const std::map& values) +{ + if (values.empty() || values.size() > MAX_PROFILE_SIZE) { + HILOGE("Param is invalid!"); + return DP_INVALID_PARAMS; + } + DistributedKv::Status status; + { + std::lock_guard lock(kvAdapterMutex_); + if (kvStorePtr_ == nullptr) { + HILOGE("kvDBPtr is null!"); + return DP_KV_DB_PTR_NULL; + } + std::vector entries; + DistributedKv::Value oldV; + DistributedKv::Key kvKey; + for (auto item : values) { + kvKey = item.first; + Entry entry; + entry.key = kvKey; + entry.value = item.second; + entries.emplace_back(entry); + } + status = kvStorePtr_->PutBatch(entries); + } + if (status != DistributedKv::Status::SUCCESS) { + HILOGE("PutBatch kv to db failed, ret: %{public}d", status); + return DP_PUT_KV_DB_FAIL; + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::GetByPrefix(const std::string& keyPrefix, std::map& values) +{ + HILOGI("key prefix: %{public}s", ProfileUtils::GetDbKeyAnonyString(keyPrefix).c_str()); + std::lock_guard lock(kvAdapterMutex_); + if (kvStorePtr_ == nullptr) { + HILOGE("kvStoragePtr_ is null"); + return DP_KV_DB_PTR_NULL; + } + DistributedKv::Key allEntryKeyPrefix(keyPrefix); + std::vector allEntries; + DistributedKv::Status status = kvStorePtr_->GetEntries(allEntryKeyPrefix, allEntries); + if (status != DistributedKv::Status::SUCCESS) { + HILOGE("Query data by keyPrefix failed, prefix: %{public}s", + ProfileUtils::GetDbKeyAnonyString(keyPrefix).c_str()); + return DP_GET_KV_DB_FAIL; + } + if (allEntries.size() == 0 || allEntries.size() > MAX_DB_SIZE) { + HILOGE("AllEntries size is invalid!size: %{public}zu! prefix: %{public}s", + allEntries.size(), ProfileUtils::GetDbKeyAnonyString(keyPrefix).c_str()); + return DP_INVALID_PARAMS; + } + for (const auto& item : allEntries) { + values[item.key.ToString()] = item.value.ToString(); + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::DeleteByPrefix(const std::string& keyPrefix) +{ + (void)keyPrefix; + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::Sync(const std::vector& deviceList, SyncMode syncMode) +{ + (void)deviceList; + (void)syncMode; + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::GetDeviceEntries(const std::string& udid, std::map& values) +{ + (void)udid; + (void)values; + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::RemoveDeviceData(const std::string& uuid) +{ + (void)uuid; + return DP_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file -- Gitee