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 7686fe996a158d7464e5bf20047a648492d73239..b1ab9a9856081d9156aed1a2d6f4fe3c002eb1b5 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt @@ -151,7 +151,7 @@ include_directories( ${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/include ${PROJECT_SOURCE_DIR}/src/executor/base ${PROJECT_SOURCE_DIR}/src/executor/document ${PROJECT_SOURCE_DIR}/src/oh_adapter diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/grd_document_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/grd_document_api.h index 09f1719c9dc5b7123b1c56c9c43f3e1492793644..fc062be4497d52feca315a8142c6a48c157b72ac 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/grd_document_api.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/grd_document_api.h @@ -16,8 +16,8 @@ #ifndef GRD_DOCUMENT_API_H #define GRD_DOCUMENT_API_H -#include "grd_type_export.h" -#include "grd_resultset_api.h" +#include "grd_base/grd_type_export.h" +#include "grd_base/grd_resultset_api.h" #ifdef __cplusplus extern "C" { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/os_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/os_api.h new file mode 100644 index 0000000000000000000000000000000000000000..e331ab336644d7cb745f46d884be9fa6ed8bf9ec --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/os_api.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. + */ +#include + +#ifndef OS_API_H +#define OS_API_H +namespace DocumentDB { +namespace OSAPI { +bool CheckPathExistence(const std::string &filePath); + +int GetRealPath(const std::string &inOriPath, std::string &outRealPath); + +void SplitFilePath(const std::string &filePath, std::string &fileDir, std::string &fileName); +} // namespace OSAPI +} // namespace DocumentDB +#endif // OS_API_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/os_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/os_api.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13528043dcaebce25d9e9bc56d54ce0a81ce866e --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/os_api.cpp @@ -0,0 +1,79 @@ +/* + * 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 "os_api.h" +#include +#include +#include + +#include "doc_errno.h" +#include "log_print.h" +#include "securec.h" + +namespace DocumentDB { +namespace { + const int ACCESS_MODE_EXISTENCE = 0; +} +namespace OSAPI { +bool CheckPathExistence(const std::string &filePath) +{ + return (access(filePath.c_str(), ACCESS_MODE_EXISTENCE) == 0); +} + +int GetRealPath(const std::string &inOriPath, std::string &outRealPath) +{ + const unsigned int MAX_PATH_LENGTH = PATH_MAX; + if (inOriPath.length() > MAX_PATH_LENGTH || MAX_PATH_LENGTH > 0x10000) { // max limit is 64K(0x10000). + GLOGE("[OS_API] OriPath too long."); + return -E_INVALID_ARGS; + } + + char *realPath = new (std::nothrow) char[MAX_PATH_LENGTH + 1]; + if (realPath == nullptr) { + return -E_OUT_OF_MEMORY; + } + if (memset_s(realPath, MAX_PATH_LENGTH + 1, 0, MAX_PATH_LENGTH + 1) != EOK) { + delete []realPath; + return -E_SECUREC_ERROR; + } + + if (realpath(inOriPath.c_str(), realPath) == nullptr) { + GLOGE("[OS_API] Realpath error:%d.", errno); + delete []realPath; + return -E_SYSTEM_API_FAIL; + } + outRealPath = std::string(realPath); + delete []realPath; + return E_OK; +} + +void SplitFilePath(const std::string &filePath, std::string &fileDir, std::string &fileName) +{ + if (filePath.empty()) { + return; + } + + auto slashPos = filePath.find_last_of('/'); + if (slashPos == std::string::npos) { + fileName = filePath; + fileDir = ""; + return; + } + + fileDir = filePath.substr(0, slashPos); + fileName = filePath.substr(slashPos + 1); + return; +} +} // namespace OSAPI +} // namespace DocumentDB \ No newline at end of file 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 668b8d3de8c295aad2d6a2b252f6f7f5aad06eb9..f4e3d65f3d7addedc13d0a9b4ae23586169668d2 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 @@ -14,30 +14,42 @@ */ #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" +#include "grd_base/grd_error.h" +#include "grd_type_inner.h" using namespace DocumentDB; -typedef struct GRD_DB { - DocumentStore *store_ = nullptr; -} GRD_DB; +int TrasnferDocErr(int err) +{ + switch (err) { + case E_OK: + return GRD_OK; + case -E_ERROR: + return GRD_INNER_ERR; + case -E_INVALID_ARGS: + return GRD_INVALID_ARGS; + default: + return GRD_INNER_ERR; + } +} int GRD_DBOpen(const char *dbPath, const char *configStr, unsigned int flags, GRD_DB **db) { - std::string path = dbPath; + std::string path = (dbPath == nullptr ? "" : dbPath); DocumentStore *store = nullptr; - DocumentStoreManager::GetDocumentStore(path, store); + int ret = DocumentStoreManager::GetDocumentStore(path, store); *db = new (std::nothrow) GRD_DB(); (*db)->store_ = store; - return GRD_OK; + return TrasnferDocErr(ret); } int GRD_DBClose(GRD_DB *db, unsigned int flags) { - if (db == nullptr) { + if (db == nullptr || db->store_ == nullptr) { return GRD_INVALID_ARGS; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/grd_document_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/grd_document_api.cpp index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..dc5ccf4653a0664803874c5a5e283e0d33cbd2ea 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/grd_document_api.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/grd_document_api.cpp @@ -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. +*/ + +#include "grd_document/grd_document_api.h" +#include "grd_base/grd_error.h" +#include "grd_type_inner.h" + +int GRD_CreateCollection(GRD_DB *db, const char *collectionName, const char *optionStr, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr) { + return GRD_INVALID_ARGS; + } + return db->store_->CreateCollection(collectionName, optionStr, flags); +} + +int GRD_DropCollection(GRD_DB *db, const char *collectionName, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr) { + return GRD_INVALID_ARGS; + } + return db->store_->DropCollection(collectionName, flags); +} + +int GRD_UpdateDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *update, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr) { + return GRD_INVALID_ARGS; + } + return db->store_->UpdateDocument(collectionName, filter, update, flags); +} + +int GRD_UpSertDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *document, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr) { + return GRD_INVALID_ARGS; + } + return db->store_->UpsertDocument(collectionName, filter, document, flags); +} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/grd_type_inner.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/grd_type_inner.h new file mode 100644 index 0000000000000000000000000000000000000000..afc300ecac3a25c701fd76ad33ea7679ec9b8a54 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/grd_type_inner.h @@ -0,0 +1,25 @@ +/* +* 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 GRD_TYPE_INNER_H +#define GRD_TYPE_INNER_H + +#include "document_store.h" + +typedef struct GRD_DB { + DocumentDB::DocumentStore *store_ = nullptr; +} GRD_DB; + +#endif // GRD_TYPE_INNER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/collection.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/collection.h new file mode 100644 index 0000000000000000000000000000000000000000..6dba77c9ec8635003e37faddb8106336e91bdab0 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/collection.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 COLLECTION_H +#define COLLECTION_H + +#include +#include "doc_common.h" +#include "kv_store_executor.h" + +namespace DocumentDB { +class Collection { +public: + Collection(std::string name, KvStoreExecutor *executor); + ~Collection(); + + int PutDocument(const Key &key, const Value &document); + int GetDocument(const Key &key, Value &document) const; + int DeleteDocument(const Key &key); + + int UpsertDocument(const Key &key, Value &document); + int UpdateDocument(const Key &key, Value &update); +private: + std::string name_; + KvStoreExecutor *executor_ = nullptr; +}; +} // namespace DocumentDB +#endif // COLLECTION_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/doc_errno.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/doc_errno.h index 73fecffa16f2b9efbd5af7a3b65da8d084f4ed66..c481c57396c08363f40b983e57189e01933bfeb2 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/doc_errno.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/doc_errno.h @@ -19,8 +19,12 @@ namespace DocumentDB { constexpr int E_OK = 0; -constexpr int E_ERROR = 1; - - +constexpr int E_BASE = 1000; +constexpr int E_ERROR = E_BASE + 1; +constexpr int E_INVALID_ARGS = E_BASE + 2; +constexpr int E_UNFINISHED = E_BASE + 7; +constexpr int E_OUT_OF_MEMORY = E_BASE + 8; +constexpr int E_SECUREC_ERROR = E_BASE + 9; +constexpr int E_SYSTEM_API_FAIL = E_BASE + 10; } // DocumentDB #endif // DOC_ERRNO_H \ No newline at end of file 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 index 52bb3c29f2d6abe42a75e543e3cc9b12474b7a5b..93925e5529d254073cda0ba6cb04943bda29316c 100644 --- 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 @@ -16,6 +16,10 @@ #ifndef DOCUMENT_STORE_H #define DOCUMENT_STORE_H +#include +#include + +#include "collection.h" #include "kv_store_executor.h" namespace DocumentDB { @@ -23,8 +27,16 @@ class DocumentStore { public: DocumentStore(KvStoreExecutor *); ~DocumentStore(); + + int CreateCollection(const std::string &name, const std::string &option, int flag); + int DropCollection(const std::string &name, int flag); + + int UpdateDocument(const std::string &collection, const std::string &filter, const std::string &update, int flag); + int UpsertDocument(const std::string &collection, const std::string &filter, const std::string &document, int flag); + private: KvStoreExecutor *executor_ = nullptr; + std::map collections_; }; } // 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 index 04bfad4accc718d77f705683f7b62cb356388598..acfb64aab060161111e64791d9b47fcabff302e3 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 @@ -30,6 +30,9 @@ public: }; static int CloseDocumentStore(DocumentStore *store, CloseType type); + +private: + static bool CheckDBPath(const std::string &path, std::string &canonicalPath); }; } // 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/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/collection.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e2562b110a1a89278264b86ad456d5b73691b1b --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/collection.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 "collection.h" +#include "doc_errno.h" + +namespace DocumentDB { +Collection::Collection(std::string name, KvStoreExecutor *executor) : name_(name), executor_(executor) +{ +} + +Collection::~Collection() +{ + executor_ = nullptr; +} + +int Collection::PutDocument(const Key &key, const Value &document) +{ + if (executor_ == nullptr) { + return -E_INVALID_ARGS; + } + return executor_->PutData(name_, key, document); +} + +int Collection::GetDocument(const Key &key, Value &document) const +{ + return E_OK; +} + +int Collection::DeleteDocument(const Key &key) +{ + return E_OK; +} + +int Collection::UpsertDocument(const Key &key, Value &document) +{ + if (executor_ == nullptr) { + return -E_INVALID_ARGS; + } + return executor_->PutData(name_, key, document); +} + +int Collection::UpdateDocument(const Key &key, Value &update) +{ + return E_OK; +} +} // namespace DocumentDB 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 ac9e5ad43483b6f03703aa807cd984ca4d5f3ff6..6fd1f679c1bd16c0664b34416f7390a83bf44b70 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 @@ -14,6 +14,7 @@ */ #include "document_store.h" +#include "doc_errno.h" namespace DocumentDB { DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) @@ -24,4 +25,33 @@ DocumentStore::~DocumentStore() { delete executor_; } -} // DocumentDB \ No newline at end of file + +int DocumentStore::CreateCollection(const std::string &name, const std::string &option, int flag) +{ + executor_->CreateCollection(name, flag); + return E_OK; +} + +int DocumentStore::DropCollection(const std::string &name, int flag) +{ + executor_->DropCollection(name, flag); + return E_OK; +} + +int DocumentStore::UpdateDocument(const std::string &collection, const std::string &filter, const std::string &update, + int flag) +{ + return E_OK; +} + +int DocumentStore::UpsertDocument(const std::string &collection, const std::string &filter, const std::string &document, + int flag) +{ + auto coll = Collection(collection, executor_); + + Key key(filter.begin(), filter.end()); + Value value(document.begin(), document.end()); + + return coll.PutDocument(key, value); +} +} // namespace 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 0466fd4eca096105ff9683f9aa6f8bc607282291..b386b44dc8055b878c497b85d8449dd8a8c9ed5e 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 @@ -13,10 +13,12 @@ * limitations under the License. */ -#include "doc_errno.h" #include "document_store_manager.h" -#include "kv_store_manager.h" +#include "doc_errno.h" #include "grd_base/grd_type_export.h" +#include "kv_store_manager.h" +#include "log_print.h" +#include "os_api.h" namespace DocumentDB { int DocumentStoreManager::GetDocumentStore(const std::string &path, DocumentStore *&store) @@ -36,4 +38,26 @@ int DocumentStoreManager::CloseDocumentStore(DocumentStore *store, CloseType typ delete store; return E_OK; } + +bool DocumentStoreManager::CheckDBPath(const std::string &path, std::string &canonicalPath) +{ + if (path.empty()) { + GLOGE("invalid path empty"); + return -E_INVALID_ARGS; + } + + if (path.back() == '/') { + GLOGE("invalid path end with slash"); + return -E_INVALID_ARGS; + } + + std::string canonicalDir; + int errCode = OSAPI::GetRealPath(path, canonicalDir); + if (errCode == E_OK) { + GLOGE("Get real path failed. %d", errCode); + return errCode; + } + + +} } // 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 index 526c32a5c3b13fcaedfd86d4928ee7932c142354..f1d21444e73602b0430e281412147037424a13c1 100644 --- 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 @@ -16,6 +16,7 @@ #ifndef KV_STORE_EXECUTOR_H #define KV_STORE_EXECUTOR_H +#include #include "doc_common.h" namespace DocumentDB { @@ -23,9 +24,12 @@ class KvStoreExecutor { public: virtual ~KvStoreExecutor() = default; - virtual int PutData(const Key &key, const Value &value) = 0; + virtual int PutData(const std::string &collName, const Key &key, const Value &value) = 0; - virtual int GetData(const Key &key, Value &value) const = 0; + virtual int GetData(const std::string &collName, const Key &key, Value &value) const = 0; + + virtual int CreateCollection(const std::string &name, int flag) = 0; + virtual int DropCollection(const std::string &name, int flag) = 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/src/kv_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/kv_store_manager.cpp index d4c91bcdec586ea8ad1067e4043041dc475d07b7..ca9b278866df660c5ce77d2b5f522e8206a73e7b 100644 --- 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 @@ -16,29 +16,18 @@ #include "doc_errno.h" #include "kv_store_manager.h" #include "sqlite_store_executor_impl.h" +#include "sqlite_utils.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) { + sqlite3 *db = nullptr; + int errCode = SQLiteUtils::CreateDataBase(path, 0, db); + if (errCode != E_OK || db == nullptr) { return -E_ERROR; } 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 78f763138d89e2c0cd0ebd870cf58bbc310479ba..9f091ca1b010f452d3e0b1ac622b44943c8d41f0 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 @@ -13,6 +13,8 @@ * limitations under the License. */ #include "doc_errno.h" +#include "log_print.h" +#include "sqlite_utils.h" #include "sqlite_store_executor_impl.h" namespace DocumentDB { @@ -26,13 +28,73 @@ SqliteStoreExecutor::~SqliteStoreExecutor() dbHandle_ = nullptr; } -int SqliteStoreExecutor::PutData(const Key &key, const Value &value) +int SqliteStoreExecutor::PutData(const std::string &collName, const Key &key, const Value &value) { + if (dbHandle_ == nullptr) { + return -E_ERROR; + } + + std::string sql = "INSERT OR REPLACE INTO '" + collName + "' VALUES (?,?);"; + int errCode = SQLiteUtils::ExecSql(dbHandle_, sql, [key, value](sqlite3_stmt *stmt) { + SQLiteUtils::BindBlobToStatement(stmt, 1, key); + SQLiteUtils::BindBlobToStatement(stmt, 2, value); + return E_OK; + }, nullptr); + if (errCode != SQLITE_OK) { + GLOGE("[sqlite executor] create collectoin failed. err=%d", errCode); + return errCode; + } + return E_OK; +} + +int SqliteStoreExecutor::GetData(const std::string &collName, const Key &key, Value &value) const +{ + if (dbHandle_ == nullptr) { + return -E_ERROR; + } + + std::string sql = "SELECT value FROM '" + collName + "' WHERE key=?;"; + int errCode = SQLiteUtils::ExecSql(dbHandle_, sql, [key](sqlite3_stmt *stmt) { + SQLiteUtils::BindBlobToStatement(stmt, 1, key); + return E_OK; + }, [&value](sqlite3_stmt *stmt) { + SQLiteUtils::GetColumnBlobValue(stmt, 0, value); + return E_OK; + }); + if (errCode != SQLITE_OK) { + GLOGE("[sqlite executor] create collectoin failed. err=%d", errCode); + return errCode; + } return E_OK; } -int SqliteStoreExecutor::GetData(const Key &key, Value &value) const +int SqliteStoreExecutor::CreateCollection(const std::string &name, int flag) { + if (dbHandle_ == nullptr) { + return -E_ERROR; + } + + std::string sql = "CREATE TABLE IF NOT EXISTS '" + name + "' (key BLOB PRIMARY KEY, value BLOB);"; + int errCode = SQLiteUtils::ExecSql(dbHandle_, sql); + if (errCode != SQLITE_OK) { + GLOGE("[sqlite executor] create collectoin failed. err=%d", errCode); + return errCode; + } + return E_OK; +} + +int SqliteStoreExecutor::DropCollection(const std::string &name, int flag) +{ + if (dbHandle_ == nullptr) { + return -E_ERROR; + } + + std::string sql = "DROP TABLE IF EXISTS '" + name + "';"; + int errCode = SQLiteUtils::ExecSql(dbHandle_, sql); + if (errCode != SQLITE_OK) { + GLOGE("[sqlite executor] drop collectoin failed. err=%d", errCode); + return errCode; + } 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 index 731a8121f5fdb6da6072e79d236e3c428e5818f7..ab15f02d3a5afdc05bcb9147f8dedda27a5bb9ab 100644 --- 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 @@ -25,8 +25,11 @@ public: SqliteStoreExecutor(sqlite3 *handle); ~SqliteStoreExecutor() override; - int PutData(const Key &key, const Value &value) override; - int GetData(const Key &key, Value &value) const override; + int PutData(const std::string &collName, const Key &key, const Value &value) override; + int GetData(const std::string &collName, const Key &key, Value &value) const override; + + int CreateCollection(const std::string &name, int flag) override; + int DropCollection(const std::string &name, int flag) override; private: sqlite3 *dbHandle_ = nullptr; 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 ea2b198f1a56920f56a10eb21841423926f8eaed..63ca9cab9ef11810fe59557d23b9a7b073eb8eef 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 @@ -18,6 +18,7 @@ #include "log_print.h" #include "grd_base/grd_db_api.h" #include "grd_base/grd_error.h" +#include "grd_document/grd_document_api.h" using namespace DocumentDB; using namespace testing::ext; @@ -47,12 +48,12 @@ void DocumentDBApiTest::TearDown(void) } /** - * @tc.name: OpenDBTest001 - * @tc.desc: Test open document db - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ + * @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"; @@ -61,7 +62,30 @@ HWTEST_F(DocumentDBApiTest, OpenDBTest001, TestSize.Level1) EXPECT_EQ(status, GRD_OK); EXPECT_NE(db, nullptr); GLOGD("Open DB test 001: status: %d", status); + + EXPECT_EQ(GRD_CreateCollection(db, "student", "", 0), GRD_OK); + + EXPECT_EQ(GRD_UpSertDoc(db, "student", "10001", "{name:\"Tom\",age:23}", 0), GRD_OK); + EXPECT_EQ(GRD_UpSertDoc(db, "student", "10001", "{name:\"Tom\",age:24}", 0), GRD_OK); + + EXPECT_EQ(GRD_DropCollection(db, "student", 0), GRD_OK); + status = GRD_DBClose(db, 0); EXPECT_EQ(status, GRD_OK); db = nullptr; -} \ No newline at end of file +} + +/** + * @tc.name: OpenDBTest001 + * @tc.desc: Test open document db with NULL path + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBApiTest, OpenDBTest002, TestSize.Level1) +{ + GRD_DB *db = nullptr; + char *path = nullptr; + int status = GRD_DBOpen(path, nullptr, 0, &db); + EXPECT_EQ(status, GRD_INVALID_ARGS); +}