Refactor update vertices

This commit is contained in:
jbajic
2022-05-16 10:32:41 +02:00
parent 9c53a9943b
commit 061d0f8aa6
3 changed files with 56 additions and 26 deletions

View File

@@ -272,17 +272,19 @@ struct CreateEdgesRequest {
4: list<NewEdge> new_edges;
}
// We still use different request for removing properties and labels
struct UpdateProperty {
1: required binary name;
2: required Value value;
struct UpdateVertex {
1: required VertexId vertex_id;
2: list<i64> create_label_ids;
3: list<i64> delete_label_ids;
4: map<i64, Value> properties;
}
// Null signifies removel of label or property
struct UpdateVerticesRequest {
1: required i64 transaction_id;
2: required list<VertexId> vertices_id;
3: list<UpdateProperty> updated_props;
4: list<binary> added_labels;
2: map<i64, binary> (cpp.template = "std::unordered_map") labels_name_map;
3: map<i64, binary> (cpp.template = "std::unordered_map") property_name_map;
4: list<UpdateVertex> update_vertices;
}
service Storage {

View File

@@ -16,6 +16,7 @@
#include <ratio>
#include <thread>
#include <bits/ranges_algo.h>
#include <folly/Executor.h>
#include <folly/Unit.h>
#include <folly/executors/IOThreadPoolExecutor.h>
@@ -232,20 +233,31 @@ void CreateEdge(const std::shared_ptr<StorageAsyncClient> &client, uint64_t src,
.get();
}
void UpdateVerticesProperties(const std::shared_ptr<StorageAsyncClient> &client, std::vector<uint64_t> vertices_id,
std::vector<std::string_view> labels, std::vector<std::string_view> property_names) {
void UpdateVertexProperties(const std::shared_ptr<StorageAsyncClient> &client, uint64_t vertex_id,
std::vector<std::string_view> add_labels, std::vector<std::string_view> remove_labels,
std::vector<std::string_view> property_names) {
interface::storage::UpdateVerticesRequest request {};
request.added_labels_ref()->assign(labels.cbegin(), labels.cend());
request.vertices_id_ref()->assign(vertices_id.cbegin(), vertices_id.cend());
auto &update_vertex = request.update_vertices_ref()->emplace_back();
int prop_count = 10;
for (const auto prop : property_names) {
interface::storage::UpdateProperty update_prop {};
update_prop.name = prop;
update_prop.value.set_int_v(prop_count);
request.updated_props_ref()->emplace_back(update_prop);
request.property_name_map()->emplace(prop_count, prop);
interface::storage::Value prop_value {};
prop_value.set_int_v(prop_count);
update_vertex.properties_ref()->emplace(prop_count, std::move(prop_value));
prop_count++;
}
int label_count = 1;
for (const auto add_label : add_labels) {
request.labels_name_map_ref()->emplace(label_count, add_label);
update_vertex.create_label_ids_ref()->push_back(label_count);
label_count++;
}
for (const auto remove_label : remove_labels) {
request.labels_name_map_ref()->emplace(label_count, remove_label);
update_vertex.delete_label_ids_ref()->push_back(label_count);
label_count++;
}
client->future_startTransaction()
.then([client, request = std::move(request)](folly::Try<int64_t> &&result) mutable {
@@ -305,8 +317,9 @@ int main(int argc, char *argv[]) {
// This should fail
// CreateEdge(client, 12121212, 2121212121, "type5", {});
UpdateVerticesProperties(client, {0, 1, 2}, {}, {"proop", "new_prop"});
UpdateVerticesProperties(client, {0}, {"el_label"}, {"proop", "new_new_prop"});
UpdateVertexProperties(client, 0, {"label3"}, {"label1", "label2"}, {"proop", "new_prop"});
UpdateVertexProperties(client, 1, {"el_label", "el_labelo"}, {}, {"proop", "new_new_prop"});
UpdateVertexProperties(client, 1, {"el_label"}, {"el_labelo"}, {"proop", "new_news_new_prop"});
const auto transaction_id = client->future_startTransaction().get();
std::vector<std::string> props{"proop", "prooop2"};

View File

@@ -194,9 +194,13 @@ void StorageServiceHandler::createEdges(::interface::storage::Result &result,
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");
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");
if (!to_node) {
throw std::runtime_error("Destination node must be in the storage");
}
auto edge = accessor->CreateEdge(&*from_node, &*to_node, accessor->NameToEdgeType(type));
for (const auto &[prop_id, prop] : new_edge.get_properties()) {
@@ -215,21 +219,32 @@ void StorageServiceHandler::updateVertices(::interface::storage::Result &result,
spdlog::info("Updating vertices...");
result.success_ref() = false;
auto accessor = active_transactions_.at(req.get_transaction_id());
const auto &labels_map = req.get_labels_name_map();
const auto &property_names_map = req.get_property_name_map();
for (const auto vertex_id : req.get_vertices_id()) {
auto vertex = accessor->FindVertex(memgraph::storage::Gid::FromInt(vertex_id), memgraph::storage::View::NEW);
for (const auto &update_vertex : req.get_update_vertices()) {
auto vertex =
accessor->FindVertex(memgraph::storage::Gid::FromInt(update_vertex.vertex_id), memgraph::storage::View::NEW);
if (!vertex) {
throw std::runtime_error("Vertex not found in storage");
}
for (const auto label_id : update_vertex.get_create_label_ids()) {
if (const auto result = vertex->AddLabel(accessor->NameToLabel(labels_map.at(label_id))); result.HasError()) {
return;
}
}
for (const auto label_id : update_vertex.get_delete_label_ids()) {
if (const auto result = vertex->RemoveLabel(accessor->NameToLabel(labels_map.at(label_id))); result.HasError()) {
return;
}
}
for (auto &updated_prop : req.get_updated_props()) {
if (const auto result = vertex->SetProperty(accessor->NameToProperty(updated_prop.name),
ThriftValueToPropertyValue(updated_prop.value));
for (auto &[prop_id, prop] : *update_vertex.properties_ref()) {
if (const auto result = vertex->SetProperty(accessor->NameToProperty(property_names_map.at(prop_id)),
ThriftValueToPropertyValue(prop));
result.HasError()) {
return;
}
// TODO Add label
}
}