diff --git a/cpp/mg_utility/data_structures/graph_view.hpp b/cpp/mg_utility/data_structures/graph_view.hpp index 8f5157efc..9af3ba253 100644 --- a/cpp/mg_utility/data_structures/graph_view.hpp +++ b/cpp/mg_utility/data_structures/graph_view.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "graph_data.hpp" @@ -97,9 +98,18 @@ class GraphView { /// ///@param memgraph_id Memgraph's internal ID ///@return TSize + ///@throws mg_exception::InvalidIDException if vertex does not exist /// virtual TSize GetInnerNodeId(std::uint64_t memgraph_id) const = 0; + /// + ///@brief Get the Inner Node ID from Memgraph ID + /// + ///@param memgraph_id Memgraph's internal ID + ///@return std::optional, which is null if vertex does not exist + /// + virtual std::optional GetInnerNodeIdOpt(std::uint64_t memgraph_id) const = 0; + /// ///@brief Get the Memgraph Edge Id from the inner renumbered node ID /// diff --git a/cpp/mg_utility/mg_graph.hpp b/cpp/mg_utility/mg_graph.hpp index d2679fdb3..3ba665e3e 100644 --- a/cpp/mg_utility/mg_graph.hpp +++ b/cpp/mg_utility/mg_graph.hpp @@ -199,8 +199,14 @@ class Graph : public GraphView { return std::nullopt; } - auto from = GetInnerNodeId(memgraph_id_from); - auto to = GetInnerNodeId(memgraph_id_to); + auto fromOpt = GetInnerNodeIdOpt(memgraph_id_from); + auto toOpt = GetInnerNodeIdOpt(memgraph_id_to); + if (!fromOpt || !toOpt) { + return std::nullopt; + } + + auto from = *fromOpt; + auto to = *toOpt; auto id = edges_.size(); inner_to_memgraph_edge_id_.emplace(id, edge_id); @@ -314,6 +320,21 @@ class Graph : public GraphView { return memgraph_to_inner_id_.at(memgraph_id); } + /// + /// Returns the GraphView ID from Memgraph's internal ID + /// + /// @param node_id Memgraphs's inner ID + /// + std::optional GetInnerNodeIdOpt(std::uint64_t memgraph_id) const override { + if (memgraph_to_inner_id_.find(memgraph_id) == memgraph_to_inner_id_.end()) { + if (IsTransactional()) { + throw mg_exception::InvalidIDException(); + } + return std::nullopt; + } + return memgraph_to_inner_id_.at(memgraph_id); + } + /// /// Returns the Memgraph database ID from graph view /// @@ -381,6 +402,10 @@ class Graph : public GraphView { nodes_to_edge_.clear(); } + void SetIsTransactional(bool is_transactional) { is_transactional_ = is_transactional; } + + bool IsTransactional() const { return is_transactional_; } + private: // Constant is used for marking deleted edges. // If edge id is equal to constant, edge is deleted. @@ -402,5 +427,7 @@ class Graph : public GraphView { std::unordered_map memgraph_to_inner_edge_id_; std::multimap, TSize> nodes_to_edge_; + + bool is_transactional_; }; } // namespace mg_graph diff --git a/cpp/mg_utility/mg_utils.hpp b/cpp/mg_utility/mg_utils.hpp index 41630a31b..6567e5e97 100644 --- a/cpp/mg_utility/mg_utils.hpp +++ b/cpp/mg_utility/mg_utils.hpp @@ -9,9 +9,9 @@ #include #include +#include "_mgp.hpp" #include "mg_graph.hpp" #include "mgp.hpp" -#include "_mgp.hpp" namespace mg_graph { @@ -119,6 +119,8 @@ std::unique_ptr> GetGraphView(mgp_graph *memgraph_graph, const char *weight_property = nullptr, double default_weight = 1.0) { auto graph = std::make_unique>(); + bool isTransactionalStorage = mgp::graph_is_transactional(memgraph_graph); + graph->SetIsTransactional(isTransactionalStorage); /// /// Mapping Memgraph in-memory vertices into the graph view