From 53eb52648e250aad6a8df6f528c35fd86e0a0ad8 Mon Sep 17 00:00:00 2001 From: YOUR_NAME <1257421253@qq.com> Date: Wed, 8 Sep 2021 12:43:35 +0000 Subject: [PATCH] s --- .../distributeddata/include/kv_manager.h | 1 + .../distributeddata/include/single_kv_store.h | 1 + .../distributeddata/src/kv_manager.cpp | 62 ++++++++++++++++++- .../distributeddata/src/single_kv_store.cpp | 32 ++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/frameworks/jskitsimpl/distributeddata/include/kv_manager.h b/frameworks/jskitsimpl/distributeddata/include/kv_manager.h index be8751a1b..9264a46b1 100644 --- a/frameworks/jskitsimpl/distributeddata/include/kv_manager.h +++ b/frameworks/jskitsimpl/distributeddata/include/kv_manager.h @@ -25,6 +25,7 @@ class KVManager { public: static napi_value CreateKVManager(napi_env env, napi_callback_info info); static napi_value GetKVStore(napi_env env, napi_callback_info info); + static napi_value DeleteKVStore(napi_env env, napi_callback_info info); private: static napi_value GetCtor(napi_env env); static napi_value Initialize(napi_env env, napi_callback_info info); diff --git a/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h b/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h index 47c0b990d..335418f81 100644 --- a/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h +++ b/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h @@ -35,6 +35,7 @@ public: static napi_value Put(napi_env env, napi_callback_info info); static napi_value Get(napi_env env, napi_callback_info info); static napi_value Delete(napi_env env, napi_callback_info info); + static napi_value PutBatch(napi_env env, napi_callback_info info); private: enum JSSubscribeType { SUBSCRIBE_LOCAL = 0, diff --git a/frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp b/frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp index a0e35405f..96dd9375b 100644 --- a/frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/kv_manager.cpp @@ -122,7 +122,8 @@ napi_value KVManager::GetCtor(napi_env env) } napi_property_descriptor clzDes[] = { - DECLARE_NAPI_METHOD("getKVStore", GetKVStore) + DECLARE_NAPI_METHOD("getKVStore", GetKVStore), + DECLARE_NAPI_METHOD("deleteKVStore", DeleteKVStore), }; NAPI_CALL(env, napi_define_class(env, "KVManager", NAPI_AUTO_LENGTH, Initialize, nullptr, sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons)); @@ -151,4 +152,63 @@ napi_value KVManager::Initialize(napi_env env, napi_callback_info info) } return self; } + +napi_value KVManager::DeleteKVStore(napi_env env, napi_callback_info info) +{ + ZLOGD("delete kv store!"); + struct ContextInfo { + std::string appId; + std::string storeId; + Options options; + KVManager *proxy = nullptr; + SingleKVStore *kvStore = nullptr; + napi_ref ref = nullptr; + napi_env env = nullptr; + ~ContextInfo() + { + if (env != nullptr) { + napi_delete_reference(env, ref); + } + } + }; + auto ctxInfo = std::make_shared(); + auto input = [ctxInfo](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { + ZLOGD("DeleteKVStore parser to native params %{public}d!", argc); + NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); + NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast(&ctxInfo->proxy)), napi_invalid_arg); + NAPI_ASSERT_BASE(env, ctxInfo->proxy != nullptr, "there is no native kv store", napi_invalid_arg); + //ctxInfo->appId = JSUtil::Convert2String(env, argv[0]); + ctxInfo->storeId = JSUtil::Convert2String(env, argv[0]); + ctxInfo->options = JSUtil::Convert2Options(env, argv[1]); + napi_value kvStore = nullptr; + napi_status status = napi_new_instance(env, SingleKVStore::GetCtor(env), argc, argv, &kvStore); + napi_unwrap(env, kvStore, reinterpret_cast(&ctxInfo->kvStore)); + if (ctxInfo->kvStore == nullptr) { + return napi_invalid_arg; + } + napi_create_reference(env, kvStore, 1, &(ctxInfo->ref)); + return status; + }; + auto output = [ctxInfo](napi_env env, napi_value *result) -> napi_status { + if (*(ctxInfo->kvStore) == nullptr) { + ZLOGE("delete kv store failed!"); + *result = nullptr; + return napi_object_expected; + } + ZLOGD("delete kv store success!"); + return napi_get_reference_value(env, ctxInfo->ref, result); + }; + auto exec = [ctxInfo](AsyncCall::Context *ctx) { + ctxInfo->proxy->kvDataManager_.GetSingleKvStore( + //{ctxInfo->appId}, + ctxInfo->options, {ctxInfo->proxy->bundleName_},{ctxInfo->storeId}, + [ctxInfo](Status, std::unique_ptr kvStore) { + *(ctxInfo->kvStore) = std::move(kvStore); + }); + }; + auto context = std::make_shared(input, output); + AsyncCall asyncCall(env, info, context, 2); + return asyncCall.Call(env, exec); +} + } diff --git a/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp b/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp index 0150561fa..42cee0b1a 100644 --- a/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp @@ -28,6 +28,11 @@ std::map SingleKVStore::eventHandlers_ = { {"syncComplete", SingleKVStore::OnSyncComplete}, }; + std::map SingleKVStore::offeventHandlers_ = { + {"dataChange", SingleKVStore::OffDataChange}, + {"syncComplete", SingleKVStore::OffSyncComplete}, + +}; SingleKVStore::~SingleKVStore() { if (kvStore_ == nullptr) { @@ -60,6 +65,7 @@ napi_value SingleKVStore::GetCtor(napi_env env) DECLARE_NAPI_METHOD("delete", SingleKVStore::Delete), DECLARE_NAPI_METHOD("on", SingleKVStore::OnEvent), DECLARE_NAPI_METHOD("sync", SingleKVStore::Sync), + DECLARE_NAPI_METHOD("putBatch",SingleKVStore::PutBatch), }; napi_value cons; NAPI_CALL(env, napi_define_class(env, "SingleKVStore", NAPI_AUTO_LENGTH, Initialize, nullptr, @@ -329,4 +335,30 @@ void SyncObserver::SyncCompleted(const std::map= 1, "args is out of range", nullptr); + NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", nullptr); + SingleKVStore *proxy = nullptr; + NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast(&proxy)), nullptr); + NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native kv store", nullptr); + + std::vector devices = JSUtil::Convert2StringArray(env, argv[0]); + int32_t mode = int32_t(SyncMode::PUSH_PULL); + napi_get_value_int32(env, argv[1], &mode); + uint32_t delay = 0; + if (argc >= 3) { + napi_get_value_uint32(env, argv[2], &delay); + } + ZLOGD("sync data %{public}d, mode:%{public}d, devices:%{public}d", argc, mode, devices.size()); + + Status status = proxy->kvStore_->Sync(devices, static_cast(mode), delay); + NAPI_ASSERT_BASE(env, status == Status::SUCCESS, "call sync failed", nullptr); + return nullptr; +} } -- Gitee