diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/grd_format_config.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/grd_format_config.h index 06e7bbeea54dae8f45f6899cac23bb9d0e5c92dc..9c5576697e75459ba868b99cd39c7823b34fc15f 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/grd_format_config.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/grd_format_config.h @@ -22,7 +22,7 @@ extern "C" { #endif // __cplusplus #define COLLECTION_LENS_MAX (512 * 1024) -#define JSON_LENS_MAX (512 * 1024) +#define JSON_LENS_MAX (1024 * 1024) #define JSON_DEEP_MAX (4) #define KEY_ID ("_id") 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 8e49164f8604e7b6a94666f4b63d0b745219fd9d..c43516bd3457282692fce45cf823903cf2879c4c 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 @@ -22,7 +22,7 @@ #include "log_print.h" namespace DocumentDB { -const int JSON_LENS_MAX = 1024 * 512; +const int JSON_LENS_MAX = 1024 * 1024; Collection::Collection(const std::string &name, KvStoreExecutor *executor) : executor_(executor) { @@ -131,8 +131,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(); + GLOGE("valStr become ========>%s", valStr.c_str()); if (valStr.length() + 1 > JSON_LENS_MAX) { GLOGE("document's length is too long"); return -E_OVER_LIMIT; @@ -185,10 +185,6 @@ int Collection::UpdateDocument(const std::string &id, const std::string &update, GLOGD("Append value failed. %d", errCode); return errCode; } - // errCode = CheckCommon::CheckDocument(originValue); - // if (errCode != E_OK) { - // return errCode; - // } std::string valStr = originValue.Print(); if (valStr.length() + 1 > JSON_LENS_MAX) { GLOGE("document's length is too long"); 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 b5837dc5de2384417ff35bfc1b5b58af78ba4c18..f22086eb0263eb73779995262c17879df0598f9a 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 @@ -24,7 +24,7 @@ namespace DocumentDB { const int COLLECTION_LENS_MAX = 512 * 1024; -const int JSON_LENS_MAX = 512 * 1024; +const int JSON_LENS_MAX = 1024 * 1024; const int JSON_DEEP_MAX = 4; constexpr const char *KEY_ID = "_id"; const bool caseSensitive = true; @@ -205,12 +205,20 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri } JsonObject documentObj = JsonObject::Parse(document, errCode, true); if (errCode != E_OK) { - GLOGE("document Parsed faild"); + GLOGE("update Parsed faild"); return errCode; } - errCode = CheckCommon::CheckDocument(documentObj); - if (errCode != E_OK) { - return errCode; + std::vector> allPath; + if (document != "{}") { + allPath = JsonCommon::ParsePath(documentObj, errCode); + if (errCode != E_OK) { + GLOGE("updateObj ParsePath faild"); + return errCode; + } + if (!CheckCommon::CheckUpdata(documentObj, allPath)) { + GLOGE("Updata format unvalid"); + return -E_INVALID_ARGS; + } } if (flags != GRD_DOC_APPEND && flags != GRD_DOC_REPLACE) { GLOGE("Check flags invalid."); @@ -234,11 +242,14 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri return errCode; } if (isOnlyId) { + std::lock_guard lock(dbMutex_); auto filterObjChild = filterObj.GetChild(); - auto idValue = JsonCommon::GetValueByFiled(filterObjChild, KEY_ID); + ValueObject idValue = JsonCommon::GetValueByFiled(filterObjChild, KEY_ID); std::string docId = idValue.GetStringValue(); - std::lock_guard lock(dbMutex_); - errCode = coll.UpsertDocument(docId, document, isReplace); + JsonObject idObj = filterObj.GetObjectItem(KEY_ID, errCode); + documentObj.InsertItemObject(0, idObj); + std::string addedIdDocument = documentObj.Print(); + errCode = coll.UpsertDocument(docId, addedIdDocument, isReplace); if (errCode == E_OK) { errCode = 1; // upsert one record. } @@ -250,15 +261,11 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri if (!isIdExist) { return -E_INVALID_ARGS; } - ResultSet resultSet; - InitResultSet(this, collection, filter, resultSet); std::lock_guard lock(dbMutex_); - errCode = resultSet.GetNext(); - if (errCode != E_OK) { - return errCode; - } - std::string docId; - resultSet.GetKey(docId); + std::string docId = idValue.GetStringValue(); + JsonObject idObj = filterObj.GetObjectItem(KEY_ID, errCode); + documentObj.InsertItemObject(0, idObj); + std::string addedIdDocument = documentObj.Print(); errCode = coll.UpsertDocument(docId, document, isReplace); if (errCode == E_OK) { errCode = 1; // upsert one record. 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 49077485374a6c3e915c28e9c60f4f6c17d247ef..32d8a335f0ac58f225895cd34592dc1ddd9d9a92 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 @@ -80,6 +80,7 @@ public: void ReplaceItemInArray(const int &index, const JsonObject &newItem, int &errCode); void ReplaceItemInObject(const std::string &filedName, const JsonObject &newItem, int &errCode); void SetItemValue(const ValueObject &value) const; + int InsertItemObject(int which, const JsonObject &newItem); std::string GetItemFiled() const; std::string GetItemFiled(int &errCode) 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 cbfb4830de036e8dd7819f6f58c9856e2801148c..530067136b22c9fe888b57cb2c29cbcb5ecd50de 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 @@ -22,7 +22,7 @@ namespace DocumentDB { namespace { const int COLLECTION_LENS_MAX = 512 * 1024; -const int JSON_LENS_MAX = 512 * 1024; +const int JSON_LENS_MAX = 1024 * 1024; bool IsNumber(const std::string &str) { @@ -411,6 +411,20 @@ void JsonObject::SetItemValue(const ValueObject &value) const } } +int JsonObject::InsertItemObject(int which, const JsonObject &newItem) +{ + if (cjson_ == nullptr) { + return E_OK; + } + if (newItem.IsNull()) { + GLOGD("Add null object."); + return E_OK; + } + cJSON *cpoyItem = cJSON_Duplicate(newItem.cjson_, true); + cJSON_InsertItemInArray(cjson_, which, cpoyItem); + return E_OK; +} + std::string JsonObject::GetItemFiled() const { if (cjson_ == nullptr) { 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 55a0b96d901d68b149afbf056de9aa6803ac9d70..e4690252018238a75d130848284f76a647f8b847 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 @@ -71,7 +71,7 @@ void DocumentDBDataTest::TearDown(void) * @tc.desc: Test upsert data into collection * @tc.type: FUNC * @tc.require: - * @tc.author: lianhuix + * @tc.author: mazhao */ HWTEST_F(DocumentDBDataTest, UpsertDataTest001, TestSize.Level0) { @@ -83,236 +83,4 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest001, TestSize.Level0) std::string append = R""({"addr.city":"DDDD"})""; EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, R""({"_id":"1234"})"", append.c_str(), GRD_DOC_APPEND), 1); -} - -/** - * @tc.name: UpsertDataTest002 - * @tc.desc: Test upsert data with db is nullptr - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpsertDataTest002, TestSize.Level0) -{ - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - EXPECT_EQ(GRD_UpsertDoc(nullptr, g_coll, "1234", document.c_str(), GRD_DOC_REPLACE), GRD_INVALID_ARGS); -} - -/** - * @tc.name: UpsertDataTest003 - * @tc.desc: Test upsert data with invalid collection name - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpsertDataTest003, TestSize.Level0) -{ - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector> invalidName = { - {nullptr, GRD_INVALID_ARGS}, - {"", GRD_INVALID_ARGS}, - {"GRD_123", GRD_INVALID_FORMAT}, - {"grd_123", GRD_INVALID_FORMAT}, - {"GM_SYS_123", GRD_INVALID_FORMAT}, - {"gm_sys_123", GRD_INVALID_FORMAT}, - }; - for (auto it : invalidName) { - GLOGD("UpsertDataTest003: upsert data with collectionname: %s", it.first); - EXPECT_EQ(GRD_UpsertDoc(g_db, it.first, "1234", document.c_str(), GRD_DOC_REPLACE), it.second); - } -} - -HWTEST_F(DocumentDBDataTest, UpsertDataTest004, TestSize.Level0) -{ - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector invalidFilter = { - nullptr, - "", - R""({"name":"Tmono"})"", - R""({"value":{"_id":"1234"}})"", - R""({"_id":1234})"", - }; - for (auto filter : invalidFilter) { - GLOGD("UpsertDataTest004: upsert data with filter: %s", filter); - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter, document.c_str(), GRD_DOC_REPLACE), GRD_INVALID_ARGS); - } -} - -HWTEST_F(DocumentDBDataTest, UpsertDataTest005, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::vector> invalidDocument = { - {"", GRD_INVALID_ARGS}, - {nullptr, GRD_INVALID_ARGS}, - {R""({invalidJsonFormat})"", GRD_INVALID_FORMAT}, - }; - for (auto it : invalidDocument) { - GLOGD("UpsertDataTest005: upsert data with document: %s", it.first); - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), it.first, GRD_DOC_REPLACE), it.second); - } -} - -/** - * @tc.name: UpsertDataTest006 - * @tc.desc: Test upsert data with invalid flags - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpsertDataTest006, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - - for (auto flags : std::vector {2, 4, 8, 64, 1024, UINT32_MAX}) { - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), flags), GRD_INVALID_ARGS); - } -} - -/** - * @tc.name: UpsertDataTest007 - * @tc.desc: Test upsert data with collection not create - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpsertDataTest007, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string val = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - EXPECT_EQ(GRD_UpsertDoc(g_db, "collection_not_exists", filter.c_str(), val.c_str(), GRD_DOC_REPLACE), GRD_NO_DATA); -} - -/** - * @tc.name: UpsertDataTest008 - * @tc.desc: Test upsert data with different document in append - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpsertDataTest008, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmn","age":18,"addr":{"city":"shanghai","postal":200001}})""; - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), GRD_DOC_REPLACE), 1); - - std::string updateDoc = R""({"name":"Xue","case":2,"age":28,"addr":{"city":"shenzhen","postal":518000}})""; - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), updateDoc.c_str(), GRD_DOC_APPEND), 1); -} - -/** - * @tc.name: UpdateDataTest001 - * @tc.desc: - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest001, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string updateDoc = R""({"name":"Xue"})""; - EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), updateDoc.c_str(), 0), GRD_OK); -} - -/** - * @tc.name: UpdateDataTest002 - * @tc.desc: Test update data with db is nullptr - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest002, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - EXPECT_EQ(GRD_UpdateDoc(nullptr, g_coll, filter.c_str(), document.c_str(), 0), GRD_INVALID_ARGS); -} - -/** - * @tc.name: UpdateDataTest003 - * @tc.desc: Test update data with invalid collection name - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest003, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector> invalidName = { - {nullptr, GRD_INVALID_ARGS}, - {"", GRD_INVALID_ARGS}, - {"GRD_123", GRD_INVALID_FORMAT}, - {"grd_123", GRD_INVALID_FORMAT}, - {"GM_SYS_123", GRD_INVALID_FORMAT}, - {"gm_sys_123", GRD_INVALID_FORMAT}, - }; - for (auto it : invalidName) { - GLOGD("UpdateDataTest003: update data with collectionname: %s", it.first); - EXPECT_EQ(GRD_UpdateDoc(g_db, it.first, filter.c_str(), document.c_str(), 0), it.second); - } -} - -/** - * @tc.name: UpdateDataTest004 - * @tc.desc: Test update data with invalid filter - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest004, TestSize.Level0) -{ - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector invalidFilter = { - nullptr, - "", - R""({"name":"Tmono"})"", - R""({"value":{"_id":"1234"}})"", - R""({"_id":1234})"", - }; - for (auto filter : invalidFilter) { - GLOGD("UpdateDataTest004: update data with filter: %s", filter); - EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter, document.c_str(), 0), GRD_INVALID_ARGS); - } -} - -/** - * @tc.name: UpdateDataTest005 - * @tc.desc: Test update data with invalid doc - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest005, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector> invalidUpdate = { - {"", GRD_INVALID_ARGS}, - {nullptr, GRD_INVALID_ARGS}, - {R""({invalidJsonFormat})"", GRD_INVALID_FORMAT}, - }; - - for (auto it : invalidUpdate) { - GLOGD("UpdateDataTest005: update data with doc: %s", it.first); - EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), it.first, 0), it.second); - } -} - -/** - * @tc.name: UpdateDataTest006 - * @tc.desc: Test update data with invalid flag - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest006, TestSize.Level0) -{ - std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::vector invalidFlags = {2, 4, 8, 1024, UINT32_MAX}; - for (auto flag : invalidFlags) { - GLOGD("UpdateDataTest006: update data with flag: %u", flag); - EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), document.c_str(), flag), GRD_INVALID_ARGS); - } } \ No newline at end of file