From a85ed5219eed6c6c9b6f82de4288042b250093f3 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Sat, 20 May 2023 17:53:55 +0800 Subject: [PATCH 01/13] Query data flow modification Signed-off-by: Jeremyzz --- .../src/common/include/document_type.h | 2 +- .../src/executor/document/check_common.cpp | 13 +- .../src/executor/document/check_common.h | 2 +- .../src/interface/include/collection.h | 9 +- .../src/interface/include/document_store.h | 6 +- .../src/interface/include/result_set.h | 3 +- .../src/interface/src/collection.cpp | 85 +------ .../src/interface/src/document_store.cpp | 214 ++++++++++++------ .../src/interface/src/result_set.cpp | 102 ++++----- .../src/interface/src/result_set_common.cpp | 3 - .../oh_adapter/include/kv_store_executor.h | 6 +- .../src/sqlite_store_executor_impl.cpp | 40 +++- .../src/sqlite_store_executor_impl.h | 6 +- .../src/oh_adapter/src/sqlite_utils.cpp | 2 + 14 files changed, 245 insertions(+), 248 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/document_type.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/document_type.h index ff525ecb..0c767b9a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/document_type.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/document_type.h @@ -28,7 +28,7 @@ struct QueryContext { ProjectionTree projectionTree; bool ifShowId = false; bool viewType = false; - bool isOnlyId = false; + bool isIdExist = false; }; } // namespace DocumentDB #endif // DOCUMENT_TYPE_H \ No newline at end of file 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 01513f9f..1e5fc758 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 @@ -41,7 +41,7 @@ bool CheckCollectionNamePrefix(const std::string &name, const std::string &prefi void ReplaceAll(std::string &inout, const std::string &what, const std::string &with) { - std::string::size_type pos {}; + std::string::size_type pos{}; while ((pos = inout.find(what.data(), pos, what.length())) != std::string::npos) { inout.replace(pos, what.length(), with.data(), with.length()); pos += with.length(); @@ -75,7 +75,7 @@ bool CheckCommon::CheckCollectionName(const std::string &collectionName, std::st return true; } -int CheckCommon::CheckFilter(JsonObject &filterObj, bool &isOnlyId, std::vector> &filterPath) +int CheckCommon::CheckFilter(JsonObject &filterObj, std::vector> &filterPath, bool &isIdExist) { for (size_t i = 0; i < filterPath.size(); i++) { if (filterPath[i].size() > JSON_DEEP_MAX) { @@ -83,9 +83,6 @@ int CheckCommon::CheckFilter(JsonObject &filterObj, bool &isOnlyId, std::vector< return -E_INVALID_ARGS; } } - if (!filterObj.GetChild().GetNext().IsNull()) { // check contained other field at the same level as the id node - isOnlyId = false; - } for (size_t i = 0; i < filterPath.size(); i++) { if (filterPath[i].empty()) { return -E_INVALID_JSON_FORMAT; @@ -104,15 +101,11 @@ int CheckCommon::CheckFilter(JsonObject &filterObj, bool &isOnlyId, std::vector< return -E_INVALID_ARGS; } } - bool isIdExisit = false; - int ret = CheckIdFormat(filterObj, isIdExisit); + int ret = CheckIdFormat(filterObj, isIdExist); if (ret != E_OK) { GLOGE("Filter Id format is illegal"); return ret; } - if (!isIdExisit) { - isOnlyId = false; - } return E_OK; } 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 ec81d50d..18a01c64 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 @@ -29,7 +29,7 @@ public: ~CheckCommon() = default; static bool CheckCollectionName(const std::string &collectionName, std::string &formattedName, int &errCode); - static int CheckFilter(JsonObject &document, bool &isOnlyId, std::vector> &filterPath); + static int CheckFilter(JsonObject &document, std::vector> &filterPath, bool &isIdExist); static int CheckIdFormat(JsonObject &data, bool &isIdExisit); static int CheckDocument(JsonObject &document); static int CheckUpdata(JsonObject &updata, std::vector> &path); 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 c04f5c7b..33158c5e 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 @@ -26,16 +26,17 @@ class Collection { public: Collection(const std::string &name, KvStoreExecutor *executor); Collection(const Collection &other); - Collection() {}; + Collection(){}; ~Collection(); int PutDocument(const Key &key, const Value &document); int InsertDocument(const Key &key, const Value &document); - int GetDocument(const Key &key, Value &document) const; - int GetMatchedDocument(const JsonObject &filterObj, std::vector> &values) const; + 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 &document, bool isReplace = true); + int UpsertDocument(const std::string &id, const std::string &newStr, 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/include/document_store.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/document_store.h index cefdc9e3..3437d8ca 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 @@ -69,13 +69,11 @@ public: private: int UpdateDataIntoDB(std::shared_ptr &context, JsonObject &filterObj, const std::string &update, bool &isReplace); - int UpsertDataIntoDB(std::shared_ptr &context, JsonObject &filterObj, JsonObject &documentObj, - 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 DeleteDataFromDB(std::shared_ptr &context, JsonObject &filterObj); int InitFindResultSet(GRD_ResultSet *grdResultSet, std::shared_ptr &context); - int CheckUpsertConflict(bool &isIdExist, std::shared_ptr &context, JsonObject &filterObj, - std::string &docId, Collection &coll); KvStoreExecutor *executor_ = nullptr; std::map collections_; std::function closeNotifier_; 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 65956e09..c6b9f935 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 @@ -33,6 +33,7 @@ public: int Init(std::shared_ptr &context, DocumentStore *store, bool ifField); int GetNext(bool isNeedTransaction = false, bool isNeedCheckTable = false); int GetValue(char **value); + int GetValue(std::string &value); int GetKey(std::string &key); int EraseCollection(); @@ -47,7 +48,7 @@ private: bool ifField_ = false; size_t index_ = 0; std::shared_ptr context_; - std::vector> matchDatas_; + std::pair matchData_; }; } // namespace DocumentDB #endif // RESULTSET_H \ No newline at end of file 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 1e6d04f2..88887ff6 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 @@ -76,21 +76,21 @@ bool Collection::FindDocument() return executor_->IsCollectionExists(name_, errCode); } -int Collection::GetDocument(const Key &key, Value &document) const +int Collection::GetDocumentByKey(const Key &key, Value &document) const { if (executor_ == nullptr) { return -E_INNER_ERROR; } - return executor_->GetData(name_, key, document); + return executor_->GetDataByKey(name_, key, document); } -int Collection::GetMatchedDocument(const JsonObject &filterObj, - std::vector> &values) const +int Collection::GetMatchedDocument(const JsonObject &filterObj, const Key &key, + std::pair &values, int isIdExist) const { if (executor_ == nullptr) { return -E_INNER_ERROR; } - return executor_->GetFieldedData(name_, filterObj, values); + return executor_->GetDataByFilter(name_, key, filterObj, values, isIdExist); } int Collection::DeleteDocument(const Key &key) @@ -114,7 +114,7 @@ int Collection::IsCollectionExists(int &errCode) return executor_->IsCollectionExists(name_, errCode); } -int Collection::UpsertDocument(const std::string &id, const std::string &document, bool isReplace) +int Collection::UpsertDocument(const std::string &id, const std::string &newStr, bool isReplace) { if (executor_ == nullptr) { return -E_INNER_ERROR; @@ -129,47 +129,12 @@ int Collection::UpsertDocument(const std::string &id, const std::string &documen GLOGE("Collection not created."); return -E_INVALID_ARGS; } - - JsonObject upsertValue = JsonObject::Parse(document, errCode, true); - if (errCode != E_OK) { - GLOGD("Parse upsert value failed. %d", errCode); - return errCode; - } - Key keyId(id.begin(), id.end()); - Value valSet(document.begin(), document.end()); - if (!isReplace) { - Value valueGot; - errCode = executor_->GetData(name_, keyId, valueGot); - std::string valueGotStr = std::string(valueGot.begin(), valueGot.end()); - if (errCode != E_OK && errCode != -E_NOT_FOUND) { - GLOGW("Get original document failed. %d", errCode); - return errCode; - } else if (errCode == E_OK) { // document has been inserted - GLOGD("Document has been inserted, append value."); - JsonObject originValue = JsonObject::Parse(valueGotStr, errCode, true); - if (errCode != E_OK) { - GLOGD("Parse original value failed. %d %s", errCode, valueGotStr.c_str()); - return errCode; - } - - errCode = JsonCommon::Append(originValue, upsertValue, isReplace); - if (errCode != E_OK) { - GLOGD("Append value failed. %d", errCode); - return errCode; - } - std::string valStr = originValue.Print(); - if (valStr.length() >= JSON_LENS_MAX) { - GLOGE("document's length is too long"); - return -E_OVER_LIMIT; - } - valSet = { valStr.begin(), valStr.end() }; - } - } + Value valSet(newStr.begin(), newStr.end()); return executor_->PutData(name_, keyId, valSet); } -int Collection::UpdateDocument(const std::string &id, const std::string &update, bool isReplace) +int Collection::UpdateDocument(const std::string &id, const std::string &newStr, bool isReplace) { if (executor_ == nullptr) { return -E_INNER_ERROR; @@ -184,40 +149,8 @@ int Collection::UpdateDocument(const std::string &id, const std::string &update, GLOGE("Collection not created."); return -E_INVALID_ARGS; } - - JsonObject updateValue = JsonObject::Parse(update, errCode, true); - if (errCode != E_OK) { - GLOGD("Parse upsert value failed. %d", errCode); - return errCode; - } - Key keyId(id.begin(), id.end()); - Value valueGot; - errCode = executor_->GetData(name_, keyId, valueGot); - std::string valueGotStr = std::string(valueGot.begin(), valueGot.end()); - if (errCode == -E_NOT_FOUND) { - GLOGW("Get original document not found."); - return -E_NOT_FOUND; - } else if (errCode != E_OK) { - GLOGE("Get original document failed. %d", errCode); - return errCode; - } - JsonObject originValue = JsonObject::Parse(valueGotStr, errCode, true); - if (errCode != E_OK) { - GLOGD("Parse original value failed. %d %s", errCode, valueGotStr.c_str()); - return errCode; - } - errCode = JsonCommon::Append(originValue, updateValue, isReplace); - if (errCode != E_OK) { - GLOGD("Append value failed. %d", errCode); - return errCode; - } - std::string valStr = originValue.Print(); - if (valStr.length() >= JSON_LENS_MAX) { - GLOGE("document's length is too long"); - return -E_OVER_LIMIT; - } - Value valSet(valStr.begin(), valStr.end()); + Value valSet(newStr.begin(), newStr.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 cb1947eb..42e6c411 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 @@ -43,7 +43,6 @@ int DocumentStore::CreateCollection(const std::string &name, const std::string & GLOGE("Check collection name invalid. %d", errCode); return errCode; } - errCode = E_OK; CollectionOption collOption = CollectionOption::ReadOption(option, errCode); if (errCode != E_OK) { @@ -128,7 +127,7 @@ END: return errCode; } -int TranFilter(JsonObject &filterObj, std::vector> &filterAllPath, bool &isOnlyId) +int TranFilter(JsonObject &filterObj, std::vector> &filterAllPath, bool &isIdExist) { int errCode = E_OK; filterAllPath = JsonCommon::ParsePath(filterObj, errCode); @@ -136,7 +135,7 @@ int TranFilter(JsonObject &filterObj, std::vector> &fil GLOGE("filter ParsePath failed"); return errCode; } - return CheckCommon::CheckFilter(filterObj, isOnlyId, filterAllPath); + return CheckCommon::CheckFilter(filterObj, filterAllPath, isIdExist); } int UpdateArgsCheck(const std::string &collection, const std::string &filter, const std::string &update, uint32_t flags) @@ -176,6 +175,41 @@ int UpdateArgsCheck(const std::string &collection, const std::string &filter, co return errCode; } +int GetUpDataRePlaceData(ResultSet &resultSet, const std::string &id, const std::string &update, std::string &valStr, bool isReplace) +{ + std::string valueGotStr; + int errCode = resultSet.GetValue(valueGotStr); + if (errCode == -E_NO_DATA) { + GLOGW("Get original document not found."); + return -E_NOT_FOUND; + } else if (errCode != E_OK) { + GLOGE("Get original document failed. %d", errCode); + return errCode; + } + JsonObject updateValue = JsonObject::Parse(update, errCode, true); + if (errCode != E_OK) { + GLOGD("Parse upsert value failed. %d", errCode); + return errCode; + } + JsonObject originValue = JsonObject::Parse(valueGotStr, errCode, true); + if (errCode != E_OK) { + GLOGD("Parse original value failed. %d %s", errCode, valueGotStr.c_str()); + return errCode; + } + errCode = JsonCommon::Append(originValue, updateValue, isReplace); + if (errCode != E_OK) { + GLOGD("Append value failed. %d", errCode); + return errCode; + } + valStr = originValue.Print(); + if (valStr.length() >= JSON_LENS_MAX) { + GLOGE("document's length is too long"); + return -E_OVER_LIMIT; + } + return errCode; +} + + int DocumentStore::UpdateDataIntoDB(std::shared_ptr &context, JsonObject &filterObj, const std::string &update, bool &isReplace) { @@ -189,27 +223,25 @@ int DocumentStore::UpdateDataIntoDB(std::shared_ptr &context, Json } std::string docId; int count = 0; + std::string valStr; auto coll = Collection(context->collectionName, executor_); - if (context->isOnlyId) { - auto filterObjChild = filterObj.GetChild(); - auto idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID); - docId = idValue.GetStringValue(); - } else { - ResultSet resultSet; - std::string filter = filterObj.Print(); - InitResultSet(context, this, resultSet, true); - // no start transaction inner - errCode = resultSet.GetNext(false, true); - if (errCode == -E_NO_DATA) { - // no need to set count - errCode = E_OK; - goto END; - } else if (errCode != E_OK) { - goto END; - } - resultSet.GetKey(docId); + ResultSet resultSet; + InitResultSet(context, this, resultSet, true); + // no start transaction inner + errCode = resultSet.GetNext(false, true); + if (errCode == -E_NO_DATA) { + // no need to set count + errCode = E_OK; + goto END; + } else if (errCode != E_OK) { + goto END; } - errCode = coll.UpdateDocument(docId, update, isReplace); + resultSet.GetKey(docId); + errCode = GetUpDataRePlaceData(resultSet, docId, update, valStr, isReplace); + if (errCode != E_OK) { + goto END; + } + errCode = coll.UpdateDocument(docId, valStr, isReplace); if (errCode == E_OK) { count++; } else if (errCode == -E_NOT_FOUND) { @@ -236,17 +268,17 @@ int DocumentStore::UpdateDocument(const std::string &collection, const std::stri GLOGE("filter Parsed failed"); return errCode; } - bool isOnlyId = true; + bool isIdExist = false; std::vector> filterAllPath; - errCode = TranFilter(filterObj, filterAllPath, isOnlyId); + errCode = TranFilter(filterObj, filterAllPath, isIdExist); if (errCode != E_OK) { return errCode; } bool isReplace = ((flags & GRD_DOC_REPLACE) == GRD_DOC_REPLACE); std::shared_ptr context = std::make_shared(); context->collectionName = collection; - context->isOnlyId = isOnlyId; context->filter = filter; + context->ifShowId = true; return UpdateDataIntoDB(context, filterObj, update, isReplace); } @@ -270,11 +302,8 @@ int UpsertArgsCheck(const std::string &collection, const std::string &filter, co return errCode; } -int DocumentStore::CheckUpsertConflict(bool &isIdExist, std::shared_ptr &context, JsonObject &filterObj, - std::string &docId, Collection &coll) +int CheckUpsertConflict(ResultSet &resultSet, JsonObject &filterObj, std::string &docId, Collection &coll) { - ResultSet resultSet; - InitResultSet(context, this, resultSet, true); int errCode = resultSet.GetNext(false, true); bool isfilterMatch = false; if (errCode == E_OK) { @@ -282,7 +311,7 @@ int DocumentStore::CheckUpsertConflict(bool &isIdExist, std::shared_ptr= JSON_LENS_MAX) { + GLOGE("document's length is too long"); + return -E_OVER_LIMIT; + } + } + return errCode; +} + +int InsertIdToDocument(JsonObject &filterObj, JsonObject &documentObj, bool &isIdExist, std::string &targetDocument, + 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; + } + return errCode; +} + int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, JsonObject &filterObj, - JsonObject &documentObj, bool &isReplace) + const std::string &document, JsonObject &documentObj, bool &isReplace) { std::lock_guard lock(dbMutex_); if (executor_ == nullptr) { @@ -303,29 +380,31 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json } Collection coll = Collection(context->collectionName, executor_); int count = 0; - std::string targetDocument; std::string docId; bool isIdExist; - auto filterObjChild = filterObj.GetChild(); - ValueObject idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID, isIdExist); - docId = idValue.GetStringValue(); - JsonObject idObj = filterObj.GetObjectItem(KEY_ID, errCode); - documentObj.InsertItemObject(0, idObj); - targetDocument = documentObj.Print(); - if (!isIdExist) { - errCode = -E_INVALID_ARGS; + ResultSet resultSet; + std::string targetDocument; + std::string newStr; + errCode = InsertIdToDocument(filterObj, documentObj, isIdExist, targetDocument, docId); + if (errCode != E_OK) { + return errCode; + } + context->isIdExist = isIdExist; + errCode = InitResultSet(context, this, resultSet, true); + 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; } - if (!context->isOnlyId) { - errCode = CheckUpsertConflict(isIdExist, context, filterObj, docId, coll); - // There are only three return values, E_ OK and - E_ NO_DATA is a normal scenario, - // and that situation can continue to move forward - if (errCode == -E_DATA_CONFLICT) { - GLOGE("upsert data conflict"); - goto END; - } + errCode = GetUpsertRePlaceData(resultSet, targetDocument, documentObj, isReplace, newStr); + if (errCode != E_OK) { + goto END; } - errCode = coll.UpsertDocument(docId, targetDocument, isReplace); + errCode = coll.UpsertDocument(docId, newStr, isReplace); if (errCode == E_OK) { count++; } else if (errCode == -E_NOT_FOUND) { @@ -357,6 +436,7 @@ int UpsertDocumentFormatCheck(const std::string &document, JsonObject &documentO } return errCode; } + int DocumentStore::UpsertDocument(const std::string &collection, const std::string &filter, const std::string &document, uint32_t flags) { @@ -379,19 +459,19 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri GLOGE("document format is illegal"); return errCode; } - bool isOnlyId = true; + bool isIdExist = false; std::vector> filterAllPath; - errCode = TranFilter(filterObj, filterAllPath, isOnlyId); + errCode = TranFilter(filterObj, filterAllPath, isIdExist); if (errCode != E_OK) { GLOGE("filter is invalid"); return errCode; } std::shared_ptr context = std::make_shared(); context->filter = filter; - context->isOnlyId = isOnlyId; context->collectionName = collection; + context->ifShowId = true; bool isReplace = ((flags & GRD_DOC_REPLACE) == GRD_DOC_REPLACE); - return UpsertDataIntoDB(context, filterObj, documentObj, isReplace); + return UpsertDataIntoDB(context, filterObj, document, documentObj, isReplace); } int InsertArgsCheck(const std::string &collection, const std::string &document, uint32_t flags) @@ -478,19 +558,13 @@ int DocumentStore::DeleteDataFromDB(std::shared_ptr &context, Json return errCode; } std::string id; - if (context->isOnlyId) { - auto filterObjChild = filterObj.GetChild(); - auto idValue = JsonCommon::GetValueInSameLevel(filterObjChild, KEY_ID); - id = idValue.GetStringValue(); - } else { - ResultSet resultSet; - InitResultSet(context, this, resultSet, true); - errCode = resultSet.GetNext(false, true); - if (errCode != E_OK) { - goto END; - } - resultSet.GetKey(id); + ResultSet resultSet; + InitResultSet(context, this, resultSet, true); + errCode = resultSet.GetNext(false, true); + if (errCode != E_OK) { + goto END; } + resultSet.GetKey(id); END: if (errCode == E_OK) { Key key(id.begin(), id.end()); @@ -513,16 +587,15 @@ int DocumentStore::DeleteDocument(const std::string &collection, const std::stri if (errCode != E_OK) { return errCode; } - bool isOnlyId = true; + bool isIdExist = false; std::vector> filterAllPath; - errCode = TranFilter(filterObj, filterAllPath, isOnlyId); + errCode = TranFilter(filterObj, filterAllPath, isIdExist); if (errCode != E_OK) { return errCode; } std::shared_ptr context = std::make_shared(); context->filter = filter; context->collectionName = collection; - context->isOnlyId = isOnlyId; return DeleteDataFromDB(context, filterObj); } Collection DocumentStore::GetCollection(std::string &collectionName) @@ -701,14 +774,13 @@ int DocumentStore::FindDocument(const std::string &collection, const std::string if (errCode != E_OK) { return errCode; } - bool isOnlyId = true; + bool isIdExist = false; std::vector> filterAllPath; - errCode = TranFilter(filterObj, filterAllPath, isOnlyId); + errCode = TranFilter(filterObj, filterAllPath, isIdExist); if (errCode != E_OK) { GLOGE("filter is invalid"); return errCode; } - context->isOnlyId = isOnlyId; if (flags == GRD_DOC_ID_DISPLAY) { context->ifShowId = true; } else { 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 5dc1741a..a2e3edb3 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,47 +44,32 @@ int ResultSet::Init(std::shared_ptr &context, DocumentStore *store int ResultSet::GetNextWithField() { int errCode = E_OK; - if (context_->isOnlyId) { - JsonObject filterObj = JsonObject::Parse(context_->filter, errCode, true, true); - if (errCode != E_OK) { - GLOGE("filter Parsed failed"); - return errCode; - } + JsonObject filterObj = JsonObject::Parse(context_->filter, errCode, true, true); + if (errCode != E_OK) { + GLOGE("filter Parsed failed"); + 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(); - if (idKey.empty()) { - return -E_NO_DATA; - } - Key key(idKey.begin(), idKey.end()); - Value document; - Collection coll = store_->GetCollection(context_->collectionName); - errCode = coll.GetDocument(key, document); - if (errCode == -E_NOT_FOUND) { - return -E_NO_DATA; - } - std::string jsonData(document.begin(), document.end()); - CutJsonBranch(jsonData); - std::vector> values; - values.emplace_back(std::make_pair(idKey, jsonData)); - matchDatas_ = values; + key.assign(idKey.begin(), idKey.end()); } else { - Collection coll = store_->GetCollection(context_->collectionName); - std::vector> values; - JsonObject filterObj = JsonObject::Parse(context_->filter, errCode, true, true); - if (errCode != E_OK) { - GLOGE("filter Parsed failed"); - return errCode; - } - errCode = coll.GetMatchedDocument(filterObj, values); - if (errCode == -E_NOT_FOUND) { - return -E_NO_DATA; - } - for (size_t i = 0; i < values.size(); i++) { - CutJsonBranch(values[i].second); - } - matchDatas_ = values; + key.assign(matchData_.first.begin(), matchData_.first.end()); + } + matchData_.first.clear(); // Delete previous data. + 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()); + CutJsonBranch(jsonData); + matchData_ = std::make_pair(jsonkey, jsonData); return E_OK; } @@ -104,27 +89,11 @@ int ResultSet::GetNextInner(bool isNeedCheckTable) return -E_INVALID_ARGS; } } - if (!ifField_ && index_ == 0) { - errCode = GetNextWithField(); - if (errCode != E_OK) { - return errCode; - } - } else if (index_ == 0) { - Collection coll = store_->GetCollection(context_->collectionName); - std::vector> values; - JsonObject filterObj = JsonObject::Parse(context_->filter, errCode, true, true); - if (errCode != E_OK) { - GLOGE("filter Parsed failed"); - return errCode; - } - errCode = coll.GetMatchedDocument(filterObj, values); - if (errCode == -E_NOT_FOUND) { - return -E_NO_DATA; - } - matchDatas_ = values; + errCode = GetNextWithField(); + if (errCode != E_OK) { + return errCode; } - index_++; - if (index_ > matchDatas_.size()) { + if (matchData_.first.empty()) { return -E_NO_DATA; } return E_OK; @@ -139,6 +108,7 @@ int ResultSet::GetNext(bool isNeedTransaction, bool isNeedCheckTable) std::lock_guard lock(store_->dbMutex_); errCode = store_->StartTransaction(); if (errCode != E_OK) { + GLOGE("Start transaction faild"); return errCode; } errCode = GetNextInner(isNeedCheckTable); @@ -153,11 +123,11 @@ int ResultSet::GetNext(bool isNeedTransaction, bool isNeedCheckTable) int ResultSet::GetValue(char **value) { std::lock_guard lock(store_->dbMutex_); - if (index_ == 0 || (index_ > matchDatas_.size())) { + if (matchData_.first.empty()) { GLOGE("The value vector in resultSet is empty"); return -E_NO_DATA; } - std::string jsonData = matchDatas_[index_ - 1].second; + std::string jsonData = matchData_.second; char *jsonstr = new char[jsonData.size() + 1]; if (jsonstr == nullptr) { GLOGE("Memory allocation failed!"); @@ -173,13 +143,23 @@ int ResultSet::GetValue(char **value) return E_OK; } -int ResultSet::GetKey(std::string &key) +int ResultSet::GetValue(std::string &value) { - if (index_ == 0 || (index_ > matchDatas_.size())) { + if (matchData_.first.empty()) { GLOGE("The value vector in resultSet is empty"); return -E_NO_DATA; } - key = matchDatas_[index_ - 1].first; + value = matchData_.second; + return E_OK; +} + +int ResultSet::GetKey(std::string &key) +{ + key = matchData_.first; + if (key.empty()) { + GLOGE("can not get data, because it is empty"); + return -E_NO_DATA; + } return E_OK; } 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 e0b9bc24..3b0b6055 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 @@ -15,9 +15,6 @@ #include "result_set_common.h" -#include -#include - #include "doc_errno.h" #include "grd_base/grd_error.h" 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 7dd31d4e..a701cddf 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 @@ -31,9 +31,9 @@ public: 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 GetData(const std::string &collName, const Key &key, Value &value) const = 0; - virtual int GetFieldedData(const std::string &collName, const JsonObject &filterObj, - std::vector> &values) const = 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, + std::pair &values, int isIdExist) const = 0; virtual int DelData(const std::string &collName, const Key &key) = 0; virtual int CreateCollection(const std::string &name, const std::string &option, bool ignoreExists) = 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 1de43e0d..c8d21319 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 @@ -74,7 +74,7 @@ int SqliteStoreExecutorImpl::GetDBConfig(std::string &config) std::string dbConfigKeyStr = "DB_CONFIG"; Key dbConfigKey = { dbConfigKeyStr.begin(), dbConfigKeyStr.end() }; Value dbConfigVal; - int errCode = GetData("grd_meta", dbConfigKey, dbConfigVal); + int errCode = GetDataByKey("grd_meta", dbConfigKey, dbConfigVal); config.assign(dbConfigVal.begin(), dbConfigVal.end()); return errCode; } @@ -152,7 +152,7 @@ int SqliteStoreExecutorImpl::InsertData(const std::string &collName, const Key & return E_OK; } -int SqliteStoreExecutorImpl::GetData(const std::string &collName, const Key &key, Value &value) const +int SqliteStoreExecutorImpl::GetDataByKey(const std::string &collName, const Key &key, Value &value) const { if (dbHandle_ == nullptr) { GLOGE("Invalid db handle."); @@ -178,8 +178,20 @@ int SqliteStoreExecutorImpl::GetData(const std::string &collName, const Key &key return innerErrorCode; } -int SqliteStoreExecutorImpl::GetFieldedData(const std::string &collName, const JsonObject &filterObj, - std::vector> &values) const +std::string GeneralInsertSql(const std::string &collName, const 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;"; + std::string sqlLarger = "SELECT key, value FROM '" + collName + "' WHERE key>?;"; + if (isIdExist) { + return sqlEqual; + } else { + return (key.empty()) ? sqlOrder : sqlLarger; + } +} + +int SqliteStoreExecutorImpl::GetDataByFilter(const std::string &collName, const Key &key, const JsonObject &filterObj, + std::pair &values, int isIdExist) const { if (dbHandle_ == nullptr) { GLOGE("Invalid db handle."); @@ -189,10 +201,15 @@ int SqliteStoreExecutorImpl::GetFieldedData(const std::string &collName, const J Value valueResult; bool isFindMatch = false; int innerErrorCode = -E_NOT_FOUND; - std::string sql = "SELECT key, value FROM '" + collName + "'ORDER BY KEY;"; + 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( dbHandle_, sql, - [](sqlite3_stmt *stmt) { + [key](sqlite3_stmt *stmt) { + if (!key.empty()) { + SQLiteUtils::BindBlobToStatement(stmt, 1, key); + } return E_OK; }, [&keyResult, &innerErrorCode, &valueResult, &filterObj, &values, &isFindMatch](sqlite3_stmt *stmt) { @@ -208,12 +225,15 @@ int SqliteStoreExecutorImpl::GetFieldedData(const std::string &collName, const J } if (JsonCommon::IsJsonNodeMatch(srcObj, filterObj, externErrCode)) { isFindMatch = true; - values.emplace_back(std::pair(keyStr, valueStr)); + values.first = keyStr; + values.second = valueStr; + innerErrorCode = E_OK; + return 1; // match count; } innerErrorCode = E_OK; return E_OK; }); - if (errCode != E_OK) { + if (errCode != E_OK && errCode != 1) { // 1 is match count; GLOGE("[sqlite executor] Get data failed. err=%d", errCode); return errCode; } @@ -231,7 +251,7 @@ int SqliteStoreExecutorImpl::DelData(const std::string &collName, const Key &key } int errCode = 0; Value valueRet; - if (GetData(collName, key, valueRet) != E_OK) { + if (GetDataByKey(collName, key, valueRet) != E_OK) { return -E_NO_DATA; } std::string sql = "DELETE FROM '" + collName + "' WHERE key=?;"; @@ -335,7 +355,7 @@ int SqliteStoreExecutorImpl::GetCollectionOption(const std::string &name, std::s std::string collOptKeyStr = "COLLECTION_OPTION_" + name; Key collOptKey = { collOptKeyStr.begin(), collOptKeyStr.end() }; Value collOptVal; - int errCode = GetData("grd_meta", collOptKey, collOptVal); + int errCode = GetDataByKey("grd_meta", collOptKey, collOptVal); option.assign(collOptVal.begin(), collOptVal.end()); return errCode; } 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 7b82379e..aa392294 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 @@ -39,9 +39,9 @@ public: 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 GetData(const std::string &collName, const Key &key, Value &value) const override; - int GetFieldedData(const std::string &collName, const JsonObject &filterObj, - std::vector> &values) const 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, + std::pair &values, int isIdExist) const override; int DelData(const std::string &collName, const Key &key) override; int CreateCollection(const std::string &name, const std::string &option, bool ignoreExists) override; 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 4daae0f1..3127cacf 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 @@ -34,6 +34,8 @@ int MapSqliteError(int errCode) switch (errCode) { case SQLITE_OK: return E_OK; + case 1: // match count; + return E_OK; case SQLITE_PERM: case SQLITE_CANTOPEN: case SQLITE_READONLY: -- Gitee From a6dfcdf7eb76098fc86e081226d22fe18f34b0f3 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 22 May 2023 10:10:19 +0800 Subject: [PATCH 02/13] fix code check opinion Signed-off-by: Jeremyzz --- .../src/interface/include/result_set.h | 4 ++-- .../src/interface/include/result_set_common.h | 2 +- .../src/interface/src/document_store.cpp | 20 ++++++++++++------- .../src/interface/src/result_set.cpp | 14 +++++++++---- .../src/interface/src/result_set_common.cpp | 6 +++--- 5 files changed, 29 insertions(+), 17 deletions(-) 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 c6b9f935..9440a781 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 @@ -30,7 +30,7 @@ class ResultSet { public: ResultSet(); ~ResultSet(); - int Init(std::shared_ptr &context, DocumentStore *store, bool ifField); + int Init(std::shared_ptr &context, DocumentStore *store, bool isCutBranch); int GetNext(bool isNeedTransaction = false, bool isNeedCheckTable = false); int GetValue(char **value); int GetValue(std::string &value); @@ -45,7 +45,7 @@ private: int GetNextWithField(); DocumentStore *store_ = nullptr; - bool ifField_ = false; + bool isCutBranch_ = false; size_t index_ = 0; std::shared_ptr context_; std::pair matchData_; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/result_set_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/result_set_common.h index 0a59f9ff..abdf5c9b 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/result_set_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/result_set_common.h @@ -25,6 +25,6 @@ namespace DocumentDB { class ValueObject; -int InitResultSet(std::shared_ptr &context, DocumentStore *store, ResultSet &resultSet, bool ifField); +int InitResultSet(std::shared_ptr &context, DocumentStore *store, ResultSet &resultSet, bool isCutBranch); } // namespace DocumentDB #endif // RESULTSET_COMMON_H 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 42e6c411..35b48cdd 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 @@ -175,7 +175,8 @@ int UpdateArgsCheck(const std::string &collection, const std::string &filter, co return errCode; } -int GetUpDataRePlaceData(ResultSet &resultSet, const std::string &id, const std::string &update, std::string &valStr, bool isReplace) +int GetUpDataRePlaceData(ResultSet &resultSet, const std::string &id, const std::string &update, std::string &valStr, + bool isReplace) { std::string valueGotStr; int errCode = resultSet.GetValue(valueGotStr); @@ -209,7 +210,6 @@ int GetUpDataRePlaceData(ResultSet &resultSet, const std::string &id, const std: return errCode; } - int DocumentStore::UpdateDataIntoDB(std::shared_ptr &context, JsonObject &filterObj, const std::string &update, bool &isReplace) { @@ -226,7 +226,10 @@ int DocumentStore::UpdateDataIntoDB(std::shared_ptr &context, Json std::string valStr; auto coll = Collection(context->collectionName, executor_); ResultSet resultSet; - InitResultSet(context, this, resultSet, true); + errCode = InitResultSet(context, this, resultSet, false); + if (errCode != E_OK) { + goto END; + } // no start transaction inner errCode = resultSet.GetNext(false, true); if (errCode == -E_NO_DATA) { @@ -390,9 +393,9 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json return errCode; } context->isIdExist = isIdExist; - errCode = InitResultSet(context, this, resultSet, true); + errCode = InitResultSet(context, this, resultSet, false); if (errCode != E_OK) { - return errCode; + goto END; } errCode = CheckUpsertConflict(resultSet, filterObj, docId, coll); // There are only three return values, the two other situation can continue to move forward. @@ -559,7 +562,10 @@ int DocumentStore::DeleteDataFromDB(std::shared_ptr &context, Json } std::string id; ResultSet resultSet; - InitResultSet(context, this, resultSet, true); + errCode = InitResultSet(context, this, resultSet, false); + if (errCode != E_OK) { + goto END; + } errCode = resultSet.GetNext(false, true); if (errCode != E_OK) { goto END; @@ -740,7 +746,7 @@ int DocumentStore::InitFindResultSet(GRD_ResultSet *grdResultSet, std::shared_pt if (errCode != E_OK) { goto END; } - errCode = InitResultSet(context, this, grdResultSet->resultSet_, false); + errCode = InitResultSet(context, this, grdResultSet->resultSet_, true); if (errCode == E_OK) { collections_[context->collectionName] = nullptr; } 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 cc647692..1eba9ee2 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 @@ -33,9 +33,9 @@ int ResultSet::EraseCollection() } return E_OK; } -int ResultSet::Init(std::shared_ptr &context, DocumentStore *store, bool ifField) +int ResultSet::Init(std::shared_ptr &context, DocumentStore *store, bool isCutBranch) { - ifField_ = ifField; + isCutBranch_ = isCutBranch; context_ = context; store_ = store; return E_OK; @@ -68,7 +68,13 @@ int ResultSet::GetNextWithField() } std::string jsonData(value.second.begin(), value.second.end()); std::string jsonkey(value.first.begin(), value.first.end()); - CutJsonBranch(jsonData); + if (isCutBranch_) { + errCode = CutJsonBranch(jsonData); + if (errCode != E_OK) { + GLOGE("cut branch faild"); + return errCode; + } + } matchData_ = std::make_pair(jsonkey, jsonData); return E_OK; } @@ -194,10 +200,10 @@ int ResultSet::CutJsonBranch(std::string &jsonData) GLOGE("jsonData Parsed failed"); return errCode; } + std::vector> allCutPath; if (context_->viewType) { std::vector singlePath; JsonObject cjsonObjChild = cjsonObj.GetChild(); - std::vector> allCutPath; errCode = CheckCutNode(&cjsonObjChild, singlePath, allCutPath); if (errCode != E_OK) { GLOGE("The node in CheckCutNode is nullptr"); 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 d6e070aa..d4e73e56 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 @@ -20,14 +20,14 @@ namespace DocumentDB { class ValueObject; -int InitResultSet(std::shared_ptr &context, DocumentStore *store, ResultSet &resultSet, bool ifField) +int InitResultSet(std::shared_ptr &context, DocumentStore *store, ResultSet &resultSet, bool isCutBranch) { - if (!ifField) { + if (isCutBranch) { if (context->projectionTree.ParseTree(context->projectionPath) == -E_INVALID_ARGS) { GLOGE("Parse ProjectionTree failed"); return -E_INVALID_ARGS; } } - return resultSet.Init(context, store, ifField); + return resultSet.Init(context, store, isCutBranch); } } // namespace DocumentDB -- Gitee From b6e64d9e0051fd2a88c3e6862feb270a2d2f6e8f Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 22 May 2023 10:29:10 +0800 Subject: [PATCH 03/13] fix code check 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 35b48cdd..62590a41 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 @@ -322,7 +322,7 @@ int CheckUpsertConflict(ResultSet &resultSet, JsonObject &filterObj, std::string return errCode; } -int GetUpsertRePlaceData(ResultSet &resultSet, std::string targetDocument, JsonObject &documentObj, bool isReplace, +int GetUpsertRePlaceData(ResultSet &resultSet, std::string &targetDocument, JsonObject &documentObj, bool isReplace, std::string &valStr) { resultSet.GetNext(); -- Gitee From 7e30b58822b286798ca6f9bc290a16f8ce6768fa Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 22 May 2023 11:44:15 +0800 Subject: [PATCH 04/13] pass all ut Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/interface/include/result_set.h | 1 + .../gaussdb_rd/src/interface/src/document_store.cpp | 2 +- .../data_share/gaussdb_rd/src/interface/src/result_set.cpp | 5 +++-- 3 files changed, 5 insertions(+), 3 deletions(-) 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 9440a781..1191c545 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 @@ -49,6 +49,7 @@ private: size_t index_ = 0; std::shared_ptr context_; std::pair matchData_; + std::string lastKeyIndex_; }; } // namespace DocumentDB #endif // RESULTSET_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 62590a41..c8d99221 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 @@ -327,7 +327,7 @@ int GetUpsertRePlaceData(ResultSet &resultSet, std::string &targetDocument, Json { resultSet.GetNext(); int errCode = resultSet.GetValue(valStr); - if (errCode != E_OK) { + if (errCode != E_OK || isReplace) { valStr = targetDocument; // If cant not find data, insert it. return E_OK; } 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 1eba9ee2..65751f2e 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 @@ -56,7 +56,7 @@ int ResultSet::GetNextWithField() std::string idKey = idValue.GetStringValue(); key.assign(idKey.begin(), idKey.end()); } else { - key.assign(matchData_.first.begin(), matchData_.first.end()); + key.assign(lastKeyIndex_.begin(), lastKeyIndex_.end()); } matchData_.first.clear(); // Delete previous data. matchData_.second.clear(); @@ -68,6 +68,7 @@ int ResultSet::GetNextWithField() } std::string jsonData(value.second.begin(), value.second.end()); std::string jsonkey(value.first.begin(), value.first.end()); + lastKeyIndex_ = jsonkey; if (isCutBranch_) { errCode = CutJsonBranch(jsonData); if (errCode != E_OK) { @@ -99,7 +100,7 @@ int ResultSet::GetNextInner(bool isNeedCheckTable) if (errCode != E_OK) { return errCode; } - if (matchData_.first.empty()) { + if (matchData_.second.empty()) { return -E_NO_DATA; } return E_OK; -- Gitee From 4e13b65b7dc3c4a6f315650bc3665187198e0502 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Wed, 24 May 2023 14:29:15 +0800 Subject: [PATCH 05/13] fix bug Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp index 7ac3a08c..d52c517f 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp @@ -102,7 +102,7 @@ JsonObject::~JsonObject() bool JsonObject::operator==(const JsonObject &other) const { - return (cJSON_Compare(this->cjson_, other.cjson_, 0) != 0); + return (cJSON_Compare(this->cjson_, other.cjson_, true) != 0); // CaseSensitive } bool JsonObject::IsNull() const -- Gitee From 12b993d36556d26abee4253366fe67b3c884237c Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Fri, 26 May 2023 11:17:02 +0800 Subject: [PATCH 06/13] fix code check Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/executor/document/check_common.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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..fb87a9aa 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 @@ -41,7 +41,7 @@ bool CheckCollectionNamePrefix(const std::string &name, const std::string &prefi void ReplaceAll(std::string &inout, const std::string &what, const std::string &with) { - std::string::size_type pos{}; + std::string::size_type pos {}; while ((pos = inout.find(what.data(), pos, what.length())) != std::string::npos) { inout.replace(pos, what.length(), with.data(), with.length()); pos += with.length(); -- Gitee From 6919da93659a67f3b2161aae1d1b69644d5f6a36 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Fri, 26 May 2023 11:21:35 +0800 Subject: [PATCH 07/13] fix code check Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/interface/src/result_set.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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..3ea59660 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 @@ -201,10 +201,10 @@ int ResultSet::CutJsonBranch(std::string &jsonData) GLOGE("jsonData Parsed failed"); return errCode; } - std::vector> allCutPath; if (context_->viewType) { std::vector singlePath; JsonObject cjsonObjChild = cjsonObj.GetChild(); + std::vector> allCutPath; errCode = CheckCutNode(&cjsonObjChild, singlePath, allCutPath); if (errCode != E_OK) { GLOGE("The node in CheckCutNode is nullptr"); -- Gitee From e4404c746d668d1ac9d69f65f1176b6f9a3cf7d7 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Fri, 26 May 2023 11:24:38 +0800 Subject: [PATCH 08/13] fix code check Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/interface/src/result_set_common.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) 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..20d14de2 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 @@ -22,11 +22,9 @@ namespace DocumentDB { class ValueObject; int InitResultSet(std::shared_ptr &context, DocumentStore *store, ResultSet &resultSet, bool isCutBranch) { - if (isCutBranch) { - if (context->projectionTree.ParseTree(context->projectionPath) == -E_INVALID_ARGS) { - GLOGE("Parse ProjectionTree failed"); - return -E_INVALID_ARGS; - } + if (isCutBranch && context->projectionTree.ParseTree(context->projectionPath) == -E_INVALID_ARGS) { + GLOGE("Parse ProjectionTree failed"); + return -E_INVALID_ARGS; } return resultSet.Init(context, store, isCutBranch); } -- Gitee From a60b236bdba8790eebea5fe8528c3bf18b621226 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Fri, 26 May 2023 11:25:32 +0800 Subject: [PATCH 09/13] fix code check Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.h | 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.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_store_executor_impl.h index aa392294..9cf59871 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 @@ -41,7 +41,7 @@ public: 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, - std::pair &values, int isIdExist) const override; + std::pair &values, int isIdExist) const override; int DelData(const std::string &collName, const Key &key) override; int CreateCollection(const std::string &name, const std::string &option, bool ignoreExists) override; -- Gitee From b949e276266e7438fa53c9b14d894f85356dd23f Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Fri, 26 May 2023 11:27:15 +0800 Subject: [PATCH 10/13] fix code check 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 c8d21319..a139b572 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 @@ -181,7 +181,7 @@ int SqliteStoreExecutorImpl::GetDataByKey(const std::string &collName, const Key std::string GeneralInsertSql(const std::string &collName, const 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;"; + std::string sqlOrder = "SELECT key, value FROM '" + collName + "' ORDER BY KEY;"; std::string sqlLarger = "SELECT key, value FROM '" + collName + "' WHERE key>?;"; if (isIdExist) { return sqlEqual; -- Gitee From 39fe6b6837b4c26ce8bf8315edafa9c5e2665561 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Fri, 26 May 2023 12:02:34 +0800 Subject: [PATCH 11/13] fix code check Signed-off-by: Jeremyzz --- .../src/sqlite_store_executor_impl.cpp | 24 +++++++++---------- .../src/oh_adapter/src/sqlite_utils.cpp | 15 ++++++------ .../src/oh_adapter/src/sqlite_utils.h | 2 +- 3 files changed, 20 insertions(+), 21 deletions(-) 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 a139b572..5143319a 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 @@ -166,7 +166,7 @@ int SqliteStoreExecutorImpl::GetDataByKey(const std::string &collName, const Key SQLiteUtils::BindBlobToStatement(stmt, 1, key); return E_OK; }, - [&value, &innerErrorCode](sqlite3_stmt *stmt) { + [&value, &innerErrorCode](sqlite3_stmt *stmt, bool &isMatchOneData) { SQLiteUtils::GetColumnBlobValue(stmt, 0, value); innerErrorCode = E_OK; return E_OK; @@ -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( @@ -212,28 +211,29 @@ int SqliteStoreExecutorImpl::GetDataByFilter(const std::string &collName, const } return E_OK; }, - [&keyResult, &innerErrorCode, &valueResult, &filterObj, &values, &isFindMatch](sqlite3_stmt *stmt) { + [&keyResult, &innerErrorCode, &valueResult, &filterObj, &values, &isFindMatch](sqlite3_stmt *stmt, + bool &isMatchOneData) { SQLiteUtils::GetColumnBlobValue(stmt, 0, keyResult); SQLiteUtils::GetColumnBlobValue(stmt, 1, valueResult); std::string keyStr(keyResult.begin(), keyResult.end()); std::string valueStr(valueResult.begin(), valueResult.end()); - int externErrCode; - JsonObject srcObj = JsonObject::Parse(valueStr, externErrCode, true); - if (externErrCode != E_OK) { + JsonObject srcObj = JsonObject::Parse(valueStr, innerErrorCode, true); + if (innerErrorCode != E_OK) { GLOGE("srcObj Parsed failed"); - return externErrCode; + return innerErrorCode; } - if (JsonCommon::IsJsonNodeMatch(srcObj, filterObj, externErrCode)) { - isFindMatch = true; + if (JsonCommon::IsJsonNodeMatch(srcObj, filterObj, innerErrorCode)) { + isFindMatch = true; // this args work in this function values.first = keyStr; values.second = valueStr; innerErrorCode = E_OK; - return 1; // match count; + isMatchOneData = true; // this args work in ExecSql fuction + return E_OK; // match count; } innerErrorCode = E_OK; return E_OK; }); - if (errCode != E_OK && errCode != 1) { // 1 is match count; + if (errCode != E_OK) { GLOGE("[sqlite executor] Get data failed. err=%d", errCode); return errCode; } @@ -340,7 +340,7 @@ bool SqliteStoreExecutorImpl::IsCollectionExists(const std::string &name, int &e SQLiteUtils::BindTextToStatement(stmt, 1, name); return E_OK; }, - [&isExists](sqlite3_stmt *stmt) { + [&isExists](sqlite3_stmt *stmt, bool &isMatchOneData) { isExists = true; return E_OK; }); 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 4509a362..6ac7e59d 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 @@ -22,7 +22,7 @@ namespace DocumentDB { const int MAX_BLOB_READ_SIZE = 5 * 1024 * 1024; // 5M limit const int MAX_TEXT_READ_SIZE = 5 * 1024 * 1024; // 5M limit -const int BUSY_TIMEOUT_MS = 3000; // 3000ms for sqlite busy timeout. +const int BUSY_TIMEOUT_MS = 3000; // 3000ms for sqlite busy timeout. const std::string BEGIN_SQL = "BEGIN TRANSACTION"; const std::string BEGIN_IMMEDIATE_SQL = "BEGIN IMMEDIATE TRANSACTION"; const std::string COMMIT_SQL = "COMMIT TRANSACTION"; @@ -34,8 +34,6 @@ int MapSqliteError(int errCode) switch (errCode) { case SQLITE_OK: return E_OK; - case 1: // match count; - return E_OK; case SQLITE_PERM: case SQLITE_CANTOPEN: case SQLITE_READONLY: @@ -181,7 +179,7 @@ int SQLiteUtils::GetColumnBlobValue(sqlite3_stmt *statement, int index, std::vec if (keySize < 0 || keySize > MAX_BLOB_READ_SIZE) { GLOGW("[SQLiteUtils][Column blob] size over limit:%d", keySize); value.resize(MAX_BLOB_READ_SIZE + 1); // Reset value size to invalid - return E_OK; // Return OK for continue get data, but value is invalid + return E_OK; // Return OK for continue get data, but value is invalid } auto keyRead = static_cast(sqlite3_column_blob(statement, index)); @@ -220,7 +218,7 @@ int SQLiteUtils::GetColumnTextValue(sqlite3_stmt *statement, int index, std::str if (valSize < 0 || valSize > MAX_TEXT_READ_SIZE) { GLOGW("[SQLiteUtils][Column text] size over limit:%d", valSize); value.resize(MAX_TEXT_READ_SIZE + 1); // Reset value size to invalid - return E_OK; // Return OK for continue get data, but value is invalid + return E_OK; // Return OK for continue get data, but value is invalid } const unsigned char *val = sqlite3_column_text(statement, index); @@ -269,18 +267,18 @@ int SQLiteUtils::ExecSql(sqlite3 *db, const std::string &sql) } int SQLiteUtils::ExecSql(sqlite3 *db, const std::string &sql, const std::function &bindCallback, - const std::function &resultCallback) + const std::function &resultCallback) { if (db == nullptr || sql.empty()) { return -E_INVALID_ARGS; } bool bindFinish = true; sqlite3_stmt *stmt = nullptr; + bool isMatchOneData = false; int errCode = SQLiteUtils::GetStatement(db, sql, stmt); if (errCode != E_OK) { goto END; } - do { if (bindCallback) { errCode = bindCallback(stmt); @@ -297,7 +295,8 @@ 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)) != E_OK)) { + if (resultCallback != nullptr && ((errCode = resultCallback(stmt, isMatchOneData)) != E_OK) || + isMatchOneData) { // find one data, stop stepping. goto END; } } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_utils.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_utils.h index 3567163c..96db4018 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_utils.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/sqlite_utils.h @@ -48,7 +48,7 @@ public: static int ExecSql(sqlite3 *db, const std::string &sql); static int ExecSql(sqlite3 *db, const std::string &sql, const std::function &bindCallback, - const std::function &resultCallback); + const std::function &resultCallback); private: static void SqliteLogCallback(void *data, int err, const char *msg); -- Gitee From ac26212e711158c320772af5b70b052672eb1608 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 09:07:25 +0800 Subject: [PATCH 12/13] add comment Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/interface/src/result_set.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 3ea59660..e1327135 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 @@ -70,7 +70,7 @@ int ResultSet::GetNextWithField() std::string jsonkey(value.first.begin(), value.first.end()); lastKeyIndex_ = jsonkey; if (isCutBranch_) { - errCode = CutJsonBranch(jsonData); + errCode = CutJsonBranch(jsonData); // Crop according to projection. if (errCode != E_OK) { GLOGE("cut branch faild"); return errCode; -- Gitee From 1ae5f40806943c8e7749f70518c4d959cc702d5c Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 09:10:35 +0800 Subject: [PATCH 13/13] fix code check Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/interface/src/result_set.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e1327135..bfbb6181 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 @@ -70,7 +70,7 @@ int ResultSet::GetNextWithField() std::string jsonkey(value.first.begin(), value.first.end()); lastKeyIndex_ = jsonkey; if (isCutBranch_) { - errCode = CutJsonBranch(jsonData); // Crop according to projection. + errCode = CutJsonBranch(jsonData); // cut jsonData branch according to projection. if (errCode != E_OK) { GLOGE("cut branch faild"); return errCode; -- Gitee