Skip to content

Commit 409c726

Browse files
idorabanIgnition
authored andcommitted
Implemented label.exists procedure
1 parent 57b76f0 commit 409c726

File tree

11 files changed

+89
-0
lines changed

11 files changed

+89
-0
lines changed

cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,5 @@ add_subdirectory(periodic_module)
148148
add_subdirectory(map_module)
149149
add_subdirectory(collections_module)
150150
add_subdirectory(text_module)
151+
add_subdirectory(label_module)
151152
add_cugraph_subdirectory(cugraph_module)

cpp/label_module/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set(label_module_src
2+
label_module.cpp
3+
algorithm/label.cpp)
4+
5+
add_query_module(label 1 "${label_module_src}")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "label.hpp"
2+
3+
void Label::Exists(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
4+
mgp::memory = memory;
5+
const auto arguments = mgp::List(args);
6+
const auto record_factory = mgp::RecordFactory(result);
7+
try {
8+
bool exists = false;
9+
10+
const auto label = arguments[1].ValueString();
11+
if (arguments[0].IsNode()) {
12+
const auto node = arguments[0].ValueNode();
13+
exists = node.HasLabel(label);
14+
}
15+
16+
auto record = record_factory.NewRecord();
17+
record.Insert(std::string(kResultExists).c_str(), std::move(exists));
18+
19+
} catch (const std::exception &e) {
20+
record_factory.SetErrorMessage(e.what());
21+
return;
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <mgp.hpp>
4+
5+
namespace Label {
6+
7+
/* exists constants */
8+
constexpr std::string_view kReturnExists = "exists";
9+
constexpr std::string_view kProcedureExists = "exists";
10+
constexpr std::string_view kArgumentsNode = "node";
11+
constexpr std::string_view kArgumentsLabel = "label";
12+
constexpr std::string_view kResultExists = "exists";
13+
14+
void Exists(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
15+
16+
} // namespace Label

cpp/label_module/label_module.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <mgp.hpp>
2+
3+
#include "algorithm/label.hpp"
4+
5+
extern "C" int mgp_init_module(struct mgp_module *module, struct mgp_memory *memory) {
6+
try {
7+
mgp::memory = memory;
8+
AddProcedure(Label::Exists, Label::kProcedureExists, mgp::ProcedureType::Read,
9+
{mgp::Parameter(Label::kArgumentsNode, mgp::Type::Any),
10+
mgp::Parameter(Label::kArgumentsLabel, mgp::Type::String)},
11+
{mgp::Return(Label::kReturnExists, mgp::Type::Bool)}, module, memory);
12+
13+
} catch (const std::exception &e) {
14+
return 1;
15+
}
16+
17+
return 0;
18+
}
19+
20+
extern "C" int mgp_shutdown_module() { return 0; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE (s1:Student {name: 'Ana'});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query: >
2+
MATCH (s1:Student {name: 'Ana'})
3+
CALL label.exists(s1, "Teacher") YIELD exists
4+
RETURN exists
5+
6+
output:
7+
- exists: False
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MERGE (s1:Student {name: 'Ana'}) MERGE (s2:Student {name: 'Marija'}) CREATE (s1)-[k:KNOWS]->(s2);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query: >
2+
MATCH (:Student)-[k:KNOWS]->(:Student)
3+
CALL label.exists(k, "Knows") YIELD exists
4+
RETURN exists
5+
6+
output:
7+
- exists: False
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE (s1:Student {name: 'Ana'});

0 commit comments

Comments
 (0)