From b7eff7d9d312da22844599ae2219eb91cd2c0e36 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Sat, 6 May 2023 19:36:29 +0800 Subject: [PATCH 1/2] Fixed the issue that documents with the same ID were not reported with errors Signed-off-by: Jeremyzz --- .../src/interface/src/document_store.cpp | 29 +++++++++++++++++-- .../unittest/api/documentdb_data_test.cpp | 10 +++++++ .../unittest/api/documentdb_find_test.cpp | 2 ++ .../unittest/api/documentdb_insert_test.cpp | 9 ++++++ 4 files changed, 48 insertions(+), 2 deletions(-) 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 210316d8..43fa04aa 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 @@ -253,17 +253,31 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri } return errCode; } + std::lock_guard lock(dbMutex_); bool isIdExist; auto filterObjChild = filterObj.GetChild(); auto idValue = JsonCommon::GetValueByFiled(filterObjChild, KEY_ID, isIdExist); if (!isIdExist) { return -E_INVALID_ARGS; } - std::lock_guard lock(dbMutex_); + ResultSet resultSet; + InitResultSet(this, collection, filter, resultSet); + errCode = resultSet.GetNext(); + bool isfilterMatch = false; + if (errCode == E_OK) { + isfilterMatch = true; + } std::string docId = idValue.GetStringValue(); JsonObject idObj = filterObj.GetObjectItem(KEY_ID, errCode); documentObj.InsertItemObject(0, idObj); std::string addedIdDocument = documentObj.Print(); + Value ValueDocument; + Key key(docId.begin(), docId.end()); + errCode = coll.GetDocument(key, ValueDocument); + if (errCode == E_OK && !(isfilterMatch)) { + GLOGE("id exist but filter does not match, data conflict"); + return -E_DATA_CONFLICT; + } errCode = coll.UpsertDocument(docId, addedIdDocument, isReplace); if (errCode == E_OK) { errCode = 1; // upsert one record. @@ -305,7 +319,18 @@ int DocumentStore::InsertDocument(const std::string &collection, const std::stri Key key(id.begin(), id.end()); Value value(document.begin(), document.end()); std::lock_guard lock(dbMutex_); - return coll.PutDocument(key, value); + Value ValueDocument; + errCode = coll.GetDocument(key, ValueDocument); + switch (errCode) { + case -E_NOT_FOUND: + return coll.PutDocument(key, value); + case -E_ERROR: + GLOGE("collection dont exsited"); + return -E_INVALID_ARGS; + default: + GLOGE("id already exsited, data conflict"); + return -E_DATA_CONFLICT; + } } int DocumentStore::DeleteDocument(const std::string &collection, const std::string &filter, int flag) 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 17ff76b7..0e4dd291 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 @@ -170,6 +170,16 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest008, TestSize.Level0) EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), updateDoc.c_str(), GRD_DOC_APPEND), 1); } +HWTEST_F(DocumentDBDataTest, UpsertDataTest009, TestSize.Level0) +{ + std::string filter = R""({"_id":"1234", "aaa" : "bbb"})""; + 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_APPEND), 1); + std::string filter2 = R""({"_id":"1234", "aaa" : "ccc"})""; + 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(), document.c_str(), GRD_DOC_APPEND), GRD_DATA_CONFLICT); +} + /** * @tc.name: UpdateDataTest001 * @tc.desc: 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 3691ff16..94f74608 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 @@ -122,6 +122,8 @@ void DocumentFindApiTest::TearDownTestCase(void) void DocumentFindApiTest::SetUp(void) { + EXPECT_EQ(GRD_DropCollection(g_db, COLLECTION_NAME, 0), GRD_OK); + EXPECT_EQ(GRD_CreateCollection(g_db, COLLECTION_NAME, "", 0), GRD_OK); InsertData(g_db, "student"); } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp index fc489a73..9b08e1c5 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_insert_test.cpp @@ -821,3 +821,12 @@ HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest043, TestSize.Level1) EXPECT_EQ(GRD_UpdateDoc(g_db, RIGHT_COLLECTION_NAME, NULL, updata1, 0), GRD_INVALID_ARGS); EXPECT_EQ(GRD_UpdateDoc(g_db, RIGHT_COLLECTION_NAME, filter, NULL, 0), GRD_INVALID_ARGS); } + +HWTEST_F(DocumentInsertApiTest, DocumentInsertApiTest044, TestSize.Level1) +{ + + const char *document1 = R""({"_id":"0123", "num":"num"})""; + const char *document2 = R""({"_id":"0123", "NUM":"No.45"})""; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_DATA_CONFLICT); +} -- Gitee From 9fde5883731b6af60c70b1eecaa29620b67c318d Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 8 May 2023 21:54:12 +0800 Subject: [PATCH 2/2] fix code Check Signed-off-by: Jeremyzz --- .../gaussdb_rd_simple/src/interface/src/document_store.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 43fa04aa..934b180f 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 @@ -253,13 +253,13 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri } return errCode; } - std::lock_guard lock(dbMutex_); bool isIdExist; auto filterObjChild = filterObj.GetChild(); auto idValue = JsonCommon::GetValueByFiled(filterObjChild, KEY_ID, isIdExist); if (!isIdExist) { return -E_INVALID_ARGS; } + std::lock_guard lock(dbMutex_); ResultSet resultSet; InitResultSet(this, collection, filter, resultSet); errCode = resultSet.GetNext(); -- Gitee