From 4d605e8d9ca6c92b4dd9e47f3a3f3239e8953596 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Wed, 8 Mar 2023 09:47:25 +0000 Subject: [PATCH] Add grd document open Signed-off-by: lianhuix --- .../gaussdb_rd_Simple/CMakeLists.txt | 197 +++++++++++++++++- .../src/common/include/doc_common.h | 26 +++ .../src/common/include/doc_errno.h | 26 +++ .../src/excutor/base/grd_db_api.c | 0 .../src/excutor/document/grd_document_api.c | 0 .../src/executor/base/grd_db_api.cpp | 26 +++ .../document/grd_document_api.cpp} | 0 .../src/include/document_store_delegate.h | 0 .../src/interface/include/document_store.h | 30 +++ .../include/document_store_manager.h | 28 +++ .../src/interface/src/document_store.cpp | 26 +++ .../interface/src/document_store_manager.cpp | 28 +++ .../oh_adapter/include/kv_store_executor.h | 31 +++ .../src/oh_adapter/include/kv_store_manager.h | 29 +++ .../src/oh_adapter/src/kv_store_manager.cpp | 48 +++++ .../src/sqlite_store_executor_impl.cpp | 36 ++++ .../src/sqlite_store_executor_impl.h | 35 ++++ .../unittest/api/doucumentdb_api_test.cpp | 59 ++++++ 18 files changed, 624 insertions(+), 1 deletion(-) create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_errno.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/excutor/base/grd_db_api.c delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/excutor/document/grd_document_api.c create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_db_api.cpp rename services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/{common/include/doc_error.h => executor/document/grd_document_api.cpp} (100%) delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/include/document_store_delegate.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store_manager.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store_manager.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_executor.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_manager.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/kv_store_manager.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/doucumentdb_api_test.cpp diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt index 1c459e3c..0c3aa66d 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt @@ -1,3 +1,198 @@ cmake_minimum_required(VERSION 3.2) -project(grd_simple VERSION 1.0.0) \ No newline at end of file +project(grd_simple VERSION 1.0.0) + + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++17 -pthread") + +function(download_repo repo_name repo_url) + execute_process( + COMMAND [ -d ${repo_name} ] + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party + RESULT_VARIABLE REPO_EXISTS + ) + message(STATUS "check ${repo_name} exists result: ${REPO_EXISTS}") + if (NOT (${REPO_EXISTS} EQUAL 0)) + message(STATUS "Downloading ${repo_name} from ${repo_url}") + execute_process( + COMMAND git clone ${repo_url} + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party + ) + message(STATUS "Download ${repo_name} finished") + endif() +endfunction(download_repo) + +function(build_repo repo_name script_path) + execute_process( + COMMAND [ -f ${repo_name}/Makefile ] + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party + RESULT_VARIABLE SCRIPT_EXISTS + ) + if (NOT (${SCRIPT_EXISTS} EQUAL 0)) + message(STATUS "Copy build script ${script_path}/Makefile to repo ${repo_name}") + execute_process( + COMMAND cp third_party/kate/${script_path}/Makefile third_party/${repo_name} -f + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ) + message(STATUS "Build ${repo_name}") + execute_process( + COMMAND make -j9 + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/${repo_name} + ) + message(STATUS "Build finished") + endif() +endfunction(build_repo) + +function(cmake_repo repo_name) + execute_process( + COMMAND [ -d ${repo_name}/build ] + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party + RESULT_VARIABLE BUILD_FINISHED + ) + if (NOT (${BUILD_FINISHED} EQUAL 0)) + message(STATUS "compile repo ${repo_name}") + execute_process( + COMMAND mkdir -p build + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/${repo_name} + ) + execute_process( + COMMAND cmake .. + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/${repo_name}/build + ) + execute_process( + COMMAND make -j9 + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/${repo_name}/build + ) + endif() +endfunction(cmake_repo) + +function(build_openssl repo_name) + execute_process( + COMMAND [ -f Makefile ] + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/${repo_name} + RESULT_VARIABLE BUILD_FINISHED + ) + if (NOT (${BUILD_FINISHED} EQUAL 0)) + message(STATUS "compile repo ${repo_name}") + execute_process( + COMMAND chmod +x ./config + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/${repo_name} + ) + execute_process( + COMMAND ./config + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/${repo_name} + ) + execute_process( + COMMAND make -j9 + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/${repo_name} + ) + endif() +endfunction(build_openssl) + +# create third_party directory +execute_process( + COMMAND mkdir -p ${PROJECT_SOURCE_DIR}/third_party + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} +) + +# Download dependencies +download_repo("third_party_cJSON" "https://gitee.com/openharmony/third_party_cJSON.git") +download_repo("third_party_googletest" "https://gitee.com/openharmony/third_party_googletest.git") +download_repo("third_party_openssl" "https://gitee.com/openharmony/third_party_openssl.git") +download_repo("third_party_sqlite" "https://gitee.com/openharmony/third_party_sqlite.git") +download_repo("utils_native" "https://gitee.com/openharmony/utils_native.git") +download_repo("kate" "https://gitee.com/lianhuix/kate.git") + +# build openssl Makefile +build_openssl("third_party_openssl") + +# build securec +build_repo("utils_native" "securec") + +# build sqlite +build_repo("third_party_sqlite" "sqlite") + +#build googletest +execute_process( + COMMAND sed -i /Wshadow/d internal_utils.cmake + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/third_party_googletest/googletest/cmake +) +execute_process( + COMMAND sed -i "s%testing::GTEST_FLAG(output)%// testing::GTEST_FLAG(output)%g" googlemock/src/gmock_main.cc + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/third_party_googletest +) +execute_process( + COMMAND sed -i "s%testing::GTEST_FLAG(output)%// testing::GTEST_FLAG(output)%g" googletest/src/gtest_main.cc + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/third_party_googletest +) +cmake_repo("third_party_googletest") + +# build cJSON +cmake_repo("third_party_cJSON" "cJSON") + +find_package(Threads) +enable_testing() + +set(DB_PATH ${PROJECT_SOURCE_DIR}/frameworks/libs/distributeddb) + +file(GLOB_RECURSE DOCUMENT_SRC "${PROJECT_SOURCE_DIR}/src/*.cpp") +file(GLOB_RECURSE DOCUMENT_TEST "${PROJECT_SOURCE_DIR}/test/*.cpp") + +include_directories( + ${PROJECT_SOURCE_DIR}/include + ${PROJECT_SOURCE_DIR}/src/storage + ${PROJECT_SOURCE_DIR}/src/storage/include + ${PROJECT_SOURCE_DIR}/src/storage/src + ${PROJECT_SOURCE_DIR}/src/common + ${PROJECT_SOURCE_DIR}/src/common/include + ${PROJECT_SOURCE_DIR}/src/executor + ${PROJECT_SOURCE_DIR}/src/executor/base + ${PROJECT_SOURCE_DIR}/src/executor/document + ${PROJECT_SOURCE_DIR}/src/oh_adapter + ${PROJECT_SOURCE_DIR}/src/oh_adapter/include + ${PROJECT_SOURCE_DIR}/src/oh_adapter/src + ${PROJECT_SOURCE_DIR}/src/interface + ${PROJECT_SOURCE_DIR}/src/interface/include + ${PROJECT_SOURCE_DIR}/src/interface/src +) + +set(DISTRIBUTEDDB_PATH ${PROJECT_SOURCE_DIR}/third_party/distributeddatamgr_kv_store/frameworks/libs/distributeddb/) +include_directories( + ${DISTRIBUTEDDB_PATH}/include + ${DISTRIBUTEDDB_PATH}/interfaces/include +) + +include_directories( + ${PROJECT_SOURCE_DIR}/third_party/third_party_googletest/googletest/include + ${PROJECT_SOURCE_DIR}/third_party/third_party_googletest/googlemock/include + ${PROJECT_SOURCE_DIR}/third_party/third_party_cJSON + ${PROJECT_SOURCE_DIR}/third_party/third_party_sqlite/include + ${PROJECT_SOURCE_DIR}/third_party/utils_native/base/include + ${PROJECT_SOURCE_DIR}/third_party/utils_native/base/src/securec + ${PROJECT_SOURCE_DIR}/third_party/third_party_openssl/include +) + +link_directories( + ${PROJECT_SOURCE_DIR}/third_party/third_party_googletest/build/lib + ${PROJECT_SOURCE_DIR}/third_party/third_party_cJSON/build + ${PROJECT_SOURCE_DIR}/third_party/third_party_openssl + ${PROJECT_SOURCE_DIR}/third_party/third_party_sqlite + ${PROJECT_SOURCE_DIR}/third_party/utils_native +) + +add_executable(document_ut ${DOCUMENT_SRC} ${DOCUMENT_TEST}) + +target_link_libraries(document_ut + cjson + + sqlite3 + securec + ssl + crypto + + gtest + gtest_main + gmock + gmock_main +) + diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h new file mode 100644 index 00000000..a4919b67 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h @@ -0,0 +1,26 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef DOC_COMMON_H +#define DOC_COMMON_H + +#include +#include + +namespace DocumentDB { +using Key = std::vector; +using Value = std::vector; +} // DocumentDB +#endif // DOC_COMMON_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_errno.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_errno.h new file mode 100644 index 00000000..73fecffa --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_errno.h @@ -0,0 +1,26 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef DOC_ERRNO_H +#define DOC_ERRNO_H + + +namespace DocumentDB { +constexpr int E_OK = 0; +constexpr int E_ERROR = 1; + + +} // DocumentDB +#endif // DOC_ERRNO_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/excutor/base/grd_db_api.c b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/excutor/base/grd_db_api.c deleted file mode 100644 index e69de29b..00000000 diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/excutor/document/grd_document_api.c b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/excutor/document/grd_document_api.c deleted file mode 100644 index e69de29b..00000000 diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_db_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_db_api.cpp new file mode 100644 index 00000000..abed4f20 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_db_api.cpp @@ -0,0 +1,26 @@ + +#include "grd_base/grd_db_api.h" +#include "grd_base/grd_error.h" +#include "document_store_manager.h" +#include "document_store.h" + +using namespace DocumentDB; + +typedef struct GRD_DB { + DocumentStore *store_ = nullptr; +} GRD_DB; + +int GRD_DBOpen(const char *dbPath, const char *configStr, unsigned int flags, GRD_DB **db) +{ + std::string path = dbPath; + DocumentStore *store = nullptr; + DocumentStoreManager::GetDocumentStore(path, store); + *db = new (std::nothrow) GRD_DB(); + (*db)->store_ = store; + return GRD_OK; +} + +int GRD_DBClose(GRD_DB *db, unsigned int flags) +{ + return GRD_OK; +} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_error.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/grd_document_api.cpp similarity index 100% rename from services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_error.h rename to services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/grd_document_api.cpp diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/include/document_store_delegate.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/include/document_store_delegate.h deleted file mode 100644 index e69de29b..00000000 diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store.h new file mode 100644 index 00000000..52bb3c29 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store.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 DOCUMENT_STORE_H +#define DOCUMENT_STORE_H + +#include "kv_store_executor.h" + +namespace DocumentDB { +class DocumentStore { +public: + DocumentStore(KvStoreExecutor *); + ~DocumentStore(); +private: + KvStoreExecutor *executor_ = nullptr; +}; +} // DocumentDB +#endif // DOCUMENT_STORE_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store_manager.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store_manager.h new file mode 100644 index 00000000..c5bb1f2d --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store_manager.h @@ -0,0 +1,28 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef DOCUMENT_STORE_MANAGER_H +#define DOCUMENT_STORE_MANAGER_H + +#include +#include "document_store.h" + +namespace DocumentDB { +class DocumentStoreManager { +public: + static int GetDocumentStore(const std::string &path, DocumentStore *&delegate); +}; +} // DocumentDB +#endif // DOCUMENT_STORE_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store.cpp new file mode 100644 index 00000000..04d87423 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store.cpp @@ -0,0 +1,26 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "document_store.h" + +namespace DocumentDB { +DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) +{ +} + +DocumentStore::~DocumentStore() +{ +} +} // DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store_manager.cpp new file mode 100644 index 00000000..080ad0d1 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store_manager.cpp @@ -0,0 +1,28 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "doc_errno.h" +#include "document_store_manager.h" +#include "kv_store_manager.h" + +namespace DocumentDB { +int DocumentStoreManager::GetDocumentStore(const std::string &path, DocumentStore *&store) +{ + KvStoreExecutor *executor = nullptr; + KvStoreManager::GetKvStore(path, executor); + store = new (std::nothrow) DocumentStore(executor); + return E_OK; +} +} // DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_executor.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_executor.h new file mode 100644 index 00000000..526c32a5 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_executor.h @@ -0,0 +1,31 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef KV_STORE_EXECUTOR_H +#define KV_STORE_EXECUTOR_H + +#include "doc_common.h" + +namespace DocumentDB { +class KvStoreExecutor { +public: + virtual ~KvStoreExecutor() = default; + + virtual int PutData(const Key &key, const Value &value) = 0; + + virtual int GetData(const Key &key, Value &value) const = 0; +}; +} // DocumentDB +#endif // KV_STORE_EXECUTOR_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_manager.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_manager.h new file mode 100644 index 00000000..6c82e983 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_manager.h @@ -0,0 +1,29 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef KV_STORE_MANAGER_H +#define KV_STORE_MANAGER_H +#include + +#include "doc_common.h" +#include "kv_store_executor.h" + +namespace DocumentDB { +class KvStoreManager { +public: + static int GetKvStore(const std::string &path, KvStoreExecutor *&executor); +}; +} // DocumentDB +#endif // KV_STORE_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/kv_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/kv_store_manager.cpp new file mode 100644 index 00000000..d4c91bcd --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/kv_store_manager.cpp @@ -0,0 +1,48 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "doc_errno.h" +#include "kv_store_manager.h" +#include "sqlite_store_executor_impl.h" + +namespace DocumentDB { +constexpr const char *APP_ID = "APP_ID"; +constexpr const char *USER_ID = "USER_ID"; +constexpr const char *STORE_ID = "STORE_ID"; + + +sqlite3 *CreateDataBase(const std::string &dbUri) +{ + sqlite3 *db = nullptr; + if (int r = sqlite3_open_v2(dbUri.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr) != SQLITE_OK) { + if (db != nullptr) { + (void)sqlite3_close_v2(db); + db = nullptr; + } + } + return db; +} + +int KvStoreManager::GetKvStore(const std::string &path, KvStoreExecutor *&executor) +{ + sqlite3 *db = CreateDataBase(path); + if (db == nullptr) { + return -E_ERROR; + } + + executor = new (std::nothrow) SqliteStoreExecutor(db); + return E_OK; +} +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp new file mode 100644 index 00000000..58b92d17 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -0,0 +1,36 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "doc_errno.h" +#include "sqlite_store_executor_impl.h" + +namespace DocumentDB { +SqliteStoreExecutor::SqliteStoreExecutor(sqlite3 *handle) : dbHandle_(handle) +{ +} + +SqliteStoreExecutor::~SqliteStoreExecutor() +{ +} + +int SqliteStoreExecutor::PutData(const Key &key, const Value &value) +{ + return E_OK; +} + +int SqliteStoreExecutor::GetData(const Key &key, Value &value) const +{ + return E_OK; +} +} // DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.h new file mode 100644 index 00000000..731a8121 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.h @@ -0,0 +1,35 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef SQLITE_STORE_EXECUTOR_IMPL_H +#define SQLITE_STORE_EXECUTOR_IMPL_H + +#include "kv_store_executor.h" +#include "sqlite3.h" + +namespace DocumentDB { +class SqliteStoreExecutor : public KvStoreExecutor { +public: + SqliteStoreExecutor(sqlite3 *handle); + ~SqliteStoreExecutor() override; + + int PutData(const Key &key, const Value &value) override; + int GetData(const Key &key, Value &value) const override; + +private: + sqlite3 *dbHandle_ = nullptr; +}; +} // DocumentDB +#endif // SQLITE_STORE_EXECUTOR_IMPL_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/doucumentdb_api_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/doucumentdb_api_test.cpp new file mode 100644 index 00000000..7fed10ca --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/doucumentdb_api_test.cpp @@ -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. +*/ + +#include + +#include "grd_base/grd_db_api.h" +using namespace testing::ext; + +class DocumentDBApiTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DocumentDBApiTest::SetUpTestCase(void) +{ +} + +void DocumentDBApiTest::TearDownTestCase(void) +{ +} + +void DocumentDBApiTest::SetUp(void) +{ +} + +void DocumentDBApiTest::TearDown(void) +{ +} + + +/** + * @tc.name: OpenDBTest001 + * @tc.desc: Test open document db + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBTest001, TestSize.Level1) +{ + std::string path = "./document.db"; + GRD_DB *db = nullptr; + GRD_DBOpen(path.c_str(), nullptr, 0, &db); + EXPECT_NE(db, nullptr); +} \ No newline at end of file -- Gitee