From a85ed5219eed6c6c9b6f82de4288042b250093f3 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Sat, 20 May 2023 17:53:55 +0800 Subject: [PATCH 01/79] 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/79] 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/79] 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/79] 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 0c4c8085ed2d98b14999d5936b9688b0bd594df0 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 23 May 2023 09:47:00 +0800 Subject: [PATCH 05/79] 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 06/79] 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 07/79] 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 08/79] 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 7e6e580ffb3b131c16b92622e5af42d79031deb5 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Wed, 24 May 2023 11:43:59 +0800 Subject: [PATCH 09/79] fix conflict of OH Signed-off-by: Jeremyzz --- .../test/unittest/api/documentdb_api_test.cpp | 16 ++++---- .../api/documentdb_collection_test.cpp | 2 +- .../unittest/api/documentdb_data_test.cpp | 13 +++--- .../unittest/api/documentdb_delete_test.cpp | 2 +- .../unittest/api/documentdb_find_test.cpp | 7 ++-- .../unittest/api/documentdb_insert_test.cpp | 8 ++-- .../documentdb_json_common_test.cpp | 41 ++++++++++--------- .../oh_adapter/documentdb_jsonobject_test.cpp | 2 +- 8 files changed, 46 insertions(+), 45 deletions(-) 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 81d9ff25..22fe15ee 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 @@ -65,7 +65,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBTest001, TestSize.Level0) EXPECT_EQ(GRD_CreateCollection(db, "student", "", 0), GRD_OK); - EXPECT_EQ(GRD_UpsertDoc(db, "student", R""({"_id":"10001"})"", R""({"name":"Tom","age":23})"", 0), 1); + EXPECT_EQ(GRD_UpsertDoc(db, "student", R""({"_id":"10001"})"", R""({"name":"Tom", "age":23})"", 0), 1); EXPECT_EQ(GRD_DropCollection(db, "student", 0), GRD_OK); @@ -190,7 +190,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigTest001, TestSize.Level0) { GRD_DB *db = nullptr; std::string path = "./document.db"; - constexpr int MAX_JSON_LEN = 512 * 1024; + const int MAX_JSON_LEN = 512 * 1024; std::string configStr = std::string(MAX_JSON_LEN, 'a'); int status = GRD_DBOpen(path.c_str(), configStr.c_str(), GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OVER_LIMIT); @@ -245,7 +245,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigMaxConnNumTest001, TestSize.Level0) R""({"maxConnNum":1000000007})"", R""({"maxConnNum":"16"})"", R""({"maxConnNum":{"value":17}})"", - R""({"maxConnNum":[16,17,18]})"", + R""({"maxConnNum":[16, 17, 18]})"", }; for (const auto &config : configList) { GLOGD("OpenDBConfigMaxConnNumTest001: test with config:%s", config.c_str()); @@ -363,7 +363,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigPageSizeTest001, TestSize.Level0) R""({"pageSize":1000000007})"", R""({"pageSize":"4"})"", R""({"pageSize":{"value":8}})"", - R""({"pageSize":[16,32,64]})"", + R""({"pageSize":[16, 32, 64]})"", }; for (const auto &config : configList) { GLOGD("OpenDBConfigPageSizeTest001: test with config:%s", config.c_str()); @@ -500,11 +500,11 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigBufferPoolTest001, TestSize.Level0) GRD_DB *db = nullptr; std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), R""({"pageSize":64,"bufferPoolSize":4096})"", GRD_DB_OPEN_CREATE, &db); + int status = GRD_DBOpen(path.c_str(), R""({"pageSize":64, "bufferPoolSize":4096})"", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OK); EXPECT_EQ(GRD_DBClose(db, 0), GRD_OK); - status = GRD_DBOpen(path.c_str(), R""({"pageSize":64,"bufferPoolSize":4095})"", GRD_DB_OPEN_CREATE, &db); + status = GRD_DBOpen(path.c_str(), R""({"pageSize":64, "bufferPoolSize":4095})"", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_INVALID_ARGS); status = GRD_DBOpen(path.c_str(), R""({"bufferPoolSize":1023})"", GRD_DB_OPEN_CREATE, &db); @@ -526,11 +526,11 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigPubBuffTest001, TestSize.Level0) GRD_DB *db = nullptr; std::string path = "./document.db"; - int status = GRD_DBOpen(path.c_str(), R""({"pageSize":64,"redopubbufsize":4033})"", GRD_DB_OPEN_CREATE, &db); + int status = GRD_DBOpen(path.c_str(), R""({"pageSize":64, "redopubbufsize":4033})"", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OK); EXPECT_EQ(GRD_DBClose(db, 0), GRD_OK); - status = GRD_DBOpen(path.c_str(), R""({"pageSize":64,"redopubbufsize":4032})"", GRD_DB_OPEN_CREATE, &db); + status = GRD_DBOpen(path.c_str(), R""({"pageSize":64, "redopubbufsize":4032})"", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_INVALID_ARGS); status = GRD_DBOpen(path.c_str(), R""({"redopubbufsize":255})"", GRD_DB_OPEN_CREATE, &db); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_collection_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_collection_test.cpp index 92aedcd5..04431be4 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_collection_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_collection_test.cpp @@ -157,7 +157,7 @@ HWTEST_F(DocumentDBCollectionTest, CollectionTest005, TestSize.Level0) R""({"maxDoc":0})"", R""({"maxDoc":"123"})"", R""({"maxDoc":{"value":1024}})"", - R""({"maxDoc":[1,2,4,8]})"", + R""({"maxDoc":[1, 2, 4, 8]})"", R""({"minDoc":1024})"", }; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp index d43519a7..1ea370b5 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp @@ -149,7 +149,7 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest006, TestSize.Level0) HWTEST_F(DocumentDBDataTest, UpsertDataTest007, TestSize.Level0) { std::string filter = R""({"_id":"1234"})""; - std::string val = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::string val = R""({"name":"Tmono", "age":18, "addr":{"city":"shanghai", "postal":200001}})""; EXPECT_EQ(GRD_UpsertDoc(g_db, "collection_not_exists", filter.c_str(), val.c_str(), GRD_DOC_REPLACE), GRD_INVALID_ARGS); } @@ -164,10 +164,10 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest007, TestSize.Level0) HWTEST_F(DocumentDBDataTest, UpsertDataTest008, TestSize.Level0) { std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmn","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::string document = R""({"name":"Tmn", "age":18, "addr":{"city":"shanghai", "postal":200001}})""; EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), GRD_DOC_REPLACE), 1); - std::string updateDoc = R""({"name":"Xue","case":2,"age":28,"addr":{"city":"shenzhen","postal":518000}})""; + std::string updateDoc = R""({"name":"Xue", "case":2, "age":28, "addr":{"city":"shenzhen", "postal":518000}})""; EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), updateDoc.c_str(), GRD_DOC_APPEND), 1); } @@ -272,7 +272,7 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest005, TestSize.Level0) {} HWTEST_F(DocumentDBDataTest, UpdateDataTest006, TestSize.Level0) { std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + std::string document = R""({"name":"Tmono", "age":18, "addr":{"city":"shanghai", "postal":200001}})""; std::vector invalidFlags = { 2, 4, 8, 1024, UINT32_MAX }; for (auto flag : invalidFlags) { GLOGD("UpdateDataTest006: update data with flag: %u", flag); @@ -293,7 +293,7 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest007, TestSize.Level0) HWTEST_F(DocumentDBDataTest, UpdateDataTest008, TestSize.Level0) { const char *updateStr = - R""({"field2":{"c_field":{"cc_field":{"ccc_field":{"ccc_field":[1,false,1.234e2,["hello world!"]]}}}}})""; + R""({"field2":{"c_field":{"cc_field":{"ccc_field":{"ccc_field":[1, false, 1.234e2, ["hello world!"]]}}}}})""; int result = GRD_UpdateDoc(g_db, g_coll, "{\"field\" : 2}", updateStr, 0); int result2 = GRD_UpsertDoc(g_db, g_coll, "{\"field\" : 2}", updateStr, 0); EXPECT_EQ(result, GRD_INVALID_ARGS); @@ -318,7 +318,8 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest009, TestSize.Level0) char *value = nullptr; EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); string valueStr = value; - string repectStr = R""({"_id":"1234","field1":1,"field2":2,"FIELD1":[1,true,1.23456789,"hello world!",null]})""; + string repectStr = R""({"_id":"1234", "field1":1, "field2":2, + "FIELD1":[1, true, 1.23456789, "hello world!", null]})""; EXPECT_EQ((valueStr == repectStr), 1); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_delete_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_delete_test.cpp index 15e5dce4..8165c73b 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_delete_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_delete_test.cpp @@ -29,7 +29,7 @@ using namespace DocumentDBUnitTest; namespace { constexpr const char *COLLECTION_NAME = "student"; constexpr const char *NULL_JSON_STR = "{}"; -constexpr int MAX_COLLECTION_LENS = 511; +const int MAX_COLLECTION_LENS = 511; std::string g_path = "./document.db"; GRD_DB *g_db = nullptr; 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..f77761d8 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 @@ -49,7 +49,9 @@ static const char *g_document4 = "{\"_id\" : \"4\", \"name\":\"doc4\",\"item\":\ static const char *g_document5 = "{\"_id\" : \"5\", \"name\":\"doc5\",\"item\":\"journal\",\"personInfo\":\ [{\"sex\" : \"woma\", \"school\" : \"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; static const char *g_document6 = "{\"_id\" : \"6\", \"name\":\"doc6\",\"item\":false,\"personInfo\":\ - [{\"school\":\"B\", \"teacher\" : \"mike\",\"age\" : 15}, {\"school\":\"C\", \"teacher\" : \"moon\",\"age\":20}]}"; + [{\"school\":\"B\", \"teacher\" : \"mike\", \"age\" : 15},\ + {\"school\":\"C\", \"teacher\" : \"moon\", \"age\" : 20}]}"; + static const char *g_document7 = "{\"_id\" : \"7\", \"name\":\"doc7\",\"item\":\"fruit\",\"other_Info\":\ [{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; static const char *g_document8 = "{\"_id\" : \"8\", \"name\":\"doc8\",\"item\":true,\"personInfo\":\ @@ -1438,17 +1440,14 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest061, TestSize.Level1) char *value = nullptr; EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); CompareValue(value, document061); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); CompareValue(value, document062); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); CompareValue(value, document063); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); 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..7ce58327 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 @@ -330,14 +330,14 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest014, TestSize.Level1) * @tc.expected:step1.GRD_INVALID_ARGS */ const char *document2 = R""({"level1" : {"level2" : {"level3" : [{ "level5" : "level5_1val", - "level5_2":"level5_2_val"}, "level4_val1","level4_val2"], "level3_2" : "level3_2_val"}},"_id":"14"})""; + "level5_2":"level5_2_val"}, "level4_val1","level4_val2"], "level3_2" : "level3_2_val"}}, "_id":"14"})""; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); /** * @tc.steps:step1.document's JSON depth is 4 * @tc.expected:step1.GRD_OK */ const char *document3 = R""({"level1" : {"level2" : {"level3" : { "level4" : "level5_1val"}, - "level3_2" : "level3_2_val"}},"_id":"14"})""; + "level3_2" : "level3_2_val"}}, "_id":"14"})""; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_OK); } @@ -563,8 +563,8 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest025, TestSize.Level1) * @tc.steps:step1.documents JSON depth is 4, which is allowed. * @tc.expected:step1.GRD_OK. */ - const char *document1 = "{\"_id\" : \"25_0\", \"level1\" : { \"level2\" : {\"level3\" :\ - {\"level4\" : \"level4Val\" } } } , \"level1_2\" : \"level1_2Val\" }"; + const char *document1 = "{\"_id\" : \"25_0\", \"level1\" : {\"level2\" : {\"level3\" :\ + {\"level4\" : \"level4Val\"}}} , \"level1_2\" : \"level1_2Val\" }"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); /** * @tc.steps:step2.documents JSON depth is exactly 4. 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 bfe3a846..bec7c70a 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 @@ -44,9 +44,9 @@ void DocumentDBJsonCommonTest::TearDown(void) {} HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest001, TestSize.Level0) { - std::string document = R""({"name":"Tmn","age":18,"addr":{"city":"shanghai","postal":200001}})""; - std::string updateDoc = R""({"name":"Xue","case":{"field1":1,"field2":"string","field3":[1,2,3]}, - "age":28,"addr":{"city":"shenzhen","postal":518000}})""; + std::string document = R""({"name":"Tmn", "age":18, "addr":{"city":"shanghai", "postal":200001}})""; + std::string updateDoc = R""({"name":"Xue", "case":{"field1":1, "field2":"string", "field3":[1, 2, 3]}, + "age":28, "addr":{"city":"shenzhen", "postal":518000}})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -68,9 +68,10 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest001, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest002, TestSize.Level0) { - std::string document = R""({"name":"Tmn","case":2,"age":[1,2,3],"addr":{"city":"shanghai","postal":200001}})""; - std::string updateDoc = R""({"name":["Xue","Neco","Lip"],"grade":99,"age":18,"addr": - [{"city":"shanghai","postal":200001},{"city":"beijing","postal":100000}]})""; + std::string document = R""({"name":"Tmn", "case":2, "age":[1, 2, 3], + "addr":{"city":"shanghai", "postal":200001}})""; + std::string updateDoc = R""({"name":["Xue", "Neco", "Lip"], "grade":99, "age":18, "addr": + [{"city":"shanghai", "postal":200001}, {"city":"beijing", "postal":100000}]})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -91,10 +92,10 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest002, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest003, TestSize.Level0) { - std::string document = R""({"name":["Tmn","BB","Alice"],"age":[1,2,3],"addr":[{"city":"shanghai","postal":200001}, - {"city":"wuhan","postal":430000}]})""; - std::string updateDoc = R""({"name":["Xue","Neco","Lip"],"age":18,"addr":[{"city":"shanghai","postal":200001}, - {"city":"beijing","postal":100000}]})""; + std::string document = R""({"name":["Tmn", "BB", "Alice"], "age":[1, 2, 3], + "addr":[{"city":"shanghai", "postal":200001}, {"city":"wuhan", "postal":430000}]})""; + std::string updateDoc = R""({"name":["Xue", "Neco", "Lip"], "age":18, "addr":[{"city":"shanghai", "postal":200001}, + {"city":"beijing", "postal":100000}]})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -129,7 +130,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest004, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest005, TestSize.Level0) { - std::string document = R""({"name":["Tmn","BB","Alice"]})""; + std::string document = R""({"name":["Tmn", "BB", "Alice"]})""; std::string updateDoc = R""({"name.2":"GG"})""; int errCode = E_OK; @@ -148,7 +149,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest005, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest006, TestSize.Level0) { - std::string document = R""({"name":{"first":"Tno","last":"moray"}})""; + std::string document = R""({"name":{"first":"Tno", "last":"moray"}})""; std::string updateDoc = R""({"name":{"midle.AA":"GG"}})""; int errCode = E_OK; @@ -167,7 +168,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest006, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest007, TestSize.Level0) { - std::string document = R""({"name":{"first":["XX","CC"],"last":"moray"}})""; + std::string document = R""({"name":{"first":["XX", "CC"], "last":"moray"}})""; std::string updateDoc = R""({"name.first.0":"LL"})""; int errCode = E_OK; @@ -186,8 +187,8 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest007, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest008, TestSize.Level0) { - std::string document = R""({"name":{"first":"XX","last":"moray"}})""; - std::string updateDoc = R""({"name":{"first":["XXX","BBB","CCC"]}})""; + std::string document = R""({"name":{"first":"XX", "last":"moray"}})""; + std::string updateDoc = R""({"name":{"first":["XXX", "BBB", "CCC"]}})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -205,7 +206,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest008, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest009, TestSize.Level0) { - std::string document = R""({"name":{"first":["XXX","BBB","CCC"],"last":"moray"}})""; + std::string document = R""({"name":{"first":["XXX", "BBB", "CCC"], "last":"moray"}})""; std::string updateDoc = R""({"name":{"first":"XX"}})""; int errCode = E_OK; @@ -286,8 +287,8 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest013, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest014, TestSize.Level0) { - std::string document = R""({"name":{"first":"Xue","second":"Lang"}, "info":"AA"})""; - std::string updateDoc = R""({"info":"GG", "name.0":"GG"})""; + std::string document = R""({"name":{"first":"Xue", "second":"Lang"}})""; + std::string updateDoc = R""({"name.0":"GG"})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -301,7 +302,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest014, TestSize.Level0) HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest015, TestSize.Level0) { std::string document = R""({"name":{"first":"Xue","second":"Lang"}})""; - std::string updateDoc = R""({"name.first":["GG","MM"]})""; + std::string updateDoc = R""({"name.first":["GG", "MM"]})""; int errCode = E_OK; JsonObject src = JsonObject::Parse(document, errCode); @@ -459,7 +460,7 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest012, TestSize.Leve { std::string document = R""({"item" : "journal", "instock" : [{"warehose" : "A", "qty" : 5}, {"warehose" : "C", "qty" : 15}]})""; - std::string filter = R""({"instock" : {"warehose" : "A", "bad" : "2" ,"qty" : 5}})""; + std::string filter = R""({"instock" : {"warehose" : "A", "bad" : "2" , "qty" : 5}})""; int errCode = E_OK; JsonObject srcObj = JsonObject::Parse(document, errCode); EXPECT_EQ(errCode, E_OK); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp index 93d46974..604f2c38 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_jsonobject_test.cpp @@ -49,7 +49,7 @@ void DocumentDBJsonObjectTest::TearDown(void) {} */ HWTEST_F(DocumentDBJsonObjectTest, JsonObjectTest001, TestSize.Level0) { - const std::string config = R""({"a":123,"b":{"c":234,"d":"12345"}})""; + const std::string config = R""({"a":123, "b":{"c":234, "d":"12345"}})""; int ret = E_OK; JsonObject conf = JsonObject::Parse(config, ret); -- Gitee From 4e13b65b7dc3c4a6f315650bc3665187198e0502 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Wed, 24 May 2023 14:29:15 +0800 Subject: [PATCH 10/79] 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 d7691297a1788de25b1a7bc663e8b20c49ec7963 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Wed, 24 May 2023 16:28:05 +0800 Subject: [PATCH 11/79] 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 12/79] 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 13/79] 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 14/79] 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 15/79] 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 12b993d36556d26abee4253366fe67b3c884237c Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Fri, 26 May 2023 11:17:02 +0800 Subject: [PATCH 16/79] 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 17/79] 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 18/79] 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 19/79] 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 20/79] 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 21/79] 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 b754dc8b82854a9ed7170cba74a410a9694ff889 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Sat, 27 May 2023 18:40:52 +0800 Subject: [PATCH 22/79] 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 23/79] 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 24/79] 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 ac26212e711158c320772af5b70b052672eb1608 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 09:07:25 +0800 Subject: [PATCH 25/79] 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 26/79] 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 From de5266bcc77c814ac780efe9bb61a0850864ce24 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 29 May 2023 10:14:18 +0800 Subject: [PATCH 27/79] 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 28/79] 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 29/79] 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 30/79] 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 31/79] 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 32/79] 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 33/79] 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 34/79] 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 35/79] 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 36/79] 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 37/79] 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 38/79] 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 39/79] 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 40/79] 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 41/79] 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 42/79] 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 43/79] 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 44/79] 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 45/79] 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 46/79] 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 From aa75574d9f246cad4a2836b58738e906f835f955 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 1 Jun 2023 10:54:58 +0800 Subject: [PATCH 47/79] add errCode and change some Signed-off-by: Jeremyzz --- .../src/interface/include/doc_errno.h | 1 + .../src/interface/src/doc_errno.cpp | 6 ++-- .../interface/src/document_store_manager.cpp | 2 +- .../src/oh_adapter/src/sqlite_utils.cpp | 1 + .../test/unittest/api/documentdb_api_test.cpp | 30 +++++++++++++++++++ 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/doc_errno.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/doc_errno.h index 274bc841..75f636b5 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/doc_errno.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/include/doc_errno.h @@ -40,6 +40,7 @@ constexpr int E_RESOURCE_BUSY = E_BASE + 50; constexpr int E_FAILED_MEMORY_ALLOCATE = E_BASE + 51; constexpr int E_INNER_ERROR = E_BASE + 52; constexpr int E_INVALID_FILE_FORMAT = E_BASE + 53; +constexpr int E_FAILED_FILE_OPERATION = E_BASE + 54; int TransferDocErr(int err); } // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/doc_errno.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/doc_errno.cpp index 3c8887e9..bf9dca10 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/doc_errno.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/doc_errno.cpp @@ -21,8 +21,8 @@ namespace DocumentDB { int GetErrorCategory(int errCode) { int categoryCode = errCode % 1000000; // 1000000: mod to get last 6 digits - categoryCode /= 1000; // 1000: deviced to remove first 3 digits - categoryCode *= 1000; // 1000: multiply to pad the output + categoryCode /= 1000; // 1000: deviced to remove first 3 digits + categoryCode *= 1000; // 1000: multiply to pad the output return categoryCode; } @@ -63,6 +63,8 @@ int TransferDocErr(int err) return GetErrorCategory(GRD_FAILED_MEMORY_ALLOCATE); case -E_INVALID_FILE_FORMAT: return GetErrorCategory(GRD_INVALID_FILE_FORMAT); + case -E_FAILED_FILE_OPERATION: + return GetErrorCategory(GRD_FAILED_FILE_OPERATION); case -E_INNER_ERROR: default: return GetErrorCategory(GRD_INNER_ERR); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store_manager.cpp index 138c86ab..c9a91cec 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store_manager.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store_manager.cpp @@ -133,7 +133,7 @@ int DocumentStoreManager::CheckDBPath(const std::string &path, std::string &cano if (path.back() == '/') { GLOGE("Invalid path end with slash"); - return -E_INVALID_ARGS; + return -E_FAILED_FILE_OPERATION; } std::string dirPath; 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 de37d418..5281df56 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 @@ -36,6 +36,7 @@ int MapSqliteError(int errCode) return E_OK; case SQLITE_PERM: case SQLITE_CANTOPEN: + return -E_INVALID_ARGS; case SQLITE_READONLY: return -E_FILE_OPERATION; case SQLITE_NOTADB: 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 d4518ff6..7eae202d 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 @@ -179,6 +179,36 @@ HWTEST_F(DocumentDBApiTest, OpenDBPathTest002, TestSize.Level0) EXPECT_EQ(status, GRD_FAILED_FILE_OPERATION); } +/** + * @tc.name: OpenDBPathTest003 + * @tc.desc: call GRD_DBOpen, input dbFile end with '\' + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBApiTest, OpenDBPathTest003, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string pathNoPerm = "/root/document.db/"; + int status = GRD_DBOpen(pathNoPerm.c_str(), nullptr, GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_FAILED_FILE_OPERATION); +} + +/** + * @tc.name: OpenDBPathTest004 + * @tc.desc: call GRD_DBOpen, input dbFile as existed menu + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBApiTest, OpenDBPathTest004, TestSize.Level0) +{ + GRD_DB *db = nullptr; + std::string pathNoPerm = "../build"; + int status = GRD_DBOpen(pathNoPerm.c_str(), nullptr, GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_INVALID_ARGS); +} + /** * @tc.name: OpenDBConfigTest001 * @tc.desc: Test open document db with invalid config option -- Gitee From bf14c0892dd6303fdee344fd5808e0e5f27d359a Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 1 Jun 2023 11:08:00 +0800 Subject: [PATCH 48/79] delete some wrong ut Signed-off-by: Jeremyzz --- .../gaussdb_rd/test/unittest/api/documentdb_api_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7eae202d..c127bfed 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 @@ -156,7 +156,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBTest004, TestSize.Level0) HWTEST_F(DocumentDBApiTest, OpenDBPathTest001, TestSize.Level0) { GRD_DB *db = nullptr; - std::vector invalidPath = { nullptr, "", "/a/b/c/" }; + std::vector invalidPath = { nullptr, ""}; for (auto path : invalidPath) { GLOGD("OpenDBPathTest001: open db with path: %s", path); int status = GRD_DBOpen(path, nullptr, GRD_DB_OPEN_CREATE, &db); -- Gitee From d62d079c889b0914fed9886658be13e9be07af9e Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 1 Jun 2023 15:14:04 +0800 Subject: [PATCH 49/79] fix bug Signed-off-by: Jeremyzz --- .../src/sqlite_store_executor_impl.cpp | 2 ++ .../test/unittest/api/documentdb_api_test.cpp | 33 ++++++++++++++++++- 2 files changed, 34 insertions(+), 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 a765b813..da40bfe6 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 @@ -75,6 +75,7 @@ int SqliteStoreExecutorImpl::GetDBConfig(std::string &config) { std::string dbConfigKeyStr = "DB_CONFIG"; Key dbConfigKey = { dbConfigKeyStr.begin(), dbConfigKeyStr.end() }; + dbConfigKey.push_back(KEY_TYPE); // Push back Key Tpye, make it become really Key. Value dbConfigVal; int errCode = GetDataByKey("grd_meta", dbConfigKey, dbConfigVal); config.assign(dbConfigVal.begin(), dbConfigVal.end()); @@ -394,6 +395,7 @@ int SqliteStoreExecutorImpl::GetCollectionOption(const std::string &name, std::s { std::string collOptKeyStr = "COLLECTION_OPTION_" + name; Key collOptKey = { collOptKeyStr.begin(), collOptKeyStr.end() }; + collOptKey.push_back(KEY_TYPE); // Push back Key Tpye, make it become really Key. Value collOptVal; int errCode = GetDataByKey("grd_meta", collOptKey, collOptVal); option.assign(collOptVal.begin(), collOptVal.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 d4518ff6..ea4f8f15 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 @@ -226,6 +226,37 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigTest003, TestSize.Level0) EXPECT_EQ(status, GRD_INVALID_ARGS); } +/** + * @tc.name: OpenDBConfigTest005 + * @tc.desc: Verify open db with different configStr connection when first connection not close, + * return GRD_INVALID_CONFIG_VALUE. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigTest005, TestSize.Level0) +{ + /** + * @tc.steps:step1. call GRD_DBOPEN to create a db with connection1. + * @tc.expected:step1. GRD_OK. + */ + const char *configStr = R"({"pageSize":64, "bufferPoolSize": 4096})"; + GRD_DB *db1 = nullptr; + std::string path = "./document.db"; + int result = GRD_DBOpen(path.c_str(), configStr, GRD_DB_OPEN_CREATE, &db1); + ASSERT_EQ(result, GRD_OK); + /** + * @tc.steps:step2. connection2 call GRD_DBOpen to open the db with the different configStr. + * @tc.expected:step2. return GRD_CONFIG_OPTION_MISMATCH. + */ + const char *configStr_2 = R"({"pageSize":4})"; + GRD_DB *db2 = nullptr; + result = GRD_DBOpen(path.c_str(), configStr_2, GRD_DB_OPEN_ONLY, &db2); + ASSERT_EQ(result, GRD_INVALID_ARGS); + + ASSERT_EQ(GRD_DBClose(db1, GRD_DB_CLOSE), GRD_OK); +} + /** * @tc.name: OpenDBConfigMaxConnNumTest001 * @tc.desc: Test open document db with invalid config item maxConnNum @@ -381,7 +412,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 ec8dfe05e363038124a7a746a5d5e246f79992cc Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 1 Jun 2023 15:17:48 +0800 Subject: [PATCH 50/79] fix Signed-off-by: Jeremyzz --- .../gaussdb_rd/test/unittest/api/documentdb_api_test.cpp | 1 - 1 file changed, 1 deletion(-) 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 ea4f8f15..b76f357c 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 @@ -412,7 +412,6 @@ 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 61f6abb7e76d69bd6a2fbc9c16d8c5c5f8520771 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 30 May 2023 11:48:08 +0800 Subject: [PATCH 51/79] fix white box Signed-off-by: Jeremyzz --- .../data_share/gaussdb_rd/src/common/src/db_config.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/db_config.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/db_config.cpp index 44ce39b6..1ec53e65 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/db_config.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/db_config.cpp @@ -56,6 +56,10 @@ bool CheckAndGetDBConfig(const JsonObject &config, const std::string &name, cons int errCode = E_OK; ValueObject configValue = config.GetObjectByPath(configField, errCode); + if (errCode != E_OK) { + GLOGE("Cant find config Value"); + return errCode; + } if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { GLOGE("Check DB config failed, not found or type of %s is not NUMBER.", name.c_str()); return false; -- Gitee From 8f23398e044e0cd4809f9f9c35664f55d3d45938 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 30 May 2023 14:39:55 +0800 Subject: [PATCH 52/79] finish white box Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/common/src/json_common.cpp | 10 ++++------ .../gaussdb_rd/src/interface/include/collection.h | 1 - .../gaussdb_rd/src/interface/src/collection.cpp | 9 --------- .../gaussdb_rd/src/interface/src/document_store.cpp | 8 ++++---- 4 files changed, 8 insertions(+), 20 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 6157cd09..cf8ae899 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 @@ -83,9 +83,9 @@ std::vector JsonCommon::GetLeafValue(const JsonObject &node) bool JsonCommon::CheckNode(JsonObject &node) { while (!node.IsNull()) { - int ret = 0; std::set fieldSet; bool isFieldNameExist = true; + int ret = E_OK; std::string fieldName = node.GetItemField(ret); if (ret != E_OK) { isFieldNameExist = false; @@ -214,8 +214,7 @@ int JsonCommon::ParseNode(JsonObject &node, std::vector singlePath, } if (!node.GetChild().IsNull() && node.GetChild().GetItemField() != "") { JsonObject nodeNew = node.GetChild(); - int ret = E_OK; - ret = ParseNode(nodeNew, singlePath, resultPath, false); + int ret = ParseNode(nodeNew, singlePath, resultPath, false); if (ret != E_OK) { return ret; } @@ -544,8 +543,7 @@ int JsonCommon::Append(const JsonObject &src, const JsonObject &add, bool isRepl { int externErrCode = E_OK; JsonObjectIterator(add, {}, - [&src, &externErrCode, &isReplace](const JsonFieldPath &path, const JsonObject &father, - const JsonObject &item) { + [&src, &externErrCode, &isReplace](const JsonFieldPath &path, const JsonObject &father, const JsonObject &item) { bool isCollapse = false; // Whether there is a path generated by the dot operator, such as t1.t2.t3 JsonFieldPath itemPath = ExpendPathForField(path, isCollapse); if (src.IsFieldExists(itemPath)) { @@ -645,7 +643,7 @@ bool JsonCommon::IsObjectItemMatch(const JsonObject &srcItem, const JsonObject & bool JsonCommon::JsonEqualJudge(const JsonFieldPath &itemPath, const JsonObject &src, const JsonObject &item, bool &isCollapse, int &isMatchFlag) { - int errCode; + int errCode = E_OK; int isAlreadyMatched = 0; JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); if (errCode != -E_JSON_PATH_NOT_EXISTS && srcItem == item) { 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 1e5f9ece..a2a41180 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 @@ -36,7 +36,6 @@ public: int DeleteDocument(Key &key); int IsCollectionExists(int &errCode); int UpsertDocument(const std::string &id, const std::string &newDocument, bool &isIdExist); - bool FindDocument(); int UpdateDocument(const std::string &id, const std::string &document); private: 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 47842e9c..09173698 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 @@ -78,15 +78,6 @@ int Collection::InsertDocument(const std::string &id, const std::string &documen return executor_->InsertData(name_, key, valSet); } -bool Collection::FindDocument() -{ - if (executor_ == nullptr) { - return -E_INNER_ERROR; - } - int errCode = E_OK; - return executor_->IsCollectionExists(name_, errCode); -} - int Collection::GetDocumentById(Key &key, Value &document) const { if (executor_ == nullptr) { 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 5d9f8a14..3222d4bb 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 @@ -367,7 +367,7 @@ int InsertIdToDocument(ResultSet &resultSet, JsonObject &filterObj, JsonObject & } if (isIdExist) { docId = idValue.GetStringValue(); - JsonObject idObj = filterObj.GetObjectItem(KEY_ID, errCode); + JsonObject idObj = filterObj.GetObjectItem(KEY_ID, errCode); // this errCode will always be E_OK. documentObj.InsertItemObject(0, idObj); } else { if (ret == E_OK) { // E_OK means find data. @@ -780,8 +780,7 @@ END: int DocumentStore::FindDocument(const std::string &collection, const std::string &filter, const std::string &projection, uint32_t flags, GRD_ResultSet *grdResultSet) { - int errCode = E_OK; - errCode = FindArgsCheck(collection, filter, projection, flags); + int errCode = FindArgsCheck(collection, filter, projection, flags); if (errCode != E_OK) { GLOGE("delete arg is illegal"); return errCode; @@ -878,7 +877,8 @@ int DocumentStore::Rollback() bool DocumentStore::IsCollectionExists(const std::string &collectionName, int &errCode) { if (executor_ == nullptr) { - return -E_INNER_ERROR; + errCode = -E_INNER_ERROR; + return false; } return executor_->IsCollectionExists(collectionName, errCode); } -- Gitee From 8aa31e5014d7e9145e3a6bb38bdc420d73820248 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 30 May 2023 14:55:14 +0800 Subject: [PATCH 53/79] fix alram of OH Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/common/src/json_common.cpp | 14 +++----------- .../src/interface/src/document_store.cpp | 8 ++++---- .../gaussdb_rd/src/interface/src/result_set.cpp | 3 +-- 3 files changed, 8 insertions(+), 17 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 cf8ae899..171c1fcb 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 @@ -504,9 +504,7 @@ bool JsonNodeAppend(const JsonObject &src, const JsonFieldPath &path, const Json int errCode = E_OK; JsonObject srcFatherItem = src.FindItem(fatherPath, errCode); std::string lastFieldName = itemPath.back(); - int isAddedFlag = false; if (errCode != E_OK) { - isAddedFlag = true; AddSpliteField(src, item, itemPath, externErrCode); return false; } @@ -520,7 +518,6 @@ bool JsonNodeAppend(const JsonObject &src, const JsonFieldPath &path, const Json GLOGE("Add item to object failed. %d", errCode); return false; } - isAddedFlag = true; return false; } if (!isCollapse) { @@ -528,13 +525,10 @@ bool JsonNodeAppend(const JsonObject &src, const JsonFieldPath &path, const Json if (!ret) { return false; // replace failed } - isAddedFlag = true; return false; // Different node types, overwrite directly, skip child node } - if (!isAddedFlag) { - GLOGE("Add nothing because data conflict"); - externErrCode = -E_DATA_CONFLICT; - } + GLOGE("Add nothing because data conflict"); + externErrCode = -E_DATA_CONFLICT; return false; // Source path not exist, overwrite directly, skip child node } } // namespace @@ -644,11 +638,9 @@ bool JsonCommon::JsonEqualJudge(const JsonFieldPath &itemPath, const JsonObject bool &isCollapse, int &isMatchFlag) { int errCode = E_OK; - int isAlreadyMatched = 0; JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); if (errCode != -E_JSON_PATH_NOT_EXISTS && srcItem == item) { isMatchFlag = true; - isAlreadyMatched = 1; return false; } JsonFieldPath granpaPath = itemPath; @@ -659,7 +651,6 @@ bool JsonCommon::JsonEqualJudge(const JsonFieldPath &itemPath, const JsonObject JsonObject fatherItem = granpaItem.GetChild(); while (!fatherItem.IsNull()) { if ((fatherItem.GetObjectItem(lastFieldName, errCode) == item)) { // this errCode is always E_OK - isAlreadyMatched = 1; isMatchFlag = true; break; } @@ -668,6 +659,7 @@ bool JsonCommon::JsonEqualJudge(const JsonFieldPath &itemPath, const JsonObject } return false; } + int isAlreadyMatched = 0; // means no match anyting return IsObjectItemMatch(srcItem, item, isAlreadyMatched, isCollapse, isMatchFlag); } 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 3222d4bb..24b1ed3c 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 @@ -156,8 +156,8 @@ int UpdateArgsCheck(const std::string &collection, const std::string &filter, co GLOGE("update Parsed failed"); return errCode; } - std::vector> allPath; if (update != "{}") { + std::vector> allPath; allPath = JsonCommon::ParsePath(updateObj, errCode); if (errCode != E_OK) { GLOGE("updateObj ParsePath failed"); @@ -434,8 +434,8 @@ END: int UpsertDocumentFormatCheck(const std::string &document, JsonObject &documentObj) { int errCode = E_OK; - std::vector> allPath; if (document != "{}") { + std::vector> allPath; allPath = JsonCommon::ParsePath(documentObj, errCode); if (errCode != E_OK) { return errCode; @@ -627,7 +627,7 @@ Collection DocumentStore::GetCollection(std::string &collectionName) return Collection(collectionName, executor_); } -int JudgeViewType(size_t &index, ValueObject &leafItem, bool &viewType) +int JudgeViewType(const size_t &index, ValueObject &leafItem, bool &viewType) { switch (leafItem.GetValueType()) { case ValueObject::ValueType::VALUE_BOOL: @@ -712,7 +712,7 @@ int FindArgsCheck(const std::string &collection, const std::string &filter, cons return errCode; } -int FindProjectionInit(const std::string &projection, std::shared_ptr &context) +int FindProjectionInit(const std::string &projection, const std::shared_ptr &context) { int errCode = E_OK; std::vector> allPath; 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 a1b73353..2b97323a 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 @@ -126,12 +126,11 @@ int ResultSet::GetNextInner(bool isNeedCheckTable) int ResultSet::GetNext(bool isNeedTransaction, bool isNeedCheckTable) { - int errCode = E_OK; if (!isNeedTransaction) { return GetNextInner(isNeedCheckTable); } std::lock_guard lock(store_->dbMutex_); - errCode = store_->StartTransaction(); + int errCode = store_->StartTransaction(); if (errCode != E_OK) { GLOGE("Start transaction faild"); return errCode; -- Gitee From c4b8bc11aebad2a6d1eabe3af18d8d818f3378f8 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Fri, 2 Jun 2023 14:45:03 +0800 Subject: [PATCH 54/79] fix memory problem 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 a54ca478..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 @@ -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 1a32f2e252fadca57c30740e9066a4b1a4697e35 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Fri, 2 Jun 2023 14:52:31 +0800 Subject: [PATCH 55/79] add ut Signed-off-by: Jeremyzz --- .../gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp | 6 ++++++ 1 file changed, 6 insertions(+) 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 c3ad96cb..385470b2 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 @@ -826,4 +826,10 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest046, TestSize.Level1) const char *document1 = R""({})""; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); } + +HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest047, TestSize.Level1) +{ + const char *document1 = R""({"empty" : null})""; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); +} } // namespace -- Gitee From 3f7daffc5c60389d8051f3be199f0f952165112e Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 1 Jun 2023 11:30:49 +0800 Subject: [PATCH 56/79] fix upsert trancefaction bug Signed-off-by: Jeremyzz --- .../src/interface/src/document_store.cpp | 2 +- .../unittest/api/documentdb_data_test.cpp | 28 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 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 24b1ed3c..78bad688 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 @@ -404,7 +404,7 @@ int DocumentStore::UpsertDataIntoDB(std::shared_ptr &context, Json } errCode = InsertIdToDocument(resultSet, filterObj, documentObj, docId); if (errCode != E_OK) { - return errCode; + goto END; } errCode = CheckUpsertConflict(resultSet, filterObj, docId, coll, isDataExist); // There are only three return values, the two other situation can continue to move forward. diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp index 1ea370b5..565dd8e2 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp @@ -134,7 +134,7 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest006, TestSize.Level0) std::string filter = R""({"_id":"1234"})""; std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - for (auto flags : std::vector { 2, 4, 8, 64, 1024, UINT32_MAX }) { + for (auto flags : std::vector{ 2, 4, 8, 64, 1024, UINT32_MAX }) { EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), flags), GRD_INVALID_ARGS); } } @@ -191,6 +191,32 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest011, TestSize.Level0) ASSERT_EQ(result, GRD_INVALID_FORMAT); } +/** + * @tc.name: UpdateDataTest012 + * @tc.desc: Input parameter collectionName is null, invoke the GRD_UpsertDoc interface to update data. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBDataTest, UpsertDataTest012, TestSize.Level0) +{ + /** + * @tc.steps: step1. Insert a document. + * @tc.expected: step1. return GRD_OK. + */ + int result = GRD_InsertDoc(g_db, g_coll, "{}", 0); + ASSERT_EQ(result, GRD_OK); + /** + * @tc.steps: step2. Parameter collectionName is Invalid format + * @tc.expected: step2. return Update faild. + */ + result = GRD_UpsertDoc(g_db, "null", "{}", "{}", 1); + ASSERT_EQ(result, GRD_INVALID_ARGS); + + result = GRD_UpsertDoc(g_db, "!! &%$^%$&*%^。m中文、、请问E:112423123", "{}", "{}", 1); + ASSERT_EQ(result, GRD_INVALID_ARGS); +} + /** * @tc.name: UpdateDataTest001 * @tc.desc: -- Gitee From 507966ed037fd627085fc9ad945eeb401f067108 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 1 Jun 2023 14:50:16 +0800 Subject: [PATCH 57/79] fix lens bug Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/interface/src/document_store.cpp | 4 ++++ .../test/unittest/api/documentdb_data_test.cpp | 10 +++++++--- 2 files changed, 11 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 78bad688..01ca996c 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 @@ -330,6 +330,10 @@ int GetUpsertRePlaceData(ResultSet &resultSet, JsonObject &documentObj, bool isR int errCode = resultSet.GetValue(valStr); if (errCode != E_OK || isReplace) { valStr = documentObj.Print(); // If cant not find data, insert it. + if (valStr.length() >= JSON_LENS_MAX) { + GLOGE("document's length is too long"); + return -E_OVER_LIMIT; + } return E_OK; } if (errCode != E_OK && errCode != -E_NOT_FOUND) { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp index 565dd8e2..bcfe0791 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp @@ -32,7 +32,7 @@ namespace { std::string g_path = "./document.db"; GRD_DB *g_db = nullptr; const char *g_coll = "student"; - +constexpr int JSON_LENS_MAX = 1024 * 1024; class DocumentDBDataTest : public testing::Test { public: static void SetUpTestCase(void); @@ -174,8 +174,12 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest008, TestSize.Level0) HWTEST_F(DocumentDBDataTest, UpsertDataTest009, TestSize.Level0) { std::string filter = R""({"_id":"abcde"})""; - std::string document = R"({"field1": ")" + string(1024 * 1024 + 1, 'a') + "\"}"; - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), GRD_DOC_REPLACE), GRD_OVER_LIMIT); + std::string head = R"({"field1": ")"; + std::string document = + head + string(JSON_LENS_MAX - filter.size() - head.size() - 1, 'a') + "\"}"; // 13 is {"field1": size + EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), GRD_DOC_APPEND), 1); + std::string document2 = head + string(JSON_LENS_MAX - filter.size() - head.size(), 'a') + "\"}"; + EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document2.c_str(), GRD_DOC_REPLACE), GRD_OVER_LIMIT); } HWTEST_F(DocumentDBDataTest, UpsertDataTest010, TestSize.Level0) -- Gitee From fe07eb6c38a69d706f0eb135e55d6654f2606853 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 1 Jun 2023 14:52:37 +0800 Subject: [PATCH 58/79] fix Signed-off-by: Jeremyzz --- .../gaussdb_rd/test/unittest/api/documentdb_data_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp index bcfe0791..abcdd0c9 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp @@ -134,7 +134,7 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest006, TestSize.Level0) std::string filter = R""({"_id":"1234"})""; std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - for (auto flags : std::vector{ 2, 4, 8, 64, 1024, UINT32_MAX }) { + for (auto flags : std::vector { 2, 4, 8, 64, 1024, UINT32_MAX }) { EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), flags), GRD_INVALID_ARGS); } } -- Gitee From 4ff6cb3306640f92c35cf263ae676ad92b25efcd Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 5 Jun 2023 19:57:28 +0800 Subject: [PATCH 59/79] fix code check opinion Signed-off-by: Jeremyzz --- .../oh_adapter/include/kv_store_executor.h | 6 +++--- .../src/sqlite_store_executor_impl.cpp | 19 +++++++++++-------- .../src/sqlite_store_executor_impl.h | 6 +++--- 3 files changed, 17 insertions(+), 14 deletions(-) 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 c32cf826..5fb869c5 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, Key &key, const Value &value) = 0; - virtual int InsertData(const std::string &collName, Key &key, const Value &value) = 0; + virtual int PutData(const std::string &collName, Key &key, const Value &value, bool isNeedAddKeyType = true) = 0; + virtual int InsertData(const std::string &collName, Key &key, const Value &value, bool isNeedAddKeyType = true) = 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; + std::pair &values, int isIdExist) const = 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; 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 da40bfe6..e66b4d83 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 @@ -75,7 +75,6 @@ int SqliteStoreExecutorImpl::GetDBConfig(std::string &config) { std::string dbConfigKeyStr = "DB_CONFIG"; Key dbConfigKey = { dbConfigKeyStr.begin(), dbConfigKeyStr.end() }; - dbConfigKey.push_back(KEY_TYPE); // Push back Key Tpye, make it become really Key. Value dbConfigVal; int errCode = GetDataByKey("grd_meta", dbConfigKey, dbConfigVal); config.assign(dbConfigVal.begin(), dbConfigVal.end()); @@ -87,7 +86,7 @@ int SqliteStoreExecutorImpl::SetDBConfig(const std::string &config) std::string dbConfigKeyStr = "DB_CONFIG"; Key dbConfigKey = { dbConfigKeyStr.begin(), dbConfigKeyStr.end() }; Value dbConfigVal = { config.begin(), config.end() }; - return PutData("grd_meta", dbConfigKey, dbConfigVal); + return PutData("grd_meta", dbConfigKey, dbConfigVal, false); // dont need to add Key type; } int SqliteStoreExecutorImpl::StartTransaction() @@ -105,12 +104,14 @@ int SqliteStoreExecutorImpl::Rollback() return SQLiteUtils::RollbackTransaction(dbHandle_); } -int SqliteStoreExecutorImpl::PutData(const std::string &collName, Key &key, const Value &value) +int SqliteStoreExecutorImpl::PutData(const std::string &collName, Key &key, const Value &value, bool isNeedAddKeyType) { if (dbHandle_ == nullptr) { return -E_ERROR; } - key.push_back(KEY_TYPE); // Stitching ID type + if (isNeedAddKeyType) { + key.push_back(KEY_TYPE); // Stitching ID type + } std::string sql = "INSERT OR REPLACE INTO '" + collName + "' VALUES (?,?);"; int errCode = SQLiteUtils::ExecSql( dbHandle_, sql, @@ -131,12 +132,15 @@ int SqliteStoreExecutorImpl::PutData(const std::string &collName, Key &key, cons return E_OK; } -int SqliteStoreExecutorImpl::InsertData(const std::string &collName, Key &key, const Value &value) +int SqliteStoreExecutorImpl::InsertData(const std::string &collName, Key &key, const Value &value, + bool isNeedAddKeyType) { if (dbHandle_ == nullptr) { return -E_ERROR; } - key.push_back(KEY_TYPE); + if (isNeedAddKeyType) { + key.push_back(KEY_TYPE); // Stitching ID type + } std::string sql = "INSERT INTO '" + collName + "' VALUES (?,?);"; int errCode = SQLiteUtils::ExecSql( dbHandle_, sql, @@ -395,7 +399,6 @@ int SqliteStoreExecutorImpl::GetCollectionOption(const std::string &name, std::s { std::string collOptKeyStr = "COLLECTION_OPTION_" + name; Key collOptKey = { collOptKeyStr.begin(), collOptKeyStr.end() }; - collOptKey.push_back(KEY_TYPE); // Push back Key Tpye, make it become really Key. Value collOptVal; int errCode = GetDataByKey("grd_meta", collOptKey, collOptVal); option.assign(collOptVal.begin(), collOptVal.end()); @@ -407,7 +410,7 @@ int SqliteStoreExecutorImpl::SetCollectionOption(const std::string &name, const std::string collOptKeyStr = "COLLECTION_OPTION_" + name; Key collOptKey = { collOptKeyStr.begin(), collOptKeyStr.end() }; Value collOptVal = { option.begin(), option.end() }; - return PutData("grd_meta", collOptKey, collOptVal); + return PutData("grd_meta", collOptKey, collOptVal, false); // dont need to add key type; } int SqliteStoreExecutorImpl::CleanCollectionOption(const std::string &name) 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 3d8fbd99..fcc28498 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, Key &key, const Value &value) override; - int InsertData(const std::string &collName, Key &key, const Value &value) override; + int PutData(const std::string &collName, Key &key, const Value &value, bool isNeedAddKeyType = true) override; + int InsertData(const std::string &collName, Key &key, const Value &value, bool isNeedAddKeyType = true) 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; + std::pair &values, int isIdExist) const override; int DelData(const std::string &collName, Key &key) override; int CreateCollection(const std::string &name, const std::string &option, bool ignoreExists) override; -- Gitee From 25fd93286471bbf29382e1a32c69363f5f0de11d Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 5 Jun 2023 10:03:22 +0800 Subject: [PATCH 60/79] reserve some question Signed-off-by: Jeremyzz --- .../interface/src/document_store_manager.cpp | 2 +- .../test/unittest/api/documentdb_api_test.cpp | 19 ++----------------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store_manager.cpp index c9a91cec..138c86ab 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store_manager.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/document_store_manager.cpp @@ -133,7 +133,7 @@ int DocumentStoreManager::CheckDBPath(const std::string &path, std::string &cano if (path.back() == '/') { GLOGE("Invalid path end with slash"); - return -E_FAILED_FILE_OPERATION; + return -E_INVALID_ARGS; } std::string dirPath; 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 c127bfed..5c90ab0c 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 @@ -156,7 +156,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBTest004, TestSize.Level0) HWTEST_F(DocumentDBApiTest, OpenDBPathTest001, TestSize.Level0) { GRD_DB *db = nullptr; - std::vector invalidPath = { nullptr, ""}; + std::vector invalidPath = { nullptr, "" }; for (auto path : invalidPath) { GLOGD("OpenDBPathTest001: open db with path: %s", path); int status = GRD_DBOpen(path, nullptr, GRD_DB_OPEN_CREATE, &db); @@ -179,21 +179,6 @@ HWTEST_F(DocumentDBApiTest, OpenDBPathTest002, TestSize.Level0) EXPECT_EQ(status, GRD_FAILED_FILE_OPERATION); } -/** - * @tc.name: OpenDBPathTest003 - * @tc.desc: call GRD_DBOpen, input dbFile end with '\' - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDBApiTest, OpenDBPathTest003, TestSize.Level0) -{ - GRD_DB *db = nullptr; - std::string pathNoPerm = "/root/document.db/"; - int status = GRD_DBOpen(pathNoPerm.c_str(), nullptr, GRD_DB_OPEN_CREATE, &db); - EXPECT_EQ(status, GRD_FAILED_FILE_OPERATION); -} - /** * @tc.name: OpenDBPathTest004 * @tc.desc: call GRD_DBOpen, input dbFile as existed menu @@ -411,7 +396,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 fd713843339fe303477cae2ca4947a1982c8a3f8 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 1 Jun 2023 10:18:35 +0800 Subject: [PATCH 61/79] FixConfigLensBug Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/common/include/doc_limit.h | 2 +- .../test/unittest/api/documentdb_api_test.cpp | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/doc_limit.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/doc_limit.h index 557b92da..fc2515f3 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/doc_limit.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/doc_limit.h @@ -17,6 +17,6 @@ #define DOC_LIMIT_H namespace DocumentDB { -constexpr int MAX_DB_CONFIG_LEN = 512 * 1024; // 512 * 1024: 512k length +constexpr int MAX_DB_CONFIG_LEN = 1024 * 1024; // 1024 * 1024: 1024k length } // namespace DocumentDB #endif // DOC_LIMIT_H \ No newline at end of file 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 5c90ab0c..b07eb35d 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 @@ -241,6 +241,35 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigTest003, TestSize.Level0) EXPECT_EQ(status, GRD_INVALID_ARGS); } +/** + * @tc.name: OpenDBConfigTest004 + * @tc.desc: call GRD_DBOpen, input the value's length of configStr is 1024K + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBApiTest, OpenDBConfigTest004, TestSize.Level0) +{ + /** + * @tc.steps:step1. input the value's length of configStr is 1024 k(not contained '\0') + */ + GRD_DB *db = nullptr; + std::string part1 = "{ \"pageSize\": \" "; + std::string part2 = "\" }"; + std::string path = "./document.db"; + std::string val = string(1024 * 1024 - part1.size() - part2.size(), 'k'); + std::string configStr = part1 + val + part2; + int ret = GRD_DBOpen(path.c_str(), configStr.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(ret, GRD_OVER_LIMIT); + /** + * @tc.steps:step2. input the value's length of configStr is 1024 k(contained '\0') + */ + std::string val2 = string(1024 * 1024 - part1.size() - part2.size() - 1, 'k'); + std::string configStr2 = part1 + val2 + part2 + "\0"; + ret = GRD_DBOpen(path.c_str(), configStr2.c_str(), GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(ret, GRD_INVALID_ARGS); +} + /** * @tc.name: OpenDBConfigMaxConnNumTest001 * @tc.desc: Test open document db with invalid config item maxConnNum -- Gitee From 7b0ea769ef6edc7b1debe246ed4f4a58393ae826 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 1 Jun 2023 15:24:48 +0800 Subject: [PATCH 62/79] fix code check Signed-off-by: Jeremyzz --- .../gaussdb_rd/test/unittest/api/documentdb_api_test.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 b07eb35d..eaba2210 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 @@ -28,6 +28,7 @@ using namespace testing::ext; using namespace DocumentDBUnitTest; namespace { +const int MAX_DB_CONFIG_LEN = 1024 * 1024; class DocumentDBApiTest : public testing::Test { public: static void SetUpTestCase(void); @@ -257,14 +258,14 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigTest004, TestSize.Level0) std::string part1 = "{ \"pageSize\": \" "; std::string part2 = "\" }"; std::string path = "./document.db"; - std::string val = string(1024 * 1024 - part1.size() - part2.size(), 'k'); + std::string val = string(MAX_DB_CONFIG_LEN - part1.size() - part2.size(), 'k'); std::string configStr = part1 + val + part2; int ret = GRD_DBOpen(path.c_str(), configStr.c_str(), GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(ret, GRD_OVER_LIMIT); /** * @tc.steps:step2. input the value's length of configStr is 1024 k(contained '\0') */ - std::string val2 = string(1024 * 1024 - part1.size() - part2.size() - 1, 'k'); + std::string val2 = string(MAX_DB_CONFIG_LEN - part1.size() - part2.size() - 1, 'k'); std::string configStr2 = part1 + val2 + part2 + "\0"; ret = GRD_DBOpen(path.c_str(), configStr2.c_str(), GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(ret, GRD_INVALID_ARGS); -- Gitee From 4ad6670f7698dc2661d2f70d5d11265d1a5929c8 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 5 Jun 2023 11:22:08 +0800 Subject: [PATCH 63/79] fix code checkout opinion Signed-off-by: Jeremyzz --- .../gaussdb_rd/test/unittest/api/documentdb_api_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 eaba2210..be3e7df4 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 @@ -16,6 +16,7 @@ #include #include "doc_errno.h" +#include "doc_limit.h" #include "documentdb_test_utils.h" #include "grd_base/grd_db_api.h" #include "grd_base/grd_error.h" @@ -28,7 +29,6 @@ using namespace testing::ext; using namespace DocumentDBUnitTest; namespace { -const int MAX_DB_CONFIG_LEN = 1024 * 1024; class DocumentDBApiTest : public testing::Test { public: static void SetUpTestCase(void); -- Gitee From ea2f016c43b97dd65658cedff9fcbabcc2ed99b3 Mon Sep 17 00:00:00 2001 From: mazhao Date: Tue, 13 Jun 2023 14:32:05 +0800 Subject: [PATCH 64/79] fix fifth white box Signed-off-by: mazhao --- .../gaussdb_rd/src/common/src/db_config.cpp | 2 +- .../gaussdb_rd/src/interface/src/document_key.cpp | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/db_config.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/db_config.cpp index 1ec53e65..5061139e 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/db_config.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/db_config.cpp @@ -57,7 +57,7 @@ bool CheckAndGetDBConfig(const JsonObject &config, const std::string &name, cons int errCode = E_OK; ValueObject configValue = config.GetObjectByPath(configField, errCode); if (errCode != E_OK) { - GLOGE("Cant find config Value"); + GLOGE("Can not find config Value"); return errCode; } if (configValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) { 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..4d676a49 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 @@ -20,7 +20,8 @@ namespace DocumentDB { static uint16_t g_oIdIncNum = 0; - +constexpr uint16_t MAX_NUMBER_OF_AUTOINCREMENTS = 65535; +constexpr uint16_t UINT_ZERO = 0; static int InitDocIdFromOid(DocKey &docKey) { time_t nowTime = time(nullptr); @@ -30,8 +31,8 @@ static int InitDocIdFromOid(DocKey &docKey) 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; + if (g_oIdIncNum > MAX_NUMBER_OF_AUTOINCREMENTS) { + g_oIdIncNum = UINT_ZERO; } 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) { @@ -46,11 +47,6 @@ static int InitDocIdFromOid(DocKey &docKey) int DocumentKey::GetOidDocKey(DocKey &key) { int ret = InitDocIdFromOid(key); - { - if (ret != E_OK) { - return ret; - } - } return ret; } -- Gitee From 4b5569d7a9067c4557c90097b4ee164ccc299498 Mon Sep 17 00:00:00 2001 From: mazhao Date: Mon, 19 Jun 2023 16:49:17 +0800 Subject: [PATCH 65/79] delete uselessFuc of gassus_rd Signed-off-by: mazhao --- .../src/common/include/collection_option.h | 3 - .../src/common/src/collection_option.cpp | 20 ----- .../src/interface/include/collection.h | 2 - .../src/interface/src/collection.cpp | 6 -- .../src/oh_adapter/include/json_object.h | 4 - .../oh_adapter/include/kv_store_executor.h | 1 - .../src/oh_adapter/src/json_object.cpp | 84 ------------------- .../src/sqlite_store_executor_impl.cpp | 10 --- .../src/sqlite_store_executor_impl.h | 1 - .../src/oh_adapter/src/sqlite_utils.cpp | 24 ------ .../src/oh_adapter/src/sqlite_utils.h | 1 - .../test/unittest/api/documentdb_api_test.cpp | 2 +- .../unittest/api/documentdb_data_test.cpp | 9 +- 13 files changed, 5 insertions(+), 162 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/collection_option.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/collection_option.h index 8f982807..6062f9fe 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/collection_option.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/collection_option.h @@ -23,9 +23,6 @@ class CollectionOption final { public: static CollectionOption ReadOption(const std::string &optStr, int &errCode); - uint32_t GetMaxDoc() const; - std::string ToString() const; - bool operator==(const CollectionOption &targetOption) const; bool operator!=(const CollectionOption &targetOption) const; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/collection_option.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/collection_option.cpp index 324a2931..d5310f73 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/collection_option.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/collection_option.cpp @@ -92,24 +92,4 @@ CollectionOption CollectionOption::ReadOption(const std::string &optStr, int &er option.option_ = optStr; return option; } - -uint32_t CollectionOption::GetMaxDoc() const -{ - return maxDoc_; -} - -std::string CollectionOption::ToString() const -{ - return option_; -} - -bool CollectionOption::operator==(const CollectionOption &targetOption) const -{ - return maxDoc_ == targetOption.maxDoc_; -} - -bool CollectionOption::operator!=(const CollectionOption &targetOption) const -{ - return !(*this == targetOption); -} } // namespace DocumentDB \ No newline at end of file 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 a2a41180..30d109ed 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 @@ -25,8 +25,6 @@ namespace DocumentDB { class Collection { public: Collection(const std::string &name, KvStoreExecutor *executor); - Collection(const Collection &other); - Collection(){}; ~Collection(); int InsertDocument(const std::string &id, const std::string &document, bool &isIdExist); 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 09173698..6f852abe 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 @@ -33,12 +33,6 @@ Collection::Collection(const std::string &name, KvStoreExecutor *executor) : exe name_ = DBConstant::COLL_PREFIX + lowerCaseName; } -Collection::Collection(const Collection &other) -{ - name_ = other.name_; - executor_ = other.executor_; -} - Collection::~Collection() { executor_ = nullptr; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h index afd0489d..664bd439 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h @@ -68,20 +68,17 @@ public: std::string Print() const; JsonObject GetObjectItem(const std::string &field, int &errCode); - JsonObject GetArrayItem(int index, int &errCode); JsonObject GetNext() const; JsonObject GetChild() const; int DeleteItemFromObject(const std::string &field); - int AddItemToObject(const JsonObject &item); int AddItemToObject(const std::string &fieldName, const JsonObject &item); int AddItemToObject(const std::string &fieldName); ValueObject GetItemValue() const; void ReplaceItemInArray(const int &index, const JsonObject &newItem, int &errCode); void ReplaceItemInObject(const std::string &fieldName, const JsonObject &newItem, int &errCode); - void SetItemValue(const ValueObject &value) const; int InsertItemObject(int which, const JsonObject &newItem); std::string GetItemField() const; @@ -92,7 +89,6 @@ public: JsonObject FindItem(const JsonFieldPath &jsonPath, int &errCode) const; JsonObject FindItemPowerMode(const JsonFieldPath &jsonPath, int &errCode) const; ValueObject GetObjectByPath(const JsonFieldPath &jsonPath, int &errCode) const; - int DeleteItemOnTarget(const JsonFieldPath &path); int DeleteItemDeeplyOnTarget(const JsonFieldPath &path); bool IsNull() const; int GetDeep(); 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 5fb869c5..a12bda14 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 @@ -41,7 +41,6 @@ public: virtual int DropCollection(const std::string &name, bool ignoreNonExists) = 0; virtual bool IsCollectionExists(const std::string &name, int &errCode) = 0; - virtual int GetCollectionOption(const std::string &name, std::string &option) = 0; virtual int SetCollectionOption(const std::string &name, const std::string &option) = 0; virtual int CleanCollectionOption(const std::string &name) = 0; }; 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 d52c517f..a61824e6 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 @@ -43,12 +43,6 @@ ValueObject::ValueObject(double val) doubleValue = val; } -ValueObject::ValueObject(const char *val) -{ - valueType = ValueType::VALUE_STRING; - stringValue = val; -} - ValueObject::ValueObject(const std::string &val) { valueType = ValueType::VALUE_STRING; @@ -273,22 +267,6 @@ JsonObject JsonObject::GetObjectItem(const std::string &field, int &errCode) return item; } -JsonObject JsonObject::GetArrayItem(int index, int &errCode) -{ - if (cjson_ == nullptr || cjson_->type != cJSON_Array) { - errCode = -E_INVALID_ARGS; - return JsonObject(); - } - - JsonObject item; - item.caseSensitive_ = caseSensitive_; - item.cjson_ = cJSON_GetArrayItem(cjson_, index); - if (item.cjson_ == nullptr) { - errCode = -E_NOT_FOUND; - } - return item; -} - JsonObject JsonObject::GetNext() const { if (cjson_ == nullptr) { @@ -326,18 +304,6 @@ int JsonObject::DeleteItemFromObject(const std::string &field) return E_OK; } -int JsonObject::AddItemToObject(const JsonObject &item) -{ - if (item.IsNull()) { - GLOGD("Add null object."); - return E_OK; - } - - cJSON *cpoyItem = cJSON_Duplicate(item.cjson_, true); - cJSON_AddItemToObject(cjson_, item.GetItemField().c_str(), cpoyItem); - return E_OK; -} - int JsonObject::AddItemToObject(const std::string &fieldName, const JsonObject &item) { if (cjson_ == nullptr) { @@ -443,23 +409,6 @@ void JsonObject::ReplaceItemInArray(const int &index, const JsonObject &newItem, } } -void JsonObject::SetItemValue(const ValueObject &value) const -{ - if (cjson_ == nullptr) { - return; - } - switch (value.GetValueType()) { - case ValueObject::ValueType::VALUE_NUMBER: - cJSON_SetNumberValue(cjson_, value.GetDoubleValue()); - break; - case ValueObject::ValueType::VALUE_STRING: - cJSON_SetValuestring(cjson_, value.GetStringValue().c_str()); - break; - default: - break; - } -} - int JsonObject::InsertItemObject(int which, const JsonObject &newItem) { if (cjson_ == nullptr) { @@ -645,39 +594,6 @@ ValueObject JsonObject::GetObjectByPath(const JsonFieldPath &jsonPath, int &errC return objGot.GetItemValue(); } -int JsonObject::DeleteItemOnTarget(const JsonFieldPath &path) -{ - if (path.empty()) { - return -E_INVALID_ARGS; - } - - std::string fieldName = path.back(); - JsonFieldPath patherPath = path; - patherPath.pop_back(); - - cJSON *nodeFather = MoveToPath(cjson_, patherPath, caseSensitive_); - if (nodeFather == nullptr) { - GLOGE("Delete item failed, json field path not found."); - return -E_JSON_PATH_NOT_EXISTS; - } - - if (nodeFather->type == cJSON_Object) { - if (caseSensitive_) { - cJSON_DeleteItemFromObjectCaseSensitive(nodeFather, fieldName.c_str()); - } else { - cJSON_DeleteItemFromObject(nodeFather, fieldName.c_str()); - } - } else if (nodeFather->type == cJSON_Array) { - if (!IsNumber(fieldName)) { - GLOGW("Invalid json field path, expect array index."); - return -E_JSON_PATH_NOT_EXISTS; - } - cJSON_DeleteItemFromArray(nodeFather, std::stoi(fieldName)); - } - - return E_OK; -} - int JsonObject::DeleteItemDeeplyOnTarget(const JsonFieldPath &path) { if (path.empty()) { 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 e66b4d83..39f34788 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 @@ -395,16 +395,6 @@ bool SqliteStoreExecutorImpl::IsCollectionExists(const std::string &name, int &e return isExists; } -int SqliteStoreExecutorImpl::GetCollectionOption(const std::string &name, std::string &option) -{ - std::string collOptKeyStr = "COLLECTION_OPTION_" + name; - Key collOptKey = { collOptKeyStr.begin(), collOptKeyStr.end() }; - Value collOptVal; - int errCode = GetDataByKey("grd_meta", collOptKey, collOptVal); - option.assign(collOptVal.begin(), collOptVal.end()); - return errCode; -} - int SqliteStoreExecutorImpl::SetCollectionOption(const std::string &name, const std::string &option) { std::string collOptKeyStr = "COLLECTION_OPTION_" + name; 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 fcc28498..8da9ed30 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 @@ -49,7 +49,6 @@ public: int DropCollection(const std::string &name, bool ignoreNonExists) override; bool IsCollectionExists(const std::string &name, int &errCode) override; - int GetCollectionOption(const std::string &name, std::string &option) override; int SetCollectionOption(const std::string &name, const std::string &option) override; int CleanCollectionOption(const std::string &name) 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 5281df56..d5e3d961 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 @@ -21,7 +21,6 @@ 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 std::string BEGIN_SQL = "BEGIN TRANSACTION"; const std::string BEGIN_IMMEDIATE_SQL = "BEGIN IMMEDIATE TRANSACTION"; @@ -209,29 +208,6 @@ int SQLiteUtils::BindTextToStatement(sqlite3_stmt *statement, int index, const s return E_OK; } -int SQLiteUtils::GetColumnTextValue(sqlite3_stmt *statement, int index, std::string &value) -{ - if (statement == nullptr) { - return -E_INVALID_ARGS; - } - - int valSize = sqlite3_column_bytes(statement, index); - 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 - } - - const unsigned char *val = sqlite3_column_text(statement, index); - if (valSize == 0 || val == nullptr) { - value = {}; - } else { - value = std::string(reinterpret_cast(val)); - } - - return E_OK; -} - int SQLiteUtils::BeginTransaction(sqlite3 *db, TransactType type) { if (type == TransactType::IMMEDIATE) { 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 96db4018..88cc77ef 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 @@ -40,7 +40,6 @@ public: static int GetColumnBlobValue(sqlite3_stmt *statement, int index, std::vector &value); static int BindTextToStatement(sqlite3_stmt *statement, int index, const std::string &value); - static int GetColumnTextValue(sqlite3_stmt *statement, int index, std::string &value); static int BeginTransaction(sqlite3 *db, TransactType type = TransactType::DEFERRED); static int CommitTransaction(sqlite3 *db); 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 489e28a9..93358885 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 @@ -206,7 +206,7 @@ HWTEST_F(DocumentDBApiTest, OpenDBConfigTest001, TestSize.Level0) { GRD_DB *db = nullptr; std::string path = "./document.db"; - const int MAX_JSON_LEN = 512 * 1024; + const int MAX_JSON_LEN = 1024 * 1024; std::string configStr = std::string(MAX_JSON_LEN, 'a'); int status = GRD_DBOpen(path.c_str(), configStr.c_str(), GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OVER_LIMIT); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp index abcdd0c9..781bbb2b 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp @@ -134,7 +134,7 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest006, TestSize.Level0) std::string filter = R""({"_id":"1234"})""; std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - for (auto flags : std::vector { 2, 4, 8, 64, 1024, UINT32_MAX }) { + for (auto flags : std::vector{ 2, 4, 8, 64, 1024, UINT32_MAX }) { EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), flags), GRD_INVALID_ARGS); } } @@ -333,11 +333,11 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest008, TestSize.Level0) HWTEST_F(DocumentDBDataTest, UpdateDataTest009, TestSize.Level0) { std::string filter = R""({"_id":"1234"})""; - std::string document = R""({"_id":"1234", "field1":{"c_field":{"cc_field":{"ccc_field":1}}}, "field2" : 2})""; + std::string document = R""({"_id":"1234","field1":{"c_field":{"cc_field":{"ccc_field":1}}},"field2":2})""; EXPECT_EQ(GRD_InsertDoc(g_db, g_coll, document.c_str(), 0), GRD_OK); - std::string updata = R""({"field1":1, "FIELD1":[1, true, 1.23456789, "hello world!", null]})""; + std::string updata = R""({"field1":1,"FIELD1":[1,true,1.23456789,"hello world!",null]})""; EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), updata.c_str(), 0), 1); GRD_ResultSet *resultSet = nullptr; @@ -348,8 +348,7 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest009, TestSize.Level0) char *value = nullptr; EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); string valueStr = value; - string repectStr = R""({"_id":"1234", "field1":1, "field2":2, - "FIELD1":[1, true, 1.23456789, "hello world!", null]})""; + string repectStr = R""({"_id":"1234","field1":1,"field2":2,"FIELD1":[1,true,1.23456789,"hello world!",null]})""; EXPECT_EQ((valueStr == repectStr), 1); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -- Gitee From e6e068637e28b9e08566d4b1c409a2080ec1e4ba Mon Sep 17 00:00:00 2001 From: mazhao Date: Mon, 26 Jun 2023 20:44:00 +0800 Subject: [PATCH 66/79] fix crash problem Signed-off-by: mazhao --- .../src/interface/src/result_set.cpp | 24 ++++---- .../unittest/api/documentdb_find_test.cpp | 56 +++++++++++++++++++ 2 files changed, 68 insertions(+), 12 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 2b97323a..2ab4fa81 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 @@ -194,19 +194,19 @@ int ResultSet::CheckCutNode(JsonObject *node, std::vector singlePat GLOGE("No node to cut"); return -E_NO_DATA; } - singlePath.emplace_back(node->GetItemField()); - size_t index = 0; - if (!context_->projectionTree.SearchTree(singlePath, index) && index == 0) { - allCutPath.emplace_back(singlePath); - } - if (!node->GetChild().IsNull()) { - JsonObject nodeNew = node->GetChild(); - CheckCutNode(&nodeNew, singlePath, allCutPath); - } - if (!node->GetNext().IsNull()) { + JsonObject nodeInstance = *node; + while (!nodeInstance.IsNull()) { + singlePath.emplace_back(nodeInstance.GetItemField()); + size_t index = 0; + if (!context_->projectionTree.SearchTree(singlePath, index) && index == 0) { + allCutPath.emplace_back(singlePath); + } + if (!nodeInstance.GetChild().IsNull()) { + JsonObject nodeChiled = nodeInstance.GetChild(); + CheckCutNode(&nodeChiled, singlePath, allCutPath); + } singlePath.pop_back(); - JsonObject nodeNew = node->GetNext(); - CheckCutNode(&nodeNew, singlePath, allCutPath); + nodeInstance = nodeInstance.GetNext(); } return E_OK; } 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 50ac7575..0bcae90e 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 @@ -78,6 +78,17 @@ static const char *g_document20 = "{\"_id\" : \"20\", \"name\":\"doc20\",\"ITEM\ static const char *g_document23 = "{\"_id\" : \"23\", \"name\":\"doc22\",\"ITEM\" : " "true,\"personInfo\":[{\"school\":\"b\", \"age\":15}, [{\"school\":\"doc23\"}, 10, " "{\"school\":\"doc23\"}, true, {\"school\":\"y\"}], {\"school\":\"b\"}]}"; +static const char *document0631 = R""({"_id":"city_11_com.acts.ohos.data.datasharetestclient_100", + "bundleName":"com.acts.ohos.data.datasharetestclient","key":"city","subscriberId":11, + "timestamp":1509100700,"userId":100,"value":{"type":1,"value":"xian"},"version":0})""; +static const char *document0632 = R""({"_id":"datashareproxy://com.acts.ohos.data.datasharetest/appInfo_11_com.acts.oh + os.data.datasharetest_100","bundleName":"com.acts.ohos.data.datasharetest","key":"datashareproxy://com.acts.ohos + .data.datasharetest/appInfo","subscriberId":11,"timestamp":1509100700,"userId":100, + "value":{"type":1,"value":"\"Qing\""},"version":0})""; +static const char *document0633 = + R""({"_id":"empty_11_com.acts.ohos.data.datasharetestclient_100","bundleName":"com.acts.ohos.data.datasharetestcl + ient","key":"empty","subscriberId":11,"timestamp":1509100700,"userId":100,"value":{"type":1,"value":"nobody sub"}, + "version":0})""; static std::vector g_data = { g_document1, g_document2, g_document3, g_document4, g_document5, g_document6, g_document7, g_document8, g_document9, g_document10, g_document11, g_document12, g_document13, g_document14, g_document15, g_document16, g_document17, g_document18, g_document19, g_document20, g_document23 }; @@ -1493,4 +1504,49 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest063, TestSize.Level1) EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } + +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest064, TestSize.Level1) +{ + char *colName1 = "data_"; + GRD_DB *test_db = nullptr; + std::string path = "./dataShare.db"; + int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &test_db); + EXPECT_EQ(status, GRD_OK); + EXPECT_EQ(GRD_CreateCollection(test_db, colName1, "", 0), GRD_OK); + + EXPECT_EQ(GRD_InsertDoc(test_db, colName1, document0631, 0), GRD_OK); + EXPECT_EQ(GRD_InsertDoc(test_db, colName1, document0632, 0), GRD_OK); + EXPECT_EQ(GRD_InsertDoc(test_db, colName1, document0633, 0), GRD_OK); + + string document1 = "{\"_id\":\"key2_11_com.acts.ohos.data.datasharetestclient_100\"\ + ,\"bundleName\":\"com.acts.ohos.data.datasharetestclient\"\ + ,\"key\":\"key2\",\"subscriberId\":11,\"timestamp\":1509100700," + "\"userId\":100,\"value\":{\"type\":0,"; + string document2 = "\"value\":["; + string document3 = "5,"; + string document4 = document3; + for (int i = 0; i < 100000; i++) { + document4 += document3; + } + document4.push_back('5'); + string document5 = "]}}"; + string document0635 = document1 + document2 + document4 + document5; + EXPECT_EQ(GRD_InsertDoc(test_db, colName1, document0635.c_str(), 0), GRD_OK); + EXPECT_EQ(status, GRD_OK); + const char *filter = "{}"; + GRD_ResultSet *resultSet = nullptr; + const char *projection = "{\"id_\":true, \"timestamp\":true, \"key\":true, \"bundleName\": true, " + "\"subscriberId\": true}"; + Query query = { filter, projection }; + EXPECT_EQ(GRD_FindDoc(test_db, colName1, query, 1, &resultSet), GRD_OK); + char *value; + while (GRD_Next(resultSet) == GRD_OK) { + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + printf("value is ======>%s\n", value); + GRD_FreeValue(value); + } + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + EXPECT_EQ(GRD_DBClose(test_db, 0), GRD_OK); + DocumentDBTestUtils::RemoveTestDbFiles(path.c_str()); +} } // namespace -- Gitee From e73f0e970cec6c9473dc5520c819b72a8fd23758 Mon Sep 17 00:00:00 2001 From: mazhao Date: Tue, 27 Jun 2023 10:32:13 +0800 Subject: [PATCH 67/79] delete fuc Signed-off-by: mazhao --- .../src/oh_adapter/include/json_object.h | 1 - .../src/oh_adapter/src/json_object.cpp | 2 +- .../unittest/api/documentdb_find_test.cpp | 24 ++++--------------- 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h index 664bd439..4f5efa4a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h @@ -37,7 +37,6 @@ public: explicit ValueObject(bool val); explicit ValueObject(double val); explicit ValueObject(const char *val); - explicit ValueObject(const std::string &val); ValueType GetValueType() const; bool GetBoolValue() const; 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 a61824e6..5130029d 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 @@ -43,7 +43,7 @@ ValueObject::ValueObject(double val) doubleValue = val; } -ValueObject::ValueObject(const std::string &val) +ValueObject::ValueObject(const char *val) { valueType = ValueType::VALUE_STRING; stringValue = val; 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 0bcae90e..ea1586be 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 @@ -35,6 +35,7 @@ namespace { std::string g_path = "./document.db"; GRD_DB *g_db = nullptr; constexpr const char *COLLECTION_NAME = "student"; +constexpr const char *colName = "data_"; const int MAX_COLLECTION_NAME = 511; const int MAX_ID_LENS = 899; @@ -78,17 +79,6 @@ static const char *g_document20 = "{\"_id\" : \"20\", \"name\":\"doc20\",\"ITEM\ static const char *g_document23 = "{\"_id\" : \"23\", \"name\":\"doc22\",\"ITEM\" : " "true,\"personInfo\":[{\"school\":\"b\", \"age\":15}, [{\"school\":\"doc23\"}, 10, " "{\"school\":\"doc23\"}, true, {\"school\":\"y\"}], {\"school\":\"b\"}]}"; -static const char *document0631 = R""({"_id":"city_11_com.acts.ohos.data.datasharetestclient_100", - "bundleName":"com.acts.ohos.data.datasharetestclient","key":"city","subscriberId":11, - "timestamp":1509100700,"userId":100,"value":{"type":1,"value":"xian"},"version":0})""; -static const char *document0632 = R""({"_id":"datashareproxy://com.acts.ohos.data.datasharetest/appInfo_11_com.acts.oh - os.data.datasharetest_100","bundleName":"com.acts.ohos.data.datasharetest","key":"datashareproxy://com.acts.ohos - .data.datasharetest/appInfo","subscriberId":11,"timestamp":1509100700,"userId":100, - "value":{"type":1,"value":"\"Qing\""},"version":0})""; -static const char *document0633 = - R""({"_id":"empty_11_com.acts.ohos.data.datasharetestclient_100","bundleName":"com.acts.ohos.data.datasharetestcl - ient","key":"empty","subscriberId":11,"timestamp":1509100700,"userId":100,"value":{"type":1,"value":"nobody sub"}, - "version":0})""; static std::vector g_data = { g_document1, g_document2, g_document3, g_document4, g_document5, g_document6, g_document7, g_document8, g_document9, g_document10, g_document11, g_document12, g_document13, g_document14, g_document15, g_document16, g_document17, g_document18, g_document19, g_document20, g_document23 }; @@ -1507,16 +1497,11 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest063, TestSize.Level1) HWTEST_F(DocumentDBFindTest, DocumentDBFindTest064, TestSize.Level1) { - char *colName1 = "data_"; GRD_DB *test_db = nullptr; std::string path = "./dataShare.db"; int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &test_db); EXPECT_EQ(status, GRD_OK); - EXPECT_EQ(GRD_CreateCollection(test_db, colName1, "", 0), GRD_OK); - - EXPECT_EQ(GRD_InsertDoc(test_db, colName1, document0631, 0), GRD_OK); - EXPECT_EQ(GRD_InsertDoc(test_db, colName1, document0632, 0), GRD_OK); - EXPECT_EQ(GRD_InsertDoc(test_db, colName1, document0633, 0), GRD_OK); + EXPECT_EQ(GRD_CreateCollection(test_db, colName, "", 0), GRD_OK); string document1 = "{\"_id\":\"key2_11_com.acts.ohos.data.datasharetestclient_100\"\ ,\"bundleName\":\"com.acts.ohos.data.datasharetestclient\"\ @@ -1531,18 +1516,17 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest064, TestSize.Level1) document4.push_back('5'); string document5 = "]}}"; string document0635 = document1 + document2 + document4 + document5; - EXPECT_EQ(GRD_InsertDoc(test_db, colName1, document0635.c_str(), 0), GRD_OK); + EXPECT_EQ(GRD_InsertDoc(test_db, colName, document0635.c_str(), 0), GRD_OK); EXPECT_EQ(status, GRD_OK); const char *filter = "{}"; GRD_ResultSet *resultSet = nullptr; const char *projection = "{\"id_\":true, \"timestamp\":true, \"key\":true, \"bundleName\": true, " "\"subscriberId\": true}"; Query query = { filter, projection }; - EXPECT_EQ(GRD_FindDoc(test_db, colName1, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_FindDoc(test_db, colName, query, 1, &resultSet), GRD_OK); char *value; while (GRD_Next(resultSet) == GRD_OK) { EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - printf("value is ======>%s\n", value); GRD_FreeValue(value); } EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -- Gitee From 49f238d38e6a6f5770715038f8056faa61c2177c Mon Sep 17 00:00:00 2001 From: mazhao Date: Thu, 29 Jun 2023 12:02:04 +0800 Subject: [PATCH 68/79] Optimize the code Signed-off-by: mazhao --- .../src/executor/document/check_common.cpp | 50 ++++++++++++------- .../src/executor/document/check_common.h | 2 +- .../src/interface/src/document_store.cpp | 15 +----- .../src/oh_adapter/include/json_object.h | 4 +- .../src/oh_adapter/src/json_object.cpp | 24 ++++++--- 5 files changed, 56 insertions(+), 39 deletions(-) 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 2140e70d..0adc9c7a 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 @@ -143,34 +143,50 @@ int CheckCommon::CheckDocument(JsonObject &documentObj, bool &isIdExist) return E_OK; } -int CheckCommon::CheckUpdata(JsonObject &updataObj, std::vector> &path) +int SplitFieldName(const std::string &fieldName, std::vector &allFieldsName) { - if (updataObj.GetDeep() > JSON_DEEP_MAX) { - GLOGE("projectionObj's json deep is deeper than JSON_DEEP_MAX"); - return -E_INVALID_ARGS; - } - for (size_t i = 0; i < path.size(); i++) { - if (path[i].empty()) { - return -E_INVALID_JSON_FORMAT; + std::string tempParseName; + std::string priFieldName = fieldName; + for (size_t j = 0; j < priFieldName.size(); j++) { + if (priFieldName[j] != '.') { + tempParseName += priFieldName[j]; } - for (size_t j = 0; j < path[i].size(); j++) { - if (path[i][j].empty()) { + if (priFieldName[j] == '.' || j == priFieldName.size() - 1) { + if ((j > 0 && priFieldName[j] == '.' && priFieldName[j - 1] == '.') || + (priFieldName[j] == '.' && j == priFieldName.size() - 1)) { return -E_INVALID_ARGS; } - for (auto oneChar : path[i][j]) { + allFieldsName.emplace_back(tempParseName); + tempParseName.clear(); + } + } + return E_OK; +} + +int CheckCommon::CheckUpdata(JsonObject &updataObj) +{ + JsonObject jsonTemp = updataObj.GetChild(); + size_t maxDeep = 0; + while (!jsonTemp.IsNull()) { + std::vector allFieldsName; + int errCode = SplitFieldName(jsonTemp.GetItemField(), allFieldsName); + if (errCode != E_OK) { + return errCode; + } + for (auto fieldName : allFieldsName) { + for (auto oneChar : fieldName) { if (!((isalpha(oneChar)) || (isdigit(oneChar)) || (oneChar == '_'))) { + GLOGE("updata fieldName is illegal"); return -E_INVALID_ARGS; } } } - if (!path[i].empty() && !path[i][0].empty() && isdigit(path[i][0][0])) { - return -E_INVALID_ARGS; - } - } - for (const auto &singlePath : path) { - if (singlePath.size() > JSON_DEEP_MAX) { + maxDeep = std::max(allFieldsName.size() + jsonTemp.GetDeep(), maxDeep); + if (maxDeep > JSON_DEEP_MAX) { + GLOGE("document's json deep is deeper than JSON_DEEP_MAX"); return -E_INVALID_ARGS; } + jsonTemp = jsonTemp.GetNext(); } bool isIdExist = true; CheckIdFormat(updataObj, isIdExist); 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 92860bc5..d38ed984 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 @@ -32,7 +32,7 @@ public: static int CheckFilter(JsonObject &document, std::vector> &filterPath, bool &isIdExist); static int CheckIdFormat(JsonObject &data, bool &isIdExisit); static int CheckDocument(JsonObject &document, bool &isIdExist); - static int CheckUpdata(JsonObject &updata, std::vector> &path); + static int CheckUpdata(JsonObject &updata); static int CheckProjection(JsonObject &projectionObj, std::vector> &path); }; using Key = std::vector; 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 01ca996c..83dac352 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 @@ -157,13 +157,7 @@ int UpdateArgsCheck(const std::string &collection, const std::string &filter, co return errCode; } if (update != "{}") { - std::vector> allPath; - allPath = JsonCommon::ParsePath(updateObj, errCode); - if (errCode != E_OK) { - GLOGE("updateObj ParsePath failed"); - return errCode; - } - errCode = CheckCommon::CheckUpdata(updateObj, allPath); + errCode = CheckCommon::CheckUpdata(updateObj); if (errCode != E_OK) { GLOGE("Updata format is illegal"); return errCode; @@ -439,12 +433,7 @@ int UpsertDocumentFormatCheck(const std::string &document, JsonObject &documentO { int errCode = E_OK; if (document != "{}") { - std::vector> allPath; - allPath = JsonCommon::ParsePath(documentObj, errCode); - if (errCode != E_OK) { - return errCode; - } - errCode = CheckCommon::CheckUpdata(documentObj, allPath); + errCode = CheckCommon::CheckUpdata(documentObj); if (errCode != E_OK) { GLOGE("UpsertDocument document format is illegal"); return errCode; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h index 4f5efa4a..c3d7a612 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h @@ -101,8 +101,8 @@ public: private: JsonObject(); int Init(const std::string &str, bool isFilter = false); - int CheckJsonRepeatField(cJSON *object); - int CheckSubObj(std::set &fieldSet, cJSON *subObj, int parentType); + int CheckJsonRepeatField(cJSON *object, bool isFirstFloor); + int CheckSubObj(std::set &fieldSet, cJSON *subObj, int parentType, bool isFirstFloor); int GetDeep(cJSON *cjson); int CheckNumber(cJSON *cjson, int &errCode); cJSON *cjson_ = nullptr; 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 5130029d..4265ab10 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 @@ -183,7 +183,8 @@ int JsonObject::Init(const std::string &str, bool isFilter) return -E_INVALID_ARGS; } if (!isFilter) { - ret = CheckJsonRepeatField(cjson_); + bool isFirstFloor = true; + ret = CheckJsonRepeatField(cjson_, isFirstFloor); if (ret != E_OK) { return ret; } @@ -191,7 +192,7 @@ int JsonObject::Init(const std::string &str, bool isFilter) return E_OK; } -int JsonObject::CheckJsonRepeatField(cJSON *object) +int JsonObject::CheckJsonRepeatField(cJSON *object, bool isFirstFloor) { if (object == nullptr) { return -E_INVALID_ARGS; @@ -204,7 +205,7 @@ int JsonObject::CheckJsonRepeatField(cJSON *object) std::set fieldSet; cJSON *subObj = object->child; while (subObj != nullptr) { - ret = CheckSubObj(fieldSet, subObj, type); + ret = CheckSubObj(fieldSet, subObj, type, isFirstFloor); if (ret != E_OK) { break; } @@ -213,7 +214,7 @@ int JsonObject::CheckJsonRepeatField(cJSON *object) return ret; } -int JsonObject::CheckSubObj(std::set &fieldSet, cJSON *subObj, int parentType) +int JsonObject::CheckSubObj(std::set &fieldSet, cJSON *subObj, int parentType, bool isFirstFloor) { if (subObj == nullptr) { return -E_INVALID_ARGS; @@ -221,9 +222,20 @@ int JsonObject::CheckSubObj(std::set &fieldSet, cJSON *subObj, int std::string fieldName; if (subObj->string != nullptr) { fieldName = subObj->string; + if (!isFirstFloor) { + for (auto oneChar : fieldName) { + if (!((isalpha(oneChar)) || (isdigit(oneChar)) || (oneChar == '_'))) { + return -E_INVALID_ARGS; + } + } + } + if (!fieldName.empty() && isdigit(fieldName[0])) { + return -E_INVALID_ARGS; + } } + isFirstFloor = false; if (parentType == cJSON_Array) { - return CheckJsonRepeatField(subObj); + return CheckJsonRepeatField(subObj, isFirstFloor); } if (fieldName.empty()) { return -E_INVALID_JSON_FORMAT; @@ -233,7 +245,7 @@ int JsonObject::CheckSubObj(std::set &fieldSet, cJSON *subObj, int } else { return -E_INVALID_JSON_FORMAT; } - return CheckJsonRepeatField(subObj); + return CheckJsonRepeatField(subObj, isFirstFloor); } std::string JsonObject::Print() const -- Gitee From 6377df8ce5c04e2fe4ee593c5bda85b482682057 Mon Sep 17 00:00:00 2001 From: mazhao Date: Mon, 3 Jul 2023 16:24:40 +0800 Subject: [PATCH 69/79] fix code check Signed-off-by: mazhao --- .../src/executor/document/check_common.cpp | 4 ++-- .../documentdb_json_common_test.cpp | 19 ------------------- 2 files changed, 2 insertions(+), 21 deletions(-) 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 0adc9c7a..8b179159 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 @@ -173,7 +173,7 @@ int CheckCommon::CheckUpdata(JsonObject &updataObj) if (errCode != E_OK) { return errCode; } - for (auto fieldName : allFieldsName) { + for (const auto &fieldName : allFieldsName) { for (auto oneChar : fieldName) { if (!((isalpha(oneChar)) || (isdigit(oneChar)) || (oneChar == '_'))) { GLOGE("updata fieldName is illegal"); @@ -214,7 +214,7 @@ int CheckCommon::CheckProjection(JsonObject &projectionObj, std::vector Date: Tue, 27 Jun 2023 12:00:43 +0800 Subject: [PATCH 70/79] fix crash bug Signed-off-by: mazhao --- .../src/oh_adapter/src/json_object.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) 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 4265ab10..1dd9b11a 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 @@ -146,17 +146,17 @@ int JsonObject::GetDeep(cJSON *cjson) int JsonObject::CheckNumber(cJSON *item, int &errCode) { - if (item != NULL && cJSON_IsNumber(item)) { - double value = cJSON_GetNumberValue(item); - if (value > __DBL_MAX__ || value < -__DBL_MAX__) { - errCode = -E_INVALID_ARGS; + while (item != nullptr) { + if (item != NULL && cJSON_IsNumber(item)) { + double value = cJSON_GetNumberValue(item); + if (value > __DBL_MAX__ || value < -__DBL_MAX__) { + errCode = -E_INVALID_ARGS; + } } - } - if (item->child != nullptr) { - return CheckNumber(item->child, errCode); - } - if (item->next != nullptr) { - return CheckNumber(item->next, errCode); + if (item->child != nullptr) { + return CheckNumber(item->child, errCode); + } + item = item->next; } return E_OK; } -- Gitee From 2eada16621bd8c7da6d3f8346410397e35331eeb Mon Sep 17 00:00:00 2001 From: mazhao Date: Tue, 27 Jun 2023 16:49:07 +0800 Subject: [PATCH 71/79] Reduce recursive functions Signed-off-by: mazhao --- .../gaussdb_rd/src/common/src/json_common.cpp | 44 ++++++++++--------- .../src/executor/document/check_common.cpp | 2 +- .../src/oh_adapter/src/json_object.cpp | 19 +++++--- 3 files changed, 38 insertions(+), 27 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 171c1fcb..357c0052 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 @@ -14,6 +14,8 @@ */ #include "json_common.h" +#include + #include "doc_errno.h" #include "log_print.h" @@ -82,26 +84,27 @@ std::vector JsonCommon::GetLeafValue(const JsonObject &node) bool JsonCommon::CheckNode(JsonObject &node) { - while (!node.IsNull()) { + std::queue jsonQueue; + jsonQueue.push(node); + while (!jsonQueue.empty()) { std::set fieldSet; bool isFieldNameExist = true; int ret = E_OK; - std::string fieldName = node.GetItemField(ret); + JsonObject item = jsonQueue.front(); + jsonQueue.pop(); + std::string fieldName = item.GetItemField(ret); if (ret != E_OK) { isFieldNameExist = false; } - if (fieldSet.find(fieldName) != fieldSet.end()) { return false; } - if (isFieldNameExist) { fieldSet.insert(fieldName); if (fieldName.empty()) { return false; } } - for (size_t i = 0; i < fieldName.size(); i++) { if (!((isalpha(fieldName[i])) || (isdigit(fieldName[i])) || fieldName[i] == '_')) { return false; @@ -110,14 +113,12 @@ bool JsonCommon::CheckNode(JsonObject &node) return false; } } - - if (!node.GetChild().IsNull()) { - JsonObject nodeNew = node.GetChild(); - if (!CheckNode(nodeNew)) { - return false; - } + if (!item.GetChild().IsNull()) { + jsonQueue.push(item.GetChild()); + } + if (!item.GetNext().IsNull()) { + jsonQueue.push(item.GetNext()); } - node = node.GetNext(); } return true; } @@ -129,10 +130,14 @@ bool JsonCommon::CheckJsonField(JsonObject &jsonObj) bool JsonCommon::CheckProjectionNode(JsonObject &node, bool isFirstLevel, int &errCode) { - while (!node.IsNull()) { + std::queue jsonQueue; + jsonQueue.push(node); + while (!jsonQueue.empty()) { int ret = 0; std::set fieldSet; - std::string fieldName = node.GetItemField(ret); + JsonObject item = jsonQueue.front(); + jsonQueue.pop(); + std::string fieldName = item.GetItemField(ret); if (fieldName.empty()) { errCode = -E_INVALID_ARGS; return false; @@ -154,13 +159,12 @@ bool JsonCommon::CheckProjectionNode(JsonObject &node, bool isFirstLevel, int &e return false; } } - if (!node.GetChild().IsNull()) { - JsonObject nodeNew = node.GetChild(); - if (!CheckProjectionNode(nodeNew, false, errCode)) { - return false; - } + if (!item.GetNext().IsNull()) { + jsonQueue.push(item.GetNext()); + } + if (!item.GetChild().IsNull()) { + jsonQueue.push(item.GetChild()); } - node = node.GetNext(); } return true; } 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 8b179159..f5b3e69c 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(); 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 1dd9b11a..760fa6a9 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 @@ -17,6 +17,7 @@ #include #include +#include #include "doc_errno.h" #include "log_print.h" @@ -146,17 +147,23 @@ int JsonObject::GetDeep(cJSON *cjson) int JsonObject::CheckNumber(cJSON *item, int &errCode) { - while (item != nullptr) { - if (item != NULL && cJSON_IsNumber(item)) { - double value = cJSON_GetNumberValue(item); + std::queue cjsonQueue; + cjsonQueue.push(item); + while (!cjsonQueue.empty()) { + cJSON *node = cjsonQueue.front(); + cjsonQueue.pop(); + if (node != NULL && cJSON_IsNumber(node)) { + double value = cJSON_GetNumberValue(node); if (value > __DBL_MAX__ || value < -__DBL_MAX__) { errCode = -E_INVALID_ARGS; } } - if (item->child != nullptr) { - return CheckNumber(item->child, errCode); + if (node->child != nullptr) { + cjsonQueue.push(node->child); + } + if (node->next != nullptr) { + cjsonQueue.push(node->next); } - item = item->next; } return E_OK; } -- Gitee From 2067495d4e3caf531ffdfe2e785317906d01fc9e Mon Sep 17 00:00:00 2001 From: mazhao Date: Tue, 27 Jun 2023 16:52:28 +0800 Subject: [PATCH 72/79] fix format Signed-off-by: mazhao --- .../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 f5b3e69c..8b179159 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 0073d0e92ddba6d3f54b5242a98b71538b2b6df9 Mon Sep 17 00:00:00 2001 From: mazhao Date: Mon, 3 Jul 2023 16:32:10 +0800 Subject: [PATCH 73/79] add find cut Signed-off-by: mazhao --- .../gaussdb_rd/src/interface/src/projection_tree.cpp | 6 +++--- .../data_share/gaussdb_rd/src/interface/src/result_set.cpp | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) 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 5c37b84b..4ab699c3 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 @@ -58,11 +58,11 @@ bool ProjectionTree::SearchTree(std::vector &singlePath, size_t &in { ProjectionNode *node = &node_; for (size_t i = 0; i < singlePath.size(); i++) { - if (node->isDeepest) { - index = i; - } if (node->sonNode[singlePath[i]] != nullptr) { node = node->sonNode[singlePath[i]]; + if (node->isDeepest) { + index = i + 1; + } } else { return false; } 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 2ab4fa81..dd28c026 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 @@ -198,10 +198,12 @@ int ResultSet::CheckCutNode(JsonObject *node, std::vector singlePat while (!nodeInstance.IsNull()) { singlePath.emplace_back(nodeInstance.GetItemField()); size_t index = 0; - if (!context_->projectionTree.SearchTree(singlePath, index) && index == 0) { + bool isMatch = context_->projectionTree.SearchTree(singlePath, index); + if ((nodeInstance.GetType() == JsonObject::Type::JSON_ARRAY && isMatch && index == 0) || + (!isMatch && index == 0)) { allCutPath.emplace_back(singlePath); } - if (!nodeInstance.GetChild().IsNull()) { + if (nodeInstance.GetType() != JsonObject::Type::JSON_ARRAY && !nodeInstance.GetChild().IsNull()) { JsonObject nodeChiled = nodeInstance.GetChild(); CheckCutNode(&nodeChiled, singlePath, allCutPath); } -- Gitee From 5e40d11c6052b0f4cf53ce413393997a7aec8880 Mon Sep 17 00:00:00 2001 From: mazhao Date: Mon, 3 Jul 2023 16:33:27 +0800 Subject: [PATCH 74/79] delete useless log Signed-off-by: mazhao --- .../data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp | 1 - 1 file changed, 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 760fa6a9..db21c450 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 @@ -625,7 +625,6 @@ int JsonObject::DeleteItemDeeplyOnTarget(const JsonFieldPath &path) cJSON *nodeFather = MoveToPath(cjson_, patherPath, caseSensitive_); if (nodeFather == nullptr) { - GLOGE("Delete item failed, json field path not found."); return -E_JSON_PATH_NOT_EXISTS; } -- Gitee From 4ce1ca4f3c8c41b30ed19200977403b87c38e7d3 Mon Sep 17 00:00:00 2001 From: mazhao Date: Mon, 10 Jul 2023 09:58:28 +0800 Subject: [PATCH 75/79] merge Format fix for ddb Signed-off-by: mazhao --- .../service/data_share/gaussdb_rd/BUILD.gn | 7 ++-- .../gaussdb_rd/src/common/src/json_common.cpp | 5 ++- .../src/executor/document/check_common.h | 2 +- .../src/interface/include/result_set.h | 1 + .../src/interface/src/collection.cpp | 2 - .../src/interface/src/document_key.cpp | 2 - .../src/oh_adapter/include/json_object.h | 2 +- .../src/oh_adapter/src/json_object.cpp | 25 +++++++++---- .../gaussdb_rd/test/unittest/BUILD.gn | 13 ++++--- .../unittest/api/documentdb_data_test.cpp | 21 +---------- .../unittest/api/documentdb_find_test.cpp | 37 ++----------------- .../unittest/api/documentdb_insert_test.cpp | 2 +- .../documentdb_json_common_test.cpp | 1 - 13 files changed, 40 insertions(+), 80 deletions(-) mode change 100644 => 100755 services/distributeddataservice/service/data_share/gaussdb_rd/BUILD.gn diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/BUILD.gn b/services/distributeddataservice/service/data_share/gaussdb_rd/BUILD.gn old mode 100644 new mode 100755 index 47128f8c..3f44a373 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/BUILD.gn +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/BUILD.gn @@ -59,6 +59,7 @@ ohos_shared_library("gaussdb_rd") { "src/executor/document/grd_resultset_api.cpp", "src/interface/src/collection.cpp", "src/interface/src/doc_errno.cpp", + "src/interface/src/document_key.cpp", "src/interface/src/document_store.cpp", "src/interface/src/document_store_manager.cpp", "src/interface/src/projection_tree.cpp", @@ -82,9 +83,9 @@ ohos_shared_library("gaussdb_rd") { external_deps = [ "c_utils:utils", - "hisysevent_native:libhisysevent", - "hitrace_native:hitrace_meter", - "hiviewdfx_hilog_native:libhilog", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", ] subsystem_name = "distributeddatamgr" 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 357c0052..a967d2e6 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 @@ -541,7 +541,8 @@ int JsonCommon::Append(const JsonObject &src, const JsonObject &add, bool isRepl { int externErrCode = E_OK; JsonObjectIterator(add, {}, - [&src, &externErrCode, &isReplace](const JsonFieldPath &path, const JsonObject &father, const JsonObject &item) { + [&src, &externErrCode, &isReplace](const JsonFieldPath &path, + const JsonObject &father, const JsonObject &item) { bool isCollapse = false; // Whether there is a path generated by the dot operator, such as t1.t2.t3 JsonFieldPath itemPath = ExpendPathForField(path, isCollapse); if (src.IsFieldExists(itemPath)) { @@ -663,7 +664,7 @@ bool JsonCommon::JsonEqualJudge(const JsonFieldPath &itemPath, const JsonObject } return false; } - int isAlreadyMatched = 0; // means no match anyting + int isAlreadyMatched = 0; // means no match anything return IsObjectItemMatch(srcItem, item, isAlreadyMatched, isCollapse, isMatchFlag); } 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 d38ed984..3ace4198 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 @@ -32,7 +32,7 @@ public: static int CheckFilter(JsonObject &document, std::vector> &filterPath, bool &isIdExist); static int CheckIdFormat(JsonObject &data, bool &isIdExisit); static int CheckDocument(JsonObject &document, bool &isIdExist); - static int CheckUpdata(JsonObject &updata); + static int CheckUpdata(JsonObject &updataObj); static int CheckProjection(JsonObject &projectionObj, std::vector> &path); }; using Key = std::vector; 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 8c015533..01c42c63 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 @@ -16,6 +16,7 @@ #ifndef RESULTSET_H #define RESULTSET_H +#include #include #include 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 6f852abe..d06b0943 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 @@ -22,8 +22,6 @@ #include "log_print.h" namespace DocumentDB { -constexpr int JSON_LENS_MAX = 1024 * 1024; - Collection::Collection(const std::string &name, KvStoreExecutor *executor) : executor_(executor) { std::string lowerCaseName = name; 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 4d676a49..a6493205 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 @@ -18,7 +18,6 @@ #include "log_print.h" #include "securec.h" namespace DocumentDB { - static uint16_t g_oIdIncNum = 0; constexpr uint16_t MAX_NUMBER_OF_AUTOINCREMENTS = 65535; constexpr uint16_t UINT_ZERO = 0; @@ -49,5 +48,4 @@ int DocumentKey::GetOidDocKey(DocKey &key) int ret = InitDocIdFromOid(key); return ret; } - } // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h index c3d7a612..0e4276f2 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h @@ -104,7 +104,7 @@ private: int CheckJsonRepeatField(cJSON *object, bool isFirstFloor); int CheckSubObj(std::set &fieldSet, cJSON *subObj, int parentType, bool isFirstFloor); int GetDeep(cJSON *cjson); - int CheckNumber(cJSON *cjson, int &errCode); + void CheckNumber(cJSON *cjson, int &errCode); cJSON *cjson_ = nullptr; int jsonDeep_ = 0; bool isOwner_ = false; 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 db21c450..9502daf9 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 @@ -145,14 +145,18 @@ int JsonObject::GetDeep(cJSON *cjson) return depth; } -int JsonObject::CheckNumber(cJSON *item, int &errCode) +void JsonObject::CheckNumber(cJSON *item, int &errCode) { std::queue cjsonQueue; cjsonQueue.push(item); while (!cjsonQueue.empty()) { cJSON *node = cjsonQueue.front(); cjsonQueue.pop(); - if (node != NULL && cJSON_IsNumber(node)) { + if (node == nullptr) { + errCode = -E_INVALID_ARGS; + break; + } + if (cJSON_IsNumber(node)) { // node is not null all the time double value = cJSON_GetNumberValue(node); if (value > __DBL_MAX__ || value < -__DBL_MAX__) { errCode = -E_INVALID_ARGS; @@ -165,7 +169,6 @@ int JsonObject::CheckNumber(cJSON *item, int &errCode) cjsonQueue.push(node->next); } } - return E_OK; } int JsonObject::Init(const std::string &str, bool isFilter) @@ -221,6 +224,16 @@ int JsonObject::CheckJsonRepeatField(cJSON *object, bool isFirstFloor) return ret; } +bool IsFieldNameLegal(const std::string &fieldName) +{ + for (auto oneChar : fieldName) { + if (!((isalpha(oneChar)) || (isdigit(oneChar)) || (oneChar == '_'))) { + return false; + } + } + return true; +} + int JsonObject::CheckSubObj(std::set &fieldSet, cJSON *subObj, int parentType, bool isFirstFloor) { if (subObj == nullptr) { @@ -230,10 +243,8 @@ int JsonObject::CheckSubObj(std::set &fieldSet, cJSON *subObj, int if (subObj->string != nullptr) { fieldName = subObj->string; if (!isFirstFloor) { - for (auto oneChar : fieldName) { - if (!((isalpha(oneChar)) || (isdigit(oneChar)) || (oneChar == '_'))) { - return -E_INVALID_ARGS; - } + if (!IsFieldNameLegal(fieldName)) { + return -E_INVALID_ARGS; } } if (!fieldName.empty() && isdigit(fieldName[0])) { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/BUILD.gn b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/BUILD.gn index b4cc59a3..c8a7cab5 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/BUILD.gn +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/BUILD.gn @@ -57,6 +57,7 @@ ohos_source_set("src_file") { "../../src/executor/document/grd_resultset_api.cpp", "../../src/interface/src/collection.cpp", "../../src/interface/src/doc_errno.cpp", + "../../src/interface/src/document_key.cpp", "../../src/interface/src/document_store.cpp", "../../src/interface/src/document_store_manager.cpp", "../../src/interface/src/projection_tree.cpp", @@ -81,9 +82,9 @@ ohos_source_set("src_file") { deps += [ "//third_party/cJSON:cjson" ] external_deps = [ "c_utils:utils", - "hisysevent_native:libhisysevent", - "hitrace_native:hitrace_meter", - "hiviewdfx_hilog_native:libhilog", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", ] subsystem_name = "distributeddatamgr" @@ -115,9 +116,9 @@ template("gaussdb_rd_unittest") { ] external_deps = [ "c_utils:utils", - "hisysevent_native:libhisysevent", - "hitrace_native:hitrace_meter", - "hiviewdfx_hilog_native:libhilog", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", ] } } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp index 781bbb2b..129ea35b 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_data_test.cpp @@ -133,8 +133,7 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest006, TestSize.Level0) { std::string filter = R""({"_id":"1234"})""; std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; - - for (auto flags : std::vector{ 2, 4, 8, 64, 1024, UINT32_MAX }) { + for (auto flags : std::vector { 2, 4, 8, 64, 1024, UINT32_MAX }) { EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), flags), GRD_INVALID_ARGS); } } @@ -274,24 +273,6 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest003, TestSize.Level0) } } -/** - * @tc.name: UpdateDataTest004 - * @tc.desc: Test update data with invalid filter - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest004, TestSize.Level0) {} - -/** - * @tc.name: UpdateDataTest005 - * @tc.desc: Test update data with invalid doc - * @tc.type: FUNC - * @tc.require: - * @tc.author: lianhuix - */ -HWTEST_F(DocumentDBDataTest, UpdateDataTest005, TestSize.Level0) {} - /** * @tc.name: UpdateDataTest006 * @tc.desc: Test update data with invalid flag 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 ea1586be..654294a3 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 @@ -1467,46 +1467,15 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest062, TestSize.Level1) } 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); -} - -HWTEST_F(DocumentDBFindTest, DocumentDBFindTest064, TestSize.Level1) { GRD_DB *test_db = nullptr; std::string path = "./dataShare.db"; int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &test_db); EXPECT_EQ(status, GRD_OK); EXPECT_EQ(GRD_CreateCollection(test_db, colName, "", 0), GRD_OK); - - string document1 = "{\"_id\":\"key2_11_com.acts.ohos.data.datasharetestclient_100\"\ - ,\"bundleName\":\"com.acts.ohos.data.datasharetestclient\"\ - ,\"key\":\"key2\",\"subscriberId\":11,\"timestamp\":1509100700," - "\"userId\":100,\"value\":{\"type\":0,"; + string document1 = "{\"_id\":\"key2_11_com.acts.ohos.data.datasharetestclient_100\",\ + \"bundleName\":\"com.acts.ohos.data.datasharetestclient\",\"key\":\"key2\",\ + \"subscriberId\":11,\"timestamp\":1509100700,""\"userId\":100,\"value\":{\"type\":0,"; string document2 = "\"value\":["; string document3 = "5,"; string document4 = document3; 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 385470b2..8e862bb6 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 @@ -829,7 +829,7 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest046, TestSize.Level1) HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest047, TestSize.Level1) { - const char *document1 = R""({"empty" : null})""; + const char *document1 = "{\"empty\" : null}"; 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_json_common_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/oh_adapter/documentdb_json_common_test.cpp index c065640b..ff4820e9 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 @@ -590,5 +590,4 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest023, TestSize.Leve EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj1, filterObj1, errCode), true); EXPECT_EQ(JsonCommon::IsJsonNodeMatch(srcObj2, filterObj1, errCode), false); } - } // namespace \ No newline at end of file -- Gitee From a99c188bc5445594d94cbb2418cfe21f1dc21be8 Mon Sep 17 00:00:00 2001 From: mazhao Date: Mon, 10 Jul 2023 15:37:14 +0800 Subject: [PATCH 76/79] merge Format fix for ddb Signed-off-by: mazhao --- .../gaussdb_rd/src/oh_adapter/include/json_object.h | 2 +- .../gaussdb_rd/src/oh_adapter/src/json_object.cpp | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h index 0e4276f2..bd1cba71 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h @@ -104,7 +104,7 @@ private: int CheckJsonRepeatField(cJSON *object, bool isFirstFloor); int CheckSubObj(std::set &fieldSet, cJSON *subObj, int parentType, bool isFirstFloor); int GetDeep(cJSON *cjson); - void CheckNumber(cJSON *cjson, int &errCode); + int CheckNumber(cJSON *cJSON); cJSON *cjson_ = nullptr; int jsonDeep_ = 0; bool isOwner_ = false; 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 9502daf9..26c50fd8 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 @@ -145,7 +145,7 @@ int JsonObject::GetDeep(cJSON *cjson) return depth; } -void JsonObject::CheckNumber(cJSON *item, int &errCode) +int JsonObject::CheckNumber(cJSON *item) { std::queue cjsonQueue; cjsonQueue.push(item); @@ -153,13 +153,12 @@ void JsonObject::CheckNumber(cJSON *item, int &errCode) cJSON *node = cjsonQueue.front(); cjsonQueue.pop(); if (node == nullptr) { - errCode = -E_INVALID_ARGS; - break; + return -E_INVALID_ARGS; } if (cJSON_IsNumber(node)) { // node is not null all the time double value = cJSON_GetNumberValue(node); if (value > __DBL_MAX__ || value < -__DBL_MAX__) { - errCode = -E_INVALID_ARGS; + return -E_INVALID_ARGS; } } if (node->child != nullptr) { @@ -169,6 +168,7 @@ void JsonObject::CheckNumber(cJSON *item, int &errCode) cjsonQueue.push(node->next); } } + return E_OK; } int JsonObject::Init(const std::string &str, bool isFilter) @@ -186,8 +186,7 @@ int JsonObject::Init(const std::string &str, bool isFilter) return -E_INVALID_ARGS; } - int ret = 0; - CheckNumber(cjson_, ret); + int ret = CheckNumber(cjson_); if (ret == -E_INVALID_ARGS) { GLOGE("Int value is larger than double"); return -E_INVALID_ARGS; -- Gitee From 54199055613b5a8a0fb2cc7c8aeaf1a875d8f792 Mon Sep 17 00:00:00 2001 From: mazhao Date: Mon, 10 Jul 2023 16:31:45 +0800 Subject: [PATCH 77/79] merge Format fix for ddb Signed-off-by: mazhao --- .../data_share/gaussdb_rd/src/oh_adapter/src/sqlite_utils.cpp | 4 ---- 1 file changed, 4 deletions(-) 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 d5e3d961..2c56227d 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 @@ -162,10 +162,6 @@ int SQLiteUtils::BindBlobToStatement(sqlite3_stmt *statement, int index, const s errCode = sqlite3_bind_blob(statement, index, static_cast(value.data()), value.size(), SQLITE_TRANSIENT); } - - if (errCode != SQLITE_OK) { - GLOGE("[SQLiteUtil][Bind blob] Failed to bind the value:%d", errCode); - } return errCode; } -- Gitee From 68c99158411940c2ff8e92a7f29898ec713034ad Mon Sep 17 00:00:00 2001 From: mazhao Date: Wed, 23 Aug 2023 09:45:06 +0800 Subject: [PATCH 78/79] fix JudgeViewType lap complex code check Signed-off-by: mazhao --- .../src/interface/src/document_store.cpp | 84 ++++++++++++------- 1 file changed, 56 insertions(+), 28 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 83dac352..e2bb9c25 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 @@ -620,43 +620,71 @@ Collection DocumentStore::GetCollection(std::string &collectionName) return Collection(collectionName, executor_); } -int JudgeViewType(const size_t &index, ValueObject &leafItem, bool &viewType) +int JudgeBoolViewType(const size_t index, ValueObject &leafItem, bool &viewType) { + if (leafItem.GetBoolValue()) { + if (index != 0 && !viewType) { + return -E_INVALID_ARGS; + } + viewType = true; + } else { + if (index != 0 && viewType) { + return E_INVALID_ARGS; + } + viewType = false; + } + return E_OK; +} + +int JudgeStringViewType(const size_t index, ValueObject &leafItem, bool &viewType) +{ + if (leafItem.GetStringValue() == "") { + if (index != 0 && !viewType) { + return -E_INVALID_ARGS; + } + viewType = true; + } else { + return -E_INVALID_ARGS; + } + return E_OK; +} + +int JudgeIntViewType(const size_t index, ValueObject &leafItem, bool &viewType) +{ + if (leafItem.GetIntValue() == 0) { + if (index != 0 && viewType) { + return -E_INVALID_ARGS; + } + viewType = false; + } else { + if (index != 0 && !viewType) { + return E_INVALID_ARGS; + } + viewType = true; + } + return E_OK; +} + +int JudgeViewType(const size_t index, ValueObject &leafItem, bool &viewType) +{ + int errCode = E_OK; switch (leafItem.GetValueType()) { case ValueObject::ValueType::VALUE_BOOL: - if (leafItem.GetBoolValue()) { - if (index != 0 && !viewType) { - return -E_INVALID_ARGS; - } - viewType = true; - } else { - if (index != 0 && viewType) { - return E_INVALID_ARGS; - } - viewType = false; + errCode = JudgeBoolViewType(index, leafItem, viewType); + if (errCode != E_OK) { + return errCode; } break; case ValueObject::ValueType::VALUE_STRING: - if (leafItem.GetStringValue() == "") { - if (index != 0 && !viewType) { - return -E_INVALID_ARGS; - } - viewType = true; - } else { - return -E_INVALID_ARGS; + errCode = JudgeStringViewType(index, leafItem, viewType); + if (errCode != E_OK) { + return errCode; } break; case ValueObject::ValueType::VALUE_NUMBER: - if (leafItem.GetIntValue() == 0) { - if (index != 0 && viewType) { - return -E_INVALID_ARGS; - } - viewType = false; - } else { - if (index != 0 && !viewType) { - return E_INVALID_ARGS; - } - viewType = true; + errCode = JudgeIntViewType(index, leafItem, viewType); + if (errCode != E_OK) { + return errCode; } break; default: -- Gitee From 45a63590ac1d4d69ada6ade02c8a14200a490e23 Mon Sep 17 00:00:00 2001 From: mazhao Date: Wed, 23 Aug 2023 10:05:48 +0800 Subject: [PATCH 79/79] fix code check opinion Signed-off-by: mazhao --- .../gaussdb_rd/src/interface/src/document_store.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 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 e2bb9c25..fe2d1ded 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 @@ -629,7 +629,7 @@ int JudgeBoolViewType(const size_t index, ValueObject &leafItem, bool &viewType) viewType = true; } else { if (index != 0 && viewType) { - return E_INVALID_ARGS; + return -E_INVALID_ARGS; } viewType = false; } @@ -658,7 +658,7 @@ int JudgeIntViewType(const size_t index, ValueObject &leafItem, bool &viewType) viewType = false; } else { if (index != 0 && !viewType) { - return E_INVALID_ARGS; + return -E_INVALID_ARGS; } viewType = true; } @@ -688,7 +688,7 @@ int JudgeViewType(const size_t index, ValueObject &leafItem, bool &viewType) } break; default: - return E_INVALID_ARGS; + return -E_INVALID_ARGS; } return E_OK; } @@ -697,7 +697,7 @@ int GetViewType(JsonObject &jsonObj, bool &viewType) { std::vector leafValue = JsonCommon::GetLeafValue(jsonObj); if (leafValue.size() == 0) { - return E_INVALID_ARGS; + return -E_INVALID_ARGS; } int ret = E_OK; for (size_t i = 0; i < leafValue.size(); i++) { @@ -851,7 +851,7 @@ int DocumentStore::EraseCollection(const std::string &collectionName) return E_OK; } GLOGE("erase collection failed"); - return E_INVALID_ARGS; + return -E_INVALID_ARGS; } void DocumentStore::OnClose(const std::function ¬ifier) -- Gitee