From 7a60cb2923598a49d9b84a94c6fc8bf119623e9e Mon Sep 17 00:00:00 2001 From: laura-ding <48548375+laura-ding@users.noreply.github.com> Date: Wed, 20 May 2020 18:34:22 +0800 Subject: [PATCH] Use new property type (#30) * use new meta.thrift * add check * add NULL default --- CMakeLists.txt | 3 +- src/meta/CMakeLists.txt | 1 + src/meta/MetaServiceUtils.cpp | 16 +- src/meta/MetaServiceUtils.h | 6 +- .../partsMan/CreateSpaceProcessor.cpp | 13 +- .../schemaMan/AlterEdgeProcessor.cpp | 2 +- .../schemaMan/AlterTagProcessor.cpp | 2 +- .../schemaMan/CreateEdgeProcessor.cpp | 74 ++-------- .../schemaMan/CreateTagProcessor.cpp | 86 ++--------- src/meta/processors/schemaMan/SchemaUtil.cpp | 138 ++++++++++++++++++ src/meta/processors/schemaMan/SchemaUtil.h | 26 ++++ src/meta/test/CMakeLists.txt | 2 + src/meta/test/MetaServiceUtilsTest.cpp | 2 +- src/meta/test/ProcessorTest.cpp | 103 +++++++++---- src/meta/test/TestUtils.h | 134 +++++++++++++---- 15 files changed, 393 insertions(+), 215 deletions(-) create mode 100644 src/meta/processors/schemaMan/SchemaUtil.cpp create mode 100644 src/meta/processors/schemaMan/SchemaUtil.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bf6ec729b..7ffc53f53 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -131,7 +131,7 @@ list(APPEND CMAKE_MODULE_PATH "${NEBULA_HOME}/cmake") message(STATUS "CMAKE_MODULE_PATH: " ${CMAKE_MODULE_PATH}) # When NEBULA_THIRDPARTY_ROOT is null, set default value as /opt/nebula/third-party if("${NEBULA_THIRDPARTY_ROOT}" STREQUAL "") - SET(NEBULA_THIRDPARTY_ROOT "/opt/nebula/third-party") + SET(NEBULA_THIRDPARTY_ROOT "/opt/vesoft/third-party") endif() if("${NEBULA_COMMON_REPO_URL}" STREQUAL "") @@ -145,6 +145,7 @@ endif() message(STATUS "NEBULA_COMMON_REPO_TAG: " ${NEBULA_COMMON_REPO_TAG}) SET(NEBULA_COMMON_PACKAGE "nebula-common") +SET(nebula-common_DIR ${CMAKE_CURRENT_SOURCE_DIR}/modules/common) # Configure the dependent projects include(AddDependentProject) diff --git a/src/meta/CMakeLists.txt b/src/meta/CMakeLists.txt index 864cea611..2fbb78726 100644 --- a/src/meta/CMakeLists.txt +++ b/src/meta/CMakeLists.txt @@ -34,6 +34,7 @@ nebula_add_library( processors/schemaMan/GetEdgeProcessor.cpp processors/schemaMan/ListEdgesProcessor.cpp processors/schemaMan/DropEdgeProcessor.cpp + processors/schemaMan/SchemaUtil.cpp processors/indexMan/CreateTagIndexProcessor.cpp processors/indexMan/DropTagIndexProcessor.cpp processors/indexMan/GetTagIndexProcessor.cpp diff --git a/src/meta/MetaServiceUtils.cpp b/src/meta/MetaServiceUtils.cpp index 0e3dac0b7..07b1c37c9 100644 --- a/src/meta/MetaServiceUtils.cpp +++ b/src/meta/MetaServiceUtils.cpp @@ -312,8 +312,8 @@ std::string MetaServiceUtils::schemaEdgeKey(GraphSpaceID spaceId, return key; } -std::string MetaServiceUtils::schemaEdgeVal(const std::string& name, - const cpp2::Schema& schema) { +std::string MetaServiceUtils::schemaVal(const std::string& name, + const cpp2::Schema& schema) { auto len = name.size(); std::string val, sval; apache::thrift::CompactSerializer::serialize(schema, &sval); @@ -341,18 +341,6 @@ std::string MetaServiceUtils::schemaTagKey(GraphSpaceID spaceId, TagID tagId, Sc return key; } -std::string MetaServiceUtils::schemaTagVal(const std::string& name, - const cpp2::Schema& schema) { - int32_t len = name.size(); - std::string val, sval; - apache::thrift::CompactSerializer::serialize(schema, &sval); - val.reserve(sizeof(int32_t) + name.size() + sval.size()); - val.append(reinterpret_cast(&len), sizeof(int32_t)) - .append(name) - .append(sval); - return val; -} - SchemaVer MetaServiceUtils::parseTagVersion(folly::StringPiece key) { auto offset = kTagsTable.size() + sizeof(GraphSpaceID) + sizeof(TagID); return std::numeric_limits::max() - diff --git a/src/meta/MetaServiceUtils.h b/src/meta/MetaServiceUtils.h index 5a429d416..93dc02a4c 100644 --- a/src/meta/MetaServiceUtils.h +++ b/src/meta/MetaServiceUtils.h @@ -107,20 +107,18 @@ class MetaServiceUtils final { static LeaderParts parseLeaderVal(folly::StringPiece val); + static std::string schemaVal(const std::string& name, const cpp2::Schema& schema); + static std::string schemaEdgePrefix(GraphSpaceID spaceId, EdgeType edgeType); static std::string schemaEdgesPrefix(GraphSpaceID spaceId); static std::string schemaEdgeKey(GraphSpaceID spaceId, EdgeType edgeType, SchemaVer version); - static std::string schemaEdgeVal(const std::string& name, const cpp2::Schema& schema); - static SchemaVer parseEdgeVersion(folly::StringPiece key); static std::string schemaTagKey(GraphSpaceID spaceId, TagID tagId, SchemaVer version); - static std::string schemaTagVal(const std::string& name, const cpp2::Schema& schema); - static SchemaVer parseTagVersion(folly::StringPiece key); static std::string schemaTagPrefix(GraphSpaceID spaceId, TagID tagId); diff --git a/src/meta/processors/partsMan/CreateSpaceProcessor.cpp b/src/meta/processors/partsMan/CreateSpaceProcessor.cpp index 139d1f42f..5f5d7912e 100644 --- a/src/meta/processors/partsMan/CreateSpaceProcessor.cpp +++ b/src/meta/processors/partsMan/CreateSpaceProcessor.cpp @@ -52,6 +52,7 @@ void CreateSpaceProcessor::process(const cpp2::CreateSpaceReq& req) { auto spaceName = properties.get_space_name(); auto partitionNum = properties.get_partition_num(); auto replicaFactor = properties.get_replica_factor(); + auto vidSize = properties.get_vid_size(); auto charsetName = properties.get_charset_name(); auto collateName = properties.get_collate_name(); @@ -60,7 +61,7 @@ void CreateSpaceProcessor::process(const cpp2::CreateSpaceReq& req) { partitionNum = FLAGS_default_parts_num; if (partitionNum <= 0) { LOG(ERROR) << "Create Space Failed : partition_num is illegal!"; - resp_.set_code(cpp2::ErrorCode::E_INVALID_PARTITION_NUM); + resp_.set_code(cpp2::ErrorCode::E_INVALID_PARM); onFinished(); return; } @@ -71,13 +72,21 @@ void CreateSpaceProcessor::process(const cpp2::CreateSpaceReq& req) { replicaFactor = FLAGS_default_replica_factor; if (replicaFactor <= 0) { LOG(ERROR) << "Create Space Failed : replicaFactor is illegal!"; - resp_.set_code(cpp2::ErrorCode::E_INVALID_REPLICA_FACTOR); + resp_.set_code(cpp2::ErrorCode::E_INVALID_PARM); onFinished(); return; } // Set the default value back to the struct, which will be written to storage properties.set_replica_factor(replicaFactor); } + if (vidSize <= 0 && vidSize > std::numeric_limits::max()) { + LOG(ERROR) << "Create Space Failed : vid_size is illegal!"; + resp_.set_code(cpp2::ErrorCode::E_INVALID_PARM); + onFinished(); + return; + } + + properties.set_vid_size(vidSize); VLOG(3) << "Create space " << spaceName << ", id " << spaceId; if ((int32_t)hosts.size() < replicaFactor) { diff --git a/src/meta/processors/schemaMan/AlterEdgeProcessor.cpp b/src/meta/processors/schemaMan/AlterEdgeProcessor.cpp index e70028f88..8b14b79a7 100644 --- a/src/meta/processors/schemaMan/AlterEdgeProcessor.cpp +++ b/src/meta/processors/schemaMan/AlterEdgeProcessor.cpp @@ -91,7 +91,7 @@ void AlterEdgeProcessor::process(const cpp2::AlterEdgeReq& req) { std::vector data; LOG(INFO) << "Alter edge " << req.get_edge_name() << ", edgeType " << edgeType; data.emplace_back(MetaServiceUtils::schemaEdgeKey(spaceId, edgeType, version), - MetaServiceUtils::schemaEdgeVal(req.get_edge_name(), schema)); + MetaServiceUtils::schemaVal(req.get_edge_name(), schema)); resp_.set_id(to(edgeType, EntryType::EDGE)); doSyncPutAndUpdate(std::move(data)); } diff --git a/src/meta/processors/schemaMan/AlterTagProcessor.cpp b/src/meta/processors/schemaMan/AlterTagProcessor.cpp index f50bb5352..9b5df366f 100644 --- a/src/meta/processors/schemaMan/AlterTagProcessor.cpp +++ b/src/meta/processors/schemaMan/AlterTagProcessor.cpp @@ -92,7 +92,7 @@ void AlterTagProcessor::process(const cpp2::AlterTagReq& req) { std::vector data; LOG(INFO) << "Alter Tag " << req.get_tag_name() << ", tagId " << tagId; data.emplace_back(MetaServiceUtils::schemaTagKey(spaceId, tagId, version), - MetaServiceUtils::schemaTagVal(req.get_tag_name(), schema)); + MetaServiceUtils::schemaVal(req.get_tag_name(), schema)); resp_.set_id(to(tagId, EntryType::TAG)); doSyncPutAndUpdate(std::move(data)); } diff --git a/src/meta/processors/schemaMan/CreateEdgeProcessor.cpp b/src/meta/processors/schemaMan/CreateEdgeProcessor.cpp index 77ab601ee..0700f81d5 100644 --- a/src/meta/processors/schemaMan/CreateEdgeProcessor.cpp +++ b/src/meta/processors/schemaMan/CreateEdgeProcessor.cpp @@ -5,6 +5,7 @@ */ #include "meta/processors/schemaMan/CreateEdgeProcessor.h" +#include "SchemaUtil.h" namespace nebula { namespace meta { @@ -27,6 +28,17 @@ void CreateEdgeProcessor::process(const cpp2::CreateEdgeReq& req) { } } + auto columns = req.get_schema().get_columns(); + if (!SchemaUtil::checkType(columns)) { + handleErrorCode(cpp2::ErrorCode::E_INVALID_PARM); + onFinished(); + return; + } + + cpp2::Schema schema; + schema.set_columns(std::move(columns)); + schema.set_schema_prop(req.get_schema().get_schema_prop()); + folly::SharedMutex::WriteHolder wHolder(LockUtils::edgeLock()); auto ret = getEdgeType(req.get_space_id(), edgeName); if (ret.ok()) { @@ -52,67 +64,7 @@ void CreateEdgeProcessor::process(const cpp2::CreateEdgeReq& req) { data.emplace_back(MetaServiceUtils::indexEdgeKey(req.get_space_id(), edgeName), std::string(reinterpret_cast(&edgeType), sizeof(EdgeType))); data.emplace_back(MetaServiceUtils::schemaEdgeKey(req.get_space_id(), edgeType, 0), - MetaServiceUtils::schemaEdgeVal(edgeName, req.get_schema())); - - LOG(INFO) << "Create Edge " << edgeName << ", edgeType " << edgeType; - auto columns = req.get_schema().get_columns(); - for (auto& column : columns) { - if (column.__isset.default_value) { - auto name = column.get_name(); - const auto* value = column.get_default_value(); - std::string defaultValue; - switch (column.get_type()) { - case cpp2::PropertyType::BOOL: - if (value->type() != nebula::Value::Type::BOOL) { - LOG(ERROR) << "Create Edge Failed: " << name - << " type mismatch"; - handleErrorCode(cpp2::ErrorCode::E_CONFLICT); - onFinished(); - return; - } - defaultValue = folly::to(value->getBool()); - break; - case cpp2::PropertyType::INT64: - if (value->type() != nebula::Value::Type::INT) { - LOG(ERROR) << "Create Edge Failed: " << name - << " type mismatch"; - handleErrorCode(cpp2::ErrorCode::E_CONFLICT); - onFinished(); - return; - } - defaultValue = folly::to(value->getInt()); - break; - case cpp2::PropertyType::DOUBLE: - if (value->type() != nebula::Value::Type::FLOAT) { - LOG(ERROR) << "Create Edge Failed: " << name - << " type mismatch"; - handleErrorCode(cpp2::ErrorCode::E_CONFLICT); - onFinished(); - return; - } - defaultValue = folly::to(value->getFloat()); - break; - case cpp2::PropertyType::STRING: - if (value->type() != nebula::Value::Type::STRING) { - LOG(ERROR) << "Create Edge Failed: " << name - << " type mismatch"; - handleErrorCode(cpp2::ErrorCode::E_CONFLICT); - onFinished(); - return; - } - defaultValue = value->getStr(); - break; - default: - break; - } - VLOG(3) << "Get Edge Default value: Property Name " << name - << ", Value " << defaultValue; - auto defaultKey = MetaServiceUtils::edgeDefaultKey(req.get_space_id(), - edgeType, - name); - data.emplace_back(std::move(defaultKey), std::move(defaultValue)); - } - } + MetaServiceUtils::schemaVal(edgeName, schema)); LOG(INFO) << "Create Edge " << edgeName << ", edgeType " << edgeType; handleErrorCode(cpp2::ErrorCode::SUCCEEDED); diff --git a/src/meta/processors/schemaMan/CreateTagProcessor.cpp b/src/meta/processors/schemaMan/CreateTagProcessor.cpp index 5d1338389..00a426b35 100644 --- a/src/meta/processors/schemaMan/CreateTagProcessor.cpp +++ b/src/meta/processors/schemaMan/CreateTagProcessor.cpp @@ -5,6 +5,7 @@ */ #include "meta/processors/schemaMan/CreateTagProcessor.h" +#include "SchemaUtil.h" namespace nebula { namespace meta { @@ -27,6 +28,17 @@ void CreateTagProcessor::process(const cpp2::CreateTagReq& req) { } } + auto columns = req.get_schema().get_columns(); + if (!SchemaUtil::checkType(columns)) { + handleErrorCode(cpp2::ErrorCode::E_INVALID_PARM); + onFinished(); + return; + } + + cpp2::Schema schema; + schema.set_columns(std::move(columns)); + schema.set_schema_prop(req.get_schema().get_schema_prop()); + folly::SharedMutex::WriteHolder wHolder(LockUtils::tagLock()); auto ret = getTagId(req.get_space_id(), tagName); if (ret.ok()) { @@ -52,80 +64,8 @@ void CreateTagProcessor::process(const cpp2::CreateTagReq& req) { std::vector data; data.emplace_back(MetaServiceUtils::indexTagKey(req.get_space_id(), tagName), std::string(reinterpret_cast(&tagId), sizeof(TagID))); - LOG(INFO) << "Create Tag " << tagName << ", tagId " << tagId; data.emplace_back(MetaServiceUtils::schemaTagKey(req.get_space_id(), tagId, 0), - MetaServiceUtils::schemaTagVal(tagName, req.get_schema())); - - auto columns = req.get_schema().get_columns(); - for (auto& column : columns) { - if (column.__isset.default_value) { - auto name = column.get_name(); - const auto* value = column.get_default_value(); - std::string defaultValue; - switch (column.get_type()) { - case cpp2::PropertyType::BOOL: - if (value->type() != nebula::Value::Type::BOOL) { - LOG(ERROR) << "Create Tag Failed: " << name - << " type mismatch"; - handleErrorCode(cpp2::ErrorCode::E_CONFLICT); - onFinished(); - return; - } - defaultValue = folly::to(value->getBool()); - break; - case cpp2::PropertyType::INT64: - if (value->type() != nebula::Value::Type::INT) { - LOG(ERROR) << "Create Tag Failed: " << name - << " type mismatch"; - handleErrorCode(cpp2::ErrorCode::E_CONFLICT); - onFinished(); - return; - } - defaultValue = folly::to(value->getInt()); - break; - case cpp2::PropertyType::DOUBLE: - if (value->type() != nebula::Value::Type::FLOAT) { - LOG(ERROR) << "Create Tag Failed: " << name - << " type mismatch"; - handleErrorCode(cpp2::ErrorCode::E_CONFLICT); - onFinished(); - return; - } - defaultValue = folly::to(value->getFloat()); - break; - case cpp2::PropertyType::STRING: - if (value->type() != nebula::Value::Type::STRING) { - LOG(ERROR) << "Create Tag Failed: " << name - << " type mismatch"; - handleErrorCode(cpp2::ErrorCode::E_CONFLICT); - onFinished(); - return; - } - defaultValue = folly::to(value->getStr()); - break; - case cpp2::PropertyType::TIMESTAMP: - if (value->type() != nebula::Value::Type::INT) { - LOG(ERROR) << "Create Tag Failed: " << name - << " type mismatch"; - handleErrorCode(cpp2::ErrorCode::E_CONFLICT); - onFinished(); - return; - } - defaultValue = folly::to(value->getInt()); - break; - default: - LOG(ERROR) << "Unsupported type"; - return; - } - - LOG(INFO) << "Get Tag Default value: Property Name " << name - << ", Value " << defaultValue; - auto defaultKey = MetaServiceUtils::tagDefaultKey(req.get_space_id(), - tagId, - name); - data.emplace_back(std::move(defaultKey), std::move(defaultValue)); - } - } + MetaServiceUtils::schemaVal(tagName, schema)); LOG(INFO) << "Create Tag " << tagName << ", TagID " << tagId; handleErrorCode(cpp2::ErrorCode::SUCCEEDED); diff --git a/src/meta/processors/schemaMan/SchemaUtil.cpp b/src/meta/processors/schemaMan/SchemaUtil.cpp new file mode 100644 index 000000000..3bbd76fd8 --- /dev/null +++ b/src/meta/processors/schemaMan/SchemaUtil.cpp @@ -0,0 +1,138 @@ +/* Copyright (c) 2020 vesoft inc. All rights reserved. + * + * This source code is licensed under Apache 2.0 License, + * attached with Common Clause Condition 1.0, found in the LICENSES directory. + */ +#include "SchemaUtil.h" + +namespace nebula { +namespace meta { +bool SchemaUtil::checkType(std::vector &columns) { + for (auto& column : columns) { + if (column.__isset.default_value) { + auto name = column.get_name(); + auto* value = column.get_default_value(); + auto nullable = column.__isset.nullable ? *column.get_nullable() : false; + if (nullable && value->isNull()) { + continue;; + } + switch (column.get_type()) { + case cpp2::PropertyType::BOOL: + if (value->type() != nebula::Value::Type::BOOL) { + LOG(ERROR) << "Invalid default value for ` " << name + << "', value type is " << value->type(); + return false; + } + break; + case cpp2::PropertyType::INT8: { + if (value->type() != nebula::Value::Type::INT) { + LOG(ERROR) << "Invalid default value for ` " << name + << "', value type is " << value->type(); + return false; + } + + auto v = value->getInt(); + if (v > std::numeric_limits::max() + || v < std::numeric_limits::min()) { + LOG(ERROR) << "`" << name << "' out of rang"; + return false; + } + break; + } + case cpp2::PropertyType::INT16: { + if (value->type() != nebula::Value::Type::INT) { + LOG(ERROR) << "Invalid default value for ` " << name + << "', value type is " << value->type(); + return false; + } + + auto v = value->getInt(); + if (v > std::numeric_limits::max() + || v < std::numeric_limits::min()) { + LOG(ERROR) << "`" << name << "' out of rang"; + return false; + } + break; + } + case cpp2::PropertyType::INT32: { + if (value->type() != nebula::Value::Type::INT) { + LOG(ERROR) << "Invalid default value for ` " << name + << "', value type is " << value->type(); + return false; + } + + auto v = value->getInt(); + if (v > std::numeric_limits::max() + || v < std::numeric_limits::min()) { + LOG(ERROR) << "`" << name << "' out of rang"; + return false; + } + break; + } + case cpp2::PropertyType::INT64: + if (value->type() != nebula::Value::Type::INT) { + LOG(ERROR) << "Invalid default value for ` " << name + << "', value type is " << value->type(); + return false; + } + break; + case cpp2::PropertyType::FLOAT: + case cpp2::PropertyType::DOUBLE: + if (value->type() != nebula::Value::Type::FLOAT) { + LOG(ERROR) << "Invalid default value for ` " << name + << "', value type is " << value->type(); + return false; + } + break; + case cpp2::PropertyType::STRING: + if (value->type() != nebula::Value::Type::STRING) { + LOG(ERROR) << "Invalid default value for ` " << name + << "', value type is " << value->type(); + return false; + } + break; + case cpp2::PropertyType::FIXED_STRING: { + if (value->type() != nebula::Value::Type::STRING) { + LOG(ERROR) << "Invalid default value for ` " << name + << "', value type is " << value->type(); + return false; + } + size_t typeLen = column.__isset.type_length ? *column.get_type_length() : 0; + if (value->getStr().size() > typeLen) { + const auto trimStr = value->getStr().substr(0, typeLen); + value->setStr(trimStr); + } + break; + } + case cpp2::PropertyType::TIMESTAMP: + if (value->type() != nebula::Value::Type::INT) { + LOG(ERROR) << "Invalid default value for ` " << name + << "', value type is " << value->type(); + return false; + } + break; + case cpp2::PropertyType::DATE: + if (value->type() != nebula::Value::Type::DATE) { + LOG(ERROR) << "Invalid default value for ` " << name + << "', value type is " << value->type(); + return false; + } + break; + case cpp2::PropertyType::DATETIME: + if (value->type() != nebula::Value::Type::DATETIME) { + LOG(ERROR) << "Invalid default value for ` " << name + << "', value type is " << value->type(); + return false; + } + break; + default: + LOG(ERROR) << "Unsupported type"; + return false; + } + } + } + return true; +} +} // namespace meta +} // namespace nebula + diff --git a/src/meta/processors/schemaMan/SchemaUtil.h b/src/meta/processors/schemaMan/SchemaUtil.h new file mode 100644 index 000000000..1c364817c --- /dev/null +++ b/src/meta/processors/schemaMan/SchemaUtil.h @@ -0,0 +1,26 @@ +/* Copyright (c) 2020 vesoft inc. All rights reserved. + * + * This source code is licensed under Apache 2.0 License, + * attached with Common Clause Condition 1.0, found in the LICENSES directory. + */ + +#ifndef META_SCHEMAUTIL_H_ +#define META_SCHEMAUTIL_H_ + +#include "meta/processors/BaseProcessor.h" + +namespace nebula { +namespace meta { +class SchemaUtil final { +public: + SchemaUtil() = default; + ~SchemaUtil() = default; + +public: + static bool checkType(std::vector &columns); +}; + +} // namespace meta +} // namespace nebula + +#endif // META_SCHEMAUTIL_H_ diff --git a/src/meta/test/CMakeLists.txt b/src/meta/test/CMakeLists.txt index 563097382..e76f3fd20 100644 --- a/src/meta/test/CMakeLists.txt +++ b/src/meta/test/CMakeLists.txt @@ -3,6 +3,7 @@ set(meta_test_deps $ $ $ + $ $ $ $ @@ -26,6 +27,7 @@ set(meta_test_deps $ $ $ + $ $ $ ) diff --git a/src/meta/test/MetaServiceUtilsTest.cpp b/src/meta/test/MetaServiceUtilsTest.cpp index 255006a2d..792d719c0 100644 --- a/src/meta/test/MetaServiceUtilsTest.cpp +++ b/src/meta/test/MetaServiceUtilsTest.cpp @@ -176,7 +176,7 @@ TEST(MetaServiceUtilsTest, TagTest) { cols.emplace_back(std::move(column)); } schema.set_columns(std::move(cols)); - auto val = MetaServiceUtils::schemaTagVal("test_tag", schema); + auto val = MetaServiceUtils::schemaVal("test_tag", schema); auto parsedSchema = MetaServiceUtils::parseSchema(val); ASSERT_EQ(parsedSchema, schema); } diff --git a/src/meta/test/ProcessorTest.cpp b/src/meta/test/ProcessorTest.cpp index 8f84097e2..e51be6005 100644 --- a/src/meta/test/ProcessorTest.cpp +++ b/src/meta/test/ProcessorTest.cpp @@ -213,6 +213,7 @@ TEST(ProcessorTest, SpaceTest) { properties.set_space_name("default_space"); properties.set_partition_num(8); properties.set_replica_factor(3); + properties.set_vid_size(10); properties.set_charset_name("utf8"); properties.set_collate_name("utf8_bin"); cpp2::CreateSpaceReq req; @@ -235,6 +236,7 @@ TEST(ProcessorTest, SpaceTest) { ASSERT_EQ("default_space", resp.item.properties.space_name); ASSERT_EQ(8, resp.item.properties.partition_num); ASSERT_EQ(3, resp.item.properties.replica_factor); + ASSERT_EQ(10, resp.item.properties.vid_size); ASSERT_EQ("utf8", resp.item.properties.charset_name); ASSERT_EQ("utf8_bin", resp.item.properties.collate_name); } @@ -479,35 +481,41 @@ TEST(ProcessorTest, CreateTagTest) { ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, resp.code); ASSERT_EQ(5, resp.get_id().get_tag_id()); } + // Wrong default value type { cpp2::Schema schemaWithDefault; decltype(schema.columns) colsWithDefault; - colsWithDefault.emplace_back(TestUtils::columnDefWithDefault(0, PropertyType::BOOL)); - colsWithDefault.emplace_back(TestUtils::columnDefWithDefault(1, PropertyType::INT64)); - colsWithDefault.emplace_back(TestUtils::columnDefWithDefault(2, PropertyType::DOUBLE)); - colsWithDefault.emplace_back(TestUtils::columnDefWithDefault(3, PropertyType::STRING)); + + cpp2::ColumnDef columnWithDefault; + columnWithDefault.set_name(folly::stringPrintf("col_type_mismatch")); + columnWithDefault.set_type(PropertyType::BOOL); + nebula::Value defaultValue; + defaultValue.setStr("default value"); + columnWithDefault.set_default_value(std::move(defaultValue)); + + colsWithDefault.push_back(std::move(columnWithDefault)); schemaWithDefault.set_columns(std::move(colsWithDefault)); cpp2::CreateTagReq req; req.set_space_id(1); - req.set_tag_name("tag_with_default"); + req.set_tag_name("tag_type_mismatche"); req.set_schema(std::move(schemaWithDefault)); auto* processor = CreateTagProcessor::instance(kv.get()); auto f = processor->getFuture(); processor->process(req); auto resp = std::move(f).get(); - ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, resp.code); - ASSERT_EQ(6, resp.get_id().get_tag_id()); + ASSERT_EQ(cpp2::ErrorCode::E_INVALID_PARM, resp.code); } + // Wrong default value { cpp2::Schema schemaWithDefault; decltype(schema.columns) colsWithDefault; cpp2::ColumnDef columnWithDefault; - columnWithDefault.set_name(folly::stringPrintf("col_type_mismatch")); - columnWithDefault.set_type(PropertyType::BOOL); + columnWithDefault.set_name(folly::stringPrintf("col_value_mismatch")); + columnWithDefault.set_type(PropertyType::INT8); nebula::Value defaultValue; - defaultValue.setStr("default value"); + defaultValue.setInt(256); columnWithDefault.set_default_value(std::move(defaultValue)); colsWithDefault.push_back(std::move(columnWithDefault)); @@ -515,13 +523,36 @@ TEST(ProcessorTest, CreateTagTest) { cpp2::CreateTagReq req; req.set_space_id(1); - req.set_tag_name("tag_type_mismatche"); + req.set_tag_name("tag_value_mismatche"); req.set_schema(std::move(schemaWithDefault)); auto* processor = CreateTagProcessor::instance(kv.get()); auto f = processor->getFuture(); processor->process(req); auto resp = std::move(f).get(); - ASSERT_EQ(cpp2::ErrorCode::E_CONFLICT, resp.code); + ASSERT_EQ(cpp2::ErrorCode::E_INVALID_PARM, resp.code); + } + { + cpp2::CreateTagReq req; + req.set_space_id(1); + req.set_tag_name("all_tag_type"); + auto allSchema = TestUtils::mockSchemaWithAllType(); + req.set_schema(std::move(allSchema)); + auto* processor = CreateTagProcessor::instance(kv.get()); + auto f = processor->getFuture(); + processor->process(req); + auto resp = std::move(f).get(); + ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, resp.code); + + cpp2::GetTagReq getReq; + getReq.set_space_id(1); + getReq.set_tag_name("all_tag_type"); + getReq.set_version(0); + auto* getProcessor = GetTagProcessor::instance(kv.get()); + auto getFut = getProcessor->getFuture(); + getProcessor->process(getReq); + auto getResp = std::move(getFut).get(); + ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, getResp.code); + TestUtils::checkSchemaWithAllType(getResp.get_schema()); } } @@ -652,10 +683,10 @@ TEST(ProcessorTest, CreateEdgeTest) { { cpp2::Schema schemaWithDefault; decltype(schema.columns) colsWithDefault; - colsWithDefault.emplace_back(TestUtils::columnDefWithDefault(0, PropertyType::BOOL)); - colsWithDefault.emplace_back(TestUtils::columnDefWithDefault(1, PropertyType::INT64)); - colsWithDefault.emplace_back(TestUtils::columnDefWithDefault(2, PropertyType::DOUBLE)); - colsWithDefault.emplace_back(TestUtils::columnDefWithDefault(3, PropertyType::STRING)); + colsWithDefault.emplace_back(TestUtils::columnDef(0, PropertyType::BOOL, Value(false))); + colsWithDefault.emplace_back(TestUtils::columnDef(1, PropertyType::INT64, Value(11))); + colsWithDefault.emplace_back(TestUtils::columnDef(2, PropertyType::DOUBLE, Value(11.0))); + colsWithDefault.emplace_back(TestUtils::columnDef(3, PropertyType::STRING, Value("test"))); schemaWithDefault.set_columns(std::move(colsWithDefault)); cpp2::CreateEdgeReq req; @@ -672,15 +703,8 @@ TEST(ProcessorTest, CreateEdgeTest) { { cpp2::Schema schemaWithDefault; decltype(schema.columns) colsWithDefault; - - cpp2::ColumnDef columnWithDefault; - columnWithDefault.set_name(folly::stringPrintf("col_type_mismatch")); - columnWithDefault.set_type(PropertyType::BOOL); - nebula::Value defaultValue; - defaultValue.setStr("default value"); - columnWithDefault.set_default_value(std::move(defaultValue)); - - colsWithDefault.push_back(std::move(columnWithDefault)); + colsWithDefault.push_back(TestUtils::columnDef(0, + PropertyType::BOOL, Value("default value"))); schemaWithDefault.set_columns(std::move(colsWithDefault)); cpp2::CreateEdgeReq req; @@ -691,7 +715,30 @@ TEST(ProcessorTest, CreateEdgeTest) { auto f = processor->getFuture(); processor->process(req); auto resp = std::move(f).get(); - ASSERT_EQ(cpp2::ErrorCode::E_CONFLICT, resp.code); + ASSERT_EQ(cpp2::ErrorCode::E_INVALID_PARM, resp.code); + } + { + cpp2::CreateTagReq req; + req.set_space_id(1); + req.set_tag_name("all_edge_type"); + auto allSchema = TestUtils::mockSchemaWithAllType(); + req.set_schema(std::move(allSchema)); + auto* processor = CreateTagProcessor::instance(kv.get()); + auto f = processor->getFuture(); + processor->process(req); + auto resp = std::move(f).get(); + ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, resp.code); + + cpp2::GetTagReq getReq; + getReq.set_space_id(1); + getReq.set_tag_name("all_edge_type"); + getReq.set_version(0); + auto* getProcessor = GetTagProcessor::instance(kv.get()); + auto getFut = getProcessor->getFuture(); + getProcessor->process(getReq); + auto getResp = std::move(getFut).get(); + ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, getResp.code); + TestUtils::checkSchemaWithAllType(getResp.get_schema()); } } @@ -820,7 +867,6 @@ TEST(ProcessorTest, KVOperationTest) { } } - TEST(ProcessorTest, ListOrGetTagsTest) { fs::TempDir rootPath("/tmp/ListOrGetTagsTest.XXXXXX"); auto kv = MockCluster::initMetaKV(rootPath.path()); @@ -970,7 +1016,6 @@ TEST(ProcessorTest, ListOrGetEdgesTest) { } } - TEST(ProcessorTest, DropTagTest) { fs::TempDir rootPath("/tmp/DropTagTest.XXXXXX"); auto kv = MockCluster::initMetaKV(rootPath.path()); @@ -1061,7 +1106,6 @@ TEST(ProcessorTest, DropTagTest) { } } - TEST(ProcessorTest, DropEdgeTest) { fs::TempDir rootPath("/tmp/DropEdgeTest.XXXXXX"); auto kv = MockCluster::initMetaKV(rootPath.path()); @@ -3337,3 +3381,4 @@ int main(int argc, char** argv) { } + diff --git a/src/meta/test/TestUtils.h b/src/meta/test/TestUtils.h index 673c5920c..0349d4677 100644 --- a/src/meta/test/TestUtils.h +++ b/src/meta/test/TestUtils.h @@ -124,36 +124,18 @@ class TestFaultInjector : public FaultInjector { class TestUtils { public: - static cpp2::ColumnDef columnDef(int32_t index, cpp2::PropertyType type) { + static cpp2::ColumnDef columnDef(int32_t index, cpp2::PropertyType type, + Value defaultValue = Value(), bool isNull = true, int16_t typeLen = 0) { cpp2::ColumnDef column; column.set_name(folly::stringPrintf("col_%d", index)); column.set_type(type); - return column; - } - - static cpp2::ColumnDef columnDefWithDefault(int32_t index, - cpp2::PropertyType type) { - cpp2::ColumnDef column; - column.set_name(folly::stringPrintf("col_%d", index)); - column.set_type(type); - nebula::Value defaultValue; - switch (type) { - case cpp2::PropertyType::BOOL: - defaultValue.setBool(true); - break; - case cpp2::PropertyType::INT64: - defaultValue.setInt(1); - break; - case cpp2::PropertyType::DOUBLE: - defaultValue.setFloat(3.14); - break; - case cpp2::PropertyType::STRING: - defaultValue.setStr("default value"); - break; - default: - LOG(ERROR) << "Unsupoort type"; + if (!defaultValue.empty()) { + column.set_default_value(std::move(defaultValue)); + } + column.set_nullable(isNull); + if (type == PropertyType::FIXED_STRING) { + column.set_type_length(typeLen); } - column.set_default_value(std::move(defaultValue)); return column; } @@ -240,7 +222,7 @@ class TestUtils { auto tagIdVal = std::string(reinterpret_cast(&tagId), sizeof(tagId)); tags.emplace_back(MetaServiceUtils::indexTagKey(1, tagName), tagIdVal); tags.emplace_back(MetaServiceUtils::schemaTagKey(1, tagId, ver++), - MetaServiceUtils::schemaTagVal(tagName, srcsch)); + MetaServiceUtils::schemaVal(tagName, srcsch)); } folly::Baton baton; kv->asyncMultiPut(0, 0, std::move(tags), @@ -272,7 +254,7 @@ class TestUtils { sizeof(edgeType)); edges.emplace_back(MetaServiceUtils::indexEdgeKey(1, edgeName), edgeTypeVal); edges.emplace_back(MetaServiceUtils::schemaEdgeKey(1, edgeType, ver++), - MetaServiceUtils::schemaEdgeVal(edgeName, srcsch)); + MetaServiceUtils::schemaVal(edgeName, srcsch)); } folly::Baton baton; @@ -283,6 +265,101 @@ class TestUtils { baton.wait(); } + static cpp2::Schema mockSchemaWithAllType() { + cpp2::Schema schema; + decltype(schema.columns) cols; + cols.emplace_back(TestUtils::columnDef(0, + PropertyType::BOOL, Value(true), false)); + cols.emplace_back(TestUtils::columnDef(1, + PropertyType::INT8, Value(NullType::__NULL__), true)); + cols.emplace_back(TestUtils::columnDef(2, + PropertyType::INT16, Value(20), false)); + cols.emplace_back(TestUtils::columnDef(3, + PropertyType::INT32, Value(200), false)); + cols.emplace_back(TestUtils::columnDef(4, + PropertyType::INT64, Value(2000), false)); + cols.emplace_back(TestUtils::columnDef(5, + PropertyType::FLOAT, Value(10.0), false)); + cols.emplace_back(TestUtils::columnDef(6, + PropertyType::DOUBLE, Value(20.0), false)); + cols.emplace_back(TestUtils::columnDef(7, + PropertyType::STRING, Value("string"), false)); + cols.emplace_back(TestUtils::columnDef(8, + PropertyType::FIXED_STRING, Value("longlongstring"), false, 10)); + cols.emplace_back(TestUtils::columnDef(9, + PropertyType::TIMESTAMP, Value(123456), true)); + cols.emplace_back(TestUtils::columnDef(10, + PropertyType::DATE, Value(Date()), true)); + cols.emplace_back(TestUtils::columnDef(11, + PropertyType::DATETIME, Value(DateTime()), true)); + schema.set_columns(std::move(cols)); + return schema; + } + + static void checkSchemaWithAllType(const cpp2::Schema &schema) { + ASSERT_EQ(schema.columns.size(), 12); + ASSERT_EQ(schema.columns[0].get_name(), "col_0"); + ASSERT_EQ(schema.columns[0].get_type(), PropertyType::BOOL); + ASSERT_EQ(*schema.columns[0].get_default_value(), Value(true)); + ASSERT_EQ(*schema.columns[0].get_nullable(), false); + + ASSERT_EQ(schema.columns[1].get_name(), "col_1"); + ASSERT_EQ(schema.columns[1].get_type(), PropertyType::INT8); + ASSERT_EQ(*schema.columns[1].get_default_value(), Value(NullType::__NULL__)); + ASSERT_EQ(*schema.columns[1].get_nullable(), true); + + ASSERT_EQ(schema.columns[2].get_name(), "col_2"); + ASSERT_EQ(schema.columns[2].get_type(), PropertyType::INT16); + ASSERT_EQ(*schema.columns[2].get_default_value(), Value(20)); + ASSERT_EQ(*schema.columns[2].get_nullable(), false); + + ASSERT_EQ(schema.columns[3].get_name(), "col_3"); + ASSERT_EQ(schema.columns[3].get_type(), PropertyType::INT32); + ASSERT_EQ(*schema.columns[3].get_default_value(), Value(200)); + ASSERT_EQ(*schema.columns[3].get_nullable(), false); + + ASSERT_EQ(schema.columns[4].get_name(), "col_4"); + ASSERT_EQ(schema.columns[4].get_type(), PropertyType::INT64); + ASSERT_EQ(*schema.columns[4].get_default_value(), Value(2000)); + ASSERT_EQ(*schema.columns[4].get_nullable(), false); + + ASSERT_EQ(schema.columns[5].get_name(), "col_5"); + ASSERT_EQ(schema.columns[5].get_type(), PropertyType::FLOAT); + ASSERT_EQ(*schema.columns[5].get_default_value(), Value(10.0)); + ASSERT_EQ(*schema.columns[5].get_nullable(), false); + + ASSERT_EQ(schema.columns[6].get_name(), "col_6"); + ASSERT_EQ(schema.columns[6].get_type(), PropertyType::DOUBLE); + ASSERT_EQ(*schema.columns[6].get_default_value(), Value(20.0)); + ASSERT_EQ(*schema.columns[6].get_nullable(), false); + + ASSERT_EQ(schema.columns[7].get_name(), "col_7"); + ASSERT_EQ(schema.columns[7].get_type(), PropertyType::STRING); + ASSERT_EQ(*schema.columns[7].get_default_value(), Value("string")); + ASSERT_EQ(*schema.columns[7].get_nullable(), false); + + ASSERT_EQ(schema.columns[8].get_name(), "col_8"); + ASSERT_EQ(schema.columns[8].get_type(), PropertyType::FIXED_STRING); + ASSERT_EQ(*schema.columns[8].get_default_value(), Value("longlongst")); + ASSERT_EQ(*schema.columns[8].get_nullable(), false); + ASSERT_EQ(*schema.columns[8].get_type_length(), 10); + + ASSERT_EQ(schema.columns[9].get_name(), "col_9"); + ASSERT_EQ(schema.columns[9].get_type(), PropertyType::TIMESTAMP); + ASSERT_EQ(*schema.columns[9].get_default_value(), Value(123456)); + ASSERT_EQ(*schema.columns[9].get_nullable(), true); + + ASSERT_EQ(schema.columns[10].get_name(), "col_10"); + ASSERT_EQ(schema.columns[10].get_type(), PropertyType::DATE); + ASSERT_EQ(*schema.columns[10].get_default_value(), Value(Date())); + ASSERT_EQ(*schema.columns[10].get_nullable(), true); + + ASSERT_EQ(schema.columns[11].get_name(), "col_11"); + ASSERT_EQ(schema.columns[11].get_type(), PropertyType::DATETIME); + ASSERT_EQ(*schema.columns[11].get_default_value(), Value(DateTime())); + ASSERT_EQ(*schema.columns[11].get_nullable(), true); + } + static bool verifySchema(cpp2::Schema &result, cpp2::Schema &expected) { if (result.get_columns().size() != expected.get_columns().size()) { @@ -397,3 +474,4 @@ class TestUtils { } // namespace nebula #endif // META_TEST_TESTUTILS_H_ +