Skip to content

Commit

Permalink
Enhance getters without calling schema_mng. (vesoft-inc#1453)
Browse files Browse the repository at this point in the history
  • Loading branch information
CPWstatic authored and dutor committed Dec 16, 2019
1 parent c430195 commit fc68333
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 30 deletions.
29 changes: 23 additions & 6 deletions src/common/filter/Expressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,29 @@ class ExpressionContext final {
public:
using EdgeInfo = boost::variant<std::string, EdgeType>;
void addSrcTagProp(const std::string &tag, const std::string &prop) {
tagMap_.emplace(tag, -1);
srcTagProps_.emplace(tag, prop);
}

void addDstTagProp(const std::string &tag, const std::string &prop) {
tagMap_.emplace(tag, -1);
dstTagProps_.emplace(tag, prop);
}

std::unordered_map<std::string, TagID>& getTagMap() {
return tagMap_;
}

bool getTagId(const std::string &tag, TagID &tagId) const {
auto tagFound = tagMap_.find(tag);
if (tagFound == tagMap_.end() || tagFound->second < 0) {
return false;
}

tagId = tagFound->second;
return true;
}

void addVariableProp(const std::string &var, const std::string &prop) {
variableProps_.emplace(var, prop);
variables_.emplace(var);
Expand All @@ -49,17 +65,17 @@ class ExpressionContext final {
}

bool addEdge(const std::string &alias, EdgeType edgeType) {
auto it = edgeMaps_.find(alias);
if (it != edgeMaps_.end()) {
auto it = edgeMap_.find(alias);
if (it != edgeMap_.end()) {
return false;
}
edgeMaps_.emplace(alias, edgeType);
edgeMap_.emplace(alias, edgeType);
return true;
}

bool getEdgeType(const std::string &alias, EdgeType &edgeType) {
auto it = edgeMaps_.find(alias);
if (it == edgeMaps_.end()) {
auto it = edgeMap_.find(alias);
if (it == edgeMap_.end()) {
return false;
}

Expand Down Expand Up @@ -160,7 +176,8 @@ class ExpressionContext final {
std::unordered_set<std::string> variables_;
std::unordered_set<std::string> inputProps_;
// alias => edgeType
std::unordered_map<std::string, EdgeType> edgeMaps_;
std::unordered_map<std::string, EdgeType> edgeMap_;
std::unordered_map<std::string, TagID> tagMap_;
bool overAll_{false};
GraphSpaceID space_;
nebula::storage::StorageClient *storageClient_{nullptr};
Expand Down
48 changes: 33 additions & 15 deletions src/graph/GoExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,17 @@ Status GoExecutor::prepareNeededProps() {
break;
}
}

auto &tagMap = expCtx_->getTagMap();
auto spaceId = ectx()->rctx()->session()->space();
for (auto &entry : tagMap) {
auto tagId = ectx()->schemaManager()->toTagID(spaceId, entry.first);
if (!tagId.ok()) {
status == Status::Error("Tag `%s' not found.", entry.first.c_str());
break;
}
entry.second = tagId.value();
}
} while (false);

return status;
Expand Down Expand Up @@ -1023,9 +1034,11 @@ bool GoExecutor::processFinalResult(RpcResponse &rpcResp, Callback cb) const {
srcid = vdata.get_vertex_id(),
this](const std::string &edgeName,
const std::string &prop) -> OptVariantType {
auto edgeStatus = ectx()->schemaManager()->toEdgeType(spaceId, edgeName);
if (!edgeStatus.ok()) {
return edgeStatus.status();
EdgeType type;
auto found = expCtx_->getEdgeType(edgeName, type);
if (!found) {
return Status::Error(
"Get edge type for `%s' failed in getters.", edgeName.c_str());
}

if (isReversely()) {
Expand All @@ -1036,8 +1049,8 @@ bool GoExecutor::processFinalResult(RpcResponse &rpcResp, Callback cb) const {
srcid,
edgeType > 0 ? edgeType : -edgeType, prop);
}
if (edgeType != edgeStatus.value() && edgeType != -edgeStatus.value()) {
return edgeHolder_->getDefaultProp(-edgeStatus.value(), prop);
if (edgeType != type && edgeType != -type) {
return edgeHolder_->getDefaultProp(-type, prop);
}
return edgeHolder_->get(boost::get<VertexID>(value(dst)),
srcid,
Expand All @@ -1046,8 +1059,8 @@ bool GoExecutor::processFinalResult(RpcResponse &rpcResp, Callback cb) const {
if (saveTypeFlag) {
colTypes.back() = iter->getSchema()->getFieldType(prop).type;
}
if (edgeType != edgeStatus.value() && edgeType != -edgeStatus.value()) {
auto sit = edgeSchema.find(edgeStatus.value());
if (edgeType != type && edgeType != -type) {
auto sit = edgeSchema.find(type);
if (sit == edgeSchema.end()) {
return Status::Error("get schema failed");
}
Expand All @@ -1065,11 +1078,13 @@ bool GoExecutor::processFinalResult(RpcResponse &rpcResp, Callback cb) const {
getters.getSrcTagProp =
[&iter, &spaceId, &tagData, &tagSchema, &saveTypeFlag, &colTypes, this](
const std::string &tag, const std::string &prop) -> OptVariantType {
auto status = ectx()->schemaManager()->toTagID(spaceId, tag);
if (!status.ok()) {
return status.status();
TagID tagId;
auto found = expCtx_->getTagId(tag, tagId);
if (!found) {
return Status::Error(
"Get tag id for `%s' failed in getters.", tag.c_str());
}
auto tagId = status.value();

auto it2 =
std::find_if(tagData.cbegin(), tagData.cend(), [&tagId](auto &td) {
if (td.tag_id == tagId) {
Expand Down Expand Up @@ -1104,11 +1119,14 @@ bool GoExecutor::processFinalResult(RpcResponse &rpcResp, Callback cb) const {
folly::sformat("get prop({}.{}) failed", tag, prop));
}
auto vid = boost::get<int64_t>(value(std::move(dst)));
auto status = ectx()->schemaManager()->toTagID(spaceId, tag);
if (!status.ok()) {
return status.status();

TagID tagId;
auto found = expCtx_->getTagId(tag, tagId);
if (!found) {
return Status::Error(
"Get tag id for `%s' failed in getters.", tag.c_str());
}
auto tagId = status.value();

if (saveTypeFlag) {
SupportedType type = vertexHolder_->getType(vid, tagId, prop);
colTypes.back() = type;
Expand Down
2 changes: 2 additions & 0 deletions src/interface/storage.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ enum ErrorCode {
E_EDGE_PROP_NOT_FOUND = -21,
E_TAG_PROP_NOT_FOUND = -22,
E_IMPROPER_DATA_TYPE = -23,
E_EDGE_NOT_FOUND = -24,
E_TAG_NOT_FOUND = -25,

// Invalid request
E_INVALID_FILTER = -31,
Expand Down
1 change: 1 addition & 0 deletions src/storage/QueryBaseProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class QueryBaseProcessor : public BaseProcessor<RESP> {
std::unordered_map<EdgeType, std::vector<PropContext>> edgeContexts_;
folly::Executor* executor_ = nullptr;
VertexCache* vertexCache_ = nullptr;
std::unordered_map<std::string, EdgeType> edgeMap_;
};

} // namespace storage
Expand Down
22 changes: 16 additions & 6 deletions src/storage/QueryBaseProcessor.inl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ cpp2::ErrorCode QueryBaseProcessor<REQ, RESP>::checkAndBuildContexts(const REQ&
break;
}
case cpp2::PropOwner::EDGE: {
auto it = kPropsInKey_.find(col.name);
EdgeType edgeType = col.id.get_edge_type();
auto it = kPropsInKey_.find(col.name);
if (it != kPropsInKey_.end()) {
prop.pikType_ = it->second;
if (prop.pikType_ == PropContext::PropInKeyType::SRC ||
Expand All @@ -111,6 +111,13 @@ cpp2::ErrorCode QueryBaseProcessor<REQ, RESP>::checkAndBuildContexts(const REQ&
prop.type_.type = nebula::cpp2::SupportedType::INT;
}
} else if (edgeType > 0) {
auto edgeName = this->schemaMan_->toEdgeName(spaceId_, edgeType);
if (!edgeName.ok()) {
VLOG(3) << "Can't find spaceId " << spaceId_ << ", edgeType " << edgeType;
return cpp2::ErrorCode::E_EDGE_NOT_FOUND;
}
this->edgeMap_.emplace(edgeName.value(), edgeType);

// Only outBound have properties on edge.
auto schema = this->schemaMan_->getEdgeSchema(spaceId_,
edgeType);
Expand Down Expand Up @@ -436,12 +443,15 @@ kvstore::ResultCode QueryBaseProcessor<REQ, RESP>::collectEdgeProps(
auto& getters = expCtx_->getters();
getters.getAliasProp = [this, edgeType, &reader](const std::string& edgeName,
const std::string& prop) -> OptVariantType {
auto edgeStatus = this->schemaMan_->toEdgeType(spaceId_, edgeName);
CHECK(edgeStatus.ok());

if (edgeType != edgeStatus.value()) {
return Status::Error("ignore this edge");
auto edgeFound = this->edgeMap_.find(edgeName);
if (edgeFound == edgeMap_.end()) {
return Status::Error(
"Edge `%s' not found when call getters.", edgeName.c_str());
}
if (edgeType != edgeFound->second) {
return Status::Error("Ignore this edge");
}

auto res = RowReader::getPropByName(reader.get(), prop);
if (!ok(res)) {
return Status::Error("Invalid Prop");
Expand Down
9 changes: 9 additions & 0 deletions src/storage/test/AdHocSchemaManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,14 @@ StatusOr<EdgeType> AdHocSchemaManager::toEdgeType(GraphSpaceID space, folly::Str
return -1;
}

StatusOr<std::string> AdHocSchemaManager::toEdgeName(GraphSpaceID space, EdgeType edgeType) {
UNUSED(space);
try {
return folly::to<std::string>(edgeType);
} catch (const std::exception& e) {
LOG(FATAL) << e.what();
}
return "";
}
} // namespace storage
} // namespace nebula
4 changes: 1 addition & 3 deletions src/storage/test/AdHocSchemaManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ class AdHocSchemaManager final : public nebula::meta::SchemaManager {

StatusOr<EdgeType> toEdgeType(GraphSpaceID space, folly::StringPiece typeName) override;

StatusOr<std::string> toEdgeName(GraphSpaceID, EdgeType) override {
LOG(FATAL) << "Unimplemented";
}
StatusOr<std::string> toEdgeName(GraphSpaceID space, EdgeType edgeType) override;

StatusOr<std::vector<std::string>> getAllEdge(GraphSpaceID) override {
LOG(FATAL) << "Unimplemented";
Expand Down

0 comments on commit fc68333

Please sign in to comment.