diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h index 51ecd6f47ccc9c7d71da9f50cb796693a717014f..a0ebfaee4eedf370bc1e0542abcc15279c08e6ab 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h @@ -28,7 +28,7 @@ public: CheckCommon() = default; ~CheckCommon() = default; - static bool CheckCollectionName(const std::string &collectionName, std::string &lowerCaseName); + static bool CheckCollectionName(const std::string &collectionName, std::string &lowerCaseName, int &errCode); static bool CheckFilter(const std::string &filter); static bool CheckIdFormat(const std::string &data); static bool CheckDocument(const std::string &document); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/collection_option.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/collection_option.cpp index 2540653e4a60be6f557c206dc8c3fca1d9453721..fcb3da1e104a498199062579447580dea2c2ae56 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/collection_option.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/collection_option.cpp @@ -84,7 +84,7 @@ CollectionOption CollectionOption::ReadOption(const std::string &optStr, int &er return {}; } - if (maxDocValue.GetIntValue() <= 0 || maxDocValue.GetIntValue() > UINT32_MAX) { + if (maxDocValue.GetIntValue() <= 0 || static_cast(maxDocValue.GetIntValue()) > UINT32_MAX) { GLOGE("Check collection option failed, invalid maxDoc value."); errCode = -E_INVALID_CONFIG_VALUE; return {}; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/db_config.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/db_config.cpp index f824ddcf9cd1caddbcc07beac9e387d0cb507275..b60de5b19c912b46c8e0e1c58ef4737f8345cb8a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/db_config.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/db_config.cpp @@ -111,7 +111,7 @@ bool CheckRedoBufSizeConfig(const JsonObject &config, uint32_t &redoBufSize, int return false; } - if (configValue.GetIntValue() < MIN_REDO_BUFFER_SIZE && configValue.GetIntValue() > MAX_REDO_BUFFER_SIZE) { + if (configValue.GetIntValue() < MIN_REDO_BUFFER_SIZE || configValue.GetIntValue() > MAX_REDO_BUFFER_SIZE) { GLOGE("Check DB config failed, invalid redoPubBufSize value."); errCode = -E_INVALID_CONFIG_VALUE; return false; @@ -160,7 +160,7 @@ bool CheckBufferPoolSizeConfig(const JsonObject &config, int32_t pageSize, uint3 return false; } - if (configValue.GetIntValue() < MIN_BUFFER_POOL_SIZE && configValue.GetIntValue() > MAX_BUFFER_POOL_SIZE || + if (configValue.GetIntValue() < MIN_BUFFER_POOL_SIZE || configValue.GetIntValue() > MAX_BUFFER_POOL_SIZE || configValue.GetIntValue() < pageSize * 33) { GLOGE("Check DB config failed, invalid bufferPoolSize value."); errCode = -E_INVALID_CONFIG_VALUE; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/doc_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/doc_common.cpp index ad8382adb69d9216065657242435ef0659d714c6..a3dc9d963f69671df8d8fdc44e5a15b26c16ebcd 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/doc_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/doc_common.cpp @@ -45,16 +45,19 @@ bool CheckCollectionNamePrefix(const std::string &name, const std::string &prefi } } -bool CheckCommon::CheckCollectionName(const std::string &collectionName, std::string &lowerCaseName) +bool CheckCommon::CheckCollectionName(const std::string &collectionName, std::string &lowerCaseName, int &errCode) { if (collectionName.empty()) { + errCode = -E_INVALID_ARGS; return false; } if (collectionName.length() + 1 > MAX_COLLECTION_NAME) { + errCode = -E_OVER_LIMIT; return false; } if (CheckCollectionNamePrefix(collectionName, COLLECTION_PREFIX_GRD) || CheckCollectionNamePrefix(collectionName, COLLECTION_PREFIX_GM_SYS)) { + errCode = -E_INVALID_COLL_NAME_FORMAT; return false; } 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 b7448fe5353f5a844a4ce810e64de4b00381feb4..6c80c6936da3636e0126063426aa8debea05bf28 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 @@ -33,6 +33,7 @@ constexpr int E_COLLECTION_CONFLICT = E_BASE + 15; constexpr int E_NO_DATA = E_BASE + 16; constexpr int E_NOT_PERMIT = E_BASE + 17; constexpr int E_DATA_CONFLICT = E_BASE + 18; +constexpr int E_INVALID_COLL_NAME_FORMAT = E_BASE + 18; constexpr int E_INVALID_JSON_FORMAT = E_BASE + 40; constexpr int E_JSON_PATH_NOT_EXISTS = E_BASE + 41; 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 a6913454e1f04b73aeaf88d7e133b783dd9532d5..9cedce6ac0faa3c89e7949a3fb89f720e125aef3 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 @@ -55,6 +55,9 @@ int TrasnferDocErr(int err) case -E_NO_DATA: outErr = GRD_NO_DATA; break; + case -E_INVALID_COLL_NAME_FORMAT: + outErr = GRD_INVALID_COLLECTION_NAME; + 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 1e3558c510f2f55e60e6307a4addc2adbc17c1ab..f354a632baa0df92c58660299a1bff6c3b0d1109 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 @@ -34,12 +34,13 @@ DocumentStore::~DocumentStore() int DocumentStore::CreateCollection(const std::string &name, const std::string &option, int flags) { std::string lowerCaseName; - if (!CheckCommon::CheckCollectionName(name, lowerCaseName)) { - GLOGE("Check collection name invalid."); - return -E_INVALID_ARGS; + int errCode = E_OK; + if (!CheckCommon::CheckCollectionName(name, lowerCaseName, errCode)) { + GLOGE("Check collection name invalid. %d", errCode); + return errCode; } - int errCode = E_OK; + errCode = E_OK; CollectionOption collOption = CollectionOption::ReadOption(option, errCode); if (errCode != E_OK) { GLOGE("Read collection option str failed. %d", errCode); @@ -52,7 +53,7 @@ int DocumentStore::CreateCollection(const std::string &name, const std::string & } std::lock_guard lock(dbMutex_); - bool ignoreExists = (flags == CHK_EXIST_COLLECTION); + bool ignoreExists = (flags != CHK_EXIST_COLLECTION); errCode = executor_->CreateCollection(lowerCaseName, ignoreExists); if (errCode != E_OK) { GLOGE("Create collection failed. %d", errCode); @@ -77,9 +78,10 @@ int DocumentStore::CreateCollection(const std::string &name, const std::string & int DocumentStore::DropCollection(const std::string &name, int flags) { std::string lowerCaseName; - if (!CheckCommon::CheckCollectionName(name, lowerCaseName)) { - GLOGE("Check collection name invalid."); - return -E_INVALID_ARGS; + int errCode = E_OK; + if (!CheckCommon::CheckCollectionName(name, lowerCaseName, errCode)) { + GLOGE("Check collection name invalid. %d", errCode); + return errCode; } if (flags != 0 && flags != CHK_NON_EXIST_COLLECTION) { @@ -88,7 +90,7 @@ int DocumentStore::DropCollection(const std::string &name, int flags) } bool ignoreNonExists = (flags == CHK_NON_EXIST_COLLECTION); - int errCode = executor_->DropCollection(lowerCaseName, ignoreNonExists); + errCode = executor_->DropCollection(lowerCaseName, ignoreNonExists); if (errCode != E_OK) { GLOGE("Drop collection failed. %d", errCode); return errCode; @@ -113,9 +115,10 @@ int DocumentStore::UpsertDocument(const std::string &collection, const std::stri int flags) { std::string lowerCaseCollName; - if (!CheckCommon::CheckCollectionName(collection, lowerCaseCollName)) { - GLOGE("Check collection name invalid."); - return -E_INVALID_ARGS; + int errCode = E_OK; + if (!CheckCommon::CheckCollectionName(collection, lowerCaseCollName, errCode)) { + GLOGE("Check collection name invalid. %d", errCode); + return errCode; } // TODO:: check filter diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store_manager.cpp index 2be63060a095847a8725c6abefdff15f8c70c11e..85a73cafb7c5ef43f86d281a24929f2f320ff3cf 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store_manager.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store_manager.cpp @@ -34,6 +34,14 @@ bool CheckDBCloseFlag(unsigned int flag) { return (flag == GRD_DB_CLOSE) || (flag == GRD_DB_CLOSE_IGNORE_ERROR); } + +bool CheckDBCreate(unsigned int flags, const std::string &path) +{ + if ((flags & GRD_DB_OPEN_CREATE) == 0 && !OSAPI::CheckPathExistence(path)) { + return false; + } + return true; +} } int DocumentStoreManager::GetDocumentStore(const std::string &path, const std::string &config, unsigned int flags, @@ -58,6 +66,11 @@ int DocumentStoreManager::GetDocumentStore(const std::string &path, const std::s return -E_INVALID_ARGS; } + if (!CheckDBCreate(flags, path)) { + GLOGE("Open db failed, file no exists."); + return -E_INVALID_ARGS; + } + KvStoreExecutor *executor = nullptr; errCode = KvStoreManager::GetKvStore(canonicalPath + "/" + dbName, dbConfig, executor); if (errCode != E_OK) { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h index 0766ed3061aca38463b49dfbf881308b41c9f465..b2e83a930943673fa6915ade08ec839de22233be 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h @@ -41,7 +41,7 @@ public: ValueType GetValueType() const; bool GetBoolValue() const; - int GetIntValue() const; + int64_t GetIntValue() const; double GetDoubleValue() const; std::string GetStringValue() const; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp index c9def30239a05dae9a2b704dbc11180ab04637ba..661dfeeb336a927c683b439e0066ce50f1b0d245 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp @@ -68,9 +68,9 @@ bool ValueObject::GetBoolValue() const return boolValue; } -int ValueObject::GetIntValue() const +int64_t ValueObject::GetIntValue() const { - return static_cast(doubleValue + 0.5); + return static_cast(doubleValue + 0.5); } double ValueObject::GetDoubleValue() const diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/documentdb_api_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/documentdb_api_test.cpp index 83ad456822ce91959001107a1faa2e1acc1590f6..382fe5ef71dd3a0cd036b1b37e1141412748e7f2 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/documentdb_api_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/documentdb_api_test.cpp @@ -487,7 +487,10 @@ HWTEST_F(DocumentDBApiTest, OpenDBFlagTest002, TestSize.Level0) { GRD_DB *db = nullptr; std::string path= "./document.db"; - int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); + int status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_ONLY, &db); + EXPECT_EQ(status, GRD_INVALID_ARGS); + + status = GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_CREATE, &db); EXPECT_EQ(status, GRD_OK); status = GRD_DBClose(db, GRD_DB_CLOSE); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/documentdb_collection_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/documentdb_collection_test.cpp index 9172399440e46690d50065d583bcfa51cfeab4db..5614bb41518ce7c9093cc6ae9f04696f4d963769 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/documentdb_collection_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/documentdb_collection_test.cpp @@ -101,8 +101,8 @@ HWTEST_F(DocumentDBCollectionTest, CollectionTest002, TestSize.Level0) for (auto *it : invalidName) { GLOGD("CollectionTest002: create collection with name: %s", it); - EXPECT_EQ(GRD_CreateCollection(g_db, it, "", 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_DropCollection(g_db, it, 0), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_CreateCollection(g_db, it, "", 0), GRD_INVALID_FORMAT); + EXPECT_EQ(GRD_DropCollection(g_db, it, 0), GRD_INVALID_FORMAT); } } @@ -143,8 +143,8 @@ HWTEST_F(DocumentDBCollectionTest, CollectionTest003, TestSize.Level0) HWTEST_F(DocumentDBCollectionTest, CollectionTest004, TestSize.Level0) { EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); - EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_DATA_CONFLICT); - EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", CHK_EXIST_COLLECTION), GRD_OK); + EXPECT_EQ(GRD_CreateCollection(g_db, "student", "", 0), GRD_OK); + EXPECT_EQ(GRD_CreateCollection(g_db, "Student", "", CHK_EXIST_COLLECTION), GRD_DATA_CONFLICT); } /** @@ -183,7 +183,7 @@ HWTEST_F(DocumentDBCollectionTest, CollectionTest006, TestSize.Level0) { EXPECT_EQ(GRD_CreateCollection(g_db, "student", R""({"maxDoc":1024})"", 0), GRD_OK); - EXPECT_EQ(GRD_CreateCollection(g_db, "student", R""({"maxDoc":2048})"", CHK_EXIST_COLLECTION), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_CreateCollection(g_db, "student", R""({"maxDoc":2048})"", 0), GRD_INVALID_ARGS); EXPECT_EQ(GRD_DropCollection(g_db, "student", 0), GRD_OK); EXPECT_EQ(GRD_DropCollection(g_db, "student", 0), GRD_NO_DATA);