From a205030a152f84dd4c03071134582c3ed75aa657 Mon Sep 17 00:00:00 2001 From: antoniofilipovic Date: Thu, 21 Jul 2022 16:34:17 +0200 Subject: [PATCH] add working graph creation --- src/query/graph.hpp | 28 +++++++++++++++++++++++----- src/query/path.hpp | 6 ++++++ src/query/plan/operator.cpp | 2 +- src/query/typed_value.hpp | 21 ++++++++++++++++----- 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/query/graph.hpp b/src/query/graph.hpp index 65896ae2b..4b54a29c4 100644 --- a/src/query/graph.hpp +++ b/src/query/graph.hpp @@ -35,14 +35,27 @@ class Graph { * Create the graph with no elements * Allocations are done using the given MemoryResource. */ - explicit Graph(utils::MemoryResource *memory = utils::NewDeleteResource()) : vertices_(memory), edges_(memory) {} + explicit Graph(utils::MemoryResource *memory) : vertices_(memory), edges_(memory) {} + + /** Construct a copy using the given utils::MemoryResource */ + Graph(const Graph &other, utils::MemoryResource *memory) + : vertices_(other.vertices_, memory), edges_(other.edges_, memory) {} /** - * Create the graph starting with the given vertex. - * Allocations are done using the given MemoryResource. + * Construct with the value of other. + * utils::MemoryResource is obtained from other. After the move, other will be + * empty. */ - explicit Graph(const VertexAccessor &vertex, utils::MemoryResource *memory = utils::NewDeleteResource()) - : vertices_(memory), edges_(memory) {} + Graph(Graph &&other) noexcept : Graph(std::move(other), other.GetMemoryResource()) {} + + /** + * Construct with the value of other, but use the given utils::MemoryResource. + * After the move, other may not be empty if `*memory != + * *other.GetMemoryResource()`, because an element-wise move will be + * performed. + */ + Graph(Graph &&other, utils::MemoryResource *memory) + : vertices_(std::move(other.vertices_), memory), edges_(std::move(other.edges_), memory) {} /** Expands the graph with the given path. */ void Expand(const Path &path) { @@ -51,6 +64,11 @@ class Graph { [this](const VertexAccessor v) { vertices_.push_back(v); }); } + /** Move assign other, utils::MemoryResource of `this` is used. */ + Graph &operator=(Graph &&) = default; + + ~Graph() = default; + /** Returns the number of expansions (edges) in this path. */ auto size() const { return edges_.size(); } diff --git a/src/query/path.hpp b/src/query/path.hpp index 832532fbc..43ecb8953 100644 --- a/src/query/path.hpp +++ b/src/query/path.hpp @@ -31,6 +31,12 @@ class Path { /** Allocator type so that STL containers are aware that we need one */ using allocator_type = utils::Allocator; + /** + * Create the path with no elements + * Allocations are done using the given MemoryResource. + */ + explicit Path(utils::MemoryResource *memory) : vertices_(memory), edges_(memory) {} + /** * Create the path starting with the given vertex. * Allocations are done using the given MemoryResource. diff --git a/src/query/plan/operator.cpp b/src/query/plan/operator.cpp index 0643ee0dc..a091f22a3 100644 --- a/src/query/plan/operator.cpp +++ b/src/query/plan/operator.cpp @@ -2612,7 +2612,7 @@ TypedValue DefaultAggregationOpValue(const Aggregate::Element &element, utils::M return TypedValue(TypedValue::TMap(memory)); case Aggregation::Op::COLLECT_LIST: return TypedValue(TypedValue::TVector(memory)); - case Aggregation::Op::PROJECT: // add here graph as aggregation value + case Aggregation::Op::PROJECT: return TypedValue(query::Graph(memory)); } } diff --git a/src/query/typed_value.hpp b/src/query/typed_value.hpp index 838b6fe89..b0eadf70e 100644 --- a/src/query/typed_value.hpp +++ b/src/query/typed_value.hpp @@ -173,11 +173,6 @@ class TypedValue { duration_v = value; } - explicit TypedValue(const query::Graph &value, utils::MemoryResource *memory = utils::NewDeleteResource()) - : memory_(memory), type_(Type::Graph) { - graph_v = value; - } - // conversion function to storage::PropertyValue explicit operator storage::PropertyValue() const; @@ -408,6 +403,22 @@ class TypedValue { new (&path_v) Path(std::move(path), memory_); } + /** + * Construct with the value of graph. + * utils::MemoryResource is obtained from graph. After the move, graph will be + * left empty. + */ + explicit TypedValue(Graph &&graph) noexcept : TypedValue(std::move(graph), graph.GetMemoryResource()) {} + + /** + * Construct with the value of graph and use the given MemoryResource. + * If `*graph.GetMemoryResource() != *memory`, this call will perform an + * element-wise move and graph is not guaranteed to be empty. + */ + TypedValue(Graph &&graph, utils::MemoryResource *memory) : memory_(memory), type_(Type::Graph) { + new (&graph_v) Graph(std::move(graph), memory_); + } + /** * Construct with the value of other. * Default utils::NewDeleteResource() is used for allocations. After the move,