FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mage/cpp/nodes_module/algorithm/nodes.cpp at main · memgraph/mage · GitHub
This repository was archived by the owner on Jan 23, 2026. It is now read-only.
memgraph
/
mage
Public archive
Notifications
You must be signed in to change notification settings
Fork
35
Star
331
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mage
/
cpp
/
nodes_module
/
algorithm
/
nodes.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
233 lines (206 loc) · 7.64 KB
Breadcrumbs
mage
/
cpp
/
nodes_module
/
algorithm
/
nodes.cpp
Copy path
File metadata and controls
233 lines (206 loc) · 7.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#
include
"
nodes.hpp
"
#
include
<
unordered_set
>
#
include
"
mgp.hpp
"
bool
Nodes::RelationshipExist
(
const
mgp::Node &node, std::string &rel_type) {
char
direction{
'
'
};
if
(rel_type[
0
] ==
'
<
'
&& rel_type[rel_type.
size
() -
1
] ==
'
>
'
) {
throw
mgp::ValueException
(
"
Invalid relationship specification!
"
);
}
else
if
(rel_type[rel_type.
size
() -
1
] ==
'
>
'
) {
direction = rel_type[rel_type.
size
() -
1
];
rel_type.
pop_back
();
}
else
if
(rel_type[
0
] ==
'
<
'
) {
direction = rel_type[
0
];
rel_type.
erase
(
0
,
1
);
}
for
(
const
auto
rel : node.
OutRelationships
()) {
if
(
std::string
(rel.
Type
()) == rel_type && direction !=
'
<
'
) {
return
true
;
}
}
for
(
const
auto
rel : node.
InRelationships
()) {
if
(
std::string
(rel.
Type
()) == rel_type && direction !=
'
>
'
) {
return
true
;
}
}
return
false
;
}
void
Nodes::RelationshipsExist
(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const
auto
arguments =
mgp::List
(args);
const
auto
record_factory =
mgp::RecordFactory
(result);
try
{
const
mgp::Graph graph =
mgp::Graph
(memgraph_graph);
const
mgp::List nodes = arguments[
0
].
ValueList
();
const
mgp::List relationships = arguments[
1
].
ValueList
();
if
(nodes.
Size
() ==
0
|| relationships.
Size
() ==
0
) {
throw
mgp::ValueException
(
"
Input lists must not be empty!
"
);
}
for
(
auto
element: nodes) {
if
(!element.
IsNode
() && !element.
IsInt
()){
throw
mgp::ValueException
(
"
Input arguments must be nodes or their ID's
"
);
}
mgp::Node node = element.
IsNode
() ? element.
ValueNode
() : graph.
GetNodeById
(
mgp::Id::FromInt
(element.
ValueInt
()));
mgp::Map return_map =
mgp::Map
();
mgp::Map relationship_map =
mgp::Map
();
for
(
auto
rel : relationships) {
std::string rel_type{rel.
ValueString
()};
if
(
RelationshipExist
(node, rel_type)) {
relationship_map.
Insert
(rel.
ValueString
(),
mgp::Value
(
true
));
}
else
{
relationship_map.
Insert
(rel.
ValueString
(),
mgp::Value
(
false
));
}
}
return_map.
Insert
(
std::string
(
kNodeRelationshipsExist
).
c_str
(),
mgp::Value
(node));
return_map.
Insert
(
std::string
(
kRelationshipsExistStatus
).
c_str
(),
mgp::Value
(
std::move
(relationship_map)));
auto
record = record_factory.
NewRecord
();
record.
Insert
(
std::string
(
kReturnRelationshipsExist
).
c_str
(),
std::move
(return_map));
}
}
catch
(
const
std::exception &e) {
record_factory.
SetErrorMessage
(e.
what
());
return
;
}
}
namespace
{
void
ThrowException
(
const
mgp::Value &value) {
std::ostringstream oss;
oss << value.
Type
();
throw
mgp::ValueException
(
"
Unsuppported type for this operation, received type:
"
+ oss.
str
());
};
mgp::Value
InsertNodeRelationshipTypes
(
const
mgp::Node &node,
std::unordered_map<std::string,
uint8_t
> &type_direction) {
mgp::Map result{};
result.
Insert
(
"
node
"
,
mgp::Value
(node));
std::unordered_set<std::string_view> types;
if
(type_direction.
empty
()) {
for
(
const
auto
relationship : node.
InRelationships
()) {
types.
insert
(relationship.
Type
());
}
for
(
const
auto
relationship : node.
OutRelationships
()) {
types.
insert
(relationship.
Type
());
}
}
else
{
for
(
const
auto
relationship : node.
InRelationships
()) {
if
(type_direction[
std::string
(relationship.
Type
())] &
1
) {
types.
insert
(relationship.
Type
());
}
}
for
(
const
auto
relationship : node.
OutRelationships
()) {
if
(type_direction[
std::string
(relationship.
Type
())] &
2
) {
types.
insert
(relationship.
Type
());
}
}
}
mgp::List types_list{types.
size
()};
for
(
const
auto
&type : types) {
auto
value =
mgp::Value
(type);
types_list.
Append
(value);
}
result.
Insert
(
"
types
"
,
mgp::Value
(
std::move
(types_list)));
return
mgp::Value
(
std::move
(result));
}
std::unordered_map<std::string,
uint8_t
>
GetTypeDirection
(
const
mgp::Value &types) {
std::unordered_map<std::string,
uint8_t
> result;
for
(
const
auto
&type_value : types.
ValueList
()) {
//
string view of temporary object ValueList(), invalid after the loop
auto
type = type_value.
ValueString
();
if
(type.
starts_with
(
'
<
'
)) {
if
(type.
ends_with
(
'
>
'
)) {
throw
mgp::ValueException
(
"
<type> format not allowed. Use type instead.
"
);
}
auto
key =
std::string
(type.
substr
(
1
, type.
size
() -
1
));
result[key] |=
1
;
}
else
if
(type.
ends_with
(
'
>
'
)) {
auto
key =
std::string
(type.
substr
(
0
, type.
size
() -
1
));
result[key] |=
2
;
}
else
{
auto
key =
std::string
(type);
result[key] |=
3
;
}
}
return
result;
}
mgp::List
GetRelationshipTypes
(mgp_graph *memgraph_graph,
const
mgp::Value &argument,
const
mgp::Value &types) {
mgp::List result{};
mgp::Graph graph{memgraph_graph};
auto
type_direction =
GetTypeDirection
(types);
auto
ParseNode = [&](
const
mgp::Value &value) {
if
(value.
IsNode
()) {
result.
AppendExtend
(
InsertNodeRelationshipTypes
(value.
ValueNode
(), type_direction));
}
else
if
(value.
IsInt
()) {
result.
AppendExtend
(
InsertNodeRelationshipTypes
(graph.
GetNodeById
(
mgp::Id::FromInt
(value.
ValueInt
())), type_direction));
}
else
{
ThrowException
(value);
}
};
if
(!argument.
IsList
()) {
ParseNode
(argument);
return
result;
}
for
(
const
auto
&list_item : argument.
ValueList
()) {
ParseNode
(list_item);
}
return
result;
}
void
DetachDeleteNode
(
const
mgp::Value &node, mgp::Graph &graph) {
if
(node.
IsInt
()) {
graph.
DetachDeleteNode
(graph.
GetNodeById
(
mgp::Id::FromInt
(node.
ValueInt
())));
}
else
if
(node.
IsNode
()) {
graph.
DetachDeleteNode
(node.
ValueNode
());
}
else
{
ThrowException
(node);
}
}
}
//
namespace
void
Nodes::RelationshipTypes
(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const
auto
arguments =
mgp::List
(args);
const
auto
record_factory =
mgp::RecordFactory
(result);
try
{
auto
record = record_factory.
NewRecord
();
record.
Insert
(
std::string
(
kResultRelationshipTypes
).
c_str
(),
GetRelationshipTypes
(memgraph_graph, arguments[
0
], arguments[
1
]));
}
catch
(
const
std::exception &e) {
record_factory.
SetErrorMessage
(e.
what
());
return
;
}
}
void
Nodes::Delete
(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const
auto
arguments =
mgp::List
(args);
const
auto
record_factory =
mgp::RecordFactory
(result);
try
{
mgp::Graph graph{memgraph_graph};
auto
nodes{arguments[
0
]};
if
(!nodes.
IsList
()) {
DetachDeleteNode
(nodes, graph);
return
;
}
for
(
const
auto
&list_item : nodes.
ValueList
()) {
DetachDeleteNode
(list_item, graph);
}
}
catch
(
const
std::exception &e) {
record_factory.
SetErrorMessage
(e.
what
());
return
;
}
}
void
Nodes::Link
(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const
auto
arguments =
mgp::List
(args);
const
auto
record_factory =
mgp::RecordFactory
(result);
try
{
mgp::Graph graph =
mgp::Graph
(memgraph_graph);
const
mgp::List list_nodes = arguments[
0
].
ValueList
();
const
std::string_view type{arguments[
1
].
ValueString
()};
const
size_t
size = list_nodes.
Size
();
if
(size < minimumNodeListSize) {
throw
mgp::ValueException
(
"
You need to input at least 2 nodes
"
);
}
for
(
size_t
i =
0
; i < size -
1
; i++) {
graph.
CreateRelationship
(list_nodes[i].
ValueNode
(), list_nodes[i +
1
].
ValueNode
(), type);
}
}
catch
(
const
std::exception &e) {
record_factory.
SetErrorMessage
(e.
what
());
return
;
}
}
Back
|
FazBrowse Home
|
New Git URL