From 73b2c6904d4d67a19df658f47d5b43dd7ffad1f2 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Sat, 11 Mar 2023 08:56:01 +0000 Subject: [PATCH 1/2] Add close document store Signed-off-by: lianhuix --- .../gaussdb_rd_Simple/CMakeLists.txt | 8 +++++- .../include/grd_base/grd_error.h | 3 +++ .../include/grd_base/grd_type_export.h | 19 +++++++++++++ .../src/executor/base/grd_db_api.cpp | 27 +++++++++++++++++++ .../{common => interface}/include/doc_errno.h | 0 .../include/document_store_manager.h | 9 ++++++- .../src/interface/src/document_store.cpp | 1 + .../interface/src/document_store_manager.cpp | 11 ++++++++ .../src/sqlite_store_executor_impl.cpp | 2 ++ .../unittest/api/doucumentdb_api_test.cpp | 10 +++++-- 10 files changed, 86 insertions(+), 4 deletions(-) rename services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/{common => interface}/include/doc_errno.h (100%) 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 0c3aa66d..1d7c2a72 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt @@ -2,9 +2,15 @@ cmake_minimum_required(VERSION 3.2) project(grd_simple VERSION 1.0.0) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++17 -pthread") +# Address Sanitizer +option(USE_ASAN "Compile with address sanitiser" OFF) +if (USE_ASAN) + message(STATUS "Compile with address sanitiser ${USE_ASAN}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -O1") +endif() + function(download_repo repo_name repo_url) execute_process( COMMAND [ -d ${repo_name} ] diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_error.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_error.h index 0abe8e96..007d13aa 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_error.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_error.h @@ -28,6 +28,9 @@ extern "C" { #define GRD_OVER_LIMIT (-2000) #define GRD_INVALID_ARGS (-3000) #define GRD_SYSTEM_ERR (-4000) +#define GRD_INNER_ERR (-8000) +#define GRD_RESOURCE_BUSY (-9000) + #define GRD_NO_DATA (-11000) #define GRD_FAILED_MEMORY_ALLOCATE (-13000) #define GRD_FAILED_MEMORY_RELEASE (-14000) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_type_export.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_type_export.h index 602b0585..352d2162 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_type_export.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_type_export.h @@ -22,11 +22,30 @@ extern "C" { typedef struct GRD_DB GRD_DB; +/** + * @brief Open database config + */ +#define GRD_DB_OPEN_ONLY 0x00 +#define GRD_DB_OPEN_CREATE 0x01 + +/** + * @brief Close database config + */ +#define GRD_DB_CLOSE 0x00 +#define GRD_DB_CLOSE_IGNORE_ERROR 0x01 + +#define GRD_DB_ID_DISPLAY 0x01 + typedef struct Query { const char *filter; const char *projection; } Query; +/** + * @brief Flags for create and drop collection + */ +#define FLAG_CHECK_UDEFINED_DUPLICAte_TABLE 1 + #ifdef __cplusplus } #endif // __cplusplus diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_db_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_db_api.cpp index abed4f20..668b8d3d 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_db_api.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_db_api.cpp @@ -1,6 +1,21 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "grd_base/grd_db_api.h" #include "grd_base/grd_error.h" +#include "doc_errno.h" #include "document_store_manager.h" #include "document_store.h" @@ -22,5 +37,17 @@ int GRD_DBOpen(const char *dbPath, const char *configStr, unsigned int flags, GR int GRD_DBClose(GRD_DB *db, unsigned int flags) { + if (db == nullptr) { + return GRD_INVALID_ARGS; + } + + DocumentStoreManager::CloseType closeType = (flags == GRD_DB_CLOSE) ? DocumentStoreManager::CloseType::NORMAL : + DocumentStoreManager::CloseType::IGNORE_ERROR; + int status = DocumentStoreManager::CloseDocumentStore(db->store_, closeType); + if (status != E_OK) { + return GRD_RESOURCE_BUSY; + } + + delete db; return GRD_OK; } 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/interface/include/doc_errno.h similarity index 100% rename from services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_errno.h rename to services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/doc_errno.h diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store_manager.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store_manager.h index c5bb1f2d..04bfad4a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store_manager.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store_manager.h @@ -22,7 +22,14 @@ namespace DocumentDB { class DocumentStoreManager { public: - static int GetDocumentStore(const std::string &path, DocumentStore *&delegate); + static int GetDocumentStore(const std::string &path, DocumentStore *&store); + + enum class CloseType { + NORMAL, + IGNORE_ERROR, + }; + + static int CloseDocumentStore(DocumentStore *store, CloseType type); }; } // 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 index 04d87423..ac9e5ad4 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store.cpp @@ -22,5 +22,6 @@ DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) DocumentStore::~DocumentStore() { + delete executor_; } } // 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 index 080ad0d1..0466fd4e 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store_manager.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store_manager.cpp @@ -16,6 +16,7 @@ #include "doc_errno.h" #include "document_store_manager.h" #include "kv_store_manager.h" +#include "grd_base/grd_type_export.h" namespace DocumentDB { int DocumentStoreManager::GetDocumentStore(const std::string &path, DocumentStore *&store) @@ -25,4 +26,14 @@ int DocumentStoreManager::GetDocumentStore(const std::string &path, DocumentStor store = new (std::nothrow) DocumentStore(executor); return E_OK; } + +int DocumentStoreManager::CloseDocumentStore(DocumentStore *store, CloseType type) +{ + if (type == CloseType::NORMAL) { + // TODO: check result set + } + + delete store; + 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.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp index 58b92d17..78f76313 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -22,6 +22,8 @@ SqliteStoreExecutor::SqliteStoreExecutor(sqlite3 *handle) : dbHandle_(handle) SqliteStoreExecutor::~SqliteStoreExecutor() { + sqlite3_close_v2(dbHandle_); + dbHandle_ = nullptr; } int SqliteStoreExecutor::PutData(const Key &key, const Value &value) 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 index 7fed10ca..011b00cc 100644 --- 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 @@ -16,6 +16,8 @@ #include #include "grd_base/grd_db_api.h" +#include "grd_base/grd_error.h" + using namespace testing::ext; class DocumentDBApiTest : public testing::Test { @@ -42,7 +44,6 @@ void DocumentDBApiTest::TearDown(void) { } - /** * @tc.name: OpenDBTest001 * @tc.desc: Test open document db @@ -54,6 +55,11 @@ HWTEST_F(DocumentDBApiTest, OpenDBTest001, TestSize.Level1) { std::string path = "./document.db"; GRD_DB *db = nullptr; - GRD_DBOpen(path.c_str(), nullptr, 0, &db); + int status = GRD_DBOpen(path.c_str(), nullptr, 0, &db); + EXPECT_EQ(status, GRD_OK); EXPECT_NE(db, nullptr); + + status = GRD_DBClose(db, 0); + EXPECT_EQ(status, GRD_OK); + db = nullptr; } \ No newline at end of file -- Gitee From d794a2b06f0095c25188667a2f58d3cab35ef196 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Sat, 11 Mar 2023 10:06:15 +0000 Subject: [PATCH 2/2] Add log module Signed-off-by: lianhuix --- .../gaussdb_rd_Simple/CMakeLists.txt | 5 +- .../src/common/include/log_print.h | 44 ++++++++++ .../src/common/src/log_print.cpp | 82 +++++++++++++++++++ .../unittest/api/doucumentdb_api_test.cpp | 4 +- 4 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/log_print.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/log_print.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 1d7c2a72..7686fe99 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt @@ -2,13 +2,13 @@ cmake_minimum_required(VERSION 3.2) project(grd_simple VERSION 1.0.0) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++17 -pthread") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -std=c++17 -pthread") # Address Sanitizer option(USE_ASAN "Compile with address sanitiser" OFF) if (USE_ASAN) message(STATUS "Compile with address sanitiser ${USE_ASAN}") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -O1") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -O0") endif() function(download_repo repo_name repo_url) @@ -176,6 +176,7 @@ include_directories( ${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 + ${PROJECT_SOURCE_DIR}/third_party/kate/log/include ) link_directories( diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/log_print.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/log_print.h new file mode 100644 index 00000000..55c800a9 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/log_print.h @@ -0,0 +1,44 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef LOG_PRINT_H +#define LOG_PRINT_H + +#include + +namespace DocumentDB { +constexpr const char *LOG_TAG_DOC = "DocumentDB"; + +class Logger { +public: + enum class Level { + LEVEL_DEBUG, + LEVEL_INFO, + LEVEL_WARN, + LEVEL_ERROR, + LEVEL_FATAL + }; + + static void Log(Level level, const std::string &tag, const char *func, int line, const char *format, ...); +}; +} // namespace DocumentDB + +#define NO_LOG(...) // No log in normal and release. Used for convenience when deep debugging +#define GLOGD(...) Logger::Log(Logger::Level::LEVEL_DEBUG, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) +#define GLOGI(...) Logger::Log(Logger::Level::LEVEL_INFO, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) +#define GLOGW(...) Logger::Log(Logger::Level::LEVEL_WARN, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) +#define GLOGE(...) Logger::Log(Logger::Level::LEVEL_ERROR, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) +#define GLOGF(...) Logger::Log(Logger::Level::LEVEL_FATAL, LOG_TAG_DOC, __FUNCTION__, __LINE__, __VA_ARGS__) +#endif // LOG_PRINT_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/log_print.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/log_print.cpp new file mode 100644 index 00000000..10fd0fff --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/log_print.cpp @@ -0,0 +1,82 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include "securec.h" +#include "hilog/log.h" + +namespace DocumentDB { +namespace { + +void PrintLog(Logger::Level level, const std::string &tag, const std::string &msg) +{ + if (msg.empty()) { + return; + } + const std::string format = "%{public}s"; + OHOS::HiviewDFX::HiLogLabel label = { LOG_CORE, 0xD001630, tag.c_str() }; // log module id. // TODO: + switch (level) { + case Logger::Level::LEVEL_DEBUG: + (void)OHOS::HiviewDFX::HiLog::Debug(label, format.c_str(), msg.c_str()); + break; + case Logger::Level::LEVEL_INFO: + (void)OHOS::HiviewDFX::HiLog::Info(label, format.c_str(), msg.c_str()); + break; + case Logger::Level::LEVEL_WARN: + (void)OHOS::HiviewDFX::HiLog::Warn(label, format.c_str(), msg.c_str()); + break; + case Logger::Level::LEVEL_ERROR: + (void)OHOS::HiviewDFX::HiLog::Error(label, format.c_str(), msg.c_str()); + break; + case Logger::Level::LEVEL_FATAL: + (void)OHOS::HiviewDFX::HiLog::Fatal(label, format.c_str(), msg.c_str()); + break; + default: + break; + } +} + +void PreparePrivateLog(const char *format, std::string &outStrFormat) +{ + static const std::string PRIVATE_TAG = "s{private}"; + outStrFormat = format; + std::string::size_type pos = outStrFormat.find(PRIVATE_TAG); + if (pos != std::string::npos) { + outStrFormat.replace(pos, PRIVATE_TAG.size(), ".3s"); + } +} +} + +void Logger::Log(Level level, const std::string &tag, const char *func, int line, const char *format, ...) +{ + static const int maxLogLength = 1024; + + va_list argList; + va_start(argList, format); + char logBuff[maxLogLength]; + std::string msg; + std::string formatTemp; + PreparePrivateLog(format, formatTemp); + int bytes = vsnprintf_s(logBuff, maxLogLength, maxLogLength - 1, formatTemp.c_str(), argList); + if (bytes < 0) { + msg = "log buffer overflow!"; + } else { + msg = logBuff; + } + va_end(argList); + + PrintLog(level, tag, msg); +} +} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/doucumentdb_api_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/doucumentdb_api_test.cpp index 011b00cc..ea2b198f 100644 --- 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 @@ -15,9 +15,11 @@ #include +#include "log_print.h" #include "grd_base/grd_db_api.h" #include "grd_base/grd_error.h" +using namespace DocumentDB; using namespace testing::ext; class DocumentDBApiTest : public testing::Test { @@ -58,7 +60,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBTest001, TestSize.Level1) int status = GRD_DBOpen(path.c_str(), nullptr, 0, &db); EXPECT_EQ(status, GRD_OK); EXPECT_NE(db, nullptr); - + GLOGD("Open DB test 001: status: %d", status); status = GRD_DBClose(db, 0); EXPECT_EQ(status, GRD_OK); db = nullptr; -- Gitee