From bdf794e229272e5cc3562bdc66ee232b8186424d Mon Sep 17 00:00:00 2001 From: jbajic Date: Thu, 12 May 2022 16:20:43 +0200 Subject: [PATCH] Create edge --- src/interface/storage.thrift | 13 +++++++ tests/manual/storage_client_demo.cpp | 51 ++++++++++++++++++++++++++++ tests/manual/storage_service.cpp | 22 ++++++++++++ tests/manual/storage_service.hpp | 2 ++ 4 files changed, 88 insertions(+) diff --git a/src/interface/storage.thrift b/src/interface/storage.thrift index b165c270d..6fba4b586 100644 --- a/src/interface/storage.thrift +++ b/src/interface/storage.thrift @@ -259,6 +259,18 @@ struct CreateVerticesRequest { 4: list new_vertices; } +struct NewEdge { + 1: VertexId src; + 2: VertexId dest; + 3: EdgeType type; + 4: map properties; +} + +struct CreateEdgesRequest { + 1: required i64 transaction_id; + 3: map (cpp.template = "std::unordered_map") property_name_map; + 4: list new_edges; +} service Storage { i64 startTransaction() @@ -266,6 +278,7 @@ service Storage { void abortTransaction(1: i64 transaction_id) Result createVertices(1: CreateVerticesRequest req) + Result createEdges(1: CreateEdgesRequest req) ScanVerticesResponse scanVertices(1: ScanVerticesRequest req) GetPropertiesResponse getProperties(1: GetPropertiesRequest req) ExpandOneResponse expandOne(1: ExpandOneRequest req) diff --git a/tests/manual/storage_client_demo.cpp b/tests/manual/storage_client_demo.cpp index a8d04e0ed..377d1dda9 100644 --- a/tests/manual/storage_client_demo.cpp +++ b/tests/manual/storage_client_demo.cpp @@ -10,6 +10,7 @@ // licenses/APL.txt. #include +#include #include #include #include @@ -188,6 +189,49 @@ void CreateVertex(const std::shared_ptr &client, std::vector .get(); } +void CreateEdge(const std::shared_ptr &client, uint64_t src, uint64_t dest, std::string type, + std::vector property_names) { + interface::storage::CreateEdgesRequest request {}; + auto &new_edge = request.new_edges_ref()->emplace_back(); + new_edge.src_ref().emplace(src); + new_edge.dest_ref().emplace(dest); + new_edge.type_ref()->name_ref().emplace(type); + + int prop_count = 1; + for (const auto prop : property_names) { + request.property_name_map()->emplace(prop_count, prop); + interface::storage::Value prop_value {}; + prop_value.set_int_v(prop_count); + new_edge.properties_ref()->emplace(prop_count, std::move(prop_value)); + prop_count++; + } + + client->future_startTransaction() + .then([client, request = std::move(request)](folly::Try &&result) mutable { + if (result.hasException()) { + LOG(INFO) << "FAILED1: " << result.exception().get_exception()->what() << std::endl; + return folly::makeFuture(std::runtime_error("failed to start transaction")); + } + const auto transaction_id = result.value(); + request.transaction_id_ref() = transaction_id; + LOG(INFO) << "Sending message..."; + + return client->future_createEdges(request).then( + [transaction_id, client](folly::Try &&reply) { + LOG(INFO) << "Closing transaction"; + if (reply.hasException()) { + LOG(INFO) << "FAILED2: " << reply.exception().get_exception()->what() << std::endl; + return client->future_abortTransaction(transaction_id).then([](folly::Try &&reply) { + return folly::makeFuture(std::runtime_error("edge creation failed")); + }); + } + LOG(INFO) << "SUCCESS\n"; + return client->future_commitTransaction(transaction_id); + }); + }) + .get(); +} + int main(int argc, char *argv[]) { FLAGS_logtostderr = true; folly::init(&argc, &argv); @@ -213,6 +257,13 @@ int main(int argc, char *argv[]) { CreateVertex(client, {"label1", "label4"}, {"proop", "prooop3"}); CreateVertex(client, {"label1", "label5"}, {"proop", "prooop4"}); + // TODO: Until we have hash-based vertex id implemented this is temporary + CreateEdge(client, 0, 1, "type1", {"proop", "prooop2"}); + CreateEdge(client, 1, 2, "type1", {"proop", "prooop2"}); + CreateEdge(client, 2, 3, "type5", {}); + // This should fail + CreateEdge(client, 12121212, 2121212121, "type5", {}); + const auto transaction_id = client->future_startTransaction().get(); std::vector props{"proop", "prooop2"}; auto res = ScanVertices(client, transaction_id, std::nullopt, props, 3); diff --git a/tests/manual/storage_service.cpp b/tests/manual/storage_service.cpp index adac8738d..f89db3fce 100644 --- a/tests/manual/storage_service.cpp +++ b/tests/manual/storage_service.cpp @@ -279,6 +279,28 @@ std::function(memgraph::storage::Verte }; } +void StorageServiceHandler::createEdges(::interface::storage::Result &result, + const ::interface::storage::CreateEdgesRequest &req) { + spdlog::info("Creating edges..."); + result.success_ref() = false; + auto accessor = active_transactions_.at(req.get_transaction_id()); + const auto &property_names_map = req.get_property_name_map(); + + for (auto &new_edge : *req.new_edges_ref()) { + const auto src = new_edge.src().value(); + const auto dest = new_edge.dest().value(); + const auto type = new_edge.type()->name().value(); + + auto from_node = accessor->FindVertex(memgraph::storage::Gid::FromInt(src), memgraph::storage::View::NEW); + if (!from_node) throw std::runtime_error("Source node must be in the storage"); + auto to_node = accessor->FindVertex(memgraph::storage::Gid::FromInt(dest), memgraph::storage::View::NEW); + if (!to_node) throw std::runtime_error("Destination node must be in the storage"); + + auto relationship = accessor->CreateEdge(&*from_node, &*to_node, accessor->NameToEdgeType(type)); + } + spdlog::info("Edges creation done!"); +} + void StorageServiceHandler::scanVertices(::interface::storage::ScanVerticesResponse &resp, const ::interface::storage::ScanVerticesRequest &req) { resp.result_ref()->success_ref() = false; diff --git a/tests/manual/storage_service.hpp b/tests/manual/storage_service.hpp index b24c3cece..9a384678c 100644 --- a/tests/manual/storage_service.hpp +++ b/tests/manual/storage_service.hpp @@ -33,6 +33,8 @@ class StorageServiceHandler final : public interface::storage::StorageSvIf { void createVertices(::interface::storage::Result &result, const ::interface::storage::CreateVerticesRequest &req) override; + void createEdges(::interface::storage::Result &result, const ::interface::storage::CreateEdgesRequest &req) override; + void scanVertices(::interface::storage::ScanVerticesResponse &resp, const ::interface::storage::ScanVerticesRequest &req) override;