From 90f213499a844322dba21a03d5740250a5f92237 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Tue, 16 May 2023 14:37:34 +0800 Subject: [PATCH] Fix big method Signed-off-by: lianhuix --- .../src/common/include/json_common.h | 5 +- .../gaussdb_rd/src/common/src/json_common.cpp | 143 ++++++++++-------- .../src/interface/include/result_set.h | 2 + .../src/interface/src/result_set.cpp | 90 ++++++----- .../unittest/api/documentdb_delete_test.cpp | 2 +- 5 files changed, 135 insertions(+), 107 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/json_common.h index 119d76da..4e8cd1ae 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/json_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/json_common.h @@ -39,12 +39,15 @@ public: static bool IsJsonNodeMatch(const JsonObject &src, const JsonObject &target, int &errCode); private: - static bool JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, const JsonObject &item, + static bool JsonEqualJudge(const JsonFieldPath &itemPath, const JsonObject &src, const JsonObject &item, int &isAlreadyMatched, bool &isCollapse, int &isMatchFlag); static bool CheckNode(JsonObject &Node); static bool CheckProjectionNode(JsonObject &Node, bool isFirstLevel, int &errCode); static void CheckLeafNode(const JsonObject &Node, std::vector &leafValue); static bool IsArrayMatch(const JsonObject &src, const JsonObject &target, int &isAlreadyMatched); + + static bool IsObjectItemMatch(const JsonObject &srcItem, const JsonObject &item, int &isAlreadyMatched, + bool &isCollapse, int &isMatchFlag); }; } // namespace DocumentDB #endif // JSON_COMMON_H \ No newline at end of file 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 0706c64e..e63cfbff 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 @@ -498,6 +498,47 @@ bool JsonNodeReplace(const JsonObject &src, const JsonFieldPath &itemPath, const } return true; } + +bool JsonNodeAppend(const JsonObject &src, const JsonFieldPath &path, const JsonObject &father, const JsonObject &item, + bool &isAddedFlag, int &externErrCode) +{ + bool isCollapse = false; + JsonFieldPath itemPath = ExpendPathForField(path, isCollapse); + JsonFieldPath fatherPath = itemPath; + fatherPath.pop_back(); + + int errCode = E_OK; + JsonObject srcFatherItem = src.FindItem(fatherPath, errCode); + std::string lastFieldName = itemPath.back(); + if (errCode != E_OK) { + isAddedFlag = true; + AddSpliteField(src, item, itemPath, externErrCode); + return false; + } + if (isCollapse && (!IsNumber(lastFieldName) || srcFatherItem.GetType() == JsonObject::Type::JSON_ARRAY)) { + errCode = srcFatherItem.AddItemToObject(lastFieldName, item); + if (errCode != E_OK) { + externErrCode = (externErrCode == E_OK ? errCode : externErrCode); + GLOGE("Add item to object failed. %d", errCode); + return false; + } + isAddedFlag = true; + return false; + } + if (!isCollapse) { + bool ret = JsonValueReplace(src, fatherPath, father, item, externErrCode); + if (!ret) { + return false; // replace failed + } + isAddedFlag = true; + return false; // Different node types, overwrite directly, skip child node + } + if (!isAddedFlag) { + GLOGE("Add nothing because data conflict"); + externErrCode = -E_DATA_CONFLICT; + } + return false; // Source path not exist, overwrite directly, skip child node +} } // namespace int JsonCommon::Append(const JsonObject &src, const JsonObject &add, bool isReplace) @@ -509,10 +550,8 @@ int JsonCommon::Append(const JsonObject &src, const JsonObject &add, bool isRepl const JsonObject &item) { bool isCollapse = false; JsonFieldPath itemPath = ExpendPathForField(path, isCollapse); - JsonFieldPath fatherPath = itemPath; - fatherPath.pop_back(); - int errCode = E_OK; if (src.IsFieldExists(itemPath)) { + int errCode = E_OK; JsonObject srcItem = src.FindItem(itemPath, errCode); if (errCode != E_OK) { externErrCode = (externErrCode == E_OK ? errCode : externErrCode); @@ -531,38 +570,7 @@ int JsonCommon::Append(const JsonObject &src, const JsonObject &add, bool isRepl externErrCode = -E_NO_DATA; return false; } - JsonObject srcFatherItem = src.FindItem(fatherPath, errCode); - std::string lastFieldName = itemPath.back(); - if (errCode != E_OK) { - isAddedFlag = true; - AddSpliteField(src, item, itemPath, externErrCode); - return false; - } else { - if (isCollapse && - (!IsNumber(itemPath.back()) || srcFatherItem.GetType() == JsonObject::Type::JSON_ARRAY)) { - errCode = srcFatherItem.AddItemToObject(itemPath.back(), item); - if (errCode != E_OK) { - externErrCode = (externErrCode == E_OK ? errCode : externErrCode); - GLOGE("Add item to object failed. %d", errCode); - return false; - } - isAddedFlag = true; - return false; - } - if (!isCollapse) { - bool ret = JsonValueReplace(src, fatherPath, father, item, externErrCode); - if (!ret) { - return false; // replace failed - } - isAddedFlag = true; - return false; // Different node types, overwrite directly, skip child node - } - } - if (!isAddedFlag) { - GLOGE("Add nothing because data conflict"); - externErrCode = -E_DATA_CONFLICT; - } - return false; // Source path not exist, overwrite directly, skip child node + return JsonNodeAppend(src, path, father, item, isAddedFlag, externErrCode); } }); return externErrCode; @@ -603,35 +611,9 @@ bool JsonCommon::IsArrayMatch(const JsonObject &src, const JsonObject &target, i return isMatch; } -bool JsonCommon::JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, const JsonObject &item, - int &isAlreadyMatched, bool &isCollapse, int &isMatchFlag) +bool JsonCommon::IsObjectItemMatch(const JsonObject &srcItem, const JsonObject &item, int &isAlreadyMatched, + bool &isCollapse, int &isMatchFlag) { - int errCode = E_OK; - // 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; - return false; - } - JsonFieldPath granpaPath = itemPath; - std::string lastFieldName = granpaPath.back(); - granpaPath.pop_back(); - JsonObject granpaItem = src.FindItemPowerMode(granpaPath, errCode); - 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 - isMatchFlag = true; - isAlreadyMatched = 1; - break; - } - isMatchFlag = false; - fatherItem = fatherItem.GetNext(); - } - return false; - } if (srcItem.GetType() == JsonObject::Type::JSON_ARRAY && item.GetType() == JsonObject::Type::JSON_ARRAY && !isAlreadyMatched) { bool isEqual = (srcItem.Print() == item.Print()); @@ -648,7 +630,7 @@ bool JsonCommon::JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, isMatchFlag = isEqual; } isAlreadyMatched = isMatchFlag; - return false; // Both leaf node, no need iterate + return false; // Both leaf node, no need iterate } else if (srcItem.GetType() != item.GetType()) { if (srcItem.GetType() == JsonObject::Type::JSON_ARRAY) { // srcItem Type is ARRAY, item Type is not ARRAY bool isEqual = IsArrayMatch(srcItem, item, isAlreadyMatched); @@ -660,7 +642,38 @@ bool JsonCommon::JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, isMatchFlag = false; return false; // Different node types, overwrite directly, skip child node } - return true; // Both array or object + return true; // Both array or object +} + +bool JsonCommon::JsonEqualJudge(const JsonFieldPath &itemPath, const JsonObject &src, const JsonObject &item, + int &isAlreadyMatched, bool &isCollapse, int &isMatchFlag) +{ + int errCode; + JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); + if (errCode != -E_JSON_PATH_NOT_EXISTS && srcItem == item) { + isMatchFlag = true; + isAlreadyMatched = 1; + return false; + } + JsonFieldPath granpaPath = itemPath; + 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) { + JsonObject fatherItem = granpaItem.GetChild(); + while (!fatherItem.IsNull()) { + if ((fatherItem.GetObjectItem(lastFieldName, errCode) == item)) { // this errCode is always E_OK + isMatchFlag = true; + isAlreadyMatched = 1; + break; + } + isMatchFlag = false; + fatherItem = fatherItem.GetNext(); + } + return false; + } + + return IsObjectItemMatch(srcItem, item, isAlreadyMatched, isCollapse, isMatchFlag); } bool JsonCommon::IsJsonNodeMatch(const JsonObject &src, const JsonObject &target, int &errCode) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/result_set.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/result_set.h index 2cd7bbd8..ea1bcf42 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/result_set.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/result_set.h @@ -45,6 +45,8 @@ private: int CutJsonBranch(std::string &jsonData); int CheckCutNode(JsonObject *node, std::vector singleCutPath, std::vector> &allCutPath); + int GetNextWithField(); + DocumentStore *store_ = nullptr; std::string collectionName_; ValueObject key_; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/result_set.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/result_set.cpp index 8e37e106..39818073 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/result_set.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/result_set.cpp @@ -56,6 +56,53 @@ int ResultSet::Init(DocumentStore *store, const std::string collectionName, cons return E_OK; } +int ResultSet::GetNextWithField() +{ + int errCode = E_OK; + if (isOnlyId_) { + JsonObject filterObj = JsonObject::Parse(filter_, errCode, true, true); + if (errCode != E_OK) { + GLOGE("filter Parsed failed"); + return errCode; + } + JsonObject filterObjChild = filterObj.GetChild(); + ValueObject idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID); + std::string idKey = idValue.GetStringValue(); + if (idKey.empty()) { + return -E_NO_DATA; + } + Key key(idKey.begin(), idKey.end()); + Value document; + Collection coll = store_->GetCollection(collectionName_); + errCode = coll.GetDocument(key, document); + if (errCode == -E_NOT_FOUND) { + return -E_NO_DATA; + } + std::string jsonData(document.begin(), document.end()); + CutJsonBranch(jsonData); + std::vector> values; + values.emplace_back(std::make_pair(idKey, jsonData)); + matchDatas_ = values; + } else { + Collection coll = store_->GetCollection(collectionName_); + std::vector> values; + JsonObject filterObj = JsonObject::Parse(filter_, errCode, true, true); + if (errCode != E_OK) { + GLOGE("filter Parsed failed"); + return errCode; + } + errCode = coll.GetMatchedDocument(filterObj, values); + if (errCode == -E_NOT_FOUND) { + return -E_NO_DATA; + } + for (size_t i = 0; i < values.size(); i++) { + CutJsonBranch(values[i].second); + } + matchDatas_ = values; + } + return E_OK; +} + int ResultSet::GetNextInner(bool isNeedCheckTable) { int errCode = E_OK; @@ -73,46 +120,9 @@ int ResultSet::GetNextInner(bool isNeedCheckTable) } } if (!ifField_ && index_ == 0) { - if (isOnlyId_) { - JsonObject filterObj = JsonObject::Parse(filter_, errCode, true, true); - if (errCode != E_OK) { - GLOGE("filter Parsed failed"); - return errCode; - } - JsonObject filterObjChild = filterObj.GetChild(); - ValueObject idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID); - std::string idKey = idValue.GetStringValue(); - if (idKey.empty()) { - return -E_NO_DATA; - } - Key key(idKey.begin(), idKey.end()); - Value document; - Collection coll = store_->GetCollection(collectionName_); - errCode = coll.GetDocument(key, document); - if (errCode == -E_NOT_FOUND) { - return -E_NO_DATA; - } - std::string jsonData(document.begin(), document.end()); - CutJsonBranch(jsonData); - std::vector> values; - values.emplace_back(std::pair(idKey, jsonData)); - matchDatas_ = values; - } else { - Collection coll = store_->GetCollection(collectionName_); - std::vector> values; - JsonObject filterObj = JsonObject::Parse(filter_, errCode, true, true); - if (errCode != E_OK) { - GLOGE("filter Parsed failed"); - return errCode; - } - errCode = coll.GetMatchedDocument(filterObj, values); - if (errCode == -E_NOT_FOUND) { - return -E_NO_DATA; - } - for (size_t i = 0; i < values.size(); i++) { - CutJsonBranch(values[i].second); - } - matchDatas_ = values; + errCode = GetNextWithField(); + if (errCode != E_OK) { + return errCode; } } else if (index_ == 0) { Collection coll = store_->GetCollection(collectionName_); 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 4debaabf..15e5dce4 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 @@ -30,7 +30,7 @@ namespace { constexpr const char *COLLECTION_NAME = "student"; constexpr const char *NULL_JSON_STR = "{}"; constexpr int MAX_COLLECTION_LENS = 511; -std::string path = "./document.db"; +std::string g_path = "./document.db"; GRD_DB *g_db = nullptr; class DocumentDBDeleteTest : public testing::Test { -- Gitee