From 7e6e580ffb3b131c16b92622e5af42d79031deb5 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Wed, 24 May 2023 11:43:59 +0800 Subject: [PATCH 1/2] 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 40a36316b1fabc8b2364ff747bde4ec264f7021e Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Tue, 30 May 2023 10:58:46 +0800 Subject: [PATCH 2/2] add Signed-off-by: Jeremyzz --- .../gaussdb_rd/src/common/src/db_config.cpp | 253 ++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100644 services/distributeddataservice/data_share/gaussdb_rd/src/common/src/db_config.cpp diff --git a/services/distributeddataservice/data_share/gaussdb_rd/src/common/src/db_config.cpp b/services/distributeddataservice/data_share/gaussdb_rd/src/common/src/db_config.cpp new file mode 100644 index 00000000..42d219ec --- /dev/null +++ b/services/distributeddataservice/data_share/gaussdb_rd/src/common/src/db_config.cpp @@ -0,0 +1,253 @@ +/* +* 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 "db_config.h" + +#include +#include +#include +#include + +#include "doc_errno.h" +#include "doc_limit.h" +#include "json_object.h" +#include "log_print.h" + +namespace DocumentDB { +namespace { +constexpr int MIN_REDO_BUFFER_SIZE = 256; +constexpr int MAX_REDO_BUFFER_SIZE = 16384; +constexpr int MIN_CONNECTION_NUM = 16; +constexpr int MAX_CONNECTION_NUM = 1024; +constexpr int MIN_BUFFER_POOL_SIZE = 1024; +constexpr int MAX_BUFFER_POOL_SIZE = 4 * 1024 * 1024; + +constexpr const char *DB_CONFIG_PAGESIZE = "pagesize"; +constexpr const char *DB_CONFIG_REDO_FLUSH_BY_TRX = "redoflushbytrx"; +constexpr const char *DB_CONFIG_REDO_PUB_BUFF_SIZE = "redopubbufsize"; +constexpr const char *DB_CONFIG_MAX_CONN_NUM = "maxconnnum"; +constexpr const char *DB_CONFIG_BUFFER_POOL_SIZE = "bufferpoolsize"; +constexpr const char *DB_CONFIG_CRC_CHECK_ENABLE = "crccheckenable"; + +const int DB_CONFIG_SIZE = 6; // db config size +const char *DB_CONFIG[DB_CONFIG_SIZE] = { DB_CONFIG_PAGESIZE, DB_CONFIG_REDO_FLUSH_BY_TRX, + DB_CONFIG_REDO_PUB_BUFF_SIZE, DB_CONFIG_MAX_CONN_NUM, DB_CONFIG_BUFFER_POOL_SIZE, DB_CONFIG_CRC_CHECK_ENABLE }; + +template +bool CheckAndGetDBConfig(const JsonObject &config, const std::string &name, const std::function &checkValid, + T &val) +{ + const JsonFieldPath configField = { name }; + if (!config.IsFieldExists(configField)) { + return true; + } + int errCode = E_OK; + ValueObject configValue = config.GetObjectByPath(configField, errCode); + if (errCode != E_OK) { + GLOGE("Cant find configValue"); + return false; + } + 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; + } + + if (checkValid && !checkValid(static_cast(configValue.GetIntValue()))) { + GLOGE("Check DB config failed, invalid %s value.", name.c_str()); + return false; + } + + val = static_cast(configValue.GetIntValue()); + return true; +} + +bool CheckPageSizeConfig(const JsonObject &config, int32_t &pageSize) +{ + std::function checkFunction = [](int32_t val) { + static const std::vector pageSizeValid = { 4, 8, 16, 32, 64 }; + return std::find(pageSizeValid.begin(), pageSizeValid.end(), val) != pageSizeValid.end(); + }; + return CheckAndGetDBConfig(config, DB_CONFIG_PAGESIZE, checkFunction, pageSize); +} + +bool CheckRedoFlushConfig(const JsonObject &config, uint32_t &redoFlush) +{ + std::function checkFunction = [](uint32_t val) { + return val == 0 || val == 1; + }; + return CheckAndGetDBConfig(config, DB_CONFIG_REDO_FLUSH_BY_TRX, checkFunction, redoFlush); +} + +bool CheckRedoBufSizeConfig(const JsonObject &config, int32_t pageSize, uint32_t &redoBufSize) +{ + std::function checkFunction = [pageSize](uint32_t val) { + return val >= MIN_REDO_BUFFER_SIZE && val <= MAX_REDO_BUFFER_SIZE && + val > static_cast(pageSize * 63); // 63: pool size should be 63 times larger then pageSize + }; + return CheckAndGetDBConfig(config, DB_CONFIG_REDO_PUB_BUFF_SIZE, checkFunction, redoBufSize); +} + +bool CheckMaxConnNumConfig(const JsonObject &config, int32_t &maxConnNum) +{ + std::function checkFunction = [](int32_t val) { + return val >= MIN_CONNECTION_NUM && val <= MAX_CONNECTION_NUM; + }; + return CheckAndGetDBConfig(config, DB_CONFIG_MAX_CONN_NUM, checkFunction, maxConnNum); +} + +bool CheckBufferPoolSizeConfig(const JsonObject &config, int32_t pageSize, uint32_t &redoBufSize) +{ + std::function checkFunction = [pageSize](uint32_t val) { + return val >= MIN_BUFFER_POOL_SIZE && val <= MAX_BUFFER_POOL_SIZE && + val >= static_cast(pageSize * 64); // 64: pool size should be 64 times larger then pageSize + }; + return CheckAndGetDBConfig(config, DB_CONFIG_BUFFER_POOL_SIZE, checkFunction, redoBufSize); +} + +bool CheckCrcCheckEnableConfig(const JsonObject &config, uint32_t &crcCheckEnable) +{ + std::function checkFunction = [](uint32_t val) { + return val == 0 || val == 1; + }; + return CheckAndGetDBConfig(config, DB_CONFIG_CRC_CHECK_ENABLE, checkFunction, crcCheckEnable); +} + +int CheckConfigValid(const JsonObject &config) +{ + JsonObject child = config.GetChild(); + while (!child.IsNull()) { + std::string fieldName = child.GetItemField(); + bool isSupport = false; + for (int i = 0; i < DB_CONFIG_SIZE; i++) { + if (strcmp(DB_CONFIG[i], fieldName.c_str()) == 0) { + isSupport = true; + break; + } + } + + if (!isSupport) { + GLOGE("Invalid db config."); + return -E_INVALID_CONFIG_VALUE; + } + + child = child.GetNext(); + } + return E_OK; +} +} // namespace + +DBConfig DBConfig::GetDBConfigFromJsonStr(const std::string &confStr, int &errCode) +{ + JsonObject dbConfig = JsonObject::Parse(confStr, errCode); + if (errCode != E_OK) { + GLOGE("Read DB config failed from str. %d", errCode); + return {}; + } + + errCode = CheckConfigValid(dbConfig); + if (errCode != E_OK) { + GLOGE("Check DB config, not support config item. %d", errCode); + return {}; + } + + DBConfig conf; + if (!CheckPageSizeConfig(dbConfig, conf.pageSize_)) { + GLOGE("Check DB config 'pageSize' failed."); + errCode = -E_INVALID_CONFIG_VALUE; + return {}; + } + + if (!CheckRedoFlushConfig(dbConfig, conf.redoFlushByTrx_)) { + GLOGE("Check DB config 'redoFlushByTrx' failed."); + errCode = -E_INVALID_CONFIG_VALUE; + return {}; + } + + if (!CheckRedoBufSizeConfig(dbConfig, conf.pageSize_, conf.redoPubBufSize_)) { + GLOGE("Check DB config 'redoPubBufSize' failed."); + errCode = -E_INVALID_CONFIG_VALUE; + return {}; + } + + if (!CheckMaxConnNumConfig(dbConfig, conf.maxConnNum_)) { + GLOGE("Check DB config 'maxConnNum' failed."); + errCode = -E_INVALID_CONFIG_VALUE; + return {}; + } + + if (!CheckBufferPoolSizeConfig(dbConfig, conf.pageSize_, conf.bufferPoolSize_)) { + GLOGE("Check DB config 'bufferPoolSize' failed."); + errCode = -E_INVALID_CONFIG_VALUE; + return {}; + } + + if (!CheckCrcCheckEnableConfig(dbConfig, conf.crcCheckEnable_)) { + GLOGE("Check DB config 'crcCheckEnable' failed."); + errCode = -E_INVALID_CONFIG_VALUE; + return {}; + } + + conf.configStr_ = confStr; + errCode = E_OK; + return conf; +} + +DBConfig DBConfig::ReadConfig(const std::string &confStr, int &errCode) +{ + if (confStr.empty()) { + return {}; + } + + if (confStr.length() + 1 > MAX_DB_CONFIG_LEN) { + GLOGE("Config json string is too long."); + errCode = -E_OVER_LIMIT; + return {}; + } + + std::string lowerCaseConfStr = confStr; + std::transform(lowerCaseConfStr.begin(), lowerCaseConfStr.end(), lowerCaseConfStr.begin(), [](unsigned char c) { + return std::tolower(c); + }); + + return GetDBConfigFromJsonStr(lowerCaseConfStr, errCode); +} + +std::string DBConfig::ToString() const +{ + return configStr_; +} + +int32_t DBConfig::GetPageSize() const +{ + return pageSize_; +} + +bool DBConfig::operator==(const DBConfig &targetConfig) const +{ + return pageSize_ == targetConfig.pageSize_ && redoFlushByTrx_ == targetConfig.redoFlushByTrx_ && + redoPubBufSize_ == targetConfig.redoPubBufSize_ && maxConnNum_ == targetConfig.maxConnNum_ && + bufferPoolSize_ == targetConfig.bufferPoolSize_ && crcCheckEnable_ == targetConfig.crcCheckEnable_; +} + +bool DBConfig::operator!=(const DBConfig &targetConfig) const +{ + return !(*this == targetConfig); +} + +bool DBConfig::CheckPersistenceEqual(const DBConfig &targetConfig) const +{ + return pageSize_ == targetConfig.pageSize_ && crcCheckEnable_ == targetConfig.crcCheckEnable_; +} +} // namespace DocumentDB \ No newline at end of file -- Gitee