From 0ffcf7cfeffa135e2a53f4f1d1dede8e1c586d7d Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Wed, 10 May 2023 19:17:57 +0800 Subject: [PATCH 1/2] fix insert same id bug Signed-off-by: Jeremyzz --- .../src/interface/include/collection.h | 1 + .../src/interface/src/collection.cpp | 8 ++++++ .../src/interface/src/document_store.cpp | 12 +-------- .../oh_adapter/include/kv_store_executor.h | 1 + .../src/sqlite_store_executor_impl.cpp | 25 +++++++++++++++++++ .../src/sqlite_store_executor_impl.h | 1 + 6 files changed, 37 insertions(+), 11 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h index bae5a593..557f3322 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/collection.h @@ -30,6 +30,7 @@ public: ~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 GetFilededDocument(const JsonObject &filterObj, std::vector> &values) const; int DeleteDocument(const Key &key); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp index 481457c0..a091a0f3 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp @@ -46,6 +46,14 @@ int Collection::PutDocument(const Key &key, const Value &document) return executor_->PutData(name_, key, document); } +int Collection::InsertDocument(const Key &key, const Value &document) +{ + if (executor_ == nullptr) { + return -E_INVALID_ARGS; + } + return executor_->InsertData(name_, key, document); +} + bool Collection::FindDocument() { if (executor_ == nullptr) { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp index a5085a10..c9153014 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp @@ -374,17 +374,7 @@ int DocumentStore::InsertDocument(const std::string &collection, const std::stri return -E_INVALID_ARGS; } Value ValueDocument; - errCode = coll.GetDocument(key, ValueDocument); - switch (errCode) { - case -E_NOT_FOUND: - return coll.PutDocument(key, value); - case -E_ERROR: - GLOGE("collection dont exsited"); - return -E_INVALID_ARGS; - default: - GLOGE("id already exsited, data conflict"); - return -E_DATA_CONFLICT; - } + return coll.InsertDocument(key, value); } int DocumentStore::DeleteDocument(const std::string &collection, const std::string &filter, uint32_t flags) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h index f608d3e6..0f3954b2 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/include/kv_store_executor.h @@ -30,6 +30,7 @@ public: 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 GetData(const std::string &collName, const Key &key, Value &value) const = 0; virtual int GetFilededData(const std::string &collName, const JsonObject &filterObj, std::vector> &values) const = 0; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp index 67d569e2..c7f262df 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -126,6 +126,31 @@ int SqliteStoreExecutor::PutData(const std::string &collName, const Key &key, co return E_OK; } +int SqliteStoreExecutor::InsertData(const std::string &collName, const Key &key, const Value &value) +{ + if (dbHandle_ == nullptr) { + return -E_ERROR; + } + std::string sql = "INSERT INTO '" + collName + "' VALUES (?,?);"; + int errCode = SQLiteUtils::ExecSql( + dbHandle_, sql, + [key, value](sqlite3_stmt *stmt) { + SQLiteUtils::BindBlobToStatement(stmt, 1, key); + SQLiteUtils::BindBlobToStatement(stmt, 2, value); + return E_OK; + }, + nullptr); + if (errCode != SQLITE_OK) { + GLOGE("[sqlite executor] Put data failed. err=%d", errCode); + if (errCode == -E_ERROR) { + GLOGE("have same ID before"); + return -E_DATA_CONFLICT; + } + return errCode; + } + return E_OK; +} + int SqliteStoreExecutor::GetData(const std::string &collName, const Key &key, Value &value) const { if (dbHandle_ == nullptr) { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h index bae50f8a..f3c0fede 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/oh_adapter/src/sqlite_store_executor_impl.h @@ -37,6 +37,7 @@ public: 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 GetData(const std::string &collName, const Key &key, Value &value) const override; int GetFilededData(const std::string &collName, const JsonObject &filterObj, std::vector> &values) const override; -- Gitee From 8112f6900d535fadd8849a3475040a907e294da2 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Wed, 10 May 2023 20:27:40 +0800 Subject: [PATCH 2/2] fix code check opinion Signed-off-by: Jeremyzz --- .../src/interface/include/doc_errno.h | 1 + .../src/interface/src/collection.cpp | 17 ++++++++--------- .../src/interface/src/doc_errno.cpp | 3 +++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h index 3a80cf42..3189dccb 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h @@ -38,6 +38,7 @@ constexpr int E_INVALID_JSON_FORMAT = E_BASE + 40; constexpr int E_JSON_PATH_NOT_EXISTS = E_BASE + 41; 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; int TransferDocErr(int err); } // namespace DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp index a091a0f3..48c62d62 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp @@ -41,7 +41,7 @@ Collection::~Collection() int Collection::PutDocument(const Key &key, const Value &document) { if (executor_ == nullptr) { - return -E_INVALID_ARGS; + return -E_INNER_ERROR; } return executor_->PutData(name_, key, document); } @@ -49,7 +49,7 @@ int Collection::PutDocument(const Key &key, const Value &document) int Collection::InsertDocument(const Key &key, const Value &document) { if (executor_ == nullptr) { - return -E_INVALID_ARGS; + return -E_INNER_ERROR; } return executor_->InsertData(name_, key, document); } @@ -57,7 +57,7 @@ int Collection::InsertDocument(const Key &key, const Value &document) bool Collection::FindDocument() { if (executor_ == nullptr) { - return -E_INVALID_ARGS; + return -E_INNER_ERROR; } int errCode = 0; return executor_->IsCollectionExists(name_, errCode); @@ -66,7 +66,7 @@ bool Collection::FindDocument() int Collection::GetDocument(const Key &key, Value &document) const { if (executor_ == nullptr) { - return -E_INVALID_ARGS; + return -E_INNER_ERROR; } return executor_->GetData(name_, key, document); } @@ -75,7 +75,7 @@ int Collection::GetFilededDocument(const JsonObject &filterObj, std::vector> &values) const { if (executor_ == nullptr) { - return -E_INVALID_ARGS; + return -E_INNER_ERROR; } return executor_->GetFilededData(name_, filterObj, values); } @@ -83,7 +83,7 @@ int Collection::GetFilededDocument(const JsonObject &filterObj, int Collection::DeleteDocument(const Key &key) { if (executor_ == nullptr) { - return -E_INVALID_ARGS; + return -E_INNER_ERROR; } return executor_->DelData(name_, key); } @@ -96,7 +96,7 @@ int Collection::IsCollectionExists(int &errCode) int Collection::UpsertDocument(const std::string &id, const std::string &document, bool isReplace) { if (executor_ == nullptr) { - return -E_INVALID_ARGS; + return -E_INNER_ERROR; } int errCode = E_OK; bool isCollExist = executor_->IsCollectionExists(name_, errCode); @@ -152,9 +152,8 @@ int Collection::UpsertDocument(const std::string &id, const std::string &documen int Collection::UpdateDocument(const std::string &id, const std::string &update, bool isReplace) { if (executor_ == nullptr) { - return -E_INVALID_ARGS; + return -E_INNER_ERROR; } - int errCode = E_OK; bool isCollExist = executor_->IsCollectionExists(name_, errCode); if (errCode != E_OK) { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp index 8672338d..533c5404 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/doc_errno.cpp @@ -74,6 +74,9 @@ int TransferDocErr(int err) case -E_OUT_OF_MEMORY: outErr = GRD_FAILED_MEMORY_ALLOCATE; break; + case -E_INNER_ERROR: + outErr = GRD_INNER_ERR; + break; default: outErr = GRD_INNER_ERR; break; -- Gitee