From 7da511346418ca737a9944f26aea456924cb059f Mon Sep 17 00:00:00 2001 From: htt1997 Date: Thu, 1 Jun 2023 16:05:54 +0800 Subject: [PATCH 1/4] update Signed-off-by: htt1997 --- datamgr_service/BUILD.gn | 1 + datamgr_service/Readme.md | 4 +- datamgr_service/bundle.json | 9 +- datamgr_service/conf/config.json | 13 + .../src/softbus_adapter_standard.cpp | 2 +- .../adapter/dfx/test/BUILD.gn | 5 +- .../unittest/distributeddata_dfx_ut_test.cpp | 16 +- .../adapter/include/dfx/reporter.h | 11 + .../include/permission/permission_validator.h | 2 + .../permission/src/permission_validator.cpp | 7 + .../distributeddataservice/app/BUILD.gn | 2 +- .../app/distributed_data.cfg | 2 +- .../app/src/feature_stub_impl.cpp | 6 +- .../app/src/flowctrl_manager/BUILD.gn | 1 - .../app/src/kvstore_data_service.cpp | 26 +- .../app/src/kvstore_data_service.h | 2 +- .../app/src/kvstore_meta_manager.cpp | 2 +- .../app/src/kvstore_meta_manager.h | 2 +- .../src/session_manager/upgrade_manager.cpp | 5 +- .../app/src/uninstaller/uninstaller_impl.cpp | 7 +- .../app/src/uninstaller/uninstaller_impl.h | 5 +- .../distributeddataservice/framework/BUILD.gn | 6 +- .../framework/cloud/cloud_event.cpp | 9 +- .../framework/cloud/cloud_info.cpp | 34 +- .../framework/cloud/schema_meta.cpp | 21 +- .../framework/cloud/sync_event.cpp | 71 ++ .../framework/directory/directory_manager.cpp | 269 ++++++++ .../framework/feature/feature_system.cpp | 10 +- .../framework/include/cloud/change_event.h | 30 + .../framework/include/cloud/cloud_event.h | 6 +- .../framework/include/cloud/cloud_info.h | 8 +- .../framework/include/cloud/schema_meta.h | 58 +- .../framework/include/cloud/sync_event.h | 53 ++ .../include/directory/directory_manager.h | 69 ++ .../framework/include/error/general_error.h | 2 + .../include/feature/feature_system.h | 7 +- .../framework/include/store/auto_cache.h | 3 +- .../framework/include/store/general_store.h | 32 +- .../framework/include/store/general_value.h | 29 +- .../framework/include/store/general_watcher.h | 29 +- .../framework/include/utils/constant.h | 41 +- .../include/utils}/ref_count.h | 2 +- .../metadata/store_meta_data_local.cpp | 4 +- .../framework/store/auto_cache.cpp | 45 +- .../framework/utils/constant.cpp | 10 + .../kvdb => framework/utils}/ref_count.cpp | 4 +- .../distributeddataservice/service/BUILD.gn | 3 + .../service/backup/src/backup_manager.cpp | 7 +- .../service/bootstrap/src/bootstrap.cpp | 5 +- .../service/cloud/cloud_service_impl.cpp | 311 ++++----- .../service/cloud/cloud_service_impl.h | 37 +- .../service/cloud/cloud_service_stub.cpp | 7 + .../service/cloud/sync_manager.cpp | 302 ++++++++ .../service/cloud/sync_manager.h | 85 +++ .../config/include/model/directory_config.h | 2 +- .../data_share/data_share_service_impl.cpp | 6 + .../data_share/data_share_service_impl.h | 3 + .../service/kvdb/kvdb_exporter.cpp | 2 +- .../service/kvdb/kvdb_service_impl.cpp | 70 +- .../service/kvdb/kvdb_service_impl.h | 5 +- .../service/kvdb/kvdb_service_stub.cpp | 70 +- .../service/kvdb/store_cache.cpp | 20 +- .../service/kvdb/store_cache.h | 4 +- .../service/kvdb/upgrade.cpp | 6 +- .../service/matrix/include/device_matrix.h | 4 + .../service/matrix/src/device_matrix.cpp | 20 +- .../service/object/object_service_impl.cpp | 7 +- .../service/object/object_service_impl.h | 2 - .../service/rdb/rdb_cloud.cpp | 124 ++++ .../service/rdb/rdb_cloud.h | 47 ++ .../service/rdb/rdb_cursor.h | 2 +- .../service/rdb/rdb_general_store.cpp | 210 +++++- .../service/rdb/rdb_general_store.h | 44 +- .../service/rdb/rdb_notifier_proxy.cpp | 23 +- .../service/rdb/rdb_notifier_proxy.h | 4 +- .../service/rdb/rdb_query.cpp | 5 + .../service/rdb/rdb_query.h | 7 +- .../service/rdb/rdb_service_impl.cpp | 353 +++++----- .../service/rdb/rdb_service_impl.h | 99 ++- .../service/rdb/rdb_service_stub.cpp | 59 +- .../service/rdb/rdb_service_stub.h | 24 - .../service/rdb/rdb_store_observer_impl.cpp | 6 +- .../service/rdb/rdb_store_observer_impl.h | 7 +- .../service/rdb/rdb_syncer.cpp | 133 ++-- .../service/rdb/rdb_syncer.h | 68 +- .../service/rdb/rdb_watcher.cpp | 37 +- .../service/rdb/rdb_watcher.h | 18 +- .../service/rdb/value_proxy.cpp | 81 ++- .../service/rdb/value_proxy.h | 9 + .../service/test/cloud_data_test.cpp | 4 +- kv_store/bundle.json | 10 +- kv_store/frameworks/CMakeLists.txt | 28 +- kv_store/frameworks/common/executor.h | 2 +- kv_store/frameworks/common/lru_bucket.h | 12 +- kv_store/frameworks/common/pool.h | 2 +- kv_store/frameworks/common/priority_queue.h | 6 +- kv_store/frameworks/common/traits.h | 3 +- .../include/ikvstore_data_service.h | 33 +- .../src/ikvstore_data_service.cpp | 42 +- .../src/kvstore_service_death_notifier.cpp | 109 ++- .../src/kvstore_service_death_notifier.h | 24 +- .../test/unittest/device_kvstore_test.cpp | 28 +- .../local_subscribe_device_store_test.cpp | 7 +- .../unittest/local_subscribe_store_test.cpp | 7 +- .../unittest/single_kvstore_client_test.cpp | 28 +- .../innerkitsimpl/kvdb/include/dev_manager.h | 16 - .../kvdb/include/single_store_impl.h | 5 - .../innerkitsimpl/kvdb/src/backup_manager.cpp | 6 +- .../innerkitsimpl/kvdb/src/dev_manager.cpp | 85 +-- .../kvdb/src/kvdb_service_client.cpp | 33 +- .../kvdb/src/single_store_impl.cpp | 88 +-- .../innerkitsimpl/kvdb/src/store_factory.cpp | 8 +- .../innerkitsimpl/kvdb/src/store_manager.cpp | 11 +- .../kvdb/test/store_factory_test.cpp | 2 + .../distributeddata/include/js_field_node.h | 22 +- .../distributeddata/include/js_kv_store.h | 4 +- .../include/js_kv_store_resultset.h | 1 - .../distributeddata/include/js_query.h | 2 +- .../distributeddata/include/js_schema.h | 14 +- .../distributeddata/include/js_util.h | 23 +- .../distributeddata/include/napi_queue.h | 3 +- .../distributeddata/include/uv_queue.h | 2 +- .../src/js_const_properties.cpp | 4 +- .../src/js_device_kv_store.cpp | 9 +- .../distributeddata/src/js_field_node.cpp | 50 +- .../distributeddata/src/js_kv_manager.cpp | 4 +- .../distributeddata/src/js_kv_store.cpp | 4 +- .../src/js_kv_store_resultset.cpp | 4 +- .../distributeddata/src/js_query.cpp | 6 +- .../distributeddata/src/js_schema.cpp | 58 +- .../src/js_single_kv_store.cpp | 13 +- .../distributeddata/src/js_util.cpp | 86 ++- .../include/js_error_utils.h | 2 +- .../include/js_field_node.h | 22 +- .../include/js_kv_store_resultset.h | 1 - .../distributedkvstore/include/js_query.h | 2 +- .../distributedkvstore/include/js_schema.h | 14 +- .../include/js_single_kv_store.h | 2 +- .../distributedkvstore/include/js_util.h | 30 +- .../distributedkvstore/include/uv_queue.h | 2 +- .../src/js_const_properties.cpp | 2 +- .../src/js_device_kv_store.cpp | 4 +- .../distributedkvstore/src/js_error_utils.cpp | 4 +- .../distributedkvstore/src/js_field_node.cpp | 56 +- .../distributedkvstore/src/js_kv_manager.cpp | 2 +- .../src/js_kv_store_resultset.cpp | 5 +- .../distributedkvstore/src/js_query.cpp | 6 +- .../distributedkvstore/src/js_schema.cpp | 54 +- .../src/js_single_kv_store.cpp | 93 +-- .../distributedkvstore/src/js_util.cpp | 69 +- .../frameworks/libs/distributeddb/BUILD.gn | 1 + .../relational/relational_schema_object.h | 1 + .../common/include/relational/table_info.h | 4 + .../distributeddb/common/src/db_constant.cpp | 2 +- .../libs/distributeddb/common/src/query.cpp | 5 + .../relational/relational_schema_object.cpp | 18 + .../common/src/relational/table_info.cpp | 11 + .../libs/distributeddb/distributeddb.gni | 1 + .../libs/distributeddb/include/query.h | 2 +- .../include/cloud/cloud_store_types.h | 47 +- .../interfaces/include/cloud/icloud_db.h | 3 +- .../relational/relational_store_delegate.h | 19 +- .../interfaces/include/runtime_config.h | 3 + .../interfaces/include/store_observer.h | 8 +- .../interfaces/include/store_types.h | 33 +- .../relational_store_delegate_impl.cpp | 13 +- .../relational_store_delegate_impl.h | 6 +- .../relational_store_sqlite_ext.cpp | 35 +- .../interfaces/src/runtime_config.cpp | 4 + .../storage/include/isync_interface.h | 2 + .../include/relational_store_connection.h | 2 +- .../storage/include/sync_generic_interface.h | 2 +- .../sqlite/cloud_sync_log_table_manager.cpp | 120 ++++ .../src/sqlite/cloud_sync_log_table_manager.h | 43 ++ .../collaboration_log_table_manager.cpp | 2 +- .../src/sqlite/log_table_manager_factory.cpp | 15 +- .../src/sqlite/log_table_manager_factory.h | 4 +- .../sqlite_relational_database_upgrader.cpp | 53 +- .../sqlite_relational_database_upgrader.h | 2 + .../relational/sqlite_relational_store.cpp | 6 +- .../relational/sqlite_relational_store.h | 2 +- .../sqlite_relational_store_connection.cpp | 4 +- .../sqlite_relational_store_connection.h | 2 +- ...qlite_single_relational_storage_engine.cpp | 14 +- .../sqlite_single_relational_storage_engine.h | 5 +- .../sqlite/split_device_log_table_manager.cpp | 2 +- .../src/sqlite/sqlite_log_table_manager.cpp | 4 +- .../storage/src/sqlite/sqlite_query_helper.h | 2 +- ...e_single_ver_relational_continue_token.cpp | 4 +- ...single_ver_relational_storage_executor.cpp | 25 +- ...e_single_ver_relational_storage_executor.h | 12 +- .../sqlite/sqlite_single_ver_storage_engine.h | 2 +- .../sqlite_single_ver_storage_executor.h | 6 +- .../src/sqlite/sqlite_storage_engine.h | 2 +- .../storage/src/sqlite/sqlite_utils.cpp | 1 - .../distributeddb/syncer/src/meta_data.cpp | 2 +- .../libs/distributeddb/syncer/src/meta_data.h | 2 +- .../syncer/src/remote_executor.cpp | 54 +- .../syncer/src/remote_executor.h | 3 +- .../syncer/src/remote_executor_packet.cpp | 15 + .../syncer/src/remote_executor_packet.h | 8 +- .../syncer/src/single_ver_data_sync.cpp | 10 +- .../syncer/src/single_ver_data_sync.h | 4 +- ...ingle_ver_relational_sync_task_context.cpp | 4 +- .../distributeddb/syncer/src/sync_types.h | 1 + .../libs/distributeddb/test/BUILD.gn | 7 + .../relationalstoredelegate_fuzzer.cpp | 3 +- .../distributeddb_communicator_test.cpp | 2 +- .../distributeddb_interfaces_log_test.cpp | 4 +- ...uteddb_interfaces_register_syncdb_test.cpp | 5 + ...buteddb_interfaces_relational_ext_test.cpp | 361 ++++++++++ ...uteddb_interfaces_relational_sync_test.cpp | 29 +- ...stributeddb_interfaces_relational_test.cpp | 643 +++++++++++++----- ...stributeddb_relational_multi_user_test.cpp | 3 +- .../virtual_communicator_aggregator.cpp | 1 - ...rtual_relational_ver_sync_db_interface.cpp | 6 +- .../distributeddata/include/kv_utils.h | 12 +- .../distributeddata/distributed_data.js | 94 +-- .../distributedkvstore/distributed_kvstore.js | 94 +-- .../innerkitsimpl/kvdb/src/store_factory.cpp | 2 +- .../innerkitsimpl/kvdb/src/store_manager.cpp | 9 +- .../distributeddata/src/js_util.cpp | 24 +- .../libaccesstoken_sdk/include/access_token.h | 119 +++- .../include/access_token_error.h | 71 ++ .../include/accesstoken_kit.h | 588 ++++++++-------- .../include/hap_token_info.h | 65 +- .../include/native_token_info.h | 42 +- .../perm_state_change_callback_customize.h | 48 +- .../include/permission_def.h | 45 +- .../include/permission_dlp_mode.h | 33 +- .../include/permission_list_state.h | 33 +- .../include/permission_state_change_info.h | 48 +- .../include/permission_state_full.h | 38 +- relational_store/CMakeLists.txt | 31 +- relational_store/bundle.json | 15 + .../js/napi/common/include/js_utils.h | 92 +-- .../js/napi/common/src/js_utils.cpp | 150 ++-- .../dataability/src/napi_predicates_utils.cpp | 2 +- .../js/napi/rdb/include/napi_rdb_js_utils.h | 34 + .../js/napi/rdb/src/napi_rdb_js_utils.cpp | 71 ++ .../js/napi/rdb/src/napi_rdb_store.cpp | 11 +- .../js/napi/rdb/src/napi_result_set.cpp | 1 + .../relationalstore/include/napi_result_set.h | 6 - .../relationalstore/src/napi_rdb_js_utils.cpp | 50 +- .../relationalstore/src/napi_rdb_store.cpp | 73 +- .../src/ishared_result_set_proxy.cpp | 3 +- .../src/ishared_result_set_stub.cpp | 2 +- .../native/rdb/include/irdb_service.h | 9 +- .../native/rdb/include/rdb_manager_impl.h | 7 +- .../native/rdb/include/rdb_notifier_stub.h | 7 +- .../native/rdb/include/rdb_service_proxy.h | 43 +- .../native/rdb/include/rdb_store_impl.h | 11 +- .../native/rdb/include/rdb_store_manager.h | 7 +- .../native/rdb/include/rdb_types_util.h | 25 +- .../native/rdb/include/result_set_proxy.h | 1 + .../include/shared_block_serializer_info.h | 5 +- .../native/rdb/include/sqlite_statement.h | 1 + .../native/rdb/include/step_result_set.h | 1 + .../native/rdb/include/task_executor.h | 39 ++ .../rdb/mock/include/rdb_store_manager.h | 6 +- .../native/rdb/mock/include/task_executor.h | 81 +++ .../native/rdb/mock/src/task_executor.cpp | 43 ++ .../native/rdb/src/abs_rdb_predicates.cpp | 35 +- .../native/rdb/src/abs_shared_result_set.cpp | 25 + .../native/rdb/src/raw_data_parser.cpp | 20 +- .../frameworks/native/rdb/src/rdb_helper.cpp | 3 +- .../native/rdb/src/rdb_manager_impl.cpp | 102 ++- .../native/rdb/src/rdb_notifier_stub.cpp | 41 +- .../native/rdb/src/rdb_service_proxy.cpp | 128 ++-- .../native/rdb/src/rdb_store_impl.cpp | 101 +-- .../native/rdb/src/rdb_store_manager.cpp | 55 +- .../native/rdb/src/rdb_types_util.cpp | 45 +- .../native/rdb/src/result_set_proxy.cpp | 5 + .../frameworks/native/rdb/src/share_block.cpp | 17 +- .../rdb/src/shared_block_serializer_info.cpp | 22 +- .../native/rdb/src/sqlite_statement.cpp | 22 +- .../native/rdb/src/sqlite_utils.cpp | 2 +- .../native/rdb/src/step_result_set.cpp | 10 + .../native/rdb/src/task_executor.cpp | 47 ++ .../native/rdb/src/values_bucket.cpp | 6 +- .../interfaces/inner_api/appdatafwk/BUILD.gn | 4 +- .../cloud_data/include/cloud_service.h | 3 +- .../interfaces/inner_api/rdb/BUILD.gn | 14 +- .../rdb/include/abs_rdb_predicates.h | 17 +- .../inner_api/rdb/include/abs_result_set.h | 4 +- .../rdb/include/abs_shared_result_set.h | 8 +- .../inner_api/rdb/include/rdb_errno.h | 8 +- .../inner_api/rdb/include/rdb_notifier.h | 11 +- .../inner_api/rdb/include/rdb_service.h | 36 +- .../inner_api/rdb/include/rdb_store.h | 27 +- .../inner_api/rdb/include/rdb_store_config.h | 2 +- .../inner_api/rdb/include/rdb_types.h | 69 +- .../inner_api/rdb/include/rdb_visibility.h | 8 +- .../inner_api/rdb/include/result_set.h | 7 + .../inner_api/rdb/include/shared_result_set.h | 4 +- .../inner_api/rdb/include/values_bucket.h | 14 +- .../inner_api/rdb/mock/include/result_set.h | 1 - .../rdb_data_ability_adapter/BUILD.gn | 1 - relational_store/interfaces/ndk/BUILD.gn | 59 ++ .../interfaces/ndk/include/ndk_logger.h | 34 + .../ndk/include/relational_cursor.h | 58 ++ .../ndk/include/relational_error_code.h | 33 + .../ndk/include/relational_predicates.h | 67 ++ .../interfaces/ndk/include/relational_store.h | 66 ++ .../ndk/include/relational_value_object.h | 40 ++ .../ndk/include/relational_values_bucket.h | 44 ++ .../interfaces/ndk/libnative_rdb.ndk.json | 68 ++ .../interfaces/ndk/src/relational_cursor.cpp | 234 +++++++ .../ndk/src/relational_cursor_impl.h | 50 ++ .../ndk/src/relational_predicates.cpp | 383 +++++++++++ .../ndk/src/relational_predicates_impl.h | 88 +++ .../interfaces/ndk/src/relational_store.cpp | 272 ++++++++ .../ndk/src/relational_store_impl.h} | 86 +-- .../ndk/src/relational_value_object.cpp | 112 +++ .../ndk/src/relational_value_object_impl.h | 38 ++ .../ndk/src/relational_values_bucket.cpp | 135 ++++ .../ndk/src/relational_values_bucket_impl.h | 38 ++ .../test/native/rdb/unittest/common.h | 6 +- .../rdb/unittest/rdb_get_store_test.cpp | 22 +- .../rdb/unittest/rdb_predicates_test.cpp | 2 +- .../rdb/unittest/rdb_store_subscribe_test.cpp | 146 ++++ .../rdb/unittest/rdb_value_bucket_test.cpp | 4 +- 322 files changed, 8638 insertions(+), 2882 deletions(-) create mode 100644 datamgr_service/services/distributeddataservice/framework/cloud/sync_event.cpp create mode 100644 datamgr_service/services/distributeddataservice/framework/directory/directory_manager.cpp create mode 100644 datamgr_service/services/distributeddataservice/framework/include/cloud/change_event.h create mode 100644 datamgr_service/services/distributeddataservice/framework/include/cloud/sync_event.h create mode 100644 datamgr_service/services/distributeddataservice/framework/include/directory/directory_manager.h rename datamgr_service/services/distributeddataservice/{service/kvdb => framework/include/utils}/ref_count.h (97%) rename datamgr_service/services/distributeddataservice/{service/kvdb => framework/utils}/ref_count.cpp (95%) create mode 100644 datamgr_service/services/distributeddataservice/service/cloud/sync_manager.cpp create mode 100644 datamgr_service/services/distributeddataservice/service/cloud/sync_manager.h create mode 100644 datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.cpp create mode 100644 datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.h create mode 100644 kv_store/frameworks/libs/distributeddb/storage/src/sqlite/cloud_sync_log_table_manager.cpp create mode 100644 kv_store/frameworks/libs/distributeddb/storage/src/sqlite/cloud_sync_log_table_manager.h create mode 100644 kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_ext_test.cpp create mode 100644 mock/innerkits/access_token/libaccesstoken_sdk/include/access_token_error.h create mode 100644 relational_store/frameworks/js/napi/rdb/include/napi_rdb_js_utils.h create mode 100644 relational_store/frameworks/js/napi/rdb/src/napi_rdb_js_utils.cpp create mode 100644 relational_store/frameworks/native/rdb/include/task_executor.h create mode 100644 relational_store/frameworks/native/rdb/mock/include/task_executor.h create mode 100644 relational_store/frameworks/native/rdb/mock/src/task_executor.cpp create mode 100644 relational_store/frameworks/native/rdb/src/task_executor.cpp create mode 100644 relational_store/interfaces/ndk/BUILD.gn create mode 100644 relational_store/interfaces/ndk/include/ndk_logger.h create mode 100644 relational_store/interfaces/ndk/include/relational_cursor.h create mode 100644 relational_store/interfaces/ndk/include/relational_error_code.h create mode 100644 relational_store/interfaces/ndk/include/relational_predicates.h create mode 100644 relational_store/interfaces/ndk/include/relational_store.h create mode 100644 relational_store/interfaces/ndk/include/relational_value_object.h create mode 100644 relational_store/interfaces/ndk/include/relational_values_bucket.h create mode 100644 relational_store/interfaces/ndk/libnative_rdb.ndk.json create mode 100644 relational_store/interfaces/ndk/src/relational_cursor.cpp create mode 100644 relational_store/interfaces/ndk/src/relational_cursor_impl.h create mode 100644 relational_store/interfaces/ndk/src/relational_predicates.cpp create mode 100644 relational_store/interfaces/ndk/src/relational_predicates_impl.h create mode 100644 relational_store/interfaces/ndk/src/relational_store.cpp rename relational_store/{frameworks/native/rdb/mock/src/timer.cpp => interfaces/ndk/src/relational_store_impl.h} (34%) create mode 100644 relational_store/interfaces/ndk/src/relational_value_object.cpp create mode 100644 relational_store/interfaces/ndk/src/relational_value_object_impl.h create mode 100644 relational_store/interfaces/ndk/src/relational_values_bucket.cpp create mode 100644 relational_store/interfaces/ndk/src/relational_values_bucket_impl.h create mode 100644 relational_store/test/native/rdb/unittest/rdb_store_subscribe_test.cpp diff --git a/datamgr_service/BUILD.gn b/datamgr_service/BUILD.gn index 8c8a8314..f2510207 100644 --- a/datamgr_service/BUILD.gn +++ b/datamgr_service/BUILD.gn @@ -18,6 +18,7 @@ group("build_native_test") { "services/distributeddataservice/adapter/test:unittest", "services/distributeddataservice/app/test:unittest", "services/distributeddataservice/framework/test:unittest", + "services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest:unittest", "services/distributeddataservice/service/test:unittest", ] } diff --git a/datamgr_service/Readme.md b/datamgr_service/Readme.md index d9572b68..8af31b6f 100644 --- a/datamgr_service/Readme.md +++ b/datamgr_service/Readme.md @@ -61,7 +61,7 @@ You call APIs of DDS to create, access, and subscribe to distributed databases. - For the single KV store, the maximum size of a key is 1 KB, and that of a value is 4 MB. - Each app can open a maximum of 16 databases simultaneously. -- DDS cannot completely replace the database in the service sandbox for storing data, because the storage types supported by them are not completely the same. You need to determine the data to be synchronized in distributed mode and store the data in DDS. +- DDS cannot completely replace the database in the service sandbox for storing data, because the storage types supported by them are not completely the same. You need to determine the data to be synchronized in distributed mode_ and store the data in DDS. - Currently, DDS does not allow customization of conflict resolution policies. - DDS supports a maximum of 1000 **KvStore** API calls per second, and 10,000 per minute. It supports a maximum of 50 **KvManager** API calls per second, and 500 per minute. @@ -93,7 +93,7 @@ Some basic concepts related to the DDS are as follows: After discovering and authenticating a device, the bottom-layer communication component notifies the DDS that the device goes online. After receiving the notification, DDS establishes an encrypted transmission channel to synchronize data between the two devices. - DDS provides both manual and automatic synchronization. In manual synchronization, you can specify the list of target devices and the synchronization mode \(PULL, PUSH, or PULL\_PUSH\). In automatic synchronization, the distributed database synchronizes data \(when devices go online or data is modified\), and you are unaware of the synchronization. + DDS provides both manual and automatic synchronization. In manual synchronization, you can specify the list of target devices and the synchronization mode_ \(PULL, PUSH, or PULL\_PUSH\). In automatic synchronization, the distributed database synchronizes data \(when devices go online or data is modified\), and you are unaware of the synchronization. - **Single KV store** diff --git a/datamgr_service/bundle.json b/datamgr_service/bundle.json index d7325b51..c0a31d6a 100644 --- a/datamgr_service/bundle.json +++ b/datamgr_service/bundle.json @@ -1,6 +1,6 @@ { - "name": "@ohos/distributeddatamgr_datamgr_service", - "version": "1.0.0", + "name": "@ohos/datamgr_service", + "version": "3.2.0", "description": "Distributed data manager that provides the capability to store data in the databases of different devices", "homePage": "https://gitee.com/openharmony", "license": "Apache V2", @@ -46,8 +46,8 @@ "adapted_system_type": [ "standard" ], - "rom": "", - "ram": "", + "rom": "5120KB", + "ram": "8192KB", "hisysevent_config": [ "//foundation/distributeddatamgr/datamgr_service/hisysevent.yaml" ], @@ -90,6 +90,7 @@ "//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/services/distributeddataservice/service/data_share:build_module", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/service/data_share/gaussdb_rd:build_module" ], "inner_kits": [], diff --git a/datamgr_service/conf/config.json b/datamgr_service/conf/config.json index 969a2761..cf89a760 100644 --- a/datamgr_service/conf/config.json +++ b/datamgr_service/conf/config.json @@ -4,12 +4,25 @@ "version": "000.000.001", "features": ["kvdb", "rdb", "object", "backup", "data_sync"], "components": [ + { + "description": "3rd party adapter", + "lib": "libconfigdemo.z.so", + "constructor": "", + "destructor": "", + "params": { + "key": "value", + "count": 1 + } + }, { "description": "cloud data interface adapter", "lib": "libdistributedclouddata.z.so" }, { "lib": "libudmf_server.z.so" + }, + { + "lib": "libdata_share_service.z.so" } ], "bundleChecker": { diff --git a/datamgr_service/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp b/datamgr_service/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp index e86d1945..d7c2d786 100644 --- a/datamgr_service/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp +++ b/datamgr_service/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp @@ -435,7 +435,7 @@ void SoftBusAdapter::SofBusDeviceChangeListenerImpl::OnDeviceChanged(const AppDi } CommunicationStrategy::GetInstance().SetStrategy(info.uuid, strategy, - [this](const std::string deviceId, Strategy strategy) { + [this](const std::string &deviceId, Strategy strategy) { std::shared_ptr conn = SoftBusAdapter::GetInstance()->GetConnect(deviceId); if (conn != nullptr) { conn->AfterStrategyUpdate(strategy); diff --git a/datamgr_service/services/distributeddataservice/adapter/dfx/test/BUILD.gn b/datamgr_service/services/distributeddataservice/adapter/dfx/test/BUILD.gn index de03ed06..0c2a6050 100644 --- a/datamgr_service/services/distributeddataservice/adapter/dfx/test/BUILD.gn +++ b/datamgr_service/services/distributeddataservice/adapter/dfx/test/BUILD.gn @@ -118,6 +118,9 @@ group("unittest") { testonly = true deps = [] - deps += [ ":DistributeddataDfxMSTTest" ] + deps += [ + ":DistributeddataDfxMSTTest", + ":DistributeddataDfxUTTest", + ] } ############################################################################### diff --git a/datamgr_service/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp b/datamgr_service/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp index b93b5632..f4887d3c 100644 --- a/datamgr_service/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp +++ b/datamgr_service/services/distributeddataservice/adapter/dfx/test/unittest/distributeddata_dfx_ut_test.cpp @@ -35,21 +35,25 @@ public: void DistributedataDfxUTTest::SetUpTestCase() { - size_t max = 12; - size_t min = 5; - Reporter::GetInstance()->SetThreadPool(std::make_shared(max, min)); FakeHivew::Clear(); } void DistributedataDfxUTTest::TearDownTestCase() { - Reporter::GetInstance()->SetThreadPool(nullptr); FakeHivew::Clear(); } -void DistributedataDfxUTTest::SetUp() {} +void DistributedataDfxUTTest::SetUp() +{ + size_t max = 12; + size_t min = 5; + Reporter::GetInstance()->SetThreadPool(std::make_shared(max, min)); +} -void DistributedataDfxUTTest::TearDown() {} +void DistributedataDfxUTTest::TearDown() +{ + Reporter::GetInstance()->SetThreadPool(nullptr); +} /** * @tc.name: Dfx001 diff --git a/datamgr_service/services/distributeddataservice/adapter/include/dfx/reporter.h b/datamgr_service/services/distributeddataservice/adapter/include/dfx/reporter.h index f84af98a..c2537b57 100644 --- a/datamgr_service/services/distributeddataservice/adapter/include/dfx/reporter.h +++ b/datamgr_service/services/distributeddataservice/adapter/include/dfx/reporter.h @@ -43,6 +43,17 @@ public: void SetThreadPool(std::shared_ptr executors) { executors_ = executors; + if (executors == nullptr) { + ServiceFault(); + RuntimeFault(); + DatabaseFault(); + CommunicationFault(); + DatabaseStatistic(); + VisitStatistic(); + TrafficStatistic(); + ApiPerformanceStatistic(); + BehaviourReporter(); + } }; private: diff --git a/datamgr_service/services/distributeddataservice/adapter/include/permission/permission_validator.h b/datamgr_service/services/distributeddataservice/adapter/include/permission/permission_validator.h index ab78f199..322d8b00 100644 --- a/datamgr_service/services/distributeddataservice/adapter/include/permission/permission_validator.h +++ b/datamgr_service/services/distributeddataservice/adapter/include/permission/permission_validator.h @@ -27,9 +27,11 @@ public: // check whether the client process have enough privilege to share data with the other devices. // tokenId: client process tokenId API_EXPORT bool CheckSyncPermission(uint32_t tokenId); + API_EXPORT bool IsCloudConfigPermit(uint32_t tokenId); private: static constexpr const char *DISTRIBUTED_DATASYNC = "ohos.permission.DISTRIBUTED_DATASYNC"; + static constexpr const char *CLOUD_DATA_CONFIG = "ohos.permission.CLOUDDATA_CONFIG"; }; } // namespace DistributedKv } // namespace OHOS diff --git a/datamgr_service/services/distributeddataservice/adapter/permission/src/permission_validator.cpp b/datamgr_service/services/distributeddataservice/adapter/permission/src/permission_validator.cpp index 93218763..7155a0fb 100644 --- a/datamgr_service/services/distributeddataservice/adapter/permission/src/permission_validator.cpp +++ b/datamgr_service/services/distributeddataservice/adapter/permission/src/permission_validator.cpp @@ -42,5 +42,12 @@ bool PermissionValidator::CheckSyncPermission(uint32_t tokenId) ZLOGE("token:0x%{public}x", tokenId); return false; } + +bool PermissionValidator::IsCloudConfigPermit(uint32_t tokenId) +{ + auto permit = AccessTokenKit::VerifyAccessToken(tokenId, CLOUD_DATA_CONFIG); + ZLOGD("cloud permit: %{public}d", permit); + return permit == PERMISSION_GRANTED; +} } // namespace DistributedKv } // namespace OHOS diff --git a/datamgr_service/services/distributeddataservice/app/BUILD.gn b/datamgr_service/services/distributeddataservice/app/BUILD.gn index 6fa9ba6a..5df04bdd 100644 --- a/datamgr_service/services/distributeddataservice/app/BUILD.gn +++ b/datamgr_service/services/distributeddataservice/app/BUILD.gn @@ -30,7 +30,7 @@ ohos_prebuilt_etc("distributed_data.cfg") { } ohos_sa_profile("distributeddata_profile") { - sources = [ "../sa_profile/1301.xml" ] + sources = [ "../sa_profile/1301.json" ] part_name = "datamgr_service" } diff --git a/datamgr_service/services/distributeddataservice/app/distributed_data.cfg b/datamgr_service/services/distributeddataservice/app/distributed_data.cfg index 3c0eb54e..45046b10 100644 --- a/datamgr_service/services/distributeddataservice/app/distributed_data.cfg +++ b/datamgr_service/services/distributeddataservice/app/distributed_data.cfg @@ -15,7 +15,7 @@ ], "services":[{ "name" : "distributeddata", - "path" : ["/system/bin/sa_main","/system/profile/distributeddata.xml"], + "path" : ["/system/bin/sa_main","/system/profile/distributeddata.json"], "uid" : "ddms", "gid" : ["system","shell","readproc","ddms"], "writepid":[ diff --git a/datamgr_service/services/distributeddataservice/app/src/feature_stub_impl.cpp b/datamgr_service/services/distributeddataservice/app/src/feature_stub_impl.cpp index cbca54b0..0a4ff147 100644 --- a/datamgr_service/services/distributeddataservice/app/src/feature_stub_impl.cpp +++ b/datamgr_service/services/distributeddataservice/app/src/feature_stub_impl.cpp @@ -13,6 +13,9 @@ * limitations under the License. */ #include "feature_stub_impl.h" + +#include "bootstrap.h" +#include "ipc_skeleton.h" namespace OHOS::DistributedData { FeatureStubImpl::FeatureStubImpl(std::shared_ptr feature) : featureImpl_(std::move(feature)) @@ -37,7 +40,8 @@ int32_t FeatureStubImpl::OnInitialize(std::shared_ptr executor) if (featureImpl_ == nullptr) { return -1; } - featureImpl_->OnExecutor(std::move(executor)); + featureImpl_->OnBind({ Bootstrap::GetInstance().GetProcessLabel(), + static_cast(IPCSkeleton::GetSelfTokenID()), std::move(executor)}); return featureImpl_->OnInitialize(); } diff --git a/datamgr_service/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn b/datamgr_service/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn index 61f30728..eae2ee83 100644 --- a/datamgr_service/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn +++ b/datamgr_service/services/distributeddataservice/app/src/flowctrl_manager/BUILD.gn @@ -23,7 +23,6 @@ ohos_static_library("distributeddata_flowctrl_static") { "${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" ] diff --git a/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.cpp b/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.cpp index 2e190156..301826ce 100644 --- a/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -50,6 +50,7 @@ #include "upgrade.h" #include "upgrade_manager.h" #include "user_delegate.h" +#include "utils/anonymous.h" #include "utils/block_integer.h" #include "utils/crypto.h" @@ -68,12 +69,24 @@ KvStoreDataService::KvStoreDataService(bool runOnCreate) : SystemAbility(runOnCreate), mutex_(), clients_() { ZLOGI("begin."); + if (executors_ == nullptr) { + constexpr size_t MAX = 12; + constexpr size_t MIN = 5; + executors_ = std::make_shared(MAX, MIN); + DistributedDB::RuntimeConfig::SetThreadPool(std::make_shared(executors_)); + } } KvStoreDataService::KvStoreDataService(int32_t systemAbilityId, bool runOnCreate) : SystemAbility(systemAbilityId, runOnCreate), mutex_(), clients_() { ZLOGI("begin"); + if (executors_ == nullptr) { + constexpr size_t MAX = 12; + constexpr size_t MIN = 5; + executors_ = std::make_shared(MAX, MIN); + DistributedDB::RuntimeConfig::SetThreadPool(std::make_shared(executors_)); + } } KvStoreDataService::~KvStoreDataService() @@ -91,7 +104,6 @@ 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(); @@ -150,16 +162,17 @@ void KvStoreDataService::LoadFeatures() { ZLOGI("begin."); auto features = FeatureSystem::GetInstance().GetFeatureName(FeatureSystem::BIND_NOW); - for (auto &feature : features) { + for (auto const &feature : features) { GetFeatureInterface(feature); } } /* RegisterClientDeathObserver */ -Status KvStoreDataService::RegisterClientDeathObserver(const AppId &appId, sptr observer) +int32_t KvStoreDataService::RegisterDeathObserver(const std::string &bundleName, sptr observer) { ZLOGD("begin."); KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); + AppId appId = { bundleName }; if (!appId.IsValid()) { ZLOGE("invalid bundleName, name:%{public}s", appId.appId.c_str()); return Status::INVALID_ARGUMENT; @@ -233,9 +246,6 @@ void KvStoreDataService::OnStart() { ZLOGI("distributeddata service onStart"); EventCenter::Defer defer; - constexpr size_t MAX = 12; - constexpr size_t MIN = 5; - executors_ = std::make_shared(MAX, MIN); Reporter::GetInstance()->SetThreadPool(executors_); AccountDelegate::GetInstance()->BindExecutor(executors_); AccountDelegate::GetInstance()->RegisterHashFunc(Crypto::Sha256); @@ -332,7 +342,7 @@ void KvStoreDataService::OnStoreMetaChanged( StoreMetaData metaData; metaData.Unmarshall({ value.begin(), value.end() }); ZLOGD("meta data info appType:%{public}s, storeId:%{public}s isDirty:%{public}d", metaData.appType.c_str(), - metaData.storeId.c_str(), metaData.isDirty); + Anonymous::Change(metaData.storeId).c_str(), metaData.isDirty); auto deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; if (metaData.deviceId != deviceId || metaData.deviceId.empty()) { ZLOGD("ignore other device change or invalid meta device"); @@ -342,7 +352,7 @@ void KvStoreDataService::OnStoreMetaChanged( if (!metaData.isDirty || metaData.appType != HARMONY_APP) { return; } - ZLOGI("dirty kv store. storeId:%{public}s", metaData.storeId.c_str()); + ZLOGI("dirty kv store. storeId:%{public}s", Anonymous::Change(metaData.storeId).c_str()); } bool KvStoreDataService::ResolveAutoLaunchParamByIdentifier( diff --git a/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.h b/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.h index 60aac352..61b3bfe2 100644 --- a/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.h @@ -50,7 +50,7 @@ public: explicit KvStoreDataService(int32_t systemAbilityId, bool runOnCreate = false); virtual ~KvStoreDataService(); - Status RegisterClientDeathObserver(const AppId &appId, sptr observer) override; + int32_t RegisterDeathObserver(const std::string &bundleName, sptr observer) override; sptr GetFeatureInterface(const std::string &name) override; diff --git a/datamgr_service/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/datamgr_service/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index 7fb54c86..c281c7a2 100644 --- a/datamgr_service/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/datamgr_service/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -28,7 +28,7 @@ #include "crypto_manager.h" #include "device_manager_adapter.h" #include "device_matrix.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "dump_helper.h" #include "eventcenter/event_center.h" #include "kvstore_data_service.h" diff --git a/datamgr_service/services/distributeddataservice/app/src/kvstore_meta_manager.h b/datamgr_service/services/distributeddataservice/app/src/kvstore_meta_manager.h index 9fb69912..db977717 100644 --- a/datamgr_service/services/distributeddataservice/app/src/kvstore_meta_manager.h +++ b/datamgr_service/services/distributeddataservice/app/src/kvstore_meta_manager.h @@ -83,7 +83,7 @@ private: void OnChange(const DistributedDB::KvStoreChangedData &data) override; std::map handlerMap_; private: - void HandleChanges(CHANGE_FLAG flag, const std::list &list); + void HandleChanges(CHANGE_FLAG flag, const std::list &entries); }; static constexpr int32_t RETRY_MAX_TIMES = 100; diff --git a/datamgr_service/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp b/datamgr_service/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp index 74c5b16f..9fee9867 100644 --- a/datamgr_service/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp +++ b/datamgr_service/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp @@ -116,8 +116,9 @@ void UpgradeManager::SetCompatibleIdentifyByType(DistributedDB::KvStoreNbDelegat auto syncIdentifier = DistributedDB::KvStoreDelegateManager::GetKvStoreIdentifier(compatibleUser, tuple.appId, tuple.storeId); - ZLOGI("set compatible identifier, store:%{public}s, user:%{public}s, device:%{public}.10s", tuple.storeId.c_str(), - compatibleUser.c_str(), DistributedData::Serializable::Marshall(devices).c_str()); + ZLOGI("set compatible identifier, store:%{public}s, user:%{public}s, device:%{public}.10s", + Anonymous::Change(tuple.storeId).c_str(), compatibleUser.c_str(), + DistributedData::Serializable::Marshall(devices).c_str()); storeDelegate->SetEqualIdentifier(syncIdentifier, devices); } diff --git a/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index cb6142eb..309ee950 100644 --- a/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -28,6 +28,7 @@ #include "metadata/store_meta_data.h" #include "permit_delegate.h" #include "cloud/cloud_info.h" +#include "utils/anonymous.h" namespace OHOS::DistributedKv { using namespace OHOS::AppDistributedKv; @@ -72,7 +73,8 @@ void UninstallEventSubscriber::OnUninstall(const std::string &bundleName, int32_ } for (auto &meta : storeMetaData) { if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) { - ZLOGI("uninstalled bundleName:%{public}s stordId:%{public}s", bundleName.c_str(), meta.storeId.c_str()); + ZLOGI("uninstalled bundleName:%{public}s stordId:%{public}s", bundleName.c_str(), + Anonymous::Change(meta.storeId).c_str()); MetaDataManager::GetInstance().DelMeta(meta.GetKey()); MetaDataManager::GetInstance().DelMeta(meta.GetSecretKey(), true); MetaDataManager::GetInstance().DelMeta(meta.GetStrategyKey()); @@ -96,7 +98,8 @@ void UninstallEventSubscriber::OnUpdate(const std::string &bundleName, int32_t u } for (auto &meta : storeMetaData) { if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) { - ZLOGI("updated bundleName:%{public}s, stordId:%{public}s", bundleName.c_str(), meta.storeId.c_str()); + ZLOGI("updated bundleName:%{public}s, stordId:%{public}s", bundleName.c_str(), + Anonymous::Change(meta.storeId).c_str()); MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true); } } diff --git a/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h b/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h index 3922fbad..3b8a3a7d 100644 --- a/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h +++ b/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h @@ -41,6 +41,7 @@ private: class UninstallerImpl : public Uninstaller { public: + UninstallerImpl() = default; ~UninstallerImpl(); Status Init(KvStoreDataService *kvStoreDataService, std::shared_ptr executors) override; @@ -50,10 +51,10 @@ public: private: static constexpr int32_t RETRY_TIME = 300; static constexpr int32_t RETRY_INTERVAL = 100; - int32_t retryTime_; + int32_t retryTime_ = 0; ExecutorPool::Task GetTask(); std::shared_ptr subscriber_ {}; - std::shared_ptr executors_; + std::shared_ptr executors_ {}; }; } // namespace OHOS::DistributedKv #endif // DISTRIBUTEDDATAMGR_UNINSTALLER_IMPL_H diff --git a/datamgr_service/services/distributeddataservice/framework/BUILD.gn b/datamgr_service/services/distributeddataservice/framework/BUILD.gn index 09a242ba..c051f1eb 100644 --- a/datamgr_service/services/distributeddataservice/framework/BUILD.gn +++ b/datamgr_service/services/distributeddataservice/framework/BUILD.gn @@ -51,6 +51,7 @@ ohos_shared_library("distributeddatasvcfwk") { "cloud/cloud_server.cpp", "cloud/schema_meta.cpp", "cloud/subscription.cpp", + "directory/directory_manager.cpp", "eventcenter/event.cpp", "eventcenter/event_center.cpp", "feature/feature_system.cpp", @@ -84,7 +85,10 @@ ohos_shared_library("distributeddatasvcfwk") { deps = [ "//third_party/openssl:libcrypto_shared" ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "hiviewdfx_hilog_native:libhilog", + ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp index cb2604bd..8146446d 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp @@ -16,16 +16,11 @@ #include "cloud/cloud_event.h" namespace OHOS::DistributedData { -CloudEvent::CloudEvent(int32_t evtId, CloudEvent::StoreInfo storeInfo, const std::string &featureName) - : Event(evtId), featureName_(featureName), storeInfo_(storeInfo) +CloudEvent::CloudEvent(int32_t evtId, CloudEvent::StoreInfo storeInfo) + : Event(evtId), storeInfo_(std::move(storeInfo)) { } -std::string CloudEvent::GetFeatureName() const -{ - return featureName_; -} - const CloudEvent::StoreInfo& CloudEvent::GetStoreInfo() const { return storeInfo_; diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp index a50f4d69..5c58d34e 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -67,19 +67,27 @@ std::string CloudInfo::GetKey() const std::map CloudInfo::GetSchemaKey() const { std::map keys; - for (const auto &app : apps) { + for (const auto &[bundle, app] : apps) { const auto key = GetKey( - SCHEMA_PREFIX, { std::to_string(user), app.bundleName, std::to_string(app.instanceId) }); + SCHEMA_PREFIX, { std::to_string(user), bundle, std::to_string(app.instanceId) }); keys.insert_or_assign(app.bundleName, key); } return keys; } -std::string CloudInfo::GetSchemaKey(const std::string &bundleName, const int32_t instanceId) const +std::string CloudInfo::GetSchemaKey(const std::string &bundleName, int32_t instanceId) const { return GetKey(SCHEMA_PREFIX, { std::to_string(user), bundleName, std::to_string(instanceId) }); } +std::string CloudInfo::GetSchemaPrefix(const std::string &bundleName) const +{ + if (bundleName.empty()) { + return GetKey(SCHEMA_PREFIX, { std::to_string(user) }); + } + return GetKey(SCHEMA_PREFIX, { std::to_string(user), bundleName}); +} + std::string CloudInfo::GetSchemaKey(const StoreMetaData &meta) { return GetKey(SCHEMA_PREFIX, { meta.user, meta.bundleName, std::to_string(meta.instanceId) }); @@ -90,14 +98,22 @@ bool CloudInfo::IsValid() const return !id.empty(); } -bool CloudInfo::IsExist(const std::string &bundleName) const +bool CloudInfo::Exist(const std::string &bundleName, int32_t instanceId) +{ + if (bundleName.empty()) { + return false; + } + auto it = apps.find(bundleName); + return it != apps.end() && it->second.bundleName == bundleName && it->second.instanceId == instanceId; +} + +bool CloudInfo::IsOn(const std::string &bundleName, int32_t instanceId) { - for (const auto &app : apps) { - if (app.bundleName == bundleName) { - return true; - } + if (bundleName.empty()) { + return false; } - return false; + auto it = apps.find(bundleName); + return it != apps.end() && it->second.instanceId == instanceId && it->second.cloudSwitch; } std::string CloudInfo::GetPrefix(const std::initializer_list &fields) diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/schema_meta.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/schema_meta.cpp index b6a8cd4b..8dd3005e 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/schema_meta.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/schema_meta.cpp @@ -18,6 +18,7 @@ namespace OHOS::DistributedData { bool SchemaMeta::Marshal(Serializable::json &node) const { SetValue(node[GET_NAME(version)], version); + SetValue(node[GET_NAME(bundleName)], bundleName); SetValue(node[GET_NAME(databases)], databases); return true; } @@ -25,11 +26,12 @@ bool SchemaMeta::Marshal(Serializable::json &node) const bool SchemaMeta::Unmarshal(const Serializable::json &node) { GetValue(node, GET_NAME(version), version); + GetValue(node, GET_NAME(bundleName), bundleName); GetValue(node, GET_NAME(databases), databases); return true; } -bool SchemaMeta::Database::Marshal(Serializable::json &node) const +bool Database::Marshal(Serializable::json &node) const { SetValue(node[GET_NAME(name)], name); SetValue(node[GET_NAME(alias)], alias); @@ -37,7 +39,7 @@ bool SchemaMeta::Database::Marshal(Serializable::json &node) const return true; } -bool SchemaMeta::Database::Unmarshal(const Serializable::json &node) +bool Database::Unmarshal(const Serializable::json &node) { GetValue(node, GET_NAME(name), name); GetValue(node, GET_NAME(alias), alias); @@ -45,7 +47,7 @@ bool SchemaMeta::Database::Unmarshal(const Serializable::json &node) return true; } -bool SchemaMeta::Table::Marshal(Serializable::json &node) const +bool Table::Marshal(Serializable::json &node) const { SetValue(node[GET_NAME(name)], name); SetValue(node[GET_NAME(alias)], alias); @@ -53,7 +55,7 @@ bool SchemaMeta::Table::Marshal(Serializable::json &node) const return true; } -bool SchemaMeta::Table::Unmarshal(const Serializable::json &node) +bool Table::Unmarshal(const Serializable::json &node) { GetValue(node, GET_NAME(name), name); GetValue(node, GET_NAME(alias), alias); @@ -61,7 +63,7 @@ bool SchemaMeta::Table::Unmarshal(const Serializable::json &node) return true; } -bool SchemaMeta::Field::Marshal(Serializable::json &node) const +bool Field::Marshal(Serializable::json &node) const { SetValue(node[GET_NAME(colName)], colName); SetValue(node[GET_NAME(alias)], alias); @@ -71,7 +73,7 @@ bool SchemaMeta::Field::Marshal(Serializable::json &node) const return true; } -bool SchemaMeta::Field::Unmarshal(const Serializable::json &node) +bool Field::Unmarshal(const Serializable::json &node) { GetValue(node, GET_NAME(colName), colName); GetValue(node, GET_NAME(alias), alias); @@ -81,7 +83,7 @@ bool SchemaMeta::Field::Unmarshal(const Serializable::json &node) return true; } -SchemaMeta::Database SchemaMeta::GetDataBase(const std::string &storeId) +Database SchemaMeta::GetDataBase(const std::string &storeId) { for (const auto &database : databases) { if (database.name == storeId) { @@ -90,4 +92,9 @@ SchemaMeta::Database SchemaMeta::GetDataBase(const std::string &storeId) } return {}; } + +bool SchemaMeta::IsValid() const +{ + return !bundleName.empty() && !databases.empty(); +} } // namespace OHOS::DistributedData \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/sync_event.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/sync_event.cpp new file mode 100644 index 00000000..814b9681 --- /dev/null +++ b/datamgr_service/services/distributeddataservice/framework/cloud/sync_event.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 "cloud/sync_event.h" + +#include +namespace OHOS::DistributedData { +SyncEvent::EventInfo::EventInfo(int32_t mode, int32_t wait, std::shared_ptr query, GenAsync async) + : mode_(mode), wait_(wait), query_(std::move(query)), asyncDetail_(std::move(async)) +{ +} + +SyncEvent::EventInfo::EventInfo(SyncEvent::EventInfo &&info) noexcept +{ + operator=(std::move(info)); +} + +SyncEvent::EventInfo &SyncEvent::EventInfo::operator=(SyncEvent::EventInfo &&info) noexcept +{ + if (this == &info) { + return *this; + } + mode_ = info.mode_; + wait_ = info.wait_; + query_ = std::move(info.query_); + asyncDetail_ = std::move(info.asyncDetail_); + return *this; +} + +SyncEvent::SyncEvent(StoreInfo storeInfo, EventInfo info) + : CloudEvent(CLOUD_SYNC, std::move(storeInfo)), info_(std::move(info)) +{ +} + +SyncEvent::SyncEvent(int32_t evtId, StoreInfo storeInfo, EventInfo info) + : CloudEvent(evtId, std::move(storeInfo)), info_(std::move(info)) +{ +} + +int32_t SyncEvent::GetMode() const +{ + return info_.mode_; +} + +int32_t SyncEvent::GetWait() const +{ + return info_.wait_; +} + +std::shared_ptr SyncEvent::GetQuery() const +{ + return info_.query_; +} + +GenAsync SyncEvent::GetAsyncDetail() const +{ + return info_.asyncDetail_; +} +} // namespace OHOS::DistributedData \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/framework/directory/directory_manager.cpp b/datamgr_service/services/distributeddataservice/framework/directory/directory_manager.cpp new file mode 100644 index 00000000..a3b0458b --- /dev/null +++ b/datamgr_service/services/distributeddataservice/framework/directory/directory_manager.cpp @@ -0,0 +1,269 @@ +/* + * 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. + */ +#define LOG_TAG "DirectoryManager" +#include "directory/directory_manager.h" + +#include +#include +#include + +#include "accesstoken_kit.h" +#include "log_print.h" +#include "types.h" +#include "unistd.h" +namespace OHOS::DistributedData { +using OHOS::DistributedKv::SecurityLevel; +using namespace OHOS::Security::AccessToken; +DirectoryManager::DirectoryManager() + : actions_({ { "{security}", &DirectoryManager::GetSecurity }, { "{store}", &DirectoryManager::GetStore }, + { "{type}", &DirectoryManager::GetType }, { "{area}", &DirectoryManager::GetArea }, + { "{userId}", &DirectoryManager::GetUserId }, { "{bundleName}", &DirectoryManager::GetBundleName }, + { "{hapName}", &DirectoryManager::GetHapName } }) +{ +} + +DirectoryManager &DirectoryManager::GetInstance() +{ + static DirectoryManager instance; + return instance; +} + +std::string DirectoryManager::GetStorePath(const StoreMetaData &metaData, uint32_t version) +{ + return GenPath(metaData, version, ""); +} + +std::string DirectoryManager::GetStoreBackupPath(const StoreMetaData &metaData, uint32_t version) +{ + auto rootBackupPath = GenPath(metaData, version, "backup"); + return rootBackupPath + "/" + metaData.storeId; +} + +std::string DirectoryManager::GetSecretKeyPath(const StoreMetaData &metaData, uint32_t version) +{ + return GenPath(metaData, version, "secret"); +} + +std::string DirectoryManager::GetMetaStorePath(uint32_t version) +{ + int32_t index = GetVersionIndex(version); + if (index < 0) { + return ""; + } + + auto &strategy = strategies_[index]; + if (strategy.autoCreate) { + CreateDirectory(strategy.metaPath); + } + return strategy.metaPath; +} + +std::string DirectoryManager::GetMetaBackupPath(uint32_t version) +{ + int32_t index = GetVersionIndex(version); + if (index < 0) { + return ""; + } + + auto &strategy = strategies_[index]; + std::string path = strategy.metaPath + "/backup"; + if (strategy.autoCreate) { + CreateDirectory(path); + } + return path; +} + +void DirectoryManager::Initialize(const std::vector &strategies) +{ + strategies_.resize(strategies.size()); + for (size_t i = 0; i < strategies.size(); ++i) { + const Strategy &strategy = strategies[i]; + StrategyImpl &impl = strategies_[i]; + impl.autoCreate = strategy.autoCreate; + impl.version = strategy.version; + impl.metaPath = strategy.metaPath; + impl.path = Split(strategy.pattern, "/"); + impl.pipes.clear(); + for (auto &value : impl.path) { + auto it = actions_.find(value); + impl.pipes.push_back(it == actions_.end() ? nullptr : it->second); + } + } + + std::sort(strategies_.begin(), strategies_.end(), + [](const StrategyImpl &curr, const StrategyImpl &prev) { return curr.version > prev.version; }); +} + +std::string DirectoryManager::GetType(const StoreMetaData &metaData) const +{ + auto type = AccessTokenKit::GetTokenTypeFlag(metaData.tokenId); + if (type == TOKEN_NATIVE || type == TOKEN_SHELL) { + return "service"; + } + return "app"; +} + +std::string DirectoryManager::GetStore(const StoreMetaData &metaData) const +{ + if (metaData.storeType >= StoreMetaData::StoreType::STORE_KV_BEGIN + && metaData.storeType <= StoreMetaData::StoreType::STORE_KV_END) { + return "kvdb"; + } + // rdb use empty session + if (metaData.storeType >= StoreMetaData::StoreType::STORE_RELATIONAL_BEGIN + && metaData.storeType <= StoreMetaData::StoreType::STORE_RELATIONAL_END) { + return "rdb"; + } + // object use meta + if (metaData.storeType >= StoreMetaData::StoreType::STORE_OBJECT_BEGIN + && metaData.storeType <= StoreMetaData::StoreType::STORE_OBJECT_END) { + return "kvdb"; + } + return "other"; +} + +std::string DirectoryManager::GetSecurity(const StoreMetaData &metaData) const +{ + switch (metaData.securityLevel) { + case SecurityLevel::NO_LABEL: + if ((metaData.bundleName != metaData.appId) || (metaData.appType != "harmony")) { + break; + } + [[fallthrough]]; + case SecurityLevel::S0: [[fallthrough]]; + case SecurityLevel::S1: + return "misc_de"; + } + return "misc_ce"; +} + +std::string DirectoryManager::GetArea(const StoreMetaData &metaData) const +{ + return std::string("el") + std::to_string(metaData.area); +} + +std::string DirectoryManager::GetUserId(const StoreMetaData &metaData) const +{ + auto type = AccessTokenKit::GetTokenTypeFlag(metaData.tokenId); + if (type == TOKEN_NATIVE || type == TOKEN_SHELL) { + return "public"; + } + return metaData.user; +} + +std::string DirectoryManager::GetBundleName(const StoreMetaData &metaData) const +{ + if (metaData.instanceId == 0) { + return metaData.bundleName; + } + return metaData.bundleName + "_" + std::to_string(metaData.instanceId); +} + +std::string DirectoryManager::GetHapName(const StoreMetaData &metaData) const +{ + return metaData.hapName; +} + +std::vector DirectoryManager::Split(const std::string &source, const std::string &pattern) const +{ + std::vector values; + std::string::size_type pos = 0; + std::string::size_type nextPos = 0; + while (nextPos != std::string::npos) { + nextPos = source.find(pattern, pos); + if (nextPos == pos) { + pos = pos + pattern.size(); + continue; + } + values.push_back(source.substr(pos, nextPos - pos)); + pos = nextPos + pattern.size(); + } + return values; +} + +int32_t DirectoryManager::GetVersionIndex(uint32_t version) const +{ + for (size_t i = 0; i < strategies_.size(); ++i) { + if (version >= strategies_[i].version) { + return i; + } + } + return int32_t(strategies_.size()) - 1; +} + +std::vector DirectoryManager::GetVersions() +{ + std::vector versions; + for (size_t i = 0; i < strategies_.size(); ++i) { + versions.push_back(strategies_[i].version); + } + return versions; +} + +std::string DirectoryManager::GenPath(const StoreMetaData &metaData, uint32_t version, const std::string &exPath) const +{ + int32_t index = GetVersionIndex(version); + if (index < 0) { + return ""; + } + std::string path; + auto &strategy = strategies_[index]; + for (size_t i = 0; i < strategy.pipes.size(); ++i) { + std::string section; + if (strategy.pipes[i] == nullptr) { + section = strategy.path[i]; + } else { + section = (this->*(strategy.pipes[i]))(metaData); + } + if (section.empty()) { + continue; + } + path += "/" + section; + } + if (!exPath.empty()) { + path += "/" + exPath; + } + if (strategy.autoCreate) { + CreateDirectory(path); + } + return path; +} + +bool DirectoryManager::CreateDirectory(const std::string &path) const +{ + if (access(path.c_str(), F_OK) == 0) { + return true; + } + + std::string::size_type index = 0; + do { + std::string subPath; + index = path.find('/', index + 1); + if (index == std::string::npos) { + subPath = path; + } else { + subPath = path.substr(0, index); + } + + if (access(subPath.c_str(), F_OK) != 0) { + if (mkdir(subPath.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) != 0) { + return false; + } + } + } while (index != std::string::npos); + + return access(path.c_str(), F_OK) == 0; +} +} // namespace OHOS::DistributedData diff --git a/datamgr_service/services/distributeddataservice/framework/feature/feature_system.cpp b/datamgr_service/services/distributeddataservice/framework/feature/feature_system.cpp index 49b69e8c..74907ec8 100644 --- a/datamgr_service/services/distributeddataservice/framework/feature/feature_system.cpp +++ b/datamgr_service/services/distributeddataservice/framework/feature/feature_system.cpp @@ -60,11 +60,6 @@ int32_t FeatureSystem::Feature::OnInitialize() 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 E_OK; @@ -106,5 +101,10 @@ int32_t FeatureSystem::Feature::OnReady(const std::string &device) { return E_OK; } + +int32_t FeatureSystem::Feature::OnBind(const FeatureSystem::Feature::BindInfo &bindInfo) +{ + return E_OK; +} } // namespace DistributedData } // namespace OHOS \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/change_event.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/change_event.h new file mode 100644 index 00000000..b0b4e87a --- /dev/null +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/change_event.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 OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CHANGE_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CHANGE_EVENT_H +#include "cloud/sync_event.h" +#include "store/general_value.h" +namespace OHOS::DistributedData { +class API_EXPORT ChangeEvent : public SyncEvent { +public: + ChangeEvent(StoreInfo storeInfo, EventInfo info) + : SyncEvent(LOCAL_CHANGE, std::move(storeInfo), std::move(info)) + { + }; + ~ChangeEvent() override = default; +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CHANGE_EVENT_H diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h index e1f04f41..ae67857f 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -26,6 +26,8 @@ public: FEATURE_INIT = EVT_CLOUD, GET_SCHEMA, DATA_CHANGE, + LOCAL_CHANGE, + CLOUD_SYNC, CLOUD_BUTT }; @@ -37,13 +39,11 @@ public: int32_t user = 0; }; - CloudEvent(int32_t evtId, StoreInfo storeInfo, const std::string &featureName = "relational_store"); + CloudEvent(int32_t evtId, StoreInfo storeInfo); ~CloudEvent() = default; - std::string GetFeatureName() const; const StoreInfo& GetStoreInfo() const; private: - std::string featureName_; StoreInfo storeInfo_; }; } // namespace OHOS::DistributedData diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h index dc732027..e27bae75 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -35,14 +35,16 @@ public: uint64_t totalSpace = 0; uint64_t remainSpace = 0; bool enableCloud = false; - std::vector apps; + std::map apps; std::string GetKey() const; std::map GetSchemaKey() const; - std::string GetSchemaKey(const std::string &bundleName, const int32_t instanceId = 0) const; + std::string GetSchemaKey(const std::string &bundleName, int32_t instanceId = 0) const; + std::string GetSchemaPrefix(const std::string &bundleName) const; static std::string GetSchemaKey(const StoreMetaData &meta); bool IsValid() const; - bool IsExist(const std::string &bundleName) const; + bool Exist(const std::string &bundleName, int32_t instanceId = 0); + bool IsOn(const std::string &bundleName, int32_t instanceId = 0); static std::string GetPrefix(const std::initializer_list &field); bool Marshal(json &node) const override; diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/schema_meta.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/schema_meta.h index 188a0132..f02e34ac 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/schema_meta.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/schema_meta.h @@ -17,44 +17,50 @@ #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SCHEMA_META_H #include "serializable/serializable.h" namespace OHOS::DistributedData { +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; +}; + class API_EXPORT SchemaMeta final : public Serializable { public: + using Database = Database; + using Table = Table; + using Field = Field; 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::string bundleName; std::vector databases; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; + bool IsValid() const; Database GetDataBase(const std::string &storeId); }; } // namespace OHOS::DistributedData diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/sync_event.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/sync_event.h new file mode 100644 index 00000000..b5b81202 --- /dev/null +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/sync_event.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_CLOUD_SYNC_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SYNC_EVENT_H +#include "cloud/cloud_event.h" +#include "store/general_value.h" +#include "visibility.h" +namespace OHOS::DistributedData { +class API_EXPORT SyncEvent : public CloudEvent { +public: + class EventInfo { + public: + API_EXPORT EventInfo(int32_t mode, int32_t wait, std::shared_ptr query, GenAsync async); + API_EXPORT EventInfo(EventInfo &&info) noexcept; + EventInfo(const EventInfo &info) = default; + API_EXPORT EventInfo &operator=(EventInfo &&info) noexcept; + EventInfo &operator=(const EventInfo &info) = default; + private: + friend SyncEvent; + int32_t mode_ = -1; + int32_t wait_ = 0; + std::shared_ptr query_; + GenAsync asyncDetail_; + }; + SyncEvent(StoreInfo storeInfo, EventInfo info); + ~SyncEvent() override = default; + int32_t GetMode() const; + int32_t GetWait() const; + std::shared_ptr GetQuery() const; + GenAsync GetAsyncDetail() const; + +protected: + SyncEvent(int32_t evtId, StoreInfo storeInfo, EventInfo info); + +private: + EventInfo info_; +}; +} + +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SYNC_EVENT_H diff --git a/datamgr_service/services/distributeddataservice/framework/include/directory/directory_manager.h b/datamgr_service/services/distributeddataservice/framework/include/directory/directory_manager.h new file mode 100644 index 00000000..ccf9ebd2 --- /dev/null +++ b/datamgr_service/services/distributeddataservice/framework/include/directory/directory_manager.h @@ -0,0 +1,69 @@ +/* + * 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. + */ + +#ifndef DISTRIBUTEDDATAMGR_DIRECTORY_MANAGER_H +#define DISTRIBUTEDDATAMGR_DIRECTORY_MANAGER_H + +#include +#include + +#include "metadata/store_meta_data.h" +#include "visibility.h" +namespace OHOS::DistributedData { +class DirectoryManager { +public: + static constexpr uint32_t INVALID_VERSION = 0xFFFFFFFF; + struct Strategy { + bool autoCreate = false; + uint32_t version = 0; + std::string pattern; + std::string metaPath; + }; + API_EXPORT static DirectoryManager &GetInstance(); + API_EXPORT std::string GetStorePath(const StoreMetaData &metaData, uint32_t version = INVALID_VERSION); + API_EXPORT std::string GetSecretKeyPath(const StoreMetaData &metaData, uint32_t version = INVALID_VERSION); + API_EXPORT std::string GetStoreBackupPath(const StoreMetaData &metaData, uint32_t version = INVALID_VERSION); + API_EXPORT std::string GetMetaStorePath(uint32_t version = INVALID_VERSION); + API_EXPORT std::string GetMetaBackupPath(uint32_t version = INVALID_VERSION); + API_EXPORT std::vector GetVersions(); + API_EXPORT void Initialize(const std::vector &strategies); + +private: + using Action = std::string (DirectoryManager::*)(const StoreMetaData &) const; + struct StrategyImpl { + bool autoCreate = false; + uint32_t version; + std::string metaPath; + std::vector path; + std::vector pipes; + }; + + DirectoryManager(); + std::string GetType(const StoreMetaData &metaData) const; + std::string GetStore(const StoreMetaData &metaData) const; + std::string GetSecurity(const StoreMetaData &metaData) const; + std::string GetArea(const StoreMetaData &metaData) const; + std::string GetUserId(const StoreMetaData &metaData) const; + std::string GetBundleName(const StoreMetaData &metaData) const; + std::string GetHapName(const StoreMetaData &metaData) const; + std::vector Split(const std::string &source, const std::string &pattern) const; + int32_t GetVersionIndex(uint32_t version) const; + std::string GenPath(const StoreMetaData &metaData, uint32_t version, const std::string &exPath = "") const; + bool CreateDirectory(const std::string &path) const; + const std::map actions_; + std::vector strategies_; +}; +} // namespace OHOS::DistributedData +#endif // DISTRIBUTEDDATAMGR_DIRECTORY_MANAGER_H diff --git a/datamgr_service/services/distributeddataservice/framework/include/error/general_error.h b/datamgr_service/services/distributeddataservice/framework/include/error/general_error.h index 8c60a916..27ec5595 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/error/general_error.h +++ b/datamgr_service/services/distributeddataservice/framework/include/error/general_error.h @@ -25,6 +25,8 @@ enum GeneralError : int32_t { E_NOT_SUPPORT, E_ALREADY_CONSUMED, E_ALREADY_CLOSED, + E_UNOPENED, + E_RETRY_TIMEOUT, E_BUTT, }; } diff --git a/datamgr_service/services/distributeddataservice/framework/include/feature/feature_system.h b/datamgr_service/services/distributeddataservice/framework/include/feature/feature_system.h index 6bb34530..d549cc6d 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/feature/feature_system.h +++ b/datamgr_service/services/distributeddataservice/framework/include/feature/feature_system.h @@ -36,10 +36,15 @@ public: }; class API_EXPORT Feature { public: + struct BindInfo { + std::string selfName; + uint32_t selfTokenId; + std::shared_ptr executors; + }; 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 OnBind(const BindInfo &bindInfo); 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 OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); diff --git a/datamgr_service/services/distributeddataservice/framework/include/store/auto_cache.h b/datamgr_service/services/distributeddataservice/framework/include/store/auto_cache.h index ca1a5e80..b5e91e68 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/store/auto_cache.h +++ b/datamgr_service/services/distributeddataservice/framework/include/store/auto_cache.h @@ -64,8 +64,7 @@ private: 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; + int32_t OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) override; private: mutable Time time_; diff --git a/datamgr_service/services/distributeddataservice/framework/include/store/general_store.h b/datamgr_service/services/distributeddataservice/framework/include/store/general_store.h index e31eb889..46b748d9 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/store/general_store.h +++ b/datamgr_service/services/distributeddataservice/framework/include/store/general_store.h @@ -23,16 +23,40 @@ #include "store/general_watcher.h" namespace OHOS::DistributedData { class CloudDB; -class SchemaMeta; +class AssetLoader; +class Database; class GeneralStore { public: using Watcher = GeneralWatcher; - using Async = std::function>)>; + using DetailAsync = GenAsync; using Devices = std::vector; + enum SyncMode { + NEARBY_BEGIN, + NEARBY_PUSH = NEARBY_BEGIN, + NEARBY_PULL, + NEARBY_PULL_PUSH, + NEARBY_END, + CLOUD_BEGIN = NEARBY_END, + CLOUD_TIME_FIRST = CLOUD_BEGIN, + CLOUD_NATIVE_FIRST, + CLOUD_ClOUD_FIRST, + CLOUD_END, + MODE_BUTT = CLOUD_END, + }; + struct BindInfo { + BindInfo(std::shared_ptr db = nullptr, std::shared_ptr loader = nullptr) + : db_(std::move(db)), loader_(std::move(loader)) + { + } + std::shared_ptr db_; + std::shared_ptr loader_; + }; virtual ~GeneralStore() = default; - virtual int32_t Bind(const SchemaMeta &schemaMeta, std::shared_ptr cloudDb) = 0; + virtual int32_t Bind(const Database &database, BindInfo bindInfo) = 0; + + virtual bool IsBound() = 0; virtual int32_t Execute(const std::string &table, const std::string &sql) = 0; @@ -46,7 +70,7 @@ public: virtual std::shared_ptr Query(const std::string &table, GenQuery &query) = 0; - virtual int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) = 0; + virtual int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, DetailAsync async, int32_t wait) = 0; virtual int32_t Watch(int32_t origin, Watcher &watcher) = 0; diff --git a/datamgr_service/services/distributeddataservice/framework/include/store/general_value.h b/datamgr_service/services/distributeddataservice/framework/include/store/general_value.h index 05d4766d..18df10a1 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/store/general_value.h +++ b/datamgr_service/services/distributeddataservice/framework/include/store/general_value.h @@ -15,6 +15,7 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_VALUE_H #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_VALUE_H +#include #include #include #include @@ -24,6 +25,30 @@ #include "error/general_error.h" #include "traits.h" namespace OHOS::DistributedData { +enum GenProgress { + SYNC_BEGIN, + SYNC_IN_PROGRESS, + SYNC_FINISH, +}; + +struct GenStatistic { + int32_t total; + int32_t success; + int32_t failed; + int32_t untreated; +}; + +struct GenTableDetail { + GenStatistic upload; + GenStatistic download; +}; + +struct GenProgressDetail { + int32_t progress; + int32_t code; + std::map details; +}; + struct Asset { uint32_t version; std::string name; @@ -37,6 +62,7 @@ struct Asset { struct GenQuery { virtual ~GenQuery() = default; virtual bool IsEqual(uint64_t tid) = 0; + virtual std::vector GetTables() = 0; template int32_t QueryInterface(T *&query) @@ -55,7 +81,8 @@ using Value = std::variant; using VBucket = std::map; using VBuckets = std::vector; - +using GenDetails = std::map; +using GenAsync = std::function; template inline constexpr size_t TYPE_INDEX = Traits::variant_index_of_v; diff --git a/datamgr_service/services/distributeddataservice/framework/include/store/general_watcher.h b/datamgr_service/services/distributeddataservice/framework/include/store/general_watcher.h index 52130eab..4583aa9d 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/store/general_watcher.h +++ b/datamgr_service/services/distributeddataservice/framework/include/store/general_watcher.h @@ -21,24 +21,33 @@ namespace OHOS::DistributedData { class GeneralWatcher { public: - enum Origin : int32_t { - ORIGIN_CLOUD, - ORIGIN_LOCAL, - ORIGIN_REMOTE, - ORIGIN_ALL, - ORIGIN_BUTT, + struct Origin { + enum OriginType : int32_t { + ORIGIN_LOCAL, + ORIGIN_NEARBY, + ORIGIN_CLOUD, + ORIGIN_ALL, + ORIGIN_BUTT, + }; + int32_t origin = ORIGIN_BUTT; + // origin is ORIGIN_LOCAL, the id is empty + // origin is ORIGIN_NEARBY, the id is networkId; + // origin is ORIGIN_CLOUD, the id is the cloud account id + std::vector id; + std::string store; }; - enum ChangeOp : int32_t { OP_INSERT, OP_UPDATE, OP_DELETE, OP_BUTT, }; - + // PK primary key + using PRIValue = std::variant; + using PRIFields = std::map; + using ChangeInfo = std::map[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; + virtual int32_t OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) = 0; }; } #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_WATCHER_H diff --git a/datamgr_service/services/distributeddataservice/framework/include/utils/constant.h b/datamgr_service/services/distributeddataservice/framework/include/utils/constant.h index 7abc9282..203ac6e7 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/utils/constant.h +++ b/datamgr_service/services/distributeddataservice/framework/include/utils/constant.h @@ -16,12 +16,13 @@ #ifndef KV_DATASERVICE_CONSTANT_H #define KV_DATASERVICE_CONSTANT_H -#include -#include #include #include #include #include +#include +#include +#include #include #include "visibility.h" @@ -41,6 +42,15 @@ public: API_EXPORT static bool NotEqual(bool first, bool second); + template + inline static constexpr bool is_pod = (std::is_standard_layout_v && std::is_trivial_v); + + template + inline static std::enable_if_t && is_pod, bool> Copy(T *tag, const S *src) + { + return DCopy(reinterpret_cast(tag), sizeof(T), reinterpret_cast(src), sizeof(S)); + }; + // delete left bland in s by reference. template static void LeftTrim(T &s); @@ -53,19 +63,14 @@ public: template static void Trim(T &s); - // delete left bland in s by reference, not change raw string. - template - static T LeftTrimCopy(T s); - - // delete right bland in s by reference, not change raw string. - template - static T RightTrimCopy(T s); - // delete both left and right bland in s by reference, not change raw string. template static T TrimCopy(T s); API_EXPORT static constexpr const char *KEY_SEPARATOR = "###"; + +private: + static bool DCopy(uint8_t *tag, size_t tagLen, const uint8_t *src, size_t srcLen); }; // trim from start (in place) @@ -90,22 +95,6 @@ void Constant::Trim(T &s) RightTrim(s); } -// trim from start (copying) -template -T Constant::LeftTrimCopy(T s) -{ - LeftTrim(s); - return s; -} - -// trim from end (copying) -template -T Constant::RightTrimCopy(T s) -{ - RightTrim(s); - return s; -} - // trim from both ends (copying) template T Constant::TrimCopy(T s) diff --git a/datamgr_service/services/distributeddataservice/service/kvdb/ref_count.h b/datamgr_service/services/distributeddataservice/framework/include/utils/ref_count.h similarity index 97% rename from datamgr_service/services/distributeddataservice/service/kvdb/ref_count.h rename to datamgr_service/services/distributeddataservice/framework/include/utils/ref_count.h index 89ee9dd6..f5522d85 100644 --- a/datamgr_service/services/distributeddataservice/service/kvdb/ref_count.h +++ b/datamgr_service/services/distributeddataservice/framework/include/utils/ref_count.h @@ -18,7 +18,7 @@ #include #include #include -namespace OHOS::DistributedKv { +namespace OHOS::DistributedData { class RefCount final { public: RefCount(); diff --git a/datamgr_service/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp b/datamgr_service/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp index 1725d66c..4b1cdf06 100644 --- a/datamgr_service/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp +++ b/datamgr_service/services/distributeddataservice/framework/metadata/store_meta_data_local.cpp @@ -40,7 +40,7 @@ bool PolicyValue::IsValueEffect() const bool StoreMetaDataLocal::HasPolicy(uint32_t type) { - for (auto &policy : policies) { + for (const auto &policy : policies) { if (policy.type == type) { return true; } @@ -50,7 +50,7 @@ bool StoreMetaDataLocal::HasPolicy(uint32_t type) PolicyValue StoreMetaDataLocal::GetPolicy(uint32_t type) { - for (auto &policy : policies) { + for (const auto &policy : policies) { if (policy.type == type) { return policy; } diff --git a/datamgr_service/services/distributeddataservice/framework/store/auto_cache.cpp b/datamgr_service/services/distributeddataservice/framework/store/auto_cache.cpp index db51ad5c..ec9dea51 100644 --- a/datamgr_service/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/datamgr_service/services/distributeddataservice/framework/store/auto_cache.cpp @@ -14,6 +14,7 @@ */ #define LOG_TAG "AutoCache" #include "store/auto_cache.h" +#include "utils/anonymous.h" #include "log_print.h" namespace OHOS::DistributedData { @@ -115,7 +116,8 @@ void AutoCache::CloseExcept(const std::set &users) 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()); + ZLOGD("tokenId:0x%{public}x storeId:%{public}s observers:%{public}zu", key, Anonymous::Change(storeId).c_str(), + watchers.size()); auto it = stores.find(storeId); if (it != stores.end()) { it->second.SetObservers(watchers); @@ -145,14 +147,14 @@ AutoCache::Delegate::Delegate(GeneralStore *delegate, const Watchers &watchers, { time_ = std::chrono::steady_clock::now() + std::chrono::minutes(INTERVAL); if (store_ != nullptr) { - store_->Watch(ORIGIN_ALL, *this); + store_->Watch(Origin::ORIGIN_ALL, *this); } } AutoCache::Delegate::~Delegate() { if (store_ != nullptr) { - store_->Unwatch(ORIGIN_ALL, *this); + store_->Unwatch(Origin::ORIGIN_ALL, *this); store_->Close(); store_ = nullptr; } @@ -173,14 +175,13 @@ 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_->Unwatch(Origin::ORIGIN_ALL, *this); + auto status = store_->Close(); + if (status == Error::E_BUSY) { + return false; + } + store_ = nullptr; } - store_ = nullptr; return true; } @@ -195,34 +196,20 @@ 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) +int32_t AutoCache::Delegate::OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) { Watchers watchers; { std::unique_lock lock(mutex_); watchers = watchers_; } - for (auto watcher : watchers) { + size_t remain = watchers.size(); + for (auto &watcher : watchers) { + remain--; if (watcher == nullptr) { continue; } - watcher->OnChange(origin, id, values); + watcher->OnChange(origin, primaryFields, (remain != 0) ? ChangeInfo(values) : std::move(values)); } return Error::E_OK; } diff --git a/datamgr_service/services/distributeddataservice/framework/utils/constant.cpp b/datamgr_service/services/distributeddataservice/framework/utils/constant.cpp index 1f327be9..d05d8e9f 100644 --- a/datamgr_service/services/distributeddataservice/framework/utils/constant.cpp +++ b/datamgr_service/services/distributeddataservice/framework/utils/constant.cpp @@ -20,6 +20,7 @@ #include #include #include "log_print.h" +#include "securec.h" namespace OHOS { namespace DistributedData { @@ -85,5 +86,14 @@ bool Constant::IsBackground(pid_t pid) } return false; } + +bool Constant::DCopy(uint8_t *tag, size_t tagLen, const uint8_t *src, size_t srcLen) +{ + if (tagLen != srcLen || tag == nullptr || src == nullptr) { + return false; + } + auto ret = memcpy_s(tag, tagLen, src, srcLen); + return ret == EOK; +} } // namespace DistributedData } // namespace OHOS diff --git a/datamgr_service/services/distributeddataservice/service/kvdb/ref_count.cpp b/datamgr_service/services/distributeddataservice/framework/utils/ref_count.cpp similarity index 95% rename from datamgr_service/services/distributeddataservice/service/kvdb/ref_count.cpp rename to datamgr_service/services/distributeddataservice/framework/utils/ref_count.cpp index fb36dcdb..977c817e 100644 --- a/datamgr_service/services/distributeddataservice/service/kvdb/ref_count.cpp +++ b/datamgr_service/services/distributeddataservice/framework/utils/ref_count.cpp @@ -13,8 +13,8 @@ * limitations under the License. */ -#include "ref_count.h" -namespace OHOS::DistributedKv { +#include "utils/ref_count.h" +namespace OHOS::DistributedData { RefCount::RefCount() { } diff --git a/datamgr_service/services/distributeddataservice/service/BUILD.gn b/datamgr_service/services/distributeddataservice/service/BUILD.gn index e27030a9..f11f1e5e 100644 --- a/datamgr_service/services/distributeddataservice/service/BUILD.gn +++ b/datamgr_service/services/distributeddataservice/service/BUILD.gn @@ -100,8 +100,10 @@ ohos_shared_library("distributeddatasvc") { "object/object_service_impl.cpp", "object/object_service_stub.cpp", "permission/src/permit_delegate.cpp", + "rdb/rdb_cursor.cpp", "rdb/rdb_general_store.cpp", "rdb/rdb_notifier_proxy.cpp", + "rdb/rdb_query.cpp", "rdb/rdb_result_set_impl.cpp", "rdb/rdb_result_set_stub.cpp", "rdb/rdb_service_impl.cpp", @@ -109,6 +111,7 @@ ohos_shared_library("distributeddatasvc") { "rdb/rdb_store_observer_impl.cpp", "rdb/rdb_syncer.cpp", "rdb/rdb_watcher.cpp", + "rdb/value_proxy.cpp", ] cflags = [ "-Wno-multichar" ] diff --git a/datamgr_service/services/distributeddataservice/service/backup/src/backup_manager.cpp b/datamgr_service/services/distributeddataservice/service/backup/src/backup_manager.cpp index 5c52cbd7..0c05825c 100644 --- a/datamgr_service/services/distributeddataservice/service/backup/src/backup_manager.cpp +++ b/datamgr_service/services/distributeddataservice/service/backup/src/backup_manager.cpp @@ -18,10 +18,11 @@ #include #include #include + #include "backuprule/backup_rule_manager.h" -#include "device_manager_adapter.h" #include "crypto_manager.h" -#include "directory_manager.h" +#include "device_manager_adapter.h" +#include "directory/directory_manager.h" #include "log_print.h" #include "metadata/meta_data_manager.h" #include "types.h" @@ -289,7 +290,7 @@ void BackupManager::CopyFile(const std::string &oldPath, const std::string &newP fout.open(newPath, std::ios_base::out | std::ios_base::trunc); } char buf[COPY_SIZE] = {0}; - while (!fin.eof()) { + while (fin.good()) { fin.read(buf, COPY_SIZE); fout.write(buf, fin.gcount()); } diff --git a/datamgr_service/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp b/datamgr_service/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp index 6cdd57ab..8f958cf3 100644 --- a/datamgr_service/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp +++ b/datamgr_service/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp @@ -16,11 +16,12 @@ #include "bootstrap.h" #include -#include "backuprule/backup_rule_manager.h" + #include "backup_manager.h" +#include "backuprule/backup_rule_manager.h" #include "checker/checker_manager.h" #include "config_factory.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "log_print.h" namespace OHOS { namespace DistributedData { diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index cd3c8d16..54956ad2 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -19,16 +19,12 @@ #include "account/account_delegate.h" #include "checker/checker_manager.h" -#include "cloud/cloud_event.h" #include "cloud/cloud_server.h" -#include "cloud/subscription.h" #include "communicator/device_manager_adapter.h" #include "eventcenter/event_center.h" -#include "feature/feature_system.h" #include "ipc_skeleton.h" #include "log_print.h" #include "metadata/meta_data_manager.h" -#include "metadata/store_meta_data.h" #include "store/auto_cache.h" #include "utils/anonymous.h" namespace OHOS::CloudData { @@ -36,6 +32,12 @@ using namespace DistributedData; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; using Account = OHOS::DistributedKv::AccountDelegate; __attribute__((used)) CloudServiceImpl::Factory CloudServiceImpl::factory_; +const CloudServiceImpl::Work CloudServiceImpl::HANDLERS[WORK_BUTT] = { + &CloudServiceImpl::DoSubscribe, + &CloudServiceImpl::UpdateCloudInfo, + &CloudServiceImpl::UpdateSchema, +}; + CloudServiceImpl::Factory::Factory() noexcept { FeatureSystem::GetInstance().RegisterCreator( @@ -55,7 +57,6 @@ CloudServiceImpl::CloudServiceImpl() { EventCenter::GetInstance().Subscribe(CloudEvent::GET_SCHEMA, [this](const Event &event) { GetSchema(event); - return; }); } @@ -67,20 +68,17 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map bool { - return appInfo.bundleName == item.first; - }); - if (it == cloudInfo.apps.end()) { + for (const auto &[bundle, value] : switches) { + if (!cloudInfo.Exist(bundle)) { continue; } - it->cloudSwitch = item.second; + cloudInfo.apps[bundle].cloudSwitch = value; } if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { return ERROR; } Execute(GetCloudTask(0, cloudInfo.user)); + syncManager_.DoCloudSync({ cloudInfo.user }); return SUCCESS; } @@ -96,6 +94,7 @@ int32_t CloudServiceImpl::DisableCloud(const std::string &id) return ERROR; } Execute(GetCloudTask(0, cloudInfo.user)); + syncManager_.StopCloudSync(cloudInfo.user); return SUCCESS; } @@ -106,19 +105,18 @@ int32_t CloudServiceImpl::ChangeAppSwitch(const std::string &id, const std::stri if (status != SUCCESS) { return status; } - auto it = std::find_if(cloudInfo.apps.begin(), cloudInfo.apps.end(), - [&bundleName](const CloudInfo::AppInfo &appInfo) -> bool { - return appInfo.bundleName == bundleName; - }); - if (it == cloudInfo.apps.end()) { + if (!cloudInfo.Exist(bundleName)) { ZLOGE("bundleName:%{public}s", bundleName.c_str()); return INVALID_ARGUMENT; } - (*it).cloudSwitch = appSwitch; + cloudInfo.apps[bundleName].cloudSwitch = (appSwitch == SWITCH_ON); if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { return ERROR; } Execute(GetCloudTask(0, cloudInfo.user)); + if (cloudInfo.enableCloud && appSwitch == SWITCH_ON) { + syncManager_.DoCloudSync({ cloudInfo.user, bundleName }); + } return SUCCESS; } @@ -130,13 +128,14 @@ int32_t CloudServiceImpl::Clean(const std::string &id, const std::map bool { - return appInfo.bundleName == bundleName; - }); - if (it == cloudInfo.apps.end()) { + if (!cloudInfo.Exist(bundleName)) { ZLOGE("bundleName:%{public}s", bundleName.c_str()); return INVALID_ARGUMENT; } - if (!it->cloudSwitch) { + if (!cloudInfo.apps[bundleName].cloudSwitch) { return CLOUD_DISABLE_SWITCH; } - auto key = cloudInfo.GetSchemaKey(bundleName); SchemaMeta schemaMeta; if (!MetaDataManager::GetInstance().LoadMeta(key, schemaMeta, true)) { ZLOGE("bundleName:%{public}s", bundleName.c_str()); return INVALID_ARGUMENT; } + const auto &app = cloudInfo.apps[bundleName]; for (const auto &database : schemaMeta.databases) { EventCenter::Defer defer; CloudEvent::StoreInfo storeInfo; - storeInfo.bundleName = it->bundleName; - storeInfo.instanceId = it->instanceId; + storeInfo.bundleName = app.bundleName; + storeInfo.instanceId = app.instanceId; storeInfo.user = cloudInfo.user; storeInfo.storeName = database.name; auto evt = std::make_unique(CloudEvent::DATA_CHANGE, storeInfo); EventCenter::GetInstance().PostEvent(std::move(evt)); } + syncManager_.DoCloudSync(SyncManager::SyncInfo(cloudInfo.user, bundleName)); return SUCCESS; } int32_t CloudServiceImpl::OnInitialize() { - FeatureInit(); - Execute(GetCloudTask(0, 0)); + Execute(GetCloudTask(0, 0, { WORK_CLOUD_INFO_UPDATE, WORK_SCHEMA_UPDATE })); return E_OK; } -int32_t CloudServiceImpl::OnExecutor(std::shared_ptr executor) +int32_t CloudServiceImpl::OnBind(const BindInfo &info) { - if (executor_ != nullptr || executor == nullptr) { + if (executor_ != nullptr || info.executors == nullptr) { return E_INVALID_ARGS; } - executor_ = std::move(executor); + executor_ = std::move(info.executors); + syncManager_.Bind(executor_); return E_OK; } int32_t CloudServiceImpl::OnUserChange(uint32_t code, const std::string &user, const std::string &account) { - Execute(GetCloudTask(0, atoi(user.c_str()))); + int32_t userId = atoi(user.c_str()); + Execute(GetCloudTask(0, userId, { WORK_CLOUD_INFO_UPDATE, WORK_SCHEMA_UPDATE })); + syncManager_.StopCloudSync(userId); return E_OK; } @@ -244,54 +242,96 @@ int32_t CloudServiceImpl::GetCloudInfoFromServer(CloudInfo &cloudInfo) { auto instance = CloudServer::GetInstance(); if (instance == nullptr) { + ZLOGW("cloud server unavailable"); return NOT_SUPPORT; } cloudInfo = instance->GetServerInfo(cloudInfo.user); + if (!cloudInfo.IsValid()) { + ZLOGE("cloud is empty, user%{public}d", cloudInfo.user); + return ERROR; + } return SUCCESS; } -void CloudServiceImpl::UpdateCloudInfo(CloudInfo &cloudInfo) +bool CloudServiceImpl::UpdateCloudInfo(int32_t user) { + CloudInfo cloudInfo; + cloudInfo.user = user; + if (GetCloudInfoFromServer(cloudInfo) != SUCCESS) { + ZLOGE("failed, user:%{public}d", user); + return false; + } CloudInfo oldInfo; if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), oldInfo, true)) { MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); - return; + return true; + } + if (oldInfo.id != cloudInfo.id) { + ZLOGE("different id, [server] id:%{public}s, [meta] id:%{public}s", + Anonymous::Change(cloudInfo.id).c_str(), Anonymous::Change(oldInfo.id).c_str()); + return false; } oldInfo.totalSpace = cloudInfo.totalSpace; oldInfo.remainSpace = cloudInfo.remainSpace; oldInfo.apps = std::move(cloudInfo.apps); - cloudInfo = oldInfo; + for (auto &[bundle, app] : cloudInfo.apps) { + if (oldInfo.Exist(bundle)) { + continue; + } + oldInfo.apps[bundle] = std::move(app); + } MetaDataManager::GetInstance().SaveMeta(oldInfo.GetKey(), oldInfo, true); + return true; } -void CloudServiceImpl::AddSchema(CloudInfo &cloudInfo) +bool CloudServiceImpl::UpdateSchema(int32_t user) { + CloudInfo cloudInfo; + cloudInfo.user = user; + if (GetCloudInfoFromServer(cloudInfo) != SUCCESS) { + ZLOGE("failed, user:%{public}d", user); + return false; + } auto keys = cloudInfo.GetSchemaKey(); - for (const auto &key : keys) { + for (const auto &[bundle, key] : keys) { SchemaMeta schemaMeta; - if (MetaDataManager::GetInstance().LoadMeta(key.second, schemaMeta, true)) { + if (MetaDataManager::GetInstance().LoadMeta(key, schemaMeta, true)) { continue; } - if (GetAppSchema(cloudInfo.user, key.first, schemaMeta) != SUCCESS) { - continue; + if (GetAppSchema(cloudInfo.user, bundle, schemaMeta) != SUCCESS) { + return false; } - MetaDataManager::GetInstance().SaveMeta(key.second, schemaMeta, true); + MetaDataManager::GetInstance().SaveMeta(key, schemaMeta, true); } + return true; } int32_t CloudServiceImpl::GetAppSchema(int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta) { auto instance = CloudServer::GetInstance(); if (instance == nullptr) { + ZLOGW("cloud server unavailable"); return SERVER_UNAVAILABLE; } schemaMeta = instance->GetAppSchema(user, bundleName); return SUCCESS; } -ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry, int32_t user) +CloudServiceImpl::Tasks CloudServiceImpl::GetCloudTask( + int32_t retry, int32_t user, const std::initializer_list &works) +{ + Tasks tasks; + tasks.reserve(works.size() + 1); + tasks.push_back(GenTask(retry, user, WORK_SUB)); + for (auto work : works) { + tasks.push_back(GenTask(retry, user, work)); + } + return tasks; +} + +ExecutorPool::Task CloudServiceImpl::GenTask(int32_t retry, int32_t user, AsyncWork work) { - return [this, retry, user]() -> void { + return [this, retry, user, work]() -> void { auto executor = executor_; if (retry >= RETRY_TIMES || executor == nullptr) { return; @@ -306,132 +346,92 @@ ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry, int32_t user) } for (auto user : users) { - Subscription subscription; - subscription.userId = user; - MetaDataManager::GetInstance().LoadMeta(subscription.GetKey(), subscription, true); - finished = DoSubscribe(subscription) && finished; + finished = (this->*HANDLERS[work])(user) && finished; } if (!finished) { - executor->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetCloudTask(retry + 1, user)); + executor->Schedule(std::chrono::seconds(RETRY_INTERVAL), GenTask(retry + 1, user, work)); } }; } -SchemaMeta CloudServiceImpl::GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId) +std::vector CloudServiceImpl::GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId) { - SchemaMeta schemaMeta; + CloudInfo cloudInfo = GetCloudInfo(userId); + if (!cloudInfo.IsValid()) { + // GetCloudInfo has print the log info. so we don`t need print again. + return {}; + } + + if (!bundleName.empty() && !cloudInfo.Exist(bundleName, instanceId)) { + ZLOGE("bundleName:%{public}s instanceId:%{public}d", bundleName.c_str(), instanceId); + return {}; + } + auto instance = CloudServer::GetInstance(); if (instance == nullptr) { ZLOGE("instance is nullptr"); - return schemaMeta; + return {}; } - CloudInfo cloudInfo; - cloudInfo.user = userId; - if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { - cloudInfo = instance->GetServerInfo(userId); - if (!cloudInfo.IsValid()) { - ZLOGE("cloudInfo is invalid"); - return schemaMeta; + + std::vector schemas; + for (auto &[name, app]: cloudInfo.apps) { + if (!bundleName.empty() && name == bundleName) { + continue; + } + std::string schemaKey = cloudInfo.GetSchemaKey(name, instanceId); + SchemaMeta schemaMeta; + if (MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true)) { + schemas.push_back(std::move(schemaMeta)); + continue; + } + + schemaMeta = instance->GetAppSchema(userId, name); + if (!schemaMeta.IsValid()) { + ZLOGE("download schema from cloud failed, user:%{public}s, bundleName:%{public}s", userId, name.c_str()); + continue; } - MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); - } - if (std::find_if(cloudInfo.apps.begin(), cloudInfo.apps.end(), - [&bundleName, &instanceId](const CloudInfo::AppInfo &appInfo) { - return appInfo.bundleName == bundleName && appInfo.instanceId == instanceId; - }) == cloudInfo.apps.end()) { - ZLOGE("bundleName:%{public}s instanceId:%{public}d", bundleName.c_str(), instanceId); - return schemaMeta; - } - std::string schemaKey = cloudInfo.GetSchemaKey(bundleName, instanceId); - if (!MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true)) { - schemaMeta = instance->GetAppSchema(cloudInfo.user, bundleName); MetaDataManager::GetInstance().SaveMeta(schemaKey, schemaMeta, true); + schemas.push_back(std::move(schemaMeta)); } - return schemaMeta; -} - -StoreMetaData CloudServiceImpl::GetStoreMata(int32_t userId, const std::string &bundleName, - const std::string &storeName, int32_t instanceId) -{ - StoreMetaData storeMetaData; - storeMetaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; - storeMetaData.user = std::to_string(userId); - storeMetaData.bundleName = bundleName; - storeMetaData.storeId = storeName; - storeMetaData.instanceId = instanceId; - MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData); - return storeMetaData; + return schemas; } -void CloudServiceImpl::FeatureInit() -{ +CloudInfo CloudServiceImpl::GetCloudInfo(int32_t userId) { CloudInfo cloudInfo; - std::vector users; - if (!DistributedKv::AccountDelegate::GetInstance()->QueryUsers(users) || users.empty()) { - return; + cloudInfo.user = userId; + if (MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { + return cloudInfo; } - for (const auto &user : users) { - if (user == USER_ID) { - continue; - } - cloudInfo.user = user; - if (GetCloudInfoFromServer(cloudInfo) != SUCCESS) { - ZLOGE("failed, user:%{public}d", user); - continue; - } - UpdateCloudInfo(cloudInfo); - AddSchema(cloudInfo); + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + ZLOGD("no cloud server"); + return cloudInfo; + } + + cloudInfo = instance->GetServerInfo(userId); + if (!cloudInfo.IsValid()) { + ZLOGE("no cloud info %{public}d", userId); + return cloudInfo; } + + MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); + return cloudInfo; } void CloudServiceImpl::GetSchema(const Event &event) { auto &rdbEvent = static_cast(event); + auto &storeInfo = rdbEvent.GetStoreInfo(); ZLOGD("Start GetSchema, bundleName:%{public}s, storeName:%{public}s, instanceId:%{public}d", - rdbEvent.GetStoreInfo().bundleName.c_str(), rdbEvent.GetStoreInfo().storeName.c_str(), - rdbEvent.GetStoreInfo().instanceId); - auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); - auto schemaMeta = GetSchemaMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().instanceId); - if (schemaMeta.databases.empty()) { - return; - } - auto storeMeta = GetStoreMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, - rdbEvent.GetStoreInfo().instanceId); - auto instance = CloudServer::GetInstance(); - if (instance == nullptr) { - ZLOGE("instance is nullptr"); - return; - } - auto database = - std::find_if(schemaMeta.databases.begin(), schemaMeta.databases.end(), [&rdbEvent](const auto &database) { - return database.name == rdbEvent.GetStoreInfo().storeName; - }); - if (database == schemaMeta.databases.end()) { - return; - } - - ZLOGD("database: %{public}s sync start", database->name.c_str()); - auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, *database); - if (cloudDB == nullptr) { - ZLOGE("cloudDB is nullptr"); - return; - } - AutoCache::Watchers watchers; - auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers); - if (store == nullptr) { - ZLOGE("store is nullptr"); - return; - } - store->Bind(schemaMeta, cloudDB); - for (const auto &table : database->tables) { - ZLOGD("table: %{public}s sync start", table.name.c_str()); - // do sync - } - return; + storeInfo.bundleName.c_str(), storeInfo.storeName.c_str(), storeInfo.instanceId); + GetSchemaMata(storeInfo.user, storeInfo.bundleName, storeInfo.instanceId); } -bool CloudServiceImpl::DoSubscribe(const Subscription &sub) +bool CloudServiceImpl::DoSubscribe(int32_t user) { + Subscription sub; + sub.userId = user; + MetaDataManager::GetInstance().LoadMeta(sub.GetKey(), sub, true); if (CloudServer::GetInstance() == nullptr) { ZLOGI("not support cloud server"); return true; @@ -451,10 +451,10 @@ bool CloudServiceImpl::DoSubscribe(const Subscription &sub) auto offThreshold = std::chrono::system_clock::now().time_since_epoch(); std::map> subDbs; std::map> unsubDbs; - for (auto &app : cloudInfo.apps) { + for (auto &[bundle, app] : cloudInfo.apps) { auto enabled = cloudInfo.enableCloud && app.cloudSwitch; auto &dbs = enabled ? subDbs : unsubDbs; - auto it = sub.expiresTime.find(app.bundleName); + auto it = sub.expiresTime.find(bundle); // cloud is enabled, but the subscription won't expire if (enabled && (it != sub.expiresTime.end() && it->second >= onThreshold.count())) { continue; @@ -465,9 +465,10 @@ bool CloudServiceImpl::DoSubscribe(const Subscription &sub) } SchemaMeta schemaMeta; - exits = MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(app.bundleName), schemaMeta, true); + exits = MetaDataManager::GetInstance().LoadMeta( + cloudInfo.GetSchemaKey(bundle), schemaMeta, true); if (exits) { - dbs[app.bundleName] = std::move(schemaMeta.databases); + dbs[bundle] = std::move(schemaMeta.databases); } } @@ -480,12 +481,14 @@ bool CloudServiceImpl::DoSubscribe(const Subscription &sub) return subDbs.empty() && unsubDbs.empty(); } -void CloudServiceImpl::Execute(ExecutorPool::Task task) +void CloudServiceImpl::Execute(Tasks tasks) { - auto executor = executor_; - if (executor == nullptr) { - return; + for (auto &task : tasks) { + auto executor = executor_; + if (executor == nullptr) { + return; + } + executor->Execute(std::move(task)); } - executor->Execute(std::move(task)); } } // namespace OHOS::CloudData \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h index 35121c57..451c7923 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -17,12 +17,12 @@ #define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SERVICE_IMPL_H #include -#include "cloud_service_stub.h" +#include "cloud/cloud_event.h" #include "cloud/cloud_info.h" #include "cloud/schema_meta.h" -#include "cloud/cloud_event.h" #include "cloud/subscription.h" - +#include "cloud_service_stub.h" +#include "sync_manager.h" namespace OHOS::CloudData { class CloudServiceImpl : public CloudServiceStub { public: @@ -35,11 +35,10 @@ public: int32_t Clean(const std::string &id, const std::map &actions) override; int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) override; int32_t OnInitialize() override; - int32_t OnExecutor(std::shared_ptr executor) override; + int32_t OnBind(const BindInfo &info) override; int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; private: - static const inline int USER_ID = 0; class Factory { public: Factory() noexcept; @@ -53,26 +52,36 @@ private: using SchemaMeta = DistributedData::SchemaMeta; using Event = DistributedData::Event; using Subscription = DistributedData::Subscription; + using Work = bool (CloudServiceImpl::*)(int32_t); + using Tasks = std::vector; + + enum AsyncWork : int32_t { + WORK_SUB = 0, + WORK_CLOUD_INFO_UPDATE, + WORK_SCHEMA_UPDATE, + WORK_BUTT, + }; static constexpr int32_t RETRY_TIMES = 10; static constexpr int32_t RETRY_INTERVAL = 30; static constexpr int32_t EXPIRE_INTERVAL = 7 * 24; // 7 day - void UpdateCloudInfo(CloudInfo &cloudInfo); - void AddSchema(CloudInfo &cloudInfo); - SchemaMeta GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId); - StoreMetaData GetStoreMata(int32_t userId, const std::string &bundleName, const std::string &storeName, - int32_t instanceId); + bool UpdateCloudInfo(int32_t user); + bool UpdateSchema(int32_t user); + std::vector GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId); + CloudInfo GetCloudInfo(int32_t userId); int32_t GetCloudInfo(uint32_t tokenId, const std::string &id, CloudInfo &cloudInfo); int32_t GetCloudInfoFromMeta(CloudInfo &cloudInfo); int32_t GetCloudInfoFromServer(CloudInfo &cloudInfo); int32_t GetAppSchema(int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta); - void FeatureInit(); void GetSchema(const Event &event); - ExecutorPool::Task GetCloudTask(int32_t retry, int32_t user); - void Execute(ExecutorPool::Task task); - bool DoSubscribe(const Subscription &sub); + Tasks GetCloudTask(int32_t retry, int32_t user, const std::initializer_list &works = {}); + ExecutorPool::Task GenTask(int32_t retry, int32_t user, AsyncWork work); + void Execute(Tasks tasks); + bool DoSubscribe(int32_t user); std::shared_ptr executor_; + SyncManager syncManager_; + static const Work HANDLERS[WORK_BUTT]; }; } // namespace OHOS::DistributedData diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.cpp index b14485f4..644d0750 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.cpp @@ -20,6 +20,7 @@ #include "log_print.h" #include "utils/anonymous.h" #include "tokenid_kit.h" +#include "permission_validator.h" namespace OHOS::CloudData { using namespace DistributedData; using namespace OHOS::Security::AccessToken; @@ -52,6 +53,12 @@ int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; } + if (!DistributedKv::PermissionValidator::GetInstance().IsCloudConfigPermit(IPCSkeleton::GetCallingTokenID())) { + ZLOGE("permission denied! code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); + auto result = static_cast(CLOUD_CONFIG_PERMISSION_DENIED); + return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; + } + std::string id; if (!ITypesUtil::Unmarshal(data, id)) { ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str()); diff --git a/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.cpp b/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.cpp new file mode 100644 index 00000000..84aa248b --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -0,0 +1,302 @@ +/* + * 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 "SyncManager" +#include "sync_manager.h" + +#include "cloud/cloud_info.h" +#include "cloud/schema_meta.h" +#include "cloud/sync_event.h" +#include "device_manager_adapter.h" +#include "eventcenter/event_center.h" +#include "log_print.h" +#include "metadata/meta_data_manager.h" +#include "store/auto_cache.h" +#include "cloud/cloud_server.h" +namespace OHOS::CloudData { +using namespace DistributedData; +using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; +SyncManager::SyncInfo::SyncInfo(int32_t user, const std::string &bundleName, const Store &store, const Tables &tables) + : user_(user), bundleName_(bundleName) +{ + if (!store.empty()) { + tables_[store] = tables; + } +} + +SyncManager::SyncInfo::SyncInfo(int32_t user, const std::string &bundleName, const Stores &stores) + : user_(user), bundleName_(bundleName) +{ + for (auto &store : stores) { + tables_[store] = {}; + } +} + +SyncManager::SyncInfo::SyncInfo(int32_t user, const std::string &bundleName, const MutliStoreTables &tables) + : user_(user), bundleName_(bundleName) +{ + tables_ = tables; +} + +void SyncManager::SyncInfo::SetMode(int32_t mode) +{ + mode_ = mode; +} + +void SyncManager::SyncInfo::SetWait(int32_t wait) +{ + wait_ = wait; +} + +void SyncManager::SyncInfo::SetAsyncDetail(GenAsync asyncDetail) +{ + async_ = std::move(asyncDetail); +} + +void SyncManager::SyncInfo::SetQuery(std::shared_ptr query) +{ + query_ = query; +} + +void SyncManager::SyncInfo::SetError(int32_t code) +{ + if (async_) { + GenDetails details; + auto &detail = details[id_]; + detail.progress = SYNC_FINISH; + detail.code = code; + async_(std::move(details)); + } +} + +std::shared_ptr SyncManager::SyncInfo::GenerateQuery(const std::string &store) +{ + if (query_ != nullptr) { + return query_; + } + class SyncQuery final : public GenQuery { + public: + SyncQuery(const std::vector &tables) : tables_(tables){}; + bool IsEqual(uint64_t tid) override + { + return false; + } + + std::vector GetTables() override + { + return tables_; + } + + private: + std::vector tables_; + }; + return std::make_shared(tables_[store]); +} + +SyncManager::SyncManager() +{ + EventCenter::GetInstance().Subscribe(CloudEvent::LOCAL_CHANGE, GetClientChangeHandler()); +} + +SyncManager::~SyncManager() +{ + if (executor_ != nullptr) { + actives_.ForEachCopies([this](auto &syncId, auto &taskId) { + executor_->Remove(taskId); + return false; + }); + executor_ = nullptr; + } +} + +int32_t SyncManager::Bind(std::shared_ptr executor) +{ + executor_ = executor; + return E_OK; +} + +int32_t SyncManager::DoCloudSync(SyncInfo syncInfo) +{ + if (executor_ == nullptr) { + return E_NOT_INIT; + } + + actives_.Compute(GenSyncId(syncInfo.user_), [this, &syncInfo](const uint64_t &key, TaskId &taskId) mutable { + taskId = executor_->Execute(GetSyncTask(0, GenSyncRef(key), std::move(syncInfo))); + return true; + }); + + return E_OK; +} + +int32_t SyncManager::StopCloudSync(int32_t user) +{ + if (executor_ == nullptr) { + return E_NOT_INIT; + } + actives_.ForEachCopies([this, user](auto &syncId, auto &taskId) { + if (Compare(syncId, user) == 0) { + executor_->Remove(taskId); + } + return false; + }); + return E_OK; +} + +ExecutorPool::Task SyncManager::GetSyncTask(int32_t retry, RefCount ref, SyncInfo &&syncInfo) +{ + retry++; + return [this, retry, ref = std::move(ref), info = std::move(syncInfo)]() mutable { + EventCenter::Defer defer(GetSyncHandler(), CloudEvent::CLOUD_SYNC); + CloudInfo cloud; + cloud.user = info.user_; + if (!MetaDataManager::GetInstance().LoadMeta(cloud.GetKey(), cloud, true)) { + info.SetError(E_NOT_INIT); + ZLOGE("no cloud info for user:%{public}d", info.user_); + return; + } + + if (!cloud.enableCloud || cloud.id != info.id_ || (!info.bundleName_.empty() && !cloud.IsOn(info.bundleName_))) { + info.SetError(E_UNOPENED); + ZLOGI("cloud off bundleName:%{public}s", info.bundleName_.c_str()); + return; + } + + std::vector schemas; + auto key = cloud.GetSchemaPrefix(info.bundleName_); + if (!MetaDataManager::GetInstance().LoadMeta(key, schemas, true)) { + DoRetry(retry, std::move(info)); + return; + } + + for (auto &schema : schemas) { + if (!cloud.IsOn(schema.bundleName)) { + continue; + } + + for (const auto &database : schema.databases) { + CloudEvent::StoreInfo storeInfo; + storeInfo.bundleName = schema.bundleName; + storeInfo.user = cloud.user; + storeInfo.storeName = database.name; + auto evt = std::make_unique(std::move(storeInfo), + SyncEvent::EventInfo{ info.mode_, info.wait_, info.GenerateQuery(database.name), info.async_ }); + EventCenter::GetInstance().PostEvent(std::move(evt)); + } + } + }; +} + +std::function SyncManager::GetSyncHandler() +{ + return [](const Event &event) { + auto &evt = static_cast(event); + auto &storeInfo = evt.GetStoreInfo(); + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + ZLOGD("not support cloud sync"); + return; + } + + StoreMetaData meta; + meta.storeId = storeInfo.storeName; + meta.bundleName = storeInfo.bundleName; + meta.user = std::to_string(storeInfo.user); + meta.instanceId = storeInfo.instanceId; + meta.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta)) { + ZLOGE("failed, no store meta bundleName:%{public}s, storeId:%{public}s", meta.bundleName.c_str(), + meta.storeId.c_str()); + return; + } + auto store = AutoCache::GetInstance().GetStore(meta, {}); + if (store == nullptr) { + ZLOGE("store null, storeId:%{public}s", meta.storeId.c_str()); + return; + } + + if (!store->IsBound()) { + CloudInfo info; + info.user = storeInfo.user; + SchemaMeta schemaMeta; + std::string schemaKey = info.GetSchemaKey(storeInfo.bundleName, storeInfo.instanceId); + if (!MetaDataManager::GetInstance().LoadMeta(std::move(schemaKey), schemaMeta, true)) { + ZLOGE("failed, no schema bundleName:%{public}s, storeId:%{public}s", storeInfo.bundleName.c_str(), + storeInfo.storeName.c_str()); + return; + } + auto dbMeta = schemaMeta.GetDataBase(storeInfo.storeName); + auto cloudDB = instance->ConnectCloudDB(storeInfo.tokenId, dbMeta); + if (cloudDB == nullptr) { + ZLOGE("failed, no cloud DB <0x%{public}x %{public}s<->%{public}s>", storeInfo.tokenId, + dbMeta.name.c_str(), dbMeta.alias.c_str()); + return; + } + store->Bind(dbMeta, std::move(cloudDB)); + } + ZLOGD("database:<%{public}d:%{public}s:%{public}s> sync start", storeInfo.user, storeInfo.bundleName.c_str(), + storeInfo.storeName.c_str()); + store->Sync({}, evt.GetMode(), *(evt.GetQuery()), evt.GetAsyncDetail(), evt.GetWait()); + }; +} + +std::function SyncManager::GetClientChangeHandler() +{ + return [this](const Event &event) { + auto &evt = static_cast(event); + auto store = evt.GetStoreInfo(); + SyncInfo syncInfo(store.user, store.bundleName, store.storeName); + syncInfo.SetMode(evt.GetMode()); + syncInfo.SetWait(evt.GetWait()); + syncInfo.SetAsyncDetail(evt.GetAsyncDetail()); + auto task = GetSyncTask(RETRY_TIMES, RefCount(), std::move(syncInfo)); + task(); + }; +} + +uint64_t SyncManager::GenSyncId(int32_t user) +{ + uint64_t syncId = user; + return (syncId << 32) | (++syncId_); +} + +RefCount SyncManager::GenSyncRef(uint64_t syncId) +{ + return RefCount([syncId, this]() { + actives_.Erase(syncId); + }); +} + +int32_t SyncManager::Compare(uint64_t syncId, int32_t user) +{ + uint64_t inner = user; + return (syncId & USER_MARK) == (inner << 32); +} + +void SyncManager::DoRetry(int32_t retry, SyncInfo &&info) +{ + CloudEvent::StoreInfo storeInfo; + storeInfo.user = info.user_; + storeInfo.bundleName = info.bundleName_; + EventCenter::GetInstance().PostEvent(std::make_unique(CloudEvent::GET_SCHEMA, storeInfo)); + if (retry > RETRY_TIMES) { + info.SetError(E_RETRY_TIMEOUT); + return; + } + actives_.Compute(GenSyncId(info.user_), [this, retry, &info](const uint64_t &key, TaskId &value) mutable { + value = executor_->Schedule(RETRY_INTERVAL, GetSyncTask(retry, GenSyncRef(key), std::move(info))); + return true; + }); +} +} // namespace OHOS::CloudData \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.h b/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.h new file mode 100644 index 00000000..fe28a15a --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/cloud/sync_manager.h @@ -0,0 +1,85 @@ +/* + * 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_CLOUD_SYNC_MANAGER_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_SYNC_MANAGER_H +#include "eventcenter/event.h" +#include "executor_pool.h" +#include "store/general_store.h" +#include "store/general_value.h" +#include "utils/ref_count.h" +#include "concurrent_map.h" +namespace OHOS::CloudData { +class SyncManager { +public: + using GenAsync = DistributedData::GenAsync; + using GenStore = DistributedData::GeneralStore; + using GenQuery = DistributedData::GenQuery; + using RefCount = DistributedData::RefCount; + class SyncInfo final { + public: + using Store = std::string; + using Stores = std::vector; + using Tables = std::vector; + using MutliStoreTables = std::map; + SyncInfo(int32_t user, const std::string &bundleName = "", const Store &store = "", const Tables &tables = {}); + SyncInfo(int32_t user, const std::string &bundleName, const Stores &stores); + SyncInfo(int32_t user, const std::string &bundleName, const MutliStoreTables &tables); + void SetMode(int32_t mode); + void SetWait(int32_t wait); + void SetAsyncDetail(GenAsync asyncDetail); + void SetQuery(std::shared_ptr query); + void SetError(int32_t code); + std::shared_ptr GenerateQuery(const std::string &store); + + private: + friend SyncManager; + int32_t mode_ = GenStore::CLOUD_TIME_FIRST; + int32_t user_ = 0; + int32_t wait_ = 0; + std::string id_ = "default"; + std::string bundleName_; + std::map> tables_; + GenAsync async_; + std::shared_ptr query_; + }; + SyncManager(); + ~SyncManager(); + int32_t Bind(std::shared_ptr executor); + int32_t DoCloudSync(SyncInfo syncInfo); + int32_t StopCloudSync(int32_t user = 0); + +private: + using Event = DistributedData::Event; + using Task = ExecutorPool::Task; + using TaskId = ExecutorPool::TaskId; + static constexpr ExecutorPool::Duration RETRY_INTERVAL = std::chrono::seconds(10); // second + static constexpr int32_t RETRY_TIMES = 6; // second + static constexpr uint64_t USER_MARK = 0xFFFFFFFF00000000; // high 32 bit + + Task GetSyncTask(int32_t retry, RefCount ref, SyncInfo &&syncInfo); + void DoRetry(int32_t retry, SyncInfo &&syncInfo); + std::function GetSyncHandler(); + std::function GetClientChangeHandler(); + uint64_t GenSyncId(int32_t user); + RefCount GenSyncRef(uint64_t syncId); + int32_t Compare(uint64_t syncId, int32_t user); + + std::atomic syncId_ = 0; + std::shared_ptr executor_; + ConcurrentMap actives_; +}; +} // namespace OHOS::CloudData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_SYNC_MANAGER_H diff --git a/datamgr_service/services/distributeddataservice/service/config/include/model/directory_config.h b/datamgr_service/services/distributeddataservice/service/config/include/model/directory_config.h index baa7b05c..03422499 100644 --- a/datamgr_service/services/distributeddataservice/service/config/include/model/directory_config.h +++ b/datamgr_service/services/distributeddataservice/service/config/include/model/directory_config.h @@ -15,7 +15,7 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_CONFIG_MODEL_DIRECTORY_CONFIG_H #define OHOS_DISTRIBUTED_DATA_SERVICES_CONFIG_MODEL_DIRECTORY_CONFIG_H -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "serializable/serializable.h" namespace OHOS { namespace DistributedData { diff --git a/datamgr_service/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 4fc2ca94..527c59e7 100644 --- a/datamgr_service/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -223,6 +223,12 @@ PermissionProxy::PermissionState DataShareServiceImpl::VerifyPermission(uint32_t return PermissionProxy::PermissionState::NOT_FIND; } +int32_t DataShareServiceImpl::OnBind(const BindInfo &binderInfo) +{ + binderInfo_ = binderInfo; + return EOK; +} + int32_t DataShareServiceImpl::GetUserId(uint32_t tokenId, bool isSingleApp) { if (isSingleApp) { diff --git a/datamgr_service/services/distributeddataservice/service/data_share/data_share_service_impl.h b/datamgr_service/services/distributeddataservice/service/data_share/data_share_service_impl.h index 7b9d9d35..a96617de 100644 --- a/datamgr_service/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -35,6 +35,8 @@ 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 OnBind(const BindInfo &binderInfo) override; + private: class Factory { public: @@ -56,6 +58,7 @@ private: static Factory factory_; static constexpr int32_t ERROR = -1; DataShareProfileInfo dataShareProfileInfo_; + BindInfo binderInfo_; }; } // namespace OHOS::DataShare #endif diff --git a/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_exporter.cpp b/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_exporter.cpp index 4b0c2e7f..22532d53 100644 --- a/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_exporter.cpp +++ b/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_exporter.cpp @@ -16,7 +16,7 @@ #include "kvdb_exporter.h" #include "backup_manager.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "log_print.h" #include "reporter.h" #include "store_cache.h" diff --git a/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 1d252ce8..98f1c5ef 100644 --- a/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -26,7 +26,7 @@ #include "communication_strategy.h" #include "crypto_manager.h" #include "device_manager_adapter.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "eventcenter/event_center.h" #include "ipc_skeleton.h" #include "log_print.h" @@ -99,7 +99,8 @@ KVDBServiceImpl::KVDBServiceImpl() if (policy.IsValueEffect()) { syncInfo.delay = policy.valueUint; } - ZLOGI("[online] appId:%{public}s, storeId:%{public}s", data.bundleName.c_str(), data.storeId.c_str()); + ZLOGI("[online] appId:%{public}s, storeId:%{public}s", data.bundleName.c_str(), + Anonymous::Change(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), @@ -154,8 +155,8 @@ Status KVDBServiceImpl::Delete(const AppId &appId, const StoreId &storeId) MetaDataManager::GetInstance().DelMeta(metaData.GetKeyLocal(), true); PermitDelegate::GetInstance().DelCache(metaData.GetKey()); storeCache_.CloseStore(tokenId, storeId); - ZLOGD("appId:%{public}s storeId:%{public}s instanceId:%{public}d", appId.appId.c_str(), storeId.storeId.c_str(), - metaData.instanceId); + ZLOGD("appId:%{public}s storeId:%{public}s instanceId:%{public}d", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str(), metaData.instanceId); return SUCCESS; } @@ -165,12 +166,12 @@ 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)) { ZLOGW("appId:%{public}s storeId:%{public}s no IMMEDIATE_SYNC_ON_CHANGE ", appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return Status::SUCCESS; } } @@ -306,8 +307,8 @@ Status KVDBServiceImpl::RmvSubscribeInfo(const AppId &appId, const StoreId &stor Status KVDBServiceImpl::Subscribe(const AppId &appId, const StoreId &storeId, sptr observer) { auto tokenId = IPCSkeleton::GetCallingTokenID(); - ZLOGI("appId:%{public}s storeId:%{public}s tokenId:0x%{public}x", appId.appId.c_str(), storeId.storeId.c_str(), - tokenId); + ZLOGI("appId:%{public}s storeId:%{public}s tokenId:0x%{public}x", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str(), tokenId); syncAgents_.Compute(tokenId, [&appId, &storeId, &observer](auto &key, SyncAgent &value) { if (value.pid_ != IPCSkeleton::GetCallingPid()) { value.ReInit(IPCSkeleton::GetCallingPid(), appId); @@ -327,8 +328,8 @@ Status KVDBServiceImpl::Subscribe(const AppId &appId, const StoreId &storeId, sp Status KVDBServiceImpl::Unsubscribe(const AppId &appId, const StoreId &storeId, sptr observer) { auto tokenId = IPCSkeleton::GetCallingTokenID(); - ZLOGI("appId:%{public}s storeId:%{public}s tokenId:0x%{public}x", appId.appId.c_str(), storeId.storeId.c_str(), - tokenId); + ZLOGI("appId:%{public}s storeId:%{public}s tokenId:0x%{public}x", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str(), tokenId); syncAgents_.ComputeIfPresent(tokenId, [&appId, &storeId, &observer](auto &key, SyncAgent &value) { if (value.pid_ != IPCSkeleton::GetCallingPid()) { ZLOGW("agent already changed! old pid:%{public}d new pid:%{public}d appId:%{public}s", @@ -352,7 +353,8 @@ Status KVDBServiceImpl::GetBackupPassword(const AppId &appId, const StoreId &sto 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()); + ZLOGD("appId:%{public}s storeId:%{public}s to export data", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); StoreMetaData meta = GetStoreMetaData(appId, storeId); AddOptions(options, meta); @@ -365,8 +367,8 @@ Status KVDBServiceImpl::BeforeCreate(const AppId &appId, const StoreId &storeId, old.area != meta.area || !options.persistent) { ZLOGE("meta appId:%{public}s storeId:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " "area:%{public}d->%{public}d persistent:%{public}d", - appId.appId.c_str(), storeId.storeId.c_str(), old.storeType, meta.storeType, old.isEncrypt, meta.isEncrypt, - old.area, meta.area, options.persistent); + appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str(), old.storeType, meta.storeType, + old.isEncrypt, meta.isEncrypt, old.area, meta.area, options.persistent); return Status::STORE_META_CHANGED; } @@ -382,7 +384,7 @@ Status KVDBServiceImpl::AfterCreate(const AppId &appId, const StoreId &storeId, { if (!appId.IsValid() || !storeId.IsValid() || !options.IsValidType()) { ZLOGE("failed please check type:%{public}d appId:%{public}s storeId:%{public}s", options.kvStoreType, - appId.appId.c_str(), storeId.storeId.c_str()); + appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str()); return INVALID_ARGUMENT; } @@ -396,8 +398,8 @@ Status KVDBServiceImpl::AfterCreate(const AppId &appId, const StoreId &storeId, auto dbStatus = Upgrade::GetInstance().UpdateStore(oldMeta, metaData, password); ZLOGI("update status:%{public}d appId:%{public}s storeId:%{public}s inst:%{public}d " "type:%{public}d->%{public}d dir:%{public}s", - dbStatus, appId.appId.c_str(), storeId.storeId.c_str(), metaData.instanceId, oldMeta.storeType, - metaData.storeType, metaData.dataDir.c_str()); + dbStatus, appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str(), metaData.instanceId, + oldMeta.storeType, metaData.storeType, metaData.dataDir.c_str()); if (dbStatus != DBStatus::OK) { status = STORE_UPGRADE_FAILED; } @@ -413,7 +415,7 @@ Status KVDBServiceImpl::AfterCreate(const AppId &appId, const StoreId &storeId, SaveLocalMetaData(options, metaData); Upgrade::GetInstance().UpdatePassword(metaData, password); ZLOGI("appId:%{public}s storeId:%{public}s instanceId:%{public}d type:%{public}d dir:%{public}s", - appId.appId.c_str(), storeId.storeId.c_str(), metaData.instanceId, metaData.storeType, + appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str(), metaData.instanceId, metaData.storeType, metaData.dataDir.c_str()); return status; } @@ -442,7 +444,7 @@ int32_t KVDBServiceImpl::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const int32_t KVDBServiceImpl::ResolveAutoLaunch(const std::string &identifier, DBLaunchParam ¶m) { ZLOGI("user:%{public}s appId:%{public}s storeId:%{public}s identifier:%{public}s", param.userId.c_str(), - param.appId.c_str(), param.storeId.c_str(), Anonymous::Change(identifier).c_str()); + param.appId.c_str(), Anonymous::Change(param.storeId).c_str(), Anonymous::Change(identifier).c_str()); std::vector metaData; auto prefix = StoreMetaData::GetPrefix({ DMAdapter::GetInstance().GetLocalDevice().uuid, param.userId }); if (!MetaDataManager::GetInstance().LoadMeta(prefix, metaData)) { @@ -462,7 +464,8 @@ int32_t KVDBServiceImpl::ResolveAutoLaunch(const std::string &identifier, DBLaun auto observers = GetObservers(storeMeta.tokenId, storeMeta.storeId); ZLOGD("user:%{public}s appId:%{public}s storeId:%{public}s observers:%{public}zu", storeMeta.user.c_str(), - storeMeta.bundleName.c_str(), storeMeta.storeId.c_str(), (observers) ? observers->size() : size_t(0)); + storeMeta.bundleName.c_str(), Anonymous::Change(storeMeta.storeId).c_str(), + (observers) ? observers->size() : size_t(0)); DBStatus status; storeCache_.GetStore(storeMeta, observers, status); } @@ -492,17 +495,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), @@ -591,11 +596,11 @@ int32_t KVDBServiceImpl::GetInstIndex(uint32_t tokenId, const AppId &appId) Status KVDBServiceImpl::DoSync(const StoreMetaData &meta, const SyncInfo &info, const SyncEnd &complete, int32_t type) { ZLOGD("seqId:0x%{public}" PRIx64 " type:%{public}d remote:%{public}zu appId:%{public}s storeId:%{public}s", - info.seqId, type, info.devices.size(), meta.bundleName.c_str(), meta.storeId.c_str()); + info.seqId, type, info.devices.size(), meta.bundleName.c_str(), Anonymous::Change(meta.storeId).c_str()); auto uuids = ConvertDevices(info.devices); if (uuids.empty()) { ZLOGW("no device online seqId:0x%{public}" PRIx64 " remote:%{public}zu appId:%{public}s storeId:%{public}s", - info.seqId, info.devices.size(), meta.bundleName.c_str(), meta.storeId.c_str()); + info.seqId, info.devices.size(), meta.bundleName.c_str(), Anonymous::Change(meta.storeId).c_str()); return Status::ERROR; } @@ -604,7 +609,7 @@ Status KVDBServiceImpl::DoSync(const StoreMetaData &meta, const SyncInfo &info, auto store = storeCache_.GetStore(meta, observers, status); if (store == nullptr) { ZLOGE("failed! status:%{public}d appId:%{public}s storeId:%{public}s dir:%{public}s", status, - meta.bundleName.c_str(), meta.storeId.c_str(), meta.dataDir.c_str()); + meta.bundleName.c_str(), Anonymous::Change(meta.storeId).c_str(), meta.dataDir.c_str()); return ConvertDbStatus(status); } bool isSuccess = false; @@ -780,7 +785,7 @@ size_t KVDBServiceImpl::GetSyncDataSize(const std::string &deviceId) auto store = storeCache_.GetStore(data, observers, status); if (store == nullptr) { ZLOGE("failed! status:%{public}d appId:%{public}s storeId:%{public}s dir:%{public}s", status, - data.bundleName.c_str(), data.storeId.c_str(), data.dataDir.c_str()); + data.bundleName.c_str(), Anonymous::Change(data.storeId).c_str(), data.dataDir.c_str()); continue; } totalSize += store->GetSyncDataSize(deviceId); @@ -788,12 +793,11 @@ size_t KVDBServiceImpl::GetSyncDataSize(const std::string &deviceId) return totalSize; } -int32_t KVDBServiceImpl::OnExecutor(std::shared_ptr executors) +int32_t KVDBServiceImpl::OnBind(const BindInfo &bindInfo) { - executors_ = executors; - storeCache_.SetThreadPool(executors); - KvStoreSyncManager::GetInstance()->SetThreadPool(executors); - ZLOGE("onexecutor:%{public}p", executors.get()); + executors_ = bindInfo.executors; + storeCache_.SetThreadPool(bindInfo.executors); + KvStoreSyncManager::GetInstance()->SetThreadPool(bindInfo.executors); return 0; } } // namespace OHOS::DistributedKv \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.h b/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.h index 5267a0bd..4a0e40a7 100644 --- a/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.h @@ -26,12 +26,13 @@ #include "metadata/store_meta_data.h" #include "metadata/store_meta_data_local.h" #include "metadata/strategy_meta_data.h" -#include "ref_count.h" +#include "utils/ref_count.h" #include "store_cache.h" namespace OHOS::DistributedKv { class API_EXPORT KVDBServiceImpl final : public KVDBServiceStub { public: using DBLaunchParam = DistributedDB::AutoLaunchParam; + using RefCount = DistributedData::RefCount; API_EXPORT KVDBServiceImpl(); virtual ~KVDBServiceImpl(); Status GetStoreIds(const AppId &appId, std::vector &storeIds) override; @@ -53,7 +54,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 OnBind(const BindInfo &bindInfo) 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/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp b/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp index 61fdde59..8894c4ad 100644 --- a/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_stub.cpp @@ -20,6 +20,7 @@ #include "itypes_util.h" #include "log_print.h" #include "utils/constant.h" +#include "utils/anonymous.h" namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; const KVDBServiceStub::Handler KVDBServiceStub::HANDLERS[TRANS_BUTT] = { @@ -60,7 +61,8 @@ int KVDBServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Message AppId appId; StoreId storeId; if (!ITypesUtil::Unmarshal(data, appId, storeId)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_INVALID_DATA_ERR; } appId.appId = Constant::TrimCopy(appId.appId); @@ -75,11 +77,11 @@ int KVDBServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Message return (this->*HANDLERS[code])(appId, storeId, data, reply); } ZLOGE("PERMISSION_DENIED uid:%{public}d appId:%{public}s storeId:%{public}s", info.uid, info.bundleName.c_str(), - info.storeId.c_str()); + Anonymous::Change(info.storeId).c_str()); if (!ITypesUtil::Marshal(reply, static_cast(PERMISSION_DENIED))) { ZLOGE("Marshal PERMISSION_DENIED code:%{public}u appId:%{public}s storeId:%{public}s", code, - appId.appId.c_str(), storeId.storeId.c_str()); + appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -102,13 +104,14 @@ int32_t KVDBServiceStub::OnBeforeCreate( { Options options; if (!ITypesUtil::Unmarshal(data, options)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_INVALID_DATA_ERR; } int32_t status = BeforeCreate(appId, storeId, options); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -120,14 +123,15 @@ int32_t KVDBServiceStub::OnAfterCreate( Options options; std::vector password; if (!ITypesUtil::Unmarshal(data, options, password)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_INVALID_DATA_ERR; } int32_t status = AfterCreate(appId, storeId, options, password); password.assign(password.size(), 0); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -138,7 +142,7 @@ int32_t KVDBServiceStub::OnDelete(const AppId &appId, const StoreId &storeId, Me int32_t status = Delete(appId, storeId); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -148,13 +152,14 @@ int32_t KVDBServiceStub::OnSync(const AppId &appId, const StoreId &storeId, Mess { SyncInfo syncInfo; if (!ITypesUtil::Unmarshal(data, syncInfo.seqId, syncInfo.mode, syncInfo.devices, syncInfo.delay, syncInfo.query)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_INVALID_DATA_ERR; } int32_t status = Sync(appId, storeId, syncInfo); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -165,14 +170,15 @@ int32_t KVDBServiceStub::OnRegisterCallback( { sptr remoteObj; if (!ITypesUtil::Unmarshal(data, remoteObj)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_INVALID_DATA_ERR; } auto syncCallback = (remoteObj == nullptr) ? nullptr : iface_cast(remoteObj); int32_t status = RegisterSyncCallback(appId, syncCallback); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -184,7 +190,7 @@ int32_t KVDBServiceStub::OnUnregisterCallback( int32_t status = UnregisterSyncCallback(appId); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -195,13 +201,14 @@ int32_t KVDBServiceStub::OnSetSyncParam( { KvSyncParam syncParam; if (!ITypesUtil::Unmarshal(data, syncParam.allowedDelayMs)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_INVALID_DATA_ERR; } int32_t status = SetSyncParam(appId, storeId, syncParam); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -214,7 +221,7 @@ int32_t KVDBServiceStub::OnGetSyncParam( int32_t status = GetSyncParam(appId, storeId, syncParam); if (!ITypesUtil::Marshal(reply, status, syncParam.allowedDelayMs)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -226,7 +233,7 @@ int32_t KVDBServiceStub::OnEnableCap( int32_t status = EnableCapability(appId, storeId); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -238,7 +245,7 @@ int32_t KVDBServiceStub::OnDisableCap( int32_t status = DisableCapability(appId, storeId); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -250,13 +257,14 @@ int32_t KVDBServiceStub::OnSetCapability( std::vector local; std::vector remote; if (!ITypesUtil::Unmarshal(data, local, remote)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_INVALID_DATA_ERR; } int32_t status = SetCapability(appId, storeId, local, remote); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -267,13 +275,14 @@ int32_t KVDBServiceStub::OnAddSubInfo(const AppId &appId, const StoreId &storeId { SyncInfo syncInfo; if (!ITypesUtil::Unmarshal(data, syncInfo.seqId, syncInfo.devices, syncInfo.query)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_INVALID_DATA_ERR; } int32_t status = AddSubscribeInfo(appId, storeId, syncInfo); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -284,13 +293,14 @@ int32_t KVDBServiceStub::OnRmvSubInfo(const AppId &appId, const StoreId &storeId { SyncInfo syncInfo; if (!ITypesUtil::Unmarshal(data, syncInfo.seqId, syncInfo.devices, syncInfo.query)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_INVALID_DATA_ERR; } int32_t status = RmvSubscribeInfo(appId, storeId, syncInfo); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -301,14 +311,15 @@ int32_t KVDBServiceStub::OnSubscribe( { sptr remoteObj; if (!ITypesUtil::Unmarshal(data, remoteObj)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_INVALID_DATA_ERR; } auto observer = (remoteObj == nullptr) ? nullptr : iface_cast(remoteObj); int32_t status = Subscribe(appId, storeId, observer); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -319,14 +330,15 @@ int32_t KVDBServiceStub::OnUnsubscribe( { sptr remoteObj; if (!ITypesUtil::Unmarshal(data, remoteObj)) { - ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(), + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_INVALID_DATA_ERR; } auto observer = (remoteObj == nullptr) ? nullptr : iface_cast(remoteObj); int32_t status = Unsubscribe(appId, storeId, observer); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } return ERR_NONE; @@ -339,7 +351,7 @@ int32_t KVDBServiceStub::OnGetBackupPassword( int32_t status = GetBackupPassword(appId, storeId, password); if (!ITypesUtil::Marshal(reply, status, password)) { ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + Anonymous::Change(storeId.storeId).c_str()); password.assign(password.size(), 0); return IPC_STUB_WRITE_PARCEL_ERR; } diff --git a/datamgr_service/services/distributeddataservice/service/kvdb/store_cache.cpp b/datamgr_service/services/distributeddataservice/service/kvdb/store_cache.cpp index 093e8e5a..3f9059e8 100644 --- a/datamgr_service/services/distributeddataservice/service/kvdb/store_cache.cpp +++ b/datamgr_service/services/distributeddataservice/service/kvdb/store_cache.cpp @@ -14,14 +14,16 @@ */ #define LOG_TAG "StoreCache" #include "store_cache.h" + #include "account/account_delegate.h" #include "crypto_manager.h" #include "device_matrix.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "log_print.h" #include "metadata/meta_data_manager.h" #include "metadata/secret_key_meta_data.h" #include "types.h" +#include "utils/anonymous.h" namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; constexpr int64_t StoreCache::INTERVAL; @@ -104,7 +106,7 @@ void StoreCache::CloseExcept(const std::set &users) void StoreCache::SetObserver(uint32_t tokenId, const std::string &storeId, std::shared_ptr observers) { stores_.ComputeIfPresent(tokenId, [&storeId, &observers](auto &key, auto &stores) { - ZLOGD("tokenId:0x%{public}x storeId:%{public}s observers:%{public}zu", key, storeId.c_str(), + ZLOGD("tokenId:0x%{public}x storeId:%{public}s observers:%{public}zu", key, Anonymous::Change(storeId).c_str(), observers ? observers->size() : size_t(0)); auto it = stores.find(storeId); if (it != stores.end()) { @@ -197,9 +199,8 @@ void StoreCache::SetThreadPool(std::shared_ptr executors) } StoreCache::DBStoreDelegate::DBStoreDelegate(DBStore *delegate, std::shared_ptr observers) - : delegate_(delegate) + : time_(std::chrono::steady_clock::now() + std::chrono::minutes(INTERVAL)), delegate_(delegate) { - time_ = std::chrono::steady_clock::now() + std::chrono::minutes(INTERVAL); SetObservers(std::move(observers)); } @@ -234,13 +235,12 @@ bool StoreCache::DBStoreDelegate::Close(DBManager &manager) std::unique_lock lock(mutex_); if (delegate_ != nullptr) { delegate_->UnRegisterObserver(this); + auto status = manager.CloseKvStore(delegate_); + if (status == DBStatus::BUSY) { + return false; + } + delegate_ = nullptr; } - - auto status = manager.CloseKvStore(delegate_); - if (status == DBStatus::BUSY) { - return false; - } - delegate_ = nullptr; return true; } diff --git a/datamgr_service/services/distributeddataservice/service/kvdb/store_cache.h b/datamgr_service/services/distributeddataservice/service/kvdb/store_cache.h index 2e4d726c..37e5133d 100644 --- a/datamgr_service/services/distributeddataservice/service/kvdb/store_cache.h +++ b/datamgr_service/services/distributeddataservice/service/kvdb/store_cache.h @@ -33,9 +33,9 @@ public: template struct Less { public: - bool operator()(const sptr &__x, const sptr &__y) const + bool operator()(const sptr &x, const sptr &y) const { - return __x.GetRefPtr() < __y.GetRefPtr(); + return x.GetRefPtr() < y.GetRefPtr(); } }; using DBStatus = DistributedDB::DBStatus; diff --git a/datamgr_service/services/distributeddataservice/service/kvdb/upgrade.cpp b/datamgr_service/services/distributeddataservice/service/kvdb/upgrade.cpp index ea40a6bd..38a370e8 100644 --- a/datamgr_service/services/distributeddataservice/service/kvdb/upgrade.cpp +++ b/datamgr_service/services/distributeddataservice/service/kvdb/upgrade.cpp @@ -18,14 +18,14 @@ #include #include +#include "accesstoken_kit.h" #include "crypto_manager.h" -#include "metadata/secret_key_meta_data.h" #include "device_manager_adapter.h" +#include "directory/directory_manager.h" #include "log_print.h" #include "metadata/meta_data_manager.h" +#include "metadata/secret_key_meta_data.h" #include "store_cache.h" -#include "accesstoken_kit.h" -#include "directory_manager.h" namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; using system_clock = std::chrono::system_clock; diff --git a/datamgr_service/services/distributeddataservice/service/matrix/include/device_matrix.h b/datamgr_service/services/distributeddataservice/service/matrix/include/device_matrix.h index e660bc93..c04223aa 100644 --- a/datamgr_service/services/distributeddataservice/service/matrix/include/device_matrix.h +++ b/datamgr_service/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/datamgr_service/services/distributeddataservice/service/matrix/src/device_matrix.cpp b/datamgr_service/services/distributeddataservice/service/matrix/src/device_matrix.cpp index ab034dea..a1ee0235 100644 --- a/datamgr_service/services/distributeddataservice/service/matrix/src/device_matrix.cpp +++ b/datamgr_service/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_); @@ -199,7 +217,7 @@ uint16_t DeviceMatrix::ConvertMask(const std::string &device, uint16_t code) if (index >= meta.maskInfo.size()) { return result; } - auto &app = meta.maskInfo[index]; + const auto &app = meta.maskInfo[index]; for (size_t i = 0; i < maskApps_.size(); i++) { if (maskApps_[i] == app) { result |= SetMask(i); diff --git a/datamgr_service/services/distributeddataservice/service/object/object_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/object/object_service_impl.cpp index 7ae417c3..ccd1e57a 100644 --- a/datamgr_service/services/distributeddataservice/service/object/object_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/object/object_service_impl.cpp @@ -23,7 +23,7 @@ #include "bootstrap.h" #include "checker/checker_manager.h" #include "device_manager_adapter.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "log_print.h" #include "metadata/appid_meta_data.h" #include "metadata/meta_data_manager.h" @@ -277,9 +277,4 @@ 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/datamgr_service/services/distributeddataservice/service/object/object_service_impl.h b/datamgr_service/services/distributeddataservice/service/object/object_service_impl.h index 932ea29a..a7f40e51 100644 --- a/datamgr_service/services/distributeddataservice/service/object/object_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/object/object_service_impl.h @@ -42,7 +42,6 @@ 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; @@ -53,7 +52,6 @@ private: ~Factory(); }; static Factory factory_; - std::shared_ptr executors_; }; } // namespace OHOS::DistributedObject #endif diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.cpp new file mode 100644 index 00000000..e0ee3e9f --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.cpp @@ -0,0 +1,124 @@ +/* + * 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 "RdbCloud" +#include "rdb_cloud.h" +#include "log_print.h" +#include "value_proxy.h" + +namespace OHOS::DistributedRdb { +using namespace DistributedDB; +using namespace DistributedData; +RdbCloud::RdbCloud(std::shared_ptr cloudDB) + : cloudDB_(std::move(cloudDB)) +{ +} + +DBStatus RdbCloud::BatchInsert( + const std::string &tableName, std::vector &&record, std::vector &extend) +{ + DistributedData::VBuckets extends; + auto error = cloudDB_->BatchInsert(tableName, ValueProxy::Convert(std::move(record)), extends); + if (error == GeneralError::E_OK) { + extend = ValueProxy::Convert(std::move(extends)); + } + return ConvertStatus(static_cast(error)); +} + +DBStatus RdbCloud::BatchUpdate( + const std::string &tableName, std::vector &&record, std::vector &extend) +{ + auto error = cloudDB_->BatchUpdate( + tableName, ValueProxy::Convert(std::move(record)), ValueProxy::Convert(std::move(extend))); + return ConvertStatus(static_cast(error)); +} + +DBStatus RdbCloud::BatchDelete(const std::string &tableName, std::vector &extend) +{ + auto error = cloudDB_->BatchDelete(tableName, ValueProxy::Convert(std::move(extend))); + return ConvertStatus(static_cast(error)); +} + +DBStatus RdbCloud::Query(const std::string &tableName, DBVBucket &extend, std::vector &data) +{ + auto cursor = cloudDB_->Query(tableName, ValueProxy::Convert(std::move(extend))); + if (cursor == nullptr) { + ZLOGE("cursor is null, table:%{public}s, extend:%{public}zu", tableName.c_str(), extend.size()); + return ConvertStatus(static_cast(E_ERROR)); + } + int32_t count = cursor->GetCount(); + data.reserve(count); + auto err = cursor->MoveToFirst(); + while (err == E_OK && count > 0) { + DistributedData::VBucket entry; + err = cursor->GetEntry(entry); + if (err != E_OK) { + break; + } + data.emplace_back(ValueProxy::Convert(std::move(entry))); + err = cursor->MoveToNext(); + count--; + } + return ConvertStatus(static_cast(err)); +} + +std::pair RdbCloud::Lock() +{ + auto error = cloudDB_->Lock(); + return std::make_pair(ConvertStatus(static_cast(error)), 0); +} + +DBStatus RdbCloud::UnLock() +{ + auto error = cloudDB_->Unlock(); + return ConvertStatus(static_cast(error)); +} + +DBStatus RdbCloud::HeartBeat() +{ + auto error = cloudDB_->Heartbeat(); + return ConvertStatus(static_cast(error)); +} + +DBStatus RdbCloud::Close() +{ + auto error = cloudDB_->Close(); + return ConvertStatus(static_cast(error)); +} + + +DBStatus RdbCloud::ConvertStatus(DistributedData::GeneralError error) +{ + switch (error) { + case GeneralError::E_OK: + return DBStatus::OK; + case GeneralError::E_BUSY: + return DBStatus::BUSY; + case GeneralError::E_INVALID_ARGS: + return DBStatus::INVALID_ARGS; + case GeneralError::E_NOT_SUPPORT: + return DBStatus::NOT_SUPPORT; + case GeneralError::E_ERROR: // fallthrough + case GeneralError::E_NOT_INIT: + case GeneralError::E_ALREADY_CONSUMED: + case GeneralError::E_ALREADY_CLOSED: + return DBStatus::CLOUD_ERROR; + default: + ZLOGE("unknown error:0x%{public}x", error); + break; + } + return DBStatus::CLOUD_ERROR; +} +} // namespace OHOS::DistributedRdb \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.h new file mode 100644 index 00000000..30e4ee59 --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_cloud.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 OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_H +#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_H +#include "cloud/cloud_db.h" +#include "cloud/cloud_store_types.h" +#include "cloud/icloud_db.h" +#include "error/general_error.h" + +namespace OHOS::DistributedRdb { +class RdbCloud : public DistributedDB::ICloudDb { +public: + using DBStatus = DistributedDB::DBStatus; + using DBVBucket = DistributedDB::VBucket; + + explicit RdbCloud(std::shared_ptr cloudDB); + virtual ~RdbCloud() = default; + DBStatus BatchInsert(const std::string &tableName, std::vector &&record, + std::vector &extend) override; + DBStatus BatchUpdate(const std::string &tableName, std::vector &&record, + std::vector &extend) override; + DBStatus BatchDelete(const std::string &tableName, std::vector &extend) override; + DBStatus Query(const std::string &tableName, DBVBucket &extend, std::vector &data) override; + std::pair Lock() override; + DBStatus UnLock() override; + DBStatus HeartBeat() override; + DBStatus Close() override; + DBStatus ConvertStatus(DistributedData::GeneralError error); + +private: + std::shared_ptr cloudDB_; +}; +} // namespace OHOS::DistributedRdb +#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_H diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_cursor.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_cursor.h index a8e19bfb..f51a4492 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_cursor.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_cursor.h @@ -20,7 +20,7 @@ namespace OHOS::DistributedRdb { class RdbCursor : public DistributedData::Cursor { public: - RdbCursor(std::shared_ptr resultSet); + explicit RdbCursor(std::shared_ptr resultSet); ~RdbCursor(); int32_t GetColumnNames(std::vector &names) const override; int32_t GetColumnName(int32_t col, std::string &name) const override; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp index 0f79fc62..f4ce8ebd 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -15,6 +15,9 @@ #define LOG_TAG "RdbGeneralStore" #include "rdb_general_store.h" +#include "cloud/asset_loader.h" +#include "cloud/cloud_db.h" +#include "cloud/schema_meta.h" #include "crypto_manager.h" #include "log_print.h" #include "metadata/meta_data_manager.h" @@ -29,6 +32,9 @@ namespace OHOS::DistributedRdb { using namespace DistributedData; using namespace DistributedDB; using namespace NativeRdb; +using DBField = DistributedDB::Field; +using DBTable = DistributedDB::TableSchema; +using DBSchema = DistributedDB::DataBaseSchema; class RdbOpenCallbackImpl : public RdbOpenCallback { public: int OnCreate(RdbStore &rdbStore) override @@ -43,6 +49,7 @@ public: RdbGeneralStore::RdbGeneralStore(const StoreMetaData &meta) : manager_(meta.appId, meta.user, meta.instanceId) { + observer_.storeId_ = meta.storeId; RelationalStoreDelegate::Option option; if (meta.isEncrypt) { std::string key = meta.GetSecretKey(); @@ -70,38 +77,83 @@ RdbGeneralStore::RdbGeneralStore(const StoreMetaData &meta) : manager_(meta.appI } } -int32_t RdbGeneralStore::Bind(const SchemaMeta &schemaMeta, std::shared_ptr cloudDb) +RdbGeneralStore::~RdbGeneralStore() { - cloudDb_ = std::move(cloudDb); - return 0; + manager_.CloseStore(delegate_); + delegate_ = nullptr; + store_ = nullptr; + bindInfo_.loader_ = nullptr; + bindInfo_.db_->Close(); + bindInfo_.db_ = nullptr; + rdbCloud_ = nullptr; +} + +int32_t RdbGeneralStore::Bind(const Database &database, BindInfo bindInfo) +{ + if (bindInfo.db_ == nullptr) { + return GeneralError::E_INVALID_ARGS; + } + + if (isBound_.exchange(true)) { + return GeneralError::E_OK; + } + + bindInfo_ = std::move(bindInfo); + rdbCloud_ = std::make_shared(bindInfo_.db_); + if (rdbCloud_ == nullptr) { + ZLOGE("rdb_cloudDb is null"); + return GeneralError::E_ERROR; + } + delegate_->SetCloudDB(rdbCloud_); + DBSchema schema; + schema.tables.resize(database.tables.size()); + for (size_t i = 0; i < database.tables.size(); i++) { + const Table &table = database.tables[i]; + DBTable &dbTable = schema.tables[i]; + dbTable.name = table.name; + for (auto &field : table.fields) { + DBField dbField; + dbField.colName = field.colName; + dbField.type = field.type; + dbField.primary = field.primary; + dbField.nullable = field.nullable; + dbTable.fields.push_back(std::move(dbField)); + } + } + delegate_->SetCloudDbSchema(std::move(schema)); + return GeneralError::E_OK; +} + +bool RdbGeneralStore::IsBound() +{ + return isBound_; } int32_t RdbGeneralStore::Close() { - manager_.CloseStore(delegate_); + auto status = manager_.CloseStore(delegate_); + if (status != DBStatus::OK) { + return status; + } delegate_ = nullptr; store_ = nullptr; - cloudDb_ = nullptr; + bindInfo_.loader_ = nullptr; + bindInfo_.db_->Close(); + bindInfo_.db_ = nullptr; return 0; } int32_t RdbGeneralStore::Execute(const std::string &table, const std::string &sql) { auto ret = store_->ExecuteSql(sql); - if (ret == NativeRdb::E_OK) { - return GeneralError::E_OK; - } - return GeneralError::E_ERROR; + return (ret == NativeRdb::E_OK) ? GeneralError::E_OK : GeneralError::E_ERROR; } int32_t RdbGeneralStore::BatchInsert(const std::string &table, VBuckets &&values) { int64_t outRowId; auto ret = store_->BatchInsert(outRowId, table, ValueProxy::Convert(std::move(values))); - if (ret == NativeRdb::E_OK) { - return GeneralError::E_OK; - } - return GeneralError::E_ERROR; + return (ret == NativeRdb::E_OK) ? GeneralError::E_OK : GeneralError::E_ERROR; } int32_t RdbGeneralStore::BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) @@ -132,40 +184,136 @@ std::shared_ptr RdbGeneralStore::Query(const std::string &table, GenQuer if (ret != GeneralError::E_OK || rdbQuery == nullptr) { return nullptr; } - return std::make_shared(store_->QueryByStep(rdbQuery->predicates_, {})); + return std::make_shared(store_->QueryByStep(rdbQuery->sql_)); } -int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) +int32_t RdbGeneralStore::Sync(const Devices &devices, int32_t mode, GenQuery &query, DetailAsync async, int32_t wait) { + DistributedDB::Query dbQuery; RdbQuery *rdbQuery = nullptr; auto ret = query.QueryInterface(rdbQuery); if (ret != GeneralError::E_OK || rdbQuery == nullptr) { - return GeneralError::E_OK; + dbQuery.FromTable(query.GetTables()); + } else { + dbQuery = rdbQuery->query_; + } + auto dbMode = DistributedDB::SyncMode(mode); + auto status = (mode < CLOUD_BEGIN) + ? delegate_->Sync(devices, dbMode, dbQuery, GetDBBriefCB(std::move(async)), wait) + : delegate_->Sync(devices, dbMode, dbQuery, GetDBProcessCB(std::move(async)), wait); + // mock + if (observer_.HasWatcher()) { + Watcher::Origin origin; + origin.origin = (mode < CLOUD_BEGIN) ? Watcher::Origin::ORIGIN_NEARBY : Watcher::Origin::ORIGIN_CLOUD; + origin.id = devices; + origin.store = observer_.storeId_; + observer_.watcher_->OnChange(origin, {}, {}); } - auto status = delegate_->Sync( - devices, DistributedDB::SyncMode(mode), RdbSyncer::MakeQuery(rdbQuery->predicates_.GetDistributedPredicates()), - [async](const std::map> &result) { - std::map> detail; - for (auto &[key, tables] : result) { - auto value = detail[key]; - for (auto &table : tables) { - value[std::move(table.tableName)] = table.status; - } - } - async(std::move(detail)); - }, - wait); return status == DistributedDB::OK ? GeneralError::E_OK : GeneralError::E_ERROR; } int32_t RdbGeneralStore::Watch(int32_t origin, Watcher &watcher) { - return GeneralError::E_NOT_SUPPORT; + if (origin != Watcher::Origin::ORIGIN_ALL || observer_.watcher_ != nullptr) { + return GeneralError::E_INVALID_ARGS; + } + + observer_.watcher_ = &watcher; + return GeneralError::E_OK; } int32_t RdbGeneralStore::Unwatch(int32_t origin, Watcher &watcher) { - return GeneralError::E_NOT_SUPPORT; + if (origin != Watcher::Origin::ORIGIN_ALL || observer_.watcher_ != &watcher) { + return GeneralError::E_INVALID_ARGS; + } + + observer_.watcher_ = nullptr; + return GeneralError::E_OK; } +RdbGeneralStore::DBBriefCB RdbGeneralStore::GetDBBriefCB(DetailAsync async) +{ + if (!async) { + return [](auto &) {}; + } + return [async = std::move(async)](const std::map> &result) { + DistributedData::GenDetails details; + for (auto &[key, tables] : result) { + auto &value = details[key]; + value.progress = FINISHED; + value.progress = GeneralError::E_OK; + for (auto &table : tables) { + if (table.status != DBStatus::OK) { + value.code = GeneralError::E_ERROR; + } + } + } + async(details); + }; +} + +RdbGeneralStore::DBProcessCB RdbGeneralStore::GetDBProcessCB(DetailAsync async) +{ + if (!async) { + return [](auto &) {}; + } + + return [async = std::move(async)](const std::map &processes) { + DistributedData::GenDetails details; + for (auto &[id, process] : processes) { + auto &detail = details[id]; + detail.progress = process.process; + detail.code = process.errCode == DBStatus::OK ? GeneralError::E_OK : GeneralError::E_ERROR; + for (auto [key, value] : process.tableProcess) { + auto &table = detail.details[key]; + table.upload.total = value.upLoadInfo.total; + table.upload.success = value.upLoadInfo.successCount; + table.upload.failed = value.upLoadInfo.failCount; + table.upload.untreated = table.upload.total - table.upload.success - table.upload.failed; + table.download.total = value.downLoadInfo.total; + table.download.success = value.downLoadInfo.successCount; + table.download.failed = value.downLoadInfo.failCount; + table.download.untreated = table.download.total - table.download.success - table.download.failed; + } + } + async(details); + }; +} + +void RdbGeneralStore::ObserverProxy::OnChange(const DBChangedIF &data) +{ + if (!HasWatcher()) { + return; + } + return; +} + +void RdbGeneralStore::ObserverProxy::OnChange(DBOrigin origin, const std::string &originalId, DBChangedData &&data) +{ + using GenOrigin = Watcher::Origin; + if (!HasWatcher()) { + return; + } + GenOrigin genOrigin; + genOrigin.origin = (origin == DBOrigin::ORIGIN_LOCAL) ? GenOrigin::ORIGIN_LOCAL + : (origin == DBOrigin::ORIGIN_CLOUD) ? GenOrigin::ORIGIN_CLOUD + : GenOrigin::ORIGIN_NEARBY; + genOrigin.id.push_back(originalId); + genOrigin.store = storeId_; + Watcher::PRIFields fields; + Watcher::ChangeInfo changeInfo; + for (int i = 0; i < DistributedDB::OP_BUTT; ++i) { + auto &info = changeInfo[data.tableName][i]; + for (auto &priData : data.primaryData[i]) { + Watcher::PRIValue value; + Convert(std::move(*(priData.begin())), value); + info.push_back(std::move(value)); + } + } + if (!data.field.empty()) { + fields[std::move(data.tableName)] = std::move(*(data.field.begin())); + } + watcher_->OnChange(genOrigin, fields, std::move(changeInfo)); +} } // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.h index 051115e2..62c8c307 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -15,13 +15,15 @@ #ifndef OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_STORE_H #define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_STORE_H +#include #include +#include "metadata/store_meta_data.h" +#include "rdb_cloud.h" +#include "rdb_store.h" #include "relational_store_delegate.h" #include "relational_store_manager.h" - -#include "rdb_store.h" #include "store/general_store.h" -#include "metadata/store_meta_data.h" +#include "store/general_value.h" namespace OHOS::DistributedRdb { class RdbGeneralStore : public DistributedData::GeneralStore { public: @@ -32,32 +34,54 @@ public: using Value = DistributedData::Value; using Values = DistributedData::Values; using StoreMetaData = DistributedData::StoreMetaData; - using SchemaMeta = DistributedData::SchemaMeta; - using CloudDB = DistributedData::CloudDB; + using Database = DistributedData::Database; using RdbStore = OHOS::NativeRdb::RdbStore; - using RdbDelegate = DistributedDB::RelationalStoreDelegate; - using RdbManager = DistributedDB::RelationalStoreManager; explicit RdbGeneralStore(const StoreMetaData &metaData); - int32_t Bind(const SchemaMeta &schemaMeta, std::shared_ptr cloudDb) override; + ~RdbGeneralStore(); + int32_t Bind(const Database &database, BindInfo bindInfo) override; + bool IsBound() 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, GenQuery &query) override; - int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, Async async, int32_t wait) override; + int32_t Sync(const Devices &devices, int32_t mode, GenQuery &query, DetailAsync async, int32_t wait) override; int32_t Watch(int32_t origin, Watcher &watcher) override; int32_t Unwatch(int32_t origin, Watcher &watcher) override; int32_t Close() override; private: + using RdbDelegate = DistributedDB::RelationalStoreDelegate; + using RdbManager = DistributedDB::RelationalStoreManager; + using SyncProcess = DistributedDB::SyncProcess; + using DBBriefCB = DistributedDB::SyncStatusCallback; + using DBProcessCB = std::function &processes)>; static constexpr uint32_t ITERATE_TIMES = 10000; + class ObserverProxy : public DistributedDB::StoreObserver { + public: + using DBChangedIF = DistributedDB::StoreChangedData; + using DBChangedData = DistributedDB::ChangedData; + using DBOrigin = DistributedDB::Origin; + void OnChange(const DistributedDB::StoreChangedData &data) override; + void OnChange(DBOrigin origin, const std::string &originalId, DBChangedData &&data) override; + bool HasWatcher() const { return watcher_ == nullptr; }; + private: + friend RdbGeneralStore; + Watcher *watcher_ = nullptr; + std::string storeId_; + }; + DBBriefCB GetDBBriefCB(DetailAsync async); + DBProcessCB GetDBProcessCB(DetailAsync async); + ObserverProxy observer_; RdbManager manager_; RdbDelegate *delegate_ = nullptr; std::shared_ptr store_; - std::shared_ptr cloudDb_; + std::shared_ptr rdbCloud_ {}; + BindInfo bindInfo_; + std::atomic isBound_ = false; }; } // namespace OHOS::DistributedRdb #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_STORE_H diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.cpp index 8818a092..6adca014 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.cpp @@ -27,7 +27,7 @@ RdbNotifierProxy::~RdbNotifierProxy() noexcept ZLOGI("destroy"); } -int32_t RdbNotifierProxy::OnComplete(uint32_t seqNum, const SyncResult &result) +int32_t RdbNotifierProxy::OnComplete(uint32_t seqNum, Details &&result) { MessageParcel data; if (!data.WriteInterfaceToken(GetDescriptor())) { @@ -67,4 +67,25 @@ int RdbNotifierProxy::OnChange(const std::string &storeName, const std::vectorSendRequest(RDB_NOTIFIER_CMD_DATA_DETAILS, data, reply, option) != 0) { + ZLOGE("send request failed"); + return RDB_ERROR; + } + return RDB_OK; +} } // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.h index 375cc745..20b05f32 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_notifier_proxy.h @@ -29,10 +29,12 @@ public: explicit RdbNotifierProxy(const sptr& object); virtual ~RdbNotifierProxy() noexcept; - int32_t OnComplete(uint32_t seqNum, const SyncResult& result) override; + int32_t OnComplete(uint32_t seqNum, Details &&result) override; int32_t OnChange(const std::string& storeName, const std::vector& devices) override; + int32_t OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo) override; + private: static inline BrokerDelegator delegator_; }; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_query.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_query.cpp index fc990eb1..4830c1fb 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_query.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_query.cpp @@ -20,4 +20,9 @@ bool RdbQuery::IsEqual(uint64_t tid) { return tid == TYPE_ID; } + +std::vector RdbQuery::GetTables() +{ + return {}; +} } // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_query.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_query.h index a8db5bf8..aea76acf 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_query.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_query.h @@ -17,18 +17,21 @@ #define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_QUERY_H #include "rdb_predicates.h" #include "store/general_value.h" +#include "query.h" namespace OHOS::DistributedRdb { class RdbQuery : public DistributedData::GenQuery { public: using Predicates = NativeRdb::RdbPredicates; - static constexpr uint64_t TYPE_ID = 0; + static constexpr uint64_t TYPE_ID = 0x20000001; RdbQuery() = default; ~RdbQuery() override = default; bool IsEqual(uint64_t tid) override; + std::vector GetTables() override; - NativeRdb::RdbPredicates predicates_; + DistributedDB::Query query_; + std::string sql_; }; } // namespace OHOS::DistributedRdb #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_QUERY_H diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index f7a9b2c7..31ec0345 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -20,7 +20,7 @@ #include "cloud/cloud_event.h" #include "communicator/device_manager_adapter.h" #include "crypto_manager.h" -#include "directory_manager.h" +#include "directory/directory_manager.h" #include "eventcenter/event_center.h" #include "ipc_skeleton.h" #include "log_print.h" @@ -68,25 +68,6 @@ RdbServiceImpl::Factory::~Factory() { } -RdbServiceImpl::DeathRecipientImpl::DeathRecipientImpl(const DeathCallback& callback) - : callback_(callback) -{ - ZLOGI("construct"); -} - -RdbServiceImpl::DeathRecipientImpl::~DeathRecipientImpl() -{ - ZLOGI("destroy"); -} - -void RdbServiceImpl::DeathRecipientImpl::OnRemoteDied(const wptr &object) -{ - ZLOGI("enter"); - if (callback_) { - callback_(); - } -} - RdbServiceImpl::RdbServiceImpl() : autoLaunchObserver_(this) { ZLOGI("construct"); @@ -94,7 +75,7 @@ RdbServiceImpl::RdbServiceImpl() : autoLaunchObserver_(this) [this](const std::string& identifier, DistributedDB::AutoLaunchParam ¶m) { return ResolveAutoLaunch(identifier, param); }); - EventCenter::GetInstance().Subscribe(CloudEvent::DATA_CHANGE, [this](const Event &event) { + auto process = [this](const Event &event) { auto &evt = static_cast(event); auto storeInfo = evt.GetStoreInfo(); StoreMetaData meta; @@ -114,10 +95,8 @@ RdbServiceImpl::RdbServiceImpl() : autoLaunchObserver_(this) ZLOGE("store null, storeId:%{public}s", meta.storeId.c_str()); return; } - for (const auto &watcher : watchers) { // mock for datachange - watcher->OnChange(GeneralWatcher::Origin::ORIGIN_CLOUD, {}); - } - }); + }; + EventCenter::GetInstance().Subscribe(CloudEvent::CLOUD_SYNC, process); } int32_t RdbServiceImpl::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) @@ -161,6 +140,12 @@ int32_t RdbServiceImpl::ResolveAutoLaunch(const std::string &identifier, Distrib return false; } +int32_t RdbServiceImpl::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) +{ + OnClientDied(pid); + return E_OK; +} + void RdbServiceImpl::OnClientDied(pid_t pid) { ZLOGI("client dead pid=%{public}d", pid); @@ -171,10 +156,10 @@ void RdbServiceImpl::OnClientDied(pid_t pid) } return false; }); - notifiers_.Erase(pid); - identifiers_.EraseIf([pid](const auto& key, pid_t& value) { - return pid == value; + identifiers_.EraseIf([pid](const auto &key, std::pair &value) { + return value.first == pid; }); + syncAgents_.EraseIf([pid](auto &key, SyncAgent &agent) { return agent.pid_ == pid; }); } bool RdbServiceImpl::CheckAccess(const std::string& bundleName, const std::string& storeName) @@ -184,7 +169,7 @@ bool RdbServiceImpl::CheckAccess(const std::string& bundleName, const std::strin storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); storeInfo.bundleName = bundleName; storeInfo.storeId = RdbSyncer::RemoveSuffix(storeName); - auto instanceId = RdbSyncer::GetInstIndex(storeInfo.tokenId, storeInfo.bundleName); + auto [instanceId, user] = RdbSyncer::GetInstIndexAndUser(storeInfo.tokenId, storeInfo.bundleName); if (instanceId != 0) { return false; } @@ -212,26 +197,23 @@ int32_t RdbServiceImpl::InitNotifier(const RdbSyncerParam ¶m, const sptr(notifier); pid_t pid = IPCSkeleton::GetCallingPid(); - auto recipient = new (std::nothrow) DeathRecipientImpl([this, pid] { - OnClientDied(pid); + uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); + syncAgents_.Compute(tokenId, [¶m, notifierProxy, pid](auto, SyncAgent &agent) { + if (pid != agent.pid_) { + agent.ReInit(pid, param.bundleName_); + } + agent.SetNotifier(notifierProxy); + return true; }); - if (recipient == nullptr) { - ZLOGE("malloc recipient failed"); - return RDB_ERROR; - } - - if (!notifier->AddDeathRecipient(recipient)) { - ZLOGE("link to death failed"); - return RDB_ERROR; - } - notifiers_.Insert(pid, iface_cast(notifier)); - ZLOGI("success pid=%{public}d", pid); + ZLOGI("success tokenId:%{public}x, pid=%{public}d", tokenId, pid); return RDB_OK; } -void RdbServiceImpl::OnDataChange(pid_t pid, const DistributedDB::StoreChangedData &data) +void RdbServiceImpl::OnDataChange(pid_t pid, uint32_t tokenId, const DistributedDB::StoreChangedData &data) { DistributedDB::StoreProperty property; data.GetStoreProperty(property); @@ -239,20 +221,21 @@ void RdbServiceImpl::OnDataChange(pid_t pid, const DistributedDB::StoreChangedDa if (pid == 0) { auto identifier = RelationalStoreManager::GetRelationalStoreIdentifier(property.userId, property.appId, property.storeId); - auto pair = identifiers_.Find(TransferStringToHex(identifier)); - if (!pair.first) { + auto [success, info] = identifiers_.Find(TransferStringToHex(identifier)); + if (!success) { ZLOGI("client doesn't subscribe"); return; } - pid = pair.second; - ZLOGI("fixed pid=%{public}d", pid); + pid = info.first; + tokenId = info.second; + ZLOGI("fixed pid=%{public}d and tokenId=0x%{public}d", pid, tokenId); } - notifiers_.ComputeIfPresent(pid, [&data, &property] (const auto& key, const sptr& value) { + auto [success, agent] = syncAgents_.Find(tokenId); + if (success && agent.notifier_ != nullptr && pid == agent.pid_) { std::string device = data.GetDataChangeDevice(); auto networkId = DmAdapter::GetInstance().ToNetworkID(device); - value->OnChange(property.storeId, { networkId }); - return true; - }); + agent.notifier_->OnChange(property.storeId, { networkId }); + } } void RdbServiceImpl::SyncerTimeout(std::shared_ptr syncer) @@ -293,14 +276,14 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa ZLOGE("pid: %{public}d, syncers size: %{public}zu. syncerNum: %{public}d", pid, syncers.size(), syncerNum_); return !syncers.empty(); } - auto rdbObserver = new (std::nothrow) RdbStoreObserverImpl(this, pid); + auto rdbObserver = new (std::nothrow) RdbStoreObserverImpl(this, pid, tokenId); if (rdbObserver == nullptr) { return !syncers.empty(); } auto syncer_ = std::make_shared(param, rdbObserver); StoreMetaData storeMetaData = GetStoreMetaData(param); MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData); - if (syncer_->Init(pid, uid, tokenId, storeMetaData) != 0) { + if (syncer_->Init(pid, uid, tokenId, storeMetaData) != RDB_OK) { return !syncers.empty(); } syncers[storeId] = syncer_; @@ -314,14 +297,15 @@ std::shared_ptr RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa }); if (syncer != nullptr) { - identifiers_.Insert(syncer->GetIdentifier(), pid); + identifiers_.Insert(syncer->GetIdentifier(), { pid, tokenId }); } else { ZLOGE("syncer is nullptr"); } return syncer; } -int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables) +int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables, + int32_t type) { ZLOGI("enter"); if (!CheckAccess(param.bundleName_, param.storeName_)) { @@ -335,44 +319,45 @@ int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const return syncer->SetDistributedTables(tables); } -int32_t RdbServiceImpl::DoSync(const RdbSyncerParam ¶m, const SyncOption &option, - const RdbPredicates &predicates, SyncResult &result) +std::pair RdbServiceImpl::DoSync(const RdbSyncerParam ¶m, const Option &option, + const RdbPredicates &pred) { if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); - return RDB_ERROR; + return {RDB_ERROR, {}}; } auto syncer = GetRdbSyncer(param); if (syncer == nullptr) { - return RDB_ERROR; + return {RDB_ERROR, {}}; } - return syncer->DoSync(option, predicates, result); + Details details; + auto status = syncer->DoSync(option, pred, [&details](auto &&result) mutable { details = std::move(result); }); + return { status, std::move(details) }; } -void RdbServiceImpl::OnAsyncComplete(pid_t pid, uint32_t seqNum, const SyncResult &result) +void RdbServiceImpl::OnAsyncComplete(uint32_t tokenId, uint32_t seqNum, Details &&result) { - ZLOGI("pid=%{public}d seqnum=%{public}u", pid, seqNum); - notifiers_.ComputeIfPresent(pid, [seqNum, &result] (const auto& key, const sptr& value) { - value->OnComplete(seqNum, result); - return true; - }); + ZLOGI("pid=%{public}x seqnum=%{public}u", tokenId, seqNum); + auto [success, agent] = syncAgents_.Find(tokenId); + if (success && agent.notifier_ != nullptr) { + agent.notifier_->OnComplete(seqNum, std::move(result)); + } } -int32_t RdbServiceImpl::DoAsync(const RdbSyncerParam ¶m, uint32_t seqNum, const SyncOption &option, - const RdbPredicates &predicates) +int32_t RdbServiceImpl::DoAsync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &pred) { if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } - pid_t pid = IPCSkeleton::GetCallingPid(); - ZLOGI("seq num=%{public}u", seqNum); + auto tokenId = IPCSkeleton::GetCallingTokenID(); + ZLOGI("seq num=%{public}u", option.seqNum); auto syncer = GetRdbSyncer(param); if (syncer == nullptr) { return RDB_ERROR; } - return syncer->DoAsync(option, predicates, [this, pid, seqNum](const SyncResult &result) { - OnAsyncComplete(pid, seqNum, result); + return syncer->DoSync(option, pred, [this, tokenId, seqNum = option.seqNum](Details &&result) { + OnAsyncComplete(tokenId, seqNum, std::move(result)); }); } @@ -404,80 +389,37 @@ std::string RdbServiceImpl::GenIdentifier(const RdbSyncerParam ¶m) return TransferStringToHex(identifier); } -int32_t RdbServiceImpl::DoSubscribe(const RdbSyncerParam& param, const SubscribeOption &option) -{ - pid_t pid = IPCSkeleton::GetCallingPid(); - auto tokenId = IPCSkeleton::GetCallingTokenID(); - switch (option.mode) { - case SubscribeMode::REMOTE: { - auto identifier = GenIdentifier(param); - ZLOGI("%{public}s %{public}.6s %{public}d", param.storeName_.c_str(), identifier.c_str(), pid); - identifiers_.Insert(identifier, pid); - break; - } - case SubscribeMode::CLOUD: // fallthrough - case SubscribeMode::CLOUD_DETAIL: { - syncAgents_.Compute(tokenId, [this, pid, tokenId, ¶m, &option](auto &key, SyncAgent &value) { - if (pid != value.pid_) { - value.ReInit(pid, param.bundleName_); - } - auto storeName = RdbSyncer::RemoveSuffix(param.storeName_); - auto it = value.watchers_.find(storeName); - if (it == value.watchers_.end()) { - auto watcher = std::make_shared(this, tokenId, storeName); - value.watchers_[storeName] = { watcher }; - value.mode_[storeName] = option.mode; - } - return true; - }); - break; - } - default: - return RDB_ERROR; - } - return RDB_OK; -} - -void RdbServiceImpl::OnChange(uint32_t tokenId, const std::string &storeName) -{ - pid_t pid = 0; - syncAgents_.ComputeIfPresent(tokenId, [&pid, &storeName](auto &key, SyncAgent &value) { - pid = value.pid_; - return true; - }); - notifiers_.ComputeIfPresent(pid, [&storeName](const auto& key, const sptr& value) { - value->OnChange(storeName, { storeName }); - return true; - }); -} - AutoCache::Watchers RdbServiceImpl::GetWatchers(uint32_t tokenId, const std::string &storeName) { - AutoCache::Watchers watchers; - syncAgents_.ComputeIfPresent(tokenId, [&storeName, &watchers](auto, SyncAgent &agent) { - auto it = agent.watchers_.find(storeName); - if (it != agent.watchers_.end()) { - watchers = it->second; - } - return true; - }); - return watchers; + auto [success, agent] = syncAgents_.Find(tokenId); + return { agent.watcher_ }; } void RdbServiceImpl::SyncAgent::ReInit(pid_t pid, const std::string &bundleName) { pid_ = pid; + count_ = 0; bundleName_ = bundleName; - watchers_.clear(); - mode_.clear(); + notifier_ = nullptr; + if (watcher_ != nullptr) { + watcher_->SetNotifier(nullptr); + } } -int32_t RdbServiceImpl::DoUnSubscribe(const RdbSyncerParam& param) +void RdbServiceImpl::SyncAgent::SetNotifier(sptr notifier) { - auto identifier = GenIdentifier(param); - ZLOGI("%{public}s %{public}.6s", param.storeName_.c_str(), identifier.c_str()); - identifiers_.Erase(identifier); - return RDB_OK; + notifier_ = notifier; + if (watcher_ != nullptr) { + watcher_->SetNotifier(notifier); + } +} + +void RdbServiceImpl::SyncAgent::SetWatcher(std::shared_ptr watcher) +{ + if (watcher_ != watcher) { + watcher_ = watcher; + watcher_->SetNotifier(notifier_); + } } int32_t RdbServiceImpl::RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql, @@ -495,44 +437,65 @@ int32_t RdbServiceImpl::RemoteQuery(const RdbSyncerParam& param, const std::stri return syncer->RemoteQuery(device, sql, selectionArgs, resultSet); } -int32_t RdbServiceImpl::CreateRDBTable(const RdbSyncerParam ¶m) +int32_t RdbServiceImpl::Sync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &predicates, + const AsyncDetail &async) { - if (!CheckAccess(param.bundleName_, param.storeName_)) { - ZLOGE("permission error"); - return RDB_ERROR; + if (!option.isAsync) { + auto [status, details] = DoSync(param, option, predicates); + async(std::move(details)); + return status; } + return DoAsync(param, option, predicates); +} +int32_t RdbServiceImpl::Subscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, + RdbStoreObserver *observer) +{ pid_t pid = IPCSkeleton::GetCallingPid(); - auto rdbObserver = new (std::nothrow) RdbStoreObserverImpl(this, pid); - if (rdbObserver == nullptr) { - return RDB_ERROR; - } - auto syncer = new (std::nothrow) RdbSyncer(param, rdbObserver); - if (syncer == nullptr) { - ZLOGE("new syncer error"); - return RDB_ERROR; - } - auto uid = IPCSkeleton::GetCallingUid(); auto tokenId = IPCSkeleton::GetCallingTokenID(); - StoreMetaData storeMetaData = GetStoreMetaData(param); - MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData); - if (syncer->Init(pid, uid, tokenId, storeMetaData) != RDB_OK) { - ZLOGE("Init error"); - delete syncer; - return RDB_ERROR; + switch (option.mode) { + case SubscribeMode::REMOTE: { + auto identifier = GenIdentifier(param); + identifiers_.Insert(identifier, std::pair{ pid, tokenId }); + ZLOGI("%{public}s %{public}.6s %{public}d", param.storeName_.c_str(), identifier.c_str(), pid); + break; + } + case SubscribeMode::CLOUD: // fallthrough + case SubscribeMode::CLOUD_DETAIL: { + syncAgents_.Compute(tokenId, [pid, ¶m](auto &key, SyncAgent &agent) { + if (pid != agent.pid_) { + agent.ReInit(pid, param.bundleName_); + } + if (agent.watcher_ == nullptr) { + agent.SetWatcher(std::make_shared()); + } + agent.count_++; + return true; + }); + break; + } + default: + return RDB_ERROR; } - delete syncer; return RDB_OK; } -int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) +int32_t RdbServiceImpl::UnSubscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, + RdbStoreObserver *observer) { - if (!CheckAccess(param.bundleName_, param.storeName_)) { - ZLOGE("permission error"); - return RDB_ERROR; - } - auto meta = GetStoreMetaData(param); - return MetaDataManager::GetInstance().DelMeta(meta.GetKey()) ? RDB_OK : RDB_ERROR; + auto identifier = GenIdentifier(param); + ZLOGI("%{public}s %{public}.6s", param.storeName_.c_str(), identifier.c_str()); + identifiers_.Erase(identifier); + syncAgents_.ComputeIfPresent(IPCSkeleton::GetCallingTokenID(), [](auto &key, SyncAgent &agent) { + if (agent.count_ > 0) { + agent.count_--; + } + if (agent.count_ == 0) { + agent.SetWatcher(nullptr); + } + return true; + }); + return RDB_OK; } int32_t RdbServiceImpl::OnInitialize() @@ -551,12 +514,21 @@ int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) return RDB_ERROR; } - EventCenter::Defer defer; - CloudEvent::StoreInfo storeInfo { IPCSkeleton::GetCallingTokenID(), param.bundleName_, - RdbSyncer::RemoveSuffix(param.storeName_), - RdbSyncer::GetInstIndex(IPCSkeleton::GetCallingTokenID(), param.bundleName_) }; - auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "relational_store"); - EventCenter::GetInstance().PostEvent(move(event)); + if (executors_ != nullptr) { + CloudEvent::StoreInfo storeInfo; + storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + storeInfo.bundleName = param.bundleName_; + storeInfo.storeName = RdbSyncer::RemoveSuffix(param.storeName_); + auto [instanceId, user]= RdbSyncer::GetInstIndexAndUser(storeInfo.tokenId, param.bundleName_); + storeInfo.instanceId = instanceId; + storeInfo.user = user; + executors_->Execute([storeInfo]() { + auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo)); + EventCenter::GetInstance().PostEvent(move(event)); + return; + }); + } + return RDB_OK; } @@ -565,11 +537,12 @@ StoreMetaData RdbServiceImpl::GetStoreMetaData(const RdbSyncerParam ¶m) StoreMetaData metaData; metaData.uid = IPCSkeleton::GetCallingUid(); metaData.tokenId = IPCSkeleton::GetCallingTokenID(); - metaData.instanceId = RdbSyncer::GetInstIndex(metaData.tokenId, param.bundleName_); + auto [instanceId, user] = RdbSyncer::GetInstIndexAndUser(metaData.tokenId, param.bundleName_); + metaData.instanceId = instanceId; metaData.bundleName = param.bundleName_; metaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; metaData.storeId = RdbSyncer::RemoveSuffix(param.storeName_); - metaData.user = std::to_string(AccountDelegate::GetInstance()->GetUserByToken(metaData.tokenId)); + metaData.user = std::to_string(user); metaData.storeType = param.type_; metaData.securityLevel = param.level_; metaData.area = param.area_; @@ -594,16 +567,22 @@ int32_t RdbServiceImpl::CreateMetaData(const RdbSyncerParam ¶m, StoreMetaDat meta.isEncrypt, old.area, meta.area); return RDB_ERROR; } - - auto saved = MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta); - if (!saved) { - return RDB_ERROR; + if (!isCreated || meta != old) { + Upgrade(param, old); + ZLOGD("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " + "area:%{public}d->%{public}d", + meta.bundleName.c_str(), meta.storeId.c_str(), old.storeType, meta.storeType, old.isEncrypt, + meta.isEncrypt, old.area, meta.area); + MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta); } AppIDMetaData appIdMeta; appIdMeta.bundleName = meta.bundleName; appIdMeta.appId = meta.appId; - saved = MetaDataManager::GetInstance().SaveMeta(appIdMeta.GetKey(), appIdMeta, true); - if (!saved) { + if (!MetaDataManager::GetInstance().SaveMeta(appIdMeta.GetKey(), appIdMeta, true)) { + ZLOGE("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " + "area:%{public}d->%{public}d", + meta.bundleName.c_str(), meta.storeId.c_str(), old.storeType, meta.storeType, old.isEncrypt, + meta.isEncrypt, old.area, meta.area); return RDB_ERROR; } if (!param.isEncrypt_ || param.password_.empty()) { @@ -612,7 +591,7 @@ int32_t RdbServiceImpl::CreateMetaData(const RdbSyncerParam ¶m, StoreMetaDat return SetSecretKey(param, meta); } -bool RdbServiceImpl::SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta) +int32_t RdbServiceImpl::SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta) { SecretKeyMetaData newSecretKey; newSecretKey.storeType = meta.storeType; @@ -626,9 +605,23 @@ bool RdbServiceImpl::SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaDa return MetaDataManager::GetInstance().SaveMeta(meta.GetSecretKey(), newSecretKey, true) ? RDB_OK : RDB_ERROR; } -int32_t RdbServiceImpl::OnExecutor(std::shared_ptr executors) +int32_t RdbServiceImpl::Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &old) { - executors_ = std::move(executors); + if (old.storeType == RDB_DEVICE_COLLABORATION && old.version < StoreMetaData::UUID_CHANGED_TAG) { + auto syncer = GetRdbSyncer(param); + if (syncer == nullptr) { + ZLOGE("syncer is null, bundleName:%{public}s storeName:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str()); + return RDB_ERROR; + } + return syncer->RemoveDeviceData(); + } return RDB_OK; } + +int32_t RdbServiceImpl::OnBind(const BindInfo &bindInfo) +{ + executors_ = bindInfo.executors; + return 0; +} } // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h index c9f3eb54..654d75a3 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -15,9 +15,7 @@ #ifndef DISTRIBUTEDDATASERVICE_RDB_SERVICE_H #define DISTRIBUTEDDATASERVICE_RDB_SERVICE_H - #include "rdb_service_stub.h" - #include #include #include @@ -26,8 +24,9 @@ #include "metadata/store_meta_data.h" #include "rdb_notifier_proxy.h" #include "rdb_syncer.h" -#include "store_observer.h" +#include "rdb_watcher.h" #include "store/auto_cache.h" +#include "store_observer.h" #include "visibility.h" namespace OHOS::DistributedRdb { class API_EXPORT RdbServiceImpl : public RdbServiceStub { @@ -41,50 +40,64 @@ public: /* IPC interface */ std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; - int32_t InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) override; + int32_t InitNotifier(const RdbSyncerParam ¶m, sptr notifier) override; - int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector& tables) override; + int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables, + int32_t type = DISTRIBUTED_DEVICE) override; int32_t RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql, const std::vector& selectionArgs, sptr& resultSet) override; - void OnDataChange(pid_t pid, const DistributedDB::StoreChangedData& data); + int32_t Sync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &predicates, + const AsyncDetail &async) override; - void OnChange(uint32_t tokenId, const std::string &storeName); + int32_t Subscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, RdbStoreObserver *observer) override; + int32_t UnSubscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, + RdbStoreObserver *observer) override; - int32_t CreateRDBTable(const RdbSyncerParam ¶m) override; + void OnDataChange(pid_t pid, uint32_t tokenId, const DistributedDB::StoreChangedData& data); - int32_t DestroyRDBTable(const RdbSyncerParam ¶m) override; int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; - int32_t OnExecutor(std::shared_ptr executors) override; - int32_t OnInitialize() override; - int32_t GetSchema(const RdbSyncerParam ¶m) override; - -protected: - int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option, - const RdbPredicates& predicates, SyncResult& result) override; + int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) override; - int32_t DoAsync(const RdbSyncerParam& param, uint32_t seqNum, const SyncOption& option, - const RdbPredicates& predicates) override; - - int32_t DoSubscribe(const RdbSyncerParam& param, const SubscribeOption &option) override; + int32_t GetSchema(const RdbSyncerParam ¶m) override; - int32_t DoUnSubscribe(const RdbSyncerParam& param) override; + int32_t OnBind(const BindInfo &bindInfo) override; private: using Watchers = DistributedData::AutoCache::Watchers; struct SyncAgent { pid_t pid_ = 0; + int32_t count_ = 0; std::string bundleName_; - std::map mode_; - std::map watchers_; + sptr notifier_ = nullptr; + std::shared_ptr watcher_ = nullptr; void ReInit(pid_t pid, const std::string &bundleName); + void SetNotifier(sptr notifier); + void SetWatcher(std::shared_ptr watcher); }; + class Factory { + public: + Factory(); + ~Factory(); + private: + std::shared_ptr product_; + }; + using StoreSyncersType = std::map>; + + 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::pair DoSync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &pred); + + int32_t DoAsync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &pred); + Watchers GetWatchers(uint32_t tokenId, const std::string &storeName); std::string GenIdentifier(const RdbSyncerParam& param); @@ -95,45 +108,25 @@ private: std::shared_ptr GetRdbSyncer(const RdbSyncerParam& param); - void OnAsyncComplete(pid_t pid, uint32_t seqNum, const SyncResult& result); + void OnAsyncComplete(uint32_t tokenId, uint32_t seqNum, Details&& result); int32_t CreateMetaData(const RdbSyncerParam ¶m, StoreMetaData &old); - StoreMetaData GetStoreMetaData(const RdbSyncerParam ¶m); - bool SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta); - class DeathRecipientImpl : public IRemoteObject::DeathRecipient { - public: - using DeathCallback = std::function; - explicit DeathRecipientImpl(const DeathCallback& callback); - ~DeathRecipientImpl() override; - void OnRemoteDied(const wptr &object) override; - private: - const DeathCallback callback_; - }; - class Factory { - public: - Factory(); - ~Factory(); - private: - std::shared_ptr product_; - }; + StoreMetaData GetStoreMetaData(const RdbSyncerParam ¶m); - using StoreSyncersType = std::map>; - int32_t syncerNum_ {}; - ConcurrentMap syncers_; - ConcurrentMap> notifiers_; - ConcurrentMap identifiers_; - RdbStoreObserverImpl autoLaunchObserver_; + int32_t SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta); - static Factory factory_; + int32_t Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &old); static std::string TransferStringToHex(const std::string& origStr); - 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_; + static Factory factory_; + int32_t syncerNum_ {}; + ConcurrentMap syncers_; + ConcurrentMap> identifiers_; ConcurrentMap syncAgents_; + RdbStoreObserverImpl autoLaunchObserver_; + std::shared_ptr executors_; }; } // namespace OHOS::DistributedRdb #endif diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index e879c523..64fd4c85 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -94,16 +94,16 @@ int32_t RdbServiceStub::OnRemoteSetDistributedTables(MessageParcel &data, Messag int32_t RdbServiceStub::OnRemoteDoSync(MessageParcel &data, MessageParcel &reply) { RdbSyncerParam param; - SyncOption option {}; + Option option {}; RdbPredicates predicates; if (!ITypesUtil::Unmarshal(data, param, option, predicates)) { - ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables:%{public}s", param.bundleName_.c_str(), - param.storeName_.c_str(), predicates.table_.c_str()); + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables:%{public}zu", param.bundleName_.c_str(), + param.storeName_.c_str(), predicates.tables_.size()); return IPC_STUB_INVALID_DATA_ERR; } - SyncResult result; - auto status = DoSync(param, option, predicates, result); + Details result; + auto status = Sync(param, option, predicates, [&result](Details &&details) { result = std::move(details); }); 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; @@ -114,16 +114,14 @@ int32_t RdbServiceStub::OnRemoteDoSync(MessageParcel &data, MessageParcel &reply int32_t RdbServiceStub::OnRemoteDoAsync(MessageParcel &data, MessageParcel &reply) { RdbSyncerParam param; - uint32_t seqNum; - SyncOption option {}; + Option option {}; RdbPredicates predicates; - if (!ITypesUtil::Unmarshal(data, param, seqNum, option, predicates)) { + if (!ITypesUtil::Unmarshal(data, param, option, predicates)) { 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()); + param.bundleName_.c_str(), param.storeName_.c_str(), option.seqNum, (*(predicates.tables_.begin())).c_str()); return IPC_STUB_INVALID_DATA_ERR; } - - auto status = DoAsync(param, seqNum, option, predicates); + auto status = Sync(param, option, predicates, nullptr); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x", status); return IPC_STUB_WRITE_PARCEL_ERR; @@ -141,7 +139,7 @@ int32_t RdbServiceStub::OnRemoteDoSubscribe(MessageParcel &data, MessageParcel & return IPC_STUB_INVALID_DATA_ERR; } - auto status = DoSubscribe(param, option); + auto status = Subscribe(param, option, nullptr); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x", status); return IPC_STUB_WRITE_PARCEL_ERR; @@ -152,13 +150,14 @@ int32_t RdbServiceStub::OnRemoteDoSubscribe(MessageParcel &data, MessageParcel & int32_t RdbServiceStub::OnRemoteDoUnSubscribe(MessageParcel &data, MessageParcel &reply) { RdbSyncerParam param; + SubscribeOption option; if (!ITypesUtil::Unmarshal(data, param)) { 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); + auto status = UnSubscribe(param, option, nullptr); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x", status); return IPC_STUB_WRITE_PARCEL_ERR; @@ -211,38 +210,4 @@ int RdbServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageP } return RDB_ERROR; } - -int32_t RdbServiceStub::OnRemoteDoCreateTable(MessageParcel &data, MessageParcel &reply) -{ - RdbSyncerParam param; - if (!ITypesUtil::Unmarshal(data, param)) { - 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 = CreateRDBTable(param); - if (!ITypesUtil::Marshal(reply, status)) { - ZLOGE("Marshal status:0x%{public}x", status); - return IPC_STUB_WRITE_PARCEL_ERR; - } - return RDB_OK; -} - -int32_t RdbServiceStub::OnRemoteDoDestroyTable(MessageParcel &data, MessageParcel &reply) -{ - RdbSyncerParam param; - if (!ITypesUtil::Unmarshal(data, param)) { - 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 (!ITypesUtil::Marshal(reply, status)) { - ZLOGE("Marshal status:0x%{public}x", status); - return IPC_STUB_WRITE_PARCEL_ERR; - } - return RDB_OK; -} } // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h index 16f4c169..b67c3e5b 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.h @@ -27,24 +27,6 @@ public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.IRdbService"); int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) override; - int32_t Sync(const RdbSyncerParam& param, const SyncOption& option, - const RdbPredicates& predicates, const SyncCallback& callback) override - { - return 0; - } - - int32_t Subscribe(const RdbSyncerParam& param, const SubscribeOption& option, - RdbStoreObserver *observer) override - { - return 0; - } - - int32_t UnSubscribe(const RdbSyncerParam& param, const SubscribeOption& option, - RdbStoreObserver *observer) override - { - return 0; - } - private: static bool CheckInterfaceToken(MessageParcel& data); @@ -66,10 +48,6 @@ private: int32_t OnRemoteDoRemoteQuery(MessageParcel& data, MessageParcel& reply); - int32_t OnRemoteDoCreateTable(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteDoDestroyTable(MessageParcel& data, MessageParcel& reply); - using RequestHandle = int (RdbServiceStub::*)(MessageParcel &, MessageParcel &); static constexpr RequestHandle HANDLERS[RDB_SERVICE_CMD_MAX] = { [RDB_SERVICE_CMD_OBTAIN_TABLE] = &RdbServiceStub::OnRemoteObtainDistributedTableName, @@ -80,8 +58,6 @@ private: [RDB_SERVICE_CMD_SUBSCRIBE] = &RdbServiceStub::OnRemoteDoSubscribe, [RDB_SERVICE_CMD_UNSUBSCRIBE] = &RdbServiceStub::OnRemoteDoUnSubscribe, [RDB_SERVICE_CMD_REMOTE_QUERY] = &RdbServiceStub::OnRemoteDoRemoteQuery, - [RDB_SERVICE_CREATE_RDB_TABLE] = &RdbServiceStub::OnRemoteDoCreateTable, - [RDB_SERVICE_DESTROY_RDB_TABLE] = &RdbServiceStub::OnRemoteDoDestroyTable, [RDB_SERVICE_CMD_GET_SCHEMA] = &RdbServiceStub::OnGetSchema }; }; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_store_observer_impl.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_store_observer_impl.cpp index 036eedb8..e9e7ec53 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_store_observer_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_store_observer_impl.cpp @@ -20,8 +20,8 @@ #include "log_print.h" namespace OHOS::DistributedRdb { -RdbStoreObserverImpl::RdbStoreObserverImpl(RdbServiceImpl* owner, pid_t pid) - : pid_(pid), owner_(owner) +RdbStoreObserverImpl::RdbStoreObserverImpl(RdbServiceImpl* owner, pid_t pid, uint32_t tokenId) + : pid_(pid), tokenId_(tokenId), owner_(owner) { ZLOGI("construct"); } @@ -35,7 +35,7 @@ void RdbStoreObserverImpl::OnChange(const DistributedDB::StoreChangedData &data) { ZLOGI("enter"); if (owner_ != nullptr) { - owner_->OnDataChange(pid_, data); + owner_->OnDataChange(pid_, tokenId_, data); } } } // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_store_observer_impl.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_store_observer_impl.h index e0043f5f..0c7af901 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_store_observer_impl.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_store_observer_impl.h @@ -23,15 +23,16 @@ namespace OHOS::DistributedRdb { class RdbServiceImpl; class RdbStoreObserverImpl : public DistributedDB::StoreObserver { public: - explicit RdbStoreObserverImpl(RdbServiceImpl* owner, pid_t pid = 0); + explicit RdbStoreObserverImpl(RdbServiceImpl* owner, pid_t pid = 0, uint32_t tokenId = 0); ~RdbStoreObserverImpl() override; void OnChange(const DistributedDB::StoreChangedData &data) override; private: - pid_t pid_ {}; - RdbServiceImpl* owner_ {}; + pid_t pid_ = 0; + uint32_t tokenId_ = 0; + RdbServiceImpl* owner_ = nullptr; }; } // namespace OHOS::DistributedRdb #endif diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp index 00a31f9a..3b2e8596 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -20,20 +20,23 @@ #include "accesstoken_kit.h" #include "account/account_delegate.h" #include "checker/checker_manager.h" +#include "cloud/change_event.h" #include "crypto_manager.h" #include "device_manager_adapter.h" #include "directory_manager.h" +#include "eventcenter/event_center.h" #include "kvstore_utils.h" #include "log_print.h" #include "metadata/appid_meta_data.h" #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" +#include "rdb_query.h" #include "rdb_result_set_impl.h" +#include "store/general_store.h" #include "types.h" +#include "types_export.h" #include "utils/constant.h" #include "utils/converter.h" -#include "types_export.h" - using OHOS::DistributedKv::KvStoreUtils; using OHOS::DistributedKv::AccountDelegate; using namespace OHOS::Security::AccessToken; @@ -114,10 +117,6 @@ int32_t RdbSyncer::Init(pid_t pid, pid_t uid, uint32_t token, const StoreMetaDat return RDB_ERROR; } - if (meta.storeType == RDB_DEVICE_COLLABORATION && meta.version < StoreMetaData::UUID_CHANGED_TAG) { - delegate_->RemoveDeviceData(); - } - ZLOGI("success"); return RDB_OK; } @@ -186,10 +185,10 @@ int32_t RdbSyncer::InitDBDelegate(const StoreMetaData &meta) return RDB_OK; } -int32_t RdbSyncer::GetInstIndex(uint32_t tokenId, const std::string &bundleName) +std::pair RdbSyncer::GetInstIndexAndUser(uint32_t tokenId, const std::string &bundleName) { if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { - return 0; + return { 0, 0 }; } HapTokenInfo tokenInfo; @@ -198,9 +197,9 @@ int32_t RdbSyncer::GetInstIndex(uint32_t tokenId, const std::string &bundleName) if (errCode != RET_SUCCESS) { ZLOGE("GetHapTokenInfo error:%{public}d, tokenId:0x%{public}x appId:%{public}s", errCode, tokenId, bundleName.c_str()); - return -1; + return { -1, -1 }; } - return tokenInfo.instIndex; + return { tokenInfo.instIndex, tokenInfo.userID }; } DistributedDB::RelationalStoreDelegate* RdbSyncer::GetDelegate() @@ -259,9 +258,9 @@ std::vector RdbSyncer::NetworkIdToUUID(const std::vector> &syncStatus, - SyncResult &result) +Details RdbSyncer::HandleSyncStatus(const std::map> &syncStatus) { + Details details; for (const auto& status : syncStatus) { auto res = DistributedDB::DBStatus::OK; for (const auto& tableStatus : status.second) { @@ -276,9 +275,27 @@ void RdbSyncer::HandleSyncStatus(const std::map 1) { + query.FromTable(predicates.tables_); + } for (const auto& operation : predicates.operations_) { if (operation.operator_ >= 0 && operation.operator_ < OPERATOR_MAX) { HANDLES[operation.operator_](operation, query); @@ -337,55 +357,41 @@ DistributedDB::Query RdbSyncer::MakeQuery(const RdbPredicates &predicates) return query; } -int32_t RdbSyncer::DoSync(const SyncOption &option, const RdbPredicates &predicates, SyncResult &result) -{ - ZLOGI("enter"); - auto* delegate = GetDelegate(); - if (delegate == nullptr) { - ZLOGE("delegate is nullptr"); - return RDB_ERROR; - } - - std::vector devices; - if (predicates.devices_.empty()) { - devices = NetworkIdToUUID(GetConnectDevices()); - } else { - devices = NetworkIdToUUID(predicates.devices_); - } - - ZLOGI("delegate sync"); - return delegate->Sync( - devices, static_cast(option.mode), MakeQuery(predicates), - [&result](const std::map> &syncStatus) { - HandleSyncStatus(syncStatus, result); - }, - true); -} - -int32_t RdbSyncer::DoAsync(const SyncOption &option, const RdbPredicates &predicates, const SyncCallback& callback) +int32_t RdbSyncer::DoSync(const Option &option, const RdbPredicates &predicates, const AsyncDetail &async) { - auto* delegate = GetDelegate(); + auto *delegate = GetDelegate(); if (delegate == nullptr) { ZLOGE("delegate is nullptr"); return RDB_ERROR; } - std::vector devices; - if (predicates.devices_.empty()) { - devices = NetworkIdToUUID(GetConnectDevices()); - } else { - devices = NetworkIdToUUID(predicates.devices_); + if (option.mode < DistributedData::GeneralStore::NEARBY_END) { + auto &networkIds = predicates.devices_; + auto devices = networkIds.empty() ? NetworkIdToUUID(GetConnectDevices()) : NetworkIdToUUID(networkIds); + return delegate->Sync( + devices, static_cast(option.mode), MakeQuery(predicates), + [async](const std::map> &syncStatus) { + async(HandleSyncStatus(syncStatus)); + }, + option.isAsync); + + } else if (option.mode < DistributedData::GeneralStore::CLOUD_END) { + CloudEvent::StoreInfo storeInfo; + storeInfo.bundleName = GetBundleName(); + storeInfo.user = AccountDelegate::GetInstance()->GetUserByToken(token_); + storeInfo.storeName = GetStoreId(); + storeInfo.tokenId = token_; + auto query = std::make_shared(); + query->query_ = MakeQuery(predicates); + auto info = ChangeEvent::EventInfo(option.mode, (option.isAsync ? 0 : WAIT_TIME), query, + [async](const GenDetails &details) { + async(HandleGenDetails(details)); + }); + auto evt = std::make_unique(std::move(storeInfo), std::move(info)); + EventCenter::GetInstance().PostEvent(std::move(evt)); } - ZLOGI("delegate sync"); - return delegate->Sync( - devices, static_cast(option.mode), MakeQuery(predicates), - [callback](const std::map> &syncStatus) { - SyncResult result; - HandleSyncStatus(syncStatus, result); - callback(result); - }, - false); + return RDB_OK; } int32_t RdbSyncer::RemoteQuery(const std::string& device, const std::string& sql, @@ -413,4 +419,19 @@ int32_t RdbSyncer::RemoteQuery(const std::string& device, const std::string& sql } return RDB_OK; } + +int32_t RdbSyncer::RemoveDeviceData() +{ + auto* delegate = GetDelegate(); + if (delegate == nullptr) { + ZLOGE("delegate is nullptr"); + return RDB_ERROR; + } + DistributedDB::DBStatus status = delegate->RemoveDeviceData(); + if (status != DistributedDB::DBStatus::OK) { + ZLOGE("DistributedDB RemoveDeviceData failed, status is %{public}d.", status); + return RDB_ERROR; + } + return RDB_OK; +} } // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h index 68d1e44f..5571f853 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -18,20 +18,25 @@ #include #include +#include +#include "iremote_object.h" +#include "metadata/secret_key_meta_data.h" #include "metadata/store_meta_data.h" +#include "rdb_service.h" #include "rdb_store_observer_impl.h" #include "rdb_types.h" #include "relational_store_delegate.h" #include "relational_store_manager.h" -#include "metadata/secret_key_meta_data.h" -#include "iremote_object.h" +#include "store/general_value.h" namespace OHOS::DistributedRdb { class RdbSyncer { public: using StoreMetaData = OHOS::DistributedData::StoreMetaData; + using GenDetails = OHOS::DistributedData::GenDetails; using SecretKeyMetaData = DistributedData::SecretKeyMetaData; - RdbSyncer(const RdbSyncerParam& param, RdbStoreObserverImpl* observer); + using Option = DistributedRdb::RdbService::Option; + RdbSyncer(const RdbSyncerParam ¶m, RdbStoreObserverImpl *observer); ~RdbSyncer() noexcept; int32_t Init(pid_t pid, pid_t uid, uint32_t token, const StoreMetaData &meta); @@ -46,22 +51,22 @@ public: std::string GetIdentifier() const; - int32_t SetDistributedTables(const std::vector& tables); + int32_t SetDistributedTables(const std::vector &tables); - int32_t DoSync(const SyncOption& option, const RdbPredicates& predicates, SyncResult& result); + int32_t DoSync(const Option &option, const RdbPredicates &predicates, const AsyncDetail &async); - int32_t DoAsync(const SyncOption& option, const RdbPredicates& predicates, const SyncCallback& callback); + int32_t RemoteQuery(const std::string &device, const std::string &sql, + const std::vector &selectionArgs, sptr &resultSet); - int32_t RemoteQuery(const std::string& device, const std::string& sql, - const std::vector& selectionArgs, sptr& resultSet); + int32_t RemoveDeviceData(); - static std::string RemoveSuffix(const std::string& name); + static std::string RemoveSuffix(const std::string &name); - static int32_t GetInstIndex(uint32_t tokenId, const std::string &bundleName); + static std::pair GetInstIndexAndUser(uint32_t tokenId, const std::string &bundleName); static bool GetPassword(const StoreMetaData &metaData, DistributedDB::CipherPassword &password); - static DistributedDB::Query MakeQuery(const RdbPredicates& predicates); + static DistributedDB::Query MakeQuery(const RdbPredicates &predicates); private: std::string GetUserId() const; @@ -72,31 +77,31 @@ private: int32_t InitDBDelegate(const StoreMetaData &meta); - DistributedDB::RelationalStoreDelegate* GetDelegate(); + DistributedDB::RelationalStoreDelegate *GetDelegate(); std::mutex mutex_; - DistributedDB::RelationalStoreManager* manager_ {}; - DistributedDB::RelationalStoreDelegate* delegate_ {}; + DistributedDB::RelationalStoreManager *manager_{}; + DistributedDB::RelationalStoreDelegate *delegate_{}; RdbSyncerParam param_; - RdbStoreObserverImpl *observer_ {}; - pid_t pid_ {}; - pid_t uid_ {}; - uint32_t token_ {}; - uint64_t timerId_ {}; + RdbStoreObserverImpl *observer_{}; + pid_t pid_{}; + pid_t uid_{}; + uint32_t token_{}; + uint64_t timerId_{}; static std::vector GetConnectDevices(); - static std::vector NetworkIdToUUID(const std::vector& networkIds); - - static void HandleSyncStatus(const std::map>& SyncStatus, - SyncResult& result); - static void EqualTo(const RdbPredicateOperation& operation, DistributedDB::Query& query); - static void NotEqualTo(const RdbPredicateOperation& operation, DistributedDB::Query& query); - static void And(const RdbPredicateOperation& operation, DistributedDB::Query& query); - static void Or(const RdbPredicateOperation& operation, DistributedDB::Query& query); - static void OrderBy(const RdbPredicateOperation& operation, DistributedDB::Query& query); - static void Limit(const RdbPredicateOperation& operation, DistributedDB::Query& query); - - using PredicateHandle = void(*)(const RdbPredicateOperation& operation, DistributedDB::Query& query); + static std::vector NetworkIdToUUID(const std::vector &networkIds); + + static Details HandleSyncStatus(const std::map> &SyncStatus); + static Details HandleGenDetails(const GenDetails &details); + static void EqualTo(const RdbPredicateOperation &operation, DistributedDB::Query &query); + static void NotEqualTo(const RdbPredicateOperation &operation, DistributedDB::Query &query); + static void And(const RdbPredicateOperation &operation, DistributedDB::Query &query); + static void Or(const RdbPredicateOperation &operation, DistributedDB::Query &query); + static void OrderBy(const RdbPredicateOperation &operation, DistributedDB::Query &query); + static void Limit(const RdbPredicateOperation &operation, DistributedDB::Query &query); + + using PredicateHandle = void (*)(const RdbPredicateOperation &operation, DistributedDB::Query &query); static inline PredicateHandle HANDLES[OPERATOR_MAX] = { [EQUAL_TO] = &RdbSyncer::EqualTo, [NOT_EQUAL_TO] = &RdbSyncer::NotEqualTo, @@ -108,6 +113,7 @@ private: static constexpr int DECIMAL_BASE = 10; static constexpr uint64_t REMOTE_QUERY_TIME_OUT = 30 * 1000; + static constexpr int32_t WAIT_TIME = 30 * 1000; }; } // namespace OHOS::DistributedRdb #endif diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_watcher.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_watcher.cpp index 01572668..c929a769 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_watcher.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_watcher.cpp @@ -16,28 +16,43 @@ #define LOG_TAG "RdbWatcher" #include "rdb_watcher.h" -#include "log_print.h" + #include "error/general_error.h" +#include "log_print.h" namespace OHOS::DistributedRdb { using namespace DistributedData; -using Err = DistributedData::GeneralError; -RdbWatcher::RdbWatcher(RdbServiceImpl *rdbService, uint32_t tokenId, const std::string &storeName) - : rdbService_(rdbService), tokenId_(tokenId), storeName_(storeName) +using Error = DistributedData::GeneralError; +RdbWatcher::RdbWatcher() { } -int32_t RdbWatcher::OnChange(Origin origin, const std::string &id) +int32_t RdbWatcher::OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) { - if (rdbService_ == nullptr) { - return Err::E_ERROR; + auto notifier = GetNotifier(); + if (notifier == nullptr) { + return E_NOT_INIT; } - rdbService_->OnChange(tokenId_, storeName_); - return Err::E_OK; + DistributedRdb::Origin rdbOrigin; + rdbOrigin.origin = origin.origin; + rdbOrigin.id = origin.id; + rdbOrigin.store = origin.store; + notifier->OnChange(rdbOrigin, primaryFields, std::move(values)); + return E_OK; } -int32_t RdbWatcher::OnChange(Origin origin, const std::string &id, const std::vector &values) +sptr RdbWatcher::GetNotifier() const { - return Err::E_NOT_SUPPORT; + std::shared_lock lock(mutex_); + return notifier_; +} + +void RdbWatcher::SetNotifier(sptr notifier) +{ + std::unique_lock lock(mutex_); + if (notifier_ == notifier) { + return; + } + notifier_ = notifier; } } // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_watcher.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_watcher.h index 65af0216..ac898854 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_watcher.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_watcher.h @@ -15,23 +15,23 @@ #ifndef OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_WATCHER_H #define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_WATCHER_H -#include "rdb_service_impl.h" +#include +#include +#include "rdb_notifier_proxy.h" #include "store/general_value.h" #include "store/general_watcher.h" namespace OHOS::DistributedRdb { -class RdbServiceImpl; class RdbWatcher : public DistributedData::GeneralWatcher { public: - explicit RdbWatcher(RdbServiceImpl *rdbService, uint32_t tokenId, const std::string &storeName); - int32_t OnChange(Origin origin, const std::string &id) override; - int32_t OnChange(Origin origin, const std::string &id, - const std::vector &values) override; + RdbWatcher(); + int32_t OnChange(const Origin &origin, const PRIFields &primaryFields, ChangeInfo &&values) override; + sptr GetNotifier() const; + void SetNotifier(sptr notifier); private: - RdbServiceImpl* rdbService_ {}; - uint32_t tokenId_ = 0; - std::string storeName_ {}; + mutable std::shared_mutex mutex_; + sptr notifier_; }; } // namespace OHOS::DistributedRdb #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_GENERAL_WATCHER_H diff --git a/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.cpp b/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.cpp index 44c8b92a..7035d9fa 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.cpp @@ -13,8 +13,11 @@ * limitations under the License. */ +#define LOG_TAG "ValueProxy" +#include "log_print.h" #include "value_proxy.h" namespace OHOS::DistributedRdb { +using namespace OHOS::DistributedData; ValueProxy::Value ValueProxy::Convert(DistributedData::Value &&value) { Value proxy; @@ -29,6 +32,13 @@ ValueProxy::Value ValueProxy::Convert(NativeRdb::ValueObject &&value) return proxy; } +ValueProxy::Value ValueProxy::Convert(DistributedDB::Type &&value) +{ + Value proxy; + DistributedData::Convert(std::move(value), proxy.value_); + return proxy; +} + ValueProxy::Values ValueProxy::Convert(DistributedData::Values &&values) { Values proxy; @@ -67,6 +77,15 @@ ValueProxy::Bucket ValueProxy::Convert(NativeRdb::ValuesBucket &&bucket) return proxy; } +ValueProxy::Bucket ValueProxy::Convert(DistributedDB::VBucket &&bucket) +{ + ValueProxy::Bucket proxy; + for (auto &[key, value] : bucket) { + proxy.value_.insert_or_assign(key, Convert(std::move(value))); + } + return proxy; +} + ValueProxy::Buckets ValueProxy::Convert(std::vector &&buckets) { ValueProxy::Buckets proxy; @@ -77,6 +96,16 @@ ValueProxy::Buckets ValueProxy::Convert(std::vector &&b 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; @@ -94,7 +123,18 @@ ValueProxy::Asset::Asset(DistributedData::Asset asset) ValueProxy::Asset::Asset(NativeRdb::AssetValue asset) { - asset_ = DistributedData::Asset{ .version = asset.version, + 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::Asset(DistributedDB::Asset asset) +{ + asset_ = DistributedData::Asset { .version = asset.version, .name = std::move(asset.name), .uri = std::move(asset.uri), .createTime = std::move(asset.createTime), @@ -123,7 +163,7 @@ ValueProxy::Asset &ValueProxy::Asset::operator=(Asset &&proxy) noexcept ValueProxy::Asset::operator NativeRdb::AssetValue() { - return NativeRdb::AssetValue{ .version = asset_.version, + return NativeRdb::AssetValue { .version = asset_.version, .name = std::move(asset_.name), .uri = std::move(asset_.uri), .createTime = std::move(asset_.createTime), @@ -137,6 +177,17 @@ ValueProxy::Asset::operator DistributedData::Asset() return std::move(asset_); } +ValueProxy::Asset::operator DistributedDB::Asset() +{ + return DistributedDB::Asset { .version = asset_.version, + .name = std::move(asset_.name), + .uri = std::move(asset_.uri), + .modifyTime = std::move(asset_.modifyTime), + .createTime = std::move(asset_.createTime), + .size = std::move(asset_.size), + .hash = std::move(asset_.hash) }; +} + ValueProxy::Assets::Assets(DistributedData::Assets assets) { assets_.clear(); @@ -155,6 +206,15 @@ ValueProxy::Assets::Assets(NativeRdb::ValueObject::Assets assets) } } +ValueProxy::Assets::Assets(DistributedDB::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) { @@ -193,6 +253,16 @@ ValueProxy::Assets::operator DistributedData::Assets() return assets; } +ValueProxy::Assets::operator DistributedDB::Assets() +{ + DistributedDB::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) { @@ -216,6 +286,13 @@ ValueProxy::Value::operator DistributedData::Value() return value; } +ValueProxy::Value::operator DistributedDB::Type() +{ + DistributedDB::Type value; + DistributedData::Convert(std::move(value_), value); + return value; +} + ValueProxy::Values &ValueProxy::Values::operator=(ValueProxy::Values &&values) noexcept { if (this == &values) { diff --git a/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.h b/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.h index a22c6e4e..3b9eeecf 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/value_proxy.h @@ -16,6 +16,7 @@ #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 "cloud/cloud_store_types.h" #include "store/general_value.h" #include "value_object.h" #include "values_bucket.h" @@ -36,10 +37,12 @@ public: }; Asset(DistributedData::Asset asset); Asset(NativeRdb::AssetValue asset); + Asset(DistributedDB::Asset asset); Asset &operator=(const Asset &proxy); Asset &operator=(Asset &&proxy) noexcept; operator NativeRdb::AssetValue(); operator DistributedData::Asset(); + operator DistributedDB::Asset(); private: DistributedData::Asset asset_; @@ -58,10 +61,12 @@ public: }; Assets(DistributedData::Assets assets); Assets(NativeRdb::ValueObject::Assets assets); + Assets(DistributedDB::Assets assets); Assets &operator=(const Assets &proxy); Assets &operator=(Assets &&proxy) noexcept; operator NativeRdb::ValueObject::Assets(); operator DistributedData::Assets(); + operator DistributedDB::Assets(); private: std::vector assets_; @@ -78,6 +83,7 @@ public: Value &operator=(Value &&value) noexcept; operator NativeRdb::ValueObject(); operator DistributedData::Value(); + operator DistributedDB::Type(); private: friend ValueProxy; @@ -164,6 +170,9 @@ public: static Buckets Convert(DistributedData::VBuckets &&buckets); static Buckets Convert(std::vector &&buckets); + static Value Convert(DistributedDB::Type &&value); + static Bucket Convert(DistributedDB::VBucket &&bucket); + static Buckets Convert(std::vector &&buckets); private: ValueProxy() = delete; ~ValueProxy() = delete; diff --git a/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp b/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp index 32057e30..ae18fbe8 100644 --- a/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -80,7 +80,7 @@ CloudInfo CloudServerMock::GetServerInfo(int32_t userId) appInfo.version = 1; appInfo.cloudSwitch = true; - cloudInfo.apps.emplace_back(std::move(appInfo)); + cloudInfo.apps[TEST_CLOUD_BUNDLE] = std::move(appInfo); return cloudInfo; } @@ -186,7 +186,7 @@ HWTEST_F(CloudDataTest, GetSchema, TestSize.Level0) ASSERT_FALSE( MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), schemaMeta, true)); CloudEvent::StoreInfo storeInfo { OHOS::IPCSkeleton::GetCallingTokenID(), TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, 0 }; - auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "test_service"); + auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo)); EventCenter::GetInstance().PostEvent(move(event)); ASSERT_TRUE( MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), schemaMeta, true)); diff --git a/kv_store/bundle.json b/kv_store/bundle.json index d0648947..af8614b8 100644 --- a/kv_store/bundle.json +++ b/kv_store/bundle.json @@ -1,8 +1,8 @@ { - "name": "@openharmony/distributeddatamgr_kv_store", - "version": "1.0.0", + "name": "@ohos/kv_store", + "version": "3.1.0", "license": "Apache License 2.0", - "description": "", + "description": "Supports distributed key-value and document-based data management, and supports the use of schemas to describe data formats", "domain": "os", "language": "", "publishAs": "code-segment", @@ -44,8 +44,8 @@ "adapted_system_type": [ "standard" ], - "rom": "", - "ram": "", + "rom": "14336KB", + "ram": "15360KB", "hisysevent_config": [], "deps": { "thrid_party": [ diff --git a/kv_store/frameworks/CMakeLists.txt b/kv_store/frameworks/CMakeLists.txt index c58a251a..d15111c7 100644 --- a/kv_store/frameworks/CMakeLists.txt +++ b/kv_store/frameworks/CMakeLists.txt @@ -12,27 +12,13 @@ set(MOCK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../mock) add_definitions(-DNAPI_EXPERIMENTAL) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/innerkitsimpl/distributeddatafwk/src kvdbSrc) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/innerkitsimpl/kvdb/src kvdbSrc) -aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/innerkitsimpl/object/src kvdbSrc) -aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/jskitsimpl/distributedkvstore/src kvdbSrc) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/jskitsimpl/distributedkvstore/include) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/jskitsimpl/distributedkvstore/src jsKVSrc) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/jskitsimpl/distributeddata/src jsDataSrc) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/innerkitsimpl/distributeddatafwk/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/innerkitsimpl/distributeddatafwk/src) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/innerkitsimpl/kvdb/include) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/innerkitsimpl/object/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/common) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../interfaces/innerkits/distributeddata/include) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../interfaces/innerkits) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../datamgr_service/services/distributeddataservice/adapter/include) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../datamgr_service/services/distributeddataservice/adapter/include/account) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../datamgr_service/services/distributeddataservice/adapter/include/autils) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../datamgr_service/services/distributeddataservice/adapter/include/broadcaster) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../datamgr_service/services/distributeddataservice/adapter/include/communicator) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../datamgr_service/services/distributeddataservice/adapter/include/dfx) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../datamgr_service/services/distributeddataservice/adapter/include/log) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../datamgr_service/services/distributeddataservice/adapter/include/permission) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../datamgr_service/services/distributeddataservice/adapter/include/security) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../datamgr_service/services/distributeddataservice/adapter/include/utils) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../datamgr_service/services/distributeddataservice/service/crypto/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../frameworks/libs/distributeddb/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../frameworks/libs/distributeddb/interfaces/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../frameworks/libs/distributeddb/interfaces/include/relational) @@ -44,4 +30,12 @@ include(${MOCK_DIR}/include/CMakeLists.txt OPTIONAL) set(links secure mock distributeddb) add_library(kvdb SHARED ${kvdbSrc}) -target_link_libraries(kvdb ${links}) \ No newline at end of file +add_library(jskvdb SHARED ${jsKVSrc}) +add_library(jsolddb SHARED ${jsDataSrc}) +target_link_libraries(kvdb ${links}) +target_link_libraries(jskvdb ${links} kvdb) +target_link_libraries(jsolddb ${links} kvdb) +target_include_directories(jskvdb PRIVATE + "${CMAKE_CURRENT_SOURCE_DIR}/jskitsimpl/distributedkvstore/include") +target_include_directories(jsolddb PRIVATE + "${CMAKE_CURRENT_SOURCE_DIR}/jskitsimpl/distributeddata/include") \ No newline at end of file diff --git a/kv_store/frameworks/common/executor.h b/kv_store/frameworks/common/executor.h index 69db7bce..3e8649ff 100644 --- a/kv_store/frameworks/common/executor.h +++ b/kv_store/frameworks/common/executor.h @@ -74,7 +74,7 @@ public: condition_.notify_one(); } - void Stop(bool wait = false) + void Stop(bool wait = false) noexcept { std::unique_lock lock(mutex_); running_ = IS_STOPPING; diff --git a/kv_store/frameworks/common/lru_bucket.h b/kv_store/frameworks/common/lru_bucket.h index 1fac6f15..ff443785 100644 --- a/kv_store/frameworks/common/lru_bucket.h +++ b/kv_store/frameworks/common/lru_bucket.h @@ -63,15 +63,17 @@ public: /** * The time complexity is O(log(index size)) **/ - bool Get(const _Key &key, _Tp &value) + bool Get(const _Key &key, _Tp &value, bool isLRU = true) { std::lock_guard lock(mutex_); auto it = indexes_.find(key); if (it != indexes_.end()) { - // move node from the list; - Remove(it->second); - // insert node to the head - Insert(&head_, it->second); + if (isLRU) { + // move node from the list; + Remove(it->second); + // insert node to the head + Insert(&head_, it->second); + } value = it->second->value_; return true; } diff --git a/kv_store/frameworks/common/pool.h b/kv_store/frameworks/common/pool.h index 5db3e1f6..1022514a 100644 --- a/kv_store/frameworks/common/pool.h +++ b/kv_store/frameworks/common/pool.h @@ -86,7 +86,7 @@ public: idle_ = cur; } - int32_t Clean(std::function)> close) + int32_t Clean(std::function)> close) noexcept { auto temp = min_; min_ = 0; diff --git a/kv_store/frameworks/common/priority_queue.h b/kv_store/frameworks/common/priority_queue.h index 97b71aea..0c2cbce5 100644 --- a/kv_store/frameworks/common/priority_queue.h +++ b/kv_store/frameworks/common/priority_queue.h @@ -44,9 +44,9 @@ public: { std::unique_lock lock(pqMtx_); while (!tasks_.empty()) { - auto time = tasks_.begin()->first; - if (time > std::chrono::steady_clock::now()) { - popCv_.wait_until(lock, time); + auto waitTme = tasks_.begin()->first; + if (waitTme > std::chrono::steady_clock::now()) { + popCv_.wait_until(lock, waitTme); continue; } auto temp = tasks_.begin(); diff --git a/kv_store/frameworks/common/traits.h b/kv_store/frameworks/common/traits.h index 26c74730..14d331e3 100644 --- a/kv_store/frameworks/common/traits.h +++ b/kv_store/frameworks/common/traits.h @@ -98,7 +98,8 @@ std::enable_if_t, const T *> get_if(const std::variant(input); } -template ? convertible_index_of_v : 0> +template ? +convertible_index_of_v : 0> constexpr std::enable_if_t && convertible_in_v, std::add_pointer_t>>> get_if(std::variant *input) diff --git a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h index b67e248d..02c85db4 100644 --- a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h +++ b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h @@ -29,16 +29,7 @@ class IKvStoreDataService : public IRemoteBroker { public: enum { GET_FEATURE_INTERFACE, - REGISTERCLIENTDEATHOBSERVER, - CLOSEKVSTORE, - CLOSEALLKVSTORE, - DELETEKVSTORE, - DELETEALLKVSTORE, - GETSINGLEKVSTORE, - GETLOCALDEVICE, - GETREMOTEDEVICES, - STARTWATCHDEVICECHANGE, - STOPWATCHDEVICECHANGE, + REGISTER_DEATH_OBSERVER, SERVICE_CMD_LAST, }; @@ -46,7 +37,7 @@ public: virtual sptr GetFeatureInterface(const std::string &name) = 0; - virtual Status RegisterClientDeathObserver(const AppId &appId, sptr observer) = 0; + virtual int32_t RegisterDeathObserver(const std::string &bundleName, sptr observer) = 0; protected: static constexpr size_t MAX_IPC_CAPACITY = 800 * 1024; @@ -58,23 +49,13 @@ public: MessageParcel &reply, MessageOption &option) override; private: - int32_t NoSupport(MessageParcel &data, MessageParcel &reply); - int32_t GetFeatureInterfaceOnRemote(MessageParcel &data, MessageParcel &reply); - int32_t RegisterClientDeathObserverOnRemote(MessageParcel &data, MessageParcel &reply); + int32_t OnGetFeatureInterfaceRequest(MessageParcel &data, MessageParcel &reply); + int32_t OnRegisterDeathObserverRequest(MessageParcel &data, MessageParcel &reply); using RequestHandler = int32_t(KvStoreDataServiceStub::*)(MessageParcel&, MessageParcel&); static constexpr RequestHandler HANDLERS[SERVICE_CMD_LAST] = { - [GET_FEATURE_INTERFACE] = &KvStoreDataServiceStub::GetFeatureInterfaceOnRemote, - [REGISTERCLIENTDEATHOBSERVER] = &KvStoreDataServiceStub::RegisterClientDeathObserverOnRemote, - [CLOSEKVSTORE] = &KvStoreDataServiceStub::NoSupport, - [CLOSEALLKVSTORE] = &KvStoreDataServiceStub::NoSupport, - [DELETEKVSTORE] = &KvStoreDataServiceStub::NoSupport, - [DELETEALLKVSTORE] = &KvStoreDataServiceStub::NoSupport, - [GETSINGLEKVSTORE] = &KvStoreDataServiceStub::NoSupport, - [GETLOCALDEVICE] = &KvStoreDataServiceStub::NoSupport, - [GETREMOTEDEVICES] = &KvStoreDataServiceStub::NoSupport, - [STARTWATCHDEVICECHANGE] = &KvStoreDataServiceStub::NoSupport, - [STOPWATCHDEVICECHANGE] = &KvStoreDataServiceStub::NoSupport, + [GET_FEATURE_INTERFACE] = &KvStoreDataServiceStub::OnGetFeatureInterfaceRequest, + [REGISTER_DEATH_OBSERVER] = &KvStoreDataServiceStub::OnRegisterDeathObserverRequest, }; }; @@ -84,7 +65,7 @@ public: ~KvStoreDataServiceProxy() = default; sptr GetFeatureInterface(const std::string &name) override; - Status RegisterClientDeathObserver(const AppId &appId, sptr observer) override; + int32_t RegisterDeathObserver(const std::string &bundleName, sptr observer) override; private: static inline BrokerDelegator delegator_; diff --git a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp index 5e9f0ec5..eb456ad2 100644 --- a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp +++ b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp @@ -61,58 +61,48 @@ sptr KvStoreDataServiceProxy::GetFeatureInterface(const std::stri return remoteObject; } -Status KvStoreDataServiceProxy::RegisterClientDeathObserver(const AppId &appId, sptr observer) +int32_t KvStoreDataServiceProxy::RegisterDeathObserver(const std::string &bundleName, sptr observer) { + if (observer == nullptr) { + return Status::INVALID_ARGUMENT; + } + MessageParcel data; MessageParcel reply; if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) { ZLOGE("write descriptor failed"); return Status::IPC_ERROR; } - if (!data.WriteString(appId.appId)) { - ZLOGW("failed to write string."); + if (!ITypesUtil::Marshal(data, bundleName, observer)) { + ZLOGE("Marshal observer failed"); return Status::IPC_ERROR; } - if (observer != nullptr) { - if (!data.WriteRemoteObject(observer)) { - ZLOGW("failed to write parcel."); - return Status::IPC_ERROR; - } - } else { - return Status::INVALID_ARGUMENT; - } MessageOption mo { MessageOption::TF_SYNC }; - int32_t error = Remote()->SendRequest(REGISTERCLIENTDEATHOBSERVER, data, reply, mo); + int32_t error = Remote()->SendRequest(REGISTER_DEATH_OBSERVER, data, reply, mo); if (error != 0) { ZLOGW("failed during IPC. errCode %d", error); return Status::IPC_ERROR; } - return static_cast(reply.ReadInt32()); -} - -int32_t KvStoreDataServiceStub::NoSupport(MessageParcel &data, MessageParcel &reply) -{ - (void)data; - (void)reply; - return NOT_SUPPORT; + return reply.ReadInt32(); } -int32_t KvStoreDataServiceStub::RegisterClientDeathObserverOnRemote(MessageParcel &data, MessageParcel &reply) +int32_t KvStoreDataServiceStub::OnRegisterDeathObserverRequest(MessageParcel &data, MessageParcel &reply) { - AppId appId = { data.ReadString() }; - sptr kvStoreClientDeathObserverProxy = data.ReadRemoteObject(); - if (kvStoreClientDeathObserverProxy == nullptr) { + std::string bundleName; + sptr observer; + if (!ITypesUtil::Unmarshal(data, bundleName, observer)) { + ZLOGE("Unmarshal observer failed"); return -1; } - Status status = RegisterClientDeathObserver(appId, std::move(kvStoreClientDeathObserverProxy)); + int32_t status = RegisterDeathObserver(bundleName, std::move(observer)); if (!reply.WriteInt32(static_cast(status))) { return -1; } return 0; } -int32_t KvStoreDataServiceStub::GetFeatureInterfaceOnRemote(MessageParcel &data, MessageParcel &reply) +int32_t KvStoreDataServiceStub::OnGetFeatureInterfaceRequest(MessageParcel &data, MessageParcel &reply) { std::string name; if (!ITypesUtil::Unmarshal(data, name)) { diff --git a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_service_death_notifier.cpp b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_service_death_notifier.cpp index 57122172..e3dabb3a 100644 --- a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_service_death_notifier.cpp +++ b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_service_death_notifier.cpp @@ -27,32 +27,55 @@ namespace OHOS { namespace DistributedKv { -AppId KvStoreServiceDeathNotifier::appId_; -std::mutex KvStoreServiceDeathNotifier::mutex_; -std::mutex KvStoreServiceDeathNotifier::watchMutex_; -sptr KvStoreServiceDeathNotifier::kvDataServiceProxy_; -sptr KvStoreServiceDeathNotifier::deathRecipientPtr_; -sptr KvStoreServiceDeathNotifier::clientDeathObserverPtr_; -std::set> KvStoreServiceDeathNotifier::serviceDeathWatchers_; + +std::mutex KvStoreServiceDeathNotifier::instanceMutex_; +KvStoreServiceDeathNotifier* KvStoreServiceDeathNotifier::instance_ = nullptr; + +KvStoreServiceDeathNotifier* KvStoreServiceDeathNotifier::GetInstance() +{ + if (instance_ == nullptr) { + std::lock_guard lock(instanceMutex_); + if (instance_ == nullptr) { + instance_ = new (std::nothrow) KvStoreServiceDeathNotifier(); + if (instance_ == nullptr) { + ZLOGE("KvStoreServiceDeathNotifier nullptr"); + } + return instance_; + } + } + return instance_; +} void KvStoreServiceDeathNotifier::SetAppId(const AppId &appId) { - std::lock_guard lg(mutex_); - appId_ = appId; + auto *instance = GetInstance(); + if (instance == nullptr) { + return; + } + std::lock_guard lg(instance->mutex_); + instance->appId_ = appId; } AppId KvStoreServiceDeathNotifier::GetAppId() { - std::lock_guard lg(mutex_); - return appId_; + auto *instance = GetInstance(); + if (instance == nullptr) { + return {}; + } + std::lock_guard lg(instance->mutex_); + return instance->appId_; } sptr KvStoreServiceDeathNotifier::GetDistributedKvDataService() { ZLOGD("begin."); - std::lock_guard lg(watchMutex_); - if (kvDataServiceProxy_ != nullptr) { - return kvDataServiceProxy_; + auto *instance = GetInstance(); + if (instance == nullptr) { + return nullptr; + } + std::lock_guard lg(instance->watchMutex_); + if (instance->kvDataServiceProxy_ != nullptr) { + return instance->kvDataServiceProxy_; } ZLOGI("create remote proxy."); @@ -63,26 +86,26 @@ sptr KvStoreServiceDeathNotifier::GetDistributedKvDataServi } auto remote = samgr->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID); - kvDataServiceProxy_ = iface_cast(remote); - if (kvDataServiceProxy_ == nullptr) { + instance->kvDataServiceProxy_ = iface_cast(remote); + if (instance->kvDataServiceProxy_ == nullptr) { ZLOGE("initialize proxy failed."); return nullptr; } - if (deathRecipientPtr_ == nullptr) { - deathRecipientPtr_ = new (std::nothrow) ServiceDeathRecipient(); - if (deathRecipientPtr_ == nullptr) { + if (instance->deathRecipientPtr_ == nullptr) { + instance->deathRecipientPtr_ = new (std::nothrow) ServiceDeathRecipient(); + if (instance->deathRecipientPtr_ == nullptr) { ZLOGW("new KvStoreDeathRecipient failed"); return nullptr; } } - if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipientPtr_))) { + if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(instance->deathRecipientPtr_))) { ZLOGE("failed to add death recipient."); } - RegisterClientDeathObserver(); + instance->RegisterClientDeathObserver(); - return kvDataServiceProxy_; + return instance->kvDataServiceProxy_; } void KvStoreServiceDeathNotifier::RegisterClientDeathObserver() @@ -97,40 +120,52 @@ void KvStoreServiceDeathNotifier::RegisterClientDeathObserver() ZLOGW("new KvStoreClientDeathObserver failed"); return; } - kvDataServiceProxy_->RegisterClientDeathObserver(GetAppId(), clientDeathObserverPtr_); + kvDataServiceProxy_->RegisterDeathObserver(GetAppId(), clientDeathObserverPtr_); } void KvStoreServiceDeathNotifier::AddServiceDeathWatcher(std::shared_ptr watcher) { - std::lock_guard lg(watchMutex_); - auto ret = serviceDeathWatchers_.insert(std::move(watcher)); + auto *instance = GetInstance(); + if (instance == nullptr) { + return; + } + std::lock_guard lg(instance->watchMutex_); + auto ret = instance->serviceDeathWatchers_.insert(std::move(watcher)); if (ret.second) { - ZLOGI("success set size: %zu", serviceDeathWatchers_.size()); + ZLOGI("success set size: %zu", instance->serviceDeathWatchers_.size()); } else { - ZLOGE("failed set size: %zu", serviceDeathWatchers_.size()); + ZLOGE("failed set size: %zu", instance->serviceDeathWatchers_.size()); } } void KvStoreServiceDeathNotifier::RemoveServiceDeathWatcher(std::shared_ptr watcher) { - std::lock_guard lg(watchMutex_); - auto it = serviceDeathWatchers_.find(std::move(watcher)); - if (it != serviceDeathWatchers_.end()) { - serviceDeathWatchers_.erase(it); - ZLOGI("find & erase set size: %zu", serviceDeathWatchers_.size()); + auto *instance = GetInstance(); + if (instance == nullptr) { + return; + } + std::lock_guard lg(instance->watchMutex_); + auto it = instance->serviceDeathWatchers_.find(std::move(watcher)); + if (it != instance->serviceDeathWatchers_.end()) { + instance->serviceDeathWatchers_.erase(it); + ZLOGI("find & erase set size: %zu", instance->serviceDeathWatchers_.size()); } else { - ZLOGE("no found set size: %zu", serviceDeathWatchers_.size()); + ZLOGE("no found set size: %zu", instance->serviceDeathWatchers_.size()); } } void KvStoreServiceDeathNotifier::ServiceDeathRecipient::OnRemoteDied(const wptr &remote) { ZLOGW("DistributedDataMgrService died."); + auto *instance = GetInstance(); + if (instance == nullptr) { + return; + } // Need to do this with the lock held - std::lock_guard lg(watchMutex_); - kvDataServiceProxy_ = nullptr; - ZLOGI("watcher set size: %zu", serviceDeathWatchers_.size()); - for (const auto &watcher : serviceDeathWatchers_) { + std::lock_guard lg(instance->watchMutex_); + instance->kvDataServiceProxy_ = nullptr; + ZLOGI("watcher set size: %zu", instance->serviceDeathWatchers_.size()); + for (const auto &watcher : instance->serviceDeathWatchers_) { if (watcher == nullptr) { ZLOGI("watcher is nullptr"); continue; diff --git a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_service_death_notifier.h b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_service_death_notifier.h index 7f5d3ae0..0090d736 100644 --- a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_service_death_notifier.h +++ b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_service_death_notifier.h @@ -28,8 +28,6 @@ namespace OHOS { namespace DistributedKv { class KvStoreServiceDeathNotifier final { public: - KvStoreServiceDeathNotifier() = delete; - ~KvStoreServiceDeathNotifier() = delete; // get DistributedKvDataService proxy object. static sptr GetDistributedKvDataService(); // temporarily used, should get in service side from binder. @@ -41,6 +39,8 @@ public: static void RemoveServiceDeathWatcher(std::shared_ptr watcher); private: + KvStoreServiceDeathNotifier() = default; + ~KvStoreServiceDeathNotifier() = default; class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { public: ServiceDeathRecipient(); @@ -49,18 +49,22 @@ private: void OnRemoteDied(const wptr &remote) override; }; + static KvStoreServiceDeathNotifier* GetInstance(); + + static std::mutex instanceMutex_; + static KvStoreServiceDeathNotifier *instance_; // add watcher for server die msg. - static void RegisterClientDeathObserver(); - static AppId appId_; + void RegisterClientDeathObserver(); + AppId appId_; // lock for kvDataServiceProxy_ and serviceDeathWatchers_. - static std::mutex watchMutex_; - static std::mutex mutex_; - static sptr kvDataServiceProxy_; - static sptr deathRecipientPtr_; - static sptr clientDeathObserverPtr_; + std::mutex watchMutex_; + std::mutex mutex_; + sptr kvDataServiceProxy_; + sptr deathRecipientPtr_; + sptr clientDeathObserverPtr_; // set of watchers for server die msg. - static std::set> serviceDeathWatchers_; + std::set> serviceDeathWatchers_; }; } // namespace DistributedKv } // namespace OHOS diff --git a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/device_kvstore_test.cpp b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/device_kvstore_test.cpp index 5d7e5e71..4f9a8732 100644 --- a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/device_kvstore_test.cpp +++ b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/device_kvstore_test.cpp @@ -117,40 +117,20 @@ public: uint64_t GetCallCount() const; private: - uint64_t callCount_; + uint64_t callCount_ = 0; }; void DeviceObserverTestImpl::OnChange(const ChangeNotification &changeNotification) { callCount_++; - const auto &insert = changeNotification.GetInsertEntries(); - insertEntries_.clear(); - for (const auto &entry : insert) { - insertEntries_.push_back(entry); - } - - const auto &update = changeNotification.GetUpdateEntries(); - updateEntries_.clear(); - for (const auto &entry : update) { - updateEntries_.push_back(entry); - } - - const auto &del = changeNotification.GetDeleteEntries(); - deleteEntries_.clear(); - for (const auto &entry : del) { - deleteEntries_.push_back(entry); - } - + insertEntries_ = changeNotification.GetInsertEntries(); + updateEntries_ = changeNotification.GetUpdateEntries(); + deleteEntries_ = changeNotification.GetDeleteEntries(); isClear_ = changeNotification.IsClear(); } DeviceObserverTestImpl::DeviceObserverTestImpl() { - callCount_ = 0; - insertEntries_ = {}; - updateEntries_ = {}; - deleteEntries_ = {}; - isClear_ = false; } void DeviceObserverTestImpl::ResetToZero() diff --git a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_device_store_test.cpp b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_device_store_test.cpp index 50b0462a..436a95a7 100644 --- a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_device_store_test.cpp +++ b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_device_store_test.cpp @@ -85,7 +85,7 @@ public: std::vector updateEntries_; std::vector deleteEntries_; std::string deviceId_; - bool isClear_; + bool isClear_ = false; DeviceObserverTest(); ~DeviceObserverTest() = default; @@ -104,11 +104,6 @@ private: DeviceObserverTest::DeviceObserverTest() { - callCount_ = 0; - insertEntries_ = {}; - updateEntries_ = {}; - deleteEntries_ = {}; - isClear_ = false; } void DeviceObserverTest::OnChange(const ChangeNotification &changeNotification) diff --git a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_store_test.cpp b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_store_test.cpp index bc0d3a72..27b75941 100644 --- a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_store_test.cpp +++ b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/local_subscribe_store_test.cpp @@ -89,7 +89,7 @@ public: std::vector insertEntries_; std::vector updateEntries_; std::vector deleteEntries_; - bool isClear_; + bool isClear_ = false; KvStoreObserverUnitTest(); ~KvStoreObserverUnitTest() {} @@ -114,11 +114,6 @@ private: KvStoreObserverUnitTest::KvStoreObserverUnitTest() { - callCount_ = 0; - insertEntries_ = {}; - updateEntries_ = {}; - deleteEntries_ = {}; - isClear_ = false; } void KvStoreObserverUnitTest::OnChange(const ChangeNotification &changeNotification) diff --git a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp index fb456e02..7c9add2f 100644 --- a/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp +++ b/kv_store/frameworks/innerkitsimpl/distributeddatafwk/test/unittest/single_kvstore_client_test.cpp @@ -101,40 +101,20 @@ public: uint64_t GetCallCount() const; private: - uint64_t callCount_; + uint64_t callCount_ = 0; }; void KvStoreObserverTestImpl::OnChange(const ChangeNotification &changeNotification) { callCount_++; - const auto &insert = changeNotification.GetInsertEntries(); - insertEntries_.clear(); - for (const auto &entry : insert) { - insertEntries_.push_back(entry); - } - - const auto &update = changeNotification.GetUpdateEntries(); - updateEntries_.clear(); - for (const auto &entry : update) { - updateEntries_.push_back(entry); - } - - const auto &del = changeNotification.GetDeleteEntries(); - deleteEntries_.clear(); - for (const auto &entry : del) { - deleteEntries_.push_back(entry); - } - + insertEntries_ = changeNotification.GetInsertEntries(); + updateEntries_ = changeNotification.GetUpdateEntries(); + deleteEntries_ = changeNotification.GetDeleteEntries(); isClear_ = changeNotification.IsClear(); } KvStoreObserverTestImpl::KvStoreObserverTestImpl() { - callCount_ = 0; - insertEntries_ = {}; - updateEntries_ = {}; - deleteEntries_ = {}; - isClear_ = false; } void KvStoreObserverTestImpl::ResetToZero() diff --git a/kv_store/frameworks/innerkitsimpl/kvdb/include/dev_manager.h b/kv_store/frameworks/innerkitsimpl/kvdb/include/dev_manager.h index 30b18d33..c5862aab 100644 --- a/kv_store/frameworks/innerkitsimpl/kvdb/include/dev_manager.h +++ b/kv_store/frameworks/innerkitsimpl/kvdb/include/dev_manager.h @@ -33,26 +33,11 @@ public: std::string ToNetworkId(const std::string &uuid); const DetailInfo &GetLocalDevice(); std::vector GetRemoteDevices(); - class Observer { - public: - Observer() = default; - virtual ~Observer() {} - virtual void Online(const std::string &networkId) = 0; - virtual void Offline(const std::string &networkId) = 0; - }; - - void Register(Observer *observer); - void Unregister(Observer *observer); private: - friend class DMStateCallback; friend class DmDeathCallback; DevManager(const std::string &pkgName); ~DevManager() = default; - void Online(const std::string &networkId); - void Offline(const std::string &networkId); - void OnChanged(const std::string &networkId); - void OnReady(const std::string &networkId); void RegisterDevCallback(); void UpdateBucket(); DetailInfo GetDevInfoFromBucket(const std::string &id); @@ -64,7 +49,6 @@ private: DetailInfo localInfo_ {}; mutable std::mutex mutex_ {}; mutable LRUBucket deviceInfos_ {64}; - ConcurrentMap observers_; }; } // namespace OHOS::DistributedKv #endif // OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_DEV_MANAGER_H diff --git a/kv_store/frameworks/innerkitsimpl/kvdb/include/single_store_impl.h b/kv_store/frameworks/innerkitsimpl/kvdb/include/single_store_impl.h index 958888cf..772fcbe7 100644 --- a/kv_store/frameworks/innerkitsimpl/kvdb/include/single_store_impl.h +++ b/kv_store/frameworks/innerkitsimpl/kvdb/include/single_store_impl.h @@ -30,7 +30,6 @@ namespace OHOS::DistributedKv { class SingleStoreImpl : public SingleKvStore, - public DevManager::Observer, public KvStoreDeathRecipient { public: using Observer = KvStoreObserver; @@ -69,8 +68,6 @@ public: Status Restore(const std::string &file, const std::string &baseDir) override; Status DeleteBackup(const std::vector &files, const std::string &baseDir, std::map &status) override; - void Online(const std::string &device) override; - void Offline(const std::string &device) override; void OnRemoteDied() override; // normal function @@ -108,8 +105,6 @@ private: bool autoSync_ = false; int32_t ref_ = 1; - uint32_t interval_ = 0; - TimePoint expiration_; mutable std::shared_mutex rwMutex_; const Convertor &convertor_; std::string appId_; diff --git a/kv_store/frameworks/innerkitsimpl/kvdb/src/backup_manager.cpp b/kv_store/frameworks/innerkitsimpl/kvdb/src/backup_manager.cpp index ad72b1a2..5a358ad6 100644 --- a/kv_store/frameworks/innerkitsimpl/kvdb/src/backup_manager.cpp +++ b/kv_store/frameworks/innerkitsimpl/kvdb/src/backup_manager.cpp @@ -162,12 +162,12 @@ StoreUtil::FileInfo BackupManager::GetBackupFileInfo( time_t modifyTime = 0; for (auto &file : files) { if (file.name == backupName) { - backupFile = file; + backupFile = std::move(file); break; } if ((file.modifyTime > modifyTime) && (file.size != 0)) { modifyTime = file.modifyTime; - backupFile = file; + backupFile = std::move(file); } } return backupFile; @@ -271,7 +271,7 @@ std::string BackupManager::GetBackupName(const std::string &fileName) void BackupManager::SetResidueInfo(BackupManager::ResidueInfo &residueInfo, const std::vector &files, const std::string &name, const std::string &postFix) { - for (auto &file : files) { + for (const auto &file : files) { auto fullName = name + postFix; auto fullTmpName = fullName + BACKUP_TMP_POSTFIX; if ((file.name == fullTmpName) && (postFix == BACKUP_POSTFIX)) { diff --git a/kv_store/frameworks/innerkitsimpl/kvdb/src/dev_manager.cpp b/kv_store/frameworks/innerkitsimpl/kvdb/src/dev_manager.cpp index 419ca854..58620ed0 100644 --- a/kv_store/frameworks/innerkitsimpl/kvdb/src/dev_manager.cpp +++ b/kv_store/frameworks/innerkitsimpl/kvdb/src/dev_manager.cpp @@ -29,38 +29,6 @@ constexpr int32_t DM_OK = 0; constexpr int32_t DM_ERROR = -1; constexpr size_t DevManager::MAX_ID_LEN; constexpr const char *PKG_NAME_EX = "_distributed_data"; -class DMStateCallback : public DeviceStateCallback { -public: - explicit DMStateCallback(DevManager &devManager) : devManager_(devManager){}; - void OnDeviceOnline(const DmDeviceInfo &deviceInfo) override; - void OnDeviceOffline(const DmDeviceInfo &deviceInfo) override; - void OnDeviceChanged(const DmDeviceInfo &deviceInfo) override; - void OnDeviceReady(const DmDeviceInfo &deviceInfo) override; - -private: - DevManager &devManager_; -}; - -void DMStateCallback::OnDeviceOnline(const DmDeviceInfo &deviceInfo) -{ - devManager_.Online(deviceInfo.networkId); -} - -void DMStateCallback::OnDeviceOffline(const DmDeviceInfo &deviceInfo) -{ - devManager_.Offline(deviceInfo.networkId); -} - -void DMStateCallback::OnDeviceChanged(const DmDeviceInfo &deviceInfo) -{ - devManager_.OnChanged(deviceInfo.networkId); -} - -void DMStateCallback::OnDeviceReady(const DmDeviceInfo &deviceInfo) -{ - devManager_.OnReady(deviceInfo.networkId); -} - class DmDeathCallback : public DmInitCallback { public: explicit DmDeathCallback(DevManager &devManager) : devManager_(devManager){}; @@ -85,13 +53,7 @@ int32_t DevManager::Init() { auto &deviceManager = DeviceManager::GetInstance(); auto deviceInitCallback = std::make_shared(*this); - auto deviceCallback = std::make_shared(*this); - int32_t errNo = deviceManager.InitDeviceManager(PKG_NAME, deviceInitCallback); - if (errNo != DM_OK) { - return errNo; - } - errNo = deviceManager.RegisterDevStateCallback(PKG_NAME, "", deviceCallback); - return errNo; + return deviceManager.InitDeviceManager(PKG_NAME, deviceInitCallback); } void DevManager::RegisterDevCallback() @@ -207,49 +169,4 @@ std::vector DevManager::GetRemoteDevices() } return dtInfos; } - -void DevManager::Online(const std::string &networkId) -{ - // do nothing - ZLOGI("%{public}s observers:%{public}zu", StoreUtil::Anonymous(networkId).c_str(), observers_.Size()); -} - -void DevManager::Offline(const std::string &networkId) -{ - DetailInfo deviceInfo; - if (deviceInfos_.Get(networkId, deviceInfo)) { - deviceInfos_.Delete(networkId); - deviceInfos_.Delete(deviceInfo.uuid); - } - ZLOGI("%{public}s observers:%{public}zu", StoreUtil::Anonymous(networkId).c_str(), observers_.Size()); - observers_.ForEach([&networkId](const auto &key, auto &value) { - value->Offline(networkId); - return false; - }); -} - -void DevManager::OnChanged(const std::string &networkId) -{ - // do nothing - ZLOGI("%{public}s observers:%{public}zu", StoreUtil::Anonymous(networkId).c_str(), observers_.Size()); -} - -void DevManager::OnReady(const std::string &networkId) -{ - ZLOGI("%{public}s observers:%{public}zu", StoreUtil::Anonymous(networkId).c_str(), observers_.Size()); - observers_.ForEach([&networkId](const auto &key, auto &value) { - value->Online(networkId); - return false; - }); -} - -void DevManager::Register(DevManager::Observer *observer) -{ - observers_.Insert(observer, observer); -} - -void DevManager::Unregister(DevManager::Observer *observer) -{ - observers_.Erase(observer); -} } // namespace OHOS::DistributedKv diff --git a/kv_store/frameworks/innerkitsimpl/kvdb/src/kvdb_service_client.cpp b/kv_store/frameworks/innerkitsimpl/kvdb/src/kvdb_service_client.cpp index 3dcfa4e9..765dbac3 100644 --- a/kv_store/frameworks/innerkitsimpl/kvdb/src/kvdb_service_client.cpp +++ b/kv_store/frameworks/innerkitsimpl/kvdb/src/kvdb_service_client.cpp @@ -119,7 +119,7 @@ Status KVDBServiceClient::BeforeCreate(const AppId &appId, const StoreId &storeI int32_t status = IPC_SEND(TRANS_BEFORE_CREATE, reply, appId, storeId, options); if (status != SUCCESS) { ZLOGE("status:0x%{public}x appId:%{public}s, storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + StoreUtil::Anonymous(storeId.storeId).c_str()); } return static_cast(status); } @@ -131,7 +131,7 @@ Status KVDBServiceClient::AfterCreate( int32_t status = IPC_SEND(TRANS_AFTER_CREATE, reply, appId, storeId, options, password); if (status != SUCCESS) { ZLOGE("status:0x%{public}x appId:%{public}s, storeId:%{public}s, encrypt:%{public}d", status, - appId.appId.c_str(), storeId.storeId.c_str(), options.encrypt); + appId.appId.c_str(), StoreUtil::Anonymous(storeId.storeId).c_str(), options.encrypt); } return static_cast(status); } @@ -142,7 +142,7 @@ Status KVDBServiceClient::Delete(const AppId &appId, const StoreId &storeId) int32_t status = IPC_SEND(TRANS_DELETE, reply, appId, storeId); if (status != SUCCESS) { ZLOGE("status:0x%{public}x appId:%{public}s, storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + StoreUtil::Anonymous(storeId.storeId).c_str()); } return static_cast(status); } @@ -154,7 +154,7 @@ Status KVDBServiceClient::Sync(const AppId &appId, const StoreId &storeId, const syncInfo.delay, syncInfo.query); if (status != SUCCESS) { ZLOGE("status:0x%{public}x, appId:%{public}s, storeId:%{public}s, sequenceId:%{public}" PRIu64, status, - appId.appId.c_str(), storeId.storeId.c_str(), syncInfo.seqId); + appId.appId.c_str(), StoreUtil::Anonymous(storeId.storeId).c_str(), syncInfo.seqId); } return static_cast(status); } @@ -186,7 +186,7 @@ Status KVDBServiceClient::SetSyncParam(const AppId &appId, const StoreId &storeI int32_t status = IPC_SEND(TRANS_SET_SYNC_PARAM, reply, appId, storeId, syncParam.allowedDelayMs); if (status != SUCCESS) { ZLOGE("status:0x%{public}x, appId:%{public}s, storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + StoreUtil::Anonymous(storeId.storeId).c_str()); } return static_cast(status); } @@ -197,7 +197,7 @@ Status KVDBServiceClient::GetSyncParam(const AppId &appId, const StoreId &storeI int32_t status = IPC_SEND(TRANS_GET_SYNC_PARAM, reply, appId, storeId); if (status != SUCCESS) { ZLOGE("status:0x%{public}x, appId:%{public}s, storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + StoreUtil::Anonymous(storeId.storeId).c_str()); return SUCCESS; } ITypesUtil::Unmarshal(reply, syncParam.allowedDelayMs); @@ -210,7 +210,7 @@ Status KVDBServiceClient::EnableCapability(const AppId &appId, const StoreId &st int32_t status = IPC_SEND(TRANS_ENABLE_CAP, reply, appId, storeId); if (status != SUCCESS) { ZLOGE("status:0x%{public}x, appId:%{public}s, storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + StoreUtil::Anonymous(storeId.storeId).c_str()); } return static_cast(status); } @@ -221,7 +221,7 @@ Status KVDBServiceClient::DisableCapability(const AppId &appId, const StoreId &s int32_t status = IPC_SEND(TRANS_DISABLE_CAP, reply, appId, storeId); if (status != SUCCESS) { ZLOGE("status:0x%{public}x, appId:%{public}s, storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + StoreUtil::Anonymous(storeId.storeId).c_str()); } return static_cast(status); } @@ -233,7 +233,7 @@ Status KVDBServiceClient::SetCapability(const AppId &appId, const StoreId &store int32_t status = IPC_SEND(TRANS_SET_CAP, reply, appId, storeId, local, remote); if (status != SUCCESS) { ZLOGE("status:0x%{public}x, appId:%{public}s, storeId:%{public}s", status, appId.appId.c_str(), - storeId.storeId.c_str()); + StoreUtil::Anonymous(storeId.storeId).c_str()); } return static_cast(status); } @@ -244,7 +244,8 @@ Status KVDBServiceClient::AddSubscribeInfo(const AppId &appId, const StoreId &st int32_t status = IPC_SEND(TRANS_ADD_SUB, reply, appId, storeId, syncInfo.seqId, syncInfo.devices, syncInfo.query); if (status != SUCCESS) { ZLOGE("status:0x%{public}x, appId:%{public}s, storeId:%{public}s, query:%{public}s", status, - appId.appId.c_str(), storeId.storeId.c_str(), StoreUtil::Anonymous(syncInfo.query).c_str()); + appId.appId.c_str(), StoreUtil::Anonymous(storeId.storeId).c_str(), + StoreUtil::Anonymous(syncInfo.query).c_str()); } return static_cast(status); } @@ -255,7 +256,8 @@ Status KVDBServiceClient::RmvSubscribeInfo(const AppId &appId, const StoreId &st int32_t status = IPC_SEND(TRANS_RMV_SUB, reply, appId, storeId, syncInfo.seqId, syncInfo.devices, syncInfo.query); if (status != SUCCESS) { ZLOGE("status:0x%{public}x, appId:%{public}s, storeId:%{public}s, query:%{public}s", status, - appId.appId.c_str(), storeId.storeId.c_str(), StoreUtil::Anonymous(syncInfo.query).c_str()); + appId.appId.c_str(), StoreUtil::Anonymous(storeId.storeId).c_str(), + StoreUtil::Anonymous(syncInfo.query).c_str()); } return static_cast(status); } @@ -266,7 +268,8 @@ Status KVDBServiceClient::Subscribe(const AppId &appId, const StoreId &storeId, int32_t status = IPC_SEND(TRANS_SUB, reply, appId, storeId, observer->AsObject()); if (status != SUCCESS) { ZLOGE("status:0x%{public}x, appId:%{public}s, storeId:%{public}s, observer:0x%{public}x", status, - appId.appId.c_str(), storeId.storeId.c_str(), StoreUtil::Anonymous(observer.GetRefPtr())); + appId.appId.c_str(), StoreUtil::Anonymous(storeId.storeId).c_str(), + StoreUtil::Anonymous(observer.GetRefPtr())); } return static_cast(status); } @@ -277,7 +280,8 @@ Status KVDBServiceClient::Unsubscribe(const AppId &appId, const StoreId &storeId int32_t status = IPC_SEND(TRANS_UNSUB, reply, appId, storeId, observer->AsObject().GetRefPtr()); if (status != SUCCESS) { ZLOGE("status:0x%{public}x, appId:%{public}s, storeId:%{public}s, observer:0x%{public}x", status, - appId.appId.c_str(), storeId.storeId.c_str(), StoreUtil::Anonymous(observer.GetRefPtr())); + appId.appId.c_str(), StoreUtil::Anonymous(storeId.storeId).c_str(), + StoreUtil::Anonymous(observer.GetRefPtr())); } return static_cast(status); } @@ -289,13 +293,12 @@ Status KVDBServiceClient::GetBackupPassword( int32_t status = IPC_SEND(TRANS_GET_PASSWORD, reply, appId, storeId); if (status != SUCCESS) { ZLOGE("status:0x%{public}x appId:%{public}s, storeId:%{public}s", status, - appId.appId.c_str(), storeId.storeId.c_str()); + appId.appId.c_str(), StoreUtil::Anonymous(storeId.storeId).c_str()); } ITypesUtil::Unmarshal(reply, password); return static_cast(status); } - sptr KVDBServiceClient::GetSyncAgent(const AppId &appId) { std::lock_guard lockGuard(agentMtx_); diff --git a/kv_store/frameworks/innerkitsimpl/kvdb/src/single_store_impl.cpp b/kv_store/frameworks/innerkitsimpl/kvdb/src/single_store_impl.cpp index d8cc1b82..2eaf335f 100644 --- a/kv_store/frameworks/innerkitsimpl/kvdb/src/single_store_impl.cpp +++ b/kv_store/frameworks/innerkitsimpl/kvdb/src/single_store_impl.cpp @@ -38,25 +38,10 @@ SingleStoreImpl::SingleStoreImpl( if (options.backup) { BackupManager::GetInstance().Prepare(options.baseDir, storeId_); } - - for (auto &policy : options.policies) { - if (policy.type != TERM_OF_SYNC_VALIDITY) { - continue; - } - auto exist = std::get_if(&policy.value); - if (exist == nullptr || *exist <= 0) { - break; - } - interval_ = *exist; - DevManager::GetInstance().Register(this); - } } SingleStoreImpl::~SingleStoreImpl() { - if (interval_ > 0) { - DevManager::GetInstance().Unregister(this); - } if (taskId_ > 0) { TaskExecutor::GetInstance().Remove(taskId_); } @@ -84,7 +69,7 @@ Status SingleStoreImpl::Put(const Key &key, const Value &value) DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); std::shared_lock lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -109,7 +94,7 @@ Status SingleStoreImpl::PutBatch(const std::vector &entries) DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); std::shared_lock lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -139,7 +124,7 @@ Status SingleStoreImpl::Delete(const Key &key) DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); std::shared_lock lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -162,7 +147,7 @@ Status SingleStoreImpl::DeleteBatch(const std::vector &keys) DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); std::shared_lock lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -189,13 +174,13 @@ Status SingleStoreImpl::StartTransaction() DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); std::shared_lock lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } auto status = RetryWithCheckPoint([this]() { return dbStore_->StartTransaction(); }); if (status != SUCCESS) { - ZLOGE("status:0x%{public}x storeId:%{public}s", status, storeId_.c_str()); + ZLOGE("status:0x%{public}x storeId:%{public}s", status, StoreUtil::Anonymous(storeId_).c_str()); } return status; } @@ -205,7 +190,7 @@ Status SingleStoreImpl::Commit() DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); std::shared_lock lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -222,14 +207,14 @@ Status SingleStoreImpl::Rollback() DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); std::shared_lock lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } auto dbStatus = dbStore_->Rollback(); auto status = StoreUtil::ConvertStatus(dbStatus); if (status != SUCCESS) { - ZLOGE("status:0x%{public}x storeId:%{public}s", status, storeId_.c_str()); + ZLOGE("status:0x%{public}x storeId:%{public}s", status, StoreUtil::Anonymous(storeId_).c_str()); } return status; } @@ -239,7 +224,7 @@ Status SingleStoreImpl::SubscribeKvStore(SubscribeType type, std::shared_ptr lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -278,7 +263,7 @@ Status SingleStoreImpl::UnSubscribeKvStore(SubscribeType type, std::shared_ptr lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -316,7 +301,7 @@ Status SingleStoreImpl::Get(const Key &key, Value &value) DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); std::shared_lock lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -406,7 +391,7 @@ Status SingleStoreImpl::CloseResultSet(std::shared_ptr &resultSet) auto status = resultSet->Close(); if (status != SUCCESS) { - ZLOGE("status:0x%{public}x storeId:%{public}s", status, storeId_.c_str()); + ZLOGE("status:0x%{public}x storeId:%{public}s", status, StoreUtil::Anonymous(storeId_).c_str()); } resultSet = nullptr; return status; @@ -417,7 +402,7 @@ Status SingleStoreImpl::GetCount(const DataQuery &query, int &result) const DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); std::shared_lock lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -435,7 +420,7 @@ Status SingleStoreImpl::GetSecurityLevel(SecurityLevel &secLevel) const DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); std::shared_lock lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -454,7 +439,7 @@ Status SingleStoreImpl::RemoveDeviceData(const std::string &device) DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); std::shared_lock lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -573,7 +558,8 @@ Status SingleStoreImpl::SubscribeWithQuery(const std::vector &devic syncInfo.query = query.ToString(); auto syncAgent = service->GetSyncAgent({ appId_ }); if (syncAgent == nullptr) { - ZLOGE("failed! invalid agent app:%{public}s, store:%{public}s!", appId_.c_str(), storeId_.c_str()); + ZLOGE("failed! invalid agent app:%{public}s, store:%{public}s!", appId_.c_str(), + StoreUtil::Anonymous(storeId_).c_str()); return ILLEGAL_STATE; } @@ -594,7 +580,8 @@ Status SingleStoreImpl::UnsubscribeWithQuery(const std::vector &dev syncInfo.query = query.ToString(); auto syncAgent = service->GetSyncAgent({ appId_ }); if (syncAgent == nullptr) { - ZLOGE("failed! invalid agent app:%{public}s, store:%{public}s!", appId_.c_str(), storeId_.c_str()); + ZLOGE("failed! invalid agent app:%{public}s, store:%{public}s!", appId_.c_str(), + StoreUtil::Anonymous(storeId_).c_str()); return ILLEGAL_STATE; } @@ -630,7 +617,8 @@ Status SingleStoreImpl::Backup(const std::string &file, const std::string &baseD DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); auto status = BackupManager::GetInstance().Backup(file, baseDir, storeId_, dbStore_); if (status != SUCCESS) { - ZLOGE("status:0x%{public}x storeId:%{public}s backup:%{public}s ", status, storeId_.c_str(), file.c_str()); + ZLOGE("status:0x%{public}x storeId:%{public}s backup:%{public}s ", status, + StoreUtil::Anonymous(storeId_).c_str(), file.c_str()); } return status; } @@ -640,7 +628,8 @@ Status SingleStoreImpl::Restore(const std::string &file, const std::string &base DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); auto status = BackupManager::GetInstance().Restore(file, baseDir, appId_, storeId_, dbStore_); if (status != SUCCESS) { - ZLOGE("status:0x%{public}x storeId:%{public}s backup:%{public}s ", status, storeId_.c_str(), file.c_str()); + ZLOGE("status:0x%{public}x storeId:%{public}s backup:%{public}s ", status, + StoreUtil::Anonymous(storeId_).c_str(), file.c_str()); } return status; } @@ -654,7 +643,7 @@ Status SingleStoreImpl::DeleteBackup(const std::vector &files, cons } auto status = BackupManager::GetInstance().DeleteBackup(results, baseDir, storeId_); if (status != SUCCESS) { - ZLOGE("status:0x%{public}x storeId:%{public}s", status, storeId_.c_str()); + ZLOGE("status:0x%{public}x storeId:%{public}s", status, StoreUtil::Anonymous(storeId_).c_str()); } return status; } @@ -731,7 +720,7 @@ Status SingleStoreImpl::GetResultSet(const DBQuery &query, std::shared_ptr lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -748,7 +737,7 @@ Status SingleStoreImpl::GetEntries(const DBQuery &query, std::vector &ent { std::shared_lock lock(rwMutex_); if (dbStore_ == nullptr) { - ZLOGE("db:%{public}s already closed!", storeId_.c_str()); + ZLOGE("db:%{public}s already closed!", StoreUtil::Anonymous(storeId_).c_str()); return ALREADY_CLOSED; } @@ -780,7 +769,8 @@ Status SingleStoreImpl::DoSync(const SyncInfo &syncInfo, std::shared_ptrGetSyncAgent({ appId_ }); if (syncAgent == nullptr) { - ZLOGE("failed! invalid agent app:%{public}s store:%{public}s!", appId_.c_str(), storeId_.c_str()); + ZLOGE("failed! invalid agent app:%{public}s store:%{public}s!", appId_.c_str(), + StoreUtil::Anonymous(storeId_).c_str()); return ILLEGAL_STATE; } @@ -797,26 +787,8 @@ void SingleStoreImpl::DoAutoSync() if (!autoSync_) { return; } - ZLOGD("app:%{public}s store:%{public}s!", appId_.c_str(), storeId_.c_str()); + ZLOGD("app:%{public}s store:%{public}s!", appId_.c_str(), StoreUtil::Anonymous(storeId_).c_str()); AutoSyncTimer::GetInstance().DoAutoSync(appId_, { { storeId_ } }); - expiration_ = steady_clock::now() + seconds(interval_); -} - -void SingleStoreImpl::Online(const std::string &device) -{ - if (!autoSync_ || steady_clock::now() >= expiration_) { - return; - } - - ZLOGI("device:%{public}s online app:%{public}s store:%{public}s Sync!", StoreUtil::Anonymous(device).c_str(), - appId_.c_str(), storeId_.c_str()); - SyncInfo syncInfo; - syncInfo.devices = { device }; - DoSync(syncInfo, nullptr); -} - -void SingleStoreImpl::Offline(const std::string &device) -{ } void SingleStoreImpl::OnRemoteDied() diff --git a/kv_store/frameworks/innerkitsimpl/kvdb/src/store_factory.cpp b/kv_store/frameworks/innerkitsimpl/kvdb/src/store_factory.cpp index 89ecbb03..fda4ce86 100644 --- a/kv_store/frameworks/innerkitsimpl/kvdb/src/store_factory.cpp +++ b/kv_store/frameworks/innerkitsimpl/kvdb/src/store_factory.cpp @@ -75,14 +75,14 @@ std::shared_ptr StoreFactory::GetOrOpenStore(const AppId &appId, if (options.encrypt && !dbPassword.IsValid()) { status = CRYPT_ERROR; ZLOGE("Crypt kvStore failed to get password, storeId is %{public}s, error is %{public}d", - storeId.storeId.c_str(), static_cast(status)); + StoreUtil::Anonymous(storeId.storeId).c_str(), static_cast(status)); return !stores.empty(); } if (options.encrypt) { status = RekeyRecover(storeId, options.baseDir, dbPassword, dbManager, options); if (status != SUCCESS) { - ZLOGE("KvStore password error, storeId is %{public}s, error is %{public}d", storeId.storeId.c_str(), - static_cast(status)); + ZLOGE("KvStore password error, storeId is %{public}s, error is %{public}d", + StoreUtil::Anonymous(storeId.storeId).c_str(), static_cast(status)); return !stores.empty(); } if (dbPassword.isKeyOutdated) { @@ -105,7 +105,7 @@ std::shared_ptr StoreFactory::GetOrOpenStore(const AppId &appId, status = StoreUtil::ConvertStatus(dbStatus); if (kvStore == nullptr) { ZLOGE("failed! status:%{public}d appId:%{public}s storeId:%{public}s path:%{public}s", dbStatus, - appId.appId.c_str(), storeId.storeId.c_str(), options.baseDir.c_str()); + appId.appId.c_str(), StoreUtil::Anonymous(storeId.storeId).c_str(), options.baseDir.c_str()); return !stores.empty(); } isCreate = true; diff --git a/kv_store/frameworks/innerkitsimpl/kvdb/src/store_manager.cpp b/kv_store/frameworks/innerkitsimpl/kvdb/src/store_manager.cpp index baed9cde..1611501d 100644 --- a/kv_store/frameworks/innerkitsimpl/kvdb/src/store_manager.cpp +++ b/kv_store/frameworks/innerkitsimpl/kvdb/src/store_manager.cpp @@ -19,6 +19,7 @@ #include "log_print.h" #include "security_manager.h" #include "store_factory.h" +#include "store_util.h" namespace OHOS::DistributedKv { StoreManager &StoreManager::GetInstance() { @@ -30,7 +31,7 @@ std::shared_ptr StoreManager::GetKVStore(const AppId &appId, cons const Options &options, Status &status) { ZLOGD("appId:%{public}s, storeId:%{public}s type:%{public}d area:%{public}d dir:%{public}s", appId.appId.c_str(), - storeId.storeId.c_str(), options.kvStoreType, options.area, options.baseDir.c_str()); + StoreUtil::Anonymous(storeId.storeId).c_str(), options.kvStoreType, options.area, options.baseDir.c_str()); status = ILLEGAL_STATE; if (!appId.IsValid() || !storeId.IsValid() || !options.IsValidType()) { status = INVALID_ARGUMENT; @@ -44,7 +45,7 @@ std::shared_ptr StoreManager::GetKVStore(const AppId &appId, cons if (status == STORE_META_CHANGED) { ZLOGE("appId:%{public}s, storeId:%{public}s type:%{public}d encrypt:%{public}d", appId.appId.c_str(), - storeId.storeId.c_str(), options.kvStoreType, options.encrypt); + StoreUtil::Anonymous(storeId.storeId).c_str(), options.kvStoreType, options.encrypt); return nullptr; } @@ -65,7 +66,7 @@ std::shared_ptr StoreManager::GetKVStore(const AppId &appId, cons Status StoreManager::CloseKVStore(const AppId &appId, const StoreId &storeId) { - ZLOGD("appId:%{public}s, storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGD("appId:%{public}s, storeId:%{public}s", appId.appId.c_str(), StoreUtil::Anonymous(storeId.storeId).c_str()); if (!appId.IsValid() || !storeId.IsValid()) { return INVALID_ARGUMENT; } @@ -99,8 +100,8 @@ Status StoreManager::GetStoreIds(const AppId &appId, std::vector &store Status StoreManager::Delete(const AppId &appId, const StoreId &storeId, const std::string &path) { - ZLOGD("appId:%{public}s, storeId:%{public}s dir:%{public}s", appId.appId.c_str(), storeId.storeId.c_str(), - path.c_str()); + ZLOGD("appId:%{public}s, storeId:%{public}s dir:%{public}s", appId.appId.c_str(), + StoreUtil::Anonymous(storeId.storeId).c_str(), path.c_str()); if (!appId.IsValid() || !storeId.IsValid()) { return INVALID_ARGUMENT; } diff --git a/kv_store/frameworks/innerkitsimpl/kvdb/test/store_factory_test.cpp b/kv_store/frameworks/innerkitsimpl/kvdb/test/store_factory_test.cpp index 7a14167b..c7aea233 100644 --- a/kv_store/frameworks/innerkitsimpl/kvdb/test/store_factory_test.cpp +++ b/kv_store/frameworks/innerkitsimpl/kvdb/test/store_factory_test.cpp @@ -26,6 +26,7 @@ #include "store_util.h" #include "sys/stat.h" #include "types.h" +namespace { using namespace testing::ext; using namespace OHOS::DistributedKv; @@ -353,3 +354,4 @@ HWTEST_F(StoreFactoryTest, RekeyNoPwdFile, TestSize.Level1) StoreManager::GetInstance().GetKVStore(appId, storeId, options, status); ASSERT_EQ(status, CRYPT_ERROR); } +} diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/include/js_field_node.h b/kv_store/frameworks/jskitsimpl/distributeddata/include/js_field_node.h index 26c75258..851a1827 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/include/js_field_node.h +++ b/kv_store/frameworks/jskitsimpl/distributeddata/include/js_field_node.h @@ -43,20 +43,20 @@ private: static napi_value GetValueType(napi_env env, napi_callback_info info); static napi_value SetValueType(napi_env env, napi_callback_info info); static std::map valueTypeToString_; - + template static napi_value GetContextValue(napi_env env, std::shared_ptr &ctxt, T &value); static JsFieldNode* GetFieldNode(napi_env env, napi_callback_info info, std::shared_ptr &ctxt); - - std::string ValueToString(JSUtil::KvStoreVariant value); - std::string ValueTypeToString(uint32_t type); - - std::list fields; - std::string fieldName; - uint32_t valueType = JSUtil::INVALID; - JSUtil::KvStoreVariant defaultValue; - bool isWithDefaultValue = false; - bool isNullable = false; + + std::string ToString(const JSUtil::KvStoreVariant &value); + std::string ToString(uint32_t type); + + std::list fields_; + std::string fieldName_; + uint32_t valueType_ = JSUtil::INVALID; + JSUtil::KvStoreVariant defaultValue_; + bool isWithDefaultValue_ = false; + bool isNullable_ = false; }; } // namespace OHOS::DistributedData #endif // OHOS_FIELD_NODE_H diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/include/js_kv_store.h b/kv_store/frameworks/jskitsimpl/distributeddata/include/js_kv_store.h index 130ca23e..e506380e 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/include/js_kv_store.h +++ b/kv_store/frameworks/jskitsimpl/distributeddata/include/js_kv_store.h @@ -27,7 +27,7 @@ enum { /* exported js SubscribeType is (DistributedKv::SubscribeType-1) */ SUBSCRIBE_LOCAL = 0, /* i.e. SubscribeType::SUBSCRIBE_TYPE_LOCAL-1 */ SUBSCRIBE_REMOTE = 1, /* i.e. SubscribeType::SUBSCRIBE_TYPE_REMOTE-1 */ - SUBSCRIBE_LOCAL_REMOTE = 2, /* i.e. SubscribeType::SUBSCRIBE_TYPE_ALL--1 */ + SUBSCRIBE_LOCAL_REMOTE = 2, /* i.e. SubscribeType::SUBSCRIBE_TYPE_ALL-1 */ SUBSCRIBE_COUNT = 3 }; /* [NOTES] @@ -110,4 +110,4 @@ private: std::shared_ptr uvQueue_; }; } // namespace OHOS::DistributedData -#endif // OHOS_SINGLE_KV_STORE_H +#endif // OHOS_KV_STORE_H diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/include/js_kv_store_resultset.h b/kv_store/frameworks/jskitsimpl/distributeddata/include/js_kv_store_resultset.h index 20c0329a..5c20d5ea 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/include/js_kv_store_resultset.h +++ b/kv_store/frameworks/jskitsimpl/distributeddata/include/js_kv_store_resultset.h @@ -14,7 +14,6 @@ */ #ifndef OHOS_KV_STORE_RESELTSET_H #define OHOS_KV_STORE_RESELTSET_H -#include #include "napi_queue.h" #include "result_set_bridge.h" #include "kvstore_result_set.h" diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/include/js_query.h b/kv_store/frameworks/jskitsimpl/distributeddata/include/js_query.h index b4f27372..1f309495 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/include/js_query.h +++ b/kv_store/frameworks/jskitsimpl/distributeddata/include/js_query.h @@ -25,7 +25,7 @@ public: JsQuery() = default; ~JsQuery() = default; - DistributedKv::DataQuery& GetNative(); + const DistributedKv::DataQuery& GetNative() const; static napi_value Constructor(napi_env env); diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/include/js_schema.h b/kv_store/frameworks/jskitsimpl/distributeddata/include/js_schema.h index 5ed0a987..615b43cb 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/include/js_schema.h +++ b/kv_store/frameworks/jskitsimpl/distributeddata/include/js_schema.h @@ -32,7 +32,6 @@ public: static napi_status ToJson(napi_env env, napi_value inner, JsSchema*& out); std::string Dump(); - private: static napi_value GetRootNode(napi_env env, napi_callback_info info); static napi_value SetRootNode(napi_env env, napi_callback_info info); @@ -45,20 +44,19 @@ private: template static napi_value GetContextValue(napi_env env, std::shared_ptr& ctxt, T &value); - static JsSchema* GetSchema(napi_env env, napi_callback_info info, std::shared_ptr &ctxt); enum { SCHEMA_MODE_SLOPPY, SCHEMA_MODE_STRICT, }; - JsFieldNode* rootNode = nullptr; - napi_env env = nullptr; // manage the root. set/get. - napi_ref ref = nullptr; // manage the root. set/get. + JsFieldNode* rootNode_ = nullptr; + napi_env env_ = nullptr; // manage the root. set/get. + napi_ref ref_ = nullptr; // manage the root. set/get. - std::vector indexes; - uint32_t mode = SCHEMA_MODE_SLOPPY; - uint32_t skip = 0; + std::vector indexes_; + uint32_t mode_ = SCHEMA_MODE_SLOPPY; + uint32_t skip_ = 0; }; } // namespace OHOS::DistributedData #endif // OHOS_SCHEMA_H diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/include/js_util.h b/kv_store/frameworks/jskitsimpl/distributeddata/include/js_util.h index 9e3d9afb..d9ebeead 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/include/js_util.h +++ b/kv_store/frameworks/jskitsimpl/distributeddata/include/js_util.h @@ -145,7 +145,7 @@ public: /* napi_value <-> std::map */ static napi_status GetValue(napi_env env, napi_value in, std::map& out); static napi_status SetValue(napi_env env, const std::map& in, napi_value& out); - + static napi_status GetValue(napi_env env, napi_value in, JsSchema*& out); static napi_status GetValue(napi_env env, napi_value in, DataQuery &out); @@ -160,23 +160,8 @@ public: static inline napi_status GetNamedProperty( napi_env env, napi_value in, const std::string& prop, T& value, bool optional = false) { - bool hasProp = false; - napi_status status = napi_has_named_property(env, in, prop.c_str(), &hasProp); - if (status != napi_ok) { - return napi_generic_failure; - } - if (!hasProp) { - return optional ? napi_ok : napi_generic_failure; - } - napi_value inner = nullptr; - status = napi_get_named_property(env, in, prop.c_str(), &inner); - if (status != napi_ok || inner == nullptr) { - return napi_generic_failure; - } - if (optional && JSUtil::IsNull(env, inner)) { - return napi_ok; - } - return GetValue(env, inner, value); + auto [status, jsValue] = GetInnerValue(env, in, prop, optional); + return (jsValue == nullptr) ? status : GetValue(env, jsValue, value); }; /* napi_define_class wrapper */ @@ -200,6 +185,8 @@ private: TUPLE_VALUE, TUPLE_SIZE }; + static std::pair GetInnerValue( + napi_env env, napi_value in, const std::string& prop, bool optional); }; } // namespace OHOS::DistributedData #endif // OHOS_JS_UTIL_H diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/include/napi_queue.h b/kv_store/frameworks/jskitsimpl/distributeddata/include/napi_queue.h index 58674c27..7022dcff 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/include/napi_queue.h +++ b/kv_store/frameworks/jskitsimpl/distributeddata/include/napi_queue.h @@ -120,7 +120,8 @@ private: NapiAsyncComplete complete = nullptr; napi_deferred deferred = nullptr; napi_async_work work = nullptr; - ~AsyncContext() { + ~AsyncContext() + { execute = nullptr; complete = nullptr; ctx = nullptr; diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/include/uv_queue.h b/kv_store/frameworks/jskitsimpl/distributeddata/include/uv_queue.h index 659cf065..b5b96ea7 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/include/uv_queue.h +++ b/kv_store/frameworks/jskitsimpl/distributeddata/include/uv_queue.h @@ -25,7 +25,7 @@ class UvQueue final { public: using NapiArgsGenerator = std::function; using NapiCallbackGetter = std::function; - UvQueue(napi_env env); + explicit UvQueue(napi_env env); ~UvQueue(); napi_env GetEnv(); diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_const_properties.cpp b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_const_properties.cpp index fab7bb3b..b7884aa9 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_const_properties.cpp +++ b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_const_properties.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "Const_Properties" +#define LOG_TAG "ConstProperties" #include "js_const_properties.h" #include "js_util.h" #include "js_kv_store.h" @@ -25,7 +25,7 @@ static napi_status SetNamedProperty(napi_env env, napi_value& obj, const std::st { napi_value property = nullptr; napi_status status = napi_create_int32(env, value, &property); - CHECK_RETURN(status == napi_ok, "int32_t to napi_value failed!", status); + CHECK_RETURN(status == napi_ok, "napi_create_int32 failed!", status); status = napi_set_named_property(env, obj, name.c_str(), property); CHECK_RETURN(status == napi_ok, "napi_set_named_property failed!", status); return status; diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_device_kv_store.cpp b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_device_kv_store.cpp index 7a0f8131..4d6efe75 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_device_kv_store.cpp +++ b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_device_kv_store.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_DeviceKVStore" +#define LOG_TAG "JsDeviceKVStore" #include "js_device_kv_store.h" #include #include "js_kv_store_resultset.h" @@ -432,6 +432,7 @@ napi_value JsDeviceKVStore::Sync(napi_env env, napi_callback_info info) ctxt->status = JSUtil::GetValue(env, argv[2], ctxt->allowedDelayMs); CHECK_ARGS_RETURN_VOID( ctxt, ctxt->status == napi_ok || JSUtil::IsNull(env, argv[2]), "invalid arg[2], i.e. invalid delay!"); + ctxt->status = napi_ok; } }; ctxt->GetCbInfoSync(env, info, input); @@ -449,7 +450,7 @@ napi_value JsDeviceKVStore::Sync(napi_env env, napi_callback_info info) napi_value JsDeviceKVStore::New(napi_env env, napi_callback_info info) { - ZLOGD("Constructor single kv store!"); + ZLOGD("Constructor deviceKVStore!"); std::string storeId; auto ctxt = std::make_shared(); auto input = [env, ctxt, &storeId](size_t argc, napi_value* argv) { @@ -463,12 +464,12 @@ napi_value JsDeviceKVStore::New(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); JsDeviceKVStore* kvStore = new (std::nothrow) JsDeviceKVStore(storeId); - NAPI_ASSERT(env, kvStore !=nullptr, "no memory for kvStore"); + NAPI_ASSERT(env, kvStore !=nullptr, "no memory for deviceKvStore"); auto finalize = [](napi_env env, void* data, void* hint) { ZLOGI("deviceKvStore finalize."); auto* kvStore = reinterpret_cast(data); - CHECK_RETURN_VOID(kvStore != nullptr, "finalize null!"); + CHECK_RETURN_VOID(kvStore != nullptr, "kvStore is null!"); delete kvStore; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, kvStore, finalize, nullptr, nullptr), kvStore); diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_field_node.cpp b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_field_node.cpp index 5762b4fb..aa4cb77e 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_field_node.cpp +++ b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_field_node.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_FieldNode" +#define LOG_TAG "JsFieldNode" #include "js_field_node.h" #include "js_util.h" #include "log_print.h" @@ -39,24 +39,24 @@ std::map JsFieldNode::valueTypeToString_ = { }; JsFieldNode::JsFieldNode(const std::string& fName) - : fieldName(fName) + : fieldName_(fName) { } std::string JsFieldNode::GetFieldName() { - return fieldName; + return fieldName_; } JsFieldNode::json JsFieldNode::GetValueForJson() { - if (fields.empty()) { - return ValueTypeToString(valueType) + "," + (isNullable ? "NULL" : "NOT NULL"); + if (fields_.empty()) { + return ToString(valueType_) + "," + (isNullable_ ? "NULL" : "NOT NULL"); } json jsFields; - for (auto fld : fields) { - jsFields[fld->fieldName] = fld->GetValueForJson(); + for (auto fld : fields_) { + jsFields[fld->fieldName_] = fld->GetValueForJson(); } return jsFields; } @@ -93,9 +93,9 @@ napi_value JsFieldNode::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("fieldNode finalize."); - auto* field = reinterpret_cast(data); - CHECK_RETURN_VOID(field != nullptr, "finalize null!"); - delete field; + auto* fieldNode = reinterpret_cast(data); + CHECK_RETURN_VOID(fieldNode != nullptr, "fieldNode is null!"); + delete fieldNode; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, fieldNode, finalize, nullptr, nullptr), fieldNode); return ctxt->self; @@ -117,7 +117,7 @@ napi_value JsFieldNode::AppendChild(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto fieldNode = reinterpret_cast(ctxt->native); - fieldNode->fields.push_back(child); + fieldNode->fields_.push_back(child); napi_get_boolean(env, true, &ctxt->output); return ctxt->output; @@ -143,7 +143,7 @@ napi_value JsFieldNode::GetDefaultValue(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto fieldNode = GetFieldNode(env, info, ctxt); CHECK_RETURN(fieldNode != nullptr, "getFieldNode nullptr!", nullptr); - return GetContextValue(env, ctxt, fieldNode->defaultValue); + return GetContextValue(env, ctxt, fieldNode->defaultValue_); } napi_value JsFieldNode::SetDefaultValue(napi_env env, napi_callback_info info) @@ -161,7 +161,7 @@ napi_value JsFieldNode::SetDefaultValue(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto fieldNode = reinterpret_cast(ctxt->native); - fieldNode->defaultValue = vv; + fieldNode->defaultValue_ = vv; return nullptr; } @@ -171,7 +171,7 @@ napi_value JsFieldNode::GetNullable(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto fieldNode = GetFieldNode(env, info, ctxt); CHECK_RETURN(fieldNode != nullptr, "getFieldNode nullptr!", nullptr); - return GetContextValue(env, ctxt, fieldNode->isNullable); + return GetContextValue(env, ctxt, fieldNode->isNullable_); } napi_value JsFieldNode::SetNullable(napi_env env, napi_callback_info info) @@ -189,7 +189,7 @@ napi_value JsFieldNode::SetNullable(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto fieldNode = reinterpret_cast(ctxt->native); - fieldNode->isNullable = isNullable; + fieldNode->isNullable_ = isNullable; return nullptr; } @@ -199,7 +199,7 @@ napi_value JsFieldNode::GetValueType(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto fieldNode = GetFieldNode(env, info, ctxt); CHECK_RETURN(fieldNode != nullptr, "getFieldNode nullptr!", nullptr); - return GetContextValue(env, ctxt, fieldNode->valueType); + return GetContextValue(env, ctxt, fieldNode->valueType_); } napi_value JsFieldNode::SetValueType(napi_env env, napi_callback_info info) @@ -219,11 +219,11 @@ napi_value JsFieldNode::SetValueType(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto fieldNode = reinterpret_cast(ctxt->native); - fieldNode->valueType = type; + fieldNode->valueType_ = type; return nullptr; } -std::string JsFieldNode::ValueToString(JSUtil::KvStoreVariant value) +std::string JsFieldNode::ToString(const JSUtil::KvStoreVariant &value) { auto strValue = std::get_if(&value); if (strValue != nullptr) { @@ -249,7 +249,7 @@ std::string JsFieldNode::ValueToString(JSUtil::KvStoreVariant value) return std::string(); } -std::string JsFieldNode::ValueTypeToString(uint32_t type) +std::string JsFieldNode::ToString(uint32_t type) { // DistributedDB::FieldType auto it = valueTypeToString_.find(type); @@ -263,16 +263,16 @@ std::string JsFieldNode::ValueTypeToString(uint32_t type) std::string JsFieldNode::Dump() { json jsFields; - for (auto fld : fields) { + for (auto fld : fields_) { jsFields.push_back(fld->Dump()); } json jsNode = { - { FIELD_NAME, fieldName }, - { VALUE_TYPE, ValueTypeToString(valueType) }, - { DEFAULT_VALUE, ValueToString(defaultValue) }, - { IS_DEFAULT_VALUE, isWithDefaultValue }, - { IS_NULLABLE, isNullable }, + { FIELD_NAME, fieldName_ }, + { VALUE_TYPE, ToString(valueType_) }, + { DEFAULT_VALUE, ToString(defaultValue_) }, + { IS_DEFAULT_VALUE, isWithDefaultValue_ }, + { IS_NULLABLE, isNullable_ }, { CHILDREN, jsFields.dump() } }; return jsNode.dump(); diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_kv_manager.cpp b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_kv_manager.cpp index 0c154263..665f48f2 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_kv_manager.cpp +++ b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_kv_manager.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_KVManager" +#define LOG_TAG "JsKVManager" #include "js_kv_manager.h" #include "distributed_kv_data_manager.h" #include "js_device_kv_store.h" @@ -392,7 +392,7 @@ napi_value JsKVManager::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("kvManager finalize."); auto* kvManager = reinterpret_cast(data); - CHECK_RETURN_VOID(kvManager != nullptr, "finalize null!"); + CHECK_RETURN_VOID(kvManager != nullptr, "kvManager is null!"); delete kvManager; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, kvManager, finalize, nullptr, nullptr), kvManager); diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_kv_store.cpp b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_kv_store.cpp index 4c9ae6c4..e6b173ec 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_kv_store.cpp +++ b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_kv_store.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_KVStore" +#define LOG_TAG "JsKVStore" #include "js_kv_store.h" #include "js_util.h" #include "js_kv_store_resultset.h" @@ -447,7 +447,7 @@ void JsKVStore::OnDataChange(napi_env env, size_t argc, napi_value* argv, std::s CHECK_STATUS_RETURN_VOID(ctxt, "napi_typeof failed!"); CHECK_ARGS_RETURN_VOID(ctxt, valueType == napi_function, "invalid arg[2], i.e. invalid callback"); - ZLOGI("subscribe data change type %{public}d", type); + ZLOGI("subscribe dataChange, type: %{public}d", type); auto proxy = reinterpret_cast(ctxt->native); std::lock_guard lck(proxy->listMutex_); for (auto& it : proxy->dataObserver_[type]) { diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_kv_store_resultset.cpp b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_kv_store_resultset.cpp index 892822b3..57202fc1 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_kv_store_resultset.cpp +++ b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_kv_store_resultset.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_KVStoreResultSet" +#define LOG_TAG "JsKVStoreResultSet" #include "js_kv_store_resultset.h" #include "js_util.h" #include "log_print.h" @@ -68,7 +68,7 @@ napi_value JsKVStoreResultSet::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("kvStoreResultSet finalize."); auto* resultSet = reinterpret_cast(data); - CHECK_RETURN_VOID(resultSet != nullptr, "finalize null!"); + CHECK_RETURN_VOID(resultSet != nullptr, "resultSet is null!"); delete resultSet; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, resultSet, finalize, nullptr, nullptr), resultSet); diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_query.cpp b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_query.cpp index ca8836f5..8cb4e8d2 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_query.cpp +++ b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_query.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_Query" +#define LOG_TAG "JsQuery" #include "js_query.h" #include "js_util.h" #include "log_print.h" @@ -22,7 +22,7 @@ using namespace OHOS::DistributedKv; namespace OHOS::DistributedData { -DataQuery& JsQuery::GetNative() +const DataQuery& JsQuery::GetNative() const { return query_; } @@ -77,7 +77,7 @@ napi_value JsQuery::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("query finalize."); auto* query = reinterpret_cast(data); - CHECK_RETURN_VOID(query != nullptr, "finalize null!"); + CHECK_RETURN_VOID(query != nullptr, "query is null!"); delete query; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, query, finalize, nullptr, nullptr), query); diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_schema.cpp b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_schema.cpp index c13b67ed..1de32338 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_schema.cpp +++ b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_schema.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_Schema" +#define LOG_TAG "JsSchema" #include "js_schema.h" #include @@ -33,16 +33,16 @@ static std::string SCHEMA_INDEXES = "SCHEMA_INDEXES"; static std::string SCHEMA_SKIPSIZE = "SCHEMA_SKIPSIZE"; static std::string DEFAULT_SCHEMA_VERSION = "1.0"; -JsSchema::JsSchema(napi_env env_) - : env(env_) +JsSchema::JsSchema(napi_env env) + : env_(env) { } JsSchema::~JsSchema() { ZLOGD("no memory leak for JsSchema"); - if (ref != nullptr) { - napi_delete_reference(env, ref); + if (ref_ != nullptr) { + napi_delete_reference(env_, ref_); } } @@ -72,7 +72,7 @@ napi_value JsSchema::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("Schema finalize."); auto* schema = reinterpret_cast(data); - CHECK_RETURN_VOID(schema != nullptr, "finalize null!"); + CHECK_RETURN_VOID(schema != nullptr, "schema is null!"); delete schema; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, schema, finalize, nullptr, nullptr), schema); @@ -104,17 +104,17 @@ napi_value JsSchema::GetRootNode(napi_env env, napi_callback_info info) ZLOGD("Schema::GetRootNode"); auto ctxt = std::make_shared(); auto schema = GetSchema(env, info, ctxt); - CHECK_RETURN(schema != nullptr, "getSchema nullptr!", nullptr); - if (schema->rootNode == nullptr) { + CHECK_RETURN(schema != nullptr, "schema is nullptr!", nullptr); + if (schema->rootNode_ == nullptr) { int argc = 1; napi_value argv[1] = { nullptr }; std::string root(SCHEMA_DEFINE); JSUtil::SetValue(env, root, argv[0]); - schema->ref = JSUtil::NewWithRef(env, argc, argv, - reinterpret_cast(&schema->rootNode), JsFieldNode::Constructor(env)); + schema->ref_ = JSUtil::NewWithRef(env, argc, argv, + reinterpret_cast(&schema->rootNode_), JsFieldNode::Constructor(env)); } - NAPI_ASSERT(env, schema->ref != nullptr, "no root, please set first!"); - NAPI_CALL(env, napi_get_reference_value(env, schema->ref, &ctxt->output)); + NAPI_ASSERT(env, schema->ref_ != nullptr, "no root, please set first!"); + NAPI_CALL(env, napi_get_reference_value(env, schema->ref_, &ctxt->output)); return ctxt->output; } @@ -131,12 +131,12 @@ napi_value JsSchema::SetRootNode(napi_env env, napi_callback_info info) CHECK_ARGS_RETURN_VOID(ctxt, node != nullptr, "invalid arg[0], i.e. invalid node!"); auto schema = reinterpret_cast(ctxt->native); - if (schema->ref != nullptr) { - napi_delete_reference(env, schema->ref); + if (schema->ref_ != nullptr) { + napi_delete_reference(env, schema->ref_); } - ctxt->status = napi_create_reference(env, argv[0], 1, &schema->ref); + ctxt->status = napi_create_reference(env, argv[0], 1, &schema->ref_); CHECK_STATUS_RETURN_VOID(ctxt, "napi_create_reference to FieldNode failed"); - schema->rootNode = node; + schema->rootNode_ = node; }; ctxt->GetCbInfoSync(env, info, input); NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); @@ -148,8 +148,8 @@ napi_value JsSchema::GetMode(napi_env env, napi_callback_info info) ZLOGD("Schema::GetMode"); auto ctxt = std::make_shared(); auto schema = GetSchema(env, info, ctxt); - CHECK_RETURN(schema != nullptr, "getSchema nullptr!", nullptr); - return GetContextValue(env, ctxt, schema->mode); + CHECK_RETURN(schema != nullptr, "schema is nullptr!", nullptr); + return GetContextValue(env, ctxt, schema->mode_); } napi_value JsSchema::SetMode(napi_env env, napi_callback_info info) @@ -166,7 +166,7 @@ napi_value JsSchema::SetMode(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto schema = reinterpret_cast(ctxt->native); - schema->mode = mode; + schema->mode_ = mode; return nullptr; } @@ -175,8 +175,8 @@ napi_value JsSchema::GetSkip(napi_env env, napi_callback_info info) ZLOGD("Schema::GetSkip"); auto ctxt = std::make_shared(); auto schema = GetSchema(env, info, ctxt); - CHECK_RETURN(schema != nullptr, "getSchema nullptr!", nullptr); - return GetContextValue(env, ctxt, schema->skip); + CHECK_RETURN(schema != nullptr, "schema is nullptr!", nullptr); + return GetContextValue(env, ctxt, schema->skip_); } napi_value JsSchema::SetSkip(napi_env env, napi_callback_info info) @@ -193,7 +193,7 @@ napi_value JsSchema::SetSkip(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto schema = reinterpret_cast(ctxt->native); - schema->skip = skip; + schema->skip_ = skip; return nullptr; } @@ -202,8 +202,8 @@ napi_value JsSchema::GetIndexes(napi_env env, napi_callback_info info) ZLOGD("Schema::GetIndexes"); auto ctxt = std::make_shared(); auto schema = GetSchema(env, info, ctxt); - CHECK_RETURN(schema != nullptr, "getSchema nullptr!", nullptr); - return GetContextValue(env, ctxt, schema->indexes); + CHECK_RETURN(schema != nullptr, "schema is nullptr!", nullptr); + return GetContextValue(env, ctxt, schema->indexes_); } napi_value JsSchema::SetIndexes(napi_env env, napi_callback_info info) @@ -220,22 +220,22 @@ napi_value JsSchema::SetIndexes(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto schema = reinterpret_cast(ctxt->native); - schema->indexes = indexes; + schema->indexes_ = indexes; return nullptr; } std::string JsSchema::Dump() { json jsIndexes = nlohmann::json::array(); - for (auto idx : indexes) { + for (auto idx : indexes_) { jsIndexes.push_back(idx); } json js = { { SCHEMA_VERSION, DEFAULT_SCHEMA_VERSION }, - { SCHEMA_MODE, (mode == SCHEMA_MODE_STRICT) ? "STRICT" : "COMPATIBLE" }, - { SCHEMA_DEFINE, rootNode->GetValueForJson() }, + { SCHEMA_MODE, (mode_ == SCHEMA_MODE_STRICT) ? "STRICT" : "COMPATIBLE" }, + { SCHEMA_DEFINE, rootNode_->GetValueForJson() }, { SCHEMA_INDEXES, jsIndexes }, - { SCHEMA_SKIPSIZE, skip }, + { SCHEMA_SKIPSIZE, skip_ }, }; return js.dump(); } diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_single_kv_store.cpp b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_single_kv_store.cpp index fa704a32..b03e2a2d 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_single_kv_store.cpp +++ b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_single_kv_store.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_SingleKVStore" +#define LOG_TAG "JsSingleKVStore" #include "js_single_kv_store.h" #include "js_util.h" #include "js_kv_store_resultset.h" @@ -105,7 +105,7 @@ napi_value JsSingleKVStore::Get(napi_env env, napi_callback_info info) enum class ArgsType : uint8_t { /* input arguments' combination type */ - KEYPREFIX = 0, + KEY_PREFIX = 0, QUERY, UNKNOWN = 255 }; @@ -127,7 +127,7 @@ static napi_status GetVariantArgs(napi_env env, size_t argc, napi_value* argv, V if (type == napi_string) { status = JSUtil::GetValue(env, argv[0], va.keyPrefix); CHECK_RETURN(!va.keyPrefix.empty(), "invalid arg[0], i.e. invalid keyPrefix!", napi_invalid_arg); - va.type = ArgsType::KEYPREFIX; + va.type = ArgsType::KEY_PREFIX; } else if (type == napi_object) { bool result = false; status = napi_instanceof(env, argv[0], JsQuery::Constructor(env), &result); @@ -170,7 +170,7 @@ napi_value JsSingleKVStore::GetEntries(napi_env env, napi_callback_info info) auto execute = [ctxt]() { auto& kvStore = reinterpret_cast(ctxt->native)->GetNative(); Status status = Status::INVALID_ARGUMENT; - if (ctxt->va.type == ArgsType::KEYPREFIX) { + if (ctxt->va.type == ArgsType::KEY_PREFIX) { OHOS::DistributedKv::Key keyPrefix(ctxt->va.keyPrefix); status = kvStore->GetEntries(keyPrefix, ctxt->entries); ZLOGD("kvStore->GetEntries() return %{public}d", status); @@ -223,7 +223,7 @@ napi_value JsSingleKVStore::GetResultSet(napi_env env, napi_callback_info info) std::shared_ptr kvResultSet; auto& kvStore = reinterpret_cast(ctxt->native)->GetNative(); Status status = Status::INVALID_ARGUMENT; - if (ctxt->va.type == ArgsType::KEYPREFIX) { + if (ctxt->va.type == ArgsType::KEY_PREFIX) { OHOS::DistributedKv::Key keyPrefix(ctxt->va.keyPrefix); status = kvStore->GetResultSet(keyPrefix, kvResultSet); ZLOGD("kvStore->GetEntries() return %{public}d", status); @@ -378,6 +378,7 @@ napi_value JsSingleKVStore::Sync(napi_env env, napi_callback_info info) ctxt->status = JSUtil::GetValue(env, argv[2], ctxt->allowedDelayMs); CHECK_ARGS_RETURN_VOID( ctxt, ctxt->status == napi_ok || JSUtil::IsNull(env, argv[2]), "invalid arg[2], i.e. invalid delay!"); + ctxt->status = napi_ok; } }; ctxt->GetCbInfoSync(env, info, input); @@ -473,7 +474,7 @@ napi_value JsSingleKVStore::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGI("singleKVStore finalize."); auto* kvStore = reinterpret_cast(data); - CHECK_RETURN_VOID(kvStore != nullptr, "finalize null!"); + CHECK_RETURN_VOID(kvStore != nullptr, "kvStore is null!"); delete kvStore; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, kvStore, finalize, nullptr, nullptr), kvStore); diff --git a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_util.cpp b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_util.cpp index f4133e01..2c40ec7b 100644 --- a/kv_store/frameworks/jskitsimpl/distributeddata/src/js_util.cpp +++ b/kv_store/frameworks/jskitsimpl/distributeddata/src/js_util.cpp @@ -185,18 +185,18 @@ JSUtil::KvStoreVariant JSUtil::Blob2VariantValue(const DistributedKv::Blob& blob std::vector real(data.begin() + 1, data.end()); ZLOGD("Blob::type %{public}d size=%{public}d", static_cast(data[0]), static_cast(real.size())); if (data[0] == JSUtil::INTEGER) { - uint32_t tmp4int = be32toh(*reinterpret_cast(&(real[0]))); - return JSUtil::KvStoreVariant(*reinterpret_cast(&tmp4int)); + uint32_t tmp = be32toh(*reinterpret_cast(&(real[0]))); + return JSUtil::KvStoreVariant(static_cast(tmp)); } else if (data[0] == JSUtil::FLOAT) { - uint32_t tmp4flt = be32toh(*reinterpret_cast(&(real[0]))); - return JSUtil::KvStoreVariant(*reinterpret_cast((void*)(&tmp4flt))); + uint32_t tmp = be32toh(*reinterpret_cast(&(real[0]))); + return JSUtil::KvStoreVariant(*reinterpret_cast((void*)(&tmp))); } else if (data[0] == JSUtil::BYTE_ARRAY) { return JSUtil::KvStoreVariant(std::vector(real.begin(), real.end())); } else if (data[0] == JSUtil::BOOLEAN) { return JSUtil::KvStoreVariant(static_cast(real[0])); } else if (data[0] == JSUtil::DOUBLE) { - uint64_t tmp4dbl = be64toh(*reinterpret_cast(&(real[0]))); - return JSUtil::KvStoreVariant(*reinterpret_cast((void*)(&tmp4dbl))); + uint64_t tmp = be64toh(*reinterpret_cast(&(real[0]))); + return JSUtil::KvStoreVariant(*reinterpret_cast((void*)(&tmp))); } else if (data[0] == JSUtil::STRING){ return JSUtil::KvStoreVariant(std::string(real.begin(), real.end())); } else { @@ -223,30 +223,30 @@ DistributedKv::Blob JSUtil::VariantValue2Blob(const JSUtil::KvStoreVariant& valu data.push_back(JSUtil::BOOLEAN); data.push_back(static_cast(*boolValue)); } - uint8_t *tmp = nullptr; + uint8_t *res = nullptr; auto intValue = std::get_if(&value); if (intValue != nullptr) { - int32_t tmp4int = *intValue; // copy value, and make it available in stack space. - htobe32(*reinterpret_cast(&tmp4int)); - tmp = reinterpret_cast(&tmp4int); + int32_t tmp = *intValue; // copy value, and make it available in stack space. + tmp = htobe32(static_cast(tmp)); + res = reinterpret_cast(&tmp); data.push_back(JSUtil::INTEGER); - data.insert(data.end(), tmp, tmp + sizeof(int32_t) / sizeof(uint8_t)); + data.insert(data.end(), res, res + sizeof(int32_t) / sizeof(uint8_t)); } auto fltValue = std::get_if(&value); if (fltValue != nullptr) { - float tmp4flt = *fltValue; // copy value, and make it available in stack space. - uint32_t tmp32 = htobe32(*reinterpret_cast(&tmp4flt)); - tmp = reinterpret_cast(&tmp32); + float tmp = *fltValue; // copy value, and make it available in stack space. + uint32_t tmp32 = htobe32(*reinterpret_cast(&tmp)); + res = reinterpret_cast(&tmp32); data.push_back(JSUtil::FLOAT); - data.insert(data.end(), tmp, tmp + sizeof(float) / sizeof(uint8_t)); + data.insert(data.end(), res, res + sizeof(float) / sizeof(uint8_t)); } auto dblValue = std::get_if(&value); if (dblValue != nullptr) { - double tmp4dbl = *dblValue; // copy value, and make it available in stack space. - uint64_t tmp64 = htobe64(*reinterpret_cast(&tmp4dbl)); - tmp = reinterpret_cast(&tmp64); + double tmp = *dblValue; // copy value, and make it available in stack space. + uint64_t tmp64 = htobe64(*reinterpret_cast(&tmp)); + res = reinterpret_cast(&tmp64); data.push_back(JSUtil::DOUBLE); - data.insert(data.end(), tmp, tmp + sizeof(double) / sizeof(uint8_t)); + data.insert(data.end(), res, res + sizeof(double) / sizeof(uint8_t)); } return DistributedKv::Blob(data); } @@ -265,7 +265,7 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, JSUtil::KvStoreVariant break; } case napi_number: { - double vNum = 0.0f; + double vNum = 0.0; status = JSUtil::GetValue(env, in, vNum); out = vNum; break; @@ -283,7 +283,7 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, JSUtil::KvStoreVariant break; } default: - ZLOGE(" napi_value -> KvStoreVariant not [Uint8Array | string | boolean | number] type=%{public}d", type); + ZLOGE("napi_value -> KvStoreVariant not [Uint8Array | string | boolean | number] type=%{public}d", type); status = napi_invalid_arg; break; } @@ -336,7 +336,7 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, JSUtil::QueryVariant& break; } case napi_number: { - double vNum = 0.0f; + double vNum = 0.0; status = JSUtil::GetValue(env, in, vNum); out = vNum; break; @@ -405,7 +405,7 @@ napi_status JSUtil::SetValue(napi_env env, const std::vector& in, napi_ CHECK_RETURN((status == napi_ok), "create array buffer failed!", status); if (memcpy_s(data, in.size(), in.data(), in.size()) != EOK) { - ZLOGE("memcpy_s not EOK"); + ZLOGE("napi_value <- std::vector: memcpy_s failed, vector size:%{public}zd", in.size()); return napi_invalid_arg; } status = napi_create_typedarray(env, napi_uint8_array, in.size(), buffer, 0, &out); @@ -492,7 +492,7 @@ napi_status JSUtil::SetValue(napi_env env, const std::vector& in, napi_ CHECK_RETURN((status == napi_ok), "invalid buffer", status); if (memcpy_s(data, bytes, in.data(), bytes) != EOK) { - ZLOGE("memcpy_s not EOK"); + ZLOGE("napi_value <- std::vector: memcpy_s failed, vector size:%{public}zd", in.size()); return napi_invalid_arg; } status = napi_create_typedarray(env, napi_int32_array, in.size(), buffer, 0, &out); @@ -531,7 +531,7 @@ napi_status JSUtil::SetValue(napi_env env, const std::vector& in, napi CHECK_RETURN((status == napi_ok), "invalid buffer", status); if (memcpy_s(data, bytes, in.data(), bytes) != EOK) { - ZLOGE("memcpy_s not EOK"); + ZLOGE("napi_value <- std::vector: memcpy_s failed, vector size:%{public}zd", in.size()); return napi_invalid_arg; } status = napi_create_typedarray(env, napi_uint32_array, in.size(), buffer, 0, &out); @@ -570,7 +570,7 @@ napi_status JSUtil::SetValue(napi_env env, const std::vector& in, napi_ CHECK_RETURN((status == napi_ok), "invalid buffer", status); if (memcpy_s(data, bytes, in.data(), bytes) != EOK) { - ZLOGE("memcpy_s not EOK"); + ZLOGE("napi_value <- std::vector: memcpy_s failed, vector size:%{public}zd", in.size()); return napi_invalid_arg; } status = napi_create_typedarray(env, napi_bigint64_array, in.size(), buffer, 0, &out); @@ -609,9 +609,9 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, std::vector& o napi_value item = nullptr; status = napi_get_element(env, in, i, &item); CHECK_RETURN((item != nullptr) && (status == napi_ok), "no element", napi_invalid_arg); - double vi = 0.0f; + double vi = 0.0; status = napi_get_value_double(env, item, &vi); - CHECK_RETURN(status == napi_ok, "element not a double", napi_invalid_arg); + CHECK_RETURN(status == napi_ok, "element is not a double", napi_invalid_arg); out.push_back(vi); } } @@ -687,7 +687,7 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, DistributedKv::Entry& napi_value propVType = nullptr; status = napi_get_named_property(env, propValue, "type", &propVType); CHECK_RETURN((status == napi_ok), "no property value.type", status); - int32_t type = 0; // int8_t + int32_t type = 0; status = GetValue(env, propVType, type); CHECK_RETURN((status == napi_ok), "no value of value.type", status); @@ -705,7 +705,7 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, DistributedKv::Entry& out.value = JSUtil::VariantValue2Blob(value); } if (type != out.value[0]) { - ZLOGE("unmarch type[%{public}d] to value.type[%{public}d]", (int)type, (int)out.value[0]); + ZLOGE("unmatch type[%{public}d] to value.type[%{public}d]", (int)type, (int)out.value[0]); } return status; } @@ -791,7 +791,7 @@ napi_status JSUtil::GetValue(napi_env env, napi_value jsValue, ValueObject::Type JSUtil::GetValue(env, jsValue, value); valueObject = value; } else if (type == napi_number) { - double value = 0; + double value = 0.0; napi_get_value_double(env, jsValue, &value); valueObject = value; } else if (type == napi_boolean) { @@ -1086,7 +1086,6 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, DataQuery &query) PredicatesProxy *predicates = nullptr; napi_unwrap(env, in, reinterpret_cast(&predicates)); CHECK_RETURN((predicates != nullptr), "invalid type", napi_invalid_arg); - std::vector keys; Status status = KvUtils::ToQuery(*(predicates->predicates_), query); if (status != Status::SUCCESS) { ZLOGD("napi_value -> GetValue DataQuery failed "); @@ -1146,4 +1145,27 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, ContextParam ¶m) } return napi_ok; } + +std::pair JSUtil::GetInnerValue( + napi_env env, napi_value in, const std::string& prop, bool optional) +{ + bool hasProp = false; + napi_status status = napi_has_named_property(env, in, prop.c_str(), &hasProp); + if (status != napi_ok) { + return std::make_pair(napi_generic_failure, nullptr); + } + if (!hasProp) { + status = optional ? napi_ok : napi_generic_failure; + return std::make_pair(status, nullptr); + } + napi_value inner = nullptr; + status = napi_get_named_property(env, in, prop.c_str(), &inner); + if (status != napi_ok || inner == nullptr) { + return std::make_pair(napi_generic_failure, nullptr); + } + if (optional && JSUtil::IsNull(env, inner)) { + return std::make_pair(napi_ok, nullptr); + } + return std::make_pair(napi_ok, inner); +} } // namespace OHOS::DistributedData diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_error_utils.h b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_error_utils.h index 774ae406..2b9788f2 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_error_utils.h +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_error_utils.h @@ -34,7 +34,7 @@ struct JsErrorCode { const std::optional GetJsErrorCode(int32_t errorCode); Status GenerateNapiError(Status status, int32_t &errCode, std::string &errMessage); -void ThrowNapiError(napi_env env, int32_t errCode, std::string errMessage, bool isParamsCheck = true); +void ThrowNapiError(napi_env env, int32_t errCode, const std::string &errMessage, bool isParamsCheck = true); napi_value GenerateErrorMsg(napi_env env, JsErrorCode jsInfo); #define ASSERT_ERR(env, assertion, errorCode, message) \ diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_field_node.h b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_field_node.h index e37cb4d1..600e2f27 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_field_node.h +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_field_node.h @@ -43,20 +43,20 @@ private: static napi_value GetValueType(napi_env env, napi_callback_info info); static napi_value SetValueType(napi_env env, napi_callback_info info); static std::map valueTypeToString_; - + template static napi_value GetContextValue(napi_env env, std::shared_ptr &ctxt, T &value); static JsFieldNode* GetFieldNode(napi_env env, napi_callback_info info, std::shared_ptr &ctxt); - - std::string ValueToString(JSUtil::KvStoreVariant value); - std::string ValueTypeToString(uint32_t type); - - std::list fields; - std::string fieldName; - uint32_t valueType = JSUtil::INVALID; - JSUtil::KvStoreVariant defaultValue; - bool isWithDefaultValue = false; - bool isNullable = false; + + std::string ToString(const JSUtil::KvStoreVariant &value); + std::string ToString(uint32_t type); + + std::list fields_; + std::string fieldName_; + uint32_t valueType_ = JSUtil::INVALID; + JSUtil::KvStoreVariant defaultValue_; + bool isWithDefaultValue_ = false; + bool isNullable_ = false; }; } // namespace OHOS::DistributedKVStore #endif // OHOS_FIELD_NODE_H diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_kv_store_resultset.h b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_kv_store_resultset.h index bfb1638c..8fb9ee62 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_kv_store_resultset.h +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_kv_store_resultset.h @@ -14,7 +14,6 @@ */ #ifndef OHOS_KV_STORE_RESELTSET_H #define OHOS_KV_STORE_RESELTSET_H -#include #include "napi_queue.h" #include "result_set_bridge.h" #include "kvstore_result_set.h" diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_query.h b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_query.h index 12c9e4ec..402e3c42 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_query.h +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_query.h @@ -25,7 +25,7 @@ public: JsQuery() = default; ~JsQuery() = default; - DistributedKv::DataQuery& GetDataQuery(); + const DistributedKv::DataQuery& GetDataQuery() const; static napi_value Constructor(napi_env env); diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_schema.h b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_schema.h index 65de452d..f1d57797 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_schema.h +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_schema.h @@ -29,7 +29,7 @@ public: static napi_value Constructor(napi_env env); static napi_value New(napi_env env, napi_callback_info info); - + static napi_status ToJson(napi_env env, napi_value inner, JsSchema*& out); std::string Dump(); @@ -52,13 +52,13 @@ private: SCHEMA_MODE_SLOPPY, SCHEMA_MODE_STRICT, }; - JsFieldNode* rootNode = nullptr; - napi_env env = nullptr; // manage the root. set/get. - napi_ref ref = nullptr; // manage the root. set/get. + JsFieldNode* rootNode_ = nullptr; + napi_env env_ = nullptr; // manage the root. set/get. + napi_ref ref_ = nullptr; // manage the root. set/get. - std::vector indexes; - uint32_t mode = SCHEMA_MODE_SLOPPY; - uint32_t skip = 0; + std::vector indexes_; + uint32_t mode_ = SCHEMA_MODE_SLOPPY; + uint32_t skip_ = 0; }; } // namespace OHOS::DistributedKVStore #endif // OHOS_SCHEMA_H diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_single_kv_store.h b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_single_kv_store.h index feb6f314..b963467e 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_single_kv_store.h +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_single_kv_store.h @@ -27,7 +27,7 @@ enum { /* exported js SubscribeType is (DistributedKv::SubscribeType-1) */ SUBSCRIBE_LOCAL = 0, /* i.e. SubscribeType::SUBSCRIBE_TYPE_LOCAL-1 */ SUBSCRIBE_REMOTE = 1, /* i.e. SubscribeType::SUBSCRIBE_TYPE_REMOTE-1 */ - SUBSCRIBE_LOCAL_REMOTE = 2, /* i.e. SubscribeType::SUBSCRIBE_TYPE_ALL--1 */ + SUBSCRIBE_LOCAL_REMOTE = 2, /* i.e. SubscribeType::SUBSCRIBE_TYPE_ALL-1 */ SUBSCRIBE_COUNT = 3 }; diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_util.h b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_util.h index 74194aee..3bb8665b 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_util.h +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/js_util.h @@ -178,26 +178,7 @@ public: /* napi_get_named_property wrapper */ template static inline napi_status GetNamedProperty( - napi_env env, napi_value in, const std::string& prop, T& value, bool optional = false) - { - bool hasProp = false; - napi_status status = napi_has_named_property(env, in, prop.c_str(), &hasProp); - if (status != napi_ok) { - return napi_generic_failure; - } - if (!hasProp) { - return optional ? napi_ok : napi_generic_failure; - } - napi_value inner = nullptr; - status = napi_get_named_property(env, in, prop.c_str(), &inner); - if (status != napi_ok || inner == nullptr) { - return napi_generic_failure; - } - if (optional && JSUtil::IsNull(env, inner)) { - return napi_ok; - } - return GetValue(env, inner, value); - }; + napi_env env, napi_value in, const std::string& prop, T& value, bool optional = false); /* napi_define_class wrapper */ static napi_value DefineClass(napi_env env, const std::string& name, @@ -223,6 +204,15 @@ private: TUPLE_SIZE }; static napi_status GetLevel(int32_t level, int32_t &out); + static std::pair GetInnerValue( + napi_env env, napi_value in, const std::string& prop, bool optional); +}; + +template +napi_status JSUtil::GetNamedProperty(napi_env env, napi_value in, const std::string &prop, T &value, bool optional) +{ + auto [status, jsValue] = GetInnerValue(env, in, prop, optional); + return (jsValue == nullptr) ? StatusMsg(status) : GetValue(env, jsValue, value); }; } // namespace OHOS::DistributedKVStore #endif // OHOS_JS_UTIL_H diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/uv_queue.h b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/uv_queue.h index 030aba97..f467a763 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/include/uv_queue.h +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/include/uv_queue.h @@ -25,7 +25,7 @@ class UvQueue final { public: using NapiArgsGenerator = std::function; using NapiCallbackGetter = std::function; - UvQueue(napi_env env); + explicit UvQueue(napi_env env); ~UvQueue(); napi_env GetEnv(); diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_const_properties.cpp b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_const_properties.cpp index 1bc5a36f..aa9394d9 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_const_properties.cpp +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_const_properties.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "Const_Properties" +#define LOG_TAG "ConstProperties" #include "js_const_properties.h" #include "js_util.h" #include "js_single_kv_store.h" diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_device_kv_store.cpp b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_device_kv_store.cpp index c3ea9f58..acb6bfe1 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_device_kv_store.cpp +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_device_kv_store.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_DeviceKVStore" +#define LOG_TAG "JsDeviceKVStore" #include "js_device_kv_store.h" #include #include "js_kv_store_resultset.h" @@ -324,7 +324,7 @@ napi_value JsDeviceKVStore::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGI("deviceKvStore finalize."); auto* kvStore = reinterpret_cast(data); - ASSERT_VOID(kvStore != nullptr, "finalize null!"); + ASSERT_VOID(kvStore != nullptr, "kvStore is null!"); delete kvStore; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, kvStore, finalize, nullptr, nullptr), kvStore); diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_error_utils.cpp b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_error_utils.cpp index 2e3f5031..2d07ea5e 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_error_utils.cpp +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_error_utils.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_ERROR_UTILS" +#define LOG_TAG "JsErrorCode" #include "js_error_utils.h" @@ -69,7 +69,7 @@ Status GenerateNapiError(Status status, int32_t &errCode, std::string &errMessag return status; } -void ThrowNapiError(napi_env env, int32_t status, std::string errMessage, bool isParamsCheck) +void ThrowNapiError(napi_env env, int32_t status, const std::string &errMessage, bool isParamsCheck) { ZLOGD("ThrowNapiError message: %{public}s", errMessage.c_str()); if (status == Status::SUCCESS) { diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_field_node.cpp b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_field_node.cpp index 872ef1bd..58c54268 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_field_node.cpp +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_field_node.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_FieldNode" +#define LOG_TAG "JSFieldNode" #include "js_field_node.h" #include "js_util.h" #include "log_print.h" @@ -39,24 +39,24 @@ std::map JsFieldNode::valueTypeToString_ = { }; JsFieldNode::JsFieldNode(const std::string& fName) - : fieldName(fName) + : fieldName_(fName) { } std::string JsFieldNode::GetFieldName() { - return fieldName; + return fieldName_; } JsFieldNode::json JsFieldNode::GetValueForJson() { - if (fields.empty()) { - return ValueTypeToString(valueType) + "," + (isNullable ? "NULL" : "NOT NULL"); + if (fields_.empty()) { + return ToString(valueType_) + "," + (isNullable_ ? "NULL" : "NOT NULL"); } json jsFields; - for (auto fld : fields) { - jsFields[fld->fieldName] = fld->GetValueForJson(); + for (auto fld : fields_) { + jsFields[fld->fieldName_] = fld->GetValueForJson(); } return jsFields; } @@ -93,9 +93,9 @@ napi_value JsFieldNode::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("fieldNode finalize."); - auto* field = reinterpret_cast(data); - ASSERT_VOID(field != nullptr, "finalize null!"); - delete field; + auto* fieldNode = reinterpret_cast(data); + ASSERT_VOID(fieldNode != nullptr, "fieldNode is null!"); + delete fieldNode; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, fieldNode, finalize, nullptr, nullptr), fieldNode); return ctxt->self; @@ -117,7 +117,7 @@ napi_value JsFieldNode::AppendChild(napi_env env, napi_callback_info info) ASSERT_NULL(!ctxt->isThrowError, "AppendChild exit"); auto fieldNode = reinterpret_cast(ctxt->native); - fieldNode->fields.push_back(child); + fieldNode->fields_.push_back(child); napi_get_boolean(env, true, &ctxt->output); return ctxt->output; @@ -142,8 +142,8 @@ napi_value JsFieldNode::GetDefaultValue(napi_env env, napi_callback_info info) ZLOGD("FieldNode::GetDefaultValue"); auto ctxt = std::make_shared(); auto fieldNode = GetFieldNode(env, info, ctxt); - ASSERT(fieldNode != nullptr, "getFieldNode nullptr!", nullptr); - return GetContextValue(env, ctxt, fieldNode->defaultValue); + ASSERT(fieldNode != nullptr, "fieldNode is nullptr!", nullptr); + return GetContextValue(env, ctxt, fieldNode->defaultValue_); } napi_value JsFieldNode::SetDefaultValue(napi_env env, napi_callback_info info) @@ -161,7 +161,7 @@ napi_value JsFieldNode::SetDefaultValue(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto fieldNode = reinterpret_cast(ctxt->native); - fieldNode->defaultValue = vv; + fieldNode->defaultValue_ = vv; return nullptr; } @@ -170,8 +170,8 @@ napi_value JsFieldNode::GetNullable(napi_env env, napi_callback_info info) ZLOGD("FieldNode::GetNullable"); auto ctxt = std::make_shared(); auto fieldNode = GetFieldNode(env, info, ctxt); - ASSERT(fieldNode != nullptr, "getFieldNode nullptr!", nullptr); - return GetContextValue(env, ctxt, fieldNode->isNullable); + ASSERT(fieldNode != nullptr, "fieldNode is nullptr!", nullptr); + return GetContextValue(env, ctxt, fieldNode->isNullable_); } napi_value JsFieldNode::SetNullable(napi_env env, napi_callback_info info) @@ -189,7 +189,7 @@ napi_value JsFieldNode::SetNullable(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto fieldNode = reinterpret_cast(ctxt->native); - fieldNode->isNullable = isNullable; + fieldNode->isNullable_ = isNullable; return nullptr; } @@ -198,8 +198,8 @@ napi_value JsFieldNode::GetValueType(napi_env env, napi_callback_info info) ZLOGD("FieldNode::GetValueType"); auto ctxt = std::make_shared(); auto fieldNode = GetFieldNode(env, info, ctxt); - ASSERT(fieldNode != nullptr, "getFieldNode nullptr!", nullptr); - return GetContextValue(env, ctxt, fieldNode->valueType); + ASSERT(fieldNode != nullptr, "fieldNode is nullptr!", nullptr); + return GetContextValue(env, ctxt, fieldNode->valueType_); } napi_value JsFieldNode::SetValueType(napi_env env, napi_callback_info info) @@ -219,11 +219,11 @@ napi_value JsFieldNode::SetValueType(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto fieldNode = reinterpret_cast(ctxt->native); - fieldNode->valueType = type; + fieldNode->valueType_ = type; return nullptr; } -std::string JsFieldNode::ValueToString(JSUtil::KvStoreVariant value) +std::string JsFieldNode::ToString(const JSUtil::KvStoreVariant &value) { auto strValue = std::get_if(&value); if (strValue != nullptr) { @@ -249,7 +249,7 @@ std::string JsFieldNode::ValueToString(JSUtil::KvStoreVariant value) return std::string(); } -std::string JsFieldNode::ValueTypeToString(uint32_t type) +std::string JsFieldNode::ToString(uint32_t type) { // DistributedDB::FieldType auto it = valueTypeToString_.find(type); @@ -263,16 +263,16 @@ std::string JsFieldNode::ValueTypeToString(uint32_t type) std::string JsFieldNode::Dump() { json jsFields; - for (auto fld : fields) { + for (auto fld : fields_) { jsFields.push_back(fld->Dump()); } json jsNode = { - { FIELD_NAME, fieldName }, - { VALUE_TYPE, ValueTypeToString(valueType) }, - { DEFAULT_VALUE, ValueToString(defaultValue) }, - { IS_DEFAULT_VALUE, isWithDefaultValue }, - { IS_NULLABLE, isNullable }, + { FIELD_NAME, fieldName_ }, + { VALUE_TYPE, ToString(valueType_) }, + { DEFAULT_VALUE, ToString(defaultValue_) }, + { IS_DEFAULT_VALUE, isWithDefaultValue_ }, + { IS_NULLABLE, isNullable_ }, { CHILDREN, jsFields.dump() } }; return jsNode.dump(); diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_kv_manager.cpp b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_kv_manager.cpp index 091f131e..509d07d1 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_kv_manager.cpp +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_kv_manager.cpp @@ -407,7 +407,7 @@ napi_value JsKVManager::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("kvManager finalize."); auto* kvManager = reinterpret_cast(data); - ASSERT_VOID(kvManager != nullptr, "finalize null!"); + ASSERT_VOID(kvManager != nullptr, "kvManager is null!"); delete kvManager; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, kvManager, finalize, nullptr, nullptr), kvManager); diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_kv_store_resultset.cpp b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_kv_store_resultset.cpp index a967b4c4..c725f827 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_kv_store_resultset.cpp +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_kv_store_resultset.cpp @@ -12,13 +12,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_KVStoreResultSet" +#define LOG_TAG "JsKVStoreResultSet" #include "js_kv_store_resultset.h" #include "js_util.h" #include "log_print.h" #include "napi_queue.h" #include "uv_queue.h" -#include "kvstore_datashare_bridge.h" #include "kv_utils.h" using namespace OHOS::DistributedKv; @@ -68,7 +67,7 @@ napi_value JsKVStoreResultSet::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("kvStoreResultSet finalize."); auto* resultSet = reinterpret_cast(data); - ASSERT_VOID(resultSet != nullptr, "finalize null!"); + ASSERT_VOID(resultSet != nullptr, "resultSet is null!"); delete resultSet; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, resultSet, finalize, nullptr, nullptr), resultSet); diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_query.cpp b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_query.cpp index 1e29df24..7c33cea0 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_query.cpp +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_query.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_Query" +#define LOG_TAG "JsQuery" #include "js_query.h" #include "js_util.h" #include "log_print.h" @@ -22,7 +22,7 @@ using namespace OHOS::DistributedKv; namespace OHOS::DistributedKVStore { -DataQuery& JsQuery::GetDataQuery() +const DataQuery& JsQuery::GetDataQuery() const { return query_; } @@ -77,7 +77,7 @@ napi_value JsQuery::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("query finalize."); auto* query = reinterpret_cast(data); - ASSERT_VOID(query != nullptr, "finalize null!"); + ASSERT_VOID(query != nullptr, "query is null!"); delete query; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, query, finalize, nullptr, nullptr), query); diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_schema.cpp b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_schema.cpp index 84e60a9b..9f6a1875 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_schema.cpp +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_schema.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_Schema" +#define LOG_TAG "JsSchema" #include "js_schema.h" #include @@ -33,16 +33,16 @@ static std::string SCHEMA_INDEXES = "SCHEMA_INDEXES"; static std::string SCHEMA_SKIPSIZE = "SCHEMA_SKIPSIZE"; static std::string DEFAULT_SCHEMA_VERSION = "1.0"; -JsSchema::JsSchema(napi_env env_) - : env(env_) +JsSchema::JsSchema(napi_env env) + : env_(env) { } JsSchema::~JsSchema() { ZLOGD("no memory leak for JsSchema"); - if (ref != nullptr) { - napi_delete_reference(env, ref); + if (ref_ != nullptr) { + napi_delete_reference(env_, ref_); } } @@ -72,7 +72,7 @@ napi_value JsSchema::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGD("Schema finalize."); auto* schema = reinterpret_cast(data); - ASSERT_VOID(schema != nullptr, "finalize null!"); + ASSERT_VOID(schema != nullptr, "schema is null!"); delete schema; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, schema, finalize, nullptr, nullptr), schema); @@ -105,16 +105,16 @@ napi_value JsSchema::GetRootNode(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto schema = GetSchema(env, info, ctxt); ASSERT(schema != nullptr, "getSchema nullptr!", nullptr); - if (schema->rootNode == nullptr) { + if (schema->rootNode_ == nullptr) { int argc = 1; napi_value argv[1] = { nullptr }; std::string root(SCHEMA_DEFINE); JSUtil::SetValue(env, root, argv[0]); - schema->ref = JSUtil::NewWithRef(env, argc, argv, - reinterpret_cast(&schema->rootNode), JsFieldNode::Constructor(env)); + schema->ref_ = JSUtil::NewWithRef(env, argc, argv, + reinterpret_cast(&schema->rootNode_), JsFieldNode::Constructor(env)); } - NAPI_ASSERT(env, schema->ref != nullptr, "no root, please set first!"); - NAPI_CALL(env, napi_get_reference_value(env, schema->ref, &ctxt->output)); + NAPI_ASSERT(env, schema->ref_ != nullptr, "no root, please set first!"); + NAPI_CALL(env, napi_get_reference_value(env, schema->ref_, &ctxt->output)); return ctxt->output; } @@ -131,12 +131,12 @@ napi_value JsSchema::SetRootNode(napi_env env, napi_callback_info info) ASSERT_ARGS(ctxt, node != nullptr, "invalid arg[0], i.e. invalid node!"); auto schema = reinterpret_cast(ctxt->native); - if (schema->ref != nullptr) { - napi_delete_reference(env, schema->ref); + if (schema->ref_ != nullptr) { + napi_delete_reference(env, schema->ref_); } - ctxt->status = napi_create_reference(env, argv[0], 1, &schema->ref); + ctxt->status = napi_create_reference(env, argv[0], 1, &schema->ref_); ASSERT_STATUS(ctxt, "napi_create_reference to FieldNode failed"); - schema->rootNode = node; + schema->rootNode_ = node; }; ctxt->GetCbInfoSync(env, info, input); NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); @@ -148,8 +148,8 @@ napi_value JsSchema::GetMode(napi_env env, napi_callback_info info) ZLOGD("Schema::GetMode"); auto ctxt = std::make_shared(); auto schema = GetSchema(env, info, ctxt); - ASSERT(schema != nullptr, "getSchema nullptr!", nullptr); - return GetContextValue(env, ctxt, schema->mode); + ASSERT(schema != nullptr, "schema is nullptr!", nullptr); + return GetContextValue(env, ctxt, schema->mode_); } napi_value JsSchema::SetMode(napi_env env, napi_callback_info info) @@ -166,7 +166,7 @@ napi_value JsSchema::SetMode(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto schema = reinterpret_cast(ctxt->native); - schema->mode = mode; + schema->mode_ = mode; return nullptr; } @@ -175,8 +175,8 @@ napi_value JsSchema::GetSkip(napi_env env, napi_callback_info info) ZLOGD("Schema::GetSkip"); auto ctxt = std::make_shared(); auto schema = GetSchema(env, info, ctxt); - ASSERT(schema != nullptr, "getSchema nullptr!", nullptr); - return GetContextValue(env, ctxt, schema->skip); + ASSERT(schema != nullptr, "schema is nullptr!", nullptr); + return GetContextValue(env, ctxt, schema->skip_); } napi_value JsSchema::SetSkip(napi_env env, napi_callback_info info) @@ -193,7 +193,7 @@ napi_value JsSchema::SetSkip(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto schema = reinterpret_cast(ctxt->native); - schema->skip = skip; + schema->skip_ = skip; return nullptr; } @@ -203,7 +203,7 @@ napi_value JsSchema::GetIndexes(napi_env env, napi_callback_info info) auto ctxt = std::make_shared(); auto schema = GetSchema(env, info, ctxt); ASSERT(schema != nullptr, "getSchema nullptr!", nullptr); - return GetContextValue(env, ctxt, schema->indexes); + return GetContextValue(env, ctxt, schema->indexes_); } napi_value JsSchema::SetIndexes(napi_env env, napi_callback_info info) @@ -220,22 +220,22 @@ napi_value JsSchema::SetIndexes(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto schema = reinterpret_cast(ctxt->native); - schema->indexes = indexes; + schema->indexes_ = indexes; return nullptr; } std::string JsSchema::Dump() { json jsIndexes = nlohmann::json::array(); - for (auto idx : indexes) { + for (auto idx : indexes_) { jsIndexes.push_back(idx); } json js = { { SCHEMA_VERSION, DEFAULT_SCHEMA_VERSION }, - { SCHEMA_MODE, (mode == SCHEMA_MODE_STRICT) ? "STRICT" : "COMPATIBLE" }, - { SCHEMA_DEFINE, rootNode->GetValueForJson() }, + { SCHEMA_MODE, (mode_ == SCHEMA_MODE_STRICT) ? "STRICT" : "COMPATIBLE" }, + { SCHEMA_DEFINE, rootNode_->GetValueForJson() }, { SCHEMA_INDEXES, jsIndexes }, - { SCHEMA_SKIPSIZE, skip }, + { SCHEMA_SKIPSIZE, skip_ }, }; return js.dump(); } diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_single_kv_store.cpp b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_single_kv_store.cpp index 127e7997..dc97949b 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_single_kv_store.cpp +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_single_kv_store.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "JS_SingleKVStore" +#define LOG_TAG "JsSingleKVStore" #include "js_single_kv_store.h" #include "js_util.h" #include "js_kv_store_resultset.h" @@ -1128,55 +1128,60 @@ napi_value JsSingleKVStore::RemoveDeviceData(napi_env env, napi_callback_info in return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute); } +struct SyncContext : public ContextBase { + std::vector deviceIdList; + uint32_t mode = 0; + uint32_t allowedDelayMs = 0; + JsQuery* query = nullptr; + napi_valuetype type = napi_undefined; + + void GetInput(napi_env env, napi_callback_info info) + { + auto input = [env, this](size_t argc, napi_value* argv) { + // required 3 arguments :: [allowedDelayMs] + ASSERT_BUSINESS_ERR(this, argc >= 2, Status::INVALID_ARGUMENT, "The number of parameters is incorrect."); + this->status = JSUtil::GetValue(env, argv[0], this->deviceIdList); + ASSERT_BUSINESS_ERR(this, this->status == napi_ok, Status::INVALID_ARGUMENT, + "The deviceIdList parameters is incorrect."); + napi_typeof(env, argv[1], &this->type); + if (this->type == napi_object) { + this->status = JSUtil::Unwrap(env, + argv[1], reinterpret_cast(&this->query), JsQuery::Constructor(env)); + ASSERT_BUSINESS_ERR(this, this->status == napi_ok, Status::INVALID_ARGUMENT, + "The parameters query is incorrect."); + this->status = JSUtil::GetValue(env, argv[2], this->mode); + ASSERT_BUSINESS_ERR(this, this->status == napi_ok, Status::INVALID_ARGUMENT, + "The parameters mode is incorrect."); + if (argc == 4) { + this->status = JSUtil::GetValue(env, argv[3], this->allowedDelayMs); + ASSERT_BUSINESS_ERR(this, (this->status == napi_ok || JSUtil::IsNull(env, argv[3])), + Status::INVALID_ARGUMENT, "The parameters delay is incorrect."); + } + } + if (this->type == napi_number) { + this->status = JSUtil::GetValue(env, argv[1], this->mode); + ASSERT_BUSINESS_ERR(this, this->status == napi_ok, Status::INVALID_ARGUMENT, + "The parameters mode is incorrect."); + if (argc == 3) { + this->status = JSUtil::GetValue(env, argv[2], this->allowedDelayMs); + ASSERT_BUSINESS_ERR(this, (this->status == napi_ok || JSUtil::IsNull(env, argv[2])), + Status::INVALID_ARGUMENT, "The parameters delay is incorrect."); + } + } + ASSERT_BUSINESS_ERR(this, (this->mode <= uint32_t(SyncMode::PUSH_PULL)) && (this->status == napi_ok), + Status::INVALID_ARGUMENT, "The number of parameters is incorrect."); + }; + ContextBase::GetCbInfoSync(env, info, input); + } +}; /* * [JS API Prototype] * sync(deviceIdList:string[], mode:SyncMode, allowedDelayMs?:number):void */ napi_value JsSingleKVStore::Sync(napi_env env, napi_callback_info info) { - struct SyncContext : public ContextBase { - std::vector deviceIdList; - uint32_t mode = 0; - uint32_t allowedDelayMs = 0; - JsQuery* query = nullptr; - napi_valuetype type = napi_undefined; - }; auto ctxt = std::make_shared(); - auto input = [env, ctxt](size_t argc, napi_value* argv) { - // required 3 arguments :: [allowedDelayMs] - ASSERT_BUSINESS_ERR(ctxt, argc >= 2, Status::INVALID_ARGUMENT, "The number of parameters is incorrect."); - ctxt->status = JSUtil::GetValue(env, argv[0], ctxt->deviceIdList); - ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::INVALID_ARGUMENT, - "The deviceIdList parameters is incorrect."); - napi_typeof(env, argv[1], &ctxt->type); - if (ctxt->type == napi_object) { - ctxt->status = JSUtil::Unwrap(env, - argv[1], reinterpret_cast(&ctxt->query), JsQuery::Constructor(env)); - ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::INVALID_ARGUMENT, - "The parameters query is incorrect."); - ctxt->status = JSUtil::GetValue(env, argv[2], ctxt->mode); - ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::INVALID_ARGUMENT, - "The parameters mode is incorrect."); - if (argc == 4) { - ctxt->status = JSUtil::GetValue(env, argv[3], ctxt->allowedDelayMs); - ASSERT_BUSINESS_ERR(ctxt, (ctxt->status == napi_ok || JSUtil::IsNull(env, argv[3])), - Status::INVALID_ARGUMENT, "The parameters delay is incorrect."); - } - } - if (ctxt->type == napi_number) { - ctxt->status = JSUtil::GetValue(env, argv[1], ctxt->mode); - ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::INVALID_ARGUMENT, - "The parameters mode is incorrect."); - if (argc == 3) { - ctxt->status = JSUtil::GetValue(env, argv[2], ctxt->allowedDelayMs); - ASSERT_BUSINESS_ERR(ctxt, (ctxt->status == napi_ok || JSUtil::IsNull(env, argv[2])), - Status::INVALID_ARGUMENT, "The parameters delay is incorrect."); - } - } - ASSERT_BUSINESS_ERR(ctxt, (ctxt->mode <= uint32_t(SyncMode::PUSH_PULL)) && (ctxt->status == napi_ok), - Status::INVALID_ARGUMENT, "The number of parameters is incorrect."); - }; - ctxt->GetCbInfoSync(env, info, input); + ctxt->GetInput(env, info); ASSERT_NULL(!ctxt->isThrowError, "Sync exit"); ZLOGD("sync deviceIdList.size=%{public}d, mode:%{public}u, allowedDelayMs:%{public}u", @@ -1277,7 +1282,7 @@ napi_value JsSingleKVStore::New(napi_env env, napi_callback_info info) auto finalize = [](napi_env env, void* data, void* hint) { ZLOGI("singleKVStore finalize."); auto* kvStore = reinterpret_cast(data); - ASSERT_VOID(kvStore != nullptr, "finalize null!"); + ASSERT_VOID(kvStore != nullptr, "kvStore is null!"); delete kvStore; }; ASSERT_CALL(env, napi_wrap(env, ctxt->self, kvStore, finalize, nullptr, nullptr), kvStore); diff --git a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_util.cpp b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_util.cpp index b4881dd0..7d6041e8 100644 --- a/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_util.cpp +++ b/kv_store/frameworks/jskitsimpl/distributedkvstore/src/js_util.cpp @@ -216,30 +216,30 @@ DistributedKv::Blob JSUtil::VariantValue2Blob(const JSUtil::KvStoreVariant& valu data.push_back(JSUtil::BOOLEAN); data.push_back(static_cast(*boolValue)); } - uint8_t *tmp = nullptr; + uint8_t *res = nullptr; auto intValue = std::get_if(&value); if (intValue != nullptr) { - int32_t tmp4int = *intValue; // copy value, and make it available in stack space. - uint32_t tmp32 = htobe32(*reinterpret_cast(&tmp4int)); - tmp = reinterpret_cast(&tmp32); + int32_t tmp = *intValue; // copy value, and make it available in stack space. + uint32_t tmp32 = htobe32(*reinterpret_cast(&tmp)); + res = reinterpret_cast(&tmp32); data.push_back(JSUtil::INTEGER); - data.insert(data.end(), tmp, tmp + sizeof(int32_t) / sizeof(uint8_t)); + data.insert(data.end(), res, res + sizeof(int32_t) / sizeof(uint8_t)); } auto fltValue = std::get_if(&value); if (fltValue != nullptr) { - float tmp4flt = *fltValue; // copy value, and make it available in stack space. - uint32_t tmp32 = htobe32(*reinterpret_cast(&tmp4flt)); - tmp = reinterpret_cast(&tmp32); + float tmp = *fltValue; // copy value, and make it available in stack space. + uint32_t tmp32 = htobe32(*reinterpret_cast(&tmp)); + res = reinterpret_cast(&tmp32); data.push_back(JSUtil::FLOAT); - data.insert(data.end(), tmp, tmp + sizeof(float) / sizeof(uint8_t)); + data.insert(data.end(), res, res + sizeof(float) / sizeof(uint8_t)); } auto dblValue = std::get_if(&value); if (dblValue != nullptr) { - double tmp4dbl = *dblValue; // copy value, and make it available in stack space. - uint64_t tmp64 = htobe64(*reinterpret_cast(&tmp4dbl)); - tmp = reinterpret_cast(&tmp64); + double tmp = *dblValue; // copy value, and make it available in stack space. + uint64_t tmp64 = htobe64(*reinterpret_cast(&tmp)); + res = reinterpret_cast(&tmp64); data.push_back(JSUtil::DOUBLE); - data.insert(data.end(), tmp, tmp + sizeof(double) / sizeof(uint8_t)); + data.insert(data.end(), res, res + sizeof(double) / sizeof(uint8_t)); } return DistributedKv::Blob(data); } @@ -258,7 +258,7 @@ JSUtil::StatusMsg JSUtil::GetValue(napi_env env, napi_value in, JSUtil::KvStoreV break; } case napi_number: { - double vNum = 0.0f; + double vNum = 0.0; statusMsg = JSUtil::GetValue(env, in, vNum); out = vNum; break; @@ -329,7 +329,7 @@ JSUtil::StatusMsg JSUtil::GetValue(napi_env env, napi_value in, JSUtil::QueryVar break; } case napi_number: { - double vNum = 0.0f; + double vNum = 0.0; statusMsg = JSUtil::GetValue(env, in, vNum); out = vNum; break; @@ -398,7 +398,7 @@ JSUtil::StatusMsg JSUtil::SetValue(napi_env env, const std::vector& in, ASSERT((statusMsg.status == napi_ok), "create array buffer failed!", statusMsg); if (memcpy_s(data, in.size(), in.data(), in.size()) != EOK) { - ZLOGE("memcpy_s not EOK"); + ZLOGE("napi_value <- std::vector: memcpy_s failed, vector size:%{public}zd", in.size()); return napi_invalid_arg; } statusMsg.status = napi_create_typedarray(env, napi_uint8_array, in.size(), buffer, 0, &out); @@ -485,7 +485,7 @@ JSUtil::StatusMsg JSUtil::SetValue(napi_env env, const std::vector& in, ASSERT((status == napi_ok), "invalid buffer", status); if (memcpy_s(data, bytes, in.data(), bytes) != EOK) { - ZLOGE("memcpy_s not EOK"); + ZLOGE("napi_value <- std::vector: memcpy_s failed, vector size:%{public}zd", in.size()); return napi_invalid_arg; } status = napi_create_typedarray(env, napi_int32_array, in.size(), buffer, 0, &out); @@ -524,7 +524,7 @@ JSUtil::StatusMsg JSUtil::SetValue(napi_env env, const std::vector& in ASSERT((status == napi_ok), "invalid buffer", status); if (memcpy_s(data, bytes, in.data(), bytes) != EOK) { - ZLOGE("memcpy_s not EOK"); + ZLOGE("napi_value <- std::vector: memcpy_s failed, vector size:%{public}zd", in.size()); return napi_invalid_arg; } status = napi_create_typedarray(env, napi_uint32_array, in.size(), buffer, 0, &out); @@ -556,14 +556,14 @@ JSUtil::StatusMsg JSUtil::SetValue(napi_env env, const std::vector& in, { ZLOGD("napi_value <- std::vector "); size_t bytes = in.size() * sizeof(int64_t); - ASSERT(bytes > 0, "invalid std::vector", napi_invalid_arg); + ASSERT(bytes > 0, "invalid std::vector", napi_invalid_arg); void* data = nullptr; napi_value buffer = nullptr; napi_status status = napi_create_arraybuffer(env, bytes, &data, &buffer); ASSERT((status == napi_ok), "invalid buffer", status); if (memcpy_s(data, bytes, in.data(), bytes) != EOK) { - ZLOGE("memcpy_s not EOK"); + ZLOGE("napi_value <- std::vector: memcpy_s failed, vector size:%{public}zd", in.size()); return napi_invalid_arg; } status = napi_create_typedarray(env, napi_bigint64_array, in.size(), buffer, 0, &out); @@ -602,7 +602,7 @@ JSUtil::StatusMsg JSUtil::GetValue(napi_env env, napi_value in, std::vector - const std::string rootPropName = "ZGF0YS5kaXN0cmlidXRlZGt2c3RvcmU"; + const std::string rootPropName = "ZGF0YS5kaXN0cmlidXRlZGt2c3RvcmU="; napi_value root = nullptr; bool hasRoot = false; napi_value global = nullptr; @@ -1234,4 +1234,27 @@ bool JSUtil::IsNull(napi_env env, napi_value value) } return false; } + +std::pair JSUtil::GetInnerValue( + napi_env env, napi_value in, const std::string& prop, bool optional) +{ + bool hasProp = false; + napi_status status = napi_has_named_property(env, in, prop.c_str(), &hasProp); + if (status != napi_ok) { + return std::make_pair(napi_generic_failure, nullptr); + } + if (!hasProp) { + status = optional ? napi_ok : napi_generic_failure; + return std::make_pair(status, nullptr); + } + napi_value inner = nullptr; + status = napi_get_named_property(env, in, prop.c_str(), &inner); + if (status != napi_ok || inner == nullptr) { + return std::make_pair(napi_generic_failure, nullptr); + } + if (optional && JSUtil::IsNull(env, inner)) { + return std::make_pair(napi_ok, nullptr); + } + return std::make_pair(napi_ok, inner); +} } // namespace OHOS::DistributedKVStore diff --git a/kv_store/frameworks/libs/distributeddb/BUILD.gn b/kv_store/frameworks/libs/distributeddb/BUILD.gn index cf7b051d..10a71422 100644 --- a/kv_store/frameworks/libs/distributeddb/BUILD.gn +++ b/kv_store/frameworks/libs/distributeddb/BUILD.gn @@ -18,6 +18,7 @@ config("distrdb_config") { include_dirs = [ "include", "interfaces/include", + "interfaces/include/cloud", "interfaces/src", "interfaces/src/relational", "common/include", diff --git a/kv_store/frameworks/libs/distributeddb/common/include/relational/relational_schema_object.h b/kv_store/frameworks/libs/distributeddb/common/include/relational/relational_schema_object.h index 13741f13..2f6e3965 100644 --- a/kv_store/frameworks/libs/distributeddb/common/include/relational/relational_schema_object.h +++ b/kv_store/frameworks/libs/distributeddb/common/include/relational/relational_schema_object.h @@ -70,6 +70,7 @@ private: int ParseCheckTableDefine(const JsonObject &inJsonObject, TableInfo &resultTable); int ParseCheckTableFieldInfo(const JsonObject &inJsonObject, const FieldPath &path, FieldInfo &table); int ParseCheckTableAutoInc(const JsonObject &inJsonObject, TableInfo &resultTable); + int ParseCheckTableSyncType(const JsonObject &inJsonObject, TableInfo &resultTable); int ParseCheckTableIndex(const JsonObject &inJsonObject, TableInfo &resultTable); int ParseCheckTableUnique(const JsonObject &inJsonObject, TableInfo &resultTable); int ParseCheckTablePrimaryKey(const JsonObject &inJsonObject, TableInfo &resultTable); diff --git a/kv_store/frameworks/libs/distributeddb/common/include/relational/table_info.h b/kv_store/frameworks/libs/distributeddb/common/include/relational/table_info.h index d664b471..1eade6a7 100644 --- a/kv_store/frameworks/libs/distributeddb/common/include/relational/table_info.h +++ b/kv_store/frameworks/libs/distributeddb/common/include/relational/table_info.h @@ -19,6 +19,7 @@ #include #include +#include "cloud/cloud_store_types.h" #include "db_types.h" #include "schema_constant.h" #include "data_value.h" @@ -68,6 +69,7 @@ class TableInfo { public: const std::string &GetTableName() const; bool GetAutoIncrement() const; + TableSyncType GetTableSyncType() const; const std::string &GetCreateTableSql() const; const FieldInfoMap &GetFields() const; // const IndexInfoMap &GetIndexDefine() const; @@ -76,6 +78,7 @@ public: void SetTableName(const std::string &tableName); void SetAutoIncrement(bool autoInc); + void SetTableSyncType(TableSyncType tableSyncType); void SetCreateTableSql(const std::string &sql); // set 'autoInc_' flag when set sql void AddField(const FieldInfo &field); void AddIndexDefine(const std::string &indexName, const CompositeFields &indexDefine); @@ -120,6 +123,7 @@ private: std::string tableName_; bool autoInc_ = false; // only 'INTEGER PRIMARY KEY' could be defined as 'AUTOINCREMENT' + TableSyncType tableSyncType_ = DEVICE_COOPERATION; std::string sql_; FieldInfoMap fields_; std::map primaryKey_; diff --git a/kv_store/frameworks/libs/distributeddb/common/src/db_constant.cpp b/kv_store/frameworks/libs/distributeddb/common/src/db_constant.cpp index abba6423..e19c6683 100644 --- a/kv_store/frameworks/libs/distributeddb/common/src/db_constant.cpp +++ b/kv_store/frameworks/libs/distributeddb/common/src/db_constant.cpp @@ -71,7 +71,7 @@ const std::string DBConstant::TIMESTAMP_ALIAS = "naturalbase_rdb_aux_timestamp"; const std::string DBConstant::LOG_TABLE_VERSION_1 = "1.0"; const std::string DBConstant::LOG_TABLE_VERSION_2 = "2.0"; -const std::string DBConstant::LOG_TABLE_VERSION_CURRENT = "2.0"; +const std::string DBConstant::LOG_TABLE_VERSION_CURRENT = "3.0"; const std::string DBConstant::LOG_TABLE_VERSION_KEY = "log_table_version"; diff --git a/kv_store/frameworks/libs/distributeddb/common/src/query.cpp b/kv_store/frameworks/libs/distributeddb/common/src/query.cpp index a1b566be..26939153 100644 --- a/kv_store/frameworks/libs/distributeddb/common/src/query.cpp +++ b/kv_store/frameworks/libs/distributeddb/common/src/query.cpp @@ -30,6 +30,11 @@ Query Query::Select(const std::string &tableName) return query; } +Query &Query::FromTable(const std::vector &tableNames) +{ + return *this; +} + Query &Query::BeginGroup() { queryExpression_.BeginGroup(); diff --git a/kv_store/frameworks/libs/distributeddb/common/src/relational/relational_schema_object.cpp b/kv_store/frameworks/libs/distributeddb/common/src/relational/relational_schema_object.cpp index 18462ad6..075be5bf 100644 --- a/kv_store/frameworks/libs/distributeddb/common/src/relational/relational_schema_object.cpp +++ b/kv_store/frameworks/libs/distributeddb/common/src/relational/relational_schema_object.cpp @@ -335,6 +335,11 @@ int RelationalSchemaObject::ParseCheckTableInfo(const JsonObject &inJsonObject) if (errCode != E_OK) { return errCode; } + + errCode = ParseCheckTableSyncType(inJsonObject, resultTable); + if (errCode != E_OK) { + return errCode; + } errCode = ParseCheckTableIndex(inJsonObject, resultTable); if (errCode != E_OK) { return errCode; @@ -479,6 +484,19 @@ int RelationalSchemaObject::ParseCheckTablePrimaryKey(const JsonObject &inJsonOb return errCode; } +int RelationalSchemaObject::ParseCheckTableSyncType(const JsonObject &inJsonObject, TableInfo &resultTable) +{ + FieldValue fieldValue; + int errCode = GetMemberFromJsonObject(inJsonObject, "TABLE_SYNC_TYPE", FieldType::LEAF_FIELD_INTEGER, + false, fieldValue); + if (errCode == E_OK) { + resultTable.SetTableSyncType(static_cast(fieldValue.integerValue)); + } else if (errCode != -E_NOT_FOUND) { + return errCode; + } + return E_OK; // if there is no "TABLE_SYNC_TYPE" filed, the table_sync_type is DEVICE_COOPERATION +} + int RelationalSchemaObject::ParseCheckTableIndex(const JsonObject &inJsonObject, TableInfo &resultTable) { if (!inJsonObject.IsFieldPathExist(FieldPath {"INDEX"})) { // INDEX is not necessary diff --git a/kv_store/frameworks/libs/distributeddb/common/src/relational/table_info.cpp b/kv_store/frameworks/libs/distributeddb/common/src/relational/table_info.cpp index 797d8b45..b43a3ca7 100644 --- a/kv_store/frameworks/libs/distributeddb/common/src/relational/table_info.cpp +++ b/kv_store/frameworks/libs/distributeddb/common/src/relational/table_info.cpp @@ -206,6 +206,16 @@ bool TableInfo::GetAutoIncrement() const return autoInc_; } +void TableInfo::SetTableSyncType(TableSyncType tableSyncType) +{ + tableSyncType_ = tableSyncType; +} + +TableSyncType TableInfo::GetTableSyncType() const +{ + return tableSyncType_; +} + const std::string &TableInfo::GetCreateTableSql() const { return sql_; @@ -634,6 +644,7 @@ std::string TableInfo::ToTableInfoString(const std::string &schemaVersion) const attrStr += R"("PRIMARY_KEY": [)" + primaryKey + "]"; } } + attrStr += R"(,"TABLE_SYNC_TYPE": )" + std::to_string(static_cast(tableSyncType_)); AddIndexDefineString(attrStr); attrStr += "}"; return attrStr; diff --git a/kv_store/frameworks/libs/distributeddb/distributeddb.gni b/kv_store/frameworks/libs/distributeddb/distributeddb.gni index 87c4b466..64a86a90 100644 --- a/kv_store/frameworks/libs/distributeddb/distributeddb.gni +++ b/kv_store/frameworks/libs/distributeddb/distributeddb.gni @@ -128,6 +128,7 @@ distributeddb_src = [ "${distributeddb_path}/storage/src/relationaldb_properties.cpp", "${distributeddb_path}/storage/src/result_entries_window.cpp", "${distributeddb_path}/storage/src/single_ver_natural_store_commit_notify_data.cpp", + "${distributeddb_path}/storage/src/sqlite/cloud_sync_log_table_manager.cpp", "${distributeddb_path}/storage/src/sqlite/collaboration_log_table_manager.cpp", "${distributeddb_path}/storage/src/sqlite/log_table_manager_factory.cpp", "${distributeddb_path}/storage/src/sqlite/query_object.cpp", diff --git a/kv_store/frameworks/libs/distributeddb/include/query.h b/kv_store/frameworks/libs/distributeddb/include/query.h index 1a1f1d90..da287c7c 100644 --- a/kv_store/frameworks/libs/distributeddb/include/query.h +++ b/kv_store/frameworks/libs/distributeddb/include/query.h @@ -33,7 +33,7 @@ public: DB_API static Query Select(); DB_API static Query Select(const std::string &tableName); - DB_API Query FromTable(const std::vector &tableNames); + DB_API Query &FromTable(const std::vector &tableNames); template DB_API Query &EqualTo(const std::string &field, const T &value) diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/cloud_store_types.h b/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/cloud_store_types.h index 7d54fa98..40f6dc3c 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/cloud_store_types.h +++ b/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/cloud_store_types.h @@ -25,15 +25,13 @@ #include "store_types.h" namespace DistributedDB { -const std::string CLOUD_EXTEND_FIELD[] = { "GID_FIELD", "CREATE_FIELD", "MODIFY_FIELD" }; -const uint32_t MAX_UPLOAD_SIZE = 1024 * 1024 * 8; enum TableSyncType { DEVICE_COOPERATION = 0, CLOUD_COOPERATION = 1, }; enum ClearMode { - DATA_INCLUDE = 0, + FLAG_AND_DATA = 0, FLAG_ONLY = 1, }; @@ -46,36 +44,11 @@ struct Asset { std::string size; std::string hash; }; - +using Nil = std::monostate; using Assets = std::vector; using Bytes = std::vector; -using CloudValue = std::variant; -using VBucket = std::map; - -enum ProcessStatus { - PREPARED = 0, - PROCESSING = 1, - FINISHED = 2, -}; - -struct Info { - uint32_t batchIndex = 0; - uint32_t total = 0; - uint32_t successCount = 0; // merge or upload success count - uint32_t failCount = 0; -}; - -struct TableProcessInfo { - ProcessStatus process = PREPARED; - Info downLoadInfo; - Info upLoadInfo; -}; - -struct SyncProcess { - ProcessStatus process = PREPARED; - DBStatus errCode = OK; - std::map tableProcess; -}; +using Type = std::variant; +using VBucket = std::map; struct Field { std::string colName; @@ -93,17 +66,5 @@ struct DataBaseSchema { std::vector tables; }; -struct CloudSyncBatch { - std::vector record; - std::vector extend; - std::vector rowid; -}; - -struct CloudSyncData { - const std::string tablename; - CloudSyncBatch insData; - CloudSyncBatch updData; - CloudSyncBatch delData; -}; } // namespace DistributedDB #endif // CLOUD_STORE_TYPE_H diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/icloud_db.h b/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/icloud_db.h index d9ca9445..f5437a08 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/icloud_db.h +++ b/kv_store/frameworks/libs/distributeddb/interfaces/include/cloud/icloud_db.h @@ -33,8 +33,7 @@ public: std::vector &extend) = 0; virtual DBStatus BatchUpdate(const std::string &tableName, std::vector &&record, std::vector &extend) = 0; - virtual DBStatus BatchDelete(const std::string &tableName, std::vector &&record, - std::vector &extend) = 0; + virtual DBStatus BatchDelete(const std::string &tableName, std::vector &extend) = 0; /** ** param[out] data: query data **/ diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/include/relational/relational_store_delegate.h b/kv_store/frameworks/libs/distributeddb/interfaces/include/relational/relational_store_delegate.h index 3e171488..5fc3c130 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/include/relational/relational_store_delegate.h +++ b/kv_store/frameworks/libs/distributeddb/interfaces/include/relational/relational_store_delegate.h @@ -41,13 +41,18 @@ public: uint32_t iterateTimes = 0; }; - DB_API virtual DBStatus CreateDistributedTable(const std::string &tableName, - TableSyncType = DEVICE_COOPERATION) = 0; + DB_API DBStatus CreateDistributedTable(const std::string &tableName, TableSyncType type = DEVICE_COOPERATION) + { + return CreateDistributedTableInner(tableName, type); + } DB_API virtual DBStatus Sync(const std::vector &devices, SyncMode mode, const Query &query, const SyncStatusCallback &onComplete, bool wait) = 0; - DB_API virtual DBStatus RemoveDeviceData(const std::string &device, ClearMode = DATA_INCLUDE) = 0; + DB_API DBStatus RemoveDeviceData(const std::string &device, ClearMode mode = FLAG_AND_DATA) + { + return RemoveDeviceDataInner(device, mode); + } DB_API virtual DBStatus RemoveDeviceData(const std::string &device, const std::string &tableName) = 0; @@ -58,13 +63,15 @@ public: // remove all device data DB_API virtual DBStatus RemoveDeviceData() = 0; - DB_API virtual DBStatus Sync(const std::vector &devices, SyncMode mode, - const Query &query, const std::function &onProcess, - int64_t waitTime) = 0; + DB_API virtual DBStatus Sync(const std::vector &devices, SyncMode mode, const Query &query, + const std::function &process)> &onProcess, int64_t waitTime) = 0; DB_API virtual DBStatus SetCloudDB(const std::shared_ptr &cloudDb) = 0; DB_API virtual DBStatus SetCloudDbSchema(const DataBaseSchema &schema) = 0; +protected: + virtual DBStatus RemoveDeviceDataInner(const std::string &device, ClearMode mode) = 0; + virtual DBStatus CreateDistributedTableInner(const std::string &tableName, TableSyncType type) = 0; }; } // namespace DistributedDB #endif // RELATIONAL_STORE_DELEGATE_H \ No newline at end of file diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/include/runtime_config.h b/kv_store/frameworks/libs/distributeddb/interfaces/include/runtime_config.h index b5824447..e1bfbe48 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/include/runtime_config.h +++ b/kv_store/frameworks/libs/distributeddb/interfaces/include/runtime_config.h @@ -19,6 +19,7 @@ #include #include +#include "cloud/icloud_data_translate.h" #include "iprocess_communicator.h" #include "iprocess_system_api_adapter.h" #include "ithread_pool.h" @@ -54,6 +55,8 @@ public: DB_API static void SetTranslateToDeviceIdCallback(const TranslateToDeviceIdCallback &callback); DB_API static void SetThreadPool(const std::shared_ptr &threadPool); + + DB_API static void SetCloudTranslate(const std::shared_ptr &dataTranslate); private: static std::mutex communicatorMutex_; static std::mutex multiUserMutex_; diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/include/store_observer.h b/kv_store/frameworks/libs/distributeddb/interfaces/include/store_observer.h index 0228b2da..3eebbb60 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/include/store_observer.h +++ b/kv_store/frameworks/libs/distributeddb/interfaces/include/store_observer.h @@ -16,8 +16,8 @@ #ifndef STORE_OBSERVER_H #define STORE_OBSERVER_H -#include "store_changed_data.h" #include "cloud/cloud_store_types.h" +#include "store_changed_data.h" namespace DistributedDB { @@ -31,7 +31,7 @@ struct ChangedData { std::string tableName; // CLOUD_COOPERATION mode, primaryData store primary keys // primayData store row id if have no data - std::vector> primaryData[OP_BUTT]; + std::vector> primaryData[OP_BUTT]; std::vector field; }; @@ -47,9 +47,9 @@ public: virtual ~StoreObserver() {} // Data change callback - virtual void OnChange(const StoreChangedData &data) = 0; + virtual void OnChange(const StoreChangedData &data) {}; - virtual void OnChange(Origin origin, const std::string &originalId, ChangedData &&data) const {}; + virtual void OnChange(Origin origin, const std::string &originalId, ChangedData &&data) {}; }; } // namespace DistributedDB diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/include/store_types.h b/kv_store/frameworks/libs/distributeddb/interfaces/include/store_types.h index 21f693ed..560cb389 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/include/store_types.h +++ b/kv_store/frameworks/libs/distributeddb/interfaces/include/store_types.h @@ -68,6 +68,8 @@ enum DBStatus { RATE_LIMIT, DATA_HANDLE_ERROR, // remote handle data failed CONSTRAINT, // constraint check failed in sqlite + CLOUD_ERROR, // cloud error + QUERY_END, // Indicates that query function has queried last data from cloud }; struct KvStoreConfig { @@ -110,6 +112,9 @@ enum SyncMode { SYNC_MODE_PUSH_ONLY, SYNC_MODE_PULL_ONLY, SYNC_MODE_PUSH_PULL, + SYNC_MODE_CLOUD_MERGE = 4, + SYNC_MODE_CLOUD_FORCE_PUSH, + SYNC_MODE_CLOUD_FORCE_PULL, }; enum ConflictResolvePolicy { @@ -121,12 +126,38 @@ struct TableStatus { std::string tableName; DBStatus status; }; + +enum ProcessStatus { + PREPARED = 0, + PROCESSING = 1, + FINISHED = 2, +}; + +struct Info { + uint32_t batchIndex = 0; + uint32_t total = 0; + uint32_t successCount = 0; // merge or upload success count + uint32_t failCount = 0; +}; + +struct TableProcessInfo { + ProcessStatus process = PREPARED; + Info downLoadInfo; + Info upLoadInfo; +}; + +struct SyncProcess { + ProcessStatus process = PREPARED; + DBStatus errCode = OK; + std::map tableProcess; +}; + using KvStoreCorruptionHandler = std::function; using StoreCorruptionHandler = std::function; using SyncStatusCallback = std::function> &devicesMap)>; - +using SyncProcessCallback = std::function &process)>; struct RemoteCondition { std::string sql; // The sql statement; std::vector bindArgs; // The bind args. diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.cpp b/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.cpp index d92004ed..06da567f 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.cpp +++ b/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.cpp @@ -38,24 +38,29 @@ RelationalStoreDelegateImpl::~RelationalStoreDelegateImpl() conn_ = nullptr; }; -DBStatus RelationalStoreDelegateImpl::RemoveDeviceData(const std::string &device, ClearMode mode) +DBStatus RelationalStoreDelegateImpl::RemoveDeviceDataInner(const std::string &device, ClearMode mode) { return RemoveDeviceData(device, ""); } -DBStatus RelationalStoreDelegateImpl::CreateDistributedTable(const std::string &tableName, TableSyncType type) +DBStatus RelationalStoreDelegateImpl::CreateDistributedTableInner(const std::string &tableName, TableSyncType type) { if (!ParamCheckUtils::CheckRelationalTableName(tableName)) { LOGE("invalid table name."); return INVALID_ARGS; } + if (!(type == DEVICE_COOPERATION || type == CLOUD_COOPERATION)) { + LOGE("invalid table sync type."); + return INVALID_ARGS; + } + if (conn_ == nullptr) { LOGE("[RelationalStore Delegate] Invalid connection for operation!"); return DB_ERROR; } - int errCode = conn_->CreateDistributedTable(tableName); + int errCode = conn_->CreateDistributedTable(tableName, type); if (errCode != E_OK) { LOGE("[RelationalStore Delegate] Create Distributed table failed:%d", errCode); return TransferDBErrno(errCode); @@ -177,7 +182,7 @@ DBStatus RelationalStoreDelegateImpl::RemoveDeviceData() } DBStatus RelationalStoreDelegateImpl::Sync(const std::vector &devices, SyncMode mode, const Query &query, - const std::function &onProcess, int64_t waitTime) + const std::function&)> &onProcess, int64_t waitTime) { return OK; } diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.h b/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.h index b3d0b1ee..ce5b598b 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.h +++ b/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_delegate_impl.h @@ -32,9 +32,9 @@ public: DBStatus Sync(const std::vector &devices, SyncMode mode, const Query &query, const SyncStatusCallback &onComplete, bool wait) override; - DBStatus RemoveDeviceData(const std::string &device, ClearMode = DATA_INCLUDE) override; + DBStatus RemoveDeviceDataInner(const std::string &device, ClearMode mode) override; - DBStatus CreateDistributedTable(const std::string &tableName, TableSyncType = DEVICE_COOPERATION) override; + DBStatus CreateDistributedTableInner(const std::string &tableName, TableSyncType type) override; DBStatus RemoveDeviceData(const std::string &device, const std::string &tableName) override; @@ -49,7 +49,7 @@ public: DBStatus RemoveDeviceData() override; DBStatus Sync(const std::vector &devices, SyncMode mode, const Query &query, - const std::function &onProcess, int64_t waitTime) override; + const std::function &)> &onProcess, int64_t waitTime) override; DBStatus SetCloudDB(const std::shared_ptr &cloudDb) override; diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_sqlite_ext.cpp b/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_sqlite_ext.cpp index 65ab912f..7607b849 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_sqlite_ext.cpp +++ b/kv_store/frameworks/libs/distributeddb/interfaces/src/relational/relational_store_sqlite_ext.cpp @@ -122,6 +122,16 @@ public: return (curTime * TO_100_NS) + currentIncCount_; // Currently Timestamp is uint64_t } + static int GetSysCurrentRawTime(uint64_t &curTime) + { + int errCode = GetCurrentSysTimeInMicrosecond(curTime); + if (errCode != E_OK) { + return errCode; + } + curTime *= TO_100_NS; + return E_OK; + } + // Init the TimeHelper static void Initialize(Timestamp maxTimestamp) { @@ -249,9 +259,24 @@ void GetSysTime(sqlite3_context *ctx, int argc, sqlite3_value **argv) sqlite3_result_int64(ctx, (sqlite3_int64)TimeHelper::GetTime(timeOffset)); } +void GetRawSysTime(sqlite3_context *ctx, int argc, sqlite3_value **argv) +{ + if (ctx == nullptr || argc != 0 || argv == nullptr) { // 0: function need zero parameter + return; + } + + uint64_t curTime = 0; + int errCode = TimeHelper::GetSysCurrentRawTime(curTime); + if (errCode != E_OK) { + sqlite3_result_error(ctx, "get raw sys time failed.", errCode); + return; + } + sqlite3_result_int64(ctx, (sqlite3_int64)(curTime)); +} + void GetLastTime(sqlite3_context *ctx, int argc, sqlite3_value **argv) { - if (ctx == nullptr || argc != 0 || argv == nullptr) { // 0: function need one parameter + if (ctx == nullptr || argc != 0 || argv == nullptr) { // 0: function need zero parameter return; } @@ -265,6 +290,13 @@ int RegisterGetSysTime(sqlite3 *db) return RegisterFunction(db, "get_sys_time", 1, nullptr, func); } +int RegisterGetRawSysTime(sqlite3 *db) +{ + TransactFunc func; + func.xFunc = &GetRawSysTime; + return RegisterFunction(db, "get_raw_sys_time", 0, nullptr, func); +} + int RegisterGetLastTime(sqlite3 *db) { TransactFunc func; @@ -413,6 +445,7 @@ void PostHandle(sqlite3 *db) RegisterCalcHash(db); RegisterGetSysTime(db); RegisterGetLastTime(db); + RegisterGetRawSysTime(db); (void)sqlite3_set_droptable_handle(db, &ClearTheLogAfterDropTable); (void)sqlite3_busy_timeout(db, BUSY_TIMEOUT); std::string recursiveTrigger = "PRAGMA recursive_triggers = ON;"; diff --git a/kv_store/frameworks/libs/distributeddb/interfaces/src/runtime_config.cpp b/kv_store/frameworks/libs/distributeddb/interfaces/src/runtime_config.cpp index 19b72c43..b94ef7be 100644 --- a/kv_store/frameworks/libs/distributeddb/interfaces/src/runtime_config.cpp +++ b/kv_store/frameworks/libs/distributeddb/interfaces/src/runtime_config.cpp @@ -141,5 +141,9 @@ void RuntimeConfig::SetThreadPool(const std::shared_ptr &threadPool { RuntimeContext::GetInstance()->SetThreadPool(threadPool); } + +void RuntimeConfig::SetCloudTranslate(const std::shared_ptr &dataTranslate) +{ +} } // namespace DistributedDB #endif \ No newline at end of file diff --git a/kv_store/frameworks/libs/distributeddb/storage/include/isync_interface.h b/kv_store/frameworks/libs/distributeddb/storage/include/isync_interface.h index d64b3bba..893305b1 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/include/isync_interface.h +++ b/kv_store/frameworks/libs/distributeddb/storage/include/isync_interface.h @@ -67,6 +67,8 @@ public: virtual int GetAllMetaKeys(std::vector &keys) const = 0; virtual const DBProperties &GetDbProperties() const = 0; + + virtual int GetSecurityOption(SecurityOption &option) const = 0; }; } // namespace DistributedDB diff --git a/kv_store/frameworks/libs/distributeddb/storage/include/relational_store_connection.h b/kv_store/frameworks/libs/distributeddb/storage/include/relational_store_connection.h index bd48de49..4d529cd8 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/include/relational_store_connection.h +++ b/kv_store/frameworks/libs/distributeddb/storage/include/relational_store_connection.h @@ -50,7 +50,7 @@ public: virtual int Close() = 0; virtual int SyncToDevice(SyncInfo &info) = 0; virtual std::string GetIdentifier() = 0; - virtual int CreateDistributedTable(const std::string &tableName) = 0; + virtual int CreateDistributedTable(const std::string &tableName, TableSyncType syncType) = 0; virtual int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier ¬ifier) = 0; virtual int RemoveDeviceData() = 0; diff --git a/kv_store/frameworks/libs/distributeddb/storage/include/sync_generic_interface.h b/kv_store/frameworks/libs/distributeddb/storage/include/sync_generic_interface.h index 3954a4d0..1e3e2d52 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/include/sync_generic_interface.h +++ b/kv_store/frameworks/libs/distributeddb/storage/include/sync_generic_interface.h @@ -83,7 +83,7 @@ public: return true; } - virtual int GetSecurityOption(SecurityOption &option) const + int GetSecurityOption(SecurityOption &option) const override { return -E_NOT_SUPPORT; } diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/cloud_sync_log_table_manager.cpp b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/cloud_sync_log_table_manager.cpp new file mode 100644 index 00000000..1391e4f3 --- /dev/null +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/cloud_sync_log_table_manager.cpp @@ -0,0 +1,120 @@ +/* + * 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_sync_log_table_manager.h" + +namespace DistributedDB { +std::string CloudSyncLogTableManager::CalcPrimaryKeyHash(const std::string &references, const TableInfo &table, + const std::string &identity) +{ + (void)identity; + std::string sql; + if (table.GetPrimaryKey().size() == 1) { + sql = "calc_hash(" + references + "'" + table.GetPrimaryKey().at(0) + "')"; + } else { + std::set primaryKeySet; // we need sort primary key by name + for (const auto &it : table.GetPrimaryKey()) { + primaryKeySet.emplace(it.second); + } + sql = "calc_hash("; + for (const auto &it : primaryKeySet) { + sql += "calc_hash(" + references + "'" + it + "')||"; + } + sql.pop_back(); + sql.pop_back(); + sql += ")"; + } + return sql; +} + +void CloudSyncLogTableManager::GetIndexSql(const TableInfo &table, std::vector &schema) +{ + const std::string tableName = GetLogTableName(table); + + std::string indexTimestampFlagGid = "CREATE INDEX IF NOT EXISTS " + tableName + + "_cloud_time_flag_gid_index ON " + tableName + "(timestamp, flag, cloud_gid);"; + schema.emplace_back(indexTimestampFlagGid); + + std::string indexHashkey = "CREATE INDEX IF NOT EXISTS " + tableName + + "_cloud_hashkey_index ON " + tableName + "(hash_key);"; + schema.emplace_back(indexHashkey); +} + +std::string CloudSyncLogTableManager::GetPrimaryKeySql(const TableInfo &table) +{ + return "PRIMARY KEY(hash_key)"; +} + +// The parameter "identity" is a hash string that identifies a device. The same for the next two functions. +std::string CloudSyncLogTableManager::GetInsertTrigger(const TableInfo &table, const std::string &identity) +{ + std::string logTblName = DBConstant::RELATIONAL_PREFIX + table.GetTableName() + "_log"; + std::string insertTrigger = "CREATE TRIGGER IF NOT EXISTS "; + insertTrigger += "naturalbase_rdb_" + table.GetTableName() + "_ON_INSERT AFTER INSERT \n"; + insertTrigger += "ON '" + table.GetTableName() + "'\n"; + insertTrigger += "WHEN (SELECT count(*) from " + DBConstant::RELATIONAL_PREFIX + "metadata "; + insertTrigger += "WHERE key = 'log_trigger_switch' AND value = 'true')\n"; + insertTrigger += "BEGIN\n"; + insertTrigger += "\t INSERT OR REPLACE INTO " + logTblName; + insertTrigger += " (data_key, device, ori_device, timestamp, wtimestamp, flag, hash_key, cloud_gid)"; + insertTrigger += " VALUES (new.rowid, '', '',"; + insertTrigger += " get_raw_sys_time(), get_raw_sys_time(), 0x02, "; + insertTrigger += CalcPrimaryKeyHash("NEW.", table, identity) + ", '');\n"; + insertTrigger += "END;"; + return insertTrigger; +} + +std::string CloudSyncLogTableManager::GetUpdateTrigger(const TableInfo &table, const std::string &identity) +{ + std::string logTblName = DBConstant::RELATIONAL_PREFIX + table.GetTableName() + "_log"; + std::string updateTrigger = "CREATE TRIGGER IF NOT EXISTS "; + updateTrigger += "naturalbase_rdb_" + table.GetTableName() + "_ON_UPDATE AFTER UPDATE \n"; + updateTrigger += "ON '" + table.GetTableName() + "'\n"; + updateTrigger += "WHEN (SELECT count(*) from " + DBConstant::RELATIONAL_PREFIX + "metadata "; + updateTrigger += "WHERE key = 'log_trigger_switch' AND value = 'true')\n"; + updateTrigger += "BEGIN\n"; + if (table.GetPrimaryKey().size() == 1 && table.GetPrimaryKey().at(0) == "rowid") { + updateTrigger += "\t UPDATE " + DBConstant::RELATIONAL_PREFIX + table.GetTableName() + "_log"; + updateTrigger += " SET timestamp=get_raw_sys_time(), device='', flag=0x02"; + updateTrigger += " WHERE data_key = OLD.rowid;\n"; + } else { // the row id may be modified + updateTrigger += "\t UPDATE " + logTblName; + updateTrigger += " SET data_key=-1,timestamp=get_raw_sys_time(), device='', flag=0x03"; + updateTrigger += " WHERE hash_key=" + CalcPrimaryKeyHash("OLD.", table, identity) + ";\n"; + updateTrigger += "\t INSERT OR REPLACE INTO " + logTblName + " VALUES (NEW.rowid, '', '', "; + updateTrigger += "get_raw_sys_time(), (select wtimestamp from " + logTblName + " where hash_key = "; + updateTrigger += CalcPrimaryKeyHash("OLD.", table, identity) + "), 0x02, "; + updateTrigger += CalcPrimaryKeyHash("NEW.", table, identity) + ", '');\n"; + } + updateTrigger += "END;"; + return updateTrigger; +} + +std::string CloudSyncLogTableManager::GetDeleteTrigger(const TableInfo &table, const std::string &identity) +{ + (void)identity; + std::string deleteTrigger = "CREATE TRIGGER IF NOT EXISTS "; + deleteTrigger += "naturalbase_rdb_" + table.GetTableName() + "_ON_DELETE BEFORE DELETE \n"; + deleteTrigger += "ON '" + table.GetTableName() + "'\n"; + deleteTrigger += "WHEN (SELECT count(*) from " + DBConstant::RELATIONAL_PREFIX + "metadata "; + deleteTrigger += "WHERE key = 'log_trigger_switch' AND VALUE = 'true')\n"; + deleteTrigger += "BEGIN\n"; + deleteTrigger += "\t UPDATE " + DBConstant::RELATIONAL_PREFIX + table.GetTableName() + "_log"; + deleteTrigger += " SET data_key=-1,flag=0x03,timestamp=get_raw_sys_time()"; + deleteTrigger += " WHERE data_key = OLD.rowid;"; + deleteTrigger += "END;"; + return deleteTrigger; +} +} // DistributedDB \ No newline at end of file diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/cloud_sync_log_table_manager.h b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/cloud_sync_log_table_manager.h new file mode 100644 index 00000000..17bb59a1 --- /dev/null +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/cloud_sync_log_table_manager.h @@ -0,0 +1,43 @@ +/* + * 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 CLOUD_SYNC_LOG_TABLE_MANAGER_H +#define CLOUD_SYNC_LOG_TABLE_MANAGER_H + +#include "sqlite_log_table_manager.h" + +namespace DistributedDB { +class CloudSyncLogTableManager : public SqliteLogTableManager { +public: + CloudSyncLogTableManager() = default; + ~CloudSyncLogTableManager() override = default; + + // The parameter "references" is "", "NEW." or "OLD.". "identity" is a hash string that identifies a device. + std::string CalcPrimaryKeyHash(const std::string &references, const TableInfo &table, + const std::string &identity) override; + +private: + void GetIndexSql(const TableInfo &table, std::vector &schema) override; + std::string GetPrimaryKeySql(const TableInfo &table) override; + + // The parameter "identity" is a hash string that identifies a device. The same for the next two functions. + std::string GetInsertTrigger(const TableInfo &table, const std::string &identity) override; + std::string GetUpdateTrigger(const TableInfo &table, const std::string &identity) override; + std::string GetDeleteTrigger(const TableInfo &table, const std::string &identity) override; +}; + +} // DistributedDB + +#endif //DISTRIBUTED_UT_CLOUD_SYNC_LOG_TABLE_MANAGER_H diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/collaboration_log_table_manager.cpp b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/collaboration_log_table_manager.cpp index 2132dde0..5110060c 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/collaboration_log_table_manager.cpp +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/collaboration_log_table_manager.cpp @@ -84,7 +84,7 @@ std::string CollaborationLogTableManager::GetUpdateTrigger(const TableInfo &tabl updateTrigger += "\t INSERT OR REPLACE INTO " + logTblName + " VALUES (NEW.rowid, '', '', get_sys_time(0), " "get_last_time(), CASE WHEN (" + CalcPrimaryKeyHash("NEW.", table, identity) + " != " + CalcPrimaryKeyHash("NEW.", table, identity) + ") THEN 0x02 ELSE 0x22 END, " + - CalcPrimaryKeyHash("NEW.", table, identity) + ");\n"; + CalcPrimaryKeyHash("NEW.", table, identity) + ", '');\n"; } updateTrigger += "END;"; return updateTrigger; diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/log_table_manager_factory.cpp b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/log_table_manager_factory.cpp index 841fa48c..a6f613f1 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/log_table_manager_factory.cpp +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/log_table_manager_factory.cpp @@ -14,16 +14,21 @@ */ #include "log_table_manager_factory.h" - +#include "cloud_sync_log_table_manager.h" #include "collaboration_log_table_manager.h" #include "split_device_log_table_manager.h" namespace DistributedDB { -std::unique_ptr LogTableManagerFactory::GetTableManager(DistributedTableMode mode) +std::unique_ptr LogTableManagerFactory::GetTableManager(DistributedTableMode mode, + TableSyncType syncType) { - if (mode == DistributedTableMode::COLLABORATION) { - return std::make_unique(); + if (syncType == CLOUD_COOPERATION) { + return std::make_unique(); + } else { + if (mode == DistributedTableMode::COLLABORATION) { + return std::make_unique(); + } + return std::make_unique(); } - return std::make_unique(); } } \ No newline at end of file diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/log_table_manager_factory.h b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/log_table_manager_factory.h index e534ea32..b31f9d50 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/log_table_manager_factory.h +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/log_table_manager_factory.h @@ -16,13 +16,15 @@ #ifndef LOG_TABLE_MANAGER_FACTORY_H #define LOG_TABLE_MANAGER_FACTORY_H #include + +#include "cloud/cloud_store_types.h" #include "sqlite_log_table_manager.h" #include "types_export.h" namespace DistributedDB { class LogTableManagerFactory final { public: - static std::unique_ptr GetTableManager(DistributedTableMode mode); + static std::unique_ptr GetTableManager(DistributedTableMode mode, TableSyncType syncType); private: LogTableManagerFactory() {} diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_database_upgrader.cpp b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_database_upgrader.cpp index a99792ab..cdc7b48d 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_database_upgrader.cpp +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_database_upgrader.cpp @@ -63,6 +63,12 @@ int SqliteRelationalDatabaseUpgrader::ExecuteUpgrade() return (errCode == -E_NOT_FOUND) ? E_OK : errCode; } + errCode = UpgradeLogTable(logTableVersion); + if (errCode != E_OK) { + LOGE("[Relational][Upgrade] Upgrade log table failed, err = %d.", errCode); + return errCode; + } + return UpgradeTrigger(logTableVersion); } @@ -75,9 +81,14 @@ int SqliteRelationalDatabaseUpgrader::EndUpgrade(bool isSuccess) return SQLiteUtils::RollbackTransaction(db_); } +bool SqliteRelationalDatabaseUpgrader::IsNewestVersion(const std::string &logTableVersion) +{ + return logTableVersion == DBConstant::LOG_TABLE_VERSION_CURRENT; +} + int SqliteRelationalDatabaseUpgrader::UpgradeTrigger(const std::string &logTableVersion) { - if (logTableVersion != DBConstant::LOG_TABLE_VERSION_1) { + if (IsNewestVersion(logTableVersion)) { LOGD("[Relational][Upgrade] No need upgrade trigger."); return E_OK; } @@ -98,10 +109,6 @@ int SqliteRelationalDatabaseUpgrader::UpgradeTrigger(const std::string &logTable } DistributedTableMode mode = schemaObject.GetTableMode(); - if (mode != DistributedTableMode::SPLIT_BY_DEVICE) { - return E_OK; - } - for (const auto &[tableName, tableInfo] : schemaObject.GetTables()) { std::string dropTriggerSql = "DROP TRIGGER IF EXISTS " + DBConstant::SYSTEM_TABLE_PREFIX + tableName + "_ON_UPDATE"; @@ -110,7 +117,7 @@ int SqliteRelationalDatabaseUpgrader::UpgradeTrigger(const std::string &logTable LOGE("[Relational][Upgrade] drop trigger failed.", errCode); return errCode; } - auto manager = LogTableManagerFactory::GetTableManager(mode); + auto manager = LogTableManagerFactory::GetTableManager(mode, tableInfo.GetTableSyncType()); errCode = manager->AddRelationalLogTableTrigger(db_, tableInfo, ""); if (errCode != E_OK) { LOGE("[Relational][Upgrade] recreate trigger failed.", errCode); @@ -119,4 +126,38 @@ int SqliteRelationalDatabaseUpgrader::UpgradeTrigger(const std::string &logTable } return E_OK; } + +int SqliteRelationalDatabaseUpgrader::UpgradeLogTable(const std::string &logTableVersion) +{ + if (IsNewestVersion(logTableVersion)) { + LOGD("[Relational][Upgrade] No need upgrade log table."); + return E_OK; + } + + // get schema from meta + std::string schemaDefine; + int errCode = SQLiteUtils::GetRelationalSchema(db_, schemaDefine); + if (errCode != E_OK) { + LOGW("[Relational][UpgradeLogTable] Get relational schema from meta return %d.", errCode); + return (errCode == -E_NOT_FOUND) ? E_OK : errCode; + } + + RelationalSchemaObject schemaObject; + errCode = schemaObject.ParseFromSchemaString(schemaDefine); + if (errCode != E_OK) { + LOGE("[Relational][UpgradeLogTable] Parse to relational schema failed.", errCode); + return errCode; + } + + for (const auto &item : schemaObject.GetTables()) { + std::string addColumnSql = "alter table " + DBConstant::RELATIONAL_PREFIX + item.first + + "_log add cloud_gid text after hash_key;"; + errCode = SQLiteUtils::ExecuteRawSQL(db_, addColumnSql); + if (errCode != E_OK) { + LOGE("[Relational][UpgradeLogTable] add column failed.", errCode); + return errCode; + } + } + return E_OK; +} } // namespace DistributedDB \ No newline at end of file diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_database_upgrader.h b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_database_upgrader.h index 5eeff9df..4debfc1e 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_database_upgrader.h +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_database_upgrader.h @@ -32,7 +32,9 @@ private: int ExecuteUpgrade(); int EndUpgrade(bool isSuccess); + bool IsNewestVersion(const std::string &logTableVersion); int UpgradeTrigger(const std::string &logTableVersion); + int UpgradeLogTable(const std::string &logTableVersion); sqlite3 *db_; }; } // namespace DistributedDB diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.cpp b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.cpp index 1d85cfa0..746a47eb 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.cpp +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.cpp @@ -94,7 +94,7 @@ void SQLiteRelationalStore::ReleaseResources() int SQLiteRelationalStore::CheckDBMode() { int errCode = E_OK; - auto *handle = GetHandle(false, errCode); + auto *handle = GetHandle(true, errCode); if (handle == nullptr) { return errCode; } @@ -404,7 +404,7 @@ void SQLiteRelationalStore::WakeUpSyncer() syncAbleEngine_->WakeUpSyncer(); } -int SQLiteRelationalStore::CreateDistributedTable(const std::string &tableName) +int SQLiteRelationalStore::CreateDistributedTable(const std::string &tableName, TableSyncType syncType) { auto mode = static_cast(sqliteStorageEngine_->GetProperties().GetIntProp( RelationalDBProperties::DISTRIBUTED_TABLE_MODE, DistributedTableMode::SPLIT_BY_DEVICE)); @@ -420,7 +420,7 @@ int SQLiteRelationalStore::CreateDistributedTable(const std::string &tableName) bool schemaChanged = false; int errCode = sqliteStorageEngine_->CreateDistributedTable(tableName, DBCommon::TransferStringToHex(localIdentity), - schemaChanged); + schemaChanged, syncType); if (errCode != E_OK) { LOGE("Create distributed table failed. %d", errCode); } diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.h b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.h index 7603a830..a66eac9c 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.h +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.h @@ -56,7 +56,7 @@ public: return storageEngine_; } - int CreateDistributedTable(const std::string &tableName); + int CreateDistributedTable(const std::string &tableName, TableSyncType syncType); int RemoveDeviceData(); int RemoveDeviceData(const std::string &device, const std::string &tableName); diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.cpp b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.cpp index 57fb3c36..37f3f2d2 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.cpp +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.cpp @@ -141,7 +141,7 @@ int SQLiteRelationalStoreConnection::RollBack() return errCode; } -int SQLiteRelationalStoreConnection::CreateDistributedTable(const std::string &tableName) +int SQLiteRelationalStoreConnection::CreateDistributedTable(const std::string &tableName, TableSyncType syncType) { auto *store = GetDB(); if (store == nullptr) { @@ -149,7 +149,7 @@ int SQLiteRelationalStoreConnection::CreateDistributedTable(const std::string &t return -E_INVALID_CONNECTION; } - int errCode = store->CreateDistributedTable(tableName); + int errCode = store->CreateDistributedTable(tableName, syncType); if (errCode != E_OK) { LOGE("[RelationalConnection] create distributed table failed. %d", errCode); } diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.h b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.h index 0c85ca89..27448106 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.h +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.h @@ -36,7 +36,7 @@ public: int Close() override; int SyncToDevice(SyncInfo &info) override; std::string GetIdentifier() override; - int CreateDistributedTable(const std::string &tableName) override; + int CreateDistributedTable(const std::string &tableName, TableSyncType syncType) override; int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier ¬ifier) override; int RemoveDeviceData() override; diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_relational_storage_engine.cpp b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_relational_storage_engine.cpp index 6b90670e..e9cf8dc8 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_relational_storage_engine.cpp +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_relational_storage_engine.cpp @@ -141,13 +141,17 @@ int SaveSchemaToMetaTable(SQLiteSingleVerRelationalStorageExecutor *handle, cons } int SQLiteSingleRelationalStorageEngine::CreateDistributedTable(const std::string &tableName, - const std::string &identity, bool &schemaChanged) + const std::string &identity, bool &schemaChanged, TableSyncType syncType) { std::lock_guard lock(schemaMutex_); RelationalSchemaObject schema = schema_; bool isUpgraded = false; if (schema.GetTable(tableName).GetTableName() == tableName) { LOGI("distributed table bas been created."); + if (schema.GetTable(tableName).GetTableSyncType() != syncType) { + LOGE("table sync type mismatch."); + return -E_TYPE_MISMATCH; + } isUpgraded = true; int errCode = UpgradeDistributedTable(tableName, schemaChanged); if (errCode != E_OK) { @@ -162,11 +166,11 @@ int SQLiteSingleRelationalStorageEngine::CreateDistributedTable(const std::strin schemaChanged = true; } - return CreateDistributedTable(tableName, isUpgraded, identity, schema); + return CreateDistributedTable(tableName, isUpgraded, identity, schema, syncType); } int SQLiteSingleRelationalStorageEngine::CreateDistributedTable(const std::string &tableName, bool isUpgraded, - const std::string &identity, RelationalSchemaObject &schema) + const std::string &identity, RelationalSchemaObject &schema, TableSyncType tableSyncType) { LOGD("Create distributed table."); int errCode = E_OK; @@ -185,7 +189,9 @@ int SQLiteSingleRelationalStorageEngine::CreateDistributedTable(const std::strin auto mode = static_cast(properties_.GetIntProp( RelationalDBProperties::DISTRIBUTED_TABLE_MODE, DistributedTableMode::SPLIT_BY_DEVICE)); TableInfo table; - errCode = handle->CreateDistributedTable(tableName, mode, isUpgraded, identity, table); + table.SetTableName(tableName); + table.SetTableSyncType(tableSyncType); + errCode = handle->CreateDistributedTable(mode, isUpgraded, identity, table, tableSyncType); if (errCode != E_OK) { LOGE("create distributed table failed. %d", errCode); (void)handle->Rollback(); diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_relational_storage_engine.h b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_relational_storage_engine.h index 540f499c..ff51bf64 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_relational_storage_engine.h +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_relational_storage_engine.h @@ -34,7 +34,8 @@ public: RelationalSchemaObject GetSchema() const; - int CreateDistributedTable(const std::string &tableName, const std::string &identity, bool &schemaChanged); + int CreateDistributedTable(const std::string &tableName, const std::string &identity, bool &schemaChanged, + TableSyncType syncType); int CleanDistributedDeviceTable(std::vector &missingTables); @@ -55,7 +56,7 @@ private: int UpgradeDistributedTable(const std::string &tableName, bool &schemaChanged); int CreateDistributedTable(const std::string &tableName, bool isUpgraded, const std::string &identity, - RelationalSchemaObject &schema); + RelationalSchemaObject &schema, TableSyncType tableSyncType); int CreateRelationalMetaTable(sqlite3 *db); diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/split_device_log_table_manager.cpp b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/split_device_log_table_manager.cpp index 6f50bc5b..1f06f5b2 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/split_device_log_table_manager.cpp +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/split_device_log_table_manager.cpp @@ -72,7 +72,7 @@ std::string SplitDeviceLogTableManager::GetUpdateTrigger(const TableInfo &table, updateTrigger += "\t INSERT OR REPLACE INTO " + logTblName + " VALUES (NEW.rowid, '', '', get_sys_time(0), " "get_last_time(), CASE WHEN (" + CalcPrimaryKeyHash("NEW.", table, identity) + " != " + CalcPrimaryKeyHash("NEW.", table, identity) + ") THEN 0x02 ELSE 0x22 END, " + - CalcPrimaryKeyHash("NEW.", table, identity) + ");\n"; + CalcPrimaryKeyHash("NEW.", table, identity) + ", '');\n"; } updateTrigger += "END;"; return updateTrigger; diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_log_table_manager.cpp b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_log_table_manager.cpp index f1755b7b..6a449a78 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_log_table_manager.cpp +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_log_table_manager.cpp @@ -44,7 +44,9 @@ int SqliteLogTableManager::CreateRelationalLogTable(sqlite3 *db, const TableInfo "timestamp INT NOT NULL," \ "wtimestamp INT NOT NULL," \ "flag INT NOT NULL," \ - "hash_key BLOB NOT NULL," + primaryKey + ");"; + "hash_key BLOB NOT NULL," \ + "cloud_gid TEXT," + + primaryKey + ");"; std::vector logTableSchema; logTableSchema.emplace_back(createTableSql); GetIndexSql(table, logTableSchema); diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h index ece7e1d5..dcd0aa34 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h @@ -123,7 +123,7 @@ private: int BindObjNodes(sqlite3_stmt *&statement, int &index) const; int GetSubscribeCondition(const std::string &accessStr, std::string &conditionStr); std::string MapKeysInToSql(size_t keysNum) const; - int BindKeysToStmt(const std::set &keys, sqlite3_stmt *&countStmt, int &index) const; + int BindKeysToStmt(const std::set &keys, sqlite3_stmt *&statement, int &index) const; std::string MapKeysInSubCondition(const std::string &accessStr) const; // For InKeys. // Return the left string of symbol in compare clause. diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_continue_token.cpp b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_continue_token.cpp index 3c3fbbac..c711dabe 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_continue_token.cpp +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_continue_token.cpp @@ -18,8 +18,8 @@ namespace DistributedDB { SQLiteSingleVerRelationalContinueToken::SQLiteSingleVerRelationalContinueToken( - const SyncTimeRange &timeRange, const QueryObject &object) - : isGettingDeletedData_(false), queryObj_(object), tableName_(queryObj_.GetTableName()), timeRange_(timeRange) + const SyncTimeRange &timeRange, const QueryObject &queryObject) + : isGettingDeletedData_(false), queryObj_(queryObject), tableName_(queryObj_.GetTableName()), timeRange_(timeRange) {} bool SQLiteSingleVerRelationalContinueToken::CheckValid() const diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp index 68aab389..5c079477 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.cpp @@ -108,17 +108,18 @@ int SQLiteSingleVerRelationalStorageExecutor::GeneLogInfoForExistedData(sqlite3 std::string logTable = DBConstant::RELATIONAL_PREFIX + tableName + "_log"; std::string sql = "INSERT INTO " + logTable + " SELECT rowid, '', '', " + timeOffsetStr + " + rowid, " + timeOffsetStr + " + rowid, 0x2, " + - calPrimaryKeyHash + " FROM '" + tableName + "' AS a WHERE 1=1;"; + calPrimaryKeyHash + ", ''" + " FROM '" + tableName + "' AS a WHERE 1=1;"; return SQLiteUtils::ExecuteRawSQL(db, sql); } -int SQLiteSingleVerRelationalStorageExecutor::CreateDistributedTable(const std::string &tableName, - DistributedTableMode mode, bool isUpgraded, const std::string &identity, TableInfo &table) +int SQLiteSingleVerRelationalStorageExecutor::CreateDistributedTable(DistributedTableMode mode, bool isUpgraded, + const std::string &identity, TableInfo &table, TableSyncType syncType) { if (dbHandle_ == nullptr) { return -E_INVALID_DB; } + const std::string tableName = table.GetTableName(); int errCode = SQLiteUtils::AnalysisSchema(dbHandle_, tableName, table); if (errCode != E_OK) { LOGE("[CreateDistributedTable] analysis table schema failed. %d", errCode); @@ -134,14 +135,16 @@ int SQLiteSingleVerRelationalStorageExecutor::CreateDistributedTable(const std:: } } - errCode = CheckTableConstraint(table, mode); - if (errCode != E_OK) { - LOGE("[CreateDistributedTable] check table constraint failed."); - return errCode; + if (syncType != CLOUD_COOPERATION) { + errCode = CheckTableConstraint(table, mode); + if (errCode != E_OK) { + LOGE("[CreateDistributedTable] check table constraint failed."); + return errCode; + } } // create log table - auto tableManager = LogTableManagerFactory::GetTableManager(mode); + auto tableManager = LogTableManagerFactory::GetTableManager(mode, syncType); errCode = tableManager->CreateRelationalLogTable(dbHandle_, table); if (errCode != E_OK) { LOGE("[CreateDistributedTable] create log table failed"); @@ -834,10 +837,8 @@ int SQLiteSingleVerRelationalStorageExecutor::SaveSyncDataItems(RelationalSyncDa return saveStmt.ResetStatements(false); }); - if (errCode == -E_NOT_FOUND) { - errCode = E_OK; - } - return saveStmt.ResetStatements(true); + int ret = saveStmt.ResetStatements(true); + return errCode != E_OK ? errCode : ret; } int SQLiteSingleVerRelationalStorageExecutor::SaveSyncItems(RelationalSyncDataInserter &inserter, bool useTrans) diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.h b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.h index b90d2a8c..c3a2b578 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.h +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_storage_executor.h @@ -36,8 +36,8 @@ public: DISABLE_COPY_ASSIGN_MOVE(SQLiteSingleVerRelationalStorageExecutor); // The parameter "identity" is a hash string that identifies a device - int CreateDistributedTable(const std::string &tableName, DistributedTableMode mode, bool isUpgraded, - const std::string &identity, TableInfo &table); + int CreateDistributedTable(DistributedTableMode mode, bool isUpgraded, const std::string &identity, + TableInfo &table, TableSyncType syncType); int UpgradeDistributedTable(const std::string &tableName, DistributedTableMode mode, bool &schemaChanged, RelationalSchemaObject &schema); @@ -77,7 +77,7 @@ public: int CheckQueryObjectLegal(const TableInfo &table, QueryObject &query, const std::string &schemaVersion); - int GetMaxTimestamp(const std::vector &tablesName, Timestamp &maxTimestamp) const; + int GetMaxTimestamp(const std::vector &tableNames, Timestamp &maxTimestamp) const; int ExecuteQueryBySqlStmt(const std::string &sql, const std::vector &bindArgs, int packetSize, std::vector &colNames, std::vector &data); @@ -89,11 +89,11 @@ public: int GetExistsDeviceList(std::set &devices) const; private: - int GetDataItemForSync(sqlite3_stmt *statement, DataItem &dataItem, bool isGettingDeletedData) const; + int GetDataItemForSync(sqlite3_stmt *stmt, DataItem &dataItem, bool isGettingDeletedData) const; int GetSyncDataPre(const DataItem &dataItem, sqlite3_stmt *queryStmt, DataItem &itemGet); - int CheckDataConflictDefeated(const DataItem &item, sqlite3_stmt *queryStmt, bool &isDefeated); + int CheckDataConflictDefeated(const DataItem &dataItem, sqlite3_stmt *queryStmt, bool &isDefeated); int SaveSyncDataItem(RelationalSyncDataInserter &inserter, SaveSyncDataStmt &saveStmt, DataItem &item); @@ -108,7 +108,7 @@ private: int AlterAuxTableForUpgrade(const TableInfo &oldTableInfo, const TableInfo &newTableInfo); - int DeleteSyncLog(const DataItem &item, RelationalSyncDataInserter &inserter, sqlite3_stmt *&rmLogStmt); + int DeleteSyncLog(const DataItem &dataItem, RelationalSyncDataInserter &inserter, sqlite3_stmt *&rmLogStmt); int ProcessMissQueryData(const DataItem &item, RelationalSyncDataInserter &inserter, sqlite3_stmt *&rmDataStmt, sqlite3_stmt *&rmLogStmt); int GetMissQueryData(sqlite3_stmt *fullStmt, DataItem &item); diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_engine.h b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_engine.h index 0582b785..89529138 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_engine.h +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_engine.h @@ -98,7 +98,7 @@ private: int GetDbHandle(bool isWrite, const SecurityOption &secOpt, sqlite3 *&dbHandle); int AttachMetaDatabase(sqlite3 *dbHandle, const OpenDbProperties &option) const; int AttachMainDbAndCacheDb(SQLiteSingleVerStorageExecutor *handle, EngineState stateBeforeMigrate); - int AttachMainDbAndCacheDb(sqlite3 *db, EngineState stateBeforeMigrate) const; + int AttachMainDbAndCacheDb(sqlite3 *dbHandle, EngineState stateBeforeMigrate) const; void RegisterFunctionIfNeed(sqlite3 *dbHandle) const; int TryAttachMetaDb(sqlite3 *&dbHandle, bool &isAttachMeta); diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h index 57a3a162..39139441 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_storage_executor.h @@ -141,9 +141,9 @@ public: void ReleaseContinueStatement(); - int GetSyncDataByTimestamp(std::vector &dataItems, size_t appendedLength, Timestamp begin, + int GetSyncDataByTimestamp(std::vector &dataItems, size_t appendLength, Timestamp begin, Timestamp end, const DataSizeSpecInfo &dataSizeInfo) const; - int GetDeletedSyncDataByTimestamp(std::vector &dataItems, size_t appendedLength, Timestamp begin, + int GetDeletedSyncDataByTimestamp(std::vector &dataItems, size_t appendLength, Timestamp begin, Timestamp end, const DataSizeSpecInfo &dataSizeInfo) const; int GetDeviceIdentifier(PragmaEntryDeviceIdentifier *identifier); @@ -204,7 +204,7 @@ public: int MigrateSyncDataByVersion(uint64_t recordVer, NotifyMigrateSyncData &syncData, std::vector &dataItems); - int GetMinVersionCacheData(std::vector &dataItems, uint64_t &maxVerIncurCacheDb) const; + int GetMinVersionCacheData(std::vector &dataItems, uint64_t &minVerIncurCacheDb) const; int GetMaxVersionInCacheDb(uint64_t &maxVersion) const; int AttachMainDbAndCacheDb(CipherType type, const CipherPassword &passwd, diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.h b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.h index b8aecd9f..711adfb5 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.h +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_storage_engine.h @@ -52,7 +52,7 @@ public: void ClearEnginePasswd() override; - int CheckEngineOption(const KvDBProperties &kvdbOption) const override; + int CheckEngineOption(const KvDBProperties &kvDBProp) const override; protected: diff --git a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp index f37ee25a..36913edb 100644 --- a/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp +++ b/kv_store/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp @@ -683,7 +683,6 @@ int AnalysisSchemaSqlAndTrigger(sqlite3 *db, const std::string &tableName, Table } errCode = -E_NOT_FOUND; - std::vector triggerList; do { int err = SQLiteUtils::StepWithRetry(statement); if (err == SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)) { diff --git a/kv_store/frameworks/libs/distributeddb/syncer/src/meta_data.cpp b/kv_store/frameworks/libs/distributeddb/syncer/src/meta_data.cpp index ffbad817..5f49fa29 100644 --- a/kv_store/frameworks/libs/distributeddb/syncer/src/meta_data.cpp +++ b/kv_store/frameworks/libs/distributeddb/syncer/src/meta_data.cpp @@ -596,7 +596,7 @@ int Metadata::SaveClientId(const std::string &deviceId, const std::string &clien return E_OK; } -int Metadata::GetHashDeviceId(const std::string &clientId, std::string &hashDevId) +int Metadata::GetHashDeviceId(const std::string &clientId, std::string &hashDevId) const { // don't use cache here avoid invalid cache std::string keyStr; diff --git a/kv_store/frameworks/libs/distributeddb/syncer/src/meta_data.h b/kv_store/frameworks/libs/distributeddb/syncer/src/meta_data.h index a73d60fa..ef0a2bf0 100644 --- a/kv_store/frameworks/libs/distributeddb/syncer/src/meta_data.h +++ b/kv_store/frameworks/libs/distributeddb/syncer/src/meta_data.h @@ -114,7 +114,7 @@ public: int SaveClientId(const std::string &deviceId, const std::string &clientId); - int GetHashDeviceId(const std::string &clientId, std::string &hashDevId); + int GetHashDeviceId(const std::string &clientId, std::string &hashDevId) const; private: int SaveMetaDataValue(const DeviceID &deviceId, const MetaDataValue &inValue, bool isNeedHash = true); diff --git a/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor.cpp b/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor.cpp index 5a72a11d..96a2420a 100644 --- a/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor.cpp +++ b/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor.cpp @@ -73,14 +73,15 @@ int RemoteExecutor::RemoteQuery(const std::string device, const RemoteCondition return -E_INVALID_ARGS; } int errCode = E_OK; + int taskErrCode = E_OK; SemaphoreUtils semaphore(0); Task task; task.result = std::make_shared(); task.target = device; task.timeout = timeout; task.condition = condition; - task.onFinished = [&semaphore, &errCode, &result](int32_t retCode, std::shared_ptr taskResult) { - errCode = retCode; + task.onFinished = [&semaphore, &taskErrCode, &result](int32_t retCode, std::shared_ptr taskResult) { + taskErrCode = retCode; result = taskResult; semaphore.SendSemaphore(); }; @@ -90,7 +91,7 @@ int RemoteExecutor::RemoteQuery(const std::string device, const RemoteCondition return errCode; } semaphore.WaitSemaphore(); - return errCode; + return taskErrCode; } int RemoteExecutor::ReceiveMessage(const std::string &targetDev, Message *inMsg) @@ -202,7 +203,7 @@ void RemoteExecutor::ParseOneRequestMessage(const std::string &device, Message * LOGW("[RemoteExecutor][ParseOneRequestMessage] closed"); return; } - int errCode = CheckPermissions(device); + int errCode = CheckPermissions(device, inMsg); if (errCode != E_OK) { (void)ResponseFailed(errCode, inMsg->GetSessionId(), inMsg->GetSequenceId(), device); return; @@ -213,7 +214,7 @@ void RemoteExecutor::ParseOneRequestMessage(const std::string &device, Message * } } -int RemoteExecutor::CheckPermissions(const std::string &device) +int RemoteExecutor::CheckPermissions(const std::string &device, Message *inMsg) { SyncGenericInterface *storage = static_cast(GetAndIncSyncInterface()); if (storage == nullptr) { @@ -229,7 +230,16 @@ int RemoteExecutor::CheckPermissions(const std::string &device) { userId, appId, storeId, device, instanceId }, CHECK_FLAG_SEND); if (errCode != E_OK) { LOGE("[RemoteExecutor][CheckPermissions] check permission errCode = %d.", errCode); + storage->DecRefCount(); + return errCode; + } + const auto *requestPacket = inMsg->GetObject(); + if (requestPacket == nullptr) { + LOGE("[RemoteExecutor] get packet object failed"); + storage->DecRefCount(); + return -E_INVALID_ARGS; } + errCode = CheckRemoteRecvData(device, storage, requestPacket->GetSecLabel()); storage->DecRefCount(); return errCode; } @@ -712,6 +722,17 @@ void RemoteExecutor::ClearInnerSource() int RemoteExecutor::FillRequestPacket(RemoteExecutorRequestPacket *packet, uint32_t sessionId, std::string &target) { + ISyncInterface *storage = GetAndIncSyncInterface(); + if (storage == nullptr) { + return -E_BUSY; + } + SecurityOption localOption; + int errCode = storage->GetSecurityOption(localOption); + storage->DecRefCount(); + storage = nullptr; + if (errCode != E_OK && errCode != -E_NOT_SUPPORT) { + return -E_SECURITY_OPTION_CHECK_ERROR; + } Task task; { std::lock_guard autoLock(taskLock_); @@ -726,6 +747,7 @@ int RemoteExecutor::FillRequestPacket(RemoteExecutorRequestPacket *packet, uint3 packet->SetSql(task.condition.sql); packet->SetBindArgs(task.condition.bindArgs); packet->SetNeedResponse(); + packet->SetSecLabel(errCode == E_NOT_SUPPORT ? NOT_SURPPORT_SEC_CLASSIFICATION : localOption.securityLabel); target = task.target; return E_OK; } @@ -944,7 +966,7 @@ int RemoteExecutor::CheckSecurityOption(ISyncInterface *storage, ICommunicator * std::string device; communicator->GetLocalIdentity(device); SecurityOption localOption; - int errCode = static_cast(storage)->GetSecurityOption(localOption); + int errCode = storage->GetSecurityOption(localOption); if (errCode != E_OK && errCode != -E_NOT_SUPPORT) { return -E_SECURITY_OPTION_CHECK_ERROR; } @@ -958,4 +980,24 @@ int RemoteExecutor::CheckSecurityOption(ISyncInterface *storage, ICommunicator * } return errCode; } + +int RemoteExecutor::CheckRemoteRecvData(const std::string &device, SyncGenericInterface *storage, + int32_t remoteSecLabel) +{ + SecurityOption localOption; + int errCode = storage->GetSecurityOption(localOption); + if (errCode == -E_NOT_SUPPORT) { + return E_OK; + } + if (errCode != E_OK) { + return -E_SECURITY_OPTION_CHECK_ERROR; + } + if (remoteSecLabel == UNKNOWN_SECURITY_LABEL || remoteSecLabel == NOT_SURPPORT_SEC_CLASSIFICATION) { + return E_OK; + } + if (RuntimeContext::GetInstance()->CheckDeviceSecurityAbility(device, localOption)) { + return E_OK; + } + return -E_SECURITY_OPTION_CHECK_ERROR; +} } \ No newline at end of file diff --git a/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor.h b/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor.h index f7c1fdd8..8c369574 100644 --- a/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor.h +++ b/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor.h @@ -93,7 +93,7 @@ private: int ReceiveRemoteExecutorAck(const std::string &targetDev, Message *inMsg); - int CheckPermissions(const std::string &device); + int CheckPermissions(const std::string &device, Message *inMsg); int SendRemoteExecutorData(const std::string &device, const Message *inMsg); @@ -143,6 +143,7 @@ private: ICommunicator *GetAndIncCommunicator() const; ISyncInterface *GetAndIncSyncInterface() const; + static int CheckRemoteRecvData(const std::string &device, SyncGenericInterface *storage, int32_t remoteSecLabel); std::mutex taskLock_; std::map> searchTaskQueue_; // key is device, value is sessionId queue diff --git a/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.cpp b/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.cpp index 5c0360a5..999bb0a4 100644 --- a/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.cpp +++ b/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.cpp @@ -95,6 +95,7 @@ uint32_t RemoteExecutorRequestPacket::CalculateLen() const } } len = Parcel::GetEightByteAlign(len); // 8-byte align + len += Parcel::GetIntLen(); return len; } @@ -120,6 +121,7 @@ int RemoteExecutorRequestPacket::Serialization(Parcel &parcel) const parcel.WriteString(entry.second); } parcel.EightByteAlign(); + parcel.WriteInt(secLabel_); if (parcel.IsError()) { return -E_PARSE_FAIL; } @@ -155,6 +157,9 @@ int RemoteExecutorRequestPacket::DeSerialization(Parcel &parcel) extraConditions_[conditionKey] = conditionVal; } parcel.EightByteAlign(); + if (version_ >= REQUEST_PACKET_VERSION_V3) { + parcel.ReadInt(secLabel_); + } if (parcel.IsError()) { return -E_PARSE_FAIL; } @@ -176,6 +181,16 @@ void RemoteExecutorRequestPacket::SetBindArgs(const std::vector &bi preparedStmt_.SetBindArgs(bindArgs); } +void RemoteExecutorRequestPacket::SetSecLabel(int32_t secLabel) +{ + secLabel_ = secLabel; +} + +int32_t RemoteExecutorRequestPacket::GetSecLabel() const +{ + return secLabel_; +} + RemoteExecutorRequestPacket* RemoteExecutorRequestPacket::Create() { return new (std::nothrow) RemoteExecutorRequestPacket(); diff --git a/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.h b/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.h index d5549709..5d25262c 100644 --- a/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.h +++ b/kv_store/frameworks/libs/distributeddb/syncer/src/remote_executor_packet.h @@ -59,18 +59,24 @@ public: void SetBindArgs(const std::vector &bindArgs); + void SetSecLabel(int32_t secLabel); + + int32_t GetSecLabel() const; + static RemoteExecutorRequestPacket* Create(); static void Release(RemoteExecutorRequestPacket *&packet); static const uint32_t REQUEST_PACKET_VERSION_V1 = SOFTWARE_VERSION_RELEASE_6_0; static const uint32_t REQUEST_PACKET_VERSION_V2 = SOFTWARE_VERSION_RELEASE_6_0 + 1; - static const uint32_t REQUEST_PACKET_VERSION_CURRENT = REQUEST_PACKET_VERSION_V2; + static const uint32_t REQUEST_PACKET_VERSION_V3 = SOFTWARE_VERSION_RELEASE_6_0 + 2; + static const uint32_t REQUEST_PACKET_VERSION_CURRENT = REQUEST_PACKET_VERSION_V3; private: uint32_t version_ = 0u; uint32_t flag_ = 0u; // 0x01 mean need reply ack PreparedStmt preparedStmt_; std::map extraConditions_; + int32_t secLabel_ = UNKNOWN_SECURITY_LABEL; // source sec label }; class RemoteExecutorAckPacket : public ISyncPacket { diff --git a/kv_store/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp b/kv_store/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp index 736f7821..05e9fcfc 100644 --- a/kv_store/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp +++ b/kv_store/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.cpp @@ -66,13 +66,16 @@ int SingleVerDataSync::Initialize(ISyncInterface *inStorage, ICommunicator *inCo int SingleVerDataSync::SyncStart(int mode, SingleVerSyncTaskContext *context) { std::lock_guard lock(lock_); + int errCode = CheckPermitSendData(mode, context); + if (errCode != E_OK) { + return errCode; + } if (sessionId_ != 0) { // auto sync timeout resend return ReSendData(context); } ResetSyncStatus(mode, context); LOGI("[DataSync] SendStart,mode=%d,label=%s,device=%s", mode_, label_.c_str(), STR_MASK(deviceId_)); int tmpMode = SyncOperation::TransferSyncMode(mode); - int errCode = E_OK; if (tmpMode == SyncModeType::PUSH) { errCode = PushStart(context); } else if (tmpMode == SyncModeType::PUSH_AND_PULL) { @@ -100,6 +103,10 @@ int SingleVerDataSync::SyncStart(int mode, SingleVerSyncTaskContext *context) int SingleVerDataSync::InnerSyncStart(SingleVerSyncTaskContext *context) { + int errCode = CheckPermitSendData(mode_, context); + if (errCode != E_OK) { + return errCode; + } while (true) { if (windowSize_ <= 0 || isAllDataHasSent_) { LOGD("[DataSync] InnerDataSync winSize=%d,isAllSent=%d,label=%s,device=%s", windowSize_, isAllDataHasSent_, @@ -111,7 +118,6 @@ int SingleVerDataSync::InnerSyncStart(SingleVerSyncTaskContext *context) LOGE("[DataSync] unexpected error"); return -E_INVALID_ARGS; } - int errCode; context->IncSequenceId(); if (mode == SyncModeType::PUSH || mode == SyncModeType::PUSH_AND_PULL) { errCode = PushStart(context); diff --git a/kv_store/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.h b/kv_store/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.h index b85d6b31..7ed2baa0 100644 --- a/kv_store/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.h +++ b/kv_store/frameworks/libs/distributeddb/syncer/src/single_ver_data_sync.h @@ -169,10 +169,10 @@ protected: SyncTimeRange dataTimeRange, bool isCheckBeforUpdate = false) const; void GetLocalWaterMark(SyncType syncType, const std::string &queryIdentify, const SingleVerSyncTaskContext *context, - WaterMark &watermark) const; + WaterMark &waterMark) const; void GetPeerWaterMark(SyncType syncType, const std::string &queryIdentify, const DeviceID &deviceId, - WaterMark &watermark) const; + WaterMark &waterMark) const; void GetPeerDeleteSyncWaterMark(const DeviceID &deviceId, WaterMark &waterMark); diff --git a/kv_store/frameworks/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.cpp b/kv_store/frameworks/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.cpp index ed04c23a..8cd3bfa9 100644 --- a/kv_store/frameworks/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.cpp +++ b/kv_store/frameworks/libs/distributeddb/syncer/src/single_ver_relational_sync_task_context.cpp @@ -43,9 +43,9 @@ void SingleVerRelationalSyncTaskContext::Clear() SingleVerSyncTaskContext::Clear(); } -void SingleVerRelationalSyncTaskContext::CopyTargetData(const ISyncTarget *target, const TaskParam &TaskParam) +void SingleVerRelationalSyncTaskContext::CopyTargetData(const ISyncTarget *target, const TaskParam &taskParam) { - SingleVerSyncTaskContext::CopyTargetData(target, TaskParam); + SingleVerSyncTaskContext::CopyTargetData(target, taskParam); std::string hashTableName = DBCommon::TransferHashString(query_.GetRelationTableName()); std::string hexTableName = DBCommon::TransferStringToHex(hashTableName); querySyncId_ = hexTableName + query_.GetIdentify(); // save as deviceId + hexTableName + queryId diff --git a/kv_store/frameworks/libs/distributeddb/syncer/src/sync_types.h b/kv_store/frameworks/libs/distributeddb/syncer/src/sync_types.h index 29253e54..26323c90 100644 --- a/kv_store/frameworks/libs/distributeddb/syncer/src/sync_types.h +++ b/kv_store/frameworks/libs/distributeddb/syncer/src/sync_types.h @@ -76,6 +76,7 @@ struct InternalSyncParma { QuerySyncObject syncQuery; }; +constexpr int32_t UNKNOWN_SECURITY_LABEL = -1; constexpr int NOT_SURPPORT_SEC_CLASSIFICATION = 0xff; constexpr uint8_t QUERY_SYNC_MODE_BASE = SyncModeType::QUERY_PUSH; constexpr int AUTO_RETRY_TIMES = 3; diff --git a/kv_store/frameworks/libs/distributeddb/test/BUILD.gn b/kv_store/frameworks/libs/distributeddb/test/BUILD.gn index 77c10fd7..4f241312 100644 --- a/kv_store/frameworks/libs/distributeddb/test/BUILD.gn +++ b/kv_store/frameworks/libs/distributeddb/test/BUILD.gn @@ -25,6 +25,7 @@ config("module_private_config") { "./unittest/common/interfaces", "../include", "../interfaces/include", + "../interfaces/include/cloud", "../interfaces/include/relational", "../interfaces/src", "../interfaces/src/relational", @@ -185,6 +186,7 @@ ohos_source_set("src_file") { "../storage/src/relationaldb_properties.cpp", "../storage/src/result_entries_window.cpp", "../storage/src/single_ver_natural_store_commit_notify_data.cpp", + "../storage/src/sqlite/cloud_sync_log_table_manager.cpp", "../storage/src/sqlite/collaboration_log_table_manager.cpp", "../storage/src/sqlite/log_table_manager_factory.cpp", "../storage/src/sqlite/query_object.cpp", @@ -454,6 +456,10 @@ distributeddb_unittest("DistributedDBInterfacesNBDelegateTest") { ] } +distributeddb_unittest("DistributedDBInterfacesRelationalExtTest") { + sources = [ "unittest/common/interfaces/distributeddb_interfaces_relational_ext_test.cpp" ] +} + distributeddb_unittest("DistributedDBCommonTest") { sources = [ "unittest/common/common/distributeddb_common_test.cpp" ] } @@ -812,6 +818,7 @@ group("unittest") { ":DistributedDBInterfacesNBUnpublishTest", ":DistributedDBInterfacesQueryDBTest", ":DistributedDBInterfacesRegisterSyncDBTest", + ":DistributedDBInterfacesRelationalExtTest", ":DistributedDBInterfacesRelationalRoutinesTest", ":DistributedDBInterfacesRelationalSyncTest", ":DistributedDBInterfacesSchemaDatabaseUpgradeTest", diff --git a/kv_store/frameworks/libs/distributeddb/test/fuzztest/relationalstoredelegate_fuzzer/relationalstoredelegate_fuzzer.cpp b/kv_store/frameworks/libs/distributeddb/test/fuzztest/relationalstoredelegate_fuzzer/relationalstoredelegate_fuzzer.cpp index 2cf32a8a..157ab2e8 100644 --- a/kv_store/frameworks/libs/distributeddb/test/fuzztest/relationalstoredelegate_fuzzer/relationalstoredelegate_fuzzer.cpp +++ b/kv_store/frameworks/libs/distributeddb/test/fuzztest/relationalstoredelegate_fuzzer/relationalstoredelegate_fuzzer.cpp @@ -116,7 +116,8 @@ void CombineTest(const uint8_t* data, size_t size) } else if (index == 2) { // 2 is the remainder mode = SyncMode::SYNC_MODE_PUSH_PULL; } - g_delegate->Sync(device, mode, query, nullptr, len % 2); // 2 is mod num for wait parameter + SyncStatusCallback callback = nullptr; + g_delegate->Sync(device, mode, query, callback, len % 2); // 2 is mod num for wait parameter std::string deviceId = device.size() > 0 ? device[0] : tableName; g_delegate->RemoveDeviceData(); g_delegate->RemoveDeviceData(deviceId); diff --git a/kv_store/frameworks/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_test.cpp b/kv_store/frameworks/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_test.cpp index 3c05f29a..36fbe246 100644 --- a/kv_store/frameworks/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_test.cpp +++ b/kv_store/frameworks/libs/distributeddb/test/unittest/common/communicator/distributeddb_communicator_test.cpp @@ -30,7 +30,6 @@ namespace { EnvHandle g_envDeviceA; EnvHandle g_envDeviceB; EnvHandle g_envDeviceC; -} static void HandleConnectChange(OnOfflineDevice &onlines, const std::string &target, bool isConnect) { @@ -853,3 +852,4 @@ HWTEST_F(DistributedDBCommunicatorTest, ReDeliverMessage003, TestSize.Level2) g_envDeviceB.commAggrHandle->RegCommunicatorLackCallback(nullptr, nullptr); AdapterStub::DisconnectAdapterStub(g_envDeviceA.adapterHandle, g_envDeviceB.adapterHandle); } +} \ No newline at end of file diff --git a/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_log_test.cpp b/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_log_test.cpp index 1685bbc1..7ccaf5da 100644 --- a/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_log_test.cpp +++ b/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_log_test.cpp @@ -15,6 +15,7 @@ #include +#include "cloud_store_types.h" #include "log_table_manager_factory.h" #include "native_sqlite.h" #include "split_device_log_table_manager.h" @@ -58,7 +59,8 @@ void DistributedDBInterfacesLogTest::TearDown() HWTEST_F(DistributedDBInterfacesLogTest, DBFactoryTest001, TestSize.Level1) { DistributedTableMode mode = DistributedTableMode::COLLABORATION; - auto tableManager = LogTableManagerFactory::GetTableManager(mode); + TableSyncType tableSyncType = TableSyncType::DEVICE_COOPERATION; + auto tableManager = LogTableManagerFactory::GetTableManager(mode, tableSyncType); EXPECT_TRUE(tableManager != nullptr); } diff --git a/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_register_syncdb_test.cpp b/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_register_syncdb_test.cpp index aca962af..37d80108 100644 --- a/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_register_syncdb_test.cpp +++ b/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_register_syncdb_test.cpp @@ -340,6 +340,7 @@ HWTEST_F(DistributedDBInterfacesRegisterSyncDBTest, RegisterObserver006, TestSiz value1.push_back(8); value2.push_back(10); DBStatus status = g_kvDelegatePtr->Put(key, value1); + EXPECT_TRUE(status == OK); std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME)); /** @@ -379,6 +380,7 @@ HWTEST_F(DistributedDBInterfacesRegisterSyncDBTest, RegisterObserver007, TestSiz key.push_back(1); value.push_back(8); DBStatus status = g_kvDelegatePtr->Put(key, value); + EXPECT_TRUE(status == OK); std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME)); /** @@ -458,6 +460,7 @@ HWTEST_F(DistributedDBInterfacesRegisterSyncDBTest, RegisterObserver009, TestSiz keys.push_back(entry.key); } DBStatus status = g_kvDelegatePtr->PutBatch(entries); + EXPECT_TRUE(status == OK); std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME)); /** @@ -506,6 +509,7 @@ HWTEST_F(DistributedDBInterfacesRegisterSyncDBTest, RegisterObserver010, TestSiz keys.push_back(key); } DBStatus status = g_kvDelegatePtr->PutBatch(entries); + EXPECT_TRUE(status == OK); std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME)); /** @@ -589,6 +593,7 @@ HWTEST_F(DistributedDBInterfacesRegisterSyncDBTest, RegisterObserver012, TestSiz keys.push_back(entry.key); } DBStatus status = g_kvDelegatePtr->PutBatch(entries); + EXPECT_TRUE(status == OK); std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME)); /** diff --git a/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_ext_test.cpp b/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_ext_test.cpp new file mode 100644 index 00000000..9a24320e --- /dev/null +++ b/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_ext_test.cpp @@ -0,0 +1,361 @@ +/* + * 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 "db_common.h" +#include "distributeddb_data_generate_unit_test.h" +#include "distributeddb_tools_unit_test.h" +#include "relational_store_manager.h" + +using namespace testing::ext; +using namespace DistributedDB; +using namespace DistributedDBUnitTest; +using namespace std; + +namespace { +constexpr const char *DB_SUFFIX = ".db"; +constexpr const char *STORE_ID = "Relational_Store_ID"; +std::string g_dbDir; +std::string g_testDir; +DistributedDB::RelationalStoreManager g_mgr(APP_ID, USER_ID); + +constexpr int E_OK = 0; +constexpr int E_ERROR = 1; +const int WAIT_TIME = 1000; // 1000ms +constexpr static uint64_t TO_100_NS = 10; // 1us to 100ns +const uint64_t MULTIPLES_BETWEEN_SECONDS_AND_MICROSECONDS = 1000000; + +class DistributedDBInterfacesRelationalExtTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp() override; + void TearDown() override; +}; + +void DistributedDBInterfacesRelationalExtTest::SetUpTestCase(void) +{ + DistributedDBToolsUnitTest::TestDirInit(g_testDir); + LOGD("Test dir is %s", g_testDir.c_str()); + g_dbDir = g_testDir + "/"; + DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir); +} + +void DistributedDBInterfacesRelationalExtTest::TearDownTestCase(void) +{ +} + +void DistributedDBInterfacesRelationalExtTest::SetUp() +{ +} + +void DistributedDBInterfacesRelationalExtTest::TearDown() +{ + DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir); +} + +static int GetCurrentSysTimeIn100Ns(uint64_t &outTime) +{ + struct timeval rawTime; + int errCode = gettimeofday(&rawTime, nullptr); + if (errCode < 0) { + return -E_ERROR; + } + outTime = static_cast(rawTime.tv_sec) * MULTIPLES_BETWEEN_SECONDS_AND_MICROSECONDS + + static_cast(rawTime.tv_usec); + outTime *= TO_100_NS; + return E_OK; +} + +/** + * @tc.name: GetRawSysTimeTest001 + * @tc.desc: Test get_raw_sys_time has been registered in sqlite + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBInterfacesRelationalExtTest, GetRawSysTimeTest001, TestSize.Level0) +{ + const std::string sql = "select get_raw_sys_time();"; + sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX); + EXPECT_NE(db, nullptr); + uint64_t curTime = 0; + int errCode = GetCurrentSysTimeIn100Ns(curTime); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql, nullptr, [curTime] (sqlite3_stmt *stmt) { + int64_t diff = MULTIPLES_BETWEEN_SECONDS_AND_MICROSECONDS * TO_100_NS; + EXPECT_TRUE(static_cast(sqlite3_column_int64(stmt, 0) - curTime) < diff); + return OK; + }), SQLITE_OK); + + EXPECT_EQ(sqlite3_close_v2(db), E_OK); +} + +void PrepareData(const std::string &tableName, bool primaryKeyIsRowId) +{ + /** + * @tc.steps:step1. create db, create table. + * @tc.expected: step1. return ok. + */ + sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX); + EXPECT_NE(db, nullptr); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK); + std::string sql; + if (primaryKeyIsRowId) { + sql = "create table " + tableName + "(rowid INTEGER primary key, id int, name TEXT);"; + } else { + sql = "create table " + tableName + "(rowid int, id int, name TEXT, PRIMARY KEY(id, name));"; + } + + EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), E_OK); + EXPECT_EQ(sqlite3_close_v2(db), E_OK); + + /** + * @tc.steps:step2. create distributed table. + * @tc.expected: step2. return ok. + */ + RelationalStoreDelegate *delegate = nullptr; + DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate); + EXPECT_EQ(status, OK); + ASSERT_NE(delegate, nullptr); + EXPECT_EQ(delegate->CreateDistributedTable(tableName, DistributedDB::CLOUD_COOPERATION), OK); + EXPECT_EQ(g_mgr.CloseStore(delegate), OK); + delegate = nullptr; +} + +/** + * @tc.name: InsertTriggerTest001 + * @tc.desc: Test insert trigger in sqlite + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBInterfacesRelationalExtTest, InsertTriggerTest001, TestSize.Level0) +{ + /** + * @tc.steps:step1. prepare data. + * @tc.expected: step1. return ok. + */ + const std::string tableName = "sync_data"; + PrepareData(tableName, false); + + /** + * @tc.steps:step2. insert data into sync_data_tmp. + * @tc.expected: step2. return ok. + */ + sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX); + EXPECT_NE(db, nullptr); + std::string sql = "insert into " + tableName + " VALUES(2, 1, 'zhangsan');"; + EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), E_OK); + + /** + * @tc.steps:step3. select data from log table. + * @tc.expected: step3. return ok. + */ + sql = "select * from " + DBConstant::RELATIONAL_PREFIX + tableName + "_log;"; + uint64_t curTime = 0; + int errCode = GetCurrentSysTimeIn100Ns(curTime); + EXPECT_EQ(errCode, E_OK); + + int resultCount = 0; + EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql, nullptr, [curTime, &resultCount] (sqlite3_stmt *stmt) { + EXPECT_EQ(sqlite3_column_int64(stmt, 0), 2); // 2 is rowid + std::string device = ""; + EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 1, device), E_OK); + EXPECT_EQ(device, ""); + std::string oriDevice = ""; + EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 2, oriDevice), E_OK); // 2 is column index + EXPECT_EQ(oriDevice, ""); + + int64_t timestamp = sqlite3_column_int64(stmt, 3); // 3 is column index + int64_t wtimestamp = sqlite3_column_int64(stmt, 4); // 4 is column index + int64_t diff = MULTIPLES_BETWEEN_SECONDS_AND_MICROSECONDS * TO_100_NS; + EXPECT_TRUE(wtimestamp - timestamp < diff); + EXPECT_TRUE(static_cast(curTime - timestamp) < diff); + EXPECT_EQ(sqlite3_column_int(stmt, 5), 2); // 5 is column index, flag == 2 + resultCount++; + return OK; + }), SQLITE_OK); + EXPECT_EQ(resultCount, 1); + EXPECT_EQ(sqlite3_close_v2(db), E_OK); +} + +void UpdateTriggerTest(bool primaryKeyIsRowId) +{ + /** + * @tc.steps:step1. prepare data. + * @tc.expected: step1. return ok. + */ + const std::string tableName = "sync_data"; + PrepareData(tableName, primaryKeyIsRowId); + + /** + * @tc.steps:step2. insert data into sync_data_tmp. + * @tc.expected: step2. return ok. + */ + sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX); + EXPECT_NE(db, nullptr); + std::string sql = "insert into " + tableName + " VALUES(2, 1, 'zhangsan');"; + EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), E_OK); + + /** + * @tc.steps:step3. update data. + * @tc.expected: step3. return ok. + */ + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME)); + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME)); + sql = "update " + tableName + " set name = 'lisi';"; + EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), E_OK); + + /** + * @tc.steps:step4. select data from log table. + * @tc.expected: step4. return ok. + */ + sql = "select * from " + DBConstant::RELATIONAL_PREFIX + tableName + "_log;"; + uint64_t curTime = 0; + int errCode = GetCurrentSysTimeIn100Ns(curTime); + EXPECT_EQ(errCode, E_OK); + + int resultCnt = 0; + std::function sqliteCallBack = [curTime, primaryKeyIsRowId, &resultCnt] (sqlite3_stmt *stmt) { + if (!primaryKeyIsRowId && resultCnt == 0) { + EXPECT_EQ(sqlite3_column_int64(stmt, 0), -1); + EXPECT_EQ(sqlite3_column_int(stmt, 5), 3); // 5 is column index, flag == 3 + } else { + EXPECT_EQ(sqlite3_column_int64(stmt, 0), 2); // 2 is rowid + EXPECT_EQ(sqlite3_column_int(stmt, 5), 2); // 5 is column index, flag == 2 + } + + std::string device = ""; + EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 1, device), E_OK); + EXPECT_EQ(device, ""); + std::string oriDevice = ""; + EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 2, oriDevice), E_OK); // 2 is column index + EXPECT_EQ(oriDevice, ""); + + int64_t timestamp = sqlite3_column_int64(stmt, 3); // 3 is column index + int64_t wtimestamp = sqlite3_column_int64(stmt, 4); // 4 is column index + int64_t diff = MULTIPLES_BETWEEN_SECONDS_AND_MICROSECONDS * TO_100_NS; + EXPECT_TRUE(timestamp - wtimestamp > diff); + EXPECT_TRUE(static_cast(curTime - timestamp) < diff); + + resultCnt++; + return OK; + }; + + EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql, nullptr, sqliteCallBack), SQLITE_OK); + if (primaryKeyIsRowId) { + EXPECT_EQ(resultCnt, 1); + } else { + EXPECT_EQ(resultCnt, 2); // 2 is log records count + } + EXPECT_EQ(sqlite3_close_v2(db), E_OK); +} + +/** + * @tc.name: UpdateTriggerTest001 + * @tc.desc: Test update trigger in sqlite for primary key is not row id + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBInterfacesRelationalExtTest, UpdateTriggerTest001, TestSize.Level0) +{ + UpdateTriggerTest(false); +} + +/** + * @tc.name: UpdateTriggerTest002 + * @tc.desc: Test update trigger in sqlite for primary key is row id + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBInterfacesRelationalExtTest, UpdateTriggerTest002, TestSize.Level0) +{ + UpdateTriggerTest(true); +} + +/** + * @tc.name: DeleteTriggerTest001 + * @tc.desc: Test delete trigger in sqlite + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBInterfacesRelationalExtTest, DeleteTriggerTest001, TestSize.Level0) +{ + /** + * @tc.steps:step1. prepare data. + * @tc.expected: step1. return ok. + */ + const std::string tableName = "sync_data"; + PrepareData(tableName, true); + + /** + * @tc.steps:step2. insert data into sync_data_tmp. + * @tc.expected: step2. return ok. + */ + sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX); + EXPECT_NE(db, nullptr); + std::string sql = "insert into " + tableName + " VALUES(2, 1, 'zhangsan');"; + EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), E_OK); + + /** + * @tc.steps:step3. delete data. + * @tc.expected: step3. return ok. + */ + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME)); + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME)); + sql = "delete from " + tableName + " where name = 'zhangsan';"; + EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), E_OK); + + /** + * @tc.steps:step4. select data from log table. + * @tc.expected: step4. return ok. + */ + sql = "select * from " + DBConstant::RELATIONAL_PREFIX + tableName + "_log;"; + uint64_t curTime = 0; + int errCode = GetCurrentSysTimeIn100Ns(curTime); + EXPECT_EQ(errCode, E_OK); + + int resultCount = 0; + EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql, nullptr, + [curTime, &resultCount] (sqlite3_stmt *stmt) { + EXPECT_EQ(sqlite3_column_int64(stmt, 0), -1); + EXPECT_EQ(sqlite3_column_int(stmt, 5), 3); // 5 is column index, flag == 3 + + std::string device = ""; + EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 1, device), E_OK); + EXPECT_EQ(device, ""); + std::string oriDevice = ""; + EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 2, oriDevice), E_OK); // 2 is column index, + EXPECT_EQ(oriDevice, ""); + + int64_t timestamp = sqlite3_column_int64(stmt, 3); // 3 is column index + int64_t wtimestamp = sqlite3_column_int64(stmt, 4); // 4 is column index + int64_t diff = MULTIPLES_BETWEEN_SECONDS_AND_MICROSECONDS * TO_100_NS; + EXPECT_TRUE(timestamp - wtimestamp > diff); + EXPECT_TRUE(static_cast(curTime - timestamp) < diff); + + resultCount++; + return OK; + }), SQLITE_OK); + EXPECT_EQ(resultCount, 1); + EXPECT_EQ(sqlite3_close_v2(db), E_OK); +} +} \ No newline at end of file diff --git a/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_sync_test.cpp b/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_sync_test.cpp index a974f4a7..4d9a77dd 100644 --- a/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_sync_test.cpp +++ b/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_sync_test.cpp @@ -82,6 +82,33 @@ namespace { void FakeOldVersionDB(sqlite3 *db) { + std::string dropLogTable = "drop table naturalbase_rdb_aux_student_1_log;"; + EXPECT_EQ(RelationalTestUtils::ExecSql(db, dropLogTable), SQLITE_OK); + dropLogTable = "drop table naturalbase_rdb_aux_sync_data_log;"; + EXPECT_EQ(RelationalTestUtils::ExecSql(db, dropLogTable), SQLITE_OK); + + std::string createLogTable = "CREATE TABLE naturalbase_rdb_aux_student_1_log(" \ + "data_key INT NOT NULL," \ + "device BLOB," \ + "ori_device BLOB," \ + "timestamp INT NOT NULL," \ + "wtimestamp INT NOT NULL," \ + "flag INT NOT NULL," \ + "hash_key BLOB NOT NULL," \ + "PRIMARY KEY(hash_key));"; + EXPECT_EQ(RelationalTestUtils::ExecSql(db, createLogTable), SQLITE_OK); + + createLogTable = "CREATE TABLE naturalbase_rdb_aux_sync_data_log(" \ + "data_key INT NOT NULL," \ + "device BLOB," \ + "ori_device BLOB," \ + "timestamp INT NOT NULL," \ + "wtimestamp INT NOT NULL," \ + "flag INT NOT NULL," \ + "hash_key BLOB NOT NULL," \ + "PRIMARY KEY(hash_key));"; + EXPECT_EQ(RelationalTestUtils::ExecSql(db, createLogTable), SQLITE_OK); + std::string dropTrigger = "DROP TRIGGER IF EXISTS naturalbase_rdb_student_1_ON_UPDATE;"; EXPECT_EQ(RelationalTestUtils::ExecSql(db, dropTrigger), SQLITE_OK); @@ -525,7 +552,7 @@ HWTEST_F(DistributedDBInterfacesRelationalSyncTest, UpgradeTriggerTest001, TestS " flag=0x03 WHERE hash_key=calc_hash(OLD.'id') AND flag&0x02=0x02;\n" "\t INSERT OR REPLACE INTO naturalbase_rdb_aux_student_1_log VALUES (NEW.rowid, '', '', get_sys_time(0), " "get_last_time(), CASE WHEN (calc_hash(NEW.'id') != calc_hash(NEW.'id')) " \ - "THEN 0x02 ELSE 0x22 END, calc_hash(NEW.'id'));\n" + "THEN 0x02 ELSE 0x22 END, calc_hash(NEW.'id'), '');\n" "END"; EXPECT_TRUE(resultTrigger == expectTrigger); } diff --git a/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp b/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp index 52e3b789..016d9a9f 100644 --- a/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp +++ b/kv_store/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp @@ -34,72 +34,71 @@ using namespace DistributedDBUnitTest; using namespace std; namespace { - constexpr const char* DB_SUFFIX = ".db"; - constexpr const char* STORE_ID = "Relational_Store_ID"; - std::string g_testDir; - std::string g_dbDir; - DistributedDB::RelationalStoreManager g_mgr(APP_ID, USER_ID); - - const std::string DEVICE_A = "real_device"; - const std::string DEVICE_B = "deviceB"; - VirtualCommunicatorAggregator* g_communicatorAggregator = nullptr; - RelationalVirtualDevice *g_deviceB = nullptr; - - const std::string NORMAL_CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS sync_data(" \ - "key BLOB NOT NULL UNIQUE," \ - "value BLOB," \ - "timestamp INT NOT NULL," \ - "flag INT NOT NULL," \ - "device BLOB," \ - "ori_device BLOB," \ - "hash_key BLOB PRIMARY KEY NOT NULL," \ - "w_timestamp INT," \ - "UNIQUE(device, ori_device));" \ - "CREATE INDEX key_index ON sync_data (key, flag);"; - - const std::string SIMPLE_CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS t1(a INT, b TEXT)"; - - const std::string CREATE_TABLE_SQL_NO_PRIMARY_KEY = "CREATE TABLE IF NOT EXISTS sync_data(" \ - "key BLOB NOT NULL UNIQUE," \ - "value BLOB," \ - "timestamp INT NOT NULL," \ - "flag INT NOT NULL," \ - "device BLOB," \ - "ori_device BLOB," \ - "hash_key BLOB NOT NULL," \ - "w_timestamp INT," \ - "UNIQUE(device, ori_device));" \ - "CREATE INDEX key_index ON sync_data (key, flag);"; - - const std::string UNSUPPORTED_FIELD_TABLE_SQL = "CREATE TABLE IF NOT EXISTS test('$.ID' INT, val BLOB);"; - - const std::string COMPOSITE_PRIMARY_KEY_TABLE_SQL = R"(CREATE TABLE workers ( - worker_id INTEGER, - last_name VARCHAR NOT NULL, - first_name VARCHAR, - join_date DATE, - PRIMARY KEY (last_name, first_name) - );)"; - - const std::string INSERT_SYNC_DATA_SQL = "INSERT OR REPLACE INTO sync_data (key, timestamp, flag, hash_key) " - "VALUES('KEY', 123456789, 1, 'HASH_KEY');"; - - const std::string INVALID_TABLE_FIELD_SQL = "create table if not exists t1 ('1 = 1; --' int primary key, b blob)"; - - void PrepareVirtualDeviceEnv(const std::string &tableName, const std::string &dbPath, - const std::vector &remoteDeviceVec) - { - sqlite3 *db = RelationalTestUtils::CreateDataBase(dbPath); - ASSERT_NE(db, nullptr); - TableInfo tableInfo; - SQLiteUtils::AnalysisSchema(db, tableName, tableInfo); - for (const auto &dev : remoteDeviceVec) { - std::vector fieldInfoList = tableInfo.GetFieldInfos(); - dev->SetLocalFieldInfo(fieldInfoList); - dev->SetTableInfo(tableInfo); - } - EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); +constexpr const char* DB_SUFFIX = ".db"; +constexpr const char* STORE_ID = "Relational_Store_ID"; +std::string g_testDir; +std::string g_dbDir; +DistributedDB::RelationalStoreManager g_mgr(APP_ID, USER_ID); + +const std::string DEVICE_A = "real_device"; +const std::string DEVICE_B = "deviceB"; +VirtualCommunicatorAggregator* g_communicatorAggregator = nullptr; +RelationalVirtualDevice *g_deviceB = nullptr; + +const std::string NORMAL_CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS sync_data(" \ + "key BLOB NOT NULL UNIQUE," \ + "value BLOB," \ + "timestamp INT NOT NULL," \ + "flag INT NOT NULL," \ + "device BLOB," \ + "ori_device BLOB," \ + "hash_key BLOB PRIMARY KEY NOT NULL," \ + "w_timestamp INT," \ + "UNIQUE(device, ori_device));" \ + "CREATE INDEX key_index ON sync_data(key, flag);"; + +const std::string SIMPLE_CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS t1(a INT, b TEXT)"; + +const std::string CREATE_TABLE_SQL_NO_PRIMARY_KEY = "CREATE TABLE IF NOT EXISTS sync_data(" \ + "key BLOB NOT NULL UNIQUE," \ + "value BLOB," \ + "timestamp INT NOT NULL," \ + "flag INT NOT NULL," \ + "device BLOB," \ + "ori_device BLOB," \ + "hash_key BLOB NOT NULL," \ + "w_timestamp INT," \ + "UNIQUE(device, ori_device));" \ + "CREATE INDEX key_index ON sync_data (key, flag);"; + +const std::string UNSUPPORTED_FIELD_TABLE_SQL = "CREATE TABLE IF NOT EXISTS test('$.ID' INT, val BLOB);"; + +const std::string COMPOSITE_PRIMARY_KEY_TABLE_SQL = R"(CREATE TABLE workers ( + worker_id INTEGER, + last_name VARCHAR NOT NULL, + first_name VARCHAR, + join_date DATE, + PRIMARY KEY (last_name, first_name) + );)"; + +const std::string INSERT_SYNC_DATA_SQL = "INSERT OR REPLACE INTO sync_data (key, timestamp, flag, hash_key) " + "VALUES('KEY', 123456789, 1, 'HASH_KEY');"; + +const std::string INVALID_TABLE_FIELD_SQL = "create table if not exists t1 ('1 = 1; --' int primary key, b blob)"; + +void PrepareVirtualDeviceEnv(const std::string &tableName, const std::string &dbPath, + const std::vector &remoteDeviceVec) +{ + sqlite3 *db = RelationalTestUtils::CreateDataBase(dbPath); + ASSERT_NE(db, nullptr); + TableInfo tableInfo; + SQLiteUtils::AnalysisSchema(db, tableName, tableInfo); + for (const auto &dev : remoteDeviceVec) { + std::vector fieldInfoList = tableInfo.GetFieldInfos(); + dev->SetLocalFieldInfo(fieldInfoList); + dev->SetTableInfo(tableInfo); } + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); } class DistributedDBInterfacesRelationalTest : public testing::Test { @@ -156,14 +155,7 @@ void DistributedDBInterfacesRelationalTest::TearDown(void) } } -/** - * @tc.name: RelationalStoreTest001 - * @tc.desc: Test open store and create distributed db - * @tc.type: FUNC - * @tc.require: AR000GK58F - * @tc.author: lianhuix - */ -HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest001, TestSize.Level1) +void NoramlCreateDistributedTableTest(TableSyncType tableSyncType) { /** * @tc.steps:step1. Prepare db file @@ -185,11 +177,11 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest001, TestSize EXPECT_EQ(status, OK); ASSERT_NE(delegate, nullptr); - status = delegate->CreateDistributedTable("sync_data"); + status = delegate->CreateDistributedTable("sync_data", tableSyncType); EXPECT_EQ(status, OK); // test create same table again - status = delegate->CreateDistributedTable("sync_data"); + status = delegate->CreateDistributedTable("sync_data", tableSyncType); EXPECT_EQ(status, OK); status = g_mgr.CloseStore(delegate); @@ -216,6 +208,30 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest001, TestSize EXPECT_EQ(status, OK); } +/** + * @tc.name: RelationalStoreTest001 + * @tc.desc: Test open store and create distributed db with DEVICE_COOPERATION type + * @tc.type: FUNC + * @tc.require: AR000GK58F + * @tc.author: lianhuix + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest001, TestSize.Level1) +{ + NoramlCreateDistributedTableTest(DistributedDB::DEVICE_COOPERATION); +} + +/** + * @tc.name: RelationalStoreTest001 + * @tc.desc: Test open store and create distributed db with CLOUD_COOPERATION type + * @tc.type: FUNC + * @tc.require: AR000GK58F + * @tc.author: lianhuix + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest001_1, TestSize.Level1) +{ + NoramlCreateDistributedTableTest(DistributedDB::CLOUD_COOPERATION); +} + /** * @tc.name: RelationalStoreTest002 * @tc.desc: Test open store with invalid path or store ID @@ -299,14 +315,7 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest003, TestSize ASSERT_EQ(delegate, nullptr); } -/** - * @tc.name: RelationalStoreTest004 - * @tc.desc: Test create distributed table with over limit - * @tc.type: FUNC - * @tc.require: AR000GK58F - * @tc.author: lianhuix - */ -HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest004, TestSize.Level1) +void CreateDistributedTableOverLimitTest(TableSyncType tableSyncTpe) { /** * @tc.steps:step1. Prepare db file with multiple tables @@ -316,7 +325,7 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest004, TestSize ASSERT_NE(db, nullptr); EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK); const int tableCount = DBConstant::MAX_DISTRIBUTED_TABLE_COUNT + 10; // 10: additional size for test abnormal scene - for (int i=0; iCreateDistributedTable("TEST_" + std::to_string(i)), OK); + EXPECT_EQ(delegate->CreateDistributedTable("TEST_" + std::to_string(i), tableSyncTpe), OK); } else { - EXPECT_NE(delegate->CreateDistributedTable("TEST_" + std::to_string(i)), OK); + EXPECT_NE(delegate->CreateDistributedTable("TEST_" + std::to_string(i), tableSyncTpe), OK); } } @@ -349,13 +358,30 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest004, TestSize } /** - * @tc.name: RelationalStoreTest005 - * @tc.desc: Test create distributed table with invalid table name + * @tc.name: RelationalStoreTest004 + * @tc.desc: Test create distributed table with over limit for DEVICE_COOPERATION type * @tc.type: FUNC * @tc.require: AR000GK58F * @tc.author: lianhuix */ -HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest005, TestSize.Level1) +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest004, TestSize.Level1) +{ + CreateDistributedTableOverLimitTest(DistributedDB::DEVICE_COOPERATION); +} + +/** + * @tc.name: RelationalStoreTest004 + * @tc.desc: Test create distributed table with over limit for CLOUD_COOPERATION type + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest004_1, TestSize.Level1) +{ + CreateDistributedTableOverLimitTest(DistributedDB::CLOUD_COOPERATION); +} + +void CreateDistributedTableInvalidArgsTest(TableSyncType tableSyncType) { /** * @tc.steps:step1. Prepare db file @@ -379,21 +405,25 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest005, TestSize * @tc.steps:step3. Create distributed table with invalid table name * @tc.expected: step3. Create distributed table failed. */ - EXPECT_NE(delegate->CreateDistributedTable(DBConstant::SYSTEM_TABLE_PREFIX + "_tmp"), OK); + EXPECT_NE(delegate->CreateDistributedTable(DBConstant::SYSTEM_TABLE_PREFIX + "_tmp", tableSyncType), OK); - EXPECT_EQ(delegate->CreateDistributedTable("Handle-J@^."), INVALID_ARGS); + EXPECT_EQ(delegate->CreateDistributedTable("Handle-J@^.", tableSyncType), INVALID_ARGS); + EXPECT_EQ(delegate->CreateDistributedTable("sync_data", + static_cast(DistributedDB::DEVICE_COOPERATION - 1)), INVALID_ARGS); + EXPECT_EQ(delegate->CreateDistributedTable("sync_data", + static_cast(DistributedDB::CLOUD_COOPERATION + 1)), INVALID_ARGS); EXPECT_EQ(RelationalTestUtils::ExecSql(db, INVALID_TABLE_FIELD_SQL), SQLITE_OK); - EXPECT_EQ(delegate->CreateDistributedTable("t1"), NOT_SUPPORT); + EXPECT_EQ(delegate->CreateDistributedTable("t1", tableSyncType), NOT_SUPPORT); /** * @tc.steps:step4. Create distributed table temp table or not exist table * @tc.expected: step4. Create distributed table failed. */ - EXPECT_EQ(delegate->CreateDistributedTable("child"), NOT_FOUND); + EXPECT_EQ(delegate->CreateDistributedTable("child", tableSyncType), NOT_FOUND); std::string tempTableSql = "CREATE TEMP TABLE child(x, y, z)"; EXPECT_EQ(RelationalTestUtils::ExecSql(db, tempTableSql), SQLITE_OK); - EXPECT_EQ(delegate->CreateDistributedTable("child"), NOT_FOUND); + EXPECT_EQ(delegate->CreateDistributedTable("child", tableSyncType), NOT_FOUND); /** * @tc.steps:step5. Close store @@ -405,13 +435,30 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest005, TestSize } /** - * @tc.name: RelationalStoreTest006 - * @tc.desc: Test create distributed table with non primary key schema + * @tc.name: RelationalStoreTest005 + * @tc.desc: Test create distributed table with invalid table name or invalid table sync type * @tc.type: FUNC * @tc.require: AR000GK58F * @tc.author: lianhuix */ -HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest006, TestSize.Level1) +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest005, TestSize.Level1) +{ + CreateDistributedTableInvalidArgsTest(DistributedDB::DEVICE_COOPERATION); +} + +/** + * @tc.name: RelationalStoreTest005 + * @tc.desc: Test create distributed table with invalid table name or invalid table sync type + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest005_1, TestSize.Level1) +{ + CreateDistributedTableInvalidArgsTest(DistributedDB::CLOUD_COOPERATION); +} + +void CreateDistributedTableNonPrimaryKeyTest(TableSyncType tableSyncType) { /** * @tc.steps:step1. Prepare db file @@ -433,10 +480,10 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest006, TestSize ASSERT_NE(delegate, nullptr); /** - * @tc.steps:step3. Create distributed table with invalid table name - * @tc.expected: step3. Create distributed table failed. + * @tc.steps:step3. Create distributed table with valid table name + * @tc.expected: step3. Create distributed table success. */ - EXPECT_EQ(delegate->CreateDistributedTable("sync_data"), OK); + EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), OK); /** * @tc.steps:step4. Close store @@ -455,13 +502,30 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest006, TestSize } /** - * @tc.name: RelationalStoreTest007 - * @tc.desc: Test create distributed table with table has invalid field name + * @tc.name: RelationalStoreTest006 + * @tc.desc: Test create distributed table with non primary key schema * @tc.type: FUNC * @tc.require: AR000GK58F * @tc.author: lianhuix */ -HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest007, TestSize.Level1) +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest006, TestSize.Level1) +{ + CreateDistributedTableNonPrimaryKeyTest(DistributedDB::DEVICE_COOPERATION); +} + +/** + * @tc.name: RelationalStoreTest006 + * @tc.desc: Test create distributed table with non primary key schema + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest006_1, TestSize.Level1) +{ + CreateDistributedTableNonPrimaryKeyTest(DistributedDB::CLOUD_COOPERATION); +} + +void CreateDistributedTableInvalidFieldTest(TableSyncType tableSyncType) { sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX); ASSERT_NE(db, nullptr); @@ -474,19 +538,36 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest007, TestSize EXPECT_EQ(status, OK); ASSERT_NE(delegate, nullptr); - EXPECT_EQ(delegate->CreateDistributedTable("test"), NOT_SUPPORT); + EXPECT_EQ(delegate->CreateDistributedTable("test", tableSyncType), NOT_SUPPORT); status = g_mgr.CloseStore(delegate); EXPECT_EQ(status, OK); } /** - * @tc.name: RelationalStoreTest008 - * @tc.desc: Test create distributed table with table has composite primary keys + * @tc.name: RelationalStoreTest007 + * @tc.desc: Test create distributed table with table has invalid field name * @tc.type: FUNC * @tc.require: AR000GK58F * @tc.author: lianhuix */ -HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest008, TestSize.Level1) +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest007, TestSize.Level1) +{ + CreateDistributedTableInvalidFieldTest(DistributedDB::DEVICE_COOPERATION); +} + +/** + * @tc.name: RelationalStoreTest007 + * @tc.desc: Test create distributed table with table has invalid field name + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest007_1, TestSize.Level1) +{ + CreateDistributedTableInvalidFieldTest(DistributedDB::CLOUD_COOPERATION); +} + +void CreateDistributedTableCompositePKTest(TableSyncType tableSyncType, int expectCode) { sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX); ASSERT_NE(db, nullptr); @@ -499,19 +580,36 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest008, TestSize EXPECT_EQ(status, OK); ASSERT_NE(delegate, nullptr); - EXPECT_EQ(delegate->CreateDistributedTable("workers"), NOT_SUPPORT); + EXPECT_EQ(delegate->CreateDistributedTable("workers", tableSyncType), expectCode); status = g_mgr.CloseStore(delegate); EXPECT_EQ(status, OK); } /** - * @tc.name: RelationalStoreTest009 - * @tc.desc: Test create distributed table with table has history data + * @tc.name: RelationalStoreTest008 + * @tc.desc: Test create distributed table with table has composite primary keys for DEVICE_COOPERATION * @tc.type: FUNC * @tc.require: AR000GK58F * @tc.author: lianhuix */ -HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest009, TestSize.Level1) +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest008, TestSize.Level1) +{ + CreateDistributedTableCompositePKTest(DistributedDB::DEVICE_COOPERATION, NOT_SUPPORT); +} + +/** + * @tc.name: RelationalStoreTest008 + * @tc.desc: Test create distributed table with table has composite primary keys for CLOUD_COOPERATION + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest008_1, TestSize.Level1) +{ + CreateDistributedTableCompositePKTest(DistributedDB::CLOUD_COOPERATION, OK); +} + +void CreateDistributedTableWithHistoryDataTest(TableSyncType tableSyncType) { sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX); ASSERT_NE(db, nullptr); @@ -530,9 +628,31 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest009, TestSize EXPECT_EQ(status, OK); } +/** + * @tc.name: RelationalStoreTest009 + * @tc.desc: Test create distributed table with table has history data for DEVICE_COOPERATION + * @tc.type: FUNC + * @tc.require: AR000GK58F + * @tc.author: lianhuix + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest009, TestSize.Level1) +{ + CreateDistributedTableWithHistoryDataTest(DistributedDB::DEVICE_COOPERATION); +} + +/** + * @tc.name: RelationalStoreTest009 + * @tc.desc: Test create distributed table with table has history data for CLOUD_COOPERATION + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalStoreTest009_1, TestSize.Level1) +{ + CreateDistributedTableWithHistoryDataTest(DistributedDB::CLOUD_COOPERATION); +} -namespace { -void TableModifyTest(const std::string &modifySql, DBStatus expect) +void TableModifyTest(const std::string &modifySql, TableSyncType tableSyncType, DBStatus expect) { /** * @tc.steps:step1. Prepare db file @@ -560,7 +680,7 @@ void TableModifyTest(const std::string &modifySql, DBStatus expect) * @tc.steps:step3. Create distributed table * @tc.expected: step3. Create distributed table OK. */ - EXPECT_EQ(delegate->CreateDistributedTable("sync_data"), OK); + EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), OK); /** * @tc.steps:step4. Upgrade table with modifySql @@ -572,7 +692,7 @@ void TableModifyTest(const std::string &modifySql, DBStatus expect) * @tc.steps:step5. Create distributed table again * @tc.expected: step5. Create distributed table return expect. */ - EXPECT_EQ(delegate->CreateDistributedTable("sync_data"), expect); + EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), expect); /** * @tc.steps:step6. Close store @@ -582,7 +702,6 @@ void TableModifyTest(const std::string &modifySql, DBStatus expect) EXPECT_EQ(status, OK); EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); } -} /** * @tc.name: RelationalTableModifyTest001 @@ -593,7 +712,8 @@ void TableModifyTest(const std::string &modifySql, DBStatus expect) */ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest001, TestSize.Level1) { - TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL DEFAULT 123;", OK); + TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL DEFAULT 123;", + DistributedDB::DEVICE_COOPERATION, OK); } /** @@ -605,7 +725,8 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest001, Te */ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest002, TestSize.Level1) { - TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL;", SCHEMA_MISMATCH); + TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL;", + DistributedDB::DEVICE_COOPERATION, SCHEMA_MISMATCH); } /** @@ -617,17 +738,50 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest002, Te */ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest003, TestSize.Level1) { - TableModifyTest("ALTER TABLE sync_data DROP COLUMN w_timestamp;", SCHEMA_MISMATCH); + TableModifyTest("ALTER TABLE sync_data DROP COLUMN w_timestamp;", + DistributedDB::DEVICE_COOPERATION, SCHEMA_MISMATCH); } /** - * @tc.name: RelationalTableModifyTest004 - * @tc.desc: Test upgrade distributed table with device table exists + * @tc.name: RelationalTableModifyTest001 + * @tc.desc: Test modify distributed table with compatible upgrade * @tc.type: FUNC - * @tc.require: AR000GK58F - * @tc.author: lianhuix + * @tc.require: + * @tc.author: zhangshijie */ -HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest004, TestSize.Level1) +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest001_1, TestSize.Level1) +{ + TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL DEFAULT 123;", + DistributedDB::CLOUD_COOPERATION, OK); +} + +/** + * @tc.name: RelationalTableModifyTest002 + * @tc.desc: Test modify distributed table with incompatible upgrade + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest002_1, TestSize.Level1) +{ + TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field INTEGER NOT NULL;", + DistributedDB::CLOUD_COOPERATION, SCHEMA_MISMATCH); +} + +/** + * @tc.name: RelationalTableModifyTest003 + * @tc.desc: Test modify distributed table with incompatible upgrade + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest003_1, TestSize.Level1) +{ + TableModifyTest("ALTER TABLE sync_data DROP COLUMN w_timestamp;", + DistributedDB::CLOUD_COOPERATION, SCHEMA_MISMATCH); +} + +void UpgradeDistributedTableTest(TableSyncType tableSyncType) { /** * @tc.steps:step1. Prepare db file @@ -654,7 +808,7 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest004, Te * @tc.steps:step3. Create distributed table * @tc.expected: step3. Create distributed table OK. */ - EXPECT_EQ(delegate->CreateDistributedTable("sync_data"), OK); + EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), OK); /** * @tc.steps:step4. Upgrade table @@ -672,7 +826,7 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest004, Te * @tc.steps:step5. Create distributed table again * @tc.expected: step5. Create distributed table return expect. */ - EXPECT_EQ(delegate->CreateDistributedTable("sync_data"), OK); + EXPECT_EQ(delegate->CreateDistributedTable("sync_data", tableSyncType), OK); /** * @tc.steps:step6. Close store @@ -683,6 +837,30 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest004, Te EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); } +/** + * @tc.name: RelationalTableModifyTest004 + * @tc.desc: Test upgrade distributed table with device table exists + * @tc.type: FUNC + * @tc.require: AR000GK58F + * @tc.author: lianhuix + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest004, TestSize.Level1) +{ + UpgradeDistributedTableTest(DistributedDB::DEVICE_COOPERATION); +} + +/** + * @tc.name: RelationalTableModifyTest004 + * @tc.desc: Test upgrade distributed table with device table exists + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest004_1, TestSize.Level1) +{ + UpgradeDistributedTableTest(DistributedDB::CLOUD_COOPERATION); +} + /** * @tc.name: RelationalTableModifyTest005 * @tc.desc: Test modify distributed table with compatible upgrade @@ -692,7 +870,21 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest004, Te */ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest005, TestSize.Level1) { - TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field STRING NOT NULL DEFAULT 'asdf';", OK); + TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field STRING NOT NULL DEFAULT 'asdf';", + DistributedDB::DEVICE_COOPERATION, OK); +} + +/** + * @tc.name: RelationalTableModifyTest005 + * @tc.desc: Test modify distributed table with compatible upgrade + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshijie + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalTableModifyTest005_1, TestSize.Level1) +{ + TableModifyTest("ALTER TABLE sync_data ADD COLUMN add_field STRING NOT NULL DEFAULT 'asdf';", + DistributedDB::CLOUD_COOPERATION, OK); } /** @@ -755,7 +947,6 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest00 EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); } -namespace { struct TableT1 { int a; std::string b; @@ -794,7 +985,6 @@ void AddDeviceSchema(RelationalVirtualDevice *device, sqlite3 *db, const std::st device->SetLocalFieldInfo(table.GetFieldInfos()); device->SetTableInfo(table); } -} /** * @tc.name: RelationalRemoveDeviceDataTest002 @@ -916,6 +1106,7 @@ void TestRemoveDeviceDataWithCallback(bool removeAll) EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); RuntimeConfig::SetTranslateToDeviceIdCallback(nullptr); } + /** * @tc.name: RelationalRemoveDeviceDataTest003 * @tc.desc: Test remove all device data and sync again @@ -1114,40 +1305,40 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalOpenStorePressureTest0 } namespace { -void ProcessSync(RelationalStoreDelegate *delegate) -{ - std::vector devices = {DEVICE_B}; - Query query = Query::Select("create").EqualTo("create", 1); - DBStatus status = delegate->Sync(devices, SyncMode::SYNC_MODE_PUSH_ONLY, query, - [&devices](const std::map> &devicesMap) { - EXPECT_EQ(devicesMap.size(), devices.size()); - EXPECT_EQ(devicesMap.at(DEVICE_B)[0].status, OK); - }, true); - EXPECT_EQ(status, OK); + void ProcessSync(RelationalStoreDelegate *delegate) + { + std::vector devices = {DEVICE_B}; + Query query = Query::Select("create").EqualTo("create", 1); + DBStatus status = delegate->Sync(devices, SyncMode::SYNC_MODE_PUSH_ONLY, query, + [&devices](const std::map> &devicesMap) { + EXPECT_EQ(devicesMap.size(), devices.size()); + EXPECT_EQ(devicesMap.at(DEVICE_B)[0].status, OK); + }, true); + EXPECT_EQ(status, OK); - std::vector data; - g_deviceB->GetAllSyncData("create", data); - EXPECT_EQ(data.size(), 1u); - - VirtualRowData virtualRowData; - DataValue d1; - d1 = static_cast(2); // 2: test data - virtualRowData.objectData.PutDataValue("create", d1); - DataValue d2; - d2.SetText("hello"); - virtualRowData.objectData.PutDataValue("ddd", d2); - DataValue d3; - d3.SetText("hello"); - virtualRowData.objectData.PutDataValue("eee", d3); - virtualRowData.logInfo.timestamp = 1; - g_deviceB->PutData("create", {virtualRowData}); - status = delegate->Sync(devices, SyncMode::SYNC_MODE_PULL_ONLY, query, - [&devices](const std::map> &devicesMap) { - EXPECT_EQ(devicesMap.size(), devices.size()); - EXPECT_EQ(devicesMap.at(DEVICE_B)[0].status, OK); - }, true); - EXPECT_EQ(status, OK); -} + std::vector data; + g_deviceB->GetAllSyncData("create", data); + EXPECT_EQ(data.size(), 1u); + + VirtualRowData virtualRowData; + DataValue d1; + d1 = static_cast(2); // 2: test data + virtualRowData.objectData.PutDataValue("create", d1); + DataValue d2; + d2.SetText("hello"); + virtualRowData.objectData.PutDataValue("ddd", d2); + DataValue d3; + d3.SetText("hello"); + virtualRowData.objectData.PutDataValue("eee", d3); + virtualRowData.logInfo.timestamp = 1; + g_deviceB->PutData("create", {virtualRowData}); + status = delegate->Sync(devices, SyncMode::SYNC_MODE_PULL_ONLY, query, + [&devices](const std::map> &devicesMap) { + EXPECT_EQ(devicesMap.size(), devices.size()); + EXPECT_EQ(devicesMap.at(DEVICE_B)[0].status, OK); + }, true); + EXPECT_EQ(status, OK); + } } /** @@ -1164,7 +1355,7 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, SqliteKeyWordTest001, TestSize.L EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK); std::string tableSql = "CREATE TABLE IF NOT EXISTS 'create' ('create' INTEGER PRIMARY KEY, b 'CREATE', " \ - "c TEXT DEFAULT 'DEFAULT', UNIQUE(b, c))"; + "c TEXT DEFAULT 'DEFAULT', UNIQUE(b, c))"; EXPECT_EQ(RelationalTestUtils::ExecSql(db, tableSql), SQLITE_OK); std::string indexSql = "CREATE INDEX IF NOT EXISTS 'index' on 'create' (b)"; EXPECT_EQ(RelationalTestUtils::ExecSql(db, indexSql), SQLITE_OK); @@ -1231,4 +1422,128 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, GetDistributedTableName001, Test devTableName = RelationalStoreManager::GetDistributedTableName("", tableName); EXPECT_EQ(devTableName, DBConstant::RELATIONAL_PREFIX + tableName + "_"); RuntimeConfig::SetTranslateToDeviceIdCallback(nullptr); +} + +/** + * @tc.name: CloudRelationalStoreTest001 + * @tc.desc: Test create distributed table in cloud table sync type + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshjie + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest001, TestSize.Level0) +{ + /** + * @tc.steps:step1. Prepare db file + * @tc.expected: step1. Return OK. + */ + sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX); + ASSERT_NE(db, nullptr); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK); + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); + + /** + * @tc.steps:step2. open relational store, create distributed table with CLOUD_COOPERATION + * @tc.expected: step2. Return OK. + */ + RelationalStoreDelegate *delegate = nullptr; + DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate); + EXPECT_EQ(status, OK); + ASSERT_NE(delegate, nullptr); + + status = delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION); + EXPECT_EQ(status, OK); + + /** + * @tc.steps:step3. open relational store, create distributed table with CLOUD_COOPERATION again + * @tc.expected: step3. Return OK. + */ + status = delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION); + EXPECT_EQ(status, OK); + + status = g_mgr.CloseStore(delegate); + EXPECT_EQ(status, OK); +} + +/** + * @tc.name: CloudRelationalStoreTest002 + * @tc.desc: Test create distributed table in diff table sync type for the same table + * @tc.type: FUNC + * @tc.require: + * @tc.author: zhangshjie + */ +HWTEST_F(DistributedDBInterfacesRelationalTest, CreateDistributedTableTest002, TestSize.Level0) +{ + /** + * @tc.steps:step1. Prepare db file + * @tc.expected: step1. Return OK. + */ + sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX); + ASSERT_NE(db, nullptr); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK); + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); + + /** + * @tc.steps:step2. open relational store, create distributed table with DEVICE_COOPERATION + * @tc.expected: step2. Return OK. + */ + RelationalStoreDelegate *delegate = nullptr; + DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate); + EXPECT_EQ(status, OK); + ASSERT_NE(delegate, nullptr); + + status = delegate->CreateDistributedTable("sync_data", DistributedDB::DEVICE_COOPERATION); + EXPECT_EQ(status, OK); + + /** + * @tc.steps:step3. create distributed table with CLOUD_COOPERATION again + * @tc.expected: step3. Return TYPE_MISMATCH. + */ + status = delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION); + EXPECT_EQ(status, TYPE_MISMATCH); + + status = g_mgr.CloseStore(delegate); + EXPECT_EQ(status, OK); + delegate = nullptr; + + /** + * @tc.steps:step4. drop table sync_data and create again + * @tc.expected: step4. Return OK. + */ + db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX); + ASSERT_NE(db, nullptr); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK); + const std::string dropSql = "drop table sync_data;"; + EXPECT_EQ(RelationalTestUtils::ExecSql(db, dropSql), SQLITE_OK); + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); + + /** + * @tc.steps:step5. open relational store, create distributed table with CLOUD_COOPERATION + * @tc.expected: step5. Return OK. + */ + status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate); + EXPECT_EQ(status, OK); + ASSERT_NE(delegate, nullptr); + + db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX); + ASSERT_NE(db, nullptr); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, NORMAL_CREATE_TABLE_SQL), SQLITE_OK); + EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); + + EXPECT_EQ(delegate->CreateDistributedTable("sync_data", DistributedDB::CLOUD_COOPERATION), OK); + + /** + * @tc.steps:step6. create distributed table with DEVICE_COOPERATION again + * @tc.expected: step6. Return TYPE_MISMATCH. + */ + status = delegate->CreateDistributedTable("sync_data", DistributedDB::DEVICE_COOPERATION); + EXPECT_EQ(status, TYPE_MISMATCH); + + status = g_mgr.CloseStore(delegate); + EXPECT_EQ(status, OK); + delegate = nullptr; +} } \ No newline at end of file diff --git a/kv_store/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp b/kv_store/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp index 15917ba4..bd61dc7a 100644 --- a/kv_store/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp +++ b/kv_store/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_relational_multi_user_test.cpp @@ -890,7 +890,8 @@ HWTEST_F(DistributedDBRelationalMultiUserTest, RdbMultiUser010, TestSize.Level1) * @tc.expected: step4. should return OK, not NOT_ACTIVE */ Query query = Query::Select(g_tableName); - EXPECT_EQ(g_rdbDelegatePtr1->Sync({DEVICE_B}, SYNC_MODE_PUSH_ONLY, query, nullptr, true), OK); + SyncStatusCallback callback = nullptr; + EXPECT_EQ(g_rdbDelegatePtr1->Sync({DEVICE_B}, SYNC_MODE_PUSH_ONLY, query, callback, true), OK); CloseStore(); } diff --git a/kv_store/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp b/kv_store/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp index 31c89841..5e2b6576 100644 --- a/kv_store/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp +++ b/kv_store/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_communicator_aggregator.cpp @@ -86,7 +86,6 @@ int VirtualCommunicatorAggregator::RegOnConnectCallback(const OnConnectCallback void VirtualCommunicatorAggregator::RunCommunicatorLackCallback(const LabelType &commLabel) { if (onCommLack_) { - std::string userId; onCommLack_(commLabel, userId_); } } diff --git a/kv_store/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp b/kv_store/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp index 0371c1fc..3e5b5440 100644 --- a/kv_store/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp +++ b/kv_store/frameworks/libs/distributeddb/test/unittest/common/syncer/virtual_relational_ver_sync_db_interface.cpp @@ -69,7 +69,7 @@ VirtualRelationalVerSyncDBInterface::VirtualRelationalVerSyncDBInterface() LOGD("virtual device init db createTime"); } -int VirtualRelationalVerSyncDBInterface::PutSyncDataWithQuery(const QueryObject &object, +int VirtualRelationalVerSyncDBInterface::PutSyncDataWithQuery(const QueryObject &query, const std::vector &entries, const std::string &deviceName) { LOGD("[PutSyncData] size %zu", entries.size()); @@ -89,7 +89,7 @@ int VirtualRelationalVerSyncDBInterface::PutSyncDataWithQuery(const QueryObject } } OptTableDataWithLog optTableDataWithLog; - optTableDataWithLog.tableName = object.GetTableName(); + optTableDataWithLog.tableName = query.GetTableName(); int errCode = DataTransformer::TransformDataItem(dataItems, localFieldInfo_, localFieldInfo_, optTableDataWithLog); if (errCode != E_OK) { @@ -107,7 +107,7 @@ int VirtualRelationalVerSyncDBInterface::PutSyncDataWithQuery(const QueryObject virtualRowData.objectData.PutDataValue(localFieldInfo_[index].GetFieldName(), dataValue); index++; } - syncData_[object.GetTableName()][GetStr(virtualRowData.logInfo.hashKey)] = virtualRowData; + syncData_[query.GetTableName()][GetStr(virtualRowData.logInfo.hashKey)] = virtualRowData; } LOGD("tableName %s", optTableDataWithLog.tableName.c_str()); return errCode; diff --git a/kv_store/interfaces/innerkits/distributeddata/include/kv_utils.h b/kv_store/interfaces/innerkits/distributeddata/include/kv_utils.h index 358ee51a..5f25f0f0 100644 --- a/kv_store/interfaces/innerkits/distributeddata/include/kv_utils.h +++ b/kv_store/interfaces/innerkits/distributeddata/include/kv_utils.h @@ -22,6 +22,7 @@ #include "datashare_values_bucket.h" #include "kvstore_result_set.h" #include "result_set_bridge.h" +#include "visibility.h" namespace OHOS { namespace DistributedKv { @@ -36,11 +37,12 @@ public: DOUBLE = 5, INVALID = 255 }; - static std::shared_ptr ToResultSetBridge(std::shared_ptr resultSet); - static Status ToQuery(const DataShare::DataShareAbsPredicates &predicates, DataQuery &query); - static Entry ToEntry(const DataShare::DataShareValuesBucket &valueBucket); - static std::vector ToEntries(const std::vector &valueBuckets); - static Status GetKeys(const DataShare::DataShareAbsPredicates &predicates, std::vector &keys); + API_EXPORT static std::shared_ptr ToResultSetBridge( + std::shared_ptr resultSet); + API_EXPORT static Status ToQuery(const DataShare::DataShareAbsPredicates &predicates, DataQuery &query); + API_EXPORT static Entry ToEntry(const DataShare::DataShareValuesBucket &valueBucket); + API_EXPORT static std::vector ToEntries(const std::vector &valueBuckets); + API_EXPORT static Status GetKeys(const DataShare::DataShareAbsPredicates &predicates, std::vector &keys); private: static void NoSupport(const DataShare::OperationItem &oper, DataQuery &query); static void EqualTo(const DataShare::OperationItem &oper, DataQuery &query); diff --git a/kv_store/interfaces/jskits/distributeddata/distributed_data.js b/kv_store/interfaces/jskits/distributeddata/distributed_data.js index 1a72602f..504578cc 100644 --- a/kv_store/interfaces/jskits/distributeddata/distributed_data.js +++ b/kv_store/interfaces/jskits/distributeddata/distributed_data.js @@ -15,50 +15,50 @@ const distributedDataSo = requireInternal('data.distributedData'); export default { - createKVManager: distributedDataSo.createKVManager, - Query: distributedDataSo.Query, - FieldNode: distributedDataSo.FieldNode, - Schema: distributedDataSo.Schema, - UserType: { - SAME_USER_ID: 0, - }, - Constants: { - MAX_KEY_LENGTH: 1024, - MAX_VALUE_LENGTH: 4194303, - MAX_KEY_LENGTH_DEVICE: 896, - MAX_STORE_ID_LENGTH: 128, - MAX_QUERY_LENGTH: 512000, - MAX_BATCH_SIZE: 128, - }, - ValueType: { - STRING: 0, - INTEGER: 1, - FLOAT: 2, - BYTE_ARRAY: 3, - BOOLEAN: 4, - DOUBLE: 5, - }, - SyncMode: { - PULL_ONLY: 0, - PUSH_ONLY: 1, - PUSH_PULL: 2, - }, - SubscribeType: { - SUBSCRIBE_TYPE_LOCAL: 0, - SUBSCRIBE_TYPE_REMOTE: 1, - SUBSCRIBE_TYPE_ALL: 2, - }, - KVStoreType: { - DEVICE_COLLABORATION: 0, - SINGLE_VERSION: 1, - MULTI_VERSION: 2, - }, - SecurityLevel: { - NO_LEVEL: 0, - S0: 1, - S1: 2, - S2: 3, - S3: 5, - S4: 6, - }, -} \ No newline at end of file + createKVManager: distributedDataSo.createKVManager, + Query: distributedDataSo.Query, + FieldNode: distributedDataSo.FieldNode, + Schema: distributedDataSo.Schema, + UserType: { + SAME_USER_ID: 0, + }, + Constants: { + MAX_KEY_LENGTH: 1024, + MAX_VALUE_LENGTH: 4194303, + MAX_KEY_LENGTH_DEVICE: 896, + MAX_STORE_ID_LENGTH: 128, + MAX_QUERY_LENGTH: 512000, + MAX_BATCH_SIZE: 128, + }, + ValueType: { + STRING: 0, + INTEGER: 1, + FLOAT: 2, + BYTE_ARRAY: 3, + BOOLEAN: 4, + DOUBLE: 5, + }, + SyncMode: { + PULL_ONLY: 0, + PUSH_ONLY: 1, + PUSH_PULL: 2, + }, + SubscribeType: { + SUBSCRIBE_TYPE_LOCAL: 0, + SUBSCRIBE_TYPE_REMOTE: 1, + SUBSCRIBE_TYPE_ALL: 2, + }, + KVStoreType: { + DEVICE_COLLABORATION: 0, + SINGLE_VERSION: 1, + MULTI_VERSION: 2, + }, + SecurityLevel: { + NO_LEVEL: 0, + S0: 1, + S1: 2, + S2: 3, + S3: 5, + S4: 6, + }, +}; \ No newline at end of file diff --git a/kv_store/interfaces/jskits/distributedkvstore/distributed_kvstore.js b/kv_store/interfaces/jskits/distributedkvstore/distributed_kvstore.js index 1a72602f..504578cc 100644 --- a/kv_store/interfaces/jskits/distributedkvstore/distributed_kvstore.js +++ b/kv_store/interfaces/jskits/distributedkvstore/distributed_kvstore.js @@ -15,50 +15,50 @@ const distributedDataSo = requireInternal('data.distributedData'); export default { - createKVManager: distributedDataSo.createKVManager, - Query: distributedDataSo.Query, - FieldNode: distributedDataSo.FieldNode, - Schema: distributedDataSo.Schema, - UserType: { - SAME_USER_ID: 0, - }, - Constants: { - MAX_KEY_LENGTH: 1024, - MAX_VALUE_LENGTH: 4194303, - MAX_KEY_LENGTH_DEVICE: 896, - MAX_STORE_ID_LENGTH: 128, - MAX_QUERY_LENGTH: 512000, - MAX_BATCH_SIZE: 128, - }, - ValueType: { - STRING: 0, - INTEGER: 1, - FLOAT: 2, - BYTE_ARRAY: 3, - BOOLEAN: 4, - DOUBLE: 5, - }, - SyncMode: { - PULL_ONLY: 0, - PUSH_ONLY: 1, - PUSH_PULL: 2, - }, - SubscribeType: { - SUBSCRIBE_TYPE_LOCAL: 0, - SUBSCRIBE_TYPE_REMOTE: 1, - SUBSCRIBE_TYPE_ALL: 2, - }, - KVStoreType: { - DEVICE_COLLABORATION: 0, - SINGLE_VERSION: 1, - MULTI_VERSION: 2, - }, - SecurityLevel: { - NO_LEVEL: 0, - S0: 1, - S1: 2, - S2: 3, - S3: 5, - S4: 6, - }, -} \ No newline at end of file + createKVManager: distributedDataSo.createKVManager, + Query: distributedDataSo.Query, + FieldNode: distributedDataSo.FieldNode, + Schema: distributedDataSo.Schema, + UserType: { + SAME_USER_ID: 0, + }, + Constants: { + MAX_KEY_LENGTH: 1024, + MAX_VALUE_LENGTH: 4194303, + MAX_KEY_LENGTH_DEVICE: 896, + MAX_STORE_ID_LENGTH: 128, + MAX_QUERY_LENGTH: 512000, + MAX_BATCH_SIZE: 128, + }, + ValueType: { + STRING: 0, + INTEGER: 1, + FLOAT: 2, + BYTE_ARRAY: 3, + BOOLEAN: 4, + DOUBLE: 5, + }, + SyncMode: { + PULL_ONLY: 0, + PUSH_ONLY: 1, + PUSH_PULL: 2, + }, + SubscribeType: { + SUBSCRIBE_TYPE_LOCAL: 0, + SUBSCRIBE_TYPE_REMOTE: 1, + SUBSCRIBE_TYPE_ALL: 2, + }, + KVStoreType: { + DEVICE_COLLABORATION: 0, + SINGLE_VERSION: 1, + MULTI_VERSION: 2, + }, + SecurityLevel: { + NO_LEVEL: 0, + S0: 1, + S1: 2, + S2: 3, + S3: 5, + S4: 6, + }, +}; \ No newline at end of file diff --git a/kv_store/kvstoremock/frameworks/innerkitsimpl/kvdb/src/store_factory.cpp b/kv_store/kvstoremock/frameworks/innerkitsimpl/kvdb/src/store_factory.cpp index b990d907..306e32a0 100644 --- a/kv_store/kvstoremock/frameworks/innerkitsimpl/kvdb/src/store_factory.cpp +++ b/kv_store/kvstoremock/frameworks/innerkitsimpl/kvdb/src/store_factory.cpp @@ -65,7 +65,7 @@ std::shared_ptr StoreFactory::GetOrOpenStore(const AppId &appId, status = StoreUtil::ConvertStatus(dbStatus); if (kvStore == nullptr) { ZLOGE("failed! status:%{public}d appId:%{public}s storeId:%{public}s path:%{public}s", dbStatus, - appId.appId.c_str(), storeId.storeId.c_str(), options.baseDir.c_str()); + appId.appId.c_str(), StoreUtil::Anonymous(storeId.storeId).c_str(), options.baseDir.c_str()); return !stores.empty(); } isCreate = true; diff --git a/kv_store/kvstoremock/frameworks/innerkitsimpl/kvdb/src/store_manager.cpp b/kv_store/kvstoremock/frameworks/innerkitsimpl/kvdb/src/store_manager.cpp index 375fa5a0..5923d36f 100644 --- a/kv_store/kvstoremock/frameworks/innerkitsimpl/kvdb/src/store_manager.cpp +++ b/kv_store/kvstoremock/frameworks/innerkitsimpl/kvdb/src/store_manager.cpp @@ -17,6 +17,7 @@ #include "log_print.h" #include "security_manager.h" #include "store_factory.h" +#include "store_util.h" namespace OHOS::DistributedKv { StoreManager &StoreManager::GetInstance() { @@ -28,7 +29,7 @@ std::shared_ptr StoreManager::GetKVStore(const AppId &appId, cons const Options &options, Status &status) { ZLOGD("appId:%{public}s, storeId:%{public}s type:%{public}d area:%{public}d dir:%{public}s", appId.appId.c_str(), - storeId.storeId.c_str(), options.kvStoreType, options.area, options.baseDir.c_str()); + StoreUtil::Anonymous(storeId.storeId).c_str(), options.kvStoreType, options.area, options.baseDir.c_str()); status = ILLEGAL_STATE; if (!appId.IsValid() || !storeId.IsValid() || !options.IsValidType()) { status = INVALID_ARGUMENT; @@ -47,7 +48,7 @@ std::shared_ptr StoreManager::GetKVStore(const AppId &appId, cons Status StoreManager::CloseKVStore(const AppId &appId, const StoreId &storeId) { - ZLOGD("appId:%{public}s, storeId:%{public}s", appId.appId.c_str(), storeId.storeId.c_str()); + ZLOGD("appId:%{public}s, storeId:%{public}s", appId.appId.c_str(), StoreUtil::Anonymous(storeId.storeId).c_str()); if (!appId.IsValid() || !storeId.IsValid()) { return INVALID_ARGUMENT; } @@ -78,8 +79,8 @@ Status StoreManager::CloseAllKVStore(const AppId &appId) Status StoreManager::Delete(const AppId &appId, const StoreId &storeId, const std::string &path) { - ZLOGD("appId:%{public}s, storeId:%{public}s dir:%{public}s", appId.appId.c_str(), storeId.storeId.c_str(), - path.c_str()); + ZLOGD("appId:%{public}s, storeId:%{public}s dir:%{public}s", appId.appId.c_str(), + StoreUtil::Anonymous(storeId.storeId).c_str(), path.c_str()); if (!appId.IsValid() || !storeId.IsValid()) { return INVALID_ARGUMENT; } diff --git a/kv_store/kvstoremock/frameworks/jskitsimpl/distributeddata/src/js_util.cpp b/kv_store/kvstoremock/frameworks/jskitsimpl/distributeddata/src/js_util.cpp index a4521a06..b2809a7c 100644 --- a/kv_store/kvstoremock/frameworks/jskitsimpl/distributeddata/src/js_util.cpp +++ b/kv_store/kvstoremock/frameworks/jskitsimpl/distributeddata/src/js_util.cpp @@ -1095,7 +1095,6 @@ napi_status JSUtil::GetValue(napi_env env, napi_value in, DataQuery &query) PredicatesProxy *predicates = nullptr; napi_unwrap(env, in, reinterpret_cast(&predicates)); CHECK_RETURN((predicates != nullptr), "invalid type", napi_invalid_arg); - std::vector keys; Status status = KvUtils::ToQuery(*(predicates->predicates_), query); if (status != Status::SUCCESS) { ZLOGD("napi_value -> GetValue DataQuery failed "); @@ -1180,4 +1179,27 @@ bool JSUtil::IsNull(napi_env env, napi_value value) } return false; } + +std::pair JSUtil::GetInnerValue( + napi_env env, napi_value in, const std::string& prop, bool optional) +{ + bool hasProp = false; + napi_status status = napi_has_named_property(env, in, prop.c_str(), &hasProp); + if (status != napi_ok) { + return std::make_pair(napi_generic_failure, nullptr); + } + if (!hasProp) { + status = optional ? napi_ok : napi_generic_failure; + return std::make_pair(status, nullptr); + } + napi_value inner = nullptr; + status = napi_get_named_property(env, in, prop.c_str(), &inner); + if (status != napi_ok || inner == nullptr) { + return std::make_pair(napi_generic_failure, nullptr); + } + if (optional && JSUtil::IsNull(env, inner)) { + return std::make_pair(napi_ok, nullptr); + } + return std::make_pair(napi_ok, inner); +} } // namespace OHOS::DistributedData diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/access_token.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/access_token.h index a74d79a9..b7ae3433 100644 --- a/mock/innerkits/access_token/libaccesstoken_sdk/include/access_token.h +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/access_token.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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,42 +13,83 @@ * limitations under the License. */ -#ifndef ACCESS_TOKEN_DEF_H -#define ACCESS_TOKEN_DEF_H +/** + * @addtogroup AccessToken + * @{ + * + * @brief Provides permission management interfaces. + * + * Provides tokenID-based application permission verification mechanism. + * When an application accesses sensitive data or APIs, this module can check + * whether the application has the corresponding permission. Allows applications + * to query their access token information or APL levcels based on token IDs. + * + * @since 7.0 + * @version 7.0 + */ + +/** + * @file access_token.h + * + * @brief Declares typedefs, enums and const values. + * + * @since 7.0 + * @version 7.0 + */ + +#ifndef ACCESS_TOKEN_H +#define ACCESS_TOKEN_H + +#include namespace OHOS { namespace Security { namespace AccessToken { typedef unsigned int AccessTokenID; -typedef unsigned int AccessTokenAttr; typedef uint64_t FullTokenID; +typedef unsigned int AccessTokenAttr; static const int DEFAULT_TOKEN_VERSION = 1; -static const int FIRSTCALLER_TOKENID_DEFAULT = 0; -static const int MAX_PERMISSION_SIZE = 1000; -static const int MAX_NATIVE_TOKEN_INFO_SIZE = 20480; -static const int INVALID_TOKENID = 0; -static const int INVALID_DLP_TOKEN_FLAG = -1; +static const AccessTokenID INVALID_TOKENID = 0; +/** + * @brief Access token kit return code + */ enum AccessTokenKitRet { RET_FAILED = -1, RET_SUCCESS = 0, }; +/** + * @brief AccessTokenID 32 bits map + */ typedef struct { unsigned int tokenUniqueID : 20; + /** reserved, default 000000 */ unsigned int res : 6; unsigned int dlpFlag : 1; + /** + * token type, for details about the valid values, + * see the definition of ATokenTypeEnum in the access_token.h file. + */ unsigned int type : 2; + /** version, default 001 */ unsigned int version : 3; } AccessTokenIDInner; +/** + * @brief Token id type + */ typedef enum TypeATokenTypeEnum { TOKEN_INVALID = -1, TOKEN_HAP = 0, TOKEN_NATIVE, TOKEN_SHELL, + TOKEN_TYPE_BUTT, } ATokenTypeEnum; +/** + * @brief Apl level + */ typedef enum TypeATokenAplEnum { APL_INVALID = 0, APL_NORMAL = 1, @@ -56,45 +97,103 @@ typedef enum TypeATokenAplEnum { APL_SYSTEM_CORE = 3, } ATokenAplEnum; +/** + * @brief Token id full definition + */ typedef union { unsigned long long tokenIDEx; struct { AccessTokenID tokenID; + /** tokenID attribute */ AccessTokenAttr tokenAttr; } tokenIdExStruct; } AccessTokenIDEx; +/** + * @brief Permission states + */ typedef enum TypePermissionState { PERMISSION_DENIED = -1, PERMISSION_GRANTED = 0, } PermissionState; +/** + * @brief Permission grant mode + */ typedef enum TypeGrantMode { + /** user grant the permisson by dynamic pop-up window */ USER_GRANT = 0, + /** + * system grant the permission automated when + * the permission is decleared and app is installed + */ SYSTEM_GRANT = 1, } GrantMode; +/** + * @brief Permission flag + */ typedef enum TypePermissionFlag { + /** + * permission has not been set by user. + */ PERMISSION_DEFAULT_FLAG = 0, + /** + * permission has been set by user, If the permission is not granted, + * a permission window is allowed to apply for permission. + */ PERMISSION_USER_SET = 1 << 0, + /** + * permission has been set by user, If the permission is not granted, + * a permission window is not allowed to apply for permission. + */ PERMISSION_USER_FIXED = 1 << 1, + /** + * permission has been set by system, + * the permission can be a user_grant one which is granted for pre-authorization and is non-cancellable. + */ PERMISSION_SYSTEM_FIXED = 1 << 2, + /** + * a user_grant permission has been set by system for pre-authorization, + * and it is cancellable. it always works with other flags. + */ PERMISSION_GRANTED_BY_POLICY = 1 << 3, + /** + * permission has been set by security component. + */ + PERMISSION_COMPONENT_SET = 1 << 4, + /* + * permission is fixed by policy and the permission cannot be granted or revoked by user + */ + PERMISSION_POLICY_FIXED = 1 << 5, } PermissionFlag; +/** + * @brief Permission operate result + */ typedef enum TypePermissionOper { + /** permission has been set, only can change it in settings */ SETTING_OPER = -1, + /** operate is passed, no need to do anything */ PASS_OPER = 0, + /** permission need dynamic pop-up windows to grant it */ DYNAMIC_OPER = 1, + /** invalid operation, something is wrong, see in md files */ INVALID_OPER = 2, } PermissionOper; +/** + * @brief Dlp types + */ typedef enum DlpType { DLP_COMMON = 0, DLP_READ = 1, DLP_FULL_CONTROL = 2, } HapDlpType; +/** + * @brief Dlp permission type + */ typedef enum TypeDlpPerm { DLP_PERM_ALL = 0, DLP_PERM_FULL_CONTROL = 1, @@ -103,4 +202,4 @@ typedef enum TypeDlpPerm { } // namespace AccessToken } // namespace Security } // namespace OHOS -#endif // ACCESS_TOKEN_DEF_H +#endif // ACCESS_TOKEN_H diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/access_token_error.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/access_token_error.h new file mode 100644 index 00000000..b7c9337d --- /dev/null +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/access_token_error.h @@ -0,0 +1,71 @@ +/* + * 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. + */ + +/** + * @addtogroup AccessToken + * @{ + * + * @brief Provides permission management interfaces. + * + * Provides tokenID-based application permission verification mechanism. + * When an application accesses sensitive data or APIs, this module can check + * whether the application has the corresponding permission. Allows applications + * to query their access token information or APL levcels based on token IDs. + * + * @since 7.0 + * @version 7.0 + */ + +/** + * @file access_token_error.h + * + * @brief Declares error numbers. + * + * @since 7.0 + * @version 7.0 + */ + +#ifndef ACCESS_TOKEN_ERROR_H +#define ACCESS_TOKEN_ERROR_H + +namespace OHOS { +namespace Security { +namespace AccessToken { +/** + * @brief error numbers + */ +enum AccessTokenError { + ERR_PERMISSION_DENIED = 201, + ERR_NOT_SYSTEM_APP = 202, + ERR_PARAM_INVALID = 12100001, + ERR_TOKENID_NOT_EXIST, + ERR_PERMISSION_NOT_EXIST, + ERR_INTERFACE_NOT_USED_TOGETHER, + ERR_EXCEEDED_MAXNUM_REGISTRATION_LIMIT, + ERR_PERMISSION_OPERATE_FAILED, + ERR_SA_WORK_ABNORMAL, + ERR_MALLOC_FAILED, + ERR_OVERSIZE, + ERR_READ_PARCEL_FAILED, + ERR_WRITE_PARCEL_FAILED, + ERR_DEVICE_NOT_EXIST, + ERR_PROCESS_NOT_EXIST, + ERR_CHECK_DCAP_FAIL, +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // ACCESS_TOKEN_ERROR_H \ No newline at end of file diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/accesstoken_kit.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/accesstoken_kit.h index 026f4199..973bd58a 100644 --- a/mock/innerkits/access_token/libaccesstoken_sdk/include/accesstoken_kit.h +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/accesstoken_kit.h @@ -1,41 +1,41 @@ /* -* Copyright (c) 2021-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. -*/ + * Copyright (c) 2021-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. + */ /** -* @addtogroup AccessToken -* @{ -* -* @brief Provides permission management. -* -* Provides tokenID-based application permission verification mechanism. -* When an application accesses sensitive data or APIs, this module can check -* whether the application has the corresponding permission. Allows applications -* to query their access token information or APL levcels based on token IDs. -* -* @since 7.0 -* @version 7.0 -*/ + * @addtogroup AccessToken + * @{ + * + * @brief Provides permission management. + * + * Provides tokenID-based application permission verification mechanism. + * When an application accesses sensitive data or APIs, this module can check + * whether the application has the corresponding permission. Allows applications + * to query their access token information or APL levcels based on token IDs. + * + * @since 7.0 + * @version 7.0 + */ /** -* @file accesstoken_kit.h -* -* @brief Declares access token interfaces. -* -* @since 7.0 -* @version 7.0 -*/ + * @file accesstoken_kit.h + * + * @brief Declares access token interfaces. + * + * @since 7.0 + * @version 7.0 + */ #ifndef INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H #define INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H @@ -57,272 +57,272 @@ namespace OHOS { namespace Security { namespace AccessToken { /** -* @brief Declares AccessTokenKit class -*/ + * @brief Declares AccessTokenKit class + */ class AccessTokenKit { public: - /** - * @brief Create a unique hap token by input values. - * @param info struct HapInfoParams quote, see hap_token_info.h - * @param policy struct HapPolicyParams quote, see hap_token_info.h - * @return union AccessTokenIDEx, see access_token.h - */ - static AccessTokenIDEx AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy); - /** - * @brief Create a unique mapping token binding remote tokenID and DeviceID. - * @param remoteDeviceID remote device deviceID - * @param remoteTokenID remote device tokenID - * @return local tokenID which mapped by local token - */ - static AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID); - /** - * @brief Update hap token info. - * @param tokenIdEx union AccessTokenIDEx quote, see access_token.h - * @param isSystemApp is system app or not - * @param appIDDesc app id description quote - * @param apiVersion app api version - * @param policy struct HapPolicyParams quote, see hap_token_info.h - * @return error code, see access_token_error.h - */ - static int UpdateHapToken(AccessTokenIDEx& tokenIdEx, - bool isSystemApp, const std::string& appIDDesc, int32_t apiVersion, const HapPolicyParams& policy); - /** - * @brief Delete token info. - * @param tokenID token id - * @return error code, see access_token_error.h - */ - static int DeleteToken(AccessTokenID tokenID); - /** - * @brief Get token type by ATM service. - * @param tokenID token id - * @return token type enum, see access_token.h - */ - static ATokenTypeEnum GetTokenType(AccessTokenID tokenID); - /** - * @brief Get token type from flag in tokenId, which doesn't depend on ATM service. - * @param tokenID token id - * @return token type enum, see access_token.h - */ - static ATokenTypeEnum GetTokenTypeFlag(AccessTokenID tokenID); - /** - * @brief Get token type by ATM service with uint_64 parameters. - * @param tokenID token id - * @return token type enum, see access_token.h - */ - static ATokenTypeEnum GetTokenType(FullTokenID tokenID); - /** - * @brief Get token type from flag in tokenId, which doesn't depend - * on ATM service, with uint_64 parameters. - * @param tokenID token id - * @return token type enum, see access_token.h - */ - static ATokenTypeEnum GetTokenTypeFlag(FullTokenID tokenID); - /** - * @brief Check native token dcap by token id. - * @param tokenID token id - * @param dcap dcap to be checked - * @return error code, see access_token_error.h - */ - static int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap); - /** - * @brief Query hap tokenID by input prarms. - * @param userID user id - * @param bundleName bundle name - * @param instIndex inst index - * @return token id if exsit or 0 if not exsit - */ - static AccessTokenID GetHapTokenID(int32_t userID, const std::string& bundleName, int32_t instIndex); - /** - * @brief Query hap token attribute by input prarms. - * @param userID user id - * @param bundleName bundle name - * @param instIndex inst index - * @return union AccessTokenIDEx, see access_token.h - */ - static AccessTokenIDEx GetHapTokenIDEx(int32_t userID, const std::string& bundleName, int32_t instIndex); - /** - * @brief Get hap token info by token id. - * @param tokenID token id - * @param hapTokenInfoRes HapTokenInfo quote, as query result - * @return error code, see access_token_error.h - */ - static int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes); - /** - * @brief Get native token info by token id. - * @param tokenID token id - * @param nativeTokenInfoRes NativeTokenInfo quote, as query result - * @return error code, see access_token_error.h - */ - static int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes); - /** - * @brief Check if the input tokenID has been granted the input permission. - * @param tokenID token id - * @param permissionName permission to be checked - * @return enum PermissionState, see access_token.h - */ - static int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName); - /** - * @brief Check if the input tokenID has been granted the input permission. - * @param callerTokenID the tokenID of caller native process or hap process - * @param firstTokenID the tokenID of first caller native process or hap process - * @param permissionName permission to be checked - * @return enum PermissionState, see access_token.h - */ - static int VerifyAccessToken( - AccessTokenID callerTokenID, AccessTokenID firstTokenID, const std::string& permissionName); - /** - * @brief Get permission definition by permission name. - * @param permissionName permission name quote - * @param permissionDefResult PermissionDef quote, as query result - * @return error code, see access_token_error.h - */ - static int GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult); - /** - * @brief Get all permission definitions by token id. - * @param tokenID token id - * @param permList PermissionDef list quote, as query result - * @return error code, see access_token_error.h - */ - static int GetDefPermissions(AccessTokenID tokenID, std::vector& permList); - /** - * @brief Get all requested permission full state by token id and grant mode. - * @param tokenID token id - * @param reqPermList PermissionStateFull list quote, as query result - * @param isSystemGrant grant mode - * @return error code, see access_token_error.h - */ - static int GetReqPermissions( - AccessTokenID tokenID, std::vector& reqPermList, bool isSystemGrant); - /** - * @brief Get permission grant flag - * @param tokenID token id - * @param permissionName permission name quote - * @param flag the permission grant flag, as query result - * @return error code, see access_token_error.h - */ - static int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName, int& flag); - /** - * @brief Get requsted permission grant result. - * @param permList PermissionListState list quote, as input and query result - * @return enum PermissionOper, see access_token.h - */ - static PermissionOper GetSelfPermissionsState(std::vector& permList); - /** - * @brief Grant input permission to input tokenID with input flag. - * @param tokenID token id - * @param permissionName permission name quote - * @param flag the permission grant flag - * @return error code, see access_token_error.h - */ - static int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag); - /** - * @brief Revoke input permission to input tokenID with input flag. - * @param tokenID token id - * @param permissionName permission name quote - * @param flag the permission grant flag - * @return error code, see access_token_error.h - */ - static int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag); - /** - * @brief Clear all user granted permissions state in input tokenID. - * @param tokenID token id - * @return error code, see access_token_error.h - */ - static int ClearUserGrantedPermissionState(AccessTokenID tokenID); - /** - * @brief Register permission state change callback. - * @param callback smart point of class PermStateChangeCallbackCustomize quote - * @return error code, see access_token_error.h - */ - static int32_t RegisterPermStateChangeCallback( - const std::shared_ptr& callback); - /** - * @brief Unregister permission state change callback. - * @param callback smart point of class PermStateChangeCallbackCustomize quote - * @return error code, see access_token_error.h - */ - static int32_t UnRegisterPermStateChangeCallback(const std::shared_ptr& callback); - /** - * @brief Get current version. - * @return error code, see access_token_error.h - */ - static int32_t GetVersion(void); - /** - * @brief Get hap dlp flag by input tokenID. - * @param tokenID token id - * @return dlp flag in tokenID bitmap, or default -1 - */ - static int32_t GetHapDlpFlag(AccessTokenID tokenID); - /** - * @brief Reload native token info. - * @return error code, see access_token_error.h - */ - static int32_t ReloadNativeTokenInfo(); - /** - * @brief Get tokenID by native process name. - * @param processName native process name - * @return token id of native process - */ - static AccessTokenID GetNativeTokenId(const std::string& processName); + /** + * @brief Create a unique hap token by input values. + * @param info struct HapInfoParams quote, see hap_token_info.h + * @param policy struct HapPolicyParams quote, see hap_token_info.h + * @return union AccessTokenIDEx, see access_token.h + */ + static AccessTokenIDEx AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy); + /** + * @brief Create a unique mapping token binding remote tokenID and DeviceID. + * @param remoteDeviceID remote device deviceID + * @param remoteTokenID remote device tokenID + * @return local tokenID which mapped by local token + */ + static AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID); + /** + * @brief Update hap token info. + * @param tokenIdEx union AccessTokenIDEx quote, see access_token.h + * @param isSystemApp is system app or not + * @param appIDDesc app id description quote + * @param apiVersion app api version + * @param policy struct HapPolicyParams quote, see hap_token_info.h + * @return error code, see access_token_error.h + */ + static int UpdateHapToken(AccessTokenIDEx& tokenIdEx, + bool isSystemApp, const std::string& appIDDesc, int32_t apiVersion, const HapPolicyParams& policy); + /** + * @brief Delete token info. + * @param tokenID token id + * @return error code, see access_token_error.h + */ + static int DeleteToken(AccessTokenID tokenID); + /** + * @brief Get token type by ATM service. + * @param tokenID token id + * @return token type enum, see access_token.h + */ + static ATokenTypeEnum GetTokenType(AccessTokenID tokenID); + /** + * @brief Get token type from flag in tokenId, which doesn't depend on ATM service. + * @param tokenID token id + * @return token type enum, see access_token.h + */ + static ATokenTypeEnum GetTokenTypeFlag(AccessTokenID tokenID); + /** + * @brief Get token type by ATM service with uint_64 parameters. + * @param tokenID token id + * @return token type enum, see access_token.h + */ + static ATokenTypeEnum GetTokenType(FullTokenID tokenID); + /** + * @brief Get token type from flag in tokenId, which doesn't depend + * on ATM service, with uint_64 parameters. + * @param tokenID token id + * @return token type enum, see access_token.h + */ + static ATokenTypeEnum GetTokenTypeFlag(FullTokenID tokenID); + /** + * @brief Check native token dcap by token id. + * @param tokenID token id + * @param dcap dcap to be checked + * @return error code, see access_token_error.h + */ + static int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap); + /** + * @brief Query hap tokenID by input prarms. + * @param userID user id + * @param bundleName bundle name + * @param instIndex inst index + * @return token id if exsit or 0 if not exsit + */ + static AccessTokenID GetHapTokenID(int32_t userID, const std::string& bundleName, int32_t instIndex); + /** + * @brief Query hap token attribute by input prarms. + * @param userID user id + * @param bundleName bundle name + * @param instIndex inst index + * @return union AccessTokenIDEx, see access_token.h + */ + static AccessTokenIDEx GetHapTokenIDEx(int32_t userID, const std::string& bundleName, int32_t instIndex); + /** + * @brief Get hap token info by token id. + * @param tokenID token id + * @param hapTokenInfoRes HapTokenInfo quote, as query result + * @return error code, see access_token_error.h + */ + static int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes); + /** + * @brief Get native token info by token id. + * @param tokenID token id + * @param nativeTokenInfoRes NativeTokenInfo quote, as query result + * @return error code, see access_token_error.h + */ + static int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes); + /** + * @brief Check if the input tokenID has been granted the input permission. + * @param tokenID token id + * @param permissionName permission to be checked + * @return enum PermissionState, see access_token.h + */ + static int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName); + /** + * @brief Check if the input tokenID has been granted the input permission. + * @param callerTokenID the tokenID of caller native process or hap process + * @param firstTokenID the tokenID of first caller native process or hap process + * @param permissionName permission to be checked + * @return enum PermissionState, see access_token.h + */ + static int VerifyAccessToken( + AccessTokenID callerTokenID, AccessTokenID firstTokenID, const std::string& permissionName); + /** + * @brief Get permission definition by permission name. + * @param permissionName permission name quote + * @param permissionDefResult PermissionDef quote, as query result + * @return error code, see access_token_error.h + */ + static int GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult); + /** + * @brief Get all permission definitions by token id. + * @param tokenID token id + * @param permList PermissionDef list quote, as query result + * @return error code, see access_token_error.h + */ + static int GetDefPermissions(AccessTokenID tokenID, std::vector& permList); + /** + * @brief Get all requested permission full state by token id and grant mode. + * @param tokenID token id + * @param reqPermList PermissionStateFull list quote, as query result + * @param isSystemGrant grant mode + * @return error code, see access_token_error.h + */ + static int GetReqPermissions( + AccessTokenID tokenID, std::vector& reqPermList, bool isSystemGrant); + /** + * @brief Get permission grant flag + * @param tokenID token id + * @param permissionName permission name quote + * @param flag the permission grant flag, as query result + * @return error code, see access_token_error.h + */ + static int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName, int& flag); + /** + * @brief Get requsted permission grant result. + * @param permList PermissionListState list quote, as input and query result + * @return enum PermissionOper, see access_token.h + */ + static PermissionOper GetSelfPermissionsState(std::vector& permList); + /** + * @brief Grant input permission to input tokenID with input flag. + * @param tokenID token id + * @param permissionName permission name quote + * @param flag the permission grant flag + * @return error code, see access_token_error.h + */ + static int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag); + /** + * @brief Revoke input permission to input tokenID with input flag. + * @param tokenID token id + * @param permissionName permission name quote + * @param flag the permission grant flag + * @return error code, see access_token_error.h + */ + static int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag); + /** + * @brief Clear all user granted permissions state in input tokenID. + * @param tokenID token id + * @return error code, see access_token_error.h + */ + static int ClearUserGrantedPermissionState(AccessTokenID tokenID); + /** + * @brief Register permission state change callback. + * @param callback smart point of class PermStateChangeCallbackCustomize quote + * @return error code, see access_token_error.h + */ + static int32_t RegisterPermStateChangeCallback( + const std::shared_ptr& callback); + /** + * @brief Unregister permission state change callback. + * @param callback smart point of class PermStateChangeCallbackCustomize quote + * @return error code, see access_token_error.h + */ + static int32_t UnRegisterPermStateChangeCallback(const std::shared_ptr& callback); + /** + * @brief Get current version. + * @return error code, see access_token_error.h + */ + static int32_t GetVersion(void); + /** + * @brief Get hap dlp flag by input tokenID. + * @param tokenID token id + * @return dlp flag in tokenID bitmap, or default -1 + */ + static int32_t GetHapDlpFlag(AccessTokenID tokenID); + /** + * @brief Reload native token info. + * @return error code, see access_token_error.h + */ + static int32_t ReloadNativeTokenInfo(); + /** + * @brief Get tokenID by native process name. + * @param processName native process name + * @return token id of native process + */ + static AccessTokenID GetNativeTokenId(const std::string& processName); #ifdef TOKEN_SYNC_ENABLE - /** - * @brief Get remote hap token info by remote token id. - * @param tokenID remote token id - * @param hapSync HapTokenInfoForSync quote, as query result - * @return error code, see access_token_error.h - */ - static int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync); - /** - * @brief Get all native token infos. - * @param nativeTokenInfosRes NativeTokenInfoForSync list quote - * as input and query result - * @return error code, see access_token_error.h - */ - static int GetAllNativeTokenInfo(std::vector& nativeTokenInfosRes); - /** - * @brief Set remote hap token info with remote deviceID. - * @param deviceID remote deviceID - * @param hapSync hap token info to set - * @return error code, see access_token_error.h - */ - static int SetRemoteHapTokenInfo(const std::string& deviceID, const HapTokenInfoForSync& hapSync); - /** - * @brief Set remote native token info list with remote deviceID. - * @param deviceID remote deviceID - * @param nativeTokenInfoList native token info list to set - * @return error code, see access_token_error.h - */ - static int SetRemoteNativeTokenInfo(const std::string& deviceID, - const std::vector& nativeTokenInfoList); - /** - * @brief Delete remote token by remote deviceID and remote tokenID. - * @param deviceID remote deviceID - * @param tokenID remote tokenID - * @return error code, see access_token_error.h - */ - static int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID); - /** - * @brief Get local mapping native tokenID by remote deviceID - * and remote tokenID. - * @param deviceID remote deviceID - * @param tokenID remote tokenID - * @return token id of mapping native tokenID - */ - static AccessTokenID GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID); - /** - * @brief Delete remote tokens by remote deviceID. - * @param deviceID remote deviceID - * @return error code, see access_token_error.h - */ - static int DeleteRemoteDeviceTokens(const std::string& deviceID); + /** + * @brief Get remote hap token info by remote token id. + * @param tokenID remote token id + * @param hapSync HapTokenInfoForSync quote, as query result + * @return error code, see access_token_error.h + */ + static int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync); + /** + * @brief Get all native token infos. + * @param nativeTokenInfosRes NativeTokenInfoForSync list quote + * as input and query result + * @return error code, see access_token_error.h + */ + static int GetAllNativeTokenInfo(std::vector& nativeTokenInfosRes); + /** + * @brief Set remote hap token info with remote deviceID. + * @param deviceID remote deviceID + * @param hapSync hap token info to set + * @return error code, see access_token_error.h + */ + static int SetRemoteHapTokenInfo(const std::string& deviceID, const HapTokenInfoForSync& hapSync); + /** + * @brief Set remote native token info list with remote deviceID. + * @param deviceID remote deviceID + * @param nativeTokenInfoList native token info list to set + * @return error code, see access_token_error.h + */ + static int SetRemoteNativeTokenInfo(const std::string& deviceID, + const std::vector& nativeTokenInfoList); + /** + * @brief Delete remote token by remote deviceID and remote tokenID. + * @param deviceID remote deviceID + * @param tokenID remote tokenID + * @return error code, see access_token_error.h + */ + static int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID); + /** + * @brief Get local mapping native tokenID by remote deviceID + * and remote tokenID. + * @param deviceID remote deviceID + * @param tokenID remote tokenID + * @return token id of mapping native tokenID + */ + static AccessTokenID GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID); + /** + * @brief Delete remote tokens by remote deviceID. + * @param deviceID remote deviceID + * @return error code, see access_token_error.h + */ + static int DeleteRemoteDeviceTokens(const std::string& deviceID); #endif - /** - * @brief Dump all token infos in the cache. - * @param tokenID token id, if tokenID is valid, only dump this token info - * @param dumpInfo all token info - */ - static void DumpTokenInfo(AccessTokenID tokenID, std::string& dumpInfo); + /** + * @brief Dump all token infos in the cache. + * @param tokenID token id, if tokenID is valid, only dump this token info + * @param dumpInfo all token info + */ + static void DumpTokenInfo(AccessTokenID tokenID, std::string& dumpInfo); }; } // namespace AccessToken } // namespace Security diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/hap_token_info.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/hap_token_info.h index a4831884..aff6dda5 100644 --- a/mock/innerkits/access_token/libaccesstoken_sdk/include/hap_token_info.h +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/hap_token_info.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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,6 +13,30 @@ * limitations under the License. */ +/** + * @addtogroup AccessToken + * @{ + * + * @brief Provides permission management interfaces. + * + * Provides tokenID-based application permission verification mechanism. + * When an application accesses sensitive data or APIs, this module can check + * whether the application has the corresponding permission. Allows applications + * to query their access token information or APL levcels based on token IDs. + * + * @since 7.0 + * @version 7.0 + */ + +/** + * @file hap_token_info.h + * + * @brief Declares hap token infos. + * + * @since 7.0 + * @version 7.0 + */ + #ifndef ACCESSTOKEN_HAP_TOKEN_INFO_H #define ACCESSTOKEN_HAP_TOKEN_INFO_H @@ -25,40 +49,79 @@ namespace OHOS { namespace Security { namespace AccessToken { +/** + * @brief Declares hap info params class + */ class HapInfoParams final { public: int userID; std::string bundleName; + /** instcance index */ int instIndex; + /** + * dlp type, for details about the valid values, + * see the definition of HapDlpType in the access_token.h file. + */ int dlpType; std::string appIDDesc; + /** which version of the SDK is used to develop the hap */ + int32_t apiVersion; + /** indicates whether the hap is a system app */ + bool isSystemApp; }; +/** + * @brief Declares hap policy params class + */ class HapPolicyParams final { public: + /** + * apl level, for details about the valid values, + * see the definition of ATokenAplEnum in the access_token.h file. + */ ATokenAplEnum apl; std::string domain; std::vector permList; std::vector permStateList; }; +/** + * @brief Declares hap token info class + */ class HapTokenInfo final { public: + /** + * apl level, for details about the valid values, + * see the definition of ATokenAplEnum in the access_token.h file. + */ ATokenAplEnum apl; char ver; int userID; std::string bundleName; + /** which version of the SDK is used to develop this hap */ + int32_t apiVersion; + /** instcance index */ int instIndex; + /** + * dlp type, for details about the valid values, + * see the definition of HapDlpType in the access_token.h file. + */ int dlpType; std::string appID; std::string deviceID; AccessTokenID tokenID; + /** token attribute */ AccessTokenAttr tokenAttr; }; +/** + * @brief Declares hap token info for distributed synchronize class + */ class HapTokenInfoForSync final { public: + /** hap token info */ HapTokenInfo baseInfo; + /** permission state list */ std::vector permStateList; }; } // namespace AccessToken diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/native_token_info.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/native_token_info.h index 62811db9..109f2a78 100644 --- a/mock/innerkits/access_token/libaccesstoken_sdk/include/native_token_info.h +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/native_token_info.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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,6 +13,30 @@ * limitations under the License. */ +/** + * @addtogroup AccessToken + * @{ + * + * @brief Provides permission management interfaces. + * + * Provides tokenID-based application permission verification mechanism. + * When an application accesses sensitive data or APIs, this module can check + * whether the application has the corresponding permission. Allows applications + * to query their access token information or APL levcels based on token IDs. + * + * @since 7.0 + * @version 7.0 + */ + +/** + * @file native_token_info.h + * + * @brief Declares native token infos. + * + * @since 7.0 + * @version 7.0 + */ + #ifndef ACCESSTOKEN_NATIVE_TOKEN_INFO_H #define ACCESSTOKEN_NATIVE_TOKEN_INFO_H @@ -24,20 +48,36 @@ namespace OHOS { namespace Security { namespace AccessToken { +/** + * @brief Declares native token info class + */ class NativeTokenInfo final { public: + /** + * apl level, for details about the valid values, + * see the definition of ATokenAplEnum in the access_token.h file. + */ ATokenAplEnum apl; unsigned char ver; + /** native process name */ std::string processName; + /** capsbility list */ std::vector dcap; AccessTokenID tokenID; + /** token attribute */ AccessTokenAttr tokenAttr; + /** native process access control permission list */ std::vector nativeAcls; }; +/** + * @brief Declares native token info for distributed synchronize class + */ class NativeTokenInfoForSync final { public: + /** native token info */ NativeTokenInfo baseInfo; + /** permission state list */ std::vector permStateList; }; } // namespace AccessToken diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/perm_state_change_callback_customize.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/perm_state_change_callback_customize.h index 8ef8962c..99546225 100644 --- a/mock/innerkits/access_token/libaccesstoken_sdk/include/perm_state_change_callback_customize.h +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/perm_state_change_callback_customize.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -13,6 +13,30 @@ * limitations under the License. */ +/** + * @addtogroup AccessToken + * @{ + * + * @brief Provides permission management interfaces. + * + * Provides tokenID-based application permission verification mechanism. + * When an application accesses sensitive data or APIs, this module can check + * whether the application has the corresponding permission. Allows applications + * to query their access token information or APL levcels based on token IDs. + * + * @since 7.0 + * @version 7.0 + */ + +/** + * @file perm_state_change_callback_customize.h + * + * @brief Declares permission state change callback class. + * + * @since 8.0 + * @version 8.0 + */ + #ifndef INTERFACES_INNER_KITS_PERM_STATE_CALLBACK_CUSTOMIZE_H #define INTERFACES_INNER_KITS_PERM_STATE_CALLBACK_CUSTOMIZE_H @@ -21,17 +45,39 @@ namespace OHOS { namespace Security { namespace AccessToken { +/** + * @brief Declares permission state change callback customize class + */ class PermStateChangeCallbackCustomize { public: + /** + * @brief Constructor without any param. + */ PermStateChangeCallbackCustomize(); + /** + * @brief Constructor with param. + * @param scopeInfo struct PermStateChangeScope quote + */ explicit PermStateChangeCallbackCustomize(const PermStateChangeScope &scopeInfo); + /** + * @brief Destructor without any param. + */ virtual ~PermStateChangeCallbackCustomize(); + /** + * @brief Pure virtual function for callback. + * @param result PermStateChangeInfo quote + */ virtual void PermStateChangeCallback(PermStateChangeInfo& result) = 0; + /** + * @brief Get private variable scopeInfo_. + * @param scopeInfo struct PermStateChangeScope quote as return value + */ void GetScope(PermStateChangeScope &scopeInfo) const; private: + /** private variable struct PermStateChangeScope */ PermStateChangeScope scopeInfo_; }; } // namespace AccessToken diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_def.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_def.h index 33539a31..a0796401 100644 --- a/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_def.h +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_def.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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,6 +13,30 @@ * limitations under the License. */ +/** + * @addtogroup AccessToken + * @{ + * + * @brief Provides permission management interfaces. + * + * Provides tokenID-based application permission verification mechanism. + * When an application accesses sensitive data or APIs, this module can check + * whether the application has the corresponding permission. Allows applications + * to query their access token information or APL levcels based on token IDs. + * + * @since 7.0 + * @version 7.0 + */ + +/** + * @file permission_def.h + * + * @brief Declares permission definition. + * + * @since 7.0 + * @version 7.0 + */ + #ifndef INTERFACES_INNER_KITS_ACCESSTOKEN_PERMISSION_DEF_H #define INTERFACES_INNER_KITS_ACCESSTOKEN_PERMISSION_DEF_H @@ -23,13 +47,27 @@ namespace OHOS { namespace Security { namespace AccessToken { +/** + * @brief Declares permission definition class + */ class PermissionDef final { public: + /** permission name */ std::string permissionName; + /** bundle name */ std::string bundleName; + /** + * grant mode, for details about the valid values, + * see the definition of GrantMode in the access_token.h file. + */ int grantMode; - TypeATokenAplEnum availableLevel; + /** which SDK version can use this permission to develop app */ + ATokenAplEnum availableLevel; + /** indicats whether this permission can be access control list permission */ bool provisionEnable; + /** + * indicates whether the distributed scene can use this permission or not + */ bool distributedSceneEnable; std::string label; int labelId; @@ -37,6 +75,9 @@ public: int descriptionId; }; +/** + * @brief Declares permission definition data class + */ class PermissionDefData final { public: AccessTokenID tokenId; diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_dlp_mode.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_dlp_mode.h index c635bc9a..358bd022 100644 --- a/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_dlp_mode.h +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_dlp_mode.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -13,6 +13,30 @@ * limitations under the License. */ +/** + * @addtogroup AccessToken + * @{ + * + * @brief Provides permission management interfaces. + * + * Provides tokenID-based application permission verification mechanism. + * When an application accesses sensitive data or APIs, this module can check + * whether the application has the corresponding permission. Allows applications + * to query their access token information or APL levcels based on token IDs. + * + * @since 7.0 + * @version 7.0 + */ + +/** + * @file permission_dlp_mode.h + * + * @brief Declares permission dlp mode class. + * + * @since 8.0 + * @version 8.0 + */ + #ifndef INTERFACES_INNER_KITS_ACCESSTOKEN_PERMISSION_DLP_MODE_H #define INTERFACES_INNER_KITS_ACCESSTOKEN_PERMISSION_DLP_MODE_H @@ -21,9 +45,16 @@ namespace OHOS { namespace Security { namespace AccessToken { +/** + * @brief Declares permission dlp mode class + */ class PermissionDlpMode final { public: std::string permissionName; + /** + * dlp mode, for details about the valid values, + * see the definition of DlpPermMode in the access_token.h file + */ int32_t dlpMode; }; } // namespace AccessToken diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_list_state.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_list_state.h index 0e8787e8..2cbb44c9 100644 --- a/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_list_state.h +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_list_state.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -13,6 +13,30 @@ * limitations under the License. */ +/** + * @addtogroup AccessToken + * @{ + * + * @brief Provides permission management interfaces. + * + * Provides tokenID-based application permission verification mechanism. + * When an application accesses sensitive data or APIs, this module can check + * whether the application has the corresponding permission. Allows applications + * to query their access token information or APL levcels based on token IDs. + * + * @since 7.0 + * @version 7.0 + */ + +/** + * @file permission_list_state.h + * + * @brief Declares permission list state class. + * + * @since 8.0 + * @version 8.0 + */ + #ifndef INTERFACES_INNER_KITS_ACCESSTOKEN_PERMISSION_LIST_STATE_H #define INTERFACES_INNER_KITS_ACCESSTOKEN_PERMISSION_LIST_STATE_H @@ -22,9 +46,16 @@ namespace OHOS { namespace Security { namespace AccessToken { +/** + * @brief Declares permission list state class. + */ class PermissionListState final { public: std::string permissionName; + /** + * permission request state, for details about the valid values, + * see the definition of HapDlpType in the access_token.h file. + */ int state; }; } // namespace AccessToken diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_state_change_info.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_state_change_info.h index 40c4e6a5..0d5752b4 100644 --- a/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_state_change_info.h +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_state_change_info.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -13,6 +13,30 @@ * limitations under the License. */ +/** + * @addtogroup AccessToken + * @{ + * + * @brief Provides permission management interfaces. + * + * Provides tokenID-based application permission verification mechanism. + * When an application accesses sensitive data or APIs, this module can check + * whether the application has the corresponding permission. Allows applications + * to query their access token information or APL levcels based on token IDs. + * + * @since 7.0 + * @version 7.0 + */ + +/** + * @file permission_state_change_info.h + * + * @brief Declares PermStateChangeInfo and PermStateChangeScope struct. + * + * @since 7.0 + * @version 7.0 + */ + #ifndef INTERFACES_INNER_KITS_PERMISSION_STATE_CHANGE_INFO_H #define INTERFACES_INNER_KITS_PERMISSION_STATE_CHANGE_INFO_H @@ -24,17 +48,37 @@ namespace OHOS { namespace Security { namespace AccessToken { +/** define tokenID list max size */ #define TOKENIDS_LIST_SIZE_MAX 1024 +/** define permission list max size */ #define PERMS_LIST_SIZE_MAX 1024 +/** + * @brief Declares permission state change info struct + */ struct PermStateChangeInfo { - int32_t PermStateChangeType; + /** + * permission state change type, for details about the valid values, + * see the definition of ActiveChangeType in the active_change_response_info.h file. + */ + int32_t permStateChangeType; AccessTokenID tokenID; std::string permissionName; }; +/** + * @brief Declares permission state change scope struct + */ struct PermStateChangeScope { + /** + * indicates which tokenID to listen the permission state change, + * empty means listen all tokenIDs in the device + */ std::vector tokenIDs; + /** + * indicates which permission to listen the state change, + * empty means listen all permission state changes in the device + */ std::vector permList; }; } // namespace AccessToken diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_state_full.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_state_full.h index be30cb56..73354f87 100644 --- a/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_state_full.h +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/permission_state_full.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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,6 +13,30 @@ * limitations under the License. */ +/** + * @addtogroup AccessToken + * @{ + * + * @brief Provides permission management interfaces. + * + * Provides tokenID-based application permission verification mechanism. + * When an application accesses sensitive data or APIs, this module can check + * whether the application has the corresponding permission. Allows applications + * to query their access token information or APL levcels based on token IDs. + * + * @since 7.0 + * @version 7.0 + */ + +/** + * @file permission_state_full.h + * + * @brief Declares permission state full class. + * + * @since 7.0 + * @version 7.0 + */ + #ifndef INTERFACES_INNER_KITS_ACCESSTOKEN_PERMISSION_STATE_FULL_H #define INTERFACES_INNER_KITS_ACCESSTOKEN_PERMISSION_STATE_FULL_H @@ -22,12 +46,24 @@ namespace OHOS { namespace Security { namespace AccessToken { +/** + * @brief Declares permission state full class + */ class PermissionStateFull final { public: std::string permissionName; + /** indicate whether permission is in common use or not */ bool isGeneral; std::vector resDeviceID; + /** + * permission grant state list, for details about the valid values, + * see the definition of PermissionState in the access_token.h file. + */ std::vector grantStatus; + /** + * permission grant flag list, for details about the valid values, + * see the definition of PermissionFlag in the access_token.h file. + */ std::vector grantFlags; }; } // namespace AccessToken diff --git a/relational_store/CMakeLists.txt b/relational_store/CMakeLists.txt index 0b5c49a9..e7586106 100644 --- a/relational_store/CMakeLists.txt +++ b/relational_store/CMakeLists.txt @@ -18,20 +18,21 @@ aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/rdb/src relat aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/rdb_data_ability_adapter/src relational_store_src) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/rdb_data_share_adapter/src relational_store_src) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/rdb_device_manager_adapter/src relational_store_src) -aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/common/src relational_store_src) -aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/dataability/src relational_store_src) -aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/relationalstore/src relational_store_src) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/ndk/src relational_store_src) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/common/src js_common_src) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/dataability/src js_ability_src) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/relationalstore/src js_relational_src) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/rdb/src js_rdb_src) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/cloud_data/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/rdb/include) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/common/include) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/dataability/include) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/relationalstore/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/inner_api/appdatafwk/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/inner_api/cloud_data/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/inner_api/dataability/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/inner_api/rdb/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/inner_api/rdb_data_ability_adapter/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/inner_api/rdb_data_share_adapter/include) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/ndk/include) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/ndk/src) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/rdb_data_share_adapter/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/rdb_device_manager_adapter/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../datamgr_service/services/distributeddataservice/adapter/include/dfx) @@ -46,4 +47,20 @@ include(${KV_STORE_DIR}/interfaces/CMakeLists.txt OPTIONAL) set(links secure mock adapter distributeddb kvdb) add_library(relational_store SHARED ${relational_store_src}) -target_link_libraries(relational_store ${links}) \ No newline at end of file +add_library(js_rdb SHARED ${js_common_src} ${js_rdb_src}) +add_library(js_relational SHARED ${js_common_src} ${js_relational_src}) +add_library(js_ability SHARED ${js_common_src} ${js_ability_src}) +target_link_libraries(relational_store ${links}) +target_link_libraries(js_rdb ${links} relational_store) +target_link_libraries(js_relational ${links} relational_store) +target_link_libraries(js_ability ${links} relational_store js_rdb) +target_include_directories(js_rdb PRIVATE + "${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/common/include" + "${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/rdb/include") +target_include_directories(js_relational PRIVATE + "${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/common/include" + "${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/relationalstore/include") +target_include_directories(js_ability PRIVATE + "${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/common/include" + "${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/rdb/include" + "${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/dataability/include") \ No newline at end of file diff --git a/relational_store/bundle.json b/relational_store/bundle.json index 03c709b1..48b3ea4c 100644 --- a/relational_store/bundle.json +++ b/relational_store/bundle.json @@ -73,12 +73,26 @@ "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/rdb:native_rdb", "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/rdb_bms_adapter:rdb_bms_adapter", "//foundation/distributeddatamgr/relational_store/frameworks/js/napi/cloud_data:clouddata", + "//foundation/distributeddatamgr/relational_store/interfaces/ndk:native_rdb_ndk", "//foundation/distributeddatamgr/relational_store/frameworks/js/napi/dataability:dataability", "//foundation/distributeddatamgr/relational_store/frameworks/js/napi/rdb:rdb", "//foundation/distributeddatamgr/relational_store/frameworks/js/napi/rdb:napi_rdb", "//foundation/distributeddatamgr/relational_store/frameworks/js/napi/relationalstore:relationalstore" ], "inner_kits": [ + { + "name": "//foundation/distributeddatamgr/relational_store/interfaces/ndk:native_rdb_ndk", + "header": { + "header_files": [ + "relational_predicates.h", + "relational_store.h", + "relational_cursor.h", + "relational_value_object.h", + "relational_values_bucket.h" + ], + "header_base": "//foundation/distributeddatamgr/relational_store/interfaces/ndk/include" + } + }, { "name": "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/rdb:native_rdb", "header": { @@ -157,6 +171,7 @@ "//foundation/distributeddatamgr/relational_store/test/js/rdb:unittest", "//foundation/distributeddatamgr/relational_store/test/js/relationalstore:performancetest", "//foundation/distributeddatamgr/relational_store/test/js/relationalstore:unittest", + "//foundation/distributeddatamgr/relational_store/test/ndk:unittest", "//foundation/distributeddatamgr/relational_store/test/native/dataability:unittest", "//foundation/distributeddatamgr/relational_store/test/native/rdb:unittest", "//foundation/distributeddatamgr/relational_store/test/native/rdb:fuzztest", diff --git a/relational_store/frameworks/js/napi/common/include/js_utils.h b/relational_store/frameworks/js/napi/common/include/js_utils.h index 3731d184..2679b024 100644 --- a/relational_store/frameworks/js/napi/common/include/js_utils.h +++ b/relational_store/frameworks/js/napi/common/include/js_utils.h @@ -52,43 +52,41 @@ constexpr int32_t SYNC_RESULT_ELEMNT_NUM = 2; napi_value GetNamedProperty(napi_env env, napi_value object, const char *name); -int32_t Convert2Value(napi_env env, napi_value jsValue, uint32_t &output); -int32_t Convert2Value(napi_env env, napi_value jsValue, int32_t &output); +int32_t Convert2ValueExt(napi_env env, napi_value jsValue, uint32_t &output); +int32_t Convert2ValueExt(napi_env env, napi_value jsValue, int32_t &output); +int32_t Convert2ValueExt(napi_env env, napi_value jsValue, int64_t &output); + int32_t Convert2Value(napi_env env, napi_value jsValue, bool &output); -int32_t Convert2Value(napi_env env, napi_value jsValue, int64_t &output); int32_t Convert2Value(napi_env env, napi_value jsValue, double &output); +int32_t Convert2Value(napi_env env, napi_value jsValue, int64_t &output); int32_t Convert2Value(napi_env env, napi_value jsValue, std::string &output); int32_t Convert2Value(napi_env env, napi_value jsValue, std::vector &output); int32_t Convert2Value(napi_env env, napi_value jsValue, std::monostate &value); +int32_t Convert2Value(napi_env env, napi_value jsValue, std::map &output); +int32_t Convert2Value(napi_env env, napi_value jsValue, std::map &output); template int32_t Convert2Value(napi_env env, napi_value jsValue, T &output); + template int32_t Convert2Value(napi_env env, napi_value jsValue, std::vector &value); -template -int32_t Convert2Value(napi_env env, napi_value jsValue, std::variant<_Types...> &value); -template -napi_value Convert2JSValue(napi_env env, const std::variant<_Types...> &value); +template +int32_t Convert2Value(napi_env env, napi_value jsValue, std::variant &value); -int32_t Convert2JSValue(napi_env env, std::string value, napi_value &output); -int32_t Convert2JSValue(napi_env env, bool value, napi_value &output); -int32_t Convert2JSValue(napi_env env, double value, napi_value &output); using Descriptor = std::function()>; - /* napi_define_class wrapper */ napi_value DefineClass(napi_env env, const std::string &name, const Descriptor &descriptor, napi_callback ctor); napi_value GetClass(napi_env env, const std::string &spaceName, const std::string &className); std::string Convert2String(napi_env env, napi_value jsStr, bool useDefaultBufSize = true); -int32_t Convert2Value(napi_env env, napi_value jsValue, std::map &output); -int32_t Convert2Value(napi_env env, napi_value jsValue, std::map &output); -int32_t Convert2Value(napi_env env, napi_value jsBool, bool &output); -int32_t Convert2Value(napi_env env, napi_value jsNum, int32_t &output); -int32_t Convert2Value(napi_env env, napi_value jsStr, std::string &output); std::vector Convert2StrVector(napi_env env, napi_value value); std::vector Convert2U8Vector(napi_env env, napi_value jsValue); std::string ConvertAny2String(napi_env env, napi_value jsValue); +int32_t Convert2JSValue(napi_env env, std::string value, napi_value &output); +int32_t Convert2JSValue(napi_env env, bool value, napi_value &output); +int32_t Convert2JSValue(napi_env env, double value, napi_value &output); + napi_value Convert2JSValue(napi_env env, const std::vector &value); napi_value Convert2JSValue(napi_env env, const std::string &value); napi_value Convert2JSValue(napi_env env, const std::vector &value); @@ -102,71 +100,80 @@ napi_value Convert2JSValue(napi_env env, const std::monostate &value); template napi_value Convert2JSValue(napi_env env, const T &value); + template napi_value Convert2JSValue(napi_env env, const std::vector &value); -template -napi_value Convert2JSValue(napi_env env, const std::variant<_Types...> &value); +template +napi_value Convert2JSValue(napi_env env, const std::variant &value); -template -int32_t GetCPPValue(napi_env env, napi_value jsValue, _T &value) +template +int32_t GetCPPValue(napi_env env, napi_value jsValue, T &value) { return napi_invalid_arg; } -template -int32_t GetCPPValue(napi_env env, napi_value jsValue, _T &value) +template +int32_t GetCPPValue(napi_env env, napi_value jsValue, T &value) { - _First cValue; + First cValue; auto ret = Convert2Value(env, jsValue, cValue); - if (ret != napi_invalid_arg) { - if (ret == napi_ok) { - value = cValue; - } + if (ret == napi_ok) { + value = cValue; return ret; } - return GetCPPValue<_T, _Types...>(env, jsValue, value); + return GetCPPValue(env, jsValue, value); } -template -napi_value GetJSValue(napi_env env, const _T &value) +template +napi_value GetJSValue(napi_env env, const T &value) { return nullptr; } -template -napi_value GetJSValue(napi_env env, const _T &value) +template +napi_value GetJSValue(napi_env env, const T &value) { - auto *val = std::get_if<_First>(&value); - if (val == nullptr) { + auto *val = std::get_if(&value); + if (val != nullptr) { return Convert2JSValue(env, *val); } - return GetJSValue<_T, _Types...>(env, value); + return GetJSValue(env, value); } } // namespace JSUtils template int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::vector &value) { + bool isArray = false; + napi_is_array(env, jsValue, &isArray); + if (!isArray) { + return napi_invalid_arg; + } + uint32_t arrLen = 0; napi_get_array_length(env, jsValue, &arrLen); if (arrLen == 0) { return napi_ok; } + for (size_t i = 0; i < arrLen; ++i) { napi_value element; napi_get_element(env, jsValue, i, &element); T item; - Convert2Value(env, element, item); + auto status = Convert2Value(env, element, item); + if (status != napi_ok) { + return napi_invalid_arg; + } value.push_back(std::move(item)); } return napi_ok; } -template -int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::variant<_Types...> &value) +template +int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::variant &value) { - return GetCPPValue(env, jsValue, value); + return GetCPPValue(env, jsValue, value); } template @@ -184,12 +191,11 @@ napi_value JSUtils::Convert2JSValue(napi_env env, const std::vector &value) return jsValue; } -template -napi_value JSUtils::Convert2JSValue(napi_env env, const std::variant<_Types...> &value) +template +napi_value JSUtils::Convert2JSValue(napi_env env, const std::variant &value) { - return GetJSValue(env, value); + return GetJSValue(env, value); } } // namespace AppDataMgrJsKit } // namespace OHOS - #endif // DISTRIBUTEDDATAMGR_APPDATAMGR_JSUTILS_H diff --git a/relational_store/frameworks/js/napi/common/src/js_utils.cpp b/relational_store/frameworks/js/napi/common/src/js_utils.cpp index af69aeda..1d33b1c5 100644 --- a/relational_store/frameworks/js/napi/common/src/js_utils.cpp +++ b/relational_store/frameworks/js/napi/common/src/js_utils.cpp @@ -38,16 +38,15 @@ std::string JSUtils::Convert2String(napi_env env, napi_value jsStr, bool useDefa str_buffer_size = (useDefaultBufSize && (str_buffer_size > DEFAULT_BUF_SIZE)) ? (DEFAULT_BUF_SIZE + BUF_CACHE_MARGIN) : (str_buffer_size + BUF_CACHE_MARGIN); - char *buf = new (std::nothrow) char[str_buffer_size]; + auto buf = std::make_unique(str_buffer_size); if (buf == nullptr) { - LOG_ERROR("JSUtils::Convert2String new failed, buf is nullptr"); + LOG_ERROR("Convert2String new failed, buf is nullptr"); return ""; } size_t len = 0; - napi_get_value_string_utf8(env, jsStr, buf, str_buffer_size, &len); + napi_get_value_string_utf8(env, jsStr, buf.get(), str_buffer_size, &len); buf[len] = 0; - std::string value(buf); - delete[] buf; + std::string value(buf.get()); return value; } @@ -112,76 +111,127 @@ std::string JSUtils::ConvertAny2String(napi_env env, napi_value jsValue) return "invalid type"; } -int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, uint32_t &output) +int32_t JSUtils::Convert2ValueExt(napi_env env, napi_value jsValue, uint32_t &output) { - napi_status status = napi_get_value_uint32(env, jsValue, &output); + napi_valuetype type; + napi_status status = napi_typeof(env, jsValue, &type); + if (status != napi_ok || type != napi_number) { + LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + + status = napi_get_value_uint32(env, jsValue, &output); if (status != napi_ok) { - LOG_ERROR("JSUtils::Convert2Value get jsVal failed, status = %{public}d", status); - return ERR; + LOG_DEBUG("Convert2Value napi_get_value_uint32 failed, status = %{public}d", status); + return status; } - return OK; + return status; } -int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, int32_t &output) +int32_t JSUtils::Convert2ValueExt(napi_env env, napi_value jsValue, int32_t &output) { - napi_status status = napi_get_value_int32(env, jsValue, &output); + napi_valuetype type; + napi_status status = napi_typeof(env, jsValue, &type); + if (status != napi_ok || type != napi_number) { + LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + + status = napi_get_value_int32(env, jsValue, &output); if (status != napi_ok) { - LOG_ERROR("JSUtils::Convert2Value get jsVal failed, status = %{public}d", status); - return ERR; + LOG_DEBUG("Convert2Value napi_get_value_int32 failed, status = %{public}d", status); + return status; } - return OK; + return status; } + int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, bool &output) { + napi_valuetype type; + napi_status status = napi_typeof(env, jsValue, &type); + if (status != napi_ok || type != napi_boolean) { + LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + bool bValue = false; - napi_status status = napi_get_value_bool(env, jsValue, &bValue); + status = napi_get_value_bool(env, jsValue, &bValue); if (status != napi_ok) { - LOG_ERROR("JSUtils::Convert2Value get jsVal failed, status = %{public}d", status); - return ERR; + LOG_ERROR("Convert2Value napi_get_value_bool failed, status = %{public}d", status); + return status; } output = bValue; - return OK; + return status; } -int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, int64_t &output) +int32_t JSUtils::Convert2ValueExt(napi_env env, napi_value jsValue, int64_t &output) { - napi_status status = napi_get_value_int64(env, jsValue, &output); + napi_valuetype type; + napi_status status = napi_typeof(env, jsValue, &type); + if (status != napi_ok || type != napi_number) { + LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + + status = napi_get_value_int64(env, jsValue, &output); if (status != napi_ok) { - LOG_ERROR("JSUtils::Convert2Value get jsVal failed, status = %{public}d", status); - return ERR; + LOG_DEBUG("Convert2Value napi_get_value_int32 failed, status = %{public}d", status); + return status; } - return OK; + return status; } int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, double &output) { + napi_valuetype type; + napi_status status = napi_typeof(env, jsValue, &type); + if (status != napi_ok || type != napi_number) { + LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + double number = 0.0; - napi_status status = napi_get_value_double(env, jsValue, &number); + status = napi_get_value_double(env, jsValue, &number); if (status != napi_ok) { - LOG_ERROR("JSUtils::Convert2Value get jsVal failed, status = %{public}d", status); - return ERR; + LOG_DEBUG("Convert2Value napi_get_value_double failed, status = %{public}d", status); + return status; } output = number; - return OK; + return status; +} + +int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, int64_t &output) +{ + LOG_INFO("Convert2Value js just support double data not support int64_t"); + return napi_invalid_arg; } int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::string &output) { - char *str = new (std::nothrow) char[MAX_VALUE_LENGTH + 1]; + napi_valuetype type; + napi_status status = napi_typeof(env, jsValue, &type); + if (status != napi_ok || type != napi_string) { + LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + + size_t length = MAX_VALUE_LENGTH; + napi_get_value_string_utf8(env, jsValue, nullptr, 0, &length); + length = length < MAX_VALUE_LENGTH ? MAX_VALUE_LENGTH : length; + std::unique_ptr str = std::make_unique(length + 1); if (str == nullptr) { - LOG_ERROR("JSUtils::Convert2Value new failed, str is nullptr"); - return ERR; + LOG_ERROR("Convert2Value new failed, str is nullptr"); + return napi_generic_failure; } + size_t valueSize = 0; - napi_status status = napi_get_value_string_utf8(env, jsValue, str, MAX_VALUE_LENGTH, &valueSize); + status = napi_get_value_string_utf8(env, jsValue, str.get(), length, &valueSize); if (status != napi_ok) { - LOG_ERROR("JSUtils::Convert2Value get jsVal failed, status = %{public}d", status); - delete[] str; - return ERR; + LOG_ERROR("Convert2Value napi_get_value_string_utf8 failed, status = %{public}d", status); + return status; } - output = std::string(str); - delete[] str; - return OK; + output = std::string(str.get()); + return status; } int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::vector &output) @@ -189,7 +239,8 @@ int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::vector((uint8_t *)data, ((uint8_t *)data) + length); - return OK; + return status; } int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::monostate &value) @@ -212,14 +265,15 @@ int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::monostate bool equal = false; napi_strict_equals(env, jsValue, tempValue, &equal); if (equal) { - return OK; + return napi_ok; } napi_get_undefined(env, &tempValue); napi_strict_equals(env, jsValue, tempValue, &equal); if (equal) { return OK; } - return ERR; + LOG_DEBUG("Convert2Value jsValue is not null"); + return napi_invalid_arg; } int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::map &output) @@ -242,7 +296,7 @@ int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::map(key, val)); } return OK; @@ -418,8 +472,8 @@ int32_t JSUtils::Convert2JSValue(napi_env env, double value, napi_value &output) napi_value JSUtils::Convert2JSValue(napi_env env, const std::monostate &value) { - napi_value result; - napi_get_undefined(env, &result); + napi_value result = nullptr; + napi_get_null(env, &result); return result; } diff --git a/relational_store/frameworks/js/napi/dataability/src/napi_predicates_utils.cpp b/relational_store/frameworks/js/napi/dataability/src/napi_predicates_utils.cpp index 21451dab..a4fa6a17 100644 --- a/relational_store/frameworks/js/napi/dataability/src/napi_predicates_utils.cpp +++ b/relational_store/frameworks/js/napi/dataability/src/napi_predicates_utils.cpp @@ -52,7 +52,7 @@ napi_value CreateRdbPredicates(napi_env env, napi_callback_info info) absPredicates->GetIndex(), absPredicates->GetGroup(), absPredicates->GetOrder(), absPredicates->GetLimit(), absPredicates->GetOffset()); - return RelationalStoreJsKit::RdbPredicatesProxy::NewInstance(env, predicates); + return RdbJsKit::RdbPredicatesProxy::NewInstance(env, predicates); } napi_value InitPredicatesUtils(napi_env env, napi_value exports) diff --git a/relational_store/frameworks/js/napi/rdb/include/napi_rdb_js_utils.h b/relational_store/frameworks/js/napi/rdb/include/napi_rdb_js_utils.h new file mode 100644 index 00000000..f7d95d29 --- /dev/null +++ b/relational_store/frameworks/js/napi/rdb/include/napi_rdb_js_utils.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 RDB_JSKIT_NAPI_RDB_JS_UTILS_H +#define RDB_JSKIT_NAPI_RDB_JS_UTILS_H +#include "asset_value.h" +#include "js_utils.h" +#include "result_set.h" +#include "value_object.h" +namespace OHOS::AppDataMgrJsKit { +namespace JSUtils { +using Asset = OHOS::NativeRdb::AssetValue; +using RowEntity = OHOS::NativeRdb::RowEntity; +template<> +int32_t Convert2Value(napi_env env, napi_value input, Asset &output); +template<> +napi_value Convert2JSValue(napi_env env, const Asset &value); +template<> +napi_value Convert2JSValue(napi_env env, const RowEntity &value); +}; // namespace JSUtils +} // namespace OHOS::AppDataMgrJsKit +#endif // RDB_JSKIT_NAPI_RDB_JS_UTILS_H diff --git a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_js_utils.cpp b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_js_utils.cpp new file mode 100644 index 00000000..832cafd4 --- /dev/null +++ b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_js_utils.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 "napi_rdb_js_utils.h" +#include "result_set.h" +#include "js_logger.h" +namespace OHOS::AppDataMgrJsKit { +namespace JSUtils { +using namespace NativeRdb; +template<> +int32_t Convert2Value(napi_env env, napi_value jsValue, Asset &output) +{ + napi_valuetype type; + napi_status status = napi_typeof(env, jsValue, &type); + if (status != napi_ok || type != napi_object) { + LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + + NAPI_CALL_BASE(env, Convert2ValueExt(env, GetNamedProperty(env, jsValue, "version"), output.version), + napi_invalid_arg); + NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, name), napi_invalid_arg); + NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, uri), napi_invalid_arg); + NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, createTime), napi_invalid_arg); + NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, modifyTime), napi_invalid_arg); + NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, size), napi_invalid_arg); + NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, hash), napi_invalid_arg); + return napi_ok; +} + +template<> +napi_value Convert2JSValue(napi_env env, const Asset &value) +{ + napi_value object; + NAPI_CALL_BASE(env, napi_create_object(env, &object), object); + NAPI_CALL_BASE(env, AddJsProperty(env, object, value, version), object); + NAPI_CALL_BASE(env, AddJsProperty(env, object, value, name), object); + NAPI_CALL_BASE(env, AddJsProperty(env, object, value, uri), object); + NAPI_CALL_BASE(env, AddJsProperty(env, object, value, createTime), object); + NAPI_CALL_BASE(env, AddJsProperty(env, object, value, modifyTime), object); + NAPI_CALL_BASE(env, AddJsProperty(env, object, value, size), object); + NAPI_CALL_BASE(env, AddJsProperty(env, object, value, hash), object); + return object; +} + +template<> +napi_value Convert2JSValue(napi_env env, const RowEntity &rowEntity) +{ + napi_value ret; + NAPI_CALL(env, napi_create_object(env, &ret)); + auto &values = rowEntity.Get(); + for (auto const &[key, object] : values) { + napi_value value = JSUtils::Convert2JSValue(env, object.value); + NAPI_CALL(env, napi_set_named_property(env, ret, key.c_str(), value)); + } + return ret; +} +}; // namespace JSUtils +} // namespace OHOS::AppDataMgrJsKit \ No newline at end of file diff --git a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store.cpp b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store.cpp index 72d25944..7afa3d8b 100644 --- a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store.cpp +++ b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store.cpp @@ -72,7 +72,6 @@ struct RdbStoreContext : public BaseContext { std::unique_ptr resultSet; #endif std::shared_ptr newResultSet; - std::unique_ptr resultSet_value; std::string aliasName; std::string pathName; std::string destName; @@ -1029,14 +1028,14 @@ napi_value RdbStoreProxy::QueryByStep(napi_env env, napi_callback_info info) auto exec = [context]() { LOG_DEBUG("RdbStoreProxy::QueryByStep Async"); RdbStoreProxy *obj = reinterpret_cast(context->boundObj); - context->resultSet_value = obj->rdbStore_->QueryByStep(context->sql, context->columns); - LOG_ERROR("RdbStoreProxy::QueryByStep is nullptr ? %{public}d ", context->resultSet_value == nullptr); - return (context->resultSet_value != nullptr) ? OK : ERR; + context->newResultSet = obj->rdbStore_->QueryByStep(context->sql, context->columns); + LOG_ERROR("RdbStoreProxy::QueryByStep is nullptr ? %{public}d ", context->newResultSet == nullptr); + return (context->newResultSet != nullptr) ? OK : ERR; }; auto output = [context](napi_env env, napi_value &result) -> int { - if (context->resultSet_value != nullptr) { + if (context->newResultSet != nullptr) { result = ResultSetProxy::NewInstance( - env, std::shared_ptr(context->resultSet_value.release()), context->apiversion); + env, context->newResultSet, context->apiversion); } LOG_DEBUG("RdbStoreProxy::QueryByStep end"); return (result != nullptr) ? OK : ERR; diff --git a/relational_store/frameworks/js/napi/rdb/src/napi_result_set.cpp b/relational_store/frameworks/js/napi/rdb/src/napi_result_set.cpp index 87dd8a47..c7208061 100644 --- a/relational_store/frameworks/js/napi/rdb/src/napi_result_set.cpp +++ b/relational_store/frameworks/js/napi/rdb/src/napi_result_set.cpp @@ -25,6 +25,7 @@ #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) #include "abs_shared_result_set.h" #include "rdb_result_set_bridge.h" +#include "shared_block.h" #include "string_ex.h" #endif diff --git a/relational_store/frameworks/js/napi/relationalstore/include/napi_result_set.h b/relational_store/frameworks/js/napi/relationalstore/include/napi_result_set.h index f0118af1..98c40e4e 100644 --- a/relational_store/frameworks/js/napi/relationalstore/include/napi_result_set.h +++ b/relational_store/frameworks/js/napi/relationalstore/include/napi_result_set.h @@ -73,13 +73,7 @@ private: static napi_value GetRow(napi_env env, napi_callback_info info); static napi_value IsClosed(napi_env env, napi_callback_info info); - static napi_value GetSharedBlockName(napi_env env, napi_callback_info info); - static napi_value GetSharedBlockAshmemFd(napi_env env, napi_callback_info info); - std::shared_ptr resultSet_; - - std::string sharedBlockName_; - int32_t sharedBlockAshmemFd_ = -1; }; } // namespace RelationalStoreJsKit } // namespace OHOS diff --git a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp index 4268d4a5..dd443b38 100644 --- a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp +++ b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp @@ -15,19 +15,54 @@ #include "napi_rdb_js_utils.h" #include "result_set.h" +#include "js_logger.h" namespace OHOS::AppDataMgrJsKit { namespace JSUtils { using namespace NativeRdb; template<> int32_t Convert2Value(napi_env env, napi_value jsValue, Asset &output) { - NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, version), napi_invalid_arg); - NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, name), napi_invalid_arg); - NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, uri), napi_invalid_arg); - NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, createTime), napi_invalid_arg); - NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, modifyTime), napi_invalid_arg); - NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, size), napi_invalid_arg); - NAPI_CALL_BASE(env, GetProperty(env, jsValue, output, hash), napi_invalid_arg); + napi_valuetype type; + napi_status status = napi_typeof(env, jsValue, &type); + if (status != napi_ok || type != napi_object) { + LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + bool isArray = false; + napi_is_array(env, jsValue, &isArray); + if (isArray) { + LOG_DEBUG("asset failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + + auto ret = Convert2ValueExt(env, GetNamedProperty(env, jsValue, "version"), output.version); + if (ret != napi_ok) { + return napi_invalid_arg; + } + ret = GetProperty(env, jsValue, output, name); + if (ret != napi_ok) { + return napi_invalid_arg; + } + ret = GetProperty(env, jsValue, output, uri); + if (ret != napi_ok) { + return napi_invalid_arg; + } + ret = GetProperty(env, jsValue, output, createTime); + if (ret != napi_ok) { + return napi_invalid_arg; + } + ret = GetProperty(env, jsValue, output, modifyTime); + if (ret != napi_ok) { + return napi_invalid_arg; + } + ret = GetProperty(env, jsValue, output, size); + if (ret != napi_ok) { + return napi_invalid_arg; + } + ret = GetProperty(env, jsValue, output, hash); + if (ret != napi_ok) { + return napi_invalid_arg; + } return napi_ok; } @@ -45,6 +80,7 @@ napi_value Convert2JSValue(napi_env env, const Asset &value) NAPI_CALL_BASE(env, AddJsProperty(env, object, value, hash), object); return object; } + template<> napi_value Convert2JSValue(napi_env env, const RowEntity &rowEntity) { diff --git a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp index 2bc17831..04086894 100644 --- a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp +++ b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp @@ -41,6 +41,7 @@ using OHOS::DistributedRdb::SubscribeMode; using OHOS::DistributedRdb::SubscribeOption; using OHOS::DistributedRdb::SyncOption; using OHOS::DistributedRdb::SyncResult; +using OHOS::DistributedRdb::Details; #endif namespace OHOS { @@ -77,6 +78,7 @@ struct RdbStoreContext : public Context { NativeRdb::ConflictResolution conflictResolution; #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) DistributedRdb::SyncResult syncResult; + DistributedRdb::Details details; #endif std::shared_ptr rdbPredicates = nullptr; @@ -263,7 +265,21 @@ int ParseSyncModeArg(const napi_env &env, const napi_value &arg, std::shared_ptr napi_status status = napi_get_value_int32(env, arg, &context->enumArg); CHECK_RETURN_SET(status == napi_ok, std::make_shared("mode", "a SyncMode Type.")); bool checked = context->enumArg == 0 || context->enumArg == 1; - CHECK_RETURN_SET(checked, std::make_shared("mode", "a SyncMode.")); + CHECK_RETURN_SET(checked, std::make_shared("mode", "a SyncMode of device.")); + + LOG_DEBUG("ParseSyncModeArg end"); + return OK; +} + +int ParseCloudSyncModeArg(const napi_env &env, const napi_value &arg, std::shared_ptr context) +{ + napi_valuetype type = napi_undefined; + napi_typeof(env, arg, &type); + CHECK_RETURN_SET(type == napi_number, std::make_shared("mode", "a SyncMode Type.")); + napi_status status = napi_get_value_int32(env, arg, &context->enumArg); + CHECK_RETURN_SET(status == napi_ok, std::make_shared("mode", "a SyncMode Type.")); + bool checked = (context->enumArg > 1 && context->enumArg <= 4); + CHECK_RETURN_SET(checked, std::make_shared("mode", "a SyncMode of cloud.")); LOG_DEBUG("ParseSyncModeArg end"); return OK; @@ -1025,14 +1041,24 @@ napi_value RdbStoreProxy::SetDistributedTables(napi_env env, napi_callback_info LOG_DEBUG("RdbStoreProxy::SetDistributedTables start"); auto context = std::make_shared(); auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) { - CHECK_RETURN_SET_E(argc == 1, std::make_shared("1 or 2")); + CHECK_RETURN_SET_E(argc == 1 || argc == 2, std::make_shared("1 - 3")); CHECK_RETURN(OK == ParserThis(env, self, context)); CHECK_RETURN(OK == ParseTablesName(env, argv[0], context)); + if (argc == 2) { + napi_valuetype type = napi_undefined; + napi_typeof(env, argv[1], &type); + CHECK_RETURN_SET_E(type == napi_number && napi_get_value_int32(env, argv[1], &context->enumArg) == napi_ok, + std::make_shared("mode", "a SyncMode Type.")); + bool checked = context->enumArg == 0 || context->enumArg == 1; + CHECK_RETURN_SET_E(checked, std::make_shared("mode", "a SyncMode of device.")); + } else { + context->enumArg = 0; + } }; auto exec = [context]() -> int { LOG_DEBUG("RdbStoreProxy::SetDistributedTables Async"); RdbStoreProxy *obj = reinterpret_cast(context->boundObj); - return obj->rdbStore_->SetDistributedTables(context->tablesNames); + return obj->rdbStore_->SetDistributedTables(context->tablesNames, context->enumArg); }; auto output = [context](napi_env env, napi_value &result) { napi_status status = napi_get_undefined(env, &result); @@ -1106,7 +1132,46 @@ napi_value RdbStoreProxy::Sync(napi_env env, napi_callback_info info) napi_value RdbStoreProxy::CloudSync(napi_env env, napi_callback_info info) { - return nullptr; + LOG_DEBUG("RdbStoreProxy::CloudSync start"); + auto context = std::make_shared(); + auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) { + CHECK_RETURN_SET_E(argc == 2 || argc == 3, std::make_shared("2 - 4")); + CHECK_RETURN(OK == ParserThis(env, self, context)); + CHECK_RETURN(OK == ParseCloudSyncModeArg(env, argv[0], context)); + uint32_t index = 1; + bool isArray = false; + napi_is_array(env, argv[index], &isArray); + if (isArray) { + CHECK_RETURN(OK == ParseTablesName(env, argv[index], context)); + index++; + } + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv[index], &valueType); + CHECK_RETURN_SET_E(valueType == napi_function, std::make_shared("a callback type")); + context->cloudSyncCallback = argv[index]; + + }; + auto exec = [context]() -> int { + LOG_DEBUG("RdbStoreProxy::CloudSync Async"); + auto *obj = reinterpret_cast(context->boundObj); + SyncOption option; + option.mode = static_cast(context->enumArg); + option.isBlock = true; + + + return obj->rdbStore_->Sync( + option, context->tablesNames, [context](const Details &details) { context->details = details; }); + }; + + auto output = [context](napi_env env, napi_value &result) { + napi_status status = napi_get_undefined(env, &result); + CHECK_RETURN_SET_E(status == napi_ok, std::make_shared(E_ERROR)); + LOG_DEBUG("RdbStoreProxy::CloudSync end"); + }; + context->SetAction(env, info, input, exec, output); + + CHECK_RETURN_NULL(context->error == nullptr || context->error->GetCode() == OK); + return AsyncCall::Call(env, context); } napi_value RdbStoreProxy::OnDataChangeEvent(napi_env env, size_t argc, napi_value *argv) diff --git a/relational_store/frameworks/native/dataability/src/ishared_result_set_proxy.cpp b/relational_store/frameworks/native/dataability/src/ishared_result_set_proxy.cpp index 7ca0ce4e..bccbea80 100644 --- a/relational_store/frameworks/native/dataability/src/ishared_result_set_proxy.cpp +++ b/relational_store/frameworks/native/dataability/src/ishared_result_set_proxy.cpp @@ -14,9 +14,10 @@ */ #include "ishared_result_set_proxy.h" -#include "rdb_errno.h" #include "iremote_proxy.h" #include "logger.h" +#include "rdb_errno.h" +#include "shared_block.h" namespace OHOS::NativeRdb { std::function( MessageParcel &parcel)> ISharedResultSet::consumerCreator_ = ISharedResultSetProxy::CreateProxy; diff --git a/relational_store/frameworks/native/dataability/src/ishared_result_set_stub.cpp b/relational_store/frameworks/native/dataability/src/ishared_result_set_stub.cpp index 3b5b51e8..115a7eea 100644 --- a/relational_store/frameworks/native/dataability/src/ishared_result_set_stub.cpp +++ b/relational_store/frameworks/native/dataability/src/ishared_result_set_stub.cpp @@ -17,7 +17,7 @@ #include #include "logger.h" #include "rdb_errno.h" - +#include "shared_block.h" namespace OHOS::NativeRdb { std::function(std::shared_ptr, MessageParcel &)> ISharedResultSet::providerCreator_ = ISharedResultSetStub::CreateStub; diff --git a/relational_store/frameworks/native/rdb/include/irdb_service.h b/relational_store/frameworks/native/rdb/include/irdb_service.h index f4da1a87..4ab69877 100644 --- a/relational_store/frameworks/native/rdb/include/irdb_service.h +++ b/relational_store/frameworks/native/rdb/include/irdb_service.h @@ -28,11 +28,14 @@ public: class IKvStoreDataService : public IRemoteBroker { public: - enum { GET_FEATURE_INTERFACE = 0 }; - - virtual sptr GetFeatureInterface(const std::string &name) = 0; + enum { + GET_FEATURE_INTERFACE = 0, + REGISTER_DEATH_OBSERVER, + }; DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IKvStoreDataService"); + virtual sptr GetFeatureInterface(const std::string &name) = 0; + virtual int32_t RegisterDeathObserver(const std::string &bundleName, sptr observer) = 0; }; } // namespace OHOS::DistributedRdb #endif diff --git a/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h b/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h index 61b84c29..7791ae15 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h +++ b/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h @@ -38,7 +38,7 @@ public: static RdbManagerImpl &GetInstance(); - int GetRdbService(const RdbSyncerParam ¶m, std::shared_ptr &service); + std::pair> GetRdbService(const RdbSyncerParam ¶m); void OnRemoteDied(); @@ -62,12 +62,12 @@ private: void ResetServiceHandle(); - static std::shared_ptr GetDistributedDataManager(); + static std::shared_ptr GetDistributedDataManager(const std::string &bundleName); std::mutex mutex_; std::shared_ptr distributedDataMgr_; std::shared_ptr rdbService_; - std::string bundleName_; + RdbSyncerParam param_; }; class RdbStoreDataServiceProxy : public IRemoteProxy { @@ -75,6 +75,7 @@ public: explicit RdbStoreDataServiceProxy(const sptr &impl); ~RdbStoreDataServiceProxy() = default; sptr GetFeatureInterface(const std::string &name) override; + int32_t RegisterDeathObserver(const std::string &bundleName, sptr observer) override; }; } // namespace OHOS::DistributedRdb #endif diff --git a/relational_store/frameworks/native/rdb/include/rdb_notifier_stub.h b/relational_store/frameworks/native/rdb/include/rdb_notifier_stub.h index 112ea29b..ddf8391c 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_notifier_stub.h +++ b/relational_store/frameworks/native/rdb/include/rdb_notifier_stub.h @@ -26,24 +26,27 @@ public: class RdbNotifierStub : public IRemoteStub { public: - using SyncCompleteHandler = std::function; + using SyncCompleteHandler = std::function; using DataChangeHandler = std::function&)>; RdbNotifierStub(const SyncCompleteHandler&, const DataChangeHandler&); virtual ~RdbNotifierStub() noexcept; int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; - int32_t OnComplete(uint32_t seqNum, const SyncResult& result) override; + int32_t OnComplete(uint32_t seqNum, Details &&result) override; int32_t OnChange(const std::string& storeName, const std::vector& devices) override; + int32_t OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo) override; private: int32_t OnCompleteInner(MessageParcel& data, MessageParcel& reply); int32_t OnChangeInner(MessageParcel& data, MessageParcel& reply); + int32_t OnDetailsInner(MessageParcel& data, MessageParcel& reply); bool CheckInterfaceToken(MessageParcel& data); using RequestHandle = int32_t (RdbNotifierStub::*)(MessageParcel&, MessageParcel&); static constexpr RequestHandle HANDLES[RDB_NOTIFIER_CMD_MAX] = { [RDB_NOTIFIER_CMD_SYNC_COMPLETE] = &RdbNotifierStub::OnCompleteInner, [RDB_NOTIFIER_CMD_DATA_CHANGE] = &RdbNotifierStub::OnChangeInner, + [RDB_NOTIFIER_CMD_DATA_DETAILS] = &RdbNotifierStub::OnChangeInner, }; SyncCompleteHandler completeNotifier_; diff --git a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h index 5e28457e..f058d214 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h +++ b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h @@ -34,12 +34,13 @@ public: std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; int32_t InitNotifier(const RdbSyncerParam ¶m); - int32_t InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) override; - - int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector& tables) override; - - int32_t Sync(const RdbSyncerParam& param, const SyncOption& option, - const RdbPredicates& predicates, const SyncCallback& callback) override; + int32_t InitNotifier(const RdbSyncerParam ¶m, sptr notifier) override; + + int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables, + int32_t type = DISTRIBUTED_DEVICE) override; + + int32_t Sync(const RdbSyncerParam& param, const Option& option, + const RdbPredicates& predicates, const AsyncDetail &async) override; int32_t Subscribe(const RdbSyncerParam& param, const SubscribeOption& option, RdbStoreObserver *observer) override; @@ -49,35 +50,31 @@ public: int32_t RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql, const std::vector& selectionArgs, sptr& resultSet) override; - int32_t CreateRDBTable(const RdbSyncerParam ¶m) override; - int32_t DestroyRDBTable(const RdbSyncerParam ¶m) override; ObserverMap ExportObservers(); void ImportObservers(ObserverMap& observers); /*CLoudData*/ int32_t GetSchema(const RdbSyncerParam ¶m) override; -protected: - int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option, - const RdbPredicates& predicates, SyncResult& result) override; +private: + std::pair DoSync(const RdbSyncerParam ¶m, const Option &option, + const RdbPredicates &predicates); - int32_t DoAsync(const RdbSyncerParam& param, uint32_t seqNum, const SyncOption& option, - const RdbPredicates& predicates) override; + int32_t DoAsync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &predicates); - int32_t DoSubscribe(const RdbSyncerParam& param, const SubscribeOption &option) override; + int32_t DoSync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &predicates, + const AsyncDetail &async); - int32_t DoUnSubscribe(const RdbSyncerParam& param) override; + int32_t DoAsync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &predicates, + const AsyncDetail &async); -private: - uint32_t GetSeqNum(); + int32_t DoSubscribe(const RdbSyncerParam& param, const SubscribeOption &option); - int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option, - const RdbPredicates& predicates, const SyncCallback& callback); + int32_t DoUnSubscribe(const RdbSyncerParam& param); - int32_t DoAsync(const RdbSyncerParam& param, const SyncOption& option, - const RdbPredicates& predicates, const SyncCallback& callback); + uint32_t GetSeqNum(); - void OnSyncComplete(uint32_t seqNum, const SyncResult& result); + void OnSyncComplete(uint32_t seqNum, Details &&result); void OnDataChange(const std::string& storeName, const std::vector& devices); @@ -85,7 +82,7 @@ private: std::atomic seqNum_ {}; - ConcurrentMap syncCallbacks_; + ConcurrentMap syncCallbacks_; ObserverMap observers_; sptr notifier_; diff --git a/relational_store/frameworks/native/rdb/include/rdb_store_impl.h b/relational_store/frameworks/native/rdb/include/rdb_store_impl.h index 078a04c7..29d8e75e 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_store_impl.h +++ b/relational_store/frameworks/native/rdb/include/rdb_store_impl.h @@ -28,6 +28,9 @@ #include "sqlite_statement.h" #include "transaction_observer.h" +namespace OHOS { +class ExecutorPool; +} namespace OHOS::NativeRdb { class RdbStoreImpl : public RdbStore, public std::enable_shared_from_this { public: @@ -99,11 +102,13 @@ public: std::shared_ptr RemoteQuery(const std::string &device, const AbsRdbPredicates &predicates, const std::vector &columns, int &errCode) override; - int SetDistributedTables(const std::vector& tables) override; + int SetDistributedTables(const std::vector& tables, int32_t type) override; std::string ObtainDistributedTableName(const std::string& device, const std::string& table, int &errCode) override; - int Sync(const SyncOption& option, const AbsRdbPredicates& predicate, const SyncCallback& callback) override; + int Sync(const SyncOption &option, const AbsRdbPredicates &predicate, const AsyncBrief &async) override; + + int Sync(const SyncOption &option, const std::vector &tables, const AsyncDetail &async) override; int Subscribe(const SubscribeOption& option, RdbStoreObserver *observer) override; @@ -131,9 +136,9 @@ private: std::string name; std::string fileType; std::stack transactionObserverStack; - bool isShared_ = false; DistributedRdb::RdbSyncerParam syncerParam_; bool isEncrypt_; + std::shared_ptr pool_; }; } // namespace OHOS::NativeRdb #endif diff --git a/relational_store/frameworks/native/rdb/include/rdb_store_manager.h b/relational_store/frameworks/native/rdb/include/rdb_store_manager.h index a091d017..4a4dc3bb 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_store_manager.h +++ b/relational_store/frameworks/native/rdb/include/rdb_store_manager.h @@ -23,7 +23,7 @@ #include "rdb_open_callback.h" #include "rdb_store_config.h" #include "rdb_store_impl.h" -#include "timer.h" +#include "task_executor.h" namespace OHOS { namespace NativeRdb { @@ -32,7 +32,7 @@ struct RdbStoreNode { RdbStoreNode &operator=(const std::shared_ptr &store); std::shared_ptr rdbStore_; - uint32_t timerId_; + TaskExecutor::TaskId taskId_; }; class RdbStoreManager { @@ -54,8 +54,9 @@ private: void AutoClose(const std::string &path); static void InitSecurityManager(const RdbStoreConfig &config); std::mutex mutex_; - std::shared_ptr timer_; std::map> storeCache_; + + std::shared_ptr pool_; // ms_ : [10*1000 ~ 10*60*1000] int ms_; }; diff --git a/relational_store/frameworks/native/rdb/include/rdb_types_util.h b/relational_store/frameworks/native/rdb/include/rdb_types_util.h index dd023b61..992263ac 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_types_util.h +++ b/relational_store/frameworks/native/rdb/include/rdb_types_util.h @@ -16,27 +16,31 @@ #ifndef DISTRIBUTED_RDB_RDB_TYPES_UTIL_H #define DISTRIBUTED_RDB_RDB_TYPES_UTIL_H #include "itypes_util.h" +#include "rdb_service.h" #include "rdb_types.h" +#include "rdb_visibility.h" #include "value_object.h" #include "values_bucket.h" -#include "rdb_visibility.h" namespace OHOS::ITypesUtil { using SubOption = DistributedRdb::SubscribeOption; using SyncerParam = DistributedRdb::RdbSyncerParam; -using SyncOption = DistributedRdb::SyncOption; +using Option = DistributedRdb::RdbService::Option; using RdbPredicates = DistributedRdb::RdbPredicates; using RdbOperation = DistributedRdb::RdbPredicateOperation; using ValueObject = NativeRdb::ValueObject; using ValuesBucket = NativeRdb::ValuesBucket; using Asset = NativeRdb::AssetValue; +using ProgressDetail = DistributedRdb::ProgressDetail; +using TableDetail = DistributedRdb::TableDetail; +using Statistic = DistributedRdb::Statistic; template<> API_EXPORT bool Marshalling(const SyncerParam &input, MessageParcel &data); template<> API_EXPORT bool Unmarshalling(SyncerParam &output, MessageParcel &data); template<> -API_EXPORT bool Marshalling(const SyncOption &input, MessageParcel &data); +API_EXPORT bool Marshalling(const Option &input, MessageParcel &data); template<> -API_EXPORT bool Unmarshalling(SyncOption &output, MessageParcel &data); +API_EXPORT bool Unmarshalling(Option &output, MessageParcel &data); template<> API_EXPORT bool Marshalling(const RdbPredicates &input, MessageParcel &data); template<> @@ -61,8 +65,21 @@ template<> API_EXPORT bool Marshalling(const Asset &input, MessageParcel &data); template<> API_EXPORT bool Unmarshalling(Asset &output, MessageParcel &data); +template<> API_EXPORT bool Marshalling(const SubOption &input, MessageParcel &data); template<> API_EXPORT bool Unmarshalling(SubOption &output, MessageParcel &data); +template<> +API_EXPORT bool Marshalling(const ProgressDetail &input, MessageParcel &data); +template<> +API_EXPORT bool Unmarshalling(ProgressDetail &output, MessageParcel &data); +template<> +API_EXPORT bool Marshalling(const TableDetail &input, MessageParcel &data); +template<> +API_EXPORT bool Unmarshalling(TableDetail &output, MessageParcel &data); +template<> +API_EXPORT bool Marshalling(const Statistic &input, MessageParcel &data); +template<> +API_EXPORT bool Unmarshalling(Statistic &output, MessageParcel &data); } #endif // DISTRIBUTED_RDB_RDB_TYPES_UTIL_H diff --git a/relational_store/frameworks/native/rdb/include/result_set_proxy.h b/relational_store/frameworks/native/rdb/include/result_set_proxy.h index 60240ab1..8924b0f5 100644 --- a/relational_store/frameworks/native/rdb/include/result_set_proxy.h +++ b/relational_store/frameworks/native/rdb/include/result_set_proxy.h @@ -46,6 +46,7 @@ public: int GetInt(int columnIndex, int &value) override; int GetLong(int columnIndex, int64_t &value) override; int GetDouble(int columnIndex, double &value) override; + int GetSize(int columnIndex, size_t &size) override; int IsColumnNull(int columnIndex, bool &isNull) override; int GetRow(RowEntity &rowEntity) override; bool IsClosed() const override; diff --git a/relational_store/frameworks/native/rdb/include/shared_block_serializer_info.h b/relational_store/frameworks/native/rdb/include/shared_block_serializer_info.h index c7cd18de..d321f05a 100644 --- a/relational_store/frameworks/native/rdb/include/shared_block_serializer_info.h +++ b/relational_store/frameworks/native/rdb/include/shared_block_serializer_info.h @@ -27,7 +27,7 @@ extern "C" { #endif class SharedBlockSerializerInfo { public: - SharedBlockSerializerInfo(AppDataFwk::SharedBlock *sharedBlock, int numColumns, int startPos); + SharedBlockSerializerInfo(AppDataFwk::SharedBlock *sharedBlock, sqlite3_stmt *stat, int numColumns, int startPos); ~SharedBlockSerializerInfo(); int AddRow(int addedRows); int Reset(int startPos); @@ -42,7 +42,8 @@ public: int GetAddedRows() const; int GetStartPos() const; private: - AppDataFwk::SharedBlock *sharedBlock_; + AppDataFwk::SharedBlock *sharedBlock_ = nullptr; + sqlite3_stmt *statement_ = nullptr; int anumColumns; int atotalRows; int astartPos; diff --git a/relational_store/frameworks/native/rdb/include/sqlite_statement.h b/relational_store/frameworks/native/rdb/include/sqlite_statement.h index 702f1cb6..67fe2dff 100644 --- a/relational_store/frameworks/native/rdb/include/sqlite_statement.h +++ b/relational_store/frameworks/native/rdb/include/sqlite_statement.h @@ -40,6 +40,7 @@ public: int GetColumnString(int index, std::string &value) const; int GetColumnLong(int index, int64_t &value) const; int GetColumnDouble(int index, double &value) const; + int GetSize(int index, size_t &size) const; int GetColumn(int index, ValueObject &value) const; bool IsReadOnly() const; int GetNumParameters(int &numParams) const; diff --git a/relational_store/frameworks/native/rdb/include/step_result_set.h b/relational_store/frameworks/native/rdb/include/step_result_set.h index 1fe297b0..05796495 100644 --- a/relational_store/frameworks/native/rdb/include/step_result_set.h +++ b/relational_store/frameworks/native/rdb/include/step_result_set.h @@ -49,6 +49,7 @@ public: int GetInt(int columnIndex, int &value) override; int GetLong(int columnIndex, int64_t &value) override; int GetDouble(int columnIndex, double &value) override; + int GetSize(int columnIndex, size_t &size) override; int GetAsset(int32_t col, ValueObject::Asset &value) override; int GetAssets(int32_t col, ValueObject::Assets &value) override; int Get(int32_t col, ValueObject &value) override; diff --git a/relational_store/frameworks/native/rdb/include/task_executor.h b/relational_store/frameworks/native/rdb/include/task_executor.h new file mode 100644 index 00000000..4f3d8d27 --- /dev/null +++ b/relational_store/frameworks/native/rdb/include/task_executor.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 DISTRIBUTED_NATIVE_RDB_TASK_EXECUTOR_H +#define DISTRIBUTED_NATIVE_RDB_TASK_EXECUTOR_H +#include "executor_pool.h" +namespace OHOS::NativeRdb { +class TaskExecutor { +public: + using TaskId = ExecutorPool::TaskId; + using Task = std::function; + using Duration = std::chrono::steady_clock::duration; + static constexpr TaskId INVALID_TASK_ID = ExecutorPool::INVALID_TASK_ID; + + static TaskExecutor &GetInstance(); + std::shared_ptr GetExecutor(); + void SetExecutor(std::shared_ptr executor); + +private: + size_t MAX_THREADS = 2; + size_t MIN_THREADS = 0; + TaskExecutor(); + ~TaskExecutor(); + mutable std::shared_mutex rwMutex_; + std::shared_ptr pool_; +}; +} // namespace OHOS::NativeRdb +#endif // DISTRIBUTED_DATA_TASK_EXECUTOR_H diff --git a/relational_store/frameworks/native/rdb/mock/include/rdb_store_manager.h b/relational_store/frameworks/native/rdb/mock/include/rdb_store_manager.h index a091d017..de808733 100644 --- a/relational_store/frameworks/native/rdb/mock/include/rdb_store_manager.h +++ b/relational_store/frameworks/native/rdb/mock/include/rdb_store_manager.h @@ -23,7 +23,7 @@ #include "rdb_open_callback.h" #include "rdb_store_config.h" #include "rdb_store_impl.h" -#include "timer.h" +#include "task_executor.h" namespace OHOS { namespace NativeRdb { @@ -32,7 +32,7 @@ struct RdbStoreNode { RdbStoreNode &operator=(const std::shared_ptr &store); std::shared_ptr rdbStore_; - uint32_t timerId_; + uint64_t taskId_; }; class RdbStoreManager { @@ -54,7 +54,7 @@ private: void AutoClose(const std::string &path); static void InitSecurityManager(const RdbStoreConfig &config); std::mutex mutex_; - std::shared_ptr timer_; + std::shared_ptr pool_; std::map> storeCache_; // ms_ : [10*1000 ~ 10*60*1000] int ms_; diff --git a/relational_store/frameworks/native/rdb/mock/include/task_executor.h b/relational_store/frameworks/native/rdb/mock/include/task_executor.h new file mode 100644 index 00000000..9ed65483 --- /dev/null +++ b/relational_store/frameworks/native/rdb/mock/include/task_executor.h @@ -0,0 +1,81 @@ +/* +* 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 DISTRIBUTED_NATIVE_RDB_TASK_EXECUTOR_H +#define DISTRIBUTED_NATIVE_RDB_TASK_EXECUTOR_H +#include +#include +#include +namespace OHOS::NativeRdb { +class TaskExecutor { +public: + using TaskId = uint64_t; + using Task = std::function; + using Duration = std::chrono::steady_clock::duration; + static constexpr TaskId INVALID_TASK_ID = static_cast(0l); + class ExecutorPool { + public: + ExecutorPool(size_t max, size_t min) + { + } + ~ExecutorPool() + { + } + + TaskId Execute(Task task) + { + return INVALID_TASK_ID; + } + + TaskId Schedule(Duration delay, Task task) + { + return INVALID_TASK_ID; + } + + TaskId Schedule(Task task, Duration interval) + { + return INVALID_TASK_ID; + } + + TaskId Schedule(Task task, Duration delay, Duration interval) + { + return INVALID_TASK_ID; + } + + TaskId Schedule(Task task, Duration delay, Duration interval, uint64_t times) + { + return INVALID_TASK_ID; + } + + bool Remove(TaskId taskId, bool wait = false) + { + return true; + } + + TaskId Reset(TaskId taskId, Duration interval) + { + return INVALID_TASK_ID; + } + }; + static TaskExecutor &GetInstance(); + std::shared_ptr GetExecutor(); + void SetExecutor(std::shared_ptr executor); + +private: + TaskExecutor(); + ~TaskExecutor(); + std::shared_ptr pool_; +}; +} // namespace OHOS::NativeRdb +#endif // DISTRIBUTED_DATA_TASK_EXECUTOR_H diff --git a/relational_store/frameworks/native/rdb/mock/src/task_executor.cpp b/relational_store/frameworks/native/rdb/mock/src/task_executor.cpp new file mode 100644 index 00000000..3249537f --- /dev/null +++ b/relational_store/frameworks/native/rdb/mock/src/task_executor.cpp @@ -0,0 +1,43 @@ +/* +* 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 "task_executor.h" + +namespace OHOS::NativeRdb { +TaskExecutor::TaskExecutor():pool_(nullptr) +{ +} + +TaskExecutor::~TaskExecutor() +{ + pool_ = nullptr; +} + +TaskExecutor &TaskExecutor::GetInstance() +{ + static TaskExecutor instance; + return instance; +} + +std::shared_ptr TaskExecutor::GetExecutor() +{ + return pool_; +} + +void TaskExecutor::SetExecutor(std::shared_ptr executor) +{ + pool_ = executor; +}; +} // namespace OHOS::NativeRdb diff --git a/relational_store/frameworks/native/rdb/src/abs_rdb_predicates.cpp b/relational_store/frameworks/native/rdb/src/abs_rdb_predicates.cpp index 82047a58..c892f201 100644 --- a/relational_store/frameworks/native/rdb/src/abs_rdb_predicates.cpp +++ b/relational_store/frameworks/native/rdb/src/abs_rdb_predicates.cpp @@ -23,13 +23,26 @@ namespace OHOS::NativeRdb { AbsRdbPredicates::AbsRdbPredicates(std::string tableName) { if (tableName.empty()) { - this->tableName = ""; + tableName_ = ""; LOG_INFO("no tableName specified."); return; } - this->tableName = tableName; + tableName_ = std::move(tableName); #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) - predicates_.table_ = tableName; + predicates_.tables_.push_back(tableName_); +#endif +} + +AbsRdbPredicates::AbsRdbPredicates(std::vector tables) +{ + if (tables.empty()) { + tableName_ = ""; + LOG_INFO("no tableName specified."); + return; + } + tableName_ = *(tables.begin()); +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) + predicates_.tables_ = std::move(tables); #endif } @@ -56,9 +69,9 @@ std::vector AbsRdbPredicates::GetJoinTypes() * Sets the join types in the predicates. The value can be {@code INNER JOIN}, {@code LEFT OUTER JOIN}, * and {@code CROSS JOIN}. */ -void AbsRdbPredicates::SetJoinTypes(const std::vector joinTypes) +void AbsRdbPredicates::SetJoinTypes(std::vector joinTypes) { - this->joinTypes = joinTypes; + this->joinTypes = std::move(joinTypes); } /** @@ -72,9 +85,9 @@ std::vector AbsRdbPredicates::GetJoinTableNames() /** * Sets the database table names of the joins in the predicates. */ -void AbsRdbPredicates::SetJoinTableNames(const std::vector joinTableNames) +void AbsRdbPredicates::SetJoinTableNames(std::vector joinTableNames) { - this->joinTableNames = joinTableNames; + this->joinTableNames = std::move(joinTableNames); } /** @@ -88,9 +101,9 @@ std::vector AbsRdbPredicates::GetJoinConditions() /** * Sets the join conditions required in the predicates. */ -void AbsRdbPredicates::SetJoinConditions(const std::vector joinConditions) +void AbsRdbPredicates::SetJoinConditions(std::vector joinConditions) { - this->joinConditions = joinConditions; + this->joinConditions = std::move(joinConditions); } /** @@ -98,7 +111,7 @@ void AbsRdbPredicates::SetJoinConditions(const std::vector joinCond */ std::string AbsRdbPredicates::GetJoinClause() const { - return tableName; + return tableName_; } /** @@ -122,7 +135,7 @@ void AbsRdbPredicates::SetJoinCount(int joinCount) */ std::string AbsRdbPredicates::GetTableName() const { - return tableName; + return tableName_; } std::string AbsRdbPredicates::ToString() const diff --git a/relational_store/frameworks/native/rdb/src/abs_shared_result_set.cpp b/relational_store/frameworks/native/rdb/src/abs_shared_result_set.cpp index 9899f947..b3b8bdce 100644 --- a/relational_store/frameworks/native/rdb/src/abs_shared_result_set.cpp +++ b/relational_store/frameworks/native/rdb/src/abs_shared_result_set.cpp @@ -372,6 +372,31 @@ int AbsSharedResultSet::GetAssets(int32_t col, ValueObject::Assets &value) return E_OK; } +int AbsSharedResultSet::GetSize(int columnIndex, size_t &size) +{ + size = 0; + int errorCode = CheckState(columnIndex); + if (errorCode != E_OK) { + return errorCode; + } + + AppDataFwk::SharedBlock::CellUnit *cellUnit = sharedBlock_->GetCellUnit(sharedBlock_->GetBlockPos(), columnIndex); + if (cellUnit == nullptr) { + LOG_ERROR("cellUnit is null!"); + return E_ERROR; + } + + int type = cellUnit->type; + if (type == AppDataFwk::SharedBlock::CELL_UNIT_TYPE_STRING + || type == AppDataFwk::SharedBlock::CELL_UNIT_TYPE_BLOB + || type == AppDataFwk::SharedBlock::CELL_UNIT_TYPE_NULL) { + sharedBlock_->GetCellUnitValueBlob(cellUnit, &size); + return E_OK; + } + + return E_INVALID_OBJECT_TYPE; +} + int AbsSharedResultSet::IsColumnNull(int columnIndex, bool &isNull) { int errorCode = CheckState(columnIndex); diff --git a/relational_store/frameworks/native/rdb/src/raw_data_parser.cpp b/relational_store/frameworks/native/rdb/src/raw_data_parser.cpp index f12752a9..ecb6ba33 100644 --- a/relational_store/frameworks/native/rdb/src/raw_data_parser.cpp +++ b/relational_store/frameworks/native/rdb/src/raw_data_parser.cpp @@ -14,8 +14,6 @@ */ #include "raw_data_parser.h" - -#include "securec.h" #include "value_object.h" namespace OHOS::NativeRdb { size_t RawDataParser::ParserRawData(const uint8_t *data, size_t length, Asset &asset) @@ -24,19 +22,26 @@ size_t RawDataParser::ParserRawData(const uint8_t *data, size_t length, Asset &a if (used + sizeof(asset.version) > length) { return used; } - memcpy_s(&asset.version, sizeof(asset.version), data, length); - return sizeof(asset.version); + std::vector alignData; + alignData.assign(data, data + sizeof(asset.version)); + asset.version = *(reinterpret_cast(alignData.data())); + used += sizeof(asset.version); + return used; } + size_t RawDataParser::ParserRawData(const uint8_t *data, size_t length, Assets &assets) { size_t used = 0; uint16_t num = 0; - if (used + sizeof(uint16_t) > length) { + if (used + sizeof(num) > length) { return used; } - memcpy_s(&num, sizeof(num), data, length); + std::vector alignData; + alignData.assign(data, data + sizeof(num)); + num = *(reinterpret_cast(alignData.data())); used += sizeof(uint16_t); - while (used < length) { + uint16_t count = 0; + while (used < length && count < num) { Asset asset; auto dataLen = ParserRawData(&data[used], length - used, asset); if (dataLen == 0) { @@ -44,6 +49,7 @@ size_t RawDataParser::ParserRawData(const uint8_t *data, size_t length, Assets & } used += dataLen; assets.push_back(std::move(asset)); + count++; } return used; } diff --git a/relational_store/frameworks/native/rdb/src/rdb_helper.cpp b/relational_store/frameworks/native/rdb/src/rdb_helper.cpp index b3110f53..fa11e9a5 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_helper.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_helper.cpp @@ -67,7 +67,8 @@ int RdbHelper::DeleteRdbStore(const std::string &dbFileName) } int result = remove(dbFileName.c_str()); if (result != 0) { - LOG_ERROR("RdbHelper DeleteRdbStore failed to delete the db file err = %{public}d", errno); + int32_t err = errno; + LOG_ERROR("RdbHelper DeleteRdbStore failed to delete the db file err = %{public}d", err); return E_REMOVE_FILE; } diff --git a/relational_store/frameworks/native/rdb/src/rdb_manager_impl.cpp b/relational_store/frameworks/native/rdb/src/rdb_manager_impl.cpp index 4023b536..401b397b 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_manager_impl.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_manager_impl.cpp @@ -31,25 +31,33 @@ namespace OHOS::DistributedRdb { using RdbServiceProxy = DistributedRdb::RdbServiceProxy; using namespace OHOS::NativeRdb; -std::shared_ptr RdbManagerImpl::GetDistributedDataManager() +class DeathStub : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.DeathStub"); +}; +class DeathStubImpl : public IRemoteStub { +}; +std::shared_ptr RdbManagerImpl::GetDistributedDataManager(const std::string &bundleName) { auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (manager == nullptr) { ZLOGE("get system ability manager failed"); return nullptr; } - auto remoteObject = manager->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID); - if (remoteObject == nullptr) { + auto dataMgr = manager->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID); + if (dataMgr == nullptr) { ZLOGE("get distributed data manager failed"); return nullptr; } - sptr rdbStoreDataServiceProxy = new(std::nothrow) RdbStoreDataServiceProxy(remoteObject); - if (rdbStoreDataServiceProxy == nullptr) { + sptr dataService = new (std::nothrow) RdbStoreDataServiceProxy(dataMgr); + if (dataService == nullptr) { ZLOGE("new RdbStoreDataServiceProxy failed"); return nullptr; } - return std::shared_ptr(rdbStoreDataServiceProxy.GetRefPtr(), - [holder = rdbStoreDataServiceProxy](const auto *) {}); + + sptr observer = new (std::nothrow) DeathStubImpl(); + dataService->RegisterDeathObserver(bundleName, observer); + return std::shared_ptr(dataService.GetRefPtr(), [dataService, observer](const auto *) {}); } static void LinkToDeath(const sptr& remote) @@ -59,6 +67,7 @@ static void LinkToDeath(const sptr& remote) new(std::nothrow) RdbManagerImpl::ServiceDeathRecipient(&manager); if (deathRecipient == nullptr) { ZLOGE("new ServiceDeathRecipient failed"); + return; } if (!remote->AddDeathRecipient(deathRecipient)) { ZLOGE("add death recipient failed"); @@ -81,52 +90,50 @@ RdbManagerImpl& RdbManagerImpl::GetInstance() return manager; } -int RdbManagerImpl::GetRdbService(const RdbSyncerParam ¶m, std::shared_ptr &service) +std::pair> RdbManagerImpl::GetRdbService(const RdbSyncerParam ¶m) { + if (param.bundleName_.empty()) { + return { E_INVALID_ARGS, nullptr }; + } + std::lock_guard lock(mutex_); if (rdbService_ != nullptr) { - service = rdbService_; - return E_OK; + return { E_OK, rdbService_ }; } + if (distributedDataMgr_ == nullptr) { - distributedDataMgr_ = GetDistributedDataManager(); + distributedDataMgr_ = GetDistributedDataManager(param.bundleName_); } if (distributedDataMgr_ == nullptr) { ZLOGE("get distributed data manager failed"); - return E_ERROR; + return { E_ERROR, nullptr }; } auto remote = distributedDataMgr_->GetFeatureInterface(DistributedRdb::RdbService::SERVICE_NAME); if (remote == nullptr) { ZLOGE("get rdb service failed"); - return E_NOT_SUPPORTED; + return { E_NOT_SUPPORTED, nullptr }; } - sptr serviceProxy = nullptr; + sptr rdbService = nullptr; if (remote->IsProxyObject()) { - serviceProxy = iface_cast(remote); + rdbService = iface_cast(remote); } - if (serviceProxy == nullptr) { - serviceProxy = new (std::nothrow) RdbServiceProxy(remote); + if (rdbService == nullptr) { + rdbService = new (std::nothrow) RdbServiceProxy(remote); } - if (serviceProxy == nullptr) { - return E_ERROR; - } -// sptr serviceProxy = iface_cast(remote); - if (serviceProxy->InitNotifier(param) != RDB_OK) { + if (rdbService == nullptr || rdbService->InitNotifier(param) != RDB_OK) { ZLOGE("init notifier failed"); - return E_ERROR; + return { E_ERROR, nullptr }; } - sptr serviceBase = serviceProxy; - LinkToDeath(serviceBase->AsObject().GetRefPtr()); - rdbService_ = std::shared_ptr(serviceProxy.GetRefPtr(), [holder = serviceProxy] (const auto*) {}); - if (rdbService_ == nullptr) { - return E_ERROR; - } - bundleName_ = param.bundleName_; - service = rdbService_; - return E_OK; + + sptr serviceBase = rdbService; + LinkToDeath(serviceBase->AsObject()); + // the rdbService is not null, so rdbService.GetRefPtr() is not null; + rdbService_ = std::shared_ptr(rdbService.GetRefPtr(), [rdbService](const auto *) {}); + param_ = param; + return { E_OK, rdbService_ }; } void RdbManagerImpl::OnRemoteDied() @@ -141,10 +148,7 @@ void RdbManagerImpl::OnRemoteDied() ResetServiceHandle(); std::this_thread::sleep_for(std::chrono::seconds(WAIT_TIME)); - RdbSyncerParam param; - param.bundleName_ = bundleName_; - std::shared_ptr service = nullptr; - int errCode = GetRdbService(param, service); + auto [errCode, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(param_); if (errCode != E_OK) { return; } @@ -197,4 +201,30 @@ sptr RdbStoreDataServiceProxy::GetFeatureInterface(const std::str } return remoteObject; } + +int32_t RdbStoreDataServiceProxy::RegisterDeathObserver(const std::string &bundleName, sptr observer) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(RdbStoreDataServiceProxy::GetDescriptor())) { + ZLOGE("write descriptor failed"); + return E_ERROR; + } + + if (!ITypesUtil::Marshal(data, bundleName, observer)) { + ZLOGE("write descriptor failed"); + return E_ERROR; + } + + MessageParcel reply; + MessageOption mo { MessageOption::TF_SYNC }; + int32_t error = Remote()->SendRequest(REGISTER_DEATH_OBSERVER, data, reply, mo); + if (error != 0) { + ZLOGE("SendRequest returned %{public}d", error); + return E_ERROR; + } + + int32_t status = E_ERROR; + ITypesUtil::Unmarshal(reply, status); + return status; +} } // namespace OHOS::DistributedRdb diff --git a/relational_store/frameworks/native/rdb/src/rdb_notifier_stub.cpp b/relational_store/frameworks/native/rdb/src/rdb_notifier_stub.cpp index 08112ac8..77f00c0d 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_notifier_stub.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_notifier_stub.cpp @@ -57,23 +57,19 @@ int RdbNotifierStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Message int32_t RdbNotifierStub::OnCompleteInner(MessageParcel &data, MessageParcel &reply) { - uint32_t seqNum; - if (!data.ReadUint32(seqNum)) { - ZLOGI("read seq num failed"); - return RDB_ERROR; - } - SyncResult result; - if (!ITypesUtil::Unmarshal(data, result)) { + uint32_t seqNum = 0; + Details result; + if (!ITypesUtil::Unmarshal(data, seqNum, result)) { ZLOGE("read sync result failed"); return RDB_ERROR; } - return OnComplete(seqNum, result); + return OnComplete(seqNum, std::move(result)); } -int32_t RdbNotifierStub::OnComplete(uint32_t seqNum, const SyncResult &result) +int32_t RdbNotifierStub::OnComplete(uint32_t seqNum, Details &&result) { if (completeNotifier_) { - completeNotifier_(seqNum, result); + completeNotifier_(seqNum, std::move(result)); } return RDB_OK; } @@ -81,13 +77,9 @@ int32_t RdbNotifierStub::OnComplete(uint32_t seqNum, const SyncResult &result) int32_t RdbNotifierStub::OnChangeInner(MessageParcel &data, MessageParcel &reply) { std::string storeName; - if (!data.ReadString(storeName)) { - ZLOGE("read store name failed"); - return RDB_ERROR; - } std::vector devices; - if (!data.ReadStringVector(&devices)) { - ZLOGE("read devices failed"); + if (!ITypesUtil::Unmarshal(data, storeName, devices)) { + ZLOGE("read sync result failed"); return RDB_ERROR; } return OnChange(storeName, devices); @@ -100,4 +92,21 @@ int32_t RdbNotifierStub::OnChange(const std::string& storeName, const std::vecto } return RDB_OK; } + +int32_t RdbNotifierStub::OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo) +{ + return 0; +} + +int32_t RdbNotifierStub::OnDetailsInner(MessageParcel &data, MessageParcel &reply) +{ + Origin origin; + PrimaryFields primaries; + ChangeInfo changeInfo; +// if (!ITypesUtil::Unmarshal(data, origin, primaries, changeInfo)) { +// ZLOGE("read sync result failed"); +// return RDB_ERROR; +// } + return OnChange(origin, primaries, std::move(changeInfo)); +} } // namespace OHOS::DistributedRdb diff --git a/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp b/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp index bac75d40..7a2e1a2e 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp @@ -51,10 +51,10 @@ RdbServiceProxy::RdbServiceProxy(const sptr &object) remote_ = Remote(); } -void RdbServiceProxy::OnSyncComplete(uint32_t seqNum, const SyncResult &result) +void RdbServiceProxy::OnSyncComplete(uint32_t seqNum, Details &&result) { - syncCallbacks_.ComputeIfPresent(seqNum, [&result] (const auto& key, const SyncCallback& callback) { - callback(result); + syncCallbacks_.ComputeIfPresent(seqNum, [&result] (const auto& key, const AsyncDetail & callback) { + callback(std::move(result)); return true; }); syncCallbacks_.Erase(seqNum); @@ -86,8 +86,8 @@ std::string RdbServiceProxy::ObtainDistributedTableName(const std::string &devic int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam ¶m) { notifier_ = new (std::nothrow) RdbNotifierStub( - [this] (uint32_t seqNum, const SyncResult& result) { - OnSyncComplete(seqNum, result); + [this] (uint32_t seqNum, Details &&result) { + OnSyncComplete(seqNum, std::move(result)); }, [this] (const std::string& storeName, const std::vector& devices) { OnDataChange(storeName, devices); @@ -98,7 +98,7 @@ int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam ¶m) return RDB_ERROR; } - if (InitNotifier(param, notifier_->AsObject().GetRefPtr()) != RDB_OK) { + if (InitNotifier(param, notifier_->AsObject()) != RDB_OK) { notifier_ = nullptr; return RDB_ERROR; } @@ -107,7 +107,7 @@ int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam ¶m) return RDB_OK; } -int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) +int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam ¶m, sptr notifier) { MessageParcel reply; int32_t status = IPC_SEND(RDB_SERVICE_CMD_INIT_NOTIFIER, reply, param, notifier); @@ -119,68 +119,76 @@ int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam ¶m, const sptr RdbServiceProxy::DoSync(const RdbSyncerParam& param, const Option &option, + const RdbPredicates &predicates) { + std::pair result{RDB_ERROR, {}}; MessageParcel reply; - int32_t status = IPC_SEND(RDB_SERVICE_CMD_SYNC, reply, param, option, predicates); + auto &[status, details] = result; + status = IPC_SEND(RDB_SERVICE_CMD_SYNC, reply, param, option, predicates); if (status != RDB_OK) { ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", status, param.bundleName_.c_str(), param.storeName_.c_str()); - return status; + return result; } - if (!ITypesUtil::Unmarshal(reply, result)) { + if (!ITypesUtil::Unmarshal(reply, details)) { ZLOGE("read result failed"); - return RDB_ERROR; + status = RDB_ERROR; + return result; } - return RDB_OK; + return result; } -int32_t RdbServiceProxy::DoSync(const RdbSyncerParam& param, const SyncOption &option, - const RdbPredicates &predicates, const SyncCallback& callback) +int32_t RdbServiceProxy::DoSync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &predicates, + const AsyncDetail &async) { - SyncResult result; - if (DoSync(param, option, predicates, result) != RDB_OK) { + auto [status, details] = DoSync(param, option, predicates); + if (status != RDB_OK) { ZLOGI("failed"); return RDB_ERROR; } ZLOGI("success"); - if (callback != nullptr) { - callback(result); + if (async != nullptr) { + async(std::move(details)); } return RDB_OK; } -int32_t RdbServiceProxy::DoAsync(const RdbSyncerParam& param, uint32_t seqNum, const SyncOption &option, - const RdbPredicates &predicates) +int32_t RdbServiceProxy::DoAsync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &predicates) { MessageParcel reply; - int32_t status = IPC_SEND(RDB_SERVICE_CMD_ASYNC, reply, param, seqNum, option, predicates); + int32_t status = IPC_SEND(RDB_SERVICE_CMD_ASYNC, reply, param, option, predicates); if (status != RDB_OK) { - ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s, seqNum:%{public}u", - status, param.bundleName_.c_str(), param.storeName_.c_str(), seqNum); + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s, seqNum:%{public}u", status, + param.bundleName_.c_str(), param.storeName_.c_str(), option.seqNum); } return status; } -int32_t RdbServiceProxy::DoAsync(const RdbSyncerParam& param, const SyncOption &option, - const RdbPredicates &predicates, const SyncCallback& callback) +int32_t RdbServiceProxy::DoAsync(const RdbSyncerParam& param, const Option &option, + const RdbPredicates &predicates, const AsyncDetail & callback) { - uint32_t num = GetSeqNum(); - if (!syncCallbacks_.Insert(num, callback)) { - ZLOGI("insert callback failed"); - return RDB_ERROR; + Option asyncOption = option; + if (callback == nullptr) { + asyncOption.seqNum = GetSeqNum(); + if (!syncCallbacks_.Insert(asyncOption.seqNum, callback)) { + ZLOGI("insert callback failed"); + return RDB_ERROR; + } } - ZLOGI("num=%{public}u", num); - - if (DoAsync(param, num, option, predicates) != RDB_OK) { + ZLOGI("num=%{public}u", asyncOption.seqNum); + if (DoAsync(param, option, predicates) != RDB_OK) { ZLOGE("failed"); - syncCallbacks_.Erase(num); + syncCallbacks_.Erase(option.seqNum); return RDB_ERROR; } @@ -188,7 +196,7 @@ int32_t RdbServiceProxy::DoAsync(const RdbSyncerParam& param, const SyncOption & return RDB_OK; } -int32_t RdbServiceProxy::SetDistributedTables(const RdbSyncerParam& param, const std::vector &tables) +int32_t RdbServiceProxy::SetDistributedTables(const RdbSyncerParam& param, const std::vector &tables, int32_t type) { MessageParcel reply; int32_t status = IPC_SEND(RDB_SERVICE_CMD_SET_DIST_TABLE, reply, param, tables); @@ -199,13 +207,13 @@ int32_t RdbServiceProxy::SetDistributedTables(const RdbSyncerParam& param, const return status; } -int32_t RdbServiceProxy::Sync(const RdbSyncerParam& param, const SyncOption &option, - const RdbPredicates &predicates, const SyncCallback &callback) +int32_t RdbServiceProxy::Sync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &predicates, + const AsyncDetail &async) { - if (option.isBlock) { - return DoSync(param, option, predicates, callback); + if (option.isAsync) { + return DoSync(param, option, predicates, async); } - return DoAsync(param, option, predicates, callback); + return DoAsync(param, option, predicates, async); } std::string RdbServiceProxy::RemoveSuffix(const std::string& name) @@ -318,32 +326,24 @@ void RdbServiceProxy::ImportObservers(ObserverMap &observers) }); } -int32_t RdbServiceProxy::CreateRDBTable(const RdbSyncerParam ¶m) +int32_t RdbServiceProxy::GetSchema(const RdbSyncerParam ¶m) { - MessageParcel reply; - int32_t status = IPC_SEND(RDB_SERVICE_CREATE_RDB_TABLE, reply, param); - if (status != RDB_OK) { - ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", status, param.bundleName_.c_str(), - param.storeName_.c_str()); + MessageParcel request; + if (!request.WriteInterfaceToken(GetDescriptor())) { + return RDB_ERROR; } - return status; -} - -int32_t RdbServiceProxy::DestroyRDBTable(const RdbSyncerParam ¶m) -{ - MessageParcel reply; - int32_t status = IPC_SEND(RDB_SERVICE_DESTROY_RDB_TABLE, reply, param); - if (status != RDB_OK) { - ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", - status, param.bundleName_.c_str(), param.storeName_.c_str()); + if (!ITypesUtil::Marshal(request, param)) { + return RDB_ERROR; } - return status; -} - -int32_t RdbServiceProxy::GetSchema(const RdbSyncerParam ¶m) -{ MessageParcel reply; - int32_t status = IPC_SEND(RDB_SERVICE_CMD_GET_SCHEMA, reply, param); + MessageOption option; + option.SetFlags(option.TF_ASYNC); + auto result = remote_->SendRequest((RDB_SERVICE_CMD_GET_SCHEMA), request, reply, option); + if (result != 0) { + return RDB_ERROR; + } + int32_t status = RDB_OK; + ITypesUtil::Unmarshal(reply, status); if (status != RDB_OK) { ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", status, param.bundleName_.c_str(), param.storeName_.c_str()); diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp index 2b8b7a56..e661ff0e 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -31,6 +31,7 @@ #include "sqlite_sql_builder.h" #include "sqlite_utils.h" #include "step_result_set.h" +#include "task_executor.h" #ifndef WINDOWS_PLATFORM #include "directory_ex.h" @@ -92,20 +93,22 @@ int RdbStoreImpl::InnerOpen(const RdbStoreConfig &config) syncerParam_.isEncrypt_ = config.IsEncrypt(); syncerParam_.password_ = {}; - std::shared_ptr service = nullptr; - errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); - if (errCode != E_OK) { - LOG_ERROR("GetRdbService failed, err is %{public}d.", errCode); - return E_OK; - } - service->GetSchema(syncerParam_); - if (!config.GetUri().empty()) { - errCode = service->CreateRDBTable(syncerParam_); - if (errCode != E_OK) { - LOG_ERROR("CreateRDBTable failed"); - } else { - isShared_ = true; - } + if (pool_ == nullptr) { + pool_ = TaskExecutor::GetInstance().GetExecutor(); + } + if (pool_ != nullptr) { + auto param = syncerParam_; + pool_->Execute([param]() { + auto [err, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(param); + if (err != E_OK || service == nullptr) { + LOG_ERROR("GetRdbService failed, err is %{public}d.", err); + return; + } + err = service->GetSchema(param); + if (err != E_OK) { + LOG_ERROR("GetSchema failed, err is %{public}d.", err); + } + }); } #endif return E_OK; @@ -120,19 +123,6 @@ RdbStoreImpl::RdbStoreImpl(const RdbStoreConfig &config) RdbStoreImpl::~RdbStoreImpl() { delete connectionPool; -#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) - if (isShared_) { - std::shared_ptr service = nullptr; - int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); - if (errCode != E_OK) { - LOG_ERROR("RdbStoreImpl::~RdbStoreImpl get service failed"); - return; - } - if (service->DestroyRDBTable(syncerParam_) != E_OK) { - LOG_ERROR("RdbStoreImpl::~RdbStoreImpl service DestroyRDBTable failed"); - } - } -#endif } #ifdef WINDOWS_PLATFORM void RdbStoreImpl::Clear() @@ -427,9 +417,9 @@ std::shared_ptr RdbStoreImpl::RemoteQuery(const std::string &device, DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__)); std::vector selectionArgs = predicates.GetWhereArgs(); std::string sql = SqliteSqlBuilder::BuildQueryString(predicates, columns); - std::shared_ptr service = nullptr; - errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); - if (errCode != E_OK) { + auto [err, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_); + errCode = err; + if (err != E_OK) { LOG_ERROR("RdbStoreImpl::RemoteQuery get service failed"); return nullptr; } @@ -1116,7 +1106,7 @@ std::shared_ptr RdbStoreImpl::QueryByStep(const std::string &sql, std } #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) -int RdbStoreImpl::SetDistributedTables(const std::vector &tables) +int RdbStoreImpl::SetDistributedTables(const std::vector &tables, int32_t type) { DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__)); if (tables.empty()) { @@ -1133,13 +1123,11 @@ int RdbStoreImpl::SetDistributedTables(const std::vector &tables) syncerParam_.password_ = std::vector(key.GetData(), key.GetData() + key.GetSize()); } } - - std::shared_ptr service = nullptr; - int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); + auto [errCode, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_); if (errCode != E_OK) { return errCode; } - int32_t errorCode = service->SetDistributedTables(syncerParam_, tables); + int32_t errorCode = service->SetDistributedTables(syncerParam_, tables, type); if (errorCode != E_OK) { LOG_ERROR("Fail to set distributed tables, error=%{public}d", errorCode); syncerParam_.password_.assign(syncerParam_.password_.size(), 0); @@ -1177,16 +1165,45 @@ std::string RdbStoreImpl::ObtainDistributedTableName(const std::string &device, return DistributedDB::RelationalStoreManager::GetDistributedTableName(uuid, table); } -int RdbStoreImpl::Sync(const SyncOption &option, const AbsRdbPredicates &predicate, const SyncCallback &callback) +int RdbStoreImpl::Sync(const SyncOption &option, const AbsRdbPredicates &predicate, const AsyncBrief &callback) +{ + DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__)); + auto [errCode, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_); + if (errCode != E_OK) { + LOG_ERROR("GetRdbService is failed, err is %{public}d.", errCode); + return errCode; + } + DistributedRdb::RdbService::Option rdbOption; + rdbOption.mode = option.mode; + rdbOption.isAsync = option.isBlock; + errCode = + service->Sync(syncerParam_, rdbOption, predicate.GetDistributedPredicates(), [callback](Details &&details) { + Briefs briefs; + for (auto &[key, value] : details) { + briefs.insert_or_assign(key, value.code); + } + callback(briefs); + }); + if (errCode != E_OK) { + LOG_ERROR("Sync is failed, err is %{public}d.", errCode); + return errCode; + } + return E_OK; +} + +int RdbStoreImpl::Sync(const SyncOption &option, const std::vector &tables, + const AsyncDetail &async) { DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__)); - std::shared_ptr service = nullptr; - int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); + auto [errCode, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_); if (errCode != E_OK) { LOG_ERROR("GetRdbService is failed, err is %{public}d.", errCode); return errCode; } - errCode = service->Sync(syncerParam_, option, predicate.GetDistributedPredicates(), callback); + DistributedRdb::RdbService::Option rdbOption; + rdbOption.mode = option.mode; + rdbOption.isAsync = option.isBlock; + errCode = service->Sync(syncerParam_, rdbOption, AbsRdbPredicates(tables).GetDistributedPredicates(), async); if (errCode != E_OK) { LOG_ERROR("Sync is failed, err is %{public}d.", errCode); return errCode; @@ -1196,8 +1213,7 @@ int RdbStoreImpl::Sync(const SyncOption &option, const AbsRdbPredicates &predica int RdbStoreImpl::Subscribe(const SubscribeOption &option, RdbStoreObserver *observer) { - std::shared_ptr service = nullptr; - int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); + auto [errCode, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_); if (errCode != E_OK) { return errCode; } @@ -1207,8 +1223,7 @@ int RdbStoreImpl::Subscribe(const SubscribeOption &option, RdbStoreObserver *obs int RdbStoreImpl::UnSubscribe(const SubscribeOption &option, RdbStoreObserver *observer) { LOG_INFO("enter"); - std::shared_ptr service = nullptr; - int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); + auto [errCode, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_); if (errCode != E_OK) { return errCode; } diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_manager.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_manager.cpp index f495be4b..aba4ee81 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_manager.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_manager.cpp @@ -18,7 +18,7 @@ #include "rdb_store_impl.h" #include "rdb_trace.h" #include "sqlite_global_config.h" -#include "unistd.h" +#include "task_executor.h" #include "rdb_store_manager.h" #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) @@ -28,7 +28,10 @@ namespace OHOS { namespace NativeRdb { -RdbStoreNode::RdbStoreNode(const std::shared_ptr &rdbStore) : rdbStore_(rdbStore), timerId_(0) {} +RdbStoreNode::RdbStoreNode(const std::shared_ptr &rdbStore) + : rdbStore_(rdbStore), taskId_(TaskExecutor::INVALID_TASK_ID) +{ +} RdbStoreNode &RdbStoreNode::operator=(const std::shared_ptr &store) { @@ -49,18 +52,10 @@ RdbStoreManager::~RdbStoreManager() { LOG_ERROR("Start"); Clear(); - if (timer_ != nullptr) { - timer_->Shutdown(true); - timer_ = nullptr; - } } -RdbStoreManager::RdbStoreManager() +RdbStoreManager::RdbStoreManager() : ms_(30000) // 30000 ms { - timer_ = std::make_shared("RdbStoreCloser"); - timer_->Setup(); - // 30000 ms - ms_ = 30000; } void RdbStoreManager::InitSecurityManager(const RdbStoreConfig &config) @@ -84,6 +79,12 @@ std::shared_ptr RdbStoreManager::GetRdbStore(const RdbStoreConfig &con RestartTimer(path, *storeCache_[path]); return rdbStore; } + if (pool_ == nullptr) { + pool_ = TaskExecutor::GetInstance().GetExecutor(); + } + if (pool_ != nullptr) { + pool_->Remove(storeCache_[path]->taskId_); + } storeCache_.erase(path); } InitSecurityManager(config); @@ -113,29 +114,39 @@ std::shared_ptr RdbStoreManager::GetRdbStore(const RdbStoreConfig &con void RdbStoreManager::RestartTimer(const std::string &path, RdbStoreNode &node) { - if (timer_ != nullptr) { - timer_->Unregister(node.timerId_); - // after 30000ms, auto close. - node.timerId_ = timer_->Register(std::bind(&RdbStoreManager::AutoClose, this, path), ms_, true); + if (pool_ == nullptr) { + pool_ = TaskExecutor::GetInstance().GetExecutor(); + } + if (pool_ != nullptr) { + pool_->Remove(node.taskId_); + node.taskId_ = + pool_->Schedule(std::chrono::milliseconds(ms_), std::bind(&RdbStoreManager::AutoClose, this, path)); } } void RdbStoreManager::AutoClose(const std::string &path) { - this->Remove(path); + std::lock_guard lock(mutex_); + auto it = storeCache_.find(path); + if (it == storeCache_.end()) { + LOG_INFO("has Removed"); + return; + } + storeCache_.erase(it); } void RdbStoreManager::Remove(const std::string &path) { std::lock_guard lock(mutex_); - if (storeCache_.find(path) == storeCache_.end()) { + auto it = storeCache_.find(path); + if (it == storeCache_.end()) { LOG_INFO("has Removed"); return; } - if (timer_ != nullptr) { - timer_->Unregister(storeCache_[path]->timerId_); + if (pool_ != nullptr) { + pool_->Remove(it->second->taskId_); } - storeCache_.erase(path); + storeCache_.erase(it); } void RdbStoreManager::Clear() @@ -143,8 +154,8 @@ void RdbStoreManager::Clear() std::lock_guard lock(mutex_); auto iter = storeCache_.begin(); while (iter != storeCache_.end()) { - if (timer_ != nullptr && iter->second != nullptr) { - timer_->Unregister(iter->second->timerId_); + if (iter->second != nullptr && pool_ != nullptr) { + pool_->Remove(iter->second->taskId_); } iter = storeCache_.erase(iter); } diff --git a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp index 907d1223..689631ea 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp @@ -29,29 +29,26 @@ bool Unmarshalling(SyncerParam &output, MessageParcel &data) } template<> -bool Marshalling(const SyncOption &input, MessageParcel &data) +bool Marshalling(const Option &input, MessageParcel &data) { - return ITypesUtil::Marshal(data, static_cast(input.mode), input.isBlock); + return ITypesUtil::Marshal(data, input.mode, input.seqNum, input.isAsync); } template<> -bool Unmarshalling(SyncOption &output, MessageParcel &data) +bool Unmarshalling(Option &output, MessageParcel &data) { - int32_t mode = static_cast(output.mode); - auto ret = ITypesUtil::Unmarshal(data, mode, output.isBlock); - output.mode = static_cast(mode); - return ret; + return ITypesUtil::Unmarshal(data, output.mode, output.seqNum, output.isAsync); } template<> bool Marshalling(const RdbPredicates &input, MessageParcel &data) { - return ITypesUtil::Marshal(data, input.table_, input.devices_, input.operations_); + return ITypesUtil::Marshal(data, input.tables_, input.devices_, input.operations_); } template<> bool Unmarshalling(RdbPredicates &output, MessageParcel &data) { - return ITypesUtil::Unmarshal(data, output.table_, output.devices_, output.operations_); + return ITypesUtil::Unmarshal(data, output.tables_, output.devices_, output.operations_); } template<> @@ -115,4 +112,34 @@ bool Unmarshalling(Asset &output, MessageParcel &data) { return Unmarshal(data, output.version, output.name, output.size, output.modifyTime, output.uri); } +template<> +bool Marshalling(const ProgressDetail &input, MessageParcel &data) +{ + return Marshal(data, input.progress, input.code, input.details); +} +template<> +bool Unmarshalling(ProgressDetail &output, MessageParcel &data) +{ + return Unmarshal(data, output.progress, output.code, output.details); +} +template<> +bool Marshalling(const TableDetail &input, MessageParcel &data) +{ + return Marshal(data, input.upload, input.download); +} +template<> +bool Unmarshalling(TableDetail &output, MessageParcel &data) +{ + return Unmarshal(data, output.upload, output.download); +} +template<> +bool Marshalling(const Statistic &input, MessageParcel &data) +{ + return Marshal(data, input.total, input.success, input.failed, input.untreated); +} +template<> +bool Unmarshalling(Statistic &output, MessageParcel &data) +{ + return Unmarshal(data, output.total, output.success, output.failed, output.untreated); +} } diff --git a/relational_store/frameworks/native/rdb/src/result_set_proxy.cpp b/relational_store/frameworks/native/rdb/src/result_set_proxy.cpp index 1dfdb580..aaeb6c79 100644 --- a/relational_store/frameworks/native/rdb/src/result_set_proxy.cpp +++ b/relational_store/frameworks/native/rdb/src/result_set_proxy.cpp @@ -228,6 +228,11 @@ int ResultSetProxy::GetDouble(int columnIndex, double &value) return E_OK; } +int ResultSetProxy::GetSize(int columnIndex, size_t &size) +{ + return E_NOT_SUPPORT; +} + int ResultSetProxy::IsColumnNull(int columnIndex, bool &isNull) { MessageParcel reply; diff --git a/relational_store/frameworks/native/rdb/src/share_block.cpp b/relational_store/frameworks/native/rdb/src/share_block.cpp index 32b75c3c..c98daf00 100644 --- a/relational_store/frameworks/native/rdb/src/share_block.cpp +++ b/relational_store/frameworks/native/rdb/src/share_block.cpp @@ -13,10 +13,11 @@ * limitations under the License. */ #include "share_block.h" +#include #include #include "logger.h" #include "shared_block_serializer_info.h" - +#include "value_object.h" namespace OHOS { namespace NativeRdb { const int ERROR_STATUS = -1; @@ -99,7 +100,7 @@ int SharedBlockSetColumnNum(AppDataFwk::SharedBlock *sharedBlock, int columnNum) void FillSharedBlockOpt(SharedBlockInfo *info) { - SharedBlockSerializerInfo serializer(info->sharedBlock, info->numColumns, info->startPos); + SharedBlockSerializerInfo serializer(info->sharedBlock, info->statement, info->numColumns, info->startPos); Sqlite3SharedBlockMethods sqliteSharedBlock = { 1, &serializer, info->isCountAllRows, info->startPos, info->requiredPos, SeriAddRow, SeriReset, SeriFinish, SeriPutString, SeriPutLong, SeriPutDouble, SeriPutBlob, SeriPutNull, SeriPutOther @@ -295,9 +296,19 @@ FillOneRowResult FillOneRowOfFloat(AppDataFwk::SharedBlock *sharedBlock, sqlite3 FillOneRowResult FillOneRowOfBlob(AppDataFwk::SharedBlock *sharedBlock, sqlite3_stmt *statement, int startPos, int addedRows, int pos) { + auto action = &AppDataFwk::SharedBlock::PutBlob; + auto *declType = sqlite3_column_decltype(statement, pos); + if (declType != nullptr) { + std::string type(declType); + std::transform(type.begin(), type.end(), type.begin(), [](auto ch) { return std::toupper(ch); }); + action = (type == ValueObject::DeclType()) ? &AppDataFwk::SharedBlock::PutAsset + : (type == ValueObject::DeclType()) ? &AppDataFwk::SharedBlock::PutAssets + : &AppDataFwk::SharedBlock::PutBlob; + } + const void *blob = sqlite3_column_blob(statement, pos); auto size = sqlite3_column_bytes(statement, pos); - int status = sharedBlock->PutBlob(addedRows, pos, blob, size); + int status = (sharedBlock->*action)(addedRows, pos, blob, size); if (status != AppDataFwk::SharedBlock::SHARED_BLOCK_OK) { LOG_ERROR("Failed allocating %{public}d bytes for blob at %{public}d,%{public}d, error=%{public}d", size, startPos + addedRows, pos, status); diff --git a/relational_store/frameworks/native/rdb/src/shared_block_serializer_info.cpp b/relational_store/frameworks/native/rdb/src/shared_block_serializer_info.cpp index 2490ff31..6510db30 100644 --- a/relational_store/frameworks/native/rdb/src/shared_block_serializer_info.cpp +++ b/relational_store/frameworks/native/rdb/src/shared_block_serializer_info.cpp @@ -14,13 +14,17 @@ */ #include "shared_block_serializer_info.h" + +#include "value_object.h" + #include "logger.h" namespace OHOS { namespace NativeRdb { -SharedBlockSerializerInfo::SharedBlockSerializerInfo(AppDataFwk::SharedBlock *sharedBlock, int numColumns, int startPos) - :sharedBlock_(sharedBlock), anumColumns(numColumns), atotalRows(0), astartPos(startPos), raddedRows(0), - risFull(false) +SharedBlockSerializerInfo::SharedBlockSerializerInfo(AppDataFwk::SharedBlock *sharedBlock, sqlite3_stmt *stat, + int numColumns, int startPos) + : sharedBlock_(sharedBlock), statement_(stat), anumColumns(numColumns), atotalRows(0), astartPos(startPos), + raddedRows(0), risFull(false) { } @@ -101,7 +105,17 @@ int SharedBlockSerializerInfo::PutDouble(int row, int column, double value) int SharedBlockSerializerInfo::PutBlob(int row, int column, const void *blob, int len) { - int status = sharedBlock_->PutBlob(row, column, blob, len); + auto action = &AppDataFwk::SharedBlock::PutBlob; + auto *declType = sqlite3_column_decltype(statement_, column); + if (declType != nullptr) { + std::string type(declType); + std::transform(type.begin(), type.end(), type.begin(), [](auto ch) { return std::toupper(ch); }); + action = (type == ValueObject::DeclType()) ? &AppDataFwk::SharedBlock::PutAsset + : (type == ValueObject::DeclType()) ? &AppDataFwk::SharedBlock::PutAssets + : &AppDataFwk::SharedBlock::PutBlob; + } + + int status = (sharedBlock_->*action)(row, column, blob, len); if (status != AppDataFwk::SharedBlock::SHARED_BLOCK_OK) { sharedBlock_->FreeLastRow(); risFull = true; diff --git a/relational_store/frameworks/native/rdb/src/sqlite_statement.cpp b/relational_store/frameworks/native/rdb/src/sqlite_statement.cpp index 9de5fd20..d2423227 100644 --- a/relational_store/frameworks/native/rdb/src/sqlite_statement.cpp +++ b/relational_store/frameworks/native/rdb/src/sqlite_statement.cpp @@ -436,7 +436,7 @@ int SqliteStatement::GetColumn(int index, ValueObject &value) const value = std::move(asset); return E_OK; } - if (declType == ValueObject::DeclType()) { + if (declType == ValueObject::DeclType()) { Assets assets; RawDataParser::ParserRawData(blob, size, assets); value = std::move(assets); @@ -451,6 +451,26 @@ int SqliteStatement::GetColumn(int index, ValueObject &value) const return E_OK; } +int SqliteStatement::GetSize(int index, size_t &size) const +{ + size = 0; + if (stmtHandle == nullptr) { + return E_INVALID_STATEMENT; + } + + if (index >= columnCount) { + return E_INVALID_COLUMN_INDEX; + } + + int type = sqlite3_column_type(stmtHandle, index); + if (type == SQLITE_BLOB || type == SQLITE_TEXT || type == SQLITE_NULL) { + size = sqlite3_column_bytes(stmtHandle, index); + return E_OK; + } + + return E_INVALID_COLUMN_TYPE; +} + bool SqliteStatement::IsReadOnly() const { return readOnly; diff --git a/relational_store/frameworks/native/rdb/src/sqlite_utils.cpp b/relational_store/frameworks/native/rdb/src/sqlite_utils.cpp index 25c5e6e3..80c13e8b 100644 --- a/relational_store/frameworks/native/rdb/src/sqlite_utils.cpp +++ b/relational_store/frameworks/native/rdb/src/sqlite_utils.cpp @@ -92,7 +92,7 @@ std::string SqliteUtils::StrToUpper(std::string s) bool SqliteUtils::IsSqlReadOnly(int sqlType) { - return (sqlType == STATEMENT_SELECT) ? true : false; + return (sqlType == STATEMENT_SELECT); } bool SqliteUtils::IsSpecial(int sqlType) diff --git a/relational_store/frameworks/native/rdb/src/step_result_set.cpp b/relational_store/frameworks/native/rdb/src/step_result_set.cpp index 31bc768e..c9f13cce 100644 --- a/relational_store/frameworks/native/rdb/src/step_result_set.cpp +++ b/relational_store/frameworks/native/rdb/src/step_result_set.cpp @@ -439,6 +439,16 @@ int StepResultSet::GetModifyTime(std::string &modifyTime) return E_OK; } +int StepResultSet::GetSize(int columnIndex, size_t &size) +{ + if (rowPos_ == INIT_POS) { + size = 0; + return E_STEP_RESULT_QUERY_NOT_EXECUTED; + } + + return sqliteStatement->GetSize(columnIndex, size); +} + int StepResultSet::IsColumnNull(int columnIndex, bool &isNull) { ColumnType columnType; diff --git a/relational_store/frameworks/native/rdb/src/task_executor.cpp b/relational_store/frameworks/native/rdb/src/task_executor.cpp new file mode 100644 index 00000000..888169cf --- /dev/null +++ b/relational_store/frameworks/native/rdb/src/task_executor.cpp @@ -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. +*/ + +#include "task_executor.h" + +namespace OHOS::NativeRdb { +TaskExecutor::TaskExecutor() +{ + pool_ = std::make_shared(MAX_THREADS, MIN_THREADS); +} + +TaskExecutor::~TaskExecutor() +{ + pool_ = nullptr; +} + +TaskExecutor &TaskExecutor::GetInstance() +{ + static TaskExecutor instance; + return instance; +} + +std::shared_ptr TaskExecutor::GetExecutor() +{ + std::shared_lock lock(rwMutex_); + return pool_; +} + +void TaskExecutor::SetExecutor(std::shared_ptr executor) +{ + std::unique_lock lock(rwMutex_); + pool_ = executor; +}; + +} // namespace OHOS::NativeRdb diff --git a/relational_store/frameworks/native/rdb/src/values_bucket.cpp b/relational_store/frameworks/native/rdb/src/values_bucket.cpp index 5f727e4d..c2e43381 100644 --- a/relational_store/frameworks/native/rdb/src/values_bucket.cpp +++ b/relational_store/frameworks/native/rdb/src/values_bucket.cpp @@ -149,15 +149,15 @@ bool ValuesBucket::Marshalling(Parcel &parcel) const return ITypesUtil::Marshal(*data, values_); } -ValuesBucket *ValuesBucket::Unmarshalling(Parcel &parcel) +ValuesBucket ValuesBucket::Unmarshalling(Parcel &parcel) { MessageParcel *data = static_cast(&parcel); if (data == nullptr) { - return nullptr; + return {}; } ValuesBucket bucket; ITypesUtil::Unmarshal(*data, bucket.values_); - return new (std::nothrow) ValuesBucket(std::move(bucket)); + return bucket; } #endif } // namespace NativeRdb diff --git a/relational_store/interfaces/inner_api/appdatafwk/BUILD.gn b/relational_store/interfaces/inner_api/appdatafwk/BUILD.gn index a1164865..17cc8ce7 100644 --- a/relational_store/interfaces/inner_api/appdatafwk/BUILD.gn +++ b/relational_store/interfaces/inner_api/appdatafwk/BUILD.gn @@ -38,9 +38,7 @@ ohos_shared_library("native_appdatafwk") { subsystem_name = "distributeddatamgr" part_name = "relational_store" - public_deps = [ - "//foundation/communication/ipc/interfaces/innerkits/ipc_single:ipc_single", - ] + public_deps = [ "${ipc_path}/ipc_single:ipc_single" ] external_deps = [ "c_utils:utils", diff --git a/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h b/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h index 13b0fa66..84e854af 100644 --- a/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h +++ b/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h @@ -52,7 +52,8 @@ public: CLOUD_DISABLE_SWITCH, IPC_ERROR, IPC_PARCEL_ERROR, - PERMISSION_DENIED + PERMISSION_DENIED, + CLOUD_CONFIG_PERMISSION_DENIED }; virtual ~CloudService() = default; diff --git a/relational_store/interfaces/inner_api/rdb/BUILD.gn b/relational_store/interfaces/inner_api/rdb/BUILD.gn index cbd3f6fe..2c6f0e72 100644 --- a/relational_store/interfaces/inner_api/rdb/BUILD.gn +++ b/relational_store/interfaces/inner_api/rdb/BUILD.gn @@ -118,7 +118,6 @@ if (is_ohos) { [ "${relational_store_innerapi_path}/appdatafwk:native_appdatafwk" ] external_deps = [ - "access_token:libaccesstoken_sdk", "c_utils:utils", "device_manager:devicemanagersdk", "hilog_native:libhilog", @@ -141,8 +140,7 @@ if (is_ohos) { deps = base_deps deps += [ - "//base/security/huks/frameworks/huks_standard/main/os_dependency:libhuks_os_dependency_standard_static", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb:distributeddb", + "${kvstore_path}/libs/distributeddb:distributeddb", "//third_party/sqlite:sqlite", ] ldflags = [ "-Wl,--exclude-libs,ALL" ] @@ -166,7 +164,6 @@ if (is_ohos) { [ "${relational_store_innerapi_path}/appdatafwk:native_appdatafwk" ] external_deps = [ - "access_token:libaccesstoken_sdk", "c_utils:utils", "device_manager:devicemanagersdk", "hilog_native:libhilog", @@ -396,10 +393,7 @@ if (is_ohos) { ] } - base_deps = [ - "//third_party/icu/icu4c:shared_icui18n", - "//third_party/icu/icu4c:shared_icuuc", - ] + base_deps = [ "//third_party/icu/icu4c:shared_icuuc" ] ohos_source_set("native_rdb") { defines = [ "ANDROID_PLATFORM" ] @@ -413,9 +407,9 @@ if (is_ohos) { sources += [ "${relational_store_native_path}/rdb/mock/src/timer.cpp" ] deps += [ - "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_android", "//commonlibrary/c_utils/base:utils", - "//third_party/sqlite:sqlite", + "//third_party/sqlite:sqlite_static", ] cflags_cc = [ "-std=c++17" ] diff --git a/relational_store/interfaces/inner_api/rdb/include/abs_rdb_predicates.h b/relational_store/interfaces/inner_api/rdb/include/abs_rdb_predicates.h index 0c5b9d9f..065cafb8 100644 --- a/relational_store/interfaces/inner_api/rdb/include/abs_rdb_predicates.h +++ b/relational_store/interfaces/inner_api/rdb/include/abs_rdb_predicates.h @@ -35,6 +35,15 @@ public: */ API_EXPORT explicit AbsRdbPredicates(std::string tableName); + /** + * @brief Constructor. + * + * A parameterized constructor used to create an AbsRdbPredicates instance. + * + * @param tableName Indicates the table name of the database. + */ + API_EXPORT explicit AbsRdbPredicates(std::vector tables); + /** * @brief Destructor. */ @@ -148,7 +157,7 @@ public: * @brief Sets the join types in the predicates. The value can be {@code INNER JOIN}, {@code LEFT OUTER JOIN}, * and {@code CROSS JOIN}. */ - API_EXPORT virtual void SetJoinTypes(const std::vector joinTypes); + API_EXPORT virtual void SetJoinTypes(std::vector joinTypes); /** * @brief Obtains the database table names of the joins in the predicates. @@ -158,7 +167,7 @@ public: /** * @brief Sets the database table names of the joins in the predicates. */ - API_EXPORT virtual void SetJoinTableNames(const std::vector joinTableNames); + API_EXPORT virtual void SetJoinTableNames(std::vector joinTableNames); /** * @brief Obtains the join conditions in the predicates. @@ -168,7 +177,7 @@ public: /** * @brief Sets the join conditions required in the predicates. */ - API_EXPORT virtual void SetJoinConditions(const std::vector joinConditions); + API_EXPORT virtual void SetJoinConditions(std::vector joinConditions); /** * @brief Obtains the join clause in the predicates. @@ -192,7 +201,7 @@ protected: int joinCount = 0; private: - std::string tableName; + std::string tableName_; mutable DistributedRdb::RdbPredicates predicates_; }; } // namespace OHOS::NativeRdb diff --git a/relational_store/interfaces/inner_api/rdb/include/abs_result_set.h b/relational_store/interfaces/inner_api/rdb/include/abs_result_set.h index b4e8e059..72579418 100644 --- a/relational_store/interfaces/inner_api/rdb/include/abs_result_set.h +++ b/relational_store/interfaces/inner_api/rdb/include/abs_result_set.h @@ -116,7 +116,7 @@ public: * @brief Obtains the value of the specified column in the current row as asset. * * The implementation class determines whether to throw an exception if the value of the specified column - * in the current row is null or the specified column is not of the double type. + * in the current row is null or the specified column is not of the Asset type. * * @param columnIndex Indicates the specified column index, which starts from 0. * @@ -128,7 +128,7 @@ public: * @brief Obtains the value of the specified column in the current row as assets. * * The implementation class determines whether to throw an exception if the value of the specified column - * in the current row is null or the specified column is not of the double type. + * in the current row is null or the specified column is not of the Assets type. * * @param columnIndex Indicates the specified column index, which starts from 0. * diff --git a/relational_store/interfaces/inner_api/rdb/include/abs_shared_result_set.h b/relational_store/interfaces/inner_api/rdb/include/abs_shared_result_set.h index ff4c1ba1..1764e415 100644 --- a/relational_store/interfaces/inner_api/rdb/include/abs_shared_result_set.h +++ b/relational_store/interfaces/inner_api/rdb/include/abs_shared_result_set.h @@ -22,7 +22,6 @@ #include #include "abs_result_set.h" -#include "shared_block.h" #include "shared_result_set.h" namespace OHOS { @@ -136,6 +135,13 @@ public: */ API_EXPORT int GetAssets(int32_t col, ValueObject::Assets &value) override; + /** + * @brief Get the size of blob or text. + * + * @param columnIndex Indicates the zero-based index of the target column. + */ + API_EXPORT int GetSize(int columnIndex, size_t &size) override; + /** * @brief Checks whether the value of the specified column in the current row is null. * diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_errno.h b/relational_store/interfaces/inner_api/rdb/include/rdb_errno.h index 113dd598..b96c8b5c 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_errno.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_errno.h @@ -15,7 +15,6 @@ #ifndef NATIVE_RDB_RDB_ERRNO_H #define NATIVE_RDB_RDB_ERRNO_H - namespace OHOS { namespace NativeRdb { @@ -39,6 +38,11 @@ constexpr int E_NOT_SUPPORTED = 801; */ constexpr int E_ERROR = E_BASE; +/** +* @brief The error code for common invalid args. +*/ +constexpr int E_INVALID_ARGS = (E_BASE + 1); + /** * @brief The error code for upgrade the read-only store. */ @@ -268,6 +272,8 @@ static constexpr int E_WAL_SIZE_OVER_LIMIT = (E_BASE + 47); * @brief The error when the connection count is used up. */ static constexpr int E_CON_OVER_LIMIT = (E_BASE + 48); + +constexpr int E_INVALID_BUNDLE_NAME = (E_BASE + 49); } // namespace NativeRdb } // namespace OHOS diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_notifier.h b/relational_store/interfaces/inner_api/rdb/include/rdb_notifier.h index 49b2ab1c..fd87f44b 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_notifier.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_notifier.h @@ -15,19 +15,24 @@ #ifndef DISTRIBUTED_RDB_RDB_NOTIFIER_H #define DISTRIBUTED_RDB_RDB_NOTIFIER_H +#include #include "rdb_types.h" namespace OHOS::DistributedRdb { class IRdbNotifier { public: - enum { + using ChangeInfo = RdbStoreObserver::ChangeInfo; + using PrimaryFields = std::map; + enum Code : int32_t { RDB_NOTIFIER_CMD_SYNC_COMPLETE, RDB_NOTIFIER_CMD_DATA_CHANGE, + RDB_NOTIFIER_CMD_DATA_DETAILS, RDB_NOTIFIER_CMD_MAX }; + virtual int32_t OnComplete(uint32_t seqNum, Details &&result) = 0; - virtual int32_t OnComplete(uint32_t seqNum, const SyncResult& result) = 0; + virtual int32_t OnChange(const std::string &storeName, const std::vector &devices) = 0; - virtual int32_t OnChange(const std::string& storeName, const std::vector& devices) = 0; + virtual int32_t OnChange(const Origin &origin, const PrimaryFields &primaries, ChangeInfo &&changeInfo) = 0; }; } // namespace OHOS::DistributedRdb #endif diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_service.h b/relational_store/interfaces/inner_api/rdb/include/rdb_service.h index 4d9ec559..91dde3bf 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_service.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_service.h @@ -18,11 +18,13 @@ #include #include -#include "iremote_object.h" #include "rdb_types.h" #include "rdb_notifier.h" namespace OHOS { +template +class sptr; +class IRemoteObject; namespace DistributedRdb { class RdbService { public: @@ -35,17 +37,23 @@ public: RDB_SERVICE_CMD_SUBSCRIBE, RDB_SERVICE_CMD_UNSUBSCRIBE, RDB_SERVICE_CMD_REMOTE_QUERY, - RDB_SERVICE_CREATE_RDB_TABLE, - RDB_SERVICE_DESTROY_RDB_TABLE, RDB_SERVICE_CMD_GET_SCHEMA, RDB_SERVICE_CMD_MAX }; + + struct Option { + int32_t mode; + uint32_t seqNum = 0; + bool isAsync = false; + }; + virtual std::string ObtainDistributedTableName(const std::string &device, const std::string &table) = 0; - virtual int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables) = 0; + virtual int32_t SetDistributedTables( + const RdbSyncerParam ¶m, const std::vector &tables, int32_t type = DISTRIBUTED_DEVICE) = 0; - virtual int32_t Sync(const RdbSyncerParam ¶m, const SyncOption &option, const RdbPredicates &predicates, - const SyncCallback &callback) = 0; + virtual int32_t Sync(const RdbSyncerParam ¶m, const Option &option, const RdbPredicates &predicates, + const AsyncDetail &async) = 0; virtual int32_t Subscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, RdbStoreObserver *observer) = 0; @@ -56,25 +64,11 @@ public: virtual int32_t RemoteQuery(const RdbSyncerParam ¶m, const std::string &device, const std::string &sql, const std::vector &selectionArgs, sptr &resultSet) = 0; - virtual int32_t CreateRDBTable(const RdbSyncerParam ¶m) = 0; - - virtual int32_t DestroyRDBTable(const RdbSyncerParam ¶m) = 0; - - virtual int32_t InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) = 0; + virtual int32_t InitNotifier(const RdbSyncerParam ¶m, sptr notifier) = 0; virtual int32_t GetSchema(const RdbSyncerParam ¶m) = 0; inline static constexpr const char *SERVICE_NAME = "relational_store"; -protected: - virtual int32_t DoSync(const RdbSyncerParam ¶m, const SyncOption &option, const RdbPredicates &predicates, - SyncResult &result) = 0; - - virtual int32_t DoAsync(const RdbSyncerParam ¶m, uint32_t seqNum, const SyncOption &option, - const RdbPredicates &predicates) = 0; - - virtual int32_t DoSubscribe(const RdbSyncerParam ¶m, const SubscribeOption &option) = 0; - - virtual int32_t DoUnSubscribe(const RdbSyncerParam ¶m) = 0; }; } } // namespace OHOS::DistributedRdb diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_store.h b/relational_store/interfaces/inner_api/rdb/include/rdb_store.h index a072f0ce..b5cc67df 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_store.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_store.h @@ -38,9 +38,17 @@ public: using SyncOption = DistributedRdb::SyncOption; /** - * @brief Use SyncCallback replace DistributedRdb::SyncCallback namespace. + * @brief Use AsyncBrief replace DistributedRdb::AsyncBrief namespace. */ - using SyncCallback = DistributedRdb::SyncCallback; + using Briefs = DistributedRdb::Briefs; + using AsyncBrief = DistributedRdb::AsyncBrief; + using SyncCallback = AsyncBrief; + + /** + * @brief Use AsyncBrief replace DistributedRdb::AsyncBrief namespace. + */ + using Details = DistributedRdb::Details; + using AsyncDetail = DistributedRdb::AsyncDetail; /** * @brief Use SubscribeMode replace DistributedRdb::SubscribeMode namespace. @@ -377,7 +385,8 @@ public: * * @param tables Indicates the tables name you want to set. */ - virtual int SetDistributedTables(const std::vector& tables) = 0; + virtual int SetDistributedTables(const std::vector &tables, + int32_t type = DistributedRdb::DistributedType::DISTRIBUTED_DEVICE) = 0; /** * @brief Obtain distributed table name of specified remote device according to local table name. @@ -391,12 +400,20 @@ public: const std::string &device, const std::string &table, int &errCode) = 0; /** - * @brief Sync data between devices. + * @brief Sync data between devices or cloud. + * + * @param device Indicates the remote device. + * @param predicate Indicates the AbsRdbPredicates {@link AbsRdbPredicates} object. + */ + virtual int Sync(const SyncOption& option, const AbsRdbPredicates& predicate, const AsyncBrief& async) = 0; + + /** + * @brief Sync data between devices or cloud. * * @param device Indicates the remote device. * @param predicate Indicates the AbsRdbPredicates {@link AbsRdbPredicates} object. */ - virtual int Sync(const SyncOption& option, const AbsRdbPredicates& predicate, const SyncCallback& callback) = 0; + virtual int Sync(const SyncOption& option, const std::vector& tables, const AsyncDetail& async) = 0; /** * @brief Subscribe to event changes. diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h b/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h index 0a5e036a..3b3be08d 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h @@ -18,7 +18,7 @@ #include #include -#include +#include "rdb_types.h" #include "rdb_visibility.h" namespace OHOS::NativeRdb { diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_types.h b/relational_store/interfaces/inner_api/rdb/include/rdb_types.h index 37111d40..3930ee73 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_types.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_types.h @@ -16,9 +16,12 @@ #ifndef DISTRIBUTED_RDB_RDB_TYPES_H #define DISTRIBUTED_RDB_RDB_TYPES_H +#include #include #include +#include #include +#include #include namespace OHOS::DistributedRdb { @@ -51,6 +54,7 @@ struct RdbSyncerParam { enum SyncMode { PUSH, PULL, + PULL_PUSH, TIME_FIRST, NATIVE_FIRST, CLOUD_FIRST, @@ -61,8 +65,41 @@ struct SyncOption { bool isBlock; }; -using SyncResult = std::map; // networkId +enum DistributedType{ + DISTRIBUTED_DEVICE = 0, + DISTRIBUTED_CLOUD +}; + +enum Progress { + SYNC_BEGIN, + SYNC_IN_PROGRESS, + SYNC_FINISH, +}; + +struct Statistic { + int32_t total; + int32_t success; + int32_t failed; + int32_t untreated; +}; + +struct TableDetail { + Statistic upload; + Statistic download; +}; + +struct ProgressDetail { + int32_t progress; + int32_t code; + std::map details; +}; + +using SyncResult = std::map; using SyncCallback = std::function; +using Briefs = std::map; +using Details = std::map; +using AsyncBrief = std::function; +using AsyncDetail = std::function; enum RdbPredicateOperator { EQUAL_TO, @@ -92,7 +129,7 @@ struct RdbPredicates { operations_.push_back({ op, field, values }); } - std::string table_; + std::vector tables_; std::vector devices_; std::vector operations_; }; @@ -108,9 +145,37 @@ struct SubscribeOption { SubscribeMode mode; }; +struct Origin { + enum OriginType : int32_t { + ORIGIN_LOCAL, + ORIGIN_NEARBY, + ORIGIN_CLOUD, + ORIGIN_ALL, + ORIGIN_BUTT, + }; + int32_t origin = ORIGIN_ALL; + // origin is ORIGIN_LOCAL, the id is empty + // origin is ORIGIN_NEARBY, the id is networkId; + // origin is ORIGIN_CLOUD, the id is the cloud account id + std::vector id; + std::string store; +}; + class RdbStoreObserver { public: + enum ChangeType : int32_t { + CHG_TYPE_INSERT = 0, + CHG_TYPE_UPDATE, + CHG_TYPE_DELETE, + CHG_TYPE_BUTT + }; + using PrimaryKey = std::variant; + using ChangeInfo = std::map[CHG_TYPE_BUTT]>; virtual void OnChange(const std::vector& devices) = 0; // networkid + virtual void OnChange(const Origin &origin, ChangeInfo &&changeInfo) + { + OnChange(origin.id); + }; }; struct DropOption { diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_visibility.h b/relational_store/interfaces/inner_api/rdb/include/rdb_visibility.h index ab7ed7b1..a62ec58d 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_visibility.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_visibility.h @@ -15,9 +15,13 @@ #ifndef NATIVE_RDB_RDB_VISIBILITY_H #define NATIVE_RDB_RDB_VISIBILITY_H - #ifndef API_EXPORT +#if defined(_MSC_VER) || defined(__MINGW32__) +#define API_EXPORT __declspec(dllexport) +#elif defined(__GNUC__) || defined(__clang__) #define API_EXPORT __attribute__((visibility ("default"))) +#else +#define API_EXPORT +#endif #endif - #endif // NATIVE_RDB_RDB_VISIBILITY_H diff --git a/relational_store/interfaces/inner_api/rdb/include/result_set.h b/relational_store/interfaces/inner_api/rdb/include/result_set.h index 37be18c6..514da296 100644 --- a/relational_store/interfaces/inner_api/rdb/include/result_set.h +++ b/relational_store/interfaces/inner_api/rdb/include/result_set.h @@ -57,6 +57,13 @@ public: */ virtual int GetRow(RowEntity &rowEntity) = 0; virtual int GetModifyTime(std::string &modifyTime) = 0; + + /** + * @brief Get the size of blob or text. + * + * @param columnIndex Indicates the zero-based index of the target column. + */ + API_EXPORT virtual int GetSize(int columnIndex, size_t &size) = 0; }; } // namespace NativeRdb diff --git a/relational_store/interfaces/inner_api/rdb/include/shared_result_set.h b/relational_store/interfaces/inner_api/rdb/include/shared_result_set.h index 5d6409ae..bddf1e95 100644 --- a/relational_store/interfaces/inner_api/rdb/include/shared_result_set.h +++ b/relational_store/interfaces/inner_api/rdb/include/shared_result_set.h @@ -17,10 +17,12 @@ #define NATIVE_RDB_SHARED_RESULT_SET_H #include -#include "shared_block.h" #include "rdb_visibility.h" namespace OHOS { +namespace AppDataFwk { +class SharedBlock; +} namespace NativeRdb { /** * The SharedResultSet class of RDB. diff --git a/relational_store/interfaces/inner_api/rdb/include/values_bucket.h b/relational_store/interfaces/inner_api/rdb/include/values_bucket.h index c4191c6a..1990f179 100644 --- a/relational_store/interfaces/inner_api/rdb/include/values_bucket.h +++ b/relational_store/interfaces/inner_api/rdb/include/values_bucket.h @@ -18,14 +18,14 @@ #include #include -#include "parcel.h" #include "value_object.h" namespace OHOS { +class Parcel; namespace NativeRdb { /** * The ValuesBucket class of RDB. */ -class API_EXPORT ValuesBucket : public virtual Parcelable { +class API_EXPORT ValuesBucket { public: /** * @brief Constructor. @@ -157,9 +157,15 @@ public: */ API_EXPORT void GetAll(std::map &output) const; - API_EXPORT bool Marshalling(Parcel &parcel) const override; + /** + * @brief set a ValuesBucket object to parcel. + */ + API_EXPORT bool Marshalling(Parcel &parcel) const; - API_EXPORT static ValuesBucket *Unmarshalling(Parcel &parcel); + /** + * @brief Obtains a ValuesBucket object from parcel. + */ + API_EXPORT static ValuesBucket Unmarshalling(Parcel &parcel); std::map values_; }; diff --git a/relational_store/interfaces/inner_api/rdb/mock/include/result_set.h b/relational_store/interfaces/inner_api/rdb/mock/include/result_set.h index 52737368..c0e3511f 100644 --- a/relational_store/interfaces/inner_api/rdb/mock/include/result_set.h +++ b/relational_store/interfaces/inner_api/rdb/mock/include/result_set.h @@ -38,7 +38,6 @@ private: std::vector indexs_; }; - class ResultSet : public RemoteResultSet { public: virtual ~ResultSet() {} diff --git a/relational_store/interfaces/inner_api/rdb_data_ability_adapter/BUILD.gn b/relational_store/interfaces/inner_api/rdb_data_ability_adapter/BUILD.gn index d05e8559..9e24b566 100644 --- a/relational_store/interfaces/inner_api/rdb_data_ability_adapter/BUILD.gn +++ b/relational_store/interfaces/inner_api/rdb_data_ability_adapter/BUILD.gn @@ -22,7 +22,6 @@ config("rdb_data_ability_adapter_config") { "include", "${datashare_path}/common/include/", "${relational_store_innerapi_path}/rdb/include", - "${datashare_common_native_path}/include", "${common_tool_path}/", ] } diff --git a/relational_store/interfaces/ndk/BUILD.gn b/relational_store/interfaces/ndk/BUILD.gn new file mode 100644 index 00000000..dec9eb86 --- /dev/null +++ b/relational_store/interfaces/ndk/BUILD.gn @@ -0,0 +1,59 @@ +# 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") +import("//build/ohos/ndk/ndk.gni") +import("//foundation/distributeddatamgr/relational_store/relational_store.gni") + +ohos_ndk_headers("native_rdb_ndk_header") { + dest_dir = "$ndk_headers_out_dir/native_rdb" + sources = [ + "./include/ndk_logger.h", + "./include/relational_cursor.h", + "./include/relational_error_code.h", + "./include/relational_predicates.h", + "./include/relational_store.h", + "./include/relational_value_object.h", + "./include/relational_values_bucket.h", + ] +} + +ohos_ndk_library("libnative_rdb_ndk") { + output_name = "native_rdb_ndk" + output_extension = "so" + ndk_description_file = "./libnative_rdb.ndk.json" + system_capability = + "SystemCapability.DistributedDataManager.RelationalStore.Core" +} + +ohos_shared_library("native_rdb_ndk") { + include_dirs = [ "./include" ] + sources = [ + "src/relational_cursor.cpp", + "src/relational_predicates.cpp", + "src/relational_store.cpp", + "src/relational_value_object.cpp", + "src/relational_values_bucket.cpp", + ] + + defines = [ "API_EXPORT=__attribute__((visibility (\"default\")))" ] + + external_deps = [ + "c_utils:utils", + "hilog_native:libhilog", + "relational_store:native_rdb", + ] + + part_name = "relational_store" + subsystem_name = "distributeddatamgr" +} diff --git a/relational_store/interfaces/ndk/include/ndk_logger.h b/relational_store/interfaces/ndk/include/ndk_logger.h new file mode 100644 index 00000000..334bf939 --- /dev/null +++ b/relational_store/interfaces/ndk/include/ndk_logger.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 RDB_NDK_LOGGER_H +#define RDB_NDK_LOGGER_H + +#include +#include + +#include "hilog/log.h" + +namespace OHOS { +namespace RdbNdk { +static const OHOS::HiviewDFX::HiLogLabel RDB_NDK_LABEL = { LOG_CORE, 0xD001656, "RdbNdk" }; +#define LOG_DEBUG(...) ((void)OHOS::HiviewDFX::HiLog::Debug(RDB_NDK_LABEL, __VA_ARGS__)) +#define LOG_INFO(...) ((void)OHOS::HiviewDFX::HiLog::Info(RDB_NDK_LABEL, __VA_ARGS__)) +#define LOG_WARN(...) ((void)OHOS::HiviewDFX::HiLog::Warn(RDB_NDK_LABEL, __VA_ARGS__)) +#define LOG_ERROR(...) ((void)OHOS::HiviewDFX::HiLog::Error(RDB_NDK_LABEL, __VA_ARGS__)) +#define LOG_FATAL(...) ((void)OHOS::HiviewDFX::HiLog::Fatal(RDB_NDK_LABEL, __VA_ARGS__)) +} // namespace RdbNdk +} // namespace OHOS + +#endif diff --git a/relational_store/interfaces/ndk/include/relational_cursor.h b/relational_store/interfaces/ndk/include/relational_cursor.h new file mode 100644 index 00000000..79c1cfd8 --- /dev/null +++ b/relational_store/interfaces/ndk/include/relational_cursor.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 RELATIONAL_CURSOR_H +#define RELATIONAL_CURSOR_H + +#define BOOL int +#define TRUE 1 +#define FALSE 0 + +#include +#include +#ifdef __cplusplus +extern "C" { +#endif + +enum OH_Rdb_ColumnType { + TYPE_NULL = 0, + TYPE_INT64, + TYPE_REAL, + TYPE_TEXT, + TYPE_BLOB, +}; + +typedef struct OH_Cursor { + int64_t id; + int (*OH_Cursor_GetColumnCount)(OH_Cursor *, int *); + int (*OH_Cursor_GetColumnType)(OH_Cursor *, int32_t, OH_Rdb_ColumnType *); + int (*OH_Cursor_GetColumnIndex)(OH_Cursor *, const char *, int *); + int (*OH_Cursor_GetColumnName)(OH_Cursor *, int32_t, char *, int); + int (*OH_Cursor_GetRowCount)(OH_Cursor *, int *); + int (*OH_Cursor_GoToNextRow)(OH_Cursor *); + int (*OH_Cursor_GetSize)(OH_Cursor *, int32_t, size_t *); + int (*OH_Cursor_GetText)(OH_Cursor *, int32_t, char *, int); + int (*OH_Cursor_GetInt64)(OH_Cursor *, int32_t, int64_t *); + int (*OH_Cursor_GetReal)(OH_Cursor *, int32_t, double *); + int (*OH_Cursor_GetBlob)(OH_Cursor *, int32_t, unsigned char *, int); + int (*OH_Cursor_IsNull)(OH_Cursor *, int32_t, BOOL *); + int (*OH_Cursor_Close)(OH_Cursor *); +} OH_Cursor; + +#ifdef __cplusplus +}; +#endif + +#endif // RELATIONAL_CURSOR_H diff --git a/relational_store/interfaces/ndk/include/relational_error_code.h b/relational_store/interfaces/ndk/include/relational_error_code.h new file mode 100644 index 00000000..8f0f0f8f --- /dev/null +++ b/relational_store/interfaces/ndk/include/relational_error_code.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 RELATIONAL_ERRNO_CODE_H +#define RELATIONAL_ERRNO_CODE_H + +#ifdef __cplusplus +extern "C" { +#endif + +enum OH_Rdb_ErrCode { + RDB_ERR_INVALID_ARGS = -2, + RDB_ERR = -1, + RDB_ERR_OK = 0 +}; + +#ifdef __cplusplus +}; +#endif + +#endif // RELATIONAL_ERRNO_CODE_H \ No newline at end of file diff --git a/relational_store/interfaces/ndk/include/relational_predicates.h b/relational_store/interfaces/ndk/include/relational_predicates.h new file mode 100644 index 00000000..c776d29a --- /dev/null +++ b/relational_store/interfaces/ndk/include/relational_predicates.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 RELATIONAL_PREDICATES_H +#define RELATIONAL_PREDICATES_H + +#include +#include +#include "relational_value_object.h" + +#ifdef __cplusplus +extern "C" { +#endif + +enum OH_Rdb_OrderType { + ASC = 0, + DESC = 1, +}; + +typedef struct OH_Predicates { + int64_t id; + OH_Predicates (*OH_Predicates_EqualTo)(OH_Predicates *, const char *, OH_Rdb_VObject *); + OH_Predicates (*OH_Predicates_NotEqualTo)(OH_Predicates *, const char *, OH_Rdb_VObject *); + OH_Predicates (*OH_Predicates_BeginWrap)(OH_Predicates *); + OH_Predicates (*OH_Predicates_EndWrap)(OH_Predicates *); + OH_Predicates (*OH_Predicates_Or)(OH_Predicates *); + OH_Predicates (*OH_Predicates_And)(OH_Predicates *); + OH_Predicates (*OH_Predicates_IsNull)(OH_Predicates *, const char *); + OH_Predicates (*OH_Predicates_IsNotNull)(OH_Predicates *, const char *); + OH_Predicates (*OH_Predicates_Like)(OH_Predicates *, const char *, OH_Rdb_VObject *); + OH_Predicates (*OH_Predicates_Between)(OH_Predicates *, const char *, OH_Rdb_VObject *); + OH_Predicates (*OH_Predicates_NotBetween)(OH_Predicates *, const char *, OH_Rdb_VObject *); + OH_Predicates (*OH_Predicates_GreaterThan)(OH_Predicates *, const char *, OH_Rdb_VObject *); + OH_Predicates (*OH_Predicates_LessThan)(OH_Predicates *, const char *, OH_Rdb_VObject *); + OH_Predicates (*OH_Predicates_GreaterThanOrEqualTo)(OH_Predicates *, const char *, OH_Rdb_VObject *); + OH_Predicates (*OH_Predicates_LessThanOrEqualTo)(OH_Predicates *, const char *, OH_Rdb_VObject *); + OH_Predicates (*OH_Predicates_OrderBy)(OH_Predicates *, const char *, OH_Rdb_OrderType); + OH_Predicates (*OH_Predicates_Distinct)(OH_Predicates *); + OH_Predicates (*OH_Predicates_Limit)(OH_Predicates *, unsigned int); + OH_Predicates (*OH_Predicates_Offset)(OH_Predicates *, unsigned int); + OH_Predicates (*OH_Predicates_GroupBy)(OH_Predicates *, OH_Rdb_VObject *); + OH_Predicates (*OH_Predicates_In)(OH_Predicates *, const char *, OH_Rdb_VObject *); + OH_Predicates (*OH_Predicates_NotIn)(OH_Predicates *, const char *, OH_Rdb_VObject *); + OH_Predicates (*OH_Predicates_Clear)(OH_Predicates *); + int (*OH_Predicates_Close)(OH_Predicates *); +} OH_Predicates; + +OH_Predicates *OH_Rdb_CreatePredicates(const char *table); +int OH_Rdb_DestroyPredicates(OH_Predicates *predicates); + +#ifdef __cplusplus +}; +#endif + +#endif // RELATIONAL_PREDICATES_H diff --git a/relational_store/interfaces/ndk/include/relational_store.h b/relational_store/interfaces/ndk/include/relational_store.h new file mode 100644 index 00000000..18e8f8d5 --- /dev/null +++ b/relational_store/interfaces/ndk/include/relational_store.h @@ -0,0 +1,66 @@ +/* + * 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 RELATIONAL_STORE_H +#define RELATIONAL_STORE_H + +#include "relational_cursor.h" +#include "relational_predicates.h" +#include "relational_values_bucket.h" + +#ifdef __cplusplus +extern "C" { +#endif + +enum OH_Rdb_SecurityLevel { + S1 = 1, + S2, + S3, + S4 +}; + +typedef struct { + const char *path; + BOOL isEncrypt; + enum OH_Rdb_SecurityLevel securityLevel; +} OH_Rdb_Config; + +typedef struct { + int64_t id; +} OH_Rdb_Store; + +OH_Rdb_Store *OH_Rdb_GetOrOpen(const OH_Rdb_Config *config, int *errCode); +int OH_Rdb_CloseStore(OH_Rdb_Store *store); +int OH_Rdb_DeleteStore(const char *path); + +int OH_Rdb_Insert(OH_Rdb_Store *store, const char *table, OH_Rdb_VBucket *valuesBucket); +int OH_Rdb_Update(OH_Rdb_Store *store, OH_Rdb_VBucket *valuesBucket, OH_Predicates *predicates); +int OH_Rdb_Delete(OH_Rdb_Store *store, OH_Predicates *predicates); +OH_Cursor *OH_Rdb_Query(OH_Rdb_Store *store, OH_Predicates *predicates, const char *const *columnNames, int length); +int OH_Rdb_Execute(OH_Rdb_Store *store, const char *sql); +OH_Cursor *OH_Rdb_ExecuteQuery(OH_Rdb_Store *store, const char *sql); +int OH_Rdb_BeginTransaction(OH_Rdb_Store *store); +int OH_Rdb_RollBack(OH_Rdb_Store *store); +int OH_Rdb_Commit(OH_Rdb_Store *store); +int OH_Rdb_Backup(OH_Rdb_Store *store, const char *databasePath); +int OH_Rdb_Restore(OH_Rdb_Store *store, const char *databasePath); +int OH_Rdb_GetVersion(OH_Rdb_Store *store, int *version); +int OH_Rdb_SetVersion(OH_Rdb_Store *store, int version); + +#ifdef __cplusplus +}; +#endif + +#endif // RELATIONAL_STORE_H diff --git a/relational_store/interfaces/ndk/include/relational_value_object.h b/relational_store/interfaces/ndk/include/relational_value_object.h new file mode 100644 index 00000000..761d61bd --- /dev/null +++ b/relational_store/interfaces/ndk/include/relational_value_object.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 RELATIONAL_VALUE_OBJECT_H +#define RELATIONAL_VALUE_OBJECT_H + +#include +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + int64_t id; +} OH_Rdb_VObject; + +OH_Rdb_VObject *OH_Rdb_CreateValueObject(); +int OH_Rdb_DestroyValueObject(OH_Rdb_VObject *valueObject); + +int OH_ValueObject_PutInt64(OH_Rdb_VObject *valueObject, int64_t *value, uint32_t count); +int OH_ValueObject_PutDouble(OH_Rdb_VObject *valueObject, double *value, uint32_t count); +int OH_ValueObject_PutText(OH_Rdb_VObject *valueObject, const char *value); +int OH_ValueObject_PutTexts(OH_Rdb_VObject *valueObject, const char **value, uint32_t count); + +#ifdef __cplusplus +}; +#endif + +#endif // RELATIONAL_VALUE_OBJECT_H diff --git a/relational_store/interfaces/ndk/include/relational_values_bucket.h b/relational_store/interfaces/ndk/include/relational_values_bucket.h new file mode 100644 index 00000000..68658f5d --- /dev/null +++ b/relational_store/interfaces/ndk/include/relational_values_bucket.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 RELATIONAL_VALUES_BUCKET_H +#define RELATIONAL_VALUES_BUCKET_H + +#include +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + int64_t id; + uint16_t capability; +} OH_Rdb_VBucket; + +OH_Rdb_VBucket *OH_Rdb_CreateValuesBucket(); +int OH_Rdb_DestroyValuesBucket(OH_Rdb_VBucket *bucket); + +int OH_VBucket_PutText(OH_Rdb_VBucket *bucket, const char *field, const char *value); +int OH_VBucket_PutInt64(OH_Rdb_VBucket *bucket, const char *field, int64_t value); +int OH_VBucket_PutReal(OH_Rdb_VBucket *bucket, const char *field, double value); +int OH_VBucket_PutBlob(OH_Rdb_VBucket *bucket, const char *field, const uint8_t *value, uint32_t size); +int OH_VBucket_PutNull(OH_Rdb_VBucket *bucket, const char *field); +int OH_VBucket_Clear(OH_Rdb_VBucket *bucket); +int OH_VBucket_Close(OH_Rdb_VBucket *bucket); + +#ifdef __cplusplus +}; +#endif + +#endif // RELATIONAL_VALUES_BUCKET_H diff --git a/relational_store/interfaces/ndk/libnative_rdb.ndk.json b/relational_store/interfaces/ndk/libnative_rdb.ndk.json new file mode 100644 index 00000000..2afe3dd6 --- /dev/null +++ b/relational_store/interfaces/ndk/libnative_rdb.ndk.json @@ -0,0 +1,68 @@ +[ + {"name":"OH_Cursor_GetColumnCount" }, + {"name":"OH_Cursor_GetColumnType" }, + {"name":"OH_Cursor_GetColumnIndex" }, + {"name":"OH_Cursor_GetColumnName" }, + {"name":"OH_Cursor_GetRowCount" }, + {"name":"OH_Cursor_GoToNextRow" }, + {"name":"OH_Cursor_GetSize" }, + {"name":"OH_Cursor_GetText" }, + {"name":"OH_Cursor_GetInt64" }, + {"name":"OH_Cursor_GetReal" }, + {"name":"OH_Cursor_GetBlob" }, + {"name":"OH_Cursor_IsNull" }, + {"name":"OH_Cursor_Close" }, + {"name":"OH_Predicates_EqualTo" }, + {"name":"OH_Predicates_NotEqualTo" }, + {"name":"OH_Predicates_BeginWrap" }, + {"name":"OH_Predicates_EndWrap" }, + {"name":"OH_Predicates_Or" }, + {"name":"OH_Predicates_And" }, + {"name":"OH_Predicates_IsNull" }, + {"name":"OH_Predicates_IsNotNull" }, + {"name":"OH_Predicates_Like" }, + {"name":"OH_Predicates_Between" }, + {"name":"OH_Predicates_NotBetween" }, + {"name":"OH_Predicates_GreaterThan" }, + {"name":"OH_Predicates_LessThan" }, + {"name":"OH_Predicates_GreaterThanOrEqualTo" }, + {"name":"OH_Predicates_LessThanOrEqualTo" }, + {"name":"OH_Predicates_OrderBy" }, + {"name":"OH_Predicates_Distinct" }, + {"name":"OH_Predicates_Limit" }, + {"name":"OH_Predicates_Offset" }, + {"name":"OH_Predicates_GroupBy" }, + {"name":"OH_Predicates_In" }, + {"name":"OH_Predicates_NotIn" }, + {"name":"OH_Predicates_Clear" }, + {"name":"OH_Predicates_Close" }, + {"name":"OH_Rdb_GetOrOpen" }, + {"name":"OH_Rdb_CloseStore" }, + {"name":"OH_Rdb_DeleteStore" }, + {"name":"OH_Rdb_Insert" }, + {"name":"OH_Rdb_Update" }, + {"name":"OH_Rdb_Delete" }, + {"name":"OH_Rdb_Query" }, + {"name":"OH_Rdb_Execute" }, + {"name":"OH_Rdb_ExecuteQuery" }, + {"name":"OH_Rdb_Transaction" }, + {"name":"OH_Rdb_RollBack" }, + {"name":"OH_Rdb_Commit" }, + {"name":"OH_Rdb_Backup" }, + {"name":"OH_Rdb_Restore"}, + {"name":"OH_Rdb_GetVersion"}, + {"name":"OH_Rdb_SetVersion"}, + {"name":"OH_ValueObject_PutInt64" }, + {"name":"OH_ValueObject_PutDouble" }, + {"name":"OH_ValueObject_PutText" }, + {"name":"OH_ValueObject_PutTexts" }, + {"name":"OH_Rdb_CreateValuesBucket" }, + {"name":"OH_Rdb_DestroyValuesBucket" }, + {"name":"OH_VBucket_PutText" }, + {"name":"OH_VBucket_PutInt64" }, + {"name":"OH_VBucket_PutReal" }, + {"name":"OH_VBucket_PutBlob" }, + {"name":"OH_VBucket_PutNull" }, + {"name":"OH_VBucket_Clear" }, + {"name":"OH_VBucket_Close" } +] \ No newline at end of file diff --git a/relational_store/interfaces/ndk/src/relational_cursor.cpp b/relational_store/interfaces/ndk/src/relational_cursor.cpp new file mode 100644 index 00000000..79726815 --- /dev/null +++ b/relational_store/interfaces/ndk/src/relational_cursor.cpp @@ -0,0 +1,234 @@ +/* + * 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 "relational_cursor.h" +#include "relational_cursor_impl.h" +#include "relational_error_code.h" +#include "rdb_errno.h" +#include "ndk_logger.h" + +#include +#include +#include + +#include "securec.h" +using OHOS::RdbNdk::RDB_NDK_LABEL; +OHOS::RdbNdk::CursorImpl::CursorImpl(std::shared_ptr resultSet) +{ + id = RDB_CURSOR_CID; + resultSet_ = resultSet; + + OH_Cursor_GetColumnCount = CURSOR_GetColumnCount; + OH_Cursor_GetColumnType = CURSOR_GetColumnType; + OH_Cursor_GetColumnIndex = CURSOR_GetColumnIndex; + OH_Cursor_GetColumnName = CURSOR_GetColumnName; + OH_Cursor_GetRowCount = CURSOR_GetRowCount; + OH_Cursor_GoToNextRow = CURSOR_GoToNextRow; + OH_Cursor_GetSize = CURSOR_GetSize; + OH_Cursor_GetText = CURSOR_GetText; + OH_Cursor_GetInt64 = CURSOR_GetInt64; + OH_Cursor_GetReal = CURSOR_GetReal; + OH_Cursor_GetBlob = CURSOR_GetBlob; + OH_Cursor_IsNull = CURSOR_IsNull; + OH_Cursor_Close = CURSOR_Close; +} + +std::shared_ptr OHOS::RdbNdk::CursorImpl::GetResultSet() +{ + return resultSet_; +} + +int CURSOR_GetColumnCount(OH_Cursor *cursor, int *count) +{ + if (cursor == nullptr || count == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d, count is NULL ? %{public}d", (cursor == nullptr), + (count == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + OHOS::RdbNdk::CursorImpl *tempCursor = static_cast(cursor); + return tempCursor->GetResultSet()->GetColumnCount(*count); +} + +int CURSOR_GetColumnType(OH_Cursor *cursor, int32_t columnIndex, OH_Rdb_ColumnType *columnType) +{ + if (cursor == nullptr || columnType == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d, columnType is NULL ? %{public}d", + (cursor == nullptr), (columnType == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempCursor = static_cast(cursor); + OHOS::NativeRdb::ColumnType type; + int error = tempCursor->GetResultSet()->GetColumnType(columnIndex, type); + *columnType = static_cast(static_cast(type)); + return error; +} + +int CURSOR_GetColumnIndex(OH_Cursor *cursor, const char *name, int *columnIndex) +{ + if (cursor == nullptr || name == nullptr || columnIndex == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d, name is NULL ? %{public}d," + "columnIndex is NULL ? %{public}d", (cursor == nullptr), (name == nullptr), columnIndex == nullptr); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempCursor = static_cast(cursor); + return tempCursor->GetResultSet()->GetColumnIndex(name, *columnIndex); +} + +int CURSOR_GetColumnName(OH_Cursor *cursor, int32_t columnIndex, char *name, int length) +{ + if (cursor == nullptr || name == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d, name is NULL ? %{public}d", (cursor == nullptr), + (name == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempCursor = static_cast(cursor); + std::string str; + int errCode = tempCursor->GetResultSet()->GetColumnName(columnIndex, str); + if (errCode != OHOS::NativeRdb::E_OK) { + return errCode; + } + errno_t result = memcpy_s(name, length, str.c_str(), str.length()); + if (result != EOK) { + LOG_ERROR("memcpy_s failed, result is %{public}d", result); + return OH_Rdb_ErrCode::RDB_ERR; + } + return errCode; +} + +int CURSOR_GetRowCount(OH_Cursor *cursor, int *count) +{ + if (cursor == nullptr || count == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d, count is NULL ? %{public}d", (cursor == nullptr), + (count == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempCursor = static_cast(cursor); + return tempCursor->GetResultSet()->GetRowCount(*count); +} + +int CURSOR_GoToNextRow(OH_Cursor *cursor) +{ + if (cursor == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d", (cursor == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempCursor = static_cast(cursor); + return tempCursor->GetResultSet()->GoToNextRow(); +} + +int CURSOR_GetSize(OH_Cursor *cursor, int32_t columnIndex, size_t *size) +{ + if (cursor == nullptr || size == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d, size is NULL ? %{public}d", (cursor == nullptr), + (size == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempCursor = static_cast(cursor); + return tempCursor->GetResultSet()->GetSize(columnIndex, *size); +} + +int CURSOR_GetText(OH_Cursor *cursor, int32_t columnIndex, char *value, int length) +{ + if (cursor == nullptr || value == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d, value is NULL ? %{public}d", (cursor == nullptr), + (value == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempCursor = static_cast(cursor); + std::string str; + int errCode = tempCursor->GetResultSet()->GetString(columnIndex, str); + if (errCode != OHOS::NativeRdb::E_OK) { + return errCode; + } + errno_t result = memcpy_s(value, length, str.c_str(), str.length()); + if (result != EOK) { + LOG_ERROR("memcpy_s failed, result is %{public}d", result); + return OH_Rdb_ErrCode::RDB_ERR; + } + return errCode; +} + +int CURSOR_GetInt64(OH_Cursor *cursor, int32_t columnIndex, int64_t *value) +{ + if (cursor == nullptr || value == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d, value is NULL ? %{public}d", (cursor == nullptr), + (value == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempCursor = static_cast(cursor); + return tempCursor->GetResultSet()->GetLong(columnIndex, *value); +} + +int CURSOR_GetReal(OH_Cursor *cursor, int32_t columnIndex, double *value) +{ + if (cursor == nullptr || value == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d, value is NULL ? %{public}d", (cursor == nullptr), + (value == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempCursor = static_cast(cursor); + return tempCursor->GetResultSet()->GetDouble(columnIndex, *value); +} + +int CURSOR_GetBlob(OH_Cursor *cursor, int32_t columnIndex, unsigned char *value, int length) +{ + if (cursor == nullptr || value == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d, value is NULL ? %{public}d", (cursor == nullptr), + (value == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempCursor = static_cast(cursor); + std::vector vec; + int errCode = tempCursor->GetResultSet()->GetBlob(columnIndex, vec); + if (errCode != OHOS::NativeRdb::E_OK) { + return errCode; + } + errno_t result = memcpy_s(value, length, vec.data(), vec.size()); + if (result != EOK) { + LOG_ERROR("memcpy_s failed, result is %{public}d", result); + return OH_Rdb_ErrCode::RDB_ERR; + } + return errCode; +} + +int CURSOR_IsNull(OH_Cursor *cursor, int32_t columnIndex, BOOL *isNull) +{ + if (cursor == nullptr || isNull == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d, value is NULL ? %{public}d", (cursor == nullptr), + (isNull == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + bool isNULLTemp = false; + auto tempCursor = static_cast(cursor); + int ret = tempCursor->GetResultSet()->IsColumnNull(columnIndex, isNULLTemp); + isNULLTemp == true ? *isNull = TRUE : *isNull = FALSE; + return ret; +} + +int CURSOR_Close(OH_Cursor *cursor) +{ + if (cursor == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) { + LOG_ERROR("Parameters set error:cursor is NULL ? %{public}d", (cursor == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempCursor = static_cast(cursor); + int errCode = tempCursor->GetResultSet()->Close(); + if (errCode != OHOS::NativeRdb::E_OK) { + return errCode; + } + delete tempCursor; + tempCursor = nullptr; + return errCode; +} diff --git a/relational_store/interfaces/ndk/src/relational_cursor_impl.h b/relational_store/interfaces/ndk/src/relational_cursor_impl.h new file mode 100644 index 00000000..e80a0cbd --- /dev/null +++ b/relational_store/interfaces/ndk/src/relational_cursor_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 RELATIONAL_CURSOR_IMPL_H +#define RELATIONAL_CURSOR_IMPL_H + +#include "relational_cursor.h" +#include "result_set.h" +#include + +int CURSOR_GetColumnCount(OH_Cursor *cursor, int *count); +int CURSOR_GetColumnType(OH_Cursor *cursor, int32_t columnIndex, OH_Rdb_ColumnType *columnType); +int CURSOR_GetColumnIndex(OH_Cursor *cursor, const char *name, int *columnIndex); +int CURSOR_GetColumnName(OH_Cursor *cursor, int32_t columnIndex, char *name, int length); +int CURSOR_GetRowCount(OH_Cursor *cursor, int *count); +int CURSOR_GoToNextRow(OH_Cursor *cursor); +int CURSOR_GetSize(OH_Cursor *cursor, int32_t columnIndex, size_t *size); +int CURSOR_GetText(OH_Cursor *cursor, int32_t columnIndex, char *value, int length); +int CURSOR_GetInt64(OH_Cursor *cursor, int32_t columnIndex, int64_t *value); +int CURSOR_GetReal(OH_Cursor *cursor, int32_t columnIndex, double *value); +int CURSOR_GetBlob(OH_Cursor *cursor, int32_t columnIndex, unsigned char *value, int length); +int CURSOR_IsNull(OH_Cursor *cursor, int32_t columnIndex, BOOL *isNull); +int CURSOR_Close(OH_Cursor *cursor); + +namespace OHOS { +namespace RdbNdk { +constexpr int RDB_CURSOR_CID = 1234563; // The class id used to uniquely identify the OH_Cursor class. +class CursorImpl : public OH_Cursor { +public: + explicit CursorImpl(std::shared_ptr resultSet); + std::shared_ptr GetResultSet(); + +private: + std::shared_ptr resultSet_; +}; +} // namespace RdbNdk +} // namespace OHOS +#endif // RELATIONAL_CURSOR_IMPL_H diff --git a/relational_store/interfaces/ndk/src/relational_predicates.cpp b/relational_store/interfaces/ndk/src/relational_predicates.cpp new file mode 100644 index 00000000..dfcfaee9 --- /dev/null +++ b/relational_store/interfaces/ndk/src/relational_predicates.cpp @@ -0,0 +1,383 @@ +/* + * 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 "relational_predicates.h" + +#include "relational_predicates_impl.h" +#include "relational_value_object_impl.h" +#include "relational_error_code.h" +#include "sqlite_global_config.h" +#include "ndk_logger.h" + +using OHOS::RdbNdk::RDB_NDK_LABEL; +using namespace OHOS::NativeRdb; + +RdbPredicates &OHOS::RdbNdk::PredicateImpl::GetPredicates() +{ + return predicates_; +} + +OH_Predicates *OH_Rdb_CreatePredicates(const char *table) +{ + if (table == nullptr) { + return nullptr; + } + return new OHOS::RdbNdk::PredicateImpl(table); +} + +int OH_Rdb_DestroyPredicates(OH_Predicates *predicates) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d", (predicates == nullptr)); + return RDB_ERR_INVALID_ARGS; + } + auto tempPredicates = static_cast(predicates); + delete tempPredicates; + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +OH_Predicates PREDICATES_EqualTo(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || field == nullptr + || valueObject == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d," + "valueObject is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr), (valueObject == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + std::vector tempValue = static_cast(valueObject)->getValue(); + tempPredicates->GetPredicates().EqualTo(field, tempValue[0]); + return *predicates; +} + +OH_Predicates PREDICATES_NotEqualTo(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || field == nullptr + || valueObject == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d," + "valueObject is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr), (valueObject == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + std::vector tempValue = static_cast(valueObject)->getValue(); + tempPredicates->GetPredicates().NotEqualTo(field, tempValue[0]); + return *predicates; +} + +OH_Predicates PREDICATES_BeginWrap(OH_Predicates *predicates) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d", (predicates == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + tempPredicates->GetPredicates().BeginWrap(); + return *predicates; +} + +OH_Predicates PREDICATES_EndWrap(OH_Predicates *predicates) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d", (predicates == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + tempPredicates->GetPredicates().EndWrap(); + return *predicates; +} + +OH_Predicates PREDICATES_Or(OH_Predicates *predicates) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d", (predicates == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + tempPredicates->GetPredicates().Or(); + return *predicates; +} + +OH_Predicates PREDICATES_And(OH_Predicates *predicates) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d", (predicates == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + tempPredicates->GetPredicates().And(); + return *predicates; +} + +OH_Predicates PREDICATES_IsNull(OH_Predicates *predicates, const char *field) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || field == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + tempPredicates->GetPredicates().IsNull(field); + return *predicates; +} + +OH_Predicates PREDICATES_IsNotNull(OH_Predicates *predicates, const char *field) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || field == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + tempPredicates->GetPredicates().IsNotNull(field); + return *predicates; +} + +OH_Predicates PREDICATES_Like(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || field == nullptr + || valueObject == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d," + "valueObject is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr), (valueObject == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + std::vector tempValue = static_cast(valueObject)->getValue(); + tempPredicates->GetPredicates().Like(field, tempValue[0]); + return *predicates; +} + +OH_Predicates PREDICATES_Between(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || field == nullptr + || valueObject == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d," + "valueObject is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr), (valueObject == nullptr)); + return *predicates; + } + std::vector tempValue = static_cast(valueObject)->getValue(); + if (tempValue.size() != 2) { + LOG_ERROR("size is %{public}d", tempValue.size()); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + + tempPredicates->GetPredicates().Between(field, tempValue[0], tempValue[1]); + return *predicates; +} + +OH_Predicates PREDICATES_NotBetween(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || field == nullptr + || valueObject == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d," + "valueObject is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr), (valueObject == nullptr)); + return *predicates; + } + std::vector tempValue = static_cast(valueObject)->getValue(); + if (tempValue.size() != 2) { + LOG_ERROR("size is %{public}d", tempValue.size()); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + tempPredicates->GetPredicates().NotBetween(field, tempValue[0], tempValue[1]); + return *predicates; +} + +OH_Predicates PREDICATES_GreaterThan(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || field == nullptr + || valueObject == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d," + "valueObject is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr), (valueObject == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + std::vector tempValue = static_cast(valueObject)->getValue(); + tempPredicates->GetPredicates().GreaterThan(field, tempValue[0]); + return *predicates; +} + +OH_Predicates PREDICATES_LessThan(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || field == nullptr + || valueObject == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d," + "valueObject is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr), (valueObject == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + std::vector tempValue = static_cast(valueObject)->getValue(); + tempPredicates->GetPredicates().LessThan(field, tempValue[0]); + return *predicates; +} + +OH_Predicates PREDICATES_GreaterThanOrEqualTo(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || field == nullptr + || valueObject == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d," + "valueObject is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr), (valueObject == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + std::vector tempValue = static_cast(valueObject)->getValue(); + tempPredicates->GetPredicates().GreaterThanOrEqualTo(field, tempValue[0]); + return *predicates; +} +OH_Predicates PREDICATES_LessThanOrEqualTo(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || field == nullptr + || valueObject == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d," + "valueObject is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr), (valueObject == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + std::vector tempValue = static_cast(valueObject)->getValue(); + tempPredicates->GetPredicates().LessThanOrEqualTo(field, tempValue[0]); + return *predicates; +} + +OH_Predicates PREDICATES_OrderBy(OH_Predicates *predicates, const char *field, OH_Rdb_OrderType type) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || field == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + if (type == OH_Rdb_OrderType::DESC) { + tempPredicates->GetPredicates().OrderByDesc(field); + return *predicates; + } + tempPredicates->GetPredicates().OrderByAsc(field); + return *predicates; +} + +OH_Predicates PREDICATES_Distinct(OH_Predicates *predicates) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d", (predicates == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + tempPredicates->GetPredicates().Distinct(); + return *predicates; +} + +OH_Predicates PREDICATES_Limit(OH_Predicates *predicates, unsigned int value) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d", (predicates == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + tempPredicates->GetPredicates().Limit(value); + return *predicates; +} + +OH_Predicates PREDICATES_Offset(OH_Predicates *predicates, unsigned int rowOffset) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d", (predicates == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + tempPredicates->GetPredicates().Offset(rowOffset); + return *predicates; +} + +OH_Predicates PREDICATES_GroupBy(OH_Predicates *predicates, OH_Rdb_VObject *valueObject) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || valueObject == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, valueObject is NULL ? %{public}d,", + (predicates == nullptr), (valueObject == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + std::vector tempValue = static_cast(valueObject)->getValue(); + if (tempValue.size() > OHOS::NativeRdb::GlobalExpr::SQLITE_MAX_COLUMN) { + return *predicates; + } + tempPredicates->GetPredicates().GroupBy(tempValue); + return *predicates; +} + +OH_Predicates PREDICATES_In(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || valueObject == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d," + "valueObject is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr), (valueObject == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + std::vector tempValue = static_cast(valueObject)->getValue(); + if (tempValue.size() > OHOS::NativeRdb::GlobalExpr::SQLITE_MAX_COLUMN) { + return *predicates; + } + + tempPredicates->GetPredicates().In(field, tempValue); + return *predicates; +} + +OH_Predicates PREDICATES_NotIn(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID || valueObject == nullptr) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d, field is NULL ? %{public}d," + "valueObject is NULL ? %{public}d", + (predicates == nullptr), (field == nullptr), (valueObject == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + std::vector tempValue = static_cast(valueObject)->getValue(); + if (tempValue.size() > OHOS::NativeRdb::GlobalExpr::SQLITE_MAX_COLUMN) { + return *predicates; + } + + tempPredicates->GetPredicates().NotIn(field, tempValue); + return *predicates; +} + +OH_Predicates PREDICATES_Clear(OH_Predicates *predicates) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d", (predicates == nullptr)); + return *predicates; + } + auto tempPredicates = static_cast(predicates); + tempPredicates->GetPredicates().Clear(); + return *predicates; +} + +int PREDICATES_Close(OH_Predicates *predicates) +{ + if (predicates == nullptr || predicates->id != OHOS::RdbNdk::RDB_PREDICATES_CID) { + LOG_ERROR("Parameters set error:predicates is NULL ? %{public}d", (predicates == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempPredicates = static_cast(predicates); + delete tempPredicates; + tempPredicates = nullptr; + return OH_Rdb_ErrCode::RDB_ERR_OK; +} \ No newline at end of file diff --git a/relational_store/interfaces/ndk/src/relational_predicates_impl.h b/relational_store/interfaces/ndk/src/relational_predicates_impl.h new file mode 100644 index 00000000..63c58032 --- /dev/null +++ b/relational_store/interfaces/ndk/src/relational_predicates_impl.h @@ -0,0 +1,88 @@ +/* + * 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 RELATIONAL_PREDICATES_IMPL_H +#define RELATIONAL_PREDICATES_IMPL_H + +#include "rdb_predicates.h" +#include "relational_predicates.h" + +OH_Predicates PREDICATES_EqualTo(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject); +OH_Predicates PREDICATES_NotEqualTo(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject); +OH_Predicates PREDICATES_BeginWrap(OH_Predicates *predicates); +OH_Predicates PREDICATES_EndWrap(OH_Predicates *predicates); +OH_Predicates PREDICATES_Or(OH_Predicates *predicates); +OH_Predicates PREDICATES_And(OH_Predicates *predicates); +OH_Predicates PREDICATES_IsNull(OH_Predicates *predicates, const char *field); +OH_Predicates PREDICATES_IsNotNull(OH_Predicates *predicates, const char *field); +OH_Predicates PREDICATES_Like(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject); +OH_Predicates PREDICATES_Between(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject); +OH_Predicates PREDICATES_NotBetween(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject); +OH_Predicates PREDICATES_GreaterThan(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject); +OH_Predicates PREDICATES_LessThan(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject); +OH_Predicates PREDICATES_GreaterThanOrEqualTo( + OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject); +OH_Predicates PREDICATES_LessThanOrEqualTo(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject); +OH_Predicates PREDICATES_OrderBy(OH_Predicates *predicates, const char *field, OH_Rdb_OrderType type); +OH_Predicates PREDICATES_Distinct(OH_Predicates *predicates); +OH_Predicates PREDICATES_Limit(OH_Predicates *predicates, unsigned int value); +OH_Predicates PREDICATES_Offset(OH_Predicates *predicates, unsigned int rowOffset); +OH_Predicates PREDICATES_GroupBy(OH_Predicates *predicates, OH_Rdb_VObject *); +OH_Predicates PREDICATES_In(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject); +OH_Predicates PREDICATES_NotIn(OH_Predicates *predicates, const char *field, OH_Rdb_VObject *valueObject); +OH_Predicates PREDICATES_Clear(OH_Predicates *predicates); +int PREDICATES_Close(OH_Predicates *predicates); + +namespace OHOS { +namespace RdbNdk { +constexpr int RDB_PREDICATES_CID = 1234561; // The class id used to uniquely identify the OH_Predicates class. +class PredicateImpl : public OH_Predicates { +public: + explicit PredicateImpl(const char *table) : predicates_(table) + { + id = RDB_PREDICATES_CID; + OH_Predicates_EqualTo = PREDICATES_EqualTo; + OH_Predicates_NotEqualTo = PREDICATES_NotEqualTo; + OH_Predicates_BeginWrap = PREDICATES_BeginWrap; + OH_Predicates_EndWrap = PREDICATES_EndWrap; + OH_Predicates_Or = PREDICATES_Or; + OH_Predicates_And = PREDICATES_And; + OH_Predicates_IsNull = PREDICATES_IsNull; + OH_Predicates_IsNotNull = PREDICATES_IsNotNull; + OH_Predicates_Like = PREDICATES_Like; + OH_Predicates_Between = PREDICATES_Between; + OH_Predicates_NotBetween = PREDICATES_NotBetween; + OH_Predicates_GreaterThan = PREDICATES_GreaterThan; + OH_Predicates_LessThan = PREDICATES_LessThan; + OH_Predicates_GreaterThanOrEqualTo = PREDICATES_GreaterThanOrEqualTo; + OH_Predicates_LessThanOrEqualTo = PREDICATES_LessThanOrEqualTo; + OH_Predicates_OrderBy = PREDICATES_OrderBy; + OH_Predicates_Distinct = PREDICATES_Distinct; + OH_Predicates_Limit = PREDICATES_Limit; + OH_Predicates_Offset = PREDICATES_Offset; + OH_Predicates_GroupBy = PREDICATES_GroupBy; + OH_Predicates_In = PREDICATES_In; + OH_Predicates_NotIn = PREDICATES_NotIn; + OH_Predicates_Clear = PREDICATES_Clear; + OH_Predicates_Close = PREDICATES_Close; + } + OHOS::NativeRdb::RdbPredicates &GetPredicates(); + +private: + OHOS::NativeRdb::RdbPredicates predicates_; +}; +} // namespace RdbNdk +} // namespace OHOS +#endif // RELATIONAL_PREDICATES_IMPL_H diff --git a/relational_store/interfaces/ndk/src/relational_store.cpp b/relational_store/interfaces/ndk/src/relational_store.cpp new file mode 100644 index 00000000..8b823bc5 --- /dev/null +++ b/relational_store/interfaces/ndk/src/relational_store.cpp @@ -0,0 +1,272 @@ +/* + * 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 "relational_store.h" + +#include "relational_cursor_impl.h" +#include "relational_predicates_impl.h" +#include "relational_store_impl.h" + +#include "rdb_errno.h" +#include "rdb_helper.h" +#include "rdb_predicates.h" +#include "relational_values_bucket_impl.h" +#include "relational_error_code.h" +#include "sqlite_global_config.h" +#include "ndk_logger.h" +using OHOS::RdbNdk::RDB_NDK_LABEL; + +OHOS::RdbNdk::StoreImpl::StoreImpl(std::shared_ptr store) +{ + id = RDB_STORE_CID; + store_ = store; +} + +std::shared_ptr OHOS::RdbNdk::StoreImpl::GetStore() +{ + return store_; +} + +class MainOpenCallback : public OHOS::NativeRdb::RdbOpenCallback { +public: + int OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override; + int OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override; +}; + +int MainOpenCallback::OnCreate(OHOS::NativeRdb::RdbStore &store) +{ + return OHOS::NativeRdb::E_OK; +} + +int MainOpenCallback::OnUpgrade(OHOS::NativeRdb::RdbStore &store, int oldVersion, int newVersion) +{ + return OHOS::NativeRdb::E_OK; +} + +OH_Rdb_Store *OH_Rdb_GetOrOpen(const OH_Rdb_Config *config, int *errCode) +{ + if (config == nullptr) { + LOG_ERROR("Parameters set error:config is NULL ? %{public}d", (config == nullptr)); + return nullptr; + } + OHOS::NativeRdb::RdbStoreConfig rdbStoreConfig(config->path); + rdbStoreConfig.SetSecurityLevel(OHOS::NativeRdb::SecurityLevel(config->securityLevel)); + rdbStoreConfig.SetEncryptStatus(config->isEncrypt); + + MainOpenCallback callback; + + std::shared_ptr store = + OHOS::NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, -1, callback, *errCode); + if (store == nullptr) { + return nullptr; + } + return new OHOS::RdbNdk::StoreImpl(store); +} + +int OH_Rdb_CloseStore(OH_Rdb_Store *store) +{ + if (store == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:config is NULL ? %{public}d", (store == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempStore = static_cast(store); + delete tempStore; + tempStore = nullptr; + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_Rdb_DeleteStore(const char *path) +{ + if (path == nullptr) { + LOG_ERROR("Parameters set error:path is NULL ? %{public}d", (path == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + int err = OHOS::NativeRdb::RdbHelper::DeleteRdbStore(path); + if (err != OHOS::NativeRdb::E_OK) { + return err; + } + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_Rdb_Insert(OH_Rdb_Store *store, const char *table, OH_Rdb_VBucket *valuesBucket) +{ + if (store == nullptr || table == nullptr || valuesBucket == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d, table is NULL ? %{public}d," + "valuesBucket is NULL ? %{public}d", + (store == nullptr), (table == nullptr), (valuesBucket == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + int64_t rowId = -1; + auto tempStore = static_cast(store); + auto valueImpl = static_cast(valuesBucket); + tempStore->GetStore()->Insert(rowId, table, valueImpl->getValuesBucket()); + return rowId >= 0 ? rowId : OH_Rdb_ErrCode::RDB_ERR; +} + +int OH_Rdb_Update(OH_Rdb_Store *store, OH_Rdb_VBucket *valueBucket, OH_Predicates *predicates) +{ + if (store == nullptr || predicates == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d, valueBucket is NULL ? %{public}d," + "predicates is NULL ? %{public}d", + (store == nullptr), (valueBucket == nullptr), (predicates == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + int updatedRows = -1; + auto tempStore = static_cast(store); + auto tempPredicate = static_cast(predicates); + auto valueImpl = static_cast(valueBucket); + + tempStore->GetStore()->Update(updatedRows, valueImpl->getValuesBucket(), (tempPredicate->GetPredicates())); + return updatedRows >= 0 ? updatedRows : OH_Rdb_ErrCode::RDB_ERR; +} + +int OH_Rdb_Delete(OH_Rdb_Store *store, OH_Predicates *predicates) +{ + if (store == nullptr || predicates == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d, predicates is NULL ? %{public}d", + (store == nullptr), (predicates == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + int deletedRows = -1; + auto tempStore = static_cast(store); + auto tempPredicate = static_cast(predicates); + tempStore->GetStore()->Delete(deletedRows, (tempPredicate->GetPredicates())); + return deletedRows >= 0 ? deletedRows : OH_Rdb_ErrCode::RDB_ERR; +} + +OH_Cursor *OH_Rdb_Query(OH_Rdb_Store *store, OH_Predicates *predicates, const char *const *columnNames, int length) +{ + if (store == nullptr || predicates == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID + || length > OHOS::NativeRdb::GlobalExpr::SQLITE_MAX_COLUMN) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d, predicates is NULL ? %{public}d," + "length is %{public}d", (store == nullptr), (predicates == nullptr), length); + return nullptr; + } + auto tempStore = static_cast(store); + auto tempPredicate = static_cast(predicates); + std::vector columns; + if (columnNames != nullptr) { + columns.reserve(length); + for (int i = 0; i < length; i++) { + columns.push_back(columnNames[i]); + } + } + + auto resultSet = tempStore->GetStore()->QueryByStep(tempPredicate->GetPredicates(), columns); + if (resultSet == nullptr) { + return nullptr; + } + return new OHOS::RdbNdk::CursorImpl(std::move(resultSet)); +} + +OH_Cursor *OH_Rdb_ExecuteQuery(OH_Rdb_Store *store, const char *sql) +{ + if (store == nullptr || sql == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d, sql is NULL ? %{public}d", (store == nullptr), + (sql == nullptr)); + return nullptr; + } + auto tempStore = static_cast(store); + auto resultSet = tempStore->GetStore()->QuerySql(sql, std::vector{}); + if (resultSet == nullptr) { + return nullptr; + } + return new OHOS::RdbNdk::CursorImpl(std::move(resultSet)); +} + +int OH_Rdb_Execute(OH_Rdb_Store *store, const char *sql) +{ + if (store == nullptr || sql == nullptr ||store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d, sql is NULL ? %{public}d", (store == nullptr), + (sql == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempStore = static_cast(store); + return tempStore->GetStore()->ExecuteSql(sql, std::vector{}); +} + +int OH_Rdb_BeginTransaction(OH_Rdb_Store *store) +{ + if (store == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d", (store == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempStore = static_cast(store); + return tempStore->GetStore()->BeginTransaction(); +} + +int OH_Rdb_RollBack(OH_Rdb_Store *store) +{ + if (store == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d", (store == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempStore = static_cast(store); + return tempStore->GetStore()->RollBack(); +} + +int OH_Rdb_Commit(OH_Rdb_Store *store) +{ + if (store == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d", (store == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempStore = static_cast(store); + return tempStore->GetStore()->Commit(); +} + +int OH_Rdb_Backup(OH_Rdb_Store *store, const char *databasePath) +{ + if (store == nullptr || databasePath == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d, databasePath is NULL ? %{public}d", + (store == nullptr), (databasePath == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempStore = static_cast(store); + + return tempStore->GetStore()->Backup(databasePath, {}); +} + +int OH_Rdb_Restore(OH_Rdb_Store *store, const char *databasePath) +{ + if (store == nullptr || databasePath == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d, databasePath is NULL ? %{public}d", + (store == nullptr), (databasePath == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempStore = static_cast(store); + + return tempStore->GetStore()->Restore(databasePath, {}); +} + +int OH_Rdb_GetVersion(OH_Rdb_Store *store, int *version) +{ + if (store == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d", (store == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempStore = static_cast(store); + return tempStore->GetStore()->GetVersion(*version); +} + +int OH_Rdb_SetVersion(OH_Rdb_Store *store, int version) +{ + if (store == nullptr || store->id != OHOS::RdbNdk::RDB_STORE_CID) { + LOG_ERROR("Parameters set error:store is NULL ? %{public}d", (store == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto tempStore = static_cast(store); + return tempStore->GetStore()->SetVersion(version); +} \ No newline at end of file diff --git a/relational_store/frameworks/native/rdb/mock/src/timer.cpp b/relational_store/interfaces/ndk/src/relational_store_impl.h similarity index 34% rename from relational_store/frameworks/native/rdb/mock/src/timer.cpp rename to relational_store/interfaces/ndk/src/relational_store_impl.h index 95b46f26..b16b603d 100644 --- a/relational_store/frameworks/native/rdb/mock/src/timer.cpp +++ b/relational_store/interfaces/ndk/src/relational_store_impl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 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,72 +13,26 @@ * limitations under the License. */ -#include "timer.h" +#ifndef RELATIONAL_STORE_IMPL_H +#define RELATIONAL_STORE_IMPL_H -namespace OHOS { -namespace Utils { - -Timer::Timer(const std::string& name, int timeoutMs) : name_(name), timeoutMs_(timeoutMs), - reactor_(nullptr) -{ -} - -uint32_t Timer::Setup() -{ - name_ = ""; - reactor_ = nullptr; - return 0; -} - -void Timer::Shutdown(bool useJoin) -{ -} - -uint32_t Timer::Register(const TimerCallback& callback, uint32_t interval /* ms */, bool once) -{ - return 0; -} - -void Timer::Unregister(uint32_t timerId) -{ - return; -} - -void Timer::MainLoop() -{ -} +#include -uint32_t Timer::DoRegister(const TimerListCallback& callback, uint32_t interval, bool once, int &timerFd) -{ - return 0; -} +#include "rdb_store.h" +#include "relational_predicates.h" +#include "relational_store.h" -void Timer::DoUnregister(uint32_t interval) -{ -} - -void Timer::OnTimer(int timerFd) -{ -} - -void Timer::DoTimerListCallback(const TimerListCallback& callback, int timerFd) -{ -} - -/* valid range: [1, UINT32_MAX], but not TIMER_ERR_DEAL_FAILED */ -uint32_t Timer::GetValidId(uint32_t timerId) const -{ - return 0; -} - -int Timer::GetTimerFd(uint32_t interval /* ms */) -{ - return timeoutMs_; -} - -void Timer::EraseUnusedTimerId(uint32_t interval, const std::vector& unusedIds) -{ -} - -} // namespace Utils +namespace OHOS { +namespace RdbNdk { +constexpr int RDB_STORE_CID = 1234560; // The class id used to uniquely identify the OH_Rdb_Store class. +class StoreImpl : public OH_Rdb_Store { +public: + explicit StoreImpl(std::shared_ptr store); + std::shared_ptr GetStore(); + +private: + std::shared_ptr store_; +}; +} // namespace RdbNdk } // namespace OHOS +#endif // RELATIONAL_STORE_IMPL_H diff --git a/relational_store/interfaces/ndk/src/relational_value_object.cpp b/relational_store/interfaces/ndk/src/relational_value_object.cpp new file mode 100644 index 00000000..476885f7 --- /dev/null +++ b/relational_store/interfaces/ndk/src/relational_value_object.cpp @@ -0,0 +1,112 @@ +/* + * 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 "relational_value_object.h" +#include "relational_value_object_impl.h" +#include "relational_error_code.h" +#include "ndk_logger.h" + +using OHOS::RdbNdk::RDB_NDK_LABEL; +OH_Rdb_VObject *OH_Rdb_CreateValueObject() +{ + return new OHOS::RdbNdk::ValueObjectImpl(); +} + +std::vector &OHOS::RdbNdk::ValueObjectImpl::getValue() +{ + return value; +} + +int OH_Rdb_DestroyValueObject(OH_Rdb_VObject *valueObject) +{ + if (valueObject == nullptr || valueObject->id != OHOS::RdbNdk::RDB_VOBJECT_CID) { + LOG_ERROR("Parameters set error:valueObject is NULL ? %{public}d", (valueObject == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + delete static_cast(valueObject); + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_ValueObject_PutInt64(OH_Rdb_VObject *valueObject, int64_t *value, uint32_t count) +{ + if (valueObject == nullptr || value == nullptr || valueObject->id != OHOS::RdbNdk::RDB_VOBJECT_CID) { + LOG_ERROR("Parameters set error:valueObject is NULL ? %{public}d, value is NULL ? %{public}d", + (valueObject == nullptr), (value == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + + auto vObject = static_cast(valueObject); + vObject->getValue().clear(); + if (count == 1) { + vObject->getValue().push_back(std::to_string(*value)); + } else { + for (int i = 0; i < count; i++) { + vObject->getValue().push_back(std::to_string(value[i])); + } + }; + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_ValueObject_PutDouble(OH_Rdb_VObject *valueObject, double *value, uint32_t count) +{ + if (valueObject == nullptr || value == nullptr || valueObject->id != OHOS::RdbNdk::RDB_VOBJECT_CID) { + LOG_ERROR("Parameters set error:valueObject is NULL ? %{public}d, value is NULL ? %{public}d", + (valueObject == nullptr), (value == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + + auto vObject = static_cast(valueObject); + vObject->getValue().clear(); + if (count == 1) { + vObject->getValue().push_back(std::to_string(*value)); + } else { + for (int i = 0; i < count; i++) { + vObject->getValue().push_back(std::to_string(value[i])); + } + } + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_ValueObject_PutText(OH_Rdb_VObject *valueObject, const char *value) +{ + if (valueObject == nullptr || value == nullptr || valueObject->id != OHOS::RdbNdk::RDB_VOBJECT_CID) { + LOG_ERROR("Parameters set error:valueObject is NULL ? %{public}d, value is NULL ? %{public}d", + (valueObject == nullptr), (value == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + + std::string textValue(value); + auto vObject = static_cast(valueObject); + vObject->getValue().clear(); + vObject->getValue().push_back(textValue); + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_ValueObject_PutTexts(OH_Rdb_VObject *valueObject, const char **value, uint32_t count) +{ + if (valueObject == nullptr || value == nullptr || valueObject->id != OHOS::RdbNdk::RDB_VOBJECT_CID) { + LOG_ERROR("Parameters set error:valueObject is NULL ? %{public}d, value is NULL ? %{public}d", + (valueObject == nullptr), (value == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + + auto vObject = static_cast(valueObject); + vObject->getValue().clear(); + for (int i = 0; i < count; i++) { + std::string textValue(value[i]); + vObject->getValue().push_back(textValue); + } + return OH_Rdb_ErrCode::RDB_ERR_OK; +} \ No newline at end of file diff --git a/relational_store/interfaces/ndk/src/relational_value_object_impl.h b/relational_store/interfaces/ndk/src/relational_value_object_impl.h new file mode 100644 index 00000000..9d64e051 --- /dev/null +++ b/relational_store/interfaces/ndk/src/relational_value_object_impl.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 RELATIONAL_VALUE_OBJECT_IMPL_H +#define RELATIONAL_VALUE_OBJECT_IMPL_H + +#include "relational_value_object.h" +#include +#include + +namespace OHOS { +namespace RdbNdk { +constexpr int RDB_VOBJECT_CID = 1234565; // The class id used to uniquely identify the OH_Rdb_VObject class. +class ValueObjectImpl : public OH_Rdb_VObject { +public: + ValueObjectImpl() + { + id = RDB_VOBJECT_CID; + } + std::vector &getValue(); +private: + std::vector value; +}; +} // namespace RdbNdk +} // namespace OHOS +#endif // RELATIONAL_VALUE_OBJECT_IMPL_H diff --git a/relational_store/interfaces/ndk/src/relational_values_bucket.cpp b/relational_store/interfaces/ndk/src/relational_values_bucket.cpp new file mode 100644 index 00000000..136d4f75 --- /dev/null +++ b/relational_store/interfaces/ndk/src/relational_values_bucket.cpp @@ -0,0 +1,135 @@ +/* + * 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 "relational_values_bucket.h" +#include "relational_values_bucket_impl.h" +#include "relational_error_code.h" +#include "value_object.h" +#include "securec.h" +#include "ndk_logger.h" + +using OHOS::RdbNdk::RDB_NDK_LABEL; +OH_Rdb_VBucket *OH_Rdb_CreateValuesBucket() +{ + return new OHOS::RdbNdk::ValuesBucketImpl(); +} + +OHOS::NativeRdb::ValuesBucket &OHOS::RdbNdk::ValuesBucketImpl::getValuesBucket() +{ + return valuesBucket_; +} +int OH_Rdb_DestroyValuesBucket(OH_Rdb_VBucket *bucket) +{ + if (bucket == nullptr || bucket->id != OHOS::RdbNdk::RDB_VBUCKET_CID) { + LOG_ERROR("Parameters set error:bucket is NULL ? %{public}d", (bucket == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + delete static_cast(bucket); + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_VBucket_PutText(OH_Rdb_VBucket *bucket, const char *field, const char *value) +{ + if (bucket == nullptr || field == nullptr || bucket->id != OHOS::RdbNdk::RDB_VBUCKET_CID) { + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + static_cast(bucket)->getValuesBucket().Put( + field, OHOS::NativeRdb::ValueObject(value)); + bucket->capability += 1; + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_VBucket_PutInt64(OH_Rdb_VBucket *bucket, const char *field, int64_t value) +{ + if (bucket == nullptr || field == nullptr || bucket->id != OHOS::RdbNdk::RDB_VBUCKET_CID) { + LOG_ERROR("Parameters set error:bucket is NULL ? %{public}d, field is NULL ? %{public}d", (bucket == nullptr), + (field == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + static_cast(bucket)->getValuesBucket().Put( + field, OHOS::NativeRdb::ValueObject(value)); + bucket->capability += 1; + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_VBucket_PutReal(OH_Rdb_VBucket *bucket, const char *field, double value) +{ + if (bucket == nullptr || field == nullptr || bucket->id != OHOS::RdbNdk::RDB_VBUCKET_CID) { + LOG_ERROR("Parameters set error:bucket is NULL ? %{public}d, field is NULL ? %{public}d", (bucket == nullptr), + (field == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + static_cast(bucket)->getValuesBucket().Put( + field, OHOS::NativeRdb::ValueObject(value)); + bucket->capability += 1; + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_VBucket_PutBlob(OH_Rdb_VBucket *bucket, const char *field, const uint8_t *value, uint32_t size) +{ + if (bucket == nullptr || field == nullptr || bucket->id != OHOS::RdbNdk::RDB_VBUCKET_CID) { + LOG_ERROR("Parameters set error:bucket is NULL ? %{public}d, field is NULL ? %{public}d", (bucket == nullptr), + (field == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + std::vector blobValue; + if (value != nullptr) { + blobValue.reserve(size); + for (uint32_t i = 0; i < size; i++) { + blobValue.push_back(value[i]); + } + } + + static_cast(bucket)->getValuesBucket().Put( + field, OHOS::NativeRdb::ValueObject(blobValue)); + bucket->capability += 1; + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_VBucket_PutNull(OH_Rdb_VBucket *bucket, const char *field) +{ + if (bucket == nullptr || field == nullptr || bucket->id != OHOS::RdbNdk::RDB_VBUCKET_CID) { + LOG_ERROR("Parameters set error:bucket is NULL ? %{public}d, field is NULL ? %{public}d", (bucket == nullptr), + (field == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + static_cast(bucket)->getValuesBucket().Put(field, OHOS::NativeRdb::ValueObject()); + bucket->capability += 1; + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_VBucket_Clear(OH_Rdb_VBucket *bucket) +{ + if (bucket == nullptr || bucket->id != OHOS::RdbNdk::RDB_VBUCKET_CID) { + LOG_ERROR("Parameters set error:bucket is NULL ? %{public}d", (bucket == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + static_cast(bucket)->getValuesBucket().Clear(); + bucket->capability = 0; + return OH_Rdb_ErrCode::RDB_ERR_OK; +} + +int OH_VBucket_Close(OH_Rdb_VBucket *bucket) +{ + if (bucket == nullptr || bucket->id != OHOS::RdbNdk::RDB_VBUCKET_CID) { + LOG_ERROR("Parameters set error:bucket is NULL ? %{public}d", (bucket == nullptr)); + return OH_Rdb_ErrCode::RDB_ERR_INVALID_ARGS; + } + auto bucketTemp = static_cast(bucket); + delete bucketTemp; + bucketTemp = nullptr; + return OH_Rdb_ErrCode::RDB_ERR_OK; +} \ No newline at end of file diff --git a/relational_store/interfaces/ndk/src/relational_values_bucket_impl.h b/relational_store/interfaces/ndk/src/relational_values_bucket_impl.h new file mode 100644 index 00000000..c05cfbcb --- /dev/null +++ b/relational_store/interfaces/ndk/src/relational_values_bucket_impl.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 RELATIONAL_VALUES_BUCKET_IMPL_H +#define RELATIONAL_VALUES_BUCKET_IMPL_H + +#include "relational_values_bucket.h" +#include "values_bucket.h" + +namespace OHOS { +namespace RdbNdk { +constexpr int RDB_VBUCKET_CID = 1234562; // The class id used to uniquely identify the OH_Rdb_VBucket class. +class ValuesBucketImpl : public OH_Rdb_VBucket { +public: + ValuesBucketImpl() + { + id = RDB_VBUCKET_CID; + capability = 0; + } + OHOS::NativeRdb::ValuesBucket &getValuesBucket(); +private: + OHOS::NativeRdb::ValuesBucket valuesBucket_; +}; +} // namespace RdbNdk +} // namespace OHOS +#endif // RELATIONAL_VALUES_BUCKET_IMPL_H diff --git a/relational_store/test/native/rdb/unittest/common.h b/relational_store/test/native/rdb/unittest/common.h index bab8e71a..fdde399c 100644 --- a/relational_store/test/native/rdb/unittest/common.h +++ b/relational_store/test/native/rdb/unittest/common.h @@ -20,9 +20,9 @@ namespace OHOS { namespace NativeRdb { - -static const std::string RDB_TEST_PATH = "/data/test/"; - +#define RDB_TEST_BASE_PATH "/data/test/" +#define RDB_TEST_C_PATH(subPath) RDB_TEST_BASE_PATH subPath +#define RDB_TEST_PATH std::string(RDB_TEST_BASE_PATH) } // namespace NativeRdb } // namespace OHOS diff --git a/relational_store/test/native/rdb/unittest/rdb_get_store_test.cpp b/relational_store/test/native/rdb/unittest/rdb_get_store_test.cpp index fe52ab41..c33c2295 100644 --- a/relational_store/test/native/rdb/unittest/rdb_get_store_test.cpp +++ b/relational_store/test/native/rdb/unittest/rdb_get_store_test.cpp @@ -35,28 +35,23 @@ public: void QueryCheck1(std::shared_ptr &store) const; void QueryCheck2(std::shared_ptr &store) const; - static const std::string MAIN_DATABASE_NAME; - static const std::string MAIN_DATABASE_NAME_RELEASE; - static const std::string MAIN_DATABASE_NAME_STATUS; - static const std::string MAIN_DATABASE_NAME_MINUS; + static constexpr const char *MAIN_DATABASE_NAME = RDB_TEST_C_PATH("getrdb.db"); + static constexpr const char *MAIN_DATABASE_NAME_RELEASE = RDB_TEST_C_PATH("releaserdb.db"); + static constexpr const char *MAIN_DATABASE_NAME_STATUS = RDB_TEST_C_PATH("status.db"); + static constexpr const char *MAIN_DATABASE_NAME_MINUS = RDB_TEST_C_PATH("minus.db"); std::shared_ptr CreateGetRDB(int version); void CreateRDB(int version); }; -const std::string RdbGetStoreTest::MAIN_DATABASE_NAME = RDB_TEST_PATH + "getrdb.db"; -const std::string RdbGetStoreTest::MAIN_DATABASE_NAME_RELEASE = RDB_TEST_PATH + "releaserdb.db"; -const std::string RdbGetStoreTest::MAIN_DATABASE_NAME_STATUS = RDB_TEST_PATH + "status.db"; -const std::string RdbGetStoreTest::MAIN_DATABASE_NAME_MINUS = RDB_TEST_PATH + "minus.db"; - class GetOpenCallback : public RdbOpenCallback { public: int OnCreate(RdbStore &rdbStore) override; int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override; - static const std::string CREATE_TABLE_TEST; + static constexpr const char *CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test1(id INTEGER PRIMARY KEY " + "AUTOINCREMENT, name TEXT NOT NULL, age INTEGER)"; }; -std::string const GetOpenCallback::CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test1(id INTEGER PRIMARY KEY " - "AUTOINCREMENT, name TEXT NOT NULL, age INTEGER)"; +constexpr const char *GetOpenCallback::CREATE_TABLE_TEST; int GetOpenCallback::OnCreate(RdbStore &store) { @@ -72,6 +67,9 @@ void RdbGetStoreTest::SetUpTestCase(void) { RdbStoreManager::GetInstance().SetReleaseTime(1000); RdbHelper::DeleteRdbStore(MAIN_DATABASE_NAME); + RdbHelper::DeleteRdbStore(MAIN_DATABASE_NAME_RELEASE); + RdbHelper::DeleteRdbStore(MAIN_DATABASE_NAME_STATUS); + RdbHelper::DeleteRdbStore(MAIN_DATABASE_NAME_STATUS); } void RdbGetStoreTest::TearDownTestCase(void) diff --git a/relational_store/test/native/rdb/unittest/rdb_predicates_test.cpp b/relational_store/test/native/rdb/unittest/rdb_predicates_test.cpp index c9378919..9c2b256d 100644 --- a/relational_store/test/native/rdb/unittest/rdb_predicates_test.cpp +++ b/relational_store/test/native/rdb/unittest/rdb_predicates_test.cpp @@ -1613,7 +1613,7 @@ HWTEST_F(RdbStorePredicateTest, RdbStore_GetDistributedPredicates_027, TestSize. RdbPredicates predicates("AllDataType"); predicates.EqualTo("stringValue", "ABCDEFGHIJKLMN")->OrderByDesc("integerValue")->Limit(2); auto distributedRdbPredicates = predicates.GetDistributedPredicates(); - EXPECT_EQ(distributedRdbPredicates.table_, "AllDataType"); + EXPECT_EQ(*(distributedRdbPredicates.tables_.begin()), "AllDataType"); EXPECT_EQ(distributedRdbPredicates.operations_.size(), 3UL); EXPECT_EQ(distributedRdbPredicates.operations_[0].operator_, OHOS::DistributedRdb::EQUAL_TO); EXPECT_EQ(distributedRdbPredicates.operations_[0].field_, "stringValue"); diff --git a/relational_store/test/native/rdb/unittest/rdb_store_subscribe_test.cpp b/relational_store/test/native/rdb/unittest/rdb_store_subscribe_test.cpp new file mode 100644 index 00000000..55c331e7 --- /dev/null +++ b/relational_store/test/native/rdb/unittest/rdb_store_subscribe_test.cpp @@ -0,0 +1,146 @@ +/* + * 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 "common.h" +#include "rdb_errno.h" +#include "rdb_helper.h" +#include "rdb_open_callback.h" +#include "rdb_store_manager.h" +#include "rdb_types.h" + +using namespace testing::ext; +using namespace OHOS::NativeRdb; +using namespace OHOS::DistributedRdb; + +class RdbStoreSubTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + static const std::string MAIN_DATABASE_NAME; + static std::shared_ptr CreateRDB(int version); + static std::shared_ptr store; +}; + +const std::string RdbStoreSubTest::MAIN_DATABASE_NAME = RDB_TEST_PATH + "getrdb.db"; +std::shared_ptr RdbStoreSubTest::store = nullptr; + +void RdbStoreSubTest::SetUpTestCase(void) +{ + store = CreateRDB(1); +} + +void RdbStoreSubTest::TearDownTestCase(void) +{ +} + +void RdbStoreSubTest::SetUp() +{ +} + +void RdbStoreSubTest::TearDown() +{ +} + +class Callback : public RdbOpenCallback { +public: + int OnCreate(RdbStore &rdbStore) override; + int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override; +}; + +int Callback::OnCreate(RdbStore &store) +{ + return E_OK; +} + +int Callback::OnUpgrade(RdbStore &store, int oldVersion, int newVersion) +{ + return E_OK; +} + +class SubObserver : public RdbStoreObserver { +public: + virtual ~SubObserver() {} + void OnChange(const std::vector& devices) override; +}; + +void SubObserver::OnChange(const std::vector &devices) +{ +} + +std::shared_ptr RdbStoreSubTest::CreateRDB(int version) +{ + RdbStoreConfig config(RdbStoreSubTest::MAIN_DATABASE_NAME); + config.SetBundleName("subscribe_test"); + config.SetArea(0); + config.SetCreateNecessary(true); + config.SetDistributedType(RDB_DEVICE_COLLABORATION); + config.SetSecurityLevel(OHOS::NativeRdb::SecurityLevel::S1); + Callback helper; + int errCode = E_OK; + std::shared_ptr store = RdbHelper::GetRdbStore(config, version, helper, errCode); + EXPECT_NE(store, nullptr); + return store; +} + +/** + * @tc.name: RdbStoreSubscribeRemote + * @tc.desc: RdbStoreSubscribe + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(RdbStoreSubTest, RdbStoreSubscribeRemote, TestSize.Level1) +{ + EXPECT_NE(store, nullptr) << "store is nullptr"; + std::shared_ptr observer = std::make_shared(); + auto status = store->Subscribe({ SubscribeMode::REMOTE }, observer.get()); + EXPECT_EQ(status, E_OK); +} + +/** + * @tc.name: RdbStoreSubscribeCloud + * @tc.desc: RdbStoreSubscribe + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(RdbStoreSubTest, RdbStoreSubscribeCloud, TestSize.Level1) +{ + EXPECT_NE(store, nullptr) << "store is nullptr"; + std::shared_ptr observer = std::make_shared(); + auto status = store->Subscribe({ SubscribeMode::CLOUD }, observer.get()); + EXPECT_EQ(status, E_OK); +} + +/** + * @tc.name: RdbStoreSubscribeCloudDetail + * @tc.desc: RdbStoreSubscribe + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(RdbStoreSubTest, RdbStoreSubscribeCloudDetail, TestSize.Level1) +{ + EXPECT_NE(store, nullptr) << "store is nullptr"; + std::shared_ptr observer = std::make_shared(); + auto status = store->Subscribe({ SubscribeMode::CLOUD_DETAIL }, observer.get()); + EXPECT_EQ(status, E_OK); +} \ No newline at end of file diff --git a/relational_store/test/native/rdb/unittest/rdb_value_bucket_test.cpp b/relational_store/test/native/rdb/unittest/rdb_value_bucket_test.cpp index b38d6625..f3c0558b 100644 --- a/relational_store/test/native/rdb/unittest/rdb_value_bucket_test.cpp +++ b/relational_store/test/native/rdb/unittest/rdb_value_bucket_test.cpp @@ -219,8 +219,8 @@ HWTEST_F(ValuesBucketTest, Values_Bucket_005, TestSize.Level1) } EXPECT_EQ(true, values.Marshalling(parcel)); - auto valuesBucket = std::shared_ptr(ValuesBucket::Unmarshalling(parcel)); - EXPECT_EQ(true, valuesBucket->IsEmpty()); + auto valuesBucket = ValuesBucket::Unmarshalling(parcel); + EXPECT_FALSE(valuesBucket.IsEmpty()); } /** -- Gitee From 4bd44cc222495db535de6e59476bbbc8528f30ee Mon Sep 17 00:00:00 2001 From: htt1997 Date: Thu, 1 Jun 2023 21:05:45 +0800 Subject: [PATCH 2/4] update Signed-off-by: htt1997 --- datamgr_service/Readme.md | 127 ------------------ .../app/src/task_manager.cpp | 3 +- .../service/cloud/cloud_service_impl.cpp | 4 +- .../service/cloud/cloud_service_impl.h | 2 +- .../service/rdb/rdb_service_impl.cpp | 9 +- .../service/rdb/rdb_syncer.cpp | 8 +- .../service/rdb/rdb_syncer.h | 2 +- .../napi/cloud_data/include/js_error_utils.h | 2 +- .../js/napi/cloud_data/src/js_error_utils.cpp | 5 +- .../js/napi/common/include/js_utils.h | 6 + .../js/napi/common/src/js_utils.cpp | 77 +++++++++++ .../relationalstore/include/napi_rdb_store.h | 8 ++ .../relationalstore/src/napi_rdb_store.cpp | 23 +++- .../native/rdb/include/rdb_store_impl.h | 3 - .../native/rdb/mock/include/rdb_store_impl.h | 3 - .../native/rdb/src/rdb_store_impl.cpp | 4 +- .../native/rdb/src/rdb_types_util.cpp | 6 +- .../interfaces/inner_api/rdb/BUILD.gn | 20 ++- .../inner_api/rdb/include/rdb_types.h | 5 +- 19 files changed, 148 insertions(+), 169 deletions(-) delete mode 100644 datamgr_service/Readme.md diff --git a/datamgr_service/Readme.md b/datamgr_service/Readme.md deleted file mode 100644 index 8af31b6f..00000000 --- a/datamgr_service/Readme.md +++ /dev/null @@ -1,127 +0,0 @@ -# distributeddatamgr\_distributeddatamgr - -- [Introduction](#section11660541593) -- [Directory Structure](#section161941989596) -- [Constraints](#section119744591305) -- [Description](#section1312121216216) -- [Repositories Involved](#section1371113476307) - -## Introduction - -Distributed Data Service \(DDS\) provides the capability to store data in the databases of different devices. DDS isolates data based on a triplet of the account, app, and database. DDS synchronizes data between trusted devices to provide users with consistent data access experience on different devices. - -DDS manages data of OpenHarmony in a distributed manner. It consists of the following parts: - -- **Service interfaces** - - DDS provides APIs for other modules to create databases, access data, and subscribe to data. Supporting the KV data model and common data types, the APIs are highly compatible and easy to use, and can be released. - -- **Service component** - - The service component manages metadata, permissions, backup and restoration, and multiple users. It also initializes the storage and synchronization components and communication adaptation layer of the distributed database. - -- **Storage component** - - The storage component provides data access, data reduction, transactions, snapshots, data combination, and conflict resolution. - -- **Synchronization component** - - The synchronization component connects the storage and communication components. It maintains data consistency between online devices by synchronizing data generated on the local device to other devices and merging data received from other devices into the local device. - -- **Communication adaptation layer** - - The communication adaptation layer invokes interfaces of the public communication layer to create and connect to communication channels, receive device online and offline messages, maintain metadata of the connected and disconnected devices, send device online and offline messages to the synchronization component, synchronize the list of devices connected to and maintained by components, and invoke interfaces to encapsulate and send data to the connected devices. - - -You call APIs of DDS to create, access, and subscribe to distributed databases. The service interfaces store data to the storage component based on the capabilities provided by the service component. The storage component invokes the synchronization component to synchronize data. The synchronization component uses the communication adaptation layer to synchronize data to remote devices, which update the data in the storage component. - -**Figure 1** How DDS works - - -![](figures/distributed-datamgr-subsystem-architecture.png) - -## Directory Structure - -``` -/foundation/distributeddatamgr/datamgr_service -├── interfaces # APIs -│ └── innerkits # Native APIs -│ └── jskits # JavaScript APIs -├── services # Service code -│ └── distributeddataservice # DDS implementation -└── test # Test case resources -``` - -## Constraints - -- To use all functions of DDS, you need to obtain the **ohos.permission.DISTRIBUTED\_DATASYNC** permission. -- DDS supports the KV data model, but not foreign keys or triggers of the relational database. -- DDS supports the following KV data model specifications: - - For the device KV store, the maximum size of a key is 896 bytes, and that of a value is 4 MB. - - For the single KV store, the maximum size of a key is 1 KB, and that of a value is 4 MB. - - Each app can open a maximum of 16 databases simultaneously. - -- DDS cannot completely replace the database in the service sandbox for storing data, because the storage types supported by them are not completely the same. You need to determine the data to be synchronized in distributed mode_ and store the data in DDS. -- Currently, DDS does not allow customization of conflict resolution policies. -- DDS supports a maximum of 1000 **KvStore** API calls per second, and 10,000 per minute. It supports a maximum of 50 **KvManager** API calls per second, and 500 per minute. - -## Description - -Some basic concepts related to the DDS are as follows: - -- **KV data model** - - KV is short for key-value. The KV database is a type of NoSQL database. Data in this type of database is organized, indexed, and stored in the form of key-value pairs. - - The KV data model is suitable for storing service data that does not involve too many data or service relationships. It provides better read and write performance than the SQL database. The KV data model is widely used in distributed scenarios because it handles conflict more easily in database version compatibility and data synchronization. The distributed database is based on the KV data model and provides KV-based access interfaces. - -- **Distributed database transactions** - - Distributed database transactions include local transactions \(same as the transactions of traditional databases\) and synchronization transactions. Synchronization transactions refer to data synchronization between devices in the unit of local transaction. Synchronization of a local transaction modification either succeeds or fails on multiple devices. - -- **Distributed database consistency** - - In a distributed scenario where multiple devices are used in the same network, distributed database consistency means that data is consistent on these devices. This consistency can be classified into strong, weak, and eventual consistency. - - - **Strong consistency**: After data is inserted, deleted, or updated on a device, other devices in the same network will obtain the updated data. - - **Weak consistency**: After data is inserted, deleted, or updated on a device, other devices in the same network may or may not obtain the updated data. The time when all devices have the same data is uncertain. - - **Eventual consistency**: After data is inserted, deleted, or updated on a device, other devices in the same network may not obtain the updated data immediately. However, data on all the devices will become consistent after some time. - - Strong consistency has high requirements on distributed data management and may be used in distributed server scenarios. DDS supports only ultimate consistency because mobile devices are not always online and there is no center. - -- **Distributed database synchronization** - - After discovering and authenticating a device, the bottom-layer communication component notifies the DDS that the device goes online. After receiving the notification, DDS establishes an encrypted transmission channel to synchronize data between the two devices. - - DDS provides both manual and automatic synchronization. In manual synchronization, you can specify the list of target devices and the synchronization mode_ \(PULL, PUSH, or PULL\_PUSH\). In automatic synchronization, the distributed database synchronizes data \(when devices go online or data is modified\), and you are unaware of the synchronization. - -- **Single KV store** - - Data is saved locally in the unit of a single KV entry. Only one entry is saved for each key. When users modify data locally, the corresponding KV entry is modified, regardless of whether the data has been synchronized. As for synchronization, the latest modification is synchronized to remote devices. - -- **Device KV store** - - The device KV store is based on the single KV store and adds device IDs before the keys of key-value pairs. This ensures that the data generated by each device is isolated from each other and is centrally managed by the system. With the device KV store, you can query data of a specified device, but cannot modify the data synchronized from remote devices. - -- **Conflict resolution policy for the distributed database** - - A data conflict occurs when multiple devices modify the same data and commit the modification to the database. In this case, the default conflict resolution policy is used, that is, the data modification committed later takes effect. Currently, the conflict resolution policy cannot be customized. - -- **Schema-based database management and data query based on predicates** - - When creating or opening a single KV store, you can specify a schema. The database detects the value format of key-value pairs based on the schema and checks the value structure. In addition, the database provides index creation and predicate-based query functions based on the fields in the values. - -- **Distributed database backup capability** - - DDS provides the database backup capability. By setting **backup** to **true**, you can trigger daily database backup. If a distributed database is damaged, DDS deletes the database and restores the most recent data from the backup database. If no backup database is available, DDS creates one. DDS can also back up encrypted databases. - - -## Repositories Involved - -Distributed Data Management subsystem - -distributeddatamgr\_datamgr\_service - -third\_party\_sqlite - - diff --git a/datamgr_service/services/distributeddataservice/app/src/task_manager.cpp b/datamgr_service/services/distributeddataservice/app/src/task_manager.cpp index 1332fc20..ec06928e 100644 --- a/datamgr_service/services/distributeddataservice/app/src/task_manager.cpp +++ b/datamgr_service/services/distributeddataservice/app/src/task_manager.cpp @@ -15,9 +15,8 @@ #include "task_manager.h" namespace OHOS::DistributedData { -TaskManager::TaskManager(std::shared_ptr executors) +TaskManager::TaskManager(std::shared_ptr executors) : executors_(executors) { - executors_ = executors; } TaskManager::~TaskManager() diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 54956ad2..991210a5 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -354,7 +354,7 @@ ExecutorPool::Task CloudServiceImpl::GenTask(int32_t retry, int32_t user, AsyncW }; } -std::vector CloudServiceImpl::GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId) +std::vector CloudServiceImpl::GetSchemaMeta(int32_t userId, const std::string &bundleName, int32_t instanceId) { CloudInfo cloudInfo = GetCloudInfo(userId); if (!cloudInfo.IsValid()) { @@ -424,7 +424,7 @@ void CloudServiceImpl::GetSchema(const Event &event) auto &storeInfo = rdbEvent.GetStoreInfo(); ZLOGD("Start GetSchema, bundleName:%{public}s, storeName:%{public}s, instanceId:%{public}d", storeInfo.bundleName.c_str(), storeInfo.storeName.c_str(), storeInfo.instanceId); - GetSchemaMata(storeInfo.user, storeInfo.bundleName, storeInfo.instanceId); + GetSchemaMeta(storeInfo.user, storeInfo.bundleName, storeInfo.instanceId); } bool CloudServiceImpl::DoSubscribe(int32_t user) diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h index 451c7923..adc9a664 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -68,7 +68,7 @@ private: bool UpdateCloudInfo(int32_t user); bool UpdateSchema(int32_t user); - std::vector GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId); + std::vector GetSchemaMeta(int32_t userId, const std::string &bundleName, int32_t instanceId); CloudInfo GetCloudInfo(int32_t userId); int32_t GetCloudInfo(uint32_t tokenId, const std::string &id, CloudInfo &cloudInfo); int32_t GetCloudInfoFromMeta(CloudInfo &cloudInfo); diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 49b00f79..88cad34f 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -243,9 +243,10 @@ void RdbServiceImpl::SyncerTimeout(std::shared_ptr syncer) if (syncer == nullptr) { return; } - ZLOGI("%{public}s", syncer->GetStoreId().c_str()); - syncers_.ComputeIfPresent(syncer->GetPid(), [this, &syncer](const auto& key, StoreSyncersType& syncers) { - syncers.erase(syncer->GetStoreId()); + auto storeId = syncer->GetStoreId(); + ZLOGI("%{public}s", storeId.c_str()); + syncers_.ComputeIfPresent(syncer->GetPid(), [this, storeId](const auto& key, StoreSyncersType& syncers) { + syncers.erase(storeId); syncerNum_--; return true; }); @@ -316,7 +317,7 @@ int32_t RdbServiceImpl::SetDistributedTables(const RdbSyncerParam ¶m, const if (syncer == nullptr) { return RDB_ERROR; } - return syncer->SetDistributedTables(tables); + return syncer->SetDistributedTables(tables, type); } std::pair RdbServiceImpl::DoSync(const RdbSyncerParam ¶m, const Option &option, diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp index 3b2e8596..ab450245 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -208,17 +208,17 @@ DistributedDB::RelationalStoreDelegate* RdbSyncer::GetDelegate() return delegate_; } -int32_t RdbSyncer::SetDistributedTables(const std::vector &tables) +int32_t RdbSyncer::SetDistributedTables(const std::vector &tables, int32_t type) { - auto* delegate = GetDelegate(); + auto *delegate = GetDelegate(); if (delegate == nullptr) { ZLOGE("delegate is nullptr"); return RDB_ERROR; } - for (const auto& table : tables) { + for (const auto &table : tables) { ZLOGI("%{public}s", table.c_str()); - auto dBStatus = delegate->CreateDistributedTable(table); + auto dBStatus = delegate->CreateDistributedTable(table, static_cast(type)); if (dBStatus != DistributedDB::DBStatus::OK) { ZLOGE("create distributed table failed, table:%{public}s, err:%{public}d", table.c_str(), dBStatus); return RDB_ERROR; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h index 5571f853..e3c6cc13 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -51,7 +51,7 @@ public: std::string GetIdentifier() const; - int32_t SetDistributedTables(const std::vector &tables); + int32_t SetDistributedTables(const std::vector &tables, int32_t type); int32_t DoSync(const Option &option, const RdbPredicates &predicates, const AsyncDetail &async); diff --git a/relational_store/frameworks/js/napi/cloud_data/include/js_error_utils.h b/relational_store/frameworks/js/napi/cloud_data/include/js_error_utils.h index c6567dfc..d3bb19a1 100644 --- a/relational_store/frameworks/js/napi/cloud_data/include/js_error_utils.h +++ b/relational_store/frameworks/js/napi/cloud_data/include/js_error_utils.h @@ -35,7 +35,7 @@ struct JsErrorCode { const std::optional GetJsErrorCode(int32_t errorCode); Status GenerateNapiError(int32_t status, int32_t &errCode, std::string &errMessage); -void ThrowNapiError(napi_env env, int32_t errCode, std::string errMessage, bool isParamsCheck = true); +void ThrowNapiError(napi_env env, int32_t errCode, const std::string &errMessage, bool isParamsCheck = true); napi_value GenerateErrorMsg(napi_env env, JsErrorCode jsInfo); #define ASSERT_ERR(env, assertion, errorCode, message) \ diff --git a/relational_store/frameworks/js/napi/cloud_data/src/js_error_utils.cpp b/relational_store/frameworks/js/napi/cloud_data/src/js_error_utils.cpp index e7f8ea51..246a5076 100644 --- a/relational_store/frameworks/js/napi/cloud_data/src/js_error_utils.cpp +++ b/relational_store/frameworks/js/napi/cloud_data/src/js_error_utils.cpp @@ -24,7 +24,8 @@ using JsErrorCode = OHOS::CloudData::JsErrorCode; static constexpr JsErrorCode JS_ERROR_CODE_MSGS[] = { { Status::INVALID_ARGUMENT, 401, "Parameter error." }, { Status::NOT_SUPPORT, 801, "Not support." }, - { Status::PERMISSION_DENIED, 201, "Permission denied." } + { Status::PERMISSION_DENIED, 202, "Permission denied, non-system app called system api." }, + { Status::CLOUD_CONFIG_PERMISSION_DENIED, 201, "Permission denied." } }; const std::optional GetJsErrorCode(int32_t errorCode) @@ -61,7 +62,7 @@ Status GenerateNapiError(int32_t status, int32_t &errCode, std::string &errMessa return static_cast(status); } -void ThrowNapiError(napi_env env, int32_t status, std::string errMessage, bool isParamsCheck) +void ThrowNapiError(napi_env env, int32_t status, const std::string &errMessage, bool isParamsCheck) { ZLOGD("ThrowNapiError message: %{public}s", errMessage.c_str()); if (status == Status::SUCCESS) { diff --git a/relational_store/frameworks/js/napi/common/include/js_utils.h b/relational_store/frameworks/js/napi/common/include/js_utils.h index 2679b024..57bf8585 100644 --- a/relational_store/frameworks/js/napi/common/include/js_utils.h +++ b/relational_store/frameworks/js/napi/common/include/js_utils.h @@ -27,6 +27,7 @@ #include "napi/native_api.h" #include "napi/native_common.h" #include "napi/native_node_api.h" +#include "rdb_types.h" namespace OHOS { namespace AppDataMgrJsKit { @@ -96,6 +97,11 @@ napi_value Convert2JSValue(napi_env env, int64_t value); napi_value Convert2JSValue(napi_env env, double value); napi_value Convert2JSValue(napi_env env, bool value); napi_value Convert2JSValue(napi_env env, const std::map &value); +napi_value Convert2JSValue(napi_env env, const DistributedRdb::Statistic &statistic); +napi_value Convert2JSValue(napi_env env, const DistributedRdb::TableDetail &tableDetail); +napi_value Convert2JSValue(napi_env env, const DistributedRdb::TableDetails &tableDetails); +napi_value Convert2JSValue(napi_env env, const DistributedRdb::ProgressDetail &progressDetail); +napi_value Convert2JSValue(napi_env env, const DistributedRdb::Details &details); napi_value Convert2JSValue(napi_env env, const std::monostate &value); template diff --git a/relational_store/frameworks/js/napi/common/src/js_utils.cpp b/relational_store/frameworks/js/napi/common/src/js_utils.cpp index 1d33b1c5..e0ec638d 100644 --- a/relational_store/frameworks/js/napi/common/src/js_utils.cpp +++ b/relational_store/frameworks/js/napi/common/src/js_utils.cpp @@ -442,6 +442,83 @@ napi_value JSUtils::Convert2JSValue(napi_env env, const std::map rdbPredicates = nullptr; @@ -1127,7 +1128,6 @@ napi_value RdbStoreProxy::CloudSync(napi_env env, napi_callback_info info) napi_typeof(env, argv[index], &valueType); CHECK_RETURN_SET_E(valueType == napi_function, std::make_shared("a callback type")); context->cloudSyncCallback = argv[index]; - }; auto exec = [context]() -> int { LOG_DEBUG("RdbStoreProxy::CloudSync Async"); @@ -1136,16 +1136,18 @@ napi_value RdbStoreProxy::CloudSync(napi_env env, napi_callback_info info) option.mode = static_cast(context->enumArg); option.isBlock = true; - - return obj->rdbStore_->Sync( - option, context->tablesNames, [context](const Details &details) { context->details = details; }); + return obj->rdbStore_->Sync(option, context->tablesNames, [context](const Details &details) { + auto callback = std::make_shared(context->env_, context->cloudSyncCallback); + callback->OnSyncCompelete(details); + }); }; auto output = [context](napi_env env, napi_value &result) { + LOG_DEBUG("RdbStoreProxy::CloudSync output"); napi_status status = napi_get_undefined(env, &result); CHECK_RETURN_SET_E(status == napi_ok, std::make_shared(E_ERROR)); - LOG_DEBUG("RdbStoreProxy::CloudSync end"); }; + context->SetAction(env, info, input, exec, output); CHECK_RETURN_NULL(context->error == nullptr || context->error->GetCode() == OK); @@ -1249,6 +1251,15 @@ napi_value RdbStoreProxy::OffEvent(napi_env env, napi_callback_info info) } return nullptr; } + +void RdbStoreProxy::NapiCoudSyncCallback::OnSyncCompelete(const DistributedRdb::Details &details) +{ + LOG_DEBUG("NapiCoudSyncCallback::OnSyncCompelete begin"); + CallFunction([details](napi_env env, int &argc, napi_value *argv) { + argc = 1; + argv[0] = details.empty() ? nullptr : JSUtils::Convert2JSValue(env, details.begin()->second); + }); +} #endif } // namespace RelationalStoreJsKit } // namespace OHOS diff --git a/relational_store/frameworks/native/rdb/include/rdb_store_impl.h b/relational_store/frameworks/native/rdb/include/rdb_store_impl.h index 29d8e75e..82074355 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_store_impl.h +++ b/relational_store/frameworks/native/rdb/include/rdb_store_impl.h @@ -126,8 +126,6 @@ private: std::map &&values, const std::string &table); const RdbStoreConfig rdbStoreConfig; SqliteConnectionPool *connectionPool; - static const int MAX_IDLE_SESSION_SIZE = 5; - std::mutex sessionMutex; bool isOpen; std::string path; std::string orgPath; @@ -135,7 +133,6 @@ private: bool isMemoryRdb; std::string name; std::string fileType; - std::stack transactionObserverStack; DistributedRdb::RdbSyncerParam syncerParam_; bool isEncrypt_; std::shared_ptr pool_; diff --git a/relational_store/frameworks/native/rdb/mock/include/rdb_store_impl.h b/relational_store/frameworks/native/rdb/mock/include/rdb_store_impl.h index 5873b031..3d429dff 100644 --- a/relational_store/frameworks/native/rdb/mock/include/rdb_store_impl.h +++ b/relational_store/frameworks/native/rdb/mock/include/rdb_store_impl.h @@ -104,8 +104,6 @@ private: const RdbStoreConfig rdbStoreConfig; SqliteConnectionPool *connectionPool; - static const int MAX_IDLE_SESSION_SIZE = 5; - std::mutex sessionMutex; bool isOpen; std::string path; std::string orgPath; @@ -113,7 +111,6 @@ private: bool isMemoryRdb; std::string name; std::string fileType; - std::stack transactionObserverStack; bool isEncrypt_; }; } // namespace OHOS::NativeRdb diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp index e661ff0e..559a26d6 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -101,7 +101,7 @@ int RdbStoreImpl::InnerOpen(const RdbStoreConfig &config) pool_->Execute([param]() { auto [err, service] = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(param); if (err != E_OK || service == nullptr) { - LOG_ERROR("GetRdbService failed, err is %{public}d.", err); + LOG_WARN("GetRdbService failed, err is %{public}d.", err); return; } err = service->GetSchema(param); @@ -1105,7 +1105,7 @@ std::shared_ptr RdbStoreImpl::QueryByStep(const std::string &sql, std return std::make_shared(connectionPool, sql, std::move(args)); } -#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) int RdbStoreImpl::SetDistributedTables(const std::vector &tables, int32_t type) { DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__)); diff --git a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp index 689631ea..f10accea 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp @@ -85,18 +85,18 @@ bool Marshalling(const ValueObject &input, MessageParcel &data) { return Marshal(data, input.value); } -template<> +template<> bool Unmarshalling(ValueObject &output, MessageParcel &data) { return Unmarshal(data, output.value); } -template<> +template<> bool Marshalling(const ValuesBucket &input, MessageParcel &data) { return Marshal(data, input.values_); } -template<> +template<> bool Unmarshalling(ValuesBucket &output, MessageParcel &data) { return Unmarshal(data, output.values_); diff --git a/relational_store/interfaces/inner_api/rdb/BUILD.gn b/relational_store/interfaces/inner_api/rdb/BUILD.gn index 2c6f0e72..ec021890 100644 --- a/relational_store/interfaces/inner_api/rdb/BUILD.gn +++ b/relational_store/interfaces/inner_api/rdb/BUILD.gn @@ -111,6 +111,7 @@ if (is_ohos) { "${relational_store_native_path}/rdb/src/share_block.cpp", "${relational_store_native_path}/rdb/src/shared_block_serializer_info.cpp", "${relational_store_native_path}/rdb/src/sqlite_shared_result_set.cpp", + "${relational_store_native_path}/rdb/src/task_executor.cpp", "${relational_store_native_path}/rdb_device_manager_adapter/src/rdb_device_manager_adapter.cpp", ] @@ -157,6 +158,7 @@ if (is_ohos) { "${relational_store_native_path}/rdb/src/share_block.cpp", "${relational_store_native_path}/rdb/src/shared_block_serializer_info.cpp", "${relational_store_native_path}/rdb/src/sqlite_shared_result_set.cpp", + "${relational_store_native_path}/rdb/src/task_executor.cpp", "${relational_store_native_path}/rdb_device_manager_adapter/src/rdb_device_manager_adapter.cpp", ] @@ -235,7 +237,8 @@ if (is_ohos) { deps = base_deps - sources += [ "${relational_store_native_path}/rdb/mock/src/timer.cpp" ] + sources += + [ "${relational_store_native_path}/rdb/mock/src/task_executor.cpp" ] deps += [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_windows", @@ -255,7 +258,8 @@ if (is_ohos) { deps = base_deps - sources += [ "${relational_store_native_path}/rdb/mock/src/timer.cpp" ] + sources += + [ "${relational_store_native_path}/rdb/mock/src/task_executor.cpp" ] deps += [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_windows", @@ -324,7 +328,8 @@ if (is_ohos) { deps = base_deps - sources += [ "${relational_store_native_path}/rdb/mock/src/timer.cpp" ] + sources += + [ "${relational_store_native_path}/rdb/mock/src/task_executor.cpp" ] deps += [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_mac", "//third_party/sqlite:sqlite_sdk", @@ -343,7 +348,8 @@ if (is_ohos) { deps = base_deps - sources += [ "${relational_store_native_path}/rdb/mock/src/timer.cpp" ] + sources += + [ "${relational_store_native_path}/rdb/mock/src/task_executor.cpp" ] deps += [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_mac", "//third_party/sqlite:sqlite_sdk", @@ -405,7 +411,8 @@ if (is_ohos) { deps = base_deps - sources += [ "${relational_store_native_path}/rdb/mock/src/timer.cpp" ] + sources += + [ "${relational_store_native_path}/rdb/mock/src/task_executor.cpp" ] deps += [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_android", "//commonlibrary/c_utils/base:utils", @@ -467,7 +474,8 @@ if (is_ohos) { deps = base_deps - sources += [ "${relational_store_native_path}/rdb/mock/src/timer.cpp" ] + sources += + [ "${relational_store_native_path}/rdb/mock/src/task_executor.cpp" ] deps += [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_ios", "//commonlibrary/c_utils/base:utilsbase", diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_types.h b/relational_store/interfaces/inner_api/rdb/include/rdb_types.h index 19e56b2e..373e2932 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_types.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_types.h @@ -88,14 +88,15 @@ struct TableDetail { Statistic download; }; +using TableDetails = std::map; + struct ProgressDetail { int32_t progress; int32_t code; - std::map details; + TableDetails details; }; using SyncResult = std::map; -using SyncCallback = std::function; using Briefs = std::map; using Details = std::map; using AsyncBrief = std::function; -- Gitee From 4abc5c6c93d239973132bedace4f45840d2d3bb5 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Thu, 1 Jun 2023 21:44:54 +0800 Subject: [PATCH 3/4] merge Signed-off-by: htt1997 --- .../frameworks/js/napi/cloud_data/src/js_error_utils.cpp | 4 ---- .../frameworks/native/rdb/mock/include/task_executor.h | 6 ------ 2 files changed, 10 deletions(-) diff --git a/relational_store/frameworks/js/napi/cloud_data/src/js_error_utils.cpp b/relational_store/frameworks/js/napi/cloud_data/src/js_error_utils.cpp index 1757efaf..59092fa5 100644 --- a/relational_store/frameworks/js/napi/cloud_data/src/js_error_utils.cpp +++ b/relational_store/frameworks/js/napi/cloud_data/src/js_error_utils.cpp @@ -24,11 +24,7 @@ using JsErrorCode = OHOS::CloudData::JsErrorCode; static constexpr JsErrorCode JS_ERROR_CODE_MSGS[] = { { Status::INVALID_ARGUMENT, 401, "Parameter error." }, { Status::NOT_SUPPORT, 801, "Not support." }, -<<<<<<< HEAD - { Status::PERMISSION_DENIED, 202, "Permission denied, non-system app called system api." }, -======= { Status::PERMISSION_DENIED, 202, "Permission denied,non-system app called system api." }, ->>>>>>> 79bb994d76d9845783ccfbdd151155bb0503f575 { Status::CLOUD_CONFIG_PERMISSION_DENIED, 201, "Permission denied." } }; diff --git a/relational_store/frameworks/native/rdb/mock/include/task_executor.h b/relational_store/frameworks/native/rdb/mock/include/task_executor.h index 4f41c6b2..9ed65483 100644 --- a/relational_store/frameworks/native/rdb/mock/include/task_executor.h +++ b/relational_store/frameworks/native/rdb/mock/include/task_executor.h @@ -23,13 +23,7 @@ public: using TaskId = uint64_t; using Task = std::function; using Duration = std::chrono::steady_clock::duration; -<<<<<<< HEAD static constexpr TaskId INVALID_TASK_ID = static_cast(0l); -======= - static constexpr Duration INVALID_DURATION = std::chrono::milliseconds(0); - static constexpr TaskId INVALID_TASK_ID = static_cast(0l); - static constexpr uint64_t UNLIMITED_TIMES = std::numeric_limits::max(); ->>>>>>> 79bb994d76d9845783ccfbdd151155bb0503f575 class ExecutorPool { public: ExecutorPool(size_t max, size_t min) -- Gitee From de918b93fe456bad15a1db7caeabb61723bf1dbb Mon Sep 17 00:00:00 2001 From: htt1997 Date: Thu, 1 Jun 2023 21:46:58 +0800 Subject: [PATCH 4/4] update Signed-off-by: htt1997 --- .../frameworks/js/napi/common/src/js_utils.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/relational_store/frameworks/js/napi/common/src/js_utils.cpp b/relational_store/frameworks/js/napi/common/src/js_utils.cpp index 4733782f..aaaef0ed 100644 --- a/relational_store/frameworks/js/napi/common/src/js_utils.cpp +++ b/relational_store/frameworks/js/napi/common/src/js_utils.cpp @@ -457,6 +457,7 @@ napi_value JSUtils::Convert2JSValue(napi_env env, const DistributedRdb::Statisti napi_set_named_property(env, jsValue, "success", success); napi_set_named_property(env, jsValue, "failed", failed); napi_set_named_property(env, jsValue, "untreated", untreated); + return jsValue; } napi_value JSUtils::Convert2JSValue(napi_env env, const DistributedRdb::TableDetail &tableDetail) { @@ -466,11 +467,10 @@ napi_value JSUtils::Convert2JSValue(napi_env env, const DistributedRdb::TableDet return nullptr; } napi_value upload = Convert2JSValue(env, tableDetail.upload); - napi_set_named_property(env, jsValue, "upload", upload); napi_value download = Convert2JSValue(env, tableDetail.download); + napi_set_named_property(env, jsValue, "upload", upload); napi_set_named_property(env, jsValue, "download", download); - - return nullptr; + return jsValue; } napi_value JSUtils::Convert2JSValue(napi_env env, const DistributedRdb::TableDetails &tableDetails) { @@ -491,7 +491,6 @@ napi_value JSUtils::Convert2JSValue(napi_env env, const DistributedRdb::TableDet napi_set_element(env, jsElement, 1, Convert2JSValue(env, result)); napi_set_element(env, jsValue, index++, jsElement); } - return jsValue; } napi_value JSUtils::Convert2JSValue(napi_env env, const DistributedRdb::ProgressDetail &progressDetail) @@ -510,6 +509,7 @@ napi_value JSUtils::Convert2JSValue(napi_env env, const DistributedRdb::Progress napi_set_named_property(env, jsValue, "schedule", schedule); napi_set_named_property(env, jsValue, "code", code); napi_set_named_property(env, jsValue, "details", details); + return jsValue; } napi_value JSUtils::Convert2JSValue(napi_env env, const DistributedRdb::Details &details) -- Gitee