diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_error.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_error.h index 007d13aae0180cabb81889dc380344b270185500..16b5eb4b94ffded7618f1aa4cc47e060c5cd8338 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_error.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_error.h @@ -22,32 +22,64 @@ extern "C" { #endif // __cplusplus +// Error category #define GRD_OK 0 +// Error category #define GRD_NOT_SUPPORT (-1000) #define GRD_OVER_LIMIT (-2000) #define GRD_INVALID_ARGS (-3000) #define GRD_SYSTEM_ERR (-4000) +#define GRD_FAILED_FILE_FORMAT (-5000) +#define GRD_INVALID_FILE_FORMAT (-6000) #define GRD_INNER_ERR (-8000) #define GRD_RESOURCE_BUSY (-9000) #define GRD_NO_DATA (-11000) #define GRD_FAILED_MEMORY_ALLOCATE (-13000) #define GRD_FAILED_MEMORY_RELEASE (-14000) +#define GRD_DATA_CONFLICT (-16000) +#define GRD_NOT_AVAILABLE (-19000) #define GRD_INVALID_FORMAT (-37000) +#define GRD_REBUILD_DATABASE (-38000) -#define GRD_FIELD_NOT_FOUND (-5003002) -#define GRD_FIELD_TYPE_NOT_MATCH (-5003003) -#define GRD_LARGE_JSON_NEST (-5003004) +// not support +#define GRD_JSON_OPERATION_NOT_SUPPORT (-5001001) -#define GRD_UNVAILABLE_JSON_LIB (-5004001) +// Exceed limit +#define GRD_JSON_LEN_LIMIT (-5002001) +// Invalid parameter +#define GRD_FIELD_TYPE_NOT_MATCH (-5003001) +#define GRD_LARGE_JSON_NEST (-5003002) +#define GRD_INVALID_JSON_TYPE (-5003003) +#define GRD_INVALID_CONFIG_VALUE (-5003004) +#define GRD_INVALID_OPERATOR (-5003005) +#define GRD_INVALID_PROJECTION_FIELD (-5003006) +#define GRD_INVALID_PROJECTION_VALUE (-5003007) +#define GRD_ARRAY_INDEX_NOT_FOUND (-5003008) +// System err +#define GRD_JSON_LIB_HANDLE_FAILED (-5004001) + +// no data #define GRD_COLLECTION_NOT_FOUND (-5011001) #define GRD_RECORD_NOT_FOUND (-5011002) +#define GRD_DB_NOT_FOUND (-11003) +#define GRD_FIELD_NOT_FOUND (-5011004) -#define GRD_INVALID_JSON_FORMAT (-5037001) +// data conflicted +#define GRD_COLLECTION_CONFLICT (-5016001) +#define GRD_KEY_CONFLICT (-5016002) +#define GRD_FIELD_TYPE_CONFLICT (-5016003) +// Cursor or ResultSet not available +#define GRD_RESULT_SET_NOT_AVAILABLE (-5019001) + +// Invalid format +#define GRD_INVALID_JSON_FORMAT (-5037001) +#define GRD_INVALID_KEY_FORMAT (-5037002) +#define GRD_INVALID_COLLECTION_NAME (-5037003) #ifdef __cplusplus } #endif // __cplusplus diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_type_export.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_type_export.h index 352d2162b9916b154b8a1d038ac0a03e7c735490..0b8ad2c98f6237379a8e5056af5bdddcd7840792 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_type_export.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_base/grd_type_export.h @@ -27,15 +27,22 @@ typedef struct GRD_DB GRD_DB; */ #define GRD_DB_OPEN_ONLY 0x00 #define GRD_DB_OPEN_CREATE 0x01 - +#define GRD_DB_OPEN_CHECK_FOR_ABNORMAL 0x02 //check data in database if close abnormally last time, + //if data is corrupted, rebuild the database +#define GRD_DB_OPEN_CREATE 0x04//check data in database when open database, if data is corrupted, rebuild the database. /** * @brief Close database config */ #define GRD_DB_CLOSE 0x00 #define GRD_DB_CLOSE_IGNORE_ERROR 0x01 -#define GRD_DB_ID_DISPLAY 0x01 +/** + * @brief flush database config +*/ +#define GRD_DB_FLUSH_ASYNC 0x00 +#define GRD_DB_FLUSH_SYNC 0x01 +#define GRD_DOC_ID_DISPLAY 0x01 typedef struct Query { const char *filter; const char *projection; @@ -44,8 +51,11 @@ typedef struct Query { /** * @brief Flags for create and drop collection */ -#define FLAG_CHECK_UDEFINED_DUPLICAte_TABLE 1 +#define IGNORE_EXIST_TABLE 1 +#define IGNORE_NOT_EXIST_TABLE 1 +#define IGNORE_DOC_APPEND 0 +#define IGNORE_DOC_REPLACE 1 #ifdef __cplusplus } #endif // __cplusplus 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 fc062be4497d52feca315a8142c6a48c157b72ac..96ad01e3b0a7f4bac02fa77e3861bf317d6489eb 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 @@ -37,6 +37,8 @@ int GRD_UpSertDoc(GRD_DB *db, const char *collectionName, const char *filter, co int GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, unsigned int flags); +int GRD_Flush(GRD_DB *db, unsigned int flags); + #ifdef __cplusplus } #endif diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h index a4919b67f803d95adf6ad4b521e9011ea0d2d532..24065eda2a3897f1cd314b0cfd012499a1749403 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h @@ -18,9 +18,23 @@ #include #include +#include "json_common.h" +class JsonCommon; namespace DocumentDB { +class CheckCommon +{ +public: + CheckCommon() = default; + ~CheckCommon(); + + static bool CheckCollectionName(const std::string &collectionName); + static bool CheckFilter(const std::string &filter); + static bool CheckIdFormat(const std::string &data); + static bool CheckDocument(const std::string &document); +}; using Key = std::vector; using Value = std::vector; + } // DocumentDB #endif // DOC_COMMON_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h new file mode 100644 index 0000000000000000000000000000000000000000..22e19391f544598bf80b62a6ef8f928439a387db --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h @@ -0,0 +1,37 @@ +/* +* 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 JSON_COMMON_H +#define JSON_COMMON_H + +#include +#include +#include "cjson_object.h" + +namespace DocumentDB { +class JsonCommon +{ +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); +}; + +} // 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 new file mode 100644 index 0000000000000000000000000000000000000000..fa39bd34db4c4eaa92ef70783d4663e5b952ea9f --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/doc_common.cpp @@ -0,0 +1,67 @@ +/* + * 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 "doc_common.h" +#include "doc_errno.h" +#include "log_print.h" +#include "securec.h" + +namespace DocumentDB { +bool CheckCommon::CheckCollectionName(const std::string &collectionName) +{ + if (collectionName.empty()) { + return false; + } + if (collectionName.length() > 512) { + return false; + } + if (collectionName.compare(0, 4, "GRD", 0, 4) == 0 || collectionName.compare(0, 7, "GM_SYS_", 0, 7) == 0) { + return false; + } + return true; +} + +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; + } + return true; +} + +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) { + return false; + } + return true; +} + +bool CheckCommon::CheckDocument(const std::string &document) +{ + 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 new file mode 100644 index 0000000000000000000000000000000000000000..49bccc16501156ef3f1b3724b2e1e767ae617485 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp @@ -0,0 +1,70 @@ +/* + * 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 "json_common.h" +#include "doc_errno.h" +#include "log_print.h" +#include "securec.h" + +namespace DocumentDB { +ResultValue* JsonCommon::GetValue(CjsonObject *root, std::vector path) +{ + return nullptr; +} + +int JsonCommon::GetIdValue(CjsonObject *root, std::vector &id) +{ + auto &node = root; + if (root == nullptr) { + return E_OK; + } + 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); + } + node = node->GetNext(); + } + return E_OK; +} + +bool JsonCommon::CheckIsJson(const std::string &data) { + CjsonObject cjsonObj; + if (cjsonObj.Parse(data) == E_ERROR) { + return false; + } + return true; +} + +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--; + } + } + return deep; +} + +} // namespace DocumentDB \ 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 dc5ccf4653a0664803874c5a5e283e0d33cbd2ea..6610fae93fbdf3bf044bebff6a646afaae4fd6ef 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 @@ -48,3 +48,19 @@ 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) { + 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); +} \ 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 93925e5529d254073cda0ba6cb04943bda29316c..bf80438361ababea2ba71a75d768faac089523b0 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 @@ -33,6 +33,8 @@ public: 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); private: KvStoreExecutor *executor_ = nullptr; 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 3e2562b110a1a89278264b86ad456d5b73691b1b..67a12ccb1d293c6178d697c44905a2b3ca812a8e 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 @@ -41,7 +41,10 @@ int Collection::GetDocument(const Key &key, Value &document) const int Collection::DeleteDocument(const Key &key) { - return E_OK; + if (executor_ == nullptr) { + return -E_INVALID_ARGS; + } + return executor_->DeleteData(name_, key); } int Collection::UpsertDocument(const Key &key, Value &document) 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 6fd1f679c1bd16c0664b34416f7390a83bf44b70..b8dcabacd608d1b34d682d3e226a592b95ed6e7d 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,9 @@ #include "document_store.h" #include "doc_errno.h" +#include "json_common.h" +#include "cjson_object.h" +#include "log_print.h" namespace DocumentDB { DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) @@ -54,4 +57,38 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri return coll.PutDocument(key, value); } -} // namespace DocumentDB \ No newline at end of file + +int DocumentStore::InsertDocument(const std::string &collection, const std::string &document, int flag) +{ + auto coll = Collection(collection, executor_); + CjsonObject documentObj; + documentObj.Parse(document); + std::vector id; + if (JsonCommon::GetIdValue(documentObj.GetChild(), id) != E_OK) { + return E_ERROR; + } + Key key(id[0].begin(), id[0].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) +{ + auto coll = Collection(collection, executor_); + CjsonObject filterObj; + filterObj.Parse(filter); + std::vector id; + if (JsonCommon::GetIdValue(filterObj.GetChild(), id) != E_OK) { + return E_ERROR; + } + if (id.size() > 1) { + Key key; + return coll.DeleteDocument(key); + } + if (id.size() == 1) { + Key key(id[0].begin(), id[0].end()); + return coll.DeleteDocument(key); + } + 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 new file mode 100644 index 0000000000000000000000000000000000000000..f0ebd8fdef639a5ed7c6f73d7ffc7e14e45283a6 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h @@ -0,0 +1,56 @@ +/* +* 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 JSON_OBJECT_H +#define JSON_OBJECT_H + +#include + +namespace DocumentDB { + +class JsonObject; +class ResultValue { +public: + enum class ValueType { + VALUE_FALSE, + VALUE_TRUE, + VALUE_NULL, + VALUE_NUMBER, + VALUE_STRING, + VALUE_ARRAY, + VALUE_OBJECT + }; + int value_number; + std::string value_string; + ValueType value_type = ValueType::VALUE_NULL; + JsonObject *value_Object = nullptr; +}; + +class JsonObject { +public: + JsonObject () = default; + virtual ~JsonObject () = default; + + virtual int Parse(const std::string &str) = 0; + virtual std::string Print() = 0; + virtual JsonObject* GetObjectItem(const std::string &field) = 0; + virtual JsonObject* GetArrayItem(const int index) = 0; + virtual ResultValue* GetItemValue() = 0; + virtual std::string GetItemFiled() = 0; + virtual int DeleteItemFromObject(const std::string &field) = 0; + virtual int Delete() = 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/include/kv_store_executor.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_executor.h index f1d21444e73602b0430e281412147037424a13c1..1891387049d969605baeaba16e656951f6f53776 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 @@ -28,6 +28,8 @@ public: virtual int GetData(const std::string &collName, const Key &key, Value &value) const = 0; + virtual int DeleteData(const std::string &collName, const Key &key) const = 0; + virtual int CreateCollection(const std::string &name, int flag) = 0; virtual int DropCollection(const std::string &name, int flag) = 0; }; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/json_operator.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/json_operator.h deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 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 new file mode 100644 index 0000000000000000000000000000000000000000..05eb385b1be8b0e66895ce89fff60845620863a6 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp @@ -0,0 +1,181 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "doc_errno.h" +#include "cjson_object.h" +#include "log_print.h" + +namespace DocumentDB { + +int CjsonObject::Parse(const std::string &str) +{ + cjson_ = cJSON_Parse(str.c_str()); + if (cjson_ == nullptr) { + return E_ERROR; + } + return E_OK; +} + +std::string CjsonObject::Print() +{ + if (cjson_ == nullptr) { + GLOGE("======================>NULL"); + return ""; + } + char *ret = cJSON_Print(cjson_); + std::string str = ret; + free(ret); + GLOGE("======================>%s", str.c_str()); + return str; +} + +CjsonObject* CjsonObject::GetObjectItem(const std::string &field) +{ + if (cjson_ == nullptr) { + return nullptr; + } + CjsonObject *item = new CjsonObject(); + auto cjson_item = cJSON_GetObjectItem(cjson_, field.c_str()); + if (cjson_item == nullptr) { + return item; + } + item->cjson_ = cjson_item; + return item; +} + +CjsonObject* CjsonObject::GetArrayItem(const int index) +{ + if (cjson_ == nullptr) { + return nullptr; + } + CjsonObject *item = new CjsonObject(); + auto cjson_item = cJSON_GetArrayItem(cjson_, index); + if (cjson_item == nullptr) { + return item; + } + item->cjson_ = cjson_item; + return item; +} + +CjsonObject* CjsonObject::GetNext() +{ + if (cjson_ == nullptr) { + return nullptr; + } + CjsonObject *next = new CjsonObject(); + if (cjson_->next == nullptr) { + return next; + } + next->cjson_ = cjson_->next; + return next; +} + +CjsonObject* CjsonObject::GetChild() +{ + if (cjson_ == nullptr) { + return nullptr; + } + CjsonObject *child = new CjsonObject(); + if (cjson_->child == nullptr) { + return child; + } + child->cjson_ = cjson_->child; + return child; +} + +int CjsonObject::DeleteItemFromObject(const std::string &field) +{ + if (field == "") { + return E_OK; + } + cJSON_DeleteItemFromObject(cjson_, field.c_str()); + return E_OK; +} + +ResultValue* CjsonObject::GetItemValue() +{ + if (cjson_ == nullptr) { + return nullptr; + } + ResultValue* value = new ResultValue(); + switch (cjson_->type) + { + case cJSON_False: + { + value->value_type = ResultValue::ValueType::VALUE_FALSE; + } + break; + case cJSON_True: + { + value->value_type = ResultValue::ValueType::VALUE_TRUE; + } + break; + case cJSON_NULL: + { + value->value_type = ResultValue::ValueType::VALUE_NULL; + } + break; + case cJSON_Number: + { + value->value_type = ResultValue::ValueType::VALUE_NUMBER; + value->value_number = cjson_->valuedouble; + } + break; + case cJSON_String: + { + value->value_type = ResultValue::ValueType::VALUE_STRING; + value->value_string = cjson_->valuestring; + } + break; + case cJSON_Array: + { + value->value_type = ResultValue::ValueType::VALUE_ARRAY; + value->value_Object = GetChild(); + } + break; + case cJSON_Object: + { + value->value_type = ResultValue::ValueType::VALUE_OBJECT; + value->value_Object = GetChild(); + } + break; + default: + break; + } + return value; +} + +std::string CjsonObject::GetItemFiled() +{ + if (cjson_ == nullptr) { + GLOGE("cjson is null"); + return ""; + } + if (cjson_->string == nullptr) { + GLOGE("cjson string is null"); + return ""; + } + return cjson_->string; +} + +int CjsonObject::Delete() +{ + if (cjson_ != nullptr) { + cJSON_Delete(cjson_); + } + 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 new file mode 100644 index 0000000000000000000000000000000000000000..dcda3cb22cd7a612405ab681368d702fcfd9b796 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h @@ -0,0 +1,44 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef CJSON_OBJECT_H +#define CJSON_OBJECT_H + +#include +#include "cJSON.h" +#include "json_object.h" + +namespace DocumentDB { +class CjsonObject : public JsonObject{ +public: + CjsonObject () = default; + virtual ~CjsonObject () = default; + + int Parse(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(); + int DeleteItemFromObject(const std::string &field) override; + ResultValue* GetItemValue() override; + std::string GetItemFiled() override; + int Delete() override; +private: + cJSON *cjson_; +}; +} // DocumentDB +#endif // CJSON_OBJECT_H + 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 9f091ca1b010f452d3e0b1ac622b44943c8d41f0..40c9c554a291690fbfb8cc3e1c15c0c453ca3ad3 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 @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "securec.h" #include "doc_errno.h" #include "log_print.h" #include "sqlite_utils.h" @@ -30,10 +31,11 @@ SqliteStoreExecutor::~SqliteStoreExecutor() int SqliteStoreExecutor::PutData(const std::string &collName, const Key &key, const Value &value) { + std::string key_value (key.begin(), key.end()); + GLOGD("[Put sqlite executor] collName = %s, key = %s", collName.c_str(), key_value.c_str()); 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); @@ -97,4 +99,22 @@ 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()); + GLOGD("[Delete sqlite executor] collName = %s, key = %s", collName.c_str(), key_value.c_str()); + sqlite3 *db = dbHandle_; + const std::string sql_delete = "DELETE FROM'" + collName + "' WHERE _id = ?;"; + 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/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 ab15f02d3a5afdc05bcb9147f8dedda27a5bb9ab..70f49a4b290da4eb039dd8a39d4ce82402f1165a 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 @@ -18,6 +18,8 @@ #include "kv_store_executor.h" #include "sqlite3.h" +#include "sqlite_utils.h" + namespace DocumentDB { class SqliteStoreExecutor : public KvStoreExecutor { @@ -27,6 +29,7 @@ public: 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 DeleteData(const std::string &collName, const Key &key) const override; int CreateCollection(const std::string &name, int flag) override; int DropCollection(const std::string &name, int flag) override; 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 0000000000000000000000000000000000000000..bfa3e62086ba248b26f9836479be17006b8e6277 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_delete_test.cpp @@ -0,0 +1,77 @@ +/* +* 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); +} + +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_insert_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_insert_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d734b740116ef56821d732112e723b892ebca0b --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/ documentdb_insert_test.cpp @@ -0,0 +1,70 @@ +/* +* 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 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); +} + +void DocumentInsertApiTest::SetUp(void) +{ +} + +void DocumentInsertApiTest::TearDown(void) +{ +} + + +/** + * @tc.name: OpenDBTest001 + * @tc.desc: Test Insert document db + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ + +HWTEST_F(DocumentInsertApiTest, InsertDBTest001, TestSize.Level1) +{ + const char *collectionName = "student"; + const char *document = "{\"a\":\"1\",\"b\":\"2\",\"c\":\"3\",\"d\":\"4\",\"e\":\"5\", \"_id\":\"12345\", \"_id\":\"678910\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, document, 0), GRD_OK); +} \ No newline at end of file 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 0000000000000000000000000000000000000000..419d87fdddf1a2a889e8d4c1e3191ac93c3cf9b0 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/json_test.cpp @@ -0,0 +1,70 @@ +/* +* 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_\"}}"; +} +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.Parse(::JSON_Test1); + 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); +}