FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mage/cpp/node_module/algorithm/node.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
/
node_module
/
algorithm
/
node.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
231 lines (207 loc) · 7.12 KB
Breadcrumbs
mage
/
cpp
/
node_module
/
algorithm
/
node.cpp
Copy path
File metadata and controls
231 lines (207 loc) · 7.12 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
#
include
"
node.hpp
"
#
include
<
unordered_set
>
#
include
"
mgp.hpp
"
namespace
{
std::unordered_map<std::string_view,
uint8_t
>
GetTypeDirection
(
const
mgp::Value &types) {
std::unordered_map<std::string_view,
uint8_t
> result;
for
(
const
auto
&type_value : types.
ValueList
()) {
auto
type = type_value.
ValueString
();
if
(type.
starts_with
(
'
<
'
)) {
if
(type.
ends_with
(
'
>
'
)) {
throw
mgp::ValueException
(
"
<type> format not allowed. Use type instead.
"
);
}
result[type.
substr
(
1
, type.
size
() -
1
)] |=
1
;
}
else
if
(type.
ends_with
(
'
>
'
)) {
result[type.
substr
(
0
, type.
size
() -
1
)] |=
2
;
}
else
{
result[type] |=
3
;
}
}
return
result;
}
mgp::List
GetRelationshipTypes
(
const
mgp::Value &node_value,
const
mgp::Value &types_value) {
auto
type_direction =
GetTypeDirection
(types_value);
std::unordered_set<std::string_view> types;
const
auto
node = node_value.
ValueNode
();
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[relationship.
Type
()] &
1
) {
types.
insert
(relationship.
Type
());
}
}
for
(
const
auto
relationship : node.
OutRelationships
()) {
if
(type_direction[relationship.
Type
()] &
2
) {
types.
insert
(relationship.
Type
());
}
}
}
mgp::List result{types.
size
()};
for
(
const
auto
&type : types) {
auto
value =
mgp::Value
(type);
result.
Append
(value);
}
return
result;
}
}
//
namespace
bool
Node::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
(
auto
rel : node.
OutRelationships
()) {
if
(
std::string
(rel.
Type
()) == rel_type && direction !=
'
<
'
) {
return
true
;
}
}
for
(
auto
rel : node.
InRelationships
()) {
if
(
std::string
(rel.
Type
()) == rel_type && direction !=
'
>
'
) {
return
true
;
}
}
return
false
;
}
void
Node::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::Node node = arguments[
0
].
ValueNode
();
const
mgp::List relationships = arguments[
1
].
ValueList
();
if
(relationships.
Size
() ==
0
) {
throw
mgp::ValueException
(
"
Input relationships list must not be empty!
"
);
}
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
));
}
}
auto
record = record_factory.
NewRecord
();
record.
Insert
(
std::string
(
kReturnRelationshipsExist
).
c_str
(),
std::move
(relationship_map));
}
catch
(
const
std::exception &e) {
record_factory.
SetErrorMessage
(e.
what
());
return
;
}
}
bool
Node::FindRelationship
(std::unordered_set<std::string_view> types, mgp::Relationships relationships) {
if
(types.
contains
(
"
"
) && relationships.
cbegin
() != relationships.
cend
()) {
return
true
;
}
for
(
auto
relationship : relationships) {
if
(types.
contains
(relationship.
Type
())) {
return
true
;
}
}
return
false
;
}
void
Node::RelationshipExists
(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
auto
node = arguments[
0
].
ValueNode
();
auto
patterns = arguments[
1
].
ValueList
();
if
(patterns.
Empty
()) {
patterns.
AppendExtend
(
mgp::Value
(
"
"
));
}
std::unordered_set<std::string_view> in_rels;
std::unordered_set<std::string_view> out_rels;
for
(
auto
pattern_value : patterns) {
auto
pattern = pattern_value.
ValueString
();
if
(pattern[
0
] ==
'
<
'
&& pattern[pattern.
size
() -
1
] ==
'
>
'
) {
throw
mgp::ValueException
(
"
Invalid relationship specification!
"
);
}
if
(pattern[
0
] ==
'
<
'
) {
in_rels.
insert
(pattern.
substr
(
1
, pattern.
size
()));
continue
;
}
if
(pattern[pattern.
size
() -
1
] ==
'
>
'
) {
out_rels.
insert
(pattern.
substr
(
0
, pattern.
size
() -
1
));
continue
;
}
in_rels.
insert
(pattern);
out_rels.
insert
(pattern);
}
auto
record = record_factory.
NewRecord
();
record.
Insert
(
std::string
(
kResultRelationshipExists
).
c_str
(),
FindRelationship
(in_rels, node.
InRelationships
()) ||
FindRelationship
(out_rels, node.
OutRelationships
()));
}
catch
(
const
std::exception &e) {
record_factory.
SetErrorMessage
(e.
what
());
return
;
}
}
void
Node::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
(arguments[
0
], arguments[
1
]));
}
catch
(
const
std::exception &e) {
record_factory.
SetErrorMessage
(e.
what
());
return
;
}
}
void
Node::DegreeIn
(mgp_list *args, mgp_func_context *ctx, mgp_func_result *res, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const
auto
arguments =
mgp::List
(args);
auto
result =
mgp::Result
(res);
try
{
const
auto
node = arguments[
0
].
ValueNode
();
const
auto
type = arguments[
1
].
ValueString
();
if
(type.
size
() ==
0
) {
result.
SetValue
((
int64_t
)node.
InDegree
());
return
;
}
int64_t
degree =
0
;
for
(
const
auto
rel : node.
InRelationships
()) {
if
(rel.
Type
() == type) {
degree +=
1
;
}
}
result.
SetValue
(degree);
}
catch
(
const
std::exception &e) {
result.
SetErrorMessage
(e.
what
());
return
;
}
}
void
Node::DegreeOut
(mgp_list *args, mgp_func_context *ctx, mgp_func_result *res, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const
auto
arguments =
mgp::List
(args);
auto
result =
mgp::Result
(res);
try
{
const
auto
node = arguments[
0
].
ValueNode
();
const
auto
type = arguments[
1
].
ValueString
();
if
(type.
size
() ==
0
) {
result.
SetValue
((
int64_t
)node.
OutDegree
());
return
;
}
int64_t
degree =
0
;
for
(
const
auto
rel : node.
OutRelationships
()) {
if
(rel.
Type
() == type) {
degree +=
1
;
}
}
result.
SetValue
(degree);
}
catch
(
const
std::exception &e) {
result.
SetErrorMessage
(e.
what
());
return
;
}
}
Back
|
FazBrowse Home
|
New Git URL