Skip to content

Commit de90df7

Browse files
authored
Rewrite refactor modules to not use deleted API (#536)
1 parent 7fe9a2b commit de90df7

File tree

6 files changed

+63
-44
lines changed

6 files changed

+63
-44
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ To learn more about development with MAGE and Docker, visit the
222222
- python3-dev
223223
- clang
224224
- unixodbc
225+
- uuid-dev
225226

226227

227228
Since Memgraph needs to load MAGE's modules, there is the `setup` script to help you. With it, you can build the modules so that Memgraph

cpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ add_subdirectory(text_module)
141141
add_subdirectory(path_module)
142142
add_subdirectory(node_module)
143143
add_subdirectory(neighbors_module)
144-
#add_subdirectory(refactor_module) this will get added back in next PR
144+
add_subdirectory(refactor_module)
145145
add_subdirectory(merge_module)
146146
add_subdirectory(csv_utils_module)
147147
add_subdirectory(algo_module)

cpp/refactor_module/algorithm/refactor.cpp

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,29 @@ void ThrowInvalidTypeException(const mgp::Value &value) {
1515
}
1616
} // namespace
1717

18+
namespace {
19+
void CopyRelProperties(mgp::Relationship &to, mgp::Relationship const &from) {
20+
for (auto const &[k, v] : from.Properties()) {
21+
to.SetProperty(k, v);
22+
}
23+
}
24+
} // namespace
25+
1826
void Refactor::From(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
1927
mgp::MemoryDispatcherGuard guard{memory};
2028
auto arguments = mgp::List(args);
2129
const auto record_factory = mgp::RecordFactory(result);
2230
try {
23-
mgp::Relationship relationship{arguments[0].ValueRelationship()};
24-
const mgp::Node new_from{arguments[1].ValueNode()};
31+
auto relationship{arguments[0].ValueRelationship()};
32+
auto const new_from{arguments[1].ValueNode()};
33+
2534
mgp::Graph graph{memgraph_graph};
35+
auto new_relationship = graph.CreateRelationship(relationship.To(), new_from, relationship.Type());
36+
CopyRelProperties(new_relationship, relationship);
37+
graph.DeleteRelationship(relationship);
2638

27-
graph.SetFrom(relationship, new_from);
2839
auto record = record_factory.NewRecord();
29-
record.Insert(std::string(kFromResult).c_str(), relationship);
40+
record.Insert(std::string(kFromResult).c_str(), new_relationship);
3041

3142
} catch (const std::exception &e) {
3243
record_factory.SetErrorMessage(e.what());
@@ -39,13 +50,16 @@ void Refactor::To(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result,
3950
auto arguments = mgp::List(args);
4051
const auto record_factory = mgp::RecordFactory(result);
4152
try {
42-
mgp::Relationship relationship{arguments[0].ValueRelationship()};
43-
const mgp::Node new_to{arguments[1].ValueNode()};
53+
auto relationship{arguments[0].ValueRelationship()};
54+
auto const new_to{arguments[1].ValueNode()};
55+
4456
mgp::Graph graph{memgraph_graph};
57+
auto new_relationship = graph.CreateRelationship(relationship.From(), new_to, relationship.Type());
58+
CopyRelProperties(new_relationship, relationship);
59+
graph.DeleteRelationship(relationship);
4560

46-
graph.SetTo(relationship, new_to);
4761
auto record = record_factory.NewRecord();
48-
record.Insert(std::string(kToResult).c_str(), relationship);
62+
record.Insert(std::string(kToResult).c_str(), new_relationship);
4963

5064
} catch (const std::exception &e) {
5165
record_factory.SetErrorMessage(e.what());
@@ -58,8 +72,8 @@ void Refactor::RenameLabel(mgp_list *args, mgp_graph *memgraph_graph, mgp_result
5872
auto arguments = mgp::List(args);
5973
const auto record_factory = mgp::RecordFactory(result);
6074
try {
61-
const auto old_label{arguments[0].ValueString()};
62-
const auto new_label{arguments[1].ValueString()};
75+
auto old_label{arguments[0].ValueString()};
76+
auto new_label{arguments[1].ValueString()};
6377
const auto nodes{arguments[2].ValueList()};
6478

6579
int64_t nodes_changed{0};
@@ -403,25 +417,28 @@ void Refactor::CloneNodes(mgp_list *args, mgp_graph *memgraph_graph, mgp_result
403417
}
404418
}
405419

406-
void Refactor::InvertRel(mgp::Graph &graph, mgp::Relationship &rel) {
420+
mgp::Relationship Refactor::InvertRel(mgp::Graph &graph, mgp::Relationship &rel) {
407421
const auto old_from = rel.From();
408422
const auto old_to = rel.To();
409-
graph.SetFrom(rel, old_to);
410-
graph.SetTo(rel, old_from);
423+
424+
auto new_rel = graph.CreateRelationship(old_to, old_from, rel.Type());
425+
CopyRelProperties(new_rel, rel);
426+
graph.DeleteRelationship(rel);
427+
return new_rel;
411428
}
412429

413430
void Refactor::Invert(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
414431
mgp::MemoryDispatcherGuard guard{memory};
415432
const auto arguments = mgp::List(args);
416433
const auto record_factory = mgp::RecordFactory(result);
417434
try {
418-
mgp::Graph graph = mgp::Graph(memgraph_graph);
435+
auto graph = mgp::Graph(memgraph_graph);
419436
mgp::Relationship rel = arguments[0].ValueRelationship();
420437

421-
InvertRel(graph, rel);
438+
auto relationship = InvertRel(graph, rel);
422439
auto record = record_factory.NewRecord();
423-
record.Insert(std::string(kResultIdInvert).c_str(), rel.Id().AsInt());
424-
record.Insert(std::string(kResultRelationshipInvert).c_str(), rel);
440+
record.Insert(std::string(kResultIdInvert).c_str(), relationship.Id().AsInt());
441+
record.Insert(std::string(kResultRelationshipInvert).c_str(), relationship);
425442
record.Insert(std::string(kResultErrorInvert).c_str(), "");
426443
} catch (const std::exception &e) {
427444
record_factory.SetErrorMessage(e.what());
@@ -549,11 +566,11 @@ void Refactor::RenameTypeProperty(mgp_list *args, mgp_graph *memgraph_graph, mgp
549566
for (auto rel_value : rels) {
550567
auto rel = rel_value.ValueRelationship();
551568
const auto prop_value = rel.GetProperty(old_name);
552-
/*since there is no bug(prop map cant have null values), it is faster to just check isNull
569+
/*since there is no bug(prop map cant have null values), it is faster to just check isNull
553570
instead of copying entire properties map and then find*/
554-
if (prop_value.IsNull()) {
555-
continue;
556-
}
571+
if (prop_value.IsNull()) {
572+
continue;
573+
}
557574
rel.RemoveProperty(old_name);
558575
rel.SetProperty(new_name, prop_value);
559576
rels_changed++;
@@ -628,12 +645,15 @@ void Refactor::DeleteAndReconnect(mgp_list *args, mgp_graph *memgraph_graph, mgp
628645

629646
const auto modify_relationship = [&graph, &relationships](mgp::Relationship relationship, const mgp::Node &node,
630647
int64_t other_node_id) {
631-
if (relationship.From().Id().AsInt() == other_node_id) {
632-
graph.SetTo(relationship, node);
633-
} else {
634-
graph.SetFrom(relationship, node);
635-
}
636-
relationships.AppendExtend(mgp::Value(relationship));
648+
auto new_relationship = std::invoke([&]() {
649+
if (relationship.From().Id().AsInt() == other_node_id) {
650+
return graph.CreateRelationship(relationship.From(), node, relationship.Type());
651+
}
652+
return graph.CreateRelationship(node, relationship.To(), relationship.Type());
653+
});
654+
CopyRelProperties(new_relationship, relationship);
655+
graph.DeleteRelationship(relationship);
656+
relationships.AppendExtend(mgp::Value(new_relationship));
637657
};
638658

639659
const auto merge_relationships = [](mgp::Relationship &rel, mgp::Relationship &other, bool combine = false) {
@@ -667,17 +687,19 @@ void Refactor::DeleteAndReconnect(mgp_list *args, mgp_graph *memgraph_graph, mgp
667687
} else {
668688
new_type = std::string(old_rel.Type()) + "_" + std::string(new_rel.Type());
669689
}
670-
graph.ChangeType(new_rel, new_type);
690+
auto new_relationship = graph.CreateRelationship(new_rel.From(), new_rel.To(), new_type);
691+
CopyRelProperties(new_relationship, new_rel);
692+
graph.DeleteRelationship(new_rel);
671693

672694
if (config.prop_strategy == PropertiesStrategy::DISCARD) {
673-
modify_relationship(new_rel, node, prev_non_deleted_node_id);
674-
merge_relationships(new_rel, old_rel);
695+
modify_relationship(new_relationship, node, prev_non_deleted_node_id);
696+
merge_relationships(new_relationship, old_rel);
675697
} else if (config.prop_strategy == PropertiesStrategy::OVERRIDE) {
676-
modify_relationship(new_rel, path.GetNodeAt(prev_non_deleted_path_index), id);
677-
merge_relationships(new_rel, old_rel);
698+
modify_relationship(new_relationship, path.GetNodeAt(prev_non_deleted_path_index), id);
699+
merge_relationships(new_relationship, old_rel);
678700
} else { // PropertiesStrategy::COMBINE
679-
modify_relationship(new_rel, node, prev_non_deleted_node_id);
680-
merge_relationships(new_rel, old_rel, true);
701+
modify_relationship(new_relationship, node, prev_non_deleted_node_id);
702+
merge_relationships(new_relationship, old_rel, true);
681703
}
682704
}
683705
} else if (!delete_node && prev_non_deleted_path_index != -1) {
@@ -825,7 +847,9 @@ void Refactor::RenameType(mgp_list *args, mgp_graph *memgraph_graph, mgp_result
825847
for (auto relationship_value : relationships) {
826848
auto relationship{relationship_value.ValueRelationship()};
827849
if (relationship.Type() == old_type) {
828-
graph.ChangeType(relationship, new_type);
850+
auto new_relationship = graph.CreateRelationship(relationship.From(), relationship.To(), new_type);
851+
CopyRelProperties(new_relationship, relationship);
852+
graph.DeleteRelationship(relationship);
829853
++rels_changed;
830854
}
831855
}

cpp/refactor_module/algorithm/refactor.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ void CollapseNode(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result,
160160

161161
void Invert(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
162162

163-
void InvertRel(mgp::Graph &graph, mgp::Relationship &rel);
163+
mgp::Relationship InvertRel(mgp::Graph &graph, mgp::Relationship &rel);
164164

165165
void DeleteAndReconnect(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
166166

167167
void RenameTypeProperty(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
168168

169-
void NormalizeAsBoolean(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
169+
void NormalizeAsBoolean(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
170170

171171
void ExtractNode(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
172172

e2e/test_module.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ def prepare_tests():
111111
):
112112
continue
113113

114-
if module_test_dir.name == 'refactor_test': # temporarily disable to make CI pass due to API changes
115-
continue
116-
117114
for test_or_group_dir in module_test_dir.iterdir():
118115
if not test_or_group_dir.is_dir():
119116
continue

e2e_correctness/test_modules.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ def get_all_tests():
8383
):
8484
continue
8585

86-
if module_test_dir.name == 'refactor_test': # temporarily disable to make CI pass due to API changes
87-
continue
88-
8986
for test_or_group_dir in module_test_dir.iterdir():
9087
if not test_or_group_dir.is_dir():
9188
continue

0 commit comments

Comments
 (0)