From 0c4c8085ed2d98b14999d5936b9688b0bd594df0 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 23 May 2023 09:47:00 +0800 Subject: [PATCH 01/32] finish GenerralId FUC Signed-off-by: Jeremyzz --- .../src/executor/document/check_common.cpp | 7 +- .../src/executor/document/check_common.h | 2 +- .../src/interface/include/document_key.h | 55 +++++++++++++++ .../src/interface/include/document_store.h | 3 +- .../src/interface/src/document_key.cpp | 68 +++++++++++++++++++ .../src/interface/src/document_store.cpp | 41 +++++++---- .../unittest/api/documentdb_find_test.cpp | 29 ++++++++ .../unittest/api/documentdb_insert_test.cpp | 2 +- .../documentdb_key_document_test.cpp | 56 +++++++++++++++ 9 files changed, 241 insertions(+), 22 deletions(-) create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_key_document_test.cpp diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/check_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/check_common.cpp index 1e5fc758..1bd7e4c3 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/check_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/check_common.cpp @@ -125,18 +125,13 @@ int CheckCommon::CheckIdFormat(JsonObject &filterJson, bool &isIdExisit) return E_OK; } -int CheckCommon::CheckDocument(JsonObject &documentObj) +int CheckCommon::CheckDocument(JsonObject &documentObj, bool &isIdExist) { if (documentObj.GetDeep() > JSON_DEEP_MAX) { GLOGE("documentObj's json deep is deeper than JSON_DEEP_MAX"); return -E_INVALID_ARGS; } - bool isIdExist = true; int ret = CheckIdFormat(documentObj, isIdExist); - if (!isIdExist) { - GLOGE("Document Id format is illegal"); - return -E_INVALID_ARGS; - } if (ret != E_OK) { return ret; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/check_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/check_common.h index 18a01c64..92860bc5 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/check_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/executor/document/check_common.h @@ -31,7 +31,7 @@ public: static bool CheckCollectionName(const std::string &collectionName, std::string &formattedName, int &errCode); static int CheckFilter(JsonObject &document, std::vector> &filterPath, bool &isIdExist); static int CheckIdFormat(JsonObject &data, bool &isIdExisit); - static int CheckDocument(JsonObject &document); + static int CheckDocument(JsonObject &document, bool &isIdExist); static int CheckUpdata(JsonObject &updata, std::vector> &path); static int CheckProjection(JsonObject &projectionObj, std::vector> &path); }; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h new file mode 100644 index 00000000..782d5fd6 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h @@ -0,0 +1,55 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef DOCUMENT_KEY_H +#define DOCUMENT_KEY_H + +#include +#include + +#include "json_object.h" + +#define GRD_DOC_OID_TIME_SIZE 4 +#define GRD_DOC_OID_INCREMENTAL_VALUE_SIZE 2 +#define GRD_DOC_OID_SIZE (GRD_DOC_OID_TIME_SIZE + GRD_DOC_OID_INCREMENTAL_VALUE_SIZE) +#define GRD_DOC_OID_HEX_SIZE (GRD_DOC_OID_SIZE * 2) +#define GRD_DOC_ID_TYPE_SIZE 1 + +namespace DocumentDB { + +typedef enum DocIdType { + INT = 1, + STRING, +} DocIdType; + +class DocKey { +public: + ~DocKey(){}; + int32_t keySize; + std::string id; + std::string key; + uint8_t type; +}; + +class DocumentKey { +public: + static int GetStringDocKey(const std::string &id, int32_t size, DocKey **key); + static int GetOidDocKey(DocKey &key); + static int GenDocKey(JsonObject &value, DocKey **key); + static int FreeDocKey(DocKey *key); +}; + +} // namespace DocumentDB +#endif // DOCUMENT_KEY_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_store.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_store.h index 3437d8ca..3f1deacb 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_store.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_store.h @@ -71,7 +71,8 @@ private: bool &isReplace); int UpsertDataIntoDB(std::shared_ptr &context, JsonObject &filterObj, const std::string &document, JsonObject &documentObj, bool &isReplace); - int InsertDataIntoDB(const std::string &collection, const std::string &document, JsonObject &documentObj); + int InsertDataIntoDB(const std::string &collection, const std::string &document, JsonObject &documentObj, + bool &isIdExist); int DeleteDataFromDB(std::shared_ptr &context, JsonObject &filterObj); int InitFindResultSet(GRD_ResultSet *grdResultSet, std::shared_ptr &context); KvStoreExecutor *executor_ = nullptr; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp new file mode 100644 index 00000000..1e5083ae --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp @@ -0,0 +1,68 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "document_key.h" + +#include "doc_errno.h" +#include "log_print.h" +#include "securec.h" +namespace DocumentDB { + +static uint16_t g_oIdIncNum = 0; + +static int InitDocIdFromOid(DocKey &docKey) +{ + time_t nowTime = time(nullptr); + if (nowTime < 0) { + return -E_INNER_ERROR; + } + uint32_t now = (uint32_t)nowTime; + uint16_t iv = g_oIdIncNum++; + // The maximum number of autoincrements is 65535, and if it is exceeded, it becomes 0. + if (g_oIdIncNum > (uint16_t)65535) { + g_oIdIncNum = (uint16_t)0; + } + char *idTemp = new char[GRD_DOC_OID_HEX_SIZE + 1]; + if (sprintf_s(idTemp, GRD_DOC_OID_HEX_SIZE + 1, "%08x%04x", now, iv) < 0) { + GLOGE("get oid error"); + return -E_INNER_ERROR; + } + docKey.id = idTemp; + delete[] idTemp; + docKey.type = (uint8_t)STRING; + return E_OK; +} + +static int SerializeDocKey(DocKey &key, const std::string &id, const int32_t size) +{ + std::string idStr = id; + key.key = idStr; + key.key = key.key + std::to_string(key.type); // Question here + key.keySize = size + GRD_DOC_ID_TYPE_SIZE; + return E_OK; +} + +int DocumentKey::GetOidDocKey(DocKey &key) +{ + int ret = InitDocIdFromOid(key); + { + if (ret != E_OK) { + return ret; + } + } + ret = SerializeDocKey(key, key.id, GRD_DOC_OID_HEX_SIZE); + return ret; +} + +} // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index c8d99221..9d81c7fa 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -18,6 +18,7 @@ #include "check_common.h" #include "collection_option.h" #include "doc_errno.h" +#include "document_key.h" #include "grd_base/grd_type_export.h" #include "grd_resultset_inner.h" #include "log_print.h" @@ -358,15 +359,18 @@ int InsertIdToDocument(JsonObject &filterObj, JsonObject &documentObj, bool &isI std::string &docId) { auto filterObjChild = filterObj.GetChild(); - ValueObject idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID, isIdExist); - docId = idValue.GetStringValue(); int errCode = E_OK; - JsonObject idObj = filterObj.GetObjectItem(KEY_ID, errCode); - documentObj.InsertItemObject(0, idObj); - targetDocument = documentObj.Print(); - if (!isIdExist) { - errCode = -E_INVALID_ARGS; + ValueObject idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID, isIdExist); + if (isIdExist) { + docId = idValue.GetStringValue(); + JsonObject idObj = filterObj.GetObjectItem(KEY_ID, errCode); + documentObj.InsertItemObject(0, idObj); + } else { + DocKey docKey; + DocumentKey::GetOidDocKey(docKey); + docId = docKey.key; } + targetDocument = documentObj.Print(); return errCode; } @@ -407,6 +411,7 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json if (errCode != E_OK) { goto END; } + GLOGE("docId is =============>%s", docId.c_str()); errCode = coll.UpsertDocument(docId, newStr, isReplace); if (errCode == E_OK) { count++; @@ -496,12 +501,21 @@ int InsertArgsCheck(const std::string &collection, const std::string &document, return errCode; } -int DocumentStore::InsertDataIntoDB(const std::string &collection, const std::string &document, JsonObject &documentObj) +int DocumentStore::InsertDataIntoDB(const std::string &collection, const std::string &document, + JsonObject &documentObj, bool &isIdExist) { std::lock_guard lock(dbMutex_); - JsonObject documentObjChild = documentObj.GetChild(); - ValueObject idValue = JsonCommon::GetValueInSameLevel(documentObjChild, KEY_ID); - std::string id = idValue.GetStringValue(); + std::string id; + if (isIdExist) { + JsonObject documentObjChild = documentObj.GetChild(); + ValueObject idValue = JsonCommon::GetValueInSameLevel(documentObjChild, KEY_ID); + id = idValue.GetStringValue(); + + } else { + DocKey docKey; + DocumentKey::GetOidDocKey(docKey); + id = docKey.key; + } Key key(id.begin(), id.end()); Value value(document.begin(), document.end()); Collection coll = Collection(collection, executor_); @@ -519,11 +533,12 @@ int DocumentStore::InsertDocument(const std::string &collection, const std::stri GLOGE("Document Parsed failed"); return errCode; } - errCode = CheckCommon::CheckDocument(documentObj); + bool isIdExist = true; + errCode = CheckCommon::CheckDocument(documentObj, isIdExist); if (errCode != E_OK) { return errCode; } - return InsertDataIntoDB(collection, document, documentObj); + return InsertDataIntoDB(collection, document, documentObj, isIdExist); } int DeleteArgsCheck(const std::string &collection, const std::string &filter, uint32_t flags) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp index 01320cbc..38da55bd 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp @@ -1465,4 +1465,33 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest062, TestSize.Level1) Query query = { filter, projection }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); } + +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest063, TestSize.Level1) +{ + const char *document = "{\"a\":1, \"doc64\" : 2}"; + const char *filter1 = "{\"b\":1}"; + EXPECT_EQ(GRD_UpsertDoc(g_db, COLLECTION_NAME, filter1, document, 0), 1); + EXPECT_EQ(GRD_UpsertDoc(g_db, COLLECTION_NAME, filter1, document, 0), 1); + EXPECT_EQ(GRD_UpsertDoc(g_db, COLLECTION_NAME, filter1, document, 0), 1); + EXPECT_EQ(GRD_UpsertDoc(g_db, COLLECTION_NAME, filter1, document, 0), 1); + EXPECT_EQ(GRD_UpsertDoc(g_db, COLLECTION_NAME, filter1, document, 0), 1); + EXPECT_EQ(GRD_UpsertDoc(g_db, COLLECTION_NAME, filter1, document, 0), 1); + EXPECT_EQ(GRD_UpsertDoc(g_db, COLLECTION_NAME, filter1, document, 0), 1); + EXPECT_EQ(GRD_UpsertDoc(g_db, COLLECTION_NAME, filter1, document, 0), 1); + const char *filter = "{\"a\":1}"; + GRD_ResultSet *resultSet = nullptr; + const char *projection = R"({})"; + Query query = { filter, projection }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} } // namespace diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp index 54be32da..d2739db2 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp @@ -824,6 +824,6 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest045, TestSize.Level1) HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest046, TestSize.Level1) { const char *document1 = R""({})""; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); } } // namespace diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_key_document_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_key_document_test.cpp new file mode 100644 index 00000000..9e53f90a --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_key_document_test.cpp @@ -0,0 +1,56 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include + +#include "doc_errno.h" +#include "document_key.h" +#include "documentdb_test_utils.h" +#include "log_print.h" + +using namespace DocumentDB; +using namespace testing::ext; +using namespace DocumentDBUnitTest; + +namespace { +class DocumentDBKeyCommonTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DocumentDBKeyCommonTest::SetUpTestCase(void) {} + +void DocumentDBKeyCommonTest::TearDownTestCase(void) {} + +void DocumentDBKeyCommonTest::SetUp(void) {} + +void DocumentDBKeyCommonTest::TearDown(void) {} + +HWTEST_F(DocumentDBKeyCommonTest, KeyCommonTest001, TestSize.Level0) +{ + DocKey docKey; + for (int i = 0; i < 65537; i++) { + DocumentKey::GetOidDocKey(docKey); + if (i > 65530) { + GLOGE("docKey key is=============>%s", docKey.key.c_str()); + } + } +} + +} // namespace \ No newline at end of file -- Gitee From f50f06cfb9d775a3c3f8dd06949435a7bc8113cb Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 23 May 2023 10:41:36 +0800 Subject: [PATCH 02/32] add Add a method to generate duplicate IDs and then regenerate Signed-off-by: Jeremyzz --- .../src/interface/include/collection.h | 5 +-- .../src/interface/src/collection.cpp | 37 +++++++++++++------ .../src/interface/src/document_store.cpp | 7 +--- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h index 33158c5e..f434fc2e 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h @@ -29,14 +29,13 @@ public: Collection(){}; ~Collection(); - int PutDocument(const Key &key, const Value &document); - int InsertDocument(const Key &key, const Value &document); + int InsertDocument(const std::string &key, const std::string &document, bool &isIdExist); int GetDocumentByKey(const Key &key, Value &document) const; int GetMatchedDocument(const JsonObject &filterObj, const Key &key, std::pair &values, int isIdExist) const; int DeleteDocument(const Key &key); int IsCollectionExists(int &errCode); - int UpsertDocument(const std::string &id, const std::string &newStr, bool isReplace = true); + int UpsertDocument(const std::string &id, const std::string &newStr, bool &isIdExist, bool isReplace = true); bool FindDocument(); int UpdateDocument(const std::string &id, const std::string &document, bool isReplace = false); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index 88887ff6..20584392 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -18,6 +18,7 @@ #include "check_common.h" #include "db_constant.h" #include "doc_errno.h" +#include "document_key.h" #include "log_print.h" namespace DocumentDB { @@ -43,15 +44,7 @@ Collection::~Collection() executor_ = nullptr; } -int Collection::PutDocument(const Key &key, const Value &document) -{ - if (executor_ == nullptr) { - return -E_INNER_ERROR; - } - return executor_->PutData(name_, key, document); -} - -int Collection::InsertDocument(const Key &key, const Value &document) +int Collection::InsertDocument(const std::string &key, const std::string &document, bool &isIdExist) { if (executor_ == nullptr) { return -E_INNER_ERROR; @@ -64,7 +57,19 @@ int Collection::InsertDocument(const Key &key, const Value &document) if (!isCollectionExist) { return -E_INVALID_ARGS; } - return executor_->InsertData(name_, key, document); + Key keyId(key.begin(), key.end()); + Value valSet(document.begin(), document.end()); + DocKey docKey; + if (!isIdExist) { + errCode = executor_->InsertData(name_, keyId, valSet); + while (errCode != E_OK) { + DocumentKey::GetOidDocKey(docKey); + keyId.assign(docKey.key.begin(), docKey.key.end()); + errCode = executor_->InsertData(name_, keyId, valSet); + } + return errCode; + } + return executor_->InsertData(name_, keyId, valSet); } bool Collection::FindDocument() @@ -114,7 +119,7 @@ int Collection::IsCollectionExists(int &errCode) return executor_->IsCollectionExists(name_, errCode); } -int Collection::UpsertDocument(const std::string &id, const std::string &newStr, bool isReplace) +int Collection::UpsertDocument(const std::string &id, const std::string &newStr, bool &isIdExist, bool isReplace) { if (executor_ == nullptr) { return -E_INNER_ERROR; @@ -131,6 +136,16 @@ int Collection::UpsertDocument(const std::string &id, const std::string &newStr, } Key keyId(id.begin(), id.end()); Value valSet(newStr.begin(), newStr.end()); + DocKey docKey; + if (!isIdExist) { + errCode = executor_->PutData(name_, keyId, valSet); + while (errCode != E_OK) { + DocumentKey::GetOidDocKey(docKey); + keyId.assign(docKey.key.begin(), docKey.key.end()); + errCode = executor_->PutData(name_, keyId, valSet); + } + return errCode; + } return executor_->PutData(name_, keyId, valSet); } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index 9d81c7fa..cd861b00 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -411,8 +411,7 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json if (errCode != E_OK) { goto END; } - GLOGE("docId is =============>%s", docId.c_str()); - errCode = coll.UpsertDocument(docId, newStr, isReplace); + errCode = coll.UpsertDocument(docId, newStr, isIdExist, isReplace); if (errCode == E_OK) { count++; } else if (errCode == -E_NOT_FOUND) { @@ -516,10 +515,8 @@ int DocumentStore::InsertDataIntoDB(const std::string &collection, const std::st DocumentKey::GetOidDocKey(docKey); id = docKey.key; } - Key key(id.begin(), id.end()); - Value value(document.begin(), document.end()); Collection coll = Collection(collection, executor_); - return coll.InsertDocument(key, value); + return coll.InsertDocument(id, document, isIdExist); } int DocumentStore::InsertDocument(const std::string &collection, const std::string &document, uint32_t flags) -- Gitee From 385506296280723b287d0f100d67a827581551c3 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 23 May 2023 15:24:42 +0800 Subject: [PATCH 03/32] fix bug that ut find Signed-off-by: Jeremyzz --- .../src/interface/include/document_key.h | 4 +- .../src/interface/include/result_set.h | 2 +- .../src/interface/src/document_key.cpp | 7 +++ .../src/interface/src/result_set.cpp | 50 +++++++++++++++++-- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h index 782d5fd6..f04d671e 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h @@ -45,10 +45,8 @@ public: class DocumentKey { public: - static int GetStringDocKey(const std::string &id, int32_t size, DocKey **key); static int GetOidDocKey(DocKey &key); - static int GenDocKey(JsonObject &value, DocKey **key); - static int FreeDocKey(DocKey *key); + static std::string GetIdFromKey(std::string &keyStr); }; } // namespace DocumentDB 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 1191c545..7d0fbf23 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 @@ -39,7 +39,7 @@ public: private: int GetNextInner(bool isNeedCheckTable); - int CutJsonBranch(std::string &jsonData); + int CutJsonBranch(std::string &jsonKey, std::string &jsonData); int CheckCutNode(JsonObject *node, std::vector singleCutPath, std::vector> &allCutPath); int GetNextWithField(); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp index 1e5083ae..76c6b459 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp @@ -65,4 +65,11 @@ int DocumentKey::GetOidDocKey(DocKey &key) return ret; } +std::string DocumentKey::GetIdFromKey(std::string &keyStr) +{ + if (!keyStr.empty()) { + keyStr.pop_back(); + } + return keyStr; +} } // namespace DocumentDB \ No newline at end of file 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 65751f2e..b379521a 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 @@ -15,6 +15,7 @@ #include "result_set.h" #include "db_constant.h" +#include "document_key.h" #include "log_print.h" #include "securec.h" @@ -70,7 +71,7 @@ int ResultSet::GetNextWithField() std::string jsonkey(value.first.begin(), value.first.end()); lastKeyIndex_ = jsonkey; if (isCutBranch_) { - errCode = CutJsonBranch(jsonData); + errCode = CutJsonBranch(jsonkey, jsonData); if (errCode != E_OK) { GLOGE("cut branch faild"); return errCode; @@ -193,7 +194,35 @@ int ResultSet::CheckCutNode(JsonObject *node, std::vector singlePat } return E_OK; } -int ResultSet::CutJsonBranch(std::string &jsonData) + +JsonObject CreatIdObj(const std::string &idStr, int errCode) +{ + JsonObject idObj = JsonObject::Parse("{}", errCode, true); // cant be faild. + idObj.AddItemToObject("_id"); + ValueObject idValue = ValueObject(idStr.c_str()); + idObj = idObj.GetObjectItem("_id", errCode); // cant be faild. + (void)idObj.SetItemValue(idValue); + return idObj; +} + +int InsertRandomId(JsonObject &cjsonObj, std::string &jsonKey) +{ + std::string idStr = DocumentKey::GetIdFromKey(jsonKey); + if (idStr.empty()) { + GLOGE("Genalral Id faild"); + return -E_INNER_ERROR; + } + int errCode = E_OK; + JsonObject idObj = CreatIdObj(idStr, errCode); + if (errCode != E_OK) { + GLOGE("CreatIdObj faild"); + return errCode; + } + cjsonObj.InsertItemObject(0, idObj); + return E_OK; +} + +int ResultSet::CutJsonBranch(std::string &jsonKey, std::string &jsonData) { int errCode; JsonObject cjsonObj = JsonObject::Parse(jsonData, errCode, true); @@ -201,8 +230,11 @@ int ResultSet::CutJsonBranch(std::string &jsonData) GLOGE("jsonData Parsed failed"); return errCode; } - std::vector> allCutPath; + bool isIdExistInValue = true; // if id exsit in the value string that get from db. + bool isInsertIdflag = false; + isIdExistInValue = cjsonObj.GetObjectItem("_id", errCode).IsNull() ? false : true; if (context_->viewType) { + std::vector> allCutPath; std::vector singlePath; JsonObject cjsonObjChild = cjsonObj.GetChild(); errCode = CheckCutNode(&cjsonObjChild, singlePath, allCutPath); @@ -214,6 +246,9 @@ int ResultSet::CutJsonBranch(std::string &jsonData) if (!context_->ifShowId || singleCutPaht[0] != KEY_ID) { cjsonObj.DeleteItemDeeplyOnTarget(singleCutPaht); } + if (singleCutPaht[0] == KEY_ID && !isIdExistInValue) { // projection has Id, and its showType is true. + isInsertIdflag = true; + } } } if (!context_->viewType) { @@ -226,6 +261,15 @@ int ResultSet::CutJsonBranch(std::string &jsonData) cjsonObj.DeleteItemDeeplyOnTarget(idPath); } } + if (context_->ifShowId && !isIdExistInValue) { + isInsertIdflag = true; // ifShowId is true,and then the data taken out does not have IDs, insert id. + } + if (isInsertIdflag) { + errCode = InsertRandomId(cjsonObj, jsonKey); + if (errCode != E_OK) { + return errCode; + } + } jsonData = cjsonObj.Print(); return E_OK; } -- Gitee From 4f5271fa86c18ac39c9f23f18c257fb2f3f13319 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 23 May 2023 17:34:47 +0800 Subject: [PATCH 04/32] fix bug Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/interface/src/result_set.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) 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 b379521a..e57ed5f2 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 @@ -14,6 +14,8 @@ */ #include "result_set.h" +#include + #include "db_constant.h" #include "document_key.h" #include "log_print.h" @@ -197,11 +199,11 @@ int ResultSet::CheckCutNode(JsonObject *node, std::vector singlePat JsonObject CreatIdObj(const std::string &idStr, int errCode) { - JsonObject idObj = JsonObject::Parse("{}", errCode, true); // cant be faild. - idObj.AddItemToObject("_id"); - ValueObject idValue = ValueObject(idStr.c_str()); - idObj = idObj.GetObjectItem("_id", errCode); // cant be faild. - (void)idObj.SetItemValue(idValue); + std::stringstream sstream; + sstream << "{\"_id\":" + << "\"" << idStr << "\"}"; + JsonObject idObj = JsonObject::Parse(sstream.str(), errCode, true); // cant be faild. + idObj = idObj.GetChild(); return idObj; } -- Gitee From d7691297a1788de25b1a7bc663e8b20c49ec7963 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Wed, 24 May 2023 16:28:05 +0800 Subject: [PATCH 05/32] fix bug Signed-off-by: Jeremyzz --- .../src/interface/src/result_set.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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 e57ed5f2..24e60db3 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 @@ -235,6 +235,9 @@ int ResultSet::CutJsonBranch(std::string &jsonKey, std::string &jsonData) bool isIdExistInValue = true; // if id exsit in the value string that get from db. bool isInsertIdflag = false; isIdExistInValue = cjsonObj.GetObjectItem("_id", errCode).IsNull() ? false : true; + if (context_->ifShowId && !isIdExistInValue) { + isInsertIdflag = true; // ifShowId is true,and then the data taken out does not have IDs, insert id. + } if (context_->viewType) { std::vector> allCutPath; std::vector singlePath; @@ -253,6 +256,12 @@ int ResultSet::CutJsonBranch(std::string &jsonKey, std::string &jsonData) } } } + if (isInsertIdflag) { + errCode = InsertRandomId(cjsonObj, jsonKey); + if (errCode != E_OK) { + return errCode; + } + } if (!context_->viewType) { for (const auto &singleCutPaht : context_->projectionPath) { cjsonObj.DeleteItemDeeplyOnTarget(singleCutPaht); @@ -263,15 +272,6 @@ int ResultSet::CutJsonBranch(std::string &jsonKey, std::string &jsonData) cjsonObj.DeleteItemDeeplyOnTarget(idPath); } } - if (context_->ifShowId && !isIdExistInValue) { - isInsertIdflag = true; // ifShowId is true,and then the data taken out does not have IDs, insert id. - } - if (isInsertIdflag) { - errCode = InsertRandomId(cjsonObj, jsonKey); - if (errCode != E_OK) { - return errCode; - } - } jsonData = cjsonObj.Print(); return E_OK; } -- Gitee From 5e1c8599a74f6129063d3cf33745b938874ed727 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Wed, 24 May 2023 17:25:17 +0800 Subject: [PATCH 06/32] fix bug Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/interface/src/result_set_common.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/result_set_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/result_set_common.cpp index d4e73e56..8a142704 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/result_set_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/result_set_common.cpp @@ -23,6 +23,12 @@ class ValueObject; int InitResultSet(std::shared_ptr &context, DocumentStore *store, ResultSet &resultSet, bool isCutBranch) { if (isCutBranch) { + for (const auto &singlePath : context->projectionPath) { + if (singlePath[0] == "_id" && context->viewType == true) { // projection has Id and viewType is true + context->ifShowId = true; + break; + } + } if (context->projectionTree.ParseTree(context->projectionPath) == -E_INVALID_ARGS) { GLOGE("Parse ProjectionTree failed"); return -E_INVALID_ARGS; -- Gitee From c3ed669114d77e62432ef199046e8b8b28ff2995 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 25 May 2023 14:51:03 +0800 Subject: [PATCH 07/32] If IdExist, add type to id tail Signed-off-by: Jeremyzz --- .../src/interface/include/collection.h | 2 +- .../src/interface/include/document_key.h | 1 + .../src/interface/src/collection.cpp | 30 ++++++++++++------- .../src/interface/src/document_key.cpp | 16 ++++++++-- .../src/interface/src/result_set.cpp | 4 ++- 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h index f434fc2e..53f38213 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h @@ -29,7 +29,7 @@ public: Collection(){}; ~Collection(); - int InsertDocument(const std::string &key, const std::string &document, bool &isIdExist); + int InsertDocument(const std::string &id, const std::string &document, bool &isIdExist); int GetDocumentByKey(const Key &key, Value &document) const; int GetMatchedDocument(const JsonObject &filterObj, const Key &key, std::pair &values, int isIdExist) const; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h index f04d671e..8aa157c0 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h @@ -46,6 +46,7 @@ public: class DocumentKey { public: static int GetOidDocKey(DocKey &key); + static int GetStringDocKey(const std::string &id, DocKey &key); static std::string GetIdFromKey(std::string &keyStr); }; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index 20584392..d3abcce6 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -44,7 +44,7 @@ Collection::~Collection() executor_ = nullptr; } -int Collection::InsertDocument(const std::string &key, const std::string &document, bool &isIdExist) +int Collection::InsertDocument(const std::string &id, const std::string &document, bool &isIdExist) { if (executor_ == nullptr) { return -E_INNER_ERROR; @@ -57,19 +57,23 @@ int Collection::InsertDocument(const std::string &key, const std::string &docume if (!isCollectionExist) { return -E_INVALID_ARGS; } - Key keyId(key.begin(), key.end()); + Key key; Value valSet(document.begin(), document.end()); DocKey docKey; if (!isIdExist) { - errCode = executor_->InsertData(name_, keyId, valSet); + errCode = executor_->InsertData(name_, key, valSet); while (errCode != E_OK) { DocumentKey::GetOidDocKey(docKey); - keyId.assign(docKey.key.begin(), docKey.key.end()); - errCode = executor_->InsertData(name_, keyId, valSet); + key.assign(docKey.key.begin(), docKey.key.end()); + errCode = executor_->InsertData(name_, key, valSet); } return errCode; + } else { + DocKey docKey; + DocumentKey::GetStringDocKey(id, docKey); + key.assign(docKey.key.begin(), docKey.key.end()); } - return executor_->InsertData(name_, keyId, valSet); + return executor_->InsertData(name_, key, valSet); } bool Collection::FindDocument() @@ -134,19 +138,23 @@ int Collection::UpsertDocument(const std::string &id, const std::string &newStr, GLOGE("Collection not created."); return -E_INVALID_ARGS; } - Key keyId(id.begin(), id.end()); + Key key(id.begin(), id.end()); Value valSet(newStr.begin(), newStr.end()); DocKey docKey; if (!isIdExist) { - errCode = executor_->PutData(name_, keyId, valSet); + errCode = executor_->PutData(name_, key, valSet); while (errCode != E_OK) { DocumentKey::GetOidDocKey(docKey); - keyId.assign(docKey.key.begin(), docKey.key.end()); - errCode = executor_->PutData(name_, keyId, valSet); + key.assign(docKey.key.begin(), docKey.key.end()); + errCode = executor_->PutData(name_, key, valSet); } return errCode; + } else { + DocKey docKey; + DocumentKey::GetStringDocKey(id, docKey); + key.assign(docKey.key.begin(), docKey.key.end()); } - return executor_->PutData(name_, keyId, valSet); + return executor_->PutData(name_, key, valSet); } int Collection::UpdateDocument(const std::string &id, const std::string &newStr, bool isReplace) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp index 76c6b459..e99582f1 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp @@ -44,15 +44,25 @@ static int InitDocIdFromOid(DocKey &docKey) return E_OK; } -static int SerializeDocKey(DocKey &key, const std::string &id, const int32_t size) +static int SerializeDocKey(DocKey &key, const std::string &id) { std::string idStr = id; key.key = idStr; key.key = key.key + std::to_string(key.type); // Question here - key.keySize = size + GRD_DOC_ID_TYPE_SIZE; return E_OK; } +int DocumentKey::GetStringDocKey(const std::string &id, DocKey &key) +{ + if (id.empty()) { + return GetOidDocKey(key); // It won't go to this branch at the moment. + } + key.id = id; + key.type = (uint8_t)STRING; + int ret = SerializeDocKey(key, key.id); + return ret; +} + int DocumentKey::GetOidDocKey(DocKey &key) { int ret = InitDocIdFromOid(key); @@ -61,7 +71,7 @@ int DocumentKey::GetOidDocKey(DocKey &key) return ret; } } - ret = SerializeDocKey(key, key.id, GRD_DOC_OID_HEX_SIZE); + ret = SerializeDocKey(key, key.id); return ret; } 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 24e60db3..9d052080 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 @@ -57,7 +57,9 @@ int ResultSet::GetNextWithField() JsonObject filterObjChild = filterObj.GetChild(); ValueObject idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID); std::string idKey = idValue.GetStringValue(); - key.assign(idKey.begin(), idKey.end()); + DocKey docKey; + DocumentKey::GetStringDocKey(idKey, docKey); + key.assign(docKey.key.begin(), docKey.key.end()); } else { key.assign(lastKeyIndex_.begin(), lastKeyIndex_.end()); } -- Gitee From 8c595d9c102d3c63b0680da2b95fb287a8b38c04 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 25 May 2023 18:05:33 +0800 Subject: [PATCH 08/32] fix bug Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/common/src/json_common.cpp | 1 - .../src/interface/include/result_set.h | 1 + .../src/interface/src/collection.cpp | 4 +- .../src/interface/src/document_store.cpp | 22 +++--- .../src/interface/src/result_set.cpp | 67 ++++++++++++------- .../src/oh_adapter/src/kv_store_manager.cpp | 2 +- .../src/sqlite_store_executor_impl.cpp | 1 - 7 files changed, 58 insertions(+), 40 deletions(-) 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 61f29c65..cf103af1 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 @@ -719,7 +719,6 @@ bool JsonCommon::IsJsonNodeMatch(const JsonObject &src, const JsonObject &target if (isAlreadyMatched == 0) { // Not match anything isMatchFlag = false; } - // Source path not exist, if leaf value is null, isMatchFlag become true, else it will become false. return false; } }); 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 7d0fbf23..8c015533 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 @@ -39,6 +39,7 @@ public: private: int GetNextInner(bool isNeedCheckTable); + int GetValueFromDB(Key &key, JsonObject &filterObj, std::string &jsonKey, std::string &jsonData); int CutJsonBranch(std::string &jsonKey, std::string &jsonData); int CheckCutNode(JsonObject *node, std::vector singleCutPath, std::vector> &allCutPath); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index d3abcce6..8bbc7246 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -61,6 +61,7 @@ int Collection::InsertDocument(const std::string &id, const std::string &documen Value valSet(document.begin(), document.end()); DocKey docKey; if (!isIdExist) { + key.assign(id.begin(), id.end()); errCode = executor_->InsertData(name_, key, valSet); while (errCode != E_OK) { DocumentKey::GetOidDocKey(docKey); @@ -138,10 +139,11 @@ int Collection::UpsertDocument(const std::string &id, const std::string &newStr, GLOGE("Collection not created."); return -E_INVALID_ARGS; } - Key key(id.begin(), id.end()); + Key key; Value valSet(newStr.begin(), newStr.end()); DocKey docKey; if (!isIdExist) { + key.assign(id.begin(), id.end()); errCode = executor_->PutData(name_, key, valSet); while (errCode != E_OK) { DocumentKey::GetOidDocKey(docKey); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index cd861b00..bbbf4415 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -280,6 +280,7 @@ int DocumentStore::UpdateDocument(const std::string &collection, const std::stri } bool isReplace = ((flags & GRD_DOC_REPLACE) == GRD_DOC_REPLACE); std::shared_ptr context = std::make_shared(); + context->isIdExist = isIdExist; context->collectionName = collection; context->filter = filter; context->ifShowId = true; @@ -355,11 +356,11 @@ int GetUpsertRePlaceData(ResultSet &resultSet, std::string &targetDocument, Json return errCode; } -int InsertIdToDocument(JsonObject &filterObj, JsonObject &documentObj, bool &isIdExist, std::string &targetDocument, - std::string &docId) +int InsertIdToDocument(JsonObject &filterObj, JsonObject &documentObj, std::string &targetDocument, std::string &docId) { auto filterObjChild = filterObj.GetChild(); int errCode = E_OK; + bool isIdExist; ValueObject idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID, isIdExist); if (isIdExist) { docId = idValue.GetStringValue(); @@ -388,15 +389,13 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json Collection coll = Collection(context->collectionName, executor_); int count = 0; std::string docId; - bool isIdExist; ResultSet resultSet; - std::string targetDocument; + g std::string targetDocument; std::string newStr; - errCode = InsertIdToDocument(filterObj, documentObj, isIdExist, targetDocument, docId); + errCode = InsertIdToDocument(filterObj, documentObj, targetDocument, docId); if (errCode != E_OK) { return errCode; } - context->isIdExist = isIdExist; errCode = InitResultSet(context, this, resultSet, false); if (errCode != E_OK) { goto END; @@ -411,7 +410,7 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json if (errCode != E_OK) { goto END; } - errCode = coll.UpsertDocument(docId, newStr, isIdExist, isReplace); + errCode = coll.UpsertDocument(docId, newStr, context->isIdExist, isReplace); if (errCode == E_OK) { count++; } else if (errCode == -E_NOT_FOUND) { @@ -477,6 +476,7 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri context->filter = filter; context->collectionName = collection; context->ifShowId = true; + context->isIdExist = isIdExist; bool isReplace = ((flags & GRD_DOC_REPLACE) == GRD_DOC_REPLACE); return UpsertDataIntoDB(context, filterObj, document, documentObj, isReplace); } @@ -612,6 +612,7 @@ int DocumentStore::DeleteDocument(const std::string &collection, const std::stri return errCode; } std::shared_ptr context = std::make_shared(); + context->isIdExist = isIdExist; context->filter = filter; context->collectionName = collection; return DeleteDataFromDB(context, filterObj); @@ -774,20 +775,18 @@ END: int DocumentStore::FindDocument(const std::string &collection, const std::string &filter, const std::string &projection, uint32_t flags, GRD_ResultSet *grdResultSet) { - std::shared_ptr context = std::make_shared(); int errCode = E_OK; errCode = FindArgsCheck(collection, filter, projection, flags); if (errCode != E_OK) { GLOGE("delete arg is illegal"); return errCode; } - context->collectionName = collection; - context->filter = filter; JsonObject filterObj = JsonObject::Parse(filter, errCode, true, true); if (errCode != E_OK) { GLOGE("filter Parsed failed"); return errCode; } + std::shared_ptr context = std::make_shared(); errCode = FindProjectionInit(projection, context); if (errCode != E_OK) { return errCode; @@ -804,6 +803,9 @@ int DocumentStore::FindDocument(const std::string &collection, const std::string } else { context->ifShowId = false; } + context->collectionName = collection; + context->filter = filter; + context->isIdExist = isIdExist; return InitFindResultSet(grdResultSet, context); } 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 9d052080..f2647e3f 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 @@ -44,6 +44,27 @@ int ResultSet::Init(std::shared_ptr &context, DocumentStore *store return E_OK; } +int ResultSet::GetValueFromDB(Key &key, JsonObject &filterObj, std::string &jsonKey, std::string &jsonData) +{ + std::pair value; + Collection coll = store_->GetCollection(context_->collectionName); + filterObj.DeleteItemFromObject("_id"); + int errCode = coll.GetMatchedDocument(filterObj, key, value, context_->isIdExist); + if (errCode == -E_NOT_FOUND) { + return -E_NO_DATA; + } + jsonData.assign(value.second.begin(), value.second.end()); + jsonKey.assign(value.first.begin(), value.first.end()); + lastKeyIndex_ = jsonKey; + if (isCutBranch_) { + errCode = CutJsonBranch(jsonKey, jsonData); + if (errCode != E_OK) { + GLOGE("cut branch faild"); + } + } + return errCode; +} + int ResultSet::GetNextWithField() { int errCode = E_OK; @@ -53,36 +74,29 @@ int ResultSet::GetNextWithField() return errCode; } Key key; - if (context_->isIdExist) { // get id from filter or from previous data. - JsonObject filterObjChild = filterObj.GetChild(); - ValueObject idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID); - std::string idKey = idValue.GetStringValue(); - DocKey docKey; - DocumentKey::GetStringDocKey(idKey, docKey); - key.assign(docKey.key.begin(), docKey.key.end()); + if (context_->isIdExist) { + if (index_ == 0) { // get id from filter, if alreay has got id once, get from lastKeyIndex. + JsonObject filterObjChild = filterObj.GetChild(); + ValueObject idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID); + std::string idKey = idValue.GetStringValue(); + DocKey docKey; + DocumentKey::GetStringDocKey(idKey, docKey); + key.assign(docKey.key.begin(), docKey.key.end()); + } else { // Use id to find data that can only get one data. + matchData_.first.clear(); // Delete previous data. + matchData_.second.clear(); + return -E_NO_DATA; + } } else { key.assign(lastKeyIndex_.begin(), lastKeyIndex_.end()); } - matchData_.first.clear(); // Delete previous data. + matchData_.first.clear(); matchData_.second.clear(); - std::pair value; - Collection coll = store_->GetCollection(context_->collectionName); - errCode = coll.GetMatchedDocument(filterObj, key, value, context_->isIdExist); - if (errCode == -E_NOT_FOUND) { - return -E_NO_DATA; - } - std::string jsonData(value.second.begin(), value.second.end()); - std::string jsonkey(value.first.begin(), value.first.end()); - lastKeyIndex_ = jsonkey; - if (isCutBranch_) { - errCode = CutJsonBranch(jsonkey, jsonData); - if (errCode != E_OK) { - GLOGE("cut branch faild"); - return errCode; - } - } - matchData_ = std::make_pair(jsonkey, jsonData); - return E_OK; + std::string jsonKey; + std::string jsonData; + errCode = GetValueFromDB(key, filterObj, jsonKey, jsonData); + matchData_ = std::make_pair(jsonKey, jsonData); + return errCode; } int ResultSet::GetNextInner(bool isNeedCheckTable) @@ -102,6 +116,7 @@ int ResultSet::GetNextInner(bool isNeedCheckTable) } } errCode = GetNextWithField(); + index_++; if (errCode != E_OK) { return errCode; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/kv_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/kv_store_manager.cpp index d038491f..8deb1bab 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/kv_store_manager.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/kv_store_manager.cpp @@ -63,7 +63,7 @@ int KvStoreManager::GetKvStore(const std::string &path, const DBConfig &config, goto END; } } - + executor = sqliteExecutor; return E_OK; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp index c8d21319..dc88f22f 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -201,7 +201,6 @@ int SqliteStoreExecutorImpl::GetDataByFilter(const std::string &collName, const Value valueResult; bool isFindMatch = false; int innerErrorCode = -E_NOT_FOUND; - std::string test(key.begin(), key.end()); std::string sql = GeneralInsertSql(collName, key, isIdExist); std::string keyStr(key.begin(), key.end()); int errCode = SQLiteUtils::ExecSql( -- Gitee From f7f52c66b5c3f360ed826d66f8618b27f41fee73 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 25 May 2023 18:09:01 +0800 Subject: [PATCH 09/32] fix buid Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/interface/src/document_store.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index bbbf4415..ef7b25de 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -390,7 +390,7 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json int count = 0; std::string docId; ResultSet resultSet; - g std::string targetDocument; + std::string targetDocument; std::string newStr; errCode = InsertIdToDocument(filterObj, documentObj, targetDocument, docId); if (errCode != E_OK) { -- Gitee From b754dc8b82854a9ed7170cba74a410a9694ff889 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Sat, 27 May 2023 18:40:52 +0800 Subject: [PATCH 10/32] fix bug Signed-off-by: Jeremyzz --- .../src/interface/src/document_store.cpp | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index ef7b25de..1b8c8bef 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -324,13 +324,11 @@ int CheckUpsertConflict(ResultSet &resultSet, JsonObject &filterObj, std::string return errCode; } -int GetUpsertRePlaceData(ResultSet &resultSet, std::string &targetDocument, JsonObject &documentObj, bool isReplace, - std::string &valStr) +int GetUpsertRePlaceData(ResultSet &resultSet, JsonObject &documentObj, bool isReplace, std::string &valStr) { - resultSet.GetNext(); int errCode = resultSet.GetValue(valStr); if (errCode != E_OK || isReplace) { - valStr = targetDocument; // If cant not find data, insert it. + valStr = documentObj.Print(); // If cant not find data, insert it. return E_OK; } if (errCode != E_OK && errCode != -E_NOT_FOUND) { @@ -356,22 +354,26 @@ int GetUpsertRePlaceData(ResultSet &resultSet, std::string &targetDocument, Json return errCode; } -int InsertIdToDocument(JsonObject &filterObj, JsonObject &documentObj, std::string &targetDocument, std::string &docId) +int InsertIdToDocument(ResultSet &resultSet, JsonObject &filterObj, JsonObject &documentObj, std::string &docId) { auto filterObjChild = filterObj.GetChild(); - int errCode = E_OK; bool isIdExist; ValueObject idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID, isIdExist); + int errCode = E_OK; + int ret = resultSet.GetNext(false, true); // All anomalies will be judged later if (isIdExist) { docId = idValue.GetStringValue(); JsonObject idObj = filterObj.GetObjectItem(KEY_ID, errCode); documentObj.InsertItemObject(0, idObj); } else { - DocKey docKey; - DocumentKey::GetOidDocKey(docKey); - docId = docKey.key; + if (ret == E_OK) { // E_OK means find data. + (void)resultSet.GetValue(docId); // This errCode will always be E_OK. + } else { + DocKey docKey; + DocumentKey::GetOidDocKey(docKey); + docId = docKey.key; + } } - targetDocument = documentObj.Print(); return errCode; } @@ -390,23 +392,22 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json int count = 0; std::string docId; ResultSet resultSet; - std::string targetDocument; std::string newStr; - errCode = InsertIdToDocument(filterObj, documentObj, targetDocument, docId); - if (errCode != E_OK) { - return errCode; - } errCode = InitResultSet(context, this, resultSet, false); if (errCode != E_OK) { goto END; } + errCode = InsertIdToDocument(resultSet, filterObj, documentObj, docId); + if (errCode != E_OK) { + return errCode; + } errCode = CheckUpsertConflict(resultSet, filterObj, docId, coll); // There are only three return values, the two other situation can continue to move forward. if (errCode == -E_DATA_CONFLICT) { GLOGE("upsert data conflict"); goto END; } - errCode = GetUpsertRePlaceData(resultSet, targetDocument, documentObj, isReplace, newStr); + errCode = GetUpsertRePlaceData(resultSet, documentObj, isReplace, newStr); if (errCode != E_OK) { goto END; } -- Gitee From 7d62de8adbba0020ad17911a62af0e604968ff9f Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Sat, 27 May 2023 19:13:06 +0800 Subject: [PATCH 11/32] fix bug Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/interface/src/document_store.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index 1b8c8bef..42e6a9e0 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -309,13 +309,17 @@ int UpsertArgsCheck(const std::string &collection, const std::string &filter, co int CheckUpsertConflict(ResultSet &resultSet, JsonObject &filterObj, std::string &docId, Collection &coll) { - int errCode = resultSet.GetNext(false, true); + std::string val; // use to know whether there is data in the resultSet or not. + int errCode = resultSet.GetValue(val); bool isfilterMatch = false; if (errCode == E_OK) { isfilterMatch = true; } Value ValueDocument; - Key key(docId.begin(), docId.end()); + DocKey docKey; + std::string keyStr = docId; + DocumentKey::GetStringDocKey(keyStr, docKey); + Key key(docKey.key.begin(), docKey.key.end()); errCode = coll.GetDocumentByKey(key, ValueDocument); if (errCode == E_OK && !(isfilterMatch)) { GLOGE("id exist but filter does not match, data conflict"); @@ -367,7 +371,7 @@ int InsertIdToDocument(ResultSet &resultSet, JsonObject &filterObj, JsonObject & documentObj.InsertItemObject(0, idObj); } else { if (ret == E_OK) { // E_OK means find data. - (void)resultSet.GetValue(docId); // This errCode will always be E_OK. + (void)resultSet.GetKey(docId); // This errCode will always be E_OK. } else { DocKey docKey; DocumentKey::GetOidDocKey(docKey); -- Gitee From c3bfc135bba61b5ae6b0bd605c8ae49a36189cfb Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Sat, 27 May 2023 23:07:04 +0800 Subject: [PATCH 12/32] fix bug Signed-off-by: Jeremyzz --- .../src/interface/include/collection.h | 6 +++--- .../gaussdb_rd/src/interface/src/collection.cpp | 8 ++++---- .../gaussdb_rd/src/interface/src/result_set.cpp | 1 + .../src/oh_adapter/include/kv_store_executor.h | 10 +++++----- .../src/sqlite_store_executor_impl.cpp | 17 +++++++++++------ .../oh_adapter/src/sqlite_store_executor_impl.h | 10 +++++----- 6 files changed, 29 insertions(+), 23 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h index 53f38213..7c7cfcb5 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h @@ -30,10 +30,10 @@ public: ~Collection(); int InsertDocument(const std::string &id, const std::string &document, bool &isIdExist); - int GetDocumentByKey(const Key &key, Value &document) const; - int GetMatchedDocument(const JsonObject &filterObj, const Key &key, std::pair &values, + int GetDocumentByKey(Key &key, Value &document) const; + int GetMatchedDocument(const JsonObject &filterObj, Key &key, std::pair &values, int isIdExist) const; - int DeleteDocument(const Key &key); + int DeleteDocument(Key &key); int IsCollectionExists(int &errCode); int UpsertDocument(const std::string &id, const std::string &newStr, bool &isIdExist, bool isReplace = true); bool FindDocument(); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index 8bbc7246..2879a04c 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -86,7 +86,7 @@ bool Collection::FindDocument() return executor_->IsCollectionExists(name_, errCode); } -int Collection::GetDocumentByKey(const Key &key, Value &document) const +int Collection::GetDocumentByKey(Key &key, Value &document) const { if (executor_ == nullptr) { return -E_INNER_ERROR; @@ -94,8 +94,8 @@ int Collection::GetDocumentByKey(const Key &key, Value &document) const return executor_->GetDataByKey(name_, key, document); } -int Collection::GetMatchedDocument(const JsonObject &filterObj, const Key &key, - std::pair &values, int isIdExist) const +int Collection::GetMatchedDocument(const JsonObject &filterObj, Key &key, std::pair &values, + int isIdExist) const { if (executor_ == nullptr) { return -E_INNER_ERROR; @@ -103,7 +103,7 @@ int Collection::GetMatchedDocument(const JsonObject &filterObj, const Key &key, return executor_->GetDataByFilter(name_, key, filterObj, values, isIdExist); } -int Collection::DeleteDocument(const Key &key) +int Collection::DeleteDocument(Key &key) { if (executor_ == nullptr) { return -E_INNER_ERROR; 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 f2647e3f..c35ff9a2 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 @@ -55,6 +55,7 @@ int ResultSet::GetValueFromDB(Key &key, JsonObject &filterObj, std::string &json } jsonData.assign(value.second.begin(), value.second.end()); jsonKey.assign(value.first.begin(), value.first.end()); + GLOGE("jsonKey is =========>%s", jsonKey.c_str()); lastKeyIndex_ = jsonKey; if (isCutBranch_) { errCode = CutJsonBranch(jsonKey, jsonData); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/kv_store_executor.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/kv_store_executor.h index a701cddf..cee70c17 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/kv_store_executor.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/kv_store_executor.h @@ -29,12 +29,12 @@ public: virtual int Commit() = 0; virtual int Rollback() = 0; - virtual int PutData(const std::string &collName, const Key &key, const Value &value) = 0; - virtual int InsertData(const std::string &collName, const Key &key, const Value &value) = 0; - virtual int GetDataByKey(const std::string &collName, const Key &key, Value &value) const = 0; - virtual int GetDataByFilter(const std::string &collName, const Key &key, const JsonObject &filterObj, + virtual int PutData(const std::string &collName, Key &key, const Value &value) = 0; + virtual int InsertData(const std::string &collName, Key &key, const Value &value) = 0; + virtual int GetDataByKey(const std::string &collName, Key &key, Value &value) const = 0; + virtual int GetDataByFilter(const std::string &collName, Key &key, const JsonObject &filterObj, std::pair &values, int isIdExist) const = 0; - virtual int DelData(const std::string &collName, const Key &key) = 0; + virtual int DelData(const std::string &collName, Key &key) = 0; virtual int CreateCollection(const std::string &name, const std::string &option, bool ignoreExists) = 0; virtual int DropCollection(const std::string &name, bool ignoreNonExists) = 0; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp index dc88f22f..8140e1d2 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -102,11 +102,12 @@ int SqliteStoreExecutorImpl::Rollback() return SQLiteUtils::RollbackTransaction(dbHandle_); } -int SqliteStoreExecutorImpl::PutData(const std::string &collName, const Key &key, const Value &value) +int SqliteStoreExecutorImpl::PutData(const std::string &collName, Key &key, const Value &value) { if (dbHandle_ == nullptr) { return -E_ERROR; } + key.push_back(uint8_t(2)); std::string sql = "INSERT OR REPLACE INTO '" + collName + "' VALUES (?,?);"; int errCode = SQLiteUtils::ExecSql( dbHandle_, sql, @@ -127,11 +128,12 @@ int SqliteStoreExecutorImpl::PutData(const std::string &collName, const Key &key return E_OK; } -int SqliteStoreExecutorImpl::InsertData(const std::string &collName, const Key &key, const Value &value) +int SqliteStoreExecutorImpl::InsertData(const std::string &collName, Key &key, const Value &value) { if (dbHandle_ == nullptr) { return -E_ERROR; } + key.push_back(uint8_t(2)); std::string sql = "INSERT INTO '" + collName + "' VALUES (?,?);"; int errCode = SQLiteUtils::ExecSql( dbHandle_, sql, @@ -152,12 +154,13 @@ int SqliteStoreExecutorImpl::InsertData(const std::string &collName, const Key & return E_OK; } -int SqliteStoreExecutorImpl::GetDataByKey(const std::string &collName, const Key &key, Value &value) const +int SqliteStoreExecutorImpl::GetDataByKey(const std::string &collName, Key &key, Value &value) const { if (dbHandle_ == nullptr) { GLOGE("Invalid db handle."); return -E_ERROR; } + key.push_back(uint8_t(2)); int innerErrorCode = -E_NOT_FOUND; std::string sql = "SELECT value FROM '" + collName + "' WHERE key=?;"; int errCode = SQLiteUtils::ExecSql( @@ -178,7 +181,7 @@ int SqliteStoreExecutorImpl::GetDataByKey(const std::string &collName, const Key return innerErrorCode; } -std::string GeneralInsertSql(const std::string &collName, const Key &key, int isIdExist) +std::string GeneralInsertSql(const std::string &collName, Key &key, int isIdExist) { std::string sqlEqual = "SELECT key, value FROM '" + collName + "' WHERE key=?;"; std::string sqlOrder = "SELECT key, value FROM '" + collName + "'ORDER BY KEY;"; @@ -190,13 +193,14 @@ std::string GeneralInsertSql(const std::string &collName, const Key &key, int is } } -int SqliteStoreExecutorImpl::GetDataByFilter(const std::string &collName, const Key &key, const JsonObject &filterObj, +int SqliteStoreExecutorImpl::GetDataByFilter(const std::string &collName, Key &key, const JsonObject &filterObj, std::pair &values, int isIdExist) const { if (dbHandle_ == nullptr) { GLOGE("Invalid db handle."); return -E_ERROR; } + key.push_back(uint8_t(2)); Value keyResult; Value valueResult; bool isFindMatch = false; @@ -242,12 +246,13 @@ int SqliteStoreExecutorImpl::GetDataByFilter(const std::string &collName, const return innerErrorCode; } -int SqliteStoreExecutorImpl::DelData(const std::string &collName, const Key &key) +int SqliteStoreExecutorImpl::DelData(const std::string &collName, Key &key) { if (dbHandle_ == nullptr) { GLOGE("Invalid db handle."); return -E_ERROR; } + key.push_back(uint8_t(2)); int errCode = 0; Value valueRet; if (GetDataByKey(collName, key, valueRet) != E_OK) { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.h index aa392294..ad93fdfd 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.h @@ -37,12 +37,12 @@ public: int Commit() override; int Rollback() override; - int PutData(const std::string &collName, const Key &key, const Value &value) override; - int InsertData(const std::string &collName, const Key &key, const Value &value) override; - int GetDataByKey(const std::string &collName, const Key &key, Value &value) const override; - int GetDataByFilter(const std::string &collName, const Key &key, const JsonObject &filterObj, + int PutData(const std::string &collName, Key &key, const Value &value) override; + int InsertData(const std::string &collName, Key &key, const Value &value) override; + int GetDataByKey(const std::string &collName, Key &key, Value &value) const override; + int GetDataByFilter(const std::string &collName, Key &key, const JsonObject &filterObj, std::pair &values, int isIdExist) const override; - int DelData(const std::string &collName, const Key &key) override; + int DelData(const std::string &collName, Key &key) override; int CreateCollection(const std::string &name, const std::string &option, bool ignoreExists) override; int DropCollection(const std::string &name, bool ignoreNonExists) override; -- Gitee From de5266bcc77c814ac780efe9bb61a0850864ce24 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 10:14:18 +0800 Subject: [PATCH 13/32] fixfuc Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/interface/src/document_key.cpp | 7 +------ .../data_share/gaussdb_rd/src/interface/src/result_set.cpp | 1 - .../gaussdb_rd/src/oh_adapter/src/sqlite_utils.cpp | 6 ++++-- .../gaussdb_rd/test/unittest/api/documentdb_api_test.cpp | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp index e99582f1..1e6482d0 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp @@ -59,8 +59,7 @@ int DocumentKey::GetStringDocKey(const std::string &id, DocKey &key) } key.id = id; key.type = (uint8_t)STRING; - int ret = SerializeDocKey(key, key.id); - return ret; + return E_OK; } int DocumentKey::GetOidDocKey(DocKey &key) @@ -71,15 +70,11 @@ int DocumentKey::GetOidDocKey(DocKey &key) return ret; } } - ret = SerializeDocKey(key, key.id); return ret; } std::string DocumentKey::GetIdFromKey(std::string &keyStr) { - if (!keyStr.empty()) { - keyStr.pop_back(); - } return keyStr; } } // namespace DocumentDB \ No newline at end of file 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 c31d6c7c..d52ae797 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 @@ -257,7 +257,6 @@ int ResultSet::CutJsonBranch(std::string &jsonKey, std::string &jsonData) isInsertIdflag = true; // ifShowId is true,and then the data taken out does not have IDs, insert id. } if (context_->viewType) { - std::vector> allCutPath; std::vector singlePath; JsonObject cjsonObjChild = cjsonObj.GetChild(); std::vector> allCutPath; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_utils.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_utils.cpp index 6ac7e59d..de37d418 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_utils.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_utils.cpp @@ -295,8 +295,10 @@ int SQLiteUtils::ExecSql(sqlite3 *db, const std::string &sql, const std::functio } else if (errCode != SQLITE_ROW) { goto END; // Step return error } - if (resultCallback != nullptr && ((errCode = resultCallback(stmt, isMatchOneData)) != E_OK) || - isMatchOneData) { // find one data, stop stepping. + if (resultCallback != nullptr) { // find one data, stop stepping. + errCode = resultCallback(stmt, isMatchOneData); + } + if (resultCallback != nullptr && ((errCode != E_OK) || isMatchOneData)) { goto END; } } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp index 22fe15ee..06a74ff3 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp @@ -383,7 +383,7 @@ int GetDBPageSize(const std::string &path) } int pageSize = 0; - SQLiteUtils::ExecSql(db, "PRAGMA page_size;", nullptr, [&pageSize](sqlite3_stmt *stmt) { + SQLiteUtils::ExecSql(db, "PRAGMA page_size;", nullptr, [&pageSize](sqlite3_stmt *stmt, bool &isMatchOneData) { pageSize = sqlite3_column_int(stmt, 0); return E_OK; }); -- Gitee From e88501e3a71cbd9672e011f8a9629bb1d2a619c9 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 10:20:10 +0800 Subject: [PATCH 14/32] fix Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp index d2b40f2b..9506bcfb 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -228,6 +228,7 @@ int SqliteStoreExecutorImpl::GetDataByFilter(const std::string &collName, Key &k } if (JsonCommon::IsJsonNodeMatch(srcObj, filterObj, innerErrorCode)) { isFindMatch = true; // this args work in this function + keyStr.pop_back() // get id from really key. values.first = keyStr; values.second = valueStr; innerErrorCode = E_OK; -- Gitee From 846c4cabce494fd96d4661feb9fb9c0713efba79 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 11:10:07 +0800 Subject: [PATCH 15/32] Modify the process Signed-off-by: Jeremyzz --- .../src/interface/src/document_key.cpp | 6 +++--- .../src/sqlite_store_executor_impl.cpp | 17 +++++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp index 1e6482d0..2d328919 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp @@ -46,9 +46,7 @@ static int InitDocIdFromOid(DocKey &docKey) static int SerializeDocKey(DocKey &key, const std::string &id) { - std::string idStr = id; - key.key = idStr; - key.key = key.key + std::to_string(key.type); // Question here + key.key = id; return E_OK; } @@ -59,6 +57,7 @@ int DocumentKey::GetStringDocKey(const std::string &id, DocKey &key) } key.id = id; key.type = (uint8_t)STRING; + (void)SerializeDocKey(key, key.id); return E_OK; } @@ -70,6 +69,7 @@ int DocumentKey::GetOidDocKey(DocKey &key) return ret; } } + (void)SerializeDocKey(key, key.id); return ret; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp index 9506bcfb..67d32460 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -160,7 +160,6 @@ int SqliteStoreExecutorImpl::GetDataByKey(const std::string &collName, Key &key, GLOGE("Invalid db handle."); return -E_ERROR; } - key.push_back(uint8_t(2)); int innerErrorCode = -E_NOT_FOUND; std::string sql = "SELECT value FROM '" + collName + "' WHERE key=?;"; int errCode = SQLiteUtils::ExecSql( @@ -193,6 +192,16 @@ std::string GeneralInsertSql(const std::string &collName, Key &key, int isIdExis } } +void AssignValueToData(std::string &keyStr, std::string &valueStr, std::pair &values, + int &innerErrorCode, bool &isMatchOneData) +{ + keyStr.pop_back(); // get id from really key. + values.first = keyStr; + values.second = valueStr; + innerErrorCode = E_OK; + isMatchOneData = true; // this args work in ExecSql fuction +} + int SqliteStoreExecutorImpl::GetDataByFilter(const std::string &collName, Key &key, const JsonObject &filterObj, std::pair &values, int isIdExist) const { @@ -228,11 +237,7 @@ int SqliteStoreExecutorImpl::GetDataByFilter(const std::string &collName, Key &k } if (JsonCommon::IsJsonNodeMatch(srcObj, filterObj, innerErrorCode)) { isFindMatch = true; // this args work in this function - keyStr.pop_back() // get id from really key. - values.first = keyStr; - values.second = valueStr; - innerErrorCode = E_OK; - isMatchOneData = true; // this args work in ExecSql fuction + (void)AssignValueToData(keyStr, valueStr,values, innerErrorCode, isMatchOneData); return E_OK; // match count; } innerErrorCode = E_OK; -- Gitee From ed78d2107b886404c9df0321479edbb7929ec43b Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 15:11:48 +0800 Subject: [PATCH 16/32] fix bug Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/interface/include/document_key.h | 8 +++----- .../gaussdb_rd/src/interface/src/document_store.cpp | 1 - .../gaussdb_rd/src/interface/src/result_set.cpp | 3 +-- .../src/oh_adapter/src/sqlite_store_executor_impl.cpp | 11 ++++++----- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h index 8aa157c0..95d4591b 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h @@ -28,13 +28,12 @@ #define GRD_DOC_ID_TYPE_SIZE 1 namespace DocumentDB { - -typedef enum DocIdType { +enum class DocIdType { INT = 1, STRING, -} DocIdType; +}; -class DocKey { +struct DocKey { public: ~DocKey(){}; int32_t keySize; @@ -49,6 +48,5 @@ public: static int GetStringDocKey(const std::string &id, DocKey &key); static std::string GetIdFromKey(std::string &keyStr); }; - } // namespace DocumentDB #endif // DOCUMENT_KEY_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index 42e6a9e0..6742a734 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -514,7 +514,6 @@ int DocumentStore::InsertDataIntoDB(const std::string &collection, const std::st JsonObject documentObjChild = documentObj.GetChild(); ValueObject idValue = JsonCommon::GetValueInSameLevel(documentObjChild, KEY_ID); id = idValue.GetStringValue(); - } else { DocKey docKey; DocumentKey::GetOidDocKey(docKey); 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 d52ae797..f8cbaf10 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 @@ -218,8 +218,7 @@ int ResultSet::CheckCutNode(JsonObject *node, std::vector singlePat JsonObject CreatIdObj(const std::string &idStr, int errCode) { std::stringstream sstream; - sstream << "{\"_id\":" - << "\"" << idStr << "\"}"; + sstream << "{\"_id\":" << "\"" << idStr << "\"}"; JsonObject idObj = JsonObject::Parse(sstream.str(), errCode, true); // cant be faild. idObj = idObj.GetChild(); return idObj; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp index 67d32460..21730bc1 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -22,6 +22,7 @@ #include "sqlite_utils.h" namespace DocumentDB { +constexpr const uint8_t KEY_TYPE = uint8_t(2); int SqliteStoreExecutorImpl::CreateDatabase(const std::string &path, const DBConfig &config, sqlite3 *&db) { if (db != nullptr) { @@ -107,7 +108,7 @@ int SqliteStoreExecutorImpl::PutData(const std::string &collName, Key &key, cons if (dbHandle_ == nullptr) { return -E_ERROR; } - key.push_back(uint8_t(2)); + key.push_back(KEY_TYPE); // Stitching ID type std::string sql = "INSERT OR REPLACE INTO '" + collName + "' VALUES (?,?);"; int errCode = SQLiteUtils::ExecSql( dbHandle_, sql, @@ -133,7 +134,7 @@ int SqliteStoreExecutorImpl::InsertData(const std::string &collName, Key &key, c if (dbHandle_ == nullptr) { return -E_ERROR; } - key.push_back(uint8_t(2)); + key.push_back(KEY_TYPE); std::string sql = "INSERT INTO '" + collName + "' VALUES (?,?);"; int errCode = SQLiteUtils::ExecSql( dbHandle_, sql, @@ -209,7 +210,7 @@ int SqliteStoreExecutorImpl::GetDataByFilter(const std::string &collName, Key &k GLOGE("Invalid db handle."); return -E_ERROR; } - key.push_back(uint8_t(2)); + key.push_back(KEY_TYPE); Value keyResult; Value valueResult; bool isFindMatch = false; @@ -237,7 +238,7 @@ int SqliteStoreExecutorImpl::GetDataByFilter(const std::string &collName, Key &k } if (JsonCommon::IsJsonNodeMatch(srcObj, filterObj, innerErrorCode)) { isFindMatch = true; // this args work in this function - (void)AssignValueToData(keyStr, valueStr,values, innerErrorCode, isMatchOneData); + (void)AssignValueToData(keyStr, valueStr, values, innerErrorCode, isMatchOneData); return E_OK; // match count; } innerErrorCode = E_OK; @@ -259,7 +260,7 @@ int SqliteStoreExecutorImpl::DelData(const std::string &collName, Key &key) GLOGE("Invalid db handle."); return -E_ERROR; } - key.push_back(uint8_t(2)); + key.push_back(KEY_TYPE); int errCode = 0; Value valueRet; if (GetDataByKey(collName, key, valueRet) != E_OK) { -- Gitee From 1e84efa4f80009f8fc3a0936aebcc9834626f122 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 15:25:57 +0800 Subject: [PATCH 17/32] fix alarm Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/common/src/json_common.cpp | 5 +++-- .../src/interface/include/document_key.h | 1 - .../src/interface/include/projection_tree.h | 3 +-- .../gaussdb_rd/src/interface/src/document_key.cpp | 14 ++------------ .../src/interface/src/document_store.cpp | 2 ++ .../src/interface/src/projection_tree.cpp | 2 +- .../gaussdb_rd/src/interface/src/result_set.cpp | 3 +-- .../oh_adapter/documentdb_json_common_test.cpp | 14 -------------- .../oh_adapter/documentdb_key_document_test.cpp | 11 ----------- 9 files changed, 10 insertions(+), 45 deletions(-) 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 cf103af1..8391997e 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 @@ -340,7 +340,7 @@ bool AddSpliteHitField(const JsonObject &src, const JsonObject &item, JsonFieldP return true; } - for (int i = abandonPath.size() - 1; i > -1; i--) { + for (int32_t i = (int32_t)abandonPath.size() - 1; i > -1; i--) { if (hitItem.GetType() != JsonObject::Type::JSON_OBJECT) { GLOGE("Add collapse item to object failed, path not exist."); externErrCode = -E_DATA_CONFLICT; @@ -382,7 +382,7 @@ bool AddSpliteField(const JsonObject &src, const JsonObject &item, const JsonFie return false; } JsonFieldPath newHitPath; - for (int i = abandonPath.size() - 1; i > -1; i--) { + for (int32_t i = (int32_t)abandonPath.size() - 1; i > -1; i--) { if (hitItem.GetType() != JsonObject::Type::JSON_OBJECT) { GLOGE("Add collapse item to object failed, path not exist."); externErrCode = -E_DATA_CONFLICT; @@ -719,6 +719,7 @@ bool JsonCommon::IsJsonNodeMatch(const JsonObject &src, const JsonObject &target if (isAlreadyMatched == 0) { // Not match anything isMatchFlag = false; } + // Source path not exist, if leaf value is null, isMatchFlag become true, else it will become false. return false; } }); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h index 95d4591b..dcef8f10 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h @@ -37,7 +37,6 @@ struct DocKey { public: ~DocKey(){}; int32_t keySize; - std::string id; std::string key; uint8_t type; }; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/projection_tree.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/projection_tree.h index f7c7b095..3cdca06d 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/projection_tree.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/projection_tree.h @@ -28,7 +28,6 @@ struct ProjectionNode { std::unordered_map sonNode; bool isDeepest; int Deep; - int ViewType; ProjectionNode() { Deep = 0; @@ -43,7 +42,7 @@ struct ProjectionNode { class ProjectionTree { public: int ParseTree(std::vector> &path); - bool SearchTree(std::vector &singlePath, int &index); + bool SearchTree(std::vector &singlePath, size_t &index); private: ProjectionNode node_; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp index 2d328919..76ceded1 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp @@ -38,15 +38,8 @@ static int InitDocIdFromOid(DocKey &docKey) GLOGE("get oid error"); return -E_INNER_ERROR; } - docKey.id = idTemp; + docKey.key = idTemp; delete[] idTemp; - docKey.type = (uint8_t)STRING; - return E_OK; -} - -static int SerializeDocKey(DocKey &key, const std::string &id) -{ - key.key = id; return E_OK; } @@ -55,9 +48,7 @@ int DocumentKey::GetStringDocKey(const std::string &id, DocKey &key) if (id.empty()) { return GetOidDocKey(key); // It won't go to this branch at the moment. } - key.id = id; - key.type = (uint8_t)STRING; - (void)SerializeDocKey(key, key.id); + key.key = id; return E_OK; } @@ -69,7 +60,6 @@ int DocumentKey::GetOidDocKey(DocKey &key) return ret; } } - (void)SerializeDocKey(key, key.id); return ret; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index 6742a734..11446999 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -28,6 +28,7 @@ namespace DocumentDB { constexpr int JSON_LENS_MAX = 1024 * 1024; constexpr const char *KEY_ID = "_id"; +constexpr const uint8_t KEY_TYPE = uint8_t(2); DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) {} @@ -320,6 +321,7 @@ int CheckUpsertConflict(ResultSet &resultSet, JsonObject &filterObj, std::string std::string keyStr = docId; DocumentKey::GetStringDocKey(keyStr, docKey); Key key(docKey.key.begin(), docKey.key.end()); + key.push_back(KEY_TYPE); // add id type flag; errCode = coll.GetDocumentByKey(key, ValueDocument); if (errCode == E_OK && !(isfilterMatch)) { GLOGE("id exist but filter does not match, data conflict"); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/projection_tree.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/projection_tree.cpp index 94c56b6f..5c37b84b 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/projection_tree.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/projection_tree.cpp @@ -54,7 +54,7 @@ int ProjectionTree::ParseTree(std::vector> &path) return E_OK; } -bool ProjectionTree::SearchTree(std::vector &singlePath, int &index) +bool ProjectionTree::SearchTree(std::vector &singlePath, size_t &index) { ProjectionNode *node = &node_; for (size_t i = 0; i < singlePath.size(); i++) { 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 f8cbaf10..03fcf76f 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 @@ -55,7 +55,6 @@ int ResultSet::GetValueFromDB(Key &key, JsonObject &filterObj, std::string &json } jsonData.assign(value.second.begin(), value.second.end()); jsonKey.assign(value.first.begin(), value.first.end()); - GLOGE("jsonKey is =========>%s", jsonKey.c_str()); lastKeyIndex_ = jsonKey; if (isCutBranch_) { errCode = CutJsonBranch(jsonKey, jsonData); @@ -199,7 +198,7 @@ int ResultSet::CheckCutNode(JsonObject *node, std::vector singlePat return -E_NO_DATA; } singlePath.emplace_back(node->GetItemField()); - int index = 0; + size_t index = 0; if (!context_->projectionTree.SearchTree(singlePath, index) && index == 0) { allCutPath.emplace_back(singlePath); } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp index bec7c70a..264a4e9a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp @@ -610,18 +610,4 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest023, TestSize.Leve EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj2, filterObj1, errCode), false); } -HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest024, 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 fieldName : singlePath) { - GLOGE("fieldName is =========>%s", fieldName.c_str()); - } - GLOGE("///////////////////////////"); - } -} } // namespace \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_key_document_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_key_document_test.cpp index 9e53f90a..7121f15c 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_key_document_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_key_document_test.cpp @@ -42,15 +42,4 @@ void DocumentDBKeyCommonTest::SetUp(void) {} void DocumentDBKeyCommonTest::TearDown(void) {} -HWTEST_F(DocumentDBKeyCommonTest, KeyCommonTest001, TestSize.Level0) -{ - DocKey docKey; - for (int i = 0; i < 65537; i++) { - DocumentKey::GetOidDocKey(docKey); - if (i > 65530) { - GLOGE("docKey key is=============>%s", docKey.key.c_str()); - } - } -} - } // namespace \ No newline at end of file -- Gitee From a91f271849ea84735385912aee7bc18e029ea37e Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 15:32:13 +0800 Subject: [PATCH 18/32] fix thing Signed-off-by: Jeremyzz --- .../src/oh_adapter/src/sqlite_store_executor_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp index 21730bc1..c04b6748 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -210,12 +210,12 @@ int SqliteStoreExecutorImpl::GetDataByFilter(const std::string &collName, Key &k GLOGE("Invalid db handle."); return -E_ERROR; } - key.push_back(KEY_TYPE); Value keyResult; Value valueResult; bool isFindMatch = false; int innerErrorCode = -E_NOT_FOUND; std::string sql = GeneralInsertSql(collName, key, isIdExist); + key.push_back(KEY_TYPE); std::string keyStr(key.begin(), key.end()); int errCode = SQLiteUtils::ExecSql( dbHandle_, sql, -- Gitee From b9965c112bb69bbb47e71e666d1bf2da7572baba Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 15:42:51 +0800 Subject: [PATCH 19/32] fix code check opinion Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/interface/include/document_key.h | 2 -- .../data_share/gaussdb_rd/src/interface/src/document_key.cpp | 4 ---- .../data_share/gaussdb_rd/src/interface/src/result_set.cpp | 5 ++--- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h index dcef8f10..718f0def 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h @@ -35,7 +35,6 @@ enum class DocIdType { struct DocKey { public: - ~DocKey(){}; int32_t keySize; std::string key; uint8_t type; @@ -45,7 +44,6 @@ class DocumentKey { public: static int GetOidDocKey(DocKey &key); static int GetStringDocKey(const std::string &id, DocKey &key); - static std::string GetIdFromKey(std::string &keyStr); }; } // namespace DocumentDB #endif // DOCUMENT_KEY_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp index 76ceded1..c84d3a17 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp @@ -63,8 +63,4 @@ int DocumentKey::GetOidDocKey(DocKey &key) return ret; } -std::string DocumentKey::GetIdFromKey(std::string &keyStr) -{ - return keyStr; -} } // namespace DocumentDB \ No newline at end of file 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 03fcf76f..e48eb589 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 @@ -225,13 +225,12 @@ JsonObject CreatIdObj(const std::string &idStr, int errCode) int InsertRandomId(JsonObject &cjsonObj, std::string &jsonKey) { - std::string idStr = DocumentKey::GetIdFromKey(jsonKey); - if (idStr.empty()) { + if (jsonKey.empty()) { GLOGE("Genalral Id faild"); return -E_INNER_ERROR; } int errCode = E_OK; - JsonObject idObj = CreatIdObj(idStr, errCode); + JsonObject idObj = CreatIdObj(jsonKey, errCode); if (errCode != E_OK) { GLOGE("CreatIdObj faild"); return errCode; -- Gitee From 65a4ddebf8ff6cc33b08ecd4c212004f2d19dc9c Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 15:56:18 +0800 Subject: [PATCH 20/32] fix code check opnion Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/interface/include/collection.h | 4 ++-- .../gaussdb_rd/src/interface/src/collection.cpp | 10 ++++------ .../gaussdb_rd/src/interface/src/document_store.cpp | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h index 7c7cfcb5..c2bb5a35 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h @@ -35,9 +35,9 @@ public: int isIdExist) const; int DeleteDocument(Key &key); int IsCollectionExists(int &errCode); - int UpsertDocument(const std::string &id, const std::string &newStr, bool &isIdExist, bool isReplace = true); + int UpsertDocument(const std::string &id, const std::string &newStr, bool &isIdExist); bool FindDocument(); - int UpdateDocument(const std::string &id, const std::string &document, bool isReplace = false); + int UpdateDocument(const std::string &id, const std::string &document); private: std::string name_; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index 2879a04c..c3429f1d 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -63,16 +63,14 @@ int Collection::InsertDocument(const std::string &id, const std::string &documen if (!isIdExist) { key.assign(id.begin(), id.end()); errCode = executor_->InsertData(name_, key, valSet); - while (errCode != E_OK) { + while (errCode == -E_DATA_CONFLICT) { // if id alreay exist, create new one. DocumentKey::GetOidDocKey(docKey); key.assign(docKey.key.begin(), docKey.key.end()); errCode = executor_->InsertData(name_, key, valSet); } return errCode; } else { - DocKey docKey; - DocumentKey::GetStringDocKey(id, docKey); - key.assign(docKey.key.begin(), docKey.key.end()); + key.assign(id.begin(), id.end()); } return executor_->InsertData(name_, key, valSet); } @@ -124,7 +122,7 @@ int Collection::IsCollectionExists(int &errCode) return executor_->IsCollectionExists(name_, errCode); } -int Collection::UpsertDocument(const std::string &id, const std::string &newStr, bool &isIdExist, bool isReplace) +int Collection::UpsertDocument(const std::string &id, const std::string &newStr, bool &isIdExist) { if (executor_ == nullptr) { return -E_INNER_ERROR; @@ -159,7 +157,7 @@ int Collection::UpsertDocument(const std::string &id, const std::string &newStr, return executor_->PutData(name_, key, valSet); } -int Collection::UpdateDocument(const std::string &id, const std::string &newStr, bool isReplace) +int Collection::UpdateDocument(const std::string &id, const std::string &newStr) { if (executor_ == nullptr) { return -E_INNER_ERROR; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index 11446999..2dd78d89 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -246,7 +246,7 @@ int DocumentStore::UpdateDataIntoDB(std::shared_ptr &context, Json if (errCode != E_OK) { goto END; } - errCode = coll.UpdateDocument(docId, valStr, isReplace); + errCode = coll.UpdateDocument(docId, valStr); if (errCode == E_OK) { count++; } else if (errCode == -E_NOT_FOUND) { @@ -417,7 +417,7 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json if (errCode != E_OK) { goto END; } - errCode = coll.UpsertDocument(docId, newStr, context->isIdExist, isReplace); + errCode = coll.UpsertDocument(docId, newStr, context->isIdExist); if (errCode == E_OK) { count++; } else if (errCode == -E_NOT_FOUND) { -- Gitee From 2ed631d3f6030052a7da171116e5912779b23e27 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 16:08:31 +0800 Subject: [PATCH 21/32] fix code check opinion Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/interface/src/collection.cpp | 4 +--- .../data_share/gaussdb_rd/src/interface/src/result_set.cpp | 2 +- .../gaussdb_rd/src/interface/src/result_set_common.cpp | 4 ++-- .../gaussdb_rd/src/oh_adapter/src/kv_store_manager.cpp | 1 - 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index c3429f1d..d49f6d21 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -150,9 +150,7 @@ int Collection::UpsertDocument(const std::string &id, const std::string &newStr, } return errCode; } else { - DocKey docKey; - DocumentKey::GetStringDocKey(id, docKey); - key.assign(docKey.key.begin(), docKey.key.end()); + key.assign(id.begin(), id.end()); } return executor_->PutData(name_, key, valSet); } 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 e48eb589..fdf711e6 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 @@ -48,7 +48,7 @@ int ResultSet::GetValueFromDB(Key &key, JsonObject &filterObj, std::string &json { std::pair value; Collection coll = store_->GetCollection(context_->collectionName); - filterObj.DeleteItemFromObject("_id"); + filterObj.DeleteItemFromObject(KEY_ID); int errCode = coll.GetMatchedDocument(filterObj, key, value, context_->isIdExist); if (errCode == -E_NOT_FOUND) { return -E_NO_DATA; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/result_set_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/result_set_common.cpp index 8a142704..dd9acaeb 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/result_set_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/result_set_common.cpp @@ -14,7 +14,7 @@ */ #include "result_set_common.h" - +#include "grd_format_config.h" #include "doc_errno.h" #include "grd_base/grd_error.h" @@ -24,7 +24,7 @@ int InitResultSet(std::shared_ptr &context, DocumentStore *store, { if (isCutBranch) { for (const auto &singlePath : context->projectionPath) { - if (singlePath[0] == "_id" && context->viewType == true) { // projection has Id and viewType is true + if (singlePath[0] == KEY_ID && context->viewType == true) { // projection has Id and viewType is true context->ifShowId = true; break; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/kv_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/kv_store_manager.cpp index 8deb1bab..79cbcc86 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/kv_store_manager.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/kv_store_manager.cpp @@ -63,7 +63,6 @@ int KvStoreManager::GetKvStore(const std::string &path, const DBConfig &config, goto END; } } - executor = sqliteExecutor; return E_OK; -- Gitee From 8c448e56af04ecef3a8a5fa8ca4b0c8413ababb9 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 16:14:12 +0800 Subject: [PATCH 22/32] fix code check opnion Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/interface/src/result_set.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 fdf711e6..cceaa0b8 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 @@ -219,8 +219,7 @@ JsonObject CreatIdObj(const std::string &idStr, int errCode) std::stringstream sstream; sstream << "{\"_id\":" << "\"" << idStr << "\"}"; JsonObject idObj = JsonObject::Parse(sstream.str(), errCode, true); // cant be faild. - idObj = idObj.GetChild(); - return idObj; + return idObj.GetChild(); } int InsertRandomId(JsonObject &cjsonObj, std::string &jsonKey) -- Gitee From 41423f0aa8b60a13eadacc3172902246c7149c32 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 16:37:25 +0800 Subject: [PATCH 23/32] fix code check opinion Signed-off-by: Jeremyzz --- .../src/interface/include/collection.h | 2 +- .../src/interface/src/collection.cpp | 8 ++++---- .../src/interface/src/document_store.cpp | 6 +++--- .../src/interface/src/result_set.cpp | 19 ++++++++++--------- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h index c2bb5a35..3ab6afa2 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h @@ -35,7 +35,7 @@ public: int isIdExist) const; int DeleteDocument(Key &key); int IsCollectionExists(int &errCode); - int UpsertDocument(const std::string &id, const std::string &newStr, bool &isIdExist); + int UpsertDocument(const std::string &id, const std::string &newDocument, bool &isIdExist); bool FindDocument(); int UpdateDocument(const std::string &id, const std::string &document); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index d49f6d21..634ffb4f 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -122,7 +122,7 @@ int Collection::IsCollectionExists(int &errCode) return executor_->IsCollectionExists(name_, errCode); } -int Collection::UpsertDocument(const std::string &id, const std::string &newStr, bool &isIdExist) +int Collection::UpsertDocument(const std::string &id, const std::string &newDocument, bool &isIdExist) { if (executor_ == nullptr) { return -E_INNER_ERROR; @@ -138,7 +138,7 @@ int Collection::UpsertDocument(const std::string &id, const std::string &newStr, return -E_INVALID_ARGS; } Key key; - Value valSet(newStr.begin(), newStr.end()); + Value valSet(newDocument.begin(), newDocument.end()); DocKey docKey; if (!isIdExist) { key.assign(id.begin(), id.end()); @@ -155,7 +155,7 @@ int Collection::UpsertDocument(const std::string &id, const std::string &newStr, return executor_->PutData(name_, key, valSet); } -int Collection::UpdateDocument(const std::string &id, const std::string &newStr) +int Collection::UpdateDocument(const std::string &id, const std::string &newDocument) { if (executor_ == nullptr) { return -E_INNER_ERROR; @@ -171,7 +171,7 @@ int Collection::UpdateDocument(const std::string &id, const std::string &newStr) return -E_INVALID_ARGS; } Key keyId(id.begin(), id.end()); - Value valSet(newStr.begin(), newStr.end()); + Value valSet(newDocument.begin(), newDocument.end()); return executor_->PutData(name_, keyId, valSet); } } // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index 2dd78d89..becb52ba 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -398,7 +398,7 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json int count = 0; std::string docId; ResultSet resultSet; - std::string newStr; + std::string newDocument; errCode = InitResultSet(context, this, resultSet, false); if (errCode != E_OK) { goto END; @@ -413,11 +413,11 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json GLOGE("upsert data conflict"); goto END; } - errCode = GetUpsertRePlaceData(resultSet, documentObj, isReplace, newStr); + errCode = GetUpsertRePlaceData(resultSet, documentObj, isReplace, newDocument); if (errCode != E_OK) { goto END; } - errCode = coll.UpsertDocument(docId, newStr, context->isIdExist); + errCode = coll.UpsertDocument(docId, newDocument, context->isIdExist); if (errCode == E_OK) { count++; } else if (errCode == -E_NOT_FOUND) { 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 cceaa0b8..fee81593 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 @@ -217,12 +217,13 @@ int ResultSet::CheckCutNode(JsonObject *node, std::vector singlePat JsonObject CreatIdObj(const std::string &idStr, int errCode) { std::stringstream sstream; - sstream << "{\"_id\":" << "\"" << idStr << "\"}"; + sstream << "{\"_id\":" + << "\"" << idStr << "\"}"; JsonObject idObj = JsonObject::Parse(sstream.str(), errCode, true); // cant be faild. - return idObj.GetChild(); + return idObj; } -int InsertRandomId(JsonObject &cjsonObj, std::string &jsonKey) +int InsertId(JsonObject &cjsonObj, std::string &jsonKey) { if (jsonKey.empty()) { GLOGE("Genalral Id faild"); @@ -234,7 +235,7 @@ int InsertRandomId(JsonObject &cjsonObj, std::string &jsonKey) GLOGE("CreatIdObj faild"); return errCode; } - cjsonObj.InsertItemObject(0, idObj); + cjsonObj.InsertItemObject(0, idObj.GetChild()); // idObj's child is _id node return E_OK; } @@ -261,17 +262,17 @@ int ResultSet::CutJsonBranch(std::string &jsonKey, std::string &jsonData) GLOGE("The node in CheckCutNode is nullptr"); return errCode; } - for (const auto &singleCutPaht : allCutPath) { - if (!context_->ifShowId || singleCutPaht[0] != KEY_ID) { - cjsonObj.DeleteItemDeeplyOnTarget(singleCutPaht); + for (const auto &singleCutPath : allCutPath) { + if (!context_->ifShowId || singleCutPath[0] != KEY_ID) { + cjsonObj.DeleteItemDeeplyOnTarget(singleCutPath); } - if (singleCutPaht[0] == KEY_ID && !isIdExistInValue) { // projection has Id, and its showType is true. + if (singleCutPath[0] == KEY_ID && !isIdExistInValue) { // projection has Id, and its showType is true. isInsertIdflag = true; } } } if (isInsertIdflag) { - errCode = InsertRandomId(cjsonObj, jsonKey); + errCode = InsertId(cjsonObj, jsonKey); if (errCode != E_OK) { return errCode; } -- Gitee From 352a53a8f4a1f70fea4a4d27d42c6358ff47c579 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 18:50:05 +0800 Subject: [PATCH 24/32] fix code check opnion Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/interface/include/document_key.h | 1 - .../gaussdb_rd/src/interface/src/document_key.cpp | 9 --------- .../gaussdb_rd/src/interface/src/document_store.cpp | 5 +---- .../gaussdb_rd/src/interface/src/result_set.cpp | 4 +--- 4 files changed, 2 insertions(+), 17 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h index 718f0def..32dbd79e 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_key.h @@ -43,7 +43,6 @@ public: class DocumentKey { public: static int GetOidDocKey(DocKey &key); - static int GetStringDocKey(const std::string &id, DocKey &key); }; } // namespace DocumentDB #endif // DOCUMENT_KEY_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp index c84d3a17..5c75bf1a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp @@ -43,15 +43,6 @@ static int InitDocIdFromOid(DocKey &docKey) return E_OK; } -int DocumentKey::GetStringDocKey(const std::string &id, DocKey &key) -{ - if (id.empty()) { - return GetOidDocKey(key); // It won't go to this branch at the moment. - } - key.key = id; - return E_OK; -} - int DocumentKey::GetOidDocKey(DocKey &key) { int ret = InitDocIdFromOid(key); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index becb52ba..a92dc4b0 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -317,10 +317,7 @@ int CheckUpsertConflict(ResultSet &resultSet, JsonObject &filterObj, std::string isfilterMatch = true; } Value ValueDocument; - DocKey docKey; - std::string keyStr = docId; - DocumentKey::GetStringDocKey(keyStr, docKey); - Key key(docKey.key.begin(), docKey.key.end()); + Key key(docId.begin(), docId.end()); key.push_back(KEY_TYPE); // add id type flag; errCode = coll.GetDocumentByKey(key, ValueDocument); if (errCode == E_OK && !(isfilterMatch)) { 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 fee81593..a1b73353 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 @@ -79,9 +79,7 @@ int ResultSet::GetNextWithField() JsonObject filterObjChild = filterObj.GetChild(); ValueObject idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID); std::string idKey = idValue.GetStringValue(); - DocKey docKey; - DocumentKey::GetStringDocKey(idKey, docKey); - key.assign(docKey.key.begin(), docKey.key.end()); + key.assign(idKey.begin(), idKey.end()); } else { // Use id to find data that can only get one data. matchData_.first.clear(); // Delete previous data. matchData_.second.clear(); -- Gitee From 98614bc822ee5b499ae68a9db335a0bcce474a3b Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 19:20:24 +0800 Subject: [PATCH 25/32] fix code check opinion Signed-off-by: Jeremyzz --- .../src/interface/include/collection.h | 2 +- .../src/interface/src/collection.cpp | 4 +-- .../src/interface/src/document_store.cpp | 6 ++-- .../oh_adapter/include/kv_store_executor.h | 1 + .../src/sqlite_store_executor_impl.cpp | 30 ++++++++++++++++++- 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h index 3ab6afa2..3e45f16f 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h @@ -30,7 +30,7 @@ public: ~Collection(); int InsertDocument(const std::string &id, const std::string &document, bool &isIdExist); - int GetDocumentByKey(Key &key, Value &document) const; + int GetDocumentById(Key &key, Value &document) const; int GetMatchedDocument(const JsonObject &filterObj, Key &key, std::pair &values, int isIdExist) const; int DeleteDocument(Key &key); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index 634ffb4f..52c6aa1d 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -84,12 +84,12 @@ bool Collection::FindDocument() return executor_->IsCollectionExists(name_, errCode); } -int Collection::GetDocumentByKey(Key &key, Value &document) const +int Collection::GetDocumentById(Key &key, Value &document) const { if (executor_ == nullptr) { return -E_INNER_ERROR; } - return executor_->GetDataByKey(name_, key, document); + return executor_->GetDataById(name_, key, document); } int Collection::GetMatchedDocument(const JsonObject &filterObj, Key &key, std::pair &values, diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index a92dc4b0..559ce0ac 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -28,7 +28,6 @@ namespace DocumentDB { constexpr int JSON_LENS_MAX = 1024 * 1024; constexpr const char *KEY_ID = "_id"; -constexpr const uint8_t KEY_TYPE = uint8_t(2); DocumentStore::DocumentStore(KvStoreExecutor *executor) : executor_(executor) {} @@ -317,9 +316,8 @@ int CheckUpsertConflict(ResultSet &resultSet, JsonObject &filterObj, std::string isfilterMatch = true; } Value ValueDocument; - Key key(docId.begin(), docId.end()); - key.push_back(KEY_TYPE); // add id type flag; - errCode = coll.GetDocumentByKey(key, ValueDocument); + Key id(docId.begin(), docId.end()); + errCode = coll.GetDocumentById(id, ValueDocument); if (errCode == E_OK && !(isfilterMatch)) { GLOGE("id exist but filter does not match, data conflict"); errCode = -E_DATA_CONFLICT; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/kv_store_executor.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/kv_store_executor.h index cee70c17..c32cf826 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/kv_store_executor.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/kv_store_executor.h @@ -32,6 +32,7 @@ public: virtual int PutData(const std::string &collName, Key &key, const Value &value) = 0; virtual int InsertData(const std::string &collName, Key &key, const Value &value) = 0; virtual int GetDataByKey(const std::string &collName, Key &key, Value &value) const = 0; + virtual int GetDataById(const std::string &collName, Key &key, Value &value) const = 0; virtual int GetDataByFilter(const std::string &collName, Key &key, const JsonObject &filterObj, std::pair &values, int isIdExist) const = 0; virtual int DelData(const std::string &collName, Key &key) = 0; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp index c04b6748..a765b813 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -18,11 +18,12 @@ #include "check_common.h" #include "db_constant.h" #include "doc_errno.h" +#include "document_key.h" #include "log_print.h" #include "sqlite_utils.h" namespace DocumentDB { -constexpr const uint8_t KEY_TYPE = uint8_t(2); +constexpr const uint8_t KEY_TYPE = uint8_t(DocIdType::STRING); int SqliteStoreExecutorImpl::CreateDatabase(const std::string &path, const DBConfig &config, sqlite3 *&db) { if (db != nullptr) { @@ -181,6 +182,33 @@ int SqliteStoreExecutorImpl::GetDataByKey(const std::string &collName, Key &key, return innerErrorCode; } +int SqliteStoreExecutorImpl::GetDataById(const std::string &collName, Key &key, Value &value) const +{ + if (dbHandle_ == nullptr) { + GLOGE("Invalid db handle."); + return -E_ERROR; + } + key.push_back(KEY_TYPE); // Stitching ID type + int innerErrorCode = -E_NOT_FOUND; + std::string sql = "SELECT value FROM '" + collName + "' WHERE key=?;"; + int errCode = SQLiteUtils::ExecSql( + dbHandle_, sql, + [key](sqlite3_stmt *stmt) { + SQLiteUtils::BindBlobToStatement(stmt, 1, key); + return E_OK; + }, + [&value, &innerErrorCode](sqlite3_stmt *stmt, bool &isMatchOneData) { + SQLiteUtils::GetColumnBlobValue(stmt, 0, value); + innerErrorCode = E_OK; + return E_OK; + }); + if (errCode != E_OK) { + GLOGE("[sqlite executor] Get data failed. err=%d", errCode); + return errCode; + } + return innerErrorCode; +} + std::string GeneralInsertSql(const std::string &collName, Key &key, int isIdExist) { std::string sqlEqual = "SELECT key, value FROM '" + collName + "' WHERE key=?;"; -- Gitee From 70f435cb4ee4b868295d1ad43decb60119d2903f Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 19:23:42 +0800 Subject: [PATCH 26/32] add thing Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.h | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.h index ad93fdfd..3d8fbd99 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.h @@ -40,6 +40,7 @@ public: int PutData(const std::string &collName, Key &key, const Value &value) override; int InsertData(const std::string &collName, Key &key, const Value &value) override; int GetDataByKey(const std::string &collName, Key &key, Value &value) const override; + int GetDataById(const std::string &collName, Key &key, Value &value) const override; int GetDataByFilter(const std::string &collName, Key &key, const JsonObject &filterObj, std::pair &values, int isIdExist) const override; int DelData(const std::string &collName, Key &key) override; -- Gitee From f8517829ced2b8fb05e9c0774c8c102cd086ad7e Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 19:35:38 +0800 Subject: [PATCH 27/32] fix code check Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/interface/src/document_key.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp index 5c75bf1a..a54ca478 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_key.cpp @@ -39,7 +39,7 @@ static int InitDocIdFromOid(DocKey &docKey) return -E_INNER_ERROR; } docKey.key = idTemp; - delete[] idTemp; + delete idTemp; return E_OK; } -- Gitee From 40661aaa98e613669538c192b110f419e112dd05 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 19:59:25 +0800 Subject: [PATCH 28/32] fix code check Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/interface/src/collection.cpp | 14 +++++++------- .../src/interface/src/document_store.cpp | 13 +++++++------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index 52c6aa1d..99b93f91 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -59,8 +59,8 @@ int Collection::InsertDocument(const std::string &id, const std::string &documen } Key key; Value valSet(document.begin(), document.end()); - DocKey docKey; if (!isIdExist) { + DocKey docKey; key.assign(id.begin(), id.end()); errCode = executor_->InsertData(name_, key, valSet); while (errCode == -E_DATA_CONFLICT) { // if id alreay exist, create new one. @@ -122,7 +122,7 @@ int Collection::IsCollectionExists(int &errCode) return executor_->IsCollectionExists(name_, errCode); } -int Collection::UpsertDocument(const std::string &id, const std::string &newDocument, bool &isIdExist) +int Collection::UpsertDocument(const std::string &id, const std::string &newDocument, bool &isDataExist) { if (executor_ == nullptr) { return -E_INNER_ERROR; @@ -139,14 +139,14 @@ int Collection::UpsertDocument(const std::string &id, const std::string &newDocu } Key key; Value valSet(newDocument.begin(), newDocument.end()); - DocKey docKey; - if (!isIdExist) { + if (!isDataExist) { + DocKey docKey; key.assign(id.begin(), id.end()); - errCode = executor_->PutData(name_, key, valSet); - while (errCode != E_OK) { + errCode = executor_->InsertData(name_, key, valSet); + while (errCode == -E_DATA_CONFLICT) { DocumentKey::GetOidDocKey(docKey); key.assign(docKey.key.begin(), docKey.key.end()); - errCode = executor_->PutData(name_, key, valSet); + errCode = executor_->InsertData(name_, key, valSet); } return errCode; } else { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index 559ce0ac..4bb0e031 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -307,18 +307,18 @@ int UpsertArgsCheck(const std::string &collection, const std::string &filter, co return errCode; } -int CheckUpsertConflict(ResultSet &resultSet, JsonObject &filterObj, std::string &docId, Collection &coll) +int CheckUpsertConflict(ResultSet &resultSet, JsonObject &filterObj, std::string &docId, Collection &coll, + bool &isDataExist) { std::string val; // use to know whether there is data in the resultSet or not. int errCode = resultSet.GetValue(val); - bool isfilterMatch = false; if (errCode == E_OK) { - isfilterMatch = true; + isDataExist = true; } Value ValueDocument; Key id(docId.begin(), docId.end()); errCode = coll.GetDocumentById(id, ValueDocument); - if (errCode == E_OK && !(isfilterMatch)) { + if (errCode == E_OK && !(isDataExist)) { GLOGE("id exist but filter does not match, data conflict"); errCode = -E_DATA_CONFLICT; } @@ -394,6 +394,7 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json std::string docId; ResultSet resultSet; std::string newDocument; + bool isDataExist = false; errCode = InitResultSet(context, this, resultSet, false); if (errCode != E_OK) { goto END; @@ -402,7 +403,7 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json if (errCode != E_OK) { return errCode; } - errCode = CheckUpsertConflict(resultSet, filterObj, docId, coll); + errCode = CheckUpsertConflict(resultSet, filterObj, docId, coll, isDataExist); // There are only three return values, the two other situation can continue to move forward. if (errCode == -E_DATA_CONFLICT) { GLOGE("upsert data conflict"); @@ -412,7 +413,7 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json if (errCode != E_OK) { goto END; } - errCode = coll.UpsertDocument(docId, newDocument, context->isIdExist); + errCode = coll.UpsertDocument(docId, newDocument, isDataExist); if (errCode == E_OK) { count++; } else if (errCode == -E_NOT_FOUND) { -- Gitee From 4dcbb6326ebe92e939c98125ec4631c57dca7aef Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 20:09:15 +0800 Subject: [PATCH 29/32] fix code check opinion Signed-off-by: Jeremyzz --- .../src/interface/include/collection.h | 1 + .../src/interface/src/collection.cpp | 40 +++++++++---------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h index 3e45f16f..1e5f9ece 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/collection.h @@ -40,6 +40,7 @@ public: int UpdateDocument(const std::string &id, const std::string &document); private: + int InsertUntilSuccess(Key &key, const std::string &id, Value &valSet); std::string name_; KvStoreExecutor *executor_ = nullptr; }; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index 99b93f91..ed0c81a8 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -44,6 +44,18 @@ Collection::~Collection() executor_ = nullptr; } +int Collection::InsertUntilSuccess(Key &key, const std::string &id, Value &valSet) +{ + DocKey docKey; + key.assign(id.begin(), id.end()); + int errCode = executor_->InsertData(name_, key, valSet); + while (errCode == -E_DATA_CONFLICT) { // if id alreay exist, create new one. + DocumentKey::GetOidDocKey(docKey); + key.assign(docKey.key.begin(), docKey.key.end()); + errCode = executor_->InsertData(name_, key, valSet); + } + return errCode; +} int Collection::InsertDocument(const std::string &id, const std::string &document, bool &isIdExist) { if (executor_ == nullptr) { @@ -60,18 +72,12 @@ int Collection::InsertDocument(const std::string &id, const std::string &documen Key key; Value valSet(document.begin(), document.end()); if (!isIdExist) { - DocKey docKey; - key.assign(id.begin(), id.end()); - errCode = executor_->InsertData(name_, key, valSet); - while (errCode == -E_DATA_CONFLICT) { // if id alreay exist, create new one. - DocumentKey::GetOidDocKey(docKey); - key.assign(docKey.key.begin(), docKey.key.end()); - errCode = executor_->InsertData(name_, key, valSet); + errCode = InsertUntilSuccess(key, id, valSet); + if (errCode != E_OK) { + return errCode; } - return errCode; - } else { - key.assign(id.begin(), id.end()); } + key.assign(id.begin(), id.end()); return executor_->InsertData(name_, key, valSet); } @@ -140,18 +146,12 @@ int Collection::UpsertDocument(const std::string &id, const std::string &newDocu Key key; Value valSet(newDocument.begin(), newDocument.end()); if (!isDataExist) { - DocKey docKey; - key.assign(id.begin(), id.end()); - errCode = executor_->InsertData(name_, key, valSet); - while (errCode == -E_DATA_CONFLICT) { - DocumentKey::GetOidDocKey(docKey); - key.assign(docKey.key.begin(), docKey.key.end()); - errCode = executor_->InsertData(name_, key, valSet); + errCode = InsertUntilSuccess(key, id, valSet); + if (errCode != E_OK) { + return errCode; } - return errCode; - } else { - key.assign(id.begin(), id.end()); } + key.assign(id.begin(), id.end()); return executor_->PutData(name_, key, valSet); } -- Gitee From b82daa304b702d9393ecf1af856b38f03b377b79 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 20:36:08 +0800 Subject: [PATCH 30/32] fix code check Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/interface/src/collection.cpp | 10 ++-------- .../test/unittest/api/documentdb_api_test.cpp | 2 +- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index ed0c81a8..47842e9c 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -72,10 +72,7 @@ int Collection::InsertDocument(const std::string &id, const std::string &documen Key key; Value valSet(document.begin(), document.end()); if (!isIdExist) { - errCode = InsertUntilSuccess(key, id, valSet); - if (errCode != E_OK) { - return errCode; - } + return InsertUntilSuccess(key, id, valSet); } key.assign(id.begin(), id.end()); return executor_->InsertData(name_, key, valSet); @@ -146,10 +143,7 @@ int Collection::UpsertDocument(const std::string &id, const std::string &newDocu Key key; Value valSet(newDocument.begin(), newDocument.end()); if (!isDataExist) { - errCode = InsertUntilSuccess(key, id, valSet); - if (errCode != E_OK) { - return errCode; - } + return InsertUntilSuccess(key, id, valSet); } key.assign(id.begin(), id.end()); return executor_->PutData(name_, key, valSet); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp index 06a74ff3..d4518ff6 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_api_test.cpp @@ -381,7 +381,7 @@ int GetDBPageSize(const std::string &path) if (db == nullptr) { return 0; } - + int pageSize = 0; SQLiteUtils::ExecSql(db, "PRAGMA page_size;", nullptr, [&pageSize](sqlite3_stmt *stmt, bool &isMatchOneData) { pageSize = sqlite3_column_int(stmt, 0); -- Gitee From 13295b939270f0e0d4dd07808d6ee4e94be4802b Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 21:01:15 +0800 Subject: [PATCH 31/32] fix code check Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/interface/src/document_store.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp index 4bb0e031..5d9f8a14 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store.cpp @@ -362,6 +362,9 @@ int InsertIdToDocument(ResultSet &resultSet, JsonObject &filterObj, JsonObject & ValueObject idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID, isIdExist); int errCode = E_OK; int ret = resultSet.GetNext(false, true); // All anomalies will be judged later + if (ret != E_OK && ret != -E_NO_DATA) { + return ret; + } if (isIdExist) { docId = idValue.GetStringValue(); JsonObject idObj = filterObj.GetObjectItem(KEY_ID, errCode); -- Gitee From e18477773d655003f8b293e923f9e37f01f6d0a3 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 21:19:44 +0800 Subject: [PATCH 32/32] fix code check Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/common/src/json_common.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 8391997e..6157cd09 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 @@ -340,7 +340,7 @@ bool AddSpliteHitField(const JsonObject &src, const JsonObject &item, JsonFieldP return true; } - for (int32_t i = (int32_t)abandonPath.size() - 1; i > -1; i--) { + for (int32_t i = static_cast(abandonPath.size()) - 1; i > -1; i--) { if (hitItem.GetType() != JsonObject::Type::JSON_OBJECT) { GLOGE("Add collapse item to object failed, path not exist."); externErrCode = -E_DATA_CONFLICT; @@ -382,7 +382,7 @@ bool AddSpliteField(const JsonObject &src, const JsonObject &item, const JsonFie return false; } JsonFieldPath newHitPath; - for (int32_t i = (int32_t)abandonPath.size() - 1; i > -1; i--) { + for (int32_t i = static_cast(abandonPath.size()) - 1; i > -1; i--) { if (hitItem.GetType() != JsonObject::Type::JSON_OBJECT) { GLOGE("Add collapse item to object failed, path not exist."); externErrCode = -E_DATA_CONFLICT; -- Gitee