diff --git a/include/mg_procedure.h b/include/mg_procedure.h index a88c99993..45eef1139 100644 --- a/include/mg_procedure.h +++ b/include/mg_procedure.h @@ -450,6 +450,16 @@ struct mgp_edges_iterator; /// Free the memory used by a mgp_edges_iterator. void mgp_edges_iterator_destroy(struct mgp_edges_iterator *it); +/// ID of a vertex; valid during a single query execution. +struct mgp_vertex_id { + int64_t as_int; +}; + +/// Get the ID of given vertex. +/// The ID is only valid for a single query execution, you should never store it +/// globally in a query module. +struct mgp_vertex_id mgp_vertex_get_id(const struct mgp_vertex *v); + /// Copy a mgp_vertex. /// Returned pointer must be freed with mgp_vertex_destroy. /// NULL is returned if unable to allocate a mgp_vertex. @@ -552,6 +562,13 @@ struct mgp_properties_iterator *mgp_edge_iter_properties( /// State of the graph database. struct mgp_graph; +/// Return the vertex corresponding to given ID. +/// The returned vertex must be freed using mgp_vertex_destroy. +/// NULL is returned if unable to allocate the vertex or if ID is not valid. +struct mgp_vertex *mgp_graph_get_vertex_by_id(const struct mgp_graph *g, + struct mgp_vertex_id id, + struct mgp_memory *memory); + /// Iterator over vertices. struct mgp_vertices_iterator; diff --git a/src/query/db_accessor.hpp b/src/query/db_accessor.hpp index fa70e5e0b..f05f703ee 100644 --- a/src/query/db_accessor.hpp +++ b/src/query/db_accessor.hpp @@ -528,6 +528,13 @@ class DbAccessor final { explicit DbAccessor(storage::Storage::Accessor *accessor) : accessor_(accessor) {} + std::optional FindVertex(storage::Gid gid, + storage::View view) { + auto maybe_vertex = accessor_->FindVertex(gid, view); + if (maybe_vertex) return VertexAccessor(*maybe_vertex); + return std::nullopt; + } + VerticesIterable Vertices(storage::View view) { return VerticesIterable(accessor_->Vertices(view)); } @@ -730,6 +737,14 @@ class DbAccessor final { } } + std::optional FindVertex(storage::Gid gid, + storage::View view) { + auto maybe_vertex = + dba_->FindVertexOptional(gid, view == storage::View::NEW); + if (maybe_vertex) return VertexAccessor(*maybe_vertex); + return std::nullopt; + } + auto Vertices(storage::View view) { auto vertices = dba_->Vertices(view == storage::View::NEW); return VerticesIterable(std::move(vertices)); diff --git a/src/query/procedure/mg_procedure_impl.cpp b/src/query/procedure/mg_procedure_impl.cpp index 62b0398d1..a2457d7d3 100644 --- a/src/query/procedure/mg_procedure_impl.cpp +++ b/src/query/procedure/mg_procedure_impl.cpp @@ -858,6 +858,10 @@ const mgp_property *mgp_properties_iterator_next(mgp_properties_iterator *it) { } } +mgp_vertex_id mgp_vertex_get_id(const mgp_vertex *v) { + return mgp_vertex_id{.as_int = v->impl.Gid().AsInt()}; +} + mgp_vertex *mgp_vertex_copy(const mgp_vertex *v, mgp_memory *memory) { return new_mgp_object(memory, *v); } @@ -1184,6 +1188,15 @@ mgp_properties_iterator *mgp_edge_iter_properties(const mgp_edge *e, } } +mgp_vertex *mgp_graph_get_vertex_by_id(const mgp_graph *graph, mgp_vertex_id id, + mgp_memory *memory) { + auto maybe_vertex = + graph->impl->FindVertex(storage::Gid::FromInt(id.as_int), graph->view); + if (maybe_vertex) + return new_mgp_object(memory, *maybe_vertex, graph); + return nullptr; +} + void mgp_vertices_iterator_destroy(mgp_vertices_iterator *it) { delete_mgp_object(it); }