From acec65e46ee4e2ec760bb5e761996d9adbbcf1f9 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 1 Apr 2024 17:55:46 +0800 Subject: [PATCH 01/15] =?UTF-8?q?kv=5Fstore=E4=B8=8A=E4=BA=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../include/metadata/store_meta_data.h | 2 + .../include/metadata/store_meta_data_local.h | 2 + .../framework/include/store/general_store.h | 28 +- .../framework/include/store/general_watcher.h | 11 +- .../framework/include/store/store_info.h | 2 + .../framework/metadata/store_meta_data.cpp | 4 + .../metadata/store_meta_data_local.cpp | 4 + .../distributeddataservice/service/BUILD.gn | 5 +- .../service/cloud/sync_manager.cpp | 57 +- .../service/kvdb/kvdb_exporter.cpp | 8 +- .../service/kvdb/kvdb_general_store.cpp | 545 ++++++++++++++++++ .../service/kvdb/kvdb_general_store.h | 134 +++++ .../service/kvdb/kvdb_query.h | 66 +++ .../service/kvdb/kvdb_service_impl.cpp | 283 ++++++--- .../service/kvdb/kvdb_service_impl.h | 20 +- .../service/kvdb/kvdb_service_stub.cpp | 18 + .../service/kvdb/kvdb_service_stub.h | 1 + .../service/kvdb/kvdb_watcher.cpp | 82 +++ .../service/kvdb/kvdb_watcher.h | 42 ++ .../service/kvdb/store_cache.cpp | 300 ---------- .../service/kvdb/store_cache.h | 87 --- .../service/kvdb/upgrade.cpp | 10 +- .../service/rdb/rdb_general_store.cpp | 45 +- .../service/rdb/rdb_general_store.h | 2 +- .../fuzztest/kvdbservicestub_fuzzer/BUILD.gn | 3 +- 25 files changed, 1213 insertions(+), 548 deletions(-) create mode 100644 services/distributeddataservice/service/kvdb/kvdb_general_store.cpp create mode 100644 services/distributeddataservice/service/kvdb/kvdb_general_store.h create mode 100644 services/distributeddataservice/service/kvdb/kvdb_query.h create mode 100644 services/distributeddataservice/service/kvdb/kvdb_watcher.cpp create mode 100644 services/distributeddataservice/service/kvdb/kvdb_watcher.h delete mode 100644 services/distributeddataservice/service/kvdb/store_cache.cpp delete mode 100644 services/distributeddataservice/service/kvdb/store_cache.h diff --git a/services/distributeddataservice/framework/include/metadata/store_meta_data.h b/services/distributeddataservice/framework/include/metadata/store_meta_data.h index fdcb3d49a..dc67495da 100644 --- a/services/distributeddataservice/framework/include/metadata/store_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/store_meta_data.h @@ -35,6 +35,8 @@ struct API_EXPORT StoreMetaData final : public Serializable { bool isManualClean = false; bool isSearchable = false; bool isNeedCompress = false; + bool cloudSync = false; + bool isPublic = false; int32_t storeType = -1; int32_t securityLevel = 0; int32_t area = 0; diff --git a/services/distributeddataservice/framework/include/metadata/store_meta_data_local.h b/services/distributeddataservice/framework/include/metadata/store_meta_data_local.h index b91701fcd..3cabd4360 100644 --- a/services/distributeddataservice/framework/include/metadata/store_meta_data_local.h +++ b/services/distributeddataservice/framework/include/metadata/store_meta_data_local.h @@ -36,6 +36,8 @@ struct API_EXPORT StoreMetaDataLocal final : public Serializable { bool isBackup = false; bool isDirty = false; bool isEncrypt = false; + bool isPublic = false; + bool cloudSync = false; std::string dataDir = ""; std::string schema = ""; std::vector policies {}; diff --git a/services/distributeddataservice/framework/include/store/general_store.h b/services/distributeddataservice/framework/include/store/general_store.h index a3be9f708..92e56245f 100644 --- a/services/distributeddataservice/framework/include/store/general_store.h +++ b/services/distributeddataservice/framework/include/store/general_store.h @@ -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 @@ -19,10 +19,10 @@ #include #include +#include "snapshot/snapshot.h" #include "store/cursor.h" #include "store/general_value.h" #include "store/general_watcher.h" -#include "snapshot/snapshot.h" namespace OHOS::DistributedData { class CloudDB; class AssetLoader; @@ -43,7 +43,9 @@ public: CLOUD_NATIVE_FIRST, CLOUD_ClOUD_FIRST, CLOUD_END, - MODE_BUTT = CLOUD_END, + NEARBY_SUBSCRIBE_REMOTE, + NEARBY_UNSUBSCRIBE_REMOTE, + MODE_BUTT, }; enum HighMode : uint32_t { MANUAL_SYNC_MODE = 0x00000, @@ -77,22 +79,28 @@ public: : db_(std::move(db)), loader_(std::move(loader)) { } + + bool operator<(const BindInfo &bindInfo) const + { + return db_ < bindInfo.db_; + } + std::shared_ptr db_; std::shared_ptr loader_; }; virtual ~GeneralStore() = default; - virtual int32_t Bind(const Database &database, BindInfo bindInfo) = 0; + virtual int32_t Bind(const std::map> &cloudDBs) = 0; virtual bool IsBound() = 0; virtual int32_t Execute(const std::string &table, const std::string &sql) = 0; - virtual int32_t SetDistributedTables(const std::vector &tables, int type, - const std::vector &references) = 0; + virtual int32_t SetDistributedTables( + const std::vector &tables, int type, const std::vector &references) = 0; - virtual int32_t SetTrackerTable(const std::string& tableName, const std::set& trackerColNames, - const std::string& extendColName) = 0; + virtual int32_t SetTrackerTable(const std::string &tableName, const std::set &trackerColNames, + const std::string &extendColName) = 0; virtual int32_t Insert(const std::string &table, VBuckets &&values) = 0; @@ -125,11 +133,11 @@ public: virtual int32_t AddRef() = 0; - virtual int32_t Release() = 0; + virtual int32_t Release() = 0; virtual int32_t BindSnapshots(std::shared_ptr>> bindAssets) = 0; - virtual int32_t MergeMigratedData(const std::string &tableName, VBuckets&& values) = 0; + virtual int32_t MergeMigratedData(const std::string &tableName, VBuckets &&values) = 0; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_STORE_H diff --git a/services/distributeddataservice/framework/include/store/general_watcher.h b/services/distributeddataservice/framework/include/store/general_watcher.h index bcf226d66..2d507ca6f 100644 --- a/services/distributeddataservice/framework/include/store/general_watcher.h +++ b/services/distributeddataservice/framework/include/store/general_watcher.h @@ -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 @@ -16,6 +16,7 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_WATCHER_H #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_WATCHER_H #include + #include "store/general_value.h" #include "visibility.h" namespace OHOS::DistributedData { @@ -51,9 +52,15 @@ public: // PK primary key using PRIValue = std::variant; using PRIFields = std::map; + using Fields = std::map>; using ChangeInfo = std::map[OP_BUTT]>; + using ChangeData = std::map[OP_BUTT]>; virtual ~GeneralWatcher() = default; virtual int32_t OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) = 0; + virtual int32_t OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas) + { + return GeneralError::E_OK; + }; }; -} +} // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_WATCHER_H diff --git a/services/distributeddataservice/framework/include/store/store_info.h b/services/distributeddataservice/framework/include/store/store_info.h index 8138955aa..0e3dbb6f6 100644 --- a/services/distributeddataservice/framework/include/store/store_info.h +++ b/services/distributeddataservice/framework/include/store/store_info.h @@ -26,6 +26,8 @@ struct StoreInfo { int32_t instanceId = 0; int32_t user = 0; std::string deviceId; + bool isPublic = false; + bool cloudSync = false; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_INFO_H diff --git a/services/distributeddataservice/framework/metadata/store_meta_data.cpp b/services/distributeddataservice/framework/metadata/store_meta_data.cpp index 31bb8b99f..47915b3fd 100644 --- a/services/distributeddataservice/framework/metadata/store_meta_data.cpp +++ b/services/distributeddataservice/framework/metadata/store_meta_data.cpp @@ -51,6 +51,8 @@ bool StoreMetaData::Marshal(json &node) const SetValue(node[GET_NAME(storeId)], storeId); SetValue(node[GET_NAME(user)], user); SetValue(node[GET_NAME(account)], account); + SetValue(node[GET_NAME(isPublic)], isPublic); + SetValue(node[GET_NAME(cloudSync)], cloudSync); // compatible with the versions which lower than VERSION_TAG_0000 SetValue(node[GET_NAME(kvStoreType)], storeType); @@ -88,6 +90,8 @@ bool StoreMetaData::Unmarshal(const json &node) GetValue(node, GET_NAME(storeId), storeId); GetValue(node, GET_NAME(user), user); GetValue(node, GET_NAME(account), account); + GetValue(node, GET_NAME(isPublic), isPublic); + GetValue(node, GET_NAME(cloudSync), cloudSync); // compatible with the older versions if (version < FIELD_CHANGED_TAG) { diff --git a/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp b/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp index 4b1cdf06a..c3352a22b 100644 --- a/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp +++ b/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp @@ -67,6 +67,8 @@ bool StoreMetaDataLocal::Marshal(json &node) const SetValue(node[GET_NAME(dataDir)], dataDir); SetValue(node[GET_NAME(schema)], schema); SetValue(node[GET_NAME(policies)], policies); + SetValue(node[GET_NAME(isPublic)], isPublic); + SetValue(node[GET_NAME(cloudSync)], cloudSync); return true; } @@ -79,6 +81,8 @@ bool StoreMetaDataLocal::Unmarshal(const json &node) GetValue(node, GET_NAME(dataDir), dataDir); GetValue(node, GET_NAME(schema), schema); GetValue(node, GET_NAME(policies), policies); + GetValue(node, GET_NAME(isPublic), isPublic); + GetValue(node, GET_NAME(cloudSync), cloudSync); return true; } diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 98d29c081..02ac9f2c6 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2022-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 @@ -92,11 +92,12 @@ ohos_shared_library("distributeddatasvc") { "dumper/src/dump_helper.cpp", "kvdb/auth_delegate.cpp", "kvdb/kvdb_exporter.cpp", + "kvdb/kvdb_general_store.cpp", "kvdb/kvdb_service_impl.cpp", "kvdb/kvdb_service_stub.cpp", + "kvdb/kvdb_watcher.cpp", "kvdb/kvstore_sync_manager.cpp", "kvdb/query_helper.cpp", - "kvdb/store_cache.cpp", "kvdb/upgrade.cpp", "kvdb/user_delegate.cpp", "matrix/src/device_matrix.cpp", diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index cae601c0a..38c18d36b 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.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 @@ -25,6 +25,7 @@ #include "metadata/meta_data_manager.h" #include "sync_strategies/network_sync_strategy.h" #include "store/auto_cache.h" +#include "user_delegate.h" #include "utils/anonymous.h" namespace OHOS::CloudData { using namespace DistributedData; @@ -96,9 +97,7 @@ std::shared_ptr SyncManager::SyncInfo::GenerateQuery(const std::string } class SyncQuery final : public GenQuery { public: - explicit SyncQuery(const std::vector &tables) : tables_(tables) - { - } + explicit SyncQuery(const std::vector &tables) : tables_(tables) {} bool IsEqual(uint64_t tid) override { @@ -117,7 +116,7 @@ std::shared_ptr SyncManager::SyncInfo::GenerateQuery(const std::string return std::make_shared(it == tables_.end() || it->second.empty() ? tables : it->second); } -bool SyncManager::SyncInfo::Contains(const std::string& storeName) +bool SyncManager::SyncInfo::Contains(const std::string &storeName) { return tables_.empty() || tables_.find(storeName) != tables_.end(); } @@ -179,7 +178,7 @@ bool SyncManager::IsValid(SyncInfo &info, CloudInfo &cloud) (info.id_ != SyncInfo::DEFAULT_ID && cloud.id != info.id_)) { info.SetError(E_CLOUD_DISABLED); ZLOGE("cloudInfo invalid:%{public}d, ", cloud.IsValid(), - Anonymous::Change(info.id_).c_str(), Anonymous::Change(cloud.id).c_str()); + Anonymous::Change(info.id_).c_str(), Anonymous::Change(cloud.id).c_str()); return false; } if (!cloud.enableCloud || (!info.bundleName_.empty() && !cloud.IsOn(info.bundleName_))) { @@ -380,28 +379,32 @@ AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, return nullptr; } - if (!store->IsBound()) { - CloudInfo info; - info.user = user; - SchemaMeta schemaMeta; - std::string schemaKey = info.GetSchemaKey(meta.bundleName, meta.instanceId); - if (!MetaDataManager::GetInstance().LoadMeta(std::move(schemaKey), schemaMeta, true)) { - ZLOGE("failed, no schema bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), - meta.GetStoreAlias().c_str()); - return nullptr; - } - auto dbMeta = schemaMeta.GetDataBase(meta.storeId); - auto cloudDB = instance->ConnectCloudDB(meta.tokenId, dbMeta); - auto assetLoader = instance->ConnectAssetLoader(meta.tokenId, dbMeta); - if (mustBind && (cloudDB == nullptr || assetLoader == nullptr)) { - ZLOGE("failed, no cloud DB <0x%{public}x %{public}s<->%{public}s>", meta.tokenId, - Anonymous::Change(dbMeta.name).c_str(), Anonymous::Change(dbMeta.alias).c_str()); - return nullptr; - } - - if (cloudDB != nullptr || assetLoader != nullptr) { - store->Bind(dbMeta, { std::move(cloudDB), std::move(assetLoader) }); + if (!store->IsBound()) { // todo ֻ���ڹ������ʱ�򣬰󶨶���� + std::set activeUsers = UserDelegate::GetInstance().GetLocalUsers(); + std::map> cloudDBs = {}; + for (auto &activeUser : activeUsers) { + CloudInfo info; + info.user = std::stoi(activeUser); + SchemaMeta schemaMeta; + std::string schemaKey = info.GetSchemaKey(meta.bundleName, meta.instanceId); + if (!MetaDataManager::GetInstance().LoadMeta(std::move(schemaKey), schemaMeta, true)) { + ZLOGE("failed, no schema bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), + meta.GetStoreAlias().c_str()); + return nullptr; + } + auto dbMeta = schemaMeta.GetDataBase(meta.storeId); + auto cloudDB = instance->ConnectCloudDB(meta.tokenId, dbMeta); + if (mustBind && cloudDB == nullptr) { + ZLOGE("failed, no cloud DB <0x%{public}x %{public}s<->%{public}s>", meta.tokenId, + Anonymous::Change(dbMeta.name).c_str(), Anonymous::Change(dbMeta.alias).c_str()); + return nullptr; + } + if (cloudDB != nullptr) { + GeneralStore::BindInfo bindInfo(std::move(cloudDB), nullptr); + cloudDBs[activeUser] = std::make_pair(dbMeta, bindInfo); + } } + store->Bind(cloudDBs); } return store; } diff --git a/services/distributeddataservice/service/kvdb/kvdb_exporter.cpp b/services/distributeddataservice/service/kvdb/kvdb_exporter.cpp index 22532d531..f31c14d1d 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_exporter.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_exporter.cpp @@ -1,5 +1,5 @@ /* -* Copyright (c) 2022 Huawei Device Co., Ltd. +* Copyright (c) 2022-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 @@ -17,9 +17,9 @@ #include "backup_manager.h" #include "directory/directory_manager.h" +#include "kvdb_general_store.h" #include "log_print.h" #include "reporter.h" -#include "store_cache.h" namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; using namespace OHOS::DistributedDataDfx; @@ -30,8 +30,8 @@ void KVDBExporter::Exporter(const StoreMetaData &meta, const std::string &backup DBManager manager(meta.appId, meta.user); auto path = DirectoryManager::GetInstance().GetStorePath(meta); manager.SetKvStoreConfig({ path }); - auto dbPassword = StoreCache::GetDBPassword(meta); - auto dbOption = StoreCache::GetDBOption(meta, dbPassword); + auto dbPassword = KVDBGeneralStore::GetDBPassword(meta); + auto dbOption = KVDBGeneralStore::GetDBOption(meta, dbPassword); manager.GetKvStore(meta.storeId, dbOption, [&manager, &backupPath, &dbPassword, &result] (DistributedDB::DBStatus dbstatus, DistributedDB::KvStoreNbDelegate *delegate) { diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp new file mode 100644 index 000000000..0ec84318a --- /dev/null +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -0,0 +1,545 @@ +/* + * Copyright (c) 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 + * + * 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 "KVDBGeneralStore" +#include "kvdb_general_store.h" + +#include "cloud/schema_meta.h" +#include "crypto_manager.h" +#include "device_matrix.h" +#include "directory/directory_manager.h" +#include "eventcenter/event_center.h" +#include "kvdb_query.h" +#include "log_print.h" +#include "metadata/meta_data_manager.h" +#include "metadata/secret_key_meta_data.h" +#include "query_helper.h" +#include "snapshot/bind_event.h" +#include "types.h" +#include "user_delegate.h" +#include "utils/anonymous.h" + +namespace OHOS::DistributedKv { +using namespace DistributedData; +using namespace DistributedDB; +using DBField = DistributedDB::Field; +using DBTable = DistributedDB::TableSchema; +using DBSchema = DistributedDB::DataBaseSchema; +using ClearMode = DistributedDB::ClearMode; +using DBStatus = DistributedDB::DBStatus; + +KVDBGeneralStore::DBPassword KVDBGeneralStore::GetDBPassword(const StoreMetaData &data) +{ + DBPassword dbPassword; + if (!data.isEncrypt) { + return dbPassword; + } + + SecretKeyMetaData secretKey; + secretKey.storeType = data.storeType; + auto storeKey = data.GetSecretKey(); + MetaDataManager::GetInstance().LoadMeta(storeKey, secretKey, true); + std::vector password; + CryptoManager::GetInstance().Decrypt(secretKey.sKey, password); + dbPassword.SetValue(password.data(), password.size()); + password.assign(password.size(), 0); + return dbPassword; +} + +KVDBGeneralStore::DBSecurity KVDBGeneralStore::GetDBSecurity(int32_t secLevel) +{ + if (secLevel < SecurityLevel::NO_LABEL || secLevel > SecurityLevel::S4) { + return { DistributedDB::NOT_SET, DistributedDB::ECE }; + } + if (secLevel == SecurityLevel::S3) { + return { DistributedDB::S3, DistributedDB::SECE }; + } + if (secLevel == SecurityLevel::S4) { + return { DistributedDB::S4, DistributedDB::ECE }; + } + return { secLevel, DistributedDB::ECE }; +} + +KVDBGeneralStore::DBOption KVDBGeneralStore::GetDBOption(const StoreMetaData &data, const DBPassword &password) +{ + DBOption dbOption; + dbOption.syncDualTupleMode = true; // tuple of (appid+storeid) + dbOption.createIfNecessary = false; + dbOption.isMemoryDb = false; + dbOption.isEncryptedDb = data.isEncrypt; + dbOption.isNeedCompressOnSync = data.isNeedCompress; + if (data.isEncrypt) { + dbOption.cipher = DistributedDB::CipherType::AES_256_GCM; + dbOption.passwd = password; + } + + if (data.storeType == KvStoreType::SINGLE_VERSION) { + dbOption.conflictResolvePolicy = DistributedDB::LAST_WIN; + } else if (data.storeType == KvStoreType::DEVICE_COLLABORATION) { + dbOption.conflictResolvePolicy = DistributedDB::DEVICE_COLLABORATION; + } + + dbOption.schema = data.schema; + dbOption.createDirByStoreIdOnly = true; + dbOption.secOption = GetDBSecurity(data.securityLevel); + return dbOption; +} + +KVDBGeneralStore::KVDBGeneralStore(const StoreMetaData &meta) : manager_(meta.appId, meta.user, meta.instanceId) +{ + observer_.storeId_ = meta.storeId; + + DBStatus status = DBStatus::NOT_FOUND; + manager_.SetKvStoreConfig({ DirectoryManager::GetInstance().GetStorePath(meta) }); + manager_.GetKvStore( + meta.storeId, GetDBOption(meta, GetDBPassword(meta)), [&status, this](auto dbStatus, auto *tmpStore) { + status = dbStatus; + delegate_ = tmpStore; + }); + if (delegate_ == nullptr || status != DBStatus::OK) { + manager_.CloseKvStore(delegate_); + return; + } + delegate_->RegisterObserver({}, DistributedDB::OBSERVER_CHANGES_FOREIGN, &observer_); + if (meta.isAutoSync) { + auto code = DeviceMatrix::GetInstance().GetCode(meta); + delegate_->SetRemotePushFinishedNotify([code](const DistributedDB::RemotePushNotifyInfo &info) { + DeviceMatrix::GetInstance().OnExchanged(info.deviceId, code, true); + }); + bool param = true; + auto data = static_cast(¶m); + delegate_->Pragma(DistributedDB::SET_SYNC_RETRY, data); + } + storeInfo_.tokenId = meta.tokenId; + storeInfo_.bundleName = meta.bundleName; + storeInfo_.storeName = meta.storeId; + storeInfo_.instanceId = meta.instanceId; + storeInfo_.user = std::stoi(meta.user); + storeInfo_.isPublic = meta.isPublic; + storeInfo_.cloudSync = meta.cloudSync; +} + +KVDBGeneralStore::~KVDBGeneralStore() +{ + if (delegate_ != nullptr) { + delegate_->UnRegisterObserver(&observer_); + } + manager_.CloseKvStore(delegate_); + delegate_ = nullptr; + for (auto &bindInfo_ : bindInfos_) { + if (bindInfo_.db_ != nullptr) { + bindInfo_.db_->Close(); + } + } + bindInfos_.clear(); + dbClouds_.clear(); +} + +int32_t KVDBGeneralStore::BindSnapshots(std::shared_ptr>> bindAssets) +{ + return GenErr::E_OK; +} + +int32_t KVDBGeneralStore::Bind(const std::map> &cloudDBs) +{ + if (cloudDBs.empty()) { + ZLOGW("No cloudDB!"); + return GeneralError::E_OK; + } + std::map schemas{}; + for (auto &iter : cloudDBs) { + auto database = iter.second.first; + auto bindInfo = iter.second.second; + + if (bindInfo.db_ == nullptr) { + return GeneralError::E_INVALID_ARGS; + } + + if (isBound_.exchange(true)) { + return GeneralError::E_OK; + } + + BindEvent::BindEventInfo eventInfo; + eventInfo.tokenId = storeInfo_.tokenId; + eventInfo.bundleName = storeInfo_.bundleName; + eventInfo.storeName = storeInfo_.storeName; + eventInfo.user = storeInfo_.user; + eventInfo.instanceId = storeInfo_.instanceId; + + auto evt = std::make_unique(BindEvent::BIND_SNAPSHOT, std::move(eventInfo)); + EventCenter::GetInstance().PostEvent(std::move(evt)); + bindInfos_.insert(std::move(bindInfo)); + dbClouds_.insert({ iter.first, std::make_shared(bindInfo.db_, nullptr) }); + + DBSchema schema; + schema.tables.resize(database.tables.size()); + for (size_t i = 0; i < database.tables.size(); i++) { + const Table &table = database.tables[i]; + DBTable &dbTable = schema.tables[i]; + dbTable.name = table.name; + dbTable.sharedTableName = table.sharedTableName; + for (auto &field : table.fields) { + DBField dbField; + dbField.colName = field.colName; + dbField.type = field.type; + dbField.primary = field.primary; + dbField.nullable = field.nullable; + dbTable.fields.push_back(std::move(dbField)); + } + } + schemas.insert({ iter.first, schema }); + } + std::unique_lock lock(rwMutex_); + if (delegate_ == nullptr) { + return GeneralError::E_ALREADY_CLOSED; + } + delegate_->SetCloudDB(dbClouds_); + delegate_->SetCloudDBSchema(schemas); + return GeneralError::E_OK; +} + +bool KVDBGeneralStore::IsBound() +{ + return isBound_; +} + +int32_t KVDBGeneralStore::Close() +{ + std::unique_lock lock(rwMutex_); + if (delegate_ == nullptr) { + return 0; + } + int32_t count = delegate_->GetTaskCount(); + if (count > 0) { + return GeneralError::E_BUSY; + } + if (delegate_ != nullptr) { + delegate_->UnRegisterObserver(&observer_); + } + auto status = manager_.CloseKvStore(delegate_); + if (status != DBStatus::OK) { + return status; + } + delegate_ = nullptr; + return GeneralError::E_OK; +} + +int32_t KVDBGeneralStore::Execute(const std::string &table, const std::string &sql) +{ + return GeneralError::E_OK; +} + +int32_t KVDBGeneralStore::Insert(const std::string &table, VBuckets &&values) +{ + return GeneralError::E_OK; +} + +int32_t KVDBGeneralStore::Update(const std::string &table, const std::string &setSql, Values &&values, + const std::string &whereSql, Values &&conditions) +{ + return GeneralError::E_OK; +} + +int32_t KVDBGeneralStore::Delete(const std::string &table, const std::string &sql, Values &&args) +{ + return GeneralError::E_OK; +} + +int32_t KVDBGeneralStore::Replace(const std::string &table, VBucket &&value) +{ + return GeneralError::E_OK; +} + +std::shared_ptr KVDBGeneralStore::Query( + __attribute__((unused)) const std::string &table, const std::string &sql, Values &&args) +{ + return nullptr; +} + +std::shared_ptr KVDBGeneralStore::Query(const std::string &table, GenQuery &query) +{ + return nullptr; +} + +int32_t KVDBGeneralStore::MergeMigratedData(const std::string &tableName, VBuckets &&values) +{ + return GeneralError::E_OK; +} + +KVDBGeneralStore::DBSyncCallback KVDBGeneralStore::GetDBSyncCompleteCB(DetailAsync async) +{ + if (!async) { + return [](auto &) {}; + } + return [async = std::move(async)](const std::map &status) { + DistributedData::GenDetails details; + for (auto &[key, dbStatus] : status) { + auto &value = details[key]; + value.progress = FINISHED; + value.code = GeneralError::E_OK; + if (dbStatus != DBStatus::OK) { + value.code = dbStatus; + } + } + async(details); + }; +} + +int32_t KVDBGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &query, DetailAsync async, int32_t wait) +{ + auto syncMode = GeneralStore::GetSyncMode(mode); + std::shared_lock lock(rwMutex_); + if (delegate_ == nullptr) { + ZLOGE("store already closed! devices count:%{public}zu, the 1st:%{public}s, mode:%{public}d", devices.size(), + devices.empty() ? "null" : Anonymous::Change(*devices.begin()).c_str(), mode); + return GeneralError::E_ALREADY_CLOSED; + } + KVDBQuery *kvQuery = nullptr; + auto ret = query.QueryInterface(kvQuery); + DistributedDB::Query dbQuery; + if (ret == GeneralError::E_OK && kvQuery != nullptr && kvQuery->IsValidQuery()) { + dbQuery = kvQuery->GetDBQuery(); + } else { + return GeneralError::E_INVALID_ARGS; + } + auto dbStatus = DistributedDB::OK; + if (syncMode == NEARBY_SUBSCRIBE_REMOTE) { + dbStatus = delegate_->SubscribeRemoteQuery(devices, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); + } else if (syncMode == NEARBY_UNSUBSCRIBE_REMOTE) { + dbStatus = delegate_->UnSubscribeRemoteQuery(devices, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); + } else { + auto dbMode = DistributedDB::SyncMode(syncMode); + if (syncMode < NEARBY_END) { + if (kvQuery->IsEmpty()) { + dbStatus = delegate_->Sync(devices, dbMode, GetDBSyncCompleteCB(std::move(async)), false); + } else { + dbStatus = delegate_->Sync(devices, dbMode, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); + } + } else if (syncMode > NEARBY_END && syncMode < CLOUD_END) { + if (!storeInfo_.cloudSync) { + return GeneralError::E_NOT_SUPPORT; + } + DistributedDB::CloudSyncOption syncOption; + syncOption.devices = devices; + syncOption.mode = dbMode; + syncOption.waitTime = wait; + if (storeInfo_.isPublic) { + std::set activeUsers = UserDelegate::GetInstance().GetLocalUsers(); + for (auto &activeUser : activeUsers) { + syncOption.users.push_back(activeUser); + } + } else { + syncOption.users.push_back(std::to_string(storeInfo_.user)); + } + dbStatus = delegate_->Sync({ devices, dbMode, {}, wait, false }, nullptr); + } else { + dbStatus = DistributedDB::INVALID_ARGS; + } + } + return ConvertStatus(dbStatus); +} + +std::shared_ptr KVDBGeneralStore::PreSharing(GenQuery &query) +{ + return nullptr; +} + +int32_t KVDBGeneralStore::Clean(const std::vector &devices, int32_t mode, const std::string &tableName) +{ + if (mode < 0 || mode > CLEAN_MODE_BUTT) { + return GeneralError::E_INVALID_ARGS; + } + DBStatus status = OK; + std::shared_lock lock(rwMutex_); + if (delegate_ == nullptr) { + ZLOGE("store already closed! devices count:%{public}zu, the 1st:%{public}s, mode:%{public}d, " + "tableName:%{public}s", + devices.size(), devices.empty() ? "null" : Anonymous::Change(*devices.begin()).c_str(), mode, + Anonymous::Change(tableName).c_str()); + return GeneralError::E_ALREADY_CLOSED; + } + switch (mode) { + case CLOUD_INFO: + status = delegate_->RemoveDeviceData("", static_cast(CLOUD_INFO)); + break; + case CLOUD_DATA: + status = delegate_->RemoveDeviceData("", static_cast(CLOUD_DATA)); + break; + case NEARBY_DATA: + if (devices.empty()) { + status = delegate_->RemoveDeviceData(); + break; + } + for (auto device : devices) { + status = delegate_->RemoveDeviceData(device); + } + break; + default: + return GeneralError::E_ERROR; + } + return status == DistributedDB::OK ? GeneralError::E_OK : GeneralError::E_ERROR; +} + +int32_t KVDBGeneralStore::Watch(int32_t origin, Watcher &watcher) +{ + if (origin != Watcher::Origin::ORIGIN_ALL || observer_.watcher_ != nullptr) { + return GeneralError::E_INVALID_ARGS; + } + + observer_.watcher_ = &watcher; + return GeneralError::E_OK; +} + +int32_t KVDBGeneralStore::Unwatch(int32_t origin, Watcher &watcher) +{ + if (origin != Watcher::Origin::ORIGIN_ALL || observer_.watcher_ != &watcher) { + return GeneralError::E_INVALID_ARGS; + } + + observer_.watcher_ = nullptr; + return GeneralError::E_OK; +} + +int32_t KVDBGeneralStore::Release() +{ + auto ref = 1; + { + std::lock_guard lock(mutex_); + if (ref_ == 0) { + return 0; + } + ref = --ref_; + } + ZLOGD("ref:%{public}d", ref); + if (ref == 0) { + delete this; + } + return ref; +} + +int32_t KVDBGeneralStore::AddRef() +{ + std::lock_guard lock(mutex_); + if (ref_ == 0) { + return 0; + } + return ++ref_; +} + +int32_t KVDBGeneralStore::SetDistributedTables( + const std::vector &tables, int32_t type, const std::vector &references) +{ + return GeneralError::E_OK; +} + +int32_t KVDBGeneralStore::SetTrackerTable( + const std::string &tableName, const std::set &trackerColNames, const std::string &extendColName) +{ + return GeneralError::E_OK; +} + +KVDBGeneralStore::GenErr KVDBGeneralStore::ConvertStatus( + DistributedDB::DBStatus status) // todo 没用到,具体转换结合接口错误码 +{ + switch (status) { + case DBStatus::OK: + return GenErr::E_OK; + case DBStatus::CLOUD_NETWORK_ERROR: + return GenErr::E_NETWORK_ERROR; + case DBStatus::CLOUD_LOCK_ERROR: + return GenErr::E_LOCKED_BY_OTHERS; + case DBStatus::CLOUD_FULL_RECORDS: + return GenErr::E_RECODE_LIMIT_EXCEEDED; + case DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT: + return GenErr::E_NO_SPACE_FOR_ASSET; + default: + ZLOGI("status:0x%{public}x", status); + break; + } + return GenErr::E_ERROR; +} + +bool KVDBGeneralStore::IsValid() +{ + return delegate_ != nullptr; +} + +int32_t KVDBGeneralStore::RegisterDetailProgressObserver(GeneralStore::DetailAsync async) +{ + return GenErr::E_OK; +} + +int32_t KVDBGeneralStore::UnregisterDetailProgressObserver() +{ + return GenErr::E_OK; +} + +void KVDBGeneralStore::ObserverProxy::OnChange( + DistributedDB::Origin origin, const std::string &originalId, DistributedDB::ChangedData &&data) +{ + if (!HasWatcher()) { + return; + } + GenOrigin genOrigin; + genOrigin.origin = (origin == DBOrigin::ORIGIN_LOCAL) + ? GenOrigin::ORIGIN_LOCAL + : (origin == DBOrigin::ORIGIN_CLOUD) ? GenOrigin::ORIGIN_CLOUD : GenOrigin::ORIGIN_NEARBY; + genOrigin.dataType = data.type == DistributedDB::ASSET ? GenOrigin::ASSET_DATA : GenOrigin::BASIC_DATA; + genOrigin.id.push_back(originalId); + genOrigin.store = storeId_; + Watcher::PRIFields fields; + Watcher::ChangeInfo changeInfo; + for (uint32_t i = 0; i < DistributedDB::OP_BUTT; ++i) { + auto &info = changeInfo[data.tableName][i]; + std::vector> a = data.primaryData[i]; + for (auto &priData : data.primaryData[i]) { + Watcher::PRIValue value; + Convert(std::move(*(priData.begin())), value); + info.push_back(std::move(value)); + } + } + if (!data.field.empty()) { + fields[std::move(data.tableName)] = std::move(*(data.field.begin())); + } + watcher_->OnChange(genOrigin, fields, std::move(changeInfo)); +} + +void KVDBGeneralStore::ObserverProxy::OnChange(const DistributedDB::KvStoreChangedData &data) +{ + if (!HasWatcher()) { + return; + } + const auto &inserts = data.GetEntriesInserted(); + const auto &deletes = data.GetEntriesDeleted(); + const auto &updates = data.GetEntriesUpdated(); + Watcher::ChangeData changeData; + ConvertChangeData(inserts, changeData[storeId_][DistributedDB::OP_INSERT]); + ConvertChangeData(deletes, changeData[storeId_][DistributedDB::OP_DELETE]); + ConvertChangeData(updates, changeData[storeId_][DistributedDB::OP_UPDATE]); + GenOrigin genOrigin; + genOrigin.origin = GenOrigin::ORIGIN_NEARBY; + genOrigin.store = storeId_; + + watcher_->OnChange(genOrigin, {}, std::move(changeData)); +} + +void KVDBGeneralStore::ObserverProxy::ConvertChangeData( + const std::list &entries, std::vector &values) +{ + for (auto entry : entries) { + auto value = std::vector{ entry.key, entry.value }; + values.push_back(value); + } +} +} // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.h b/services/distributeddataservice/service/kvdb/kvdb_general_store.h new file mode 100644 index 000000000..3cfc0b971 --- /dev/null +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.h @@ -0,0 +1,134 @@ +/* + * Copyright (c) 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 + * + * 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_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_GENERAL_STORE_H +#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_GENERAL_STORE_H + +#include +#include +#include + +#include "kv_store_changed_data.h" +#include "kv_store_delegate_manager.h" +#include "kv_store_nb_delegate.h" +#include "kvstore_sync_callback.h" +#include "kvstore_sync_manager.h" +#include "metadata/store_meta_data.h" +#include "rdb_cloud.h" +#include "store/general_store.h" +#include "store/general_value.h" +#include "store_observer.h" + +namespace OHOS::DistributedKv { +class KVDBGeneralStore : public DistributedData::GeneralStore { +public: + using Cursor = DistributedData::Cursor; + using GenQuery = DistributedData::GenQuery; + using VBucket = DistributedData::VBucket; + using VBuckets = DistributedData::VBuckets; + using Value = DistributedData::Value; + using Values = DistributedData::Values; + using StoreMetaData = DistributedData::StoreMetaData; + using Database = DistributedData::Database; + using GenErr = DistributedData::GeneralError; + using Reference = DistributedData::Reference; + using SyncCallback = KvStoreSyncCallback; + using SyncEnd = KvStoreSyncManager::SyncEnd; + using DBStore = DistributedDB::KvStoreNbDelegate; + using Store = std::shared_ptr; + using DBStatus = DistributedDB::DBStatus; + using DBOption = DistributedDB::KvStoreNbDelegate::Option; + using DBSecurity = DistributedDB::SecurityOption; + using DBPassword = DistributedDB::CipherPassword; + using BindAssets = DistributedData::BindAssets; + using DBMode = DistributedDB::SyncMode; + + explicit KVDBGeneralStore(const StoreMetaData &meta); + ~KVDBGeneralStore(); + int32_t Bind(const std::map> &cloudDBs) override; + bool IsBound() override; + bool IsValid(); + int32_t Execute(const std::string &table, const std::string &sql) override; + int32_t SetDistributedTables( + const std::vector &tables, int32_t type, const std::vector &references) override; + int32_t SetTrackerTable(const std::string &tableName, const std::set &trackerColNames, + const std::string &extendColName) override; + int32_t Insert(const std::string &table, VBuckets &&values) override; + int32_t Update(const std::string &table, const std::string &setSql, Values &&values, const std::string &whereSql, + Values &&conditions) override; + int32_t Replace(const std::string &table, VBucket &&value) override; + int32_t Delete(const std::string &table, const std::string &sql, Values &&args) override; + std::shared_ptr Query(const std::string &table, const std::string &sql, Values &&args) override; + std::shared_ptr Query(const std::string &table, GenQuery &query) override; + int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, DetailAsync async, int32_t wait) override; + std::shared_ptr PreSharing(GenQuery &query) override; + int32_t Clean(const std::vector &devices, int32_t mode, const std::string &tableName) override; + int32_t Watch(int32_t origin, Watcher &watcher) override; + int32_t Unwatch(int32_t origin, Watcher &watcher) override; + int32_t RegisterDetailProgressObserver(DetailAsync async) override; + int32_t UnregisterDetailProgressObserver() override; + int32_t Close() override; + int32_t AddRef() override; + int32_t Release() override; + int32_t BindSnapshots( + std::shared_ptr>> bindAssets) override; + int32_t MergeMigratedData(const std::string &tableName, VBuckets &&values) override; + + static DBPassword GetDBPassword(const StoreMetaData &data); + static DBOption GetDBOption(const StoreMetaData &data, const DBPassword &password); + static DBSecurity GetDBSecurity(int32_t secLevel); + +private: + KVDBGeneralStore(const KVDBGeneralStore &store); + KVDBGeneralStore &operator=(const KVDBGeneralStore &store); + using KvDelegate = DistributedDB::KvStoreNbDelegate; + using KvManager = DistributedDB::KvStoreDelegateManager; + using SyncProcess = DistributedDB::SyncProcess; + using DBSyncCallback = std::function &status)>; + using DBProcessCB = std::function &processes)>; + static GenErr ConvertStatus(DistributedDB::DBStatus status); + DBSyncCallback GetDBSyncCompleteCB(DetailAsync async); + class ObserverProxy : public DistributedDB::KvStoreObserver { + public: + using DBOrigin = DistributedDB::Origin; + using GenOrigin = Watcher::Origin; + ~ObserverProxy() = default; + void OnChange(DistributedDB::Origin origin, const std::string &originalId, DistributedDB::ChangedData &&data); + void OnChange(const DistributedDB::KvStoreChangedData &data) override; + void ConvertChangeData(const std::list &entries, std::vector &values); + bool HasWatcher() const + { + return watcher_ != nullptr; + } + + private: + friend KVDBGeneralStore; + Watcher *watcher_ = nullptr; + std::string storeId_; + }; + + ObserverProxy observer_; + KvManager manager_; + KvDelegate *delegate_ = nullptr; + std::map> dbClouds_{}; + std::set bindInfos_; + std::atomic isBound_ = false; + std::mutex mutex_; + int32_t ref_ = 1; + mutable std::shared_mutex rwMutex_; + DistributedData::StoreInfo storeInfo_; +}; +} // namespace OHOS::DistributedKv +#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_GENERAL_STORE_H diff --git a/services/distributeddataservice/service/kvdb/kvdb_query.h b/services/distributeddataservice/service/kvdb/kvdb_query.h new file mode 100644 index 000000000..83d913563 --- /dev/null +++ b/services/distributeddataservice/service/kvdb/kvdb_query.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 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 + * + * 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_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_QUERY_H +#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_QUERY_H + +#include "query.h" +#include "query_helper.h" +#include "store/general_value.h" +#include "store_types.h" + +namespace OHOS::DistributedKv { +class KVDBQuery : public DistributedData::GenQuery { +public: + static constexpr uint64_t TYPE_ID = 0x20000002; + + explicit KVDBQuery(const std::string &query) + { + query_ = query; + dbQuery_ = QueryHelper::StringToDbQuery(query, isSuccess_); + } + ~KVDBQuery() = default; + bool IsEqual(uint64_t tid) + { + return tid == TYPE_ID; + } + + std::vector GetTables() + { + return {}; + } + + bool IsValidQuery() + { + return !(!isSuccess_ && !query_.empty()); + } + + bool IsEmpty() + { + return query_.empty(); + } + + DistributedDB::Query GetDBQuery() + { + return dbQuery_; + } + + bool isSuccess_ = false; + std::string query_; + DistributedDB::Query dbQuery_; +}; +} // namespace OHOS::DistributedKv + +#endif //OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_QUERY_H diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index e55d6614a..122665cdd 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -22,6 +22,7 @@ #include "account/account_delegate.h" #include "backup_manager.h" #include "checker/checker_manager.h" +#include "cloud/change_event.h" #include "communication_provider.h" #include "crypto_manager.h" #include "device_manager_adapter.h" @@ -29,6 +30,8 @@ #include "dump/dump_manager.h" #include "eventcenter/event_center.h" #include "ipc_skeleton.h" +#include "kvdb_general_store.h" +#include "kvdb_query.h" #include "log_print.h" #include "matrix_event.h" #include "metadata/appid_meta_data.h" @@ -36,10 +39,13 @@ #include "metadata/meta_data_manager.h" #include "permit_delegate.h" #include "query_helper.h" +#include "store/general_store.h" +#include "store/store_info.h" #include "upgrade.h" #include "utils/anonymous.h" #include "utils/constant.h" #include "utils/converter.h" + namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; using namespace OHOS::AppDistributedKv; @@ -47,6 +53,8 @@ using namespace OHOS::Security::AccessToken; using system_clock = std::chrono::system_clock; using DMAdapter = DistributedData::DeviceManagerAdapter; using DumpManager = OHOS::DistributedData::DumpManager; +using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; + __attribute__((used)) KVDBServiceImpl::Factory KVDBServiceImpl::factory_; KVDBServiceImpl::Factory::Factory() { @@ -56,6 +64,16 @@ KVDBServiceImpl::Factory::Factory() } return product_; }); + auto creator = [](const StoreMetaData &metaData) -> GeneralStore* { + auto store = new (std::nothrow) KVDBGeneralStore(metaData); + if (store != nullptr && !store->IsValid()) { + delete store; + store = nullptr; + } + return store; + }; + AutoCache::GetInstance().RegCreator(KvStoreType::SINGLE_VERSION, creator); + AutoCache::GetInstance().RegCreator(KvStoreType::DEVICE_COLLABORATION, creator); } KVDBServiceImpl::Factory::~Factory() @@ -105,6 +123,29 @@ KVDBServiceImpl::KVDBServiceImpl() std::bind(&KVDBServiceImpl::DoComplete, this, data, syncInfo, refCount, std::placeholders::_1)); } }); + auto process = [this](const Event &event) { + auto &evt = static_cast(event); + auto &storeInfo = evt.GetStoreInfo(); + StoreMetaData meta; + meta.storeId = storeInfo.storeName; + meta.bundleName = storeInfo.bundleName; + meta.user = std::to_string(storeInfo.user); + meta.instanceId = storeInfo.instanceId; + meta.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta, true)) { + ZLOGE("meta empty, bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), + meta.GetStoreAlias().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.GetStoreAlias().c_str()); + return; + } + store->RegisterDetailProgressObserver(nullptr); + }; + EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_SYNC, process); } KVDBServiceImpl::~KVDBServiceImpl() @@ -170,7 +211,6 @@ Status KVDBServiceImpl::Delete(const AppId &appId, const StoreId &storeId) return true; } syncAgent.delayTimes_.erase(storeId); - syncAgent.observers_.erase(storeId); return true; }); MetaDataManager::GetInstance().DelMeta(metaData.GetKey()); @@ -179,12 +219,34 @@ Status KVDBServiceImpl::Delete(const AppId &appId, const StoreId &storeId) MetaDataManager::GetInstance().DelMeta(metaData.GetStrategyKey()); MetaDataManager::GetInstance().DelMeta(metaData.GetKeyLocal(), true); PermitDelegate::GetInstance().DelCache(metaData.GetKey()); - storeCache_.CloseStore(tokenId, storeId); + AutoCache::GetInstance().CloseStore(tokenId, storeId); ZLOGD("appId:%{public}s storeId:%{public}s instanceId:%{public}d", appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str(), metaData.instanceId); return SUCCESS; } +Status KVDBServiceImpl::CloudSync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) +{ + StoreMetaData metaData = GetStoreMetaData(appId, storeId); + MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData); + if (!metaData.cloudSync) { + ZLOGE("appId:%{public}s storeId:%{public}s instanceId:%{public}d not supports cloud sync", + appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str(), metaData.instanceId); + return NOT_SUPPORT; + } + DistributedData::StoreInfo storeInfo; + storeInfo.bundleName = appId.appId; + storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + storeInfo.user = AccountDelegate::GetInstance()->GetUserByToken(IPCSkeleton::GetCallingTokenID()); + storeInfo.storeName = storeId; + auto mixMode = static_cast(GeneralStore::MixMode(GeneralStore::CLOUD_TIME_FIRST, + metaData.isAutoSync ? GeneralStore::AUTO_SYNC_MODE : GeneralStore::MANUAL_SYNC_MODE)); + auto info = ChangeEvent::EventInfo(mixMode, 0, metaData.isAutoSync, nullptr, nullptr); + auto evt = std::make_unique(std::move(storeInfo), std::move(info)); + EventCenter::GetInstance().PostEvent(std::move(evt)); + return SUCCESS; // todo 异步接口,只会返回成功。 +} + Status KVDBServiceImpl::Sync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) { StoreMetaData metaData = GetStoreMetaData(appId, storeId); @@ -361,19 +423,22 @@ Status KVDBServiceImpl::Subscribe(const AppId &appId, const StoreId &storeId, sp auto tokenId = IPCSkeleton::GetCallingTokenID(); ZLOGI("appId:%{public}s storeId:%{public}s tokenId:0x%{public}x", appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str(), tokenId); - syncAgents_.Compute(tokenId, [&appId, &storeId, &observer](auto &key, SyncAgent &value) { - if (value.pid_ != IPCSkeleton::GetCallingPid()) { - value.ReInit(IPCSkeleton::GetCallingPid(), appId); + bool isCreate = false; + syncAgents_.Compute(tokenId, [&appId, &storeId, &observer, &isCreate](auto &key, SyncAgent &agent) { + if (agent.pid_ != IPCSkeleton::GetCallingPid()) { + agent.ReInit(IPCSkeleton::GetCallingPid(), appId); } - auto it = value.observers_.find(storeId); - if (it == value.observers_.end()) { - value.observers_[storeId] = std::make_shared(); + if (agent.watcher_ == nullptr) { + isCreate = true; + agent.SetWatcher(std::make_shared()); } - value.observers_[storeId]->insert(observer); + agent.SetObserver(iface_cast(observer->AsObject())); + agent.count_++; return true; }); - auto observers = GetObservers(tokenId, storeId); - storeCache_.SetObserver(tokenId, storeId, observers); + if (isCreate) { + AutoCache::GetInstance().SetObserver(tokenId, storeId, GetWatchers(tokenId, storeId)); + } return SUCCESS; } @@ -382,18 +447,20 @@ Status KVDBServiceImpl::Unsubscribe(const AppId &appId, const StoreId &storeId, auto tokenId = IPCSkeleton::GetCallingTokenID(); ZLOGI("appId:%{public}s storeId:%{public}s tokenId:0x%{public}x", appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str(), tokenId); - syncAgents_.ComputeIfPresent(tokenId, [&appId, &storeId, &observer](auto &key, SyncAgent &value) { - if (value.pid_ != IPCSkeleton::GetCallingPid()) { - ZLOGW("agent already changed! old pid:%{public}d new pid:%{public}d appId:%{public}s", - IPCSkeleton::GetCallingPid(), value.pid_, appId.appId.c_str()); - return true; + bool destroyed = false; + syncAgents_.ComputeIfPresent(tokenId, [&appId, &storeId, &observer, &destroyed](auto &key, SyncAgent &agent) { + if (agent.count_ > 0) { + agent.count_--; } - auto it = value.observers_.find(storeId); - if (it != value.observers_.end()) { - it->second->erase(observer); + if (agent.count_ == 0) { + destroyed = true; + agent.SetWatcher(nullptr); } return true; }); + if (destroyed) { + AutoCache::GetInstance().SetObserver(tokenId, storeId, GetWatchers(tokenId, storeId)); + } return SUCCESS; } @@ -423,6 +490,21 @@ Status KVDBServiceImpl::BeforeCreate(const AppId &appId, const StoreId &storeId, old.isEncrypt, meta.isEncrypt, old.area, meta.area, options.persistent); return Status::STORE_META_CHANGED; } + if (options.cloudSync) { + if (executors_ != nullptr) { + DistributedData::StoreInfo storeInfo; + storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + storeInfo.bundleName = appId.appId; + storeInfo.storeName = storeId; + storeInfo.instanceId = GetInstIndex(storeInfo.tokenId, appId); + storeInfo.user = std::stoi(meta.user); + storeInfo.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + executors_->Execute([storeInfo]() { + auto event = std::make_unique(CloudEvent::GET_SCHEMA, storeInfo); + EventCenter::GetInstance().PostEvent(move(event)); + }); + } + } auto dbStatus = DBStatus::OK; if (old != meta) { @@ -478,21 +560,21 @@ Status KVDBServiceImpl::AfterCreate(const AppId &appId, const StoreId &storeId, int32_t KVDBServiceImpl::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &appId) { ZLOGI("pid:%{public}d uid:%{public}d appId:%{public}s", pid, uid, appId.c_str()); - std::vector storeIds; - syncAgents_.ComputeIfPresent(tokenId, [pid, &storeIds](auto &, SyncAgent &value) { - if (value.pid_ != pid) { - return true; + syncAgents_.EraseIf([pid](auto &key, SyncAgent &agent) { + if (agent.pid_ != pid) { + return false; } - - for (auto &[key, value] : value.observers_) { - storeIds.push_back(key); + if (agent.watcher_ != nullptr) { + agent.watcher_->SetObserver(nullptr); + } + auto stores = AutoCache::GetInstance().GetStoresIfPresent(key); + for (auto store : stores) { + if (store != nullptr) { + store->UnregisterDetailProgressObserver(); + } } - return false; + return true; }); - - for (auto &storeId : storeIds) { - storeCache_.CloseStore(tokenId, storeId); - } return SUCCESS; } @@ -508,24 +590,23 @@ int32_t KVDBServiceImpl::ResolveAutoLaunch(const std::string &identifier, DBLaun } for (const auto &storeMeta : metaData) { - if (storeMeta.storeType < StoreMetaData::StoreType::STORE_KV_BEGIN - || storeMeta.storeType > StoreMetaData::StoreType::STORE_KV_END) { + if (storeMeta.storeType < StoreMetaData::StoreType::STORE_KV_BEGIN || + storeMeta.storeType > StoreMetaData::StoreType::STORE_KV_END) { continue; } auto identifierTag = DBManager::GetKvStoreIdentifier("", storeMeta.appId, storeMeta.storeId, true); if (identifier != identifierTag) { continue; } + auto watchers = GetWatchers(storeMeta.tokenId, storeMeta.storeId); + AutoCache::GetInstance().GetStore(storeMeta, watchers); - auto observers = GetObservers(storeMeta.tokenId, storeMeta.storeId); - ZLOGD("user:%{public}s appId:%{public}s storeId:%{public}s observers:%{public}zu", storeMeta.user.c_str(), - storeMeta.bundleName.c_str(), Anonymous::Change(storeMeta.storeId).c_str(), - (observers) ? observers->size() : size_t(0)); - DBStatus status; - storeCache_.GetStore(storeMeta, observers, status); + ZLOGD("user:%{public}s appId:%{public}s storeId:%{public}s", storeMeta.user.c_str(), + storeMeta.bundleName.c_str(), Anonymous::Change(storeMeta.storeId).c_str()); } return SUCCESS; } + int32_t KVDBServiceImpl::OnUserChange(uint32_t code, const std::string &user, const std::string &account) { (void)code; @@ -534,7 +615,7 @@ int32_t KVDBServiceImpl::OnUserChange(uint32_t code, const std::string &user, co std::vector users; AccountDelegate::GetInstance()->QueryUsers(users); std::set userIds(users.begin(), users.end()); - storeCache_.CloseExcept(userIds); + AutoCache::GetInstance().CloseExcept(userIds); return SUCCESS; } @@ -595,6 +676,8 @@ void KVDBServiceImpl::AddOptions(const Options &options, StoreMetaData &metaData metaData.schema = options.schema; metaData.account = AccountDelegate::GetInstance()->GetCurrentAccountId(); metaData.isNeedCompress = options.isNeedCompress; + metaData.isPublic = options.isPublic; + metaData.cloudSync = options.cloudSync; } void KVDBServiceImpl::SaveLocalMetaData(const Options &options, const StoreMetaData &metaData) @@ -605,6 +688,8 @@ void KVDBServiceImpl::SaveLocalMetaData(const Options &options, const StoreMetaD localMetaData.isEncrypt = options.encrypt; localMetaData.dataDir = DirectoryManager::GetInstance().GetStorePath(metaData); localMetaData.schema = options.schema; + localMetaData.isPublic = options.isPublic; + localMetaData.cloudSync = options.cloudSync; for (auto &policy : options.policies) { OHOS::DistributedData::PolicyValue value; value.type = policy.type; @@ -658,6 +743,16 @@ int32_t KVDBServiceImpl::GetInstIndex(uint32_t tokenId, const AppId &appId) return tokenInfo.instIndex; } +KVDBServiceImpl::DBResult KVDBServiceImpl::HandleGenDetails(const GenDetails &details) +{ + DBResult dbResults; + for (const auto &[id, detail] : details) { + auto &dbResult = dbResults[id]; + dbResult = DBStatus(detail.code); + } + return dbResults; +} + Status KVDBServiceImpl::DoSync(const StoreMetaData &meta, const SyncInfo &info, const SyncEnd &complete, int32_t type) { ZLOGD("seqId:0x%{public}" PRIx64 " type:%{public}d remote:%{public}zu appId:%{public}s storeId:%{public}s", @@ -693,8 +788,8 @@ Status KVDBServiceImpl::DoSyncInOrder( auto metaData = meta; metaData.deviceId = uuid; auto matrixMeta = DeviceMatrix::GetInstance().GetMatrixMeta(uuid); - if (matrixMeta.version == MatrixMetaData::DEFAULT_VERSION - || !MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData)) { + if (matrixMeta.version == MatrixMetaData::DEFAULT_VERSION || + !MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData)) { isAfterMeta = true; } } @@ -737,42 +832,28 @@ Status KVDBServiceImpl::DoSyncBegin(const std::vector &devices, con if (devices.empty()) { return Status::INVALID_ARGUMENT; } - DistributedDB::DBStatus status; - auto observers = GetObservers(meta.tokenId, meta.storeId); - auto store = storeCache_.GetStore(meta, observers, status); + DistributedDB::DBStatus status = DistributedDB::OK; + auto watcher = GetWatchers(meta.tokenId, meta.storeId); + auto store = AutoCache::GetInstance().GetStore(meta, watcher); if (store == nullptr) { ZLOGE("failed! status:%{public}d appId:%{public}s storeId:%{public}s dir:%{public}s", status, meta.bundleName.c_str(), Anonymous::Change(meta.storeId).c_str(), meta.dataDir.c_str()); return ConvertDbStatus(status); } - bool isSuccess = false; - auto dbQuery = QueryHelper::StringToDbQuery(info.query, isSuccess); - if (!isSuccess && !info.query.empty()) { + KVDBQuery query(info.query); + if (!query.IsValidQuery()) { ZLOGE("failed DBQuery:%{public}s", Anonymous::Change(info.query).c_str()); return Status::INVALID_ARGUMENT; } - - switch (type) { - case ACTION_SYNC: - { - if (info.query.empty()) { - status = store->Sync(devices, ConvertDBMode(SyncMode(info.mode)), complete, false); - } else { - status = store->Sync(devices, ConvertDBMode(SyncMode(info.mode)), complete, dbQuery, false); - } - break; - } - case ACTION_SUBSCRIBE: - status = store->SubscribeRemoteQuery(devices, complete, dbQuery, false); - break; - case ACTION_UNSUBSCRIBE: - status = store->UnSubscribeRemoteQuery(devices, complete, dbQuery, false); - break; - default: - status = DBStatus::INVALID_ARGS; - break; - } - return ConvertDbStatus(status); + auto mode = ConvertGeneralSyncMode(SyncMode(info.mode), SyncAction(type)); + auto ret = store->Sync( + devices, mode, query, + [this, &complete](const GenDetails &result) mutable { + auto deviceStatus = HandleGenDetails(result); + complete(deviceStatus); + }, + 0); + return Status(ret); } Status KVDBServiceImpl::DoComplete(const StoreMetaData &meta, const SyncInfo &info, RefCount refCount, @@ -876,6 +957,23 @@ KVDBServiceImpl::DBMode KVDBServiceImpl::ConvertDBMode(SyncMode syncMode) const return dbMode; } +GeneralStore::SyncMode KVDBServiceImpl::ConvertGeneralSyncMode(SyncMode syncMode, SyncAction syncAction) const +{ + GeneralStore::SyncMode generalSyncMode = GeneralStore::SyncMode::NEARBY_END; + if (syncAction == SyncAction::ACTION_SUBSCRIBE) { + generalSyncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; + } else if (syncAction == SyncAction::ACTION_UNSUBSCRIBE) { + generalSyncMode = GeneralStore::SyncMode::NEARBY_UNSUBSCRIBE_REMOTE; + } else if (syncAction == SyncAction::ACTION_SYNC && syncMode == SyncMode::PUSH) { + generalSyncMode = GeneralStore::SyncMode::NEARBY_PUSH; + } else if (syncAction == SyncAction::ACTION_SYNC && syncMode == SyncMode::PULL) { + generalSyncMode = GeneralStore::SyncMode::NEARBY_PULL; + } else if (syncAction == SyncAction::ACTION_SYNC && syncMode == SyncMode::PUSH_PULL) { + generalSyncMode = GeneralStore::SyncMode::NEARBY_PULL_PUSH; + } + return generalSyncMode; +} + std::vector KVDBServiceImpl::ConvertDevices(const std::vector &deviceIds) const { if (deviceIds.empty()) { @@ -884,34 +982,49 @@ std::vector KVDBServiceImpl::ConvertDevices(const std::vector KVDBServiceImpl::GetObservers(uint32_t tokenId, const std::string &storeId) +AutoCache::Watchers KVDBServiceImpl::GetWatchers(uint32_t tokenId, const std::string &storeId) { - std::shared_ptr observers; - syncAgents_.ComputeIfPresent(tokenId, [&storeId, &observers](auto, SyncAgent &agent) { - auto it = agent.observers_.find(storeId); - if (it != agent.observers_.end()) { - observers = it->second; - } - return true; - }); - return observers; + auto [success, agent] = syncAgents_.Find(tokenId); + if (agent.watcher_ == nullptr) { + return {}; + } + return { agent.watcher_ }; } void KVDBServiceImpl::SyncAgent::ReInit(pid_t pid, const AppId &appId) { - ZLOGW("pid:%{public}d->%{public}d appId:%{public}s callback:%{public}d observer:%{public}zu", pid, pid_, - appId_.appId.c_str(), callback_ == nullptr, observers_.size()); + ZLOGW("pid:%{public}d->%{public}d appId:%{public}s callback:%{public}d", pid, pid_, + appId_.appId.c_str(), callback_ == nullptr); pid_ = pid; appId_ = appId; callback_ = nullptr; delayTimes_.clear(); - observers_.clear(); + count_ = 0; + watcher_ = nullptr; + observer_ = nullptr; +} + +void KVDBServiceImpl::SyncAgent::SetWatcher(std::shared_ptr watcher) +{ + if (watcher_ != watcher) { + watcher_ = watcher; + if (watcher_ != nullptr) { + watcher_->SetObserver(observer_); + } + } +} + +void KVDBServiceImpl::SyncAgent::SetObserver(sptr observer) +{ + observer_ = observer; + if (watcher_ != nullptr) { + watcher_->SetObserver(observer); + } } int32_t KVDBServiceImpl::OnBind(const BindInfo &bindInfo) { executors_ = bindInfo.executors; - storeCache_.SetThreadPool(bindInfo.executors); KvStoreSyncManager::GetInstance()->SetThreadPool(bindInfo.executors); return 0; } diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h index 4cdac4574..7251f4e5c 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -20,14 +20,17 @@ #include "concurrent_map.h" #include "device_matrix.h" +#include "kv_store_delegate_manager.h" #include "kv_store_nb_delegate.h" #include "kvdb_service_stub.h" +#include "kvdb_watcher.h" #include "kvstore_sync_manager.h" #include "metadata/store_meta_data.h" #include "metadata/store_meta_data_local.h" #include "metadata/strategy_meta_data.h" +#include "store/auto_cache.h" +#include "store/general_value.h" #include "utils/ref_count.h" -#include "store_cache.h" namespace OHOS::DistributedKv { class API_EXPORT KVDBServiceImpl final : public KVDBServiceStub { public: @@ -41,6 +44,7 @@ public: Status AfterCreate(const AppId &appId, const StoreId &storeId, const Options &options, const std::vector &password) override; Status Delete(const AppId &appId, const StoreId &storeId) override; + Status CloudSync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; Status Sync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; Status SyncExt(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; Status RegisterSyncCallback(const AppId &appId, sptr callback) override; @@ -62,6 +66,7 @@ public: int32_t ResolveAutoLaunch(const std::string &identifier, DBLaunchParam ¶m) override; int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; int32_t OnReady(const std::string &device) override; + private: using StoreMetaData = OHOS::DistributedData::StoreMetaData; using StrategyMeta = OHOS::DistributedData::StrategyMeta; @@ -80,10 +85,14 @@ private: struct SyncAgent { pid_t pid_ = 0; AppId appId_; + int32_t count_ = 0; sptr callback_; std::map delayTimes_; - std::map> observers_; + std::shared_ptr watcher_; + sptr observer_; void ReInit(pid_t pid, const AppId &appId); + void SetObserver(sptr observer); + void SetWatcher(std::shared_ptr watcher); }; class Factory { public: @@ -106,7 +115,9 @@ private: Status ConvertDbStatus(DBStatus status) const; DBMode ConvertDBMode(SyncMode syncMode) const; std::vector ConvertDevices(const std::vector &deviceIds) const; - std::shared_ptr GetObservers(uint32_t tokenId, const std::string &storeId); + DistributedData::GeneralStore::SyncMode ConvertGeneralSyncMode(SyncMode syncMode, SyncAction syncAction) const; + DBResult HandleGenDetails(const DistributedData::GenDetails &details); + DistributedData::AutoCache::Watchers GetWatchers(uint32_t tokenId, const std::string &storeId); using SyncResult = std::pair, std::map>; SyncResult ProcessResult(const std::map &results); void SaveLocalMetaData(const Options &options, const StoreMetaData &metaData); @@ -115,7 +126,6 @@ private: void DumpKvServiceInfo(int fd, std::map> ¶ms); static Factory factory_; ConcurrentMap syncAgents_; - StoreCache storeCache_; std::shared_ptr executors_; }; } // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp index 729e52b3d..4f73a7efa 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp @@ -30,6 +30,7 @@ const KVDBServiceStub::Handler &KVDBServiceStub::OnAfterCreate, &KVDBServiceStub::OnDelete, &KVDBServiceStub::OnSync, + &KVDBServiceStub::OnCloudSync, &KVDBServiceStub::OnRegisterCallback, &KVDBServiceStub::OnUnregisterCallback, &KVDBServiceStub::OnSetSyncParam, @@ -169,6 +170,23 @@ int32_t KVDBServiceStub::OnSync(const AppId &appId, const StoreId &storeId, Mess return ERR_NONE; } +int32_t KVDBServiceStub::OnCloudSync( + const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply) +{ + SyncInfo syncInfo; + if (!ITypesUtil::Unmarshal(data, syncInfo.seqId, syncInfo.mode, syncInfo.devices, syncInfo.delay, syncInfo.query)) { + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + int32_t status = CloudSync(appId, storeId, syncInfo); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return ERR_NONE; +} + int32_t KVDBServiceStub::OnSyncExt( const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply) { diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_stub.h b/services/distributeddataservice/service/kvdb/kvdb_service_stub.h index 183fdbe18..e0cd3d3f5 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_stub.h +++ b/services/distributeddataservice/service/kvdb/kvdb_service_stub.h @@ -32,6 +32,7 @@ private: int32_t OnAfterCreate(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply); int32_t OnDelete(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply); int32_t OnSync(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply); + int32_t OnCloudSync(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply); int32_t OnRegisterCallback(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply); int32_t OnUnregisterCallback(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply); int32_t OnSetSyncParam(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply); diff --git a/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp b/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp new file mode 100644 index 000000000..477a68126 --- /dev/null +++ b/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp @@ -0,0 +1,82 @@ +/* + * 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 "KVDBWatcher" + +#include "kvdb_watcher.h" + +#include "error/general_error.h" +#include "ikvstore_observer.h" +#include "log_print.h" +#include "utils/anonymous.h" + +namespace OHOS::DistributedKv { +using namespace DistributedData; +using Error = DistributedData::GeneralError; +KVDBWatcher::KVDBWatcher() {} + +int32_t KVDBWatcher::OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) +{ + return E_OK; +} + +int32_t KVDBWatcher::OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas) +{ + auto store = origin.store; + auto changeData = datas.find(store); + if (changeData != datas.end()) { + auto observer = GetObserver(); + if (observer == nullptr) { + return E_NOT_INIT; + } + auto inserts = ConvertToEntries(changeData->second[OP_INSERT]); + auto updates = ConvertToEntries(changeData->second[OP_UPDATE]); + auto deletes = ConvertToEntries(changeData->second[OP_DELETE]); + ChangeNotification change(std::move(inserts), std::move(updates), std::move(deletes), {}, false); + observer->OnChange(change); + } + return E_OK; +} + +sptr KVDBWatcher::GetObserver() const +{ + std::shared_lock lock(mutex_); + return observer_; +} + +void KVDBWatcher::SetObserver(sptr observer) +{ + std::unique_lock lock(mutex_); + if (observer_ == observer) { + return; + } + observer_ = observer; +} + +std::vector KVDBWatcher::ConvertToEntries(const std::vector &values) +{ + std::vector changeData{}; + for (auto &info : values) { + auto key = std::get_if(&info[0]); + if (key->empty()) { + continue; + } + auto value = std::get_if(&info[1]); + Entry tmpEntry{ *key, *value }; + changeData.push_back(tmpEntry); + } + return changeData; +} +} // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/service/kvdb/kvdb_watcher.h b/services/distributeddataservice/service/kvdb/kvdb_watcher.h new file mode 100644 index 000000000..e9d36b94a --- /dev/null +++ b/services/distributeddataservice/service/kvdb/kvdb_watcher.h @@ -0,0 +1,42 @@ +/* + * 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. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_WATCHER_H +#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_WATCHER_H +#include +#include + +#include "ikvstore_observer.h" +#include "kv_store_changed_data.h" +#include "store/general_value.h" +#include "store/general_watcher.h" + +namespace OHOS::DistributedKv { +class KVDBWatcher : public DistributedData::GeneralWatcher { +public: + using DBEntry = DistributedDB::Entry; + KVDBWatcher(); + int32_t OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) override; + int32_t OnChange(const Origin &origin, const GeneralWatcher::Fields &fields, ChangeData &&datas) override; + sptr GetObserver() const; + void SetObserver(sptr observer); + +private: + std::vector ConvertToEntries(const std::vector &values); + mutable std::shared_mutex mutex_; + sptr observer_; +}; +} // namespace OHOS::DistributedKv +#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_WATCHER_H diff --git a/services/distributeddataservice/service/kvdb/store_cache.cpp b/services/distributeddataservice/service/kvdb/store_cache.cpp deleted file mode 100644 index fa04d6804..000000000 --- a/services/distributeddataservice/service/kvdb/store_cache.cpp +++ /dev/null @@ -1,300 +0,0 @@ -/* - * Copyright (c) 2022 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 "StoreCache" -#include "store_cache.h" - -#include "account/account_delegate.h" -#include "crypto_manager.h" -#include "device_matrix.h" -#include "directory/directory_manager.h" -#include "log_print.h" -#include "metadata/meta_data_manager.h" -#include "metadata/secret_key_meta_data.h" -#include "types.h" -#include "utils/anonymous.h" -namespace OHOS::DistributedKv { -using namespace OHOS::DistributedData; -constexpr int64_t StoreCache::INTERVAL; -StoreCache::Store StoreCache::GetStore(const StoreMetaData &data, std::shared_ptr observers, - DBStatus &status) -{ - Store store = nullptr; - status = DBStatus::NOT_FOUND; - stores_.Compute(data.tokenId, [&](const auto &key, std::map &stores) { - auto it = stores.find(data.storeId); - if (it != stores.end()) { - it->second.SetObservers(observers); - store = it->second; - return true; - } - - DBStore *dbStore = nullptr; - DBManager manager(data.appId, data.user, data.instanceId); - manager.SetKvStoreConfig({ DirectoryManager::GetInstance().GetStorePath(data) }); - manager.GetKvStore(data.storeId, GetDBOption(data, GetDBPassword(data)), - [&status, &dbStore](auto dbStatus, auto *tmpStore) { - status = dbStatus; - dbStore = tmpStore; - }); - - if (dbStore == nullptr) { - return !stores.empty(); - } - - if (data.isAutoSync) { - auto code = DeviceMatrix::GetInstance().GetCode(data); - dbStore->SetRemotePushFinishedNotify([code](const DistributedDB::RemotePushNotifyInfo &info) { - DeviceMatrix::GetInstance().OnExchanged(info.deviceId, code, true); - }); - bool param = true; - auto data = static_cast(¶m); - dbStore->Pragma(DistributedDB::SET_SYNC_RETRY, data); - } - - auto result = stores.emplace(std::piecewise_construct, std::forward_as_tuple(data.storeId), - std::forward_as_tuple(dbStore, observers)); - store = result.first->second; - return !stores.empty(); - }); - - executors_->Schedule(std::chrono::minutes(INTERVAL), std::bind(&StoreCache::GarbageCollect, this)); - return store; -} - -void StoreCache::CloseStore(uint32_t tokenId, const std::string &storeId) -{ - stores_.ComputeIfPresent(tokenId, [&storeId](auto &key, std::map &delegates) { - DBManager manager("", ""); - auto it = delegates.find(storeId); - if (it != delegates.end()) { - it->second.Close(manager); - delegates.erase(it); - } - return !delegates.empty(); - }); -} - -void StoreCache::CloseExcept(const std::set &users) -{ - DBManager manager("", ""); - stores_.EraseIf([&manager, &users](const auto &tokenId, std::map &delegates) { - auto userId = AccountDelegate::GetInstance()->GetUserByToken(tokenId); - if (users.count(userId) != 0) { - return delegates.empty(); - } - for (auto it = delegates.begin(); it != delegates.end();) { - // if the kv store is BUSY we wait more INTERVAL minutes again - if (!it->second.Close(manager)) { - ++it; - } else { - it = delegates.erase(it); - } - } - return delegates.empty(); - }); -} - -void StoreCache::SetObserver(uint32_t tokenId, const std::string &storeId, std::shared_ptr observers) -{ - stores_.ComputeIfPresent(tokenId, [&storeId, &observers](auto &key, auto &stores) { - ZLOGD("tokenId:0x%{public}x storeId:%{public}s observers:%{public}zu", key, Anonymous::Change(storeId).c_str(), - observers ? observers->size() : size_t(0)); - auto it = stores.find(storeId); - if (it != stores.end()) { - it->second.SetObservers(observers); - } - return true; - }); -} - -void StoreCache::GarbageCollect() -{ - DBManager manager("", ""); - auto current = std::chrono::steady_clock::now(); - stores_.EraseIf([&manager, ¤t](auto &key, std::map &delegates) { - for (auto it = delegates.begin(); it != delegates.end();) { - // if the kv store is BUSY we wait more INTERVAL minutes again - if ((it->second < current) && it->second.Close(manager)) { - it = delegates.erase(it); - } else { - ++it; - } - } - return delegates.empty(); - }); - if (!stores_.Empty()) { - ZLOGD("stores size:%{public}zu", stores_.Size()); - executors_->Schedule(std::chrono::minutes(INTERVAL), std::bind(&StoreCache::GarbageCollect, this)); - } -} - -StoreCache::DBOption StoreCache::GetDBOption(const StoreMetaData &data, const DBPassword &password) -{ - DBOption dbOption; - dbOption.syncDualTupleMode = true; // tuple of (appid+storeid) - dbOption.createIfNecessary = false; - dbOption.isMemoryDb = false; - dbOption.isEncryptedDb = data.isEncrypt; - dbOption.isNeedCompressOnSync = data.isNeedCompress; - if (data.isEncrypt) { - dbOption.cipher = DistributedDB::CipherType::AES_256_GCM; - dbOption.passwd = password; - } - - if (data.storeType == KvStoreType::SINGLE_VERSION) { - dbOption.conflictResolvePolicy = DistributedDB::LAST_WIN; - } else if (data.storeType == KvStoreType::DEVICE_COLLABORATION) { - dbOption.conflictResolvePolicy = DistributedDB::DEVICE_COLLABORATION; - } - - dbOption.schema = data.schema; - dbOption.createDirByStoreIdOnly = true; - dbOption.secOption = GetDBSecurity(data.securityLevel); - return dbOption; -} - -StoreCache::DBSecurity StoreCache::GetDBSecurity(int32_t secLevel) -{ - if (secLevel < SecurityLevel::NO_LABEL || secLevel > SecurityLevel::S4) { - return { DistributedDB::NOT_SET, DistributedDB::ECE }; - } - if (secLevel == SecurityLevel::S3) { - return { DistributedDB::S3, DistributedDB::SECE }; - } - if (secLevel == SecurityLevel::S4) { - return { DistributedDB::S4, DistributedDB::ECE }; - } - return { secLevel, DistributedDB::ECE }; -} - -StoreCache::DBPassword StoreCache::GetDBPassword(const StoreMetaData &data) -{ - DBPassword dbPassword; - if (!data.isEncrypt) { - return dbPassword; - } - - SecretKeyMetaData secretKey; - secretKey.storeType = data.storeType; - auto storeKey = data.GetSecretKey(); - MetaDataManager::GetInstance().LoadMeta(storeKey, secretKey, true); - std::vector password; - CryptoManager::GetInstance().Decrypt(secretKey.sKey, password); - dbPassword.SetValue(password.data(), password.size()); - password.assign(password.size(), 0); - return dbPassword; -} - -void StoreCache::SetThreadPool(std::shared_ptr executors) -{ - executors_ = executors; -} - -StoreCache::DBStoreDelegate::DBStoreDelegate(DBStore *delegate, std::shared_ptr observers) - : time_(std::chrono::steady_clock::now() + std::chrono::minutes(INTERVAL)), delegate_(delegate) -{ - SetObservers(std::move(observers)); -} - -StoreCache::DBStoreDelegate::~DBStoreDelegate() -{ - if (delegate_ != nullptr) { - delegate_->UnRegisterObserver(this); - } - DBManager manager("", ""); - manager.CloseKvStore(delegate_); - delegate_ = nullptr; -} - -StoreCache::DBStoreDelegate::operator std::shared_ptr () -{ - time_ = std::chrono::steady_clock::now() + std::chrono::minutes(INTERVAL); - mutex_.lock_shared(); - if (delegate_ == nullptr) { - mutex_.unlock_shared(); - return nullptr; - } - return std::shared_ptr(delegate_, [this](DBStore *) { mutex_.unlock_shared();}); -} - -bool StoreCache::DBStoreDelegate::operator<(const Time &time) const -{ - return time_ < time; -} - -bool StoreCache::DBStoreDelegate::Close(DBManager &manager) -{ - std::unique_lock lock(mutex_); - if (delegate_ != nullptr) { - delegate_->UnRegisterObserver(this); - auto status = manager.CloseKvStore(delegate_); - if (status == DBStatus::BUSY) { - return false; - } - delegate_ = nullptr; - } - return true; -} - -void StoreCache::DBStoreDelegate::OnChange(const DistributedDB::KvStoreChangedData &data) -{ - if (observers_ == nullptr || delegate_ == nullptr) { - ZLOGE("already closed"); - return; - } - - time_ = std::chrono::steady_clock::now() + std::chrono::minutes(INTERVAL); - auto observers = observers_; - std::vector key; - auto inserts = Convert(data.GetEntriesInserted()); - auto updates = Convert(data.GetEntriesUpdated()); - auto deletes = Convert(data.GetEntriesDeleted()); - ZLOGD("C:%{public}zu U:%{public}zu D:%{public}zu storeId:%{public}s", inserts.size(), updates.size(), - deletes.size(), delegate_->GetStoreId().c_str()); - ChangeNotification change(std::move(inserts), std::move(updates), std::move(deletes), {}, false); - for (auto &observer : *observers) { - if (observer == nullptr) { - continue; - } - observer->OnChange(change); - } -} - -void StoreCache::DBStoreDelegate::SetObservers(std::shared_ptr observers) -{ - if (observers_ == observers || delegate_ == nullptr) { - return; - } - - observers_ = observers; - - if (observers_ != nullptr && !observers_->empty()) { - ZLOGD("storeId:%{public}s observers:%{public}zu", delegate_->GetStoreId().c_str(), observers_->size()); - delegate_->RegisterObserver({}, DistributedDB::OBSERVER_CHANGES_FOREIGN, this); - } -} - -std::vector StoreCache::DBStoreDelegate::Convert(const std::list &dbEntries) -{ - std::vector entries; - for (const auto &entry : dbEntries) { - Entry tmpEntry; - tmpEntry.key = entry.key; - tmpEntry.value = entry.value; - entries.push_back(tmpEntry); - } - return entries; -} -}; // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/service/kvdb/store_cache.h b/services/distributeddataservice/service/kvdb/store_cache.h deleted file mode 100644 index 37e5133df..000000000 --- a/services/distributeddataservice/service/kvdb/store_cache.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2022 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_DISTRIBUTED_DATA_SERVICE_KVDB_STORE_CACHE_H -#define OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_STORE_CACHE_H -#include -#include -#include -#include - -#include "concurrent_map.h" -#include "executor_pool.h" -#include "ikvstore_observer.h" -#include "kv_store_nb_delegate.h" -#include "metadata/store_meta_data.h" -#include "refbase.h" - -namespace OHOS::DistributedKv { -class StoreCache { -public: - template - struct Less { - public: - bool operator()(const sptr &x, const sptr &y) const - { - return x.GetRefPtr() < y.GetRefPtr(); - } - }; - using DBStatus = DistributedDB::DBStatus; - using DBStore = DistributedDB::KvStoreNbDelegate; - using Store = std::shared_ptr; - using DBManager = DistributedDB::KvStoreDelegateManager; - using DBObserver = DistributedDB::KvStoreObserver; - using DBChangeData = DistributedDB::KvStoreChangedData; - using DBEntry = DistributedDB::Entry; - using Observers = std::set, Less>; - using StoreMetaData = OHOS::DistributedData::StoreMetaData; - using Time = std::chrono::steady_clock::time_point; - using DBOption = DistributedDB::KvStoreNbDelegate::Option; - using DBSecurity = DistributedDB::SecurityOption; - using DBPassword = DistributedDB::CipherPassword; - - struct DBStoreDelegate : public DBObserver { - DBStoreDelegate(DBStore *delegate, std::shared_ptr observers); - ~DBStoreDelegate(); - operator std::shared_ptr (); - bool operator<(const Time &time) const; - bool Close(DBManager &manager); - void OnChange(const DBChangeData &data) override; - void SetObservers(std::shared_ptr observers); - - private: - std::vector Convert(const std::list &dbEntries); - mutable Time time_; - DBStore *delegate_ = nullptr; - std::shared_ptr observers_ = nullptr; - std::shared_mutex mutex_; - }; - - Store GetStore(const StoreMetaData &data, std::shared_ptr observers, DBStatus &status); - void CloseStore(uint32_t tokenId, const std::string &storeId); - void CloseExcept(const std::set &users); - void SetObserver(uint32_t tokenId, const std::string &storeId, std::shared_ptr observers); - static DBOption GetDBOption(const StoreMetaData &data, const DBPassword &password); - static DBSecurity GetDBSecurity(int32_t secLevel); - static DBPassword GetDBPassword(const StoreMetaData &data); - void SetThreadPool(std::shared_ptr executors); -private: - void GarbageCollect(); - static constexpr int64_t INTERVAL = 1; - ConcurrentMap> stores_; - std::shared_ptr executors_; -}; -} // namespace OHOS::DistributedKv -#endif // OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_STORE_CACHE_H diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index 38a370e88..11232567b 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -22,10 +22,10 @@ #include "crypto_manager.h" #include "device_manager_adapter.h" #include "directory/directory_manager.h" +#include "kvdb_general_store.h" #include "log_print.h" #include "metadata/meta_data_manager.h" #include "metadata/secret_key_meta_data.h" -#include "store_cache.h" namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; using system_clock = std::chrono::system_clock; @@ -91,7 +91,7 @@ Upgrade::DBStatus Upgrade::ExportStore(const StoreMeta &old, const StoreMeta &me void Upgrade::UpdatePassword(const StoreMeta &meta, const std::vector &password) { if (!meta.isEncrypt) { - return ; + return; } SecretKeyMetaData secretKey; @@ -146,7 +146,7 @@ Upgrade::AutoStore Upgrade::GetDBStore(const StoreMeta &meta, const std::vector< DBPassword password; password.SetValue(pwd.data(), pwd.size()); AutoStore dbStore(nullptr, release); - manager.GetKvStore(meta.storeId, StoreCache::GetDBOption(meta, password), + manager.GetKvStore(meta.storeId, KVDBGeneralStore::GetDBOption(meta, password), [&dbStore](auto dbStatus, auto *tmpStore) { dbStore.reset(tmpStore); }); @@ -171,4 +171,4 @@ std::string Upgrade::GetEncryptedUuidByMeta(const StoreMeta &meta) calcUuid_.Insert(keyUuid, uuid); return uuid; } -} \ No newline at end of file +} // namespace OHOS::DistributedKv \ No newline at end of file diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index ab0c7df88..3db33de90 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.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 @@ -18,6 +18,7 @@ #include #include + #include "cache_cursor.h" #include "cloud/asset_loader.h" #include "cloud/cloud_db.h" @@ -42,7 +43,7 @@ namespace OHOS::DistributedRdb { using namespace DistributedData; using namespace DistributedDB; using namespace NativeRdb; -using namespace CloudData; +using namespace CloudData; using namespace std::chrono; using DBField = DistributedDB::Field; using DBTable = DistributedDB::TableSchema; @@ -119,8 +120,13 @@ int32_t RdbGeneralStore::BindSnapshots(std::shared_ptr> &cloudDBs) { + if (cloudDBs.empty()) { + return GeneralError::E_OK; + } + auto database = cloudDBs.begin()->second.first; + auto bindInfo = cloudDBs.begin()->second.second; if (bindInfo.db_ == nullptr || bindInfo.loader_ == nullptr) { return GeneralError::E_INVALID_ARGS; } @@ -420,7 +426,7 @@ std::shared_ptr RdbGeneralStore::Query(const std::string &table, GenQuer return nullptr; } -int32_t RdbGeneralStore::MergeMigratedData(const std::string& tableName, VBuckets&& values) +int32_t RdbGeneralStore::MergeMigratedData(const std::string &tableName, VBuckets &&values) { std::shared_lock lock(rwMutex_); if (delegate_ == nullptr) { @@ -463,9 +469,9 @@ int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &qu return status == DistributedDB::OK ? GeneralError::E_OK : GeneralError::E_ERROR; } -std::shared_ptr RdbGeneralStore::PreSharing(GenQuery& query) +std::shared_ptr RdbGeneralStore::PreSharing(GenQuery &query) { - RdbQuery* rdbQuery = nullptr; + RdbQuery *rdbQuery = nullptr; auto ret = query.QueryInterface(rdbQuery); if (ret != GeneralError::E_OK || rdbQuery == nullptr) { ZLOGE("not RdbQuery!"); @@ -502,7 +508,7 @@ std::shared_ptr RdbGeneralStore::PreSharing(GenQuery& query) return std::make_shared(std::move(values)); } -VBuckets RdbGeneralStore::ExtractExtend(VBuckets& values) const +VBuckets RdbGeneralStore::ExtractExtend(VBuckets &values) const { VBuckets extends(values.size()); for (auto value = values.begin(), extend = extends.begin(); value != values.end() && extend != extends.end(); @@ -520,13 +526,13 @@ VBuckets RdbGeneralStore::ExtractExtend(VBuckets& values) const return extends; } -std::string RdbGeneralStore::BuildSql(const std::string& table, const std::string& statement, - const std::vector& columns) const +std::string RdbGeneralStore::BuildSql( + const std::string &table, const std::string &statement, const std::vector &columns) const { std::string sql = "select "; sql.append(CLOUD_GID); std::string sqlNode = "select rowid"; - for (auto& column : columns) { + for (auto &column : columns) { sql.append(", ").append(column); sqlNode.append(", ").append(column); } @@ -627,10 +633,10 @@ RdbGeneralStore::DBBriefCB RdbGeneralStore::GetDBBriefCB(DetailAsync async) RdbGeneralStore::DBProcessCB RdbGeneralStore::GetDBProcessCB(DetailAsync async, uint32_t highMode) { if (!async && (highMode == MANUAL_SYNC_MODE || !async_)) { - return [](auto&) {}; + return [](auto &) {}; } - return [async, autoAsync = async_, highMode](const std::map& processes) { + return [async, autoAsync = async_, highMode](const std::map &processes) { DistributedData::GenDetails details; for (auto &[id, process] : processes) { auto &detail = details[id]; @@ -714,8 +720,8 @@ int32_t RdbGeneralStore::SetDistributedTables(const std::vector &ta return GeneralError::E_OK; } -int32_t RdbGeneralStore::SetTrackerTable(const std::string& tableName, const std::set& trackerColNames, - const std::string& extendColName) +int32_t RdbGeneralStore::SetTrackerTable( + const std::string &tableName, const std::set &trackerColNames, const std::string &extendColName) { std::shared_lock lock(rwMutex_); if (delegate_ == nullptr) { @@ -782,7 +788,7 @@ int32_t RdbGeneralStore::UnregisterDetailProgressObserver() return GenErr::E_OK; } -VBuckets RdbGeneralStore::QuerySql(const std::string& sql, Values &&args) +VBuckets RdbGeneralStore::QuerySql(const std::string &sql, Values &&args) { std::vector changedData; std::vector bindArgs = ValueProxy::Convert(std::move(args)); @@ -811,7 +817,8 @@ void RdbGeneralStore::ObserverProxy::OnChange(const DBChangedIF &data) data.GetStoreProperty(property); genOrigin.id.push_back(networkId); genOrigin.store = storeId_; - watcher_->OnChange(genOrigin, {}, {}); + GeneralWatcher::ChangeInfo changeInfo{}; + watcher_->OnChange(genOrigin, {}, std::move(changeInfo)); return; } @@ -823,9 +830,9 @@ void RdbGeneralStore::ObserverProxy::OnChange(DBOrigin origin, const std::string ZLOGD("store:%{public}s table:%{public}s data change from :%{public}s", Anonymous::Change(storeId_).c_str(), Anonymous::Change(data.tableName).c_str(), Anonymous::Change(originalId).c_str()); GenOrigin genOrigin; - genOrigin.origin = (origin == DBOrigin::ORIGIN_LOCAL) ? GenOrigin::ORIGIN_LOCAL - : (origin == DBOrigin::ORIGIN_CLOUD) ? GenOrigin::ORIGIN_CLOUD - : GenOrigin::ORIGIN_NEARBY; + genOrigin.origin = (origin == DBOrigin::ORIGIN_LOCAL) + ? GenOrigin::ORIGIN_LOCAL + : (origin == DBOrigin::ORIGIN_CLOUD) ? GenOrigin::ORIGIN_CLOUD : GenOrigin::ORIGIN_NEARBY; genOrigin.dataType = data.type == DistributedDB::ASSET ? GenOrigin::ASSET_DATA : GenOrigin::BASIC_DATA; genOrigin.id.push_back(originalId); genOrigin.store = storeId_; diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h index d10754be4..25c62426d 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -47,7 +47,7 @@ public: explicit RdbGeneralStore(const StoreMetaData &meta); ~RdbGeneralStore(); - int32_t Bind(const Database &database, BindInfo bindInfo) override; + int32_t Bind(const std::map> &cloudDBs) override; bool IsBound() override; bool IsValid(); int32_t Execute(const std::string &table, const std::string &sql) override; diff --git a/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn index a85fde35f..3c4bb4379 100644 --- a/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn @@ -69,11 +69,12 @@ ohos_fuzztest("KvdbServiceStubFuzzTest") { "${data_service_path}/service/crypto/src/crypto_manager.cpp", "${data_service_path}/service/kvdb/auth_delegate.cpp", "${data_service_path}/service/kvdb/kvdb_exporter.cpp", + "${data_service_path}/service/kvdb/kvdb_general_store.cpp", "${data_service_path}/service/kvdb/kvdb_service_impl.cpp", "${data_service_path}/service/kvdb/kvdb_service_stub.cpp", + "${data_service_path}/service/kvdb/kvdb_watcher.cpp", "${data_service_path}/service/kvdb/kvstore_sync_manager.cpp", "${data_service_path}/service/kvdb/query_helper.cpp", - "${data_service_path}/service/kvdb/store_cache.cpp", "${data_service_path}/service/kvdb/upgrade.cpp", "${data_service_path}/service/kvdb/user_delegate.cpp", "${data_service_path}/service/matrix/src/device_matrix.cpp", -- Gitee From 0ea1fee09778d37f4f8f794f607f7216540ffa49 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Sun, 7 Apr 2024 11:27:27 +0800 Subject: [PATCH 02/15] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/kvdb/kvdb_general_store.cpp | 6 ++--- .../service/kvdb/kvdb_service_impl.cpp | 26 +++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index 0ec84318a..40cec1d9f 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -158,9 +158,9 @@ int32_t KVDBGeneralStore::Bind(const std::map schemas{}; - for (auto &iter : cloudDBs) { - auto database = iter.second.first; - auto bindInfo = iter.second.second; + for (auto &[userId, cloudDB] : cloudDBs) { + auto database = cloudDB.first; + auto bindInfo = cloudDB.second; if (bindInfo.db_ == nullptr) { return GeneralError::E_INVALID_ARGS; diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 122665cdd..960232f6d 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -490,20 +490,18 @@ Status KVDBServiceImpl::BeforeCreate(const AppId &appId, const StoreId &storeId, old.isEncrypt, meta.isEncrypt, old.area, meta.area, options.persistent); return Status::STORE_META_CHANGED; } - if (options.cloudSync) { - if (executors_ != nullptr) { - DistributedData::StoreInfo storeInfo; - storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); - storeInfo.bundleName = appId.appId; - storeInfo.storeName = storeId; - storeInfo.instanceId = GetInstIndex(storeInfo.tokenId, appId); - storeInfo.user = std::stoi(meta.user); - storeInfo.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; - executors_->Execute([storeInfo]() { - auto event = std::make_unique(CloudEvent::GET_SCHEMA, storeInfo); - EventCenter::GetInstance().PostEvent(move(event)); - }); - } + if (options.cloudSync || executors_ != nullptr) { + DistributedData::StoreInfo storeInfo; + storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + storeInfo.bundleName = appId.appId; + storeInfo.storeName = storeId; + storeInfo.instanceId = GetInstIndex(storeInfo.tokenId, appId); + storeInfo.user = std::stoi(meta.user); + storeInfo.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + executors_->Execute([storeInfo]() { + auto event = std::make_unique(CloudEvent::GET_SCHEMA, storeInfo); + EventCenter::GetInstance().PostEvent(move(event)); + }); } auto dbStatus = DBStatus::OK; -- Gitee From 92da6d32589a489a32546cfcceea6390e1aa9a47 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Sun, 7 Apr 2024 14:26:48 +0800 Subject: [PATCH 03/15] =?UTF-8?q?=E5=8E=BB=E6=8E=89options=E4=B8=AD?= =?UTF-8?q?=E7=9A=84cloudSync=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../include/metadata/store_meta_data.h | 1 - .../include/metadata/store_meta_data_local.h | 1 - .../framework/include/store/auto_cache.h | 1 + .../framework/include/store/store_info.h | 1 - .../framework/metadata/store_meta_data.cpp | 2 -- .../metadata/store_meta_data_local.cpp | 2 -- .../framework/store/auto_cache.cpp | 18 +++++++++++++ .../service/kvdb/kvdb_general_store.cpp | 14 ++++------ .../service/kvdb/kvdb_general_store.h | 3 ++- .../service/kvdb/kvdb_service_impl.cpp | 26 +++++++------------ .../service/kvdb/kvdb_service_impl.h | 2 +- .../service/kvdb/kvdb_watcher.cpp | 19 +++++++------- .../service/kvdb/kvdb_watcher.h | 6 ++--- 13 files changed, 48 insertions(+), 48 deletions(-) diff --git a/services/distributeddataservice/framework/include/metadata/store_meta_data.h b/services/distributeddataservice/framework/include/metadata/store_meta_data.h index dc67495da..56c012b9d 100644 --- a/services/distributeddataservice/framework/include/metadata/store_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/store_meta_data.h @@ -35,7 +35,6 @@ struct API_EXPORT StoreMetaData final : public Serializable { bool isManualClean = false; bool isSearchable = false; bool isNeedCompress = false; - bool cloudSync = false; bool isPublic = false; int32_t storeType = -1; int32_t securityLevel = 0; diff --git a/services/distributeddataservice/framework/include/metadata/store_meta_data_local.h b/services/distributeddataservice/framework/include/metadata/store_meta_data_local.h index 3cabd4360..301810c77 100644 --- a/services/distributeddataservice/framework/include/metadata/store_meta_data_local.h +++ b/services/distributeddataservice/framework/include/metadata/store_meta_data_local.h @@ -37,7 +37,6 @@ struct API_EXPORT StoreMetaDataLocal final : public Serializable { bool isDirty = false; bool isEncrypt = false; bool isPublic = false; - bool cloudSync = false; std::string dataDir = ""; std::string schema = ""; std::vector policies {}; diff --git a/services/distributeddataservice/framework/include/store/auto_cache.h b/services/distributeddataservice/framework/include/store/auto_cache.h index 7967fe87d..69701343d 100644 --- a/services/distributeddataservice/framework/include/store/auto_cache.h +++ b/services/distributeddataservice/framework/include/store/auto_cache.h @@ -72,6 +72,7 @@ private: int32_t GetUser() const; void SetObservers(const Watchers &watchers); int32_t OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) override; + int32_t OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas) override; private: mutable Time time_; diff --git a/services/distributeddataservice/framework/include/store/store_info.h b/services/distributeddataservice/framework/include/store/store_info.h index 0e3dbb6f6..4eddd5aa7 100644 --- a/services/distributeddataservice/framework/include/store/store_info.h +++ b/services/distributeddataservice/framework/include/store/store_info.h @@ -27,7 +27,6 @@ struct StoreInfo { int32_t user = 0; std::string deviceId; bool isPublic = false; - bool cloudSync = false; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_INFO_H diff --git a/services/distributeddataservice/framework/metadata/store_meta_data.cpp b/services/distributeddataservice/framework/metadata/store_meta_data.cpp index 47915b3fd..60b29fd78 100644 --- a/services/distributeddataservice/framework/metadata/store_meta_data.cpp +++ b/services/distributeddataservice/framework/metadata/store_meta_data.cpp @@ -52,7 +52,6 @@ bool StoreMetaData::Marshal(json &node) const SetValue(node[GET_NAME(user)], user); SetValue(node[GET_NAME(account)], account); SetValue(node[GET_NAME(isPublic)], isPublic); - SetValue(node[GET_NAME(cloudSync)], cloudSync); // compatible with the versions which lower than VERSION_TAG_0000 SetValue(node[GET_NAME(kvStoreType)], storeType); @@ -91,7 +90,6 @@ bool StoreMetaData::Unmarshal(const json &node) GetValue(node, GET_NAME(user), user); GetValue(node, GET_NAME(account), account); GetValue(node, GET_NAME(isPublic), isPublic); - GetValue(node, GET_NAME(cloudSync), cloudSync); // compatible with the older versions if (version < FIELD_CHANGED_TAG) { diff --git a/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp b/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp index c3352a22b..d95daa906 100644 --- a/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp +++ b/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp @@ -68,7 +68,6 @@ bool StoreMetaDataLocal::Marshal(json &node) const SetValue(node[GET_NAME(schema)], schema); SetValue(node[GET_NAME(policies)], policies); SetValue(node[GET_NAME(isPublic)], isPublic); - SetValue(node[GET_NAME(cloudSync)], cloudSync); return true; } @@ -82,7 +81,6 @@ bool StoreMetaDataLocal::Unmarshal(const json &node) GetValue(node, GET_NAME(schema), schema); GetValue(node, GET_NAME(policies), policies); GetValue(node, GET_NAME(isPublic), isPublic); - GetValue(node, GET_NAME(cloudSync), cloudSync); return true; } diff --git a/services/distributeddataservice/framework/store/auto_cache.cpp b/services/distributeddataservice/framework/store/auto_cache.cpp index b7a5b7d1d..5705e6367 100644 --- a/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/services/distributeddataservice/framework/store/auto_cache.cpp @@ -264,4 +264,22 @@ int32_t AutoCache::Delegate::OnChange(const Origin &origin, const PRIFields &pri } return Error::E_OK; } + +int32_t AutoCache::Delegate::OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas) +{ + Watchers watchers; + { + std::unique_lock lock(mutex_); + watchers = watchers_; + } + size_t remain = watchers.size(); + for (auto &watcher : watchers) { + remain--; + if (watcher == nullptr) { + continue; + } + watcher->OnChange(origin, fields, (remain != 0) ? ChangeData(datas) : std::move(datas)); + } + return Error::E_OK; +} } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index 40cec1d9f..08b5018e4 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -127,7 +127,6 @@ KVDBGeneralStore::KVDBGeneralStore(const StoreMetaData &meta) : manager_(meta.ap storeInfo_.instanceId = meta.instanceId; storeInfo_.user = std::stoi(meta.user); storeInfo_.isPublic = meta.isPublic; - storeInfo_.cloudSync = meta.cloudSync; } KVDBGeneralStore::~KVDBGeneralStore() @@ -157,7 +156,7 @@ int32_t KVDBGeneralStore::Bind(const std::map schemas{}; + std::map schemas{}; for (auto &[userId, cloudDB] : cloudDBs) { auto database = cloudDB.first; auto bindInfo = cloudDB.second; @@ -180,7 +179,7 @@ int32_t KVDBGeneralStore::Bind(const std::map(BindEvent::BIND_SNAPSHOT, std::move(eventInfo)); EventCenter::GetInstance().PostEvent(std::move(evt)); bindInfos_.insert(std::move(bindInfo)); - dbClouds_.insert({ iter.first, std::make_shared(bindInfo.db_, nullptr) }); + dbClouds_.insert({ userId, std::make_shared(bindInfo.db_, nullptr) }); DBSchema schema; schema.tables.resize(database.tables.size()); @@ -198,14 +197,14 @@ int32_t KVDBGeneralStore::Bind(const std::map lock(rwMutex_); if (delegate_ == nullptr) { return GeneralError::E_ALREADY_CLOSED; } delegate_->SetCloudDB(dbClouds_); - delegate_->SetCloudDBSchema(schemas); + delegate_->SetCloudDbSchema(std::move(schemas)); return GeneralError::E_OK; } @@ -327,9 +326,6 @@ int32_t KVDBGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &q dbStatus = delegate_->Sync(devices, dbMode, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); } } else if (syncMode > NEARBY_END && syncMode < CLOUD_END) { - if (!storeInfo_.cloudSync) { - return GeneralError::E_NOT_SUPPORT; - } DistributedDB::CloudSyncOption syncOption; syncOption.devices = devices; syncOption.mode = dbMode; @@ -342,7 +338,7 @@ int32_t KVDBGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &q } else { syncOption.users.push_back(std::to_string(storeInfo_.user)); } - dbStatus = delegate_->Sync({ devices, dbMode, {}, wait, false }, nullptr); + dbStatus = delegate_->Sync(syncOption, nullptr); } else { dbStatus = DistributedDB::INVALID_ARGS; } diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.h b/services/distributeddataservice/service/kvdb/kvdb_general_store.h index 3cfc0b971..b23685b32 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.h +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.h @@ -105,7 +105,8 @@ private: using DBOrigin = DistributedDB::Origin; using GenOrigin = Watcher::Origin; ~ObserverProxy() = default; - void OnChange(DistributedDB::Origin origin, const std::string &originalId, DistributedDB::ChangedData &&data); + void OnChange( + DistributedDB::Origin origin, const std::string &originalId, DistributedDB::ChangedData &&data) override; void OnChange(const DistributedDB::KvStoreChangedData &data) override; void ConvertChangeData(const std::list &entries, std::vector &values); bool HasWatcher() const diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 960232f6d..e9aa53b56 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -229,11 +229,6 @@ Status KVDBServiceImpl::CloudSync(const AppId &appId, const StoreId &storeId, co { StoreMetaData metaData = GetStoreMetaData(appId, storeId); MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData); - if (!metaData.cloudSync) { - ZLOGE("appId:%{public}s storeId:%{public}s instanceId:%{public}d not supports cloud sync", - appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str(), metaData.instanceId); - return NOT_SUPPORT; - } DistributedData::StoreInfo storeInfo; storeInfo.bundleName = appId.appId; storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); @@ -490,7 +485,7 @@ Status KVDBServiceImpl::BeforeCreate(const AppId &appId, const StoreId &storeId, old.isEncrypt, meta.isEncrypt, old.area, meta.area, options.persistent); return Status::STORE_META_CHANGED; } - if (options.cloudSync || executors_ != nullptr) { + if (executors_ != nullptr) { DistributedData::StoreInfo storeInfo; storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); storeInfo.bundleName = appId.appId; @@ -675,7 +670,6 @@ void KVDBServiceImpl::AddOptions(const Options &options, StoreMetaData &metaData metaData.account = AccountDelegate::GetInstance()->GetCurrentAccountId(); metaData.isNeedCompress = options.isNeedCompress; metaData.isPublic = options.isPublic; - metaData.cloudSync = options.cloudSync; } void KVDBServiceImpl::SaveLocalMetaData(const Options &options, const StoreMetaData &metaData) @@ -687,7 +681,6 @@ void KVDBServiceImpl::SaveLocalMetaData(const Options &options, const StoreMetaD localMetaData.dataDir = DirectoryManager::GetInstance().GetStorePath(metaData); localMetaData.schema = options.schema; localMetaData.isPublic = options.isPublic; - localMetaData.cloudSync = options.cloudSync; for (auto &policy : options.policies) { OHOS::DistributedData::PolicyValue value; value.type = policy.type; @@ -830,13 +823,12 @@ Status KVDBServiceImpl::DoSyncBegin(const std::vector &devices, con if (devices.empty()) { return Status::INVALID_ARGUMENT; } - DistributedDB::DBStatus status = DistributedDB::OK; auto watcher = GetWatchers(meta.tokenId, meta.storeId); auto store = AutoCache::GetInstance().GetStore(meta, watcher); if (store == nullptr) { - ZLOGE("failed! status:%{public}d appId:%{public}s storeId:%{public}s dir:%{public}s", status, - meta.bundleName.c_str(), Anonymous::Change(meta.storeId).c_str(), meta.dataDir.c_str()); - return ConvertDbStatus(status); + ZLOGE("GetStore failed! appId:%{public}s storeId:%{public}s dir:%{public}s", meta.bundleName.c_str(), + Anonymous::Change(meta.storeId).c_str(), meta.dataDir.c_str()); + return Status::ERROR; } KVDBQuery query(info.query); if (!query.IsValidQuery()) { @@ -846,7 +838,7 @@ Status KVDBServiceImpl::DoSyncBegin(const std::vector &devices, con auto mode = ConvertGeneralSyncMode(SyncMode(info.mode), SyncAction(type)); auto ret = store->Sync( devices, mode, query, - [this, &complete](const GenDetails &result) mutable { + [this, complete](const GenDetails &result) mutable { auto deviceStatus = HandleGenDetails(result); complete(deviceStatus); }, @@ -999,7 +991,7 @@ void KVDBServiceImpl::SyncAgent::ReInit(pid_t pid, const AppId &appId) delayTimes_.clear(); count_ = 0; watcher_ = nullptr; - observer_ = nullptr; + observers_.clear(); } void KVDBServiceImpl::SyncAgent::SetWatcher(std::shared_ptr watcher) @@ -1007,16 +999,16 @@ void KVDBServiceImpl::SyncAgent::SetWatcher(std::shared_ptr watcher if (watcher_ != watcher) { watcher_ = watcher; if (watcher_ != nullptr) { - watcher_->SetObserver(observer_); + watcher_->SetObservers(observers_); } } } void KVDBServiceImpl::SyncAgent::SetObserver(sptr observer) { - observer_ = observer; + observers_.insert(observer); if (watcher_ != nullptr) { - watcher_->SetObserver(observer); + watcher_->SetObservers(observers_); } } diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h index 7251f4e5c..eee530d58 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h @@ -89,7 +89,7 @@ private: sptr callback_; std::map delayTimes_; std::shared_ptr watcher_; - sptr observer_; + std::set> observers_; void ReInit(pid_t pid, const AppId &appId); void SetObserver(sptr observer); void SetWatcher(std::shared_ptr watcher); diff --git a/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp b/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp index 477a68126..ef0f675e4 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp @@ -37,32 +37,31 @@ int32_t KVDBWatcher::OnChange(const Origin &origin, const Fields &fields, Change auto store = origin.store; auto changeData = datas.find(store); if (changeData != datas.end()) { - auto observer = GetObserver(); - if (observer == nullptr) { + auto observers = GetObservers(); + if (observers.empty()) { return E_NOT_INIT; } auto inserts = ConvertToEntries(changeData->second[OP_INSERT]); auto updates = ConvertToEntries(changeData->second[OP_UPDATE]); auto deletes = ConvertToEntries(changeData->second[OP_DELETE]); ChangeNotification change(std::move(inserts), std::move(updates), std::move(deletes), {}, false); - observer->OnChange(change); + for(auto &observer : observers) { + observer->OnChange(change); + } } return E_OK; } -sptr KVDBWatcher::GetObserver() const +std::set> KVDBWatcher::GetObservers() const { std::shared_lock lock(mutex_); - return observer_; + return observers_; } -void KVDBWatcher::SetObserver(sptr observer) +void KVDBWatcher::SetObservers(std::set> observers) { std::unique_lock lock(mutex_); - if (observer_ == observer) { - return; - } - observer_ = observer; + observers_ = std::move(observers); } std::vector KVDBWatcher::ConvertToEntries(const std::vector &values) diff --git a/services/distributeddataservice/service/kvdb/kvdb_watcher.h b/services/distributeddataservice/service/kvdb/kvdb_watcher.h index e9d36b94a..a7bc4944f 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_watcher.h +++ b/services/distributeddataservice/service/kvdb/kvdb_watcher.h @@ -30,13 +30,13 @@ public: KVDBWatcher(); int32_t OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) override; int32_t OnChange(const Origin &origin, const GeneralWatcher::Fields &fields, ChangeData &&datas) override; - sptr GetObserver() const; - void SetObserver(sptr observer); + std::set> GetObservers() const; + void SetObservers(std::set> observers); private: std::vector ConvertToEntries(const std::vector &values); mutable std::shared_mutex mutex_; - sptr observer_; + std::set> observers_; }; } // namespace OHOS::DistributedKv #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_WATCHER_H -- Gitee From 23d571fa11c576b75aa2ea591502fbcbe2e4f596 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Wed, 10 Apr 2024 18:01:19 +0800 Subject: [PATCH 04/15] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/cloud/sync_manager.cpp | 9 +++++---- .../service/kvdb/kvdb_general_store.cpp | 2 +- .../service/kvdb/kvdb_service_impl.cpp | 11 ++++++++++- .../service/kvdb/kvdb_service_impl.h | 1 + .../service/kvdb/kvdb_watcher.cpp | 8 +++++++- .../service/kvdb/kvdb_watcher.h | 1 + 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 38c18d36b..dbf9d8334 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -379,7 +379,7 @@ AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, return nullptr; } - if (!store->IsBound()) { // todo ֻ���ڹ������ʱ�򣬰󶨶���� + if (!store->IsBound()) { std::set activeUsers = UserDelegate::GetInstance().GetLocalUsers(); std::map> cloudDBs = {}; for (auto &activeUser : activeUsers) { @@ -394,13 +394,14 @@ AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, } auto dbMeta = schemaMeta.GetDataBase(meta.storeId); auto cloudDB = instance->ConnectCloudDB(meta.tokenId, dbMeta); - if (mustBind && cloudDB == nullptr) { + auto assetLoader = instance->ConnectAssetLoader(meta.tokenId, dbMeta); + if (mustBind && (cloudDB == nullptr || assetLoader == nullptr)) { ZLOGE("failed, no cloud DB <0x%{public}x %{public}s<->%{public}s>", meta.tokenId, Anonymous::Change(dbMeta.name).c_str(), Anonymous::Change(dbMeta.alias).c_str()); return nullptr; } - if (cloudDB != nullptr) { - GeneralStore::BindInfo bindInfo(std::move(cloudDB), nullptr); + if (cloudDB != nullptr || assetLoader != nullptr) { + GeneralStore::BindInfo bindInfo(std::move(cloudDB), std::move(assetLoader)); cloudDBs[activeUser] = std::make_pair(dbMeta, bindInfo); } } diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index 08b5018e4..e1dfbe003 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -178,8 +178,8 @@ int32_t KVDBGeneralStore::Bind(const std::map(BindEvent::BIND_SNAPSHOT, std::move(eventInfo)); EventCenter::GetInstance().PostEvent(std::move(evt)); - bindInfos_.insert(std::move(bindInfo)); dbClouds_.insert({ userId, std::make_shared(bindInfo.db_, nullptr) }); + bindInfos_.insert(std::move(bindInfo)); DBSchema schema; schema.tables.resize(database.tables.size()); diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index e9aa53b56..f778a5e33 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -558,7 +558,8 @@ int32_t KVDBServiceImpl::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const return false; } if (agent.watcher_ != nullptr) { - agent.watcher_->SetObserver(nullptr); + agent.watcher_->ClearObservers(); + agent.ClearObservers(); } auto stores = AutoCache::GetInstance().GetStoresIfPresent(key); for (auto store : stores) { @@ -1012,6 +1013,14 @@ void KVDBServiceImpl::SyncAgent::SetObserver(sptr observer } } +void KVDBServiceImpl::SyncAgent::ClearObservers() +{ + observers_.clear(); + if (watcher_ != nullptr) { + watcher_->ClearObservers(); + } +} + int32_t KVDBServiceImpl::OnBind(const BindInfo &bindInfo) { executors_ = bindInfo.executors; diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h index eee530d58..4c32f6a7c 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h @@ -93,6 +93,7 @@ private: void ReInit(pid_t pid, const AppId &appId); void SetObserver(sptr observer); void SetWatcher(std::shared_ptr watcher); + void ClearObservers(); }; class Factory { public: diff --git a/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp b/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp index ef0f675e4..73e4d8dc7 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp @@ -64,12 +64,18 @@ void KVDBWatcher::SetObservers(std::set> observers) observers_ = std::move(observers); } +void KVDBWatcher::ClearObservers() +{ + std::unique_lock lock(mutex_); + observers_.clear(); +} + std::vector KVDBWatcher::ConvertToEntries(const std::vector &values) { std::vector changeData{}; for (auto &info : values) { auto key = std::get_if(&info[0]); - if (key->empty()) { + if (key == nullptr) { continue; } auto value = std::get_if(&info[1]); diff --git a/services/distributeddataservice/service/kvdb/kvdb_watcher.h b/services/distributeddataservice/service/kvdb/kvdb_watcher.h index a7bc4944f..abfff5407 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_watcher.h +++ b/services/distributeddataservice/service/kvdb/kvdb_watcher.h @@ -32,6 +32,7 @@ public: int32_t OnChange(const Origin &origin, const GeneralWatcher::Fields &fields, ChangeData &&datas) override; std::set> GetObservers() const; void SetObservers(std::set> observers); + void ClearObservers(); private: std::vector ConvertToEntries(const std::vector &values); -- Gitee From ed3dc2abb483715d9da345a8dc83778720e10ae1 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Wed, 10 Apr 2024 20:19:10 +0800 Subject: [PATCH 05/15] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=9A=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=97=B6=E7=9A=84ConnectCloudDB=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../framework/cloud/cloud_server.cpp | 6 ++++-- .../framework/include/cloud/cloud_server.h | 5 +++-- .../distributeddataservice/service/cloud/sync_manager.cpp | 7 +++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/framework/cloud/cloud_server.cpp b/services/distributeddataservice/framework/cloud/cloud_server.cpp index 897a567d6..d0689dfe8 100644 --- a/services/distributeddataservice/framework/cloud/cloud_server.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_server.cpp @@ -50,12 +50,14 @@ int32_t CloudServer::Unsubscribe(int32_t userId, const std::map CloudServer::ConnectAssetLoader(uint32_t tokenId, const CloudServer::Database &dbMeta) +std::shared_ptr CloudServer::ConnectAssetLoader( + const std::string &bundleName, int user, const CloudServer::Database &dbMeta) { return nullptr; } -std::shared_ptr CloudServer::ConnectCloudDB(uint32_t tokenId, const CloudServer::Database &dbMeta) +std::shared_ptr CloudServer::ConnectCloudDB( + const std::string &bundleName, int user, const CloudServer::Database &dbMeta) { return nullptr; } diff --git a/services/distributeddataservice/framework/include/cloud/cloud_server.h b/services/distributeddataservice/framework/include/cloud/cloud_server.h index ebe4220c0..4b2a6ae0d 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_server.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_server.h @@ -33,8 +33,9 @@ public: virtual SchemaMeta GetAppSchema(int32_t userId, const std::string &bundleName); virtual int32_t Subscribe(int32_t userId, const std::map> &dbs); virtual int32_t Unsubscribe(int32_t userId, const std::map> &dbs); - virtual std::shared_ptr ConnectAssetLoader(uint32_t tokenId, const Database &dbMeta); - virtual std::shared_ptr ConnectCloudDB(uint32_t tokenId, const Database &dbMeta); + virtual std::shared_ptr ConnectAssetLoader( + const std::string &bundleName, int user, const Database &dbMeta); + virtual std::shared_ptr ConnectCloudDB(const std::string &bundleName, int user, const Database &dbMeta); virtual std::shared_ptr ConnectSharingCenter(int32_t userId, const std::string &bunleName); virtual void Clean(int32_t userId); virtual void ReleaseUserInfo(int32_t userId); diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index dbf9d8334..5d3367c9f 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -383,6 +383,9 @@ AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, std::set activeUsers = UserDelegate::GetInstance().GetLocalUsers(); std::map> cloudDBs = {}; for (auto &activeUser : activeUsers) { + if (activeUser == "0") { + continue; + } CloudInfo info; info.user = std::stoi(activeUser); SchemaMeta schemaMeta; @@ -393,8 +396,8 @@ AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, return nullptr; } auto dbMeta = schemaMeta.GetDataBase(meta.storeId); - auto cloudDB = instance->ConnectCloudDB(meta.tokenId, dbMeta); - auto assetLoader = instance->ConnectAssetLoader(meta.tokenId, dbMeta); + auto cloudDB = instance->ConnectCloudDB(meta.bundleName, info.user, dbMeta); + auto assetLoader = instance->ConnectAssetLoader(meta.bundleName, info.user, dbMeta); if (mustBind && (cloudDB == nullptr || assetLoader == nullptr)) { ZLOGE("failed, no cloud DB <0x%{public}x %{public}s<->%{public}s>", meta.tokenId, Anonymous::Change(dbMeta.name).c_str(), Anonymous::Change(dbMeta.alias).c_str()); -- Gitee From 8bb92557225bbdcea00017d0855dce1bf189701a Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 12 Apr 2024 14:58:09 +0800 Subject: [PATCH 06/15] =?UTF-8?q?=E7=AB=AF=E4=BA=91=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=95=B0=E6=8D=AE=E5=8F=98=E5=8C=96=E7=9B=91?= =?UTF-8?q?=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../framework/include/store/store_info.h | 1 - .../rust/extension/cloud_server_impl.cpp | 30 +++++----------- .../rust/extension/cloud_server_impl.h | 5 +-- .../service/kvdb/kvdb_general_store.cpp | 36 +++++-------------- .../service/kvdb/kvdb_general_store.h | 30 +++++----------- .../service/kvdb/kvdb_service_impl.cpp | 27 +++++++++----- .../service/kvdb/kvdb_service_impl.h | 1 + .../service/kvdb/kvdb_watcher.cpp | 32 +++++++++++++++++ .../service/kvdb/kvdb_watcher.h | 2 +- 9 files changed, 81 insertions(+), 83 deletions(-) diff --git a/services/distributeddataservice/framework/include/store/store_info.h b/services/distributeddataservice/framework/include/store/store_info.h index 4eddd5aa7..8138955aa 100644 --- a/services/distributeddataservice/framework/include/store/store_info.h +++ b/services/distributeddataservice/framework/include/store/store_info.h @@ -26,7 +26,6 @@ struct StoreInfo { int32_t instanceId = 0; int32_t user = 0; std::string deviceId; - bool isPublic = false; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_INFO_H diff --git a/services/distributeddataservice/rust/extension/cloud_server_impl.cpp b/services/distributeddataservice/rust/extension/cloud_server_impl.cpp index 22304b26a..316f28623 100644 --- a/services/distributeddataservice/rust/extension/cloud_server_impl.cpp +++ b/services/distributeddataservice/rust/extension/cloud_server_impl.cpp @@ -597,41 +597,27 @@ int32_t CloudServerImpl::DoUnsubscribe(std::shared_ptr serv return DBErr::E_OK; } -std::shared_ptr CloudServerImpl::ConnectAssetLoader(uint32_t tokenId, const DBMeta &dbMeta) +std::shared_ptr CloudServerImpl::ConnectAssetLoader( + const std::string &bundleName, int user, const DBMeta &dbMeta) { - if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { - return nullptr; - } - HapTokenInfo hapInfo; - if (AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != RET_SUCCESS) { - return nullptr; - } auto data = ExtensionUtil::Convert(dbMeta); if (data.first == nullptr) { return nullptr; } - OhCloudExtCloudAssetLoader *loader = OhCloudExtCloudAssetLoaderNew(hapInfo.userID, - reinterpret_cast(hapInfo.bundleName.c_str()), - hapInfo.bundleName.size(), data.first); + OhCloudExtCloudAssetLoader *loader = OhCloudExtCloudAssetLoaderNew( + user, reinterpret_cast(bundleName.c_str()), bundleName.size(), data.first); return loader != nullptr ? std::make_shared(loader) : nullptr; } -std::shared_ptr CloudServerImpl::ConnectCloudDB(uint32_t tokenId, const DBMeta &dbMeta) +std::shared_ptr CloudServerImpl::ConnectCloudDB( + const std::string &bundleName, int user, const DBMeta &dbMeta) { - if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { - return nullptr; - } - HapTokenInfo hapInfo; - if (AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != RET_SUCCESS) { - return nullptr; - } auto data = ExtensionUtil::Convert(dbMeta); if (data.first == nullptr) { return nullptr; } - OhCloudExtCloudDatabase *cloudDb = OhCloudExtCloudDbNew(hapInfo.userID, - reinterpret_cast(hapInfo.bundleName.c_str()), - hapInfo.bundleName.size(), data.first); + OhCloudExtCloudDatabase *cloudDb = OhCloudExtCloudDbNew( + user, reinterpret_cast(bundleName.c_str()), bundleName.size(), data.first); return cloudDb != nullptr ? std::make_shared(cloudDb) : nullptr; } } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/rust/extension/cloud_server_impl.h b/services/distributeddataservice/rust/extension/cloud_server_impl.h index 197bdd3cc..e52b446dd 100644 --- a/services/distributeddataservice/rust/extension/cloud_server_impl.h +++ b/services/distributeddataservice/rust/extension/cloud_server_impl.h @@ -40,8 +40,9 @@ public: DBSchemaMeta GetAppSchema(int32_t userId, const std::string &bundleName) override; int32_t Subscribe(int32_t userId, const std::map> &dbs) override; int32_t Unsubscribe(int32_t userId, const std::map> &dbs) override; - std::shared_ptr ConnectAssetLoader(uint32_t tokenId, const DBMeta &dbMeta) override; - std::shared_ptr ConnectCloudDB(uint32_t tokenId, const DBMeta &dbMeta) override; + std::shared_ptr ConnectAssetLoader( + const std::string &bundleName, int user, const DBMeta &dbMeta) override; + std::shared_ptr ConnectCloudDB(const std::string &bundleName, int user, const DBMeta &dbMeta) override; private: static constexpr uint64_t INTERVAL = 6 * 24; diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index e1dfbe003..609eb88d8 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -37,7 +37,6 @@ using DBField = DistributedDB::Field; using DBTable = DistributedDB::TableSchema; using DBSchema = DistributedDB::DataBaseSchema; using ClearMode = DistributedDB::ClearMode; -using DBStatus = DistributedDB::DBStatus; KVDBGeneralStore::DBPassword KVDBGeneralStore::GetDBPassword(const StoreMetaData &data) { @@ -112,6 +111,7 @@ KVDBGeneralStore::KVDBGeneralStore(const StoreMetaData &meta) : manager_(meta.ap return; } delegate_->RegisterObserver({}, DistributedDB::OBSERVER_CHANGES_FOREIGN, &observer_); + delegate_->RegisterObserver({}, DistributedDB::OBSERVER_CHANGES_CLOUD, &observer_); if (meta.isAutoSync) { auto code = DeviceMatrix::GetInstance().GetCode(meta); delegate_->SetRemotePushFinishedNotify([code](const DistributedDB::RemotePushNotifyInfo &info) { @@ -126,7 +126,7 @@ KVDBGeneralStore::KVDBGeneralStore(const StoreMetaData &meta) : manager_(meta.ap storeInfo_.storeName = meta.storeId; storeInfo_.instanceId = meta.instanceId; storeInfo_.user = std::stoi(meta.user); - storeInfo_.isPublic = meta.isPublic; + isPublic_ = meta.isPublic; } KVDBGeneralStore::~KVDBGeneralStore() @@ -282,7 +282,7 @@ KVDBGeneralStore::DBSyncCallback KVDBGeneralStore::GetDBSyncCompleteCB(DetailAsy return [](auto &) {}; } return [async = std::move(async)](const std::map &status) { - DistributedData::GenDetails details; + GenDetails details; for (auto &[key, dbStatus] : status) { auto &value = details[key]; value.progress = FINISHED; @@ -330,14 +330,7 @@ int32_t KVDBGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &q syncOption.devices = devices; syncOption.mode = dbMode; syncOption.waitTime = wait; - if (storeInfo_.isPublic) { - std::set activeUsers = UserDelegate::GetInstance().GetLocalUsers(); - for (auto &activeUser : activeUsers) { - syncOption.users.push_back(activeUser); - } - } else { - syncOption.users.push_back(std::to_string(storeInfo_.user)); - } + syncOption.users.push_back(std::to_string(storeInfo_.user)); dbStatus = delegate_->Sync(syncOption, nullptr); } else { dbStatus = DistributedDB::INVALID_ARGS; @@ -445,8 +438,7 @@ int32_t KVDBGeneralStore::SetTrackerTable( return GeneralError::E_OK; } -KVDBGeneralStore::GenErr KVDBGeneralStore::ConvertStatus( - DistributedDB::DBStatus status) // todo 没用到,具体转换结合接口错误码 +KVDBGeneralStore::GenErr KVDBGeneralStore::ConvertStatus(DistributedDB::DBStatus status) { switch (status) { case DBStatus::OK: @@ -481,34 +473,25 @@ int32_t KVDBGeneralStore::UnregisterDetailProgressObserver() return GenErr::E_OK; } -void KVDBGeneralStore::ObserverProxy::OnChange( - DistributedDB::Origin origin, const std::string &originalId, DistributedDB::ChangedData &&data) +void KVDBGeneralStore::ObserverProxy::OnChange(DBOrigin origin, const std::string &originalId, DBChangeData &&data) { if (!HasWatcher()) { return; } GenOrigin genOrigin; - genOrigin.origin = (origin == DBOrigin::ORIGIN_LOCAL) - ? GenOrigin::ORIGIN_LOCAL - : (origin == DBOrigin::ORIGIN_CLOUD) ? GenOrigin::ORIGIN_CLOUD : GenOrigin::ORIGIN_NEARBY; - genOrigin.dataType = data.type == DistributedDB::ASSET ? GenOrigin::ASSET_DATA : GenOrigin::BASIC_DATA; + genOrigin.origin = (origin == DBOrigin::ORIGIN_CLOUD) ? GenOrigin::ORIGIN_CLOUD : GenOrigin::ORIGIN_NEARBY; genOrigin.id.push_back(originalId); genOrigin.store = storeId_; - Watcher::PRIFields fields; Watcher::ChangeInfo changeInfo; for (uint32_t i = 0; i < DistributedDB::OP_BUTT; ++i) { auto &info = changeInfo[data.tableName][i]; - std::vector> a = data.primaryData[i]; for (auto &priData : data.primaryData[i]) { Watcher::PRIValue value; Convert(std::move(*(priData.begin())), value); info.push_back(std::move(value)); } } - if (!data.field.empty()) { - fields[std::move(data.tableName)] = std::move(*(data.field.begin())); - } - watcher_->OnChange(genOrigin, fields, std::move(changeInfo)); + watcher_->OnChange(genOrigin, {}, std::move(changeInfo)); } void KVDBGeneralStore::ObserverProxy::OnChange(const DistributedDB::KvStoreChangedData &data) @@ -530,8 +513,7 @@ void KVDBGeneralStore::ObserverProxy::OnChange(const DistributedDB::KvStoreChang watcher_->OnChange(genOrigin, {}, std::move(changeData)); } -void KVDBGeneralStore::ObserverProxy::ConvertChangeData( - const std::list &entries, std::vector &values) +void KVDBGeneralStore::ObserverProxy::ConvertChangeData(const std::list &entries, std::vector &values) { for (auto entry : entries) { auto value = std::vector{ entry.key, entry.value }; diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.h b/services/distributeddataservice/service/kvdb/kvdb_general_store.h index b23685b32..71c9fb74f 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.h +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.h @@ -32,28 +32,15 @@ #include "store_observer.h" namespace OHOS::DistributedKv { +using namespace DistributedData; class KVDBGeneralStore : public DistributedData::GeneralStore { public: - using Cursor = DistributedData::Cursor; - using GenQuery = DistributedData::GenQuery; - using VBucket = DistributedData::VBucket; - using VBuckets = DistributedData::VBuckets; using Value = DistributedData::Value; - using Values = DistributedData::Values; - using StoreMetaData = DistributedData::StoreMetaData; - using Database = DistributedData::Database; using GenErr = DistributedData::GeneralError; - using Reference = DistributedData::Reference; - using SyncCallback = KvStoreSyncCallback; - using SyncEnd = KvStoreSyncManager::SyncEnd; - using DBStore = DistributedDB::KvStoreNbDelegate; - using Store = std::shared_ptr; using DBStatus = DistributedDB::DBStatus; using DBOption = DistributedDB::KvStoreNbDelegate::Option; using DBSecurity = DistributedDB::SecurityOption; using DBPassword = DistributedDB::CipherPassword; - using BindAssets = DistributedData::BindAssets; - using DBMode = DistributedDB::SyncMode; explicit KVDBGeneralStore(const StoreMetaData &meta); ~KVDBGeneralStore(); @@ -82,8 +69,7 @@ public: int32_t Close() override; int32_t AddRef() override; int32_t Release() override; - int32_t BindSnapshots( - std::shared_ptr>> bindAssets) override; + int32_t BindSnapshots(std::shared_ptr>> bindAssets) override; int32_t MergeMigratedData(const std::string &tableName, VBuckets &&values) override; static DBPassword GetDBPassword(const StoreMetaData &data); @@ -98,17 +84,18 @@ private: using SyncProcess = DistributedDB::SyncProcess; using DBSyncCallback = std::function &status)>; using DBProcessCB = std::function &processes)>; - static GenErr ConvertStatus(DistributedDB::DBStatus status); + static GenErr ConvertStatus(DBStatus status); DBSyncCallback GetDBSyncCompleteCB(DetailAsync async); class ObserverProxy : public DistributedDB::KvStoreObserver { public: using DBOrigin = DistributedDB::Origin; + using DBChangeData = DistributedDB::ChangedData; + using DBEntry = DistributedDB::Entry; using GenOrigin = Watcher::Origin; ~ObserverProxy() = default; - void OnChange( - DistributedDB::Origin origin, const std::string &originalId, DistributedDB::ChangedData &&data) override; + void OnChange(DBOrigin origin, const std::string &originalId, DBChangeData &&data) override; void OnChange(const DistributedDB::KvStoreChangedData &data) override; - void ConvertChangeData(const std::list &entries, std::vector &values); + void ConvertChangeData(const std::list &entries, std::vector &values); bool HasWatcher() const { return watcher_ != nullptr; @@ -129,7 +116,8 @@ private: std::mutex mutex_; int32_t ref_ = 1; mutable std::shared_mutex rwMutex_; - DistributedData::StoreInfo storeInfo_; + StoreInfo storeInfo_; + bool isPublic_ = false; }; } // namespace OHOS::DistributedKv #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_GENERAL_STORE_H diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index f778a5e33..de16626e9 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -39,7 +39,6 @@ #include "metadata/meta_data_manager.h" #include "permit_delegate.h" #include "query_helper.h" -#include "store/general_store.h" #include "store/store_info.h" #include "upgrade.h" #include "utils/anonymous.h" @@ -53,7 +52,6 @@ using namespace OHOS::Security::AccessToken; using system_clock = std::chrono::system_clock; using DMAdapter = DistributedData::DeviceManagerAdapter; using DumpManager = OHOS::DistributedData::DumpManager; -using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; __attribute__((used)) KVDBServiceImpl::Factory KVDBServiceImpl::factory_; KVDBServiceImpl::Factory::Factory() @@ -123,6 +121,16 @@ KVDBServiceImpl::KVDBServiceImpl() std::bind(&KVDBServiceImpl::DoComplete, this, data, syncInfo, refCount, std::placeholders::_1)); } }); + Init(); +} + +KVDBServiceImpl::~KVDBServiceImpl() +{ + DumpManager::GetInstance().RemoveHandler("FEATURE_INFO", uintptr_t(this)); +} + +void KVDBServiceImpl::Init() +{ auto process = [this](const Event &event) { auto &evt = static_cast(event); auto &storeInfo = evt.GetStoreInfo(); @@ -131,7 +139,13 @@ KVDBServiceImpl::KVDBServiceImpl() meta.bundleName = storeInfo.bundleName; meta.user = std::to_string(storeInfo.user); meta.instanceId = storeInfo.instanceId; - meta.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + meta.deviceId = DMAdapter::GetInstance().GetLocalDevice().uuid; + if (meta.storeType < StoreMetaData::StoreType::STORE_KV_BEGIN || + meta.storeType > StoreMetaData::StoreType::STORE_KV_END) { + ZLOGE("StoreType not match, bundleName:%{public}s, storeId:%{public}s, storeType: %{public}d", + meta.bundleName.c_str(), meta.GetStoreAlias().c_str(), meta.storeType); + return; + } if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta, true)) { ZLOGE("meta empty, bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), meta.GetStoreAlias().c_str()); @@ -148,11 +162,6 @@ KVDBServiceImpl::KVDBServiceImpl() EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_SYNC, process); } -KVDBServiceImpl::~KVDBServiceImpl() -{ - DumpManager::GetInstance().RemoveHandler("FEATURE_INFO", uintptr_t(this)); -} - void KVDBServiceImpl::RegisterKvServiceInfo() { OHOS::DistributedData::DumpManager::Config serviceInfoConfig; @@ -492,7 +501,7 @@ Status KVDBServiceImpl::BeforeCreate(const AppId &appId, const StoreId &storeId, storeInfo.storeName = storeId; storeInfo.instanceId = GetInstIndex(storeInfo.tokenId, appId); storeInfo.user = std::stoi(meta.user); - storeInfo.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + storeInfo.deviceId = DMAdapter::GetInstance().GetLocalDevice().uuid; executors_->Execute([storeInfo]() { auto event = std::make_unique(CloudEvent::GET_SCHEMA, storeInfo); EventCenter::GetInstance().PostEvent(move(event)); diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h index 4c32f6a7c..12f99e3b4 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h @@ -103,6 +103,7 @@ private: std::shared_ptr product_; }; + void Init(); void AddOptions(const Options &options, StoreMetaData &metaData); StoreMetaData GetStoreMetaData(const AppId &appId, const StoreId &storeId); StrategyMeta GetStrategyMeta(const AppId &appId, const StoreId &storeId); diff --git a/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp b/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp index 73e4d8dc7..6821eed91 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp @@ -20,6 +20,7 @@ #include "error/general_error.h" #include "ikvstore_observer.h" #include "log_print.h" +#include "types.h" #include "utils/anonymous.h" namespace OHOS::DistributedKv { @@ -29,6 +30,24 @@ KVDBWatcher::KVDBWatcher() {} int32_t KVDBWatcher::OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) { + auto store = origin.store; + auto changeData = values.find(store); + if (changeData != values.end()) { + auto observers = GetObservers(); + if (observers.empty()) { + return E_NOT_INIT; + } + std::vector keys[OP_BUTT]{}; + keys[OP_INSERT] = ConvertToKeys(changeData->second[OP_INSERT]); + keys[OP_UPDATE] = ConvertToKeys(changeData->second[OP_UPDATE]); + keys[OP_DELETE] = ConvertToKeys(changeData->second[OP_DELETE]); + DataOrigin dataOrigin; + dataOrigin.id = origin.id; + dataOrigin.store = origin.store; + for(auto &observer : observers) { + observer->OnChange(dataOrigin, std::move(keys)); + } + } return E_OK; } @@ -84,4 +103,17 @@ std::vector KVDBWatcher::ConvertToEntries(const std::vector &valu } return changeData; } + +std::vector KVDBWatcher::ConvertToKeys(const std::vector &values) +{ + std::vector keys{}; + for (auto &info : values) { + auto key = std::get_if(&info); + if (key == nullptr) { + continue; + } + keys.push_back(*key); + } + return keys; +} } // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/service/kvdb/kvdb_watcher.h b/services/distributeddataservice/service/kvdb/kvdb_watcher.h index abfff5407..92a50b596 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_watcher.h +++ b/services/distributeddataservice/service/kvdb/kvdb_watcher.h @@ -26,7 +26,6 @@ namespace OHOS::DistributedKv { class KVDBWatcher : public DistributedData::GeneralWatcher { public: - using DBEntry = DistributedDB::Entry; KVDBWatcher(); int32_t OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) override; int32_t OnChange(const Origin &origin, const GeneralWatcher::Fields &fields, ChangeData &&datas) override; @@ -36,6 +35,7 @@ public: private: std::vector ConvertToEntries(const std::vector &values); + std::vector ConvertToKeys(const std::vector &values); mutable std::shared_mutex mutex_; std::set> observers_; }; -- Gitee From 631b0a7a1ee2e99a6736b1e12d891345dba3ba3f Mon Sep 17 00:00:00 2001 From: Hollokin Date: Tue, 16 Apr 2024 09:49:44 +0800 Subject: [PATCH 07/15] =?UTF-8?q?=E5=85=AC=E5=85=B1=E5=BA=93=E7=AB=AF?= =?UTF-8?q?=E4=BA=91=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../framework/include/store/store_info.h | 1 + .../service/kvdb/kvdb_general_store.cpp | 11 +------- .../service/kvdb/kvdb_general_store.h | 1 - .../service/kvdb/kvdb_service_impl.cpp | 27 ++++++++++++------- .../service/kvdb/kvdb_service_impl.h | 2 +- .../service/kvdb/kvdb_service_stub.cpp | 8 +----- 6 files changed, 21 insertions(+), 29 deletions(-) diff --git a/services/distributeddataservice/framework/include/store/store_info.h b/services/distributeddataservice/framework/include/store/store_info.h index 8138955aa..4eddd5aa7 100644 --- a/services/distributeddataservice/framework/include/store/store_info.h +++ b/services/distributeddataservice/framework/include/store/store_info.h @@ -26,6 +26,7 @@ struct StoreInfo { int32_t instanceId = 0; int32_t user = 0; std::string deviceId; + bool isPublic = false; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_INFO_H diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index 609eb88d8..83eafdff4 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -126,7 +126,7 @@ KVDBGeneralStore::KVDBGeneralStore(const StoreMetaData &meta) : manager_(meta.ap storeInfo_.storeName = meta.storeId; storeInfo_.instanceId = meta.instanceId; storeInfo_.user = std::stoi(meta.user); - isPublic_ = meta.isPublic; + storeInfo_.isPublic = meta.isPublic; } KVDBGeneralStore::~KVDBGeneralStore() @@ -169,15 +169,6 @@ int32_t KVDBGeneralStore::Bind(const std::map(BindEvent::BIND_SNAPSHOT, std::move(eventInfo)); - EventCenter::GetInstance().PostEvent(std::move(evt)); dbClouds_.insert({ userId, std::make_shared(bindInfo.db_, nullptr) }); bindInfos_.insert(std::move(bindInfo)); diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.h b/services/distributeddataservice/service/kvdb/kvdb_general_store.h index 71c9fb74f..57a83e014 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.h +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.h @@ -117,7 +117,6 @@ private: int32_t ref_ = 1; mutable std::shared_mutex rwMutex_; StoreInfo storeInfo_; - bool isPublic_ = false; }; } // namespace OHOS::DistributedKv #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_GENERAL_STORE_H diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index de16626e9..478c56ba6 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -137,26 +137,32 @@ void KVDBServiceImpl::Init() StoreMetaData meta; meta.storeId = storeInfo.storeName; meta.bundleName = storeInfo.bundleName; - meta.user = std::to_string(storeInfo.user); - meta.instanceId = storeInfo.instanceId; + meta.user = storeInfo.isPublic ? "0" : std::to_string(storeInfo.user); meta.deviceId = DMAdapter::GetInstance().GetLocalDevice().uuid; - if (meta.storeType < StoreMetaData::StoreType::STORE_KV_BEGIN || - meta.storeType > StoreMetaData::StoreType::STORE_KV_END) { - ZLOGE("StoreType not match, bundleName:%{public}s, storeId:%{public}s, storeType: %{public}d", - meta.bundleName.c_str(), meta.GetStoreAlias().c_str(), meta.storeType); - return; - } if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta, true)) { ZLOGE("meta empty, bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), meta.GetStoreAlias().c_str()); return; } + if (meta.storeType < StoreMetaData::StoreType::STORE_KV_BEGIN || + meta.storeType > StoreMetaData::StoreType::STORE_KV_END) { + ZLOGE("StoreType not match, bundleName:%{public}s, storeId:%{public}s, storeType: %{public}d", + meta.bundleName.c_str(), meta.GetStoreAlias().c_str(), meta.storeType); + 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.GetStoreAlias().c_str()); return; } + if (meta.isPublic) { + auto mixMode = static_cast(GeneralStore::MixMode(GeneralStore::CLOUD_TIME_FIRST, + meta.isAutoSync ? GeneralStore::AUTO_SYNC_MODE : GeneralStore::MANUAL_SYNC_MODE)); + auto info = ChangeEvent::EventInfo(mixMode, 0, meta.isAutoSync, nullptr, nullptr); + auto evt = std::make_unique(std::move(storeInfo), std::move(info)); + EventCenter::GetInstance().PostEvent(std::move(evt)); + } store->RegisterDetailProgressObserver(nullptr); }; EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_SYNC, process); @@ -234,7 +240,7 @@ Status KVDBServiceImpl::Delete(const AppId &appId, const StoreId &storeId) return SUCCESS; } -Status KVDBServiceImpl::CloudSync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) +Status KVDBServiceImpl::CloudSync(const AppId &appId, const StoreId &storeId) { StoreMetaData metaData = GetStoreMetaData(appId, storeId); MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData); @@ -243,12 +249,13 @@ Status KVDBServiceImpl::CloudSync(const AppId &appId, const StoreId &storeId, co storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); storeInfo.user = AccountDelegate::GetInstance()->GetUserByToken(IPCSkeleton::GetCallingTokenID()); storeInfo.storeName = storeId; + storeInfo.isPublic = metaData.isPublic; auto mixMode = static_cast(GeneralStore::MixMode(GeneralStore::CLOUD_TIME_FIRST, metaData.isAutoSync ? GeneralStore::AUTO_SYNC_MODE : GeneralStore::MANUAL_SYNC_MODE)); auto info = ChangeEvent::EventInfo(mixMode, 0, metaData.isAutoSync, nullptr, nullptr); auto evt = std::make_unique(std::move(storeInfo), std::move(info)); EventCenter::GetInstance().PostEvent(std::move(evt)); - return SUCCESS; // todo 异步接口,只会返回成功。 + return SUCCESS; } Status KVDBServiceImpl::Sync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h index 12f99e3b4..4e604ad9e 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h @@ -44,7 +44,7 @@ public: Status AfterCreate(const AppId &appId, const StoreId &storeId, const Options &options, const std::vector &password) override; Status Delete(const AppId &appId, const StoreId &storeId) override; - Status CloudSync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; + Status CloudSync(const AppId &appId, const StoreId &storeId) override; Status Sync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; Status SyncExt(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; Status RegisterSyncCallback(const AppId &appId, sptr callback) override; diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp index 4f73a7efa..0a60988b5 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp @@ -173,13 +173,7 @@ int32_t KVDBServiceStub::OnSync(const AppId &appId, const StoreId &storeId, Mess int32_t KVDBServiceStub::OnCloudSync( const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply) { - SyncInfo syncInfo; - if (!ITypesUtil::Unmarshal(data, syncInfo.seqId, syncInfo.mode, syncInfo.devices, syncInfo.delay, syncInfo.query)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), - Anonymous::Change(storeId.storeId).c_str()); - return IPC_STUB_INVALID_DATA_ERR; - } - int32_t status = CloudSync(appId, storeId, syncInfo); + int32_t status = CloudSync(appId, storeId); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x", status); return IPC_STUB_WRITE_PARCEL_ERR; -- Gitee From acd1f50dfbcb87352aec4d3ef8a37a24b098e296 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Tue, 16 Apr 2024 20:02:36 +0800 Subject: [PATCH 08/15] =?UTF-8?q?=E8=A1=A5=E5=85=85TDD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../framework/test/general_store_test.cpp | 10 + .../service/kvdb/kvdb_general_store.cpp | 10 +- .../service/kvdb/kvdb_general_store.h | 2 - .../service/kvdb/kvdb_service_impl.cpp | 5 - .../service/test/BUILD.gn | 29 +++ .../fuzztest/kvdbservicestub_fuzzer/BUILD.gn | 1 + .../service/test/kvdb_general_store_test.cpp | 237 ++++++++++++++++++ 7 files changed, 283 insertions(+), 11 deletions(-) create mode 100644 services/distributeddataservice/service/test/kvdb_general_store_test.cpp diff --git a/services/distributeddataservice/framework/test/general_store_test.cpp b/services/distributeddataservice/framework/test/general_store_test.cpp index 401602a4f..35cc0a958 100644 --- a/services/distributeddataservice/framework/test/general_store_test.cpp +++ b/services/distributeddataservice/framework/test/general_store_test.cpp @@ -47,6 +47,16 @@ HWTEST_F(GeneralStoreTest, GetSyncMode, TestSize.Level1) auto mixMode = GeneralStore::MixMode(syncMode, highMode); auto ret = GeneralStore::GetSyncMode(mixMode); ASSERT_EQ(ret, syncMode); + + syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; + mixMode = GeneralStore::MixMode(syncMode, highMode); + ret = GeneralStore::GetSyncMode(mixMode); + ASSERT_EQ(ret, syncMode); + + syncMode = GeneralStore::SyncMode::NEARBY_UNSUBSCRIBE_REMOTE; + mixMode = GeneralStore::MixMode(syncMode, highMode); + ret = GeneralStore::GetSyncMode(mixMode); + ASSERT_EQ(ret, syncMode); } /** diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index 83eafdff4..1c8bb6b26 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -303,6 +303,10 @@ int32_t KVDBGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &q } else { return GeneralError::E_INVALID_ARGS; } + if (devices.empty()) { + ZLOGE("Devices is empty! mode:%{public}d", mode); + return GeneralError::E_INVALID_ARGS; + } auto dbStatus = DistributedDB::OK; if (syncMode == NEARBY_SUBSCRIBE_REMOTE) { dbStatus = delegate_->SubscribeRemoteQuery(devices, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); @@ -343,10 +347,8 @@ int32_t KVDBGeneralStore::Clean(const std::vector &devices, int32_t DBStatus status = OK; std::shared_lock lock(rwMutex_); if (delegate_ == nullptr) { - ZLOGE("store already closed! devices count:%{public}zu, the 1st:%{public}s, mode:%{public}d, " - "tableName:%{public}s", - devices.size(), devices.empty() ? "null" : Anonymous::Change(*devices.begin()).c_str(), mode, - Anonymous::Change(tableName).c_str()); + ZLOGE("store already closed! devices count:%{public}zu, the 1st:%{public}s, mode:%{public}d", devices.size(), + devices.empty() ? "null" : Anonymous::Change(*devices.begin()).c_str(), mode); return GeneralError::E_ALREADY_CLOSED; } switch (mode) { diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.h b/services/distributeddataservice/service/kvdb/kvdb_general_store.h index 57a83e014..49b4491e1 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.h +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.h @@ -77,8 +77,6 @@ public: static DBSecurity GetDBSecurity(int32_t secLevel); private: - KVDBGeneralStore(const KVDBGeneralStore &store); - KVDBGeneralStore &operator=(const KVDBGeneralStore &store); using KvDelegate = DistributedDB::KvStoreNbDelegate; using KvManager = DistributedDB::KvStoreDelegateManager; using SyncProcess = DistributedDB::SyncProcess; diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 478c56ba6..a8b6fff1b 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -246,10 +246,8 @@ Status KVDBServiceImpl::CloudSync(const AppId &appId, const StoreId &storeId) MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData); DistributedData::StoreInfo storeInfo; storeInfo.bundleName = appId.appId; - storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); storeInfo.user = AccountDelegate::GetInstance()->GetUserByToken(IPCSkeleton::GetCallingTokenID()); storeInfo.storeName = storeId; - storeInfo.isPublic = metaData.isPublic; auto mixMode = static_cast(GeneralStore::MixMode(GeneralStore::CLOUD_TIME_FIRST, metaData.isAutoSync ? GeneralStore::AUTO_SYNC_MODE : GeneralStore::MANUAL_SYNC_MODE)); auto info = ChangeEvent::EventInfo(mixMode, 0, metaData.isAutoSync, nullptr, nullptr); @@ -503,12 +501,9 @@ Status KVDBServiceImpl::BeforeCreate(const AppId &appId, const StoreId &storeId, } if (executors_ != nullptr) { DistributedData::StoreInfo storeInfo; - storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); storeInfo.bundleName = appId.appId; - storeInfo.storeName = storeId; storeInfo.instanceId = GetInstIndex(storeInfo.tokenId, appId); storeInfo.user = std::stoi(meta.user); - storeInfo.deviceId = DMAdapter::GetInstance().GetLocalDevice().uuid; executors_->Execute([storeInfo]() { auto event = std::make_unique(CloudEvent::GET_SCHEMA, storeInfo); EventCenter::GetInstance().PostEvent(move(event)); diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 4fc4238fb..6c5ec43f1 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -233,6 +233,34 @@ ohos_unittest("DeviceMatrixTest") { ] } +ohos_unittest("KVDBGeneralStoreTest") { + module_out_path = module_output_path + sources = [ + "kvdb_general_store_test.cpp" + ] + + configs = [ ":module_private_config" ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] + + deps = [ + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:distributeddatasvc", + "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb:distributeddb", + "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata:distributeddata_inner", + "//third_party/googletest:gtest_main", + ] +} + ohos_unittest("RdbResultSetImplTest") { module_out_path = module_output_path sources = [ @@ -436,6 +464,7 @@ group("unittest") { ":CryptoManagerTest", ":DeviceMatrixTest", ":DirectoryManagerTest", + ":KVDBGeneralStoreTest", ":MetaDataTest", ":ObjectAssetMachineTest", ":RdbResultSetImplTest", diff --git a/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn index 3c4bb4379..8e2591d32 100644 --- a/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn @@ -32,6 +32,7 @@ ohos_fuzztest("KvdbServiceStubFuzzTest") { "${data_service_path}/service/kvdb", "${data_service_path}/service/matrix/include", "${data_service_path}/service/permission/include", + "${data_service_path}/service/rdb", "${kv_store_common_path}", "${kv_store_path}/frameworks/innerkitsimpl/distributeddatafwk/include", "${kv_store_path}/frameworks/innerkitsimpl/distributeddatasvc/include", diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp new file mode 100644 index 000000000..9bfdf4974 --- /dev/null +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -0,0 +1,237 @@ +/* +* Copyright (c) 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 +* +* 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 "KVDBGeneralStoreTest" + +#include "kvdb_general_store.h" + +#include + +#include "account/account_delegate.h" +#include "communicator/device_manager_adapter.h" +#include "device_matrix.h" +#include "distributed_kv_data_manager.h" +#include "eventcenter/event_center.h" +#include "feature/feature_system.h" +#include "ipc_skeleton.h" +#include "log_print.h" +#include "metadata/meta_data_manager.h" +#include "metadata/store_meta_data.h" +#include "metadata/store_meta_data_local.h" +#include "mock/db_store_mock.h" +#include "rdb_types.h" + +using namespace testing::ext; +using namespace OHOS::DistributedData; +using namespace OHOS::DistributedKv; +using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; + +namespace OHOS::Test { +namespace DistributedDataTest { +class KVDBGeneralStoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + +protected: + static constexpr const char *TEST_DISTRIBUTEDDATA_BUNDLE = "test_distributeddata"; + static constexpr const char *TEST_DISTRIBUTEDDATA_STORE = "test_service_meta"; + + void InitMetaData(); + static std::shared_ptr dbStoreMock_; + StoreMetaData metaData_; +}; + +SchemaMeta CloudDataTest::schemaMeta_; + +void KVDBGeneralStoreTest::InitMetaData() +{ + metaData_.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + metaData_.appId = TEST_DISTRIBUTEDDATA_BUNDLE; + metaData_.bundleName = TEST_DISTRIBUTEDDATA_BUNDLE; + metaData_.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + metaData_.user = std::to_string(DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(metaData_.tokenId)); + metaData_.area = OHOS::DistributedKv::EL1; + metaData_.instanceId = 0; + metaData_.isAutoSync = true; + metaData_.storeType = 1; + metaData_.storeId = TEST_DISTRIBUTEDDATA_STORE; + PolicyValue value; + value.type = OHOS::DistributedKv::PolicyType::IMMEDIATE_SYNC_ON_ONLINE; +} + +void KVDBGeneralStoreTest::SetUpTestCase(void) +{ +} + +void KVDBGeneralStoreTest::TearDownTestCase() {} + +void KVDBGeneralStoreTest::SetUp() +{ +} + +void KVDBGeneralStoreTest::TearDown() {} + +/** +* @tc.name: GetDBPasswordTest +* @tc.desc: GetDBPassword from meta. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest, TestSize.Level0) +{ + ZLOGI("GetDBPasswordTest start"); + InitMetaData(); + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_TRUE(dbPassword.GetSize() == 0); + metaData_.isEncrypt = true; + dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + ASSERT_TRUE(dbPassword.GetSize() != 0); +} + +/** +* @tc.name: GetDBSecurityTest +* @tc.desc: GetDBSecurity +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) +{ + ZLOGI("GetDBSecurityTest start"); + auto dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::INVALID_LABEL); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::NO_LABEL); + EXPECT_EQ(dbSecurity.securityLabel, SecurityLevel::NO_LABEL); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S0); + EXPECT_EQ(dbSecurity.securityLabel, SecurityLevel::S0); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S1); + EXPECT_EQ(dbSecurity.securityLabel, SecurityLevel::S1); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S2); + EXPECT_EQ(dbSecurity.securityLabel, SecurityLevel::S2); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S3); + EXPECT_EQ(dbSecurity.securityLabel, SecurityLevel::S3); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::SECE); + + dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S4); + EXPECT_EQ(dbSecurity.securityLabel, SecurityLevel::S4); + EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); +} + +/** +* @tc.name: GetDBOptionTest +* @tc.desc: GetDBOption from meta and dbPassword +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) +{ + ZLOGI("GetDBOptionTest start"); + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); + EXPECT_EQ(dbOption.syncDualTupleMode, true); + EXPECT_EQ(dbOption.createIfNecessary, false); + EXPECT_EQ(dbOption.isMemoryDb, false); + EXPECT_EQ(dbOption.isEncryptedDb, metaData_.isEncrypt); + EXPECT_EQ(dbOption.isNeedCompressOnSync, metaData_.isNeedCompress); + EXPECT_EQ(dbOption.schema, metaData_.schema); + EXPECT_EQ(dbOption.passwd, dbPassword); + EXPECT_EQ(dbOption.cipher, DistributedDB::CipherType::AES_256_GCM); + EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); + EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); + EXPECT_EQ(dbOption.secOption, GetDBSecurity(metaData_.securityLevel)); +} + +/** +* @tc.name: CloseTest +* @tc.desc: Close kvdb general store. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) +{ + ZLOGI("CloseTest start"); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + auto ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: SyncTest +* @tc.desc: Sync. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) +{ + ZLOGI("SyncTest start"); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = ""; + KVDBQuery query(kvQuery); + auto ret = store->Sync( + {}, mixMode, query, [](const GenDetails &result) {}, 0); + EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); + auto ret = store->Sync( + { "default_deviceId" }, mixMode, query, [](const GenDetails &result) {}, 0); + EXPECT_NE(ret, GeneralError::E_OK); + auto ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_OK); +} + +/** +* @tc.name: CleanTest +* @tc.desc: Clean. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, CleanTest, TestSize.Level0) +{ + ZLOGI("CleanTest start"); + auto store = new (std::nothrow) KVDBGeneralStore(metaData_); + ASSERT_NE(store, nullptr); + EXPECT_EQ(store->IsValid(), true); + uint32_t syncMode = GeneralStore::SyncMode::NEARBY_PUSH; + uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; + auto mixMode = GeneralStore::MixMode(syncMode, highMode); + std::string kvQuery = ""; + KVDBQuery query(kvQuery); + auto ret = store->Clean({}, GeneralStore::CleanMode::NEARBY_DATA, ""); + EXPECT_EQ(ret, GeneralError::E_OK); + auto ret = store->Close(); + EXPECT_EQ(ret, GeneralError::E_OK); +} +} // namespace DistributedDataTest +} // namespace OHOS::Test -- Gitee From eec28354224fb08e3825d004c4de999ac82fb0b3 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Thu, 18 Apr 2024 20:35:13 +0800 Subject: [PATCH 09/15] public kv store cloud sync Signed-off-by: Hollokin --- .../service/cloud/cloud_service_impl.cpp | 8 ++- .../service/cloud/sync_manager.cpp | 14 ++++- .../service/kvdb/kvdb_general_store.cpp | 56 ++++++++++--------- 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 1f5f16eee..eeabc3b0a 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -732,7 +732,13 @@ void CloudServiceImpl::GetSchema(const Event &event) auto &storeInfo = rdbEvent.GetStoreInfo(); ZLOGD("Start GetSchema, bundleName:%{public}s, storeName:%{public}s, instanceId:%{public}d", storeInfo.bundleName.c_str(), Anonymous::Change(storeInfo.storeName).c_str(), storeInfo.instanceId); - GetSchemaMeta(storeInfo.user, storeInfo.bundleName, storeInfo.instanceId); + if (storeInfo.user == 0) { + std::vector users; + AccountDelegate::GetInstance()->QueryUsers(users); + GetSchemaMeta(users[0], storeInfo.bundleName, storeInfo.instanceId); + } else { + GetSchemaMeta(storeInfo.user, storeInfo.bundleName, storeInfo.instanceId); + } } void CloudServiceImpl::CloudShare(const Event &event) diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 5d3367c9f..a2ba6d517 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -204,6 +204,13 @@ ExecutorPool::Task SyncManager::GetSyncTask(int32_t times, bool retry, RefCount times++; return [this, times, retry, keep = std::move(ref), info = std::move(syncInfo)]() mutable { activeInfos_.Erase(info.syncId_); + bool createdByDefaultUser = false; + if (info.user_ == 0) { + std::vector users; + AccountDelegate::GetInstance()->QueryUsers(users); + info.user_ = users[0]; + createdByDefaultUser = true; + } CloudInfo cloud; cloud.user = info.user_; if (!IsValid(info, cloud)) { @@ -228,6 +235,9 @@ ExecutorPool::Task SyncManager::GetSyncTask(int32_t times, bool retry, RefCount } StoreInfo storeInfo = { 0, schema.bundleName, database.name, cloud.apps[schema.bundleName].instanceId, cloud.user }; + if (createdByDefaultUser) { + storeInfo.user = 0; + } auto status = syncStrategy_->CheckSyncAction(storeInfo); if (status != SUCCESS) { ZLOGW("Verification strategy failed, status:%{public}d. %{public}d:%{public}s:%{public}s", status, @@ -237,7 +247,7 @@ ExecutorPool::Task SyncManager::GetSyncTask(int32_t times, bool retry, RefCount } auto query = info.GenerateQuery(database.name, database.GetTableNames()); auto evt = std::make_unique(std::move(storeInfo), - SyncEvent::EventInfo { info.mode_, info.wait_, retry, std::move(query), info.async_ }); + SyncEvent::EventInfo{ info.mode_, info.wait_, retry, std::move(query), info.async_ }); EventCenter::GetInstance().PostEvent(std::move(evt)); } } @@ -363,7 +373,7 @@ void SyncManager::UpdateSchema(const SyncManager::SyncInfo &syncInfo) AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, bool mustBind) { - if (!Account::GetInstance()->IsVerified(user)) { + if (user != 0 && !Account::GetInstance()->IsVerified(user)) { ZLOGW("user:%{public}d is locked!", user); return nullptr; } diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index 1c8bb6b26..2e191f4c8 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -295,38 +295,44 @@ int32_t KVDBGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &q devices.empty() ? "null" : Anonymous::Change(*devices.begin()).c_str(), mode); return GeneralError::E_ALREADY_CLOSED; } - KVDBQuery *kvQuery = nullptr; - auto ret = query.QueryInterface(kvQuery); - DistributedDB::Query dbQuery; - if (ret == GeneralError::E_OK && kvQuery != nullptr && kvQuery->IsValidQuery()) { - dbQuery = kvQuery->GetDBQuery(); - } else { - return GeneralError::E_INVALID_ARGS; - } - if (devices.empty()) { - ZLOGE("Devices is empty! mode:%{public}d", mode); - return GeneralError::E_INVALID_ARGS; - } auto dbStatus = DistributedDB::OK; - if (syncMode == NEARBY_SUBSCRIBE_REMOTE) { - dbStatus = delegate_->SubscribeRemoteQuery(devices, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); - } else if (syncMode == NEARBY_UNSUBSCRIBE_REMOTE) { - dbStatus = delegate_->UnSubscribeRemoteQuery(devices, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); + auto dbMode = DistributedDB::SyncMode(syncMode); + if (syncMode > NEARBY_END && syncMode < CLOUD_END) { + DistributedDB::CloudSyncOption syncOption; + syncOption.devices = devices; + syncOption.mode = dbMode; + syncOption.waitTime = wait; + if (storeInfo_.user == 0) { + std::vector users; + AccountDelegate::GetInstance()->QueryUsers(users); + syncOption.users.push_back(std::to_string(users[0])); + } else { + syncOption.users.push_back(std::to_string(storeInfo_.user)); + } + dbStatus = delegate_->Sync(syncOption, nullptr); } else { - auto dbMode = DistributedDB::SyncMode(syncMode); - if (syncMode < NEARBY_END) { + if (devices.empty()) { + ZLOGE("Devices is empty! mode:%{public}d", mode); + return GeneralError::E_INVALID_ARGS; + } + KVDBQuery *kvQuery = nullptr; + auto ret = query.QueryInterface(kvQuery); + DistributedDB::Query dbQuery; + if (ret == GeneralError::E_OK && kvQuery != nullptr && kvQuery->IsValidQuery()) { + dbQuery = kvQuery->GetDBQuery(); + } else { + return GeneralError::E_INVALID_ARGS; + } + if (syncMode == NEARBY_SUBSCRIBE_REMOTE) { + dbStatus = delegate_->SubscribeRemoteQuery(devices, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); + } else if (syncMode == NEARBY_UNSUBSCRIBE_REMOTE) { + dbStatus = delegate_->UnSubscribeRemoteQuery(devices, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); + } else if (syncMode < NEARBY_END) { if (kvQuery->IsEmpty()) { dbStatus = delegate_->Sync(devices, dbMode, GetDBSyncCompleteCB(std::move(async)), false); } else { dbStatus = delegate_->Sync(devices, dbMode, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); } - } else if (syncMode > NEARBY_END && syncMode < CLOUD_END) { - DistributedDB::CloudSyncOption syncOption; - syncOption.devices = devices; - syncOption.mode = dbMode; - syncOption.waitTime = wait; - syncOption.users.push_back(std::to_string(storeInfo_.user)); - dbStatus = delegate_->Sync(syncOption, nullptr); } else { dbStatus = DistributedDB::INVALID_ARGS; } -- Gitee From d7f198ccb1e941ecbdf0dc9693bdf2e1b2cbcf8e Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 22 Apr 2024 14:28:40 +0800 Subject: [PATCH 10/15] =?UTF-8?q?=E8=A7=A3=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/kvdb/kvdb_general_store.cpp | 10 +++++----- .../service/kvdb/kvdb_general_store.h | 2 +- .../service/kvdb/kvdb_service_impl.cpp | 6 ++++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index 4f4bb8d7a..bc117bbeb 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -286,13 +286,13 @@ KVDBGeneralStore::DBSyncCallback KVDBGeneralStore::GetDBSyncCompleteCB(DetailAsy }; } -int32_t KVDBGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &query, DetailAsync async, int32_t wait) +int32_t KVDBGeneralStore::Sync(const Devices &devices, GenQuery &query, DetailAsync async, SyncParam &syncParm) { - auto syncMode = GeneralStore::GetSyncMode(mode); + auto syncMode = GeneralStore::GetSyncMode(syncParm.mode); std::shared_lock lock(rwMutex_); if (delegate_ == nullptr) { ZLOGE("store already closed! devices count:%{public}zu, the 1st:%{public}s, mode:%{public}d", devices.size(), - devices.empty() ? "null" : Anonymous::Change(*devices.begin()).c_str(), mode); + devices.empty() ? "null" : Anonymous::Change(*devices.begin()).c_str(), syncParm.mode); return GeneralError::E_ALREADY_CLOSED; } auto dbStatus = DistributedDB::OK; @@ -301,7 +301,7 @@ int32_t KVDBGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &q DistributedDB::CloudSyncOption syncOption; syncOption.devices = devices; syncOption.mode = dbMode; - syncOption.waitTime = wait; + syncOption.waitTime = syncParm.wait; if (storeInfo_.user == 0) { std::vector users; AccountDelegate::GetInstance()->QueryUsers(users); @@ -312,7 +312,7 @@ int32_t KVDBGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &q dbStatus = delegate_->Sync(syncOption, nullptr); } else { if (devices.empty()) { - ZLOGE("Devices is empty! mode:%{public}d", mode); + ZLOGE("Devices is empty! mode:%{public}d", syncParm.mode); return GeneralError::E_INVALID_ARGS; } KVDBQuery *kvQuery = nullptr; diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.h b/services/distributeddataservice/service/kvdb/kvdb_general_store.h index 49b4491e1..067b5bfcc 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.h +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.h @@ -59,7 +59,7 @@ public: int32_t Delete(const std::string &table, const std::string &sql, Values &&args) override; std::shared_ptr Query(const std::string &table, const std::string &sql, Values &&args) override; std::shared_ptr Query(const std::string &table, GenQuery &query) override; - int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, DetailAsync async, int32_t wait) override; + int32_t Sync(const Devices &devices, GenQuery &query, DetailAsync async, SyncParam &syncParm) override; std::shared_ptr PreSharing(GenQuery &query) override; int32_t Clean(const std::vector &devices, int32_t mode, const std::string &tableName) override; int32_t Watch(int32_t origin, Watcher &watcher) override; diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index a8b6fff1b..4e98f048a 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -848,13 +848,15 @@ Status KVDBServiceImpl::DoSyncBegin(const std::vector &devices, con return Status::INVALID_ARGUMENT; } auto mode = ConvertGeneralSyncMode(SyncMode(info.mode), SyncAction(type)); + SyncParam syncParam{}; + syncParam.mode = mode; auto ret = store->Sync( - devices, mode, query, + devices, query, [this, complete](const GenDetails &result) mutable { auto deviceStatus = HandleGenDetails(result); complete(deviceStatus); }, - 0); + syncParam); return Status(ret); } -- Gitee From 0ea248c71ef2bcd22c3cc010a572502dacbc6ad5 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 22 Apr 2024 15:42:41 +0800 Subject: [PATCH 11/15] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/cloud/sync_manager.cpp | 12 ++++---- .../service/kvdb/kvdb_general_store.cpp | 18 ++++++++---- .../service/kvdb/kvdb_general_store.h | 1 - .../service/kvdb/kvdb_query.h | 2 +- .../service/kvdb/kvdb_service_impl.cpp | 17 ++++------- .../service/kvdb/kvdb_watcher.cpp | 10 +++---- .../service/kvdb/kvdb_watcher.h | 2 +- .../service/test/BUILD.gn | 1 + .../service/test/kvdb_general_store_test.cpp | 29 ++++++------------- 9 files changed, 41 insertions(+), 51 deletions(-) diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 64163767c..09d36c7ad 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -407,14 +407,15 @@ AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, } if (!store->IsBound()) { - std::set activeUsers = UserDelegate::GetInstance().GetLocalUsers(); + std::vector activeUsers{}; + AccountDelegate::GetInstance()->QueryForegroundUsers(activeUsers); std::map> cloudDBs = {}; for (auto &activeUser : activeUsers) { - if (activeUser == "0") { + if (activeUser == 0) { continue; } CloudInfo info; - info.user = std::stoi(activeUser); + info.user = activeUser; SchemaMeta schemaMeta; std::string schemaKey = info.GetSchemaKey(meta.bundleName, meta.instanceId); if (!MetaDataManager::GetInstance().LoadMeta(std::move(schemaKey), schemaMeta, true)) { @@ -431,8 +432,9 @@ AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, return nullptr; } if (cloudDB != nullptr || assetLoader != nullptr) { - GeneralStore::BindInfo bindInfo(std::move(cloudDB), std::move(assetLoader)); - cloudDBs[activeUser] = std::make_pair(dbMeta, bindInfo); + GeneralStore::BindInfo bindInfo((cloudDB != nullptr) ? std::move(cloudDB) : cloudDB, + (assetLoader != nullptr) ? std::move(assetLoader) : assetLoader); + cloudDBs[std::to_string(activeUser)] = std::make_pair(dbMeta, bindInfo); } } store->Bind(cloudDBs); diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index bc117bbeb..9a225f001 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -25,6 +25,7 @@ #include "metadata/meta_data_manager.h" #include "metadata/secret_key_meta_data.h" #include "query_helper.h" +#include "rdb_cloud.h" #include "snapshot/bind_event.h" #include "types.h" #include "user_delegate.h" @@ -101,6 +102,7 @@ KVDBGeneralStore::KVDBGeneralStore(const StoreMetaData &meta) : manager_(meta.ap DBStatus status = DBStatus::NOT_FOUND; manager_.SetKvStoreConfig({ DirectoryManager::GetInstance().GetStorePath(meta) }); + std::unique_lock lock(rwMutex_); manager_.GetKvStore( meta.storeId, GetDBOption(meta, GetDBPassword(meta)), [&status, this](auto dbStatus, auto *tmpStore) { status = dbStatus; @@ -131,11 +133,14 @@ KVDBGeneralStore::KVDBGeneralStore(const StoreMetaData &meta) : manager_(meta.ap KVDBGeneralStore::~KVDBGeneralStore() { - if (delegate_ != nullptr) { - delegate_->UnRegisterObserver(&observer_); + { + std::unique_lock lock(rwMutex_); + if (delegate_ != nullptr) { + delegate_->UnRegisterObserver(&observer_); + } + manager_.CloseKvStore(delegate_); + delegate_ = nullptr; } - manager_.CloseKvStore(delegate_); - delegate_ = nullptr; for (auto &bindInfo_ : bindInfos_) { if (bindInfo_.db_ != nullptr) { bindInfo_.db_->Close(); @@ -208,7 +213,7 @@ int32_t KVDBGeneralStore::Close() { std::unique_lock lock(rwMutex_); if (delegate_ == nullptr) { - return 0; + return GeneralError::E_OK; } int32_t count = delegate_->GetTaskCount(); if (count > 0) { @@ -351,13 +356,13 @@ int32_t KVDBGeneralStore::Clean(const std::vector &devices, int32_t if (mode < 0 || mode > CLEAN_MODE_BUTT) { return GeneralError::E_INVALID_ARGS; } - DBStatus status = OK; std::shared_lock lock(rwMutex_); if (delegate_ == nullptr) { ZLOGE("store already closed! devices count:%{public}zu, the 1st:%{public}s, mode:%{public}d", devices.size(), devices.empty() ? "null" : Anonymous::Change(*devices.begin()).c_str(), mode); return GeneralError::E_ALREADY_CLOSED; } + DBStatus status = OK; switch (mode) { case CLOUD_INFO: status = delegate_->RemoveDeviceData("", static_cast(CLOUD_INFO)); @@ -460,6 +465,7 @@ KVDBGeneralStore::GenErr KVDBGeneralStore::ConvertStatus(DistributedDB::DBStatus bool KVDBGeneralStore::IsValid() { + std::unique_lock lock(rwMutex_); return delegate_ != nullptr; } diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.h b/services/distributeddataservice/service/kvdb/kvdb_general_store.h index 067b5bfcc..89eaea687 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.h +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.h @@ -26,7 +26,6 @@ #include "kvstore_sync_callback.h" #include "kvstore_sync_manager.h" #include "metadata/store_meta_data.h" -#include "rdb_cloud.h" #include "store/general_store.h" #include "store/general_value.h" #include "store_observer.h" diff --git a/services/distributeddataservice/service/kvdb/kvdb_query.h b/services/distributeddataservice/service/kvdb/kvdb_query.h index 83d913563..927a65391 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_query.h +++ b/services/distributeddataservice/service/kvdb/kvdb_query.h @@ -44,7 +44,7 @@ public: bool IsValidQuery() { - return !(!isSuccess_ && !query_.empty()); + return isSuccess_; } bool IsEmpty() diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 4e98f048a..1763f36be 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -146,8 +146,6 @@ void KVDBServiceImpl::Init() } if (meta.storeType < StoreMetaData::StoreType::STORE_KV_BEGIN || meta.storeType > StoreMetaData::StoreType::STORE_KV_END) { - ZLOGE("StoreType not match, bundleName:%{public}s, storeId:%{public}s, storeType: %{public}d", - meta.bundleName.c_str(), meta.GetStoreAlias().c_str(), meta.storeType); return; } auto watchers = GetWatchers(meta.tokenId, meta.storeId); @@ -156,13 +154,6 @@ void KVDBServiceImpl::Init() ZLOGE("store null, storeId:%{public}s", meta.GetStoreAlias().c_str()); return; } - if (meta.isPublic) { - auto mixMode = static_cast(GeneralStore::MixMode(GeneralStore::CLOUD_TIME_FIRST, - meta.isAutoSync ? GeneralStore::AUTO_SYNC_MODE : GeneralStore::MANUAL_SYNC_MODE)); - auto info = ChangeEvent::EventInfo(mixMode, 0, meta.isAutoSync, nullptr, nullptr); - auto evt = std::make_unique(std::move(storeInfo), std::move(info)); - EventCenter::GetInstance().PostEvent(std::move(evt)); - } store->RegisterDetailProgressObserver(nullptr); }; EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_SYNC, process); @@ -429,6 +420,9 @@ Status KVDBServiceImpl::RmvSubscribeInfo(const AppId &appId, const StoreId &stor Status KVDBServiceImpl::Subscribe(const AppId &appId, const StoreId &storeId, sptr observer) { + if (observer == nullptr) { + return INVALID_ARGUMENT; + } auto tokenId = IPCSkeleton::GetCallingTokenID(); ZLOGI("appId:%{public}s storeId:%{public}s tokenId:0x%{public}x", appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str(), tokenId); @@ -748,10 +742,9 @@ int32_t KVDBServiceImpl::GetInstIndex(uint32_t tokenId, const AppId &appId) KVDBServiceImpl::DBResult KVDBServiceImpl::HandleGenDetails(const GenDetails &details) { - DBResult dbResults; + DBResult dbResults{}; for (const auto &[id, detail] : details) { - auto &dbResult = dbResults[id]; - dbResult = DBStatus(detail.code); + dbResults[id] = DBStatus(detail.code); } return dbResults; } diff --git a/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp b/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp index 6821eed91..8bc689221 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 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 @@ -94,12 +94,12 @@ std::vector KVDBWatcher::ConvertToEntries(const std::vector &valu std::vector changeData{}; for (auto &info : values) { auto key = std::get_if(&info[0]); - if (key == nullptr) { + auto value = std::get_if(&info[1]); + if (key == nullptr || value == nullptr) { continue; } - auto value = std::get_if(&info[1]); Entry tmpEntry{ *key, *value }; - changeData.push_back(tmpEntry); + changeData.push_back(std::move(tmpEntry)); } return changeData; } @@ -112,7 +112,7 @@ std::vector KVDBWatcher::ConvertToKeys(const std::vector if (key == nullptr) { continue; } - keys.push_back(*key); + keys.push_back(std::move(*key)); } return keys; } diff --git a/services/distributeddataservice/service/kvdb/kvdb_watcher.h b/services/distributeddataservice/service/kvdb/kvdb_watcher.h index 92a50b596..407ced509 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_watcher.h +++ b/services/distributeddataservice/service/kvdb/kvdb_watcher.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 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 diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index f273ddbab..61989adf1 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -26,6 +26,7 @@ config("module_private_config") { "../config/include/", "../crypto/include/", "../directory/include/", + "../kvdb/", "../matrix/include/", "../common/", "../rdb/", diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index 9bfdf4974..4b00e119c 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -25,7 +25,7 @@ #include "eventcenter/event_center.h" #include "feature/feature_system.h" #include "ipc_skeleton.h" -#include "log_print.h" +#include "kvdb_query.h" #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" #include "metadata/store_meta_data_local.h" @@ -55,8 +55,6 @@ protected: StoreMetaData metaData_; }; -SchemaMeta CloudDataTest::schemaMeta_; - void KVDBGeneralStoreTest::InitMetaData() { metaData_.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; @@ -94,7 +92,6 @@ void KVDBGeneralStoreTest::TearDown() {} */ HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest, TestSize.Level0) { - ZLOGI("GetDBPasswordTest start"); InitMetaData(); auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); ASSERT_TRUE(dbPassword.GetSize() == 0); @@ -112,7 +109,6 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) { - ZLOGI("GetDBSecurityTest start"); auto dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::INVALID_LABEL); EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); @@ -151,7 +147,6 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) { - ZLOGI("GetDBOptionTest start"); auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); EXPECT_EQ(dbOption.syncDualTupleMode, true); @@ -164,7 +159,7 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) EXPECT_EQ(dbOption.cipher, DistributedDB::CipherType::AES_256_GCM); EXPECT_EQ(dbOption.conflictResolvePolicy, DistributedDB::LAST_WIN); EXPECT_EQ(dbOption.createDirByStoreIdOnly, true); - EXPECT_EQ(dbOption.secOption, GetDBSecurity(metaData_.securityLevel)); + EXPECT_EQ(dbOption.secOption, KVDBGeneralStore::GetDBSecurity(metaData_.securityLevel)); } /** @@ -176,7 +171,6 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) { - ZLOGI("CloseTest start"); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); auto ret = store->Close(); @@ -192,7 +186,6 @@ HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) { - ZLOGI("SyncTest start"); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; @@ -200,13 +193,15 @@ HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) auto mixMode = GeneralStore::MixMode(syncMode, highMode); std::string kvQuery = ""; KVDBQuery query(kvQuery); + SyncParam syncParam{}; + syncParam.mode = mixMode; auto ret = store->Sync( - {}, mixMode, query, [](const GenDetails &result) {}, 0); + {}, query, [](const GenDetails &result) {}, syncParam); EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - auto ret = store->Sync( - { "default_deviceId" }, mixMode, query, [](const GenDetails &result) {}, 0); + ret = store->Sync( + { "default_deviceId" }, query, [](const GenDetails &result) {}, syncParam); EXPECT_NE(ret, GeneralError::E_OK); - auto ret = store->Close(); + ret = store->Close(); EXPECT_EQ(ret, GeneralError::E_OK); } @@ -219,18 +214,12 @@ HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, CleanTest, TestSize.Level0) { - ZLOGI("CleanTest start"); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); EXPECT_EQ(store->IsValid(), true); - uint32_t syncMode = GeneralStore::SyncMode::NEARBY_PUSH; - uint32_t highMode = GeneralStore::HighMode::MANUAL_SYNC_MODE; - auto mixMode = GeneralStore::MixMode(syncMode, highMode); - std::string kvQuery = ""; - KVDBQuery query(kvQuery); auto ret = store->Clean({}, GeneralStore::CleanMode::NEARBY_DATA, ""); EXPECT_EQ(ret, GeneralError::E_OK); - auto ret = store->Close(); + ret = store->Close(); EXPECT_EQ(ret, GeneralError::E_OK); } } // namespace DistributedDataTest -- Gitee From 91be2b5745189b72197537876c69089c2c2b37fa Mon Sep 17 00:00:00 2001 From: Hollokin Date: Wed, 24 Apr 2024 20:41:10 +0800 Subject: [PATCH 12/15] =?UTF-8?q?=E8=A1=A5=E5=85=85TDD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../service/kvdb/kvdb_general_store.cpp | 1 + .../service/test/BUILD.gn | 18 ++- .../fuzztest/kvdbservicestub_fuzzer/BUILD.gn | 1 + .../service/test/kvdb_general_store_test.cpp | 128 +++++++++++------- 4 files changed, 96 insertions(+), 52 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index 9a225f001..d72fba4fe 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -109,6 +109,7 @@ KVDBGeneralStore::KVDBGeneralStore(const StoreMetaData &meta) : manager_(meta.ap delegate_ = tmpStore; }); if (delegate_ == nullptr || status != DBStatus::OK) { + ZLOGI("GetKvStore end. delegate is null?[%{public}d], status = %{public}d", delegate_ == nullptr, status); manager_.CloseKvStore(delegate_); return; } diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 61989adf1..8a38d898e 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -237,7 +237,21 @@ ohos_unittest("DeviceMatrixTest") { ohos_unittest("KVDBGeneralStoreTest") { module_out_path = module_output_path sources = [ - "kvdb_general_store_test.cpp" + "kvdb_general_store_test.cpp", + "../common/value_proxy.cpp", + "../kvdb/kvdb_general_store.cpp", + "../rdb/rdb_cloud.cpp", + "../rdb/rdb_query.cpp", + "mock/db_change_data_mock.cpp", + "mock/db_store_mock.cpp", + ] + + include_dirs = [ + "${data_service_path}/service/common", + "${data_service_path}/service/rdb", + "${relational_store_path}/interfaces/innerapi/clouddata/include", + "${relational_store_path}/interfaces/inner_api/rdb/include", + "${relational_store_path}/interfaces/inner_api/common_type/include", ] configs = [ ":module_private_config" ] @@ -250,6 +264,7 @@ ohos_unittest("KVDBGeneralStoreTest") { "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", + "kv_store:distributeddata_inner", ] deps = [ @@ -258,6 +273,7 @@ ohos_unittest("KVDBGeneralStoreTest") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:distributeddatasvc", "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb:distributeddb", "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata:distributeddata_inner", + "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/rdb:native_rdb", "//third_party/googletest:gtest_main", ] } diff --git a/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn index 8e2591d32..d0a512eb8 100644 --- a/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn @@ -88,6 +88,7 @@ ohos_fuzztest("KvdbServiceStubFuzzTest") { "${data_service_path}/adapter:distributeddata_adapter", "${data_service_path}/adapter/utils:distributeddata_utils_static", "${data_service_path}/framework:distributeddatasvcfwk", + "${data_service_path}/service:distributeddatasvc", "${kv_store_distributeddb_path}:distributeddb", ] diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index 4b00e119c..533f5f327 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -17,28 +17,24 @@ #include "kvdb_general_store.h" #include +#include -#include "account/account_delegate.h" -#include "communicator/device_manager_adapter.h" -#include "device_matrix.h" -#include "distributed_kv_data_manager.h" -#include "eventcenter/event_center.h" -#include "feature/feature_system.h" -#include "ipc_skeleton.h" +#include "bootstrap.h" +#include "crypto_manager.h" #include "kvdb_query.h" +#include "log_print.h" #include "metadata/meta_data_manager.h" +#include "metadata/secret_key_meta_data.h" #include "metadata/store_meta_data.h" #include "metadata/store_meta_data_local.h" #include "mock/db_store_mock.h" -#include "rdb_types.h" using namespace testing::ext; -using namespace OHOS::DistributedData; -using namespace OHOS::DistributedKv; -using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; +using DBStoreMock = OHOS::DistributedData::DBStoreMock; +using StoreMetaData = OHOS::DistributedData::StoreMetaData; -namespace OHOS::Test { -namespace DistributedDataTest { +namespace OHOS { +namespace DistributedKv { class KVDBGeneralStoreTest : public testing::Test { public: static void SetUpTestCase(void); @@ -50,25 +46,39 @@ protected: static constexpr const char *TEST_DISTRIBUTEDDATA_BUNDLE = "test_distributeddata"; static constexpr const char *TEST_DISTRIBUTEDDATA_STORE = "test_service_meta"; - void InitMetaData(); + static void InitMetaData(); + static std::vector Random(uint32_t len); static std::shared_ptr dbStoreMock_; StoreMetaData metaData_; }; +std::shared_ptr KVDBGeneralStoreTest::dbStoreMock_ = std::make_shared(); +static const uint32_t KEY_LENGTH = 32; +static const uint32_t ENCRYPT_KEY_LENGTH = 48; + void KVDBGeneralStoreTest::InitMetaData() { - metaData_.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; - metaData_.appId = TEST_DISTRIBUTEDDATA_BUNDLE; metaData_.bundleName = TEST_DISTRIBUTEDDATA_BUNDLE; - metaData_.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); - metaData_.user = std::to_string(DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(metaData_.tokenId)); + metaData_.appId = TEST_DISTRIBUTEDDATA_BUNDLE; + metaData_.user = "0"; metaData_.area = OHOS::DistributedKv::EL1; metaData_.instanceId = 0; metaData_.isAutoSync = true; - metaData_.storeType = 1; + metaData_.storeType = KvStoreType::SINGLE_VERSION; metaData_.storeId = TEST_DISTRIBUTEDDATA_STORE; - PolicyValue value; - value.type = OHOS::DistributedKv::PolicyType::IMMEDIATE_SYNC_ON_ONLINE; + metaData_.dataDir = "/data/service/el1/public/database/" + std::string(TEST_DISTRIBUTEDDATA_BUNDLE) + "/kvdb"; + metaData_.securityLevel = SecurityLevel::S2; +} + +std::vector KVDBGeneralStoreTest::Random(uint32_t len) +{ + std::random_device randomDevice; + std::uniform_int_distribution distribution(0, std::numeric_limits::max()); + std::vector key(len); + for (uint32_t i = 0; i < len; i++) { + key[i] = static_cast(distribution(randomDevice)); + } + return key; } void KVDBGeneralStoreTest::SetUpTestCase(void) @@ -79,25 +89,57 @@ void KVDBGeneralStoreTest::TearDownTestCase() {} void KVDBGeneralStoreTest::SetUp() { + Bootstrap::GetInstance().LoadDirectory(); } void KVDBGeneralStoreTest::TearDown() {} /** -* @tc.name: GetDBPasswordTest +* @tc.name: GetDBPasswordTest_001 * @tc.desc: GetDBPassword from meta. * @tc.type: FUNC * @tc.require: * @tc.author: Hollokin */ -HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest, TestSize.Level0) +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_001, TestSize.Level0) { + ZLOGI("GetDBPasswordTest start"); InitMetaData(); + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr); + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), metaData_, true)); auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); ASSERT_TRUE(dbPassword.GetSize() == 0); +} + +/** +* @tc.name: GetDBPasswordTest_002 +* @tc.desc: GetDBPassword from encrypt meta. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_002, TestSize.Level0) +{ + ZLOGI("GetDBPasswordTest_002 start"); + InitMetaData(); + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr); metaData_.isEncrypt = true; - dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); + + auto errCode = CryptoManager::GetInstance().GenerateRootKey(); + EXPECT_EQ(errCode, CryptoManager::ErrCode::SUCCESS); + + std::vector randomKey = Random(KEY_LENGTH); + SecretKeyMetaData secretKey; + secretKey.storeType = metaData_.storeType; + secretKey.sKey = CryptoManager::GetInstance().Encrypt(randomKey); + EXPECT_EQ(secretKey.sKey.size(), ENCRYPT_KEY_LENGTH); + EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), secretKey, true)); + + auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); ASSERT_TRUE(dbPassword.GetSize() != 0); + randomKey.assign(randomKey.size(), 0); } /** @@ -114,27 +156,27 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::NO_LABEL); - EXPECT_EQ(dbSecurity.securityLabel, SecurityLevel::NO_LABEL); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::NOT_SET); EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S0); - EXPECT_EQ(dbSecurity.securityLabel, SecurityLevel::S0); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S0); EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S1); - EXPECT_EQ(dbSecurity.securityLabel, SecurityLevel::S1); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S1); EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S2); - EXPECT_EQ(dbSecurity.securityLabel, SecurityLevel::S2); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S2); EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S3); - EXPECT_EQ(dbSecurity.securityLabel, SecurityLevel::S3); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S3); EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::SECE); dbSecurity = KVDBGeneralStore::GetDBSecurity(SecurityLevel::S4); - EXPECT_EQ(dbSecurity.securityLabel, SecurityLevel::S4); + EXPECT_EQ(dbSecurity.securityLabel, DistributedDB::S4); EXPECT_EQ(dbSecurity.securityFlag, DistributedDB::ECE); } @@ -147,6 +189,7 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBSecurityTest, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, GetDBOptionTest, TestSize.Level0) { + metaData_.isEncrypt = true; auto dbPassword = KVDBGeneralStore::GetDBPassword(metaData_); auto dbOption = KVDBGeneralStore::GetDBOption(metaData_, dbPassword); EXPECT_EQ(dbOption.syncDualTupleMode, true); @@ -186,6 +229,10 @@ HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) { + ZLOGI("SyncTest start"); + InitMetaData(); + mkdir(("/data/service/el1/public/database/" + std::string(TEST_DISTRIBUTEDDATA_BUNDLE)).c_str(), + (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); uint32_t syncMode = GeneralStore::SyncMode::NEARBY_SUBSCRIBE_REMOTE; @@ -197,30 +244,9 @@ HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) syncParam.mode = mixMode; auto ret = store->Sync( {}, query, [](const GenDetails &result) {}, syncParam); - EXPECT_EQ(ret, GeneralError::E_INVALID_ARGS); - ret = store->Sync( - { "default_deviceId" }, query, [](const GenDetails &result) {}, syncParam); EXPECT_NE(ret, GeneralError::E_OK); ret = store->Close(); EXPECT_EQ(ret, GeneralError::E_OK); } - -/** -* @tc.name: CleanTest -* @tc.desc: Clean. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Hollokin -*/ -HWTEST_F(KVDBGeneralStoreTest, CleanTest, TestSize.Level0) -{ - auto store = new (std::nothrow) KVDBGeneralStore(metaData_); - ASSERT_NE(store, nullptr); - EXPECT_EQ(store->IsValid(), true); - auto ret = store->Clean({}, GeneralStore::CleanMode::NEARBY_DATA, ""); - EXPECT_EQ(ret, GeneralError::E_OK); - ret = store->Close(); - EXPECT_EQ(ret, GeneralError::E_OK); -} } // namespace DistributedDataTest } // namespace OHOS::Test -- Gitee From 4f807a89ae9532b713a53f7cbb8c8c065ee3a4b0 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Thu, 25 Apr 2024 15:59:34 +0800 Subject: [PATCH 13/15] =?UTF-8?q?isPublic=E4=B8=8D=E8=B7=A8=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=EF=BC=8C=E5=8F=AA=E5=9C=A8localStoreMetaData=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../framework/include/metadata/store_meta_data.h | 1 - .../framework/metadata/store_meta_data.cpp | 2 -- .../service/kvdb/kvdb_general_store.cpp | 3 +-- .../service/kvdb/kvdb_service_impl.cpp | 7 +++---- 4 files changed, 4 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/framework/include/metadata/store_meta_data.h b/services/distributeddataservice/framework/include/metadata/store_meta_data.h index da6ee1780..6540932ef 100644 --- a/services/distributeddataservice/framework/include/metadata/store_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/store_meta_data.h @@ -36,7 +36,6 @@ struct API_EXPORT StoreMetaData final : public Serializable { bool isManualClean = false; bool isSearchable = false; bool isNeedCompress = false; - bool isPublic = false; int32_t storeType = -1; int32_t securityLevel = 0; int32_t area = 0; diff --git a/services/distributeddataservice/framework/metadata/store_meta_data.cpp b/services/distributeddataservice/framework/metadata/store_meta_data.cpp index 529e15465..939e95996 100644 --- a/services/distributeddataservice/framework/metadata/store_meta_data.cpp +++ b/services/distributeddataservice/framework/metadata/store_meta_data.cpp @@ -51,7 +51,6 @@ bool StoreMetaData::Marshal(json &node) const SetValue(node[GET_NAME(storeId)], storeId); SetValue(node[GET_NAME(user)], user); SetValue(node[GET_NAME(account)], account); - SetValue(node[GET_NAME(isPublic)], isPublic); // compatible with the versions which lower than VERSION_TAG_0000 SetValue(node[GET_NAME(kvStoreType)], storeType); @@ -89,7 +88,6 @@ bool StoreMetaData::Unmarshal(const json &node) GetValue(node, GET_NAME(storeId), storeId); GetValue(node, GET_NAME(user), user); GetValue(node, GET_NAME(account), account); - GetValue(node, GET_NAME(isPublic), isPublic); // compatible with the older versions if (version < FIELD_CHANGED_TAG) { diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index d72fba4fe..44cedb4e4 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -129,7 +129,6 @@ KVDBGeneralStore::KVDBGeneralStore(const StoreMetaData &meta) : manager_(meta.ap storeInfo_.storeName = meta.storeId; storeInfo_.instanceId = meta.instanceId; storeInfo_.user = std::stoi(meta.user); - storeInfo_.isPublic = meta.isPublic; } KVDBGeneralStore::~KVDBGeneralStore() @@ -522,7 +521,7 @@ void KVDBGeneralStore::ObserverProxy::OnChange(const DistributedDB::KvStoreChang void KVDBGeneralStore::ObserverProxy::ConvertChangeData(const std::list &entries, std::vector &values) { - for (auto entry : entries) { + for (auto &entry : entries) { auto value = std::vector{ entry.key, entry.value }; values.push_back(value); } diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 1763f36be..7905a55d1 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -121,7 +121,6 @@ KVDBServiceImpl::KVDBServiceImpl() std::bind(&KVDBServiceImpl::DoComplete, this, data, syncInfo, refCount, std::placeholders::_1)); } }); - Init(); } KVDBServiceImpl::~KVDBServiceImpl() @@ -137,7 +136,7 @@ void KVDBServiceImpl::Init() StoreMetaData meta; meta.storeId = storeInfo.storeName; meta.bundleName = storeInfo.bundleName; - meta.user = storeInfo.isPublic ? "0" : std::to_string(storeInfo.user); + meta.user = std::to_string(storeInfo.user); meta.deviceId = DMAdapter::GetInstance().GetLocalDevice().uuid; if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta, true)) { ZLOGE("meta empty, bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), @@ -497,7 +496,7 @@ Status KVDBServiceImpl::BeforeCreate(const AppId &appId, const StoreId &storeId, DistributedData::StoreInfo storeInfo; storeInfo.bundleName = appId.appId; storeInfo.instanceId = GetInstIndex(storeInfo.tokenId, appId); - storeInfo.user = std::stoi(meta.user); + storeInfo.user = std::atoi(meta.user.c_str()); executors_->Execute([storeInfo]() { auto event = std::make_unique(CloudEvent::GET_SCHEMA, storeInfo); EventCenter::GetInstance().PostEvent(move(event)); @@ -675,7 +674,6 @@ void KVDBServiceImpl::AddOptions(const Options &options, StoreMetaData &metaData metaData.schema = options.schema; metaData.account = AccountDelegate::GetInstance()->GetCurrentAccountId(); metaData.isNeedCompress = options.isNeedCompress; - metaData.isPublic = options.isPublic; } void KVDBServiceImpl::SaveLocalMetaData(const Options &options, const StoreMetaData &metaData) @@ -1038,6 +1036,7 @@ int32_t KVDBServiceImpl::OnInitialize() { RegisterKvServiceInfo(); RegisterHandler(); + Init(); return SUCCESS; } } // namespace OHOS::DistributedKv \ No newline at end of file -- Gitee From 2c296756ae9cb8ba52034174cc902266a5148826 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Thu, 25 Apr 2024 19:26:34 +0800 Subject: [PATCH 14/15] =?UTF-8?q?=E8=A7=A3=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../framework/include/store/general_store.h | 2 +- .../service/cloud/sync_manager.cpp | 59 ++++++++++++------- .../service/kvdb/kvdb_general_store.cpp | 13 ++-- .../service/kvdb/kvdb_general_store.h | 2 +- .../service/rdb/rdb_general_store.cpp | 7 +-- .../service/rdb/rdb_general_store.h | 2 +- 6 files changed, 50 insertions(+), 35 deletions(-) diff --git a/services/distributeddataservice/framework/include/store/general_store.h b/services/distributeddataservice/framework/include/store/general_store.h index 9948309eb..da1478185 100644 --- a/services/distributeddataservice/framework/include/store/general_store.h +++ b/services/distributeddataservice/framework/include/store/general_store.h @@ -90,7 +90,7 @@ public: }; virtual ~GeneralStore() = default; - virtual int32_t Bind(const std::map> &cloudDBs) = 0; + virtual int32_t Bind(Database &database, const std::map &bindInfos) = 0; virtual bool IsBound() = 0; diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 6e29ac934..daa087f8d 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -187,7 +187,7 @@ GeneralError SyncManager::IsValid(SyncInfo &info, CloudInfo &cloud) info.SetError(E_CLOUD_DISABLED); ZLOGE("cloudInfo invalid:%{public}d, ", cloud.IsValid(), Anonymous::Change(info.id_).c_str(), Anonymous::Change(cloud.id).c_str()); - return false; + return E_CLOUD_DISABLED; } if (!cloud.enableCloud || (!info.bundleName_.empty() && !cloud.IsOn(info.bundleName_))) { info.SetError(E_CLOUD_DISABLED); @@ -220,7 +220,7 @@ std::function SyncManager::GetPostEventTask(const std::vectorCheckSyncAction(storeInfo); if (status != SUCCESS) { ZLOGW("Verification strategy failed, status:%{public}d. %{public}d:%{public}s:%{public}s", status, @@ -245,6 +245,15 @@ ExecutorPool::Task SyncManager::GetSyncTask(int32_t times, bool retry, RefCount times++; return [this, times, retry, keep = std::move(ref), info = std::move(syncInfo)]() mutable { activeInfos_.Erase(info.syncId_); + bool createdByDefaultUser = false; + if (info.user_ == 0) { + std::vector users; + AccountDelegate::GetInstance()->QueryUsers(users); + if (!users.empty()) { + info.user_ = users[0]; + } + createdByDefaultUser = true; + } CloudInfo cloud; cloud.user = info.user_; auto retryer = GetRetryer(times, info); @@ -270,6 +279,9 @@ ExecutorPool::Task SyncManager::GetSyncTask(int32_t times, bool retry, RefCount } Defer defer(GetSyncHandler(std::move(retryer)), CloudEvent::CLOUD_SYNC); + if (createdByDefaultUser) { + info.user_ = 0; + } auto task = GetPostEventTask(schemas, cloud, info, retry); if (task != nullptr) { task(); @@ -421,25 +433,32 @@ AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, } if (!store->IsBound()) { - std::vector activeUsers{}; - AccountDelegate::GetInstance()->QueryForegroundUsers(activeUsers); - std::map> cloudDBs = {}; - for (auto &activeUser : activeUsers) { + std::vector users{}; + CloudInfo info; + if (user == 0) { + AccountDelegate::GetInstance()->QueryForegroundUsers(users); + if (!users.empty()) { + info.user = users[0]; + } + } else { + info.user = user; + users.push_back(user); + } + SchemaMeta schemaMeta; + std::string schemaKey = info.GetSchemaKey(meta.bundleName, meta.instanceId); + if (!MetaDataManager::GetInstance().LoadMeta(std::move(schemaKey), schemaMeta, true)) { + ZLOGE("failed, no schema bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), + meta.GetStoreAlias().c_str()); + return nullptr; + } + auto dbMeta = schemaMeta.GetDataBase(meta.storeId); + std::map bindInfos = {}; + for (auto &activeUser : users) { if (activeUser == 0) { continue; } - CloudInfo info; - info.user = activeUser; - SchemaMeta schemaMeta; - std::string schemaKey = info.GetSchemaKey(meta.bundleName, meta.instanceId); - if (!MetaDataManager::GetInstance().LoadMeta(std::move(schemaKey), schemaMeta, true)) { - ZLOGE("failed, no schema bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), - meta.GetStoreAlias().c_str()); - return nullptr; - } - auto dbMeta = schemaMeta.GetDataBase(meta.storeId); - auto cloudDB = instance->ConnectCloudDB(meta.bundleName, info.user, dbMeta); - auto assetLoader = instance->ConnectAssetLoader(meta.bundleName, info.user, dbMeta); + auto cloudDB = instance->ConnectCloudDB(meta.bundleName, activeUser, dbMeta); + auto assetLoader = instance->ConnectAssetLoader(meta.bundleName, activeUser, dbMeta); if (mustBind && (cloudDB == nullptr || assetLoader == nullptr)) { ZLOGE("failed, no cloud DB <0x%{public}x %{public}s<->%{public}s>", meta.tokenId, Anonymous::Change(dbMeta.name).c_str(), Anonymous::Change(dbMeta.alias).c_str()); @@ -448,10 +467,10 @@ AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, if (cloudDB != nullptr || assetLoader != nullptr) { GeneralStore::BindInfo bindInfo((cloudDB != nullptr) ? std::move(cloudDB) : cloudDB, (assetLoader != nullptr) ? std::move(assetLoader) : assetLoader); - cloudDBs[std::to_string(activeUser)] = std::make_pair(dbMeta, bindInfo); + bindInfos[activeUser] = bindInfo; } } - store->Bind(cloudDBs); + store->Bind(dbMeta, bindInfos); } return store; } diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index 44cedb4e4..0cf4c84e6 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -155,17 +155,14 @@ int32_t KVDBGeneralStore::BindSnapshots(std::shared_ptr> &cloudDBs) +int32_t KVDBGeneralStore::Bind(Database &database, const std::map &bindInfos) { - if (cloudDBs.empty()) { + if (bindInfos.empty()) { ZLOGW("No cloudDB!"); return GeneralError::E_OK; } std::map schemas{}; - for (auto &[userId, cloudDB] : cloudDBs) { - auto database = cloudDB.first; - auto bindInfo = cloudDB.second; - + for (auto &[userId, bindInfo] : bindInfos) { if (bindInfo.db_ == nullptr) { return GeneralError::E_INVALID_ARGS; } @@ -174,7 +171,7 @@ int32_t KVDBGeneralStore::Bind(const std::map(bindInfo.db_, nullptr) }); + dbClouds_.insert({ std::to_string(userId), std::make_shared(bindInfo.db_, nullptr) }); bindInfos_.insert(std::move(bindInfo)); DBSchema schema; @@ -193,7 +190,7 @@ int32_t KVDBGeneralStore::Bind(const std::map lock(rwMutex_); if (delegate_ == nullptr) { diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.h b/services/distributeddataservice/service/kvdb/kvdb_general_store.h index 89eaea687..307ae6d7c 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.h +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.h @@ -43,7 +43,7 @@ public: explicit KVDBGeneralStore(const StoreMetaData &meta); ~KVDBGeneralStore(); - int32_t Bind(const std::map> &cloudDBs) override; + int32_t Bind(Database &database, const std::map &bindInfos) override; bool IsBound() override; bool IsValid(); int32_t Execute(const std::string &table, const std::string &sql) override; diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/services/distributeddataservice/service/rdb/rdb_general_store.cpp index 5f1a843bb..7af9dfce2 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -120,13 +120,12 @@ int32_t RdbGeneralStore::BindSnapshots(std::shared_ptr> &cloudDBs) +int32_t RdbGeneralStore::Bind(Database &database, const std::map &bindInfos) { - if (cloudDBs.empty()) { + if (bindInfos.empty()) { return GeneralError::E_OK; } - auto database = cloudDBs.begin()->second.first; - auto bindInfo = cloudDBs.begin()->second.second; + auto bindInfo = bindInfos.begin()->second; if (bindInfo.db_ == nullptr || bindInfo.loader_ == nullptr) { return GeneralError::E_INVALID_ARGS; } diff --git a/services/distributeddataservice/service/rdb/rdb_general_store.h b/services/distributeddataservice/service/rdb/rdb_general_store.h index 07ebce110..7c3e64ea3 100644 --- a/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -47,7 +47,7 @@ public: explicit RdbGeneralStore(const StoreMetaData &meta); ~RdbGeneralStore(); - int32_t Bind(const std::map> &cloudDBs) override; + int32_t Bind(Database &database, const std::map &bindInfos) override; bool IsBound() override; bool IsValid(); int32_t Execute(const std::string &table, const std::string &sql) override; -- Gitee From 2aa50220fc274c68b63d6ee0803b8567b8cbb53e Mon Sep 17 00:00:00 2001 From: Hollokin Date: Thu, 25 Apr 2024 20:52:47 +0800 Subject: [PATCH 15/15] =?UTF-8?q?=E4=BF=9D=E7=95=99=E5=8E=9F=E5=85=88?= =?UTF-8?q?=E7=9A=84ConnectAssetLoader=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../framework/cloud/cloud_server.cpp | 10 +++ .../framework/include/cloud/cloud_server.h | 2 + .../framework/include/store/general_watcher.h | 5 +- .../framework/include/store/store_info.h | 1 - .../rust/extension/cloud_server_impl.cpp | 38 ++++++++++ .../rust/extension/cloud_server_impl.h | 2 + .../service/cloud/sync_manager.cpp | 70 +++++++++++-------- .../service/cloud/sync_manager.h | 19 +++-- .../service/kvdb/kvdb_general_store.cpp | 50 +++++++------ .../service/kvdb/kvdb_general_store.h | 1 + .../service/kvdb/kvdb_service_impl.cpp | 1 + .../service/kvdb/kvdb_service_stub.cpp | 2 +- .../service/kvdb/kvdb_watcher.cpp | 4 +- .../service/rdb/rdb_watcher.cpp | 5 ++ .../service/rdb/rdb_watcher.h | 1 + .../service/test/BUILD.gn | 12 ++-- .../fuzztest/kvdbservicestub_fuzzer/BUILD.gn | 6 +- .../service/test/kvdb_general_store_test.cpp | 24 +++---- 18 files changed, 166 insertions(+), 87 deletions(-) diff --git a/services/distributeddataservice/framework/cloud/cloud_server.cpp b/services/distributeddataservice/framework/cloud/cloud_server.cpp index d0689dfe8..30ee46a20 100644 --- a/services/distributeddataservice/framework/cloud/cloud_server.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_server.cpp @@ -50,12 +50,22 @@ int32_t CloudServer::Unsubscribe(int32_t userId, const std::map CloudServer::ConnectAssetLoader(uint32_t tokenId, const CloudServer::Database &dbMeta) +{ + return nullptr; +} + std::shared_ptr CloudServer::ConnectAssetLoader( const std::string &bundleName, int user, const CloudServer::Database &dbMeta) { return nullptr; } +std::shared_ptr CloudServer::ConnectCloudDB(uint32_t tokenId, const CloudServer::Database &dbMeta) +{ + return nullptr; +} + std::shared_ptr CloudServer::ConnectCloudDB( const std::string &bundleName, int user, const CloudServer::Database &dbMeta) { diff --git a/services/distributeddataservice/framework/include/cloud/cloud_server.h b/services/distributeddataservice/framework/include/cloud/cloud_server.h index 4b2a6ae0d..460d43dd4 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_server.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_server.h @@ -35,7 +35,9 @@ public: virtual int32_t Unsubscribe(int32_t userId, const std::map> &dbs); virtual std::shared_ptr ConnectAssetLoader( const std::string &bundleName, int user, const Database &dbMeta); + virtual std::shared_ptr ConnectAssetLoader(uint32_t tokenId, const Database &dbMeta); virtual std::shared_ptr ConnectCloudDB(const std::string &bundleName, int user, const Database &dbMeta); + virtual std::shared_ptr ConnectCloudDB(uint32_t tokenId, const Database &dbMeta); virtual std::shared_ptr ConnectSharingCenter(int32_t userId, const std::string &bunleName); virtual void Clean(int32_t userId); virtual void ReleaseUserInfo(int32_t userId); diff --git a/services/distributeddataservice/framework/include/store/general_watcher.h b/services/distributeddataservice/framework/include/store/general_watcher.h index 2d507ca6f..bcdfd5d1f 100644 --- a/services/distributeddataservice/framework/include/store/general_watcher.h +++ b/services/distributeddataservice/framework/include/store/general_watcher.h @@ -57,10 +57,7 @@ public: using ChangeData = std::map[OP_BUTT]>; virtual ~GeneralWatcher() = default; virtual int32_t OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) = 0; - virtual int32_t OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas) - { - return GeneralError::E_OK; - }; + virtual int32_t OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas) = 0; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_WATCHER_H diff --git a/services/distributeddataservice/framework/include/store/store_info.h b/services/distributeddataservice/framework/include/store/store_info.h index ce0001a91..8c5d02b2b 100644 --- a/services/distributeddataservice/framework/include/store/store_info.h +++ b/services/distributeddataservice/framework/include/store/store_info.h @@ -26,7 +26,6 @@ struct StoreInfo { int32_t instanceId = 0; int32_t user = 0; std::string deviceId; - bool isPublic = false; uint64_t syncId = 0; }; } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/rust/extension/cloud_server_impl.cpp b/services/distributeddataservice/rust/extension/cloud_server_impl.cpp index 316f28623..660b90855 100644 --- a/services/distributeddataservice/rust/extension/cloud_server_impl.cpp +++ b/services/distributeddataservice/rust/extension/cloud_server_impl.cpp @@ -597,6 +597,25 @@ int32_t CloudServerImpl::DoUnsubscribe(std::shared_ptr serv return DBErr::E_OK; } +std::shared_ptr CloudServerImpl::ConnectAssetLoader(uint32_t tokenId, const DBMeta &dbMeta) +{ + if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { + return nullptr; + } + HapTokenInfo hapInfo; + if (AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != RET_SUCCESS) { + return nullptr; + } + auto data = ExtensionUtil::Convert(dbMeta); + if (data.first == nullptr) { + return nullptr; + } + OhCloudExtCloudAssetLoader *loader = OhCloudExtCloudAssetLoaderNew(hapInfo.userID, + reinterpret_cast(hapInfo.bundleName.c_str()), + hapInfo.bundleName.size(), data.first); + return loader != nullptr ? std::make_shared(loader) : nullptr; +} + std::shared_ptr CloudServerImpl::ConnectAssetLoader( const std::string &bundleName, int user, const DBMeta &dbMeta) { @@ -609,6 +628,25 @@ std::shared_ptr CloudServerImpl::ConnectAssetLoader( return loader != nullptr ? std::make_shared(loader) : nullptr; } +std::shared_ptr CloudServerImpl::ConnectCloudDB(uint32_t tokenId, const DBMeta &dbMeta) +{ + if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { + return nullptr; + } + HapTokenInfo hapInfo; + if (AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != RET_SUCCESS) { + return nullptr; + } + auto data = ExtensionUtil::Convert(dbMeta); + if (data.first == nullptr) { + return nullptr; + } + OhCloudExtCloudDatabase *cloudDb = OhCloudExtCloudDbNew(hapInfo.userID, + reinterpret_cast(hapInfo.bundleName.c_str()), + hapInfo.bundleName.size(), data.first); + return cloudDb != nullptr ? std::make_shared(cloudDb) : nullptr; +} + std::shared_ptr CloudServerImpl::ConnectCloudDB( const std::string &bundleName, int user, const DBMeta &dbMeta) { diff --git a/services/distributeddataservice/rust/extension/cloud_server_impl.h b/services/distributeddataservice/rust/extension/cloud_server_impl.h index e52b446dd..d669d6d1e 100644 --- a/services/distributeddataservice/rust/extension/cloud_server_impl.h +++ b/services/distributeddataservice/rust/extension/cloud_server_impl.h @@ -40,8 +40,10 @@ public: DBSchemaMeta GetAppSchema(int32_t userId, const std::string &bundleName) override; int32_t Subscribe(int32_t userId, const std::map> &dbs) override; int32_t Unsubscribe(int32_t userId, const std::map> &dbs) override; + std::shared_ptr ConnectAssetLoader(uint32_t tokenId, const DBMeta &dbMeta) override; std::shared_ptr ConnectAssetLoader( const std::string &bundleName, int user, const DBMeta &dbMeta) override; + std::shared_ptr ConnectCloudDB(uint32_t tokenId, const DBMeta &dbMeta) override; std::shared_ptr ConnectCloudDB(const std::string &bundleName, int user, const DBMeta &dbMeta) override; private: diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index daa087f8d..d6bdb9ff5 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -26,10 +26,9 @@ #include "log_print.h" #include "metadata/meta_data_manager.h" #include "sync_strategies/network_sync_strategy.h" -#include "store/auto_cache.h" #include "user_delegate.h" -#include "store/general_store.h" #include "utils/anonymous.h" + namespace OHOS::CloudData { using namespace DistributedData; using namespace DistributedKv; @@ -220,7 +219,7 @@ std::function SyncManager::GetPostEventTask(const std::vectorCheckSyncAction(storeInfo); if (status != SUCCESS) { ZLOGW("Verification strategy failed, status:%{public}d. %{public}d:%{public}s:%{public}s", status, @@ -301,10 +300,14 @@ std::function SyncManager::GetSyncHandler(Retryer retryer) StoreMetaData meta(storeInfo); meta.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta, true)) { - ZLOGE("failed, no store meta bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), - meta.GetStoreAlias().c_str()); - DoExceptionalCallback(async, details, storeInfo); - return; + meta.user = "0"; // check if it is a public store. + StoreMetaDataLocal localMetaData; + if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKeyLocal(), localMetaData, true) || + !localMetaData.isPublic || !MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta, true)) { + ZLOGE("failed, no store meta. bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), + meta.GetStoreAlias().c_str()); + return DoExceptionalCallback(async, details, storeInfo); + } } auto store = GetStore(meta, storeInfo.user); if (store == nullptr) { @@ -312,7 +315,6 @@ std::function SyncManager::GetSyncHandler(Retryer retryer) DoExceptionalCallback(async, details, storeInfo); return; } - ZLOGD("database:<%{public}d:%{public}s:%{public}s> sync start", storeInfo.user, storeInfo.bundleName.c_str(), meta.GetStoreAlias().c_str()); SyncParam syncParam = { evt.GetMode(), evt.GetWait(), evt.IsCompensation() }; @@ -414,6 +416,35 @@ void SyncManager::UpdateSchema(const SyncManager::SyncInfo &syncInfo) EventCenter::GetInstance().PostEvent(std::make_unique(CloudEvent::GET_SCHEMA, storeInfo)); } +std::map SyncManager::GetBindInfos(const StoreMetaData &meta, + const std::vector &users, CloudInfo &info, Database &schemaDatabase, bool mustBind) +{ + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + ZLOGD("not support cloud sync"); + return {}; + } + std::map bindInfos = {}; + for (auto &activeUser : users) { + if (activeUser == 0) { + continue; + } + auto cloudDB = instance->ConnectCloudDB(meta.bundleName, activeUser, schemaDatabase); + auto assetLoader = instance->ConnectAssetLoader(meta.bundleName, activeUser, schemaDatabase); + if (mustBind && (cloudDB == nullptr || assetLoader == nullptr)) { + ZLOGE("failed, no cloud DB <0x%{public}x %{public}s<->%{public}s>", meta.tokenId, + Anonymous::Change(schemaDatabase.name).c_str(), Anonymous::Change(schemaDatabase.alias).c_str()); + return {}; + } + if (cloudDB != nullptr || assetLoader != nullptr) { + GeneralStore::BindInfo bindInfo((cloudDB != nullptr) ? std::move(cloudDB) : cloudDB, + (assetLoader != nullptr) ? std::move(assetLoader) : assetLoader); + bindInfos[activeUser] = bindInfo; + } + } + return bindInfos; +} + AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, bool mustBind) { if (user != 0 && !Account::GetInstance()->IsVerified(user)) { @@ -425,13 +456,11 @@ AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, ZLOGD("not support cloud sync"); return nullptr; } - auto store = AutoCache::GetInstance().GetStore(meta, {}); if (store == nullptr) { ZLOGE("store null, storeId:%{public}s", meta.GetStoreAlias().c_str()); return nullptr; } - if (!store->IsBound()) { std::vector users{}; CloudInfo info; @@ -448,28 +477,11 @@ AutoCache::Store SyncManager::GetStore(const StoreMetaData &meta, int32_t user, std::string schemaKey = info.GetSchemaKey(meta.bundleName, meta.instanceId); if (!MetaDataManager::GetInstance().LoadMeta(std::move(schemaKey), schemaMeta, true)) { ZLOGE("failed, no schema bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), - meta.GetStoreAlias().c_str()); + meta.GetStoreAlias().c_str()); return nullptr; } auto dbMeta = schemaMeta.GetDataBase(meta.storeId); - std::map bindInfos = {}; - for (auto &activeUser : users) { - if (activeUser == 0) { - continue; - } - auto cloudDB = instance->ConnectCloudDB(meta.bundleName, activeUser, dbMeta); - auto assetLoader = instance->ConnectAssetLoader(meta.bundleName, activeUser, dbMeta); - if (mustBind && (cloudDB == nullptr || assetLoader == nullptr)) { - ZLOGE("failed, no cloud DB <0x%{public}x %{public}s<->%{public}s>", meta.tokenId, - Anonymous::Change(dbMeta.name).c_str(), Anonymous::Change(dbMeta.alias).c_str()); - return nullptr; - } - if (cloudDB != nullptr || assetLoader != nullptr) { - GeneralStore::BindInfo bindInfo((cloudDB != nullptr) ? std::move(cloudDB) : cloudDB, - (assetLoader != nullptr) ? std::move(assetLoader) : assetLoader); - bindInfos[activeUser] = bindInfo; - } - } + std::map bindInfos = GetBindInfos(meta, users, info, dbMeta, mustBind); store->Bind(dbMeta, bindInfos); } return store; diff --git a/services/distributeddataservice/service/cloud/sync_manager.h b/services/distributeddataservice/service/cloud/sync_manager.h index a32dc782b..8c5bfc450 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.h +++ b/services/distributeddataservice/service/cloud/sync_manager.h @@ -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 @@ -15,17 +15,20 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_SYNC_MANAGER_H #define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_SYNC_MANAGER_H + +#include "cloud/cloud_event.h" +#include "cloud/cloud_info.h" +#include "cloud/sync_strategy.h" +#include "cloud_types.h" +#include "concurrent_map.h" #include "eventcenter/event.h" #include "executor_pool.h" +#include "metadata/store_meta_data_local.h" #include "store/auto_cache.h" #include "store/general_store.h" #include "store/general_value.h" #include "utils/ref_count.h" -#include "concurrent_map.h" -#include "cloud/cloud_info.h" -#include "cloud/sync_strategy.h" -#include "cloud_types.h" -#include "cloud/cloud_event.h" + namespace OHOS::CloudData { class SyncManager { public: @@ -54,7 +57,7 @@ public: void SetError(int32_t code) const; void SetCompensation(bool isCompensation); std::shared_ptr GenerateQuery(const std::string &store, const Tables &tables); - bool Contains(const std::string& storeName); + bool Contains(const std::string &storeName); inline static constexpr const char *DEFAULT_ID = "default"; private: @@ -102,6 +105,8 @@ private: static uint64_t GenerateId(int32_t user); static ExecutorPool::Duration GetInterval(int32_t code); + static std::map GetBindInfos(const StoreMetaData &meta, + const std::vector &users, CloudInfo &info, DistributedData::Database &schemaDatabase, bool mustBind); Task GetSyncTask(int32_t times, bool retry, RefCount ref, SyncInfo &&syncInfo); void UpdateSchema(const SyncInfo &syncInfo); std::function GetSyncHandler(Retryer retryer); diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp index 0cf4c84e6..3005117a2 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.cpp @@ -109,7 +109,7 @@ KVDBGeneralStore::KVDBGeneralStore(const StoreMetaData &meta) : manager_(meta.ap delegate_ = tmpStore; }); if (delegate_ == nullptr || status != DBStatus::OK) { - ZLOGI("GetKvStore end. delegate is null?[%{public}d], status = %{public}d", delegate_ == nullptr, status); + ZLOGE("GetKvStore failed. delegate is null?[%{public}d], status = %{public}d", delegate_ == nullptr, status); manager_.CloseKvStore(delegate_); return; } @@ -152,7 +152,7 @@ KVDBGeneralStore::~KVDBGeneralStore() int32_t KVDBGeneralStore::BindSnapshots(std::shared_ptr>> bindAssets) { - return GenErr::E_OK; + return GenErr::E_NOT_SUPPORT; } int32_t KVDBGeneralStore::Bind(Database &database, const std::map &bindInfos) @@ -229,28 +229,28 @@ int32_t KVDBGeneralStore::Close() int32_t KVDBGeneralStore::Execute(const std::string &table, const std::string &sql) { - return GeneralError::E_OK; + return GeneralError::E_NOT_SUPPORT; } int32_t KVDBGeneralStore::Insert(const std::string &table, VBuckets &&values) { - return GeneralError::E_OK; + return GeneralError::E_NOT_SUPPORT; } int32_t KVDBGeneralStore::Update(const std::string &table, const std::string &setSql, Values &&values, const std::string &whereSql, Values &&conditions) { - return GeneralError::E_OK; + return GeneralError::E_NOT_SUPPORT; } int32_t KVDBGeneralStore::Delete(const std::string &table, const std::string &sql, Values &&args) { - return GeneralError::E_OK; + return GeneralError::E_NOT_SUPPORT; } int32_t KVDBGeneralStore::Replace(const std::string &table, VBucket &&value) { - return GeneralError::E_OK; + return GeneralError::E_NOT_SUPPORT; } std::shared_ptr KVDBGeneralStore::Query( @@ -266,7 +266,7 @@ std::shared_ptr KVDBGeneralStore::Query(const std::string &table, GenQue int32_t KVDBGeneralStore::MergeMigratedData(const std::string &tableName, VBuckets &&values) { - return GeneralError::E_OK; + return GeneralError::E_NOT_SUPPORT; } KVDBGeneralStore::DBSyncCallback KVDBGeneralStore::GetDBSyncCompleteCB(DetailAsync async) @@ -288,6 +288,22 @@ KVDBGeneralStore::DBSyncCallback KVDBGeneralStore::GetDBSyncCompleteCB(DetailAsy }; } +DBStatus KVDBGeneralStore::CloudSync(const Devices &devices, DistributedDB::SyncMode &cloudSyncMode, int64_t wait) +{ + DistributedDB::CloudSyncOption syncOption; + syncOption.devices = devices; + syncOption.mode = cloudSyncMode; + syncOption.waitTime = wait; + if (storeInfo_.user == 0) { + std::vector users; + AccountDelegate::GetInstance()->QueryUsers(users); + syncOption.users.push_back(std::to_string(users[0])); + } else { + syncOption.users.push_back(std::to_string(storeInfo_.user)); + } + return delegate_->Sync(syncOption, nullptr); +} + int32_t KVDBGeneralStore::Sync(const Devices &devices, GenQuery &query, DetailAsync async, SyncParam &syncParm) { auto syncMode = GeneralStore::GetSyncMode(syncParm.mode); @@ -300,18 +316,7 @@ int32_t KVDBGeneralStore::Sync(const Devices &devices, GenQuery &query, DetailAs auto dbStatus = DistributedDB::OK; auto dbMode = DistributedDB::SyncMode(syncMode); if (syncMode > NEARBY_END && syncMode < CLOUD_END) { - DistributedDB::CloudSyncOption syncOption; - syncOption.devices = devices; - syncOption.mode = dbMode; - syncOption.waitTime = syncParm.wait; - if (storeInfo_.user == 0) { - std::vector users; - AccountDelegate::GetInstance()->QueryUsers(users); - syncOption.users.push_back(std::to_string(users[0])); - } else { - syncOption.users.push_back(std::to_string(storeInfo_.user)); - } - dbStatus = delegate_->Sync(syncOption, nullptr); + dbStatus = CloudSync(devices, dbMode, syncParm.wait); } else { if (devices.empty()) { ZLOGE("Devices is empty! mode:%{public}d", syncParm.mode); @@ -328,7 +333,8 @@ int32_t KVDBGeneralStore::Sync(const Devices &devices, GenQuery &query, DetailAs if (syncMode == NEARBY_SUBSCRIBE_REMOTE) { dbStatus = delegate_->SubscribeRemoteQuery(devices, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); } else if (syncMode == NEARBY_UNSUBSCRIBE_REMOTE) { - dbStatus = delegate_->UnSubscribeRemoteQuery(devices, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); + dbStatus = + delegate_->UnSubscribeRemoteQuery(devices, GetDBSyncCompleteCB(std::move(async)), dbQuery, false); } else if (syncMode < NEARBY_END) { if (kvQuery->IsEmpty()) { dbStatus = delegate_->Sync(devices, dbMode, GetDBSyncCompleteCB(std::move(async)), false); @@ -487,7 +493,7 @@ void KVDBGeneralStore::ObserverProxy::OnChange(DBOrigin origin, const std::strin genOrigin.store = storeId_; Watcher::ChangeInfo changeInfo; for (uint32_t i = 0; i < DistributedDB::OP_BUTT; ++i) { - auto &info = changeInfo[data.tableName][i]; + auto &info = changeInfo[storeId_][i]; for (auto &priData : data.primaryData[i]) { Watcher::PRIValue value; Convert(std::move(*(priData.begin())), value); diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.h b/services/distributeddataservice/service/kvdb/kvdb_general_store.h index 307ae6d7c..0a91022ac 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.h +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.h @@ -83,6 +83,7 @@ private: using DBProcessCB = std::function &processes)>; static GenErr ConvertStatus(DBStatus status); DBSyncCallback GetDBSyncCompleteCB(DetailAsync async); + DBStatus CloudSync(const Devices &devices, DistributedDB::SyncMode &cloudSyncMode, int64_t wait); class ObserverProxy : public DistributedDB::KvStoreObserver { public: using DBOrigin = DistributedDB::Origin; diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 7905a55d1..cd913372c 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -260,6 +260,7 @@ Status KVDBServiceImpl::Sync(const AppId &appId, const StoreId &storeId, const S Anonymous::Change(storeId.storeId).c_str()); return Status::SUCCESS; } + CloudSync(appId, storeId); } return KvStoreSyncManager::GetInstance()->AddSyncOperation(uintptr_t(metaData.tokenId), delay, std::bind(&KVDBServiceImpl::DoSyncInOrder, this, metaData, syncInfo, std::placeholders::_1, ACTION_SYNC), diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp index 0a60988b5..358889cab 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp @@ -30,7 +30,6 @@ const KVDBServiceStub::Handler &KVDBServiceStub::OnAfterCreate, &KVDBServiceStub::OnDelete, &KVDBServiceStub::OnSync, - &KVDBServiceStub::OnCloudSync, &KVDBServiceStub::OnRegisterCallback, &KVDBServiceStub::OnUnregisterCallback, &KVDBServiceStub::OnSetSyncParam, @@ -44,6 +43,7 @@ const KVDBServiceStub::Handler &KVDBServiceStub::OnUnsubscribe, &KVDBServiceStub::OnGetBackupPassword, &KVDBServiceStub::OnSyncExt, + &KVDBServiceStub::OnCloudSync, }; int KVDBServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) diff --git a/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp b/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp index 8bc689221..231caae3e 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_watcher.cpp @@ -44,7 +44,7 @@ int32_t KVDBWatcher::OnChange(const Origin &origin, const PRIFields &primaryFiel DataOrigin dataOrigin; dataOrigin.id = origin.id; dataOrigin.store = origin.store; - for(auto &observer : observers) { + for (auto &observer : observers) { observer->OnChange(dataOrigin, std::move(keys)); } } @@ -64,7 +64,7 @@ int32_t KVDBWatcher::OnChange(const Origin &origin, const Fields &fields, Change auto updates = ConvertToEntries(changeData->second[OP_UPDATE]); auto deletes = ConvertToEntries(changeData->second[OP_DELETE]); ChangeNotification change(std::move(inserts), std::move(updates), std::move(deletes), {}, false); - for(auto &observer : observers) { + for (auto &observer : observers) { observer->OnChange(change); } } diff --git a/services/distributeddataservice/service/rdb/rdb_watcher.cpp b/services/distributeddataservice/service/rdb/rdb_watcher.cpp index 7544ebdae..1791f649a 100644 --- a/services/distributeddataservice/service/rdb/rdb_watcher.cpp +++ b/services/distributeddataservice/service/rdb/rdb_watcher.cpp @@ -47,6 +47,11 @@ int32_t RdbWatcher::OnChange(const Origin &origin, const PRIFields &primaryField return E_OK; } +int32_t RdbWatcher::OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas) +{ + return E_OK; +} + sptr RdbWatcher::GetNotifier() const { std::shared_lock lock(mutex_); diff --git a/services/distributeddataservice/service/rdb/rdb_watcher.h b/services/distributeddataservice/service/rdb/rdb_watcher.h index ac898854c..c6e95dd94 100644 --- a/services/distributeddataservice/service/rdb/rdb_watcher.h +++ b/services/distributeddataservice/service/rdb/rdb_watcher.h @@ -26,6 +26,7 @@ class RdbWatcher : public DistributedData::GeneralWatcher { public: RdbWatcher(); int32_t OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) override; + int32_t OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas) override; sptr GetNotifier() const; void SetNotifier(sptr notifier); diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 5089cf533..1cfa1af89 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -237,11 +237,11 @@ ohos_unittest("DeviceMatrixTest") { ohos_unittest("KVDBGeneralStoreTest") { module_out_path = module_output_path sources = [ - "kvdb_general_store_test.cpp", "../common/value_proxy.cpp", "../kvdb/kvdb_general_store.cpp", "../rdb/rdb_cloud.cpp", "../rdb/rdb_query.cpp", + "kvdb_general_store_test.cpp", "mock/db_change_data_mock.cpp", "mock/db_store_mock.cpp", ] @@ -268,12 +268,10 @@ ohos_unittest("KVDBGeneralStoreTest") { ] deps = [ - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:distributeddatasvc", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb:distributeddb", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata:distributeddata_inner", - "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/rdb:native_rdb", + "${data_service_path}/adapter:distributeddata_adapter", + "${data_service_path}/framework:distributeddatasvcfwk", + "${data_service_path}/service:distributeddatasvc", + "${kv_store_distributeddb_path}:distributeddb", "//third_party/googletest:gtest_main", ] } diff --git a/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn index d0a512eb8..d5a1231cd 100644 --- a/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/kvdbservicestub_fuzzer/BUILD.gn @@ -26,7 +26,7 @@ ohos_fuzztest("KvdbServiceStubFuzzTest") { "${data_service_path}/framework/include", "${data_service_path}/service/backup/include", "${data_service_path}/service/bootstrap/include", - "${data_service_path}/service/common/include", + "${data_service_path}/service/common", "${data_service_path}/service/config/include", "${data_service_path}/service/crypto/include", "${data_service_path}/service/kvdb", @@ -41,6 +41,7 @@ ohos_fuzztest("KvdbServiceStubFuzzTest") { "${kv_store_distributeddb_path}/include/", "${kv_store_distributeddb_path}/interfaces/include/", "${kv_store_distributeddb_path}/interfaces/include/relational", + "${relational_store_path}/interfaces/inner_api/common_type/include", "//third_party/json/single_include", ] @@ -59,6 +60,7 @@ ohos_fuzztest("KvdbServiceStubFuzzTest") { "${data_service_path}/app/src/checker/system_checker.cpp", "${data_service_path}/service/backup/src/backup_manager.cpp", "${data_service_path}/service/bootstrap/src/bootstrap.cpp", + "${data_service_path}/service/common/value_proxy.cpp", "${data_service_path}/service/config/src/config_factory.cpp", "${data_service_path}/service/config/src/model/backup_config.cpp", "${data_service_path}/service/config/src/model/checker_config.cpp", @@ -81,6 +83,8 @@ ohos_fuzztest("KvdbServiceStubFuzzTest") { "${data_service_path}/service/matrix/src/device_matrix.cpp", "${data_service_path}/service/matrix/src/matrix_event.cpp", "${data_service_path}/service/permission/src/permit_delegate.cpp", + "${data_service_path}/service/rdb/rdb_cloud.cpp", + "${data_service_path}/service/rdb/rdb_query.cpp", "kvdbservicestub_fuzzer.cpp", ] diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index 533f5f327..23a555dd4 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -43,37 +43,37 @@ public: void TearDown(); protected: - static constexpr const char *TEST_DISTRIBUTEDDATA_BUNDLE = "test_distributeddata"; - static constexpr const char *TEST_DISTRIBUTEDDATA_STORE = "test_service_meta"; + static constexpr const char *bundleName = "test_distributeddata"; + static constexpr const char *storeName = "test_service_meta"; - static void InitMetaData(); + void InitMetaData(); static std::vector Random(uint32_t len); static std::shared_ptr dbStoreMock_; StoreMetaData metaData_; }; -std::shared_ptr KVDBGeneralStoreTest::dbStoreMock_ = std::make_shared(); +std::shared_ptr KVDBGeneralStoreTest::dbStoreMock_ = std::make_shared(); static const uint32_t KEY_LENGTH = 32; static const uint32_t ENCRYPT_KEY_LENGTH = 48; void KVDBGeneralStoreTest::InitMetaData() { - metaData_.bundleName = TEST_DISTRIBUTEDDATA_BUNDLE; - metaData_.appId = TEST_DISTRIBUTEDDATA_BUNDLE; + metaData_.bundleName = bundleName; + metaData_.appId = bundleName; metaData_.user = "0"; metaData_.area = OHOS::DistributedKv::EL1; metaData_.instanceId = 0; metaData_.isAutoSync = true; metaData_.storeType = KvStoreType::SINGLE_VERSION; - metaData_.storeId = TEST_DISTRIBUTEDDATA_STORE; - metaData_.dataDir = "/data/service/el1/public/database/" + std::string(TEST_DISTRIBUTEDDATA_BUNDLE) + "/kvdb"; + metaData_.storeId = storeName; + metaData_.dataDir = "/data/service/el1/public/database/" + std::string(bundleName) + "/kvdb"; metaData_.securityLevel = SecurityLevel::S2; } std::vector KVDBGeneralStoreTest::Random(uint32_t len) { std::random_device randomDevice; - std::uniform_int_distribution distribution(0, std::numeric_limits::max()); + std::uniform_int_distribution distribution(0, std::numeric_limits::max()); std::vector key(len); for (uint32_t i = 0; i < len; i++) { key[i] = static_cast(distribution(randomDevice)); @@ -90,6 +90,7 @@ void KVDBGeneralStoreTest::TearDownTestCase() {} void KVDBGeneralStoreTest::SetUp() { Bootstrap::GetInstance().LoadDirectory(); + InitMetaData(); } void KVDBGeneralStoreTest::TearDown() {} @@ -104,7 +105,6 @@ void KVDBGeneralStoreTest::TearDown() {} HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_001, TestSize.Level0) { ZLOGI("GetDBPasswordTest start"); - InitMetaData(); MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr); EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetSecretKey(), metaData_, true)); @@ -122,7 +122,6 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_001, TestSize.Level0) HWTEST_F(KVDBGeneralStoreTest, GetDBPasswordTest_002, TestSize.Level0) { ZLOGI("GetDBPasswordTest_002 start"); - InitMetaData(); MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr); metaData_.isEncrypt = true; EXPECT_TRUE(MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_, true)); @@ -230,8 +229,7 @@ HWTEST_F(KVDBGeneralStoreTest, CloseTest, TestSize.Level0) HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) { ZLOGI("SyncTest start"); - InitMetaData(); - mkdir(("/data/service/el1/public/database/" + std::string(TEST_DISTRIBUTEDDATA_BUNDLE)).c_str(), + mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); -- Gitee