From e4d690700e3b1e64c2a8dc193f5d90866f20b8cc Mon Sep 17 00:00:00 2001 From: code4lala Date: Thu, 19 Jan 2023 14:44:35 +0800 Subject: [PATCH 001/152] openssl 3.x Signed-off-by: code4lala Change-Id: I811174e9ecc5097a1e035134b3ceed99f005ba8a --- services/distributeddataservice/adapter/BUILD.gn | 1 + services/distributeddataservice/adapter/account/BUILD.gn | 1 + .../distributeddataservice/adapter/account/test/BUILD.gn | 1 + services/distributeddataservice/adapter/autils/BUILD.gn | 1 + services/distributeddataservice/adapter/autils/test/BUILD.gn | 1 + services/distributeddataservice/adapter/broadcaster/BUILD.gn | 1 + .../distributeddataservice/adapter/communicator/BUILD.gn | 1 + .../adapter/communicator/test/BUILD.gn | 2 ++ services/distributeddataservice/adapter/dfx/BUILD.gn | 1 + services/distributeddataservice/adapter/dfx/test/BUILD.gn | 2 ++ services/distributeddataservice/adapter/permission/BUILD.gn | 1 + .../distributeddataservice/adapter/permission/test/BUILD.gn | 1 + services/distributeddataservice/adapter/utils/BUILD.gn | 1 + services/distributeddataservice/app/BUILD.gn | 1 + services/distributeddataservice/app/src/checker/BUILD.gn | 1 + .../distributeddataservice/app/src/flowctrl_manager/BUILD.gn | 1 + services/distributeddataservice/app/src/uninstaller/BUILD.gn | 1 + services/distributeddataservice/app/test/BUILD.gn | 1 + services/distributeddataservice/framework/BUILD.gn | 1 + services/distributeddataservice/framework/test/BUILD.gn | 1 + services/distributeddataservice/service/BUILD.gn | 1 + services/distributeddataservice/service/test/BUILD.gn | 5 ++++- services/distributeddataservice/test/BUILD.gn | 1 + .../test/fuzztest/schemaquery_fuzzer/BUILD.gn | 1 + test/fuzztest/autolaunch_fuzzer/BUILD.gn | 1 + test/fuzztest/blob_fuzzer/BUILD.gn | 1 + test/fuzztest/devicekvstore_fuzzer/BUILD.gn | 1 + test/fuzztest/distributedkvdatamanager_fuzzer/BUILD.gn | 1 + test/fuzztest/kvstoredisksize_fuzzer/BUILD.gn | 1 + test/fuzztest/singlekvstore_fuzzer/BUILD.gn | 1 + test/fuzztest/taskscheduler_fuzzer/BUILD.gn | 1 + test/fuzztest/typesutil_fuzzer/BUILD.gn | 1 + 32 files changed, 37 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/adapter/BUILD.gn b/services/distributeddataservice/adapter/BUILD.gn index 8f2232af..34abe79d 100644 --- a/services/distributeddataservice/adapter/BUILD.gn +++ b/services/distributeddataservice/adapter/BUILD.gn @@ -20,6 +20,7 @@ config("distributeddata_adapter_private_config") { cflags = [ "-Wno-multichar" ] cflags_cc = [ "-fvisibility=hidden" ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } config("distributeddata_adapter_public_config") { diff --git a/services/distributeddataservice/adapter/account/BUILD.gn b/services/distributeddataservice/adapter/account/BUILD.gn index bf53ea52..11a20558 100755 --- a/services/distributeddataservice/adapter/account/BUILD.gn +++ b/services/distributeddataservice/adapter/account/BUILD.gn @@ -59,4 +59,5 @@ ohos_static_library("distributeddata_account_static") { } subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/adapter/account/test/BUILD.gn b/services/distributeddataservice/adapter/account/test/BUILD.gn index f0540307..fa8c3d6e 100755 --- a/services/distributeddataservice/adapter/account/test/BUILD.gn +++ b/services/distributeddataservice/adapter/account/test/BUILD.gn @@ -41,6 +41,7 @@ ohos_unittest("AccountDelegateTest") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } ############################################################################### diff --git a/services/distributeddataservice/adapter/autils/BUILD.gn b/services/distributeddataservice/adapter/autils/BUILD.gn index bb40ed87..ad0f6ebe 100755 --- a/services/distributeddataservice/adapter/autils/BUILD.gn +++ b/services/distributeddataservice/adapter/autils/BUILD.gn @@ -39,4 +39,5 @@ ohos_static_library("distributeddata_autils_static") { ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/adapter/autils/test/BUILD.gn b/services/distributeddataservice/adapter/autils/test/BUILD.gn index 283d74b8..afd7c605 100755 --- a/services/distributeddataservice/adapter/autils/test/BUILD.gn +++ b/services/distributeddataservice/adapter/autils/test/BUILD.gn @@ -22,6 +22,7 @@ config("module_private_config") { "../../include/autils/", "//foundation/distributeddatamgr/kv_store/frameworks/common", ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } ohos_unittest("KvStoreThreadPoolTest") { diff --git a/services/distributeddataservice/adapter/broadcaster/BUILD.gn b/services/distributeddataservice/adapter/broadcaster/BUILD.gn index 387aae13..78c9885b 100755 --- a/services/distributeddataservice/adapter/broadcaster/BUILD.gn +++ b/services/distributeddataservice/adapter/broadcaster/BUILD.gn @@ -41,4 +41,5 @@ ohos_static_library("distributeddata_broadcaster_static") { ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/adapter/communicator/BUILD.gn b/services/distributeddataservice/adapter/communicator/BUILD.gn index 90916afd..48c03993 100755 --- a/services/distributeddataservice/adapter/communicator/BUILD.gn +++ b/services/distributeddataservice/adapter/communicator/BUILD.gn @@ -64,4 +64,5 @@ ohos_static_library("distributeddata_communicator_static") { subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/adapter/communicator/test/BUILD.gn b/services/distributeddataservice/adapter/communicator/test/BUILD.gn index 318f2192..a3fa43ac 100755 --- a/services/distributeddataservice/adapter/communicator/test/BUILD.gn +++ b/services/distributeddataservice/adapter/communicator/test/BUILD.gn @@ -37,6 +37,7 @@ ohos_unittest("CommunicationProviderTest") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/communicator:distributeddata_communicator_static", "//third_party/googletest:gtest_main", ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } ohos_unittest("DeviceManagerAdapterTest") { @@ -62,6 +63,7 @@ ohos_unittest("DeviceManagerAdapterTest") { "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp:devicemanagersdk", "//third_party/googletest:gtest_main", ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } ############################################################################### diff --git a/services/distributeddataservice/adapter/dfx/BUILD.gn b/services/distributeddataservice/adapter/dfx/BUILD.gn index 97d2ebde..bbffd1ac 100644 --- a/services/distributeddataservice/adapter/dfx/BUILD.gn +++ b/services/distributeddataservice/adapter/dfx/BUILD.gn @@ -54,4 +54,5 @@ ohos_static_library("distributeddata_dfx_static") { ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/adapter/dfx/test/BUILD.gn b/services/distributeddataservice/adapter/dfx/test/BUILD.gn index 3ae42016..0c2a6050 100755 --- a/services/distributeddataservice/adapter/dfx/test/BUILD.gn +++ b/services/distributeddataservice/adapter/dfx/test/BUILD.gn @@ -52,6 +52,7 @@ ohos_unittest("DistributeddataDfxMSTTest") { "//third_party/googletest:gtest_main", "//third_party/openssl:libcrypto_shared", ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } ############################################################################### @@ -109,6 +110,7 @@ ohos_unittest("DistributeddataDfxUTTest") { "//third_party/googletest:gtest_main", "//third_party/openssl:libcrypto_shared", ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } ############################################################################### diff --git a/services/distributeddataservice/adapter/permission/BUILD.gn b/services/distributeddataservice/adapter/permission/BUILD.gn index c6f96a2a..9d63f401 100644 --- a/services/distributeddataservice/adapter/permission/BUILD.gn +++ b/services/distributeddataservice/adapter/permission/BUILD.gn @@ -39,4 +39,5 @@ ohos_static_library("distributeddata_permission_static") { ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/adapter/permission/test/BUILD.gn b/services/distributeddataservice/adapter/permission/test/BUILD.gn index 0c859758..a19f4c30 100644 --- a/services/distributeddataservice/adapter/permission/test/BUILD.gn +++ b/services/distributeddataservice/adapter/permission/test/BUILD.gn @@ -43,6 +43,7 @@ ohos_unittest("PermissionValidatorTest") { ] external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } group("unittest") { diff --git a/services/distributeddataservice/adapter/utils/BUILD.gn b/services/distributeddataservice/adapter/utils/BUILD.gn index cd85ee93..6539b7ab 100755 --- a/services/distributeddataservice/adapter/utils/BUILD.gn +++ b/services/distributeddataservice/adapter/utils/BUILD.gn @@ -39,4 +39,5 @@ ohos_static_library("distributeddata_utils_static") { ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index d4f0e710..39d24c52 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -144,4 +144,5 @@ ohos_shared_library("distributeddataservice") { subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/app/src/checker/BUILD.gn b/services/distributeddataservice/app/src/checker/BUILD.gn index 6bf706d9..48afce56 100644 --- a/services/distributeddataservice/app/src/checker/BUILD.gn +++ b/services/distributeddataservice/app/src/checker/BUILD.gn @@ -46,4 +46,5 @@ ohos_static_library("distributeddata_checker_static") { ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn b/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn index cf921d03..c17ecd1e 100755 --- a/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn +++ b/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn @@ -31,4 +31,5 @@ ohos_static_library("distributeddata_flowctrl_static") { subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/app/src/uninstaller/BUILD.gn b/services/distributeddataservice/app/src/uninstaller/BUILD.gn index a2905d66..26b64f9d 100755 --- a/services/distributeddataservice/app/src/uninstaller/BUILD.gn +++ b/services/distributeddataservice/app/src/uninstaller/BUILD.gn @@ -57,4 +57,5 @@ ohos_static_library("distributeddata_uninstaller_static") { ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index d081b35a..87d66c6d 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -58,6 +58,7 @@ config("module_private_config") { [ "//base/powermgr/power_manager/interfaces/innerkits/native/include" ] } ldflags = [ "-Wl,--whole-archive" ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } ohos_unittest("KvStoreDataServiceTest") { diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index 20b957ba..d22d731b 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -68,4 +68,5 @@ ohos_shared_library("distributeddatasvcfwk") { subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/framework/test/BUILD.gn b/services/distributeddataservice/framework/test/BUILD.gn index 006e0bd0..1e2ccfa5 100644 --- a/services/distributeddataservice/framework/test/BUILD.gn +++ b/services/distributeddataservice/framework/test/BUILD.gn @@ -24,6 +24,7 @@ config("module_private_config") { "../../service/bootstrap/include/", ] ldflags = [ "-Wl,--whole-archive" ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } ohos_unittest("CheckerManagerTest") { diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index b1b15a8a..e6116f80 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -134,4 +134,5 @@ ohos_shared_library("distributeddatasvc") { subsystem_name = "distributeddatamgr" part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index f334bc90..70ad6fdd 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -29,7 +29,10 @@ config("module_private_config") { "../../framework/include/", ] - defines = [ "TEST_ON_DEVICE" ] + defines = [ + "TEST_ON_DEVICE", + "OPENSSL_SUPPRESS_DEPRECATED", + ] } ohos_unittest("ConfigFactoryTest") { diff --git a/services/distributeddataservice/test/BUILD.gn b/services/distributeddataservice/test/BUILD.gn index ae66f36b..91743fb8 100755 --- a/services/distributeddataservice/test/BUILD.gn +++ b/services/distributeddataservice/test/BUILD.gn @@ -58,6 +58,7 @@ config("module_private_config") { "RELEASE_MODE_V2", "RELEASE_MODE_V3", "OMIT_MULTI_VER", + "OPENSSL_SUPPRESS_DEPRECATED", ] ldflags = [ "-Wl,--exclude-libs,ALL" ] } diff --git a/services/distributeddataservice/test/fuzztest/schemaquery_fuzzer/BUILD.gn b/services/distributeddataservice/test/fuzztest/schemaquery_fuzzer/BUILD.gn index 57cdc683..d2236940 100644 --- a/services/distributeddataservice/test/fuzztest/schemaquery_fuzzer/BUILD.gn +++ b/services/distributeddataservice/test/fuzztest/schemaquery_fuzzer/BUILD.gn @@ -64,6 +64,7 @@ ohos_fuzztest("SchemaQueryFuzzTest") { "OMIT_FLATBUFFER", "RELATIONAL_STORE", "SQLITE_DISTRIBUTE_RELATIONAL", + "OPENSSL_SUPPRESS_DEPRECATED", ] external_deps = [ diff --git a/test/fuzztest/autolaunch_fuzzer/BUILD.gn b/test/fuzztest/autolaunch_fuzzer/BUILD.gn index 70d15988..fb3e4402 100644 --- a/test/fuzztest/autolaunch_fuzzer/BUILD.gn +++ b/test/fuzztest/autolaunch_fuzzer/BUILD.gn @@ -78,6 +78,7 @@ ohos_fuzztest("AutoLaunchFuzzTest") { "OMIT_FLATBUFFER", "RELATIONAL_STORE", "SQLITE_DISTRIBUTE_RELATIONAL", + "OPENSSL_SUPPRESS_DEPRECATED", ] deps = [ diff --git a/test/fuzztest/blob_fuzzer/BUILD.gn b/test/fuzztest/blob_fuzzer/BUILD.gn index 7396013a..13f9ee66 100644 --- a/test/fuzztest/blob_fuzzer/BUILD.gn +++ b/test/fuzztest/blob_fuzzer/BUILD.gn @@ -48,6 +48,7 @@ ohos_fuzztest("BlobFuzzTest") { "ipc:ipc_core", "kv_store:distributeddata_inner", ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } ############################################################################### diff --git a/test/fuzztest/devicekvstore_fuzzer/BUILD.gn b/test/fuzztest/devicekvstore_fuzzer/BUILD.gn index 577a1e34..e8309cc7 100644 --- a/test/fuzztest/devicekvstore_fuzzer/BUILD.gn +++ b/test/fuzztest/devicekvstore_fuzzer/BUILD.gn @@ -51,6 +51,7 @@ ohos_fuzztest("DeviceKvStoreFuzzTest") { "OMIT_FLATBUFFER", "RELATIONAL_STORE", "SQLITE_DISTRIBUTE_RELATIONAL", + "OPENSSL_SUPPRESS_DEPRECATED", ] deps = [ diff --git a/test/fuzztest/distributedkvdatamanager_fuzzer/BUILD.gn b/test/fuzztest/distributedkvdatamanager_fuzzer/BUILD.gn index 2ff80cab..583476d9 100644 --- a/test/fuzztest/distributedkvdatamanager_fuzzer/BUILD.gn +++ b/test/fuzztest/distributedkvdatamanager_fuzzer/BUILD.gn @@ -51,6 +51,7 @@ ohos_fuzztest("DistributedKvDataManagerFuzzTest") { "OMIT_FLATBUFFER", "RELATIONAL_STORE", "SQLITE_DISTRIBUTE_RELATIONAL", + "OPENSSL_SUPPRESS_DEPRECATED", ] deps = [ diff --git a/test/fuzztest/kvstoredisksize_fuzzer/BUILD.gn b/test/fuzztest/kvstoredisksize_fuzzer/BUILD.gn index fd7b73ab..9ee8840f 100644 --- a/test/fuzztest/kvstoredisksize_fuzzer/BUILD.gn +++ b/test/fuzztest/kvstoredisksize_fuzzer/BUILD.gn @@ -79,6 +79,7 @@ ohos_fuzztest("KvStoreDiskSizeFuzzTest") { "OMIT_FLATBUFFER", "RELATIONAL_STORE", "SQLITE_DISTRIBUTE_RELATIONAL", + "OPENSSL_SUPPRESS_DEPRECATED", ] deps = [ diff --git a/test/fuzztest/singlekvstore_fuzzer/BUILD.gn b/test/fuzztest/singlekvstore_fuzzer/BUILD.gn index 76b4c5da..0536e820 100644 --- a/test/fuzztest/singlekvstore_fuzzer/BUILD.gn +++ b/test/fuzztest/singlekvstore_fuzzer/BUILD.gn @@ -51,6 +51,7 @@ ohos_fuzztest("SingleKvStoreFuzzTest") { "OMIT_FLATBUFFER", "RELATIONAL_STORE", "SQLITE_DISTRIBUTE_RELATIONAL", + "OPENSSL_SUPPRESS_DEPRECATED", ] deps = [ diff --git a/test/fuzztest/taskscheduler_fuzzer/BUILD.gn b/test/fuzztest/taskscheduler_fuzzer/BUILD.gn index 0f37df0c..40c48e26 100644 --- a/test/fuzztest/taskscheduler_fuzzer/BUILD.gn +++ b/test/fuzztest/taskscheduler_fuzzer/BUILD.gn @@ -37,6 +37,7 @@ ohos_fuzztest("TaskSchedulerFuzzTest") { deps = [] external_deps = [ "c_utils:utils" ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } ############################################################################### diff --git a/test/fuzztest/typesutil_fuzzer/BUILD.gn b/test/fuzztest/typesutil_fuzzer/BUILD.gn index f4b39c11..d181ca75 100644 --- a/test/fuzztest/typesutil_fuzzer/BUILD.gn +++ b/test/fuzztest/typesutil_fuzzer/BUILD.gn @@ -49,6 +49,7 @@ ohos_fuzztest("TypesUtilFuzzTest") { "ipc:ipc_core", "kv_store:distributeddata_inner", ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } ############################################################################### -- Gitee From 6a2022db3165f6ab14fb718bcb718d37cbf59bce Mon Sep 17 00:00:00 2001 From: niudongyao Date: Sun, 5 Mar 2023 11:50:18 +0800 Subject: [PATCH 002/152] fix Signed-off-by: niudongyao --- .../data_share/data_share_profile_info.cpp | 56 +++++++++---------- .../data_share/data_share_profile_info.h | 8 +-- .../service/data_share/permission_proxy.cpp | 7 ++- .../service/data_share/uri_utils.cpp | 2 +- 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/services/distributeddataservice/service/data_share/data_share_profile_info.cpp b/services/distributeddataservice/service/data_share/data_share_profile_info.cpp index 329b9499..88574c2c 100644 --- a/services/distributeddataservice/service/data_share/data_share_profile_info.cpp +++ b/services/distributeddataservice/service/data_share/data_share_profile_info.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #define LOG_TAG "DataShareProfileInfo" #include "data_share_profile_info.h" @@ -57,9 +57,7 @@ bool ProfileInfo::Marshal(json &node) const bool ProfileInfo::Unmarshal(const json &node) { - bool ret = true; - ret = GetValue(node, GET_NAME(tableConfig), tableConfig) && ret; - return ret; + return GetValue(node, GET_NAME(tableConfig), tableConfig); } bool DataShareProfileInfo::GetProfileInfoFromExtension(const AppExecFwk::BundleInfo &bundleInfo, @@ -71,15 +69,15 @@ bool DataShareProfileInfo::GetProfileInfoFromExtension(const AppExecFwk::BundleI return true; } - for (auto &item : bundleInfo.extensionInfos) { + for (auto const &item : bundleInfo.extensionInfos) { if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { - std::vector infos; + bool isCompressed = !item.hapPath.empty(); std::string resourcePath = isCompressed ? item.hapPath : item.resourcePath; - if (!GetResProfileByMetadata(item.metadata, resourcePath, isCompressed, infos) || infos.empty()) { - ZLOGE("failed, bundleName is %{public}s, resourcePath is %{public}s, metadata.size is %{public}zu," - "infos.size is %{public}zu", bundleInfo.name.c_str(), resourcePath.c_str(), item.metadata.size(), - infos.size()); + std::vector infos = GetResProfileByMetadata(item.metadata, resourcePath, isCompressed); + if (infos.empty()) { + ZLOGE("failed, bundleName is %{public}s, resourcePath is %{public}s, metadata.size is %{public}zu,", + bundleInfo.name.c_str(), resourcePath.c_str(), item.metadata.size()); return false; } return profileInfo.Unmarshall(infos[0]); @@ -89,23 +87,24 @@ bool DataShareProfileInfo::GetProfileInfoFromExtension(const AppExecFwk::BundleI return false; } -bool DataShareProfileInfo::GetResProfileByMetadata(const std::vector &metadata, - const std::string &resourcePath, bool isCompressed, std::vector &profileInfos) const +std::vector DataShareProfileInfo::GetResProfileByMetadata( + const std::vector &metadata, const std::string &resourcePath, bool isCompressed) const { + std::vector profileInfos; if (metadata.empty() || resourcePath.empty()) { - return false; + return profileInfos; } std::shared_ptr resMgr = InitResMgr(resourcePath); if (resMgr == nullptr) { - return false; + return profileInfos; } - for (auto &meta : metadata) { + for (auto const &meta : metadata) { if (meta.name.compare(DATA_SHARE_PROFILE_META) == 0) { - return GetResFromResMgr(meta.resource, *resMgr, isCompressed, profileInfos); + return GetResFromResMgr(meta.resource, *resMgr, isCompressed); } } - return false; + return profileInfos; } std::shared_ptr DataShareProfileInfo::InitResMgr(const std::string &resourcePath) const @@ -128,17 +127,18 @@ std::shared_ptr DataShareProfileInfo::InitResMgr(const std::str return resMgr; } -bool DataShareProfileInfo::GetResFromResMgr(const std::string &resName, ResourceManager &resMgr, - bool isCompressed, std::vector &profileInfos) const +std::vector DataShareProfileInfo::GetResFromResMgr(const std::string &resName, ResourceManager &resMgr, + bool isCompressed) const { + std::vector profileInfos; if (resName.empty()) { - return false; + return profileInfos; } size_t pos = resName.rfind(PROFILE_FILE_PREFIX); if ((pos == std::string::npos) || (pos == resName.length() - PROFILE_PREFIX_LEN)) { ZLOGE("res name invalid, resName is %{public}s", resName.c_str()); - return false; + return profileInfos; } std::string profileName = resName.substr(pos + PROFILE_PREFIX_LEN); // hap is compressed status, get file content. @@ -150,34 +150,34 @@ bool DataShareProfileInfo::GetResFromResMgr(const std::string &resName, Resource if (ret != SUCCESS || fileContent == nullptr) { ZLOGE("failed, ret is %{public}d, profileName is %{public}s", ret, profileName.c_str()); - return false; + return profileInfos; } if (len == 0) { ZLOGE("fileContent is empty, profileName is %{public}s", profileName.c_str()); - return false; + return profileInfos; } std::string rawData(fileContent.get(), fileContent.get() + len); if (!Config::IsJson(rawData)) { ZLOGE("rawData is not json, profileName is %{public}s", profileName.c_str()); - return false; + return profileInfos; } profileInfos.push_back(std::move(rawData)); - return true; + return profileInfos; } // hap is decompressed status, get file path then read file. std::string resPath; RState ret = resMgr.GetProfileByName(profileName.c_str(), resPath); if (ret != SUCCESS) { ZLOGE("profileName not found, ret is %{public}d, profileName is %{public}s", ret, profileName.c_str()); - return false; + return profileInfos; } std::string profile = ReadProfile(resPath); if (profile.empty()) { ZLOGE("Read profile failed, resPath is %{public}s", resPath.c_str()); - return false; + return profileInfos; } profileInfos.push_back(std::move(profile)); - return true; + return profileInfos; } bool DataShareProfileInfo::IsFileExisted(const std::string &filePath) const diff --git a/services/distributeddataservice/service/data_share/data_share_profile_info.h b/services/distributeddataservice/service/data_share/data_share_profile_info.h index 9dac5236..b943aaf1 100644 --- a/services/distributeddataservice/service/data_share/data_share_profile_info.h +++ b/services/distributeddataservice/service/data_share/data_share_profile_info.h @@ -45,11 +45,11 @@ public: ProfileInfo &profileInfo, bool &isSingleApp); private: - bool GetResProfileByMetadata(const std::vector &metadata, const std::string &resourcePath, - bool isCompressed, std::vector &profileInfos) const; + std::vector GetResProfileByMetadata(const std::vector &metadata, + const std::string &resourcePath, bool isCompressed) const; std::shared_ptr InitResMgr(const std::string &basicString) const; - bool GetResFromResMgr(const std::string &resName, ResourceManager &resMgr, bool isCompressed, - std::vector &profileInfos) const; + std::vector GetResFromResMgr(const std::string &resName, ResourceManager &resMgr, + bool isCompressed) const; std::string ReadProfile(const std::string &resPath) const; bool IsFileExisted(const std::string &filePath) const; }; diff --git a/services/distributeddataservice/service/data_share/permission_proxy.cpp b/services/distributeddataservice/service/data_share/permission_proxy.cpp index 91ea2b97..b37bf4f6 100644 --- a/services/distributeddataservice/service/data_share/permission_proxy.cpp +++ b/services/distributeddataservice/service/data_share/permission_proxy.cpp @@ -40,7 +40,7 @@ bool PermissionProxy::GetBundleInfo(const std::string &bundleName, uint32_t toke PermissionProxy::PermissionState PermissionProxy::QueryWritePermission(uint32_t tokenId, std::string &permission, const AppExecFwk::BundleInfo &bundleInfo) { - for (auto &item : bundleInfo.extensionInfos) { + for (auto const &item : bundleInfo.extensionInfos) { if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { permission = item.writePermission; if (permission.empty()) { @@ -62,9 +62,10 @@ PermissionProxy::PermissionState PermissionProxy::QueryWritePermission(uint32_t PermissionProxy::PermissionState PermissionProxy::QueryReadPermission(uint32_t tokenId, std::string &permission, const AppExecFwk::BundleInfo &bundleInfo) { - for (auto &item : bundleInfo.extensionInfos) { + for (auto const &item : bundleInfo.extensionInfos) { if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { - if (item.readPermission.empty()) { + permission = item.readPermission; + if (permission.empty()) { ZLOGW("ReadPermission is empty! BundleName is %{public}s, tokenId is %{public}x", bundleInfo.name.c_str(), tokenId); return PermissionState::NOT_FIND; diff --git a/services/distributeddataservice/service/data_share/uri_utils.cpp b/services/distributeddataservice/service/data_share/uri_utils.cpp index a4e8a82a..6d3df793 100644 --- a/services/distributeddataservice/service/data_share/uri_utils.cpp +++ b/services/distributeddataservice/service/data_share/uri_utils.cpp @@ -52,7 +52,7 @@ bool URIUtils::IsValidPath(const std::vector &splitUri, bool tableN } if (!tableNameEmpty) { - if (splitUri.size() < URI_INDEX_MAX) { + if (splitUri.size() < URI_INDEX_TABLENAME + 1) { ZLOGE("Uri need contains tableName"); return false; } -- Gitee From a0cecff7bf32cf89ca8b1bc6898c6516c375fba8 Mon Sep 17 00:00:00 2001 From: niudongyao Date: Sun, 5 Mar 2023 11:50:48 +0800 Subject: [PATCH 003/152] fix Signed-off-by: niudongyao --- .../service/data_share/data_share_profile_info.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/data_share_profile_info.cpp b/services/distributeddataservice/service/data_share/data_share_profile_info.cpp index 88574c2c..2b286378 100644 --- a/services/distributeddataservice/service/data_share/data_share_profile_info.cpp +++ b/services/distributeddataservice/service/data_share/data_share_profile_info.cpp @@ -71,7 +71,6 @@ bool DataShareProfileInfo::GetProfileInfoFromExtension(const AppExecFwk::BundleI for (auto const &item : bundleInfo.extensionInfos) { if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { - bool isCompressed = !item.hapPath.empty(); std::string resourcePath = isCompressed ? item.hapPath : item.resourcePath; std::vector infos = GetResProfileByMetadata(item.metadata, resourcePath, isCompressed); -- Gitee From c8bc76df6e0680363d2d47a1cde2154291223bfe Mon Sep 17 00:00:00 2001 From: niudongyao Date: Wed, 8 Mar 2023 16:33:29 +0800 Subject: [PATCH 004/152] modify comments Signed-off-by: niudongyao --- .../data_share/data_share_service_impl.cpp | 5 ++--- .../service/data_share/permission_proxy.cpp | 14 ++++++------- .../service/data_share/permission_proxy.h | 6 ++---- .../service/data_share/uri_utils.cpp | 20 +++++++++---------- .../service/data_share/uri_utils.h | 13 ++++++------ 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 63c9fd3f..4fc2ca94 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -212,13 +212,12 @@ std::string DataShareServiceImpl::GetRealityTableName(uint32_t tokenId, const Ap PermissionProxy::PermissionState DataShareServiceImpl::VerifyPermission(uint32_t tokenID, DataShareServiceImpl::PermissionType permissionType, const AppExecFwk::BundleInfo &bundleInfo) { - std::string permission; switch (permissionType) { case PermissionType::READ_PERMISSION: { - return PermissionProxy::QueryReadPermission(tokenID, permission, bundleInfo); + return PermissionProxy::QueryReadPermission(tokenID, bundleInfo); } case PermissionType::WRITE_PERMISSION: { - return PermissionProxy::QueryWritePermission(tokenID, permission, bundleInfo); + return PermissionProxy::QueryWritePermission(tokenID, bundleInfo); } } return PermissionProxy::PermissionState::NOT_FIND; diff --git a/services/distributeddataservice/service/data_share/permission_proxy.cpp b/services/distributeddataservice/service/data_share/permission_proxy.cpp index b37bf4f6..76806bfa 100644 --- a/services/distributeddataservice/service/data_share/permission_proxy.cpp +++ b/services/distributeddataservice/service/data_share/permission_proxy.cpp @@ -38,17 +38,16 @@ bool PermissionProxy::GetBundleInfo(const std::string &bundleName, uint32_t toke } PermissionProxy::PermissionState PermissionProxy::QueryWritePermission(uint32_t tokenId, - std::string &permission, const AppExecFwk::BundleInfo &bundleInfo) + const AppExecFwk::BundleInfo &bundleInfo) { for (auto const &item : bundleInfo.extensionInfos) { if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { - permission = item.writePermission; - if (permission.empty()) { + if (item.writePermission.empty()) { ZLOGW("WritePermission is empty! BundleName is %{public}s, tokenId is %{public}x", bundleInfo.name.c_str(), tokenId); return PermissionState::NOT_FIND; } - int status = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, permission); + int status = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, item.writePermission); if (status != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { ZLOGE("Verify write permission denied!"); return PermissionState::DENIED; @@ -60,17 +59,16 @@ PermissionProxy::PermissionState PermissionProxy::QueryWritePermission(uint32_t } PermissionProxy::PermissionState PermissionProxy::QueryReadPermission(uint32_t tokenId, - std::string &permission, const AppExecFwk::BundleInfo &bundleInfo) + const AppExecFwk::BundleInfo &bundleInfo) { for (auto const &item : bundleInfo.extensionInfos) { if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { - permission = item.readPermission; - if (permission.empty()) { + if (item.readPermission.empty()) { ZLOGW("ReadPermission is empty! BundleName is %{public}s, tokenId is %{public}x", bundleInfo.name.c_str(), tokenId); return PermissionState::NOT_FIND; } - int status = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, permission); + int status = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, item.readPermission); if (status != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { ZLOGE("Verify Read permission denied!"); return PermissionState::DENIED; diff --git a/services/distributeddataservice/service/data_share/permission_proxy.h b/services/distributeddataservice/service/data_share/permission_proxy.h index 1d17f321..1c6d4c78 100644 --- a/services/distributeddataservice/service/data_share/permission_proxy.h +++ b/services/distributeddataservice/service/data_share/permission_proxy.h @@ -39,10 +39,8 @@ public: }; static bool GetBundleInfo(const std::string &bundleName, uint32_t tokenId, AppExecFwk::BundleInfo &bundleInfo); - static PermissionState QueryWritePermission(uint32_t tokenId, - std::string &permission, const AppExecFwk::BundleInfo &bundleInfo); - static PermissionState QueryReadPermission(uint32_t tokenId, - std::string &permission, const AppExecFwk::BundleInfo &bundleInfo); + static PermissionState QueryWritePermission(uint32_t tokenId, const AppExecFwk::BundleInfo &bundleInfo); + static PermissionState QueryReadPermission(uint32_t tokenId, const AppExecFwk::BundleInfo &bundleInfo); static bool QueryMetaData(const std::string &bundleName, const std::string &storeName, DistributedData::StoreMetaData &metaData, int32_t userId); static std::string GetTableNameByCrossUserMode(const ProfileInfo &profileInfo, diff --git a/services/distributeddataservice/service/data_share/uri_utils.cpp b/services/distributeddataservice/service/data_share/uri_utils.cpp index 6d3df793..19bc64f4 100644 --- a/services/distributeddataservice/service/data_share/uri_utils.cpp +++ b/services/distributeddataservice/service/data_share/uri_utils.cpp @@ -31,32 +31,32 @@ bool URIUtils::GetInfoFromURI(const std::string &uri, UriInfo &uriInfo, bool tab return false; } - uriInfo.bundleName = splitUri[URI_INDEX_BUNLDENAME]; - uriInfo.moduleName = splitUri[URI_INDEX_MODULENAME]; - uriInfo.storeName = splitUri[URI_INDEX_STORENAME]; - if (splitUri.size() > URI_INDEX_MIN) { - uriInfo.tableName = splitUri[URI_INDEX_TABLENAME]; + uriInfo.bundleName = splitUri[BUNDLE_NAME]; + uriInfo.moduleName = splitUri[MODULE_NAME]; + uriInfo.storeName = splitUri[STORE_NAME]; + if (splitUri.size() > TABLE_NAME) { + uriInfo.tableName = splitUri[TABLE_NAME]; } return true; } bool URIUtils::IsValidPath(const std::vector &splitUri, bool tableNameEmpty) { - if (splitUri.size() < URI_INDEX_MIN) { + if (splitUri.size() < TABLE_NAME) { return false; } - if (splitUri[URI_INDEX_BUNLDENAME].empty() || splitUri[URI_INDEX_MODULENAME].empty() || - splitUri[URI_INDEX_STORENAME].empty()) { + if (splitUri[BUNDLE_NAME].empty() || splitUri[MODULE_NAME].empty() || + splitUri[STORE_NAME].empty()) { ZLOGE("Uri has empty field!"); return false; } if (!tableNameEmpty) { - if (splitUri.size() < URI_INDEX_TABLENAME + 1) { + if (splitUri.size() < PARAM_BUTT) { ZLOGE("Uri need contains tableName"); return false; } - if (splitUri[URI_INDEX_TABLENAME].empty()) { + if (splitUri[TABLE_NAME].empty()) { ZLOGE("Uri tableName can't be empty!"); return false; } diff --git a/services/distributeddataservice/service/data_share/uri_utils.h b/services/distributeddataservice/service/data_share/uri_utils.h index 99a2e1b6..f65deb50 100644 --- a/services/distributeddataservice/service/data_share/uri_utils.h +++ b/services/distributeddataservice/service/data_share/uri_utils.h @@ -38,12 +38,13 @@ public: private: static bool IsValidPath(const std::vector &splitUri, bool tableNameEmpty); - static constexpr size_t URI_INDEX_BUNLDENAME = 0; - static constexpr size_t URI_INDEX_MODULENAME = 1; - static constexpr size_t URI_INDEX_STORENAME = 2; - static constexpr size_t URI_INDEX_TABLENAME = 3; - static constexpr size_t URI_INDEX_MIN = 3; - static constexpr size_t URI_INDEX_MAX = 4; + enum PATH_PARAM : int32_t { + BUNDLE_NAME = 0, + MODULE_NAME, + STORE_NAME, + TABLE_NAME, + PARAM_BUTT + }; }; } // namespace OHOS::DataShare #endif // DATASHARESERVICE_URI_UTILS_H -- Gitee From fcf9534e26ddf9112273175d608516c47a457d43 Mon Sep 17 00:00:00 2001 From: niudongyao Date: Wed, 8 Mar 2023 17:31:07 +0800 Subject: [PATCH 005/152] modify Signed-off-by: niudongyao --- .../distributeddataservice/service/data_share/uri_utils.cpp | 4 ++-- .../distributeddataservice/service/data_share/uri_utils.h | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/data_share/uri_utils.cpp b/services/distributeddataservice/service/data_share/uri_utils.cpp index 19bc64f4..5a26fc9e 100644 --- a/services/distributeddataservice/service/data_share/uri_utils.cpp +++ b/services/distributeddataservice/service/data_share/uri_utils.cpp @@ -34,7 +34,7 @@ bool URIUtils::GetInfoFromURI(const std::string &uri, UriInfo &uriInfo, bool tab uriInfo.bundleName = splitUri[BUNDLE_NAME]; uriInfo.moduleName = splitUri[MODULE_NAME]; uriInfo.storeName = splitUri[STORE_NAME]; - if (splitUri.size() > TABLE_NAME) { + if (splitUri.size() > OPTIONAL_BEGIN) { uriInfo.tableName = splitUri[TABLE_NAME]; } return true; @@ -42,7 +42,7 @@ bool URIUtils::GetInfoFromURI(const std::string &uri, UriInfo &uriInfo, bool tab bool URIUtils::IsValidPath(const std::vector &splitUri, bool tableNameEmpty) { - if (splitUri.size() < TABLE_NAME) { + if (splitUri.size() < OPTIONAL_BEGIN) { return false; } if (splitUri[BUNDLE_NAME].empty() || splitUri[MODULE_NAME].empty() || diff --git a/services/distributeddataservice/service/data_share/uri_utils.h b/services/distributeddataservice/service/data_share/uri_utils.h index f65deb50..7e2f29d3 100644 --- a/services/distributeddataservice/service/data_share/uri_utils.h +++ b/services/distributeddataservice/service/data_share/uri_utils.h @@ -42,7 +42,8 @@ private: BUNDLE_NAME = 0, MODULE_NAME, STORE_NAME, - TABLE_NAME, + OPTIONAL_BEGIN, + TABLE_NAME = OPTIONAL_BEGIN, PARAM_BUTT }; }; -- Gitee From 7939ab77de375b3520066a36671557d414716702 Mon Sep 17 00:00:00 2001 From: lihuihui Date: Wed, 15 Mar 2023 18:50:44 +0800 Subject: [PATCH 006/152] getrow Signed-off-by: lihuihui --- .../service/rdb/rdb_result_set_impl.cpp | 13 +++++++++++++ .../service/rdb/rdb_result_set_impl.h | 1 + 2 files changed, 14 insertions(+) diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp b/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp index 1be8c197..d2070363 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp @@ -296,6 +296,19 @@ int RdbResultSetImpl::IsColumnNull(int columnIndex, bool &isNull) return NativeRdb::E_OK; } +int RdbResultSetImpl::GetRow(std::map &data) +{ + std::shared_lock lock(this->mutex_); + if (resultSet_ == nullptr) { + return NativeRdb::E_STEP_RESULT_CLOSED; + } + DBStatus status = resultSet_->GetRow(data); + if (status != DBStatus::OK) { + return NativeRdb::E_ERROR; + } + return NativeRdb::E_OK; +} + bool RdbResultSetImpl::IsClosed() const { std::shared_lock lock(this->mutex_); diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h index 63e16f45..a10e465f 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h @@ -49,6 +49,7 @@ public: int GetLong(int columnIndex, int64_t &value) override; int GetDouble(int columnIndex, double &value) override; int IsColumnNull(int columnIndex, bool &isNull) override; + int GetRow(std::map &data) override; bool IsClosed() const override; int Close() override; -- Gitee From 1b0d32f6330d2b68326b484c6c1545837b8636e7 Mon Sep 17 00:00:00 2001 From: shilei Date: Thu, 16 Mar 2023 09:11:51 +0000 Subject: [PATCH 007/152] fix problem Signed-off-by: shilei Change-Id: I9930e5933d2fa43c6c0b86a1b1c3ed8f9255385e --- .../app/src/uninstaller/uninstaller_impl.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index 2ab4cb22..36f24caf 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -18,6 +18,7 @@ #include "uninstaller_impl.h" #include #include +#include "bundle_common_event.h" #include "common_event_manager.h" #include "common_event_support.h" #include "device_manager_adapter.h" @@ -46,7 +47,7 @@ void UninstallEventSubscriber::OnReceiveEvent(const CommonEventData &event) Want want = event.GetWant(); std::string action = want.GetAction(); if (action != CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED && - action != CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) { + action != OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) { return; } @@ -82,7 +83,7 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService) } MatchingSkills matchingSkills; matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED); + matchingSkills.AddEvent(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED); CommonEventSubscribeInfo info(matchingSkills); auto callback = [kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { kvStoreDataService->OnUninstall(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID()); -- Gitee From cc06aee13cd0dc7f0865ec020dc226c59e98b343 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Mon, 20 Mar 2023 21:01:19 +0800 Subject: [PATCH 008/152] init Signed-off-by: htt1997 --- .../backuprule/backup_rule_manager.cpp | 5 ++- .../framework/checker/checker_manager.cpp | 5 ++- .../service/kvdb/user_delegate.cpp | 33 +++++++++++-------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/services/distributeddataservice/framework/backuprule/backup_rule_manager.cpp b/services/distributeddataservice/framework/backuprule/backup_rule_manager.cpp index 2fe05913..483f16ee 100644 --- a/services/distributeddataservice/framework/backuprule/backup_rule_manager.cpp +++ b/services/distributeddataservice/framework/backuprule/backup_rule_manager.cpp @@ -41,7 +41,10 @@ void BackupRuleManager::RegisterPlugin(const std::string &backupRule, std::funct if (it.first) { return; } - getters_[backupRule] = getter; + getters_.Compute(backupRule, [&getter](const auto &key, auto &value) { + value = move(getter); + return true; + }); } bool BackupRuleManager::CanBackup() diff --git a/services/distributeddataservice/framework/checker/checker_manager.cpp b/services/distributeddataservice/framework/checker/checker_manager.cpp index b7213f3c..7b1e7282 100644 --- a/services/distributeddataservice/framework/checker/checker_manager.cpp +++ b/services/distributeddataservice/framework/checker/checker_manager.cpp @@ -46,7 +46,10 @@ void CheckerManager::RegisterPlugin(const std::string &checker, std::function UserDelegate::GetUsers(const std::string &deviceId) if (!deviceUserMap_.Contains(deviceId)) { LoadFromMeta(deviceId); } - for (const auto &entry : deviceUserMap_[deviceId]) { - userStatus.emplace_back(entry.first, entry.second); - } + deviceUserMap_.ComputeIfPresent(deviceId, [&userStatus](const auto &, std::map &userMap) { + std::for_each(userMap.begin(), userMap.end(), [&userStatus](auto key, auto value) { + userStatus.emplace_back(key, value); + }); + return true; + }); + ZLOGI("device:%{public}s, users:%{public}s", Anonymous::Change(deviceId).c_str(), Serializable::Marshall(userStatus).c_str()); return userStatus; @@ -99,14 +103,12 @@ void UserDelegate::UpdateUsers(const std::string &deviceId, const std::vector &userMap) { userMap = {}; - for (auto &user : userStatus) { + std::for_each(userStatus.begin(), userStatus.end(), [&userMap](const auto &user) { userMap[user.id] = user.isActive; - } + }); + ZLOGI("end, device:%{public}.10s, users:%{public}zu", Anonymous::Change(key).c_str(), userMap.size()); return true; }); - - ZLOGI("end, device:%{public}s, users:%{public}zu", Anonymous::Change(deviceId).c_str(), - deviceUserMap_[deviceId].size()); } bool UserDelegate::InitLocalUserMeta() @@ -124,10 +126,12 @@ bool UserDelegate::InitLocalUserMeta() UserMetaData userMetaData; userMetaData.deviceId = GetLocalDeviceId(); UpdateUsers(userMetaData.deviceId, userStatus); - for (auto &pair : deviceUserMap_[userMetaData.deviceId]) { - userMetaData.users.emplace_back(pair.first, pair.second); - } - + deviceUserMap_.ComputeIfPresent(userMetaData.deviceId, [&userMetaData](const auto &, std::map &userMap) { + std::for_each(userMap.begin(), userMap.end(), [&userMetaData](auto key, auto value) { + userMetaData.users.emplace_back(key, value); + }); + return true; + }); ZLOGI("put user meta data save meta data"); return MetaDataManager::GetInstance().SaveMeta(UserMetaRow::GetKeyFor(userMetaData.deviceId), userMetaData); } @@ -140,7 +144,10 @@ void UserDelegate::LoadFromMeta(const std::string &deviceId) for (const auto &user : userMetaData.users) { userMap[user.id] = user.isActive; } - deviceUserMap_[deviceId] = userMap; + deviceUserMap_.Compute(deviceId, [&userMap](const auto &, auto &value) { + value = userMap; + return true; + }); } UserDelegate &UserDelegate::GetInstance() -- Gitee From c308484b23bda7a5d4bf0c183d690a70682d1a9a Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 21 Mar 2023 08:59:02 +0800 Subject: [PATCH 009/152] update Signed-off-by: htt1997 --- .../distributeddataservice/service/kvdb/user_delegate.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/user_delegate.cpp b/services/distributeddataservice/service/kvdb/user_delegate.cpp index 91379de5..cfce3523 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.cpp +++ b/services/distributeddataservice/service/kvdb/user_delegate.cpp @@ -82,9 +82,9 @@ std::vector UserDelegate::GetUsers(const std::string &deviceId) LoadFromMeta(deviceId); } deviceUserMap_.ComputeIfPresent(deviceId, [&userStatus](const auto &, std::map &userMap) { - std::for_each(userMap.begin(), userMap.end(), [&userStatus](auto key, auto value) { + for (const auto &[key, value] : userMap) { userStatus.emplace_back(key, value); - }); + } return true; }); @@ -127,9 +127,9 @@ bool UserDelegate::InitLocalUserMeta() userMetaData.deviceId = GetLocalDeviceId(); UpdateUsers(userMetaData.deviceId, userStatus); deviceUserMap_.ComputeIfPresent(userMetaData.deviceId, [&userMetaData](const auto &, std::map &userMap) { - std::for_each(userMap.begin(), userMap.end(), [&userMetaData](auto key, auto value) { + for (const auto &[key, value] : userMap) { userMetaData.users.emplace_back(key, value); - }); + } return true; }); ZLOGI("put user meta data save meta data"); -- Gitee From b5b719b165bc45052b9c0caa7f1d8e71d69459d7 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 21 Mar 2023 09:14:20 +0800 Subject: [PATCH 010/152] update Signed-off-by: htt1997 --- .../distributeddataservice/service/kvdb/user_delegate.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/user_delegate.cpp b/services/distributeddataservice/service/kvdb/user_delegate.cpp index cfce3523..686bee77 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.cpp +++ b/services/distributeddataservice/service/kvdb/user_delegate.cpp @@ -103,9 +103,9 @@ void UserDelegate::UpdateUsers(const std::string &deviceId, const std::vector &userMap) { userMap = {}; - std::for_each(userStatus.begin(), userStatus.end(), [&userMap](const auto &user) { + for (const auto &user : userStatus) { userMap[user.id] = user.isActive; - }); + } ZLOGI("end, device:%{public}.10s, users:%{public}zu", Anonymous::Change(key).c_str(), userMap.size()); return true; }); -- Gitee From 720216a71e8f741e610c5663a634dfe8eab21fee Mon Sep 17 00:00:00 2001 From: lihuihui Date: Wed, 22 Mar 2023 09:48:07 +0800 Subject: [PATCH 011/152] getrow Signed-off-by: lihuihui --- .../service/rdb/rdb_result_set_impl.cpp | 10 +--------- .../service/rdb/rdb_result_set_impl.h | 3 ++- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp b/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp index d2070363..ec001c60 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp @@ -296,16 +296,8 @@ int RdbResultSetImpl::IsColumnNull(int columnIndex, bool &isNull) return NativeRdb::E_OK; } -int RdbResultSetImpl::GetRow(std::map &data) +int RdbResultSetImpl::GetRow(NativeRdb::ValuesBucket &valuesBucket) { - std::shared_lock lock(this->mutex_); - if (resultSet_ == nullptr) { - return NativeRdb::E_STEP_RESULT_CLOSED; - } - DBStatus status = resultSet_->GetRow(data); - if (status != DBStatus::OK) { - return NativeRdb::E_ERROR; - } return NativeRdb::E_OK; } diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h index a10e465f..7273781f 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h @@ -19,6 +19,7 @@ #include #include "rdb_result_set_stub.h" #include "distributeddb/result_set.h" +#include "values_bucket.h" namespace OHOS::DistributedRdb { class RdbResultSetImpl final : public RdbResultSetStub { @@ -49,7 +50,7 @@ public: int GetLong(int columnIndex, int64_t &value) override; int GetDouble(int columnIndex, double &value) override; int IsColumnNull(int columnIndex, bool &isNull) override; - int GetRow(std::map &data) override; + int GetRow(NativeRdb::ValuesBucket &valuesBucket) override; bool IsClosed() const override; int Close() override; -- Gitee From eb9712521551d607a8c5567a16ac095248c4d91f Mon Sep 17 00:00:00 2001 From: renjiecui Date: Wed, 22 Mar 2023 11:18:23 +0800 Subject: [PATCH 012/152] fix testcase Signed-off-by: renjiecui --- .../app/test/unittest/session_manager_test.cpp | 2 +- .../framework/test/serializable_test.cpp | 14 -------------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/services/distributeddataservice/app/test/unittest/session_manager_test.cpp b/services/distributeddataservice/app/test/unittest/session_manager_test.cpp index f2994fdc..4d6d7bd7 100644 --- a/services/distributeddataservice/app/test/unittest/session_manager_test.cpp +++ b/services/distributeddataservice/app/test/unittest/session_manager_test.cpp @@ -121,7 +121,7 @@ HWTEST_F(SessionManagerTest, PackAndUnPack01, TestSize.Level2) std::vector users; auto recvHandler = RouteHeadHandlerImpl::Create({}); ASSERT_NE(recvHandler, nullptr); - uint32_t parseSize = 0; + uint32_t parseSize = 1; recvHandler->ParseHeadData(data.get(), routeHeadSize, parseSize, users); EXPECT_EQ(routeHeadSize, parseSize); ASSERT_EQ(users.size(), 1); diff --git a/services/distributeddataservice/framework/test/serializable_test.cpp b/services/distributeddataservice/framework/test/serializable_test.cpp index 55364fd9..0f767026 100644 --- a/services/distributeddataservice/framework/test/serializable_test.cpp +++ b/services/distributeddataservice/framework/test/serializable_test.cpp @@ -126,20 +126,6 @@ HWTEST_F(SerializableTest, GetNormalVal, TestSize.Level2) ASSERT_TRUE(normal == normal1) << normal1.name; } -/** -* @tc.name: Delete Serializable -* @tc.desc: can delete child class, but not delete parent class point. -* @tc.type: FUNC -* @tc.require: -* @tc.author: Sven Wang -*/ -HWTEST_F(SerializableTest, DeleteSerializable, TestSize.Level2) -{ - ZLOGI("SerializableSuite GetVal begin."); - NormalEx *normalEx = new NormalEx(); - delete normalEx; -} - /** * @tc.name: SerializableSuiteGetMutilVal * @tc.desc: mutil value case. -- Gitee From 45d97ec0395bfaad2192f00b83244f6e4861d18f Mon Sep 17 00:00:00 2001 From: dboy190 Date: Wed, 22 Mar 2023 15:27:40 +0800 Subject: [PATCH 013/152] add udmf into ddms process Signed-off-by: dboy190 --- conf/config.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/conf/config.json b/conf/config.json index 91ca003b..2bf2041c 100644 --- a/conf/config.json +++ b/conf/config.json @@ -16,6 +16,9 @@ }, { "lib": "libconfigdemo2.z.so" + }, + { + "lib": "libudmf_server.z.so" } ], "bundleChecker": { -- Gitee From a852c8500c5de800a3d5c6304ccb0c071ca5d628 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Thu, 23 Mar 2023 16:33:30 +0800 Subject: [PATCH 014/152] update Signed-off-by: htt1997 --- .../backuprule/backup_rule_manager.cpp | 9 +--- .../framework/checker/checker_manager.cpp | 9 +--- .../service/kvdb/user_delegate.cpp | 51 ++++++++----------- .../service/kvdb/user_delegate.h | 1 - 4 files changed, 26 insertions(+), 44 deletions(-) diff --git a/services/distributeddataservice/framework/backuprule/backup_rule_manager.cpp b/services/distributeddataservice/framework/backuprule/backup_rule_manager.cpp index 483f16ee..22dc48a6 100644 --- a/services/distributeddataservice/framework/backuprule/backup_rule_manager.cpp +++ b/services/distributeddataservice/framework/backuprule/backup_rule_manager.cpp @@ -37,13 +37,8 @@ void BackupRuleManager::LoadBackupRules(const std::vector &backupRu void BackupRuleManager::RegisterPlugin(const std::string &backupRule, std::function getter) { - auto it = getters_.Find(backupRule); - if (it.first) { - return; - } - getters_.Compute(backupRule, [&getter](const auto &key, auto &value) { - value = move(getter); - return true; + getters_.ComputeIfAbsent(backupRule, [&getter](const auto &) { + return move(getter); }); } diff --git a/services/distributeddataservice/framework/checker/checker_manager.cpp b/services/distributeddataservice/framework/checker/checker_manager.cpp index 7b1e7282..825d9404 100644 --- a/services/distributeddataservice/framework/checker/checker_manager.cpp +++ b/services/distributeddataservice/framework/checker/checker_manager.cpp @@ -42,13 +42,8 @@ void CheckerManager::LoadCheckers(std::vector &checkers) void CheckerManager::RegisterPlugin(const std::string &checker, std::function getter) { - auto it = getters_.Find(checker); - if (it.first) { - return; - } - getters_.Compute(checker, [&getter](const auto &key, auto &value) { - value = move(getter); - return true; + getters_.ComputeIfAbsent(checker, [&getter](const auto &) { + return move(getter); }); } diff --git a/services/distributeddataservice/service/kvdb/user_delegate.cpp b/services/distributeddataservice/service/kvdb/user_delegate.cpp index 686bee77..ef7e2232 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.cpp +++ b/services/distributeddataservice/service/kvdb/user_delegate.cpp @@ -53,15 +53,19 @@ std::set UserDelegate::GetLocalUsers() ZLOGE("failed to get local device id"); return {}; } - if (!deviceUserMap_.Contains(deviceId)) { - LoadFromMeta(deviceId); - } std::set users; - deviceUserMap_.ComputeIfPresent(deviceId, [&users](auto&, std::map &value) { - for (auto [user, active] : value) { + deviceUserMap_.Compute(deviceId, [&users](const auto &key, auto &userMap) { + if (userMap.empty()) { + UserMetaData userMetaData; + MetaDataManager::GetInstance().LoadMeta(UserMetaRow::GetKeyFor(key), userMetaData); + for (const auto &user : userMetaData.users) { + userMap.insert_or_assign(user.id, user.isActive); + } + } + for (const auto [user, active] : userMap) { users.emplace(std::to_string(user)); } - return !value.empty(); + return !userMap.empty(); }); return users; } @@ -78,16 +82,19 @@ std::vector UserDelegate::GetRemoteUserStatus(const std::vector UserDelegate::GetUsers(const std::string &deviceId) { std::vector userStatus; - if (!deviceUserMap_.Contains(deviceId)) { - LoadFromMeta(deviceId); - } - deviceUserMap_.ComputeIfPresent(deviceId, [&userStatus](const auto &, std::map &userMap) { - for (const auto &[key, value] : userMap) { + deviceUserMap_.Compute(deviceId, [&userStatus](const auto &key, auto &userMap) { + if (userMap.empty()) { + UserMetaData userMetaData; + MetaDataManager::GetInstance().LoadMeta(UserMetaRow::GetKeyFor(key), userMetaData); + for (const auto &user : userMetaData.users) { + userMap.insert_or_assign(user.id, user.isActive); + } + } + for (const auto [key, value] : userMap) { userStatus.emplace_back(key, value); } - return true; + return !userMap.empty(); }); - ZLOGI("device:%{public}s, users:%{public}s", Anonymous::Change(deviceId).c_str(), Serializable::Marshall(userStatus).c_str()); return userStatus; @@ -100,13 +107,13 @@ void UserDelegate::DeleteUsers(const std::string &deviceId) void UserDelegate::UpdateUsers(const std::string &deviceId, const std::vector &userStatus) { - ZLOGI("begin, device:%{public}.10s, users:%{public}zu", Anonymous::Change(deviceId).c_str(), userStatus.size()); + ZLOGI("begin, device:%{public}s, users:%{public}zu", Anonymous::Change(deviceId).c_str(), userStatus.size()); deviceUserMap_.Compute(deviceId, [&userStatus](const auto &key, std::map &userMap) { userMap = {}; for (const auto &user : userStatus) { userMap[user.id] = user.isActive; } - ZLOGI("end, device:%{public}.10s, users:%{public}zu", Anonymous::Change(key).c_str(), userMap.size()); + ZLOGI("end, device:%{public}s, users:%{public}zu", Anonymous::Change(key).c_str(), userMap.size()); return true; }); } @@ -136,20 +143,6 @@ bool UserDelegate::InitLocalUserMeta() return MetaDataManager::GetInstance().SaveMeta(UserMetaRow::GetKeyFor(userMetaData.deviceId), userMetaData); } -void UserDelegate::LoadFromMeta(const std::string &deviceId) -{ - UserMetaData userMetaData; - MetaDataManager::GetInstance().LoadMeta(UserMetaRow::GetKeyFor(deviceId), userMetaData); - std::map userMap; - for (const auto &user : userMetaData.users) { - userMap[user.id] = user.isActive; - } - deviceUserMap_.Compute(deviceId, [&userMap](const auto &, auto &value) { - value = userMap; - return true; - }); -} - UserDelegate &UserDelegate::GetInstance() { static UserDelegate instance; diff --git a/services/distributeddataservice/service/kvdb/user_delegate.h b/services/distributeddataservice/service/kvdb/user_delegate.h index fb3797d6..440c25f7 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.h +++ b/services/distributeddataservice/service/kvdb/user_delegate.h @@ -56,7 +56,6 @@ private: UserDelegate &userDelegate_; }; std::vector GetUsers(const std::string &deviceId); - void LoadFromMeta(const std::string &deviceId); void UpdateUsers(const std::string &deviceId, const std::vector &userStatus); void DeleteUsers(const std::string &deviceId); bool NotifyUserEvent(const UserEvent &userEvent); -- Gitee From f5c2f2483fbfef2602f13006999fe9596eb4057f Mon Sep 17 00:00:00 2001 From: renjiecui Date: Thu, 23 Mar 2023 19:07:26 +0800 Subject: [PATCH 015/152] fix gaojing Signed-off-by: renjiecui --- .../adapter/communicator/src/data_buffer.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp b/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp index 7f4060c8..4438f2b3 100644 --- a/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp +++ b/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp @@ -44,6 +44,9 @@ bool DataBuffer::Init(size_t size) return false; } size_ = std::min(size, MAX_DATA_LEN); + if (size <= 1) { + return false; + } buf_ = new(std::nothrow) char[size_](); if (buf_ == nullptr) { return false; -- Gitee From 7c0b50e00c9b070fd738b1f2ffa573c278d67748 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Thu, 23 Mar 2023 19:15:18 +0800 Subject: [PATCH 016/152] fix ut Signed-off-by: htt1997 --- .../distributeddataservice/service/test/config_factory_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/test/config_factory_test.cpp b/services/distributeddataservice/service/test/config_factory_test.cpp index 6430fac4..b1ee7541 100644 --- a/services/distributeddataservice/service/test/config_factory_test.cpp +++ b/services/distributeddataservice/service/test/config_factory_test.cpp @@ -61,7 +61,7 @@ HWTEST_F(ConfigFactoryTest, ComponentConfig, TestSize.Level0) { auto *components = ConfigFactory::GetInstance().GetComponentConfig(); ASSERT_NE(components, nullptr); - ASSERT_EQ(components->size(), 2); + ASSERT_EQ(components->size(), 3); const ComponentConfig &config = (*components)[0]; ASSERT_EQ(config.description, "3rd party adapter"); ASSERT_EQ(config.lib, "libconfigdemo.z.so"); -- Gitee From fa066248ab7077b36a9345802726c044454cd34b Mon Sep 17 00:00:00 2001 From: renjiecui Date: Fri, 24 Mar 2023 17:01:34 +0800 Subject: [PATCH 017/152] fix gaojing Signed-off-by: renjiecui --- .../adapter/communicator/src/data_buffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp b/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp index 4438f2b3..3c7e47f1 100644 --- a/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp +++ b/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp @@ -44,7 +44,7 @@ bool DataBuffer::Init(size_t size) return false; } size_ = std::min(size, MAX_DATA_LEN); - if (size <= 1) { + if (size <= 0) { return false; } buf_ = new(std::nothrow) char[size_](); -- Gitee From 3b61aa9da56293398fca1323612c7f6427fad772 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Fri, 24 Mar 2023 19:40:15 +0800 Subject: [PATCH 018/152] fix gaojing Signed-off-by: renjiecui --- .../adapter/communicator/src/data_buffer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp b/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp index 3c7e47f1..95fda11a 100644 --- a/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp +++ b/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp @@ -40,13 +40,13 @@ DataBuffer::~DataBuffer() bool DataBuffer::Init(size_t size) { - if (buf_ != nullptr) { + if (size <= 0) { return false; } - size_ = std::min(size, MAX_DATA_LEN); - if (size <= 0) { + if (buf_ != nullptr) { return false; } + size_ = std::min(size, MAX_DATA_LEN); buf_ = new(std::nothrow) char[size_](); if (buf_ == nullptr) { return false; -- Gitee From 978a4c7a5046f97488e93923728c640cdb09f6f3 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sat, 25 Mar 2023 09:11:34 +0800 Subject: [PATCH 019/152] fix gaojing Signed-off-by: renjiecui --- .../adapter/communicator/src/data_buffer.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp b/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp index 95fda11a..f9c47623 100644 --- a/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp +++ b/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp @@ -40,10 +40,7 @@ DataBuffer::~DataBuffer() bool DataBuffer::Init(size_t size) { - if (size <= 0) { - return false; - } - if (buf_ != nullptr) { + if (buf_ != nullptr || size <= 0) { return false; } size_ = std::min(size, MAX_DATA_LEN); -- Gitee From a3bd1758902b0b595d4dc7e439a430120698ac15 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sat, 25 Mar 2023 09:15:14 +0800 Subject: [PATCH 020/152] fix gaojing Signed-off-by: renjiecui --- .../distributeddataservice/service/test/config_factory_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/test/config_factory_test.cpp b/services/distributeddataservice/service/test/config_factory_test.cpp index 6430fac4..b1ee7541 100644 --- a/services/distributeddataservice/service/test/config_factory_test.cpp +++ b/services/distributeddataservice/service/test/config_factory_test.cpp @@ -61,7 +61,7 @@ HWTEST_F(ConfigFactoryTest, ComponentConfig, TestSize.Level0) { auto *components = ConfigFactory::GetInstance().GetComponentConfig(); ASSERT_NE(components, nullptr); - ASSERT_EQ(components->size(), 2); + ASSERT_EQ(components->size(), 3); const ComponentConfig &config = (*components)[0]; ASSERT_EQ(config.description, "3rd party adapter"); ASSERT_EQ(config.lib, "libconfigdemo.z.so"); -- Gitee From e7c2fa0afcd8fbcac7f89e02b3e8241bdeff6303 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sat, 25 Mar 2023 10:20:46 +0800 Subject: [PATCH 021/152] fix gaojing Signed-off-by: renjiecui --- .../adapter/communicator/src/data_buffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp b/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp index f9c47623..84d2f95a 100644 --- a/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp +++ b/services/distributeddataservice/adapter/communicator/src/data_buffer.cpp @@ -40,7 +40,7 @@ DataBuffer::~DataBuffer() bool DataBuffer::Init(size_t size) { - if (buf_ != nullptr || size <= 0) { + if (buf_ != nullptr || size == 0) { return false; } size_ = std::min(size, MAX_DATA_LEN); -- Gitee From 494ebd169c9a01fbd0e099d548c59876fd363825 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sat, 25 Mar 2023 17:04:02 +0800 Subject: [PATCH 022/152] update Signed-off-by: htt1997 --- .../service/kvdb/user_delegate.cpp | 34 +++++++++---------- .../service/kvdb/user_delegate.h | 2 +- .../service/test/config_factory_test.cpp | 2 +- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/user_delegate.cpp b/services/distributeddataservice/service/kvdb/user_delegate.cpp index ef7e2232..ee66bb38 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.cpp +++ b/services/distributeddataservice/service/kvdb/user_delegate.cpp @@ -54,18 +54,18 @@ std::set UserDelegate::GetLocalUsers() return {}; } std::set users; - deviceUserMap_.Compute(deviceId, [&users](const auto &key, auto &userMap) { - if (userMap.empty()) { + deviceUser_.Compute(deviceId, [&users](const auto &key, auto &value) { + if (value.empty()) { UserMetaData userMetaData; MetaDataManager::GetInstance().LoadMeta(UserMetaRow::GetKeyFor(key), userMetaData); for (const auto &user : userMetaData.users) { - userMap.insert_or_assign(user.id, user.isActive); + value[user.id] = user.isActive; } } - for (const auto [user, active] : userMap) { + for (const auto [user, active] : value) { users.emplace(std::to_string(user)); } - return !userMap.empty(); + return !value.empty(); }); return users; } @@ -82,18 +82,18 @@ std::vector UserDelegate::GetRemoteUserStatus(const std::vector UserDelegate::GetUsers(const std::string &deviceId) { std::vector userStatus; - deviceUserMap_.Compute(deviceId, [&userStatus](const auto &key, auto &userMap) { - if (userMap.empty()) { + deviceUser_.Compute(deviceId, [&userStatus](const auto &key, auto &users) { + if (users.empty()) { UserMetaData userMetaData; MetaDataManager::GetInstance().LoadMeta(UserMetaRow::GetKeyFor(key), userMetaData); for (const auto &user : userMetaData.users) { - userMap.insert_or_assign(user.id, user.isActive); + users[user.id] = user.isActive; } } - for (const auto [key, value] : userMap) { + for (const auto [key, value] : users) { userStatus.emplace_back(key, value); } - return !userMap.empty(); + return !users.empty(); }); ZLOGI("device:%{public}s, users:%{public}s", Anonymous::Change(deviceId).c_str(), Serializable::Marshall(userStatus).c_str()); @@ -102,18 +102,18 @@ std::vector UserDelegate::GetUsers(const std::string &deviceId) void UserDelegate::DeleteUsers(const std::string &deviceId) { - deviceUserMap_.Erase(deviceId); + deviceUser_.Erase(deviceId); } void UserDelegate::UpdateUsers(const std::string &deviceId, const std::vector &userStatus) { ZLOGI("begin, device:%{public}s, users:%{public}zu", Anonymous::Change(deviceId).c_str(), userStatus.size()); - deviceUserMap_.Compute(deviceId, [&userStatus](const auto &key, std::map &userMap) { - userMap = {}; + deviceUser_.Compute(deviceId, [&userStatus](const auto &key, std::map &users) { + users = {}; for (const auto &user : userStatus) { - userMap[user.id] = user.isActive; + users[user.id] = user.isActive; } - ZLOGI("end, device:%{public}s, users:%{public}zu", Anonymous::Change(key).c_str(), userMap.size()); + ZLOGI("end, device:%{public}s, users:%{public}zu", Anonymous::Change(key).c_str(), users.size()); return true; }); } @@ -133,8 +133,8 @@ bool UserDelegate::InitLocalUserMeta() UserMetaData userMetaData; userMetaData.deviceId = GetLocalDeviceId(); UpdateUsers(userMetaData.deviceId, userStatus); - deviceUserMap_.ComputeIfPresent(userMetaData.deviceId, [&userMetaData](const auto &, std::map &userMap) { - for (const auto &[key, value] : userMap) { + deviceUser_.ComputeIfPresent(userMetaData.deviceId, [&userMetaData](const auto &, std::map &users) { + for (const auto &[key, value] : users) { userMetaData.users.emplace_back(key, value); } return true; diff --git a/services/distributeddataservice/service/kvdb/user_delegate.h b/services/distributeddataservice/service/kvdb/user_delegate.h index 440c25f7..56dec6c1 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.h +++ b/services/distributeddataservice/service/kvdb/user_delegate.h @@ -61,7 +61,7 @@ private: bool NotifyUserEvent(const UserEvent &userEvent); // device : { user : isActive } - ConcurrentMap> deviceUserMap_; + ConcurrentMap> deviceUser_; }; } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/service/test/config_factory_test.cpp b/services/distributeddataservice/service/test/config_factory_test.cpp index b1ee7541..6430fac4 100644 --- a/services/distributeddataservice/service/test/config_factory_test.cpp +++ b/services/distributeddataservice/service/test/config_factory_test.cpp @@ -61,7 +61,7 @@ HWTEST_F(ConfigFactoryTest, ComponentConfig, TestSize.Level0) { auto *components = ConfigFactory::GetInstance().GetComponentConfig(); ASSERT_NE(components, nullptr); - ASSERT_EQ(components->size(), 3); + ASSERT_EQ(components->size(), 2); const ComponentConfig &config = (*components)[0]; ASSERT_EQ(config.description, "3rd party adapter"); ASSERT_EQ(config.lib, "libconfigdemo.z.so"); -- Gitee From 6728706397e9c1f98046e328f6866f901ceb82ec Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Mon, 27 Mar 2023 10:31:58 +0800 Subject: [PATCH 023/152] fix code Signed-off-by: zuojiangjiang --- .../service/data_share/data_share_profile_info.cpp | 2 +- .../distributeddataservice/service/data_share/rdb_adaptor.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/data_share/data_share_profile_info.cpp b/services/distributeddataservice/service/data_share/data_share_profile_info.cpp index 2b286378..da563ec8 100644 --- a/services/distributeddataservice/service/data_share/data_share_profile_info.cpp +++ b/services/distributeddataservice/service/data_share/data_share_profile_info.cpp @@ -99,7 +99,7 @@ std::vector DataShareProfileInfo::GetResProfileByMetadata( } for (auto const &meta : metadata) { - if (meta.name.compare(DATA_SHARE_PROFILE_META) == 0) { + if (meta.name == DATA_SHARE_PROFILE_META) { return GetResFromResMgr(meta.resource, *resMgr, isCompressed); } } diff --git a/services/distributeddataservice/service/data_share/rdb_adaptor.h b/services/distributeddataservice/service/data_share/rdb_adaptor.h index fccfc607..57e9a423 100644 --- a/services/distributeddataservice/service/data_share/rdb_adaptor.h +++ b/services/distributeddataservice/service/data_share/rdb_adaptor.h @@ -34,7 +34,7 @@ using StoreMetaData = OHOS::DistributedData::StoreMetaData; using namespace OHOS::NativeRdb; class RdbDelegate { public: - RdbDelegate(const StoreMetaData &data, int &errCode); + RdbDelegate(const StoreMetaData &meta, int &errCode); virtual ~RdbDelegate(); int64_t Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket); int64_t Update(const std::string &tableName, const DataSharePredicates &predicate, -- Gitee From ad3d88628a599a962b33fad2c5407156a3549bdf Mon Sep 17 00:00:00 2001 From: ylq121 Date: Tue, 28 Mar 2023 19:08:16 +0800 Subject: [PATCH 024/152] xiugai Signed-off-by: ylq121 --- .../service/test/mock/db_store_mock.cpp | 5 +++++ .../distributeddataservice/service/test/mock/db_store_mock.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/services/distributeddataservice/service/test/mock/db_store_mock.cpp b/services/distributeddataservice/service/test/mock/db_store_mock.cpp index 9f7e3647..87639965 100644 --- a/services/distributeddataservice/service/test/mock/db_store_mock.cpp +++ b/services/distributeddataservice/service/test/mock/db_store_mock.cpp @@ -305,5 +305,10 @@ DBStatus DBStoreMock::DeleteBatch(ConcurrentMap &store, const std::v }); return OK; } + +DBStatus DBStoreMock::UpdateKey(const UpdateKeyCallback &callback) +{ + return NOT_SUPPORT; +} } // namespace DistributedData } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/service/test/mock/db_store_mock.h b/services/distributeddataservice/service/test/mock/db_store_mock.h index eedd339a..e50def98 100644 --- a/services/distributeddataservice/service/test/mock/db_store_mock.h +++ b/services/distributeddataservice/service/test/mock/db_store_mock.h @@ -36,6 +36,7 @@ public: using SecurityOption = DistributedDB::SecurityOption; using RemotePushFinishedNotifier = DistributedDB::RemotePushFinishedNotifier; using PushDataInterceptor = DistributedDB::PushDataInterceptor; + using UpdateKeyCallback = DistributedDB::UpdateKeyCallback; DBStatus Get(const Key &key, Value &value) const override; DBStatus GetEntries(const Key &keyPrefix, std::vector &entries) const override; DBStatus GetEntries(const Key &keyPrefix, KvStoreResultSet *&resultSet) const override; @@ -87,6 +88,7 @@ public: DBStatus RemoveDeviceData() override; DBStatus GetKeys(const Key &keyPrefix, std::vector &keys) const override; size_t GetSyncDataSize(const std::string &device) const override; + DBStatus UpdateKey(const UpdateKeyCallback &callback) override; private: static const uint32_t DEFAULT_SIZE = 0; -- Gitee From 62d5a19658ae17ab395efabf078e5a5d605fa05c Mon Sep 17 00:00:00 2001 From: ylq121 Date: Wed, 29 Mar 2023 09:48:45 +0800 Subject: [PATCH 025/152] uuid -s Signed-off-by: ylq121 --- .../src/device_manager_adapter.cpp | 15 +++++++++ .../communicator/device_manager_adapter.h | 1 + .../app/src/kvstore_data_service.cpp | 20 ++++++++++++ .../include/metadata/store_meta_data.h | 2 +- .../service/kvdb/upgrade.cpp | 31 +++++++++++++++++++ .../service/kvdb/upgrade.h | 9 ++++++ 6 files changed, 77 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index 5862e35d..d8359638 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -467,4 +467,19 @@ std::string DeviceManagerAdapter::ToNetworkID(const std::string &id) { return GetDeviceInfoFromCache(id).networkId; } + +std::string DeviceManagerAdapter::CalcClientUuid(const std::string &appId, const std::string &uuid) +{ + if (appId.empty() || uuid.empty()) { + return ""; + } + std::string encryptedUuid; + auto ret = DeviceManager::GetInstance().GenerateEncryptedUuid(PKG_NAME, uuid, appId, encryptedUuid); + if (ret != DM_OK) { + ZLOGE("failed, result:%{public}d", ret); + return ""; + } + return encryptedUuid; +} + } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h index 0fc8a548..52d4b99d 100644 --- a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h +++ b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h @@ -47,6 +47,7 @@ public: DeviceInfo GetDeviceInfo(const std::string &id); std::string GetUuidByNetworkId(const std::string &networkId); std::string GetUdidByNetworkId(const std::string &networkId); + std::string CalcClientUuid(const std::string &appId, const std::string &uuid); std::string ToUUID(const std::string &id); std::string ToUDID(const std::string &id); static std::vector ToUUID(const std::vector &devices); diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index c9138c65..22c231f4 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -19,6 +19,7 @@ #include #include +#include "accesstoken_kit.h" #include "auth_delegate.h" #include "auto_launch_export.h" #include "bootstrap.h" @@ -35,6 +36,7 @@ #include "iservice_registry.h" #include "kvstore_account_observer.h" #include "log_print.h" +#include "metadata/appid_meta_data.h" #include "metadata/meta_data_manager.h" #include "metadata/secret_key_meta_data.h" #include "permission_validator.h" @@ -57,6 +59,7 @@ using namespace OHOS::DistributedDataDfx; using KvStoreDelegateManager = DistributedDB::KvStoreDelegateManager; using SecretKeyMeta = DistributedData::SecretKeyMetaData; using DmAdapter = DistributedData::DeviceManagerAdapter; +using DBConfig = DistributedDB::RuntimeConfig; REGISTER_SYSTEM_ABILITY_BY_ID(KvStoreDataService, DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID, true); @@ -97,6 +100,23 @@ void KvStoreDataService::Initialize() AccountDelegate::GetInstance()->Subscribe(accountEventObserver_); deviceInnerListener_ = std::make_unique(*this); DmAdapter::GetInstance().StartWatchDeviceChange(deviceInnerListener_.get(), { "innerListener" }); + auto translateToDeviceIdCall = [this](const std::string &oriDevId, const DistributedDB::StoreInfo &info) { + StoreMetaData meta; + AppIDMetaData appIdMeta; + MetaDataManager::GetInstance().LoadMeta(info.appId, appIdMeta, true); + meta.bundleName = appIdMeta.bundleName; + meta.storeId = info.storeId; + meta.user = info.userId; + meta.deviceId = oriDevId; + MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta); + if (OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(meta.tokenId) == + OHOS::Security::AccessToken::TOKEN_HAP) { + auto uuid = DmAdapter::GetInstance().CalcClientUuid(info.appId, oriDevId); + return uuid; + } + return oriDevId; + }; + DBConfig::SetTranslateToDeviceIdCallback(translateToDeviceIdCall); } sptr KvStoreDataService::GetFeatureInterface(const std::string &name) diff --git a/services/distributeddataservice/framework/include/metadata/store_meta_data.h b/services/distributeddataservice/framework/include/metadata/store_meta_data.h index 2a2fcc5f..34f4fca2 100644 --- a/services/distributeddataservice/framework/include/metadata/store_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/store_meta_data.h @@ -22,7 +22,7 @@ namespace OHOS::DistributedData { struct API_EXPORT StoreMetaData final : public Serializable { // record meta version for compatible, should update when modify store meta data structure. - static constexpr uint32_t CURRENT_VERSION = 0x03000003; + static constexpr uint32_t CURRENT_VERSION = 0x03000004; // UID -> uid, deviceAccountId -> userId, userId -> user static constexpr uint32_t FIELD_CHANGED_TAG = 0x03000003; uint32_t version = CURRENT_VERSION; diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index 8220ef83..9fdacf9b 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -20,6 +20,7 @@ #include "crypto_manager.h" #include "metadata/secret_key_meta_data.h" +#include "log_print.h" #include "metadata/meta_data_manager.h" #include "store_cache.h" #include "directory_manager.h" @@ -34,6 +35,13 @@ Upgrade &Upgrade::GetInstance() Upgrade::DBStatus Upgrade::UpdateStore(const StoreMeta &old, const StoreMeta &meta, const std::vector &pwd) { + if (old.version <= VERSION && old.storeType == DEVICE_COLLABORATION) { + auto upStatus = Upgrade::GetInstance().UpdateUuid(old, meta, pwd); + if (upStatus != DBStatus::OK) { + return DBStatus::DB_ERROR; + } + } + if (old.dataDir == meta.dataDir) { return DBStatus::OK; } @@ -89,6 +97,29 @@ void Upgrade::UpdatePassword(const StoreMeta &meta, const std::vector & MetaDataManager::GetInstance().SaveMeta(meta.GetSecretKey(), secretKey, true); } +Upgrade::DBStatus Upgrade::UpdateUuid(const StoreMeta &old, const StoreMeta &meta, const std::vector &pwd) +{ + auto kvStore = GetDBStore(meta, pwd); + if (kvStore == nullptr) { + return DBStatus::DB_ERROR; + } + kvStore->RemoveDeviceData(); + auto dbStatus = kvStore->UpdateKey([appId = meta.appId](const DBKey &originKey, DBKey &newKey) { + auto oriUuid = DMAdapter::GetInstance().GetLocalDevice().uuid; + auto newUuid = DMAdapter::GetInstance().CalcClientUuid(appId, oriUuid); + newKey.assign(originKey.begin(), originKey.end()); + uint32_t length = *(reinterpret_cast(&(*(newKey.end() - sizeof(uint32_t))))); + length = le32toh(length); + newKey.erase(newKey.begin(), newKey.begin() + length); + newKey.insert(newKey.begin(), newUuid.begin(), newUuid.end()); + }); + + if (dbStatus != DBStatus::OK) { + ZLOGE("fail to update Uuid, status:%{public}d", dbStatus); + } + return dbStatus; +} + bool Upgrade::RegisterExporter(uint32_t version, Exporter exporter) { (void)version; diff --git a/services/distributeddataservice/service/kvdb/upgrade.h b/services/distributeddataservice/service/kvdb/upgrade.h index 62644591..2d41540a 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.h +++ b/services/distributeddataservice/service/kvdb/upgrade.h @@ -17,6 +17,8 @@ #define OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_UPGRADE_H #include #include + +#include "device_manager_adapter.h" #include "kv_store_delegate_manager.h" #include "kv_store_nb_delegate.h" #include "metadata/store_meta_data.h" @@ -29,8 +31,11 @@ public: using DBStatus = DistributedDB::DBStatus; using DBStore = DistributedDB::KvStoreNbDelegate; using DBManager = DistributedDB::KvStoreDelegateManager; + using DMAdapter = DistributedData::DeviceManagerAdapter; using Exporter = std::function; using Cleaner = std::function; + using DBKey = std::vector; + API_EXPORT static Upgrade &GetInstance(); API_EXPORT bool RegisterExporter(uint32_t version, Exporter exporter); API_EXPORT bool RegisterCleaner(uint32_t version, Cleaner cleaner); @@ -38,10 +43,14 @@ public: DBStatus UpdateStore(const StoreMeta &old, const StoreMeta &metaData, const std::vector &pwd); DBStatus ExportStore(const StoreMeta &old, const StoreMeta &meta); void UpdatePassword(const StoreMeta &meta, const std::vector &password); + DBStatus UpdateUuid(const StoreMeta &old, const StoreMeta &meta, const std::vector &pwd); private: using AutoStore = std::unique_ptr>; AutoStore GetDBStore(const StoreMeta &meta, const std::vector &pwd); + static constexpr size_t MAX_DEV_KEY_LEN = 896; + static constexpr uint32_t VERSION = 0x03000003; + Exporter exporter_; Cleaner cleaner_; }; -- Gitee From f4728b08edf16fc39412ae497f39c09b29656c58 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Wed, 29 Mar 2023 10:50:35 +0800 Subject: [PATCH 026/152] delete duplicates Signed-off-by: ylq121 --- .../service/kvdb/kvdb_service_impl.cpp | 30 ------------------- .../service/kvdb/kvdb_service_impl.h | 4 +-- .../service/kvdb/kvdb_service_stub.cpp | 29 ------------------ .../service/kvdb/kvdb_service_stub.h | 2 -- 4 files changed, 1 insertion(+), 64 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 6e19c96a..de70ec23 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -39,7 +39,6 @@ #include "utils/anonymous.h" #include "utils/constant.h" #include "utils/converter.h" -#include "utils/crypto.h" namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; using namespace OHOS::AppDistributedKv; @@ -351,35 +350,6 @@ Status KVDBServiceImpl::GetBackupPassword(const AppId &appId, const StoreId &sto return (BackupManager::GetInstance().GetPassWord(metaData, password)) ? SUCCESS : ERROR; } -KVDBService::DevBrief KVDBServiceImpl::GetLocalDevice() -{ - DevBrief brief; - CheckerManager::StoreInfo storeInfo; - storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); - storeInfo.uid = IPCSkeleton::GetCallingPid(); - auto appId = CheckerManager::GetInstance().GetAppId(storeInfo); - auto device = DMAdapter::GetInstance().GetLocalDevice(); - brief.networkId = std::move(device.networkId); - brief.uuid = Crypto::Sha256(appId + "_" + device.uuid); - return brief; -} - -std::vector KVDBServiceImpl::GetRemoteDevices() -{ - std::vector briefs; - CheckerManager::StoreInfo storeInfo; - storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); - storeInfo.uid = IPCSkeleton::GetCallingPid(); - auto appId = CheckerManager::GetInstance().GetAppId(storeInfo); - auto devices = DMAdapter::GetInstance().GetRemoteDevices(); - for (const auto &device : devices) { - DevBrief brief; - brief.networkId = std::move(device.networkId); - brief.uuid = Crypto::Sha256(appId + "_" + device.uuid); - briefs.push_back(std::move(brief)); - } - return briefs; -} Status KVDBServiceImpl::BeforeCreate(const AppId &appId, const StoreId &storeId, const Options &options) { diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h index 72dddfc4..3cee4848 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h @@ -53,9 +53,7 @@ public: Status Subscribe(const AppId &appId, const StoreId &storeId, sptr observer) override; Status Unsubscribe(const AppId &appId, const StoreId &storeId, sptr observer) override; Status GetBackupPassword(const AppId &appId, const StoreId &storeId, std::vector &password) override; - DevBrief GetLocalDevice() override; - std::vector GetRemoteDevices() override; - + int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &appId) override; 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; diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp index 95e333dc..e893f613 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp @@ -40,8 +40,6 @@ const KVDBServiceStub::Handler KVDBServiceStub::HANDLERS[TRANS_BUTT] = { &KVDBServiceStub::OnSubscribe, &KVDBServiceStub::OnUnsubscribe, &KVDBServiceStub::OnGetBackupPassword, - &KVDBServiceStub::OnGetLocalDevice, - &KVDBServiceStub::OnGetRemoteDevices, }; int KVDBServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) @@ -225,33 +223,6 @@ int32_t KVDBServiceStub::OnGetSyncParam( return ERR_NONE; } -int32_t KVDBServiceStub::OnGetLocalDevice( - const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply) -{ - (void)appId; - (void)storeId; - int32_t status = SUCCESS; - auto brief = GetLocalDevice(); - if (!ITypesUtil::Marshal(reply, status, brief)) { - ZLOGE("Marshal device brief:{%{public}u, %{public}u}", brief.networkId.empty(), brief.uuid.empty()); - return IPC_STUB_WRITE_PARCEL_ERR; - } - return ERR_NONE; -} - -int32_t KVDBServiceStub::OnGetRemoteDevices( - const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply) -{ - (void)appId; - (void)storeId; - int32_t status = SUCCESS; - auto briefs = GetRemoteDevices(); - if (!ITypesUtil::Marshal(reply, status, briefs)) { - ZLOGE("Marshal device brief:%{public}zu", briefs.size()); - return IPC_STUB_WRITE_PARCEL_ERR; - } - return ERR_NONE; -} int32_t KVDBServiceStub::OnEnableCap( 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 8b5f8b90..ab0bacc4 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_stub.h +++ b/services/distributeddataservice/service/kvdb/kvdb_service_stub.h @@ -43,8 +43,6 @@ private: int32_t OnSubscribe(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply); int32_t OnUnsubscribe(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply); int32_t OnGetBackupPassword(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply); - int32_t OnGetLocalDevice(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply); - int32_t OnGetRemoteDevices(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply); static const Handler HANDLERS[TRANS_BUTT]; }; } // namespace OHOS::DistributedKv -- Gitee From 83b153f3037916cc90e83dcf93e7e287bf938ab2 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Thu, 30 Mar 2023 10:12:24 +0800 Subject: [PATCH 027/152] xiugai Signed-off-by: ylq121 --- .../adapter/communicator/src/device_manager_adapter.cpp | 2 +- .../app/src/kvstore_data_service.cpp | 7 ++++--- .../service/kvdb/kvdb_service_stub.cpp | 5 +---- services/distributeddataservice/service/kvdb/upgrade.cpp | 6 +++++- services/distributeddataservice/service/kvdb/upgrade.h | 4 ---- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index d8359638..b4eccc5e 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -470,7 +470,7 @@ std::string DeviceManagerAdapter::ToNetworkID(const std::string &id) std::string DeviceManagerAdapter::CalcClientUuid(const std::string &appId, const std::string &uuid) { - if (appId.empty() || uuid.empty()) { + if (uuid.empty()) { return ""; } std::string encryptedUuid; diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 22c231f4..e47c3677 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -100,7 +100,7 @@ void KvStoreDataService::Initialize() AccountDelegate::GetInstance()->Subscribe(accountEventObserver_); deviceInnerListener_ = std::make_unique(*this); DmAdapter::GetInstance().StartWatchDeviceChange(deviceInnerListener_.get(), { "innerListener" }); - auto translateToDeviceIdCall = [this](const std::string &oriDevId, const DistributedDB::StoreInfo &info) { + auto translateCall = [this](const std::string &oriDevId, const DistributedDB::StoreInfo &info) { StoreMetaData meta; AppIDMetaData appIdMeta; MetaDataManager::GetInstance().LoadMeta(info.appId, appIdMeta, true); @@ -114,9 +114,10 @@ void KvStoreDataService::Initialize() auto uuid = DmAdapter::GetInstance().CalcClientUuid(info.appId, oriDevId); return uuid; } - return oriDevId; + auto uuid = DmAdapter::GetInstance().CalcClientUuid(" ", oriDevId); + return uuid; }; - DBConfig::SetTranslateToDeviceIdCallback(translateToDeviceIdCall); + DBConfig::SetTranslateToDeviceIdCallback(translateCall); } sptr KvStoreDataService::GetFeatureInterface(const std::string &name) diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp index e893f613..766ea020 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp @@ -52,16 +52,13 @@ int KVDBServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Message return -1; } - if (TRANS_HEAD > code || code >= TRANS_BUTT || HANDLERS[code] == nullptr) { + if (TRANS_HEAD > code || code > TRANS_BUTT || HANDLERS[code] == nullptr) { ZLOGE("not support code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); return -1; } AppId appId; StoreId storeId; - if (TRANS_NO_APPID_BEGIN <= code && code <= TRANS_NO_APPID_END) { - return (this->*HANDLERS[code])(appId, storeId, data, reply); - } if (!ITypesUtil::Unmarshal(data, appId, storeId)) { ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); return IPC_STUB_INVALID_DATA_ERR; diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index 9fdacf9b..5b4cb3e9 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -20,6 +20,7 @@ #include "crypto_manager.h" #include "metadata/secret_key_meta_data.h" +#include "device_manager_adapter.h" #include "log_print.h" #include "metadata/meta_data_manager.h" #include "store_cache.h" @@ -27,6 +28,9 @@ namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; using system_clock = std::chrono::system_clock; +using DMAdapter = DistributedData::DeviceManagerAdapter; +using DBKey = std::vector; + Upgrade &Upgrade::GetInstance() { static Upgrade upgrade; @@ -35,7 +39,7 @@ Upgrade &Upgrade::GetInstance() Upgrade::DBStatus Upgrade::UpdateStore(const StoreMeta &old, const StoreMeta &meta, const std::vector &pwd) { - if (old.version <= VERSION && old.storeType == DEVICE_COLLABORATION) { + if (old.version < meta.UUID_CHANGED_TAG && old.storeType == DEVICE_COLLABORATION) { auto upStatus = Upgrade::GetInstance().UpdateUuid(old, meta, pwd); if (upStatus != DBStatus::OK) { return DBStatus::DB_ERROR; diff --git a/services/distributeddataservice/service/kvdb/upgrade.h b/services/distributeddataservice/service/kvdb/upgrade.h index 2d41540a..0c8a5a70 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.h +++ b/services/distributeddataservice/service/kvdb/upgrade.h @@ -18,7 +18,6 @@ #include #include -#include "device_manager_adapter.h" #include "kv_store_delegate_manager.h" #include "kv_store_nb_delegate.h" #include "metadata/store_meta_data.h" @@ -31,10 +30,8 @@ public: using DBStatus = DistributedDB::DBStatus; using DBStore = DistributedDB::KvStoreNbDelegate; using DBManager = DistributedDB::KvStoreDelegateManager; - using DMAdapter = DistributedData::DeviceManagerAdapter; using Exporter = std::function; using Cleaner = std::function; - using DBKey = std::vector; API_EXPORT static Upgrade &GetInstance(); API_EXPORT bool RegisterExporter(uint32_t version, Exporter exporter); @@ -49,7 +46,6 @@ private: using AutoStore = std::unique_ptr>; AutoStore GetDBStore(const StoreMeta &meta, const std::vector &pwd); static constexpr size_t MAX_DEV_KEY_LEN = 896; - static constexpr uint32_t VERSION = 0x03000003; Exporter exporter_; Cleaner cleaner_; -- Gitee From 8715c7552e24a804cbe14748ee6e53fb8025e571 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Thu, 30 Mar 2023 10:19:55 +0800 Subject: [PATCH 028/152] xiugai Signed-off-by: ylq121 --- .../distributeddataservice/app/src/kvstore_data_service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index e47c3677..cf38810e 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -100,7 +100,7 @@ void KvStoreDataService::Initialize() AccountDelegate::GetInstance()->Subscribe(accountEventObserver_); deviceInnerListener_ = std::make_unique(*this); DmAdapter::GetInstance().StartWatchDeviceChange(deviceInnerListener_.get(), { "innerListener" }); - auto translateCall = [this](const std::string &oriDevId, const DistributedDB::StoreInfo &info) { + auto translateCall = [](const std::string &oriDevId, const DistributedDB::StoreInfo &info) { StoreMetaData meta; AppIDMetaData appIdMeta; MetaDataManager::GetInstance().LoadMeta(info.appId, appIdMeta, true); -- Gitee From 591831b7166ee9f4440655b712ad5e838f2e71c1 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Thu, 30 Mar 2023 10:32:24 +0800 Subject: [PATCH 029/152] xiugai Signed-off-by: ylq121 --- .../distributeddataservice/service/kvdb/kvdb_service_impl.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index de70ec23..a19acff4 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -350,7 +350,6 @@ Status KVDBServiceImpl::GetBackupPassword(const AppId &appId, const StoreId &sto return (BackupManager::GetInstance().GetPassWord(metaData, password)) ? SUCCESS : ERROR; } - Status KVDBServiceImpl::BeforeCreate(const AppId &appId, const StoreId &storeId, const Options &options) { ZLOGD("appId:%{public}s storeId:%{public}s to export data", appId.appId.c_str(), storeId.storeId.c_str()); -- Gitee From d347d6d2c74bd82ab4dad857b158655e9c5cf09b Mon Sep 17 00:00:00 2001 From: ylq121 Date: Thu, 30 Mar 2023 10:38:21 +0800 Subject: [PATCH 030/152] xiugai Signed-off-by: ylq121 --- .../distributeddataservice/service/kvdb/kvdb_service_stub.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp index 766ea020..f678507d 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp @@ -220,7 +220,6 @@ int32_t KVDBServiceStub::OnGetSyncParam( return ERR_NONE; } - int32_t KVDBServiceStub::OnEnableCap( const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply) { -- Gitee From 496c461a91cdcefb2febbe806f4dbbfc20274c2b Mon Sep 17 00:00:00 2001 From: ylq121 Date: Thu, 30 Mar 2023 11:15:28 +0800 Subject: [PATCH 031/152] xiugai Signed-off-by: ylq121 --- .../framework/include/metadata/store_meta_data.h | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/framework/include/metadata/store_meta_data.h b/services/distributeddataservice/framework/include/metadata/store_meta_data.h index 34f4fca2..bb134e35 100644 --- a/services/distributeddataservice/framework/include/metadata/store_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/store_meta_data.h @@ -25,6 +25,7 @@ struct API_EXPORT StoreMetaData final : public Serializable { static constexpr uint32_t CURRENT_VERSION = 0x03000004; // UID -> uid, deviceAccountId -> userId, userId -> user static constexpr uint32_t FIELD_CHANGED_TAG = 0x03000003; + static constexpr uint32_t UUID_CHANGED_TAG = 0x03000004; uint32_t version = CURRENT_VERSION; bool isAutoSync = false; bool isBackup = false; -- Gitee From 5a507cd79cca2fdee09c0740a4723a01fdcca64b Mon Sep 17 00:00:00 2001 From: leiiyb Date: Wed, 29 Mar 2023 14:48:11 +0800 Subject: [PATCH 032/152] Convert to encrypted uuid Signed-off-by: leiiyb --- .../distributeddataservice/service/rdb/rdb_syncer.cpp | 11 ++++++++--- .../distributeddataservice/service/rdb/rdb_syncer.h | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/services/distributeddataservice/service/rdb/rdb_syncer.cpp index 1bd74d45..3d47af3e 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -109,9 +109,10 @@ int32_t RdbSyncer::Init( pid_ = pid; uid_ = uid; token_ = token; + StoreMetaData oldMeta; StoreMetaData meta; - if (CreateMetaData(meta) != RDB_OK) { + if (CreateMetaData(meta, oldMeta) != RDB_OK) { ZLOGE("create meta data failed"); return RDB_ERROR; } @@ -119,6 +120,11 @@ int32_t RdbSyncer::Init( ZLOGE("delegate is nullptr"); return RDB_ERROR; } + + if (oldMeta.storeType == RDB_DEVICE_COLLABORATION && oldMeta.version < StoreMetaData::UUID_CHANGED_TAG) { + delegate_->RemoveDeviceData(); + } + ZLOGI("success"); return RDB_OK; } @@ -150,10 +156,9 @@ void RdbSyncer::FillMetaData(StoreMetaData &meta) meta.isEncrypt = param_.isEncrypt_; } -int32_t RdbSyncer::CreateMetaData(StoreMetaData &meta) +int32_t RdbSyncer::CreateMetaData(StoreMetaData &meta, StoreMetaData &old) { FillMetaData(meta); - StoreMetaData old; bool isCreated = MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), old); if (isCreated && (old.storeType != meta.storeType || Constant::NotEqual(old.isEncrypt, meta.isEncrypt) || old.area != meta.area)) { diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.h b/services/distributeddataservice/service/rdb/rdb_syncer.h index e049a864..9b5f90b4 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -70,7 +70,7 @@ private: std::string GetAppId() const; - int32_t CreateMetaData(StoreMetaData &meta); + int32_t CreateMetaData(StoreMetaData &meta, StoreMetaData &old); void FillMetaData(StoreMetaData &meta); int32_t InitDBDelegate(const StoreMetaData &meta); bool SetSecretKey(const StoreMetaData &meta); -- Gitee From 75be5c1314074a46ab60626b340ec1ff81056ca7 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Fri, 31 Mar 2023 09:10:00 +0800 Subject: [PATCH 033/152] xiugai Signed-off-by: ylq121 --- .../adapter/communicator/BUILD.gn | 3 +++ .../communicator/src/device_manager_adapter.cpp | 12 ++++++++++++ .../include/communicator/device_manager_adapter.h | 3 +++ .../app/src/kvstore_data_service.cpp | 11 ++--------- .../service/kvdb/kvdb_service_stub.cpp | 2 +- .../service/kvdb/upgrade.cpp | 14 +++++--------- .../distributeddataservice/service/kvdb/upgrade.h | 1 - 7 files changed, 26 insertions(+), 20 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/BUILD.gn b/services/distributeddataservice/adapter/communicator/BUILD.gn index c3e9a4a7..ad5c8575 100755 --- a/services/distributeddataservice/adapter/communicator/BUILD.gn +++ b/services/distributeddataservice/adapter/communicator/BUILD.gn @@ -45,6 +45,7 @@ ohos_static_library("distributeddata_communicator_static") { "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/interfaces/include", "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/include", "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/interfaces/include/relational", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", ] cflags_cc = [ "-fvisibility=hidden" ] @@ -52,6 +53,7 @@ ohos_static_library("distributeddata_communicator_static") { deps = [ "../dfx:distributeddata_dfx_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", ] external_deps = [ @@ -59,6 +61,7 @@ ohos_static_library("distributeddata_communicator_static") { "device_manager:devicemanagersdk", "dsoftbus:softbus_client", "hiviewdfx_hilog_native:libhilog", + "access_token:libaccesstoken_sdk", ] subsystem_name = "distributeddatamgr" diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index b4eccc5e..2d24628f 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -16,6 +16,7 @@ #define LOG_TAG "DeviceManagerAdapter" #include "device_manager_adapter.h" #include +#include "accesstoken_kit.h" #include "log_print.h" #include "kvstore_utils.h" @@ -482,4 +483,15 @@ std::string DeviceManagerAdapter::CalcClientUuid(const std::string &appId, const return encryptedUuid; } +std::string DeviceManagerAdapter::GetEncryptedUuidByMeta(const StoreMetaData &meta) +{ + if (OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(meta.tokenId) == + OHOS::Security::AccessToken::TOKEN_HAP) { + auto uuid = CalcClientUuid(meta.appId, meta.deviceId); + return uuid; + } + auto uuid = CalcClientUuid(" ", meta.deviceId); + return uuid; +} + } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h index 52d4b99d..190eea8d 100644 --- a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h +++ b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h @@ -27,6 +27,7 @@ #include "dm_device_info.h" #include "task_scheduler.h" #include "lru_bucket.h" +#include "metadata/store_meta_data.h" namespace OHOS { namespace DistributedData { @@ -39,6 +40,7 @@ public: using AppDeviceChangeListener = OHOS::AppDistributedKv::AppDeviceChangeListener; using Status = OHOS::DistributedKv::Status; static DeviceManagerAdapter &GetInstance(); + using StoreMetaData = OHOS::DistributedData::StoreMetaData; void Init(); Status StartWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo); Status StopWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo); @@ -47,6 +49,7 @@ public: DeviceInfo GetDeviceInfo(const std::string &id); std::string GetUuidByNetworkId(const std::string &networkId); std::string GetUdidByNetworkId(const std::string &networkId); + std::string GetEncryptedUuidByMeta(const StoreMetaData &meta); std::string CalcClientUuid(const std::string &appId, const std::string &uuid); std::string ToUUID(const std::string &id); std::string ToUDID(const std::string &id); diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index cf38810e..e64459b5 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -19,7 +19,6 @@ #include #include -#include "accesstoken_kit.h" #include "auth_delegate.h" #include "auto_launch_export.h" #include "bootstrap.h" @@ -108,14 +107,8 @@ void KvStoreDataService::Initialize() meta.storeId = info.storeId; meta.user = info.userId; meta.deviceId = oriDevId; - MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta); - if (OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(meta.tokenId) == - OHOS::Security::AccessToken::TOKEN_HAP) { - auto uuid = DmAdapter::GetInstance().CalcClientUuid(info.appId, oriDevId); - return uuid; - } - auto uuid = DmAdapter::GetInstance().CalcClientUuid(" ", oriDevId); - return uuid; + MetaDataManager::GetInstance().LoadMeta(meta.GetKey(),meta); + return DmAdapter::GetInstance().GetEncryptedUuidByMeta(meta); }; DBConfig::SetTranslateToDeviceIdCallback(translateCall); } diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp index f678507d..61fdde59 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp @@ -52,7 +52,7 @@ int KVDBServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Message return -1; } - if (TRANS_HEAD > code || code > TRANS_BUTT || HANDLERS[code] == nullptr) { + if (TRANS_HEAD > code || code >= TRANS_BUTT || HANDLERS[code] == nullptr) { ZLOGE("not support code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); return -1; } diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index 5b4cb3e9..b833b4a7 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -29,7 +29,7 @@ namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; using system_clock = std::chrono::system_clock; using DMAdapter = DistributedData::DeviceManagerAdapter; -using DBKey = std::vector; +using DBKey = DistributedDB::Key; Upgrade &Upgrade::GetInstance() { @@ -108,14 +108,10 @@ Upgrade::DBStatus Upgrade::UpdateUuid(const StoreMeta &old, const StoreMeta &met return DBStatus::DB_ERROR; } kvStore->RemoveDeviceData(); - auto dbStatus = kvStore->UpdateKey([appId = meta.appId](const DBKey &originKey, DBKey &newKey) { - auto oriUuid = DMAdapter::GetInstance().GetLocalDevice().uuid; - auto newUuid = DMAdapter::GetInstance().CalcClientUuid(appId, oriUuid); - newKey.assign(originKey.begin(), originKey.end()); - uint32_t length = *(reinterpret_cast(&(*(newKey.end() - sizeof(uint32_t))))); - length = le32toh(length); - newKey.erase(newKey.begin(), newKey.begin() + length); - newKey.insert(newKey.begin(), newUuid.begin(), newUuid.end()); + auto uuid = DMAdapter::GetInstance().GetEncryptedUuidByMeta(meta); + auto dbStatus = kvStore->UpdateKey([uuid](const DBKey &originKey, DBKey &newKey) { + newKey = originKey; + memcpy_s(newKey.data(), newKey.size(), uuid.data(), uuid.size()); }); if (dbStatus != DBStatus::OK) { diff --git a/services/distributeddataservice/service/kvdb/upgrade.h b/services/distributeddataservice/service/kvdb/upgrade.h index 0c8a5a70..d641e835 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.h +++ b/services/distributeddataservice/service/kvdb/upgrade.h @@ -45,7 +45,6 @@ public: private: using AutoStore = std::unique_ptr>; AutoStore GetDBStore(const StoreMeta &meta, const std::vector &pwd); - static constexpr size_t MAX_DEV_KEY_LEN = 896; Exporter exporter_; Cleaner cleaner_; -- Gitee From 9f4f767e6dc66e102af6e0a673b01053427ab563 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Fri, 31 Mar 2023 09:34:12 +0800 Subject: [PATCH 034/152] xiugai Signed-off-by: ylq121 --- services/distributeddataservice/service/kvdb/upgrade.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index b833b4a7..3931581f 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -39,7 +39,7 @@ Upgrade &Upgrade::GetInstance() Upgrade::DBStatus Upgrade::UpdateStore(const StoreMeta &old, const StoreMeta &meta, const std::vector &pwd) { - if (old.version < meta.UUID_CHANGED_TAG && old.storeType == DEVICE_COLLABORATION) { + if (old.version < StoreMeta::UUID_CHANGED_TAG && old.storeType == DEVICE_COLLABORATION) { auto upStatus = Upgrade::GetInstance().UpdateUuid(old, meta, pwd); if (upStatus != DBStatus::OK) { return DBStatus::DB_ERROR; -- Gitee From a2ed3c755a23f943125f33af956d23bf7a444fd8 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Fri, 31 Mar 2023 09:43:42 +0800 Subject: [PATCH 035/152] xiugai Signed-off-by: ylq121 --- services/distributeddataservice/service/kvdb/upgrade.cpp | 1 - services/distributeddataservice/service/kvdb/upgrade.h | 1 - 2 files changed, 2 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index 3931581f..2eb43c0a 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -113,7 +113,6 @@ Upgrade::DBStatus Upgrade::UpdateUuid(const StoreMeta &old, const StoreMeta &met newKey = originKey; memcpy_s(newKey.data(), newKey.size(), uuid.data(), uuid.size()); }); - if (dbStatus != DBStatus::OK) { ZLOGE("fail to update Uuid, status:%{public}d", dbStatus); } diff --git a/services/distributeddataservice/service/kvdb/upgrade.h b/services/distributeddataservice/service/kvdb/upgrade.h index d641e835..92ffc35c 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.h +++ b/services/distributeddataservice/service/kvdb/upgrade.h @@ -45,7 +45,6 @@ public: private: using AutoStore = std::unique_ptr>; AutoStore GetDBStore(const StoreMeta &meta, const std::vector &pwd); - Exporter exporter_; Cleaner cleaner_; }; -- Gitee From 4193cb97f1bc403a60638cbf5ef8c01e4038044a Mon Sep 17 00:00:00 2001 From: renjiecui Date: Fri, 31 Mar 2023 15:42:50 +0800 Subject: [PATCH 036/152] fix gaojing Signed-off-by: renjiecui --- .../framework/test/serializable_test.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/services/distributeddataservice/framework/test/serializable_test.cpp b/services/distributeddataservice/framework/test/serializable_test.cpp index 0f767026..55364fd9 100644 --- a/services/distributeddataservice/framework/test/serializable_test.cpp +++ b/services/distributeddataservice/framework/test/serializable_test.cpp @@ -126,6 +126,20 @@ HWTEST_F(SerializableTest, GetNormalVal, TestSize.Level2) ASSERT_TRUE(normal == normal1) << normal1.name; } +/** +* @tc.name: Delete Serializable +* @tc.desc: can delete child class, but not delete parent class point. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Sven Wang +*/ +HWTEST_F(SerializableTest, DeleteSerializable, TestSize.Level2) +{ + ZLOGI("SerializableSuite GetVal begin."); + NormalEx *normalEx = new NormalEx(); + delete normalEx; +} + /** * @tc.name: SerializableSuiteGetMutilVal * @tc.desc: mutil value case. -- Gitee From 826b8c94ac5645c82d4b76b7a6c65c06f4525d16 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Fri, 31 Mar 2023 16:56:17 +0800 Subject: [PATCH 037/152] fix dmadapter Signed-off-by: zuojiangjiang --- .../adapter/communicator/src/device_manager_adapter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index 5862e35d..b5394fc1 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -311,7 +311,7 @@ void DeviceManagerAdapter::SaveDeviceInfo(const DeviceInfo &dvInfo, const Device DeviceInfo DeviceManagerAdapter::GetLocalDevice() { std::lock_guard lock(devInfoMutex_); - if (!localInfo_.uuid.empty()) { + if (!localInfo_.uuid.empty() && !localInfo_.udid.empty()) { return localInfo_; } @@ -324,7 +324,7 @@ DeviceInfo DeviceManagerAdapter::GetLocalDevice() auto networkId = std::string(info.networkId); auto uuid = GetUuidByNetworkId(networkId); auto udid = GetUdidByNetworkId(networkId); - if (uuid.empty() || udid.empty()) { + if (uuid.empty()) { return {}; } ZLOGI("[LocalDevice] uuid:%{public}s, name:%{public}s, type:%{public}d", -- Gitee From c0992f5041b4dbf0d8dc05260b4451ae3a9e5f89 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Fri, 31 Mar 2023 19:28:57 +0800 Subject: [PATCH 038/152] update Signed-off-by: htt1997 --- .../app/src/session_manager/upgrade_manager.cpp | 9 +++++---- .../app/src/session_manager/upgrade_manager.h | 2 +- .../framework/backuprule/backup_rule_manager.cpp | 4 ++-- .../framework/checker/checker_manager.cpp | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp index cf130bb4..c59353c4 100644 --- a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp +++ b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp @@ -51,8 +51,9 @@ void UpgradeManager::Init() CapMetaData UpgradeManager::GetCapability(const std::string &deviceId, bool &status) { status = true; - if (capabilityMap_.Contains(deviceId)) { - return capabilityMap_.Find(deviceId).second; + auto cap = capabilities_.Find(deviceId); + if (cap.first) { + return cap.second; } ZLOGI("load capability from meta"); CapMetaData capMetaData; @@ -60,7 +61,7 @@ CapMetaData UpgradeManager::GetCapability(const std::string &deviceId, bool &sta ZLOGD("cap key:%{public}s", Anonymous::Change(std::string(dbKey.begin(), dbKey.end())).c_str()); status = MetaDataManager::GetInstance().LoadMeta(std::string(dbKey.begin(), dbKey.end()), capMetaData); if (status) { - capabilityMap_.Insert(deviceId, capMetaData); + capabilities_.Insert(deviceId, capMetaData); } ZLOGI("device:%{public}s, version:%{public}d, insert:%{public}d", Anonymous::Change(deviceId).c_str(), capMetaData.version, status); @@ -75,7 +76,7 @@ bool UpgradeManager::InitLocalCapability() auto dbKey = CapMetaRow::GetKeyFor(localDeviceId); bool status = MetaDataManager::GetInstance().SaveMeta({ dbKey.begin(), dbKey.end() }, capMetaData); if (status) { - capabilityMap_.Insert(localDeviceId, capMetaData); + capabilities_.Insert(localDeviceId, capMetaData); } ZLOGI("put capability meta data ret %{public}d", status); return status; diff --git a/services/distributeddataservice/app/src/session_manager/upgrade_manager.h b/services/distributeddataservice/app/src/session_manager/upgrade_manager.h index 18c044b2..79cc6dba 100644 --- a/services/distributeddataservice/app/src/session_manager/upgrade_manager.h +++ b/services/distributeddataservice/app/src/session_manager/upgrade_manager.h @@ -37,7 +37,7 @@ public: private: bool InitLocalCapability(); - ConcurrentMap capabilityMap_ {}; + ConcurrentMap capabilities_ {}; }; } // namespace OHOS::DistributedData #endif // DISTRIBUTEDDATAMGR_UPGRADE_MANAGER_H diff --git a/services/distributeddataservice/framework/backuprule/backup_rule_manager.cpp b/services/distributeddataservice/framework/backuprule/backup_rule_manager.cpp index 22dc48a6..d967ad2f 100644 --- a/services/distributeddataservice/framework/backuprule/backup_rule_manager.cpp +++ b/services/distributeddataservice/framework/backuprule/backup_rule_manager.cpp @@ -37,8 +37,8 @@ void BackupRuleManager::LoadBackupRules(const std::vector &backupRu void BackupRuleManager::RegisterPlugin(const std::string &backupRule, std::function getter) { - getters_.ComputeIfAbsent(backupRule, [&getter](const auto &) { - return move(getter); + getters_.ComputeIfAbsent(backupRule, [&getter](const auto &) mutable { + return std::move(getter); }); } diff --git a/services/distributeddataservice/framework/checker/checker_manager.cpp b/services/distributeddataservice/framework/checker/checker_manager.cpp index 825d9404..8dbc1bb3 100644 --- a/services/distributeddataservice/framework/checker/checker_manager.cpp +++ b/services/distributeddataservice/framework/checker/checker_manager.cpp @@ -42,8 +42,8 @@ void CheckerManager::LoadCheckers(std::vector &checkers) void CheckerManager::RegisterPlugin(const std::string &checker, std::function getter) { - getters_.ComputeIfAbsent(checker, [&getter](const auto &) { - return move(getter); + getters_.ComputeIfAbsent(checker, [&getter](const auto &) mutable { + return std::move(getter); }); } -- Gitee From bbeecfebebec95f22f57551153989296482fe5a9 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Fri, 31 Mar 2023 19:38:29 +0800 Subject: [PATCH 039/152] xiugai Signed-off-by: ylq121 --- .../src/device_manager_adapter.cpp | 13 ------------ .../communicator/device_manager_adapter.h | 4 +--- .../app/src/kvstore_data_service.cpp | 3 ++- .../service/kvdb/upgrade.cpp | 21 ++++++++++++++++++- .../service/kvdb/upgrade.h | 5 +++++ 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index 2d24628f..9275b933 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -16,7 +16,6 @@ #define LOG_TAG "DeviceManagerAdapter" #include "device_manager_adapter.h" #include -#include "accesstoken_kit.h" #include "log_print.h" #include "kvstore_utils.h" @@ -482,16 +481,4 @@ std::string DeviceManagerAdapter::CalcClientUuid(const std::string &appId, const } return encryptedUuid; } - -std::string DeviceManagerAdapter::GetEncryptedUuidByMeta(const StoreMetaData &meta) -{ - if (OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(meta.tokenId) == - OHOS::Security::AccessToken::TOKEN_HAP) { - auto uuid = CalcClientUuid(meta.appId, meta.deviceId); - return uuid; - } - auto uuid = CalcClientUuid(" ", meta.deviceId); - return uuid; -} - } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h index 190eea8d..d4a72a19 100644 --- a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h +++ b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h @@ -27,7 +27,6 @@ #include "dm_device_info.h" #include "task_scheduler.h" #include "lru_bucket.h" -#include "metadata/store_meta_data.h" namespace OHOS { namespace DistributedData { @@ -40,7 +39,7 @@ public: using AppDeviceChangeListener = OHOS::AppDistributedKv::AppDeviceChangeListener; using Status = OHOS::DistributedKv::Status; static DeviceManagerAdapter &GetInstance(); - using StoreMetaData = OHOS::DistributedData::StoreMetaData; + void Init(); Status StartWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo); Status StopWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo); @@ -49,7 +48,6 @@ public: DeviceInfo GetDeviceInfo(const std::string &id); std::string GetUuidByNetworkId(const std::string &networkId); std::string GetUdidByNetworkId(const std::string &networkId); - std::string GetEncryptedUuidByMeta(const StoreMetaData &meta); std::string CalcClientUuid(const std::string &appId, const std::string &uuid); std::string ToUUID(const std::string &id); std::string ToUDID(const std::string &id); diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index e64459b5..eef53d3c 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -50,6 +50,7 @@ #include "user_delegate.h" #include "utils/block_integer.h" #include "utils/crypto.h" +#include "upgrade.h" namespace OHOS::DistributedKv { using namespace std::chrono; @@ -108,7 +109,7 @@ void KvStoreDataService::Initialize() meta.user = info.userId; meta.deviceId = oriDevId; MetaDataManager::GetInstance().LoadMeta(meta.GetKey(),meta); - return DmAdapter::GetInstance().GetEncryptedUuidByMeta(meta); + return Upgrade::GetInstance().GetEncryptedUuidByMeta(meta); }; DBConfig::SetTranslateToDeviceIdCallback(translateCall); } diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index 2eb43c0a..96e3f6de 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -24,6 +24,7 @@ #include "log_print.h" #include "metadata/meta_data_manager.h" #include "store_cache.h" +#include "accesstoken_kit.h" #include "directory_manager.h" namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; @@ -108,7 +109,7 @@ Upgrade::DBStatus Upgrade::UpdateUuid(const StoreMeta &old, const StoreMeta &met return DBStatus::DB_ERROR; } kvStore->RemoveDeviceData(); - auto uuid = DMAdapter::GetInstance().GetEncryptedUuidByMeta(meta); + auto uuid = GetEncryptedUuidByMeta(meta); auto dbStatus = kvStore->UpdateKey([uuid](const DBKey &originKey, DBKey &newKey) { newKey = originKey; memcpy_s(newKey.data(), newKey.size(), uuid.data(), uuid.size()); @@ -147,4 +148,22 @@ Upgrade::AutoStore Upgrade::GetDBStore(const StoreMeta &meta, const std::vector< }); return dbStore; } + +std::string Upgrade::GetEncryptedUuidByMeta(const StoreMeta &meta) +{ + std::string keyUuid = meta.appId + meta.storeId; + if(calcUuid_.Contains(keyUuid)){ + return calcUuid_[keyUuid]; + } + std::string uuid; + if (OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(meta.tokenId) == + OHOS::Security::AccessToken::TOKEN_HAP) { + uuid = DMAdapter::GetInstance().CalcClientUuid(meta.appId, meta.deviceId); + calcUuid_.Insert(keyUuid,uuid); + return uuid; + } + uuid = DMAdapter::GetInstance().CalcClientUuid(" ",meta.deviceId); + calcUuid_.Insert(keyUuid,uuid); + return uuid; +} } \ No newline at end of file diff --git a/services/distributeddataservice/service/kvdb/upgrade.h b/services/distributeddataservice/service/kvdb/upgrade.h index 92ffc35c..ab28d46e 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.h +++ b/services/distributeddataservice/service/kvdb/upgrade.h @@ -22,6 +22,8 @@ #include "kv_store_nb_delegate.h" #include "metadata/store_meta_data.h" #include "types.h" +#include "concurrent_map.h" + namespace OHOS::DistributedKv { class Upgrade { public: @@ -41,10 +43,13 @@ public: DBStatus ExportStore(const StoreMeta &old, const StoreMeta &meta); void UpdatePassword(const StoreMeta &meta, const std::vector &password); DBStatus UpdateUuid(const StoreMeta &old, const StoreMeta &meta, const std::vector &pwd); + API_EXPORT std::string GetEncryptedUuidByMeta(const StoreMeta &meta); private: using AutoStore = std::unique_ptr>; AutoStore GetDBStore(const StoreMeta &meta, const std::vector &pwd); + ConcurrentMap calcUuid_ {}; + Exporter exporter_; Cleaner cleaner_; }; -- Gitee From 54522d8844987cdb19528e757189ea4e17e63625 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Fri, 31 Mar 2023 19:42:51 +0800 Subject: [PATCH 040/152] xiugai Signed-off-by: ylq121 --- .../distributeddataservice/app/src/kvstore_data_service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index eef53d3c..50bef791 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -108,7 +108,7 @@ void KvStoreDataService::Initialize() meta.storeId = info.storeId; meta.user = info.userId; meta.deviceId = oriDevId; - MetaDataManager::GetInstance().LoadMeta(meta.GetKey(),meta); + MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta); return Upgrade::GetInstance().GetEncryptedUuidByMeta(meta); }; DBConfig::SetTranslateToDeviceIdCallback(translateCall); -- Gitee From 80a929e97d341c547c282f3c7c4e43219e833077 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Fri, 31 Mar 2023 19:45:07 +0800 Subject: [PATCH 041/152] xiugai Signed-off-by: ylq121 --- services/distributeddataservice/adapter/communicator/BUILD.gn | 3 --- 1 file changed, 3 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/BUILD.gn b/services/distributeddataservice/adapter/communicator/BUILD.gn index ad5c8575..c3e9a4a7 100755 --- a/services/distributeddataservice/adapter/communicator/BUILD.gn +++ b/services/distributeddataservice/adapter/communicator/BUILD.gn @@ -45,7 +45,6 @@ ohos_static_library("distributeddata_communicator_static") { "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/interfaces/include", "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/include", "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/interfaces/include/relational", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", ] cflags_cc = [ "-fvisibility=hidden" ] @@ -53,7 +52,6 @@ ohos_static_library("distributeddata_communicator_static") { deps = [ "../dfx:distributeddata_dfx_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", ] external_deps = [ @@ -61,7 +59,6 @@ ohos_static_library("distributeddata_communicator_static") { "device_manager:devicemanagersdk", "dsoftbus:softbus_client", "hiviewdfx_hilog_native:libhilog", - "access_token:libaccesstoken_sdk", ] subsystem_name = "distributeddatamgr" -- Gitee From 3d9417e9a8c639f35571dbcdd6c11781985529c2 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Fri, 31 Mar 2023 20:26:48 +0800 Subject: [PATCH 042/152] xiugai Signed-off-by: ylq121 --- .../service/kvdb/upgrade.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index 96e3f6de..4b662aca 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -112,7 +112,11 @@ Upgrade::DBStatus Upgrade::UpdateUuid(const StoreMeta &old, const StoreMeta &met auto uuid = GetEncryptedUuidByMeta(meta); auto dbStatus = kvStore->UpdateKey([uuid](const DBKey &originKey, DBKey &newKey) { newKey = originKey; - memcpy_s(newKey.data(), newKey.size(), uuid.data(), uuid.size()); + errno_t err = EOK; + err = memcpy_s(newKey.data(), newKey.size(), uuid.data(), uuid.size()); + if (err != EOK) { + ZLOGE("memcpy_s failed, err:%{public}d", err); + } }); if (dbStatus != DBStatus::OK) { ZLOGE("fail to update Uuid, status:%{public}d", dbStatus); @@ -152,18 +156,18 @@ Upgrade::AutoStore Upgrade::GetDBStore(const StoreMeta &meta, const std::vector< std::string Upgrade::GetEncryptedUuidByMeta(const StoreMeta &meta) { std::string keyUuid = meta.appId + meta.storeId; - if(calcUuid_.Contains(keyUuid)){ + if (calcUuid_.Contains(keyUuid)) { return calcUuid_[keyUuid]; } std::string uuid; if (OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(meta.tokenId) == OHOS::Security::AccessToken::TOKEN_HAP) { uuid = DMAdapter::GetInstance().CalcClientUuid(meta.appId, meta.deviceId); - calcUuid_.Insert(keyUuid,uuid); + calcUuid_.Insert(keyUuid, uuid); return uuid; } - uuid = DMAdapter::GetInstance().CalcClientUuid(" ",meta.deviceId); - calcUuid_.Insert(keyUuid,uuid); + uuid = DMAdapter::GetInstance().CalcClientUuid(" ", meta.deviceId); + calcUuid_.Insert(keyUuid, uuid); return uuid; } } \ No newline at end of file -- Gitee From f833529c89c87ce8767c927cba5f18ed22ac7d15 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Fri, 31 Mar 2023 20:32:35 +0800 Subject: [PATCH 043/152] xiugai Signed-off-by: ylq121 --- services/distributeddataservice/service/kvdb/upgrade.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/kvdb/upgrade.h b/services/distributeddataservice/service/kvdb/upgrade.h index ab28d46e..b6d83b02 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.h +++ b/services/distributeddataservice/service/kvdb/upgrade.h @@ -48,7 +48,7 @@ public: private: using AutoStore = std::unique_ptr>; AutoStore GetDBStore(const StoreMeta &meta, const std::vector &pwd); - ConcurrentMap calcUuid_ {}; + ConcurrentMap calcUuid_; Exporter exporter_; Cleaner cleaner_; -- Gitee From b517b316367b9b75462cba2938147a908436bc35 Mon Sep 17 00:00:00 2001 From: lihuihui Date: Mon, 3 Apr 2023 16:23:48 +0800 Subject: [PATCH 044/152] getrow Signed-off-by: lihuihui --- .../service/rdb/rdb_result_set_impl.cpp | 5 +++-- .../distributeddataservice/service/rdb/rdb_result_set_impl.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp b/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp index ec001c60..7f9897f0 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp @@ -296,9 +296,10 @@ int RdbResultSetImpl::IsColumnNull(int columnIndex, bool &isNull) return NativeRdb::E_OK; } -int RdbResultSetImpl::GetRow(NativeRdb::ValuesBucket &valuesBucket) + +int RdbResultSetImpl::GetRow(std::vector &columns, NativeRdb::ValuesBucket &valuesBucket) { - return NativeRdb::E_OK; + return NativeRdb::E_NOT_SUPPORT; } bool RdbResultSetImpl::IsClosed() const diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h index 7273781f..95bb77ed 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h @@ -50,7 +50,7 @@ public: int GetLong(int columnIndex, int64_t &value) override; int GetDouble(int columnIndex, double &value) override; int IsColumnNull(int columnIndex, bool &isNull) override; - int GetRow(NativeRdb::ValuesBucket &valuesBucket) override; + int GetRow(std::vector &columns, NativeRdb::ValuesBucket &valuesBucket) override; bool IsClosed() const override; int Close() override; -- Gitee From 5f749d300aafc63b40fe2f2ae0d0f130006a4f7b Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 4 Apr 2023 15:38:48 +0800 Subject: [PATCH 045/152] fix code smell Signed-off-by: htt1997 --- .../framework/test/serializable_test.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/services/distributeddataservice/framework/test/serializable_test.cpp b/services/distributeddataservice/framework/test/serializable_test.cpp index 55364fd9..95c0154b 100644 --- a/services/distributeddataservice/framework/test/serializable_test.cpp +++ b/services/distributeddataservice/framework/test/serializable_test.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #define LOG_TAG "SerializableTest" +#include #include "log_print.h" #include "serializable/serializable.h" #include "gtest/gtest.h" @@ -112,7 +113,7 @@ public: */ HWTEST_F(SerializableTest, GetNormalVal, TestSize.Level2) { - ZLOGI("SerializableSuite GetVal begin."); + ZLOGI("SerializableSuite GetNormalVal begin."); Normal normal; normal.name = "normal"; normal.count = -1; @@ -135,9 +136,9 @@ HWTEST_F(SerializableTest, GetNormalVal, TestSize.Level2) */ HWTEST_F(SerializableTest, DeleteSerializable, TestSize.Level2) { - ZLOGI("SerializableSuite GetVal begin."); - NormalEx *normalEx = new NormalEx(); - delete normalEx; + ZLOGI("SerializableSuite DeleteSerializable begin."); + ASSERT_FALSE(std::is_destructible::value); + ASSERT_TRUE(std::is_destructible::value); } /** @@ -149,7 +150,7 @@ HWTEST_F(SerializableTest, DeleteSerializable, TestSize.Level2) */ HWTEST_F(SerializableTest, GetMutilVal, TestSize.Level2) { - ZLOGI("SerializableSuite GetVal begin."); + ZLOGI("SerializableSuite GetMutilVal begin."); NormalEx normalEx; normalEx.normals = {Normal()}; normalEx.name = "normalEx"; -- Gitee From e63ac129c82a7f508985430827d19c9c0e629d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=9B=E5=93=B2=E9=BA=92?= Date: Wed, 5 Apr 2023 07:40:12 +0000 Subject: [PATCH 046/152] update services/distributeddataservice/service/directory/src/directory_manager.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 辛哲麒 --- .../service/directory/src/directory_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/directory/src/directory_manager.cpp b/services/distributeddataservice/service/directory/src/directory_manager.cpp index eba3ef89..bba79049 100644 --- a/services/distributeddataservice/service/directory/src/directory_manager.cpp +++ b/services/distributeddataservice/service/directory/src/directory_manager.cpp @@ -208,7 +208,7 @@ std::vector DirectoryManager::GetVersions() { std::vector versions; for (size_t i = 0; i < strategies_.size(); ++i) { - versions[i] = strategies_[i].version; + versions.push_back(strategies_[i].version); } return versions; } -- Gitee From 5f191562923de4954869b96be35d370c7fcae8bd Mon Sep 17 00:00:00 2001 From: lihuihui Date: Thu, 6 Apr 2023 20:05:32 +0800 Subject: [PATCH 047/152] getrow Signed-off-by: lihuihui --- .../distributeddataservice/service/rdb/irdb_result_set.h | 4 ++-- .../service/rdb/rdb_result_set_impl.cpp | 6 ------ .../service/rdb/rdb_result_set_impl.h | 1 - 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/service/rdb/irdb_result_set.h b/services/distributeddataservice/service/rdb/irdb_result_set.h index c2e81993..f2d9d6a8 100644 --- a/services/distributeddataservice/service/rdb/irdb_result_set.h +++ b/services/distributeddataservice/service/rdb/irdb_result_set.h @@ -17,10 +17,10 @@ #define DISTRIBUTED_RDB_IRDB_RESULT_SET_H #include "iremote_broker.h" -#include "result_set.h" +#include "abs_result_set.h namespace OHOS::DistributedRdb { -class IRdbResultSet : public NativeRdb::ResultSet, public IRemoteBroker { +class IRdbResultSet : public NativeRdb::AbsResultSet, public IRemoteBroker { public: using ColumnType = NativeRdb::ColumnType; virtual ~IRdbResultSet() = default; diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp b/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp index 7f9897f0..1be8c197 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp @@ -296,12 +296,6 @@ int RdbResultSetImpl::IsColumnNull(int columnIndex, bool &isNull) return NativeRdb::E_OK; } - -int RdbResultSetImpl::GetRow(std::vector &columns, NativeRdb::ValuesBucket &valuesBucket) -{ - return NativeRdb::E_NOT_SUPPORT; -} - bool RdbResultSetImpl::IsClosed() const { std::shared_lock lock(this->mutex_); diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h index 95bb77ed..efd276cb 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h @@ -50,7 +50,6 @@ public: int GetLong(int columnIndex, int64_t &value) override; int GetDouble(int columnIndex, double &value) override; int IsColumnNull(int columnIndex, bool &isNull) override; - int GetRow(std::vector &columns, NativeRdb::ValuesBucket &valuesBucket) override; bool IsClosed() const override; int Close() override; -- Gitee From 69d2d346172f779715305601890d187d0527760c Mon Sep 17 00:00:00 2001 From: lihuihui Date: Thu, 6 Apr 2023 20:06:38 +0800 Subject: [PATCH 048/152] getrow Signed-off-by: lihuihui --- .../distributeddataservice/service/rdb/rdb_result_set_impl.h | 1 - 1 file changed, 1 deletion(-) diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h index efd276cb..63e16f45 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h @@ -19,7 +19,6 @@ #include #include "rdb_result_set_stub.h" #include "distributeddb/result_set.h" -#include "values_bucket.h" namespace OHOS::DistributedRdb { class RdbResultSetImpl final : public RdbResultSetStub { -- Gitee From 8d7b39ff47c1b814b0dc9d33f93d442dca867d6a Mon Sep 17 00:00:00 2001 From: lihuihui Date: Thu, 6 Apr 2023 20:37:37 +0800 Subject: [PATCH 049/152] getrow Signed-off-by: lihuihui --- services/distributeddataservice/service/rdb/irdb_result_set.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/rdb/irdb_result_set.h b/services/distributeddataservice/service/rdb/irdb_result_set.h index f2d9d6a8..ac41eaf1 100644 --- a/services/distributeddataservice/service/rdb/irdb_result_set.h +++ b/services/distributeddataservice/service/rdb/irdb_result_set.h @@ -17,7 +17,7 @@ #define DISTRIBUTED_RDB_IRDB_RESULT_SET_H #include "iremote_broker.h" -#include "abs_result_set.h +#include "abs_result_set.h" namespace OHOS::DistributedRdb { class IRdbResultSet : public NativeRdb::AbsResultSet, public IRemoteBroker { -- Gitee From beceee10e1d407ce8bd25d9e684a353c545dc2d2 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Fri, 7 Apr 2023 09:31:49 +0800 Subject: [PATCH 050/152] fix yongli Signed-off-by: renjiecui --- .../app/test/unittest/uninstaller_test.cpp | 53 ------------------- 1 file changed, 53 deletions(-) delete mode 100644 services/distributeddataservice/app/test/unittest/uninstaller_test.cpp diff --git a/services/distributeddataservice/app/test/unittest/uninstaller_test.cpp b/services/distributeddataservice/app/test/unittest/uninstaller_test.cpp deleted file mode 100644 index dedd386d..00000000 --- a/services/distributeddataservice/app/test/unittest/uninstaller_test.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include "uninstaller/uninstaller.h" - -using namespace testing::ext; -using namespace OHOS::DistributedKv; - -class UninstallerTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void UninstallerTest::SetUpTestCase(void) -{} - -void UninstallerTest::TearDownTestCase(void) -{} - -void UninstallerTest::SetUp(void) -{} - -void UninstallerTest::TearDown(void) -{} - -/** - * @tc.name: Test001 - * @tc.desc: test get uninstaller instance. - * @tc.type: FUNC - * @tc.require: SR000DOGUN AR000DPSE9 - * @tc.author: hongbo - */ -HWTEST_F(UninstallerTest, Test001, TestSize.Level0) -{ - auto &unin = Uninstaller::GetInstance(); - unin.Init(nullptr); -} -- Gitee From dcc1cbed80942973ade0697f17e6d920ddea3f41 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Fri, 7 Apr 2023 10:09:17 +0800 Subject: [PATCH 051/152] fix yongli Signed-off-by: renjiecui --- .../distributeddataservice/app/test/BUILD.gn | 59 ------------------- 1 file changed, 59 deletions(-) diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 87d66c6d..628ff741 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -199,64 +199,6 @@ ohos_unittest("KvStoreFlowCtrlManagerTest") { part_name = "datamgr_service" } -ohos_unittest("KvStoreUninstallerTest") { - module_out_path = module_output_path - sources = [ - "../src/dump_helper.cpp", - "../src/feature_stub_impl.cpp", - "../src/kvstore_account_observer.cpp", - "../src/kvstore_data_service.cpp", - "../src/kvstore_device_listener.cpp", - "../src/kvstore_meta_manager.cpp", - "../src/security/security.cpp", - "../src/security/sensitive.cpp", - "../src/session_manager/route_head_handler_impl.cpp", - "../src/session_manager/session_manager.cpp", - "../src/session_manager/upgrade_manager.cpp", - "unittest/uninstaller_test.cpp", - ] - - configs = [ ":module_private_config" ] - - deps = [ - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src/checker:distributeddata_checker_static", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src/flowctrl_manager:distributeddata_flowctrl_static", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src/uninstaller:distributeddata_uninstaller_static", - "//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", - ] - - external_deps = [ - "ability_base:base", - "ability_base:want", - "access_token:libaccesstoken_sdk", - "c_utils:utils", - "dataclassification:data_transit_mgr", - "device_auth:deviceauth_sdk", - "hisysevent_native:libhisysevent", - "hitrace_native:hitrace_meter", - "hitrace_native:libhitracechain", - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr:samgr_proxy", - ] - - if (datamgr_service_power) { - external_deps += [ - "battery_manager:batterysrv_client", - "power_manager:powermgr_client", - ] - } - part_name = "datamgr_service" -} - ############################################################################### group("unittest") { @@ -266,7 +208,6 @@ group("unittest") { deps += [ ":KvStoreDataServiceTest", ":KvStoreFlowCtrlManagerTest", - ":KvStoreUninstallerTest", ":SessionManagerTest", ] } -- Gitee From 8c464b9eb3bdb14b751df0b93379d023f677b278 Mon Sep 17 00:00:00 2001 From: lihuihui Date: Fri, 7 Apr 2023 14:49:07 +0800 Subject: [PATCH 052/152] getrow Signed-off-by: lihuihui --- .../distributeddataservice/service/rdb/irdb_result_set.h | 4 ++-- .../service/rdb/rdb_result_set_impl.cpp | 5 +++++ .../distributeddataservice/service/rdb/rdb_result_set_impl.h | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/rdb/irdb_result_set.h b/services/distributeddataservice/service/rdb/irdb_result_set.h index ac41eaf1..c2e81993 100644 --- a/services/distributeddataservice/service/rdb/irdb_result_set.h +++ b/services/distributeddataservice/service/rdb/irdb_result_set.h @@ -17,10 +17,10 @@ #define DISTRIBUTED_RDB_IRDB_RESULT_SET_H #include "iremote_broker.h" -#include "abs_result_set.h" +#include "result_set.h" namespace OHOS::DistributedRdb { -class IRdbResultSet : public NativeRdb::AbsResultSet, public IRemoteBroker { +class IRdbResultSet : public NativeRdb::ResultSet, public IRemoteBroker { public: using ColumnType = NativeRdb::ColumnType; virtual ~IRdbResultSet() = default; diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp b/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp index 1be8c197..58a6aad9 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp @@ -296,6 +296,11 @@ int RdbResultSetImpl::IsColumnNull(int columnIndex, bool &isNull) return NativeRdb::E_OK; } +int RdbResultSetImpl::GetRow(std::vector &columnNames, NativeRdb::ValuesBucket &valuesBucket) +{ + return NativeRdb::E_OK; +} + bool RdbResultSetImpl::IsClosed() const { std::shared_lock lock(this->mutex_); diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h index 63e16f45..fc83e136 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h @@ -19,6 +19,7 @@ #include #include "rdb_result_set_stub.h" #include "distributeddb/result_set.h" +#include "values_bucket.h" namespace OHOS::DistributedRdb { class RdbResultSetImpl final : public RdbResultSetStub { @@ -49,6 +50,7 @@ public: int GetLong(int columnIndex, int64_t &value) override; int GetDouble(int columnIndex, double &value) override; int IsColumnNull(int columnIndex, bool &isNull) override; + int GetRow(std::vector &columnNames, NativeRdb::ValuesBucket &valuesBucket) override; bool IsClosed() const override; int Close() override; -- Gitee From 9b50ff01af3439180648f6668ea0e9c0f0ed9a23 Mon Sep 17 00:00:00 2001 From: xxlight Date: Wed, 12 Apr 2023 12:01:44 +0800 Subject: [PATCH 053/152] Descripiton: fix gn visibility problem Issue: I6UZYW Signed-off-by: xxlight Change-Id: Id62789abfc80d5b25e40292c551cdf5e5087d09d --- services/distributeddataservice/adapter/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/adapter/BUILD.gn b/services/distributeddataservice/adapter/BUILD.gn index 34abe79d..c1e309f1 100644 --- a/services/distributeddataservice/adapter/BUILD.gn +++ b/services/distributeddataservice/adapter/BUILD.gn @@ -24,7 +24,7 @@ config("distributeddata_adapter_private_config") { } config("distributeddata_adapter_public_config") { - visibility = [ "//foundation/distributeddatamgr/datamgr_service:*" ] + visibility = [ ":*" ] include_dirs = [ "include/log", -- Gitee From 06778f585620876b3aab9d230160224477748492 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Wed, 12 Apr 2023 15:59:03 +0800 Subject: [PATCH 054/152] xiugai Signed-off-by: ylq121 --- .../service/bootstrap/src/bootstrap.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp index 7d39b94f..d1dd6f16 100644 --- a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp +++ b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp @@ -58,7 +58,10 @@ void Bootstrap::LoadComponents() if (comp.lib.empty()) { continue; } - + char path[PATH_MAX] = { 0 }; + if (realpath(comp.lib.c_str(), path) == NULL) { + continue; + } // no need to close the component, so we don't keep the handles auto handle = dlopen(comp.lib.c_str(), RTLD_LAZY); if (handle == nullptr) { -- Gitee From 336b9bfe8fa7e1a8b7c7839c8093d277aee5a3b5 Mon Sep 17 00:00:00 2001 From: lihuihui Date: Wed, 12 Apr 2023 17:21:42 +0800 Subject: [PATCH 055/152] getrow Signed-off-by: lihuihui --- .../distributeddataservice/service/rdb/irdb_result_set.h | 4 ++-- .../service/rdb/rdb_result_set_impl.cpp | 5 ----- .../distributeddataservice/service/rdb/rdb_result_set_impl.h | 2 -- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/service/rdb/irdb_result_set.h b/services/distributeddataservice/service/rdb/irdb_result_set.h index c2e81993..5926784c 100644 --- a/services/distributeddataservice/service/rdb/irdb_result_set.h +++ b/services/distributeddataservice/service/rdb/irdb_result_set.h @@ -17,10 +17,10 @@ #define DISTRIBUTED_RDB_IRDB_RESULT_SET_H #include "iremote_broker.h" -#include "result_set.h" +#include "remote_result_set.h" namespace OHOS::DistributedRdb { -class IRdbResultSet : public NativeRdb::ResultSet, public IRemoteBroker { +class IRdbResultSet : public NativeRdb::RemoteResultSet, public IRemoteBroker { public: using ColumnType = NativeRdb::ColumnType; virtual ~IRdbResultSet() = default; diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp b/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp index 58a6aad9..1be8c197 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.cpp @@ -296,11 +296,6 @@ int RdbResultSetImpl::IsColumnNull(int columnIndex, bool &isNull) return NativeRdb::E_OK; } -int RdbResultSetImpl::GetRow(std::vector &columnNames, NativeRdb::ValuesBucket &valuesBucket) -{ - return NativeRdb::E_OK; -} - bool RdbResultSetImpl::IsClosed() const { std::shared_lock lock(this->mutex_); diff --git a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h index fc83e136..63e16f45 100644 --- a/services/distributeddataservice/service/rdb/rdb_result_set_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_result_set_impl.h @@ -19,7 +19,6 @@ #include #include "rdb_result_set_stub.h" #include "distributeddb/result_set.h" -#include "values_bucket.h" namespace OHOS::DistributedRdb { class RdbResultSetImpl final : public RdbResultSetStub { @@ -50,7 +49,6 @@ public: int GetLong(int columnIndex, int64_t &value) override; int GetDouble(int columnIndex, double &value) override; int IsColumnNull(int columnIndex, bool &isNull) override; - int GetRow(std::vector &columnNames, NativeRdb::ValuesBucket &valuesBucket) override; bool IsClosed() const override; int Close() override; -- Gitee From 620245651fe27160687db334a120bdbde1d100d5 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Mon, 17 Apr 2023 20:27:26 +0800 Subject: [PATCH 056/152] correct invalid path Signed-off-by: ylq121 --- .../distributeddataservice/service/bootstrap/src/bootstrap.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp index d1dd6f16..96172649 100644 --- a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp +++ b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp @@ -63,7 +63,7 @@ void Bootstrap::LoadComponents() continue; } // no need to close the component, so we don't keep the handles - auto handle = dlopen(comp.lib.c_str(), RTLD_LAZY); + auto handle = dlopen(path, RTLD_LAZY); if (handle == nullptr) { ZLOGE("dlopen(%{public}s) failed(%{public}d)!", comp.lib.c_str(), errno); continue; -- Gitee From df7d5807d9d027b0792f079db1c70426d6239be5 Mon Sep 17 00:00:00 2001 From: ht Date: Wed, 19 Apr 2023 07:34:02 +0000 Subject: [PATCH 057/152] build:adapter DM new permissions Signed-off-by: ht --- services/distributeddataservice/app/distributed_data.cfg | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/distributed_data.cfg b/services/distributeddataservice/app/distributed_data.cfg index 494bc79b..a42d9dc4 100644 --- a/services/distributeddataservice/app/distributed_data.cfg +++ b/services/distributeddataservice/app/distributed_data.cfg @@ -30,7 +30,8 @@ "apl" : "system_basic", "permission" : [ "ohos.permission.DISTRIBUTED_DATASYNC", - "ohos.permission.MANAGE_LOCAL_ACCOUNTS" + "ohos.permission.MANAGE_LOCAL_ACCOUNTS", + "ohos.permission.ACCESS_SERVICE_DM" ] } ] -- Gitee From 5ab16fd5186fd0008a214d5d04ee80f105bb758f Mon Sep 17 00:00:00 2001 From: srr101 Date: Thu, 20 Apr 2023 10:48:32 +0800 Subject: [PATCH 058/152] =?UTF-8?q?=E5=88=B7=E5=B1=8F=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: srr101 --- .../service/data_share/permission_proxy.cpp | 4 ++-- .../distributeddataservice/service/data_share/rdb_adaptor.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/data_share/permission_proxy.cpp b/services/distributeddataservice/service/data_share/permission_proxy.cpp index 76806bfa..c6c90df9 100644 --- a/services/distributeddataservice/service/data_share/permission_proxy.cpp +++ b/services/distributeddataservice/service/data_share/permission_proxy.cpp @@ -43,7 +43,7 @@ PermissionProxy::PermissionState PermissionProxy::QueryWritePermission(uint32_t for (auto const &item : bundleInfo.extensionInfos) { if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { if (item.writePermission.empty()) { - ZLOGW("WritePermission is empty! BundleName is %{public}s, tokenId is %{public}x", + ZLOGD("WritePermission is empty! BundleName is %{public}s, tokenId is %{public}x", bundleInfo.name.c_str(), tokenId); return PermissionState::NOT_FIND; } @@ -64,7 +64,7 @@ PermissionProxy::PermissionState PermissionProxy::QueryReadPermission(uint32_t t for (auto const &item : bundleInfo.extensionInfos) { if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { if (item.readPermission.empty()) { - ZLOGW("ReadPermission is empty! BundleName is %{public}s, tokenId is %{public}x", + ZLOGD("ReadPermission is empty! BundleName is %{public}s, tokenId is %{public}x", bundleInfo.name.c_str(), tokenId); return PermissionState::NOT_FIND; } diff --git a/services/distributeddataservice/service/data_share/rdb_adaptor.cpp b/services/distributeddataservice/service/data_share/rdb_adaptor.cpp index 9f4589fb..82ba6da6 100644 --- a/services/distributeddataservice/service/data_share/rdb_adaptor.cpp +++ b/services/distributeddataservice/service/data_share/rdb_adaptor.cpp @@ -77,7 +77,7 @@ RdbDelegate::RdbDelegate(const StoreMetaData &meta, int &errCode) RdbDelegate::~RdbDelegate() { - ZLOGI("destroy"); + ZLOGD("destroy"); } int64_t RdbDelegate::Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket) -- Gitee From b566b83b5d81f2508a54ca45c91caa0f1d42aef1 Mon Sep 17 00:00:00 2001 From: srr101 Date: Fri, 21 Apr 2023 14:52:56 +0800 Subject: [PATCH 059/152] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=AD=89=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: srr101 --- .../service/data_share/permission_proxy.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/data_share/permission_proxy.cpp b/services/distributeddataservice/service/data_share/permission_proxy.cpp index c6c90df9..76806bfa 100644 --- a/services/distributeddataservice/service/data_share/permission_proxy.cpp +++ b/services/distributeddataservice/service/data_share/permission_proxy.cpp @@ -43,7 +43,7 @@ PermissionProxy::PermissionState PermissionProxy::QueryWritePermission(uint32_t for (auto const &item : bundleInfo.extensionInfos) { if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { if (item.writePermission.empty()) { - ZLOGD("WritePermission is empty! BundleName is %{public}s, tokenId is %{public}x", + ZLOGW("WritePermission is empty! BundleName is %{public}s, tokenId is %{public}x", bundleInfo.name.c_str(), tokenId); return PermissionState::NOT_FIND; } @@ -64,7 +64,7 @@ PermissionProxy::PermissionState PermissionProxy::QueryReadPermission(uint32_t t for (auto const &item : bundleInfo.extensionInfos) { if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { if (item.readPermission.empty()) { - ZLOGD("ReadPermission is empty! BundleName is %{public}s, tokenId is %{public}x", + ZLOGW("ReadPermission is empty! BundleName is %{public}s, tokenId is %{public}x", bundleInfo.name.c_str(), tokenId); return PermissionState::NOT_FIND; } -- Gitee From ab962deef2bc778a8dc58467353a0704e4a91d20 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 19 Apr 2023 15:22:17 +0800 Subject: [PATCH 060/152] refactor:stub serialize optimization Signed-off-by: htt1997 --- .../data_share/data_share_service_stub.cpp | 49 +++--- .../service/object/object_service_stub.cpp | 53 +++--- .../service/rdb/rdb_service_stub.cpp | 152 ++++++++++-------- 3 files changed, 136 insertions(+), 118 deletions(-) diff --git a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp index ba7fe7c7..a388f8cc 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp @@ -21,6 +21,7 @@ #include "ishared_result_set.h" #include "itypes_util.h" #include "log_print.h" +#include "utils/anonymous.h" namespace OHOS { namespace DataShare { @@ -40,13 +41,14 @@ int32_t DataShareServiceStub::OnRemoteInsert(MessageParcel &data, MessageParcel std::string uri; DataShareValuesBucket bucket; if (!ITypesUtil::Unmarshal(data, uri, bucket.valuesMap)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal uri:%{public}s bucket size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(), + bucket.valuesMap.size()); + return IPC_STUB_INVALID_DATA_ERR; } int32_t status = Insert(uri, bucket); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("OnRemoteInsert fail %{public}d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -57,13 +59,14 @@ int32_t DataShareServiceStub::OnRemoteUpdate(MessageParcel &data, MessageParcel DataSharePredicates predicate; DataShareValuesBucket bucket; if (!ITypesUtil::Unmarshal(data, uri, predicate, bucket.valuesMap)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal uri:%{public}s bucket size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(), + bucket.valuesMap.size()); + return IPC_STUB_INVALID_DATA_ERR; } int32_t status = Update(uri, predicate, bucket); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("OnRemoteUpdate fail %d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -73,13 +76,13 @@ int32_t DataShareServiceStub::OnRemoteDelete(MessageParcel &data, MessageParcel std::string uri; DataSharePredicates predicate; if (!ITypesUtil::Unmarshal(data, uri, predicate)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal uri:%{public}s", DistributedData::Anonymous::Change(uri).c_str()); + return IPC_STUB_INVALID_DATA_ERR; } int32_t status = Delete(uri, predicate); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("OnRemoteDelete fail %d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -90,15 +93,15 @@ int32_t DataShareServiceStub::OnRemoteQuery(MessageParcel &data, MessageParcel & DataSharePredicates predicate; std::vector columns; if (!ITypesUtil::Unmarshal(data, uri, predicate, columns)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal uri:%{public}s columns size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(), + columns.size()); + return IPC_STUB_INVALID_DATA_ERR; } - int errCode = 0; - auto result = ISharedResultSet::WriteToParcel(Query(uri, predicate, columns, errCode), reply); - reply.WriteInt32(errCode); - if (result == nullptr) { - ZLOGW("!resultSet->Marshalling(reply)"); - return -1; + int status = 0; + auto result = ISharedResultSet::WriteToParcel(Query(uri, predicate, columns, status), reply); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } diff --git a/services/distributeddataservice/service/object/object_service_stub.cpp b/services/distributeddataservice/service/object/object_service_stub.cpp index cf142a29..a7118b31 100644 --- a/services/distributeddataservice/service/object/object_service_stub.cpp +++ b/services/distributeddataservice/service/object/object_service_stub.cpp @@ -21,6 +21,7 @@ #include "itypes_util.h" #include "log_print.h" +#include "utils/anonymous.h" namespace OHOS::DistributedObject { using namespace DistributedKv; @@ -32,17 +33,19 @@ int32_t ObjectServiceStub::ObjectStoreSaveOnRemote(MessageParcel &data, MessageP std::map> objectData; sptr obj; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, deviceId, objectData, obj)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s deviceId:%{public}s objectData size:%{public}zu", + sessionId.c_str(), bundleName.c_str(), DistributedData::Anonymous::Change(deviceId).c_str(), + objectData.size()); + return IPC_STUB_INVALID_DATA_ERR; } if (obj == nullptr) { ZLOGW("callback null"); return -1; } int32_t status = ObjectStoreSave(bundleName, sessionId, deviceId, objectData, obj); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("ObjectStoreSaveOnRemote fail %{public}d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -53,17 +56,17 @@ int32_t ObjectServiceStub::ObjectStoreRevokeSaveOnRemote(MessageParcel &data, Me std::string bundleName; sptr obj; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", sessionId.c_str(), bundleName.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } if (obj == nullptr) { ZLOGW("callback null"); return -1; } int32_t status = ObjectStoreRevokeSave(bundleName, sessionId, obj); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("ObjectStoreRevokeSaveOnRemote fail %{public}d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -74,17 +77,17 @@ int32_t ObjectServiceStub::ObjectStoreRetrieveOnRemote(MessageParcel &data, Mess std::string bundleName; sptr obj; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", sessionId.c_str(), bundleName.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } if (obj == nullptr) { ZLOGW("callback null"); return -1; } int32_t status = ObjectStoreRetrieve(bundleName, sessionId, obj); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("ObjectStoreRetrieveOnRemote fail %{public}d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -95,17 +98,17 @@ int32_t ObjectServiceStub::OnSubscribeRequest(MessageParcel &data, MessageParcel std::string bundleName; sptr obj; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", sessionId.c_str(), bundleName.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } if (obj == nullptr) { ZLOGW("callback null"); return -1; } int32_t status = RegisterDataObserver(bundleName, sessionId, obj); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("OnSubscribeRequest fail %{public}d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } @@ -115,13 +118,13 @@ int32_t ObjectServiceStub::OnUnsubscribeRequest(MessageParcel &data, MessageParc std::string sessionId; std::string bundleName; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId)) { - ZLOGW("read device list failed."); - return -1; + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", sessionId.c_str(), bundleName.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } int32_t status = UnregisterDataChangeObserver(bundleName, sessionId); - if (!reply.WriteInt32(static_cast(status))) { - ZLOGE("OnSubscribeRequest fail %{public}d", static_cast(status)); - return -1; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } return 0; } diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index fc8a5472..e46ef484 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -19,6 +19,7 @@ #include #include "log_print.h" #include "itypes_util.h" +#include "utils/anonymous.h" namespace OHOS::DistributedRdb { int32_t RdbServiceStub::OnRemoteObtainDistributedTableName(MessageParcel &data, MessageParcel &reply) @@ -26,12 +27,15 @@ int32_t RdbServiceStub::OnRemoteObtainDistributedTableName(MessageParcel &data, std::string device; std::string table; if (!ITypesUtil::Unmarshal(data, device, table)) { - ZLOGE("read from message parcel failed"); - reply.WriteString(""); - return RDB_OK; + ZLOGE("Unmarshal device:%{public}s table:%{public}s", device.c_str(), table.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } - reply.WriteString(ObtainDistributedTableName(device, table)); + std::string distributedTableName = ObtainDistributedTableName(device, table); + if (!ITypesUtil::Marshal(reply, distributedTableName)) { + ZLOGE("Marshal distributedTableName:%{public}s", distributedTableName.c_str()); + return IPC_STUB_WRITE_PARCEL_ERR; + } return RDB_OK; } @@ -39,23 +43,16 @@ int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel { RdbSyncerParam param; sptr notifier; - if (!ITypesUtil::Unmarshal(data, param, notifier)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; - } - if (notifier == nullptr) { - ZLOGE("notifier is null"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; - } - if (InitNotifier(param, notifier) != RDB_OK) { - ZLOGE("init notifier failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; - } - ZLOGI("success"); - reply.WriteInt32(RDB_OK); + if (!ITypesUtil::Unmarshal(data, param, notifier) || notifier == nullptr) { + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + auto status = InitNotifier(param, notifier); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } return RDB_OK; } @@ -64,12 +61,16 @@ int32_t RdbServiceStub::OnRemoteSetDistributedTables(MessageParcel &data, Messag RdbSyncerParam param; std::vector tables; if (!ITypesUtil::Unmarshal(data, param, tables)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables size:%{public}zu", + param.bundleName_.c_str(), param.storeName_.c_str(), tables.size()); + return IPC_STUB_INVALID_DATA_ERR; } - reply.WriteInt32(SetDistributedTables(param, tables)); + auto status = SetDistributedTables(param, tables); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } return RDB_OK; } @@ -79,19 +80,16 @@ int32_t RdbServiceStub::OnRemoteDoSync(MessageParcel &data, MessageParcel &reply SyncOption option {}; RdbPredicates predicates; if (!ITypesUtil::Unmarshal(data, param, option, predicates)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str(), predicates.table_.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } SyncResult result; - if (DoSync(param, option, predicates, result) != RDB_OK) { - reply.WriteInt32(RDB_ERROR); - return RDB_OK; - } - if (!ITypesUtil::Marshal(reply, result)) { - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + auto status = DoSync(param, option, predicates, result); + if (!ITypesUtil::Marshal(reply, status, result)) { + ZLOGE("Marshal status:0x%{public}x result size:%{public}zu", status, result.size()); + return IPC_STUB_WRITE_PARCEL_ERR; } return RDB_OK; } @@ -103,12 +101,16 @@ int32_t RdbServiceStub::OnRemoteDoAsync(MessageParcel &data, MessageParcel &repl SyncOption option {}; RdbPredicates predicates; if (!ITypesUtil::Unmarshal(data, param, seqNum, option, predicates)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s seqNum:%{public}u tables:%{public}s", + param.bundleName_.c_str(), param.storeName_.c_str(), seqNum, predicates.table_.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } - reply.WriteInt32(DoAsync(param, seqNum, option, predicates)); + auto status = DoAsync(param, seqNum, option, predicates); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } return RDB_OK; } @@ -116,11 +118,16 @@ int32_t RdbServiceStub::OnRemoteDoSubscribe(MessageParcel &data, MessageParcel & { RdbSyncerParam param; if (!ITypesUtil::Unmarshal(data, param)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + + auto status = DoSubscribe(param); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } - reply.WriteInt32(DoSubscribe(param)); return RDB_OK; } @@ -128,11 +135,16 @@ int32_t RdbServiceStub::OnRemoteDoUnSubscribe(MessageParcel &data, MessageParcel { RdbSyncerParam param; if (!ITypesUtil::Unmarshal(data, param)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + + auto status = DoUnSubscribe(param); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } - reply.WriteInt32(DoUnSubscribe(param)); return RDB_OK; } @@ -143,19 +155,19 @@ int32_t RdbServiceStub::OnRemoteDoRemoteQuery(MessageParcel& data, MessageParcel std::string sql; std::vector selectionArgs; if (!ITypesUtil::Unmarshal(data, param, device, sql, selectionArgs)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s device:%{public}s sql:%{public}s " + "selectionArgs size:%{public}zu", param.bundleName_.c_str(), param.storeName_.c_str(), + DistributedData::Anonymous::Change(device).c_str(), + DistributedData::Anonymous::Change(sql).c_str(), selectionArgs.size()); + return IPC_STUB_INVALID_DATA_ERR; } sptr resultSet; - int32_t status = RemoteQuery(param, device, sql, selectionArgs, resultSet); - if (status != RDB_OK) { - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + auto status = RemoteQuery(param, device, sql, selectionArgs, resultSet); + if (!ITypesUtil::Marshal(reply, status, resultSet)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } - reply.WriteInt32(RDB_OK); - reply.WriteRemoteObject(resultSet); return RDB_OK; } @@ -188,17 +200,18 @@ int32_t RdbServiceStub::OnRemoteDoCreateTable(MessageParcel &data, MessageParcel std::string writePermission; std::string readPermission; if (!ITypesUtil::Unmarshal(data, param, writePermission, readPermission)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s writePermission:%{public}s " + "readPermission:%{public}s", param.bundleName_.c_str(), param.storeName_.c_str(), + DistributedData::Anonymous::Change(writePermission).c_str(), + DistributedData::Anonymous::Change(readPermission).c_str()); + return IPC_STUB_INVALID_DATA_ERR; } int32_t status = CreateRDBTable(param, writePermission, readPermission); - if (status != RDB_OK) { - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } - reply.WriteInt32(RDB_OK); return RDB_OK; } @@ -206,17 +219,16 @@ int32_t RdbServiceStub::OnRemoteDoDestroyTable(MessageParcel &data, MessageParce { RdbSyncerParam param; if (!ITypesUtil::Unmarshal(data, param)) { - ZLOGE("read from message parcel failed"); - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str()); + return IPC_STUB_INVALID_DATA_ERR; } int32_t status = DestroyRDBTable(param); - if (status != RDB_OK) { - reply.WriteInt32(RDB_ERROR); - return RDB_OK; + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; } - reply.WriteInt32(RDB_OK); return RDB_OK; } } // namespace OHOS::DistributedRdb -- Gitee From f6e2fae7faf0f5e8c7fe334181f6f8495ba23d82 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Mon, 24 Apr 2023 10:03:16 +0800 Subject: [PATCH 061/152] rollback Signed-off-by: ylq121 --- .../service/bootstrap/src/bootstrap.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp index 96172649..741f18b9 100644 --- a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp +++ b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp @@ -58,12 +58,8 @@ void Bootstrap::LoadComponents() if (comp.lib.empty()) { continue; } - char path[PATH_MAX] = { 0 }; - if (realpath(comp.lib.c_str(), path) == NULL) { - continue; - } // no need to close the component, so we don't keep the handles - auto handle = dlopen(path, RTLD_LAZY); + auto handle = dlopen(comp.lib.c_str(), RTLD_LAZY); if (handle == nullptr) { ZLOGE("dlopen(%{public}s) failed(%{public}d)!", comp.lib.c_str(), errno); continue; -- Gitee From 323e6d8f6cb6439af42bfaee718b656c78aee70f Mon Sep 17 00:00:00 2001 From: htt1997 Date: Mon, 24 Apr 2023 16:22:11 +0800 Subject: [PATCH 062/152] fix:add anonymous Signed-off-by: htt1997 --- .../service/object/object_service_stub.cpp | 16 ++++++++++------ .../service/rdb/rdb_service_stub.cpp | 3 ++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/service/object/object_service_stub.cpp b/services/distributeddataservice/service/object/object_service_stub.cpp index a7118b31..9a5281bf 100644 --- a/services/distributeddataservice/service/object/object_service_stub.cpp +++ b/services/distributeddataservice/service/object/object_service_stub.cpp @@ -34,8 +34,8 @@ int32_t ObjectServiceStub::ObjectStoreSaveOnRemote(MessageParcel &data, MessageP sptr obj; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, deviceId, objectData, obj)) { ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s deviceId:%{public}s objectData size:%{public}zu", - sessionId.c_str(), bundleName.c_str(), DistributedData::Anonymous::Change(deviceId).c_str(), - objectData.size()); + DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(), + DistributedData::Anonymous::Change(deviceId).c_str(), objectData.size()); return IPC_STUB_INVALID_DATA_ERR; } if (obj == nullptr) { @@ -56,7 +56,8 @@ int32_t ObjectServiceStub::ObjectStoreRevokeSaveOnRemote(MessageParcel &data, Me std::string bundleName; sptr obj; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj)) { - ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", sessionId.c_str(), bundleName.c_str()); + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", + DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str()); return IPC_STUB_INVALID_DATA_ERR; } if (obj == nullptr) { @@ -77,7 +78,8 @@ int32_t ObjectServiceStub::ObjectStoreRetrieveOnRemote(MessageParcel &data, Mess std::string bundleName; sptr obj; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj)) { - ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", sessionId.c_str(), bundleName.c_str()); + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", + DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str()); return IPC_STUB_INVALID_DATA_ERR; } if (obj == nullptr) { @@ -98,7 +100,8 @@ int32_t ObjectServiceStub::OnSubscribeRequest(MessageParcel &data, MessageParcel std::string bundleName; sptr obj; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj)) { - ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", sessionId.c_str(), bundleName.c_str()); + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", + DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str()); return IPC_STUB_INVALID_DATA_ERR; } if (obj == nullptr) { @@ -118,7 +121,8 @@ int32_t ObjectServiceStub::OnUnsubscribeRequest(MessageParcel &data, MessageParc std::string sessionId; std::string bundleName; if (!ITypesUtil::Unmarshal(data, bundleName, sessionId)) { - ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", sessionId.c_str(), bundleName.c_str()); + ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s", + DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str()); return IPC_STUB_INVALID_DATA_ERR; } int32_t status = UnregisterDataChangeObserver(bundleName, sessionId); diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index e46ef484..931792ac 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -27,7 +27,8 @@ int32_t RdbServiceStub::OnRemoteObtainDistributedTableName(MessageParcel &data, std::string device; std::string table; if (!ITypesUtil::Unmarshal(data, device, table)) { - ZLOGE("Unmarshal device:%{public}s table:%{public}s", device.c_str(), table.c_str()); + ZLOGE("Unmarshal device:%{public}s table:%{public}s", DistributedData::Anonymous::Change(device).c_str(), + table.c_str()); return IPC_STUB_INVALID_DATA_ERR; } -- Gitee From ba8ce4e38929d690794573d0cb18f89bf2b96cf4 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 26 Apr 2023 12:03:41 +0800 Subject: [PATCH 063/152] add cloud sync framework and local store interface Signed-off-by: Sven Wang --- .../distributeddataservice/framework/BUILD.gn | 6 + .../framework/cloud/asset_loader.cpp | 27 +++ .../framework/cloud/cloud_db.cpp | 107 +++++++++ .../framework/cloud/cloud_info.cpp | 127 ++++++++++ .../framework/cloud/cloud_server.cpp | 52 ++++ .../framework/cloud/schema_meta.cpp | 93 +++++++ .../framework/include/cloud/asset_loader.h | 28 +++ .../framework/include/cloud/cloud_db.h | 51 ++++ .../framework/include/cloud/cloud_info.h | 58 +++++ .../framework/include/cloud/cloud_server.h | 39 +++ .../framework/include/cloud/schema_meta.h | 61 +++++ .../framework/include/error/general_error.h | 31 +++ .../include/serializable/serializable.h | 2 + .../framework/include/store/auto_cache.h | 86 +++++++ .../framework/include/store/cursor.h | 51 ++++ .../framework/include/store/general_store.h | 53 ++++ .../framework/include/store/general_value.h | 76 ++++++ .../framework/include/store/general_watcher.h | 44 ++++ .../framework/serializable/serializable.cpp | 16 ++ .../framework/store/auto_cache.cpp | 227 ++++++++++++++++++ 20 files changed, 1235 insertions(+) create mode 100644 services/distributeddataservice/framework/cloud/asset_loader.cpp create mode 100644 services/distributeddataservice/framework/cloud/cloud_db.cpp create mode 100644 services/distributeddataservice/framework/cloud/cloud_info.cpp create mode 100644 services/distributeddataservice/framework/cloud/cloud_server.cpp create mode 100644 services/distributeddataservice/framework/cloud/schema_meta.cpp create mode 100644 services/distributeddataservice/framework/include/cloud/asset_loader.h create mode 100644 services/distributeddataservice/framework/include/cloud/cloud_db.h create mode 100644 services/distributeddataservice/framework/include/cloud/cloud_info.h create mode 100644 services/distributeddataservice/framework/include/cloud/cloud_server.h create mode 100644 services/distributeddataservice/framework/include/cloud/schema_meta.h create mode 100644 services/distributeddataservice/framework/include/error/general_error.h create mode 100644 services/distributeddataservice/framework/include/store/auto_cache.h create mode 100644 services/distributeddataservice/framework/include/store/cursor.h create mode 100644 services/distributeddataservice/framework/include/store/general_store.h create mode 100644 services/distributeddataservice/framework/include/store/general_value.h create mode 100644 services/distributeddataservice/framework/include/store/general_watcher.h create mode 100644 services/distributeddataservice/framework/store/auto_cache.cpp diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index d22d731b..4ce626b7 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -34,6 +34,11 @@ ohos_shared_library("distributeddatasvcfwk") { sources = [ "backuprule/backup_rule_manager.cpp", "checker/checker_manager.cpp", + "cloud/asset_loader.cpp", + "cloud/cloud_db.cpp", + "cloud/cloud_info.cpp", + "cloud/cloud_server.cpp", + "cloud/schema_meta.cpp", "eventcenter/event.cpp", "eventcenter/event_center.cpp", "feature/feature_system.cpp", @@ -50,6 +55,7 @@ ohos_shared_library("distributeddatasvcfwk") { "metadata/strategy_meta_data.cpp", "metadata/user_meta_data.cpp", "serializable/serializable.cpp", + "store/auto_cache.cpp", "utils/anonymous.cpp", "utils/block_integer.cpp", "utils/constant.cpp", diff --git a/services/distributeddataservice/framework/cloud/asset_loader.cpp b/services/distributeddataservice/framework/cloud/asset_loader.cpp new file mode 100644 index 00000000..cef89e7d --- /dev/null +++ b/services/distributeddataservice/framework/cloud/asset_loader.cpp @@ -0,0 +1,27 @@ +/* + * 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. + */ + +#include "cloud/asset_loader.h" +namespace OHOS::DistributedData { +int32_t AssetLoader::Upload(const std::vector &assets) +{ + return E_NOT_SUPPORT; +} + +int32_t AssetLoader::Download(std::vector &assets) +{ + return E_NOT_SUPPORT; +} +} diff --git a/services/distributeddataservice/framework/cloud/cloud_db.cpp b/services/distributeddataservice/framework/cloud/cloud_db.cpp new file mode 100644 index 00000000..ea4d041d --- /dev/null +++ b/services/distributeddataservice/framework/cloud/cloud_db.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cloud/cloud_db.h" +namespace OHOS::DistributedData { +int32_t CloudDB::Close() +{ + return E_NOT_SUPPORT; +} + +int32_t CloudDB::Execute(const std::string &table, const std::string &sql) +{ + return E_NOT_SUPPORT; +} + +int32_t CloudDB::BatchInsert(const std::string &table, VBuckets &&values) +{ + return E_NOT_SUPPORT; +} + +int32_t CloudDB::BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) +{ + return E_NOT_SUPPORT; +} + +int32_t CloudDB::Delete(const std::string &table, const std::string &sql, Values &&args) +{ + return E_NOT_SUPPORT; +} + +std::shared_ptr CloudDB::Query(const std::string &table, const std::string &sql, Values &&args) +{ + return nullptr; +} + +std::shared_ptr CloudDB::Query(const std::string &table, const GenQuery &query) +{ + return nullptr; +} + +int32_t CloudDB::Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait) +{ + return E_NOT_SUPPORT; +} + +int32_t CloudDB::Watch(int32_t origin, Watcher &watcher) +{ + return E_NOT_SUPPORT; +} + +int32_t CloudDB::Unwatch(int32_t origin, Watcher &watcher) +{ + return E_NOT_SUPPORT; +} + +int32_t CloudDB::Execute(const std::string &table, const std::string &sql, const VBucket &extend) +{ + return E_NOT_SUPPORT; +} + +int32_t CloudDB::BatchInsert(const std::string &table, VBuckets &&values, VBuckets &extends) +{ + return E_NOT_SUPPORT; +} + +int32_t CloudDB::BatchUpdate(const std::string &table, VBuckets &&values, const VBuckets &extends) +{ + return E_NOT_SUPPORT; +} + +int32_t CloudDB::BatchDelete(const std::string &table, const VBuckets &extends) +{ + return E_NOT_SUPPORT; +} + +std::shared_ptr CloudDB::Query(const std::string &table, const VBucket &extend) +{ + return nullptr; +} + +int32_t CloudDB::Lock() +{ + return E_NOT_SUPPORT; +} + +int32_t CloudDB::Heartbeat() +{ + return E_NOT_SUPPORT; +} + +int32_t CloudDB::Unlock() +{ + return E_NOT_SUPPORT; +} +} // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/framework/cloud/cloud_info.cpp b/services/distributeddataservice/framework/cloud/cloud_info.cpp new file mode 100644 index 00000000..be368f11 --- /dev/null +++ b/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -0,0 +1,127 @@ +/* + * 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. + */ + +#include "cloud/cloud_info.h" +#include "utils/constant.h" + +namespace OHOS::DistributedData { +bool CloudInfo::Marshal(Serializable::json &node) const +{ + SetValue(node[GET_NAME(user)], user); + SetValue(node[GET_NAME(id)], id); + SetValue(node[GET_NAME(totalSpace)], totalSpace); + SetValue(node[GET_NAME(remainSpace)], remainSpace); + SetValue(node[GET_NAME(enableCloud)], enableCloud); + SetValue(node[GET_NAME(apps)], apps); + return true; +} + +bool CloudInfo::Unmarshal(const Serializable::json &node) +{ + GetValue(node, GET_NAME(user), user); + GetValue(node, GET_NAME(id), id); + GetValue(node, GET_NAME(totalSpace), totalSpace); + GetValue(node, GET_NAME(remainSpace), remainSpace); + GetValue(node, GET_NAME(enableCloud), enableCloud); + GetValue(node, GET_NAME(apps), apps); + return true; +} + +bool CloudInfo::AppInfo::Marshal(Serializable::json &node) const +{ + SetValue(node[GET_NAME(bundleName)], bundleName); + SetValue(node[GET_NAME(appId)], appId); + SetValue(node[GET_NAME(version)], version); + SetValue(node[GET_NAME(cloudSwitch)], cloudSwitch); + return true; +} + +bool CloudInfo::AppInfo::Unmarshal(const Serializable::json &node) +{ + GetValue(node, GET_NAME(bundleName), bundleName); + GetValue(node, GET_NAME(appId), appId); + GetValue(node, GET_NAME(version), version); + GetValue(node, GET_NAME(cloudSwitch), cloudSwitch); + return true; +} + +std::string CloudInfo::GetKey() const +{ + return GetKey(INFO_PREFIX, { std::to_string(user), id }); +} + +std::map CloudInfo::GetSchemaKey() const +{ + std::map keys; + for (const auto &app : apps) { + const auto key = GetKey(SCHEMA_PREFIX, { std::to_string(user), id, app.bundleName }); + keys.insert_or_assign(app.bundleName, key); + } + return keys; +} + +std::string CloudInfo::GetSchemaKey(std::string bundleName) const +{ + return GetKey(SCHEMA_PREFIX, { std::to_string(user), id, bundleName }); +} + +bool CloudInfo::IsValid() const +{ + return !id.empty(); +} + +bool CloudInfo::IsExist(const std::string &bundleName) const +{ + for (const auto &app : apps) { + if (app.bundleName == bundleName) { + return true; + } + } + return false; +} + +void CloudInfo::UpdateApp(const std::vector &appInfos) +{ + auto tmpInfo = appInfos; + for (auto &info : tmpInfo) { + auto app = GetApp(info.bundleName); + if (app.bundleName.empty()) { + continue; + } + info.cloudSwitch = app.cloudSwitch; + } + apps = tmpInfo; +} + +CloudInfo::AppInfo &CloudInfo::GetApp(const std::string &bundleName) +{ + for (auto &app : apps) { + if (app.bundleName == bundleName) { + return app; + } + } + return appNil_; +} + +std::string CloudInfo::GetPrefix(const std::initializer_list &fields) +{ + return GetKey(INFO_PREFIX, fields).append(Constant::KEY_SEPARATOR); +} + +std::string CloudInfo::GetKey(const std::string &prefix, const std::initializer_list &fields) +{ + return Constant::Join(prefix, Constant::KEY_SEPARATOR, fields); +} +} // namespace OHOS::DistributedData \ No newline at end of file diff --git a/services/distributeddataservice/framework/cloud/cloud_server.cpp b/services/distributeddataservice/framework/cloud/cloud_server.cpp new file mode 100644 index 00000000..a2e0f716 --- /dev/null +++ b/services/distributeddataservice/framework/cloud/cloud_server.cpp @@ -0,0 +1,52 @@ +/* + * 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. + */ + +#include "cloud/cloud_server.h" +namespace OHOS::DistributedData { +CloudServer *CloudServer::instance_ = nullptr; +CloudServer *CloudServer::GetInstance() +{ + return instance_; +} + +bool CloudServer::RegisterCloudInstance(CloudServer *instance) +{ + if (instance_ != nullptr) { + return false; + } + instance_ = instance; + return true; +} + +CloudInfo CloudServer::GetServerInfo(int32_t userId) +{ + return CloudInfo(); +} + +SchemaMeta CloudServer::GetAppSchema(int32_t userId, const std::string &bundleName) +{ + return SchemaMeta(); +} + +std::shared_ptr CloudServer::ConnectAssetLoader(uint32_t tokenId, const CloudServer::Database &dbMeta) +{ + return nullptr; +} + +std::shared_ptr CloudServer::ConnectCloudDB(uint32_t tokenId, const CloudServer::Database &dbMeta) +{ + return nullptr; +} +} // namespace OHOS::DistributedData \ No newline at end of file diff --git a/services/distributeddataservice/framework/cloud/schema_meta.cpp b/services/distributeddataservice/framework/cloud/schema_meta.cpp new file mode 100644 index 00000000..b6a8cd4b --- /dev/null +++ b/services/distributeddataservice/framework/cloud/schema_meta.cpp @@ -0,0 +1,93 @@ +/* + * 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. + */ + +#include "cloud/schema_meta.h" +namespace OHOS::DistributedData { +bool SchemaMeta::Marshal(Serializable::json &node) const +{ + SetValue(node[GET_NAME(version)], version); + SetValue(node[GET_NAME(databases)], databases); + return true; +} + +bool SchemaMeta::Unmarshal(const Serializable::json &node) +{ + GetValue(node, GET_NAME(version), version); + GetValue(node, GET_NAME(databases), databases); + return true; +} + +bool SchemaMeta::Database::Marshal(Serializable::json &node) const +{ + SetValue(node[GET_NAME(name)], name); + SetValue(node[GET_NAME(alias)], alias); + SetValue(node[GET_NAME(tables)], tables); + return true; +} + +bool SchemaMeta::Database::Unmarshal(const Serializable::json &node) +{ + GetValue(node, GET_NAME(name), name); + GetValue(node, GET_NAME(alias), alias); + GetValue(node, GET_NAME(tables), tables); + return true; +} + +bool SchemaMeta::Table::Marshal(Serializable::json &node) const +{ + SetValue(node[GET_NAME(name)], name); + SetValue(node[GET_NAME(alias)], alias); + SetValue(node[GET_NAME(fields)], fields); + return true; +} + +bool SchemaMeta::Table::Unmarshal(const Serializable::json &node) +{ + GetValue(node, GET_NAME(name), name); + GetValue(node, GET_NAME(alias), alias); + GetValue(node, GET_NAME(fields), fields); + return true; +} + +bool SchemaMeta::Field::Marshal(Serializable::json &node) const +{ + SetValue(node[GET_NAME(colName)], colName); + SetValue(node[GET_NAME(alias)], alias); + SetValue(node[GET_NAME(type)], type); + SetValue(node[GET_NAME(primary)], primary); + SetValue(node[GET_NAME(nullable)], nullable); + return true; +} + +bool SchemaMeta::Field::Unmarshal(const Serializable::json &node) +{ + GetValue(node, GET_NAME(colName), colName); + GetValue(node, GET_NAME(alias), alias); + GetValue(node, GET_NAME(type), type); + GetValue(node, GET_NAME(primary), primary); + GetValue(node, GET_NAME(nullable), nullable); + return true; +} + +SchemaMeta::Database SchemaMeta::GetDataBase(const std::string &storeId) +{ + for (const auto &database : databases) { + if (database.name == storeId) { + return database; + } + } + return {}; +} +} // namespace OHOS::DistributedData \ No newline at end of file diff --git a/services/distributeddataservice/framework/include/cloud/asset_loader.h b/services/distributeddataservice/framework/include/cloud/asset_loader.h new file mode 100644 index 00000000..0df69eb0 --- /dev/null +++ b/services/distributeddataservice/framework/include/cloud/asset_loader.h @@ -0,0 +1,28 @@ +/* + * 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_SERVICES_FRAMEWORK_CLOUD_ASSET_LOADER_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_ASSET_LOADER_H +#include +#include "store/general_value.h" +#include "visibility.h" +namespace OHOS::DistributedData { +class API_EXPORT AssetLoader { +public: + virtual ~AssetLoader() = default; + virtual int32_t Upload(const std::vector &assets); + virtual int32_t Download(std::vector &assets); +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_ASSET_LOADER_H diff --git a/services/distributeddataservice/framework/include/cloud/cloud_db.h b/services/distributeddataservice/framework/include/cloud/cloud_db.h new file mode 100644 index 00000000..9b67c9a4 --- /dev/null +++ b/services/distributeddataservice/framework/include/cloud/cloud_db.h @@ -0,0 +1,51 @@ +/* + * 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_SERVICES_FRAMEWORK_CLOUD_CLOUD_DB_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_DB_H +#include "store/general_store.h" +#include "visibility.h" +namespace OHOS::DistributedData { +class API_EXPORT CloudDB : public GeneralStore { +public: + int32_t Close() override; + int32_t Execute(const std::string &table, const std::string &sql) override; + int32_t BatchInsert(const std::string &table, VBuckets &&values) override; + int32_t BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) 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, const GenQuery &query) override; + int32_t Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait) override; + int32_t Watch(int32_t origin, Watcher &watcher) override; + int32_t Unwatch(int32_t origin, Watcher &watcher) override; + + virtual int32_t Execute(const std::string &table, const std::string &sql, const VBucket &extend); + + virtual int32_t BatchInsert(const std::string &table, VBuckets &&values, VBuckets &extends); + + virtual int32_t BatchUpdate(const std::string &table, VBuckets &&values, const VBuckets &extends); + + virtual int32_t BatchDelete(const std::string &table, const VBuckets &extends); + + virtual std::shared_ptr Query(const std::string &table, const VBucket &extend); + + virtual int32_t Lock(); + + virtual int32_t Heartbeat(); + + virtual int32_t Unlock(); +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_DB_H diff --git a/services/distributeddataservice/framework/include/cloud/cloud_info.h b/services/distributeddataservice/framework/include/cloud/cloud_info.h new file mode 100644 index 00000000..c7d2cd17 --- /dev/null +++ b/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -0,0 +1,58 @@ +/* + * 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_SERVICES_FRAMEWORK_CLOUD_CLOUD_INFO_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_INFO_H +#include "serializable/serializable.h" +namespace OHOS::DistributedData { +class API_EXPORT CloudInfo final : public Serializable { +public: + struct API_EXPORT AppInfo final : public Serializable { + std::string bundleName = ""; + std::string appId = ""; + uint64_t version = 0; + bool cloudSwitch = false; + + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + }; + int32_t user = 0; + std::string id = ""; + uint64_t totalSpace = 0; + uint64_t remainSpace = 0; + bool enableCloud = false; + std::vector apps; + + std::string GetKey() const; + std::map GetSchemaKey() const; + std::string GetSchemaKey(std::string bundleName) const; + bool IsValid() const; + bool IsExist(const std::string &bundleName) const; + void UpdateApp(const std::vector &appInfos); + AppInfo &GetApp(const std::string &bundleName); + static std::string GetPrefix(const std::initializer_list &field); + + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + +private: + static constexpr const char *INFO_PREFIX = "CLOUD_INFO"; + static constexpr const char *SCHEMA_PREFIX = "CLOUD_SCHEMA"; + + static std::string GetKey(const std::string &prefix, const std::initializer_list &fields); + AppInfo appNil_ {}; +}; +} +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_INFO_H diff --git a/services/distributeddataservice/framework/include/cloud/cloud_server.h b/services/distributeddataservice/framework/include/cloud/cloud_server.h new file mode 100644 index 00000000..eee93713 --- /dev/null +++ b/services/distributeddataservice/framework/include/cloud/cloud_server.h @@ -0,0 +1,39 @@ +/* + * 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_SERVICES_FRAMEWORK_CLOUD_CLOUD_SERVER_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_SERVER_H +#include "cloud/asset_loader.h" +#include "cloud/cloud_db.h" +#include "cloud/cloud_info.h" +#include "cloud/schema_meta.h" +#include "visibility.h" +namespace OHOS::DistributedData { +class API_EXPORT CloudServer { +public: + using Database = SchemaMeta::Database; + API_EXPORT static CloudServer *GetInstance(); + API_EXPORT static bool RegisterCloudInstance(CloudServer *instance); + + virtual CloudInfo GetServerInfo(int32_t userId); + virtual SchemaMeta GetAppSchema(int32_t userId, const std::string &bundleName); + virtual std::shared_ptr ConnectAssetLoader(uint32_t tokenId, const Database &dbMeta); + virtual std::shared_ptr ConnectCloudDB(uint32_t tokenId, const Database &dbMeta); + +private: + static CloudServer *instance_; +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_SERVER_H diff --git a/services/distributeddataservice/framework/include/cloud/schema_meta.h b/services/distributeddataservice/framework/include/cloud/schema_meta.h new file mode 100644 index 00000000..188a0132 --- /dev/null +++ b/services/distributeddataservice/framework/include/cloud/schema_meta.h @@ -0,0 +1,61 @@ +/* + * 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_SERVICES_FRAMEWORK_CLOUD_SCHEMA_META_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SCHEMA_META_H +#include "serializable/serializable.h" +namespace OHOS::DistributedData { +class API_EXPORT SchemaMeta final : public Serializable { +public: + static constexpr const char *DELETE_FIELD = "#_deleted"; + static constexpr const char *GID_FIELD = "#_gid"; + static constexpr const char *CREATE_FIELD = "#_createTime"; + static constexpr const char *MODIFY_FIELD = "#_modifyTime"; + static constexpr const char *CURSOR_FIELD = "#_cursor"; + struct API_EXPORT Field final : public Serializable { + std::string colName; + std::string alias; + int32_t type = 0; + bool primary = false; + bool nullable = true; + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + }; + + struct API_EXPORT Table final : public Serializable { + std::string name; + std::string alias; + std::vector fields; + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + }; + + struct API_EXPORT Database final : public Serializable { + std::string name = ""; + std::string alias; + std::vector tables; + + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + }; + int32_t version = 0; + std::vector databases; + + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + Database GetDataBase(const std::string &storeId); +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SCHEMA_META_H diff --git a/services/distributeddataservice/framework/include/error/general_error.h b/services/distributeddataservice/framework/include/error/general_error.h new file mode 100644 index 00000000..8c60a916 --- /dev/null +++ b/services/distributeddataservice/framework/include/error/general_error.h @@ -0,0 +1,31 @@ +/* + * 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_SERVICES_FRAMEWORK_STORE_GENERAL_ERROR_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_ERROR_H +namespace OHOS::DistributedData { +enum GeneralError : int32_t { + E_OK = 0, + E_ERROR, + E_BUSY, + E_INVALID_ARGS, + E_NOT_INIT, + E_NOT_SUPPORT, + E_ALREADY_CONSUMED, + E_ALREADY_CLOSED, + E_BUTT, +}; +} +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_ERROR_H diff --git a/services/distributeddataservice/framework/include/serializable/serializable.h b/services/distributeddataservice/framework/include/serializable/serializable.h index dc2337c9..4b16f8dd 100644 --- a/services/distributeddataservice/framework/include/serializable/serializable.h +++ b/services/distributeddataservice/framework/include/serializable/serializable.h @@ -55,6 +55,7 @@ public: API_EXPORT static bool GetValue(const json &node, const std::string &name, uint32_t &value); API_EXPORT static bool GetValue(const json &node, const std::string &name, int32_t &value); API_EXPORT static bool GetValue(const json &node, const std::string &name, int64_t &value); + API_EXPORT static bool GetValue(const json &node, const std::string &name, uint64_t &value); API_EXPORT static bool GetValue(const json &node, const std::string &name, bool &value); API_EXPORT static bool GetValue(const json &node, const std::string &name, std::vector &value); API_EXPORT static bool GetValue(const json &node, const std::string &name, Serializable &value); @@ -62,6 +63,7 @@ public: API_EXPORT static bool SetValue(json &node, const uint32_t &value); API_EXPORT static bool SetValue(json &node, const int32_t &value); API_EXPORT static bool SetValue(json &node, const int64_t &value); + API_EXPORT static bool SetValue(json &node, const uint64_t &value); API_EXPORT static bool SetValue(json &node, const bool &value); API_EXPORT static bool SetValue(json &node, const std::vector &value); API_EXPORT static bool SetValue(json &node, const Serializable &value); diff --git a/services/distributeddataservice/framework/include/store/auto_cache.h b/services/distributeddataservice/framework/include/store/auto_cache.h new file mode 100644 index 00000000..67b48d42 --- /dev/null +++ b/services/distributeddataservice/framework/include/store/auto_cache.h @@ -0,0 +1,86 @@ +/* + * 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_SERVICES_FRAMEWORK_STORE_STORE_AUTO_CACHE_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_AUTO_CACHE_H +#include +#include + +#include "concurrent_map.h" +#include "error/general_error.h" +#include "executor_pool.h" +#include "metadata/store_meta_data.h" +#include "store/general_store.h" +#include "store/general_value.h" +#include "store/general_watcher.h" +#include "visibility.h" +namespace OHOS::DistributedData { +class AutoCache { +public: + using Error = GeneralError; + using Store = std::shared_ptr; + using Watcher = GeneralWatcher; + using Watchers = std::set>; + using Time = std::chrono::steady_clock::time_point; + using Executor = ExecutorPool; + using TaskId = ExecutorPool::TaskId; + using Creator = std::function; + API_EXPORT static AutoCache &GetInstance(); + + API_EXPORT int32_t RegCreator(int32_t type, Creator creator); + + API_EXPORT void Bind(std::shared_ptr executor); + + API_EXPORT Store GetStore(const StoreMetaData &meta, const Watchers &watchers); + + API_EXPORT void CloseStore(uint32_t tokenId, const std::string &storeId); + + API_EXPORT void CloseExcept(const std::set &users); + + API_EXPORT void SetObserver(uint32_t tokenId, const std::string &storeId, const Watchers &watchers); + +private: + AutoCache(); + ~AutoCache(); + void GarbageCollect(bool isForce); + struct Delegate : public GeneralWatcher { + Delegate(GeneralStore *delegate, const Watchers &watchers, int32_t user); + ~Delegate(); + operator Store(); + bool operator<(const Time &time) const; + bool Close(); + int32_t GetUser() const; + void SetObservers(const Watchers &watchers); + int32_t OnChange(Origin origin, const std::string &id) override; + int32_t OnChange(Origin origin, const std::string &id, const std::vector &values) override; + + private: + mutable Time time_; + GeneralStore *store_ = nullptr; + Watchers watchers_; + int32_t user_; + std::shared_mutex mutex_; + }; + + static constexpr int64_t INTERVAL = 1; + static constexpr size_t MAX_CREATOR_NUM = 30; + + std::shared_ptr executor_; + TaskId taskId_ = Executor::INVALID_TASK_ID; + ConcurrentMap> stores_; + Creator creators_[MAX_CREATOR_NUM]; +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_AUTO_CACHE_H diff --git a/services/distributeddataservice/framework/include/store/cursor.h b/services/distributeddataservice/framework/include/store/cursor.h new file mode 100644 index 00000000..e588c5fd --- /dev/null +++ b/services/distributeddataservice/framework/include/store/cursor.h @@ -0,0 +1,51 @@ +/* + * 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_SERVICES_FRAMEWORK_STORE_CURSOR_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_CURSOR_H +#include +#include +#include + +#include "store/general_value.h" +namespace OHOS::DistributedData { +class Cursor { +public: + virtual ~Cursor() = default; + + virtual int32_t GetColumnNames(std::vector &names) const = 0; + + virtual int32_t GetColumnName(int32_t col, std::string &name) const = 0; + + virtual int32_t GetColumnType(int32_t col) const = 0; + + virtual int32_t GetCount() const = 0; + + virtual int32_t MoveToFirst() = 0; + + virtual int32_t MoveToNext() = 0; + + virtual int32_t GetEntry(VBucket &entry) = 0; + + virtual int32_t GetRow(VBucket &data) = 0; + + virtual int32_t Get(int32_t col, Value &value) = 0; + + virtual int32_t Get(const std::string &col, Value &value) = 0; + + virtual int32_t Close() = 0; +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_CURSOR_H diff --git a/services/distributeddataservice/framework/include/store/general_store.h b/services/distributeddataservice/framework/include/store/general_store.h new file mode 100644 index 00000000..3b88c4f4 --- /dev/null +++ b/services/distributeddataservice/framework/include/store/general_store.h @@ -0,0 +1,53 @@ +/* + * 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_SERVICES_FRAMEWORK_STORE_GENERAL_STORE_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_STORE_H +#include +#include +#include "store/cursor.h" +#include "store/general_value.h" +#include "store/general_watcher.h" +namespace OHOS::DistributedData { +class GeneralStore { +public: + using Watcher = GeneralWatcher; + using Async = std::function>)>; + using Devices = std::vector; + + virtual ~GeneralStore() = default; + + virtual int32_t Close() = 0; + + virtual int32_t Execute(const std::string &table, const std::string &sql) = 0; + + virtual int32_t BatchInsert(const std::string &table, VBuckets &&values) = 0; + + virtual int32_t BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) = 0; + + virtual int32_t Delete(const std::string &table, const std::string &sql, Values &&args) = 0; + + virtual std::shared_ptr Query(const std::string &table, const std::string &sql, Values &&args) = 0; + + virtual std::shared_ptr Query(const std::string &table, const GenQuery &query) = 0; + + virtual int32_t Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait) = 0; + + virtual int32_t Watch(int32_t origin, Watcher &watcher) = 0; + + virtual int32_t Unwatch(int32_t origin, Watcher &watcher) = 0; +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_STORE_H diff --git a/services/distributeddataservice/framework/include/store/general_value.h b/services/distributeddataservice/framework/include/store/general_value.h new file mode 100644 index 00000000..ad2e3141 --- /dev/null +++ b/services/distributeddataservice/framework/include/store/general_value.h @@ -0,0 +1,76 @@ +/* + * 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_SERVICES_FRAMEWORK_STORE_GENERAL_VALUE_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_VALUE_H +#include +#include +#include +#include +#include +#include "traits.h" +#include "error/general_error.h" +namespace OHOS::DistributedData { +struct Asset { + uint32_t version; + std::string name; + std::string uri; + std::string createTime; + std::string modifyTime; + std::string size; + std::string hash; +}; + +struct GenQuery { + virtual ~GenQuery() = default; + virtual int32_t GetInterfaceId() = 0; +}; + +using Assets = std::vector; +using Bytes = std::vector; +using Value = std::variant; +using Values = std::vector; +using VBucket = std::map; +using VBuckets = std::vector; + +template +inline constexpr size_t TYPE_INDEX = Traits::variant_index_of_v; + +inline constexpr size_t TYPE_MAX = Traits::variant_size_of_v; + +template +bool GetItem(const T &input, O &output) +{ + return false; +} + +template +bool GetItem(const T &input, O &output) +{ + auto val = Traits::get_if(&input); + if (val != nullptr) { + output = *val; + return true; + } + return GetItem(input, output); +} + +template +bool Convert(const T &input, std::variant &output) +{ + return GetItem(input, output); +} +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_VALUE_H diff --git a/services/distributeddataservice/framework/include/store/general_watcher.h b/services/distributeddataservice/framework/include/store/general_watcher.h new file mode 100644 index 00000000..52130eab --- /dev/null +++ b/services/distributeddataservice/framework/include/store/general_watcher.h @@ -0,0 +1,44 @@ +/* + * 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_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 { +class GeneralWatcher { +public: + enum Origin : int32_t { + ORIGIN_CLOUD, + ORIGIN_LOCAL, + ORIGIN_REMOTE, + ORIGIN_ALL, + ORIGIN_BUTT, + }; + + enum ChangeOp : int32_t { + OP_INSERT, + OP_UPDATE, + OP_DELETE, + OP_BUTT, + }; + + virtual ~GeneralWatcher() = default; + virtual int32_t OnChange(Origin origin, const std::string &id) = 0; + virtual int32_t OnChange(Origin origin, const std::string &id, const std::vector &values) = 0; +}; +} +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_WATCHER_H diff --git a/services/distributeddataservice/framework/serializable/serializable.cpp b/services/distributeddataservice/framework/serializable/serializable.cpp index 9622af95..509880a8 100644 --- a/services/distributeddataservice/framework/serializable/serializable.cpp +++ b/services/distributeddataservice/framework/serializable/serializable.cpp @@ -103,6 +103,16 @@ bool Serializable::GetValue(const json &node, const std::string &name, int64_t & return true; } +bool Serializable::GetValue(const json &node, const std::string &name, uint64_t &value) +{ + auto &subNode = GetSubNode(node, name); + if (subNode.is_null() || !subNode.is_number_unsigned()) { + return false; + } + subNode.get_to(value); + return true; +} + bool Serializable::GetValue(const json &node, const std::string &name, bool &value) { auto &subNode = GetSubNode(node, name); @@ -156,6 +166,12 @@ bool Serializable::SetValue(json &node, const int64_t &value) return true; } +bool Serializable::SetValue(json &node, const uint64_t &value) +{ + node = value; + return true; +} + bool Serializable::SetValue(json &node, const bool &value) { node = value; diff --git a/services/distributeddataservice/framework/store/auto_cache.cpp b/services/distributeddataservice/framework/store/auto_cache.cpp new file mode 100644 index 00000000..212f6c1b --- /dev/null +++ b/services/distributeddataservice/framework/store/auto_cache.cpp @@ -0,0 +1,227 @@ +/* + * 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 "AutoCache" +#include "store/auto_cache.h" + +#include +namespace OHOS::DistributedData { +AutoCache &AutoCache::GetInstance() +{ + static AutoCache cache; + return cache; +} + +int32_t AutoCache::RegCreator(int32_t type, Creator creator) +{ + if (type >= MAX_CREATOR_NUM) { + return E_ERROR; + } + creators_[type] = creator; + return 0; +} + +void AutoCache::Bind(std::shared_ptr executor) +{ + if (executor == nullptr || taskId_ != Executor::INVALID_TASK_ID) { + return; + } + executor_ = executor; + taskId_ = executor_->Schedule(std::bind(&AutoCache::GarbageCollect, this, false), std::chrono::minutes(INTERVAL), + std::chrono::minutes(INTERVAL)); +} + +AutoCache::AutoCache() {} + +AutoCache::~AutoCache() +{ + GarbageCollect(true); + if (executor_ != nullptr) { + executor_->Remove(taskId_, true); + } +} + +AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers &watchers) +{ + Store store; + if (meta.storeType >= MAX_CREATOR_NUM || !creators_[meta.storeType]) { + return store; + } + + stores_.Compute(meta.tokenId, + [this, &meta, &watchers, &store](auto &, std::map &stores) -> bool { + auto it = stores.find(meta.storeId); + if (it != stores.end()) { + it->second.SetObservers(watchers); + store = it->second; + return !stores.empty(); + } + auto *dbStore = creators_[meta.storeType](meta); + if (dbStore == nullptr) { + return !stores.empty(); + } + auto result = stores.emplace(std::piecewise_construct, std::forward_as_tuple(meta.storeId), + std::forward_as_tuple(dbStore, watchers, atoi(meta.user.c_str()))); + store = result.first->second; + return !stores.empty(); + }); + return store; +} + +void AutoCache::CloseStore(uint32_t tokenId, const std::string &storeId) +{ + stores_.ComputeIfPresent(tokenId, [&storeId](auto &key, std::map &delegates) { + auto it = delegates.find(storeId); + if (it != delegates.end()) { + it->second.Close(); + delegates.erase(it); + } + return !delegates.empty(); + }); +} + +void AutoCache::CloseExcept(const std::set &users) +{ + stores_.EraseIf([&users](const auto &tokenId, std::map &delegates) { + if (delegates.empty() || users.count(delegates.begin()->second.GetUser()) != 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()) { + ++it; + } else { + it = delegates.erase(it); + } + } + return delegates.empty(); + }); +} + +void AutoCache::SetObserver(uint32_t tokenId, const std::string &storeId, const AutoCache::Watchers &watchers) +{ + stores_.ComputeIfPresent(tokenId, [&storeId, &watchers](auto &key, auto &stores) { + ZLOGD("tokenId:0x%{public}x storeId:%{public}s observers:%{public}zu", key, storeId.c_str(), watchers.size()); + auto it = stores.find(storeId); + if (it != stores.end()) { + it->second.SetObservers(watchers); + } + return true; + }); +} + +void AutoCache::GarbageCollect(bool isForce) +{ + auto current = std::chrono::steady_clock::now(); + stores_.EraseIf([¤t, isForce](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 ((isForce || it->second < current) && it->second.Close()) { + it = delegates.erase(it); + } else { + ++it; + } + } + return delegates.empty(); + }); +} + +AutoCache::Delegate::Delegate(GeneralStore *delegate, const Watchers &watchers, int32_t user) + : store_(delegate), watchers_(watchers), user_(user) +{ + time_ = std::chrono::steady_clock::now() + std::chrono::minutes(INTERVAL); + if (store_ != nullptr) { + store_->Watch(ORIGIN_ALL, *this); + } +} + +AutoCache::Delegate::~Delegate() +{ + if (store_ != nullptr) { + store_->Unwatch(ORIGIN_ALL, *this); + store_->Close(); + store_ = nullptr; + } +} + +AutoCache::Delegate::operator Store() +{ + time_ = std::chrono::steady_clock::now() + std::chrono::minutes(INTERVAL); + return Store(store_, [](GeneralStore *) {}); +} + +bool AutoCache::Delegate::operator<(const AutoCache::Time &time) const +{ + return time_ < time; +} + +bool AutoCache::Delegate::Close() +{ + std::unique_lock lock(mutex_); + if (store_ != nullptr) { + store_->Unwatch(ORIGIN_ALL, *this); + } + + auto status = store_->Close(); + if (status == Error::E_BUSY) { + return false; + } + store_ = nullptr; + return true; +} + +void AutoCache::Delegate::SetObservers(const AutoCache::Watchers &watchers) +{ + std::unique_lock lock(mutex_); + watchers_ = watchers; +} + +int32_t AutoCache::Delegate::GetUser() const +{ + return user_; +} + +int32_t AutoCache::Delegate::OnChange(Origin origin, const std::string &id) +{ + Watchers watchers; + { + std::unique_lock lock(mutex_); + watchers = watchers_; + } + for (auto watcher : watchers) { + if (watcher == nullptr) { + continue; + } + watcher->OnChange(origin, id); + } + return Error::E_OK; +} + +int32_t AutoCache::Delegate::OnChange(Origin origin, const std::string &id, const std::vector &values) +{ + Watchers watchers; + { + std::unique_lock lock(mutex_); + watchers = watchers_; + } + for (auto watcher : watchers) { + if (watcher == nullptr) { + continue; + } + watcher->OnChange(origin, id, values); + } + return Error::E_OK; +} +} // namespace OHOS::DistributedData -- Gitee From 996a1ab8ffc0e93c9f82bfbdcbf4ddcf45fc4d16 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 26 Apr 2023 14:10:51 +0800 Subject: [PATCH 064/152] fixed complite bugs Signed-off-by: Sven Wang --- .../distributeddataservice/framework/include/store/auto_cache.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/framework/include/store/auto_cache.h b/services/distributeddataservice/framework/include/store/auto_cache.h index 67b48d42..ffed302f 100644 --- a/services/distributeddataservice/framework/include/store/auto_cache.h +++ b/services/distributeddataservice/framework/include/store/auto_cache.h @@ -75,7 +75,7 @@ private: }; static constexpr int64_t INTERVAL = 1; - static constexpr size_t MAX_CREATOR_NUM = 30; + static constexpr int32_t MAX_CREATOR_NUM = 30; std::shared_ptr executor_; TaskId taskId_ = Executor::INVALID_TASK_ID; -- Gitee From ff6a6d203eeddbd756f7e6fd3f80664da9bc193f Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 26 Apr 2023 16:01:47 +0800 Subject: [PATCH 065/152] fixed bug interface Signed-off-by: Sven Wang --- .../framework/cloud/cloud_info.cpp | 23 ------------------- .../framework/include/cloud/cloud_info.h | 3 --- 2 files changed, 26 deletions(-) diff --git a/services/distributeddataservice/framework/cloud/cloud_info.cpp b/services/distributeddataservice/framework/cloud/cloud_info.cpp index be368f11..f3f141eb 100644 --- a/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -92,29 +92,6 @@ bool CloudInfo::IsExist(const std::string &bundleName) const return false; } -void CloudInfo::UpdateApp(const std::vector &appInfos) -{ - auto tmpInfo = appInfos; - for (auto &info : tmpInfo) { - auto app = GetApp(info.bundleName); - if (app.bundleName.empty()) { - continue; - } - info.cloudSwitch = app.cloudSwitch; - } - apps = tmpInfo; -} - -CloudInfo::AppInfo &CloudInfo::GetApp(const std::string &bundleName) -{ - for (auto &app : apps) { - if (app.bundleName == bundleName) { - return app; - } - } - return appNil_; -} - std::string CloudInfo::GetPrefix(const std::initializer_list &fields) { return GetKey(INFO_PREFIX, fields).append(Constant::KEY_SEPARATOR); diff --git a/services/distributeddataservice/framework/include/cloud/cloud_info.h b/services/distributeddataservice/framework/include/cloud/cloud_info.h index c7d2cd17..5576bca8 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_info.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -40,8 +40,6 @@ public: std::string GetSchemaKey(std::string bundleName) const; bool IsValid() const; bool IsExist(const std::string &bundleName) const; - void UpdateApp(const std::vector &appInfos); - AppInfo &GetApp(const std::string &bundleName); static std::string GetPrefix(const std::initializer_list &field); bool Marshal(json &node) const override; @@ -52,7 +50,6 @@ private: static constexpr const char *SCHEMA_PREFIX = "CLOUD_SCHEMA"; static std::string GetKey(const std::string &prefix, const std::initializer_list &fields); - AppInfo appNil_ {}; }; } #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_INFO_H -- Gitee From ecb483ad26f771de5d77243531a9eb6dfb0ab598 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Wed, 26 Apr 2023 17:51:31 +0800 Subject: [PATCH 066/152] move the input to output Signed-off-by: Sven Wang --- .../framework/include/store/general_value.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/framework/include/store/general_value.h b/services/distributeddataservice/framework/include/store/general_value.h index ad2e3141..eca104b1 100644 --- a/services/distributeddataservice/framework/include/store/general_value.h +++ b/services/distributeddataservice/framework/include/store/general_value.h @@ -51,26 +51,26 @@ inline constexpr size_t TYPE_INDEX = Traits::variant_index_of_v; inline constexpr size_t TYPE_MAX = Traits::variant_size_of_v; template -bool GetItem(const T &input, O &output) +bool GetItem(T &&input, O &output) { return false; } template -bool GetItem(const T &input, O &output) +bool GetItem(T &&input, O &output) { auto val = Traits::get_if(&input); if (val != nullptr) { - output = *val; + output = std::move(*val); return true; } - return GetItem(input, output); + return GetItem(std::move(input), output); } template -bool Convert(const T &input, std::variant &output) +bool Convert(T &&input, std::variant &output) { - return GetItem(input, output); + return GetItem(std::move(input), output); } } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_VALUE_H -- Gitee From 6180ce69a1206fd9fd9b73bd9ef4ee7a1900da09 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Thu, 27 Apr 2023 23:45:05 +0800 Subject: [PATCH 067/152] correct uuid key Signed-off-by: ylq121 --- services/distributeddataservice/service/kvdb/upgrade.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index 4b662aca..05e4211d 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -155,7 +155,7 @@ Upgrade::AutoStore Upgrade::GetDBStore(const StoreMeta &meta, const std::vector< std::string Upgrade::GetEncryptedUuidByMeta(const StoreMeta &meta) { - std::string keyUuid = meta.appId + meta.storeId; + std::string keyUuid = meta.appId + meta.storeId + meta.deviceId; if (calcUuid_.Contains(keyUuid)) { return calcUuid_[keyUuid]; } -- Gitee From d4a9588653ad461fb3d8e2f18e956e17c7c42a6c Mon Sep 17 00:00:00 2001 From: ylq121 Date: Fri, 28 Apr 2023 09:13:57 +0800 Subject: [PATCH 068/152] fix uuid issue Signed-off-by: ylq121 --- services/distributeddataservice/service/kvdb/upgrade.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index 05e4211d..fba290ac 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -155,7 +155,7 @@ Upgrade::AutoStore Upgrade::GetDBStore(const StoreMeta &meta, const std::vector< std::string Upgrade::GetEncryptedUuidByMeta(const StoreMeta &meta) { - std::string keyUuid = meta.appId + meta.storeId + meta.deviceId; + std::string keyUuid = meta.appId + meta.deviceId; if (calcUuid_.Contains(keyUuid)) { return calcUuid_[keyUuid]; } -- Gitee From 826bb9039b9189bab98f8a75cb41a9d1981723f4 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Fri, 28 Apr 2023 10:27:20 +0800 Subject: [PATCH 069/152] fix Signed-off-by: ylq121 --- services/distributeddataservice/service/kvdb/upgrade.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index fba290ac..9df8d815 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -156,9 +156,11 @@ Upgrade::AutoStore Upgrade::GetDBStore(const StoreMeta &meta, const std::vector< std::string Upgrade::GetEncryptedUuidByMeta(const StoreMeta &meta) { std::string keyUuid = meta.appId + meta.deviceId; - if (calcUuid_.Contains(keyUuid)) { - return calcUuid_[keyUuid]; - } + auto pair = calcUuid_.Find(keyUuid); + if (pair.first)) + { + return pair.second; + } std::string uuid; if (OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(meta.tokenId) == OHOS::Security::AccessToken::TOKEN_HAP) { -- Gitee From 64e770cb065c3d6633e600b32ba0aa03260655d1 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Fri, 28 Apr 2023 10:47:46 +0800 Subject: [PATCH 070/152] xiugai Signed-off-by: ylq121 --- services/distributeddataservice/service/kvdb/upgrade.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index 9df8d815..2b0ba0cf 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -157,7 +157,7 @@ std::string Upgrade::GetEncryptedUuidByMeta(const StoreMeta &meta) { std::string keyUuid = meta.appId + meta.deviceId; auto pair = calcUuid_.Find(keyUuid); - if (pair.first)) + if (pair.first) { return pair.second; } -- Gitee From fe857c3534775191e5d7d97d69c16230231c3f11 Mon Sep 17 00:00:00 2001 From: ylq121 Date: Fri, 28 Apr 2023 10:56:12 +0800 Subject: [PATCH 071/152] xiugai Signed-off-by: ylq121 --- services/distributeddataservice/service/kvdb/upgrade.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/upgrade.cpp b/services/distributeddataservice/service/kvdb/upgrade.cpp index 2b0ba0cf..ea40a6bd 100644 --- a/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -157,10 +157,9 @@ std::string Upgrade::GetEncryptedUuidByMeta(const StoreMeta &meta) { std::string keyUuid = meta.appId + meta.deviceId; auto pair = calcUuid_.Find(keyUuid); - if (pair.first) - { - return pair.second; - } + if (pair.first) { + return pair.second; + } std::string uuid; if (OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(meta.tokenId) == OHOS::Security::AccessToken::TOKEN_HAP) { -- Gitee From db65d4a9437333f035bd424fe835f29c4d9a9708 Mon Sep 17 00:00:00 2001 From: hanlu Date: Wed, 3 May 2023 17:41:41 +0800 Subject: [PATCH 072/152] f Signed-off-by: hanlu --- .../distributeddataservice/service/BUILD.gn | 44 +- .../{ => common}/bundle_mgr_proxy.cpp | 167 +- .../{ => common}/bundle_mgr_proxy.h | 98 +- .../service/data_share/common/context.h | 86 + .../service/data_share/common/db_delegate.cpp | 71 + .../service/data_share/common/db_delegate.h | 97 ++ .../data_share/common/div_strategy.cpp | 28 + .../service/data_share/common/div_strategy.h | 38 + .../service/data_share/common/kv_delegate.cpp | 222 +++ .../service/data_share/common/kv_delegate.h | 51 + .../rdb_delegate.cpp} | 312 ++-- .../{rdb_adaptor.h => common/rdb_delegate.h} | 131 +- .../data_share/common/scheduler_manager.cpp | 148 ++ .../data_share/common/scheduler_manager.h | 48 + .../data_share/common/seq_strategy.cpp | 45 + .../service/data_share/common/seq_strategy.h | 35 + .../service/data_share/common/strategy.h | 41 + .../data_share/common/template_manager.cpp | 528 ++++++ .../data_share/common/template_manager.h | 116 ++ .../data_share/{ => common}/uri_utils.cpp | 143 +- .../data_share/{ => common}/uri_utils.h | 97 +- .../data_share/data/published_data.cpp | 179 ++ .../service/data_share/data/published_data.h | 61 + .../service/data_share/data/template_data.cpp | 95 ++ .../template_data.h} | 111 +- .../service/data_share/data_proxy_observer.h | 37 + .../data_share/data_share_obs_proxy.cpp | 58 + .../service/data_share/data_share_obs_proxy.h | 45 + .../data_share/data_share_profile_info.cpp | 216 --- .../data_share/data_share_service_impl.cpp | 421 +++-- .../data_share/data_share_service_impl.h | 39 +- .../data_share/data_share_service_stub.cpp | 211 ++- .../data_share/data_share_service_stub.h | 27 +- .../data_share/data_share_types_util.cpp | 121 +- .../data_share/data_share_types_util.h | 40 +- .../gaussdb_rd_simple/.vscode/settings.json | 60 + .../data_share/gaussdb_rd_simple/BUILD.gn | 91 ++ .../gaussdb_rd_simple/CMakeLists.txt | 38 + .../data_share/gaussdb_rd_simple/README.md | 1 + .../include/grd_base/grd_db_api.h | 34 + .../include/grd_base/grd_error.h | 86 + .../include/grd_base/grd_resultset_api.h | 36 + .../include/grd_base/grd_type_export.h | 67 + .../include/grd_document/grd_document_api.h | 46 + .../src/common/include/collection_option.h | 37 + .../src/common/include/db_config.h | 46 + .../src/common/include/doc_limit.h | 22 + .../src/common/include/json_common.h | 47 + .../src/common/include/log_print.h | 44 + .../src/common/include/os_api.h | 30 + .../src/common/src/collection_option.cpp | 118 ++ .../src/common/src/db_config.cpp | 300 ++++ .../src/common/src/json_common.cpp | 311 ++++ .../src/common/src/log_print.cpp | 82 + .../src/common/src/os_api.cpp | 84 + .../src/executor/base/grd_db_api.cpp | 75 + .../src/executor/document/document_check.cpp | 152 ++ .../src/executor/document/document_check.h | 42 + .../executor/document/grd_document_api.cpp | 121 ++ .../executor/document/grd_resultset_api.cpp | 71 + .../src/executor/include/grd_format_config.h | 32 + .../executor/include/grd_resultset_inner.h | 26 + .../src/executor/include/grd_type_inner.h | 27 + .../src/interface/include/collection.h | 42 + .../src/interface/include/doc_errno.h | 44 + .../src/interface/include/document_store.h | 50 + .../include/document_store_manager.h | 35 + .../src/interface/include/projection_tree.h | 37 + .../src/interface/include/result_set.h | 53 + .../src/interface/include/result_set_common.h | 31 + .../src/interface/src/collection.cpp | 177 ++ .../src/interface/src/doc_errno.cpp | 79 + .../src/interface/src/document_store.cpp | 449 +++++ .../interface/src/document_store_manager.cpp | 142 ++ .../src/interface/src/projection_tree.cpp | 76 + .../src/interface/src/result_set.cpp | 161 ++ .../src/interface/src/result_set_common.cpp | 30 + .../src/oh_adapter/include/json_object.h | 113 ++ .../oh_adapter/include/kv_store_executor.h | 40 + .../src/oh_adapter/include/kv_store_manager.h | 30 + .../src/oh_adapter/src/json_object.cpp | 539 ++++++ .../src/oh_adapter/src/kv_store_manager.cpp | 69 + .../src/sqlite_store_executor_impl.cpp | 260 +++ .../src/sqlite_store_executor_impl.h | 50 + .../src/oh_adapter/src/sqlite_utils.cpp | 305 ++++ .../src/oh_adapter/src/sqlite_utils.h | 57 + .../gaussdb_rd_simple/test/unittest/BUILD.gn | 164 ++ .../test/unittest/api/documentdb_api_test.cpp | 657 ++++++++ .../api/documentdb_collection_test.cpp | 220 +++ .../unittest/api/documentdb_data_test.cpp | 318 ++++ .../unittest/api/documentdb_delete_test.cpp | 326 ++++ .../unittest/api/documentdb_find_test.cpp | 1449 +++++++++++++++++ .../unittest/api/documentdb_insert_test.cpp | 754 +++++++++ .../test/unittest/api/documentdb_test_utils.h | 27 + .../unittest/common/documentdb_test_utils.cpp | 24 + .../unittest/common/documentdb_test_utils.h | 27 + .../documentdb_json_common_test.cpp | 330 ++++ .../oh_adapter/documentdb_jsonobject_test.cpp | 68 + .../service/data_share/idata_share_service.h | 36 +- .../service/data_share/permission_proxy.cpp | 151 -- ...d_config_from_data_proxy_node_strategy.cpp | 94 ++ ...ad_config_from_data_proxy_node_strategy.h} | 50 +- ...g_from_data_share_bundle_info_strategy.cpp | 117 ++ ...fig_from_data_share_bundle_info_strategy.h | 33 + .../data_share/strategies/delete_strategy.cpp | 68 + .../data_share/strategies/delete_strategy.h | 33 + .../general/check_is_data_proxy_strategy.cpp | 24 + .../general/check_is_data_proxy_strategy.h | 26 + .../general/check_is_single_app_strategy.cpp | 25 + .../general/check_is_single_app_strategy.h | 26 + .../general/connect_extension_strategy.cpp | 104 ++ .../general/connect_extension_strategy.h | 33 + .../strategies/general/empty_strategy.cpp | 24 + .../strategies/general/empty_strategy.h | 26 + .../general/load_config_common_strategy.cpp | 46 + .../general/load_config_common_strategy.h | 29 + .../load_config_data_info_strategy.cpp | 89 + .../general/load_config_data_info_strategy.h | 34 + .../load_config_from_bundle_info_strategy.cpp | 27 + .../load_config_from_bundle_info_strategy.h | 26 + .../general/permission_strategy.cpp | 41 + .../strategies/general/permission_strategy.h | 27 + ...process_single_app_user_cross_strategy.cpp | 46 + .../process_single_app_user_cross_strategy.h | 30 + .../strategies/get_data_strategy.cpp | 73 + .../data_share/strategies/get_data_strategy.h | 36 + .../data_share/strategies/insert_strategy.cpp | 69 + .../data_share/strategies/insert_strategy.h | 32 + .../strategies/publish_strategy.cpp | 70 + .../data_share/strategies/publish_strategy.h | 33 + .../data_share/strategies/query_strategy.cpp | 70 + .../data_share/strategies/query_strategy.h | 34 + .../strategies/subscribe_strategy.cpp | 58 + .../strategies/subscribe_strategy.h | 32 + .../data_share/strategies/update_strategy.cpp | 69 + .../data_share/strategies/update_strategy.h | 35 + 136 files changed, 14379 insertions(+), 1063 deletions(-) rename services/distributeddataservice/service/data_share/{ => common}/bundle_mgr_proxy.cpp (61%) rename services/distributeddataservice/service/data_share/{ => common}/bundle_mgr_proxy.h (61%) create mode 100644 services/distributeddataservice/service/data_share/common/context.h create mode 100644 services/distributeddataservice/service/data_share/common/db_delegate.cpp create mode 100644 services/distributeddataservice/service/data_share/common/db_delegate.h create mode 100644 services/distributeddataservice/service/data_share/common/div_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/common/div_strategy.h create mode 100644 services/distributeddataservice/service/data_share/common/kv_delegate.cpp create mode 100644 services/distributeddataservice/service/data_share/common/kv_delegate.h rename services/distributeddataservice/service/data_share/{rdb_adaptor.cpp => common/rdb_delegate.cpp} (39%) rename services/distributeddataservice/service/data_share/{rdb_adaptor.h => common/rdb_delegate.h} (52%) create mode 100644 services/distributeddataservice/service/data_share/common/scheduler_manager.cpp create mode 100644 services/distributeddataservice/service/data_share/common/scheduler_manager.h create mode 100644 services/distributeddataservice/service/data_share/common/seq_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/common/seq_strategy.h create mode 100644 services/distributeddataservice/service/data_share/common/strategy.h create mode 100644 services/distributeddataservice/service/data_share/common/template_manager.cpp create mode 100644 services/distributeddataservice/service/data_share/common/template_manager.h rename services/distributeddataservice/service/data_share/{ => common}/uri_utils.cpp (46%) rename services/distributeddataservice/service/data_share/{ => common}/uri_utils.h (68%) create mode 100644 services/distributeddataservice/service/data_share/data/published_data.cpp create mode 100644 services/distributeddataservice/service/data_share/data/published_data.h create mode 100644 services/distributeddataservice/service/data_share/data/template_data.cpp rename services/distributeddataservice/service/data_share/{data_share_profile_info.h => data/template_data.h} (40%) create mode 100644 services/distributeddataservice/service/data_share/data_proxy_observer.h create mode 100644 services/distributeddataservice/service/data_share/data_share_obs_proxy.cpp create mode 100644 services/distributeddataservice/service/data_share/data_share_obs_proxy.h delete mode 100644 services/distributeddataservice/service/data_share/data_share_profile_info.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/.vscode/settings.json create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/BUILD.gn create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/CMakeLists.txt create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/README.md create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_db_api.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_error.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_resultset_api.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_type_export.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_document/grd_document_api.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/collection_option.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/db_config.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/doc_limit.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/log_print.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/os_api.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/collection_option.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/db_config.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/log_print.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/os_api.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/base/grd_db_api.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_document_api.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_resultset_api.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_format_config.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_resultset_inner.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_type_inner.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store_manager.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/projection_tree.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set_common.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store_manager.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/projection_tree.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set_common.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_manager.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/kv_store_manager.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/BUILD.gn create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_api_test.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_collection_test.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_delete_test.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_test_utils.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp delete mode 100644 services/distributeddataservice/service/data_share/permission_proxy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp rename services/distributeddataservice/service/data_share/{permission_proxy.h => strategies/data_proxy/load_config_from_data_proxy_node_strategy.h} (32%) create mode 100644 services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/delete_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/general/check_is_data_proxy_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/general/check_is_data_proxy_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/general/check_is_single_app_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/general/check_is_single_app_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/general/empty_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/general/empty_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/general/load_config_from_bundle_info_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/general/load_config_from_bundle_info_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/general/permission_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/general/permission_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/general/process_single_app_user_cross_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/general/process_single_app_user_cross_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/get_data_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/insert_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/publish_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/query_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/query_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/subscribe_strategy.h create mode 100644 services/distributeddataservice/service/data_share/strategies/update_strategy.cpp create mode 100644 services/distributeddataservice/service/data_share/strategies/update_strategy.h diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index d05b54c5..ef6f4d53 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -23,7 +23,10 @@ config("module_public_config") { "bootstrap/include", "config/include", "crypto/include", - "datashare", + "data_share", + "data_share/strategies", + "data_share/common", + "data_share/data", "directory/include", "kvdb", "matrix/include", @@ -66,14 +69,39 @@ ohos_shared_library("distributeddatasvc") { "config/src/model/network_config.cpp", "config/src/model/protocol_config.cpp", "crypto/src/crypto_manager.cpp", - "data_share/bundle_mgr_proxy.cpp", - "data_share/data_share_profile_info.cpp", + "data_share/common/bundle_mgr_proxy.cpp", + "data_share/common/db_delegate.cpp", + "data_share/common/div_strategy.cpp", + "data_share/common/kv_delegate.cpp", + "data_share/common/rdb_delegate.cpp", + "data_share/common/scheduler_manager.cpp", + "data_share/common/seq_strategy.cpp", + "data_share/common/template_manager.cpp", + "data_share/common/uri_utils.cpp", + "data_share/data/published_data.cpp", + "data_share/data/template_data.cpp", "data_share/data_share_service_impl.cpp", "data_share/data_share_service_stub.cpp", "data_share/data_share_types_util.cpp", - "data_share/permission_proxy.cpp", - "data_share/rdb_adaptor.cpp", - "data_share/uri_utils.cpp", + "data_share/data_share_obs_proxy.cpp", + "data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp", + "data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp", + "data_share/strategies/delete_strategy.cpp", + "data_share/strategies/general/check_is_data_proxy_strategy.cpp", + "data_share/strategies/general/check_is_single_app_strategy.cpp", + "data_share/strategies/general/connect_extension_strategy.cpp", + "data_share/strategies/general/empty_strategy.cpp", + "data_share/strategies/general/load_config_common_strategy.cpp", + "data_share/strategies/general/load_config_data_info_strategy.cpp", + "data_share/strategies/general/load_config_from_bundle_info_strategy.cpp", + "data_share/strategies/general/permission_strategy.cpp", + "data_share/strategies/general/process_single_app_user_cross_strategy.cpp", + "data_share/strategies/get_data_strategy.cpp", + "data_share/strategies/insert_strategy.cpp", + "data_share/strategies/publish_strategy.cpp", + "data_share/strategies/query_strategy.cpp", + "data_share/strategies/subscribe_strategy.cpp", + "data_share/strategies/update_strategy.cpp", "directory/src/directory_manager.cpp", "kvdb/auth_delegate.cpp", "kvdb/executor_factory.cpp", @@ -114,11 +142,14 @@ ohos_shared_library("distributeddatasvc") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb:distributeddb", "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata:distributeddata_inner", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share/gaussdb_rd_simple:documentdb" ] external_deps = [ "ability_base:zuri", + "ability_base:want", "ability_runtime:dataobs_manager", + "ability_runtime:ability_manager", "access_token:libaccesstoken_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", @@ -131,6 +162,7 @@ ohos_shared_library("distributeddatasvc") { "ipc:ipc_core", "relational_store:native_rdb", "relational_store:rdb_data_share_adapter", + "relational_store:rdb_bms_adapter", "resource_management:global_resmgr", "samgr:samgr_proxy", ] diff --git a/services/distributeddataservice/service/data_share/bundle_mgr_proxy.cpp b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp similarity index 61% rename from services/distributeddataservice/service/data_share/bundle_mgr_proxy.cpp rename to services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp index ef7683a2..4519382f 100644 --- a/services/distributeddataservice/service/data_share/bundle_mgr_proxy.cpp +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp @@ -1,78 +1,91 @@ -/* - * 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 "BundleMgrProxy" -#include "bundle_mgr_proxy.h" - -#include "account/account_delegate.h" -#include "if_system_ability_manager.h" -#include "iservice_registry.h" -#include "log_print.h" -#include "system_ability_definition.h" - -namespace OHOS::DataShare { -sptr BundleMgrProxy::GetBundleMgrProxy() -{ - std::lock_guard lock(mutex_); - if (proxy_ != nullptr) { - return proxy_; - } - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (systemAbilityManager == nullptr) { - ZLOGE("Failed to get system ability mgr."); - return nullptr; - } - - sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (remoteObject == nullptr) { - ZLOGE("Failed to get bundle manager proxy."); - return nullptr; - } - sptr deathRecipient = new (std::nothrow) - BundleMgrProxy::ServiceDeathRecipient(this); - if (deathRecipient == nullptr) { - return nullptr; - } - remoteObject->AddDeathRecipient(deathRecipient); - proxy_ = iface_cast(remoteObject); - ZLOGD("Get bundle manager proxy success."); - return proxy_; -} - -bool BundleMgrProxy::GetBundleInfoFromBMS( - const std::string &bundleName, uint32_t tokenId, AppExecFwk::BundleInfo &bundleInfo) -{ - auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); - auto bmsClient = GetBundleMgrProxy(); - if (!bmsClient) { - ZLOGE("GetBundleMgrProxy is nullptr!"); - return false; - } - bool ret = bmsClient->GetBundleInfo( - bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, userId); - if (!ret) { - ZLOGE("GetBundleInfo failed!"); - return false; - } - return true; -} - -void BundleMgrProxy::OnProxyDied() -{ - std::lock_guard lock(mutex_); - proxy_ = nullptr; - ZLOGE("DIED."); -} +/* + * 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 "BundleMgrProxy" +#include "bundle_mgr_proxy.h" + +#include "account/account_delegate.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "log_print.h" +#include "system_ability_definition.h" + +namespace OHOS::DataShare { +sptr BundleMgrProxy::GetBundleMgrProxy() +{ + std::lock_guard lock(mutex_); + if (proxy_ != nullptr) { + return iface_cast(proxy_); + } + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + ZLOGE("Failed to get system ability mgr."); + return nullptr; + } + + proxy_ = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (proxy_ == nullptr) { + ZLOGE("Failed to get bundle manager proxy."); + return nullptr; + } + deathRecipient_ = new (std::nothrow)BundleMgrProxy::ServiceDeathRecipient(weak_from_this()); + if (deathRecipient_ == nullptr) { + ZLOGE("deathRecipient alloc failed."); + return nullptr; + } + proxy_->AddDeathRecipient(deathRecipient_); + return iface_cast(proxy_); +} + +bool BundleMgrProxy::GetBundleInfoFromBMS( + const std::string &bundleName, int32_t userId, AppExecFwk::BundleInfo &bundleInfo) +{ + auto bmsClient = GetBundleMgrProxy(); + if (bmsClient == nullptr) { + ZLOGE("GetBundleMgrProxy is nullptr!"); + return false; + } + bool ret = bmsClient->GetBundleInfo( + bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, userId); + if (!ret) { + ZLOGE("GetBundleInfo failed!bundleName is %{public}s, userId is %{public}d", bundleName.c_str(), userId); + return false; + } + return true; +} + +void BundleMgrProxy::OnProxyDied() +{ + std::lock_guard lock(mutex_); + if (proxy_ != nullptr) { + proxy_->RemoveDeathRecipient(deathRecipient_); + } + proxy_ = nullptr; +} + +BundleMgrProxy::~BundleMgrProxy() +{ + std::lock_guard lock(mutex_); + if (proxy_ != nullptr) { + proxy_->RemoveDeathRecipient(deathRecipient_); + } +} + +std::shared_ptr BundleMgrProxy::GetInstance() +{ + static std::shared_ptr proxy(new BundleMgrProxy()); + return proxy; +} } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/bundle_mgr_proxy.h b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h similarity index 61% rename from services/distributeddataservice/service/data_share/bundle_mgr_proxy.h rename to services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h index d36cb950..e66197a1 100644 --- a/services/distributeddataservice/service/data_share/bundle_mgr_proxy.h +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h @@ -1,46 +1,52 @@ -/* - * 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 DATASHARESERVICE_BUNDLEMGR_PROXY_H -#define DATASHARESERVICE_BUNDLEMGR_PROXY_H - -#include - -#include "bundlemgr/bundle_mgr_proxy.h" -namespace OHOS::DataShare { -class BundleMgrProxy { -public: - bool GetBundleInfoFromBMS(const std::string &bundleName, uint32_t tokenId, AppExecFwk::BundleInfo &bundleInfo); - -private: - class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { - public: - explicit ServiceDeathRecipient(BundleMgrProxy* owner) : owner_(owner) {} - void OnRemoteDied(const wptr &object) override - { - if (owner_ != nullptr) { - owner_->OnProxyDied(); - } - } - private: - BundleMgrProxy* owner_; - }; - sptr GetBundleMgrProxy(); - void OnProxyDied(); - std::mutex mutex_; - sptr proxy_; -}; -} // namespace OHOS::DataShare -#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H +/* + * 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 DATASHARESERVICE_BUNDLEMGR_PROXY_H +#define DATASHARESERVICE_BUNDLEMGR_PROXY_H + +#include +#include +#include "bundlemgr/bundle_mgr_proxy.h" +namespace OHOS::DataShare { +class BundleMgrProxy final : public std::enable_shared_from_this { +public: + ~BundleMgrProxy(); + static std::shared_ptr GetInstance(); + bool GetBundleInfoFromBMS(const std::string &bundleName, int32_t userId, AppExecFwk::BundleInfo &bundleInfo); + +private: + BundleMgrProxy() = default; + class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { + public: + explicit ServiceDeathRecipient(std::weak_ptr owner) : owner_(owner) {} + void OnRemoteDied(const wptr &object) override + { + auto owner = owner_.lock(); + if (owner != nullptr) { + owner->OnProxyDied(); + } + } + + private: + std::weak_ptr owner_; + }; + sptr GetBundleMgrProxy(); + void OnProxyDied(); + std::mutex mutex_; + sptr proxy_; + sptr deathRecipient_; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/common/context.h b/services/distributeddataservice/service/data_share/common/context.h new file mode 100644 index 00000000..7f6d2b4f --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/context.h @@ -0,0 +1,86 @@ +/* + * 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 DATASHARESERVICE_CONTEXT_H +#define DATASHARESERVICE_CONTEXT_H + +#include +#include +#include +#include + +#include "bundle_info.h" + +namespace OHOS::DataShare { +enum AccessSystemMode : uint8_t { + UNDEFINED, + USER_SHARED_MODE, + USER_SINGLE_MODE, + MAX, +}; +class Context { +public: + explicit Context() {} + explicit Context(const std::string &uri) : uri(uri) {} + virtual ~Context() = default; + std::string uri; + int32_t currentUserId = -1; + std::string permission; + uint32_t callerTokenId = -1; + std::string callerBundleName; + std::string calledBundleName; + std::string calledModuleName; + std::string calledStoreName; + std::string calledTableName; + std::string calledSourceDir; // the dir of db + int version = -1; + int errCode = -1; + bool isRead = false; + bool isPrivate = false; + AccessSystemMode accessSystemMode = AccessSystemMode::UNDEFINED; + OHOS::AppExecFwk::BundleInfo bundleInfo; + std::string type = "rdb"; + + + virtual std::string GetTpltProviderBundleName() { + return ""; + } + + virtual uint64_t GetTpltSubscriberId() { + return 0; + } +}; + +class TemplateContext : public Context { +public: + TemplateContext(const std::string &uri, const std::string &tpltProviderBundleName, int64_t subscriberId) + : Context(uri), tpltProviderBundleName(tpltProviderBundleName), subscriberId(subscriberId) + { + } + std::string GetTpltProviderBundleName() override + { + return tpltProviderBundleName; + } + uint64_t GetTpltSubscriberId() override + { + return subscriberId; + } + +private: + std::string tpltProviderBundleName; + int64_t subscriberId; +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp new file mode 100644 index 00000000..f4769c55 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -0,0 +1,71 @@ +/* + * 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 "DBAdaptor" +#include "db_delegate.h" +#include "rdb_delegate.h" +#include "kv_delegate.h" +namespace OHOS::DataShare { +std::shared_ptr DBDelegate::Create(const std::string &dir, int version, int &errCode, + bool registerFunction) +{ + return std::make_shared(dir, version, errCode, registerFunction); +} + +const std::string KvDBDelegate::TEMPLATE_TABLE = "template_"; +const std::string KvDBDelegate::DATA_TABLE = "data_"; + +std::shared_ptr KvDBDelegate::GetInstance(bool reInit, const std::string &dir) +{ + static std::shared_ptr delegate = nullptr; + static std::mutex mutex; + std::lock_guard lock(mutex); + if(delegate == nullptr || reInit) { + delegate = std::make_shared(dir); + } + return delegate; +} + +bool KvData::Marshal(DistributedData::Serializable::json &node) const +{ + auto ret = SetValue(node, *GetId()); + if (HasVersion()) { + ret &= SetValue(node, GetVersion()); + } + return ret & SetValue(node, GetValue()); +} + +bool Id::Marshal(DistributedData::Serializable::json &node) const +{ + return SetValue(node["_id"], id_); +} + +bool Id::Unmarshal(const DistributedData::Serializable::json &node) +{ + return GetValue(node, "_id", id_); +} + +Id::Id(const std::string &id) : id_(id) {} + +VersionData::VersionData(int version) : version_(version) {} + +bool VersionData::Unmarshal(const DistributedData::Serializable::json &node) +{ + return GetValue(node, "version", version_); +} +bool VersionData::Marshal(DistributedData::Serializable::json &node) const +{ + return SetValue(node["version"], version_); +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.h b/services/distributeddataservice/service/data_share/common/db_delegate.h new file mode 100644 index 00000000..5407826d --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/db_delegate.h @@ -0,0 +1,97 @@ +/* + * 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 DATASHARESERVICE_DB_DELEGATE_H +#define DATASHARESERVICE_DB_DELEGATE_H + +#include + +#include "concurrent_map.h" +#include "datashare_predicates.h" +#include "datashare_result_set.h" +#include "datashare_values_bucket.h" +#include "result_set.h" +#include "serializable/serializable.h" + +namespace OHOS::DataShare { +class DBDelegate { +public: + static std::shared_ptr Create(const std::string &dir, int version, int &errCode, + bool registerFunction = false); + virtual int64_t Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket) = 0; + virtual int64_t Update(const std::string &tableName, const DataSharePredicates &predicate, + const DataShareValuesBucket &valuesBucket) = 0; + virtual int64_t Delete(const std::string &tableName, const DataSharePredicates &predicate) = 0; + virtual std::shared_ptr Query(const std::string &tableName, + const DataSharePredicates &predicates, const std::vector &columns, int &errCode) = 0; + virtual std::shared_ptr Query( + const std::string &sql, const std::vector &selectionArgs = std::vector()) = 0; + virtual int ExecuteSql(const std::string &sql) = 0; +}; + +struct RdbStoreContext { + RdbStoreContext(const std::string &dir, int version) : dir_(dir), version_(version) {} + std::string dir_; + int version_; +}; + +struct Id final: public DistributedData::Serializable { + Id(const std::string &id); + ~Id() = default; + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + +private: + std::string id_; +}; + +class VersionData : public DistributedData::Serializable { +public: + VersionData(int version); + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + operator int() + { + return version_; + }; + +private: + int version_; +}; + +struct KvData : public DistributedData::Serializable { + virtual std::shared_ptr GetId() const = 0; + virtual bool HasVersion() const = 0; + virtual VersionData GetVersion() const = 0; + virtual const Serializable &GetValue() const = 0; + bool Marshal(json &node) const override; +}; + +class KvDBDelegate { +public: + static const std::string TEMPLATE_TABLE; + static const std::string DATA_TABLE; + static std::shared_ptr GetInstance(bool reInit = false, const std::string &dir = ""); + virtual ~KvDBDelegate() = default; + virtual int32_t Upsert(const std::string &collectionName, const KvData &value) = 0; + virtual int32_t DeleteById(const std::string &collectionName, const Id &id) = 0; + virtual int32_t Get(const std::string &collectionName, const Id &id, std::string &value) = 0; + virtual int32_t Get(const std::string &collectionName, const std::string &filter, const std::string &projection, + std::string &result) = 0; + virtual int32_t GetBatch(const std::string &collectionName, const std::string &filter, + const std::string &projection, std::vector &result) = 0; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_DB_DELEGATE_H diff --git a/services/distributeddataservice/service/data_share/common/div_strategy.cpp b/services/distributeddataservice/service/data_share/common/div_strategy.cpp new file mode 100644 index 00000000..afec15a0 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/div_strategy.cpp @@ -0,0 +1,28 @@ +/* + * 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 "DivStrategy" +#include "div_strategy.h" + +#include "log_print.h" +namespace OHOS::DataShare { +bool DivStrategy::operator()(std::shared_ptr context) +{ + if (check_ == nullptr || trueAction_ == nullptr || falseAction_ == nullptr) { + ZLOGE("wrong strategy"); + return false; + } + return (*check_)(context) ? (*trueAction_)(context) : (*falseAction_)(context); +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/div_strategy.h b/services/distributeddataservice/service/data_share/common/div_strategy.h new file mode 100644 index 00000000..5989a3dd --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/div_strategy.h @@ -0,0 +1,38 @@ +/* + * 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 DATASHARESERVICE_DIV_STRAGETY_H +#define DATASHARESERVICE_DIV_STRAGETY_H + +#include "context.h" +#include "strategy.h" + +namespace OHOS::DataShare { +class DivStrategy : public Strategy { +public: + DivStrategy(std::shared_ptr check, + std::shared_ptr trueAction, std::shared_ptr falseAction) + : check_(check), trueAction_(trueAction), falseAction_(falseAction) + { + } + bool operator()(std::shared_ptr context) override; + +private: + std::shared_ptr check_; + std::shared_ptr trueAction_; + std::shared_ptr falseAction_; +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp new file mode 100644 index 00000000..4be6ae0e --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp @@ -0,0 +1,222 @@ +/* + * 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 "KvAdaptor" +#include "kv_delegate.h" +#include "datashare_errno.h" +#include "directory_manager.h" +#include "grd_document/grd_document_api.h" +#include "grd_base/grd_error.h" +#include "ipc_skeleton.h" +#include "log_print.h" + +namespace OHOS::DataShare { +int64_t KvDelegate::Upsert(const std::string &collectionName, const std::string &filter, const std::string &value) +{ + std::lock_guard lock(mutex_); + if (!Init()) { + ZLOGE("init failed, %{public}s", collectionName.c_str()); + return E_ERROR; + } + int count = GRD_UpsertDoc(db_, collectionName.c_str(), filter.c_str(), value.c_str(), 0); + if (count <= 0) { + ZLOGE("GRD_UpSertDoc failed,status %{public}d", count); + return count; + } + Flush(); + return E_OK; +} + +int64_t KvDelegate::Delete(const std::string &collectionName, const std::string &filter) +{ + std::lock_guard lock(mutex_); + if (!Init()) { + ZLOGE("init failed, %{public}s", collectionName.c_str()); + return E_ERROR; + } + int count = GRD_DeleteDoc(db_, collectionName.c_str(), filter.c_str(), 0); + if (count <= 0) { + ZLOGE("GRD_UpSertDoc failed,status %{public}d", count); + return count; + } + Flush(); + return E_OK; +} + +bool KvDelegate::Init() +{ + if (isInitDone_) { + return true; + } + int status = GRD_DBOpen((path_ + "/dataShare.db").c_str(), nullptr, GRD_DB_OPEN_CREATE, &db_); + ZLOGE("GRD_DBOpen hanluhanlu,path111 %{public}s", path_.c_str()); + if (status != GRD_OK || db_ == nullptr) { + ZLOGE("GRD_DBOpen failed,status %{public}d", status); + return false; + } + + status = GRD_CreateCollection(db_, TEMPLATE_TABLE.c_str(), nullptr, 0); + if (status != GRD_OK) { + ZLOGE("GRD_CreateCollection template table failed,status %{public}d", status); + return false; + } + + status = GRD_CreateCollection(db_, DATA_TABLE.c_str(), nullptr, 0); + if (status != GRD_OK) { + ZLOGE("GRD_CreateCollection data table failed,status %{public}d", status); + return false; + } + isInitDone_ = true; + return true; +} + +KvDelegate::~KvDelegate() +{ + std::lock_guard lock(mutex_); + if (isInitDone_) { + int status = GRD_DBClose(db_, 0); + if (status != GRD_OK) { + ZLOGE("GRD_DBClose failed,status %{public}d", status); + } + } +} + +int32_t KvDelegate::Upsert(const std::string &collectionName, const KvData &value) +{ + std::string id = DistributedData::Serializable::Marshall(*value.GetId()); + if (value.HasVersion() && value.GetVersion() != 0) { + int version = -1; + if (GetVersion(collectionName, id, version)) { + if (value.GetVersion() <= version) { + ZLOGE("GetVersion failed,%{public}s id %{private}s %{public}d %{public}d", collectionName.c_str(), + id.c_str(), static_cast(value.GetVersion()), version); + return E_VERSION_NOT_NEWER; + } + } + } + return Upsert(collectionName, id, DistributedData::Serializable::Marshall(value.GetValue())); +} + +int32_t KvDelegate::DeleteById(const std::string &collectionName, const Id &id) +{ + return Delete(collectionName, DistributedData::Serializable::Marshall(id)); +} + +int32_t KvDelegate::Get(const std::string &collectionName, const Id &id, std::string &value) +{ + std::string filter = DistributedData::Serializable::Marshall(id); + if (Get(collectionName, filter, "{}", value) != E_OK) { + ZLOGE("Get failed, %{public}s %{public}s", collectionName.c_str(), filter.c_str()); + return false; + } + return true; +} + +bool KvDelegate::GetVersion(const std::string &collectionName, const std::string &filter, int &version) +{ + std::string value; + if (Get(collectionName, filter, "{}", value) != E_OK) { + ZLOGE("Get failed, %{public}s %{public}s", collectionName.c_str(), filter.c_str()); + return false; + } + VersionData data(-1); + if (!DistributedData::Serializable::Unmarshall(value, data)) { + ZLOGE("Unmarshall failed,data %{public}s", value.c_str()); + return false; + } + version = data; + return true; +} + +int32_t KvDelegate::Get( + const std::string &collectionName, const std::string &filter, const std::string &projection, std::string &result) +{ + std::lock_guard lock(mutex_); + if (!Init()) { + ZLOGE("init failed, %{public}s", collectionName.c_str()); + return E_ERROR; + } + Query query; + query.filter = filter.c_str(); + query.projection = projection.c_str(); + GRD_ResultSet *resultSet = nullptr; + int status = GRD_FindDoc(db_, collectionName.c_str(), query, 0, &resultSet); + if (status != GRD_OK || resultSet == nullptr) { + ZLOGE("GRD_FindDoc failed,status %{public}d", status); + return status; + } + status = GRD_Next(resultSet); + if (status != GRD_OK) { + GRD_FreeResultSet(resultSet); + ZLOGE("GRD_Next failed,status %{public}d", status); + return status; + } + char *value = nullptr; + status = GRD_GetValue(resultSet, &value); + if (status != GRD_OK || value == nullptr) { + GRD_FreeResultSet(resultSet); + ZLOGE("GRD_GetValue failed,status %{public}d", status); + return status; + } + result = value; + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + return E_OK; +} + +void KvDelegate::Flush() +{ + int status = GRD_Flush(db_, GRD_DB_FLUSH_ASYNC); + if (status != GRD_OK) { + ZLOGE("GRD_Flush failed,status %{public}d", status); + } +} + +int32_t KvDelegate::GetBatch(const std::string &collectionName, const std::string &filter, + const std::string &projection, std::vector &result) +{ + std::lock_guard lock(mutex_); + if (!Init()) { + ZLOGE("init failed, %{public}s", collectionName.c_str()); + return E_ERROR; + } + Query query; + query.filter = filter.c_str(); + query.projection = projection.c_str(); + GRD_ResultSet *resultSet; + int status = GRD_FindDoc(db_, collectionName.c_str(), query, 0, &resultSet); + if (status != GRD_OK || resultSet == nullptr) { + ZLOGE("GRD_UpSertDoc failed,status %{public}d", status); + return status; + } + char *value = nullptr; + while (GRD_Next(resultSet) == GRD_OK) { + status = GRD_GetValue(resultSet, &value); + if (status != GRD_OK || value == nullptr) { + GRD_FreeResultSet(resultSet); + ZLOGE("GRD_GetValue failed,status %{public}d", status); + return status; + } + result.emplace_back(value); + GRD_FreeValue(value); + } + GRD_FreeResultSet(resultSet); + return E_OK; +} +KvDelegate::KvDelegate(const std::string &path) : path_(path) { + if (path_.empty()) { + path_ = "/data/app/el1/0/database/distributeddata/kvdb"; // todo hanlu delete + } +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/kv_delegate.h b/services/distributeddataservice/service/data_share/common/kv_delegate.h new file mode 100644 index 00000000..e3a29031 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/kv_delegate.h @@ -0,0 +1,51 @@ +/* + * 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 DATASHARESERVICE_KV_DELEGATE_H +#define DATASHARESERVICE_KV_DELEGATE_H + +#include +#include + +#include "db_delegate.h" +#include "grd_base/grd_db_api.h" + +namespace OHOS::DataShare { +class KvDelegate final : public KvDBDelegate { +public: + KvDelegate(const std::string &path); + virtual ~KvDelegate(); + int32_t Upsert(const std::string &collectionName, const KvData &value) override; + int32_t DeleteById(const std::string &collectionName, const Id &id) override; + int32_t Get(const std::string &collectionName, const Id &id, std::string &value) override; + + int32_t Get(const std::string &collectionName, const std::string &filter, const std::string &projection, + std::string &result) override; + int32_t GetBatch(const std::string &collectionName, const std::string &filter, const std::string &projection, + std::vector &result) override; + +private: + bool Init(); + bool GetVersion(const std::string &collectionName, const std::string &filter, int &version); + int64_t Upsert(const std::string &collectionName, const std::string &filter, const std::string &value); + int64_t Delete(const std::string &collectionName, const std::string &filter); + void Flush(); + std::mutex mutex_; + std::string path_; + GRD_DB *db_ = nullptr; + bool isInitDone_ = false; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_KV_DELEGATE_H diff --git a/services/distributeddataservice/service/data_share/rdb_adaptor.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp similarity index 39% rename from services/distributeddataservice/service/data_share/rdb_adaptor.cpp rename to services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 82ba6da6..d4187e2f 100644 --- a/services/distributeddataservice/service/data_share/rdb_adaptor.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -1,143 +1,171 @@ -/* - * 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 "RdbAdaptor" -#include "rdb_adaptor.h" - -#include "log_print.h" -#include "permission_proxy.h" -#include "rdb_utils.h" -#include "rdb_errno.h" - -namespace OHOS::DataShare { -int32_t RdbAdaptor::Insert(const UriInfo &uriInfo, const DataShareValuesBucket &valuesBucket, int32_t userId) -{ - DistributedData::StoreMetaData metaData; - if (!PermissionProxy::QueryMetaData(uriInfo.bundleName, uriInfo.storeName, metaData, userId)) { - return -1; - } - int errCode = E_OK; - RdbDelegate delegate(metaData, errCode); - return delegate.Insert(uriInfo.tableName, valuesBucket); -} -int32_t RdbAdaptor::Update(const UriInfo &uriInfo, const DataSharePredicates &predicate, - const DataShareValuesBucket &valuesBucket, int32_t userId) -{ - DistributedData::StoreMetaData metaData; - if (!PermissionProxy::QueryMetaData(uriInfo.bundleName, uriInfo.storeName, metaData, userId)) { - return -1; - } - int errCode = E_OK; - RdbDelegate delegate(metaData, errCode); - return delegate.Update(uriInfo.tableName, predicate, valuesBucket); -} -int32_t RdbAdaptor::Delete(const UriInfo &uriInfo, const DataSharePredicates &predicate, int32_t userId) -{ - DistributedData::StoreMetaData metaData; - if (!PermissionProxy::QueryMetaData(uriInfo.bundleName, uriInfo.storeName, metaData, userId)) { - return -1; - } - int errCode = E_OK; - RdbDelegate delegate(metaData, errCode); - return delegate.Delete(uriInfo.tableName, predicate); -} -std::shared_ptr RdbAdaptor::Query(const UriInfo &uriInfo, const DataSharePredicates &predicates, - const std::vector &columns, int32_t userId, int &errCode) -{ - DistributedData::StoreMetaData metaData; - if (!PermissionProxy::QueryMetaData(uriInfo.bundleName, uriInfo.storeName, metaData, userId)) { - errCode = E_DB_NOT_EXIST; - return nullptr; - } - RdbDelegate delegate(metaData, errCode); - return delegate.Query(uriInfo.tableName, predicates, columns); -} - -RdbDelegate::RdbDelegate(const StoreMetaData &meta, int &errCode) -{ - RdbStoreConfig config(meta.dataDir); - config.SetCreateNecessary(false); - DefaultOpenCallback callback; - store_ = RdbHelper::GetRdbStore(config, meta.version, callback, errCode); - if (errCode != E_OK) { - ZLOGE("GetRdbStore failed, errCode is %{public}d, storeId is %{public}s", errCode, meta.storeId.c_str()); - } -} - -RdbDelegate::~RdbDelegate() -{ - ZLOGD("destroy"); -} - -int64_t RdbDelegate::Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket) -{ - if (store_ == nullptr) { - ZLOGE("store is null"); - return 0; - } - int64_t rowId = 0; - ValuesBucket bucket = RdbDataShareAdapter::RdbUtils::ToValuesBucket(valuesBucket); - int ret = store_->Insert(rowId, tableName, bucket); - if (ret != E_OK) { - ZLOGE("Insert failed %{public}d", ret); - } - return rowId; -} -int64_t RdbDelegate::Update(const std::string &tableName, const DataSharePredicates &predicate, - const DataShareValuesBucket &valuesBucket) -{ - if (store_ == nullptr) { - ZLOGE("store is null"); - return 0; - } - int rowId = 0; - ValuesBucket bucket = RdbDataShareAdapter::RdbUtils::ToValuesBucket(valuesBucket); - RdbPredicates predicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicate, tableName); - int ret = store_->Update(rowId, bucket, predicates); - if (ret != E_OK) { - ZLOGE("Insert failed %{public}d", ret); - } - return rowId; -} -int64_t RdbDelegate::Delete(const std::string &tableName, const DataSharePredicates &predicate) -{ - if (store_ == nullptr) { - ZLOGE("store is null"); - return 0; - } - int rowId = 0; - RdbPredicates predicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicate, tableName); - int ret = store_->Delete(rowId, predicates); - if (ret != E_OK) { - ZLOGE("Insert failed %{public}d", ret); - } - return rowId; -} -std::shared_ptr RdbDelegate::Query(const std::string &tableName, - const DataSharePredicates &predicates, const std::vector &columns) -{ - if (store_ == nullptr) { - ZLOGE("store is null"); - return nullptr; - } - RdbPredicates rdbPredicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicates, tableName); - std::shared_ptr resultSet = store_->QueryByStep(rdbPredicates, columns); - if (resultSet == nullptr) { - ZLOGE("Query failed"); - return nullptr; - } - auto bridge = RdbDataShareAdapter::RdbUtils::ToResultSetBridge(resultSet); - return std::make_shared(bridge); -} +/* + * 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 "RdbAdaptor" +#include "rdb_delegate.h" + +#include "log_print.h" +#include "rdb_utils.h" +#include "scheduler_manager.h" +#include "utils/anonymous.h" +namespace OHOS::DataShare { +constexpr static int32_t MAX_RESULTSET_COUNT = 16; +std::atomic RdbDelegate::resultSetCount = 0; +RdbDelegate::RdbDelegate(const std::string &dir, int version, int &errCode, bool registerFunction) +{ + RdbStoreConfig config(dir); + config.SetCreateNecessary(false); + DefaultOpenCallback callback; + store_ = RdbHelper::GetRdbStore(config, version, callback, errCode); + if (errCode != E_OK) { + ZLOGE("GetRdbStore failed, errCode is %{public}d, dir is %{public}s", errCode, + DistributedData::Anonymous::Change(dir).c_str()); + } +} + +int64_t RdbDelegate::Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket) +{ + if (store_ == nullptr) { + ZLOGE("store is null"); + return 0; + } + int64_t rowId = 0; + ValuesBucket bucket = RdbDataShareAdapter::RdbUtils::ToValuesBucket(valuesBucket); + int ret = store_->Insert(rowId, tableName, bucket); + if (ret != E_OK) { + ZLOGE("Insert failed %{public}s %{public}d", tableName.c_str(), ret); + } + return rowId; +} +int64_t RdbDelegate::Update( + const std::string &tableName, const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket) +{ + if (store_ == nullptr) { + ZLOGE("store is null"); + return 0; + } + int changeCount = 0; + ValuesBucket bucket = RdbDataShareAdapter::RdbUtils::ToValuesBucket(valuesBucket); + RdbPredicates predicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicate, tableName); + int ret = store_->Update(changeCount, bucket, predicates); + if (ret != E_OK) { + ZLOGE("Update failed %{public}s %{public}d", tableName.c_str(), ret); + } + return changeCount; +} +int64_t RdbDelegate::Delete(const std::string &tableName, const DataSharePredicates &predicate) +{ + if (store_ == nullptr) { + ZLOGE("store is null"); + return 0; + } + int changeCount = 0; + RdbPredicates predicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicate, tableName); + int ret = store_->Delete(changeCount, predicates); + if (ret != E_OK) { + ZLOGE("Delete failed %{public}s %{public}d", tableName.c_str(), ret); + } + return changeCount; +} +std::shared_ptr RdbDelegate::Query( + const std::string &tableName, const DataSharePredicates &predicates, + const std::vector &columns, int &errCode) +{ + if (store_ == nullptr) { + ZLOGE("store is null"); + return nullptr; + } + int count = resultSetCount.fetch_add(1); + ZLOGI("start query %{public}d", count); + if (count > MAX_RESULTSET_COUNT) { + ZLOGE("resultSetCount is full"); + resultSetCount--; + return nullptr; + } + RdbPredicates rdbPredicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicates, tableName); + std::shared_ptr resultSet = store_->QueryByStep(rdbPredicates, columns); + if (resultSet == nullptr) { + ZLOGE("Query failed %{public}s", tableName.c_str()); + resultSetCount--; + return nullptr; + } + auto bridge = RdbDataShareAdapter::RdbUtils::ToResultSetBridge(resultSet); + return std::shared_ptr(new DataShareResultSet(bridge), [](auto p) { + ZLOGI("release resultset"); + resultSetCount--; + delete p; + }); +} +class ResultSetJsonFormatter final: public DistributedData::Serializable { +public: + ResultSetJsonFormatter(const std::shared_ptr &resultSet) + : resultSet(resultSet) + { + } + ~ResultSetJsonFormatter() {} + bool Marshal(json &node) const override + { + int columnCount = 0; + auto result = resultSet->GetColumnCount(columnCount); + if (result != EOK) { + ZLOGE("GetColumnCount err, %{public}d", result); + return false; + } + while (resultSet->GoToNextRow() == E_OK) { + json result; + for (int i = 0; i < columnCount; i++) { + std::string columnName; + std::string value; + resultSet->GetColumnName(i, columnName); + resultSet->GetString(i, value); + SetValue(result[columnName], value); + } + node.push_back(result); + } + return true; + } + bool Unmarshal(const json &node) override + { + return false; + } + +private: + std::shared_ptr resultSet; +}; + +std::shared_ptr RdbDelegate::Query( + const std::string &sql, const std::vector &selectionArgs) +{ + if (store_ == nullptr) { + ZLOGE("store is null"); + return nullptr; + } + std::shared_ptr resultSet = store_->QueryByStep(sql, selectionArgs); + if (resultSet == nullptr) { + ZLOGE("Query failed %{private}s", sql.c_str()); + return nullptr; + } + + return std::make_shared(resultSet); +} + +int RdbDelegate::ExecuteSql(const std::string &sql) +{ + if (store_ == nullptr) { + ZLOGE("store is null"); + return -1; + } + return store_->ExecuteSql(sql); +} } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/rdb_adaptor.h b/services/distributeddataservice/service/data_share/common/rdb_delegate.h similarity index 52% rename from services/distributeddataservice/service/data_share/rdb_adaptor.h rename to services/distributeddataservice/service/data_share/common/rdb_delegate.h index 57e9a423..94669369 100644 --- a/services/distributeddataservice/service/data_share/rdb_adaptor.h +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.h @@ -1,70 +1,61 @@ -/* - * 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 DATASHARESERVICE_RDB_ADAPTOR_H -#define DATASHARESERVICE_RDB_ADAPTOR_H - -#include -#include - -#include "concurrent_map.h" -#include "datashare_predicates.h" -#include "datashare_result_set.h" -#include "datashare_values_bucket.h" -#include "metadata/store_meta_data.h" -#include "rdb_errno.h" -#include "rdb_helper.h" -#include "rdb_store.h" -#include "uri_utils.h" - -namespace OHOS::DataShare { -using StoreMetaData = OHOS::DistributedData::StoreMetaData; -using namespace OHOS::NativeRdb; -class RdbDelegate { -public: - RdbDelegate(const StoreMetaData &meta, int &errCode); - virtual ~RdbDelegate(); - int64_t Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket); - int64_t Update(const std::string &tableName, const DataSharePredicates &predicate, - const DataShareValuesBucket &valuesBucket); - int64_t Delete(const std::string &tableName, const DataSharePredicates &predicate); - std::shared_ptr Query(const std::string &tableName, const DataSharePredicates &predicates, - const std::vector &columns); - -private: - std::shared_ptr store_; -}; -class RdbAdaptor { -public: - static int32_t Insert(const UriInfo &uriInfo, const DataShareValuesBucket &valuesBucket, int32_t userId); - static int32_t Update(const UriInfo &uriInfo, const DataSharePredicates &predicate, - const DataShareValuesBucket &valuesBucket, int32_t userId); - static int32_t Delete(const UriInfo &uriInfo, const DataSharePredicates &predicate, int32_t userId); - static std::shared_ptr Query(const UriInfo &uriInfo, const DataSharePredicates &predicates, - const std::vector &columns, int32_t userId, int &errCode); -}; -class DefaultOpenCallback : public RdbOpenCallback { -public: - int OnCreate(RdbStore &rdbStore) override - { - return E_OK; - } - int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override - { - return E_OK; - } -}; -} // namespace OHOS::DataShare -#endif // DATASHARESERVICE_RDB_ADAPTOR_H +/* + * 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 DATASHARESERVICE_RDB_DELEGATE_H +#define DATASHARESERVICE_RDB_DELEGATE_H + +#include +#include + +#include "concurrent_map.h" +#include "db_delegate.h" +#include "rdb_errno.h" +#include "rdb_helper.h" +#include "rdb_store.h" +#include "uri_utils.h" +#include "rdb_utils.h" + +namespace OHOS::DataShare { +using namespace OHOS::NativeRdb; +class RdbDelegate final : public DBDelegate { +public: + explicit RdbDelegate(const std::string &dir, int version, int &errCode, bool registerFunction); + int64_t Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket) override; + int64_t Update(const std::string &tableName, const DataSharePredicates &predicate, + const DataShareValuesBucket &valuesBucket) override; + int64_t Delete(const std::string &tableName, const DataSharePredicates &predicate) override; + std::shared_ptr Query(const std::string &tableName, const DataSharePredicates &predicates, + const std::vector &columns, int &errCode) override; + std::shared_ptr Query( + const std::string &sql, const std::vector &selectionArgs) override; + int ExecuteSql(const std::string &sql) override; + +private: + static std::atomic resultSetCount; + std::shared_ptr store_; +}; +class DefaultOpenCallback : public RdbOpenCallback { +public: + int OnCreate(RdbStore &rdbStore) override + { + return E_OK; + } + int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override + { + return E_OK; + } +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_RDB_DELEGATE_H diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp new file mode 100644 index 00000000..23da5d84 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp @@ -0,0 +1,148 @@ +/* + * 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 "SchedulerManager" + +#include "scheduler_manager.h" + +#include "log_print.h" +#include "uri_utils.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +SchedulerManager &SchedulerManager::GetInstance() +{ + static SchedulerManager instance; + return instance; +} + +void SchedulerManager::Execute(const std::string &uri, const std::string &rdbDir, int version) +{ + if (!URIUtils::IsDataProxyURI(uri)) { + return; + } + int errCode = 0; + auto delegate = DBDelegate::Create(rdbDir, version, errCode, true); + if (delegate == nullptr) { + ZLOGE("malloc fail %{public}s", DistributedData::Anonymous::Change(uri).c_str()); + return; + } + std::vector keys = RdbSubscriberManager::GetInstance().GetKeysByUri(uri); + for (auto &key : keys) { + if (RdbSubscriberManager::GetInstance().GetObserverCount(key) == 0) { + continue; + } + ExecuteSchedulerSQL(key, delegate); + } +} + +void SchedulerManager::Execute(const Key &key, const std::string &rdbDir, int version) +{ + int errCode = 0; + auto delegate = DBDelegate::Create(rdbDir, version, errCode, true); + if (delegate == nullptr) { + ZLOGE("malloc fail %{public}s", DistributedData::Anonymous::Change(key.uri_).c_str()); + return; + } + ExecuteSchedulerSQL(key, delegate); +} + +void SchedulerManager::SetTimer(const RdbStoreContext &rdbContext, const std::string &uri, int64_t subscriberId, + const std::string &bundleName, int64_t reminderTime) +{ + std::lock_guard lock(mutex_); + if (scheduler_ == nullptr) { + scheduler_ = std::make_shared(TIME_TASK_NUM, "remind_timer"); + } + auto manager = RdbSubscriberManager::GetInstance(); + Key key(uri, subscriberId, bundleName); + auto it = timerCache_.find(key); + if (it != timerCache_.end()) { + // has current timer, reset time + std::time_t now = time(nullptr); + scheduler_->Reset(it->second, std::chrono::seconds(reminderTime - now)); + return; + } + // not find task in map, create new timer + std::time_t now = time(nullptr); + auto taskId = scheduler_->At(TaskScheduler::Clock::now() + std::chrono::seconds(reminderTime - now), [&]() { + // 1. execute schedulerSQL in next time + Execute(key, rdbContext.dir_, rdbContext.version_); + // 2. notify + RdbSubscriberManager::GetInstance().EmitByKey(key, rdbContext.dir_, rdbContext.version_); + }); + if (taskId == TaskScheduler::INVALID_TASK_ID) { + ZLOGE("create timer failed, over the max capacity"); + return; + } + timerCache_.emplace(key, taskId); +} + +void SchedulerManager::ExecuteSchedulerSQL(const Key &key, std::shared_ptr delegate) +{ + Template tpl; + if (!TemplateManager::GetInstance().GetTemplate(key.uri_, key.subscriberId_, key.bundleName_, tpl)) { + ZLOGE("template undefined, %{public}s, %{public}" PRId64 ", %{public}s", + DistributedData::Anonymous::Change(key.uri_).c_str(), key.subscriberId_, key.bundleName_.c_str()); + return; + } + if (tpl.scheduler_.empty()) { + ZLOGW("template scheduler_ empty, %{public}s, %{public}" PRId64 ", %{public}s", + DistributedData::Anonymous::Change(key.uri_).c_str(), key.subscriberId_, key.bundleName_.c_str()); + return; + } + + if (!GenRemindTimerFuncParams(key.uri_, key.subscriberId_, key.bundleName_, tpl.scheduler_)) { + return; + } + int errCode = delegate->ExecuteSql(tpl.scheduler_); + if (errCode != E_OK) { + ZLOGE("Execute schedulerSql failed, %{public}s, %{public}" PRId64 ", %{public}s", + DistributedData::Anonymous::Change(key.uri_).c_str(), key.subscriberId_, key.bundleName_.c_str()); + } +} + +bool SchedulerManager::GenRemindTimerFuncParams(const std::string &uri, int64_t subscriberId, + const std::string &bundleName, std::string &schedulerSQL) +{ + auto index = schedulerSQL.find("remindTimer("); + if (index == -1) { + ZLOGE("not find remindTimer, sql is %{public}s", schedulerSQL.c_str()); + return false; + } + index += strlen("remindTimer("); + std::string keyStr = "\"" + uri + "\", " + std::to_string(subscriberId) + ", \"" + bundleName + "\", "; + schedulerSQL.insert(index, keyStr); + return true; +} + +void SchedulerManager::RemoveTimer(const Key &key) +{ + std::lock_guard lock(mutex_); + if (scheduler_ == nullptr) { + ZLOGD("scheduler_ is nullptr"); + return; + } + auto it = timerCache_.find(key); + if (it != timerCache_.end()) { + ZLOGD("RemoveTimer %{public}s %{public}s %{public}" PRId64, + DistributedData::Anonymous::Change(key.uri_).c_str(), key.bundleName_.c_str(), key.subscriberId_); + scheduler_->Remove(it->second); + timerCache_.erase(key); + if (timerCache_.empty()) { + scheduler_ = nullptr; + } + } +} +} // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.h b/services/distributeddataservice/service/data_share/common/scheduler_manager.h new file mode 100644 index 00000000..dbe21438 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.h @@ -0,0 +1,48 @@ +/* + * 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 SCHEDULER_MANAGER_H +#define SCHEDULER_MANAGER_H + +#include + +#include "db_delegate.h" +#include "task_scheduler.h" +#include "template_manager.h" + +namespace OHOS::DataShare { +class SchedulerManager { +public: + static SchedulerManager &GetInstance(); + void Execute(const std::string &uri, const std::string &rdbDir, int version); + void Execute(const Key &key, const std::string &rdbDir, int version); + void SetTimer(const RdbStoreContext &rdbContext, const std::string &uri, int64_t subscriberId, + const std::string &bundleName, int64_t reminderTime); + void RemoveTimer(const Key &key); + +private: + static constexpr size_t TIME_TASK_NUM = 10; + SchedulerManager() = default; + ~SchedulerManager() = default; + bool GenRemindTimerFuncParams(const std::string &uri, int64_t subscriberId, const std::string &bundleName, + std::string &schedulerSQL); + void ExecuteSchedulerSQL(const Key &key, std::shared_ptr delegate = nullptr); + + std::recursive_mutex mutex_; + std::map timerCache_; + std::shared_ptr scheduler_; +}; +} // namespace OHOS::DataShare +#endif //SCHEDULER_MANAGER_H diff --git a/services/distributeddataservice/service/data_share/common/seq_strategy.cpp b/services/distributeddataservice/service/data_share/common/seq_strategy.cpp new file mode 100644 index 00000000..cad92ca6 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/seq_strategy.cpp @@ -0,0 +1,45 @@ +/* + * 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 "SeqStrategy" +#include "seq_strategy.h" + +#include "log_print.h" +namespace OHOS::DataShare { +bool SeqStrategy::operator()(std::shared_ptr context) +{ + for (auto &action : actions_) { + // true:success false:terminal + if (!(*action)(context)) { + return false; + } + } + return true; +} +bool SeqStrategy::Init(std::initializer_list strategies) +{ + for (auto &item: strategies) { + if (item == nullptr) { + actions_.clear(); + return false; + } + actions_.emplace_back(item); + } + return true; +} +bool SeqStrategy::IsEmpty() +{ + return actions_.empty(); +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/seq_strategy.h b/services/distributeddataservice/service/data_share/common/seq_strategy.h new file mode 100644 index 00000000..95bf655d --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/seq_strategy.h @@ -0,0 +1,35 @@ +/* + * 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 DATASHARESERVICE_SEQ_STRAGETY_H +#define DATASHARESERVICE_SEQ_STRAGETY_H + +#include + +#include "context.h" +#include "strategy.h" + +namespace OHOS::DataShare { +class SeqStrategy : public Strategy { +public: + bool IsEmpty(); + bool Init(std::initializer_list strategies); + bool operator()(std::shared_ptr context) override; + +private: + std::list> actions_; +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/common/strategy.h b/services/distributeddataservice/service/data_share/common/strategy.h new file mode 100644 index 00000000..0f603e8b --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/strategy.h @@ -0,0 +1,41 @@ +/* + * 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 DATASHARESERVICE_STRAGETY_H +#define DATASHARESERVICE_STRAGETY_H + +#include +#include + +#include "context.h" + +namespace OHOS::DataShare { +class Strategy { +public: + Strategy() = default; + Strategy(const Strategy &) = delete; + Strategy(const Strategy &&) = delete; + Strategy &operator=(const Strategy &) = delete; + Strategy &operator=(const Strategy &&) = delete; + virtual ~Strategy() + { + }; + virtual bool operator()(std::shared_ptr context) + { + return false; + }; +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp new file mode 100644 index 00000000..2877b3fc --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -0,0 +1,528 @@ +/* + * 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 "TemplateManager" + +#include "template_manager.h" + +#include "db_delegate.h" +#include "log_print.h" +#include "published_data.h" +#include "scheduler_manager.h" +#include "template_data.h" +#include "uri_utils.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +bool TemplateManager::GetTemplate( + const std::string &uri, const int64_t subscriberId, const std::string &bundleName, Template &tpl) +{ + auto delegate = KvDBDelegate::GetInstance(); + if (delegate == nullptr) { + ZLOGE("db open failed"); + return false; + } + TemplateData data(uri, bundleName, subscriberId, tpl); + std::string result; + bool find = delegate->Get(KvDBDelegate::TEMPLATE_TABLE, *data.GetId(), result); + if (!find) { + ZLOGE("db Get failed, %{public}s, %{public}" PRId64, bundleName.c_str(), subscriberId); + return false; + } + if (!DistributedData::Serializable::Unmarshall(result, data.value)) { + ZLOGE("Unmarshall failed, %{public}s, %{public}" PRId64, bundleName.c_str(), subscriberId); + return false; + } + tpl = data.value.tpl.tpl; + return true; +} + +bool TemplateManager::AddTemplate(const std::string &uri, const TemplateId &tplId, const Template &tpl) +{ + auto delegate = KvDBDelegate::GetInstance(); + if (delegate == nullptr) { + ZLOGE("db open failed"); + return false; + } + TemplateData data(uri, tplId.bundleName_, tplId.subscriberId_, tpl); + auto status = delegate->Upsert(KvDBDelegate::TEMPLATE_TABLE, data); + if (status != E_OK) { + ZLOGE("db Upsert failed, %{public}d", status); + return false; + } + return true; +} + +bool TemplateManager::DelTemplate(const std::string &uri, const TemplateId &tplId) +{ + auto delegate = KvDBDelegate::GetInstance(); + if (delegate == nullptr) { + ZLOGE("db open failed"); + return false; + } + Template tpl; + TemplateData data(uri, tplId.bundleName_, tplId.subscriberId_, tpl); + auto status = delegate->DeleteById(KvDBDelegate::TEMPLATE_TABLE, *data.GetId()); + if (status != E_OK) { + ZLOGE("db DeleteById failed, %{public}d", status); + return false; + } + SchedulerManager::GetInstance().RemoveTimer(Key(uri, tplId.subscriberId_, tplId.bundleName_)); + return true; +} + +Key::Key(const std::string &uri, const int64_t subscriberId, const std::string &bundleName) + : uri_(uri), subscriberId_(subscriberId), bundleName_(bundleName) +{ +} + +bool Key::operator==(const Key &rhs) const +{ + return uri_ == rhs.uri_ && subscriberId_ == rhs.subscriberId_ && bundleName_ == rhs.bundleName_; +} + +bool Key::operator!=(const Key &rhs) const +{ + return !(rhs == *this); +} +bool Key::operator<(const Key &rhs) const +{ + if (uri_ < rhs.uri_) + return true; + if (rhs.uri_ < uri_) + return false; + if (subscriberId_ < rhs.subscriberId_) + return true; + if (rhs.subscriberId_ < subscriberId_) + return false; + return bundleName_ < rhs.bundleName_; +} +bool Key::operator>(const Key &rhs) const +{ + return rhs < *this; +} +bool Key::operator<=(const Key &rhs) const +{ + return !(rhs < *this); +} +bool Key::operator>=(const Key &rhs) const +{ + return !(*this < rhs); +} + +TemplateManager::TemplateManager() {} + +TemplateManager &TemplateManager::GetInstance() +{ + static TemplateManager manager; + return manager; +} + +RdbSubscriberManager &RdbSubscriberManager::GetInstance() +{ + static RdbSubscriberManager manager; + return manager; +} + +int RdbSubscriberManager::AddRdbSubscriber(const std::string &uri, const TemplateId &tplId, + const sptr observer, std::shared_ptr context) +{ + int result = E_OK; + Key key(uri, tplId.subscriberId_, tplId.bundleName_); + rdbCache_.Compute( + key, [&observer, &context, &result, this](const auto &key, std::vector &value) { + ZLOGI("add subscriber, uri %{private}s tokenId %{public}d", key.uri_.c_str(), context->callerTokenId); + ObserverNode observerNode(observer, context->callerTokenId); + std::vector node({ observerNode }); + result = Notify(key, node, context->calledSourceDir, context->version); + if (result != E_OK) { + return false; + } + value.emplace_back(observerNode); + if (GetEnableObserverCount(key) == 1) { + SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); + } + return true; + }); + return result; +} + +int RdbSubscriberManager::DelRdbSubscriber( + const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId) +{ + Key key(uri, tplId.subscriberId_, tplId.bundleName_); + auto result = rdbCache_.ComputeIfPresent(key, [&callerTokenId, this](const auto &key, std::vector &value) { + ZLOGI("delete subscriber, uri %{public}s tokenId %{public}d", key.uri_.c_str(), callerTokenId); + for (auto it = value.begin(); it != value.end();) { + if (it->callerTokenId == callerTokenId) { + ZLOGI("erase start"); + it = value.erase(it); + } else { + it++; + } + } + if (GetEnableObserverCount(key) == 0) { + SchedulerManager::GetInstance().RemoveTimer(key); + } + return !value.empty(); + }); + return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; +} + +int RdbSubscriberManager::DisableRdbSubscriber( + const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId) +{ + Key key(uri, tplId.subscriberId_, tplId.bundleName_); + auto result = rdbCache_.ComputeIfPresent(key, [&callerTokenId, this](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end(); it++) { + if (it->callerTokenId == callerTokenId) { + it->enabled = false; + } + } + if (GetEnableObserverCount(key) == 0) { + SchedulerManager::GetInstance().RemoveTimer(key); + } + return true; + }); + return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; +} + +int RdbSubscriberManager::EnableRdbSubscriber(const std::string &uri, const TemplateId &tplId, std::shared_ptr context) +{ + Key key(uri, tplId.subscriberId_, tplId.bundleName_); + auto result = rdbCache_.ComputeIfPresent(key, [&context, this](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end(); it++) { + if (it->callerTokenId == context->callerTokenId) { + it->enabled = true; + ObserverNode observerNode(it->observer, context->callerTokenId); + std::vector node({ observerNode }); + Notify(key, node, context->calledSourceDir, context->version); + if (GetEnableObserverCount(key) == 1) { + SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); + } + } + } + return true; + }); + return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; +} + +class JsonFormatter : public DistributedData::Serializable { +public: + JsonFormatter(const std::string &key, const std::shared_ptr &value) + : key_(key), value_(value) + { + } + bool Marshal(json &node) const override + { + if (value_ == nullptr) { + ZLOGE("null value %{public}s", key_.c_str()); + return false; + } + // ZLOGE("key %{public}s value %{public}s", key_.c_str(), DistributedData::Serializable::Marshall(value_).c_str()); + return SetValue(node[key_], *value_); + } + bool Unmarshal(const json &node) override + { + if (value_ == nullptr) { + ZLOGE("null value %{public}s", key_.c_str()); + return false; + } + return GetValue(node, key_, *value_); + } + +private: + std::string key_; + std::shared_ptr value_; +}; + +void RdbSubscriberManager::Emit(const std::string &uri, std::shared_ptr context) +{ + if (!URIUtils::IsDataProxyURI(uri)) { + return; + } + rdbCache_.ForEach([&uri, &context, this](const Key &key, std::vector &val) { + if (key.uri_ != uri) { + return false; + } + Notify(key, val, context->calledSourceDir, context->version); + return false; + }); +} + +std::vector RdbSubscriberManager::GetKeysByUri(const std::string &uri) +{ + std::vector results; + rdbCache_.ForEach([&uri, &results](const Key &key, std::vector &val) { + if (key.uri_ != uri) { + return false; + } + results.emplace_back(key); + return false; + }); + return results; +} + +void RdbSubscriberManager::EmitByKey(const Key &key, const std::string &rdbPath, int version) +{ + if (!URIUtils::IsDataProxyURI(key.uri_)) { + return; + } + rdbCache_.ComputeIfPresent(key, [&rdbPath, &version, this](const Key &key, std::vector &val) { + Notify(key, val, rdbPath, version); + return true; + }); +} + +int RdbSubscriberManager::GetObserverCount(const Key &key) +{ + auto pair = rdbCache_.Find(key); + if (!pair.first) { + return 0; + } + return pair.second.size(); +} + +int RdbSubscriberManager::GetEnableObserverCount(const Key &key) +{ + auto pair = rdbCache_.Find(key); + if (!pair.first) { + return 0; + } + int count = 0; + for (const auto &observer : pair.second) { + if (observer.enabled) { + count++; + } + } + return count; +} + +int RdbSubscriberManager::Notify( + const Key &key, std::vector &val, const std::string &rdbDir, int rdbVersion) +{ + Template tpl; + if (!TemplateManager::GetInstance().GetTemplate(key.uri_, key.subscriberId_, key.bundleName_, tpl)) { + ZLOGE("template undefined, %{public}s, %{public}" PRId64 ", %{public}s", + DistributedData::Anonymous::Change(key.uri_).c_str(), key.subscriberId_, key.bundleName_.c_str()); + return E_TEMPLATE_NOT_EXIST; + } + int errCode; + auto delegate = DBDelegate::Create(rdbDir, rdbVersion, errCode); + if (delegate == nullptr) { + ZLOGE("malloc fail %{public}s %{public}s", DistributedData::Anonymous::Change(key.uri_).c_str(), + key.bundleName_.c_str()); + return errCode; + } + RdbChangeNode changeNode; + changeNode.uri_ = key.uri_; + changeNode.templateId_.subscriberId_ = key.subscriberId_; + changeNode.templateId_.bundleName_ = key.bundleName_; + for (const auto &predicate : tpl.predicates_) { + JsonFormatter formatter(predicate.key_, delegate->Query(predicate.selectSql_)); + changeNode.data_.emplace_back(DistributedData::Serializable::Marshall(formatter)); + } + + ZLOGI("emit, size %{public}d %{private}s", val.size(), changeNode.uri_.c_str()); + for (auto &callback : val) { + if (callback.enabled && callback.observer != nullptr) { + callback.observer->OnChangeFromRdb(changeNode); + } + } + return E_OK; +} + +PublishedDataSubscriberManager &PublishedDataSubscriberManager::GetInstance() +{ + static PublishedDataSubscriberManager manager; + return manager; +} + +int PublishedDataSubscriberManager::AddSubscriber(const std::string &key, const std::string &callerBundleName, + const int64_t subscriberId, const sptr observer, const uint32_t callerTokenId) +{ + PublishedDataKey publishedDataKey(key, callerBundleName, subscriberId); + publishedDataCache_.Compute( + publishedDataKey, [&observer, &callerTokenId, this](const PublishedDataKey &key, std::vector &value) { + ZLOGI("add publish subscriber, uri %{private}s tokenId %{public}d", key.key_.c_str(), callerTokenId); + value.emplace_back(observer, callerTokenId); + return true; + }); + return E_OK; +} + +int PublishedDataSubscriberManager::DelSubscriber(const std::string &uri, const std::string &callerBundleName, + const int64_t subscriberId, const uint32_t callerTokenId) +{ + PublishedDataKey key(uri, callerBundleName, subscriberId); + auto result = publishedDataCache_.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end();) { + if (it->callerTokenId == callerTokenId) { + it = value.erase(it); + } else { + it++; + } + } + return !value.empty(); + }); + return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; +} + +int PublishedDataSubscriberManager::DisableSubscriber(const std::string &uri, const std::string &callerBundleName, + const int64_t subscriberId, const uint32_t callerTokenId) +{ + PublishedDataKey key(uri, callerBundleName, subscriberId); + auto result = publishedDataCache_.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end(); it++) { + if (it->callerTokenId == callerTokenId) { + it->enabled = false; + } + } + return true; + }); + return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; +} + +int PublishedDataSubscriberManager::EnableSubscriber(const std::string &uri, const std::string &callerBundleName, + const int64_t subscriberId, const uint32_t callerTokenId) +{ + PublishedDataKey key(uri, callerBundleName, subscriberId); + auto result = publishedDataCache_.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end(); it++) { + if (it->callerTokenId == callerTokenId) { + it->enabled = true; + } + } + return true; + }); + return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; +} + +void PublishedDataSubscriberManager::Emit( + std::vector keys, const std::string &ownerBundleName, const sptr observer) +{ + int32_t status; + // key is bundleName, value is change node + std::map publishedResult; + std::map, std::vector> callbacks; + publishedDataCache_.ForEach([&keys, &status, &observer, &publishedResult, &callbacks]( + const PublishedDataKey &key, std::vector &val) { + for (auto &data : keys) { + ZLOGE("HANLU start try emit %{public}s %{public}s %{public}" PRId64, data.bundleName_.c_str(), + data.key_.c_str(), data.subscriberId_); + ZLOGE("HANLU observer %{public}s %{public}s %{public}" PRId64, key.bundleName_.c_str(), key.key_.c_str(), + key.subscriberId_); + if (key != data) { + continue; + } + publishedResult[key].subscriberId_ = data.subscriberId_; + publishedResult[key].key_ = data.key_; + PublishedData publishedData(data.key_, data.bundleName_, data.subscriberId_); + status = PublishedData::Query(DistributedData::Serializable::Marshall(*publishedData.GetId()), publishedResult[key].value_); + if (status != E_OK) { + ZLOGE("query fail %{public}s %{public}s %{public}" PRId64, data.bundleName_.c_str(), data.key_.c_str(), + data.subscriberId_); + publishedResult.erase(key); + continue; + } + break; + } + for (auto &callback : val) { + if (callback.enabled && callback.observer != nullptr) { + // callback the observer, others do not call + if (observer != nullptr && callback.observer != observer) { + continue; + } + callbacks[callback.observer].emplace_back(key); + } + } + return false; + }); + PublishedDataChangeNode result; + for (auto &[callback, keys] : callbacks) { + result.datas_.clear(); + for (auto &key : keys) { + if (publishedResult.count(key) != 0) { + result.datas_.emplace_back(publishedResult[key]); + } + } + if (result.datas_.empty()) { + continue; + } + result.ownerBundleName_ = ownerBundleName; + ZLOGE("HANLU emit start %{public}d", result.datas_.size()); + callback->OnChangeFromPublishedData(result); + } +} + +PublishedDataKey::PublishedDataKey(const std::string &key, const std::string &bundleName, const int64_t subscriberId) + : key_(key), bundleName_(bundleName), subscriberId_(subscriberId) +{ + /* private published data can use key as simple uri */ + /* etc: datashareproxy://{bundleName}/meeting can use meeting replaced */ + /* if key is normal uri, bundleName is from uri */ + if (URIUtils::IsDataProxyURI(key_)) { + URIUtils::GetBundleNameFromProxyURI(key_, bundleName_); + } +} + +bool PublishedDataKey::operator<(const PublishedDataKey &rhs) const +{ + if (key_ < rhs.key_) + return true; + if (rhs.key_ < key_) + return false; + if (bundleName_ < rhs.bundleName_) + return true; + if (rhs.bundleName_ < bundleName_) + return false; + return subscriberId_ < rhs.subscriberId_; +} + +bool PublishedDataKey::operator>(const PublishedDataKey &rhs) const +{ + return rhs < *this; +} + +bool PublishedDataKey::operator<=(const PublishedDataKey &rhs) const +{ + return !(rhs < *this); +} + +bool PublishedDataKey::operator>=(const PublishedDataKey &rhs) const +{ + return !(*this < rhs); +} + +bool PublishedDataKey::operator==(const PublishedDataKey &rhs) const +{ + return key_ == rhs.key_ && bundleName_ == rhs.bundleName_ && subscriberId_ == rhs.subscriberId_; +} + +bool PublishedDataKey::operator!=(const PublishedDataKey &rhs) const +{ + return !(rhs == *this); +} + +RdbSubscriberManager::ObserverNode::ObserverNode(const sptr &observer, uint32_t callerTokenId) + : observer(observer), callerTokenId(callerTokenId) +{ +} + +PublishedDataSubscriberManager::ObserverNode::ObserverNode( + const sptr &observer, uint32_t callerTokenId) + : observer(observer), callerTokenId(callerTokenId) +{ +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/template_manager.h b/services/distributeddataservice/service/data_share/common/template_manager.h new file mode 100644 index 00000000..4929b04f --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/template_manager.h @@ -0,0 +1,116 @@ +/* + * 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 DATASHARESERVICE_TEMPLATE_MANAGER_H +#define DATASHARESERVICE_TEMPLATE_MANAGER_H + +#include +#include + +#include "concurrent_map.h" +#include "datashare_template.h" +#include "data_proxy_observer.h" +#include "context.h" +namespace OHOS::DataShare { +struct Key { + Key(const std::string &uri, const int64_t subscriberId, const std::string &bundleName); + bool operator==(const Key &rhs) const; + bool operator!=(const Key &rhs) const; + bool operator<(const Key &rhs) const; + bool operator>(const Key &rhs) const; + bool operator<=(const Key &rhs) const; + bool operator>=(const Key &rhs) const; + const std::string uri_; + const int64_t subscriberId_; + const std::string bundleName_; +}; +class TemplateManager { +public: + static TemplateManager &GetInstance(); + bool AddTemplate(const std::string &uri, const TemplateId &tplId, const Template &tpl); + bool DelTemplate(const std::string &uri, const TemplateId &tplId); + bool GetTemplate(const std::string &uri, int64_t subscriberId, const std::string &bundleName, Template &tpl); + +private: + TemplateManager(); + friend class RdbSubscriberManager; +}; + +class RdbSubscriberManager { +public: + static RdbSubscriberManager &GetInstance(); + int AddRdbSubscriber(const std::string &uri, const TemplateId &tplId, const sptr observer, + std::shared_ptr context); + int DelRdbSubscriber(const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId); + int DisableRdbSubscriber( + const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId); + int EnableRdbSubscriber(const std::string &uri, const TemplateId &tplId, std::shared_ptr context); + void Emit(const std::string &uri, std::shared_ptr context); + void EmitByKey(const Key &key, const std::string &rdbPath, int version); + int GetObserverCount(const Key &key); + std::vector GetKeysByUri(const std::string &uri); + +private: + struct ObserverNode { + ObserverNode(const sptr &observer, uint32_t callerTokenId); + sptr observer; + uint32_t callerTokenId; + bool enabled = true; + }; + RdbSubscriberManager() = default; + ConcurrentMap> rdbCache_; + int Notify(const Key &key, std::vector &val, const std::string &rdbDir, int rdbVersion); + int GetEnableObserverCount(const Key &key); +}; + +struct PublishedDataKey { + PublishedDataKey(const std::string &key, const std::string &bundleName, const int64_t subscriberId); + bool operator<(const PublishedDataKey &rhs) const; + bool operator>(const PublishedDataKey &rhs) const; + bool operator<=(const PublishedDataKey &rhs) const; + bool operator>=(const PublishedDataKey &rhs) const; + bool operator==(const PublishedDataKey &rhs) const; + bool operator!=(const PublishedDataKey &rhs) const; + std::string key_; + std::string bundleName_; + int64_t subscriberId_; +}; + +class PublishedDataSubscriberManager { +public: + static PublishedDataSubscriberManager &GetInstance(); + int AddSubscriber(const std::string &key, const std::string &callerBundleName, const int64_t subscriberId, + const sptr observer, const uint32_t callerTokenId); + int DelSubscriber(const std::string &key, const std::string &callerBundleName, const int64_t subscriberId, + const uint32_t callerTokenId); + int DisableSubscriber(const std::string &key, const std::string &callerBundleName, const int64_t subscriberId, + const uint32_t callerTokenId); + int EnableSubscriber(const std::string &key, const std::string &callerBundleName, const int64_t subscriberId, + const uint32_t callerTokenId); + void Emit(const std::vector keys, const std::string &ownerBundleName, + const sptr observer = nullptr); + +private: + struct ObserverNode { + ObserverNode(const sptr &observer, uint32_t callerTokenId); + sptr observer; + uint32_t callerTokenId; + bool enabled = true; + }; + PublishedDataSubscriberManager() = default; + ConcurrentMap> publishedDataCache_; +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/uri_utils.cpp b/services/distributeddataservice/service/data_share/common/uri_utils.cpp similarity index 46% rename from services/distributeddataservice/service/data_share/uri_utils.cpp rename to services/distributeddataservice/service/data_share/common/uri_utils.cpp index 5a26fc9e..bc8ac9c2 100644 --- a/services/distributeddataservice/service/data_share/uri_utils.cpp +++ b/services/distributeddataservice/service/data_share/common/uri_utils.cpp @@ -1,66 +1,79 @@ -/* - * 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 "URIUtils" - -#include "uri_utils.h" - -#include "log_print.h" -#include "string_ex.h" -#include "uri.h" - -namespace OHOS::DataShare { -bool URIUtils::GetInfoFromURI(const std::string &uri, UriInfo &uriInfo, bool tableNameEmpty) -{ - Uri uriTemp(uri); - std::vector splitUri; - SplitStr(uriTemp.GetPath(), "/", splitUri); - if (!IsValidPath(splitUri, tableNameEmpty)) { - ZLOGE("Invalid uri: %{public}s", uri.c_str()); - return false; - } - - uriInfo.bundleName = splitUri[BUNDLE_NAME]; - uriInfo.moduleName = splitUri[MODULE_NAME]; - uriInfo.storeName = splitUri[STORE_NAME]; - if (splitUri.size() > OPTIONAL_BEGIN) { - uriInfo.tableName = splitUri[TABLE_NAME]; - } - return true; -} - -bool URIUtils::IsValidPath(const std::vector &splitUri, bool tableNameEmpty) -{ - if (splitUri.size() < OPTIONAL_BEGIN) { - return false; - } - if (splitUri[BUNDLE_NAME].empty() || splitUri[MODULE_NAME].empty() || - splitUri[STORE_NAME].empty()) { - ZLOGE("Uri has empty field!"); - return false; - } - - if (!tableNameEmpty) { - if (splitUri.size() < PARAM_BUTT) { - ZLOGE("Uri need contains tableName"); - return false; - } - if (splitUri[TABLE_NAME].empty()) { - ZLOGE("Uri tableName can't be empty!"); - return false; - } - } - return true; -} +/* + * 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 "URIUtils" + +#include "uri_utils.h" + +#include "log_print.h" +#include "string_ex.h" +#include "uri.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +const std::string URIUtils::DATA_SHARE_SCHEMA = "datashare:///"; +const std::string URIUtils::DATA_PROXY_SCHEMA = "datashareproxy://"; +bool URIUtils::GetInfoFromURI(const std::string &uri, UriInfo &uriInfo) +{ + Uri uriTemp(uri); + std::vector splitUri; + SplitStr(uriTemp.GetPath(), "/", splitUri); + if (splitUri.size() < PARAM_SIZE) { + ZLOGE("Invalid uri: %{public}s", DistributedData::Anonymous::Change(uri).c_str()); + return false; + } + + if (splitUri[BUNDLE_NAME].empty() || splitUri[MODULE_NAME].empty() || + splitUri[STORE_NAME].empty() || splitUri[TABLE_NAME].empty()) { + ZLOGE("Uri has empty field! bundleName: %{public}s uri: %{public}s", splitUri[BUNDLE_NAME].c_str(), + DistributedData::Anonymous::Change(uri).c_str()); + return false; + } + + uriInfo.bundleName = splitUri[BUNDLE_NAME]; + uriInfo.moduleName = splitUri[MODULE_NAME]; + uriInfo.storeName = splitUri[STORE_NAME]; + uriInfo.tableName = splitUri[TABLE_NAME]; + return true; +} + +bool URIUtils::IsDataProxyURI(const std::string &uri) +{ + return uri.compare(0, URIUtils::DATA_PROXY_SCHEMA.size(), URIUtils::DATA_PROXY_SCHEMA) == 0; +} + +bool URIUtils::GetBundleNameFromProxyURI(const std::string &uri, std::string &bundleName) +{ + Uri uriTemp(uri); + if (!uriTemp.GetAuthority().empty()) { + bundleName = uriTemp.GetAuthority(); + } + return true; +} + +bool URIUtils::GetUserIdFromProxyURI(const std::string &uri, int32_t &user) +{ + Uri uriTemp(uri); + std::vector splitUri; + SplitStr(uriTemp.GetQuery(), "=", splitUri); + for (auto iter = splitUri.begin(); iter < splitUri.end(); iter++) { + if (*iter == "user" && iter + 1 < splitUri.end()) { + const char *value = (iter + 1)->c_str(); + user = atoi(value); + return true; + } + } + return false; +} } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/uri_utils.h b/services/distributeddataservice/service/data_share/common/uri_utils.h similarity index 68% rename from services/distributeddataservice/service/data_share/uri_utils.h rename to services/distributeddataservice/service/data_share/common/uri_utils.h index 7e2f29d3..9366ba38 100644 --- a/services/distributeddataservice/service/data_share/uri_utils.h +++ b/services/distributeddataservice/service/data_share/common/uri_utils.h @@ -1,51 +1,46 @@ -/* - * 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 DATASHARESERVICE_URI_UTILS_H -#define DATASHARESERVICE_URI_UTILS_H - -#include -#include - -namespace OHOS::DataShare { -struct UriInfo { - std::string bundleName; - std::string moduleName; - std::string storeName; - std::string tableName; - - void SetTableName(const std::string &name) - { - tableName = name; - } -}; - -class URIUtils { -public: - static bool GetInfoFromURI(const std::string &uri, UriInfo &uriInfo, bool tableNameEmpty = false); - -private: - static bool IsValidPath(const std::vector &splitUri, bool tableNameEmpty); - enum PATH_PARAM : int32_t { - BUNDLE_NAME = 0, - MODULE_NAME, - STORE_NAME, - OPTIONAL_BEGIN, - TABLE_NAME = OPTIONAL_BEGIN, - PARAM_BUTT - }; -}; -} // namespace OHOS::DataShare -#endif // DATASHARESERVICE_URI_UTILS_H +/* + * 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 DATASHARESERVICE_URI_UTILS_H +#define DATASHARESERVICE_URI_UTILS_H + +#include +namespace OHOS::DataShare { +struct UriInfo { + std::string bundleName; + std::string moduleName; + std::string storeName; + std::string tableName; +}; + +class URIUtils { +public: + static bool GetInfoFromURI(const std::string &uri, UriInfo &uriInfo); + static bool GetBundleNameFromProxyURI(const std::string &uri, std::string &bundleName); + static bool GetUserIdFromProxyURI(const std::string &uri, int32_t &user); + static bool IsDataProxyURI(const std::string &uri); + const static std::string DATA_SHARE_SCHEMA; + const static std::string DATA_PROXY_SCHEMA; + +private: + enum PATH_PARAM : int32_t { BUNDLE_NAME = 0, + MODULE_NAME, + STORE_NAME, + TABLE_NAME, + PARAM_SIZE + }; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_URI_UTILS_H diff --git a/services/distributeddataservice/service/data_share/data/published_data.cpp b/services/distributeddataservice/service/data_share/data/published_data.cpp new file mode 100644 index 00000000..22a607ff --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/published_data.cpp @@ -0,0 +1,179 @@ +/* + * 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 "PublishedData" +#include "published_data.h" + +#include "log_print.h" + +namespace OHOS::DataShare { +bool PublishedData::HasVersion() const +{ + return true; +} + +VersionData PublishedData::GetVersion() const +{ + return value.version; +} + +const DistributedData::Serializable &PublishedData::GetValue() const +{ + return value; +} + +bool PublishedData::Unmarshal(const DistributedData::Serializable::json &node) +{ + return value.Unmarshal(node); +} + +PublishedData::PublishedData(const std::string &key, const std::string &bundleName, int64_t subscriberId, + const std::variant, std::string> &value, const int version) + : value(key, bundleName, subscriberId, value, version) +{ +} + +std::vector PublishedData::Query(const std::string &bundleName) +{ + auto delegate = KvDBDelegate::GetInstance(); + if (delegate == nullptr) { + ZLOGE("db open failed"); + return std::vector(); + } + std::vector queryResults; + json filter; + filter["bundleName"] = bundleName; + int32_t status = + delegate->GetBatch(KvDBDelegate::DATA_TABLE, filter.dump(), PublishedData::GetFullProjection(), queryResults); + if (status != E_OK) { + ZLOGE("db Upsert failed, %{public}s %{public}d", bundleName.c_str(), status); + return std::vector(); + } + std::vector results; + for (auto &result : queryResults) { + PublishedData data; + if (data.Unmarshall(result)) { + results.push_back(std::move(data)); + } + } + return results; +} + +bool PublishedDataNode::Marshal(DistributedData::Serializable::json &node) const +{ + bool ret = SetValue(node["key"], key); + ret = ret && SetValue(node["bundleName"], bundleName); + ret = ret && SetValue(node["subscriberId"], subscriberId); + ret = ret && SetValue(node, version); + if (value.index() == 1) { + std::string valueStr = std::get(value); + ret = ret && SetValue(node["type"], PublishedDataType::STRING); + ret = ret && SetValue(node["value"], valueStr); + } else { + sptr ashmem = std::get>(value); + if (ashmem == nullptr) { + ZLOGE("get ashmem null"); + return false; + } + const uint8_t *data = static_cast(ashmem->ReadFromAshmem(ashmem->GetAshmemSize(), 0)); + if (data == nullptr) { + ZLOGE("ReadFromAshmem null"); + return false; + } + ret = ret && SetValue(node["type"], PublishedDataType::ASHMEM); + node["value"] = std::vector(data, data + ashmem->GetAshmemSize()); + } + std::time_t now = time(nullptr); + ret = ret && SetValue(node["timestamp"], now); + return ret; +} + +bool PublishedDataNode::Unmarshal(const DistributedData::Serializable::json &node) +{ + bool ret = GetValue(node, "key", key); + ret = ret && GetValue(node, "bundleName", bundleName); + ret = ret && GetValue(node, "subscriberId", subscriberId); + ret = ret && version.Unmarshal(node); + int32_t type = 0; + ret = ret && GetValue(node, "type", type); + if (!ret) { + ZLOGE("Unmarshal PublishedDataNode failed, %{public}s", key.c_str()); + return false; + } + if (type == PublishedDataType::STRING) { + std::string strValue; + ret = ret && GetValue(node, "value", strValue); + value = strValue; + } else if (type == PublishedDataType::ASHMEM) { + std::vector binaryData = node["value"]; + std::string ashmemName = "PublishedData" + key + "_" + bundleName + "_" + std::to_string(subscriberId); + auto ashmem = Ashmem::CreateAshmem(ashmemName.c_str(), binaryData.size()); + if (ashmem == nullptr) { + ZLOGE("SharedBlock: CreateAshmem function error."); + return false; + } + + bool ret = ashmem->MapReadAndWriteAshmem(); + if (!ret) { + ZLOGE("SharedBlock: MapReadAndWriteAshmem function error."); + ashmem->CloseAshmem(); + return false; + } + ashmem->WriteToAshmem(&binaryData[0], binaryData.size(), 0); + value = ashmem; + } + return ret; +} + +PublishedDataNode::PublishedDataNode(const std::string &key, const std::string &bundleName, int64_t subscriberId, + const std::variant, std::string> &value, const VersionData &version) + : key(key), bundleName(bundleName), subscriberId(subscriberId), value(value), version(version) +{ +} + +std::string PublishedData::GetFullProjection() +{ + json projection; + projection["key"] = 1; + projection["bundleName"] = 1; + projection["type"] = 1; + projection["value"] = 1; + projection["subscriberId"] = 1; + projection["version"] = 1; + return projection.dump(); +} +int32_t PublishedData::Query( + const std::string &filter, std::variant, std::string> &publishedData) +{ + auto delegate = KvDBDelegate::GetInstance(); + if (delegate == nullptr) { + ZLOGE("db open failed"); + return E_ERROR; + } + std::string queryResult; + int32_t status = + delegate->Get(KvDBDelegate::DATA_TABLE, filter, "{}", queryResult); + if (status != E_OK) { + ZLOGE("db Get failed, %{public}s %{public}d", filter.c_str(), status); + return status; + } + PublishedData data; + if(!data.Unmarshall(queryResult)) { + ZLOGE("Unmarshall failed, %{private}s", queryResult.c_str()); + return E_ERROR; + } + publishedData = data.value.value; + return E_OK; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/published_data.h b/services/distributeddataservice/service/data_share/data/published_data.h new file mode 100644 index 00000000..459b477c --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/published_data.h @@ -0,0 +1,61 @@ +/* + * 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 DATASHARESERVICE_PUBLISHED_DATA_H +#define DATASHARESERVICE_PUBLISHED_DATA_H + +#include "db_delegate.h" +#include "serializable/serializable.h" + +namespace OHOS::DataShare { +enum PublishedDataType : int32_t +{ + STRING, + ASHMEM +}; +struct PublishedDataNode final: public DistributedData::Serializable { + PublishedDataNode(const std::string &key, const std::string &bundleName, int64_t subscriberId, + const std::variant, std::string> &value, const VersionData &version); + ~PublishedDataNode() = default; + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + std::string key; + std::string bundleName; + int64_t subscriberId; + std::variant, std::string> value; + VersionData version; +}; + +struct PublishedData final: public KvData { + static std::vector Query(const std::string &bundleName); + static int32_t Query(const std::string &filter, std::variant, std::string> &publishedData); + PublishedData(const std::string &key = "", const std::string &bundleName = "", int64_t subscriberId = 0, + const std::variant, std::string> &value = "", const int version = 0); + ~PublishedData() = default; + std::shared_ptr GetId() const override + { + return std::make_shared(value.key + "_" + std::to_string(value.subscriberId) + "_" + value.bundleName); + } + bool HasVersion() const override; + VersionData GetVersion() const override; + const DistributedData::Serializable &GetValue() const override; + bool Unmarshal(const json &node) override; + PublishedDataNode value; + +private: + static std::string GetFullProjection(); +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/data/template_data.cpp b/services/distributeddataservice/service/data_share/data/template_data.cpp new file mode 100644 index 00000000..fbaaffa7 --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/template_data.cpp @@ -0,0 +1,95 @@ +/* + * 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 "TemplateData" +#include "template_data.h" + +namespace OHOS::DataShare { +bool TemplateNode::Marshal(DistributedData::Serializable::json &node) const +{ + bool ret; + for (auto &predicate : tpl.predicates_) { + ret = SetValue(node["predicates"][predicate.key_], predicate.selectSql_); + } + ret = ret && SetValue(node["scheduler"], tpl.scheduler_); + return ret; +} + +bool TemplateNode::Unmarshal(const DistributedData::Serializable::json &node) +{ + auto &predicatesNode = node["predicates"]; + for (auto it = predicatesNode.begin(); it != predicatesNode.end(); ++it) { + tpl.predicates_.emplace_back(static_cast(it.key()), static_cast(it.value())); + } + bool ret = GetValue(node, "scheduler", tpl.scheduler_); + return ret; +} + +TemplateNode::TemplateNode(const Template &tpl) : tpl(tpl) {} + +bool TemplateRootNode::Marshal(DistributedData::Serializable::json &node) const +{ + bool ret = SetValue(node["uri"], uri); + ret = ret && SetValue(node["bundleName"], bundleName); + ret = ret && SetValue(node["subscriberId"], subscriberId); + ret = ret && SetValue(node["template"], tpl); + return ret; +} + +bool TemplateRootNode::Unmarshal(const DistributedData::Serializable::json &node) +{ + bool ret = GetValue(node, "uri", uri); + ret = ret && GetValue(node, "bundleName", bundleName); + ret = ret && GetValue(node, "subscriberId", subscriberId); + ret = ret && GetValue(node, "template", tpl); + return ret; +} + +TemplateRootNode::TemplateRootNode( + const std::string &uri, const std::string &bundleName, int64_t subscriberId, const Template &tpl) + : uri(uri), bundleName(bundleName), subscriberId(subscriberId), tpl(tpl) +{ +} + +std::shared_ptr TemplateData::GetId() const +{ + return std::make_shared(value.uri + "_" + std::to_string(value.subscriberId) + "_" + value.bundleName); +} + +bool TemplateData::HasVersion() const +{ + return false; +} + +VersionData TemplateData::GetVersion() const +{ + return VersionData(0); +} + +const DistributedData::Serializable &TemplateData::GetValue() const +{ + return value; +} + +bool TemplateData::Unmarshal(const DistributedData::Serializable::json &node) +{ + return value.Unmarshal(node); +} + +TemplateData::TemplateData( + const std::string &uri, const std::string &bundleName, int64_t subscriberId, const Template &tpl) + : value(uri, bundleName, subscriberId, tpl) +{ +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data_share_profile_info.h b/services/distributeddataservice/service/data_share/data/template_data.h similarity index 40% rename from services/distributeddataservice/service/data_share/data_share_profile_info.h rename to services/distributeddataservice/service/data_share/data/template_data.h index b943aaf1..1c8558d3 100644 --- a/services/distributeddataservice/service/data_share/data_share_profile_info.h +++ b/services/distributeddataservice/service/data_share/data/template_data.h @@ -1,57 +1,54 @@ -/* - * 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 DATA_SHARE_PROFILE_INFO_H -#define DATA_SHARE_PROFILE_INFO_H - -#include "bundle_info.h" -#include "uri_utils.h" -#include "serializable/serializable.h" -#include "resource_manager.h" - -namespace OHOS::DataShare { -using namespace OHOS::Global::Resource; -struct Config final : public DistributedData::Serializable { - std::string uri = "*"; - int crossUserMode = 0; - std::string writePermission = ""; - std::string readPermission = ""; - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; -}; - -struct ProfileInfo : public DistributedData::Serializable { - std::vector tableConfig; - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; -}; - -class DataShareProfileInfo { -public: - DataShareProfileInfo() = default; - bool GetProfileInfoFromExtension(const AppExecFwk::BundleInfo &bundleInfo, - ProfileInfo &profileInfo, bool &isSingleApp); - -private: - std::vector GetResProfileByMetadata(const std::vector &metadata, - const std::string &resourcePath, bool isCompressed) const; - std::shared_ptr InitResMgr(const std::string &basicString) const; - std::vector GetResFromResMgr(const std::string &resName, ResourceManager &resMgr, - bool isCompressed) const; - std::string ReadProfile(const std::string &resPath) const; - bool IsFileExisted(const std::string &filePath) const; -}; -} // namespace OHOS::DataShare -#endif // DATA_SHARE_PROFILE_INFO_H +/* + * 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 DATASHARESERVICE_TEMPLATE_DATA_H +#define DATASHARESERVICE_TEMPLATE_DATA_H + +#include "datashare_template.h" +#include "db_delegate.h" +#include "serializable/serializable.h" + +namespace OHOS::DataShare { +struct TemplateNode : public DistributedData::Serializable { + TemplateNode() = default; + TemplateNode(const Template &tpl); + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + Template tpl; +}; + +struct TemplateRootNode : public DistributedData::Serializable { + TemplateRootNode() = default; + TemplateRootNode(const std::string &uri, const std::string &bundleName, int64_t subscriberId, const Template &tpl); + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + std::string uri; + std::string bundleName; + int64_t subscriberId; + TemplateNode tpl; +}; + +struct TemplateData final : public KvData { + TemplateData() = default; + TemplateData(const std::string &uri, const std::string &bundleName, int64_t subscriberId, const Template &tpl); + std::shared_ptr GetId() const override; + bool HasVersion() const override; + VersionData GetVersion() const override; + const DistributedData::Serializable &GetValue() const override; + bool Unmarshal(const json &node) override; + TemplateRootNode value; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/data_proxy_observer.h b/services/distributeddataservice/service/data_share/data_proxy_observer.h new file mode 100644 index 00000000..40aeff58 --- /dev/null +++ b/services/distributeddataservice/service/data_share/data_proxy_observer.h @@ -0,0 +1,37 @@ +/* + * 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 DATA_PROXY_OBSERVER_H +#define DATA_PROXY_OBSERVER_H + +#include +#include "datashare_template.h" + +namespace OHOS { +namespace DataShare { +class IDataProxyRdbObserver : public OHOS::IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DataShare.IDataProxyRdbObserver"); + virtual void OnChangeFromRdb(RdbChangeNode &changeNode) = 0; +}; + +class IDataProxyPublishedDataObserver : public OHOS::IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DataShare.IDataProxyPublishedDataObserver"); + virtual void OnChangeFromPublishedData(PublishedDataChangeNode &changeNode) = 0; +}; +} // namespace DataShare +} // namespace OHOS +#endif //DATA_PROXY_OBSERVER_H diff --git a/services/distributeddataservice/service/data_share/data_share_obs_proxy.cpp b/services/distributeddataservice/service/data_share/data_share_obs_proxy.cpp new file mode 100644 index 00000000..ee9b6015 --- /dev/null +++ b/services/distributeddataservice/service/data_share/data_share_obs_proxy.cpp @@ -0,0 +1,58 @@ +// +// Created by niudongyao on 2023/3/17. +// +#define LOG_TAG "ObserverProxy" +#include "data_share_obs_proxy.h" + +#include "itypes_util.h" +#include "log_print.h" + +namespace OHOS { +namespace DataShare { +static constexpr int REQUEST_CODE = 0; +void RdbObserverProxy::OnChangeFromRdb(RdbChangeNode &changeNode) +{ + MessageParcel parcel; + if (!parcel.WriteInterfaceToken(RdbObserverProxy::GetDescriptor())) { + return ; + } + + if (!ITypesUtil::Marshal(parcel, changeNode)) { + ZLOGE("failed to WriteParcelable changeNode "); + return; + } + + MessageParcel reply; + MessageOption option; + int32_t result = Remote()->SendRequest(REQUEST_CODE, parcel, reply, option); + if (result == ERR_NONE) { + ZLOGI("SendRequest ok, retval is %{public}d", reply.ReadInt32()); + } else { + ZLOGE("SendRequest error, result=%{public}d", result); + return; + } +} + +void PublishedDataObserverProxy::OnChangeFromPublishedData(PublishedDataChangeNode &changeNode) { + MessageParcel parcel; + if (!parcel.WriteInterfaceToken(PublishedDataObserverProxy::GetDescriptor())) { + return ; + } + + if (!ITypesUtil::Marshal(parcel, changeNode)) { + ZLOGE("failed to WriteParcelable changeNode "); + return; + } + + MessageParcel reply; + MessageOption option; + int32_t result = Remote()->SendRequest(REQUEST_CODE, parcel, reply, option); + if (result == ERR_NONE) { + ZLOGI("SendRequest ok, retval is %{public}d", reply.ReadInt32()); + } else { + ZLOGE("SendRequest error, result=%{public}d", result); + return; + } +} +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data_share_obs_proxy.h b/services/distributeddataservice/service/data_share/data_share_obs_proxy.h new file mode 100644 index 00000000..4bad326d --- /dev/null +++ b/services/distributeddataservice/service/data_share/data_share_obs_proxy.h @@ -0,0 +1,45 @@ +/* + * 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 DATASHARESERVICE_DATA_SHARE_OBS_PROXY_H +#define DATASHARESERVICE_DATA_SHARE_OBS_PROXY_H + +#include "iremote_proxy.h" +#include "data_proxy_observer.h" +#include "datashare_template.h" + +namespace OHOS::DataShare { +class RdbObserverProxy : public IRemoteProxy { +public: + explicit RdbObserverProxy(const sptr& remote) : IRemoteProxy(remote) {} + virtual ~RdbObserverProxy() {} + void OnChangeFromRdb(RdbChangeNode &changeNode) override; + +private: + static inline BrokerDelegator delegator_; +}; + +class PublishedDataObserverProxy : public IRemoteProxy { +public: + explicit PublishedDataObserverProxy(const sptr& remote) + : IRemoteProxy(remote) {} + virtual ~PublishedDataObserverProxy() {} + void OnChangeFromPublishedData(PublishedDataChangeNode &changeNode) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace OHOS::DataShare +#endif //DATA_SHARE_OBS_PROXY_H diff --git a/services/distributeddataservice/service/data_share/data_share_profile_info.cpp b/services/distributeddataservice/service/data_share/data_share_profile_info.cpp deleted file mode 100644 index da563ec8..00000000 --- a/services/distributeddataservice/service/data_share/data_share_profile_info.cpp +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright (c) 2022-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 "DataShareProfileInfo" -#include "data_share_profile_info.h" - -#include -#include -#include -#include - -#include "bundle_info.h" -#include "log_print.h" - -namespace OHOS::DataShare { -namespace { - constexpr const char* DATA_SHARE_PROFILE_META = "ohos.extension.dataShare"; - constexpr const char* PROFILE_FILE_PREFIX = "$profile:"; - const size_t PROFILE_PREFIX_LEN = strlen(PROFILE_FILE_PREFIX); -} -bool Config::Marshal(json &node) const -{ - SetValue(node[GET_NAME(uri)], uri); - SetValue(node[GET_NAME(crossUserMode)], crossUserMode); - SetValue(node[GET_NAME(readPermission)], readPermission); - SetValue(node[GET_NAME(writePermission)], writePermission); - return true; -} - -bool Config::Unmarshal(const json &node) -{ - bool ret = true; - ret = GetValue(node, GET_NAME(uri), uri) && ret; - GetValue(node, GET_NAME(crossUserMode), crossUserMode); - GetValue(node, GET_NAME(readPermission), readPermission); - GetValue(node, GET_NAME(writePermission), writePermission); - return ret; -} - -bool ProfileInfo::Marshal(json &node) const -{ - SetValue(node[GET_NAME(tableConfig)], tableConfig); - return true; -} - -bool ProfileInfo::Unmarshal(const json &node) -{ - return GetValue(node, GET_NAME(tableConfig), tableConfig); -} - -bool DataShareProfileInfo::GetProfileInfoFromExtension(const AppExecFwk::BundleInfo &bundleInfo, - ProfileInfo &profileInfo, bool &isSingleApp) -{ - isSingleApp = bundleInfo.singleton; - // non singleApp don't need get profileInfo - if (!isSingleApp) { - return true; - } - - for (auto const &item : bundleInfo.extensionInfos) { - if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { - bool isCompressed = !item.hapPath.empty(); - std::string resourcePath = isCompressed ? item.hapPath : item.resourcePath; - std::vector infos = GetResProfileByMetadata(item.metadata, resourcePath, isCompressed); - if (infos.empty()) { - ZLOGE("failed, bundleName is %{public}s, resourcePath is %{public}s, metadata.size is %{public}zu,", - bundleInfo.name.c_str(), resourcePath.c_str(), item.metadata.size()); - return false; - } - return profileInfo.Unmarshall(infos[0]); - } - } - ZLOGE("not find datashare extension, bundleName is %{public}s", bundleInfo.name.c_str()); - return false; -} - -std::vector DataShareProfileInfo::GetResProfileByMetadata( - const std::vector &metadata, const std::string &resourcePath, bool isCompressed) const -{ - std::vector profileInfos; - if (metadata.empty() || resourcePath.empty()) { - return profileInfos; - } - std::shared_ptr resMgr = InitResMgr(resourcePath); - if (resMgr == nullptr) { - return profileInfos; - } - - for (auto const &meta : metadata) { - if (meta.name == DATA_SHARE_PROFILE_META) { - return GetResFromResMgr(meta.resource, *resMgr, isCompressed); - } - } - return profileInfos; -} - -std::shared_ptr DataShareProfileInfo::InitResMgr(const std::string &resourcePath) const -{ - std::shared_ptr resMgr(CreateResourceManager()); - if (resMgr == nullptr) { - return nullptr; - } - - std::unique_ptr resConfig(CreateResConfig()); - if (resConfig == nullptr) { - return nullptr; - } - resMgr->UpdateResConfig(*resConfig); - - if (!resMgr->AddResource(resourcePath.c_str())) { - ZLOGE("add resource failed, resourcePath is %{public}s", resourcePath.c_str()); - return nullptr; - } - return resMgr; -} - -std::vector DataShareProfileInfo::GetResFromResMgr(const std::string &resName, ResourceManager &resMgr, - bool isCompressed) const -{ - std::vector profileInfos; - if (resName.empty()) { - return profileInfos; - } - - size_t pos = resName.rfind(PROFILE_FILE_PREFIX); - if ((pos == std::string::npos) || (pos == resName.length() - PROFILE_PREFIX_LEN)) { - ZLOGE("res name invalid, resName is %{public}s", resName.c_str()); - return profileInfos; - } - std::string profileName = resName.substr(pos + PROFILE_PREFIX_LEN); - // hap is compressed status, get file content. - if (isCompressed) { - ZLOGD("compressed status."); - std::unique_ptr fileContent = nullptr; - size_t len = 0; - RState ret = resMgr.GetProfileDataByName(profileName.c_str(), len, fileContent); - if (ret != SUCCESS || fileContent == nullptr) { - ZLOGE("failed, ret is %{public}d, profileName is %{public}s", - ret, profileName.c_str()); - return profileInfos; - } - if (len == 0) { - ZLOGE("fileContent is empty, profileName is %{public}s", profileName.c_str()); - return profileInfos; - } - std::string rawData(fileContent.get(), fileContent.get() + len); - if (!Config::IsJson(rawData)) { - ZLOGE("rawData is not json, profileName is %{public}s", profileName.c_str()); - return profileInfos; - } - profileInfos.push_back(std::move(rawData)); - return profileInfos; - } - // hap is decompressed status, get file path then read file. - std::string resPath; - RState ret = resMgr.GetProfileByName(profileName.c_str(), resPath); - if (ret != SUCCESS) { - ZLOGE("profileName not found, ret is %{public}d, profileName is %{public}s", ret, profileName.c_str()); - return profileInfos; - } - std::string profile = ReadProfile(resPath); - if (profile.empty()) { - ZLOGE("Read profile failed, resPath is %{public}s", resPath.c_str()); - return profileInfos; - } - profileInfos.push_back(std::move(profile)); - return profileInfos; -} - -bool DataShareProfileInfo::IsFileExisted(const std::string &filePath) const -{ - if (filePath.empty()) { - return false; - } - if (access(filePath.c_str(), F_OK) != 0) { - ZLOGE("can not access file, errno is %{public}d, filePath is %{public}s", errno, filePath.c_str()); - return false; - } - return true; -} - -std::string DataShareProfileInfo::ReadProfile(const std::string &resPath) const -{ - if (!IsFileExisted(resPath)) { - return ""; - } - std::fstream in; - in.open(resPath, std::ios_base::in | std::ios_base::binary); - if (!in.is_open()) { - ZLOGE("the file can not open, errno is %{public}d", errno); - return ""; - } - in.seekg(0, std::ios::end); - int64_t size = in.tellg(); - if (size <= 0) { - ZLOGE("the file is empty, resPath is %{public}s", resPath.c_str()); - return ""; - } - in.seekg(0, std::ios::beg); - std::ostringstream tmp; - tmp << in.rdbuf(); - return tmp.str(); -} -} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 4fc2ca94..0262a403 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -19,13 +19,23 @@ #include "accesstoken_kit.h" #include "account/account_delegate.h" -#include "bundle_constants.h" +#include "bootstrap.h" #include "dataobs_mgr_client.h" +#include "datashare_errno.h" +#include "datashare_template.h" +#include "delete_strategy.h" +#include "directory_manager.h" +#include "get_data_strategy.h" +#include "hap_token_info.h" +#include "insert_strategy.h" #include "ipc_skeleton.h" #include "log_print.h" -#include "rdb_adaptor.h" -#include "uri.h" -#include "uri_utils.h" +#include "publish_strategy.h" +#include "query_strategy.h" +#include "scheduler_manager.h" +#include "subscribe_strategy.h" +#include "template_manager.h" +#include "update_strategy.h" #include "utils/anonymous.h" namespace OHOS::DataShare { @@ -33,53 +43,29 @@ using FeatureSystem = DistributedData::FeatureSystem; __attribute__((used)) DataShareServiceImpl::Factory DataShareServiceImpl::factory_; DataShareServiceImpl::Factory::Factory() { - FeatureSystem::GetInstance().RegisterCreator("data_share", - []() { return std::make_shared(); }); + FeatureSystem::GetInstance().RegisterCreator("data_share", []() { + return std::make_shared(); + }); } -DataShareServiceImpl::Factory::~Factory() -{ -} +DataShareServiceImpl::Factory::~Factory() {} int32_t DataShareServiceImpl::Insert(const std::string &uri, const DataShareValuesBucket &valuesBucket) { ZLOGD("Insert enter."); - UriInfo uriInfo; - if (!URIUtils::GetInfoFromURI(uri, uriInfo)) { - ZLOGE("GetInfoFromURI failed!"); - return ERROR; - } - - uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); - AppExecFwk::BundleInfo bundleInfo; - if (!PermissionProxy::GetBundleInfo(uriInfo.bundleName, tokenId, bundleInfo)) { - ZLOGE("GetBundleInfo failed, BundleName is %{public}s, tokenId is %{public}x", - uriInfo.bundleName.c_str(), tokenId); - return ERROR; - } - - auto permissionState = VerifyPermission(tokenId, PermissionType::WRITE_PERMISSION, bundleInfo); - if (permissionState == PermissionProxy::PermissionState::DENIED) { - ZLOGE("Verify permission failed, bundleName is %{public}s, tokenId is %{public}x", - uriInfo.bundleName.c_str(), tokenId); - return ERROR; + auto context = std::make_shared(uri); + auto ret = InsertStrategy::Execute(context, valuesBucket); + if (ret) { + NotifyChange(uri); + RdbSubscriberManager::GetInstance().Emit(uri, context); + SchedulerManager::GetInstance().Execute(uri, context->calledSourceDir, context->version); + return ret; } - - uriInfo.SetTableName(GetRealityTableName(tokenId, bundleInfo, uriInfo)); - auto userId = GetUserId(tokenId, bundleInfo.singleton); - int32_t ret = RdbAdaptor::Insert(uriInfo, valuesBucket, userId); - if (ret == ERROR) { - ZLOGE("Insert error, uri is %{public}s", DistributedData::Anonymous::Change(uri).c_str()); - return ERROR; - } - NotifyChange(uri); return ret; } bool DataShareServiceImpl::NotifyChange(const std::string &uri) { - ZLOGD("Start"); - ZLOGE("NotifyChange Uri = %{public}s", DistributedData::Anonymous::Change(uri).c_str()); auto obsMgrClient = AAFwk::DataObsMgrClient::GetInstance(); if (obsMgrClient == nullptr) { ZLOGE("obsMgrClient is nullptr"); @@ -98,136 +84,305 @@ int32_t DataShareServiceImpl::Update(const std::string &uri, const DataSharePred const DataShareValuesBucket &valuesBucket) { ZLOGD("Update enter."); - UriInfo uriInfo; - if (!URIUtils::GetInfoFromURI(uri, uriInfo)) { - ZLOGE("GetInfoFromURI failed!"); - return ERROR; - } - uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); - AppExecFwk::BundleInfo bundleInfo; - if (!PermissionProxy::GetBundleInfo(uriInfo.bundleName, tokenId, bundleInfo)) { - ZLOGE("GetBundleInfo failed, BundleName is %{public}s, tokenId is %{public}x", - uriInfo.bundleName.c_str(), tokenId); - return ERROR; - } - - auto permissionState = VerifyPermission(tokenId, PermissionType::WRITE_PERMISSION, bundleInfo); - if (permissionState == PermissionProxy::PermissionState::DENIED) { - ZLOGE("Verify permission failed, bundleName is %{public}s, tokenId is %{public}x", - uriInfo.bundleName.c_str(), tokenId); - return ERROR; + auto context = std::make_shared(uri); + auto ret = UpdateStrategy::Execute(context, predicate, valuesBucket); + if (ret) { + NotifyChange(uri); + RdbSubscriberManager::GetInstance().Emit(uri, context); + SchedulerManager::GetInstance().Execute(uri, context->calledSourceDir, context->version); + return ret; } - - uriInfo.SetTableName(GetRealityTableName(tokenId, bundleInfo, uriInfo)); - auto userId = GetUserId(tokenId, bundleInfo.singleton); - int32_t ret = RdbAdaptor::Update(uriInfo, predicate, valuesBucket, userId); - if (ret == ERROR) { - ZLOGE("Update error, uri is %{public}s", DistributedData::Anonymous::Change(uri).c_str()); - return ERROR; - } - NotifyChange(uri); return ret; } int32_t DataShareServiceImpl::Delete(const std::string &uri, const DataSharePredicates &predicate) { ZLOGD("Delete enter."); - UriInfo uriInfo; - if (!URIUtils::GetInfoFromURI(uri, uriInfo)) { - ZLOGE("GetInfoFromURI failed!"); - return ERROR; + auto context = std::make_shared(uri); + auto ret = DeleteStrategy::Execute(context, predicate); + if (ret) { + NotifyChange(uri); + RdbSubscriberManager::GetInstance().Emit(uri, context); + SchedulerManager::GetInstance().Execute(uri, context->calledSourceDir, context->version); + return ret; } + return ret; +} + +std::shared_ptr DataShareServiceImpl::Query(const std::string &uri, + const DataSharePredicates &predicates, const std::vector &columns, int &errCode) +{ + ZLOGD("Query enter."); + auto context = std::make_shared(uri); + return QueryStrategy::Execute(context, predicates, columns, errCode); +} - uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); - AppExecFwk::BundleInfo bundleInfo; - if (!PermissionProxy::GetBundleInfo(uriInfo.bundleName, tokenId, bundleInfo)) { - ZLOGE("GetBundleInfo failed, BundleName is %{public}s, tokenId is %{public}x", - uriInfo.bundleName.c_str(), tokenId); +int32_t DataShareServiceImpl::AddTemplate(const std::string &uri, const int64_t subscriberId, const Template &tplt) +{ + TemplateId tpltId; + tpltId.subscriberId_ = subscriberId; + if (!GetCallerBundleName(tpltId.bundleName_)) { + ZLOGE("get bundleName error, %{public}s", DistributedData::Anonymous::Change(uri).c_str()); return ERROR; } + return TemplateManager::GetInstance().AddTemplate(uri, tpltId, tplt); +} - auto permissionState = VerifyPermission(tokenId, PermissionType::WRITE_PERMISSION, bundleInfo); - if (permissionState == PermissionProxy::PermissionState::DENIED) { - ZLOGE("Verify permission failed, bundleName is %{public}s, tokenId is %{public}x", - uriInfo.bundleName.c_str(), tokenId); +int32_t DataShareServiceImpl::DelTemplate(const std::string &uri, const int64_t subscriberId) +{ + TemplateId tpltId; + tpltId.subscriberId_ = subscriberId; + if (!GetCallerBundleName(tpltId.bundleName_)) { + ZLOGE("get bundleName error, %{public}s", DistributedData::Anonymous::Change(uri).c_str()); return ERROR; } + return TemplateManager::GetInstance().DelTemplate(uri, tpltId); +} - uriInfo.SetTableName(GetRealityTableName(tokenId, bundleInfo, uriInfo)); - auto userId = GetUserId(tokenId, bundleInfo.singleton); - int32_t ret = RdbAdaptor::Delete(uriInfo, predicate, userId); - if (ret == ERROR) { - ZLOGE("Delete error, uri is %{public}s", DistributedData::Anonymous::Change(uri).c_str()); - return ERROR; +bool DataShareServiceImpl::GetCallerBundleName(std::string &bundleName) +{ + auto tokenId = IPCSkeleton::GetCallingTokenID(); + if (Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId) != Security::AccessToken::TOKEN_HAP) { + return false; } - NotifyChange(uri); - return ret; + Security::AccessToken::HapTokenInfo tokenInfo; + auto result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); + if (result != Security::AccessToken::RET_SUCCESS) { + ZLOGE("token:0x%{public}x, result:%{public}d", tokenId, result); + return false; + } + bundleName = tokenInfo.bundleName; + return true; } -std::shared_ptr DataShareServiceImpl::Query(const std::string &uri, - const DataSharePredicates &predicates, const std::vector &columns, int &errCode) +std::vector DataShareServiceImpl::Publish(const Data &data, const std::string &bundleNameOfProvider) { - ZLOGD("Query enter."); - UriInfo uriInfo; - if (!URIUtils::GetInfoFromURI(uri, uriInfo)) { - ZLOGE("GetInfoFromURI failed!"); - return nullptr; + std::vector results; + std::vector publishedData; + std::string callerBundleName; + GetCallerBundleName(callerBundleName); + for (const auto &item : data.datas_) { + ZLOGE("hanlu start publish %{public}s %{public}s", item.key_.c_str(), bundleNameOfProvider.c_str()); + auto context = std::make_shared(item.key_); + context->version = data.version_; + context->callerBundleName = callerBundleName; + context->calledBundleName = bundleNameOfProvider; + int32_t result = PublishStrategy::Execute(context, item); + results.emplace_back(item.key_, result); + if (result != EOK) { + ZLOGE("publish error, key is %{public}s", DistributedData::Anonymous::Change(item.key_).c_str()); + continue; + } + publishedData.emplace_back(context->uri, callerBundleName, item.subscriberId_); } + PublishedDataSubscriberManager::GetInstance().Emit(publishedData, callerBundleName); + return results; +} - uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); - AppExecFwk::BundleInfo bundleInfo; - if (!PermissionProxy::GetBundleInfo(uriInfo.bundleName, tokenId, bundleInfo)) { - ZLOGE("GetBundleInfo failed, BundleName is %{public}s, tokenId is %{public}x", - uriInfo.bundleName.c_str(), tokenId); - return nullptr; +Data DataShareServiceImpl::GetData(const std::string &bundleNameOfProvider) +{ + auto context = std::make_shared(); + context->calledBundleName = bundleNameOfProvider; + return GetDataStrategy::Execute(context); +} + +std::vector DataShareServiceImpl::SubscribeRdbData( + const std::vector &uris, const TemplateId &id, const sptr observer) +{ + std::vector results; + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + results.emplace_back(uri, SubscribeStrategy::Execute(context, [&id, &observer, &context]() -> bool { + return RdbSubscriberManager::GetInstance().AddRdbSubscriber(context->uri, id, observer, context); + })); } - - auto permissionState = VerifyPermission(tokenId, PermissionType::READ_PERMISSION, bundleInfo); - if (permissionState == PermissionProxy::PermissionState::DENIED) { - ZLOGE("Verify permission failed, bundleName is %{public}s, tokenId is %{public}x", - uriInfo.bundleName.c_str(), tokenId); - return nullptr; + return results; +} + +std::vector DataShareServiceImpl::UnSubscribeRdbData( + const std::vector &uris, const TemplateId &id) +{ + std::vector results; + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + results.emplace_back(uri, SubscribeStrategy::Execute(context, [&id, &context]() -> bool { + return RdbSubscriberManager::GetInstance().DelRdbSubscriber(context->uri, id, context->callerTokenId); + })); } - - uriInfo.SetTableName(GetRealityTableName(tokenId, bundleInfo, uriInfo)); - auto userId = GetUserId(tokenId, bundleInfo.singleton); - return RdbAdaptor::Query(uriInfo, predicates, columns, userId, errCode); + return results; } -std::string DataShareServiceImpl::GetRealityTableName(uint32_t tokenId, const AppExecFwk::BundleInfo &bundleInfo, - const UriInfo &uriInfo) +std::vector DataShareServiceImpl::EnableSubscribeRdbData( + const std::vector &uris, const TemplateId &id) { - auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); - bool isSingleApp; - ProfileInfo profileInfo; - if (!dataShareProfileInfo_.GetProfileInfoFromExtension(bundleInfo, profileInfo, isSingleApp)) { - ZLOGE("failed, BundleName is %{public}s, tokenId is %{public}x", - uriInfo.bundleName.c_str(), tokenId); - return uriInfo.tableName; + std::vector results; + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + results.emplace_back(uri, SubscribeStrategy::Execute(context, [&id, &context]() -> bool { + return RdbSubscriberManager::GetInstance().EnableRdbSubscriber(context->uri, id, context); + })); } - return PermissionProxy::GetTableNameByCrossUserMode(profileInfo, userId, isSingleApp, uriInfo); + return results; } +std::vector DataShareServiceImpl::DisableSubscribeRdbData( + const std::vector &uris, const TemplateId &id) +{ + std::vector results; + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + results.emplace_back(uri, SubscribeStrategy::Execute(context, [&id, &context]() -> bool { + return RdbSubscriberManager::GetInstance().DisableRdbSubscriber(context->uri, id, context->callerTokenId); + })); + } + return results; +} -PermissionProxy::PermissionState DataShareServiceImpl::VerifyPermission(uint32_t tokenID, - DataShareServiceImpl::PermissionType permissionType, const AppExecFwk::BundleInfo &bundleInfo) +std::vector DataShareServiceImpl::SubscribePublishedData(const std::vector &uris, + const int64_t subscriberId, const sptr observer) { - switch (permissionType) { - case PermissionType::READ_PERMISSION: { - return PermissionProxy::QueryReadPermission(tokenID, bundleInfo); + std::vector results; + std::string callerBundleName; + GetCallerBundleName(callerBundleName); + std::vector publishedKeys; + int32_t result; + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + PublishedDataKey key(uri, callerBundleName, subscriberId); + context->callerBundleName = callerBundleName; + context->calledBundleName = key.bundleName_; + result = SubscribeStrategy::Execute( + context, [&subscriberId, &observer, &callerBundleName, &context]() -> bool { + return PublishedDataSubscriberManager::GetInstance().AddSubscriber( + context->uri, callerBundleName, subscriberId, observer, context->callerTokenId); + }); + results.emplace_back(uri, result); + if (result == E_OK) { + publishedKeys.emplace_back(key); } - case PermissionType::WRITE_PERMISSION: { - return PermissionProxy::QueryWritePermission(tokenID, bundleInfo); + } + PublishedDataSubscriberManager::GetInstance().Emit(publishedKeys, callerBundleName, observer); + return results; +} + +std::vector DataShareServiceImpl::UnSubscribePublishedData(const std::vector &uris, + const int64_t subscriberId) +{ + std::vector results; + std::string callerBundleName; + GetCallerBundleName(callerBundleName); + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + PublishedDataKey key(uri, callerBundleName, subscriberId); + context->callerBundleName = callerBundleName; + context->calledBundleName = key.bundleName_; + results.emplace_back( + uri, SubscribeStrategy::Execute(context, [&subscriberId, &callerBundleName, &context]() -> bool { + return PublishedDataSubscriberManager::GetInstance().DelSubscriber( + context->uri, callerBundleName, subscriberId, context->callerTokenId); + })); + } + return results; +} + +std::vector DataShareServiceImpl::EnableSubscribePublishedData(const std::vector &uris, + const int64_t subscriberId) +{ + std::vector results; + std::string callerBundleName; + GetCallerBundleName(callerBundleName); + std::vector publishedKeys; + int32_t result; + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + PublishedDataKey key(uri, callerBundleName, subscriberId); + context->callerBundleName = callerBundleName; + context->calledBundleName = key.bundleName_; + result = SubscribeStrategy::Execute(context, [&subscriberId, &callerBundleName, &context]() -> bool { + return PublishedDataSubscriberManager::GetInstance().EnableSubscriber( + context->uri, callerBundleName, subscriberId, context->callerTokenId); + }); + results.emplace_back(uri, result); + if (result == E_OK) { + publishedKeys.emplace_back(key); } } - return PermissionProxy::PermissionState::NOT_FIND; + PublishedDataSubscriberManager::GetInstance().Emit(publishedKeys, callerBundleName); + return results; } -int32_t DataShareServiceImpl::GetUserId(uint32_t tokenId, bool isSingleApp) +std::vector DataShareServiceImpl::DisableSubscribePublishedData(const std::vector &uris, + const int64_t subscriberId) { - if (isSingleApp) { - return 0; + std::vector results; + std::string callerBundleName; + GetCallerBundleName(callerBundleName); + for (const auto &uri : uris) { + auto context = std::make_shared(uri); + PublishedDataKey key(uri, callerBundleName, subscriberId); + context->callerBundleName = callerBundleName; + context->calledBundleName = key.bundleName_; + results.emplace_back( + uri, SubscribeStrategy::Execute(context, [&subscriberId, &callerBundleName, &context]() -> bool { + return PublishedDataSubscriberManager::GetInstance().DisableSubscriber( + context->uri, callerBundleName, subscriberId, context->callerTokenId); + })); } - return DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); + return results; +} + +enum DataShareKvStoreType : int32_t { + DATA_SHARE_SINGLE_VERSION = 30, + DISTRIBUTED_TYPE_BUTT +}; + +int32_t DataShareServiceImpl::OnInitialize() +{ + auto token = IPCSkeleton::GetCallingTokenID(); + const std::string accountId = DistributedKv::AccountDelegate::GetInstance()->GetCurrentAccountId(); + const auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(token); + DistributedData::StoreMetaData saveMeta; + saveMeta.appType = "default"; + saveMeta.storeId = "data_share_data_"; + saveMeta.isAutoSync = false; + saveMeta.isBackup = false; + saveMeta.isEncrypt = false; + saveMeta.bundleName = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); + saveMeta.appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); + saveMeta.user = std::to_string(userId); + saveMeta.account = accountId; + saveMeta.tokenId = token; + saveMeta.securityLevel = DistributedKv::SecurityLevel::S1; + saveMeta.area = 1; + saveMeta.uid = IPCSkeleton::GetCallingUid(); + saveMeta.storeType = DATA_SHARE_SINGLE_VERSION; + saveMeta.dataDir = DistributedData::DirectoryManager::GetInstance().GetStorePath(saveMeta); + ZLOGE("hanlu1 dir %{public}s %{public}d", saveMeta.dataDir.c_str(), token); + KvDBDelegate::GetInstance(false, saveMeta.dataDir); + return EOK; +} + +int32_t DataShareServiceImpl::OnUserChange(uint32_t code, const std::string &user, const std::string &account) +{ + auto token = IPCSkeleton::GetCallingTokenID(); + const std::string accountId = DistributedKv::AccountDelegate::GetInstance()->GetCurrentAccountId(); + DistributedData::StoreMetaData saveMeta; + saveMeta.appType = "default"; + saveMeta.storeId = "data_share_data_"; + saveMeta.isAutoSync = false; + saveMeta.isBackup = false; + saveMeta.isEncrypt = false; + saveMeta.bundleName = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); + saveMeta.appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); + saveMeta.user = user; + saveMeta.account = account; + saveMeta.tokenId = token; + saveMeta.securityLevel = DistributedKv::SecurityLevel::S1; + saveMeta.area = 1; + saveMeta.uid = IPCSkeleton::GetCallingUid(); + saveMeta.storeType = DATA_SHARE_SINGLE_VERSION; + saveMeta.dataDir = DistributedData::DirectoryManager::GetInstance().GetStorePath(saveMeta); + KvDBDelegate::GetInstance(true, saveMeta.dataDir); + return EOK; } } // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.h b/services/distributeddataservice/service/data_share/data_share_service_impl.h index 7b9d9d35..a6929282 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -18,12 +18,11 @@ #include -#include "bundle_info.h" #include "data_share_service_stub.h" -#include "data_share_profile_info.h" -#include "permission_proxy.h" -#include "visibility.h" +#include "datashare_template.h" +#include "data_proxy_observer.h" #include "uri_utils.h" +#include "visibility.h" namespace OHOS::DataShare { class API_EXPORT DataShareServiceImpl : public DataShareServiceStub { @@ -35,6 +34,29 @@ public: int32_t Delete(const std::string &uri, const DataSharePredicates &predicate) override; std::shared_ptr Query(const std::string &uri, const DataSharePredicates &predicates, const std::vector &columns, int &errCode) override; + int32_t AddTemplate(const std::string &uri, const int64_t subscriberId, const Template &tplt) override; + int32_t DelTemplate(const std::string &uri, const int64_t subscriberId) override; + std::vector Publish(const Data &data, const std::string &bundleNameOfProvider) override; + Data GetData(const std::string &bundleNameOfProvider) override; + std::vector SubscribeRdbData( + const std::vector &uris, const TemplateId &id, const sptr observer) override; + std::vector UnSubscribeRdbData( + const std::vector &uris, const TemplateId &id) override; + std::vector EnableSubscribeRdbData( + const std::vector &uris, const TemplateId &id) override; + std::vector DisableSubscribeRdbData( + const std::vector &uris, const TemplateId &id) override; + std::vector SubscribePublishedData(const std::vector &uris, + const int64_t subscriberId, const sptr observer) override; + std::vector UnSubscribePublishedData(const std::vector &uris, + const int64_t subscriberId) override; + std::vector EnableSubscribePublishedData(const std::vector &uris, + const int64_t subscriberId) override; + std::vector DisableSubscribePublishedData(const std::vector &uris, + const int64_t subscriberId) override; + int32_t OnInitialize() override; + int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; + private: class Factory { public: @@ -48,14 +70,9 @@ private: }; bool NotifyChange(const std::string &uri); - PermissionProxy::PermissionState VerifyPermission(uint32_t tokenID, PermissionType permissionType, - const AppExecFwk::BundleInfo &bundleInfo); - int32_t GetUserId(uint32_t tokenID, bool isSingleApp); - std::string GetRealityTableName(uint32_t tokenId, const AppExecFwk::BundleInfo &bundleInfo, - const UriInfo &uriInfo); + bool GetCallerBundleName(std::string &bundleName); static Factory factory_; static constexpr int32_t ERROR = -1; - DataShareProfileInfo dataShareProfileInfo_; }; } // namespace OHOS::DataShare #endif diff --git a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp index a388f8cc..dbd30235 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -17,7 +17,8 @@ #include "data_share_service_stub.h" -#include +#include "data_share_obs_proxy.h" +#include "ipc_skeleton.h" #include "ishared_result_set.h" #include "itypes_util.h" #include "log_print.h" @@ -106,6 +107,212 @@ int32_t DataShareServiceStub::OnRemoteQuery(MessageParcel &data, MessageParcel & return 0; } +int32_t DataShareServiceStub::OnRemoteAddTemplate(MessageParcel &data, MessageParcel &reply) +{ + std::string uri; + int64_t subscriberId; + std::vector predicates; + std::string scheduler; + if (!ITypesUtil::Unmarshal(data, uri, subscriberId, predicates, scheduler)) { + ZLOGW("read device list failed."); + return -1; + } + Template tpl(predicates, scheduler); + int32_t status = AddTemplate(uri, subscriberId, tpl); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return 0; +} + +int32_t DataShareServiceStub::OnRemoteDelTemplate(MessageParcel &data, MessageParcel &reply) +{ + std::string uri; + int64_t subscriberId; + if (!ITypesUtil::Unmarshal(data, uri, subscriberId)) { + ZLOGW("read device list failed."); + return -1; + } + int32_t status = DelTemplate(uri, subscriberId); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return 0; +} + +int32_t DataShareServiceStub::OnRemotePublish(MessageParcel &data, MessageParcel &reply) +{ + ZLOGE("hanlu enter"); + Data publishData; + std::string bundleName; + if (!ITypesUtil::Unmarshal(data, publishData.datas_, publishData.version_, bundleName)) { + ZLOGW("read device list failed."); + return -1; + } + std::vector results = Publish(publishData, bundleName); + if (!ITypesUtil::Marshal(reply, results)) { + ZLOGE("ITypesUtil::Marshal(reply, results) failed"); + return -1; + } + return 0; +} + +int32_t DataShareServiceStub::OnRemoteGetData(MessageParcel &data, MessageParcel &reply) +{ + std::string bundleName; + if (!ITypesUtil::Unmarshal(data, bundleName)) { + ZLOGW("read device list failed."); + return -1; + } + auto results = GetData(bundleName); + if (!ITypesUtil::Marshal(reply, results.datas_)) { + ZLOGE("ITypesUtil::Marshal(reply, results) failed"); + return -1; + } + return 0; +} + +int32_t DataShareServiceStub::OnRemoteSubscribeRdbData(MessageParcel &data, MessageParcel &reply) +{ + std::vector uris; + TemplateId templateId; + if (!ITypesUtil::Unmarshal(data, uris, templateId)) { + ZLOGE("read device list failed."); + return -1; + } + auto remoteObj = data.ReadRemoteObject(); + auto observer = iface_cast(remoteObj); + if (observer == nullptr) { + ZLOGE("obServer is nullptr"); + return -1; + } + std::vector results = SubscribeRdbData(uris, templateId, observer); + if (!ITypesUtil::Marshal(reply, results)) { + ZLOGE("ITypesUtil::Marshal(reply, results) failed"); + return -1; + } + return 0; +} + +int32_t DataShareServiceStub::OnRemoteUnSubscribeRdbData(MessageParcel &data, MessageParcel &reply) +{ + std::vector uris; + TemplateId templateId; + if (!ITypesUtil::Unmarshal(data, uris, templateId)) { + ZLOGE("read device list failed."); + return -1; + } + std::vector results = UnSubscribeRdbData(uris, templateId); + if (!ITypesUtil::Marshal(reply, results)) { + ZLOGE("ITypesUtil::Marshal(reply, results) failed"); + return -1; + } + return 0; +} + +int32_t DataShareServiceStub::OnRemoteEnableSubscribeRdbData(MessageParcel &data, MessageParcel &reply) +{ + std::vector uris; + TemplateId templateId; + if (!ITypesUtil::Unmarshal(data, uris, templateId)) { + ZLOGE("read device list failed."); + return -1; + } + std::vector results = EnableSubscribeRdbData(uris, templateId); + if (!ITypesUtil::Marshal(reply, results)) { + ZLOGE("ITypesUtil::Marshal(reply, results) failed"); + return -1; + } + return 0; +} + +int32_t DataShareServiceStub::OnRemoteDisableSubscribeRdbData(MessageParcel &data, MessageParcel &reply) +{ + std::vector uris; + TemplateId templateId; + if (!ITypesUtil::Unmarshal(data, uris, templateId)) { + ZLOGE("read device list failed."); + return -1; + } + std::vector results = DisableSubscribeRdbData(uris, templateId); + if (!ITypesUtil::Marshal(reply, results)) { + ZLOGE("ITypesUtil::Marshal(reply, results) failed"); + return -1; + } + return 0; +} + +int32_t DataShareServiceStub::OnRemoteSubscribePublishedData(MessageParcel &data, MessageParcel &reply) +{ + std::vector uris; + int64_t subscriberId; + if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) { + ZLOGE("read device list failed."); + return -1; + } + auto observer = iface_cast(data.ReadRemoteObject()); + if (observer == nullptr) { + ZLOGE("obServer is nullptr"); + return -1; + } + std::vector results = SubscribePublishedData(uris, subscriberId, observer); + if (!ITypesUtil::Marshal(reply, results)) { + ZLOGE("ITypesUtil::Marshal(reply, results) failed"); + return -1; + } + return 0; +} + +int32_t DataShareServiceStub::OnRemoteUnSubscribePublishedData(MessageParcel &data, MessageParcel &reply) +{ + std::vector uris; + int64_t subscriberId; + if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) { + ZLOGE("read device list failed."); + return -1; + } + std::vector results = UnSubscribePublishedData(uris, subscriberId); + if (!ITypesUtil::Marshal(reply, results)) { + ZLOGE("ITypesUtil::Marshal(reply, results) failed"); + return -1; + } + return 0; +} + +int32_t DataShareServiceStub::OnRemoteEnableSubscribePublishedData(MessageParcel &data, MessageParcel &reply) +{ + std::vector uris; + int64_t subscriberId; + if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) { + ZLOGE("read device list failed."); + return -1; + } + std::vector results = EnableSubscribePublishedData(uris, subscriberId); + if (!ITypesUtil::Marshal(reply, results)) { + ZLOGE("ITypesUtil::Marshal(reply, results) failed"); + return -1; + } + return 0; +} + +int32_t DataShareServiceStub::OnRemoteDisableSubscribePublishedData(MessageParcel &data, MessageParcel &reply) +{ + std::vector uris; + int64_t subscriberId; + if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) { + ZLOGE("read device list failed."); + return -1; + } + std::vector results = DisableSubscribePublishedData(uris, subscriberId); + if (!ITypesUtil::Marshal(reply, results)) { + ZLOGE("ITypesUtil::Marshal(reply, results) failed"); + return -1; + } + return 0; +} + int DataShareServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) { ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid()); diff --git a/services/distributeddataservice/service/data_share/data_share_service_stub.h b/services/distributeddataservice/service/data_share/data_share_service_stub.h index 043a6874..2b3e89c7 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_stub.h +++ b/services/distributeddataservice/service/data_share/data_share_service_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -31,6 +31,19 @@ private: int32_t OnRemoteUpdate(MessageParcel& data, MessageParcel& reply); int32_t OnRemoteDelete(MessageParcel& data, MessageParcel& reply); int32_t OnRemoteQuery(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteAddTemplate(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteDelTemplate(MessageParcel& data, MessageParcel& reply); + int32_t OnRemotePublish(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteGetData(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteSubscribeRdbData(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteUnSubscribeRdbData(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteEnableSubscribeRdbData(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteDisableSubscribeRdbData(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteSubscribePublishedData(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteUnSubscribePublishedData(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteEnableSubscribePublishedData(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteDisableSubscribePublishedData(MessageParcel& data, MessageParcel& reply); + using RequestHandle = int (DataShareServiceStub::*)(MessageParcel &, MessageParcel &); static constexpr RequestHandle HANDLERS[DATA_SHARE_SERVICE_CMD_MAX] = { @@ -38,6 +51,18 @@ private: &DataShareServiceStub::OnRemoteDelete, &DataShareServiceStub::OnRemoteUpdate, &DataShareServiceStub::OnRemoteQuery, + &DataShareServiceStub::OnRemoteAddTemplate, + &DataShareServiceStub::OnRemoteDelTemplate, + &DataShareServiceStub::OnRemotePublish, + &DataShareServiceStub::OnRemoteGetData, + &DataShareServiceStub::OnRemoteSubscribeRdbData, + &DataShareServiceStub::OnRemoteUnSubscribeRdbData, + &DataShareServiceStub::OnRemoteEnableSubscribeRdbData, + &DataShareServiceStub::OnRemoteDisableSubscribeRdbData, + &DataShareServiceStub::OnRemoteSubscribePublishedData, + &DataShareServiceStub::OnRemoteUnSubscribePublishedData, + &DataShareServiceStub::OnRemoteEnableSubscribePublishedData, + &DataShareServiceStub::OnRemoteDisableSubscribePublishedData }; }; } // namespace DataShare diff --git a/services/distributeddataservice/service/data_share/data_share_types_util.cpp b/services/distributeddataservice/service/data_share/data_share_types_util.cpp index 11d3bd3a..178380df 100644 --- a/services/distributeddataservice/service/data_share/data_share_types_util.cpp +++ b/services/distributeddataservice/service/data_share/data_share_types_util.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -45,4 +45,123 @@ bool Unmarshalling(Operation &operation, MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, operation.operation, operation.singleParams, operation.multiParams); } + +template<> +bool Unmarshalling(PublishedDataItem &dataItem, MessageParcel &parcel) +{ + dataItem.key_ = parcel.ReadString(); + dataItem.subscriberId_ = parcel.ReadInt64(); + auto index = parcel.ReadUint32(); + ZLOGE("hanlu Unmarshalling %{public}s", dataItem.key_.c_str()); + if (index == 0) { + sptr ashmem = parcel.ReadAshmem(); + dataItem.value_ = ashmem; + ZLOGE("hanlu ReadAshmem %{public}p", ashmem.GetRefPtr()); + bool ret = ashmem->MapReadOnlyAshmem(); + if (!ret) { + ZLOGE("MapReadAndWriteAshmem fail, %{private}s", dataItem.key_.c_str()); + return false; + } + ZLOGE("hanlu MapReadAndWriteAshmem %{public}p", ashmem.GetRefPtr()); + } else { + dataItem.value_ = parcel.ReadString(); + } + return true; +} + +template<> +bool Marshalling(const PublishedDataItem &dataItem, MessageParcel &parcel) +{ + if (!parcel.WriteString(dataItem.key_)) { + return false; + } + if (!parcel.WriteInt64(dataItem.subscriberId_)) { + return false; + } + auto index = static_cast(dataItem.value_.index()); + if (!parcel.WriteUint32(index)) { + return false; + } + if (index == 0) { + sptr ashmem = std::get>(dataItem.value_); + if (ashmem == nullptr) { + ZLOGE("ashmem null"); + return false; + } + return parcel.WriteAshmem(ashmem); + } + return parcel.WriteString(std::get(dataItem.value_)); +} + +template<> +bool Unmarshalling(Data &data, MessageParcel &parcel) +{ + int32_t len = parcel.ReadInt32(); + if (len < 0) { + return false; + } + size_t size = static_cast(len); + size_t readAbleSize = parcel.GetReadableBytes(); + if ((size > readAbleSize) || (size > data.datas_.max_size())) { + return false; + } + std::vector dataItems; + for (size_t i = 0; i < size; i++) { + PublishedDataItem value; + if (!Unmarshalling(value, parcel)) { + return false; + } + dataItems.emplace_back(std::move(value)); + } + data.datas_ = dataItems; + data.version_ = parcel.ReadInt32(); + return true; +} + +template<> +bool Unmarshalling(TemplateId &templateId, MessageParcel &parcel) +{ + return ITypesUtil::Unmarshal(parcel, templateId.subscriberId_, templateId.bundleName_); +} + +template<> +bool Marshalling(const TemplateId &templateId, MessageParcel &parcel) +{ + return ITypesUtil::Marshal(parcel, templateId.subscriberId_, templateId.bundleName_); +} + +template<> +bool Unmarshalling(PredicateTemplateNode &predicateTemplateNode, MessageParcel &parcel) +{ + return ITypesUtil::Unmarshal(parcel, predicateTemplateNode.key_, predicateTemplateNode.selectSql_); +} + +template<> +bool Marshalling(const RdbChangeNode &changeNode, MessageParcel &parcel) +{ + return ITypesUtil::Marshal(parcel, changeNode.uri_, changeNode.templateId_, changeNode.data_); +} + +template<> +bool Marshalling(const PublishedDataChangeNode &changeNode, MessageParcel &parcel) +{ + if (!parcel.WriteString(changeNode.ownerBundleName_)) { + return false; + } + if (!parcel.WriteInt32(changeNode.datas_.size())) { + return false; + } + for (const auto &dataItem : changeNode.datas_) { + if (!Marshalling(dataItem, parcel)) { + return false; + } + } + return true; +} + +template<> +bool Marshalling(const OperationResult &operationResult, MessageParcel &parcel) +{ + return ITypesUtil::Marshal(parcel, operationResult.key_, operationResult.errCode_); +} } \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data_share_types_util.h b/services/distributeddataservice/service/data_share/data_share_types_util.h index 917c9611..4f4ebf9b 100644 --- a/services/distributeddataservice/service/data_share/data_share_types_util.h +++ b/services/distributeddataservice/service/data_share/data_share_types_util.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -15,17 +15,55 @@ #ifndef DATASHARESERVICE_DATA_SHARE_TYPES_UTIL_H #define DATASHARESERVICE_DATA_SHARE_TYPES_UTIL_H + #include "datashare_predicates.h" +#include "datashare_template.h" #include "datashare_values_bucket.h" +#include "data_proxy_observer.h" #include "itypes_util.h" + namespace OHOS::ITypesUtil { using Predicates = DataShare::DataSharePredicates; using Operation = DataShare::OperationItem; +using PublishedDataItem = DataShare::PublishedDataItem; +using Data = DataShare::Data; +using TemplateId = DataShare::TemplateId; +using PredicateTemplateNode = DataShare::PredicateTemplateNode; +using RdbChangeNode = DataShare::RdbChangeNode; +using PublishedDataChangeNode = DataShare::PublishedDataChangeNode; +using OperationResult = DataShare::OperationResult; template<> bool Unmarshalling(Predicates &predicates, MessageParcel &parcel); template<> bool Unmarshalling(Operation &operation, MessageParcel &parcel); + +template<> +bool Unmarshalling(PublishedDataItem &dataItem, MessageParcel &parcel); + +template<> +bool Marshalling(const PublishedDataItem &templateId, MessageParcel &parcel); + +template<> +bool Unmarshalling(Data &data, MessageParcel &parcel); + +template<> +bool Unmarshalling(TemplateId &templateId, MessageParcel &parcel); + +template<> +bool Unmarshalling(PredicateTemplateNode &predicateTemplateNode, MessageParcel &parcel); + +template<> +bool Marshalling(const RdbChangeNode &changeNode, MessageParcel &parcel); + +template<> +bool Marshalling(const PublishedDataChangeNode &changeNode, MessageParcel &parcel); + +template<> +bool Marshalling(const OperationResult &operationResult, MessageParcel &parcel); + +template<> +bool Marshalling(const TemplateId &changeNode, MessageParcel &parcel); }; #endif // DATASHARESERVICE_DATA_SHARE_TYPES_UTIL_H diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/.vscode/settings.json b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/.vscode/settings.json new file mode 100644 index 00000000..626b352b --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/.vscode/settings.json @@ -0,0 +1,60 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "map": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "ranges": "cpp", + "semaphore": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/BUILD.gn b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/BUILD.gn new file mode 100644 index 00000000..53e16705 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/BUILD.gn @@ -0,0 +1,91 @@ +# Copyright (c) 2021 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. +import("//build/ohos.gni") + +config("documentdb_config") { + visibility = [ ":*" ] + include_dirs = [ + "src/common/include", + "src/executor/include", + "src/executor/document", + "src/oh_adapter/include", + "src/oh_adapter/src", + "src/interface/include", + ] + + defines = [ + "SQLITE_ENABLE_SNAPSHOT", + "SQLITE_HAS_CODEC", + "SQLITE_ENABLE_JSON1", + "USING_HILOG_LOGGER", + "USE_SQLITE_SYMBOLS", + "SQLITE_ENABLE_DROPTABLE_CALLBACK", + ] + if (is_debug) { + defines += [ "TRACE_SQLITE_EXECUTE" ] + } +} + +config("documentdb_public_config") { + visibility = [ "*:*" ] + include_dirs = [ "include" ] +} + +group("build_module") { + deps = [ ":documentdb" ] +} + +ohos_shared_library("documentdb") { + sources = [ + "src/common/src/collection_option.cpp", + "src/common/src/db_config.cpp", + "src/common/src/json_common.cpp", + "src/common/src/log_print.cpp", + "src/common/src/os_api.cpp", + "src/executor/base/grd_db_api.cpp", + "src/executor/document/document_check.cpp", + "src/executor/document/grd_document_api.cpp", + "src/executor/document/grd_resultset_api.cpp", + "src/interface/src/collection.cpp", + "src/interface/src/doc_errno.cpp", + "src/interface/src/document_store.cpp", + "src/interface/src/document_store_manager.cpp", + "src/interface/src/projection_tree.cpp", + "src/interface/src/result_set.cpp", + "src/interface/src/result_set_common.cpp", + "src/oh_adapter/src/json_object.cpp", + "src/oh_adapter/src/kv_store_manager.cpp", + "src/oh_adapter/src/sqlite_store_executor_impl.cpp", + "src/oh_adapter/src/sqlite_utils.cpp", + ] + + configs = [ ":documentdb_config" ] + public_configs = [ ":documentdb_public_config" ] + + deps = [ "//third_party/sqlite:sqlite" ] + + configs += [ "//third_party/cJSON:cJSON_config" ] + ldflags = [ "-Wl,--exclude-libs,ALL" ] + cflags_cc = [ "-fvisibility=hidden" ] + deps += [ "//third_party/cJSON:cjson" ] + + external_deps = [ + "c_utils:utils", + "hisysevent_native:libhisysevent", + "hitrace_native:hitrace_meter", + "hiviewdfx_hilog_native:libhilog", + ] + + subsystem_name = "distributeddatamgr" + part_name = "datamgr_service" +} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/CMakeLists.txt b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/CMakeLists.txt new file mode 100644 index 00000000..75734853 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 3.10.2) +project(document_ut) + +set(GAUSE_SOURCE_DIR "${PROJECT_SOURCE_DIR}") + +file(GLOB_RECURSE DOCUMENT_SRC ${GAUSE_SOURCE_DIR}/src/*.cpp) +message(STATUS "hanlu ${GAUSE_SOURCE_DIR} ${DOCUMENT_SRC}") +include_directories( + ${GAUSE_SOURCE_DIR}/include + ${GAUSE_SOURCE_DIR}/src/storage + ${GAUSE_SOURCE_DIR}/src/storage/include + ${GAUSE_SOURCE_DIR}/src/storage/src + ${GAUSE_SOURCE_DIR}/src/common + ${GAUSE_SOURCE_DIR}/src/common/include + ${GAUSE_SOURCE_DIR}/src/executor/include + ${GAUSE_SOURCE_DIR}/src/executor/base + ${GAUSE_SOURCE_DIR}/src/executor/document + ${GAUSE_SOURCE_DIR}/src/oh_adapter + ${GAUSE_SOURCE_DIR}/src/oh_adapter/include + ${GAUSE_SOURCE_DIR}/src/oh_adapter/src + ${GAUSE_SOURCE_DIR}/src/interface + ${GAUSE_SOURCE_DIR}/src/interface/include + ${GAUSE_SOURCE_DIR}/src/interface/src + ${GAUSE_SOURCE_DIR}/third_party/third_party_cJSON +) +aux_source_directory( ${GAUSE_SOURCE_DIR}/third_party/third_party_cJSON DOCUMENT_SRC) +set(MOCK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../mock") +set(KV_STORE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../kv_store") +include(${MOCK_DIR}/include/CMakeLists.txt OPTIONAL) +include(${KV_STORE_DIR}/interfaces/CMakeLists.txt OPTIONAL) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../utils_native/base/include) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../utils_native/safwk/native/include) +message(STATUS "hanlu3 ${CMAKE_CURRENT_SOURCE_DIR}../../../../../../utils_native/base/include") +add_library(document_ut SHARED ${DOCUMENT_SRC}) +set(links mock kvdb adapter data_share secure) +target_link_libraries(document_ut ${links}) + + diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/README.md b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/README.md new file mode 100644 index 00000000..1d777fec --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/README.md @@ -0,0 +1 @@ +# Document DB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_db_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_db_api.h new file mode 100644 index 00000000..323c487d --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_db_api.h @@ -0,0 +1,34 @@ +/* +* 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 GRD_DB_API_H +#define GRD_DB_API_H + +#include "grd_type_export.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +DOC_API int GRD_DBOpen(const char *dbPath, const char *configStr, unsigned int flags, GRD_DB **db); + +DOC_API int GRD_DBClose(GRD_DB *db, unsigned int flags); + +DOC_API int GRD_Flush(GRD_DB *db, unsigned int flags); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // GRD_DB_API_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_error.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_error.h new file mode 100644 index 00000000..ecc7c705 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_error.h @@ -0,0 +1,86 @@ +/* +* 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 GRD_ERROR_H +#define GRD_ERROR_H + +#include "grd_type_export.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// Error category +#define GRD_OK 0 + +// Error category +#define GRD_NOT_SUPPORT (-1000) +#define GRD_OVER_LIMIT (-2000) +#define GRD_INVALID_ARGS (-3000) +#define GRD_SYSTEM_ERR (-4000) +#define GRD_FAILED_FILE_OPERATION (-5000) +#define GRD_INVALID_FILE_FORMAT (-6000) +#define GRD_INNER_ERR (-8000) +#define GRD_RESOURCE_BUSY (-9000) + +#define GRD_NO_DATA (-11000) +#define GRD_FAILED_MEMORY_ALLOCATE (-13000) +#define GRD_FAILED_MEMORY_RELEASE (-14000) +#define GRD_DATA_CONFLICT (-16000) +#define GRD_NOT_AVAILABLE (-19000) +#define GRD_INVALID_FORMAT (-37000) +#define GRD_REBUILD_DATABASE (-38000) + +// not support +#define GRD_JSON_OPERATION_NOT_SUPPORT (-5001001) + +// Exceed limit +#define GRD_JSON_LEN_LIMIT (-5002001) + +// Invalid parameter +#define GRD_FIELD_TYPE_NOT_MATCH (-5003001) +#define GRD_LARGE_JSON_NEST (-5003002) +#define GRD_INVALID_JSON_TYPE (-5003003) +#define GRD_INVALID_CONFIG_VALUE (-5003004) +#define GRD_INVALID_OPERATOR (-5003005) +#define GRD_INVALID_PROJECTION_FIELD (-5003006) +#define GRD_INVALID_PROJECTION_VALUE (-5003007) +#define GRD_ARRAY_INDEX_NOT_FOUND (-5003008) + +// System err +#define GRD_JSON_LIB_HANDLE_FAILED (-5004001) + +// no data +#define GRD_COLLECTION_NOT_FOUND (-5011001) +#define GRD_RECORD_NOT_FOUND (-5011002) +#define GRD_DB_NOT_FOUND (-11003) +#define GRD_FIELD_NOT_FOUND (-5011004) + +// data conflicted +#define GRD_COLLECTION_CONFLICT (-5016001) +#define GRD_KEY_CONFLICT (-5016002) +#define GRD_FIELD_TYPE_CONFLICT (-5016003) + +// Cursor or ResultSet not available +#define GRD_RESULT_SET_NOT_AVAILABLE (-5019001) + +// Invalid format +#define GRD_INVALID_JSON_FORMAT (-5037001) +#define GRD_INVALID_KEY_FORMAT (-5037002) +#define GRD_INVALID_COLLECTION_NAME (-5037003) +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // GRD_ERROR_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_resultset_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_resultset_api.h new file mode 100644 index 00000000..16e0b007 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_resultset_api.h @@ -0,0 +1,36 @@ +/* +* 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 GRD_RESULTSET_API_H +#define GRD_RESULTSET_API_H + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef struct GRD_ResultSet GRD_ResultSet; + +DOC_API int GRD_Next(GRD_ResultSet *resultSet); + +DOC_API int GRD_GetValue(GRD_ResultSet *resultSet, char **value); + +DOC_API int GRD_FreeValue(char *value); + +DOC_API int GRD_FreeResultSet(GRD_ResultSet *resultSet); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // GRD_RESULTSET_API_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_type_export.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_type_export.h new file mode 100644 index 00000000..b729d216 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_type_export.h @@ -0,0 +1,67 @@ +/* +* 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 GRD_TYPE_EXPORT_H +#define GRD_TYPE_EXPORT_H + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#ifndef _WIN32 + #define DOC_API __attribute__ ((visibility ("default"))) +#endif + +typedef struct GRD_DB GRD_DB; + +/** + * @brief Open database config + */ +#define GRD_DB_OPEN_ONLY 0x00 +#define GRD_DB_OPEN_CREATE 0x01 +#define GRD_DB_OPEN_CHECK_FOR_ABNORMAL 0x02 //check data in database if close abnormally last time, + //if data is corrupted, rebuild the database +#define GRD_DB_OPEN_CHECK 0x04//check data in database when open database, if data is corrupted, rebuild the database. +/** + * @brief Close database config + */ +#define GRD_DB_CLOSE 0x00 +#define GRD_DB_CLOSE_IGNORE_ERROR 0x01 + +/** + * @brief flush database config + */ +#define GRD_DB_FLUSH_ASYNC 0x00 +#define GRD_DB_FLUSH_SYNC 0x01 + +#define GRD_DOC_ID_DISPLAY 0x01 +typedef struct Query { + const char *filter; + const char *projection; +} Query; + +/** + * @brief Flags for create and drop collection + */ +#define CHK_EXIST_COLLECTION 1 +#define CHK_NON_EXIST_COLLECTION 1 + +#define GRD_DOC_APPEND 0 +#define GRD_DOC_REPLACE 1 + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // GRD_TYPE_EXPORT_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_document/grd_document_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_document/grd_document_api.h new file mode 100644 index 00000000..b4df9c6b --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_document/grd_document_api.h @@ -0,0 +1,46 @@ +/* +* 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 GRD_DOCUMENT_API_H +#define GRD_DOCUMENT_API_H + +#include "grd_base/grd_type_export.h" +#include "grd_base/grd_resultset_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +DOC_API int GRD_CreateCollection(GRD_DB *db, const char *collectionName, const char *optionStr, unsigned int flags); + +DOC_API int GRD_DropCollection(GRD_DB *db, const char *collectionName, unsigned int flags); + +DOC_API int GRD_InsertDoc(GRD_DB *db, const char *collectionName, const char *document, unsigned int flags); + +DOC_API int GRD_FindDoc(GRD_DB *db, const char *collectionName, Query query, unsigned int flags, + GRD_ResultSet **resultSet); + +DOC_API int GRD_UpdateDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *update, + unsigned int flags); + +DOC_API int GRD_UpsertDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *document, + unsigned int flags); + +DOC_API int GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, unsigned int flags); + +#ifdef __cplusplus +} +#endif +#endif // GRD_DOCUMENT_API_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/collection_option.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/collection_option.h new file mode 100644 index 00000000..72759240 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/collection_option.h @@ -0,0 +1,37 @@ +/* +* 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 COLLECTION_OPTION_H +#define COLLECTION_OPTION_H + +#include +#include + +namespace DocumentDB { +class CollectionOption final { +public: + static CollectionOption ReadOption(const std::string &optStr, int &errCode); + + uint32_t GetMaxDoc() const; + std::string ToString() const; + + bool operator==(const CollectionOption &targetOption) const; + bool operator!=(const CollectionOption &targetOption) const; +private: + std::string option_ = "{}"; + uint32_t maxDoc_ = UINT32_MAX; +}; +} // namespace DocumentDB +#endif // COLLECTION_OPTION_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/db_config.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/db_config.h new file mode 100644 index 00000000..c04116f2 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/db_config.h @@ -0,0 +1,46 @@ +/* +* 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 DB_CONFIG_H +#define DB_CONFIG_H + +#include + +namespace DocumentDB { +class DBConfig final { +public: + static DBConfig ReadConfig(const std::string &confStr, int &errCode); + + ~DBConfig() = default; + std::string ToString() const; + + int32_t GetPageSize() const; + + bool operator==(const DBConfig &targetConfig) const; + bool operator!=(const DBConfig &targetConfig) const; + +private: + DBConfig() = default; + + std::string configStr_ = {}; + int32_t pageSize_ = 4; // 4: default page size k + uint32_t redoFlushByTrx_ = 0; + uint32_t redoPubBufSize_ = 1024; // 1024: default 1024k buff size + int32_t maxConnNum_ = 100; // 100: default max conn + uint32_t bufferPoolSize_ = 1024; // 100: default 1024k pool size + uint32_t crcCheckEnable_ = 1; +}; +} // namespace DocumentDB +#endif // DB_CONFIG_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/doc_limit.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/doc_limit.h new file mode 100644 index 00000000..38ceb943 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/doc_limit.h @@ -0,0 +1,22 @@ +/* +* 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 DOC_LIMIT_H +#define DOC_LIMIT_H + +namespace DocumentDB { +constexpr const int MAX_DB_CONFIG_LEN = 512 * 1024; // 512 * 1024: 512k length +} // namespace DocumentDB +#endif // DOC_LIMIT_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h new file mode 100644 index 00000000..4494f01e --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h @@ -0,0 +1,47 @@ +/* +* 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 JSON_COMMON_H +#define JSON_COMMON_H + +#include +#include +#include +#include +#include "json_object.h" + +namespace DocumentDB { +class JsonCommon +{ +public: + JsonCommon() = default; + ~JsonCommon(); + + static ValueObject GetValueByFiled(JsonObject &node, const std::string& filed); + static bool CheckJsonField(JsonObject &node); + static bool CheckProjectionField(JsonObject &node); + static int ParseNode(JsonObject &Node, std::vector singlePath, std::vector> &resultPath, bool isFirstFloor); + static std::vector> ParsePath(const JsonObject &node); + static std::vector GetLeafValue(JsonObject &node); + + static int Append(const JsonObject &src, const JsonObject &add); + +private: + static bool CheckNode(JsonObject &Node, std::set filedSet, bool &errFlag); + static bool CheckProjectionNode(JsonObject &Node, std::set filedSet, bool &errFlag, bool isFirstFloor); + static int CheckLeafNode(JsonObject &Node, std::vector &leafValue); +}; +} // DocumentDB +#endif // JSON_COMMON_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/log_print.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/log_print.h new file mode 100644 index 00000000..55c800a9 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/log_print.h @@ -0,0 +1,44 @@ +/* +* 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 LOG_PRINT_H +#define LOG_PRINT_H + +#include + +namespace DocumentDB { +constexpr const char *LOG_TAG_DOC = "DocumentDB"; + +class Logger { +public: + enum class Level { + LEVEL_DEBUG, + LEVEL_INFO, + LEVEL_WARN, + LEVEL_ERROR, + LEVEL_FATAL + }; + + static void Log(Level level, const std::string &tag, const char *func, int line, const char *format, ...); +}; +} // namespace DocumentDB + +#define NO_LOG(...) // No log in normal and release. Used for convenience when deep debugging +#define GLOGD(...) Logger::Log(Logger::Level::LEVEL_DEBUG, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) +#define GLOGI(...) Logger::Log(Logger::Level::LEVEL_INFO, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) +#define GLOGW(...) Logger::Log(Logger::Level::LEVEL_WARN, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) +#define GLOGE(...) Logger::Log(Logger::Level::LEVEL_ERROR, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) +#define GLOGF(...) Logger::Log(Logger::Level::LEVEL_FATAL, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) +#endif // LOG_PRINT_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/os_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/os_api.h new file mode 100644 index 00000000..6bfbf3bb --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/os_api.h @@ -0,0 +1,30 @@ +/* + * 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. + */ +#include + +#ifndef OS_API_H +#define OS_API_H +namespace DocumentDB { +namespace OSAPI { +bool CheckPermission(const std::string &filePath); + +bool CheckPathExistence(const std::string &filePath); + +int GetRealPath(const std::string &inOriPath, std::string &outRealPath); + +void SplitFilePath(const std::string &filePath, std::string &fileDir, std::string &fileName); +} // namespace OSAPI +} // namespace DocumentDB +#endif // OS_API_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/collection_option.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/collection_option.cpp new file mode 100644 index 00000000..fcb3da1e --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/collection_option.cpp @@ -0,0 +1,118 @@ +/* +* 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. +*/ + +#include "collection_option.h" + +#include +#include + +#include "doc_errno.h" +#include "json_object.h" +#include "log_print.h" + +namespace DocumentDB { +namespace { +const std::string OPT_MAX_DOC = "maxdoc"; +const std::vector DB_CONFIG = { + OPT_MAX_DOC, +}; + +bool CheckConfigSupport(const JsonObject &config, int &errCode) +{ + + JsonObject child = config.GetChild(); + while (!child.IsNull()) { + std::string fieldName = child.GetItemFiled(); + if (std::find(DB_CONFIG.begin(), DB_CONFIG.end(), fieldName) == DB_CONFIG.end()) { + GLOGE("Invalid collection config."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + child = child.GetNext(); + } + return true; +} +} +CollectionOption CollectionOption::ReadOption(const std::string &optStr, int &errCode) +{ + if (optStr.empty()) { + return {}; + } + + std::string lowerCaseOptStr = optStr; + std::transform(lowerCaseOptStr.begin(), lowerCaseOptStr.end(), lowerCaseOptStr.begin(), [](unsigned char c) { + return std::tolower(c); + }); + + JsonObject collOpt = JsonObject::Parse(lowerCaseOptStr, errCode); + if (errCode != E_OK) { + GLOGE("Read collection option failed from str. %d", errCode); + return {}; + } + + if (!CheckConfigSupport(collOpt, errCode)) { + GLOGE("Check collection option, not support config item. %d", errCode); + return {}; + } + + static const JsonFieldPath maxDocField = {OPT_MAX_DOC}; + if (!collOpt.IsFieldExists(maxDocField)) { + return {}; + } + + ValueObject maxDocValue = collOpt.GetObjectByPath(maxDocField, errCode); + if (errCode != E_OK) { + GLOGE("Read collection option failed. %d", errCode); + return {}; + } + + if (maxDocValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { + GLOGE("Check collection option failed, the field type of maxDoc is not NUMBER."); + errCode = -E_INVALID_CONFIG_VALUE; + return {}; + } + + if (maxDocValue.GetIntValue() <= 0 || static_cast(maxDocValue.GetIntValue()) > UINT32_MAX) { + GLOGE("Check collection option failed, invalid maxDoc value."); + errCode = -E_INVALID_CONFIG_VALUE; + return {}; + } + + CollectionOption option; + option.maxDoc_ = static_cast(maxDocValue.GetIntValue()); + option.option_ = optStr; + return option; +} + +uint32_t CollectionOption::GetMaxDoc() const +{ + return maxDoc_; +} + +std::string CollectionOption::ToString() const +{ + return option_; +} + +bool CollectionOption::operator==(const CollectionOption &targetOption) const +{ + return maxDoc_ == targetOption.maxDoc_; +} + +bool CollectionOption::operator!=(const CollectionOption &targetOption) const +{ + return !(*this == targetOption); +} +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/db_config.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/db_config.cpp new file mode 100644 index 00000000..b60de5b1 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/db_config.cpp @@ -0,0 +1,300 @@ +/* +* 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. +*/ + +#include "db_config.h" + +#include +#include + +#include "doc_errno.h" +#include "doc_limit.h" +#include "log_print.h" +#include "json_object.h" + +namespace DocumentDB { +namespace { +const int MIN_REDO_BUFFER_SIZE = 256; +const int MAX_REDO_BUFFER_SIZE = 16384; +const int MIN_CONNECTION_NUM = 16; +const int MAX_CONNECTION_NUM = 1024; +const int MIN_BUFFER_POOL_SIZE = 1024; +const int MAX_BUFFER_POOL_SIZE = 4 * 1024 * 1024; + +const std::string DB_CONFIG_PAGESIZE = "pagesize"; +const std::string DB_CONFIG_REDO_FLUSH_BY_TRX = "redoflushbytrx"; +const std::string DB_CONFIG_REDO_PUB_BUFF_SIZE = "redopubbufsize"; +const std::string DB_CONFIG_MAX_CONN_NUM = "maxconnnum"; +const std::string DB_CONFIG_BUFFER_POOL_SIZE = "bufferpoolsize"; +const std::string DB_CONFIG_CRC_CHECK_ENABLE = "crccheckenable"; + +const std::vector DB_CONFIG = { + DB_CONFIG_PAGESIZE, + DB_CONFIG_REDO_FLUSH_BY_TRX, + DB_CONFIG_REDO_PUB_BUFF_SIZE, + DB_CONFIG_MAX_CONN_NUM, + DB_CONFIG_BUFFER_POOL_SIZE, + DB_CONFIG_CRC_CHECK_ENABLE +}; + +bool CheckPageSizeConfig(const JsonObject &config, int32_t &pageSize, int &errCode) +{ + static const JsonFieldPath pageSizeField = {DB_CONFIG_PAGESIZE}; + if (!config.IsFieldExists(pageSizeField)) { + return true; + } + + ValueObject configValue = config.GetObjectByPath(pageSizeField, errCode); + if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { + GLOGE("Check DB config failed, the field type of pageSize is not NUMBER."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + + static const std::vector pageSizeValid = {4, 8, 16, 32, 64}; + if (std::find(pageSizeValid.begin(), pageSizeValid.end(), configValue.GetIntValue()) == pageSizeValid.end()) { + GLOGE("Check DB config failed, invalid pageSize value."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + + pageSize = static_cast(configValue.GetIntValue()); + return true; +} + +bool CheckRedoFlushConfig(const JsonObject &config, uint32_t &redoFlush, int &errCode) +{ + static const JsonFieldPath redoFlushField = {DB_CONFIG_REDO_FLUSH_BY_TRX}; + if (!config.IsFieldExists(redoFlushField)) { + return true; + } + + ValueObject configValue = config.GetObjectByPath(redoFlushField, errCode); + if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { + GLOGE("Check DB config failed, the field type of redoFlushByTrx is not NUMBER."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + + if (configValue.GetIntValue() != 0 && configValue.GetIntValue() != 1) { + GLOGE("Check DB config failed, invalid redoFlushByTrx value."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + + redoFlush = static_cast(configValue.GetIntValue()); + return true; +} + +bool CheckRedoBufSizeConfig(const JsonObject &config, uint32_t &redoBufSize, int &errCode) +{ + static const JsonFieldPath redoBufSizeField = {DB_CONFIG_REDO_PUB_BUFF_SIZE}; + if (!config.IsFieldExists(redoBufSizeField)) { + return true; + } + + ValueObject configValue = config.GetObjectByPath(redoBufSizeField, errCode); + if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { + GLOGE("Check DB config failed, the field type of redoPubBufSize is not NUMBER."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + + if (configValue.GetIntValue() < MIN_REDO_BUFFER_SIZE || configValue.GetIntValue() > MAX_REDO_BUFFER_SIZE) { + GLOGE("Check DB config failed, invalid redoPubBufSize value."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + + redoBufSize = static_cast(configValue.GetIntValue()); + return true; +} + +bool CheckMaxConnNumConfig(const JsonObject &config, int32_t &maxConnNum, int &errCode) +{ + static const JsonFieldPath maxConnNumField = {DB_CONFIG_MAX_CONN_NUM}; + if (!config.IsFieldExists(maxConnNumField)) { + return true; + } + + ValueObject configValue = config.GetObjectByPath(maxConnNumField, errCode); + if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { + GLOGE("Check DB config failed, the field type of maxConnNum is not NUMBER."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + + if (configValue.GetIntValue() < MIN_CONNECTION_NUM || configValue.GetIntValue() > MAX_CONNECTION_NUM) { + GLOGE("Check DB config failed, invalid maxConnNum value."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + + maxConnNum = static_cast(configValue.GetIntValue()); + return true; +} + +bool CheckBufferPoolSizeConfig(const JsonObject &config, int32_t pageSize, uint32_t &redoBufSize, + int &errCode) +{ + static const JsonFieldPath bufferPoolSizeField = {DB_CONFIG_BUFFER_POOL_SIZE}; + if (!config.IsFieldExists(bufferPoolSizeField)) { + return true; + } + + ValueObject configValue = config.GetObjectByPath(bufferPoolSizeField, errCode); + if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { + GLOGE("Check DB config failed, the field type of bufferPoolSize is not NUMBER."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + + if (configValue.GetIntValue() < MIN_BUFFER_POOL_SIZE || configValue.GetIntValue() > MAX_BUFFER_POOL_SIZE || + configValue.GetIntValue() < pageSize * 33) { + GLOGE("Check DB config failed, invalid bufferPoolSize value."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + + redoBufSize = static_cast(configValue.GetIntValue()); + return true; +} + +bool CheckCrcCheckEnableConfig(const JsonObject &config, uint32_t &crcCheckEnable, int &errCode) +{ + static const JsonFieldPath crcCheckEnableField = {DB_CONFIG_CRC_CHECK_ENABLE}; + if (!config.IsFieldExists(crcCheckEnableField)) { + return true; + } + + ValueObject configValue = config.GetObjectByPath(crcCheckEnableField, errCode); + if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { + GLOGE("Check DB config failed, the field type of crcCheckEnable is not NUMBER."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + + if (configValue.GetIntValue() != 0 && configValue.GetIntValue() != 1) { + GLOGE("Check DB config failed, invalid crcCheckEnable value."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + + crcCheckEnable = static_cast(configValue.GetIntValue()); + return true; +} + +bool CheckConfigSupport(const JsonObject &config, int &errCode) +{ + JsonObject child = config.GetChild(); + while (!child.IsNull()) { + std::string fieldName = child.GetItemFiled(); + if (std::find(DB_CONFIG.begin(), DB_CONFIG.end(), fieldName) == DB_CONFIG.end()) { + GLOGE("Invalid db config."); + errCode = -E_INVALID_CONFIG_VALUE; + return false; + } + child = child.GetNext(); + } + return true; +} +} + +DBConfig DBConfig::ReadConfig(const std::string &confStr, int &errCode) +{ + if (confStr.empty()) { + return {}; + } + + if (confStr.length() + 1 > MAX_DB_CONFIG_LEN) { + GLOGE("Config json string is too long."); + errCode = -E_OVER_LIMIT; + return {}; + } + + std::string lowerCaseConfStr = confStr; + std::transform(lowerCaseConfStr.begin(), lowerCaseConfStr.end(), lowerCaseConfStr.begin(), [](unsigned char c) { + return std::tolower(c); + }); + + JsonObject dbConfig = JsonObject::Parse(lowerCaseConfStr, errCode); + if (errCode != E_OK) { + GLOGE("Read DB config failed from str. %d", errCode); + return {}; + } + + if (!CheckConfigSupport(dbConfig, errCode)) { + GLOGE("Check DB config, not support config item. %d", errCode); + return {}; + } + + DBConfig conf; + if (!CheckPageSizeConfig(dbConfig, conf.pageSize_, errCode)) { + GLOGE("Check DB config 'pageSize' failed. %d", errCode); + return {}; + } + + if (!CheckRedoFlushConfig(dbConfig, conf.redoFlushByTrx_, errCode)) { + GLOGE("Check DB config 'redoFlushByTrx' failed. %d", errCode); + return {}; + } + + if (!CheckRedoBufSizeConfig(dbConfig, conf.redoPubBufSize_, errCode)) { + GLOGE("Check DB config 'redoPubBufSize' failed. %d", errCode); + return {}; + } + + if (!CheckMaxConnNumConfig(dbConfig, conf.maxConnNum_, errCode)) { + GLOGE("Check DB config 'maxConnNum' failed. %d", errCode); + return {}; + } + + if (!CheckBufferPoolSizeConfig(dbConfig, conf.pageSize_, conf.bufferPoolSize_, errCode)) { + GLOGE("Check DB config 'bufferPoolSize' failed. %d", errCode); + return {}; + } + + if (!CheckCrcCheckEnableConfig(dbConfig, conf.crcCheckEnable_, errCode)) { + GLOGE("Check DB config 'crcCheckEnable' failed. %d", errCode); + return {}; + } + + conf.configStr_ = confStr; + errCode = E_OK; + return conf; +} + +std::string DBConfig::ToString() const +{ + return configStr_; +} + +int32_t DBConfig::GetPageSize() const +{ + return pageSize_; +} + +bool DBConfig::operator==(const DBConfig &targetConfig) const +{ + return configStr_ == targetConfig.configStr_ && pageSize_ == targetConfig.pageSize_ && + redoFlushByTrx_ == targetConfig.redoFlushByTrx_ && redoPubBufSize_ == targetConfig.redoPubBufSize_ && + maxConnNum_ == targetConfig.maxConnNum_ && bufferPoolSize_ == targetConfig.bufferPoolSize_ && + crcCheckEnable_ == targetConfig.crcCheckEnable_; +} + +bool DBConfig::operator!=(const DBConfig &targetConfig) const +{ + return !(*this == targetConfig); +} +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp new file mode 100644 index 00000000..47a75e9c --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp @@ -0,0 +1,311 @@ +/* + * 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. + */ +#include +#include + +#include "json_common.h" +#include "doc_errno.h" +#include "log_print.h" +#include "securec.h" + +namespace DocumentDB { +ValueObject JsonCommon::GetValueByFiled(JsonObject &node, const std::string& filed) +{ + while (!node.IsNull()) { + if (node.GetItemFiled() == filed) { + auto itemValue = node.GetItemValue(); + return itemValue; + } + if (node.GetNext().IsNull()) { + return ValueObject(); + } + auto nodeNew = node.GetNext(); + node = nodeNew; + } + return ValueObject(); +} + +int JsonCommon::CheckLeafNode(JsonObject &node, std::vector &leafValue) +{ + if (node.GetChild().IsNull()) { + auto itemValue = node.GetItemValue(); + leafValue.emplace_back(itemValue); + } + if (!node.GetChild().IsNull()) { + auto nodeNew = node.GetChild(); + CheckLeafNode(nodeNew, leafValue); + } + if (!node.GetNext().IsNull()) { + auto nodeNew = node.GetNext(); + CheckLeafNode(nodeNew, leafValue); + } + return E_OK; +} + +std::vector JsonCommon::GetLeafValue(JsonObject &node) +{ + std::vector leafValue; + CheckLeafNode(node, leafValue); + return leafValue; +} + +bool JsonCommon::CheckNode(JsonObject &node, std::set filedSet, bool &errFlag) +{ + if (!errFlag) { + return false; + } + std::string fieldName; + if (!node.IsNull()) { + int ret = 0; + fieldName = node.GetItemFiled(ret); + if (filedSet.find(fieldName) == filedSet.end()) { + if (ret == E_OK) { + filedSet.insert(fieldName); + } + if (ret == E_OK && fieldName.empty()) { + errFlag = false; + return false; + } + } + else { + errFlag = false; + return false; + } + for (int i = 0; i < fieldName.size(); i++) { + if (!((isalpha(fieldName[i])) || (isdigit(fieldName[i])) || '_' == fieldName[i])) { + errFlag = false; + return false; + } + if (i == 0 && (isdigit(fieldName[i]))) { + errFlag = false; + return false; + } + } + } + if (!node.GetChild().IsNull()) { + auto nodeNew = node.GetChild(); + std::set newFiledSet; + CheckNode(nodeNew, newFiledSet, errFlag); + } + if (!node.GetNext().IsNull()) { + auto nodeNew = node.GetNext(); + CheckNode(nodeNew, filedSet, errFlag); + } + return errFlag; +} + +bool JsonCommon::CheckJsonField(JsonObject &jsonObj) +{ + std::set filedSet; + bool errFlag = true; + return CheckNode(jsonObj, filedSet, errFlag); +} + +bool JsonCommon::CheckProjectionNode(JsonObject &node, std::set filedSet, bool &errFlag, bool isFirstFloor) +{ + if (!errFlag) { + return false; + } + std::string fieldName; + if (!node.IsNull()) { + int ret = 0; + fieldName = node.GetItemFiled(ret); + if (filedSet.find(fieldName) == filedSet.end()) { + if (ret == E_OK) { + filedSet.insert(fieldName); + } + if (ret == E_OK && fieldName.empty()) { + errFlag = false; + return false; + } + } + else { + errFlag = false; + return false; + } + for (int i = 0; i < fieldName.size(); i++) { + if (!((isalpha(fieldName[i])) || (isdigit(fieldName[i])) || ('_' == fieldName[i]) || (isFirstFloor && '.' == fieldName[i]))) { + errFlag = false; + return false; + } + if (i == 0 && (isdigit(fieldName[i]))) { + errFlag = false; + return false; + } + } + } + if (!node.GetChild().IsNull()) { + auto nodeNew = node.GetChild(); + std::set newFiledSet; + CheckProjectionNode(nodeNew, newFiledSet, errFlag, false); + } + if (!node.GetNext().IsNull()) { + auto nodeNew = node.GetNext(); + CheckProjectionNode(nodeNew, filedSet, errFlag, isFirstFloor); + } + return errFlag; +} + +bool JsonCommon::CheckProjectionField(JsonObject &jsonObj) +{ + std::set filedSet; + bool errFlag = true; + bool isFirstFloor = true; + return CheckProjectionNode(jsonObj, filedSet, errFlag, isFirstFloor); +} + +int JsonCommon::ParseNode(JsonObject &node, std::vector singlePath, std::vector> &resultPath, bool isFirstFloor) +{ + std::vector fatherPath; + if (isFirstFloor) { + std::string tempParseName; + std::vector allFiledsName; + std::string priFieldName = node.GetItemFiled(); + for (int j = 0; j < priFieldName.size(); j++) { + if (priFieldName[j] != '.') { + tempParseName = tempParseName + priFieldName[j]; + } + if (priFieldName[j] == '.' || j == priFieldName.size() - 1) { + allFiledsName.emplace_back(tempParseName); + tempParseName.clear(); + } + } + fatherPath = singlePath; + singlePath.insert(singlePath.end(), allFiledsName.begin(), allFiledsName.end()); + } else { + std::vector allFiledsName; + allFiledsName.emplace_back(node.GetItemFiled()); + fatherPath = singlePath; + singlePath.insert(singlePath.end(), allFiledsName.begin(), allFiledsName.end()); + } + if (!node.GetChild().IsNull() && node.GetChild().GetItemFiled() != "") { + auto nodeNew = node.GetChild(); + ParseNode(nodeNew, singlePath, resultPath, false); + } + else { + resultPath.emplace_back(singlePath); + } + if (!node.GetNext().IsNull()) { + auto nodeNew = node.GetNext(); + ParseNode(nodeNew, fatherPath, resultPath, isFirstFloor); + } + return 0; +} + +std::vector> JsonCommon::ParsePath(const JsonObject &root) +{ + std::vector> resultPath; + auto projectionJson = root.GetChild(); + if (projectionJson.IsNull()) { + GLOGE("projectionJson is null"); + } + std::vector singlePath; + ParseNode(projectionJson, singlePath, resultPath, true); + return resultPath; +} + +namespace { +JsonFieldPath ExpendPath(const JsonFieldPath &path, bool &isCollapse) +{ + if (path.size() > 1) { // only first lever has collapse field + return path; + } + JsonFieldPath splitPath; + const std::string &str = path[0]; + size_t start = 0; + size_t end = 0; + while ((end = str.find('.', start)) != std::string::npos) { + splitPath.push_back(str.substr(start, end - start)); + start = end + 1; + } + if (start < str.length()) { + splitPath.push_back(str.substr(start)); + } + isCollapse = (splitPath.size() > 1); + return splitPath; +} + +void JsonObjectIterator(const JsonObject &obj, JsonFieldPath path, + std::function foo) +{ + JsonObject child = obj.GetChild(); + while(!child.IsNull()) { + JsonFieldPath childPath = path; + childPath.push_back(child.GetItemFiled()); + if (foo != nullptr && foo(childPath, obj, child)) { + JsonObjectIterator(child, childPath, foo); + } + child = child.GetNext(); + } + return; +} +} + +int JsonCommon::Append(const JsonObject &src, const JsonObject &add) +{ + int externErrCode = E_OK; + JsonObjectIterator(add, {}, + [&src, &externErrCode](const JsonFieldPath &path, const JsonObject &father, const JsonObject &item) { + bool isCollapse = false; + JsonFieldPath itemPath = ExpendPath(path, isCollapse); + JsonFieldPath fatherPath = itemPath; + fatherPath.pop_back(); + int errCode = E_OK; + if (src.IsFieldExists(itemPath)) { + JsonObject srcItem = src.FindItem(itemPath, errCode); + if (errCode != E_OK) { + externErrCode = (externErrCode == E_OK ? errCode : externErrCode); + GLOGE("Find item in source json object failed. %d", errCode); + return false; + } + if (srcItem.GetType() == JsonObject::Type::JSON_LEAF && item.GetType() == JsonObject::Type::JSON_LEAF) { + srcItem.SetItemValue(item.GetItemValue()); + return false; // Both leaf node, no need iterate + } else if (srcItem.GetType() != item.GetType()) { + JsonObject srcFatherItem = src.FindItem(fatherPath, errCode); + if (errCode != E_OK) { + externErrCode = (externErrCode == E_OK ? errCode : externErrCode); + GLOGE("Find father item in source json object failed. %d", errCode); + return false; + } + srcFatherItem.DeleteItemFromObject(itemPath.back()); + srcFatherItem.AddItemToObject(itemPath.back(), item); + return false; // Different node types, overwrite directly, skip child node + } + return true; // Both array or object + } else { + if (isCollapse) { + GLOGE("Add collapse item to object failed, path not exist."); + externErrCode = -E_DATA_CONFLICT; + return false; + } + JsonObject srcFatherItem = src.FindItem(fatherPath, errCode); + if (errCode == E_OK) { + errCode = srcFatherItem.AddItemToObject(itemPath.back(), item); + if (errCode != E_OK) { + externErrCode = (externErrCode == E_OK ? errCode : externErrCode); + GLOGE("Add item to object failed. %d", errCode); + return false; + } + } else { + externErrCode = -E_DATA_CONFLICT; + GLOGE("Find father item in source json object failed. %d", errCode); + } + return false; // Source path not exist, overwrite directly, skip child node + } + }); + return externErrCode; +} +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/log_print.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/log_print.cpp new file mode 100644 index 00000000..10fd0fff --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/log_print.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. +*/ + +#include +#include "securec.h" +#include "hilog/log.h" + +namespace DocumentDB { +namespace { + +void PrintLog(Logger::Level level, const std::string &tag, const std::string &msg) +{ + if (msg.empty()) { + return; + } + const std::string format = "%{public}s"; + OHOS::HiviewDFX::HiLogLabel label = { LOG_CORE, 0xD001630, tag.c_str() }; // log module id. // TODO: + switch (level) { + case Logger::Level::LEVEL_DEBUG: + (void)OHOS::HiviewDFX::HiLog::Debug(label, format.c_str(), msg.c_str()); + break; + case Logger::Level::LEVEL_INFO: + (void)OHOS::HiviewDFX::HiLog::Info(label, format.c_str(), msg.c_str()); + break; + case Logger::Level::LEVEL_WARN: + (void)OHOS::HiviewDFX::HiLog::Warn(label, format.c_str(), msg.c_str()); + break; + case Logger::Level::LEVEL_ERROR: + (void)OHOS::HiviewDFX::HiLog::Error(label, format.c_str(), msg.c_str()); + break; + case Logger::Level::LEVEL_FATAL: + (void)OHOS::HiviewDFX::HiLog::Fatal(label, format.c_str(), msg.c_str()); + break; + default: + break; + } +} + +void PreparePrivateLog(const char *format, std::string &outStrFormat) +{ + static const std::string PRIVATE_TAG = "s{private}"; + outStrFormat = format; + std::string::size_type pos = outStrFormat.find(PRIVATE_TAG); + if (pos != std::string::npos) { + outStrFormat.replace(pos, PRIVATE_TAG.size(), ".3s"); + } +} +} + +void Logger::Log(Level level, const std::string &tag, const char *func, int line, const char *format, ...) +{ + static const int maxLogLength = 1024; + + va_list argList; + va_start(argList, format); + char logBuff[maxLogLength]; + std::string msg; + std::string formatTemp; + PreparePrivateLog(format, formatTemp); + int bytes = vsnprintf_s(logBuff, maxLogLength, maxLogLength - 1, formatTemp.c_str(), argList); + if (bytes < 0) { + msg = "log buffer overflow!"; + } else { + msg = logBuff; + } + va_end(argList); + + PrintLog(level, tag, msg); +} +} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/os_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/os_api.cpp new file mode 100644 index 00000000..31786a74 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/os_api.cpp @@ -0,0 +1,84 @@ +/* + * 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. + */ +#include "os_api.h" +#include +#include +#include + +#include "doc_errno.h" +#include "log_print.h" +#include "securec.h" + +namespace DocumentDB { +namespace { + const int ACCESS_MODE_EXISTENCE = 0; +} +namespace OSAPI { +bool CheckPermission(const std::string &filePath) +{ + return (access(filePath.c_str(), R_OK) == 0) && (access(filePath.c_str(), W_OK) == 0); +} + +bool CheckPathExistence(const std::string &filePath) +{ + return (access(filePath.c_str(), ACCESS_MODE_EXISTENCE) == 0); +} + +int GetRealPath(const std::string &inOriPath, std::string &outRealPath) +{ + const unsigned int MAX_PATH_LENGTH = PATH_MAX; + if (inOriPath.length() > MAX_PATH_LENGTH || MAX_PATH_LENGTH > 0x10000) { // max limit is 64K(0x10000). + GLOGE("[OS_API] OriPath too long."); + return -E_INVALID_ARGS; + } + + char *realPath = new (std::nothrow) char[MAX_PATH_LENGTH + 1]; + if (realPath == nullptr) { + return -E_OUT_OF_MEMORY; + } + if (memset_s(realPath, MAX_PATH_LENGTH + 1, 0, MAX_PATH_LENGTH + 1) != EOK) { + delete []realPath; + return -E_SECUREC_ERROR; + } + + if (realpath(inOriPath.c_str(), realPath) == nullptr) { + GLOGE("[OS_API] Realpath error:%d.", errno); + delete []realPath; + return -E_SYSTEM_API_FAIL; + } + outRealPath = std::string(realPath); + delete []realPath; + return E_OK; +} + +void SplitFilePath(const std::string &filePath, std::string &fileDir, std::string &fileName) +{ + if (filePath.empty()) { + return; + } + + auto slashPos = filePath.find_last_of('/'); + if (slashPos == std::string::npos) { + fileName = filePath; + fileDir = ""; + return; + } + + fileDir = filePath.substr(0, slashPos); + fileName = filePath.substr(slashPos + 1); + return; +} +} // namespace OSAPI +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/base/grd_db_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/base/grd_db_api.cpp new file mode 100644 index 00000000..fff6188d --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/base/grd_db_api.cpp @@ -0,0 +1,75 @@ +/* +* 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. +*/ + +#include "grd_base/grd_db_api.h" + +#include "doc_errno.h" +#include "document_store_manager.h" +#include "grd_base/grd_error.h" +#include "log_print.h" +#include "grd_type_inner.h" + +using namespace DocumentDB; + +int GRD_DBOpen(const char *dbPath, const char *configStr, unsigned int flags, GRD_DB **db) +{ + if (db == nullptr) { + return GRD_INVALID_ARGS; + } + std::string path = (dbPath == nullptr ? "" : dbPath); + std::string config = (configStr == nullptr ? "" : configStr); + DocumentStore *store = nullptr; + int ret = DocumentStoreManager::GetDocumentStore(path, config, flags, store); + if (ret != E_OK || store == nullptr) { + return TrasnferDocErr(ret); + } + + *db = new (std::nothrow) GRD_DB(); + if (*db == nullptr) { + (void)DocumentStoreManager::CloseDocumentStore(store, GRD_DB_CLOSE_IGNORE_ERROR); + store = nullptr; + ret = -E_OUT_OF_MEMORY; + } + + (*db)->store_ = store; + return TrasnferDocErr(ret); +} + +int GRD_DBClose(GRD_DB *db, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr) { + return GRD_INVALID_ARGS; + } + + int ret = DocumentStoreManager::CloseDocumentStore(db->store_, flags); + if (ret != E_OK) { + return TrasnferDocErr(ret); + } + + db->store_ = nullptr; + delete db; + return GRD_OK; +} + +int GRD_Flush(GRD_DB *db, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr) { + return GRD_INVALID_ARGS; + } + if (flags != GRD_DB_FLUSH_ASYNC && flags != GRD_DB_FLUSH_SYNC) { + return GRD_INVALID_ARGS; + } + return GRD_OK; +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp new file mode 100644 index 00000000..e8370c8d --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp @@ -0,0 +1,152 @@ +/* + * 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. + */ +#include +#include + +#include "document_check.h" +#include "doc_errno.h" +#include "log_print.h" +#include "securec.h" + +namespace DocumentDB { +namespace { +constexpr const char *KEY_ID = "_id"; +constexpr const char *COLLECTION_PREFIX_GRD = "GRD_"; +constexpr const char *COLLECTION_PREFIX_GM_SYS = "GM_SYS_"; +const int MAX_COLLECTION_NAME = 511; +const int MAX_ID_LENS = 899; +const int JSON_DEEP_MAX = 4; + +bool CheckCollectionNamePrefix(const std::string &name, const std::string &prefix) +{ + if (name.length() < prefix.length()) { + return false; + } + + auto itPrefix = prefix.begin(); + auto itName = name.begin(); + while (itPrefix != prefix.end()) { + if (std::tolower(*itPrefix) != std::tolower(*itName)) { + return false; + } + itPrefix++; + itName++; + } + return true; +} +} + +bool CheckCommon::CheckCollectionName(const std::string &collectionName, std::string &lowerCaseName, int &errCode) +{ + if (collectionName.empty()) { + errCode = -E_INVALID_ARGS; + return false; + } + if (collectionName.length() > MAX_COLLECTION_NAME) { + errCode = -E_OVER_LIMIT; + return false; + } + if (CheckCollectionNamePrefix(collectionName, COLLECTION_PREFIX_GRD) || + CheckCollectionNamePrefix(collectionName, COLLECTION_PREFIX_GM_SYS)) { + GLOGE("Collection name is illegal"); + errCode = -E_INVALID_COLL_NAME_FORMAT; + return false; + } + lowerCaseName = collectionName; + std::transform(lowerCaseName.begin(), lowerCaseName.end(), lowerCaseName.begin(), [](unsigned char c){ + return std::tolower(c); + }); + return true; +} + +int CheckCommon::CheckFilter(JsonObject &filterObj) +{ + if (filterObj.GetDeep() > JSON_DEEP_MAX) { + GLOGE("filter's json deep is deeper than JSON_DEEP_MAX"); + return -E_INVALID_ARGS; + } + int ret = CheckIdFormat(filterObj); + if (ret != E_OK) { + GLOGE("Filter Id format is illegal"); + return ret; + } + if (!filterObj.GetChild().GetNext().IsNull()) { + return -E_INVALID_ARGS; + } + return E_OK; +} + +int CheckCommon::CheckIdFormat(JsonObject &filterJson) +{ + auto filterObjChild = filterJson.GetChild(); + auto idValue = JsonCommon::GetValueByFiled(filterObjChild, KEY_ID); + if (idValue.GetValueType() != ValueObject::ValueType::VALUE_STRING) { + return -E_INVALID_ARGS; + } + if (idValue.GetStringValue().length() > MAX_ID_LENS) { + return -E_OVER_LIMIT; + } + return E_OK; +} + +int CheckCommon::CheckDocument(JsonObject &documentObj) +{ + if (documentObj.GetDeep() > JSON_DEEP_MAX) { + GLOGE("documentObj's json deep is deeper than JSON_DEEP_MAX"); + return -E_INVALID_ARGS; + } + int ret = CheckIdFormat(documentObj); + if (ret != E_OK) { + GLOGE("Document Id format is illegal"); + return ret; + } + if (!documentObj.GetChild().IsNull()) { + auto documentObjChild = documentObj.GetChild(); + if (!JsonCommon::CheckJsonField(documentObjChild)) { + GLOGE("Document json field format is illegal"); + return -E_INVALID_ARGS; + } + } + return E_OK; +} + +bool CheckCommon::CheckProjection(JsonObject &projectionObj, std::vector> &path) +{ + if (projectionObj.GetDeep() > JSON_DEEP_MAX) { + GLOGE("projectionObj's json deep is deeper than JSON_DEEP_MAX"); + return -E_INVALID_ARGS; + } + if (!projectionObj.GetChild().IsNull()) { + auto projectionObjChild = projectionObj.GetChild(); + if (!JsonCommon::CheckProjectionField(projectionObjChild)) { + GLOGE("projection json field format is illegal"); + return false; + } + } + for (int i = 0; i < path.size(); i++) { + for (auto fieldName : path[i]) { + for (int i = 0; i < fieldName.size(); i++) { + if (!((isalpha(fieldName[i])) || (isdigit(fieldName[i])) || ('_' == fieldName[i]))) { + return false; + } + if (i == 0 && (isdigit(fieldName[i]))) { + return false; + } + } + } + } + return true; +} +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h new file mode 100644 index 00000000..57769260 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.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 DOCUMENT_CHECK_H +#define DOCUMENT_CHECK_H + +#include +#include +#include "json_common.h" + +namespace DocumentDB { +class JsonCommon; +class CheckCommon +{ +public: + CheckCommon() = default; + ~CheckCommon() = default; + + static bool CheckCollectionName(const std::string &collectionName, std::string &lowerCaseName, int &errCode); + static int CheckFilter(JsonObject &document); + static int CheckIdFormat(JsonObject &data); + static int CheckDocument(JsonObject &document); + static bool CheckProjection(JsonObject &projectionObj, std::vector> &path); +}; +using Key = std::vector; +using Value = std::vector; + +constexpr const char *COLL_PREFIX = "GRD_COLL_"; +} // DocumentDB +#endif // DOCUMENT_CHECK_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_document_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_document_api.cpp new file mode 100644 index 00000000..e033b9fb --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_document_api.cpp @@ -0,0 +1,121 @@ +/* +* 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. +*/ + +#include "grd_document/grd_document_api.h" +#include "grd_base/grd_error.h" +#include "grd_type_inner.h" +#include "grd_resultset_inner.h" +#include "log_print.h" +using namespace DocumentDB; + +int GRD_CreateCollection(GRD_DB *db, const char *collectionName, const char *optionStr, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr) { + return GRD_INVALID_ARGS; + } + + std::string name = (collectionName == nullptr ? "" : collectionName); + std::string option = (optionStr == nullptr ? "" : optionStr); + int ret = db->store_->CreateCollection(name, option, flags); + return TrasnferDocErr(ret); +} + +int GRD_DropCollection(GRD_DB *db, const char *collectionName, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr) { + return GRD_INVALID_ARGS; + } + + std::string name = (collectionName == nullptr ? "" : collectionName); + int ret = db->store_->DropCollection(name, flags); + return TrasnferDocErr(ret); +} + +int GRD_UpdateDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *update, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr) { + return GRD_INVALID_ARGS; + } + + std::string name = (collectionName == nullptr ? "" : collectionName); + std::string filterStr = (filter == nullptr ? "" : filter); + std::string updateStr = (update == nullptr ? "" : update); + int ret = db->store_->UpdateDocument(name, filterStr, updateStr, flags); + return TrasnferDocErr(ret); +} + +int GRD_UpsertDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *document, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr) { + return GRD_INVALID_ARGS; + } + + std::string name = (collectionName == nullptr ? "" : collectionName); + std::string filterStr = (filter == nullptr ? "" : filter); + std::string documentStr = (document == nullptr ? "" : document); + int ret = db->store_->UpsertDocument(name, filterStr, documentStr, flags); + return TrasnferDocErr(ret); +} + +int GRD_InsertDoc(GRD_DB *db, const char *collectionName, const char *document, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || document == nullptr) { + return GRD_INVALID_ARGS; + } + int ret = db->store_->InsertDocument(collectionName, document, flags); + return TrasnferDocErr(ret); +} + +int GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr || filter == nullptr || collectionName == nullptr) { + return GRD_INVALID_ARGS; + } + int ret = db->store_->DeleteDocument(collectionName, filter, flags); + int errCode = TrasnferDocErr(ret); + int deleteCount = 0; + switch (errCode) { + case GRD_OK: + deleteCount = 1; + return deleteCount; + break; + case GRD_NO_DATA: + deleteCount = 0; + return deleteCount; + break; + } + return errCode; +} + +int GRD_FindDoc(GRD_DB *db, const char *collectionName, Query query, unsigned int flags, GRD_ResultSet **resultSet) +{ + if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || resultSet == nullptr || query.filter == nullptr + || query.projection == nullptr) { + return GRD_INVALID_ARGS; + } + GRD_ResultSet *grdResultSet = new (std::nothrow)GRD_ResultSet(); + if (grdResultSet == nullptr) { + GLOGE("Memory allocation failed!" ); + return -E_FAILED_MEMORY_ALLOCATE; + } + int ret = db->store_->FindDocument(collectionName, query.filter, query.projection, flags, grdResultSet); + if (ret != E_OK) { + delete grdResultSet; + *resultSet = nullptr; + return TrasnferDocErr(ret); + } + *resultSet = grdResultSet; + return TrasnferDocErr(ret); +} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_resultset_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_resultset_api.cpp new file mode 100644 index 00000000..6db93e85 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_resultset_api.cpp @@ -0,0 +1,71 @@ +/* +* 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. +*/ +#include + +#include "grd_base/grd_error.h" +#include "doc_errno.h" +#include "grd_base/grd_error.h" +#include "grd_resultset_inner.h" +#include "grd_base/grd_resultset_api.h" +#include "log_print.h" + +using namespace DocumentDB; + +int GRD_Next(GRD_ResultSet *resultSet) +{ + if (resultSet == nullptr) { + GLOGE("resultSet is nullptr"); + return GRD_INVALID_ARGS; + }; + std::mutex dbMutex; + std::lock_guard lock(dbMutex); + int ret = resultSet->resultSet_.GetNext(); + return TrasnferDocErr(ret); +} + +int GRD_GetValue(GRD_ResultSet *resultSet, char **value) +{ + if (resultSet == nullptr) { + GLOGE("resultSet is nullptr,cant get value from it"); + return GRD_INVALID_ARGS; + }; + char *val = nullptr; + int ret = resultSet->resultSet_.GetValue(&val); + if (val == nullptr) { + GLOGE("Value that get from resultSet is nullptr"); + return GRD_NOT_AVAILABLE; + } + *value = val; + return TrasnferDocErr(ret); +} + +int GRD_FreeValue(char *value) +{ + if (value == nullptr) { + return GRD_OK; + } + delete[] value; + return GRD_OK; +} + +int GRD_FreeResultSet(GRD_ResultSet *resultSet) +{ + if (resultSet == nullptr) { + return GRD_INVALID_ARGS; + } + resultSet->resultSet_.EraseCollection(); + delete resultSet; + return GRD_OK; +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_format_config.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_format_config.h new file mode 100644 index 00000000..06e7bbee --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_format_config.h @@ -0,0 +1,32 @@ +/* +* 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 GRD_FORMAT_CONFIG_H +#define GRD_FORMAT_CONFIG_H + + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#define COLLECTION_LENS_MAX (512 * 1024) +#define JSON_LENS_MAX (512 * 1024) +#define JSON_DEEP_MAX (4) +#define KEY_ID ("_id") + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // GRD_FORMAT_CONFIG_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_resultset_inner.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_resultset_inner.h new file mode 100644 index 00000000..d840ad79 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_resultset_inner.h @@ -0,0 +1,26 @@ +/* +* 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 GRD_RESULTSET_INNER_H +#define GRD_RESULTSET_INNER_H + +#include "doc_errno.h" +#include "grd_base/grd_error.h" +#include "result_set.h" + +typedef struct GRD_ResultSet { + DocumentDB::ResultSet resultSet_; +} GRD_ResultSet; +#endif // GRD_RESULTSET_INNER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_type_inner.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_type_inner.h new file mode 100644 index 00000000..63663701 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_type_inner.h @@ -0,0 +1,27 @@ +/* +* 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 GRD_TYPE_INNER_H +#define GRD_TYPE_INNER_H + +#include "document_store.h" +#include "doc_errno.h" +#include "grd_base/grd_error.h" + +typedef struct GRD_DB { + DocumentDB::DocumentStore *store_ = nullptr; +} GRD_DB; + +#endif // GRD_TYPE_INNER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h new file mode 100644 index 00000000..e242bcc7 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.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 COLLECTION_H +#define COLLECTION_H + +#include +#include "document_check.h" +#include "kv_store_executor.h" + +namespace DocumentDB { +class Collection { +public: + Collection(const std::string &name, KvStoreExecutor *executor); + Collection(const Collection &a) {}; + Collection() {}; + ~Collection(); + + int PutDocument(const Key &key, const Value &document); + int GetDocument(const Key &key, Value &document) const; + int DeleteDocument(const Key &key); + int UpsertDocument(const std::string &id, const std::string &document, bool isReplace = true); + bool FindDocument(); + int UpdateDocument(const std::string &id, const std::string &document); +private: + std::string name_; + KvStoreExecutor *executor_ = nullptr; +}; +} // namespace DocumentDB +#endif // COLLECTION_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h new file mode 100644 index 00000000..6f159683 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h @@ -0,0 +1,44 @@ +/* +* 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 DOC_ERRNO_H +#define DOC_ERRNO_H + +namespace DocumentDB { +constexpr int E_OK = 0; +constexpr int E_BASE = 1000; +constexpr int E_ERROR = E_BASE + 1; +constexpr int E_INVALID_ARGS = E_BASE + 2; +constexpr int E_UNFINISHED = E_BASE + 7; +constexpr int E_OUT_OF_MEMORY = E_BASE + 8; +constexpr int E_SECUREC_ERROR = E_BASE + 9; +constexpr int E_SYSTEM_API_FAIL = E_BASE + 10; +constexpr int E_FILE_OPERATION = E_BASE + 11; +constexpr int E_OVER_LIMIT = E_BASE + 12; +constexpr int E_INVALID_CONFIG_VALUE = E_BASE + 13; +constexpr int E_NOT_FOUND = E_BASE + 14; +constexpr int E_COLLECTION_CONFLICT = E_BASE + 15; +constexpr int E_NO_DATA = E_BASE + 16; +constexpr int E_NOT_PERMIT = E_BASE + 17; +constexpr int E_DATA_CONFLICT = E_BASE + 18; +constexpr int E_INVALID_COLL_NAME_FORMAT = E_BASE + 18; +constexpr int E_INVALID_JSON_FORMAT = E_BASE + 40; +constexpr int E_JSON_PATH_NOT_EXISTS = E_BASE + 41; +constexpr int E_RESOURCE_BUSY = E_BASE + 50; +constexpr int E_FAILED_MEMORY_ALLOCATE = E_BASE + 51; + +int TrasnferDocErr(int err); +} // DocumentDB +#endif // DOC_ERRNO_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store.h new file mode 100644 index 00000000..648c6499 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store.h @@ -0,0 +1,50 @@ +/* +* 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 DOCUMENT_STORE_H +#define DOCUMENT_STORE_H + +#include +#include +#include + +#include "kv_store_executor.h" +#include "collection.h" + +struct GRD_ResultSet; +namespace DocumentDB { +class DocumentStore { +public: + DocumentStore(KvStoreExecutor *); + ~DocumentStore(); + + int CreateCollection(const std::string &name, const std::string &option, int flags); + int DropCollection(const std::string &name, int flags); + + int UpdateDocument(const std::string &collection, const std::string &filter, const std::string &update, int flags); + int UpsertDocument(const std::string &collection, const std::string &filter, const std::string &document, int flags); + int InsertDocument(const std::string &collection, const std::string &document, int flag); + int DeleteDocument(const std::string &collection, const std::string &filter, int flag); + int FindDocument(const std::string &collection, const std::string &filter, const std::string &projection, int flags, GRD_ResultSet *grdResultSet); + KvStoreExecutor *GetExecutor(int errCode); + int EraseCollection(const std::string collectionName); +private: + int GetViewType(JsonObject &jsonObj, bool &viewType); + std::mutex dbMutex_; + KvStoreExecutor *executor_ = nullptr; + std::map collections_; +}; +} // DocumentDB +#endif // DOCUMENT_STORE_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store_manager.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store_manager.h new file mode 100644 index 00000000..70043162 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store_manager.h @@ -0,0 +1,35 @@ +/* +* 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 DOCUMENT_STORE_MANAGER_H +#define DOCUMENT_STORE_MANAGER_H + +#include +#include "document_store.h" + +namespace DocumentDB { +class DocumentStoreManager { +public: + static int GetDocumentStore(const std::string &path, const std::string &config, unsigned int flags, + DocumentStore *&store); + + static int CloseDocumentStore(DocumentStore *store, unsigned int flags); + +private: + static bool CheckDBPath(const std::string &path, std::string &canonicalPath, std::string &dbName, int &errCode); + static bool CheckDBConfig(const std::string &config, int &errCode); +}; +} // DocumentDB +#endif // DOCUMENT_STORE_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/projection_tree.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/projection_tree.h new file mode 100644 index 00000000..15920bd4 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/projection_tree.h @@ -0,0 +1,37 @@ +#ifndef PROJECTION_TREE_H +#define PROJECTION_TREE_H + +#include +#include +#include +#include "doc_errno.h" +#include "json_common.h" +#include "log_print.h" + +namespace DocumentDB { +struct ProjectionNode { + std::unordered_map SonNode; + bool isDeepest; + int Deep; + int ViewType; + ProjectionNode() { + Deep = 0; + isDeepest = true; + } + int DeleteProjectionNode(); + ~ProjectionNode () { + DeleteProjectionNode(); + } +}; +class ProjectionTree { +public: + ProjectionTree(); + ~ProjectionTree(); + + int ParseTree(std::vector> &path); + bool SearchTree(std::vector &singlePath, int &index); +private: + ProjectionNode node_; +}; +} // DocumentDB +#endif // PROJECTION_TREE_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set.h new file mode 100644 index 00000000..dc87115e --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set.h @@ -0,0 +1,53 @@ +/* +* 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 RESULTSET_H +#define RESULTSET_H + +#include +#include "grd_base/grd_type_export.h" +#include "projection_tree.h" +#include "vector" +#include "doc_errno.h" +#include "json_object.h" +#include "securec.h" +#include "document_check.h" +#include "document_store.h" + +namespace DocumentDB { +class ResultSet { +public: + ResultSet(); + ~ResultSet(); + + int Init(DocumentStore *store, const std::string collectionName, ValueObject &key, std::vector> &path, bool ifShowId, bool viewType); + int GetNext(); + int GetValue(char **value); + int EraseCollection(); +private: + int CutJsonBranch(std::string &jsonData); + int CheckCutNode(JsonObject *node, std::vector singleCutPath, std::vector> &allCutPath); + DocumentStore *store_ = nullptr; + std::string collectionName_; + ValueObject key_; + bool ifShowId_ = false; + bool viewType_ = false; + ProjectionTree projectionTree_; + std::vector> projectionPath_; + int index_ = 0; + std::vector findValue_; +}; +} // DocumentDB +#endif //RESULTSET_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set_common.h new file mode 100644 index 00000000..dfef9c26 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set_common.h @@ -0,0 +1,31 @@ +/* +* 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 RESULTSET_COMMON_H +#define RESULTSET_COMMON_H + +#include +#include "grd_base/grd_type_export.h" +#include "vector" +#include "doc_errno.h" +#include "result_set.h" + +namespace DocumentDB { +class ValueObject; +int InitResultSet(DocumentStore *store, const std::string collectionName, ValueObject &key, std::vector> &path, bool ifShowId, bool viewType, + ResultSet &resultSet); +} // DocumentDB +#endif //RESULTSET_COMMON_H + diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp new file mode 100644 index 00000000..fc9fd3ba --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp @@ -0,0 +1,177 @@ +/* +* 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. +*/ + +#include "collection.h" + +#include + +#include "document_check.h" +#include "doc_errno.h" +#include "log_print.h" + +namespace DocumentDB { +Collection::Collection(const std::string &name, KvStoreExecutor *executor) : executor_(executor) +{ + std::string lowerCaseName = name; + std::transform(lowerCaseName.begin(), lowerCaseName.end(), lowerCaseName.begin(), [](unsigned char c) { + return std::tolower(c); + }); + name_ = COLL_PREFIX + lowerCaseName; +} + +Collection::~Collection() +{ + executor_ = nullptr; +} + +int Collection::PutDocument(const Key &key, const Value &document) +{ + if (executor_ == nullptr) { + return -E_INVALID_ARGS; + } + return executor_->PutData(name_, key, document); +} + +bool Collection::FindDocument() +{ + if (executor_ == nullptr) { + return -E_INVALID_ARGS; + } + int errCode = 0; + return executor_->IsCollectionExists(name_, errCode); +} + +int Collection::GetDocument(const Key &key, Value &document) const +{ + if (executor_ == nullptr) { + return -E_INVALID_ARGS; + } + return executor_->GetData(name_, key, document); +} + +int Collection::DeleteDocument(const Key &key) +{ + if (executor_ == nullptr) { + return -E_INVALID_ARGS; + } + return executor_->DelData(name_, key); +} + +int Collection::UpsertDocument(const std::string &id, const std::string &document, bool isReplace) +{ + if (executor_ == nullptr) { + return -E_INVALID_ARGS; + } + + int errCode = E_OK; + bool isCollExist = executor_->IsCollectionExists(name_, errCode); + if (errCode != E_OK) { + GLOGE("Check collection failed. %d", errCode); + return -errCode; + } + if (!isCollExist) { + GLOGE("Collection not created."); + return -E_NO_DATA; + } + + JsonObject upsertValue = JsonObject::Parse(document, errCode); + if (errCode != E_OK) { + GLOGD("Parse upsert value failed. %d", errCode); + return errCode; + } + + Key keyId(id.begin(), id.end()); + Value valSet(document.begin(), document.end()); + + if (!isReplace) { + Value valueGot; + errCode = executor_->GetData(name_, keyId, valueGot); + std::string valueGotStr = std::string(valueGot.begin(), valueGot.end()); + + if (errCode != E_OK && errCode != -E_NOT_FOUND) { + GLOGE("Get original document failed. %d", errCode); + return errCode; + } else if (errCode == E_OK) { // document has been inserted + GLOGD("Document has been inserted, append value."); + JsonObject originValue = JsonObject::Parse(valueGotStr, errCode); + if (errCode != E_OK) { + GLOGD("Parse original value failed. %d %s", errCode, valueGotStr.c_str()); + return errCode; + } + + errCode = JsonCommon::Append(originValue, upsertValue); + if (errCode != E_OK) { + GLOGD("Append value failed. %d", errCode); + return errCode; + } + + std::string valStr = originValue.Print(); + valSet = {valStr.begin(), valStr.end()}; + } + } + + return executor_->PutData(name_, keyId, valSet); +} + +int Collection::UpdateDocument(const std::string &id, const std::string &update) +{ + if (executor_ == nullptr) { + return -E_INVALID_ARGS; + } + + int errCode = E_OK; + bool isCollExist = executor_->IsCollectionExists(name_, errCode); + if (errCode != E_OK) { + GLOGE("Check collection failed. %d", errCode); + return -errCode; + } + if (!isCollExist) { + GLOGE("Collection not created."); + return -E_NO_DATA; + } + + JsonObject updateValue = JsonObject::Parse(update, errCode); + if (errCode != E_OK) { + GLOGD("Parse upsert value failed. %d", errCode); + return errCode; + } + + Key keyId(id.begin(), id.end()); + Value valueGot; + errCode = executor_->GetData(name_, keyId, valueGot); + std::string valueGotStr = std::string(valueGot.begin(), valueGot.end()); + if (errCode != E_OK) { + GLOGE("Get original document failed. %d", errCode); + return errCode; + } + + GLOGD("Update document value."); + JsonObject originValue = JsonObject::Parse(valueGotStr, errCode); + if (errCode != E_OK) { + GLOGD("Parse original value failed. %d %s", errCode, valueGotStr.c_str()); + return errCode; + } + + errCode = JsonCommon::Append(originValue, updateValue); + if (errCode != E_OK) { + GLOGD("Append value failed. %d", errCode); + return errCode; + } + + std::string valStr = originValue.Print(); + Value valSet(valStr.begin(), valStr.end()); + return executor_->PutData(name_, keyId, valSet); +} +} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp new file mode 100644 index 00000000..5bae14d9 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp @@ -0,0 +1,79 @@ +/* +* 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. +*/ + +#include "doc_errno.h" +#include "grd_base/grd_error.h" + +namespace DocumentDB { +int GetErrorCategory(int errCode) +{ + int categoryCode = errCode % 1000000; + categoryCode /= 1000; + categoryCode *= 1000; + return categoryCode; +} + +int TrasnferDocErr(int err) +{ + if(err > 0) { + return err; + } + + int outErr = GRD_OK; + switch (err) { + case E_OK: + return GRD_OK; + case -E_ERROR: + outErr = GRD_INNER_ERR; + break; + case -E_INVALID_ARGS: + outErr = GRD_INVALID_ARGS; + break; + case -E_FILE_OPERATION: + outErr = GRD_FAILED_FILE_OPERATION; + break; + case -E_OVER_LIMIT: + outErr = GRD_OVER_LIMIT; + break; + case -E_INVALID_JSON_FORMAT: + outErr = GRD_INVALID_JSON_FORMAT; + break; + case -E_INVALID_CONFIG_VALUE: + outErr = GRD_INVALID_CONFIG_VALUE; + break; + case -E_COLLECTION_CONFLICT: + outErr = GRD_COLLECTION_CONFLICT; + break; + case -E_NO_DATA: + case -E_NOT_FOUND: + outErr = GRD_NO_DATA; + break; + case -E_INVALID_COLL_NAME_FORMAT: + outErr = GRD_INVALID_COLLECTION_NAME; + break; + case -E_RESOURCE_BUSY: + outErr = GRD_RESOURCE_BUSY; + break; + case -E_FAILED_MEMORY_ALLOCATE: + outErr = GRD_FAILED_MEMORY_ALLOCATE; + break; + default: + outErr = GRD_INNER_ERR; + break; + } + + return GetErrorCategory(outErr); +} +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp new file mode 100644 index 00000000..0bfe0aff --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp @@ -0,0 +1,449 @@ +/* +* 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. +*/ + +#include "document_store.h" +#include "collection_option.h" +#include "document_check.h" +#include "doc_errno.h" +#include "grd_base/grd_type_export.h" +#include "log_print.h" +#include "result_set_common.h" +#include "grd_resultset_inner.h" + +namespace DocumentDB { +const int JSON_LENS_MAX = 512 * 1024; +constexpr const char *KEY_ID = "_id"; +const bool caseIsSensitive = true; + +DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) +{ +} + +DocumentStore::~DocumentStore() +{ + delete executor_; +} + +int DocumentStore::CreateCollection(const std::string &name, const std::string &option, int flags) +{ + std::string lowerCaseName; + int errCode = E_OK; + if (!CheckCommon::CheckCollectionName(name, lowerCaseName, errCode)) { + GLOGE("Check collection name invalid. %d", errCode); + return errCode; + } + + errCode = E_OK; + CollectionOption collOption = CollectionOption::ReadOption(option, errCode); + if (errCode != E_OK) { + GLOGE("Read collection option str failed. %d", errCode); + return errCode; + } + + if (flags != 0 && flags != CHK_EXIST_COLLECTION) { + GLOGE("Check flags invalid."); + return -E_INVALID_ARGS; + } + + std::lock_guard lock(dbMutex_); + bool ignoreExists = (flags != CHK_EXIST_COLLECTION); + errCode = executor_->CreateCollection(lowerCaseName, ignoreExists); + if (errCode != E_OK) { + GLOGE("Create collection failed. %d", errCode); + return errCode; + } + std::string oriOptStr; + errCode = executor_->GetCollectionOption(lowerCaseName, oriOptStr); + if (errCode == -E_NOT_FOUND) { + executor_->SetCollectionOption(lowerCaseName, collOption.ToString()); + errCode = E_OK; + } else { + CollectionOption oriOption = CollectionOption::ReadOption(oriOptStr, errCode); + if (collOption != oriOption) { + GLOGE("Create collection failed, option changed."); + return -E_INVALID_CONFIG_VALUE; + } + } + + return errCode; +} + +int DocumentStore::DropCollection(const std::string &name, int flags) +{ + std::string lowerCaseName; + int errCode = E_OK; + if (!CheckCommon::CheckCollectionName(name, lowerCaseName, errCode)) { + GLOGE("Check collection name invalid. %d", errCode); + return errCode; + } + + if (flags != 0 && flags != CHK_NON_EXIST_COLLECTION) { + GLOGE("Check flags invalid."); + return -E_INVALID_ARGS; + } + + bool ignoreNonExists = (flags != CHK_NON_EXIST_COLLECTION); + std::lock_guard lock(dbMutex_); + errCode = executor_->DropCollection(lowerCaseName, ignoreNonExists); + if (errCode != E_OK) { + GLOGE("Drop collection failed. %d", errCode); + return errCode; + } + + errCode = executor_->CleanCollectionOption(lowerCaseName); + if (errCode != E_OK && errCode != -E_NO_DATA) { + GLOGE("Clean collection option failed. %d", errCode); + return errCode; + } + + return E_OK; +} + +namespace { +bool CheckFilter(const std::string &filter, std::string &idStr, int &errCode) +{ + if (filter.empty()) { + errCode = -E_INVALID_ARGS; + GLOGE("Check filter invalid. %d", errCode); + return false; + } + + JsonObject filterObject = JsonObject::Parse(filter, errCode, true); + if (errCode != E_OK) { + GLOGE("Parse filter failed. %d", errCode); + return false; + } + + JsonObject filterId = filterObject.GetObjectItem("_id", errCode); + if (errCode != E_OK || filterId.GetItemValue().GetValueType() != ValueObject::ValueType::VALUE_STRING) { + GLOGE("Check filter '_id' not found or type not string."); + errCode = -E_INVALID_ARGS; + return false; + } + + idStr = filterId.GetItemValue().GetStringValue(); + return true; +} + +bool CheckDocument(const std::string &updateStr, int &errCode) +{ + if (updateStr.empty()) { + errCode = -E_INVALID_ARGS; + return false; + } + + JsonObject updateObj = JsonObject::Parse(updateStr, errCode); + if (updateObj.IsNull() || errCode != E_OK) { + GLOGE("Parse update document failed. %d", errCode); + return false; + } + + JsonObject filterId = updateObj.GetObjectItem("_id", errCode); + if (errCode != -E_NOT_FOUND) { + GLOGE("Can not change '_id' with update document failed."); + return false; + } + + return true; +} +} + +int DocumentStore::UpdateDocument(const std::string &collection, const std::string &filter, const std::string &update, + int flags) +{ + std::string lowerCaseCollName; + int errCode = E_OK; + if (!CheckCommon::CheckCollectionName(collection, lowerCaseCollName, errCode)) { + GLOGE("Check collection name invalid. %d", errCode); + return errCode; + } + + std::string idStr; + if (!CheckFilter(filter, idStr, errCode)) { + GLOGE("Check update filter failed. %d", errCode); + return errCode; + } + + if (!CheckDocument(update, errCode)) { + GLOGE("Check update document failed. %d", errCode); + return errCode; + } + + if (flags != 0) { + GLOGE("Check flags invalid."); + return -E_INVALID_ARGS; + } + + std::string docId(idStr.begin(), idStr.end()); + + std::lock_guard lock(dbMutex_); + auto coll = Collection(lowerCaseCollName, executor_); + errCode = coll.UpdateDocument(docId, update); + if (errCode == E_OK) { + errCode = 1; // update one record. + } + return errCode; +} + +int DocumentStore::UpsertDocument(const std::string &collection, const std::string &filter, const std::string &document, + int flags) +{ + std::string lowerCaseCollName; + int errCode = E_OK; + if (!CheckCommon::CheckCollectionName(collection, lowerCaseCollName, errCode)) { + GLOGE("Check collection name invalid. %d", errCode); + return errCode; + } + + std::string idStr; + if (!CheckFilter(filter, idStr, errCode)) { + GLOGE("Check upsert filter failed. %d", errCode); + return errCode; + } + + if (!CheckDocument(document, errCode)) { + GLOGE("Check upsert document failed. %d", errCode); + return errCode; + } + + if (flags != GRD_DOC_APPEND && flags != GRD_DOC_REPLACE) { + GLOGE("Check flags invalid."); + return -E_INVALID_ARGS; + } + + std::string docId(idStr.begin(), idStr.end()); + bool isReplace = ((flags & GRD_DOC_REPLACE) == GRD_DOC_REPLACE); + + std::lock_guard lock(dbMutex_); + auto coll = Collection(lowerCaseCollName, executor_); + errCode = coll.UpsertDocument(docId, document, isReplace); + if (errCode == E_OK) { + errCode = 1; // upsert one record. + } + return errCode; +} + +int DocumentStore::InsertDocument(const std::string &collection, const std::string &document, int flag) +{ + if (flag != 0) { + GLOGE("InsertDocument flag is not zero"); + return -E_INVALID_ARGS; + } + std::string lowerCaseCollName; + int errCode = E_OK; + if (!CheckCommon::CheckCollectionName(collection, lowerCaseCollName, errCode)) { + GLOGE("Check collection name invalid. %d", errCode); + return errCode; + } + auto coll = Collection(collection, executor_); + if (document.length() + 1 > JSON_LENS_MAX) { + GLOGE("document's length is larger than JSON_LENS_MAX"); + return -E_OVER_LIMIT; + } + JsonObject documentObj = JsonObject::Parse(document, errCode, caseIsSensitive); + if (errCode != E_OK) { + GLOGE("Document Parsed faild"); + return errCode; + } + errCode = CheckCommon::CheckDocument(documentObj); + if (errCode != E_OK) { + return errCode; + } + auto documentObjChild = documentObj.GetChild(); + auto idValue = JsonCommon::GetValueByFiled(documentObjChild, KEY_ID); + std::string id = idValue.GetStringValue(); + Key key(id.begin(), id.end()); + Value value(document.begin(), document.end()); + std::lock_guard lock(dbMutex_); + return coll.PutDocument(key, value); +} + +int DocumentStore::DeleteDocument(const std::string &collection, const std::string &filter, int flag) +{ + if (flag != 0) { + GLOGE("DeleteDocument flag is not zero"); + return -E_INVALID_ARGS; + } + std::string lowerCaseCollName; + int errCode = E_OK; + if (!CheckCommon::CheckCollectionName(collection, lowerCaseCollName, errCode)) { + GLOGE("Check collection name invalid. %d", errCode); + return errCode; + } + auto coll = Collection(collection, executor_); + if (filter.empty()) { + GLOGE("Filter is empty"); + return -E_INVALID_ARGS; + } + if (filter.length() + 1 > JSON_LENS_MAX) { + GLOGE("filter's length is larger than JSON_LENS_MAX"); + return -E_OVER_LIMIT; + } + JsonObject filterObj = JsonObject::Parse(filter, errCode, caseIsSensitive); + if (errCode != E_OK) { + GLOGE("filter Parsed faild"); + return errCode; + } + errCode = CheckCommon::CheckFilter(filterObj); + if (errCode != E_OK) { + return errCode; + } + auto filterObjChild = filterObj.GetChild(); + auto idValue = JsonCommon::GetValueByFiled(filterObjChild, KEY_ID); + std::string id = idValue.GetStringValue(); + Key key(id.begin(), id.end()); + std::lock_guard lock(dbMutex_); + return coll.DeleteDocument(key); +} +KvStoreExecutor *DocumentStore::GetExecutor(int errCode) +{ + return executor_; +} +int DocumentStore::FindDocument(const std::string &collection, const std::string &filter, const std::string &projection, + int flags, GRD_ResultSet *grdResultSet) +{ + if (flags != 0 && flags != GRD_DOC_ID_DISPLAY) { + GLOGE("FindDocument flag is illegal"); + return -E_INVALID_ARGS;; + } + std::string lowerCaseCollName; + int errCode = E_OK; + if (!CheckCommon::CheckCollectionName(collection, lowerCaseCollName, errCode)) { + GLOGE("Check collection name invalid. %d", errCode); + return errCode; + } + if (filter.length() + 1 > JSON_LENS_MAX) { + GLOGE("filter's length is larger than JSON_LENS_MAX"); + return -E_OVER_LIMIT; + } + JsonObject filterObj = JsonObject::Parse(filter, errCode, caseIsSensitive); + if (errCode != E_OK) { + GLOGE("filter Parsed faild"); + return errCode; + } + errCode = CheckCommon::CheckFilter(filterObj); + if (errCode != E_OK) { + return errCode; + } + auto filterObjChild = filterObj.GetChild(); + auto idValue = JsonCommon::GetValueByFiled(filterObjChild, KEY_ID); + if (projection.length() + 1 > JSON_LENS_MAX) { + GLOGE("projection's length is larger than JSON_LENS_MAX"); + return -E_OVER_LIMIT; + } + JsonObject projectionObj = JsonObject::Parse(projection, errCode, caseIsSensitive); + if (errCode != E_OK) { + GLOGE("projection Parsed faild"); + return errCode; + } + bool viewType = false; + std::vector> allPath; + if (projection != "{}") { + allPath = JsonCommon::ParsePath(projectionObj); + if (!CheckCommon::CheckProjection(projectionObj, allPath)) { + GLOGE("projection format unvalid"); + return -E_INVALID_ARGS; + } + if (GetViewType(projectionObj, viewType) != E_OK) { + GLOGE("GetViewType faild"); + return -E_INVALID_ARGS; + } + } + bool ifShowId = false; + if (flags == GRD_DOC_ID_DISPLAY) { + ifShowId = true; + } + if (collections_.find(collection) != collections_.end()) { + GLOGE("DB is resource busy"); + return -E_RESOURCE_BUSY; + } + auto coll = Collection(collection, executor_); + std::lock_guard lock(dbMutex_); + if (!coll.FindDocument()) { + GLOGE("no corresponding table name"); + return -E_INVALID_ARGS; + } + int ret = InitResultSet(this, collection, idValue, allPath, ifShowId, viewType, grdResultSet->resultSet_); + if (ret == E_OK) { + collections_[collection] = nullptr; + } + if (ret != E_OK) { + collections_.erase(collection); + } + return ret; +} + +int DocumentStore::EraseCollection(const std::string collectionName) { + if (collections_.find(collectionName) != collections_.end()) { + collections_.erase(collectionName); + return E_OK; + } + GLOGE("erase collection failed"); + return E_OK; +} + +int DocumentStore::GetViewType(JsonObject &jsonObj, bool &viewType) { + auto leafValue = JsonCommon::GetLeafValue(jsonObj); + if (leafValue.size() == 0) { + return E_INVALID_ARGS; + } + for (size_t i = 0; i < leafValue.size(); i++) { + switch (leafValue[i].GetValueType()) { + case ValueObject::ValueType::VALUE_BOOL: + if (leafValue[i].GetBoolValue()) { + if (i != 0 && !viewType) { + return -E_INVALID_ARGS; + } + viewType = true; + } + else { + if (i != 0 && viewType) { + return E_INVALID_ARGS; + } + viewType = false; + } + break; + case ValueObject::ValueType::VALUE_STRING: + if (leafValue[i].GetStringValue() == "") { + if (i != 0 && !viewType) { + return -E_INVALID_ARGS; + } + viewType = true; + } + else { + return -E_INVALID_ARGS; + } + break; + case ValueObject::ValueType::VALUE_NUMBER: + if (leafValue[i].GetIntValue() == 0) { + if (i != 0 && viewType) { + return -E_INVALID_ARGS; + } + viewType = false; + } + else { + if (i != 0 && !viewType) { + return E_INVALID_ARGS; + } + viewType = true; + } + break; + default: + return E_INVALID_ARGS; + } + } + return E_OK; +} +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store_manager.cpp new file mode 100644 index 00000000..f4f2faac --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store_manager.cpp @@ -0,0 +1,142 @@ +/* +* 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. +*/ + +#include "db_config.h" +#include "document_store_manager.h" +#include "doc_errno.h" +#include "grd_base/grd_type_export.h" +#include "kv_store_manager.h" +#include "log_print.h" +#include "os_api.h" + +namespace DocumentDB { +namespace { +bool CheckDBOpenFlag(unsigned int flag) +{ + unsigned int mask = ~(GRD_DB_OPEN_CREATE | GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK); + unsigned int invalidOpt = (GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK); + return ((flag & mask) == 0x00) && ((flag & invalidOpt) != invalidOpt); +} + +bool CheckDBCloseFlag(unsigned int flag) +{ + return (flag == GRD_DB_CLOSE) || (flag == GRD_DB_CLOSE_IGNORE_ERROR); +} + +bool CheckDBCreate(unsigned int flags, const std::string &path) +{ + if ((flags & GRD_DB_OPEN_CREATE) == 0 && !OSAPI::CheckPathExistence(path)) { + return false; + } + return true; +} +} + +int DocumentStoreManager::GetDocumentStore(const std::string &path, const std::string &config, unsigned int flags, + DocumentStore *&store) +{ + std::string canonicalPath; + std::string dbName; + int errCode = E_OK; + if (!CheckDBPath(path, canonicalPath, dbName, errCode)) { + GLOGE("Check document db file path failed."); + return errCode; + } + + DBConfig dbConfig = DBConfig::ReadConfig(config, errCode); + if (errCode != E_OK) { + GLOGE("Read db config str failed. %d", errCode); + return errCode; + } + + if (!CheckDBOpenFlag(flags)) { + GLOGE("Check document db open flags failed."); + return -E_INVALID_ARGS; + } + + if (!CheckDBCreate(flags, path)) { + GLOGE("Open db failed, file no exists."); + return -E_INVALID_ARGS; + } + + KvStoreExecutor *executor = nullptr; + errCode = KvStoreManager::GetKvStore(canonicalPath + "/" + dbName, dbConfig, executor); + if (errCode != E_OK) { + GLOGE("Open document store failed. %d", errCode); + return errCode; + } + + store = new (std::nothrow) DocumentStore(executor); + if (store == nullptr) { + GLOGE("Memory allocation failed!" ); + return -E_FAILED_MEMORY_ALLOCATE; + } + if (store == nullptr) { + return -E_OUT_OF_MEMORY; + } + + return errCode; +} + +int DocumentStoreManager::CloseDocumentStore(DocumentStore *store, unsigned int flags) +{ + if (!CheckDBCloseFlag(flags)) { + GLOGE("Check document db close flags failed."); + return -E_INVALID_ARGS; + } + + delete store; + return E_OK; +} + +bool DocumentStoreManager::CheckDBPath(const std::string &path, std::string &canonicalPath, std::string &dbName, + int &errCode) +{ + if (path.empty()) { + GLOGE("Invalid path empty"); + errCode = -E_INVALID_ARGS; + return false; + } + + if (path.back() == '/') { + GLOGE("Invalid path end with slash"); + errCode = -E_INVALID_ARGS; + return false; + } + + std::string dirPath; + OSAPI::SplitFilePath(path, dirPath, dbName); + + int innerErrCode = OSAPI::GetRealPath(dirPath, canonicalPath); + if (innerErrCode != E_OK) { + GLOGE("Get real path failed. %d", errCode); + errCode = -E_FILE_OPERATION; + return false; + } + + if (!OSAPI::CheckPermission(canonicalPath)) { + GLOGE("Check path permission failed. %d", errCode); + errCode = -E_FILE_OPERATION; + return false; + } + + return true; +} + +bool DocumentStoreManager::CheckDBConfig(const std::string &config, int &errCode) +{ + return true; +} +} // DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/projection_tree.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/projection_tree.cpp new file mode 100644 index 00000000..edf6f356 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/projection_tree.cpp @@ -0,0 +1,76 @@ +#include +#include "projection_tree.h" + + +namespace DocumentDB { +const int JSON_DEEP_MAX = 4; + +ProjectionTree::ProjectionTree() { +} + +ProjectionTree::~ProjectionTree() { +} + +int ProjectionTree::ParseTree(std::vector> &path) { + ProjectionNode *node = &node_; + if (node == NULL) { + return E_OK; + } + for (int i = 0; i < path.size(); i++) { + node = &node_; + for (int j = 0; j < path[i].size(); j++) { + if (node->SonNode[path[i][j]] != nullptr) { + node = node->SonNode[path[i][j]]; + if (j < path[i].size() - 1 && node->isDeepest) { + return -E_INVALID_ARGS; + } + if (j == path[i].size() - 1 && !node->isDeepest) { + return -E_INVALID_ARGS; + } + } + else { + auto tempNode = new (std::nothrow) ProjectionNode; + if (tempNode == nullptr) { + GLOGE("Memory allocation failed!" ); + return -E_FAILED_MEMORY_ALLOCATE; + } + tempNode->Deep = node->Deep + 1; + if (tempNode->Deep > JSON_DEEP_MAX) { + delete tempNode; + return -E_INVALID_ARGS; + } + node->isDeepest = false; + node->SonNode[path[i][j]] = tempNode; + node = node->SonNode[path[i][j]]; + } + } + } + return E_OK; +} + +bool ProjectionTree::SearchTree(std::vector &singlePath, int &index) { + ProjectionNode *node = &node_; + for (int i = 0; i < singlePath.size(); i++) { + if (node->isDeepest) { + index = i; + } + if (node->SonNode[singlePath[i]] != nullptr) { + node = node->SonNode[singlePath[i]]; + } + else { + return false; + } + } + return true; +} + +int ProjectionNode::DeleteProjectionNode() { + for (auto item : SonNode) { + if (item.second != nullptr) { + delete item.second; + item.second = nullptr; + } + } + return E_OK; +} +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp new file mode 100644 index 00000000..223cee45 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp @@ -0,0 +1,161 @@ +/* +* 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. +*/ +#include "result_set.h" +#include "log_print.h" + +namespace DocumentDB { +constexpr const char *KEY_ID = "_id"; + +ResultSet::ResultSet() +{ + +} +ResultSet::~ResultSet() +{ + +} +int ResultSet::EraseCollection() +{ + if (store_ != nullptr) { + store_->EraseCollection(collectionName_); + } + return E_OK; +} +int ResultSet::Init(DocumentStore *store, const std::string collectionName, ValueObject &key, std::vector> &path, bool ifShowId, bool viewType) +{ + store_ = store; + collectionName_ = collectionName; + key_ = key; + projectionPath_ = path; + if (projectionTree_.ParseTree(path) == -E_INVALID_ARGS) { + GLOGE("Parse ProjectionTree failed"); + return -E_INVALID_ARGS; + } + ifShowId_ = ifShowId; + viewType_ = viewType; + findValue_.reserve(1 + 1); + return E_OK; +} + +int ResultSet::GetNext() +{ + index_++; + if (index_ != 1) { + if (findValue_.size() != 0) { + findValue_.pop_back(); + } + return -E_NO_DATA; + } + std::string idValue = key_.GetStringValue(); + if (idValue.empty()) { + GLOGE("id is empty"); + return -E_NO_DATA; + } + Key key(idValue.begin(), idValue.end()); + Value document; + int errCode = 0; + auto coll = Collection(collectionName_, store_->GetExecutor(errCode)); + errCode = coll.GetDocument(key, document); + if (errCode == -E_NOT_FOUND) { + GLOGE("Cant get value from db"); + return -E_NO_DATA; + } + std::string jsonData(document.begin(), document.end()); + CutJsonBranch(jsonData); + findValue_.emplace_back(jsonData); + return E_OK; +} + +int ResultSet::GetValue(char **value) +{ + if (findValue_.size() == 0) { + GLOGE("The value vector in resultSet is empty"); + return -E_NO_DATA; + } + auto jsonData = findValue_.back(); + char *jsonstr = new char[jsonData.size() + 1]; + if (jsonstr == nullptr) { + GLOGE("Memory allocation failed!" ); + return -E_FAILED_MEMORY_ALLOCATE; + } + errno_t err = strcpy_s(jsonstr, jsonData.size() + 1, jsonData.c_str()); + if (err != 0) { + GLOGE("strcpy_s failed"); + delete[] jsonstr; + return -E_NO_DATA;; + } + *value = jsonstr; + return E_OK; +} + +int ResultSet::CheckCutNode(JsonObject *node, std::vector singlePath, std::vector> &allCutPath) +{ + if (node == nullptr) { + GLOGE("No node to cut"); + return -E_NO_DATA; + } + singlePath.emplace_back(node->GetItemFiled()); + int index = 0; + if (!projectionTree_.SearchTree(singlePath, index) && index == 0) { + allCutPath.emplace_back(singlePath); + } + if (!node->GetChild().IsNull()) { + auto nodeNew = node->GetChild(); + CheckCutNode(&nodeNew, singlePath, allCutPath); + } + if (!node->GetNext().IsNull()) { + singlePath.pop_back(); + auto nodeNew = node->GetNext(); + CheckCutNode(&nodeNew, singlePath, allCutPath); + } + return E_OK; +} +int ResultSet::CutJsonBranch(std::string &jsonData) +{ + int errCode; + JsonObject cjsonObj = JsonObject::Parse(jsonData, errCode, true); + if (errCode != E_OK) { + GLOGE("jsonData Parsed faild"); + return errCode; + } + std::vector> allCutPath; + if (viewType_) { + std::vector singlePath; + auto cjsonObjChild = cjsonObj.GetChild(); + errCode = CheckCutNode(&cjsonObjChild, singlePath, allCutPath); + if (errCode != E_OK) { + GLOGE("The node in CheckCutNode is nullptr"); + return errCode; + } + for (size_t i = 0; i < allCutPath.size(); i++) { + if (!ifShowId_ || allCutPath[i][0] != KEY_ID) { + cjsonObj.DeleteItemDeeplyOnTarget(allCutPath[i]); + } + } + } + if (!viewType_) { + for (size_t i = 0; i < projectionPath_.size(); i++) { + cjsonObj.DeleteItemDeeplyOnTarget(projectionPath_[i]); + } + if (!ifShowId_) { + std::vector idPath; + idPath.emplace_back(KEY_ID); + cjsonObj.DeleteItemDeeplyOnTarget(idPath); + } + } + jsonData = cjsonObj.Print(); + return E_OK; +} +} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set_common.cpp new file mode 100644 index 00000000..ad195a6b --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set_common.cpp @@ -0,0 +1,30 @@ +/* +* 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. +*/ + +#include "doc_errno.h" +#include "grd_base/grd_error.h" +#include "result_set_common.h" +#include +#include + +namespace DocumentDB { +class ValueObject; +int InitResultSet(DocumentStore *store, const std::string collectionName, ValueObject &key, std::vector> &path, bool ifShowId, bool viewType, + ResultSet &resultSet) +{ + return resultSet.Init(store, collectionName, key, path, ifShowId, viewType); +} +} + diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h new file mode 100644 index 00000000..12510f1d --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h @@ -0,0 +1,113 @@ +/* +* 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 JSON_OBJECT_H +#define JSON_OBJECT_H + +#include +#include +#include +#include + +#include "cJSON.h" + +namespace DocumentDB { +class ValueObject { +public: + enum class ValueType { + VALUE_NULL = 0, + VALUE_BOOL, + VALUE_NUMBER, + VALUE_STRING, + }; + + ValueObject() = default; + explicit ValueObject(bool val); + explicit ValueObject(double val); + explicit ValueObject(const char *val); + explicit ValueObject(const std::string &val); + + ValueType GetValueType() const; + bool GetBoolValue() const; + int64_t GetIntValue() const; + double GetDoubleValue() const; + std::string GetStringValue() const; + +private: + ValueType valueType = ValueType::VALUE_NULL; + union { + bool boolValue; + double doubleValue; + }; + std::string stringValue; +}; +using JsonFieldPath = std::vector; + +using ResultValue = ValueObject; +using JsonFieldPath = std::vector; + +class JsonObject { +public: + static JsonObject Parse(const std::string &jsonStr, int &errCode, bool caseSensitive = false); + + ~JsonObject (); + + std::string Print() const; + + JsonObject GetObjectItem(const std::string &field, int &errCode); + JsonObject GetArrayItem(int index, int &errCode); + + JsonObject GetNext() const; + JsonObject GetChild() const; + + int DeleteItemFromObject(const std::string &field); + int AddItemToObject(const JsonObject &item); + int AddItemToObject(const std::string &fieldName, const JsonObject &item); + + ValueObject GetItemValue() const; + void SetItemValue(const ValueObject &value) const; + + std::string GetItemFiled() const; + std::string GetItemFiled(int &errCode) const; + + bool IsFieldExists(const JsonFieldPath &jsonPath) const; + JsonObject FindItem(const JsonFieldPath &jsonPath, int &errCode) const; + ValueObject GetObjectByPath(const JsonFieldPath &jsonPath, int &errCode) const; + int DeleteItemOnTarget(const JsonFieldPath &path); + int DeleteItemDeeplyOnTarget(const JsonFieldPath &path); + bool IsNull() const; + int GetDeep(); + enum class Type { + JSON_LEAF, + JSON_OBJECT, + JSON_ARRAY + }; + Type GetType() const; + +private: + JsonObject(); + int Init(const std::string &str); + + int GetDeep(cJSON *cjson); + int CheckNumber(cJSON *cjson, int &errCode); + + cJSON *cjson_ = nullptr; + int jsonDeep_ = 0; + bool isOwner_ = false; + bool caseSensitive_ = false; +}; +} // DocumentDB +#endif // JSON_OBJECT_H + diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h new file mode 100644 index 00000000..0f81f5b1 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h @@ -0,0 +1,40 @@ +/* +* 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 KV_STORE_EXECUTOR_H +#define KV_STORE_EXECUTOR_H + +#include +#include "document_check.h" + +namespace DocumentDB { +class KvStoreExecutor { +public: + virtual ~KvStoreExecutor() = default; + + virtual int PutData(const std::string &collName, const Key &key, const Value &value) = 0; + virtual int GetData(const std::string &collName, const Key &key, Value &value) const = 0; + virtual int DelData(const std::string &collName, const Key &key) = 0; + + virtual int CreateCollection(const std::string &name, bool ignoreExists) = 0; + virtual int DropCollection(const std::string &name, bool ignoreNonExists) = 0; + virtual bool IsCollectionExists(const std::string &name, int &errCode) = 0; + + virtual int GetCollectionOption(const std::string &name, std::string &option) = 0; + virtual int SetCollectionOption(const std::string &name, const std::string &option) = 0; + virtual int CleanCollectionOption(const std::string &name) = 0; +}; +} // DocumentDB +#endif // KV_STORE_EXECUTOR_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_manager.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_manager.h new file mode 100644 index 00000000..c1df1072 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_manager.h @@ -0,0 +1,30 @@ +/* +* 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 KV_STORE_MANAGER_H +#define KV_STORE_MANAGER_H +#include + +#include "db_config.h" +#include "document_check.h" +#include "kv_store_executor.h" + +namespace DocumentDB { +class KvStoreManager { +public: + static int GetKvStore(const std::string &path, const DBConfig &config, KvStoreExecutor *&executor); +}; +} // DocumentDB +#endif // KV_STORE_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp new file mode 100644 index 00000000..3966a72e --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp @@ -0,0 +1,539 @@ +/* +* 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. +*/ + +#include "json_object.h" +#include +#include "doc_errno.h" +#include "log_print.h" + +namespace DocumentDB { + +namespace { +bool IsNumber(const std::string &str) +{ + return std::all_of(str.begin(), str.end(), [](char c) { + return std::isdigit(c); + }); +} +} + +ValueObject::ValueObject(bool val) +{ + valueType = ValueType::VALUE_BOOL; + boolValue = val; +} + +ValueObject::ValueObject(double val) +{ + valueType = ValueType::VALUE_NUMBER; + doubleValue = val; +} + +ValueObject::ValueObject(const char *val) +{ + valueType = ValueType::VALUE_STRING; + stringValue = val; +} + +ValueObject::ValueObject(const std::string &val) +{ + valueType = ValueType::VALUE_STRING; + stringValue = val; +} + +ValueObject::ValueType ValueObject::GetValueType() const +{ + return valueType; +} + +bool ValueObject::GetBoolValue() const +{ + return boolValue; +} + +int64_t ValueObject::GetIntValue() const +{ + return static_cast(doubleValue + 0.5); +} + +double ValueObject::GetDoubleValue() const +{ + return doubleValue; +} + +std::string ValueObject::GetStringValue() const +{ + return stringValue; +} + +JsonObject JsonObject::Parse(const std::string &jsonStr, int &errCode, bool caseSensitive) +{ + JsonObject obj; + errCode = obj.Init(jsonStr); + obj.caseSensitive_ = caseSensitive; + return obj; +} + +JsonObject::JsonObject() +{ + cjson_ = nullptr; +} + +JsonObject::~JsonObject() +{ + if (isOwner_) { + cJSON_Delete(cjson_); + } +} + +bool JsonObject::IsNull() const +{ + if (cjson_ == nullptr) { + return true; + } + return false; +} + +JsonObject::Type JsonObject::GetType() const +{ + if (cjson_->type == cJSON_Object) { + return JsonObject::Type::JSON_OBJECT; + } else if (cjson_->type == cJSON_Array) { + return JsonObject::Type::JSON_ARRAY; + } + return JsonObject::Type::JSON_LEAF; +} +int JsonObject::GetDeep() +{ + if (cjson_ == nullptr) { + GLOGE("cJson is nullptr,deep is 0"); + return 0; + } + if (jsonDeep_ != 0) { + return jsonDeep_; + } + jsonDeep_ = GetDeep(cjson_); + return jsonDeep_; + +} +int JsonObject::GetDeep(cJSON *cjson) +{ + if (cjson->child == nullptr) { + jsonDeep_ = 0; + return 0; // leaf node + } + + int depth = -1; + cJSON *child = cjson->child; + while (child != nullptr) { + depth = std::max(depth, GetDeep(child) + 1); + child = child->next; + } + jsonDeep_ = depth; + return depth; +} + + +int JsonObject::CheckNumber(cJSON *item, int &errCode) +{ + if (item != NULL && cJSON_IsNumber(item)) { + double value = cJSON_GetNumberValue(item); + if (value > __DBL_MAX__ || value < -__DBL_MAX__) { + errCode = E_INVALID_ARGS; + } + } + if (item->child != nullptr) { + return CheckNumber(item->child, errCode); + } + if (item->next != nullptr) { + return CheckNumber(item->next, errCode); + } + return E_OK; +} + +int JsonObject::Init(const std::string &str) +{ + const char *end = NULL; + isOwner_ = true; + cjson_ = cJSON_ParseWithOpts(str.c_str(), &end, true); + if (cjson_ == nullptr) { + GLOGE("Json's format is wrong"); + return -E_INVALID_JSON_FORMAT; + } + + if (cjson_->type != cJSON_Object) { + GLOGE("after Parse,cjson_'s type is not cJSON_Object"); + return -E_INVALID_ARGS; + } + + int ret = 0; + CheckNumber(cjson_, ret); + if (ret == E_INVALID_ARGS) { + GLOGE("Int value is larger than double"); + return -E_INVALID_ARGS; + } + return E_OK; +} + +std::string JsonObject::Print() const +{ + if (cjson_ == nullptr) { + return ""; + } + char *ret = cJSON_PrintUnformatted(cjson_); + std::string str = ret; + cJSON_free(ret); + return str; +} + +JsonObject JsonObject::GetObjectItem(const std::string &field, int &errCode) +{ + if (cjson_ == nullptr || (cjson_->type & cJSON_Object) != cJSON_Object) { + errCode = -E_INVALID_ARGS; + return JsonObject(); + } + + JsonObject item; + item.caseSensitive_ = caseSensitive_; + if (caseSensitive_) { + item.cjson_ = cJSON_GetObjectItemCaseSensitive(cjson_, field.c_str()); + } else { + item.cjson_ = cJSON_GetObjectItem(cjson_, field.c_str()); + } + if (item.cjson_ == nullptr) { + errCode = -E_NOT_FOUND; + } + return item; +} + +JsonObject JsonObject::GetArrayItem(int index, int &errCode) +{ + if (cjson_ == nullptr || (cjson_->type & cJSON_Array) != cJSON_Array) { + errCode = -E_INVALID_ARGS; + return JsonObject(); + } + + JsonObject item; + item.caseSensitive_ = caseSensitive_; + item.cjson_ = cJSON_GetArrayItem(cjson_, index); + if (item.cjson_ == nullptr) { + errCode = -E_NOT_FOUND; + } + return item; +} + +JsonObject JsonObject::GetNext() const +{ + if (cjson_ == nullptr) { + return JsonObject(); + } + JsonObject next; + next.caseSensitive_ = caseSensitive_; + if (cjson_->next == nullptr) { + return JsonObject(); + } + next.cjson_ = cjson_->next; + return next; +} + +JsonObject JsonObject::GetChild() const +{ + if (cjson_ == nullptr) { + return JsonObject(); + } + JsonObject child; + child.caseSensitive_ = caseSensitive_; + if (cjson_->child == nullptr) { + return JsonObject(); + } + child.cjson_ = cjson_->child; + return child; +} + +int JsonObject::DeleteItemFromObject(const std::string &field) +{ + if (field.empty()) { + return E_OK; + } + cJSON_DeleteItemFromObjectCaseSensitive(cjson_, field.c_str()); + return E_OK; +} + +int JsonObject::AddItemToObject(const JsonObject &item) +{ + if (item.IsNull()) { + GLOGD("Add null object."); + return E_OK; + } + + cJSON *cpoyItem = cJSON_Duplicate(item.cjson_, true); + cJSON_AddItemToObject(cjson_, item.GetItemFiled().c_str(), cpoyItem); + return E_OK; +} + +int JsonObject::AddItemToObject(const std::string &fieldName, const JsonObject &item) +{ + if (item.IsNull()) { + GLOGD("Add null object."); + return E_OK; + } + // TODO: check item exist + if (cjson_->type == cJSON_Array) { + int n = 0; + cJSON *child = cjson_->child; + while (child != nullptr) { + child = child->next; + n++; + } + if (IsNumber(fieldName) && n <= std::stoi(fieldName)) { + GLOGE("Add item object to array over size."); + return -E_DATA_CONFLICT; + } + } + cJSON *cpoyItem = cJSON_Duplicate(item.cjson_, true); + cJSON_AddItemToObject(cjson_, fieldName.c_str(), cpoyItem); + return E_OK; +} + +ValueObject JsonObject::GetItemValue() const +{ + if (cjson_ == nullptr) { + return ValueObject(); + } + + ValueObject value; + switch (cjson_->type) { + case cJSON_False: + case cJSON_True: + return ValueObject(cjson_->type == cJSON_True); + case cJSON_NULL: + return ValueObject(); + case cJSON_Number: + return ValueObject(cjson_->valuedouble); + case cJSON_String: + return ValueObject(cjson_->valuestring); + case cJSON_Array: + case cJSON_Object: + default: + GLOGW("Invalid json type: %d", cjson_->type); + break; + } + + return value; +} + +void JsonObject::SetItemValue(const ValueObject &value) const +{ + if (cjson_ == nullptr) { + return; + } + switch(value.GetValueType()) { + case ValueObject::ValueType::VALUE_NUMBER: + cJSON_SetNumberValue(cjson_, value.GetDoubleValue()); + break; + case ValueObject::ValueType::VALUE_STRING: + cJSON_SetValuestring(cjson_, value.GetStringValue().c_str()); + break; + default: + break; + } +} + +std::string JsonObject::GetItemFiled() const +{ + if (cjson_ == nullptr) { + return ""; + } + + if (cjson_->string == nullptr) { + cJSON *tail = cjson_; + while(tail->next != nullptr) { + tail = tail->next; + } + + int index = 0; + cJSON *head = cjson_; + while (head->prev != tail) { + head = head->prev; + index++; + } + return std::to_string(index); + } else { + return cjson_->string; + } +} + +std::string JsonObject::GetItemFiled(int &errCode) const +{ + if (cjson_ == nullptr) { + errCode = E_INVALID_ARGS; + return ""; + } + if (cjson_->string == nullptr) { + errCode = E_INVALID_ARGS; + return ""; + } + errCode = E_OK; + return cjson_->string; +} + +cJSON *GetChild(cJSON *cjson, const std::string &field, bool caseSens) +{ + if (cjson->type == cJSON_Object) { + if (caseSens) { + return cJSON_GetObjectItemCaseSensitive(cjson, field.c_str()); + } else { + return cJSON_GetObjectItem(cjson, field.c_str()); + } + } else if (cjson->type == cJSON_Array) { + if (!IsNumber(field)) { + GLOGW("Invalid json field path, expect array index."); + return nullptr; + } + return cJSON_GetArrayItem(cjson, std::stoi(field)); + } + + GLOGW("Invalid json field type, expect object or array."); + return nullptr; +} + +cJSON *MoveToPath(cJSON *cjson, const JsonFieldPath &jsonPath, bool caseSens) +{ + for (const auto &field : jsonPath) { + cjson = GetChild(cjson, field, caseSens); + if (cjson == nullptr) { + break; + } + } + return cjson; +} + +bool JsonObject::IsFieldExists(const JsonFieldPath &jsonPath) const +{ + return (MoveToPath(cjson_, jsonPath, caseSensitive_) != nullptr); +} + +JsonObject JsonObject::FindItem(const JsonFieldPath &jsonPath, int &errCode) const +{ + if (jsonPath.empty()) { + JsonObject curr = JsonObject(); + curr.cjson_ = cjson_; + curr.caseSensitive_ = caseSensitive_; + curr.isOwner_ = false; + GLOGW("Path empty, return current object"); + return curr; + } + + cJSON *findItem = MoveToPath(cjson_, jsonPath, caseSensitive_); + if (findItem == nullptr) { + GLOGE("Find item failed. json field path not found."); + errCode = -E_JSON_PATH_NOT_EXISTS; + return {}; + } + + JsonObject item; + item.caseSensitive_ = caseSensitive_; + item.cjson_ = findItem; + return item; +} + +ValueObject JsonObject::GetObjectByPath(const JsonFieldPath &jsonPath, int &errCode) const +{ + JsonObject objGot = FindItem(jsonPath, errCode); + if (errCode != E_OK) { + GLOGE("Get json value object failed. %d", errCode); + return {}; + } + return objGot.GetItemValue(); +} + +int JsonObject::DeleteItemOnTarget(const JsonFieldPath &path) +{ + if (path.empty()) { + return -E_INVALID_ARGS; + } + + std::string fieldName = path.back(); + JsonFieldPath patherPath = path; + patherPath.pop_back(); + + cJSON *nodeFather = MoveToPath(cjson_, patherPath, caseSensitive_); + if (nodeFather == nullptr) { + GLOGE("Delete item failed, json field path not found."); + return -E_JSON_PATH_NOT_EXISTS; + } + + if (nodeFather->type == cJSON_Object) { + if (caseSensitive_) { + cJSON_DeleteItemFromObjectCaseSensitive(nodeFather, fieldName.c_str()); + } else { + cJSON_DeleteItemFromObject(nodeFather, fieldName.c_str()); + } + } else if (nodeFather->type == cJSON_Array) { + if (!IsNumber(fieldName)) { + GLOGW("Invalid json field path, expect array index."); + return -E_JSON_PATH_NOT_EXISTS; + } + cJSON_DeleteItemFromArray(nodeFather, std::stoi(fieldName)); + } + + return E_OK; +} + +int JsonObject::DeleteItemDeeplyOnTarget(const JsonFieldPath &path) +{ + if (path.empty()) { + return -E_INVALID_ARGS; + } + + std::string fieldName = path.back(); + JsonFieldPath patherPath = path; + patherPath.pop_back(); + + cJSON *nodeFather = MoveToPath(cjson_, patherPath, caseSensitive_); + if (nodeFather == nullptr) { + GLOGE("Delete item failed, json field path not found."); + return -E_JSON_PATH_NOT_EXISTS; + } + + if (nodeFather->type == cJSON_Object) { + if (caseSensitive_) { + cJSON_DeleteItemFromObjectCaseSensitive(nodeFather, fieldName.c_str()); + if (nodeFather->child == nullptr && path.size() > 1) { + JsonFieldPath fatherPath(path.begin(), path.end() - 1); + DeleteItemDeeplyOnTarget(fatherPath); + } + } else { + cJSON_DeleteItemFromObject(nodeFather, fieldName.c_str()); + if (nodeFather->child == nullptr && path.size() > 1) { + JsonFieldPath fatherPath(path.begin(), path.end() - 1); + DeleteItemDeeplyOnTarget(fatherPath); + } + } + } else if (nodeFather->type == cJSON_Array) { + if (!IsNumber(fieldName)) { + GLOGW("Invalid json field path, expect array index."); + return -E_JSON_PATH_NOT_EXISTS; + } + cJSON_DeleteItemFromArray(nodeFather, std::stoi(fieldName)); + if (nodeFather->child == nullptr && path.size() > 1) { + JsonFieldPath fatherPath(path.begin(), path.end() - 1); + DeleteItemDeeplyOnTarget(fatherPath); + } + } + + return E_OK; +} +} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/kv_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/kv_store_manager.cpp new file mode 100644 index 00000000..ac45c871 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/kv_store_manager.cpp @@ -0,0 +1,69 @@ +/* +* 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. +*/ + +#include "doc_errno.h" +#include "kv_store_manager.h" +#include "log_print.h" +#include "sqlite_store_executor_impl.h" +#include "sqlite_utils.h" + +namespace DocumentDB { +int KvStoreManager::GetKvStore(const std::string &path, const DBConfig &config, KvStoreExecutor *&executor) +{ + if (executor != nullptr) { + return -E_INVALID_ARGS; + } + + sqlite3 *db = nullptr; + int errCode = SqliteStoreExecutor::CreateDatabase(path, config, db); + if (errCode != E_OK) { + GLOGE("Get kv store failed. %d", errCode); + return errCode; + } + + auto *sqliteExecutor = new (std::nothrow) SqliteStoreExecutor(db); + if (sqliteExecutor == nullptr) { + sqlite3_close_v2(db); + return -E_OUT_OF_MEMORY; + } + + std::string oriConfigStr; + errCode = sqliteExecutor->GetDBConfig(oriConfigStr); + if (errCode == -E_NOT_FOUND) { + errCode = sqliteExecutor->SetDBConfig(config.ToString()); + } else if (errCode != E_OK) { + goto END; + } else { + DBConfig oriDbConfig = DBConfig::ReadConfig(oriConfigStr, errCode); + if (errCode != E_OK) { + GLOGE("Read db config failed. %d", errCode); + goto END; + } + if (config != oriDbConfig) { + errCode = -E_INVALID_CONFIG_VALUE; + GLOGE("Get kv store failed, db config changed. %d", errCode); + goto END; + } + } + + executor = sqliteExecutor; + return E_OK; + +END: + delete sqliteExecutor; + sqliteExecutor = nullptr; + return errCode; +} +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp new file mode 100644 index 00000000..937a2793 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -0,0 +1,260 @@ +/* +* 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. +*/ + +#include "document_check.h" +#include "doc_errno.h" +#include "log_print.h" +#include "sqlite_utils.h" +#include "sqlite_store_executor_impl.h" + +namespace DocumentDB { +int SqliteStoreExecutor::CreateDatabase(const std::string &path, const DBConfig &config, sqlite3 *&db) +{ + if (db != nullptr) { + return -E_INVALID_ARGS; + } + + int errCode = SQLiteUtils::CreateDataBase(path, 0, db); + if (errCode != E_OK || db == nullptr) { + GLOGE("Open or create database failed. %d", errCode); + return errCode; + } + + std::string pageSizeSql = "PRAGMA page_size=" + std::to_string(config.GetPageSize() * 1024); + errCode = SQLiteUtils::ExecSql(db, pageSizeSql); + if (errCode != E_OK) { + GLOGE("Set db page size failed. %d", errCode); + goto END; + } + + errCode = SQLiteUtils::ExecSql(db, "CREATE TABLE IF NOT EXISTS grd_meta (key BLOB PRIMARY KEY, value BLOB);"); + if (errCode != E_OK) { + GLOGE("Create meta table failed. %d", errCode); + goto END; + } + + return E_OK; + +END: + sqlite3_close_v2(db); + db = nullptr; + return errCode; +} + +SqliteStoreExecutor::SqliteStoreExecutor(sqlite3 *handle) : dbHandle_(handle) +{ +} + +SqliteStoreExecutor::~SqliteStoreExecutor() +{ + sqlite3_close_v2(dbHandle_); + dbHandle_ = nullptr; +} + +int SqliteStoreExecutor::GetDBConfig(std::string &config) +{ + std::string dbConfigKeyStr = "DB_CONFIG"; + Key dbConfigKey = {dbConfigKeyStr.begin(), dbConfigKeyStr.end()}; + Value dbConfigVal; + int errCode = GetData("grd_meta", dbConfigKey, dbConfigVal); + config.assign(dbConfigVal.begin(), dbConfigVal.end()); + return errCode; +} + +int SqliteStoreExecutor::SetDBConfig(const std::string &config) +{ + std::string dbConfigKeyStr = "DB_CONFIG"; + Key dbConfigKey = {dbConfigKeyStr.begin(), dbConfigKeyStr.end()}; + Value dbConfigVal = {config.begin(), config.end()}; + return PutData("grd_meta", dbConfigKey, dbConfigVal); +} + +int SqliteStoreExecutor::PutData(const std::string &collName, const Key &key, const Value &value) +{ + if (dbHandle_ == nullptr) { + return -E_ERROR; + } + + std::string sql = "INSERT OR REPLACE INTO '" + collName + "' VALUES (?,?);"; + int errCode = SQLiteUtils::ExecSql(dbHandle_, sql, [key, value](sqlite3_stmt *stmt) { + SQLiteUtils::BindBlobToStatement(stmt, 1, key); + SQLiteUtils::BindBlobToStatement(stmt, 2, value); + return E_OK; + }, nullptr); + if (errCode != SQLITE_OK) { + GLOGE("[sqlite executor] Put data failed. err=%d", errCode); + if (errCode == -E_ERROR) { + GLOGE("Cant find the collection"); + return -E_INVALID_ARGS; + } + return errCode; + } + return E_OK; +} + +int SqliteStoreExecutor::GetData(const std::string &collName, const Key &key, Value &value) const +{ + if (dbHandle_ == nullptr) { + GLOGE("Invalid db handle."); + return -E_ERROR; + } + int innerErrorCode = -E_NOT_FOUND; + std::string sql = "SELECT value FROM '" + collName + "' WHERE key=?;"; + int errCode = SQLiteUtils::ExecSql(dbHandle_, sql, [key](sqlite3_stmt *stmt) { + SQLiteUtils::BindBlobToStatement(stmt, 1, key); + return E_OK; + }, [&value, &innerErrorCode](sqlite3_stmt *stmt) { + SQLiteUtils::GetColumnBlobValue(stmt, 0, value); + innerErrorCode = E_OK; + return E_OK; + }); + if (errCode != SQLITE_OK) { + GLOGE("[sqlite executor] Get data failed. err=%d", errCode); + return errCode; + } + return innerErrorCode; +} + +int SqliteStoreExecutor::DelData(const std::string &collName, const Key &key) +{ + if (dbHandle_ == nullptr) { + GLOGE("Invalid db handle."); + return -E_ERROR; + } + int errCode = 0; + if (!IsCollectionExists(collName, errCode)) { + return -E_INVALID_ARGS; + } + Value valueRet; + if (GetData(collName, key, valueRet) != E_OK) { + return -E_NO_DATA; + } + std::string sql = "DELETE FROM '" + collName + "' WHERE key=?;"; + errCode = SQLiteUtils::ExecSql(dbHandle_, sql, [key](sqlite3_stmt *stmt) { + SQLiteUtils::BindBlobToStatement(stmt, 1, key); + return E_OK; + }, nullptr); + + if (errCode != SQLITE_OK) { + GLOGE("[sqlite executor] Delete data failed. err=%d", errCode); + if (errCode == -E_ERROR) { + GLOGE("Cant find the collection"); + return -E_NO_DATA; + } + } + return errCode; +} + +int SqliteStoreExecutor::CreateCollection(const std::string &name, bool ignoreExists) +{ + if (dbHandle_ == nullptr) { + return -E_ERROR; + } + std::string collName = COLL_PREFIX + name; + if (!ignoreExists) { + int errCode = E_OK; + bool isExists = IsCollectionExists(collName, errCode); + if (errCode != E_OK) { + return errCode; + } + if (isExists) { + GLOGE("[sqlite executor] Create collectoin failed, collection already exists."); + return -E_COLLECTION_CONFLICT; + } + } + + std::string sql = "CREATE TABLE IF NOT EXISTS '" + collName + "' (key BLOB PRIMARY KEY, value BLOB);"; + int errCode = SQLiteUtils::ExecSql(dbHandle_, sql); + if (errCode != SQLITE_OK) { + GLOGE("[sqlite executor] Create collectoin failed. err=%d", errCode); + return errCode; + } + return E_OK; +} + +int SqliteStoreExecutor::DropCollection(const std::string &name, bool ignoreNonExists) +{ + if (dbHandle_ == nullptr) { + return -E_ERROR; + } + + std::string collName = COLL_PREFIX + name; + if (!ignoreNonExists) { + int errCode = E_OK; + bool isExists = IsCollectionExists(collName, errCode); + if (errCode != E_OK) { + return errCode; + } + if (!isExists) { + GLOGE("[sqlite executor] Drop collectoin failed, collection not exists."); + return -E_NO_DATA; + } + } + + std::string sql = "DROP TABLE IF EXISTS '" + collName + "';"; + int errCode = SQLiteUtils::ExecSql(dbHandle_, sql); + if (errCode != SQLITE_OK) { + GLOGE("[sqlite executor] Drop collectoin failed. err=%d", errCode); + return errCode; + } + return E_OK; +} + +bool SqliteStoreExecutor::IsCollectionExists(const std::string &name, int &errCode) +{ + bool isExists = false; + std::string sql = "SELECT tbl_name FROM sqlite_master WHERE tbl_name=?;"; + + errCode = SQLiteUtils::ExecSql(dbHandle_, sql, [name](sqlite3_stmt *stmt) { + SQLiteUtils::BindTextToStatement(stmt, 1, name); + return E_OK; + }, [&isExists](sqlite3_stmt *stmt) { + isExists = true; + return E_OK; + }); + + if (errCode != E_OK) { + GLOGE("Check collection exist failed. %d", errCode); + } + + return isExists; +} + +int SqliteStoreExecutor::GetCollectionOption(const std::string &name, std::string &option) +{ + std::string collOptKeyStr = "COLLECTION_OPTION_" + name; + Key collOptKey = {collOptKeyStr.begin(), collOptKeyStr.end()}; + Value collOptVal; + int errCode = GetData("grd_meta", collOptKey, collOptVal); + option.assign(collOptVal.begin(), collOptVal.end()); + return errCode; +} + +int SqliteStoreExecutor::SetCollectionOption(const std::string &name, const std::string &option) +{ + std::string collOptKeyStr = "COLLECTION_OPTION_" + name; + Key collOptKey = {collOptKeyStr.begin(), collOptKeyStr.end()}; + Value collOptVal = {option.begin(), option.end()}; + return PutData("grd_meta", collOptKey, collOptVal); +} + +int SqliteStoreExecutor::CleanCollectionOption(const std::string &name) +{ + std::string collOptKeyStr = "COLLECTION_OPTION_" + name; + Key collOptKey = {collOptKeyStr.begin(), collOptKeyStr.end()}; + return DelData("grd_meta", collOptKey); +} + +} // DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h new file mode 100644 index 00000000..bc6857b8 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h @@ -0,0 +1,50 @@ +/* +* 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 SQLITE_STORE_EXECUTOR_IMPL_H +#define SQLITE_STORE_EXECUTOR_IMPL_H + +#include "db_config.h" +#include "kv_store_executor.h" +#include "sqlite3.h" + +namespace DocumentDB { +class SqliteStoreExecutor : public KvStoreExecutor { +public: + static int CreateDatabase(const std::string &path, const DBConfig &config, sqlite3 *&db); + + SqliteStoreExecutor(sqlite3 *handle); + ~SqliteStoreExecutor() override; + + int GetDBConfig(std::string &config); + int SetDBConfig(const std::string &config); + + int PutData(const std::string &collName, const Key &key, const Value &value) override; + int GetData(const std::string &collName, const Key &key, Value &value) const override; + int DelData(const std::string &collName, const Key &key) override; + + int CreateCollection(const std::string &name, bool ignoreExists) override; + int DropCollection(const std::string &name, bool ignoreNonExists) override; + bool IsCollectionExists(const std::string &name, int &errCode) override; + + int GetCollectionOption(const std::string &name, std::string &option) override; + int SetCollectionOption(const std::string &name, const std::string &option) override; + int CleanCollectionOption(const std::string &name) override; + +private: + sqlite3 *dbHandle_ = nullptr; +}; +} // DocumentDB +#endif // SQLITE_STORE_EXECUTOR_IMPL_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.cpp new file mode 100644 index 00000000..bb86791f --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.cpp @@ -0,0 +1,305 @@ +/* + * 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. + */ +#include "sqlite_utils.h" + +#include +#include "doc_errno.h" +#include "log_print.h" + +namespace DocumentDB { +const int MAX_BLOB_READ_SIZE = 5 * 1024 * 1024; // 5M limit +const int MAX_TEXT_READ_SIZE = 5 * 1024 * 1024; // 5M limit +const int BUSY_TIMEOUT_MS = 3000; // 3000ms for sqlite busy timeout. +const std::string BEGIN_SQL = "BEGIN TRANSACTION"; +const std::string BEGIN_IMMEDIATE_SQL = "BEGIN IMMEDIATE TRANSACTION"; +const std::string COMMIT_SQL = "COMMIT TRANSACTION"; +const std::string ROLLBACK_SQL = "ROLLBACK TRANSACTION"; + +namespace { +int MapSqliteError(int errCode) +{ + switch (errCode) { + case SQLITE_OK: + return E_OK; + case SQLITE_PERM: + case SQLITE_CANTOPEN: + case SQLITE_READONLY: + return -E_FILE_OPERATION; + default: + return -E_ERROR; + } +} + +std::mutex g_logConfigMutex; +bool g_configLog = false; +} + +void SQLiteUtils::SqliteLogCallback(void *data, int err, const char *msg) +{ + GLOGD("[SQLite] err=%d sys=%d %s msg=%s", err, errno, sqlite3_errstr(err), msg); +} + +int SQLiteUtils::CreateDataBase(const std::string &path, int flag, sqlite3 *&db) +{ + { + std::lock_guard lock(g_logConfigMutex); + if (!g_configLog) { + sqlite3_config(SQLITE_CONFIG_LOG, &SqliteLogCallback, nullptr); + g_configLog = true; + } + } + + int errCode = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr); + if (errCode != SQLITE_OK) { + GLOGE("Open database [%s] failed. %d", path.c_str(), errCode); + if (db != nullptr) { + (void)sqlite3_close_v2(db); + db = nullptr; + } + return MapSqliteError(errCode); + } + + errCode = sqlite3_busy_timeout(db, BUSY_TIMEOUT_MS); + if (errCode != SQLITE_OK) { + GLOGE("Set busy timeout failed:%d", errCode); + } + return MapSqliteError(errCode); +} + +int SQLiteUtils::GetStatement(sqlite3 *db, const std::string &sql, sqlite3_stmt *&statement) +{ + if (db == nullptr) { + GLOGE("Invalid db for get statement"); + return -E_INVALID_ARGS; + } + + // Prepare the new statement only when the input parameter is not null + if (statement != nullptr) { + return E_OK; + } + int errCode = sqlite3_prepare_v2(db, sql.c_str(), -1, &statement, nullptr); + if (errCode != SQLITE_OK) { + GLOGE("Prepare SQLite statement failed:%d", errCode); + (void)SQLiteUtils::ResetStatement(statement, true); + return errCode; + } + + if (statement == nullptr) { + return -E_ERROR; + } + + return E_OK; +} + +int SQLiteUtils::StepWithRetry(sqlite3_stmt *statement) +{ + if (statement == nullptr) { + return -E_INVALID_ARGS; + } + + int errCode = sqlite3_step(statement); + if (errCode != SQLITE_DONE && errCode != SQLITE_ROW) { + GLOGE("[SQLiteUtils] Step error:%d, sys:%d", errCode, errno); + } + + return errCode; +} + +int SQLiteUtils::ResetStatement(sqlite3_stmt *&statement, bool finalize) +{ + if (statement == nullptr) { + return -E_INVALID_ARGS; + } + + int errCode = E_OK; + if (!finalize) { + errCode = sqlite3_reset(statement); + if (errCode != SQLITE_OK) { + GLOGE("[SQLiteUtils] reset statement error:%d, sys:%d", errCode, errno); + goto FINALIZE; + } + + (void)sqlite3_clear_bindings(statement); + return errCode; + } + +FINALIZE: + int finalizeResult = sqlite3_finalize(statement); + if (finalizeResult != SQLITE_OK) { + GLOGE("[SQLiteUtils] finalize statement error:%d, sys:%d", finalizeResult, errno); + } + statement = nullptr; + return (errCode == SQLITE_OK ? finalizeResult : errCode); +} + +int SQLiteUtils::BindBlobToStatement(sqlite3_stmt *statement, int index, const std::vector &value) +{ + if (statement == nullptr) { + return -E_INVALID_ARGS; + } + + int errCode; + if (value.empty()) { + errCode = sqlite3_bind_zeroblob(statement, index, -1); // -1 for zero-length blob. + } else { + errCode = sqlite3_bind_blob(statement, index, static_cast(value.data()), + value.size(), SQLITE_TRANSIENT); + } + + if (errCode != SQLITE_OK) { + GLOGE("[SQLiteUtil][Bind blob] Failed to bind the value:%d", errCode); + } + return errCode; +} + +int SQLiteUtils::GetColumnBlobValue(sqlite3_stmt *statement, int index, std::vector &value) +{ + if (statement == nullptr) { + return -E_INVALID_ARGS; + } + + int keySize = sqlite3_column_bytes(statement, index); + if (keySize < 0 || keySize > MAX_BLOB_READ_SIZE) { + GLOGW("[SQLiteUtils][Column blob] size over limit:%d", keySize); + value.resize(MAX_BLOB_READ_SIZE + 1); // Reset value size to invalid + return E_OK; // Return OK for continue get data, but value is invalid + } + + auto keyRead = static_cast(sqlite3_column_blob(statement, index)); + if (keySize == 0 || keyRead == nullptr) { + value.resize(0); + } else { + value.resize(keySize); + value.assign(keyRead, keyRead + keySize); + } + + return E_OK; +} + +int SQLiteUtils::BindTextToStatement(sqlite3_stmt *statement, int index, const std::string &value) +{ + if (statement == nullptr) { + return -E_INVALID_ARGS; + } + + int errCode = sqlite3_bind_text(statement, index, value.c_str(), value.length(), SQLITE_TRANSIENT); + if (errCode != SQLITE_OK) { + GLOGE("[SQLiteUtil][Bind text]Failed to bind the value:%d", errCode); + return errCode; + } + + return E_OK; +} + +int SQLiteUtils::GetColumnTextValue(sqlite3_stmt *statement, int index, std::string &value) +{ + if (statement == nullptr) { + return -E_INVALID_ARGS; + } + + int valSize = sqlite3_column_bytes(statement, index); + if (valSize < 0 || valSize > MAX_TEXT_READ_SIZE) { + GLOGW("[SQLiteUtils][Column text] size over limit:%d", valSize); + value.resize(MAX_TEXT_READ_SIZE + 1); // Reset value size to invalid + return E_OK; // Return OK for continue get data, but value is invalid + } + + const unsigned char *val = sqlite3_column_text(statement, index); + if (valSize == 0 || val == nullptr) { + value = {}; + } else { + value = std::string(reinterpret_cast(val)); + } + + return E_OK; +} + +int SQLiteUtils::BeginTransaction(sqlite3 *db, TransactType type) +{ + if (type == TransactType::IMMEDIATE) { + return ExecSql(db, BEGIN_IMMEDIATE_SQL); + } + + return ExecSql(db, BEGIN_SQL); +} + +int SQLiteUtils::CommitTransaction(sqlite3 *db) +{ + return ExecSql(db, COMMIT_SQL); +} + +int SQLiteUtils::RollbackTransaction(sqlite3 *db) +{ + return ExecSql(db, ROLLBACK_SQL); +} + +int SQLiteUtils::ExecSql(sqlite3 *db, const std::string &sql) +{ + if (db == nullptr || sql.empty()) { + return -E_INVALID_ARGS; + } + + char *errMsg = nullptr; + int errCode = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errMsg); + if (errCode != SQLITE_OK && errMsg != nullptr) { + GLOGE("Execute sql failed. %d err: %s", errCode, errMsg); + } + + sqlite3_free(errMsg); + return MapSqliteError(errCode); +} + +int SQLiteUtils::ExecSql(sqlite3 *db, const std::string &sql, const std::function &bindCallback, + const std::function &resultCallback) +{ + if (db == nullptr || sql.empty()) { + return -E_INVALID_ARGS; + } + bool bindFinish = true; + sqlite3_stmt *stmt = nullptr; + int errCode = SQLiteUtils::GetStatement(db, sql, stmt); + if (errCode != E_OK) { + goto END; + } + + do { + if (bindCallback) { + errCode = bindCallback(stmt); + if (errCode != E_OK && errCode != -E_UNFINISHED) { + goto END; + } + bindFinish = (errCode != -E_UNFINISHED); // continue bind if unfinished + } + + while (true) { + errCode = SQLiteUtils::StepWithRetry(stmt); + if (errCode == SQLITE_DONE) { + errCode = E_OK; // Step finished + break; + } else if (errCode != SQLITE_ROW) { + goto END; // Step return error + } + if (resultCallback != nullptr && ((errCode = resultCallback(stmt)) != E_OK)) { + goto END; + } + } + errCode = SQLiteUtils::ResetStatement(stmt, false); + } while (!bindFinish); + +END: + (void)SQLiteUtils::ResetStatement(stmt, true); + return MapSqliteError(errCode); +} +} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.h new file mode 100644 index 00000000..597a576e --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.h @@ -0,0 +1,57 @@ +/* + * 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 SQLITE_UTILS_H +#define SQLITE_UTILS_H + +#include +#include +#include + +#include "sqlite3sym.h" + +namespace DocumentDB { +enum class TransactType { + DEFERRED, + IMMEDIATE, +}; + +class SQLiteUtils { +public: + static int CreateDataBase(const std::string &path, int flag, sqlite3 *&db); + + static int GetStatement(sqlite3 *db, const std::string &sql, sqlite3_stmt *&statement); + static int StepWithRetry(sqlite3_stmt *statement); + static int ResetStatement(sqlite3_stmt *&statement, bool finalize); + + static int BindBlobToStatement(sqlite3_stmt *statement, int index, const std::vector &value); + static int GetColumnBlobValue(sqlite3_stmt *statement, int index, std::vector &value); + + static int BindTextToStatement(sqlite3_stmt *statement, int index, const std::string &value); + static int GetColumnTextValue(sqlite3_stmt *statement, int index, std::string &value); + + static int BeginTransaction(sqlite3 *db, TransactType type = TransactType::DEFERRED); + static int CommitTransaction(sqlite3 *db); + static int RollbackTransaction(sqlite3 *db); + + static int ExecSql(sqlite3 *db, const std::string &sql); + static int ExecSql(sqlite3 *db, const std::string &sql, const std::function &bindCallback, + const std::function &resultCallback); + +private: + static void SqliteLogCallback(void *data, int err, const char *msg); +}; +} // namespace DocumentDB +#endif // SQLITE_UTILS_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/BUILD.gn b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/BUILD.gn new file mode 100644 index 00000000..51d0a307 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/BUILD.gn @@ -0,0 +1,164 @@ +# Copyright (c) 2021 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. +import("//build/test.gni") + +module_output_path = "datamgr_service/documentdb" + +############################################################################### +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "../../include", + "../../src/common/include", + "../../src/executor/include", + "../../src/executor/document", + "../../src/oh_adapter/include", + "../../src/oh_adapter/src", + "../../src/interface/include", + + "common", + ] + + defines = [ + "SQLITE_ENABLE_SNAPSHOT", + "SQLITE_HAS_CODEC", + "SQLITE_ENABLE_JSON1", + "USING_HILOG_LOGGER", + "USE_SQLITE_SYMBOLS", + "SQLITE_ENABLE_DROPTABLE_CALLBACK", + ] +} + +############################################################################### +ohos_source_set("src_file") { + testonly = true + + sources = [ + "../../src/common/src/collection_option.cpp", + "../../src/common/src/db_config.cpp", + "../../src/common/src/json_common.cpp", + "../../src/common/src/log_print.cpp", + "../../src/common/src/os_api.cpp", + "../../src/executor/base/grd_db_api.cpp", + "../../src/executor/document/document_check.cpp", + "../../src/executor/document/grd_document_api.cpp", + "../../src/executor/document/grd_resultset_api.cpp", + "../../src/interface/src/collection.cpp", + "../../src/interface/src/doc_errno.cpp", + "../../src/interface/src/document_store.cpp", + "../../src/interface/src/document_store_manager.cpp", + "../../src/interface/src/projection_tree.cpp", + "../../src/interface/src/result_set.cpp", + "../../src/interface/src/result_set_common.cpp", + "../../src/oh_adapter/src/json_object.cpp", + "../../src/oh_adapter/src/kv_store_manager.cpp", + "../../src/oh_adapter/src/sqlite_store_executor_impl.cpp", + "../../src/oh_adapter/src/sqlite_utils.cpp", + + "common/documentdb_test_utils.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/googletest:gtest_main", + "//third_party/sqlite:sqlite", + ] + + configs += [ "//third_party/cJSON:cJSON_config" ] + ldflags = [ "-Wl,--exclude-libs,ALL" ] + deps += [ "//third_party/cJSON:cjson" ] + external_deps = [ + "c_utils:utils", + "hisysevent_native:libhisysevent", + "hitrace_native:hitrace_meter", + "hiviewdfx_hilog_native:libhilog", + ] + part_name = "kv_store" +} + +template("documentdb_unittest") { + ohos_unittest(target_name) { + forward_variables_from(invoker, "*") + module_out_path = module_output_path + if (!defined(deps)) { + deps = [] + } + if (!defined(external_deps)) { + external_deps = [] + } + configs = [ ":module_private_config" ] + deps += [ + ":src_file", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//third_party/sqlite:sqlite", + ] + configs += [ "//third_party/cJSON:cJSON_config" ] + ldflags = [ "-Wl,--exclude-libs,ALL" ] + deps += [ + "//third_party/cJSON:cjson", + "//third_party/openssl:libcrypto_shared", + ] + external_deps = [ + "c_utils:utils", + "hisysevent_native:libhisysevent", + "hitrace_native:hitrace_meter", + "hiviewdfx_hilog_native:libhilog", + ] + } +} + +documentdb_unittest("DocumentDBApiTest") { + sources = [ "api/documentdb_api_test.cpp" ] +} + +documentdb_unittest("DocumentDBCollectionTest") { + sources = [ "api/documentdb_collection_test.cpp" ] +} + +documentdb_unittest("DocumentDBDataTest") { + sources = [ "api/documentdb_data_test.cpp" ] +} + +documentdb_unittest("DocumentDBJsonCommonTest") { + sources = [ "oh_adapter/documentdb_json_common_test.cpp" ] +} + +documentdb_unittest("DocumentDBJsonObjectTest") { + sources = [ "oh_adapter/documentdb_jsonobject_test.cpp" ] +} + +############################################################################### +group("unittest") { + testonly = true + deps = [ "//third_party/googletest:gmock" ] + + deps += [ + ":DocumentDBApiTest", + ":DocumentDBCollectionTest", + ":DocumentDBDataTest", + ":DocumentDBJsonCommonTest", + ":DocumentDBJsonObjectTest", + ] +} + +############################################################################### + +group("document_fuzztest") { + testonly = true + deps = [] + deps += [] +} +############################################################################### diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_api_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_api_test.cpp new file mode 100644 index 00000000..c442be61 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_api_test.cpp @@ -0,0 +1,657 @@ +/* +* 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. +*/ + +#include + +#include "doc_errno.h" +#include "documentdb_test_utils.h" +#include "log_print.h" +#include "grd_base/grd_db_api.h" +#include "grd_base/grd_error.h" +#include "grd_document/grd_document_api.h" +#include "sqlite_utils.h" + +using namespace DocumentDB; +using namespace testing::ext; +using namespace DocumentDBUnitTest; + +class DocumentDBApiTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DocumentDBApiTest::SetUpTestCase(void) +{ +} + +void DocumentDBApiTest::TearDownTestCase(void) +{ +} + +void DocumentDBApiTest::SetUp(void) +{ +} + +void DocumentDBApiTest::TearDown(void) +{ + DocumentDBTestUtils::RemoveTestDbFiles("./document.db"); +} + +/** + * @tc.name: OpenDBTest001 + * @tc.desc: Test open document db + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBTest001, TestSize.Level0) +{ + std::string path = "./document.db"; + GRD_DB *db = nullptr; + int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + EXPECT_NE(db, nullptr); + GLOGD("Open DB test 001: status: %d", status); + + EXPECT_EQ(GRD_CreateCollection(db, "student", "", 0), GRD_OK); + + EXPECT_EQ(GRD_UpsertDoc(db, "student", R""({"_id":"10001"})"", R""({"name":"Tom","age":23})"", 0), 1); + EXPECT_EQ(GRD_UpsertDoc(db, "student", R""({"_id":"10001"})"", R""({"name":"Tom","age":23})"", 0), 1); + + EXPECT_EQ(GRD_DropCollection(db, "student", 0), GRD_OK); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + DocumentDBTestUtils::RemoveTestDbFiles(path); +} + +/** + * @tc.name: OpenDBTest002 + * @tc.desc: Test open document db with invalid db + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBTest002, TestSize.Level0) +{ + std::string path = "./document.db"; + int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, nullptr); + EXPECT_EQ(status, GRD_INVALID_ARGS); + + GRD_DB *db = nullptr; + status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + EXPECT_NE(db, nullptr); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + + status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); +} + +/** + * @tc.name: OpenDBPathTest001 + * @tc.desc: Test open document db with NULL path + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBPathTest001, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::vector invalidPath = { + nullptr, + "", + "/a/b/c/" + }; + for (auto path : invalidPath) { + GLOGD("OpenDBPathTest001: open db with path: %s", path); + int status = GRD_DBOpen(path, nullptr, GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_INVALID_ARGS); + } +} + +/** + * @tc.name: OpenDBPathTest002 + * @tc.desc: Test open document db with file no permission + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBPathTest002, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string pathNoPerm = "/root/document.db"; + int status = GRD_DBOpen(pathNoPerm.c_str(), nullptr, GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_FAILED_FILE_OPERATION); +} + +/** + * @tc.name: OpenDBConfigTest001 + * @tc.desc: Test open document db with invalid config option + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigTest001, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + const int MAX_JSON_LEN = 512 * 1024; + std::string configStr = std::string(MAX_JSON_LEN, 'a'); + int status = GRD_DBOpen(path.c_str(), configStr.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OVER_LIMIT); +} + +/** + * @tc.name: OpenDBConfigTest002 + * @tc.desc: Test open document db with invalid config format + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigTest002, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + int status = GRD_DBOpen(path.c_str(), "{aa}", GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_INVALID_FORMAT); +} + +/** + * @tc.name: OpenDBConfigTest003 + * @tc.desc: Test open document db with config not support + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigTest003, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + int status = GRD_DBOpen(path.c_str(), R""({"notSupport":123})"", GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_INVALID_ARGS); +} + +/** + * @tc.name: OpenDBConfigMaxConnNumTest001 + * @tc.desc: Test open document db with invalid config item maxConnNum + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest001, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + + std::vector configList = { + R""({"maxConnNum":0})"", + R""({"maxConnNum":15})"", + R""({"maxConnNum":1025})"", + R""({"maxConnNum":1000000007})"", + R""({"maxConnNum":"16"})"", + R""({"maxConnNum":{"value":17}})"", + R""({"maxConnNum":[16,17,18]})"", + }; + for (const auto &config : configList) { + GLOGD("OpenDBConfigMaxConnNumTest001: test with config:%s", config.c_str()); + int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); + ASSERT_EQ(status, GRD_INVALID_ARGS); + } +} + +/** + * @tc.name: OpenDBConfigMaxConnNumTest002 + * @tc.desc: Test open document db with valid item maxConnNum + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest002, TestSize.Level1) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + + for (int i = 16; i <= 1024; i++) { + std::string config = "{\"maxConnNum\":" + std::to_string(i) + "}"; + int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + ASSERT_NE(db, nullptr); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + DocumentDBTestUtils::RemoveTestDbFiles(path); + } +} + +/** + * @tc.name: OpenDBConfigMaxConnNumTest003 + * @tc.desc: Test reopen document db with different maxConnNum + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest003, TestSize.Level1) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + + std::string config = R""({"maxConnNum":16})""; + int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + config = R""({"maxConnNum":17})""; + status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_INVALID_ARGS); + + DocumentDBTestUtils::RemoveTestDbFiles(path); +} + +/** + * @tc.name: OpenDBConfigMaxConnNumTest004 + * @tc.desc: Test open document db over maxConnNum + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest004, TestSize.Level1) +{ + std::string path= "./document.db"; + + int maxCnt = 16; + std::string config = "{\"maxConnNum\":" + std::to_string(maxCnt) + "}"; + + std::vector dbList; + while (maxCnt--) { + GRD_DB *db = nullptr; + int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + EXPECT_NE(db, nullptr); + dbList.push_back(db); + } + + GRD_DB *db = nullptr; + int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + EXPECT_NE(db, nullptr); + dbList.push_back(db); + + for (auto *it : dbList) { + status = GRD_DBClose(it, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + } + + DocumentDBTestUtils::RemoveTestDbFiles(path); +} + +/** + * @tc.name: OpenDBConfigPageSizeTest001 + * @tc.desc: Test open document db with invalid config item pageSize + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest001, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + + std::vector configList = { + R""({"pageSize":0})"", + R""({"pageSize":5})"", + R""({"pageSize":48})"", + R""({"pageSize":1000000007})"", + R""({"pageSize":"4"})"", + R""({"pageSize":{"value":8}})"", + R""({"pageSize":[16,32,64]})"", + }; + for (const auto &config : configList) { + GLOGD("OpenDBConfigPageSizeTest001: test with config:%s", config.c_str()); + int status = GRD_DBOpen(path.c_str(), config.c_str(), 0, &db); + EXPECT_EQ(status, GRD_INVALID_ARGS); + } +} + +namespace { +int GetDBPageSize(const std::string &path) +{ + sqlite3 *db = nullptr; + int ret = SQLiteUtils::CreateDataBase(path, 0, db); + EXPECT_EQ(ret, E_OK); + if (db == nullptr) { + return 0; + } + + int pageSize = 0; + SQLiteUtils::ExecSql(db, "PRAGMA page_size;", nullptr, [&pageSize](sqlite3_stmt *stmt) { + pageSize = sqlite3_column_int(stmt, 0); + return E_OK; + }); + + sqlite3_close_v2(db); + return pageSize; +} +} + +/** + * @tc.name: OpenDBConfigPageSizeTest002 + * @tc.desc: Test open document db with valid config item pageSize + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest002, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + + for (int size : {4, 8, 16, 32, 64}) { + std::string config = "{\"pageSize\":" + std::to_string(size) + "}"; + int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + EXPECT_EQ(GetDBPageSize(path), size * 1024); + DocumentDBTestUtils::RemoveTestDbFiles(path); + } +} + +/** + * @tc.name: OpenDBConfigPageSizeTest003 + * @tc.desc: Test reopen document db with different pageSize + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest003, TestSize.Level1) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + + std::string config = R""({"pageSize":4})""; + int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + config = R""({"pageSize":8})""; + status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_INVALID_ARGS); + + DocumentDBTestUtils::RemoveTestDbFiles(path); +} + +/** + * @tc.name: OpenDBConfigRedoFlushTest001 + * @tc.desc: Test open document db with valid config item redoFlushByTrx + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigRedoFlushTest001, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + + for (int flush : {0, 1}) { + std::string config = "{\"redoFlushByTrx\":" + std::to_string(flush) + "}"; + int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + DocumentDBTestUtils::RemoveTestDbFiles(path); + } +} + +/** + * @tc.name: OpenDBConfigXXXTest001 + * @tc.desc: Test open document db with invalid config item redoFlushByTrx + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigRedoFlushTest002, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + + std::string config = R""({"redoFlushByTrx":3})""; + int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_INVALID_ARGS); +} + +/** + * @tc.name: OpenDBFlagTest001 + * @tc.desc: Test open document db with invalid flag + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBFlagTest001, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + std::vector invaldFlag = { + GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK, + GRD_DB_OPEN_CREATE | GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK, + 0x08, + 0xffff, + UINT32_MAX + }; + for (unsigned int flag : invaldFlag) { + GLOGD("OpenDBFlagTest001: open doc db with flag %u", flag); + int status = GRD_DBOpen(path.c_str(), "", flag, &db); + EXPECT_EQ(status, GRD_INVALID_ARGS); + } +} + +/** + * @tc.name: OpenDBFlagTest002 + * @tc.desc: Test open document db with valid flag + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBFlagTest002, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_ONLY, &db); + EXPECT_EQ(status, GRD_INVALID_ARGS); + + status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_ONLY, &db); + EXPECT_EQ(status, GRD_OK); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CHECK_FOR_ABNORMAL, &db); + EXPECT_EQ(status, GRD_OK); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CHECK, &db); + EXPECT_EQ(status, GRD_OK); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + DocumentDBTestUtils::RemoveTestDbFiles(path); +} + +/** + * @tc.name: CloseDBTest001 + * @tc.desc: Test close document db with invalid db + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, CloseDBTest001, TestSize.Level0) +{ + GRD_DB *db = nullptr; + int status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_INVALID_ARGS); + + status = GRD_DBClose(db, GRD_DB_CLOSE_IGNORE_ERROR); + EXPECT_EQ(status, GRD_INVALID_ARGS); + db = nullptr; +} + +/** + * @tc.name: CloseDBFlagTest001 + * @tc.desc: Test close document db with valid flag + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, CloseDBFlagTest001, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + ASSERT_NE(db, nullptr); + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + DocumentDBTestUtils::RemoveTestDbFiles(path); +} + +/** + * @tc.name: CloseDBFlagTest002 + * @tc.desc: Test close document db with valid flag + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, CloseDBFlagTest002, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + ASSERT_NE(db, nullptr); + + // TODO: open result set + + status = GRD_DBClose(db, GRD_DB_CLOSE_IGNORE_ERROR); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + DocumentDBTestUtils::RemoveTestDbFiles(path); +} + +/** + * @tc.name: CloseDBFlagTest003 + * @tc.desc: Test close document db with invalid flag + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, CloseDBFlagTest003, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string path= "./document.db"; + int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + ASSERT_NE(db, nullptr); + + std::vector invaldFlag = { + 0x02, + 0x03, + 0xffff, + UINT32_MAX + }; + for (unsigned int flag : invaldFlag) { + GLOGD("CloseDBFlagTest003: close doc db with flag %u", flag); + status = GRD_DBClose(db, flag); + EXPECT_EQ(status, GRD_INVALID_ARGS); + } + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + DocumentDBTestUtils::RemoveTestDbFiles(path); +} + +/** + * @tc.name: FlushDBTest001 + * @tc.desc: Test flush document db + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, FlushDBTest001, TestSize.Level0) +{ + EXPECT_EQ(GRD_Flush(nullptr, GRD_DB_FLUSH_ASYNC), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_Flush(nullptr, GRD_DB_FLUSH_SYNC), GRD_INVALID_ARGS); + + GRD_DB *db = nullptr; + std::string path= "./document.db"; + int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + ASSERT_NE(db, nullptr); + + EXPECT_EQ(GRD_Flush(db, GRD_DB_FLUSH_ASYNC), GRD_OK); + EXPECT_EQ(GRD_Flush(db, GRD_DB_FLUSH_SYNC), GRD_OK); + std::vector invalidFlags = {2, 4, 8, 512, 1024, UINT32_MAX}; + for (auto flags : invalidFlags) { + EXPECT_EQ(GRD_Flush(db, flags), GRD_INVALID_ARGS); + } + + status = GRD_DBClose(db, GRD_DB_CLOSE); + EXPECT_EQ(status, GRD_OK); + db = nullptr; + + DocumentDBTestUtils::RemoveTestDbFiles(path); +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_collection_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_collection_test.cpp new file mode 100644 index 00000000..a15df4df --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_collection_test.cpp @@ -0,0 +1,220 @@ +/* +* 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. +*/ + +#include + +#include "doc_errno.h" +#include "documentdb_test_utils.h" +#include "log_print.h" +#include "grd_base/grd_db_api.h" +#include "grd_base/grd_error.h" +#include "grd_document/grd_document_api.h" +#include "sqlite_utils.h" + +using namespace DocumentDB; +using namespace testing::ext; +using namespace DocumentDBUnitTest; + +namespace { +std::string g_path = "./document.db"; +GRD_DB *g_db = nullptr; +} + +class DocumentDBCollectionTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DocumentDBCollectionTest::SetUpTestCase(void) +{ +} + +void DocumentDBCollectionTest::TearDownTestCase(void) +{ +} + +void DocumentDBCollectionTest::SetUp(void) +{ + EXPECT_EQ(GRD_DBOpen(g_path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &g_db), GRD_OK); + EXPECT_NE(g_db, nullptr); +} + +void DocumentDBCollectionTest::TearDown(void) +{ + if (g_db != nullptr) { + EXPECT_EQ(GRD_DBClose(g_db, GRD_DB_CLOSE), GRD_OK); + g_db = nullptr; + } + DocumentDBTestUtils::RemoveTestDbFiles(g_path); +} + +/** + * @tc.name: CollectionTest001 + * @tc.desc: Test create collection with null db + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBCollectionTest, CollectionTest001, TestSize.Level0) +{ + EXPECT_EQ(GRD_CreateCollection(nullptr, "student", "", 0), GRD_INVALID_ARGS); +} + +namespace { +const int MAX_COLLECTION_LEN = 512; +} + +/** + * @tc.name: CollectionTest002 + * @tc.desc: Test create/drop collection with invalid collection name + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBCollectionTest, CollectionTest002, TestSize.Level0) +{ + string overLenName(MAX_COLLECTION_LEN, 'a'); + EXPECT_EQ(GRD_CreateCollection(g_db, overLenName.c_str(), "", 0), GRD_OVER_LIMIT); + EXPECT_EQ(GRD_DropCollection(g_db, overLenName.c_str(), 0), GRD_OVER_LIMIT); + + std::vector invalidName = { + nullptr, + "", + }; + + for (auto *it : invalidName) { + GLOGD("CollectionTest002: create collection with name: %s", it); + EXPECT_EQ(GRD_CreateCollection(g_db, it, "", 0), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_DropCollection(g_db, it, 0), GRD_INVALID_ARGS); + } + + std::vector invalidNameFormat = { + "GRD_123", + "grd_123", + "GM_SYS_123", + "gm_sys_123" + }; + + for (auto *it : invalidNameFormat) { + GLOGD("CollectionTest002: create collection with name: %s", it); + EXPECT_EQ(GRD_CreateCollection(g_db, it, "", 0), GRD_INVALID_FORMAT); + EXPECT_EQ(GRD_DropCollection(g_db, it, 0), GRD_INVALID_FORMAT); + } +} + +/** + * @tc.name: CollectionTest003 + * @tc.desc: Test create/drop collection with valid collection name + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBCollectionTest, CollectionTest003, TestSize.Level0) +{ + string overLenName(MAX_COLLECTION_LEN - 1, 'a'); + std::vector validName = { + "123", + "&^%@", + "中文字符", + "sqlite_master", + "NULL", + "SELECT", + overLenName.c_str() + }; + + for (auto *it : validName) { + GLOGD("CollectionTest003: create collection with name: %s", it); + EXPECT_EQ(GRD_CreateCollection(g_db, it, "", 0), GRD_OK); + EXPECT_EQ(GRD_DropCollection(g_db, it, 0), GRD_OK); + } +} + +/** + * @tc.name: CollectionTest004 + * @tc.desc: Test create collection with ignore flag + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBCollectionTest, CollectionTest004, TestSize.Level0) +{ + EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); + EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); + EXPECT_EQ(GRD_CreateCollection(g_db, "Student", "", CHK_EXIST_COLLECTION), GRD_DATA_CONFLICT); +} + +/** + * @tc.name: CollectionTest005 + * @tc.desc: Test create collection with invalid option + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBCollectionTest, CollectionTest005, TestSize.Level0) +{ + EXPECT_EQ(GRD_CreateCollection(g_db, "student", R""({aa})"", 0), GRD_INVALID_FORMAT); + + std::vector invalidOption = { + R""({"maxDoc":0})"", + R""({"maxDoc":"123"})"", + R""({"maxDoc":{"value":1024}})"", + R""({"maxDoc":[1,2,4,8]})"", + R""({"minDoc":1024})"", + }; + + for (auto opt : invalidOption) { + GLOGD("CollectionTest005: create collection with option: %s", opt); + EXPECT_EQ(GRD_CreateCollection(g_db, "student", opt, 0), GRD_INVALID_ARGS); + } +} + +/** + * @tc.name: CollectionTest006 + * @tc.desc: Test create/drop collection with valid flag + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBCollectionTest, CollectionTest006, TestSize.Level0) +{ + EXPECT_EQ(GRD_CreateCollection(g_db, "student", R""({"maxDoc":1024})"", 0), GRD_OK); + + EXPECT_EQ(GRD_CreateCollection(g_db, "student", R""({"maxDoc":2048})"", 0), GRD_INVALID_ARGS); + + EXPECT_EQ(GRD_DropCollection(g_db, "student", 0), GRD_OK); + EXPECT_EQ(GRD_DropCollection(g_db, "student", 0), GRD_OK); + EXPECT_EQ(GRD_DropCollection(g_db, "student", CHK_NON_EXIST_COLLECTION), GRD_NO_DATA); + + // Create collection with different option returnh OK after drop collection + EXPECT_EQ(GRD_CreateCollection(g_db, "student", R""({"maxDoc":2048})"", 0), GRD_OK); +} + +/** + * @tc.name: CollectionTest007 + * @tc.desc: Test create/drop collection with invalid flag + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBCollectionTest, CollectionTest007, TestSize.Level0) +{ + for (int flag : std::vector {2, 4, 8, 1024, UINT32_MAX}) { + EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", flag), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_DropCollection(g_db, "student", flag), GRD_INVALID_ARGS); + } +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp new file mode 100644 index 00000000..228978ba --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp @@ -0,0 +1,318 @@ +/* +* 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. +*/ + +#include + +#include "doc_errno.h" +#include "documentdb_test_utils.h" +#include "log_print.h" +#include "grd_base/grd_db_api.h" +#include "grd_base/grd_error.h" +#include "grd_document/grd_document_api.h" +#include "sqlite_utils.h" + +using namespace DocumentDB; +using namespace testing::ext; +using namespace DocumentDBUnitTest; + +namespace { +std::string g_path = "./document.db"; +GRD_DB *g_db = nullptr; +const char *g_coll = "student"; +} + +class DocumentDBDataTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DocumentDBDataTest::SetUpTestCase(void) +{ +} + +void DocumentDBDataTest::TearDownTestCase(void) +{ +} + +void DocumentDBDataTest::SetUp(void) +{ + EXPECT_EQ(GRD_DBOpen(g_path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &g_db), GRD_OK); + EXPECT_NE(g_db, nullptr); + + EXPECT_EQ(GRD_CreateCollection(g_db, g_coll, "", 0), GRD_OK); +} + +void DocumentDBDataTest::TearDown(void) +{ + if (g_db != nullptr) { + EXPECT_EQ(GRD_DBClose(g_db, GRD_DB_CLOSE), GRD_OK); + g_db = nullptr; + } + DocumentDBTestUtils::RemoveTestDbFiles(g_path); +} + +/** + * @tc.name: UpsertDataTest001 + * @tc.desc: Test upsert data into collection + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpsertDataTest001, TestSize.Level0) +{ + std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, R""({"_id":"1234"})"", document.c_str(), GRD_DOC_REPLACE), 1); + + std::string update = R""({"CC":"AAAA"})""; + EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, R""({"_id":"1234"})"", update.c_str(), 0), 1); + + std::string append = R""({"addr.city":"DDDD"})""; + EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, R""({"_id":"1234"})"", append.c_str(), GRD_DOC_APPEND), 1); +} + +/** + * @tc.name: UpsertDataTest002 + * @tc.desc: Test upsert data with db is nullptr + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpsertDataTest002, TestSize.Level0) +{ + std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + EXPECT_EQ(GRD_UpsertDoc(nullptr, g_coll, "1234", document.c_str(), GRD_DOC_REPLACE), GRD_INVALID_ARGS); +} + +/** + * @tc.name: UpsertDataTest003 + * @tc.desc: Test upsert data with invalid collection name + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpsertDataTest003, TestSize.Level0) +{ + std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::vector> invalidName = { + {nullptr, GRD_INVALID_ARGS}, + {"", GRD_INVALID_ARGS}, + {"GRD_123", GRD_INVALID_FORMAT}, + {"grd_123", GRD_INVALID_FORMAT}, + {"GM_SYS_123", GRD_INVALID_FORMAT}, + {"gm_sys_123", GRD_INVALID_FORMAT}, + }; + for (auto it : invalidName) { + GLOGD("UpsertDataTest003: upsert data with collectionname: %s", it.first); + EXPECT_EQ(GRD_UpsertDoc(g_db, it.first, "1234", document.c_str(), GRD_DOC_REPLACE), it.second); + } +} + +HWTEST_F(DocumentDBDataTest, UpsertDataTest004, TestSize.Level0) +{ + std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::vector invalidFilter = { + nullptr, + "", + R""({"name":"Tmono"})"", + R""({"value":{"_id":"1234"}})"", + R""({"_id":1234})"", + }; + for (auto filter : invalidFilter) { + GLOGD("UpsertDataTest004: upsert data with filter: %s", filter); + EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter, document.c_str(), GRD_DOC_REPLACE), GRD_INVALID_ARGS); + } +} + +HWTEST_F(DocumentDBDataTest, UpsertDataTest005, TestSize.Level0) +{ + std::string filter = R""({"_id":"1234"})""; + std::vector> invalidDocument = { + {"", GRD_INVALID_ARGS}, + {nullptr, GRD_INVALID_ARGS}, + {R""({invalidJsonFormat})"", GRD_INVALID_FORMAT}, + }; + for (auto it : invalidDocument) { + GLOGD("UpsertDataTest005: upsert data with document: %s", it.first); + EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), it.first, GRD_DOC_REPLACE), it.second); + } +} + +/** + * @tc.name: UpsertDataTest006 + * @tc.desc: Test upsert data with invalid flags + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpsertDataTest006, TestSize.Level0) +{ + std::string filter = R""({"_id":"1234"})""; + std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + + for (auto flags : std::vector {2, 4, 8, 64, 1024, UINT32_MAX}) { + EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), flags), GRD_INVALID_ARGS); + } +} + +/** + * @tc.name: UpsertDataTest007 + * @tc.desc: Test upsert data with collection not create + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpsertDataTest007, TestSize.Level0) +{ + std::string filter = R""({"_id":"1234"})""; + std::string val = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + EXPECT_EQ(GRD_UpsertDoc(g_db, "collection_not_exists", filter.c_str(), val.c_str(), GRD_DOC_REPLACE), GRD_NO_DATA); +} + +/** + * @tc.name: UpsertDataTest008 + * @tc.desc: Test upsert data with different document in append + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpsertDataTest008, TestSize.Level0) +{ + std::string filter = R""({"_id":"1234"})""; + std::string document = R""({"name":"Tmn","age":18,"addr":{"city":"shanghai","postal":200001}})""; + EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), GRD_DOC_REPLACE), 1); + + std::string updateDoc = R""({"name":"Xue","case":2,"age":28,"addr":{"city":"shenzhen","postal":518000}})""; + EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), updateDoc.c_str(), GRD_DOC_APPEND), 1); +} + +/** + * @tc.name: UpdateDataTest001 + * @tc.desc: + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpdateDataTest001, TestSize.Level0) +{ + std::string filter = R""({"_id":"1234"})""; + std::string updateDoc = R""({"name":"Xue"})""; + EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), updateDoc.c_str(), 0), GRD_NO_DATA); +} + +/** + * @tc.name: UpdateDataTest002 + * @tc.desc: Test update data with db is nullptr + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpdateDataTest002, TestSize.Level0) +{ + std::string filter = R""({"_id":"1234"})""; + std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + EXPECT_EQ(GRD_UpdateDoc(nullptr, g_coll, filter.c_str(), document.c_str(), 0), GRD_INVALID_ARGS); +} + +/** + * @tc.name: UpdateDataTest003 + * @tc.desc: Test update data with invalid collection name + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpdateDataTest003, TestSize.Level0) +{ + std::string filter = R""({"_id":"1234"})""; + std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::vector> invalidName = { + {nullptr, GRD_INVALID_ARGS}, + {"", GRD_INVALID_ARGS}, + {"GRD_123", GRD_INVALID_FORMAT}, + {"grd_123", GRD_INVALID_FORMAT}, + {"GM_SYS_123", GRD_INVALID_FORMAT}, + {"gm_sys_123", GRD_INVALID_FORMAT}, + }; + for (auto it : invalidName) { + GLOGD("UpdateDataTest003: update data with collectionname: %s", it.first); + EXPECT_EQ(GRD_UpdateDoc(g_db, it.first, filter.c_str(), document.c_str(), 0), it.second); + } +} + +/** + * @tc.name: UpdateDataTest004 + * @tc.desc: Test update data with invalid filter + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpdateDataTest004, TestSize.Level0) +{ + std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::vector invalidFilter = { + nullptr, + "", + R""({"name":"Tmono"})"", + R""({"value":{"_id":"1234"}})"", + R""({"_id":1234})"", + }; + for (auto filter : invalidFilter) { + GLOGD("UpdateDataTest004: update data with filter: %s", filter); + EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter, document.c_str(), 0), GRD_INVALID_ARGS); + } +} + +/** + * @tc.name: UpdateDataTest005 + * @tc.desc: Test update data with invalid doc + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpdateDataTest005, TestSize.Level0) +{ + std::string filter = R""({"_id":"1234"})""; + std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::vector> invalidUpdate = { + {"", GRD_INVALID_ARGS}, + {nullptr, GRD_INVALID_ARGS}, + {R""({invalidJsonFormat})"", GRD_INVALID_FORMAT}, + }; + + for (auto it : invalidUpdate) { + GLOGD("UpdateDataTest005: update data with doc: %s", it.first); + EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), it.first, 0), it.second); + } +} + +/** + * @tc.name: UpdateDataTest006 + * @tc.desc: Test update data with invalid flag + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpdateDataTest006, TestSize.Level0) +{ + std::string filter = R""({"_id":"1234"})""; + std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::vector invalidFlags = {1, 2, 4, 8, 1024, UINT32_MAX}; + for (auto flag : invalidFlags) { + GLOGD("UpdateDataTest006: update data with flag: %u", flag); + EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), document.c_str(), flag), GRD_INVALID_ARGS); + } +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_delete_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_delete_test.cpp new file mode 100644 index 00000000..305a1f5a --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_delete_test.cpp @@ -0,0 +1,326 @@ +/* +* 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. +*/ + +#include + +#include "grd_base/grd_db_api.h" +#include "grd_document/grd_document_api.h" +#include "grd_base/grd_error.h" +#include "grd_base/grd_type_export.h" +#include "grd_type_inner.h" +#include "grd_resultset_inner.h" +#include "grd_base/grd_resultset_api.h" + +using namespace testing::ext; +namespace { +constexpr const char *COLLECTION_NAME = "student"; +constexpr const char *NULL_JSON_STR = "{}"; +const int MAX_COLLECTION_LENS = 511; +std::string path = "./document.db"; +GRD_DB *g_db = nullptr; +} + +class DocumentDeleteApiTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + void InsertDoc(const char *collectionName, const char *document); +}; +void DocumentDeleteApiTest::SetUpTestCase(void) +{ + std::string path = "./document.db"; + int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &g_db); + EXPECT_EQ(status, GRD_OK); +} + +void DocumentDeleteApiTest::TearDownTestCase(void) +{ + EXPECT_EQ(GRD_DBClose(g_db, 0), GRD_OK); + remove(path.c_str()); +} + +void DocumentDeleteApiTest::SetUp(void) +{ + /** + * @tc.steps:step1. Create Collection + * @tc.expected: step1. GRD_OK + */ + EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); + /** + * @tc.steps:step2. Insert many document in order to delete + * @tc.expected: step2. GRD_OK + */ + const char *document1 = + "{ \ + \"_id\" : \"1\", \ + \"name\": \"xiaoming\", \ + \"address\": \"beijing\", \ + \"age\" : 15, \ + \"friend\" : {\"name\" : \"David\", \"sex\" : \"female\", \"age\" : 90}, \ + \"subject\": [\"math\", \"English\", \"music\"] \ + }"; + const char *document2 = + "{ \ + \"_id\" : \"2\", \ + \"name\": \"ori\", \ + \"address\": \"beijing\", \ + \"age\" : 15, \ + \"friend\" : {\"name\" : \"David\", \"sex\" : \"female\", \"age\" : 90}, \ + \"subject\": [\"math\", \"English\", \"music\"] \ + }"; + const char *document3 = + "{ \ + \"_id\" : \"3\", \ + \"name\": \"David\", \ + \"address\": \"beijing\", \ + \"age\" : 15, \ + \"friend\" : {\"name\" : \"David\", \"sex\" : \"female\", \"age\" : 90}, \ + \"subject\": [\"math\", \"English\", \"music\"] \ + }"; + DocumentDeleteApiTest::InsertDoc(COLLECTION_NAME, document1); + DocumentDeleteApiTest::InsertDoc(COLLECTION_NAME, document2); + DocumentDeleteApiTest::InsertDoc(COLLECTION_NAME, document3); +} + +void DocumentDeleteApiTest::TearDown(void) +{ + /** + * @tc.steps:step1. Call GRD_DropCollection to drop the collection + * @tc.expected: step1. GRD_OK + */ + EXPECT_EQ(GRD_DropCollection(g_db, COLLECTION_NAME, 0), GRD_OK); +} + +static void ChkDeleteResWithFilter(const char *filter) +{ + /** + * @tc.steps:step1. Try to find the deleted document + * @tc.expected: step1. GRD_OK + */ + Query query; + query.filter = filter; + const char *projection = "{}"; + query.projection = projection; + GRD_ResultSet *resultSet = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + /** + * @tc.steps:step2. The resultset should be NULL + * @tc.expected: step2. GRD_OK + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +void DocumentDeleteApiTest::InsertDoc(const char *collectionName, const char *document) +{ + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, document, 0), GRD_OK); +} + +/** + * @tc.name: DocumentDelete001 + * @tc.desc: Delete with NULL filter + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDeleteApiTest, DeleteDBTest001, TestSize.Level1) +{ + /** + * @tc.steps:step1. Delete all the document + * @tc.expected: step1. GRD_INVALID_ARGS + */ + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, NULL_JSON_STR, 0), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentDelete002 + * @tc.desc: Delete with filter which has no _id + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDeleteApiTest, DeleteDBTest002, TestSize.Level1) +{ + /** + * @tc.steps:step1. Delete with filter which has no _id + * @tc.expected: step1. GRD_INVALID_ARGS + */ + const char *filter = "{\"age\" : 15}"; + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentDelete003 + * @tc.desc: Delete with filter which has more than one fileds. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDeleteApiTest, DeleteDBTest003, TestSize.Level1) +{ + /** + * @tc.steps:step1. Delete with filter which has more than one fileds. + * @tc.expected: step1. GRD_INVALID_ARGS + */ + const char *filter = "{\"_id\" : \"1\", \"age\" : 15}"; + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentDelete004 + * @tc.desc: Test delete with invalid input + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDeleteApiTest, DeleteDBTest004, TestSize.Level1) +{ + /** + * @tc.steps:step1. Test delete with un-zero flags + * @tc.expected: step1. GRD_INVALID_ARGS + */ + const char *filter1 = "{\"_id\" : \"1\"}"; + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter1, 1), GRD_INVALID_ARGS); + /** + * @tc.steps:step2. Test delete with NULL collection name + * @tc.expected: step2. GRD_INVALID_ARGS + */ + // const char *filter2 = "{\"_id\" : \"1\"}"; + // EXPECT_EQ(GRD_DeleteDoc(g_db, NULL, filter2, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step3. Test delete with empty collection name + * @tc.expected: step3. GRD_INVALID_ARGS + */ + // const char *filter1 = "{\"_id\" : \"1\"}"; + // EXPECT_EQ(GRD_DeleteDoc(g_db, "", filter1, 1), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentDelete005 + * @tc.desc: Test delete with same collection name + * but one is uppercase(delete) and the other is lowercase(insert) + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDeleteApiTest, DeleteDBTest005, TestSize.Level1) +{ + /** + * @tc.step1: Test delete with same collection name + * but one is uppercase(delete) and the other is lowercase(insert) + * @tc.expected: step1. GRD_INVALID_ARGS + */ + const char *filter = "{\"_id\" : \"1\"}"; + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), 1); + /** + * @tc.step2: Check whether doc has been deleted compeletely + * @tc.expected: step2. GRD_OK + */ + ChkDeleteResWithFilter(filter); +} + +/** + * @tc.name: DocumentDelete006 + * @tc.desc: Test delete after calling find interface + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDeleteApiTest, DeleteDBTest006, TestSize.Level1) +{ + /** + * @tc.step1: Create filter with _id and get the record according to filter condition. + * @tc.expected: step1. GRD_OK + */ + const char *filter = "{\"_id\" : \"1\"}"; + GRD_ResultSet *resultSet = nullptr; + Query query = {filter, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), 1); + /** + * @tc.step2: Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDelete007 + * @tc.desc: Test delete with too long collectionName. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDeleteApiTest, DeleteDBTest007, TestSize.Level1) +{ + const char *filter = "{\"_id\" : \"1\"}"; + string collectionName1(MAX_COLLECTION_LENS, 'a'); + EXPECT_EQ(GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0), GRD_OK); + EXPECT_EQ(GRD_DeleteDoc(g_db, collectionName1.c_str(), filter, 0), 0); + EXPECT_EQ(GRD_DropCollection(g_db, collectionName1.c_str(), 0), GRD_OK); + + string collectionName2(MAX_COLLECTION_LENS + 1, 'a'); + EXPECT_EQ(GRD_DeleteDoc(g_db, collectionName2.c_str(), filter, 0), GRD_OVER_LIMIT); +} + + +/** + * @tc.name: DocumentDelete008 + * @tc.desc: Test delete with invalid NULL input for all parameters. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDeleteApiTest, DeleteDBTest008, TestSize.Level1) +{ + /** + * @tc.steps:step1. Delete with filter which has more than one fileds. + * @tc.expected: step1. GRD_INVALID_ARGS + */ + const char *filter = "{\"_id\" : \"1\"}"; + EXPECT_EQ(GRD_DeleteDoc(NULL, COLLECTION_NAME, filter, 0), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_DeleteDoc(g_db, NULL, filter, 0), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_DeleteDoc(g_db, "", filter, 0), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, NULL, 0), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, "", 0), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_DeleteDoc(g_db, "notExisted", filter, 0), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentDelete010 + * @tc.desc: Test delete document when filter _id is int and string + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDeleteApiTest, DeleteDBTest010, TestSize.Level1) +{ + /** + * @tc.steps:step1. Test delete document when filter _id is int and string. + * @tc.expected: step1. GRD_INVALID_ARGS + */ + std::vector filterVec = {R"({"_id" : 1})", R"({"_id":[1, 2]})", + R"({"_id" : {"t1" : 1}})", R"({"_id":null})", R"({"_id":true})", R"({"_id" : 1.333})", R"({"_id" : -2.0})"}; + for (const auto &item : filterVec) { + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, item.c_str(), 0), GRD_INVALID_ARGS); + } + const char *filter = "{\"_id\" : \"1\"}"; + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), 1); + +} + diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp new file mode 100644 index 00000000..0051493b --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp @@ -0,0 +1,1449 @@ +/* +* 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. +*/ + +#include + +#include "grd_base/grd_db_api.h" +#include "grd_document/grd_document_api.h" +#include "grd_base/grd_error.h" +#include "grd_base/grd_type_export.h" +#include "grd_type_inner.h" +#include "grd_base/grd_resultset_api.h" +#include "doc_errno.h" +#include "log_print.h" +#include "documentdb_test_utils.h" +#include "grd_resultset_inner.h" + +using namespace testing::ext; +namespace { +std::string path = "./document.db"; +GRD_DB *g_db = nullptr; +constexpr const char *COLLECTION_NAME = "student"; +constexpr const char *NULL_JSON_STR = "{}"; +const int E_OK = 0; +const int MAX_COLLECTION_NAME = 511; +const int INT_MAX = 2147483647; +const int INT_MIN = -2147483648; +const int MAX_ID_LENS = 899; +static const char *g_document1 = "{\"_id\" : \"1\", \"name\":\"doc1\",\"item\":\"journal\",\"personInfo\":\ + {\"school\":\"AB\", \"age\" : 51}}"; +static const char *g_document2 = "{\"_id\" : \"2\", \"name\":\"doc2\",\"item\": 1, \"personInfo\":\ + [1, \"my string\", {\"school\":\"AB\", \"age\" : 51}, true, {\"school\":\"CD\", \"age\" : 15}, false]}"; +static const char *g_document3 = "{\"_id\" : \"3\", \"name\":\"doc3\",\"item\":\"notebook\",\"personInfo\":\ + [{\"school\":\"C\", \"age\" : 5}]}"; +static const char *g_document4 = "{\"_id\" : \"4\", \"name\":\"doc4\",\"item\":\"paper\",\"personInfo\":\ + {\"grade\" : 1, \"school\":\"A\", \"age\" : 18}}"; +static const char *g_document5 = "{\"_id\" : \"5\", \"name\":\"doc5\",\"item\":\"journal\",\"personInfo\":\ + [{\"sex\" : \"woma\", \"school\" : \"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; +static const char *g_document6 = "{\"_id\" : \"6\", \"name\":\"doc6\",\"item\":false,\"personInfo\":\ + [{\"school\":\"B\", \"teacher\" : \"mike\",\"age\" : 15}, {\"school\":\"C\", \"teacher\" : \"moon\",\"age\" : 20}]}"; +static const char *g_document7 = "{\"_id\" : \"7\", \"name\":\"doc7\",\"item\":\"fruit\",\"other_Info\":\ + [{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; +static const char *g_document8 = "{\"_id\" : \"8\", \"name\":\"doc8\",\"item\":true,\"personInfo\":\ + [{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; +static const char *g_document9 = "{\"_id\" : \"9\", \"name\":\"doc9\",\"item\": true}"; +static const char *g_document10 = "{\"_id\" : \"10\", \"name\":\"doc10\", \"parent\" : \"kate\"}"; +static const char *g_document11 = "{\"_id\" : \"11\", \"name\":\"doc11\", \"other\" : \"null\"}"; +static const char *g_document12 = "{\"_id\" : \"12\", \"name\":\"doc12\",\"other\" : null}"; +static const char *g_document13 = "{\"_id\" : \"13\", \"name\":\"doc13\",\"item\" : \"shoes\",\"personInfo\":\ + {\"school\":\"AB\", \"age\" : 15}}"; +static const char *g_document14 = "{\"_id\" : \"14\", \"name\":\"doc14\",\"item\" : true,\"personInfo\":\ + [{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 85}]}"; +static const char *g_document15 = "{\"_id\" : \"15\", \"name\":\"doc15\",\"personInfo\":[{\"school\":\"C\", \"age\" : 5}]}"; +static const char *g_document16 = "{\"_id\" : \"16\", \"name\":\"doc16\", \"nested1\":{\"nested2\":{\"nested3\":\ + {\"nested4\":\"ABC\", \"field2\":\"CCC\"}}}}"; +static const char *g_document17 = "{\"_id\" : \"17\", \"name\":\"doc17\",\"personInfo\":\"oh,ok\"}"; +static const char *g_document18 = "{\"_id\" : \"18\", \"name\":\"doc18\",\"item\" : \"mobile phone\",\"personInfo\":\ + {\"school\":\"DD\", \"age\":66}, \"color\":\"blue\"}"; +static const char *g_document19 = "{\"_id\" : \"19\", \"name\":\"doc19\",\"ITEM\" : true,\"PERSONINFO\":\ + {\"school\":\"AB\", \"age\":15}}"; +static const char *g_document20 = "{\"_id\" : \"20\", \"name\":\"doc20\",\"ITEM\" : true,\"personInfo\":\ + [{\"SCHOOL\":\"B\", \"AGE\":15}, {\"SCHOOL\":\"C\", \"AGE\":35}]}"; +static std::vectorg_data = {g_document1, g_document2, g_document3, g_document4, g_document5, g_document6, g_document7, + g_document8, g_document9, g_document10, g_document11, g_document12, g_document13, g_document14, g_document15, g_document16, g_document17, g_document18, g_document19, g_document20}; + +static void InsertData(GRD_DB *g_db, const char *collectionName) +{ + for (const auto &item : g_data) { + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, item, 0), GRD_OK); + } +} + +static void CompareValue(const char *value, const char *targetValue) +{ + int errCode; + DocumentDB::JsonObject valueObj = DocumentDB::JsonObject::Parse(value, errCode); + EXPECT_EQ(errCode, E_OK); + DocumentDB::JsonObject targetValueObj = DocumentDB::JsonObject::Parse(targetValue, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(valueObj.Print(), targetValueObj.Print()); +} +} + +class DocumentFindApiTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + void InsertDoc(const char *collectionName, const char *document); +}; +void DocumentFindApiTest::SetUpTestCase(void) +{ + std::string path = "./document.db"; + int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &g_db); + EXPECT_EQ(status, GRD_OK); + EXPECT_EQ(GRD_CreateCollection(g_db, COLLECTION_NAME, "", 0), GRD_OK); + EXPECT_NE(g_db, nullptr); +} + +void DocumentFindApiTest::TearDownTestCase(void) +{ + EXPECT_EQ(GRD_DBClose(g_db, 0), GRD_OK); + remove(path.c_str()); +} + +void DocumentFindApiTest::SetUp(void) +{ + InsertData(g_db, "student"); +} + +void DocumentFindApiTest::TearDown(void) +{ +} + + +/** + * @tc.name: DocumentFindApiTest001 + * @tc.desc: Test Insert document db + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest001, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with _id and get the record according to filter condition. + * @tc.expected: step1. Succeed to get the record, the matching record is g_document6. + */ + const char *filter = "{\"_id\" : \"6\"}"; + GRD_ResultSet *resultSet = nullptr; + Query query = {filter, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = NULL; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document6); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest002 + * @tc.desc: Test filter with multiple fields and _id. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest002, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with multiple and _id. and get the record according to filter condition. + * @tc.expected: step1. Faild to get the record, the result is GRD_INVALID_ARGS, GRD_GetValue return GRD_NOT_AVAILABLE and GRD_Next return GRD_NO_DATA. + */ + const char *filter = "{\"_id\" : \"6\", \"name\":\"doc6\"}"; + GRD_ResultSet *resultSet = nullptr; + Query query = {filter, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + char *value = NULL; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest004 + * @tc.desc: test filter with string filter without _id. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest004, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter without _id and get the record according to filter condition. + * @tc.expected: step1. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + const char *filter = "{\"name\":\"doc6\"}"; + GRD_ResultSet *resultSet = nullptr; + Query query = {filter, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + char *value = NULL; + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest005 + * @tc.desc: test filter field with other word. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest005, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with _id and number + * @tc.expected: step1. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + GRD_ResultSet *resultSet1 = nullptr; + const char *filter1 = "{\"_id\" : \"1\", \"info\" : 1}"; + Query query1 = {filter1, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query1, 1, &resultSet1), GRD_INVALID_ARGS); + + /** + * @tc.steps: step2. Create filter with two _id + * @tc.expected: step2. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + GRD_ResultSet *resultSet2 = nullptr; + const char *filter2 = "{\"_id\" : \"1\", \"_id\" : \"2\"}"; + Query query2 = {filter2, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query2, 1, &resultSet2), GRD_INVALID_ARGS); + + /** + * @tc.steps: step3. Create filter with array and _id + * @tc.expected: step3. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + GRD_ResultSet *resultSet3 = nullptr; + const char *filter3 = "{\"_id\" : \"1\", \"info\" : [\"2\", 1]}"; + Query query3 = {filter3, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query3, 1, &resultSet3), GRD_INVALID_ARGS); + + /** + * @tc.steps: step4. Create filter with object and _id + * @tc.expected: step4. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + GRD_ResultSet *resultSet4 = nullptr; + const char *filter4 = "{\"_id\" : \"1\", \"info\" : {\"info_val\" : \"1\"}}"; + Query query4 = {filter4, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query4, 1, &resultSet4), GRD_INVALID_ARGS); + + /** + * @tc.steps: step5. Create filter with bool and _id + * @tc.expected: step5. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + GRD_ResultSet *resultSet5 = nullptr; + const char *filter5 = "{\"_id\" : \"1\", \"info\" : true}"; + Query query5 = {filter5, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query5, 1, &resultSet5), GRD_INVALID_ARGS); + + /** + * @tc.steps: step6. Create filter with null and _id + * @tc.expected: step6. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + GRD_ResultSet *resultSet6 = nullptr; + const char *filter6 = "{\"_id\" : \"1\", \"info\" : null}"; + Query query6 = {filter6, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query6, 1, &resultSet6), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest006 + * @tc.desc: test filter field with id which has different type of value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest006, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with _id which value is string + * @tc.expected: step1. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + GRD_ResultSet *resultSet1 = nullptr; + const char *filter1 = "{\"_id\" : \"valstring\"}"; + Query query1 = {filter1, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query1, 1, &resultSet1), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet1), GRD_OK); + + /** + * @tc.steps: step2. Create filter with _id which value is number + * @tc.expected: step2. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + GRD_ResultSet *resultSet2 = nullptr; + const char *filter2 = "{\"_id\" : 1}"; + Query query2 = {filter2, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query2, 1, &resultSet2), GRD_INVALID_ARGS); + + /** + * @tc.steps: step3. Create filter with _id which value is array + * @tc.expected: step3. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + GRD_ResultSet *resultSet3 = nullptr; + const char *filter3 = "{\"_id\" : [\"2\", 1]}"; + Query query3 = {filter3, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query3, 1, &resultSet3), GRD_INVALID_ARGS); + + /** + * @tc.steps: step4. Create filter with _id which value is object + * @tc.expected: step4. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + GRD_ResultSet *resultSet4 = nullptr; + const char *filter4 = "{\"_id\" : {\"info_val\" : \"1\"}}"; + Query query4 = {filter4, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query4, 1, &resultSet4), GRD_INVALID_ARGS); + + /** + * @tc.steps: step5. Create filter with _id which value is bool + * @tc.expected: step5. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + GRD_ResultSet *resultSet5 = nullptr; + const char *filter5 = "{\"_id\" : true}"; + Query query5 = {filter5, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query5, 1, &resultSet5), GRD_INVALID_ARGS); + + /** + * @tc.steps: step6. Create filter with _id which value is null + * @tc.expected: step6. Faild to get the record, the result is GRD_INVALID_ARGS, + */ + GRD_ResultSet *resultSet6 = nullptr; + const char *filter6 = "{\"_id\" : null}"; + Query query6 = {filter6, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query6, 1, &resultSet6), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest016 + * @tc.desc: Test filter with collection Name is invalid. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest016, TestSize.Level1) +{ + const char *colName1 = "grd_type"; + const char *colName2 = "GM_SYS_sysfff"; + GRD_ResultSet *resultSet = NULL; + const char *filter = "{\"_id\" : \"1\"}"; + Query query = {filter, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, colName1, query, 1, &resultSet), GRD_INVALID_FORMAT); + EXPECT_EQ(GRD_FindDoc(g_db, colName2, query, 1, &resultSet), GRD_INVALID_FORMAT); +} + +// /** +// * @tc.name: DocumentFindApiTest017 +// * @tc.desc: Test filter field with large filter +// * @tc.type: FUNC +// * @tc.require: +// * @tc.author: mazhao +// */ +// HWTEST_F(DocumentFindApiTest, DocumentFindApiTest017, TestSize.Level1) +// { +// GRD_ResultSet *resultSet = nullptr; +// string documentPart1 = "{\"_id\" : \"18\", \"item\" :\" "; +// string documentPart2 = "\" }"; +// string jsonVal = string(512 * 1024 - documentPart1.size() - documentPart2.size() - 1, 'k'); +// string document = documentPart1 + jsonVal + documentPart2; +// string jsonVal1 = string(512 * 1024 - documentPart1.size() - documentPart2.size(), 'k'); +// string document1 = documentPart1 + jsonVal1 + documentPart2; + +// Query query = {document.c_str(), "{}"}; +// EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); +// EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); +// char *value = NULL; +// EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); +// EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + +// query = {document1.c_str(), "{}"}; +// EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OVER_LIMIT); +// } + +/** + * @tc.name: DocumentFindApiTest019 + * @tc.desc: Test filter field with no result + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest019, TestSize.Level1) +{ + const char *filter = "{\"_id\" : \"100\"}"; + GRD_ResultSet *resultSet = nullptr; + Query query = {filter, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + char *value = NULL; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest023 + * @tc.desc: Test filter field with double find. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest023, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with _id and get the record according to filter condition. + * @tc.expected: step1. succeed to get the record, the matching record is g_document6. + */ + const char *filter = "{\"_id\" : \"6\"}"; + GRD_ResultSet *resultSet = nullptr; + GRD_ResultSet *resultSet2 = nullptr; + Query query = {filter, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet2), GRD_RESOURCE_BUSY); + + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = NULL; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document6); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document6); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest024 + * @tc.desc: Test filter field with multi collections + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest024, TestSize.Level1) +{ + const char *filter = "{\"_id\" : \"6\"}"; + GRD_ResultSet *resultSet = nullptr; + GRD_ResultSet *resultSet2 = nullptr; + Query query = {filter, "{}"}; + const char* collectionName = "DocumentFindApiTest024"; + EXPECT_EQ(GRD_CreateCollection(g_db, collectionName, "", 0), GRD_OK); + InsertData(g_db, collectionName); + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_FindDoc(g_db, collectionName, query, 1, &resultSet2), GRD_OK); + + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = NULL; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document6); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + + EXPECT_EQ(GRD_Next(resultSet2), GRD_OK); + char *value2 = NULL; + EXPECT_EQ(GRD_GetValue(resultSet2, &value2), GRD_OK); + CompareValue(value2, g_document6); + EXPECT_EQ(GRD_FreeValue(value2), GRD_OK); + + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_Next(resultSet2), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_GetValue(resultSet2, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet2), GRD_OK); + + EXPECT_EQ(GRD_DropCollection(g_db, collectionName, 0), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest025 + * @tc.desc: Test nested projection, with viewType equals to 1. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest025, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter to match g_document16, _id flag is 0. + * Create projection to display name,nested4. + * @tc.expected: step1. resultSet init successfuly, the result is GRD_OK, + */ + const char *filter = "{\"_id\" : \"16\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\": true, \"nested1.nested2.nested3.nested4\":true}"; + const char *targetDocument = "{\"name\":\"doc16\", \"nested1\":{\"nested2\":{\"nested3\":\ + {\"nested4\":\"ABC\"}}}}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. After loop, cannot get more record. + * @tc.expected: step2. Return GRD_NO_DATA. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step3. Create filter to match g_document16, _id flag is 0; + * Create projection to display name、nested4 with different projection format. + * @tc.expected: step3. succeed to get the record. + */ + projectionInfo = "{\"name\": true, \"nested1\":{\"nested2\":{\"nested3\":{\"nested4\":true}}}}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step4. After loop, cannot get more record. + * @tc.expected: step4. return GRD_NO_DATA. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step5. Create filter to match g_document16, _id flag is 0. + * Create projection to conceal name,nested4 with different projection format. + * @tc.expected: step5. succeed to get the record. + */ + projectionInfo = "{\"name\": 0, \"nested1.nested2.nested3.nested4\":0}"; + targetDocument = "{\"nested1\":{\"nested2\":{\"nested3\":{\"field2\":\"CCC\"}}}}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} +#include +/** + * @tc.name: DocumentFindApiTest026 + * @tc.desc: Test nested projection, with _id flag equals to 1. Projection is 5 level. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest026, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter to match g_document16, _id flag is 0. + * Create projection to display name,nested5 + * @tc.expected: step1. Error GRD_INVALID_ARGS. + */ + const char *filter = "{\"_id\" : \"16\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\": true, \"nested1.nested2.nested3.nested4.nested5\":true}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); + // EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + // char *value = nullptr; + // EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); + /** + * @tc.steps: step2. After loop, cannot get more record. + * @tc.expected: step2. Return GRD_NO_DATA. + */ + // EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + // EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest027 + * @tc.desc: Test projection with invalid field, _id field equals to 1. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest027, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. + * Create projection to display name, other _info and non existing field. + * @tc.expected: step1. Match the g_document7 and display name, other_info + */ + const char *filter = "{\"_id\" : \"7\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\": true, \"other_Info\":true, \"non_exist_field\":true}"; + const char *targetDocument = "{\"name\": \"doc7\", \"other_Info\":[{\"school\":\"BX\", \"age\":15},\ + {\"school\":\"C\", \"age\":35}]}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + /** + * @tc.steps: step2. Create filter to match g_document7, _id flag is 0. + * Create projection to display name, other _info and existing field with space. + * @tc.expected: step2. Return GRD_INVALID_ARGS. + */ + projectionInfo = "{\"name\": true, \"other_Info\":true, \" item \":true}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps: step3. Create filter to match g_document7, _id flag is 0. + * Create projection to display name, other _info and existing field with different case. + * @tc.expected: step3. Match the g_document7 and display name, other_Info. + */ + projectionInfo = "{\"name\": true, \"other_Info\":true, \"ITEM\": true}"; + query = {filter, projectionInfo}; + resultSet = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest028 + * @tc.desc: Test projection with invalid field in Array,_id field equals to 1. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest028, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. + * Create projection to display name, non existing field in array. + * @tc.expected: step1. Match the g_document7 and display name, other_info. + */ + const char *filter = "{\"_id\" : \"7\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\": true, \"other_Info.non_exist_field\":true}"; + const char *targetDocument = "{\"name\": \"doc7\"}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + /** + * @tc.steps: step2. Create filter to match g_document7, _id flag is 0. + * Create projection to display name, other _info and existing field with space. + * @tc.expected: step2. Return GRD_INVALID_ARGS. + */ + projectionInfo = "{\"name\": true, \"other_Info\":{\"non_exist_field\":true}}"; + query = {filter, projectionInfo}; + resultSet = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + /** + * @tc.steps: step3. Create filter to match g_document7, _id flag is 0. + * Create projection to display name, non existing field in array with index format. + * @tc.expected: step3. Match the g_document7 and display name, other_Info. + */ + projectionInfo = "{\"name\": true, \"other_Info.0\": true}"; + query = {filter, projectionInfo}; + resultSet = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest029 + * @tc.desc: Test projection with path conflict._id field equals to 0. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest029, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter to match g_document4, _id flag is 0. + * Create projection to display conflict path. + * @tc.expected: step1. Return GRD_INVALID_ARGS. + */ + const char *filter = "{\"_id\" : \"4\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"personInfo\": true, \"personInfo.grade\": true}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest030 + * @tc.desc: Test _id flag and field.None exist field. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest030, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. + * @tc.expected: step1. Match the g_document7 and return empty json. + */ + const char *filter = "{\"_id\" : \"7\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"non_exist_field\":true}"; + int flag = 0; + const char *targetDocument = "{}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + /** + * @tc.steps: step2. Create filter to match g_document7, _id flag is 1. + * @tc.expected: step2. Match g_document7, and return a json with _id. + */ + resultSet = nullptr; + flag = 1; + targetDocument = "{\"_id\": \"7\"}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + value = NULL; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest031 + * @tc.desc: Test _id flag and field.Exist field with 1 value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest031, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. + * @tc.expected: step1. Match the g_document7 and return json with name, item. + */ + const char *filter = "{\"_id\" : \"7\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\":true, \"item\":true}"; + int flag = 0; + const char *targetDocument = "{\"name\":\"doc7\", \"item\":\"fruit\"}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + /** + * @tc.steps: step2. Create filter to match g_document7, _id flag is 1. + * @tc.expected: step2. Match g_document7, and return a json with _id. + */ + resultSet = nullptr; + flag = 1; + projectionInfo = "{\"name\": 1, \"item\": 1}"; + targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + value = NULL; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest032 + * @tc.desc: Test _id flag and field.Exist field with 1 value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest032, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. + * @tc.expected: step1. Match the g_document7 and return json with name, item. + */ + const char *filter = "{\"_id\" : \"7\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\":true, \"item\":true}"; + int flag = 0; + const char *targetDocument = "{\"name\":\"doc7\", \"item\":\"fruit\"}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step2. Create filter to match g_document7, _id flag is 1. + * @tc.expected: step2. Match g_document7, and return a json with _id. + */ + resultSet = nullptr; + flag = 1; + projectionInfo = "{\"name\": 1, \"item\": 1}"; + targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + value = NULL; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step3. Create filter to match g_document7, _id flag is 1.Projection value is not 0. + * @tc.expected: step3. Match g_document7, and return a json with name, item and _id. + */ + resultSet = nullptr; + flag = 1; + projectionInfo = "{\"name\": 10, \"item\": 10}"; + targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + value = NULL; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest033 + * @tc.desc: Test _id flag and field.Exist field with 0 value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest033, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. + * @tc.expected: step1. Match the g_document7 and return json with name, item and _id + */ + const char *filter = "{\"_id\" : \"7\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\":false, \"item\":false}"; + int flag = 0; + const char *targetDocument = "{\"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}";; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step2. Create filter to match g_document7, _id flag is 1. + * @tc.expected: step2. Match g_document7, and return a json without name and item. + */ + resultSet = nullptr; + flag = 1; + projectionInfo = "{\"name\": 0, \"item\": 0}"; + targetDocument = "{\"_id\": \"7\", \"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}";; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + value = NULL; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest034 + * @tc.desc: Test projection with nonexist field in nested structure. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest034, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter to match g_document4, _id flag is 0. + * @tc.expected: step1. Match the g_document4 and return json without name + */ + const char *filter = "{\"_id\" : \"4\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\": 1, \"personInfo.grade1\": 1, \ + \"personInfo.shool1\": 1, \"personInfo.age1\": 1}"; + int flag = 0; + const char *targetDocument = "{\"name\":\"doc4\"}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + /** + * @tc.steps: step2. Create filter to match g_document4, _id flag is 0, display part of fields in nested structure. + * @tc.expected: step2. Match the g_document4 and return json without name + */ + projectionInfo = "{\"name\": false, \"personInfo.grade1\": false, \ + \"personInfo.shool1\": false, \"personInfo.age1\": false}"; + const char *targetDocument2 = "{\"item\":\"paper\",\"personInfo\":{\"grade\" : 1, \"school\":\"A\", \"age\" : 18}}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument2); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + /** + * @tc.steps: step3. Create filter to match g_document4, _id flag is 0, display part of fields in nested structure. + * @tc.expected: step3. Match the g_document4 and return json with name, personInfo.school and personInfo.age. + */ + projectionInfo = "{\"name\": 1, \"personInfo.school\": 1, \"personInfo.age\": 1}"; + const char *targetDocument3 = "{\"name\":\"doc4\", \"personInfo\": {\"school\":\"A\", \"age\" : 18}}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument3); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + /** + * @tc.steps: step4. Create filter to match g_document4, _id flag is 0, display part of fields in nested structure. + * @tc.expected: step4. Match the g_document4 and return json with name, personInfo.school + */ + projectionInfo = "{\"name\": 1, \"personInfo.school\": 1, \"personInfo.age1\": 1}"; + const char *targetDocument4 = "{\"name\":\"doc4\", \"personInfo\": {\"school\":\"A\"}}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument4); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest035 + * @tc.desc: test filter with id string filter + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest035, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with _id and get the record according to filter condition. + * @tc.expected: step1. succeed to get the record, the matching record is g_document17 + */ + const char *filter = "{\"_id\" : \"17\"}"; + GRD_ResultSet *resultSet = nullptr; + int flag = 0; + Query query = {filter, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, GRD_DOC_ID_DISPLAY, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document17); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest036 + * @tc.desc: Test with invalid collectionName. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest036, TestSize.Level1) +{ + /** + * @tc.steps: step1. Test with invalid collectionName. + * @tc.expected: step1. Return GRD_INVALID_ARGS. + */ + const char *filter = "{\"_id\" : \"17\"}"; + GRD_ResultSet *resultSet = nullptr; + int flag = 0; + Query query = {filter, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, "", query, 0, &resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FindDoc(g_db, NULL, query, 0, &resultSet), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest037 + * @tc.desc: Test filed with different value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest037, TestSize.Level1) +{ + /** + * @tc.steps: step1. Test filed with different value.some are 1, other are 0. + * @tc.expected: step1. Return GRD_INVALID_ARGS. + */ + const char *filter = "{\"_id\" : \"4\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\":1, \"personInfo\":0, \"item\":1}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps: step2. Test filed with different value.some are 2, other are 0. + * @tc.expected: step2. Return GRD_INVALID_ARGS. + */ + projectionInfo = "{\"name\":2, \"personInfo\":0, \"item\":2}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps: step3. Test filed with different value.some are 0, other are true. + * @tc.expected: step3. Return GRD_INVALID_ARGS. + */ + projectionInfo = "{\"name\":true, \"personInfo\":0, \"item\":true}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps: step4. Test filed with different value.some are 0, other are "". + * @tc.expected: step4. Return GRD_INVALID_ARGS. + */ + projectionInfo = "{\"name\":\"\", \"personInfo\":0, \"item\":\"\"}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps: step5. Test filed with different value.some are 1, other are false. + * @tc.expected: step5. Return GRD_INVALID_ARGS. + */ + projectionInfo = "{\"name\":false, \"personInfo\":1, \"item\":false"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT); + + /** + * @tc.steps: step6. Test filed with different value.some are -1.123, other are false. + * @tc.expected: step6. Return GRD_INVALID_ARGS. + */ + projectionInfo = "{\"name\":false, \"personInfo\":-1.123, \"item\":false"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT); + + /** + * @tc.steps: step7. Test filed with different value.some are true, other are false. + * @tc.expected: step7. Return GRD_INVALID_ARGS. + */ + projectionInfo = "{\"name\":false, \"personInfo\":true, \"item\":false"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT); +} + +/** + * @tc.name: DocumentFindApiTest038 + * @tc.desc: Test field with false value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest038, TestSize.Level1) +{ + /** + * @tc.steps: step1. Test field with different false value. Some are false, other are 0. flag is 0. + * @tc.expected: step1. Match the g_document6 and return empty json. + */ + const char *filter = "{\"_id\" : \"6\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\":false, \"personInfo\": 0, \"item\":0}"; + int flag = 0; + const char *targetDocument = "{}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + /** + * @tc.steps: step2. Test field with different false value.Some are false, others are 0. flag is 1. + * @tc.expected: step2. Match g_document6, Return json with _id. + */ + targetDocument = "{\"_id\": \"6\"}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest039 + * @tc.desc: Test field with true value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest039, TestSize.Level1) +{ + /** + * @tc.steps: step1. Test field with different true value. Some are true, other are 1. flag is 0. + * @tc.expected: step1. Match the g_document18 and return json with name, item, personInfo.age and color. + */ + const char *filter = "{\"_id\" : \"18\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\":true, \"personInfo.age\": \"\", \"item\":1, \"color\":10, \"nonExist\" : -100}"; + const char *targetDocument = "{\"name\":\"doc18\", \"item\":\"mobile phone\", \"personInfo\":\ + {\"age\":66}, \"color\":\"blue\"}"; + int flag = 0; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + /** + * @tc.steps: step2. Test field with different true value.Some are false, others are 0. flag is 1. + * @tc.expected: step2. Match g_document18, Return json with name, item, personInfo.age, color and _id. + */ + targetDocument = "{\"_id\" : \"18\", \"name\":\"doc18\",\"item\" : \"mobile phone\",\"personInfo\":\ + {\"age\":66}, \"color\":\"blue\"}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest040 + * @tc.desc: Test field with invalid value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest040, TestSize.Level1) +{ + /** + * @tc.steps: step1. Test field with invalid value.Value is array. + * @tc.expected: step1. Match the g_document18 and return GRD_INVALID_ARGS. + */ + const char *filter = "{\"_id\" : \"18\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"personInfo\":[true, 1]}"; + int flag = 1; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps: step2. Test field with invalid value.Value is null. + * @tc.expected: step2. Match the g_document18 and return GRD_INVALID_ARGS. + */ + projectionInfo = "{\"personInfo\":null}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps: step3. Test field with invalid value.Value is invalid string. + * @tc.expected: step3. Match the g_document18 and return GRD_INVALID_ARGS. + */ + projectionInfo = "{\"personInfo\":\"invalid string.\"}"; + query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest042 + * @tc.desc: Test field with no existed uppercase filter + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest042, TestSize.Level1) +{ + /** + * @tc.steps: step1. Test field with no existed uppercase filter. + * @tc.expected: step1. not match any item. + */ + const char *filter = "{\"_iD\" : \"18\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"Name\":true, \"personInfo.age\": \"\", \"item\":1, \"COLOR\":10, \"nonExist\" : -100}"; + int flag = 0; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps: step2. Test field with upper projection. + * @tc.expected: step2. Match g_document18, Return json with item, personInfo.age, color and _id. + */ + const char *filter1 = "{\"_id\" : \"18\"}"; + const char* targetDocument = "{\"_id\" : \"18\", \"item\" : \"mobile phone\",\"personInfo\":\ + {\"age\":66}}"; + query = {filter1, projectionInfo}; + char *value = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest044 + * @tc.desc: Test field with uppercase projection + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest044, TestSize.Level1) +{ + /** + * @tc.steps: step1. Test with false uppercase projection + * @tc.expected: step1. Match g_document18, Return json with item, personInfo.age and _id. + */ + const char *filter = "{\"_id\" : \"18\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"Name\":0, \"personInfo.age\": false, \"personInfo.SCHOOL\": false, \"item\":\ + false, \"COLOR\":false, \"nonExist\" : false}"; + const char *targetDocument = "{\"_id\" : \"18\", \"name\":\"doc18\", \"personInfo\":\ + {\"school\":\"DD\"}, \"color\":\"blue\"}"; + int flag = 0; + Query query = {filter, projectionInfo}; + char *value = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, targetDocument); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest045 + * @tc.desc: Test field with too long collectionName + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest045, TestSize.Level1) +{ + /** + * @tc.steps: step1. Test with false uppercase projection + * @tc.expected: step1. Match g_document18, Return json with item, personInfo.age and _id. + */ + const char *filter = "{\"_id\" : \"18\"}"; + GRD_ResultSet *resultSet = nullptr; + int flag = 0; + Query query = {filter, "{}"}; + string collectionName1(MAX_COLLECTION_NAME, 'a'); + ASSERT_EQ(GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0), GRD_OK); + EXPECT_EQ(GRD_FindDoc(g_db, collectionName1.c_str(), query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + ASSERT_EQ(GRD_DropCollection(g_db, collectionName1.c_str(), 0), GRD_OK); + + string collectionName2(MAX_COLLECTION_NAME + 1, 'a'); + EXPECT_EQ(GRD_FindDoc(g_db, collectionName2.c_str(), query, 1, &resultSet), GRD_OVER_LIMIT); + EXPECT_EQ(GRD_FindDoc(g_db, "", query, 1, &resultSet), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest052 + * @tc.desc: Test field when id string len is large than max + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest052, TestSize.Level1) +{ + /** + * @tc.steps: step1. Test with false uppercase projection + * @tc.expected: step1. Match g_document18, Return json with item, personInfo.age and _id. + */ + const char *filter = "{\"_id\" : \"18\"}"; + GRD_ResultSet *resultSet = nullptr; + int flag = 0; + Query query = {filter, "{}"}; + string collectionName1(MAX_COLLECTION_NAME, 'a'); + ASSERT_EQ(GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0), GRD_OK); + EXPECT_EQ(GRD_FindDoc(g_db, collectionName1.c_str(), query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + ASSERT_EQ(GRD_DropCollection(g_db, collectionName1.c_str(), 0), GRD_OK); + + string collectionName2(MAX_COLLECTION_NAME + 1, 'a'); + EXPECT_EQ(GRD_FindDoc(g_db, collectionName2.c_str(), query, 1, &resultSet), GRD_OVER_LIMIT); + EXPECT_EQ(GRD_FindDoc(g_db, "", query, 1, &resultSet), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest053 + * @tc.desc: Test with invalid flags + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest053, TestSize.Level1) +{ + /** + * @tc.steps: step1. Test with invalid flags which is 3. + * @tc.expected: step1. Return GRD_INVALID_ARGS. + */ + const char *filter = "{\"_id\" : \"18\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 3, &resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps:step1.parameter flags is int_max + * @tc.expected:step1.GRD_INVALID_ARGS + */ + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, INT_MAX, &resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps:step1.parameter flags is INT_MIN + * @tc.expected:step1.GRD_INVALID_ARGS + */ + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, INT_MIN, &resultSet), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest054 + * @tc.desc: Test with null g_db and resultSet, filter. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest054, TestSize.Level1) +{ + /** + * @tc.steps: step1. Test with null g_db. + * @tc.expected: step1. Return GRD_INVALID_ARGS. + */ + const char *filter = "{\"_id\" : \"18\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{}"; + Query query = {filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(nullptr, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps: step2. Test with null resultSet. + * @tc.expected: step2. Return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, nullptr), GRD_INVALID_ARGS); + + /** + * @tc.steps: step1. Test with query that has two nullptr data. + * @tc.expected: step1. Return GRD_INVALID_ARGS. + */ + query = {nullptr, nullptr}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentFindApiTest055 + * @tc.desc: Find doc, but filter' _id value lens is larger than MAX_ID_LENS + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest055, TestSize.Level1) +{ + /** + * @tc.steps:step1.Find doc, but filter' _id value lens is larger than MAX_ID_LENS + * @tc.expected:step1.GRD_OVER_LIMIT. + */ + string document1 = "{\"_id\" : "; + string document2 = "\""; + string document4 = "\""; + string document5 = "}"; + string document_midlle(MAX_ID_LENS + 1, 'k'); + string filter = document1 + document2 + document_midlle + document4 + document5; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{}"; + Query query = {filter.c_str(), projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OVER_LIMIT); + + /** + * @tc.steps:step1.Find doc, filter' _id value lens is equal as MAX_ID_LENS + * @tc.expected:step1.GRD_OK. + */ + string document_midlle2(MAX_ID_LENS, 'k'); + filter = document1 + document2 + document_midlle2 + document4 + document5; + query = {filter.c_str(), projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp new file mode 100644 index 00000000..5f75c674 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp @@ -0,0 +1,754 @@ +/* +* 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. +*/ + +#include + +#include "grd_base/grd_db_api.h" +#include "grd_document/grd_document_api.h" +#include "grd_base/grd_error.h" +using namespace testing::ext; +namespace { +std::string path = "./document.db"; +GRD_DB *g_db = nullptr; +const char *RIGHT_COLLECTION_NAME = "student"; +const char *NO_EXIST_COLLECTION_NAME = "no_exisit"; +const int INT_MAX = 2147483647; +const int INT_MIN = -2147483648; +const int MAX_COLLECTION_LENS = 511; +const int MAX_ID_LENS = 899; + +static void TestInsertDocIntoCertainColl(const char *collectionName, const char *projection, int expectedResult) +{ + /** * @tc.steps: step1. Create Collection + * @tc.expected: step1. GRD_OK + */ + EXPECT_EQ(GRD_CreateCollection(g_db, collectionName, "", 0), expectedResult); + /** + * @tc.steps: step2. Insert projection into colloction. + * @tc.expected: step2. GRD_OK + */ + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, projection, 0), expectedResult); + /** + * @tc.steps: step3. Call GRD_DroCollection to drop the collection. + * @tc.expected: step3. GRD_OK + */ + EXPECT_EQ(GRD_DropCollection(g_db, collectionName, 0), expectedResult); +} + +const char* SetRandomDocument(int i) +{ + string document1 = "{\"_id\" : "; + string document2 = "\""; + string document3 = {'2','6'}; + string document4 = "\""; + string document5 = ", \"name\" : \"Ori\"}"; + string document = document1 + document2 + document3 + document4 + document5; + return document.c_str(); +} +} + +class DocumentInsertApiTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +void DocumentInsertApiTest::SetUpTestCase(void) +{ + std::string path = "./document.db"; + int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &g_db); + EXPECT_EQ(status, GRD_OK); + EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); + EXPECT_NE(g_db, nullptr); +} + +void DocumentInsertApiTest::TearDownTestCase(void) +{ + EXPECT_EQ(GRD_DBClose(g_db, 0), GRD_OK); + remove(path.c_str()); +} + +void DocumentInsertApiTest::SetUp(void) +{ +} + +void DocumentInsertApiTest::TearDown(void) +{ +} + + +/** + * @tc.name: DocumentInsertApiTest001 + * @tc.desc: Insert documents into collection which dose not exist + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest001, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert document into collection which dose not exist + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *document1 = "{\"_id\" : \"1\", \"name\" : \"Ori\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, NO_EXIST_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentInsertApiTest002 + * @tc.desc: Insert documents into collection which _id is not string + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest002, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert a document whose _id is integer + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *document1 = "{\"_id\" : 2, \"name\" : \"Ori\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step2.Insert a document whose _id is bool + * @tc.expected:step2.GRD_INVALID_ARGS + */ + const char *document2 = "{\"_id\" : true, \"name\" : \"Chuan\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step2.Insert a document whose _id is NULL + * @tc.expected:step2.GRD_INVALID_ARGS + */ + const char *document3 = "{\"_id\" : null, \"name\" : \"Chuan\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step2.Insert a document whose _id is ARRAY + * @tc.expected:step2.GRD_INVALID_ARGS + */ + const char *document4 = "{\"_id\" : [\"2\"], \"name\" : \"Chuan\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document4, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step2.Insert a document whose _id is OBJECT + * @tc.expected:step2.GRD_INVALID_ARGS + */ + const char *document5 = "{\"_id\" : {\"val\" : \"2\"}, \"name\" : \"Chuan\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document5, 0), GRD_INVALID_ARGS); +} + + +/** + * @tc.name: DocumentInsertApiTest003 + * @tc.desc: Insert a document whose _id has appeared before + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest003, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert a document whose _id is string + * @tc.expected:step1.GRD_OK + */ + const char *document1 = "{\"_id\" : \"3\", \"name\" : \"Ori\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); + + /** + * @tc.steps:step2.Insert a document whose _id has appeared before + * @tc.expected:step2.GRD_DATA_CONFLICT + */ + // const char *document2 = "{\"_id\" : \"3\", \"name\" : \"Chuan\"}"; + // EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_DATA_CONFLICT); +} + +/** + * @tc.name: DocumentInsertApiTest004 + * @tc.desc: Test Insert with null parameter. parameter db is NULL + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest004, TestSize.Level1) +{ + /** + * @tc.steps:step1.step1.parameter db is NULL + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *document1 = "{\"_id\" : \"4\", \"name\" : \"Ori\"}"; + EXPECT_EQ(GRD_InsertDoc(NULL, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentInsertApiTest005 + * @tc.desc: Test insert with null parameter. parameter collectionName is NULL. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest005, TestSize.Level1) +{ + /** + * @tc.steps:step1.Parameter collectionName is NULL + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *document1 = "{\"_id\" : \"5\", \"name\" : \"Ori\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, NULL, document1, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step2.Parameter collectionName is empty string + * @tc.expected:step2.GRD_INVALID_ARGS + */ + const char *document2 = "{\"_id\" : \"5\", \"name\" : \"Chuang\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, "", document2, 0), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentInsertApiTest006 + * @tc.desc: parameter flags is not zero + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest006, TestSize.Level1) +{ + /** + * @tc.steps:step1.parameter flags is not zero + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *document1 = "{\"_id\" : \"6\", \"name\" : \"Ori\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 1), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentInsertApiTest007 + * @tc.desc: parameter flags is INT_MAX + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest007, TestSize.Level1) +{ + /** + * @tc.steps:step1.parameter flags is int_max + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *document1 = "{\"_id\" : \"7\", \"name\" : \"Ori\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, INT_MAX), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentInsertApiTest008 + * @tc.desc: parameter flags is int_min + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest008, TestSize.Level1) +{ + /** + * @tc.steps:step1.parameter flags is int_min + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *document1 = "{\"_id\" : \"8\", \"name\" : \"Ori\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, INT_MIN), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentInsertApiTest009 + * @tc.desc: parameter collectionName and document is NULL or invalid + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest009, TestSize.Level1) +{ + /** + * @tc.steps:step1.parameter collectionName and document is NULL; + * @tc.expected:step1.GRD_INVALID_ARGS + */ + EXPECT_EQ(GRD_InsertDoc(g_db, NULL, NULL, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step2.parameter collectionName is larger than max_collectionName_lens; + * @tc.expected:step2.GRD_OVER_LIMIT + */ + const char *document1 = "{\"_id\" : \"9\", \"name\" : \"Ori\"}"; + std::string collectionName2(MAX_COLLECTION_LENS + 1, 'a'); + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName2.c_str(), document1, 0), GRD_OVER_LIMIT); +} + +/** + * @tc.name: DocumentInsertApiTest010 + * @tc.desc: parameter collectionName contains irregular charactor + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest010, TestSize.Level1) +{ + /** + * @tc.steps:step1.Create Collection whose parameter collectionName contains irregular charactor + * @tc.expected:step1.GRD_OK + */ + const char *collectionName = "collction@!#"; + const char *document1 = "{\"_id\" : \"10\", \"name\" : \"Ori\"}"; + TestInsertDocIntoCertainColl(collectionName, document1, GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest011 + * @tc.desc: parameter collectionName is longer than 256 charactors + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest011, TestSize.Level1) +{ + /** + * @tc.steps:step1.Create Collection whose parameter collectionName contains irregular charactor + * @tc.expected:step1.GRD_OK + */ + string collectionName(257, 'k'); + const char *document1 = "{\"_id\" : \"10\", \"name\" : \"Ori\"}"; + TestInsertDocIntoCertainColl(collectionName.c_str(), document1, GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest014 + * @tc.desc: Inserted document's JSON depth is larger than 4, which is 5. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest014, TestSize.Level1) +{ + /** + * @tc.steps:step1.document's JSON depth is larger than 4, which is 5. + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *document1 = "{\"level1\" : {\"level2\" : {\"level3\" : {\"level4\": {\"level5\" : 1}}, \"level3_2\" : \"level3_2_val\"\ + }},\"_id\":\"14\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step1.document's JSON depth is larger than 4, which is 5.But with array type. + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *document2 = "{\"level1\" : {\"level2\" : {\"level3\" : [{ \"level5\" : \"level5_1val\", \"level5_2\":\ + \"level5_2_val\"}, \"level4_val1\",\"level4_val2\"], \"level3_2\" : \"level3_2_val\"\ + }},\"_id\":\"14\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step1.document's JSON depth is 4 + * @tc.expected:step1.GRD_OK + */ + const char *document3 = "{\"level1\" : {\"level2\" : {\"level3\" : { \"level4\" : \"level5_1val\"}, \"level3_2\" : \"level3_2_val\"\ + }},\"_id\":\"14\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest015 + * @tc.desc: Inserted document with all kinds of size + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest015, TestSize.Level1) +{ + /** + * @tc.steps:step1.document's JSON is bigger than 512k - 1 + * @tc.expected:step1.GRD_INVALID_ARGS + */ + string documentPart1 = "{ \"_id\" : \"15\", \"textVal\" : \" "; + string documentPart2 = "\" }"; + string jsonVal = string(512 * 1024 - documentPart1.size() - documentPart2.size(), 'k'); + string document = documentPart1 + jsonVal + documentPart2; + //EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OVER_LIMIT); + /** + * @tc.steps:step2.Insert document's JSON is a large data but lower than 512k - 1 + * @tc.expected:step2.GRD_OK + */ + string jsonVal2 = string(512 * 1024 - 1 - documentPart1.size() - documentPart2.size(), 'k'); + string document2 = documentPart1 + jsonVal2 + documentPart2; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2.c_str(), 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest016 + * @tc.desc: document JSON string contains irregular char + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest016, TestSize.Level1) +{ + /** + * @tc.steps:step1.document JSON string contains irregular char. + * @tc.expected:step1.GRD_OK + */ + const char *document1 = "{\"_id\" : \"16\", \"name\" : \"!@#Ori\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest017 + * @tc.desc: document JSON string contains invalid value type such as BLOB type + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest017, TestSize.Level1) +{ + /** + * @tc.steps:step1.document JSON string contains invalid value type such as BLOB type. + * @tc.expected:step1.GRD_INVALID_FORMAT. + */ + const char *document = "{\"_id\" : \"17\", \"level1\" : {\"level2\" : {\"level3\" : {\"level4\" : x'1234'\ + } } }, \"level1_2\" : \"level1_2Val\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document, 0), GRD_INVALID_FORMAT); +} + +/** + * @tc.name: DocumentInsertApiTest018 + * @tc.desc: The Inserted document is not JSON format + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest018, TestSize.Level1) +{ + /** + * @tc.steps:step1.The Inserted document is not JSON format + * @tc.expected:step1.GRD_INVALID_FORMAT. + */ + const char *document = "some random string not JSON format"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document, 0), GRD_INVALID_FORMAT); +} + +/** + * @tc.name: DocumentInsertApiTest019 + * @tc.desc: Insert a normal documents + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest019, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert a normal documents which _id is in the end of the string + * @tc.expected:step1.GRD_OK. + */ + const char *document1 = "{\"name\" : \"Jack\", \"age\" : 18, \"friend\" : {\"name\" : \" lucy\"}, \"_id\" : \"19\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest022 + * @tc.desc: parameter collectionName is equal to 256 charactors + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest022, TestSize.Level1) +{ + /** + * @tc.steps:step1.parameter collectionName is equal to 256 charactors + * @tc.expected:step1.GRD_OK. + */ + string collectionName = string(256, 'k'); + string collectionName1(MAX_COLLECTION_LENS, 'a'); + const char *document1 = "{\"_id\" : \"22\", \"name\" : \"Ori\"}"; + TestInsertDocIntoCertainColl(collectionName.c_str(), document1, GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest023 + * @tc.desc: parameter collectionName contains upper & lower case charactors, + * numbers and underline + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest023, TestSize.Level1) +{ + /** + * @tc.steps:step1.parameter collectionName contains upper & lower case charactors, + * numbers and underline + * @tc.expected:step1.GRD_OK. + */ + string collectionName = "Aads_sd__23Asb_"; + const char *document1 = "{\"_id\" : \"23\", \"name\" : \"Ori\"}"; + TestInsertDocIntoCertainColl(collectionName.c_str(), document1, GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest024 + * @tc.desc: parameter collectionName's head is GRD_ or GM_SYS_ + * numbers and underline + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest024, TestSize.Level1) +{ + /** + * @tc.steps:step1.parameter collectionName's head is GRD_ + * @tc.expected:step1.GRD_INVALID_FORMAT. + */ + string collectionName = "GRD_collectionName"; + const char *document1 = "{\"_id\" : \"24\", \"name\" : \"Ori\"}"; + GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document1, 0), GRD_INVALID_FORMAT); + + /** + * @tc.steps:step2.parameter collectionName's head is GM_SYS_ + * @tc.expected:step2.GRD_INVALID_FORMAT. + */ + collectionName = "GM_SYS__collectionName"; + const char *document2 = "{\"_id\" : \"24_2\", \"name\" : \"Ori\"}"; + GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document2, 0), GRD_INVALID_FORMAT); + + /** + * @tc.steps:step3.parameter collectionName's head is grd_ + * @tc.expected:step3.GRD_INVALID_FORMAT. + */ + collectionName = "grd_collectionName"; + const char *document3 = "{\"_id\" : \"24_3\", \"name\" : \"Ori\"}"; + GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document3, 0), GRD_INVALID_FORMAT); + + /** + * @tc.steps:step4.parameter collectionName's head is gm_sys_ + * @tc.expected:step4.GRD_INVALID_FORMAT. + */ + collectionName = "gm_sys_collectionName"; + const char *document4 = "{\"_id\" : \"24_4\", \"name\" : \"Ori\"}"; + GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document4, 0), GRD_INVALID_FORMAT); + + /** + * @tc.steps:step5.parameter collectionName's head is gM_sYs_ that has Uppercase and lowercase at the same time. + * @tc.expected:step5.GRD_INVALID_FORMAT. + */ + collectionName = "gM_sYs_collectionName"; + const char *document5 = "{\"_id\" : \"24_5\", \"name\" : \"Ori\"}"; + GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document5, 0), GRD_INVALID_FORMAT); + + /** + * @tc.steps:step6.parameter collectionName's head is gRd_ that has Uppercase and lowercase at the same time. + * @tc.expected:step6.GRD_INVALID_FORMAT. + */ + collectionName = "gRd_collectionName"; + const char *document6 = "{\"_id\" : \"24_6\", \"name\" : \"Ori\"}"; + GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document6, 0), GRD_INVALID_FORMAT); + + /** + * @tc.steps:step7.parameter collectionName's head is grd@ that has no '_' + * @tc.expected:step7.GRD_INVALID_FORMAT. + */ + collectionName = "gRd@collectionName"; + const char *document7 = "{\"_id\" : \"24_7\", \"name\" : \"Ori\"}"; + GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document7, 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest025 + * @tc.desc: Insert document whose depth is 4, which is allowed + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest025, TestSize.Level1) +{ + /** + * @tc.steps:step1.documents JSON depth is 4, which is allowed. + * @tc.expected:step1.GRD_OK. + */ + const char *document1 = "{\"_id\" : \"25_0\", \"level1\" : { \"level2\" : {\"level3\" :\ + {\"level4\" : \"level4Val\" } } } , \"level1_2\" : \"level1_2Val\" }"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); + /** + * @tc.steps:step2.documents JSON depth is exactly 4. + * @tc.expected:step2.GRD_OK. + */ + const char *document2 = "{\"_id\" : \"25_1\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \"student_info\" : [{\"name\":\"张三\", \ + \"age\" : 18, \"sex\" : \"男\"}, { \"newName1\" : [\"qw\", \"dr\", 0, \"ab\"] }]}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_OK); + /** + * @tc.steps:step3.documents JSON depth is exactly 4, but the last field in array contains leading number + * @tc.expected:step3.GRD_INVALID_ARGS. + */ + const char *document3 = "{\"_id\" : \"25_2\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \"student_info\" : [{\"name\":\"张三\", \ + \"age\" : 18, \"sex\" : \"男\"}, [\"qw\", \"dr\", 0, \"ab\", {\"0ab\" : null}]]}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step4.documents JSON depth is exactly 5. + * @tc.expected:step4.GRD_INVALID_ARGS. + */ + const char *document4 = "{\"_id\" : \"25_3\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \"student_info\" : [{\"name\":\"张三\", \ + \"age\" : 18, \"sex\" : \"男\"}, { \"newName1\" : [\"qw\", \"dr\", 0, \"ab\", {\"level5\" : 1}] }]}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document4, 0), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentInsertApiTest026 + * @tc.desc: Insert 100 normal documents continuously + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert 100 normal documents continuously + * @tc.expected:step1.GRD_OK. + */ + string document1 = "{\"_id\" : "; + string document2 = "\""; + string document4 = "\""; + string document5 = ", \"name\" : \"Ori\"}"; + for (int i = 0; i < 5; i++) { + string document_midlle = "26" + std::to_string(i); + string document = document1 + document2 + document_midlle + document4 + document5; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OK); + } +} + +/** + * @tc.name: DocumentInsertApiTest035 + * @tc.desc: Insert a document whose value contains + * upper &lower case charactors, numbers and underline. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest035, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert a document whose value contains + * upper &lower case charactors, numbers and underline. + * @tc.expected:step1.GRD_OK. + */ + const char *document1 = "{\"_id\" : \"35\", \"A_aBdk_324_\" : \"value\", \"name\" : \"Chuan\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); + /** + * @tc.steps:step1.Insert a document whose value contains + * upper &lower case charactors, numbers and underline. + * But the field started with number, which is not allowed. + * @tc.expected:step1.GRD_OK. + */ + const char *document2 = "{\"_id\" : \"35_2\", \"1A_aBdk_324_\" : \"value\", \"name\" : \"Chuan\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentInsertApiTest036 + * @tc.desc: Insert a document whose value contains + * string, number, bool, null, array and object type + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest036, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert a document whose value contains + * string, number, bool, null, array and object type + * @tc.expected:step1.GRD_OK. + */ + const char *document1 = "{\"_id\" : \"36_0\", \"stringType\" : \"stringVal\", \"numType\" : 1, \"BoolType\" : true,\ + \"nullType\" : null, \"arrayType\" : [1, 2, 3, 4], \"objectType\" : {\"A\" : 3}}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest038 + * @tc.desc: Insert document whose value is over the range of double + * string, number, bool, null, array and object type + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest038, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert document whose value is over the range of double + * @tc.expected:step1.GRD_INVALID_ARGS. + */ + const char *document1 = R"({"_id" : "38_0", "field2" : 1.79769313486232e308})"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step2.Insert document whose value is over the range of double + * @tc.expected:step2.GRD_INVALID_ARGS. + */ + const char *document2 = R"({"_id" : "38_1", "t1" : {"field2" : 1.79769313486232e308}})"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step3.Insert document whose value is over the range of double + * @tc.expected:step3.GRD_INVALID_ARGS. + */ + const char *document3 = R"({"_id" : "38_2", "t1" : [1, 2, 1.79769313486232e308]})"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step4.Insert document whose value is over the range of double + * @tc.expected:step4.GRD_INVALID_ARGS. + */ + const char *document4 = R"({"_id" : "38_3", "t1" : [1, 2, -1.7976931348623167E+308]})"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step5.Insert document with minimum double value + * @tc.expected:step5.GRD_INVALID_ARGS. + */ + const char *document5 = R"({"_id" : "38_4", "t1" : [1, 2, -1.79769313486231570E+308]})"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document5, 0), GRD_OK); + /** + * @tc.steps:step6.Insert document with maxium double value + * @tc.expected:step6.GRD_INVALID_ARGS. + */ + const char *document6 = R"({"_id" : "38_5", "t1" : [1, 2, 1.79769313486231570E+308]})"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document6, 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest039 + * @tc.desc: Insert a filter which _id value's lens is larger than MAX_ID_LENS + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest039, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert a filter which _id value's lens is larger than MAX_ID_LENS. + * @tc.expected:step1.GRD_OVER_LIMIT. + */ + string document1 = "{\"_id\" : "; + string document2 = "\""; + string document4 = "\""; + string document5 = ", \"name\" : \"Ori\"}"; + string document_midlle(MAX_ID_LENS + 1, 'k'); + string document = document1 + document2 + document_midlle + document4 + document5; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OVER_LIMIT); + + /** + * @tc.steps:step1.Insert a filter which _id value's lens is equal as MAX_ID_LENS. + * @tc.expected:step1.GRD_OK. + */ + string document_midlle2(MAX_ID_LENS, 'k'); + document = document1 + document2 + document_midlle2 + document4 + document5; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OK); +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_test_utils.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_test_utils.h new file mode 100644 index 00000000..a513875e --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_test_utils.h @@ -0,0 +1,27 @@ +/* +* 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 DOCUMENTDB_TEST_UTILS_H +#define DOCUMENTDB_TEST_UTILS_H +#include + + +namespace DocumentDBUnitTest { +class DocumentDBTestUtils { +public: + static int RemoveTestDbFiles(const std::string &dir); +}; +} // namespace DocumentDBUnitTest +#endif // DOCUMENTDB_TEST_UTILS_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.cpp new file mode 100644 index 00000000..f26640ad --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.cpp @@ -0,0 +1,24 @@ +/* +* 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. +*/ + +#include "documentdb_test_utils.h" + +namespace DocumentDBUnitTest { +int DocumentDBTestUtils::RemoveTestDbFiles(const std::string &dir) +{ + (void)remove(dir.c_str()); // TODO: remove dir or files + return 0; +} +} // namespace DocumentDBUnitTest \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.h new file mode 100644 index 00000000..a513875e --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.h @@ -0,0 +1,27 @@ +/* +* 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 DOCUMENTDB_TEST_UTILS_H +#define DOCUMENTDB_TEST_UTILS_H +#include + + +namespace DocumentDBUnitTest { +class DocumentDBTestUtils { +public: + static int RemoveTestDbFiles(const std::string &dir); +}; +} // namespace DocumentDBUnitTest +#endif // DOCUMENTDB_TEST_UTILS_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp new file mode 100644 index 00000000..11717fe7 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp @@ -0,0 +1,330 @@ +/* +* 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. +*/ + +#include +#include + +#include "documentdb_test_utils.h" +#include "doc_errno.h" +#include "json_common.h" +#include "log_print.h" + +using namespace DocumentDB; +using namespace testing::ext; +using namespace DocumentDBUnitTest; + +class DocumentDBJsonCommonTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DocumentDBJsonCommonTest::SetUpTestCase(void) +{ +} + +void DocumentDBJsonCommonTest::TearDownTestCase(void) +{ +} + +void DocumentDBJsonCommonTest::SetUp(void) +{ +} + +void DocumentDBJsonCommonTest::TearDown(void) +{ +} + +/** + * @tc.name: OpenDBTest001 + * @tc.desc: Test open document db + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest001, TestSize.Level0) +{ + std::string document = R""({"name":"Tmn","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::string updateDoc = R""({"name":"Xue","case":{"field1":1,"field2":"string","field3":[1,2,3]},"age":28,"addr":{"city":"shenzhen","postal":518000}})""; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + + EXPECT_EQ(JsonCommon::Append(src, add), E_OK); + GLOGD("result: %s", src.Print().c_str()); + + JsonObject itemCase = src.FindItem({"case", "field1"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemCase.GetItemValue().GetIntValue(), 1); + + JsonObject itemName = src.FindItem({"name"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemName.GetItemValue().GetStringValue(), "Xue"); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest002, TestSize.Level0) +{ + std::string document = R""({"name":"Tmn","case":2,"age":[1,2,3],"addr":{"city":"shanghai","postal":200001}})""; + std::string updateDoc = R""({"name":["Xue","Neco","Lip"],"grade":99,"age":18,"addr": + [{"city":"shanghai","postal":200001},{"city":"beijing","postal":100000}]})""; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(JsonCommon::Append(src, add), E_OK); + GLOGD("result: %s", src.Print().c_str()); + + JsonObject itemCase = src.FindItem({"grade"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemCase.GetItemValue().GetIntValue(), 99); // 99: grade + + JsonObject itemName = src.FindItem({"name", "1"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemName.GetItemValue().GetStringValue(), "Neco"); +} + + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest003, TestSize.Level0) +{ + std::string document = R""({"name":["Tmn","BB","Alice"],"age":[1,2,3],"addr":[{"city":"shanghai","postal":200001},{"city":"wuhan","postal":430000}]})""; + std::string updateDoc = R""({"name":["Xue","Neco","Lip"],"age":18,"addr":[{"city":"shanghai","postal":200001},{"city":"beijing","postal":100000}]})""; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(JsonCommon::Append(src, add), E_OK); + + GLOGD("result: %s", src.Print().c_str()); + JsonObject itemCase = src.FindItem({"addr", "1", "city"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "beijing"); // 99: grade + + JsonObject itemName = src.FindItem({"name", "1"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemName.GetItemValue().GetStringValue(), "Neco"); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest004, TestSize.Level0) +{ + std::string document = R""({"name":["Tmn","BB","Alice"]})""; + std::string updateDoc = R""({"name.5":"GG"})"";; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(JsonCommon::Append(src, add), -E_DATA_CONFLICT); + GLOGD("result: %s", src.Print().c_str()); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest005, TestSize.Level0) +{ + std::string document = R""({"name":["Tmn","BB","Alice"]})""; + std::string updateDoc = R""({"name.2":"GG"})"";; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + + EXPECT_EQ(JsonCommon::Append(src, add), E_OK); + GLOGD("result: %s", src.Print().c_str()); + + JsonObject itemCase = src.FindItem({"name", "2"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "GG"); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest006, TestSize.Level0) +{ + std::string document = R""({"name":{"first":"Tno","last":"moray"}})""; + std::string updateDoc = R""({"name":{"midle.AA":"GG"}})""; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + + EXPECT_EQ(JsonCommon::Append(src, add), E_OK); + GLOGD("result: %s", src.Print().c_str()); + + JsonObject itemCase = src.FindItem({"name", "midle.AA"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "GG"); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest007, TestSize.Level0) +{ + std::string document = R""({"name":{"first":["XX","CC"],"last":"moray"}})""; + std::string updateDoc = R""({"name.first.0":"LL"})""; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + + EXPECT_EQ(JsonCommon::Append(src, add), E_OK); + GLOGD("result: %s", src.Print().c_str()); + + JsonObject itemCase = src.FindItem({"name", "first", "0"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "LL"); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest008, TestSize.Level0) +{ + std::string document = R""({"name":{"first":"XX","last":"moray"}})""; + std::string updateDoc = R""({"name":{"first":["XXX","BBB","CCC"]}})""; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + + EXPECT_EQ(JsonCommon::Append(src, add), E_OK); + GLOGD("result: %s", src.Print().c_str()); + + JsonObject itemCase = src.FindItem({"name", "first", "0"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "XXX"); +} + + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest009, TestSize.Level0) +{ + std::string document = R""({"name":{"first":["XXX","BBB","CCC"],"last":"moray"}})""; + std::string updateDoc = R""({"name":{"first":"XX"}})""; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + + EXPECT_EQ(JsonCommon::Append(src, add), E_OK); + GLOGD("result: %s", src.Print().c_str()); + + JsonObject itemCase = src.FindItem({"name", "first"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "XX"); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest010, TestSize.Level0) +{ + std::string document = R""({"name":{"first":["XXX","BBB","CCC"],"last":"moray"}})""; + std::string updateDoc = R""({"name":{"first":{"XX":"AA"}}})""; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + + EXPECT_EQ(JsonCommon::Append(src, add), E_OK); + GLOGD("result: %s", src.Print().c_str()); + + JsonObject itemCase = src.FindItem({"name", "first", "XX"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "AA"); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest011, TestSize.Level0) +{ + std::string document = R""({"name":{"first":["XXX","BBB","CCC"],"last":"moray"}})""; + std::string updateDoc = R""({"name.last.AA.B":"Mnado"})""; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + + EXPECT_EQ(JsonCommon::Append(src, add), -E_DATA_CONFLICT); + GLOGD("result: %s", src.Print().c_str()); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest012, TestSize.Level0) +{ + std::string document = R""({"name":["Tmn","BB","Alice"]})""; + std::string updateDoc = R""({"name.first":"GG"})"";; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(JsonCommon::Append(src, add), -E_DATA_CONFLICT); + GLOGD("result: %s", src.Print().c_str()); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest013, TestSize.Level0) +{ + std::string document = R""({"name":["Tmn","BB","Alice"]})""; + std::string updateDoc = R""({"name":{"first":"GG"}})"";; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(JsonCommon::Append(src, add), E_OK); + GLOGD("result: %s", src.Print().c_str()); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest014, TestSize.Level0) +{ + std::string document = R""({"name":{"first":"Xue","second":"Lang"}})""; + std::string updateDoc = R""({"name.0":"GG"})"";; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(JsonCommon::Append(src, add), -E_DATA_CONFLICT); + GLOGD("result: %s", src.Print().c_str()); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest015, TestSize.Level0) +{ + std::string document = R""({"name":{"first":"Xue","second":"Lang"}})""; + std::string updateDoc = R""({"name.first":["GG","MM"]})"";; + + int errCode = E_OK; + JsonObject src = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject add = JsonObject::Parse(updateDoc, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(JsonCommon::Append(src, add), E_OK); + GLOGD("result: %s", src.Print().c_str()); + + JsonObject itemCase = src.FindItem({"name", "first", "0"}, errCode); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "GG"); +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp new file mode 100644 index 00000000..fa16a27a --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp @@ -0,0 +1,68 @@ +/* +* 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. +*/ + +#include +#include "documentdb_test_utils.h" +#include "doc_errno.h" +#include "json_object.h" + +using namespace DocumentDB; +using namespace testing::ext; +using namespace DocumentDBUnitTest; + +class DocumentDBJsonObjectTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DocumentDBJsonObjectTest::SetUpTestCase(void) +{ +} + +void DocumentDBJsonObjectTest::TearDownTestCase(void) +{ +} + +void DocumentDBJsonObjectTest::SetUp(void) +{ +} + +void DocumentDBJsonObjectTest::TearDown(void) +{ +} + +/** + * @tc.name: OpenDBTest001 + * @tc.desc: Test open document db + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBJsonObjectTest, JsonObjectTest001, TestSize.Level0) +{ + const std::string config = R""({"a":123,"b":{"c":234,"d":"12345"}})""; + + int ret = E_OK; + JsonObject conf = JsonObject::Parse(config, ret); + EXPECT_EQ(ret, E_OK); + + ValueObject obj = conf.GetObjectByPath({"b", "c"}, ret); + + EXPECT_EQ(obj.GetValueType(), ValueObject::ValueType::VALUE_NUMBER); + EXPECT_EQ(obj.GetIntValue(), 234); +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/idata_share_service.h b/services/distributeddataservice/service/data_share/idata_share_service.h index 87573705..1bfdc05d 100644 --- a/services/distributeddataservice/service/data_share/idata_share_service.h +++ b/services/distributeddataservice/service/data_share/idata_share_service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -22,6 +22,8 @@ #include "datashare_predicates.h" #include "datashare_result_set.h" #include "datashare_values_bucket.h" +#include "datashare_template.h" +#include "data_proxy_observer.h" namespace OHOS::DataShare { class IDataShareService { @@ -31,6 +33,18 @@ public: DATA_SHARE_SERVICE_CMD_DELETE, DATA_SHARE_SERVICE_CMD_UPDATE, DATA_SHARE_SERVICE_CMD_QUERY, + DATA_SHARE_SERVICE_CMD_ADD_TEMPLATE, + DATA_SHARE_SERVICE_CMD_DEL_TEMPLATE, + DATA_SHARE_SERVICE_CMD_PUBLISH, + DATA_SHARE_SERVICE_CMD_GET_DATA, + DATA_SHARE_SERVICE_CMD_SUBSCRIBE_RDB, + DATA_SHARE_SERVICE_CMD_UNSUBSCRIBE_RDB, + DATA_SHARE_SERVICE_CMD_ENABLE_SUBSCRIBE_RDB, + DATA_SHARE_SERVICE_CMD_DISABLE_SUBSCRIBE_RDB, + DATA_SHARE_SERVICE_CMD_SUBSCRIBE_PUBLISHED, + DATA_SHARE_SERVICE_CMD_UNSUBSCRIBE_PUBLISHED, + DATA_SHARE_SERVICE_CMD_ENABLE_SUBSCRIBE_PUBLISHED, + DATA_SHARE_SERVICE_CMD_DISABLE_SUBSCRIBE_PUBLISHED, DATA_SHARE_SERVICE_CMD_MAX }; @@ -43,6 +57,26 @@ public: virtual int32_t Delete(const std::string &uri, const DataSharePredicates &predicate) = 0; virtual std::shared_ptr Query(const std::string &uri, const DataSharePredicates &predicates, const std::vector &columns, int &errCode) = 0; + virtual int32_t AddTemplate(const std::string &uri, const int64_t subscriberId, const Template &tplt) = 0; + virtual int32_t DelTemplate(const std::string &uri, const int64_t subscriberId) = 0; + virtual std::vector Publish(const Data &data, const std::string &bundleNameOfProvider) = 0; + virtual Data GetData(const std::string &bundleNameOfProvider) = 0; + virtual std::vector SubscribeRdbData( + const std::vector &uris, const TemplateId &id, const sptr observer) = 0; + virtual std::vector UnSubscribeRdbData( + const std::vector &uris, const TemplateId &id) = 0; + virtual std::vector EnableSubscribeRdbData( + const std::vector &uris, const TemplateId &id) = 0; + virtual std::vector DisableSubscribeRdbData( + const std::vector &uris, const TemplateId &id) = 0; + virtual std::vector SubscribePublishedData(const std::vector &uris, + const int64_t subscriberId, const sptr observer) = 0; + virtual std::vector UnSubscribePublishedData(const std::vector &uris, + const int64_t subscriberId) = 0; + virtual std::vector EnableSubscribePublishedData(const std::vector &uris, + const int64_t subscriberId) = 0; + virtual std::vector DisableSubscribePublishedData(const std::vector &uris, + const int64_t subscriberId) = 0; }; } // namespace OHOS::DataShare #endif diff --git a/services/distributeddataservice/service/data_share/permission_proxy.cpp b/services/distributeddataservice/service/data_share/permission_proxy.cpp deleted file mode 100644 index 76806bfa..00000000 --- a/services/distributeddataservice/service/data_share/permission_proxy.cpp +++ /dev/null @@ -1,151 +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 "PermissionProxy" -#include "permission_proxy.h" - -#include "accesstoken_kit.h" -#include "account/account_delegate.h" -#include "bundle_info.h" -#include "bundle_mgr_proxy.h" -#include "device_manager_adapter.h" -#include "log_print.h" -#include "metadata/appid_meta_data.h" -#include "metadata/meta_data_manager.h" -#include "utils/anonymous.h" - -namespace OHOS::DataShare { -BundleMgrProxy PermissionProxy::bmsProxy_; -bool PermissionProxy::GetBundleInfo(const std::string &bundleName, uint32_t tokenId, - AppExecFwk::BundleInfo &bundleInfo) -{ - if (!bmsProxy_.GetBundleInfoFromBMS(bundleName, tokenId, bundleInfo)) { - ZLOGE("GetBundleInfoFromBMS failed!"); - return false; - } - return true; -} - -PermissionProxy::PermissionState PermissionProxy::QueryWritePermission(uint32_t tokenId, - const AppExecFwk::BundleInfo &bundleInfo) -{ - for (auto const &item : bundleInfo.extensionInfos) { - if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { - if (item.writePermission.empty()) { - ZLOGW("WritePermission is empty! BundleName is %{public}s, tokenId is %{public}x", - bundleInfo.name.c_str(), tokenId); - return PermissionState::NOT_FIND; - } - int status = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, item.writePermission); - if (status != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { - ZLOGE("Verify write permission denied!"); - return PermissionState::DENIED; - } - return PermissionState::GRANTED; - } - } - return PermissionState::DENIED; -} - -PermissionProxy::PermissionState PermissionProxy::QueryReadPermission(uint32_t tokenId, - const AppExecFwk::BundleInfo &bundleInfo) -{ - for (auto const &item : bundleInfo.extensionInfos) { - if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { - if (item.readPermission.empty()) { - ZLOGW("ReadPermission is empty! BundleName is %{public}s, tokenId is %{public}x", - bundleInfo.name.c_str(), tokenId); - return PermissionState::NOT_FIND; - } - int status = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, item.readPermission); - if (status != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { - ZLOGE("Verify Read permission denied!"); - return PermissionState::DENIED; - } - return PermissionState::GRANTED; - } - } - return PermissionState::DENIED; -} - -std::string PermissionProxy::GetTableNameByCrossUserMode(const ProfileInfo &profileInfo, - int32_t userId, bool isSingleApp, const UriInfo &uriInfo) -{ - std::string tableName = uriInfo.tableName; - if (!isSingleApp) { - return tableName; - } - - AccessSystemMode crossUserMode = GetCrossUserMode(profileInfo, uriInfo); - if (crossUserMode == AccessSystemMode::UNIQUE) { - ZLOGD("hap in unique mode, bundleName is %{public}s, userId is %{public}d", - uriInfo.bundleName.c_str(), userId); - return tableName.append("_").append(std::to_string(userId)); - } - return tableName; -} - -PermissionProxy::AccessSystemMode PermissionProxy::GetCrossUserMode(const ProfileInfo &profileInfo, - const UriInfo &uriInfo) -{ - AccessSystemMode crossUserMode = AccessSystemMode::UNDEFINED; - for (auto &item : profileInfo.tableConfig) { - UriInfo temp; - AccessSystemMode curUserMode = static_cast(item.crossUserMode); - if (curUserMode == AccessSystemMode::UNDEFINED) { - continue; - } - if (item.uri == "*") { - crossUserMode = crossUserMode == AccessSystemMode::UNDEFINED ? curUserMode : crossUserMode; - continue; - } - if (!URIUtils::GetInfoFromURI(item.uri, temp, true)) { - ZLOGE("GetInfoFromURI failed, uri is %{public}s", DistributedData::Anonymous::Change(item.uri).c_str()); - continue; - } - if (temp.storeName != uriInfo.storeName) { - continue; - } - if (temp.tableName.empty()) { - crossUserMode = curUserMode; - continue; - } - if (temp.tableName == uriInfo.tableName) { - return curUserMode; - } - } - return crossUserMode; -} - -void PermissionProxy::FillData(DistributedData::StoreMetaData &meta, int32_t userId) -{ - meta.deviceId = DistributedData::DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid; - meta.user = std::to_string(userId); -} - -bool PermissionProxy::QueryMetaData(const std::string &bundleName, const std::string &storeName, - DistributedData::StoreMetaData &metaData, int32_t userId) -{ - DistributedData::StoreMetaData meta; - FillData(meta, userId); - meta.bundleName = bundleName; - meta.storeId = storeName; - bool isCreated = DistributedData::MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), metaData); - if (!isCreated) { - ZLOGE("Interface token is not equal"); - return false; - } - return true; -} -} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp new file mode 100644 index 00000000..cd2fc79b --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp @@ -0,0 +1,94 @@ +/* + * 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 "LoadConfigFromDataProxyNodeStrategy" + +#include "load_config_from_data_proxy_node_strategy.h" + +#include "datashare_errno.h" + +#include "bundle_mgr_proxy.h" +#include "common/uri_utils.h" +#include "log_print.h" + +namespace OHOS::DataShare { +bool LoadConfigFromDataProxyNodeStrategy::operator()(std::shared_ptr context) +{ + if (!LoadConfigFromUri(context)) { + return false; + } + context->type = DataProperties::PUBLISHED_DATA_TYPE; + ZLOGE("hanlu enter %{public}s %{public}d", context->calledBundleName.c_str(), context->callerTokenId); + if (!BundleMgrProxy::GetInstance()->GetBundleInfoFromBMS( + context->calledBundleName, context->currentUserId, context->bundleInfo)) { + ZLOGE("GetBundleInfoFromBMS failed! bundleName: %{public}s", context->calledBundleName.c_str()); + return false; + } + for (auto &hapModuleInfo : context->bundleInfo.hapModuleInfos) { + ZLOGE("hanlu uri: %{public}d", hapModuleInfo.proxyDatas.size()); + auto proxyDatas = hapModuleInfo.proxyDatas; + for (auto &proxyData : proxyDatas) { + ZLOGE("hanlu uri: %{public}s inputuri:%{public}s", proxyData.uri.c_str(), context->uri.c_str()); + if (proxyData.uri != context->uri) { + continue; + } + context->permission = context->isRead ? proxyData.requiredReadPermission + : proxyData.requiredWritePermission; + if (context->permission.empty()) { + context->permission = "reject"; + } + DataProperties properties; + bool isCompressed = !hapModuleInfo.hapPath.empty(); + std::string resourcePath = isCompressed ? hapModuleInfo.hapPath : hapModuleInfo.resourcePath; + if (!DataShareProfileInfo::GetDataPropertiesFromProxyDatas( + proxyData, resourcePath, isCompressed, properties)) { + return true; + } + GetContextInfoFromDataProperties(properties, hapModuleInfo.moduleName, context); + ZLOGE("hanlu calledStoreName: %{public}s %{public}s", context->calledStoreName.c_str(), + context->calledTableName.c_str()); + return true; + } + } + if (context->callerBundleName == context->calledBundleName) { + ZLOGI("access private data, caller and called is same, go"); + return true; + } + context->errCode = E_URI_NOT_EXIST; + ZLOGI("not find DataProperties! %{private}s is private", context->uri.c_str()); + return false; +} + +bool LoadConfigFromDataProxyNodeStrategy::GetContextInfoFromDataProperties(const DataProperties &properties, + const std::string &moduleName, std::shared_ptr context) +{ + if (properties.scope == DataProperties::MODULE_SCOPE) { + // module scope + context->calledModuleName = moduleName; + } + context->calledStoreName = properties.storeName; + context->calledTableName = properties.tableName; + context->type = properties.type; + return true; +} + +bool LoadConfigFromDataProxyNodeStrategy::LoadConfigFromUri(std::shared_ptr context) +{ + if (!URIUtils::GetBundleNameFromProxyURI(context->uri, context->calledBundleName)) { + return false; + } + ZLOGE("hanlu enter %{publis}s", context->calledBundleName.c_str()); + return true; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/permission_proxy.h b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.h similarity index 32% rename from services/distributeddataservice/service/data_share/permission_proxy.h rename to services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.h index 1c6d4c78..8c8066cf 100644 --- a/services/distributeddataservice/service/data_share/permission_proxy.h +++ b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -13,43 +13,29 @@ * limitations under the License. */ -#ifndef DATASHARESERVICE_PERMISSION_PROXY_H -#define DATASHARESERVICE_PERMISSION_PROXY_H +#ifndef DATASHARESERVICE_LOAD_CONFIG_FROM_DATA_PROXY_NODE_STRAGETY_H +#define DATASHARESERVICE_LOAD_CONFIG_FROM_DATA_PROXY_NODE_STRAGETY_H -#include - -#include "bundle_info.h" -#include "bundle_mgr_proxy.h" #include "data_share_profile_info.h" -#include "metadata/store_meta_data.h" +#include "strategy.h" namespace OHOS::DataShare { -class PermissionProxy { +using namespace OHOS::RdbBMSAdapter; +class LoadConfigFromDataProxyNodeStrategy final : public Strategy { public: - enum class AccessSystemMode { - UNDEFINED = 0, - SHARED, - UNIQUE, - }; - - enum class PermissionState { - DENIED = -1, - NOT_FIND = 0, - GRANTED, - }; - - static bool GetBundleInfo(const std::string &bundleName, uint32_t tokenId, AppExecFwk::BundleInfo &bundleInfo); - static PermissionState QueryWritePermission(uint32_t tokenId, const AppExecFwk::BundleInfo &bundleInfo); - static PermissionState QueryReadPermission(uint32_t tokenId, const AppExecFwk::BundleInfo &bundleInfo); - static bool QueryMetaData(const std::string &bundleName, const std::string &storeName, - DistributedData::StoreMetaData &metaData, int32_t userId); - static std::string GetTableNameByCrossUserMode(const ProfileInfo &profileInfo, - int32_t userId, bool isSingleApp, const UriInfo &uriInfo); + LoadConfigFromDataProxyNodeStrategy() = default; + bool operator()(std::shared_ptr context) override; private: - static void FillData(DistributedData::StoreMetaData &data, int32_t userId); - static AccessSystemMode GetCrossUserMode(const ProfileInfo &profileInfo, const UriInfo &uriInfo); - static BundleMgrProxy bmsProxy_; + enum PATH_PARAMS : int32_t + { + STORE_NAME = 0, + TABLE_NAME, + PARAM_SIZE + }; + bool LoadConfigFromUri(std::shared_ptr context); + bool GetContextInfoFromDataProperties( + const DataProperties &properties, const std::string &moduleName, std::shared_ptr context); }; } // namespace OHOS::DataShare -#endif // DATASHARESERVICE_PERMISSION_PROXY_H \ No newline at end of file +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp new file mode 100644 index 00000000..4ab18e5e --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp @@ -0,0 +1,117 @@ +/* + * 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 "LoadConfigFromDataShareBundleInfoStrategy" + +#include "load_config_from_data_share_bundle_info_strategy.h" + +#include "bundle_mgr_proxy.h" +#include "log_print.h" +#include "uri_utils.h" + +namespace OHOS::DataShare { +struct ConfigData { + constexpr static int8_t TABLE_MATCH_PRIORITY = 3; + constexpr static int8_t STORE_MATCH_PRIORITY = 2; + constexpr static int8_t COMMON_MATCH_PRIORITY = 1; + constexpr static int8_t UNDEFINED_PRIORITY = -1; + ConfigData() : crossMode_(AccessSystemMode::UNDEFINED, UNDEFINED_PRIORITY) {} + void SetCrossUserMode(uint8_t priority, uint8_t crossMode) + { + if (crossMode_.second < priority && crossMode > AccessSystemMode::UNDEFINED && + crossMode < AccessSystemMode::MAX) { + crossMode_.first = static_cast(crossMode); + crossMode_.second = priority; + } + } + void FillIntoContext(std::shared_ptr context) + { + if (crossMode_.second != ConfigData::UNDEFINED_PRIORITY) { + context->accessSystemMode = crossMode_.first; + } + } + +private: + std::pair crossMode_; +}; + +bool LoadConfigFromDataShareBundleInfoStrategy::LoadConfigFromProfile( + const ProfileInfo &profileInfo, std::shared_ptr context) +{ + std::string storeUri = URIUtils::DATA_SHARE_SCHEMA + context->calledBundleName + "/" + context->calledModuleName + "/" + + context->calledStoreName; + std::string tableUri = storeUri + "/" + context->calledTableName; + ConfigData result; + for (auto &item : profileInfo.tableConfig) { + if (item.uri == tableUri) { + result.SetCrossUserMode(ConfigData::TABLE_MATCH_PRIORITY, item.crossUserMode); + continue; + } + if (item.uri == storeUri) { + result.SetCrossUserMode(ConfigData::STORE_MATCH_PRIORITY, item.crossUserMode); + continue; + } + if (item.uri == "*") { + result.SetCrossUserMode(ConfigData::COMMON_MATCH_PRIORITY, item.crossUserMode); + continue; + } + } + result.FillIntoContext(context); + return true; +} + +bool LoadConfigFromDataShareBundleInfoStrategy::operator()(std::shared_ptr context) +{ + if (!LoadConfigFromUri(context)) { + ZLOGE("LoadConfigFromUri failed! bundleName: %{public}s", context->calledBundleName.c_str()); + return false; + } + if (!BundleMgrProxy::GetInstance()->GetBundleInfoFromBMS( + context->calledBundleName, context->currentUserId, context->bundleInfo)) { + ZLOGE("GetBundleInfoFromBMS failed! bundleName: %{public}s", context->calledBundleName.c_str()); + return false; + } + for (auto &item : context->bundleInfo.extensionInfos) { + if (item.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { + context->permission = context->isRead ? item.readPermission : item.writePermission; + + std::vector infos; + auto ret = DataShareProfileInfo::GetResConfigFile(item, infos); + if (!ret) { + return true; // optional meta data config + } + ProfileInfo profileInfo; + if (!profileInfo.Unmarshall(infos[0])) { + ZLOGE("parse failed! %{public}s", infos[0].c_str()); + return false; + } + LoadConfigFromProfile(profileInfo, context); + return true; + } + } + return false; +} +bool LoadConfigFromDataShareBundleInfoStrategy::LoadConfigFromUri(std::shared_ptr context) +{ + UriInfo uriInfo; + if (!URIUtils::GetInfoFromURI(context->uri, uriInfo)) { + return false; + } + context->calledBundleName = std::move(uriInfo.bundleName); + context->calledModuleName = std::move(uriInfo.moduleName); + context->calledStoreName = std::move(uriInfo.storeName); + context->calledTableName = std::move(uriInfo.tableName); + return true; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.h b/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.h new file mode 100644 index 00000000..4b62a6f5 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.h @@ -0,0 +1,33 @@ +/* + * 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 DATASHARESERVICE_LOAD_CONFIG_FROM_DATA_SHARE_BUNDLE_INFO_STRAGETY_H +#define DATASHARESERVICE_LOAD_CONFIG_FROM_DATA_SHARE_BUNDLE_INFO_STRAGETY_H + +#include "data_share_profile_info.h" +#include "strategy.h" +namespace OHOS::DataShare { +using namespace OHOS::RdbBMSAdapter; +class LoadConfigFromDataShareBundleInfoStrategy final: public Strategy { +public: + LoadConfigFromDataShareBundleInfoStrategy() = default; + bool operator()(std::shared_ptr context) override; + +private: + bool LoadConfigFromProfile(const ProfileInfo &profileInfo, std::shared_ptr context); + bool LoadConfigFromUri(std::shared_ptr context); +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp new file mode 100644 index 00000000..b46f729f --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp @@ -0,0 +1,68 @@ +/* + * 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 "DeleteStrategy" + +#include "delete_strategy.h" + +#include "db_delegate.h" +#include "general/load_config_common_strategy.h" +#include "general/load_config_data_info_strategy.h" +#include "general/load_config_from_bundle_info_strategy.h" +#include "general/permission_strategy.h" +#include "general/process_single_app_user_cross_strategy.h" +#include "log_print.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +int64_t DeleteStrategy::Execute(std::shared_ptr context, const DataSharePredicates &predicate) +{ + auto preProcess = GetStrategy(); + if (preProcess == nullptr) { + ZLOGE("get strategy fail, maybe memory not enough"); + return -1; + } + if (!(*preProcess)(context)) { + ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); + return -1; + } + int errCode = E_OK; + auto delegate = DBDelegate::Create(context->calledSourceDir, context->version, errCode); + if (delegate == nullptr) { + ZLOGE("Create fail %{public}s %{public}s", context->calledBundleName.c_str(), context->calledTableName.c_str()); + return -1; + } + return delegate->Delete(context->calledTableName, predicate); +} +Strategy *DeleteStrategy::GetStrategy() +{ + static std::mutex mutex; + static SeqStrategy strategies; + std::lock_guard lock(mutex); + if (!strategies.IsEmpty()) { + return &strategies; + } + auto ret = strategies.Init({ + new (std::nothrow)LoadConfigCommonStrategy(), + new (std::nothrow)LoadConfigFromBundleInfoStrategy(), + new (std::nothrow)PermissionStrategy(), + new (std::nothrow)LoadConfigDataInfoStrategy(), + new (std::nothrow)ProcessSingleAppUserCrossStrategy() + }); + if (!ret) { + return nullptr; + } + return &strategies; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/delete_strategy.h b/services/distributeddataservice/service/data_share/strategies/delete_strategy.h new file mode 100644 index 00000000..08e98e3f --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/delete_strategy.h @@ -0,0 +1,33 @@ +/* + * 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 DATASHARESERVICE_DELETE_STRAGETY_H +#define DATASHARESERVICE_DELETE_STRAGETY_H + +#include + +#include "datashare_predicates.h" +#include "seq_strategy.h" + +namespace OHOS::DataShare { +class DeleteStrategy final { +public: + static int64_t Execute(std::shared_ptr context, const DataSharePredicates &predicate); + +private: + static Strategy *GetStrategy(); +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/general/check_is_data_proxy_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/check_is_data_proxy_strategy.cpp new file mode 100644 index 00000000..6842ed8b --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/check_is_data_proxy_strategy.cpp @@ -0,0 +1,24 @@ +/* + * 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. + */ +#include "check_is_data_proxy_strategy.h" + +#include "uri_utils.h" + +namespace OHOS::DataShare { +bool CheckIsDataProxyStrategy::operator()(std::shared_ptr context) +{ + return URIUtils::IsDataProxyURI(context->uri); +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/general/check_is_data_proxy_strategy.h b/services/distributeddataservice/service/data_share/strategies/general/check_is_data_proxy_strategy.h new file mode 100644 index 00000000..35da7671 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/check_is_data_proxy_strategy.h @@ -0,0 +1,26 @@ +/* + * 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 DATASHARESERVICE_CHECK_IS_DATA_PROXY_STRAGETY_H +#define DATASHARESERVICE_CHECK_IS_DATA_PROXY_STRAGETY_H + +#include "strategy.h" +namespace OHOS::DataShare { +class CheckIsDataProxyStrategy final : public Strategy { +public: + bool operator()(std::shared_ptr context) override; +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/general/check_is_single_app_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/check_is_single_app_strategy.cpp new file mode 100644 index 00000000..76da4f48 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/check_is_single_app_strategy.cpp @@ -0,0 +1,25 @@ +/* + * 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 "CheckIsSystemAppStrategy" + +#include "check_is_single_app_strategy.h" +#include "log_print.h" + +namespace OHOS::DataShare { +bool CheckIsSingleAppStrategy::operator()(std::shared_ptr context) +{ + return context->bundleInfo.singleton; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/general/check_is_single_app_strategy.h b/services/distributeddataservice/service/data_share/strategies/general/check_is_single_app_strategy.h new file mode 100644 index 00000000..4fa66f46 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/check_is_single_app_strategy.h @@ -0,0 +1,26 @@ +/* + * 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 DATASHARESERVICE_CHECK_IS_SYSTEM_APP_STRAGETY_H +#define DATASHARESERVICE_CHECK_IS_SYSTEM_APP_STRAGETY_H + +#include "strategy.h" +namespace OHOS::DataShare { +class CheckIsSingleAppStrategy final : public Strategy { +public: + bool operator()(std::shared_ptr context) override; +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp new file mode 100644 index 00000000..cca52b21 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp @@ -0,0 +1,104 @@ +/* + * 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 "ConnectExtensionStrategy" + +#include "connect_extension_strategy.h" + +#include + +#include "ability_connect_callback_stub.h" +#include "ability_manager_client.h" +#include "log_print.h" + +namespace OHOS::DataShare { +bool ConnectExtensionStrategy::operator()(std::shared_ptr context) +{ + for (auto &extension: context->bundleInfo.extensionInfos) { + if (extension.type == AppExecFwk::ExtensionAbilityType::DATASHARE) { + return Connect(context); + } + } + return false; +} + +ConnectExtensionStrategy::ConnectExtensionStrategy() : data_(1) {} +class CallBackImpl : public AAFwk::AbilityConnectionStub { +public: + CallBackImpl(BlockData &data) : data_(data) {} + void OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) override + { + bool result = true; + data_.SetValue(result); + } + void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override + { + bool result = false; + data_.SetValue(result); + } + +private: + BlockData &data_; +}; + +bool ConnectExtensionStrategy::Connect(std::shared_ptr context) +{ + std::lock_guard lock(mutex_); + AAFwk::Want want; + want.SetUri(context->uri); + data_.Clear(); + sptr callback = new CallBackImpl(data_); + ZLOGI("Start connect %{public}s", context->uri.c_str()); + ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, callback, nullptr); + if (ret != ERR_OK) { + ZLOGE("connect ability failed, ret = %{public}d", ret); + return false; + } + bool result = data_.GetValue(); + if (result) { + ZLOGI("connect ability ended successfully"); + } + data_.Clear(); + AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(callback); + if (!data_.GetValue()) { + ZLOGI("disconnect ability ended successfully"); + } + return result; +} + +bool ConnectExtensionStrategy::Execute( + std::shared_ptr context, std::function isFinished, int maxWaitTimeMs) +{ + ConnectExtensionStrategy strategy; + if (!strategy(context)) { + return false; + } + if (isFinished == nullptr) { + return true; + } + int waitTime = 0; + constexpr static int RETRY_TIME = 500; + while (!isFinished()) { + if (waitTime > maxWaitTimeMs) { + ZLOGE("cannot finish work"); + return false; + } + ZLOGI("has wait %{public}d ms", waitTime); + std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_TIME)); + waitTime += RETRY_TIME; + } + return true; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.h b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.h new file mode 100644 index 00000000..57d76c48 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.h @@ -0,0 +1,33 @@ +/* + * 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 DATASHARESERVICE_CONNECT_EXTENSION_STRAGETY_H +#define DATASHARESERVICE_CONNECT_EXTENSION_STRAGETY_H + +#include "block_data.h" +#include "strategy.h" +namespace OHOS::DataShare { +class ConnectExtensionStrategy final : public Strategy { +public: + ConnectExtensionStrategy(); + bool operator()(std::shared_ptr context) override; + static bool Execute(std::shared_ptr context, std::function isFinished, int maxWaitTimeMs = 2000); +private: + inline bool Connect(std::shared_ptr context); + std::mutex mutex_; + BlockData data_; +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/general/empty_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/empty_strategy.cpp new file mode 100644 index 00000000..260e5442 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/empty_strategy.cpp @@ -0,0 +1,24 @@ +/* + * 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 "EmptyStrategy" + +#include "empty_strategy.h" + +namespace OHOS::DataShare { +bool EmptyStrategy::operator()(std::shared_ptr context) +{ + return true; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/general/empty_strategy.h b/services/distributeddataservice/service/data_share/strategies/general/empty_strategy.h new file mode 100644 index 00000000..a08e2bfc --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/empty_strategy.h @@ -0,0 +1,26 @@ +/* + * 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 DATASHARESERVICE_EMPTY_STRAGETY_H +#define DATASHARESERVICE_EMPTY_STRAGETY_H + +#include "strategy.h" +namespace OHOS::DataShare { +class EmptyStrategy final : public Strategy { +public: + bool operator()(std::shared_ptr context) override; +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp new file mode 100644 index 00000000..0602a1f7 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp @@ -0,0 +1,46 @@ +/* + * 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 "LoadConfigCommonStrategy" +#include "load_config_common_strategy.h" + +#include "account/account_delegate.h" +#include "ipc_skeleton.h" +#include "log_print.h" +#include "uri_utils.h" + +namespace OHOS::DataShare { +bool LoadConfigCommonStrategy::operator()(std::shared_ptr context) +{ + context->callerTokenId = IPCSkeleton::GetCallingTokenID(); + context->currentUserId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(context->callerTokenId); + // single app, userId is in uri + if (context->currentUserId == 0) { + URIUtils::GetUserIdFromProxyURI(context->uri, context->currentUserId); + ZLOGI("user uri's userId %{public}d", context->currentUserId); + } + FormatUri(context->uri); + return true; +} + +void LoadConfigCommonStrategy::FormatUri(std::string &uri) +{ + auto pos = uri.find('?'); + if (pos == std::string::npos) { + return; + } + + uri = uri.substr(0, pos); +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.h b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.h new file mode 100644 index 00000000..9847158a --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.h @@ -0,0 +1,29 @@ +/* + * 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 DATASHARESERVICE_LOAD_CONFIG_COMMON_STRAGETY_H +#define DATASHARESERVICE_LOAD_CONFIG_COMMON_STRAGETY_H + +#include "strategy.h" +namespace OHOS::DataShare { +class LoadConfigCommonStrategy final : public Strategy { +public: + bool operator()(std::shared_ptr context) override; + +private: + void FormatUri(std::string &uri); +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp new file mode 100644 index 00000000..2db6630b --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp @@ -0,0 +1,89 @@ +/* + * 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 "LoadConfigDataInfoStrategy" +#include "load_config_data_info_strategy.h" + +#include "check_is_single_app_strategy.h" +#include "connect_extension_strategy.h" +#include "device_manager_adapter.h" +#include "log_print.h" +#include "metadata/meta_data_manager.h" +#include "metadata/store_meta_data.h" +#include "rdb_errno.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +LoadConfigDataInfoStrategy::LoadConfigDataInfoStrategy() + : DivStrategy(std::make_shared(), std::make_shared(), + std::make_shared()) +{ +} +static bool QueryMetaData(const std::string &bundleName, const std::string &storeName, + DistributedData::StoreMetaData &metaData, const int32_t userId) +{ + DistributedData::StoreMetaData meta; + meta.deviceId = DistributedData::DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid; + meta.user = std::to_string(userId); + meta.bundleName = bundleName; + meta.storeId = storeName; + bool isCreated = DistributedData::MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), metaData); + if (!isCreated) { + ZLOGE("DB not exist"); + return false; + } + return true; +} + +bool LoadConfigNormalDataInfoStrategy::operator()(std::shared_ptr context) +{ + if (context->type != "rdb") { + return false; + } + DistributedData::StoreMetaData metaData; + if (!QueryMetaData(context->calledBundleName, context->calledStoreName, metaData, context->currentUserId)) { + // connect extension and retry + if (!ConnectExtensionStrategy::Execute(context, [&context, &metaData]() { + return QueryMetaData( + context->calledBundleName, context->calledStoreName, metaData, context->currentUserId); + })) { + ZLOGE("QueryMetaData fail, %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); + context->errCode = NativeRdb::E_DB_NOT_EXIST; + return false; + } + } + context->calledSourceDir = metaData.dataDir; + ZLOGE("hanlu dir %{public}s", context->calledSourceDir.c_str()); + context->version = metaData.version; + return true; +} + +bool LoadConfigSingleDataInfoStrategy::operator()(std::shared_ptr context) +{ + DistributedData::StoreMetaData metaData; + if (!QueryMetaData(context->calledBundleName, context->calledStoreName, metaData, 0)) { + // connect extension and retry + if (!ConnectExtensionStrategy::Execute(context, [&context, &metaData]() { + return QueryMetaData(context->calledBundleName, context->calledStoreName, metaData, 0); + })) { + ZLOGE("QueryMetaData fail, %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); + context->errCode = NativeRdb::E_DB_NOT_EXIST; + return false; + } + } + context->calledSourceDir = metaData.dataDir; + context->version = metaData.version; + return true; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.h b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.h new file mode 100644 index 00000000..54d0dad8 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.h @@ -0,0 +1,34 @@ +/* + * 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 DATASHARESERVICE_LOAD_CONFIG_DATA_INFO_STRAGETY_H +#define DATASHARESERVICE_LOAD_CONFIG_DATA_INFO_STRAGETY_H + +#include "div_strategy.h" +namespace OHOS::DataShare { +class LoadConfigSingleDataInfoStrategy final : public Strategy { +public: + bool operator()(std::shared_ptr context) override; +}; +class LoadConfigNormalDataInfoStrategy final : public Strategy { +public: + bool operator()(std::shared_ptr context) override; +}; +class LoadConfigDataInfoStrategy final : public DivStrategy { +public: + LoadConfigDataInfoStrategy(); +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_from_bundle_info_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/load_config_from_bundle_info_strategy.cpp new file mode 100644 index 00000000..e70849ed --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_from_bundle_info_strategy.cpp @@ -0,0 +1,27 @@ +/* + * 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. + */ +#include "load_config_from_bundle_info_strategy.h" + +#include "check_is_data_proxy_strategy.h" +#include "data_proxy/load_config_from_data_proxy_node_strategy.h" +#include "data_share/load_config_from_data_share_bundle_info_strategy.h" + +namespace OHOS::DataShare { +LoadConfigFromBundleInfoStrategy::LoadConfigFromBundleInfoStrategy() + : DivStrategy(std::make_shared(), std::make_shared(), + std::make_shared()) +{ +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_from_bundle_info_strategy.h b/services/distributeddataservice/service/data_share/strategies/general/load_config_from_bundle_info_strategy.h new file mode 100644 index 00000000..d2801222 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_from_bundle_info_strategy.h @@ -0,0 +1,26 @@ +/* + * 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 DATASHARESERVICE_LOAD_CONFIG_FROM_BUNDLE_INFO_STRAGETY_H +#define DATASHARESERVICE_LOAD_CONFIG_FROM_BUNDLE_INFO_STRAGETY_H + +#include "div_strategy.h" +namespace OHOS::DataShare { +class LoadConfigFromBundleInfoStrategy final: public DivStrategy { +public: + LoadConfigFromBundleInfoStrategy(); +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.cpp new file mode 100644 index 00000000..afa21456 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.cpp @@ -0,0 +1,41 @@ +/* + * 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 "PermissionStrategy" + +#include "permission_strategy.h" + +#include "accesstoken_kit.h" +#include "log_print.h" + +namespace OHOS::DataShare { +bool PermissionStrategy::operator()(std::shared_ptr context) +{ + ZLOGE("hanlu check! callerTokenId:%{public}u permission:%{public}s", + context->callerTokenId, context->permission.c_str()); + if (context->permission == "reject") { + return false; + } + if (!context->permission.empty()) { + int status = + Security::AccessToken::AccessTokenKit::VerifyAccessToken(context->callerTokenId, context->permission); + if (status != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + ZLOGE("Verify permission denied! callerTokenId:%{public}u permission:%{public}s", + context->callerTokenId, context->permission.c_str()); + return false; + } + } + return true; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.h b/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.h new file mode 100644 index 00000000..467d868e --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.h @@ -0,0 +1,27 @@ +/* + * 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 DATASHARESERVICE_PERMISSION_STRAGETY_H +#define DATASHARESERVICE_PERMISSION_STRAGETY_H + +#include "strategy.h" +namespace OHOS::DataShare { +class PermissionStrategy final : public Strategy { +public: + PermissionStrategy() = default; + bool operator()(std::shared_ptr context) override; +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/general/process_single_app_user_cross_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/process_single_app_user_cross_strategy.cpp new file mode 100644 index 00000000..f802778b --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/process_single_app_user_cross_strategy.cpp @@ -0,0 +1,46 @@ +/* + * 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 "ProcessUserCrossStrategy" +#include "process_single_app_user_cross_strategy.h" + +#include "bundlemgr/bundle_mgr_client.h" +#include "check_is_single_app_strategy.h" +#include "empty_strategy.h" +#include "log_print.h" + +namespace OHOS::DataShare { +ProcessSingleAppUserCrossStrategy::ProcessSingleAppUserCrossStrategy() + : DivStrategy(std::make_shared(), std::make_shared(), + std::make_shared()) +{ +} + +bool ProcessUserCrossStrategy::operator()(std::shared_ptr context) +{ + if (context->accessSystemMode == UNDEFINED) { + ZLOGE("single app must config user cross mode, please check it, bundleName: %{public}s", + context->calledBundleName.c_str()); + return false; + } + ZLOGE("hanlu enter %{public}d", context->accessSystemMode); + if (context->accessSystemMode == USER_SINGLE_MODE) { + ZLOGE("hanlu table userid %{public}d", context->currentUserId); + context->calledTableName.append("_").append(std::to_string(context->currentUserId)); + ZLOGE("hanlu table userid %{public}s", context->calledTableName.c_str()); + return true; + } + return true; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/general/process_single_app_user_cross_strategy.h b/services/distributeddataservice/service/data_share/strategies/general/process_single_app_user_cross_strategy.h new file mode 100644 index 00000000..1e2be33b --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/general/process_single_app_user_cross_strategy.h @@ -0,0 +1,30 @@ +/* + * 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 DATASHARESERVICE_PROCESS_SINGLE_APP_USER_CROSS_STRAGETY_H +#define DATASHARESERVICE_PROCESS_SINGLE_APP_USER_CROSS_STRAGETY_H + +#include "div_strategy.h" +namespace OHOS::DataShare { +class ProcessUserCrossStrategy final : public Strategy { +public: + bool operator()(std::shared_ptr context) override; +}; +class ProcessSingleAppUserCrossStrategy final : public DivStrategy { +public: + ProcessSingleAppUserCrossStrategy(); +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp new file mode 100644 index 00000000..266fcbb0 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp @@ -0,0 +1,73 @@ +/* + * 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 "GetDataStrategy" + +#include "get_data_strategy.h" + +#include "general/load_config_common_strategy.h" +#include "general/permission_strategy.h" +#include "data_proxy/load_config_from_data_proxy_node_strategy.h" +#include "log_print.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +Data GetDataStrategy::Execute(std::shared_ptr context) +{ + auto preProcess = GetStrategy(); + if (preProcess == nullptr) { + ZLOGE("get strategy fail, maybe memory not enough"); + return Data(); + } + if (!(*preProcess)(context)) { + ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); + return Data(); + } + std::vector queryResult = PublishedData::Query(context->calledBundleName); + return Convert(queryResult); + +} + +Strategy *GetDataStrategy::GetStrategy() +{ + static std::mutex mutex; + static SeqStrategy strategies; + std::lock_guard lock(mutex); + if (!strategies.IsEmpty()) { + return &strategies; + } + auto ret = strategies.Init({ + new (std::nothrow) LoadConfigCommonStrategy(), + new (std::nothrow) LoadConfigFromDataProxyNodeStrategy(), + new (std::nothrow) PermissionStrategy() + }); + if (!ret) { + return nullptr; + } + return &strategies; +} + +Data GetDataStrategy::Convert(std::vector datas) +{ + Data data; + data.version_ = -1; + for (auto &item : datas) { + if (item.value.version > data.version_) { + data.version_ = item.value.version; + } + data.datas_.emplace_back(item.value.key, item.value.subscriberId, item.value.value); + } + return data; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.h b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.h new file mode 100644 index 00000000..d946709b --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.h @@ -0,0 +1,36 @@ +/* + * 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 DATASHARESERVICE_GET_DATA_STRAGETY_H +#define DATASHARESERVICE_GET_DATA_STRAGETY_H + +#include + +#include "data_proxy_observer.h" +#include "datashare_template.h" +#include "published_data.h" +#include "seq_strategy.h" + +namespace OHOS::DataShare { +class GetDataStrategy final { +public: + static Data Execute(std::shared_ptr context); + +private: + static Strategy *GetStrategy(); + static Data Convert(std::vector datas); +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp new file mode 100644 index 00000000..87ac59ed --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp @@ -0,0 +1,69 @@ +/* + * 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 "InsertStrategy" + +#include "insert_strategy.h" + +#include "db_delegate.h" +#include "general/load_config_common_strategy.h" +#include "general/load_config_data_info_strategy.h" +#include "general/load_config_from_bundle_info_strategy.h" +#include "general/permission_strategy.h" +#include "general/process_single_app_user_cross_strategy.h" +#include "log_print.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +int64_t InsertStrategy::Execute(std::shared_ptr context, const DataShareValuesBucket &valuesBucket) +{ + auto preProcess = GetStrategy(); + if (preProcess == nullptr) { + ZLOGE("get strategy fail, maybe memory not enough"); + return -1; + } + if (!(*preProcess)(context)) { + ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); + return -1; + } + int errCode = E_OK; + auto delegate = DBDelegate::Create(context->calledSourceDir, context->version, errCode); + if (delegate == nullptr) { + ZLOGE("malloc fail %{public}s %{public}s", context->calledBundleName.c_str(), context->calledTableName.c_str()); + return -1; + } + return delegate->Insert(context->calledTableName, valuesBucket); +} + +Strategy *InsertStrategy::GetStrategy() +{ + static std::mutex mutex; + static SeqStrategy strategies; + std::lock_guard lock(mutex); + if (!strategies.IsEmpty()) { + return &strategies; + } + auto ret = strategies.Init({ + new (std::nothrow)LoadConfigCommonStrategy(), + new (std::nothrow)LoadConfigFromBundleInfoStrategy(), + new (std::nothrow)PermissionStrategy(), + new (std::nothrow)LoadConfigDataInfoStrategy(), + new (std::nothrow)ProcessSingleAppUserCrossStrategy() + }); + if (!ret) { + return nullptr; + } + return &strategies; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/insert_strategy.h b/services/distributeddataservice/service/data_share/strategies/insert_strategy.h new file mode 100644 index 00000000..c5472018 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/insert_strategy.h @@ -0,0 +1,32 @@ +/* + * 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 DATASHARESERVICE_INSERT_STRAGETY_H +#define DATASHARESERVICE_INSERT_STRAGETY_H + +#include + +#include "datashare_values_bucket.h" +#include "seq_strategy.h" + +namespace OHOS::DataShare { +class InsertStrategy final { +public: + static int64_t Execute(std::shared_ptr context, const DataShareValuesBucket &valuesBucket); + +private: + static Strategy *GetStrategy(); +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp new file mode 100644 index 00000000..70925980 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp @@ -0,0 +1,70 @@ +/* + * 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 "PublishStrategy" + +#include "publish_strategy.h" + +#include "data_proxy/load_config_from_data_proxy_node_strategy.h" +#include "general/load_config_common_strategy.h" +#include "general/permission_strategy.h" +#include "log_print.h" +#include "published_data.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +int32_t PublishStrategy::Execute(std::shared_ptr context, const PublishedDataItem &item) +{ + auto preProcess = GetStrategy(); + if (preProcess == nullptr) { + ZLOGE("get strategy fail, maybe memory not enough"); + return -1; + } + if (!(*preProcess)(context)) { + ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); + return -1; + } + auto delegate = KvDBDelegate::GetInstance(); + if (delegate == nullptr) { + ZLOGE("db open failed"); + return -1; + } + PublishedData data(context->uri, context->calledBundleName, item.subscriberId_, item.value_, context->version); + int32_t status = delegate->Upsert(KvDBDelegate::DATA_TABLE, data); + if (status != E_OK) { + ZLOGE("db Upsert failed, %{public}s %{public}s %{public}d", context->calledBundleName.c_str(), + DistributedData::Anonymous::Change(context->uri).c_str(), status); + return -1; + } + return E_OK; +} + +Strategy *PublishStrategy::GetStrategy() +{ + static std::mutex mutex; + static SeqStrategy strategies; + std::lock_guard lock(mutex); + if (!strategies.IsEmpty()) { + return &strategies; + } + auto ret = strategies.Init({ + new (std::nothrow) LoadConfigCommonStrategy(), + new (std::nothrow) LoadConfigFromDataProxyNodeStrategy(), + new (std::nothrow) PermissionStrategy() }); + if (!ret) { + return nullptr; + } + return &strategies; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/publish_strategy.h b/services/distributeddataservice/service/data_share/strategies/publish_strategy.h new file mode 100644 index 00000000..70acdf7c --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/publish_strategy.h @@ -0,0 +1,33 @@ +/* + * 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 DATASHARESERVICE_PUBLISH_STRAGETY_H +#define DATASHARESERVICE_PUBLISH_STRAGETY_H + +#include + +#include "data_proxy_observer.h" +#include "seq_strategy.h" + +namespace OHOS::DataShare { +class PublishStrategy final { +public: + static int32_t Execute(std::shared_ptr context, const PublishedDataItem &item); + +private: + static Strategy *GetStrategy(); +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp new file mode 100644 index 00000000..c598e7ee --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp @@ -0,0 +1,70 @@ +/* + * 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 "QueryStrategy" + +#include "query_strategy.h" + +#include "general/load_config_common_strategy.h" +#include "general/load_config_data_info_strategy.h" +#include "general/load_config_from_bundle_info_strategy.h" +#include "general/permission_strategy.h" +#include "general/process_single_app_user_cross_strategy.h" +#include "log_print.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +std::shared_ptr QueryStrategy::Execute( + std::shared_ptr context, const DataSharePredicates &predicates, + const std::vector &columns, int &errCode) +{ + auto preProcess = GetStrategy(); + if (preProcess == nullptr) { + ZLOGE("get strategy fail, maybe memory not enough"); + return nullptr; + } + if (!(*preProcess)(context)) { + errCode = context->errCode; + ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); + return nullptr; + } + auto delegate = DBDelegate::Create(context->calledSourceDir, context->version, errCode); + if (delegate == nullptr) { + ZLOGE("malloc fail %{public}s %{public}s", context->calledBundleName.c_str(), context->calledTableName.c_str()); + return nullptr; + } + return delegate->Query(context->calledTableName, predicates, columns, errCode); +} + +Strategy *QueryStrategy::GetStrategy() +{ + static std::mutex mutex; + static SeqStrategy strategies; + std::lock_guard lock(mutex); + if (!strategies.IsEmpty()) { + return &strategies; + } + auto ret = strategies.Init({ + new (std::nothrow)LoadConfigCommonStrategy(), + new (std::nothrow)LoadConfigFromBundleInfoStrategy(), + new (std::nothrow)PermissionStrategy(), + new (std::nothrow)LoadConfigDataInfoStrategy(), + new (std::nothrow)ProcessSingleAppUserCrossStrategy() + }); + if (!ret) { + return nullptr; + } + return &strategies; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/query_strategy.h b/services/distributeddataservice/service/data_share/strategies/query_strategy.h new file mode 100644 index 00000000..13b0186f --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/query_strategy.h @@ -0,0 +1,34 @@ +/* + * 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 DATASHARESERVICE_QUERY_STRAGETY_H +#define DATASHARESERVICE_QUERY_STRAGETY_H + +#include + +#include "seq_strategy.h" +#include "rdb_delegate.h" + +namespace OHOS::DataShare { +class QueryStrategy final { +public: + static std::shared_ptr Execute(std::shared_ptr context, + const DataSharePredicates &predicates, const std::vector &columns, int &errCode); + +private: + static Strategy *GetStrategy(); +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp new file mode 100644 index 00000000..606e5bcc --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp @@ -0,0 +1,58 @@ +/* + * 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 "SubscribeStrategy" + +#include "subscribe_strategy.h" + +#include "data_proxy/load_config_from_data_proxy_node_strategy.h" +#include "general/load_config_common_strategy.h" +#include "general/load_config_data_info_strategy.h" +#include "general/permission_strategy.h" +#include "log_print.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +int32_t SubscribeStrategy::Execute(std::shared_ptr context, std::function process) +{ + auto preProcess = GetStrategy(); + if (preProcess == nullptr) { + ZLOGE("get strategy fail, maybe memory not enough"); + return -1; + } + if (!(*preProcess)(context)) { + ZLOGE("pre process fail, uri_: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); + return context->errCode; + } + return process(); +} +Strategy *SubscribeStrategy::GetStrategy() +{ + static std::mutex mutex; + static SeqStrategy strategies; + std::lock_guard lock(mutex); + if (!strategies.IsEmpty()) { + return &strategies; + } + auto ret = strategies.Init({ + new (std::nothrow)LoadConfigCommonStrategy(), + new (std::nothrow)LoadConfigFromDataProxyNodeStrategy(), + new (std::nothrow)PermissionStrategy() + }); + if (!ret) { + return nullptr; + } + return &strategies; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.h b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.h new file mode 100644 index 00000000..f11c4a6d --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.h @@ -0,0 +1,32 @@ +/* + * 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 DATASHARESERVICE_SUBSCRIBE_STRAGETY_H +#define DATASHARESERVICE_SUBSCRIBE_STRAGETY_H + +#include +#include "seq_strategy.h" +#include "data_proxy_observer.h" + +namespace OHOS::DataShare { +class SubscribeStrategy final { +public: + static int32_t Execute(std::shared_ptr context, std::function process); + +private: + static Strategy *GetStrategy(); +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp new file mode 100644 index 00000000..419c53a8 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp @@ -0,0 +1,69 @@ +/* + * 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 "UpdateStrategy" + +#include "update_strategy.h" + +#include "general/load_config_common_strategy.h" +#include "general/load_config_data_info_strategy.h" +#include "general/load_config_from_bundle_info_strategy.h" +#include "general/permission_strategy.h" +#include "general/process_single_app_user_cross_strategy.h" +#include "log_print.h" +#include "utils/anonymous.h" + +namespace OHOS::DataShare { +int64_t UpdateStrategy::Execute( + std::shared_ptr context, const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket) +{ + auto preProcess = GetStrategy(); + if (preProcess == nullptr) { + ZLOGE("get strategy fail, maybe memory not enough"); + return -1; + } + if (!(*preProcess)(context)) { + ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); + return -1; + } + int errCode = E_OK; + auto delegate = DBDelegate::Create(context->calledSourceDir, context->version, errCode); + if (delegate == nullptr) { + ZLOGE("malloc fail %{public}s %{public}s", context->calledBundleName.c_str(), context->calledTableName.c_str()); + return -1; + } + return delegate->Update(context->calledTableName, predicate, valuesBucket); +} + +Strategy *UpdateStrategy::GetStrategy() +{ + static std::mutex mutex; + static SeqStrategy strategies; + std::lock_guard lock(mutex); + if (!strategies.IsEmpty()) { + return &strategies; + } + auto ret = strategies.Init({ + new (std::nothrow)LoadConfigCommonStrategy(), + new (std::nothrow)LoadConfigFromBundleInfoStrategy(), + new (std::nothrow)PermissionStrategy(), + new (std::nothrow)LoadConfigDataInfoStrategy(), + new (std::nothrow)ProcessSingleAppUserCrossStrategy() + }); + if (!ret) { + return nullptr; + } + return &strategies; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/update_strategy.h b/services/distributeddataservice/service/data_share/strategies/update_strategy.h new file mode 100644 index 00000000..4ee54764 --- /dev/null +++ b/services/distributeddataservice/service/data_share/strategies/update_strategy.h @@ -0,0 +1,35 @@ +/* + * 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 DATASHARESERVICE_UPDATE_STRAGETY_H +#define DATASHARESERVICE_UPDATE_STRAGETY_H + +#include + +#include "context.h" +#include "rdb_delegate.h" +#include "seq_strategy.h" + +namespace OHOS::DataShare { +class UpdateStrategy final { +public: + static int64_t Execute(std::shared_ptr context, const DataSharePredicates &predicate, + const DataShareValuesBucket &valuesBucket); + +private: + static Strategy *GetStrategy(); +}; +} // namespace OHOS::DataShare +#endif -- Gitee From 8996e7e4b76d57e86dbe95492facd7870a4d05f5 Mon Sep 17 00:00:00 2001 From: hanlu Date: Wed, 3 May 2023 18:07:19 +0800 Subject: [PATCH 073/152] f Signed-off-by: hanlu --- .../service/data_share/common/kv_delegate.cpp | 8 ++------ .../service/data_share/common/template_manager.cpp | 5 ----- .../service/data_share/data_share_service_impl.cpp | 5 +++-- .../service/data_share/data_share_service_stub.cpp | 1 - .../service/data_share/data_share_types_util.cpp | 3 --- .../load_config_from_data_proxy_node_strategy.cpp | 6 ------ .../strategies/general/load_config_data_info_strategy.cpp | 1 - .../data_share/strategies/general/permission_strategy.cpp | 2 -- .../general/process_single_app_user_cross_strategy.cpp | 3 --- .../service/directory/src/directory_manager.cpp | 5 +++++ 10 files changed, 10 insertions(+), 29 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp index 4be6ae0e..113abe4e 100644 --- a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp @@ -60,7 +60,6 @@ bool KvDelegate::Init() return true; } int status = GRD_DBOpen((path_ + "/dataShare.db").c_str(), nullptr, GRD_DB_OPEN_CREATE, &db_); - ZLOGE("GRD_DBOpen hanluhanlu,path111 %{public}s", path_.c_str()); if (status != GRD_OK || db_ == nullptr) { ZLOGE("GRD_DBOpen failed,status %{public}d", status); return false; @@ -214,9 +213,6 @@ int32_t KvDelegate::GetBatch(const std::string &collectionName, const std::strin GRD_FreeResultSet(resultSet); return E_OK; } -KvDelegate::KvDelegate(const std::string &path) : path_(path) { - if (path_.empty()) { - path_ = "/data/app/el1/0/database/distributeddata/kvdb"; // todo hanlu delete - } -} + +KvDelegate::KvDelegate(const std::string &path) : path_(path) {} } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index 2877b3fc..a98af697 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -419,10 +419,6 @@ void PublishedDataSubscriberManager::Emit( publishedDataCache_.ForEach([&keys, &status, &observer, &publishedResult, &callbacks]( const PublishedDataKey &key, std::vector &val) { for (auto &data : keys) { - ZLOGE("HANLU start try emit %{public}s %{public}s %{public}" PRId64, data.bundleName_.c_str(), - data.key_.c_str(), data.subscriberId_); - ZLOGE("HANLU observer %{public}s %{public}s %{public}" PRId64, key.bundleName_.c_str(), key.key_.c_str(), - key.subscriberId_); if (key != data) { continue; } @@ -461,7 +457,6 @@ void PublishedDataSubscriberManager::Emit( continue; } result.ownerBundleName_ = ownerBundleName; - ZLOGE("HANLU emit start %{public}d", result.datas_.size()); callback->OnChangeFromPublishedData(result); } } diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 0262a403..34f913e6 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -162,7 +162,6 @@ std::vector DataShareServiceImpl::Publish(const Data &data, con std::string callerBundleName; GetCallerBundleName(callerBundleName); for (const auto &item : data.datas_) { - ZLOGE("hanlu start publish %{public}s %{public}s", item.key_.c_str(), bundleNameOfProvider.c_str()); auto context = std::make_shared(item.key_); context->version = data.version_; context->callerBundleName = callerBundleName; @@ -181,7 +180,10 @@ std::vector DataShareServiceImpl::Publish(const Data &data, con Data DataShareServiceImpl::GetData(const std::string &bundleNameOfProvider) { + std::string callerBundleName; + GetCallerBundleName(callerBundleName); auto context = std::make_shared(); + context->callerBundleName = callerBundleName; context->calledBundleName = bundleNameOfProvider; return GetDataStrategy::Execute(context); } @@ -357,7 +359,6 @@ int32_t DataShareServiceImpl::OnInitialize() saveMeta.uid = IPCSkeleton::GetCallingUid(); saveMeta.storeType = DATA_SHARE_SINGLE_VERSION; saveMeta.dataDir = DistributedData::DirectoryManager::GetInstance().GetStorePath(saveMeta); - ZLOGE("hanlu1 dir %{public}s %{public}d", saveMeta.dataDir.c_str(), token); KvDBDelegate::GetInstance(false, saveMeta.dataDir); return EOK; } diff --git a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp index dbd30235..7a3818d4 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp @@ -144,7 +144,6 @@ int32_t DataShareServiceStub::OnRemoteDelTemplate(MessageParcel &data, MessagePa int32_t DataShareServiceStub::OnRemotePublish(MessageParcel &data, MessageParcel &reply) { - ZLOGE("hanlu enter"); Data publishData; std::string bundleName; if (!ITypesUtil::Unmarshal(data, publishData.datas_, publishData.version_, bundleName)) { diff --git a/services/distributeddataservice/service/data_share/data_share_types_util.cpp b/services/distributeddataservice/service/data_share/data_share_types_util.cpp index 178380df..847fb8d1 100644 --- a/services/distributeddataservice/service/data_share/data_share_types_util.cpp +++ b/services/distributeddataservice/service/data_share/data_share_types_util.cpp @@ -52,17 +52,14 @@ bool Unmarshalling(PublishedDataItem &dataItem, MessageParcel &parcel) dataItem.key_ = parcel.ReadString(); dataItem.subscriberId_ = parcel.ReadInt64(); auto index = parcel.ReadUint32(); - ZLOGE("hanlu Unmarshalling %{public}s", dataItem.key_.c_str()); if (index == 0) { sptr ashmem = parcel.ReadAshmem(); dataItem.value_ = ashmem; - ZLOGE("hanlu ReadAshmem %{public}p", ashmem.GetRefPtr()); bool ret = ashmem->MapReadOnlyAshmem(); if (!ret) { ZLOGE("MapReadAndWriteAshmem fail, %{private}s", dataItem.key_.c_str()); return false; } - ZLOGE("hanlu MapReadAndWriteAshmem %{public}p", ashmem.GetRefPtr()); } else { dataItem.value_ = parcel.ReadString(); } diff --git a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp index cd2fc79b..600c242d 100644 --- a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp @@ -29,17 +29,14 @@ bool LoadConfigFromDataProxyNodeStrategy::operator()(std::shared_ptr co return false; } context->type = DataProperties::PUBLISHED_DATA_TYPE; - ZLOGE("hanlu enter %{public}s %{public}d", context->calledBundleName.c_str(), context->callerTokenId); if (!BundleMgrProxy::GetInstance()->GetBundleInfoFromBMS( context->calledBundleName, context->currentUserId, context->bundleInfo)) { ZLOGE("GetBundleInfoFromBMS failed! bundleName: %{public}s", context->calledBundleName.c_str()); return false; } for (auto &hapModuleInfo : context->bundleInfo.hapModuleInfos) { - ZLOGE("hanlu uri: %{public}d", hapModuleInfo.proxyDatas.size()); auto proxyDatas = hapModuleInfo.proxyDatas; for (auto &proxyData : proxyDatas) { - ZLOGE("hanlu uri: %{public}s inputuri:%{public}s", proxyData.uri.c_str(), context->uri.c_str()); if (proxyData.uri != context->uri) { continue; } @@ -56,8 +53,6 @@ bool LoadConfigFromDataProxyNodeStrategy::operator()(std::shared_ptr co return true; } GetContextInfoFromDataProperties(properties, hapModuleInfo.moduleName, context); - ZLOGE("hanlu calledStoreName: %{public}s %{public}s", context->calledStoreName.c_str(), - context->calledTableName.c_str()); return true; } } @@ -88,7 +83,6 @@ bool LoadConfigFromDataProxyNodeStrategy::LoadConfigFromUri(std::shared_ptruri, context->calledBundleName)) { return false; } - ZLOGE("hanlu enter %{publis}s", context->calledBundleName.c_str()); return true; } } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp index 2db6630b..39285621 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp @@ -64,7 +64,6 @@ bool LoadConfigNormalDataInfoStrategy::operator()(std::shared_ptr conte } } context->calledSourceDir = metaData.dataDir; - ZLOGE("hanlu dir %{public}s", context->calledSourceDir.c_str()); context->version = metaData.version; return true; } diff --git a/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.cpp index afa21456..86a4ffba 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/permission_strategy.cpp @@ -22,8 +22,6 @@ namespace OHOS::DataShare { bool PermissionStrategy::operator()(std::shared_ptr context) { - ZLOGE("hanlu check! callerTokenId:%{public}u permission:%{public}s", - context->callerTokenId, context->permission.c_str()); if (context->permission == "reject") { return false; } diff --git a/services/distributeddataservice/service/data_share/strategies/general/process_single_app_user_cross_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/process_single_app_user_cross_strategy.cpp index f802778b..d3d19c33 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/process_single_app_user_cross_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/process_single_app_user_cross_strategy.cpp @@ -34,11 +34,8 @@ bool ProcessUserCrossStrategy::operator()(std::shared_ptr context) context->calledBundleName.c_str()); return false; } - ZLOGE("hanlu enter %{public}d", context->accessSystemMode); if (context->accessSystemMode == USER_SINGLE_MODE) { - ZLOGE("hanlu table userid %{public}d", context->currentUserId); context->calledTableName.append("_").append(std::to_string(context->currentUserId)); - ZLOGE("hanlu table userid %{public}s", context->calledTableName.c_str()); return true; } return true; diff --git a/services/distributeddataservice/service/directory/src/directory_manager.cpp b/services/distributeddataservice/service/directory/src/directory_manager.cpp index bba79049..edcdda5f 100644 --- a/services/distributeddataservice/service/directory/src/directory_manager.cpp +++ b/services/distributeddataservice/service/directory/src/directory_manager.cpp @@ -132,6 +132,11 @@ std::string DirectoryManager::GetStore(const StoreMetaData &metaData) const && metaData.storeType <= StoreMetaData::StoreType::STORE_OBJECT_END) { return "kvdb"; } + // datashare use meta + if (metaData.storeType >= StoreMetaData::StoreType::STORE_DATA_SHARE_BEGIN + && metaData.storeType <= StoreMetaData::StoreType::STORE_DATA_SHARE_END) { + return "kvdb"; + } return "other"; } -- Gitee From 414336a12b684c8c13e135d23a19eabb9569924d Mon Sep 17 00:00:00 2001 From: hanlu Date: Wed, 3 May 2023 18:09:48 +0800 Subject: [PATCH 074/152] f Signed-off-by: hanlu --- .../gaussdb_rd_simple/.vscode/settings.json | 60 ------------------- .../gaussdb_rd_simple/CMakeLists.txt | 38 ------------ 2 files changed, 98 deletions(-) delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/.vscode/settings.json delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/CMakeLists.txt diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/.vscode/settings.json b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/.vscode/settings.json deleted file mode 100644 index 626b352b..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/.vscode/settings.json +++ /dev/null @@ -1,60 +0,0 @@ -{ - "files.associations": { - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "cctype": "cpp", - "chrono": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "concepts": "cpp", - "condition_variable": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "map": "cpp", - "set": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "ratio": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "initializer_list": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "mutex": "cpp", - "new": "cpp", - "numbers": "cpp", - "ostream": "cpp", - "ranges": "cpp", - "semaphore": "cpp", - "stdexcept": "cpp", - "stop_token": "cpp", - "streambuf": "cpp", - "thread": "cpp", - "typeinfo": "cpp" - } -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/CMakeLists.txt b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/CMakeLists.txt deleted file mode 100644 index 75734853..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/CMakeLists.txt +++ /dev/null @@ -1,38 +0,0 @@ -cmake_minimum_required(VERSION 3.10.2) -project(document_ut) - -set(GAUSE_SOURCE_DIR "${PROJECT_SOURCE_DIR}") - -file(GLOB_RECURSE DOCUMENT_SRC ${GAUSE_SOURCE_DIR}/src/*.cpp) -message(STATUS "hanlu ${GAUSE_SOURCE_DIR} ${DOCUMENT_SRC}") -include_directories( - ${GAUSE_SOURCE_DIR}/include - ${GAUSE_SOURCE_DIR}/src/storage - ${GAUSE_SOURCE_DIR}/src/storage/include - ${GAUSE_SOURCE_DIR}/src/storage/src - ${GAUSE_SOURCE_DIR}/src/common - ${GAUSE_SOURCE_DIR}/src/common/include - ${GAUSE_SOURCE_DIR}/src/executor/include - ${GAUSE_SOURCE_DIR}/src/executor/base - ${GAUSE_SOURCE_DIR}/src/executor/document - ${GAUSE_SOURCE_DIR}/src/oh_adapter - ${GAUSE_SOURCE_DIR}/src/oh_adapter/include - ${GAUSE_SOURCE_DIR}/src/oh_adapter/src - ${GAUSE_SOURCE_DIR}/src/interface - ${GAUSE_SOURCE_DIR}/src/interface/include - ${GAUSE_SOURCE_DIR}/src/interface/src - ${GAUSE_SOURCE_DIR}/third_party/third_party_cJSON -) -aux_source_directory( ${GAUSE_SOURCE_DIR}/third_party/third_party_cJSON DOCUMENT_SRC) -set(MOCK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../mock") -set(KV_STORE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../kv_store") -include(${MOCK_DIR}/include/CMakeLists.txt OPTIONAL) -include(${KV_STORE_DIR}/interfaces/CMakeLists.txt OPTIONAL) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../utils_native/base/include) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../utils_native/safwk/native/include) -message(STATUS "hanlu3 ${CMAKE_CURRENT_SOURCE_DIR}../../../../../../utils_native/base/include") -add_library(document_ut SHARED ${DOCUMENT_SRC}) -set(links mock kvdb adapter data_share secure) -target_link_libraries(document_ut ${links}) - - -- Gitee From 23c753d590773ddd84b83fa7babc239b19fc3f06 Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 4 May 2023 10:48:47 +0800 Subject: [PATCH 075/152] f Signed-off-by: hanlu --- .../service/data_share/common/db_delegate.cpp | 8 +- .../data_share/data/published_data.cpp | 14 +- .../service/data_share/data/published_data.h | 8 +- .../include/grd_base/grd_type_export.h | 19 +- .../include/grd_document/grd_document_api.h | 14 +- .../src/common/include/collection_option.h | 1 + .../src/common/include/db_config.h | 2 +- .../src/common/include/json_common.h | 19 +- .../src/common/include/log_print.h | 3 +- .../src/common/src/collection_option.cpp | 9 +- .../src/common/src/db_config.cpp | 37 ++-- .../src/common/src/json_common.cpp | 130 +++++------ .../src/common/src/log_print.cpp | 7 +- .../src/common/src/os_api.cpp | 11 +- .../src/executor/base/grd_db_api.cpp | 2 +- .../src/executor/document/document_check.cpp | 9 +- .../src/executor/document/document_check.h | 6 +- .../executor/document/grd_document_api.cpp | 15 +- .../executor/document/grd_resultset_api.cpp | 4 +- .../src/executor/include/grd_format_config.h | 1 - .../src/executor/include/grd_type_inner.h | 2 +- .../src/interface/include/collection.h | 6 +- .../src/interface/include/doc_errno.h | 2 +- .../src/interface/include/document_store.h | 15 +- .../include/document_store_manager.h | 7 +- .../src/interface/include/projection_tree.h | 14 +- .../src/interface/include/result_set.h | 20 +- .../src/interface/include/result_set_common.h | 12 +- .../src/interface/src/collection.cpp | 4 +- .../src/interface/src/doc_errno.cpp | 5 +- .../src/interface/src/document_store.cpp | 103 +++++---- .../interface/src/document_store_manager.cpp | 17 +- .../src/interface/src/projection_tree.cpp | 25 ++- .../src/interface/src/result_set.cpp | 30 ++- .../src/interface/src/result_set_common.cpp | 13 +- .../src/oh_adapter/include/json_object.h | 15 +- .../oh_adapter/include/kv_store_executor.h | 3 +- .../src/oh_adapter/include/kv_store_manager.h | 2 +- .../src/oh_adapter/src/json_object.cpp | 10 +- .../src/oh_adapter/src/kv_store_manager.cpp | 5 +- .../src/sqlite_store_executor_impl.cpp | 85 ++++---- .../src/sqlite_store_executor_impl.h | 2 +- .../src/oh_adapter/src/sqlite_utils.cpp | 17 +- .../src/oh_adapter/src/sqlite_utils.h | 7 +- .../test/unittest/api/documentdb_api_test.cpp | 80 +++---- .../api/documentdb_collection_test.cpp | 32 +-- .../unittest/api/documentdb_data_test.cpp | 52 +++-- .../unittest/api/documentdb_delete_test.cpp | 26 +-- .../unittest/api/documentdb_find_test.cpp | 204 +++++++++--------- .../unittest/api/documentdb_insert_test.cpp | 116 +++++----- .../test/unittest/api/documentdb_test_utils.h | 1 - .../unittest/common/documentdb_test_utils.h | 1 - .../documentdb_json_common_test.cpp | 73 ++++--- .../oh_adapter/documentdb_jsonobject_test.cpp | 21 +- 54 files changed, 661 insertions(+), 685 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp index f4769c55..378396cc 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -14,11 +14,11 @@ */ #define LOG_TAG "DBAdaptor" #include "db_delegate.h" -#include "rdb_delegate.h" + #include "kv_delegate.h" +#include "rdb_delegate.h" namespace OHOS::DataShare { -std::shared_ptr DBDelegate::Create(const std::string &dir, int version, int &errCode, - bool registerFunction) +std::shared_ptr DBDelegate::Create(const std::string &dir, int version, int &errCode, bool registerFunction) { return std::make_shared(dir, version, errCode, registerFunction); } @@ -31,7 +31,7 @@ std::shared_ptr KvDBDelegate::GetInstance(bool reInit, const std:: static std::shared_ptr delegate = nullptr; static std::mutex mutex; std::lock_guard lock(mutex); - if(delegate == nullptr || reInit) { + if (delegate == nullptr || reInit) { delegate = std::make_shared(dir); } return delegate; diff --git a/services/distributeddataservice/service/data_share/data/published_data.cpp b/services/distributeddataservice/service/data_share/data/published_data.cpp index 22a607ff..a4a90611 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.cpp +++ b/services/distributeddataservice/service/data_share/data/published_data.cpp @@ -81,7 +81,7 @@ bool PublishedDataNode::Marshal(DistributedData::Serializable::json &node) const ret = ret && SetValue(node["type"], PublishedDataType::STRING); ret = ret && SetValue(node["value"], valueStr); } else { - sptr ashmem = std::get>(value); + sptr ashmem = std::get>(value); if (ashmem == nullptr) { ZLOGE("get ashmem null"); return false; @@ -108,7 +108,7 @@ bool PublishedDataNode::Unmarshal(const DistributedData::Serializable::json &nod int32_t type = 0; ret = ret && GetValue(node, "type", type); if (!ret) { - ZLOGE("Unmarshal PublishedDataNode failed, %{public}s", key.c_str()); + ZLOGE("Unmarshal PublishedDataNode failed, %{private}s", key.c_str()); return false; } if (type == PublishedDataType::STRING) { @@ -124,7 +124,7 @@ bool PublishedDataNode::Unmarshal(const DistributedData::Serializable::json &nod return false; } - bool ret = ashmem->MapReadAndWriteAshmem(); + ret = ashmem->MapReadAndWriteAshmem(); if (!ret) { ZLOGE("SharedBlock: MapReadAndWriteAshmem function error."); ashmem->CloseAshmem(); @@ -153,8 +153,7 @@ std::string PublishedData::GetFullProjection() projection["version"] = 1; return projection.dump(); } -int32_t PublishedData::Query( - const std::string &filter, std::variant, std::string> &publishedData) +int32_t PublishedData::Query(const std::string &filter, std::variant, std::string> &publishedData) { auto delegate = KvDBDelegate::GetInstance(); if (delegate == nullptr) { @@ -162,14 +161,13 @@ int32_t PublishedData::Query( return E_ERROR; } std::string queryResult; - int32_t status = - delegate->Get(KvDBDelegate::DATA_TABLE, filter, "{}", queryResult); + int32_t status = delegate->Get(KvDBDelegate::DATA_TABLE, filter, "{}", queryResult); if (status != E_OK) { ZLOGE("db Get failed, %{public}s %{public}d", filter.c_str(), status); return status; } PublishedData data; - if(!data.Unmarshall(queryResult)) { + if (!data.Unmarshall(queryResult)) { ZLOGE("Unmarshall failed, %{private}s", queryResult.c_str()); return E_ERROR; } diff --git a/services/distributeddataservice/service/data_share/data/published_data.h b/services/distributeddataservice/service/data_share/data/published_data.h index 459b477c..d083ee92 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.h +++ b/services/distributeddataservice/service/data_share/data/published_data.h @@ -20,12 +20,12 @@ #include "serializable/serializable.h" namespace OHOS::DataShare { -enum PublishedDataType : int32_t -{ +enum PublishedDataType : int32_t { STRING, ASHMEM }; -struct PublishedDataNode final: public DistributedData::Serializable { + +struct PublishedDataNode final : public DistributedData::Serializable { PublishedDataNode(const std::string &key, const std::string &bundleName, int64_t subscriberId, const std::variant, std::string> &value, const VersionData &version); ~PublishedDataNode() = default; @@ -38,7 +38,7 @@ struct PublishedDataNode final: public DistributedData::Serializable { VersionData version; }; -struct PublishedData final: public KvData { +struct PublishedData final : public KvData { static std::vector Query(const std::string &bundleName); static int32_t Query(const std::string &filter, std::variant, std::string> &publishedData); PublishedData(const std::string &key = "", const std::string &bundleName = "", int64_t subscriberId = 0, diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_type_export.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_type_export.h index b729d216..02e15324 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_type_export.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_type_export.h @@ -21,7 +21,7 @@ extern "C" { #endif // __cplusplus #ifndef _WIN32 - #define DOC_API __attribute__ ((visibility ("default"))) +#define DOC_API __attribute__((visibility("default"))) #endif typedef struct GRD_DB GRD_DB; @@ -29,16 +29,17 @@ typedef struct GRD_DB GRD_DB; /** * @brief Open database config */ -#define GRD_DB_OPEN_ONLY 0x00 -#define GRD_DB_OPEN_CREATE 0x01 -#define GRD_DB_OPEN_CHECK_FOR_ABNORMAL 0x02 //check data in database if close abnormally last time, - //if data is corrupted, rebuild the database -#define GRD_DB_OPEN_CHECK 0x04//check data in database when open database, if data is corrupted, rebuild the database. +#define GRD_DB_OPEN_ONLY 0x00 +#define GRD_DB_OPEN_CREATE 0x01 +#define GRD_DB_OPEN_CHECK_FOR_ABNORMAL \ + 0x02 //check data in database if close abnormally last time, \ + //if data is corrupted, rebuild the database +#define GRD_DB_OPEN_CHECK 0x04 //check data in database when open database, if data is corrupted, rebuild the database. /** * @brief Close database config */ -#define GRD_DB_CLOSE 0x00 -#define GRD_DB_CLOSE_IGNORE_ERROR 0x01 +#define GRD_DB_CLOSE 0x00 +#define GRD_DB_CLOSE_IGNORE_ERROR 0x01 /** * @brief flush database config @@ -46,7 +47,7 @@ typedef struct GRD_DB GRD_DB; #define GRD_DB_FLUSH_ASYNC 0x00 #define GRD_DB_FLUSH_SYNC 0x01 -#define GRD_DOC_ID_DISPLAY 0x01 +#define GRD_DOC_ID_DISPLAY 0x01 typedef struct Query { const char *filter; const char *projection; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_document/grd_document_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_document/grd_document_api.h index b4df9c6b..b8808904 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_document/grd_document_api.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_document/grd_document_api.h @@ -16,8 +16,8 @@ #ifndef GRD_DOCUMENT_API_H #define GRD_DOCUMENT_API_H -#include "grd_base/grd_type_export.h" #include "grd_base/grd_resultset_api.h" +#include "grd_base/grd_type_export.h" #ifdef __cplusplus extern "C" { @@ -29,14 +29,14 @@ DOC_API int GRD_DropCollection(GRD_DB *db, const char *collectionName, unsigned DOC_API int GRD_InsertDoc(GRD_DB *db, const char *collectionName, const char *document, unsigned int flags); -DOC_API int GRD_FindDoc(GRD_DB *db, const char *collectionName, Query query, unsigned int flags, - GRD_ResultSet **resultSet); +DOC_API int GRD_FindDoc( + GRD_DB *db, const char *collectionName, Query query, unsigned int flags, GRD_ResultSet **resultSet); -DOC_API int GRD_UpdateDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *update, - unsigned int flags); +DOC_API int GRD_UpdateDoc( + GRD_DB *db, const char *collectionName, const char *filter, const char *update, unsigned int flags); -DOC_API int GRD_UpsertDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *document, - unsigned int flags); +DOC_API int GRD_UpsertDoc( + GRD_DB *db, const char *collectionName, const char *filter, const char *document, unsigned int flags); DOC_API int GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, unsigned int flags); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/collection_option.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/collection_option.h index 72759240..42bd55db 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/collection_option.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/collection_option.h @@ -29,6 +29,7 @@ public: bool operator==(const CollectionOption &targetOption) const; bool operator!=(const CollectionOption &targetOption) const; + private: std::string option_ = "{}"; uint32_t maxDoc_ = UINT32_MAX; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/db_config.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/db_config.h index c04116f2..ec7d8f44 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/db_config.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/db_config.h @@ -38,7 +38,7 @@ private: int32_t pageSize_ = 4; // 4: default page size k uint32_t redoFlushByTrx_ = 0; uint32_t redoPubBufSize_ = 1024; // 1024: default 1024k buff size - int32_t maxConnNum_ = 100; // 100: default max conn + int32_t maxConnNum_ = 100; // 100: default max conn uint32_t bufferPoolSize_ = 1024; // 100: default 1024k pool size uint32_t crcCheckEnable_ = 1; }; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h index 4494f01e..f7300a06 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h @@ -17,31 +17,32 @@ #define JSON_COMMON_H #include -#include -#include #include +#include +#include + #include "json_object.h" namespace DocumentDB { -class JsonCommon -{ +class JsonCommon { public: JsonCommon() = default; ~JsonCommon(); - static ValueObject GetValueByFiled(JsonObject &node, const std::string& filed); + static ValueObject GetValueByFiled(JsonObject &node, const std::string &filed); static bool CheckJsonField(JsonObject &node); static bool CheckProjectionField(JsonObject &node); - static int ParseNode(JsonObject &Node, std::vector singlePath, std::vector> &resultPath, bool isFirstFloor); + static int ParseNode(JsonObject &Node, std::vector singlePath, + std::vector> &resultPath, bool isFirstFloor); static std::vector> ParsePath(const JsonObject &node); - static std::vector GetLeafValue(JsonObject &node); + static std::vector GetLeafValue(JsonObject &node); static int Append(const JsonObject &src, const JsonObject &add); - + private: static bool CheckNode(JsonObject &Node, std::set filedSet, bool &errFlag); static bool CheckProjectionNode(JsonObject &Node, std::set filedSet, bool &errFlag, bool isFirstFloor); static int CheckLeafNode(JsonObject &Node, std::vector &leafValue); }; -} // DocumentDB +} // namespace DocumentDB #endif // JSON_COMMON_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/log_print.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/log_print.h index 55c800a9..5954986d 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/log_print.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/log_print.h @@ -23,7 +23,8 @@ constexpr const char *LOG_TAG_DOC = "DocumentDB"; class Logger { public: - enum class Level { + enum class Level + { LEVEL_DEBUG, LEVEL_INFO, LEVEL_WARN, diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/collection_option.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/collection_option.cpp index fcb3da1e..c15c3895 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/collection_option.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/collection_option.cpp @@ -15,8 +15,8 @@ #include "collection_option.h" -#include #include +#include #include "doc_errno.h" #include "json_object.h" @@ -31,7 +31,6 @@ const std::vector DB_CONFIG = { bool CheckConfigSupport(const JsonObject &config, int &errCode) { - JsonObject child = config.GetChild(); while (!child.IsNull()) { std::string fieldName = child.GetItemFiled(); @@ -44,7 +43,7 @@ bool CheckConfigSupport(const JsonObject &config, int &errCode) } return true; } -} +} // namespace CollectionOption CollectionOption::ReadOption(const std::string &optStr, int &errCode) { if (optStr.empty()) { @@ -67,7 +66,7 @@ CollectionOption CollectionOption::ReadOption(const std::string &optStr, int &er return {}; } - static const JsonFieldPath maxDocField = {OPT_MAX_DOC}; + static const JsonFieldPath maxDocField = { OPT_MAX_DOC }; if (!collOpt.IsFieldExists(maxDocField)) { return {}; } @@ -115,4 +114,4 @@ bool CollectionOption::operator!=(const CollectionOption &targetOption) const { return !(*this == targetOption); } -} \ No newline at end of file +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/db_config.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/db_config.cpp index b60de5b1..af9b7429 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/db_config.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/db_config.cpp @@ -20,8 +20,8 @@ #include "doc_errno.h" #include "doc_limit.h" -#include "log_print.h" #include "json_object.h" +#include "log_print.h" namespace DocumentDB { namespace { @@ -39,18 +39,12 @@ const std::string DB_CONFIG_MAX_CONN_NUM = "maxconnnum"; const std::string DB_CONFIG_BUFFER_POOL_SIZE = "bufferpoolsize"; const std::string DB_CONFIG_CRC_CHECK_ENABLE = "crccheckenable"; -const std::vector DB_CONFIG = { - DB_CONFIG_PAGESIZE, - DB_CONFIG_REDO_FLUSH_BY_TRX, - DB_CONFIG_REDO_PUB_BUFF_SIZE, - DB_CONFIG_MAX_CONN_NUM, - DB_CONFIG_BUFFER_POOL_SIZE, - DB_CONFIG_CRC_CHECK_ENABLE -}; +const std::vector DB_CONFIG = { DB_CONFIG_PAGESIZE, DB_CONFIG_REDO_FLUSH_BY_TRX, + DB_CONFIG_REDO_PUB_BUFF_SIZE, DB_CONFIG_MAX_CONN_NUM, DB_CONFIG_BUFFER_POOL_SIZE, DB_CONFIG_CRC_CHECK_ENABLE }; bool CheckPageSizeConfig(const JsonObject &config, int32_t &pageSize, int &errCode) { - static const JsonFieldPath pageSizeField = {DB_CONFIG_PAGESIZE}; + static const JsonFieldPath pageSizeField = { DB_CONFIG_PAGESIZE }; if (!config.IsFieldExists(pageSizeField)) { return true; } @@ -62,7 +56,7 @@ bool CheckPageSizeConfig(const JsonObject &config, int32_t &pageSize, int &errCo return false; } - static const std::vector pageSizeValid = {4, 8, 16, 32, 64}; + static const std::vector pageSizeValid = { 4, 8, 16, 32, 64 }; if (std::find(pageSizeValid.begin(), pageSizeValid.end(), configValue.GetIntValue()) == pageSizeValid.end()) { GLOGE("Check DB config failed, invalid pageSize value."); errCode = -E_INVALID_CONFIG_VALUE; @@ -75,7 +69,7 @@ bool CheckPageSizeConfig(const JsonObject &config, int32_t &pageSize, int &errCo bool CheckRedoFlushConfig(const JsonObject &config, uint32_t &redoFlush, int &errCode) { - static const JsonFieldPath redoFlushField = {DB_CONFIG_REDO_FLUSH_BY_TRX}; + static const JsonFieldPath redoFlushField = { DB_CONFIG_REDO_FLUSH_BY_TRX }; if (!config.IsFieldExists(redoFlushField)) { return true; } @@ -99,7 +93,7 @@ bool CheckRedoFlushConfig(const JsonObject &config, uint32_t &redoFlush, int &er bool CheckRedoBufSizeConfig(const JsonObject &config, uint32_t &redoBufSize, int &errCode) { - static const JsonFieldPath redoBufSizeField = {DB_CONFIG_REDO_PUB_BUFF_SIZE}; + static const JsonFieldPath redoBufSizeField = { DB_CONFIG_REDO_PUB_BUFF_SIZE }; if (!config.IsFieldExists(redoBufSizeField)) { return true; } @@ -123,7 +117,7 @@ bool CheckRedoBufSizeConfig(const JsonObject &config, uint32_t &redoBufSize, int bool CheckMaxConnNumConfig(const JsonObject &config, int32_t &maxConnNum, int &errCode) { - static const JsonFieldPath maxConnNumField = {DB_CONFIG_MAX_CONN_NUM}; + static const JsonFieldPath maxConnNumField = { DB_CONFIG_MAX_CONN_NUM }; if (!config.IsFieldExists(maxConnNumField)) { return true; } @@ -145,10 +139,9 @@ bool CheckMaxConnNumConfig(const JsonObject &config, int32_t &maxConnNum, int &e return true; } -bool CheckBufferPoolSizeConfig(const JsonObject &config, int32_t pageSize, uint32_t &redoBufSize, - int &errCode) +bool CheckBufferPoolSizeConfig(const JsonObject &config, int32_t pageSize, uint32_t &redoBufSize, int &errCode) { - static const JsonFieldPath bufferPoolSizeField = {DB_CONFIG_BUFFER_POOL_SIZE}; + static const JsonFieldPath bufferPoolSizeField = { DB_CONFIG_BUFFER_POOL_SIZE }; if (!config.IsFieldExists(bufferPoolSizeField)) { return true; } @@ -173,7 +166,7 @@ bool CheckBufferPoolSizeConfig(const JsonObject &config, int32_t pageSize, uint3 bool CheckCrcCheckEnableConfig(const JsonObject &config, uint32_t &crcCheckEnable, int &errCode) { - static const JsonFieldPath crcCheckEnableField = {DB_CONFIG_CRC_CHECK_ENABLE}; + static const JsonFieldPath crcCheckEnableField = { DB_CONFIG_CRC_CHECK_ENABLE }; if (!config.IsFieldExists(crcCheckEnableField)) { return true; } @@ -209,7 +202,7 @@ bool CheckConfigSupport(const JsonObject &config, int &errCode) } return true; } -} +} // namespace DBConfig DBConfig::ReadConfig(const std::string &confStr, int &errCode) { @@ -288,9 +281,9 @@ int32_t DBConfig::GetPageSize() const bool DBConfig::operator==(const DBConfig &targetConfig) const { return configStr_ == targetConfig.configStr_ && pageSize_ == targetConfig.pageSize_ && - redoFlushByTrx_ == targetConfig.redoFlushByTrx_ && redoPubBufSize_ == targetConfig.redoPubBufSize_ && - maxConnNum_ == targetConfig.maxConnNum_ && bufferPoolSize_ == targetConfig.bufferPoolSize_ && - crcCheckEnable_ == targetConfig.crcCheckEnable_; + redoFlushByTrx_ == targetConfig.redoFlushByTrx_ && redoPubBufSize_ == targetConfig.redoPubBufSize_ && + maxConnNum_ == targetConfig.maxConnNum_ && bufferPoolSize_ == targetConfig.bufferPoolSize_ && + crcCheckEnable_ == targetConfig.crcCheckEnable_; } bool DBConfig::operator!=(const DBConfig &targetConfig) const diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp index 47a75e9c..b92474f8 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp @@ -12,16 +12,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "json_common.h" + #include #include -#include "json_common.h" #include "doc_errno.h" #include "log_print.h" #include "securec.h" namespace DocumentDB { -ValueObject JsonCommon::GetValueByFiled(JsonObject &node, const std::string& filed) +ValueObject JsonCommon::GetValueByFiled(JsonObject &node, const std::string &filed) { while (!node.IsNull()) { if (node.GetItemFiled() == filed) { @@ -54,19 +55,19 @@ int JsonCommon::CheckLeafNode(JsonObject &node, std::vector &leafVa return E_OK; } -std::vector JsonCommon::GetLeafValue(JsonObject &node) +std::vector JsonCommon::GetLeafValue(JsonObject &node) { std::vector leafValue; CheckLeafNode(node, leafValue); return leafValue; } -bool JsonCommon::CheckNode(JsonObject &node, std::set filedSet, bool &errFlag) +bool JsonCommon::CheckNode(JsonObject &node, std::set filedSet, bool &errFlag) { if (!errFlag) { return false; } - std::string fieldName; + std::string fieldName; if (!node.IsNull()) { int ret = 0; fieldName = node.GetItemFiled(ret); @@ -78,8 +79,7 @@ bool JsonCommon::CheckNode(JsonObject &node, std::set filedSet, boo errFlag = false; return false; } - } - else { + } else { errFlag = false; return false; } @@ -92,7 +92,7 @@ bool JsonCommon::CheckNode(JsonObject &node, std::set filedSet, boo errFlag = false; return false; } - } + } } if (!node.GetChild().IsNull()) { auto nodeNew = node.GetChild(); @@ -106,19 +106,19 @@ bool JsonCommon::CheckNode(JsonObject &node, std::set filedSet, boo return errFlag; } -bool JsonCommon::CheckJsonField(JsonObject &jsonObj) +bool JsonCommon::CheckJsonField(JsonObject &jsonObj) { std::set filedSet; bool errFlag = true; return CheckNode(jsonObj, filedSet, errFlag); } -bool JsonCommon::CheckProjectionNode(JsonObject &node, std::set filedSet, bool &errFlag, bool isFirstFloor) +bool JsonCommon::CheckProjectionNode(JsonObject &node, std::set filedSet, bool &errFlag, bool isFirstFloor) { if (!errFlag) { return false; } - std::string fieldName; + std::string fieldName; if (!node.IsNull()) { int ret = 0; fieldName = node.GetItemFiled(ret); @@ -130,13 +130,13 @@ bool JsonCommon::CheckProjectionNode(JsonObject &node, std::set fil errFlag = false; return false; } - } - else { + } else { errFlag = false; return false; } for (int i = 0; i < fieldName.size(); i++) { - if (!((isalpha(fieldName[i])) || (isdigit(fieldName[i])) || ('_' == fieldName[i]) || (isFirstFloor && '.' == fieldName[i]))) { + if (!((isalpha(fieldName[i])) || (isdigit(fieldName[i])) || ('_' == fieldName[i]) || + (isFirstFloor && '.' == fieldName[i]))) { errFlag = false; return false; } @@ -144,7 +144,7 @@ bool JsonCommon::CheckProjectionNode(JsonObject &node, std::set fil errFlag = false; return false; } - } + } } if (!node.GetChild().IsNull()) { auto nodeNew = node.GetChild(); @@ -158,7 +158,7 @@ bool JsonCommon::CheckProjectionNode(JsonObject &node, std::set fil return errFlag; } -bool JsonCommon::CheckProjectionField(JsonObject &jsonObj) +bool JsonCommon::CheckProjectionField(JsonObject &jsonObj) { std::set filedSet; bool errFlag = true; @@ -166,7 +166,8 @@ bool JsonCommon::CheckProjectionField(JsonObject &jsonObj) return CheckProjectionNode(jsonObj, filedSet, errFlag, isFirstFloor); } -int JsonCommon::ParseNode(JsonObject &node, std::vector singlePath, std::vector> &resultPath, bool isFirstFloor) +int JsonCommon::ParseNode(JsonObject &node, std::vector singlePath, + std::vector> &resultPath, bool isFirstFloor) { std::vector fatherPath; if (isFirstFloor) { @@ -193,8 +194,7 @@ int JsonCommon::ParseNode(JsonObject &node, std::vector singlePath, if (!node.GetChild().IsNull() && node.GetChild().GetItemFiled() != "") { auto nodeNew = node.GetChild(); ParseNode(nodeNew, singlePath, resultPath, false); - } - else { + } else { resultPath.emplace_back(singlePath); } if (!node.GetNext().IsNull()) { @@ -238,10 +238,10 @@ JsonFieldPath ExpendPath(const JsonFieldPath &path, bool &isCollapse) } void JsonObjectIterator(const JsonObject &obj, JsonFieldPath path, - std::function foo) + std::function foo) { JsonObject child = obj.GetChild(); - while(!child.IsNull()) { + while (!child.IsNull()) { JsonFieldPath childPath = path; childPath.push_back(child.GetItemFiled()); if (foo != nullptr && foo(childPath, obj, child)) { @@ -251,61 +251,61 @@ void JsonObjectIterator(const JsonObject &obj, JsonFieldPath path, } return; } -} +} // namespace int JsonCommon::Append(const JsonObject &src, const JsonObject &add) { int externErrCode = E_OK; - JsonObjectIterator(add, {}, - [&src, &externErrCode](const JsonFieldPath &path, const JsonObject &father, const JsonObject &item) { - bool isCollapse = false; - JsonFieldPath itemPath = ExpendPath(path, isCollapse); - JsonFieldPath fatherPath = itemPath; - fatherPath.pop_back(); - int errCode = E_OK; - if (src.IsFieldExists(itemPath)) { - JsonObject srcItem = src.FindItem(itemPath, errCode); - if (errCode != E_OK) { - externErrCode = (externErrCode == E_OK ? errCode : externErrCode); - GLOGE("Find item in source json object failed. %d", errCode); - return false; - } - if (srcItem.GetType() == JsonObject::Type::JSON_LEAF && item.GetType() == JsonObject::Type::JSON_LEAF) { - srcItem.SetItemValue(item.GetItemValue()); - return false; // Both leaf node, no need iterate - } else if (srcItem.GetType() != item.GetType()) { - JsonObject srcFatherItem = src.FindItem(fatherPath, errCode); + JsonObjectIterator( + add, {}, [&src, &externErrCode](const JsonFieldPath &path, const JsonObject &father, const JsonObject &item) { + bool isCollapse = false; + JsonFieldPath itemPath = ExpendPath(path, isCollapse); + JsonFieldPath fatherPath = itemPath; + fatherPath.pop_back(); + int errCode = E_OK; + if (src.IsFieldExists(itemPath)) { + JsonObject srcItem = src.FindItem(itemPath, errCode); if (errCode != E_OK) { externErrCode = (externErrCode == E_OK ? errCode : externErrCode); - GLOGE("Find father item in source json object failed. %d", errCode); + GLOGE("Find item in source json object failed. %d", errCode); return false; } - srcFatherItem.DeleteItemFromObject(itemPath.back()); - srcFatherItem.AddItemToObject(itemPath.back(), item); - return false; // Different node types, overwrite directly, skip child node - } - return true; // Both array or object - } else { - if (isCollapse) { - GLOGE("Add collapse item to object failed, path not exist."); - externErrCode = -E_DATA_CONFLICT; - return false; - } - JsonObject srcFatherItem = src.FindItem(fatherPath, errCode); - if (errCode == E_OK) { - errCode = srcFatherItem.AddItemToObject(itemPath.back(), item); - if (errCode != E_OK) { - externErrCode = (externErrCode == E_OK ? errCode : externErrCode); - GLOGE("Add item to object failed. %d", errCode); - return false; + if (srcItem.GetType() == JsonObject::Type::JSON_LEAF && item.GetType() == JsonObject::Type::JSON_LEAF) { + srcItem.SetItemValue(item.GetItemValue()); + return false; // Both leaf node, no need iterate + } else if (srcItem.GetType() != item.GetType()) { + JsonObject srcFatherItem = src.FindItem(fatherPath, errCode); + if (errCode != E_OK) { + externErrCode = (externErrCode == E_OK ? errCode : externErrCode); + GLOGE("Find father item in source json object failed. %d", errCode); + return false; + } + srcFatherItem.DeleteItemFromObject(itemPath.back()); + srcFatherItem.AddItemToObject(itemPath.back(), item); + return false; // Different node types, overwrite directly, skip child node } + return true; // Both array or object } else { - externErrCode = -E_DATA_CONFLICT; - GLOGE("Find father item in source json object failed. %d", errCode); + if (isCollapse) { + GLOGE("Add collapse item to object failed, path not exist."); + externErrCode = -E_DATA_CONFLICT; + return false; + } + JsonObject srcFatherItem = src.FindItem(fatherPath, errCode); + if (errCode == E_OK) { + errCode = srcFatherItem.AddItemToObject(itemPath.back(), item); + if (errCode != E_OK) { + externErrCode = (externErrCode == E_OK ? errCode : externErrCode); + GLOGE("Add item to object failed. %d", errCode); + return false; + } + } else { + externErrCode = -E_DATA_CONFLICT; + GLOGE("Find father item in source json object failed. %d", errCode); + } + return false; // Source path not exist, overwrite directly, skip child node } - return false; // Source path not exist, overwrite directly, skip child node - } - }); + }); return externErrCode; } } // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/log_print.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/log_print.cpp index 10fd0fff..e6df873c 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/log_print.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/log_print.cpp @@ -14,8 +14,9 @@ */ #include -#include "securec.h" + #include "hilog/log.h" +#include "securec.h" namespace DocumentDB { namespace { @@ -57,7 +58,7 @@ void PreparePrivateLog(const char *format, std::string &outStrFormat) outStrFormat.replace(pos, PRIVATE_TAG.size(), ".3s"); } } -} +} // namespace void Logger::Log(Level level, const std::string &tag, const char *func, int line, const char *format, ...) { @@ -79,4 +80,4 @@ void Logger::Log(Level level, const std::string &tag, const char *func, int line PrintLog(level, tag, msg); } -} +} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/os_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/os_api.cpp index 31786a74..1af7c1ff 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/os_api.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/os_api.cpp @@ -13,9 +13,10 @@ * limitations under the License. */ #include "os_api.h" + +#include #include #include -#include #include "doc_errno.h" #include "log_print.h" @@ -23,7 +24,7 @@ namespace DocumentDB { namespace { - const int ACCESS_MODE_EXISTENCE = 0; +const int ACCESS_MODE_EXISTENCE = 0; } namespace OSAPI { bool CheckPermission(const std::string &filePath) @@ -49,17 +50,17 @@ int GetRealPath(const std::string &inOriPath, std::string &outRealPath) return -E_OUT_OF_MEMORY; } if (memset_s(realPath, MAX_PATH_LENGTH + 1, 0, MAX_PATH_LENGTH + 1) != EOK) { - delete []realPath; + delete[] realPath; return -E_SECUREC_ERROR; } if (realpath(inOriPath.c_str(), realPath) == nullptr) { GLOGE("[OS_API] Realpath error:%d.", errno); - delete []realPath; + delete[] realPath; return -E_SYSTEM_API_FAIL; } outRealPath = std::string(realPath); - delete []realPath; + delete[] realPath; return E_OK; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/base/grd_db_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/base/grd_db_api.cpp index fff6188d..fa6e5f76 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/base/grd_db_api.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/base/grd_db_api.cpp @@ -18,8 +18,8 @@ #include "doc_errno.h" #include "document_store_manager.h" #include "grd_base/grd_error.h" -#include "log_print.h" #include "grd_type_inner.h" +#include "log_print.h" using namespace DocumentDB; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp index e8370c8d..f24c950f 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp @@ -12,10 +12,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "document_check.h" + #include #include -#include "document_check.h" #include "doc_errno.h" #include "log_print.h" #include "securec.h" @@ -46,7 +47,7 @@ bool CheckCollectionNamePrefix(const std::string &name, const std::string &prefi } return true; } -} +} // namespace bool CheckCommon::CheckCollectionName(const std::string &collectionName, std::string &lowerCaseName, int &errCode) { @@ -65,14 +66,14 @@ bool CheckCommon::CheckCollectionName(const std::string &collectionName, std::st return false; } lowerCaseName = collectionName; - std::transform(lowerCaseName.begin(), lowerCaseName.end(), lowerCaseName.begin(), [](unsigned char c){ + std::transform(lowerCaseName.begin(), lowerCaseName.end(), lowerCaseName.begin(), [](unsigned char c) { return std::tolower(c); }); return true; } int CheckCommon::CheckFilter(JsonObject &filterObj) -{ +{ if (filterObj.GetDeep() > JSON_DEEP_MAX) { GLOGE("filter's json deep is deeper than JSON_DEEP_MAX"); return -E_INVALID_ARGS; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h index 57769260..8b66a70d 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h @@ -18,12 +18,12 @@ #include #include + #include "json_common.h" namespace DocumentDB { class JsonCommon; -class CheckCommon -{ +class CheckCommon { public: CheckCommon() = default; ~CheckCommon() = default; @@ -38,5 +38,5 @@ using Key = std::vector; using Value = std::vector; constexpr const char *COLL_PREFIX = "GRD_COLL_"; -} // DocumentDB +} // namespace DocumentDB #endif // DOCUMENT_CHECK_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_document_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_document_api.cpp index e033b9fb..8bcaa1ab 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_document_api.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_document_api.cpp @@ -14,9 +14,10 @@ */ #include "grd_document/grd_document_api.h" + #include "grd_base/grd_error.h" -#include "grd_type_inner.h" #include "grd_resultset_inner.h" +#include "grd_type_inner.h" #include "log_print.h" using namespace DocumentDB; @@ -86,7 +87,7 @@ int GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, un int ret = db->store_->DeleteDocument(collectionName, filter, flags); int errCode = TrasnferDocErr(ret); int deleteCount = 0; - switch (errCode) { + switch (errCode) { case GRD_OK: deleteCount = 1; return deleteCount; @@ -101,13 +102,13 @@ int GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, un int GRD_FindDoc(GRD_DB *db, const char *collectionName, Query query, unsigned int flags, GRD_ResultSet **resultSet) { - if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || resultSet == nullptr || query.filter == nullptr - || query.projection == nullptr) { + if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || resultSet == nullptr || + query.filter == nullptr || query.projection == nullptr) { return GRD_INVALID_ARGS; } - GRD_ResultSet *grdResultSet = new (std::nothrow)GRD_ResultSet(); + GRD_ResultSet *grdResultSet = new (std::nothrow) GRD_ResultSet(); if (grdResultSet == nullptr) { - GLOGE("Memory allocation failed!" ); + GLOGE("Memory allocation failed!"); return -E_FAILED_MEMORY_ALLOCATE; } int ret = db->store_->FindDocument(collectionName, query.filter, query.projection, flags, grdResultSet); @@ -117,5 +118,5 @@ int GRD_FindDoc(GRD_DB *db, const char *collectionName, Query query, unsigned in return TrasnferDocErr(ret); } *resultSet = grdResultSet; - return TrasnferDocErr(ret); + return TrasnferDocErr(ret); } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_resultset_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_resultset_api.cpp index 6db93e85..e0b1c86a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_resultset_api.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_resultset_api.cpp @@ -12,13 +12,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "grd_base/grd_resultset_api.h" + #include -#include "grd_base/grd_error.h" #include "doc_errno.h" #include "grd_base/grd_error.h" #include "grd_resultset_inner.h" -#include "grd_base/grd_resultset_api.h" #include "log_print.h" using namespace DocumentDB; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_format_config.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_format_config.h index 06e7bbee..e03feb5d 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_format_config.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_format_config.h @@ -16,7 +16,6 @@ #ifndef GRD_FORMAT_CONFIG_H #define GRD_FORMAT_CONFIG_H - #ifdef __cplusplus extern "C" { #endif // __cplusplus diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_type_inner.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_type_inner.h index 63663701..f26f6ac4 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_type_inner.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_type_inner.h @@ -16,8 +16,8 @@ #ifndef GRD_TYPE_INNER_H #define GRD_TYPE_INNER_H -#include "document_store.h" #include "doc_errno.h" +#include "document_store.h" #include "grd_base/grd_error.h" typedef struct GRD_DB { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h index e242bcc7..2fc0556e 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h @@ -17,6 +17,7 @@ #define COLLECTION_H #include + #include "document_check.h" #include "kv_store_executor.h" @@ -24,8 +25,8 @@ namespace DocumentDB { class Collection { public: Collection(const std::string &name, KvStoreExecutor *executor); - Collection(const Collection &a) {}; - Collection() {}; + Collection(const Collection &a){}; + Collection(){}; ~Collection(); int PutDocument(const Key &key, const Value &document); @@ -34,6 +35,7 @@ public: int UpsertDocument(const std::string &id, const std::string &document, bool isReplace = true); bool FindDocument(); int UpdateDocument(const std::string &id, const std::string &document); + private: std::string name_; KvStoreExecutor *executor_ = nullptr; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h index 6f159683..da91d40b 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h @@ -40,5 +40,5 @@ constexpr int E_RESOURCE_BUSY = E_BASE + 50; constexpr int E_FAILED_MEMORY_ALLOCATE = E_BASE + 51; int TrasnferDocErr(int err); -} // DocumentDB +} // namespace DocumentDB #endif // DOC_ERRNO_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store.h index 648c6499..794035c6 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store.h @@ -16,12 +16,12 @@ #ifndef DOCUMENT_STORE_H #define DOCUMENT_STORE_H -#include #include #include +#include -#include "kv_store_executor.h" #include "collection.h" +#include "kv_store_executor.h" struct GRD_ResultSet; namespace DocumentDB { @@ -34,17 +34,20 @@ public: int DropCollection(const std::string &name, int flags); int UpdateDocument(const std::string &collection, const std::string &filter, const std::string &update, int flags); - int UpsertDocument(const std::string &collection, const std::string &filter, const std::string &document, int flags); + int UpsertDocument( + const std::string &collection, const std::string &filter, const std::string &document, int flags); int InsertDocument(const std::string &collection, const std::string &document, int flag); int DeleteDocument(const std::string &collection, const std::string &filter, int flag); - int FindDocument(const std::string &collection, const std::string &filter, const std::string &projection, int flags, GRD_ResultSet *grdResultSet); + int FindDocument(const std::string &collection, const std::string &filter, const std::string &projection, + int flags, GRD_ResultSet *grdResultSet); KvStoreExecutor *GetExecutor(int errCode); int EraseCollection(const std::string collectionName); + private: int GetViewType(JsonObject &jsonObj, bool &viewType); std::mutex dbMutex_; KvStoreExecutor *executor_ = nullptr; - std::map collections_; + std::map collections_; }; -} // DocumentDB +} // namespace DocumentDB #endif // DOCUMENT_STORE_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store_manager.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store_manager.h index 70043162..90b70638 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store_manager.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store_manager.h @@ -17,13 +17,14 @@ #define DOCUMENT_STORE_MANAGER_H #include + #include "document_store.h" namespace DocumentDB { class DocumentStoreManager { public: - static int GetDocumentStore(const std::string &path, const std::string &config, unsigned int flags, - DocumentStore *&store); + static int GetDocumentStore( + const std::string &path, const std::string &config, unsigned int flags, DocumentStore *&store); static int CloseDocumentStore(DocumentStore *store, unsigned int flags); @@ -31,5 +32,5 @@ private: static bool CheckDBPath(const std::string &path, std::string &canonicalPath, std::string &dbName, int &errCode); static bool CheckDBConfig(const std::string &config, int &errCode); }; -} // DocumentDB +} // namespace DocumentDB #endif // DOCUMENT_STORE_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/projection_tree.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/projection_tree.h index 15920bd4..2a5686bf 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/projection_tree.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/projection_tree.h @@ -2,24 +2,27 @@ #define PROJECTION_TREE_H #include -#include #include +#include + #include "doc_errno.h" #include "json_common.h" #include "log_print.h" namespace DocumentDB { struct ProjectionNode { - std::unordered_map SonNode; + std::unordered_map SonNode; bool isDeepest; int Deep; int ViewType; - ProjectionNode() { + ProjectionNode() + { Deep = 0; isDeepest = true; } int DeleteProjectionNode(); - ~ProjectionNode () { + ~ProjectionNode() + { DeleteProjectionNode(); } }; @@ -30,8 +33,9 @@ public: int ParseTree(std::vector> &path); bool SearchTree(std::vector &singlePath, int &index); + private: ProjectionNode node_; }; -} // DocumentDB +} // namespace DocumentDB #endif // PROJECTION_TREE_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set.h index dc87115e..745bfde0 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set.h @@ -17,14 +17,15 @@ #define RESULTSET_H #include -#include "grd_base/grd_type_export.h" -#include "projection_tree.h" -#include "vector" + #include "doc_errno.h" -#include "json_object.h" -#include "securec.h" #include "document_check.h" #include "document_store.h" +#include "grd_base/grd_type_export.h" +#include "json_object.h" +#include "projection_tree.h" +#include "securec.h" +#include "vector" namespace DocumentDB { class ResultSet { @@ -32,13 +33,16 @@ public: ResultSet(); ~ResultSet(); - int Init(DocumentStore *store, const std::string collectionName, ValueObject &key, std::vector> &path, bool ifShowId, bool viewType); + int Init(DocumentStore *store, const std::string collectionName, ValueObject &key, + std::vector> &path, bool ifShowId, bool viewType); int GetNext(); int GetValue(char **value); int EraseCollection(); + private: int CutJsonBranch(std::string &jsonData); - int CheckCutNode(JsonObject *node, std::vector singleCutPath, std::vector> &allCutPath); + int CheckCutNode( + JsonObject *node, std::vector singleCutPath, std::vector> &allCutPath); DocumentStore *store_ = nullptr; std::string collectionName_; ValueObject key_; @@ -49,5 +53,5 @@ private: int index_ = 0; std::vector findValue_; }; -} // DocumentDB +} // namespace DocumentDB #endif //RESULTSET_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set_common.h index dfef9c26..ac7890aa 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set_common.h @@ -17,15 +17,15 @@ #define RESULTSET_COMMON_H #include -#include "grd_base/grd_type_export.h" -#include "vector" + #include "doc_errno.h" +#include "grd_base/grd_type_export.h" #include "result_set.h" +#include "vector" namespace DocumentDB { class ValueObject; -int InitResultSet(DocumentStore *store, const std::string collectionName, ValueObject &key, std::vector> &path, bool ifShowId, bool viewType, - ResultSet &resultSet); -} // DocumentDB +int InitResultSet(DocumentStore *store, const std::string collectionName, ValueObject &key, + std::vector> &path, bool ifShowId, bool viewType, ResultSet &resultSet); +} // namespace DocumentDB #endif //RESULTSET_COMMON_H - diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp index fc9fd3ba..af65939b 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp @@ -17,8 +17,8 @@ #include -#include "document_check.h" #include "doc_errno.h" +#include "document_check.h" #include "log_print.h" namespace DocumentDB { @@ -118,7 +118,7 @@ int Collection::UpsertDocument(const std::string &id, const std::string &documen } std::string valStr = originValue.Print(); - valSet = {valStr.begin(), valStr.end()}; + valSet = { valStr.begin(), valStr.end() }; } } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp index 5bae14d9..af0e0ec7 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp @@ -14,6 +14,7 @@ */ #include "doc_errno.h" + #include "grd_base/grd_error.h" namespace DocumentDB { @@ -27,7 +28,7 @@ int GetErrorCategory(int errCode) int TrasnferDocErr(int err) { - if(err > 0) { + if (err > 0) { return err; } @@ -76,4 +77,4 @@ int TrasnferDocErr(int err) return GetErrorCategory(outErr); } -} \ No newline at end of file +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp index 0bfe0aff..1e43ce53 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp @@ -14,22 +14,21 @@ */ #include "document_store.h" + #include "collection_option.h" -#include "document_check.h" #include "doc_errno.h" +#include "document_check.h" #include "grd_base/grd_type_export.h" +#include "grd_resultset_inner.h" #include "log_print.h" #include "result_set_common.h" -#include "grd_resultset_inner.h" namespace DocumentDB { const int JSON_LENS_MAX = 512 * 1024; constexpr const char *KEY_ID = "_id"; const bool caseIsSensitive = true; -DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) -{ -} +DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) {} DocumentStore::~DocumentStore() { @@ -158,10 +157,10 @@ bool CheckDocument(const std::string &updateStr, int &errCode) return true; } -} +} // namespace -int DocumentStore::UpdateDocument(const std::string &collection, const std::string &filter, const std::string &update, - int flags) +int DocumentStore::UpdateDocument( + const std::string &collection, const std::string &filter, const std::string &update, int flags) { std::string lowerCaseCollName; int errCode = E_OK; @@ -197,8 +196,8 @@ int DocumentStore::UpdateDocument(const std::string &collection, const std::stri return errCode; } -int DocumentStore::UpsertDocument(const std::string &collection, const std::string &filter, const std::string &document, - int flags) +int DocumentStore::UpsertDocument( + const std::string &collection, const std::string &filter, const std::string &document, int flags) { std::string lowerCaseCollName; int errCode = E_OK; @@ -311,12 +310,13 @@ KvStoreExecutor *DocumentStore::GetExecutor(int errCode) { return executor_; } -int DocumentStore::FindDocument(const std::string &collection, const std::string &filter, const std::string &projection, - int flags, GRD_ResultSet *grdResultSet) +int DocumentStore::FindDocument(const std::string &collection, const std::string &filter, + const std::string &projection, int flags, GRD_ResultSet *grdResultSet) { if (flags != 0 && flags != GRD_DOC_ID_DISPLAY) { GLOGE("FindDocument flag is illegal"); - return -E_INVALID_ARGS;; + return -E_INVALID_ARGS; + ; } std::string lowerCaseCollName; int errCode = E_OK; @@ -385,7 +385,8 @@ int DocumentStore::FindDocument(const std::string &collection, const std::string return ret; } -int DocumentStore::EraseCollection(const std::string collectionName) { +int DocumentStore::EraseCollection(const std::string collectionName) +{ if (collections_.find(collectionName) != collections_.end()) { collections_.erase(collectionName); return E_OK; @@ -394,54 +395,52 @@ int DocumentStore::EraseCollection(const std::string collectionName) { return E_OK; } -int DocumentStore::GetViewType(JsonObject &jsonObj, bool &viewType) { +int DocumentStore::GetViewType(JsonObject &jsonObj, bool &viewType) +{ auto leafValue = JsonCommon::GetLeafValue(jsonObj); if (leafValue.size() == 0) { return E_INVALID_ARGS; } for (size_t i = 0; i < leafValue.size(); i++) { switch (leafValue[i].GetValueType()) { - case ValueObject::ValueType::VALUE_BOOL: - if (leafValue[i].GetBoolValue()) { - if (i != 0 && !viewType) { - return -E_INVALID_ARGS; - } - viewType = true; - } - else { - if (i != 0 && viewType) { - return E_INVALID_ARGS; - } - viewType = false; - } - break; - case ValueObject::ValueType::VALUE_STRING: - if (leafValue[i].GetStringValue() == "") { - if (i != 0 && !viewType) { - return -E_INVALID_ARGS; + case ValueObject::ValueType::VALUE_BOOL: + if (leafValue[i].GetBoolValue()) { + if (i != 0 && !viewType) { + return -E_INVALID_ARGS; + } + viewType = true; + } else { + if (i != 0 && viewType) { + return E_INVALID_ARGS; + } + viewType = false; } - viewType = true; - } - else { - return -E_INVALID_ARGS; - } - break; - case ValueObject::ValueType::VALUE_NUMBER: - if (leafValue[i].GetIntValue() == 0) { - if (i != 0 && viewType) { + break; + case ValueObject::ValueType::VALUE_STRING: + if (leafValue[i].GetStringValue() == "") { + if (i != 0 && !viewType) { + return -E_INVALID_ARGS; + } + viewType = true; + } else { return -E_INVALID_ARGS; } - viewType = false; - } - else { - if (i != 0 && !viewType) { - return E_INVALID_ARGS; + break; + case ValueObject::ValueType::VALUE_NUMBER: + if (leafValue[i].GetIntValue() == 0) { + if (i != 0 && viewType) { + return -E_INVALID_ARGS; + } + viewType = false; + } else { + if (i != 0 && !viewType) { + return E_INVALID_ARGS; + } + viewType = true; } - viewType = true; - } - break; - default: - return E_INVALID_ARGS; + break; + default: + return E_INVALID_ARGS; } } return E_OK; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store_manager.cpp index f4f2faac..28906838 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store_manager.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store_manager.cpp @@ -13,8 +13,9 @@ * limitations under the License. */ -#include "db_config.h" #include "document_store_manager.h" + +#include "db_config.h" #include "doc_errno.h" #include "grd_base/grd_type_export.h" #include "kv_store_manager.h" @@ -42,10 +43,10 @@ bool CheckDBCreate(unsigned int flags, const std::string &path) } return true; } -} +} // namespace -int DocumentStoreManager::GetDocumentStore(const std::string &path, const std::string &config, unsigned int flags, - DocumentStore *&store) +int DocumentStoreManager::GetDocumentStore( + const std::string &path, const std::string &config, unsigned int flags, DocumentStore *&store) { std::string canonicalPath; std::string dbName; @@ -80,7 +81,7 @@ int DocumentStoreManager::GetDocumentStore(const std::string &path, const std::s store = new (std::nothrow) DocumentStore(executor); if (store == nullptr) { - GLOGE("Memory allocation failed!" ); + GLOGE("Memory allocation failed!"); return -E_FAILED_MEMORY_ALLOCATE; } if (store == nullptr) { @@ -101,8 +102,8 @@ int DocumentStoreManager::CloseDocumentStore(DocumentStore *store, unsigned int return E_OK; } -bool DocumentStoreManager::CheckDBPath(const std::string &path, std::string &canonicalPath, std::string &dbName, - int &errCode) +bool DocumentStoreManager::CheckDBPath( + const std::string &path, std::string &canonicalPath, std::string &dbName, int &errCode) { if (path.empty()) { GLOGE("Invalid path empty"); @@ -139,4 +140,4 @@ bool DocumentStoreManager::CheckDBConfig(const std::string &config, int &errCode { return true; } -} // DocumentDB \ No newline at end of file +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/projection_tree.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/projection_tree.cpp index edf6f356..cba39d51 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/projection_tree.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/projection_tree.cpp @@ -1,17 +1,16 @@ -#include #include "projection_tree.h" +#include namespace DocumentDB { const int JSON_DEEP_MAX = 4; -ProjectionTree::ProjectionTree() { -} +ProjectionTree::ProjectionTree() {} -ProjectionTree::~ProjectionTree() { -} +ProjectionTree::~ProjectionTree() {} -int ProjectionTree::ParseTree(std::vector> &path) { +int ProjectionTree::ParseTree(std::vector> &path) +{ ProjectionNode *node = &node_; if (node == NULL) { return E_OK; @@ -27,11 +26,10 @@ int ProjectionTree::ParseTree(std::vector> &path) { if (j == path[i].size() - 1 && !node->isDeepest) { return -E_INVALID_ARGS; } - } - else { + } else { auto tempNode = new (std::nothrow) ProjectionNode; if (tempNode == nullptr) { - GLOGE("Memory allocation failed!" ); + GLOGE("Memory allocation failed!"); return -E_FAILED_MEMORY_ALLOCATE; } tempNode->Deep = node->Deep + 1; @@ -48,7 +46,8 @@ int ProjectionTree::ParseTree(std::vector> &path) { return E_OK; } -bool ProjectionTree::SearchTree(std::vector &singlePath, int &index) { +bool ProjectionTree::SearchTree(std::vector &singlePath, int &index) +{ ProjectionNode *node = &node_; for (int i = 0; i < singlePath.size(); i++) { if (node->isDeepest) { @@ -56,15 +55,15 @@ bool ProjectionTree::SearchTree(std::vector &singlePath, int &index } if (node->SonNode[singlePath[i]] != nullptr) { node = node->SonNode[singlePath[i]]; - } - else { + } else { return false; } } return true; } -int ProjectionNode::DeleteProjectionNode() { +int ProjectionNode::DeleteProjectionNode() +{ for (auto item : SonNode) { if (item.second != nullptr) { delete item.second; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp index 223cee45..5c86d381 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp @@ -13,19 +13,14 @@ * limitations under the License. */ #include "result_set.h" + #include "log_print.h" namespace DocumentDB { constexpr const char *KEY_ID = "_id"; -ResultSet::ResultSet() -{ - -} -ResultSet::~ResultSet() -{ - -} +ResultSet::ResultSet() {} +ResultSet::~ResultSet() {} int ResultSet::EraseCollection() { if (store_ != nullptr) { @@ -33,9 +28,10 @@ int ResultSet::EraseCollection() } return E_OK; } -int ResultSet::Init(DocumentStore *store, const std::string collectionName, ValueObject &key, std::vector> &path, bool ifShowId, bool viewType) +int ResultSet::Init(DocumentStore *store, const std::string collectionName, ValueObject &key, + std::vector> &path, bool ifShowId, bool viewType) { - store_ = store; + store_ = store; collectionName_ = collectionName; key_ = key; projectionPath_ = path; @@ -43,7 +39,7 @@ int ResultSet::Init(DocumentStore *store, const std::string collectionName, Valu GLOGE("Parse ProjectionTree failed"); return -E_INVALID_ARGS; } - ifShowId_ = ifShowId; + ifShowId_ = ifShowId; viewType_ = viewType; findValue_.reserve(1 + 1); return E_OK; @@ -78,29 +74,31 @@ int ResultSet::GetNext() return E_OK; } -int ResultSet::GetValue(char **value) +int ResultSet::GetValue(char **value) { if (findValue_.size() == 0) { GLOGE("The value vector in resultSet is empty"); return -E_NO_DATA; } - auto jsonData = findValue_.back(); + auto jsonData = findValue_.back(); char *jsonstr = new char[jsonData.size() + 1]; if (jsonstr == nullptr) { - GLOGE("Memory allocation failed!" ); + GLOGE("Memory allocation failed!"); return -E_FAILED_MEMORY_ALLOCATE; } errno_t err = strcpy_s(jsonstr, jsonData.size() + 1, jsonData.c_str()); if (err != 0) { GLOGE("strcpy_s failed"); delete[] jsonstr; - return -E_NO_DATA;; + return -E_NO_DATA; + ; } *value = jsonstr; return E_OK; } -int ResultSet::CheckCutNode(JsonObject *node, std::vector singlePath, std::vector> &allCutPath) +int ResultSet::CheckCutNode( + JsonObject *node, std::vector singlePath, std::vector> &allCutPath) { if (node == nullptr) { GLOGE("No node to cut"); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set_common.cpp index ad195a6b..99399efe 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set_common.cpp @@ -13,18 +13,19 @@ * limitations under the License. */ -#include "doc_errno.h" -#include "grd_base/grd_error.h" #include "result_set_common.h" + #include #include +#include "doc_errno.h" +#include "grd_base/grd_error.h" + namespace DocumentDB { class ValueObject; -int InitResultSet(DocumentStore *store, const std::string collectionName, ValueObject &key, std::vector> &path, bool ifShowId, bool viewType, - ResultSet &resultSet) +int InitResultSet(DocumentStore *store, const std::string collectionName, ValueObject &key, + std::vector> &path, bool ifShowId, bool viewType, ResultSet &resultSet) { return resultSet.Init(store, collectionName, key, path, ifShowId, viewType); } -} - +} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h index 12510f1d..2a92b01d 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h @@ -18,15 +18,16 @@ #include #include -#include #include +#include #include "cJSON.h" namespace DocumentDB { class ValueObject { public: - enum class ValueType { + enum class ValueType + { VALUE_NULL = 0, VALUE_BOOL, VALUE_NUMBER, @@ -62,7 +63,7 @@ class JsonObject { public: static JsonObject Parse(const std::string &jsonStr, int &errCode, bool caseSensitive = false); - ~JsonObject (); + ~JsonObject(); std::string Print() const; @@ -78,7 +79,7 @@ public: ValueObject GetItemValue() const; void SetItemValue(const ValueObject &value) const; - + std::string GetItemFiled() const; std::string GetItemFiled(int &errCode) const; @@ -89,7 +90,8 @@ public: int DeleteItemDeeplyOnTarget(const JsonFieldPath &path); bool IsNull() const; int GetDeep(); - enum class Type { + enum class Type + { JSON_LEAF, JSON_OBJECT, JSON_ARRAY @@ -108,6 +110,5 @@ private: bool isOwner_ = false; bool caseSensitive_ = false; }; -} // DocumentDB +} // namespace DocumentDB #endif // JSON_OBJECT_H - diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h index 0f81f5b1..59299f7b 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h @@ -17,6 +17,7 @@ #define KV_STORE_EXECUTOR_H #include + #include "document_check.h" namespace DocumentDB { @@ -36,5 +37,5 @@ public: virtual int SetCollectionOption(const std::string &name, const std::string &option) = 0; virtual int CleanCollectionOption(const std::string &name) = 0; }; -} // DocumentDB +} // namespace DocumentDB #endif // KV_STORE_EXECUTOR_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_manager.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_manager.h index c1df1072..2a84cf89 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_manager.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_manager.h @@ -26,5 +26,5 @@ class KvStoreManager { public: static int GetKvStore(const std::string &path, const DBConfig &config, KvStoreExecutor *&executor); }; -} // DocumentDB +} // namespace DocumentDB #endif // KV_STORE_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp index 3966a72e..56412228 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp @@ -14,7 +14,9 @@ */ #include "json_object.h" + #include + #include "doc_errno.h" #include "log_print.h" @@ -27,7 +29,7 @@ bool IsNumber(const std::string &str) return std::isdigit(c); }); } -} +} // namespace ValueObject::ValueObject(bool val) { @@ -126,7 +128,6 @@ int JsonObject::GetDeep() } jsonDeep_ = GetDeep(cjson_); return jsonDeep_; - } int JsonObject::GetDeep(cJSON *cjson) { @@ -145,7 +146,6 @@ int JsonObject::GetDeep(cJSON *cjson) return depth; } - int JsonObject::CheckNumber(cJSON *item, int &errCode) { if (item != NULL && cJSON_IsNumber(item)) { @@ -339,7 +339,7 @@ void JsonObject::SetItemValue(const ValueObject &value) const if (cjson_ == nullptr) { return; } - switch(value.GetValueType()) { + switch (value.GetValueType()) { case ValueObject::ValueType::VALUE_NUMBER: cJSON_SetNumberValue(cjson_, value.GetDoubleValue()); break; @@ -359,7 +359,7 @@ std::string JsonObject::GetItemFiled() const if (cjson_->string == nullptr) { cJSON *tail = cjson_; - while(tail->next != nullptr) { + while (tail->next != nullptr) { tail = tail->next; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/kv_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/kv_store_manager.cpp index ac45c871..40fe20e5 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/kv_store_manager.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/kv_store_manager.cpp @@ -13,8 +13,9 @@ * limitations under the License. */ -#include "doc_errno.h" #include "kv_store_manager.h" + +#include "doc_errno.h" #include "log_print.h" #include "sqlite_store_executor_impl.h" #include "sqlite_utils.h" @@ -66,4 +67,4 @@ END: sqliteExecutor = nullptr; return errCode; } -} \ No newline at end of file +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp index 937a2793..8dc09ad4 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -13,11 +13,12 @@ * limitations under the License. */ -#include "document_check.h" +#include "sqlite_store_executor_impl.h" + #include "doc_errno.h" +#include "document_check.h" #include "log_print.h" #include "sqlite_utils.h" -#include "sqlite_store_executor_impl.h" namespace DocumentDB { int SqliteStoreExecutor::CreateDatabase(const std::string &path, const DBConfig &config, sqlite3 *&db) @@ -53,9 +54,7 @@ END: return errCode; } -SqliteStoreExecutor::SqliteStoreExecutor(sqlite3 *handle) : dbHandle_(handle) -{ -} +SqliteStoreExecutor::SqliteStoreExecutor(sqlite3 *handle) : dbHandle_(handle) {} SqliteStoreExecutor::~SqliteStoreExecutor() { @@ -66,7 +65,7 @@ SqliteStoreExecutor::~SqliteStoreExecutor() int SqliteStoreExecutor::GetDBConfig(std::string &config) { std::string dbConfigKeyStr = "DB_CONFIG"; - Key dbConfigKey = {dbConfigKeyStr.begin(), dbConfigKeyStr.end()}; + Key dbConfigKey = { dbConfigKeyStr.begin(), dbConfigKeyStr.end() }; Value dbConfigVal; int errCode = GetData("grd_meta", dbConfigKey, dbConfigVal); config.assign(dbConfigVal.begin(), dbConfigVal.end()); @@ -76,8 +75,8 @@ int SqliteStoreExecutor::GetDBConfig(std::string &config) int SqliteStoreExecutor::SetDBConfig(const std::string &config) { std::string dbConfigKeyStr = "DB_CONFIG"; - Key dbConfigKey = {dbConfigKeyStr.begin(), dbConfigKeyStr.end()}; - Value dbConfigVal = {config.begin(), config.end()}; + Key dbConfigKey = { dbConfigKeyStr.begin(), dbConfigKeyStr.end() }; + Value dbConfigVal = { config.begin(), config.end() }; return PutData("grd_meta", dbConfigKey, dbConfigVal); } @@ -88,11 +87,14 @@ int SqliteStoreExecutor::PutData(const std::string &collName, const Key &key, co } std::string sql = "INSERT OR REPLACE INTO '" + collName + "' VALUES (?,?);"; - int errCode = SQLiteUtils::ExecSql(dbHandle_, sql, [key, value](sqlite3_stmt *stmt) { - SQLiteUtils::BindBlobToStatement(stmt, 1, key); - SQLiteUtils::BindBlobToStatement(stmt, 2, value); - return E_OK; - }, nullptr); + int errCode = SQLiteUtils::ExecSql( + dbHandle_, sql, + [key, value](sqlite3_stmt *stmt) { + SQLiteUtils::BindBlobToStatement(stmt, 1, key); + SQLiteUtils::BindBlobToStatement(stmt, 2, value); + return E_OK; + }, + nullptr); if (errCode != SQLITE_OK) { GLOGE("[sqlite executor] Put data failed. err=%d", errCode); if (errCode == -E_ERROR) { @@ -112,14 +114,17 @@ int SqliteStoreExecutor::GetData(const std::string &collName, const Key &key, Va } int innerErrorCode = -E_NOT_FOUND; std::string sql = "SELECT value FROM '" + collName + "' WHERE key=?;"; - int errCode = SQLiteUtils::ExecSql(dbHandle_, sql, [key](sqlite3_stmt *stmt) { - SQLiteUtils::BindBlobToStatement(stmt, 1, key); - return E_OK; - }, [&value, &innerErrorCode](sqlite3_stmt *stmt) { - SQLiteUtils::GetColumnBlobValue(stmt, 0, value); - innerErrorCode = E_OK; - return E_OK; - }); + int errCode = SQLiteUtils::ExecSql( + dbHandle_, sql, + [key](sqlite3_stmt *stmt) { + SQLiteUtils::BindBlobToStatement(stmt, 1, key); + return E_OK; + }, + [&value, &innerErrorCode](sqlite3_stmt *stmt) { + SQLiteUtils::GetColumnBlobValue(stmt, 0, value); + innerErrorCode = E_OK; + return E_OK; + }); if (errCode != SQLITE_OK) { GLOGE("[sqlite executor] Get data failed. err=%d", errCode); return errCode; @@ -142,10 +147,13 @@ int SqliteStoreExecutor::DelData(const std::string &collName, const Key &key) return -E_NO_DATA; } std::string sql = "DELETE FROM '" + collName + "' WHERE key=?;"; - errCode = SQLiteUtils::ExecSql(dbHandle_, sql, [key](sqlite3_stmt *stmt) { - SQLiteUtils::BindBlobToStatement(stmt, 1, key); - return E_OK; - }, nullptr); + errCode = SQLiteUtils::ExecSql( + dbHandle_, sql, + [key](sqlite3_stmt *stmt) { + SQLiteUtils::BindBlobToStatement(stmt, 1, key); + return E_OK; + }, + nullptr); if (errCode != SQLITE_OK) { GLOGE("[sqlite executor] Delete data failed. err=%d", errCode); @@ -217,13 +225,16 @@ bool SqliteStoreExecutor::IsCollectionExists(const std::string &name, int &errCo bool isExists = false; std::string sql = "SELECT tbl_name FROM sqlite_master WHERE tbl_name=?;"; - errCode = SQLiteUtils::ExecSql(dbHandle_, sql, [name](sqlite3_stmt *stmt) { - SQLiteUtils::BindTextToStatement(stmt, 1, name); - return E_OK; - }, [&isExists](sqlite3_stmt *stmt) { - isExists = true; - return E_OK; - }); + errCode = SQLiteUtils::ExecSql( + dbHandle_, sql, + [name](sqlite3_stmt *stmt) { + SQLiteUtils::BindTextToStatement(stmt, 1, name); + return E_OK; + }, + [&isExists](sqlite3_stmt *stmt) { + isExists = true; + return E_OK; + }); if (errCode != E_OK) { GLOGE("Check collection exist failed. %d", errCode); @@ -235,7 +246,7 @@ bool SqliteStoreExecutor::IsCollectionExists(const std::string &name, int &errCo int SqliteStoreExecutor::GetCollectionOption(const std::string &name, std::string &option) { std::string collOptKeyStr = "COLLECTION_OPTION_" + name; - Key collOptKey = {collOptKeyStr.begin(), collOptKeyStr.end()}; + Key collOptKey = { collOptKeyStr.begin(), collOptKeyStr.end() }; Value collOptVal; int errCode = GetData("grd_meta", collOptKey, collOptVal); option.assign(collOptVal.begin(), collOptVal.end()); @@ -245,16 +256,16 @@ int SqliteStoreExecutor::GetCollectionOption(const std::string &name, std::strin int SqliteStoreExecutor::SetCollectionOption(const std::string &name, const std::string &option) { std::string collOptKeyStr = "COLLECTION_OPTION_" + name; - Key collOptKey = {collOptKeyStr.begin(), collOptKeyStr.end()}; - Value collOptVal = {option.begin(), option.end()}; + Key collOptKey = { collOptKeyStr.begin(), collOptKeyStr.end() }; + Value collOptVal = { option.begin(), option.end() }; return PutData("grd_meta", collOptKey, collOptVal); } int SqliteStoreExecutor::CleanCollectionOption(const std::string &name) { std::string collOptKeyStr = "COLLECTION_OPTION_" + name; - Key collOptKey = {collOptKeyStr.begin(), collOptKeyStr.end()}; + Key collOptKey = { collOptKeyStr.begin(), collOptKeyStr.end() }; return DelData("grd_meta", collOptKey); } -} // DocumentDB \ No newline at end of file +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h index bc6857b8..514b9ae1 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h @@ -46,5 +46,5 @@ public: private: sqlite3 *dbHandle_ = nullptr; }; -} // DocumentDB +} // namespace DocumentDB #endif // SQLITE_STORE_EXECUTOR_IMPL_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.cpp index bb86791f..49f72653 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.cpp @@ -15,13 +15,14 @@ #include "sqlite_utils.h" #include + #include "doc_errno.h" #include "log_print.h" namespace DocumentDB { const int MAX_BLOB_READ_SIZE = 5 * 1024 * 1024; // 5M limit const int MAX_TEXT_READ_SIZE = 5 * 1024 * 1024; // 5M limit -const int BUSY_TIMEOUT_MS = 3000; // 3000ms for sqlite busy timeout. +const int BUSY_TIMEOUT_MS = 3000; // 3000ms for sqlite busy timeout. const std::string BEGIN_SQL = "BEGIN TRANSACTION"; const std::string BEGIN_IMMEDIATE_SQL = "BEGIN IMMEDIATE TRANSACTION"; const std::string COMMIT_SQL = "COMMIT TRANSACTION"; @@ -44,7 +45,7 @@ int MapSqliteError(int errCode) std::mutex g_logConfigMutex; bool g_configLog = false; -} +} // namespace void SQLiteUtils::SqliteLogCallback(void *data, int err, const char *msg) { @@ -154,8 +155,8 @@ int SQLiteUtils::BindBlobToStatement(sqlite3_stmt *statement, int index, const s if (value.empty()) { errCode = sqlite3_bind_zeroblob(statement, index, -1); // -1 for zero-length blob. } else { - errCode = sqlite3_bind_blob(statement, index, static_cast(value.data()), - value.size(), SQLITE_TRANSIENT); + errCode = sqlite3_bind_blob( + statement, index, static_cast(value.data()), value.size(), SQLITE_TRANSIENT); } if (errCode != SQLITE_OK) { @@ -174,7 +175,7 @@ int SQLiteUtils::GetColumnBlobValue(sqlite3_stmt *statement, int index, std::vec if (keySize < 0 || keySize > MAX_BLOB_READ_SIZE) { GLOGW("[SQLiteUtils][Column blob] size over limit:%d", keySize); value.resize(MAX_BLOB_READ_SIZE + 1); // Reset value size to invalid - return E_OK; // Return OK for continue get data, but value is invalid + return E_OK; // Return OK for continue get data, but value is invalid } auto keyRead = static_cast(sqlite3_column_blob(statement, index)); @@ -213,7 +214,7 @@ int SQLiteUtils::GetColumnTextValue(sqlite3_stmt *statement, int index, std::str if (valSize < 0 || valSize > MAX_TEXT_READ_SIZE) { GLOGW("[SQLiteUtils][Column text] size over limit:%d", valSize); value.resize(MAX_TEXT_READ_SIZE + 1); // Reset value size to invalid - return E_OK; // Return OK for continue get data, but value is invalid + return E_OK; // Return OK for continue get data, but value is invalid } const unsigned char *val = sqlite3_column_text(statement, index); @@ -261,8 +262,8 @@ int SQLiteUtils::ExecSql(sqlite3 *db, const std::string &sql) return MapSqliteError(errCode); } -int SQLiteUtils::ExecSql(sqlite3 *db, const std::string &sql, const std::function &bindCallback, - const std::function &resultCallback) +int SQLiteUtils::ExecSql(sqlite3 *db, const std::string &sql, const std::function &bindCallback, + const std::function &resultCallback) { if (db == nullptr || sql.empty()) { return -E_INVALID_ARGS; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.h index 597a576e..42606f43 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.h @@ -23,7 +23,8 @@ #include "sqlite3sym.h" namespace DocumentDB { -enum class TransactType { +enum class TransactType +{ DEFERRED, IMMEDIATE, }; @@ -47,8 +48,8 @@ public: static int RollbackTransaction(sqlite3 *db); static int ExecSql(sqlite3 *db, const std::string &sql); - static int ExecSql(sqlite3 *db, const std::string &sql, const std::function &bindCallback, - const std::function &resultCallback); + static int ExecSql(sqlite3 *db, const std::string &sql, const std::function &bindCallback, + const std::function &resultCallback); private: static void SqliteLogCallback(void *data, int err, const char *msg); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_api_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_api_test.cpp index c442be61..47182cfa 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_api_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_api_test.cpp @@ -17,10 +17,10 @@ #include "doc_errno.h" #include "documentdb_test_utils.h" -#include "log_print.h" #include "grd_base/grd_db_api.h" #include "grd_base/grd_error.h" #include "grd_document/grd_document_api.h" +#include "log_print.h" #include "sqlite_utils.h" using namespace DocumentDB; @@ -35,17 +35,11 @@ public: void TearDown(); }; -void DocumentDBApiTest::SetUpTestCase(void) -{ -} +void DocumentDBApiTest::SetUpTestCase(void) {} -void DocumentDBApiTest::TearDownTestCase(void) -{ -} +void DocumentDBApiTest::TearDownTestCase(void) {} -void DocumentDBApiTest::SetUp(void) -{ -} +void DocumentDBApiTest::SetUp(void) {} void DocumentDBApiTest::TearDown(void) { @@ -120,11 +114,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBTest002, TestSize.Level0) HWTEST_F(DocumentDBApiTest, OpenDBPathTest001, TestSize.Level0) { GRD_DB *db = nullptr; - std::vector invalidPath = { - nullptr, - "", - "/a/b/c/" - }; + std::vector invalidPath = { nullptr, "", "/a/b/c/" }; for (auto path : invalidPath) { GLOGD("OpenDBPathTest001: open db with path: %s", path); int status = GRD_DBOpen(path, nullptr, GRD_DB_OPEN_CREATE, &db); @@ -157,7 +147,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBPathTest002, TestSize.Level0) HWTEST_F(DocumentDBApiTest, OpenDBConfigTest001, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; const int MAX_JSON_LEN = 512 * 1024; std::string configStr = std::string(MAX_JSON_LEN, 'a'); int status = GRD_DBOpen(path.c_str(), configStr.c_str(), GRD_DB_OPEN_CREATE, &db); @@ -174,7 +164,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigTest001, TestSize.Level0) HWTEST_F(DocumentDBApiTest, OpenDBConfigTest002, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; int status = GRD_DBOpen(path.c_str(), "{aa}", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_INVALID_FORMAT); } @@ -189,7 +179,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigTest002, TestSize.Level0) HWTEST_F(DocumentDBApiTest, OpenDBConfigTest003, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; int status = GRD_DBOpen(path.c_str(), R""({"notSupport":123})"", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_INVALID_ARGS); } @@ -204,7 +194,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigTest003, TestSize.Level0) HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest001, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; std::vector configList = { R""({"maxConnNum":0})"", @@ -232,7 +222,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest001, TestSize.Level0) HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest002, TestSize.Level1) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; for (int i = 16; i <= 1024; i++) { std::string config = "{\"maxConnNum\":" + std::to_string(i) + "}"; @@ -258,7 +248,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest002, TestSize.Level1) HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest003, TestSize.Level1) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; std::string config = R""({"maxConnNum":16})""; int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); @@ -284,7 +274,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest003, TestSize.Level1) */ HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest004, TestSize.Level1) { - std::string path= "./document.db"; + std::string path = "./document.db"; int maxCnt = 16; std::string config = "{\"maxConnNum\":" + std::to_string(maxCnt) + "}"; @@ -322,7 +312,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest004, TestSize.Level1) HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest001, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; std::vector configList = { R""({"pageSize":0})"", @@ -359,7 +349,7 @@ int GetDBPageSize(const std::string &path) sqlite3_close_v2(db); return pageSize; } -} +} // namespace /** * @tc.name: OpenDBConfigPageSizeTest002 @@ -371,9 +361,9 @@ int GetDBPageSize(const std::string &path) HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest002, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; - for (int size : {4, 8, 16, 32, 64}) { + for (int size : { 4, 8, 16, 32, 64 }) { std::string config = "{\"pageSize\":" + std::to_string(size) + "}"; int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OK); @@ -397,7 +387,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest002, TestSize.Level0) HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest003, TestSize.Level1) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; std::string config = R""({"pageSize":4})""; int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); @@ -424,9 +414,9 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest003, TestSize.Level1) HWTEST_F(DocumentDBApiTest, OpenDBConfigRedoFlushTest001, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; - for (int flush : {0, 1}) { + for (int flush : { 0, 1 }) { std::string config = "{\"redoFlushByTrx\":" + std::to_string(flush) + "}"; int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OK); @@ -449,7 +439,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigRedoFlushTest001, TestSize.Level0) HWTEST_F(DocumentDBApiTest, OpenDBConfigRedoFlushTest002, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; std::string config = R""({"redoFlushByTrx":3})""; int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); @@ -466,14 +456,9 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigRedoFlushTest002, TestSize.Level0) HWTEST_F(DocumentDBApiTest, OpenDBFlagTest001, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; - std::vector invaldFlag = { - GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK, - GRD_DB_OPEN_CREATE | GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK, - 0x08, - 0xffff, - UINT32_MAX - }; + std::string path = "./document.db"; + std::vector invaldFlag = { GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK, + GRD_DB_OPEN_CREATE | GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK, 0x08, 0xffff, UINT32_MAX }; for (unsigned int flag : invaldFlag) { GLOGD("OpenDBFlagTest001: open doc db with flag %u", flag); int status = GRD_DBOpen(path.c_str(), "", flag, &db); @@ -491,7 +476,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBFlagTest001, TestSize.Level0) HWTEST_F(DocumentDBApiTest, OpenDBFlagTest002, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_ONLY, &db); EXPECT_EQ(status, GRD_INVALID_ARGS); @@ -554,7 +539,7 @@ HWTEST_F(DocumentDBApiTest, CloseDBTest001, TestSize.Level0) HWTEST_F(DocumentDBApiTest, CloseDBFlagTest001, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OK); ASSERT_NE(db, nullptr); @@ -576,7 +561,7 @@ HWTEST_F(DocumentDBApiTest, CloseDBFlagTest001, TestSize.Level0) HWTEST_F(DocumentDBApiTest, CloseDBFlagTest002, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OK); ASSERT_NE(db, nullptr); @@ -600,17 +585,12 @@ HWTEST_F(DocumentDBApiTest, CloseDBFlagTest002, TestSize.Level0) HWTEST_F(DocumentDBApiTest, CloseDBFlagTest003, TestSize.Level0) { GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OK); ASSERT_NE(db, nullptr); - std::vector invaldFlag = { - 0x02, - 0x03, - 0xffff, - UINT32_MAX - }; + std::vector invaldFlag = { 0x02, 0x03, 0xffff, UINT32_MAX }; for (unsigned int flag : invaldFlag) { GLOGD("CloseDBFlagTest003: close doc db with flag %u", flag); status = GRD_DBClose(db, flag); @@ -637,14 +617,14 @@ HWTEST_F(DocumentDBApiTest, FlushDBTest001, TestSize.Level0) EXPECT_EQ(GRD_Flush(nullptr, GRD_DB_FLUSH_SYNC), GRD_INVALID_ARGS); GRD_DB *db = nullptr; - std::string path= "./document.db"; + std::string path = "./document.db"; int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OK); ASSERT_NE(db, nullptr); EXPECT_EQ(GRD_Flush(db, GRD_DB_FLUSH_ASYNC), GRD_OK); EXPECT_EQ(GRD_Flush(db, GRD_DB_FLUSH_SYNC), GRD_OK); - std::vector invalidFlags = {2, 4, 8, 512, 1024, UINT32_MAX}; + std::vector invalidFlags = { 2, 4, 8, 512, 1024, UINT32_MAX }; for (auto flags : invalidFlags) { EXPECT_EQ(GRD_Flush(db, flags), GRD_INVALID_ARGS); } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_collection_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_collection_test.cpp index a15df4df..a5e72086 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_collection_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_collection_test.cpp @@ -17,10 +17,10 @@ #include "doc_errno.h" #include "documentdb_test_utils.h" -#include "log_print.h" #include "grd_base/grd_db_api.h" #include "grd_base/grd_error.h" #include "grd_document/grd_document_api.h" +#include "log_print.h" #include "sqlite_utils.h" using namespace DocumentDB; @@ -30,7 +30,7 @@ using namespace DocumentDBUnitTest; namespace { std::string g_path = "./document.db"; GRD_DB *g_db = nullptr; -} +} // namespace class DocumentDBCollectionTest : public testing::Test { public: @@ -40,13 +40,9 @@ public: void TearDown(); }; -void DocumentDBCollectionTest::SetUpTestCase(void) -{ -} +void DocumentDBCollectionTest::SetUpTestCase(void) {} -void DocumentDBCollectionTest::TearDownTestCase(void) -{ -} +void DocumentDBCollectionTest::TearDownTestCase(void) {} void DocumentDBCollectionTest::SetUp(void) { @@ -103,12 +99,7 @@ HWTEST_F(DocumentDBCollectionTest, CollectionTest002, TestSize.Level0) EXPECT_EQ(GRD_DropCollection(g_db, it, 0), GRD_INVALID_ARGS); } - std::vector invalidNameFormat = { - "GRD_123", - "grd_123", - "GM_SYS_123", - "gm_sys_123" - }; + std::vector invalidNameFormat = { "GRD_123", "grd_123", "GM_SYS_123", "gm_sys_123" }; for (auto *it : invalidNameFormat) { GLOGD("CollectionTest002: create collection with name: %s", it); @@ -127,15 +118,8 @@ HWTEST_F(DocumentDBCollectionTest, CollectionTest002, TestSize.Level0) HWTEST_F(DocumentDBCollectionTest, CollectionTest003, TestSize.Level0) { string overLenName(MAX_COLLECTION_LEN - 1, 'a'); - std::vector validName = { - "123", - "&^%@", - "中文字符", - "sqlite_master", - "NULL", - "SELECT", - overLenName.c_str() - }; + std::vector validName = { "123", "&^%@", "中文字符", "sqlite_master", "NULL", "SELECT", + overLenName.c_str() }; for (auto *it : validName) { GLOGD("CollectionTest003: create collection with name: %s", it); @@ -213,7 +197,7 @@ HWTEST_F(DocumentDBCollectionTest, CollectionTest006, TestSize.Level0) */ HWTEST_F(DocumentDBCollectionTest, CollectionTest007, TestSize.Level0) { - for (int flag : std::vector {2, 4, 8, 1024, UINT32_MAX}) { + for (int flag : std::vector{ 2, 4, 8, 1024, UINT32_MAX }) { EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", flag), GRD_INVALID_ARGS); EXPECT_EQ(GRD_DropCollection(g_db, "student", flag), GRD_INVALID_ARGS); } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp index 228978ba..2194bffb 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp @@ -17,10 +17,10 @@ #include "doc_errno.h" #include "documentdb_test_utils.h" -#include "log_print.h" #include "grd_base/grd_db_api.h" #include "grd_base/grd_error.h" #include "grd_document/grd_document_api.h" +#include "log_print.h" #include "sqlite_utils.h" using namespace DocumentDB; @@ -31,7 +31,7 @@ namespace { std::string g_path = "./document.db"; GRD_DB *g_db = nullptr; const char *g_coll = "student"; -} +} // namespace class DocumentDBDataTest : public testing::Test { public: @@ -41,13 +41,9 @@ public: void TearDown(); }; -void DocumentDBDataTest::SetUpTestCase(void) -{ -} +void DocumentDBDataTest::SetUpTestCase(void) {} -void DocumentDBDataTest::TearDownTestCase(void) -{ -} +void DocumentDBDataTest::TearDownTestCase(void) {} void DocumentDBDataTest::SetUp(void) { @@ -109,12 +105,12 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest003, TestSize.Level0) { std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; std::vector> invalidName = { - {nullptr, GRD_INVALID_ARGS}, - {"", GRD_INVALID_ARGS}, - {"GRD_123", GRD_INVALID_FORMAT}, - {"grd_123", GRD_INVALID_FORMAT}, - {"GM_SYS_123", GRD_INVALID_FORMAT}, - {"gm_sys_123", GRD_INVALID_FORMAT}, + { nullptr, GRD_INVALID_ARGS }, + { "", GRD_INVALID_ARGS }, + { "GRD_123", GRD_INVALID_FORMAT }, + { "grd_123", GRD_INVALID_FORMAT }, + { "GM_SYS_123", GRD_INVALID_FORMAT }, + { "gm_sys_123", GRD_INVALID_FORMAT }, }; for (auto it : invalidName) { GLOGD("UpsertDataTest003: upsert data with collectionname: %s", it.first); @@ -142,9 +138,9 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest005, TestSize.Level0) { std::string filter = R""({"_id":"1234"})""; std::vector> invalidDocument = { - {"", GRD_INVALID_ARGS}, - {nullptr, GRD_INVALID_ARGS}, - {R""({invalidJsonFormat})"", GRD_INVALID_FORMAT}, + { "", GRD_INVALID_ARGS }, + { nullptr, GRD_INVALID_ARGS }, + { R""({invalidJsonFormat})"", GRD_INVALID_FORMAT }, }; for (auto it : invalidDocument) { GLOGD("UpsertDataTest005: upsert data with document: %s", it.first); @@ -164,7 +160,7 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest006, TestSize.Level0) std::string filter = R""({"_id":"1234"})""; std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - for (auto flags : std::vector {2, 4, 8, 64, 1024, UINT32_MAX}) { + for (auto flags : std::vector{ 2, 4, 8, 64, 1024, UINT32_MAX }) { EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), flags), GRD_INVALID_ARGS); } } @@ -240,12 +236,12 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest003, TestSize.Level0) std::string filter = R""({"_id":"1234"})""; std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; std::vector> invalidName = { - {nullptr, GRD_INVALID_ARGS}, - {"", GRD_INVALID_ARGS}, - {"GRD_123", GRD_INVALID_FORMAT}, - {"grd_123", GRD_INVALID_FORMAT}, - {"GM_SYS_123", GRD_INVALID_FORMAT}, - {"gm_sys_123", GRD_INVALID_FORMAT}, + { nullptr, GRD_INVALID_ARGS }, + { "", GRD_INVALID_ARGS }, + { "GRD_123", GRD_INVALID_FORMAT }, + { "grd_123", GRD_INVALID_FORMAT }, + { "GM_SYS_123", GRD_INVALID_FORMAT }, + { "gm_sys_123", GRD_INVALID_FORMAT }, }; for (auto it : invalidName) { GLOGD("UpdateDataTest003: update data with collectionname: %s", it.first); @@ -288,9 +284,9 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest005, TestSize.Level0) std::string filter = R""({"_id":"1234"})""; std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; std::vector> invalidUpdate = { - {"", GRD_INVALID_ARGS}, - {nullptr, GRD_INVALID_ARGS}, - {R""({invalidJsonFormat})"", GRD_INVALID_FORMAT}, + { "", GRD_INVALID_ARGS }, + { nullptr, GRD_INVALID_ARGS }, + { R""({invalidJsonFormat})"", GRD_INVALID_FORMAT }, }; for (auto it : invalidUpdate) { @@ -310,7 +306,7 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest006, TestSize.Level0) { std::string filter = R""({"_id":"1234"})""; std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector invalidFlags = {1, 2, 4, 8, 1024, UINT32_MAX}; + std::vector invalidFlags = { 1, 2, 4, 8, 1024, UINT32_MAX }; for (auto flag : invalidFlags) { GLOGD("UpdateDataTest006: update data with flag: %u", flag); EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), document.c_str(), flag), GRD_INVALID_ARGS); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_delete_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_delete_test.cpp index 305a1f5a..a1deee42 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_delete_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_delete_test.cpp @@ -16,12 +16,12 @@ #include #include "grd_base/grd_db_api.h" -#include "grd_document/grd_document_api.h" #include "grd_base/grd_error.h" +#include "grd_base/grd_resultset_api.h" #include "grd_base/grd_type_export.h" -#include "grd_type_inner.h" +#include "grd_document/grd_document_api.h" #include "grd_resultset_inner.h" -#include "grd_base/grd_resultset_api.h" +#include "grd_type_inner.h" using namespace testing::ext; namespace { @@ -30,7 +30,7 @@ constexpr const char *NULL_JSON_STR = "{}"; const int MAX_COLLECTION_LENS = 511; std::string path = "./document.db"; GRD_DB *g_db = nullptr; -} +} // namespace class DocumentDeleteApiTest : public testing::Test { public: @@ -64,8 +64,7 @@ void DocumentDeleteApiTest::SetUp(void) * @tc.steps:step2. Insert many document in order to delete * @tc.expected: step2. GRD_OK */ - const char *document1 = - "{ \ + const char *document1 = "{ \ \"_id\" : \"1\", \ \"name\": \"xiaoming\", \ \"address\": \"beijing\", \ @@ -73,8 +72,7 @@ void DocumentDeleteApiTest::SetUp(void) \"friend\" : {\"name\" : \"David\", \"sex\" : \"female\", \"age\" : 90}, \ \"subject\": [\"math\", \"English\", \"music\"] \ }"; - const char *document2 = - "{ \ + const char *document2 = "{ \ \"_id\" : \"2\", \ \"name\": \"ori\", \ \"address\": \"beijing\", \ @@ -82,8 +80,7 @@ void DocumentDeleteApiTest::SetUp(void) \"friend\" : {\"name\" : \"David\", \"sex\" : \"female\", \"age\" : 90}, \ \"subject\": [\"math\", \"English\", \"music\"] \ }"; - const char *document3 = - "{ \ + const char *document3 = "{ \ \"_id\" : \"3\", \ \"name\": \"David\", \ \"address\": \"beijing\", \ @@ -248,7 +245,7 @@ HWTEST_F(DocumentDeleteApiTest, DeleteDBTest006, TestSize.Level1) */ const char *filter = "{\"_id\" : \"1\"}"; GRD_ResultSet *resultSet = nullptr; - Query query = {filter, "{}"}; + Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), 1); /** @@ -278,7 +275,6 @@ HWTEST_F(DocumentDeleteApiTest, DeleteDBTest007, TestSize.Level1) EXPECT_EQ(GRD_DeleteDoc(g_db, collectionName2.c_str(), filter, 0), GRD_OVER_LIMIT); } - /** * @tc.name: DocumentDelete008 * @tc.desc: Test delete with invalid NULL input for all parameters. @@ -314,13 +310,11 @@ HWTEST_F(DocumentDeleteApiTest, DeleteDBTest010, TestSize.Level1) * @tc.steps:step1. Test delete document when filter _id is int and string. * @tc.expected: step1. GRD_INVALID_ARGS */ - std::vector filterVec = {R"({"_id" : 1})", R"({"_id":[1, 2]})", - R"({"_id" : {"t1" : 1}})", R"({"_id":null})", R"({"_id":true})", R"({"_id" : 1.333})", R"({"_id" : -2.0})"}; + std::vector filterVec = { R"({"_id" : 1})", R"({"_id":[1, 2]})", R"({"_id" : {"t1" : 1}})", + R"({"_id":null})", R"({"_id":true})", R"({"_id" : 1.333})", R"({"_id" : -2.0})" }; for (const auto &item : filterVec) { EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, item.c_str(), 0), GRD_INVALID_ARGS); } const char *filter = "{\"_id\" : \"1\"}"; EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), 1); - } - diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp index 0051493b..6515db6a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp @@ -15,16 +15,16 @@ #include +#include "doc_errno.h" +#include "documentdb_test_utils.h" #include "grd_base/grd_db_api.h" -#include "grd_document/grd_document_api.h" #include "grd_base/grd_error.h" +#include "grd_base/grd_resultset_api.h" #include "grd_base/grd_type_export.h" +#include "grd_document/grd_document_api.h" +#include "grd_resultset_inner.h" #include "grd_type_inner.h" -#include "grd_base/grd_resultset_api.h" -#include "doc_errno.h" #include "log_print.h" -#include "documentdb_test_utils.h" -#include "grd_resultset_inner.h" using namespace testing::ext; namespace { @@ -61,7 +61,8 @@ static const char *g_document13 = "{\"_id\" : \"13\", \"name\":\"doc13\",\"item\ {\"school\":\"AB\", \"age\" : 15}}"; static const char *g_document14 = "{\"_id\" : \"14\", \"name\":\"doc14\",\"item\" : true,\"personInfo\":\ [{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 85}]}"; -static const char *g_document15 = "{\"_id\" : \"15\", \"name\":\"doc15\",\"personInfo\":[{\"school\":\"C\", \"age\" : 5}]}"; +static const char *g_document15 = "{\"_id\" : \"15\", \"name\":\"doc15\",\"personInfo\":[{\"school\":\"C\", \"age\" : " + "5}]}"; static const char *g_document16 = "{\"_id\" : \"16\", \"name\":\"doc16\", \"nested1\":{\"nested2\":{\"nested3\":\ {\"nested4\":\"ABC\", \"field2\":\"CCC\"}}}}"; static const char *g_document17 = "{\"_id\" : \"17\", \"name\":\"doc17\",\"personInfo\":\"oh,ok\"}"; @@ -71,10 +72,11 @@ static const char *g_document19 = "{\"_id\" : \"19\", \"name\":\"doc19\",\"ITEM\ {\"school\":\"AB\", \"age\":15}}"; static const char *g_document20 = "{\"_id\" : \"20\", \"name\":\"doc20\",\"ITEM\" : true,\"personInfo\":\ [{\"SCHOOL\":\"B\", \"AGE\":15}, {\"SCHOOL\":\"C\", \"AGE\":35}]}"; -static std::vectorg_data = {g_document1, g_document2, g_document3, g_document4, g_document5, g_document6, g_document7, - g_document8, g_document9, g_document10, g_document11, g_document12, g_document13, g_document14, g_document15, g_document16, g_document17, g_document18, g_document19, g_document20}; +static std::vector g_data = { g_document1, g_document2, g_document3, g_document4, g_document5, + g_document6, g_document7, g_document8, g_document9, g_document10, g_document11, g_document12, g_document13, + g_document14, g_document15, g_document16, g_document17, g_document18, g_document19, g_document20 }; -static void InsertData(GRD_DB *g_db, const char *collectionName) +static void InsertData(GRD_DB *g_db, const char *collectionName) { for (const auto &item : g_data) { EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, item, 0), GRD_OK); @@ -90,7 +92,7 @@ static void CompareValue(const char *value, const char *targetValue) EXPECT_EQ(errCode, E_OK); EXPECT_EQ(valueObj.Print(), targetValueObj.Print()); } -} +} // namespace class DocumentFindApiTest : public testing::Test { public: @@ -120,10 +122,7 @@ void DocumentFindApiTest::SetUp(void) InsertData(g_db, "student"); } -void DocumentFindApiTest::TearDown(void) -{ -} - +void DocumentFindApiTest::TearDown(void) {} /** * @tc.name: DocumentFindApiTest001 @@ -140,7 +139,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest001, TestSize.Level1) */ const char *filter = "{\"_id\" : \"6\"}"; GRD_ResultSet *resultSet = nullptr; - Query query = {filter, "{}"}; + Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = NULL; @@ -171,9 +170,9 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest002, TestSize.Level1) */ const char *filter = "{\"_id\" : \"6\", \"name\":\"doc6\"}"; GRD_ResultSet *resultSet = nullptr; - Query query = {filter, "{}"}; + Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); - /** + /** * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. */ @@ -199,9 +198,9 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest004, TestSize.Level1) */ const char *filter = "{\"name\":\"doc6\"}"; GRD_ResultSet *resultSet = nullptr; - Query query = {filter, "{}"}; + Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); - + /** * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. @@ -228,7 +227,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest005, TestSize.Level1) */ GRD_ResultSet *resultSet1 = nullptr; const char *filter1 = "{\"_id\" : \"1\", \"info\" : 1}"; - Query query1 = {filter1, "{}"}; + Query query1 = { filter1, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query1, 1, &resultSet1), GRD_INVALID_ARGS); /** @@ -237,7 +236,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest005, TestSize.Level1) */ GRD_ResultSet *resultSet2 = nullptr; const char *filter2 = "{\"_id\" : \"1\", \"_id\" : \"2\"}"; - Query query2 = {filter2, "{}"}; + Query query2 = { filter2, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query2, 1, &resultSet2), GRD_INVALID_ARGS); /** @@ -246,7 +245,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest005, TestSize.Level1) */ GRD_ResultSet *resultSet3 = nullptr; const char *filter3 = "{\"_id\" : \"1\", \"info\" : [\"2\", 1]}"; - Query query3 = {filter3, "{}"}; + Query query3 = { filter3, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query3, 1, &resultSet3), GRD_INVALID_ARGS); /** @@ -255,7 +254,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest005, TestSize.Level1) */ GRD_ResultSet *resultSet4 = nullptr; const char *filter4 = "{\"_id\" : \"1\", \"info\" : {\"info_val\" : \"1\"}}"; - Query query4 = {filter4, "{}"}; + Query query4 = { filter4, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query4, 1, &resultSet4), GRD_INVALID_ARGS); /** @@ -264,7 +263,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest005, TestSize.Level1) */ GRD_ResultSet *resultSet5 = nullptr; const char *filter5 = "{\"_id\" : \"1\", \"info\" : true}"; - Query query5 = {filter5, "{}"}; + Query query5 = { filter5, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query5, 1, &resultSet5), GRD_INVALID_ARGS); /** @@ -273,7 +272,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest005, TestSize.Level1) */ GRD_ResultSet *resultSet6 = nullptr; const char *filter6 = "{\"_id\" : \"1\", \"info\" : null}"; - Query query6 = {filter6, "{}"}; + Query query6 = { filter6, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query6, 1, &resultSet6), GRD_INVALID_ARGS); } @@ -292,7 +291,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest006, TestSize.Level1) */ GRD_ResultSet *resultSet1 = nullptr; const char *filter1 = "{\"_id\" : \"valstring\"}"; - Query query1 = {filter1, "{}"}; + Query query1 = { filter1, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query1, 1, &resultSet1), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet1), GRD_OK); @@ -302,7 +301,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest006, TestSize.Level1) */ GRD_ResultSet *resultSet2 = nullptr; const char *filter2 = "{\"_id\" : 1}"; - Query query2 = {filter2, "{}"}; + Query query2 = { filter2, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query2, 1, &resultSet2), GRD_INVALID_ARGS); /** @@ -311,7 +310,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest006, TestSize.Level1) */ GRD_ResultSet *resultSet3 = nullptr; const char *filter3 = "{\"_id\" : [\"2\", 1]}"; - Query query3 = {filter3, "{}"}; + Query query3 = { filter3, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query3, 1, &resultSet3), GRD_INVALID_ARGS); /** @@ -320,7 +319,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest006, TestSize.Level1) */ GRD_ResultSet *resultSet4 = nullptr; const char *filter4 = "{\"_id\" : {\"info_val\" : \"1\"}}"; - Query query4 = {filter4, "{}"}; + Query query4 = { filter4, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query4, 1, &resultSet4), GRD_INVALID_ARGS); /** @@ -329,7 +328,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest006, TestSize.Level1) */ GRD_ResultSet *resultSet5 = nullptr; const char *filter5 = "{\"_id\" : true}"; - Query query5 = {filter5, "{}"}; + Query query5 = { filter5, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query5, 1, &resultSet5), GRD_INVALID_ARGS); /** @@ -338,7 +337,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest006, TestSize.Level1) */ GRD_ResultSet *resultSet6 = nullptr; const char *filter6 = "{\"_id\" : null}"; - Query query6 = {filter6, "{}"}; + Query query6 = { filter6, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query6, 1, &resultSet6), GRD_INVALID_ARGS); } @@ -355,7 +354,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest016, TestSize.Level1) const char *colName2 = "GM_SYS_sysfff"; GRD_ResultSet *resultSet = NULL; const char *filter = "{\"_id\" : \"1\"}"; - Query query = {filter, "{}"}; + Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, colName1, query, 1, &resultSet), GRD_INVALID_FORMAT); EXPECT_EQ(GRD_FindDoc(g_db, colName2, query, 1, &resultSet), GRD_INVALID_FORMAT); } @@ -399,7 +398,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest019, TestSize.Level1) { const char *filter = "{\"_id\" : \"100\"}"; GRD_ResultSet *resultSet = nullptr; - Query query = {filter, "{}"}; + Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); char *value = NULL; @@ -423,7 +422,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest023, TestSize.Level1) const char *filter = "{\"_id\" : \"6\"}"; GRD_ResultSet *resultSet = nullptr; GRD_ResultSet *resultSet2 = nullptr; - Query query = {filter, "{}"}; + Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet2), GRD_RESOURCE_BUSY); @@ -460,8 +459,8 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest024, TestSize.Level1) const char *filter = "{\"_id\" : \"6\"}"; GRD_ResultSet *resultSet = nullptr; GRD_ResultSet *resultSet2 = nullptr; - Query query = {filter, "{}"}; - const char* collectionName = "DocumentFindApiTest024"; + Query query = { filter, "{}" }; + const char *collectionName = "DocumentFindApiTest024"; EXPECT_EQ(GRD_CreateCollection(g_db, collectionName, "", 0), GRD_OK); InsertData(g_db, collectionName); EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); @@ -508,7 +507,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest025, TestSize.Level1) const char *projectionInfo = "{\"name\": true, \"nested1.nested2.nested3.nested4\":true}"; const char *targetDocument = "{\"name\":\"doc16\", \"nested1\":{\"nested2\":{\"nested3\":\ {\"nested4\":\"ABC\"}}}}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; @@ -528,7 +527,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest025, TestSize.Level1) * @tc.expected: step3. succeed to get the record. */ projectionInfo = "{\"name\": true, \"nested1\":{\"nested2\":{\"nested3\":{\"nested4\":true}}}}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); @@ -547,7 +546,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest025, TestSize.Level1) */ projectionInfo = "{\"name\": 0, \"nested1.nested2.nested3.nested4\":0}"; targetDocument = "{\"nested1\":{\"nested2\":{\"nested3\":{\"field2\":\"CCC\"}}}}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); @@ -573,7 +572,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest026, TestSize.Level1) const char *filter = "{\"_id\" : \"16\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"name\": true, \"nested1.nested2.nested3.nested4.nested5\":true}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); // EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); // char *value = nullptr; @@ -605,7 +604,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest027, TestSize.Level1) const char *projectionInfo = "{\"name\": true, \"other_Info\":true, \"non_exist_field\":true}"; const char *targetDocument = "{\"name\": \"doc7\", \"other_Info\":[{\"school\":\"BX\", \"age\":15},\ {\"school\":\"C\", \"age\":35}]}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; @@ -613,14 +612,14 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest027, TestSize.Level1) CompareValue(value, targetDocument); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - + /** * @tc.steps: step2. Create filter to match g_document7, _id flag is 0. * Create projection to display name, other _info and existing field with space. * @tc.expected: step2. Return GRD_INVALID_ARGS. */ projectionInfo = "{\"name\": true, \"other_Info\":true, \" item \":true}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); /** @@ -629,7 +628,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest027, TestSize.Level1) * @tc.expected: step3. Match the g_document7 and display name, other_Info. */ projectionInfo = "{\"name\": true, \"other_Info\":true, \"ITEM\": true}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; resultSet = nullptr; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); @@ -657,7 +656,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest028, TestSize.Level1) GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"name\": true, \"other_Info.non_exist_field\":true}"; const char *targetDocument = "{\"name\": \"doc7\"}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; @@ -665,14 +664,14 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest028, TestSize.Level1) CompareValue(value, targetDocument); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - + /** * @tc.steps: step2. Create filter to match g_document7, _id flag is 0. * Create projection to display name, other _info and existing field with space. * @tc.expected: step2. Return GRD_INVALID_ARGS. */ projectionInfo = "{\"name\": true, \"other_Info\":{\"non_exist_field\":true}}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; resultSet = nullptr; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); @@ -687,7 +686,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest028, TestSize.Level1) * @tc.expected: step3. Match the g_document7 and display name, other_Info. */ projectionInfo = "{\"name\": true, \"other_Info.0\": true}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; resultSet = nullptr; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); @@ -710,7 +709,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest029, TestSize.Level1) const char *filter = "{\"_id\" : \"4\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"personInfo\": true, \"personInfo.grade\": true}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); } @@ -732,7 +731,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest030, TestSize.Level1) const char *projectionInfo = "{\"non_exist_field\":true}"; int flag = 0; const char *targetDocument = "{}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; @@ -740,7 +739,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest030, TestSize.Level1) CompareValue(value, targetDocument); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - + /** * @tc.steps: step2. Create filter to match g_document7, _id flag is 1. * @tc.expected: step2. Match g_document7, and return a json with _id. @@ -748,7 +747,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest030, TestSize.Level1) resultSet = nullptr; flag = 1; targetDocument = "{\"_id\": \"7\"}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); value = NULL; @@ -776,7 +775,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest031, TestSize.Level1) const char *projectionInfo = "{\"name\":true, \"item\":true}"; int flag = 0; const char *targetDocument = "{\"name\":\"doc7\", \"item\":\"fruit\"}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; @@ -784,7 +783,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest031, TestSize.Level1) CompareValue(value, targetDocument); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - + /** * @tc.steps: step2. Create filter to match g_document7, _id flag is 1. * @tc.expected: step2. Match g_document7, and return a json with _id. @@ -793,7 +792,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest031, TestSize.Level1) flag = 1; projectionInfo = "{\"name\": 1, \"item\": 1}"; targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); value = NULL; @@ -821,7 +820,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest032, TestSize.Level1) const char *projectionInfo = "{\"name\":true, \"item\":true}"; int flag = 0; const char *targetDocument = "{\"name\":\"doc7\", \"item\":\"fruit\"}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; @@ -837,7 +836,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest032, TestSize.Level1) flag = 1; projectionInfo = "{\"name\": 1, \"item\": 1}"; targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); value = NULL; @@ -853,7 +852,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest032, TestSize.Level1) flag = 1; projectionInfo = "{\"name\": 10, \"item\": 10}"; targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); value = NULL; @@ -880,8 +879,10 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest033, TestSize.Level1) GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"name\":false, \"item\":false}"; int flag = 0; - const char *targetDocument = "{\"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}";; - Query query = {filter, projectionInfo}; + const char *targetDocument = "{\"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : " + "35}]}"; + ; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; @@ -896,8 +897,10 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest033, TestSize.Level1) resultSet = nullptr; flag = 1; projectionInfo = "{\"name\": 0, \"item\": 0}"; - targetDocument = "{\"_id\": \"7\", \"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}";; - query = {filter, projectionInfo}; + targetDocument = "{\"_id\": \"7\", \"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" " + ": 35}]}"; + ; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); value = NULL; @@ -926,7 +929,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest034, TestSize.Level1) \"personInfo.shool1\": 1, \"personInfo.age1\": 1}"; int flag = 0; const char *targetDocument = "{\"name\":\"doc4\"}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; @@ -941,8 +944,9 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest034, TestSize.Level1) */ projectionInfo = "{\"name\": false, \"personInfo.grade1\": false, \ \"personInfo.shool1\": false, \"personInfo.age1\": false}"; - const char *targetDocument2 = "{\"item\":\"paper\",\"personInfo\":{\"grade\" : 1, \"school\":\"A\", \"age\" : 18}}"; - query = {filter, projectionInfo}; + const char *targetDocument2 = "{\"item\":\"paper\",\"personInfo\":{\"grade\" : 1, \"school\":\"A\", \"age\" : " + "18}}"; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); @@ -956,7 +960,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest034, TestSize.Level1) */ projectionInfo = "{\"name\": 1, \"personInfo.school\": 1, \"personInfo.age\": 1}"; const char *targetDocument3 = "{\"name\":\"doc4\", \"personInfo\": {\"school\":\"A\", \"age\" : 18}}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); @@ -970,7 +974,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest034, TestSize.Level1) */ projectionInfo = "{\"name\": 1, \"personInfo.school\": 1, \"personInfo.age1\": 1}"; const char *targetDocument4 = "{\"name\":\"doc4\", \"personInfo\": {\"school\":\"A\"}}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); @@ -995,7 +999,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest035, TestSize.Level1) const char *filter = "{\"_id\" : \"17\"}"; GRD_ResultSet *resultSet = nullptr; int flag = 0; - Query query = {filter, "{}"}; + Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, GRD_DOC_ID_DISPLAY, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; @@ -1027,7 +1031,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest036, TestSize.Level1) const char *filter = "{\"_id\" : \"17\"}"; GRD_ResultSet *resultSet = nullptr; int flag = 0; - Query query = {filter, "{}"}; + Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, "", query, 0, &resultSet), GRD_INVALID_ARGS); EXPECT_EQ(GRD_FindDoc(g_db, NULL, query, 0, &resultSet), GRD_INVALID_ARGS); } @@ -1048,7 +1052,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest037, TestSize.Level1) const char *filter = "{\"_id\" : \"4\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"name\":1, \"personInfo\":0, \"item\":1}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); /** @@ -1056,7 +1060,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest037, TestSize.Level1) * @tc.expected: step2. Return GRD_INVALID_ARGS. */ projectionInfo = "{\"name\":2, \"personInfo\":0, \"item\":2}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); /** @@ -1064,7 +1068,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest037, TestSize.Level1) * @tc.expected: step3. Return GRD_INVALID_ARGS. */ projectionInfo = "{\"name\":true, \"personInfo\":0, \"item\":true}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); /** @@ -1072,7 +1076,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest037, TestSize.Level1) * @tc.expected: step4. Return GRD_INVALID_ARGS. */ projectionInfo = "{\"name\":\"\", \"personInfo\":0, \"item\":\"\"}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); /** @@ -1080,7 +1084,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest037, TestSize.Level1) * @tc.expected: step5. Return GRD_INVALID_ARGS. */ projectionInfo = "{\"name\":false, \"personInfo\":1, \"item\":false"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT); /** @@ -1088,7 +1092,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest037, TestSize.Level1) * @tc.expected: step6. Return GRD_INVALID_ARGS. */ projectionInfo = "{\"name\":false, \"personInfo\":-1.123, \"item\":false"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT); /** @@ -1096,7 +1100,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest037, TestSize.Level1) * @tc.expected: step7. Return GRD_INVALID_ARGS. */ projectionInfo = "{\"name\":false, \"personInfo\":true, \"item\":false"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT); } @@ -1118,7 +1122,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest038, TestSize.Level1) const char *projectionInfo = "{\"name\":false, \"personInfo\": 0, \"item\":0}"; int flag = 0; const char *targetDocument = "{}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; @@ -1132,7 +1136,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest038, TestSize.Level1) * @tc.expected: step2. Match g_document6, Return json with _id. */ targetDocument = "{\"_id\": \"6\"}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); @@ -1156,11 +1160,12 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest039, TestSize.Level1) */ const char *filter = "{\"_id\" : \"18\"}"; GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\":true, \"personInfo.age\": \"\", \"item\":1, \"color\":10, \"nonExist\" : -100}"; + const char *projectionInfo = "{\"name\":true, \"personInfo.age\": \"\", \"item\":1, \"color\":10, \"nonExist\" : " + "-100}"; const char *targetDocument = "{\"name\":\"doc18\", \"item\":\"mobile phone\", \"personInfo\":\ {\"age\":66}, \"color\":\"blue\"}"; int flag = 0; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; @@ -1175,7 +1180,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest039, TestSize.Level1) */ targetDocument = "{\"_id\" : \"18\", \"name\":\"doc18\",\"item\" : \"mobile phone\",\"personInfo\":\ {\"age\":66}, \"color\":\"blue\"}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); @@ -1201,7 +1206,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest040, TestSize.Level1) GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"personInfo\":[true, 1]}"; int flag = 1; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); /** @@ -1209,7 +1214,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest040, TestSize.Level1) * @tc.expected: step2. Match the g_document18 and return GRD_INVALID_ARGS. */ projectionInfo = "{\"personInfo\":null}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); /** @@ -1217,7 +1222,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest040, TestSize.Level1) * @tc.expected: step3. Match the g_document18 and return GRD_INVALID_ARGS. */ projectionInfo = "{\"personInfo\":\"invalid string.\"}"; - query = {filter, projectionInfo}; + query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); } @@ -1236,9 +1241,10 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest042, TestSize.Level1) */ const char *filter = "{\"_iD\" : \"18\"}"; GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"Name\":true, \"personInfo.age\": \"\", \"item\":1, \"COLOR\":10, \"nonExist\" : -100}"; + const char *projectionInfo = "{\"Name\":true, \"personInfo.age\": \"\", \"item\":1, \"COLOR\":10, \"nonExist\" : " + "-100}"; int flag = 0; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); @@ -1247,10 +1253,10 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest042, TestSize.Level1) * @tc.steps: step2. Test field with upper projection. * @tc.expected: step2. Match g_document18, Return json with item, personInfo.age, color and _id. */ - const char *filter1 = "{\"_id\" : \"18\"}"; - const char* targetDocument = "{\"_id\" : \"18\", \"item\" : \"mobile phone\",\"personInfo\":\ + const char *filter1 = "{\"_id\" : \"18\"}"; + const char *targetDocument = "{\"_id\" : \"18\", \"item\" : \"mobile phone\",\"personInfo\":\ {\"age\":66}}"; - query = {filter1, projectionInfo}; + query = { filter1, projectionInfo }; char *value = nullptr; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); @@ -1281,7 +1287,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest044, TestSize.Level1) const char *targetDocument = "{\"_id\" : \"18\", \"name\":\"doc18\", \"personInfo\":\ {\"school\":\"DD\"}, \"color\":\"blue\"}"; int flag = 0; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; char *value = nullptr; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); @@ -1308,7 +1314,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest045, TestSize.Level1) const char *filter = "{\"_id\" : \"18\"}"; GRD_ResultSet *resultSet = nullptr; int flag = 0; - Query query = {filter, "{}"}; + Query query = { filter, "{}" }; string collectionName1(MAX_COLLECTION_NAME, 'a'); ASSERT_EQ(GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0), GRD_OK); EXPECT_EQ(GRD_FindDoc(g_db, collectionName1.c_str(), query, 1, &resultSet), GRD_OK); @@ -1336,7 +1342,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest052, TestSize.Level1) const char *filter = "{\"_id\" : \"18\"}"; GRD_ResultSet *resultSet = nullptr; int flag = 0; - Query query = {filter, "{}"}; + Query query = { filter, "{}" }; string collectionName1(MAX_COLLECTION_NAME, 'a'); ASSERT_EQ(GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0), GRD_OK); EXPECT_EQ(GRD_FindDoc(g_db, collectionName1.c_str(), query, 1, &resultSet), GRD_OK); @@ -1364,7 +1370,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest053, TestSize.Level1) const char *filter = "{\"_id\" : \"18\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 3, &resultSet), GRD_INVALID_ARGS); /** @@ -1396,7 +1402,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest054, TestSize.Level1) const char *filter = "{\"_id\" : \"18\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{}"; - Query query = {filter, projectionInfo}; + Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(nullptr, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); /** @@ -1409,7 +1415,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest054, TestSize.Level1) * @tc.steps: step1. Test with query that has two nullptr data. * @tc.expected: step1. Return GRD_INVALID_ARGS. */ - query = {nullptr, nullptr}; + query = { nullptr, nullptr }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); } @@ -1426,7 +1432,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest055, TestSize.Level1) * @tc.steps:step1.Find doc, but filter' _id value lens is larger than MAX_ID_LENS * @tc.expected:step1.GRD_OVER_LIMIT. */ - string document1 = "{\"_id\" : "; + string document1 = "{\"_id\" : "; string document2 = "\""; string document4 = "\""; string document5 = "}"; @@ -1434,7 +1440,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest055, TestSize.Level1) string filter = document1 + document2 + document_midlle + document4 + document5; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{}"; - Query query = {filter.c_str(), projectionInfo}; + Query query = { filter.c_str(), projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OVER_LIMIT); /** @@ -1443,7 +1449,7 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest055, TestSize.Level1) */ string document_midlle2(MAX_ID_LENS, 'k'); filter = document1 + document2 + document_midlle2 + document4 + document5; - query = {filter.c_str(), projectionInfo}; + query = { filter.c_str(), projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp index 5f75c674..3663f29b 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp @@ -16,8 +16,8 @@ #include #include "grd_base/grd_db_api.h" -#include "grd_document/grd_document_api.h" #include "grd_base/grd_error.h" +#include "grd_document/grd_document_api.h" using namespace testing::ext; namespace { std::string path = "./document.db"; @@ -31,33 +31,33 @@ const int MAX_ID_LENS = 899; static void TestInsertDocIntoCertainColl(const char *collectionName, const char *projection, int expectedResult) { - /** * @tc.steps: step1. Create Collection + /** * @tc.steps: step1. Create Collection * @tc.expected: step1. GRD_OK */ - EXPECT_EQ(GRD_CreateCollection(g_db, collectionName, "", 0), expectedResult); - /** + EXPECT_EQ(GRD_CreateCollection(g_db, collectionName, "", 0), expectedResult); + /** * @tc.steps: step2. Insert projection into colloction. * @tc.expected: step2. GRD_OK */ - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, projection, 0), expectedResult); - /** + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, projection, 0), expectedResult); + /** * @tc.steps: step3. Call GRD_DroCollection to drop the collection. * @tc.expected: step3. GRD_OK */ - EXPECT_EQ(GRD_DropCollection(g_db, collectionName, 0), expectedResult); + EXPECT_EQ(GRD_DropCollection(g_db, collectionName, 0), expectedResult); } -const char* SetRandomDocument(int i) +const char *SetRandomDocument(int i) { - string document1 = "{\"_id\" : "; + string document1 = "{\"_id\" : "; string document2 = "\""; - string document3 = {'2','6'}; + string document3 = { '2', '6' }; string document4 = "\""; string document5 = ", \"name\" : \"Ori\"}"; string document = document1 + document2 + document3 + document4 + document5; return document.c_str(); } -} +} // namespace class DocumentInsertApiTest : public testing::Test { public: @@ -81,14 +81,9 @@ void DocumentInsertApiTest::TearDownTestCase(void) remove(path.c_str()); } -void DocumentInsertApiTest::SetUp(void) -{ -} - -void DocumentInsertApiTest::TearDown(void) -{ -} +void DocumentInsertApiTest::SetUp(void) {} +void DocumentInsertApiTest::TearDown(void) {} /** * @tc.name: DocumentInsertApiTest001 @@ -103,7 +98,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest001, TestSize.Level1) * @tc.steps:step1.Insert document into collection which dose not exist * @tc.expected:step1.GRD_INVALID_ARGS */ - const char *document1 = "{\"_id\" : \"1\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"1\", \"name\" : \"Ori\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, NO_EXIST_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); } @@ -120,35 +115,34 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest002, TestSize.Level1) * @tc.steps:step1.Insert a document whose _id is integer * @tc.expected:step1.GRD_INVALID_ARGS */ - const char *document1 = "{\"_id\" : 2, \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : 2, \"name\" : \"Ori\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); /** * @tc.steps:step2.Insert a document whose _id is bool * @tc.expected:step2.GRD_INVALID_ARGS */ - const char *document2 = "{\"_id\" : true, \"name\" : \"Chuan\"}"; + const char *document2 = "{\"_id\" : true, \"name\" : \"Chuan\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); /** * @tc.steps:step2.Insert a document whose _id is NULL * @tc.expected:step2.GRD_INVALID_ARGS */ - const char *document3 = "{\"_id\" : null, \"name\" : \"Chuan\"}"; + const char *document3 = "{\"_id\" : null, \"name\" : \"Chuan\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_INVALID_ARGS); /** * @tc.steps:step2.Insert a document whose _id is ARRAY * @tc.expected:step2.GRD_INVALID_ARGS */ - const char *document4 = "{\"_id\" : [\"2\"], \"name\" : \"Chuan\"}"; + const char *document4 = "{\"_id\" : [\"2\"], \"name\" : \"Chuan\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document4, 0), GRD_INVALID_ARGS); /** * @tc.steps:step2.Insert a document whose _id is OBJECT * @tc.expected:step2.GRD_INVALID_ARGS */ - const char *document5 = "{\"_id\" : {\"val\" : \"2\"}, \"name\" : \"Chuan\"}"; + const char *document5 = "{\"_id\" : {\"val\" : \"2\"}, \"name\" : \"Chuan\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document5, 0), GRD_INVALID_ARGS); } - /** * @tc.name: DocumentInsertApiTest003 * @tc.desc: Insert a document whose _id has appeared before @@ -162,7 +156,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest003, TestSize.Level1) * @tc.steps:step1.Insert a document whose _id is string * @tc.expected:step1.GRD_OK */ - const char *document1 = "{\"_id\" : \"3\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"3\", \"name\" : \"Ori\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); /** @@ -186,7 +180,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest004, TestSize.Level1) * @tc.steps:step1.step1.parameter db is NULL * @tc.expected:step1.GRD_INVALID_ARGS */ - const char *document1 = "{\"_id\" : \"4\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"4\", \"name\" : \"Ori\"}"; EXPECT_EQ(GRD_InsertDoc(NULL, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); } @@ -203,13 +197,13 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest005, TestSize.Level1) * @tc.steps:step1.Parameter collectionName is NULL * @tc.expected:step1.GRD_INVALID_ARGS */ - const char *document1 = "{\"_id\" : \"5\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"5\", \"name\" : \"Ori\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, NULL, document1, 0), GRD_INVALID_ARGS); /** * @tc.steps:step2.Parameter collectionName is empty string * @tc.expected:step2.GRD_INVALID_ARGS */ - const char *document2 = "{\"_id\" : \"5\", \"name\" : \"Chuang\"}"; + const char *document2 = "{\"_id\" : \"5\", \"name\" : \"Chuang\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, "", document2, 0), GRD_INVALID_ARGS); } @@ -226,7 +220,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest006, TestSize.Level1) * @tc.steps:step1.parameter flags is not zero * @tc.expected:step1.GRD_INVALID_ARGS */ - const char *document1 = "{\"_id\" : \"6\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"6\", \"name\" : \"Ori\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 1), GRD_INVALID_ARGS); } @@ -243,7 +237,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest007, TestSize.Level1) * @tc.steps:step1.parameter flags is int_max * @tc.expected:step1.GRD_INVALID_ARGS */ - const char *document1 = "{\"_id\" : \"7\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"7\", \"name\" : \"Ori\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, INT_MAX), GRD_INVALID_ARGS); } @@ -260,7 +254,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest008, TestSize.Level1) * @tc.steps:step1.parameter flags is int_min * @tc.expected:step1.GRD_INVALID_ARGS */ - const char *document1 = "{\"_id\" : \"8\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"8\", \"name\" : \"Ori\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, INT_MIN), GRD_INVALID_ARGS); } @@ -282,9 +276,9 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest009, TestSize.Level1) * @tc.steps:step2.parameter collectionName is larger than max_collectionName_lens; * @tc.expected:step2.GRD_OVER_LIMIT */ - const char *document1 = "{\"_id\" : \"9\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"9\", \"name\" : \"Ori\"}"; std::string collectionName2(MAX_COLLECTION_LENS + 1, 'a'); - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName2.c_str(), document1, 0), GRD_OVER_LIMIT); + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName2.c_str(), document1, 0), GRD_OVER_LIMIT); } /** @@ -301,7 +295,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest010, TestSize.Level1) * @tc.expected:step1.GRD_OK */ const char *collectionName = "collction@!#"; - const char *document1 = "{\"_id\" : \"10\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"10\", \"name\" : \"Ori\"}"; TestInsertDocIntoCertainColl(collectionName, document1, GRD_OK); } @@ -319,7 +313,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest011, TestSize.Level1) * @tc.expected:step1.GRD_OK */ string collectionName(257, 'k'); - const char *document1 = "{\"_id\" : \"10\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"10\", \"name\" : \"Ori\"}"; TestInsertDocIntoCertainColl(collectionName.c_str(), document1, GRD_OK); } @@ -336,7 +330,8 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest014, TestSize.Level1) * @tc.steps:step1.document's JSON depth is larger than 4, which is 5. * @tc.expected:step1.GRD_INVALID_ARGS */ - const char *document1 = "{\"level1\" : {\"level2\" : {\"level3\" : {\"level4\": {\"level5\" : 1}}, \"level3_2\" : \"level3_2_val\"\ + const char *document1 = + "{\"level1\" : {\"level2\" : {\"level3\" : {\"level4\": {\"level5\" : 1}}, \"level3_2\" : \"level3_2_val\"\ }},\"_id\":\"14\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); /** @@ -351,7 +346,8 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest014, TestSize.Level1) * @tc.steps:step1.document's JSON depth is 4 * @tc.expected:step1.GRD_OK */ - const char *document3 = "{\"level1\" : {\"level2\" : {\"level3\" : { \"level4\" : \"level5_1val\"}, \"level3_2\" : \"level3_2_val\"\ + const char *document3 = + "{\"level1\" : {\"level2\" : {\"level3\" : { \"level4\" : \"level5_1val\"}, \"level3_2\" : \"level3_2_val\"\ }},\"_id\":\"14\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_OK); } @@ -396,7 +392,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest016, TestSize.Level1) * @tc.steps:step1.document JSON string contains irregular char. * @tc.expected:step1.GRD_OK */ - const char *document1 = "{\"_id\" : \"16\", \"name\" : \"!@#Ori\"}"; + const char *document1 = "{\"_id\" : \"16\", \"name\" : \"!@#Ori\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); } @@ -448,7 +444,8 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest019, TestSize.Level1) * @tc.steps:step1.Insert a normal documents which _id is in the end of the string * @tc.expected:step1.GRD_OK. */ - const char *document1 = "{\"name\" : \"Jack\", \"age\" : 18, \"friend\" : {\"name\" : \" lucy\"}, \"_id\" : \"19\"}"; + const char *document1 = "{\"name\" : \"Jack\", \"age\" : 18, \"friend\" : {\"name\" : \" lucy\"}, \"_id\" : " + "\"19\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); } @@ -467,7 +464,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest022, TestSize.Level1) */ string collectionName = string(256, 'k'); string collectionName1(MAX_COLLECTION_LENS, 'a'); - const char *document1 = "{\"_id\" : \"22\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"22\", \"name\" : \"Ori\"}"; TestInsertDocIntoCertainColl(collectionName.c_str(), document1, GRD_OK); } @@ -487,7 +484,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest023, TestSize.Level1) * @tc.expected:step1.GRD_OK. */ string collectionName = "Aads_sd__23Asb_"; - const char *document1 = "{\"_id\" : \"23\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"23\", \"name\" : \"Ori\"}"; TestInsertDocIntoCertainColl(collectionName.c_str(), document1, GRD_OK); } @@ -506,7 +503,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest024, TestSize.Level1) * @tc.expected:step1.GRD_INVALID_FORMAT. */ string collectionName = "GRD_collectionName"; - const char *document1 = "{\"_id\" : \"24\", \"name\" : \"Ori\"}"; + const char *document1 = "{\"_id\" : \"24\", \"name\" : \"Ori\"}"; GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document1, 0), GRD_INVALID_FORMAT); @@ -515,7 +512,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest024, TestSize.Level1) * @tc.expected:step2.GRD_INVALID_FORMAT. */ collectionName = "GM_SYS__collectionName"; - const char *document2 = "{\"_id\" : \"24_2\", \"name\" : \"Ori\"}"; + const char *document2 = "{\"_id\" : \"24_2\", \"name\" : \"Ori\"}"; GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document2, 0), GRD_INVALID_FORMAT); @@ -524,7 +521,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest024, TestSize.Level1) * @tc.expected:step3.GRD_INVALID_FORMAT. */ collectionName = "grd_collectionName"; - const char *document3 = "{\"_id\" : \"24_3\", \"name\" : \"Ori\"}"; + const char *document3 = "{\"_id\" : \"24_3\", \"name\" : \"Ori\"}"; GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document3, 0), GRD_INVALID_FORMAT); @@ -533,7 +530,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest024, TestSize.Level1) * @tc.expected:step4.GRD_INVALID_FORMAT. */ collectionName = "gm_sys_collectionName"; - const char *document4 = "{\"_id\" : \"24_4\", \"name\" : \"Ori\"}"; + const char *document4 = "{\"_id\" : \"24_4\", \"name\" : \"Ori\"}"; GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document4, 0), GRD_INVALID_FORMAT); @@ -542,7 +539,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest024, TestSize.Level1) * @tc.expected:step5.GRD_INVALID_FORMAT. */ collectionName = "gM_sYs_collectionName"; - const char *document5 = "{\"_id\" : \"24_5\", \"name\" : \"Ori\"}"; + const char *document5 = "{\"_id\" : \"24_5\", \"name\" : \"Ori\"}"; GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document5, 0), GRD_INVALID_FORMAT); @@ -551,7 +548,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest024, TestSize.Level1) * @tc.expected:step6.GRD_INVALID_FORMAT. */ collectionName = "gRd_collectionName"; - const char *document6 = "{\"_id\" : \"24_6\", \"name\" : \"Ori\"}"; + const char *document6 = "{\"_id\" : \"24_6\", \"name\" : \"Ori\"}"; GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document6, 0), GRD_INVALID_FORMAT); @@ -560,7 +557,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest024, TestSize.Level1) * @tc.expected:step7.GRD_INVALID_FORMAT. */ collectionName = "gRd@collectionName"; - const char *document7 = "{\"_id\" : \"24_7\", \"name\" : \"Ori\"}"; + const char *document7 = "{\"_id\" : \"24_7\", \"name\" : \"Ori\"}"; GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document7, 0), GRD_OK); } @@ -585,21 +582,24 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest025, TestSize.Level1) * @tc.steps:step2.documents JSON depth is exactly 4. * @tc.expected:step2.GRD_OK. */ - const char *document2 = "{\"_id\" : \"25_1\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \"student_info\" : [{\"name\":\"张三\", \ + const char *document2 = + "{\"_id\" : \"25_1\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \"student_info\" : [{\"name\":\"张三\", \ \"age\" : 18, \"sex\" : \"男\"}, { \"newName1\" : [\"qw\", \"dr\", 0, \"ab\"] }]}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_OK); /** * @tc.steps:step3.documents JSON depth is exactly 4, but the last field in array contains leading number * @tc.expected:step3.GRD_INVALID_ARGS. */ - const char *document3 = "{\"_id\" : \"25_2\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \"student_info\" : [{\"name\":\"张三\", \ + const char *document3 = + "{\"_id\" : \"25_2\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \"student_info\" : [{\"name\":\"张三\", \ \"age\" : 18, \"sex\" : \"男\"}, [\"qw\", \"dr\", 0, \"ab\", {\"0ab\" : null}]]}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_INVALID_ARGS); /** * @tc.steps:step4.documents JSON depth is exactly 5. * @tc.expected:step4.GRD_INVALID_ARGS. */ - const char *document4 = "{\"_id\" : \"25_3\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \"student_info\" : [{\"name\":\"张三\", \ + const char *document4 = + "{\"_id\" : \"25_3\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \"student_info\" : [{\"name\":\"张三\", \ \"age\" : 18, \"sex\" : \"男\"}, { \"newName1\" : [\"qw\", \"dr\", 0, \"ab\", {\"level5\" : 1}] }]}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document4, 0), GRD_INVALID_ARGS); } @@ -617,14 +617,14 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest, TestSize.Level1) * @tc.steps:step1.Insert 100 normal documents continuously * @tc.expected:step1.GRD_OK. */ - string document1 = "{\"_id\" : "; + string document1 = "{\"_id\" : "; string document2 = "\""; string document4 = "\""; string document5 = ", \"name\" : \"Ori\"}"; for (int i = 0; i < 5; i++) { - string document_midlle = "26" + std::to_string(i); - string document = document1 + document2 + document_midlle + document4 + document5; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OK); + string document_midlle = "26" + std::to_string(i); + string document = document1 + document2 + document_midlle + document4 + document5; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OK); } } @@ -715,7 +715,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest038, TestSize.Level1) */ const char *document5 = R"({"_id" : "38_4", "t1" : [1, 2, -1.79769313486231570E+308]})"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document5, 0), GRD_OK); - /** + /** * @tc.steps:step6.Insert document with maxium double value * @tc.expected:step6.GRD_INVALID_ARGS. */ @@ -736,7 +736,7 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest039, TestSize.Level1) * @tc.steps:step1.Insert a filter which _id value's lens is larger than MAX_ID_LENS. * @tc.expected:step1.GRD_OVER_LIMIT. */ - string document1 = "{\"_id\" : "; + string document1 = "{\"_id\" : "; string document2 = "\""; string document4 = "\""; string document5 = ", \"name\" : \"Ori\"}"; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_test_utils.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_test_utils.h index a513875e..b1dc3f51 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_test_utils.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_test_utils.h @@ -17,7 +17,6 @@ #define DOCUMENTDB_TEST_UTILS_H #include - namespace DocumentDBUnitTest { class DocumentDBTestUtils { public: diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.h index a513875e..b1dc3f51 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.h @@ -17,7 +17,6 @@ #define DOCUMENTDB_TEST_UTILS_H #include - namespace DocumentDBUnitTest { class DocumentDBTestUtils { public: diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp index 11717fe7..891e61d2 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp @@ -16,8 +16,8 @@ #include #include -#include "documentdb_test_utils.h" #include "doc_errno.h" +#include "documentdb_test_utils.h" #include "json_common.h" #include "log_print.h" @@ -33,21 +33,13 @@ public: void TearDown(); }; -void DocumentDBJsonCommonTest::SetUpTestCase(void) -{ -} +void DocumentDBJsonCommonTest::SetUpTestCase(void) {} -void DocumentDBJsonCommonTest::TearDownTestCase(void) -{ -} +void DocumentDBJsonCommonTest::TearDownTestCase(void) {} -void DocumentDBJsonCommonTest::SetUp(void) -{ -} +void DocumentDBJsonCommonTest::SetUp(void) {} -void DocumentDBJsonCommonTest::TearDown(void) -{ -} +void DocumentDBJsonCommonTest::TearDown(void) {} /** * @tc.name: OpenDBTest001 @@ -59,7 +51,8 @@ void DocumentDBJsonCommonTest::TearDown(void) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest001, TestSize.Level0) { std::string document = R""({"name":"Tmn","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::string updateDoc = R""({"name":"Xue","case":{"field1":1,"field2":"string","field3":[1,2,3]},"age":28,"addr":{"city":"shenzhen","postal":518000}})""; + std::string updateDoc = + R""({"name":"Xue","case":{"field1":1,"field2":"string","field3":[1,2,3]},"age":28,"addr":{"city":"shenzhen","postal":518000}})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -70,11 +63,11 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest001, TestSize.Level0) EXPECT_EQ(JsonCommon::Append(src, add), E_OK); GLOGD("result: %s", src.Print().c_str()); - JsonObject itemCase = src.FindItem({"case", "field1"}, errCode); + JsonObject itemCase = src.FindItem({ "case", "field1" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemCase.GetItemValue().GetIntValue(), 1); - JsonObject itemName = src.FindItem({"name"}, errCode); + JsonObject itemName = src.FindItem({ "name" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemName.GetItemValue().GetStringValue(), "Xue"); } @@ -93,20 +86,21 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest002, TestSize.Level0) EXPECT_EQ(JsonCommon::Append(src, add), E_OK); GLOGD("result: %s", src.Print().c_str()); - JsonObject itemCase = src.FindItem({"grade"}, errCode); + JsonObject itemCase = src.FindItem({ "grade" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemCase.GetItemValue().GetIntValue(), 99); // 99: grade - JsonObject itemName = src.FindItem({"name", "1"}, errCode); + JsonObject itemName = src.FindItem({ "name", "1" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemName.GetItemValue().GetStringValue(), "Neco"); } - HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest003, TestSize.Level0) { - std::string document = R""({"name":["Tmn","BB","Alice"],"age":[1,2,3],"addr":[{"city":"shanghai","postal":200001},{"city":"wuhan","postal":430000}]})""; - std::string updateDoc = R""({"name":["Xue","Neco","Lip"],"age":18,"addr":[{"city":"shanghai","postal":200001},{"city":"beijing","postal":100000}]})""; + std::string document = + R""({"name":["Tmn","BB","Alice"],"age":[1,2,3],"addr":[{"city":"shanghai","postal":200001},{"city":"wuhan","postal":430000}]})""; + std::string updateDoc = + R""({"name":["Xue","Neco","Lip"],"age":18,"addr":[{"city":"shanghai","postal":200001},{"city":"beijing","postal":100000}]})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -116,11 +110,11 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest003, TestSize.Level0) EXPECT_EQ(JsonCommon::Append(src, add), E_OK); GLOGD("result: %s", src.Print().c_str()); - JsonObject itemCase = src.FindItem({"addr", "1", "city"}, errCode); + JsonObject itemCase = src.FindItem({ "addr", "1", "city" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "beijing"); // 99: grade - JsonObject itemName = src.FindItem({"name", "1"}, errCode); + JsonObject itemName = src.FindItem({ "name", "1" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemName.GetItemValue().GetStringValue(), "Neco"); } @@ -128,7 +122,8 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest003, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest004, TestSize.Level0) { std::string document = R""({"name":["Tmn","BB","Alice"]})""; - std::string updateDoc = R""({"name.5":"GG"})"";; + std::string updateDoc = R""({"name.5":"GG"})""; + ; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -142,7 +137,8 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest004, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest005, TestSize.Level0) { std::string document = R""({"name":["Tmn","BB","Alice"]})""; - std::string updateDoc = R""({"name.2":"GG"})"";; + std::string updateDoc = R""({"name.2":"GG"})""; + ; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -153,7 +149,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest005, TestSize.Level0) EXPECT_EQ(JsonCommon::Append(src, add), E_OK); GLOGD("result: %s", src.Print().c_str()); - JsonObject itemCase = src.FindItem({"name", "2"}, errCode); + JsonObject itemCase = src.FindItem({ "name", "2" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "GG"); } @@ -172,7 +168,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest006, TestSize.Level0) EXPECT_EQ(JsonCommon::Append(src, add), E_OK); GLOGD("result: %s", src.Print().c_str()); - JsonObject itemCase = src.FindItem({"name", "midle.AA"}, errCode); + JsonObject itemCase = src.FindItem({ "name", "midle.AA" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "GG"); } @@ -191,7 +187,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest007, TestSize.Level0) EXPECT_EQ(JsonCommon::Append(src, add), E_OK); GLOGD("result: %s", src.Print().c_str()); - JsonObject itemCase = src.FindItem({"name", "first", "0"}, errCode); + JsonObject itemCase = src.FindItem({ "name", "first", "0" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "LL"); } @@ -210,12 +206,11 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest008, TestSize.Level0) EXPECT_EQ(JsonCommon::Append(src, add), E_OK); GLOGD("result: %s", src.Print().c_str()); - JsonObject itemCase = src.FindItem({"name", "first", "0"}, errCode); + JsonObject itemCase = src.FindItem({ "name", "first", "0" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "XXX"); } - HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest009, TestSize.Level0) { std::string document = R""({"name":{"first":["XXX","BBB","CCC"],"last":"moray"}})""; @@ -230,7 +225,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest009, TestSize.Level0) EXPECT_EQ(JsonCommon::Append(src, add), E_OK); GLOGD("result: %s", src.Print().c_str()); - JsonObject itemCase = src.FindItem({"name", "first"}, errCode); + JsonObject itemCase = src.FindItem({ "name", "first" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "XX"); } @@ -249,7 +244,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest010, TestSize.Level0) EXPECT_EQ(JsonCommon::Append(src, add), E_OK); GLOGD("result: %s", src.Print().c_str()); - JsonObject itemCase = src.FindItem({"name", "first", "XX"}, errCode); + JsonObject itemCase = src.FindItem({ "name", "first", "XX" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "AA"); } @@ -272,7 +267,8 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest011, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest012, TestSize.Level0) { std::string document = R""({"name":["Tmn","BB","Alice"]})""; - std::string updateDoc = R""({"name.first":"GG"})"";; + std::string updateDoc = R""({"name.first":"GG"})""; + ; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -286,7 +282,8 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest012, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest013, TestSize.Level0) { std::string document = R""({"name":["Tmn","BB","Alice"]})""; - std::string updateDoc = R""({"name":{"first":"GG"}})"";; + std::string updateDoc = R""({"name":{"first":"GG"}})""; + ; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -300,7 +297,8 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest013, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest014, TestSize.Level0) { std::string document = R""({"name":{"first":"Xue","second":"Lang"}})""; - std::string updateDoc = R""({"name.0":"GG"})"";; + std::string updateDoc = R""({"name.0":"GG"})""; + ; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -314,7 +312,8 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest014, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest015, TestSize.Level0) { std::string document = R""({"name":{"first":"Xue","second":"Lang"}})""; - std::string updateDoc = R""({"name.first":["GG","MM"]})"";; + std::string updateDoc = R""({"name.first":["GG","MM"]})""; + ; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -324,7 +323,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest015, TestSize.Level0) EXPECT_EQ(JsonCommon::Append(src, add), E_OK); GLOGD("result: %s", src.Print().c_str()); - JsonObject itemCase = src.FindItem({"name", "first", "0"}, errCode); + JsonObject itemCase = src.FindItem({ "name", "first", "0" }, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "GG"); } \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp index fa16a27a..b9ce2849 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp @@ -14,8 +14,9 @@ */ #include -#include "documentdb_test_utils.h" + #include "doc_errno.h" +#include "documentdb_test_utils.h" #include "json_object.h" using namespace DocumentDB; @@ -30,21 +31,13 @@ public: void TearDown(); }; -void DocumentDBJsonObjectTest::SetUpTestCase(void) -{ -} +void DocumentDBJsonObjectTest::SetUpTestCase(void) {} -void DocumentDBJsonObjectTest::TearDownTestCase(void) -{ -} +void DocumentDBJsonObjectTest::TearDownTestCase(void) {} -void DocumentDBJsonObjectTest::SetUp(void) -{ -} +void DocumentDBJsonObjectTest::SetUp(void) {} -void DocumentDBJsonObjectTest::TearDown(void) -{ -} +void DocumentDBJsonObjectTest::TearDown(void) {} /** * @tc.name: OpenDBTest001 @@ -61,7 +54,7 @@ HWTEST_F(DocumentDBJsonObjectTest, JsonObjectTest001, TestSize.Level0) JsonObject conf = JsonObject::Parse(config, ret); EXPECT_EQ(ret, E_OK); - ValueObject obj = conf.GetObjectByPath({"b", "c"}, ret); + ValueObject obj = conf.GetObjectByPath({ "b", "c" }, ret); EXPECT_EQ(obj.GetValueType(), ValueObject::ValueType::VALUE_NUMBER); EXPECT_EQ(obj.GetIntValue(), 234); -- Gitee From 8b0a28e45b8e3d66a71e44e4e97c2beae86d4b03 Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 4 May 2023 11:30:22 +0800 Subject: [PATCH 076/152] f Signed-off-by: hanlu --- services/distributeddataservice/service/BUILD.gn | 10 +++++----- .../include/grd_base/grd_resultset_api.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index ef6f4d53..746b371a 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -80,10 +80,10 @@ ohos_shared_library("distributeddatasvc") { "data_share/common/uri_utils.cpp", "data_share/data/published_data.cpp", "data_share/data/template_data.cpp", + "data_share/data_share_obs_proxy.cpp", "data_share/data_share_service_impl.cpp", "data_share/data_share_service_stub.cpp", "data_share/data_share_types_util.cpp", - "data_share/data_share_obs_proxy.cpp", "data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp", "data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp", "data_share/strategies/delete_strategy.cpp", @@ -140,16 +140,16 @@ ohos_shared_library("distributeddatasvc") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share/gaussdb_rd_simple:documentdb", "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb:distributeddb", "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata:distributeddata_inner", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share/gaussdb_rd_simple:documentdb" ] external_deps = [ - "ability_base:zuri", "ability_base:want", - "ability_runtime:dataobs_manager", + "ability_base:zuri", "ability_runtime:ability_manager", + "ability_runtime:dataobs_manager", "access_token:libaccesstoken_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", @@ -161,8 +161,8 @@ ohos_shared_library("distributeddatasvc") { "huks:libhukssdk", "ipc:ipc_core", "relational_store:native_rdb", - "relational_store:rdb_data_share_adapter", "relational_store:rdb_bms_adapter", + "relational_store:rdb_data_share_adapter", "resource_management:global_resmgr", "samgr:samgr_proxy", ] diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_resultset_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_resultset_api.h index 16e0b007..c47274bf 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_resultset_api.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_resultset_api.h @@ -15,7 +15,7 @@ #ifndef GRD_RESULTSET_API_H #define GRD_RESULTSET_API_H - +#include "grd_type_export.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus -- Gitee From f37d64fdbf85f5dfb95cbd5b6bcca912885e4644 Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 4 May 2023 14:46:37 +0800 Subject: [PATCH 077/152] f Signed-off-by: hanlu --- .../service/data_share/data/published_data.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/data_share/data/published_data.cpp b/services/distributeddataservice/service/data_share/data/published_data.cpp index a4a90611..828eca25 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.cpp +++ b/services/distributeddataservice/service/data_share/data/published_data.cpp @@ -54,8 +54,7 @@ std::vector PublishedData::Query(const std::string &bundleName) std::vector queryResults; json filter; filter["bundleName"] = bundleName; - int32_t status = - delegate->GetBatch(KvDBDelegate::DATA_TABLE, filter.dump(), PublishedData::GetFullProjection(), queryResults); + int32_t status = delegate->GetBatch(KvDBDelegate::DATA_TABLE, filter.dump(), "{}", queryResults); if (status != E_OK) { ZLOGE("db Upsert failed, %{public}s %{public}d", bundleName.c_str(), status); return std::vector(); -- Gitee From 60ad62c218ff6e92acdec85a82a2eb9d6a3a6cad Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 4 May 2023 16:10:13 +0800 Subject: [PATCH 078/152] f Signed-off-by: hanlu --- .../include/metadata/store_meta_data.h | 2 + .../data_share/common/template_manager.cpp | 142 +++++++++--------- 2 files changed, 76 insertions(+), 68 deletions(-) diff --git a/services/distributeddataservice/framework/include/metadata/store_meta_data.h b/services/distributeddataservice/framework/include/metadata/store_meta_data.h index bb134e35..98719900 100644 --- a/services/distributeddataservice/framework/include/metadata/store_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/store_meta_data.h @@ -55,6 +55,8 @@ struct API_EXPORT StoreMetaData final : public Serializable { STORE_RELATIONAL_END = 19, STORE_OBJECT_BEGIN = 20, STORE_OBJECT_END = 29, + STORE_DATA_SHARE_BEGIN = 30, + STORE_DATA_SHARE_END = 39, STORE_BUTT = 255 }; diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index a98af697..62fd5587 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -140,21 +140,20 @@ int RdbSubscriberManager::AddRdbSubscriber(const std::string &uri, const Templat { int result = E_OK; Key key(uri, tplId.subscriberId_, tplId.bundleName_); - rdbCache_.Compute( - key, [&observer, &context, &result, this](const auto &key, std::vector &value) { - ZLOGI("add subscriber, uri %{private}s tokenId %{public}d", key.uri_.c_str(), context->callerTokenId); - ObserverNode observerNode(observer, context->callerTokenId); - std::vector node({ observerNode }); - result = Notify(key, node, context->calledSourceDir, context->version); - if (result != E_OK) { - return false; - } - value.emplace_back(observerNode); - if (GetEnableObserverCount(key) == 1) { - SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); - } - return true; - }); + rdbCache_.Compute(key, [&observer, &context, &result, this](const auto &key, std::vector &value) { + ZLOGI("add subscriber, uri %{private}s tokenId %{public}d", key.uri_.c_str(), context->callerTokenId); + ObserverNode observerNode(observer, context->callerTokenId); + std::vector node({ observerNode }); + result = Notify(key, node, context->calledSourceDir, context->version); + if (result != E_OK) { + return false; + } + value.emplace_back(observerNode); + if (GetEnableObserverCount(key) == 1) { + SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); + } + return true; + }); return result; } @@ -162,21 +161,22 @@ int RdbSubscriberManager::DelRdbSubscriber( const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId) { Key key(uri, tplId.subscriberId_, tplId.bundleName_); - auto result = rdbCache_.ComputeIfPresent(key, [&callerTokenId, this](const auto &key, std::vector &value) { - ZLOGI("delete subscriber, uri %{public}s tokenId %{public}d", key.uri_.c_str(), callerTokenId); - for (auto it = value.begin(); it != value.end();) { - if (it->callerTokenId == callerTokenId) { - ZLOGI("erase start"); - it = value.erase(it); - } else { - it++; + auto result = + rdbCache_.ComputeIfPresent(key, [&callerTokenId, this](const auto &key, std::vector &value) { + ZLOGI("delete subscriber, uri %{public}s tokenId %{public}d", key.uri_.c_str(), callerTokenId); + for (auto it = value.begin(); it != value.end();) { + if (it->callerTokenId == callerTokenId) { + ZLOGI("erase start"); + it = value.erase(it); + } else { + it++; + } } - } - if (GetEnableObserverCount(key) == 0) { - SchedulerManager::GetInstance().RemoveTimer(key); - } - return !value.empty(); - }); + if (GetEnableObserverCount(key) == 0) { + SchedulerManager::GetInstance().RemoveTimer(key); + } + return !value.empty(); + }); return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; } @@ -184,21 +184,23 @@ int RdbSubscriberManager::DisableRdbSubscriber( const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId) { Key key(uri, tplId.subscriberId_, tplId.bundleName_); - auto result = rdbCache_.ComputeIfPresent(key, [&callerTokenId, this](const auto &key, std::vector &value) { - for (auto it = value.begin(); it != value.end(); it++) { - if (it->callerTokenId == callerTokenId) { - it->enabled = false; + auto result = + rdbCache_.ComputeIfPresent(key, [&callerTokenId, this](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end(); it++) { + if (it->callerTokenId == callerTokenId) { + it->enabled = false; + } } - } - if (GetEnableObserverCount(key) == 0) { - SchedulerManager::GetInstance().RemoveTimer(key); - } - return true; - }); + if (GetEnableObserverCount(key) == 0) { + SchedulerManager::GetInstance().RemoveTimer(key); + } + return true; + }); return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; } -int RdbSubscriberManager::EnableRdbSubscriber(const std::string &uri, const TemplateId &tplId, std::shared_ptr context) +int RdbSubscriberManager::EnableRdbSubscriber(const std::string &uri, const TemplateId &tplId, + std::shared_ptr context) { Key key(uri, tplId.subscriberId_, tplId.bundleName_); auto result = rdbCache_.ComputeIfPresent(key, [&context, this](const auto &key, std::vector &value) { @@ -353,8 +355,8 @@ int PublishedDataSubscriberManager::AddSubscriber(const std::string &key, const const int64_t subscriberId, const sptr observer, const uint32_t callerTokenId) { PublishedDataKey publishedDataKey(key, callerBundleName, subscriberId); - publishedDataCache_.Compute( - publishedDataKey, [&observer, &callerTokenId, this](const PublishedDataKey &key, std::vector &value) { + publishedDataCache_.Compute(publishedDataKey, + [&observer, &callerTokenId, this](const PublishedDataKey &key, std::vector &value) { ZLOGI("add publish subscriber, uri %{private}s tokenId %{public}d", key.key_.c_str(), callerTokenId); value.emplace_back(observer, callerTokenId); return true; @@ -366,16 +368,17 @@ int PublishedDataSubscriberManager::DelSubscriber(const std::string &uri, const const int64_t subscriberId, const uint32_t callerTokenId) { PublishedDataKey key(uri, callerBundleName, subscriberId); - auto result = publishedDataCache_.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { - for (auto it = value.begin(); it != value.end();) { - if (it->callerTokenId == callerTokenId) { - it = value.erase(it); - } else { - it++; + auto result = + publishedDataCache_.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end();) { + if (it->callerTokenId == callerTokenId) { + it = value.erase(it); + } else { + it++; + } } - } - return !value.empty(); - }); + return !value.empty(); + }); return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; } @@ -383,14 +386,15 @@ int PublishedDataSubscriberManager::DisableSubscriber(const std::string &uri, co const int64_t subscriberId, const uint32_t callerTokenId) { PublishedDataKey key(uri, callerBundleName, subscriberId); - auto result = publishedDataCache_.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { - for (auto it = value.begin(); it != value.end(); it++) { - if (it->callerTokenId == callerTokenId) { - it->enabled = false; + auto result = + publishedDataCache_.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end(); it++) { + if (it->callerTokenId == callerTokenId) { + it->enabled = false; + } } - } - return true; - }); + return true; + }); return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; } @@ -398,19 +402,20 @@ int PublishedDataSubscriberManager::EnableSubscriber(const std::string &uri, con const int64_t subscriberId, const uint32_t callerTokenId) { PublishedDataKey key(uri, callerBundleName, subscriberId); - auto result = publishedDataCache_.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { - for (auto it = value.begin(); it != value.end(); it++) { - if (it->callerTokenId == callerTokenId) { - it->enabled = true; + auto result = + publishedDataCache_.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { + for (auto it = value.begin(); it != value.end(); it++) { + if (it->callerTokenId == callerTokenId) { + it->enabled = true; + } } - } - return true; - }); + return true; + }); return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; } -void PublishedDataSubscriberManager::Emit( - std::vector keys, const std::string &ownerBundleName, const sptr observer) +void PublishedDataSubscriberManager::Emit(std::vector keys, const std::string &ownerBundleName, + const sptr observer) { int32_t status; // key is bundleName, value is change node @@ -425,7 +430,8 @@ void PublishedDataSubscriberManager::Emit( publishedResult[key].subscriberId_ = data.subscriberId_; publishedResult[key].key_ = data.key_; PublishedData publishedData(data.key_, data.bundleName_, data.subscriberId_); - status = PublishedData::Query(DistributedData::Serializable::Marshall(*publishedData.GetId()), publishedResult[key].value_); + status = PublishedData::Query( + DistributedData::Serializable::Marshall(*publishedData.GetId()), publishedResult[key].value_); if (status != E_OK) { ZLOGE("query fail %{public}s %{public}s %{public}" PRId64, data.bundleName_.c_str(), data.key_.c_str(), data.subscriberId_); -- Gitee From 9dac4a9dcfcc386703cd23727a0053072bb8c7c8 Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 4 May 2023 19:21:14 +0800 Subject: [PATCH 079/152] f Signed-off-by: hanlu --- .../service/data_share/common/kv_delegate.cpp | 108 -- .../service/data_share/common/kv_delegate.h | 3 - .../data_share/common/template_manager.cpp | 3 +- .../data_share/gaussdb_rd_simple/BUILD.gn | 91 -- .../data_share/gaussdb_rd_simple/README.md | 1 - .../include/grd_base/grd_db_api.h | 34 - .../include/grd_base/grd_error.h | 86 - .../include/grd_base/grd_resultset_api.h | 36 - .../include/grd_base/grd_type_export.h | 68 - .../include/grd_document/grd_document_api.h | 46 - .../src/common/include/collection_option.h | 38 - .../src/common/include/db_config.h | 46 - .../src/common/include/doc_limit.h | 22 - .../src/common/include/json_common.h | 48 - .../src/common/include/log_print.h | 45 - .../src/common/include/os_api.h | 30 - .../src/common/src/collection_option.cpp | 117 -- .../src/common/src/db_config.cpp | 293 ---- .../src/common/src/json_common.cpp | 311 ---- .../src/common/src/log_print.cpp | 83 - .../src/common/src/os_api.cpp | 85 - .../src/executor/base/grd_db_api.cpp | 75 - .../src/executor/document/document_check.cpp | 153 -- .../src/executor/document/document_check.h | 42 - .../executor/document/grd_document_api.cpp | 122 -- .../executor/document/grd_resultset_api.cpp | 71 - .../src/executor/include/grd_format_config.h | 31 - .../executor/include/grd_resultset_inner.h | 26 - .../src/executor/include/grd_type_inner.h | 27 - .../src/interface/include/collection.h | 44 - .../src/interface/include/doc_errno.h | 44 - .../src/interface/include/document_store.h | 53 - .../include/document_store_manager.h | 36 - .../src/interface/include/projection_tree.h | 41 - .../src/interface/include/result_set.h | 57 - .../src/interface/include/result_set_common.h | 31 - .../src/interface/src/collection.cpp | 177 -- .../src/interface/src/doc_errno.cpp | 80 - .../src/interface/src/document_store.cpp | 448 ----- .../interface/src/document_store_manager.cpp | 143 -- .../src/interface/src/projection_tree.cpp | 75 - .../src/interface/src/result_set.cpp | 159 -- .../src/interface/src/result_set_common.cpp | 31 - .../src/oh_adapter/include/json_object.h | 114 -- .../oh_adapter/include/kv_store_executor.h | 41 - .../src/oh_adapter/include/kv_store_manager.h | 30 - .../src/oh_adapter/src/json_object.cpp | 539 ------ .../src/oh_adapter/src/kv_store_manager.cpp | 70 - .../src/sqlite_store_executor_impl.cpp | 271 --- .../src/sqlite_store_executor_impl.h | 50 - .../src/oh_adapter/src/sqlite_utils.cpp | 306 ---- .../src/oh_adapter/src/sqlite_utils.h | 58 - .../gaussdb_rd_simple/test/unittest/BUILD.gn | 164 -- .../test/unittest/api/documentdb_api_test.cpp | 637 -------- .../api/documentdb_collection_test.cpp | 204 --- .../unittest/api/documentdb_data_test.cpp | 314 ---- .../unittest/api/documentdb_delete_test.cpp | 320 ---- .../unittest/api/documentdb_find_test.cpp | 1455 ----------------- .../unittest/api/documentdb_insert_test.cpp | 754 --------- .../test/unittest/api/documentdb_test_utils.h | 26 - .../unittest/common/documentdb_test_utils.cpp | 24 - .../unittest/common/documentdb_test_utils.h | 26 - .../documentdb_json_common_test.cpp | 329 ---- .../oh_adapter/documentdb_jsonobject_test.cpp | 61 - ...d_config_from_data_proxy_node_strategy.cpp | 3 +- ...g_from_data_share_bundle_info_strategy.cpp | 4 +- 66 files changed, 4 insertions(+), 9356 deletions(-) delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/BUILD.gn delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/README.md delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_db_api.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_error.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_resultset_api.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_type_export.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_document/grd_document_api.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/collection_option.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/db_config.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/doc_limit.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/log_print.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/os_api.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/collection_option.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/db_config.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/log_print.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/os_api.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/base/grd_db_api.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_document_api.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_resultset_api.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_format_config.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_resultset_inner.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_type_inner.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store_manager.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/projection_tree.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set_common.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store_manager.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/projection_tree.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set_common.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_manager.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/kv_store_manager.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/BUILD.gn delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_api_test.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_collection_test.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_delete_test.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_test_utils.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp diff --git a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp index 113abe4e..6bb653c9 100644 --- a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp @@ -16,41 +16,17 @@ #include "kv_delegate.h" #include "datashare_errno.h" #include "directory_manager.h" -#include "grd_document/grd_document_api.h" -#include "grd_base/grd_error.h" #include "ipc_skeleton.h" #include "log_print.h" namespace OHOS::DataShare { int64_t KvDelegate::Upsert(const std::string &collectionName, const std::string &filter, const std::string &value) { - std::lock_guard lock(mutex_); - if (!Init()) { - ZLOGE("init failed, %{public}s", collectionName.c_str()); - return E_ERROR; - } - int count = GRD_UpsertDoc(db_, collectionName.c_str(), filter.c_str(), value.c_str(), 0); - if (count <= 0) { - ZLOGE("GRD_UpSertDoc failed,status %{public}d", count); - return count; - } - Flush(); return E_OK; } int64_t KvDelegate::Delete(const std::string &collectionName, const std::string &filter) { - std::lock_guard lock(mutex_); - if (!Init()) { - ZLOGE("init failed, %{public}s", collectionName.c_str()); - return E_ERROR; - } - int count = GRD_DeleteDoc(db_, collectionName.c_str(), filter.c_str(), 0); - if (count <= 0) { - ZLOGE("GRD_UpSertDoc failed,status %{public}d", count); - return count; - } - Flush(); return E_OK; } @@ -59,36 +35,12 @@ bool KvDelegate::Init() if (isInitDone_) { return true; } - int status = GRD_DBOpen((path_ + "/dataShare.db").c_str(), nullptr, GRD_DB_OPEN_CREATE, &db_); - if (status != GRD_OK || db_ == nullptr) { - ZLOGE("GRD_DBOpen failed,status %{public}d", status); - return false; - } - - status = GRD_CreateCollection(db_, TEMPLATE_TABLE.c_str(), nullptr, 0); - if (status != GRD_OK) { - ZLOGE("GRD_CreateCollection template table failed,status %{public}d", status); - return false; - } - - status = GRD_CreateCollection(db_, DATA_TABLE.c_str(), nullptr, 0); - if (status != GRD_OK) { - ZLOGE("GRD_CreateCollection data table failed,status %{public}d", status); - return false; - } isInitDone_ = true; return true; } KvDelegate::~KvDelegate() { - std::lock_guard lock(mutex_); - if (isInitDone_) { - int status = GRD_DBClose(db_, 0); - if (status != GRD_OK) { - ZLOGE("GRD_DBClose failed,status %{public}d", status); - } - } } int32_t KvDelegate::Upsert(const std::string &collectionName, const KvData &value) @@ -141,76 +93,16 @@ bool KvDelegate::GetVersion(const std::string &collectionName, const std::string int32_t KvDelegate::Get( const std::string &collectionName, const std::string &filter, const std::string &projection, std::string &result) { - std::lock_guard lock(mutex_); - if (!Init()) { - ZLOGE("init failed, %{public}s", collectionName.c_str()); - return E_ERROR; - } - Query query; - query.filter = filter.c_str(); - query.projection = projection.c_str(); - GRD_ResultSet *resultSet = nullptr; - int status = GRD_FindDoc(db_, collectionName.c_str(), query, 0, &resultSet); - if (status != GRD_OK || resultSet == nullptr) { - ZLOGE("GRD_FindDoc failed,status %{public}d", status); - return status; - } - status = GRD_Next(resultSet); - if (status != GRD_OK) { - GRD_FreeResultSet(resultSet); - ZLOGE("GRD_Next failed,status %{public}d", status); - return status; - } - char *value = nullptr; - status = GRD_GetValue(resultSet, &value); - if (status != GRD_OK || value == nullptr) { - GRD_FreeResultSet(resultSet); - ZLOGE("GRD_GetValue failed,status %{public}d", status); - return status; - } - result = value; - GRD_FreeValue(value); - GRD_FreeResultSet(resultSet); return E_OK; } void KvDelegate::Flush() { - int status = GRD_Flush(db_, GRD_DB_FLUSH_ASYNC); - if (status != GRD_OK) { - ZLOGE("GRD_Flush failed,status %{public}d", status); - } } int32_t KvDelegate::GetBatch(const std::string &collectionName, const std::string &filter, const std::string &projection, std::vector &result) { - std::lock_guard lock(mutex_); - if (!Init()) { - ZLOGE("init failed, %{public}s", collectionName.c_str()); - return E_ERROR; - } - Query query; - query.filter = filter.c_str(); - query.projection = projection.c_str(); - GRD_ResultSet *resultSet; - int status = GRD_FindDoc(db_, collectionName.c_str(), query, 0, &resultSet); - if (status != GRD_OK || resultSet == nullptr) { - ZLOGE("GRD_UpSertDoc failed,status %{public}d", status); - return status; - } - char *value = nullptr; - while (GRD_Next(resultSet) == GRD_OK) { - status = GRD_GetValue(resultSet, &value); - if (status != GRD_OK || value == nullptr) { - GRD_FreeResultSet(resultSet); - ZLOGE("GRD_GetValue failed,status %{public}d", status); - return status; - } - result.emplace_back(value); - GRD_FreeValue(value); - } - GRD_FreeResultSet(resultSet); return E_OK; } diff --git a/services/distributeddataservice/service/data_share/common/kv_delegate.h b/services/distributeddataservice/service/data_share/common/kv_delegate.h index e3a29031..82742960 100644 --- a/services/distributeddataservice/service/data_share/common/kv_delegate.h +++ b/services/distributeddataservice/service/data_share/common/kv_delegate.h @@ -20,7 +20,6 @@ #include #include "db_delegate.h" -#include "grd_base/grd_db_api.h" namespace OHOS::DataShare { class KvDelegate final : public KvDBDelegate { @@ -42,9 +41,7 @@ private: int64_t Upsert(const std::string &collectionName, const std::string &filter, const std::string &value); int64_t Delete(const std::string &collectionName, const std::string &filter); void Flush(); - std::mutex mutex_; std::string path_; - GRD_DB *db_ = nullptr; bool isInitDone_ = false; }; } // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index 62fd5587..327441c3 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -199,7 +199,7 @@ int RdbSubscriberManager::DisableRdbSubscriber( return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; } -int RdbSubscriberManager::EnableRdbSubscriber(const std::string &uri, const TemplateId &tplId, +int RdbSubscriberManager::EnableRdbSubscriber(const std::string &uri, const TemplateId &tplId, std::shared_ptr context) { Key key(uri, tplId.subscriberId_, tplId.bundleName_); @@ -232,7 +232,6 @@ public: ZLOGE("null value %{public}s", key_.c_str()); return false; } - // ZLOGE("key %{public}s value %{public}s", key_.c_str(), DistributedData::Serializable::Marshall(value_).c_str()); return SetValue(node[key_], *value_); } bool Unmarshal(const json &node) override diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/BUILD.gn b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/BUILD.gn deleted file mode 100644 index 53e16705..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/BUILD.gn +++ /dev/null @@ -1,91 +0,0 @@ -# Copyright (c) 2021 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. -import("//build/ohos.gni") - -config("documentdb_config") { - visibility = [ ":*" ] - include_dirs = [ - "src/common/include", - "src/executor/include", - "src/executor/document", - "src/oh_adapter/include", - "src/oh_adapter/src", - "src/interface/include", - ] - - defines = [ - "SQLITE_ENABLE_SNAPSHOT", - "SQLITE_HAS_CODEC", - "SQLITE_ENABLE_JSON1", - "USING_HILOG_LOGGER", - "USE_SQLITE_SYMBOLS", - "SQLITE_ENABLE_DROPTABLE_CALLBACK", - ] - if (is_debug) { - defines += [ "TRACE_SQLITE_EXECUTE" ] - } -} - -config("documentdb_public_config") { - visibility = [ "*:*" ] - include_dirs = [ "include" ] -} - -group("build_module") { - deps = [ ":documentdb" ] -} - -ohos_shared_library("documentdb") { - sources = [ - "src/common/src/collection_option.cpp", - "src/common/src/db_config.cpp", - "src/common/src/json_common.cpp", - "src/common/src/log_print.cpp", - "src/common/src/os_api.cpp", - "src/executor/base/grd_db_api.cpp", - "src/executor/document/document_check.cpp", - "src/executor/document/grd_document_api.cpp", - "src/executor/document/grd_resultset_api.cpp", - "src/interface/src/collection.cpp", - "src/interface/src/doc_errno.cpp", - "src/interface/src/document_store.cpp", - "src/interface/src/document_store_manager.cpp", - "src/interface/src/projection_tree.cpp", - "src/interface/src/result_set.cpp", - "src/interface/src/result_set_common.cpp", - "src/oh_adapter/src/json_object.cpp", - "src/oh_adapter/src/kv_store_manager.cpp", - "src/oh_adapter/src/sqlite_store_executor_impl.cpp", - "src/oh_adapter/src/sqlite_utils.cpp", - ] - - configs = [ ":documentdb_config" ] - public_configs = [ ":documentdb_public_config" ] - - deps = [ "//third_party/sqlite:sqlite" ] - - configs += [ "//third_party/cJSON:cJSON_config" ] - ldflags = [ "-Wl,--exclude-libs,ALL" ] - cflags_cc = [ "-fvisibility=hidden" ] - deps += [ "//third_party/cJSON:cjson" ] - - external_deps = [ - "c_utils:utils", - "hisysevent_native:libhisysevent", - "hitrace_native:hitrace_meter", - "hiviewdfx_hilog_native:libhilog", - ] - - subsystem_name = "distributeddatamgr" - part_name = "datamgr_service" -} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/README.md b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/README.md deleted file mode 100644 index 1d777fec..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/README.md +++ /dev/null @@ -1 +0,0 @@ -# Document DB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_db_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_db_api.h deleted file mode 100644 index 323c487d..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_db_api.h +++ /dev/null @@ -1,34 +0,0 @@ -/* -* 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 GRD_DB_API_H -#define GRD_DB_API_H - -#include "grd_type_export.h" - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -DOC_API int GRD_DBOpen(const char *dbPath, const char *configStr, unsigned int flags, GRD_DB **db); - -DOC_API int GRD_DBClose(GRD_DB *db, unsigned int flags); - -DOC_API int GRD_Flush(GRD_DB *db, unsigned int flags); - -#ifdef __cplusplus -} -#endif // __cplusplus -#endif // GRD_DB_API_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_error.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_error.h deleted file mode 100644 index ecc7c705..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_error.h +++ /dev/null @@ -1,86 +0,0 @@ -/* -* 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 GRD_ERROR_H -#define GRD_ERROR_H - -#include "grd_type_export.h" - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -// Error category -#define GRD_OK 0 - -// Error category -#define GRD_NOT_SUPPORT (-1000) -#define GRD_OVER_LIMIT (-2000) -#define GRD_INVALID_ARGS (-3000) -#define GRD_SYSTEM_ERR (-4000) -#define GRD_FAILED_FILE_OPERATION (-5000) -#define GRD_INVALID_FILE_FORMAT (-6000) -#define GRD_INNER_ERR (-8000) -#define GRD_RESOURCE_BUSY (-9000) - -#define GRD_NO_DATA (-11000) -#define GRD_FAILED_MEMORY_ALLOCATE (-13000) -#define GRD_FAILED_MEMORY_RELEASE (-14000) -#define GRD_DATA_CONFLICT (-16000) -#define GRD_NOT_AVAILABLE (-19000) -#define GRD_INVALID_FORMAT (-37000) -#define GRD_REBUILD_DATABASE (-38000) - -// not support -#define GRD_JSON_OPERATION_NOT_SUPPORT (-5001001) - -// Exceed limit -#define GRD_JSON_LEN_LIMIT (-5002001) - -// Invalid parameter -#define GRD_FIELD_TYPE_NOT_MATCH (-5003001) -#define GRD_LARGE_JSON_NEST (-5003002) -#define GRD_INVALID_JSON_TYPE (-5003003) -#define GRD_INVALID_CONFIG_VALUE (-5003004) -#define GRD_INVALID_OPERATOR (-5003005) -#define GRD_INVALID_PROJECTION_FIELD (-5003006) -#define GRD_INVALID_PROJECTION_VALUE (-5003007) -#define GRD_ARRAY_INDEX_NOT_FOUND (-5003008) - -// System err -#define GRD_JSON_LIB_HANDLE_FAILED (-5004001) - -// no data -#define GRD_COLLECTION_NOT_FOUND (-5011001) -#define GRD_RECORD_NOT_FOUND (-5011002) -#define GRD_DB_NOT_FOUND (-11003) -#define GRD_FIELD_NOT_FOUND (-5011004) - -// data conflicted -#define GRD_COLLECTION_CONFLICT (-5016001) -#define GRD_KEY_CONFLICT (-5016002) -#define GRD_FIELD_TYPE_CONFLICT (-5016003) - -// Cursor or ResultSet not available -#define GRD_RESULT_SET_NOT_AVAILABLE (-5019001) - -// Invalid format -#define GRD_INVALID_JSON_FORMAT (-5037001) -#define GRD_INVALID_KEY_FORMAT (-5037002) -#define GRD_INVALID_COLLECTION_NAME (-5037003) -#ifdef __cplusplus -} -#endif // __cplusplus -#endif // GRD_ERROR_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_resultset_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_resultset_api.h deleted file mode 100644 index c47274bf..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_resultset_api.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -* 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 GRD_RESULTSET_API_H -#define GRD_RESULTSET_API_H -#include "grd_type_export.h" -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -typedef struct GRD_ResultSet GRD_ResultSet; - -DOC_API int GRD_Next(GRD_ResultSet *resultSet); - -DOC_API int GRD_GetValue(GRD_ResultSet *resultSet, char **value); - -DOC_API int GRD_FreeValue(char *value); - -DOC_API int GRD_FreeResultSet(GRD_ResultSet *resultSet); - -#ifdef __cplusplus -} -#endif // __cplusplus -#endif // GRD_RESULTSET_API_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_type_export.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_type_export.h deleted file mode 100644 index 02e15324..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_base/grd_type_export.h +++ /dev/null @@ -1,68 +0,0 @@ -/* -* 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 GRD_TYPE_EXPORT_H -#define GRD_TYPE_EXPORT_H - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -#ifndef _WIN32 -#define DOC_API __attribute__((visibility("default"))) -#endif - -typedef struct GRD_DB GRD_DB; - -/** - * @brief Open database config - */ -#define GRD_DB_OPEN_ONLY 0x00 -#define GRD_DB_OPEN_CREATE 0x01 -#define GRD_DB_OPEN_CHECK_FOR_ABNORMAL \ - 0x02 //check data in database if close abnormally last time, \ - //if data is corrupted, rebuild the database -#define GRD_DB_OPEN_CHECK 0x04 //check data in database when open database, if data is corrupted, rebuild the database. -/** - * @brief Close database config - */ -#define GRD_DB_CLOSE 0x00 -#define GRD_DB_CLOSE_IGNORE_ERROR 0x01 - -/** - * @brief flush database config - */ -#define GRD_DB_FLUSH_ASYNC 0x00 -#define GRD_DB_FLUSH_SYNC 0x01 - -#define GRD_DOC_ID_DISPLAY 0x01 -typedef struct Query { - const char *filter; - const char *projection; -} Query; - -/** - * @brief Flags for create and drop collection - */ -#define CHK_EXIST_COLLECTION 1 -#define CHK_NON_EXIST_COLLECTION 1 - -#define GRD_DOC_APPEND 0 -#define GRD_DOC_REPLACE 1 - -#ifdef __cplusplus -} -#endif // __cplusplus -#endif // GRD_TYPE_EXPORT_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_document/grd_document_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_document/grd_document_api.h deleted file mode 100644 index b8808904..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/include/grd_document/grd_document_api.h +++ /dev/null @@ -1,46 +0,0 @@ -/* -* 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 GRD_DOCUMENT_API_H -#define GRD_DOCUMENT_API_H - -#include "grd_base/grd_resultset_api.h" -#include "grd_base/grd_type_export.h" - -#ifdef __cplusplus -extern "C" { -#endif - -DOC_API int GRD_CreateCollection(GRD_DB *db, const char *collectionName, const char *optionStr, unsigned int flags); - -DOC_API int GRD_DropCollection(GRD_DB *db, const char *collectionName, unsigned int flags); - -DOC_API int GRD_InsertDoc(GRD_DB *db, const char *collectionName, const char *document, unsigned int flags); - -DOC_API int GRD_FindDoc( - GRD_DB *db, const char *collectionName, Query query, unsigned int flags, GRD_ResultSet **resultSet); - -DOC_API int GRD_UpdateDoc( - GRD_DB *db, const char *collectionName, const char *filter, const char *update, unsigned int flags); - -DOC_API int GRD_UpsertDoc( - GRD_DB *db, const char *collectionName, const char *filter, const char *document, unsigned int flags); - -DOC_API int GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, unsigned int flags); - -#ifdef __cplusplus -} -#endif -#endif // GRD_DOCUMENT_API_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/collection_option.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/collection_option.h deleted file mode 100644 index 42bd55db..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/collection_option.h +++ /dev/null @@ -1,38 +0,0 @@ -/* -* 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 COLLECTION_OPTION_H -#define COLLECTION_OPTION_H - -#include -#include - -namespace DocumentDB { -class CollectionOption final { -public: - static CollectionOption ReadOption(const std::string &optStr, int &errCode); - - uint32_t GetMaxDoc() const; - std::string ToString() const; - - bool operator==(const CollectionOption &targetOption) const; - bool operator!=(const CollectionOption &targetOption) const; - -private: - std::string option_ = "{}"; - uint32_t maxDoc_ = UINT32_MAX; -}; -} // namespace DocumentDB -#endif // COLLECTION_OPTION_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/db_config.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/db_config.h deleted file mode 100644 index ec7d8f44..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/db_config.h +++ /dev/null @@ -1,46 +0,0 @@ -/* -* 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 DB_CONFIG_H -#define DB_CONFIG_H - -#include - -namespace DocumentDB { -class DBConfig final { -public: - static DBConfig ReadConfig(const std::string &confStr, int &errCode); - - ~DBConfig() = default; - std::string ToString() const; - - int32_t GetPageSize() const; - - bool operator==(const DBConfig &targetConfig) const; - bool operator!=(const DBConfig &targetConfig) const; - -private: - DBConfig() = default; - - std::string configStr_ = {}; - int32_t pageSize_ = 4; // 4: default page size k - uint32_t redoFlushByTrx_ = 0; - uint32_t redoPubBufSize_ = 1024; // 1024: default 1024k buff size - int32_t maxConnNum_ = 100; // 100: default max conn - uint32_t bufferPoolSize_ = 1024; // 100: default 1024k pool size - uint32_t crcCheckEnable_ = 1; -}; -} // namespace DocumentDB -#endif // DB_CONFIG_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/doc_limit.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/doc_limit.h deleted file mode 100644 index 38ceb943..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/doc_limit.h +++ /dev/null @@ -1,22 +0,0 @@ -/* -* 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 DOC_LIMIT_H -#define DOC_LIMIT_H - -namespace DocumentDB { -constexpr const int MAX_DB_CONFIG_LEN = 512 * 1024; // 512 * 1024: 512k length -} // namespace DocumentDB -#endif // DOC_LIMIT_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h deleted file mode 100644 index f7300a06..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h +++ /dev/null @@ -1,48 +0,0 @@ -/* -* 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 JSON_COMMON_H -#define JSON_COMMON_H - -#include -#include -#include -#include - -#include "json_object.h" - -namespace DocumentDB { -class JsonCommon { -public: - JsonCommon() = default; - ~JsonCommon(); - - static ValueObject GetValueByFiled(JsonObject &node, const std::string &filed); - static bool CheckJsonField(JsonObject &node); - static bool CheckProjectionField(JsonObject &node); - static int ParseNode(JsonObject &Node, std::vector singlePath, - std::vector> &resultPath, bool isFirstFloor); - static std::vector> ParsePath(const JsonObject &node); - static std::vector GetLeafValue(JsonObject &node); - - static int Append(const JsonObject &src, const JsonObject &add); - -private: - static bool CheckNode(JsonObject &Node, std::set filedSet, bool &errFlag); - static bool CheckProjectionNode(JsonObject &Node, std::set filedSet, bool &errFlag, bool isFirstFloor); - static int CheckLeafNode(JsonObject &Node, std::vector &leafValue); -}; -} // namespace DocumentDB -#endif // JSON_COMMON_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/log_print.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/log_print.h deleted file mode 100644 index 5954986d..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/log_print.h +++ /dev/null @@ -1,45 +0,0 @@ -/* -* 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 LOG_PRINT_H -#define LOG_PRINT_H - -#include - -namespace DocumentDB { -constexpr const char *LOG_TAG_DOC = "DocumentDB"; - -class Logger { -public: - enum class Level - { - LEVEL_DEBUG, - LEVEL_INFO, - LEVEL_WARN, - LEVEL_ERROR, - LEVEL_FATAL - }; - - static void Log(Level level, const std::string &tag, const char *func, int line, const char *format, ...); -}; -} // namespace DocumentDB - -#define NO_LOG(...) // No log in normal and release. Used for convenience when deep debugging -#define GLOGD(...) Logger::Log(Logger::Level::LEVEL_DEBUG, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) -#define GLOGI(...) Logger::Log(Logger::Level::LEVEL_INFO, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) -#define GLOGW(...) Logger::Log(Logger::Level::LEVEL_WARN, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) -#define GLOGE(...) Logger::Log(Logger::Level::LEVEL_ERROR, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) -#define GLOGF(...) Logger::Log(Logger::Level::LEVEL_FATAL, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) -#endif // LOG_PRINT_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/os_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/os_api.h deleted file mode 100644 index 6bfbf3bb..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/os_api.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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. - */ -#include - -#ifndef OS_API_H -#define OS_API_H -namespace DocumentDB { -namespace OSAPI { -bool CheckPermission(const std::string &filePath); - -bool CheckPathExistence(const std::string &filePath); - -int GetRealPath(const std::string &inOriPath, std::string &outRealPath); - -void SplitFilePath(const std::string &filePath, std::string &fileDir, std::string &fileName); -} // namespace OSAPI -} // namespace DocumentDB -#endif // OS_API_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/collection_option.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/collection_option.cpp deleted file mode 100644 index c15c3895..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/collection_option.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/* -* 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. -*/ - -#include "collection_option.h" - -#include -#include - -#include "doc_errno.h" -#include "json_object.h" -#include "log_print.h" - -namespace DocumentDB { -namespace { -const std::string OPT_MAX_DOC = "maxdoc"; -const std::vector DB_CONFIG = { - OPT_MAX_DOC, -}; - -bool CheckConfigSupport(const JsonObject &config, int &errCode) -{ - JsonObject child = config.GetChild(); - while (!child.IsNull()) { - std::string fieldName = child.GetItemFiled(); - if (std::find(DB_CONFIG.begin(), DB_CONFIG.end(), fieldName) == DB_CONFIG.end()) { - GLOGE("Invalid collection config."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - child = child.GetNext(); - } - return true; -} -} // namespace -CollectionOption CollectionOption::ReadOption(const std::string &optStr, int &errCode) -{ - if (optStr.empty()) { - return {}; - } - - std::string lowerCaseOptStr = optStr; - std::transform(lowerCaseOptStr.begin(), lowerCaseOptStr.end(), lowerCaseOptStr.begin(), [](unsigned char c) { - return std::tolower(c); - }); - - JsonObject collOpt = JsonObject::Parse(lowerCaseOptStr, errCode); - if (errCode != E_OK) { - GLOGE("Read collection option failed from str. %d", errCode); - return {}; - } - - if (!CheckConfigSupport(collOpt, errCode)) { - GLOGE("Check collection option, not support config item. %d", errCode); - return {}; - } - - static const JsonFieldPath maxDocField = { OPT_MAX_DOC }; - if (!collOpt.IsFieldExists(maxDocField)) { - return {}; - } - - ValueObject maxDocValue = collOpt.GetObjectByPath(maxDocField, errCode); - if (errCode != E_OK) { - GLOGE("Read collection option failed. %d", errCode); - return {}; - } - - if (maxDocValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { - GLOGE("Check collection option failed, the field type of maxDoc is not NUMBER."); - errCode = -E_INVALID_CONFIG_VALUE; - return {}; - } - - if (maxDocValue.GetIntValue() <= 0 || static_cast(maxDocValue.GetIntValue()) > UINT32_MAX) { - GLOGE("Check collection option failed, invalid maxDoc value."); - errCode = -E_INVALID_CONFIG_VALUE; - return {}; - } - - CollectionOption option; - option.maxDoc_ = static_cast(maxDocValue.GetIntValue()); - option.option_ = optStr; - return option; -} - -uint32_t CollectionOption::GetMaxDoc() const -{ - return maxDoc_; -} - -std::string CollectionOption::ToString() const -{ - return option_; -} - -bool CollectionOption::operator==(const CollectionOption &targetOption) const -{ - return maxDoc_ == targetOption.maxDoc_; -} - -bool CollectionOption::operator!=(const CollectionOption &targetOption) const -{ - return !(*this == targetOption); -} -} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/db_config.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/db_config.cpp deleted file mode 100644 index af9b7429..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/db_config.cpp +++ /dev/null @@ -1,293 +0,0 @@ -/* -* 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. -*/ - -#include "db_config.h" - -#include -#include - -#include "doc_errno.h" -#include "doc_limit.h" -#include "json_object.h" -#include "log_print.h" - -namespace DocumentDB { -namespace { -const int MIN_REDO_BUFFER_SIZE = 256; -const int MAX_REDO_BUFFER_SIZE = 16384; -const int MIN_CONNECTION_NUM = 16; -const int MAX_CONNECTION_NUM = 1024; -const int MIN_BUFFER_POOL_SIZE = 1024; -const int MAX_BUFFER_POOL_SIZE = 4 * 1024 * 1024; - -const std::string DB_CONFIG_PAGESIZE = "pagesize"; -const std::string DB_CONFIG_REDO_FLUSH_BY_TRX = "redoflushbytrx"; -const std::string DB_CONFIG_REDO_PUB_BUFF_SIZE = "redopubbufsize"; -const std::string DB_CONFIG_MAX_CONN_NUM = "maxconnnum"; -const std::string DB_CONFIG_BUFFER_POOL_SIZE = "bufferpoolsize"; -const std::string DB_CONFIG_CRC_CHECK_ENABLE = "crccheckenable"; - -const std::vector DB_CONFIG = { DB_CONFIG_PAGESIZE, DB_CONFIG_REDO_FLUSH_BY_TRX, - DB_CONFIG_REDO_PUB_BUFF_SIZE, DB_CONFIG_MAX_CONN_NUM, DB_CONFIG_BUFFER_POOL_SIZE, DB_CONFIG_CRC_CHECK_ENABLE }; - -bool CheckPageSizeConfig(const JsonObject &config, int32_t &pageSize, int &errCode) -{ - static const JsonFieldPath pageSizeField = { DB_CONFIG_PAGESIZE }; - if (!config.IsFieldExists(pageSizeField)) { - return true; - } - - ValueObject configValue = config.GetObjectByPath(pageSizeField, errCode); - if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { - GLOGE("Check DB config failed, the field type of pageSize is not NUMBER."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - - static const std::vector pageSizeValid = { 4, 8, 16, 32, 64 }; - if (std::find(pageSizeValid.begin(), pageSizeValid.end(), configValue.GetIntValue()) == pageSizeValid.end()) { - GLOGE("Check DB config failed, invalid pageSize value."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - - pageSize = static_cast(configValue.GetIntValue()); - return true; -} - -bool CheckRedoFlushConfig(const JsonObject &config, uint32_t &redoFlush, int &errCode) -{ - static const JsonFieldPath redoFlushField = { DB_CONFIG_REDO_FLUSH_BY_TRX }; - if (!config.IsFieldExists(redoFlushField)) { - return true; - } - - ValueObject configValue = config.GetObjectByPath(redoFlushField, errCode); - if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { - GLOGE("Check DB config failed, the field type of redoFlushByTrx is not NUMBER."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - - if (configValue.GetIntValue() != 0 && configValue.GetIntValue() != 1) { - GLOGE("Check DB config failed, invalid redoFlushByTrx value."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - - redoFlush = static_cast(configValue.GetIntValue()); - return true; -} - -bool CheckRedoBufSizeConfig(const JsonObject &config, uint32_t &redoBufSize, int &errCode) -{ - static const JsonFieldPath redoBufSizeField = { DB_CONFIG_REDO_PUB_BUFF_SIZE }; - if (!config.IsFieldExists(redoBufSizeField)) { - return true; - } - - ValueObject configValue = config.GetObjectByPath(redoBufSizeField, errCode); - if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { - GLOGE("Check DB config failed, the field type of redoPubBufSize is not NUMBER."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - - if (configValue.GetIntValue() < MIN_REDO_BUFFER_SIZE || configValue.GetIntValue() > MAX_REDO_BUFFER_SIZE) { - GLOGE("Check DB config failed, invalid redoPubBufSize value."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - - redoBufSize = static_cast(configValue.GetIntValue()); - return true; -} - -bool CheckMaxConnNumConfig(const JsonObject &config, int32_t &maxConnNum, int &errCode) -{ - static const JsonFieldPath maxConnNumField = { DB_CONFIG_MAX_CONN_NUM }; - if (!config.IsFieldExists(maxConnNumField)) { - return true; - } - - ValueObject configValue = config.GetObjectByPath(maxConnNumField, errCode); - if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { - GLOGE("Check DB config failed, the field type of maxConnNum is not NUMBER."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - - if (configValue.GetIntValue() < MIN_CONNECTION_NUM || configValue.GetIntValue() > MAX_CONNECTION_NUM) { - GLOGE("Check DB config failed, invalid maxConnNum value."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - - maxConnNum = static_cast(configValue.GetIntValue()); - return true; -} - -bool CheckBufferPoolSizeConfig(const JsonObject &config, int32_t pageSize, uint32_t &redoBufSize, int &errCode) -{ - static const JsonFieldPath bufferPoolSizeField = { DB_CONFIG_BUFFER_POOL_SIZE }; - if (!config.IsFieldExists(bufferPoolSizeField)) { - return true; - } - - ValueObject configValue = config.GetObjectByPath(bufferPoolSizeField, errCode); - if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { - GLOGE("Check DB config failed, the field type of bufferPoolSize is not NUMBER."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - - if (configValue.GetIntValue() < MIN_BUFFER_POOL_SIZE || configValue.GetIntValue() > MAX_BUFFER_POOL_SIZE || - configValue.GetIntValue() < pageSize * 33) { - GLOGE("Check DB config failed, invalid bufferPoolSize value."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - - redoBufSize = static_cast(configValue.GetIntValue()); - return true; -} - -bool CheckCrcCheckEnableConfig(const JsonObject &config, uint32_t &crcCheckEnable, int &errCode) -{ - static const JsonFieldPath crcCheckEnableField = { DB_CONFIG_CRC_CHECK_ENABLE }; - if (!config.IsFieldExists(crcCheckEnableField)) { - return true; - } - - ValueObject configValue = config.GetObjectByPath(crcCheckEnableField, errCode); - if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { - GLOGE("Check DB config failed, the field type of crcCheckEnable is not NUMBER."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - - if (configValue.GetIntValue() != 0 && configValue.GetIntValue() != 1) { - GLOGE("Check DB config failed, invalid crcCheckEnable value."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - - crcCheckEnable = static_cast(configValue.GetIntValue()); - return true; -} - -bool CheckConfigSupport(const JsonObject &config, int &errCode) -{ - JsonObject child = config.GetChild(); - while (!child.IsNull()) { - std::string fieldName = child.GetItemFiled(); - if (std::find(DB_CONFIG.begin(), DB_CONFIG.end(), fieldName) == DB_CONFIG.end()) { - GLOGE("Invalid db config."); - errCode = -E_INVALID_CONFIG_VALUE; - return false; - } - child = child.GetNext(); - } - return true; -} -} // namespace - -DBConfig DBConfig::ReadConfig(const std::string &confStr, int &errCode) -{ - if (confStr.empty()) { - return {}; - } - - if (confStr.length() + 1 > MAX_DB_CONFIG_LEN) { - GLOGE("Config json string is too long."); - errCode = -E_OVER_LIMIT; - return {}; - } - - std::string lowerCaseConfStr = confStr; - std::transform(lowerCaseConfStr.begin(), lowerCaseConfStr.end(), lowerCaseConfStr.begin(), [](unsigned char c) { - return std::tolower(c); - }); - - JsonObject dbConfig = JsonObject::Parse(lowerCaseConfStr, errCode); - if (errCode != E_OK) { - GLOGE("Read DB config failed from str. %d", errCode); - return {}; - } - - if (!CheckConfigSupport(dbConfig, errCode)) { - GLOGE("Check DB config, not support config item. %d", errCode); - return {}; - } - - DBConfig conf; - if (!CheckPageSizeConfig(dbConfig, conf.pageSize_, errCode)) { - GLOGE("Check DB config 'pageSize' failed. %d", errCode); - return {}; - } - - if (!CheckRedoFlushConfig(dbConfig, conf.redoFlushByTrx_, errCode)) { - GLOGE("Check DB config 'redoFlushByTrx' failed. %d", errCode); - return {}; - } - - if (!CheckRedoBufSizeConfig(dbConfig, conf.redoPubBufSize_, errCode)) { - GLOGE("Check DB config 'redoPubBufSize' failed. %d", errCode); - return {}; - } - - if (!CheckMaxConnNumConfig(dbConfig, conf.maxConnNum_, errCode)) { - GLOGE("Check DB config 'maxConnNum' failed. %d", errCode); - return {}; - } - - if (!CheckBufferPoolSizeConfig(dbConfig, conf.pageSize_, conf.bufferPoolSize_, errCode)) { - GLOGE("Check DB config 'bufferPoolSize' failed. %d", errCode); - return {}; - } - - if (!CheckCrcCheckEnableConfig(dbConfig, conf.crcCheckEnable_, errCode)) { - GLOGE("Check DB config 'crcCheckEnable' failed. %d", errCode); - return {}; - } - - conf.configStr_ = confStr; - errCode = E_OK; - return conf; -} - -std::string DBConfig::ToString() const -{ - return configStr_; -} - -int32_t DBConfig::GetPageSize() const -{ - return pageSize_; -} - -bool DBConfig::operator==(const DBConfig &targetConfig) const -{ - return configStr_ == targetConfig.configStr_ && pageSize_ == targetConfig.pageSize_ && - redoFlushByTrx_ == targetConfig.redoFlushByTrx_ && redoPubBufSize_ == targetConfig.redoPubBufSize_ && - maxConnNum_ == targetConfig.maxConnNum_ && bufferPoolSize_ == targetConfig.bufferPoolSize_ && - crcCheckEnable_ == targetConfig.crcCheckEnable_; -} - -bool DBConfig::operator!=(const DBConfig &targetConfig) const -{ - return !(*this == targetConfig); -} -} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp deleted file mode 100644 index b92474f8..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp +++ /dev/null @@ -1,311 +0,0 @@ -/* - * 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. - */ -#include "json_common.h" - -#include -#include - -#include "doc_errno.h" -#include "log_print.h" -#include "securec.h" - -namespace DocumentDB { -ValueObject JsonCommon::GetValueByFiled(JsonObject &node, const std::string &filed) -{ - while (!node.IsNull()) { - if (node.GetItemFiled() == filed) { - auto itemValue = node.GetItemValue(); - return itemValue; - } - if (node.GetNext().IsNull()) { - return ValueObject(); - } - auto nodeNew = node.GetNext(); - node = nodeNew; - } - return ValueObject(); -} - -int JsonCommon::CheckLeafNode(JsonObject &node, std::vector &leafValue) -{ - if (node.GetChild().IsNull()) { - auto itemValue = node.GetItemValue(); - leafValue.emplace_back(itemValue); - } - if (!node.GetChild().IsNull()) { - auto nodeNew = node.GetChild(); - CheckLeafNode(nodeNew, leafValue); - } - if (!node.GetNext().IsNull()) { - auto nodeNew = node.GetNext(); - CheckLeafNode(nodeNew, leafValue); - } - return E_OK; -} - -std::vector JsonCommon::GetLeafValue(JsonObject &node) -{ - std::vector leafValue; - CheckLeafNode(node, leafValue); - return leafValue; -} - -bool JsonCommon::CheckNode(JsonObject &node, std::set filedSet, bool &errFlag) -{ - if (!errFlag) { - return false; - } - std::string fieldName; - if (!node.IsNull()) { - int ret = 0; - fieldName = node.GetItemFiled(ret); - if (filedSet.find(fieldName) == filedSet.end()) { - if (ret == E_OK) { - filedSet.insert(fieldName); - } - if (ret == E_OK && fieldName.empty()) { - errFlag = false; - return false; - } - } else { - errFlag = false; - return false; - } - for (int i = 0; i < fieldName.size(); i++) { - if (!((isalpha(fieldName[i])) || (isdigit(fieldName[i])) || '_' == fieldName[i])) { - errFlag = false; - return false; - } - if (i == 0 && (isdigit(fieldName[i]))) { - errFlag = false; - return false; - } - } - } - if (!node.GetChild().IsNull()) { - auto nodeNew = node.GetChild(); - std::set newFiledSet; - CheckNode(nodeNew, newFiledSet, errFlag); - } - if (!node.GetNext().IsNull()) { - auto nodeNew = node.GetNext(); - CheckNode(nodeNew, filedSet, errFlag); - } - return errFlag; -} - -bool JsonCommon::CheckJsonField(JsonObject &jsonObj) -{ - std::set filedSet; - bool errFlag = true; - return CheckNode(jsonObj, filedSet, errFlag); -} - -bool JsonCommon::CheckProjectionNode(JsonObject &node, std::set filedSet, bool &errFlag, bool isFirstFloor) -{ - if (!errFlag) { - return false; - } - std::string fieldName; - if (!node.IsNull()) { - int ret = 0; - fieldName = node.GetItemFiled(ret); - if (filedSet.find(fieldName) == filedSet.end()) { - if (ret == E_OK) { - filedSet.insert(fieldName); - } - if (ret == E_OK && fieldName.empty()) { - errFlag = false; - return false; - } - } else { - errFlag = false; - return false; - } - for (int i = 0; i < fieldName.size(); i++) { - if (!((isalpha(fieldName[i])) || (isdigit(fieldName[i])) || ('_' == fieldName[i]) || - (isFirstFloor && '.' == fieldName[i]))) { - errFlag = false; - return false; - } - if (i == 0 && (isdigit(fieldName[i]))) { - errFlag = false; - return false; - } - } - } - if (!node.GetChild().IsNull()) { - auto nodeNew = node.GetChild(); - std::set newFiledSet; - CheckProjectionNode(nodeNew, newFiledSet, errFlag, false); - } - if (!node.GetNext().IsNull()) { - auto nodeNew = node.GetNext(); - CheckProjectionNode(nodeNew, filedSet, errFlag, isFirstFloor); - } - return errFlag; -} - -bool JsonCommon::CheckProjectionField(JsonObject &jsonObj) -{ - std::set filedSet; - bool errFlag = true; - bool isFirstFloor = true; - return CheckProjectionNode(jsonObj, filedSet, errFlag, isFirstFloor); -} - -int JsonCommon::ParseNode(JsonObject &node, std::vector singlePath, - std::vector> &resultPath, bool isFirstFloor) -{ - std::vector fatherPath; - if (isFirstFloor) { - std::string tempParseName; - std::vector allFiledsName; - std::string priFieldName = node.GetItemFiled(); - for (int j = 0; j < priFieldName.size(); j++) { - if (priFieldName[j] != '.') { - tempParseName = tempParseName + priFieldName[j]; - } - if (priFieldName[j] == '.' || j == priFieldName.size() - 1) { - allFiledsName.emplace_back(tempParseName); - tempParseName.clear(); - } - } - fatherPath = singlePath; - singlePath.insert(singlePath.end(), allFiledsName.begin(), allFiledsName.end()); - } else { - std::vector allFiledsName; - allFiledsName.emplace_back(node.GetItemFiled()); - fatherPath = singlePath; - singlePath.insert(singlePath.end(), allFiledsName.begin(), allFiledsName.end()); - } - if (!node.GetChild().IsNull() && node.GetChild().GetItemFiled() != "") { - auto nodeNew = node.GetChild(); - ParseNode(nodeNew, singlePath, resultPath, false); - } else { - resultPath.emplace_back(singlePath); - } - if (!node.GetNext().IsNull()) { - auto nodeNew = node.GetNext(); - ParseNode(nodeNew, fatherPath, resultPath, isFirstFloor); - } - return 0; -} - -std::vector> JsonCommon::ParsePath(const JsonObject &root) -{ - std::vector> resultPath; - auto projectionJson = root.GetChild(); - if (projectionJson.IsNull()) { - GLOGE("projectionJson is null"); - } - std::vector singlePath; - ParseNode(projectionJson, singlePath, resultPath, true); - return resultPath; -} - -namespace { -JsonFieldPath ExpendPath(const JsonFieldPath &path, bool &isCollapse) -{ - if (path.size() > 1) { // only first lever has collapse field - return path; - } - JsonFieldPath splitPath; - const std::string &str = path[0]; - size_t start = 0; - size_t end = 0; - while ((end = str.find('.', start)) != std::string::npos) { - splitPath.push_back(str.substr(start, end - start)); - start = end + 1; - } - if (start < str.length()) { - splitPath.push_back(str.substr(start)); - } - isCollapse = (splitPath.size() > 1); - return splitPath; -} - -void JsonObjectIterator(const JsonObject &obj, JsonFieldPath path, - std::function foo) -{ - JsonObject child = obj.GetChild(); - while (!child.IsNull()) { - JsonFieldPath childPath = path; - childPath.push_back(child.GetItemFiled()); - if (foo != nullptr && foo(childPath, obj, child)) { - JsonObjectIterator(child, childPath, foo); - } - child = child.GetNext(); - } - return; -} -} // namespace - -int JsonCommon::Append(const JsonObject &src, const JsonObject &add) -{ - int externErrCode = E_OK; - JsonObjectIterator( - add, {}, [&src, &externErrCode](const JsonFieldPath &path, const JsonObject &father, const JsonObject &item) { - bool isCollapse = false; - JsonFieldPath itemPath = ExpendPath(path, isCollapse); - JsonFieldPath fatherPath = itemPath; - fatherPath.pop_back(); - int errCode = E_OK; - if (src.IsFieldExists(itemPath)) { - JsonObject srcItem = src.FindItem(itemPath, errCode); - if (errCode != E_OK) { - externErrCode = (externErrCode == E_OK ? errCode : externErrCode); - GLOGE("Find item in source json object failed. %d", errCode); - return false; - } - if (srcItem.GetType() == JsonObject::Type::JSON_LEAF && item.GetType() == JsonObject::Type::JSON_LEAF) { - srcItem.SetItemValue(item.GetItemValue()); - return false; // Both leaf node, no need iterate - } else if (srcItem.GetType() != item.GetType()) { - JsonObject srcFatherItem = src.FindItem(fatherPath, errCode); - if (errCode != E_OK) { - externErrCode = (externErrCode == E_OK ? errCode : externErrCode); - GLOGE("Find father item in source json object failed. %d", errCode); - return false; - } - srcFatherItem.DeleteItemFromObject(itemPath.back()); - srcFatherItem.AddItemToObject(itemPath.back(), item); - return false; // Different node types, overwrite directly, skip child node - } - return true; // Both array or object - } else { - if (isCollapse) { - GLOGE("Add collapse item to object failed, path not exist."); - externErrCode = -E_DATA_CONFLICT; - return false; - } - JsonObject srcFatherItem = src.FindItem(fatherPath, errCode); - if (errCode == E_OK) { - errCode = srcFatherItem.AddItemToObject(itemPath.back(), item); - if (errCode != E_OK) { - externErrCode = (externErrCode == E_OK ? errCode : externErrCode); - GLOGE("Add item to object failed. %d", errCode); - return false; - } - } else { - externErrCode = -E_DATA_CONFLICT; - GLOGE("Find father item in source json object failed. %d", errCode); - } - return false; // Source path not exist, overwrite directly, skip child node - } - }); - return externErrCode; -} -} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/log_print.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/log_print.cpp deleted file mode 100644 index e6df873c..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/log_print.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* -* 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. -*/ - -#include - -#include "hilog/log.h" -#include "securec.h" - -namespace DocumentDB { -namespace { - -void PrintLog(Logger::Level level, const std::string &tag, const std::string &msg) -{ - if (msg.empty()) { - return; - } - const std::string format = "%{public}s"; - OHOS::HiviewDFX::HiLogLabel label = { LOG_CORE, 0xD001630, tag.c_str() }; // log module id. // TODO: - switch (level) { - case Logger::Level::LEVEL_DEBUG: - (void)OHOS::HiviewDFX::HiLog::Debug(label, format.c_str(), msg.c_str()); - break; - case Logger::Level::LEVEL_INFO: - (void)OHOS::HiviewDFX::HiLog::Info(label, format.c_str(), msg.c_str()); - break; - case Logger::Level::LEVEL_WARN: - (void)OHOS::HiviewDFX::HiLog::Warn(label, format.c_str(), msg.c_str()); - break; - case Logger::Level::LEVEL_ERROR: - (void)OHOS::HiviewDFX::HiLog::Error(label, format.c_str(), msg.c_str()); - break; - case Logger::Level::LEVEL_FATAL: - (void)OHOS::HiviewDFX::HiLog::Fatal(label, format.c_str(), msg.c_str()); - break; - default: - break; - } -} - -void PreparePrivateLog(const char *format, std::string &outStrFormat) -{ - static const std::string PRIVATE_TAG = "s{private}"; - outStrFormat = format; - std::string::size_type pos = outStrFormat.find(PRIVATE_TAG); - if (pos != std::string::npos) { - outStrFormat.replace(pos, PRIVATE_TAG.size(), ".3s"); - } -} -} // namespace - -void Logger::Log(Level level, const std::string &tag, const char *func, int line, const char *format, ...) -{ - static const int maxLogLength = 1024; - - va_list argList; - va_start(argList, format); - char logBuff[maxLogLength]; - std::string msg; - std::string formatTemp; - PreparePrivateLog(format, formatTemp); - int bytes = vsnprintf_s(logBuff, maxLogLength, maxLogLength - 1, formatTemp.c_str(), argList); - if (bytes < 0) { - msg = "log buffer overflow!"; - } else { - msg = logBuff; - } - va_end(argList); - - PrintLog(level, tag, msg); -} -} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/os_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/os_api.cpp deleted file mode 100644 index 1af7c1ff..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/os_api.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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. - */ -#include "os_api.h" - -#include -#include -#include - -#include "doc_errno.h" -#include "log_print.h" -#include "securec.h" - -namespace DocumentDB { -namespace { -const int ACCESS_MODE_EXISTENCE = 0; -} -namespace OSAPI { -bool CheckPermission(const std::string &filePath) -{ - return (access(filePath.c_str(), R_OK) == 0) && (access(filePath.c_str(), W_OK) == 0); -} - -bool CheckPathExistence(const std::string &filePath) -{ - return (access(filePath.c_str(), ACCESS_MODE_EXISTENCE) == 0); -} - -int GetRealPath(const std::string &inOriPath, std::string &outRealPath) -{ - const unsigned int MAX_PATH_LENGTH = PATH_MAX; - if (inOriPath.length() > MAX_PATH_LENGTH || MAX_PATH_LENGTH > 0x10000) { // max limit is 64K(0x10000). - GLOGE("[OS_API] OriPath too long."); - return -E_INVALID_ARGS; - } - - char *realPath = new (std::nothrow) char[MAX_PATH_LENGTH + 1]; - if (realPath == nullptr) { - return -E_OUT_OF_MEMORY; - } - if (memset_s(realPath, MAX_PATH_LENGTH + 1, 0, MAX_PATH_LENGTH + 1) != EOK) { - delete[] realPath; - return -E_SECUREC_ERROR; - } - - if (realpath(inOriPath.c_str(), realPath) == nullptr) { - GLOGE("[OS_API] Realpath error:%d.", errno); - delete[] realPath; - return -E_SYSTEM_API_FAIL; - } - outRealPath = std::string(realPath); - delete[] realPath; - return E_OK; -} - -void SplitFilePath(const std::string &filePath, std::string &fileDir, std::string &fileName) -{ - if (filePath.empty()) { - return; - } - - auto slashPos = filePath.find_last_of('/'); - if (slashPos == std::string::npos) { - fileName = filePath; - fileDir = ""; - return; - } - - fileDir = filePath.substr(0, slashPos); - fileName = filePath.substr(slashPos + 1); - return; -} -} // namespace OSAPI -} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/base/grd_db_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/base/grd_db_api.cpp deleted file mode 100644 index fa6e5f76..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/base/grd_db_api.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* -* 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. -*/ - -#include "grd_base/grd_db_api.h" - -#include "doc_errno.h" -#include "document_store_manager.h" -#include "grd_base/grd_error.h" -#include "grd_type_inner.h" -#include "log_print.h" - -using namespace DocumentDB; - -int GRD_DBOpen(const char *dbPath, const char *configStr, unsigned int flags, GRD_DB **db) -{ - if (db == nullptr) { - return GRD_INVALID_ARGS; - } - std::string path = (dbPath == nullptr ? "" : dbPath); - std::string config = (configStr == nullptr ? "" : configStr); - DocumentStore *store = nullptr; - int ret = DocumentStoreManager::GetDocumentStore(path, config, flags, store); - if (ret != E_OK || store == nullptr) { - return TrasnferDocErr(ret); - } - - *db = new (std::nothrow) GRD_DB(); - if (*db == nullptr) { - (void)DocumentStoreManager::CloseDocumentStore(store, GRD_DB_CLOSE_IGNORE_ERROR); - store = nullptr; - ret = -E_OUT_OF_MEMORY; - } - - (*db)->store_ = store; - return TrasnferDocErr(ret); -} - -int GRD_DBClose(GRD_DB *db, unsigned int flags) -{ - if (db == nullptr || db->store_ == nullptr) { - return GRD_INVALID_ARGS; - } - - int ret = DocumentStoreManager::CloseDocumentStore(db->store_, flags); - if (ret != E_OK) { - return TrasnferDocErr(ret); - } - - db->store_ = nullptr; - delete db; - return GRD_OK; -} - -int GRD_Flush(GRD_DB *db, unsigned int flags) -{ - if (db == nullptr || db->store_ == nullptr) { - return GRD_INVALID_ARGS; - } - if (flags != GRD_DB_FLUSH_ASYNC && flags != GRD_DB_FLUSH_SYNC) { - return GRD_INVALID_ARGS; - } - return GRD_OK; -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp deleted file mode 100644 index f24c950f..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp +++ /dev/null @@ -1,153 +0,0 @@ -/* - * 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. - */ -#include "document_check.h" - -#include -#include - -#include "doc_errno.h" -#include "log_print.h" -#include "securec.h" - -namespace DocumentDB { -namespace { -constexpr const char *KEY_ID = "_id"; -constexpr const char *COLLECTION_PREFIX_GRD = "GRD_"; -constexpr const char *COLLECTION_PREFIX_GM_SYS = "GM_SYS_"; -const int MAX_COLLECTION_NAME = 511; -const int MAX_ID_LENS = 899; -const int JSON_DEEP_MAX = 4; - -bool CheckCollectionNamePrefix(const std::string &name, const std::string &prefix) -{ - if (name.length() < prefix.length()) { - return false; - } - - auto itPrefix = prefix.begin(); - auto itName = name.begin(); - while (itPrefix != prefix.end()) { - if (std::tolower(*itPrefix) != std::tolower(*itName)) { - return false; - } - itPrefix++; - itName++; - } - return true; -} -} // namespace - -bool CheckCommon::CheckCollectionName(const std::string &collectionName, std::string &lowerCaseName, int &errCode) -{ - if (collectionName.empty()) { - errCode = -E_INVALID_ARGS; - return false; - } - if (collectionName.length() > MAX_COLLECTION_NAME) { - errCode = -E_OVER_LIMIT; - return false; - } - if (CheckCollectionNamePrefix(collectionName, COLLECTION_PREFIX_GRD) || - CheckCollectionNamePrefix(collectionName, COLLECTION_PREFIX_GM_SYS)) { - GLOGE("Collection name is illegal"); - errCode = -E_INVALID_COLL_NAME_FORMAT; - return false; - } - lowerCaseName = collectionName; - std::transform(lowerCaseName.begin(), lowerCaseName.end(), lowerCaseName.begin(), [](unsigned char c) { - return std::tolower(c); - }); - return true; -} - -int CheckCommon::CheckFilter(JsonObject &filterObj) -{ - if (filterObj.GetDeep() > JSON_DEEP_MAX) { - GLOGE("filter's json deep is deeper than JSON_DEEP_MAX"); - return -E_INVALID_ARGS; - } - int ret = CheckIdFormat(filterObj); - if (ret != E_OK) { - GLOGE("Filter Id format is illegal"); - return ret; - } - if (!filterObj.GetChild().GetNext().IsNull()) { - return -E_INVALID_ARGS; - } - return E_OK; -} - -int CheckCommon::CheckIdFormat(JsonObject &filterJson) -{ - auto filterObjChild = filterJson.GetChild(); - auto idValue = JsonCommon::GetValueByFiled(filterObjChild, KEY_ID); - if (idValue.GetValueType() != ValueObject::ValueType::VALUE_STRING) { - return -E_INVALID_ARGS; - } - if (idValue.GetStringValue().length() > MAX_ID_LENS) { - return -E_OVER_LIMIT; - } - return E_OK; -} - -int CheckCommon::CheckDocument(JsonObject &documentObj) -{ - if (documentObj.GetDeep() > JSON_DEEP_MAX) { - GLOGE("documentObj's json deep is deeper than JSON_DEEP_MAX"); - return -E_INVALID_ARGS; - } - int ret = CheckIdFormat(documentObj); - if (ret != E_OK) { - GLOGE("Document Id format is illegal"); - return ret; - } - if (!documentObj.GetChild().IsNull()) { - auto documentObjChild = documentObj.GetChild(); - if (!JsonCommon::CheckJsonField(documentObjChild)) { - GLOGE("Document json field format is illegal"); - return -E_INVALID_ARGS; - } - } - return E_OK; -} - -bool CheckCommon::CheckProjection(JsonObject &projectionObj, std::vector> &path) -{ - if (projectionObj.GetDeep() > JSON_DEEP_MAX) { - GLOGE("projectionObj's json deep is deeper than JSON_DEEP_MAX"); - return -E_INVALID_ARGS; - } - if (!projectionObj.GetChild().IsNull()) { - auto projectionObjChild = projectionObj.GetChild(); - if (!JsonCommon::CheckProjectionField(projectionObjChild)) { - GLOGE("projection json field format is illegal"); - return false; - } - } - for (int i = 0; i < path.size(); i++) { - for (auto fieldName : path[i]) { - for (int i = 0; i < fieldName.size(); i++) { - if (!((isalpha(fieldName[i])) || (isdigit(fieldName[i])) || ('_' == fieldName[i]))) { - return false; - } - if (i == 0 && (isdigit(fieldName[i]))) { - return false; - } - } - } - } - return true; -} -} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h deleted file mode 100644 index 8b66a70d..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.h +++ /dev/null @@ -1,42 +0,0 @@ -/* -* 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 DOCUMENT_CHECK_H -#define DOCUMENT_CHECK_H - -#include -#include - -#include "json_common.h" - -namespace DocumentDB { -class JsonCommon; -class CheckCommon { -public: - CheckCommon() = default; - ~CheckCommon() = default; - - static bool CheckCollectionName(const std::string &collectionName, std::string &lowerCaseName, int &errCode); - static int CheckFilter(JsonObject &document); - static int CheckIdFormat(JsonObject &data); - static int CheckDocument(JsonObject &document); - static bool CheckProjection(JsonObject &projectionObj, std::vector> &path); -}; -using Key = std::vector; -using Value = std::vector; - -constexpr const char *COLL_PREFIX = "GRD_COLL_"; -} // namespace DocumentDB -#endif // DOCUMENT_CHECK_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_document_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_document_api.cpp deleted file mode 100644 index 8bcaa1ab..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_document_api.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/* -* 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. -*/ - -#include "grd_document/grd_document_api.h" - -#include "grd_base/grd_error.h" -#include "grd_resultset_inner.h" -#include "grd_type_inner.h" -#include "log_print.h" -using namespace DocumentDB; - -int GRD_CreateCollection(GRD_DB *db, const char *collectionName, const char *optionStr, unsigned int flags) -{ - if (db == nullptr || db->store_ == nullptr) { - return GRD_INVALID_ARGS; - } - - std::string name = (collectionName == nullptr ? "" : collectionName); - std::string option = (optionStr == nullptr ? "" : optionStr); - int ret = db->store_->CreateCollection(name, option, flags); - return TrasnferDocErr(ret); -} - -int GRD_DropCollection(GRD_DB *db, const char *collectionName, unsigned int flags) -{ - if (db == nullptr || db->store_ == nullptr) { - return GRD_INVALID_ARGS; - } - - std::string name = (collectionName == nullptr ? "" : collectionName); - int ret = db->store_->DropCollection(name, flags); - return TrasnferDocErr(ret); -} - -int GRD_UpdateDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *update, unsigned int flags) -{ - if (db == nullptr || db->store_ == nullptr) { - return GRD_INVALID_ARGS; - } - - std::string name = (collectionName == nullptr ? "" : collectionName); - std::string filterStr = (filter == nullptr ? "" : filter); - std::string updateStr = (update == nullptr ? "" : update); - int ret = db->store_->UpdateDocument(name, filterStr, updateStr, flags); - return TrasnferDocErr(ret); -} - -int GRD_UpsertDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *document, unsigned int flags) -{ - if (db == nullptr || db->store_ == nullptr) { - return GRD_INVALID_ARGS; - } - - std::string name = (collectionName == nullptr ? "" : collectionName); - std::string filterStr = (filter == nullptr ? "" : filter); - std::string documentStr = (document == nullptr ? "" : document); - int ret = db->store_->UpsertDocument(name, filterStr, documentStr, flags); - return TrasnferDocErr(ret); -} - -int GRD_InsertDoc(GRD_DB *db, const char *collectionName, const char *document, unsigned int flags) -{ - if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || document == nullptr) { - return GRD_INVALID_ARGS; - } - int ret = db->store_->InsertDocument(collectionName, document, flags); - return TrasnferDocErr(ret); -} - -int GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, unsigned int flags) -{ - if (db == nullptr || db->store_ == nullptr || filter == nullptr || collectionName == nullptr) { - return GRD_INVALID_ARGS; - } - int ret = db->store_->DeleteDocument(collectionName, filter, flags); - int errCode = TrasnferDocErr(ret); - int deleteCount = 0; - switch (errCode) { - case GRD_OK: - deleteCount = 1; - return deleteCount; - break; - case GRD_NO_DATA: - deleteCount = 0; - return deleteCount; - break; - } - return errCode; -} - -int GRD_FindDoc(GRD_DB *db, const char *collectionName, Query query, unsigned int flags, GRD_ResultSet **resultSet) -{ - if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || resultSet == nullptr || - query.filter == nullptr || query.projection == nullptr) { - return GRD_INVALID_ARGS; - } - GRD_ResultSet *grdResultSet = new (std::nothrow) GRD_ResultSet(); - if (grdResultSet == nullptr) { - GLOGE("Memory allocation failed!"); - return -E_FAILED_MEMORY_ALLOCATE; - } - int ret = db->store_->FindDocument(collectionName, query.filter, query.projection, flags, grdResultSet); - if (ret != E_OK) { - delete grdResultSet; - *resultSet = nullptr; - return TrasnferDocErr(ret); - } - *resultSet = grdResultSet; - return TrasnferDocErr(ret); -} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_resultset_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_resultset_api.cpp deleted file mode 100644 index e0b1c86a..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/grd_resultset_api.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* -* 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. -*/ -#include "grd_base/grd_resultset_api.h" - -#include - -#include "doc_errno.h" -#include "grd_base/grd_error.h" -#include "grd_resultset_inner.h" -#include "log_print.h" - -using namespace DocumentDB; - -int GRD_Next(GRD_ResultSet *resultSet) -{ - if (resultSet == nullptr) { - GLOGE("resultSet is nullptr"); - return GRD_INVALID_ARGS; - }; - std::mutex dbMutex; - std::lock_guard lock(dbMutex); - int ret = resultSet->resultSet_.GetNext(); - return TrasnferDocErr(ret); -} - -int GRD_GetValue(GRD_ResultSet *resultSet, char **value) -{ - if (resultSet == nullptr) { - GLOGE("resultSet is nullptr,cant get value from it"); - return GRD_INVALID_ARGS; - }; - char *val = nullptr; - int ret = resultSet->resultSet_.GetValue(&val); - if (val == nullptr) { - GLOGE("Value that get from resultSet is nullptr"); - return GRD_NOT_AVAILABLE; - } - *value = val; - return TrasnferDocErr(ret); -} - -int GRD_FreeValue(char *value) -{ - if (value == nullptr) { - return GRD_OK; - } - delete[] value; - return GRD_OK; -} - -int GRD_FreeResultSet(GRD_ResultSet *resultSet) -{ - if (resultSet == nullptr) { - return GRD_INVALID_ARGS; - } - resultSet->resultSet_.EraseCollection(); - delete resultSet; - return GRD_OK; -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_format_config.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_format_config.h deleted file mode 100644 index e03feb5d..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_format_config.h +++ /dev/null @@ -1,31 +0,0 @@ -/* -* 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 GRD_FORMAT_CONFIG_H -#define GRD_FORMAT_CONFIG_H - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -#define COLLECTION_LENS_MAX (512 * 1024) -#define JSON_LENS_MAX (512 * 1024) -#define JSON_DEEP_MAX (4) -#define KEY_ID ("_id") - -#ifdef __cplusplus -} -#endif // __cplusplus -#endif // GRD_FORMAT_CONFIG_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_resultset_inner.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_resultset_inner.h deleted file mode 100644 index d840ad79..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_resultset_inner.h +++ /dev/null @@ -1,26 +0,0 @@ -/* -* 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 GRD_RESULTSET_INNER_H -#define GRD_RESULTSET_INNER_H - -#include "doc_errno.h" -#include "grd_base/grd_error.h" -#include "result_set.h" - -typedef struct GRD_ResultSet { - DocumentDB::ResultSet resultSet_; -} GRD_ResultSet; -#endif // GRD_RESULTSET_INNER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_type_inner.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_type_inner.h deleted file mode 100644 index f26f6ac4..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/include/grd_type_inner.h +++ /dev/null @@ -1,27 +0,0 @@ -/* -* 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 GRD_TYPE_INNER_H -#define GRD_TYPE_INNER_H - -#include "doc_errno.h" -#include "document_store.h" -#include "grd_base/grd_error.h" - -typedef struct GRD_DB { - DocumentDB::DocumentStore *store_ = nullptr; -} GRD_DB; - -#endif // GRD_TYPE_INNER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h deleted file mode 100644 index 2fc0556e..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h +++ /dev/null @@ -1,44 +0,0 @@ -/* -* 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 COLLECTION_H -#define COLLECTION_H - -#include - -#include "document_check.h" -#include "kv_store_executor.h" - -namespace DocumentDB { -class Collection { -public: - Collection(const std::string &name, KvStoreExecutor *executor); - Collection(const Collection &a){}; - Collection(){}; - ~Collection(); - - int PutDocument(const Key &key, const Value &document); - int GetDocument(const Key &key, Value &document) const; - int DeleteDocument(const Key &key); - int UpsertDocument(const std::string &id, const std::string &document, bool isReplace = true); - bool FindDocument(); - int UpdateDocument(const std::string &id, const std::string &document); - -private: - std::string name_; - KvStoreExecutor *executor_ = nullptr; -}; -} // namespace DocumentDB -#endif // COLLECTION_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h deleted file mode 100644 index da91d40b..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h +++ /dev/null @@ -1,44 +0,0 @@ -/* -* 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 DOC_ERRNO_H -#define DOC_ERRNO_H - -namespace DocumentDB { -constexpr int E_OK = 0; -constexpr int E_BASE = 1000; -constexpr int E_ERROR = E_BASE + 1; -constexpr int E_INVALID_ARGS = E_BASE + 2; -constexpr int E_UNFINISHED = E_BASE + 7; -constexpr int E_OUT_OF_MEMORY = E_BASE + 8; -constexpr int E_SECUREC_ERROR = E_BASE + 9; -constexpr int E_SYSTEM_API_FAIL = E_BASE + 10; -constexpr int E_FILE_OPERATION = E_BASE + 11; -constexpr int E_OVER_LIMIT = E_BASE + 12; -constexpr int E_INVALID_CONFIG_VALUE = E_BASE + 13; -constexpr int E_NOT_FOUND = E_BASE + 14; -constexpr int E_COLLECTION_CONFLICT = E_BASE + 15; -constexpr int E_NO_DATA = E_BASE + 16; -constexpr int E_NOT_PERMIT = E_BASE + 17; -constexpr int E_DATA_CONFLICT = E_BASE + 18; -constexpr int E_INVALID_COLL_NAME_FORMAT = E_BASE + 18; -constexpr int E_INVALID_JSON_FORMAT = E_BASE + 40; -constexpr int E_JSON_PATH_NOT_EXISTS = E_BASE + 41; -constexpr int E_RESOURCE_BUSY = E_BASE + 50; -constexpr int E_FAILED_MEMORY_ALLOCATE = E_BASE + 51; - -int TrasnferDocErr(int err); -} // namespace DocumentDB -#endif // DOC_ERRNO_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store.h deleted file mode 100644 index 794035c6..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store.h +++ /dev/null @@ -1,53 +0,0 @@ -/* -* 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 DOCUMENT_STORE_H -#define DOCUMENT_STORE_H - -#include -#include -#include - -#include "collection.h" -#include "kv_store_executor.h" - -struct GRD_ResultSet; -namespace DocumentDB { -class DocumentStore { -public: - DocumentStore(KvStoreExecutor *); - ~DocumentStore(); - - int CreateCollection(const std::string &name, const std::string &option, int flags); - int DropCollection(const std::string &name, int flags); - - int UpdateDocument(const std::string &collection, const std::string &filter, const std::string &update, int flags); - int UpsertDocument( - const std::string &collection, const std::string &filter, const std::string &document, int flags); - int InsertDocument(const std::string &collection, const std::string &document, int flag); - int DeleteDocument(const std::string &collection, const std::string &filter, int flag); - int FindDocument(const std::string &collection, const std::string &filter, const std::string &projection, - int flags, GRD_ResultSet *grdResultSet); - KvStoreExecutor *GetExecutor(int errCode); - int EraseCollection(const std::string collectionName); - -private: - int GetViewType(JsonObject &jsonObj, bool &viewType); - std::mutex dbMutex_; - KvStoreExecutor *executor_ = nullptr; - std::map collections_; -}; -} // namespace DocumentDB -#endif // DOCUMENT_STORE_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store_manager.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store_manager.h deleted file mode 100644 index 90b70638..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/document_store_manager.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -* 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 DOCUMENT_STORE_MANAGER_H -#define DOCUMENT_STORE_MANAGER_H - -#include - -#include "document_store.h" - -namespace DocumentDB { -class DocumentStoreManager { -public: - static int GetDocumentStore( - const std::string &path, const std::string &config, unsigned int flags, DocumentStore *&store); - - static int CloseDocumentStore(DocumentStore *store, unsigned int flags); - -private: - static bool CheckDBPath(const std::string &path, std::string &canonicalPath, std::string &dbName, int &errCode); - static bool CheckDBConfig(const std::string &config, int &errCode); -}; -} // namespace DocumentDB -#endif // DOCUMENT_STORE_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/projection_tree.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/projection_tree.h deleted file mode 100644 index 2a5686bf..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/projection_tree.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef PROJECTION_TREE_H -#define PROJECTION_TREE_H - -#include -#include -#include - -#include "doc_errno.h" -#include "json_common.h" -#include "log_print.h" - -namespace DocumentDB { -struct ProjectionNode { - std::unordered_map SonNode; - bool isDeepest; - int Deep; - int ViewType; - ProjectionNode() - { - Deep = 0; - isDeepest = true; - } - int DeleteProjectionNode(); - ~ProjectionNode() - { - DeleteProjectionNode(); - } -}; -class ProjectionTree { -public: - ProjectionTree(); - ~ProjectionTree(); - - int ParseTree(std::vector> &path); - bool SearchTree(std::vector &singlePath, int &index); - -private: - ProjectionNode node_; -}; -} // namespace DocumentDB -#endif // PROJECTION_TREE_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set.h deleted file mode 100644 index 745bfde0..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set.h +++ /dev/null @@ -1,57 +0,0 @@ -/* -* 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 RESULTSET_H -#define RESULTSET_H - -#include - -#include "doc_errno.h" -#include "document_check.h" -#include "document_store.h" -#include "grd_base/grd_type_export.h" -#include "json_object.h" -#include "projection_tree.h" -#include "securec.h" -#include "vector" - -namespace DocumentDB { -class ResultSet { -public: - ResultSet(); - ~ResultSet(); - - int Init(DocumentStore *store, const std::string collectionName, ValueObject &key, - std::vector> &path, bool ifShowId, bool viewType); - int GetNext(); - int GetValue(char **value); - int EraseCollection(); - -private: - int CutJsonBranch(std::string &jsonData); - int CheckCutNode( - JsonObject *node, std::vector singleCutPath, std::vector> &allCutPath); - DocumentStore *store_ = nullptr; - std::string collectionName_; - ValueObject key_; - bool ifShowId_ = false; - bool viewType_ = false; - ProjectionTree projectionTree_; - std::vector> projectionPath_; - int index_ = 0; - std::vector findValue_; -}; -} // namespace DocumentDB -#endif //RESULTSET_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set_common.h deleted file mode 100644 index ac7890aa..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/result_set_common.h +++ /dev/null @@ -1,31 +0,0 @@ -/* -* 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 RESULTSET_COMMON_H -#define RESULTSET_COMMON_H - -#include - -#include "doc_errno.h" -#include "grd_base/grd_type_export.h" -#include "result_set.h" -#include "vector" - -namespace DocumentDB { -class ValueObject; -int InitResultSet(DocumentStore *store, const std::string collectionName, ValueObject &key, - std::vector> &path, bool ifShowId, bool viewType, ResultSet &resultSet); -} // namespace DocumentDB -#endif //RESULTSET_COMMON_H diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp deleted file mode 100644 index af65939b..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp +++ /dev/null @@ -1,177 +0,0 @@ -/* -* 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. -*/ - -#include "collection.h" - -#include - -#include "doc_errno.h" -#include "document_check.h" -#include "log_print.h" - -namespace DocumentDB { -Collection::Collection(const std::string &name, KvStoreExecutor *executor) : executor_(executor) -{ - std::string lowerCaseName = name; - std::transform(lowerCaseName.begin(), lowerCaseName.end(), lowerCaseName.begin(), [](unsigned char c) { - return std::tolower(c); - }); - name_ = COLL_PREFIX + lowerCaseName; -} - -Collection::~Collection() -{ - executor_ = nullptr; -} - -int Collection::PutDocument(const Key &key, const Value &document) -{ - if (executor_ == nullptr) { - return -E_INVALID_ARGS; - } - return executor_->PutData(name_, key, document); -} - -bool Collection::FindDocument() -{ - if (executor_ == nullptr) { - return -E_INVALID_ARGS; - } - int errCode = 0; - return executor_->IsCollectionExists(name_, errCode); -} - -int Collection::GetDocument(const Key &key, Value &document) const -{ - if (executor_ == nullptr) { - return -E_INVALID_ARGS; - } - return executor_->GetData(name_, key, document); -} - -int Collection::DeleteDocument(const Key &key) -{ - if (executor_ == nullptr) { - return -E_INVALID_ARGS; - } - return executor_->DelData(name_, key); -} - -int Collection::UpsertDocument(const std::string &id, const std::string &document, bool isReplace) -{ - if (executor_ == nullptr) { - return -E_INVALID_ARGS; - } - - int errCode = E_OK; - bool isCollExist = executor_->IsCollectionExists(name_, errCode); - if (errCode != E_OK) { - GLOGE("Check collection failed. %d", errCode); - return -errCode; - } - if (!isCollExist) { - GLOGE("Collection not created."); - return -E_NO_DATA; - } - - JsonObject upsertValue = JsonObject::Parse(document, errCode); - if (errCode != E_OK) { - GLOGD("Parse upsert value failed. %d", errCode); - return errCode; - } - - Key keyId(id.begin(), id.end()); - Value valSet(document.begin(), document.end()); - - if (!isReplace) { - Value valueGot; - errCode = executor_->GetData(name_, keyId, valueGot); - std::string valueGotStr = std::string(valueGot.begin(), valueGot.end()); - - if (errCode != E_OK && errCode != -E_NOT_FOUND) { - GLOGE("Get original document failed. %d", errCode); - return errCode; - } else if (errCode == E_OK) { // document has been inserted - GLOGD("Document has been inserted, append value."); - JsonObject originValue = JsonObject::Parse(valueGotStr, errCode); - if (errCode != E_OK) { - GLOGD("Parse original value failed. %d %s", errCode, valueGotStr.c_str()); - return errCode; - } - - errCode = JsonCommon::Append(originValue, upsertValue); - if (errCode != E_OK) { - GLOGD("Append value failed. %d", errCode); - return errCode; - } - - std::string valStr = originValue.Print(); - valSet = { valStr.begin(), valStr.end() }; - } - } - - return executor_->PutData(name_, keyId, valSet); -} - -int Collection::UpdateDocument(const std::string &id, const std::string &update) -{ - if (executor_ == nullptr) { - return -E_INVALID_ARGS; - } - - int errCode = E_OK; - bool isCollExist = executor_->IsCollectionExists(name_, errCode); - if (errCode != E_OK) { - GLOGE("Check collection failed. %d", errCode); - return -errCode; - } - if (!isCollExist) { - GLOGE("Collection not created."); - return -E_NO_DATA; - } - - JsonObject updateValue = JsonObject::Parse(update, errCode); - if (errCode != E_OK) { - GLOGD("Parse upsert value failed. %d", errCode); - return errCode; - } - - Key keyId(id.begin(), id.end()); - Value valueGot; - errCode = executor_->GetData(name_, keyId, valueGot); - std::string valueGotStr = std::string(valueGot.begin(), valueGot.end()); - if (errCode != E_OK) { - GLOGE("Get original document failed. %d", errCode); - return errCode; - } - - GLOGD("Update document value."); - JsonObject originValue = JsonObject::Parse(valueGotStr, errCode); - if (errCode != E_OK) { - GLOGD("Parse original value failed. %d %s", errCode, valueGotStr.c_str()); - return errCode; - } - - errCode = JsonCommon::Append(originValue, updateValue); - if (errCode != E_OK) { - GLOGD("Append value failed. %d", errCode); - return errCode; - } - - std::string valStr = originValue.Print(); - Value valSet(valStr.begin(), valStr.end()); - return executor_->PutData(name_, keyId, valSet); -} -} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp deleted file mode 100644 index af0e0ec7..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* -* 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. -*/ - -#include "doc_errno.h" - -#include "grd_base/grd_error.h" - -namespace DocumentDB { -int GetErrorCategory(int errCode) -{ - int categoryCode = errCode % 1000000; - categoryCode /= 1000; - categoryCode *= 1000; - return categoryCode; -} - -int TrasnferDocErr(int err) -{ - if (err > 0) { - return err; - } - - int outErr = GRD_OK; - switch (err) { - case E_OK: - return GRD_OK; - case -E_ERROR: - outErr = GRD_INNER_ERR; - break; - case -E_INVALID_ARGS: - outErr = GRD_INVALID_ARGS; - break; - case -E_FILE_OPERATION: - outErr = GRD_FAILED_FILE_OPERATION; - break; - case -E_OVER_LIMIT: - outErr = GRD_OVER_LIMIT; - break; - case -E_INVALID_JSON_FORMAT: - outErr = GRD_INVALID_JSON_FORMAT; - break; - case -E_INVALID_CONFIG_VALUE: - outErr = GRD_INVALID_CONFIG_VALUE; - break; - case -E_COLLECTION_CONFLICT: - outErr = GRD_COLLECTION_CONFLICT; - break; - case -E_NO_DATA: - case -E_NOT_FOUND: - outErr = GRD_NO_DATA; - break; - case -E_INVALID_COLL_NAME_FORMAT: - outErr = GRD_INVALID_COLLECTION_NAME; - break; - case -E_RESOURCE_BUSY: - outErr = GRD_RESOURCE_BUSY; - break; - case -E_FAILED_MEMORY_ALLOCATE: - outErr = GRD_FAILED_MEMORY_ALLOCATE; - break; - default: - outErr = GRD_INNER_ERR; - break; - } - - return GetErrorCategory(outErr); -} -} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp deleted file mode 100644 index 1e43ce53..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp +++ /dev/null @@ -1,448 +0,0 @@ -/* -* 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. -*/ - -#include "document_store.h" - -#include "collection_option.h" -#include "doc_errno.h" -#include "document_check.h" -#include "grd_base/grd_type_export.h" -#include "grd_resultset_inner.h" -#include "log_print.h" -#include "result_set_common.h" - -namespace DocumentDB { -const int JSON_LENS_MAX = 512 * 1024; -constexpr const char *KEY_ID = "_id"; -const bool caseIsSensitive = true; - -DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) {} - -DocumentStore::~DocumentStore() -{ - delete executor_; -} - -int DocumentStore::CreateCollection(const std::string &name, const std::string &option, int flags) -{ - std::string lowerCaseName; - int errCode = E_OK; - if (!CheckCommon::CheckCollectionName(name, lowerCaseName, errCode)) { - GLOGE("Check collection name invalid. %d", errCode); - return errCode; - } - - errCode = E_OK; - CollectionOption collOption = CollectionOption::ReadOption(option, errCode); - if (errCode != E_OK) { - GLOGE("Read collection option str failed. %d", errCode); - return errCode; - } - - if (flags != 0 && flags != CHK_EXIST_COLLECTION) { - GLOGE("Check flags invalid."); - return -E_INVALID_ARGS; - } - - std::lock_guard lock(dbMutex_); - bool ignoreExists = (flags != CHK_EXIST_COLLECTION); - errCode = executor_->CreateCollection(lowerCaseName, ignoreExists); - if (errCode != E_OK) { - GLOGE("Create collection failed. %d", errCode); - return errCode; - } - std::string oriOptStr; - errCode = executor_->GetCollectionOption(lowerCaseName, oriOptStr); - if (errCode == -E_NOT_FOUND) { - executor_->SetCollectionOption(lowerCaseName, collOption.ToString()); - errCode = E_OK; - } else { - CollectionOption oriOption = CollectionOption::ReadOption(oriOptStr, errCode); - if (collOption != oriOption) { - GLOGE("Create collection failed, option changed."); - return -E_INVALID_CONFIG_VALUE; - } - } - - return errCode; -} - -int DocumentStore::DropCollection(const std::string &name, int flags) -{ - std::string lowerCaseName; - int errCode = E_OK; - if (!CheckCommon::CheckCollectionName(name, lowerCaseName, errCode)) { - GLOGE("Check collection name invalid. %d", errCode); - return errCode; - } - - if (flags != 0 && flags != CHK_NON_EXIST_COLLECTION) { - GLOGE("Check flags invalid."); - return -E_INVALID_ARGS; - } - - bool ignoreNonExists = (flags != CHK_NON_EXIST_COLLECTION); - std::lock_guard lock(dbMutex_); - errCode = executor_->DropCollection(lowerCaseName, ignoreNonExists); - if (errCode != E_OK) { - GLOGE("Drop collection failed. %d", errCode); - return errCode; - } - - errCode = executor_->CleanCollectionOption(lowerCaseName); - if (errCode != E_OK && errCode != -E_NO_DATA) { - GLOGE("Clean collection option failed. %d", errCode); - return errCode; - } - - return E_OK; -} - -namespace { -bool CheckFilter(const std::string &filter, std::string &idStr, int &errCode) -{ - if (filter.empty()) { - errCode = -E_INVALID_ARGS; - GLOGE("Check filter invalid. %d", errCode); - return false; - } - - JsonObject filterObject = JsonObject::Parse(filter, errCode, true); - if (errCode != E_OK) { - GLOGE("Parse filter failed. %d", errCode); - return false; - } - - JsonObject filterId = filterObject.GetObjectItem("_id", errCode); - if (errCode != E_OK || filterId.GetItemValue().GetValueType() != ValueObject::ValueType::VALUE_STRING) { - GLOGE("Check filter '_id' not found or type not string."); - errCode = -E_INVALID_ARGS; - return false; - } - - idStr = filterId.GetItemValue().GetStringValue(); - return true; -} - -bool CheckDocument(const std::string &updateStr, int &errCode) -{ - if (updateStr.empty()) { - errCode = -E_INVALID_ARGS; - return false; - } - - JsonObject updateObj = JsonObject::Parse(updateStr, errCode); - if (updateObj.IsNull() || errCode != E_OK) { - GLOGE("Parse update document failed. %d", errCode); - return false; - } - - JsonObject filterId = updateObj.GetObjectItem("_id", errCode); - if (errCode != -E_NOT_FOUND) { - GLOGE("Can not change '_id' with update document failed."); - return false; - } - - return true; -} -} // namespace - -int DocumentStore::UpdateDocument( - const std::string &collection, const std::string &filter, const std::string &update, int flags) -{ - std::string lowerCaseCollName; - int errCode = E_OK; - if (!CheckCommon::CheckCollectionName(collection, lowerCaseCollName, errCode)) { - GLOGE("Check collection name invalid. %d", errCode); - return errCode; - } - - std::string idStr; - if (!CheckFilter(filter, idStr, errCode)) { - GLOGE("Check update filter failed. %d", errCode); - return errCode; - } - - if (!CheckDocument(update, errCode)) { - GLOGE("Check update document failed. %d", errCode); - return errCode; - } - - if (flags != 0) { - GLOGE("Check flags invalid."); - return -E_INVALID_ARGS; - } - - std::string docId(idStr.begin(), idStr.end()); - - std::lock_guard lock(dbMutex_); - auto coll = Collection(lowerCaseCollName, executor_); - errCode = coll.UpdateDocument(docId, update); - if (errCode == E_OK) { - errCode = 1; // update one record. - } - return errCode; -} - -int DocumentStore::UpsertDocument( - const std::string &collection, const std::string &filter, const std::string &document, int flags) -{ - std::string lowerCaseCollName; - int errCode = E_OK; - if (!CheckCommon::CheckCollectionName(collection, lowerCaseCollName, errCode)) { - GLOGE("Check collection name invalid. %d", errCode); - return errCode; - } - - std::string idStr; - if (!CheckFilter(filter, idStr, errCode)) { - GLOGE("Check upsert filter failed. %d", errCode); - return errCode; - } - - if (!CheckDocument(document, errCode)) { - GLOGE("Check upsert document failed. %d", errCode); - return errCode; - } - - if (flags != GRD_DOC_APPEND && flags != GRD_DOC_REPLACE) { - GLOGE("Check flags invalid."); - return -E_INVALID_ARGS; - } - - std::string docId(idStr.begin(), idStr.end()); - bool isReplace = ((flags & GRD_DOC_REPLACE) == GRD_DOC_REPLACE); - - std::lock_guard lock(dbMutex_); - auto coll = Collection(lowerCaseCollName, executor_); - errCode = coll.UpsertDocument(docId, document, isReplace); - if (errCode == E_OK) { - errCode = 1; // upsert one record. - } - return errCode; -} - -int DocumentStore::InsertDocument(const std::string &collection, const std::string &document, int flag) -{ - if (flag != 0) { - GLOGE("InsertDocument flag is not zero"); - return -E_INVALID_ARGS; - } - std::string lowerCaseCollName; - int errCode = E_OK; - if (!CheckCommon::CheckCollectionName(collection, lowerCaseCollName, errCode)) { - GLOGE("Check collection name invalid. %d", errCode); - return errCode; - } - auto coll = Collection(collection, executor_); - if (document.length() + 1 > JSON_LENS_MAX) { - GLOGE("document's length is larger than JSON_LENS_MAX"); - return -E_OVER_LIMIT; - } - JsonObject documentObj = JsonObject::Parse(document, errCode, caseIsSensitive); - if (errCode != E_OK) { - GLOGE("Document Parsed faild"); - return errCode; - } - errCode = CheckCommon::CheckDocument(documentObj); - if (errCode != E_OK) { - return errCode; - } - auto documentObjChild = documentObj.GetChild(); - auto idValue = JsonCommon::GetValueByFiled(documentObjChild, KEY_ID); - std::string id = idValue.GetStringValue(); - Key key(id.begin(), id.end()); - Value value(document.begin(), document.end()); - std::lock_guard lock(dbMutex_); - return coll.PutDocument(key, value); -} - -int DocumentStore::DeleteDocument(const std::string &collection, const std::string &filter, int flag) -{ - if (flag != 0) { - GLOGE("DeleteDocument flag is not zero"); - return -E_INVALID_ARGS; - } - std::string lowerCaseCollName; - int errCode = E_OK; - if (!CheckCommon::CheckCollectionName(collection, lowerCaseCollName, errCode)) { - GLOGE("Check collection name invalid. %d", errCode); - return errCode; - } - auto coll = Collection(collection, executor_); - if (filter.empty()) { - GLOGE("Filter is empty"); - return -E_INVALID_ARGS; - } - if (filter.length() + 1 > JSON_LENS_MAX) { - GLOGE("filter's length is larger than JSON_LENS_MAX"); - return -E_OVER_LIMIT; - } - JsonObject filterObj = JsonObject::Parse(filter, errCode, caseIsSensitive); - if (errCode != E_OK) { - GLOGE("filter Parsed faild"); - return errCode; - } - errCode = CheckCommon::CheckFilter(filterObj); - if (errCode != E_OK) { - return errCode; - } - auto filterObjChild = filterObj.GetChild(); - auto idValue = JsonCommon::GetValueByFiled(filterObjChild, KEY_ID); - std::string id = idValue.GetStringValue(); - Key key(id.begin(), id.end()); - std::lock_guard lock(dbMutex_); - return coll.DeleteDocument(key); -} -KvStoreExecutor *DocumentStore::GetExecutor(int errCode) -{ - return executor_; -} -int DocumentStore::FindDocument(const std::string &collection, const std::string &filter, - const std::string &projection, int flags, GRD_ResultSet *grdResultSet) -{ - if (flags != 0 && flags != GRD_DOC_ID_DISPLAY) { - GLOGE("FindDocument flag is illegal"); - return -E_INVALID_ARGS; - ; - } - std::string lowerCaseCollName; - int errCode = E_OK; - if (!CheckCommon::CheckCollectionName(collection, lowerCaseCollName, errCode)) { - GLOGE("Check collection name invalid. %d", errCode); - return errCode; - } - if (filter.length() + 1 > JSON_LENS_MAX) { - GLOGE("filter's length is larger than JSON_LENS_MAX"); - return -E_OVER_LIMIT; - } - JsonObject filterObj = JsonObject::Parse(filter, errCode, caseIsSensitive); - if (errCode != E_OK) { - GLOGE("filter Parsed faild"); - return errCode; - } - errCode = CheckCommon::CheckFilter(filterObj); - if (errCode != E_OK) { - return errCode; - } - auto filterObjChild = filterObj.GetChild(); - auto idValue = JsonCommon::GetValueByFiled(filterObjChild, KEY_ID); - if (projection.length() + 1 > JSON_LENS_MAX) { - GLOGE("projection's length is larger than JSON_LENS_MAX"); - return -E_OVER_LIMIT; - } - JsonObject projectionObj = JsonObject::Parse(projection, errCode, caseIsSensitive); - if (errCode != E_OK) { - GLOGE("projection Parsed faild"); - return errCode; - } - bool viewType = false; - std::vector> allPath; - if (projection != "{}") { - allPath = JsonCommon::ParsePath(projectionObj); - if (!CheckCommon::CheckProjection(projectionObj, allPath)) { - GLOGE("projection format unvalid"); - return -E_INVALID_ARGS; - } - if (GetViewType(projectionObj, viewType) != E_OK) { - GLOGE("GetViewType faild"); - return -E_INVALID_ARGS; - } - } - bool ifShowId = false; - if (flags == GRD_DOC_ID_DISPLAY) { - ifShowId = true; - } - if (collections_.find(collection) != collections_.end()) { - GLOGE("DB is resource busy"); - return -E_RESOURCE_BUSY; - } - auto coll = Collection(collection, executor_); - std::lock_guard lock(dbMutex_); - if (!coll.FindDocument()) { - GLOGE("no corresponding table name"); - return -E_INVALID_ARGS; - } - int ret = InitResultSet(this, collection, idValue, allPath, ifShowId, viewType, grdResultSet->resultSet_); - if (ret == E_OK) { - collections_[collection] = nullptr; - } - if (ret != E_OK) { - collections_.erase(collection); - } - return ret; -} - -int DocumentStore::EraseCollection(const std::string collectionName) -{ - if (collections_.find(collectionName) != collections_.end()) { - collections_.erase(collectionName); - return E_OK; - } - GLOGE("erase collection failed"); - return E_OK; -} - -int DocumentStore::GetViewType(JsonObject &jsonObj, bool &viewType) -{ - auto leafValue = JsonCommon::GetLeafValue(jsonObj); - if (leafValue.size() == 0) { - return E_INVALID_ARGS; - } - for (size_t i = 0; i < leafValue.size(); i++) { - switch (leafValue[i].GetValueType()) { - case ValueObject::ValueType::VALUE_BOOL: - if (leafValue[i].GetBoolValue()) { - if (i != 0 && !viewType) { - return -E_INVALID_ARGS; - } - viewType = true; - } else { - if (i != 0 && viewType) { - return E_INVALID_ARGS; - } - viewType = false; - } - break; - case ValueObject::ValueType::VALUE_STRING: - if (leafValue[i].GetStringValue() == "") { - if (i != 0 && !viewType) { - return -E_INVALID_ARGS; - } - viewType = true; - } else { - return -E_INVALID_ARGS; - } - break; - case ValueObject::ValueType::VALUE_NUMBER: - if (leafValue[i].GetIntValue() == 0) { - if (i != 0 && viewType) { - return -E_INVALID_ARGS; - } - viewType = false; - } else { - if (i != 0 && !viewType) { - return E_INVALID_ARGS; - } - viewType = true; - } - break; - default: - return E_INVALID_ARGS; - } - } - return E_OK; -} -} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store_manager.cpp deleted file mode 100644 index 28906838..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store_manager.cpp +++ /dev/null @@ -1,143 +0,0 @@ -/* -* 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. -*/ - -#include "document_store_manager.h" - -#include "db_config.h" -#include "doc_errno.h" -#include "grd_base/grd_type_export.h" -#include "kv_store_manager.h" -#include "log_print.h" -#include "os_api.h" - -namespace DocumentDB { -namespace { -bool CheckDBOpenFlag(unsigned int flag) -{ - unsigned int mask = ~(GRD_DB_OPEN_CREATE | GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK); - unsigned int invalidOpt = (GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK); - return ((flag & mask) == 0x00) && ((flag & invalidOpt) != invalidOpt); -} - -bool CheckDBCloseFlag(unsigned int flag) -{ - return (flag == GRD_DB_CLOSE) || (flag == GRD_DB_CLOSE_IGNORE_ERROR); -} - -bool CheckDBCreate(unsigned int flags, const std::string &path) -{ - if ((flags & GRD_DB_OPEN_CREATE) == 0 && !OSAPI::CheckPathExistence(path)) { - return false; - } - return true; -} -} // namespace - -int DocumentStoreManager::GetDocumentStore( - const std::string &path, const std::string &config, unsigned int flags, DocumentStore *&store) -{ - std::string canonicalPath; - std::string dbName; - int errCode = E_OK; - if (!CheckDBPath(path, canonicalPath, dbName, errCode)) { - GLOGE("Check document db file path failed."); - return errCode; - } - - DBConfig dbConfig = DBConfig::ReadConfig(config, errCode); - if (errCode != E_OK) { - GLOGE("Read db config str failed. %d", errCode); - return errCode; - } - - if (!CheckDBOpenFlag(flags)) { - GLOGE("Check document db open flags failed."); - return -E_INVALID_ARGS; - } - - if (!CheckDBCreate(flags, path)) { - GLOGE("Open db failed, file no exists."); - return -E_INVALID_ARGS; - } - - KvStoreExecutor *executor = nullptr; - errCode = KvStoreManager::GetKvStore(canonicalPath + "/" + dbName, dbConfig, executor); - if (errCode != E_OK) { - GLOGE("Open document store failed. %d", errCode); - return errCode; - } - - store = new (std::nothrow) DocumentStore(executor); - if (store == nullptr) { - GLOGE("Memory allocation failed!"); - return -E_FAILED_MEMORY_ALLOCATE; - } - if (store == nullptr) { - return -E_OUT_OF_MEMORY; - } - - return errCode; -} - -int DocumentStoreManager::CloseDocumentStore(DocumentStore *store, unsigned int flags) -{ - if (!CheckDBCloseFlag(flags)) { - GLOGE("Check document db close flags failed."); - return -E_INVALID_ARGS; - } - - delete store; - return E_OK; -} - -bool DocumentStoreManager::CheckDBPath( - const std::string &path, std::string &canonicalPath, std::string &dbName, int &errCode) -{ - if (path.empty()) { - GLOGE("Invalid path empty"); - errCode = -E_INVALID_ARGS; - return false; - } - - if (path.back() == '/') { - GLOGE("Invalid path end with slash"); - errCode = -E_INVALID_ARGS; - return false; - } - - std::string dirPath; - OSAPI::SplitFilePath(path, dirPath, dbName); - - int innerErrCode = OSAPI::GetRealPath(dirPath, canonicalPath); - if (innerErrCode != E_OK) { - GLOGE("Get real path failed. %d", errCode); - errCode = -E_FILE_OPERATION; - return false; - } - - if (!OSAPI::CheckPermission(canonicalPath)) { - GLOGE("Check path permission failed. %d", errCode); - errCode = -E_FILE_OPERATION; - return false; - } - - return true; -} - -bool DocumentStoreManager::CheckDBConfig(const std::string &config, int &errCode) -{ - return true; -} -} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/projection_tree.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/projection_tree.cpp deleted file mode 100644 index cba39d51..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/projection_tree.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "projection_tree.h" - -#include - -namespace DocumentDB { -const int JSON_DEEP_MAX = 4; - -ProjectionTree::ProjectionTree() {} - -ProjectionTree::~ProjectionTree() {} - -int ProjectionTree::ParseTree(std::vector> &path) -{ - ProjectionNode *node = &node_; - if (node == NULL) { - return E_OK; - } - for (int i = 0; i < path.size(); i++) { - node = &node_; - for (int j = 0; j < path[i].size(); j++) { - if (node->SonNode[path[i][j]] != nullptr) { - node = node->SonNode[path[i][j]]; - if (j < path[i].size() - 1 && node->isDeepest) { - return -E_INVALID_ARGS; - } - if (j == path[i].size() - 1 && !node->isDeepest) { - return -E_INVALID_ARGS; - } - } else { - auto tempNode = new (std::nothrow) ProjectionNode; - if (tempNode == nullptr) { - GLOGE("Memory allocation failed!"); - return -E_FAILED_MEMORY_ALLOCATE; - } - tempNode->Deep = node->Deep + 1; - if (tempNode->Deep > JSON_DEEP_MAX) { - delete tempNode; - return -E_INVALID_ARGS; - } - node->isDeepest = false; - node->SonNode[path[i][j]] = tempNode; - node = node->SonNode[path[i][j]]; - } - } - } - return E_OK; -} - -bool ProjectionTree::SearchTree(std::vector &singlePath, int &index) -{ - ProjectionNode *node = &node_; - for (int i = 0; i < singlePath.size(); i++) { - if (node->isDeepest) { - index = i; - } - if (node->SonNode[singlePath[i]] != nullptr) { - node = node->SonNode[singlePath[i]]; - } else { - return false; - } - } - return true; -} - -int ProjectionNode::DeleteProjectionNode() -{ - for (auto item : SonNode) { - if (item.second != nullptr) { - delete item.second; - item.second = nullptr; - } - } - return E_OK; -} -} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp deleted file mode 100644 index 5c86d381..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set.cpp +++ /dev/null @@ -1,159 +0,0 @@ -/* -* 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. -*/ -#include "result_set.h" - -#include "log_print.h" - -namespace DocumentDB { -constexpr const char *KEY_ID = "_id"; - -ResultSet::ResultSet() {} -ResultSet::~ResultSet() {} -int ResultSet::EraseCollection() -{ - if (store_ != nullptr) { - store_->EraseCollection(collectionName_); - } - return E_OK; -} -int ResultSet::Init(DocumentStore *store, const std::string collectionName, ValueObject &key, - std::vector> &path, bool ifShowId, bool viewType) -{ - store_ = store; - collectionName_ = collectionName; - key_ = key; - projectionPath_ = path; - if (projectionTree_.ParseTree(path) == -E_INVALID_ARGS) { - GLOGE("Parse ProjectionTree failed"); - return -E_INVALID_ARGS; - } - ifShowId_ = ifShowId; - viewType_ = viewType; - findValue_.reserve(1 + 1); - return E_OK; -} - -int ResultSet::GetNext() -{ - index_++; - if (index_ != 1) { - if (findValue_.size() != 0) { - findValue_.pop_back(); - } - return -E_NO_DATA; - } - std::string idValue = key_.GetStringValue(); - if (idValue.empty()) { - GLOGE("id is empty"); - return -E_NO_DATA; - } - Key key(idValue.begin(), idValue.end()); - Value document; - int errCode = 0; - auto coll = Collection(collectionName_, store_->GetExecutor(errCode)); - errCode = coll.GetDocument(key, document); - if (errCode == -E_NOT_FOUND) { - GLOGE("Cant get value from db"); - return -E_NO_DATA; - } - std::string jsonData(document.begin(), document.end()); - CutJsonBranch(jsonData); - findValue_.emplace_back(jsonData); - return E_OK; -} - -int ResultSet::GetValue(char **value) -{ - if (findValue_.size() == 0) { - GLOGE("The value vector in resultSet is empty"); - return -E_NO_DATA; - } - auto jsonData = findValue_.back(); - char *jsonstr = new char[jsonData.size() + 1]; - if (jsonstr == nullptr) { - GLOGE("Memory allocation failed!"); - return -E_FAILED_MEMORY_ALLOCATE; - } - errno_t err = strcpy_s(jsonstr, jsonData.size() + 1, jsonData.c_str()); - if (err != 0) { - GLOGE("strcpy_s failed"); - delete[] jsonstr; - return -E_NO_DATA; - ; - } - *value = jsonstr; - return E_OK; -} - -int ResultSet::CheckCutNode( - JsonObject *node, std::vector singlePath, std::vector> &allCutPath) -{ - if (node == nullptr) { - GLOGE("No node to cut"); - return -E_NO_DATA; - } - singlePath.emplace_back(node->GetItemFiled()); - int index = 0; - if (!projectionTree_.SearchTree(singlePath, index) && index == 0) { - allCutPath.emplace_back(singlePath); - } - if (!node->GetChild().IsNull()) { - auto nodeNew = node->GetChild(); - CheckCutNode(&nodeNew, singlePath, allCutPath); - } - if (!node->GetNext().IsNull()) { - singlePath.pop_back(); - auto nodeNew = node->GetNext(); - CheckCutNode(&nodeNew, singlePath, allCutPath); - } - return E_OK; -} -int ResultSet::CutJsonBranch(std::string &jsonData) -{ - int errCode; - JsonObject cjsonObj = JsonObject::Parse(jsonData, errCode, true); - if (errCode != E_OK) { - GLOGE("jsonData Parsed faild"); - return errCode; - } - std::vector> allCutPath; - if (viewType_) { - std::vector singlePath; - auto cjsonObjChild = cjsonObj.GetChild(); - errCode = CheckCutNode(&cjsonObjChild, singlePath, allCutPath); - if (errCode != E_OK) { - GLOGE("The node in CheckCutNode is nullptr"); - return errCode; - } - for (size_t i = 0; i < allCutPath.size(); i++) { - if (!ifShowId_ || allCutPath[i][0] != KEY_ID) { - cjsonObj.DeleteItemDeeplyOnTarget(allCutPath[i]); - } - } - } - if (!viewType_) { - for (size_t i = 0; i < projectionPath_.size(); i++) { - cjsonObj.DeleteItemDeeplyOnTarget(projectionPath_[i]); - } - if (!ifShowId_) { - std::vector idPath; - idPath.emplace_back(KEY_ID); - cjsonObj.DeleteItemDeeplyOnTarget(idPath); - } - } - jsonData = cjsonObj.Print(); - return E_OK; -} -} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set_common.cpp deleted file mode 100644 index 99399efe..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/result_set_common.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* -* 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. -*/ - -#include "result_set_common.h" - -#include -#include - -#include "doc_errno.h" -#include "grd_base/grd_error.h" - -namespace DocumentDB { -class ValueObject; -int InitResultSet(DocumentStore *store, const std::string collectionName, ValueObject &key, - std::vector> &path, bool ifShowId, bool viewType, ResultSet &resultSet) -{ - return resultSet.Init(store, collectionName, key, path, ifShowId, viewType); -} -} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h deleted file mode 100644 index 2a92b01d..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/json_object.h +++ /dev/null @@ -1,114 +0,0 @@ -/* -* 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 JSON_OBJECT_H -#define JSON_OBJECT_H - -#include -#include -#include -#include - -#include "cJSON.h" - -namespace DocumentDB { -class ValueObject { -public: - enum class ValueType - { - VALUE_NULL = 0, - VALUE_BOOL, - VALUE_NUMBER, - VALUE_STRING, - }; - - ValueObject() = default; - explicit ValueObject(bool val); - explicit ValueObject(double val); - explicit ValueObject(const char *val); - explicit ValueObject(const std::string &val); - - ValueType GetValueType() const; - bool GetBoolValue() const; - int64_t GetIntValue() const; - double GetDoubleValue() const; - std::string GetStringValue() const; - -private: - ValueType valueType = ValueType::VALUE_NULL; - union { - bool boolValue; - double doubleValue; - }; - std::string stringValue; -}; -using JsonFieldPath = std::vector; - -using ResultValue = ValueObject; -using JsonFieldPath = std::vector; - -class JsonObject { -public: - static JsonObject Parse(const std::string &jsonStr, int &errCode, bool caseSensitive = false); - - ~JsonObject(); - - std::string Print() const; - - JsonObject GetObjectItem(const std::string &field, int &errCode); - JsonObject GetArrayItem(int index, int &errCode); - - JsonObject GetNext() const; - JsonObject GetChild() const; - - int DeleteItemFromObject(const std::string &field); - int AddItemToObject(const JsonObject &item); - int AddItemToObject(const std::string &fieldName, const JsonObject &item); - - ValueObject GetItemValue() const; - void SetItemValue(const ValueObject &value) const; - - std::string GetItemFiled() const; - std::string GetItemFiled(int &errCode) const; - - bool IsFieldExists(const JsonFieldPath &jsonPath) const; - JsonObject FindItem(const JsonFieldPath &jsonPath, int &errCode) const; - ValueObject GetObjectByPath(const JsonFieldPath &jsonPath, int &errCode) const; - int DeleteItemOnTarget(const JsonFieldPath &path); - int DeleteItemDeeplyOnTarget(const JsonFieldPath &path); - bool IsNull() const; - int GetDeep(); - enum class Type - { - JSON_LEAF, - JSON_OBJECT, - JSON_ARRAY - }; - Type GetType() const; - -private: - JsonObject(); - int Init(const std::string &str); - - int GetDeep(cJSON *cjson); - int CheckNumber(cJSON *cjson, int &errCode); - - cJSON *cjson_ = nullptr; - int jsonDeep_ = 0; - bool isOwner_ = false; - bool caseSensitive_ = false; -}; -} // namespace DocumentDB -#endif // JSON_OBJECT_H diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h deleted file mode 100644 index 59299f7b..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h +++ /dev/null @@ -1,41 +0,0 @@ -/* -* 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 KV_STORE_EXECUTOR_H -#define KV_STORE_EXECUTOR_H - -#include - -#include "document_check.h" - -namespace DocumentDB { -class KvStoreExecutor { -public: - virtual ~KvStoreExecutor() = default; - - virtual int PutData(const std::string &collName, const Key &key, const Value &value) = 0; - virtual int GetData(const std::string &collName, const Key &key, Value &value) const = 0; - virtual int DelData(const std::string &collName, const Key &key) = 0; - - virtual int CreateCollection(const std::string &name, bool ignoreExists) = 0; - virtual int DropCollection(const std::string &name, bool ignoreNonExists) = 0; - virtual bool IsCollectionExists(const std::string &name, int &errCode) = 0; - - virtual int GetCollectionOption(const std::string &name, std::string &option) = 0; - virtual int SetCollectionOption(const std::string &name, const std::string &option) = 0; - virtual int CleanCollectionOption(const std::string &name) = 0; -}; -} // namespace DocumentDB -#endif // KV_STORE_EXECUTOR_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_manager.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_manager.h deleted file mode 100644 index 2a84cf89..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_manager.h +++ /dev/null @@ -1,30 +0,0 @@ -/* -* 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 KV_STORE_MANAGER_H -#define KV_STORE_MANAGER_H -#include - -#include "db_config.h" -#include "document_check.h" -#include "kv_store_executor.h" - -namespace DocumentDB { -class KvStoreManager { -public: - static int GetKvStore(const std::string &path, const DBConfig &config, KvStoreExecutor *&executor); -}; -} // namespace DocumentDB -#endif // KV_STORE_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp deleted file mode 100644 index 56412228..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp +++ /dev/null @@ -1,539 +0,0 @@ -/* -* 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. -*/ - -#include "json_object.h" - -#include - -#include "doc_errno.h" -#include "log_print.h" - -namespace DocumentDB { - -namespace { -bool IsNumber(const std::string &str) -{ - return std::all_of(str.begin(), str.end(), [](char c) { - return std::isdigit(c); - }); -} -} // namespace - -ValueObject::ValueObject(bool val) -{ - valueType = ValueType::VALUE_BOOL; - boolValue = val; -} - -ValueObject::ValueObject(double val) -{ - valueType = ValueType::VALUE_NUMBER; - doubleValue = val; -} - -ValueObject::ValueObject(const char *val) -{ - valueType = ValueType::VALUE_STRING; - stringValue = val; -} - -ValueObject::ValueObject(const std::string &val) -{ - valueType = ValueType::VALUE_STRING; - stringValue = val; -} - -ValueObject::ValueType ValueObject::GetValueType() const -{ - return valueType; -} - -bool ValueObject::GetBoolValue() const -{ - return boolValue; -} - -int64_t ValueObject::GetIntValue() const -{ - return static_cast(doubleValue + 0.5); -} - -double ValueObject::GetDoubleValue() const -{ - return doubleValue; -} - -std::string ValueObject::GetStringValue() const -{ - return stringValue; -} - -JsonObject JsonObject::Parse(const std::string &jsonStr, int &errCode, bool caseSensitive) -{ - JsonObject obj; - errCode = obj.Init(jsonStr); - obj.caseSensitive_ = caseSensitive; - return obj; -} - -JsonObject::JsonObject() -{ - cjson_ = nullptr; -} - -JsonObject::~JsonObject() -{ - if (isOwner_) { - cJSON_Delete(cjson_); - } -} - -bool JsonObject::IsNull() const -{ - if (cjson_ == nullptr) { - return true; - } - return false; -} - -JsonObject::Type JsonObject::GetType() const -{ - if (cjson_->type == cJSON_Object) { - return JsonObject::Type::JSON_OBJECT; - } else if (cjson_->type == cJSON_Array) { - return JsonObject::Type::JSON_ARRAY; - } - return JsonObject::Type::JSON_LEAF; -} -int JsonObject::GetDeep() -{ - if (cjson_ == nullptr) { - GLOGE("cJson is nullptr,deep is 0"); - return 0; - } - if (jsonDeep_ != 0) { - return jsonDeep_; - } - jsonDeep_ = GetDeep(cjson_); - return jsonDeep_; -} -int JsonObject::GetDeep(cJSON *cjson) -{ - if (cjson->child == nullptr) { - jsonDeep_ = 0; - return 0; // leaf node - } - - int depth = -1; - cJSON *child = cjson->child; - while (child != nullptr) { - depth = std::max(depth, GetDeep(child) + 1); - child = child->next; - } - jsonDeep_ = depth; - return depth; -} - -int JsonObject::CheckNumber(cJSON *item, int &errCode) -{ - if (item != NULL && cJSON_IsNumber(item)) { - double value = cJSON_GetNumberValue(item); - if (value > __DBL_MAX__ || value < -__DBL_MAX__) { - errCode = E_INVALID_ARGS; - } - } - if (item->child != nullptr) { - return CheckNumber(item->child, errCode); - } - if (item->next != nullptr) { - return CheckNumber(item->next, errCode); - } - return E_OK; -} - -int JsonObject::Init(const std::string &str) -{ - const char *end = NULL; - isOwner_ = true; - cjson_ = cJSON_ParseWithOpts(str.c_str(), &end, true); - if (cjson_ == nullptr) { - GLOGE("Json's format is wrong"); - return -E_INVALID_JSON_FORMAT; - } - - if (cjson_->type != cJSON_Object) { - GLOGE("after Parse,cjson_'s type is not cJSON_Object"); - return -E_INVALID_ARGS; - } - - int ret = 0; - CheckNumber(cjson_, ret); - if (ret == E_INVALID_ARGS) { - GLOGE("Int value is larger than double"); - return -E_INVALID_ARGS; - } - return E_OK; -} - -std::string JsonObject::Print() const -{ - if (cjson_ == nullptr) { - return ""; - } - char *ret = cJSON_PrintUnformatted(cjson_); - std::string str = ret; - cJSON_free(ret); - return str; -} - -JsonObject JsonObject::GetObjectItem(const std::string &field, int &errCode) -{ - if (cjson_ == nullptr || (cjson_->type & cJSON_Object) != cJSON_Object) { - errCode = -E_INVALID_ARGS; - return JsonObject(); - } - - JsonObject item; - item.caseSensitive_ = caseSensitive_; - if (caseSensitive_) { - item.cjson_ = cJSON_GetObjectItemCaseSensitive(cjson_, field.c_str()); - } else { - item.cjson_ = cJSON_GetObjectItem(cjson_, field.c_str()); - } - if (item.cjson_ == nullptr) { - errCode = -E_NOT_FOUND; - } - return item; -} - -JsonObject JsonObject::GetArrayItem(int index, int &errCode) -{ - if (cjson_ == nullptr || (cjson_->type & cJSON_Array) != cJSON_Array) { - errCode = -E_INVALID_ARGS; - return JsonObject(); - } - - JsonObject item; - item.caseSensitive_ = caseSensitive_; - item.cjson_ = cJSON_GetArrayItem(cjson_, index); - if (item.cjson_ == nullptr) { - errCode = -E_NOT_FOUND; - } - return item; -} - -JsonObject JsonObject::GetNext() const -{ - if (cjson_ == nullptr) { - return JsonObject(); - } - JsonObject next; - next.caseSensitive_ = caseSensitive_; - if (cjson_->next == nullptr) { - return JsonObject(); - } - next.cjson_ = cjson_->next; - return next; -} - -JsonObject JsonObject::GetChild() const -{ - if (cjson_ == nullptr) { - return JsonObject(); - } - JsonObject child; - child.caseSensitive_ = caseSensitive_; - if (cjson_->child == nullptr) { - return JsonObject(); - } - child.cjson_ = cjson_->child; - return child; -} - -int JsonObject::DeleteItemFromObject(const std::string &field) -{ - if (field.empty()) { - return E_OK; - } - cJSON_DeleteItemFromObjectCaseSensitive(cjson_, field.c_str()); - return E_OK; -} - -int JsonObject::AddItemToObject(const JsonObject &item) -{ - if (item.IsNull()) { - GLOGD("Add null object."); - return E_OK; - } - - cJSON *cpoyItem = cJSON_Duplicate(item.cjson_, true); - cJSON_AddItemToObject(cjson_, item.GetItemFiled().c_str(), cpoyItem); - return E_OK; -} - -int JsonObject::AddItemToObject(const std::string &fieldName, const JsonObject &item) -{ - if (item.IsNull()) { - GLOGD("Add null object."); - return E_OK; - } - // TODO: check item exist - if (cjson_->type == cJSON_Array) { - int n = 0; - cJSON *child = cjson_->child; - while (child != nullptr) { - child = child->next; - n++; - } - if (IsNumber(fieldName) && n <= std::stoi(fieldName)) { - GLOGE("Add item object to array over size."); - return -E_DATA_CONFLICT; - } - } - cJSON *cpoyItem = cJSON_Duplicate(item.cjson_, true); - cJSON_AddItemToObject(cjson_, fieldName.c_str(), cpoyItem); - return E_OK; -} - -ValueObject JsonObject::GetItemValue() const -{ - if (cjson_ == nullptr) { - return ValueObject(); - } - - ValueObject value; - switch (cjson_->type) { - case cJSON_False: - case cJSON_True: - return ValueObject(cjson_->type == cJSON_True); - case cJSON_NULL: - return ValueObject(); - case cJSON_Number: - return ValueObject(cjson_->valuedouble); - case cJSON_String: - return ValueObject(cjson_->valuestring); - case cJSON_Array: - case cJSON_Object: - default: - GLOGW("Invalid json type: %d", cjson_->type); - break; - } - - return value; -} - -void JsonObject::SetItemValue(const ValueObject &value) const -{ - if (cjson_ == nullptr) { - return; - } - switch (value.GetValueType()) { - case ValueObject::ValueType::VALUE_NUMBER: - cJSON_SetNumberValue(cjson_, value.GetDoubleValue()); - break; - case ValueObject::ValueType::VALUE_STRING: - cJSON_SetValuestring(cjson_, value.GetStringValue().c_str()); - break; - default: - break; - } -} - -std::string JsonObject::GetItemFiled() const -{ - if (cjson_ == nullptr) { - return ""; - } - - if (cjson_->string == nullptr) { - cJSON *tail = cjson_; - while (tail->next != nullptr) { - tail = tail->next; - } - - int index = 0; - cJSON *head = cjson_; - while (head->prev != tail) { - head = head->prev; - index++; - } - return std::to_string(index); - } else { - return cjson_->string; - } -} - -std::string JsonObject::GetItemFiled(int &errCode) const -{ - if (cjson_ == nullptr) { - errCode = E_INVALID_ARGS; - return ""; - } - if (cjson_->string == nullptr) { - errCode = E_INVALID_ARGS; - return ""; - } - errCode = E_OK; - return cjson_->string; -} - -cJSON *GetChild(cJSON *cjson, const std::string &field, bool caseSens) -{ - if (cjson->type == cJSON_Object) { - if (caseSens) { - return cJSON_GetObjectItemCaseSensitive(cjson, field.c_str()); - } else { - return cJSON_GetObjectItem(cjson, field.c_str()); - } - } else if (cjson->type == cJSON_Array) { - if (!IsNumber(field)) { - GLOGW("Invalid json field path, expect array index."); - return nullptr; - } - return cJSON_GetArrayItem(cjson, std::stoi(field)); - } - - GLOGW("Invalid json field type, expect object or array."); - return nullptr; -} - -cJSON *MoveToPath(cJSON *cjson, const JsonFieldPath &jsonPath, bool caseSens) -{ - for (const auto &field : jsonPath) { - cjson = GetChild(cjson, field, caseSens); - if (cjson == nullptr) { - break; - } - } - return cjson; -} - -bool JsonObject::IsFieldExists(const JsonFieldPath &jsonPath) const -{ - return (MoveToPath(cjson_, jsonPath, caseSensitive_) != nullptr); -} - -JsonObject JsonObject::FindItem(const JsonFieldPath &jsonPath, int &errCode) const -{ - if (jsonPath.empty()) { - JsonObject curr = JsonObject(); - curr.cjson_ = cjson_; - curr.caseSensitive_ = caseSensitive_; - curr.isOwner_ = false; - GLOGW("Path empty, return current object"); - return curr; - } - - cJSON *findItem = MoveToPath(cjson_, jsonPath, caseSensitive_); - if (findItem == nullptr) { - GLOGE("Find item failed. json field path not found."); - errCode = -E_JSON_PATH_NOT_EXISTS; - return {}; - } - - JsonObject item; - item.caseSensitive_ = caseSensitive_; - item.cjson_ = findItem; - return item; -} - -ValueObject JsonObject::GetObjectByPath(const JsonFieldPath &jsonPath, int &errCode) const -{ - JsonObject objGot = FindItem(jsonPath, errCode); - if (errCode != E_OK) { - GLOGE("Get json value object failed. %d", errCode); - return {}; - } - return objGot.GetItemValue(); -} - -int JsonObject::DeleteItemOnTarget(const JsonFieldPath &path) -{ - if (path.empty()) { - return -E_INVALID_ARGS; - } - - std::string fieldName = path.back(); - JsonFieldPath patherPath = path; - patherPath.pop_back(); - - cJSON *nodeFather = MoveToPath(cjson_, patherPath, caseSensitive_); - if (nodeFather == nullptr) { - GLOGE("Delete item failed, json field path not found."); - return -E_JSON_PATH_NOT_EXISTS; - } - - if (nodeFather->type == cJSON_Object) { - if (caseSensitive_) { - cJSON_DeleteItemFromObjectCaseSensitive(nodeFather, fieldName.c_str()); - } else { - cJSON_DeleteItemFromObject(nodeFather, fieldName.c_str()); - } - } else if (nodeFather->type == cJSON_Array) { - if (!IsNumber(fieldName)) { - GLOGW("Invalid json field path, expect array index."); - return -E_JSON_PATH_NOT_EXISTS; - } - cJSON_DeleteItemFromArray(nodeFather, std::stoi(fieldName)); - } - - return E_OK; -} - -int JsonObject::DeleteItemDeeplyOnTarget(const JsonFieldPath &path) -{ - if (path.empty()) { - return -E_INVALID_ARGS; - } - - std::string fieldName = path.back(); - JsonFieldPath patherPath = path; - patherPath.pop_back(); - - cJSON *nodeFather = MoveToPath(cjson_, patherPath, caseSensitive_); - if (nodeFather == nullptr) { - GLOGE("Delete item failed, json field path not found."); - return -E_JSON_PATH_NOT_EXISTS; - } - - if (nodeFather->type == cJSON_Object) { - if (caseSensitive_) { - cJSON_DeleteItemFromObjectCaseSensitive(nodeFather, fieldName.c_str()); - if (nodeFather->child == nullptr && path.size() > 1) { - JsonFieldPath fatherPath(path.begin(), path.end() - 1); - DeleteItemDeeplyOnTarget(fatherPath); - } - } else { - cJSON_DeleteItemFromObject(nodeFather, fieldName.c_str()); - if (nodeFather->child == nullptr && path.size() > 1) { - JsonFieldPath fatherPath(path.begin(), path.end() - 1); - DeleteItemDeeplyOnTarget(fatherPath); - } - } - } else if (nodeFather->type == cJSON_Array) { - if (!IsNumber(fieldName)) { - GLOGW("Invalid json field path, expect array index."); - return -E_JSON_PATH_NOT_EXISTS; - } - cJSON_DeleteItemFromArray(nodeFather, std::stoi(fieldName)); - if (nodeFather->child == nullptr && path.size() > 1) { - JsonFieldPath fatherPath(path.begin(), path.end() - 1); - DeleteItemDeeplyOnTarget(fatherPath); - } - } - - return E_OK; -} -} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/kv_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/kv_store_manager.cpp deleted file mode 100644 index 40fe20e5..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/kv_store_manager.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* -* 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. -*/ - -#include "kv_store_manager.h" - -#include "doc_errno.h" -#include "log_print.h" -#include "sqlite_store_executor_impl.h" -#include "sqlite_utils.h" - -namespace DocumentDB { -int KvStoreManager::GetKvStore(const std::string &path, const DBConfig &config, KvStoreExecutor *&executor) -{ - if (executor != nullptr) { - return -E_INVALID_ARGS; - } - - sqlite3 *db = nullptr; - int errCode = SqliteStoreExecutor::CreateDatabase(path, config, db); - if (errCode != E_OK) { - GLOGE("Get kv store failed. %d", errCode); - return errCode; - } - - auto *sqliteExecutor = new (std::nothrow) SqliteStoreExecutor(db); - if (sqliteExecutor == nullptr) { - sqlite3_close_v2(db); - return -E_OUT_OF_MEMORY; - } - - std::string oriConfigStr; - errCode = sqliteExecutor->GetDBConfig(oriConfigStr); - if (errCode == -E_NOT_FOUND) { - errCode = sqliteExecutor->SetDBConfig(config.ToString()); - } else if (errCode != E_OK) { - goto END; - } else { - DBConfig oriDbConfig = DBConfig::ReadConfig(oriConfigStr, errCode); - if (errCode != E_OK) { - GLOGE("Read db config failed. %d", errCode); - goto END; - } - if (config != oriDbConfig) { - errCode = -E_INVALID_CONFIG_VALUE; - GLOGE("Get kv store failed, db config changed. %d", errCode); - goto END; - } - } - - executor = sqliteExecutor; - return E_OK; - -END: - delete sqliteExecutor; - sqliteExecutor = nullptr; - return errCode; -} -} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp deleted file mode 100644 index 8dc09ad4..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ /dev/null @@ -1,271 +0,0 @@ -/* -* 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. -*/ - -#include "sqlite_store_executor_impl.h" - -#include "doc_errno.h" -#include "document_check.h" -#include "log_print.h" -#include "sqlite_utils.h" - -namespace DocumentDB { -int SqliteStoreExecutor::CreateDatabase(const std::string &path, const DBConfig &config, sqlite3 *&db) -{ - if (db != nullptr) { - return -E_INVALID_ARGS; - } - - int errCode = SQLiteUtils::CreateDataBase(path, 0, db); - if (errCode != E_OK || db == nullptr) { - GLOGE("Open or create database failed. %d", errCode); - return errCode; - } - - std::string pageSizeSql = "PRAGMA page_size=" + std::to_string(config.GetPageSize() * 1024); - errCode = SQLiteUtils::ExecSql(db, pageSizeSql); - if (errCode != E_OK) { - GLOGE("Set db page size failed. %d", errCode); - goto END; - } - - errCode = SQLiteUtils::ExecSql(db, "CREATE TABLE IF NOT EXISTS grd_meta (key BLOB PRIMARY KEY, value BLOB);"); - if (errCode != E_OK) { - GLOGE("Create meta table failed. %d", errCode); - goto END; - } - - return E_OK; - -END: - sqlite3_close_v2(db); - db = nullptr; - return errCode; -} - -SqliteStoreExecutor::SqliteStoreExecutor(sqlite3 *handle) : dbHandle_(handle) {} - -SqliteStoreExecutor::~SqliteStoreExecutor() -{ - sqlite3_close_v2(dbHandle_); - dbHandle_ = nullptr; -} - -int SqliteStoreExecutor::GetDBConfig(std::string &config) -{ - std::string dbConfigKeyStr = "DB_CONFIG"; - Key dbConfigKey = { dbConfigKeyStr.begin(), dbConfigKeyStr.end() }; - Value dbConfigVal; - int errCode = GetData("grd_meta", dbConfigKey, dbConfigVal); - config.assign(dbConfigVal.begin(), dbConfigVal.end()); - return errCode; -} - -int SqliteStoreExecutor::SetDBConfig(const std::string &config) -{ - std::string dbConfigKeyStr = "DB_CONFIG"; - Key dbConfigKey = { dbConfigKeyStr.begin(), dbConfigKeyStr.end() }; - Value dbConfigVal = { config.begin(), config.end() }; - return PutData("grd_meta", dbConfigKey, dbConfigVal); -} - -int SqliteStoreExecutor::PutData(const std::string &collName, const Key &key, const Value &value) -{ - if (dbHandle_ == nullptr) { - return -E_ERROR; - } - - std::string sql = "INSERT OR REPLACE INTO '" + collName + "' VALUES (?,?);"; - int errCode = SQLiteUtils::ExecSql( - dbHandle_, sql, - [key, value](sqlite3_stmt *stmt) { - SQLiteUtils::BindBlobToStatement(stmt, 1, key); - SQLiteUtils::BindBlobToStatement(stmt, 2, value); - return E_OK; - }, - nullptr); - if (errCode != SQLITE_OK) { - GLOGE("[sqlite executor] Put data failed. err=%d", errCode); - if (errCode == -E_ERROR) { - GLOGE("Cant find the collection"); - return -E_INVALID_ARGS; - } - return errCode; - } - return E_OK; -} - -int SqliteStoreExecutor::GetData(const std::string &collName, const Key &key, Value &value) const -{ - if (dbHandle_ == nullptr) { - GLOGE("Invalid db handle."); - return -E_ERROR; - } - int innerErrorCode = -E_NOT_FOUND; - std::string sql = "SELECT value FROM '" + collName + "' WHERE key=?;"; - int errCode = SQLiteUtils::ExecSql( - dbHandle_, sql, - [key](sqlite3_stmt *stmt) { - SQLiteUtils::BindBlobToStatement(stmt, 1, key); - return E_OK; - }, - [&value, &innerErrorCode](sqlite3_stmt *stmt) { - SQLiteUtils::GetColumnBlobValue(stmt, 0, value); - innerErrorCode = E_OK; - return E_OK; - }); - if (errCode != SQLITE_OK) { - GLOGE("[sqlite executor] Get data failed. err=%d", errCode); - return errCode; - } - return innerErrorCode; -} - -int SqliteStoreExecutor::DelData(const std::string &collName, const Key &key) -{ - if (dbHandle_ == nullptr) { - GLOGE("Invalid db handle."); - return -E_ERROR; - } - int errCode = 0; - if (!IsCollectionExists(collName, errCode)) { - return -E_INVALID_ARGS; - } - Value valueRet; - if (GetData(collName, key, valueRet) != E_OK) { - return -E_NO_DATA; - } - std::string sql = "DELETE FROM '" + collName + "' WHERE key=?;"; - errCode = SQLiteUtils::ExecSql( - dbHandle_, sql, - [key](sqlite3_stmt *stmt) { - SQLiteUtils::BindBlobToStatement(stmt, 1, key); - return E_OK; - }, - nullptr); - - if (errCode != SQLITE_OK) { - GLOGE("[sqlite executor] Delete data failed. err=%d", errCode); - if (errCode == -E_ERROR) { - GLOGE("Cant find the collection"); - return -E_NO_DATA; - } - } - return errCode; -} - -int SqliteStoreExecutor::CreateCollection(const std::string &name, bool ignoreExists) -{ - if (dbHandle_ == nullptr) { - return -E_ERROR; - } - std::string collName = COLL_PREFIX + name; - if (!ignoreExists) { - int errCode = E_OK; - bool isExists = IsCollectionExists(collName, errCode); - if (errCode != E_OK) { - return errCode; - } - if (isExists) { - GLOGE("[sqlite executor] Create collectoin failed, collection already exists."); - return -E_COLLECTION_CONFLICT; - } - } - - std::string sql = "CREATE TABLE IF NOT EXISTS '" + collName + "' (key BLOB PRIMARY KEY, value BLOB);"; - int errCode = SQLiteUtils::ExecSql(dbHandle_, sql); - if (errCode != SQLITE_OK) { - GLOGE("[sqlite executor] Create collectoin failed. err=%d", errCode); - return errCode; - } - return E_OK; -} - -int SqliteStoreExecutor::DropCollection(const std::string &name, bool ignoreNonExists) -{ - if (dbHandle_ == nullptr) { - return -E_ERROR; - } - - std::string collName = COLL_PREFIX + name; - if (!ignoreNonExists) { - int errCode = E_OK; - bool isExists = IsCollectionExists(collName, errCode); - if (errCode != E_OK) { - return errCode; - } - if (!isExists) { - GLOGE("[sqlite executor] Drop collectoin failed, collection not exists."); - return -E_NO_DATA; - } - } - - std::string sql = "DROP TABLE IF EXISTS '" + collName + "';"; - int errCode = SQLiteUtils::ExecSql(dbHandle_, sql); - if (errCode != SQLITE_OK) { - GLOGE("[sqlite executor] Drop collectoin failed. err=%d", errCode); - return errCode; - } - return E_OK; -} - -bool SqliteStoreExecutor::IsCollectionExists(const std::string &name, int &errCode) -{ - bool isExists = false; - std::string sql = "SELECT tbl_name FROM sqlite_master WHERE tbl_name=?;"; - - errCode = SQLiteUtils::ExecSql( - dbHandle_, sql, - [name](sqlite3_stmt *stmt) { - SQLiteUtils::BindTextToStatement(stmt, 1, name); - return E_OK; - }, - [&isExists](sqlite3_stmt *stmt) { - isExists = true; - return E_OK; - }); - - if (errCode != E_OK) { - GLOGE("Check collection exist failed. %d", errCode); - } - - return isExists; -} - -int SqliteStoreExecutor::GetCollectionOption(const std::string &name, std::string &option) -{ - std::string collOptKeyStr = "COLLECTION_OPTION_" + name; - Key collOptKey = { collOptKeyStr.begin(), collOptKeyStr.end() }; - Value collOptVal; - int errCode = GetData("grd_meta", collOptKey, collOptVal); - option.assign(collOptVal.begin(), collOptVal.end()); - return errCode; -} - -int SqliteStoreExecutor::SetCollectionOption(const std::string &name, const std::string &option) -{ - std::string collOptKeyStr = "COLLECTION_OPTION_" + name; - Key collOptKey = { collOptKeyStr.begin(), collOptKeyStr.end() }; - Value collOptVal = { option.begin(), option.end() }; - return PutData("grd_meta", collOptKey, collOptVal); -} - -int SqliteStoreExecutor::CleanCollectionOption(const std::string &name) -{ - std::string collOptKeyStr = "COLLECTION_OPTION_" + name; - Key collOptKey = { collOptKeyStr.begin(), collOptKeyStr.end() }; - return DelData("grd_meta", collOptKey); -} - -} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h deleted file mode 100644 index 514b9ae1..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h +++ /dev/null @@ -1,50 +0,0 @@ -/* -* 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 SQLITE_STORE_EXECUTOR_IMPL_H -#define SQLITE_STORE_EXECUTOR_IMPL_H - -#include "db_config.h" -#include "kv_store_executor.h" -#include "sqlite3.h" - -namespace DocumentDB { -class SqliteStoreExecutor : public KvStoreExecutor { -public: - static int CreateDatabase(const std::string &path, const DBConfig &config, sqlite3 *&db); - - SqliteStoreExecutor(sqlite3 *handle); - ~SqliteStoreExecutor() override; - - int GetDBConfig(std::string &config); - int SetDBConfig(const std::string &config); - - int PutData(const std::string &collName, const Key &key, const Value &value) override; - int GetData(const std::string &collName, const Key &key, Value &value) const override; - int DelData(const std::string &collName, const Key &key) override; - - int CreateCollection(const std::string &name, bool ignoreExists) override; - int DropCollection(const std::string &name, bool ignoreNonExists) override; - bool IsCollectionExists(const std::string &name, int &errCode) override; - - int GetCollectionOption(const std::string &name, std::string &option) override; - int SetCollectionOption(const std::string &name, const std::string &option) override; - int CleanCollectionOption(const std::string &name) override; - -private: - sqlite3 *dbHandle_ = nullptr; -}; -} // namespace DocumentDB -#endif // SQLITE_STORE_EXECUTOR_IMPL_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.cpp deleted file mode 100644 index 49f72653..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.cpp +++ /dev/null @@ -1,306 +0,0 @@ -/* - * 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. - */ -#include "sqlite_utils.h" - -#include - -#include "doc_errno.h" -#include "log_print.h" - -namespace DocumentDB { -const int MAX_BLOB_READ_SIZE = 5 * 1024 * 1024; // 5M limit -const int MAX_TEXT_READ_SIZE = 5 * 1024 * 1024; // 5M limit -const int BUSY_TIMEOUT_MS = 3000; // 3000ms for sqlite busy timeout. -const std::string BEGIN_SQL = "BEGIN TRANSACTION"; -const std::string BEGIN_IMMEDIATE_SQL = "BEGIN IMMEDIATE TRANSACTION"; -const std::string COMMIT_SQL = "COMMIT TRANSACTION"; -const std::string ROLLBACK_SQL = "ROLLBACK TRANSACTION"; - -namespace { -int MapSqliteError(int errCode) -{ - switch (errCode) { - case SQLITE_OK: - return E_OK; - case SQLITE_PERM: - case SQLITE_CANTOPEN: - case SQLITE_READONLY: - return -E_FILE_OPERATION; - default: - return -E_ERROR; - } -} - -std::mutex g_logConfigMutex; -bool g_configLog = false; -} // namespace - -void SQLiteUtils::SqliteLogCallback(void *data, int err, const char *msg) -{ - GLOGD("[SQLite] err=%d sys=%d %s msg=%s", err, errno, sqlite3_errstr(err), msg); -} - -int SQLiteUtils::CreateDataBase(const std::string &path, int flag, sqlite3 *&db) -{ - { - std::lock_guard lock(g_logConfigMutex); - if (!g_configLog) { - sqlite3_config(SQLITE_CONFIG_LOG, &SqliteLogCallback, nullptr); - g_configLog = true; - } - } - - int errCode = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr); - if (errCode != SQLITE_OK) { - GLOGE("Open database [%s] failed. %d", path.c_str(), errCode); - if (db != nullptr) { - (void)sqlite3_close_v2(db); - db = nullptr; - } - return MapSqliteError(errCode); - } - - errCode = sqlite3_busy_timeout(db, BUSY_TIMEOUT_MS); - if (errCode != SQLITE_OK) { - GLOGE("Set busy timeout failed:%d", errCode); - } - return MapSqliteError(errCode); -} - -int SQLiteUtils::GetStatement(sqlite3 *db, const std::string &sql, sqlite3_stmt *&statement) -{ - if (db == nullptr) { - GLOGE("Invalid db for get statement"); - return -E_INVALID_ARGS; - } - - // Prepare the new statement only when the input parameter is not null - if (statement != nullptr) { - return E_OK; - } - int errCode = sqlite3_prepare_v2(db, sql.c_str(), -1, &statement, nullptr); - if (errCode != SQLITE_OK) { - GLOGE("Prepare SQLite statement failed:%d", errCode); - (void)SQLiteUtils::ResetStatement(statement, true); - return errCode; - } - - if (statement == nullptr) { - return -E_ERROR; - } - - return E_OK; -} - -int SQLiteUtils::StepWithRetry(sqlite3_stmt *statement) -{ - if (statement == nullptr) { - return -E_INVALID_ARGS; - } - - int errCode = sqlite3_step(statement); - if (errCode != SQLITE_DONE && errCode != SQLITE_ROW) { - GLOGE("[SQLiteUtils] Step error:%d, sys:%d", errCode, errno); - } - - return errCode; -} - -int SQLiteUtils::ResetStatement(sqlite3_stmt *&statement, bool finalize) -{ - if (statement == nullptr) { - return -E_INVALID_ARGS; - } - - int errCode = E_OK; - if (!finalize) { - errCode = sqlite3_reset(statement); - if (errCode != SQLITE_OK) { - GLOGE("[SQLiteUtils] reset statement error:%d, sys:%d", errCode, errno); - goto FINALIZE; - } - - (void)sqlite3_clear_bindings(statement); - return errCode; - } - -FINALIZE: - int finalizeResult = sqlite3_finalize(statement); - if (finalizeResult != SQLITE_OK) { - GLOGE("[SQLiteUtils] finalize statement error:%d, sys:%d", finalizeResult, errno); - } - statement = nullptr; - return (errCode == SQLITE_OK ? finalizeResult : errCode); -} - -int SQLiteUtils::BindBlobToStatement(sqlite3_stmt *statement, int index, const std::vector &value) -{ - if (statement == nullptr) { - return -E_INVALID_ARGS; - } - - int errCode; - if (value.empty()) { - errCode = sqlite3_bind_zeroblob(statement, index, -1); // -1 for zero-length blob. - } else { - errCode = sqlite3_bind_blob( - statement, index, static_cast(value.data()), value.size(), SQLITE_TRANSIENT); - } - - if (errCode != SQLITE_OK) { - GLOGE("[SQLiteUtil][Bind blob] Failed to bind the value:%d", errCode); - } - return errCode; -} - -int SQLiteUtils::GetColumnBlobValue(sqlite3_stmt *statement, int index, std::vector &value) -{ - if (statement == nullptr) { - return -E_INVALID_ARGS; - } - - int keySize = sqlite3_column_bytes(statement, index); - if (keySize < 0 || keySize > MAX_BLOB_READ_SIZE) { - GLOGW("[SQLiteUtils][Column blob] size over limit:%d", keySize); - value.resize(MAX_BLOB_READ_SIZE + 1); // Reset value size to invalid - return E_OK; // Return OK for continue get data, but value is invalid - } - - auto keyRead = static_cast(sqlite3_column_blob(statement, index)); - if (keySize == 0 || keyRead == nullptr) { - value.resize(0); - } else { - value.resize(keySize); - value.assign(keyRead, keyRead + keySize); - } - - return E_OK; -} - -int SQLiteUtils::BindTextToStatement(sqlite3_stmt *statement, int index, const std::string &value) -{ - if (statement == nullptr) { - return -E_INVALID_ARGS; - } - - int errCode = sqlite3_bind_text(statement, index, value.c_str(), value.length(), SQLITE_TRANSIENT); - if (errCode != SQLITE_OK) { - GLOGE("[SQLiteUtil][Bind text]Failed to bind the value:%d", errCode); - return errCode; - } - - return E_OK; -} - -int SQLiteUtils::GetColumnTextValue(sqlite3_stmt *statement, int index, std::string &value) -{ - if (statement == nullptr) { - return -E_INVALID_ARGS; - } - - int valSize = sqlite3_column_bytes(statement, index); - if (valSize < 0 || valSize > MAX_TEXT_READ_SIZE) { - GLOGW("[SQLiteUtils][Column text] size over limit:%d", valSize); - value.resize(MAX_TEXT_READ_SIZE + 1); // Reset value size to invalid - return E_OK; // Return OK for continue get data, but value is invalid - } - - const unsigned char *val = sqlite3_column_text(statement, index); - if (valSize == 0 || val == nullptr) { - value = {}; - } else { - value = std::string(reinterpret_cast(val)); - } - - return E_OK; -} - -int SQLiteUtils::BeginTransaction(sqlite3 *db, TransactType type) -{ - if (type == TransactType::IMMEDIATE) { - return ExecSql(db, BEGIN_IMMEDIATE_SQL); - } - - return ExecSql(db, BEGIN_SQL); -} - -int SQLiteUtils::CommitTransaction(sqlite3 *db) -{ - return ExecSql(db, COMMIT_SQL); -} - -int SQLiteUtils::RollbackTransaction(sqlite3 *db) -{ - return ExecSql(db, ROLLBACK_SQL); -} - -int SQLiteUtils::ExecSql(sqlite3 *db, const std::string &sql) -{ - if (db == nullptr || sql.empty()) { - return -E_INVALID_ARGS; - } - - char *errMsg = nullptr; - int errCode = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errMsg); - if (errCode != SQLITE_OK && errMsg != nullptr) { - GLOGE("Execute sql failed. %d err: %s", errCode, errMsg); - } - - sqlite3_free(errMsg); - return MapSqliteError(errCode); -} - -int SQLiteUtils::ExecSql(sqlite3 *db, const std::string &sql, const std::function &bindCallback, - const std::function &resultCallback) -{ - if (db == nullptr || sql.empty()) { - return -E_INVALID_ARGS; - } - bool bindFinish = true; - sqlite3_stmt *stmt = nullptr; - int errCode = SQLiteUtils::GetStatement(db, sql, stmt); - if (errCode != E_OK) { - goto END; - } - - do { - if (bindCallback) { - errCode = bindCallback(stmt); - if (errCode != E_OK && errCode != -E_UNFINISHED) { - goto END; - } - bindFinish = (errCode != -E_UNFINISHED); // continue bind if unfinished - } - - while (true) { - errCode = SQLiteUtils::StepWithRetry(stmt); - if (errCode == SQLITE_DONE) { - errCode = E_OK; // Step finished - break; - } else if (errCode != SQLITE_ROW) { - goto END; // Step return error - } - if (resultCallback != nullptr && ((errCode = resultCallback(stmt)) != E_OK)) { - goto END; - } - } - errCode = SQLiteUtils::ResetStatement(stmt, false); - } while (!bindFinish); - -END: - (void)SQLiteUtils::ResetStatement(stmt, true); - return MapSqliteError(errCode); -} -} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.h deleted file mode 100644 index 42606f43..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_utils.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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 SQLITE_UTILS_H -#define SQLITE_UTILS_H - -#include -#include -#include - -#include "sqlite3sym.h" - -namespace DocumentDB { -enum class TransactType -{ - DEFERRED, - IMMEDIATE, -}; - -class SQLiteUtils { -public: - static int CreateDataBase(const std::string &path, int flag, sqlite3 *&db); - - static int GetStatement(sqlite3 *db, const std::string &sql, sqlite3_stmt *&statement); - static int StepWithRetry(sqlite3_stmt *statement); - static int ResetStatement(sqlite3_stmt *&statement, bool finalize); - - static int BindBlobToStatement(sqlite3_stmt *statement, int index, const std::vector &value); - static int GetColumnBlobValue(sqlite3_stmt *statement, int index, std::vector &value); - - static int BindTextToStatement(sqlite3_stmt *statement, int index, const std::string &value); - static int GetColumnTextValue(sqlite3_stmt *statement, int index, std::string &value); - - static int BeginTransaction(sqlite3 *db, TransactType type = TransactType::DEFERRED); - static int CommitTransaction(sqlite3 *db); - static int RollbackTransaction(sqlite3 *db); - - static int ExecSql(sqlite3 *db, const std::string &sql); - static int ExecSql(sqlite3 *db, const std::string &sql, const std::function &bindCallback, - const std::function &resultCallback); - -private: - static void SqliteLogCallback(void *data, int err, const char *msg); -}; -} // namespace DocumentDB -#endif // SQLITE_UTILS_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/BUILD.gn b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/BUILD.gn deleted file mode 100644 index 51d0a307..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/BUILD.gn +++ /dev/null @@ -1,164 +0,0 @@ -# Copyright (c) 2021 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. -import("//build/test.gni") - -module_output_path = "datamgr_service/documentdb" - -############################################################################### -config("module_private_config") { - visibility = [ ":*" ] - - include_dirs = [ - "../../include", - "../../src/common/include", - "../../src/executor/include", - "../../src/executor/document", - "../../src/oh_adapter/include", - "../../src/oh_adapter/src", - "../../src/interface/include", - - "common", - ] - - defines = [ - "SQLITE_ENABLE_SNAPSHOT", - "SQLITE_HAS_CODEC", - "SQLITE_ENABLE_JSON1", - "USING_HILOG_LOGGER", - "USE_SQLITE_SYMBOLS", - "SQLITE_ENABLE_DROPTABLE_CALLBACK", - ] -} - -############################################################################### -ohos_source_set("src_file") { - testonly = true - - sources = [ - "../../src/common/src/collection_option.cpp", - "../../src/common/src/db_config.cpp", - "../../src/common/src/json_common.cpp", - "../../src/common/src/log_print.cpp", - "../../src/common/src/os_api.cpp", - "../../src/executor/base/grd_db_api.cpp", - "../../src/executor/document/document_check.cpp", - "../../src/executor/document/grd_document_api.cpp", - "../../src/executor/document/grd_resultset_api.cpp", - "../../src/interface/src/collection.cpp", - "../../src/interface/src/doc_errno.cpp", - "../../src/interface/src/document_store.cpp", - "../../src/interface/src/document_store_manager.cpp", - "../../src/interface/src/projection_tree.cpp", - "../../src/interface/src/result_set.cpp", - "../../src/interface/src/result_set_common.cpp", - "../../src/oh_adapter/src/json_object.cpp", - "../../src/oh_adapter/src/kv_store_manager.cpp", - "../../src/oh_adapter/src/sqlite_store_executor_impl.cpp", - "../../src/oh_adapter/src/sqlite_utils.cpp", - - "common/documentdb_test_utils.cpp", - ] - - configs = [ ":module_private_config" ] - - deps = [ - "//third_party/googletest:gtest_main", - "//third_party/sqlite:sqlite", - ] - - configs += [ "//third_party/cJSON:cJSON_config" ] - ldflags = [ "-Wl,--exclude-libs,ALL" ] - deps += [ "//third_party/cJSON:cjson" ] - external_deps = [ - "c_utils:utils", - "hisysevent_native:libhisysevent", - "hitrace_native:hitrace_meter", - "hiviewdfx_hilog_native:libhilog", - ] - part_name = "kv_store" -} - -template("documentdb_unittest") { - ohos_unittest(target_name) { - forward_variables_from(invoker, "*") - module_out_path = module_output_path - if (!defined(deps)) { - deps = [] - } - if (!defined(external_deps)) { - external_deps = [] - } - configs = [ ":module_private_config" ] - deps += [ - ":src_file", - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - "//third_party/sqlite:sqlite", - ] - configs += [ "//third_party/cJSON:cJSON_config" ] - ldflags = [ "-Wl,--exclude-libs,ALL" ] - deps += [ - "//third_party/cJSON:cjson", - "//third_party/openssl:libcrypto_shared", - ] - external_deps = [ - "c_utils:utils", - "hisysevent_native:libhisysevent", - "hitrace_native:hitrace_meter", - "hiviewdfx_hilog_native:libhilog", - ] - } -} - -documentdb_unittest("DocumentDBApiTest") { - sources = [ "api/documentdb_api_test.cpp" ] -} - -documentdb_unittest("DocumentDBCollectionTest") { - sources = [ "api/documentdb_collection_test.cpp" ] -} - -documentdb_unittest("DocumentDBDataTest") { - sources = [ "api/documentdb_data_test.cpp" ] -} - -documentdb_unittest("DocumentDBJsonCommonTest") { - sources = [ "oh_adapter/documentdb_json_common_test.cpp" ] -} - -documentdb_unittest("DocumentDBJsonObjectTest") { - sources = [ "oh_adapter/documentdb_jsonobject_test.cpp" ] -} - -############################################################################### -group("unittest") { - testonly = true - deps = [ "//third_party/googletest:gmock" ] - - deps += [ - ":DocumentDBApiTest", - ":DocumentDBCollectionTest", - ":DocumentDBDataTest", - ":DocumentDBJsonCommonTest", - ":DocumentDBJsonObjectTest", - ] -} - -############################################################################### - -group("document_fuzztest") { - testonly = true - deps = [] - deps += [] -} -############################################################################### diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_api_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_api_test.cpp deleted file mode 100644 index 47182cfa..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_api_test.cpp +++ /dev/null @@ -1,637 +0,0 @@ -/* -* 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. -*/ - -#include - -#include "doc_errno.h" -#include "documentdb_test_utils.h" -#include "grd_base/grd_db_api.h" -#include "grd_base/grd_error.h" -#include "grd_document/grd_document_api.h" -#include "log_print.h" -#include "sqlite_utils.h" - -using namespace DocumentDB; -using namespace testing::ext; -using namespace DocumentDBUnitTest; - -class DocumentDBApiTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void DocumentDBApiTest::SetUpTestCase(void) {} - -void DocumentDBApiTest::TearDownTestCase(void) {} - -void DocumentDBApiTest::SetUp(void) {} - -void DocumentDBApiTest::TearDown(void) -{ - DocumentDBTestUtils::RemoveTestDbFiles("./document.db"); -} - -/** - * @tc.name: OpenDBTest001 - * @tc.desc: Test open document db - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBTest001, TestSize.Level0) -{ - std::string path = "./document.db"; - GRD_DB *db = nullptr; - int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - EXPECT_NE(db, nullptr); - GLOGD("Open DB test 001: status: %d", status); - - EXPECT_EQ(GRD_CreateCollection(db, "student", "", 0), GRD_OK); - - EXPECT_EQ(GRD_UpsertDoc(db, "student", R""({"_id":"10001"})"", R""({"name":"Tom","age":23})"", 0), 1); - EXPECT_EQ(GRD_UpsertDoc(db, "student", R""({"_id":"10001"})"", R""({"name":"Tom","age":23})"", 0), 1); - - EXPECT_EQ(GRD_DropCollection(db, "student", 0), GRD_OK); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - DocumentDBTestUtils::RemoveTestDbFiles(path); -} - -/** - * @tc.name: OpenDBTest002 - * @tc.desc: Test open document db with invalid db - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBTest002, TestSize.Level0) -{ - std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, nullptr); - EXPECT_EQ(status, GRD_INVALID_ARGS); - - GRD_DB *db = nullptr; - status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - EXPECT_NE(db, nullptr); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - - status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); -} - -/** - * @tc.name: OpenDBPathTest001 - * @tc.desc: Test open document db with NULL path - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBPathTest001, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::vector invalidPath = { nullptr, "", "/a/b/c/" }; - for (auto path : invalidPath) { - GLOGD("OpenDBPathTest001: open db with path: %s", path); - int status = GRD_DBOpen(path, nullptr, GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_INVALID_ARGS); - } -} - -/** - * @tc.name: OpenDBPathTest002 - * @tc.desc: Test open document db with file no permission - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBPathTest002, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string pathNoPerm = "/root/document.db"; - int status = GRD_DBOpen(pathNoPerm.c_str(), nullptr, GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_FAILED_FILE_OPERATION); -} - -/** - * @tc.name: OpenDBConfigTest001 - * @tc.desc: Test open document db with invalid config option - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBConfigTest001, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - const int MAX_JSON_LEN = 512 * 1024; - std::string configStr = std::string(MAX_JSON_LEN, 'a'); - int status = GRD_DBOpen(path.c_str(), configStr.c_str(), GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OVER_LIMIT); -} - -/** - * @tc.name: OpenDBConfigTest002 - * @tc.desc: Test open document db with invalid config format - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBConfigTest002, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), "{aa}", GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_INVALID_FORMAT); -} - -/** - * @tc.name: OpenDBConfigTest003 - * @tc.desc: Test open document db with config not support - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBConfigTest003, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), R""({"notSupport":123})"", GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_INVALID_ARGS); -} - -/** - * @tc.name: OpenDBConfigMaxConnNumTest001 - * @tc.desc: Test open document db with invalid config item maxConnNum - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest001, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - - std::vector configList = { - R""({"maxConnNum":0})"", - R""({"maxConnNum":15})"", - R""({"maxConnNum":1025})"", - R""({"maxConnNum":1000000007})"", - R""({"maxConnNum":"16"})"", - R""({"maxConnNum":{"value":17}})"", - R""({"maxConnNum":[16,17,18]})"", - }; - for (const auto &config : configList) { - GLOGD("OpenDBConfigMaxConnNumTest001: test with config:%s", config.c_str()); - int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); - ASSERT_EQ(status, GRD_INVALID_ARGS); - } -} - -/** - * @tc.name: OpenDBConfigMaxConnNumTest002 - * @tc.desc: Test open document db with valid item maxConnNum - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest002, TestSize.Level1) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - - for (int i = 16; i <= 1024; i++) { - std::string config = "{\"maxConnNum\":" + std::to_string(i) + "}"; - int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - ASSERT_NE(db, nullptr); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - DocumentDBTestUtils::RemoveTestDbFiles(path); - } -} - -/** - * @tc.name: OpenDBConfigMaxConnNumTest003 - * @tc.desc: Test reopen document db with different maxConnNum - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest003, TestSize.Level1) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - - std::string config = R""({"maxConnNum":16})""; - int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - config = R""({"maxConnNum":17})""; - status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_INVALID_ARGS); - - DocumentDBTestUtils::RemoveTestDbFiles(path); -} - -/** - * @tc.name: OpenDBConfigMaxConnNumTest004 - * @tc.desc: Test open document db over maxConnNum - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest004, TestSize.Level1) -{ - std::string path = "./document.db"; - - int maxCnt = 16; - std::string config = "{\"maxConnNum\":" + std::to_string(maxCnt) + "}"; - - std::vector dbList; - while (maxCnt--) { - GRD_DB *db = nullptr; - int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - EXPECT_NE(db, nullptr); - dbList.push_back(db); - } - - GRD_DB *db = nullptr; - int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - EXPECT_NE(db, nullptr); - dbList.push_back(db); - - for (auto *it : dbList) { - status = GRD_DBClose(it, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - } - - DocumentDBTestUtils::RemoveTestDbFiles(path); -} - -/** - * @tc.name: OpenDBConfigPageSizeTest001 - * @tc.desc: Test open document db with invalid config item pageSize - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest001, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - - std::vector configList = { - R""({"pageSize":0})"", - R""({"pageSize":5})"", - R""({"pageSize":48})"", - R""({"pageSize":1000000007})"", - R""({"pageSize":"4"})"", - R""({"pageSize":{"value":8}})"", - R""({"pageSize":[16,32,64]})"", - }; - for (const auto &config : configList) { - GLOGD("OpenDBConfigPageSizeTest001: test with config:%s", config.c_str()); - int status = GRD_DBOpen(path.c_str(), config.c_str(), 0, &db); - EXPECT_EQ(status, GRD_INVALID_ARGS); - } -} - -namespace { -int GetDBPageSize(const std::string &path) -{ - sqlite3 *db = nullptr; - int ret = SQLiteUtils::CreateDataBase(path, 0, db); - EXPECT_EQ(ret, E_OK); - if (db == nullptr) { - return 0; - } - - int pageSize = 0; - SQLiteUtils::ExecSql(db, "PRAGMA page_size;", nullptr, [&pageSize](sqlite3_stmt *stmt) { - pageSize = sqlite3_column_int(stmt, 0); - return E_OK; - }); - - sqlite3_close_v2(db); - return pageSize; -} -} // namespace - -/** - * @tc.name: OpenDBConfigPageSizeTest002 - * @tc.desc: Test open document db with valid config item pageSize - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest002, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - - for (int size : { 4, 8, 16, 32, 64 }) { - std::string config = "{\"pageSize\":" + std::to_string(size) + "}"; - int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - EXPECT_EQ(GetDBPageSize(path), size * 1024); - DocumentDBTestUtils::RemoveTestDbFiles(path); - } -} - -/** - * @tc.name: OpenDBConfigPageSizeTest003 - * @tc.desc: Test reopen document db with different pageSize - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest003, TestSize.Level1) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - - std::string config = R""({"pageSize":4})""; - int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - config = R""({"pageSize":8})""; - status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_INVALID_ARGS); - - DocumentDBTestUtils::RemoveTestDbFiles(path); -} - -/** - * @tc.name: OpenDBConfigRedoFlushTest001 - * @tc.desc: Test open document db with valid config item redoFlushByTrx - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBConfigRedoFlushTest001, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - - for (int flush : { 0, 1 }) { - std::string config = "{\"redoFlushByTrx\":" + std::to_string(flush) + "}"; - int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - DocumentDBTestUtils::RemoveTestDbFiles(path); - } -} - -/** - * @tc.name: OpenDBConfigXXXTest001 - * @tc.desc: Test open document db with invalid config item redoFlushByTrx - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBConfigRedoFlushTest002, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - - std::string config = R""({"redoFlushByTrx":3})""; - int status = GRD_DBOpen(path.c_str(), config.c_str(), GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_INVALID_ARGS); -} - -/** - * @tc.name: OpenDBFlagTest001 - * @tc.desc: Test open document db with invalid flag - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBFlagTest001, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - std::vector invaldFlag = { GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK, - GRD_DB_OPEN_CREATE | GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK, 0x08, 0xffff, UINT32_MAX }; - for (unsigned int flag : invaldFlag) { - GLOGD("OpenDBFlagTest001: open doc db with flag %u", flag); - int status = GRD_DBOpen(path.c_str(), "", flag, &db); - EXPECT_EQ(status, GRD_INVALID_ARGS); - } -} - -/** - * @tc.name: OpenDBFlagTest002 - * @tc.desc: Test open document db with valid flag - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, OpenDBFlagTest002, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_ONLY, &db); - EXPECT_EQ(status, GRD_INVALID_ARGS); - - status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_ONLY, &db); - EXPECT_EQ(status, GRD_OK); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CHECK_FOR_ABNORMAL, &db); - EXPECT_EQ(status, GRD_OK); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CHECK, &db); - EXPECT_EQ(status, GRD_OK); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - DocumentDBTestUtils::RemoveTestDbFiles(path); -} - -/** - * @tc.name: CloseDBTest001 - * @tc.desc: Test close document db with invalid db - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, CloseDBTest001, TestSize.Level0) -{ - GRD_DB *db = nullptr; - int status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_INVALID_ARGS); - - status = GRD_DBClose(db, GRD_DB_CLOSE_IGNORE_ERROR); - EXPECT_EQ(status, GRD_INVALID_ARGS); - db = nullptr; -} - -/** - * @tc.name: CloseDBFlagTest001 - * @tc.desc: Test close document db with valid flag - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, CloseDBFlagTest001, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - ASSERT_NE(db, nullptr); - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - DocumentDBTestUtils::RemoveTestDbFiles(path); -} - -/** - * @tc.name: CloseDBFlagTest002 - * @tc.desc: Test close document db with valid flag - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, CloseDBFlagTest002, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - ASSERT_NE(db, nullptr); - - // TODO: open result set - - status = GRD_DBClose(db, GRD_DB_CLOSE_IGNORE_ERROR); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - DocumentDBTestUtils::RemoveTestDbFiles(path); -} - -/** - * @tc.name: CloseDBFlagTest003 - * @tc.desc: Test close document db with invalid flag - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, CloseDBFlagTest003, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - ASSERT_NE(db, nullptr); - - std::vector invaldFlag = { 0x02, 0x03, 0xffff, UINT32_MAX }; - for (unsigned int flag : invaldFlag) { - GLOGD("CloseDBFlagTest003: close doc db with flag %u", flag); - status = GRD_DBClose(db, flag); - EXPECT_EQ(status, GRD_INVALID_ARGS); - } - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - DocumentDBTestUtils::RemoveTestDbFiles(path); -} - -/** - * @tc.name: FlushDBTest001 - * @tc.desc: Test flush document db - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBApiTest, FlushDBTest001, TestSize.Level0) -{ - EXPECT_EQ(GRD_Flush(nullptr, GRD_DB_FLUSH_ASYNC), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_Flush(nullptr, GRD_DB_FLUSH_SYNC), GRD_INVALID_ARGS); - - GRD_DB *db = nullptr; - std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_OK); - ASSERT_NE(db, nullptr); - - EXPECT_EQ(GRD_Flush(db, GRD_DB_FLUSH_ASYNC), GRD_OK); - EXPECT_EQ(GRD_Flush(db, GRD_DB_FLUSH_SYNC), GRD_OK); - std::vector invalidFlags = { 2, 4, 8, 512, 1024, UINT32_MAX }; - for (auto flags : invalidFlags) { - EXPECT_EQ(GRD_Flush(db, flags), GRD_INVALID_ARGS); - } - - status = GRD_DBClose(db, GRD_DB_CLOSE); - EXPECT_EQ(status, GRD_OK); - db = nullptr; - - DocumentDBTestUtils::RemoveTestDbFiles(path); -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_collection_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_collection_test.cpp deleted file mode 100644 index a5e72086..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_collection_test.cpp +++ /dev/null @@ -1,204 +0,0 @@ -/* -* 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. -*/ - -#include - -#include "doc_errno.h" -#include "documentdb_test_utils.h" -#include "grd_base/grd_db_api.h" -#include "grd_base/grd_error.h" -#include "grd_document/grd_document_api.h" -#include "log_print.h" -#include "sqlite_utils.h" - -using namespace DocumentDB; -using namespace testing::ext; -using namespace DocumentDBUnitTest; - -namespace { -std::string g_path = "./document.db"; -GRD_DB *g_db = nullptr; -} // namespace - -class DocumentDBCollectionTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void DocumentDBCollectionTest::SetUpTestCase(void) {} - -void DocumentDBCollectionTest::TearDownTestCase(void) {} - -void DocumentDBCollectionTest::SetUp(void) -{ - EXPECT_EQ(GRD_DBOpen(g_path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &g_db), GRD_OK); - EXPECT_NE(g_db, nullptr); -} - -void DocumentDBCollectionTest::TearDown(void) -{ - if (g_db != nullptr) { - EXPECT_EQ(GRD_DBClose(g_db, GRD_DB_CLOSE), GRD_OK); - g_db = nullptr; - } - DocumentDBTestUtils::RemoveTestDbFiles(g_path); -} - -/** - * @tc.name: CollectionTest001 - * @tc.desc: Test create collection with null db - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBCollectionTest, CollectionTest001, TestSize.Level0) -{ - EXPECT_EQ(GRD_CreateCollection(nullptr, "student", "", 0), GRD_INVALID_ARGS); -} - -namespace { -const int MAX_COLLECTION_LEN = 512; -} - -/** - * @tc.name: CollectionTest002 - * @tc.desc: Test create/drop collection with invalid collection name - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBCollectionTest, CollectionTest002, TestSize.Level0) -{ - string overLenName(MAX_COLLECTION_LEN, 'a'); - EXPECT_EQ(GRD_CreateCollection(g_db, overLenName.c_str(), "", 0), GRD_OVER_LIMIT); - EXPECT_EQ(GRD_DropCollection(g_db, overLenName.c_str(), 0), GRD_OVER_LIMIT); - - std::vector invalidName = { - nullptr, - "", - }; - - for (auto *it : invalidName) { - GLOGD("CollectionTest002: create collection with name: %s", it); - EXPECT_EQ(GRD_CreateCollection(g_db, it, "", 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_DropCollection(g_db, it, 0), GRD_INVALID_ARGS); - } - - std::vector invalidNameFormat = { "GRD_123", "grd_123", "GM_SYS_123", "gm_sys_123" }; - - for (auto *it : invalidNameFormat) { - GLOGD("CollectionTest002: create collection with name: %s", it); - EXPECT_EQ(GRD_CreateCollection(g_db, it, "", 0), GRD_INVALID_FORMAT); - EXPECT_EQ(GRD_DropCollection(g_db, it, 0), GRD_INVALID_FORMAT); - } -} - -/** - * @tc.name: CollectionTest003 - * @tc.desc: Test create/drop collection with valid collection name - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBCollectionTest, CollectionTest003, TestSize.Level0) -{ - string overLenName(MAX_COLLECTION_LEN - 1, 'a'); - std::vector validName = { "123", "&^%@", "中文字符", "sqlite_master", "NULL", "SELECT", - overLenName.c_str() }; - - for (auto *it : validName) { - GLOGD("CollectionTest003: create collection with name: %s", it); - EXPECT_EQ(GRD_CreateCollection(g_db, it, "", 0), GRD_OK); - EXPECT_EQ(GRD_DropCollection(g_db, it, 0), GRD_OK); - } -} - -/** - * @tc.name: CollectionTest004 - * @tc.desc: Test create collection with ignore flag - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBCollectionTest, CollectionTest004, TestSize.Level0) -{ - EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); - EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); - EXPECT_EQ(GRD_CreateCollection(g_db, "Student", "", CHK_EXIST_COLLECTION), GRD_DATA_CONFLICT); -} - -/** - * @tc.name: CollectionTest005 - * @tc.desc: Test create collection with invalid option - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBCollectionTest, CollectionTest005, TestSize.Level0) -{ - EXPECT_EQ(GRD_CreateCollection(g_db, "student", R""({aa})"", 0), GRD_INVALID_FORMAT); - - std::vector invalidOption = { - R""({"maxDoc":0})"", - R""({"maxDoc":"123"})"", - R""({"maxDoc":{"value":1024}})"", - R""({"maxDoc":[1,2,4,8]})"", - R""({"minDoc":1024})"", - }; - - for (auto opt : invalidOption) { - GLOGD("CollectionTest005: create collection with option: %s", opt); - EXPECT_EQ(GRD_CreateCollection(g_db, "student", opt, 0), GRD_INVALID_ARGS); - } -} - -/** - * @tc.name: CollectionTest006 - * @tc.desc: Test create/drop collection with valid flag - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBCollectionTest, CollectionTest006, TestSize.Level0) -{ - EXPECT_EQ(GRD_CreateCollection(g_db, "student", R""({"maxDoc":1024})"", 0), GRD_OK); - - EXPECT_EQ(GRD_CreateCollection(g_db, "student", R""({"maxDoc":2048})"", 0), GRD_INVALID_ARGS); - - EXPECT_EQ(GRD_DropCollection(g_db, "student", 0), GRD_OK); - EXPECT_EQ(GRD_DropCollection(g_db, "student", 0), GRD_OK); - EXPECT_EQ(GRD_DropCollection(g_db, "student", CHK_NON_EXIST_COLLECTION), GRD_NO_DATA); - - // Create collection with different option returnh OK after drop collection - EXPECT_EQ(GRD_CreateCollection(g_db, "student", R""({"maxDoc":2048})"", 0), GRD_OK); -} - -/** - * @tc.name: CollectionTest007 - * @tc.desc: Test create/drop collection with invalid flag - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBCollectionTest, CollectionTest007, TestSize.Level0) -{ - for (int flag : std::vector{ 2, 4, 8, 1024, UINT32_MAX }) { - EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", flag), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_DropCollection(g_db, "student", flag), GRD_INVALID_ARGS); - } -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp deleted file mode 100644 index 2194bffb..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp +++ /dev/null @@ -1,314 +0,0 @@ -/* -* 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. -*/ - -#include - -#include "doc_errno.h" -#include "documentdb_test_utils.h" -#include "grd_base/grd_db_api.h" -#include "grd_base/grd_error.h" -#include "grd_document/grd_document_api.h" -#include "log_print.h" -#include "sqlite_utils.h" - -using namespace DocumentDB; -using namespace testing::ext; -using namespace DocumentDBUnitTest; - -namespace { -std::string g_path = "./document.db"; -GRD_DB *g_db = nullptr; -const char *g_coll = "student"; -} // namespace - -class DocumentDBDataTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void DocumentDBDataTest::SetUpTestCase(void) {} - -void DocumentDBDataTest::TearDownTestCase(void) {} - -void DocumentDBDataTest::SetUp(void) -{ - EXPECT_EQ(GRD_DBOpen(g_path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &g_db), GRD_OK); - EXPECT_NE(g_db, nullptr); - - EXPECT_EQ(GRD_CreateCollection(g_db, g_coll, "", 0), GRD_OK); -} - -void DocumentDBDataTest::TearDown(void) -{ - if (g_db != nullptr) { - EXPECT_EQ(GRD_DBClose(g_db, GRD_DB_CLOSE), GRD_OK); - g_db = nullptr; - } - DocumentDBTestUtils::RemoveTestDbFiles(g_path); -} - -/** - * @tc.name: UpsertDataTest001 - * @tc.desc: Test upsert data into collection - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpsertDataTest001, TestSize.Level0) -{ - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, R""({"_id":"1234"})"", document.c_str(), GRD_DOC_REPLACE), 1); - - std::string update = R""({"CC":"AAAA"})""; - EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, R""({"_id":"1234"})"", update.c_str(), 0), 1); - - std::string append = R""({"addr.city":"DDDD"})""; - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, R""({"_id":"1234"})"", append.c_str(), GRD_DOC_APPEND), 1); -} - -/** - * @tc.name: UpsertDataTest002 - * @tc.desc: Test upsert data with db is nullptr - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpsertDataTest002, TestSize.Level0) -{ - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - EXPECT_EQ(GRD_UpsertDoc(nullptr, g_coll, "1234", document.c_str(), GRD_DOC_REPLACE), GRD_INVALID_ARGS); -} - -/** - * @tc.name: UpsertDataTest003 - * @tc.desc: Test upsert data with invalid collection name - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpsertDataTest003, TestSize.Level0) -{ - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector> invalidName = { - { nullptr, GRD_INVALID_ARGS }, - { "", GRD_INVALID_ARGS }, - { "GRD_123", GRD_INVALID_FORMAT }, - { "grd_123", GRD_INVALID_FORMAT }, - { "GM_SYS_123", GRD_INVALID_FORMAT }, - { "gm_sys_123", GRD_INVALID_FORMAT }, - }; - for (auto it : invalidName) { - GLOGD("UpsertDataTest003: upsert data with collectionname: %s", it.first); - EXPECT_EQ(GRD_UpsertDoc(g_db, it.first, "1234", document.c_str(), GRD_DOC_REPLACE), it.second); - } -} - -HWTEST_F(DocumentDBDataTest, UpsertDataTest004, TestSize.Level0) -{ - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector invalidFilter = { - nullptr, - "", - R""({"name":"Tmono"})"", - R""({"value":{"_id":"1234"}})"", - R""({"_id":1234})"", - }; - for (auto filter : invalidFilter) { - GLOGD("UpsertDataTest004: upsert data with filter: %s", filter); - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter, document.c_str(), GRD_DOC_REPLACE), GRD_INVALID_ARGS); - } -} - -HWTEST_F(DocumentDBDataTest, UpsertDataTest005, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::vector> invalidDocument = { - { "", GRD_INVALID_ARGS }, - { nullptr, GRD_INVALID_ARGS }, - { R""({invalidJsonFormat})"", GRD_INVALID_FORMAT }, - }; - for (auto it : invalidDocument) { - GLOGD("UpsertDataTest005: upsert data with document: %s", it.first); - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), it.first, GRD_DOC_REPLACE), it.second); - } -} - -/** - * @tc.name: UpsertDataTest006 - * @tc.desc: Test upsert data with invalid flags - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpsertDataTest006, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - - for (auto flags : std::vector{ 2, 4, 8, 64, 1024, UINT32_MAX }) { - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), flags), GRD_INVALID_ARGS); - } -} - -/** - * @tc.name: UpsertDataTest007 - * @tc.desc: Test upsert data with collection not create - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpsertDataTest007, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string val = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - EXPECT_EQ(GRD_UpsertDoc(g_db, "collection_not_exists", filter.c_str(), val.c_str(), GRD_DOC_REPLACE), GRD_NO_DATA); -} - -/** - * @tc.name: UpsertDataTest008 - * @tc.desc: Test upsert data with different document in append - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpsertDataTest008, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmn","age":18,"addr":{"city":"shanghai","postal":200001}})""; - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), GRD_DOC_REPLACE), 1); - - std::string updateDoc = R""({"name":"Xue","case":2,"age":28,"addr":{"city":"shenzhen","postal":518000}})""; - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), updateDoc.c_str(), GRD_DOC_APPEND), 1); -} - -/** - * @tc.name: UpdateDataTest001 - * @tc.desc: - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest001, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string updateDoc = R""({"name":"Xue"})""; - EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), updateDoc.c_str(), 0), GRD_NO_DATA); -} - -/** - * @tc.name: UpdateDataTest002 - * @tc.desc: Test update data with db is nullptr - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest002, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - EXPECT_EQ(GRD_UpdateDoc(nullptr, g_coll, filter.c_str(), document.c_str(), 0), GRD_INVALID_ARGS); -} - -/** - * @tc.name: UpdateDataTest003 - * @tc.desc: Test update data with invalid collection name - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest003, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector> invalidName = { - { nullptr, GRD_INVALID_ARGS }, - { "", GRD_INVALID_ARGS }, - { "GRD_123", GRD_INVALID_FORMAT }, - { "grd_123", GRD_INVALID_FORMAT }, - { "GM_SYS_123", GRD_INVALID_FORMAT }, - { "gm_sys_123", GRD_INVALID_FORMAT }, - }; - for (auto it : invalidName) { - GLOGD("UpdateDataTest003: update data with collectionname: %s", it.first); - EXPECT_EQ(GRD_UpdateDoc(g_db, it.first, filter.c_str(), document.c_str(), 0), it.second); - } -} - -/** - * @tc.name: UpdateDataTest004 - * @tc.desc: Test update data with invalid filter - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest004, TestSize.Level0) -{ - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector invalidFilter = { - nullptr, - "", - R""({"name":"Tmono"})"", - R""({"value":{"_id":"1234"}})"", - R""({"_id":1234})"", - }; - for (auto filter : invalidFilter) { - GLOGD("UpdateDataTest004: update data with filter: %s", filter); - EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter, document.c_str(), 0), GRD_INVALID_ARGS); - } -} - -/** - * @tc.name: UpdateDataTest005 - * @tc.desc: Test update data with invalid doc - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest005, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector> invalidUpdate = { - { "", GRD_INVALID_ARGS }, - { nullptr, GRD_INVALID_ARGS }, - { R""({invalidJsonFormat})"", GRD_INVALID_FORMAT }, - }; - - for (auto it : invalidUpdate) { - GLOGD("UpdateDataTest005: update data with doc: %s", it.first); - EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), it.first, 0), it.second); - } -} - -/** - * @tc.name: UpdateDataTest006 - * @tc.desc: Test update data with invalid flag - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest006, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector invalidFlags = { 1, 2, 4, 8, 1024, UINT32_MAX }; - for (auto flag : invalidFlags) { - GLOGD("UpdateDataTest006: update data with flag: %u", flag); - EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), document.c_str(), flag), GRD_INVALID_ARGS); - } -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_delete_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_delete_test.cpp deleted file mode 100644 index a1deee42..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_delete_test.cpp +++ /dev/null @@ -1,320 +0,0 @@ -/* -* 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. -*/ - -#include - -#include "grd_base/grd_db_api.h" -#include "grd_base/grd_error.h" -#include "grd_base/grd_resultset_api.h" -#include "grd_base/grd_type_export.h" -#include "grd_document/grd_document_api.h" -#include "grd_resultset_inner.h" -#include "grd_type_inner.h" - -using namespace testing::ext; -namespace { -constexpr const char *COLLECTION_NAME = "student"; -constexpr const char *NULL_JSON_STR = "{}"; -const int MAX_COLLECTION_LENS = 511; -std::string path = "./document.db"; -GRD_DB *g_db = nullptr; -} // namespace - -class DocumentDeleteApiTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); - void InsertDoc(const char *collectionName, const char *document); -}; -void DocumentDeleteApiTest::SetUpTestCase(void) -{ - std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &g_db); - EXPECT_EQ(status, GRD_OK); -} - -void DocumentDeleteApiTest::TearDownTestCase(void) -{ - EXPECT_EQ(GRD_DBClose(g_db, 0), GRD_OK); - remove(path.c_str()); -} - -void DocumentDeleteApiTest::SetUp(void) -{ - /** - * @tc.steps:step1. Create Collection - * @tc.expected: step1. GRD_OK - */ - EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); - /** - * @tc.steps:step2. Insert many document in order to delete - * @tc.expected: step2. GRD_OK - */ - const char *document1 = "{ \ - \"_id\" : \"1\", \ - \"name\": \"xiaoming\", \ - \"address\": \"beijing\", \ - \"age\" : 15, \ - \"friend\" : {\"name\" : \"David\", \"sex\" : \"female\", \"age\" : 90}, \ - \"subject\": [\"math\", \"English\", \"music\"] \ - }"; - const char *document2 = "{ \ - \"_id\" : \"2\", \ - \"name\": \"ori\", \ - \"address\": \"beijing\", \ - \"age\" : 15, \ - \"friend\" : {\"name\" : \"David\", \"sex\" : \"female\", \"age\" : 90}, \ - \"subject\": [\"math\", \"English\", \"music\"] \ - }"; - const char *document3 = "{ \ - \"_id\" : \"3\", \ - \"name\": \"David\", \ - \"address\": \"beijing\", \ - \"age\" : 15, \ - \"friend\" : {\"name\" : \"David\", \"sex\" : \"female\", \"age\" : 90}, \ - \"subject\": [\"math\", \"English\", \"music\"] \ - }"; - DocumentDeleteApiTest::InsertDoc(COLLECTION_NAME, document1); - DocumentDeleteApiTest::InsertDoc(COLLECTION_NAME, document2); - DocumentDeleteApiTest::InsertDoc(COLLECTION_NAME, document3); -} - -void DocumentDeleteApiTest::TearDown(void) -{ - /** - * @tc.steps:step1. Call GRD_DropCollection to drop the collection - * @tc.expected: step1. GRD_OK - */ - EXPECT_EQ(GRD_DropCollection(g_db, COLLECTION_NAME, 0), GRD_OK); -} - -static void ChkDeleteResWithFilter(const char *filter) -{ - /** - * @tc.steps:step1. Try to find the deleted document - * @tc.expected: step1. GRD_OK - */ - Query query; - query.filter = filter; - const char *projection = "{}"; - query.projection = projection; - GRD_ResultSet *resultSet = nullptr; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - /** - * @tc.steps:step2. The resultset should be NULL - * @tc.expected: step2. GRD_OK - */ - EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -void DocumentDeleteApiTest::InsertDoc(const char *collectionName, const char *document) -{ - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, document, 0), GRD_OK); -} - -/** - * @tc.name: DocumentDelete001 - * @tc.desc: Delete with NULL filter - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDeleteApiTest, DeleteDBTest001, TestSize.Level1) -{ - /** - * @tc.steps:step1. Delete all the document - * @tc.expected: step1. GRD_INVALID_ARGS - */ - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, NULL_JSON_STR, 0), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentDelete002 - * @tc.desc: Delete with filter which has no _id - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDeleteApiTest, DeleteDBTest002, TestSize.Level1) -{ - /** - * @tc.steps:step1. Delete with filter which has no _id - * @tc.expected: step1. GRD_INVALID_ARGS - */ - const char *filter = "{\"age\" : 15}"; - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentDelete003 - * @tc.desc: Delete with filter which has more than one fileds. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDeleteApiTest, DeleteDBTest003, TestSize.Level1) -{ - /** - * @tc.steps:step1. Delete with filter which has more than one fileds. - * @tc.expected: step1. GRD_INVALID_ARGS - */ - const char *filter = "{\"_id\" : \"1\", \"age\" : 15}"; - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentDelete004 - * @tc.desc: Test delete with invalid input - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDeleteApiTest, DeleteDBTest004, TestSize.Level1) -{ - /** - * @tc.steps:step1. Test delete with un-zero flags - * @tc.expected: step1. GRD_INVALID_ARGS - */ - const char *filter1 = "{\"_id\" : \"1\"}"; - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter1, 1), GRD_INVALID_ARGS); - /** - * @tc.steps:step2. Test delete with NULL collection name - * @tc.expected: step2. GRD_INVALID_ARGS - */ - // const char *filter2 = "{\"_id\" : \"1\"}"; - // EXPECT_EQ(GRD_DeleteDoc(g_db, NULL, filter2, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step3. Test delete with empty collection name - * @tc.expected: step3. GRD_INVALID_ARGS - */ - // const char *filter1 = "{\"_id\" : \"1\"}"; - // EXPECT_EQ(GRD_DeleteDoc(g_db, "", filter1, 1), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentDelete005 - * @tc.desc: Test delete with same collection name - * but one is uppercase(delete) and the other is lowercase(insert) - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDeleteApiTest, DeleteDBTest005, TestSize.Level1) -{ - /** - * @tc.step1: Test delete with same collection name - * but one is uppercase(delete) and the other is lowercase(insert) - * @tc.expected: step1. GRD_INVALID_ARGS - */ - const char *filter = "{\"_id\" : \"1\"}"; - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), 1); - /** - * @tc.step2: Check whether doc has been deleted compeletely - * @tc.expected: step2. GRD_OK - */ - ChkDeleteResWithFilter(filter); -} - -/** - * @tc.name: DocumentDelete006 - * @tc.desc: Test delete after calling find interface - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDeleteApiTest, DeleteDBTest006, TestSize.Level1) -{ - /** - * @tc.step1: Create filter with _id and get the record according to filter condition. - * @tc.expected: step1. GRD_OK - */ - const char *filter = "{\"_id\" : \"1\"}"; - GRD_ResultSet *resultSet = nullptr; - Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), 1); - /** - * @tc.step2: Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. - */ - EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentDelete007 - * @tc.desc: Test delete with too long collectionName. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDeleteApiTest, DeleteDBTest007, TestSize.Level1) -{ - const char *filter = "{\"_id\" : \"1\"}"; - string collectionName1(MAX_COLLECTION_LENS, 'a'); - EXPECT_EQ(GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0), GRD_OK); - EXPECT_EQ(GRD_DeleteDoc(g_db, collectionName1.c_str(), filter, 0), 0); - EXPECT_EQ(GRD_DropCollection(g_db, collectionName1.c_str(), 0), GRD_OK); - - string collectionName2(MAX_COLLECTION_LENS + 1, 'a'); - EXPECT_EQ(GRD_DeleteDoc(g_db, collectionName2.c_str(), filter, 0), GRD_OVER_LIMIT); -} - -/** - * @tc.name: DocumentDelete008 - * @tc.desc: Test delete with invalid NULL input for all parameters. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDeleteApiTest, DeleteDBTest008, TestSize.Level1) -{ - /** - * @tc.steps:step1. Delete with filter which has more than one fileds. - * @tc.expected: step1. GRD_INVALID_ARGS - */ - const char *filter = "{\"_id\" : \"1\"}"; - EXPECT_EQ(GRD_DeleteDoc(NULL, COLLECTION_NAME, filter, 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_DeleteDoc(g_db, NULL, filter, 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_DeleteDoc(g_db, "", filter, 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, NULL, 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, "", 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_DeleteDoc(g_db, "notExisted", filter, 0), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentDelete010 - * @tc.desc: Test delete document when filter _id is int and string - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDeleteApiTest, DeleteDBTest010, TestSize.Level1) -{ - /** - * @tc.steps:step1. Test delete document when filter _id is int and string. - * @tc.expected: step1. GRD_INVALID_ARGS - */ - std::vector filterVec = { R"({"_id" : 1})", R"({"_id":[1, 2]})", R"({"_id" : {"t1" : 1}})", - R"({"_id":null})", R"({"_id":true})", R"({"_id" : 1.333})", R"({"_id" : -2.0})" }; - for (const auto &item : filterVec) { - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, item.c_str(), 0), GRD_INVALID_ARGS); - } - const char *filter = "{\"_id\" : \"1\"}"; - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), 1); -} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp deleted file mode 100644 index 6515db6a..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp +++ /dev/null @@ -1,1455 +0,0 @@ -/* -* 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. -*/ - -#include - -#include "doc_errno.h" -#include "documentdb_test_utils.h" -#include "grd_base/grd_db_api.h" -#include "grd_base/grd_error.h" -#include "grd_base/grd_resultset_api.h" -#include "grd_base/grd_type_export.h" -#include "grd_document/grd_document_api.h" -#include "grd_resultset_inner.h" -#include "grd_type_inner.h" -#include "log_print.h" - -using namespace testing::ext; -namespace { -std::string path = "./document.db"; -GRD_DB *g_db = nullptr; -constexpr const char *COLLECTION_NAME = "student"; -constexpr const char *NULL_JSON_STR = "{}"; -const int E_OK = 0; -const int MAX_COLLECTION_NAME = 511; -const int INT_MAX = 2147483647; -const int INT_MIN = -2147483648; -const int MAX_ID_LENS = 899; -static const char *g_document1 = "{\"_id\" : \"1\", \"name\":\"doc1\",\"item\":\"journal\",\"personInfo\":\ - {\"school\":\"AB\", \"age\" : 51}}"; -static const char *g_document2 = "{\"_id\" : \"2\", \"name\":\"doc2\",\"item\": 1, \"personInfo\":\ - [1, \"my string\", {\"school\":\"AB\", \"age\" : 51}, true, {\"school\":\"CD\", \"age\" : 15}, false]}"; -static const char *g_document3 = "{\"_id\" : \"3\", \"name\":\"doc3\",\"item\":\"notebook\",\"personInfo\":\ - [{\"school\":\"C\", \"age\" : 5}]}"; -static const char *g_document4 = "{\"_id\" : \"4\", \"name\":\"doc4\",\"item\":\"paper\",\"personInfo\":\ - {\"grade\" : 1, \"school\":\"A\", \"age\" : 18}}"; -static const char *g_document5 = "{\"_id\" : \"5\", \"name\":\"doc5\",\"item\":\"journal\",\"personInfo\":\ - [{\"sex\" : \"woma\", \"school\" : \"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; -static const char *g_document6 = "{\"_id\" : \"6\", \"name\":\"doc6\",\"item\":false,\"personInfo\":\ - [{\"school\":\"B\", \"teacher\" : \"mike\",\"age\" : 15}, {\"school\":\"C\", \"teacher\" : \"moon\",\"age\" : 20}]}"; -static const char *g_document7 = "{\"_id\" : \"7\", \"name\":\"doc7\",\"item\":\"fruit\",\"other_Info\":\ - [{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; -static const char *g_document8 = "{\"_id\" : \"8\", \"name\":\"doc8\",\"item\":true,\"personInfo\":\ - [{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; -static const char *g_document9 = "{\"_id\" : \"9\", \"name\":\"doc9\",\"item\": true}"; -static const char *g_document10 = "{\"_id\" : \"10\", \"name\":\"doc10\", \"parent\" : \"kate\"}"; -static const char *g_document11 = "{\"_id\" : \"11\", \"name\":\"doc11\", \"other\" : \"null\"}"; -static const char *g_document12 = "{\"_id\" : \"12\", \"name\":\"doc12\",\"other\" : null}"; -static const char *g_document13 = "{\"_id\" : \"13\", \"name\":\"doc13\",\"item\" : \"shoes\",\"personInfo\":\ - {\"school\":\"AB\", \"age\" : 15}}"; -static const char *g_document14 = "{\"_id\" : \"14\", \"name\":\"doc14\",\"item\" : true,\"personInfo\":\ - [{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 85}]}"; -static const char *g_document15 = "{\"_id\" : \"15\", \"name\":\"doc15\",\"personInfo\":[{\"school\":\"C\", \"age\" : " - "5}]}"; -static const char *g_document16 = "{\"_id\" : \"16\", \"name\":\"doc16\", \"nested1\":{\"nested2\":{\"nested3\":\ - {\"nested4\":\"ABC\", \"field2\":\"CCC\"}}}}"; -static const char *g_document17 = "{\"_id\" : \"17\", \"name\":\"doc17\",\"personInfo\":\"oh,ok\"}"; -static const char *g_document18 = "{\"_id\" : \"18\", \"name\":\"doc18\",\"item\" : \"mobile phone\",\"personInfo\":\ - {\"school\":\"DD\", \"age\":66}, \"color\":\"blue\"}"; -static const char *g_document19 = "{\"_id\" : \"19\", \"name\":\"doc19\",\"ITEM\" : true,\"PERSONINFO\":\ - {\"school\":\"AB\", \"age\":15}}"; -static const char *g_document20 = "{\"_id\" : \"20\", \"name\":\"doc20\",\"ITEM\" : true,\"personInfo\":\ - [{\"SCHOOL\":\"B\", \"AGE\":15}, {\"SCHOOL\":\"C\", \"AGE\":35}]}"; -static std::vector g_data = { g_document1, g_document2, g_document3, g_document4, g_document5, - g_document6, g_document7, g_document8, g_document9, g_document10, g_document11, g_document12, g_document13, - g_document14, g_document15, g_document16, g_document17, g_document18, g_document19, g_document20 }; - -static void InsertData(GRD_DB *g_db, const char *collectionName) -{ - for (const auto &item : g_data) { - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, item, 0), GRD_OK); - } -} - -static void CompareValue(const char *value, const char *targetValue) -{ - int errCode; - DocumentDB::JsonObject valueObj = DocumentDB::JsonObject::Parse(value, errCode); - EXPECT_EQ(errCode, E_OK); - DocumentDB::JsonObject targetValueObj = DocumentDB::JsonObject::Parse(targetValue, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(valueObj.Print(), targetValueObj.Print()); -} -} // namespace - -class DocumentFindApiTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); - void InsertDoc(const char *collectionName, const char *document); -}; -void DocumentFindApiTest::SetUpTestCase(void) -{ - std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &g_db); - EXPECT_EQ(status, GRD_OK); - EXPECT_EQ(GRD_CreateCollection(g_db, COLLECTION_NAME, "", 0), GRD_OK); - EXPECT_NE(g_db, nullptr); -} - -void DocumentFindApiTest::TearDownTestCase(void) -{ - EXPECT_EQ(GRD_DBClose(g_db, 0), GRD_OK); - remove(path.c_str()); -} - -void DocumentFindApiTest::SetUp(void) -{ - InsertData(g_db, "student"); -} - -void DocumentFindApiTest::TearDown(void) {} - -/** - * @tc.name: DocumentFindApiTest001 - * @tc.desc: Test Insert document db - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest001, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter with _id and get the record according to filter condition. - * @tc.expected: step1. Succeed to get the record, the matching record is g_document6. - */ - const char *filter = "{\"_id\" : \"6\"}"; - GRD_ResultSet *resultSet = nullptr; - Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = NULL; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, g_document6); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - /** - * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. - */ - EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest002 - * @tc.desc: Test filter with multiple fields and _id. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest002, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter with multiple and _id. and get the record according to filter condition. - * @tc.expected: step1. Faild to get the record, the result is GRD_INVALID_ARGS, GRD_GetValue return GRD_NOT_AVAILABLE and GRD_Next return GRD_NO_DATA. - */ - const char *filter = "{\"_id\" : \"6\", \"name\":\"doc6\"}"; - GRD_ResultSet *resultSet = nullptr; - Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); - /** - * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. - */ - EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); - char *value = NULL; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest004 - * @tc.desc: test filter with string filter without _id. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest004, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter without _id and get the record according to filter condition. - * @tc.expected: step1. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - const char *filter = "{\"name\":\"doc6\"}"; - GRD_ResultSet *resultSet = nullptr; - Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. - */ - char *value = NULL; - EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest005 - * @tc.desc: test filter field with other word. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest005, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter with _id and number - * @tc.expected: step1. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet1 = nullptr; - const char *filter1 = "{\"_id\" : \"1\", \"info\" : 1}"; - Query query1 = { filter1, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query1, 1, &resultSet1), GRD_INVALID_ARGS); - - /** - * @tc.steps: step2. Create filter with two _id - * @tc.expected: step2. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet2 = nullptr; - const char *filter2 = "{\"_id\" : \"1\", \"_id\" : \"2\"}"; - Query query2 = { filter2, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query2, 1, &resultSet2), GRD_INVALID_ARGS); - - /** - * @tc.steps: step3. Create filter with array and _id - * @tc.expected: step3. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet3 = nullptr; - const char *filter3 = "{\"_id\" : \"1\", \"info\" : [\"2\", 1]}"; - Query query3 = { filter3, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query3, 1, &resultSet3), GRD_INVALID_ARGS); - - /** - * @tc.steps: step4. Create filter with object and _id - * @tc.expected: step4. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet4 = nullptr; - const char *filter4 = "{\"_id\" : \"1\", \"info\" : {\"info_val\" : \"1\"}}"; - Query query4 = { filter4, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query4, 1, &resultSet4), GRD_INVALID_ARGS); - - /** - * @tc.steps: step5. Create filter with bool and _id - * @tc.expected: step5. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet5 = nullptr; - const char *filter5 = "{\"_id\" : \"1\", \"info\" : true}"; - Query query5 = { filter5, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query5, 1, &resultSet5), GRD_INVALID_ARGS); - - /** - * @tc.steps: step6. Create filter with null and _id - * @tc.expected: step6. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet6 = nullptr; - const char *filter6 = "{\"_id\" : \"1\", \"info\" : null}"; - Query query6 = { filter6, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query6, 1, &resultSet6), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest006 - * @tc.desc: test filter field with id which has different type of value. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest006, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter with _id which value is string - * @tc.expected: step1. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet1 = nullptr; - const char *filter1 = "{\"_id\" : \"valstring\"}"; - Query query1 = { filter1, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query1, 1, &resultSet1), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet1), GRD_OK); - - /** - * @tc.steps: step2. Create filter with _id which value is number - * @tc.expected: step2. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet2 = nullptr; - const char *filter2 = "{\"_id\" : 1}"; - Query query2 = { filter2, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query2, 1, &resultSet2), GRD_INVALID_ARGS); - - /** - * @tc.steps: step3. Create filter with _id which value is array - * @tc.expected: step3. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet3 = nullptr; - const char *filter3 = "{\"_id\" : [\"2\", 1]}"; - Query query3 = { filter3, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query3, 1, &resultSet3), GRD_INVALID_ARGS); - - /** - * @tc.steps: step4. Create filter with _id which value is object - * @tc.expected: step4. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet4 = nullptr; - const char *filter4 = "{\"_id\" : {\"info_val\" : \"1\"}}"; - Query query4 = { filter4, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query4, 1, &resultSet4), GRD_INVALID_ARGS); - - /** - * @tc.steps: step5. Create filter with _id which value is bool - * @tc.expected: step5. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet5 = nullptr; - const char *filter5 = "{\"_id\" : true}"; - Query query5 = { filter5, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query5, 1, &resultSet5), GRD_INVALID_ARGS); - - /** - * @tc.steps: step6. Create filter with _id which value is null - * @tc.expected: step6. Faild to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet6 = nullptr; - const char *filter6 = "{\"_id\" : null}"; - Query query6 = { filter6, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query6, 1, &resultSet6), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest016 - * @tc.desc: Test filter with collection Name is invalid. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest016, TestSize.Level1) -{ - const char *colName1 = "grd_type"; - const char *colName2 = "GM_SYS_sysfff"; - GRD_ResultSet *resultSet = NULL; - const char *filter = "{\"_id\" : \"1\"}"; - Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, colName1, query, 1, &resultSet), GRD_INVALID_FORMAT); - EXPECT_EQ(GRD_FindDoc(g_db, colName2, query, 1, &resultSet), GRD_INVALID_FORMAT); -} - -// /** -// * @tc.name: DocumentFindApiTest017 -// * @tc.desc: Test filter field with large filter -// * @tc.type: FUNC -// * @tc.require: -// * @tc.author: mazhao -// */ -// HWTEST_F(DocumentFindApiTest, DocumentFindApiTest017, TestSize.Level1) -// { -// GRD_ResultSet *resultSet = nullptr; -// string documentPart1 = "{\"_id\" : \"18\", \"item\" :\" "; -// string documentPart2 = "\" }"; -// string jsonVal = string(512 * 1024 - documentPart1.size() - documentPart2.size() - 1, 'k'); -// string document = documentPart1 + jsonVal + documentPart2; -// string jsonVal1 = string(512 * 1024 - documentPart1.size() - documentPart2.size(), 'k'); -// string document1 = documentPart1 + jsonVal1 + documentPart2; - -// Query query = {document.c_str(), "{}"}; -// EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); -// EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); -// char *value = NULL; -// EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); -// EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - -// query = {document1.c_str(), "{}"}; -// EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OVER_LIMIT); -// } - -/** - * @tc.name: DocumentFindApiTest019 - * @tc.desc: Test filter field with no result - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest019, TestSize.Level1) -{ - const char *filter = "{\"_id\" : \"100\"}"; - GRD_ResultSet *resultSet = nullptr; - Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - char *value = NULL; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest023 - * @tc.desc: Test filter field with double find. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest023, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter with _id and get the record according to filter condition. - * @tc.expected: step1. succeed to get the record, the matching record is g_document6. - */ - const char *filter = "{\"_id\" : \"6\"}"; - GRD_ResultSet *resultSet = nullptr; - GRD_ResultSet *resultSet2 = nullptr; - Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet2), GRD_RESOURCE_BUSY); - - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = NULL; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, g_document6); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - /** - * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. - */ - EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, g_document6); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest024 - * @tc.desc: Test filter field with multi collections - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest024, TestSize.Level1) -{ - const char *filter = "{\"_id\" : \"6\"}"; - GRD_ResultSet *resultSet = nullptr; - GRD_ResultSet *resultSet2 = nullptr; - Query query = { filter, "{}" }; - const char *collectionName = "DocumentFindApiTest024"; - EXPECT_EQ(GRD_CreateCollection(g_db, collectionName, "", 0), GRD_OK); - InsertData(g_db, collectionName); - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_FindDoc(g_db, collectionName, query, 1, &resultSet2), GRD_OK); - - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = NULL; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, g_document6); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - - EXPECT_EQ(GRD_Next(resultSet2), GRD_OK); - char *value2 = NULL; - EXPECT_EQ(GRD_GetValue(resultSet2, &value2), GRD_OK); - CompareValue(value2, g_document6); - EXPECT_EQ(GRD_FreeValue(value2), GRD_OK); - - EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - EXPECT_EQ(GRD_Next(resultSet2), GRD_NO_DATA); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); - EXPECT_EQ(GRD_GetValue(resultSet2, &value), GRD_NOT_AVAILABLE); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet2), GRD_OK); - - EXPECT_EQ(GRD_DropCollection(g_db, collectionName, 0), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest025 - * @tc.desc: Test nested projection, with viewType equals to 1. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest025, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter to match g_document16, _id flag is 0. - * Create projection to display name,nested4. - * @tc.expected: step1. resultSet init successfuly, the result is GRD_OK, - */ - const char *filter = "{\"_id\" : \"16\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\": true, \"nested1.nested2.nested3.nested4\":true}"; - const char *targetDocument = "{\"name\":\"doc16\", \"nested1\":{\"nested2\":{\"nested3\":\ - {\"nested4\":\"ABC\"}}}}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - /** - * @tc.steps: step2. After loop, cannot get more record. - * @tc.expected: step2. Return GRD_NO_DATA. - */ - EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - /** - * @tc.steps: step3. Create filter to match g_document16, _id flag is 0; - * Create projection to display name、nested4 with different projection format. - * @tc.expected: step3. succeed to get the record. - */ - projectionInfo = "{\"name\": true, \"nested1\":{\"nested2\":{\"nested3\":{\"nested4\":true}}}}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - /** - * @tc.steps: step4. After loop, cannot get more record. - * @tc.expected: step4. return GRD_NO_DATA. - */ - EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - /** - * @tc.steps: step5. Create filter to match g_document16, _id flag is 0. - * Create projection to conceal name,nested4 with different projection format. - * @tc.expected: step5. succeed to get the record. - */ - projectionInfo = "{\"name\": 0, \"nested1.nested2.nested3.nested4\":0}"; - targetDocument = "{\"nested1\":{\"nested2\":{\"nested3\":{\"field2\":\"CCC\"}}}}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} -#include -/** - * @tc.name: DocumentFindApiTest026 - * @tc.desc: Test nested projection, with _id flag equals to 1. Projection is 5 level. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest026, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter to match g_document16, _id flag is 0. - * Create projection to display name,nested5 - * @tc.expected: step1. Error GRD_INVALID_ARGS. - */ - const char *filter = "{\"_id\" : \"16\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\": true, \"nested1.nested2.nested3.nested4.nested5\":true}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); - // EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); - // char *value = nullptr; - // EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); - /** - * @tc.steps: step2. After loop, cannot get more record. - * @tc.expected: step2. Return GRD_NO_DATA. - */ - // EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); - // EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest027 - * @tc.desc: Test projection with invalid field, _id field equals to 1. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest027, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. - * Create projection to display name, other _info and non existing field. - * @tc.expected: step1. Match the g_document7 and display name, other_info - */ - const char *filter = "{\"_id\" : \"7\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\": true, \"other_Info\":true, \"non_exist_field\":true}"; - const char *targetDocument = "{\"name\": \"doc7\", \"other_Info\":[{\"school\":\"BX\", \"age\":15},\ - {\"school\":\"C\", \"age\":35}]}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - - /** - * @tc.steps: step2. Create filter to match g_document7, _id flag is 0. - * Create projection to display name, other _info and existing field with space. - * @tc.expected: step2. Return GRD_INVALID_ARGS. - */ - projectionInfo = "{\"name\": true, \"other_Info\":true, \" item \":true}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps: step3. Create filter to match g_document7, _id flag is 0. - * Create projection to display name, other _info and existing field with different case. - * @tc.expected: step3. Match the g_document7 and display name, other_Info. - */ - projectionInfo = "{\"name\": true, \"other_Info\":true, \"ITEM\": true}"; - query = { filter, projectionInfo }; - resultSet = nullptr; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest028 - * @tc.desc: Test projection with invalid field in Array,_id field equals to 1. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest028, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. - * Create projection to display name, non existing field in array. - * @tc.expected: step1. Match the g_document7 and display name, other_info. - */ - const char *filter = "{\"_id\" : \"7\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\": true, \"other_Info.non_exist_field\":true}"; - const char *targetDocument = "{\"name\": \"doc7\"}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - - /** - * @tc.steps: step2. Create filter to match g_document7, _id flag is 0. - * Create projection to display name, other _info and existing field with space. - * @tc.expected: step2. Return GRD_INVALID_ARGS. - */ - projectionInfo = "{\"name\": true, \"other_Info\":{\"non_exist_field\":true}}"; - query = { filter, projectionInfo }; - resultSet = nullptr; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - - /** - * @tc.steps: step3. Create filter to match g_document7, _id flag is 0. - * Create projection to display name, non existing field in array with index format. - * @tc.expected: step3. Match the g_document7 and display name, other_Info. - */ - projectionInfo = "{\"name\": true, \"other_Info.0\": true}"; - query = { filter, projectionInfo }; - resultSet = nullptr; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest029 - * @tc.desc: Test projection with path conflict._id field equals to 0. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest029, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter to match g_document4, _id flag is 0. - * Create projection to display conflict path. - * @tc.expected: step1. Return GRD_INVALID_ARGS. - */ - const char *filter = "{\"_id\" : \"4\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"personInfo\": true, \"personInfo.grade\": true}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest030 - * @tc.desc: Test _id flag and field.None exist field. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest030, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. - * @tc.expected: step1. Match the g_document7 and return empty json. - */ - const char *filter = "{\"_id\" : \"7\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"non_exist_field\":true}"; - int flag = 0; - const char *targetDocument = "{}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - - /** - * @tc.steps: step2. Create filter to match g_document7, _id flag is 1. - * @tc.expected: step2. Match g_document7, and return a json with _id. - */ - resultSet = nullptr; - flag = 1; - targetDocument = "{\"_id\": \"7\"}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - value = NULL; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest031 - * @tc.desc: Test _id flag and field.Exist field with 1 value. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest031, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. - * @tc.expected: step1. Match the g_document7 and return json with name, item. - */ - const char *filter = "{\"_id\" : \"7\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\":true, \"item\":true}"; - int flag = 0; - const char *targetDocument = "{\"name\":\"doc7\", \"item\":\"fruit\"}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - - /** - * @tc.steps: step2. Create filter to match g_document7, _id flag is 1. - * @tc.expected: step2. Match g_document7, and return a json with _id. - */ - resultSet = nullptr; - flag = 1; - projectionInfo = "{\"name\": 1, \"item\": 1}"; - targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - value = NULL; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest032 - * @tc.desc: Test _id flag and field.Exist field with 1 value. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest032, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. - * @tc.expected: step1. Match the g_document7 and return json with name, item. - */ - const char *filter = "{\"_id\" : \"7\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\":true, \"item\":true}"; - int flag = 0; - const char *targetDocument = "{\"name\":\"doc7\", \"item\":\"fruit\"}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - /** - * @tc.steps: step2. Create filter to match g_document7, _id flag is 1. - * @tc.expected: step2. Match g_document7, and return a json with _id. - */ - resultSet = nullptr; - flag = 1; - projectionInfo = "{\"name\": 1, \"item\": 1}"; - targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - value = NULL; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - /** - * @tc.steps: step3. Create filter to match g_document7, _id flag is 1.Projection value is not 0. - * @tc.expected: step3. Match g_document7, and return a json with name, item and _id. - */ - resultSet = nullptr; - flag = 1; - projectionInfo = "{\"name\": 10, \"item\": 10}"; - targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - value = NULL; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest033 - * @tc.desc: Test _id flag and field.Exist field with 0 value. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest033, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. - * @tc.expected: step1. Match the g_document7 and return json with name, item and _id - */ - const char *filter = "{\"_id\" : \"7\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\":false, \"item\":false}"; - int flag = 0; - const char *targetDocument = "{\"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : " - "35}]}"; - ; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - /** - * @tc.steps: step2. Create filter to match g_document7, _id flag is 1. - * @tc.expected: step2. Match g_document7, and return a json without name and item. - */ - resultSet = nullptr; - flag = 1; - projectionInfo = "{\"name\": 0, \"item\": 0}"; - targetDocument = "{\"_id\": \"7\", \"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" " - ": 35}]}"; - ; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - value = NULL; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest034 - * @tc.desc: Test projection with nonexist field in nested structure. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest034, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter to match g_document4, _id flag is 0. - * @tc.expected: step1. Match the g_document4 and return json without name - */ - const char *filter = "{\"_id\" : \"4\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\": 1, \"personInfo.grade1\": 1, \ - \"personInfo.shool1\": 1, \"personInfo.age1\": 1}"; - int flag = 0; - const char *targetDocument = "{\"name\":\"doc4\"}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - - /** - * @tc.steps: step2. Create filter to match g_document4, _id flag is 0, display part of fields in nested structure. - * @tc.expected: step2. Match the g_document4 and return json without name - */ - projectionInfo = "{\"name\": false, \"personInfo.grade1\": false, \ - \"personInfo.shool1\": false, \"personInfo.age1\": false}"; - const char *targetDocument2 = "{\"item\":\"paper\",\"personInfo\":{\"grade\" : 1, \"school\":\"A\", \"age\" : " - "18}}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument2); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - - /** - * @tc.steps: step3. Create filter to match g_document4, _id flag is 0, display part of fields in nested structure. - * @tc.expected: step3. Match the g_document4 and return json with name, personInfo.school and personInfo.age. - */ - projectionInfo = "{\"name\": 1, \"personInfo.school\": 1, \"personInfo.age\": 1}"; - const char *targetDocument3 = "{\"name\":\"doc4\", \"personInfo\": {\"school\":\"A\", \"age\" : 18}}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument3); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - - /** - * @tc.steps: step4. Create filter to match g_document4, _id flag is 0, display part of fields in nested structure. - * @tc.expected: step4. Match the g_document4 and return json with name, personInfo.school - */ - projectionInfo = "{\"name\": 1, \"personInfo.school\": 1, \"personInfo.age1\": 1}"; - const char *targetDocument4 = "{\"name\":\"doc4\", \"personInfo\": {\"school\":\"A\"}}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument4); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest035 - * @tc.desc: test filter with id string filter - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest035, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter with _id and get the record according to filter condition. - * @tc.expected: step1. succeed to get the record, the matching record is g_document17 - */ - const char *filter = "{\"_id\" : \"17\"}"; - GRD_ResultSet *resultSet = nullptr; - int flag = 0; - Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, GRD_DOC_ID_DISPLAY, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, g_document17); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - /** - * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. - */ - EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest036 - * @tc.desc: Test with invalid collectionName. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest036, TestSize.Level1) -{ - /** - * @tc.steps: step1. Test with invalid collectionName. - * @tc.expected: step1. Return GRD_INVALID_ARGS. - */ - const char *filter = "{\"_id\" : \"17\"}"; - GRD_ResultSet *resultSet = nullptr; - int flag = 0; - Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, "", query, 0, &resultSet), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_FindDoc(g_db, NULL, query, 0, &resultSet), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest037 - * @tc.desc: Test filed with different value. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest037, TestSize.Level1) -{ - /** - * @tc.steps: step1. Test filed with different value.some are 1, other are 0. - * @tc.expected: step1. Return GRD_INVALID_ARGS. - */ - const char *filter = "{\"_id\" : \"4\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\":1, \"personInfo\":0, \"item\":1}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps: step2. Test filed with different value.some are 2, other are 0. - * @tc.expected: step2. Return GRD_INVALID_ARGS. - */ - projectionInfo = "{\"name\":2, \"personInfo\":0, \"item\":2}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps: step3. Test filed with different value.some are 0, other are true. - * @tc.expected: step3. Return GRD_INVALID_ARGS. - */ - projectionInfo = "{\"name\":true, \"personInfo\":0, \"item\":true}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps: step4. Test filed with different value.some are 0, other are "". - * @tc.expected: step4. Return GRD_INVALID_ARGS. - */ - projectionInfo = "{\"name\":\"\", \"personInfo\":0, \"item\":\"\"}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps: step5. Test filed with different value.some are 1, other are false. - * @tc.expected: step5. Return GRD_INVALID_ARGS. - */ - projectionInfo = "{\"name\":false, \"personInfo\":1, \"item\":false"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT); - - /** - * @tc.steps: step6. Test filed with different value.some are -1.123, other are false. - * @tc.expected: step6. Return GRD_INVALID_ARGS. - */ - projectionInfo = "{\"name\":false, \"personInfo\":-1.123, \"item\":false"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT); - - /** - * @tc.steps: step7. Test filed with different value.some are true, other are false. - * @tc.expected: step7. Return GRD_INVALID_ARGS. - */ - projectionInfo = "{\"name\":false, \"personInfo\":true, \"item\":false"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT); -} - -/** - * @tc.name: DocumentFindApiTest038 - * @tc.desc: Test field with false value. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest038, TestSize.Level1) -{ - /** - * @tc.steps: step1. Test field with different false value. Some are false, other are 0. flag is 0. - * @tc.expected: step1. Match the g_document6 and return empty json. - */ - const char *filter = "{\"_id\" : \"6\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\":false, \"personInfo\": 0, \"item\":0}"; - int flag = 0; - const char *targetDocument = "{}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - - /** - * @tc.steps: step2. Test field with different false value.Some are false, others are 0. flag is 1. - * @tc.expected: step2. Match g_document6, Return json with _id. - */ - targetDocument = "{\"_id\": \"6\"}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest039 - * @tc.desc: Test field with true value. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest039, TestSize.Level1) -{ - /** - * @tc.steps: step1. Test field with different true value. Some are true, other are 1. flag is 0. - * @tc.expected: step1. Match the g_document18 and return json with name, item, personInfo.age and color. - */ - const char *filter = "{\"_id\" : \"18\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\":true, \"personInfo.age\": \"\", \"item\":1, \"color\":10, \"nonExist\" : " - "-100}"; - const char *targetDocument = "{\"name\":\"doc18\", \"item\":\"mobile phone\", \"personInfo\":\ - {\"age\":66}, \"color\":\"blue\"}"; - int flag = 0; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - - /** - * @tc.steps: step2. Test field with different true value.Some are false, others are 0. flag is 1. - * @tc.expected: step2. Match g_document18, Return json with name, item, personInfo.age, color and _id. - */ - targetDocument = "{\"_id\" : \"18\", \"name\":\"doc18\",\"item\" : \"mobile phone\",\"personInfo\":\ - {\"age\":66}, \"color\":\"blue\"}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest040 - * @tc.desc: Test field with invalid value. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest040, TestSize.Level1) -{ - /** - * @tc.steps: step1. Test field with invalid value.Value is array. - * @tc.expected: step1. Match the g_document18 and return GRD_INVALID_ARGS. - */ - const char *filter = "{\"_id\" : \"18\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"personInfo\":[true, 1]}"; - int flag = 1; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps: step2. Test field with invalid value.Value is null. - * @tc.expected: step2. Match the g_document18 and return GRD_INVALID_ARGS. - */ - projectionInfo = "{\"personInfo\":null}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps: step3. Test field with invalid value.Value is invalid string. - * @tc.expected: step3. Match the g_document18 and return GRD_INVALID_ARGS. - */ - projectionInfo = "{\"personInfo\":\"invalid string.\"}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest042 - * @tc.desc: Test field with no existed uppercase filter - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest042, TestSize.Level1) -{ - /** - * @tc.steps: step1. Test field with no existed uppercase filter. - * @tc.expected: step1. not match any item. - */ - const char *filter = "{\"_iD\" : \"18\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"Name\":true, \"personInfo.age\": \"\", \"item\":1, \"COLOR\":10, \"nonExist\" : " - "-100}"; - int flag = 0; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps: step2. Test field with upper projection. - * @tc.expected: step2. Match g_document18, Return json with item, personInfo.age, color and _id. - */ - const char *filter1 = "{\"_id\" : \"18\"}"; - const char *targetDocument = "{\"_id\" : \"18\", \"item\" : \"mobile phone\",\"personInfo\":\ - {\"age\":66}}"; - query = { filter1, projectionInfo }; - char *value = nullptr; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest044 - * @tc.desc: Test field with uppercase projection - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest044, TestSize.Level1) -{ - /** - * @tc.steps: step1. Test with false uppercase projection - * @tc.expected: step1. Match g_document18, Return json with item, personInfo.age and _id. - */ - const char *filter = "{\"_id\" : \"18\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"Name\":0, \"personInfo.age\": false, \"personInfo.SCHOOL\": false, \"item\":\ - false, \"COLOR\":false, \"nonExist\" : false}"; - const char *targetDocument = "{\"_id\" : \"18\", \"name\":\"doc18\", \"personInfo\":\ - {\"school\":\"DD\"}, \"color\":\"blue\"}"; - int flag = 0; - Query query = { filter, projectionInfo }; - char *value = nullptr; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest045 - * @tc.desc: Test field with too long collectionName - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest045, TestSize.Level1) -{ - /** - * @tc.steps: step1. Test with false uppercase projection - * @tc.expected: step1. Match g_document18, Return json with item, personInfo.age and _id. - */ - const char *filter = "{\"_id\" : \"18\"}"; - GRD_ResultSet *resultSet = nullptr; - int flag = 0; - Query query = { filter, "{}" }; - string collectionName1(MAX_COLLECTION_NAME, 'a'); - ASSERT_EQ(GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0), GRD_OK); - EXPECT_EQ(GRD_FindDoc(g_db, collectionName1.c_str(), query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - ASSERT_EQ(GRD_DropCollection(g_db, collectionName1.c_str(), 0), GRD_OK); - - string collectionName2(MAX_COLLECTION_NAME + 1, 'a'); - EXPECT_EQ(GRD_FindDoc(g_db, collectionName2.c_str(), query, 1, &resultSet), GRD_OVER_LIMIT); - EXPECT_EQ(GRD_FindDoc(g_db, "", query, 1, &resultSet), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest052 - * @tc.desc: Test field when id string len is large than max - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest052, TestSize.Level1) -{ - /** - * @tc.steps: step1. Test with false uppercase projection - * @tc.expected: step1. Match g_document18, Return json with item, personInfo.age and _id. - */ - const char *filter = "{\"_id\" : \"18\"}"; - GRD_ResultSet *resultSet = nullptr; - int flag = 0; - Query query = { filter, "{}" }; - string collectionName1(MAX_COLLECTION_NAME, 'a'); - ASSERT_EQ(GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0), GRD_OK); - EXPECT_EQ(GRD_FindDoc(g_db, collectionName1.c_str(), query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - ASSERT_EQ(GRD_DropCollection(g_db, collectionName1.c_str(), 0), GRD_OK); - - string collectionName2(MAX_COLLECTION_NAME + 1, 'a'); - EXPECT_EQ(GRD_FindDoc(g_db, collectionName2.c_str(), query, 1, &resultSet), GRD_OVER_LIMIT); - EXPECT_EQ(GRD_FindDoc(g_db, "", query, 1, &resultSet), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest053 - * @tc.desc: Test with invalid flags - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest053, TestSize.Level1) -{ - /** - * @tc.steps: step1. Test with invalid flags which is 3. - * @tc.expected: step1. Return GRD_INVALID_ARGS. - */ - const char *filter = "{\"_id\" : \"18\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 3, &resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps:step1.parameter flags is int_max - * @tc.expected:step1.GRD_INVALID_ARGS - */ - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, INT_MAX, &resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps:step1.parameter flags is INT_MIN - * @tc.expected:step1.GRD_INVALID_ARGS - */ - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, INT_MIN, &resultSet), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest054 - * @tc.desc: Test with null g_db and resultSet, filter. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest054, TestSize.Level1) -{ - /** - * @tc.steps: step1. Test with null g_db. - * @tc.expected: step1. Return GRD_INVALID_ARGS. - */ - const char *filter = "{\"_id\" : \"18\"}"; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(nullptr, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps: step2. Test with null resultSet. - * @tc.expected: step2. Return GRD_INVALID_ARGS. - */ - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, nullptr), GRD_INVALID_ARGS); - - /** - * @tc.steps: step1. Test with query that has two nullptr data. - * @tc.expected: step1. Return GRD_INVALID_ARGS. - */ - query = { nullptr, nullptr }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentFindApiTest055 - * @tc.desc: Find doc, but filter' _id value lens is larger than MAX_ID_LENS - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest055, TestSize.Level1) -{ - /** - * @tc.steps:step1.Find doc, but filter' _id value lens is larger than MAX_ID_LENS - * @tc.expected:step1.GRD_OVER_LIMIT. - */ - string document1 = "{\"_id\" : "; - string document2 = "\""; - string document4 = "\""; - string document5 = "}"; - string document_midlle(MAX_ID_LENS + 1, 'k'); - string filter = document1 + document2 + document_midlle + document4 + document5; - GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{}"; - Query query = { filter.c_str(), projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OVER_LIMIT); - - /** - * @tc.steps:step1.Find doc, filter' _id value lens is equal as MAX_ID_LENS - * @tc.expected:step1.GRD_OK. - */ - string document_midlle2(MAX_ID_LENS, 'k'); - filter = document1 + document2 + document_midlle2 + document4 + document5; - query = { filter.c_str(), projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp deleted file mode 100644 index 3663f29b..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp +++ /dev/null @@ -1,754 +0,0 @@ -/* -* 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. -*/ - -#include - -#include "grd_base/grd_db_api.h" -#include "grd_base/grd_error.h" -#include "grd_document/grd_document_api.h" -using namespace testing::ext; -namespace { -std::string path = "./document.db"; -GRD_DB *g_db = nullptr; -const char *RIGHT_COLLECTION_NAME = "student"; -const char *NO_EXIST_COLLECTION_NAME = "no_exisit"; -const int INT_MAX = 2147483647; -const int INT_MIN = -2147483648; -const int MAX_COLLECTION_LENS = 511; -const int MAX_ID_LENS = 899; - -static void TestInsertDocIntoCertainColl(const char *collectionName, const char *projection, int expectedResult) -{ - /** * @tc.steps: step1. Create Collection - * @tc.expected: step1. GRD_OK - */ - EXPECT_EQ(GRD_CreateCollection(g_db, collectionName, "", 0), expectedResult); - /** - * @tc.steps: step2. Insert projection into colloction. - * @tc.expected: step2. GRD_OK - */ - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, projection, 0), expectedResult); - /** - * @tc.steps: step3. Call GRD_DroCollection to drop the collection. - * @tc.expected: step3. GRD_OK - */ - EXPECT_EQ(GRD_DropCollection(g_db, collectionName, 0), expectedResult); -} - -const char *SetRandomDocument(int i) -{ - string document1 = "{\"_id\" : "; - string document2 = "\""; - string document3 = { '2', '6' }; - string document4 = "\""; - string document5 = ", \"name\" : \"Ori\"}"; - string document = document1 + document2 + document3 + document4 + document5; - return document.c_str(); -} -} // namespace - -class DocumentInsertApiTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; -void DocumentInsertApiTest::SetUpTestCase(void) -{ - std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &g_db); - EXPECT_EQ(status, GRD_OK); - EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); - EXPECT_NE(g_db, nullptr); -} - -void DocumentInsertApiTest::TearDownTestCase(void) -{ - EXPECT_EQ(GRD_DBClose(g_db, 0), GRD_OK); - remove(path.c_str()); -} - -void DocumentInsertApiTest::SetUp(void) {} - -void DocumentInsertApiTest::TearDown(void) {} - -/** - * @tc.name: DocumentInsertApiTest001 - * @tc.desc: Insert documents into collection which dose not exist - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest001, TestSize.Level1) -{ - /** - * @tc.steps:step1.Insert document into collection which dose not exist - * @tc.expected:step1.GRD_INVALID_ARGS - */ - const char *document1 = "{\"_id\" : \"1\", \"name\" : \"Ori\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, NO_EXIST_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentInsertApiTest002 - * @tc.desc: Insert documents into collection which _id is not string - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest002, TestSize.Level1) -{ - /** - * @tc.steps:step1.Insert a document whose _id is integer - * @tc.expected:step1.GRD_INVALID_ARGS - */ - const char *document1 = "{\"_id\" : 2, \"name\" : \"Ori\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step2.Insert a document whose _id is bool - * @tc.expected:step2.GRD_INVALID_ARGS - */ - const char *document2 = "{\"_id\" : true, \"name\" : \"Chuan\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step2.Insert a document whose _id is NULL - * @tc.expected:step2.GRD_INVALID_ARGS - */ - const char *document3 = "{\"_id\" : null, \"name\" : \"Chuan\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step2.Insert a document whose _id is ARRAY - * @tc.expected:step2.GRD_INVALID_ARGS - */ - const char *document4 = "{\"_id\" : [\"2\"], \"name\" : \"Chuan\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document4, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step2.Insert a document whose _id is OBJECT - * @tc.expected:step2.GRD_INVALID_ARGS - */ - const char *document5 = "{\"_id\" : {\"val\" : \"2\"}, \"name\" : \"Chuan\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document5, 0), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentInsertApiTest003 - * @tc.desc: Insert a document whose _id has appeared before - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest003, TestSize.Level1) -{ - /** - * @tc.steps:step1.Insert a document whose _id is string - * @tc.expected:step1.GRD_OK - */ - const char *document1 = "{\"_id\" : \"3\", \"name\" : \"Ori\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); - - /** - * @tc.steps:step2.Insert a document whose _id has appeared before - * @tc.expected:step2.GRD_DATA_CONFLICT - */ - // const char *document2 = "{\"_id\" : \"3\", \"name\" : \"Chuan\"}"; - // EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_DATA_CONFLICT); -} - -/** - * @tc.name: DocumentInsertApiTest004 - * @tc.desc: Test Insert with null parameter. parameter db is NULL - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest004, TestSize.Level1) -{ - /** - * @tc.steps:step1.step1.parameter db is NULL - * @tc.expected:step1.GRD_INVALID_ARGS - */ - const char *document1 = "{\"_id\" : \"4\", \"name\" : \"Ori\"}"; - EXPECT_EQ(GRD_InsertDoc(NULL, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentInsertApiTest005 - * @tc.desc: Test insert with null parameter. parameter collectionName is NULL. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest005, TestSize.Level1) -{ - /** - * @tc.steps:step1.Parameter collectionName is NULL - * @tc.expected:step1.GRD_INVALID_ARGS - */ - const char *document1 = "{\"_id\" : \"5\", \"name\" : \"Ori\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, NULL, document1, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step2.Parameter collectionName is empty string - * @tc.expected:step2.GRD_INVALID_ARGS - */ - const char *document2 = "{\"_id\" : \"5\", \"name\" : \"Chuang\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, "", document2, 0), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentInsertApiTest006 - * @tc.desc: parameter flags is not zero - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest006, TestSize.Level1) -{ - /** - * @tc.steps:step1.parameter flags is not zero - * @tc.expected:step1.GRD_INVALID_ARGS - */ - const char *document1 = "{\"_id\" : \"6\", \"name\" : \"Ori\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 1), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentInsertApiTest007 - * @tc.desc: parameter flags is INT_MAX - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest007, TestSize.Level1) -{ - /** - * @tc.steps:step1.parameter flags is int_max - * @tc.expected:step1.GRD_INVALID_ARGS - */ - const char *document1 = "{\"_id\" : \"7\", \"name\" : \"Ori\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, INT_MAX), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentInsertApiTest008 - * @tc.desc: parameter flags is int_min - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest008, TestSize.Level1) -{ - /** - * @tc.steps:step1.parameter flags is int_min - * @tc.expected:step1.GRD_INVALID_ARGS - */ - const char *document1 = "{\"_id\" : \"8\", \"name\" : \"Ori\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, INT_MIN), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentInsertApiTest009 - * @tc.desc: parameter collectionName and document is NULL or invalid - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest009, TestSize.Level1) -{ - /** - * @tc.steps:step1.parameter collectionName and document is NULL; - * @tc.expected:step1.GRD_INVALID_ARGS - */ - EXPECT_EQ(GRD_InsertDoc(g_db, NULL, NULL, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step2.parameter collectionName is larger than max_collectionName_lens; - * @tc.expected:step2.GRD_OVER_LIMIT - */ - const char *document1 = "{\"_id\" : \"9\", \"name\" : \"Ori\"}"; - std::string collectionName2(MAX_COLLECTION_LENS + 1, 'a'); - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName2.c_str(), document1, 0), GRD_OVER_LIMIT); -} - -/** - * @tc.name: DocumentInsertApiTest010 - * @tc.desc: parameter collectionName contains irregular charactor - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest010, TestSize.Level1) -{ - /** - * @tc.steps:step1.Create Collection whose parameter collectionName contains irregular charactor - * @tc.expected:step1.GRD_OK - */ - const char *collectionName = "collction@!#"; - const char *document1 = "{\"_id\" : \"10\", \"name\" : \"Ori\"}"; - TestInsertDocIntoCertainColl(collectionName, document1, GRD_OK); -} - -/** - * @tc.name: DocumentInsertApiTest011 - * @tc.desc: parameter collectionName is longer than 256 charactors - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest011, TestSize.Level1) -{ - /** - * @tc.steps:step1.Create Collection whose parameter collectionName contains irregular charactor - * @tc.expected:step1.GRD_OK - */ - string collectionName(257, 'k'); - const char *document1 = "{\"_id\" : \"10\", \"name\" : \"Ori\"}"; - TestInsertDocIntoCertainColl(collectionName.c_str(), document1, GRD_OK); -} - -/** - * @tc.name: DocumentInsertApiTest014 - * @tc.desc: Inserted document's JSON depth is larger than 4, which is 5. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest014, TestSize.Level1) -{ - /** - * @tc.steps:step1.document's JSON depth is larger than 4, which is 5. - * @tc.expected:step1.GRD_INVALID_ARGS - */ - const char *document1 = - "{\"level1\" : {\"level2\" : {\"level3\" : {\"level4\": {\"level5\" : 1}}, \"level3_2\" : \"level3_2_val\"\ - }},\"_id\":\"14\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step1.document's JSON depth is larger than 4, which is 5.But with array type. - * @tc.expected:step1.GRD_INVALID_ARGS - */ - const char *document2 = "{\"level1\" : {\"level2\" : {\"level3\" : [{ \"level5\" : \"level5_1val\", \"level5_2\":\ - \"level5_2_val\"}, \"level4_val1\",\"level4_val2\"], \"level3_2\" : \"level3_2_val\"\ - }},\"_id\":\"14\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step1.document's JSON depth is 4 - * @tc.expected:step1.GRD_OK - */ - const char *document3 = - "{\"level1\" : {\"level2\" : {\"level3\" : { \"level4\" : \"level5_1val\"}, \"level3_2\" : \"level3_2_val\"\ - }},\"_id\":\"14\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_OK); -} - -/** - * @tc.name: DocumentInsertApiTest015 - * @tc.desc: Inserted document with all kinds of size - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest015, TestSize.Level1) -{ - /** - * @tc.steps:step1.document's JSON is bigger than 512k - 1 - * @tc.expected:step1.GRD_INVALID_ARGS - */ - string documentPart1 = "{ \"_id\" : \"15\", \"textVal\" : \" "; - string documentPart2 = "\" }"; - string jsonVal = string(512 * 1024 - documentPart1.size() - documentPart2.size(), 'k'); - string document = documentPart1 + jsonVal + documentPart2; - //EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OVER_LIMIT); - /** - * @tc.steps:step2.Insert document's JSON is a large data but lower than 512k - 1 - * @tc.expected:step2.GRD_OK - */ - string jsonVal2 = string(512 * 1024 - 1 - documentPart1.size() - documentPart2.size(), 'k'); - string document2 = documentPart1 + jsonVal2 + documentPart2; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2.c_str(), 0), GRD_OK); -} - -/** - * @tc.name: DocumentInsertApiTest016 - * @tc.desc: document JSON string contains irregular char - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest016, TestSize.Level1) -{ - /** - * @tc.steps:step1.document JSON string contains irregular char. - * @tc.expected:step1.GRD_OK - */ - const char *document1 = "{\"_id\" : \"16\", \"name\" : \"!@#Ori\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); -} - -/** - * @tc.name: DocumentInsertApiTest017 - * @tc.desc: document JSON string contains invalid value type such as BLOB type - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest017, TestSize.Level1) -{ - /** - * @tc.steps:step1.document JSON string contains invalid value type such as BLOB type. - * @tc.expected:step1.GRD_INVALID_FORMAT. - */ - const char *document = "{\"_id\" : \"17\", \"level1\" : {\"level2\" : {\"level3\" : {\"level4\" : x'1234'\ - } } }, \"level1_2\" : \"level1_2Val\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document, 0), GRD_INVALID_FORMAT); -} - -/** - * @tc.name: DocumentInsertApiTest018 - * @tc.desc: The Inserted document is not JSON format - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest018, TestSize.Level1) -{ - /** - * @tc.steps:step1.The Inserted document is not JSON format - * @tc.expected:step1.GRD_INVALID_FORMAT. - */ - const char *document = "some random string not JSON format"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document, 0), GRD_INVALID_FORMAT); -} - -/** - * @tc.name: DocumentInsertApiTest019 - * @tc.desc: Insert a normal documents - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest019, TestSize.Level1) -{ - /** - * @tc.steps:step1.Insert a normal documents which _id is in the end of the string - * @tc.expected:step1.GRD_OK. - */ - const char *document1 = "{\"name\" : \"Jack\", \"age\" : 18, \"friend\" : {\"name\" : \" lucy\"}, \"_id\" : " - "\"19\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); -} - -/** - * @tc.name: DocumentInsertApiTest022 - * @tc.desc: parameter collectionName is equal to 256 charactors - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest022, TestSize.Level1) -{ - /** - * @tc.steps:step1.parameter collectionName is equal to 256 charactors - * @tc.expected:step1.GRD_OK. - */ - string collectionName = string(256, 'k'); - string collectionName1(MAX_COLLECTION_LENS, 'a'); - const char *document1 = "{\"_id\" : \"22\", \"name\" : \"Ori\"}"; - TestInsertDocIntoCertainColl(collectionName.c_str(), document1, GRD_OK); -} - -/** - * @tc.name: DocumentInsertApiTest023 - * @tc.desc: parameter collectionName contains upper & lower case charactors, - * numbers and underline - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest023, TestSize.Level1) -{ - /** - * @tc.steps:step1.parameter collectionName contains upper & lower case charactors, - * numbers and underline - * @tc.expected:step1.GRD_OK. - */ - string collectionName = "Aads_sd__23Asb_"; - const char *document1 = "{\"_id\" : \"23\", \"name\" : \"Ori\"}"; - TestInsertDocIntoCertainColl(collectionName.c_str(), document1, GRD_OK); -} - -/** - * @tc.name: DocumentInsertApiTest024 - * @tc.desc: parameter collectionName's head is GRD_ or GM_SYS_ - * numbers and underline - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest024, TestSize.Level1) -{ - /** - * @tc.steps:step1.parameter collectionName's head is GRD_ - * @tc.expected:step1.GRD_INVALID_FORMAT. - */ - string collectionName = "GRD_collectionName"; - const char *document1 = "{\"_id\" : \"24\", \"name\" : \"Ori\"}"; - GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document1, 0), GRD_INVALID_FORMAT); - - /** - * @tc.steps:step2.parameter collectionName's head is GM_SYS_ - * @tc.expected:step2.GRD_INVALID_FORMAT. - */ - collectionName = "GM_SYS__collectionName"; - const char *document2 = "{\"_id\" : \"24_2\", \"name\" : \"Ori\"}"; - GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document2, 0), GRD_INVALID_FORMAT); - - /** - * @tc.steps:step3.parameter collectionName's head is grd_ - * @tc.expected:step3.GRD_INVALID_FORMAT. - */ - collectionName = "grd_collectionName"; - const char *document3 = "{\"_id\" : \"24_3\", \"name\" : \"Ori\"}"; - GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document3, 0), GRD_INVALID_FORMAT); - - /** - * @tc.steps:step4.parameter collectionName's head is gm_sys_ - * @tc.expected:step4.GRD_INVALID_FORMAT. - */ - collectionName = "gm_sys_collectionName"; - const char *document4 = "{\"_id\" : \"24_4\", \"name\" : \"Ori\"}"; - GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document4, 0), GRD_INVALID_FORMAT); - - /** - * @tc.steps:step5.parameter collectionName's head is gM_sYs_ that has Uppercase and lowercase at the same time. - * @tc.expected:step5.GRD_INVALID_FORMAT. - */ - collectionName = "gM_sYs_collectionName"; - const char *document5 = "{\"_id\" : \"24_5\", \"name\" : \"Ori\"}"; - GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document5, 0), GRD_INVALID_FORMAT); - - /** - * @tc.steps:step6.parameter collectionName's head is gRd_ that has Uppercase and lowercase at the same time. - * @tc.expected:step6.GRD_INVALID_FORMAT. - */ - collectionName = "gRd_collectionName"; - const char *document6 = "{\"_id\" : \"24_6\", \"name\" : \"Ori\"}"; - GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document6, 0), GRD_INVALID_FORMAT); - - /** - * @tc.steps:step7.parameter collectionName's head is grd@ that has no '_' - * @tc.expected:step7.GRD_INVALID_FORMAT. - */ - collectionName = "gRd@collectionName"; - const char *document7 = "{\"_id\" : \"24_7\", \"name\" : \"Ori\"}"; - GRD_CreateCollection(g_db, collectionName.c_str(), "", 0); - EXPECT_EQ(GRD_InsertDoc(g_db, collectionName.c_str(), document7, 0), GRD_OK); -} - -/** - * @tc.name: DocumentInsertApiTest025 - * @tc.desc: Insert document whose depth is 4, which is allowed - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest025, TestSize.Level1) -{ - /** - * @tc.steps:step1.documents JSON depth is 4, which is allowed. - * @tc.expected:step1.GRD_OK. - */ - const char *document1 = "{\"_id\" : \"25_0\", \"level1\" : { \"level2\" : {\"level3\" :\ - {\"level4\" : \"level4Val\" } } } , \"level1_2\" : \"level1_2Val\" }"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); - /** - * @tc.steps:step2.documents JSON depth is exactly 4. - * @tc.expected:step2.GRD_OK. - */ - const char *document2 = - "{\"_id\" : \"25_1\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \"student_info\" : [{\"name\":\"张三\", \ - \"age\" : 18, \"sex\" : \"男\"}, { \"newName1\" : [\"qw\", \"dr\", 0, \"ab\"] }]}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_OK); - /** - * @tc.steps:step3.documents JSON depth is exactly 4, but the last field in array contains leading number - * @tc.expected:step3.GRD_INVALID_ARGS. - */ - const char *document3 = - "{\"_id\" : \"25_2\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \"student_info\" : [{\"name\":\"张三\", \ - \"age\" : 18, \"sex\" : \"男\"}, [\"qw\", \"dr\", 0, \"ab\", {\"0ab\" : null}]]}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step4.documents JSON depth is exactly 5. - * @tc.expected:step4.GRD_INVALID_ARGS. - */ - const char *document4 = - "{\"_id\" : \"25_3\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \"student_info\" : [{\"name\":\"张三\", \ - \"age\" : 18, \"sex\" : \"男\"}, { \"newName1\" : [\"qw\", \"dr\", 0, \"ab\", {\"level5\" : 1}] }]}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document4, 0), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentInsertApiTest026 - * @tc.desc: Insert 100 normal documents continuously - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest, TestSize.Level1) -{ - /** - * @tc.steps:step1.Insert 100 normal documents continuously - * @tc.expected:step1.GRD_OK. - */ - string document1 = "{\"_id\" : "; - string document2 = "\""; - string document4 = "\""; - string document5 = ", \"name\" : \"Ori\"}"; - for (int i = 0; i < 5; i++) { - string document_midlle = "26" + std::to_string(i); - string document = document1 + document2 + document_midlle + document4 + document5; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OK); - } -} - -/** - * @tc.name: DocumentInsertApiTest035 - * @tc.desc: Insert a document whose value contains - * upper &lower case charactors, numbers and underline. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest035, TestSize.Level1) -{ - /** - * @tc.steps:step1.Insert a document whose value contains - * upper &lower case charactors, numbers and underline. - * @tc.expected:step1.GRD_OK. - */ - const char *document1 = "{\"_id\" : \"35\", \"A_aBdk_324_\" : \"value\", \"name\" : \"Chuan\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); - /** - * @tc.steps:step1.Insert a document whose value contains - * upper &lower case charactors, numbers and underline. - * But the field started with number, which is not allowed. - * @tc.expected:step1.GRD_OK. - */ - const char *document2 = "{\"_id\" : \"35_2\", \"1A_aBdk_324_\" : \"value\", \"name\" : \"Chuan\"}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); -} - -/** - * @tc.name: DocumentInsertApiTest036 - * @tc.desc: Insert a document whose value contains - * string, number, bool, null, array and object type - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest036, TestSize.Level1) -{ - /** - * @tc.steps:step1.Insert a document whose value contains - * string, number, bool, null, array and object type - * @tc.expected:step1.GRD_OK. - */ - const char *document1 = "{\"_id\" : \"36_0\", \"stringType\" : \"stringVal\", \"numType\" : 1, \"BoolType\" : true,\ - \"nullType\" : null, \"arrayType\" : [1, 2, 3, 4], \"objectType\" : {\"A\" : 3}}"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); -} - -/** - * @tc.name: DocumentInsertApiTest038 - * @tc.desc: Insert document whose value is over the range of double - * string, number, bool, null, array and object type - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest038, TestSize.Level1) -{ - /** - * @tc.steps:step1.Insert document whose value is over the range of double - * @tc.expected:step1.GRD_INVALID_ARGS. - */ - const char *document1 = R"({"_id" : "38_0", "field2" : 1.79769313486232e308})"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step2.Insert document whose value is over the range of double - * @tc.expected:step2.GRD_INVALID_ARGS. - */ - const char *document2 = R"({"_id" : "38_1", "t1" : {"field2" : 1.79769313486232e308}})"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step3.Insert document whose value is over the range of double - * @tc.expected:step3.GRD_INVALID_ARGS. - */ - const char *document3 = R"({"_id" : "38_2", "t1" : [1, 2, 1.79769313486232e308]})"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step4.Insert document whose value is over the range of double - * @tc.expected:step4.GRD_INVALID_ARGS. - */ - const char *document4 = R"({"_id" : "38_3", "t1" : [1, 2, -1.7976931348623167E+308]})"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); - /** - * @tc.steps:step5.Insert document with minimum double value - * @tc.expected:step5.GRD_INVALID_ARGS. - */ - const char *document5 = R"({"_id" : "38_4", "t1" : [1, 2, -1.79769313486231570E+308]})"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document5, 0), GRD_OK); - /** - * @tc.steps:step6.Insert document with maxium double value - * @tc.expected:step6.GRD_INVALID_ARGS. - */ - const char *document6 = R"({"_id" : "38_5", "t1" : [1, 2, 1.79769313486231570E+308]})"; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document6, 0), GRD_OK); -} - -/** - * @tc.name: DocumentInsertApiTest039 - * @tc.desc: Insert a filter which _id value's lens is larger than MAX_ID_LENS - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest039, TestSize.Level1) -{ - /** - * @tc.steps:step1.Insert a filter which _id value's lens is larger than MAX_ID_LENS. - * @tc.expected:step1.GRD_OVER_LIMIT. - */ - string document1 = "{\"_id\" : "; - string document2 = "\""; - string document4 = "\""; - string document5 = ", \"name\" : \"Ori\"}"; - string document_midlle(MAX_ID_LENS + 1, 'k'); - string document = document1 + document2 + document_midlle + document4 + document5; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OVER_LIMIT); - - /** - * @tc.steps:step1.Insert a filter which _id value's lens is equal as MAX_ID_LENS. - * @tc.expected:step1.GRD_OK. - */ - string document_midlle2(MAX_ID_LENS, 'k'); - document = document1 + document2 + document_midlle2 + document4 + document5; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OK); -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_test_utils.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_test_utils.h deleted file mode 100644 index b1dc3f51..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_test_utils.h +++ /dev/null @@ -1,26 +0,0 @@ -/* -* 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 DOCUMENTDB_TEST_UTILS_H -#define DOCUMENTDB_TEST_UTILS_H -#include - -namespace DocumentDBUnitTest { -class DocumentDBTestUtils { -public: - static int RemoveTestDbFiles(const std::string &dir); -}; -} // namespace DocumentDBUnitTest -#endif // DOCUMENTDB_TEST_UTILS_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.cpp deleted file mode 100644 index f26640ad..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/* -* 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. -*/ - -#include "documentdb_test_utils.h" - -namespace DocumentDBUnitTest { -int DocumentDBTestUtils::RemoveTestDbFiles(const std::string &dir) -{ - (void)remove(dir.c_str()); // TODO: remove dir or files - return 0; -} -} // namespace DocumentDBUnitTest \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.h deleted file mode 100644 index b1dc3f51..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/common/documentdb_test_utils.h +++ /dev/null @@ -1,26 +0,0 @@ -/* -* 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 DOCUMENTDB_TEST_UTILS_H -#define DOCUMENTDB_TEST_UTILS_H -#include - -namespace DocumentDBUnitTest { -class DocumentDBTestUtils { -public: - static int RemoveTestDbFiles(const std::string &dir); -}; -} // namespace DocumentDBUnitTest -#endif // DOCUMENTDB_TEST_UTILS_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp deleted file mode 100644 index 891e61d2..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp +++ /dev/null @@ -1,329 +0,0 @@ -/* -* 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. -*/ - -#include -#include - -#include "doc_errno.h" -#include "documentdb_test_utils.h" -#include "json_common.h" -#include "log_print.h" - -using namespace DocumentDB; -using namespace testing::ext; -using namespace DocumentDBUnitTest; - -class DocumentDBJsonCommonTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void DocumentDBJsonCommonTest::SetUpTestCase(void) {} - -void DocumentDBJsonCommonTest::TearDownTestCase(void) {} - -void DocumentDBJsonCommonTest::SetUp(void) {} - -void DocumentDBJsonCommonTest::TearDown(void) {} - -/** - * @tc.name: OpenDBTest001 - * @tc.desc: Test open document db - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest001, TestSize.Level0) -{ - std::string document = R""({"name":"Tmn","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::string updateDoc = - R""({"name":"Xue","case":{"field1":1,"field2":"string","field3":[1,2,3]},"age":28,"addr":{"city":"shenzhen","postal":518000}})""; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - - EXPECT_EQ(JsonCommon::Append(src, add), E_OK); - GLOGD("result: %s", src.Print().c_str()); - - JsonObject itemCase = src.FindItem({ "case", "field1" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemCase.GetItemValue().GetIntValue(), 1); - - JsonObject itemName = src.FindItem({ "name" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemName.GetItemValue().GetStringValue(), "Xue"); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest002, TestSize.Level0) -{ - std::string document = R""({"name":"Tmn","case":2,"age":[1,2,3],"addr":{"city":"shanghai","postal":200001}})""; - std::string updateDoc = R""({"name":["Xue","Neco","Lip"],"grade":99,"age":18,"addr": - [{"city":"shanghai","postal":200001},{"city":"beijing","postal":100000}]})""; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(JsonCommon::Append(src, add), E_OK); - GLOGD("result: %s", src.Print().c_str()); - - JsonObject itemCase = src.FindItem({ "grade" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemCase.GetItemValue().GetIntValue(), 99); // 99: grade - - JsonObject itemName = src.FindItem({ "name", "1" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemName.GetItemValue().GetStringValue(), "Neco"); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest003, TestSize.Level0) -{ - std::string document = - R""({"name":["Tmn","BB","Alice"],"age":[1,2,3],"addr":[{"city":"shanghai","postal":200001},{"city":"wuhan","postal":430000}]})""; - std::string updateDoc = - R""({"name":["Xue","Neco","Lip"],"age":18,"addr":[{"city":"shanghai","postal":200001},{"city":"beijing","postal":100000}]})""; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(JsonCommon::Append(src, add), E_OK); - - GLOGD("result: %s", src.Print().c_str()); - JsonObject itemCase = src.FindItem({ "addr", "1", "city" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "beijing"); // 99: grade - - JsonObject itemName = src.FindItem({ "name", "1" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemName.GetItemValue().GetStringValue(), "Neco"); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest004, TestSize.Level0) -{ - std::string document = R""({"name":["Tmn","BB","Alice"]})""; - std::string updateDoc = R""({"name.5":"GG"})""; - ; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(JsonCommon::Append(src, add), -E_DATA_CONFLICT); - GLOGD("result: %s", src.Print().c_str()); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest005, TestSize.Level0) -{ - std::string document = R""({"name":["Tmn","BB","Alice"]})""; - std::string updateDoc = R""({"name.2":"GG"})""; - ; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - - EXPECT_EQ(JsonCommon::Append(src, add), E_OK); - GLOGD("result: %s", src.Print().c_str()); - - JsonObject itemCase = src.FindItem({ "name", "2" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "GG"); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest006, TestSize.Level0) -{ - std::string document = R""({"name":{"first":"Tno","last":"moray"}})""; - std::string updateDoc = R""({"name":{"midle.AA":"GG"}})""; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - - EXPECT_EQ(JsonCommon::Append(src, add), E_OK); - GLOGD("result: %s", src.Print().c_str()); - - JsonObject itemCase = src.FindItem({ "name", "midle.AA" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "GG"); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest007, TestSize.Level0) -{ - std::string document = R""({"name":{"first":["XX","CC"],"last":"moray"}})""; - std::string updateDoc = R""({"name.first.0":"LL"})""; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - - EXPECT_EQ(JsonCommon::Append(src, add), E_OK); - GLOGD("result: %s", src.Print().c_str()); - - JsonObject itemCase = src.FindItem({ "name", "first", "0" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "LL"); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest008, TestSize.Level0) -{ - std::string document = R""({"name":{"first":"XX","last":"moray"}})""; - std::string updateDoc = R""({"name":{"first":["XXX","BBB","CCC"]}})""; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - - EXPECT_EQ(JsonCommon::Append(src, add), E_OK); - GLOGD("result: %s", src.Print().c_str()); - - JsonObject itemCase = src.FindItem({ "name", "first", "0" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "XXX"); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest009, TestSize.Level0) -{ - std::string document = R""({"name":{"first":["XXX","BBB","CCC"],"last":"moray"}})""; - std::string updateDoc = R""({"name":{"first":"XX"}})""; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - - EXPECT_EQ(JsonCommon::Append(src, add), E_OK); - GLOGD("result: %s", src.Print().c_str()); - - JsonObject itemCase = src.FindItem({ "name", "first" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "XX"); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest010, TestSize.Level0) -{ - std::string document = R""({"name":{"first":["XXX","BBB","CCC"],"last":"moray"}})""; - std::string updateDoc = R""({"name":{"first":{"XX":"AA"}}})""; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - - EXPECT_EQ(JsonCommon::Append(src, add), E_OK); - GLOGD("result: %s", src.Print().c_str()); - - JsonObject itemCase = src.FindItem({ "name", "first", "XX" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "AA"); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest011, TestSize.Level0) -{ - std::string document = R""({"name":{"first":["XXX","BBB","CCC"],"last":"moray"}})""; - std::string updateDoc = R""({"name.last.AA.B":"Mnado"})""; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - - EXPECT_EQ(JsonCommon::Append(src, add), -E_DATA_CONFLICT); - GLOGD("result: %s", src.Print().c_str()); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest012, TestSize.Level0) -{ - std::string document = R""({"name":["Tmn","BB","Alice"]})""; - std::string updateDoc = R""({"name.first":"GG"})""; - ; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(JsonCommon::Append(src, add), -E_DATA_CONFLICT); - GLOGD("result: %s", src.Print().c_str()); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest013, TestSize.Level0) -{ - std::string document = R""({"name":["Tmn","BB","Alice"]})""; - std::string updateDoc = R""({"name":{"first":"GG"}})""; - ; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(JsonCommon::Append(src, add), E_OK); - GLOGD("result: %s", src.Print().c_str()); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest014, TestSize.Level0) -{ - std::string document = R""({"name":{"first":"Xue","second":"Lang"}})""; - std::string updateDoc = R""({"name.0":"GG"})""; - ; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(JsonCommon::Append(src, add), -E_DATA_CONFLICT); - GLOGD("result: %s", src.Print().c_str()); -} - -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest015, TestSize.Level0) -{ - std::string document = R""({"name":{"first":"Xue","second":"Lang"}})""; - std::string updateDoc = R""({"name.first":["GG","MM"]})""; - ; - - int errCode = E_OK; - JsonObject src = JsonObject::Parse(document, errCode); - EXPECT_EQ(errCode, E_OK); - JsonObject add = JsonObject::Parse(updateDoc, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(JsonCommon::Append(src, add), E_OK); - GLOGD("result: %s", src.Print().c_str()); - - JsonObject itemCase = src.FindItem({ "name", "first", "0" }, errCode); - EXPECT_EQ(errCode, E_OK); - EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "GG"); -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp deleted file mode 100644 index b9ce2849..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* -* 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. -*/ - -#include - -#include "doc_errno.h" -#include "documentdb_test_utils.h" -#include "json_object.h" - -using namespace DocumentDB; -using namespace testing::ext; -using namespace DocumentDBUnitTest; - -class DocumentDBJsonObjectTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void DocumentDBJsonObjectTest::SetUpTestCase(void) {} - -void DocumentDBJsonObjectTest::TearDownTestCase(void) {} - -void DocumentDBJsonObjectTest::SetUp(void) {} - -void DocumentDBJsonObjectTest::TearDown(void) {} - -/** - * @tc.name: OpenDBTest001 - * @tc.desc: Test open document db - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBJsonObjectTest, JsonObjectTest001, TestSize.Level0) -{ - const std::string config = R""({"a":123,"b":{"c":234,"d":"12345"}})""; - - int ret = E_OK; - JsonObject conf = JsonObject::Parse(config, ret); - EXPECT_EQ(ret, E_OK); - - ValueObject obj = conf.GetObjectByPath({ "b", "c" }, ret); - - EXPECT_EQ(obj.GetValueType(), ValueObject::ValueType::VALUE_NUMBER); - EXPECT_EQ(obj.GetIntValue(), 234); -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp index 600c242d..f263282d 100644 --- a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp @@ -16,10 +16,9 @@ #include "load_config_from_data_proxy_node_strategy.h" -#include "datashare_errno.h" - #include "bundle_mgr_proxy.h" #include "common/uri_utils.h" +#include "datashare_errno.h" #include "log_print.h" namespace OHOS::DataShare { diff --git a/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp index 4ab18e5e..5df4f1bb 100644 --- a/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp @@ -49,8 +49,8 @@ private: bool LoadConfigFromDataShareBundleInfoStrategy::LoadConfigFromProfile( const ProfileInfo &profileInfo, std::shared_ptr context) { - std::string storeUri = URIUtils::DATA_SHARE_SCHEMA + context->calledBundleName + "/" + context->calledModuleName + "/" + - context->calledStoreName; + std::string storeUri = URIUtils::DATA_SHARE_SCHEMA + context->calledBundleName + "/" + context->calledModuleName + + "/" + context->calledStoreName; std::string tableUri = storeUri + "/" + context->calledTableName; ConfigData result; for (auto &item : profileInfo.tableConfig) { -- Gitee From 59cc93b0da7103a1b6877279f2599e9aea70255f Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 4 May 2023 19:22:56 +0800 Subject: [PATCH 080/152] f Signed-off-by: hanlu --- services/distributeddataservice/service/BUILD.gn | 1 - 1 file changed, 1 deletion(-) diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 746b371a..524ec57d 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -140,7 +140,6 @@ ohos_shared_library("distributeddatasvc") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share/gaussdb_rd_simple:documentdb", "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb:distributeddb", "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata:distributeddata_inner", ] -- Gitee From 2b005df6821e6f753b7209128f30cdb1cd63dffc Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 4 May 2023 20:30:46 +0800 Subject: [PATCH 081/152] f Signed-off-by: hanlu --- .../service/data_share/common/db_delegate.cpp | 12 +-- .../service/data_share/common/db_delegate.h | 16 ++-- .../service/data_share/common/kv_delegate.cpp | 4 - .../service/data_share/common/kv_delegate.h | 4 +- .../data_share/common/rdb_delegate.cpp | 2 +- .../data_share/common/scheduler_manager.cpp | 18 ++-- .../data_share/common/template_manager.cpp | 88 ++++++++++--------- .../data_share/common/template_manager.h | 14 +-- .../service/data_share/common/uri_utils.h | 7 +- .../service/data_share/data/published_data.h | 2 +- .../data_share/data_share_obs_proxy.cpp | 11 +-- .../data_share/data_share_service_impl.cpp | 8 +- .../data_share/data_share_service_impl.h | 4 +- .../data_share/data_share_service_stub.cpp | 1 + .../data_share/data_share_service_stub.h | 2 - ...d_config_from_data_proxy_node_strategy.cpp | 4 +- ...oad_config_from_data_proxy_node_strategy.h | 3 +- .../general/connect_extension_strategy.cpp | 2 +- .../general/load_config_common_strategy.cpp | 2 +- .../strategies/get_data_strategy.cpp | 1 - 20 files changed, 104 insertions(+), 101 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp index 378396cc..e05c4f15 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -48,24 +48,24 @@ bool KvData::Marshal(DistributedData::Serializable::json &node) const bool Id::Marshal(DistributedData::Serializable::json &node) const { - return SetValue(node["_id"], id_); + return SetValue(node["_id"], id); } bool Id::Unmarshal(const DistributedData::Serializable::json &node) { - return GetValue(node, "_id", id_); + return GetValue(node, "_id", id); } -Id::Id(const std::string &id) : id_(id) {} +Id::Id(const std::string &id) : id(id) {} -VersionData::VersionData(int version) : version_(version) {} +VersionData::VersionData(int version) : version(version) {} bool VersionData::Unmarshal(const DistributedData::Serializable::json &node) { - return GetValue(node, "version", version_); + return GetValue(node, "version", version); } bool VersionData::Marshal(DistributedData::Serializable::json &node) const { - return SetValue(node["version"], version_); + return SetValue(node["version"], version); } } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.h b/services/distributeddataservice/service/data_share/common/db_delegate.h index 5407826d..e214c9fa 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.h +++ b/services/distributeddataservice/service/data_share/common/db_delegate.h @@ -42,33 +42,33 @@ public: }; struct RdbStoreContext { - RdbStoreContext(const std::string &dir, int version) : dir_(dir), version_(version) {} - std::string dir_; - int version_; + RdbStoreContext(const std::string &dir, int version) : dir(dir), version(version) {} + std::string dir; + int version; }; struct Id final: public DistributedData::Serializable { - Id(const std::string &id); + explicit Id(const std::string &id); ~Id() = default; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; private: - std::string id_; + std::string id; }; class VersionData : public DistributedData::Serializable { public: - VersionData(int version); + explicit VersionData(int version); bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; operator int() { - return version_; + return version; }; private: - int version_; + int version; }; struct KvData : public DistributedData::Serializable { diff --git a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp index 6bb653c9..cbbb3fcd 100644 --- a/services/distributeddataservice/service/data_share/common/kv_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/kv_delegate.cpp @@ -39,10 +39,6 @@ bool KvDelegate::Init() return true; } -KvDelegate::~KvDelegate() -{ -} - int32_t KvDelegate::Upsert(const std::string &collectionName, const KvData &value) { std::string id = DistributedData::Serializable::Marshall(*value.GetId()); diff --git a/services/distributeddataservice/service/data_share/common/kv_delegate.h b/services/distributeddataservice/service/data_share/common/kv_delegate.h index 82742960..f01c4174 100644 --- a/services/distributeddataservice/service/data_share/common/kv_delegate.h +++ b/services/distributeddataservice/service/data_share/common/kv_delegate.h @@ -24,8 +24,8 @@ namespace OHOS::DataShare { class KvDelegate final : public KvDBDelegate { public: - KvDelegate(const std::string &path); - virtual ~KvDelegate(); + explicit KvDelegate(const std::string &path); + ~KvDelegate() = default; int32_t Upsert(const std::string &collectionName, const KvData &value) override; int32_t DeleteById(const std::string &collectionName, const Id &id) override; int32_t Get(const std::string &collectionName, const Id &id, std::string &value) override; diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index d4187e2f..055e9e21 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -109,7 +109,7 @@ std::shared_ptr RdbDelegate::Query( } class ResultSetJsonFormatter final: public DistributedData::Serializable { public: - ResultSetJsonFormatter(const std::shared_ptr &resultSet) + explicit ResultSetJsonFormatter(const std::shared_ptr &resultSet) : resultSet(resultSet) { } diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp index 23da5d84..853ec27b 100644 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp @@ -52,7 +52,7 @@ void SchedulerManager::Execute(const Key &key, const std::string &rdbDir, int ve int errCode = 0; auto delegate = DBDelegate::Create(rdbDir, version, errCode, true); if (delegate == nullptr) { - ZLOGE("malloc fail %{public}s", DistributedData::Anonymous::Change(key.uri_).c_str()); + ZLOGE("malloc fail %{public}s", DistributedData::Anonymous::Change(key.uri).c_str()); return; } ExecuteSchedulerSQL(key, delegate); @@ -78,9 +78,9 @@ void SchedulerManager::SetTimer(const RdbStoreContext &rdbContext, const std::st std::time_t now = time(nullptr); auto taskId = scheduler_->At(TaskScheduler::Clock::now() + std::chrono::seconds(reminderTime - now), [&]() { // 1. execute schedulerSQL in next time - Execute(key, rdbContext.dir_, rdbContext.version_); + Execute(key, rdbContext.dir, rdbContext.version); // 2. notify - RdbSubscriberManager::GetInstance().EmitByKey(key, rdbContext.dir_, rdbContext.version_); + RdbSubscriberManager::GetInstance().EmitByKey(key, rdbContext.dir, rdbContext.version); }); if (taskId == TaskScheduler::INVALID_TASK_ID) { ZLOGE("create timer failed, over the max capacity"); @@ -92,24 +92,24 @@ void SchedulerManager::SetTimer(const RdbStoreContext &rdbContext, const std::st void SchedulerManager::ExecuteSchedulerSQL(const Key &key, std::shared_ptr delegate) { Template tpl; - if (!TemplateManager::GetInstance().GetTemplate(key.uri_, key.subscriberId_, key.bundleName_, tpl)) { + if (!TemplateManager::GetInstance().GetTemplate(key.uri, key.subscriberId, key.bundleName, tpl)) { ZLOGE("template undefined, %{public}s, %{public}" PRId64 ", %{public}s", - DistributedData::Anonymous::Change(key.uri_).c_str(), key.subscriberId_, key.bundleName_.c_str()); + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); return; } if (tpl.scheduler_.empty()) { ZLOGW("template scheduler_ empty, %{public}s, %{public}" PRId64 ", %{public}s", - DistributedData::Anonymous::Change(key.uri_).c_str(), key.subscriberId_, key.bundleName_.c_str()); + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); return; } - if (!GenRemindTimerFuncParams(key.uri_, key.subscriberId_, key.bundleName_, tpl.scheduler_)) { + if (!GenRemindTimerFuncParams(key.uri, key.subscriberId, key.bundleName, tpl.scheduler_)) { return; } int errCode = delegate->ExecuteSql(tpl.scheduler_); if (errCode != E_OK) { ZLOGE("Execute schedulerSql failed, %{public}s, %{public}" PRId64 ", %{public}s", - DistributedData::Anonymous::Change(key.uri_).c_str(), key.subscriberId_, key.bundleName_.c_str()); + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); } } @@ -137,7 +137,7 @@ void SchedulerManager::RemoveTimer(const Key &key) auto it = timerCache_.find(key); if (it != timerCache_.end()) { ZLOGD("RemoveTimer %{public}s %{public}s %{public}" PRId64, - DistributedData::Anonymous::Change(key.uri_).c_str(), key.bundleName_.c_str(), key.subscriberId_); + DistributedData::Anonymous::Change(key.uri).c_str(), key.bundleName.c_str(), key.subscriberId); scheduler_->Remove(it->second); timerCache_.erase(key); if (timerCache_.empty()) { diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index 327441c3..47fc305c 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -83,13 +83,13 @@ bool TemplateManager::DelTemplate(const std::string &uri, const TemplateId &tplI } Key::Key(const std::string &uri, const int64_t subscriberId, const std::string &bundleName) - : uri_(uri), subscriberId_(subscriberId), bundleName_(bundleName) + : uri(uri), subscriberId(subscriberId), bundleName(bundleName) { } bool Key::operator==(const Key &rhs) const { - return uri_ == rhs.uri_ && subscriberId_ == rhs.subscriberId_ && bundleName_ == rhs.bundleName_; + return uri == rhs.uri && subscriberId == rhs.subscriberId && bundleName == rhs.bundleName; } bool Key::operator!=(const Key &rhs) const @@ -98,15 +98,19 @@ bool Key::operator!=(const Key &rhs) const } bool Key::operator<(const Key &rhs) const { - if (uri_ < rhs.uri_) + if (uri < rhs.uri) { return true; - if (rhs.uri_ < uri_) + } + if (rhs.uri < uri) { return false; - if (subscriberId_ < rhs.subscriberId_) + } + if (subscriberId < rhs.subscriberId) { return true; - if (rhs.subscriberId_ < subscriberId_) + } + if (rhs.subscriberId < subscriberId) { return false; - return bundleName_ < rhs.bundleName_; + } + return bundleName < rhs.bundleName; } bool Key::operator>(const Key &rhs) const { @@ -141,7 +145,7 @@ int RdbSubscriberManager::AddRdbSubscriber(const std::string &uri, const Templat int result = E_OK; Key key(uri, tplId.subscriberId_, tplId.bundleName_); rdbCache_.Compute(key, [&observer, &context, &result, this](const auto &key, std::vector &value) { - ZLOGI("add subscriber, uri %{private}s tokenId %{public}d", key.uri_.c_str(), context->callerTokenId); + ZLOGI("add subscriber, uri %{private}s tokenId %{public}d", key.uri.c_str(), context->callerTokenId); ObserverNode observerNode(observer, context->callerTokenId); std::vector node({ observerNode }); result = Notify(key, node, context->calledSourceDir, context->version); @@ -163,7 +167,7 @@ int RdbSubscriberManager::DelRdbSubscriber( Key key(uri, tplId.subscriberId_, tplId.bundleName_); auto result = rdbCache_.ComputeIfPresent(key, [&callerTokenId, this](const auto &key, std::vector &value) { - ZLOGI("delete subscriber, uri %{public}s tokenId %{public}d", key.uri_.c_str(), callerTokenId); + ZLOGI("delete subscriber, uri %{private}s tokenId %{public}d", key.uri.c_str(), callerTokenId); for (auto it = value.begin(); it != value.end();) { if (it->callerTokenId == callerTokenId) { ZLOGI("erase start"); @@ -254,7 +258,7 @@ void RdbSubscriberManager::Emit(const std::string &uri, std::shared_ptr return; } rdbCache_.ForEach([&uri, &context, this](const Key &key, std::vector &val) { - if (key.uri_ != uri) { + if (key.uri != uri) { return false; } Notify(key, val, context->calledSourceDir, context->version); @@ -266,7 +270,7 @@ std::vector RdbSubscriberManager::GetKeysByUri(const std::string &uri) { std::vector results; rdbCache_.ForEach([&uri, &results](const Key &key, std::vector &val) { - if (key.uri_ != uri) { + if (key.uri != uri) { return false; } results.emplace_back(key); @@ -277,7 +281,7 @@ std::vector RdbSubscriberManager::GetKeysByUri(const std::string &uri) void RdbSubscriberManager::EmitByKey(const Key &key, const std::string &rdbPath, int version) { - if (!URIUtils::IsDataProxyURI(key.uri_)) { + if (!URIUtils::IsDataProxyURI(key.uri)) { return; } rdbCache_.ComputeIfPresent(key, [&rdbPath, &version, this](const Key &key, std::vector &val) { @@ -314,22 +318,22 @@ int RdbSubscriberManager::Notify( const Key &key, std::vector &val, const std::string &rdbDir, int rdbVersion) { Template tpl; - if (!TemplateManager::GetInstance().GetTemplate(key.uri_, key.subscriberId_, key.bundleName_, tpl)) { + if (!TemplateManager::GetInstance().GetTemplate(key.uri, key.subscriberId, key.bundleName, tpl)) { ZLOGE("template undefined, %{public}s, %{public}" PRId64 ", %{public}s", - DistributedData::Anonymous::Change(key.uri_).c_str(), key.subscriberId_, key.bundleName_.c_str()); + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); return E_TEMPLATE_NOT_EXIST; } int errCode; auto delegate = DBDelegate::Create(rdbDir, rdbVersion, errCode); if (delegate == nullptr) { - ZLOGE("malloc fail %{public}s %{public}s", DistributedData::Anonymous::Change(key.uri_).c_str(), - key.bundleName_.c_str()); + ZLOGE("malloc fail %{public}s %{public}s", DistributedData::Anonymous::Change(key.uri).c_str(), + key.bundleName.c_str()); return errCode; } RdbChangeNode changeNode; - changeNode.uri_ = key.uri_; - changeNode.templateId_.subscriberId_ = key.subscriberId_; - changeNode.templateId_.bundleName_ = key.bundleName_; + changeNode.uri_ = key.uri; + changeNode.templateId_.subscriberId_ = key.subscriberId; + changeNode.templateId_.bundleName_ = key.bundleName; for (const auto &predicate : tpl.predicates_) { JsonFormatter formatter(predicate.key_, delegate->Query(predicate.selectSql_)); changeNode.data_.emplace_back(DistributedData::Serializable::Marshall(formatter)); @@ -354,9 +358,9 @@ int PublishedDataSubscriberManager::AddSubscriber(const std::string &key, const const int64_t subscriberId, const sptr observer, const uint32_t callerTokenId) { PublishedDataKey publishedDataKey(key, callerBundleName, subscriberId); - publishedDataCache_.Compute(publishedDataKey, + publishedDataCache.Compute(publishedDataKey, [&observer, &callerTokenId, this](const PublishedDataKey &key, std::vector &value) { - ZLOGI("add publish subscriber, uri %{private}s tokenId %{public}d", key.key_.c_str(), callerTokenId); + ZLOGI("add publish subscriber, uri %{private}s tokenId %{public}d", key.key.c_str(), callerTokenId); value.emplace_back(observer, callerTokenId); return true; }); @@ -368,7 +372,7 @@ int PublishedDataSubscriberManager::DelSubscriber(const std::string &uri, const { PublishedDataKey key(uri, callerBundleName, subscriberId); auto result = - publishedDataCache_.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { + publishedDataCache.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { for (auto it = value.begin(); it != value.end();) { if (it->callerTokenId == callerTokenId) { it = value.erase(it); @@ -386,7 +390,7 @@ int PublishedDataSubscriberManager::DisableSubscriber(const std::string &uri, co { PublishedDataKey key(uri, callerBundleName, subscriberId); auto result = - publishedDataCache_.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { + publishedDataCache.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { for (auto it = value.begin(); it != value.end(); it++) { if (it->callerTokenId == callerTokenId) { it->enabled = false; @@ -402,7 +406,7 @@ int PublishedDataSubscriberManager::EnableSubscriber(const std::string &uri, con { PublishedDataKey key(uri, callerBundleName, subscriberId); auto result = - publishedDataCache_.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { + publishedDataCache.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { for (auto it = value.begin(); it != value.end(); it++) { if (it->callerTokenId == callerTokenId) { it->enabled = true; @@ -420,20 +424,20 @@ void PublishedDataSubscriberManager::Emit(std::vector keys, co // key is bundleName, value is change node std::map publishedResult; std::map, std::vector> callbacks; - publishedDataCache_.ForEach([&keys, &status, &observer, &publishedResult, &callbacks]( + publishedDataCache.ForEach([&keys, &status, &observer, &publishedResult, &callbacks]( const PublishedDataKey &key, std::vector &val) { for (auto &data : keys) { if (key != data) { continue; } - publishedResult[key].subscriberId_ = data.subscriberId_; - publishedResult[key].key_ = data.key_; - PublishedData publishedData(data.key_, data.bundleName_, data.subscriberId_); + publishedResult[key].subscriberId_ = data.subscriberId; + publishedResult[key].key_ = data.key; + PublishedData publishedData(data.key, data.bundleName, data.subscriberId); status = PublishedData::Query( DistributedData::Serializable::Marshall(*publishedData.GetId()), publishedResult[key].value_); if (status != E_OK) { - ZLOGE("query fail %{public}s %{public}s %{public}" PRId64, data.bundleName_.c_str(), data.key_.c_str(), - data.subscriberId_); + ZLOGE("query fail %{public}s %{public}s %{public}" PRId64, data.bundleName.c_str(), data.key.c_str(), + data.subscriberId); publishedResult.erase(key); continue; } @@ -467,27 +471,31 @@ void PublishedDataSubscriberManager::Emit(std::vector keys, co } PublishedDataKey::PublishedDataKey(const std::string &key, const std::string &bundleName, const int64_t subscriberId) - : key_(key), bundleName_(bundleName), subscriberId_(subscriberId) + : key(key), bundleName(bundleName), subscriberId(subscriberId) { /* private published data can use key as simple uri */ /* etc: datashareproxy://{bundleName}/meeting can use meeting replaced */ /* if key is normal uri, bundleName is from uri */ - if (URIUtils::IsDataProxyURI(key_)) { - URIUtils::GetBundleNameFromProxyURI(key_, bundleName_); + if (URIUtils::IsDataProxyURI(key)) { + URIUtils::GetBundleNameFromProxyURI(key, bundleName); } } bool PublishedDataKey::operator<(const PublishedDataKey &rhs) const { - if (key_ < rhs.key_) + if (key < rhs.key) { return true; - if (rhs.key_ < key_) + } + if (rhs.key < key) { return false; - if (bundleName_ < rhs.bundleName_) + } + if (bundleName < rhs.bundleName) { return true; - if (rhs.bundleName_ < bundleName_) + } + if (rhs.bundleName < bundleName) { return false; - return subscriberId_ < rhs.subscriberId_; + } + return subscriberId < rhs.subscriberId; } bool PublishedDataKey::operator>(const PublishedDataKey &rhs) const @@ -507,7 +515,7 @@ bool PublishedDataKey::operator>=(const PublishedDataKey &rhs) const bool PublishedDataKey::operator==(const PublishedDataKey &rhs) const { - return key_ == rhs.key_ && bundleName_ == rhs.bundleName_ && subscriberId_ == rhs.subscriberId_; + return key == rhs.key && bundleName == rhs.bundleName && subscriberId == rhs.subscriberId; } bool PublishedDataKey::operator!=(const PublishedDataKey &rhs) const @@ -525,4 +533,4 @@ PublishedDataSubscriberManager::ObserverNode::ObserverNode( : observer(observer), callerTokenId(callerTokenId) { } -} // namespace OHOS::DataShare \ No newline at end of file +} // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/common/template_manager.h b/services/distributeddataservice/service/data_share/common/template_manager.h index 4929b04f..6dd19a48 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.h +++ b/services/distributeddataservice/service/data_share/common/template_manager.h @@ -32,9 +32,9 @@ struct Key { bool operator>(const Key &rhs) const; bool operator<=(const Key &rhs) const; bool operator>=(const Key &rhs) const; - const std::string uri_; - const int64_t subscriberId_; - const std::string bundleName_; + const std::string uri; + const int64_t subscriberId; + const std::string bundleName; }; class TemplateManager { public: @@ -83,9 +83,9 @@ struct PublishedDataKey { bool operator>=(const PublishedDataKey &rhs) const; bool operator==(const PublishedDataKey &rhs) const; bool operator!=(const PublishedDataKey &rhs) const; - std::string key_; - std::string bundleName_; - int64_t subscriberId_; + std::string key; + std::string bundleName; + int64_t subscriberId; }; class PublishedDataSubscriberManager { @@ -110,7 +110,7 @@ private: bool enabled = true; }; PublishedDataSubscriberManager() = default; - ConcurrentMap> publishedDataCache_; + ConcurrentMap> publishedDataCache; }; } // namespace OHOS::DataShare #endif diff --git a/services/distributeddataservice/service/data_share/common/uri_utils.h b/services/distributeddataservice/service/data_share/common/uri_utils.h index 9366ba38..615b3ef7 100644 --- a/services/distributeddataservice/service/data_share/common/uri_utils.h +++ b/services/distributeddataservice/service/data_share/common/uri_utils.h @@ -31,11 +31,12 @@ public: static bool GetBundleNameFromProxyURI(const std::string &uri, std::string &bundleName); static bool GetUserIdFromProxyURI(const std::string &uri, int32_t &user); static bool IsDataProxyURI(const std::string &uri); - const static std::string DATA_SHARE_SCHEMA; - const static std::string DATA_PROXY_SCHEMA; + static const std::string DATA_SHARE_SCHEMA; + static const std::string DATA_PROXY_SCHEMA; private: - enum PATH_PARAM : int32_t { BUNDLE_NAME = 0, + enum PATH_PARAM : int32_t { + BUNDLE_NAME = 0, MODULE_NAME, STORE_NAME, TABLE_NAME, diff --git a/services/distributeddataservice/service/data_share/data/published_data.h b/services/distributeddataservice/service/data_share/data/published_data.h index d083ee92..b90c1ea5 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.h +++ b/services/distributeddataservice/service/data_share/data/published_data.h @@ -41,7 +41,7 @@ struct PublishedDataNode final : public DistributedData::Serializable { struct PublishedData final : public KvData { static std::vector Query(const std::string &bundleName); static int32_t Query(const std::string &filter, std::variant, std::string> &publishedData); - PublishedData(const std::string &key = "", const std::string &bundleName = "", int64_t subscriberId = 0, + explicit PublishedData(const std::string &key = "", const std::string &bundleName = "", int64_t subscriberId = 0, const std::variant, std::string> &value = "", const int version = 0); ~PublishedData() = default; std::shared_ptr GetId() const override diff --git a/services/distributeddataservice/service/data_share/data_share_obs_proxy.cpp b/services/distributeddataservice/service/data_share/data_share_obs_proxy.cpp index ee9b6015..fe9fe695 100644 --- a/services/distributeddataservice/service/data_share/data_share_obs_proxy.cpp +++ b/services/distributeddataservice/service/data_share/data_share_obs_proxy.cpp @@ -14,7 +14,7 @@ void RdbObserverProxy::OnChangeFromRdb(RdbChangeNode &changeNode) { MessageParcel parcel; if (!parcel.WriteInterfaceToken(RdbObserverProxy::GetDescriptor())) { - return ; + return; } if (!ITypesUtil::Marshal(parcel, changeNode)) { @@ -33,10 +33,11 @@ void RdbObserverProxy::OnChangeFromRdb(RdbChangeNode &changeNode) } } -void PublishedDataObserverProxy::OnChangeFromPublishedData(PublishedDataChangeNode &changeNode) { +void PublishedDataObserverProxy::OnChangeFromPublishedData(PublishedDataChangeNode &changeNode) +{ MessageParcel parcel; if (!parcel.WriteInterfaceToken(PublishedDataObserverProxy::GetDescriptor())) { - return ; + return; } if (!ITypesUtil::Marshal(parcel, changeNode)) { @@ -54,5 +55,5 @@ void PublishedDataObserverProxy::OnChangeFromPublishedData(PublishedDataChangeNo return; } } -} -} // namespace OHOS::DataShare \ No newline at end of file +} // namespace DataShare +} // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 34f913e6..7f24e6fe 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -252,7 +252,7 @@ std::vector DataShareServiceImpl::SubscribePublishedData(const auto context = std::make_shared(uri); PublishedDataKey key(uri, callerBundleName, subscriberId); context->callerBundleName = callerBundleName; - context->calledBundleName = key.bundleName_; + context->calledBundleName = key.bundleName; result = SubscribeStrategy::Execute( context, [&subscriberId, &observer, &callerBundleName, &context]() -> bool { return PublishedDataSubscriberManager::GetInstance().AddSubscriber( @@ -277,7 +277,7 @@ std::vector DataShareServiceImpl::UnSubscribePublishedData(cons auto context = std::make_shared(uri); PublishedDataKey key(uri, callerBundleName, subscriberId); context->callerBundleName = callerBundleName; - context->calledBundleName = key.bundleName_; + context->calledBundleName = key.bundleName; results.emplace_back( uri, SubscribeStrategy::Execute(context, [&subscriberId, &callerBundleName, &context]() -> bool { return PublishedDataSubscriberManager::GetInstance().DelSubscriber( @@ -299,7 +299,7 @@ std::vector DataShareServiceImpl::EnableSubscribePublishedData( auto context = std::make_shared(uri); PublishedDataKey key(uri, callerBundleName, subscriberId); context->callerBundleName = callerBundleName; - context->calledBundleName = key.bundleName_; + context->calledBundleName = key.bundleName; result = SubscribeStrategy::Execute(context, [&subscriberId, &callerBundleName, &context]() -> bool { return PublishedDataSubscriberManager::GetInstance().EnableSubscriber( context->uri, callerBundleName, subscriberId, context->callerTokenId); @@ -323,7 +323,7 @@ std::vector DataShareServiceImpl::DisableSubscribePublishedData auto context = std::make_shared(uri); PublishedDataKey key(uri, callerBundleName, subscriberId); context->callerBundleName = callerBundleName; - context->calledBundleName = key.bundleName_; + context->calledBundleName = key.bundleName; results.emplace_back( uri, SubscribeStrategy::Execute(context, [&subscriberId, &callerBundleName, &context]() -> bool { return PublishedDataSubscriberManager::GetInstance().DisableSubscriber( diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.h b/services/distributeddataservice/service/data_share/data_share_service_impl.h index a6929282..a0fc8069 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -38,8 +38,8 @@ public: int32_t DelTemplate(const std::string &uri, const int64_t subscriberId) override; std::vector Publish(const Data &data, const std::string &bundleNameOfProvider) override; Data GetData(const std::string &bundleNameOfProvider) override; - std::vector SubscribeRdbData( - const std::vector &uris, const TemplateId &id, const sptr observer) override; + std::vector SubscribeRdbData(const std::vector &uris, + const TemplateId &id, const sptr observer) override; std::vector UnSubscribeRdbData( const std::vector &uris, const TemplateId &id) override; std::vector EnableSubscribeRdbData( diff --git a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp index 7a3818d4..56f91b53 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp @@ -165,6 +165,7 @@ int32_t DataShareServiceStub::OnRemoteGetData(MessageParcel &data, MessageParcel ZLOGW("read device list failed."); return -1; } + ZLOGE("hanlu bundleName %{public}s", bundleName.c_str()); auto results = GetData(bundleName); if (!ITypesUtil::Marshal(reply, results.datas_)) { ZLOGE("ITypesUtil::Marshal(reply, results) failed"); diff --git a/services/distributeddataservice/service/data_share/data_share_service_stub.h b/services/distributeddataservice/service/data_share/data_share_service_stub.h index 2b3e89c7..05e4e060 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_stub.h +++ b/services/distributeddataservice/service/data_share/data_share_service_stub.h @@ -43,8 +43,6 @@ private: int32_t OnRemoteUnSubscribePublishedData(MessageParcel& data, MessageParcel& reply); int32_t OnRemoteEnableSubscribePublishedData(MessageParcel& data, MessageParcel& reply); int32_t OnRemoteDisableSubscribePublishedData(MessageParcel& data, MessageParcel& reply); - - using RequestHandle = int (DataShareServiceStub::*)(MessageParcel &, MessageParcel &); static constexpr RequestHandle HANDLERS[DATA_SHARE_SERVICE_CMD_MAX] = { &DataShareServiceStub::OnRemoteInsert, diff --git a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp index f263282d..d2ad30b5 100644 --- a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp @@ -29,7 +29,7 @@ bool LoadConfigFromDataProxyNodeStrategy::operator()(std::shared_ptr co } context->type = DataProperties::PUBLISHED_DATA_TYPE; if (!BundleMgrProxy::GetInstance()->GetBundleInfoFromBMS( - context->calledBundleName, context->currentUserId, context->bundleInfo)) { + context->calledBundleName, context->currentUserId, context->bundleInfo)) { ZLOGE("GetBundleInfoFromBMS failed! bundleName: %{public}s", context->calledBundleName.c_str()); return false; } @@ -48,7 +48,7 @@ bool LoadConfigFromDataProxyNodeStrategy::operator()(std::shared_ptr co bool isCompressed = !hapModuleInfo.hapPath.empty(); std::string resourcePath = isCompressed ? hapModuleInfo.hapPath : hapModuleInfo.resourcePath; if (!DataShareProfileInfo::GetDataPropertiesFromProxyDatas( - proxyData, resourcePath, isCompressed, properties)) { + proxyData, resourcePath, isCompressed, properties)) { return true; } GetContextInfoFromDataProperties(properties, hapModuleInfo.moduleName, context); diff --git a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.h b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.h index 8c8066cf..db26c9aa 100644 --- a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.h +++ b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.h @@ -27,8 +27,7 @@ public: bool operator()(std::shared_ptr context) override; private: - enum PATH_PARAMS : int32_t - { + enum PATH_PARAMS : int32_t { STORE_NAME = 0, TABLE_NAME, PARAM_SIZE diff --git a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp index cca52b21..446d0532 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp @@ -89,7 +89,7 @@ bool ConnectExtensionStrategy::Execute( return true; } int waitTime = 0; - constexpr static int RETRY_TIME = 500; + static constexpr int RETRY_TIME = 500; while (!isFinished()) { if (waitTime > maxWaitTimeMs) { ZLOGE("cannot finish work"); diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp index 0602a1f7..b911ef2d 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp @@ -26,7 +26,7 @@ bool LoadConfigCommonStrategy::operator()(std::shared_ptr context) context->callerTokenId = IPCSkeleton::GetCallingTokenID(); context->currentUserId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(context->callerTokenId); // single app, userId is in uri - if (context->currentUserId == 0) { + if (context->currentUserId == 0) { URIUtils::GetUserIdFromProxyURI(context->uri, context->currentUserId); ZLOGI("user uri's userId %{public}d", context->currentUserId); } diff --git a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp index 266fcbb0..6771727f 100644 --- a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp @@ -36,7 +36,6 @@ Data GetDataStrategy::Execute(std::shared_ptr context) } std::vector queryResult = PublishedData::Query(context->calledBundleName); return Convert(queryResult); - } Strategy *GetDataStrategy::GetStrategy() -- Gitee From ee71bd4b104c609ead856a6af24792368d471cb1 Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 4 May 2023 20:38:27 +0800 Subject: [PATCH 082/152] f Signed-off-by: hanlu --- .../service/data_share/common/scheduler_manager.cpp | 8 ++++---- .../service/data_share/common/scheduler_manager.h | 6 +++--- .../service/data_share/data/published_data.cpp | 6 +++--- .../service/data_share/data/published_data.h | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp index 853ec27b..9a694d70 100644 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp @@ -94,12 +94,12 @@ void SchedulerManager::ExecuteSchedulerSQL(const Key &key, std::shared_ptrExecuteSql(tpl.scheduler_); if (errCode != E_OK) { ZLOGE("Execute schedulerSql failed, %{public}s, %{public}" PRId64 ", %{public}s", - DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); + DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); } } @@ -137,7 +137,7 @@ void SchedulerManager::RemoveTimer(const Key &key) auto it = timerCache_.find(key); if (it != timerCache_.end()) { ZLOGD("RemoveTimer %{public}s %{public}s %{public}" PRId64, - DistributedData::Anonymous::Change(key.uri).c_str(), key.bundleName.c_str(), key.subscriberId); + DistributedData::Anonymous::Change(key.uri).c_str(), key.bundleName.c_str(), key.subscriberId); scheduler_->Remove(it->second); timerCache_.erase(key); if (timerCache_.empty()) { diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.h b/services/distributeddataservice/service/data_share/common/scheduler_manager.h index dbe21438..813f4557 100644 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.h +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.h @@ -36,8 +36,8 @@ private: static constexpr size_t TIME_TASK_NUM = 10; SchedulerManager() = default; ~SchedulerManager() = default; - bool GenRemindTimerFuncParams(const std::string &uri, int64_t subscriberId, const std::string &bundleName, - std::string &schedulerSQL); + bool GenRemindTimerFuncParams( + const std::string &uri, int64_t subscriberId, const std::string &bundleName, std::string &schedulerSQL); void ExecuteSchedulerSQL(const Key &key, std::shared_ptr delegate = nullptr); std::recursive_mutex mutex_; @@ -45,4 +45,4 @@ private: std::shared_ptr scheduler_; }; } // namespace OHOS::DataShare -#endif //SCHEDULER_MANAGER_H +#endif // SCHEDULER_MANAGER_H diff --git a/services/distributeddataservice/service/data_share/data/published_data.cpp b/services/distributeddataservice/service/data_share/data/published_data.cpp index 828eca25..be7bb744 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.cpp +++ b/services/distributeddataservice/service/data_share/data/published_data.cpp @@ -39,8 +39,8 @@ bool PublishedData::Unmarshal(const DistributedData::Serializable::json &node) } PublishedData::PublishedData(const std::string &key, const std::string &bundleName, int64_t subscriberId, - const std::variant, std::string> &value, const int version) - : value(key, bundleName, subscriberId, value, version) + const std::variant, std::string> &inputValue, const int version) + : value(key, bundleName, subscriberId, inputValue, version) { } @@ -136,7 +136,7 @@ bool PublishedDataNode::Unmarshal(const DistributedData::Serializable::json &nod } PublishedDataNode::PublishedDataNode(const std::string &key, const std::string &bundleName, int64_t subscriberId, - const std::variant, std::string> &value, const VersionData &version) + const std::variant, std::string> &value, const int version) : key(key), bundleName(bundleName), subscriberId(subscriberId), value(value), version(version) { } diff --git a/services/distributeddataservice/service/data_share/data/published_data.h b/services/distributeddataservice/service/data_share/data/published_data.h index b90c1ea5..0f286482 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.h +++ b/services/distributeddataservice/service/data_share/data/published_data.h @@ -27,7 +27,7 @@ enum PublishedDataType : int32_t { struct PublishedDataNode final : public DistributedData::Serializable { PublishedDataNode(const std::string &key, const std::string &bundleName, int64_t subscriberId, - const std::variant, std::string> &value, const VersionData &version); + const std::variant, std::string> &value, const int version); ~PublishedDataNode() = default; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; -- Gitee From 62e287390444df6deeeb7707fcf80bdb6ce9131f Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 4 May 2023 21:25:36 +0800 Subject: [PATCH 083/152] f Signed-off-by: hanlu --- .../service/data_share/common/template_manager.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index 47fc305c..bbcb7166 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -470,8 +470,8 @@ void PublishedDataSubscriberManager::Emit(std::vector keys, co } } -PublishedDataKey::PublishedDataKey(const std::string &key, const std::string &bundleName, const int64_t subscriberId) - : key(key), bundleName(bundleName), subscriberId(subscriberId) +PublishedDataKey::PublishedDataKey(const std::string &key, const std::string &bundle, const int64_t subscriberId) + : key(key), bundleName(bundle), subscriberId(subscriberId) { /* private published data can use key as simple uri */ /* etc: datashareproxy://{bundleName}/meeting can use meeting replaced */ -- Gitee From c03e9a2254a6747cb7b325095231e4b9d5cceffb Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 4 May 2023 21:44:23 +0800 Subject: [PATCH 084/152] f Signed-off-by: hanlu --- .../service/data_share/common/context.h | 9 --------- .../service/data_share/common/rdb_delegate.cpp | 1 + .../data_share/common/template_manager.cpp | 4 ++-- .../service/data_share/common/template_manager.h | 15 ++++++--------- .../service/data_share/data/published_data.h | 2 +- .../service/data_share/data/template_data.h | 2 +- .../service/data_share/data_share_obs_proxy.h | 2 +- .../load_config_from_data_proxy_node_strategy.h | 2 +- 8 files changed, 13 insertions(+), 24 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/context.h b/services/distributeddataservice/service/data_share/common/context.h index 7f6d2b4f..211f014a 100644 --- a/services/distributeddataservice/service/data_share/common/context.h +++ b/services/distributeddataservice/service/data_share/common/context.h @@ -52,15 +52,6 @@ public: AccessSystemMode accessSystemMode = AccessSystemMode::UNDEFINED; OHOS::AppExecFwk::BundleInfo bundleInfo; std::string type = "rdb"; - - - virtual std::string GetTpltProviderBundleName() { - return ""; - } - - virtual uint64_t GetTpltSubscriberId() { - return 0; - } }; class TemplateContext : public Context { diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 055e9e21..61727a32 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -107,6 +107,7 @@ std::shared_ptr RdbDelegate::Query( delete p; }); } + class ResultSetJsonFormatter final: public DistributedData::Serializable { public: explicit ResultSetJsonFormatter(const std::shared_ptr &resultSet) diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index bbcb7166..1c29864e 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -417,8 +417,8 @@ int PublishedDataSubscriberManager::EnableSubscriber(const std::string &uri, con return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; } -void PublishedDataSubscriberManager::Emit(std::vector keys, const std::string &ownerBundleName, - const sptr observer) +void PublishedDataSubscriberManager::Emit(const std::vector &keys, + const std::string &ownerBundleName, const sptr observer) { int32_t status; // key is bundleName, value is change node diff --git a/services/distributeddataservice/service/data_share/common/template_manager.h b/services/distributeddataservice/service/data_share/common/template_manager.h index 6dd19a48..362e90a5 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.h +++ b/services/distributeddataservice/service/data_share/common/template_manager.h @@ -91,15 +91,12 @@ struct PublishedDataKey { class PublishedDataSubscriberManager { public: static PublishedDataSubscriberManager &GetInstance(); - int AddSubscriber(const std::string &key, const std::string &callerBundleName, const int64_t subscriberId, - const sptr observer, const uint32_t callerTokenId); - int DelSubscriber(const std::string &key, const std::string &callerBundleName, const int64_t subscriberId, - const uint32_t callerTokenId); - int DisableSubscriber(const std::string &key, const std::string &callerBundleName, const int64_t subscriberId, - const uint32_t callerTokenId); - int EnableSubscriber(const std::string &key, const std::string &callerBundleName, const int64_t subscriberId, - const uint32_t callerTokenId); - void Emit(const std::vector keys, const std::string &ownerBundleName, + int AddSubscriber(const std::string &, const std::string &, const int64_t, + const sptr, const uint32_t); + int DelSubscriber(const std::string &, const std::string &, const int64_t, const uint32_t); + int DisableSubscriber(const std::string &, const std::string &, const int64_t, const uint32_t); + int EnableSubscriber(const std::string &, const std::string &, const int64_t , const uint32_t); + void Emit(const std::vector &, const std::string &, const sptr observer = nullptr); private: diff --git a/services/distributeddataservice/service/data_share/data/published_data.h b/services/distributeddataservice/service/data_share/data/published_data.h index 0f286482..3e9099f1 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.h +++ b/services/distributeddataservice/service/data_share/data/published_data.h @@ -20,7 +20,7 @@ #include "serializable/serializable.h" namespace OHOS::DataShare { -enum PublishedDataType : int32_t { +enum class PublishedDataType : int32_t { STRING, ASHMEM }; diff --git a/services/distributeddataservice/service/data_share/data/template_data.h b/services/distributeddataservice/service/data_share/data/template_data.h index 1c8558d3..4b194f37 100644 --- a/services/distributeddataservice/service/data_share/data/template_data.h +++ b/services/distributeddataservice/service/data_share/data/template_data.h @@ -23,7 +23,7 @@ namespace OHOS::DataShare { struct TemplateNode : public DistributedData::Serializable { TemplateNode() = default; - TemplateNode(const Template &tpl); + explicit TemplateNode(const Template &tpl); bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; Template tpl; diff --git a/services/distributeddataservice/service/data_share/data_share_obs_proxy.h b/services/distributeddataservice/service/data_share/data_share_obs_proxy.h index 4bad326d..b54e0b0c 100644 --- a/services/distributeddataservice/service/data_share/data_share_obs_proxy.h +++ b/services/distributeddataservice/service/data_share/data_share_obs_proxy.h @@ -42,4 +42,4 @@ private: static inline BrokerDelegator delegator_; }; } // namespace OHOS::DataShare -#endif //DATA_SHARE_OBS_PROXY_H +#endif // DATA_SHARE_OBS_PROXY_H diff --git a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.h b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.h index db26c9aa..802bc214 100644 --- a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.h +++ b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.h @@ -27,7 +27,7 @@ public: bool operator()(std::shared_ptr context) override; private: - enum PATH_PARAMS : int32_t { + enum class PATH_PARAMS : int32_t { STORE_NAME = 0, TABLE_NAME, PARAM_SIZE -- Gitee From 976d30f59f9d81ce865de6636ea14587c85da3bf Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 4 May 2023 22:03:16 +0800 Subject: [PATCH 085/152] f Signed-off-by: hanlu --- .../service/data_share/common/context.h | 20 ------------------- .../service/data_share/data/published_data.h | 8 +++++--- 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/context.h b/services/distributeddataservice/service/data_share/common/context.h index 211f014a..9b9d9b72 100644 --- a/services/distributeddataservice/service/data_share/common/context.h +++ b/services/distributeddataservice/service/data_share/common/context.h @@ -53,25 +53,5 @@ public: OHOS::AppExecFwk::BundleInfo bundleInfo; std::string type = "rdb"; }; - -class TemplateContext : public Context { -public: - TemplateContext(const std::string &uri, const std::string &tpltProviderBundleName, int64_t subscriberId) - : Context(uri), tpltProviderBundleName(tpltProviderBundleName), subscriberId(subscriberId) - { - } - std::string GetTpltProviderBundleName() override - { - return tpltProviderBundleName; - } - uint64_t GetTpltSubscriberId() override - { - return subscriberId; - } - -private: - std::string tpltProviderBundleName; - int64_t subscriberId; -}; } // namespace OHOS::DataShare #endif diff --git a/services/distributeddataservice/service/data_share/data/published_data.h b/services/distributeddataservice/service/data_share/data/published_data.h index 3e9099f1..f6180e35 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.h +++ b/services/distributeddataservice/service/data_share/data/published_data.h @@ -20,9 +20,11 @@ #include "serializable/serializable.h" namespace OHOS::DataShare { -enum class PublishedDataType : int32_t { - STRING, - ASHMEM +class PublishedDataType { +public: + static constexpr int8_t STRING = 0; + static constexpr int8_t ASHMEM = 1; + }; struct PublishedDataNode final : public DistributedData::Serializable { -- Gitee From 632697eb7feef80814cae0553f3e8bcf4ca93a06 Mon Sep 17 00:00:00 2001 From: hanlu Date: Thu, 4 May 2023 22:25:15 +0800 Subject: [PATCH 086/152] f Signed-off-by: hanlu --- .../service/data_share/common/kv_delegate.h | 1 - .../service/data_share/common/template_manager.h | 3 +-- .../service/data_share/data/published_data.h | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/kv_delegate.h b/services/distributeddataservice/service/data_share/common/kv_delegate.h index f01c4174..fe111337 100644 --- a/services/distributeddataservice/service/data_share/common/kv_delegate.h +++ b/services/distributeddataservice/service/data_share/common/kv_delegate.h @@ -25,7 +25,6 @@ namespace OHOS::DataShare { class KvDelegate final : public KvDBDelegate { public: explicit KvDelegate(const std::string &path); - ~KvDelegate() = default; int32_t Upsert(const std::string &collectionName, const KvData &value) override; int32_t DeleteById(const std::string &collectionName, const Id &id) override; int32_t Get(const std::string &collectionName, const Id &id, std::string &value) override; diff --git a/services/distributeddataservice/service/data_share/common/template_manager.h b/services/distributeddataservice/service/data_share/common/template_manager.h index 362e90a5..f93a3586 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.h +++ b/services/distributeddataservice/service/data_share/common/template_manager.h @@ -95,10 +95,9 @@ public: const sptr, const uint32_t); int DelSubscriber(const std::string &, const std::string &, const int64_t, const uint32_t); int DisableSubscriber(const std::string &, const std::string &, const int64_t, const uint32_t); - int EnableSubscriber(const std::string &, const std::string &, const int64_t , const uint32_t); + int EnableSubscriber(const std::string &, const std::string &, const int64_t, const uint32_t); void Emit(const std::vector &, const std::string &, const sptr observer = nullptr); - private: struct ObserverNode { ObserverNode(const sptr &observer, uint32_t callerTokenId); diff --git a/services/distributeddataservice/service/data_share/data/published_data.h b/services/distributeddataservice/service/data_share/data/published_data.h index f6180e35..3e719892 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.h +++ b/services/distributeddataservice/service/data_share/data/published_data.h @@ -24,7 +24,6 @@ class PublishedDataType { public: static constexpr int8_t STRING = 0; static constexpr int8_t ASHMEM = 1; - }; struct PublishedDataNode final : public DistributedData::Serializable { -- Gitee From 285daf91efdd562bd4ea08c6bcb02684a8fdea24 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 5 May 2023 10:21:32 +0800 Subject: [PATCH 087/152] f Signed-off-by: hanlu --- .../data_share/common/template_manager.cpp | 27 ++++++++++++------- .../data_share/common/template_manager.h | 2 ++ .../data_share/data_share_obs_proxy.cpp | 18 ++++++++++--- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index 1c29864e..e730aed0 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -424,7 +424,7 @@ void PublishedDataSubscriberManager::Emit(const std::vector &k // key is bundleName, value is change node std::map publishedResult; std::map, std::vector> callbacks; - publishedDataCache.ForEach([&keys, &status, &observer, &publishedResult, &callbacks]( + publishedDataCache.ForEach([&keys, &status, &observer, &publishedResult, &callbacks, this]( const PublishedDataKey &key, std::vector &val) { for (auto &data : keys) { if (key != data) { @@ -441,17 +441,9 @@ void PublishedDataSubscriberManager::Emit(const std::vector &k publishedResult.erase(key); continue; } + PutInto(callbacks, val, key, observer); break; } - for (auto &callback : val) { - if (callback.enabled && callback.observer != nullptr) { - // callback the observer, others do not call - if (observer != nullptr && callback.observer != observer) { - continue; - } - callbacks[callback.observer].emplace_back(key); - } - } return false; }); PublishedDataChangeNode result; @@ -470,6 +462,21 @@ void PublishedDataSubscriberManager::Emit(const std::vector &k } } +void PublishedDataSubscriberManager::PutInto( + std::map, std::vector> &callbacks, + std::vector &val, const PublishedDataKey &key, const sptr observer) +{ + for (auto &callback : val) { + if (callback.enabled && callback.observer != nullptr) { + // callback the observer, others do not call + if (observer != nullptr && callback.observer != observer) { + continue; + } + callbacks[callback.observer].emplace_back(key); + } + } +} + PublishedDataKey::PublishedDataKey(const std::string &key, const std::string &bundle, const int64_t subscriberId) : key(key), bundleName(bundle), subscriberId(subscriberId) { diff --git a/services/distributeddataservice/service/data_share/common/template_manager.h b/services/distributeddataservice/service/data_share/common/template_manager.h index f93a3586..34bf7a7e 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.h +++ b/services/distributeddataservice/service/data_share/common/template_manager.h @@ -106,6 +106,8 @@ private: bool enabled = true; }; PublishedDataSubscriberManager() = default; + void PutInto(std::map, std::vector> &, + std::vector &, const PublishedDataKey &, const sptr); ConcurrentMap> publishedDataCache; }; } // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/data_share_obs_proxy.cpp b/services/distributeddataservice/service/data_share/data_share_obs_proxy.cpp index fe9fe695..f754e57e 100644 --- a/services/distributeddataservice/service/data_share/data_share_obs_proxy.cpp +++ b/services/distributeddataservice/service/data_share/data_share_obs_proxy.cpp @@ -1,6 +1,18 @@ -// -// Created by niudongyao on 2023/3/17. -// +/* + * 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 "ObserverProxy" #include "data_share_obs_proxy.h" -- Gitee From 229f6cc7a4b85adff241d6bd84e954b341c730b1 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 5 May 2023 11:40:06 +0800 Subject: [PATCH 088/152] f Signed-off-by: hanlu --- .../distributeddataservice/service/BUILD.gn | 2 + .../data_share/common/rdb_delegate.cpp | 44 ++--------------- .../data_share/common/scheduler_manager.cpp | 8 ++++ .../data_share/common/template_manager.cpp | 29 +---------- .../data_share/common/template_manager.h | 1 + .../data_share/data/json_formatter.cpp | 38 +++++++++++++++ .../service/data_share/data/json_formatter.h | 37 ++++++++++++++ .../data_share/data/published_data.cpp | 4 ++ .../data/resultset_json_formatter.cpp | 48 +++++++++++++++++++ .../data/resultset_json_formatter.h | 36 ++++++++++++++ .../general/connect_extension_strategy.cpp | 6 +-- 11 files changed, 182 insertions(+), 71 deletions(-) create mode 100644 services/distributeddataservice/service/data_share/data/json_formatter.cpp create mode 100644 services/distributeddataservice/service/data_share/data/json_formatter.h create mode 100644 services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp create mode 100644 services/distributeddataservice/service/data_share/data/resultset_json_formatter.h diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 524ec57d..543810b1 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -78,7 +78,9 @@ ohos_shared_library("distributeddatasvc") { "data_share/common/seq_strategy.cpp", "data_share/common/template_manager.cpp", "data_share/common/uri_utils.cpp", + "data_share/data/json_formatter.cpp", "data_share/data/published_data.cpp", + "data_share/data/resultset_json_formatter.cpp", "data_share/data/template_data.cpp", "data_share/data_share_obs_proxy.cpp", "data_share/data_share_service_impl.cpp", diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 61727a32..ef91d9e6 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -16,9 +16,10 @@ #include "rdb_delegate.h" #include "log_print.h" -#include "rdb_utils.h" +#include "resultset_json_formatter.h" #include "scheduler_manager.h" #include "utils/anonymous.h" + namespace OHOS::DataShare { constexpr static int32_t MAX_RESULTSET_COUNT = 16; std::atomic RdbDelegate::resultSetCount = 0; @@ -87,7 +88,7 @@ std::shared_ptr RdbDelegate::Query( return nullptr; } int count = resultSetCount.fetch_add(1); - ZLOGI("start query %{public}d", count); + ZLOGD("start query %{public}d", count); if (count > MAX_RESULTSET_COUNT) { ZLOGE("resultSetCount is full"); resultSetCount--; @@ -102,49 +103,12 @@ std::shared_ptr RdbDelegate::Query( } auto bridge = RdbDataShareAdapter::RdbUtils::ToResultSetBridge(resultSet); return std::shared_ptr(new DataShareResultSet(bridge), [](auto p) { - ZLOGI("release resultset"); + ZLOGD("release resultset"); resultSetCount--; delete p; }); } -class ResultSetJsonFormatter final: public DistributedData::Serializable { -public: - explicit ResultSetJsonFormatter(const std::shared_ptr &resultSet) - : resultSet(resultSet) - { - } - ~ResultSetJsonFormatter() {} - bool Marshal(json &node) const override - { - int columnCount = 0; - auto result = resultSet->GetColumnCount(columnCount); - if (result != EOK) { - ZLOGE("GetColumnCount err, %{public}d", result); - return false; - } - while (resultSet->GoToNextRow() == E_OK) { - json result; - for (int i = 0; i < columnCount; i++) { - std::string columnName; - std::string value; - resultSet->GetColumnName(i, columnName); - resultSet->GetString(i, value); - SetValue(result[columnName], value); - } - node.push_back(result); - } - return true; - } - bool Unmarshal(const json &node) override - { - return false; - } - -private: - std::shared_ptr resultSet; -}; - std::shared_ptr RdbDelegate::Query( const std::string &sql, const std::vector &selectionArgs) { diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp index 9a694d70..8c64604d 100644 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp @@ -71,11 +71,19 @@ void SchedulerManager::SetTimer(const RdbStoreContext &rdbContext, const std::st if (it != timerCache_.end()) { // has current timer, reset time std::time_t now = time(nullptr); + if (now > reminderTime) { + ZLOGE("scheduler time fail %{private}s %{private}s", key.uri.c_str(), bundleName.c_str()); + return; + } scheduler_->Reset(it->second, std::chrono::seconds(reminderTime - now)); return; } // not find task in map, create new timer std::time_t now = time(nullptr); + if (now > reminderTime) { + ZLOGE("scheduler time fail %{private}s %{private}s", key.uri.c_str(), bundleName.c_str()); + return; + } auto taskId = scheduler_->At(TaskScheduler::Clock::now() + std::chrono::seconds(reminderTime - now), [&]() { // 1. execute schedulerSQL in next time Execute(key, rdbContext.dir, rdbContext.version); diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index e730aed0..8b98f162 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -17,6 +17,7 @@ #include "template_manager.h" #include "db_delegate.h" +#include "json_formatter.h" #include "log_print.h" #include "published_data.h" #include "scheduler_manager.h" @@ -224,34 +225,6 @@ int RdbSubscriberManager::EnableRdbSubscriber(const std::string &uri, const Temp return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; } -class JsonFormatter : public DistributedData::Serializable { -public: - JsonFormatter(const std::string &key, const std::shared_ptr &value) - : key_(key), value_(value) - { - } - bool Marshal(json &node) const override - { - if (value_ == nullptr) { - ZLOGE("null value %{public}s", key_.c_str()); - return false; - } - return SetValue(node[key_], *value_); - } - bool Unmarshal(const json &node) override - { - if (value_ == nullptr) { - ZLOGE("null value %{public}s", key_.c_str()); - return false; - } - return GetValue(node, key_, *value_); - } - -private: - std::string key_; - std::shared_ptr value_; -}; - void RdbSubscriberManager::Emit(const std::string &uri, std::shared_ptr context) { if (!URIUtils::IsDataProxyURI(uri)) { diff --git a/services/distributeddataservice/service/data_share/common/template_manager.h b/services/distributeddataservice/service/data_share/common/template_manager.h index 34bf7a7e..c5a96ef0 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.h +++ b/services/distributeddataservice/service/data_share/common/template_manager.h @@ -69,6 +69,7 @@ private: uint32_t callerTokenId; bool enabled = true; }; + RdbSubscriberManager() = default; ConcurrentMap> rdbCache_; int Notify(const Key &key, std::vector &val, const std::string &rdbDir, int rdbVersion); diff --git a/services/distributeddataservice/service/data_share/data/json_formatter.cpp b/services/distributeddataservice/service/data_share/data/json_formatter.cpp new file mode 100644 index 00000000..db190824 --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/json_formatter.cpp @@ -0,0 +1,38 @@ +/* + * 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 "JsonFormatter" +#include "json_formatter.h" + +#include "log_print.h" + +namespace OHOS::DataShare { +bool JsonFormatter::Marshal(json &node) const +{ + if (value_ == nullptr) { + ZLOGE("null value %{public}s", key_.c_str()); + return false; + } + return SetValue(node[key_], *value_); +} + +bool JsonFormatter::Unmarshal(const json &node) +{ + if (value_ == nullptr) { + ZLOGE("null value %{public}s", key_.c_str()); + return false; + } + return GetValue(node, key_, *value_); +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/json_formatter.h b/services/distributeddataservice/service/data_share/data/json_formatter.h new file mode 100644 index 00000000..d9a229e6 --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/json_formatter.h @@ -0,0 +1,37 @@ +/* + * 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 DATASHARESERVICE_JSON_FORMATTER_H +#define DATASHARESERVICE_JSON_FORMATTER_H + +#include "serializable/serializable.h" + +namespace OHOS::DataShare { + +class JsonFormatter : public DistributedData::Serializable { +public: + JsonFormatter(const std::string &key, const std::shared_ptr &value) + : key_(key), value_(value) + { + } + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + +private: + std::string key_; + std::shared_ptr value_; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/data/published_data.cpp b/services/distributeddataservice/service/data_share/data/published_data.cpp index be7bb744..6666c9aa 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.cpp +++ b/services/distributeddataservice/service/data_share/data/published_data.cpp @@ -94,6 +94,10 @@ bool PublishedDataNode::Marshal(DistributedData::Serializable::json &node) const node["value"] = std::vector(data, data + ashmem->GetAshmemSize()); } std::time_t now = time(nullptr); + if (now <= 0) { + ZLOGE("time error"); + return false; + } ret = ret && SetValue(node["timestamp"], now); return ret; } diff --git a/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp new file mode 100644 index 00000000..931a4d54 --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp @@ -0,0 +1,48 @@ +/* + * 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 "ResultSetJsonFormatter" +#include "resultset_json_formatter.h" + +#include "rdb_errno.h" +#include "log_print.h" + +namespace OHOS::DataShare { +bool ResultSetJsonFormatter::Marshal(json &node) const +{ + int columnCount = 0; + auto result = resultSet->GetColumnCount(columnCount); + if (result != NativeRdb::E_OK) { + ZLOGE("GetColumnCount err, %{public}d", result); + return false; + } + while (resultSet->GoToNextRow() == NativeRdb::E_OK) { + json result; + for (int i = 0; i < columnCount; i++) { + std::string columnName; + std::string value; + resultSet->GetColumnName(i, columnName); + resultSet->GetString(i, value); + SetValue(result[columnName], value); + } + node.push_back(result); + } + return true; +} + +bool ResultSetJsonFormatter::Unmarshal(const DistributedData::Serializable::json &node) +{ + return false; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h new file mode 100644 index 00000000..87376f3a --- /dev/null +++ b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h @@ -0,0 +1,36 @@ +/* + * 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 DATASHARESERVICE_RESULTSET_JSON_FORMATTER_H +#define DATASHARESERVICE_RESULTSET_JSON_FORMATTER_H + +#include "datashare_template.h" +#include "rdb_utils.h" +#include "serializable/serializable.h" + +namespace OHOS::DataShare { + +class ResultSetJsonFormatter final : public DistributedData::Serializable { +public: + explicit ResultSetJsonFormatter(const std::shared_ptr &resultSet) : resultSet(resultSet) {} + ~ResultSetJsonFormatter() {} + bool Marshal(json &node) const override; + bool Unmarshal(const json &node) override; + +private: + std::shared_ptr resultSet; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_RESULTSET_JSON_FORMATTER_H diff --git a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp index 446d0532..376eeb5b 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp @@ -89,15 +89,15 @@ bool ConnectExtensionStrategy::Execute( return true; } int waitTime = 0; - static constexpr int RETRY_TIME = 500; + static constexpr int retryTime = 500; while (!isFinished()) { if (waitTime > maxWaitTimeMs) { ZLOGE("cannot finish work"); return false; } ZLOGI("has wait %{public}d ms", waitTime); - std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_TIME)); - waitTime += RETRY_TIME; + std::this_thread::sleep_for(std::chrono::milliseconds(retryTime)); + waitTime += retryTime; } return true; } -- Gitee From 8ebaf9cddd933a5fcbf206438ec9c10faea9d401 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 5 May 2023 11:45:11 +0800 Subject: [PATCH 089/152] s Signed-off-by: hanlu --- .../service/data_share/common/db_delegate.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.h b/services/distributeddataservice/service/data_share/common/db_delegate.h index e214c9fa..f0e3e880 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.h +++ b/services/distributeddataservice/service/data_share/common/db_delegate.h @@ -62,6 +62,11 @@ public: explicit VersionData(int version); bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; + VersionData &operator=(int inputVersion) + { + version = inputVersion; + return *this; + }; operator int() { return version; -- Gitee From 80f4841e2ed531acf0cc5f09ddcadcc95f50e73c Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 5 May 2023 14:12:44 +0800 Subject: [PATCH 090/152] f Signed-off-by: hanlu --- .../data_share/common/template_manager.cpp | 10 +++++----- .../data_share/common/template_manager.h | 17 ++++++++++------- .../service/data_share/data/json_formatter.h | 1 - .../service/data_share/data/published_data.cpp | 2 +- .../data_share/data/resultset_json_formatter.h | 1 - .../data_share/strategies/delete_strategy.cpp | 15 ++++++++------- .../data_share/strategies/get_data_strategy.cpp | 6 ++++-- .../data_share/strategies/insert_strategy.cpp | 5 +++-- .../data_share/strategies/publish_strategy.cpp | 6 ++++-- .../data_share/strategies/query_strategy.cpp | 15 ++++++++------- .../strategies/subscribe_strategy.cpp | 11 ++++++----- .../data_share/strategies/update_strategy.cpp | 5 +++-- 12 files changed, 52 insertions(+), 42 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp index 8b98f162..ba66003c 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/template_manager.cpp @@ -147,13 +147,13 @@ int RdbSubscriberManager::AddRdbSubscriber(const std::string &uri, const Templat Key key(uri, tplId.subscriberId_, tplId.bundleName_); rdbCache_.Compute(key, [&observer, &context, &result, this](const auto &key, std::vector &value) { ZLOGI("add subscriber, uri %{private}s tokenId %{public}d", key.uri.c_str(), context->callerTokenId); - ObserverNode observerNode(observer, context->callerTokenId); - std::vector node({ observerNode }); + std::vector node; + node.emplace_back(observer, context->callerTokenId); result = Notify(key, node, context->calledSourceDir, context->version); if (result != E_OK) { return false; } - value.emplace_back(observerNode); + value.emplace_back(observer, context->callerTokenId); if (GetEnableObserverCount(key) == 1) { SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); } @@ -212,8 +212,8 @@ int RdbSubscriberManager::EnableRdbSubscriber(const std::string &uri, const Temp for (auto it = value.begin(); it != value.end(); it++) { if (it->callerTokenId == context->callerTokenId) { it->enabled = true; - ObserverNode observerNode(it->observer, context->callerTokenId); - std::vector node({ observerNode }); + std::vector node; + node.emplace_back(it->observer, context->callerTokenId); Notify(key, node, context->calledSourceDir, context->version); if (GetEnableObserverCount(key) == 1) { SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); diff --git a/services/distributeddataservice/service/data_share/common/template_manager.h b/services/distributeddataservice/service/data_share/common/template_manager.h index c5a96ef0..58afdc5f 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.h +++ b/services/distributeddataservice/service/data_share/common/template_manager.h @@ -92,13 +92,16 @@ struct PublishedDataKey { class PublishedDataSubscriberManager { public: static PublishedDataSubscriberManager &GetInstance(); - int AddSubscriber(const std::string &, const std::string &, const int64_t, - const sptr, const uint32_t); - int DelSubscriber(const std::string &, const std::string &, const int64_t, const uint32_t); - int DisableSubscriber(const std::string &, const std::string &, const int64_t, const uint32_t); - int EnableSubscriber(const std::string &, const std::string &, const int64_t, const uint32_t); - void Emit(const std::vector &, const std::string &, - const sptr observer = nullptr); + int AddSubscriber(const std::string &key, const std::string &callerBundleName, const int64_t subscriberId, + const sptr observer, const uint32_t callerTokenId); + int DelSubscriber(const std::string &uri, const std::string &callerBundleName, const int64_t subscriberId, + const uint32_t callerTokenId); + int DisableSubscriber(const std::string &uri, const std::string &callerBundleName, const int64_t subscriberId, + const uint32_t callerTokenId); + int EnableSubscriber(const std::string &uri, const std::string &callerBundleName, const int64_t subscriberId, + const uint32_t callerTokenId); + void Emit(const std::vector &keys, const std::string &ownerBundleName, + const sptr observer); private: struct ObserverNode { ObserverNode(const sptr &observer, uint32_t callerTokenId); diff --git a/services/distributeddataservice/service/data_share/data/json_formatter.h b/services/distributeddataservice/service/data_share/data/json_formatter.h index d9a229e6..7c41e197 100644 --- a/services/distributeddataservice/service/data_share/data/json_formatter.h +++ b/services/distributeddataservice/service/data_share/data/json_formatter.h @@ -19,7 +19,6 @@ #include "serializable/serializable.h" namespace OHOS::DataShare { - class JsonFormatter : public DistributedData::Serializable { public: JsonFormatter(const std::string &key, const std::shared_ptr &value) diff --git a/services/distributeddataservice/service/data_share/data/published_data.cpp b/services/distributeddataservice/service/data_share/data/published_data.cpp index 6666c9aa..ae3739b9 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.cpp +++ b/services/distributeddataservice/service/data_share/data/published_data.cpp @@ -95,7 +95,7 @@ bool PublishedDataNode::Marshal(DistributedData::Serializable::json &node) const } std::time_t now = time(nullptr); if (now <= 0) { - ZLOGE("time error"); + ZLOGE("time error"); return false; } ret = ret && SetValue(node["timestamp"], now); diff --git a/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h index 87376f3a..0dfcabdf 100644 --- a/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h +++ b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h @@ -21,7 +21,6 @@ #include "serializable/serializable.h" namespace OHOS::DataShare { - class ResultSetJsonFormatter final : public DistributedData::Serializable { public: explicit ResultSetJsonFormatter(const std::shared_ptr &resultSet) : resultSet(resultSet) {} diff --git a/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp index b46f729f..1250c11e 100644 --- a/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp @@ -53,13 +53,14 @@ Strategy *DeleteStrategy::GetStrategy() if (!strategies.IsEmpty()) { return &strategies; } - auto ret = strategies.Init({ - new (std::nothrow)LoadConfigCommonStrategy(), - new (std::nothrow)LoadConfigFromBundleInfoStrategy(), - new (std::nothrow)PermissionStrategy(), - new (std::nothrow)LoadConfigDataInfoStrategy(), - new (std::nothrow)ProcessSingleAppUserCrossStrategy() - }); + std::initializer_list list = { + new (std::nothrow)LoadConfigCommonStrategy(), + new (std::nothrow)LoadConfigFromBundleInfoStrategy(), + new (std::nothrow)PermissionStrategy(), + new (std::nothrow)LoadConfigDataInfoStrategy(), + new (std::nothrow)ProcessSingleAppUserCrossStrategy() + }; + auto ret = strategies.Init(list); if (!ret) { return nullptr; } diff --git a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp index 6771727f..34b15f2e 100644 --- a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp @@ -46,11 +46,13 @@ Strategy *GetDataStrategy::GetStrategy() if (!strategies.IsEmpty()) { return &strategies; } - auto ret = strategies.Init({ + std::initializer_list list = { + new (std::nothrow) LoadConfigCommonStrategy(), new (std::nothrow) LoadConfigFromDataProxyNodeStrategy(), new (std::nothrow) PermissionStrategy() - }); + }; + auto ret = strategies.Init(list); if (!ret) { return nullptr; } diff --git a/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp index 87ac59ed..8356296a 100644 --- a/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp @@ -54,13 +54,14 @@ Strategy *InsertStrategy::GetStrategy() if (!strategies.IsEmpty()) { return &strategies; } - auto ret = strategies.Init({ + std::initializer_list list = { new (std::nothrow)LoadConfigCommonStrategy(), new (std::nothrow)LoadConfigFromBundleInfoStrategy(), new (std::nothrow)PermissionStrategy(), new (std::nothrow)LoadConfigDataInfoStrategy(), new (std::nothrow)ProcessSingleAppUserCrossStrategy() - }); + }; + auto ret = strategies.Init(list); if (!ret) { return nullptr; } diff --git a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp index 70925980..1d767524 100644 --- a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp @@ -58,10 +58,12 @@ Strategy *PublishStrategy::GetStrategy() if (!strategies.IsEmpty()) { return &strategies; } - auto ret = strategies.Init({ + std::initializer_list list = { new (std::nothrow) LoadConfigCommonStrategy(), new (std::nothrow) LoadConfigFromDataProxyNodeStrategy(), - new (std::nothrow) PermissionStrategy() }); + new (std::nothrow) PermissionStrategy() + }; + auto ret = strategies.Init(list); if (!ret) { return nullptr; } diff --git a/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp index c598e7ee..7b6f8629 100644 --- a/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp @@ -55,13 +55,14 @@ Strategy *QueryStrategy::GetStrategy() if (!strategies.IsEmpty()) { return &strategies; } - auto ret = strategies.Init({ - new (std::nothrow)LoadConfigCommonStrategy(), - new (std::nothrow)LoadConfigFromBundleInfoStrategy(), - new (std::nothrow)PermissionStrategy(), - new (std::nothrow)LoadConfigDataInfoStrategy(), - new (std::nothrow)ProcessSingleAppUserCrossStrategy() - }); + std::initializer_list list = { + new (std::nothrow)LoadConfigCommonStrategy(), + new (std::nothrow)LoadConfigFromBundleInfoStrategy(), + new (std::nothrow)PermissionStrategy(), + new (std::nothrow)LoadConfigDataInfoStrategy(), + new (std::nothrow)ProcessSingleAppUserCrossStrategy() + }; + auto ret = strategies.Init(list); if (!ret) { return nullptr; } diff --git a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp index 606e5bcc..a2913ab2 100644 --- a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp @@ -45,11 +45,12 @@ Strategy *SubscribeStrategy::GetStrategy() if (!strategies.IsEmpty()) { return &strategies; } - auto ret = strategies.Init({ - new (std::nothrow)LoadConfigCommonStrategy(), - new (std::nothrow)LoadConfigFromDataProxyNodeStrategy(), - new (std::nothrow)PermissionStrategy() - }); + std::initializer_list list = { + new (std::nothrow)LoadConfigCommonStrategy(), + new (std::nothrow)LoadConfigFromDataProxyNodeStrategy(), + new (std::nothrow)PermissionStrategy() + }; + auto ret = strategies.Init(list); if (!ret) { return nullptr; } diff --git a/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp index 419c53a8..8ea70248 100644 --- a/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp @@ -54,13 +54,14 @@ Strategy *UpdateStrategy::GetStrategy() if (!strategies.IsEmpty()) { return &strategies; } - auto ret = strategies.Init({ + std::initializer_list list = { new (std::nothrow)LoadConfigCommonStrategy(), new (std::nothrow)LoadConfigFromBundleInfoStrategy(), new (std::nothrow)PermissionStrategy(), new (std::nothrow)LoadConfigDataInfoStrategy(), new (std::nothrow)ProcessSingleAppUserCrossStrategy() - }); + }; + auto ret = strategies.Init(list); if (!ret) { return nullptr; } -- Gitee From bfef910a3d51b35caef482b9e38fe2475546632d Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 5 May 2023 14:47:50 +0800 Subject: [PATCH 091/152] f Signed-off-by: hanlu --- .../service/data_share/common/callback_impl.h | 42 +++++++++++++++++++ .../data_share/common/template_manager.h | 2 +- .../data_share/data/published_data.cpp | 8 ++-- .../service/data_share/data/published_data.h | 12 ++---- .../service/data_share/data_proxy_observer.h | 2 +- .../general/connect_extension_strategy.cpp | 24 +---------- .../general/connect_extension_strategy.h | 1 - 7 files changed, 54 insertions(+), 37 deletions(-) create mode 100644 services/distributeddataservice/service/data_share/common/callback_impl.h diff --git a/services/distributeddataservice/service/data_share/common/callback_impl.h b/services/distributeddataservice/service/data_share/common/callback_impl.h new file mode 100644 index 00000000..07237909 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/callback_impl.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 DATASHARESERVICE_CALLBACK_IMPL_H +#define DATASHARESERVICE_CALLBACK_IMPL_H + +#include "ability_connect_callback_stub.h" +#include "ability_manager_client.h" +#include "block_data.h" + +namespace OHOS::DataShare { +class CallbackImpl : public AAFwk::AbilityConnectionStub { +public: + CallbackImpl(BlockData &data) : data_(data) {} + void OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) override + { + bool result = true; + data_.SetValue(result); + } + void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override + { + bool result = false; + data_.SetValue(result); + } + +private: + BlockData &data_; +}; +} // namespace OHOS::DataShare +#endif diff --git a/services/distributeddataservice/service/data_share/common/template_manager.h b/services/distributeddataservice/service/data_share/common/template_manager.h index 58afdc5f..f6b4e5a5 100644 --- a/services/distributeddataservice/service/data_share/common/template_manager.h +++ b/services/distributeddataservice/service/data_share/common/template_manager.h @@ -101,7 +101,7 @@ public: int EnableSubscriber(const std::string &uri, const std::string &callerBundleName, const int64_t subscriberId, const uint32_t callerTokenId); void Emit(const std::vector &keys, const std::string &ownerBundleName, - const sptr observer); + const sptr observer = nullptr); private: struct ObserverNode { ObserverNode(const sptr &observer, uint32_t callerTokenId); diff --git a/services/distributeddataservice/service/data_share/data/published_data.cpp b/services/distributeddataservice/service/data_share/data/published_data.cpp index ae3739b9..fb1ffc1a 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.cpp +++ b/services/distributeddataservice/service/data_share/data/published_data.cpp @@ -77,7 +77,7 @@ bool PublishedDataNode::Marshal(DistributedData::Serializable::json &node) const ret = ret && SetValue(node, version); if (value.index() == 1) { std::string valueStr = std::get(value); - ret = ret && SetValue(node["type"], PublishedDataType::STRING); + ret = ret && SetValue(node["type"], PublishedData::STRING); ret = ret && SetValue(node["value"], valueStr); } else { sptr ashmem = std::get>(value); @@ -90,7 +90,7 @@ bool PublishedDataNode::Marshal(DistributedData::Serializable::json &node) const ZLOGE("ReadFromAshmem null"); return false; } - ret = ret && SetValue(node["type"], PublishedDataType::ASHMEM); + ret = ret && SetValue(node["type"], PublishedData::ASHMEM); node["value"] = std::vector(data, data + ashmem->GetAshmemSize()); } std::time_t now = time(nullptr); @@ -114,11 +114,11 @@ bool PublishedDataNode::Unmarshal(const DistributedData::Serializable::json &nod ZLOGE("Unmarshal PublishedDataNode failed, %{private}s", key.c_str()); return false; } - if (type == PublishedDataType::STRING) { + if (type == PublishedData::STRING) { std::string strValue; ret = ret && GetValue(node, "value", strValue); value = strValue; - } else if (type == PublishedDataType::ASHMEM) { + } else if (type == PublishedData::ASHMEM) { std::vector binaryData = node["value"]; std::string ashmemName = "PublishedData" + key + "_" + bundleName + "_" + std::to_string(subscriberId); auto ashmem = Ashmem::CreateAshmem(ashmemName.c_str(), binaryData.size()); diff --git a/services/distributeddataservice/service/data_share/data/published_data.h b/services/distributeddataservice/service/data_share/data/published_data.h index 3e719892..485718de 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.h +++ b/services/distributeddataservice/service/data_share/data/published_data.h @@ -20,12 +20,6 @@ #include "serializable/serializable.h" namespace OHOS::DataShare { -class PublishedDataType { -public: - static constexpr int8_t STRING = 0; - static constexpr int8_t ASHMEM = 1; -}; - struct PublishedDataNode final : public DistributedData::Serializable { PublishedDataNode(const std::string &key, const std::string &bundleName, int64_t subscriberId, const std::variant, std::string> &value, const int version); @@ -39,7 +33,8 @@ struct PublishedDataNode final : public DistributedData::Serializable { VersionData version; }; -struct PublishedData final : public KvData { +class PublishedData final : public KvData { +public: static std::vector Query(const std::string &bundleName); static int32_t Query(const std::string &filter, std::variant, std::string> &publishedData); explicit PublishedData(const std::string &key = "", const std::string &bundleName = "", int64_t subscriberId = 0, @@ -53,8 +48,9 @@ struct PublishedData final : public KvData { VersionData GetVersion() const override; const DistributedData::Serializable &GetValue() const override; bool Unmarshal(const json &node) override; + static constexpr int8_t STRING = 0; + static constexpr int8_t ASHMEM = 1; PublishedDataNode value; - private: static std::string GetFullProjection(); }; diff --git a/services/distributeddataservice/service/data_share/data_proxy_observer.h b/services/distributeddataservice/service/data_share/data_proxy_observer.h index 40aeff58..10a71f53 100644 --- a/services/distributeddataservice/service/data_share/data_proxy_observer.h +++ b/services/distributeddataservice/service/data_share/data_proxy_observer.h @@ -34,4 +34,4 @@ public: }; } // namespace DataShare } // namespace OHOS -#endif //DATA_PROXY_OBSERVER_H +#endif // DATA_PROXY_OBSERVER_H diff --git a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp index 376eeb5b..eecd0c38 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.cpp @@ -17,10 +17,8 @@ #include "connect_extension_strategy.h" #include - -#include "ability_connect_callback_stub.h" -#include "ability_manager_client.h" #include "log_print.h" +#include "callback_impl.h" namespace OHOS::DataShare { bool ConnectExtensionStrategy::operator()(std::shared_ptr context) @@ -34,24 +32,6 @@ bool ConnectExtensionStrategy::operator()(std::shared_ptr context) } ConnectExtensionStrategy::ConnectExtensionStrategy() : data_(1) {} -class CallBackImpl : public AAFwk::AbilityConnectionStub { -public: - CallBackImpl(BlockData &data) : data_(data) {} - void OnAbilityConnectDone( - const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) override - { - bool result = true; - data_.SetValue(result); - } - void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override - { - bool result = false; - data_.SetValue(result); - } - -private: - BlockData &data_; -}; bool ConnectExtensionStrategy::Connect(std::shared_ptr context) { @@ -59,7 +39,7 @@ bool ConnectExtensionStrategy::Connect(std::shared_ptr context) AAFwk::Want want; want.SetUri(context->uri); data_.Clear(); - sptr callback = new CallBackImpl(data_); + sptr callback = new CallbackImpl(data_); ZLOGI("Start connect %{public}s", context->uri.c_str()); ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, callback, nullptr); if (ret != ERR_OK) { diff --git a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.h b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.h index 57d76c48..9cb4e3d2 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.h +++ b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.h @@ -16,7 +16,6 @@ #ifndef DATASHARESERVICE_CONNECT_EXTENSION_STRAGETY_H #define DATASHARESERVICE_CONNECT_EXTENSION_STRAGETY_H -#include "block_data.h" #include "strategy.h" namespace OHOS::DataShare { class ConnectExtensionStrategy final : public Strategy { -- Gitee From 788926fbfdf11d0f6151d37aefbedcd553c62187 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 5 May 2023 14:52:53 +0800 Subject: [PATCH 092/152] f Signed-off-by: hanlu --- .../data_share/strategies/general/connect_extension_strategy.h | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.h b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.h index 9cb4e3d2..57d76c48 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.h +++ b/services/distributeddataservice/service/data_share/strategies/general/connect_extension_strategy.h @@ -16,6 +16,7 @@ #ifndef DATASHARESERVICE_CONNECT_EXTENSION_STRAGETY_H #define DATASHARESERVICE_CONNECT_EXTENSION_STRAGETY_H +#include "block_data.h" #include "strategy.h" namespace OHOS::DataShare { class ConnectExtensionStrategy final : public Strategy { -- Gitee From 9cafd6348312ca7ed40468c36e08406953d3a82b Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 5 May 2023 16:35:15 +0800 Subject: [PATCH 093/152] f Signed-off-by: hanlu --- .../service/data_share/common/context.h | 1 - .../service/data_share/data/published_data.cpp | 11 ----------- .../service/data_share/data/published_data.h | 2 -- 3 files changed, 14 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/context.h b/services/distributeddataservice/service/data_share/common/context.h index 9b9d9b72..d506e4b1 100644 --- a/services/distributeddataservice/service/data_share/common/context.h +++ b/services/distributeddataservice/service/data_share/common/context.h @@ -48,7 +48,6 @@ public: int version = -1; int errCode = -1; bool isRead = false; - bool isPrivate = false; AccessSystemMode accessSystemMode = AccessSystemMode::UNDEFINED; OHOS::AppExecFwk::BundleInfo bundleInfo; std::string type = "rdb"; diff --git a/services/distributeddataservice/service/data_share/data/published_data.cpp b/services/distributeddataservice/service/data_share/data/published_data.cpp index fb1ffc1a..ed0e1a82 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.cpp +++ b/services/distributeddataservice/service/data_share/data/published_data.cpp @@ -145,17 +145,6 @@ PublishedDataNode::PublishedDataNode(const std::string &key, const std::string & { } -std::string PublishedData::GetFullProjection() -{ - json projection; - projection["key"] = 1; - projection["bundleName"] = 1; - projection["type"] = 1; - projection["value"] = 1; - projection["subscriberId"] = 1; - projection["version"] = 1; - return projection.dump(); -} int32_t PublishedData::Query(const std::string &filter, std::variant, std::string> &publishedData) { auto delegate = KvDBDelegate::GetInstance(); diff --git a/services/distributeddataservice/service/data_share/data/published_data.h b/services/distributeddataservice/service/data_share/data/published_data.h index 485718de..4acd3ee8 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.h +++ b/services/distributeddataservice/service/data_share/data/published_data.h @@ -51,8 +51,6 @@ public: static constexpr int8_t STRING = 0; static constexpr int8_t ASHMEM = 1; PublishedDataNode value; -private: - static std::string GetFullProjection(); }; } // namespace OHOS::DataShare #endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H -- Gitee From dc332eefba2e09acbf1214323c8325ace8d2d849 Mon Sep 17 00:00:00 2001 From: hanlu Date: Fri, 5 May 2023 16:48:32 +0800 Subject: [PATCH 094/152] f Signed-off-by: hanlu --- .../service/data_share/strategies/delete_strategy.cpp | 3 +++ .../service/data_share/strategies/get_data_strategy.cpp | 4 +++- .../service/data_share/strategies/insert_strategy.cpp | 3 +++ .../service/data_share/strategies/publish_strategy.cpp | 3 +++ .../service/data_share/strategies/query_strategy.cpp | 3 +++ .../service/data_share/strategies/subscribe_strategy.cpp | 3 +++ .../service/data_share/strategies/update_strategy.cpp | 3 +++ 7 files changed, 21 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp index 1250c11e..bdeb7411 100644 --- a/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp @@ -62,6 +62,9 @@ Strategy *DeleteStrategy::GetStrategy() }; auto ret = strategies.Init(list); if (!ret) { + std::for_each(list.begin(), list.end(), [](Strategy *item) { + free(item); + }); return nullptr; } return &strategies; diff --git a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp index 34b15f2e..4cc91cae 100644 --- a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp @@ -47,13 +47,15 @@ Strategy *GetDataStrategy::GetStrategy() return &strategies; } std::initializer_list list = { - new (std::nothrow) LoadConfigCommonStrategy(), new (std::nothrow) LoadConfigFromDataProxyNodeStrategy(), new (std::nothrow) PermissionStrategy() }; auto ret = strategies.Init(list); if (!ret) { + std::for_each(list.begin(), list.end(), [](Strategy *item) { + free(item); + }); return nullptr; } return &strategies; diff --git a/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp index 8356296a..5a9b782d 100644 --- a/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp @@ -63,6 +63,9 @@ Strategy *InsertStrategy::GetStrategy() }; auto ret = strategies.Init(list); if (!ret) { + std::for_each(list.begin(), list.end(), [](Strategy *item) { + free(item); + }); return nullptr; } return &strategies; diff --git a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp index 1d767524..7628388b 100644 --- a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp @@ -65,6 +65,9 @@ Strategy *PublishStrategy::GetStrategy() }; auto ret = strategies.Init(list); if (!ret) { + std::for_each(list.begin(), list.end(), [](Strategy *item) { + free(item); + }); return nullptr; } return &strategies; diff --git a/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp index 7b6f8629..e07f4712 100644 --- a/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp @@ -64,6 +64,9 @@ Strategy *QueryStrategy::GetStrategy() }; auto ret = strategies.Init(list); if (!ret) { + std::for_each(list.begin(), list.end(), [](Strategy *item) { + free(item); + }); return nullptr; } return &strategies; diff --git a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp index a2913ab2..c546fc00 100644 --- a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp @@ -52,6 +52,9 @@ Strategy *SubscribeStrategy::GetStrategy() }; auto ret = strategies.Init(list); if (!ret) { + std::for_each(list.begin(), list.end(), [](Strategy *item) { + free(item); + }); return nullptr; } return &strategies; diff --git a/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp index 8ea70248..0b9e2e9b 100644 --- a/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp @@ -63,6 +63,9 @@ Strategy *UpdateStrategy::GetStrategy() }; auto ret = strategies.Init(list); if (!ret) { + std::for_each(list.begin(), list.end(), [](Strategy *item) { + free(item); + }); return nullptr; } return &strategies; -- Gitee From 015eae35eaf617d44e3b562618cf97a17bf5239d Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Fri, 5 May 2023 19:34:32 +0800 Subject: [PATCH 095/152] add bind cloud db interfaces Signed-off-by: Sven Wang --- conf/config.json | 3 +- .../app/src/feature_stub_impl.cpp | 3 +- .../app/src/feature_stub_impl.h | 3 +- .../app/src/kvstore_data_service.cpp | 15 +++--- .../app/src/kvstore_data_service.h | 3 +- .../framework/cloud/cloud_db.cpp | 48 ++++-------------- .../framework/feature/feature_system.cpp | 49 +++++++++++++------ .../framework/include/cloud/cloud_db.h | 31 +++++++----- .../include/feature/feature_system.h | 16 ++++-- .../framework/include/store/general_store.h | 12 +++-- .../framework/include/store/general_value.h | 12 ++++- .../service/object/object_service_impl.cpp | 7 ++- .../service/test/config_factory_test.cpp | 2 +- 13 files changed, 117 insertions(+), 87 deletions(-) diff --git a/conf/config.json b/conf/config.json index 2bf2041c..6f13c2cf 100644 --- a/conf/config.json +++ b/conf/config.json @@ -15,7 +15,8 @@ } }, { - "lib": "libconfigdemo2.z.so" + "description": "cloud data interface adapter", + "lib": "libdistributedclouddata.z.so" }, { "lib": "libudmf_server.z.so" diff --git a/services/distributeddataservice/app/src/feature_stub_impl.cpp b/services/distributeddataservice/app/src/feature_stub_impl.cpp index 06cd7ee3..16876215 100644 --- a/services/distributeddataservice/app/src/feature_stub_impl.cpp +++ b/services/distributeddataservice/app/src/feature_stub_impl.cpp @@ -32,11 +32,12 @@ int FeatureStubImpl::OnRemoteRequest(uint32_t code, MessageParcel &data, Message return featureImpl_->OnRemoteRequest(code, data, reply); } -int32_t FeatureStubImpl::OnInitialize() +int32_t FeatureStubImpl::OnInitialize(std::shared_ptr executor) { if (featureImpl_ == nullptr) { return -1; } + featureImpl_->OnExecutor(std::move(executor)); return featureImpl_->OnInitialize(); } diff --git a/services/distributeddataservice/app/src/feature_stub_impl.h b/services/distributeddataservice/app/src/feature_stub_impl.h index 4e4562df..6fc9c824 100644 --- a/services/distributeddataservice/app/src/feature_stub_impl.h +++ b/services/distributeddataservice/app/src/feature_stub_impl.h @@ -15,6 +15,7 @@ #ifndef OHOS_DISTRIBUTED_DATA_APP_FEATURE_STUB_IMPL_H #define OHOS_DISTRIBUTED_DATA_APP_FEATURE_STUB_IMPL_H +#include "executor_pool.h" #include "feature/feature_system.h" #include "iremote_stub.h" namespace OHOS::DistributedData { @@ -29,7 +30,7 @@ public: ~FeatureStubImpl(); int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; - int32_t OnInitialize(); + int32_t OnInitialize(std::shared_ptr executor); int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName); int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m); diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 50bef791..ed802b36 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -116,7 +116,7 @@ void KvStoreDataService::Initialize() sptr KvStoreDataService::GetFeatureInterface(const std::string &name) { - sptr feature; + sptr feature; bool isFirstCreate = false; features_.Compute(name, [&feature, &isFirstCreate](const auto &key, auto &value) ->bool { if (value != nullptr) { @@ -132,21 +132,24 @@ sptr KvStoreDataService::GetFeatureInterface(const std::string &n return false; } - value = new DistributedData::FeatureStubImpl(impl); + value = new FeatureStubImpl(impl); feature = value; isFirstCreate = true; return true; }); if (isFirstCreate) { - feature->OnInitialize(); + feature->OnInitialize(executor_); } return feature != nullptr ? feature->AsObject() : nullptr; } -void KvStoreDataService::InitObjectStore() +void KvStoreDataService::LoadFeatures() { ZLOGI("begin."); - auto feature = GetFeatureInterface("data_object"); + auto features = FeatureSystem::GetInstance().GetFeatureName(FeatureSystem::BIND_NOW); + for (auto &feature : features) { + GetFeatureInterface(feature); + } } /* RegisterClientDeathObserver */ @@ -286,7 +289,7 @@ void KvStoreDataService::StartService() ZLOGI("begin."); KvStoreMetaManager::GetInstance().InitMetaListener(); DeviceMatrix::GetInstance().Initialize(IPCSkeleton::GetCallingTokenID(), Bootstrap::GetInstance().GetMetaDBName()); - InitObjectStore(); + LoadFeatures(); bool ret = SystemAbility::Publish(this); if (!ret) { DumpHelper::GetInstance().AddErrorInfo("StartService: Service publish failed."); diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index 990f4500..ab09f50f 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -105,7 +105,7 @@ private: void Initialize(); - void InitObjectStore(); + void LoadFeatures(); void StartService(); @@ -130,6 +130,7 @@ private: std::shared_ptr security_; ConcurrentMap> features_; std::shared_ptr deviceInnerListener_; + std::shared_ptr executor_; }; class DbMetaCallbackDelegateMgr : public DbMetaCallbackDelegate { diff --git a/services/distributeddataservice/framework/cloud/cloud_db.cpp b/services/distributeddataservice/framework/cloud/cloud_db.cpp index ea4d041d..6525a4f0 100644 --- a/services/distributeddataservice/framework/cloud/cloud_db.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_db.cpp @@ -15,37 +15,27 @@ #include "cloud/cloud_db.h" namespace OHOS::DistributedData { -int32_t CloudDB::Close() -{ - return E_NOT_SUPPORT; -} - -int32_t CloudDB::Execute(const std::string &table, const std::string &sql) +int32_t CloudDB::Execute(const std::string &table, const std::string &sql, const VBucket &extend) { return E_NOT_SUPPORT; } -int32_t CloudDB::BatchInsert(const std::string &table, VBuckets &&values) +int32_t CloudDB::BatchInsert(const std::string &table, VBuckets &&values, VBuckets &extends) { return E_NOT_SUPPORT; } -int32_t CloudDB::BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) +int32_t CloudDB::BatchUpdate(const std::string &table, VBuckets &&values, const VBuckets &extends) { return E_NOT_SUPPORT; } -int32_t CloudDB::Delete(const std::string &table, const std::string &sql, Values &&args) +int32_t CloudDB::BatchDelete(const std::string &table, const VBuckets &extends) { return E_NOT_SUPPORT; } -std::shared_ptr CloudDB::Query(const std::string &table, const std::string &sql, Values &&args) -{ - return nullptr; -} - -std::shared_ptr CloudDB::Query(const std::string &table, const GenQuery &query) +std::shared_ptr CloudDB::Query(const std::string &table, const VBucket &extend) { return nullptr; } @@ -65,42 +55,22 @@ int32_t CloudDB::Unwatch(int32_t origin, Watcher &watcher) return E_NOT_SUPPORT; } -int32_t CloudDB::Execute(const std::string &table, const std::string &sql, const VBucket &extend) -{ - return E_NOT_SUPPORT; -} - -int32_t CloudDB::BatchInsert(const std::string &table, VBuckets &&values, VBuckets &extends) -{ - return E_NOT_SUPPORT; -} - -int32_t CloudDB::BatchUpdate(const std::string &table, VBuckets &&values, const VBuckets &extends) -{ - return E_NOT_SUPPORT; -} - -int32_t CloudDB::BatchDelete(const std::string &table, const VBuckets &extends) +int32_t CloudDB::Lock() { return E_NOT_SUPPORT; } -std::shared_ptr CloudDB::Query(const std::string &table, const VBucket &extend) -{ - return nullptr; -} - -int32_t CloudDB::Lock() +int32_t CloudDB::Heartbeat() { return E_NOT_SUPPORT; } -int32_t CloudDB::Heartbeat() +int32_t CloudDB::Unlock() { return E_NOT_SUPPORT; } -int32_t CloudDB::Unlock() +int32_t CloudDB::Close() { return E_NOT_SUPPORT; } diff --git a/services/distributeddataservice/framework/feature/feature_system.cpp b/services/distributeddataservice/framework/feature/feature_system.cpp index 6013e9cf..5225f27e 100644 --- a/services/distributeddataservice/framework/feature/feature_system.cpp +++ b/services/distributeddataservice/framework/feature/feature_system.cpp @@ -22,19 +22,33 @@ FeatureSystem &FeatureSystem::GetInstance() return instance; } -int32_t FeatureSystem::RegisterCreator(const std::string &name, Creator creator) +int32_t FeatureSystem::RegisterCreator(const std::string &name, Creator creator, int32_t flag) { - creators_.InsertOrAssign(name, std::move(creator)); - return STUB_SUCCESS; + creators_.InsertOrAssign(name, std::pair{ std::move(creator), flag }); + return E_OK; } FeatureSystem::Creator FeatureSystem::GetCreator(const std::string &name) { - auto it = creators_.Find(name); - if (it.first) { - return it.second; + auto [success, pair] = creators_.Find(name); + if (!success) { + return nullptr; } - return nullptr; + auto [creator, flag] = std::move(pair); + return creator; +} + +std::vector FeatureSystem::GetFeatureName(int32_t flag) +{ + std::vector features; + creators_.ForEach([flag, &features](const std::string &key, auto &pair) -> bool { + auto &[creator, bindFlag] = pair; + if (bindFlag == flag) { + features.push_back(key); + } + return false; + }); + return features; } FeatureSystem::Feature::~Feature() @@ -43,43 +57,48 @@ FeatureSystem::Feature::~Feature() int32_t FeatureSystem::Feature::OnInitialize() { - return STUB_SUCCESS; + return E_OK; +} + +int32_t FeatureSystem::Feature::OnExecutor(std::shared_ptr executors) +{ + return E_OK; } int32_t FeatureSystem::Feature::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) { - return STUB_SUCCESS; + return E_OK; } int32_t FeatureSystem::Feature::OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId) { - return STUB_SUCCESS; + return E_OK; } int32_t FeatureSystem::Feature::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) { - return STUB_SUCCESS; + return E_OK; } int32_t FeatureSystem::Feature::OnUserChange(uint32_t code, const std::string &user, const std::string &account) { - return STUB_SUCCESS; + return E_OK; } int32_t FeatureSystem::Feature::Online(const std::string &device) { - return STUB_SUCCESS; + return E_OK; } int32_t FeatureSystem::Feature::Offline(const std::string &device) { - return STUB_SUCCESS; + return E_OK; } int32_t FeatureSystem::Feature::OnReady(const std::string &device) { - return STUB_SUCCESS; + return E_OK; } } } \ No newline at end of file diff --git a/services/distributeddataservice/framework/include/cloud/cloud_db.h b/services/distributeddataservice/framework/include/cloud/cloud_db.h index 9b67c9a4..57d15f5c 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_db.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_db.h @@ -15,22 +15,19 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_DB_H #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_DB_H -#include "store/general_store.h" +#include +#include + +#include "store/cursor.h" +#include "store/general_value.h" +#include "store/general_watcher.h" #include "visibility.h" namespace OHOS::DistributedData { -class API_EXPORT CloudDB : public GeneralStore { +class API_EXPORT CloudDB { public: - int32_t Close() override; - int32_t Execute(const std::string &table, const std::string &sql) override; - int32_t BatchInsert(const std::string &table, VBuckets &&values) override; - int32_t BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) 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, const GenQuery &query) override; - int32_t Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait) override; - int32_t Watch(int32_t origin, Watcher &watcher) override; - int32_t Unwatch(int32_t origin, Watcher &watcher) override; - + using Watcher = GeneralWatcher; + using Async = std::function>)>; + using Devices = std::vector; virtual int32_t Execute(const std::string &table, const std::string &sql, const VBucket &extend); virtual int32_t BatchInsert(const std::string &table, VBuckets &&values, VBuckets &extends); @@ -41,11 +38,19 @@ public: virtual std::shared_ptr Query(const std::string &table, const VBucket &extend); + virtual int32_t Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait); + + virtual int32_t Watch(int32_t origin, Watcher &watcher); + + virtual int32_t Unwatch(int32_t origin, Watcher &watcher); + virtual int32_t Lock(); virtual int32_t Heartbeat(); virtual int32_t Unlock(); + + virtual int32_t Close(); }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_DB_H diff --git a/services/distributeddataservice/framework/include/feature/feature_system.h b/services/distributeddataservice/framework/include/feature/feature_system.h index 7b4ad3f7..8aa732cf 100644 --- a/services/distributeddataservice/framework/include/feature/feature_system.h +++ b/services/distributeddataservice/framework/include/feature/feature_system.h @@ -15,7 +15,10 @@ #ifndef OHOS_DISTRIBUTED_DATA_FRAMEWORK_SYSTEM_SYSTEM_H #define OHOS_DISTRIBUTED_DATA_FRAMEWORK_SYSTEM_SYSTEM_H #include + #include "concurrent_map.h" +#include "error/general_error.h" +#include "executor_pool.h" #include "visibility.h" namespace DistributedDB { struct AutoLaunchParam; @@ -25,12 +28,18 @@ class MessageParcel; namespace DistributedData { class API_EXPORT FeatureSystem { public: - static constexpr int32_t STUB_SUCCESS = 0; + using Error = GeneralError; + inline static constexpr int32_t STUB_SUCCESS = Error::E_OK; + enum BindFlag : int32_t { + BIND_LAZY, + BIND_NOW + }; class API_EXPORT Feature { public: virtual ~Feature(); virtual int OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply) = 0; virtual int32_t OnInitialize(); + virtual int32_t OnExecutor(std::shared_ptr executors); virtual int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName); virtual int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); virtual int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m); @@ -41,8 +50,9 @@ public: }; using Creator = std::function()>; static FeatureSystem &GetInstance(); - int32_t RegisterCreator(const std::string &name, Creator creator); + int32_t RegisterCreator(const std::string &name, Creator creator, int32_t flag = BIND_LAZY); Creator GetCreator(const std::string &name); + std::vector GetFeatureName(int32_t flag); private: FeatureSystem() = default; @@ -51,7 +61,7 @@ private: FeatureSystem &operator=(const FeatureSystem &) = delete; FeatureSystem &operator=(FeatureSystem &&) = delete; - ConcurrentMap creators_; + ConcurrentMap> creators_; }; } // namespace DistributedData } diff --git a/services/distributeddataservice/framework/include/store/general_store.h b/services/distributeddataservice/framework/include/store/general_store.h index 3b88c4f4..56cdcf87 100644 --- a/services/distributeddataservice/framework/include/store/general_store.h +++ b/services/distributeddataservice/framework/include/store/general_store.h @@ -15,12 +15,14 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_STORE_H #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_STORE_H -#include #include +#include + #include "store/cursor.h" #include "store/general_value.h" #include "store/general_watcher.h" namespace OHOS::DistributedData { +class CloudDB; class GeneralStore { public: using Watcher = GeneralWatcher; @@ -29,7 +31,7 @@ public: virtual ~GeneralStore() = default; - virtual int32_t Close() = 0; + virtual int32_t Bind(std::shared_ptr cloudDb) = 0; virtual int32_t Execute(const std::string &table, const std::string &sql) = 0; @@ -41,13 +43,15 @@ public: virtual std::shared_ptr Query(const std::string &table, const std::string &sql, Values &&args) = 0; - virtual std::shared_ptr Query(const std::string &table, const GenQuery &query) = 0; + virtual std::shared_ptr Query(const std::string &table, GenQuery &query) = 0; - virtual int32_t Sync(const Devices &devices, int32_t mode, const GenQuery &query, Async async, int32_t wait) = 0; + virtual int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) = 0; virtual int32_t Watch(int32_t origin, Watcher &watcher) = 0; virtual int32_t Unwatch(int32_t origin, Watcher &watcher) = 0; + + virtual int32_t Close() = 0; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_STORE_H diff --git a/services/distributeddataservice/framework/include/store/general_value.h b/services/distributeddataservice/framework/include/store/general_value.h index eca104b1..9f2a6407 100644 --- a/services/distributeddataservice/framework/include/store/general_value.h +++ b/services/distributeddataservice/framework/include/store/general_value.h @@ -35,7 +35,17 @@ struct Asset { struct GenQuery { virtual ~GenQuery() = default; - virtual int32_t GetInterfaceId() = 0; + virtual bool IsEqual(uint64_t tid) = 0; + + template + int32_t QueryInterface(T *&query) + { + if (!IsEqual(T::TYPE_ID)) { + return E_INVALID_ARGS; + } + query = static_cast(this); + return E_OK; + }; }; using Assets = std::vector; diff --git a/services/distributeddataservice/service/object/object_service_impl.cpp b/services/distributeddataservice/service/object/object_service_impl.cpp index 8b65f0ed..745b3c66 100644 --- a/services/distributeddataservice/service/object/object_service_impl.cpp +++ b/services/distributeddataservice/service/object/object_service_impl.cpp @@ -38,7 +38,12 @@ using FeatureSystem = OHOS::DistributedData::FeatureSystem; __attribute__((used)) ObjectServiceImpl::Factory ObjectServiceImpl::factory_; ObjectServiceImpl::Factory::Factory() { - FeatureSystem::GetInstance().RegisterCreator("data_object", []() { return std::make_shared(); }); + FeatureSystem::GetInstance().RegisterCreator( + "data_object", + []() { + return std::make_shared(); + }, + FeatureSystem::BIND_NOW); } ObjectServiceImpl::Factory::~Factory() diff --git a/services/distributeddataservice/service/test/config_factory_test.cpp b/services/distributeddataservice/service/test/config_factory_test.cpp index b1ee7541..70a6a5ad 100644 --- a/services/distributeddataservice/service/test/config_factory_test.cpp +++ b/services/distributeddataservice/service/test/config_factory_test.cpp @@ -69,7 +69,7 @@ HWTEST_F(ConfigFactoryTest, ComponentConfig, TestSize.Level0) ASSERT_EQ(config.destructor, ""); ASSERT_EQ(config.params, "{\"count\":1,\"key\":\"value\"}"); const ComponentConfig &cfg = (*components)[1]; - ASSERT_EQ(cfg.lib, "libconfigdemo2.z.so"); + ASSERT_EQ(cfg.lib, "libdistributedclouddata.z.so"); } /** -- Gitee From ba0883a2188363d91683501b2f24fefd2531ab63 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sat, 6 May 2023 10:40:50 +0800 Subject: [PATCH 096/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../src/account_delegate_default_impl.cpp | 2 +- .../src/account_delegate_default_impl.h | 2 +- .../src/account_delegate_normal_impl.cpp | 5 +- .../src/account_delegate_normal_impl.h | 3 +- .../communicator/src/app_pipe_handler.h | 1 - .../src/device_manager_adapter.cpp | 25 ++++--- .../src/behaviour/behaviour_reporter_impl.cpp | 5 +- .../src/behaviour/behaviour_reporter_impl.h | 4 ++ .../src/fault/communication_fault_impl.cpp | 6 +- .../dfx/src/fault/communication_fault_impl.h | 4 ++ .../dfx/src/fault/database_fault_impl.cpp | 5 +- .../dfx/src/fault/database_fault_impl.h | 4 ++ .../dfx/src/fault/runtime_fault_impl.cpp | 6 +- .../dfx/src/fault/runtime_fault_impl.h | 4 ++ .../dfx/src/fault/service_fault_impl.cpp | 6 +- .../dfx/src/fault/service_fault_impl.h | 4 ++ .../adapter/dfx/src/hiview_adapter.cpp | 67 +++++++++---------- .../adapter/dfx/src/hiview_adapter.h | 27 ++++---- .../adapter/dfx/src/reporter.cpp | 9 +++ .../api_performance_statistic_impl.cpp | 5 +- .../api_performance_statistic_impl.h | 4 ++ .../src/statistic/database_statistic_impl.cpp | 6 +- .../src/statistic/database_statistic_impl.h | 4 ++ .../src/statistic/traffic_statistic_impl.cpp | 6 +- .../src/statistic/traffic_statistic_impl.h | 4 ++ .../src/statistic/visit_statistic_impl.cpp | 6 +- .../dfx/src/statistic/visit_statistic_impl.h | 4 ++ .../unittest/distributeddata_dfx_mst_test.cpp | 13 +++- .../unittest/distributeddata_dfx_ut_test.cpp | 4 ++ .../include/account/account_delegate.h | 3 +- .../communicator/device_manager_adapter.h | 7 +- .../adapter/include/dfx/reporter.h | 8 +++ .../app/src/feature_stub_impl.cpp | 15 ++++- .../app/src/feature_stub_impl.h | 2 +- .../app/src/kvstore_account_observer.cpp | 5 +- .../app/src/kvstore_account_observer.h | 10 ++- .../app/src/kvstore_data_service.cpp | 40 ++++++----- .../app/src/kvstore_data_service.h | 12 ++-- .../app/src/kvstore_meta_manager.cpp | 5 +- .../app/src/kvstore_meta_manager.h | 4 +- .../app/src/security/security.cpp | 2 +- .../app/src/security/security.h | 6 +- .../src/session_manager/upgrade_manager.cpp | 11 ++- .../app/src/session_manager/upgrade_manager.h | 2 +- .../app/src/task_manager.cpp | 55 +++++++++++++++ .../app/src/task_manager.h | 40 +++++++++++ .../app/src/uninstaller/uninstaller.h | 5 +- .../app/src/uninstaller/uninstaller_impl.cpp | 5 +- .../app/src/uninstaller/uninstaller_impl.h | 2 +- .../distributeddataservice/app/test/BUILD.gn | 1 + .../test/unittest/session_manager_test.cpp | 5 +- .../framework/feature/feature_system.cpp | 7 +- .../include/feature/feature_system.h | 2 + .../service/backup/include/backup_manager.h | 7 +- .../service/backup/src/backup_manager.cpp | 46 +++++++------ .../service/bootstrap/include/bootstrap.h | 3 +- .../service/bootstrap/src/bootstrap.cpp | 4 +- .../service/kvdb/kvdb_service_impl.cpp | 8 +++ .../service/kvdb/kvdb_service_impl.h | 3 +- .../service/kvdb/kvstore_sync_manager.cpp | 18 +++-- .../service/kvdb/kvstore_sync_manager.h | 5 +- .../service/kvdb/store_cache.cpp | 13 ++-- .../service/kvdb/store_cache.h | 11 +-- .../service/kvdb/user_delegate.cpp | 7 +- .../service/kvdb/user_delegate.h | 3 +- .../service/object/object_manager.cpp | 19 ++++-- .../service/object/object_manager.h | 6 +- .../service/object/object_service_impl.cpp | 5 ++ .../service/object/object_service_impl.h | 2 + .../service/rdb/rdb_service_impl.cpp | 25 ++++--- .../service/rdb/rdb_service_impl.h | 5 +- .../service/rdb/rdb_syncer.cpp | 10 --- .../service/rdb/rdb_syncer.h | 5 -- 73 files changed, 476 insertions(+), 228 deletions(-) create mode 100644 services/distributeddataservice/app/src/task_manager.cpp create mode 100644 services/distributeddataservice/app/src/task_manager.h diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp index b9d90b27..0ac69a0e 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp +++ b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp @@ -49,7 +49,7 @@ bool AccountDelegateDefaultImpl::QueryUsers(std::vector &users) return true; } -void AccountDelegateDefaultImpl::SubscribeAccountEvent() +void AccountDelegateDefaultImpl::SubscribeAccountEvent(std::shared_ptr executors) { ZLOGD("no account part."); } diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h index 1bfbcb10..811739a3 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h +++ b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h @@ -25,7 +25,7 @@ public: std::string GetCurrentAccountId() const override; int32_t GetUserByToken(uint32_t tokenId) const override; bool QueryUsers(std::vector &users) override; - void SubscribeAccountEvent() override; + void SubscribeAccountEvent(std::shared_ptr executors) override; void UnsubscribeAccountEvent() override; private: ~AccountDelegateDefaultImpl(); diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp index d9e96dee..562820b6 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp +++ b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp @@ -77,7 +77,7 @@ bool AccountDelegateNormalImpl::QueryUsers(std::vector &users) return AccountSA::OsAccountManager::QueryActiveOsAccountIds(users) == 0; } -void AccountDelegateNormalImpl::SubscribeAccountEvent() +void AccountDelegateNormalImpl::SubscribeAccountEvent(std::shared_ptr executors) { ZLOGI("Subscribe account event listener start."); MatchingSkills matchingSkills; @@ -90,7 +90,7 @@ void AccountDelegateNormalImpl::SubscribeAccountEvent() NotifyAccountChanged(account); }); - std::thread th = std::thread([eventSubscriber = eventSubscriber_]() { + executors->Execute([eventSubscriber = eventSubscriber_]() { int tryTimes = 0; constexpr int MAX_RETRY_TIME = 300; constexpr int RETRY_WAIT_TIME_S = 1; @@ -111,7 +111,6 @@ void AccountDelegateNormalImpl::SubscribeAccountEvent() } ZLOGI("success to register subscriber."); }); - th.detach(); } AccountDelegateNormalImpl::~AccountDelegateNormalImpl() diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h index 153dfa24..3bc7c18b 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h +++ b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h @@ -19,6 +19,7 @@ #include "common_event_manager.h" #include "common_event_subscriber.h" #include "common_event_support.h" +#include "executor_pool.h" #include "log_print.h" namespace OHOS { @@ -29,7 +30,7 @@ public: std::string GetCurrentAccountId() const override; int32_t GetUserByToken(uint32_t tokenId) const override; bool QueryUsers(std::vector &users) override; - void SubscribeAccountEvent() override; + void SubscribeAccountEvent(std::shared_ptr executors) override; void UnsubscribeAccountEvent() override; private: diff --git a/services/distributeddataservice/adapter/communicator/src/app_pipe_handler.h b/services/distributeddataservice/adapter/communicator/src/app_pipe_handler.h index ec8db4b9..5fe49b7e 100644 --- a/services/distributeddataservice/adapter/communicator/src/app_pipe_handler.h +++ b/services/distributeddataservice/adapter/communicator/src/app_pipe_handler.h @@ -20,7 +20,6 @@ #include #include #include -#include "task_scheduler.h" #include "log_print.h" #include "reporter.h" #include "app_data_change_listener.h" diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index d2cafb1f..9fdf2d15 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -59,17 +59,19 @@ void DataMgrDmStateCall::OnDeviceReady(const DmDeviceInfo &info) class DataMgrDmInitCall final : public DistributedHardware::DmInitCallback { public: - explicit DataMgrDmInitCall(DeviceManagerAdapter &dmAdapter) : dmAdapter_(dmAdapter) {} + explicit DataMgrDmInitCall(DeviceManagerAdapter &dmAdapter, std::shared_ptr executors) + : dmAdapter_(dmAdapter), executors_(executors){} void OnRemoteDied() override; private: DeviceManagerAdapter &dmAdapter_; + std::shared_ptr executors_; }; void DataMgrDmInitCall::OnRemoteDied() { ZLOGI("device manager died, init again"); - dmAdapter_.Init(); + dmAdapter_.Init(executors_); } DeviceManagerAdapter::DeviceManagerAdapter() @@ -88,9 +90,10 @@ DeviceManagerAdapter &DeviceManagerAdapter::GetInstance() return dmAdapter; } -void DeviceManagerAdapter::Init() +void DeviceManagerAdapter::Init(std::shared_ptr executors) { ZLOGI("begin"); + executors_ = std::move(executors); RegDevCallback()(); } @@ -99,15 +102,14 @@ std::function DeviceManagerAdapter::RegDevCallback() return [this]() { auto &devManager = DeviceManager::GetInstance(); auto dmStateCall = std::make_shared(*this); - auto dmInitCall = std::make_shared(*this); + auto dmInitCall = std::make_shared(*this, executors_); auto resultInit = devManager.InitDeviceManager(PKG_NAME, dmInitCall); auto resultState = devManager.RegisterDevStateCallback(PKG_NAME, "", dmStateCall); if (resultInit == DM_OK && resultState == DM_OK) { return; } constexpr int32_t INTERVAL = 500; - auto time = std::chrono::steady_clock::now() + std::chrono::milliseconds(INTERVAL); - scheduler_.At(time, RegDevCallback()); + executors_->Schedule(RegDevCallback(), std::chrono::milliseconds(INTERVAL)); }; } @@ -167,8 +169,11 @@ void DeviceManagerAdapter::Online(const DmDeviceInfo &info) item->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_ONLINE); } } - auto time = std::chrono::steady_clock::now() + std::chrono::milliseconds(SYNC_TIMEOUT); - scheduler_.At(time, [this, dvInfo]() { TimeOut(dvInfo.uuid); }); + executors_->Schedule( + [this, dvInfo]() { + TimeOut(dvInfo.uuid); + }, + std::chrono::milliseconds(SYNC_TIMEOUT)); syncTask_.Insert(dvInfo.uuid, dvInfo.uuid); for (const auto &item : observers) { // set compatible identify, sync service meta if (item == nullptr) { @@ -236,7 +241,7 @@ void DeviceManagerAdapter::Offline(const DmDeviceInfo &info) return false; }); }; - scheduler_.Execute(std::move(task)); + executors_->Execute(std::move(task)); } void DeviceManagerAdapter::OnChanged(const DmDeviceInfo &info) @@ -267,7 +272,7 @@ void DeviceManagerAdapter::OnReady(const DmDeviceInfo &info) return false; }); }; - scheduler_.Execute(std::move(task)); + executors_->Execute(std::move(task)); } bool DeviceManagerAdapter::GetDeviceInfo(const DmDeviceInfo &dmInfo, DeviceInfo &dvInfo) diff --git a/services/distributeddataservice/adapter/dfx/src/behaviour/behaviour_reporter_impl.cpp b/services/distributeddataservice/adapter/dfx/src/behaviour/behaviour_reporter_impl.cpp index d109c720..777e14c8 100644 --- a/services/distributeddataservice/adapter/dfx/src/behaviour/behaviour_reporter_impl.cpp +++ b/services/distributeddataservice/adapter/dfx/src/behaviour/behaviour_reporter_impl.cpp @@ -19,9 +19,12 @@ namespace OHOS { namespace DistributedDataDfx { ReportStatus BehaviourReporterImpl::Report(const BehaviourMsg &msg) { - HiViewAdapter::ReportBehaviour(DfxCodeConstant::DATABASE_BEHAVIOUR, msg); + HiViewAdapter::ReportBehaviour(DfxCodeConstant::DATABASE_BEHAVIOUR, msg, executors_); return ReportStatus::SUCCESS; } +void BehaviourReporterImpl::SetThreadPool(std::shared_ptr executors) { + executors_ = executors; +} } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/behaviour/behaviour_reporter_impl.h b/services/distributeddataservice/adapter/dfx/src/behaviour/behaviour_reporter_impl.h index 0c3face2..7ac010b1 100644 --- a/services/distributeddataservice/adapter/dfx/src/behaviour/behaviour_reporter_impl.h +++ b/services/distributeddataservice/adapter/dfx/src/behaviour/behaviour_reporter_impl.h @@ -25,6 +25,10 @@ class BehaviourReporterImpl : public BehaviourReporter { public: virtual ~BehaviourReporterImpl() {} ReportStatus Report(const struct BehaviourMsg &msg) override; + void SetThreadPool(std::shared_ptr executors); + +private: + std::shared_ptr executors_; }; } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/fault/communication_fault_impl.cpp b/services/distributeddataservice/adapter/dfx/src/fault/communication_fault_impl.cpp index 9c1b32be..ea5b488f 100644 --- a/services/distributeddataservice/adapter/dfx/src/fault/communication_fault_impl.cpp +++ b/services/distributeddataservice/adapter/dfx/src/fault/communication_fault_impl.cpp @@ -19,8 +19,12 @@ namespace OHOS { namespace DistributedDataDfx { ReportStatus CommunicationFaultImpl::Report(const CommFaultMsg &msg) { - HiViewAdapter::ReportCommFault(DfxCodeConstant::DATABASE_SYNC_FAILED, msg); + HiViewAdapter::ReportCommFault(DfxCodeConstant::DATABASE_SYNC_FAILED, msg, executors_); return ReportStatus::SUCCESS; } +void CommunicationFaultImpl::SetThreadPool(std::shared_ptr executors) +{ + executors_ = executors; +} } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/fault/communication_fault_impl.h b/services/distributeddataservice/adapter/dfx/src/fault/communication_fault_impl.h index c78cd679..b5ec17f5 100644 --- a/services/distributeddataservice/adapter/dfx/src/fault/communication_fault_impl.h +++ b/services/distributeddataservice/adapter/dfx/src/fault/communication_fault_impl.h @@ -34,6 +34,10 @@ public: return ReportStatus::SUCCESS; }; ReportStatus Report(const struct CommFaultMsg &msg) override; + void SetThreadPool(std::shared_ptr executors); + +private: + std::shared_ptr executors_; }; } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/fault/database_fault_impl.cpp b/services/distributeddataservice/adapter/dfx/src/fault/database_fault_impl.cpp index 3e9b3de5..f7e99cfa 100644 --- a/services/distributeddataservice/adapter/dfx/src/fault/database_fault_impl.cpp +++ b/services/distributeddataservice/adapter/dfx/src/fault/database_fault_impl.cpp @@ -30,8 +30,11 @@ ReportStatus DatabaseFaultImpl::Report(const DBFaultMsg &msg) return ReportStatus::ERROR; } - HiViewAdapter::ReportDBFault(eventID, msg); + HiViewAdapter::ReportDBFault(eventID, msg, executors_); return ReportStatus::SUCCESS; } +void DatabaseFaultImpl::SetThreadPool(std::shared_ptr executors) { + executors_ = executors; +} } // namespace DistributedDataDfx } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/adapter/dfx/src/fault/database_fault_impl.h b/services/distributeddataservice/adapter/dfx/src/fault/database_fault_impl.h index 16a054f4..66c5b39b 100644 --- a/services/distributeddataservice/adapter/dfx/src/fault/database_fault_impl.h +++ b/services/distributeddataservice/adapter/dfx/src/fault/database_fault_impl.h @@ -33,6 +33,10 @@ public: return ReportStatus::SUCCESS; }; ReportStatus Report(const DBFaultMsg &msg) override; + void SetThreadPool(std::shared_ptr executors); + +private: + std::shared_ptr executors_; }; } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/fault/runtime_fault_impl.cpp b/services/distributeddataservice/adapter/dfx/src/fault/runtime_fault_impl.cpp index 3fa0543a..3e2dae52 100644 --- a/services/distributeddataservice/adapter/dfx/src/fault/runtime_fault_impl.cpp +++ b/services/distributeddataservice/adapter/dfx/src/fault/runtime_fault_impl.cpp @@ -19,8 +19,12 @@ namespace OHOS { namespace DistributedDataDfx { ReportStatus RuntimeFaultImpl::Report(const FaultMsg &msg) { - HiViewAdapter::ReportFault(DfxCodeConstant::RUNTIME_FAULT, msg); + HiViewAdapter::ReportFault(DfxCodeConstant::RUNTIME_FAULT, msg, executors_); return ReportStatus::SUCCESS; } +void RuntimeFaultImpl::SetThreadPool(std::shared_ptr executors) +{ + executors_ = executors; +} } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/fault/runtime_fault_impl.h b/services/distributeddataservice/adapter/dfx/src/fault/runtime_fault_impl.h index 47b20a35..f4aea1b0 100644 --- a/services/distributeddataservice/adapter/dfx/src/fault/runtime_fault_impl.h +++ b/services/distributeddataservice/adapter/dfx/src/fault/runtime_fault_impl.h @@ -32,6 +32,10 @@ public: return ReportStatus::SUCCESS; }; ReportStatus Report(const FaultMsg &msg) override; + void SetThreadPool(std::shared_ptr executors); + +private: + std::shared_ptr executors_; }; } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/fault/service_fault_impl.cpp b/services/distributeddataservice/adapter/dfx/src/fault/service_fault_impl.cpp index 5fb3a55f..b1eb464f 100644 --- a/services/distributeddataservice/adapter/dfx/src/fault/service_fault_impl.cpp +++ b/services/distributeddataservice/adapter/dfx/src/fault/service_fault_impl.cpp @@ -19,8 +19,12 @@ namespace OHOS { namespace DistributedDataDfx { ReportStatus ServiceFaultImpl::Report(const OHOS::DistributedDataDfx::FaultMsg &msg) { - HiViewAdapter::ReportFault(DfxCodeConstant::SERVICE_FAULT, msg); + HiViewAdapter::ReportFault(DfxCodeConstant::SERVICE_FAULT, msg, executors_); return ReportStatus::SUCCESS; } +void ServiceFaultImpl::SetThreadPool(std::shared_ptr executors) +{ + executors_ = executors; +} } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/fault/service_fault_impl.h b/services/distributeddataservice/adapter/dfx/src/fault/service_fault_impl.h index 4d79300c..fb5eed6a 100644 --- a/services/distributeddataservice/adapter/dfx/src/fault/service_fault_impl.h +++ b/services/distributeddataservice/adapter/dfx/src/fault/service_fault_impl.h @@ -33,6 +33,10 @@ public: return ReportStatus::SUCCESS; }; ReportStatus Report(const FaultMsg &msg) override; + void SetThreadPool(std::shared_ptr executors); + +private: + std::shared_ptr executors_; }; } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp b/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp index 59c44c8c..2caa310f 100644 --- a/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp +++ b/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp @@ -79,12 +79,11 @@ std::mutex HiViewAdapter::apiPerformanceMutex_; std::map> HiViewAdapter::apiPerformanceStat_; bool HiViewAdapter::running_ = false; -TaskScheduler HiViewAdapter::scheduler_ {"HiView"}; std::mutex HiViewAdapter::runMutex_; -void HiViewAdapter::ReportFault(int dfxCode, const FaultMsg &msg) +void HiViewAdapter::ReportFault(int dfxCode, const FaultMsg &msg, std::shared_ptr executors) { - KvStoreTask task([dfxCode, msg]() { + ExecutorPool::Task task([dfxCode, msg]() { HiSysEventWrite(HiSysEvent::Domain::DISTRIBUTED_DATAMGR, CoverEventID(dfxCode), HiSysEvent::EventType::FAULT, @@ -93,12 +92,12 @@ void HiViewAdapter::ReportFault(int dfxCode, const FaultMsg &msg) INTERFACE_NAME, msg.interfaceName, ERROR_TYPE, static_cast(msg.errorType)); }); - scheduler_.At(std::chrono::steady_clock::now(), std::move(task)); + executors->Execute(std::move(task)); } -void HiViewAdapter::ReportDBFault(int dfxCode, const DBFaultMsg &msg) +void HiViewAdapter::ReportDBFault(int dfxCode, const DBFaultMsg &msg, std::shared_ptr executors) { - KvStoreTask task([dfxCode, msg]() { + ExecutorPool::Task task([dfxCode, msg]() { HiSysEventWrite(HiSysEvent::Domain::DISTRIBUTED_DATAMGR, CoverEventID(dfxCode), HiSysEvent::EventType::FAULT, @@ -107,13 +106,12 @@ void HiViewAdapter::ReportDBFault(int dfxCode, const DBFaultMsg &msg) MODULE_NAME, msg.moduleName, ERROR_TYPE, static_cast(msg.errorType)); }); - scheduler_.At(std::chrono::steady_clock::now(), std::move(task)); + executors->Execute(std::move(task)); } - -void HiViewAdapter::ReportCommFault(int dfxCode, const CommFaultMsg &msg) +void HiViewAdapter::ReportCommFault(int dfxCode, const CommFaultMsg &msg, std::shared_ptr executors) { - KvStoreTask task([dfxCode, msg]() { + ExecutorPool ::Task task([dfxCode, msg]() { std::string message; for (size_t i = 0; i < msg.deviceId.size(); i++) { message.append("No: ").append(std::to_string(i)) @@ -128,12 +126,12 @@ void HiViewAdapter::ReportCommFault(int dfxCode, const CommFaultMsg &msg) STORE_ID, msg.storeId, SYNC_ERROR_INFO, message); }); - scheduler_.At(std::chrono::steady_clock::now(), std::move(task)); + executors->Execute(std::move(task)); } -void HiViewAdapter::ReportBehaviour(int dfxCode, const BehaviourMsg &msg) +void HiViewAdapter::ReportBehaviour(int dfxCode, const BehaviourMsg &msg, std::shared_ptr executors) { - KvStoreTask task([dfxCode, msg]() { + ExecutorPool::Task task([dfxCode, msg]() { std::string message; message.append("Behaviour type : ").append(std::to_string(static_cast(msg.behaviourType))) .append(" behaviour info : ").append(msg.extensionInfo); @@ -145,19 +143,19 @@ void HiViewAdapter::ReportBehaviour(int dfxCode, const BehaviourMsg &msg) STORE_ID, msg.storeId, BEHAVIOUR_INFO, message); }); - scheduler_.At(std::chrono::steady_clock::now(), std::move(task)); + executors->Execute(std::move(task)); } -void HiViewAdapter::ReportDatabaseStatistic(int dfxCode, const DbStat &stat) +void HiViewAdapter::ReportDatabaseStatistic(int dfxCode, const DbStat &stat, std::shared_ptr executors) { - KvStoreTask task([dfxCode, stat]() { + ExecutorPool::Task task([dfxCode, stat]() { std::lock_guard lock(dbMutex_); if (!dbStat_.count(stat.GetKey())) { dbStat_.insert({stat.GetKey(), {stat, 0, dfxCode}}); } }); - scheduler_.At(std::chrono::steady_clock::now(), std::move(task)); - StartTimerThread(); + executors->Execute(std::move(task)); + StartTimerThread(executors); } void HiViewAdapter::ReportDbSize(const StatisticWrap &stat) @@ -205,9 +203,10 @@ void HiViewAdapter::InvokeDbSize() dbStat_.clear(); } -void HiViewAdapter::ReportTrafficStatistic(int dfxCode, const TrafficStat &stat) +void HiViewAdapter::ReportTrafficStatistic(int dfxCode, const TrafficStat &stat, + std::shared_ptr executors) { - KvStoreTask task([dfxCode, stat]() { + ExecutorPool::Task task([dfxCode, stat]() { std::lock_guard lock(trafficMutex_); auto it = trafficStat_.find(stat.GetKey()); if (it != trafficStat_.end()) { @@ -217,8 +216,8 @@ void HiViewAdapter::ReportTrafficStatistic(int dfxCode, const TrafficStat &stat) trafficStat_.insert({stat.GetKey(), {stat, 0, dfxCode}}); } }); - scheduler_.At(std::chrono::steady_clock::now(), std::move(task)); - StartTimerThread(); + executors->Execute(std::move(task)); + StartTimerThread(executors); } void HiViewAdapter::InvokeTraffic() @@ -243,9 +242,9 @@ void HiViewAdapter::InvokeTraffic() trafficStat_.clear(); } -void HiViewAdapter::ReportVisitStatistic(int dfxCode, const VisitStat &stat) +void HiViewAdapter::ReportVisitStatistic(int dfxCode, const VisitStat &stat, std::shared_ptr executors) { - KvStoreTask task([dfxCode, stat]() { + ExecutorPool ::Task task([dfxCode, stat]() { std::lock_guard lock(visitMutex_); auto it = visitStat_.find(stat.GetKey()); if (it == visitStat_.end()) { @@ -254,8 +253,8 @@ void HiViewAdapter::ReportVisitStatistic(int dfxCode, const VisitStat &stat) it->second.times++; } }); - scheduler_.At(std::chrono::steady_clock::now(), std::move(task)); - StartTimerThread(); + executors->Execute(std::move(task)); + StartTimerThread(executors); } void HiViewAdapter::InvokeVisit() @@ -273,9 +272,10 @@ void HiViewAdapter::InvokeVisit() visitStat_.clear(); } -void HiViewAdapter::ReportApiPerformanceStatistic(int dfxCode, const ApiPerformanceStat &stat) +void HiViewAdapter::ReportApiPerformanceStatistic(int dfxCode, const ApiPerformanceStat &stat, + std::shared_ptr executors) { - KvStoreTask task([dfxCode, stat]() { + ExecutorPool::Task task([dfxCode, stat]() { std::lock_guard lock(apiPerformanceMutex_); auto it = apiPerformanceStat_.find(stat.GetKey()); if (it == apiPerformanceStat_.end()) { @@ -294,8 +294,8 @@ void HiViewAdapter::ReportApiPerformanceStatistic(int dfxCode, const ApiPerforma } }); - scheduler_.At(std::chrono::steady_clock::now(), std::move(task)); - StartTimerThread(); + executors->Execute(std::move(task)); + StartTimerThread(executors); } void HiViewAdapter::InvokeApiPerformance() @@ -319,7 +319,7 @@ void HiViewAdapter::InvokeApiPerformance() ZLOGI("DdsTrace interface: clean"); } -void HiViewAdapter::StartTimerThread() +void HiViewAdapter::StartTimerThread(std::shared_ptr executors) { if (running_) { return; @@ -329,8 +329,7 @@ void HiViewAdapter::StartTimerThread() return; } running_ = true; - std::chrono::duration delay(0); - std::chrono::duration internal(WAIT_TIME); + auto interval = std::chrono::seconds(WAIT_TIME); auto fun = []() { time_t current = time(nullptr); tm localTime = { 0 }; @@ -342,7 +341,7 @@ void HiViewAdapter::StartTimerThread() InvokeTraffic(); InvokeVisit(); }; - scheduler_.Every(delay, internal, fun); + executors->Schedule(fun, interval); } std::string HiViewAdapter::CoverEventID(int dfxCode) diff --git a/services/distributeddataservice/adapter/dfx/src/hiview_adapter.h b/services/distributeddataservice/adapter/dfx/src/hiview_adapter.h index d7c57f93..21de2703 100644 --- a/services/distributeddataservice/adapter/dfx/src/hiview_adapter.h +++ b/services/distributeddataservice/adapter/dfx/src/hiview_adapter.h @@ -18,12 +18,11 @@ #include #include -#include "dfx_types.h" + #include "dfx_code_constant.h" +#include "dfx_types.h" +#include "executor_pool.h" #include "hisysevent.h" -#include "task_scheduler.h" -#include "kv_store_thread_pool.h" -#include "kv_store_task.h" #include "value_hash.h" namespace OHOS { @@ -38,15 +37,16 @@ struct StatisticWrap { class HiViewAdapter { public: ~HiViewAdapter(); - static void ReportFault(int dfxCode, const FaultMsg &msg); - static void ReportDBFault(int dfxCode, const DBFaultMsg &msg); - static void ReportCommFault(int dfxCode, const CommFaultMsg &msg); - static void ReportVisitStatistic(int dfxCode, const VisitStat &stat); - static void ReportTrafficStatistic(int dfxCode, const TrafficStat &stat); - static void ReportDatabaseStatistic(int dfxCode, const DbStat &stat); - static void ReportApiPerformanceStatistic(int dfxCode, const ApiPerformanceStat &stat); - static void ReportBehaviour(int dfxCode, const BehaviourMsg &msg); - static void StartTimerThread(); + static void ReportFault(int dfxCode, const FaultMsg &msg, std::shared_ptr executors); + static void ReportDBFault(int dfxCode, const DBFaultMsg &msg, std::shared_ptr executors); + static void ReportCommFault(int dfxCode, const CommFaultMsg &msg, std::shared_ptr executors); + static void ReportVisitStatistic(int dfxCode, const VisitStat &stat, std::shared_ptr executors); + static void ReportTrafficStatistic(int dfxCode, const TrafficStat &stat, std::shared_ptr executors); + static void ReportDatabaseStatistic(int dfxCode, const DbStat &stat, std::shared_ptr executors); + static void ReportApiPerformanceStatistic(int dfxCode, const ApiPerformanceStat &stat, + std::shared_ptr executors); + static void ReportBehaviour(int dfxCode, const BehaviourMsg &msg, std::shared_ptr executors); + static void StartTimerThread(std::shared_ptr executors); private: static std::mutex visitMutex_; @@ -70,7 +70,6 @@ private: private: static std::mutex runMutex_; static bool running_; - static TaskScheduler scheduler_; static const inline int DAILY_REPORT_TIME = 23; static const inline int WAIT_TIME = 1 * 60 * 60; // 1 hours }; diff --git a/services/distributeddataservice/adapter/dfx/src/reporter.cpp b/services/distributeddataservice/adapter/dfx/src/reporter.cpp index 41b96ab3..63102aec 100644 --- a/services/distributeddataservice/adapter/dfx/src/reporter.cpp +++ b/services/distributeddataservice/adapter/dfx/src/reporter.cpp @@ -37,54 +37,63 @@ Reporter* Reporter::GetInstance() FaultReporter* Reporter::CommunicationFault() { static CommunicationFaultImpl communicationFault; + communicationFault.SetThreadPool(executors_); return &communicationFault; } FaultReporter* Reporter::DatabaseFault() { static DatabaseFaultImpl databaseFault; + databaseFault.SetThreadPool(executors_); return &databaseFault; } FaultReporter* Reporter::RuntimeFault() { static RuntimeFaultImpl runtimeFault; + runtimeFault.SetThreadPool(executors_); return &runtimeFault; } FaultReporter* Reporter::ServiceFault() { static ServiceFaultImpl serviceFault; + serviceFault.SetThreadPool(executors_); return &serviceFault; } StatisticReporter* Reporter::TrafficStatistic() { static TrafficStatisticImpl trafficStatistic; + trafficStatistic.SetThreadPool(executors_); return &trafficStatistic; } StatisticReporter* Reporter::VisitStatistic() { static VisitStatisticImpl visitStatistic; + visitStatistic.SetThreadPool(executors_); return &visitStatistic; } StatisticReporter* Reporter::DatabaseStatistic() { static DatabaseStatisticImpl databaseStatistic; + databaseStatistic.SetThreadPool(executors_); return &databaseStatistic; } StatisticReporter* Reporter::ApiPerformanceStatistic() { static ApiPerformanceStatisticImpl apiPerformanceStat; + apiPerformanceStat.SetThreadPool(executors_); return &apiPerformanceStat; } BehaviourReporter* Reporter::BehaviourReporter() { static BehaviourReporterImpl behaviourReporterImpl; + behaviourReporterImpl.SetThreadPool(executors_); return &behaviourReporterImpl; } } // namespace DistributedDataDfx diff --git a/services/distributeddataservice/adapter/dfx/src/statistic/api_performance_statistic_impl.cpp b/services/distributeddataservice/adapter/dfx/src/statistic/api_performance_statistic_impl.cpp index 9e7d7897..da1bcd45 100644 --- a/services/distributeddataservice/adapter/dfx/src/statistic/api_performance_statistic_impl.cpp +++ b/services/distributeddataservice/adapter/dfx/src/statistic/api_performance_statistic_impl.cpp @@ -19,9 +19,12 @@ namespace OHOS { namespace DistributedDataDfx { ReportStatus ApiPerformanceStatisticImpl::Report(const ApiPerformanceStat &stat) { - HiViewAdapter::ReportApiPerformanceStatistic(DfxCodeConstant::API_PERFORMANCE_INTERFACE, stat); + HiViewAdapter::ReportApiPerformanceStatistic(DfxCodeConstant::API_PERFORMANCE_INTERFACE, stat, executors_); return ReportStatus::SUCCESS; } +void ApiPerformanceStatisticImpl::SetThreadPool(std::shared_ptr executors) { + executors_ = executors; +} } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/statistic/api_performance_statistic_impl.h b/services/distributeddataservice/adapter/dfx/src/statistic/api_performance_statistic_impl.h index 36692d95..f253ee7b 100644 --- a/services/distributeddataservice/adapter/dfx/src/statistic/api_performance_statistic_impl.h +++ b/services/distributeddataservice/adapter/dfx/src/statistic/api_performance_statistic_impl.h @@ -26,6 +26,10 @@ class ApiPerformanceStatisticImpl : public StatisticReporter public: virtual ~ApiPerformanceStatisticImpl() {} ReportStatus Report(const ApiPerformanceStat &stat) override; + void SetThreadPool(std::shared_ptr executors); + +private: + std::shared_ptr executors_; }; } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/statistic/database_statistic_impl.cpp b/services/distributeddataservice/adapter/dfx/src/statistic/database_statistic_impl.cpp index 9ffa814b..070e1828 100644 --- a/services/distributeddataservice/adapter/dfx/src/statistic/database_statistic_impl.cpp +++ b/services/distributeddataservice/adapter/dfx/src/statistic/database_statistic_impl.cpp @@ -19,8 +19,12 @@ namespace OHOS { namespace DistributedDataDfx { ReportStatus DatabaseStatisticImpl::Report(const DbStat &stat) { - HiViewAdapter::ReportDatabaseStatistic(DfxCodeConstant::DATABASE_STATISTIC, stat); + HiViewAdapter::ReportDatabaseStatistic(DfxCodeConstant::DATABASE_STATISTIC, stat, executors_); return ReportStatus::SUCCESS; } +void DatabaseStatisticImpl::SetThreadPool(std::shared_ptr executors) +{ + executors_ = executors; +} } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/statistic/database_statistic_impl.h b/services/distributeddataservice/adapter/dfx/src/statistic/database_statistic_impl.h index c3e90763..c3aa6774 100644 --- a/services/distributeddataservice/adapter/dfx/src/statistic/database_statistic_impl.h +++ b/services/distributeddataservice/adapter/dfx/src/statistic/database_statistic_impl.h @@ -25,6 +25,10 @@ class DatabaseStatisticImpl : public StatisticReporter { public: virtual ~DatabaseStatisticImpl() {} ReportStatus Report(const DbStat &stat) override; + void SetThreadPool(std::shared_ptr executors); + +private: + std::shared_ptr executors_; }; } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/statistic/traffic_statistic_impl.cpp b/services/distributeddataservice/adapter/dfx/src/statistic/traffic_statistic_impl.cpp index 2821a6e2..0ee80df4 100644 --- a/services/distributeddataservice/adapter/dfx/src/statistic/traffic_statistic_impl.cpp +++ b/services/distributeddataservice/adapter/dfx/src/statistic/traffic_statistic_impl.cpp @@ -19,8 +19,12 @@ namespace OHOS { namespace DistributedDataDfx { ReportStatus TrafficStatisticImpl::Report(const TrafficStat &stat) { - HiViewAdapter::ReportTrafficStatistic(DfxCodeConstant::TRAFFIC_STATISTIC, stat); + HiViewAdapter::ReportTrafficStatistic(DfxCodeConstant::TRAFFIC_STATISTIC, stat, executors_); return ReportStatus::SUCCESS; } +void TrafficStatisticImpl::SetThreadPool(std::shared_ptr executors) +{ + executors_ = executors; +} } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/statistic/traffic_statistic_impl.h b/services/distributeddataservice/adapter/dfx/src/statistic/traffic_statistic_impl.h index 3961b1c3..c65e8888 100644 --- a/services/distributeddataservice/adapter/dfx/src/statistic/traffic_statistic_impl.h +++ b/services/distributeddataservice/adapter/dfx/src/statistic/traffic_statistic_impl.h @@ -25,6 +25,10 @@ class TrafficStatisticImpl : public StatisticReporter { public: virtual ~TrafficStatisticImpl() {} ReportStatus Report(const TrafficStat &stat) override; + void SetThreadPool(std::shared_ptr executors); + +private: + std::shared_ptr executors_; }; } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/statistic/visit_statistic_impl.cpp b/services/distributeddataservice/adapter/dfx/src/statistic/visit_statistic_impl.cpp index 8b86b733..99536be8 100644 --- a/services/distributeddataservice/adapter/dfx/src/statistic/visit_statistic_impl.cpp +++ b/services/distributeddataservice/adapter/dfx/src/statistic/visit_statistic_impl.cpp @@ -19,8 +19,12 @@ namespace OHOS { namespace DistributedDataDfx { ReportStatus VisitStatisticImpl::Report(const VisitStat &stat) { - HiViewAdapter::ReportVisitStatistic(DfxCodeConstant::VISIT_STATISTIC, stat); + HiViewAdapter::ReportVisitStatistic(DfxCodeConstant::VISIT_STATISTIC, stat, executors_); return ReportStatus::SUCCESS; } +void VisitStatisticImpl::SetThreadPool(std::shared_ptr executors) +{ + executors_ = executors; +} } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/src/statistic/visit_statistic_impl.h b/services/distributeddataservice/adapter/dfx/src/statistic/visit_statistic_impl.h index fed8ab72..fb5b933e 100644 --- a/services/distributeddataservice/adapter/dfx/src/statistic/visit_statistic_impl.h +++ b/services/distributeddataservice/adapter/dfx/src/statistic/visit_statistic_impl.h @@ -25,6 +25,10 @@ class VisitStatisticImpl : public StatisticReporter { public: virtual ~VisitStatisticImpl() {} ReportStatus Report(const VisitStat &stat) override; + void SetThreadPool(std::shared_ptr executors); + +private: + std::shared_ptr executors_; }; } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_mst_test.cpp b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_mst_test.cpp index 521ad0f9..ea5fd516 100644 --- a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_mst_test.cpp +++ b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_mst_test.cpp @@ -29,11 +29,20 @@ public: void SetUp(); void TearDown(); +protected: + static std::shared_ptr executors_; }; -void DistributedataDfxMSTTest::SetUpTestCase() {} +std::shared_ptr DistributedataDfxMSTTest::executors_ = std::make_shared(12, 5); -void DistributedataDfxMSTTest::TearDownTestCase() {} +void DistributedataDfxMSTTest::SetUpTestCase() +{ + Reporter::GetInstance()->SetThreadPool(DistributedataDfxMSTTest::executors_); +} + +void DistributedataDfxMSTTest::TearDownTestCase() +{ +} void DistributedataDfxMSTTest::SetUp() {} diff --git a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp index f0ee9682..3b08cd5e 100644 --- a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp +++ b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp @@ -31,10 +31,14 @@ public: void SetUp(); void TearDown(); +protected: + static std::shared_ptr executors_; }; +std::shared_ptr DistributedataDfxUTTest::executors_ = std::make_shared(12, 5); void DistributedataDfxUTTest::SetUpTestCase() { + Reporter::GetInstance()->SetThreadPool(DistributedataDfxUTTest::executors_); FakeHivew::Clear(); } diff --git a/services/distributeddataservice/adapter/include/account/account_delegate.h b/services/distributeddataservice/adapter/include/account/account_delegate.h index d7fa49da..d19123bd 100644 --- a/services/distributeddataservice/adapter/include/account/account_delegate.h +++ b/services/distributeddataservice/adapter/include/account/account_delegate.h @@ -18,6 +18,7 @@ #include #include +#include "executor_pool.h" #include "types.h" #include "visibility.h" @@ -58,7 +59,7 @@ public: API_EXPORT virtual Status Unsubscribe(std::shared_ptr observer) = 0; API_EXPORT virtual std::string GetCurrentAccountId() const = 0; API_EXPORT virtual int32_t GetUserByToken(uint32_t tokenId) const = 0; - API_EXPORT virtual void SubscribeAccountEvent() = 0; + API_EXPORT virtual void SubscribeAccountEvent(std::shared_ptr executors) = 0; API_EXPORT virtual void UnsubscribeAccountEvent() = 0; API_EXPORT virtual bool QueryUsers(std::vector &users) = 0; API_EXPORT virtual bool RegisterHashFunc(HashFunc hash) = 0; diff --git a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h index d4a72a19..9cb05a88 100644 --- a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h +++ b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h @@ -25,7 +25,7 @@ #include "device_manager.h" #include "device_manager_callback.h" #include "dm_device_info.h" -#include "task_scheduler.h" +#include "executor_pool.h" #include "lru_bucket.h" namespace OHOS { @@ -33,14 +33,13 @@ namespace DistributedData { class API_EXPORT DeviceManagerAdapter { public: using DmDeviceInfo = OHOS::DistributedHardware::DmDeviceInfo; - using KvScheduler = OHOS::TaskScheduler; using DeviceInfo = OHOS::AppDistributedKv::DeviceInfo; using PipeInfo = OHOS::AppDistributedKv::PipeInfo; using AppDeviceChangeListener = OHOS::AppDistributedKv::AppDeviceChangeListener; using Status = OHOS::DistributedKv::Status; static DeviceManagerAdapter &GetInstance(); - void Init(); + void Init(std::shared_ptr executors); Status StartWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo); Status StopWatchDeviceChange(const AppDeviceChangeListener *observer, const PipeInfo &pipeInfo); DeviceInfo GetLocalDevice(); @@ -77,9 +76,9 @@ private: ConcurrentMap observers_ {}; LRUBucket deviceInfos_ {64}; static constexpr size_t TIME_TASK_CAPACITY = 50; - KvScheduler scheduler_ {TIME_TASK_CAPACITY, "dm_adapter"}; static constexpr int32_t SYNC_TIMEOUT = 10 * 1000; // ms ConcurrentMap syncTask_ {}; + std::shared_ptr executors_; }; } // namespace DistributedData } // namespace OHOS diff --git a/services/distributeddataservice/adapter/include/dfx/reporter.h b/services/distributeddataservice/adapter/include/dfx/reporter.h index 8adb5bd6..f84af98a 100644 --- a/services/distributeddataservice/adapter/include/dfx/reporter.h +++ b/services/distributeddataservice/adapter/include/dfx/reporter.h @@ -22,6 +22,7 @@ #include "statistic_reporter.h" #include "fault_reporter.h" #include "behaviour_reporter.h" +#include "executor_pool.h" namespace OHOS { namespace DistributedDataDfx { @@ -39,6 +40,13 @@ public: KVSTORE_API StatisticReporter* ApiPerformanceStatistic(); KVSTORE_API BehaviourReporter* BehaviourReporter(); + void SetThreadPool(std::shared_ptr executors) + { + executors_ = executors; + }; + +private: + std::shared_ptr executors_; }; } // namespace DistributedDataDfx } // namespace OHOS diff --git a/services/distributeddataservice/app/src/feature_stub_impl.cpp b/services/distributeddataservice/app/src/feature_stub_impl.cpp index 06cd7ee3..46ff92d8 100644 --- a/services/distributeddataservice/app/src/feature_stub_impl.cpp +++ b/services/distributeddataservice/app/src/feature_stub_impl.cpp @@ -32,11 +32,12 @@ int FeatureStubImpl::OnRemoteRequest(uint32_t code, MessageParcel &data, Message return featureImpl_->OnRemoteRequest(code, data, reply); } -int32_t FeatureStubImpl::OnInitialize() +int32_t FeatureStubImpl::OnInitialize(std::shared_ptr executors) { if (featureImpl_ == nullptr) { return -1; } + featureImpl_->OnExecutor(executors); return featureImpl_->OnInitialize(); } @@ -95,4 +96,14 @@ int32_t FeatureStubImpl::OnReady(const std::string &device) } return featureImpl_->OnReady(device); } -} \ No newline at end of file + +void FeatureStubImpl::SetThreadPool(TaskExecutorAdapter* threadPool) +{ + threadPool_ = threadPool; +} + +TaskExecutorAdapter* FeatureStubImpl::GetThreadPool() +{ + return threadPool_; +} +} diff --git a/services/distributeddataservice/app/src/feature_stub_impl.h b/services/distributeddataservice/app/src/feature_stub_impl.h index 4e4562df..e619d420 100644 --- a/services/distributeddataservice/app/src/feature_stub_impl.h +++ b/services/distributeddataservice/app/src/feature_stub_impl.h @@ -29,7 +29,7 @@ public: ~FeatureStubImpl(); int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; - int32_t OnInitialize(); + int32_t OnInitialize(std::shared_ptr executors); int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName); int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m); diff --git a/services/distributeddataservice/app/src/kvstore_account_observer.cpp b/services/distributeddataservice/app/src/kvstore_account_observer.cpp index f8258225..4c6f0be5 100644 --- a/services/distributeddataservice/app/src/kvstore_account_observer.cpp +++ b/services/distributeddataservice/app/src/kvstore_account_observer.cpp @@ -17,7 +17,6 @@ #include "kvstore_account_observer.h" #include -#include "executor_factory.h" #include "kvstore_data_service.h" #include "log_print.h" @@ -27,11 +26,11 @@ std::atomic g_kvStoreAccountEventStatus {0}; void KvStoreAccountObserver::OnAccountChanged(const AccountEventInfo &eventInfo) { ZLOGI("account event %d, begin.", eventInfo.status); - KvStoreTask task([this, eventInfo]() { + ExecutorPool::Task task([this, eventInfo]() { ZLOGI("account event processing in thread"); kvStoreDataService_.AccountEventChanged(eventInfo); }); - DistributedData::ExecutorFactory::GetInstance().Execute(std::move(task)); + executors_->Execute(std::move(task)); ZLOGI("account event %d, end.", eventInfo.status); } } // namespace DistributedKv diff --git a/services/distributeddataservice/app/src/kvstore_account_observer.h b/services/distributeddataservice/app/src/kvstore_account_observer.h index ddc4103d..a0598ec0 100644 --- a/services/distributeddataservice/app/src/kvstore_account_observer.h +++ b/services/distributeddataservice/app/src/kvstore_account_observer.h @@ -17,7 +17,9 @@ #define KVSTORE_ACCOUNT_OBSERVER_H #include + #include "account_delegate.h" +#include "executor_pool.h" namespace OHOS { namespace DistributedKv { @@ -32,8 +34,11 @@ do { \ class KvStoreDataService; class KvStoreAccountObserver : public AccountDelegate::Observer { public: - explicit KvStoreAccountObserver(KvStoreDataService &kvStoreDataService) - : kvStoreDataService_(kvStoreDataService) {} + explicit KvStoreAccountObserver(KvStoreDataService &kvStoreDataService, + std::shared_ptr executors) + : kvStoreDataService_(kvStoreDataService), executors_(executors) + { + } ~KvStoreAccountObserver() override = default; void OnAccountChanged(const AccountEventInfo &eventInfo) override; @@ -50,6 +55,7 @@ public: private: KvStoreDataService &kvStoreDataService_; + std::shared_ptr executors_; }; } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 50bef791..a31bdf50 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -30,7 +30,6 @@ #include "device_manager_adapter.h" #include "device_matrix.h" #include "eventcenter/event_center.h" -#include "executor_factory.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" #include "kvstore_account_observer.h" @@ -41,16 +40,18 @@ #include "permission_validator.h" #include "permit_delegate.h" #include "process_communicator_impl.h" +#include "reporter.h" #include "route_head_handler_impl.h" #include "runtime_config.h" #include "string_ex.h" #include "system_ability_definition.h" +#include "task_manager.h" #include "uninstaller/uninstaller.h" +#include "upgrade.h" #include "upgrade_manager.h" #include "user_delegate.h" #include "utils/block_integer.h" #include "utils/crypto.h" -#include "upgrade.h" namespace OHOS::DistributedKv { using namespace std::chrono; @@ -90,13 +91,14 @@ void KvStoreDataService::Initialize() #endif auto communicator = std::make_shared(RouteHeadHandlerImpl::Create); auto ret = KvStoreDelegateManager::SetProcessCommunicator(communicator); + DistributedDB::RuntimeConfig::SetThreadPool(std::make_shared(executors_)); ZLOGI("set communicator ret:%{public}d.", static_cast(ret)); AppDistributedKv::CommunicationProvider::GetInstance(); PermitDelegate::GetInstance().Init(); - InitSecurityAdapter(); - KvStoreMetaManager::GetInstance().InitMetaParameter(); - accountEventObserver_ = std::make_shared(*this); + InitSecurityAdapter(executors_); + KvStoreMetaManager::GetInstance().InitMetaParameter(executors_); + accountEventObserver_ = std::make_shared(*this, executors_); AccountDelegate::GetInstance()->Subscribe(accountEventObserver_); deviceInnerListener_ = std::make_unique(*this); DmAdapter::GetInstance().StartWatchDeviceChange(deviceInnerListener_.get(), { "innerListener" }); @@ -138,7 +140,7 @@ sptr KvStoreDataService::GetFeatureInterface(const std::string &n return true; }); if (isFirstCreate) { - feature->OnInitialize(); + feature->OnInitialize(executors_); } return feature != nullptr ? feature->AsObject() : nullptr; } @@ -227,8 +229,10 @@ void KvStoreDataService::OnStart() { ZLOGI("distributeddata service onStart"); EventCenter::Defer defer; + executors_ = std::make_shared(12, 5); + Reporter::GetInstance()->SetThreadPool(executors_); AccountDelegate::GetInstance()->RegisterHashFunc(Crypto::Sha256); - DmAdapter::GetInstance().Init(); + DmAdapter::GetInstance().Init(executors_); static constexpr int32_t RETRY_TIMES = 50; static constexpr int32_t RETRY_INTERVAL = 500 * 1000; // unit is ms for (BlockInteger retry(RETRY_INTERVAL); retry < RETRY_TIMES; ++retry) { @@ -242,7 +246,7 @@ void KvStoreDataService::OnStart() Bootstrap::GetInstance().LoadDirectory(); Bootstrap::GetInstance().LoadCheckers(); Bootstrap::GetInstance().LoadNetworks(); - Bootstrap::GetInstance().LoadBackup(); + Bootstrap::GetInstance().LoadBackup(executors_); Initialize(); auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgr != nullptr) { @@ -265,8 +269,8 @@ void KvStoreDataService::OnAddSystemAbility(int32_t systemAbilityId, const std:: if (systemAbilityId != COMMON_EVENT_SERVICE_ID) { return; } - AccountDelegate::GetInstance()->SubscribeAccountEvent(); - Uninstaller::GetInstance().Init(this); + AccountDelegate::GetInstance()->SubscribeAccountEvent(executors_); + Uninstaller::GetInstance().Init(this, executors_); } void KvStoreDataService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) @@ -291,17 +295,17 @@ void KvStoreDataService::StartService() if (!ret) { DumpHelper::GetInstance().AddErrorInfo("StartService: Service publish failed."); } - Uninstaller::GetInstance().Init(this); + Uninstaller::GetInstance().Init(this, executors_); // Initialize meta db delegate manager. KvStoreMetaManager::GetInstance().SubscribeMeta(KvStoreMetaRow::KEY_PREFIX, [this](const std::vector &key, const std::vector &value, CHANGE_FLAG flag) { OnStoreMetaChanged(key, value, flag); }); - UpgradeManager::GetInstance().Init(); - UserDelegate::GetInstance().Init(); + UpgradeManager::GetInstance().Init(executors_); + UserDelegate::GetInstance().Init(executors_); // subscribe account event listener to EventNotificationMgr - AccountDelegate::GetInstance()->SubscribeAccountEvent(); + AccountDelegate::GetInstance()->SubscribeAccountEvent(executors_); auto autoLaunch = [this](const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) -> bool { auto status = ResolveAutoLaunchParamByIdentifier(identifier, param); features_.ForEachCopies([&identifier, ¶m](const auto &, sptr &value) { @@ -450,14 +454,14 @@ void KvStoreDataService::ResolveAutoLaunchCompatible(const StoreMetaData &storeM store = delegate; } }); - KvStoreTask delayTask([store]() { + ExecutorPool::Task delayTask([store]() { constexpr const int CLOSE_STORE_DELAY_TIME = 60; // unit: seconds std::this_thread::sleep_for(std::chrono::seconds(CLOSE_STORE_DELAY_TIME)); ZLOGI("AutoLaunch:close store after 60s while autolaunch finishied"); DistributedDB::KvStoreDelegateManager delegateManager("", ""); delegateManager.CloseKvStore(store); }); - ExecutorFactory::GetInstance().Execute(std::move(delayTask)); + executors_->Execute(std::move(delayTask)); } Status KvStoreDataService::InitNbDbOption(const Options &options, const std::vector &cipherKey, @@ -609,11 +613,11 @@ void KvStoreDataService::NotifyAccountEvent(const AccountEventInfo &eventInfo) } } -void KvStoreDataService::InitSecurityAdapter() +void KvStoreDataService::InitSecurityAdapter(std::shared_ptr executors) { auto ret = DATASL_OnStart(); ZLOGI("datasl on start ret:%d", ret); - security_ = std::make_shared(); + security_ = std::make_shared(executors); if (security_ == nullptr) { ZLOGE("security is nullptr."); return; diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index 990f4500..894fa13b 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -22,16 +22,19 @@ #include "account_delegate.h" #include "constant.h" +#include "dump_helper.h" +#include "feature_stub_impl.h" #include "ikvstore_data_service.h" +#include "ithread_pool.h" #include "kvstore_device_listener.h" #include "kvstore_meta_manager.h" #include "metadata/store_meta_data.h" #include "reporter.h" +#include "runtime_config.h" #include "security/security.h" #include "system_ability.h" +#include "executor_pool.h" #include "types.h" -#include "dump_helper.h" -#include "feature_stub_impl.h" namespace OHOS::DistributedKv { class KvStoreAccountObserver; @@ -109,14 +112,14 @@ private: void StartService(); - void InitSecurityAdapter(); + void InitSecurityAdapter(std::shared_ptr executors); void OnStoreMetaChanged(const std::vector &key, const std::vector &value, CHANGE_FLAG flag); Status AppExit(pid_t uid, pid_t pid, uint32_t token, const AppId &appId); bool ResolveAutoLaunchParamByIdentifier(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m); - static void ResolveAutoLaunchCompatible(const StoreMetaData &meta, const std::string &identifier); + void ResolveAutoLaunchCompatible(const StoreMetaData &meta, const std::string &identifier); static DistributedDB::SecurityOption ConvertSecurity(int securityLevel); static Status InitNbDbOption(const Options &options, const std::vector &cipherKey, DistributedDB::KvStoreNbDelegate::Option &dbOption); @@ -130,6 +133,7 @@ private: std::shared_ptr security_; ConcurrentMap> features_; std::shared_ptr deviceInnerListener_; + std::shared_ptr executors_; }; class DbMetaCallbackDelegateMgr : public DbMetaCallbackDelegate { diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index 91c77d7c..f3ddf7c5 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -169,10 +169,10 @@ void KvStoreMetaManager::InitMetaData() ZLOGI("end."); } -void KvStoreMetaManager::InitMetaParameter() +void KvStoreMetaManager::InitMetaParameter(std::shared_ptr executors) { ZLOGI("start."); - std::thread th = std::thread([]() { + executors->Execute([]() { constexpr int32_t RETRY_MAX_TIMES = 100; constexpr int32_t RETRY_INTERVAL = 1 * 1000 * 1000; // retry after 1 second BlockInteger retry(RETRY_INTERVAL); @@ -191,7 +191,6 @@ void KvStoreMetaManager::InitMetaParameter() ZLOGW("GenerateRootKey failed, retry times:%{public}d.", static_cast(retry)); } }); - th.detach(); DistributedDB::KvStoreConfig kvStoreConfig{ metaDBDirectory_ }; delegateManager_.SetKvStoreConfig(kvStoreConfig); } diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.h b/services/distributeddataservice/app/src/kvstore_meta_manager.h index 7cea63df..1702e466 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.h +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.h @@ -18,9 +18,9 @@ #include #include "app_device_change_listener.h" +#include "executor_pool.h" #include "kv_store_delegate.h" #include "kv_store_delegate_manager.h" -#include "kv_store_task.h" #include "system_ability.h" #include "types.h" @@ -48,7 +48,7 @@ public: static KvStoreMetaManager &GetInstance(); - void InitMetaParameter(); + void InitMetaParameter(std::shared_ptr executors); void InitMetaListener(); void InitBroadcast(); void InitDeviceOnline(); diff --git a/services/distributeddataservice/app/src/security/security.cpp b/services/distributeddataservice/app/src/security/security.cpp index 44845416..0ff95061 100644 --- a/services/distributeddataservice/app/src/security/security.cpp +++ b/services/distributeddataservice/app/src/security/security.cpp @@ -161,7 +161,7 @@ Sensitive Security::GetSensitiveByUuid(const std::string &uuid) const { auto it = devicesUdid_.Find(uuid); if (!it.first) { - taskScheduler_.Execute([this, uuid]() { + executors_->Execute([this, uuid]() { auto it = devicesUdid_.Find(uuid); if (it.first) { return; diff --git a/services/distributeddataservice/app/src/security/security.h b/services/distributeddataservice/app/src/security/security.h index debf2d60..c2a8f4c0 100644 --- a/services/distributeddataservice/app/src/security/security.h +++ b/services/distributeddataservice/app/src/security/security.h @@ -17,8 +17,9 @@ #define OHOS_SECURITY_H #include #include -#include + #include "app_device_change_listener.h" +#include "executor_pool.h" #include "iprocess_system_api_adapter.h" #include "kv_store_delegate_manager.h" #include "sensitive.h" @@ -33,6 +34,7 @@ public: using OnAccessControlledEvent = DistributedDB::OnAccessControlledEvent; using SecurityOption = DistributedDB::SecurityOption; Security(); + Security(std::shared_ptr executors) : executors_(executors){}; ~Security() override; static bool IsSupportSecurity(); @@ -76,8 +78,8 @@ private: DBStatus GetFileSecurityOption(const std::string &filePath, SecurityOption &option) const; DBStatus GetDirSecurityOption(const std::string &filePath, SecurityOption &option) const; - mutable TaskScheduler taskScheduler_ { "security" }; mutable ConcurrentMap devicesUdid_; + std::shared_ptr executors_; }; } // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp index c59353c4..803adeea 100644 --- a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp +++ b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp @@ -19,7 +19,6 @@ #include #include "account_delegate.h" #include "device_manager_adapter.h" -#include "executor_factory.h" #include "log_print.h" #include "metadata/meta_data_manager.h" #include "utils/anonymous.h" @@ -34,9 +33,9 @@ UpgradeManager &UpgradeManager::GetInstance() return instance; } -void UpgradeManager::Init() +void UpgradeManager::Init(std::shared_ptr executors) { - OHOS::DistributedKv::KvStoreTask retryTask([this]() { + executors->Execute([this]() { do { if (InitLocalCapability()) { break; @@ -45,15 +44,13 @@ void UpgradeManager::Init() std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_INTERVAL)); } while (true); }); - ExecutorFactory::GetInstance().Execute(std::move(retryTask)); } CapMetaData UpgradeManager::GetCapability(const std::string &deviceId, bool &status) { status = true; - auto cap = capabilities_.Find(deviceId); - if (cap.first) { - return cap.second; + if (capabilityMap_.Contains(deviceId)) { + return capabilityMap_.Find(deviceId).second; } ZLOGI("load capability from meta"); CapMetaData capMetaData; diff --git a/services/distributeddataservice/app/src/session_manager/upgrade_manager.h b/services/distributeddataservice/app/src/session_manager/upgrade_manager.h index 79cc6dba..ab799f62 100644 --- a/services/distributeddataservice/app/src/session_manager/upgrade_manager.h +++ b/services/distributeddataservice/app/src/session_manager/upgrade_manager.h @@ -29,7 +29,7 @@ using OHOS::DistributedKv::KvStoreTuple; class UpgradeManager { public: static UpgradeManager &GetInstance(); - void Init(); + void Init(std::shared_ptr executors); CapMetaData GetCapability(const std::string &deviceId, bool &status); static void SetCompatibleIdentifyByType( KvStoreNbDelegate *storeDelegate, const KvStoreTuple &tuple, AUTH_GROUP_TYPE groupType); diff --git a/services/distributeddataservice/app/src/task_manager.cpp b/services/distributeddataservice/app/src/task_manager.cpp new file mode 100644 index 00000000..bf6d7432 --- /dev/null +++ b/services/distributeddataservice/app/src/task_manager.cpp @@ -0,0 +1,55 @@ +/* +* 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. +*/ + +#include "task_manager.h" +namespace OHOS::DistributedData { +TaskManager::TaskManager(std::shared_ptr executors) +{ + executors_ = executors; +} + +TaskManager::~TaskManager() +{ + executors_ = nullptr; +} +TaskManager::TaskId TaskManager::Execute(const Task &task) +{ + return executors_->Execute(task); +} +TaskManager::TaskId TaskManager::Execute(const Task &task, Duration delay) +{ + return executors_->Execute(task, delay); +} +TaskManager::TaskId TaskManager::Schedule(const Task &task, Duration interval) +{ + return executors_->Schedule(task, interval); +} +TaskManager::TaskId TaskManager::Schedule(const Task &task, Duration delay, Duration interval) +{ + return executors_->Schedule(task, delay, interval); +} +TaskManager::TaskId TaskManager::Schedule(const Task &task, Duration delay, Duration interval, uint64_t times) +{ + return executors_->Schedule(task, delay, interval, times); +} +bool TaskManager::Remove(const TaskId &taskId, bool wait) +{ + return executors_->Remove(taskId, wait); +} +TaskManager::TaskId TaskManager::Reset(const TaskId &taskId, Duration interval) +{ + return executors_->Reset(taskId, interval); +} +} // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/app/src/task_manager.h b/services/distributeddataservice/app/src/task_manager.h new file mode 100644 index 00000000..a2d9d76a --- /dev/null +++ b/services/distributeddataservice/app/src/task_manager.h @@ -0,0 +1,40 @@ +/* +* 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 DISTRIBUTED_DATA_TASK_EXECUTOR_H +#define DISTRIBUTED_DATA_TASK_EXECUTOR_H +#include "ithread_pool.h" +#include "executor_pool.h" +namespace OHOS::DistributedData { +class TaskManager : public DistributedDB::IThreadPool { +public: + using TaskId = uint64_t; + using Task = std::function; + using Duration = std::chrono::steady_clock::duration; + TaskManager() = default; + TaskManager(std::shared_ptr executors); + ~TaskManager() override; + TaskId Execute(const Task &task) override; + TaskId Execute(const Task &task, Duration delay) override; + TaskId Schedule(const Task &task, Duration interval) override; + TaskId Schedule(const Task &task, Duration delay, Duration interval) override; + TaskId Schedule(const Task &task, Duration delay, Duration interval, uint64_t times) override; + bool Remove(const TaskId &taskId, bool wait) override; + TaskId Reset(const TaskId &taskId, Duration interval) override; + +private: + std::shared_ptr executors_ = nullptr; +}; +} // namespace OHOS::DistributedData +#endif // DISTRIBUTED_DATA_TASK_EXECUTOR_H diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller.h b/services/distributeddataservice/app/src/uninstaller/uninstaller.h index 244c5139..60478307 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller.h @@ -20,13 +20,14 @@ #include #include "visibility.h" - +#include "executor_pool.h" namespace OHOS::DistributedKv { class KvStoreDataService; enum Status : int32_t; class Uninstaller { public: - KVSTORE_API virtual Status Init(KvStoreDataService *kvStoreDataService) = 0; + KVSTORE_API virtual Status Init(KvStoreDataService *kvStoreDataService, + std::shared_ptr executors) = 0; KVSTORE_API virtual void UnsubscribeEvent() = 0; KVSTORE_API virtual ~Uninstaller() {} KVSTORE_API static Uninstaller &GetInstance(); diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index 36f24caf..afb290f2 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -75,7 +75,7 @@ void UninstallerImpl::UnsubscribeEvent() } } -Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService) +Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared_ptr executors) { if (kvStoreDataService == nullptr) { ZLOGW("kvStoreDataService is null."); @@ -108,7 +108,7 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService) }; auto subscriber = std::make_shared(info, callback); subscriber_ = subscriber; - std::thread th = std::thread([subscriber] { + executors->Execute([subscriber] { constexpr int32_t RETRY_TIME = 300; constexpr int32_t RETRY_INTERVAL = 100 * 1000; for (BlockInteger retry(RETRY_INTERVAL); retry < RETRY_TIME; ++retry) { @@ -119,7 +119,6 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService) ZLOGE("subscribe uninstall event fail, try times:%d", static_cast(retry)); } }); - th.detach(); return Status::SUCCESS; } } // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h index 00dc1f08..7e5c20bd 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h @@ -40,7 +40,7 @@ class UninstallerImpl : public Uninstaller { public: ~UninstallerImpl(); - Status Init(KvStoreDataService *kvStoreDataService) override; + Status Init(KvStoreDataService *kvStoreDataService, std::shared_ptr executors) override; void UnsubscribeEvent() override; diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 628ff741..70fb69c5 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -75,6 +75,7 @@ ohos_unittest("KvStoreDataServiceTest") { "../src/session_manager/route_head_handler_impl.cpp", "../src/session_manager/session_manager.cpp", "../src/session_manager/upgrade_manager.cpp", + "../src/task_manager.cpp", "unittest/kvstore_data_service_test.cpp", ] diff --git a/services/distributeddataservice/app/test/unittest/session_manager_test.cpp b/services/distributeddataservice/app/test/unittest/session_manager_test.cpp index 4d6d7bd7..e834cfe2 100644 --- a/services/distributeddataservice/app/test/unittest/session_manager_test.cpp +++ b/services/distributeddataservice/app/test/unittest/session_manager_test.cpp @@ -37,12 +37,13 @@ class SessionManagerTest : public testing::Test { public: static void SetUpTestCase() { + auto executors = std::make_shared(12, 5); Bootstrap::GetInstance().LoadComponents(); Bootstrap::GetInstance().LoadDirectory(); Bootstrap::GetInstance().LoadCheckers(); - KvStoreMetaManager::GetInstance().InitMetaParameter(); + KvStoreMetaManager::GetInstance().InitMetaParameter(executors); KvStoreMetaManager::GetInstance().InitMetaListener(); - DeviceManagerAdapter::GetInstance().Init(); + DeviceManagerAdapter::GetInstance().Init(executors); // init peer device UserMetaData userMetaData; diff --git a/services/distributeddataservice/framework/feature/feature_system.cpp b/services/distributeddataservice/framework/feature/feature_system.cpp index 6013e9cf..83df930d 100644 --- a/services/distributeddataservice/framework/feature/feature_system.cpp +++ b/services/distributeddataservice/framework/feature/feature_system.cpp @@ -81,5 +81,10 @@ int32_t FeatureSystem::Feature::OnReady(const std::string &device) { return STUB_SUCCESS; } + +int32_t FeatureSystem::Feature::OnExecutor(std::shared_ptr executors) +{ + return STUB_SUCCESS; +} +} } -} \ No newline at end of file diff --git a/services/distributeddataservice/framework/include/feature/feature_system.h b/services/distributeddataservice/framework/include/feature/feature_system.h index 7b4ad3f7..a20e23cd 100644 --- a/services/distributeddataservice/framework/include/feature/feature_system.h +++ b/services/distributeddataservice/framework/include/feature/feature_system.h @@ -16,6 +16,7 @@ #define OHOS_DISTRIBUTED_DATA_FRAMEWORK_SYSTEM_SYSTEM_H #include #include "concurrent_map.h" +#include "executor_pool.h" #include "visibility.h" namespace DistributedDB { struct AutoLaunchParam; @@ -31,6 +32,7 @@ public: virtual ~Feature(); virtual int OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply) = 0; virtual int32_t OnInitialize(); + virtual int32_t OnExecutor(std::shared_ptr executors); virtual int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName); virtual int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); virtual int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m); diff --git a/services/distributeddataservice/service/backup/include/backup_manager.h b/services/distributeddataservice/service/backup/include/backup_manager.h index 43060215..7233afea 100644 --- a/services/distributeddataservice/service/backup/include/backup_manager.h +++ b/services/distributeddataservice/service/backup/include/backup_manager.h @@ -16,9 +16,9 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_BACKUP_BACKUP_MANAGER_H #define OHOS_DISTRIBUTED_DATA_SERVICES_BACKUP_BACKUP_MANAGER_H -#include "task_scheduler.h" -#include "metadata/store_meta_data.h" +#include "executor_pool.h" #include "metadata/secret_key_meta_data.h" +#include "metadata/store_meta_data.h" #include "types.h" namespace OHOS::DistributedData { class BackupManager { @@ -37,7 +37,7 @@ public: }; static BackupManager &GetInstance(); void Init(); - void BackSchedule(); + void BackSchedule(std::shared_ptr executors); void SetBackupParam(const BackupParam &backupParam); void RegisterExporter(int32_t type, Exporter exporter); bool GetPassWord(const StoreMetaData &meta, std::vector &password); @@ -64,7 +64,6 @@ private: int64_t backupSuccessTime_ = 0; int64_t backupNumber_ = 0; int64_t startNum_ = 0; - TaskScheduler scheduler_ { "backup" }; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_BACKUP_BACKUP_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/backup/src/backup_manager.cpp b/services/distributeddataservice/service/backup/src/backup_manager.cpp index 66164fff..7bde171c 100644 --- a/services/distributeddataservice/service/backup/src/backup_manager.cpp +++ b/services/distributeddataservice/service/backup/src/backup_manager.cpp @@ -90,35 +90,37 @@ void BackupManager::RegisterExporter(int32_t type, Exporter exporter) } } -void BackupManager::BackSchedule() +void BackupManager::BackSchedule(std::shared_ptr executors) { std::chrono::duration delay(schedularDelay_); std::chrono::duration internal(schedularInternal_); ZLOGI("BackupManager Schedule start."); - scheduler_.Every(delay, internal, [this]() { - if (!CanBackup()) { - return; - } + executors->Schedule( + [this]() { + if (!CanBackup()) { + return; + } - ZLOGI("start automatic backup."); - std::vector metas; - MetaDataManager::GetInstance().LoadMeta( - StoreMetaData::GetPrefix({DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid}), metas); + ZLOGI("start automatic backup."); + std::vector metas; + MetaDataManager::GetInstance().LoadMeta( + StoreMetaData::GetPrefix({ DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid }), metas); - int64_t end = std::min(startNum_ + backupNumber_, static_cast(metas.size())); - for (int64_t i = startNum_; i < end; startNum_++, i++) { - auto &meta = metas[i]; - if (!meta.isBackup || meta.isDirty) { - continue; + int64_t end = std::min(startNum_ + backupNumber_, static_cast(metas.size())); + for (int64_t i = startNum_; i < end; startNum_++, i++) { + auto &meta = metas[i]; + if (!meta.isBackup || meta.isDirty) { + continue; + } + DoBackup(meta); } - DoBackup(meta); - } - if (startNum_ >= static_cast(metas.size())) { - startNum_ = 0; - } - sync(); - backupSuccessTime_ = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); - }); + if (startNum_ >= static_cast(metas.size())) { + startNum_ = 0; + } + sync(); + backupSuccessTime_ = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + }, + internal, delay); } void BackupManager::DoBackup(const StoreMetaData &meta) diff --git a/services/distributeddataservice/service/bootstrap/include/bootstrap.h b/services/distributeddataservice/service/bootstrap/include/bootstrap.h index a8698438..c2301236 100644 --- a/services/distributeddataservice/service/bootstrap/include/bootstrap.h +++ b/services/distributeddataservice/service/bootstrap/include/bootstrap.h @@ -16,6 +16,7 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_BOOTSTRAP_BOOTSTRAP_H #define OHOS_DISTRIBUTED_DATA_SERVICES_BOOTSTRAP_BOOTSTRAP_H #include +#include "executor_pool.h" #include "visibility.h" namespace OHOS { namespace DistributedData { @@ -28,7 +29,7 @@ public: API_EXPORT void LoadCheckers(); API_EXPORT void LoadNetworks(); API_EXPORT void LoadDirectory(); - API_EXPORT void LoadBackup(); + API_EXPORT void LoadBackup(std::shared_ptr executors); private: static constexpr const char *DEFAULT_LABEL = "distributeddata"; static constexpr const char *DEFAULT_META = "service_meta"; diff --git a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp index 741f18b9..299c70ba 100644 --- a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp +++ b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp @@ -94,7 +94,7 @@ void Bootstrap::LoadCheckers() } } -void Bootstrap::LoadBackup() +void Bootstrap::LoadBackup(std::shared_ptr executors) { auto *backupRules = ConfigFactory::GetInstance().GetBackupConfig(); if (backupRules == nullptr) { @@ -106,7 +106,7 @@ void Bootstrap::LoadBackup() backupRules->schedularInternal, backupRules->backupInternal, backupRules->backupNumber}; BackupManager::GetInstance().SetBackupParam(backupParam); BackupManager::GetInstance().Init(); - BackupManager::GetInstance().BackSchedule(); + BackupManager::GetInstance().BackSchedule(executors); } void Bootstrap::LoadNetworks() diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index a19acff4..086b2807 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -788,4 +788,12 @@ size_t KVDBServiceImpl::GetSyncDataSize(const std::string &deviceId) return totalSize; } +int32_t KVDBServiceImpl::OnExecutor(std::shared_ptr executors) +{ + executors_ = executors; + storeCache_.SetThreadPool(executors); + KvStoreSyncManager::GetInstance()->SetThreadPool(executors); + ZLOGE("onexecutor:%{public}p", executors.get()); + return 0; +} } // namespace OHOS::DistributedKv \ No newline at end of file diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h index 3cee4848..b1c3e5fd 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.h +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.h @@ -53,7 +53,7 @@ public: Status Subscribe(const AppId &appId, const StoreId &storeId, sptr observer) override; Status Unsubscribe(const AppId &appId, const StoreId &storeId, sptr observer) override; Status GetBackupPassword(const AppId &appId, const StoreId &storeId, std::vector &password) override; - + int32_t OnExecutor(std::shared_ptr executors) override; int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &appId) override; 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; @@ -105,6 +105,7 @@ private: static Factory factory_; ConcurrentMap syncAgents_; StoreCache storeCache_; + std::shared_ptr executors_; }; } // namespace OHOS::DistributedKv #endif // OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_SERVICE_IMPL_H diff --git a/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp index d2610983..43c99003 100644 --- a/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp +++ b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp @@ -18,9 +18,7 @@ namespace OHOS { namespace DistributedKv { -KvStoreSyncManager::KvStoreSyncManager() : syncScheduler_("SyncMgr") -{} - +KvStoreSyncManager::KvStoreSyncManager() {} KvStoreSyncManager::~KvStoreSyncManager() {} Status KvStoreSyncManager::AddSyncOperation(uintptr_t syncId, uint32_t delayMs, const SyncFunc &syncFunc, @@ -112,7 +110,11 @@ void KvStoreSyncManager::AddTimer(const TimePoint &expireTime) { ZLOGD("time %lld", expireTime.time_since_epoch().count()); nextScheduleTime_ = expireTime; - syncScheduler_.At(expireTime, [time = expireTime, this]() { Schedule(time); }); + executors_->Execute( + [time = expireTime, this]() { + Schedule(time); + }, + expireTime - std::chrono::steady_clock::now()); } bool KvStoreSyncManager::GetTimeoutSyncOps(const TimePoint ¤tTime, std::list &syncOps) @@ -175,5 +177,9 @@ void KvStoreSyncManager::Schedule(const TimePoint &time) AddTimer(nextTime); } } -} // namespace DistributedKv -} // namespace OHOS +void KvStoreSyncManager::SetThreadPool(std::shared_ptr executors) +{ + executors_ = executors; +} +} // namespace DistributedKv +} // namespace OHOS diff --git a/services/distributeddataservice/service/kvdb/kvstore_sync_manager.h b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.h index 1376358c..bad171c5 100644 --- a/services/distributeddataservice/service/kvdb/kvstore_sync_manager.h +++ b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.h @@ -20,7 +20,7 @@ #include #include -#include "task_scheduler.h" +#include "executor_pool.h" #include "kv_store_nb_delegate.h" #include "types.h" @@ -50,6 +50,7 @@ public: TimePoint beginTime; }; using OpPred = std::function; + void SetThreadPool(std::shared_ptr executors); Status AddSyncOperation(uintptr_t syncId, uint32_t delayMs, const SyncFunc &syncFunc, const SyncEnd &syncEnd); Status RemoveSyncOperation(uintptr_t syncId); @@ -73,8 +74,8 @@ private: std::list realtimeSyncingOps_; std::list delaySyncingOps_; std::multimap scheduleSyncOps_; + std::shared_ptr executors_; - TaskScheduler syncScheduler_ { "sync_mgr" }; TimePoint nextScheduleTime_; std::atomic_uint32_t syncOpSeq_ = 0; }; diff --git a/services/distributeddataservice/service/kvdb/store_cache.cpp b/services/distributeddataservice/service/kvdb/store_cache.cpp index 80574e51..2c7151ca 100644 --- a/services/distributeddataservice/service/kvdb/store_cache.cpp +++ b/services/distributeddataservice/service/kvdb/store_cache.cpp @@ -25,7 +25,6 @@ namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; constexpr int64_t StoreCache::INTERVAL; -constexpr size_t StoreCache::TIME_TASK_NUM; StoreCache::Store StoreCache::GetStore(const StoreMetaData &data, std::shared_ptr observers, DBStatus &status) { @@ -65,9 +64,7 @@ StoreCache::Store StoreCache::GetStore(const StoreMetaData &data, std::shared_pt return !stores.empty(); }); - scheduler_.At(std::chrono::steady_clock::now() + std::chrono::minutes(INTERVAL), - std::bind(&StoreCache::GarbageCollect, this)); - + executors_->Execute(std::bind(&StoreCache::GarbageCollect, this), std::chrono::minutes(INTERVAL)); return store; } @@ -134,7 +131,8 @@ void StoreCache::GarbageCollect() }); if (!stores_.Empty()) { ZLOGD("stores size:%{public}zu", stores_.Size()); - scheduler_.At(current + std::chrono::minutes(INTERVAL), std::bind(&StoreCache::GarbageCollect, this)); + executors_->Execute(std::bind(&StoreCache::GarbageCollect, this), + std::chrono::minutes(INTERVAL)); } } @@ -194,6 +192,11 @@ StoreCache::DBPassword StoreCache::GetDBPassword(const StoreMetaData &data) return dbPassword; } +void StoreCache::SetThreadPool(std::shared_ptr executors) +{ + executors_ = executors; +} + StoreCache::DBStoreDelegate::DBStoreDelegate(DBStore *delegate, std::shared_ptr observers) : delegate_(delegate) { diff --git a/services/distributeddataservice/service/kvdb/store_cache.h b/services/distributeddataservice/service/kvdb/store_cache.h index 10a4c872..2e4d726c 100644 --- a/services/distributeddataservice/service/kvdb/store_cache.h +++ b/services/distributeddataservice/service/kvdb/store_cache.h @@ -15,16 +15,17 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_STORE_CACHE_H #define OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_STORE_CACHE_H -#include #include +#include #include #include + #include "concurrent_map.h" -#include "task_scheduler.h" +#include "executor_pool.h" +#include "ikvstore_observer.h" #include "kv_store_nb_delegate.h" #include "metadata/store_meta_data.h" #include "refbase.h" -#include "ikvstore_observer.h" namespace OHOS::DistributedKv { class StoreCache { @@ -75,12 +76,12 @@ public: 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; - static constexpr size_t TIME_TASK_NUM = 1; ConcurrentMap> stores_; - TaskScheduler scheduler_{ TIME_TASK_NUM, "store_cache" }; + std::shared_ptr executors_; }; } // namespace OHOS::DistributedKv #endif // OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_STORE_CACHE_H diff --git a/services/distributeddataservice/service/kvdb/user_delegate.cpp b/services/distributeddataservice/service/kvdb/user_delegate.cpp index ee66bb38..b913ae9b 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.cpp +++ b/services/distributeddataservice/service/kvdb/user_delegate.cpp @@ -18,7 +18,6 @@ #include #include "communicator/device_manager_adapter.h" -#include "executor_factory.h" #include "log_print.h" #include "metadata/meta_data_manager.h" #include "utils/anonymous.h" @@ -149,9 +148,9 @@ UserDelegate &UserDelegate::GetInstance() return instance; } -void UserDelegate::Init() +void UserDelegate::Init(std::shared_ptr executors) { - KvStoreTask retryTask([this]() { + ExecutorPool::Task retryTask([this]() { do { static constexpr int RETRY_INTERVAL = 500; // millisecond std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_INTERVAL)); @@ -183,7 +182,7 @@ void UserDelegate::Init() return true; }); if (!InitLocalUserMeta()) { - ExecutorFactory::GetInstance().Execute(std::move(retryTask)); + executors->Execute(std::move(retryTask)); } ZLOGD("subscribe os account ret:%{public}d", ret); } diff --git a/services/distributeddataservice/service/kvdb/user_delegate.h b/services/distributeddataservice/service/kvdb/user_delegate.h index 56dec6c1..21825a1f 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.h +++ b/services/distributeddataservice/service/kvdb/user_delegate.h @@ -20,6 +20,7 @@ #include #include "account/account_delegate.h" #include "concurrent_map.h" +#include "executor_pool.h" #include "metadata/user_meta_data.h" #include "visibility.h" @@ -34,7 +35,7 @@ public: }; API_EXPORT static UserDelegate &GetInstance(); - API_EXPORT void Init(); + API_EXPORT void Init(std::shared_ptr executors); API_EXPORT std::vector GetLocalUserStatus(); API_EXPORT std::set GetLocalUsers(); API_EXPORT std::vector GetRemoteUserStatus(const std::string &deviceId); diff --git a/services/distributeddataservice/service/object/object_manager.cpp b/services/distributeddataservice/service/object/object_manager.cpp index f80a9290..4abd5e9f 100644 --- a/services/distributeddataservice/service/object/object_manager.cpp +++ b/services/distributeddataservice/service/object/object_manager.cpp @@ -27,10 +27,7 @@ namespace OHOS { namespace DistributedObject { using namespace OHOS::DistributedKv; -ObjectStoreManager::ObjectStoreManager() : timer_("CloseRetryTimer") -{ - timer_.Setup(); -} +ObjectStoreManager::ObjectStoreManager() {} DistributedDB::KvStoreNbDelegate *ObjectStoreManager::OpenObjectKvStore() { @@ -353,7 +350,12 @@ void ObjectStoreManager::FlushClosedStore() ZLOGD("close store"); auto status = kvStoreDelegateManager_->CloseKvStore(delegate_); if (status != DistributedDB::DBStatus::OK) { - timer_.Register([this]() { FlushClosedStore(); }, 1000, true); // retry after 1000ms + int timeOut = 1000; + executors_->Execute( + [this]() { + FlushClosedStore(); + }, + std::chrono::milliseconds(timeOut)); ZLOGE("GetEntries fail %{public}d", status); return; } @@ -567,8 +569,7 @@ int64_t ObjectStoreManager::GetTime(const std::string &key) void ObjectStoreManager::CloseAfterMinute() { - scheduler_.At(std::chrono::steady_clock::now() + std::chrono::minutes(INTERVAL), - std::bind(&ObjectStoreManager::Close, this)); + executors_->Execute(std::bind(&ObjectStoreManager::Close, this), std::chrono::minutes(INTERVAL)); } std::string ObjectStoreManager::GetBundleName(const std::string &key) @@ -582,6 +583,10 @@ std::string ObjectStoreManager::GetBundleName(const std::string &key) return result; } +void ObjectStoreManager::SetThreadPool(std::shared_ptr executors) { + executors_ = executors; +} + uint64_t SequenceSyncManager::AddNotifier(const std::string &userId, SyncCallBack &callback) { std::lock_guard lock(notifierLock_); diff --git a/services/distributeddataservice/service/object/object_manager.h b/services/distributeddataservice/service/object/object_manager.h index da5117b7..d3d924de 100644 --- a/services/distributeddataservice/service/object/object_manager.h +++ b/services/distributeddataservice/service/object/object_manager.h @@ -22,12 +22,10 @@ #include "device_manager_adapter.h" #include "object_callback.h" #include "object_callback_proxy.h" -#include "task_scheduler.h" #include "kv_store_delegate_manager.h" #include "kvstore_sync_callback.h" #include "object_common.h" #include "object_data_listener.h" -#include "timer.h" #include "types.h" namespace OHOS { @@ -93,6 +91,7 @@ public: void NotifyChange(std::map> &changedData); void CloseAfterMinute(); int32_t Open(); + void SetThreadPool(std::shared_ptr executors); private: constexpr static const char *SEPERATOR = "_"; constexpr static const char *LOCAL_DEVICE = "local"; @@ -150,11 +149,10 @@ private: uint32_t syncCount_ = 0; std::string userId_; std::atomic isSyncing_ = false; - Utils::Timer timer_; ConcurrentMap callbacks_; static constexpr size_t TIME_TASK_NUM = 1; static constexpr int64_t INTERVAL = 1; - TaskScheduler scheduler_ { TIME_TASK_NUM, "object_mgr" }; + std::shared_ptr executors_; }; } // namespace DistributedObject } // namespace OHOS diff --git a/services/distributeddataservice/service/object/object_service_impl.cpp b/services/distributeddataservice/service/object/object_service_impl.cpp index 8b65f0ed..f34ad858 100644 --- a/services/distributeddataservice/service/object/object_service_impl.cpp +++ b/services/distributeddataservice/service/object/object_service_impl.cpp @@ -272,4 +272,9 @@ int32_t ObjectServiceImpl::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, con ObjectServiceImpl::ObjectServiceImpl() { } +int32_t ObjectServiceImpl::OnExecutor(std::shared_ptr executors) +{ + executors_ = executors; + return 0; +} } // namespace OHOS::DistributedObject diff --git a/services/distributeddataservice/service/object/object_service_impl.h b/services/distributeddataservice/service/object/object_service_impl.h index a7f40e51..932ea29a 100644 --- a/services/distributeddataservice/service/object/object_service_impl.h +++ b/services/distributeddataservice/service/object/object_service_impl.h @@ -42,6 +42,7 @@ public: int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &appId) override; int32_t OnInitialize() override; + int32_t OnExecutor(std::shared_ptr executors) override; int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId) override; @@ -52,6 +53,7 @@ private: ~Factory(); }; static Factory factory_; + std::shared_ptr executors_; }; } // namespace OHOS::DistributedObject #endif diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 6f7eb43c..d5c742bc 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -68,11 +68,9 @@ void RdbServiceImpl::DeathRecipientImpl::OnRemoteDied(const wptr } } -RdbServiceImpl::RdbServiceImpl() - : timer_("SyncerTimer", -1), autoLaunchObserver_(this) +RdbServiceImpl::RdbServiceImpl() : autoLaunchObserver_(this) { ZLOGI("construct"); - timer_.Setup(); DistributedDB::RelationalStoreManager::SetAutoLaunchRequestCallback( [this](const std::string& identifier, DistributedDB::AutoLaunchParam ¶m) { return ResolveAutoLaunch(identifier, param); @@ -125,9 +123,6 @@ void RdbServiceImpl::OnClientDied(pid_t pid) ZLOGI("client dead pid=%{public}d", pid); syncers_.ComputeIfPresent(pid, [this](const auto& key, StoreSyncersType& syncers) { syncerNum_ -= static_cast(syncers.size()); - for (const auto& [name, syncer] : syncers) { - timer_.Unregister(syncer->GetTimerId()); - } return false; }); notifiers_.Erase(pid); @@ -236,9 +231,10 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa if (it != syncers.end()) { syncer = it->second; if (!param.isEncrypt_ || param.password_.empty()) { - timer_.Unregister(syncer->GetTimerId()); - uint32_t timerId = timer_.Register([this, syncer]() { SyncerTimeout(syncer); }, SYNCER_TIMEOUT, true); - syncer->SetTimerId(timerId); + executors_->Execute( + [this, syncer] { + SyncerTimeout(syncer); + }, std::chrono::milliseconds(SYNCER_TIMEOUT)); return true; } syncers.erase(storeId); @@ -262,8 +258,10 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa syncers[storeId] = syncer_; syncer = syncer_; syncerNum_++; - uint32_t timerId = timer_.Register([this, syncer]() { SyncerTimeout(syncer); }, SYNCER_TIMEOUT, true); - syncer->SetTimerId(timerId); + executors_->Execute( + [this, syncer]() { + SyncerTimeout(syncer); + }, std::chrono::milliseconds(SYNCER_TIMEOUT)); return !syncers.empty(); }); @@ -446,4 +444,9 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) delete syncer; return RDB_OK; } +int32_t RdbServiceImpl::OnExecutor(std::shared_ptr executors) +{ + executors_ = executors; + return 0; +} } // namespace OHOS::DistributedRdb diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index 35f93b7c..4a32c712 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -27,7 +27,6 @@ #include "rdb_notifier_proxy.h" #include "rdb_syncer.h" #include "store_observer.h" -#include "timer.h" #include "visibility.h" namespace OHOS::DistributedRdb { class API_EXPORT RdbServiceImpl : public RdbServiceStub { @@ -56,6 +55,8 @@ public: int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; + int32_t OnExecutor(std::shared_ptr executors) override; + protected: int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option, const RdbPredicates& predicates, SyncResult& result) override; @@ -98,7 +99,6 @@ private: ConcurrentMap syncers_; ConcurrentMap> notifiers_; ConcurrentMap identifiers_; - Utils::Timer timer_; RdbStoreObserverImpl autoLaunchObserver_; static Factory factory_; @@ -108,6 +108,7 @@ private: static constexpr int32_t MAX_SYNCER_NUM = 50; static constexpr int32_t MAX_SYNCER_PER_PROCESS = 10; static constexpr int32_t SYNCER_TIMEOUT = 60 * 1000; // ms + std::shared_ptr executors_; }; } // namespace OHOS::DistributedRdb #endif diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/services/distributeddataservice/service/rdb/rdb_syncer.cpp index 3d47af3e..f61d8540 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -62,16 +62,6 @@ RdbSyncer::~RdbSyncer() noexcept } } -void RdbSyncer::SetTimerId(uint32_t timerId) -{ - timerId_ = timerId; -} - -uint32_t RdbSyncer::GetTimerId() const -{ - return timerId_; -} - pid_t RdbSyncer::GetPid() const { return pid_; diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.h b/services/distributeddataservice/service/rdb/rdb_syncer.h index 9b5f90b4..c768bdf5 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -39,10 +39,6 @@ public: pid_t GetPid() const; - void SetTimerId(uint32_t timerId); - - uint32_t GetTimerId() const; - std::string GetStoreId() const; std::string GetIdentifier() const; @@ -85,7 +81,6 @@ private: pid_t pid_ {}; pid_t uid_ {}; uint32_t token_ {}; - uint32_t timerId_ {}; static std::vector GetConnectDevices(); static std::vector NetworkIdToUUID(const std::vector& networkIds); -- Gitee From 27b91d797ae6c76efd7e04dd256b5707f0fc46c8 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sat, 6 May 2023 11:02:41 +0800 Subject: [PATCH 097/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../distributeddataservice/framework/include/store/auto_cache.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/framework/include/store/auto_cache.h b/services/distributeddataservice/framework/include/store/auto_cache.h index ffed302f..4267a895 100644 --- a/services/distributeddataservice/framework/include/store/auto_cache.h +++ b/services/distributeddataservice/framework/include/store/auto_cache.h @@ -17,7 +17,7 @@ #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_AUTO_CACHE_H #include #include - +#include #include "concurrent_map.h" #include "error/general_error.h" #include "executor_pool.h" -- Gitee From 7fd2008749b25b98dd2e858ff3ad8d3d2a69ba9f Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sat, 6 May 2023 11:31:39 +0800 Subject: [PATCH 098/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../communicator/src/device_manager_adapter.cpp | 2 +- .../dfx/src/behaviour/behaviour_reporter_impl.cpp | 3 ++- .../adapter/dfx/src/fault/database_fault_impl.cpp | 3 ++- .../src/statistic/api_performance_statistic_impl.cpp | 3 ++- .../dfx/test/unittest/distributeddata_dfx_mst_test.cpp | 7 ++----- .../dfx/test/unittest/distributeddata_dfx_ut_test.cpp | 6 ++---- .../app/src/feature_stub_impl.cpp | 10 ---------- .../app/src/kvstore_data_service.cpp | 4 +++- .../distributeddataservice/app/src/security/security.h | 2 +- .../app/src/session_manager/upgrade_manager.cpp | 4 ++-- services/distributeddataservice/app/src/task_manager.h | 2 +- .../service/object/object_manager.cpp | 5 +++-- 12 files changed, 21 insertions(+), 30 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index 9fdf2d15..4786abf6 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -60,7 +60,7 @@ void DataMgrDmStateCall::OnDeviceReady(const DmDeviceInfo &info) class DataMgrDmInitCall final : public DistributedHardware::DmInitCallback { public: explicit DataMgrDmInitCall(DeviceManagerAdapter &dmAdapter, std::shared_ptr executors) - : dmAdapter_(dmAdapter), executors_(executors){} + : dmAdapter_(dmAdapter), executors_(executors) {} void OnRemoteDied() override; private: diff --git a/services/distributeddataservice/adapter/dfx/src/behaviour/behaviour_reporter_impl.cpp b/services/distributeddataservice/adapter/dfx/src/behaviour/behaviour_reporter_impl.cpp index 777e14c8..9e3b4619 100644 --- a/services/distributeddataservice/adapter/dfx/src/behaviour/behaviour_reporter_impl.cpp +++ b/services/distributeddataservice/adapter/dfx/src/behaviour/behaviour_reporter_impl.cpp @@ -22,7 +22,8 @@ ReportStatus BehaviourReporterImpl::Report(const BehaviourMsg &msg) HiViewAdapter::ReportBehaviour(DfxCodeConstant::DATABASE_BEHAVIOUR, msg, executors_); return ReportStatus::SUCCESS; } -void BehaviourReporterImpl::SetThreadPool(std::shared_ptr executors) { +void BehaviourReporterImpl::SetThreadPool(std::shared_ptr executors) +{ executors_ = executors; } } // namespace DistributedDataDfx diff --git a/services/distributeddataservice/adapter/dfx/src/fault/database_fault_impl.cpp b/services/distributeddataservice/adapter/dfx/src/fault/database_fault_impl.cpp index f7e99cfa..9369a31b 100644 --- a/services/distributeddataservice/adapter/dfx/src/fault/database_fault_impl.cpp +++ b/services/distributeddataservice/adapter/dfx/src/fault/database_fault_impl.cpp @@ -33,7 +33,8 @@ ReportStatus DatabaseFaultImpl::Report(const DBFaultMsg &msg) HiViewAdapter::ReportDBFault(eventID, msg, executors_); return ReportStatus::SUCCESS; } -void DatabaseFaultImpl::SetThreadPool(std::shared_ptr executors) { +void DatabaseFaultImpl::SetThreadPool(std::shared_ptr executors) +{ executors_ = executors; } } // namespace DistributedDataDfx diff --git a/services/distributeddataservice/adapter/dfx/src/statistic/api_performance_statistic_impl.cpp b/services/distributeddataservice/adapter/dfx/src/statistic/api_performance_statistic_impl.cpp index da1bcd45..0bf922a1 100644 --- a/services/distributeddataservice/adapter/dfx/src/statistic/api_performance_statistic_impl.cpp +++ b/services/distributeddataservice/adapter/dfx/src/statistic/api_performance_statistic_impl.cpp @@ -22,7 +22,8 @@ ReportStatus ApiPerformanceStatisticImpl::Report(const ApiPerformanceStat &stat) HiViewAdapter::ReportApiPerformanceStatistic(DfxCodeConstant::API_PERFORMANCE_INTERFACE, stat, executors_); return ReportStatus::SUCCESS; } -void ApiPerformanceStatisticImpl::SetThreadPool(std::shared_ptr executors) { +void ApiPerformanceStatisticImpl::SetThreadPool(std::shared_ptr executors) +{ executors_ = executors; } } // namespace DistributedDataDfx diff --git a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_mst_test.cpp b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_mst_test.cpp index ea5fd516..a45c47b4 100644 --- a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_mst_test.cpp +++ b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_mst_test.cpp @@ -29,19 +29,16 @@ public: void SetUp(); void TearDown(); -protected: - static std::shared_ptr executors_; }; -std::shared_ptr DistributedataDfxMSTTest::executors_ = std::make_shared(12, 5); - void DistributedataDfxMSTTest::SetUpTestCase() { - Reporter::GetInstance()->SetThreadPool(DistributedataDfxMSTTest::executors_); + Reporter::GetInstance()->SetThreadPool(std::make_shared(12, 5)); } void DistributedataDfxMSTTest::TearDownTestCase() { + Reporter::GetInstance()->SetThreadPool(nullptr); } void DistributedataDfxMSTTest::SetUp() {} diff --git a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp index 3b08cd5e..7afb8a0d 100644 --- a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp +++ b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp @@ -31,19 +31,17 @@ public: void SetUp(); void TearDown(); -protected: - static std::shared_ptr executors_; }; -std::shared_ptr DistributedataDfxUTTest::executors_ = std::make_shared(12, 5); void DistributedataDfxUTTest::SetUpTestCase() { - Reporter::GetInstance()->SetThreadPool(DistributedataDfxUTTest::executors_); + Reporter::GetInstance()->SetThreadPool(std::make_shared(12, 5)); FakeHivew::Clear(); } void DistributedataDfxUTTest::TearDownTestCase() { + Reporter::GetInstance()->SetThreadPool(nullptr); FakeHivew::Clear(); } diff --git a/services/distributeddataservice/app/src/feature_stub_impl.cpp b/services/distributeddataservice/app/src/feature_stub_impl.cpp index 46ff92d8..1df3a6e5 100644 --- a/services/distributeddataservice/app/src/feature_stub_impl.cpp +++ b/services/distributeddataservice/app/src/feature_stub_impl.cpp @@ -96,14 +96,4 @@ int32_t FeatureStubImpl::OnReady(const std::string &device) } return featureImpl_->OnReady(device); } - -void FeatureStubImpl::SetThreadPool(TaskExecutorAdapter* threadPool) -{ - threadPool_ = threadPool; -} - -TaskExecutorAdapter* FeatureStubImpl::GetThreadPool() -{ - return threadPool_; -} } diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index a31bdf50..75897d7d 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -229,7 +229,9 @@ void KvStoreDataService::OnStart() { ZLOGI("distributeddata service onStart"); EventCenter::Defer defer; - executors_ = std::make_shared(12, 5); + size_t max = 12; + size_t min = 5; + executors_ = std::make_shared(max, min); Reporter::GetInstance()->SetThreadPool(executors_); AccountDelegate::GetInstance()->RegisterHashFunc(Crypto::Sha256); DmAdapter::GetInstance().Init(executors_); diff --git a/services/distributeddataservice/app/src/security/security.h b/services/distributeddataservice/app/src/security/security.h index c2a8f4c0..702f2af2 100644 --- a/services/distributeddataservice/app/src/security/security.h +++ b/services/distributeddataservice/app/src/security/security.h @@ -34,7 +34,7 @@ public: using OnAccessControlledEvent = DistributedDB::OnAccessControlledEvent; using SecurityOption = DistributedDB::SecurityOption; Security(); - Security(std::shared_ptr executors) : executors_(executors){}; + explicit Security(std::shared_ptr executors) : executors_(executors) {}; ~Security() override; static bool IsSupportSecurity(); diff --git a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp index 803adeea..de0262c2 100644 --- a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp +++ b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp @@ -49,8 +49,8 @@ void UpgradeManager::Init(std::shared_ptr executors) CapMetaData UpgradeManager::GetCapability(const std::string &deviceId, bool &status) { status = true; - if (capabilityMap_.Contains(deviceId)) { - return capabilityMap_.Find(deviceId).second; + if (capabilities_.Contains(deviceId)) { + return capabilities_.Find(deviceId).second; } ZLOGI("load capability from meta"); CapMetaData capMetaData; diff --git a/services/distributeddataservice/app/src/task_manager.h b/services/distributeddataservice/app/src/task_manager.h index a2d9d76a..e997cab6 100644 --- a/services/distributeddataservice/app/src/task_manager.h +++ b/services/distributeddataservice/app/src/task_manager.h @@ -23,7 +23,7 @@ public: using Task = std::function; using Duration = std::chrono::steady_clock::duration; TaskManager() = default; - TaskManager(std::shared_ptr executors); + explicit TaskManager(std::shared_ptr executors); ~TaskManager() override; TaskId Execute(const Task &task) override; TaskId Execute(const Task &task, Duration delay) override; diff --git a/services/distributeddataservice/service/object/object_manager.cpp b/services/distributeddataservice/service/object/object_manager.cpp index 4abd5e9f..2d645dd6 100644 --- a/services/distributeddataservice/service/object/object_manager.cpp +++ b/services/distributeddataservice/service/object/object_manager.cpp @@ -350,7 +350,7 @@ void ObjectStoreManager::FlushClosedStore() ZLOGD("close store"); auto status = kvStoreDelegateManager_->CloseKvStore(delegate_); if (status != DistributedDB::DBStatus::OK) { - int timeOut = 1000; + int timeOut = 1000; executors_->Execute( [this]() { FlushClosedStore(); @@ -583,7 +583,8 @@ std::string ObjectStoreManager::GetBundleName(const std::string &key) return result; } -void ObjectStoreManager::SetThreadPool(std::shared_ptr executors) { +void ObjectStoreManager::SetThreadPool(std::shared_ptr executors) +{ executors_ = executors; } -- Gitee From dce93632f607ddca41b706e2550d06d40e92dcc4 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Sat, 6 May 2023 11:32:56 +0800 Subject: [PATCH 099/152] add type convert proxy Signed-off-by: Sven Wang --- .../framework/include/store/general_value.h | 5 +- .../framework/store/auto_cache.cpp | 2 +- .../distributeddataservice/service/BUILD.gn | 1 + .../service/rdb/value_proxy.cpp | 255 ++++++++++++++++++ .../service/rdb/value_proxy.h | 172 ++++++++++++ 5 files changed, 432 insertions(+), 3 deletions(-) create mode 100644 services/distributeddataservice/service/rdb/value_proxy.cpp create mode 100644 services/distributeddataservice/service/rdb/value_proxy.h diff --git a/services/distributeddataservice/framework/include/store/general_value.h b/services/distributeddataservice/framework/include/store/general_value.h index 9f2a6407..05d4766d 100644 --- a/services/distributeddataservice/framework/include/store/general_value.h +++ b/services/distributeddataservice/framework/include/store/general_value.h @@ -20,8 +20,9 @@ #include #include #include -#include "traits.h" + #include "error/general_error.h" +#include "traits.h" namespace OHOS::DistributedData { struct Asset { uint32_t version; @@ -69,7 +70,7 @@ bool GetItem(T &&input, O &output) template bool GetItem(T &&input, O &output) { - auto val = Traits::get_if(&input); + auto val = Traits::get_if(&input); if (val != nullptr) { output = std::move(*val); return true; diff --git a/services/distributeddataservice/framework/store/auto_cache.cpp b/services/distributeddataservice/framework/store/auto_cache.cpp index 212f6c1b..edeb10cd 100644 --- a/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/services/distributeddataservice/framework/store/auto_cache.cpp @@ -15,7 +15,7 @@ #define LOG_TAG "AutoCache" #include "store/auto_cache.h" -#include +#include "log_print.h" namespace OHOS::DistributedData { AutoCache &AutoCache::GetInstance() { diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index d05b54c5..de6204fc 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -101,6 +101,7 @@ ohos_shared_library("distributeddatasvc") { "rdb/rdb_service_stub.cpp", "rdb/rdb_store_observer_impl.cpp", "rdb/rdb_syncer.cpp", + "rdb/value_proxy.cpp", ] cflags = [ "-Wno-multichar" ] diff --git a/services/distributeddataservice/service/rdb/value_proxy.cpp b/services/distributeddataservice/service/rdb/value_proxy.cpp new file mode 100644 index 00000000..44c8b92a --- /dev/null +++ b/services/distributeddataservice/service/rdb/value_proxy.cpp @@ -0,0 +1,255 @@ +/* + * 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. + */ + +#include "value_proxy.h" +namespace OHOS::DistributedRdb { +ValueProxy::Value ValueProxy::Convert(DistributedData::Value &&value) +{ + Value proxy; + DistributedData::Convert(std::move(value), proxy.value_); + return proxy; +} + +ValueProxy::Value ValueProxy::Convert(NativeRdb::ValueObject &&value) +{ + Value proxy; + DistributedData::Convert(std::move(value.value), proxy.value_); + return proxy; +} + +ValueProxy::Values ValueProxy::Convert(DistributedData::Values &&values) +{ + Values proxy; + proxy.value_.reserve(values.size()); + for (auto &value : values) { + proxy.value_.emplace_back(Convert(std::move(value))); + } + return proxy; +} + +ValueProxy::Values ValueProxy::Convert(std::vector &&values) +{ + Values proxy; + proxy.value_.reserve(values.size()); + for (auto &value : values) { + proxy.value_.emplace_back(Convert(std::move(value))); + } + return proxy; +} + +ValueProxy::Bucket ValueProxy::Convert(DistributedData::VBucket &&bucket) +{ + ValueProxy::Bucket proxy; + for (auto &[key, value] : bucket) { + proxy.value_.insert_or_assign(key, Convert(std::move(value))); + } + return proxy; +} + +ValueProxy::Bucket ValueProxy::Convert(NativeRdb::ValuesBucket &&bucket) +{ + ValueProxy::Bucket proxy; + for (auto &[key, value] : bucket.values_) { + proxy.value_.insert_or_assign(key, Convert(std::move(value))); + } + return proxy; +} + +ValueProxy::Buckets ValueProxy::Convert(std::vector &&buckets) +{ + ValueProxy::Buckets proxy; + proxy.value_.reserve(buckets.size()); + for (auto &bucket : buckets) { + proxy.value_.emplace_back(Convert(std::move(bucket))); + } + return proxy; +} + +ValueProxy::Buckets ValueProxy::Convert(DistributedData::VBuckets &&buckets) +{ + ValueProxy::Buckets proxy; + proxy.value_.reserve(buckets.size()); + for (auto &bucket : buckets) { + proxy.value_.emplace_back(Convert(std::move(bucket))); + } + return proxy; +} + +ValueProxy::Asset::Asset(DistributedData::Asset asset) +{ + asset_ = std::move(asset); +} + +ValueProxy::Asset::Asset(NativeRdb::AssetValue asset) +{ + asset_ = DistributedData::Asset{ .version = asset.version, + .name = std::move(asset.name), + .uri = std::move(asset.uri), + .createTime = std::move(asset.createTime), + .modifyTime = std::move(asset.modifyTime), + .size = std::move(asset.size), + .hash = std::move(asset.hash) }; +} + +ValueProxy::Asset &ValueProxy::Asset::operator=(const Asset &proxy) +{ + if (this == &proxy) { + return *this; + } + asset_ = proxy.asset_; + return *this; +} + +ValueProxy::Asset &ValueProxy::Asset::operator=(Asset &&proxy) noexcept +{ + if (this == &proxy) { + return *this; + } + asset_ = std::move(proxy); + return *this; +} + +ValueProxy::Asset::operator NativeRdb::AssetValue() +{ + return NativeRdb::AssetValue{ .version = asset_.version, + .name = std::move(asset_.name), + .uri = std::move(asset_.uri), + .createTime = std::move(asset_.createTime), + .modifyTime = std::move(asset_.modifyTime), + .size = std::move(asset_.size), + .hash = std::move(asset_.hash) }; +} + +ValueProxy::Asset::operator DistributedData::Asset() +{ + return std::move(asset_); +} + +ValueProxy::Assets::Assets(DistributedData::Assets assets) +{ + assets_.clear(); + assets_.reserve(assets.size()); + for (auto &asset : assets) { + assets_.emplace_back(std::move(asset)); + } +} + +ValueProxy::Assets::Assets(NativeRdb::ValueObject::Assets assets) +{ + assets_.clear(); + assets_.reserve(assets.size()); + for (auto &asset : assets) { + assets_.emplace_back(std::move(asset)); + } +} + +ValueProxy::Assets &ValueProxy::Assets::operator=(const Assets &proxy) +{ + if (this == &proxy) { + return *this; + } + assets_ = proxy.assets_; + return *this; +} + +ValueProxy::Assets &ValueProxy::Assets::operator=(Assets &&proxy) noexcept +{ + if (this == &proxy) { + return *this; + } + assets_ = std::move(proxy.assets_); + return *this; +} + +ValueProxy::Assets::operator NativeRdb::ValueObject::Assets() +{ + NativeRdb::ValueObject::Assets assets; + assets.reserve(assets_.size()); + for (auto &asset : assets_) { + assets.push_back(std::move(asset)); + } + return assets; +} + +ValueProxy::Assets::operator DistributedData::Assets() +{ + DistributedData::Assets assets; + assets.reserve(assets_.size()); + for (auto &asset : assets_) { + assets.push_back(std::move(asset)); + } + return assets; +} + +ValueProxy::Value &ValueProxy::Value::operator=(ValueProxy::Value &&value) noexcept +{ + if (this == &value) { + return *this; + } + value_ = std::move(value.value_); + return *this; +} + +ValueProxy::Value::operator NativeRdb::ValueObject() +{ + NativeRdb::ValueObject object; + DistributedData::Convert(std::move(value_), object.value); + return object; +} + +ValueProxy::Value::operator DistributedData::Value() +{ + DistributedData::Value value; + DistributedData::Convert(std::move(value_), value); + return value; +} + +ValueProxy::Values &ValueProxy::Values::operator=(ValueProxy::Values &&values) noexcept +{ + if (this == &values) { + return *this; + } + value_ = std::move(values.value_); + return *this; +} + +ValueProxy::Bucket &ValueProxy::Bucket::operator=(Bucket &&bucket) noexcept +{ + if (this != &bucket) { + return *this; + } + value_ = std::move(bucket.value_); + return *this; +} + +ValueProxy::Bucket::operator NativeRdb::ValuesBucket() +{ + NativeRdb::ValuesBucket bucket; + for (auto &[key, value] : value_) { + bucket.values_.insert_or_assign(key, std::move(value)); + } + value_.clear(); + return bucket; +} + +ValueProxy::Buckets &ValueProxy::Buckets::operator=(Buckets &&buckets) noexcept +{ + if (this != &buckets) { + return *this; + } + value_ = std::move(buckets.value_); + return *this; +} +} // namespace OHOS::DistributedRdb \ No newline at end of file diff --git a/services/distributeddataservice/service/rdb/value_proxy.h b/services/distributeddataservice/service/rdb/value_proxy.h new file mode 100644 index 00000000..a22c6e4e --- /dev/null +++ b/services/distributeddataservice/service/rdb/value_proxy.h @@ -0,0 +1,172 @@ +/* + * 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_RDB_VALUE_PROXY_H +#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_VALUE_PROXY_H +#include "asset_value.h" +#include "store/general_value.h" +#include "value_object.h" +#include "values_bucket.h" +namespace OHOS::DistributedRdb { +class ValueProxy final { +public: + using Bytes = DistributedData::Bytes; + class Asset { + public: + Asset() = default; + Asset(Asset &&proxy) noexcept + { + *this = std::move(proxy); + }; + Asset(const Asset &proxy) + { + *this = proxy; + }; + Asset(DistributedData::Asset asset); + Asset(NativeRdb::AssetValue asset); + Asset &operator=(const Asset &proxy); + Asset &operator=(Asset &&proxy) noexcept; + operator NativeRdb::AssetValue(); + operator DistributedData::Asset(); + + private: + DistributedData::Asset asset_; + }; + + class Assets { + public: + Assets() = default; + Assets(Assets &&proxy) noexcept + { + *this = std::move(proxy); + }; + Assets(const Assets &proxy) + { + *this = proxy; + }; + Assets(DistributedData::Assets assets); + Assets(NativeRdb::ValueObject::Assets assets); + Assets &operator=(const Assets &proxy); + Assets &operator=(Assets &&proxy) noexcept; + operator NativeRdb::ValueObject::Assets(); + operator DistributedData::Assets(); + + private: + std::vector assets_; + }; + using Proxy = std::variant; + + class Value { + public: + Value() = default; + Value(Value &&value) noexcept + { + *this = std::move(value); + }; + Value &operator=(Value &&value) noexcept; + operator NativeRdb::ValueObject(); + operator DistributedData::Value(); + + private: + friend ValueProxy; + Proxy value_; + }; + class Values { + public: + Values() = default; + Values(Values &&values) noexcept + { + *this = std::move(values); + }; + Values &operator=(Values &&values) noexcept; + template + operator std::vector() + { + std::vector objects; + objects.reserve(value_.size()); + for (auto &proxy : value_) { + objects.emplace_back(std::move(proxy)); + } + value_.clear(); + return objects; + } + + private: + friend ValueProxy; + std::vector value_; + }; + class Bucket { + public: + Bucket() = default; + Bucket(Bucket &&bucket) noexcept + { + *this = std::move(bucket); + }; + Bucket &operator=(Bucket &&bucket) noexcept; + template + operator std::map() + { + std::map bucket; + for (auto &[key, value] : value_) { + bucket.insert_or_assign(key, std::move(value)); + } + value_.clear(); + return bucket; + } + operator NativeRdb::ValuesBucket(); + + private: + friend ValueProxy; + std::map value_; + }; + class Buckets { + public: + Buckets() = default; + Buckets(Buckets &&buckets) noexcept + { + *this = std::move(buckets); + }; + Buckets &operator=(Buckets &&buckets) noexcept; + template + operator std::vector() + { + std::vector buckets; + buckets.reserve(value_.size()); + for (auto &bucket : value_) { + buckets.emplace_back(std::move(bucket)); + } + value_.clear(); + return buckets; + } + + private: + friend ValueProxy; + std::vector value_; + }; + static Value Convert(DistributedData::Value &&value); + static Value Convert(NativeRdb::ValueObject &&value); + static Values Convert(DistributedData::Values &&values); + static Values Convert(std::vector &&values); + static Bucket Convert(DistributedData::VBucket &&bucket); + static Bucket Convert(NativeRdb::ValuesBucket &&bucket); + static Buckets Convert(DistributedData::VBuckets &&buckets); + static Buckets Convert(std::vector &&buckets); + +private: + ValueProxy() = delete; + ~ValueProxy() = delete; +}; +} // namespace OHOS::DistributedRdb +#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_VALUE_PROXY_H -- Gitee From a781f0515a1f616fa29e2f30fdd3cd47ae815539 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sat, 6 May 2023 11:38:18 +0800 Subject: [PATCH 100/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../dfx/test/unittest/distributeddata_dfx_mst_test.cpp | 4 +++- .../adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp | 4 +++- .../distributeddataservice/app/src/kvstore_data_service.cpp | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_mst_test.cpp b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_mst_test.cpp index a45c47b4..5945937f 100644 --- a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_mst_test.cpp +++ b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_mst_test.cpp @@ -33,7 +33,9 @@ public: void DistributedataDfxMSTTest::SetUpTestCase() { - Reporter::GetInstance()->SetThreadPool(std::make_shared(12, 5)); + size_t max = 12; + size_t min = 5; + Reporter::GetInstance()->SetThreadPool(std::make_shared(max, min)); } void DistributedataDfxMSTTest::TearDownTestCase() diff --git a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp index 7afb8a0d..b93b5632 100644 --- a/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp +++ b/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp @@ -35,7 +35,9 @@ public: void DistributedataDfxUTTest::SetUpTestCase() { - Reporter::GetInstance()->SetThreadPool(std::make_shared(12, 5)); + size_t max = 12; + size_t min = 5; + Reporter::GetInstance()->SetThreadPool(std::make_shared(max, min)); FakeHivew::Clear(); } diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 75897d7d..0932c8d8 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -229,8 +229,8 @@ void KvStoreDataService::OnStart() { ZLOGI("distributeddata service onStart"); EventCenter::Defer defer; - size_t max = 12; - size_t min = 5; + size_t max = 12; + size_t min = 5; executors_ = std::make_shared(max, min); Reporter::GetInstance()->SetThreadPool(executors_); AccountDelegate::GetInstance()->RegisterHashFunc(Crypto::Sha256); -- Gitee From 3d502db680a01738a08c8949d0ecb5ee4c9a4564 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Sat, 6 May 2023 13:57:21 +0800 Subject: [PATCH 101/152] fixed not asset value Signed-off-by: Sven Wang --- .../distributeddataservice/service/BUILD.gn | 1 - .../service/rdb/value_proxy.cpp | 255 ------------------ .../service/rdb/value_proxy.h | 172 ------------ 3 files changed, 428 deletions(-) delete mode 100644 services/distributeddataservice/service/rdb/value_proxy.cpp delete mode 100644 services/distributeddataservice/service/rdb/value_proxy.h diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index de6204fc..d05b54c5 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -101,7 +101,6 @@ ohos_shared_library("distributeddatasvc") { "rdb/rdb_service_stub.cpp", "rdb/rdb_store_observer_impl.cpp", "rdb/rdb_syncer.cpp", - "rdb/value_proxy.cpp", ] cflags = [ "-Wno-multichar" ] diff --git a/services/distributeddataservice/service/rdb/value_proxy.cpp b/services/distributeddataservice/service/rdb/value_proxy.cpp deleted file mode 100644 index 44c8b92a..00000000 --- a/services/distributeddataservice/service/rdb/value_proxy.cpp +++ /dev/null @@ -1,255 +0,0 @@ -/* - * 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. - */ - -#include "value_proxy.h" -namespace OHOS::DistributedRdb { -ValueProxy::Value ValueProxy::Convert(DistributedData::Value &&value) -{ - Value proxy; - DistributedData::Convert(std::move(value), proxy.value_); - return proxy; -} - -ValueProxy::Value ValueProxy::Convert(NativeRdb::ValueObject &&value) -{ - Value proxy; - DistributedData::Convert(std::move(value.value), proxy.value_); - return proxy; -} - -ValueProxy::Values ValueProxy::Convert(DistributedData::Values &&values) -{ - Values proxy; - proxy.value_.reserve(values.size()); - for (auto &value : values) { - proxy.value_.emplace_back(Convert(std::move(value))); - } - return proxy; -} - -ValueProxy::Values ValueProxy::Convert(std::vector &&values) -{ - Values proxy; - proxy.value_.reserve(values.size()); - for (auto &value : values) { - proxy.value_.emplace_back(Convert(std::move(value))); - } - return proxy; -} - -ValueProxy::Bucket ValueProxy::Convert(DistributedData::VBucket &&bucket) -{ - ValueProxy::Bucket proxy; - for (auto &[key, value] : bucket) { - proxy.value_.insert_or_assign(key, Convert(std::move(value))); - } - return proxy; -} - -ValueProxy::Bucket ValueProxy::Convert(NativeRdb::ValuesBucket &&bucket) -{ - ValueProxy::Bucket proxy; - for (auto &[key, value] : bucket.values_) { - proxy.value_.insert_or_assign(key, Convert(std::move(value))); - } - return proxy; -} - -ValueProxy::Buckets ValueProxy::Convert(std::vector &&buckets) -{ - ValueProxy::Buckets proxy; - proxy.value_.reserve(buckets.size()); - for (auto &bucket : buckets) { - proxy.value_.emplace_back(Convert(std::move(bucket))); - } - return proxy; -} - -ValueProxy::Buckets ValueProxy::Convert(DistributedData::VBuckets &&buckets) -{ - ValueProxy::Buckets proxy; - proxy.value_.reserve(buckets.size()); - for (auto &bucket : buckets) { - proxy.value_.emplace_back(Convert(std::move(bucket))); - } - return proxy; -} - -ValueProxy::Asset::Asset(DistributedData::Asset asset) -{ - asset_ = std::move(asset); -} - -ValueProxy::Asset::Asset(NativeRdb::AssetValue asset) -{ - asset_ = DistributedData::Asset{ .version = asset.version, - .name = std::move(asset.name), - .uri = std::move(asset.uri), - .createTime = std::move(asset.createTime), - .modifyTime = std::move(asset.modifyTime), - .size = std::move(asset.size), - .hash = std::move(asset.hash) }; -} - -ValueProxy::Asset &ValueProxy::Asset::operator=(const Asset &proxy) -{ - if (this == &proxy) { - return *this; - } - asset_ = proxy.asset_; - return *this; -} - -ValueProxy::Asset &ValueProxy::Asset::operator=(Asset &&proxy) noexcept -{ - if (this == &proxy) { - return *this; - } - asset_ = std::move(proxy); - return *this; -} - -ValueProxy::Asset::operator NativeRdb::AssetValue() -{ - return NativeRdb::AssetValue{ .version = asset_.version, - .name = std::move(asset_.name), - .uri = std::move(asset_.uri), - .createTime = std::move(asset_.createTime), - .modifyTime = std::move(asset_.modifyTime), - .size = std::move(asset_.size), - .hash = std::move(asset_.hash) }; -} - -ValueProxy::Asset::operator DistributedData::Asset() -{ - return std::move(asset_); -} - -ValueProxy::Assets::Assets(DistributedData::Assets assets) -{ - assets_.clear(); - assets_.reserve(assets.size()); - for (auto &asset : assets) { - assets_.emplace_back(std::move(asset)); - } -} - -ValueProxy::Assets::Assets(NativeRdb::ValueObject::Assets assets) -{ - assets_.clear(); - assets_.reserve(assets.size()); - for (auto &asset : assets) { - assets_.emplace_back(std::move(asset)); - } -} - -ValueProxy::Assets &ValueProxy::Assets::operator=(const Assets &proxy) -{ - if (this == &proxy) { - return *this; - } - assets_ = proxy.assets_; - return *this; -} - -ValueProxy::Assets &ValueProxy::Assets::operator=(Assets &&proxy) noexcept -{ - if (this == &proxy) { - return *this; - } - assets_ = std::move(proxy.assets_); - return *this; -} - -ValueProxy::Assets::operator NativeRdb::ValueObject::Assets() -{ - NativeRdb::ValueObject::Assets assets; - assets.reserve(assets_.size()); - for (auto &asset : assets_) { - assets.push_back(std::move(asset)); - } - return assets; -} - -ValueProxy::Assets::operator DistributedData::Assets() -{ - DistributedData::Assets assets; - assets.reserve(assets_.size()); - for (auto &asset : assets_) { - assets.push_back(std::move(asset)); - } - return assets; -} - -ValueProxy::Value &ValueProxy::Value::operator=(ValueProxy::Value &&value) noexcept -{ - if (this == &value) { - return *this; - } - value_ = std::move(value.value_); - return *this; -} - -ValueProxy::Value::operator NativeRdb::ValueObject() -{ - NativeRdb::ValueObject object; - DistributedData::Convert(std::move(value_), object.value); - return object; -} - -ValueProxy::Value::operator DistributedData::Value() -{ - DistributedData::Value value; - DistributedData::Convert(std::move(value_), value); - return value; -} - -ValueProxy::Values &ValueProxy::Values::operator=(ValueProxy::Values &&values) noexcept -{ - if (this == &values) { - return *this; - } - value_ = std::move(values.value_); - return *this; -} - -ValueProxy::Bucket &ValueProxy::Bucket::operator=(Bucket &&bucket) noexcept -{ - if (this != &bucket) { - return *this; - } - value_ = std::move(bucket.value_); - return *this; -} - -ValueProxy::Bucket::operator NativeRdb::ValuesBucket() -{ - NativeRdb::ValuesBucket bucket; - for (auto &[key, value] : value_) { - bucket.values_.insert_or_assign(key, std::move(value)); - } - value_.clear(); - return bucket; -} - -ValueProxy::Buckets &ValueProxy::Buckets::operator=(Buckets &&buckets) noexcept -{ - if (this != &buckets) { - return *this; - } - value_ = std::move(buckets.value_); - return *this; -} -} // namespace OHOS::DistributedRdb \ No newline at end of file diff --git a/services/distributeddataservice/service/rdb/value_proxy.h b/services/distributeddataservice/service/rdb/value_proxy.h deleted file mode 100644 index a22c6e4e..00000000 --- a/services/distributeddataservice/service/rdb/value_proxy.h +++ /dev/null @@ -1,172 +0,0 @@ -/* - * 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_RDB_VALUE_PROXY_H -#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_VALUE_PROXY_H -#include "asset_value.h" -#include "store/general_value.h" -#include "value_object.h" -#include "values_bucket.h" -namespace OHOS::DistributedRdb { -class ValueProxy final { -public: - using Bytes = DistributedData::Bytes; - class Asset { - public: - Asset() = default; - Asset(Asset &&proxy) noexcept - { - *this = std::move(proxy); - }; - Asset(const Asset &proxy) - { - *this = proxy; - }; - Asset(DistributedData::Asset asset); - Asset(NativeRdb::AssetValue asset); - Asset &operator=(const Asset &proxy); - Asset &operator=(Asset &&proxy) noexcept; - operator NativeRdb::AssetValue(); - operator DistributedData::Asset(); - - private: - DistributedData::Asset asset_; - }; - - class Assets { - public: - Assets() = default; - Assets(Assets &&proxy) noexcept - { - *this = std::move(proxy); - }; - Assets(const Assets &proxy) - { - *this = proxy; - }; - Assets(DistributedData::Assets assets); - Assets(NativeRdb::ValueObject::Assets assets); - Assets &operator=(const Assets &proxy); - Assets &operator=(Assets &&proxy) noexcept; - operator NativeRdb::ValueObject::Assets(); - operator DistributedData::Assets(); - - private: - std::vector assets_; - }; - using Proxy = std::variant; - - class Value { - public: - Value() = default; - Value(Value &&value) noexcept - { - *this = std::move(value); - }; - Value &operator=(Value &&value) noexcept; - operator NativeRdb::ValueObject(); - operator DistributedData::Value(); - - private: - friend ValueProxy; - Proxy value_; - }; - class Values { - public: - Values() = default; - Values(Values &&values) noexcept - { - *this = std::move(values); - }; - Values &operator=(Values &&values) noexcept; - template - operator std::vector() - { - std::vector objects; - objects.reserve(value_.size()); - for (auto &proxy : value_) { - objects.emplace_back(std::move(proxy)); - } - value_.clear(); - return objects; - } - - private: - friend ValueProxy; - std::vector value_; - }; - class Bucket { - public: - Bucket() = default; - Bucket(Bucket &&bucket) noexcept - { - *this = std::move(bucket); - }; - Bucket &operator=(Bucket &&bucket) noexcept; - template - operator std::map() - { - std::map bucket; - for (auto &[key, value] : value_) { - bucket.insert_or_assign(key, std::move(value)); - } - value_.clear(); - return bucket; - } - operator NativeRdb::ValuesBucket(); - - private: - friend ValueProxy; - std::map value_; - }; - class Buckets { - public: - Buckets() = default; - Buckets(Buckets &&buckets) noexcept - { - *this = std::move(buckets); - }; - Buckets &operator=(Buckets &&buckets) noexcept; - template - operator std::vector() - { - std::vector buckets; - buckets.reserve(value_.size()); - for (auto &bucket : value_) { - buckets.emplace_back(std::move(bucket)); - } - value_.clear(); - return buckets; - } - - private: - friend ValueProxy; - std::vector value_; - }; - static Value Convert(DistributedData::Value &&value); - static Value Convert(NativeRdb::ValueObject &&value); - static Values Convert(DistributedData::Values &&values); - static Values Convert(std::vector &&values); - static Bucket Convert(DistributedData::VBucket &&bucket); - static Bucket Convert(NativeRdb::ValuesBucket &&bucket); - static Buckets Convert(DistributedData::VBuckets &&buckets); - static Buckets Convert(std::vector &&buckets); - -private: - ValueProxy() = delete; - ~ValueProxy() = delete; -}; -} // namespace OHOS::DistributedRdb -#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_VALUE_PROXY_H -- Gitee From cdaeab1af9f8f316a04f87ca8e41770e2376e24e Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sat, 6 May 2023 14:06:11 +0800 Subject: [PATCH 102/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../test/unittest/device_manager_adapter_test.cpp | 5 ++++- services/distributeddataservice/app/BUILD.gn | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/adapter/communicator/test/unittest/device_manager_adapter_test.cpp b/services/distributeddataservice/adapter/communicator/test/unittest/device_manager_adapter_test.cpp index e91ac0e4..967a6d03 100644 --- a/services/distributeddataservice/adapter/communicator/test/unittest/device_manager_adapter_test.cpp +++ b/services/distributeddataservice/adapter/communicator/test/unittest/device_manager_adapter_test.cpp @@ -15,6 +15,7 @@ #include "device_manager_adapter.h" +#include "executor_pool.h" #include "gtest/gtest.h" #include "types.h" namespace { @@ -36,7 +37,9 @@ class DeviceManagerAdapterTest : public testing::Test { public: static void SetUpTestCase(void) { - DeviceManagerAdapter::GetInstance().Init(); + size_t max = 12; + size_t min = 5; + DeviceManagerAdapter::GetInstance().Init(std::make_shared(max, min)); } static void TearDownTestCase(void) {} void SetUp() {} diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 48797a85..87f34f1f 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -89,6 +89,7 @@ ohos_shared_library("distributeddataservice") { "src/session_manager/route_head_handler_impl.cpp", "src/session_manager/session_manager.cpp", "src/session_manager/upgrade_manager.cpp", + "src/task_manager.cpp", ] if (datamgr_service_power) { -- Gitee From 2911f4583e51b8b636f8684f52df0534dde12c19 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sat, 6 May 2023 14:15:32 +0800 Subject: [PATCH 103/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../communicator/test/unittest/device_manager_adapter_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/adapter/communicator/test/unittest/device_manager_adapter_test.cpp b/services/distributeddataservice/adapter/communicator/test/unittest/device_manager_adapter_test.cpp index 967a6d03..6e0df490 100644 --- a/services/distributeddataservice/adapter/communicator/test/unittest/device_manager_adapter_test.cpp +++ b/services/distributeddataservice/adapter/communicator/test/unittest/device_manager_adapter_test.cpp @@ -15,8 +15,8 @@ #include "device_manager_adapter.h" -#include "executor_pool.h" #include "gtest/gtest.h" +#include "executor_pool.h" #include "types.h" namespace { using namespace testing::ext; -- Gitee From f7eaf91c8275cf92124385f3d900f9d50739953d Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sat, 6 May 2023 14:32:34 +0800 Subject: [PATCH 104/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../app/test/unittest/kvstore_data_service_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp b/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp index f016c8cb..9369578b 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp @@ -57,7 +57,7 @@ HWTEST_F(KvStoreDataServiceTest, RegisterClientDeathObserver001, TestSize.Level1 KvStoreDataService kvDataService; Bootstrap::GetInstance().LoadComponents(); Bootstrap::GetInstance().LoadCheckers(); - KvStoreMetaManager::GetInstance().InitMetaParameter(); + KvStoreMetaManager::GetInstance().InitMetaParameter(std::make_shared(12, 5)); Status status = kvDataService.RegisterClientDeathObserver(appId, new KvStoreClientDeathObserver()); EXPECT_EQ(status, Status::SUCCESS) << "RegisterClientDeathObserver failed"; } -- Gitee From eee6f74fec95f00f70bd917ac0ced45970b26cca Mon Sep 17 00:00:00 2001 From: hanlu Date: Sat, 6 May 2023 15:31:30 +0800 Subject: [PATCH 105/152] f Signed-off-by: hanlu --- .../distributeddataservice/service/BUILD.gn | 10 - .../data_share/common/rdb_delegate.cpp | 4 +- .../data_share/common/scheduler_manager.cpp | 156 ------ .../data_share/common/scheduler_manager.h | 48 -- .../data_share/common/template_manager.cpp | 516 ------------------ .../data_share/common/template_manager.h | 118 ---- .../data_share/data/json_formatter.cpp | 38 -- .../service/data_share/data/json_formatter.h | 36 -- .../data_share/data/published_data.cpp | 169 ------ .../service/data_share/data/published_data.h | 56 -- .../data/resultset_json_formatter.cpp | 48 -- .../data/resultset_json_formatter.h | 35 -- .../service/data_share/data/template_data.cpp | 95 ---- .../service/data_share/data/template_data.h | 54 -- .../data_share/data_share_service_impl.cpp | 131 +---- .../data_share/data_share_service_stub.cpp | 6 +- .../data_share/data_share_types_util.cpp | 84 +-- .../strategies/get_data_strategy.cpp | 16 +- .../data_share/strategies/get_data_strategy.h | 3 - .../strategies/publish_strategy.cpp | 15 +- 20 files changed, 35 insertions(+), 1603 deletions(-) delete mode 100644 services/distributeddataservice/service/data_share/common/scheduler_manager.cpp delete mode 100644 services/distributeddataservice/service/data_share/common/scheduler_manager.h delete mode 100644 services/distributeddataservice/service/data_share/common/template_manager.cpp delete mode 100644 services/distributeddataservice/service/data_share/common/template_manager.h delete mode 100644 services/distributeddataservice/service/data_share/data/json_formatter.cpp delete mode 100644 services/distributeddataservice/service/data_share/data/json_formatter.h delete mode 100644 services/distributeddataservice/service/data_share/data/published_data.cpp delete mode 100644 services/distributeddataservice/service/data_share/data/published_data.h delete mode 100644 services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp delete mode 100644 services/distributeddataservice/service/data_share/data/resultset_json_formatter.h delete mode 100644 services/distributeddataservice/service/data_share/data/template_data.cpp delete mode 100644 services/distributeddataservice/service/data_share/data/template_data.h diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 543810b1..eff162a9 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -74,18 +74,8 @@ ohos_shared_library("distributeddatasvc") { "data_share/common/div_strategy.cpp", "data_share/common/kv_delegate.cpp", "data_share/common/rdb_delegate.cpp", - "data_share/common/scheduler_manager.cpp", "data_share/common/seq_strategy.cpp", - "data_share/common/template_manager.cpp", "data_share/common/uri_utils.cpp", - "data_share/data/json_formatter.cpp", - "data_share/data/published_data.cpp", - "data_share/data/resultset_json_formatter.cpp", - "data_share/data/template_data.cpp", - "data_share/data_share_obs_proxy.cpp", - "data_share/data_share_service_impl.cpp", - "data_share/data_share_service_stub.cpp", - "data_share/data_share_types_util.cpp", "data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp", "data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp", "data_share/strategies/delete_strategy.cpp", diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index ef91d9e6..59924717 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -16,8 +16,6 @@ #include "rdb_delegate.h" #include "log_print.h" -#include "resultset_json_formatter.h" -#include "scheduler_manager.h" #include "utils/anonymous.h" namespace OHOS::DataShare { @@ -122,7 +120,7 @@ std::shared_ptr RdbDelegate::Query( return nullptr; } - return std::make_shared(resultSet); + return nullptr; } int RdbDelegate::ExecuteSql(const std::string &sql) diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp deleted file mode 100644 index 8c64604d..00000000 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp +++ /dev/null @@ -1,156 +0,0 @@ -/* - * 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 "SchedulerManager" - -#include "scheduler_manager.h" - -#include "log_print.h" -#include "uri_utils.h" -#include "utils/anonymous.h" - -namespace OHOS::DataShare { -SchedulerManager &SchedulerManager::GetInstance() -{ - static SchedulerManager instance; - return instance; -} - -void SchedulerManager::Execute(const std::string &uri, const std::string &rdbDir, int version) -{ - if (!URIUtils::IsDataProxyURI(uri)) { - return; - } - int errCode = 0; - auto delegate = DBDelegate::Create(rdbDir, version, errCode, true); - if (delegate == nullptr) { - ZLOGE("malloc fail %{public}s", DistributedData::Anonymous::Change(uri).c_str()); - return; - } - std::vector keys = RdbSubscriberManager::GetInstance().GetKeysByUri(uri); - for (auto &key : keys) { - if (RdbSubscriberManager::GetInstance().GetObserverCount(key) == 0) { - continue; - } - ExecuteSchedulerSQL(key, delegate); - } -} - -void SchedulerManager::Execute(const Key &key, const std::string &rdbDir, int version) -{ - int errCode = 0; - auto delegate = DBDelegate::Create(rdbDir, version, errCode, true); - if (delegate == nullptr) { - ZLOGE("malloc fail %{public}s", DistributedData::Anonymous::Change(key.uri).c_str()); - return; - } - ExecuteSchedulerSQL(key, delegate); -} - -void SchedulerManager::SetTimer(const RdbStoreContext &rdbContext, const std::string &uri, int64_t subscriberId, - const std::string &bundleName, int64_t reminderTime) -{ - std::lock_guard lock(mutex_); - if (scheduler_ == nullptr) { - scheduler_ = std::make_shared(TIME_TASK_NUM, "remind_timer"); - } - auto manager = RdbSubscriberManager::GetInstance(); - Key key(uri, subscriberId, bundleName); - auto it = timerCache_.find(key); - if (it != timerCache_.end()) { - // has current timer, reset time - std::time_t now = time(nullptr); - if (now > reminderTime) { - ZLOGE("scheduler time fail %{private}s %{private}s", key.uri.c_str(), bundleName.c_str()); - return; - } - scheduler_->Reset(it->second, std::chrono::seconds(reminderTime - now)); - return; - } - // not find task in map, create new timer - std::time_t now = time(nullptr); - if (now > reminderTime) { - ZLOGE("scheduler time fail %{private}s %{private}s", key.uri.c_str(), bundleName.c_str()); - return; - } - auto taskId = scheduler_->At(TaskScheduler::Clock::now() + std::chrono::seconds(reminderTime - now), [&]() { - // 1. execute schedulerSQL in next time - Execute(key, rdbContext.dir, rdbContext.version); - // 2. notify - RdbSubscriberManager::GetInstance().EmitByKey(key, rdbContext.dir, rdbContext.version); - }); - if (taskId == TaskScheduler::INVALID_TASK_ID) { - ZLOGE("create timer failed, over the max capacity"); - return; - } - timerCache_.emplace(key, taskId); -} - -void SchedulerManager::ExecuteSchedulerSQL(const Key &key, std::shared_ptr delegate) -{ - Template tpl; - if (!TemplateManager::GetInstance().GetTemplate(key.uri, key.subscriberId, key.bundleName, tpl)) { - ZLOGE("template undefined, %{public}s, %{public}" PRId64 ", %{public}s", - DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); - return; - } - if (tpl.scheduler_.empty()) { - ZLOGW("template scheduler_ empty, %{public}s, %{public}" PRId64 ", %{public}s", - DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); - return; - } - - if (!GenRemindTimerFuncParams(key.uri, key.subscriberId, key.bundleName, tpl.scheduler_)) { - return; - } - int errCode = delegate->ExecuteSql(tpl.scheduler_); - if (errCode != E_OK) { - ZLOGE("Execute schedulerSql failed, %{public}s, %{public}" PRId64 ", %{public}s", - DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); - } -} - -bool SchedulerManager::GenRemindTimerFuncParams(const std::string &uri, int64_t subscriberId, - const std::string &bundleName, std::string &schedulerSQL) -{ - auto index = schedulerSQL.find("remindTimer("); - if (index == -1) { - ZLOGE("not find remindTimer, sql is %{public}s", schedulerSQL.c_str()); - return false; - } - index += strlen("remindTimer("); - std::string keyStr = "\"" + uri + "\", " + std::to_string(subscriberId) + ", \"" + bundleName + "\", "; - schedulerSQL.insert(index, keyStr); - return true; -} - -void SchedulerManager::RemoveTimer(const Key &key) -{ - std::lock_guard lock(mutex_); - if (scheduler_ == nullptr) { - ZLOGD("scheduler_ is nullptr"); - return; - } - auto it = timerCache_.find(key); - if (it != timerCache_.end()) { - ZLOGD("RemoveTimer %{public}s %{public}s %{public}" PRId64, - DistributedData::Anonymous::Change(key.uri).c_str(), key.bundleName.c_str(), key.subscriberId); - scheduler_->Remove(it->second); - timerCache_.erase(key); - if (timerCache_.empty()) { - scheduler_ = nullptr; - } - } -} -} // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.h b/services/distributeddataservice/service/data_share/common/scheduler_manager.h deleted file mode 100644 index 813f4557..00000000 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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 SCHEDULER_MANAGER_H -#define SCHEDULER_MANAGER_H - -#include - -#include "db_delegate.h" -#include "task_scheduler.h" -#include "template_manager.h" - -namespace OHOS::DataShare { -class SchedulerManager { -public: - static SchedulerManager &GetInstance(); - void Execute(const std::string &uri, const std::string &rdbDir, int version); - void Execute(const Key &key, const std::string &rdbDir, int version); - void SetTimer(const RdbStoreContext &rdbContext, const std::string &uri, int64_t subscriberId, - const std::string &bundleName, int64_t reminderTime); - void RemoveTimer(const Key &key); - -private: - static constexpr size_t TIME_TASK_NUM = 10; - SchedulerManager() = default; - ~SchedulerManager() = default; - bool GenRemindTimerFuncParams( - const std::string &uri, int64_t subscriberId, const std::string &bundleName, std::string &schedulerSQL); - void ExecuteSchedulerSQL(const Key &key, std::shared_ptr delegate = nullptr); - - std::recursive_mutex mutex_; - std::map timerCache_; - std::shared_ptr scheduler_; -}; -} // namespace OHOS::DataShare -#endif // SCHEDULER_MANAGER_H diff --git a/services/distributeddataservice/service/data_share/common/template_manager.cpp b/services/distributeddataservice/service/data_share/common/template_manager.cpp deleted file mode 100644 index ba66003c..00000000 --- a/services/distributeddataservice/service/data_share/common/template_manager.cpp +++ /dev/null @@ -1,516 +0,0 @@ -/* - * 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 "TemplateManager" - -#include "template_manager.h" - -#include "db_delegate.h" -#include "json_formatter.h" -#include "log_print.h" -#include "published_data.h" -#include "scheduler_manager.h" -#include "template_data.h" -#include "uri_utils.h" -#include "utils/anonymous.h" - -namespace OHOS::DataShare { -bool TemplateManager::GetTemplate( - const std::string &uri, const int64_t subscriberId, const std::string &bundleName, Template &tpl) -{ - auto delegate = KvDBDelegate::GetInstance(); - if (delegate == nullptr) { - ZLOGE("db open failed"); - return false; - } - TemplateData data(uri, bundleName, subscriberId, tpl); - std::string result; - bool find = delegate->Get(KvDBDelegate::TEMPLATE_TABLE, *data.GetId(), result); - if (!find) { - ZLOGE("db Get failed, %{public}s, %{public}" PRId64, bundleName.c_str(), subscriberId); - return false; - } - if (!DistributedData::Serializable::Unmarshall(result, data.value)) { - ZLOGE("Unmarshall failed, %{public}s, %{public}" PRId64, bundleName.c_str(), subscriberId); - return false; - } - tpl = data.value.tpl.tpl; - return true; -} - -bool TemplateManager::AddTemplate(const std::string &uri, const TemplateId &tplId, const Template &tpl) -{ - auto delegate = KvDBDelegate::GetInstance(); - if (delegate == nullptr) { - ZLOGE("db open failed"); - return false; - } - TemplateData data(uri, tplId.bundleName_, tplId.subscriberId_, tpl); - auto status = delegate->Upsert(KvDBDelegate::TEMPLATE_TABLE, data); - if (status != E_OK) { - ZLOGE("db Upsert failed, %{public}d", status); - return false; - } - return true; -} - -bool TemplateManager::DelTemplate(const std::string &uri, const TemplateId &tplId) -{ - auto delegate = KvDBDelegate::GetInstance(); - if (delegate == nullptr) { - ZLOGE("db open failed"); - return false; - } - Template tpl; - TemplateData data(uri, tplId.bundleName_, tplId.subscriberId_, tpl); - auto status = delegate->DeleteById(KvDBDelegate::TEMPLATE_TABLE, *data.GetId()); - if (status != E_OK) { - ZLOGE("db DeleteById failed, %{public}d", status); - return false; - } - SchedulerManager::GetInstance().RemoveTimer(Key(uri, tplId.subscriberId_, tplId.bundleName_)); - return true; -} - -Key::Key(const std::string &uri, const int64_t subscriberId, const std::string &bundleName) - : uri(uri), subscriberId(subscriberId), bundleName(bundleName) -{ -} - -bool Key::operator==(const Key &rhs) const -{ - return uri == rhs.uri && subscriberId == rhs.subscriberId && bundleName == rhs.bundleName; -} - -bool Key::operator!=(const Key &rhs) const -{ - return !(rhs == *this); -} -bool Key::operator<(const Key &rhs) const -{ - if (uri < rhs.uri) { - return true; - } - if (rhs.uri < uri) { - return false; - } - if (subscriberId < rhs.subscriberId) { - return true; - } - if (rhs.subscriberId < subscriberId) { - return false; - } - return bundleName < rhs.bundleName; -} -bool Key::operator>(const Key &rhs) const -{ - return rhs < *this; -} -bool Key::operator<=(const Key &rhs) const -{ - return !(rhs < *this); -} -bool Key::operator>=(const Key &rhs) const -{ - return !(*this < rhs); -} - -TemplateManager::TemplateManager() {} - -TemplateManager &TemplateManager::GetInstance() -{ - static TemplateManager manager; - return manager; -} - -RdbSubscriberManager &RdbSubscriberManager::GetInstance() -{ - static RdbSubscriberManager manager; - return manager; -} - -int RdbSubscriberManager::AddRdbSubscriber(const std::string &uri, const TemplateId &tplId, - const sptr observer, std::shared_ptr context) -{ - int result = E_OK; - Key key(uri, tplId.subscriberId_, tplId.bundleName_); - rdbCache_.Compute(key, [&observer, &context, &result, this](const auto &key, std::vector &value) { - ZLOGI("add subscriber, uri %{private}s tokenId %{public}d", key.uri.c_str(), context->callerTokenId); - std::vector node; - node.emplace_back(observer, context->callerTokenId); - result = Notify(key, node, context->calledSourceDir, context->version); - if (result != E_OK) { - return false; - } - value.emplace_back(observer, context->callerTokenId); - if (GetEnableObserverCount(key) == 1) { - SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); - } - return true; - }); - return result; -} - -int RdbSubscriberManager::DelRdbSubscriber( - const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId) -{ - Key key(uri, tplId.subscriberId_, tplId.bundleName_); - auto result = - rdbCache_.ComputeIfPresent(key, [&callerTokenId, this](const auto &key, std::vector &value) { - ZLOGI("delete subscriber, uri %{private}s tokenId %{public}d", key.uri.c_str(), callerTokenId); - for (auto it = value.begin(); it != value.end();) { - if (it->callerTokenId == callerTokenId) { - ZLOGI("erase start"); - it = value.erase(it); - } else { - it++; - } - } - if (GetEnableObserverCount(key) == 0) { - SchedulerManager::GetInstance().RemoveTimer(key); - } - return !value.empty(); - }); - return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; -} - -int RdbSubscriberManager::DisableRdbSubscriber( - const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId) -{ - Key key(uri, tplId.subscriberId_, tplId.bundleName_); - auto result = - rdbCache_.ComputeIfPresent(key, [&callerTokenId, this](const auto &key, std::vector &value) { - for (auto it = value.begin(); it != value.end(); it++) { - if (it->callerTokenId == callerTokenId) { - it->enabled = false; - } - } - if (GetEnableObserverCount(key) == 0) { - SchedulerManager::GetInstance().RemoveTimer(key); - } - return true; - }); - return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; -} - -int RdbSubscriberManager::EnableRdbSubscriber(const std::string &uri, const TemplateId &tplId, - std::shared_ptr context) -{ - Key key(uri, tplId.subscriberId_, tplId.bundleName_); - auto result = rdbCache_.ComputeIfPresent(key, [&context, this](const auto &key, std::vector &value) { - for (auto it = value.begin(); it != value.end(); it++) { - if (it->callerTokenId == context->callerTokenId) { - it->enabled = true; - std::vector node; - node.emplace_back(it->observer, context->callerTokenId); - Notify(key, node, context->calledSourceDir, context->version); - if (GetEnableObserverCount(key) == 1) { - SchedulerManager::GetInstance().Execute(key, context->calledSourceDir, context->version); - } - } - } - return true; - }); - return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; -} - -void RdbSubscriberManager::Emit(const std::string &uri, std::shared_ptr context) -{ - if (!URIUtils::IsDataProxyURI(uri)) { - return; - } - rdbCache_.ForEach([&uri, &context, this](const Key &key, std::vector &val) { - if (key.uri != uri) { - return false; - } - Notify(key, val, context->calledSourceDir, context->version); - return false; - }); -} - -std::vector RdbSubscriberManager::GetKeysByUri(const std::string &uri) -{ - std::vector results; - rdbCache_.ForEach([&uri, &results](const Key &key, std::vector &val) { - if (key.uri != uri) { - return false; - } - results.emplace_back(key); - return false; - }); - return results; -} - -void RdbSubscriberManager::EmitByKey(const Key &key, const std::string &rdbPath, int version) -{ - if (!URIUtils::IsDataProxyURI(key.uri)) { - return; - } - rdbCache_.ComputeIfPresent(key, [&rdbPath, &version, this](const Key &key, std::vector &val) { - Notify(key, val, rdbPath, version); - return true; - }); -} - -int RdbSubscriberManager::GetObserverCount(const Key &key) -{ - auto pair = rdbCache_.Find(key); - if (!pair.first) { - return 0; - } - return pair.second.size(); -} - -int RdbSubscriberManager::GetEnableObserverCount(const Key &key) -{ - auto pair = rdbCache_.Find(key); - if (!pair.first) { - return 0; - } - int count = 0; - for (const auto &observer : pair.second) { - if (observer.enabled) { - count++; - } - } - return count; -} - -int RdbSubscriberManager::Notify( - const Key &key, std::vector &val, const std::string &rdbDir, int rdbVersion) -{ - Template tpl; - if (!TemplateManager::GetInstance().GetTemplate(key.uri, key.subscriberId, key.bundleName, tpl)) { - ZLOGE("template undefined, %{public}s, %{public}" PRId64 ", %{public}s", - DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); - return E_TEMPLATE_NOT_EXIST; - } - int errCode; - auto delegate = DBDelegate::Create(rdbDir, rdbVersion, errCode); - if (delegate == nullptr) { - ZLOGE("malloc fail %{public}s %{public}s", DistributedData::Anonymous::Change(key.uri).c_str(), - key.bundleName.c_str()); - return errCode; - } - RdbChangeNode changeNode; - changeNode.uri_ = key.uri; - changeNode.templateId_.subscriberId_ = key.subscriberId; - changeNode.templateId_.bundleName_ = key.bundleName; - for (const auto &predicate : tpl.predicates_) { - JsonFormatter formatter(predicate.key_, delegate->Query(predicate.selectSql_)); - changeNode.data_.emplace_back(DistributedData::Serializable::Marshall(formatter)); - } - - ZLOGI("emit, size %{public}d %{private}s", val.size(), changeNode.uri_.c_str()); - for (auto &callback : val) { - if (callback.enabled && callback.observer != nullptr) { - callback.observer->OnChangeFromRdb(changeNode); - } - } - return E_OK; -} - -PublishedDataSubscriberManager &PublishedDataSubscriberManager::GetInstance() -{ - static PublishedDataSubscriberManager manager; - return manager; -} - -int PublishedDataSubscriberManager::AddSubscriber(const std::string &key, const std::string &callerBundleName, - const int64_t subscriberId, const sptr observer, const uint32_t callerTokenId) -{ - PublishedDataKey publishedDataKey(key, callerBundleName, subscriberId); - publishedDataCache.Compute(publishedDataKey, - [&observer, &callerTokenId, this](const PublishedDataKey &key, std::vector &value) { - ZLOGI("add publish subscriber, uri %{private}s tokenId %{public}d", key.key.c_str(), callerTokenId); - value.emplace_back(observer, callerTokenId); - return true; - }); - return E_OK; -} - -int PublishedDataSubscriberManager::DelSubscriber(const std::string &uri, const std::string &callerBundleName, - const int64_t subscriberId, const uint32_t callerTokenId) -{ - PublishedDataKey key(uri, callerBundleName, subscriberId); - auto result = - publishedDataCache.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { - for (auto it = value.begin(); it != value.end();) { - if (it->callerTokenId == callerTokenId) { - it = value.erase(it); - } else { - it++; - } - } - return !value.empty(); - }); - return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; -} - -int PublishedDataSubscriberManager::DisableSubscriber(const std::string &uri, const std::string &callerBundleName, - const int64_t subscriberId, const uint32_t callerTokenId) -{ - PublishedDataKey key(uri, callerBundleName, subscriberId); - auto result = - publishedDataCache.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { - for (auto it = value.begin(); it != value.end(); it++) { - if (it->callerTokenId == callerTokenId) { - it->enabled = false; - } - } - return true; - }); - return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; -} - -int PublishedDataSubscriberManager::EnableSubscriber(const std::string &uri, const std::string &callerBundleName, - const int64_t subscriberId, const uint32_t callerTokenId) -{ - PublishedDataKey key(uri, callerBundleName, subscriberId); - auto result = - publishedDataCache.ComputeIfPresent(key, [&callerTokenId](const auto &key, std::vector &value) { - for (auto it = value.begin(); it != value.end(); it++) { - if (it->callerTokenId == callerTokenId) { - it->enabled = true; - } - } - return true; - }); - return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; -} - -void PublishedDataSubscriberManager::Emit(const std::vector &keys, - const std::string &ownerBundleName, const sptr observer) -{ - int32_t status; - // key is bundleName, value is change node - std::map publishedResult; - std::map, std::vector> callbacks; - publishedDataCache.ForEach([&keys, &status, &observer, &publishedResult, &callbacks, this]( - const PublishedDataKey &key, std::vector &val) { - for (auto &data : keys) { - if (key != data) { - continue; - } - publishedResult[key].subscriberId_ = data.subscriberId; - publishedResult[key].key_ = data.key; - PublishedData publishedData(data.key, data.bundleName, data.subscriberId); - status = PublishedData::Query( - DistributedData::Serializable::Marshall(*publishedData.GetId()), publishedResult[key].value_); - if (status != E_OK) { - ZLOGE("query fail %{public}s %{public}s %{public}" PRId64, data.bundleName.c_str(), data.key.c_str(), - data.subscriberId); - publishedResult.erase(key); - continue; - } - PutInto(callbacks, val, key, observer); - break; - } - return false; - }); - PublishedDataChangeNode result; - for (auto &[callback, keys] : callbacks) { - result.datas_.clear(); - for (auto &key : keys) { - if (publishedResult.count(key) != 0) { - result.datas_.emplace_back(publishedResult[key]); - } - } - if (result.datas_.empty()) { - continue; - } - result.ownerBundleName_ = ownerBundleName; - callback->OnChangeFromPublishedData(result); - } -} - -void PublishedDataSubscriberManager::PutInto( - std::map, std::vector> &callbacks, - std::vector &val, const PublishedDataKey &key, const sptr observer) -{ - for (auto &callback : val) { - if (callback.enabled && callback.observer != nullptr) { - // callback the observer, others do not call - if (observer != nullptr && callback.observer != observer) { - continue; - } - callbacks[callback.observer].emplace_back(key); - } - } -} - -PublishedDataKey::PublishedDataKey(const std::string &key, const std::string &bundle, const int64_t subscriberId) - : key(key), bundleName(bundle), subscriberId(subscriberId) -{ - /* private published data can use key as simple uri */ - /* etc: datashareproxy://{bundleName}/meeting can use meeting replaced */ - /* if key is normal uri, bundleName is from uri */ - if (URIUtils::IsDataProxyURI(key)) { - URIUtils::GetBundleNameFromProxyURI(key, bundleName); - } -} - -bool PublishedDataKey::operator<(const PublishedDataKey &rhs) const -{ - if (key < rhs.key) { - return true; - } - if (rhs.key < key) { - return false; - } - if (bundleName < rhs.bundleName) { - return true; - } - if (rhs.bundleName < bundleName) { - return false; - } - return subscriberId < rhs.subscriberId; -} - -bool PublishedDataKey::operator>(const PublishedDataKey &rhs) const -{ - return rhs < *this; -} - -bool PublishedDataKey::operator<=(const PublishedDataKey &rhs) const -{ - return !(rhs < *this); -} - -bool PublishedDataKey::operator>=(const PublishedDataKey &rhs) const -{ - return !(*this < rhs); -} - -bool PublishedDataKey::operator==(const PublishedDataKey &rhs) const -{ - return key == rhs.key && bundleName == rhs.bundleName && subscriberId == rhs.subscriberId; -} - -bool PublishedDataKey::operator!=(const PublishedDataKey &rhs) const -{ - return !(rhs == *this); -} - -RdbSubscriberManager::ObserverNode::ObserverNode(const sptr &observer, uint32_t callerTokenId) - : observer(observer), callerTokenId(callerTokenId) -{ -} - -PublishedDataSubscriberManager::ObserverNode::ObserverNode( - const sptr &observer, uint32_t callerTokenId) - : observer(observer), callerTokenId(callerTokenId) -{ -} -} // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/common/template_manager.h b/services/distributeddataservice/service/data_share/common/template_manager.h deleted file mode 100644 index f6b4e5a5..00000000 --- a/services/distributeddataservice/service/data_share/common/template_manager.h +++ /dev/null @@ -1,118 +0,0 @@ -/* - * 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 DATASHARESERVICE_TEMPLATE_MANAGER_H -#define DATASHARESERVICE_TEMPLATE_MANAGER_H - -#include -#include - -#include "concurrent_map.h" -#include "datashare_template.h" -#include "data_proxy_observer.h" -#include "context.h" -namespace OHOS::DataShare { -struct Key { - Key(const std::string &uri, const int64_t subscriberId, const std::string &bundleName); - bool operator==(const Key &rhs) const; - bool operator!=(const Key &rhs) const; - bool operator<(const Key &rhs) const; - bool operator>(const Key &rhs) const; - bool operator<=(const Key &rhs) const; - bool operator>=(const Key &rhs) const; - const std::string uri; - const int64_t subscriberId; - const std::string bundleName; -}; -class TemplateManager { -public: - static TemplateManager &GetInstance(); - bool AddTemplate(const std::string &uri, const TemplateId &tplId, const Template &tpl); - bool DelTemplate(const std::string &uri, const TemplateId &tplId); - bool GetTemplate(const std::string &uri, int64_t subscriberId, const std::string &bundleName, Template &tpl); - -private: - TemplateManager(); - friend class RdbSubscriberManager; -}; - -class RdbSubscriberManager { -public: - static RdbSubscriberManager &GetInstance(); - int AddRdbSubscriber(const std::string &uri, const TemplateId &tplId, const sptr observer, - std::shared_ptr context); - int DelRdbSubscriber(const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId); - int DisableRdbSubscriber( - const std::string &uri, const TemplateId &tplId, const uint32_t callerTokenId); - int EnableRdbSubscriber(const std::string &uri, const TemplateId &tplId, std::shared_ptr context); - void Emit(const std::string &uri, std::shared_ptr context); - void EmitByKey(const Key &key, const std::string &rdbPath, int version); - int GetObserverCount(const Key &key); - std::vector GetKeysByUri(const std::string &uri); - -private: - struct ObserverNode { - ObserverNode(const sptr &observer, uint32_t callerTokenId); - sptr observer; - uint32_t callerTokenId; - bool enabled = true; - }; - - RdbSubscriberManager() = default; - ConcurrentMap> rdbCache_; - int Notify(const Key &key, std::vector &val, const std::string &rdbDir, int rdbVersion); - int GetEnableObserverCount(const Key &key); -}; - -struct PublishedDataKey { - PublishedDataKey(const std::string &key, const std::string &bundleName, const int64_t subscriberId); - bool operator<(const PublishedDataKey &rhs) const; - bool operator>(const PublishedDataKey &rhs) const; - bool operator<=(const PublishedDataKey &rhs) const; - bool operator>=(const PublishedDataKey &rhs) const; - bool operator==(const PublishedDataKey &rhs) const; - bool operator!=(const PublishedDataKey &rhs) const; - std::string key; - std::string bundleName; - int64_t subscriberId; -}; - -class PublishedDataSubscriberManager { -public: - static PublishedDataSubscriberManager &GetInstance(); - int AddSubscriber(const std::string &key, const std::string &callerBundleName, const int64_t subscriberId, - const sptr observer, const uint32_t callerTokenId); - int DelSubscriber(const std::string &uri, const std::string &callerBundleName, const int64_t subscriberId, - const uint32_t callerTokenId); - int DisableSubscriber(const std::string &uri, const std::string &callerBundleName, const int64_t subscriberId, - const uint32_t callerTokenId); - int EnableSubscriber(const std::string &uri, const std::string &callerBundleName, const int64_t subscriberId, - const uint32_t callerTokenId); - void Emit(const std::vector &keys, const std::string &ownerBundleName, - const sptr observer = nullptr); -private: - struct ObserverNode { - ObserverNode(const sptr &observer, uint32_t callerTokenId); - sptr observer; - uint32_t callerTokenId; - bool enabled = true; - }; - PublishedDataSubscriberManager() = default; - void PutInto(std::map, std::vector> &, - std::vector &, const PublishedDataKey &, const sptr); - ConcurrentMap> publishedDataCache; -}; -} // namespace OHOS::DataShare -#endif diff --git a/services/distributeddataservice/service/data_share/data/json_formatter.cpp b/services/distributeddataservice/service/data_share/data/json_formatter.cpp deleted file mode 100644 index db190824..00000000 --- a/services/distributeddataservice/service/data_share/data/json_formatter.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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 "JsonFormatter" -#include "json_formatter.h" - -#include "log_print.h" - -namespace OHOS::DataShare { -bool JsonFormatter::Marshal(json &node) const -{ - if (value_ == nullptr) { - ZLOGE("null value %{public}s", key_.c_str()); - return false; - } - return SetValue(node[key_], *value_); -} - -bool JsonFormatter::Unmarshal(const json &node) -{ - if (value_ == nullptr) { - ZLOGE("null value %{public}s", key_.c_str()); - return false; - } - return GetValue(node, key_, *value_); -} -} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/json_formatter.h b/services/distributeddataservice/service/data_share/data/json_formatter.h deleted file mode 100644 index 7c41e197..00000000 --- a/services/distributeddataservice/service/data_share/data/json_formatter.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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 DATASHARESERVICE_JSON_FORMATTER_H -#define DATASHARESERVICE_JSON_FORMATTER_H - -#include "serializable/serializable.h" - -namespace OHOS::DataShare { -class JsonFormatter : public DistributedData::Serializable { -public: - JsonFormatter(const std::string &key, const std::shared_ptr &value) - : key_(key), value_(value) - { - } - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; - -private: - std::string key_; - std::shared_ptr value_; -}; -} // namespace OHOS::DataShare -#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/data/published_data.cpp b/services/distributeddataservice/service/data_share/data/published_data.cpp deleted file mode 100644 index ed0e1a82..00000000 --- a/services/distributeddataservice/service/data_share/data/published_data.cpp +++ /dev/null @@ -1,169 +0,0 @@ -/* - * 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 "PublishedData" -#include "published_data.h" - -#include "log_print.h" - -namespace OHOS::DataShare { -bool PublishedData::HasVersion() const -{ - return true; -} - -VersionData PublishedData::GetVersion() const -{ - return value.version; -} - -const DistributedData::Serializable &PublishedData::GetValue() const -{ - return value; -} - -bool PublishedData::Unmarshal(const DistributedData::Serializable::json &node) -{ - return value.Unmarshal(node); -} - -PublishedData::PublishedData(const std::string &key, const std::string &bundleName, int64_t subscriberId, - const std::variant, std::string> &inputValue, const int version) - : value(key, bundleName, subscriberId, inputValue, version) -{ -} - -std::vector PublishedData::Query(const std::string &bundleName) -{ - auto delegate = KvDBDelegate::GetInstance(); - if (delegate == nullptr) { - ZLOGE("db open failed"); - return std::vector(); - } - std::vector queryResults; - json filter; - filter["bundleName"] = bundleName; - int32_t status = delegate->GetBatch(KvDBDelegate::DATA_TABLE, filter.dump(), "{}", queryResults); - if (status != E_OK) { - ZLOGE("db Upsert failed, %{public}s %{public}d", bundleName.c_str(), status); - return std::vector(); - } - std::vector results; - for (auto &result : queryResults) { - PublishedData data; - if (data.Unmarshall(result)) { - results.push_back(std::move(data)); - } - } - return results; -} - -bool PublishedDataNode::Marshal(DistributedData::Serializable::json &node) const -{ - bool ret = SetValue(node["key"], key); - ret = ret && SetValue(node["bundleName"], bundleName); - ret = ret && SetValue(node["subscriberId"], subscriberId); - ret = ret && SetValue(node, version); - if (value.index() == 1) { - std::string valueStr = std::get(value); - ret = ret && SetValue(node["type"], PublishedData::STRING); - ret = ret && SetValue(node["value"], valueStr); - } else { - sptr ashmem = std::get>(value); - if (ashmem == nullptr) { - ZLOGE("get ashmem null"); - return false; - } - const uint8_t *data = static_cast(ashmem->ReadFromAshmem(ashmem->GetAshmemSize(), 0)); - if (data == nullptr) { - ZLOGE("ReadFromAshmem null"); - return false; - } - ret = ret && SetValue(node["type"], PublishedData::ASHMEM); - node["value"] = std::vector(data, data + ashmem->GetAshmemSize()); - } - std::time_t now = time(nullptr); - if (now <= 0) { - ZLOGE("time error"); - return false; - } - ret = ret && SetValue(node["timestamp"], now); - return ret; -} - -bool PublishedDataNode::Unmarshal(const DistributedData::Serializable::json &node) -{ - bool ret = GetValue(node, "key", key); - ret = ret && GetValue(node, "bundleName", bundleName); - ret = ret && GetValue(node, "subscriberId", subscriberId); - ret = ret && version.Unmarshal(node); - int32_t type = 0; - ret = ret && GetValue(node, "type", type); - if (!ret) { - ZLOGE("Unmarshal PublishedDataNode failed, %{private}s", key.c_str()); - return false; - } - if (type == PublishedData::STRING) { - std::string strValue; - ret = ret && GetValue(node, "value", strValue); - value = strValue; - } else if (type == PublishedData::ASHMEM) { - std::vector binaryData = node["value"]; - std::string ashmemName = "PublishedData" + key + "_" + bundleName + "_" + std::to_string(subscriberId); - auto ashmem = Ashmem::CreateAshmem(ashmemName.c_str(), binaryData.size()); - if (ashmem == nullptr) { - ZLOGE("SharedBlock: CreateAshmem function error."); - return false; - } - - ret = ashmem->MapReadAndWriteAshmem(); - if (!ret) { - ZLOGE("SharedBlock: MapReadAndWriteAshmem function error."); - ashmem->CloseAshmem(); - return false; - } - ashmem->WriteToAshmem(&binaryData[0], binaryData.size(), 0); - value = ashmem; - } - return ret; -} - -PublishedDataNode::PublishedDataNode(const std::string &key, const std::string &bundleName, int64_t subscriberId, - const std::variant, std::string> &value, const int version) - : key(key), bundleName(bundleName), subscriberId(subscriberId), value(value), version(version) -{ -} - -int32_t PublishedData::Query(const std::string &filter, std::variant, std::string> &publishedData) -{ - auto delegate = KvDBDelegate::GetInstance(); - if (delegate == nullptr) { - ZLOGE("db open failed"); - return E_ERROR; - } - std::string queryResult; - int32_t status = delegate->Get(KvDBDelegate::DATA_TABLE, filter, "{}", queryResult); - if (status != E_OK) { - ZLOGE("db Get failed, %{public}s %{public}d", filter.c_str(), status); - return status; - } - PublishedData data; - if (!data.Unmarshall(queryResult)) { - ZLOGE("Unmarshall failed, %{private}s", queryResult.c_str()); - return E_ERROR; - } - publishedData = data.value.value; - return E_OK; -} -} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/published_data.h b/services/distributeddataservice/service/data_share/data/published_data.h deleted file mode 100644 index 4acd3ee8..00000000 --- a/services/distributeddataservice/service/data_share/data/published_data.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 DATASHARESERVICE_PUBLISHED_DATA_H -#define DATASHARESERVICE_PUBLISHED_DATA_H - -#include "db_delegate.h" -#include "serializable/serializable.h" - -namespace OHOS::DataShare { -struct PublishedDataNode final : public DistributedData::Serializable { - PublishedDataNode(const std::string &key, const std::string &bundleName, int64_t subscriberId, - const std::variant, std::string> &value, const int version); - ~PublishedDataNode() = default; - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; - std::string key; - std::string bundleName; - int64_t subscriberId; - std::variant, std::string> value; - VersionData version; -}; - -class PublishedData final : public KvData { -public: - static std::vector Query(const std::string &bundleName); - static int32_t Query(const std::string &filter, std::variant, std::string> &publishedData); - explicit PublishedData(const std::string &key = "", const std::string &bundleName = "", int64_t subscriberId = 0, - const std::variant, std::string> &value = "", const int version = 0); - ~PublishedData() = default; - std::shared_ptr GetId() const override - { - return std::make_shared(value.key + "_" + std::to_string(value.subscriberId) + "_" + value.bundleName); - } - bool HasVersion() const override; - VersionData GetVersion() const override; - const DistributedData::Serializable &GetValue() const override; - bool Unmarshal(const json &node) override; - static constexpr int8_t STRING = 0; - static constexpr int8_t ASHMEM = 1; - PublishedDataNode value; -}; -} // namespace OHOS::DataShare -#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp deleted file mode 100644 index 931a4d54..00000000 --- a/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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 "ResultSetJsonFormatter" -#include "resultset_json_formatter.h" - -#include "rdb_errno.h" -#include "log_print.h" - -namespace OHOS::DataShare { -bool ResultSetJsonFormatter::Marshal(json &node) const -{ - int columnCount = 0; - auto result = resultSet->GetColumnCount(columnCount); - if (result != NativeRdb::E_OK) { - ZLOGE("GetColumnCount err, %{public}d", result); - return false; - } - while (resultSet->GoToNextRow() == NativeRdb::E_OK) { - json result; - for (int i = 0; i < columnCount; i++) { - std::string columnName; - std::string value; - resultSet->GetColumnName(i, columnName); - resultSet->GetString(i, value); - SetValue(result[columnName], value); - } - node.push_back(result); - } - return true; -} - -bool ResultSetJsonFormatter::Unmarshal(const DistributedData::Serializable::json &node) -{ - return false; -} -} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h deleted file mode 100644 index 0dfcabdf..00000000 --- a/services/distributeddataservice/service/data_share/data/resultset_json_formatter.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 DATASHARESERVICE_RESULTSET_JSON_FORMATTER_H -#define DATASHARESERVICE_RESULTSET_JSON_FORMATTER_H - -#include "datashare_template.h" -#include "rdb_utils.h" -#include "serializable/serializable.h" - -namespace OHOS::DataShare { -class ResultSetJsonFormatter final : public DistributedData::Serializable { -public: - explicit ResultSetJsonFormatter(const std::shared_ptr &resultSet) : resultSet(resultSet) {} - ~ResultSetJsonFormatter() {} - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; - -private: - std::shared_ptr resultSet; -}; -} // namespace OHOS::DataShare -#endif // DATASHARESERVICE_RESULTSET_JSON_FORMATTER_H diff --git a/services/distributeddataservice/service/data_share/data/template_data.cpp b/services/distributeddataservice/service/data_share/data/template_data.cpp deleted file mode 100644 index fbaaffa7..00000000 --- a/services/distributeddataservice/service/data_share/data/template_data.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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 "TemplateData" -#include "template_data.h" - -namespace OHOS::DataShare { -bool TemplateNode::Marshal(DistributedData::Serializable::json &node) const -{ - bool ret; - for (auto &predicate : tpl.predicates_) { - ret = SetValue(node["predicates"][predicate.key_], predicate.selectSql_); - } - ret = ret && SetValue(node["scheduler"], tpl.scheduler_); - return ret; -} - -bool TemplateNode::Unmarshal(const DistributedData::Serializable::json &node) -{ - auto &predicatesNode = node["predicates"]; - for (auto it = predicatesNode.begin(); it != predicatesNode.end(); ++it) { - tpl.predicates_.emplace_back(static_cast(it.key()), static_cast(it.value())); - } - bool ret = GetValue(node, "scheduler", tpl.scheduler_); - return ret; -} - -TemplateNode::TemplateNode(const Template &tpl) : tpl(tpl) {} - -bool TemplateRootNode::Marshal(DistributedData::Serializable::json &node) const -{ - bool ret = SetValue(node["uri"], uri); - ret = ret && SetValue(node["bundleName"], bundleName); - ret = ret && SetValue(node["subscriberId"], subscriberId); - ret = ret && SetValue(node["template"], tpl); - return ret; -} - -bool TemplateRootNode::Unmarshal(const DistributedData::Serializable::json &node) -{ - bool ret = GetValue(node, "uri", uri); - ret = ret && GetValue(node, "bundleName", bundleName); - ret = ret && GetValue(node, "subscriberId", subscriberId); - ret = ret && GetValue(node, "template", tpl); - return ret; -} - -TemplateRootNode::TemplateRootNode( - const std::string &uri, const std::string &bundleName, int64_t subscriberId, const Template &tpl) - : uri(uri), bundleName(bundleName), subscriberId(subscriberId), tpl(tpl) -{ -} - -std::shared_ptr TemplateData::GetId() const -{ - return std::make_shared(value.uri + "_" + std::to_string(value.subscriberId) + "_" + value.bundleName); -} - -bool TemplateData::HasVersion() const -{ - return false; -} - -VersionData TemplateData::GetVersion() const -{ - return VersionData(0); -} - -const DistributedData::Serializable &TemplateData::GetValue() const -{ - return value; -} - -bool TemplateData::Unmarshal(const DistributedData::Serializable::json &node) -{ - return value.Unmarshal(node); -} - -TemplateData::TemplateData( - const std::string &uri, const std::string &bundleName, int64_t subscriberId, const Template &tpl) - : value(uri, bundleName, subscriberId, tpl) -{ -} -} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/data/template_data.h b/services/distributeddataservice/service/data_share/data/template_data.h deleted file mode 100644 index 4b194f37..00000000 --- a/services/distributeddataservice/service/data_share/data/template_data.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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 DATASHARESERVICE_TEMPLATE_DATA_H -#define DATASHARESERVICE_TEMPLATE_DATA_H - -#include "datashare_template.h" -#include "db_delegate.h" -#include "serializable/serializable.h" - -namespace OHOS::DataShare { -struct TemplateNode : public DistributedData::Serializable { - TemplateNode() = default; - explicit TemplateNode(const Template &tpl); - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; - Template tpl; -}; - -struct TemplateRootNode : public DistributedData::Serializable { - TemplateRootNode() = default; - TemplateRootNode(const std::string &uri, const std::string &bundleName, int64_t subscriberId, const Template &tpl); - bool Marshal(json &node) const override; - bool Unmarshal(const json &node) override; - std::string uri; - std::string bundleName; - int64_t subscriberId; - TemplateNode tpl; -}; - -struct TemplateData final : public KvData { - TemplateData() = default; - TemplateData(const std::string &uri, const std::string &bundleName, int64_t subscriberId, const Template &tpl); - std::shared_ptr GetId() const override; - bool HasVersion() const override; - VersionData GetVersion() const override; - const DistributedData::Serializable &GetValue() const override; - bool Unmarshal(const json &node) override; - TemplateRootNode value; -}; -} // namespace OHOS::DataShare -#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 7f24e6fe..6b3d45a6 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -32,9 +32,7 @@ #include "log_print.h" #include "publish_strategy.h" #include "query_strategy.h" -#include "scheduler_manager.h" #include "subscribe_strategy.h" -#include "template_manager.h" #include "update_strategy.h" #include "utils/anonymous.h" @@ -57,8 +55,6 @@ int32_t DataShareServiceImpl::Insert(const std::string &uri, const DataShareValu auto ret = InsertStrategy::Execute(context, valuesBucket); if (ret) { NotifyChange(uri); - RdbSubscriberManager::GetInstance().Emit(uri, context); - SchedulerManager::GetInstance().Execute(uri, context->calledSourceDir, context->version); return ret; } return ret; @@ -88,8 +84,6 @@ int32_t DataShareServiceImpl::Update(const std::string &uri, const DataSharePred auto ret = UpdateStrategy::Execute(context, predicate, valuesBucket); if (ret) { NotifyChange(uri); - RdbSubscriberManager::GetInstance().Emit(uri, context); - SchedulerManager::GetInstance().Execute(uri, context->calledSourceDir, context->version); return ret; } return ret; @@ -102,8 +96,6 @@ int32_t DataShareServiceImpl::Delete(const std::string &uri, const DataSharePred auto ret = DeleteStrategy::Execute(context, predicate); if (ret) { NotifyChange(uri); - RdbSubscriberManager::GetInstance().Emit(uri, context); - SchedulerManager::GetInstance().Execute(uri, context->calledSourceDir, context->version); return ret; } return ret; @@ -125,7 +117,7 @@ int32_t DataShareServiceImpl::AddTemplate(const std::string &uri, const int64_t ZLOGE("get bundleName error, %{public}s", DistributedData::Anonymous::Change(uri).c_str()); return ERROR; } - return TemplateManager::GetInstance().AddTemplate(uri, tpltId, tplt); + return ERROR; } int32_t DataShareServiceImpl::DelTemplate(const std::string &uri, const int64_t subscriberId) @@ -136,7 +128,7 @@ int32_t DataShareServiceImpl::DelTemplate(const std::string &uri, const int64_t ZLOGE("get bundleName error, %{public}s", DistributedData::Anonymous::Change(uri).c_str()); return ERROR; } - return TemplateManager::GetInstance().DelTemplate(uri, tpltId); + return ERROR; } bool DataShareServiceImpl::GetCallerBundleName(std::string &bundleName) @@ -157,48 +149,18 @@ bool DataShareServiceImpl::GetCallerBundleName(std::string &bundleName) std::vector DataShareServiceImpl::Publish(const Data &data, const std::string &bundleNameOfProvider) { - std::vector results; - std::vector publishedData; - std::string callerBundleName; - GetCallerBundleName(callerBundleName); - for (const auto &item : data.datas_) { - auto context = std::make_shared(item.key_); - context->version = data.version_; - context->callerBundleName = callerBundleName; - context->calledBundleName = bundleNameOfProvider; - int32_t result = PublishStrategy::Execute(context, item); - results.emplace_back(item.key_, result); - if (result != EOK) { - ZLOGE("publish error, key is %{public}s", DistributedData::Anonymous::Change(item.key_).c_str()); - continue; - } - publishedData.emplace_back(context->uri, callerBundleName, item.subscriberId_); - } - PublishedDataSubscriberManager::GetInstance().Emit(publishedData, callerBundleName); - return results; + return std::vector(); } Data DataShareServiceImpl::GetData(const std::string &bundleNameOfProvider) { - std::string callerBundleName; - GetCallerBundleName(callerBundleName); - auto context = std::make_shared(); - context->callerBundleName = callerBundleName; - context->calledBundleName = bundleNameOfProvider; - return GetDataStrategy::Execute(context); + return Data(); } std::vector DataShareServiceImpl::SubscribeRdbData( const std::vector &uris, const TemplateId &id, const sptr observer) { - std::vector results; - for (const auto &uri : uris) { - auto context = std::make_shared(uri); - results.emplace_back(uri, SubscribeStrategy::Execute(context, [&id, &observer, &context]() -> bool { - return RdbSubscriberManager::GetInstance().AddRdbSubscriber(context->uri, id, observer, context); - })); - } - return results; + return std::vector(); } std::vector DataShareServiceImpl::UnSubscribeRdbData( @@ -208,7 +170,7 @@ std::vector DataShareServiceImpl::UnSubscribeRdbData( for (const auto &uri : uris) { auto context = std::make_shared(uri); results.emplace_back(uri, SubscribeStrategy::Execute(context, [&id, &context]() -> bool { - return RdbSubscriberManager::GetInstance().DelRdbSubscriber(context->uri, id, context->callerTokenId); + return true; })); } return results; @@ -221,7 +183,7 @@ std::vector DataShareServiceImpl::EnableSubscribeRdbData( for (const auto &uri : uris) { auto context = std::make_shared(uri); results.emplace_back(uri, SubscribeStrategy::Execute(context, [&id, &context]() -> bool { - return RdbSubscriberManager::GetInstance().EnableRdbSubscriber(context->uri, id, context); + return true; })); } return results; @@ -234,7 +196,7 @@ std::vector DataShareServiceImpl::DisableSubscribeRdbData( for (const auto &uri : uris) { auto context = std::make_shared(uri); results.emplace_back(uri, SubscribeStrategy::Execute(context, [&id, &context]() -> bool { - return RdbSubscriberManager::GetInstance().DisableRdbSubscriber(context->uri, id, context->callerTokenId); + return true; })); } return results; @@ -243,94 +205,25 @@ std::vector DataShareServiceImpl::DisableSubscribeRdbData( std::vector DataShareServiceImpl::SubscribePublishedData(const std::vector &uris, const int64_t subscriberId, const sptr observer) { - std::vector results; - std::string callerBundleName; - GetCallerBundleName(callerBundleName); - std::vector publishedKeys; - int32_t result; - for (const auto &uri : uris) { - auto context = std::make_shared(uri); - PublishedDataKey key(uri, callerBundleName, subscriberId); - context->callerBundleName = callerBundleName; - context->calledBundleName = key.bundleName; - result = SubscribeStrategy::Execute( - context, [&subscriberId, &observer, &callerBundleName, &context]() -> bool { - return PublishedDataSubscriberManager::GetInstance().AddSubscriber( - context->uri, callerBundleName, subscriberId, observer, context->callerTokenId); - }); - results.emplace_back(uri, result); - if (result == E_OK) { - publishedKeys.emplace_back(key); - } - } - PublishedDataSubscriberManager::GetInstance().Emit(publishedKeys, callerBundleName, observer); - return results; + return std::vector(); } std::vector DataShareServiceImpl::UnSubscribePublishedData(const std::vector &uris, const int64_t subscriberId) { - std::vector results; - std::string callerBundleName; - GetCallerBundleName(callerBundleName); - for (const auto &uri : uris) { - auto context = std::make_shared(uri); - PublishedDataKey key(uri, callerBundleName, subscriberId); - context->callerBundleName = callerBundleName; - context->calledBundleName = key.bundleName; - results.emplace_back( - uri, SubscribeStrategy::Execute(context, [&subscriberId, &callerBundleName, &context]() -> bool { - return PublishedDataSubscriberManager::GetInstance().DelSubscriber( - context->uri, callerBundleName, subscriberId, context->callerTokenId); - })); - } - return results; + return std::vector(); } std::vector DataShareServiceImpl::EnableSubscribePublishedData(const std::vector &uris, const int64_t subscriberId) { - std::vector results; - std::string callerBundleName; - GetCallerBundleName(callerBundleName); - std::vector publishedKeys; - int32_t result; - for (const auto &uri : uris) { - auto context = std::make_shared(uri); - PublishedDataKey key(uri, callerBundleName, subscriberId); - context->callerBundleName = callerBundleName; - context->calledBundleName = key.bundleName; - result = SubscribeStrategy::Execute(context, [&subscriberId, &callerBundleName, &context]() -> bool { - return PublishedDataSubscriberManager::GetInstance().EnableSubscriber( - context->uri, callerBundleName, subscriberId, context->callerTokenId); - }); - results.emplace_back(uri, result); - if (result == E_OK) { - publishedKeys.emplace_back(key); - } - } - PublishedDataSubscriberManager::GetInstance().Emit(publishedKeys, callerBundleName); - return results; + return std::vector(); } std::vector DataShareServiceImpl::DisableSubscribePublishedData(const std::vector &uris, const int64_t subscriberId) { - std::vector results; - std::string callerBundleName; - GetCallerBundleName(callerBundleName); - for (const auto &uri : uris) { - auto context = std::make_shared(uri); - PublishedDataKey key(uri, callerBundleName, subscriberId); - context->callerBundleName = callerBundleName; - context->calledBundleName = key.bundleName; - results.emplace_back( - uri, SubscribeStrategy::Execute(context, [&subscriberId, &callerBundleName, &context]() -> bool { - return PublishedDataSubscriberManager::GetInstance().DisableSubscriber( - context->uri, callerBundleName, subscriberId, context->callerTokenId); - })); - } - return results; + return std::vector(); } enum DataShareKvStoreType : int32_t { diff --git a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp index 56f91b53..2e29828d 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp @@ -111,13 +111,11 @@ int32_t DataShareServiceStub::OnRemoteAddTemplate(MessageParcel &data, MessagePa { std::string uri; int64_t subscriberId; - std::vector predicates; - std::string scheduler; - if (!ITypesUtil::Unmarshal(data, uri, subscriberId, predicates, scheduler)) { + Template tpl; + if (!ITypesUtil::Unmarshal(data, uri, subscriberId, tpl.predicates_, tpl.scheduler_)) { ZLOGW("read device list failed."); return -1; } - Template tpl(predicates, scheduler); int32_t status = AddTemplate(uri, subscriberId, tpl); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x", status); diff --git a/services/distributeddataservice/service/data_share/data_share_types_util.cpp b/services/distributeddataservice/service/data_share/data_share_types_util.cpp index 847fb8d1..ec255ec2 100644 --- a/services/distributeddataservice/service/data_share/data_share_types_util.cpp +++ b/services/distributeddataservice/service/data_share/data_share_types_util.cpp @@ -49,70 +49,19 @@ bool Unmarshalling(Operation &operation, MessageParcel &parcel) template<> bool Unmarshalling(PublishedDataItem &dataItem, MessageParcel &parcel) { - dataItem.key_ = parcel.ReadString(); - dataItem.subscriberId_ = parcel.ReadInt64(); - auto index = parcel.ReadUint32(); - if (index == 0) { - sptr ashmem = parcel.ReadAshmem(); - dataItem.value_ = ashmem; - bool ret = ashmem->MapReadOnlyAshmem(); - if (!ret) { - ZLOGE("MapReadAndWriteAshmem fail, %{private}s", dataItem.key_.c_str()); - return false; - } - } else { - dataItem.value_ = parcel.ReadString(); - } - return true; + return ITypesUtil::Unmarshal(parcel, dataItem.key_, dataItem.subscriberId_, dataItem.value_); } template<> bool Marshalling(const PublishedDataItem &dataItem, MessageParcel &parcel) { - if (!parcel.WriteString(dataItem.key_)) { - return false; - } - if (!parcel.WriteInt64(dataItem.subscriberId_)) { - return false; - } - auto index = static_cast(dataItem.value_.index()); - if (!parcel.WriteUint32(index)) { - return false; - } - if (index == 0) { - sptr ashmem = std::get>(dataItem.value_); - if (ashmem == nullptr) { - ZLOGE("ashmem null"); - return false; - } - return parcel.WriteAshmem(ashmem); - } - return parcel.WriteString(std::get(dataItem.value_)); + return ITypesUtil::Marshal(parcel, dataItem.key_, dataItem.subscriberId_, dataItem.value_); } template<> bool Unmarshalling(Data &data, MessageParcel &parcel) { - int32_t len = parcel.ReadInt32(); - if (len < 0) { - return false; - } - size_t size = static_cast(len); - size_t readAbleSize = parcel.GetReadableBytes(); - if ((size > readAbleSize) || (size > data.datas_.max_size())) { - return false; - } - std::vector dataItems; - for (size_t i = 0; i < size; i++) { - PublishedDataItem value; - if (!Unmarshalling(value, parcel)) { - return false; - } - dataItems.emplace_back(std::move(value)); - } - data.datas_ = dataItems; - data.version_ = parcel.ReadInt32(); - return true; + return ITypesUtil::Unmarshal(parcel, data.datas_, data.version_); } template<> @@ -142,18 +91,7 @@ bool Marshalling(const RdbChangeNode &changeNode, MessageParcel &parcel) template<> bool Marshalling(const PublishedDataChangeNode &changeNode, MessageParcel &parcel) { - if (!parcel.WriteString(changeNode.ownerBundleName_)) { - return false; - } - if (!parcel.WriteInt32(changeNode.datas_.size())) { - return false; - } - for (const auto &dataItem : changeNode.datas_) { - if (!Marshalling(dataItem, parcel)) { - return false; - } - } - return true; + return ITypesUtil::Marshal(parcel, changeNode.ownerBundleName_, changeNode.datas_); } template<> @@ -161,4 +99,18 @@ bool Marshalling(const OperationResult &operationResult, MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, operationResult.key_, operationResult.errCode_); } + +template<> +bool ITypesUtil::Unmarshalling(AshmemNode &node, MessageParcel &parcel) +{ + node.isManaged = true; + node.ashmem = parcel.ReadAshmem(); + return true; +} + +template<> +bool ITypesUtil::Marshalling(const AshmemNode &node, MessageParcel &parcel) +{ + return parcel.WriteAshmem(node.ashmem); +} } \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp index 4cc91cae..59e0b54d 100644 --- a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp @@ -34,8 +34,7 @@ Data GetDataStrategy::Execute(std::shared_ptr context) ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); return Data(); } - std::vector queryResult = PublishedData::Query(context->calledBundleName); - return Convert(queryResult); + return Data(); } Strategy *GetDataStrategy::GetStrategy() @@ -60,17 +59,4 @@ Strategy *GetDataStrategy::GetStrategy() } return &strategies; } - -Data GetDataStrategy::Convert(std::vector datas) -{ - Data data; - data.version_ = -1; - for (auto &item : datas) { - if (item.value.version > data.version_) { - data.version_ = item.value.version; - } - data.datas_.emplace_back(item.value.key, item.value.subscriberId, item.value.value); - } - return data; -} } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.h b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.h index d946709b..062f2a79 100644 --- a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.h +++ b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.h @@ -19,8 +19,6 @@ #include #include "data_proxy_observer.h" -#include "datashare_template.h" -#include "published_data.h" #include "seq_strategy.h" namespace OHOS::DataShare { @@ -30,7 +28,6 @@ public: private: static Strategy *GetStrategy(); - static Data Convert(std::vector datas); }; } // namespace OHOS::DataShare #endif diff --git a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp index 7628388b..07c50460 100644 --- a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp @@ -20,7 +20,6 @@ #include "general/load_config_common_strategy.h" #include "general/permission_strategy.h" #include "log_print.h" -#include "published_data.h" #include "utils/anonymous.h" namespace OHOS::DataShare { @@ -35,19 +34,7 @@ int32_t PublishStrategy::Execute(std::shared_ptr context, const Publish ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); return -1; } - auto delegate = KvDBDelegate::GetInstance(); - if (delegate == nullptr) { - ZLOGE("db open failed"); - return -1; - } - PublishedData data(context->uri, context->calledBundleName, item.subscriberId_, item.value_, context->version); - int32_t status = delegate->Upsert(KvDBDelegate::DATA_TABLE, data); - if (status != E_OK) { - ZLOGE("db Upsert failed, %{public}s %{public}s %{public}d", context->calledBundleName.c_str(), - DistributedData::Anonymous::Change(context->uri).c_str(), status); - return -1; - } - return E_OK; + return 0; } Strategy *PublishStrategy::GetStrategy() -- Gitee From a876ceb783942fc83efc8c3d4c1eedb3669f6344 Mon Sep 17 00:00:00 2001 From: hanlu Date: Sat, 6 May 2023 15:43:25 +0800 Subject: [PATCH 106/152] s Signed-off-by: hanlu --- .../framework/include/metadata/store_meta_data.h | 2 -- .../service/data_share/data_share_service_impl.cpp | 2 +- .../service/directory/src/directory_manager.cpp | 5 ----- 3 files changed, 1 insertion(+), 8 deletions(-) diff --git a/services/distributeddataservice/framework/include/metadata/store_meta_data.h b/services/distributeddataservice/framework/include/metadata/store_meta_data.h index 98719900..bb134e35 100644 --- a/services/distributeddataservice/framework/include/metadata/store_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/store_meta_data.h @@ -55,8 +55,6 @@ struct API_EXPORT StoreMetaData final : public Serializable { STORE_RELATIONAL_END = 19, STORE_OBJECT_BEGIN = 20, STORE_OBJECT_END = 29, - STORE_DATA_SHARE_BEGIN = 30, - STORE_DATA_SHARE_END = 39, STORE_BUTT = 255 }; diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 6b3d45a6..92cda7be 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -227,7 +227,7 @@ std::vector DataShareServiceImpl::DisableSubscribePublishedData } enum DataShareKvStoreType : int32_t { - DATA_SHARE_SINGLE_VERSION = 30, + DATA_SHARE_SINGLE_VERSION = 0, DISTRIBUTED_TYPE_BUTT }; diff --git a/services/distributeddataservice/service/directory/src/directory_manager.cpp b/services/distributeddataservice/service/directory/src/directory_manager.cpp index edcdda5f..bba79049 100644 --- a/services/distributeddataservice/service/directory/src/directory_manager.cpp +++ b/services/distributeddataservice/service/directory/src/directory_manager.cpp @@ -132,11 +132,6 @@ std::string DirectoryManager::GetStore(const StoreMetaData &metaData) const && metaData.storeType <= StoreMetaData::StoreType::STORE_OBJECT_END) { return "kvdb"; } - // datashare use meta - if (metaData.storeType >= StoreMetaData::StoreType::STORE_DATA_SHARE_BEGIN - && metaData.storeType <= StoreMetaData::StoreType::STORE_DATA_SHARE_END) { - return "kvdb"; - } return "other"; } -- Gitee From 868d63b5fb1febb2a14e3d055270d4d83294f22a Mon Sep 17 00:00:00 2001 From: hanlu Date: Sat, 6 May 2023 19:07:02 +0800 Subject: [PATCH 107/152] f Signed-off-by: hanlu --- .../distributeddataservice/service/BUILD.gn | 4 +++ .../data_share/data_share_service_impl.cpp | 12 ++++----- .../data_share/data_share_service_impl.h | 12 ++++----- .../data_share/data_share_service_stub.cpp | 24 +++++++++--------- .../data_share/data_share_service_stub.h | 25 +++++++++---------- .../data_share/data_share_types_util.cpp | 4 +-- .../service/data_share/idata_share_service.h | 12 ++++----- 7 files changed, 48 insertions(+), 45 deletions(-) diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index eff162a9..1009d598 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -76,6 +76,10 @@ ohos_shared_library("distributeddatasvc") { "data_share/common/rdb_delegate.cpp", "data_share/common/seq_strategy.cpp", "data_share/common/uri_utils.cpp", + "data_share/data_share_obs_proxy.cpp", + "data_share/data_share_service_impl.cpp", + "data_share/data_share_service_stub.cpp", + "data_share/data_share_types_util.cpp", "data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp", "data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp", "data_share/strategies/delete_strategy.cpp", diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 92cda7be..833de07f 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -163,7 +163,7 @@ std::vector DataShareServiceImpl::SubscribeRdbData( return std::vector(); } -std::vector DataShareServiceImpl::UnSubscribeRdbData( +std::vector DataShareServiceImpl::UnsubscribeRdbData( const std::vector &uris, const TemplateId &id) { std::vector results; @@ -176,7 +176,7 @@ std::vector DataShareServiceImpl::UnSubscribeRdbData( return results; } -std::vector DataShareServiceImpl::EnableSubscribeRdbData( +std::vector DataShareServiceImpl::EnableRdbSubs( const std::vector &uris, const TemplateId &id) { std::vector results; @@ -189,7 +189,7 @@ std::vector DataShareServiceImpl::EnableSubscribeRdbData( return results; } -std::vector DataShareServiceImpl::DisableSubscribeRdbData( +std::vector DataShareServiceImpl::DisableRdbSubs( const std::vector &uris, const TemplateId &id) { std::vector results; @@ -208,19 +208,19 @@ std::vector DataShareServiceImpl::SubscribePublishedData(const return std::vector(); } -std::vector DataShareServiceImpl::UnSubscribePublishedData(const std::vector &uris, +std::vector DataShareServiceImpl::UnsubscribePublishedData(const std::vector &uris, const int64_t subscriberId) { return std::vector(); } -std::vector DataShareServiceImpl::EnableSubscribePublishedData(const std::vector &uris, +std::vector DataShareServiceImpl::EnablePubSubs(const std::vector &uris, const int64_t subscriberId) { return std::vector(); } -std::vector DataShareServiceImpl::DisableSubscribePublishedData(const std::vector &uris, +std::vector DataShareServiceImpl::DisablePubSubs(const std::vector &uris, const int64_t subscriberId) { return std::vector(); diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.h b/services/distributeddataservice/service/data_share/data_share_service_impl.h index a0fc8069..6cf14030 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -40,19 +40,19 @@ public: Data GetData(const std::string &bundleNameOfProvider) override; std::vector SubscribeRdbData(const std::vector &uris, const TemplateId &id, const sptr observer) override; - std::vector UnSubscribeRdbData( + std::vector UnsubscribeRdbData( const std::vector &uris, const TemplateId &id) override; - std::vector EnableSubscribeRdbData( + std::vector EnableRdbSubs( const std::vector &uris, const TemplateId &id) override; - std::vector DisableSubscribeRdbData( + std::vector DisableRdbSubs( const std::vector &uris, const TemplateId &id) override; std::vector SubscribePublishedData(const std::vector &uris, const int64_t subscriberId, const sptr observer) override; - std::vector UnSubscribePublishedData(const std::vector &uris, + std::vector UnsubscribePublishedData(const std::vector &uris, const int64_t subscriberId) override; - std::vector EnableSubscribePublishedData(const std::vector &uris, + std::vector EnablePubSubs(const std::vector &uris, const int64_t subscriberId) override; - std::vector DisableSubscribePublishedData(const std::vector &uris, + std::vector DisablePubSubs(const std::vector &uris, const int64_t subscriberId) override; int32_t OnInitialize() override; int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; diff --git a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp index 2e29828d..e1fb190d 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp @@ -194,7 +194,7 @@ int32_t DataShareServiceStub::OnRemoteSubscribeRdbData(MessageParcel &data, Mess return 0; } -int32_t DataShareServiceStub::OnRemoteUnSubscribeRdbData(MessageParcel &data, MessageParcel &reply) +int32_t DataShareServiceStub::OnRemoteUnsubscribeRdbData(MessageParcel &data, MessageParcel &reply) { std::vector uris; TemplateId templateId; @@ -202,7 +202,7 @@ int32_t DataShareServiceStub::OnRemoteUnSubscribeRdbData(MessageParcel &data, Me ZLOGE("read device list failed."); return -1; } - std::vector results = UnSubscribeRdbData(uris, templateId); + std::vector results = UnsubscribeRdbData(uris, templateId); if (!ITypesUtil::Marshal(reply, results)) { ZLOGE("ITypesUtil::Marshal(reply, results) failed"); return -1; @@ -210,7 +210,7 @@ int32_t DataShareServiceStub::OnRemoteUnSubscribeRdbData(MessageParcel &data, Me return 0; } -int32_t DataShareServiceStub::OnRemoteEnableSubscribeRdbData(MessageParcel &data, MessageParcel &reply) +int32_t DataShareServiceStub::OnRemoteEnableRdbSubs(MessageParcel &data, MessageParcel &reply) { std::vector uris; TemplateId templateId; @@ -218,7 +218,7 @@ int32_t DataShareServiceStub::OnRemoteEnableSubscribeRdbData(MessageParcel &data ZLOGE("read device list failed."); return -1; } - std::vector results = EnableSubscribeRdbData(uris, templateId); + std::vector results = EnableRdbSubs(uris, templateId); if (!ITypesUtil::Marshal(reply, results)) { ZLOGE("ITypesUtil::Marshal(reply, results) failed"); return -1; @@ -226,7 +226,7 @@ int32_t DataShareServiceStub::OnRemoteEnableSubscribeRdbData(MessageParcel &data return 0; } -int32_t DataShareServiceStub::OnRemoteDisableSubscribeRdbData(MessageParcel &data, MessageParcel &reply) +int32_t DataShareServiceStub::OnRemoteDisableRdbSubs(MessageParcel &data, MessageParcel &reply) { std::vector uris; TemplateId templateId; @@ -234,7 +234,7 @@ int32_t DataShareServiceStub::OnRemoteDisableSubscribeRdbData(MessageParcel &dat ZLOGE("read device list failed."); return -1; } - std::vector results = DisableSubscribeRdbData(uris, templateId); + std::vector results = DisableRdbSubs(uris, templateId); if (!ITypesUtil::Marshal(reply, results)) { ZLOGE("ITypesUtil::Marshal(reply, results) failed"); return -1; @@ -263,7 +263,7 @@ int32_t DataShareServiceStub::OnRemoteSubscribePublishedData(MessageParcel &data return 0; } -int32_t DataShareServiceStub::OnRemoteUnSubscribePublishedData(MessageParcel &data, MessageParcel &reply) +int32_t DataShareServiceStub::OnRemoteUnsubscribePublishedData(MessageParcel &data, MessageParcel &reply) { std::vector uris; int64_t subscriberId; @@ -271,7 +271,7 @@ int32_t DataShareServiceStub::OnRemoteUnSubscribePublishedData(MessageParcel &da ZLOGE("read device list failed."); return -1; } - std::vector results = UnSubscribePublishedData(uris, subscriberId); + std::vector results = UnsubscribePublishedData(uris, subscriberId); if (!ITypesUtil::Marshal(reply, results)) { ZLOGE("ITypesUtil::Marshal(reply, results) failed"); return -1; @@ -279,7 +279,7 @@ int32_t DataShareServiceStub::OnRemoteUnSubscribePublishedData(MessageParcel &da return 0; } -int32_t DataShareServiceStub::OnRemoteEnableSubscribePublishedData(MessageParcel &data, MessageParcel &reply) +int32_t DataShareServiceStub::OnRemoteEnablePubSubs(MessageParcel &data, MessageParcel &reply) { std::vector uris; int64_t subscriberId; @@ -287,7 +287,7 @@ int32_t DataShareServiceStub::OnRemoteEnableSubscribePublishedData(MessageParcel ZLOGE("read device list failed."); return -1; } - std::vector results = EnableSubscribePublishedData(uris, subscriberId); + std::vector results = EnablePubSubs(uris, subscriberId); if (!ITypesUtil::Marshal(reply, results)) { ZLOGE("ITypesUtil::Marshal(reply, results) failed"); return -1; @@ -295,7 +295,7 @@ int32_t DataShareServiceStub::OnRemoteEnableSubscribePublishedData(MessageParcel return 0; } -int32_t DataShareServiceStub::OnRemoteDisableSubscribePublishedData(MessageParcel &data, MessageParcel &reply) +int32_t DataShareServiceStub::OnRemoteDisablePubSubs(MessageParcel &data, MessageParcel &reply) { std::vector uris; int64_t subscriberId; @@ -303,7 +303,7 @@ int32_t DataShareServiceStub::OnRemoteDisableSubscribePublishedData(MessageParce ZLOGE("read device list failed."); return -1; } - std::vector results = DisableSubscribePublishedData(uris, subscriberId); + std::vector results = DisablePubSubs(uris, subscriberId); if (!ITypesUtil::Marshal(reply, results)) { ZLOGE("ITypesUtil::Marshal(reply, results) failed"); return -1; diff --git a/services/distributeddataservice/service/data_share/data_share_service_stub.h b/services/distributeddataservice/service/data_share/data_share_service_stub.h index 05e4e060..b786529f 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_stub.h +++ b/services/distributeddataservice/service/data_share/data_share_service_stub.h @@ -36,13 +36,13 @@ private: int32_t OnRemotePublish(MessageParcel& data, MessageParcel& reply); int32_t OnRemoteGetData(MessageParcel& data, MessageParcel& reply); int32_t OnRemoteSubscribeRdbData(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteUnSubscribeRdbData(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteEnableSubscribeRdbData(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteDisableSubscribeRdbData(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteUnsubscribeRdbData(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteEnableRdbSubs(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteDisableRdbSubs(MessageParcel& data, MessageParcel& reply); int32_t OnRemoteSubscribePublishedData(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteUnSubscribePublishedData(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteEnableSubscribePublishedData(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteDisableSubscribePublishedData(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteUnsubscribePublishedData(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteEnablePubSubs(MessageParcel& data, MessageParcel& reply); + int32_t OnRemoteDisablePubSubs(MessageParcel& data, MessageParcel& reply); using RequestHandle = int (DataShareServiceStub::*)(MessageParcel &, MessageParcel &); static constexpr RequestHandle HANDLERS[DATA_SHARE_SERVICE_CMD_MAX] = { &DataShareServiceStub::OnRemoteInsert, @@ -54,14 +54,13 @@ private: &DataShareServiceStub::OnRemotePublish, &DataShareServiceStub::OnRemoteGetData, &DataShareServiceStub::OnRemoteSubscribeRdbData, - &DataShareServiceStub::OnRemoteUnSubscribeRdbData, - &DataShareServiceStub::OnRemoteEnableSubscribeRdbData, - &DataShareServiceStub::OnRemoteDisableSubscribeRdbData, + &DataShareServiceStub::OnRemoteUnsubscribeRdbData, + &DataShareServiceStub::OnRemoteEnableRdbSubs, + &DataShareServiceStub::OnRemoteDisableRdbSubs, &DataShareServiceStub::OnRemoteSubscribePublishedData, - &DataShareServiceStub::OnRemoteUnSubscribePublishedData, - &DataShareServiceStub::OnRemoteEnableSubscribePublishedData, - &DataShareServiceStub::OnRemoteDisableSubscribePublishedData - }; + &DataShareServiceStub::OnRemoteUnsubscribePublishedData, + &DataShareServiceStub::OnRemoteEnablePubSubs, + &DataShareServiceStub::OnRemoteDisablePubSubs }; }; } // namespace DataShare } // namespace OHOS diff --git a/services/distributeddataservice/service/data_share/data_share_types_util.cpp b/services/distributeddataservice/service/data_share/data_share_types_util.cpp index ec255ec2..804a6f76 100644 --- a/services/distributeddataservice/service/data_share/data_share_types_util.cpp +++ b/services/distributeddataservice/service/data_share/data_share_types_util.cpp @@ -101,7 +101,7 @@ bool Marshalling(const OperationResult &operationResult, MessageParcel &parcel) } template<> -bool ITypesUtil::Unmarshalling(AshmemNode &node, MessageParcel &parcel) +bool Unmarshalling(AshmemNode &node, MessageParcel &parcel) { node.isManaged = true; node.ashmem = parcel.ReadAshmem(); @@ -109,7 +109,7 @@ bool ITypesUtil::Unmarshalling(AshmemNode &node, MessageParcel &parcel) } template<> -bool ITypesUtil::Marshalling(const AshmemNode &node, MessageParcel &parcel) +bool Marshalling(const AshmemNode &node, MessageParcel &parcel) { return parcel.WriteAshmem(node.ashmem); } diff --git a/services/distributeddataservice/service/data_share/idata_share_service.h b/services/distributeddataservice/service/data_share/idata_share_service.h index 1bfdc05d..6d465247 100644 --- a/services/distributeddataservice/service/data_share/idata_share_service.h +++ b/services/distributeddataservice/service/data_share/idata_share_service.h @@ -63,19 +63,19 @@ public: virtual Data GetData(const std::string &bundleNameOfProvider) = 0; virtual std::vector SubscribeRdbData( const std::vector &uris, const TemplateId &id, const sptr observer) = 0; - virtual std::vector UnSubscribeRdbData( + virtual std::vector UnsubscribeRdbData( const std::vector &uris, const TemplateId &id) = 0; - virtual std::vector EnableSubscribeRdbData( + virtual std::vector EnableRdbSubs( const std::vector &uris, const TemplateId &id) = 0; - virtual std::vector DisableSubscribeRdbData( + virtual std::vector DisableRdbSubs( const std::vector &uris, const TemplateId &id) = 0; virtual std::vector SubscribePublishedData(const std::vector &uris, const int64_t subscriberId, const sptr observer) = 0; - virtual std::vector UnSubscribePublishedData(const std::vector &uris, + virtual std::vector UnsubscribePublishedData(const std::vector &uris, const int64_t subscriberId) = 0; - virtual std::vector EnableSubscribePublishedData(const std::vector &uris, + virtual std::vector EnablePubSubs(const std::vector &uris, const int64_t subscriberId) = 0; - virtual std::vector DisableSubscribePublishedData(const std::vector &uris, + virtual std::vector DisablePubSubs(const std::vector &uris, const int64_t subscriberId) = 0; }; } // namespace OHOS::DataShare -- Gitee From 3334557d8fbb6b0c54a1cba6557e566647924228 Mon Sep 17 00:00:00 2001 From: hanlu Date: Sat, 6 May 2023 19:18:30 +0800 Subject: [PATCH 108/152] f Signed-off-by: hanlu --- .../service/data_share/common/db_delegate.cpp | 6 +++--- .../service/data_share/common/db_delegate.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp index e05c4f15..435ecefa 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -48,15 +48,15 @@ bool KvData::Marshal(DistributedData::Serializable::json &node) const bool Id::Marshal(DistributedData::Serializable::json &node) const { - return SetValue(node["_id"], id); + return SetValue(node[GET_NAME(_id)], _id); } bool Id::Unmarshal(const DistributedData::Serializable::json &node) { - return GetValue(node, "_id", id); + return GetValue(node, GET_NAME(_id), _id); } -Id::Id(const std::string &id) : id(id) {} +Id::Id(const std::string &id) : _id(id) {} VersionData::VersionData(int version) : version(version) {} diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.h b/services/distributeddataservice/service/data_share/common/db_delegate.h index f0e3e880..677aea2b 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.h +++ b/services/distributeddataservice/service/data_share/common/db_delegate.h @@ -54,7 +54,7 @@ struct Id final: public DistributedData::Serializable { bool Unmarshal(const json &node) override; private: - std::string id; + std::string _id; }; class VersionData : public DistributedData::Serializable { -- Gitee From 473d5123365ab2bc42a54f4fb4928fea0b7ecc1b Mon Sep 17 00:00:00 2001 From: hanlu Date: Sat, 6 May 2023 19:19:37 +0800 Subject: [PATCH 109/152] f Signed-off-by: hanlu --- .../service/data_share/common/db_delegate.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp index 435ecefa..5b9a80c2 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -62,10 +62,10 @@ VersionData::VersionData(int version) : version(version) {} bool VersionData::Unmarshal(const DistributedData::Serializable::json &node) { - return GetValue(node, "version", version); + return GetValue(node, GET_NAME(version), version); } bool VersionData::Marshal(DistributedData::Serializable::json &node) const { - return SetValue(node["version"], version); + return SetValue(node[GET_NAME(version)], version); } } // namespace OHOS::DataShare \ No newline at end of file -- Gitee From 2574f9e01bb5c07ab0adb51c91e3b80968434e1a Mon Sep 17 00:00:00 2001 From: hanlu Date: Sat, 6 May 2023 19:24:06 +0800 Subject: [PATCH 110/152] f Signed-off-by: hanlu --- .../service/data_share/common/db_delegate.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp index 5b9a80c2..c145abbd 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -64,6 +64,7 @@ bool VersionData::Unmarshal(const DistributedData::Serializable::json &node) { return GetValue(node, GET_NAME(version), version); } + bool VersionData::Marshal(DistributedData::Serializable::json &node) const { return SetValue(node[GET_NAME(version)], version); -- Gitee From 0d28492dd4e70e2e96b8588f151d73708d182704 Mon Sep 17 00:00:00 2001 From: dingdongdong Date: Sat, 6 May 2023 23:22:06 +0800 Subject: [PATCH 111/152] add uri permission proxy Signed-off-by: dingdongdong --- services/distributeddataservice/app/distributed_data.cfg | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/app/distributed_data.cfg b/services/distributeddataservice/app/distributed_data.cfg index a42d9dc4..3c0eb54e 100644 --- a/services/distributeddataservice/app/distributed_data.cfg +++ b/services/distributeddataservice/app/distributed_data.cfg @@ -29,10 +29,11 @@ "secon" : "u:r:distributeddata:s0", "apl" : "system_basic", "permission" : [ - "ohos.permission.DISTRIBUTED_DATASYNC", - "ohos.permission.MANAGE_LOCAL_ACCOUNTS", - "ohos.permission.ACCESS_SERVICE_DM" + "ohos.permission.DISTRIBUTED_DATASYNC", + "ohos.permission.MANAGE_LOCAL_ACCOUNTS", + "ohos.permission.ACCESS_SERVICE_DM", + "ohos.permission.PROXY_AUTHORIZATION_URI" ] } ] -} +} \ No newline at end of file -- Gitee From e0a7461c6b42b79f830c60954194b09acdd3b427 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 11:02:11 +0800 Subject: [PATCH 112/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../src/account_delegate_normal_impl.cpp | 34 +++++++------- .../src/account_delegate_normal_impl.h | 5 ++ .../app/src/kvstore_data_service.cpp | 9 ++-- .../app/src/kvstore_meta_manager.cpp | 46 +++++++++++-------- .../app/src/kvstore_meta_manager.h | 6 +++ .../src/session_manager/upgrade_manager.cpp | 30 +++++++----- .../app/src/session_manager/upgrade_manager.h | 4 ++ .../service/backup/include/backup_manager.h | 5 +- .../service/backup/src/backup_manager.cpp | 11 +++-- .../service/bootstrap/include/bootstrap.h | 2 +- .../service/bootstrap/src/bootstrap.cpp | 2 +- .../service/kvdb/user_delegate.cpp | 30 ++++++------ .../service/kvdb/user_delegate.h | 5 +- .../service/rdb/rdb_service_impl.cpp | 8 +--- 14 files changed, 117 insertions(+), 80 deletions(-) diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp index 562820b6..9e878d93 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp +++ b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp @@ -89,28 +89,28 @@ void AccountDelegateNormalImpl::SubscribeAccountEvent(std::shared_ptrExecute(GetTask()); +} - executors->Execute([eventSubscriber = eventSubscriber_]() { - int tryTimes = 0; - constexpr int MAX_RETRY_TIME = 300; - constexpr int RETRY_WAIT_TIME_S = 1; - - // we use this method to make sure register success - while (tryTimes < MAX_RETRY_TIME) { - auto result = CommonEventManager::SubscribeCommonEvent(eventSubscriber); - if (result) { - break; - } - - ZLOGD("fail to register subscriber, error:%{public}d, time:%{public}d", result, tryTimes); - sleep(RETRY_WAIT_TIME_S); - tryTimes++; +ExecutorPool::Task AccountDelegateNormalImpl::GetTask() +{ + return [this] { + auto result = CommonEventManager::SubscribeCommonEvent(eventSubscriber_); + if (result) { + ZLOGI("success to register subscriber."); + return; } + ZLOGD("fail to register subscriber, error:%{public}d, time:%{public}d", result, tryTimes); + tryTimes++; if (tryTimes == MAX_RETRY_TIME) { ZLOGE("fail to register subscriber!"); + return; } - ZLOGI("success to register subscriber."); - }); + executors_->Execute(GetTask(), std::chrono::seconds(RETRY_WAIT_TIME_S)); + }; } AccountDelegateNormalImpl::~AccountDelegateNormalImpl() diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h index 3bc7c18b..60e07cf5 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h +++ b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h @@ -36,7 +36,12 @@ public: private: ~AccountDelegateNormalImpl(); std::string Sha256AccountId(const std::string &plainText) const; + ExecutorPool::Task GetTask(); + int tryTimes = 0; + static constexpr int MAX_RETRY_TIME = 300; + static constexpr int RETRY_WAIT_TIME_S = 1; std::shared_ptr eventSubscriber_ {}; + std::shared_ptr executors_; }; } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 0932c8d8..8fc9af71 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -21,6 +21,7 @@ #include "auth_delegate.h" #include "auto_launch_export.h" +#include "backup_manager.h" #include "bootstrap.h" #include "checker/checker_manager.h" #include "communication_provider.h" @@ -248,7 +249,8 @@ void KvStoreDataService::OnStart() Bootstrap::GetInstance().LoadDirectory(); Bootstrap::GetInstance().LoadCheckers(); Bootstrap::GetInstance().LoadNetworks(); - Bootstrap::GetInstance().LoadBackup(executors_); + BackupManager::GetInstance().Init(executors_); + Bootstrap::GetInstance().LoadBackup(); Initialize(); auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgr != nullptr) { @@ -457,13 +459,12 @@ void KvStoreDataService::ResolveAutoLaunchCompatible(const StoreMetaData &storeM } }); ExecutorPool::Task delayTask([store]() { - constexpr const int CLOSE_STORE_DELAY_TIME = 60; // unit: seconds - std::this_thread::sleep_for(std::chrono::seconds(CLOSE_STORE_DELAY_TIME)); ZLOGI("AutoLaunch:close store after 60s while autolaunch finishied"); DistributedDB::KvStoreDelegateManager delegateManager("", ""); delegateManager.CloseKvStore(store); }); - executors_->Execute(std::move(delayTask)); + constexpr const int CLOSE_STORE_DELAY_TIME = 60; // unit: seconds + executors_->Execute(std::move(delayTask), std::chrono::seconds(CLOSE_STORE_DELAY_TIME)); } Status KvStoreDataService::InitNbDbOption(const Options &options, const std::vector &cipherKey, diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index f3ddf7c5..cdfb5f05 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -172,29 +172,37 @@ void KvStoreMetaManager::InitMetaData() void KvStoreMetaManager::InitMetaParameter(std::shared_ptr executors) { ZLOGI("start."); - executors->Execute([]() { - constexpr int32_t RETRY_MAX_TIMES = 100; - constexpr int32_t RETRY_INTERVAL = 1 * 1000 * 1000; // retry after 1 second - BlockInteger retry(RETRY_INTERVAL); - while (retry < RETRY_MAX_TIMES) { - auto status = CryptoManager::GetInstance().CheckRootKey(); - if (status == CryptoManager::ErrCode::SUCCESS) { - ZLOGI("root key exist."); - break; - } - if (status == CryptoManager::ErrCode::NOT_EXIST && - CryptoManager::GetInstance().GenerateRootKey() == CryptoManager::ErrCode::SUCCESS) { - ZLOGI("GenerateRootKey success."); - break; - } - ++retry; - ZLOGW("GenerateRootKey failed, retry times:%{public}d.", static_cast(retry)); - } - }); + if (!executors_) { + executors_ = executors; + } + executors_->Execute(GetTask()); DistributedDB::KvStoreConfig kvStoreConfig{ metaDBDirectory_ }; delegateManager_.SetKvStoreConfig(kvStoreConfig); } +ExecutorPool::Task KvStoreMetaManager::GetTask() +{ + return [this] { + auto status = CryptoManager::GetInstance().CheckRootKey(); + if (status == CryptoManager::ErrCode::SUCCESS) { + ZLOGI("root key exist."); + return; + } + if (status == CryptoManager::ErrCode::NOT_EXIST && + CryptoManager::GetInstance().GenerateRootKey() == CryptoManager::ErrCode::SUCCESS) { + ZLOGI("GenerateRootKey success."); + return; + } + retryTimes_++; + ZLOGW("GenerateRootKey failed, retry times:%{public}d.", static_cast(retryTimes_)); + if (retryTimes_ == RETRY_MAX_TIMES) { + ZLOGE("fail to register subscriber!"); + return; + } + executors_->Execute(GetTask(), std::chrono::seconds(RETRY_INTERVAL)); + }; +} + KvStoreMetaManager::NbDelegate KvStoreMetaManager::GetMetaKvStore() { if (metaDelegate_ != nullptr) { diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.h b/services/distributeddataservice/app/src/kvstore_meta_manager.h index 1702e466..669e5173 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.h +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.h @@ -72,6 +72,8 @@ private: std::string GetBackupPath() const; + ExecutorPool::Task GetTask(); + class KvStoreMetaObserver : public DistributedDB::KvStoreObserver { public: virtual ~KvStoreMetaObserver(); @@ -83,6 +85,9 @@ private: void HandleChanges(CHANGE_FLAG flag, const std::list &list); }; + static constexpr int32_t RETRY_MAX_TIMES = 100; + static constexpr int32_t RETRY_INTERVAL = 1; + int32_t retryTimes_ = 0; NbDelegate metaDelegate_; std::string metaDBDirectory_; const std::string label_; @@ -90,6 +95,7 @@ private: static MetaDeviceChangeListenerImpl listener_; KvStoreMetaObserver metaObserver_; std::recursive_mutex mutex_; + std::shared_ptr executors_; }; } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp index de0262c2..52aad0ff 100644 --- a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp +++ b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp @@ -35,22 +35,30 @@ UpgradeManager &UpgradeManager::GetInstance() void UpgradeManager::Init(std::shared_ptr executors) { - executors->Execute([this]() { - do { - if (InitLocalCapability()) { - break; - } - static constexpr int RETRY_INTERVAL = 500; // millisecond - std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_INTERVAL)); - } while (true); - }); + if (executors_) { + return; + } + executors_ = std::move(executors); + executors_->Execute(GetTask()); +} + +ExecutorPool::Task UpgradeManager::GetTask() +{ + return [this]{ + auto succ = InitLocalCapability(); + if (succ) { + return; + } + executors_->Execute(GetTask(), std::chrono::milliseconds(RETRY_INTERVAL)); + }; } CapMetaData UpgradeManager::GetCapability(const std::string &deviceId, bool &status) { status = true; - if (capabilities_.Contains(deviceId)) { - return capabilities_.Find(deviceId).second; + auto index = capabilities_.Find(deviceId); + if (index.first) { + return index.second; } ZLOGI("load capability from meta"); CapMetaData capMetaData; diff --git a/services/distributeddataservice/app/src/session_manager/upgrade_manager.h b/services/distributeddataservice/app/src/session_manager/upgrade_manager.h index ab799f62..795178f4 100644 --- a/services/distributeddataservice/app/src/session_manager/upgrade_manager.h +++ b/services/distributeddataservice/app/src/session_manager/upgrade_manager.h @@ -21,6 +21,7 @@ #include "concurrent_map.h" #include "kvstore_meta_manager.h" #include "metadata/capability_meta_data.h" +#include "executor_pool.h" #include "types.h" namespace OHOS::DistributedData { using DistributedDB::KvStoreNbDelegate; @@ -36,8 +37,11 @@ public: static std::string GetIdentifierByType(int32_t groupType, bool &isSuccess); private: + static constexpr int RETRY_INTERVAL = 500; // milliseconds bool InitLocalCapability(); + ExecutorPool::Task GetTask(); ConcurrentMap capabilities_ {}; + std::shared_ptr executors_; }; } // namespace OHOS::DistributedData #endif // DISTRIBUTEDDATAMGR_UPGRADE_MANAGER_H diff --git a/services/distributeddataservice/service/backup/include/backup_manager.h b/services/distributeddataservice/service/backup/include/backup_manager.h index 7233afea..0423cef9 100644 --- a/services/distributeddataservice/service/backup/include/backup_manager.h +++ b/services/distributeddataservice/service/backup/include/backup_manager.h @@ -36,8 +36,8 @@ public: CLEAN_DATA, }; static BackupManager &GetInstance(); - void Init(); - void BackSchedule(std::shared_ptr executors); + void Init(std::shared_ptr executors); + void BackSchedule(); void SetBackupParam(const BackupParam &backupParam); void RegisterExporter(int32_t type, Exporter exporter); bool GetPassWord(const StoreMetaData &meta, std::vector &password); @@ -64,6 +64,7 @@ private: int64_t backupSuccessTime_ = 0; int64_t backupNumber_ = 0; int64_t startNum_ = 0; + std::shared_ptr executors_; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_BACKUP_BACKUP_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/backup/src/backup_manager.cpp b/services/distributeddataservice/service/backup/src/backup_manager.cpp index 7bde171c..68bfd7d5 100644 --- a/services/distributeddataservice/service/backup/src/backup_manager.cpp +++ b/services/distributeddataservice/service/backup/src/backup_manager.cpp @@ -48,8 +48,11 @@ BackupManager &BackupManager::GetInstance() return instance; } -void BackupManager::Init() +void BackupManager::Init(std::shared_ptr executors) { + if (!executors_) { + executors_ = executors; + } std::vector metas; MetaDataManager::GetInstance().LoadMeta( StoreMetaData::GetPrefix({DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid}), metas); @@ -90,12 +93,12 @@ void BackupManager::RegisterExporter(int32_t type, Exporter exporter) } } -void BackupManager::BackSchedule(std::shared_ptr executors) +void BackupManager::BackSchedule() { std::chrono::duration delay(schedularDelay_); std::chrono::duration internal(schedularInternal_); ZLOGI("BackupManager Schedule start."); - executors->Schedule( + executors_->Schedule( [this]() { if (!CanBackup()) { return; @@ -120,7 +123,7 @@ void BackupManager::BackSchedule(std::shared_ptr executors) sync(); backupSuccessTime_ = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); }, - internal, delay); + delay, internal); } void BackupManager::DoBackup(const StoreMetaData &meta) diff --git a/services/distributeddataservice/service/bootstrap/include/bootstrap.h b/services/distributeddataservice/service/bootstrap/include/bootstrap.h index c2301236..e98d00f0 100644 --- a/services/distributeddataservice/service/bootstrap/include/bootstrap.h +++ b/services/distributeddataservice/service/bootstrap/include/bootstrap.h @@ -29,7 +29,7 @@ public: API_EXPORT void LoadCheckers(); API_EXPORT void LoadNetworks(); API_EXPORT void LoadDirectory(); - API_EXPORT void LoadBackup(std::shared_ptr executors); + API_EXPORT void LoadBackup(); private: static constexpr const char *DEFAULT_LABEL = "distributeddata"; static constexpr const char *DEFAULT_META = "service_meta"; diff --git a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp index 299c70ba..8e1fff33 100644 --- a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp +++ b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp @@ -106,7 +106,7 @@ void Bootstrap::LoadBackup(std::shared_ptr executors) backupRules->schedularInternal, backupRules->backupInternal, backupRules->backupNumber}; BackupManager::GetInstance().SetBackupParam(backupParam); BackupManager::GetInstance().Init(); - BackupManager::GetInstance().BackSchedule(executors); + BackupManager::GetInstance().BackSchedule(); } void Bootstrap::LoadNetworks() diff --git a/services/distributeddataservice/service/kvdb/user_delegate.cpp b/services/distributeddataservice/service/kvdb/user_delegate.cpp index b913ae9b..e626e803 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.cpp +++ b/services/distributeddataservice/service/kvdb/user_delegate.cpp @@ -148,20 +148,8 @@ UserDelegate &UserDelegate::GetInstance() return instance; } -void UserDelegate::Init(std::shared_ptr executors) +void UserDelegate::Init(const std::shared_ptr& executors) { - ExecutorPool::Task retryTask([this]() { - do { - static constexpr int RETRY_INTERVAL = 500; // millisecond - std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_INTERVAL)); - if (!InitLocalUserMeta()) { - continue; - } - break; - } while (true); - ZLOGI("update user meta ok"); - }); - auto ret = AccountDelegate::GetInstance()->Subscribe(std::make_shared(*this)); MetaDataManager::GetInstance().Subscribe( UserMetaRow::KEY_PREFIX, [this](const std::string &key, const std::string &value, int32_t flag) -> auto { @@ -181,12 +169,26 @@ void UserDelegate::Init(std::shared_ptr executors) } return true; }); + if (!executors_) { + executors_ = executors; + } if (!InitLocalUserMeta()) { - executors->Execute(std::move(retryTask)); + executors_->Execute(GeTask()); } ZLOGD("subscribe os account ret:%{public}d", ret); } +ExecutorPool::Task UserDelegate::GeTask() +{ + return [this]{ + auto ret = InitLocalUserMeta(); + if (ret) { + return; + } + executors_->Execute(GeTask(), std::chrono::milliseconds(RETRY_INTERVAL)); + }; +} + bool UserDelegate::NotifyUserEvent(const UserDelegate::UserEvent &userEvent) { // update all local user status diff --git a/services/distributeddataservice/service/kvdb/user_delegate.h b/services/distributeddataservice/service/kvdb/user_delegate.h index 21825a1f..67009804 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.h +++ b/services/distributeddataservice/service/kvdb/user_delegate.h @@ -35,7 +35,7 @@ public: }; API_EXPORT static UserDelegate &GetInstance(); - API_EXPORT void Init(std::shared_ptr executors); + API_EXPORT void Init(const std::shared_ptr& executors); API_EXPORT std::vector GetLocalUserStatus(); API_EXPORT std::set GetLocalUsers(); API_EXPORT std::vector GetRemoteUserStatus(const std::string &deviceId); @@ -60,7 +60,10 @@ private: void UpdateUsers(const std::string &deviceId, const std::vector &userStatus); void DeleteUsers(const std::string &deviceId); bool NotifyUserEvent(const UserEvent &userEvent); + ExecutorPool::Task GeTask(); + static constexpr int RETRY_INTERVAL = 500; // millisecond + std::shared_ptr executors_; // device : { user : isActive } ConcurrentMap> deviceUser_; }; diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index d5c742bc..9d5e495d 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -232,9 +232,7 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa syncer = it->second; if (!param.isEncrypt_ || param.password_.empty()) { executors_->Execute( - [this, syncer] { - SyncerTimeout(syncer); - }, std::chrono::milliseconds(SYNCER_TIMEOUT)); + [this, syncer] { SyncerTimeout(syncer); }, std::chrono::milliseconds(SYNCER_TIMEOUT)); return true; } syncers.erase(storeId); @@ -259,9 +257,7 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa syncer = syncer_; syncerNum_++; executors_->Execute( - [this, syncer]() { - SyncerTimeout(syncer); - }, std::chrono::milliseconds(SYNCER_TIMEOUT)); + [this, syncer] { SyncerTimeout(syncer); }, std::chrono::milliseconds(SYNCER_TIMEOUT)); return !syncers.empty(); }); -- Gitee From 7bdfecca1916c0b91b5d243d907be4a26c606289 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 11:15:46 +0800 Subject: [PATCH 113/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../app/src/session_manager/upgrade_manager.cpp | 2 +- .../distributeddataservice/service/bootstrap/src/bootstrap.cpp | 3 +-- services/distributeddataservice/service/kvdb/user_delegate.cpp | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp index 52aad0ff..f6719549 100644 --- a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp +++ b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp @@ -44,7 +44,7 @@ void UpgradeManager::Init(std::shared_ptr executors) ExecutorPool::Task UpgradeManager::GetTask() { - return [this]{ + return [this] { auto succ = InitLocalCapability(); if (succ) { return; diff --git a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp index 8e1fff33..dbcf5e1b 100644 --- a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp +++ b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp @@ -94,7 +94,7 @@ void Bootstrap::LoadCheckers() } } -void Bootstrap::LoadBackup(std::shared_ptr executors) +void Bootstrap::LoadBackup() { auto *backupRules = ConfigFactory::GetInstance().GetBackupConfig(); if (backupRules == nullptr) { @@ -105,7 +105,6 @@ void Bootstrap::LoadBackup(std::shared_ptr executors) BackupManager::BackupParam backupParam = { backupRules->schedularDelay, backupRules->schedularInternal, backupRules->backupInternal, backupRules->backupNumber}; BackupManager::GetInstance().SetBackupParam(backupParam); - BackupManager::GetInstance().Init(); BackupManager::GetInstance().BackSchedule(); } diff --git a/services/distributeddataservice/service/kvdb/user_delegate.cpp b/services/distributeddataservice/service/kvdb/user_delegate.cpp index e626e803..f7c27316 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.cpp +++ b/services/distributeddataservice/service/kvdb/user_delegate.cpp @@ -180,7 +180,7 @@ void UserDelegate::Init(const std::shared_ptr& executors) ExecutorPool::Task UserDelegate::GeTask() { - return [this]{ + return [this] { auto ret = InitLocalUserMeta(); if (ret) { return; -- Gitee From d2093b3c98db8bd5e4ad44f98c8699d431f6939c Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 14:02:53 +0800 Subject: [PATCH 114/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- services/distributeddataservice/app/BUILD.gn | 1 + .../distributeddataservice/app/src/kvstore_data_service.cpp | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 87f34f1f..1f335e0e 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -40,6 +40,7 @@ config("module_private_config") { include_dirs = [ "//foundation/distributeddatamgr/kv_store/frameworks/common", "//foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/backup/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/bootstrap/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/config/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/crypto/include", diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 8fc9af71..d97c249a 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -249,7 +249,7 @@ void KvStoreDataService::OnStart() Bootstrap::GetInstance().LoadDirectory(); Bootstrap::GetInstance().LoadCheckers(); Bootstrap::GetInstance().LoadNetworks(); - BackupManager::GetInstance().Init(executors_); + DistributedData::BackupManager::GetInstance().Init(executors_); Bootstrap::GetInstance().LoadBackup(); Initialize(); auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); @@ -463,8 +463,8 @@ void KvStoreDataService::ResolveAutoLaunchCompatible(const StoreMetaData &storeM DistributedDB::KvStoreDelegateManager delegateManager("", ""); delegateManager.CloseKvStore(store); }); - constexpr const int CLOSE_STORE_DELAY_TIME = 60; // unit: seconds - executors_->Execute(std::move(delayTask), std::chrono::seconds(CLOSE_STORE_DELAY_TIME)); + constexpr const int closeStoreDelayTime = 60; // unit: seconds + executors_->Execute(std::move(delayTask), std::chrono::seconds(closeStoreDelayTime)); } Status KvStoreDataService::InitNbDbOption(const Options &options, const std::vector &cipherKey, -- Gitee From f51443bcd769fed3934ed3409e44e2549f058558 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 14:13:39 +0800 Subject: [PATCH 115/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- services/distributeddataservice/app/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 1f335e0e..3f8be8fd 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -40,7 +40,6 @@ config("module_private_config") { include_dirs = [ "//foundation/distributeddatamgr/kv_store/frameworks/common", "//foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/backup/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/bootstrap/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/config/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/crypto/include", @@ -61,6 +60,7 @@ config("module_private_config") { "../adapter/include/utils", "../adapter/include/dfx", "../adapter/include", + "../service/backup/include", "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include", # for ipc_core interfaces. -- Gitee From 61a33e105a0459952068a690d7101c4e82cf2f99 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 14:48:26 +0800 Subject: [PATCH 116/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../app/src/kvstore_data_service.cpp | 10 +++++----- .../service/backup/include/backup_manager.h | 5 +++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index d97c249a..17a0d328 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -230,9 +230,9 @@ void KvStoreDataService::OnStart() { ZLOGI("distributeddata service onStart"); EventCenter::Defer defer; - size_t max = 12; - size_t min = 5; - executors_ = std::make_shared(max, min); + constexpr size_t MAX = 12; + constexpr size_t MIN = 5; + executors_ = std::make_shared(MAX, MIN); Reporter::GetInstance()->SetThreadPool(executors_); AccountDelegate::GetInstance()->RegisterHashFunc(Crypto::Sha256); DmAdapter::GetInstance().Init(executors_); @@ -463,8 +463,8 @@ void KvStoreDataService::ResolveAutoLaunchCompatible(const StoreMetaData &storeM DistributedDB::KvStoreDelegateManager delegateManager("", ""); delegateManager.CloseKvStore(store); }); - constexpr const int closeStoreDelayTime = 60; // unit: seconds - executors_->Execute(std::move(delayTask), std::chrono::seconds(closeStoreDelayTime)); + constexpr int CLOSE_STORE_DELAY_TIME = 60; // unit: seconds + executors_->Execute(std::move(delayTask), std::chrono::seconds(CLOSE_STORE_DELAY_TIME)); } Status KvStoreDataService::InitNbDbOption(const Options &options, const std::vector &cipherKey, diff --git a/services/distributeddataservice/service/backup/include/backup_manager.h b/services/distributeddataservice/service/backup/include/backup_manager.h index 0423cef9..9cd5a921 100644 --- a/services/distributeddataservice/service/backup/include/backup_manager.h +++ b/services/distributeddataservice/service/backup/include/backup_manager.h @@ -20,6 +20,7 @@ #include "metadata/secret_key_meta_data.h" #include "metadata/store_meta_data.h" #include "types.h" +#include "visibility.h" namespace OHOS::DistributedData { class BackupManager { public: @@ -35,8 +36,8 @@ public: ROLLBACK, CLEAN_DATA, }; - static BackupManager &GetInstance(); - void Init(std::shared_ptr executors); + API_EXPORT static BackupManager &GetInstance(); + API_EXPORT void Init(std::shared_ptr executors); void BackSchedule(); void SetBackupParam(const BackupParam &backupParam); void RegisterExporter(int32_t type, Exporter exporter); -- Gitee From fd1da86d78dd89f54f6a57ddc13cfa53483e36e0 Mon Sep 17 00:00:00 2001 From: hanlu Date: Sun, 7 May 2023 15:33:14 +0800 Subject: [PATCH 117/152] f Signed-off-by: hanlu --- .../service/data_share/common/rdb_delegate.cpp | 2 +- .../service/data_share/common/seq_strategy.cpp | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 59924717..952b7512 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -28,7 +28,7 @@ RdbDelegate::RdbDelegate(const std::string &dir, int version, int &errCode, bool DefaultOpenCallback callback; store_ = RdbHelper::GetRdbStore(config, version, callback, errCode); if (errCode != E_OK) { - ZLOGE("GetRdbStore failed, errCode is %{public}d, dir is %{public}s", errCode, + ZLOGW("GetRdbStore failed, errCode is %{public}d, dir is %{public}s", errCode, DistributedData::Anonymous::Change(dir).c_str()); } } diff --git a/services/distributeddataservice/service/data_share/common/seq_strategy.cpp b/services/distributeddataservice/service/data_share/common/seq_strategy.cpp index cad92ca6..62700398 100644 --- a/services/distributeddataservice/service/data_share/common/seq_strategy.cpp +++ b/services/distributeddataservice/service/data_share/common/seq_strategy.cpp @@ -20,7 +20,6 @@ namespace OHOS::DataShare { bool SeqStrategy::operator()(std::shared_ptr context) { for (auto &action : actions_) { - // true:success false:terminal if (!(*action)(context)) { return false; } -- Gitee From 4fafd5b7e6ee229665b78568463ae16c4df3955e Mon Sep 17 00:00:00 2001 From: hanlu Date: Sun, 7 May 2023 15:46:29 +0800 Subject: [PATCH 118/152] f Signed-off-by: hanlu --- .../service/data_share/data_share_service_stub.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp index e1fb190d..023f4e75 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp @@ -181,7 +181,7 @@ int32_t DataShareServiceStub::OnRemoteSubscribeRdbData(MessageParcel &data, Mess return -1; } auto remoteObj = data.ReadRemoteObject(); - auto observer = iface_cast(remoteObj); + sptr observer = new (std::nothrow)RdbObserverProxy(remoteObj); if (observer == nullptr) { ZLOGE("obServer is nullptr"); return -1; @@ -250,7 +250,7 @@ int32_t DataShareServiceStub::OnRemoteSubscribePublishedData(MessageParcel &data ZLOGE("read device list failed."); return -1; } - auto observer = iface_cast(data.ReadRemoteObject()); + sptr observer = new (std::nothrow)PublishedDataObserverProxy(data.ReadRemoteObject()); if (observer == nullptr) { ZLOGE("obServer is nullptr"); return -1; -- Gitee From 6402e5fbd70ae919400536505bc4862835d943c0 Mon Sep 17 00:00:00 2001 From: hanlu Date: Sun, 7 May 2023 15:53:45 +0800 Subject: [PATCH 119/152] f Signed-off-by: hanlu --- .../service/data_share/common/db_delegate.cpp | 4 ++-- .../service/data_share/common/db_delegate.h | 3 +-- .../service/data_share/common/rdb_delegate.cpp | 9 +++++---- .../service/data_share/common/rdb_delegate.h | 3 ++- .../service/data_share/strategies/delete_strategy.cpp | 3 +-- .../service/data_share/strategies/insert_strategy.cpp | 3 +-- .../service/data_share/strategies/query_strategy.cpp | 2 +- .../service/data_share/strategies/update_strategy.cpp | 3 +-- 8 files changed, 14 insertions(+), 16 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp index c145abbd..10fddab5 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -18,9 +18,9 @@ #include "kv_delegate.h" #include "rdb_delegate.h" namespace OHOS::DataShare { -std::shared_ptr DBDelegate::Create(const std::string &dir, int version, int &errCode, bool registerFunction) +std::shared_ptr DBDelegate::Create(const std::string &dir, int version) { - return std::make_shared(dir, version, errCode, registerFunction); + return std::make_shared(dir, version); } const std::string KvDBDelegate::TEMPLATE_TABLE = "template_"; diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.h b/services/distributeddataservice/service/data_share/common/db_delegate.h index 677aea2b..68bc62d5 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.h +++ b/services/distributeddataservice/service/data_share/common/db_delegate.h @@ -28,8 +28,7 @@ namespace OHOS::DataShare { class DBDelegate { public: - static std::shared_ptr Create(const std::string &dir, int version, int &errCode, - bool registerFunction = false); + static std::shared_ptr Create(const std::string &dir, int version); virtual int64_t Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket) = 0; virtual int64_t Update(const std::string &tableName, const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket) = 0; diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 952b7512..22fa5b8a 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -21,14 +21,14 @@ namespace OHOS::DataShare { constexpr static int32_t MAX_RESULTSET_COUNT = 16; std::atomic RdbDelegate::resultSetCount = 0; -RdbDelegate::RdbDelegate(const std::string &dir, int version, int &errCode, bool registerFunction) +RdbDelegate::RdbDelegate(const std::string &dir, int version) { RdbStoreConfig config(dir); config.SetCreateNecessary(false); DefaultOpenCallback callback; - store_ = RdbHelper::GetRdbStore(config, version, callback, errCode); - if (errCode != E_OK) { - ZLOGW("GetRdbStore failed, errCode is %{public}d, dir is %{public}s", errCode, + store_ = RdbHelper::GetRdbStore(config, version, callback, errCode_); + if (errCode_ != E_OK) { + ZLOGW("GetRdbStore failed, errCode is %{public}d, dir is %{public}s", errCode_, DistributedData::Anonymous::Change(dir).c_str()); } } @@ -83,6 +83,7 @@ std::shared_ptr RdbDelegate::Query( { if (store_ == nullptr) { ZLOGE("store is null"); + errCode = errCode_; return nullptr; } int count = resultSetCount.fetch_add(1); diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.h b/services/distributeddataservice/service/data_share/common/rdb_delegate.h index 94669369..b8fdcd92 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.h +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.h @@ -31,7 +31,7 @@ namespace OHOS::DataShare { using namespace OHOS::NativeRdb; class RdbDelegate final : public DBDelegate { public: - explicit RdbDelegate(const std::string &dir, int version, int &errCode, bool registerFunction); + explicit RdbDelegate(const std::string &dir, int version); int64_t Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket) override; int64_t Update(const std::string &tableName, const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket) override; @@ -45,6 +45,7 @@ public: private: static std::atomic resultSetCount; std::shared_ptr store_; + int errCode_; }; class DefaultOpenCallback : public RdbOpenCallback { public: diff --git a/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp index bdeb7411..5b0c8ae8 100644 --- a/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp @@ -37,8 +37,7 @@ int64_t DeleteStrategy::Execute(std::shared_ptr context, const DataShar ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); return -1; } - int errCode = E_OK; - auto delegate = DBDelegate::Create(context->calledSourceDir, context->version, errCode); + auto delegate = DBDelegate::Create(context->calledSourceDir, context->version); if (delegate == nullptr) { ZLOGE("Create fail %{public}s %{public}s", context->calledBundleName.c_str(), context->calledTableName.c_str()); return -1; diff --git a/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp index 5a9b782d..dbe4f1ef 100644 --- a/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp @@ -37,8 +37,7 @@ int64_t InsertStrategy::Execute(std::shared_ptr context, const DataShar ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); return -1; } - int errCode = E_OK; - auto delegate = DBDelegate::Create(context->calledSourceDir, context->version, errCode); + auto delegate = DBDelegate::Create(context->calledSourceDir, context->version); if (delegate == nullptr) { ZLOGE("malloc fail %{public}s %{public}s", context->calledBundleName.c_str(), context->calledTableName.c_str()); return -1; diff --git a/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp index e07f4712..befec41b 100644 --- a/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp @@ -39,7 +39,7 @@ std::shared_ptr QueryStrategy::Execute( ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); return nullptr; } - auto delegate = DBDelegate::Create(context->calledSourceDir, context->version, errCode); + auto delegate = DBDelegate::Create(context->calledSourceDir, context->version); if (delegate == nullptr) { ZLOGE("malloc fail %{public}s %{public}s", context->calledBundleName.c_str(), context->calledTableName.c_str()); return nullptr; diff --git a/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp index 0b9e2e9b..c29a389b 100644 --- a/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp @@ -37,8 +37,7 @@ int64_t UpdateStrategy::Execute( ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); return -1; } - int errCode = E_OK; - auto delegate = DBDelegate::Create(context->calledSourceDir, context->version, errCode); + auto delegate = DBDelegate::Create(context->calledSourceDir, context->version); if (delegate == nullptr) { ZLOGE("malloc fail %{public}s %{public}s", context->calledBundleName.c_str(), context->calledTableName.c_str()); return -1; -- Gitee From 76fea2655a15a7ec540b0d64808e19b343e831d3 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 15:54:15 +0800 Subject: [PATCH 120/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../src/account_delegate_normal_impl.cpp | 2 +- .../app/src/kvstore_data_service.cpp | 2 +- .../app/src/kvstore_meta_manager.cpp | 2 +- .../src/session_manager/upgrade_manager.cpp | 2 +- .../app/src/task_manager.cpp | 6 ++--- .../app/src/task_manager.h | 3 ++- .../app/src/uninstaller/uninstaller_impl.cpp | 27 +++++++++++-------- .../app/src/uninstaller/uninstaller_impl.h | 5 ++++ .../distributeddataservice/app/test/BUILD.gn | 1 + .../service/backup/include/backup_manager.h | 6 ++--- .../service/backup/src/backup_manager.cpp | 3 ++- .../service/kvdb/kvstore_sync_manager.cpp | 6 ++--- .../service/kvdb/user_delegate.cpp | 2 +- 13 files changed, 40 insertions(+), 27 deletions(-) diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp index 9e878d93..152c528a 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp +++ b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp @@ -109,7 +109,7 @@ ExecutorPool::Task AccountDelegateNormalImpl::GetTask() ZLOGE("fail to register subscriber!"); return; } - executors_->Execute(GetTask(), std::chrono::seconds(RETRY_WAIT_TIME_S)); + executors_->Schedule(std::chrono::seconds(RETRY_WAIT_TIME_S), GetTask()); }; } diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 17a0d328..eadd1648 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -464,7 +464,7 @@ void KvStoreDataService::ResolveAutoLaunchCompatible(const StoreMetaData &storeM delegateManager.CloseKvStore(store); }); constexpr int CLOSE_STORE_DELAY_TIME = 60; // unit: seconds - executors_->Execute(std::move(delayTask), std::chrono::seconds(CLOSE_STORE_DELAY_TIME)); + executors_->Schedule(std::chrono::seconds(CLOSE_STORE_DELAY_TIME), std::move(delayTask)); } Status KvStoreDataService::InitNbDbOption(const Options &options, const std::vector &cipherKey, diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index cdfb5f05..4ecee416 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -199,7 +199,7 @@ ExecutorPool::Task KvStoreMetaManager::GetTask() ZLOGE("fail to register subscriber!"); return; } - executors_->Execute(GetTask(), std::chrono::seconds(RETRY_INTERVAL)); + executors_->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetTask()); }; } diff --git a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp index f6719549..3d4db8d4 100644 --- a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp +++ b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp @@ -49,7 +49,7 @@ ExecutorPool::Task UpgradeManager::GetTask() if (succ) { return; } - executors_->Execute(GetTask(), std::chrono::milliseconds(RETRY_INTERVAL)); + executors_->Schedule(std::chrono::milliseconds(RETRY_INTERVAL), GetTask()); }; } diff --git a/services/distributeddataservice/app/src/task_manager.cpp b/services/distributeddataservice/app/src/task_manager.cpp index bf6d7432..3281501c 100644 --- a/services/distributeddataservice/app/src/task_manager.cpp +++ b/services/distributeddataservice/app/src/task_manager.cpp @@ -30,7 +30,7 @@ TaskManager::TaskId TaskManager::Execute(const Task &task) } TaskManager::TaskId TaskManager::Execute(const Task &task, Duration delay) { - return executors_->Execute(task, delay); + return executors_->Schedule(delay, task); } TaskManager::TaskId TaskManager::Schedule(const Task &task, Duration interval) { @@ -38,11 +38,11 @@ TaskManager::TaskId TaskManager::Schedule(const Task &task, Duration interval) } TaskManager::TaskId TaskManager::Schedule(const Task &task, Duration delay, Duration interval) { - return executors_->Schedule(task, delay, interval); + return executors_->Schedule(delay, task, interval); } TaskManager::TaskId TaskManager::Schedule(const Task &task, Duration delay, Duration interval, uint64_t times) { - return executors_->Schedule(task, delay, interval, times); + return executors_->Schedule(delay, task, interval, times); } bool TaskManager::Remove(const TaskId &taskId, bool wait) { diff --git a/services/distributeddataservice/app/src/task_manager.h b/services/distributeddataservice/app/src/task_manager.h index e997cab6..ef117d9b 100644 --- a/services/distributeddataservice/app/src/task_manager.h +++ b/services/distributeddataservice/app/src/task_manager.h @@ -16,8 +16,9 @@ #define DISTRIBUTED_DATA_TASK_EXECUTOR_H #include "ithread_pool.h" #include "executor_pool.h" +#include "visibility.h" namespace OHOS::DistributedData { -class TaskManager : public DistributedDB::IThreadPool { +class API_EXPORT TaskManager : public DistributedDB::IThreadPool { public: using TaskId = uint64_t; using Task = std::function; diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index afb290f2..b0801b49 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -108,17 +108,22 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared }; auto subscriber = std::make_shared(info, callback); subscriber_ = subscriber; - executors->Execute([subscriber] { - constexpr int32_t RETRY_TIME = 300; - constexpr int32_t RETRY_INTERVAL = 100 * 1000; - for (BlockInteger retry(RETRY_INTERVAL); retry < RETRY_TIME; ++retry) { - if (CommonEventManager::SubscribeCommonEvent(subscriber)) { - ZLOGI("subscribe uninstall event success"); - break; - } - ZLOGE("subscribe uninstall event fail, try times:%d", static_cast(retry)); - } - }); + executors_->Execute(GetTask()); return Status::SUCCESS; } +ExecutorPool::Task UninstallerImpl::GetTask() +{ + return [this]{ + auto succ = CommonEventManager::SubscribeCommonEvent(subscriber_); + if (succ) { + ZLOGI("subscribe uninstall event success"); + return; + } + ZLOGE("subscribe uninstall event fail, try times:%d", retryTime_); + if (retryTime_++ >= RETRY_TIME) { + return; + } + executors_->Schedule(std::chrono::milliseconds(RETRY_INTERVAL), GetTask()); + }; +} } // namespace OHOS::DistributedKv diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h index 7e5c20bd..837fd1da 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h @@ -45,7 +45,12 @@ public: void UnsubscribeEvent() override; private: + static constexpr int32_t RETRY_TIME = 300; + static constexpr int32_t RETRY_INTERVAL = 100 * 1000; + int32_t retryTime_; + ExecutorPool::Task GetTask(); std::shared_ptr subscriber_ {}; + std::shared_ptr executors_; }; } // namespace OHOS::DistributedKv #endif // DISTRIBUTEDDATAMGR_UNINSTALLER_IMPL_H diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 70fb69c5..3ce6b41d 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -49,6 +49,7 @@ config("module_private_config") { "unittest", "../src/uninstaller", "../src/flowctrl_manager", + "../../service/backup/include", "../../../../interfaces/innerkits/distributeddata", "//third_party/json/single_include", ] diff --git a/services/distributeddataservice/service/backup/include/backup_manager.h b/services/distributeddataservice/service/backup/include/backup_manager.h index 9cd5a921..c9d653b2 100644 --- a/services/distributeddataservice/service/backup/include/backup_manager.h +++ b/services/distributeddataservice/service/backup/include/backup_manager.h @@ -22,7 +22,7 @@ #include "types.h" #include "visibility.h" namespace OHOS::DistributedData { -class BackupManager { +class API_EXPORT BackupManager { public: using Exporter = std::function; struct BackupParam { @@ -36,8 +36,8 @@ public: ROLLBACK, CLEAN_DATA, }; - API_EXPORT static BackupManager &GetInstance(); - API_EXPORT void Init(std::shared_ptr executors); + static BackupManager &GetInstance(); + void Init(std::shared_ptr executors); void BackSchedule(); void SetBackupParam(const BackupParam &backupParam); void RegisterExporter(int32_t type, Exporter exporter); diff --git a/services/distributeddataservice/service/backup/src/backup_manager.cpp b/services/distributeddataservice/service/backup/src/backup_manager.cpp index 68bfd7d5..b884b0d5 100644 --- a/services/distributeddataservice/service/backup/src/backup_manager.cpp +++ b/services/distributeddataservice/service/backup/src/backup_manager.cpp @@ -99,6 +99,7 @@ void BackupManager::BackSchedule() std::chrono::duration internal(schedularInternal_); ZLOGI("BackupManager Schedule start."); executors_->Schedule( + delay, [this]() { if (!CanBackup()) { return; @@ -123,7 +124,7 @@ void BackupManager::BackSchedule() sync(); backupSuccessTime_ = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); }, - delay, internal); + internal); } void BackupManager::DoBackup(const StoreMetaData &meta) diff --git a/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp index 43c99003..ac011bcf 100644 --- a/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp +++ b/services/distributeddataservice/service/kvdb/kvstore_sync_manager.cpp @@ -110,11 +110,11 @@ void KvStoreSyncManager::AddTimer(const TimePoint &expireTime) { ZLOGD("time %lld", expireTime.time_since_epoch().count()); nextScheduleTime_ = expireTime; - executors_->Execute( + executors_->Schedule( + expireTime - std::chrono::steady_clock::now(), [time = expireTime, this]() { Schedule(time); - }, - expireTime - std::chrono::steady_clock::now()); + }); } bool KvStoreSyncManager::GetTimeoutSyncOps(const TimePoint ¤tTime, std::list &syncOps) diff --git a/services/distributeddataservice/service/kvdb/user_delegate.cpp b/services/distributeddataservice/service/kvdb/user_delegate.cpp index f7c27316..97bbe024 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.cpp +++ b/services/distributeddataservice/service/kvdb/user_delegate.cpp @@ -185,7 +185,7 @@ ExecutorPool::Task UserDelegate::GeTask() if (ret) { return; } - executors_->Execute(GeTask(), std::chrono::milliseconds(RETRY_INTERVAL)); + executors_->Schedule(std::chrono::milliseconds(RETRY_INTERVAL), GeTask()); }; } -- Gitee From 104370c62bbe2c5b8a0254ec0768f8ee169614f7 Mon Sep 17 00:00:00 2001 From: e Date: Sun, 7 May 2023 16:02:25 +0800 Subject: [PATCH 121/152] =?UTF-8?q?=E9=83=A8=E4=BB=B6=E5=8C=96=E7=BB=9D?= =?UTF-8?q?=E5=AF=B9=E8=B7=AF=E5=BE=84=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: e --- datamgr_service.gni | 11 ++++++++ .../distributeddataservice/adapter/BUILD.gn | 5 ++-- .../adapter/account/BUILD.gn | 6 ++-- .../adapter/autils/BUILD.gn | 4 +-- .../adapter/broadcaster/BUILD.gn | 6 ++-- .../adapter/communicator/BUILD.gn | 14 +++++----- .../adapter/dfx/BUILD.gn | 4 +-- .../adapter/permission/BUILD.gn | 6 ++-- .../adapter/utils/BUILD.gn | 6 ++-- services/distributeddataservice/app/BUILD.gn | 14 ++++------ .../app/src/checker/BUILD.gn | 6 ++-- .../app/src/flowctrl_manager/BUILD.gn | 9 ++++-- .../app/src/uninstaller/BUILD.gn | 10 +++---- .../distributeddataservice/framework/BUILD.gn | 12 +++++--- .../distributeddataservice/service/BUILD.gn | 28 +++++++++---------- 15 files changed, 76 insertions(+), 65 deletions(-) diff --git a/datamgr_service.gni b/datamgr_service.gni index d1abd9af..e8ca9a86 100644 --- a/datamgr_service.gni +++ b/datamgr_service.gni @@ -11,6 +11,17 @@ # See the License for the specific language governing permissions and # limitations under the License. +distributedfilejs_path = "//foundation/distributeddatamgr/distributedfile" + +kv_store_path = "//foundation/distributeddatamgr/kv_store" + +kv_store_common_path = "${kv_store_path}/frameworks/common" + +kv_store_distributeddb_path = "${kv_store_path}/frameworks/libs/distributeddb" + +dsoftbus_core_path = "//foundation/communication/dsoftbus/core/common/include" + +datashare_path = "//foundation/distributeddatamgr/data_share" declare_args() { datamgr_service_power = true if (!defined(global_parts_info.power_manager_native_powermgr_client) || diff --git a/services/distributeddataservice/adapter/BUILD.gn b/services/distributeddataservice/adapter/BUILD.gn index c1e309f1..338870e1 100644 --- a/services/distributeddataservice/adapter/BUILD.gn +++ b/services/distributeddataservice/adapter/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/ohos.gni") import("//build/ohos_var.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") config("distributeddata_adapter_private_config") { visibility = [ ":*" ] @@ -33,8 +34,7 @@ config("distributeddata_adapter_public_config") { "include/autils", "include/utils", "include", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include/", - "//foundation/distributeddatamgr/kv_store/frameworks/common", + "${kv_store_common_path}", ] } @@ -56,6 +56,7 @@ ohos_shared_library("distributeddata_adapter") { "hitrace_native:hitrace_meter", "hitrace_native:libhitracechain", "hiviewdfx_hilog_native:libhilog", + "kv_store:distributeddata_inner", ] public_configs = [ ":distributeddata_adapter_public_config" ] diff --git a/services/distributeddataservice/adapter/account/BUILD.gn b/services/distributeddataservice/adapter/account/BUILD.gn index 11a20558..205c81e2 100755 --- a/services/distributeddataservice/adapter/account/BUILD.gn +++ b/services/distributeddataservice/adapter/account/BUILD.gn @@ -26,11 +26,7 @@ ohos_static_library("distributeddata_account_static") { "../include/permission", "../include/utils", "./src", - "//commonlibrary/c_utils/base/include", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", - "//foundation/distributeddatamgr/kv_store/frameworks/common", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", - "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", ] cflags_cc = [ "-fvisibility=hidden" ] @@ -44,6 +40,8 @@ ohos_static_library("distributeddata_account_static") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "kv_store:distributeddata_inner", ] if (os_account_part_is_enabled) { diff --git a/services/distributeddataservice/adapter/autils/BUILD.gn b/services/distributeddataservice/adapter/autils/BUILD.gn index ad0f6ebe..48efe8f5 100755 --- a/services/distributeddataservice/adapter/autils/BUILD.gn +++ b/services/distributeddataservice/adapter/autils/BUILD.gn @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import("//build/ohos.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") ohos_static_library("distributeddata_autils_static") { sources = [ @@ -25,9 +26,8 @@ ohos_static_library("distributeddata_autils_static") { include_dirs = [ "../include/autils", "../include/log", - "//commonlibrary/c_utils/base/include", "../include/dfx", - "//foundation/distributeddatamgr/kv_store/frameworks/common", + "${kv_store_common_path}", ] cflags_cc = [ "-fvisibility=hidden" ] diff --git a/services/distributeddataservice/adapter/broadcaster/BUILD.gn b/services/distributeddataservice/adapter/broadcaster/BUILD.gn index 78c9885b..50f7e890 100755 --- a/services/distributeddataservice/adapter/broadcaster/BUILD.gn +++ b/services/distributeddataservice/adapter/broadcaster/BUILD.gn @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import("//build/ohos.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") ohos_static_library("distributeddata_broadcaster_static") { sources = [ @@ -22,9 +23,7 @@ ohos_static_library("distributeddata_broadcaster_static") { "../include/broadcaster", "../include/log", "./src", - "//commonlibrary/c_utils/base/include", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", - "//foundation/distributeddatamgr/kv_store/frameworks/common", + "${kv_store_common_path}", ] cflags_cc = [ "-fvisibility=hidden" ] @@ -38,6 +37,7 @@ ohos_static_library("distributeddata_broadcaster_static") { "common_event_service:cesfwk_innerkits", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "kv_store:distributeddata_inner", ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/services/distributeddataservice/adapter/communicator/BUILD.gn b/services/distributeddataservice/adapter/communicator/BUILD.gn index c3e9a4a7..bdd36260 100755 --- a/services/distributeddataservice/adapter/communicator/BUILD.gn +++ b/services/distributeddataservice/adapter/communicator/BUILD.gn @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import("//build/ohos.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") ohos_static_library("distributeddata_communicator_static") { sources = [ "src/app_pipe_handler.cpp", @@ -33,18 +34,16 @@ ohos_static_library("distributeddata_communicator_static") { ] include_dirs = [ - "//commonlibrary/c_utils/base/include", "../include/communicator", "../include/dfx", "../include/log", "../include/autils", "../include/utils", - "//foundation/communication/dsoftbus/core/common/include", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", - "//foundation/distributeddatamgr/kv_store/frameworks/common", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/interfaces/include", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/include", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/interfaces/include/relational", + "${dsoftbus_core_path}", + "${kv_store_common_path}", + "${kv_store_distributeddb_path}/interfaces/include", + "${kv_store_distributeddb_path}/include", + "${kv_store_distributeddb_path}/interfaces/include/relational", ] cflags_cc = [ "-fvisibility=hidden" ] @@ -59,6 +58,7 @@ ohos_static_library("distributeddata_communicator_static") { "device_manager:devicemanagersdk", "dsoftbus:softbus_client", "hiviewdfx_hilog_native:libhilog", + "kv_store:distributeddata_inner", ] subsystem_name = "distributeddatamgr" diff --git a/services/distributeddataservice/adapter/dfx/BUILD.gn b/services/distributeddataservice/adapter/dfx/BUILD.gn index bbffd1ac..c137b61f 100644 --- a/services/distributeddataservice/adapter/dfx/BUILD.gn +++ b/services/distributeddataservice/adapter/dfx/BUILD.gn @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import("//build/ohos.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") ohos_static_library("distributeddata_dfx_static") { sources = [ @@ -34,8 +35,7 @@ ohos_static_library("distributeddata_dfx_static") { "../include/dfx", "../include/log", "../include/autils", - "//commonlibrary/c_utils/base/include", - "//foundation/distributeddatamgr/kv_store/frameworks/common", + "${kv_store_common_path}", "//third_party/openssl/include/", ] diff --git a/services/distributeddataservice/adapter/permission/BUILD.gn b/services/distributeddataservice/adapter/permission/BUILD.gn index 9d63f401..303cac4b 100644 --- a/services/distributeddataservice/adapter/permission/BUILD.gn +++ b/services/distributeddataservice/adapter/permission/BUILD.gn @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import("//build/ohos.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") ohos_static_library("distributeddata_permission_static") { sources = [ "src/permission_validator.cpp" ] @@ -20,9 +21,7 @@ ohos_static_library("distributeddata_permission_static") { include_dirs = [ "../include/permission", "../include/utils", - "//commonlibrary/c_utils/base/include", - "//foundation/distributeddatamgr/kv_store/frameworks/common", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", + "${kv_store_common_path}", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", ] @@ -36,6 +35,7 @@ ohos_static_library("distributeddata_permission_static") { "access_token:libaccesstoken_sdk", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", + "kv_store:distributeddata_inner", ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/services/distributeddataservice/adapter/utils/BUILD.gn b/services/distributeddataservice/adapter/utils/BUILD.gn index 6539b7ab..078f90c7 100755 --- a/services/distributeddataservice/adapter/utils/BUILD.gn +++ b/services/distributeddataservice/adapter/utils/BUILD.gn @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import("//build/ohos.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") ohos_static_library("distributeddata_utils_static") { sources = [ "src/kvstore_utils.cpp" ] @@ -26,16 +27,15 @@ ohos_static_library("distributeddata_utils_static") { "../include/utils", "../include/log", "../include/communicator", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", - "//commonlibrary/c_utils/base/include", "../include/dfx", - "//foundation/distributeddatamgr/kv_store/frameworks/common", + "${kv_store_common_path}", ] ldflags = [ "-Wl,--exclude-libs,ALL" ] external_deps = [ "c_utils:utils", "hiviewdfx_hilog_native:libhilog", + "kv_store:distributeddata_inner", ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 48797a85..20d0751c 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -38,8 +38,7 @@ ohos_sa_profile("distributeddata_profile") { config("module_private_config") { visibility = [ ":*" ] include_dirs = [ - "//foundation/distributeddatamgr/kv_store/frameworks/common", - "//foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include", + "${kv_store_common_path}", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/bootstrap/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/config/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/crypto/include", @@ -50,9 +49,7 @@ config("module_private_config") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/backup/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/kvdb", - "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/mod_securitylabel", - "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include", - "//utils/system/safwk/native/include", + "${distributedfilejs_path}/interfaces/kits/js/src/mod_securitylabel", "../adapter/include/account", "../adapter/include/permission", "../adapter/include/uninstaller", @@ -60,10 +57,8 @@ config("module_private_config") { "../adapter/include/utils", "../adapter/include/dfx", "../adapter/include", - "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include", # for ipc_core interfaces. - "//commonlibrary/c_utils/base/include", "include", "src", "src/security", @@ -109,8 +104,7 @@ ohos_shared_library("distributeddataservice") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src/uninstaller:distributeddata_uninstaller_static", "//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", + "${kv_store_distributeddb_path}", ] external_deps = [ @@ -121,11 +115,13 @@ ohos_shared_library("distributeddataservice") { "bundle_framework:appexecfwk_core", "c_utils:utils", "dataclassification:data_transit_mgr", + "device_manager:devicemanagersdk", "hisysevent_native:libhisysevent", "hitrace_native:hitrace_meter", "hitrace_native:libhitracechain", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "kv_store:distributeddata_inner", "safwk:system_ability_fwk", "samgr:samgr_proxy", ] diff --git a/services/distributeddataservice/app/src/checker/BUILD.gn b/services/distributeddataservice/app/src/checker/BUILD.gn index 48afce56..b50e5b77 100644 --- a/services/distributeddataservice/app/src/checker/BUILD.gn +++ b/services/distributeddataservice/app/src/checker/BUILD.gn @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import("//build/ohos.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") ohos_static_library("distributeddata_checker_static") { sources = [ @@ -21,9 +22,7 @@ ohos_static_library("distributeddata_checker_static") { cflags_cc = [ "-fvisibility=hidden" ] include_dirs = [ - "//commonlibrary/c_utils/base/include", - "//foundation/distributeddatamgr/kv_store/frameworks/common", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", + "${kv_store_common_path}", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", ] @@ -42,6 +41,7 @@ ohos_static_library("distributeddata_checker_static") { "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "kv_store:distributeddata_inner", "samgr:samgr_proxy", ] subsystem_name = "distributeddatamgr" diff --git a/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn b/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn index c17ecd1e..e3c1d300 100755 --- a/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn +++ b/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/ohos.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") ohos_static_library("distributeddata_flowctrl_static") { sources = [ "kvstore_flowctrl_manager.cpp" ] @@ -19,15 +20,17 @@ ohos_static_library("distributeddata_flowctrl_static") { include_dirs = [ "../../../adapter/include/account", "../../src", - "//foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", + "${kv_store_path}/frameworks/innerkitsimpl/distributeddatafwk/include", "//third_party/json/single_include", "//commonlibrary/c_utils/base/include", ] cflags_cc = [ "-fvisibility=hidden" ] - external_deps = [ "c_utils:utils" ] + external_deps = [ + "c_utils:utils", + "kv_store:distributeddata_inner", + ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/services/distributeddataservice/app/src/uninstaller/BUILD.gn b/services/distributeddataservice/app/src/uninstaller/BUILD.gn index 85a133f2..48c870ea 100755 --- a/services/distributeddataservice/app/src/uninstaller/BUILD.gn +++ b/services/distributeddataservice/app/src/uninstaller/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/ohos.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") ohos_static_library("distributeddata_uninstaller_static") { sources = [ @@ -22,14 +23,11 @@ ohos_static_library("distributeddata_uninstaller_static") { include_dirs = [ "../../../adapter/include/account", "../../src", - "//foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", + "${kv_store_path}/frameworks/innerkitsimpl/distributeddatafwk/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/kvdb", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/permission/include", - "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include", "//third_party/json/single_include", - "//commonlibrary/c_utils/base/include", ] cflags_cc = [ "-fvisibility=hidden" ] @@ -38,12 +36,14 @@ ohos_static_library("distributeddata_uninstaller_static") { "//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", + "${kv_store_distributeddb_path}:distributeddb", ] external_deps = [ "bundle_framework:appexecfwk_base", "c_utils:utils", + "device_manager:devicemanagersdk", + "kv_store:distributeddata_inner", # "ces:libcommonevent", "ability_base:want", diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index 4ce626b7..4d60b8f9 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/ohos.gni") import("//build/ohos_var.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") group("build_module") { deps = [ ":distributeddatasvcfwk" ] @@ -23,10 +24,10 @@ config("module_public_config") { "//third_party/json/single_include", "//third_party/openssl/include/", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/include", - "//foundation/distributeddatamgr/kv_store/frameworks/common", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", + "${kv_store_common_path}", "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/interfaces/include/", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/include/", + "${kv_store_distributeddb_path}/interfaces/include/", + "${kv_store_distributeddb_path}/include/", ] } @@ -70,7 +71,10 @@ ohos_shared_library("distributeddatasvcfwk") { deps = [ "//third_party/openssl:libcrypto_shared" ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "kv_store:distributeddata_inner", + ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index d05b54c5..409afc7b 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/ohos.gni") import("//build/ohos_var.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") group("build_module") { deps = [ ":distributeddatasvc" ] @@ -34,25 +35,22 @@ config("module_public_config") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", - "//foundation/distributeddatamgr/data_share/frameworks/native/common/include", - "//foundation/distributeddatamgr/data_share/interfaces/inner_api/common/include", - "//foundation/distributeddatamgr/data_share/interfaces/inner_api/consumer/include", - "//foundation/distributeddatamgr/kv_store/frameworks/common", - "//foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include", - "//foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/kvdb/include", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/include/", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/interfaces/include/", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/interfaces/include/relational", - "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include", - "//foundation/bundlemanager/bundle_framework/interfaces/inner_api/appexecfwk_base/include", + "${datashare_path}/frameworks/native/common/include", + "${datashare_path}/interfaces/inner_api/common/include", + "${datashare_path}/interfaces/inner_api/consumer/include", + "${kv_store_common_path}", + "${kv_store_path}/frameworks/innerkitsimpl/distributeddatafwk/include", + "${kv_store_path}/frameworks/innerkitsimpl/kvdb/include", + "${kv_store_distributeddb_path}/include/", + "${kv_store_distributeddb_path}/interfaces/include/", + "${kv_store_distributeddb_path}/interfaces/include/relational", ] } ohos_shared_library("distributeddatasvc") { include_dirs = [ "../../../../data_object/frameworks/innerkitsimpl/include", - "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/rdb/include", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/include/distributeddb", + "${kv_store_distributeddb_path}", ] sources = [ "backup/src/backup_manager.cpp", @@ -112,8 +110,7 @@ ohos_shared_library("distributeddatasvc") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb:distributeddb", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata:distributeddata_inner", + "${kv_store_distributeddb_path}:distributeddb", ] external_deps = [ @@ -129,6 +126,7 @@ ohos_shared_library("distributeddatasvc") { "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", + "kv_store:distributeddata_inner", "relational_store:native_rdb", "relational_store:rdb_data_share_adapter", "resource_management:global_resmgr", -- Gitee From 55bcfb581f91ba77997dea3cee28f500d5eb4f9c Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 16:23:35 +0800 Subject: [PATCH 122/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- services/distributeddataservice/app/src/task_manager.cpp | 4 ++-- .../app/src/uninstaller/uninstaller_impl.cpp | 2 +- .../service/backup/src/backup_manager.cpp | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/services/distributeddataservice/app/src/task_manager.cpp b/services/distributeddataservice/app/src/task_manager.cpp index 3281501c..1332fc20 100644 --- a/services/distributeddataservice/app/src/task_manager.cpp +++ b/services/distributeddataservice/app/src/task_manager.cpp @@ -38,11 +38,11 @@ TaskManager::TaskId TaskManager::Schedule(const Task &task, Duration interval) } TaskManager::TaskId TaskManager::Schedule(const Task &task, Duration delay, Duration interval) { - return executors_->Schedule(delay, task, interval); + return executors_->Schedule(task, delay, interval); } TaskManager::TaskId TaskManager::Schedule(const Task &task, Duration delay, Duration interval, uint64_t times) { - return executors_->Schedule(delay, task, interval, times); + return executors_->Schedule(task, delay, interval, times); } bool TaskManager::Remove(const TaskId &taskId, bool wait) { diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index b0801b49..13570c08 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -113,7 +113,7 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared } ExecutorPool::Task UninstallerImpl::GetTask() { - return [this]{ + return [this] { auto succ = CommonEventManager::SubscribeCommonEvent(subscriber_); if (succ) { ZLOGI("subscribe uninstall event success"); diff --git a/services/distributeddataservice/service/backup/src/backup_manager.cpp b/services/distributeddataservice/service/backup/src/backup_manager.cpp index b884b0d5..68bfd7d5 100644 --- a/services/distributeddataservice/service/backup/src/backup_manager.cpp +++ b/services/distributeddataservice/service/backup/src/backup_manager.cpp @@ -99,7 +99,6 @@ void BackupManager::BackSchedule() std::chrono::duration internal(schedularInternal_); ZLOGI("BackupManager Schedule start."); executors_->Schedule( - delay, [this]() { if (!CanBackup()) { return; @@ -124,7 +123,7 @@ void BackupManager::BackSchedule() sync(); backupSuccessTime_ = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); }, - internal); + delay, internal); } void BackupManager::DoBackup(const StoreMetaData &meta) -- Gitee From 5673c645add82e2902d6c5ad3f044fc1888bd138 Mon Sep 17 00:00:00 2001 From: e Date: Sun, 7 May 2023 16:34:19 +0800 Subject: [PATCH 123/152] format_check Signed-off-by: e --- services/distributeddataservice/adapter/utils/BUILD.gn | 2 +- services/distributeddataservice/app/BUILD.gn | 2 +- .../distributeddataservice/app/src/flowctrl_manager/BUILD.gn | 4 ++-- services/distributeddataservice/app/src/uninstaller/BUILD.gn | 2 +- services/distributeddataservice/framework/BUILD.gn | 2 +- services/distributeddataservice/service/BUILD.gn | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/adapter/utils/BUILD.gn b/services/distributeddataservice/adapter/utils/BUILD.gn index 078f90c7..afbc1a79 100755 --- a/services/distributeddataservice/adapter/utils/BUILD.gn +++ b/services/distributeddataservice/adapter/utils/BUILD.gn @@ -35,7 +35,7 @@ ohos_static_library("distributeddata_utils_static") { external_deps = [ "c_utils:utils", "hiviewdfx_hilog_native:libhilog", - "kv_store:distributeddata_inner", + "kv_store:distributeddata_inner", ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 20d0751c..1b7c65de 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -96,6 +96,7 @@ ohos_shared_library("distributeddataservice") { configs = [ ":module_private_config" ] deps = [ + "${kv_store_distributeddb_path}", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", @@ -104,7 +105,6 @@ ohos_shared_library("distributeddataservice") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/src/uninstaller:distributeddata_uninstaller_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:distributeddatasvc", - "${kv_store_distributeddb_path}", ] external_deps = [ diff --git a/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn b/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn index e3c1d300..dd24dc9a 100755 --- a/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn +++ b/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn @@ -27,9 +27,9 @@ ohos_static_library("distributeddata_flowctrl_static") { cflags_cc = [ "-fvisibility=hidden" ] - external_deps = [ + external_deps = [ "c_utils:utils", - "kv_store:distributeddata_inner", + "kv_store:distributeddata_inner", ] subsystem_name = "distributeddatamgr" diff --git a/services/distributeddataservice/app/src/uninstaller/BUILD.gn b/services/distributeddataservice/app/src/uninstaller/BUILD.gn index 48c870ea..b706cf2e 100755 --- a/services/distributeddataservice/app/src/uninstaller/BUILD.gn +++ b/services/distributeddataservice/app/src/uninstaller/BUILD.gn @@ -33,10 +33,10 @@ ohos_static_library("distributeddata_uninstaller_static") { cflags_cc = [ "-fvisibility=hidden" ] deps = [ + "${kv_store_distributeddb_path}:distributeddb", "//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", - "${kv_store_distributeddb_path}:distributeddb", ] external_deps = [ diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index 4d60b8f9..68e10b22 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -71,7 +71,7 @@ ohos_shared_library("distributeddatasvcfwk") { deps = [ "//third_party/openssl:libcrypto_shared" ] - external_deps = [ + external_deps = [ "hiviewdfx_hilog_native:libhilog", "kv_store:distributeddata_inner", ] diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 409afc7b..cace3740 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -107,10 +107,10 @@ ohos_shared_library("distributeddatasvc") { configs = [ ":module_public_config" ] deps = [ + "${kv_store_distributeddb_path}:distributeddb", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", - "${kv_store_distributeddb_path}:distributeddb", ] external_deps = [ -- Gitee From 4fba350f2e359106d2b9890c27e362fd222b21c5 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 16:40:30 +0800 Subject: [PATCH 124/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../service/rdb/rdb_service_impl.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 9d5e495d..9d371f2a 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -231,8 +231,8 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa if (it != syncers.end()) { syncer = it->second; if (!param.isEncrypt_ || param.password_.empty()) { - executors_->Execute( - [this, syncer] { SyncerTimeout(syncer); }, std::chrono::milliseconds(SYNCER_TIMEOUT)); + executors_->Schedule( + std::chrono::milliseconds(SYNCER_TIMEOUT), [this, syncer] { SyncerTimeout(syncer); }); return true; } syncers.erase(storeId); @@ -256,8 +256,8 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa syncers[storeId] = syncer_; syncer = syncer_; syncerNum_++; - executors_->Execute( - [this, syncer] { SyncerTimeout(syncer); }, std::chrono::milliseconds(SYNCER_TIMEOUT)); + executors_->Schedule( + std::chrono::milliseconds(SYNCER_TIMEOUT), [this, syncer] { SyncerTimeout(syncer); }); return !syncers.empty(); }); -- Gitee From bd8b0cafd1beb20a8e6ef45e5fb110104456d92d Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 16:55:52 +0800 Subject: [PATCH 125/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- services/distributeddataservice/service/kvdb/store_cache.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/store_cache.cpp b/services/distributeddataservice/service/kvdb/store_cache.cpp index 2c7151ca..093e8e5a 100644 --- a/services/distributeddataservice/service/kvdb/store_cache.cpp +++ b/services/distributeddataservice/service/kvdb/store_cache.cpp @@ -64,7 +64,7 @@ StoreCache::Store StoreCache::GetStore(const StoreMetaData &data, std::shared_pt return !stores.empty(); }); - executors_->Execute(std::bind(&StoreCache::GarbageCollect, this), std::chrono::minutes(INTERVAL)); + executors_->Schedule(std::chrono::minutes(INTERVAL), std::bind(&StoreCache::GarbageCollect, this)); return store; } @@ -131,8 +131,7 @@ void StoreCache::GarbageCollect() }); if (!stores_.Empty()) { ZLOGD("stores size:%{public}zu", stores_.Size()); - executors_->Execute(std::bind(&StoreCache::GarbageCollect, this), - std::chrono::minutes(INTERVAL)); + executors_->Schedule(std::chrono::minutes(INTERVAL), std::bind(&StoreCache::GarbageCollect, this)); } } -- Gitee From cecd7969863258c7d78685f943ca1883e02edde3 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 17:10:38 +0800 Subject: [PATCH 126/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../service/object/object_manager.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/service/object/object_manager.cpp b/services/distributeddataservice/service/object/object_manager.cpp index 2d645dd6..a911a5f8 100644 --- a/services/distributeddataservice/service/object/object_manager.cpp +++ b/services/distributeddataservice/service/object/object_manager.cpp @@ -351,11 +351,9 @@ void ObjectStoreManager::FlushClosedStore() auto status = kvStoreDelegateManager_->CloseKvStore(delegate_); if (status != DistributedDB::DBStatus::OK) { int timeOut = 1000; - executors_->Execute( - [this]() { - FlushClosedStore(); - }, - std::chrono::milliseconds(timeOut)); + executors_->Schedule(std::chrono::milliseconds(timeOut), [this]() { + FlushClosedStore(); + }); ZLOGE("GetEntries fail %{public}d", status); return; } @@ -569,7 +567,7 @@ int64_t ObjectStoreManager::GetTime(const std::string &key) void ObjectStoreManager::CloseAfterMinute() { - executors_->Execute(std::bind(&ObjectStoreManager::Close, this), std::chrono::minutes(INTERVAL)); + executors_->Schedule(std::chrono::minutes(INTERVAL), std::bind(&ObjectStoreManager::Close, this)); } std::string ObjectStoreManager::GetBundleName(const std::string &key) -- Gitee From 619b95c6595add2b705c61579f8c13185958cbe8 Mon Sep 17 00:00:00 2001 From: e Date: Sun, 7 May 2023 17:32:13 +0800 Subject: [PATCH 127/152] change 5_7 Signed-off-by: e --- datamgr_service.gni | 4 ++++ services/distributeddataservice/adapter/BUILD.gn | 2 +- services/distributeddataservice/adapter/account/BUILD.gn | 5 +++-- .../distributeddataservice/adapter/broadcaster/BUILD.gn | 2 +- .../distributeddataservice/adapter/communicator/BUILD.gn | 2 +- .../distributeddataservice/adapter/permission/BUILD.gn | 2 +- services/distributeddataservice/adapter/utils/BUILD.gn | 2 +- services/distributeddataservice/app/BUILD.gn | 3 ++- services/distributeddataservice/app/src/checker/BUILD.gn | 2 +- .../app/src/flowctrl_manager/BUILD.gn | 6 ++---- .../distributeddataservice/app/src/uninstaller/BUILD.gn | 4 ++-- services/distributeddataservice/framework/BUILD.gn | 7 ++----- 12 files changed, 21 insertions(+), 20 deletions(-) diff --git a/datamgr_service.gni b/datamgr_service.gni index e8ca9a86..6ec402a1 100644 --- a/datamgr_service.gni +++ b/datamgr_service.gni @@ -22,6 +22,10 @@ kv_store_distributeddb_path = "${kv_store_path}/frameworks/libs/distributeddb" dsoftbus_core_path = "//foundation/communication/dsoftbus/core/common/include" datashare_path = "//foundation/distributeddatamgr/data_share" + +ipc_core_path = "//foundation/communication/ipc/interfaces/innerkits/ipc_core" + +device_manager_path = "//foundation/distributedhardware/device_manager" declare_args() { datamgr_service_power = true if (!defined(global_parts_info.power_manager_native_powermgr_client) || diff --git a/services/distributeddataservice/adapter/BUILD.gn b/services/distributeddataservice/adapter/BUILD.gn index 338870e1..caf33746 100644 --- a/services/distributeddataservice/adapter/BUILD.gn +++ b/services/distributeddataservice/adapter/BUILD.gn @@ -34,6 +34,7 @@ config("distributeddata_adapter_public_config") { "include/autils", "include/utils", "include", + "${kv_store_path}/interfaces/innerkits/distributeddata/include/", "${kv_store_common_path}", ] } @@ -56,7 +57,6 @@ ohos_shared_library("distributeddata_adapter") { "hitrace_native:hitrace_meter", "hitrace_native:libhitracechain", "hiviewdfx_hilog_native:libhilog", - "kv_store:distributeddata_inner", ] public_configs = [ ":distributeddata_adapter_public_config" ] diff --git a/services/distributeddataservice/adapter/account/BUILD.gn b/services/distributeddataservice/adapter/account/BUILD.gn index 205c81e2..d2a449b8 100755 --- a/services/distributeddataservice/adapter/account/BUILD.gn +++ b/services/distributeddataservice/adapter/account/BUILD.gn @@ -26,7 +26,10 @@ ohos_static_library("distributeddata_account_static") { "../include/permission", "../include/utils", "./src", + "${kv_store_common_path}", + "${kv_store_path}/interfaces/innerkits/distributeddata/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", + "${ipc_core_path}/include", ] cflags_cc = [ "-fvisibility=hidden" ] @@ -40,8 +43,6 @@ ohos_static_library("distributeddata_account_static") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_single", - "kv_store:distributeddata_inner", ] if (os_account_part_is_enabled) { diff --git a/services/distributeddataservice/adapter/broadcaster/BUILD.gn b/services/distributeddataservice/adapter/broadcaster/BUILD.gn index 50f7e890..fe0fc348 100755 --- a/services/distributeddataservice/adapter/broadcaster/BUILD.gn +++ b/services/distributeddataservice/adapter/broadcaster/BUILD.gn @@ -23,6 +23,7 @@ ohos_static_library("distributeddata_broadcaster_static") { "../include/broadcaster", "../include/log", "./src", + "${kv_store_path}/interfaces/innerkits/distributeddata/include", "${kv_store_common_path}", ] @@ -37,7 +38,6 @@ ohos_static_library("distributeddata_broadcaster_static") { "common_event_service:cesfwk_innerkits", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", - "kv_store:distributeddata_inner", ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/services/distributeddataservice/adapter/communicator/BUILD.gn b/services/distributeddataservice/adapter/communicator/BUILD.gn index bdd36260..be70a5dd 100755 --- a/services/distributeddataservice/adapter/communicator/BUILD.gn +++ b/services/distributeddataservice/adapter/communicator/BUILD.gn @@ -44,6 +44,7 @@ ohos_static_library("distributeddata_communicator_static") { "${kv_store_distributeddb_path}/interfaces/include", "${kv_store_distributeddb_path}/include", "${kv_store_distributeddb_path}/interfaces/include/relational", + "${kv_store_path}/interfaces/innerkits/distributeddata/include", ] cflags_cc = [ "-fvisibility=hidden" ] @@ -58,7 +59,6 @@ ohos_static_library("distributeddata_communicator_static") { "device_manager:devicemanagersdk", "dsoftbus:softbus_client", "hiviewdfx_hilog_native:libhilog", - "kv_store:distributeddata_inner", ] subsystem_name = "distributeddatamgr" diff --git a/services/distributeddataservice/adapter/permission/BUILD.gn b/services/distributeddataservice/adapter/permission/BUILD.gn index 303cac4b..48e77927 100644 --- a/services/distributeddataservice/adapter/permission/BUILD.gn +++ b/services/distributeddataservice/adapter/permission/BUILD.gn @@ -22,6 +22,7 @@ ohos_static_library("distributeddata_permission_static") { "../include/permission", "../include/utils", "${kv_store_common_path}", + "${kv_store_path}/interfaces/innerkits/distributeddata/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", ] @@ -35,7 +36,6 @@ ohos_static_library("distributeddata_permission_static") { "access_token:libaccesstoken_sdk", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", - "kv_store:distributeddata_inner", ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/services/distributeddataservice/adapter/utils/BUILD.gn b/services/distributeddataservice/adapter/utils/BUILD.gn index afbc1a79..b57d430b 100755 --- a/services/distributeddataservice/adapter/utils/BUILD.gn +++ b/services/distributeddataservice/adapter/utils/BUILD.gn @@ -29,13 +29,13 @@ ohos_static_library("distributeddata_utils_static") { "../include/communicator", "../include/dfx", "${kv_store_common_path}", + "${kv_store_path}/interfaces/innerkits/distributeddata/include", ] ldflags = [ "-Wl,--exclude-libs,ALL" ] external_deps = [ "c_utils:utils", "hiviewdfx_hilog_native:libhilog", - "kv_store:distributeddata_inner", ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 1b7c65de..9f7aa536 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -39,6 +39,7 @@ config("module_private_config") { visibility = [ ":*" ] include_dirs = [ "${kv_store_common_path}", + "${kv_store_path}/frameworks/innerkitsimpl/distributeddatafwk/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/bootstrap/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/config/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/crypto/include", @@ -49,6 +50,7 @@ config("module_private_config") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/backup/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/kvdb", + "${device_manager_path}/interfaces/inner_kits/native_cpp/include", "${distributedfilejs_path}/interfaces/kits/js/src/mod_securitylabel", "../adapter/include/account", "../adapter/include/permission", @@ -115,7 +117,6 @@ ohos_shared_library("distributeddataservice") { "bundle_framework:appexecfwk_core", "c_utils:utils", "dataclassification:data_transit_mgr", - "device_manager:devicemanagersdk", "hisysevent_native:libhisysevent", "hitrace_native:hitrace_meter", "hitrace_native:libhitracechain", diff --git a/services/distributeddataservice/app/src/checker/BUILD.gn b/services/distributeddataservice/app/src/checker/BUILD.gn index b50e5b77..1f59492c 100644 --- a/services/distributeddataservice/app/src/checker/BUILD.gn +++ b/services/distributeddataservice/app/src/checker/BUILD.gn @@ -24,6 +24,7 @@ ohos_static_library("distributeddata_checker_static") { include_dirs = [ "${kv_store_common_path}", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", + "${kv_store_path}/interfaces/innerkits/distributeddata/include", ] if (build_public_version) { @@ -41,7 +42,6 @@ ohos_static_library("distributeddata_checker_static") { "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", - "kv_store:distributeddata_inner", "samgr:samgr_proxy", ] subsystem_name = "distributeddatamgr" diff --git a/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn b/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn index dd24dc9a..61f30728 100755 --- a/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn +++ b/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn @@ -21,16 +21,14 @@ ohos_static_library("distributeddata_flowctrl_static") { "../../../adapter/include/account", "../../src", "${kv_store_path}/frameworks/innerkitsimpl/distributeddatafwk/include", + "${kv_store_path}/interfaces/innerkits/distributeddata/include", "//third_party/json/single_include", "//commonlibrary/c_utils/base/include", ] cflags_cc = [ "-fvisibility=hidden" ] - external_deps = [ - "c_utils:utils", - "kv_store:distributeddata_inner", - ] + external_deps = [ "c_utils:utils" ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/services/distributeddataservice/app/src/uninstaller/BUILD.gn b/services/distributeddataservice/app/src/uninstaller/BUILD.gn index b706cf2e..04daeef7 100755 --- a/services/distributeddataservice/app/src/uninstaller/BUILD.gn +++ b/services/distributeddataservice/app/src/uninstaller/BUILD.gn @@ -24,9 +24,11 @@ ohos_static_library("distributeddata_uninstaller_static") { "../../../adapter/include/account", "../../src", "${kv_store_path}/frameworks/innerkitsimpl/distributeddatafwk/include", + "${kv_store_path}/interfaces/innerkits/distributeddata/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework/include", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/kvdb", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/permission/include", + "${device_manager_path}/interfaces/inner_kits/native_cpp/include", "//third_party/json/single_include", ] @@ -42,8 +44,6 @@ ohos_static_library("distributeddata_uninstaller_static") { external_deps = [ "bundle_framework:appexecfwk_base", "c_utils:utils", - "device_manager:devicemanagersdk", - "kv_store:distributeddata_inner", # "ces:libcommonevent", "ability_base:want", diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index 68e10b22..d26ce334 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -24,8 +24,8 @@ config("module_public_config") { "//third_party/json/single_include", "//third_party/openssl/include/", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/include", + "${kv_store_path}/interfaces/innerkits/distributeddata/include", "${kv_store_common_path}", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/interfaces/include/", "${kv_store_distributeddb_path}/interfaces/include/", "${kv_store_distributeddb_path}/include/", ] @@ -71,10 +71,7 @@ ohos_shared_library("distributeddatasvcfwk") { deps = [ "//third_party/openssl:libcrypto_shared" ] - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "kv_store:distributeddata_inner", - ] + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" -- Gitee From c12d89c84bceb7da97bb265ad3b19fb2c39d04db Mon Sep 17 00:00:00 2001 From: e Date: Sun, 7 May 2023 17:56:16 +0800 Subject: [PATCH 128/152] change 5_7_2 Signed-off-by: e --- services/distributeddataservice/app/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 9f7aa536..73ccab66 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -98,7 +98,7 @@ ohos_shared_library("distributeddataservice") { configs = [ ":module_private_config" ] deps = [ - "${kv_store_distributeddb_path}", + "${kv_store_distributeddb_path:distributeddb}", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", -- Gitee From 31cab45a4592dca248e28eba18f0ccfd692030fc Mon Sep 17 00:00:00 2001 From: e Date: Sun, 7 May 2023 18:14:45 +0800 Subject: [PATCH 129/152] 5_7_3 Signed-off-by: e --- services/distributeddataservice/app/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 73ccab66..4ca7f782 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -98,7 +98,7 @@ ohos_shared_library("distributeddataservice") { configs = [ ":module_private_config" ] deps = [ - "${kv_store_distributeddb_path:distributeddb}", + "${kv_store_distributeddb_path}:distributeddb", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", -- Gitee From 1145b3e3d25ca52b2aa40bf5b4f4e27483d6246d Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 18:14:52 +0800 Subject: [PATCH 130/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../distributeddataservice/app/src/kvstore_data_service.cpp | 2 +- services/distributeddataservice/app/src/task_manager.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index eadd1648..355a543a 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -21,7 +21,6 @@ #include "auth_delegate.h" #include "auto_launch_export.h" -#include "backup_manager.h" #include "bootstrap.h" #include "checker/checker_manager.h" #include "communication_provider.h" @@ -53,6 +52,7 @@ #include "user_delegate.h" #include "utils/block_integer.h" #include "utils/crypto.h" +#include "../../service/backup/include/backup_manager.h" namespace OHOS::DistributedKv { using namespace std::chrono; diff --git a/services/distributeddataservice/app/src/task_manager.h b/services/distributeddataservice/app/src/task_manager.h index ef117d9b..c7a7b800 100644 --- a/services/distributeddataservice/app/src/task_manager.h +++ b/services/distributeddataservice/app/src/task_manager.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef DISTRIBUTED_DATA_TASK_EXECUTOR_H -#define DISTRIBUTED_DATA_TASK_EXECUTOR_H +#ifndef DISTRIBUTED_DATA_TASK_MANAGER_H +#define DISTRIBUTED_DATA_TASK_MANAGER_H #include "ithread_pool.h" #include "executor_pool.h" #include "visibility.h" @@ -38,4 +38,4 @@ private: std::shared_ptr executors_ = nullptr; }; } // namespace OHOS::DistributedData -#endif // DISTRIBUTED_DATA_TASK_EXECUTOR_H +#endif // DISTRIBUTED_DATA_TASK_MANAGER_H -- Gitee From c660404bc908f48a7c266d65cb8eb3d9ee19e96f Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 19:20:24 +0800 Subject: [PATCH 131/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../app/src/kvstore_data_service.cpp | 4 +--- .../service/backup/include/backup_manager.h | 7 +++---- .../service/backup/src/backup_manager.cpp | 10 +++++----- .../service/bootstrap/include/bootstrap.h | 2 +- .../service/bootstrap/src/bootstrap.cpp | 5 +++-- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 355a543a..cc275179 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -52,7 +52,6 @@ #include "user_delegate.h" #include "utils/block_integer.h" #include "utils/crypto.h" -#include "../../service/backup/include/backup_manager.h" namespace OHOS::DistributedKv { using namespace std::chrono; @@ -249,8 +248,7 @@ void KvStoreDataService::OnStart() Bootstrap::GetInstance().LoadDirectory(); Bootstrap::GetInstance().LoadCheckers(); Bootstrap::GetInstance().LoadNetworks(); - DistributedData::BackupManager::GetInstance().Init(executors_); - Bootstrap::GetInstance().LoadBackup(); + Bootstrap::GetInstance().LoadBackup(executors_); Initialize(); auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgr != nullptr) { diff --git a/services/distributeddataservice/service/backup/include/backup_manager.h b/services/distributeddataservice/service/backup/include/backup_manager.h index c9d653b2..1d4c38e5 100644 --- a/services/distributeddataservice/service/backup/include/backup_manager.h +++ b/services/distributeddataservice/service/backup/include/backup_manager.h @@ -20,9 +20,8 @@ #include "metadata/secret_key_meta_data.h" #include "metadata/store_meta_data.h" #include "types.h" -#include "visibility.h" namespace OHOS::DistributedData { -class API_EXPORT BackupManager { +class BackupManager { public: using Exporter = std::function; struct BackupParam { @@ -37,8 +36,8 @@ public: CLEAN_DATA, }; static BackupManager &GetInstance(); - void Init(std::shared_ptr executors); - void BackSchedule(); + void Init(); + void BackSchedule(std::shared_ptr executors); void SetBackupParam(const BackupParam &backupParam); void RegisterExporter(int32_t type, Exporter exporter); bool GetPassWord(const StoreMetaData &meta, std::vector &password); diff --git a/services/distributeddataservice/service/backup/src/backup_manager.cpp b/services/distributeddataservice/service/backup/src/backup_manager.cpp index 68bfd7d5..5c52cbd7 100644 --- a/services/distributeddataservice/service/backup/src/backup_manager.cpp +++ b/services/distributeddataservice/service/backup/src/backup_manager.cpp @@ -48,11 +48,8 @@ BackupManager &BackupManager::GetInstance() return instance; } -void BackupManager::Init(std::shared_ptr executors) +void BackupManager::Init() { - if (!executors_) { - executors_ = executors; - } std::vector metas; MetaDataManager::GetInstance().LoadMeta( StoreMetaData::GetPrefix({DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid}), metas); @@ -93,8 +90,11 @@ void BackupManager::RegisterExporter(int32_t type, Exporter exporter) } } -void BackupManager::BackSchedule() +void BackupManager::BackSchedule(std::shared_ptr executors) { + if (!executors_) { + executors_ = std::move(executors); + } std::chrono::duration delay(schedularDelay_); std::chrono::duration internal(schedularInternal_); ZLOGI("BackupManager Schedule start."); diff --git a/services/distributeddataservice/service/bootstrap/include/bootstrap.h b/services/distributeddataservice/service/bootstrap/include/bootstrap.h index e98d00f0..c2301236 100644 --- a/services/distributeddataservice/service/bootstrap/include/bootstrap.h +++ b/services/distributeddataservice/service/bootstrap/include/bootstrap.h @@ -29,7 +29,7 @@ public: API_EXPORT void LoadCheckers(); API_EXPORT void LoadNetworks(); API_EXPORT void LoadDirectory(); - API_EXPORT void LoadBackup(); + API_EXPORT void LoadBackup(std::shared_ptr executors); private: static constexpr const char *DEFAULT_LABEL = "distributeddata"; static constexpr const char *DEFAULT_META = "service_meta"; diff --git a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp index dbcf5e1b..6cdd57ab 100644 --- a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp +++ b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp @@ -94,7 +94,7 @@ void Bootstrap::LoadCheckers() } } -void Bootstrap::LoadBackup() +void Bootstrap::LoadBackup(std::shared_ptr executors) { auto *backupRules = ConfigFactory::GetInstance().GetBackupConfig(); if (backupRules == nullptr) { @@ -105,7 +105,8 @@ void Bootstrap::LoadBackup() BackupManager::BackupParam backupParam = { backupRules->schedularDelay, backupRules->schedularInternal, backupRules->backupInternal, backupRules->backupNumber}; BackupManager::GetInstance().SetBackupParam(backupParam); - BackupManager::GetInstance().BackSchedule(); + BackupManager::GetInstance().Init(); + BackupManager::GetInstance().BackSchedule(std::move(executors)); } void Bootstrap::LoadNetworks() -- Gitee From 6f1fe9b2d80edae5368a969897d57f0a9173bacf Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 19:59:31 +0800 Subject: [PATCH 132/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../adapter/communicator/src/device_manager_adapter.cpp | 4 ++-- .../distributeddataservice/adapter/dfx/src/hiview_adapter.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index 4786abf6..96dbd680 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -170,10 +170,10 @@ void DeviceManagerAdapter::Online(const DmDeviceInfo &info) } } executors_->Schedule( + std::chrono::milliseconds(SYNC_TIMEOUT), [this, dvInfo]() { TimeOut(dvInfo.uuid); - }, - std::chrono::milliseconds(SYNC_TIMEOUT)); + }); syncTask_.Insert(dvInfo.uuid, dvInfo.uuid); for (const auto &item : observers) { // set compatible identify, sync service meta if (item == nullptr) { diff --git a/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp b/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp index 2caa310f..9ac6fef2 100644 --- a/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp +++ b/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp @@ -244,7 +244,7 @@ void HiViewAdapter::InvokeTraffic() void HiViewAdapter::ReportVisitStatistic(int dfxCode, const VisitStat &stat, std::shared_ptr executors) { - ExecutorPool ::Task task([dfxCode, stat]() { + ExecutorPool::Task task([dfxCode, stat]() { std::lock_guard lock(visitMutex_); auto it = visitStat_.find(stat.GetKey()); if (it == visitStat_.end()) { -- Gitee From 979fca62fa5baf2df5bd2b7e5eef1cbeae79d12a Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 22:19:20 +0800 Subject: [PATCH 133/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- services/distributeddataservice/adapter/dfx/test/BUILD.gn | 1 - services/distributeddataservice/app/BUILD.gn | 1 - .../distributeddataservice/app/src/kvstore_data_service.cpp | 2 -- .../app/src/uninstaller/uninstaller_impl.cpp | 1 + .../app/src/uninstaller/uninstaller_impl.h | 2 +- .../distributeddataservice/service/kvdb/user_delegate.cpp | 4 +--- 6 files changed, 3 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/adapter/dfx/test/BUILD.gn b/services/distributeddataservice/adapter/dfx/test/BUILD.gn index 0c2a6050..542ca18d 100755 --- a/services/distributeddataservice/adapter/dfx/test/BUILD.gn +++ b/services/distributeddataservice/adapter/dfx/test/BUILD.gn @@ -120,7 +120,6 @@ group("unittest") { deps += [ ":DistributeddataDfxMSTTest", - ":DistributeddataDfxUTTest", ] } ############################################################################### diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 3f8be8fd..87f34f1f 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -60,7 +60,6 @@ config("module_private_config") { "../adapter/include/utils", "../adapter/include/dfx", "../adapter/include", - "../service/backup/include", "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include", # for ipc_core interfaces. diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index cc275179..19d1259e 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -297,7 +297,6 @@ void KvStoreDataService::StartService() if (!ret) { DumpHelper::GetInstance().AddErrorInfo("StartService: Service publish failed."); } - Uninstaller::GetInstance().Init(this, executors_); // Initialize meta db delegate manager. KvStoreMetaManager::GetInstance().SubscribeMeta(KvStoreMetaRow::KEY_PREFIX, [this](const std::vector &key, const std::vector &value, CHANGE_FLAG flag) { @@ -307,7 +306,6 @@ void KvStoreDataService::StartService() UserDelegate::GetInstance().Init(executors_); // subscribe account event listener to EventNotificationMgr - AccountDelegate::GetInstance()->SubscribeAccountEvent(executors_); auto autoLaunch = [this](const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) -> bool { auto status = ResolveAutoLaunchParamByIdentifier(identifier, param); features_.ForEachCopies([&identifier, ¶m](const auto &, sptr &value) { diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index 13570c08..81072e46 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -108,6 +108,7 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared }; auto subscriber = std::make_shared(info, callback); subscriber_ = subscriber; + executors_ = executors; executors_->Execute(GetTask()); return Status::SUCCESS; } diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h index 837fd1da..50ec5fe3 100644 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h @@ -46,7 +46,7 @@ public: private: static constexpr int32_t RETRY_TIME = 300; - static constexpr int32_t RETRY_INTERVAL = 100 * 1000; + static constexpr int32_t RETRY_INTERVAL = 100; int32_t retryTime_; ExecutorPool::Task GetTask(); std::shared_ptr subscriber_ {}; diff --git a/services/distributeddataservice/service/kvdb/user_delegate.cpp b/services/distributeddataservice/service/kvdb/user_delegate.cpp index 97bbe024..2cd03d0f 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.cpp +++ b/services/distributeddataservice/service/kvdb/user_delegate.cpp @@ -172,9 +172,7 @@ void UserDelegate::Init(const std::shared_ptr& executors) if (!executors_) { executors_ = executors; } - if (!InitLocalUserMeta()) { - executors_->Execute(GeTask()); - } + executors_->Execute(GeTask()); ZLOGD("subscribe os account ret:%{public}d", ret); } -- Gitee From 3be8901d6bec7188ca7b5f5934192afb5890ae8f Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 22:25:39 +0800 Subject: [PATCH 134/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- services/distributeddataservice/app/src/task_manager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/src/task_manager.h b/services/distributeddataservice/app/src/task_manager.h index c7a7b800..caf1050e 100644 --- a/services/distributeddataservice/app/src/task_manager.h +++ b/services/distributeddataservice/app/src/task_manager.h @@ -18,7 +18,7 @@ #include "executor_pool.h" #include "visibility.h" namespace OHOS::DistributedData { -class API_EXPORT TaskManager : public DistributedDB::IThreadPool { +class TaskManager : public DistributedDB::IThreadPool { public: using TaskId = uint64_t; using Task = std::function; -- Gitee From 3e64e27c1e06866eff44cdc9ae69e5c2a607eecc Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 23:10:39 +0800 Subject: [PATCH 135/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../src/account_delegate_default_impl.cpp | 2 +- .../src/account_delegate_default_impl.h | 2 +- .../src/account_delegate_normal_impl.cpp | 24 ++++++++++--------- .../src/account_delegate_normal_impl.h | 6 ++--- .../src/device_manager_adapter.cpp | 4 +++- .../app/src/kvstore_data_service.cpp | 6 +++-- .../app/src/kvstore_meta_manager.cpp | 20 ++++++++-------- .../app/src/kvstore_meta_manager.h | 6 ++--- .../service/rdb/rdb_service_impl.cpp | 11 +++++---- .../service/rdb/rdb_syncer.cpp | 10 ++++++++ .../service/rdb/rdb_syncer.h | 5 ++++ 11 files changed, 60 insertions(+), 36 deletions(-) diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp index 0ac69a0e..b9d90b27 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp +++ b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp @@ -49,7 +49,7 @@ bool AccountDelegateDefaultImpl::QueryUsers(std::vector &users) return true; } -void AccountDelegateDefaultImpl::SubscribeAccountEvent(std::shared_ptr executors) +void AccountDelegateDefaultImpl::SubscribeAccountEvent() { ZLOGD("no account part."); } diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h index 811739a3..1bfbcb10 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h +++ b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h @@ -25,7 +25,7 @@ public: std::string GetCurrentAccountId() const override; int32_t GetUserByToken(uint32_t tokenId) const override; bool QueryUsers(std::vector &users) override; - void SubscribeAccountEvent(std::shared_ptr executors) override; + void SubscribeAccountEvent() override; void UnsubscribeAccountEvent() override; private: ~AccountDelegateDefaultImpl(); diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp index 152c528a..5dd42f3f 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp +++ b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp @@ -77,7 +77,7 @@ bool AccountDelegateNormalImpl::QueryUsers(std::vector &users) return AccountSA::OsAccountManager::QueryActiveOsAccountIds(users) == 0; } -void AccountDelegateNormalImpl::SubscribeAccountEvent(std::shared_ptr executors) +void AccountDelegateNormalImpl::SubscribeAccountEvent() { ZLOGI("Subscribe account event listener start."); MatchingSkills matchingSkills; @@ -89,27 +89,24 @@ void AccountDelegateNormalImpl::SubscribeAccountEvent(std::shared_ptrExecute(GetTask()); + executors_->Execute(GetTask(0)); } -ExecutorPool::Task AccountDelegateNormalImpl::GetTask() +ExecutorPool::Task AccountDelegateNormalImpl::GetTask(uint32_t retry) { - return [this] { + return [this, retry] { auto result = CommonEventManager::SubscribeCommonEvent(eventSubscriber_); if (result) { ZLOGI("success to register subscriber."); return; } - ZLOGD("fail to register subscriber, error:%{public}d, time:%{public}d", result, tryTimes); - tryTimes++; - if (tryTimes == MAX_RETRY_TIME) { + ZLOGD("fail to register subscriber, error:%{public}d, time:%{public}d", result, retry); + + if (retry + 1 > MAX_RETRY_TIME) { ZLOGE("fail to register subscriber!"); return; } - executors_->Schedule(std::chrono::seconds(RETRY_WAIT_TIME_S), GetTask()); + executors_->Schedule(std::chrono::seconds(RETRY_WAIT_TIME_S), GetTask(retry + 1)); }; } @@ -151,5 +148,10 @@ std::string AccountDelegateNormalImpl::Sha256AccountId(const std::string &plainT auto plainVal = htobe64(plain); return DoHash(static_cast(&plainVal), sizeof(plainVal), true); } + +void AccountDelegateNormalImpl::BindExecutor(std::shared_ptr executors) +{ + executors_ = executors; +} } // namespace DistributedKv } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h index 60e07cf5..86bdf15c 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h +++ b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h @@ -30,14 +30,14 @@ public: std::string GetCurrentAccountId() const override; int32_t GetUserByToken(uint32_t tokenId) const override; bool QueryUsers(std::vector &users) override; - void SubscribeAccountEvent(std::shared_ptr executors) override; + void SubscribeAccountEvent() override; void UnsubscribeAccountEvent() override; + void BindExecutor(std::shared_ptr executors) override; private: ~AccountDelegateNormalImpl(); std::string Sha256AccountId(const std::string &plainText) const; - ExecutorPool::Task GetTask(); - int tryTimes = 0; + ExecutorPool::Task GetTask(uint32_t retry); static constexpr int MAX_RETRY_TIME = 300; static constexpr int RETRY_WAIT_TIME_S = 1; std::shared_ptr eventSubscriber_ {}; diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index 96dbd680..2ea67eb8 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -93,7 +93,9 @@ DeviceManagerAdapter &DeviceManagerAdapter::GetInstance() void DeviceManagerAdapter::Init(std::shared_ptr executors) { ZLOGI("begin"); - executors_ = std::move(executors); + if (executors_ == nullptr) { + executors_ = std::move(executors); + } RegDevCallback()(); } diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 19d1259e..9cae0d75 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -97,7 +97,8 @@ void KvStoreDataService::Initialize() AppDistributedKv::CommunicationProvider::GetInstance(); PermitDelegate::GetInstance().Init(); InitSecurityAdapter(executors_); - KvStoreMetaManager::GetInstance().InitMetaParameter(executors_); + KvStoreMetaManager::GetInstance().BindExecutor(executors_); + KvStoreMetaManager::GetInstance().InitMetaParameter(); accountEventObserver_ = std::make_shared(*this, executors_); AccountDelegate::GetInstance()->Subscribe(accountEventObserver_); deviceInnerListener_ = std::make_unique(*this); @@ -233,6 +234,7 @@ void KvStoreDataService::OnStart() constexpr size_t MIN = 5; executors_ = std::make_shared(MAX, MIN); Reporter::GetInstance()->SetThreadPool(executors_); + AccountDelegate::GetInstance()->BindExecutor(executors_); AccountDelegate::GetInstance()->RegisterHashFunc(Crypto::Sha256); DmAdapter::GetInstance().Init(executors_); static constexpr int32_t RETRY_TIMES = 50; @@ -271,7 +273,7 @@ void KvStoreDataService::OnAddSystemAbility(int32_t systemAbilityId, const std:: if (systemAbilityId != COMMON_EVENT_SERVICE_ID) { return; } - AccountDelegate::GetInstance()->SubscribeAccountEvent(executors_); + AccountDelegate::GetInstance()->SubscribeAccountEvent(); Uninstaller::GetInstance().Init(this, executors_); } diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index 4ecee416..5cd0c85e 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -169,20 +169,17 @@ void KvStoreMetaManager::InitMetaData() ZLOGI("end."); } -void KvStoreMetaManager::InitMetaParameter(std::shared_ptr executors) +void KvStoreMetaManager::InitMetaParameter() { ZLOGI("start."); - if (!executors_) { - executors_ = executors; - } - executors_->Execute(GetTask()); + executors_->Execute(GetTask(0)); DistributedDB::KvStoreConfig kvStoreConfig{ metaDBDirectory_ }; delegateManager_.SetKvStoreConfig(kvStoreConfig); } -ExecutorPool::Task KvStoreMetaManager::GetTask() +ExecutorPool::Task KvStoreMetaManager::GetTask(uint32_t retry) { - return [this] { + return [this, retry] { auto status = CryptoManager::GetInstance().CheckRootKey(); if (status == CryptoManager::ErrCode::SUCCESS) { ZLOGI("root key exist."); @@ -193,13 +190,12 @@ ExecutorPool::Task KvStoreMetaManager::GetTask() ZLOGI("GenerateRootKey success."); return; } - retryTimes_++; ZLOGW("GenerateRootKey failed, retry times:%{public}d.", static_cast(retryTimes_)); - if (retryTimes_ == RETRY_MAX_TIMES) { + if (retry + 1 > RETRY_MAX_TIMES) { ZLOGE("fail to register subscriber!"); return; } - executors_->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetTask()); + executors_->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetTask(retry + 1)); }; } @@ -398,5 +394,9 @@ size_t KvStoreMetaManager::GetSyncDataSize(const std::string &deviceId) return metaDelegate->GetSyncDataSize(deviceId); } +void KvStoreMetaManager::BindExecutor(std::shared_ptr executors) +{ + executors_ = executors; +} } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.h b/services/distributeddataservice/app/src/kvstore_meta_manager.h index 669e5173..9fb69912 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.h +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.h @@ -48,12 +48,13 @@ public: static KvStoreMetaManager &GetInstance(); - void InitMetaParameter(std::shared_ptr executors); + void InitMetaParameter(); void InitMetaListener(); void InitBroadcast(); void InitDeviceOnline(); void SubscribeMeta(const std::string &keyPrefix, const ChangeObserver &observer); size_t GetSyncDataSize(const std::string &deviceId); + void BindExecutor(std::shared_ptr executors); private: using NbDelegate = std::shared_ptr; NbDelegate GetMetaKvStore(); @@ -72,7 +73,7 @@ private: std::string GetBackupPath() const; - ExecutorPool::Task GetTask(); + ExecutorPool::Task GetTask(uint32_t retry); class KvStoreMetaObserver : public DistributedDB::KvStoreObserver { public: @@ -87,7 +88,6 @@ private: static constexpr int32_t RETRY_MAX_TIMES = 100; static constexpr int32_t RETRY_INTERVAL = 1; - int32_t retryTimes_ = 0; NbDelegate metaDelegate_; std::string metaDBDirectory_; const std::string label_; diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 9d371f2a..d526c6d2 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -231,8 +231,9 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa if (it != syncers.end()) { syncer = it->second; if (!param.isEncrypt_ || param.password_.empty()) { - executors_->Schedule( - std::chrono::milliseconds(SYNCER_TIMEOUT), [this, syncer] { SyncerTimeout(syncer); }); + executors_->Remove(syncer->GetTimerId(), true); + auto timerId = executors_->Schedule(std::chrono::milliseconds(SYNCER_TIMEOUT), [this, syncer] { SyncerTimeout(syncer); }); + syncer->SetTimerId(timerId ); return true; } syncers.erase(storeId); @@ -256,8 +257,10 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa syncers[storeId] = syncer_; syncer = syncer_; syncerNum_++; - executors_->Schedule( - std::chrono::milliseconds(SYNCER_TIMEOUT), [this, syncer] { SyncerTimeout(syncer); }); + auto timerId = executors_->Schedule(std::chrono::milliseconds(SYNCER_TIMEOUT), [this, syncer] { + SyncerTimeout(syncer); + }); + syncer->SetTimerId(timerId); return !syncers.empty(); }); diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/services/distributeddataservice/service/rdb/rdb_syncer.cpp index f61d8540..3d47af3e 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -62,6 +62,16 @@ RdbSyncer::~RdbSyncer() noexcept } } +void RdbSyncer::SetTimerId(uint32_t timerId) +{ + timerId_ = timerId; +} + +uint32_t RdbSyncer::GetTimerId() const +{ + return timerId_; +} + pid_t RdbSyncer::GetPid() const { return pid_; diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.h b/services/distributeddataservice/service/rdb/rdb_syncer.h index c768bdf5..9b5f90b4 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -39,6 +39,10 @@ public: pid_t GetPid() const; + void SetTimerId(uint32_t timerId); + + uint32_t GetTimerId() const; + std::string GetStoreId() const; std::string GetIdentifier() const; @@ -81,6 +85,7 @@ private: pid_t pid_ {}; pid_t uid_ {}; uint32_t token_ {}; + uint32_t timerId_ {}; static std::vector GetConnectDevices(); static std::vector NetworkIdToUUID(const std::vector& networkIds); -- Gitee From 7de5f4b019ceb22bf3068050902007901a30f308 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 23:22:06 +0800 Subject: [PATCH 136/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../adapter/include/account/account_delegate.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/adapter/include/account/account_delegate.h b/services/distributeddataservice/adapter/include/account/account_delegate.h index d19123bd..79cdb36d 100644 --- a/services/distributeddataservice/adapter/include/account/account_delegate.h +++ b/services/distributeddataservice/adapter/include/account/account_delegate.h @@ -59,10 +59,11 @@ public: API_EXPORT virtual Status Unsubscribe(std::shared_ptr observer) = 0; API_EXPORT virtual std::string GetCurrentAccountId() const = 0; API_EXPORT virtual int32_t GetUserByToken(uint32_t tokenId) const = 0; - API_EXPORT virtual void SubscribeAccountEvent(std::shared_ptr executors) = 0; + API_EXPORT virtual void SubscribeAccountEvent() = 0; API_EXPORT virtual void UnsubscribeAccountEvent() = 0; API_EXPORT virtual bool QueryUsers(std::vector &users) = 0; API_EXPORT virtual bool RegisterHashFunc(HashFunc hash) = 0; + API_EXPORT virtual void BindExecutor(std::shared_ptr executors) = 0; API_EXPORT static AccountDelegate *GetInstance(); private: -- Gitee From f20ec7f9b35994e60e209b44b623d300117c07b1 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 23:27:05 +0800 Subject: [PATCH 137/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../distributeddataservice/app/src/kvstore_meta_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index 5cd0c85e..7fb54c86 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -190,7 +190,7 @@ ExecutorPool::Task KvStoreMetaManager::GetTask(uint32_t retry) ZLOGI("GenerateRootKey success."); return; } - ZLOGW("GenerateRootKey failed, retry times:%{public}d.", static_cast(retryTimes_)); + ZLOGW("GenerateRootKey failed, retry times:%{public}d.", static_cast(retry)); if (retry + 1 > RETRY_MAX_TIMES) { ZLOGE("fail to register subscriber!"); return; -- Gitee From 821708630266622990e3d2f217b2a1c81d75aefc Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 23:47:20 +0800 Subject: [PATCH 138/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../app/test/unittest/kvstore_data_service_test.cpp | 3 ++- .../app/test/unittest/session_manager_test.cpp | 3 ++- .../distributeddataservice/service/rdb/rdb_service_impl.cpp | 6 ++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp b/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp index 9369578b..e335ae22 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp @@ -57,7 +57,8 @@ HWTEST_F(KvStoreDataServiceTest, RegisterClientDeathObserver001, TestSize.Level1 KvStoreDataService kvDataService; Bootstrap::GetInstance().LoadComponents(); Bootstrap::GetInstance().LoadCheckers(); - KvStoreMetaManager::GetInstance().InitMetaParameter(std::make_shared(12, 5)); + KvStoreMetaManager::GetInstance().BindExecutor(std::make_shared(12, 5)); + KvStoreMetaManager::GetInstance().InitMetaParameter(); Status status = kvDataService.RegisterClientDeathObserver(appId, new KvStoreClientDeathObserver()); EXPECT_EQ(status, Status::SUCCESS) << "RegisterClientDeathObserver failed"; } diff --git a/services/distributeddataservice/app/test/unittest/session_manager_test.cpp b/services/distributeddataservice/app/test/unittest/session_manager_test.cpp index e834cfe2..6320bfd6 100644 --- a/services/distributeddataservice/app/test/unittest/session_manager_test.cpp +++ b/services/distributeddataservice/app/test/unittest/session_manager_test.cpp @@ -41,7 +41,8 @@ public: Bootstrap::GetInstance().LoadComponents(); Bootstrap::GetInstance().LoadDirectory(); Bootstrap::GetInstance().LoadCheckers(); - KvStoreMetaManager::GetInstance().InitMetaParameter(executors); + KvStoreMetaManager::GetInstance().BindExecutor(executors); + KvStoreMetaManager::GetInstance().InitMetaParameter(); KvStoreMetaManager::GetInstance().InitMetaListener(); DeviceManagerAdapter::GetInstance().Init(executors); diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index d526c6d2..983f6df9 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -232,8 +232,10 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa syncer = it->second; if (!param.isEncrypt_ || param.password_.empty()) { executors_->Remove(syncer->GetTimerId(), true); - auto timerId = executors_->Schedule(std::chrono::milliseconds(SYNCER_TIMEOUT), [this, syncer] { SyncerTimeout(syncer); }); - syncer->SetTimerId(timerId ); + auto timerId = executors_->Schedule(std::chrono::milliseconds(SYNCER_TIMEOUT), [this, syncer] { + SyncerTimeout(syncer); + }); + syncer->SetTimerId(timerId); return true; } syncers.erase(storeId); -- Gitee From edbc1f623cd1772fa1eeea73d7f95196345fbf52 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 23:49:10 +0800 Subject: [PATCH 139/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- services/distributeddataservice/service/rdb/rdb_syncer.cpp | 4 ++-- services/distributeddataservice/service/rdb/rdb_syncer.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/services/distributeddataservice/service/rdb/rdb_syncer.cpp index 3d47af3e..f8f6192a 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -62,12 +62,12 @@ RdbSyncer::~RdbSyncer() noexcept } } -void RdbSyncer::SetTimerId(uint32_t timerId) +void RdbSyncer::SetTimerId(uint64_t timerId) { timerId_ = timerId; } -uint32_t RdbSyncer::GetTimerId() const +uint64_t RdbSyncer::GetTimerId() const { return timerId_; } diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.h b/services/distributeddataservice/service/rdb/rdb_syncer.h index 9b5f90b4..42602bdb 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -39,9 +39,9 @@ public: pid_t GetPid() const; - void SetTimerId(uint32_t timerId); + void SetTimerId(uint64_t timerId); - uint32_t GetTimerId() const; + uint64_t GetTimerId() const; std::string GetStoreId() const; @@ -85,7 +85,7 @@ private: pid_t pid_ {}; pid_t uid_ {}; uint32_t token_ {}; - uint32_t timerId_ {}; + uint64_t timerId_ {}; static std::vector GetConnectDevices(); static std::vector NetworkIdToUUID(const std::vector& networkIds); -- Gitee From 4bc16eae8854f2be6fb9e1d9dcd624d8376aab80 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Sun, 7 May 2023 23:57:07 +0800 Subject: [PATCH 140/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- services/distributeddataservice/adapter/dfx/test/BUILD.gn | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/services/distributeddataservice/adapter/dfx/test/BUILD.gn b/services/distributeddataservice/adapter/dfx/test/BUILD.gn index 542ca18d..de03ed06 100755 --- a/services/distributeddataservice/adapter/dfx/test/BUILD.gn +++ b/services/distributeddataservice/adapter/dfx/test/BUILD.gn @@ -118,8 +118,6 @@ group("unittest") { testonly = true deps = [] - deps += [ - ":DistributeddataDfxMSTTest", - ] + deps += [ ":DistributeddataDfxMSTTest" ] } ############################################################################### -- Gitee From 8a6cd949f005a9408ad2a566bac2adcb64f33eac Mon Sep 17 00:00:00 2001 From: hanlu Date: Mon, 8 May 2023 11:00:24 +0800 Subject: [PATCH 141/152] f Signed-off-by: hanlu --- .../service/data_share/common/bundle_mgr_proxy.h | 2 +- .../service/data_share/common/db_delegate.cpp | 3 --- .../service/data_share/common/db_delegate.h | 4 ++-- .../service/data_share/common/rdb_delegate.cpp | 1 - .../service/data_share/common/uri_utils.cpp | 2 -- .../service/data_share/common/uri_utils.h | 4 ++-- .../strategies/general/load_config_common_strategy.cpp | 1 - 7 files changed, 5 insertions(+), 12 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h index e66197a1..073b4349 100644 --- a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h @@ -22,12 +22,12 @@ namespace OHOS::DataShare { class BundleMgrProxy final : public std::enable_shared_from_this { public: - ~BundleMgrProxy(); static std::shared_ptr GetInstance(); bool GetBundleInfoFromBMS(const std::string &bundleName, int32_t userId, AppExecFwk::BundleInfo &bundleInfo); private: BundleMgrProxy() = default; + ~BundleMgrProxy(); class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { public: explicit ServiceDeathRecipient(std::weak_ptr owner) : owner_(owner) {} diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp index 10fddab5..97e451bc 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -23,9 +23,6 @@ std::shared_ptr DBDelegate::Create(const std::string &dir, int versi return std::make_shared(dir, version); } -const std::string KvDBDelegate::TEMPLATE_TABLE = "template_"; -const std::string KvDBDelegate::DATA_TABLE = "data_"; - std::shared_ptr KvDBDelegate::GetInstance(bool reInit, const std::string &dir) { static std::shared_ptr delegate = nullptr; diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.h b/services/distributeddataservice/service/data_share/common/db_delegate.h index 68bc62d5..e6ec2721 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.h +++ b/services/distributeddataservice/service/data_share/common/db_delegate.h @@ -85,8 +85,8 @@ struct KvData : public DistributedData::Serializable { class KvDBDelegate { public: - static const std::string TEMPLATE_TABLE; - static const std::string DATA_TABLE; + static constexpr const char *TEMPLATE_TABLE = "template_"; + static constexpr const char *DATA_TABLE = "data_"; static std::shared_ptr GetInstance(bool reInit = false, const std::string &dir = ""); virtual ~KvDBDelegate() = default; virtual int32_t Upsert(const std::string &collectionName, const KvData &value) = 0; diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index 22fa5b8a..a5e6ad21 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -120,7 +120,6 @@ std::shared_ptr RdbDelegate::Query( ZLOGE("Query failed %{private}s", sql.c_str()); return nullptr; } - return nullptr; } diff --git a/services/distributeddataservice/service/data_share/common/uri_utils.cpp b/services/distributeddataservice/service/data_share/common/uri_utils.cpp index bc8ac9c2..403573f6 100644 --- a/services/distributeddataservice/service/data_share/common/uri_utils.cpp +++ b/services/distributeddataservice/service/data_share/common/uri_utils.cpp @@ -22,8 +22,6 @@ #include "utils/anonymous.h" namespace OHOS::DataShare { -const std::string URIUtils::DATA_SHARE_SCHEMA = "datashare:///"; -const std::string URIUtils::DATA_PROXY_SCHEMA = "datashareproxy://"; bool URIUtils::GetInfoFromURI(const std::string &uri, UriInfo &uriInfo) { Uri uriTemp(uri); diff --git a/services/distributeddataservice/service/data_share/common/uri_utils.h b/services/distributeddataservice/service/data_share/common/uri_utils.h index 615b3ef7..d2efad71 100644 --- a/services/distributeddataservice/service/data_share/common/uri_utils.h +++ b/services/distributeddataservice/service/data_share/common/uri_utils.h @@ -31,8 +31,8 @@ public: static bool GetBundleNameFromProxyURI(const std::string &uri, std::string &bundleName); static bool GetUserIdFromProxyURI(const std::string &uri, int32_t &user); static bool IsDataProxyURI(const std::string &uri); - static const std::string DATA_SHARE_SCHEMA; - static const std::string DATA_PROXY_SCHEMA; + static constexpr const char *DATA_SHARE_SCHEMA = "datashare:///";; + static constexpr const char *DATA_PROXY_SCHEMA = "datashareproxy://"; private: enum PATH_PARAM : int32_t { diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp index b911ef2d..5f9a931d 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp @@ -28,7 +28,6 @@ bool LoadConfigCommonStrategy::operator()(std::shared_ptr context) // single app, userId is in uri if (context->currentUserId == 0) { URIUtils::GetUserIdFromProxyURI(context->uri, context->currentUserId); - ZLOGI("user uri's userId %{public}d", context->currentUserId); } FormatUri(context->uri); return true; -- Gitee From 5d11009dbee275434658adfbeabe284ebd04f0e3 Mon Sep 17 00:00:00 2001 From: hanlu Date: Mon, 8 May 2023 11:44:25 +0800 Subject: [PATCH 142/152] f Signed-off-by: hanlu --- .../service/data_share/common/bundle_mgr_proxy.h | 2 +- .../service/data_share/strategies/delete_strategy.cpp | 2 +- .../service/data_share/strategies/get_data_strategy.cpp | 2 +- .../service/data_share/strategies/insert_strategy.cpp | 2 +- .../service/data_share/strategies/publish_strategy.cpp | 2 +- .../service/data_share/strategies/query_strategy.cpp | 2 +- .../service/data_share/strategies/subscribe_strategy.cpp | 2 +- .../service/data_share/strategies/update_strategy.cpp | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h index 073b4349..e66197a1 100644 --- a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h @@ -22,12 +22,12 @@ namespace OHOS::DataShare { class BundleMgrProxy final : public std::enable_shared_from_this { public: + ~BundleMgrProxy(); static std::shared_ptr GetInstance(); bool GetBundleInfoFromBMS(const std::string &bundleName, int32_t userId, AppExecFwk::BundleInfo &bundleInfo); private: BundleMgrProxy() = default; - ~BundleMgrProxy(); class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { public: explicit ServiceDeathRecipient(std::weak_ptr owner) : owner_(owner) {} diff --git a/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp index 5b0c8ae8..1fc64494 100644 --- a/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/delete_strategy.cpp @@ -62,7 +62,7 @@ Strategy *DeleteStrategy::GetStrategy() auto ret = strategies.Init(list); if (!ret) { std::for_each(list.begin(), list.end(), [](Strategy *item) { - free(item); + delete item; }); return nullptr; } diff --git a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp index 59e0b54d..2d5e432b 100644 --- a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp @@ -53,7 +53,7 @@ Strategy *GetDataStrategy::GetStrategy() auto ret = strategies.Init(list); if (!ret) { std::for_each(list.begin(), list.end(), [](Strategy *item) { - free(item); + delete item; }); return nullptr; } diff --git a/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp index dbe4f1ef..792ead9c 100644 --- a/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/insert_strategy.cpp @@ -63,7 +63,7 @@ Strategy *InsertStrategy::GetStrategy() auto ret = strategies.Init(list); if (!ret) { std::for_each(list.begin(), list.end(), [](Strategy *item) { - free(item); + delete item; }); return nullptr; } diff --git a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp index 07c50460..d658954c 100644 --- a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp @@ -53,7 +53,7 @@ Strategy *PublishStrategy::GetStrategy() auto ret = strategies.Init(list); if (!ret) { std::for_each(list.begin(), list.end(), [](Strategy *item) { - free(item); + delete item; }); return nullptr; } diff --git a/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp index befec41b..c2e2852a 100644 --- a/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/query_strategy.cpp @@ -65,7 +65,7 @@ Strategy *QueryStrategy::GetStrategy() auto ret = strategies.Init(list); if (!ret) { std::for_each(list.begin(), list.end(), [](Strategy *item) { - free(item); + delete item; }); return nullptr; } diff --git a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp index c546fc00..3835435c 100644 --- a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp @@ -53,7 +53,7 @@ Strategy *SubscribeStrategy::GetStrategy() auto ret = strategies.Init(list); if (!ret) { std::for_each(list.begin(), list.end(), [](Strategy *item) { - free(item); + delete item; }); return nullptr; } diff --git a/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp index c29a389b..800832ac 100644 --- a/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/update_strategy.cpp @@ -63,7 +63,7 @@ Strategy *UpdateStrategy::GetStrategy() auto ret = strategies.Init(list); if (!ret) { std::for_each(list.begin(), list.end(), [](Strategy *item) { - free(item); + delete item; }); return nullptr; } -- Gitee From 9d1ba980c93592222643f951232f1d8333064f8a Mon Sep 17 00:00:00 2001 From: hanlu Date: Mon, 8 May 2023 11:50:38 +0800 Subject: [PATCH 143/152] f Signed-off-by: hanlu --- .../service/data_share/common/uri_utils.cpp | 2 +- .../service/data_share/common/uri_utils.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/uri_utils.cpp b/services/distributeddataservice/service/data_share/common/uri_utils.cpp index 403573f6..5d6a4ffb 100644 --- a/services/distributeddataservice/service/data_share/common/uri_utils.cpp +++ b/services/distributeddataservice/service/data_share/common/uri_utils.cpp @@ -48,7 +48,7 @@ bool URIUtils::GetInfoFromURI(const std::string &uri, UriInfo &uriInfo) bool URIUtils::IsDataProxyURI(const std::string &uri) { - return uri.compare(0, URIUtils::DATA_PROXY_SCHEMA.size(), URIUtils::DATA_PROXY_SCHEMA) == 0; + return uri.compare(0, DATA_PROXY_SCHEMA_LEN, URIUtils::DATA_PROXY_SCHEMA) == 0; } bool URIUtils::GetBundleNameFromProxyURI(const std::string &uri, std::string &bundleName) diff --git a/services/distributeddataservice/service/data_share/common/uri_utils.h b/services/distributeddataservice/service/data_share/common/uri_utils.h index d2efad71..0bf62d16 100644 --- a/services/distributeddataservice/service/data_share/common/uri_utils.h +++ b/services/distributeddataservice/service/data_share/common/uri_utils.h @@ -32,7 +32,8 @@ public: static bool GetUserIdFromProxyURI(const std::string &uri, int32_t &user); static bool IsDataProxyURI(const std::string &uri); static constexpr const char *DATA_SHARE_SCHEMA = "datashare:///";; - static constexpr const char *DATA_PROXY_SCHEMA = "datashareproxy://"; + static constexpr const char DATA_PROXY_SCHEMA[] = "datashareproxy://"; + static constexpr int DATA_PROXY_SCHEMA_LEN = sizeof(DATA_PROXY_SCHEMA); private: enum PATH_PARAM : int32_t { -- Gitee From 8e9d39e4febfd96c26d65cc0cba716bf187caf46 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Mon, 8 May 2023 15:54:45 +0800 Subject: [PATCH 144/152] merge the diff Signed-off-by: Sven Wang --- .../framework/feature/feature_system.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/services/distributeddataservice/framework/feature/feature_system.cpp b/services/distributeddataservice/framework/feature/feature_system.cpp index 7f6804b9..cab8fa9a 100644 --- a/services/distributeddataservice/framework/feature/feature_system.cpp +++ b/services/distributeddataservice/framework/feature/feature_system.cpp @@ -100,10 +100,5 @@ int32_t FeatureSystem::Feature::OnReady(const std::string &device) { return E_OK; } - -int32_t FeatureSystem::Feature::OnExecutor(std::shared_ptr executors) -{ - return STUB_SUCCESS; -} } } -- Gitee From c162aa0a80e5194f86c3817eed35633708bb4e19 Mon Sep 17 00:00:00 2001 From: renjiecui Date: Mon, 8 May 2023 15:56:05 +0800 Subject: [PATCH 145/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../distributeddataservice/service/rdb/rdb_service_impl.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 983f6df9..ef578fd7 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -123,6 +123,9 @@ void RdbServiceImpl::OnClientDied(pid_t pid) ZLOGI("client dead pid=%{public}d", pid); syncers_.ComputeIfPresent(pid, [this](const auto& key, StoreSyncersType& syncers) { syncerNum_ -= static_cast(syncers.size()); + for (const auto& [name, syncer] : syncers) { + executors_->Remove(syncer->GetTimerId()); + } return false; }); notifiers_.Erase(pid); -- Gitee From 7cc687351c2340835317eeca2f838ae4cd92ff9d Mon Sep 17 00:00:00 2001 From: renjiecui Date: Mon, 8 May 2023 16:00:46 +0800 Subject: [PATCH 146/152] add ExtcutorPool and modify code Signed-off-by: renjiecui --- .../adapter/account/src/account_delegate_default_impl.cpp | 5 +++++ .../adapter/account/src/account_delegate_default_impl.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp index b9d90b27..7c6d0282 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp +++ b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp @@ -63,5 +63,10 @@ AccountDelegateDefaultImpl::~AccountDelegateDefaultImpl() { ZLOGD("destruct"); } + +void AccountDelegateDefaultImpl::BindExecutor(std::shared_ptr executors) +{ + ZLOGD("no account part"); +} } // namespace DistributedKv } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h index 1bfbcb10..33ee5fda 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h +++ b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h @@ -27,6 +27,8 @@ public: bool QueryUsers(std::vector &users) override; void SubscribeAccountEvent() override; void UnsubscribeAccountEvent() override; + void BindExecutor(std::shared_ptr executors) override; + private: ~AccountDelegateDefaultImpl(); }; -- Gitee From 0dbe54fd8aad15d919dbb5355ebd323eaf4dd3b2 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Mon, 8 May 2023 16:40:17 +0800 Subject: [PATCH 147/152] fixed merge bugs Signed-off-by: Sven Wang --- .../distributeddataservice/app/src/kvstore_data_service.cpp | 2 +- .../distributeddataservice/app/src/kvstore_data_service.h | 2 +- .../framework/feature/feature_system.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 5620cad2..ee5ef9e7 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -141,7 +141,7 @@ sptr KvStoreDataService::GetFeatureInterface(const std::string &n return true; }); if (isFirstCreate) { - feature->OnInitialize(executor_); + feature->OnInitialize(executors_); } return feature != nullptr ? feature->AsObject() : nullptr; } diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index 672c7c62..fae1dbac 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -133,7 +133,7 @@ private: std::shared_ptr security_; ConcurrentMap> features_; std::shared_ptr deviceInnerListener_; - std::shared_ptr executor_; + std::shared_ptr executors_; }; class DbMetaCallbackDelegateMgr : public DbMetaCallbackDelegate { diff --git a/services/distributeddataservice/framework/feature/feature_system.cpp b/services/distributeddataservice/framework/feature/feature_system.cpp index cab8fa9a..6bfd3d23 100644 --- a/services/distributeddataservice/framework/feature/feature_system.cpp +++ b/services/distributeddataservice/framework/feature/feature_system.cpp @@ -100,5 +100,5 @@ int32_t FeatureSystem::Feature::OnReady(const std::string &device) { return E_OK; } -} -} +} // namespace DistributedData +} // namespace OHOS \ No newline at end of file -- Gitee From 29d74a33d7d3256a2f80225f9184168747f38203 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Tue, 9 May 2023 16:29:06 +0800 Subject: [PATCH 148/152] add Subscribe the cloud data change interface Signed-off-by: Sven Wang --- .../framework/cloud/cloud_server.cpp | 10 ++++++++++ .../framework/include/cloud/cloud_db.h | 2 ++ .../framework/include/cloud/cloud_server.h | 2 ++ 3 files changed, 14 insertions(+) diff --git a/services/distributeddataservice/framework/cloud/cloud_server.cpp b/services/distributeddataservice/framework/cloud/cloud_server.cpp index a2e0f716..17c704a1 100644 --- a/services/distributeddataservice/framework/cloud/cloud_server.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_server.cpp @@ -40,6 +40,16 @@ SchemaMeta CloudServer::GetAppSchema(int32_t userId, const std::string &bundleNa return SchemaMeta(); } +int32_t CloudServer::Subscribe(int32_t userId, const std::map> &dbs) +{ + return 0; +} + +int32_t CloudServer::Unsubscribe(int32_t userId, const std::map> &dbs) +{ + return 0; +} + std::shared_ptr CloudServer::ConnectAssetLoader(uint32_t tokenId, const CloudServer::Database &dbMeta) { return nullptr; diff --git a/services/distributeddataservice/framework/include/cloud/cloud_db.h b/services/distributeddataservice/framework/include/cloud/cloud_db.h index 57d15f5c..c7599bda 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_db.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_db.h @@ -28,6 +28,8 @@ public: using Watcher = GeneralWatcher; using Async = std::function>)>; using Devices = std::vector; + virtual ~CloudDB() = default; + virtual int32_t Execute(const std::string &table, const std::string &sql, const VBucket &extend); virtual int32_t BatchInsert(const std::string &table, VBuckets &&values, VBuckets &extends); diff --git a/services/distributeddataservice/framework/include/cloud/cloud_server.h b/services/distributeddataservice/framework/include/cloud/cloud_server.h index eee93713..709a0cbc 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_server.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_server.h @@ -29,6 +29,8 @@ public: virtual CloudInfo GetServerInfo(int32_t userId); 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); -- Gitee From 7966c4b64fe62ad9696ee440b735c135305576a5 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Tue, 9 May 2023 17:21:47 +0800 Subject: [PATCH 149/152] update Signed-off-by: Sven Wang --- services/distributeddataservice/framework/cloud/cloud_db.cpp | 5 +++++ .../framework/include/cloud/cloud_db.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/services/distributeddataservice/framework/cloud/cloud_db.cpp b/services/distributeddataservice/framework/cloud/cloud_db.cpp index 6525a4f0..dd149f8e 100644 --- a/services/distributeddataservice/framework/cloud/cloud_db.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_db.cpp @@ -70,6 +70,11 @@ int32_t CloudDB::Unlock() return E_NOT_SUPPORT; } +int64_t CloudDB::AliveTime() +{ + return -1; +} + int32_t CloudDB::Close() { return E_NOT_SUPPORT; diff --git a/services/distributeddataservice/framework/include/cloud/cloud_db.h b/services/distributeddataservice/framework/include/cloud/cloud_db.h index c7599bda..4882d8d7 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_db.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_db.h @@ -52,6 +52,8 @@ public: virtual int32_t Unlock(); + virtual int64_t AliveTime(); + virtual int32_t Close(); }; } // namespace OHOS::DistributedData -- Gitee From 187320f78ae21578226afbaa6d92e5b4254a87ab Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Tue, 9 May 2023 18:05:34 +0800 Subject: [PATCH 150/152] dm switch Signed-off-by: zuojiangjiang --- .../service/kvdb/kvdb_service_impl.cpp | 14 ++++++++------ .../service/matrix/include/device_matrix.h | 4 ++++ .../service/matrix/src/device_matrix.cpp | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 086b2807..8428cb58 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -165,7 +165,7 @@ Status KVDBServiceImpl::Sync(const AppId &appId, const StoreId &storeId, const S MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData); auto delay = GetSyncDelayTime(syncInfo.delay, storeId); if (metaData.isAutoSync && syncInfo.seqId == std::numeric_limits::max()) { - DeviceMatrix::GetInstance().OnChanged(DeviceMatrix::GetInstance().GetCode(metaData)); + DeviceMatrix::GetInstance().OnChanged(metaData); StoreMetaDataLocal localMeta; MetaDataManager::GetInstance().LoadMeta(metaData.GetKeyLocal(), localMeta, true); if (!localMeta.HasPolicy(IMMEDIATE_SYNC_ON_CHANGE)) { @@ -492,17 +492,19 @@ int32_t KVDBServiceImpl::OnReady(const std::string &device) if (!data.isAutoSync) { continue; } + ZLOGI("[onReady] appId:%{public}s, storeId:%{public}s", data.bundleName.c_str(), data.storeId.c_str()); StoreMetaDataLocal localMetaData; MetaDataManager::GetInstance().LoadMeta(data.GetKeyLocal(), localMetaData, true); - if (!localMetaData.HasPolicy(PolicyType::IMMEDIATE_SYNC_ON_READY)) { + if (!localMetaData.HasPolicy(PolicyType::IMMEDIATE_SYNC_ON_READY) && + (!localMetaData.HasPolicy(PolicyType::TERM_OF_SYNC_VALIDITY) || + !DeviceMatrix::GetInstance().IsChangedInTerm(data, + localMetaData.GetPolicy(PolicyType::TERM_OF_SYNC_VALIDITY).valueUint))) { continue; } - auto policy = localMetaData.GetPolicy(PolicyType::IMMEDIATE_SYNC_ON_READY); SyncInfo syncInfo; - syncInfo.mode = PUSH_PULL; - syncInfo.delay = policy.IsValueEffect() ? policy.valueUint : 0; + syncInfo.delay = localMetaData.HasPolicy(PolicyType::IMMEDIATE_SYNC_ON_READY) ? + localMetaData.GetPolicy(PolicyType::IMMEDIATE_SYNC_ON_READY).valueUint : 0; syncInfo.devices = { device }; - ZLOGI("[onReady] appId:%{public}s, storeId:%{public}s", data.bundleName.c_str(), data.storeId.c_str()); auto delay = GetSyncDelayTime(syncInfo.delay, { data.storeId }); KvStoreSyncManager::GetInstance()->AddSyncOperation(uintptr_t(data.tokenId), delay, std::bind(&KVDBServiceImpl::DoSync, this, data, syncInfo, std::placeholders::_1, ACTION_SYNC), diff --git a/services/distributeddataservice/service/matrix/include/device_matrix.h b/services/distributeddataservice/service/matrix/include/device_matrix.h index e660bc93..c04223aa 100644 --- a/services/distributeddataservice/service/matrix/include/device_matrix.h +++ b/services/distributeddataservice/service/matrix/include/device_matrix.h @@ -24,6 +24,7 @@ namespace OHOS::DistributedData { class API_EXPORT DeviceMatrix { public: + using TimePoint = std::chrono::steady_clock::time_point; static constexpr uint16_t META_STORE_MASK = 0x0001; enum : int32_t { MATRIX_ONLINE = Event::EVT_CUSTOM, @@ -37,7 +38,9 @@ public: void Offline(const std::string &device); uint16_t OnBroadcast(const std::string &device, uint16_t code); void OnChanged(uint16_t code); + void OnChanged(const StoreMetaData &metaData); void OnExchanged(const std::string &device, uint16_t code, bool isRemote = false); + bool IsChangedInTerm(const StoreMetaData &metaData, uint64_t term); uint16_t GetCode(const StoreMetaData &metaData); void Clear(); @@ -72,6 +75,7 @@ private: std::map remotes_; std::vector maskApps_ = { "distributed_device_profile_service" }; LRUBucket versions_{ MAX_DEVICES }; + LRUBucket changeTime_ { 64 }; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICE_MATRIX_DEVICE_MATRIX_H diff --git a/services/distributeddataservice/service/matrix/src/device_matrix.cpp b/services/distributeddataservice/service/matrix/src/device_matrix.cpp index ab034dea..2217be08 100644 --- a/services/distributeddataservice/service/matrix/src/device_matrix.cpp +++ b/services/distributeddataservice/service/matrix/src/device_matrix.cpp @@ -131,6 +131,24 @@ void DeviceMatrix::OnChanged(uint16_t code) } } +void DeviceMatrix::OnChanged(const StoreMetaData &metaData) +{ + auto code = GetCode(metaData); + if (code != 0) { + OnChanged(code); + } + changeTime_.Set(metaData.tokenId, std::chrono::steady_clock::now()); +} + +bool DeviceMatrix::IsChangedInTerm(const StoreMetaData &metaData, uint64_t term) +{ + TimePoint changeTime; + if (!changeTime_.Get(metaData.tokenId, changeTime, false)) { + return false; + } + return std::chrono::steady_clock::now() < (changeTime + std::chrono::seconds(term)); +} + void DeviceMatrix::OnExchanged(const std::string &device, uint16_t code, bool isRemote) { std::lock_guard lockGuard(mutex_); -- Gitee From ab984336896e245f3916d48e55600c5f82126eee Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Tue, 9 May 2023 20:00:04 +0800 Subject: [PATCH 151/152] public the service framework interface Signed-off-by: Sven Wang --- .../distributeddataservice/framework/BUILD.gn | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index d26ce334..98a05fb2 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -17,7 +17,7 @@ import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") group("build_module") { deps = [ ":distributeddatasvcfwk" ] } -config("module_public_config") { +config("module_config") { visibility = [ ":*" ] include_dirs = [ "include", @@ -31,6 +31,15 @@ config("module_public_config") { ] } +config("module_public_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "//third_party/json/single_include", + "${kv_store_common_path}", + ] +} + ohos_shared_library("distributeddatasvcfwk") { sources = [ "backuprule/backup_rule_manager.cpp", @@ -67,7 +76,9 @@ ohos_shared_library("distributeddatasvcfwk") { cflags_cc = [ "-fvisibility=hidden" ] - configs = [ ":module_public_config" ] + configs = [ ":module_config" ] + + public_configs = [ ":module_public_config" ] deps = [ "//third_party/openssl:libcrypto_shared" ] -- Gitee From 4bbe1dc878bb2b1a7847c7b9b3e7f2f2a6f36fd2 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Thu, 11 May 2023 10:04:07 +0800 Subject: [PATCH 152/152] Add grd api Signed-off-by: lianhuix --- bundle.json | 3 +- .../service/data_share/gaussdb_rd/BUILD.gn | 74 ++++++++++++++++ .../service/data_share/gaussdb_rd/README.md | 1 + .../gaussdb_rd/include/grd_base/grd_db_api.h | 36 ++++++++ .../gaussdb_rd/include/grd_base/grd_error.h | 86 +++++++++++++++++++ .../include/grd_base/grd_resultset_api.h | 40 +++++++++ .../include/grd_base/grd_type_export.h | 69 +++++++++++++++ .../include/grd_document/grd_document_api.h | 48 +++++++++++ .../src/executor/base/grd_db_api.cpp | 33 +++++++ .../executor/document/grd_document_api.cpp | 53 ++++++++++++ .../executor/document/grd_resultset_api.cpp | 37 ++++++++ 11 files changed, 479 insertions(+), 1 deletion(-) create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/BUILD.gn create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/README.md create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_db_api.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_error.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_resultset_api.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_type_export.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_document/grd_document_api.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/base/grd_db_api.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/grd_document_api.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/grd_resultset_api.cpp diff --git a/bundle.json b/bundle.json index 79367b70..d7325b51 100644 --- a/bundle.json +++ b/bundle.json @@ -89,7 +89,8 @@ "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app:build_module", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:build_module", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service:build_module", - "//foundation/distributeddatamgr/datamgr_service/conf:build_module" + "//foundation/distributeddatamgr/datamgr_service/conf:build_module", + "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share/gaussdb_rd:build_module" ], "inner_kits": [], "test": [ diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/BUILD.gn b/services/distributeddataservice/service/data_share/gaussdb_rd/BUILD.gn new file mode 100644 index 00000000..911ed42d --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/BUILD.gn @@ -0,0 +1,74 @@ +# 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. +import("//build/ohos.gni") + +config("gaussdb_rd_config") { + visibility = [ ":*" ] + include_dirs = [ + "src/common/include", + "src/executor/include", + "src/executor/document", + "src/oh_adapter/include", + "src/oh_adapter/src", + "src/interface/include", + ] + + defines = [ + "SQLITE_ENABLE_SNAPSHOT", + "SQLITE_HAS_CODEC", + "SQLITE_ENABLE_JSON1", + "USING_HILOG_LOGGER", + "USE_SQLITE_SYMBOLS", + "SQLITE_ENABLE_DROPTABLE_CALLBACK", + ] + if (is_debug) { + defines += [ "TRACE_SQLITE_EXECUTE" ] + } +} + +config("gaussdb_rd_public_config") { + visibility = [ "*:*" ] + include_dirs = [ "include" ] +} + +group("build_module") { + deps = [ ":gaussdb_rd" ] +} + +ohos_shared_library("gaussdb_rd") { + sources = [ + "src/executor/base/grd_db_api.cpp", + "src/executor/document/grd_document_api.cpp", + "src/executor/document/grd_resultset_api.cpp", + ] + + configs = [ ":gaussdb_rd_config" ] + public_configs = [ ":gaussdb_rd_public_config" ] + + deps = [ "//third_party/sqlite:sqlite" ] + + configs += [ "//third_party/cJSON:cJSON_config" ] + ldflags = [ "-Wl,--exclude-libs,ALL" ] + cflags_cc = [ "-fvisibility=hidden" ] + deps += [ "//third_party/cJSON:cjson" ] + + external_deps = [ + "c_utils:utils", + "hisysevent_native:libhisysevent", + "hitrace_native:hitrace_meter", + "hiviewdfx_hilog_native:libhilog", + ] + + subsystem_name = "distributeddatamgr" + part_name = "datamgr_service" +} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/README.md b/services/distributeddataservice/service/data_share/gaussdb_rd/README.md new file mode 100644 index 00000000..ebc0b1fc --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/README.md @@ -0,0 +1 @@ +# Gauss RD \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_db_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_db_api.h new file mode 100644 index 00000000..cc1da4b9 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_db_api.h @@ -0,0 +1,36 @@ +/* +* 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 GRD_DB_API_H +#define GRD_DB_API_H + +#include + +#include "grd_type_export.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +GRD_API int32_t GRD_DBOpen(const char *dbPath, const char *configStr, uint32_t flags, GRD_DB **db); + +GRD_API int32_t GRD_DBClose(GRD_DB *db, uint32_t flags); + +GRD_API int32_t GRD_Flush(GRD_DB *db, uint32_t flags); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // GRD_DB_API_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_error.h b/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_error.h new file mode 100644 index 00000000..ecc7c705 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_error.h @@ -0,0 +1,86 @@ +/* +* 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 GRD_ERROR_H +#define GRD_ERROR_H + +#include "grd_type_export.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// Error category +#define GRD_OK 0 + +// Error category +#define GRD_NOT_SUPPORT (-1000) +#define GRD_OVER_LIMIT (-2000) +#define GRD_INVALID_ARGS (-3000) +#define GRD_SYSTEM_ERR (-4000) +#define GRD_FAILED_FILE_OPERATION (-5000) +#define GRD_INVALID_FILE_FORMAT (-6000) +#define GRD_INNER_ERR (-8000) +#define GRD_RESOURCE_BUSY (-9000) + +#define GRD_NO_DATA (-11000) +#define GRD_FAILED_MEMORY_ALLOCATE (-13000) +#define GRD_FAILED_MEMORY_RELEASE (-14000) +#define GRD_DATA_CONFLICT (-16000) +#define GRD_NOT_AVAILABLE (-19000) +#define GRD_INVALID_FORMAT (-37000) +#define GRD_REBUILD_DATABASE (-38000) + +// not support +#define GRD_JSON_OPERATION_NOT_SUPPORT (-5001001) + +// Exceed limit +#define GRD_JSON_LEN_LIMIT (-5002001) + +// Invalid parameter +#define GRD_FIELD_TYPE_NOT_MATCH (-5003001) +#define GRD_LARGE_JSON_NEST (-5003002) +#define GRD_INVALID_JSON_TYPE (-5003003) +#define GRD_INVALID_CONFIG_VALUE (-5003004) +#define GRD_INVALID_OPERATOR (-5003005) +#define GRD_INVALID_PROJECTION_FIELD (-5003006) +#define GRD_INVALID_PROJECTION_VALUE (-5003007) +#define GRD_ARRAY_INDEX_NOT_FOUND (-5003008) + +// System err +#define GRD_JSON_LIB_HANDLE_FAILED (-5004001) + +// no data +#define GRD_COLLECTION_NOT_FOUND (-5011001) +#define GRD_RECORD_NOT_FOUND (-5011002) +#define GRD_DB_NOT_FOUND (-11003) +#define GRD_FIELD_NOT_FOUND (-5011004) + +// data conflicted +#define GRD_COLLECTION_CONFLICT (-5016001) +#define GRD_KEY_CONFLICT (-5016002) +#define GRD_FIELD_TYPE_CONFLICT (-5016003) + +// Cursor or ResultSet not available +#define GRD_RESULT_SET_NOT_AVAILABLE (-5019001) + +// Invalid format +#define GRD_INVALID_JSON_FORMAT (-5037001) +#define GRD_INVALID_KEY_FORMAT (-5037002) +#define GRD_INVALID_COLLECTION_NAME (-5037003) +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // GRD_ERROR_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_resultset_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_resultset_api.h new file mode 100644 index 00000000..c9365f18 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_resultset_api.h @@ -0,0 +1,40 @@ +/* +* 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 GRD_RESULTSET_API_H +#define GRD_RESULTSET_API_H + +#include + +#include "grd_base/grd_type_export.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef struct GRD_ResultSet GRD_ResultSet; + +GRD_API int32_t GRD_Next(GRD_ResultSet *resultSet); + +GRD_API int32_t GRD_GetValue(GRD_ResultSet *resultSet, char **value); + +GRD_API int32_t GRD_FreeValue(char *value); + +GRD_API int32_t GRD_FreeResultSet(GRD_ResultSet *resultSet); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // GRD_RESULTSET_API_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_type_export.h b/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_type_export.h new file mode 100644 index 00000000..d116ea22 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_base/grd_type_export.h @@ -0,0 +1,69 @@ +/* +* 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 GRD_TYPE_EXPORT_H +#define GRD_TYPE_EXPORT_H + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#ifndef _WIN32 +#define GRD_API __attribute__((visibility("default"))) +#endif + +typedef struct GRD_DB GRD_DB; + +/** + * @brief Open database config + */ +#define GRD_DB_OPEN_ONLY 0x00 +#define GRD_DB_OPEN_CREATE 0x01 +// check data in database if close abnormally last time, if data is corrupted, rebuild the database +#define GRD_DB_OPEN_CHECK_FOR_ABNORMAL 0x02 +// check data in database when open database, if data is corrupted, rebuild the database. +#define GRD_DB_OPEN_CHECK 0x04 + +/** + * @brief Close database config + */ +#define GRD_DB_CLOSE 0x00 +#define GRD_DB_CLOSE_IGNORE_ERROR 0x01 + +/** + * @brief flush database config + */ +#define GRD_DB_FLUSH_ASYNC 0x00 +#define GRD_DB_FLUSH_SYNC 0x01 + +#define GRD_DOC_ID_DISPLAY 0x01 +typedef struct Query { + const char *filter; + const char *projection; +} Query; + +/** + * @brief Flags for create and drop collection + */ +#define CHK_EXIST_COLLECTION 1 +#define CHK_NON_EXIST_COLLECTION 1 + +#define GRD_DOC_APPEND 0 +#define GRD_DOC_REPLACE 1 + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // GRD_TYPE_EXPORT_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_document/grd_document_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_document/grd_document_api.h new file mode 100644 index 00000000..bd6e77c2 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/include/grd_document/grd_document_api.h @@ -0,0 +1,48 @@ +/* +* 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 GRD_DOCUMENT_API_H +#define GRD_DOCUMENT_API_H + +#include + +#include "grd_base/grd_resultset_api.h" +#include "grd_base/grd_type_export.h" + +#ifdef __cplusplus +extern "C" { +#endif + +GRD_API int32_t GRD_CreateCollection(GRD_DB *db, const char *collectionName, const char *optionStr, uint32_t flags); + +GRD_API int32_t GRD_DropCollection(GRD_DB *db, const char *collectionName, uint32_t flags); + +GRD_API int32_t GRD_InsertDoc(GRD_DB *db, const char *collectionName, const char *document, uint32_t flags); + +GRD_API int32_t GRD_FindDoc(GRD_DB *db, const char *collectionName, Query query, uint32_t flags, + GRD_ResultSet **resultSet); + +GRD_API int32_t GRD_UpdateDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *update, + uint32_t flags); + +GRD_API int32_t GRD_UpsertDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *document, + uint32_t flags); + +GRD_API int32_t GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, uint32_t flags); + +#ifdef __cplusplus +} +#endif +#endif // GRD_DOCUMENT_API_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/base/grd_db_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/base/grd_db_api.cpp new file mode 100644 index 00000000..55a7c04e --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/base/grd_db_api.cpp @@ -0,0 +1,33 @@ +/* +* 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. +*/ + +#include "grd_base/grd_db_api.h" + +#include "grd_base/grd_error.h" + +int32_t GRD_DBOpen(const char *dbPath, const char *configStr, uint32_t flags, GRD_DB **db) +{ + return GRD_OK; +} + +int32_t GRD_DBClose(GRD_DB *db, uint32_t flags) +{ + return GRD_OK; +} + +int32_t GRD_Flush(GRD_DB *db, uint32_t flags) +{ + return GRD_OK; +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/grd_document_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/grd_document_api.cpp new file mode 100644 index 00000000..82059110 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/grd_document_api.cpp @@ -0,0 +1,53 @@ +/* +* 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. +*/ + +#include "grd_document/grd_document_api.h" + +#include "grd_base/grd_error.h" + +int32_t GRD_CreateCollection(GRD_DB *db, const char *collectionName, const char *optionStr, uint32_t flags) +{ + return GRD_OK; +} + +int32_t GRD_DropCollection(GRD_DB *db, const char *collectionName, uint32_t flags) +{ + return GRD_OK; +} + +int32_t GRD_UpdateDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *update, uint32_t flags) +{ + return GRD_OK; +} + +int32_t GRD_UpsertDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *document, uint32_t flags) +{ + return GRD_OK; +} + +int32_t GRD_InsertDoc(GRD_DB *db, const char *collectionName, const char *document, uint32_t flags) +{ + return GRD_OK; +} + +int32_t GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, uint32_t flags) +{ + return GRD_OK; +} + +int32_t GRD_FindDoc(GRD_DB *db, const char *collectionName, Query query, uint32_t flags, GRD_ResultSet **resultSet) +{ + return GRD_OK; +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/grd_resultset_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/grd_resultset_api.cpp new file mode 100644 index 00000000..4349f9f3 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/grd_resultset_api.cpp @@ -0,0 +1,37 @@ +/* +* 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. +*/ +#include "grd_base/grd_resultset_api.h" + +#include "grd_base/grd_error.h" + +int32_t GRD_Next(GRD_ResultSet *resultSet) +{ + return GRD_OK; +} + +int32_t GRD_GetValue(GRD_ResultSet *resultSet, char **value) +{ + return GRD_OK; +} + +int32_t GRD_FreeValue(char *value) +{ + return GRD_OK; +} + +int32_t GRD_FreeResultSet(GRD_ResultSet *resultSet) +{ + return GRD_OK; +} \ No newline at end of file -- Gitee