From fe731fd5a02ed1a015f63c91b657c4ef4640caa2 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 11 May 2023 20:57:15 +0800 Subject: [PATCH 1/7] fix white box Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/common/src/db_config.cpp | 12 ++++++------ .../gaussdb_rd/src/interface/src/collection.cpp | 7 +++---- .../gaussdb_rd/src/interface/src/document_store.cpp | 2 +- .../gaussdb_rd/src/interface/src/projection_tree.cpp | 2 +- .../test/unittest/api/documentdb_api_test.cpp | 2 +- .../test/unittest/api/documentdb_delete_test.cpp | 2 +- 6 files changed, 13 insertions(+), 14 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/db_config.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/db_config.cpp index e800910f..0058c687 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/db_config.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/db_config.cpp @@ -27,12 +27,12 @@ namespace DocumentDB { namespace { -const int MIN_REDO_BUFFER_SIZE = 256; -const int MAX_REDO_BUFFER_SIZE = 16384; -const int MIN_CONNECTION_NUM = 16; -const int MAX_CONNECTION_NUM = 1024; -const int MIN_BUFFER_POOL_SIZE = 1024; -const int MAX_BUFFER_POOL_SIZE = 4 * 1024 * 1024; +constexpr int MIN_REDO_BUFFER_SIZE = 256; +constexpr int MAX_REDO_BUFFER_SIZE = 16384; +constexpr int MIN_CONNECTION_NUM = 16; +constexpr int MAX_CONNECTION_NUM = 1024; +constexpr int MIN_BUFFER_POOL_SIZE = 1024; +constexpr int MAX_BUFFER_POOL_SIZE = 4 * 1024 * 1024; constexpr const char *DB_CONFIG_PAGESIZE = "pagesize"; constexpr const char *DB_CONFIG_REDO_FLUSH_BY_TRX = "redoflushbytrx"; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index e4c2d8fd..6f6e3f02 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -22,7 +22,7 @@ #include "log_print.h" namespace DocumentDB { -const int JSON_LENS_MAX = 1024 * 1024; +constexpr int JSON_LENS_MAX = 1024 * 1024; Collection::Collection(const std::string &name, KvStoreExecutor *executor) : executor_(executor) { @@ -143,9 +143,8 @@ int Collection::UpsertDocument(const std::string &id, const std::string &documen GLOGD("Append value failed. %d", errCode); return errCode; } - // kkk std::string valStr = originValue.Print(); - if (valStr.length() + 1 > JSON_LENS_MAX) { + if (valStr.length() >= JSON_LENS_MAX) { GLOGE("document's length is too long"); return -E_OVER_LIMIT; } @@ -200,7 +199,7 @@ int Collection::UpdateDocument(const std::string &id, const std::string &update, return errCode; } std::string valStr = originValue.Print(); - if (valStr.length() + 1 > JSON_LENS_MAX) { + if (valStr.length() >= JSON_LENS_MAX) { GLOGE("document's length is too long"); return -E_OVER_LIMIT; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index 39eea099..84048283 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -24,7 +24,7 @@ #include "result_set_common.h" namespace DocumentDB { -const int JSON_LENS_MAX = 1024 * 1024; +constexpr int JSON_LENS_MAX = 1024 * 1024; constexpr const char *KEY_ID = "_id"; DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) {} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/projection_tree.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/projection_tree.cpp index bd41e033..31d5acb3 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/projection_tree.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/projection_tree.cpp @@ -17,7 +17,7 @@ #include namespace DocumentDB { -const int JSON_DEEP_MAX = 4; +constexpr int JSON_DEEP_MAX = 4; int ProjectionTree::ParseTree(std::vector> &path) { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp index ab4c1dfa..b3c4e65f 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp @@ -190,7 +190,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigTest001, TestSize.Level0) { GRD_DB *db = nullptr; std::string path = "./document.db"; - const int MAX_JSON_LEN = 512 * 1024; + constexpr int MAX_JSON_LEN = 512 * 1024; std::string configStr = std::string(MAX_JSON_LEN, 'a'); int status = GRD_DBOpen(path.c_str(), configStr.c_str(), GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OVER_LIMIT); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_delete_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_delete_test.cpp index a41d3557..cc6fb2bd 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_delete_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_delete_test.cpp @@ -27,7 +27,7 @@ using namespace testing::ext; namespace { constexpr const char *COLLECTION_NAME = "student"; constexpr const char *NULL_JSON_STR = "{}"; -const int MAX_COLLECTION_LENS = 511; +constexpr int MAX_COLLECTION_LENS = 511; std::string path = "./document.db"; GRD_DB *g_db = nullptr; -- Gitee From acb08b83bc2b9d9f1d7ac83f5f06332cc216aad1 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 11 May 2023 21:02:15 +0800 Subject: [PATCH 2/7] fix Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/common/src/json_common.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp index 810e40a1..0c04de86 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp @@ -312,7 +312,7 @@ bool IsNumber(const std::string &str) bool AddSpliteField(const JsonObject &src, const JsonObject &item, const JsonFieldPath &itemPath, int &externErrCode) { - int errCode = 0; + int errCode = E_OK; JsonFieldPath abandonPath; JsonFieldPath hitPath = itemPath; while (!hitPath.empty()) { @@ -387,7 +387,7 @@ bool AddSpliteField(const JsonObject &src, const JsonObject &item, const JsonFie bool JsonValueReplace(const JsonObject &src, const JsonFieldPath &fatherPath, const JsonObject &father, const JsonObject &item, int &externErrCode) { - int errCode = 0; + int errCode = E_OK; JsonFieldPath granPaPath = fatherPath; if (!granPaPath.empty()) { granPaPath.pop_back(); @@ -428,7 +428,7 @@ bool JsonValueReplace(const JsonObject &src, const JsonFieldPath &fatherPath, co bool JsonNodeReplace(const JsonObject &src, const JsonFieldPath &itemPath, const JsonObject &father, const JsonObject &item, int &externErrCode) { - int errCode = 0; + int errCode = E_OK; JsonFieldPath fatherPath = itemPath; fatherPath.pop_back(); if (!fatherPath.empty()) { @@ -565,7 +565,7 @@ bool JsonCommon::IsArrayMatch(const JsonObject &src, const JsonObject &target, i JsonObject srcChild = src.GetChild(); JsonObject targetObj = target; bool isMatch = false; - int errCode = 0; + int errCode = E_OK; while (!srcChild.IsNull()) { if (srcChild.GetType() == JsonObject::Type::JSON_OBJECT && target.GetType() == JsonObject::Type::JSON_OBJECT && (IsJsonNodeMatch(srcChild, target, errCode))) { // The return value reflects the value of errCode @@ -581,7 +581,7 @@ bool JsonCommon::IsArrayMatch(const JsonObject &src, const JsonObject &target, i bool JsonCommon::JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, const JsonObject &item, int &isAlreadyMatched, bool &isCollapse, int &isMatchFlag) { - int errCode; + int errCode = E_OK; JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); if (errCode != -E_JSON_PATH_NOT_EXISTS && srcItem == item) { isMatchFlag = true; -- Gitee From 069ec13b604eda06bd5ec3f5639a9cf3cd62f20e Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 11 May 2023 21:21:36 +0800 Subject: [PATCH 3/7] fix Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/common/src/json_common.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp index 0c04de86..ac6f2921 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp @@ -318,7 +318,7 @@ bool AddSpliteField(const JsonObject &src, const JsonObject &item, const JsonFie while (!hitPath.empty()) { abandonPath.emplace_back(hitPath.back()); JsonObject srcFatherItem = src.FindItem(hitPath, errCode); - if (errCode != -E_JSON_PATH_NOT_EXISTS) { + if (errCode == E_OK) { break; } if (!srcFatherItem.IsNull()) { @@ -582,8 +582,8 @@ bool JsonCommon::JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, int &isAlreadyMatched, bool &isCollapse, int &isMatchFlag) { int errCode = E_OK; - JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); - if (errCode != -E_JSON_PATH_NOT_EXISTS && srcItem == item) { + JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); // This function has only two error codes, and the other error code is not an exception, but the specified node was not found + if (errCode == E_OK && srcItem == item) { isMatchFlag = true; isAlreadyMatched = 1; return false; @@ -592,7 +592,7 @@ bool JsonCommon::JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, std::string lastFieldName = granpaPath.back(); granpaPath.pop_back(); JsonObject granpaItem = src.FindItemPowerMode(granpaPath, errCode); - if (errCode != -E_JSON_PATH_NOT_EXISTS && granpaItem.GetType() == JsonObject::Type::JSON_ARRAY && isCollapse) { + if (errCode == E_OK && granpaItem.GetType() == JsonObject::Type::JSON_ARRAY && isCollapse) { JsonObject fatherItem = granpaItem.GetChild(); while (!fatherItem.IsNull()) { if ((fatherItem.GetObjectItem(lastFieldName, errCode) == item)) { // this errCode is always E_OK -- Gitee From 2f23fd4adf7efb9d7f45c364f0a88ca40d915084 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 11 May 2023 21:26:30 +0800 Subject: [PATCH 4/7] fix Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/oh_adapter/src/json_object.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp index 347c4c80..50090a14 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp @@ -233,10 +233,7 @@ int JsonObject::CheckSubObj(std::set &fieldSet, cJSON *subObj, int } if (parentType == cJSON_Array) { ret = CheckJsonRepeatField(subObj); - if (ret != E_OK) { - return ret; - } - return E_OK; + return ret; } if (fieldName.empty()) { return -E_INVALID_JSON_FORMAT; @@ -250,10 +247,7 @@ int JsonObject::CheckSubObj(std::set &fieldSet, cJSON *subObj, int return ret; } ret = CheckJsonRepeatField(subObj); - if (ret != E_OK) { - return ret; - } - return E_OK; + return ret; } std::string JsonObject::Print() const -- Gitee From 34f4c82f3d28884ee670feb6393f1556506b3766 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 11 May 2023 21:31:42 +0800 Subject: [PATCH 5/7] fix Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/oh_adapter/src/json_object.cpp | 6 +----- .../src/oh_adapter/src/sqlite_store_executor_impl.cpp | 5 ++--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp index 50090a14..c20660c7 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp @@ -508,11 +508,7 @@ std::string JsonObject::GetItemField() const std::string JsonObject::GetItemField(int &errCode) const { - if (cjson_ == nullptr) { - errCode = E_INVALID_ARGS; - return ""; - } - if (cjson_->string == nullptr) { + if (cjson_ == nullptr || cjson_->string == nullptr) { errCode = E_INVALID_ARGS; return ""; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp index eed4e2f5..e10335a6 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -279,7 +279,7 @@ int SqliteStoreExecutor::CreateCollection(const std::string &name, const std::st if (errCode != E_OK) { GLOGE("[sqlite executor] Set collection option failed. err=%d", errCode); } - return E_OK; + return errCode; } int SqliteStoreExecutor::DropCollection(const std::string &name, bool ignoreNonExists) @@ -305,9 +305,8 @@ int SqliteStoreExecutor::DropCollection(const std::string &name, bool ignoreNonE int errCode = SQLiteUtils::ExecSql(dbHandle_, sql); if (errCode != E_OK) { GLOGE("[sqlite executor] Drop collection failed. err=%d", errCode); - return errCode; } - return E_OK; + return errCode; } bool SqliteStoreExecutor::IsCollectionExists(const std::string &name, int &errCode) -- Gitee From f019e0f2d16fa9617b33ee5bef2bfefdd7f07947 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 11 May 2023 23:24:55 +0800 Subject: [PATCH 6/7] fix Signed-off-by: Jeremyzz --- .../src/interface/src/document_store.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index 84048283..67ac1559 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -195,7 +195,10 @@ int DocumentStore::UpdateDocument(const std::string &collection, const std::stri return errCode; } ResultSet resultSet; - InitResultSet(this, collection, filter, resultSet); + int ret = InitResultSet(this, collection, filter, resultSet); + if (ret != E_OK) { + return ret; + } std::lock_guard lock(dbMutex_); bool isCollectionExist = coll.IsCollectionExists(errCode); if (errCode != E_OK) { @@ -310,7 +313,10 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri return -E_INVALID_ARGS; } ResultSet resultSet; - InitResultSet(this, collection, filter, resultSet); + int ret = InitResultSet(this, collection, filter, resultSet); + if (ret != E_OK) { + return ret; + } errCode = resultSet.GetNext(); bool isfilterMatch = false; if (errCode == E_OK) { @@ -322,7 +328,7 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri std::string addedIdDocument = documentObj.Print(); Value ValueDocument; Key key(docId.begin(), docId.end()); - errCode = coll.GetDocument(key, ValueDocument); + errCode = coll.GetDocument(key, ValueDocument); // if errCode is not E_OK, it means same id document is already exist. if (errCode == E_OK && !(isfilterMatch)) { GLOGE("id exist but filter does not match, data conflict"); return -E_DATA_CONFLICT; @@ -431,7 +437,10 @@ int DocumentStore::DeleteDocument(const std::string &collection, const std::stri return coll.DeleteDocument(key); } ResultSet resultSet; - InitResultSet(this, collection, filter, resultSet); + int ret = InitResultSet(this, collection, filter, resultSet); + if (ret != E_OK) { + return ret; + } std::lock_guard lock(dbMutex_); bool isCollectionExist = coll.IsCollectionExists(errCode); if (errCode != E_OK) { -- Gitee From 26725549ccef618d3d7a499643b23540c9aa0400 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 16 May 2023 15:31:05 +0800 Subject: [PATCH 7/7] fix code check opinion Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/common/src/json_common.cpp | 4 +++- .../gaussdb_rd/src/oh_adapter/src/json_object.cpp | 12 +++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp index ac6f2921..772454bf 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp @@ -582,7 +582,9 @@ bool JsonCommon::JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, int &isAlreadyMatched, bool &isCollapse, int &isMatchFlag) { int errCode = E_OK; - JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); // This function has only two error codes, and the other error code is not an exception, but the specified node was not found + // This function has only two error codes, and the other error + // code is not an exception, but the specified node was not found + JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); if (errCode == E_OK && srcItem == item) { isMatchFlag = true; isAlreadyMatched = 1; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp index c20660c7..48ef7a1f 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp @@ -226,14 +226,12 @@ int JsonObject::CheckSubObj(std::set &fieldSet, cJSON *subObj, int if (subObj == nullptr) { return -E_INVALID_ARGS; } - int ret = E_OK; std::string fieldName; if (subObj->string != nullptr) { fieldName = subObj->string; } if (parentType == cJSON_Array) { - ret = CheckJsonRepeatField(subObj); - return ret; + return CheckJsonRepeatField(subObj); } if (fieldName.empty()) { return -E_INVALID_JSON_FORMAT; @@ -241,13 +239,9 @@ int JsonObject::CheckSubObj(std::set &fieldSet, cJSON *subObj, int if (fieldSet.find(fieldName) == fieldSet.end()) { fieldSet.insert(fieldName); } else { - ret = -E_INVALID_JSON_FORMAT; - } - if (ret != E_OK) { - return ret; + return -E_INVALID_JSON_FORMAT; } - ret = CheckJsonRepeatField(subObj); - return ret; + return CheckJsonRepeatField(subObj); } std::string JsonObject::Print() const -- Gitee