Skip to content

Commit

Permalink
Use new property type (vesoft-inc#30)
Browse files Browse the repository at this point in the history
* use new meta.thrift

* add check

* add NULL default
  • Loading branch information
laura-ding authored May 20, 2020
1 parent 786b1fb commit 7a60cb2
Show file tree
Hide file tree
Showing 15 changed files with 393 additions and 215 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "")
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/meta/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 2 additions & 14 deletions src/meta/MetaServiceUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<const char*>(&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<SchemaVer>::max() -
Expand Down
6 changes: 2 additions & 4 deletions src/meta/MetaServiceUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/meta/processors/partsMan/CreateSpaceProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
}
Expand All @@ -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<int32_t>::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) {
Expand Down
2 changes: 1 addition & 1 deletion src/meta/processors/schemaMan/AlterEdgeProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void AlterEdgeProcessor::process(const cpp2::AlterEdgeReq& req) {
std::vector<kvstore::KV> 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));
}
Expand Down
2 changes: 1 addition & 1 deletion src/meta/processors/schemaMan/AlterTagProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void AlterTagProcessor::process(const cpp2::AlterTagReq& req) {
std::vector<kvstore::KV> 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));
}
Expand Down
74 changes: 13 additions & 61 deletions src/meta/processors/schemaMan/CreateEdgeProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include "meta/processors/schemaMan/CreateEdgeProcessor.h"
#include "SchemaUtil.h"

namespace nebula {
namespace meta {
Expand All @@ -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()) {
Expand All @@ -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<const char*>(&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<std::string>(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<std::string>(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<std::string>(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);
Expand Down
86 changes: 13 additions & 73 deletions src/meta/processors/schemaMan/CreateTagProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include "meta/processors/schemaMan/CreateTagProcessor.h"
#include "SchemaUtil.h"

namespace nebula {
namespace meta {
Expand All @@ -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()) {
Expand All @@ -52,80 +64,8 @@ void CreateTagProcessor::process(const cpp2::CreateTagReq& req) {
std::vector<kvstore::KV> data;
data.emplace_back(MetaServiceUtils::indexTagKey(req.get_space_id(), tagName),
std::string(reinterpret_cast<const char*>(&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<std::string>(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<std::string>(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<std::string>(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<std::string>(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<std::string>(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);
Expand Down
Loading

0 comments on commit 7a60cb2

Please sign in to comment.