From 66c7a6ee41b58d1a2bcd2f002176edf1e7221025 Mon Sep 17 00:00:00 2001 From: mazhao Date: Sat, 1 Apr 2023 02:00:42 +0000 Subject: [PATCH 1/2] =?UTF-8?q?common=20method=20and=20insert=E3=80=81find?= =?UTF-8?q?=E3=80=81delete=20initially=20realized?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: mazhao --- .../src/common/include/json_common.h | 14 +- .../src/common/src/doc_common.cpp | 23 +- .../src/common/src/json_common.cpp | 143 ++++++-- .../src/executor/base/grd_resultset_api.cpp | 54 +++ .../executor/document/grd_document_api.cpp | 25 ++ .../src/executor/include/grd_format_config.h | 32 ++ .../src/executor/include/grd_type_inner.h | 4 + .../src/interface/include/collection.h | 1 + .../src/interface/include/document_store.h | 9 +- .../src/interface/include/projection_tree.h | 37 +++ .../src/interface/include/result_set.h | 51 +++ .../src/interface/src/collection.cpp | 12 +- .../src/interface/src/document_store.cpp | 9 +- .../src/interface/src/projection_tree.cpp | 50 +++ .../src/interface/src/result_set.cpp | 104 ++++++ .../src/oh_adapter/include/json_object.h | 9 +- .../src/oh_adapter/src/cjson_object.cpp | 180 ++++++++-- .../src/oh_adapter/src/cjson_object.h | 23 +- .../src/sqlite_store_executor_impl.cpp | 34 +- .../unittest/api/ documentdb_delete_test.cpp | 78 +++++ .../unittest/api/ documentdb_find_test.cpp | 93 ++++++ .../unittest/api/ documentdb_insert_test.cpp | 309 ++++++++++++++++++ .../test/unittest/api/json_test.cpp | 71 ++++ 23 files changed, 1279 insertions(+), 86 deletions(-) create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_resultset_api.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/grd_format_config.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/projection_tree.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/result_set.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/projection_tree.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/result_set.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_delete_test.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_find_test.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_insert_test.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/json_test.cpp diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h index 22e19391..bf9ec26f 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h @@ -18,6 +18,7 @@ #include #include +#include #include "cjson_object.h" namespace DocumentDB { @@ -27,11 +28,14 @@ public: JsonCommon() = default; ~JsonCommon(); - ResultValue* GetValue(CjsonObject *root, std::vector path); - static int GetIdValue(CjsonObject *root, std::vector &id); - static bool CheckIsJson(const std::string &data); - static int GetJsonDeep(const std::string &data); + static ResultValue* GetValueByFiled(JsonObject *node, const std::string& filed); + static bool CheckJsonField(const std::string &data); + static int ParseNode(JsonObject *Node, std::vector onePath, std::vector> &parsePath, bool isFirstFloor); + static std::vector> ParsePath(JsonObject* node); + static std::vector GetLeafValue(JsonObject *node); +private: + static bool CheckNode(JsonObject *Node, std::set setString, bool &errflag); + static int CheckLeafNode(JsonObject *Node, std::vector &leafValue); }; - } // DocumentDB #endif // JSON_COMMON_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/doc_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/doc_common.cpp index fa39bd34..751ed6b2 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/doc_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/doc_common.cpp @@ -13,11 +13,11 @@ * limitations under the License. */ #include - #include "doc_common.h" #include "doc_errno.h" #include "log_print.h" #include "securec.h" +#include "cjson_object.h" namespace DocumentDB { bool CheckCommon::CheckCollectionName(const std::string &collectionName) @@ -36,12 +36,6 @@ bool CheckCommon::CheckCollectionName(const std::string &collectionName) bool CheckCommon::CheckFilter(const std::string &filter) { - if (JsonCommon::CheckIsJson(filter) == false) { - return false; - } - if (JsonCommon::GetJsonDeep(filter) > 4) { - return false; - } if (CheckIdFormat(filter) == false) { return false; } @@ -50,10 +44,10 @@ bool CheckCommon::CheckFilter(const std::string &filter) bool CheckCommon::CheckIdFormat(const std::string &data) { - CjsonObject filter_json; - filter_json.Parse(data); - std::vector id; - if (JsonCommon::GetIdValue(&filter_json, id) == E_ERROR) { + JsonObject* filter_json = new CjsonObject(); + filter_json->Init(data); + auto resultValue = JsonCommon::GetValueByFiled(filter_json, "_id"); + if (resultValue != nullptr && resultValue->value_type != ResultValue::ValueType::VALUE_STRING) { return false; } return true; @@ -61,7 +55,12 @@ bool CheckCommon::CheckIdFormat(const std::string &data) bool CheckCommon::CheckDocument(const std::string &document) { + if (CheckIdFormat(document) == false) { + return false; + } + if (JsonCommon::CheckJsonField(document) == false) { + return false; + } return true; } - } // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp index 49bccc16..a6c13dcc 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp @@ -20,51 +20,136 @@ #include "securec.h" namespace DocumentDB { -ResultValue* JsonCommon::GetValue(CjsonObject *root, std::vector path) +ResultValue* JsonCommon::GetValueByFiled(JsonObject *node, const std::string& filed) { + if (node == nullptr) { + return nullptr; + } + while (node != nullptr) { + if (node->GetItemFiled() == filed) + { + auto item_value = node->GetItemValue(); + return item_value; + } + if (node->GetNext() == nullptr) { + return nullptr; + } + node = node->GetNext(); + } return nullptr; } -int JsonCommon::GetIdValue(CjsonObject *root, std::vector &id) +int JsonCommon::CheckLeafNode(JsonObject *node, std::vector &leafValue) +{ + if (node->GetChild() == nullptr) { + auto item_value = node->GetItemValue(); + leafValue.emplace_back(item_value); + } + if (node->GetChild() != nullptr) { + CheckLeafNode(node->GetChild(), leafValue); + } + if (node->GetNext() != nullptr) { + CheckLeafNode(node->GetNext(), leafValue); + } + return E_OK; +} +std::vector JsonCommon::GetLeafValue(JsonObject *node) { - auto &node = root; - if (root == nullptr) { - return E_OK; + std::vector leafValue; + CheckLeafNode(node, leafValue); + return leafValue; +} + +bool JsonCommon::CheckNode(JsonObject *node, std::set setString, bool &errflag) { + if (errflag == false) { + return false; } - while (node->GetNext() != nullptr) { - if (node->GetItemFiled() == "_id") { - auto item_value = node->GetItemValue(); - if (item_value->value_type != ResultValue::ValueType::VALUE_STRING) { - return E_ERROR; - } - id.emplace_back(item_value->value_string); + std::string field_str; + if (node->GetItemValue() != nullptr) { + field_str = node->GetItemFiled(); + if (setString.find(field_str) == setString.end()) { + setString.insert(field_str); } - node = node->GetNext(); + else { + errflag = false; + return false; + } + for (int i = 0; i < field_str.size(); i++) { + if (!(('a'<=field_str[i] && field_str[i]<='z')|| ('A'<=field_str[i] && field_str[i]<='Z') || ('0'<=field_str[i] && field_str[i]<='9') || '_' == field_str[i])) { + errflag = false; + return false; + } + } } - return E_OK; + if (node->GetChild() != nullptr) { + std::set stringSet_new; + CheckNode(node->GetChild(), stringSet_new, errflag); + } + if (node->GetNext() != nullptr) { + CheckNode(node->GetNext(), setString, errflag); + } + return errflag; } -bool JsonCommon::CheckIsJson(const std::string &data) { - CjsonObject cjsonObj; - if (cjsonObj.Parse(data) == E_ERROR) { +bool JsonCommon::CheckJsonField(const std::string &data) { + JsonObject* jsonObj = new CjsonObject; + if (jsonObj->Init(data) != E_OK) { return false; } - return true; + std::set stringSet; + bool errflag = true; + return CheckNode(jsonObj, stringSet, errflag); } -int JsonCommon::GetJsonDeep(const std::string &data) { - int lens = 0; - int deep = 0; - for (int i = 0; i < data.size(); i++) { - if (data[i] == '[' || data[i] == '{') { - lens++; - } - else if (data[i] == ']' || data[i] == '}') { - deep = std::max(deep, lens); - lens--; +int JsonCommon::ParseNode(JsonObject* node, std::vector onePath, std::vector> &parsePath, bool isFirstFloor) +{ + std::vector forePath; + if (isFirstFloor) { + std::string tempparse_name; + std::vector parsed_mixfiled_name; + std::string mixfield_name = node->GetItemFiled(); + for (int j = 0; j < mixfield_name.size(); j++) { + if (mixfield_name[j] != '.') { + tempparse_name = tempparse_name + mixfield_name[j]; + } + if (mixfield_name[j] == '.' || j == mixfield_name.size() - 1) { + parsed_mixfiled_name.emplace_back(tempparse_name); + tempparse_name.clear(); + } } + forePath = onePath; + onePath.insert(onePath.end(), parsed_mixfiled_name.begin(), parsed_mixfiled_name.end()); + } else { + std::vector parsed_mixfiled_name; + parsed_mixfiled_name.emplace_back(node->GetItemFiled()); + forePath = onePath; + onePath.insert(onePath.end(), parsed_mixfiled_name.begin(), parsed_mixfiled_name.end()); + } + + if (node->GetChild() != nullptr && node->GetChild()->GetItemFiled() != "") { + ParseNode(node->GetChild(), onePath, parsePath, false); } - return deep; + else { + parsePath.emplace_back(onePath); + } + if (node->GetNext() != nullptr) { + ParseNode(node->GetNext(), forePath, parsePath, isFirstFloor); + } + return 0; +} + +std::vector> JsonCommon::ParsePath(JsonObject* root) +{ + std::vector> parsePath; + auto projection_json = root->GetChild(); + if (projection_json == nullptr) { + GLOGE("projection_json is null"); + } + std::vector onePath; + ParseNode(projection_json, onePath, parsePath, true); + return parsePath; } + + } // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_resultset_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_resultset_api.cpp new file mode 100644 index 00000000..1269f51e --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/base/grd_resultset_api.cpp @@ -0,0 +1,54 @@ +/* +* 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_error.h" +#include "doc_errno.h" +#include "grd_base/grd_error.h" +#include "grd_type_inner.h" +#include "grd_base/grd_resultset_api.h" + +using namespace DocumentDB; + +int GRD_Next(GRD_ResultSet *resultSet) +{ + resultSet->resultSet_->GetNext(); + return E_OK; +} + +int GRD_GetValue(GRD_ResultSet *resultSet, char **value) +{ + resultSet->resultSet_->GetValue(value); + return E_OK; +} + +int GRD_FreeValue(char *value) +{ + delete[] value; + value = nullptr;//为什么调用delete后,value的指针不为空捏。 + if (value != nullptr) { + GLOGE("delete fail"); + } + return E_OK; +} + +int GRD_FreeResultSet(GRD_ResultSet *resultSet) +{ + delete[] resultSet; + resultSet = nullptr; + if (resultSet != nullptr) { + GLOGE("delete fail"); + } + return E_OK; +} \ No newline at end of file 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 dc5ccf46..942075d5 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 @@ -17,6 +17,7 @@ #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) { @@ -48,3 +49,27 @@ int GRD_UpSertDoc(GRD_DB *db, const char *collectionName, const char *filter, co } return db->store_->UpsertDocument(collectionName, filter, document, flags); } + +int GRD_InsertDoc(GRD_DB *db, const char *collectionName, const char *document, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr || collectionName == nullptr) { + return GRD_INVALID_ARGS; + } + return db->store_->InsertDocument(collectionName, document, flags); +} + +int GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, unsigned int flags) +{ + if (db == nullptr || db->store_ == nullptr) { + return GRD_INVALID_ARGS; + } + return db->store_->DeleteDocument(collectionName, filter, flags); +} + +int GRD_FindDoc(GRD_DB *db, const char *collectionName, Query query, unsigned int flags, GRD_ResultSet **resultSet) +{ + if (db == nullptr || db->store_ == nullptr) { + return GRD_INVALID_ARGS; + } + return db->store_->FindDocument(db, collectionName, query.filter, query.projection, flags, resultSet[0]->resultSet_); +} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/grd_format_config.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/grd_format_config.h new file mode 100644 index 00000000..e18f04df --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/grd_format_config.h @@ -0,0 +1,32 @@ +/* +* 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_ERROR_H +#define GRD_ERROR_H + + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#define COLLECTION_LENS_MAX (512) +#define JSON_LENS_MAX (512) +#define JSON_DEEP_MAX (4) + + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // GRD_FORMAT_CONFIG_H \ No newline at end of file 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 index afc300ec..d46324ff 100644 --- 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 @@ -22,4 +22,8 @@ typedef struct GRD_DB { DocumentDB::DocumentStore *store_ = nullptr; } GRD_DB; +typedef struct GRD_ResultSet { + DocumentDB::ResultSet *resultSet_ = new DocumentDB::ResultSet(); +} GRD_ResultSet; + #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 index 6dba77c9..519d50a4 100644 --- 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 @@ -24,6 +24,7 @@ namespace DocumentDB { class Collection { public: Collection(std::string name, KvStoreExecutor *executor); + Collection(const Collection& collection); ~Collection(); int PutDocument(const Key &key, const Value &document); 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 93925e55..971d4e4c 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 @@ -21,6 +21,9 @@ #include "collection.h" #include "kv_store_executor.h" +#include "result_set.h" +#include "projection_tree.h" +#include "json_common.h" namespace DocumentDB { class DocumentStore { @@ -30,11 +33,13 @@ public: 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); - + int InsertDocument(const std::string &collection, const std::string &document, int flag); + int DeleteDocument(const std::string &collection, const std::string &document, int flag); + int FindDocument(GRD_DB* db, const std::string &collection, const std::string &filter, const std::string &projection, int flags, ResultSet *resultSet); private: + int GetViewType(JsonObject &jsonObj, bool &viewType); KvStoreExecutor *executor_ = nullptr; std::map collections_; }; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/projection_tree.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/projection_tree.h new file mode 100644 index 00000000..96bf930a --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/projection_tree.h @@ -0,0 +1,37 @@ +#ifndef PROJECTION_TREE_H +#define PROJECTION_TREE_H + +#include +#include +#include +#include "doc_errno.h" +#include "json_common.h" +#include "log_print.h" + +namespace DocumentDB { +struct ProjectionNode { + std::unordered_map SonNode; + bool isDeepest; + int Deep; + int ViewType; + ProjectionNode() { + Deep = 1; + isDeepest = true; + } +}; +class ProjectionTree { +public: + ProjectionTree() = default; + + ~ProjectionTree() = default; + + void Init() { + root_ = new ProjectionNode(); + } + int ParseTree(std::vector> &path); + bool SerchTree(std::vector &onePath, int &index); +private: + ProjectionNode *root_; +}; +} // DocumentDB +#endif // PROJECTION_TREE_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/result_set.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/result_set.h new file mode 100644 index 00000000..ab710712 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/result_set.h @@ -0,0 +1,51 @@ +/* +* 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 RESULTSET_H +#define RESULTSET_H + +#include +#include "grd_base/grd_type_export.h" +#include "projection_tree.h" +#include "vector" +#include "doc_errno.h" +#include "cjson_object.h" +#include "securec.h" +#include "collection.h" +#include "doc_common.h" + +namespace DocumentDB { + +class ResultSet { +public: + ResultSet() = default; + ~ResultSet(); + + int Init(Collection &coll, const std::string &key, std::vector> &path, bool ifShowId, bool viewType); + int GetNext(); + int GetValue(char **value); +private: + int CutString(std::string &jsonData); + int CheckCutNode(JsonObject *node, std::vector onePath, std::vector> &allPath, int blother_index); + Collection* coll_; + std::string key_; + bool ifShowId_; + bool viewType_; + ProjectionTree* projectionTree_; + std::vector> path_; + int index_; +}; +} // DocumentDB +#endif //RESULTSET_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 index 3e2562b1..0f66ca62 100644 --- 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 @@ -15,12 +15,19 @@ #include "collection.h" #include "doc_errno.h" +#include "log_print.h" namespace DocumentDB { Collection::Collection(std::string name, KvStoreExecutor *executor) : name_(name), executor_(executor) { } +Collection::Collection(const Collection& collection) +{ + name_ = collection.name_; + executor_ = collection.executor_; +} + Collection::~Collection() { executor_ = nullptr; @@ -36,7 +43,10 @@ int Collection::PutDocument(const Key &key, const Value &document) int Collection::GetDocument(const Key &key, Value &document) const { - return E_OK; + if (executor_ == nullptr) { + return -E_INVALID_ARGS; + } + return executor_->GetData(name_, key, document); } int Collection::DeleteDocument(const Key &key) 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 6fd1f679..b7a96501 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 @@ -15,6 +15,13 @@ #include "document_store.h" #include "doc_errno.h" +<<<<<<< Updated upstream +======= +#include "json_common.h" +#include "cjson_object.h" +#include "log_print.h" +#include "doc_common.h" +>>>>>>> Stashed changes namespace DocumentDB { DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) @@ -54,4 +61,4 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri return coll.PutDocument(key, value); } -} // namespace DocumentDB \ No newline at end of file +} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/projection_tree.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/projection_tree.cpp new file mode 100644 index 00000000..333c787e --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/projection_tree.cpp @@ -0,0 +1,50 @@ +#include +#include "projection_tree.h" + + +namespace DocumentDB { +int ProjectionTree::ParseTree(std::vector> &path) { + ProjectionNode *node = root_; + if (node == NULL) { + return E_OK; + } + for (int i = 0; i < path.size(); i++) { + node = root_; + for (int j = 0; j < path[i].size(); j++) { + if (node->SonNode[path[i][j]] != nullptr) { + node = node->SonNode[path[i][j]]; + if (j < path[i].size() - 1 && node->isDeepest == true) { + return E_INVALID_ARGS; + } + if (j == path[i].size() - 1 && node->isDeepest == false) { + return E_INVALID_ARGS; + } + } + else { + auto tempNode = new ProjectionNode; + tempNode->Deep = node->Deep + 1; + node->isDeepest = false; + node->SonNode[path[i][j]] = tempNode; + node = node->SonNode[path[i][j]]; + } + } + } + return E_OK; +} + +bool ProjectionTree::SerchTree(std::vector &onePath, int &index) { + ProjectionNode *node = root_; + for (int i = 0; i < onePath.size(); i++) { + if (node->isDeepest) { + index = i; + } + if (node->SonNode[onePath[i]] != nullptr) { + node = node->SonNode[onePath[i]]; + } + else { + return false; + } + } + return true; +} +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/result_set.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/result_set.cpp new file mode 100644 index 00000000..d363bae9 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/result_set.cpp @@ -0,0 +1,104 @@ +/* +* 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 "result_set.h" +#include "log_print.h" + +namespace DocumentDB { +int ResultSet::Init(Collection &coll, const std::string &key, std::vector> &path, bool ifShowId, bool viewType) +{ + coll_ = new Collection(coll); + key_ = key; + path_ = path; + ProjectionTree* projectionTree = new ProjectionTree(); + projectionTree->Init(); + if (projectionTree->ParseTree(path) == E_INVALID_ARGS) { + return E_INVALID_ARGS; + } + projectionTree_ = projectionTree; + ifShowId_ = ifShowId; + viewType_ = viewType; +} + +int ResultSet::GetNext() +{ + index_++; + return E_OK; +} + +int ResultSet::GetValue(char **value) +{ + if (index_ != 1) { + return E_OK; + } + if (projectionTree_ == nullptr) { + GLOGE("======>projectionTree_ is null"); + return E_INVALID_ARGS; + } + Value document; + Key key(key_.begin(), key_.end()); + coll_->GetDocument(key, document); + std::string jsonData(document.begin(), document.end()); + CutString(jsonData); + errno_t err = strcpy_s(value[0], jsonData.size() + 1, jsonData.c_str()); + if (err != 0) { + GLOGE("strcpy_s failed"); + } + return E_OK; +} + +int ResultSet::CheckCutNode(JsonObject *node, std::vector onePath, std::vector> &allPath, int blother_index) +{ + if (node == nullptr) { + return E_OK; + } + onePath.emplace_back(node->GetItemFiled()); + int index = 0; + if (projectionTree_->SerchTree(onePath, index) == false && index == 0) { + allPath.emplace_back(onePath); + } + if (node->GetChild() != nullptr) { + CheckCutNode(node->GetChild(), onePath, allPath, index); + } + if (node->GetNext() != nullptr) { + onePath.pop_back(); + CheckCutNode(node->GetNext(), onePath, allPath, index); + } + return E_OK; +} +int ResultSet::CutString(std::string &jsonData) +{ + CjsonObject cjsonObj; + cjsonObj.Init(jsonData); + std::vector> allPath; + if (viewType_ == true) { + std::vector onePath; + CheckCutNode(cjsonObj.GetChild(), onePath, allPath, 0); + for (int i = 0; i < allPath.size(); i++) { + if (ifShowId_ == false || allPath[i][0] != "_id") { + cjsonObj.DeleteItemOnTarget(allPath[i]); + } + } + } + if (viewType_ == false) { + for (int i = 0; i < path_[0].size() - 1; i++) { + if (ifShowId_ == false || path_[i][0] != "_id") { + cjsonObj.DeleteItemOnTarget(path_[i]); + } + } + } + jsonData = cjsonObj.Print(); + return E_OK; +} +} // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h index f0ebd8fd..b7b5a6c1 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h @@ -40,17 +40,18 @@ public: class JsonObject { public: - JsonObject () = default; virtual ~JsonObject () = default; - - virtual int Parse(const std::string &str) = 0; + virtual int Init(const std::string &str) = 0; virtual std::string Print() = 0; - virtual JsonObject* GetObjectItem(const std::string &field) = 0; + virtual JsonObject* GetObjectItem(const std::string &field, bool caseSensitive) = 0; virtual JsonObject* GetArrayItem(const int index) = 0; + virtual JsonObject* GetNext() = 0; + virtual JsonObject* GetChild() = 0; virtual ResultValue* GetItemValue() = 0; virtual std::string GetItemFiled() = 0; virtual int DeleteItemFromObject(const std::string &field) = 0; virtual int Delete() = 0; + virtual int DeleteItemOnTarget(std::vector path) = 0; }; } // DocumentDB #endif // JSON_OBJECT_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp index 05eb385b..3889968a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp @@ -16,14 +16,52 @@ #include "doc_errno.h" #include "cjson_object.h" #include "log_print.h" +#include "grd_format_config.h" + namespace DocumentDB { -int CjsonObject::Parse(const std::string &str) +CjsonObject::CjsonObject() +{ +} + +CjsonObject::CjsonObject(const CjsonObject& newObj) +{ + cjson_ = newObj.cjson_; +} + +CjsonObject::~CjsonObject() +{ + +} + +int CjsonObject::GetDeep(cJSON *cjson, int deep, int &maxDeep) { + if (cjson->child == nullptr) { + maxDeep = std::max(deep, maxDeep); + } + if (cjson->child != nullptr) { + GetDeep(cjson->child, deep + 1, maxDeep); + } + if (cjson->next != nullptr) { + GetDeep(cjson->next, deep, maxDeep); + } + return E_OK; +} + +int CjsonObject::Init(const std::string &str) +{ + if (str.length() + 1 > JSON_LENS_MAX) { + return E_INVALID_ARGS; + } cjson_ = cJSON_Parse(str.c_str()); if (cjson_ == nullptr) { - return E_ERROR; + return E_INVALID_ARGS; + } + int deep = 0; + GetDeep(cjson_, 0, deep); + if (deep > JSON_DEEP_MAX) { + return E_INVALID_ARGS; } return E_OK; } @@ -31,31 +69,27 @@ int CjsonObject::Parse(const std::string &str) std::string CjsonObject::Print() { if (cjson_ == nullptr) { - GLOGE("======================>NULL"); return ""; } - char *ret = cJSON_Print(cjson_); + char *ret = cJSON_PrintUnformatted(cjson_); std::string str = ret; free(ret); - GLOGE("======================>%s", str.c_str()); return str; } -CjsonObject* CjsonObject::GetObjectItem(const std::string &field) +JsonObject* CjsonObject::GetObjectItem(const std::string &field, bool caseSensitive) { - if (cjson_ == nullptr) { - return nullptr; - } CjsonObject *item = new CjsonObject(); - auto cjson_item = cJSON_GetObjectItem(cjson_, field.c_str()); - if (cjson_item == nullptr) { - return item; + if (caseSensitive) { + cJSON *cjson_item = cJSON_GetObjectItemCaseSensitive(cjson_, field.c_str()); + } else { + cJSON *cjson_item = cJSON_GetObjectItem(cjson_, field.c_str()); + item->cjson_ = cjson_item; } - item->cjson_ = cjson_item; return item; } -CjsonObject* CjsonObject::GetArrayItem(const int index) +JsonObject* CjsonObject::GetArrayItem(const int index) { if (cjson_ == nullptr) { return nullptr; @@ -69,27 +103,27 @@ CjsonObject* CjsonObject::GetArrayItem(const int index) return item; } -CjsonObject* CjsonObject::GetNext() +JsonObject* CjsonObject::GetNext() { if (cjson_ == nullptr) { return nullptr; } CjsonObject *next = new CjsonObject(); if (cjson_->next == nullptr) { - return next; + return nullptr; } next->cjson_ = cjson_->next; return next; } -CjsonObject* CjsonObject::GetChild() +JsonObject* CjsonObject::GetChild() { if (cjson_ == nullptr) { return nullptr; } CjsonObject *child = new CjsonObject(); if (cjson_->child == nullptr) { - return child; + return nullptr; } child->cjson_ = cjson_->child; return child; @@ -100,7 +134,7 @@ int CjsonObject::DeleteItemFromObject(const std::string &field) if (field == "") { return E_OK; } - cJSON_DeleteItemFromObject(cjson_, field.c_str()); + cJSON_DeleteItemFromObjectCaseSensitive(cjson_, field.c_str()); return E_OK; } @@ -142,13 +176,17 @@ ResultValue* CjsonObject::GetItemValue() case cJSON_Array: { value->value_type = ResultValue::ValueType::VALUE_ARRAY; - value->value_Object = GetChild(); + if (GetChild() != nullptr) { + value->value_Object = GetChild(); + } } break; case cJSON_Object: { value->value_type = ResultValue::ValueType::VALUE_OBJECT; - value->value_Object = GetChild(); + if (GetChild() != nullptr) { + value->value_Object = GetChild(); + } } break; default: @@ -160,13 +198,12 @@ ResultValue* CjsonObject::GetItemValue() std::string CjsonObject::GetItemFiled() { if (cjson_ == nullptr) { - GLOGE("cjson is null"); return ""; } if (cjson_->string == nullptr) { - GLOGE("cjson string is null"); return ""; } + //GLOGE("=========>%s", cjson_->string); return cjson_->string; } @@ -178,4 +215,101 @@ int CjsonObject::Delete() return E_OK; } +CjsonObject* CjsonObject::FindItem(const std::vector json_path) +{ + auto a = cjson_; + cJSON *cJsondata_item = a; + cJSON *cJsondata_temp; + if (json_path.size() != 0 && json_path[0] == "") { + return this; + } + for (int i = 0; i < json_path.size(); i++) { + if (cJsondata_item->type == cJSON_Object) { + if (cJSON_GetObjectItem(cJsondata_item,json_path[i].c_str()) == nullptr) { + return nullptr; + } + cJsondata_temp = cJSON_GetObjectItem(cJsondata_item,json_path[i].c_str()); + cJsondata_item = cJsondata_temp; + } + if (cJsondata_item->type == cJSON_Array) { + int nums = 0; + for (int j = 0; j < json_path[i].size(); j++) { + if (json_path[i][j]-'0' > 9 || json_path[i][j] - '0' < 0 || json_path[i][j] - '0' > cJSON_GetArraySize(cJsondata_item)) { + break; + } + auto GetArrayret = cJSON_GetArrayItem(cJsondata_item, json_path[i][j]-'0'); + if (typeid(GetArrayret) == typeid(cJSON*)) { + cJsondata_item = GetArrayret; + } + else { + if ( i != json_path.size() - 1) { + GLOGD("[cjson_object]=====>Patharg wrong"); + } + else { + return nullptr; + } + } + } + } + + } + CjsonObject *item = new CjsonObject(); + item->cjson_ = cJsondata_item; + return item; +} + +int CjsonObject::DeleteItemOnTarget(std::vector path) +{ + cJSON *node_father; + cJSON *cJsondata_root; + std::string lastString; + if (path.size()>0) { + lastString = path.back(); + path.pop_back(); + if (path.size() == 0) { + std::vector emptyPath; + auto cjsonObj = FindItem(emptyPath); + if (cjsonObj == nullptr) { + return E_OK; + } + node_father = cjsonObj->cjson_; + cJsondata_root = cjsonObj->cjson_; + path.emplace_back(lastString); + } + else { + std::vector forePath; + for (int i = 0; i < path.size() - 1; i++) { + forePath.emplace_back(path[i]); + } + auto foreObj = FindItem(forePath); + if (foreObj == nullptr) { + return E_OK; + } + cJsondata_root = foreObj->cjson_; + auto cjsonObj = FindItem(path); + if (cjsonObj == nullptr) { + return E_OK; + } + node_father = cjsonObj->cjson_; + path.emplace_back(lastString); + } + } + if (node_father->type == cJSON_Object) { + if (cJSON_GetObjectItem(node_father, lastString.c_str()) != nullptr) { + cJSON_DeleteItemFromObject(node_father, lastString.c_str()); + } + else { + GLOGE("no item that can be deleted"); + } + } + if (node_father->type == cJSON_Array) { + if (cJSON_GetArrayItem(node_father, lastString[0] - '0') != nullptr) { + cJSON_DeleteItemFromArray(node_father, lastString[0] - '0'); + } + else { + GLOGE("no item that can be deleted"); + } + } + return E_OK; +} } \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h index dcda3cb2..10c2a8d0 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h @@ -17,26 +17,33 @@ #define CJSON_OBJECT_H #include +#include +#include + #include "cJSON.h" #include "json_object.h" namespace DocumentDB { -class CjsonObject : public JsonObject{ +class CjsonObject : public virtual JsonObject { public: - CjsonObject () = default; - virtual ~CjsonObject () = default; + CjsonObject(); + CjsonObject(const CjsonObject& newObj); + ~CjsonObject () override; - int Parse(const std::string &str) override; + int Init(const std::string &str) override; std::string Print() override; - CjsonObject* GetObjectItem(const std::string &field) override; - CjsonObject* GetArrayItem(const int index) override; - CjsonObject* GetNext(); - CjsonObject* GetChild(); + JsonObject* GetObjectItem(const std::string &field, bool caseSensitive) override; + JsonObject* GetArrayItem(const int index) override; + JsonObject* GetNext() override; + JsonObject* GetChild() override; int DeleteItemFromObject(const std::string &field) override; ResultValue* GetItemValue() override; std::string GetItemFiled() override; int Delete() override; + CjsonObject* FindItem(const std::vector json_path); + int DeleteItemOnTarget(std::vector path); private: + int GetDeep(cJSON *cjson, int deep, int &maxDeep); cJSON *cjson_; }; } // DocumentDB 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 9f091ca1..8b56bce5 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 @@ -30,11 +30,19 @@ SqliteStoreExecutor::~SqliteStoreExecutor() int SqliteStoreExecutor::PutData(const std::string &collName, const Key &key, const Value &value) { +<<<<<<< Updated upstream if (dbHandle_ == nullptr) { return -E_ERROR; } std::string sql = "INSERT OR REPLACE INTO '" + collName + "' VALUES (?,?);"; +======= + std::string key_value (key.begin(), key.end()); + if (dbHandle_ == nullptr) { + return -E_ERROR; + } + std::string sql = "INSERT INTO '" + collName + "' VALUES (?,?);"; +>>>>>>> Stashed changes int errCode = SQLiteUtils::ExecSql(dbHandle_, sql, [key, value](sqlite3_stmt *stmt) { SQLiteUtils::BindBlobToStatement(stmt, 1, key); SQLiteUtils::BindBlobToStatement(stmt, 2, value); @@ -52,7 +60,10 @@ int SqliteStoreExecutor::GetData(const std::string &collName, const Key &key, Va if (dbHandle_ == nullptr) { return -E_ERROR; } - + std::string key_value (key.begin(), key.end()); + 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); @@ -65,6 +76,10 @@ int SqliteStoreExecutor::GetData(const std::string &collName, const Key &key, Va GLOGE("[sqlite executor] create collectoin failed. err=%d", errCode); return errCode; } + std::string ret_value (value.begin(), value.end()); + if (dbHandle_ == nullptr) { + return -E_ERROR; + } return E_OK; } @@ -97,4 +112,21 @@ int SqliteStoreExecutor::DropCollection(const std::string &name, int flag) } return E_OK; } + +int SqliteStoreExecutor::DeleteData(const std::string &collName, const Key &key) const +{ + std::string key_value (key.begin(), key.end()); + sqlite3 *db = dbHandle_; + const std::string sql_delete = "DELETE FROM '" + collName + "' WHERE key = ?;"; + const std::function &bindCallback = [&](sqlite3_stmt *stmt) -> int { + SQLiteUtils::BindBlobToStatement(stmt, 1, key); + return E_OK; + }; + int errCode = SQLiteUtils::ExecSql(db, sql_delete, bindCallback, nullptr); + if (errCode != SQLITE_OK) { + GLOGE("[sqlite executor] create 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/test/unittest/api/ documentdb_delete_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_delete_test.cpp new file mode 100644 index 00000000..3efe574b --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_delete_test.cpp @@ -0,0 +1,78 @@ +/* +* 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" +#include "grd_document/grd_document_api.h" +#include "grd_base/grd_error.h" +using namespace testing::ext; +namespace { + +std::string path = "./document.db"; +GRD_DB *g_db = nullptr; +} + +class DocumentDeleteApiTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + void InsertDoc(const char *collectionName, const char *document); +}; +void DocumentDeleteApiTest::SetUpTestCase(void) +{ + GRD_DBOpen(path.c_str(), nullptr, 0, &g_db); + EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); + EXPECT_NE(g_db, nullptr); +} + +void DocumentDeleteApiTest::TearDownTestCase(void) +{ + EXPECT_EQ(GRD_DBClose(g_db, 0), GRD_OK); + remove(path.c_str()); +} + +void DocumentDeleteApiTest::SetUp(void) +{ +} + +void DocumentDeleteApiTest::TearDown(void) +{ +} + + +void DocumentDeleteApiTest::InsertDoc(const char *collectionName, const char *document) +{ + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, document, 0), GRD_OK); +} + +/** + * @tc.name: DocumentDelete001 + * @tc.desc: Test Insert document db + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ + +HWTEST_F(DocumentDeleteApiTest, DeleteDBTest001, TestSize.Level1) +{ + const char *collectionName = "student"; + const char *document = "{\"_id\":\"12345\", \"name\":\"jack\"}"; + const char *filter = "{\"_id\":\"12345\", \"name\":\"jack\"}"; + InsertDoc(collectionName, document); + EXPECT_EQ(GRD_DeleteDoc(g_db, collectionName, filter, 0), GRD_OK); +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_find_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_find_test.cpp new file mode 100644 index 00000000..6dc34e9f --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_find_test.cpp @@ -0,0 +1,93 @@ +/* +* 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" +#include "grd_document/grd_document_api.h" +#include "grd_base/grd_error.h" +#include "grd_base/grd_type_export.h" +#include "grd_type_inner.h" +#include "grd_base/grd_resultset_api.h" +using namespace testing::ext; +namespace { +std::string path = "./document.db"; +GRD_DB *g_db = nullptr; +} + +class DocumentFindApiTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + void InsertDoc(const char *collectionName, const char *document); +}; +void DocumentFindApiTest::SetUpTestCase(void) +{ + GRD_DBOpen(path.c_str(), nullptr, 0, &g_db); + EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); + EXPECT_NE(g_db, nullptr); +} + +void DocumentFindApiTest::TearDownTestCase(void) +{ + EXPECT_EQ(GRD_DBClose(g_db, 0), GRD_OK); + remove(path.c_str()); +} + +void DocumentFindApiTest::SetUp(void) +{ +} + +void DocumentFindApiTest::TearDown(void) +{ +} + + +void DocumentFindApiTest::InsertDoc(const char *collectionName, const char *document) +{ + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, document, 0), GRD_OK); +} + +/** + * @tc.name: DocumentDelete001 + * @tc.desc: Test Insert document db + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ + +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest, TestSize.Level1) +{ + const char *collectionName = "student"; + const char *document = "{\"_id\":\"12345\", \"name\":\"jack\", \"friend\" : {\"name\" : \"lucy\", \"country\" : \"Englis\", \"age\" : 19, \"t1\" : {\"t2\" : {\"t3\" : 1}, \"t5\" : {\"t3\" : 1}, \"t6\" : {\"t3\" : 1}}}}"; + InsertDoc(collectionName, document); + const char *filter = "{\"_id\":\"12345\", \"name\":\"jack\", \"friend\" : {\"name\" : \"lucy\", \"country\" : \"Englis\", \"age\" : 19}}"; + const char *projection = "{\"friend.t1\":{\"t2\":1, \"t5\":1}}"; + Query query; + query.filter = filter; + query.projection = projection; + GRD_ResultSet **resultSet; + resultSet = new GRD_ResultSet *[1]; + resultSet[0] = new GRD_ResultSet; + EXPECT_EQ(GRD_FindDoc(g_db, collectionName, query, GRD_DOC_ID_DISPLAY, resultSet), GRD_OK); + char **test; + test = new char*[1]; + test[0] = new char[521 + 1]; + GRD_Next(resultSet[0]); + GRD_GetValue(resultSet[0], test); + GRD_FreeValue(test[0]); +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_insert_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_insert_test.cpp new file mode 100644 index 00000000..bbf2becb --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_insert_test.cpp @@ -0,0 +1,309 @@ +/* +* 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" +#include "grd_document/grd_document_api.h" +#include "grd_base/grd_error.h" +using namespace testing::ext; + +#define JSON_STR_HASID "{\"_id\" : 1, \"name\" : \"Ori\"}" +namespace { + +std::string path = "./document.db"; +GRD_DB *g_db = nullptr; +const char *right_collectionName = "student"; +const char *no_exsit_collectionName = "no_exisit"; +const int int_max = 2147483647; +const int int_min = -2147483648; +const int max_collection_lens = 511; +} + +class DocumentInsertApiTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +void DocumentInsertApiTest::SetUpTestCase(void) +{ + GRD_DBOpen(path.c_str(), nullptr, 0, &g_db); + EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); + EXPECT_NE(g_db, nullptr); +} + +void DocumentInsertApiTest::TearDownTestCase(void) +{ + EXPECT_EQ(GRD_DBClose(g_db, 0), GRD_OK); + remove(path.c_str()); +} + +void DocumentInsertApiTest::SetUp(void) +{ +} + +void DocumentInsertApiTest::TearDown(void) +{ +} + + +/** + * @tc.name: DocumentInsertApiTest001 + * @tc.desc: Insert documents into collection which dose not exist + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, InsertDBTest001, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert document into collection which dose not exist + * @tc.expected:step1.GRD_NO_DATA + */ + EXPECT_NE(GRD_InsertDoc(g_db, no_exsit_collectionName, JSON_STR_HASID, 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest002 + * @tc.desc: Insert documents into collection which dose not exist + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, InsertDBTest002, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert a document whose _id is integer + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *document1 = "{\"_id\" : 1, \"name\" : \"Ori\"}"; + EXPECT_NE(GRD_InsertDoc(g_db, right_collectionName, document1, 0), GRD_OK); + /** + * @tc.steps:step2.Insert a document whose _id has appeared before + * @tc.expected:step2.GRD_INVALID_ARGS + */ + const char *document2 = "{\"_id\" : 1, \"name\" : \"Chuan\"}"; + EXPECT_NE(GRD_InsertDoc(g_db, right_collectionName, document2, 0), GRD_OK); +} + + +/** + * @tc.name: DocumentInsertApiTest003 + * @tc.desc: Insert a document whose _id has appeared before + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, InsertDBTest003, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert a document whose _id is string + * @tc.expected:step1.GRD_OK + */ + const char *document1 = "{\"_id\" : \"1\", \"name\" : \"Ori\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, right_collectionName, document1, 0), GRD_OK); + + /** + * @tc.steps:step2.Insert a document whose _id has appeared before + * @tc.expected:step2.GRD_DATA_CONFLICT + */ + const char *document2 = "{\"_id\" : \"1\", \"name\" : \"Chuan\"}"; + EXPECT_NE(GRD_InsertDoc(g_db, right_collectionName, document2, 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest004 + * @tc.desc: Test Insert with null parameter. parameter db is NULL + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, InsertDBTest004, TestSize.Level1) +{ + /** + * @tc.steps:step1.step1.parameter db is NULL + * @tc.expected:step1.GRD_INVALID_ARGS + */ + EXPECT_NE(GRD_InsertDoc(NULL, right_collectionName, JSON_STR_HASID, 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest005 + * @tc.desc: Test insert with null parameter. parameter collectionName is NULL. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, InsertDBTest005, TestSize.Level1) +{ + /** + * @tc.steps:step1.Parameter collectionName is NULL + * @tc.expected:step1.GRD_INVALID_ARGS + */ + EXPECT_NE(GRD_InsertDoc(g_db, NULL, JSON_STR_HASID, 0), GRD_OK); + /** + * @tc.steps:step2.Parameter collectionName is empty string + * @tc.expected:step2.GRD_INVALID_ARGS + */ + const char *document2 = "{\"_id\" : 1, \"name\" : \"Chuan\"}"; + EXPECT_NE(GRD_InsertDoc(g_db, "", JSON_STR_HASID, 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest006 + * @tc.desc: parameter flags is not zero + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, InsertDBTest006, TestSize.Level1) +{ + /** + * @tc.steps:step1.parameter flags is not zero + * @tc.expected:step1.GRD_INVALID_ARGS + */ + EXPECT_NE(GRD_InsertDoc(g_db, right_collectionName, JSON_STR_HASID, 1), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest007 + * @tc.desc: parameter flags is INT_MAX + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, InsertDBTest007, TestSize.Level1) +{ + /** + * @tc.steps:step1.parameter flags is int_max + * @tc.expected:step1.GRD_INVALID_ARGS + */ + EXPECT_NE(GRD_InsertDoc(g_db, right_collectionName, JSON_STR_HASID, int_max), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest008 + * @tc.desc: parameter flags is int_min + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, InsertDBTest008, TestSize.Level1) +{ + /** + * @tc.steps:step1.parameter flags is int_min + * @tc.expected:step1.GRD_INVALID_ARGS + */ + EXPECT_NE(GRD_InsertDoc(g_db, right_collectionName, JSON_STR_HASID, int_min), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest009 + * @tc.desc: parameter collectionName and document is NULL or invalid + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, InsertDBTest009, TestSize.Level1) +{ + /** + * @tc.steps:step1.parameter collectionName and document is NULL; + * @tc.expected:step1.GRD_INVALID_ARGS + */ + EXPECT_NE(GRD_InsertDoc(g_db, NULL, NULL, 0), GRD_OK); + /** + * @tc.steps:step2.parameter collectionName is larger than max_collectionName_lens; + * @tc.expected:step2.GRD_INVALID_ARGS + */ + std::string collectionName2(max_collection_lens + 1, 'a'); + EXPECT_NE(GRD_InsertDoc(g_db, collectionName2.c_str(), JSON_STR_HASID, 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest010 + * @tc.desc: Inserted document's JSON depth is larger than 4, which is 5. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, InsertDBTest010, TestSize.Level1) +{ + /** + * @tc.steps:step1.Create Collection whose parameter collectionName contains irregular charactor + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *collectionName = "@!#"; + EXPECT_NE(GRD_InsertDoc(g_db, collectionName, JSON_STR_HASID, 0), GRD_OK); + /** + * @tc.steps:step2.parameter collectionName is larger than max_collectionName_lens; + * @tc.expected:step2.GRD_INVALID_ARGS + */ + std::string collectionName2(max_collection_lens + 1, 'a'); + EXPECT_NE(GRD_InsertDoc(g_db, collectionName2.c_str(), JSON_STR_HASID, int_min), GRD_OK); +} + + +/** + * @tc.name: DocumentInsertApiTest014 + * @tc.desc: Inserted document's JSON depth is larger than 4, which is 5. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, InsertDBTest014, TestSize.Level1) +{ + /** + * @tc.steps:step1.document's JSON depth is larger than 4, which is 5. + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *document1 = "{\"level1\" : {\"level2\" : {\"level3\" : [ { \"level5\" : \"level5_1val\", \"level5_2\":\ + \"level5_2_val\"}, \"level4_val1\",\"level4_val2\"], \"level3_2\" : \"level3_2_val\"\ + }},\"_id\":\"2\"}"; + EXPECT_NE(GRD_InsertDoc(g_db, right_collectionName, document1, 0), GRD_OK); + /** + * @tc.steps:step1.document's JSON depth is 4 + * @tc.expected:step1.GRD_OK + */ + const char *document2 = "{\"level1\" : {\"level2\" : {\"level3\" : { \"level4\" : \"level5_1val\"}, \"level3_2\" : \"level3_2_val\"\ + }},\"_id\":\"2\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, right_collectionName, document2, 0), GRD_OK); +} + +/** + * @tc.name: DocumentInsertApiTest015 + * @tc.desc: Inserted wrong document's filed + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentInsertApiTest, InsertDBTest015, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert document whose field dose not contain id. + * @tc.expected:step1.GRD_INVALID_ARGS + */ + const char *document1 = "{\"age\" : \"3\", \"name\" : \"Ori\"}"; + EXPECT_NE(GRD_InsertDoc(g_db, right_collectionName, document1, 0), GRD_OK); + /** + * @tc.steps:step2.Insert document whose field contains special characters + * @tc.expected:step2.GRD_INVALID_ARGS + */ + const char *document2 = "{\"_id\" : \"3\", \"@&name\" : \"Chuan\"}"; + EXPECT_NE(GRD_InsertDoc(g_db, right_collectionName, document2, 0), GRD_OK); +} + + diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/json_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/json_test.cpp new file mode 100644 index 00000000..020ece93 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/json_test.cpp @@ -0,0 +1,71 @@ +/* +* 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" +#include "grd_document/grd_document_api.h" +#include "grd_base/grd_error.h" +#include "cjson_object.h" +#include "json_object.h" +#include "cJSON.h" + + +using namespace testing::ext; +namespace { + string JSON_Test1 = "{\"5\":[{\"2\":[[{\"0\":\"value_\"},\"value_\"],\"value_\"],\"3\":\"value_\"}," + "\"value_\"],\"6\":{\"7\":\"value_\",\"8\":\"value_\"}, \"_id\":\"12345\"}";; +} +class jsonObjectTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + void InsertDoc(const char *collectionName, const char *document); +}; +void jsonObjectTest::SetUpTestCase(void) +{ +} + +void jsonObjectTest::TearDownTestCase(void) +{ +} + +void jsonObjectTest::SetUp(void) +{ +} + +void jsonObjectTest::TearDown(void) +{ +} + + +/** + * @tc.name: JsonTest001 + * @tc.desc: JsonTest parse and print + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ + +HWTEST_F(jsonObjectTest, jsonObject001, TestSize.Level1) +{ + DocumentDB::CjsonObject cjson; + cjson.Init(::JSON_Test1); + cjson.Print(); + // EXPECT_EQ(strcmp(cjson.GetObjectItem("#6")->Print().c_str(), cJSON_Print(cJSON_GetObjectItem(cJSON_Parse(::JSON_Test1.c_str()), "#6"))), 0); + // EXPECT_NE(cjson.GetNext(),nullptr); +} -- Gitee From d3f6b4f438df13862abfb96fd06cbe9263bfe407 Mon Sep 17 00:00:00 2001 From: mazhao Date: Sat, 1 Apr 2023 02:31:18 +0000 Subject: [PATCH 2/2] Functional complements Signed-off-by: mazhao --- .../src/interface/src/document_store.cpp | 161 +++++++++++++++++- .../oh_adapter/include/kv_store_executor.h | 2 + .../src/sqlite_store_executor_impl.cpp | 10 +- .../src/sqlite_store_executor_impl.h | 2 +- 4 files changed, 162 insertions(+), 13 deletions(-) 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 b7a96501..9ec1b382 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 @@ -15,13 +15,10 @@ #include "document_store.h" #include "doc_errno.h" -<<<<<<< Updated upstream -======= #include "json_common.h" #include "cjson_object.h" #include "log_print.h" #include "doc_common.h" ->>>>>>> Stashed changes namespace DocumentDB { DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) @@ -61,4 +58,162 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri return coll.PutDocument(key, value); } + +int DocumentStore::InsertDocument(const std::string &collection, const std::string &document, int flag) +{ + // if (!CheckCommon::CheckDocument(document) || !CheckCommon::CheckCollectionName(collection) || flag != 0) { + // return E_INVALID_ARGS; + // } + auto coll = Collection(collection, executor_); + CjsonObject documentObj; + if (documentObj.Init(document) != E_OK) { + return E_INVALID_ARGS; + } + auto id_value = JsonCommon::GetValueByFiled(documentObj.GetChild(), "_id"); + if (id_value == nullptr) { + return E_INVALID_ARGS; + } + if (id_value->value_type != ResultValue::ValueType::VALUE_STRING) { + return E_INVALID_ARGS; + } + std::string id_str = id_value->value_string; + Key key(id_str.begin(), id_str.end()); + Value value(document.begin(), document.end()); + return coll.PutDocument(key, value); +} + +int DocumentStore::DeleteDocument(const std::string &collection, const std::string &filter, int flag) +{ + if (!CheckCommon::CheckFilter(filter) || !CheckCommon::CheckCollectionName(collection) || flag != 0) { + return E_INVALID_ARGS; + } + auto coll = Collection(collection, executor_); + CjsonObject filterObj; + if (filterObj.Init(filter) != E_OK) { + return E_INVALID_ARGS; + } + auto id_value = JsonCommon::GetValueByFiled(filterObj.GetChild(), "_id"); + if (id_value == nullptr) { + return E_INVALID_ARGS; + } + if (id_value->value_type != ResultValue::ValueType::VALUE_STRING) { + return E_INVALID_ARGS; + } + std::string id_str = id_value->value_string; + Key key(id_str.begin(), id_str.end()); + return coll.DeleteDocument(key); +} + +int DocumentStore::FindDocument(GRD_DB *db, const std::string &collection, const std::string &filter, const std::string &projection, int flags, ResultSet *resultSet) +{ + if (!CheckCommon::CheckFilter(filter) || !CheckCommon::CheckCollectionName(collection)) { + return E_INVALID_ARGS; + } + CjsonObject filterObj; + if (filterObj.Init(filter) != E_OK) { + return E_INVALID_ARGS; + } + auto id_value = JsonCommon::GetValueByFiled(filterObj.GetChild(), "_id"); + if (id_value == nullptr) { + return E_INVALID_ARGS; + } + if (id_value->value_type != ResultValue::ValueType::VALUE_STRING) { + return E_INVALID_ARGS; + } + std::string filter_id = id_value->value_string; + CjsonObject projectionObj; + if (projectionObj.Init(projection) != E_OK) { + return E_INVALID_ARGS; + } + bool viewType = false; + if (GetViewType(projectionObj, viewType) != E_OK) { + return E_INVALID_ARGS; + } + bool ifShowId = false; + auto projection_id = JsonCommon::GetValueByFiled(projectionObj.GetChild(), "_id"); + if (flags == GRD_DOC_ID_DISPLAY) { + if (projection_id != nullptr && viewType == true) { + ifShowId = false; + } + else { + ifShowId = true; + } + } + auto all_path = JsonCommon::ParsePath(&projectionObj); + Collection coll = Collection(collection, executor_); + resultSet->Init(coll, filter_id, all_path, ifShowId, viewType); + return E_OK; +} + +int DocumentStore::GetViewType(JsonObject &jsonObj, bool &viewType) { + auto leafValue = JsonCommon::GetLeafValue(&jsonObj); + if (leafValue.size() == 0) { + return E_INVALID_ARGS; + } + switch (leafValue[0]->value_type) { + case ResultValue::ValueType::VALUE_TRUE: + viewType = true; + break; + case ResultValue::ValueType::VALUE_FALSE: + viewType = false; + break; + case ResultValue::ValueType::VALUE_STRING: + if (leafValue[0]->value_string == "") { + viewType = true; + } + else { + return E_INVALID_ARGS; + } + break; + case ResultValue::ValueType::VALUE_NUMBER: + if (leafValue[0]->value_number == 0) { + viewType = false; + } + else { + viewType = true; + } + break; + default: + return E_INVALID_ARGS; + } + for (int i = 1; i < leafValue.size(); i++) { + switch (leafValue[i]->value_type) { + case ResultValue::ValueType::VALUE_TRUE: + if (viewType != true) { + return E_INVALID_ARGS; + } + break; + case ResultValue::ValueType::VALUE_FALSE: + if (viewType != false) { + return E_INVALID_ARGS; + } + break; + case ResultValue::ValueType::VALUE_STRING: + if (leafValue[i]->value_string == "") { + if (viewType != false) { + return E_INVALID_ARGS; + } + } + else { + return E_INVALID_ARGS; + } + break; + case ResultValue::ValueType::VALUE_NUMBER: + if (leafValue[i]->value_number == 0) { + if (viewType != false) { + return E_INVALID_ARGS; + } + } + else { + if (viewType != true) { + return E_INVALID_ARGS; + } + } + break; + default: + return E_INVALID_ARGS; + } + } + return E_OK; +} } // namespace DocumentDB 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 f1d21444..2c3fc197 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 @@ -27,9 +27,11 @@ public: virtual int PutData(const std::string &collName, const Key &key, const Value &value) = 0; virtual int GetData(const std::string &collName, const Key &key, Value &value) const = 0; + virtual int DeleteData(const std::string &collName, const Key &key) = 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/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp index 8b56bce5..d882ee2c 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 @@ -30,19 +30,11 @@ SqliteStoreExecutor::~SqliteStoreExecutor() int SqliteStoreExecutor::PutData(const std::string &collName, const Key &key, const Value &value) { -<<<<<<< Updated upstream - if (dbHandle_ == nullptr) { - return -E_ERROR; - } - - std::string sql = "INSERT OR REPLACE INTO '" + collName + "' VALUES (?,?);"; -======= std::string key_value (key.begin(), key.end()); if (dbHandle_ == nullptr) { return -E_ERROR; } std::string sql = "INSERT INTO '" + collName + "' VALUES (?,?);"; ->>>>>>> Stashed changes int errCode = SQLiteUtils::ExecSql(dbHandle_, sql, [key, value](sqlite3_stmt *stmt) { SQLiteUtils::BindBlobToStatement(stmt, 1, key); SQLiteUtils::BindBlobToStatement(stmt, 2, value); @@ -113,7 +105,7 @@ int SqliteStoreExecutor::DropCollection(const std::string &name, int flag) return E_OK; } -int SqliteStoreExecutor::DeleteData(const std::string &collName, const Key &key) const +int SqliteStoreExecutor::DeleteData(const std::string &collName, const Key &key) { std::string key_value (key.begin(), key.end()); sqlite3 *db = dbHandle_; 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 ab15f02d..d4301bac 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 @@ -30,7 +30,7 @@ public: int CreateCollection(const std::string &name, int flag) override; int DropCollection(const std::string &name, int flag) override; - + int DeleteData(const std::string &collName, const Key &key) override; private: sqlite3 *dbHandle_ = nullptr; }; -- Gitee