From 5505257daa4ce17c86f425003f48fef29aefe5b5 Mon Sep 17 00:00:00 2001 From: antoniofilipovic Date: Tue, 16 Aug 2022 14:22:49 +0200 Subject: [PATCH] implement edge insertion --- src/query/db_accessor.cpp | 12 +++++-- src/query/db_accessor.hpp | 2 +- src/query/procedure/mg_procedure_impl.cpp | 43 +++++++++++------------ 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/query/db_accessor.cpp b/src/query/db_accessor.cpp index d271d0617..9531ab38b 100644 --- a/src/query/db_accessor.cpp +++ b/src/query/db_accessor.cpp @@ -57,10 +57,16 @@ storage::Result> SubgraphDbAccessor::RemoveEdge(Edge return result; } -storage::Result SubgraphDbAccessor::InsertEdge(VertexAccessor *from, VertexAccessor *to, +storage::Result SubgraphDbAccessor::InsertEdge(SubgraphVertexAccessor *from, SubgraphVertexAccessor *to, const storage::EdgeTypeId &edge_type) { - auto result = db_accessor_->InsertEdge(from, to, edge_type); - // todo antoniofilipovic add edge to subgraph + VertexAccessor *from_impl = &from->impl_; + VertexAccessor *to_impl = &to->impl_; + + auto result = db_accessor_->InsertEdge(from_impl, to_impl, edge_type); + if (result.HasError()) { + return result; + } + this->graph_->InsertEdge(*result); return result; } diff --git a/src/query/db_accessor.hpp b/src/query/db_accessor.hpp index 73c89f4d5..d134d7e90 100644 --- a/src/query/db_accessor.hpp +++ b/src/query/db_accessor.hpp @@ -479,7 +479,7 @@ class SubgraphDbAccessor final { storage::Result> RemoveEdge(EdgeAccessor *edge); - storage::Result InsertEdge(VertexAccessor *from, VertexAccessor *to, + storage::Result InsertEdge(SubgraphVertexAccessor *from, SubgraphVertexAccessor *to, const storage::EdgeTypeId &edge_type); storage::Result>>> DetachRemoveVertex( diff --git a/src/query/procedure/mg_procedure_impl.cpp b/src/query/procedure/mg_procedure_impl.cpp index ee4c98979..e16c32c1b 100644 --- a/src/query/procedure/mg_procedure_impl.cpp +++ b/src/query/procedure/mg_procedure_impl.cpp @@ -2385,28 +2385,27 @@ mgp_error mgp_graph_create_edge(mgp_graph *graph, mgp_vertex *from, mgp_vertex * if (!MgpGraphIsMutable(*graph)) { throw ImmutableObjectException{"Cannot create an edge in an immutable graph!"}; } - auto edge = std::visit(memgraph::utils::Overloaded{ - [from, to, type](memgraph::query::DbAccessor *impl) { - if (std::holds_alternative(from->impl) || - std::holds_alternative(to->impl)) { - throw std::logic_error{"Both vertices must be of VertexAccessorType"}; - } - return impl->InsertEdge(&std::get(from->impl), - &std::get(to->impl), - impl->NameToEdgeType(type.name)); - }, - [from, to, type](memgraph::query::SubgraphDbAccessor *impl) { - // todo antoniofilipovic change this, it is wrong here since it needs to be - // SubgraphVertexAccessor - if (std::holds_alternative(from->impl) || - std::holds_alternative(to->impl)) { - throw std::logic_error{"Wrong type"}; - } - return impl->InsertEdge(&std::get(from->impl), - &std::get(to->impl), - impl->NameToEdgeType(type.name)); - }}, - graph->impl); + auto edge = + std::visit(memgraph::utils::Overloaded{ + [from, to, type](memgraph::query::DbAccessor *impl) { + if (std::holds_alternative(from->impl) || + std::holds_alternative(to->impl)) { + throw std::logic_error{"Both vertices must be of VertexAccessor type"}; + } + return impl->InsertEdge(&std::get(from->impl), + &std::get(to->impl), + impl->NameToEdgeType(type.name)); + }, + [from, to, type](memgraph::query::SubgraphDbAccessor *impl) { + if (std::holds_alternative(from->impl) || + std::holds_alternative(to->impl)) { + throw std::logic_error{"Both vertices must be of SubgraphVertexAccessor type"}; + } + return impl->InsertEdge(&std::get(from->impl), + &std::get(to->impl), + impl->NameToEdgeType(type.name)); + }}, + graph->impl); if (edge.HasError()) { switch (edge.GetError()) {