query/procedure: Expose vertex ID through C API

Reviewers: mferencevic, ipaljak

Reviewed By: ipaljak

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D2568
This commit is contained in:
Teon Banek 2019-11-26 15:19:34 +01:00
parent 03ecc58715
commit df6ce53c67
3 changed files with 45 additions and 0 deletions

View File

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

View File

@ -528,6 +528,13 @@ class DbAccessor final {
explicit DbAccessor(storage::Storage::Accessor *accessor)
: accessor_(accessor) {}
std::optional<VertexAccessor> 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<VertexAccessor> 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<decltype(vertices)>(std::move(vertices));

View File

@ -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<mgp_vertex>(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<mgp_vertex>(memory, *maybe_vertex, graph);
return nullptr;
}
void mgp_vertices_iterator_destroy(mgp_vertices_iterator *it) {
delete_mgp_object(it);
}