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 0c3aa66d8acc05f89857e060c8f1c49dd102319a..7686fe996a158d7464e5bf20047a648492d73239 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt @@ -2,8 +2,14 @@ cmake_minimum_required(VERSION 3.2) project(grd_simple VERSION 1.0.0) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -std=c++17 -pthread") -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 -O0") +endif() function(download_repo repo_name repo_url) execute_process( @@ -170,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/include/grd_base/grd_error.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_error.h index 0abe8e9637aa79a04aafd9ccdad56e34a86c957f..007d13aae0180cabb81889dc380344b270185500 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 602b058595b3f867afa16081476f1c890500c3e7..352d2162b9916b154b8a1d038ac0a03e7c735490 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/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 0000000000000000000000000000000000000000..55c800a94be0def392e1400e14cf3bba6750c365 --- /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 0000000000000000000000000000000000000000..10fd0fffb8ed44bd2d8ca63ee51291932e5107a4 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/log_print.cpp @@ -0,0 +1,82 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include "securec.h" +#include "hilog/log.h" + +namespace DocumentDB { +namespace { + +void PrintLog(Logger::Level level, const std::string &tag, const std::string &msg) +{ + if (msg.empty()) { + return; + } + const std::string format = "%{public}s"; + OHOS::HiviewDFX::HiLogLabel label = { LOG_CORE, 0xD001630, tag.c_str() }; // log module id. // TODO: + switch (level) { + case Logger::Level::LEVEL_DEBUG: + (void)OHOS::HiviewDFX::HiLog::Debug(label, format.c_str(), msg.c_str()); + break; + case Logger::Level::LEVEL_INFO: + (void)OHOS::HiviewDFX::HiLog::Info(label, format.c_str(), msg.c_str()); + break; + case Logger::Level::LEVEL_WARN: + (void)OHOS::HiviewDFX::HiLog::Warn(label, format.c_str(), msg.c_str()); + break; + case Logger::Level::LEVEL_ERROR: + (void)OHOS::HiviewDFX::HiLog::Error(label, format.c_str(), msg.c_str()); + break; + case Logger::Level::LEVEL_FATAL: + (void)OHOS::HiviewDFX::HiLog::Fatal(label, format.c_str(), msg.c_str()); + break; + default: + break; + } +} + +void PreparePrivateLog(const char *format, std::string &outStrFormat) +{ + static const std::string PRIVATE_TAG = "s{private}"; + outStrFormat = format; + std::string::size_type pos = outStrFormat.find(PRIVATE_TAG); + if (pos != std::string::npos) { + outStrFormat.replace(pos, PRIVATE_TAG.size(), ".3s"); + } +} +} + +void Logger::Log(Level level, const std::string &tag, const char *func, int line, const char *format, ...) +{ + static const int maxLogLength = 1024; + + va_list argList; + va_start(argList, format); + char logBuff[maxLogLength]; + std::string msg; + std::string formatTemp; + PreparePrivateLog(format, formatTemp); + int bytes = vsnprintf_s(logBuff, maxLogLength, maxLogLength - 1, formatTemp.c_str(), argList); + if (bytes < 0) { + msg = "log buffer overflow!"; + } else { + msg = logBuff; + } + va_end(argList); + + PrintLog(level, tag, msg); +} +} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_db_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_db_api.cpp index abed4f200e674712612a7739422f51e18f4d5b51..668b8d3de8c295aad2d6a2b252f6f7f5aad06eb9 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 c5bb1f2d786da2ebd02415abfe0a9980da8ba051..04bfad4accc718d77f705683f7b62cb356388598 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 04d8742309fe337adbd72af829d4f2c20d7e17a2..ac9e5ad43483b6f03703aa807cd984ca4d5f3ff6 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 080ad0d154ae5db8b1195d2b7d74b869cf3a5e3e..0466fd4eca096105ff9683f9aa6f8bc607282291 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 58b92d174ca80a502c5ed70121b4e392c195e7ea..78f763138d89e2c0cd0ebd870cf58bbc310479ba 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 7fed10cab1456a45bfaf4b962030d2a310726cdc..ea2b198f1a56920f56a10eb21841423926f8eaed 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,7 +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 { @@ -42,7 +46,6 @@ void DocumentDBApiTest::TearDown(void) { } - /** * @tc.name: OpenDBTest001 * @tc.desc: Test open document db @@ -54,6 +57,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); + GLOGD("Open DB test 001: status: %d", status); + status = GRD_DBClose(db, 0); + EXPECT_EQ(status, GRD_OK); + db = nullptr; } \ No newline at end of file