From 7d41c580dc94308060840c76dd0aa5ca52b07d15 Mon Sep 17 00:00:00 2001 From: Kostas Kyrimis Date: Thu, 11 Aug 2022 17:10:33 +0300 Subject: [PATCH] Separate decl/def between source and header on files db_accessor and graph --- src/query/CMakeLists.txt | 4 +- src/query/db_accessor.cpp | 92 +++++++++++++++++++++++++++++++++++++++ src/query/db_accessor.hpp | 68 ++++++++--------------------- src/query/graph.cpp | 57 ++++++++++++++++++++++++ src/query/graph.hpp | 48 ++++++-------------- 5 files changed, 185 insertions(+), 84 deletions(-) create mode 100644 src/query/db_accessor.cpp create mode 100644 src/query/graph.cpp diff --git a/src/query/CMakeLists.txt b/src/query/CMakeLists.txt index 0302df4da..83f476488 100644 --- a/src/query/CMakeLists.txt +++ b/src/query/CMakeLists.txt @@ -39,7 +39,9 @@ set(mg_query_sources stream/common.cpp trigger.cpp trigger_context.cpp - typed_value.cpp) + typed_value.cpp + graph.cpp + db_accessor.cpp) find_package(Boost REQUIRED) diff --git a/src/query/db_accessor.cpp b/src/query/db_accessor.cpp new file mode 100644 index 000000000..5d6b5167e --- /dev/null +++ b/src/query/db_accessor.cpp @@ -0,0 +1,92 @@ +// Copyright 2022 Memgraph Ltd. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source +// License, and you may not use this file except in compliance with the Business Source License. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +#pragma once + +#include "query/graph.hpp" + +namespace memgraph::query { +SubgraphDbAccessor::SubgraphDbAccessor(query::DbAccessor *db_accessor, Graph *graph) + : db_accessor_(db_accessor), graph_(graph) {} + +SubgraphDbAccessor *SubgraphDbAccessor::MakeSubgraphDbAccessor(DbAccessor *db_accessor, Graph *graph) { + return new SubgraphDbAccessor(db_accessor, graph); +} + +storage::PropertyId SubgraphDbAccessor::SubgraphDBNameToProperty(const std::string_view name) { + return db_accessor_->NameToProperty(name); +} + +storage::LabelId SubgraphDbAccessor::NameToLabel(const std::string_view name) { + return db_accessor_->NameToLabel(name); +} + +storage::EdgeTypeId SubgraphDbAccessor::NameToEdgeType(const std::string_view name) { + return db_accessor_->NameToEdgeType(name); +} + +const std::string &SubgraphDbAccessor::PropertyToName(storage::PropertyId prop) const { + return db_accessor_->PropertyToName(prop); +} + +const std::string &SubgraphDbAccessor::LabelToName(storage::LabelId label) const { + return db_accessor_->LabelToName(label); +} + +const std::string &SubgraphDbAccessor::EdgeTypeToName(storage::EdgeTypeId type) const { + return db_accessor_->EdgeTypeToName(type); +} + +storage::Result> SubgraphDbAccessor::RemoveEdge(EdgeAccessor *edge) { + auto result = db_accessor_->RemoveEdge(edge); + // todo antoniofilipovic remove edge from subgraph + return result; +} + +storage::Result SubgraphDbAccessor::InsertEdge(VertexAccessor *from, VertexAccessor *to, + const storage::EdgeTypeId &edge_type) { + auto result = db_accessor_->InsertEdge(from, to, edge_type); + // todo antoniofilipovic add edge to subgraph + return result; +} + +storage::Result>>> +SubgraphDbAccessor::DetachRemoveVertex(VertexAccessor *vertex_accessor) { + auto result = db_accessor_->DetachRemoveVertex(vertex_accessor); + // todo antoniofilipovic remove vertex and edges from subgraph + return result; +} + +storage::Result> SubgraphDbAccessor::RemoveVertex(VertexAccessor *vertex_accessor) { + auto result = db_accessor_->RemoveVertex(vertex_accessor); + // todo antoniofilipovic remove vertex from subgraph + return result; +} + +VertexAccessor SubgraphDbAccessor::InsertVertex() { + auto result = db_accessor_->InsertVertex(); + // todo antoniofilipovic add vertex to subgraph + return result; +} + +VerticesIterable SubgraphDbAccessor::Vertices(storage::View view) { + // todo antoniofilipovic change to get vertices from subgraph + return VerticesIterable(graph_->vertices()); + // return db_accessor_->Vertices(view); +} + +std::optional SubgraphDbAccessor::FindVertex(storage::Gid gid, storage::View view) { + // todo antoniofilipovic change to return SubgraphVertexAccessor && add check that vertex exists in subgraph + return db_accessor_->FindVertex(gid, view); +} + +query::Graph *SubgraphDbAccessor::getGraph() { return graph_; } +} // namespace memgraph::query diff --git a/src/query/db_accessor.hpp b/src/query/db_accessor.hpp index f385604c9..ff6cc156a 100644 --- a/src/query/db_accessor.hpp +++ b/src/query/db_accessor.hpp @@ -17,7 +17,6 @@ #include #include "query/exceptions.hpp" -#include "query/graph.hpp" #include "storage/v2/id_types.hpp" #include "storage/v2/property_value.hpp" #include "storage/v2/result.hpp" @@ -48,8 +47,8 @@ namespace memgraph::query { -class VertexAccessor; class Graph; +class VertexAccessor; class EdgeAccessor final { public: @@ -467,72 +466,43 @@ class DbAccessor final { class SubgraphDbAccessor final { DbAccessor *db_accessor_; - query::Graph *graph_; + Graph *graph_; public: - explicit SubgraphDbAccessor(query::DbAccessor *db_accessor, query::Graph *graph) - : db_accessor_(db_accessor), graph_(graph) {} + explicit SubgraphDbAccessor(DbAccessor *db_accessor, Graph *graph); - static SubgraphDbAccessor *MakeSubgraphDbAccessor(query::DbAccessor *db_accessor, query::Graph *graph) { - return new SubgraphDbAccessor(db_accessor, graph); - } + static SubgraphDbAccessor *MakeSubgraphDbAccessor(DbAccessor *db_accessor, Graph *graph); + storage::PropertyId SubgraphDBNameToProperty(const std::string_view name); - storage::PropertyId NameToProperty(const std::string_view name) { return db_accessor_->NameToProperty(name); } + storage::PropertyId NameToProperty(const std::string_view name); - storage::LabelId NameToLabel(const std::string_view name) { return db_accessor_->NameToLabel(name); } + storage::LabelId NameToLabel(const std::string_view name); - storage::EdgeTypeId NameToEdgeType(const std::string_view name) { return db_accessor_->NameToEdgeType(name); } + storage::EdgeTypeId NameToEdgeType(const std::string_view name); - const std::string &PropertyToName(storage::PropertyId prop) const { return db_accessor_->PropertyToName(prop); } + const std::string &PropertyToName(storage::PropertyId prop) const; - const std::string &LabelToName(storage::LabelId label) const { return db_accessor_->LabelToName(label); } + const std::string &LabelToName(storage::LabelId label) const; - const std::string &EdgeTypeToName(storage::EdgeTypeId type) const { return db_accessor_->EdgeTypeToName(type); } + const std::string &EdgeTypeToName(storage::EdgeTypeId type) const; - storage::Result> RemoveEdge(EdgeAccessor *edge) { - auto result = db_accessor_->RemoveEdge(edge); - // todo antoniofilipovic remove edge from subgraph - return result; - } + storage::Result> RemoveEdge(EdgeAccessor *edge); storage::Result InsertEdge(VertexAccessor *from, VertexAccessor *to, - const storage::EdgeTypeId &edge_type) { - auto result = db_accessor_->InsertEdge(from, to, edge_type); - // todo antoniofilipovic add edge to subgraph - return result; - } + const storage::EdgeTypeId &edge_type); storage::Result>>> DetachRemoveVertex( - VertexAccessor *vertex_accessor) { - auto result = db_accessor_->DetachRemoveVertex(vertex_accessor); - // todo antoniofilipovic remove vertex and edges from subgraph - return result; - } + VertexAccessor *vertex_accessor); - storage::Result> RemoveVertex(VertexAccessor *vertex_accessor) { - auto result = db_accessor_->RemoveVertex(vertex_accessor); - // todo antoniofilipovic remove vertex from subgraph - return result; - } + storage::Result> RemoveVertex(VertexAccessor *vertex_accessor); - VertexAccessor InsertVertex() { - auto result = db_accessor_->InsertVertex(); - // todo antoniofilipovic add vertex to subgraph - return result; - } + VertexAccessor InsertVertex(); - VerticesIterable Vertices(storage::View view) { - // todo antoniofilipovic change to get vertices from subgraph - return VerticesIterable(graph_->vertices); - // return db_accessor_->Vertices(view); - } + VerticesIterable Vertices(storage::View view); - std::optional FindVertex(storage::Gid gid, storage::View view) { - // todo antoniofilipovic change to return SubgraphVertexAccessor && add check that vertex exists in subgraph - return db_accessor_->FindVertex(gid, view); - } + std::optional FindVertex(storage::Gid gid, storage::View view); - query::Graph *getGraph() { return graph_; } + Graph *getGraph(); }; // class SubgraphEdgeAccessor final { diff --git a/src/query/graph.cpp b/src/query/graph.cpp new file mode 100644 index 000000000..c27c053e5 --- /dev/null +++ b/src/query/graph.cpp @@ -0,0 +1,57 @@ +// Copyright 2022 Memgraph Ltd. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source +// License, and you may not use this file except in compliance with the Business Source License. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +#include "query/graph.hpp" +#include "query/path.hpp" + +namespace memgraph::query { + +Graph::Graph(utils::MemoryResource *memory) : vertices_(memory), edges_(memory) {} + +Graph::Graph(const Graph &other, utils::MemoryResource *memory) + : vertices_(other.vertices_, memory), edges_(other.edges_, memory) {} + +Graph::Graph(Graph &&other) noexcept : Graph(std::move(other), other.GetMemoryResource()) {} + +Graph::Graph(Graph &&other, utils::MemoryResource *memory) + : vertices_(std::move(other.vertices_), memory), edges_(std::move(other.edges_), memory) {} + +void Graph::Expand(const Path &path) { + const auto path_vertices_ = path.vertices(); + const auto path_edges_ = path.edges(); + std::for_each(path_vertices_.begin(), path_vertices_.end(), [this](const VertexAccessor v) { vertices_.insert(v); }); + std::for_each(path_edges_.begin(), path_edges_.end(), [this](const EdgeAccessor e) { edges_.insert(e); }); +} + +std::vector Graph::OutEdges(query::VertexAccessor vertex_accessor) { + std::vector out_edges; + for (auto it = edges_.begin(); it != edges_.end(); ++it) { + if (it->From() == vertex_accessor) { + out_edges.emplace_back(*it); + } + } + return out_edges; +} + +/** Move assign other, utils::MemoryResource of `this` is used. */ +Graph &Graph::operator=(Graph &&) = default; + +/** Returns the number of expansions (edges) in this path. */ +auto Graph::size() const { return edges_.size(); } + +auto &Graph::vertices() { return vertices_; } +auto &Graph::edges() { return edges_; } +const auto &Graph::vertices() const { return vertices_; } +const auto &Graph::edges() const { return edges_; } + +utils::MemoryResource *Graph::GetMemoryResource() const { return vertices_.get_allocator().GetMemoryResource(); } + +} // namespace memgraph::query diff --git a/src/query/graph.hpp b/src/query/graph.hpp index 5242324b7..97afa161e 100644 --- a/src/query/graph.hpp +++ b/src/query/graph.hpp @@ -13,9 +13,7 @@ #include #include - #include "query/db_accessor.hpp" -#include "query/path.hpp" #include "utils/logging.hpp" #include "utils/memory.hpp" #include "utils/pmr/unordered_set.hpp" @@ -23,6 +21,7 @@ namespace memgraph::query { +class Path; /** * A data structure that holds a graph. A graph consists of at least one * vertex, and zero or more edges. @@ -36,18 +35,17 @@ class Graph final { * Create the graph with no elements * Allocations are done using the given MemoryResource. */ - explicit Graph(utils::MemoryResource *memory) : vertices_(memory), edges_(memory) {} + explicit Graph(utils::MemoryResource *memory); /** Construct a copy using the given utils::MemoryResource */ - Graph(const Graph &other, utils::MemoryResource *memory) - : vertices_(other.vertices_, memory), edges_(other.edges_, memory) {} + Graph(const Graph &other, utils::MemoryResource *memory); /** * Construct with the value of other. * utils::MemoryResource is obtained from other. After the move, other will be * empty. */ - Graph(Graph &&other) noexcept : Graph(std::move(other), other.GetMemoryResource()) {} + Graph(Graph &&other) noexcept; /** * Construct with the value of other, but use the given utils::MemoryResource. @@ -55,42 +53,24 @@ class Graph final { * *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) {} + Graph(Graph &&other, utils::MemoryResource *memory); /** Expands the graph with the given path. */ - void Expand(const Path &path) { - const auto path_vertices_ = path.vertices(); - const auto path_edges_ = path.edges(); - std::for_each(path_vertices_.begin(), path_vertices_.end(), - [this](const VertexAccessor v) { vertices_.insert(v); }); - std::for_each(path_edges_.begin(), path_edges_.end(), [this](const EdgeAccessor e) { edges_.insert(e); }); - } - - std::vector OutEdges(query::VertexAccessor vertex_accessor) { - std::vector out_edges; - for (auto it = edges_.begin(); it != edges_.end(); ++it) { - if (it->From() == vertex_accessor) { - out_edges.emplace_back(*it); - } - } - return out_edges; - } + void Expand(const Path &path); + std::vector OutEdges(VertexAccessor vertex_accessor); /** Move assign other, utils::MemoryResource of `this` is used. */ - Graph &operator=(Graph &&) = default; - - ~Graph() = default; + Graph &operator=(Graph &&); /** Returns the number of expansions (edges) in this path. */ - auto size() const { return edges_.size(); } + auto size() const; - auto &vertices() { return vertices_; } - auto &edges() { return edges_; } - const auto &vertices() const { return vertices_; } - const auto &edges() const { return edges_; } + auto &vertices(); + auto &edges(); + const auto &vertices() const; + const auto &edges() const; - utils::MemoryResource *GetMemoryResource() const { return vertices_.get_allocator().GetMemoryResource(); } + utils::MemoryResource *GetMemoryResource() const; private: // Contains all the vertices in the Graph.