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 a2aa4b8d0b561b148787a62fe0e745170bfafe6f..beff4ff8e5be387f60af87607300ea67c58182ed 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 @@ -583,31 +583,44 @@ bool JsonCommon::JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, { int errCode; JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); + if (errCode != E_OK) { + GLOGE("fine item falied"); + return errCode; + } + if (srcItem == item) { + isMatchFlag = true; + isAlreadyMatched = 1; + return false; + } JsonFieldPath granpaPath = itemPath; std::string lastFiledName = granpaPath.back(); granpaPath.pop_back(); JsonObject granpaItem = src.FindItemPowerMode(granpaPath, errCode); + if (errCode != E_OK) { + GLOGE("fine item falied"); + return errCode; + } if (granpaItem.GetType() == JsonObject::Type::JSON_ARRAY && isCollapse) { JsonObject fatherItem = granpaItem.GetChild(); while (!fatherItem.IsNull()) { - int isEqual = true; - int compareRet = (fatherItem.GetObjectItem(lastFiledName, errCode).Print() == item.Print()); - if (errCode == E_OK) { - isEqual = compareRet; - } - if (isEqual) { - GLOGI("Filter value is equal with src"); - isMatchFlag = isEqual; + if ((fatherItem.GetObjectItem(lastFiledName, errCode) == item)) { + if (errCode != E_OK) { + GLOGE("get item falied"); + return errCode; + } + 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()); - if (!isEqual) { - GLOGI("Filter value is No equal with src"); + if (!isEqual) { // Filter value is No equal with src isMatchFlag = isEqual; } isAlreadyMatched = isMatchFlag; @@ -616,22 +629,19 @@ bool JsonCommon::JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, if (srcItem.GetType() == JsonObject::Type::JSON_LEAF && item.GetType() == JsonObject::Type::JSON_LEAF && !isAlreadyMatched) { bool isEqual = isValueEqual(srcItem.GetItemValue(), item.GetItemValue()); - if (!isEqual) { - GLOGI("Filter value is No equal with src"); + if (!isEqual) { // Filter value is No equal with src isMatchFlag = isEqual; } isAlreadyMatched = isMatchFlag; return false; // Both leaf node, no need iterate } else if (srcItem.GetType() != item.GetType()) { - if (srcItem.GetType() == JsonObject::Type::JSON_ARRAY) { - GLOGI("srcItem Type is ARRAY, item Type is not ARRAY"); + if (srcItem.GetType() == JsonObject::Type::JSON_ARRAY) { // srcItem Type is ARRAY, item Type is not ARRAY bool isEqual = IsArrayMatch(srcItem, item, isAlreadyMatched); if (!isEqual) { isMatchFlag = isEqual; } return true; } - GLOGI("valueType is different"); isMatchFlag = false; return false; // Different node types, overwrite directly, skip child node } @@ -655,10 +665,14 @@ bool JsonCommon::IsJsonNodeMatch(const JsonObject &src, const JsonObject &target } else { JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); + if (errCode != E_OK) { + GLOGE("fine item falied"); + return errCode; + } if (srcItem.GetType() == JsonObject::Type::JSON_ARRAY) { return JsonEqualJudge(itemPath, src, item, isAlreadyMatched, isCollapse, isMatchFlag); } - if (srcItem.Print() == item.Print()) { + if (srcItem == item) { isMatchFlag = true; isAlreadyMatched = true; return false; @@ -667,26 +681,23 @@ bool JsonCommon::IsJsonNodeMatch(const JsonObject &src, const JsonObject &target return false; } } else { - if (isCollapse) { - GLOGE("Match failed, path not exist."); - isMatchFlag = false; - return false; - } - GLOGI("Not match anything"); - if (isAlreadyMatched == 0) { - isMatchFlag = false; - } std::vector ItemLeafValue = GetLeafValue(item); int isNULLFlag = true; for (auto ValueItem : ItemLeafValue) { - if (ValueItem.GetValueType() != ValueObject::ValueType::VALUE_NULL) { - GLOGI("leaf value is not null"); + if (ValueItem.GetValueType() != ValueObject::ValueType::VALUE_NULL) { // leaf value is not null isNULLFlag = false; - } else { - GLOGI("filter leaf is null, Src leaf is dont exist"); + } else { // filter leaf is null, Src leaf is dont exist isMatchFlag = true; + return false; } } + if (isCollapse) { // Match failed, path not exist + isMatchFlag = false; + return false; + } + if (isAlreadyMatched == 0) { //Not match anything + isMatchFlag = false; + } return false; // Source path not exist, if leaf value is null, isMatchFlag become true, else it will become false. } }); 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 f205698408e267f17f8c5eac22a09131f2dcf9e4..f4ebbdd30742f5b8d4e85a84e20864d6b684296f 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 @@ -61,6 +61,7 @@ using JsonFieldPath = std::vector; class JsonObject { public: static JsonObject Parse(const std::string &jsonStr, int &errCode, bool caseSensitive = false); + bool operator==(const JsonObject& other) const; // If the two nodes exist with a different fieldName, then return 0. ~JsonObject(); std::string Print() const; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp index fe6106e2ad1bad69309526b2b9d34adea0e932be..8667b9da85c8f80c421cc3e07c23d2fbfcf26e2d 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/json_object.cpp @@ -123,6 +123,11 @@ JsonObject::~JsonObject() } } +bool JsonObject::operator==(const JsonObject& other) const +{ + return (cJSON_Compare(this->cjson_, other.cjson_, 0) != 0); +} + bool JsonObject::IsNull() const { if (cjson_ == nullptr) { 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 index e8772016cb61cc5b48b5ed8f680f6776742822b1..4e3e8000e12b1904be0bf83a5eb83b07709c57d7 100644 --- 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 @@ -1520,63 +1520,3 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest057, TestSize.Level1) // EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } -/** - * @tc.name: DocumentFindApiTest058 - * @tc.desc: Test findDoc with no _id. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest058, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter with _id and get the record according to filter condition. - * @tc.expected: step1. Succeed to get the record, the matching record is g_document6. - */ - // const char *filter = "{\"personInfo.0.school\" : \"B\", \"$personInfo.0.age\" : 15}"; - // GRD_ResultSet *resultSet = nullptr; - // Query query = {filter, "{}"}; - // EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - // EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - // char *value = NULL; - // EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - // EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - /** - * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. - */ - // EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - // EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - // CompareValue(value, g_document2); - // EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - - // EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - // EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - // CompareValue(value, g_document13); - // EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - - // EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - // EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - // CompareValue(value, g_document13); - //EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentFindApiTest059 - * @tc.desc: Test findDoc with invalid field - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentFindApiTest, DocumentFindApiTest059, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter with _id and get the record according to filter condition. - * @tc.expected: step1. Succeed to get the record, the matching record is g_document6. - */ - const char *filter = "{\"123a1\":123}"; - GRD_ResultSet *resultSet = nullptr; - const char *projection = "{}"; - Query query = { filter, projection }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp index 9c125f7cb211a35653a459a5fcc4208b600f8738..157770f8fd6f930087a309ab9a9c53346e4be675 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp @@ -592,12 +592,33 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest022, TestSize.Leve "k12" : "v12"})"; string document2 = R"({"_id" : "002", "key1" : {"key2" : {"key3" : {"key4" : 123, "k42" : "v42"}, "k32" : "v32"}}, "k12" : "v12"})"; const char *filter = R"({"key1" : {"key2" : {"key3" : {"key4" : 123, "k42" : "v42"}, "k32" : "v32"}}})"; + const char *filter2 = R"({"key1" : {"k22" : "v22", "key2" : {"key3" : {"key4" : 123, "k42" : "v42"}, "k32" : "v32"}}})"; int errCode = E_OK; JsonObject srcObj1 = JsonObject::Parse(document, errCode); JsonObject srcObj2 = JsonObject::Parse(document2, errCode); EXPECT_EQ(errCode, E_OK); - JsonObject filterObj = JsonObject::Parse(filter, errCode); - EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj1, filterObj, errCode), false); - EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj2, filterObj, errCode), true); + JsonObject filterObj1 = JsonObject::Parse(filter, errCode); + JsonObject filterObj2 = JsonObject::Parse(filter2, errCode); + EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj1, filterObj1, errCode), false); + EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj2, filterObj1, errCode), true); + + EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj1, filterObj2, errCode), true); + EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj2, filterObj2, errCode), false); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest023, TestSize.Level0) +{ + string document = R"({"_id" : "001", "key1" : {"key2" : {"key3" : {"key4" : 123, "k42" : "v42"}, "k32" : "v32"}, "k22" : "v22"}, + "k12" : "v12", "key2" : {"key3" : {"key4" : 123, "k42" : "v42"}, "k32" : "v32"}})"; + string document2 = R"({"_id" : "002", "key1" : {"key2" : {"key3" : {"key4" : 123}, "k32" : "v32"}, "k22" : "v22"}, "k12" : "v12"})"; + const char *filter = R"({"key2" : {"key3" : {"key4" : 123, "k42" : "v42"}, "k32" : "v32"}})"; + int errCode = E_OK; + JsonObject srcObj1 = JsonObject::Parse(document, errCode); + JsonObject srcObj2 = JsonObject::Parse(document2, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj1 = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj1, filterObj1, errCode), true); + EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj2, filterObj1, errCode), false); } + } \ No newline at end of file