Create edge

This commit is contained in:
jbajic
2022-05-12 16:20:43 +02:00
parent 3dd90e2acb
commit bdf794e229
4 changed files with 88 additions and 0 deletions

View File

@@ -259,6 +259,18 @@ struct CreateVerticesRequest {
4: list<NewVertex> new_vertices;
}
struct NewEdge {
1: VertexId src;
2: VertexId dest;
3: EdgeType type;
4: map<i64, Value> properties;
}
struct CreateEdgesRequest {
1: required i64 transaction_id;
3: map<i64, binary> (cpp.template = "std::unordered_map") property_name_map;
4: list<NewEdge> 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)

View File

@@ -10,6 +10,7 @@
// licenses/APL.txt.
#include <chrono>
#include <cstdint>
#include <iostream>
#include <optional>
#include <ratio>
@@ -188,6 +189,49 @@ void CreateVertex(const std::shared_ptr<StorageAsyncClient> &client, std::vector
.get();
}
void CreateEdge(const std::shared_ptr<StorageAsyncClient> &client, uint64_t src, uint64_t dest, std::string type,
std::vector<std::string_view> 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<int64_t> &&result) mutable {
if (result.hasException()) {
LOG(INFO) << "FAILED1: " << result.exception().get_exception()->what() << std::endl;
return folly::makeFuture<interface::storage::Result>(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<interface::storage::Result> &&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<void> &&reply) {
return folly::makeFuture<interface::storage::Result>(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<std::string> props{"proop", "prooop2"};
auto res = ScanVertices(client, transaction_id, std::nullopt, props, 3);

View File

@@ -279,6 +279,28 @@ std::function<memgraph::utils::BasicResult<std::string>(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;

View File

@@ -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;