From d62d079c889b0914fed9886658be13e9be07af9e Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Thu, 1 Jun 2023 15:14:04 +0800 Subject: [PATCH 1/3] 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 2/3] 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 4ff6cb3306640f92c35cf263ae676ad92b25efcd Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Mon, 5 Jun 2023 19:57:28 +0800 Subject: [PATCH 3/3] 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