implement remove vertex

This commit is contained in:
antoniofilipovic 2022-08-16 14:03:16 +02:00
parent 30638dbc75
commit 93a2de96c1
5 changed files with 32 additions and 12 deletions

View File

@ -71,10 +71,15 @@ SubgraphDbAccessor::DetachRemoveVertex(VertexAccessor *vertex_accessor) {
return result;
}
storage::Result<std::optional<VertexAccessor>> SubgraphDbAccessor::RemoveVertex(VertexAccessor *vertex_accessor) {
storage::Result<std::optional<VertexAccessor>> SubgraphDbAccessor::RemoveVertex(
SubgraphVertexAccessor *subgraphvertex_accessor) {
VertexAccessor *vertex_accessor = &subgraphvertex_accessor->impl_;
auto result = db_accessor_->RemoveVertex(vertex_accessor);
// todo antoniofilipovic remove vertex from subgraph
return result;
if (result.HasError() || !*result) {
return result;
}
return this->graph_->RemoveVertex(*vertex_accessor);
;
}
SubgraphVertexAccessor SubgraphDbAccessor::InsertVertex() {
@ -100,6 +105,8 @@ std::optional<VertexAccessor> SubgraphDbAccessor::FindVertex(storage::Gid gid, s
query::Graph *SubgraphDbAccessor::getGraph() { return graph_; }
VertexAccessor SubgraphVertexAccessor::GetVertexAccessor() { return impl_; }
auto SubgraphVertexAccessor::OutEdges(storage::View view) -> decltype(impl_.OutEdges(view)) const {
// todo antoniofilipovic add filtering here

View File

@ -225,6 +225,7 @@ class SubgraphVertexAccessor final {
storage::Result<storage::PropertyValue> SetProperty(storage::PropertyId key, const storage::PropertyValue &value) {
return impl_.SetProperty(key, value);
}
VertexAccessor GetVertexAccessor();
};
} // namespace memgraph::query
@ -484,7 +485,7 @@ class SubgraphDbAccessor final {
storage::Result<std::optional<std::pair<VertexAccessor, std::vector<EdgeAccessor>>>> DetachRemoveVertex(
VertexAccessor *vertex_accessor);
storage::Result<std::optional<VertexAccessor>> RemoveVertex(VertexAccessor *vertex_accessor);
storage::Result<std::optional<VertexAccessor>> RemoveVertex(SubgraphVertexAccessor *vertex_accessor);
SubgraphVertexAccessor InsertVertex();

View File

@ -42,6 +42,17 @@ bool Graph::ContainsVertex(const VertexAccessor &vertex) {
return false;
}
std::optional<VertexAccessor> Graph::RemoveVertex(const VertexAccessor &vertex) {
if (!ContainsVertex(vertex)) {
return std::nullopt;
}
auto value = vertices_.erase(vertex);
if (value == 0) {
return std::nullopt;
}
return vertex;
}
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) {

View File

@ -60,6 +60,7 @@ class Graph final {
void InsertVertex(const VertexAccessor &vertex);
void InsertEdge(const EdgeAccessor &edge);
bool ContainsVertex(const VertexAccessor &vertex);
std::optional<VertexAccessor> RemoveVertex(const VertexAccessor &vertex);
std::vector<EdgeAccessor> OutEdges(VertexAccessor vertex_accessor);

View File

@ -2273,19 +2273,19 @@ mgp_error mgp_graph_delete_vertex(struct mgp_graph *graph, mgp_vertex *vertex) {
const auto result =
std::visit(memgraph::utils::Overloaded{
[vertex](memgraph::query::DbAccessor *impl) {
[&](memgraph::query::DbAccessor *impl) {
if (std::holds_alternative<memgraph::query::SubgraphVertexAccessor>(vertex->impl)) {
throw std::logic_error{"Wrong type"};
throw std::logic_error{
"Remove vertex for DbAccessor should not get reference to SubgraphVertexAccessor"};
}
return impl->RemoveVertex(&std::get<memgraph::query::VertexAccessor>(vertex->impl));
},
[vertex](memgraph::query::SubgraphDbAccessor *impl) {
// todo antoniofilipovic change this, it is wrong here since it needs to be
// SubgraphVertexAccessor
if (std::holds_alternative<memgraph::query::SubgraphVertexAccessor>(vertex->impl)) {
throw std::logic_error{"Wrong type"};
[&](memgraph::query::SubgraphDbAccessor *impl) {
if (std::holds_alternative<memgraph::query::VertexAccessor>(vertex->impl)) {
throw std::logic_error{
"Remove vertex for SubgraphDbAccessor should not get reference to VertexAccessor"};
}
return impl->RemoveVertex(&std::get<memgraph::query::VertexAccessor>(vertex->impl));
return impl->RemoveVertex(&(std::get<memgraph::query::SubgraphVertexAccessor>(vertex->impl)));
}},
graph->impl);