diff --git a/src/graph/executor/query/TraverseExecutor.cpp b/src/graph/executor/query/TraverseExecutor.cpp index f0e319c9462..f6cf7910acf 100644 --- a/src/graph/executor/query/TraverseExecutor.cpp +++ b/src/graph/executor/query/TraverseExecutor.cpp @@ -36,7 +36,6 @@ Status TraverseExecutor::buildRequestVids() { auto iter = static_cast(inputIter); size_t iterSize = iter->size(); vids_.reserve(iterSize); - initVids_.reserve(iterSize); auto* src = traverse_->src(); QueryExpressionContext ctx(ectx_); @@ -56,11 +55,9 @@ Status TraverseExecutor::buildRequestVids() { } if (uniqueVid.emplace(vid).second) { vids_.emplace_back(vid); - initVids_.emplace_back(vid); } } } else { - initVids_.reserve(iterSize); const auto& spaceInfo = qctx()->rctx()->session()->space(); const auto& metaVidType = *(spaceInfo.spaceDesc.vid_type_ref()); auto vidType = SchemaUtil::propTypeToValueType(metaVidType.get_type()); @@ -71,7 +68,6 @@ Status TraverseExecutor::buildRequestVids() { continue; } vids_.emplace_back(vid); - initVids_.emplace_back(vid); } } return Status::OK(); @@ -164,27 +160,18 @@ folly::Future TraverseExecutor::handleResponse(RpcResponse&& resps) { auto listVal = std::make_shared(std::move(list)); auto iter = std::make_unique(listVal); if (currentStep_ == 1) { - if (range_ && range_->min() == 0) { - result_.rows = buildZeroStepPath(iter.get()); - } - // match (v)-[e:Rel]-(v1:Label1)-[e1*2]->() where id(v0) in [6, 23] return v1 - // the attributes of v1 will be obtained in the second traverse operator - // If the conditions are not met, the path in the previous step needs to be filtered out - std::unordered_set existVids; - existVids.reserve(iter->numRows()); + initVertices_.reserve(iter->numRows()); auto vertices = iter->getVertices(); + // match (v)-[e:Rel]-(v1:Label1)-[e1*2]->() where id(v0) in [6, 23] return v1 + // save the vertex that meets the filter conditions as the starting vertex of the current + // traverse for (auto& vertex : vertices.values) { if (vertex.isVertex()) { - existVids.emplace(vertex); + initVertices_.emplace_back(vertex); } } - auto initVidIter = initVids_.begin(); - while (initVidIter != initVids_.end()) { - if (existVids.find(*initVidIter) == existVids.end()) { - initVidIter = initVids_.erase(initVidIter); - } else { - initVidIter++; - } + if (range_ && range_->min() == 0) { + result_.rows = buildZeroStepPath(); } } expand(iter.get()); @@ -248,15 +235,14 @@ void TraverseExecutor::expand(GetNeighborsIter* iter) { } } -std::vector TraverseExecutor::buildZeroStepPath(GetNeighborsIter* iter) { - if (!iter || iter->numRows() == 0) { +std::vector TraverseExecutor::buildZeroStepPath() { + if (initVertices_.empty()) { return std::vector(); } std::vector result; - result.reserve(iter->size()); - auto vertices = iter->getVertices(); + result.reserve(initVertices_.size()); if (traverse_->trackPrevPath()) { - for (auto& vertex : vertices.values) { + for (auto& vertex : initVertices_) { auto dstIter = dst2PathsMap_.find(vertex); if (dstIter == dst2PathsMap_.end()) { continue; @@ -272,7 +258,7 @@ std::vector TraverseExecutor::buildZeroStepPath(GetNeighborsIter* iter) { } } } else { - for (auto& vertex : vertices.values) { + for (auto& vertex : initVertices_) { Row row; List edgeList; edgeList.values.emplace_back(vertex); @@ -297,8 +283,8 @@ folly::Future TraverseExecutor::buildResult() { return finish(ResultBuilder().value(Value(std::move(result_))).build()); } if (FLAGS_max_job_size <= 1) { - for (const auto& vid : initVids_) { - auto paths = buildPath(vid, minStep, maxStep); + for (const auto& initVertex : initVertices_) { + auto paths = buildPath(initVertex, minStep, maxStep); if (paths.empty()) { continue; } @@ -312,22 +298,22 @@ folly::Future TraverseExecutor::buildResult() { } folly::Future TraverseExecutor::buildPathMultiJobs(size_t minStep, size_t maxStep) { - DataSet vids; - vids.rows.reserve(initVids_.size()); - for (auto& vid : initVids_) { + DataSet vertices; + vertices.rows.reserve(initVertices_.size()); + for (auto& initVertex : initVertices_) { Row row; - row.values.emplace_back(std::move(vid)); - vids.rows.emplace_back(std::move(row)); + row.values.emplace_back(std::move(initVertex)); + vertices.rows.emplace_back(std::move(row)); } - auto val = std::make_shared(std::move(vids)); + auto val = std::make_shared(std::move(vertices)); auto iter = std::make_unique(val); auto scatter = [this, minStep, maxStep]( size_t begin, size_t end, Iterator* tmpIter) mutable -> std::vector { std::vector rows; for (; tmpIter->valid() && begin++ < end; tmpIter->next()) { - auto& vid = tmpIter->getColumn(0); - auto paths = buildPath(vid, minStep, maxStep); + auto& initVertex = tmpIter->getColumn(0); + auto paths = buildPath(initVertex, minStep, maxStep); if (paths.empty()) { continue; } @@ -355,8 +341,10 @@ folly::Future TraverseExecutor::buildPathMultiJobs(size_t minStep, size_ } // build path based on BFS through adjancency list -std::vector TraverseExecutor::buildPath(const Value& vid, size_t minStep, size_t maxStep) { - auto vidIter = adjList_.find(vid); +std::vector TraverseExecutor::buildPath(const Value& initVertex, + size_t minStep, + size_t maxStep) { + auto vidIter = adjList_.find(initVertex); if (vidIter == adjList_.end()) { return std::vector(); } @@ -380,7 +368,7 @@ std::vector TraverseExecutor::buildPath(const Value& vid, size_t minStep, s if (maxStep == 1) { if (traverse_->trackPrevPath()) { std::vector newResult; - auto dstIter = dst2PathsMap_.find(vid); + auto dstIter = dst2PathsMap_.find(initVertex); if (dstIter == dst2PathsMap_.end()) { return std::vector(); } @@ -461,7 +449,7 @@ std::vector TraverseExecutor::buildPath(const Value& vid, size_t minStep, s } if (traverse_->trackPrevPath()) { std::vector newPaths; - auto dstIter = dst2PathsMap_.find(vid); + auto dstIter = dst2PathsMap_.find(initVertex); if (dstIter != dst2PathsMap_.end()) { auto& prevPaths = dstIter->second; for (auto& prevPath : prevPaths) { diff --git a/src/graph/executor/query/TraverseExecutor.h b/src/graph/executor/query/TraverseExecutor.h index dbdd14d3caf..ed4ea9bf800 100644 --- a/src/graph/executor/query/TraverseExecutor.h +++ b/src/graph/executor/query/TraverseExecutor.h @@ -62,7 +62,7 @@ class TraverseExecutor final : public StorageAccessExecutor { folly::Future buildResult(); - std::vector buildPath(const Value& vid, size_t minStep, size_t maxStep); + std::vector buildPath(const Value& initVertex, size_t minStep, size_t maxStep); folly::Future buildPathMultiJobs(size_t minStep, size_t maxStep); @@ -77,7 +77,7 @@ class TraverseExecutor final : public StorageAccessExecutor { bool hasSameEdge(const std::vector& edgeList, const Edge& edge); - std::vector buildZeroStepPath(GetNeighborsIter* iter); + std::vector buildZeroStepPath(); Expression* selectFilter(); @@ -127,7 +127,7 @@ class TraverseExecutor final : public StorageAccessExecutor { ObjectPool objPool_; std::vector vids_; - std::vector initVids_; + std::vector initVertices_; DataSet result_; // Key : vertex Value : adjacent edges std::unordered_map, VertexHash, VertexEqual> adjList_;