Separate decl/def between source and header on files db_accessor and graph

This commit is contained in:
Kostas Kyrimis
2022-08-11 17:10:33 +03:00
parent c65ba1618b
commit 7d41c580dc
5 changed files with 185 additions and 84 deletions

View File

@@ -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)

92
src/query/db_accessor.cpp Normal file
View File

@@ -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<std::optional<EdgeAccessor>> SubgraphDbAccessor::RemoveEdge(EdgeAccessor *edge) {
auto result = db_accessor_->RemoveEdge(edge);
// todo antoniofilipovic remove edge from subgraph
return result;
}
storage::Result<EdgeAccessor> 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<std::optional<std::pair<VertexAccessor, std::vector<EdgeAccessor>>>>
SubgraphDbAccessor::DetachRemoveVertex(VertexAccessor *vertex_accessor) {
auto result = db_accessor_->DetachRemoveVertex(vertex_accessor);
// todo antoniofilipovic remove vertex and edges from subgraph
return result;
}
storage::Result<std::optional<VertexAccessor>> 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<VertexAccessor> 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

View File

@@ -17,7 +17,6 @@
#include <cppitertools/imap.hpp>
#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<std::optional<EdgeAccessor>> RemoveEdge(EdgeAccessor *edge) {
auto result = db_accessor_->RemoveEdge(edge);
// todo antoniofilipovic remove edge from subgraph
return result;
}
storage::Result<std::optional<EdgeAccessor>> RemoveEdge(EdgeAccessor *edge);
storage::Result<EdgeAccessor> 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<std::optional<std::pair<VertexAccessor, std::vector<EdgeAccessor>>>> 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<std::optional<VertexAccessor>> RemoveVertex(VertexAccessor *vertex_accessor) {
auto result = db_accessor_->RemoveVertex(vertex_accessor);
// todo antoniofilipovic remove vertex from subgraph
return result;
}
storage::Result<std::optional<VertexAccessor>> 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<VertexAccessor> 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<VertexAccessor> FindVertex(storage::Gid gid, storage::View view);
query::Graph *getGraph() { return graph_; }
Graph *getGraph();
};
// class SubgraphEdgeAccessor final {

57
src/query/graph.cpp Normal file
View File

@@ -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<query::EdgeAccessor> Graph::OutEdges(query::VertexAccessor vertex_accessor) {
std::vector<query::EdgeAccessor> 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

View File

@@ -13,9 +13,7 @@
#include <functional>
#include <utility>
#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<query::EdgeAccessor> OutEdges(query::VertexAccessor vertex_accessor) {
std::vector<query::EdgeAccessor> 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<EdgeAccessor> 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.