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 bae5a5938b23e75693be378b9853869ed37d3882..557f33228350cb3db423175d87ed32d962f52290 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/include/doc_errno.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/include/doc_errno.h index 3a80cf4251a678384ce245cd3d1a1d3d8ee70c08..3189dccbb8d93dcbd8b1f848b23a8784682f98e7 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 481457c06c02321463f1580822f9cc76a77173bd..48c62d629b262d2e0268350fb35d3b973fc5f272 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,15 +41,23 @@ 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); } +int Collection::InsertDocument(const Key &key, const Value &document) +{ + if (executor_ == nullptr) { + return -E_INNER_ERROR; + } + return executor_->InsertData(name_, key, document); +} + bool Collection::FindDocument() { if (executor_ == nullptr) { - return -E_INVALID_ARGS; + return -E_INNER_ERROR; } int errCode = 0; return executor_->IsCollectionExists(name_, errCode); @@ -58,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); } @@ -67,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); } @@ -75,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); } @@ -88,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); @@ -144,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 8672338d917d8eea419f87bd2835b3e533d592e7..533c540439d5dd679db09670962c9fa0c1394ba4 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; 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 a5085a10aa21ec6d7d8436cb033223d964e70d4e..c91530142f1ce40f851baaf8b4068ee569c09904 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 f608d3e6e4b033b54fd16a69f8a7f9da25b05bfa..0f3954b260dc5f604313c18fcc4700d103667ec1 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 67d569e25dde6763dd836d3aaf48d002e2695d5e..c7f262df93c0bdae9d86553db5fa1c12df930619 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 bae50f8ab82b301a516ece417c63dc6c3942ffd6..f3c0fedea0ba71cd4b119cce784fba0be9e99f22 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;