Skip to content

Commit

Permalink
Implement algo.cover
Browse files Browse the repository at this point in the history
  • Loading branch information
mpintaric55334 committed Oct 11, 2023
1 parent 9694f86 commit 79f49db
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cpp/algo_module/algo_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
extern "C" int mgp_init_module(struct mgp_module *module, struct mgp_memory *memory) {
try {
mgp::MemoryDispatcherGuard guard{memory};
AddProcedure(Algo::Cover, std::string(Algo::kProcedureCover).c_str(), mgp::ProcedureType::Read,
{mgp::Parameter(std::string(Algo::kCoverArg1).c_str(), {mgp::Type::List, mgp::Type::Node})},
{mgp::Return(std::string(Algo::kCoverRet1).c_str(), mgp::Type::Relationship)}, module, memory);

} catch (const std::exception &e) {
return 1;
Expand Down
27 changes: 27 additions & 0 deletions cpp/algo_module/algorithm/algo.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
#include "algo.hpp"

void Algo::Cover(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 list_nodes = arguments[0].ValueList();
std::unordered_set<mgp::Node> nodes;
for (const auto elem : list_nodes) {
auto node = elem.ValueNode();
nodes.insert(node);
}

for (auto node : nodes) {
for (auto rel : node.OutRelationships()) {
if (nodes.find(rel.To()) != nodes.end()) {
auto record = record_factory.NewRecord();
record.Insert(std::string(kCoverRet1).c_str(), rel);
}
}
}

} catch (const std::exception &e) {
record_factory.SetErrorMessage(e.what());
return;
}
}
11 changes: 10 additions & 1 deletion cpp/algo_module/algorithm/algo.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#pragma once

#include <mgp.hpp>
#include <unordered_set>

namespace Algo {} // namespace Algo
namespace Algo {

/* cover constants */
constexpr std::string_view kProcedureCover = "cover";
constexpr std::string_view kCoverArg1 = "nodes";
constexpr std::string_view kCoverRet1 = "rel";

void Cover(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
} // namespace Algo
1 change: 1 addition & 0 deletions e2e/algo_test/test_cover1/input.cyp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE (d:Dog),(h:Human),(ho:House), (d)-[l:LOVES]->(h),(h)-[:LIVES_IN]->(ho),(d)-[se:SELF_REL]->(d)
9 changes: 9 additions & 0 deletions e2e/algo_test/test_cover1/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
query: >
MATCH (n)
WITH collect(n) AS list
CALL algo.cover(list) YIELD rel RETURN rel;
output:
- rel: {'label': 'LIVES_IN','properties': {}}
- rel: {'label': 'LOVES','properties': {}}
- rel: {'label': 'SELF_REL','properties': {}}
1 change: 1 addition & 0 deletions e2e/algo_test/test_cover2/input.cyp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE (d:Dog),(h:Human),(ho:House), (d)-[l:LOVES]->(h),(h)-[:LIVES_IN]->(ho),(d)-[se:SELF_REL]->(d)
6 changes: 6 additions & 0 deletions e2e/algo_test/test_cover2/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query: >
MATCH (d:Dog)
CALL algo.cover([d]) YIELD rel RETURN rel;
output:
- rel: {'label': 'SELF_REL','properties': {}}
1 change: 1 addition & 0 deletions e2e/algo_test/test_cover3/input.cyp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MERGE (d:Dog {name: "Rex" ,id:0}) WITH d UNWIND range(0, 1000) AS id MERGE (h:Human {name: "Humie" + id, id:id}) MERGE (d)-[l:LOVES {id:id, how:"very"}]->(h);
7 changes: 7 additions & 0 deletions e2e/algo_test/test_cover3/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query: >
MATCH (n)
WITH collect(n) AS list
CALL algo.cover(list) YIELD rel RETURN count(rel) AS count;
output:
- count: 1001

0 comments on commit 79f49db

Please sign in to comment.