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 beff4ff8e5be387f60af87607300ea67c58182ed..bf8c9e652e40277ae60f0d76dab7368929231912 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 @@ -189,40 +189,42 @@ bool JsonCommon::CheckProjectionField(JsonObject &jsonObj) int JsonCommon::ParseNode(JsonObject &node, std::vector singlePath, std::vector> &resultPath, bool isFirstFloor) { - std::vector fatherPath; - if (isFirstFloor) { - std::string tempParseName; - std::vector allFiledsName; - std::string priFieldName = node.GetItemFiled(); - for (size_t j = 0; j < priFieldName.size(); j++) { - if (priFieldName[j] != '.') { - tempParseName = tempParseName + priFieldName[j]; - } - if (priFieldName[j] == '.' || j == priFieldName.size() - 1) { - if (j > 0 && priFieldName[j] == '.' && priFieldName[j - 1] == '.') { - return -E_INVALID_ARGS; + while (!node.IsNull()) { + int insertCount = 0; + if (isFirstFloor) { + std::string tempParseName; + std::vector allFiledsName; + std::string priFieldName = node.GetItemFiled(); + for (size_t j = 0; j < priFieldName.size(); j++) { + if (priFieldName[j] != '.') { + tempParseName = tempParseName + priFieldName[j]; + } + if (priFieldName[j] == '.' || j == priFieldName.size() - 1) { + if (j > 0 && priFieldName[j] == '.' && priFieldName[j - 1] == '.') { + return -E_INVALID_ARGS; + } + allFiledsName.emplace_back(tempParseName); + insertCount++; + tempParseName.clear(); } - allFiledsName.emplace_back(tempParseName); - tempParseName.clear(); } + singlePath.insert(singlePath.end(), allFiledsName.begin(), allFiledsName.end()); + } else { + std::vector allFiledsName; + allFiledsName.emplace_back(node.GetItemFiled()); + insertCount++; + singlePath.insert(singlePath.end(), allFiledsName.begin(), allFiledsName.end()); + } + if (!node.GetChild().IsNull() && node.GetChild().GetItemFiled() != "") { + auto nodeNew = node.GetChild(); + ParseNode(nodeNew, singlePath, resultPath, false); + } else { + resultPath.emplace_back(singlePath); } - fatherPath = singlePath; - singlePath.insert(singlePath.end(), allFiledsName.begin(), allFiledsName.end()); - } else { - std::vector allFiledsName; - allFiledsName.emplace_back(node.GetItemFiled()); - fatherPath = singlePath; - singlePath.insert(singlePath.end(), allFiledsName.begin(), allFiledsName.end()); - } - if (!node.GetChild().IsNull() && node.GetChild().GetItemFiled() != "") { - auto nodeNew = node.GetChild(); - ParseNode(nodeNew, singlePath, resultPath, false); - } else { - resultPath.emplace_back(singlePath); - } - if (!node.GetNext().IsNull()) { - auto nodeNew = node.GetNext(); - ParseNode(nodeNew, fatherPath, resultPath, isFirstFloor); + for (int i = 0; i < insertCount; i++) { + singlePath.pop_back(); + } + node = node.GetNext(); } return E_OK; } @@ -526,8 +528,7 @@ int JsonCommon::Append(const JsonObject &src, const JsonObject &add, bool isRepl if (!isCollapse) { bool ret = JsonValueReplace(src, fatherPath, father, item, externErrCode); if (!ret) { - GLOGE("replace failed"); - return false; + return false; // replace faild } isAddedFlag = true; return false; // Different node types, overwrite directly, skip child node diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp index 8e9a250858e0fbd00ce4577b92b80cf1df32fb8d..f87342c7977219d0c299f09fa7ad774b4fbe9c53 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp @@ -272,41 +272,18 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest006, TestSize.Level0) HWTEST_F(DocumentDBDataTest, UpdateDataTest007, TestSize.Level0) { - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"_id":"1234", "field1":{"c_field":{"cc_field":{"ccc_field":1}}}, "field2" : 2})""; - - EXPECT_EQ(GRD_InsertDoc(g_db, g_coll, document.c_str(), 0), GRD_OK); - - std::string updata = R""({"field1":1, "FIELD1":[1, true, 1.23456789, "hello world!", null]})""; - EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), updata.c_str(), 0), 1); - - GRD_ResultSet *resultSet = nullptr; - const char *projection = "{}"; - Query query = { filter.c_str(), projection }; - EXPECT_EQ(GRD_FindDoc(g_db, g_coll, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = NULL; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - string valueStr = value; - string repectStr = R""({"_id":"1234","field1":1,"field2":2,"FIELD1":[1,true,1.23456789,"hello world!",null]})""; - EXPECT_EQ((valueStr == repectStr), 1); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -HWTEST_F(DocumentDBDataTest, UpdateDataTest008, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string updata = R""({"field1":1, "FIELD1":[1, true, 1.23456789, "hello world!", null]})""; - EXPECT_EQ(GRD_UpdateDoc(g_db, "grd_aa", filter.c_str(), updata.c_str(), 0), GRD_INVALID_FORMAT); - EXPECT_EQ(GRD_UpdateDoc(g_db, "gRd_aa", filter.c_str(), updata.c_str(), 0), GRD_INVALID_FORMAT); -} + int result = GRD_OK; + const char *doc = R"({"_id":"007", "field1":{"c_field":{"cc_field":{"ccc_field":1}}}, "field2":2})"; + result = GRD_InsertDoc(g_db,g_coll, doc, 0); + cJSON *updata = cJSON_CreateObject(); + for (int i = 0; i <= 40000; i++) { + string temp = "f" + string(5 - std::to_string(i).size(), '0') + std::to_string(i); + cJSON_AddStringToObject(updata, temp.c_str(), "a"); + } + char *updateStr = cJSON_PrintUnformatted(updata); + result = GRD_UpdateDoc(g_db, g_coll, R""({"_id":"007"})"", updateStr, 0); + EXPECT_EQ(result, 1); + cJSON_Delete(updata); + cJSON_free; -HWTEST_F(DocumentDBDataTest, UpdateDataTest009, TestSize.Level0) -{ - const char *updateStr = R""({"field2":{"c_field":{"cc_field":{"ccc_field":{"ccc_field":[1,false,1.234e2,["hello world!"]]}}}}})""; - int result = GRD_UpdateDoc(g_db, g_coll, "{\"field\" : 2}", updateStr, 0); - int result2 = GRD_UpsertDoc(g_db, g_coll, "{\"field\" : 2}", updateStr, 0); - EXPECT_EQ(result, GRD_INVALID_ARGS); - EXPECT_EQ(result2, 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 157770f8fd6f930087a309ab9a9c53346e4be675..5a0c3626969b6db6a3913f1fd1f0918b3806eee2 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 @@ -621,4 +621,18 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest023, TestSize.Leve EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj2, filterObj1, errCode), false); } +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest023, TestSize.Level0) +{ + std::string document = "{\"name\": 1, \"personInfo.school\": 1, \"personInfo.age\": 1}"; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + auto path = JsonCommon::ParsePath(srcObj, errCode); + for (auto singlePath : path) { + for (auto filedName : singlePath) { + GLOGE("filedName is =========>%s", filedName.c_str()); + } + GLOGE("///////////////////////////"); + } +} } \ No newline at end of file