Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle analytical mode in community_detection during parallel changes #400

Merged
merged 24 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
323cec8
Handle analytical mode in community_detection during parallel changes
Oct 23, 2023
8b9d4e0
Fix core dump when vertex is already deleted from memgraph graph
Oct 24, 2023
fa23b72
First create for vertex existence and insert if exists
Oct 26, 2023
0805098
Add fixes for the C and C++ APIs
antepusic Nov 6, 2023
c314170
Fix doubled ; after statement
antepusic Nov 9, 2023
4c8f555
Improve the C and C++ API fixes
antepusic Nov 9, 2023
f404305
Fix C API result returning
antepusic Nov 9, 2023
57e5909
Add remaining deletion checks in the C++ API
antepusic Nov 10, 2023
5d93ba3
Resolve merge conflict
antepusic Nov 15, 2023
4d4727c
Merge branch 'main' into handle-analytical-mode
antepusic Nov 15, 2023
507aec0
Add review suggestions
antepusic Nov 25, 2023
baac657
Merge branch 'main' into handle-analytical-mode
antepusic Nov 25, 2023
710dac5
Merge branch 'handle-analytical-mode' of https://github.com/memgraph/…
antepusic Nov 25, 2023
a8106dc
Add review comments
antepusic Nov 27, 2023
23cb924
Fix build
antepusic Nov 27, 2023
865f6be
Merge branch 'main' into handle-analytical-mode
antepusic Dec 5, 2023
c67d161
Get checks to use latest memgraph
antepusic Dec 5, 2023
86589fb
Merge branch 'handle-analytical-mode' of https://github.com/memgraph/…
antepusic Dec 5, 2023
05e08b9
Fix version
antepusic Dec 5, 2023
bc70a80
Update submodule to current master
antepusic Dec 5, 2023
508425a
Fix node existence check
antepusic Dec 5, 2023
a2e6b3c
Merge branch 'main' into handle-analytical-mode
antepusic Dec 5, 2023
e8180fc
Extract duplicate code into a method
antepusic Dec 5, 2023
1cd20ac
Remove duplication in community_detection_online
antepusic Dec 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion cpp/community_detection_module/community_detection_module.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cstdint>
#include <mg_exceptions.hpp>
#include <mg_utils.hpp>

Expand All @@ -24,10 +25,20 @@ const double kDefaultWeight = 1.0;

void InsertLouvainRecord(mgp_graph *graph, mgp_result *result, mgp_memory *memory, const std::uint64_t node_id,
const std::uint64_t community) {
auto *vertex = mgp::graph_get_vertex_by_id(graph, mgp_vertex_id{.as_int = static_cast<int64_t>(node_id)}, memory);
if (!vertex) {
if (mgp::graph_is_transactional(graph)) {
throw mg_exception::InvalidIDException();
}
// For Analytical mode it's possible that some vertices/edges are missing
// because of changes in parallel transactions.
return;
}

mgp_result_record *record = mgp::result_new_record(result);
if (record == nullptr) throw mg_exception::NotEnoughMemoryException();

mg_utility::InsertNodeValueResult(graph, record, kFieldNode, node_id, memory);
mg_utility::InsertNodeValueResult(record, kFieldNode, vertex, memory);
mg_utility::InsertIntValueResult(record, kFieldCommunity, community, memory);
}

Expand Down
10 changes: 10 additions & 0 deletions cpp/mg_utility/data_structures/graph_view.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <optional>
#include <vector>

#include "graph_data.hpp"
Expand Down Expand Up @@ -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<TSize>, which is null if vertex does not exist
///
virtual std::optional<TSize> GetInnerNodeIdOpt(std::uint64_t memgraph_id) const = 0;
as51340 marked this conversation as resolved.
Show resolved Hide resolved

///
///@brief Get the Memgraph Edge Id from the inner renumbered node ID
///
Expand Down
31 changes: 29 additions & 2 deletions cpp/mg_utility/mg_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,14 @@ class Graph : public GraphView<TSize> {
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);
Expand Down Expand Up @@ -314,6 +320,21 @@ class Graph : public GraphView<TSize> {
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<TSize> 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);
gitbuda marked this conversation as resolved.
Show resolved Hide resolved
gitbuda marked this conversation as resolved.
Show resolved Hide resolved
}

///
/// Returns the Memgraph database ID from graph view
///
Expand Down Expand Up @@ -381,6 +402,10 @@ class Graph : public GraphView<TSize> {
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.
Expand All @@ -402,5 +427,7 @@ class Graph : public GraphView<TSize> {
std::unordered_map<std::uint64_t, TSize> memgraph_to_inner_edge_id_;

std::multimap<std::pair<TSize, TSize>, TSize> nodes_to_edge_;

bool is_transactional_;
};
} // namespace mg_graph
11 changes: 9 additions & 2 deletions cpp/mg_utility/mg_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include <memory>
#include <unordered_set>

#include "_mgp.hpp"
#include "mg_graph.hpp"
#include "mgp.hpp"
#include "_mgp.hpp"

namespace mg_graph {

Expand Down Expand Up @@ -119,6 +119,8 @@ std::unique_ptr<mg_graph::Graph<TSize>> GetGraphView(mgp_graph *memgraph_graph,
const char *weight_property = nullptr,
double default_weight = 1.0) {
auto graph = std::make_unique<mg_graph::Graph<TSize>>();
bool isTransactionalStorage = mgp::graph_is_transactional(memgraph_graph);
graph->SetIsTransactional(isTransactionalStorage);
gitbuda marked this conversation as resolved.
Show resolved Hide resolved

///
/// Mapping Memgraph in-memory vertices into the graph view
Expand Down Expand Up @@ -281,10 +283,15 @@ void InsertNodeValueResult(mgp_result_record *record, const char *field_name, mg

/// Inserts a node with its ID node_id to create a vertex and insert
/// the node to the field field_name of the record mgp_result_record record.
void InsertNodeValueResult(mgp_graph *graph, mgp_result_record *record, const char *field_name, const int node_id,
/// Returns true is insert is successful, false otherwise
bool InsertNodeValueResult(mgp_graph *graph, mgp_result_record *record, const char *field_name, const int node_id,
mgp_memory *memory) {
auto *vertex = mgp::graph_get_vertex_by_id(graph, mgp_vertex_id{.as_int = node_id}, memory);
if (!vertex) {
return false;
}
InsertNodeValueResult(record, field_name, vertex, memory);
return true;
}

/// Inserts a relationship of value edge_value to the field field_name of
Expand Down