From 55e0dbca804201bac4ab30c127b43c6e1890994c Mon Sep 17 00:00:00 2001 From: jeremy Date: Fri, 21 Oct 2022 16:32:49 +0200 Subject: [PATCH 001/103] Add limit to ExpandOne Add missing pragma Add test Merge conflicts --- src/storage/v3/expr.hpp | 2 + src/storage/v3/request_helper.cpp | 77 ++++++++++----------- src/storage/v3/request_helper.hpp | 65 +++++++++++++++-- src/storage/v3/shard_rsm.cpp | 111 +++++++++++++++++++++++------- tests/simulation/shard_rsm.cpp | 65 +++++++++++++++++ 5 files changed, 248 insertions(+), 72 deletions(-) diff --git a/src/storage/v3/expr.hpp b/src/storage/v3/expr.hpp index c3199abf1..7eefdd71c 100644 --- a/src/storage/v3/expr.hpp +++ b/src/storage/v3/expr.hpp @@ -9,6 +9,8 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. +#pragma once + #include #include "db_accessor.hpp" diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index bb1c8bca4..7dab4c5c1 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -19,43 +19,6 @@ namespace memgraph::storage::v3 { -std::vector OrderByElements(Shard::Accessor &acc, DbAccessor &dba, VerticesIterable &vertices_iterable, - std::vector &order_bys) { - std::vector ordered; - ordered.reserve(acc.ApproximateVertexCount()); - std::vector ordering; - ordering.reserve(order_bys.size()); - for (const auto &order : order_bys) { - switch (order.direction) { - case memgraph::msgs::OrderingDirection::ASCENDING: { - ordering.push_back(Ordering::ASC); - break; - } - case memgraph::msgs::OrderingDirection::DESCENDING: { - ordering.push_back(Ordering::DESC); - break; - } - } - } - auto compare_typed_values = TypedValueVectorCompare(ordering); - for (auto it = vertices_iterable.begin(); it != vertices_iterable.end(); ++it) { - std::vector properties_order_by; - properties_order_by.reserve(order_bys.size()); - - for (const auto &order_by : order_bys) { - const auto val = - ComputeExpression(dba, *it, std::nullopt, order_by.expression.expression, expr::identifier_node_symbol, ""); - properties_order_by.push_back(val); - } - ordered.push_back({std::move(properties_order_by), *it}); - } - - std::sort(ordered.begin(), ordered.end(), [compare_typed_values](const auto &pair1, const auto &pair2) { - return compare_typed_values(pair1.properties_order_by, pair2.properties_order_by); - }); - return ordered; -} - VerticesIterable::Iterator GetStartVertexIterator(VerticesIterable &vertex_iterable, const std::vector &start_ids, const View view) { auto it = vertex_iterable.begin(); @@ -68,15 +31,47 @@ VerticesIterable::Iterator GetStartVertexIterator(VerticesIterable &vertex_itera return it; } -std::vector::const_iterator GetStartOrderedElementsIterator(const std::vector &ordered_elements, - const std::vector &start_ids, - const View view) { +std::vector>::const_iterator GetStartOrderedElementsIterator( + const std::vector> &ordered_elements, const std::vector &start_ids, + const View view) { for (auto it = ordered_elements.begin(); it != ordered_elements.end(); ++it) { - if (const auto &vertex = it->vertex_acc; start_ids <= vertex.PrimaryKey(view).GetValue()) { + if (const auto &vertex = it->object_acc; start_ids <= vertex.PrimaryKey(view).GetValue()) { return it; } } return ordered_elements.end(); } +std::vector GetEdgesFromVertex(const VertexAccessor &vertex_accessor, + const msgs::EdgeDirection direction) { + switch (direction) { + case memgraph::msgs::EdgeDirection::IN: { + auto edges = vertex_accessor.InEdges(View::OLD); + if (edges.HasValue()) { + return edges.GetValue(); + } + return {}; + } + case memgraph::msgs::EdgeDirection::OUT: { + auto edges = vertex_accessor.OutEdges(View::OLD); + if (edges.HasValue()) { + return edges.GetValue(); + } + return {}; + } + case memgraph::msgs::EdgeDirection::BOTH: { + auto maybe_in_edges = vertex_accessor.InEdges(View::OLD); + auto maybe_out_edges = vertex_accessor.OutEdges(View::OLD); + std::vector edges; + if (maybe_in_edges.HasValue()) { + edges = maybe_in_edges.GetValue(); + } + if (maybe_out_edges.HasValue()) { + edges.insert(edges.end(), maybe_out_edges.GetValue().begin(), maybe_out_edges.GetValue().end()); + } + return edges; + } + } +} + } // namespace memgraph::storage::v3 diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 24ed40f8c..6212c4a69 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -9,14 +9,21 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. +#pragma once + #include #include "ast/ast.hpp" +#include "pretty_print_ast_to_original_expression.hpp" // #NoCommit why like this? +#include "query/v2/requests.hpp" #include "storage/v3/bindings/typed_value.hpp" +#include "storage/v3/edge_accessor.hpp" +#include "storage/v3/expr.hpp" #include "storage/v3/shard.hpp" #include "storage/v3/vertex_accessor.hpp" - namespace memgraph::storage::v3 { +template +concept ObjectAccessor = std::is_same_v || std::is_same_v; inline bool TypedValueCompare(const TypedValue &a, const TypedValue &b) { // in ordering null comes after everything else @@ -99,18 +106,62 @@ class TypedValueVectorCompare final { std::vector ordering_; }; +template struct Element { std::vector properties_order_by; - VertexAccessor vertex_acc; + TObjectAccessor object_acc; }; -std::vector OrderByElements(Shard::Accessor &acc, DbAccessor &dba, VerticesIterable &vertices_iterable, - std::vector &order_bys); +// #NoCommit in cpp +template +std::vector> OrderByElements(Shard::Accessor &acc, DbAccessor &dba, TIterable &iterable, + std::vector &order_bys) { + std::vector> ordered; + ordered.reserve(acc.ApproximateVertexCount()); + std::vector ordering; + ordering.reserve(order_bys.size()); + for (const auto &order : order_bys) { + switch (order.direction) { + case memgraph::msgs::OrderingDirection::ASCENDING: { + ordering.push_back(Ordering::ASC); + break; + } + case memgraph::msgs::OrderingDirection::DESCENDING: { + ordering.push_back(Ordering::DESC); + break; + } + } + } + auto compare_typed_values = TypedValueVectorCompare(ordering); + auto it = iterable.begin(); + for (; it != iterable.end(); ++it) { + std::vector properties_order_by; + properties_order_by.reserve(order_bys.size()); + + for (const auto &order_by : order_bys) { + if constexpr (std::is_same_v) { + properties_order_by.push_back(ComputeExpression(dba, *it, std::nullopt, order_by.expression.expression, + expr::identifier_node_symbol, expr::identifier_edge_symbol)); + } else { + properties_order_by.push_back(ComputeExpression(dba, std::nullopt, *it, order_by.expression.expression, + expr::identifier_node_symbol, expr::identifier_edge_symbol)); + } + } + ordered.push_back({std::move(properties_order_by), *it}); + } + + std::sort(ordered.begin(), ordered.end(), [compare_typed_values](const auto &pair1, const auto &pair2) { + return compare_typed_values(pair1.properties_order_by, pair2.properties_order_by); + }); + return ordered; +} VerticesIterable::Iterator GetStartVertexIterator(VerticesIterable &vertex_iterable, const std::vector &start_ids, View view); -std::vector::const_iterator GetStartOrderedElementsIterator(const std::vector &ordered_elements, - const std::vector &start_ids, - View view); +std::vector>::const_iterator GetStartOrderedElementsIterator( + const std::vector> &ordered_elements, const std::vector &start_ids, + View view); + +std::vector GetEdgesFromVertex(const VertexAccessor &vertex_accessor, msgs::EdgeDirection direction); } // namespace memgraph::storage::v3 diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 679f95172..97b275530 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -15,6 +15,7 @@ #include #include +#include #include "parser/opencypher/parser.hpp" #include "query/v2/requests.hpp" #include "storage/v3/bindings/ast/ast.hpp" @@ -26,6 +27,7 @@ #include "storage/v3/bindings/symbol_generator.hpp" #include "storage/v3/bindings/symbol_table.hpp" #include "storage/v3/bindings/typed_value.hpp" +#include "storage/v3/edge_accessor.hpp" #include "storage/v3/expr.hpp" #include "storage/v3/id_types.hpp" #include "storage/v3/key_store.hpp" @@ -802,18 +804,18 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { uint64_t sample_counter{0}; auto vertex_iterable = acc.Vertices(view); if (!req.order_bys.empty()) { - const auto ordered = OrderByElements(acc, dba, vertex_iterable, req.order_bys); + const auto ordered = OrderByElements(acc, dba, vertex_iterable, req.order_bys); // we are traversing Elements auto it = GetStartOrderedElementsIterator(ordered, start_id, View(req.storage_view)); for (; it != ordered.end(); ++it) { - emplace_scan_result(it->vertex_acc); + emplace_scan_result(it->object_acc); ++sample_counter; if (req.batch_limit && sample_counter == req.batch_limit) { // Reached the maximum specified batch size. // Get the next element before exiting. ++it; if (it != ordered.end()) { - const auto &next_vertex = it->vertex_acc; + const auto &next_vertex = it->object_acc; next_start_id = ConstructValueVertex(next_vertex, view).vertex_v.id; } @@ -854,36 +856,97 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { bool action_successful = true; std::vector results; + auto batch_limit = req.limit; + auto dba = DbAccessor{&acc}; auto maybe_filter_based_on_edge_uniquness = InitializeEdgeUniqunessFunction(req.only_unique_neighbor_rows); auto edge_filler = InitializeEdgeFillerFunction(req); - for (auto &src_vertex : req.src_vertices) { - // Get Vertex acc - auto src_vertex_acc_opt = acc.FindVertex(ConvertPropertyVector((src_vertex.second)), View::NEW); - if (!src_vertex_acc_opt) { - action_successful = false; - spdlog::debug("Encountered an error while trying to obtain VertexAccessor. Transaction id: {}", - req.transaction_id.logical_id); - break; - } + //#NoCommit code below is duplicated, one needs to factor it once it's clear + if (req.order_by) { + auto vertex_iterable = acc.Vertices(View::OLD); + const auto ordered_vertices = OrderByElements(acc, dba, vertex_iterable, *req.order_by); + std::vector>>> vertex_ordered_edges; + vertex_ordered_edges.reserve(req.src_vertices.size()); - if (!req.filters.empty()) { - // NOTE - DbAccessor might get removed in the future. - auto dba = DbAccessor{&acc}; - const bool eval = FilterOnVertex(dba, src_vertex_acc_opt.value(), req.filters, expr::identifier_node_symbol); - if (!eval) { - continue; + for (auto &src_vertex : req.src_vertices) { + // Get Vertex acc + auto src_vertex_acc_opt = acc.FindVertex(ConvertPropertyVector((src_vertex.second)), View::NEW); + if (!src_vertex_acc_opt) { + action_successful = false; + spdlog::debug("Encountered an error while trying to obtain VertexAccessor. Transaction id: {}", + req.transaction_id.logical_id); + break; + } + + if (!req.filters.empty()) { + // NOTE - DbAccessor might get removed in the future. + auto dba = DbAccessor{&acc}; + const bool eval = FilterOnVertex(dba, src_vertex_acc_opt.value(), req.filters, expr::identifier_node_symbol); + if (!eval) { + continue; + } + } + auto result = GetExpandOneResult(acc, src_vertex, req, maybe_filter_based_on_edge_uniquness, edge_filler); + + if (!result) { + action_successful = false; + break; + } + + results.emplace_back(result.value()); + if (batch_limit) { // #NoCommit can ebd one differently + --*batch_limit; + if (batch_limit < 0) { + break; + } } } - auto result = GetExpandOneResult(acc, src_vertex, req, maybe_filter_based_on_edge_uniquness, edge_filler); + // Now we have to construct response like this, and here we will care about + // limit: + // v1 e1 + // v1 e2 + // v1 e3 + // v2 e4 + // v2 e5 + // v2 e6 + // v2 e7 + // v3 e8 + // v3 e9 + } else { + for (auto &src_vertex : req.src_vertices) { + // Get Vertex acc + auto src_vertex_acc_opt = acc.FindVertex(ConvertPropertyVector((src_vertex.second)), View::NEW); + if (!src_vertex_acc_opt) { + action_successful = false; + spdlog::debug("Encountered an error while trying to obtain VertexAccessor. Transaction id: {}", + req.transaction_id.logical_id); + break; + } - if (!result) { - action_successful = false; - break; + if (!req.filters.empty()) { + // NOTE - DbAccessor might get removed in the future. + auto dba = DbAccessor{&acc}; + const bool eval = FilterOnVertex(dba, src_vertex_acc_opt.value(), req.filters, expr::identifier_node_symbol); + if (!eval) { + continue; + } + } + auto result = GetExpandOneResult(acc, src_vertex, req, maybe_filter_based_on_edge_uniquness, edge_filler); + + if (!result) { + action_successful = false; + break; + } + + results.emplace_back(result.value()); + if (batch_limit) { // #NoCommit can ebd one differently + --*batch_limit; + if (batch_limit < 0) { + break; + } + } } - - results.emplace_back(result.value()); } msgs::ExpandOneResponse resp{}; diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index d5f8f2775..6c3ea7d60 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -680,6 +680,61 @@ void AttemptToExpandOneWithUniqueEdges(ShardClient &client, uint64_t src_vertex_ } } +void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_val, EdgeTypeId edge_type_id) { + // Source vertex + msgs::Label label = {.id = get_primary_label()}; + auto src_vertex = std::make_pair(label, GetPrimaryKey(src_vertex_val)); + + // Edge type + auto edge_type = msgs::EdgeType{}; + edge_type.id = edge_type_id; + + // Edge direction + auto edge_direction = msgs::EdgeDirection::OUT; + + // Source Vertex properties to look for + std::optional> src_vertex_properties = {}; + + // Edge properties to look for + std::optional> edge_properties = {}; + + std::vector expressions; + std::optional> order_by = {}; + // std::optional limit = 5; + // std::optional filter = {}; + + msgs::ExpandOneRequest expand_one_req{}; + + expand_one_req.direction = edge_direction; + expand_one_req.edge_properties = edge_properties; + expand_one_req.edge_types = {edge_type}; + // expand_one_req.expressions = expressions; #NoCommit not existing? + // expand_one_req.filter = filter; + // expand_one_req.limit = limit; + expand_one_req.order_by = order_by; + expand_one_req.src_vertex_properties = src_vertex_properties; + expand_one_req.src_vertices = {src_vertex}; + expand_one_req.transaction_id.logical_id = GetTransactionId(); + + while (true) { + auto read_res = client.SendReadRequest(expand_one_req); + if (read_res.HasError()) { + continue; + } + + auto write_response_result = read_res.GetValue(); + auto write_response = std::get(write_response_result); + MG_ASSERT(write_response.result.size() == 1); + // MG_ASSERT(write_response.result[0].edges_with_all_properties->size() == 10); // #NoCommit where does that come + // from? + // auto number_of_properties_on_edge = + // (std::get>(write_response.result[0].edges_with_all_properties.value()[0])) + // .size(); + // MG_ASSERT(number_of_properties_on_edge == 1); + break; + } +} + void AttemptToExpandOneWithSpecifiedSrcVertexProperties(ShardClient &client, uint64_t src_vertex_val, EdgeTypeId edge_type_id) { // Source vertex @@ -1021,6 +1076,9 @@ void TestExpandOneGraphOne(ShardClient &client) { auto edge_prop_id = GetUniqueInteger(); auto edge_prop_val = GetUniqueInteger(); + std::vector edges_ids(10); + std::generate(edges_ids.begin(), edges_ids.end(), GetUniqueInteger); + // (V1)-[edge_type_id]->(V2) MG_ASSERT(AttemptToAddEdgeWithProperties(client, unique_prop_val_1, unique_prop_val_2, edge_gid_1, edge_prop_id, edge_prop_val, {edge_type_id})); @@ -1028,6 +1086,13 @@ void TestExpandOneGraphOne(ShardClient &client) { MG_ASSERT(AttemptToAddEdgeWithProperties(client, unique_prop_val_1, unique_prop_val_3, edge_gid_2, edge_prop_id, edge_prop_val, {edge_type_id})); + // (V2)-[edge_type_id]->(V3) x 10 + std::for_each(edges_ids.begin(), edges_ids.end(), [&](const auto &edge_id) { + MG_ASSERT(AttemptToAddEdgeWithProperties(client, unique_prop_val_2, unique_prop_val_3, edge_id, edge_prop_id, + edge_prop_val, {edge_type_id})); + }); + AttemptToExpandOneLimitAndOrderBy(client, unique_prop_val_2, edge_type_id); // #NoCommit + AttemptToExpandOneSimple(client, unique_prop_val_1, edge_type_id); AttemptToExpandOneWithWrongEdgeType(client, unique_prop_val_1, wrong_edge_type_id); AttemptToExpandOneWithSpecifiedSrcVertexProperties(client, unique_prop_val_1, edge_type_id); From b82e8748ad9bf2a0a96a59cf9b4f00eaf9f86ba1 Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 24 Oct 2022 12:03:51 +0200 Subject: [PATCH 002/103] Attempt impl --- src/query/v2/requests.hpp | 2 +- src/storage/v3/request_helper.cpp | 20 +++--- src/storage/v3/request_helper.hpp | 3 +- src/storage/v3/shard_rsm.cpp | 105 +++++++++++++++++++++--------- tests/simulation/shard_rsm.cpp | 32 ++++----- 5 files changed, 103 insertions(+), 59 deletions(-) diff --git a/src/query/v2/requests.hpp b/src/query/v2/requests.hpp index 49ae346cb..d8d637037 100644 --- a/src/query/v2/requests.hpp +++ b/src/query/v2/requests.hpp @@ -404,7 +404,7 @@ struct ExpandOneRequest { std::vector vertex_expressions; std::vector edge_expressions; - std::optional> order_by; + std::vector order_by; // Limit the edges or the vertices? std::optional limit; std::vector filters; diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index 7dab4c5c1..60633fbbd 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -42,36 +42,38 @@ std::vector>::const_iterator GetStartOrderedElementsIter return ordered_elements.end(); } -std::vector GetEdgesFromVertex(const VertexAccessor &vertex_accessor, - const msgs::EdgeDirection direction) { +std::array, 2> GetEdgesFromVertex(const VertexAccessor &vertex_accessor, + const msgs::EdgeDirection direction) { + std::vector in_edges; + std::vector out_edges; + switch (direction) { case memgraph::msgs::EdgeDirection::IN: { auto edges = vertex_accessor.InEdges(View::OLD); if (edges.HasValue()) { - return edges.GetValue(); + in_edges = edges.GetValue(); } - return {}; } case memgraph::msgs::EdgeDirection::OUT: { auto edges = vertex_accessor.OutEdges(View::OLD); if (edges.HasValue()) { - return edges.GetValue(); + out_edges = edges.GetValue(); } - return {}; } case memgraph::msgs::EdgeDirection::BOTH: { auto maybe_in_edges = vertex_accessor.InEdges(View::OLD); auto maybe_out_edges = vertex_accessor.OutEdges(View::OLD); std::vector edges; if (maybe_in_edges.HasValue()) { - edges = maybe_in_edges.GetValue(); + in_edges = maybe_in_edges.GetValue(); } if (maybe_out_edges.HasValue()) { - edges.insert(edges.end(), maybe_out_edges.GetValue().begin(), maybe_out_edges.GetValue().end()); + out_edges = maybe_out_edges.GetValue(); } - return edges; } } + + return std::array, 2>{in_edges, out_edges}; } } // namespace memgraph::storage::v3 diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 6212c4a69..63758cf63 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -163,5 +163,6 @@ std::vector>::const_iterator GetStartOrderedElementsIter const std::vector> &ordered_elements, const std::vector &start_ids, View view); -std::vector GetEdgesFromVertex(const VertexAccessor &vertex_accessor, msgs::EdgeDirection direction); +std::array, 2> GetEdgesFromVertex(const VertexAccessor &vertex_accessor, + msgs::EdgeDirection direction); } // namespace memgraph::storage::v3 diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 97b275530..5f30692ec 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -354,6 +354,41 @@ std::optional GetExpandOneResult( return result_row; } +std::optional GetExpandOneResult2( + VertexAccessor v_acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, + std::vector in_edge_accessors, std::vector out_edge_accessors, + const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler) { + /// Fill up source vertex + auto source_vertex = FillUpSourceVertex(v_acc, req, src_vertex); + if (!source_vertex) { + return std::nullopt; + } + + /// Fill up source vertex properties + auto src_vertex_properties = FillUpSourceVertexProperties(v_acc, req); + if (!src_vertex_properties) { + return std::nullopt; + } + + /// Fill up connecting edges + auto in_edges = maybe_filter_based_on_edge_uniquness(std::move(in_edge_accessors), msgs::EdgeDirection::IN); + auto out_edges = maybe_filter_based_on_edge_uniquness(std::move(out_edge_accessors), msgs::EdgeDirection::OUT); + + msgs::ExpandOneResultRow result_row; + result_row.src_vertex = std::move(*source_vertex); + result_row.src_vertex_properties = std::move(*src_vertex_properties); + static constexpr bool kInEdges = true; + static constexpr bool kOutEdges = false; + if (!in_edges.empty() && !FillEdges(in_edges, req, result_row, edge_filler)) { + return std::nullopt; + } + if (!out_edges.empty() && !FillEdges(out_edges, req, result_row, edge_filler)) { + return std::nullopt; + } + + return result_row; +} + EdgeUniqunessFunction InitializeEdgeUniqunessFunction(bool only_unique_neighbor_rows) { // Functions to select connecting edges based on uniquness EdgeUniqunessFunction maybe_filter_based_on_edge_uniquness; @@ -862,57 +897,67 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { auto maybe_filter_based_on_edge_uniquness = InitializeEdgeUniqunessFunction(req.only_unique_neighbor_rows); auto edge_filler = InitializeEdgeFillerFunction(req); - //#NoCommit code below is duplicated, one needs to factor it once it's clear - if (req.order_by) { + //#NoCommit code below is duplicated, one needs to factor it once it's clear! + if (!req.order_by.empty()) { auto vertex_iterable = acc.Vertices(View::OLD); - const auto ordered_vertices = OrderByElements(acc, dba, vertex_iterable, *req.order_by); - std::vector>>> vertex_ordered_edges; - vertex_ordered_edges.reserve(req.src_vertices.size()); + const auto ordered_vertices = OrderByElements(acc, dba, vertex_iterable, req.order_by); - for (auto &src_vertex : req.src_vertices) { + for (auto &ordered_vertice : ordered_vertices) { // Get Vertex acc - auto src_vertex_acc_opt = acc.FindVertex(ConvertPropertyVector((src_vertex.second)), View::NEW); - if (!src_vertex_acc_opt) { - action_successful = false; - spdlog::debug("Encountered an error while trying to obtain VertexAccessor. Transaction id: {}", - req.transaction_id.logical_id); - break; - } + auto src_vertex_acc = ordered_vertice.object_acc; if (!req.filters.empty()) { // NOTE - DbAccessor might get removed in the future. - auto dba = DbAccessor{&acc}; - const bool eval = FilterOnVertex(dba, src_vertex_acc_opt.value(), req.filters, expr::identifier_node_symbol); + const bool eval = FilterOnVertex(dba, src_vertex_acc, req.filters, expr::identifier_node_symbol); if (!eval) { continue; } } - auto result = GetExpandOneResult(acc, src_vertex, req, maybe_filter_based_on_edge_uniquness, edge_filler); - if (!result) { + auto [in_edge_accessors, out_edge_accessors] = GetEdgesFromVertex(src_vertex_acc, req.direction); + const auto in_ordered_edges = OrderByElements(acc, dba, in_edge_accessors, req.order_by); + const auto out_ordered_edges = OrderByElements(acc, dba, out_edge_accessors, req.order_by); + + std::vector in_edge_ordered_accessors; + std::transform(in_ordered_edges.begin(), in_ordered_edges.end(), std::back_inserter(in_edge_ordered_accessors), + [](auto &edge_element) { return edge_element.object_acc; }); + + std::vector out_edge_ordered_accessors; + std::transform(out_ordered_edges.begin(), out_ordered_edges.end(), std::back_inserter(out_edge_ordered_accessors), + [](auto &edge_element) { return edge_element.object_acc; }); + + auto label_id = src_vertex_acc.PrimaryLabel(View::NEW); + if (label_id.HasError()) { + action_successful = false; + break; + } + auto primary_key = src_vertex_acc.PrimaryKey(View::NEW); + if (primary_key.HasError()) { action_successful = false; break; } - results.emplace_back(result.value()); - if (batch_limit) { // #NoCommit can ebd one differently + msgs::VertexId src_vertice; + src_vertice.first = msgs::Label{.id = *label_id}; + src_vertice.second = conversions::ConvertValueVector(*primary_key); + auto maybe_result = + GetExpandOneResult2(src_vertex_acc, src_vertice, req, in_edge_ordered_accessors, out_edge_ordered_accessors, + maybe_filter_based_on_edge_uniquness, edge_filler); + + if (!maybe_result) { + action_successful = false; + break; + } + + results.emplace_back(maybe_result.value()); + + if (batch_limit) { // #NoCommit can be done differently --*batch_limit; if (batch_limit < 0) { break; } } } - // Now we have to construct response like this, and here we will care about - // limit: - // v1 e1 - // v1 e2 - // v1 e3 - // v2 e4 - // v2 e5 - // v2 e6 - // v2 e7 - // v3 e8 - // v3 e9 } else { for (auto &src_vertex : req.src_vertices) { // Get Vertex acc diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index 6c3ea7d60..8883e4952 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -531,7 +531,7 @@ void AttemptToExpandOneWithWrongEdgeType(ShardClient &client, uint64_t src_verte std::optional> edge_properties = {}; std::vector expressions; - std::optional> order_by = {}; + std::vector order_by = {}; std::optional limit = {}; std::vector filter = {}; @@ -586,7 +586,7 @@ void AttemptToExpandOneSimple(ShardClient &client, uint64_t src_vertex_val, Edge std::optional> edge_properties = {}; std::vector expressions; - std::optional> order_by = {}; + std::vector order_by = {}; std::optional limit = {}; std::vector filter = {}; @@ -642,7 +642,7 @@ void AttemptToExpandOneWithUniqueEdges(ShardClient &client, uint64_t src_vertex_ std::optional> edge_properties = {}; std::vector expressions; - std::optional> order_by = {}; + std::vector order_by = {}; std::optional limit = {}; std::vector filter = {}; @@ -698,9 +698,9 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ // Edge properties to look for std::optional> edge_properties = {}; - std::vector expressions; - std::optional> order_by = {}; - // std::optional limit = 5; + std::vector order_by = { + {msgs::Expression{"MG_SYMBOL_NODE.prop1"}, msgs::OrderingDirection::DESCENDING}}; + std::optional limit = 3; // std::optional filter = {}; msgs::ExpandOneRequest expand_one_req{}; @@ -708,9 +708,8 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ expand_one_req.direction = edge_direction; expand_one_req.edge_properties = edge_properties; expand_one_req.edge_types = {edge_type}; - // expand_one_req.expressions = expressions; #NoCommit not existing? - // expand_one_req.filter = filter; - // expand_one_req.limit = limit; + // expand_one_req.filter = filter; #NoCommit later? + expand_one_req.limit = limit; expand_one_req.order_by = order_by; expand_one_req.src_vertex_properties = src_vertex_properties; expand_one_req.src_vertices = {src_vertex}; @@ -725,12 +724,9 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ auto write_response_result = read_res.GetValue(); auto write_response = std::get(write_response_result); MG_ASSERT(write_response.result.size() == 1); - // MG_ASSERT(write_response.result[0].edges_with_all_properties->size() == 10); // #NoCommit where does that come - // from? - // auto number_of_properties_on_edge = - // (std::get>(write_response.result[0].edges_with_all_properties.value()[0])) - // .size(); - // MG_ASSERT(number_of_properties_on_edge == 1); + MG_ASSERT(write_response.result[0].out_edges_with_all_properties.size() == 10); + auto number_of_properties_on_edge = write_response.result[0].out_edges_with_all_properties.size(); + MG_ASSERT(number_of_properties_on_edge == 1); break; } } @@ -756,7 +752,7 @@ void AttemptToExpandOneWithSpecifiedSrcVertexProperties(ShardClient &client, uin std::optional> edge_properties = {}; std::vector expressions; - std::optional> order_by = {}; + std::vector order_by = {}; std::optional limit = {}; std::vector filter = {}; @@ -816,7 +812,7 @@ void AttemptToExpandOneWithSpecifiedEdgeProperties(ShardClient &client, uint64_t std::optional> edge_properties = {specified_edge_prop}; std::vector expressions; - std::optional> order_by = {}; + std::vector order_by = {}; std::optional limit = {}; std::vector filter = {}; @@ -875,7 +871,7 @@ void AttemptToExpandOneWithFilters(ShardClient &client, uint64_t src_vertex_val, std::optional> edge_properties = {}; std::vector expressions; - std::optional> order_by = {}; + std::vector order_by = {}; std::optional limit = {}; std::vector filter = {}; From 386a0c568646478e3826870ae759bd7cadf08c53 Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 24 Oct 2022 15:44:37 +0200 Subject: [PATCH 003/103] add comment --- tests/simulation/shard_rsm.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index 8883e4952..0fba6d74c 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -701,14 +701,17 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ std::vector order_by = { {msgs::Expression{"MG_SYMBOL_NODE.prop1"}, msgs::OrderingDirection::DESCENDING}}; std::optional limit = 3; - // std::optional filter = {}; + std::vector filters = {}; + + // #NoCommit some filter? + // #NoCommit more orderBy? msgs::ExpandOneRequest expand_one_req{}; expand_one_req.direction = edge_direction; expand_one_req.edge_properties = edge_properties; expand_one_req.edge_types = {edge_type}; - // expand_one_req.filter = filter; #NoCommit later? + expand_one_req.filters = filters; expand_one_req.limit = limit; expand_one_req.order_by = order_by; expand_one_req.src_vertex_properties = src_vertex_properties; From 51e6802aa74f9e59a2f7941c039640ed883e7537 Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 24 Oct 2022 15:44:57 +0200 Subject: [PATCH 004/103] Safeguard in case ComputeExpression is called without opt --- src/storage/v3/expr.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/storage/v3/expr.cpp b/src/storage/v3/expr.cpp index 1d6739433..eaff472bd 100644 --- a/src/storage/v3/expr.cpp +++ b/src/storage/v3/expr.cpp @@ -186,7 +186,7 @@ TypedValue ComputeExpression(DbAccessor &dba, const std::optional(expr))->Accept(symbol_generator); - if (node_identifier.symbol_pos_ != -1) { + if (node_identifier.symbol_pos_ != -1 && v_acc.has_value()) { MG_ASSERT(std::find_if(symbol_table.table().begin(), symbol_table.table().end(), [&node_name](const std::pair &position_symbol_pair) { return position_symbol_pair.second.name() == node_name; @@ -195,7 +195,7 @@ TypedValue ComputeExpression(DbAccessor &dba, const std::optional &position_symbol_pair) { return position_symbol_pair.second.name() == edge_name; From 8b9e7e2c65010e15012ec2d52cd939ec29c4c8fc Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 24 Oct 2022 16:23:42 +0200 Subject: [PATCH 005/103] Correct behavior of batch limit (was size_t) --- src/storage/v3/shard_rsm.cpp | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 5f30692ec..7b64731d9 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -950,12 +950,8 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { } results.emplace_back(maybe_result.value()); - - if (batch_limit) { // #NoCommit can be done differently - --*batch_limit; - if (batch_limit < 0) { - break; - } + if (batch_limit.has_value() && results.size() >= batch_limit.value()) { + break; } } } else { @@ -985,11 +981,8 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { } results.emplace_back(result.value()); - if (batch_limit) { // #NoCommit can ebd one differently - --*batch_limit; - if (batch_limit < 0) { - break; - } + if (batch_limit.has_value() && results.size() >= batch_limit.value()) { + break; } } } From 33c9ccee663dfa0c98dd6f72e8755e7e02d39d29 Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 24 Oct 2022 16:38:28 +0200 Subject: [PATCH 006/103] Adapt test --- tests/simulation/shard_rsm.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index 0fba6d74c..0805ace17 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -701,10 +701,7 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ std::vector order_by = { {msgs::Expression{"MG_SYMBOL_NODE.prop1"}, msgs::OrderingDirection::DESCENDING}}; std::optional limit = 3; - std::vector filters = {}; - - // #NoCommit some filter? - // #NoCommit more orderBy? + std::vector filters = {"MG_SYMBOL_NODE.prop1 != " + std::to_string(src_vertex_val)}; msgs::ExpandOneRequest expand_one_req{}; @@ -726,10 +723,23 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ auto write_response_result = read_res.GetValue(); auto write_response = std::get(write_response_result); - MG_ASSERT(write_response.result.size() == 1); - MG_ASSERT(write_response.result[0].out_edges_with_all_properties.size() == 10); - auto number_of_properties_on_edge = write_response.result[0].out_edges_with_all_properties.size(); - MG_ASSERT(number_of_properties_on_edge == 1); + + // We check that we do not have more results than the limit. Based on the data in the graph, we know that we should + // receive exactly limit responses. + MG_ASSERT(write_response.result.size() == limit); + + // We also check that the vertices are ordered by prop1 DESC + + std::is_sorted(write_response.result.cbegin(), write_response.result.cend(), + [](const auto &vertex, const auto &other_vertex) { + const auto primary_key = vertex.src_vertex.id.second; + const auto other_primary_key = other_vertex.src_vertex.id.second; + + MG_ASSERT(primary_key.size() == 1); + MG_ASSERT(other_primary_key.size() == 1); + return primary_key[0].int_v > other_primary_key[0].int_v; + }); + break; } } @@ -1090,9 +1100,9 @@ void TestExpandOneGraphOne(ShardClient &client) { MG_ASSERT(AttemptToAddEdgeWithProperties(client, unique_prop_val_2, unique_prop_val_3, edge_id, edge_prop_id, edge_prop_val, {edge_type_id})); }); - AttemptToExpandOneLimitAndOrderBy(client, unique_prop_val_2, edge_type_id); // #NoCommit AttemptToExpandOneSimple(client, unique_prop_val_1, edge_type_id); + AttemptToExpandOneLimitAndOrderBy(client, unique_prop_val_2, edge_type_id); AttemptToExpandOneWithWrongEdgeType(client, unique_prop_val_1, wrong_edge_type_id); AttemptToExpandOneWithSpecifiedSrcVertexProperties(client, unique_prop_val_1, edge_type_id); AttemptToExpandOneWithSpecifiedEdgeProperties(client, unique_prop_val_1, edge_type_id, edge_prop_id); From 862af552667bc5814600d3a9d2106fb1ed077b77 Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 24 Oct 2022 16:39:38 +0200 Subject: [PATCH 007/103] Remove #NoCommit --- src/storage/v3/request_helper.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 63758cf63..86cff9bea 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -14,7 +14,7 @@ #include #include "ast/ast.hpp" -#include "pretty_print_ast_to_original_expression.hpp" // #NoCommit why like this? +#include "pretty_print_ast_to_original_expression.hpp" #include "query/v2/requests.hpp" #include "storage/v3/bindings/typed_value.hpp" #include "storage/v3/edge_accessor.hpp" @@ -112,7 +112,6 @@ struct Element { TObjectAccessor object_acc; }; -// #NoCommit in cpp template std::vector> OrderByElements(Shard::Accessor &acc, DbAccessor &dba, TIterable &iterable, std::vector &order_bys) { From e901c1fdb78abfe25becb4ec428af3409e221ea2 Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 25 Oct 2022 10:45:33 +0200 Subject: [PATCH 008/103] Refactor code --- src/storage/v3/shard_rsm.cpp | 231 +++++++++++++++++++---------------- 1 file changed, 126 insertions(+), 105 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 7b64731d9..cae2c93d7 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -354,7 +354,7 @@ std::optional GetExpandOneResult( return result_row; } -std::optional GetExpandOneResult2( +std::optional GetExpandOneResult( VertexAccessor v_acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, std::vector in_edge_accessors, std::vector out_edge_accessors, const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler) { @@ -494,6 +494,129 @@ EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { return edge_filler; } +msgs::ReadResponses HandleReadWithOrderBy(msgs::ExpandOneRequest &&req, Shard::Accessor &&acc) { + bool action_successful = true; + + std::vector results; + auto batch_limit = req.limit; + auto dba = DbAccessor{&acc}; + + auto maybe_filter_based_on_edge_uniquness = InitializeEdgeUniqunessFunction(req.only_unique_neighbor_rows); + auto edge_filler = InitializeEdgeFillerFunction(req); + + auto vertex_iterable = acc.Vertices(View::OLD); + const auto ordered_vertices = OrderByElements(acc, dba, vertex_iterable, req.order_by); + + for (const auto &ordered_vertice : ordered_vertices) { + // Get Vertex acc + auto src_vertex_acc = ordered_vertice.object_acc; + + if (!req.filters.empty()) { + // NOTE - DbAccessor might get removed in the future. + const bool eval = FilterOnVertex(dba, src_vertex_acc, req.filters, expr::identifier_node_symbol); + if (!eval) { + continue; + } + } + + auto [in_edge_accessors, out_edge_accessors] = GetEdgesFromVertex(src_vertex_acc, req.direction); + const auto in_ordered_edges = OrderByElements(acc, dba, in_edge_accessors, req.order_by); + const auto out_ordered_edges = OrderByElements(acc, dba, out_edge_accessors, req.order_by); + + std::vector in_edge_ordered_accessors; + std::transform(in_ordered_edges.begin(), in_ordered_edges.end(), std::back_inserter(in_edge_ordered_accessors), + [](auto &edge_element) { return edge_element.object_acc; }); + + std::vector out_edge_ordered_accessors; + std::transform(out_ordered_edges.begin(), out_ordered_edges.end(), std::back_inserter(out_edge_ordered_accessors), + [](auto &edge_element) { return edge_element.object_acc; }); + + auto label_id = src_vertex_acc.PrimaryLabel(View::NEW); + if (label_id.HasError()) { + action_successful = false; + break; + } + auto primary_key = src_vertex_acc.PrimaryKey(View::NEW); + if (primary_key.HasError()) { + action_successful = false; + break; + } + + msgs::VertexId src_vertice(msgs::Label{.id = *label_id}, conversions::ConvertValueVector(*primary_key)); + auto maybe_result = + GetExpandOneResult(src_vertex_acc, src_vertice, req, in_edge_ordered_accessors, out_edge_ordered_accessors, + maybe_filter_based_on_edge_uniquness, edge_filler); + + if (!maybe_result) { + action_successful = false; + break; + } + + results.emplace_back(maybe_result.value()); + if (batch_limit.has_value() && results.size() >= batch_limit.value()) { + break; + } + } + + msgs::ExpandOneResponse resp{}; + resp.success = action_successful; + if (action_successful) { + resp.result = std::move(results); + } + + return resp; +} + +msgs::ReadResponses HandleReadWithoutOrderBy(msgs::ExpandOneRequest &&req, Shard::Accessor &&acc) { + bool action_successful = true; + + std::vector results; + auto batch_limit = req.limit; + auto dba = DbAccessor{&acc}; + + auto maybe_filter_based_on_edge_uniquness = InitializeEdgeUniqunessFunction(req.only_unique_neighbor_rows); + auto edge_filler = InitializeEdgeFillerFunction(req); + + for (auto &src_vertex : req.src_vertices) { + // Get Vertex acc + auto src_vertex_acc_opt = acc.FindVertex(ConvertPropertyVector((src_vertex.second)), View::NEW); + if (!src_vertex_acc_opt) { + action_successful = false; + spdlog::debug("Encountered an error while trying to obtain VertexAccessor. Transaction id: {}", + req.transaction_id.logical_id); + break; + } + + if (!req.filters.empty()) { + // NOTE - DbAccessor might get removed in the future. + auto dba = DbAccessor{&acc}; + const bool eval = FilterOnVertex(dba, src_vertex_acc_opt.value(), req.filters, expr::identifier_node_symbol); + if (!eval) { + continue; + } + } + auto result = GetExpandOneResult(acc, src_vertex, req, maybe_filter_based_on_edge_uniquness, edge_filler); + + if (!result) { + action_successful = false; + break; + } + + results.emplace_back(result.value()); + if (batch_limit.has_value() && results.size() >= batch_limit.value()) { + break; + } + } + + msgs::ExpandOneResponse resp{}; + resp.success = action_successful; + if (action_successful) { + resp.result = std::move(results); + } + + return resp; +} + }; // namespace msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); @@ -887,113 +1010,11 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { } msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { - auto acc = shard_->Access(req.transaction_id); - bool action_successful = true; - - std::vector results; - auto batch_limit = req.limit; - auto dba = DbAccessor{&acc}; - - auto maybe_filter_based_on_edge_uniquness = InitializeEdgeUniqunessFunction(req.only_unique_neighbor_rows); - auto edge_filler = InitializeEdgeFillerFunction(req); - - //#NoCommit code below is duplicated, one needs to factor it once it's clear! if (!req.order_by.empty()) { - auto vertex_iterable = acc.Vertices(View::OLD); - const auto ordered_vertices = OrderByElements(acc, dba, vertex_iterable, req.order_by); - - for (auto &ordered_vertice : ordered_vertices) { - // Get Vertex acc - auto src_vertex_acc = ordered_vertice.object_acc; - - if (!req.filters.empty()) { - // NOTE - DbAccessor might get removed in the future. - const bool eval = FilterOnVertex(dba, src_vertex_acc, req.filters, expr::identifier_node_symbol); - if (!eval) { - continue; - } - } - - auto [in_edge_accessors, out_edge_accessors] = GetEdgesFromVertex(src_vertex_acc, req.direction); - const auto in_ordered_edges = OrderByElements(acc, dba, in_edge_accessors, req.order_by); - const auto out_ordered_edges = OrderByElements(acc, dba, out_edge_accessors, req.order_by); - - std::vector in_edge_ordered_accessors; - std::transform(in_ordered_edges.begin(), in_ordered_edges.end(), std::back_inserter(in_edge_ordered_accessors), - [](auto &edge_element) { return edge_element.object_acc; }); - - std::vector out_edge_ordered_accessors; - std::transform(out_ordered_edges.begin(), out_ordered_edges.end(), std::back_inserter(out_edge_ordered_accessors), - [](auto &edge_element) { return edge_element.object_acc; }); - - auto label_id = src_vertex_acc.PrimaryLabel(View::NEW); - if (label_id.HasError()) { - action_successful = false; - break; - } - auto primary_key = src_vertex_acc.PrimaryKey(View::NEW); - if (primary_key.HasError()) { - action_successful = false; - break; - } - - msgs::VertexId src_vertice; - src_vertice.first = msgs::Label{.id = *label_id}; - src_vertice.second = conversions::ConvertValueVector(*primary_key); - auto maybe_result = - GetExpandOneResult2(src_vertex_acc, src_vertice, req, in_edge_ordered_accessors, out_edge_ordered_accessors, - maybe_filter_based_on_edge_uniquness, edge_filler); - - if (!maybe_result) { - action_successful = false; - break; - } - - results.emplace_back(maybe_result.value()); - if (batch_limit.has_value() && results.size() >= batch_limit.value()) { - break; - } - } + return HandleReadWithOrderBy(std::move(req), shard_->Access(req.transaction_id)); } else { - for (auto &src_vertex : req.src_vertices) { - // Get Vertex acc - auto src_vertex_acc_opt = acc.FindVertex(ConvertPropertyVector((src_vertex.second)), View::NEW); - if (!src_vertex_acc_opt) { - action_successful = false; - spdlog::debug("Encountered an error while trying to obtain VertexAccessor. Transaction id: {}", - req.transaction_id.logical_id); - break; - } - - if (!req.filters.empty()) { - // NOTE - DbAccessor might get removed in the future. - auto dba = DbAccessor{&acc}; - const bool eval = FilterOnVertex(dba, src_vertex_acc_opt.value(), req.filters, expr::identifier_node_symbol); - if (!eval) { - continue; - } - } - auto result = GetExpandOneResult(acc, src_vertex, req, maybe_filter_based_on_edge_uniquness, edge_filler); - - if (!result) { - action_successful = false; - break; - } - - results.emplace_back(result.value()); - if (batch_limit.has_value() && results.size() >= batch_limit.value()) { - break; - } - } + return HandleReadWithoutOrderBy(std::move(req), shard_->Access(req.transaction_id)); } - - msgs::ExpandOneResponse resp{}; - resp.success = action_successful; - if (action_successful) { - resp.result = std::move(results); - } - - return resp; } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CommitRequest &&req) { From b4f68e7a60d92ccdfbe972d4fd9557b6fbab240e Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 25 Oct 2022 10:56:16 +0200 Subject: [PATCH 009/103] remove includes --- src/storage/v3/shard_rsm.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index cae2c93d7..054900e57 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -15,7 +15,6 @@ #include #include -#include #include "parser/opencypher/parser.hpp" #include "query/v2/requests.hpp" #include "storage/v3/bindings/ast/ast.hpp" From 18423ce34d74f7a32118092192336130667b1cf2 Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 25 Oct 2022 11:01:04 +0200 Subject: [PATCH 010/103] remove includes --- src/storage/v3/shard_rsm.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 054900e57..1350b0e39 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -26,7 +26,6 @@ #include "storage/v3/bindings/symbol_generator.hpp" #include "storage/v3/bindings/symbol_table.hpp" #include "storage/v3/bindings/typed_value.hpp" -#include "storage/v3/edge_accessor.hpp" #include "storage/v3/expr.hpp" #include "storage/v3/id_types.hpp" #include "storage/v3/key_store.hpp" From c1d0fddaac1213deb7a4b1bf0cf8c3eab98815c5 Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 25 Oct 2022 11:31:23 +0200 Subject: [PATCH 011/103] Remove unnecessary else --- src/storage/v3/shard_rsm.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 1350b0e39..b8dadbb7b 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -1010,9 +1010,9 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { if (!req.order_by.empty()) { return HandleReadWithOrderBy(std::move(req), shard_->Access(req.transaction_id)); - } else { + } else + return HandleReadWithoutOrderBy(std::move(req), shard_->Access(req.transaction_id)); - } } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CommitRequest &&req) { From cdab8828e42bb3083fc782ff0092c569b07a88fa Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 25 Oct 2022 12:30:14 +0200 Subject: [PATCH 012/103] remove else --- src/storage/v3/shard_rsm.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index b8dadbb7b..700433541 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -1010,9 +1010,9 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { if (!req.order_by.empty()) { return HandleReadWithOrderBy(std::move(req), shard_->Access(req.transaction_id)); - } else + } - return HandleReadWithoutOrderBy(std::move(req), shard_->Access(req.transaction_id)); + return HandleReadWithoutOrderBy(std::move(req), shard_->Access(req.transaction_id)); } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CommitRequest &&req) { From 79c2ae206fe3e62b89e84ffb3b2e11e3cc5b3b6d Mon Sep 17 00:00:00 2001 From: jeremy Date: Fri, 28 Oct 2022 11:24:41 +0200 Subject: [PATCH 013/103] Update FillEdges usage (for compilation) --- src/storage/v3/shard_rsm.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 6ebd0374e..69429493c 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -367,10 +367,10 @@ std::optional GetExpandOneResult( result_row.src_vertex_properties = std::move(*src_vertex_properties); static constexpr bool kInEdges = true; static constexpr bool kOutEdges = false; - if (!in_edges.empty() && !FillEdges(in_edges, req, result_row, edge_filler)) { + if (!in_edges.empty() && !FillEdges(in_edges, result_row, edge_filler)) { return std::nullopt; } - if (!out_edges.empty() && !FillEdges(out_edges, req, result_row, edge_filler)) { + if (!out_edges.empty() && !FillEdges(out_edges, result_row, edge_filler)) { return std::nullopt; } From d0b8b27c29b09025b71bd81bb00ed8e9548aa636 Mon Sep 17 00:00:00 2001 From: jeremy Date: Fri, 28 Oct 2022 12:52:37 +0200 Subject: [PATCH 014/103] Rename ordered->sorted --- src/storage/v3/shard_rsm.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 69429493c..b4078aaad 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -493,9 +493,9 @@ msgs::ReadResponses HandleReadWithOrderBy(msgs::ExpandOneRequest &&req, Shard::A auto edge_filler = InitializeEdgeFillerFunction(req); auto vertex_iterable = acc.Vertices(View::OLD); - const auto ordered_vertices = OrderByElements(acc, dba, vertex_iterable, req.order_by); + const auto sorted_vertices = OrderByElements(acc, dba, vertex_iterable, req.order_by); - for (const auto &ordered_vertice : ordered_vertices) { + for (const auto &ordered_vertice : sorted_vertices) { // Get Vertex acc auto src_vertex_acc = ordered_vertice.object_acc; From 009c1b40741bb98d645015b74200870eb6c793fe Mon Sep 17 00:00:00 2001 From: jeremy Date: Fri, 28 Oct 2022 12:55:23 +0200 Subject: [PATCH 015/103] Replace include --- src/storage/v3/request_helper.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 86cff9bea..c291e63d4 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -14,8 +14,8 @@ #include #include "ast/ast.hpp" -#include "pretty_print_ast_to_original_expression.hpp" #include "query/v2/requests.hpp" +#include "storage/v3/bindings/pretty_print_ast_to_original_expression.hpp" #include "storage/v3/bindings/typed_value.hpp" #include "storage/v3/edge_accessor.hpp" #include "storage/v3/expr.hpp" From 74181114c2d6b6951816184401e608bb92da2e8e Mon Sep 17 00:00:00 2001 From: jeremy Date: Fri, 28 Oct 2022 12:55:47 +0200 Subject: [PATCH 016/103] Remove un-necessary variable --- src/storage/v3/shard_rsm.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index b4078aaad..105b3b3b4 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -577,7 +577,6 @@ msgs::ReadResponses HandleReadWithoutOrderBy(msgs::ExpandOneRequest &&req, Shard if (!req.filters.empty()) { // NOTE - DbAccessor might get removed in the future. - auto dba = DbAccessor{&acc}; const bool eval = FilterOnVertex(dba, src_vertex_acc_opt.value(), req.filters, expr::identifier_node_symbol); if (!eval) { continue; From 0d5ee49e19006bd82f70cf4a589e1dfa7ddd065e Mon Sep 17 00:00:00 2001 From: jeremy Date: Fri, 28 Oct 2022 12:55:56 +0200 Subject: [PATCH 017/103] Correct test expectation --- tests/simulation/shard_rsm.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index da4df9f34..ef15146af 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -726,7 +726,8 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ // We check that we do not have more results than the limit. Based on the data in the graph, we know that we should // receive exactly limit responses. - MG_ASSERT(write_response.result.size() == limit); + auto expected_number_of_rows = std::min(expand_one_req.src_vertices.size(), limit); + MG_ASSERT(write_response.result.size() == expected_number_of_rows); // We also check that the vertices are ordered by prop1 DESC From 46388ad35c3a7a86bff0f536a720cb762e126cc7 Mon Sep 17 00:00:00 2001 From: jeremy Date: Fri, 28 Oct 2022 13:10:00 +0200 Subject: [PATCH 018/103] Correct compilation --- tests/simulation/shard_rsm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index ef15146af..dac9c26b2 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -700,7 +700,7 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ std::vector order_by = { {msgs::Expression{"MG_SYMBOL_NODE.prop1"}, msgs::OrderingDirection::DESCENDING}}; - std::optional limit = 3; + size_t limit = 3; std::vector filters = {"MG_SYMBOL_NODE.prop1 != " + std::to_string(src_vertex_val)}; msgs::ExpandOneRequest expand_one_req{}; From be7aa55686d49dbe57433e2d1424c5c32e78496c Mon Sep 17 00:00:00 2001 From: jeremy Date: Fri, 28 Oct 2022 13:21:43 +0200 Subject: [PATCH 019/103] Add std::move --- src/storage/v3/shard_rsm.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 105b3b3b4..8727c0440 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -276,7 +276,7 @@ std::optional, 2>> FillUpConnectingEdges( break; } } - return std::array, 2>{in_edges, out_edges}; + return std::array, 2>{std::move(in_edges), std::move(out_edges)}; } using AllEdgePropertyDataSructure = std::map; @@ -492,7 +492,7 @@ msgs::ReadResponses HandleReadWithOrderBy(msgs::ExpandOneRequest &&req, Shard::A auto maybe_filter_based_on_edge_uniquness = InitializeEdgeUniqunessFunction(req.only_unique_neighbor_rows); auto edge_filler = InitializeEdgeFillerFunction(req); - auto vertex_iterable = acc.Vertices(View::OLD); + auto vertex_iterable = acc.Vertices(View::OLD); // #NoCommit we get all vertices in the DB here, is it wanted? const auto sorted_vertices = OrderByElements(acc, dba, vertex_iterable, req.order_by); for (const auto &ordered_vertice : sorted_vertices) { @@ -530,7 +530,8 @@ msgs::ReadResponses HandleReadWithOrderBy(msgs::ExpandOneRequest &&req, Shard::A break; } - msgs::VertexId src_vertice(msgs::Label{.id = *label_id}, conversions::ConvertValueVector(*primary_key)); + msgs::VertexId src_vertice(msgs::Label{.id = *label_id}, + conversions::ConvertValueVector(*primary_key)); // #NoCommit rename auto maybe_result = GetExpandOneResult(src_vertex_acc, src_vertice, req, in_edge_ordered_accessors, out_edge_ordered_accessors, maybe_filter_based_on_edge_uniquness, edge_filler); From 476e2670d5e24063e98035c735c3000bd9f81225 Mon Sep 17 00:00:00 2001 From: Jeremy B <97525434+42jeremy@users.noreply.github.com> Date: Fri, 28 Oct 2022 13:21:48 +0200 Subject: [PATCH 020/103] Update src/storage/v3/request_helper.cpp Co-authored-by: Jure Bajic --- src/storage/v3/request_helper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index 60633fbbd..591d7b6d7 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -73,7 +73,7 @@ std::array, 2> GetEdgesFromVertex(const VertexAccessor } } - return std::array, 2>{in_edges, out_edges}; + return std::array, 2>{std::move(in_edges), std::move(out_edges)}; } } // namespace memgraph::storage::v3 From 1c17692a260001eeeda0eb24048b761054803405 Mon Sep 17 00:00:00 2001 From: Jeremy B <97525434+42jeremy@users.noreply.github.com> Date: Fri, 28 Oct 2022 13:22:07 +0200 Subject: [PATCH 021/103] Update src/storage/v3/shard_rsm.cpp Co-authored-by: Jure Bajic --- src/storage/v3/shard_rsm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 69429493c..86ccbb4c9 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -513,7 +513,7 @@ msgs::ReadResponses HandleReadWithOrderBy(msgs::ExpandOneRequest &&req, Shard::A std::vector in_edge_ordered_accessors; std::transform(in_ordered_edges.begin(), in_ordered_edges.end(), std::back_inserter(in_edge_ordered_accessors), - [](auto &edge_element) { return edge_element.object_acc; }); + [](const auto &edge_element) { return edge_element.object_acc; }); std::vector out_edge_ordered_accessors; std::transform(out_ordered_edges.begin(), out_ordered_edges.end(), std::back_inserter(out_edge_ordered_accessors), From b6814b7a49905b3a9685d16ba0dbe9e9216e97ca Mon Sep 17 00:00:00 2001 From: Jeremy B <97525434+42jeremy@users.noreply.github.com> Date: Fri, 28 Oct 2022 13:22:49 +0200 Subject: [PATCH 022/103] Update src/storage/v3/shard_rsm.cpp Co-authored-by: Jure Bajic --- src/storage/v3/shard_rsm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 86ccbb4c9..f7b655a29 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -517,7 +517,7 @@ msgs::ReadResponses HandleReadWithOrderBy(msgs::ExpandOneRequest &&req, Shard::A std::vector out_edge_ordered_accessors; std::transform(out_ordered_edges.begin(), out_ordered_edges.end(), std::back_inserter(out_edge_ordered_accessors), - [](auto &edge_element) { return edge_element.object_acc; }); + [](const auto &edge_element) { return edge_element.object_acc; }); auto label_id = src_vertex_acc.PrimaryLabel(View::NEW); if (label_id.HasError()) { From e0f6c951c1b3050dabbc6979a8e1a2bc73adb53e Mon Sep 17 00:00:00 2001 From: jeremy Date: Fri, 28 Oct 2022 15:31:15 +0200 Subject: [PATCH 023/103] Add possibilty to orderByElement on vector --- src/storage/v3/request_helper.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index c291e63d4..e3c5b9d3f 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -138,7 +138,8 @@ std::vector> OrderByElements(Shard::Accessor &acc, DbAc properties_order_by.reserve(order_bys.size()); for (const auto &order_by : order_bys) { - if constexpr (std::is_same_v) { + if constexpr (std::is_same_v || + std::is_same_v>) { properties_order_by.push_back(ComputeExpression(dba, *it, std::nullopt, order_by.expression.expression, expr::identifier_node_symbol, expr::identifier_edge_symbol)); } else { From b2e9717ec3967adcfd49c37c7de4de40f321b04b Mon Sep 17 00:00:00 2001 From: jeremy Date: Fri, 28 Oct 2022 15:31:29 +0200 Subject: [PATCH 024/103] Factor HandleRead(msgs::ExpandOneRequest.. --- src/storage/v3/shard_rsm.cpp | 220 +++++++++++++++-------------------- 1 file changed, 94 insertions(+), 126 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 4c520858a..20808517d 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -482,129 +482,6 @@ EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { return edge_filler; } -msgs::ReadResponses HandleReadWithOrderBy(msgs::ExpandOneRequest &&req, Shard::Accessor &&acc) { - bool action_successful = true; - - std::vector results; - auto batch_limit = req.limit; - auto dba = DbAccessor{&acc}; - - auto maybe_filter_based_on_edge_uniquness = InitializeEdgeUniqunessFunction(req.only_unique_neighbor_rows); - auto edge_filler = InitializeEdgeFillerFunction(req); - - auto vertex_iterable = acc.Vertices(View::OLD); // #NoCommit we get all vertices in the DB here, is it wanted? - const auto sorted_vertices = OrderByElements(acc, dba, vertex_iterable, req.order_by); - - for (const auto &ordered_vertice : sorted_vertices) { - // Get Vertex acc - auto src_vertex_acc = ordered_vertice.object_acc; - - if (!req.filters.empty()) { - // NOTE - DbAccessor might get removed in the future. - const bool eval = FilterOnVertex(dba, src_vertex_acc, req.filters, expr::identifier_node_symbol); - if (!eval) { - continue; - } - } - - auto [in_edge_accessors, out_edge_accessors] = GetEdgesFromVertex(src_vertex_acc, req.direction); - const auto in_ordered_edges = OrderByElements(acc, dba, in_edge_accessors, req.order_by); - const auto out_ordered_edges = OrderByElements(acc, dba, out_edge_accessors, req.order_by); - - std::vector in_edge_ordered_accessors; - std::transform(in_ordered_edges.begin(), in_ordered_edges.end(), std::back_inserter(in_edge_ordered_accessors), - [](const auto &edge_element) { return edge_element.object_acc; }); - - std::vector out_edge_ordered_accessors; - std::transform(out_ordered_edges.begin(), out_ordered_edges.end(), std::back_inserter(out_edge_ordered_accessors), - [](const auto &edge_element) { return edge_element.object_acc; }); - - auto label_id = src_vertex_acc.PrimaryLabel(View::NEW); - if (label_id.HasError()) { - action_successful = false; - break; - } - auto primary_key = src_vertex_acc.PrimaryKey(View::NEW); - if (primary_key.HasError()) { - action_successful = false; - break; - } - - msgs::VertexId src_vertice(msgs::Label{.id = *label_id}, - conversions::ConvertValueVector(*primary_key)); // #NoCommit rename - auto maybe_result = - GetExpandOneResult(src_vertex_acc, src_vertice, req, in_edge_ordered_accessors, out_edge_ordered_accessors, - maybe_filter_based_on_edge_uniquness, edge_filler); - - if (!maybe_result) { - action_successful = false; - break; - } - - results.emplace_back(maybe_result.value()); - if (batch_limit.has_value() && results.size() >= batch_limit.value()) { - break; - } - } - - msgs::ExpandOneResponse resp{}; - resp.success = action_successful; - if (action_successful) { - resp.result = std::move(results); - } - - return resp; -} - -msgs::ReadResponses HandleReadWithoutOrderBy(msgs::ExpandOneRequest &&req, Shard::Accessor &&acc) { - bool action_successful = true; - - std::vector results; - auto batch_limit = req.limit; - auto dba = DbAccessor{&acc}; - - auto maybe_filter_based_on_edge_uniquness = InitializeEdgeUniqunessFunction(req.only_unique_neighbor_rows); - auto edge_filler = InitializeEdgeFillerFunction(req); - - for (auto &src_vertex : req.src_vertices) { - // Get Vertex acc - auto src_vertex_acc_opt = acc.FindVertex(ConvertPropertyVector((src_vertex.second)), View::NEW); - if (!src_vertex_acc_opt) { - action_successful = false; - spdlog::debug("Encountered an error while trying to obtain VertexAccessor. Transaction id: {}", - req.transaction_id.logical_id); - break; - } - - if (!req.filters.empty()) { - // NOTE - DbAccessor might get removed in the future. - const bool eval = FilterOnVertex(dba, src_vertex_acc_opt.value(), req.filters, expr::identifier_node_symbol); - if (!eval) { - continue; - } - } - auto result = GetExpandOneResult(acc, src_vertex, req, maybe_filter_based_on_edge_uniquness, edge_filler); - - if (!result) { - action_successful = false; - break; - } - - results.emplace_back(result.value()); - if (batch_limit.has_value() && results.size() >= batch_limit.value()) { - break; - } - } - - msgs::ExpandOneResponse resp{}; - resp.success = action_successful; - if (action_successful) { - resp.result = std::move(results); - } - - return resp; -} - }; // namespace msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); @@ -984,11 +861,102 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { } msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { - if (!req.order_by.empty()) { - return HandleReadWithOrderBy(std::move(req), shard_->Access(req.transaction_id)); + auto acc = shard_->Access(req.transaction_id); + bool action_successful = true; + + std::vector results; + auto batch_limit = req.limit; + auto dba = DbAccessor{&acc}; + + auto maybe_filter_based_on_edge_uniquness = InitializeEdgeUniqunessFunction(req.only_unique_neighbor_rows); + auto edge_filler = InitializeEdgeFillerFunction(req); + + std::vector vertex_accessors; + vertex_accessors.reserve(req.src_vertices.size()); + for (auto &src_vertex : req.src_vertices) { + // Get Vertex acc + auto src_vertex_acc_opt = acc.FindVertex(ConvertPropertyVector((src_vertex.second)), View::NEW); + if (!src_vertex_acc_opt) { + action_successful = false; + spdlog::debug("Encountered an error while trying to obtain VertexAccessor. Transaction id: {}", + req.transaction_id.logical_id); + break; + } + if (!req.filters.empty()) { + // NOTE - DbAccessor might get removed in the future. + const bool eval = FilterOnVertex(dba, src_vertex_acc_opt.value(), req.filters, expr::identifier_node_symbol); + if (!eval) { + continue; + } + } + + vertex_accessors.emplace_back(src_vertex_acc_opt.value()); } - return HandleReadWithoutOrderBy(std::move(req), shard_->Access(req.transaction_id)); + if (!req.order_by.empty()) { + // #NoCommit can we do differently to avoid this? We need OrderByElements but currently + // #NoCommit it returns vector, so this workaround is here to avoid more duplication later + auto sorted_vertices = OrderByElements(acc, dba, vertex_accessors, req.order_by); + vertex_accessors.clear(); + std::transform(sorted_vertices.begin(), sorted_vertices.end(), std::back_inserter(vertex_accessors), + [](auto &vertex) { return vertex.object_acc; }); + } + + for (const auto &src_vertex_acc : vertex_accessors) { + auto label_id = src_vertex_acc.PrimaryLabel(View::NEW); + if (label_id.HasError()) { + action_successful = false; + break; + } + + auto primary_key = src_vertex_acc.PrimaryKey(View::NEW); + if (primary_key.HasError()) { + action_successful = false; + break; + } + + msgs::VertexId src_vertice(msgs::Label{.id = *label_id}, + conversions::ConvertValueVector(*primary_key)); // #NoCommit rename + + std::optional maybe_result; + + if (req.order_by.empty()) { + maybe_result = GetExpandOneResult(acc, src_vertice, req, maybe_filter_based_on_edge_uniquness, edge_filler); + + } else { + auto [in_edge_accessors, out_edge_accessors] = GetEdgesFromVertex(src_vertex_acc, req.direction); + const auto in_ordered_edges = OrderByElements(acc, dba, in_edge_accessors, req.order_by); + const auto out_ordered_edges = OrderByElements(acc, dba, out_edge_accessors, req.order_by); + + std::vector in_edge_ordered_accessors; + std::transform(in_ordered_edges.begin(), in_ordered_edges.end(), std::back_inserter(in_edge_ordered_accessors), + [](const auto &edge_element) { return edge_element.object_acc; }); + + std::vector out_edge_ordered_accessors; + std::transform(out_ordered_edges.begin(), out_ordered_edges.end(), std::back_inserter(out_edge_ordered_accessors), + [](const auto &edge_element) { return edge_element.object_acc; }); + maybe_result = GetExpandOneResult(src_vertex_acc, src_vertice, req, in_edge_ordered_accessors, + out_edge_ordered_accessors, maybe_filter_based_on_edge_uniquness, edge_filler); + } + + if (!maybe_result) { + action_successful = false; + break; + } + + results.emplace_back(maybe_result.value()); + if (batch_limit.has_value() && results.size() >= batch_limit.value()) { + break; + } + } + + msgs::ExpandOneResponse resp{}; + resp.success = action_successful; + if (action_successful) { + resp.result = std::move(results); + } + + return resp; } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CommitRequest &&req) { From b2f3fab6935fe0e8fecf10a563e15da4f2078f4f Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 8 Nov 2022 15:06:27 +0100 Subject: [PATCH 025/103] Remove comment --- src/storage/v3/shard_rsm.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 6725c0f23..9d1568bff 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -362,7 +362,8 @@ std::optional GetExpandOneResult( std::optional GetExpandOneResult( VertexAccessor v_acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, std::vector in_edge_accessors, std::vector out_edge_accessors, - const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler) { + const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, + const Schemas::Schema *schema) { /// Fill up source vertex auto source_vertex = FillUpSourceVertex(v_acc, req, src_vertex); if (!source_vertex) { @@ -370,7 +371,7 @@ std::optional GetExpandOneResult( } /// Fill up source vertex properties - auto src_vertex_properties = FillUpSourceVertexProperties(v_acc, req); + auto src_vertex_properties = FillUpSourceVertexProperties(v_acc, req, storage::v3::View::NEW, schema); if (!src_vertex_properties) { return std::nullopt; } @@ -932,13 +933,13 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { break; } - msgs::VertexId src_vertice(msgs::Label{.id = *label_id}, - conversions::ConvertValueVector(*primary_key)); // #NoCommit rename + msgs::VertexId src_vertice(msgs::Label{.id = *label_id}, conversions::ConvertValueVector(*primary_key)); std::optional maybe_result; if (req.order_by.empty()) { - maybe_result = GetExpandOneResult(acc, src_vertice, req, maybe_filter_based_on_edge_uniquness, edge_filler); + maybe_result = GetExpandOneResult(acc, src_vertice, req, maybe_filter_based_on_edge_uniquness, edge_filler, + shard_->GetSchema(shard_->PrimaryLabel())); } else { auto [in_edge_accessors, out_edge_accessors] = GetEdgesFromVertex(src_vertex_acc, req.direction); @@ -953,7 +954,8 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { std::transform(out_ordered_edges.begin(), out_ordered_edges.end(), std::back_inserter(out_edge_ordered_accessors), [](const auto &edge_element) { return edge_element.object_acc; }); maybe_result = GetExpandOneResult(src_vertex_acc, src_vertice, req, in_edge_ordered_accessors, - out_edge_ordered_accessors, maybe_filter_based_on_edge_uniquness, edge_filler); + out_edge_ordered_accessors, maybe_filter_based_on_edge_uniquness, edge_filler, + shard_->GetSchema(shard_->PrimaryLabel())); } if (!maybe_result) { From cad0e80d008c3040d8c99a535ce68429244e41eb Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 8 Nov 2022 17:42:31 +0100 Subject: [PATCH 026/103] Update test --- tests/simulation/shard_rsm.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index dac9c26b2..4c61f50b6 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -701,7 +701,7 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ std::vector order_by = { {msgs::Expression{"MG_SYMBOL_NODE.prop1"}, msgs::OrderingDirection::DESCENDING}}; size_t limit = 3; - std::vector filters = {"MG_SYMBOL_NODE.prop1 != " + std::to_string(src_vertex_val)}; + std::vector filters = {"MG_SYMBOL_NODE.prop1 != -1"}; msgs::ExpandOneRequest expand_one_req{}; @@ -731,16 +731,17 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ // We also check that the vertices are ordered by prop1 DESC - std::is_sorted(write_response.result.cbegin(), write_response.result.cend(), - [](const auto &vertex, const auto &other_vertex) { - const auto primary_key = vertex.src_vertex.id.second; - const auto other_primary_key = other_vertex.src_vertex.id.second; + auto is_sorted = std::is_sorted(write_response.result.cbegin(), write_response.result.cend(), + [](const auto &vertex, const auto &other_vertex) { + const auto primary_key = vertex.src_vertex.id.second; + const auto other_primary_key = other_vertex.src_vertex.id.second; - MG_ASSERT(primary_key.size() == 1); - MG_ASSERT(other_primary_key.size() == 1); - return primary_key[0].int_v > other_primary_key[0].int_v; - }); + MG_ASSERT(primary_key.size() == 1); + MG_ASSERT(other_primary_key.size() == 1); + return primary_key[0].int_v > other_primary_key[0].int_v; + }); + MG_ASSERT(is_sorted); break; } } From 2087877df2edfe72f643ee099f6c9dd96c43a415 Mon Sep 17 00:00:00 2001 From: jeremy Date: Wed, 9 Nov 2022 08:48:51 +0100 Subject: [PATCH 027/103] Add more checks in test --- tests/simulation/shard_rsm.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index 4c61f50b6..b54f7a993 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -727,7 +727,12 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ // We check that we do not have more results than the limit. Based on the data in the graph, we know that we should // receive exactly limit responses. auto expected_number_of_rows = std::min(expand_one_req.src_vertices.size(), limit); + MG_ASSERT(expected_number_of_rows == 1); // We are sending a single vertex to expand MG_ASSERT(write_response.result.size() == expected_number_of_rows); + const auto expected_number_of_edges = 10; // We know there are 10 out-going edges from V2->V3 + MG_ASSERT(write_response.result[0].out_edges_with_all_properties.size() == expected_number_of_edges); + MG_ASSERT(write_response.result[0] + .out_edges_with_specific_properties.empty()); // We are not asking for specific properties // We also check that the vertices are ordered by prop1 DESC From a2735c8953b531e1d0f9bbf7b348862444225bc2 Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 9 Nov 2022 12:10:46 +0100 Subject: [PATCH 028/103] Add label add/remove in UpdateVertex req --- src/query/v2/requests.hpp | 8 +++--- src/storage/v3/shard_rsm.cpp | 47 +++++++++++++++++++++++++++++++++- tests/simulation/shard_rsm.cpp | 6 ++--- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/query/v2/requests.hpp b/src/query/v2/requests.hpp index 2ea4efb57..fe2851893 100644 --- a/src/query/v2/requests.hpp +++ b/src/query/v2/requests.hpp @@ -450,10 +450,12 @@ struct ExpandOneResponse { std::vector result; }; -struct UpdateVertexProp { +struct UpdateVertex { PrimaryKey primary_key; // This should be a map - std::vector> property_updates; + std::vector add_labels; + std::vector remove_labels; + std::map property_updates; }; struct UpdateEdgeProp { @@ -496,7 +498,7 @@ struct DeleteVerticesResponse { struct UpdateVerticesRequest { Hlc transaction_id; - std::vector new_properties; + std::vector update_vertices; }; struct UpdateVerticesResponse { diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index f288e5e27..bf66bc463 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -535,12 +535,46 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { return msgs::CreateVerticesResponse{.success = action_successful}; } +void HandleError(const ResultErrorType &error, const std::string_view action) { + std::visit( + [action](T &&error) { + using ErrorType = std::remove_cvref_t; + if constexpr (std::is_same_v) { + spdlog::debug("{} failed with error: SchemaViolation", action); + } else if constexpr (std::is_same_v) { + switch (error) { + case Error::DELETED_OBJECT: + spdlog::debug("{} failed with error: DELETED_OBJECT", action); + break; + case Error::NONEXISTENT_OBJECT: + spdlog::debug("{} failed with error: NONEXISTENT_OBJECT", action); + break; + case Error::SERIALIZATION_ERROR: + spdlog::debug("{} failed with error: SERIALIZATION_ERROR", action); + break; + case Error::PROPERTIES_DISABLED: + spdlog::debug("{} failed with error: PROPERTIES_DISABLED", action); + break; + case Error::VERTEX_HAS_EDGES: + spdlog::debug("{} failed with error: VERTEX_HAS_EDGES", action); + break; + case Error::VERTEX_ALREADY_INSERTED: + spdlog::debug("{} failed with error: VERTEX_ALREADY_INSERTED", action); + break; + } + } else { + static_assert(kAlwaysFalse, "Missing type from variant visitor"); + } + }, + error); +} + msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); bool action_successful = true; - for (auto &vertex : req.new_properties) { + for (auto &vertex : req.update_vertices) { if (!action_successful) { break; } @@ -553,6 +587,17 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { continue; } + for (const auto label : vertex.add_labels) { + if (const auto maybe_error = vertex_to_update->AddLabelAndValidate(label); maybe_error.HasError()) { + HandleError(maybe_error.GetError(), "Update Vertex"); + } + } + for (const auto label : vertex.remove_labels) { + if (const auto maybe_error = vertex_to_update->RemoveLabelAndValidate(label); maybe_error.HasError()) { + HandleError(maybe_error.GetError(), "Update Vertex"); + } + } + for (auto &update_prop : vertex.property_updates) { auto result_schema = vertex_to_update->SetPropertyAndValidate(update_prop.first, ToPropertyValue(std::move(update_prop.second))); diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index 64d0a0861..23dfecac1 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -189,13 +189,13 @@ bool AttemptToUpdateVertex(ShardClient &client, int64_t value) { std::vector> property_updates; auto property_update = std::make_pair(PropertyId::FromUint(5), msgs::Value(static_cast(10000))); - auto vertex_prop = msgs::UpdateVertexProp{}; + msgs::UpdateVertex vertex_prop; vertex_prop.primary_key = vertex_id; vertex_prop.property_updates = {property_update}; - auto update_req = msgs::UpdateVerticesRequest{}; + msgs::UpdateVerticesRequest update_req; update_req.transaction_id.logical_id = GetTransactionId(); - update_req.new_properties = {vertex_prop}; + update_req.update_vertices = {vertex_prop}; while (true) { auto write_res = client.SendWriteRequest(update_req); From 23f1536eac5e72fb4710bf1dd55168937842a3af Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 9 Nov 2022 15:42:09 +0100 Subject: [PATCH 029/103] Add tests --- src/storage/v3/shard_rsm.cpp | 44 ++++-------------------------- tests/simulation/shard_rsm.cpp | 50 +++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 45 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index bf66bc463..18e4a6911 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -590,6 +590,8 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { for (const auto label : vertex.add_labels) { if (const auto maybe_error = vertex_to_update->AddLabelAndValidate(label); maybe_error.HasError()) { HandleError(maybe_error.GetError(), "Update Vertex"); + action_successful = false; + break; } } for (const auto label : vertex.remove_labels) { @@ -599,45 +601,11 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { } for (auto &update_prop : vertex.property_updates) { - auto result_schema = - vertex_to_update->SetPropertyAndValidate(update_prop.first, ToPropertyValue(std::move(update_prop.second))); - if (result_schema.HasError()) { - auto &error = result_schema.GetError(); - - std::visit( - [](T &&error) { - using ErrorType = std::remove_cvref_t; - if constexpr (std::is_same_v) { - spdlog::debug("Updating vertex failed with error: SchemaViolation"); - } else if constexpr (std::is_same_v) { - switch (error) { - case Error::DELETED_OBJECT: - spdlog::debug("Updating vertex failed with error: DELETED_OBJECT"); - break; - case Error::NONEXISTENT_OBJECT: - spdlog::debug("Updating vertex failed with error: NONEXISTENT_OBJECT"); - break; - case Error::SERIALIZATION_ERROR: - spdlog::debug("Updating vertex failed with error: SERIALIZATION_ERROR"); - break; - case Error::PROPERTIES_DISABLED: - spdlog::debug("Updating vertex failed with error: PROPERTIES_DISABLED"); - break; - case Error::VERTEX_HAS_EDGES: - spdlog::debug("Updating vertex failed with error: VERTEX_HAS_EDGES"); - break; - case Error::VERTEX_ALREADY_INSERTED: - spdlog::debug("Updating vertex failed with error: VERTEX_ALREADY_INSERTED"); - break; - } - } else { - static_assert(kAlwaysFalse, "Missing type from variant visitor"); - } - }, - error); - + if (const auto result_schema = vertex_to_update->SetPropertyAndValidate( + update_prop.first, ToPropertyValue(std::move(update_prop.second))); + result_schema.HasError()) { + HandleError(result_schema.GetError(), "Update Vertex"); action_successful = false; - break; } } diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index 23dfecac1..500649f4c 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -183,19 +183,53 @@ bool AttemptToDeleteVertex(ShardClient &client, int64_t value) { } } -bool AttemptToUpdateVertex(ShardClient &client, int64_t value) { - auto vertex_id = GetValuePrimaryKeysWithValue(value)[0]; +bool AttemptToUpdateVertex(ShardClient &client, int64_t vertex_primary_key, std::vector add_labels = {}, + std::vector remove_labels = {}) { + auto vertex_id = GetValuePrimaryKeysWithValue(vertex_primary_key)[0]; std::vector> property_updates; auto property_update = std::make_pair(PropertyId::FromUint(5), msgs::Value(static_cast(10000))); - msgs::UpdateVertex vertex_prop; - vertex_prop.primary_key = vertex_id; - vertex_prop.property_updates = {property_update}; + msgs::UpdateVertex update_vertex; + update_vertex.primary_key = vertex_id; + update_vertex.property_updates = {property_update}; + update_vertex.add_labels = add_labels; + update_vertex.remove_labels = remove_labels; msgs::UpdateVerticesRequest update_req; update_req.transaction_id.logical_id = GetTransactionId(); - update_req.update_vertices = {vertex_prop}; + update_req.update_vertices = {update_vertex}; + + while (true) { + auto write_res = client.SendWriteRequest(update_req); + if (write_res.HasError()) { + continue; + } + + auto write_response_result = write_res.GetValue(); + auto write_response = std::get(write_response_result); + + Commit(client, update_req.transaction_id); + return write_response.success; + } +} + +bool AttemptToRemoveVertexProperty(ShardClient &client, int64_t primary_key, std::vector add_labels = {}, + std::vector remove_labels = {}) { + auto vertex_id = GetValuePrimaryKeysWithValue(primary_key)[0]; + + std::vector> property_updates; + auto property_update = std::make_pair(PropertyId::FromUint(5), msgs::Value()); + + msgs::UpdateVertex update_vertex; + update_vertex.primary_key = vertex_id; + update_vertex.property_updates = {property_update}; + update_vertex.add_labels = add_labels; + update_vertex.remove_labels = remove_labels; + + msgs::UpdateVerticesRequest update_req; + update_req.transaction_id.logical_id = GetTransactionId(); + update_req.update_vertices = {update_vertex}; while (true) { auto write_res = client.SendWriteRequest(update_req); @@ -872,7 +906,9 @@ void TestCreateAndUpdateVertices(ShardClient &client) { auto unique_prop_val = GetUniqueInteger(); MG_ASSERT(AttemptToCreateVertex(client, unique_prop_val)); - MG_ASSERT(AttemptToUpdateVertex(client, unique_prop_val)); + MG_ASSERT(AttemptToUpdateVertex(client, unique_prop_val, {LabelId::FromInt(3)})); + MG_ASSERT(AttemptToUpdateVertex(client, unique_prop_val, {}, {LabelId::FromInt(3)})); + MG_ASSERT(AttemptToRemoveVertexProperty(client, unique_prop_val)); } void TestCreateEdge(ShardClient &client) { From 691f6af36df13ca33793c4431a692190353f12b4 Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 9 Nov 2022 15:52:08 +0100 Subject: [PATCH 030/103] Remove redundant code --- src/storage/v3/shard_rsm.cpp | 112 +++++++++++++---------------------- 1 file changed, 40 insertions(+), 72 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 18e4a6911..9f5c33218 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -318,6 +318,40 @@ bool FillEdges(const std::vector &edges, msgs::ExpandOneResultRow return true; } +void LogError(const ResultErrorType &error, const std::string_view action) { + std::visit( + [action](T &&error) { + using ErrorType = std::remove_cvref_t; + if constexpr (std::is_same_v) { + spdlog::debug("{} failed with error: SchemaViolation", action); + } else if constexpr (std::is_same_v) { + switch (error) { + case Error::DELETED_OBJECT: + spdlog::debug("{} failed with error: DELETED_OBJECT", action); + break; + case Error::NONEXISTENT_OBJECT: + spdlog::debug("{} failed with error: NONEXISTENT_OBJECT", action); + break; + case Error::SERIALIZATION_ERROR: + spdlog::debug("{} failed with error: SERIALIZATION_ERROR", action); + break; + case Error::PROPERTIES_DISABLED: + spdlog::debug("{} failed with error: PROPERTIES_DISABLED", action); + break; + case Error::VERTEX_HAS_EDGES: + spdlog::debug("{} failed with error: VERTEX_HAS_EDGES", action); + break; + case Error::VERTEX_ALREADY_INSERTED: + spdlog::debug("{} failed with error: VERTEX_ALREADY_INSERTED", action); + break; + } + } else { + static_assert(kAlwaysFalse, "Missing type from variant visitor"); + } + }, + error); +} + std::optional GetExpandOneResult( Shard::Accessor &acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, @@ -490,42 +524,10 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { PrimaryKey transformed_pk; std::transform(new_vertex.primary_key.begin(), new_vertex.primary_key.end(), std::back_inserter(transformed_pk), [](msgs::Value &val) { return ToPropertyValue(std::move(val)); }); - auto result_schema = acc.CreateVertexAndValidate(converted_label_ids, transformed_pk, converted_property_map); - if (result_schema.HasError()) { - auto &error = result_schema.GetError(); - - std::visit( - [](T &&error) { - using ErrorType = std::remove_cvref_t; - if constexpr (std::is_same_v) { - spdlog::debug("Creating vertex failed with error: SchemaViolation"); - } else if constexpr (std::is_same_v) { - switch (error) { - case Error::DELETED_OBJECT: - spdlog::debug("Creating vertex failed with error: DELETED_OBJECT"); - break; - case Error::NONEXISTENT_OBJECT: - spdlog::debug("Creating vertex failed with error: NONEXISTENT_OBJECT"); - break; - case Error::SERIALIZATION_ERROR: - spdlog::debug("Creating vertex failed with error: SERIALIZATION_ERROR"); - break; - case Error::PROPERTIES_DISABLED: - spdlog::debug("Creating vertex failed with error: PROPERTIES_DISABLED"); - break; - case Error::VERTEX_HAS_EDGES: - spdlog::debug("Creating vertex failed with error: VERTEX_HAS_EDGES"); - break; - case Error::VERTEX_ALREADY_INSERTED: - spdlog::debug("Creating vertex failed with error: VERTEX_ALREADY_INSERTED"); - break; - } - } else { - static_assert(kAlwaysFalse, "Missing type from variant visitor"); - } - }, - error); + if (auto result_schema = acc.CreateVertexAndValidate(converted_label_ids, transformed_pk, converted_property_map); + result_schema.HasError()) { + LogError(result_schema.GetError(), "Creating Vertex"); action_successful = false; break; @@ -535,40 +537,6 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { return msgs::CreateVerticesResponse{.success = action_successful}; } -void HandleError(const ResultErrorType &error, const std::string_view action) { - std::visit( - [action](T &&error) { - using ErrorType = std::remove_cvref_t; - if constexpr (std::is_same_v) { - spdlog::debug("{} failed with error: SchemaViolation", action); - } else if constexpr (std::is_same_v) { - switch (error) { - case Error::DELETED_OBJECT: - spdlog::debug("{} failed with error: DELETED_OBJECT", action); - break; - case Error::NONEXISTENT_OBJECT: - spdlog::debug("{} failed with error: NONEXISTENT_OBJECT", action); - break; - case Error::SERIALIZATION_ERROR: - spdlog::debug("{} failed with error: SERIALIZATION_ERROR", action); - break; - case Error::PROPERTIES_DISABLED: - spdlog::debug("{} failed with error: PROPERTIES_DISABLED", action); - break; - case Error::VERTEX_HAS_EDGES: - spdlog::debug("{} failed with error: VERTEX_HAS_EDGES", action); - break; - case Error::VERTEX_ALREADY_INSERTED: - spdlog::debug("{} failed with error: VERTEX_ALREADY_INSERTED", action); - break; - } - } else { - static_assert(kAlwaysFalse, "Missing type from variant visitor"); - } - }, - error); -} - msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); @@ -589,14 +557,14 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { for (const auto label : vertex.add_labels) { if (const auto maybe_error = vertex_to_update->AddLabelAndValidate(label); maybe_error.HasError()) { - HandleError(maybe_error.GetError(), "Update Vertex"); + LogError(maybe_error.GetError(), "Update Vertex"); action_successful = false; break; } } for (const auto label : vertex.remove_labels) { if (const auto maybe_error = vertex_to_update->RemoveLabelAndValidate(label); maybe_error.HasError()) { - HandleError(maybe_error.GetError(), "Update Vertex"); + LogError(maybe_error.GetError(), "Update Vertex"); } } @@ -604,7 +572,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { if (const auto result_schema = vertex_to_update->SetPropertyAndValidate( update_prop.first, ToPropertyValue(std::move(update_prop.second))); result_schema.HasError()) { - HandleError(result_schema.GetError(), "Update Vertex"); + LogError(result_schema.GetError(), "Update Vertex"); action_successful = false; break; } From 8636788ab295a97a52ba8fcddebd528455e4fb46 Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 9 Nov 2022 15:55:34 +0100 Subject: [PATCH 031/103] Fix typos --- src/storage/v3/shard_rsm.cpp | 30 ++++++++++++++---------------- tests/simulation/shard_rsm.cpp | 2 +- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 9f5c33218..f59264725 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -57,11 +57,11 @@ using conversions::ToPropertyValue; namespace { namespace msgs = msgs; -using AllEdgePropertyDataSructure = std::map; -using SpecificEdgePropertyDataSructure = std::vector; +using AllEdgePropertyDataStructure = std::map; +using SpecificEdgePropertyDataStructure = std::vector; -using AllEdgeProperties = std::tuple; -using SpecificEdgeProperties = std::tuple; +using AllEdgeProperties = std::tuple; +using SpecificEdgeProperties = std::tuple; using SpecificEdgePropertiesVector = std::vector; using AllEdgePropertiesVector = std::vector; @@ -69,7 +69,7 @@ using AllEdgePropertiesVector = std::vector; using EdgeAccessors = std::vector; using EdgeFiller = std::function; -using EdgeUniqunessFunction = std::function; +using EdgeUniquenessFunction = std::function; struct VertexIdCmpr { bool operator()(const storage::v3::VertexId *lhs, const storage::v3::VertexId *rhs) const { return *lhs < *rhs; } @@ -180,8 +180,6 @@ std::vector EvaluateVertexExpressions(DbAccessor &dba, const VertexA return evaluated_expressions; } -struct LocalError {}; - std::optional> FillUpSourceVertexSecondaryLabels(const std::optional &v_acc, const msgs::ExpandOneRequest &req) { auto secondary_labels = v_acc->Labels(View::NEW); @@ -242,7 +240,7 @@ std::optional> FillUpSourceVertexProperties(const st std::optional, 2>> FillUpConnectingEdges( const std::optional &v_acc, const msgs::ExpandOneRequest &req, - const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness) { + const EdgeUniquenessFunction &maybe_filter_based_on_edge_uniquness) { std::vector edge_types{}; edge_types.reserve(req.edge_types.size()); std::transform(req.edge_types.begin(), req.edge_types.end(), std::back_inserter(edge_types), @@ -296,11 +294,11 @@ std::optional, 2>> FillUpConnectingEdges( return std::array, 2>{in_edges, out_edges}; } -using AllEdgePropertyDataSructure = std::map; -using SpecificEdgePropertyDataSructure = std::vector; +using AllEdgePropertyDataStructure = std::map; +using SpecificEdgePropertyDataStructure = std::vector; -using AllEdgeProperties = std::tuple; -using SpecificEdgeProperties = std::tuple; +using AllEdgeProperties = std::tuple; +using SpecificEdgeProperties = std::tuple; using SpecificEdgePropertiesVector = std::vector; using AllEdgePropertiesVector = std::vector; @@ -354,7 +352,7 @@ void LogError(const ResultErrorType &error, const std::string_view action) { std::optional GetExpandOneResult( Shard::Accessor &acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, - const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, + const EdgeUniquenessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, const Schemas::Schema *schema) { /// Fill up source vertex const auto primary_key = ConvertPropertyVector(src_vertex.second); @@ -397,9 +395,9 @@ std::optional GetExpandOneResult( return result_row; } -EdgeUniqunessFunction InitializeEdgeUniqunessFunction(bool only_unique_neighbor_rows) { +EdgeUniquenessFunction InitializeEdgeUniquenessFunction(bool only_unique_neighbor_rows) { // Functions to select connecting edges based on uniquness - EdgeUniqunessFunction maybe_filter_based_on_edge_uniquness; + EdgeUniquenessFunction maybe_filter_based_on_edge_uniquness; if (only_unique_neighbor_rows) { maybe_filter_based_on_edge_uniquness = [](EdgeAccessors &&edges, @@ -871,7 +869,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { std::vector results; - auto maybe_filter_based_on_edge_uniquness = InitializeEdgeUniqunessFunction(req.only_unique_neighbor_rows); + auto maybe_filter_based_on_edge_uniquness = InitializeEdgeUniquenessFunction(req.only_unique_neighbor_rows); auto edge_filler = InitializeEdgeFillerFunction(req); for (auto &src_vertex : req.src_vertices) { diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index 500649f4c..5405abd8d 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -906,7 +906,7 @@ void TestCreateAndUpdateVertices(ShardClient &client) { auto unique_prop_val = GetUniqueInteger(); MG_ASSERT(AttemptToCreateVertex(client, unique_prop_val)); - MG_ASSERT(AttemptToUpdateVertex(client, unique_prop_val, {LabelId::FromInt(3)})); + MG_ASSERT(AttemptToUpdateVertex(client, unique_prop_val, {LabelId::FromInt(3)}, {})); MG_ASSERT(AttemptToUpdateVertex(client, unique_prop_val, {}, {LabelId::FromInt(3)})); MG_ASSERT(AttemptToRemoveVertexProperty(client, unique_prop_val)); } From a03041956596fe2f4e0972d8de1dbf01c582be0f Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 9 Nov 2022 16:07:27 +0100 Subject: [PATCH 032/103] Move LogResultError to helper --- src/storage/v3/request_helper.cpp | 34 +++++++++++++++++++++++++ src/storage/v3/request_helper.hpp | 5 ++++ src/storage/v3/shard_rsm.cpp | 42 +++---------------------------- src/storage/v3/shard_rsm.hpp | 3 --- 4 files changed, 43 insertions(+), 41 deletions(-) diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index bb1c8bca4..5c0297581 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -79,4 +79,38 @@ std::vector::const_iterator GetStartOrderedElementsIterator(const std:: return ordered_elements.end(); } +void LogResultError(const ResultErrorType &error, const std::string_view action) { + std::visit( + [action](T &&error) { + using ErrorType = std::remove_cvref_t; + if constexpr (std::is_same_v) { + spdlog::debug("{} failed with error: SchemaViolation", action); + } else if constexpr (std::is_same_v) { + switch (error) { + case Error::DELETED_OBJECT: + spdlog::debug("{} failed with error: DELETED_OBJECT", action); + break; + case Error::NONEXISTENT_OBJECT: + spdlog::debug("{} failed with error: NONEXISTENT_OBJECT", action); + break; + case Error::SERIALIZATION_ERROR: + spdlog::debug("{} failed with error: SERIALIZATION_ERROR", action); + break; + case Error::PROPERTIES_DISABLED: + spdlog::debug("{} failed with error: PROPERTIES_DISABLED", action); + break; + case Error::VERTEX_HAS_EDGES: + spdlog::debug("{} failed with error: VERTEX_HAS_EDGES", action); + break; + case Error::VERTEX_ALREADY_INSERTED: + spdlog::debug("{} failed with error: VERTEX_ALREADY_INSERTED", action); + break; + } + } else { + static_assert(kAlwaysFalse, "Missing type from variant visitor"); + } + }, + error); +} + } // namespace memgraph::storage::v3 diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 24ed40f8c..fc75f7717 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -18,6 +18,9 @@ namespace memgraph::storage::v3 { +template +constexpr auto kAlwaysFalse{false}; + inline bool TypedValueCompare(const TypedValue &a, const TypedValue &b) { // in ordering null comes after everything else // at the same time Null is not less that null @@ -113,4 +116,6 @@ VerticesIterable::Iterator GetStartVertexIterator(VerticesIterable &vertex_itera std::vector::const_iterator GetStartOrderedElementsIterator(const std::vector &ordered_elements, const std::vector &start_ids, View view); + +void LogResultError(const ResultErrorType &error, std::string_view action); } // namespace memgraph::storage::v3 diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index f59264725..67aefdda6 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -316,40 +316,6 @@ bool FillEdges(const std::vector &edges, msgs::ExpandOneResultRow return true; } -void LogError(const ResultErrorType &error, const std::string_view action) { - std::visit( - [action](T &&error) { - using ErrorType = std::remove_cvref_t; - if constexpr (std::is_same_v) { - spdlog::debug("{} failed with error: SchemaViolation", action); - } else if constexpr (std::is_same_v) { - switch (error) { - case Error::DELETED_OBJECT: - spdlog::debug("{} failed with error: DELETED_OBJECT", action); - break; - case Error::NONEXISTENT_OBJECT: - spdlog::debug("{} failed with error: NONEXISTENT_OBJECT", action); - break; - case Error::SERIALIZATION_ERROR: - spdlog::debug("{} failed with error: SERIALIZATION_ERROR", action); - break; - case Error::PROPERTIES_DISABLED: - spdlog::debug("{} failed with error: PROPERTIES_DISABLED", action); - break; - case Error::VERTEX_HAS_EDGES: - spdlog::debug("{} failed with error: VERTEX_HAS_EDGES", action); - break; - case Error::VERTEX_ALREADY_INSERTED: - spdlog::debug("{} failed with error: VERTEX_ALREADY_INSERTED", action); - break; - } - } else { - static_assert(kAlwaysFalse, "Missing type from variant visitor"); - } - }, - error); -} - std::optional GetExpandOneResult( Shard::Accessor &acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, const EdgeUniquenessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, @@ -525,7 +491,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { if (auto result_schema = acc.CreateVertexAndValidate(converted_label_ids, transformed_pk, converted_property_map); result_schema.HasError()) { - LogError(result_schema.GetError(), "Creating Vertex"); + LogResultError(result_schema.GetError(), "Creating Vertex"); action_successful = false; break; @@ -555,14 +521,14 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { for (const auto label : vertex.add_labels) { if (const auto maybe_error = vertex_to_update->AddLabelAndValidate(label); maybe_error.HasError()) { - LogError(maybe_error.GetError(), "Update Vertex"); + LogResultError(maybe_error.GetError(), "Add vertex labels"); action_successful = false; break; } } for (const auto label : vertex.remove_labels) { if (const auto maybe_error = vertex_to_update->RemoveLabelAndValidate(label); maybe_error.HasError()) { - LogError(maybe_error.GetError(), "Update Vertex"); + LogResultError(maybe_error.GetError(), "Remove vertex labels"); } } @@ -570,7 +536,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { if (const auto result_schema = vertex_to_update->SetPropertyAndValidate( update_prop.first, ToPropertyValue(std::move(update_prop.second))); result_schema.HasError()) { - LogError(result_schema.GetError(), "Update Vertex"); + LogResultError(result_schema.GetError(), "Update vertex properties"); action_successful = false; break; } diff --git a/src/storage/v3/shard_rsm.hpp b/src/storage/v3/shard_rsm.hpp index 95cad99fd..d301bf40b 100644 --- a/src/storage/v3/shard_rsm.hpp +++ b/src/storage/v3/shard_rsm.hpp @@ -21,9 +21,6 @@ namespace memgraph::storage::v3 { -template -constexpr auto kAlwaysFalse = false; - class ShardRsm { std::unique_ptr shard_; From 2045f54577fbb2439dffc65d75a885764ea39405 Mon Sep 17 00:00:00 2001 From: jeremy Date: Wed, 9 Nov 2022 21:36:41 +0100 Subject: [PATCH 033/103] Correct merge issue --- src/storage/v3/shard_rsm.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 6623bc316..5f6cb5959 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -369,8 +369,10 @@ std::optional GetExpandOneResult( const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, const Schemas::Schema *schema) { /// Fill up source vertex - auto source_vertex = FillUpSourceVertex(v_acc, req, src_vertex); - if (!source_vertex) { + msgs::Vertex source_vertex = {.id = src_vertex}; + if (const auto maybe_secondary_labels = FillUpSourceVertexSecondaryLabels(v_acc, req); maybe_secondary_labels) { + source_vertex.labels = *maybe_secondary_labels; + } else { return std::nullopt; } @@ -385,7 +387,7 @@ std::optional GetExpandOneResult( auto out_edges = maybe_filter_based_on_edge_uniquness(std::move(out_edge_accessors), msgs::EdgeDirection::OUT); msgs::ExpandOneResultRow result_row; - result_row.src_vertex = std::move(*source_vertex); + result_row.src_vertex = std::move(source_vertex); result_row.src_vertex_properties = std::move(*src_vertex_properties); static constexpr bool kInEdges = true; static constexpr bool kOutEdges = false; From 6eabceca4aeab81e1104ba09309cd87b56bf6264 Mon Sep 17 00:00:00 2001 From: jbajic Date: Mon, 14 Nov 2022 09:02:55 +0100 Subject: [PATCH 034/103] Add unit tests for UpdateVertex --- src/storage/v3/shard_rsm.cpp | 4 +- tests/unit/CMakeLists.txt | 3 + tests/unit/storage_v3_shard_rsm.cpp | 317 ++++++++++++++++++++++++++++ 3 files changed, 323 insertions(+), 1 deletion(-) create mode 100644 tests/unit/storage_v3_shard_rsm.cpp diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 67aefdda6..3321754aa 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -529,6 +529,8 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { for (const auto label : vertex.remove_labels) { if (const auto maybe_error = vertex_to_update->RemoveLabelAndValidate(label); maybe_error.HasError()) { LogResultError(maybe_error.GetError(), "Remove vertex labels"); + action_successful = false; + break; } } @@ -536,8 +538,8 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { if (const auto result_schema = vertex_to_update->SetPropertyAndValidate( update_prop.first, ToPropertyValue(std::move(update_prop.second))); result_schema.HasError()) { - LogResultError(result_schema.GetError(), "Update vertex properties"); action_successful = false; + LogResultError(result_schema.GetError(), "Update vertex properties"); break; } } diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 0e5824318..b3c8ee06e 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -343,6 +343,9 @@ target_link_libraries(${test_prefix}storage_v3_edge mg-storage-v3) add_unit_test(storage_v3_isolation_level.cpp) target_link_libraries(${test_prefix}storage_v3_isolation_level mg-storage-v3) +add_unit_test(storage_v3_shard_rsm.cpp) +target_link_libraries(${test_prefix}storage_v3_shard_rsm mg-storage-v3) + add_unit_test(replication_persistence_helper.cpp) target_link_libraries(${test_prefix}replication_persistence_helper mg-storage-v2) diff --git a/tests/unit/storage_v3_shard_rsm.cpp b/tests/unit/storage_v3_shard_rsm.cpp new file mode 100644 index 000000000..a45a60db4 --- /dev/null +++ b/tests/unit/storage_v3_shard_rsm.cpp @@ -0,0 +1,317 @@ +// Copyright 2022 Memgraph Ltd. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source +// License, and you may not use this file except in compliance with the Business Source License. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "common/types.hpp" +#include "query/v2/requests.hpp" +#include "storage/v3/id_types.hpp" +#include "storage/v3/property_value.hpp" +#include "storage/v3/schema_validator.hpp" +#include "storage/v3/schemas.hpp" +#include "storage/v3/shard_rsm.hpp" +#include "storage/v3/storage.hpp" +#include "storage/v3/temporal.hpp" +#include "storage/v3/vertex_id.hpp" + +using testing::Pair; +using testing::UnorderedElementsAre; +using SchemaType = memgraph::common::SchemaType; + +namespace memgraph::storage::v3::tests { + +uint64_t GetTransactionId() { + static uint64_t transaction_id = 0; + return transaction_id++; +} + +class ShardRSMTest : public testing::Test { + private: + NameIdMapper id_mapper_{{{1, "primary_label"}, + {2, "primary_label2"}, + {3, "label"}, + {4, "primary_prop1"}, + {5, "primary_prop2"}, + {6, "prop"}}}; + + protected: + ShardRSMTest() { + PropertyValue min_pk(static_cast(0)); + std::vector min_prim_key = {min_pk}; + PropertyValue max_pk(static_cast(10000000)); + std::vector max_prim_key = {max_pk}; + + auto shard_ptr1 = std::make_unique(primary_label, min_prim_key, max_prim_key, std::vector{schema_prop}); + shard_ptr1->StoreMapping({{1, "primary_label"}, + {2, "primary_label2"}, + {3, "label"}, + {4, "primary_prop1"}, + {5, "primary_prop2"}, + {6, "prop"}}); + shard_ptr1->CreateSchema(primary_label2, {{primary_property2, SchemaType::INT}}); + shard_rsm = std::make_unique(std::move(shard_ptr1)); + } + + LabelId NameToLabel(const std::string &name) { return LabelId::FromUint(id_mapper_.NameToId(name)); } + + PropertyId NameToProperty(const std::string &name) { return PropertyId::FromUint(id_mapper_.NameToId(name)); } + + auto Commit(const auto &req) { + const coordinator::Hlc commit_timestamp{GetTransactionId()}; + msgs::CommitRequest commit_req; + commit_req.transaction_id = req.transaction_id; + commit_req.commit_timestamp = commit_timestamp; + return shard_rsm->Apply(commit_req); + } + + void CreateVertex(const msgs::PrimaryKey &primary_key, const std::vector labels, + const std::vector> &properties) { + msgs::NewVertex vertex = {labels, primary_key, properties}; + msgs::CreateVerticesRequest create_req; + create_req.new_vertices = {vertex}; + create_req.new_vertices = {vertex}; + create_req.transaction_id.logical_id = GetTransactionId(); + + auto write_res = shard_rsm->Apply(create_req); + ASSERT_TRUE(std::holds_alternative(write_res)); + + auto commit_res = Commit(create_req); + ASSERT_TRUE(std::holds_alternative(commit_res)); + ASSERT_TRUE(std::get(commit_res).success); + } + + void AssertVertexExists(const msgs::PrimaryKey &primary_key, const std::vector &labels, + const std::vector> &properties) { + msgs::ScanVerticesRequest scan_req; + scan_req.props_to_return = std::nullopt; + scan_req.start_id = msgs::VertexId{msgs::Label{.id = primary_label}, primary_key}; + scan_req.storage_view = msgs::StorageView::OLD; + scan_req.transaction_id.logical_id = GetTransactionId(); + + // Make request + auto maybe_read_res = shard_rsm->Read(scan_req); + ASSERT_TRUE(std::holds_alternative(maybe_read_res)); + const auto read_res = std::get(maybe_read_res); + EXPECT_TRUE(read_res.success); + EXPECT_EQ(read_res.results.size(), 1); + + // Read results + const auto res = read_res.results[0]; + const auto vtx_id = msgs::VertexId{msgs::Label{.id = primary_label}, primary_key}; + EXPECT_EQ(res.vertex.id, vtx_id); + EXPECT_EQ(res.vertex.labels, labels); + EXPECT_EQ(res.props, properties); + } + + LabelId primary_label{NameToLabel("primary_label")}; + LabelId primary_label2{NameToLabel("primary_label2")}; + LabelId label{NameToLabel("label")}; + PropertyId primary_property1{NameToProperty("primary_prop1")}; + PropertyId primary_property2{NameToProperty("primary_prop2")}; + PropertyId prop{NameToProperty("prop")}; + SchemaProperty schema_prop{primary_property1, SchemaType::INT}; + std::unique_ptr shard_rsm; +}; + +TEST_F(ShardRSMTest, TestUpdateVertexSecondaryProperty) { + const msgs::Value primary_key_val{static_cast(1)}; + const msgs::PrimaryKey pk{primary_key_val}; + + // Create Vertex + CreateVertex(pk, {}, {}); + + // Add property prop + static constexpr int64_t updated_vertex_id{10}; + { + msgs::UpdateVerticesRequest update_req; + update_req.transaction_id.logical_id = GetTransactionId(); + + update_req.update_vertices = + std::vector{{pk, {}, {}, {{msgs::PropertyId(prop), msgs::Value(updated_vertex_id)}}}}; + + const auto write_res = shard_rsm->Apply(update_req); + ASSERT_TRUE(std::holds_alternative(write_res)); + EXPECT_TRUE(std::get(write_res).success); + + const auto commit_res = Commit(update_req); + ASSERT_TRUE(std::holds_alternative(commit_res)); + EXPECT_TRUE(std::get(commit_res).success); + } + AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}, {prop, msgs::Value(updated_vertex_id)}}); + + // Update property prop + static constexpr int64_t updated_vertex_id_2{101}; + { + msgs::UpdateVerticesRequest update_req; + update_req.transaction_id.logical_id = GetTransactionId(); + + update_req.update_vertices = + std::vector{{pk, {}, {}, {{msgs::PropertyId(prop), msgs::Value(updated_vertex_id_2)}}}}; + + const auto write_res = shard_rsm->Apply(update_req); + ASSERT_TRUE(std::holds_alternative(write_res)); + ASSERT_TRUE(std::get(write_res).success); + + const auto commit_res = Commit(update_req); + ASSERT_TRUE(std::holds_alternative(commit_res)); + EXPECT_TRUE(std::get(commit_res).success); + AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}, {prop, msgs::Value(updated_vertex_id_2)}}); + } + AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}, {prop, msgs::Value(updated_vertex_id_2)}}); + + // Remove property prop + { + msgs::UpdateVerticesRequest update_req; + update_req.transaction_id.logical_id = GetTransactionId(); + + update_req.update_vertices = + std::vector{{pk, {}, {}, {{msgs::PropertyId(prop), msgs::Value()}}}}; + + const auto write_res = shard_rsm->Apply(update_req); + ASSERT_TRUE(std::holds_alternative(write_res)); + EXPECT_TRUE(std::get(write_res).success); + + const auto commit_res = Commit(update_req); + ASSERT_TRUE(std::holds_alternative(commit_res)); + EXPECT_TRUE(std::get(commit_res).success); + } + AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}}); +} + +TEST_F(ShardRSMTest, TestUpdateVertexPrimaryProperty) { + const msgs::Value primary_key_val{static_cast(1)}; + const msgs::PrimaryKey pk{primary_key_val}; + + // Create Vertex + CreateVertex(pk, {}, {}); + + // Try to update primary property + static constexpr int64_t updated_vertex_id{10}; + { + msgs::UpdateVerticesRequest update_req; + update_req.transaction_id.logical_id = GetTransactionId(); + + update_req.update_vertices = std::vector{ + {pk, {}, {}, {{msgs::PropertyId(primary_property1), msgs::Value(updated_vertex_id)}}}}; + + const auto write_res = shard_rsm->Apply(update_req); + ASSERT_TRUE(std::holds_alternative(write_res)); + EXPECT_FALSE(std::get(write_res).success); + } + AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}}); + // Try to update primary property of another schema + { + msgs::UpdateVerticesRequest update_req; + update_req.transaction_id.logical_id = GetTransactionId(); + + update_req.update_vertices = std::vector{ + {pk, {}, {}, {{msgs::PropertyId(primary_property2), msgs::Value(updated_vertex_id)}}}}; + + const auto write_res = shard_rsm->Apply(update_req); + ASSERT_TRUE(std::holds_alternative(write_res)); + EXPECT_TRUE(std::get(write_res).success); + + const auto commit_res = Commit(update_req); + ASSERT_TRUE(std::holds_alternative(commit_res)); + EXPECT_TRUE(std::get(commit_res).success); + } + AssertVertexExists(pk, {}, + {{primary_property1, primary_key_val}, {primary_property2, msgs::Value(updated_vertex_id)}}); +} + +TEST_F(ShardRSMTest, TestUpdateSecondaryLabel) { + const msgs::Value primary_key_val{static_cast(1)}; + const msgs::PrimaryKey pk{primary_key_val}; + + // Create Vertex + CreateVertex(pk, {}, {}); + + // Add label label + const msgs::Label secondary_label{label}; + { + msgs::UpdateVerticesRequest update_req; + update_req.transaction_id.logical_id = GetTransactionId(); + + update_req.update_vertices = std::vector{{pk, {label}, {}, {}}}; + + const auto write_res = shard_rsm->Apply(update_req); + ASSERT_TRUE(std::holds_alternative(write_res)); + ASSERT_TRUE(std::get(write_res).success); + + const auto commit_res = Commit(update_req); + ASSERT_TRUE(std::holds_alternative(commit_res)); + EXPECT_TRUE(std::get(commit_res).success); + } + AssertVertexExists(pk, {secondary_label}, {{primary_property1, primary_key_val}}); + + // Remove primary label + { + msgs::UpdateVerticesRequest update_req; + update_req.transaction_id.logical_id = GetTransactionId(); + + update_req.update_vertices = std::vector{{pk, {}, {label}, {}}}; + + const auto write_res = shard_rsm->Apply(update_req); + ASSERT_TRUE(std::holds_alternative(write_res)); + EXPECT_TRUE(std::get(write_res).success); + + const auto commit_res = Commit(update_req); + ASSERT_TRUE(std::holds_alternative(commit_res)); + EXPECT_TRUE(std::get(commit_res).success); + } + AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}}); +} + +TEST_F(ShardRSMTest, TestUpdatePrimaryLabel) { + const msgs::Value primary_key_val{static_cast(1)}; + const msgs::PrimaryKey pk{primary_key_val}; + + // Create Vertex + CreateVertex(pk, {}, {}); + + // Remove primary label + { + msgs::UpdateVerticesRequest update_req; + update_req.transaction_id.logical_id = GetTransactionId(); + + update_req.update_vertices = std::vector{{pk, {}, {primary_label}, {}}}; + + const auto write_res = shard_rsm->Apply(update_req); + ASSERT_TRUE(std::holds_alternative(write_res)); + EXPECT_FALSE(std::get(write_res).success); + } + AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}}); + + // Add different primary label + { + msgs::UpdateVerticesRequest update_req; + update_req.transaction_id.logical_id = GetTransactionId(); + + update_req.update_vertices = std::vector{{pk, {primary_label2}, {}, {}}}; + + const auto write_res = shard_rsm->Apply(update_req); + ASSERT_TRUE(std::holds_alternative(write_res)); + EXPECT_FALSE(std::get(write_res).success); + } + AssertVertexExists(pk, {}, {{primary_property1, primary_key_val}}); +} + +} // namespace memgraph::storage::v3::tests From 0462b8fc8fd08a0d3d30a23e099422fd3395de50 Mon Sep 17 00:00:00 2001 From: jbajic Date: Mon, 14 Nov 2022 09:15:03 +0100 Subject: [PATCH 035/103] Address review comments --- src/query/v2/requests.hpp | 2 +- src/storage/v3/request_helper.cpp | 2 +- src/storage/v3/request_helper.hpp | 4 +--- src/utils/template_utils.hpp | 18 ++++++++++++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 src/utils/template_utils.hpp diff --git a/src/query/v2/requests.hpp b/src/query/v2/requests.hpp index fe2851893..356e2c0da 100644 --- a/src/query/v2/requests.hpp +++ b/src/query/v2/requests.hpp @@ -452,7 +452,7 @@ struct ExpandOneResponse { struct UpdateVertex { PrimaryKey primary_key; - // This should be a map + // Labels are first added and then removed from vertices std::vector add_labels; std::vector remove_labels; std::map property_updates; diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index 5c0297581..0be659c43 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -107,7 +107,7 @@ void LogResultError(const ResultErrorType &error, const std::string_view action) break; } } else { - static_assert(kAlwaysFalse, "Missing type from variant visitor"); + static_assert(utils::kAlwaysFalse, "Missing type from variant visitor"); } }, error); diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index fc75f7717..1d26e338d 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -15,12 +15,10 @@ #include "storage/v3/bindings/typed_value.hpp" #include "storage/v3/shard.hpp" #include "storage/v3/vertex_accessor.hpp" +#include "utils/template_utils.hpp" namespace memgraph::storage::v3 { -template -constexpr auto kAlwaysFalse{false}; - inline bool TypedValueCompare(const TypedValue &a, const TypedValue &b) { // in ordering null comes after everything else // at the same time Null is not less that null diff --git a/src/utils/template_utils.hpp b/src/utils/template_utils.hpp new file mode 100644 index 000000000..e84cbc652 --- /dev/null +++ b/src/utils/template_utils.hpp @@ -0,0 +1,18 @@ +// Copyright 2022 Memgraph Ltd. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source +// License, and you may not use this file except in compliance with the Business Source License. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. +#pragma once + +namespace memgraph::utils { + +template +constexpr auto kAlwaysFalse{false}; + +} // namespace memgraph::utils From a9c5d4072173c375db2ed85c2fd172a523aaea03 Mon Sep 17 00:00:00 2001 From: Jure Bajic Date: Mon, 14 Nov 2022 10:32:18 +0100 Subject: [PATCH 036/103] Update src/utils/template_utils.hpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: János Benjamin Antal --- src/utils/template_utils.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils/template_utils.hpp b/src/utils/template_utils.hpp index e84cbc652..4065283b2 100644 --- a/src/utils/template_utils.hpp +++ b/src/utils/template_utils.hpp @@ -8,6 +8,7 @@ // the Business Source License, use of this software will be governed // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. + #pragma once namespace memgraph::utils { From ef755e466cc191b0ba76fe27b630c27e9d267aeb Mon Sep 17 00:00:00 2001 From: jbajic Date: Mon, 14 Nov 2022 14:15:29 +0100 Subject: [PATCH 037/103] Add ShardError --- src/storage/v3/result.hpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index 86fdb10d0..ecec4ad1e 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -11,6 +11,7 @@ #pragma once +#include #include #include "utils/result.hpp" @@ -19,16 +20,35 @@ namespace memgraph::storage::v3 { static_assert(std::is_same_v); -enum class Error : uint8_t { +enum class ErrorCode { SERIALIZATION_ERROR, NONEXISTENT_OBJECT, DELETED_OBJECT, VERTEX_HAS_EDGES, PROPERTIES_DISABLED, - VERTEX_ALREADY_INSERTED + VERTEX_ALREADY_INSERTED, + // Schema Violations + SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL, + SCHEMA_VERTEX_PROPERTY_WRONG_TYPE, + SCHEMA_VERTEX_UPDATE_PRIMARY_KEY, + SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL, + SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY, + SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED, }; +struct ShardError { + ShardError(ErrorCode code, std::string message, std::string source) + : code{code}, message{std::move(message)}, source{std::move(source)} {} + + ErrorCode code; + // TODO Maybe add category + std::string message; + std::string source; +}; + +#define SHARD_ERROR(...) memgraph::storage::v3::ShardError(__VA_ARGS__, fmt::format("{}:{}", __FILE__, __LINE__)) + template -using Result = utils::BasicResult; +using Result = utils::BasicResult; } // namespace memgraph::storage::v3 From 618237cc962b06be11e65a1e9d1d27684a83a2e6 Mon Sep 17 00:00:00 2001 From: jbajic Date: Mon, 14 Nov 2022 14:25:20 +0100 Subject: [PATCH 038/103] Adapt schema validator --- src/storage/v3/name_id_mapper.hpp | 5 ++ src/storage/v3/schema_validator.cpp | 72 ++++++++++++++--------------- src/storage/v3/schema_validator.hpp | 45 +++++------------- src/storage/v3/shard.cpp | 3 +- 4 files changed, 52 insertions(+), 73 deletions(-) diff --git a/src/storage/v3/name_id_mapper.hpp b/src/storage/v3/name_id_mapper.hpp index f241bca54..70b5c5ded 100644 --- a/src/storage/v3/name_id_mapper.hpp +++ b/src/storage/v3/name_id_mapper.hpp @@ -17,6 +17,7 @@ #include #include +#include "storage/v3/id_types.hpp" #include "utils/logging.hpp" #include "utils/skip_list.hpp" @@ -47,6 +48,10 @@ class NameIdMapper final { return kUnmappedId; } + const std::string &IdToName(const LabelId label_id) const { return IdToName(label_id.AsInt()); } + + const std::string &IdToName(const PropertyId property_id) const { return IdToName(property_id.AsInt()); } + const std::string &IdToName(const uint64_t id) const { auto it = id_to_name_.find(id); MG_ASSERT(it != id_to_name_.end(), "Id not know in mapper!"); diff --git a/src/storage/v3/schema_validator.cpp b/src/storage/v3/schema_validator.cpp index 748eda28d..48fd95eba 100644 --- a/src/storage/v3/schema_validator.cpp +++ b/src/storage/v3/schema_validator.cpp @@ -16,67 +16,60 @@ #include #include "common/types.hpp" +#include "storage/v3/name_id_mapper.hpp" +#include "storage/v3/result.hpp" #include "storage/v3/schemas.hpp" namespace memgraph::storage::v3 { -bool operator==(const SchemaViolation &lhs, const SchemaViolation &rhs) { - return lhs.status == rhs.status && lhs.label == rhs.label && - lhs.violated_schema_property == rhs.violated_schema_property && - lhs.violated_property_value == rhs.violated_property_value; -} +SchemaValidator::SchemaValidator(Schemas &schemas, const NameIdMapper &name_id_mapper) + : schemas_{&schemas}, name_id_mapper_{&name_id_mapper} {} -SchemaViolation::SchemaViolation(ValidationStatus status, LabelId label) : status{status}, label{label} {} - -SchemaViolation::SchemaViolation(ValidationStatus status, LabelId label, SchemaProperty violated_schema_property) - : status{status}, label{label}, violated_schema_property{violated_schema_property} {} - -SchemaViolation::SchemaViolation(ValidationStatus status, LabelId label, SchemaProperty violated_schema_property, - PropertyValue violated_property_value) - : status{status}, - label{label}, - violated_schema_property{violated_schema_property}, - violated_property_value{violated_property_value} {} - -SchemaValidator::SchemaValidator(Schemas &schemas) : schemas_{schemas} {} - -std::optional SchemaValidator::ValidateVertexCreate( +std::optional SchemaValidator::ValidateVertexCreate( LabelId primary_label, const std::vector &labels, const std::vector &primary_properties) const { // Schema on primary label - const auto *schema = schemas_.GetSchema(primary_label); + const auto *schema = schemas_->GetSchema(primary_label); if (schema == nullptr) { - return SchemaViolation(SchemaViolation::ValidationStatus::NO_SCHEMA_DEFINED_FOR_LABEL, primary_label); + return SHARD_ERROR(ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL, + fmt::format("Schema not defined for label :{}", name_id_mapper_->IdToName(primary_label))); } // Is there another primary label among secondary labels for (const auto &secondary_label : labels) { - if (schemas_.GetSchema(secondary_label)) { - return SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_SECONDARY_LABEL_IS_PRIMARY, secondary_label); + if (schemas_->GetSchema(secondary_label)) { + return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY, + fmt::format("Cannot add label :{}, since it is defined as a primary label", + name_id_mapper_->IdToName(secondary_label))); } } // Quick size check if (schema->second.size() != primary_properties.size()) { - return SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_PRIMARY_PROPERTIES_UNDEFINED, primary_label); + return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED, + fmt::format("Not all primary properties have been specified for :{} vertex", + name_id_mapper_->IdToName(primary_label))); } // Check only properties defined by schema for (size_t i{0}; i < schema->second.size(); ++i) { // Check schema property type if (auto property_schema_type = PropertyTypeToSchemaType(primary_properties[i]); property_schema_type && *property_schema_type != schema->second[i].type) { - return SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_PROPERTY_WRONG_TYPE, primary_label, - schema->second[i], primary_properties[i]); + return SHARD_ERROR( + ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE, + fmt::format("Property {} is of wrong type, expected {}, actual {}", + name_id_mapper_->IdToName(schema->second[i].property_id), + SchemaTypeToString(schema->second[i].type), SchemaTypeToString(*property_schema_type))); } } return std::nullopt; } -std::optional SchemaValidator::ValidatePropertyUpdate(const LabelId primary_label, - const PropertyId property_id) const { +std::optional SchemaValidator::ValidatePropertyUpdate(const LabelId primary_label, + const PropertyId property_id) const { // Verify existence of schema on primary label - const auto *schema = schemas_.GetSchema(primary_label); + const auto *schema = schemas_->GetSchema(primary_label); MG_ASSERT(schema, "Cannot validate against non existing schema!"); // Verify that updating property is not part of schema @@ -84,16 +77,19 @@ std::optional SchemaValidator::ValidatePropertyUpdate(const Lab schema->second, [property_id](const auto &schema_property) { return property_id == schema_property.property_id; }); schema_property != schema->second.end()) { - return SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_KEY, primary_label, - *schema_property); + return SHARD_ERROR( + ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY, + fmt::format("Cannot update primary property {} of schema on label :{}", + name_id_mapper_->IdToName(schema_property->property_id), name_id_mapper_->IdToName(primary_label))); } return std::nullopt; } -std::optional SchemaValidator::ValidateLabelUpdate(const LabelId label) const { - const auto *schema = schemas_.GetSchema(label); +std::optional SchemaValidator::ValidateLabelUpdate(const LabelId label) const { + const auto *schema = schemas_->GetSchema(label); if (schema) { - return SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_LABEL, label); + return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL, + fmt::format("Cannot add/remove primary label :{}", name_id_mapper_->IdToName(label))); } return std::nullopt; } @@ -103,15 +99,15 @@ const Schemas::Schema *SchemaValidator::GetSchema(LabelId label) const { return VertexValidator::VertexValidator(const SchemaValidator &schema_validator, const LabelId primary_label) : schema_validator{&schema_validator}, primary_label_{primary_label} {} -std::optional VertexValidator::ValidatePropertyUpdate(PropertyId property_id) const { +std::optional VertexValidator::ValidatePropertyUpdate(PropertyId property_id) const { return schema_validator->ValidatePropertyUpdate(primary_label_, property_id); }; -std::optional VertexValidator::ValidateAddLabel(LabelId label) const { +std::optional VertexValidator::ValidateAddLabel(LabelId label) const { return schema_validator->ValidateLabelUpdate(label); } -std::optional VertexValidator::ValidateRemoveLabel(LabelId label) const { +std::optional VertexValidator::ValidateRemoveLabel(LabelId label) const { return schema_validator->ValidateLabelUpdate(label); } diff --git a/src/storage/v3/schema_validator.hpp b/src/storage/v3/schema_validator.hpp index ce813fde2..329d4a582 100644 --- a/src/storage/v3/schema_validator.hpp +++ b/src/storage/v3/schema_validator.hpp @@ -14,65 +14,42 @@ #include #include +#include "storage/v2/result.hpp" #include "storage/v3/id_types.hpp" +#include "storage/v3/name_id_mapper.hpp" #include "storage/v3/property_value.hpp" #include "storage/v3/result.hpp" #include "storage/v3/schemas.hpp" namespace memgraph::storage::v3 { -struct SchemaViolation { - enum class ValidationStatus : uint8_t { - NO_SCHEMA_DEFINED_FOR_LABEL, - VERTEX_PROPERTY_WRONG_TYPE, - VERTEX_UPDATE_PRIMARY_KEY, - VERTEX_UPDATE_PRIMARY_LABEL, - VERTEX_SECONDARY_LABEL_IS_PRIMARY, - VERTEX_PRIMARY_PROPERTIES_UNDEFINED, - }; - - SchemaViolation(ValidationStatus status, LabelId label); - - SchemaViolation(ValidationStatus status, LabelId label, SchemaProperty violated_schema_property); - - SchemaViolation(ValidationStatus status, LabelId label, SchemaProperty violated_schema_property, - PropertyValue violated_property_value); - - friend bool operator==(const SchemaViolation &lhs, const SchemaViolation &rhs); - - ValidationStatus status; - LabelId label; - std::optional violated_schema_property; - std::optional violated_property_value; -}; - class SchemaValidator { public: - explicit SchemaValidator(Schemas &schemas); + explicit SchemaValidator(Schemas &schemas, const NameIdMapper &name_id_mapper); - [[nodiscard]] std::optional ValidateVertexCreate( + [[nodiscard]] std::optional ValidateVertexCreate( LabelId primary_label, const std::vector &labels, const std::vector &primary_properties) const; - [[nodiscard]] std::optional ValidatePropertyUpdate(LabelId primary_label, - PropertyId property_id) const; + [[nodiscard]] std::optional ValidatePropertyUpdate(LabelId primary_label, PropertyId property_id) const; - [[nodiscard]] std::optional ValidateLabelUpdate(LabelId label) const; + [[nodiscard]] std::optional ValidateLabelUpdate(LabelId label) const; const Schemas::Schema *GetSchema(LabelId label) const; private: - Schemas &schemas_; + Schemas *schemas_; + const NameIdMapper *name_id_mapper_; }; struct VertexValidator { explicit VertexValidator(const SchemaValidator &schema_validator, LabelId primary_label); - [[nodiscard]] std::optional ValidatePropertyUpdate(PropertyId property_id) const; + [[nodiscard]] std::optional ValidatePropertyUpdate(PropertyId property_id) const; - [[nodiscard]] std::optional ValidateAddLabel(LabelId label) const; + [[nodiscard]] std::optional ValidateAddLabel(LabelId label) const; - [[nodiscard]] std::optional ValidateRemoveLabel(LabelId label) const; + [[nodiscard]] std::optional ValidateRemoveLabel(LabelId label) const; const SchemaValidator *schema_validator; diff --git a/src/storage/v3/shard.cpp b/src/storage/v3/shard.cpp index 0e596172d..e4d993ffa 100644 --- a/src/storage/v3/shard.cpp +++ b/src/storage/v3/shard.cpp @@ -31,6 +31,7 @@ #include "storage/v3/indices.hpp" #include "storage/v3/key_store.hpp" #include "storage/v3/mvcc.hpp" +#include "storage/v3/name_id_mapper.hpp" #include "storage/v3/property_value.hpp" #include "storage/v3/schema_validator.hpp" #include "storage/v3/shard_operation_result.hpp" @@ -327,7 +328,7 @@ Shard::Shard(const LabelId primary_label, const PrimaryKey min_primary_key, : primary_label_{primary_label}, min_primary_key_{min_primary_key}, max_primary_key_{max_primary_key}, - schema_validator_{schemas_}, + schema_validator_{schemas_, name_id_mapper_}, vertex_validator_{schema_validator_, primary_label}, indices_{config.items, vertex_validator_}, isolation_level_{config.transaction.isolation_level}, From 131d7f2a744d07c9c0d03bb7c57dd251284ec9f6 Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 14 Nov 2022 18:21:03 +0100 Subject: [PATCH 039/103] OrderByElements: no longer templated over vertice/edge types. For edges, we always need to have access to the corresponding vertex_accessor (ex of sorting expr needing both : "vertex.map[edge]") ComputeExpression: made assert instead of if check --- src/storage/v3/expr.cpp | 6 ++- src/storage/v3/request_helper.hpp | 87 ++++++++++++++++++++----------- src/storage/v3/shard_rsm.cpp | 12 ++--- 3 files changed, 68 insertions(+), 37 deletions(-) diff --git a/src/storage/v3/expr.cpp b/src/storage/v3/expr.cpp index eaff472bd..53146f595 100644 --- a/src/storage/v3/expr.cpp +++ b/src/storage/v3/expr.cpp @@ -186,7 +186,8 @@ TypedValue ComputeExpression(DbAccessor &dba, const std::optional(expr))->Accept(symbol_generator); - if (node_identifier.symbol_pos_ != -1 && v_acc.has_value()) { + if (node_identifier.symbol_pos_ != -1) { + MG_ASSERT(v_acc.has_value()); MG_ASSERT(std::find_if(symbol_table.table().begin(), symbol_table.table().end(), [&node_name](const std::pair &position_symbol_pair) { return position_symbol_pair.second.name() == node_name; @@ -195,7 +196,8 @@ TypedValue ComputeExpression(DbAccessor &dba, const std::optional &position_symbol_pair) { return position_symbol_pair.second.name() == edge_name; diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index e3c5b9d3f..955059f26 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -112,44 +112,73 @@ struct Element { TObjectAccessor object_acc; }; -template -std::vector> OrderByElements(Shard::Accessor &acc, DbAccessor &dba, TIterable &iterable, - std::vector &order_bys) { - std::vector> ordered; - ordered.reserve(acc.ApproximateVertexCount()); +template +std::vector> OrderByVertices(Shard::Accessor &acc, DbAccessor &dba, TIterable &iterable, + std::vector &order_bys) { + static_assert(std::is_same_v || std::is_same_v>); + std::vector ordering; ordering.reserve(order_bys.size()); - for (const auto &order : order_bys) { - switch (order.direction) { - case memgraph::msgs::OrderingDirection::ASCENDING: { - ordering.push_back(Ordering::ASC); - break; - } - case memgraph::msgs::OrderingDirection::DESCENDING: { - ordering.push_back(Ordering::DESC); - break; - } + std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(ordering), [](const auto &order_by) { + if (memgraph::msgs::OrderingDirection::ASCENDING == order_by.direction) { + return Ordering::ASC; } - } - auto compare_typed_values = TypedValueVectorCompare(ordering); - auto it = iterable.begin(); - for (; it != iterable.end(); ++it) { + MG_ASSERT(memgraph::msgs::OrderingDirection::DESCENDING == order_by.direction); + return Ordering::DESC; + }); + + std::vector> ordered; + ordered.reserve(acc.ApproximateVertexCount()); + for (auto it = iterable.begin(); it != iterable.end(); ++it) { std::vector properties_order_by; properties_order_by.reserve(order_bys.size()); - for (const auto &order_by : order_bys) { - if constexpr (std::is_same_v || - std::is_same_v>) { - properties_order_by.push_back(ComputeExpression(dba, *it, std::nullopt, order_by.expression.expression, - expr::identifier_node_symbol, expr::identifier_edge_symbol)); - } else { - properties_order_by.push_back(ComputeExpression(dba, std::nullopt, *it, order_by.expression.expression, - expr::identifier_node_symbol, expr::identifier_edge_symbol)); - } - } + std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(properties_order_by), + [&dba, &it](const auto &order_by) { + return ComputeExpression(dba, *it, std::nullopt /*e_acc*/, order_by.expression.expression, + expr::identifier_node_symbol, expr::identifier_edge_symbol); + }); + ordered.push_back({std::move(properties_order_by), *it}); } + auto compare_typed_values = TypedValueVectorCompare(ordering); + std::sort(ordered.begin(), ordered.end(), [compare_typed_values](const auto &pair1, const auto &pair2) { + return compare_typed_values(pair1.properties_order_by, pair2.properties_order_by); + }); + return ordered; +} + +template +std::vector> OrderByEdges(Shard::Accessor &acc, DbAccessor &dba, TIterable &iterable, + std::vector &order_bys, + const VertexAccessor &vertex_acc) { + static_assert(std::is_same_v>); // Can be extended if needed + + std::vector ordering; + ordering.reserve(order_bys.size()); + std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(ordering), [](const auto &order_by) { + if (memgraph::msgs::OrderingDirection::ASCENDING == order_by.direction) { + return Ordering::ASC; + } + MG_ASSERT(memgraph::msgs::OrderingDirection::DESCENDING == order_by.direction); + return Ordering::DESC; + }); + + std::vector> ordered; + for (auto it = iterable.begin(); it != iterable.end(); ++it) { + std::vector properties_order_by; + properties_order_by.reserve(order_bys.size()); + std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(properties_order_by), + [&dba, &vertex_acc, &it](const auto &order_by) { + return ComputeExpression(dba, vertex_acc, *it, order_by.expression.expression, + expr::identifier_node_symbol, expr::identifier_edge_symbol); + }); + + ordered.push_back({std::move(properties_order_by), *it}); + } + + auto compare_typed_values = TypedValueVectorCompare(ordering); std::sort(ordered.begin(), ordered.end(), [compare_typed_values](const auto &pair1, const auto &pair2) { return compare_typed_values(pair1.properties_order_by, pair2.properties_order_by); }); diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 5f6cb5959..55b726c79 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -875,7 +875,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { uint64_t sample_counter{0}; auto vertex_iterable = acc.Vertices(view); if (!req.order_bys.empty()) { - const auto ordered = OrderByElements(acc, dba, vertex_iterable, req.order_bys); + const auto ordered = OrderByVertices(acc, dba, vertex_iterable, req.order_bys); // we are traversing Elements auto it = GetStartOrderedElementsIterator(ordered, start_id, View(req.storage_view)); for (; it != ordered.end(); ++it) { @@ -956,9 +956,9 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { } if (!req.order_by.empty()) { - // #NoCommit can we do differently to avoid this? We need OrderByElements but currently - // #NoCommit it returns vector, so this workaround is here to avoid more duplication later - auto sorted_vertices = OrderByElements(acc, dba, vertex_accessors, req.order_by); + // Can we do differently to avoid this? We need OrderByElements but currently it returns vector, so this + // workaround is here to avoid more duplication later + auto sorted_vertices = OrderByVertices(acc, dba, vertex_accessors, req.order_by); vertex_accessors.clear(); std::transform(sorted_vertices.begin(), sorted_vertices.end(), std::back_inserter(vertex_accessors), [](auto &vertex) { return vertex.object_acc; }); @@ -987,8 +987,8 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { } else { auto [in_edge_accessors, out_edge_accessors] = GetEdgesFromVertex(src_vertex_acc, req.direction); - const auto in_ordered_edges = OrderByElements(acc, dba, in_edge_accessors, req.order_by); - const auto out_ordered_edges = OrderByElements(acc, dba, out_edge_accessors, req.order_by); + const auto in_ordered_edges = OrderByEdges(acc, dba, in_edge_accessors, req.order_by, src_vertex_acc); + const auto out_ordered_edges = OrderByEdges(acc, dba, out_edge_accessors, req.order_by, src_vertex_acc); std::vector in_edge_ordered_accessors; std::transform(in_ordered_edges.begin(), in_ordered_edges.end(), std::back_inserter(in_edge_ordered_accessors), From cca4e97bcff287764d8d9646c6f7773f4a9b0c48 Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 15 Nov 2022 13:37:43 +0100 Subject: [PATCH 040/103] Remove un-needed argument from OrderByEdges --- src/storage/v3/request_helper.hpp | 2 +- src/storage/v3/shard_rsm.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 955059f26..4d3018566 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -150,7 +150,7 @@ std::vector> OrderByVertices(Shard::Accessor &acc, DbAcc } template -std::vector> OrderByEdges(Shard::Accessor &acc, DbAccessor &dba, TIterable &iterable, +std::vector> OrderByEdges(DbAccessor &dba, TIterable &iterable, std::vector &order_bys, const VertexAccessor &vertex_acc) { static_assert(std::is_same_v>); // Can be extended if needed diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 55b726c79..2acdf9f3c 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -987,8 +987,8 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { } else { auto [in_edge_accessors, out_edge_accessors] = GetEdgesFromVertex(src_vertex_acc, req.direction); - const auto in_ordered_edges = OrderByEdges(acc, dba, in_edge_accessors, req.order_by, src_vertex_acc); - const auto out_ordered_edges = OrderByEdges(acc, dba, out_edge_accessors, req.order_by, src_vertex_acc); + const auto in_ordered_edges = OrderByEdges(dba, in_edge_accessors, req.order_by, src_vertex_acc); + const auto out_ordered_edges = OrderByEdges(dba, out_edge_accessors, req.order_by, src_vertex_acc); std::vector in_edge_ordered_accessors; std::transform(in_ordered_edges.begin(), in_ordered_edges.end(), std::back_inserter(in_edge_ordered_accessors), From 07032887a4c915623d27d8e07658ac1fbdfc7b29 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 15 Nov 2022 18:24:28 +0100 Subject: [PATCH 041/103] Adapt mg-expr --- src/expr/interpret/eval.hpp | 65 ++++++++++++++------------------ src/query/v2/bindings/eval.hpp | 11 ++++-- src/storage/v3/bindings/eval.hpp | 3 +- 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/src/expr/interpret/eval.hpp b/src/expr/interpret/eval.hpp index 70127c023..d105eeefb 100644 --- a/src/expr/interpret/eval.hpp +++ b/src/expr/interpret/eval.hpp @@ -32,7 +32,7 @@ struct StorageTag {}; struct QueryEngineTag {}; template + typename PropertyValue, typename ConvFunctor, typename TError, typename Tag = StorageTag> class ExpressionEvaluator : public ExpressionVisitor { public: ExpressionEvaluator(Frame *frame, const SymbolTable &symbol_table, const EvaluationContext &ctx, @@ -100,6 +100,27 @@ class ExpressionEvaluator : public ExpressionVisitor { #undef BINARY_OPERATOR_VISITOR #undef UNARY_OPERATOR_VISITOR + void HandleShardError(TError &shard_error, const std::string_view accessed_object) { + switch (shard_error) { + case TError::DELETED_OBJECT: + throw ExpressionRuntimeException("Trying to access {} on a deleted object.", accessed_object); + case TError::NONEXISTENT_OBJECT: + throw ExpressionRuntimeException("Trying to access {} from a node object doesn't exist.", accessed_object); + case TError::SERIALIZATION_ERROR: + case TError::VERTEX_HAS_EDGES: + case TError::PROPERTIES_DISABLED: + case TError::VERTEX_ALREADY_INSERTED: + throw ExpressionRuntimeException("Unexpected error when accessing {}.", accessed_object); + case TError::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: + case TError::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: + case TError::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: + case TError::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: + case TError::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: + case TError::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: + throw ExpressionRuntimeException("Unexpected schema violation when accessing {}.", accessed_object); + } + } + TypedValue Visit(AndOperator &op) override { auto value1 = op.expression1_->Accept(*this); if (value1.IsBool() && !value1.ValueBool()) { @@ -385,7 +406,7 @@ class ExpressionEvaluator : public ExpressionVisitor { typename TReturnType = std::enable_if_t, bool>> TReturnType HasLabelImpl(const VertexAccessor &vertex, const LabelIx &label, StorageTag /*tag*/) { auto has_label = vertex.HasLabel(view_, GetLabel(label)); - if (has_label.HasError() && has_label.GetError() == Error::NONEXISTENT_OBJECT) { + if (has_label.HasError() && has_label.GetError().code == TError::NONEXISTENT_OBJECT) { // This is a very nasty and temporary hack in order to make MERGE // work. The old storage had the following logic when returning an // `OLD` view: `return old ? old : new`. That means that if the @@ -396,17 +417,7 @@ class ExpressionEvaluator : public ExpressionVisitor { has_label = vertex.HasLabel(StorageView::NEW, GetLabel(label)); } if (has_label.HasError()) { - switch (has_label.GetError()) { - case Error::DELETED_OBJECT: - throw ExpressionRuntimeException("Trying to access labels on a deleted node."); - case Error::NONEXISTENT_OBJECT: - throw ExpressionRuntimeException("Trying to access labels from a node that doesn't exist."); - case Error::SERIALIZATION_ERROR: - case Error::VERTEX_HAS_EDGES: - case Error::PROPERTIES_DISABLED: - case Error::VERTEX_ALREADY_INSERTED: - throw ExpressionRuntimeException("Unexpected error when accessing labels."); - } + HandleShardError(has_label.GetError().code, "labels"); } return *has_label; } @@ -734,7 +745,7 @@ class ExpressionEvaluator : public ExpressionVisitor { class TReturnType = std::enable_if_t, TypedValue>> TypedValue GetProperty(const TRecordAccessor &record_accessor, PropertyIx prop) { auto maybe_prop = record_accessor.GetProperty(view_, ctx_->properties[prop.ix]); - if (maybe_prop.HasError() && maybe_prop.GetError() == Error::NONEXISTENT_OBJECT) { + if (maybe_prop.HasError() && maybe_prop.GetError().code == TError::NONEXISTENT_OBJECT) { // This is a very nasty and temporary hack in order to make MERGE work. // The old storage had the following logic when returning an `OLD` view: // `return old ? old : new`. That means that if the `OLD` view didn't @@ -744,17 +755,7 @@ class ExpressionEvaluator : public ExpressionVisitor { maybe_prop = record_accessor.GetProperty(StorageView::NEW, ctx_->properties[prop.ix]); } if (maybe_prop.HasError()) { - switch (maybe_prop.GetError()) { - case Error::DELETED_OBJECT: - throw ExpressionRuntimeException("Trying to get a property from a deleted object."); - case Error::NONEXISTENT_OBJECT: - throw ExpressionRuntimeException("Trying to get a property from an object that doesn't exist."); - case Error::SERIALIZATION_ERROR: - case Error::VERTEX_HAS_EDGES: - case Error::PROPERTIES_DISABLED: - case Error::VERTEX_ALREADY_INSERTED: - throw ExpressionRuntimeException("Unexpected error when getting a property."); - } + HandleShardError(maybe_prop.GetError().code, "property"); } return conv_(*maybe_prop, ctx_->memory); } @@ -763,7 +764,7 @@ class ExpressionEvaluator : public ExpressionVisitor { class TReturnType = std::enable_if_t, TypedValue>> TypedValue GetProperty(const TRecordAccessor &record_accessor, const std::string_view name) { auto maybe_prop = record_accessor.GetProperty(view_, dba_->NameToProperty(name)); - if (maybe_prop.HasError() && maybe_prop.GetError() == Error::NONEXISTENT_OBJECT) { + if (maybe_prop.HasError() && maybe_prop.GetError().code == TError::NONEXISTENT_OBJECT) { // This is a very nasty and temporary hack in order to make MERGE work. // The old storage had the following logic when returning an `OLD` view: // `return old ? old : new`. That means that if the `OLD` view didn't @@ -773,17 +774,7 @@ class ExpressionEvaluator : public ExpressionVisitor { maybe_prop = record_accessor.GetProperty(view_, dba_->NameToProperty(name)); } if (maybe_prop.HasError()) { - switch (maybe_prop.GetError()) { - case Error::DELETED_OBJECT: - throw ExpressionRuntimeException("Trying to get a property from a deleted object."); - case Error::NONEXISTENT_OBJECT: - throw ExpressionRuntimeException("Trying to get a property from an object that doesn't exist."); - case Error::SERIALIZATION_ERROR: - case Error::VERTEX_HAS_EDGES: - case Error::PROPERTIES_DISABLED: - case Error::VERTEX_ALREADY_INSERTED: - throw ExpressionRuntimeException("Unexpected error when getting a property."); - } + HandleShardError(maybe_prop.GetError().code, "property"); } return conv_(*maybe_prop, ctx_->memory); } diff --git a/src/query/v2/bindings/eval.hpp b/src/query/v2/bindings/eval.hpp index 52455bdb2..78aca436f 100644 --- a/src/query/v2/bindings/eval.hpp +++ b/src/query/v2/bindings/eval.hpp @@ -21,11 +21,12 @@ #include "query/v2/requests.hpp" #include "storage/v3/conversions.hpp" #include "storage/v3/property_value.hpp" +#include "storage/v3/result.hpp" #include "storage/v3/view.hpp" namespace memgraph::msgs { class ShardRequestManagerInterface; -} // namespace memgraph::msgs +} // namespace memgraph::msgs namespace memgraph::query::v2 { @@ -42,8 +43,10 @@ class Callable { }; } // namespace detail -using ExpressionEvaluator = memgraph::expr::ExpressionEvaluator< - TypedValue, memgraph::query::v2::EvaluationContext, memgraph::msgs::ShardRequestManagerInterface, storage::v3::View, - storage::v3::LabelId, msgs::Value, detail::Callable, memgraph::storage::v3::Error, memgraph::expr::QueryEngineTag>; +using ExpressionEvaluator = + memgraph::expr::ExpressionEvaluator; } // namespace memgraph::query::v2 diff --git a/src/storage/v3/bindings/eval.hpp b/src/storage/v3/bindings/eval.hpp index 7f93bb79c..063705806 100644 --- a/src/storage/v3/bindings/eval.hpp +++ b/src/storage/v3/bindings/eval.hpp @@ -21,6 +21,7 @@ #include "storage/v3/id_types.hpp" #include "storage/v3/property_store.hpp" #include "storage/v3/property_value.hpp" +#include "storage/v3/result.hpp" #include "storage/v3/view.hpp" #include "utils/memory.hpp" @@ -87,6 +88,6 @@ struct EvaluationContext { using ExpressionEvaluator = memgraph::expr::ExpressionEvaluator; + memgraph::storage::v3::ErrorCode>; } // namespace memgraph::storage::v3 From 3d66bbd988c9c11e3cab8e5b65fa0d62bcec63ba Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 15 Nov 2022 18:24:40 +0100 Subject: [PATCH 042/103] Remove shard_operation_result --- src/storage/v3/shard_operation_result.hpp | 26 ----------------------- 1 file changed, 26 deletions(-) delete mode 100644 src/storage/v3/shard_operation_result.hpp diff --git a/src/storage/v3/shard_operation_result.hpp b/src/storage/v3/shard_operation_result.hpp deleted file mode 100644 index 8de800b83..000000000 --- a/src/storage/v3/shard_operation_result.hpp +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2022 Memgraph Ltd. -// -// Use of this software is governed by the Business Source License -// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source -// License, and you may not use this file except in compliance with the Business Source License. -// -// As of the Change Date specified in that file, in accordance with -// the Business Source License, use of this software will be governed -// by the Apache License, Version 2.0, included in the file -// licenses/APL.txt. - -#pragma once - -#include - -#include "storage/v3/result.hpp" -#include "storage/v3/schema_validator.hpp" - -namespace memgraph::storage::v3 { - -using ResultErrorType = std::variant; - -template -using ShardOperationResult = utils::BasicResult; - -} // namespace memgraph::storage::v3 From 9c05910e68b2f3dd8bb7fb5f8f5bbb386b566658 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 15 Nov 2022 18:25:12 +0100 Subject: [PATCH 043/103] Adapt storage to new erro handling --- src/storage/v3/bindings/db_accessor.hpp | 10 +-- src/storage/v3/edge_accessor.cpp | 31 +++---- src/storage/v3/edge_accessor.hpp | 10 +-- src/storage/v3/result.hpp | 4 +- src/storage/v3/schema_validator.cpp | 2 +- src/storage/v3/shard.cpp | 42 ++++----- src/storage/v3/shard.hpp | 11 ++- src/storage/v3/shard_rsm.cpp | 98 +++++++------------- src/storage/v3/vertex_accessor.cpp | 113 ++++++++++++------------ src/storage/v3/vertex_accessor.hpp | 48 +++++----- 10 files changed, 170 insertions(+), 199 deletions(-) diff --git a/src/storage/v3/bindings/db_accessor.hpp b/src/storage/v3/bindings/db_accessor.hpp index 186a1b5c5..1a5e846fa 100644 --- a/src/storage/v3/bindings/db_accessor.hpp +++ b/src/storage/v3/bindings/db_accessor.hpp @@ -78,8 +78,8 @@ class DbAccessor final { return VerticesIterable(accessor_->Vertices(label, property, lower, upper, view)); } - storage::v3::Result InsertEdge(VertexAccessor *from, VertexAccessor *to, - const storage::v3::EdgeTypeId &edge_type) { + storage::v3::ShardResult InsertEdge(VertexAccessor *from, VertexAccessor *to, + const storage::v3::EdgeTypeId &edge_type) { static constexpr auto kDummyGid = storage::v3::Gid::FromUint(0); auto maybe_edge = accessor_->CreateEdge(from->Id(storage::v3::View::NEW).GetValue(), to->Id(storage::v3::View::NEW).GetValue(), edge_type, kDummyGid); @@ -87,7 +87,7 @@ class DbAccessor final { return EdgeAccessor(*maybe_edge); } - storage::v3::Result> RemoveEdge(EdgeAccessor *edge) { + storage::v3::ShardResult> RemoveEdge(EdgeAccessor *edge) { auto res = accessor_->DeleteEdge(edge->FromVertex(), edge->ToVertex(), edge->Gid()); if (res.HasError()) { return res.GetError(); @@ -101,7 +101,7 @@ class DbAccessor final { return std::make_optional(*value); } - storage::v3::Result>>> DetachRemoveVertex( + storage::v3::ShardResult>>> DetachRemoveVertex( VertexAccessor *vertex_accessor) { using ReturnType = std::pair>; @@ -125,7 +125,7 @@ class DbAccessor final { return std::make_optional(vertex, std::move(deleted_edges)); } - storage::v3::Result> RemoveVertex(VertexAccessor *vertex_accessor) { + storage::v3::ShardResult> RemoveVertex(VertexAccessor *vertex_accessor) { auto res = accessor_->DeleteVertex(vertex_accessor); if (res.HasError()) { return res.GetError(); diff --git a/src/storage/v3/edge_accessor.cpp b/src/storage/v3/edge_accessor.cpp index ed4abd1fe..95dd6875a 100644 --- a/src/storage/v3/edge_accessor.cpp +++ b/src/storage/v3/edge_accessor.cpp @@ -15,6 +15,7 @@ #include "storage/v3/mvcc.hpp" #include "storage/v3/property_value.hpp" +#include "storage/v3/result.hpp" #include "storage/v3/schema_validator.hpp" #include "storage/v3/vertex_accessor.hpp" #include "utils/memory_tracker.hpp" @@ -54,13 +55,13 @@ const VertexId &EdgeAccessor::FromVertex() const { return from_vertex_; } const VertexId &EdgeAccessor::ToVertex() const { return to_vertex_; } -Result EdgeAccessor::SetProperty(PropertyId property, const PropertyValue &value) { +ShardResult EdgeAccessor::SetProperty(PropertyId property, const PropertyValue &value) { utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!config_.properties_on_edges) return Error::PROPERTIES_DISABLED; + if (!config_.properties_on_edges) return SHARD_ERROR(ErrorCode::PROPERTIES_DISABLED); - if (!PrepareForWrite(transaction_, edge_.ptr)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, edge_.ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (edge_.ptr->deleted) return Error::DELETED_OBJECT; + if (edge_.ptr->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto current_value = edge_.ptr->properties.GetProperty(property); // We could skip setting the value if the previous one is the same to the new @@ -75,12 +76,12 @@ Result EdgeAccessor::SetProperty(PropertyId property, const Prope return std::move(current_value); } -Result> EdgeAccessor::ClearProperties() { - if (!config_.properties_on_edges) return Error::PROPERTIES_DISABLED; +ShardResult> EdgeAccessor::ClearProperties() { + if (!config_.properties_on_edges) return SHARD_ERROR(ErrorCode::PROPERTIES_DISABLED); - if (!PrepareForWrite(transaction_, edge_.ptr)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, edge_.ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (edge_.ptr->deleted) return Error::DELETED_OBJECT; + if (edge_.ptr->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto properties = edge_.ptr->properties.Properties(); for (const auto &property : properties) { @@ -92,11 +93,11 @@ Result> EdgeAccessor::ClearProperties() { return std::move(properties); } -Result EdgeAccessor::GetProperty(View view, PropertyId property) const { +ShardResult EdgeAccessor::GetProperty(View view, PropertyId property) const { return GetProperty(property, view); } -Result EdgeAccessor::GetProperty(PropertyId property, View view) const { +ShardResult EdgeAccessor::GetProperty(PropertyId property, View view) const { if (!config_.properties_on_edges) return PropertyValue(); auto exists = true; auto deleted = edge_.ptr->deleted; @@ -128,12 +129,12 @@ Result EdgeAccessor::GetProperty(PropertyId property, View view) break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(value); } -Result> EdgeAccessor::Properties(View view) const { +ShardResult> EdgeAccessor::Properties(View view) const { if (!config_.properties_on_edges) return std::map{}; auto exists = true; auto deleted = edge_.ptr->deleted; @@ -174,8 +175,8 @@ Result> EdgeAccessor::Properties(View view) break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(properties); } diff --git a/src/storage/v3/edge_accessor.hpp b/src/storage/v3/edge_accessor.hpp index 546a09200..105d28fa4 100644 --- a/src/storage/v3/edge_accessor.hpp +++ b/src/storage/v3/edge_accessor.hpp @@ -56,19 +56,19 @@ class EdgeAccessor final { /// Set a property value and return the old value. /// @throw std::bad_alloc - Result SetProperty(PropertyId property, const PropertyValue &value); + ShardResult SetProperty(PropertyId property, const PropertyValue &value); /// Remove all properties and return old values for each removed property. /// @throw std::bad_alloc - Result> ClearProperties(); + ShardResult> ClearProperties(); /// @throw std::bad_alloc - Result GetProperty(PropertyId property, View view) const; + ShardResult GetProperty(PropertyId property, View view) const; - Result GetProperty(View view, PropertyId property) const; + ShardResult GetProperty(View view, PropertyId property) const; /// @throw std::bad_alloc - Result> Properties(View view) const; + ShardResult> Properties(View view) const; Gid Gid() const noexcept { if (config_.properties_on_edges) { diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index ecec4ad1e..b13677f5d 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -40,6 +40,8 @@ struct ShardError { ShardError(ErrorCode code, std::string message, std::string source) : code{code}, message{std::move(message)}, source{std::move(source)} {} + ShardError(ErrorCode code, std::string source) : code{code}, source{std::move(source)} {} + ErrorCode code; // TODO Maybe add category std::string message; @@ -49,6 +51,6 @@ struct ShardError { #define SHARD_ERROR(...) memgraph::storage::v3::ShardError(__VA_ARGS__, fmt::format("{}:{}", __FILE__, __LINE__)) template -using Result = utils::BasicResult; +using ShardResult = utils::BasicResult; } // namespace memgraph::storage::v3 diff --git a/src/storage/v3/schema_validator.cpp b/src/storage/v3/schema_validator.cpp index 48fd95eba..64df35a7e 100644 --- a/src/storage/v3/schema_validator.cpp +++ b/src/storage/v3/schema_validator.cpp @@ -94,7 +94,7 @@ std::optional SchemaValidator::ValidateLabelUpdate(const LabelId lab return std::nullopt; } -const Schemas::Schema *SchemaValidator::GetSchema(LabelId label) const { return schemas_.GetSchema(label); } +const Schemas::Schema *SchemaValidator::GetSchema(LabelId label) const { return schemas_->GetSchema(label); } VertexValidator::VertexValidator(const SchemaValidator &schema_validator, const LabelId primary_label) : schema_validator{&schema_validator}, primary_label_{primary_label} {} diff --git a/src/storage/v3/shard.cpp b/src/storage/v3/shard.cpp index e4d993ffa..0fafe4bf6 100644 --- a/src/storage/v3/shard.cpp +++ b/src/storage/v3/shard.cpp @@ -33,8 +33,8 @@ #include "storage/v3/mvcc.hpp" #include "storage/v3/name_id_mapper.hpp" #include "storage/v3/property_value.hpp" +#include "storage/v3/result.hpp" #include "storage/v3/schema_validator.hpp" -#include "storage/v3/shard_operation_result.hpp" #include "storage/v3/transaction.hpp" #include "storage/v3/vertex.hpp" #include "storage/v3/vertex_accessor.hpp" @@ -345,7 +345,7 @@ Shard::~Shard() {} Shard::Accessor::Accessor(Shard &shard, Transaction &transaction) : shard_(&shard), transaction_(&transaction), config_(shard_->config_.items) {} -ShardOperationResult Shard::Accessor::CreateVertexAndValidate( +ShardResult Shard::Accessor::CreateVertexAndValidate( const std::vector &labels, const std::vector &primary_properties, const std::vector> &properties) { OOMExceptionEnabler oom_exception; @@ -364,7 +364,7 @@ ShardOperationResult Shard::Accessor::CreateVertexAndValidate( VertexAccessor vertex_acc{&it->vertex, transaction_, &shard_->indices_, config_, shard_->vertex_validator_}; if (!inserted) { - return {Error::VERTEX_ALREADY_INSERTED}; + return SHARD_ERROR(ErrorCode::VERTEX_ALREADY_INSERTED); } MG_ASSERT(it != acc.end(), "Invalid Vertex accessor!"); @@ -395,19 +395,19 @@ std::optional Shard::Accessor::FindVertex(std::vectorvertex, transaction_, &shard_->indices_, config_, shard_->vertex_validator_, view); } -Result> Shard::Accessor::DeleteVertex(VertexAccessor *vertex) { +ShardResult> Shard::Accessor::DeleteVertex(VertexAccessor *vertex) { MG_ASSERT(vertex->transaction_ == transaction_, "VertexAccessor must be from the same transaction as the storage " "accessor when deleting a vertex!"); auto *vertex_ptr = vertex->vertex_; - if (!PrepareForWrite(transaction_, vertex_ptr)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); if (vertex_ptr->deleted) { return std::optional{}; } - if (!vertex_ptr->in_edges.empty() || !vertex_ptr->out_edges.empty()) return Error::VERTEX_HAS_EDGES; + if (!vertex_ptr->in_edges.empty() || !vertex_ptr->out_edges.empty()) return SHARD_ERROR(ErrorCode::VERTEX_HAS_EDGES); CreateAndLinkDelta(transaction_, vertex_ptr, Delta::RecreateObjectTag()); vertex_ptr->deleted = true; @@ -416,7 +416,7 @@ Result> Shard::Accessor::DeleteVertex(VertexAccess shard_->vertex_validator_, true); } -Result>>> Shard::Accessor::DetachDeleteVertex( +ShardResult>>> Shard::Accessor::DetachDeleteVertex( VertexAccessor *vertex) { using ReturnType = std::pair>; @@ -429,7 +429,7 @@ Result>>> Shar std::vector out_edges; { - if (!PrepareForWrite(transaction_, vertex_ptr)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); if (vertex_ptr->deleted) return std::optional{}; @@ -444,7 +444,7 @@ Result>>> Shar EdgeAccessor e(edge, edge_type, from_vertex, vertex_id, transaction_, &shard_->indices_, config_); auto ret = DeleteEdge(e.FromVertex(), e.ToVertex(), e.Gid()); if (ret.HasError()) { - MG_ASSERT(ret.GetError() == Error::SERIALIZATION_ERROR, "Invalid database state!"); + MG_ASSERT(ret.GetError().code == ErrorCode::SERIALIZATION_ERROR, "Invalid database state!"); return ret.GetError(); } @@ -457,7 +457,7 @@ Result>>> Shar EdgeAccessor e(edge, edge_type, vertex_id, to_vertex, transaction_, &shard_->indices_, config_); auto ret = DeleteEdge(e.FromVertex(), e.ToVertex(), e.Gid()); if (ret.HasError()) { - MG_ASSERT(ret.GetError() == Error::SERIALIZATION_ERROR, "Invalid database state!"); + MG_ASSERT(ret.GetError().code == ErrorCode::SERIALIZATION_ERROR, "Invalid database state!"); return ret.GetError(); } @@ -470,7 +470,7 @@ Result>>> Shar // vertex. Some other transaction could have modified the vertex in the // meantime if we didn't have any edges to delete. - if (!PrepareForWrite(transaction_, vertex_ptr)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); MG_ASSERT(!vertex_ptr->deleted, "Invalid database state!"); @@ -482,8 +482,8 @@ Result>>> Shar std::move(deleted_edges)); } -Result Shard::Accessor::CreateEdge(VertexId from_vertex_id, VertexId to_vertex_id, - const EdgeTypeId edge_type, const Gid gid) { +ShardResult Shard::Accessor::CreateEdge(VertexId from_vertex_id, VertexId to_vertex_id, + const EdgeTypeId edge_type, const Gid gid) { OOMExceptionEnabler oom_exception; Vertex *from_vertex{nullptr}; Vertex *to_vertex{nullptr}; @@ -507,12 +507,12 @@ Result Shard::Accessor::CreateEdge(VertexId from_vertex_id, Vertex } if (from_is_local) { - if (!PrepareForWrite(transaction_, from_vertex)) return Error::SERIALIZATION_ERROR; - if (from_vertex->deleted) return Error::DELETED_OBJECT; + if (!PrepareForWrite(transaction_, from_vertex)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (from_vertex->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); } if (to_is_local && to_vertex != from_vertex) { - if (!PrepareForWrite(transaction_, to_vertex)) return Error::SERIALIZATION_ERROR; - if (to_vertex->deleted) return Error::DELETED_OBJECT; + if (!PrepareForWrite(transaction_, to_vertex)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (to_vertex->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); } EdgeRef edge(gid); @@ -541,8 +541,8 @@ Result Shard::Accessor::CreateEdge(VertexId from_vertex_id, Vertex &shard_->indices_, config_); } -Result> Shard::Accessor::DeleteEdge(VertexId from_vertex_id, VertexId to_vertex_id, - const Gid edge_id) { +ShardResult> Shard::Accessor::DeleteEdge(VertexId from_vertex_id, VertexId to_vertex_id, + const Gid edge_id) { Vertex *from_vertex{nullptr}; Vertex *to_vertex{nullptr}; @@ -567,13 +567,13 @@ Result> Shard::Accessor::DeleteEdge(VertexId from_ve if (from_is_local) { if (!PrepareForWrite(transaction_, from_vertex)) { - return Error::SERIALIZATION_ERROR; + return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); } MG_ASSERT(!from_vertex->deleted, "Invalid database state!"); } if (to_is_local && to_vertex != from_vertex) { if (!PrepareForWrite(transaction_, to_vertex)) { - return Error::SERIALIZATION_ERROR; + return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); } MG_ASSERT(!to_vertex->deleted, "Invalid database state!"); } diff --git a/src/storage/v3/shard.hpp b/src/storage/v3/shard.hpp index 778c92abd..5c025ae57 100644 --- a/src/storage/v3/shard.hpp +++ b/src/storage/v3/shard.hpp @@ -38,7 +38,6 @@ #include "storage/v3/result.hpp" #include "storage/v3/schema_validator.hpp" #include "storage/v3/schemas.hpp" -#include "storage/v3/shard_operation_result.hpp" #include "storage/v3/transaction.hpp" #include "storage/v3/vertex.hpp" #include "storage/v3/vertex_accessor.hpp" @@ -207,7 +206,7 @@ class Shard final { public: /// @throw std::bad_alloc - ShardOperationResult CreateVertexAndValidate( + ShardResult CreateVertexAndValidate( const std::vector &labels, const std::vector &primary_properties, const std::vector> &properties); @@ -262,19 +261,19 @@ class Shard final { /// @return Accessor to the deleted vertex if a deletion took place, std::nullopt otherwise /// @throw std::bad_alloc - Result> DeleteVertex(VertexAccessor *vertex); + ShardResult> DeleteVertex(VertexAccessor *vertex); /// @return Accessor to the deleted vertex and deleted edges if a deletion took place, std::nullopt otherwise /// @throw std::bad_alloc - Result>>> DetachDeleteVertex( + ShardResult>>> DetachDeleteVertex( VertexAccessor *vertex); /// @throw std::bad_alloc - Result CreateEdge(VertexId from_vertex_id, VertexId to_vertex_id, EdgeTypeId edge_type, Gid gid); + ShardResult CreateEdge(VertexId from_vertex_id, VertexId to_vertex_id, EdgeTypeId edge_type, Gid gid); /// Accessor to the deleted edge if a deletion took place, std::nullopt otherwise /// @throw std::bad_alloc - Result> DeleteEdge(VertexId from_vertex_id, VertexId to_vertex_id, Gid edge_id); + ShardResult> DeleteEdge(VertexId from_vertex_id, VertexId to_vertex_id, Gid edge_id); LabelId NameToLabel(std::string_view name) const; diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index f288e5e27..f3d879ef4 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -34,6 +34,7 @@ #include "storage/v3/key_store.hpp" #include "storage/v3/property_value.hpp" #include "storage/v3/request_helper.hpp" +#include "storage/v3/result.hpp" #include "storage/v3/schemas.hpp" #include "storage/v3/shard.hpp" #include "storage/v3/shard_rsm.hpp" @@ -468,6 +469,37 @@ EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { return edge_filler; } +void LogResultError(const ShardError &error, const std::string_view action = "") { + switch (error.code) { + case ErrorCode::DELETED_OBJECT: + spdlog::debug("{} failed with error: DELETED_OBJECT, at {}", action, error.source); + break; + case ErrorCode::NONEXISTENT_OBJECT: + spdlog::debug("{} failed with error: NONEXISTENT_OBJECT, at {}", action, error.source); + break; + case ErrorCode::SERIALIZATION_ERROR: + spdlog::debug("{} failed with error: SERIALIZATION_ERROR, at {}", action, error.source); + break; + case ErrorCode::PROPERTIES_DISABLED: + spdlog::debug("{} failed with error: PROPERTIES_DISABLED, at {}", action, error.source); + break; + case ErrorCode::VERTEX_HAS_EDGES: + spdlog::debug("{} failed with error: VERTEX_HAS_EDGES, at {}", action, error.source); + break; + case ErrorCode::VERTEX_ALREADY_INSERTED: + spdlog::debug("{} failed with error: VERTEX_ALREADY_INSERTED, at {}", action, error.source); + break; + case ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: + case ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: + case ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: + case ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: + case ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: + case ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: + spdlog::debug("Schema violation: {} at {}", error.message, error.source); + break; + } +} + }; // namespace msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); @@ -494,38 +526,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { if (result_schema.HasError()) { auto &error = result_schema.GetError(); - - std::visit( - [](T &&error) { - using ErrorType = std::remove_cvref_t; - if constexpr (std::is_same_v) { - spdlog::debug("Creating vertex failed with error: SchemaViolation"); - } else if constexpr (std::is_same_v) { - switch (error) { - case Error::DELETED_OBJECT: - spdlog::debug("Creating vertex failed with error: DELETED_OBJECT"); - break; - case Error::NONEXISTENT_OBJECT: - spdlog::debug("Creating vertex failed with error: NONEXISTENT_OBJECT"); - break; - case Error::SERIALIZATION_ERROR: - spdlog::debug("Creating vertex failed with error: SERIALIZATION_ERROR"); - break; - case Error::PROPERTIES_DISABLED: - spdlog::debug("Creating vertex failed with error: PROPERTIES_DISABLED"); - break; - case Error::VERTEX_HAS_EDGES: - spdlog::debug("Creating vertex failed with error: VERTEX_HAS_EDGES"); - break; - case Error::VERTEX_ALREADY_INSERTED: - spdlog::debug("Creating vertex failed with error: VERTEX_ALREADY_INSERTED"); - break; - } - } else { - static_assert(kAlwaysFalse, "Missing type from variant visitor"); - } - }, - error); + spdlog::debug("Creating vertex failed with error: VERTEX_ALREADY_INSERTED"); action_successful = false; break; @@ -558,38 +559,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { vertex_to_update->SetPropertyAndValidate(update_prop.first, ToPropertyValue(std::move(update_prop.second))); if (result_schema.HasError()) { auto &error = result_schema.GetError(); - - std::visit( - [](T &&error) { - using ErrorType = std::remove_cvref_t; - if constexpr (std::is_same_v) { - spdlog::debug("Updating vertex failed with error: SchemaViolation"); - } else if constexpr (std::is_same_v) { - switch (error) { - case Error::DELETED_OBJECT: - spdlog::debug("Updating vertex failed with error: DELETED_OBJECT"); - break; - case Error::NONEXISTENT_OBJECT: - spdlog::debug("Updating vertex failed with error: NONEXISTENT_OBJECT"); - break; - case Error::SERIALIZATION_ERROR: - spdlog::debug("Updating vertex failed with error: SERIALIZATION_ERROR"); - break; - case Error::PROPERTIES_DISABLED: - spdlog::debug("Updating vertex failed with error: PROPERTIES_DISABLED"); - break; - case Error::VERTEX_HAS_EDGES: - spdlog::debug("Updating vertex failed with error: VERTEX_HAS_EDGES"); - break; - case Error::VERTEX_ALREADY_INSERTED: - spdlog::debug("Updating vertex failed with error: VERTEX_ALREADY_INSERTED"); - break; - } - } else { - static_assert(kAlwaysFalse, "Missing type from variant visitor"); - } - }, - error); + LogResultError(error); action_successful = false; diff --git a/src/storage/v3/vertex_accessor.cpp b/src/storage/v3/vertex_accessor.cpp index 543caa88a..6949c08cc 100644 --- a/src/storage/v3/vertex_accessor.cpp +++ b/src/storage/v3/vertex_accessor.cpp @@ -21,8 +21,8 @@ #include "storage/v3/key_store.hpp" #include "storage/v3/mvcc.hpp" #include "storage/v3/property_value.hpp" +#include "storage/v3/result.hpp" #include "storage/v3/shard.hpp" -#include "storage/v3/shard_operation_result.hpp" #include "storage/v3/vertex.hpp" #include "utils/logging.hpp" #include "utils/memory_tracker.hpp" @@ -80,12 +80,12 @@ bool VertexAccessor::IsVisible(View view) const { return exists && (for_deleted_ || !deleted); } -Result VertexAccessor::AddLabel(LabelId label) { +ShardResult VertexAccessor::AddLabel(LabelId label) { utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!PrepareForWrite(transaction_, vertex_)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return Error::DELETED_OBJECT; + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); if (std::find(vertex_->labels.begin(), vertex_->labels.end(), label) != vertex_->labels.end()) return false; @@ -98,15 +98,15 @@ Result VertexAccessor::AddLabel(LabelId label) { return true; } -ShardOperationResult VertexAccessor::AddLabelAndValidate(LabelId label) { +ShardResult VertexAccessor::AddLabelAndValidate(LabelId label) { if (const auto maybe_violation_error = vertex_validator_->ValidateAddLabel(label); maybe_violation_error) { return {*maybe_violation_error}; } utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!PrepareForWrite(transaction_, vertex_)) return {Error::SERIALIZATION_ERROR}; + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return {Error::DELETED_OBJECT}; + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); if (std::find(vertex_->labels.begin(), vertex_->labels.end(), label) != vertex_->labels.end()) return false; @@ -119,10 +119,10 @@ ShardOperationResult VertexAccessor::AddLabelAndValidate(LabelId label) { return true; } -Result VertexAccessor::RemoveLabel(LabelId label) { - if (!PrepareForWrite(transaction_, vertex_)) return Error::SERIALIZATION_ERROR; +ShardResult VertexAccessor::RemoveLabel(LabelId label) { + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return Error::DELETED_OBJECT; + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto it = std::find(vertex_->labels.begin(), vertex_->labels.end(), label); if (it == vertex_->labels.end()) return false; @@ -134,14 +134,14 @@ Result VertexAccessor::RemoveLabel(LabelId label) { return true; } -ShardOperationResult VertexAccessor::RemoveLabelAndValidate(LabelId label) { +ShardResult VertexAccessor::RemoveLabelAndValidate(LabelId label) { if (const auto maybe_violation_error = vertex_validator_->ValidateRemoveLabel(label); maybe_violation_error) { return {*maybe_violation_error}; } - if (!PrepareForWrite(transaction_, vertex_)) return {Error::SERIALIZATION_ERROR}; + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return {Error::DELETED_OBJECT}; + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto it = std::find(vertex_->labels.begin(), vertex_->labels.end(), label); if (it == vertex_->labels.end()) return false; @@ -153,9 +153,9 @@ ShardOperationResult VertexAccessor::RemoveLabelAndValidate(LabelId label) return true; } -Result VertexAccessor::HasLabel(View view, LabelId label) const { return HasLabel(label, view); } +ShardResult VertexAccessor::HasLabel(View view, LabelId label) const { return HasLabel(label, view); } -Result VertexAccessor::HasLabel(LabelId label, View view) const { +ShardResult VertexAccessor::HasLabel(LabelId label, View view) const { bool exists = true; bool deleted = false; bool has_label = false; @@ -197,12 +197,12 @@ Result VertexAccessor::HasLabel(LabelId label, View view) const { break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return has_label; } -Result VertexAccessor::PrimaryLabel(const View view) const { +ShardResult VertexAccessor::PrimaryLabel(const View view) const { if (const auto result = CheckVertexExistence(view); result.HasError()) { return result.GetError(); } @@ -210,21 +210,21 @@ Result VertexAccessor::PrimaryLabel(const View view) const { return vertex_validator_->primary_label_; } -Result VertexAccessor::PrimaryKey(const View view) const { +ShardResult VertexAccessor::PrimaryKey(const View view) const { if (const auto result = CheckVertexExistence(view); result.HasError()) { return result.GetError(); } return vertex_->keys.Keys(); } -Result VertexAccessor::Id(View view) const { +ShardResult VertexAccessor::Id(View view) const { if (const auto result = CheckVertexExistence(view); result.HasError()) { return result.GetError(); } return VertexId{vertex_validator_->primary_label_, vertex_->keys.Keys()}; }; -Result> VertexAccessor::Labels(View view) const { +ShardResult> VertexAccessor::Labels(View view) const { bool exists = true; bool deleted = false; std::vector labels; @@ -267,17 +267,17 @@ Result> VertexAccessor::Labels(View view) const { break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(labels); } -Result VertexAccessor::SetProperty(PropertyId property, const PropertyValue &value) { +ShardResult VertexAccessor::SetProperty(PropertyId property, const PropertyValue &value) { utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!PrepareForWrite(transaction_, vertex_)) return Error::SERIALIZATION_ERROR; + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return Error::DELETED_OBJECT; + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto current_value = vertex_->properties.GetProperty(property); // We could skip setting the value if the previous one is the same to the new @@ -294,7 +294,7 @@ Result VertexAccessor::SetProperty(PropertyId property, const Pro return std::move(current_value); } -Result VertexAccessor::CheckVertexExistence(View view) const { +ShardResult VertexAccessor::CheckVertexExistence(View view) const { bool exists = true; bool deleted = false; Delta *delta = nullptr; @@ -323,27 +323,26 @@ Result VertexAccessor::CheckVertexExistence(View view) const { } }); if (!exists) { - return Error::NONEXISTENT_OBJECT; + return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); } if (!for_deleted_ && deleted) { - return Error::DELETED_OBJECT; + return SHARD_ERROR(ErrorCode::DELETED_OBJECT); } return {}; } -ShardOperationResult VertexAccessor::SetPropertyAndValidate(PropertyId property, - const PropertyValue &value) { +ShardResult VertexAccessor::SetPropertyAndValidate(PropertyId property, const PropertyValue &value) { if (auto maybe_violation_error = vertex_validator_->ValidatePropertyUpdate(property); maybe_violation_error) { return {*maybe_violation_error}; } utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; if (!PrepareForWrite(transaction_, vertex_)) { - return {Error::SERIALIZATION_ERROR}; + return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); } if (vertex_->deleted) { - return {Error::DELETED_OBJECT}; + return SHARD_ERROR(ErrorCode::DELETED_OBJECT); } auto current_value = vertex_->properties.GetProperty(property); @@ -361,10 +360,10 @@ ShardOperationResult VertexAccessor::SetPropertyAndValidate(Prope return std::move(current_value); } -Result> VertexAccessor::ClearProperties() { - if (!PrepareForWrite(transaction_, vertex_)) return Error::SERIALIZATION_ERROR; +ShardResult> VertexAccessor::ClearProperties() { + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return Error::DELETED_OBJECT; + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto properties = vertex_->properties.Properties(); for (const auto &property : properties) { @@ -377,7 +376,7 @@ Result> VertexAccessor::ClearProperties() { return std::move(properties); } -Result VertexAccessor::GetProperty(View view, PropertyId property) const { +ShardResult VertexAccessor::GetProperty(View view, PropertyId property) const { return GetProperty(property, view).GetValue(); } @@ -407,7 +406,7 @@ PropertyValue VertexAccessor::GetPropertyValue(PropertyId property, View view) c return value; } -Result VertexAccessor::GetProperty(PropertyId property, View view) const { +ShardResult VertexAccessor::GetProperty(PropertyId property, View view) const { bool exists = true; bool deleted = false; PropertyValue value; @@ -442,12 +441,12 @@ Result VertexAccessor::GetProperty(PropertyId property, View view break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(value); } -Result> VertexAccessor::Properties(View view) const { +ShardResult> VertexAccessor::Properties(View view) const { bool exists = true; bool deleted = false; std::map properties; @@ -492,13 +491,13 @@ Result> VertexAccessor::Properties(View view break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(properties); } -Result> VertexAccessor::InEdges(View view, const std::vector &edge_types, - const VertexId *destination_id) const { +ShardResult> VertexAccessor::InEdges(View view, const std::vector &edge_types, + const VertexId *destination_id) const { bool exists = true; bool deleted = false; std::vector in_edges; @@ -564,8 +563,8 @@ Result> VertexAccessor::InEdges(View view, const std:: break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); std::vector ret; if (in_edges.empty()) { return ret; @@ -579,8 +578,8 @@ Result> VertexAccessor::InEdges(View view, const std:: return ret; } -Result> VertexAccessor::OutEdges(View view, const std::vector &edge_types, - const VertexId *destination_id) const { +ShardResult> VertexAccessor::OutEdges(View view, const std::vector &edge_types, + const VertexId *destination_id) const { bool exists = true; bool deleted = false; std::vector out_edges; @@ -644,8 +643,8 @@ Result> VertexAccessor::OutEdges(View view, const std: break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); std::vector ret; if (out_edges.empty()) { return ret; @@ -659,7 +658,7 @@ Result> VertexAccessor::OutEdges(View view, const std: return ret; } -Result VertexAccessor::InDegree(View view) const { +ShardResult VertexAccessor::InDegree(View view) const { bool exists = true; bool deleted = false; size_t degree = 0; @@ -691,12 +690,12 @@ Result VertexAccessor::InDegree(View view) const { break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return degree; } -Result VertexAccessor::OutDegree(View view) const { +ShardResult VertexAccessor::OutDegree(View view) const { bool exists = true; bool deleted = false; size_t degree = 0; @@ -728,8 +727,8 @@ Result VertexAccessor::OutDegree(View view) const { break; } }); - if (!exists) return Error::NONEXISTENT_OBJECT; - if (!for_deleted_ && deleted) return Error::DELETED_OBJECT; + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return degree; } diff --git a/src/storage/v3/vertex_accessor.hpp b/src/storage/v3/vertex_accessor.hpp index 682a04e39..20ee9955b 100644 --- a/src/storage/v3/vertex_accessor.hpp +++ b/src/storage/v3/vertex_accessor.hpp @@ -17,7 +17,7 @@ #include "storage/v3/id_types.hpp" #include "storage/v3/key_store.hpp" #include "storage/v3/result.hpp" -#include "storage/v3/shard_operation_result.hpp" +#include "storage/v3/schema_validator.hpp" #include "storage/v3/transaction.hpp" #include "storage/v3/vertex.hpp" #include "storage/v3/vertex_id.hpp" @@ -55,61 +55,61 @@ class VertexAccessor final { /// `false` is returned if the label already existed, or SchemaViolation /// if adding the label has violated one of the schema constraints. /// @throw std::bad_alloc - ShardOperationResult AddLabelAndValidate(LabelId label); + ShardResult AddLabelAndValidate(LabelId label); /// Remove a label and return `true` if deletion took place. /// `false` is returned if the vertex did not have a label already. or SchemaViolation /// if adding the label has violated one of the schema constraints. /// @throw std::bad_alloc - ShardOperationResult RemoveLabelAndValidate(LabelId label); + ShardResult RemoveLabelAndValidate(LabelId label); - Result HasLabel(View view, LabelId label) const; + ShardResult HasLabel(View view, LabelId label) const; - Result HasLabel(LabelId label, View view) const; + ShardResult HasLabel(LabelId label, View view) const; /// @throw std::bad_alloc /// @throw std::length_error if the resulting vector exceeds /// std::vector::max_size(). - Result> Labels(View view) const; + ShardResult> Labels(View view) const; - Result PrimaryLabel(View view) const; + ShardResult PrimaryLabel(View view) const; - Result PrimaryKey(View view) const; + ShardResult PrimaryKey(View view) const; - Result Id(View view) const; + ShardResult Id(View view) const; /// Set a property value and return the old value or error. /// @throw std::bad_alloc - ShardOperationResult SetPropertyAndValidate(PropertyId property, const PropertyValue &value); + ShardResult SetPropertyAndValidate(PropertyId property, const PropertyValue &value); /// Remove all properties and return the values of the removed properties. /// @throw std::bad_alloc - Result> ClearProperties(); + ShardResult> ClearProperties(); /// @throw std::bad_alloc - Result GetProperty(PropertyId property, View view) const; + ShardResult GetProperty(PropertyId property, View view) const; // TODO Remove this - Result GetProperty(View view, PropertyId property) const; + ShardResult GetProperty(View view, PropertyId property) const; /// @throw std::bad_alloc - Result> Properties(View view) const; + ShardResult> Properties(View view) const; /// @throw std::bad_alloc /// @throw std::length_error if the resulting vector exceeds /// std::vector::max_size(). - Result> InEdges(View view, const std::vector &edge_types = {}, - const VertexId *destination_id = nullptr) const; + ShardResult> InEdges(View view, const std::vector &edge_types = {}, + const VertexId *destination_id = nullptr) const; /// @throw std::bad_alloc /// @throw std::length_error if the resulting vector exceeds /// std::vector::max_size(). - Result> OutEdges(View view, const std::vector &edge_types = {}, - const VertexId *destination_id = nullptr) const; + ShardResult> OutEdges(View view, const std::vector &edge_types = {}, + const VertexId *destination_id = nullptr) const; - Result InDegree(View view) const; + ShardResult InDegree(View view) const; - Result OutDegree(View view) const; + ShardResult OutDegree(View view) const; const SchemaValidator *GetSchemaValidator() const; @@ -122,20 +122,20 @@ class VertexAccessor final { /// Add a label and return `true` if insertion took place. /// `false` is returned if the label already existed. /// @throw std::bad_alloc - Result AddLabel(LabelId label); + ShardResult AddLabel(LabelId label); /// Remove a label and return `true` if deletion took place. /// `false` is returned if the vertex did not have a label already. /// @throw std::bad_alloc - Result RemoveLabel(LabelId label); + ShardResult RemoveLabel(LabelId label); /// Set a property value and return the old value. /// @throw std::bad_alloc - Result SetProperty(PropertyId property, const PropertyValue &value); + ShardResult SetProperty(PropertyId property, const PropertyValue &value); PropertyValue GetPropertyValue(PropertyId property, View view) const; - Result CheckVertexExistence(View view) const; + ShardResult CheckVertexExistence(View view) const; Vertex *vertex_; Transaction *transaction_; From 5656a24c9638e1715cc31820774b10b1b01f9982 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 15 Nov 2022 18:25:25 +0100 Subject: [PATCH 044/103] Remove unused exception --- src/query/v2/exceptions.hpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/query/v2/exceptions.hpp b/src/query/v2/exceptions.hpp index 959672eae..e0802a6cc 100644 --- a/src/query/v2/exceptions.hpp +++ b/src/query/v2/exceptions.hpp @@ -224,12 +224,4 @@ class VersionInfoInMulticommandTxException : public QueryException { : QueryException("Version info query not allowed in multicommand transactions.") {} }; -/** - * An exception for an illegal operation that violates schema - */ -class SchemaViolationException : public QueryRuntimeException { - public: - using QueryRuntimeException::QueryRuntimeException; -}; - } // namespace memgraph::query::v2 From 3f97a1349347be6da00dd1ecbb3eb59954f9367b Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 15 Nov 2022 18:30:25 +0100 Subject: [PATCH 045/103] Revert TError to Error --- src/expr/interpret/eval.hpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/expr/interpret/eval.hpp b/src/expr/interpret/eval.hpp index d105eeefb..04676dd7b 100644 --- a/src/expr/interpret/eval.hpp +++ b/src/expr/interpret/eval.hpp @@ -32,7 +32,7 @@ struct StorageTag {}; struct QueryEngineTag {}; template + typename PropertyValue, typename ConvFunctor, typename Error, typename Tag = StorageTag> class ExpressionEvaluator : public ExpressionVisitor { public: ExpressionEvaluator(Frame *frame, const SymbolTable &symbol_table, const EvaluationContext &ctx, @@ -100,23 +100,23 @@ class ExpressionEvaluator : public ExpressionVisitor { #undef BINARY_OPERATOR_VISITOR #undef UNARY_OPERATOR_VISITOR - void HandleShardError(TError &shard_error, const std::string_view accessed_object) { + void HandleShardError(Error &shard_error, const std::string_view accessed_object) { switch (shard_error) { - case TError::DELETED_OBJECT: + case Error::DELETED_OBJECT: throw ExpressionRuntimeException("Trying to access {} on a deleted object.", accessed_object); - case TError::NONEXISTENT_OBJECT: + case Error::NONEXISTENT_OBJECT: throw ExpressionRuntimeException("Trying to access {} from a node object doesn't exist.", accessed_object); - case TError::SERIALIZATION_ERROR: - case TError::VERTEX_HAS_EDGES: - case TError::PROPERTIES_DISABLED: - case TError::VERTEX_ALREADY_INSERTED: + case Error::SERIALIZATION_ERROR: + case Error::VERTEX_HAS_EDGES: + case Error::PROPERTIES_DISABLED: + case Error::VERTEX_ALREADY_INSERTED: throw ExpressionRuntimeException("Unexpected error when accessing {}.", accessed_object); - case TError::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: - case TError::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: - case TError::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: - case TError::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: - case TError::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: - case TError::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: + case Error::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: + case Error::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: + case Error::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: + case Error::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: + case Error::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: + case Error::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: throw ExpressionRuntimeException("Unexpected schema violation when accessing {}.", accessed_object); } } @@ -406,7 +406,7 @@ class ExpressionEvaluator : public ExpressionVisitor { typename TReturnType = std::enable_if_t, bool>> TReturnType HasLabelImpl(const VertexAccessor &vertex, const LabelIx &label, StorageTag /*tag*/) { auto has_label = vertex.HasLabel(view_, GetLabel(label)); - if (has_label.HasError() && has_label.GetError().code == TError::NONEXISTENT_OBJECT) { + if (has_label.HasError() && has_label.GetError().code == Error::NONEXISTENT_OBJECT) { // This is a very nasty and temporary hack in order to make MERGE // work. The old storage had the following logic when returning an // `OLD` view: `return old ? old : new`. That means that if the @@ -745,7 +745,7 @@ class ExpressionEvaluator : public ExpressionVisitor { class TReturnType = std::enable_if_t, TypedValue>> TypedValue GetProperty(const TRecordAccessor &record_accessor, PropertyIx prop) { auto maybe_prop = record_accessor.GetProperty(view_, ctx_->properties[prop.ix]); - if (maybe_prop.HasError() && maybe_prop.GetError().code == TError::NONEXISTENT_OBJECT) { + if (maybe_prop.HasError() && maybe_prop.GetError().code == Error::NONEXISTENT_OBJECT) { // This is a very nasty and temporary hack in order to make MERGE work. // The old storage had the following logic when returning an `OLD` view: // `return old ? old : new`. That means that if the `OLD` view didn't @@ -764,7 +764,7 @@ class ExpressionEvaluator : public ExpressionVisitor { class TReturnType = std::enable_if_t, TypedValue>> TypedValue GetProperty(const TRecordAccessor &record_accessor, const std::string_view name) { auto maybe_prop = record_accessor.GetProperty(view_, dba_->NameToProperty(name)); - if (maybe_prop.HasError() && maybe_prop.GetError().code == TError::NONEXISTENT_OBJECT) { + if (maybe_prop.HasError() && maybe_prop.GetError().code == Error::NONEXISTENT_OBJECT) { // This is a very nasty and temporary hack in order to make MERGE work. // The old storage had the following logic when returning an `OLD` view: // `return old ? old : new`. That means that if the `OLD` view didn't From 1c94c59a2413952997e9bfc7e18ed35bde5dbea2 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 15 Nov 2022 19:05:20 +0100 Subject: [PATCH 046/103] Fix tests --- src/query/v2/db_accessor.hpp | 1 - src/storage/v3/result.hpp | 2 + tests/unit/storage_v3.cpp | 114 +++++++++------------ tests/unit/storage_v3_edge.cpp | 13 ++- tests/unit/storage_v3_schema.cpp | 36 +++---- tests/unit/storage_v3_vertex_accessors.cpp | 24 ++--- 6 files changed, 77 insertions(+), 113 deletions(-) diff --git a/src/query/v2/db_accessor.hpp b/src/query/v2/db_accessor.hpp index b76da6131..11118a899 100644 --- a/src/query/v2/db_accessor.hpp +++ b/src/query/v2/db_accessor.hpp @@ -23,7 +23,6 @@ #include "storage/v3/key_store.hpp" #include "storage/v3/property_value.hpp" #include "storage/v3/result.hpp" -#include "storage/v3/shard_operation_result.hpp" /////////////////////////////////////////////////////////// // Our communication layer and query engine don't mix diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index b13677f5d..4ba64cf02 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -46,6 +46,8 @@ struct ShardError { // TODO Maybe add category std::string message; std::string source; + + inline friend bool operator==(const ShardError &lhs, const ShardError &rhs) { return lhs.code == rhs.code; } }; #define SHARD_ERROR(...) memgraph::storage::v3::ShardError(__VA_ARGS__, fmt::format("{}:{}", __FILE__, __LINE__)) diff --git a/tests/unit/storage_v3.cpp b/tests/unit/storage_v3.cpp index b107786e8..cd524cf41 100644 --- a/tests/unit/storage_v3.cpp +++ b/tests/unit/storage_v3.cpp @@ -579,7 +579,7 @@ TEST_P(StorageV3, VertexDeleteSerializationError) { EXPECT_EQ(CountVertices(acc2, View::NEW), 1U); auto res = acc2.DeleteVertex(&*vertex); ASSERT_TRUE(res.HasError()); - ASSERT_EQ(res.GetError(), Error::SERIALIZATION_ERROR); + ASSERT_EQ(res.GetError(), SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR)); EXPECT_EQ(CountVertices(acc2, View::OLD), 1U); EXPECT_EQ(CountVertices(acc2, View::NEW), 1U); acc2.AdvanceCommand(); @@ -660,12 +660,11 @@ TEST_P(StorageV3, VertexDeleteSpecialCases) { } } -template -void AssertErrorInVariant(TResultHolder &holder, TError error_type) { - ASSERT_TRUE(holder.HasError()); - const auto error = holder.GetError(); - ASSERT_TRUE(std::holds_alternative(error)); - ASSERT_EQ(std::get(error), error_type); +template +void AssertShardErrorEqual(const ShardResult &lhs, const ShardError &rhs) { + ASSERT_TRUE(lhs.HasError()); + const auto error = lhs.GetError(); + ASSERT_EQ(error, rhs); } // NOLINTNEXTLINE(hicpp-special-member-functions) @@ -711,20 +710,20 @@ TEST_P(StorageV3, VertexDeleteLabel) { // Check whether label 5 exists ASSERT_FALSE(vertex->HasLabel(label5, View::OLD).GetValue()); - ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), Error::DELETED_OBJECT); + ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); ASSERT_EQ(vertex->Labels(View::OLD)->size(), 0); - ASSERT_EQ(vertex->Labels(View::NEW).GetError(), Error::DELETED_OBJECT); + ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); // Try to add the label { auto ret = vertex->AddLabelAndValidate(label5); - AssertErrorInVariant(ret, Error::DELETED_OBJECT); + AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); } // Try to remove the label { auto ret = vertex->RemoveLabelAndValidate(label5); - AssertErrorInVariant(ret, Error::DELETED_OBJECT); + AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); } acc.Abort(); @@ -779,33 +778,33 @@ TEST_P(StorageV3, VertexDeleteLabel) { // Check whether label 5 exists ASSERT_TRUE(vertex->HasLabel(label5, View::OLD).GetValue()); - ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), Error::DELETED_OBJECT); + ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); { auto labels = vertex->Labels(View::OLD).GetValue(); ASSERT_EQ(labels.size(), 1); ASSERT_EQ(labels[0], label5); } - ASSERT_EQ(vertex->Labels(View::NEW).GetError(), Error::DELETED_OBJECT); + ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); // Advance command acc.AdvanceCommand(); // Check whether label 5 exists - ASSERT_EQ(vertex->HasLabel(label5, View::OLD).GetError(), Error::DELETED_OBJECT); - ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), Error::DELETED_OBJECT); - ASSERT_EQ(vertex->Labels(View::OLD).GetError(), Error::DELETED_OBJECT); - ASSERT_EQ(vertex->Labels(View::NEW).GetError(), Error::DELETED_OBJECT); + ASSERT_EQ(vertex->HasLabel(label5, View::OLD).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Labels(View::OLD).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); // Try to add the label { auto ret = vertex->AddLabelAndValidate(label5); - AssertErrorInVariant(ret, Error::DELETED_OBJECT); + AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); } // Try to remove the label { auto ret = vertex->RemoveLabelAndValidate(label5); - AssertErrorInVariant(ret, Error::DELETED_OBJECT); + AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); } acc.Abort(); @@ -855,14 +854,14 @@ TEST_P(StorageV3, VertexDeleteProperty) { // Check whether label 5 exists ASSERT_TRUE(vertex->GetProperty(property5, View::OLD)->IsNull()); - ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), Error::DELETED_OBJECT); + ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); ASSERT_EQ(vertex->Properties(View::OLD)->size(), 0); - ASSERT_EQ(vertex->Properties(View::NEW).GetError(), Error::DELETED_OBJECT); + ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); // Try to set the property5 { auto ret = vertex->SetPropertyAndValidate(property5, PropertyValue("haihai")); - AssertErrorInVariant(ret, Error::DELETED_OBJECT); + AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); } acc.Abort(); @@ -918,27 +917,27 @@ TEST_P(StorageV3, VertexDeleteProperty) { // Check whether property 5 exists ASSERT_EQ(vertex->GetProperty(property5, View::OLD)->ValueString(), "nandare"); - ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), Error::DELETED_OBJECT); + ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); { auto properties = vertex->Properties(View::OLD).GetValue(); ASSERT_EQ(properties.size(), 1); ASSERT_EQ(properties[property5].ValueString(), "nandare"); } - ASSERT_EQ(vertex->Properties(View::NEW).GetError(), Error::DELETED_OBJECT); + ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); // Advance command acc.AdvanceCommand(); // Check whether property 5 exists - ASSERT_EQ(vertex->GetProperty(property5, View::OLD).GetError(), Error::DELETED_OBJECT); - ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), Error::DELETED_OBJECT); - ASSERT_EQ(vertex->Properties(View::OLD).GetError(), Error::DELETED_OBJECT); - ASSERT_EQ(vertex->Properties(View::NEW).GetError(), Error::DELETED_OBJECT); + ASSERT_EQ(vertex->GetProperty(property5, View::OLD).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Properties(View::OLD).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); // Try to set the property { auto ret = vertex->SetPropertyAndValidate(property5, PropertyValue("haihai")); - AssertErrorInVariant(ret, Error::DELETED_OBJECT); + AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); } acc.Abort(); @@ -1371,7 +1370,7 @@ TEST_P(StorageV3, VertexLabelSerializationError) { { auto res = vertex->AddLabelAndValidate(label1); - AssertErrorInVariant(res, Error::SERIALIZATION_ERROR); + AssertShardErrorEqual(res, SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR)); } } @@ -1865,7 +1864,7 @@ TEST_P(StorageV3, VertexPropertySerializationError) { { auto res = vertex->SetPropertyAndValidate(property2, PropertyValue("nandare")); - AssertErrorInVariant(res, Error::SERIALIZATION_ERROR); + AssertShardErrorEqual(res, SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR)); } } @@ -2255,14 +2254,14 @@ TEST_P(StorageV3, VertexNonexistentLabelPropertyEdgeAPI) { auto vertex = CreateVertexAndValidate(acc, {}, PropertyValue{0}, {}); // Check state before (OLD view). - ASSERT_EQ(vertex.Labels(View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.HasLabel(label1, View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.Properties(View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.GetProperty(property1, View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.InEdges(View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.OutEdges(View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.InDegree(View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.OutDegree(View::OLD).GetError(), Error::NONEXISTENT_OBJECT); + ASSERT_EQ(vertex.Labels(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.HasLabel(label1, View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.Properties(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.GetProperty(property1, View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.InEdges(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.OutEdges(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.InDegree(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.OutDegree(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); // Check state before (NEW view). ASSERT_EQ(vertex.Labels(View::NEW)->size(), 0); @@ -2282,14 +2281,14 @@ TEST_P(StorageV3, VertexNonexistentLabelPropertyEdgeAPI) { .HasValue()); // Check state after (OLD view). - ASSERT_EQ(vertex.Labels(View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.HasLabel(label1, View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.Properties(View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.GetProperty(property1, View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.InEdges(View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.OutEdges(View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.InDegree(View::OLD).GetError(), Error::NONEXISTENT_OBJECT); - ASSERT_EQ(vertex.OutDegree(View::OLD).GetError(), Error::NONEXISTENT_OBJECT); + ASSERT_EQ(vertex.Labels(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.HasLabel(label1, View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.Properties(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.GetProperty(property1, View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.InEdges(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.OutEdges(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.InDegree(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.OutDegree(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); // Check state after (NEW view). ASSERT_EQ(vertex.Labels(View::NEW)->size(), 1); @@ -2657,42 +2656,31 @@ TEST_P(StorageV3, TestCreateVertexAndValidate) { ASSERT_TRUE(vertex2.HasError()); auto error = vertex2.GetError(); - auto error_ptr = std::get_if(&error); - ASSERT_TRUE(error_ptr); - ASSERT_TRUE(*error_ptr == storage::v3::Error::VERTEX_ALREADY_INSERTED); + ASSERT_TRUE(error.code == storage::v3::ErrorCode::VERTEX_ALREADY_INSERTED); } { auto acc = store.Access(GetNextHlc()); auto vertex = acc.CreateVertexAndValidate({primary_label}, {PropertyValue{0}}, {}); ASSERT_TRUE(vertex.HasError()); - ASSERT_TRUE(std::holds_alternative(vertex.GetError())); - EXPECT_EQ(std::get(vertex.GetError()), - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_SECONDARY_LABEL_IS_PRIMARY, primary_label)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { auto acc = store.Access(GetNextHlc()); auto vertex = acc.CreateVertexAndValidate({primary_label}, {PropertyValue{0}}, {}); ASSERT_TRUE(vertex.HasError()); - ASSERT_TRUE(std::holds_alternative(vertex.GetError())); - EXPECT_EQ(std::get(vertex.GetError()), - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_SECONDARY_LABEL_IS_PRIMARY, primary_label)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { auto acc = store.Access(GetNextHlc()); auto vertex = acc.CreateVertexAndValidate({}, {}, {}); ASSERT_TRUE(vertex.HasError()); - ASSERT_TRUE(std::holds_alternative(vertex.GetError())); - EXPECT_EQ(std::get(vertex.GetError()), - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_PRIMARY_PROPERTIES_UNDEFINED, primary_label)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); } { auto acc = store.Access(GetNextHlc()); auto vertex = acc.CreateVertexAndValidate({}, {PropertyValue{"test"}}, {}); ASSERT_TRUE(vertex.HasError()); - ASSERT_TRUE(std::holds_alternative(vertex.GetError())); - EXPECT_EQ(std::get(vertex.GetError()), - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_PROPERTY_WRONG_TYPE, primary_label, - {primary_property, common::SchemaType::INT}, PropertyValue("test"))); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } } } // namespace memgraph::storage::v3::tests diff --git a/tests/unit/storage_v3_edge.cpp b/tests/unit/storage_v3_edge.cpp index 21c4214ce..64f7f4e2e 100644 --- a/tests/unit/storage_v3_edge.cpp +++ b/tests/unit/storage_v3_edge.cpp @@ -18,7 +18,6 @@ #include "storage/v3/name_id_mapper.hpp" #include "storage/v3/property_value.hpp" #include "storage/v3/shard.hpp" -#include "storage/v3/shard_operation_result.hpp" namespace memgraph::storage::v3::tests { using testing::UnorderedElementsAre; @@ -39,7 +38,7 @@ class StorageEdgeTest : public ::testing::TestWithParam { return store.NameToEdgeType(edge_type_name); } - static ShardOperationResult CreateVertex(Shard::Accessor &acc, const PropertyValue &key) { + static ShardResult CreateVertex(Shard::Accessor &acc, const PropertyValue &key) { return acc.CreateVertexAndValidate({}, {key}, {}); } @@ -3243,7 +3242,7 @@ TEST_P(StorageEdgeTest, VertexDetachDeleteSingleCommit) { { auto ret = acc.DeleteVertex(&vertex_from.value()); ASSERT_TRUE(ret.HasError()); - ASSERT_EQ(ret.GetError(), Error::VERTEX_HAS_EDGES); + ASSERT_EQ(ret.GetError(), SHARD_ERROR(ErrorCode::VERTEX_HAS_EDGES)); } // Detach delete vertex @@ -3256,8 +3255,8 @@ TEST_P(StorageEdgeTest, VertexDetachDeleteSingleCommit) { // Check edges ASSERT_EQ(vertex_from->InEdges(View::OLD)->size(), 0); ASSERT_EQ(*vertex_from->InDegree(View::OLD), 0); - ASSERT_EQ(vertex_from->InEdges(View::NEW).GetError(), Error::DELETED_OBJECT); - ASSERT_EQ(vertex_from->InDegree(View::NEW).GetError(), Error::DELETED_OBJECT); + ASSERT_EQ(vertex_from->InEdges(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex_from->InDegree(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); { auto ret = vertex_from->OutEdges(View::OLD); ASSERT_TRUE(ret.HasValue()); @@ -3270,8 +3269,8 @@ TEST_P(StorageEdgeTest, VertexDetachDeleteSingleCommit) { ASSERT_EQ(e.FromVertex(), from_id); ASSERT_EQ(e.ToVertex(), to_id); } - ASSERT_EQ(vertex_from->OutEdges(View::NEW).GetError(), Error::DELETED_OBJECT); - ASSERT_EQ(vertex_from->OutDegree(View::NEW).GetError(), Error::DELETED_OBJECT); + ASSERT_EQ(vertex_from->OutEdges(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex_from->OutDegree(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); { auto ret = vertex_to->InEdges(View::OLD); ASSERT_TRUE(ret.HasValue()); diff --git a/tests/unit/storage_v3_schema.cpp b/tests/unit/storage_v3_schema.cpp index c36b92bc3..2189521ca 100644 --- a/tests/unit/storage_v3_schema.cpp +++ b/tests/unit/storage_v3_schema.cpp @@ -162,9 +162,8 @@ class SchemaValidatorTest : public testing::Test { PropertyId NameToProperty(const std::string &name) { return PropertyId::FromUint(id_mapper_.NameToId(name)); } - protected: Schemas schemas; - SchemaValidator schema_validator{schemas}; + SchemaValidator schema_validator{schemas, id_mapper_}; PropertyId prop_string{NameToProperty("prop1")}; PropertyId prop_int{NameToProperty("prop2")}; PropertyId prop_duration{NameToProperty("prop3")}; @@ -180,50 +179,43 @@ TEST_F(SchemaValidatorTest, TestSchemaValidateVertexCreate) { { const auto schema_violation = schema_validator.ValidateVertexCreate(NameToLabel("test"), {}, {PropertyValue(1)}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, - SchemaViolation(SchemaViolation::ValidationStatus::NO_SCHEMA_DEFINED_FOR_LABEL, NameToLabel("test"))); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_PRIMARY_PROPERTIES_UNDEFINED, label2)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); } // Validate wrong secondary label { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {label1}, {PropertyValue("test")}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_SECONDARY_LABEL_IS_PRIMARY, label1)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {label2}, {PropertyValue("test")}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_SECONDARY_LABEL_IS_PRIMARY, label2)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } // Validate wrong property type { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {}, {PropertyValue(1)}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_PROPERTY_WRONG_TYPE, label1, - schema_prop_string, PropertyValue(1))); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {PropertyValue("test"), PropertyValue(12), PropertyValue(1)}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_PROPERTY_WRONG_TYPE, label2, - schema_prop_duration, PropertyValue(1))); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } { const auto wrong_prop = PropertyValue(TemporalData(TemporalType::Date, 1234)); const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {PropertyValue("test"), PropertyValue(12), wrong_prop}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_PROPERTY_WRONG_TYPE, label2, - schema_prop_duration, wrong_prop)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } // Passing validations EXPECT_EQ(schema_validator.ValidateVertexCreate(label1, {}, {PropertyValue("test")}), std::nullopt); @@ -245,14 +237,12 @@ TEST_F(SchemaValidatorTest, TestSchemaValidatePropertyUpdate) { { const auto schema_violation = schema_validator.ValidatePropertyUpdate(label1, prop_string); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_KEY, label1, - schema_prop_string)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } { const auto schema_violation = schema_validator.ValidatePropertyUpdate(label2, prop_duration); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_KEY, label2, - schema_prop_duration)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } EXPECT_EQ(schema_validator.ValidatePropertyUpdate(label1, prop_int), std::nullopt); EXPECT_EQ(schema_validator.ValidatePropertyUpdate(label1, prop_duration), std::nullopt); @@ -264,14 +254,12 @@ TEST_F(SchemaValidatorTest, TestSchemaValidatePropertyUpdateLabel) { { const auto schema_violation = schema_validator.ValidateLabelUpdate(label1); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_LABEL, label1)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } { const auto schema_violation = schema_validator.ValidateLabelUpdate(label2); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_LABEL, label2)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } EXPECT_EQ(schema_validator.ValidateLabelUpdate(NameToLabel("test")), std::nullopt); } diff --git a/tests/unit/storage_v3_vertex_accessors.cpp b/tests/unit/storage_v3_vertex_accessors.cpp index 683f58a56..3ba8def82 100644 --- a/tests/unit/storage_v3_vertex_accessors.cpp +++ b/tests/unit/storage_v3_vertex_accessors.cpp @@ -76,7 +76,7 @@ TEST_F(StorageV3Accessor, TestPrimaryLabel) { ASSERT_TRUE(vertex.PrimaryLabel(View::OLD).HasError()); const auto error_primary_label = vertex.PrimaryLabel(View::OLD).GetError(); ASSERT_FALSE(vertex.PrimaryLabel(View::NEW).HasError()); - EXPECT_EQ(error_primary_label, Error::NONEXISTENT_OBJECT); + EXPECT_EQ(error_primary_label, SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); } { auto acc = storage.Access(GetNextHlc()); @@ -127,9 +127,7 @@ TEST_F(StorageV3Accessor, TestAddLabels) { const auto label1 = NameToLabelId("label"); auto vertex = acc.CreateVertexAndValidate({label1}, {PropertyValue{2}}, {}); ASSERT_TRUE(vertex.HasError()); - ASSERT_TRUE(std::holds_alternative(vertex.GetError())); - EXPECT_EQ(std::get(vertex.GetError()), - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_SECONDARY_LABEL_IS_PRIMARY, label1)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { auto acc = storage.Access(GetNextHlc()); @@ -138,9 +136,7 @@ TEST_F(StorageV3Accessor, TestAddLabels) { ASSERT_TRUE(vertex.HasValue()); const auto schema_violation = vertex->AddLabelAndValidate(label1); ASSERT_TRUE(schema_violation.HasError()); - ASSERT_TRUE(std::holds_alternative(schema_violation.GetError())); - EXPECT_EQ(std::get(schema_violation.GetError()), - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_LABEL, label1)); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } } @@ -184,9 +180,7 @@ TEST_F(StorageV3Accessor, TestRemoveLabels) { auto vertex = CreateVertexAndValidate(acc, {}, PropertyValue{2}); const auto res1 = vertex.RemoveLabelAndValidate(primary_label); ASSERT_TRUE(res1.HasError()); - ASSERT_TRUE(std::holds_alternative(res1.GetError())); - EXPECT_EQ(std::get(res1.GetError()), - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_LABEL, primary_label)); + EXPECT_EQ(res1.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } } @@ -205,20 +199,14 @@ TEST_F(StorageV3Accessor, TestSetKeysAndProperties) { auto vertex = CreateVertexAndValidate(acc, {}, PropertyValue{1}); const auto res = vertex.SetPropertyAndValidate(primary_property, PropertyValue(1)); ASSERT_TRUE(res.HasError()); - ASSERT_TRUE(std::holds_alternative(res.GetError())); - EXPECT_EQ(std::get(res.GetError()), - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_KEY, primary_label, - SchemaProperty{primary_property, common::SchemaType::INT})); + EXPECT_EQ(res.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } { auto acc = storage.Access(GetNextHlc()); auto vertex = CreateVertexAndValidate(acc, {}, PropertyValue{2}); const auto res = vertex.SetPropertyAndValidate(primary_property, PropertyValue()); ASSERT_TRUE(res.HasError()); - ASSERT_TRUE(std::holds_alternative(res.GetError())); - EXPECT_EQ(std::get(res.GetError()), - SchemaViolation(SchemaViolation::ValidationStatus::VERTEX_UPDATE_PRIMARY_KEY, primary_label, - SchemaProperty{primary_property, common::SchemaType::INT})); + EXPECT_EQ(res.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } } From 8629ee5ebcfa87258b00de437b669000b077bac6 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 15 Nov 2022 19:30:34 +0100 Subject: [PATCH 047/103] Fix query v2 --- src/query/v2/common.hpp | 25 ++------------- src/query/v2/db_accessor.hpp | 56 ++++++++++++++++------------------ src/query/v2/plan/operator.cpp | 24 ++++++++++----- 3 files changed, 44 insertions(+), 61 deletions(-) diff --git a/src/query/v2/common.hpp b/src/query/v2/common.hpp index 55c60ce10..774a90a37 100644 --- a/src/query/v2/common.hpp +++ b/src/query/v2/common.hpp @@ -28,7 +28,6 @@ #include "storage/v3/id_types.hpp" #include "storage/v3/property_value.hpp" #include "storage/v3/result.hpp" -#include "storage/v3/shard_operation_result.hpp" #include "storage/v3/view.hpp" #include "utils/exceptions.hpp" #include "utils/logging.hpp" @@ -85,7 +84,7 @@ inline void ExpectType(const Symbol &symbol, const TypedValue &value, TypedValue template concept AccessorWithSetProperty = requires(T accessor, const storage::v3::PropertyId key, const storage::v3::PropertyValue new_value) { - { accessor.SetProperty(key, new_value) } -> std::same_as>; + { accessor.SetProperty(key, new_value) } -> std::same_as>; }; template @@ -93,28 +92,8 @@ concept AccessorWithSetPropertyAndValidate = requires(T accessor, const storage: const storage::v3::PropertyValue new_value) { { accessor.SetPropertyAndValidate(key, new_value) - } -> std::same_as>; + } -> std::same_as>; }; -template -concept RecordAccessor = - AccessorWithSetProperty || AccessorWithSetPropertyAndValidate; - -inline void HandleErrorOnPropertyUpdate(const storage::v3::Error error) { - switch (error) { - case storage::v3::Error::SERIALIZATION_ERROR: - throw TransactionSerializationException(); - case storage::v3::Error::DELETED_OBJECT: - throw QueryRuntimeException("Trying to set properties on a deleted object."); - case storage::v3::Error::PROPERTIES_DISABLED: - throw QueryRuntimeException("Can't set property because properties on edges are disabled."); - case storage::v3::Error::VERTEX_HAS_EDGES: - case storage::v3::Error::NONEXISTENT_OBJECT: - case storage::v3::Error::VERTEX_ALREADY_INSERTED: - - throw QueryRuntimeException("Unexpected error when setting a property."); - } -} - int64_t QueryTimestamp(); } // namespace memgraph::query::v2 diff --git a/src/query/v2/db_accessor.hpp b/src/query/v2/db_accessor.hpp index 11118a899..dfa23376f 100644 --- a/src/query/v2/db_accessor.hpp +++ b/src/query/v2/db_accessor.hpp @@ -64,21 +64,21 @@ class EdgeAccessor final { auto Properties(storage::v3::View view) const { return impl_.Properties(view); } - storage::v3::Result GetProperty(storage::v3::View view, - storage::v3::PropertyId key) const { + storage::v3::ShardResult GetProperty(storage::v3::View view, + storage::v3::PropertyId key) const { return impl_.GetProperty(key, view); } - storage::v3::Result SetProperty(storage::v3::PropertyId key, - const storage::v3::PropertyValue &value) { + storage::v3::ShardResult SetProperty(storage::v3::PropertyId key, + const storage::v3::PropertyValue &value) { return impl_.SetProperty(key, value); } - storage::v3::Result RemoveProperty(storage::v3::PropertyId key) { + storage::v3::ShardResult RemoveProperty(storage::v3::PropertyId key) { return SetProperty(key, storage::v3::PropertyValue()); } - storage::v3::Result> ClearProperties() { + storage::v3::ShardResult> ClearProperties() { return impl_.ClearProperties(); } @@ -113,53 +113,49 @@ class VertexAccessor final { auto PrimaryKey(storage::v3::View view) const { return impl_.PrimaryKey(view); } - storage::v3::ShardOperationResult AddLabel(storage::v3::LabelId label) { + storage::v3::ShardResult AddLabel(storage::v3::LabelId label) { return impl_.AddLabelAndValidate(label); } + + storage::v3::ShardResult AddLabelAndValidate(storage::v3::LabelId label) { return impl_.AddLabelAndValidate(label); } - storage::v3::ShardOperationResult AddLabelAndValidate(storage::v3::LabelId label) { - return impl_.AddLabelAndValidate(label); - } + storage::v3::ShardResult RemoveLabel(storage::v3::LabelId label) { return impl_.RemoveLabelAndValidate(label); } - storage::v3::ShardOperationResult RemoveLabel(storage::v3::LabelId label) { + storage::v3::ShardResult RemoveLabelAndValidate(storage::v3::LabelId label) { return impl_.RemoveLabelAndValidate(label); } - storage::v3::ShardOperationResult RemoveLabelAndValidate(storage::v3::LabelId label) { - return impl_.RemoveLabelAndValidate(label); - } - - storage::v3::Result HasLabel(storage::v3::View view, storage::v3::LabelId label) const { + storage::v3::ShardResult HasLabel(storage::v3::View view, storage::v3::LabelId label) const { return impl_.HasLabel(label, view); } auto Properties(storage::v3::View view) const { return impl_.Properties(view); } - storage::v3::Result GetProperty(storage::v3::View view, - storage::v3::PropertyId key) const { + storage::v3::ShardResult GetProperty(storage::v3::View view, + storage::v3::PropertyId key) const { return impl_.GetProperty(key, view); } - storage::v3::ShardOperationResult SetProperty(storage::v3::PropertyId key, - const storage::v3::PropertyValue &value) { + storage::v3::ShardResult SetProperty(storage::v3::PropertyId key, + const storage::v3::PropertyValue &value) { return impl_.SetPropertyAndValidate(key, value); } - storage::v3::ShardOperationResult SetPropertyAndValidate( - storage::v3::PropertyId key, const storage::v3::PropertyValue &value) { + storage::v3::ShardResult SetPropertyAndValidate(storage::v3::PropertyId key, + const storage::v3::PropertyValue &value) { return impl_.SetPropertyAndValidate(key, value); } - storage::v3::ShardOperationResult RemovePropertyAndValidate(storage::v3::PropertyId key) { + storage::v3::ShardResult RemovePropertyAndValidate(storage::v3::PropertyId key) { return SetPropertyAndValidate(key, storage::v3::PropertyValue{}); } - storage::v3::Result> ClearProperties() { + storage::v3::ShardResult> ClearProperties() { return impl_.ClearProperties(); } auto InEdges(storage::v3::View view, const std::vector &edge_types) const - -> storage::v3::Result { + -> storage::v3::ShardResult { auto maybe_edges = impl_.InEdges(view, edge_types); if (maybe_edges.HasError()) return maybe_edges.GetError(); return iter::imap(MakeEdgeAccessor, std::move(*maybe_edges)); @@ -169,7 +165,7 @@ class VertexAccessor final { auto InEdges(storage::v3::View view, const std::vector &edge_types, const VertexAccessor &dest) const - -> storage::v3::Result { + -> storage::v3::ShardResult { const auto dest_id = dest.impl_.Id(view).GetValue(); auto maybe_edges = impl_.InEdges(view, edge_types, &dest_id); if (maybe_edges.HasError()) return maybe_edges.GetError(); @@ -177,7 +173,7 @@ class VertexAccessor final { } auto OutEdges(storage::v3::View view, const std::vector &edge_types) const - -> storage::v3::Result { + -> storage::v3::ShardResult { auto maybe_edges = impl_.OutEdges(view, edge_types); if (maybe_edges.HasError()) return maybe_edges.GetError(); return iter::imap(MakeEdgeAccessor, std::move(*maybe_edges)); @@ -187,16 +183,16 @@ class VertexAccessor final { auto OutEdges(storage::v3::View view, const std::vector &edge_types, const VertexAccessor &dest) const - -> storage::v3::Result { + -> storage::v3::ShardResult { const auto dest_id = dest.impl_.Id(view).GetValue(); auto maybe_edges = impl_.OutEdges(view, edge_types, &dest_id); if (maybe_edges.HasError()) return maybe_edges.GetError(); return iter::imap(MakeEdgeAccessor, std::move(*maybe_edges)); } - storage::v3::Result InDegree(storage::v3::View view) const { return impl_.InDegree(view); } + storage::v3::ShardResult InDegree(storage::v3::View view) const { return impl_.InDegree(view); } - storage::v3::Result OutDegree(storage::v3::View view) const { return impl_.OutDegree(view); } + storage::v3::ShardResult OutDegree(storage::v3::View view) const { return impl_.OutDegree(view); } // TODO(jbajic) Fix Remove Gid static int64_t CypherId() { return 1; } diff --git a/src/query/v2/plan/operator.cpp b/src/query/v2/plan/operator.cpp index 068ca9192..5e6e8bc9a 100644 --- a/src/query/v2/plan/operator.cpp +++ b/src/query/v2/plan/operator.cpp @@ -42,6 +42,7 @@ #include "query/v2/shard_request_manager.hpp" #include "storage/v3/conversions.hpp" #include "storage/v3/property_value.hpp" +#include "storage/v3/result.hpp" #include "utils/algorithm.hpp" #include "utils/csv_parsing.hpp" #include "utils/event_counter.hpp" @@ -566,17 +567,24 @@ UniqueCursorPtr ScanAllById::MakeCursor(utils::MemoryResource *mem) const { namespace { template -auto UnwrapEdgesResult(storage::v3::Result &&result) { +auto UnwrapEdgesResult(storage::v3::ShardResult &&result) { if (result.HasError()) { - switch (result.GetError()) { - case storage::v3::Error::DELETED_OBJECT: + switch (result.GetError().code) { + case storage::v3::ErrorCode::DELETED_OBJECT: throw QueryRuntimeException("Trying to get relationships of a deleted node."); - case storage::v3::Error::NONEXISTENT_OBJECT: + case storage::v3::ErrorCode::NONEXISTENT_OBJECT: throw query::v2::QueryRuntimeException("Trying to get relationships from a node that doesn't exist."); - case storage::v3::Error::VERTEX_HAS_EDGES: - case storage::v3::Error::SERIALIZATION_ERROR: - case storage::v3::Error::PROPERTIES_DISABLED: + case storage::v3::ErrorCode::VERTEX_HAS_EDGES: + case storage::v3::ErrorCode::SERIALIZATION_ERROR: + case storage::v3::ErrorCode::PROPERTIES_DISABLED: throw QueryRuntimeException("Unexpected error when accessing relationships."); + case storage::v3::ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: + case storage::v3::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: + case storage::v3::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: + case storage::v3::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: + case storage::v3::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: + case storage::v3::ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: + throw QueryRuntimeException("SchemaViolation occurred when accessing relationships."); } } return std::move(*result); @@ -843,7 +851,7 @@ concept AccessorWithProperties = requires(T value, storage::v3::PropertyId prope storage::v3::PropertyValue property_value) { { value.ClearProperties() - } -> std::same_as>>; + } -> std::same_as>>; {value.SetProperty(property_id, property_value)}; }; From 14ddd7254d5be185fae59038eb8c0466a69b93f4 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 15 Nov 2022 19:37:24 +0100 Subject: [PATCH 048/103] Fix communication and memgraph --- src/glue/v2/communication.cpp | 14 ++++++------ src/glue/v2/communication.hpp | 8 +++---- src/memgraph.cpp | 40 +++++++++++++++++++++++------------ 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/glue/v2/communication.cpp b/src/glue/v2/communication.cpp index ebca8c23f..495ce1698 100644 --- a/src/glue/v2/communication.cpp +++ b/src/glue/v2/communication.cpp @@ -71,7 +71,7 @@ query::v2::TypedValue ToTypedValue(const Value &value) { } } -storage::v3::Result ToBoltVertex( +storage::v3::ShardResult ToBoltVertex( const query::v2::accessors::VertexAccessor &vertex, const msgs::ShardRequestManagerInterface *shard_request_manager, storage::v3::View /*view*/) { auto id = communication::bolt::Id::FromUint(0); @@ -91,7 +91,7 @@ storage::v3::Result ToBoltVertex( return communication::bolt::Vertex{id, new_labels, new_properties}; } -storage::v3::Result ToBoltEdge( +storage::v3::ShardResult ToBoltEdge( const query::v2::accessors::EdgeAccessor &edge, const msgs::ShardRequestManagerInterface *shard_request_manager, storage::v3::View /*view*/) { // TODO(jbajic) Fix bolt communication @@ -108,16 +108,16 @@ storage::v3::Result ToBoltEdge( return communication::bolt::Edge{id, from, to, type, new_properties}; } -storage::v3::Result ToBoltPath( +storage::v3::ShardResult ToBoltPath( const query::v2::accessors::Path & /*edge*/, const msgs::ShardRequestManagerInterface * /*shard_request_manager*/, storage::v3::View /*view*/) { // TODO(jbajic) Fix bolt communication - return {storage::v3::Error::DELETED_OBJECT}; + return {SHARD_ERROR(storage::v3::ErrorCode::DELETED_OBJECT)}; } -storage::v3::Result ToBoltValue(const query::v2::TypedValue &value, - const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View view) { +storage::v3::ShardResult ToBoltValue(const query::v2::TypedValue &value, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View view) { switch (value.type()) { case query::v2::TypedValue::Type::Null: return Value(); diff --git a/src/glue/v2/communication.hpp b/src/glue/v2/communication.hpp index ea9c6b4c9..debc26adf 100644 --- a/src/glue/v2/communication.hpp +++ b/src/glue/v2/communication.hpp @@ -35,7 +35,7 @@ namespace memgraph::glue::v2 { /// @param storage::v3::View for deciding which vertex attributes are visible. /// /// @throw std::bad_alloc -storage::v3::Result ToBoltVertex( +storage::v3::ShardResult ToBoltVertex( const storage::v3::VertexAccessor &vertex, const msgs::ShardRequestManagerInterface *shard_request_manager, storage::v3::View view); @@ -44,7 +44,7 @@ storage::v3::Result ToBoltVertex( /// @param storage::v3::View for deciding which edge attributes are visible. /// /// @throw std::bad_alloc -storage::v3::Result ToBoltEdge( +storage::v3::ShardResult ToBoltEdge( const storage::v3::EdgeAccessor &edge, const msgs::ShardRequestManagerInterface *shard_request_manager, storage::v3::View view); @@ -53,7 +53,7 @@ storage::v3::Result ToBoltEdge( /// @param storage::v3::View for ToBoltVertex and ToBoltEdge. /// /// @throw std::bad_alloc -storage::v3::Result ToBoltPath( +storage::v3::ShardResult ToBoltPath( const query::v2::accessors::Path &path, const msgs::ShardRequestManagerInterface *shard_request_manager, storage::v3::View view); @@ -62,7 +62,7 @@ storage::v3::Result ToBoltPath( /// @param storage::v3::View for ToBoltVertex and ToBoltEdge. /// /// @throw std::bad_alloc -storage::v3::Result ToBoltValue( +storage::v3::ShardResult ToBoltValue( const query::v2::TypedValue &value, const msgs::ShardRequestManagerInterface *shard_request_manager, storage::v3::View view); diff --git a/src/memgraph.cpp b/src/memgraph.cpp index deaf9e568..f553d1821 100644 --- a/src/memgraph.cpp +++ b/src/memgraph.cpp @@ -483,13 +483,19 @@ class BoltSession final : public memgraph::communication::bolt::Session Date: Wed, 16 Nov 2022 08:58:06 +0100 Subject: [PATCH 049/103] Fix scheme test --- tests/unit/storage_v3_schema.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/unit/storage_v3_schema.cpp b/tests/unit/storage_v3_schema.cpp index 2189521ca..e5f651bf2 100644 --- a/tests/unit/storage_v3_schema.cpp +++ b/tests/unit/storage_v3_schema.cpp @@ -150,7 +150,15 @@ TEST_F(SchemaTest, TestSchemaDrop) { class SchemaValidatorTest : public testing::Test { private: - NameIdMapper id_mapper_{{{1, "label1"}, {2, "label2"}, {3, "prop1"}, {4, "prop2"}, {5, "prop3"}}}; + NameIdMapper id_mapper_{{{1, "label1"}, + {2, "label2"}, + {3, "prop1"}, + {4, "prop2"}, + {5, "prop3"}, + {6, "label4"}, + {7, "label5"}, + {8, "label6"}, + {9, "test"}}}; protected: void SetUp() override { From bd11225d236b0fa16b579c41df8a303a40f6899a Mon Sep 17 00:00:00 2001 From: jeremy Date: Wed, 16 Nov 2022 14:14:35 +0100 Subject: [PATCH 050/103] Use ref instead of optional Use ref i.o. ptr Rename variable for clarity --- src/storage/v3/expr.cpp | 5 ++--- src/storage/v3/expr.hpp | 5 ++--- src/storage/v3/shard_rsm.cpp | 39 +++++++++++++++++++++--------------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/storage/v3/expr.cpp b/src/storage/v3/expr.cpp index 53146f595..8062a2662 100644 --- a/src/storage/v3/expr.cpp +++ b/src/storage/v3/expr.cpp @@ -165,7 +165,7 @@ std::any ParseExpression(const std::string &expr, memgraph::expr::AstStorage &st return visitor.visit(ast); } -TypedValue ComputeExpression(DbAccessor &dba, const std::optional &v_acc, +TypedValue ComputeExpression(DbAccessor &dba, const memgraph::storage::v3::VertexAccessor &v_acc, const std::optional &e_acc, const std::string &expression, std::string_view node_name, std::string_view edge_name) { AstStorage storage; @@ -187,13 +187,12 @@ TypedValue ComputeExpression(DbAccessor &dba, const std::optional(expr))->Accept(symbol_generator); if (node_identifier.symbol_pos_ != -1) { - MG_ASSERT(v_acc.has_value()); MG_ASSERT(std::find_if(symbol_table.table().begin(), symbol_table.table().end(), [&node_name](const std::pair &position_symbol_pair) { return position_symbol_pair.second.name() == node_name; }) != symbol_table.table().end()); - frame[symbol_table.at(node_identifier)] = *v_acc; + frame[symbol_table.at(node_identifier)] = v_acc; } if (edge_identifier.symbol_pos_ != -1) { diff --git a/src/storage/v3/expr.hpp b/src/storage/v3/expr.hpp index 7eefdd71c..d344d36b2 100644 --- a/src/storage/v3/expr.hpp +++ b/src/storage/v3/expr.hpp @@ -50,8 +50,7 @@ auto Eval(TExpression *expr, EvaluationContext &ctx, AstStorage &storage, Expres std::any ParseExpression(const std::string &expr, AstStorage &storage); -TypedValue ComputeExpression(DbAccessor &dba, const std::optional &v_acc, - const std::optional &e_acc, const std::string &expression, - std::string_view node_name, std::string_view edge_name); +TypedValue ComputeExpression(DbAccessor &dba, const VertexAccessor &v_acc, const std::optional &e_acc, + const std::string &expression, std::string_view node_name, std::string_view edge_name); } // namespace memgraph::storage::v3 diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 2acdf9f3c..bf31e8ced 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -42,6 +42,7 @@ #include "storage/v3/vertex_accessor.hpp" #include "storage/v3/vertex_id.hpp" #include "storage/v3/view.hpp" +#include "utils/logging.hpp" namespace memgraph::storage::v3 { using msgs::Label; @@ -117,7 +118,7 @@ std::optional> CollectSpecificPropertiesFromAccessor } std::optional> PrimaryKeysFromAccessor(const VertexAccessor &acc, View view, - const Schemas::Schema *schema) { + const Schemas::Schema &schema) { std::map ret; auto props = acc.Properties(view); auto maybe_pk = acc.PrimaryKey(view); @@ -126,16 +127,16 @@ std::optional> PrimaryKeysFromAccessor(const VertexA return std::nullopt; } auto &pk = maybe_pk.GetValue(); - MG_ASSERT(schema->second.size() == pk.size(), "PrimaryKey size does not match schema!"); - for (size_t i{0}; i < schema->second.size(); ++i) { - ret.emplace(schema->second[i].property_id, FromPropertyValueToValue(std::move(pk[i]))); + MG_ASSERT(schema.second.size() == pk.size(), "PrimaryKey size does not match schema!"); + for (size_t i{0}; i < schema.second.size(); ++i) { + ret.emplace(schema.second[i].property_id, FromPropertyValueToValue(std::move(pk[i]))); } return ret; } std::optional> CollectAllPropertiesFromAccessor(const VertexAccessor &acc, View view, - const Schemas::Schema *schema) { + const Schemas::Schema &schema) { std::map ret; auto props = acc.Properties(view); if (props.HasError()) { @@ -204,7 +205,7 @@ std::optional> FillUpSourceVertexSecondaryLabels(const std::optional> FillUpSourceVertexProperties(const std::optional &v_acc, const msgs::ExpandOneRequest &req, storage::v3::View view, - const Schemas::Schema *schema) { + const Schemas::Schema &schema) { std::map src_vertex_properties; if (!req.src_vertex_properties) { @@ -321,7 +322,7 @@ bool FillEdges(const std::vector &edges, msgs::ExpandOneResultRow std::optional GetExpandOneResult( Shard::Accessor &acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, - const Schemas::Schema *schema) { + const Schemas::Schema &schema) { /// Fill up source vertex const auto primary_key = ConvertPropertyVector(src_vertex.second); auto v_acc = acc.FindVertex(primary_key, View::NEW); @@ -367,7 +368,7 @@ std::optional GetExpandOneResult( VertexAccessor v_acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, std::vector in_edge_accessors, std::vector out_edge_accessors, const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, - const Schemas::Schema *schema) { + const Schemas::Schema &schema) { /// Fill up source vertex msgs::Vertex source_vertex = {.id = src_vertex}; if (const auto maybe_secondary_labels = FillUpSourceVertexSecondaryLabels(v_acc, req); maybe_secondary_labels) { @@ -856,7 +857,8 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { found_props = CollectSpecificPropertiesFromAccessor(vertex, req.props_to_return.value(), view); } else { const auto *schema = shard_->GetSchema(shard_->PrimaryLabel()); - found_props = CollectAllPropertiesFromAccessor(vertex, view, schema); + MG_ASSERT(schema); + found_props = CollectAllPropertiesFromAccessor(vertex, view, *schema); } // TODO(gvolfing) -VERIFY- @@ -958,9 +960,10 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { if (!req.order_by.empty()) { // Can we do differently to avoid this? We need OrderByElements but currently it returns vector, so this // workaround is here to avoid more duplication later - auto sorted_vertices = OrderByVertices(acc, dba, vertex_accessors, req.order_by); + auto local_sorted_vertices = OrderByVertices( + acc, dba, vertex_accessors, req.order_by); // #NoCommit see whether we can avoid the extra std::transform vertex_accessors.clear(); - std::transform(sorted_vertices.begin(), sorted_vertices.end(), std::back_inserter(vertex_accessors), + std::transform(local_sorted_vertices.begin(), local_sorted_vertices.end(), std::back_inserter(vertex_accessors), [](auto &vertex) { return vertex.object_acc; }); } @@ -982,8 +985,10 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { std::optional maybe_result; if (req.order_by.empty()) { - maybe_result = GetExpandOneResult(acc, src_vertice, req, maybe_filter_based_on_edge_uniquness, edge_filler, - shard_->GetSchema(shard_->PrimaryLabel())); + auto schema = shard_->GetSchema(shard_->PrimaryLabel()); + MG_ASSERT(schema); + maybe_result = + GetExpandOneResult(acc, src_vertice, req, maybe_filter_based_on_edge_uniquness, edge_filler, *schema); } else { auto [in_edge_accessors, out_edge_accessors] = GetEdgesFromVertex(src_vertex_acc, req.direction); @@ -997,9 +1002,11 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { std::vector out_edge_ordered_accessors; std::transform(out_ordered_edges.begin(), out_ordered_edges.end(), std::back_inserter(out_edge_ordered_accessors), [](const auto &edge_element) { return edge_element.object_acc; }); - maybe_result = GetExpandOneResult(src_vertex_acc, src_vertice, req, in_edge_ordered_accessors, - out_edge_ordered_accessors, maybe_filter_based_on_edge_uniquness, edge_filler, - shard_->GetSchema(shard_->PrimaryLabel())); + auto schema = shard_->GetSchema(shard_->PrimaryLabel()); + MG_ASSERT(schema); + maybe_result = + GetExpandOneResult(src_vertex_acc, src_vertice, req, in_edge_ordered_accessors, out_edge_ordered_accessors, + maybe_filter_based_on_edge_uniquness, edge_filler, *schema); } if (!maybe_result) { From 7bdcd8f9f4bece3f74f416311fc0c63075dafa61 Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 16 Nov 2022 14:48:06 +0100 Subject: [PATCH 051/103] Add shard_error in response --- src/expr/interpret/eval.hpp | 1 + src/memgraph.cpp | 2 + src/query/v2/requests.hpp | 16 ++++ src/storage/v3/result.hpp | 36 ++++++++ src/storage/v3/shard_rsm.cpp | 156 ++++++++++++++++------------------- 5 files changed, 124 insertions(+), 87 deletions(-) diff --git a/src/expr/interpret/eval.hpp b/src/expr/interpret/eval.hpp index 04676dd7b..85911678d 100644 --- a/src/expr/interpret/eval.hpp +++ b/src/expr/interpret/eval.hpp @@ -110,6 +110,7 @@ class ExpressionEvaluator : public ExpressionVisitor { case Error::VERTEX_HAS_EDGES: case Error::PROPERTIES_DISABLED: case Error::VERTEX_ALREADY_INSERTED: + case Error::OBJECT_NOT_FOUND: throw ExpressionRuntimeException("Unexpected error when accessing {}.", accessed_object); case Error::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: case Error::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: diff --git a/src/memgraph.cpp b/src/memgraph.cpp index f553d1821..b73a522de 100644 --- a/src/memgraph.cpp +++ b/src/memgraph.cpp @@ -496,6 +496,7 @@ class BoltSession final : public memgraph::communication::bolt::Session error; std::optional next_start_id; std::vector results; }; @@ -382,6 +389,7 @@ struct GetPropertiesRequest { struct GetPropertiesResponse { bool success; + std::optional error; }; enum class EdgeDirection : uint8_t { OUT = 1, IN = 2, BOTH = 3 }; @@ -447,6 +455,7 @@ struct ExpandOneResultRow { struct ExpandOneResponse { bool success; + std::optional error; std::vector result; }; @@ -481,6 +490,7 @@ struct CreateVerticesRequest { struct CreateVerticesResponse { bool success; + std::optional error; }; struct DeleteVerticesRequest { @@ -492,6 +502,7 @@ struct DeleteVerticesRequest { struct DeleteVerticesResponse { bool success; + std::optional error; }; struct UpdateVerticesRequest { @@ -501,6 +512,7 @@ struct UpdateVerticesRequest { struct UpdateVerticesResponse { bool success; + std::optional error; }; /* @@ -523,6 +535,7 @@ struct CreateExpandRequest { struct CreateExpandResponse { bool success; + std::optional error; }; struct DeleteEdgesRequest { @@ -532,6 +545,7 @@ struct DeleteEdgesRequest { struct DeleteEdgesResponse { bool success; + std::optional error; }; struct UpdateEdgesRequest { @@ -541,6 +555,7 @@ struct UpdateEdgesRequest { struct UpdateEdgesResponse { bool success; + std::optional error; }; struct CommitRequest { @@ -550,6 +565,7 @@ struct CommitRequest { struct CommitResponse { bool success; + std::optional error; }; using ReadRequests = std::variant; diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index 4ba64cf02..f2eddc276 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -12,6 +12,7 @@ #pragma once #include +#include #include #include "utils/result.hpp" @@ -34,8 +35,43 @@ enum class ErrorCode { SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL, SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY, SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED, + + // NEW Ones + OBJECT_NOT_FOUND, // Different from NONEXISTENT_OBJECT since ine the latter it + // could be found it could have a delta that specified deletion }; +constexpr std::string_view ErrorCodeToString(const ErrorCode code) { + switch (code) { + case ErrorCode::SERIALIZATION_ERROR: + return "SERIALIZATION_ERROR"; + case ErrorCode::NONEXISTENT_OBJECT: + return "NONEXISTENT_OBJECT"; + case ErrorCode::DELETED_OBJECT: + return "DELETED_OBJECT"; + case ErrorCode::VERTEX_HAS_EDGES: + return "VERTEX_HAS_EDGES"; + case ErrorCode::PROPERTIES_DISABLED: + return "PROPERTIES_DISABLED"; + case ErrorCode::VERTEX_ALREADY_INSERTED: + return "VERTEX_ALREADY_INSERTED"; + case ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: + return "SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL"; + case ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: + return "SCHEMA_VERTEX_PROPERTY_WRONG_TYPE"; + case ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: + return "SCHEMA_VERTEX_UPDATE_PRIMARY_KEY"; + case ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: + return "SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL"; + case ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: + return "SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY"; + case ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: + return "SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED"; + case ErrorCode::OBJECT_NOT_FOUND: + return "OBJECT_NOT_FOUND"; + } +} + struct ShardError { ShardError(ErrorCode code, std::string message, std::string source) : code{code}, message{std::move(message)}, source{std::move(source)} {} diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index f3d879ef4..6f6ada082 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -469,35 +469,11 @@ EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { return edge_filler; } -void LogResultError(const ShardError &error, const std::string_view action = "") { - switch (error.code) { - case ErrorCode::DELETED_OBJECT: - spdlog::debug("{} failed with error: DELETED_OBJECT, at {}", action, error.source); - break; - case ErrorCode::NONEXISTENT_OBJECT: - spdlog::debug("{} failed with error: NONEXISTENT_OBJECT, at {}", action, error.source); - break; - case ErrorCode::SERIALIZATION_ERROR: - spdlog::debug("{} failed with error: SERIALIZATION_ERROR, at {}", action, error.source); - break; - case ErrorCode::PROPERTIES_DISABLED: - spdlog::debug("{} failed with error: PROPERTIES_DISABLED, at {}", action, error.source); - break; - case ErrorCode::VERTEX_HAS_EDGES: - spdlog::debug("{} failed with error: VERTEX_HAS_EDGES, at {}", action, error.source); - break; - case ErrorCode::VERTEX_ALREADY_INSERTED: - spdlog::debug("{} failed with error: VERTEX_ALREADY_INSERTED, at {}", action, error.source); - break; - case ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: - case ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: - case ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: - case ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: - case ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: - case ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: - spdlog::debug("Schema violation: {} at {}", error.message, error.source); - break; - } +auto CreateErrorResponse(const ShardError &shard_error, const auto transaction_id, const std::string_view action) { + msgs::ShardError message_shard_error{shard_error.code, shard_error.message}; + spdlog::debug("{} In transaction {} {} failed: {}: {}", shard_error.source, transaction_id, action, + ErrorCodeToString(shard_error.code), shard_error.message); + return message_shard_error; } }; // namespace @@ -505,6 +481,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); bool action_successful = true; + std::optional shard_error; for (auto &new_vertex : req.new_vertices) { /// TODO(gvolfing) Consider other methods than converting. Change either @@ -525,21 +502,20 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { auto result_schema = acc.CreateVertexAndValidate(converted_label_ids, transformed_pk, converted_property_map); if (result_schema.HasError()) { - auto &error = result_schema.GetError(); - spdlog::debug("Creating vertex failed with error: VERTEX_ALREADY_INSERTED"); - + shard_error.emplace(CreateErrorResponse(result_schema.GetError(), req.transaction_id, "creating vertices")); action_successful = false; break; } } - return msgs::CreateVerticesResponse{.success = action_successful}; + return msgs::CreateVerticesResponse{action_successful, std::move(shard_error)}; } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); bool action_successful = true; + std::optional shard_error; for (auto &vertex : req.new_properties) { if (!action_successful) { @@ -549,7 +525,8 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { auto vertex_to_update = acc.FindVertex(ConvertPropertyVector(std::move(vertex.primary_key)), View::OLD); if (!vertex_to_update) { action_successful = false; - spdlog::debug("Vertex could not be found while trying to update its properties. Transaction id: {}", + shard_error.emplace(msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND}); + spdlog::debug("In transaction {} vertex could not be found while trying to update its properties.", req.transaction_id.logical_id); continue; } @@ -558,68 +535,66 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { auto result_schema = vertex_to_update->SetPropertyAndValidate(update_prop.first, ToPropertyValue(std::move(update_prop.second))); if (result_schema.HasError()) { - auto &error = result_schema.GetError(); - LogResultError(error); - action_successful = false; - + shard_error.emplace(CreateErrorResponse(result_schema.GetError(), req.transaction_id, "updating vertices")); break; } } } - return msgs::UpdateVerticesResponse{.success = action_successful}; + return msgs::UpdateVerticesResponse{action_successful, std::move(shard_error)}; } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::DeleteVerticesRequest &&req) { bool action_successful = true; + std::optional shard_error; auto acc = shard_->Access(req.transaction_id); for (auto &propval : req.primary_keys) { - if (!action_successful) { - break; - } - auto vertex_acc = acc.FindVertex(ConvertPropertyVector(std::move(propval)), View::OLD); if (!vertex_acc) { spdlog::debug("Error while trying to delete vertex. Vertex to delete does not exist. Transaction id: {}", req.transaction_id.logical_id); action_successful = false; - } else { - // TODO(gvolfing) - // Since we will not have different kinds of deletion types in one transaction, - // we dont have to enter the switch statement on every iteration. Optimize this. - switch (req.deletion_type) { - case msgs::DeleteVerticesRequest::DeletionType::DELETE: { - auto result = acc.DeleteVertex(&vertex_acc.value()); - if (result.HasError() || !(result.GetValue().has_value())) { - action_successful = false; - spdlog::debug("Error while trying to delete vertex. Transaction id: {}", req.transaction_id.logical_id); - } - - break; - } - case msgs::DeleteVerticesRequest::DeletionType::DETACH_DELETE: { - auto result = acc.DetachDeleteVertex(&vertex_acc.value()); - if (result.HasError() || !(result.GetValue().has_value())) { - action_successful = false; - spdlog::debug("Error while trying to detach and delete vertex. Transaction id: {}", - req.transaction_id.logical_id); - } - - break; + shard_error.emplace(msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND}); + spdlog::debug("In transaction {} vertex could not be found while trying to delete it.", + req.transaction_id.logical_id); + break; + } + // TODO(gvolfing) + // Since we will not have different kinds of deletion types in one transaction, + // we dont have to enter the switch statement on every iteration. Optimize this. + switch (req.deletion_type) { + case msgs::DeleteVerticesRequest::DeletionType::DELETE: { + auto result = acc.DeleteVertex(&vertex_acc.value()); + if (result.HasError() || !(result.GetValue().has_value())) { + action_successful = false; + shard_error.emplace(CreateErrorResponse(result.GetError(), req.transaction_id, "deleting vertices")); } + break; } + case msgs::DeleteVerticesRequest::DeletionType::DETACH_DELETE: { + auto result = acc.DetachDeleteVertex(&vertex_acc.value()); + if (result.HasError() || !(result.GetValue().has_value())) { + action_successful = false; + shard_error.emplace(CreateErrorResponse(result.GetError(), req.transaction_id, "deleting vertices")); + } + break; + } + } + if (!action_successful) { + break; } } - return msgs::DeleteVerticesResponse{.success = action_successful}; + return msgs::DeleteVerticesResponse{action_successful, std::move(shard_error)}; } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { auto acc = shard_->Access(req.transaction_id); bool action_successful = true; + std::optional shard_error; for (auto &new_expand : req.new_expands) { const auto from_vertex_id = @@ -629,6 +604,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { VertexId{new_expand.dest_vertex.first.id, ConvertPropertyVector(std::move(new_expand.dest_vertex.second))}; if (!(shard_->IsVertexBelongToShard(from_vertex_id) || shard_->IsVertexBelongToShard(to_vertex_id))) { + // TODO Code for this action_successful = false; spdlog::debug("Error while trying to insert edge, none of the vertices belong to this shard. Transaction id: {}", req.transaction_id.logical_id); @@ -642,16 +618,17 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { for (const auto &[property, value] : new_expand.properties) { if (const auto maybe_error = edge.SetProperty(property, ToPropertyValue(value)); maybe_error.HasError()) { action_successful = false; - spdlog::debug("Setting edge property was not successful. Transaction id: {}", - req.transaction_id.logical_id); - break; - } - if (!action_successful) { + shard_error.emplace( + CreateErrorResponse(maybe_error.GetError(), req.transaction_id, "setting edge property")); break; } } + if (!action_successful) { + break; + } } } else { + // TODO Code for this action_successful = false; spdlog::debug("Creating edge was not successful. Transaction id: {}", req.transaction_id.logical_id); break; @@ -663,19 +640,19 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { auto set_result = edge_acc->SetProperty(edge_prop_key, ToPropertyValue(std::move(edge_prop_val))); if (set_result.HasError()) { action_successful = false; - spdlog::debug("Adding property to edge was not successful. Transaction id: {}", - req.transaction_id.logical_id); + shard_error.emplace(CreateErrorResponse(set_result.GetError(), req.transaction_id, "adding edge property")); break; } } } } - return msgs::CreateExpandResponse{.success = action_successful}; + return msgs::CreateExpandResponse{action_successful, std::move(shard_error)}; } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::DeleteEdgesRequest &&req) { bool action_successful = true; + std::optional shard_error; auto acc = shard_->Access(req.transaction_id); for (auto &edge : req.edges) { @@ -687,13 +664,13 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::DeleteEdgesRequest &&req) { VertexId(edge.dst.first.id, ConvertPropertyVector(std::move(edge.dst.second))), Gid::FromUint(edge.id.gid)); if (edge_acc.HasError() || !edge_acc.HasValue()) { - spdlog::debug("Error while trying to delete edge. Transaction id: {}", req.transaction_id.logical_id); action_successful = false; + shard_error.emplace(CreateErrorResponse(edge_acc.GetError(), req.transaction_id, "delete edge")); continue; } } - return msgs::DeleteEdgesResponse{.success = action_successful}; + return msgs::DeleteEdgesResponse{action_successful, std::move(shard_error)}; } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { @@ -701,6 +678,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { auto acc = shard_->Access(req.transaction_id); bool action_successful = true; + std::optional shard_error; for (auto &edge : req.new_properties) { if (!action_successful) { @@ -709,19 +687,19 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { auto vertex_acc = acc.FindVertex(ConvertPropertyVector(std::move(edge.src.second)), View::OLD); if (!vertex_acc) { + // TODO Code here action_successful = false; spdlog::debug("Encountered an error while trying to acquire VertexAccessor with transaction id: {}", req.transaction_id.logical_id); continue; } - // Since we are using the source vertex of the edge we are only intrested + // Since we are using the source vertex of the edge we are only interested // in the vertex's out-going edges auto edges_res = vertex_acc->OutEdges(View::OLD); if (edges_res.HasError()) { action_successful = false; - spdlog::debug("Encountered an error while trying to acquire EdgeAccessor with transaction id: {}", - req.transaction_id.logical_id); + shard_error.emplace(CreateErrorResponse(edges_res.GetError(), req.transaction_id, "update edge")); continue; } @@ -737,14 +715,15 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { // Check if the property was set if SetProperty does not do that itself. auto res = edge_accessor.SetProperty(key, ToPropertyValue(std::move(value))); if (res.HasError()) { - spdlog::debug("Encountered an error while trying to set the property of an Edge with transaction id: {}", - req.transaction_id.logical_id); + // TODO why not set action unsuccessful here? + shard_error.emplace(CreateErrorResponse(edges_res.GetError(), req.transaction_id, "update edge")); } } } } if (!edge_accessor_did_match) { + // TODO Code here action_successful = false; spdlog::debug("Could not find the Edge with the specified Gid. Transaction id: {}", req.transaction_id.logical_id); @@ -752,12 +731,13 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { } } - return msgs::UpdateEdgesResponse{.success = action_successful}; + return msgs::UpdateEdgesResponse{action_successful, std::move(shard_error)}; } msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); bool action_successful = true; + std::optional shard_error; std::vector results; if (req.batch_limit) { @@ -795,6 +775,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { // Vertex is separated from the properties in the response. // Is it useful to return just a vertex without the properties? if (!found_props) { + // TODO code here action_successful = false; } @@ -843,8 +824,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { } } - msgs::ScanVerticesResponse resp{}; - resp.success = action_successful; + msgs::ScanVerticesResponse resp{.success = action_successful, .error = std::move(shard_error)}; if (action_successful) { resp.next_start_id = next_start_id; @@ -857,6 +837,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { auto acc = shard_->Access(req.transaction_id); bool action_successful = true; + std::optional shard_error; std::vector results; @@ -867,6 +848,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { // Get Vertex acc auto src_vertex_acc_opt = acc.FindVertex(ConvertPropertyVector((src_vertex.second)), View::NEW); if (!src_vertex_acc_opt) { + // TODO Code error action_successful = false; spdlog::debug("Encountered an error while trying to obtain VertexAccessor. Transaction id: {}", req.transaction_id.logical_id); @@ -885,6 +867,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { shard_->GetSchema(shard_->PrimaryLabel())); if (!result) { + // Code Error action_successful = false; break; } @@ -892,8 +875,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { results.emplace_back(result.value()); } - msgs::ExpandOneResponse resp{}; - resp.success = action_successful; + msgs::ExpandOneResponse resp{.success = action_successful, .error = std::move(shard_error)}; if (action_successful) { resp.result = std::move(results); } From 9261fabe603d38968bed47a650d506ed181d34f1 Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 16 Nov 2022 17:41:43 +0100 Subject: [PATCH 052/103] Adapt for unfound object --- src/storage/v3/result.hpp | 3 +- src/storage/v3/shard_rsm.cpp | 116 +++++++++++++++++++---------------- 2 files changed, 65 insertions(+), 54 deletions(-) diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index f2eddc276..0c1289255 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -37,8 +37,7 @@ enum class ErrorCode { SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED, // NEW Ones - OBJECT_NOT_FOUND, // Different from NONEXISTENT_OBJECT since ine the latter it - // could be found it could have a delta that specified deletion + OBJECT_NOT_FOUND, }; constexpr std::string_view ErrorCodeToString(const ErrorCode code) { diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 6f6ada082..dcdd23c70 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -69,7 +69,8 @@ using AllEdgePropertiesVector = std::vector; using EdgeAccessors = std::vector; -using EdgeFiller = std::function; +using EdgeFiller = + std::function(const EdgeAccessor &edge, bool is_in_edge, msgs::ExpandOneResultRow &result_row)>; using EdgeUniqunessFunction = std::function; struct VertexIdCmpr { @@ -183,13 +184,13 @@ std::vector EvaluateVertexExpressions(DbAccessor &dba, const VertexA struct LocalError {}; -std::optional> FillUpSourceVertexSecondaryLabels(const std::optional &v_acc, - const msgs::ExpandOneRequest &req) { +ShardResult> FillUpSourceVertexSecondaryLabels(const std::optional &v_acc, + const msgs::ExpandOneRequest &req) { auto secondary_labels = v_acc->Labels(View::NEW); if (secondary_labels.HasError()) { spdlog::debug("Encountered an error while trying to get the secondary labels of a vertex. Transaction id: {}", req.transaction_id.logical_id); - return std::nullopt; + return secondary_labels.GetError(); } auto &sec_labels = secondary_labels.GetValue(); @@ -202,10 +203,10 @@ std::optional> FillUpSourceVertexSecondaryLabels(const return msgs_secondary_labels; } -std::optional> FillUpSourceVertexProperties(const std::optional &v_acc, - const msgs::ExpandOneRequest &req, - storage::v3::View view, - const Schemas::Schema *schema) { +ShardResult> FillUpSourceVertexProperties(const std::optional &v_acc, + const msgs::ExpandOneRequest &req, + storage::v3::View view, + const Schemas::Schema *schema) { std::map src_vertex_properties; if (!req.src_vertex_properties) { @@ -213,7 +214,7 @@ std::optional> FillUpSourceVertexProperties(const st if (props.HasError()) { spdlog::debug("Encountered an error while trying to access vertex properties. Transaction id: {}", req.transaction_id.logical_id); - return std::nullopt; + return props.GetError(); } for (auto &[key, val] : props.GetValue()) { @@ -232,7 +233,7 @@ std::optional> FillUpSourceVertexProperties(const st if (prop_val.HasError()) { spdlog::debug("Encountered an error while trying to access vertex properties. Transaction id: {}", req.transaction_id.logical_id); - return std::nullopt; + return prop_val.GetError(); } src_vertex_properties.insert(std::make_pair(prop, FromPropertyValueToValue(std::move(prop_val.GetValue())))); } @@ -241,7 +242,7 @@ std::optional> FillUpSourceVertexProperties(const st return src_vertex_properties; } -std::optional, 2>> FillUpConnectingEdges( +ShardResult, 2>> FillUpConnectingEdges( const std::optional &v_acc, const msgs::ExpandOneRequest &req, const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness) { std::vector edge_types{}; @@ -258,7 +259,7 @@ std::optional, 2>> FillUpConnectingEdges( if (out_edges_result.HasError()) { spdlog::debug("Encountered an error while trying to get out-going EdgeAccessors. Transaction id: {}", req.transaction_id.logical_id); - return std::nullopt; + return out_edges_result.GetError(); } out_edges = maybe_filter_based_on_edge_uniquness(std::move(out_edges_result.GetValue()), msgs::EdgeDirection::OUT); @@ -270,7 +271,7 @@ std::optional, 2>> FillUpConnectingEdges( spdlog::debug( "Encountered an error while trying to get in-going EdgeAccessors. Transaction id: {}"[req.transaction_id .logical_id]); - return std::nullopt; + return in_edges_result.GetError(); } in_edges = maybe_filter_based_on_edge_uniquness(std::move(in_edges_result.GetValue()), msgs::EdgeDirection::IN); break; @@ -280,14 +281,14 @@ std::optional, 2>> FillUpConnectingEdges( if (in_edges_result.HasError()) { spdlog::debug("Encountered an error while trying to get in-going EdgeAccessors. Transaction id: {}", req.transaction_id.logical_id); - return std::nullopt; + return in_edges_result.GetError(); } in_edges = maybe_filter_based_on_edge_uniquness(std::move(in_edges_result.GetValue()), msgs::EdgeDirection::IN); auto out_edges_result = v_acc->OutEdges(View::NEW, edge_types); if (out_edges_result.HasError()) { spdlog::debug("Encountered an error while trying to get out-going EdgeAccessors. Transaction id: {}", req.transaction_id.logical_id); - return std::nullopt; + return out_edges_result.GetError(); } out_edges = maybe_filter_based_on_edge_uniquness(std::move(out_edges_result.GetValue()), msgs::EdgeDirection::OUT); @@ -306,20 +307,21 @@ using SpecificEdgeProperties = std::tuple; using AllEdgePropertiesVector = std::vector; -using EdgeFiller = std::function; +using EdgeFiller = + std::function(const EdgeAccessor &edge, bool is_in_edge, msgs::ExpandOneResultRow &result_row)>; template -bool FillEdges(const std::vector &edges, msgs::ExpandOneResultRow &row, const EdgeFiller &edge_filler) { +ShardResult FillEdges(const std::vector &edges, msgs::ExpandOneResultRow &row, + const EdgeFiller &edge_filler) { for (const auto &edge : edges) { - if (!edge_filler(edge, are_in_edges, row)) { - return false; + if (const auto res = edge_filler(edge, are_in_edges, row); res.HasError()) { + return res.GetError(); } } - - return true; + return {}; } -std::optional GetExpandOneResult( +ShardResult GetExpandOneResult( Shard::Accessor &acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, const Schemas::Schema *schema) { @@ -328,37 +330,42 @@ std::optional GetExpandOneResult( auto v_acc = acc.FindVertex(primary_key, View::NEW); msgs::Vertex source_vertex = {.id = src_vertex}; - if (const auto maybe_secondary_labels = FillUpSourceVertexSecondaryLabels(v_acc, req); maybe_secondary_labels) { + const auto maybe_secondary_labels = FillUpSourceVertexSecondaryLabels(v_acc, req); + if (maybe_secondary_labels.HasError()) { source_vertex.labels = *maybe_secondary_labels; } else { - return std::nullopt; + return maybe_secondary_labels.GetError(); } - std::optional> src_vertex_properties; - src_vertex_properties = FillUpSourceVertexProperties(v_acc, req, storage::v3::View::NEW, schema); + auto src_vertex_properties = FillUpSourceVertexProperties(v_acc, req, storage::v3::View::NEW, schema); - if (!src_vertex_properties) { - return std::nullopt; + if (src_vertex_properties.HasError()) { + return src_vertex_properties.GetError(); } /// Fill up connecting edges auto fill_up_connecting_edges = FillUpConnectingEdges(v_acc, req, maybe_filter_based_on_edge_uniquness); - if (!fill_up_connecting_edges) { - return std::nullopt; + if (fill_up_connecting_edges.HasError()) { + return fill_up_connecting_edges.GetError(); } - auto [in_edges, out_edges] = fill_up_connecting_edges.value(); + auto [in_edges, out_edges] = fill_up_connecting_edges.GetValue(); msgs::ExpandOneResultRow result_row; result_row.src_vertex = std::move(source_vertex); result_row.src_vertex_properties = std::move(*src_vertex_properties); static constexpr bool kInEdges = true; static constexpr bool kOutEdges = false; - if (!in_edges.empty() && !FillEdges(in_edges, result_row, edge_filler)) { - return std::nullopt; + if (!in_edges.empty()) { + if (const auto fill_edges_res = FillEdges(in_edges, result_row, edge_filler); fill_edges_res.HasError()) { + return fill_edges_res.GetError(); + } } - if (!out_edges.empty() && !FillEdges(out_edges, result_row, edge_filler)) { - return std::nullopt; + if (!out_edges.empty()) { + if (const auto fill_edges_res = FillEdges(out_edges, result_row, edge_filler); + fill_edges_res.HasError()) { + return fill_edges_res.GetError(); + } } return result_row; @@ -417,12 +424,13 @@ EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { EdgeFiller edge_filler; if (!req.edge_properties) { - edge_filler = [transaction_id = req.transaction_id.logical_id](const EdgeAccessor &edge, const bool is_in_edge, - msgs::ExpandOneResultRow &result_row) -> bool { + edge_filler = [transaction_id = req.transaction_id.logical_id]( + const EdgeAccessor &edge, const bool is_in_edge, + msgs::ExpandOneResultRow &result_row) -> ShardResult { auto properties_results = edge.Properties(View::NEW); if (properties_results.HasError()) { spdlog::debug("Encountered an error while trying to get edge properties. Transaction id: {}", transaction_id); - return false; + return properties_results.GetError(); } std::map value_properties; @@ -437,12 +445,12 @@ EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { } else { result_row.out_edges_with_all_properties.push_back(std::move(edges)); } - return true; + return {}; }; } else { // TODO(gvolfing) - do we want to set the action_successful here? edge_filler = [&req](const EdgeAccessor &edge, const bool is_in_edge, - msgs::ExpandOneResultRow &result_row) -> bool { + msgs::ExpandOneResultRow &result_row) -> ShardResult { std::vector value_properties; value_properties.reserve(req.edge_properties.value().size()); for (const auto &edge_prop : req.edge_properties.value()) { @@ -450,7 +458,7 @@ EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { if (property_result.HasError()) { spdlog::debug("Encountered an error while trying to get edge properties. Transaction id: {}", req.transaction_id.logical_id); - return false; + return property_result.GetError(); } value_properties.emplace_back(FromPropertyValueToValue(std::move(property_result.GetValue()))); } @@ -462,7 +470,7 @@ EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { } else { result_row.out_edges_with_specific_properties.push_back(std::move(edges)); } - return true; + return {}; }; } @@ -604,8 +612,9 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { VertexId{new_expand.dest_vertex.first.id, ConvertPropertyVector(std::move(new_expand.dest_vertex.second))}; if (!(shard_->IsVertexBelongToShard(from_vertex_id) || shard_->IsVertexBelongToShard(to_vertex_id))) { - // TODO Code for this action_successful = false; + shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND, + "Error while trying to insert edge, none of the vertices belong to this shard"}; spdlog::debug("Error while trying to insert edge, none of the vertices belong to this shard. Transaction id: {}", req.transaction_id.logical_id); break; @@ -630,6 +639,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { } else { // TODO Code for this action_successful = false; + shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND}; spdlog::debug("Creating edge was not successful. Transaction id: {}", req.transaction_id.logical_id); break; } @@ -687,8 +697,8 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { auto vertex_acc = acc.FindVertex(ConvertPropertyVector(std::move(edge.src.second)), View::OLD); if (!vertex_acc) { - // TODO Code here action_successful = false; + shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND, "Source vertex was not found"}; spdlog::debug("Encountered an error while trying to acquire VertexAccessor with transaction id: {}", req.transaction_id.logical_id); continue; @@ -715,7 +725,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { // Check if the property was set if SetProperty does not do that itself. auto res = edge_accessor.SetProperty(key, ToPropertyValue(std::move(value))); if (res.HasError()) { - // TODO why not set action unsuccessful here? + // TODO(jbajic) why not set action unsuccessful here? shard_error.emplace(CreateErrorResponse(edges_res.GetError(), req.transaction_id, "update edge")); } } @@ -723,7 +733,8 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { } if (!edge_accessor_did_match) { - // TODO Code here + // TODO(jbajic) Do we need this + shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND, "Edge was not found"}; action_successful = false; spdlog::debug("Could not find the Edge with the specified Gid. Transaction id: {}", req.transaction_id.logical_id); @@ -775,7 +786,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { // Vertex is separated from the properties in the response. // Is it useful to return just a vertex without the properties? if (!found_props) { - // TODO code here + shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND, "Requested properties were not found!"}; action_successful = false; } @@ -848,7 +859,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { // Get Vertex acc auto src_vertex_acc_opt = acc.FindVertex(ConvertPropertyVector((src_vertex.second)), View::NEW); if (!src_vertex_acc_opt) { - // TODO Code error + shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND, "Source vertex was not found."}; action_successful = false; spdlog::debug("Encountered an error while trying to obtain VertexAccessor. Transaction id: {}", req.transaction_id.logical_id); @@ -863,16 +874,17 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { continue; } } - auto result = GetExpandOneResult(acc, src_vertex, req, maybe_filter_based_on_edge_uniquness, edge_filler, - shard_->GetSchema(shard_->PrimaryLabel())); + const auto result = GetExpandOneResult(acc, src_vertex, req, maybe_filter_based_on_edge_uniquness, edge_filler, + shard_->GetSchema(shard_->PrimaryLabel())); - if (!result) { + if (result.HasError()) { // Code Error + shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND, "Source vertex was not found."}; action_successful = false; break; } - results.emplace_back(result.value()); + results.emplace_back(result.GetValue()); } msgs::ExpandOneResponse resp{.success = action_successful, .error = std::move(shard_error)}; From 15fc3c08347e9fb46f42c74beaa9161eb8fa6a05 Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 16 Nov 2022 18:12:25 +0100 Subject: [PATCH 053/103] Fix error --- src/storage/v3/shard_rsm.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index dcdd23c70..1ea22fa8f 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -332,10 +332,9 @@ ShardResult GetExpandOneResult( msgs::Vertex source_vertex = {.id = src_vertex}; const auto maybe_secondary_labels = FillUpSourceVertexSecondaryLabels(v_acc, req); if (maybe_secondary_labels.HasError()) { - source_vertex.labels = *maybe_secondary_labels; - } else { return maybe_secondary_labels.GetError(); } + source_vertex.labels = *maybe_secondary_labels; auto src_vertex_properties = FillUpSourceVertexProperties(v_acc, req, storage::v3::View::NEW, schema); From b3ef0ccd7138968671cf3f6bc278b3072c6d26da Mon Sep 17 00:00:00 2001 From: jeremy Date: Wed, 16 Nov 2022 18:50:22 +0100 Subject: [PATCH 054/103] Moving function from shard_rsm to helper files --- src/storage/v3/request_helper.cpp | 436 +++++++++++++++++++++++++ src/storage/v3/request_helper.hpp | 41 +++ src/storage/v3/shard_rsm.cpp | 457 +-------------------------- src/storage/v3/value_conversions.hpp | 23 ++ 4 files changed, 503 insertions(+), 454 deletions(-) diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index 591d7b6d7..b5c7ea2dd 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -16,8 +16,444 @@ #include "pretty_print_ast_to_original_expression.hpp" #include "storage/v3/bindings/db_accessor.hpp" #include "storage/v3/expr.hpp" +#include "storage/v3/value_conversions.hpp" namespace memgraph::storage::v3 { +using msgs::Label; +using msgs::PropertyId; + +using conversions::ConvertPropertyMap; +using conversions::ConvertPropertyVector; +using conversions::ConvertValueVector; +using conversions::FromPropertyValueToValue; +using conversions::ToMsgsVertexId; +using conversions::ToPropertyValue; + +namespace { +namespace msgs = msgs; + +using AllEdgePropertyDataSructure = std::map; +using SpecificEdgePropertyDataSructure = std::vector; + +using AllEdgeProperties = std::tuple; +using SpecificEdgeProperties = std::tuple; + +using SpecificEdgePropertiesVector = std::vector; +using AllEdgePropertiesVector = std::vector; + +struct VertexIdCmpr { + bool operator()(const storage::v3::VertexId *lhs, const storage::v3::VertexId *rhs) const { return *lhs < *rhs; } +}; + +std::optional> PrimaryKeysFromAccessor(const VertexAccessor &acc, View view, + const Schemas::Schema &schema) { + std::map ret; + auto props = acc.Properties(view); + auto maybe_pk = acc.PrimaryKey(view); + if (maybe_pk.HasError()) { + spdlog::debug("Encountered an error while trying to get vertex primary key."); + return std::nullopt; + } + auto &pk = maybe_pk.GetValue(); + MG_ASSERT(schema.second.size() == pk.size(), "PrimaryKey size does not match schema!"); + for (size_t i{0}; i < schema.second.size(); ++i) { + ret.emplace(schema.second[i].property_id, FromPropertyValueToValue(std::move(pk[i]))); + } + + return ret; +} + +struct LocalError {}; + +std::optional> FillUpSourceVertexSecondaryLabels(const std::optional &v_acc, + const msgs::ExpandOneRequest &req) { + auto secondary_labels = v_acc->Labels(View::NEW); + if (secondary_labels.HasError()) { + spdlog::debug("Encountered an error while trying to get the secondary labels of a vertex. Transaction id: {}", + req.transaction_id.logical_id); + return std::nullopt; + } + + auto &sec_labels = secondary_labels.GetValue(); + std::vector msgs_secondary_labels; + msgs_secondary_labels.reserve(sec_labels.size()); + + std::transform(sec_labels.begin(), sec_labels.end(), std::back_inserter(msgs_secondary_labels), + [](auto label_id) { return msgs::Label{.id = label_id}; }); + + return msgs_secondary_labels; +} + +std::optional> FillUpSourceVertexProperties(const std::optional &v_acc, + const msgs::ExpandOneRequest &req, + storage::v3::View view, + const Schemas::Schema &schema) { + std::map src_vertex_properties; + + if (!req.src_vertex_properties) { + auto props = v_acc->Properties(View::NEW); + if (props.HasError()) { + spdlog::debug("Encountered an error while trying to access vertex properties. Transaction id: {}", + req.transaction_id.logical_id); + return std::nullopt; + } + + for (auto &[key, val] : props.GetValue()) { + src_vertex_properties.insert(std::make_pair(key, FromPropertyValueToValue(std::move(val)))); + } + auto pks = PrimaryKeysFromAccessor(*v_acc, view, schema); + if (pks) { + src_vertex_properties.merge(*pks); + } + + } else if (req.src_vertex_properties.value().empty()) { + // NOOP + } else { + for (const auto &prop : req.src_vertex_properties.value()) { + auto prop_val = v_acc->GetProperty(prop, View::OLD); + if (prop_val.HasError()) { + spdlog::debug("Encountered an error while trying to access vertex properties. Transaction id: {}", + req.transaction_id.logical_id); + return std::nullopt; + } + src_vertex_properties.insert(std::make_pair(prop, FromPropertyValueToValue(std::move(prop_val.GetValue())))); + } + } + + return src_vertex_properties; +} + +std::optional, 2>> FillUpConnectingEdges( + const std::optional &v_acc, const msgs::ExpandOneRequest &req, + const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness) { + std::vector edge_types{}; + edge_types.reserve(req.edge_types.size()); + std::transform(req.edge_types.begin(), req.edge_types.end(), std::back_inserter(edge_types), + [](const msgs::EdgeType &edge_type) { return edge_type.id; }); + + std::vector in_edges; + std::vector out_edges; + + switch (req.direction) { + case msgs::EdgeDirection::OUT: { + auto out_edges_result = v_acc->OutEdges(View::NEW, edge_types); + if (out_edges_result.HasError()) { + spdlog::debug("Encountered an error while trying to get out-going EdgeAccessors. Transaction id: {}", + req.transaction_id.logical_id); + return std::nullopt; + } + out_edges = + maybe_filter_based_on_edge_uniquness(std::move(out_edges_result.GetValue()), msgs::EdgeDirection::OUT); + break; + } + case msgs::EdgeDirection::IN: { + auto in_edges_result = v_acc->InEdges(View::NEW, edge_types); + if (in_edges_result.HasError()) { + spdlog::debug( + "Encountered an error while trying to get in-going EdgeAccessors. Transaction id: {}"[req.transaction_id + .logical_id]); + return std::nullopt; + } + in_edges = maybe_filter_based_on_edge_uniquness(std::move(in_edges_result.GetValue()), msgs::EdgeDirection::IN); + break; + } + case msgs::EdgeDirection::BOTH: { + auto in_edges_result = v_acc->InEdges(View::NEW, edge_types); + if (in_edges_result.HasError()) { + spdlog::debug("Encountered an error while trying to get in-going EdgeAccessors. Transaction id: {}", + req.transaction_id.logical_id); + return std::nullopt; + } + in_edges = maybe_filter_based_on_edge_uniquness(std::move(in_edges_result.GetValue()), msgs::EdgeDirection::IN); + auto out_edges_result = v_acc->OutEdges(View::NEW, edge_types); + if (out_edges_result.HasError()) { + spdlog::debug("Encountered an error while trying to get out-going EdgeAccessors. Transaction id: {}", + req.transaction_id.logical_id); + return std::nullopt; + } + out_edges = + maybe_filter_based_on_edge_uniquness(std::move(out_edges_result.GetValue()), msgs::EdgeDirection::OUT); + break; + } + } + return std::array, 2>{std::move(in_edges), std::move(out_edges)}; +} + +using AllEdgePropertyDataSructure = std::map; +using SpecificEdgePropertyDataSructure = std::vector; + +using AllEdgeProperties = std::tuple; +using SpecificEdgeProperties = std::tuple; + +using SpecificEdgePropertiesVector = std::vector; +using AllEdgePropertiesVector = std::vector; + +using EdgeFiller = std::function; + +template +bool FillEdges(const std::vector &edges, msgs::ExpandOneResultRow &row, const EdgeFiller &edge_filler) { + for (const auto &edge : edges) { + if (!edge_filler(edge, are_in_edges, row)) { + return false; + } + } + + return true; +} + +}; // namespace + +std::optional> CollectSpecificPropertiesFromAccessor(const VertexAccessor &acc, + const std::vector &props, + View view) { + std::map ret; + + for (const auto &prop : props) { + auto result = acc.GetProperty(prop, view); + if (result.HasError()) { + spdlog::debug("Encountered an Error while trying to get a vertex property."); + return std::nullopt; + } + auto &value = result.GetValue(); + ret.emplace(std::make_pair(prop, FromPropertyValueToValue(std::move(value)))); + } + + return ret; +} + +std::vector EvaluateVertexExpressions(DbAccessor &dba, const VertexAccessor &v_acc, + const std::vector &expressions, + std::string_view node_name) { + std::vector evaluated_expressions; + evaluated_expressions.reserve(expressions.size()); + + std::transform(expressions.begin(), expressions.end(), std::back_inserter(evaluated_expressions), + [&dba, &v_acc, &node_name](const auto &expression) { + return ComputeExpression(dba, v_acc, std::nullopt, expression, node_name, ""); + }); + + return evaluated_expressions; +} + +std::optional> CollectAllPropertiesFromAccessor(const VertexAccessor &acc, View view, + const Schemas::Schema &schema) { + std::map ret; + auto props = acc.Properties(view); + if (props.HasError()) { + spdlog::debug("Encountered an error while trying to get vertex properties."); + return std::nullopt; + } + + auto &properties = props.GetValue(); + std::transform(properties.begin(), properties.end(), std::inserter(ret, ret.begin()), + [](std::pair &pair) { + return std::make_pair(pair.first, FromPropertyValueToValue(std::move(pair.second))); + }); + properties.clear(); + + auto pks = PrimaryKeysFromAccessor(acc, view, schema); + if (pks) { + ret.merge(*pks); + } + + return ret; +} + +EdgeUniqunessFunction InitializeEdgeUniqunessFunction(bool only_unique_neighbor_rows) { + // Functions to select connecting edges based on uniquness + EdgeUniqunessFunction maybe_filter_based_on_edge_uniquness; + + if (only_unique_neighbor_rows) { + maybe_filter_based_on_edge_uniquness = [](EdgeAccessors &&edges, + msgs::EdgeDirection edge_direction) -> EdgeAccessors { + std::function &, const storage::v3::EdgeAccessor &)> + is_edge_unique; + switch (edge_direction) { + case msgs::EdgeDirection::OUT: { + is_edge_unique = [](std::set &other_vertex_set, + const storage::v3::EdgeAccessor &edge_acc) { + auto [it, insertion_happened] = other_vertex_set.insert(&edge_acc.ToVertex()); + return insertion_happened; + }; + break; + } + case msgs::EdgeDirection::IN: { + is_edge_unique = [](std::set &other_vertex_set, + const storage::v3::EdgeAccessor &edge_acc) { + auto [it, insertion_happened] = other_vertex_set.insert(&edge_acc.FromVertex()); + return insertion_happened; + }; + break; + } + case msgs::EdgeDirection::BOTH: + MG_ASSERT(false, "This is should never happen, msgs::EdgeDirection::BOTH should not be passed here."); + } + + EdgeAccessors ret; + std::set other_vertex_set; + + for (const auto &edge : edges) { + if (is_edge_unique(other_vertex_set, edge)) { + ret.emplace_back(edge); + } + } + + return ret; + }; + } else { + maybe_filter_based_on_edge_uniquness = + [](EdgeAccessors &&edges, msgs::EdgeDirection /*edge_direction*/) -> EdgeAccessors { return std::move(edges); }; + } + + return maybe_filter_based_on_edge_uniquness; +} + +EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { + EdgeFiller edge_filler; + + if (!req.edge_properties) { + edge_filler = [transaction_id = req.transaction_id.logical_id](const EdgeAccessor &edge, const bool is_in_edge, + msgs::ExpandOneResultRow &result_row) -> bool { + auto properties_results = edge.Properties(View::NEW); + if (properties_results.HasError()) { + spdlog::debug("Encountered an error while trying to get edge properties. Transaction id: {}", transaction_id); + return false; + } + + std::map value_properties; + for (auto &[prop_key, prop_val] : properties_results.GetValue()) { + value_properties.insert(std::make_pair(prop_key, FromPropertyValueToValue(std::move(prop_val)))); + } + using EdgeWithAllProperties = msgs::ExpandOneResultRow::EdgeWithAllProperties; + EdgeWithAllProperties edges{ToMsgsVertexId(edge.FromVertex()), msgs::EdgeType{edge.EdgeType()}, + edge.Gid().AsUint(), std::move(value_properties)}; + if (is_in_edge) { + result_row.in_edges_with_all_properties.push_back(std::move(edges)); + } else { + result_row.out_edges_with_all_properties.push_back(std::move(edges)); + } + return true; + }; + } else { + // TODO(gvolfing) - do we want to set the action_successful here? + edge_filler = [&req](const EdgeAccessor &edge, const bool is_in_edge, + msgs::ExpandOneResultRow &result_row) -> bool { + std::vector value_properties; + value_properties.reserve(req.edge_properties.value().size()); + for (const auto &edge_prop : req.edge_properties.value()) { + auto property_result = edge.GetProperty(edge_prop, View::NEW); + if (property_result.HasError()) { + spdlog::debug("Encountered an error while trying to get edge properties. Transaction id: {}", + req.transaction_id.logical_id); + return false; + } + value_properties.emplace_back(FromPropertyValueToValue(std::move(property_result.GetValue()))); + } + using EdgeWithSpecificProperties = msgs::ExpandOneResultRow::EdgeWithSpecificProperties; + EdgeWithSpecificProperties edges{ToMsgsVertexId(edge.FromVertex()), msgs::EdgeType{edge.EdgeType()}, + edge.Gid().AsUint(), std::move(value_properties)}; + if (is_in_edge) { + result_row.in_edges_with_specific_properties.push_back(std::move(edges)); + } else { + result_row.out_edges_with_specific_properties.push_back(std::move(edges)); + } + return true; + }; + } + + return edge_filler; +} + +bool FilterOnVertex(DbAccessor &dba, const storage::v3::VertexAccessor &v_acc, const std::vector &filters, + const std::string_view node_name) { + return std::ranges::all_of(filters, [&node_name, &dba, &v_acc](const auto &filter_expr) { + auto res = ComputeExpression(dba, v_acc, std::nullopt, filter_expr, node_name, ""); + return res.IsBool() && res.ValueBool(); + }); +} + +std::optional GetExpandOneResult( + Shard::Accessor &acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, + const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, + const Schemas::Schema &schema) { + /// Fill up source vertex + const auto primary_key = ConvertPropertyVector(src_vertex.second); + auto v_acc = acc.FindVertex(primary_key, View::NEW); + + msgs::Vertex source_vertex = {.id = src_vertex}; + if (const auto maybe_secondary_labels = FillUpSourceVertexSecondaryLabels(v_acc, req); maybe_secondary_labels) { + source_vertex.labels = *maybe_secondary_labels; + } else { + return std::nullopt; + } + + std::optional> src_vertex_properties; + src_vertex_properties = FillUpSourceVertexProperties(v_acc, req, storage::v3::View::NEW, schema); + + if (!src_vertex_properties) { + return std::nullopt; + } + + /// Fill up connecting edges + auto fill_up_connecting_edges = FillUpConnectingEdges(v_acc, req, maybe_filter_based_on_edge_uniquness); + if (!fill_up_connecting_edges) { + return std::nullopt; + } + + auto [in_edges, out_edges] = fill_up_connecting_edges.value(); + + msgs::ExpandOneResultRow result_row; + result_row.src_vertex = std::move(source_vertex); + result_row.src_vertex_properties = std::move(*src_vertex_properties); + static constexpr bool kInEdges = true; + static constexpr bool kOutEdges = false; + if (!in_edges.empty() && !FillEdges(in_edges, result_row, edge_filler)) { + return std::nullopt; + } + if (!out_edges.empty() && !FillEdges(out_edges, result_row, edge_filler)) { + return std::nullopt; + } + + return result_row; +} + +std::optional GetExpandOneResult( + VertexAccessor v_acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, + std::vector in_edge_accessors, std::vector out_edge_accessors, + const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, + const Schemas::Schema &schema) { + /// Fill up source vertex + msgs::Vertex source_vertex = {.id = src_vertex}; + if (const auto maybe_secondary_labels = FillUpSourceVertexSecondaryLabels(v_acc, req); maybe_secondary_labels) { + source_vertex.labels = *maybe_secondary_labels; + } else { + return std::nullopt; + } + + /// Fill up source vertex properties + auto src_vertex_properties = FillUpSourceVertexProperties(v_acc, req, storage::v3::View::NEW, schema); + if (!src_vertex_properties) { + return std::nullopt; + } + + /// Fill up connecting edges + auto in_edges = maybe_filter_based_on_edge_uniquness(std::move(in_edge_accessors), msgs::EdgeDirection::IN); + auto out_edges = maybe_filter_based_on_edge_uniquness(std::move(out_edge_accessors), msgs::EdgeDirection::OUT); + + msgs::ExpandOneResultRow result_row; + result_row.src_vertex = std::move(source_vertex); + result_row.src_vertex_properties = std::move(*src_vertex_properties); + static constexpr bool kInEdges = true; + static constexpr bool kOutEdges = false; + if (!in_edges.empty() && !FillEdges(in_edges, result_row, edge_filler)) { + return std::nullopt; + } + if (!out_edges.empty() && !FillEdges(out_edges, result_row, edge_filler)) { + return std::nullopt; + } + + return result_row; +} VerticesIterable::Iterator GetStartVertexIterator(VerticesIterable &vertex_iterable, const std::vector &start_ids, const View view) { diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 4d3018566..035cc1ae5 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -22,6 +22,11 @@ #include "storage/v3/shard.hpp" #include "storage/v3/vertex_accessor.hpp" namespace memgraph::storage::v3 { +using EdgeAccessors = std::vector; +using EdgeUniqunessFunction = std::function; +using EdgeFiller = std::function; +using msgs::Value; + template concept ObjectAccessor = std::is_same_v || std::is_same_v; @@ -194,4 +199,40 @@ std::vector>::const_iterator GetStartOrderedElementsIter std::array, 2> GetEdgesFromVertex(const VertexAccessor &vertex_accessor, msgs::EdgeDirection direction); + +bool FilterOnVertex(DbAccessor &dba, const storage::v3::VertexAccessor &v_acc, const std::vector &filters, + const std::string_view node_name); + +std::vector EvaluateVertexExpressions(DbAccessor &dba, const VertexAccessor &v_acc, + const std::vector &expressions, + std::string_view node_name); + +std::optional> CollectSpecificPropertiesFromAccessor(const VertexAccessor &acc, + const std::vector &props, + View view); + +std::optional> CollectAllPropertiesFromAccessor(const VertexAccessor &acc, View view, + const Schemas::Schema &schema); + +EdgeUniqunessFunction InitializeEdgeUniqunessFunction(bool only_unique_neighbor_rows); + +EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req); + +std::optional> CollectSpecificPropertiesFromAccessor(const VertexAccessor &acc, + const std::vector &props, + View view); + +bool FilterOnVertex(DbAccessor &dba, const storage::v3::VertexAccessor &v_acc, const std::vector &filters, + const std::string_view node_name); + +std::optional GetExpandOneResult( + Shard::Accessor &acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, + const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, + const Schemas::Schema &schema); + +std::optional GetExpandOneResult( + VertexAccessor v_acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, + std::vector in_edge_accessors, std::vector out_edge_accessors, + const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, + const Schemas::Schema &schema); } // namespace memgraph::storage::v3 diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index bf31e8ced..841b64e44 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -45,469 +45,18 @@ #include "utils/logging.hpp" namespace memgraph::storage::v3 { -using msgs::Label; +using msgs::Label; // #NoCommit not needed? using msgs::PropertyId; using msgs::Value; +using conversions::ConvertPropertyMap; using conversions::ConvertPropertyVector; using conversions::ConvertValueVector; +using conversions::FromMap; using conversions::FromPropertyValueToValue; using conversions::ToMsgsVertexId; using conversions::ToPropertyValue; -namespace { -namespace msgs = msgs; - -using AllEdgePropertyDataSructure = std::map; -using SpecificEdgePropertyDataSructure = std::vector; - -using AllEdgeProperties = std::tuple; -using SpecificEdgeProperties = std::tuple; - -using SpecificEdgePropertiesVector = std::vector; -using AllEdgePropertiesVector = std::vector; - -using EdgeAccessors = std::vector; - -using EdgeFiller = std::function; -using EdgeUniqunessFunction = std::function; - -struct VertexIdCmpr { - bool operator()(const storage::v3::VertexId *lhs, const storage::v3::VertexId *rhs) const { return *lhs < *rhs; } -}; - -std::vector> ConvertPropertyMap( - std::vector> &&properties) { - std::vector> ret; - ret.reserve(properties.size()); - - std::transform(std::make_move_iterator(properties.begin()), std::make_move_iterator(properties.end()), - std::back_inserter(ret), [](std::pair &&property) { - return std::make_pair(property.first, ToPropertyValue(std::move(property.second))); - }); - - return ret; -} - -std::vector> FromMap(const std::map &properties) { - std::vector> ret; - ret.reserve(properties.size()); - - std::transform(properties.begin(), properties.end(), std::back_inserter(ret), - [](const auto &property) { return std::make_pair(property.first, property.second); }); - - return ret; -} - -std::optional> CollectSpecificPropertiesFromAccessor(const VertexAccessor &acc, - const std::vector &props, - View view) { - std::map ret; - - for (const auto &prop : props) { - auto result = acc.GetProperty(prop, view); - if (result.HasError()) { - spdlog::debug("Encountered an Error while trying to get a vertex property."); - return std::nullopt; - } - auto &value = result.GetValue(); - ret.emplace(std::make_pair(prop, FromPropertyValueToValue(std::move(value)))); - } - - return ret; -} - -std::optional> PrimaryKeysFromAccessor(const VertexAccessor &acc, View view, - const Schemas::Schema &schema) { - std::map ret; - auto props = acc.Properties(view); - auto maybe_pk = acc.PrimaryKey(view); - if (maybe_pk.HasError()) { - spdlog::debug("Encountered an error while trying to get vertex primary key."); - return std::nullopt; - } - auto &pk = maybe_pk.GetValue(); - MG_ASSERT(schema.second.size() == pk.size(), "PrimaryKey size does not match schema!"); - for (size_t i{0}; i < schema.second.size(); ++i) { - ret.emplace(schema.second[i].property_id, FromPropertyValueToValue(std::move(pk[i]))); - } - - return ret; -} - -std::optional> CollectAllPropertiesFromAccessor(const VertexAccessor &acc, View view, - const Schemas::Schema &schema) { - std::map ret; - auto props = acc.Properties(view); - if (props.HasError()) { - spdlog::debug("Encountered an error while trying to get vertex properties."); - return std::nullopt; - } - - auto &properties = props.GetValue(); - std::transform(properties.begin(), properties.end(), std::inserter(ret, ret.begin()), - [](std::pair &pair) { - return std::make_pair(pair.first, FromPropertyValueToValue(std::move(pair.second))); - }); - properties.clear(); - - auto pks = PrimaryKeysFromAccessor(acc, view, schema); - if (pks) { - ret.merge(*pks); - } - - return ret; -} - -bool FilterOnVertex(DbAccessor &dba, const storage::v3::VertexAccessor &v_acc, const std::vector &filters, - const std::string_view node_name) { - return std::ranges::all_of(filters, [&node_name, &dba, &v_acc](const auto &filter_expr) { - auto res = ComputeExpression(dba, v_acc, std::nullopt, filter_expr, node_name, ""); - return res.IsBool() && res.ValueBool(); - }); -} - -std::vector EvaluateVertexExpressions(DbAccessor &dba, const VertexAccessor &v_acc, - const std::vector &expressions, - std::string_view node_name) { - std::vector evaluated_expressions; - evaluated_expressions.reserve(expressions.size()); - - std::transform(expressions.begin(), expressions.end(), std::back_inserter(evaluated_expressions), - [&dba, &v_acc, &node_name](const auto &expression) { - return ComputeExpression(dba, v_acc, std::nullopt, expression, node_name, ""); - }); - - return evaluated_expressions; -} - -struct LocalError {}; - -std::optional> FillUpSourceVertexSecondaryLabels(const std::optional &v_acc, - const msgs::ExpandOneRequest &req) { - auto secondary_labels = v_acc->Labels(View::NEW); - if (secondary_labels.HasError()) { - spdlog::debug("Encountered an error while trying to get the secondary labels of a vertex. Transaction id: {}", - req.transaction_id.logical_id); - return std::nullopt; - } - - auto &sec_labels = secondary_labels.GetValue(); - std::vector msgs_secondary_labels; - msgs_secondary_labels.reserve(sec_labels.size()); - - std::transform(sec_labels.begin(), sec_labels.end(), std::back_inserter(msgs_secondary_labels), - [](auto label_id) { return msgs::Label{.id = label_id}; }); - - return msgs_secondary_labels; -} - -std::optional> FillUpSourceVertexProperties(const std::optional &v_acc, - const msgs::ExpandOneRequest &req, - storage::v3::View view, - const Schemas::Schema &schema) { - std::map src_vertex_properties; - - if (!req.src_vertex_properties) { - auto props = v_acc->Properties(View::NEW); - if (props.HasError()) { - spdlog::debug("Encountered an error while trying to access vertex properties. Transaction id: {}", - req.transaction_id.logical_id); - return std::nullopt; - } - - for (auto &[key, val] : props.GetValue()) { - src_vertex_properties.insert(std::make_pair(key, FromPropertyValueToValue(std::move(val)))); - } - auto pks = PrimaryKeysFromAccessor(*v_acc, view, schema); - if (pks) { - src_vertex_properties.merge(*pks); - } - - } else if (req.src_vertex_properties.value().empty()) { - // NOOP - } else { - for (const auto &prop : req.src_vertex_properties.value()) { - auto prop_val = v_acc->GetProperty(prop, View::OLD); - if (prop_val.HasError()) { - spdlog::debug("Encountered an error while trying to access vertex properties. Transaction id: {}", - req.transaction_id.logical_id); - return std::nullopt; - } - src_vertex_properties.insert(std::make_pair(prop, FromPropertyValueToValue(std::move(prop_val.GetValue())))); - } - } - - return src_vertex_properties; -} - -std::optional, 2>> FillUpConnectingEdges( - const std::optional &v_acc, const msgs::ExpandOneRequest &req, - const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness) { - std::vector edge_types{}; - edge_types.reserve(req.edge_types.size()); - std::transform(req.edge_types.begin(), req.edge_types.end(), std::back_inserter(edge_types), - [](const msgs::EdgeType &edge_type) { return edge_type.id; }); - - std::vector in_edges; - std::vector out_edges; - - switch (req.direction) { - case msgs::EdgeDirection::OUT: { - auto out_edges_result = v_acc->OutEdges(View::NEW, edge_types); - if (out_edges_result.HasError()) { - spdlog::debug("Encountered an error while trying to get out-going EdgeAccessors. Transaction id: {}", - req.transaction_id.logical_id); - return std::nullopt; - } - out_edges = - maybe_filter_based_on_edge_uniquness(std::move(out_edges_result.GetValue()), msgs::EdgeDirection::OUT); - break; - } - case msgs::EdgeDirection::IN: { - auto in_edges_result = v_acc->InEdges(View::NEW, edge_types); - if (in_edges_result.HasError()) { - spdlog::debug( - "Encountered an error while trying to get in-going EdgeAccessors. Transaction id: {}"[req.transaction_id - .logical_id]); - return std::nullopt; - } - in_edges = maybe_filter_based_on_edge_uniquness(std::move(in_edges_result.GetValue()), msgs::EdgeDirection::IN); - break; - } - case msgs::EdgeDirection::BOTH: { - auto in_edges_result = v_acc->InEdges(View::NEW, edge_types); - if (in_edges_result.HasError()) { - spdlog::debug("Encountered an error while trying to get in-going EdgeAccessors. Transaction id: {}", - req.transaction_id.logical_id); - return std::nullopt; - } - in_edges = maybe_filter_based_on_edge_uniquness(std::move(in_edges_result.GetValue()), msgs::EdgeDirection::IN); - auto out_edges_result = v_acc->OutEdges(View::NEW, edge_types); - if (out_edges_result.HasError()) { - spdlog::debug("Encountered an error while trying to get out-going EdgeAccessors. Transaction id: {}", - req.transaction_id.logical_id); - return std::nullopt; - } - out_edges = - maybe_filter_based_on_edge_uniquness(std::move(out_edges_result.GetValue()), msgs::EdgeDirection::OUT); - break; - } - } - return std::array, 2>{std::move(in_edges), std::move(out_edges)}; -} - -using AllEdgePropertyDataSructure = std::map; -using SpecificEdgePropertyDataSructure = std::vector; - -using AllEdgeProperties = std::tuple; -using SpecificEdgeProperties = std::tuple; - -using SpecificEdgePropertiesVector = std::vector; -using AllEdgePropertiesVector = std::vector; - -using EdgeFiller = std::function; - -template -bool FillEdges(const std::vector &edges, msgs::ExpandOneResultRow &row, const EdgeFiller &edge_filler) { - for (const auto &edge : edges) { - if (!edge_filler(edge, are_in_edges, row)) { - return false; - } - } - - return true; -} - -std::optional GetExpandOneResult( - Shard::Accessor &acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, - const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, - const Schemas::Schema &schema) { - /// Fill up source vertex - const auto primary_key = ConvertPropertyVector(src_vertex.second); - auto v_acc = acc.FindVertex(primary_key, View::NEW); - - msgs::Vertex source_vertex = {.id = src_vertex}; - if (const auto maybe_secondary_labels = FillUpSourceVertexSecondaryLabels(v_acc, req); maybe_secondary_labels) { - source_vertex.labels = *maybe_secondary_labels; - } else { - return std::nullopt; - } - - std::optional> src_vertex_properties; - src_vertex_properties = FillUpSourceVertexProperties(v_acc, req, storage::v3::View::NEW, schema); - - if (!src_vertex_properties) { - return std::nullopt; - } - - /// Fill up connecting edges - auto fill_up_connecting_edges = FillUpConnectingEdges(v_acc, req, maybe_filter_based_on_edge_uniquness); - if (!fill_up_connecting_edges) { - return std::nullopt; - } - - auto [in_edges, out_edges] = fill_up_connecting_edges.value(); - - msgs::ExpandOneResultRow result_row; - result_row.src_vertex = std::move(source_vertex); - result_row.src_vertex_properties = std::move(*src_vertex_properties); - static constexpr bool kInEdges = true; - static constexpr bool kOutEdges = false; - if (!in_edges.empty() && !FillEdges(in_edges, result_row, edge_filler)) { - return std::nullopt; - } - if (!out_edges.empty() && !FillEdges(out_edges, result_row, edge_filler)) { - return std::nullopt; - } - - return result_row; -} - -std::optional GetExpandOneResult( - VertexAccessor v_acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, - std::vector in_edge_accessors, std::vector out_edge_accessors, - const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, - const Schemas::Schema &schema) { - /// Fill up source vertex - msgs::Vertex source_vertex = {.id = src_vertex}; - if (const auto maybe_secondary_labels = FillUpSourceVertexSecondaryLabels(v_acc, req); maybe_secondary_labels) { - source_vertex.labels = *maybe_secondary_labels; - } else { - return std::nullopt; - } - - /// Fill up source vertex properties - auto src_vertex_properties = FillUpSourceVertexProperties(v_acc, req, storage::v3::View::NEW, schema); - if (!src_vertex_properties) { - return std::nullopt; - } - - /// Fill up connecting edges - auto in_edges = maybe_filter_based_on_edge_uniquness(std::move(in_edge_accessors), msgs::EdgeDirection::IN); - auto out_edges = maybe_filter_based_on_edge_uniquness(std::move(out_edge_accessors), msgs::EdgeDirection::OUT); - - msgs::ExpandOneResultRow result_row; - result_row.src_vertex = std::move(source_vertex); - result_row.src_vertex_properties = std::move(*src_vertex_properties); - static constexpr bool kInEdges = true; - static constexpr bool kOutEdges = false; - if (!in_edges.empty() && !FillEdges(in_edges, result_row, edge_filler)) { - return std::nullopt; - } - if (!out_edges.empty() && !FillEdges(out_edges, result_row, edge_filler)) { - return std::nullopt; - } - - return result_row; -} - -EdgeUniqunessFunction InitializeEdgeUniqunessFunction(bool only_unique_neighbor_rows) { - // Functions to select connecting edges based on uniquness - EdgeUniqunessFunction maybe_filter_based_on_edge_uniquness; - - if (only_unique_neighbor_rows) { - maybe_filter_based_on_edge_uniquness = [](EdgeAccessors &&edges, - msgs::EdgeDirection edge_direction) -> EdgeAccessors { - std::function &, const storage::v3::EdgeAccessor &)> - is_edge_unique; - switch (edge_direction) { - case msgs::EdgeDirection::OUT: { - is_edge_unique = [](std::set &other_vertex_set, - const storage::v3::EdgeAccessor &edge_acc) { - auto [it, insertion_happened] = other_vertex_set.insert(&edge_acc.ToVertex()); - return insertion_happened; - }; - break; - } - case msgs::EdgeDirection::IN: { - is_edge_unique = [](std::set &other_vertex_set, - const storage::v3::EdgeAccessor &edge_acc) { - auto [it, insertion_happened] = other_vertex_set.insert(&edge_acc.FromVertex()); - return insertion_happened; - }; - break; - } - case msgs::EdgeDirection::BOTH: - MG_ASSERT(false, "This is should never happen, msgs::EdgeDirection::BOTH should not be passed here."); - } - - EdgeAccessors ret; - std::set other_vertex_set; - - for (const auto &edge : edges) { - if (is_edge_unique(other_vertex_set, edge)) { - ret.emplace_back(edge); - } - } - - return ret; - }; - } else { - maybe_filter_based_on_edge_uniquness = - [](EdgeAccessors &&edges, msgs::EdgeDirection /*edge_direction*/) -> EdgeAccessors { return std::move(edges); }; - } - - return maybe_filter_based_on_edge_uniquness; -} - -EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { - EdgeFiller edge_filler; - - if (!req.edge_properties) { - edge_filler = [transaction_id = req.transaction_id.logical_id](const EdgeAccessor &edge, const bool is_in_edge, - msgs::ExpandOneResultRow &result_row) -> bool { - auto properties_results = edge.Properties(View::NEW); - if (properties_results.HasError()) { - spdlog::debug("Encountered an error while trying to get edge properties. Transaction id: {}", transaction_id); - return false; - } - - std::map value_properties; - for (auto &[prop_key, prop_val] : properties_results.GetValue()) { - value_properties.insert(std::make_pair(prop_key, FromPropertyValueToValue(std::move(prop_val)))); - } - using EdgeWithAllProperties = msgs::ExpandOneResultRow::EdgeWithAllProperties; - EdgeWithAllProperties edges{ToMsgsVertexId(edge.FromVertex()), msgs::EdgeType{edge.EdgeType()}, - edge.Gid().AsUint(), std::move(value_properties)}; - if (is_in_edge) { - result_row.in_edges_with_all_properties.push_back(std::move(edges)); - } else { - result_row.out_edges_with_all_properties.push_back(std::move(edges)); - } - return true; - }; - } else { - // TODO(gvolfing) - do we want to set the action_successful here? - edge_filler = [&req](const EdgeAccessor &edge, const bool is_in_edge, - msgs::ExpandOneResultRow &result_row) -> bool { - std::vector value_properties; - value_properties.reserve(req.edge_properties.value().size()); - for (const auto &edge_prop : req.edge_properties.value()) { - auto property_result = edge.GetProperty(edge_prop, View::NEW); - if (property_result.HasError()) { - spdlog::debug("Encountered an error while trying to get edge properties. Transaction id: {}", - req.transaction_id.logical_id); - return false; - } - value_properties.emplace_back(FromPropertyValueToValue(std::move(property_result.GetValue()))); - } - using EdgeWithSpecificProperties = msgs::ExpandOneResultRow::EdgeWithSpecificProperties; - EdgeWithSpecificProperties edges{ToMsgsVertexId(edge.FromVertex()), msgs::EdgeType{edge.EdgeType()}, - edge.Gid().AsUint(), std::move(value_properties)}; - if (is_in_edge) { - result_row.in_edges_with_specific_properties.push_back(std::move(edges)); - } else { - result_row.out_edges_with_specific_properties.push_back(std::move(edges)); - } - return true; - }; - } - - return edge_filler; -} - -}; // namespace msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); diff --git a/src/storage/v3/value_conversions.hpp b/src/storage/v3/value_conversions.hpp index 05fd1394b..80b6f02b9 100644 --- a/src/storage/v3/value_conversions.hpp +++ b/src/storage/v3/value_conversions.hpp @@ -129,4 +129,27 @@ inline std::vector ConvertValueVector(const std::vector> ConvertPropertyMap( + std::vector> &&properties) { + std::vector> ret; + ret.reserve(properties.size()); + + std::transform(std::make_move_iterator(properties.begin()), std::make_move_iterator(properties.end()), + std::back_inserter(ret), [](std::pair &&property) { + return std::make_pair(property.first, ToPropertyValue(std::move(property.second))); + }); + + return ret; +} + +inline std::vector> FromMap(const std::map &properties) { + std::vector> ret; + ret.reserve(properties.size()); + + std::transform(properties.begin(), properties.end(), std::back_inserter(ret), + [](const auto &property) { return std::make_pair(property.first, property.second); }); + + return ret; +} } // namespace memgraph::storage::conversions From c4e22ffde3606b92a26cbeb351d04fca8f649b38 Mon Sep 17 00:00:00 2001 From: jeremy Date: Wed, 16 Nov 2022 18:51:57 +0100 Subject: [PATCH 055/103] Remove unnecessary tag --- src/storage/v3/shard_rsm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 841b64e44..5bb925fa8 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -45,7 +45,7 @@ #include "utils/logging.hpp" namespace memgraph::storage::v3 { -using msgs::Label; // #NoCommit not needed? +using msgs::Label; using msgs::PropertyId; using msgs::Value; From e98ef634decaaccd2ea3ebf39c97eb5856f3b4ee Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 16 Nov 2022 21:03:36 +0100 Subject: [PATCH 056/103] Ignore warning --- src/storage/v3/result.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index 0c1289255..158a1eab7 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -11,6 +11,7 @@ #pragma once +#include #include #include #include @@ -21,7 +22,7 @@ namespace memgraph::storage::v3 { static_assert(std::is_same_v); -enum class ErrorCode { +enum class ErrorCode : uint8_t { SERIALIZATION_ERROR, NONEXISTENT_OBJECT, DELETED_OBJECT, @@ -36,7 +37,6 @@ enum class ErrorCode { SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY, SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED, - // NEW Ones OBJECT_NOT_FOUND, }; @@ -85,6 +85,7 @@ struct ShardError { inline friend bool operator==(const ShardError &lhs, const ShardError &rhs) { return lhs.code == rhs.code; } }; +// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define SHARD_ERROR(...) memgraph::storage::v3::ShardError(__VA_ARGS__, fmt::format("{}:{}", __FILE__, __LINE__)) template From ec4804b72a06c4aa1867fadc973a0eea4ab5f8b3 Mon Sep 17 00:00:00 2001 From: jbajic Date: Thu, 17 Nov 2022 11:03:03 +0100 Subject: [PATCH 057/103] Move ErrorCode to common --- src/common/errors.hpp | 68 ++++++++++++++++ src/glue/v2/communication.cpp | 4 +- src/memgraph.cpp | 53 ++++++------ src/query/v2/bindings/eval.hpp | 8 +- src/query/v2/plan/operator.cpp | 24 +++--- src/query/v2/requests.hpp | 2 +- src/storage/v3/bindings/eval.hpp | 2 +- src/storage/v3/edge_accessor.cpp | 20 ++--- src/storage/v3/result.hpp | 56 +------------ src/storage/v3/schema_validator.cpp | 12 +-- src/storage/v3/shard.cpp | 27 ++++--- src/storage/v3/shard_rsm.cpp | 18 ++--- src/storage/v3/vertex_accessor.cpp | 64 +++++++-------- tests/unit/storage_v3.cpp | 93 +++++++++++----------- tests/unit/storage_v3_edge.cpp | 10 +-- tests/unit/storage_v3_schema.cpp | 23 +++--- tests/unit/storage_v3_vertex_accessors.cpp | 13 +-- 17 files changed, 260 insertions(+), 237 deletions(-) create mode 100644 src/common/errors.hpp diff --git a/src/common/errors.hpp b/src/common/errors.hpp new file mode 100644 index 000000000..1a2e0ce85 --- /dev/null +++ b/src/common/errors.hpp @@ -0,0 +1,68 @@ +// Copyright 2022 Memgraph Ltd. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source +// License, and you may not use this file except in compliance with the Business Source License. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +#pragma once + +#include +#include + +namespace memgraph::common { + +enum class ErrorCode : uint8_t { + SERIALIZATION_ERROR, + NONEXISTENT_OBJECT, + DELETED_OBJECT, + VERTEX_HAS_EDGES, + PROPERTIES_DISABLED, + VERTEX_ALREADY_INSERTED, + // Schema Violations + SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL, + SCHEMA_VERTEX_PROPERTY_WRONG_TYPE, + SCHEMA_VERTEX_UPDATE_PRIMARY_KEY, + SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL, + SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY, + SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED, + + OBJECT_NOT_FOUND, +}; + +constexpr std::string_view ErrorCodeToString(const ErrorCode code) { + switch (code) { + case ErrorCode::SERIALIZATION_ERROR: + return "SERIALIZATION_ERROR"; + case ErrorCode::NONEXISTENT_OBJECT: + return "NONEXISTENT_OBJECT"; + case ErrorCode::DELETED_OBJECT: + return "DELETED_OBJECT"; + case ErrorCode::VERTEX_HAS_EDGES: + return "VERTEX_HAS_EDGES"; + case ErrorCode::PROPERTIES_DISABLED: + return "PROPERTIES_DISABLED"; + case ErrorCode::VERTEX_ALREADY_INSERTED: + return "VERTEX_ALREADY_INSERTED"; + case ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: + return "SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL"; + case ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: + return "SCHEMA_VERTEX_PROPERTY_WRONG_TYPE"; + case ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: + return "SCHEMA_VERTEX_UPDATE_PRIMARY_KEY"; + case ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: + return "SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL"; + case ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: + return "SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY"; + case ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: + return "SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED"; + case ErrorCode::OBJECT_NOT_FOUND: + return "OBJECT_NOT_FOUND"; + } +} + +} // namespace memgraph::common diff --git a/src/glue/v2/communication.cpp b/src/glue/v2/communication.cpp index 495ce1698..0b185d19b 100644 --- a/src/glue/v2/communication.cpp +++ b/src/glue/v2/communication.cpp @@ -15,13 +15,13 @@ #include #include +#include "common/errors.hpp" #include "coordinator/shard_map.hpp" #include "query/v2/accessors.hpp" #include "query/v2/requests.hpp" #include "query/v2/shard_request_manager.hpp" #include "storage/v3/edge_accessor.hpp" #include "storage/v3/id_types.hpp" -#include "storage/v3/result.hpp" #include "storage/v3/shard.hpp" #include "storage/v3/vertex_accessor.hpp" #include "storage/v3/view.hpp" @@ -112,7 +112,7 @@ storage::v3::ShardResult ToBoltPath( const query::v2::accessors::Path & /*edge*/, const msgs::ShardRequestManagerInterface * /*shard_request_manager*/, storage::v3::View /*view*/) { // TODO(jbajic) Fix bolt communication - return {SHARD_ERROR(storage::v3::ErrorCode::DELETED_OBJECT)}; + return {SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)}; } storage::v3::ShardResult ToBoltValue(const query::v2::TypedValue &value, diff --git a/src/memgraph.cpp b/src/memgraph.cpp index b73a522de..23582e9ea 100644 --- a/src/memgraph.cpp +++ b/src/memgraph.cpp @@ -33,6 +33,7 @@ #include #include +#include "common/errors.hpp" #include "communication/bolt/v1/constants.hpp" #include "communication/websocket/auth.hpp" #include "communication/websocket/server.hpp" @@ -484,19 +485,19 @@ class BoltSession final : public memgraph::communication::bolt::Session; +using ExpressionEvaluator = memgraph::expr::ExpressionEvaluator< + TypedValue, memgraph::query::v2::EvaluationContext, memgraph::msgs::ShardRequestManagerInterface, storage::v3::View, + storage::v3::LabelId, msgs::Value, detail::Callable, common::ErrorCode, memgraph::expr::QueryEngineTag>; } // namespace memgraph::query::v2 diff --git a/src/query/v2/plan/operator.cpp b/src/query/v2/plan/operator.cpp index 5e6e8bc9a..59e1c6d68 100644 --- a/src/query/v2/plan/operator.cpp +++ b/src/query/v2/plan/operator.cpp @@ -26,6 +26,7 @@ #include #include +#include "common/errors.hpp" #include "expr/ast/pretty_print_ast_to_original_expression.hpp" #include "expr/exceptions.hpp" #include "query/exceptions.hpp" @@ -42,7 +43,6 @@ #include "query/v2/shard_request_manager.hpp" #include "storage/v3/conversions.hpp" #include "storage/v3/property_value.hpp" -#include "storage/v3/result.hpp" #include "utils/algorithm.hpp" #include "utils/csv_parsing.hpp" #include "utils/event_counter.hpp" @@ -570,20 +570,20 @@ template auto UnwrapEdgesResult(storage::v3::ShardResult &&result) { if (result.HasError()) { switch (result.GetError().code) { - case storage::v3::ErrorCode::DELETED_OBJECT: + case common::ErrorCode::DELETED_OBJECT: throw QueryRuntimeException("Trying to get relationships of a deleted node."); - case storage::v3::ErrorCode::NONEXISTENT_OBJECT: + case common::ErrorCode::NONEXISTENT_OBJECT: throw query::v2::QueryRuntimeException("Trying to get relationships from a node that doesn't exist."); - case storage::v3::ErrorCode::VERTEX_HAS_EDGES: - case storage::v3::ErrorCode::SERIALIZATION_ERROR: - case storage::v3::ErrorCode::PROPERTIES_DISABLED: + case common::ErrorCode::VERTEX_HAS_EDGES: + case common::ErrorCode::SERIALIZATION_ERROR: + case common::ErrorCode::PROPERTIES_DISABLED: throw QueryRuntimeException("Unexpected error when accessing relationships."); - case storage::v3::ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: - case storage::v3::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: - case storage::v3::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: - case storage::v3::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: - case storage::v3::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: - case storage::v3::ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: + case common::ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: + case common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: + case common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: + case common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: + case common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: + case common::ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: throw QueryRuntimeException("SchemaViolation occurred when accessing relationships."); } } diff --git a/src/query/v2/requests.hpp b/src/query/v2/requests.hpp index ec2154a7d..62a4e93f8 100644 --- a/src/query/v2/requests.hpp +++ b/src/query/v2/requests.hpp @@ -319,7 +319,7 @@ struct Value { }; struct ShardError { - storage::v3::ErrorCode code; + common::ErrorCode code; std::string message; }; diff --git a/src/storage/v3/bindings/eval.hpp b/src/storage/v3/bindings/eval.hpp index 063705806..c3024e006 100644 --- a/src/storage/v3/bindings/eval.hpp +++ b/src/storage/v3/bindings/eval.hpp @@ -88,6 +88,6 @@ struct EvaluationContext { using ExpressionEvaluator = memgraph::expr::ExpressionEvaluator; + common::ErrorCode>; } // namespace memgraph::storage::v3 diff --git a/src/storage/v3/edge_accessor.cpp b/src/storage/v3/edge_accessor.cpp index 95dd6875a..4438e0acb 100644 --- a/src/storage/v3/edge_accessor.cpp +++ b/src/storage/v3/edge_accessor.cpp @@ -57,11 +57,11 @@ const VertexId &EdgeAccessor::ToVertex() const { return to_vertex_; } ShardResult EdgeAccessor::SetProperty(PropertyId property, const PropertyValue &value) { utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!config_.properties_on_edges) return SHARD_ERROR(ErrorCode::PROPERTIES_DISABLED); + if (!config_.properties_on_edges) return SHARD_ERROR(common::ErrorCode::PROPERTIES_DISABLED); - if (!PrepareForWrite(transaction_, edge_.ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, edge_.ptr)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); - if (edge_.ptr->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (edge_.ptr->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); auto current_value = edge_.ptr->properties.GetProperty(property); // We could skip setting the value if the previous one is the same to the new @@ -77,11 +77,11 @@ ShardResult EdgeAccessor::SetProperty(PropertyId property, const } ShardResult> EdgeAccessor::ClearProperties() { - if (!config_.properties_on_edges) return SHARD_ERROR(ErrorCode::PROPERTIES_DISABLED); + if (!config_.properties_on_edges) return SHARD_ERROR(common::ErrorCode::PROPERTIES_DISABLED); - if (!PrepareForWrite(transaction_, edge_.ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, edge_.ptr)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); - if (edge_.ptr->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (edge_.ptr->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); auto properties = edge_.ptr->properties.Properties(); for (const auto &property : properties) { @@ -129,8 +129,8 @@ ShardResult EdgeAccessor::GetProperty(PropertyId property, View v break; } }); - if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); return std::move(value); } @@ -175,8 +175,8 @@ ShardResult> EdgeAccessor::Properties(View v break; } }); - if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); return std::move(properties); } diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index 158a1eab7..c9c0c8a4b 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -16,68 +16,20 @@ #include #include +#include "common/errors.hpp" #include "utils/result.hpp" namespace memgraph::storage::v3 { static_assert(std::is_same_v); -enum class ErrorCode : uint8_t { - SERIALIZATION_ERROR, - NONEXISTENT_OBJECT, - DELETED_OBJECT, - VERTEX_HAS_EDGES, - PROPERTIES_DISABLED, - VERTEX_ALREADY_INSERTED, - // Schema Violations - SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL, - SCHEMA_VERTEX_PROPERTY_WRONG_TYPE, - SCHEMA_VERTEX_UPDATE_PRIMARY_KEY, - SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL, - SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY, - SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED, - - OBJECT_NOT_FOUND, -}; - -constexpr std::string_view ErrorCodeToString(const ErrorCode code) { - switch (code) { - case ErrorCode::SERIALIZATION_ERROR: - return "SERIALIZATION_ERROR"; - case ErrorCode::NONEXISTENT_OBJECT: - return "NONEXISTENT_OBJECT"; - case ErrorCode::DELETED_OBJECT: - return "DELETED_OBJECT"; - case ErrorCode::VERTEX_HAS_EDGES: - return "VERTEX_HAS_EDGES"; - case ErrorCode::PROPERTIES_DISABLED: - return "PROPERTIES_DISABLED"; - case ErrorCode::VERTEX_ALREADY_INSERTED: - return "VERTEX_ALREADY_INSERTED"; - case ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: - return "SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL"; - case ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: - return "SCHEMA_VERTEX_PROPERTY_WRONG_TYPE"; - case ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: - return "SCHEMA_VERTEX_UPDATE_PRIMARY_KEY"; - case ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: - return "SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL"; - case ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: - return "SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY"; - case ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: - return "SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED"; - case ErrorCode::OBJECT_NOT_FOUND: - return "OBJECT_NOT_FOUND"; - } -} - struct ShardError { - ShardError(ErrorCode code, std::string message, std::string source) + ShardError(common::ErrorCode code, std::string message, std::string source) : code{code}, message{std::move(message)}, source{std::move(source)} {} - ShardError(ErrorCode code, std::string source) : code{code}, source{std::move(source)} {} + ShardError(common::ErrorCode code, std::string source) : code{code}, source{std::move(source)} {} - ErrorCode code; + common::ErrorCode code; // TODO Maybe add category std::string message; std::string source; diff --git a/src/storage/v3/schema_validator.cpp b/src/storage/v3/schema_validator.cpp index 64df35a7e..97c068524 100644 --- a/src/storage/v3/schema_validator.cpp +++ b/src/storage/v3/schema_validator.cpp @@ -31,14 +31,14 @@ std::optional SchemaValidator::ValidateVertexCreate( // Schema on primary label const auto *schema = schemas_->GetSchema(primary_label); if (schema == nullptr) { - return SHARD_ERROR(ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL, + return SHARD_ERROR(common::ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL, fmt::format("Schema not defined for label :{}", name_id_mapper_->IdToName(primary_label))); } // Is there another primary label among secondary labels for (const auto &secondary_label : labels) { if (schemas_->GetSchema(secondary_label)) { - return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY, + return SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY, fmt::format("Cannot add label :{}, since it is defined as a primary label", name_id_mapper_->IdToName(secondary_label))); } @@ -46,7 +46,7 @@ std::optional SchemaValidator::ValidateVertexCreate( // Quick size check if (schema->second.size() != primary_properties.size()) { - return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED, + return SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED, fmt::format("Not all primary properties have been specified for :{} vertex", name_id_mapper_->IdToName(primary_label))); } @@ -56,7 +56,7 @@ std::optional SchemaValidator::ValidateVertexCreate( if (auto property_schema_type = PropertyTypeToSchemaType(primary_properties[i]); property_schema_type && *property_schema_type != schema->second[i].type) { return SHARD_ERROR( - ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE, + common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE, fmt::format("Property {} is of wrong type, expected {}, actual {}", name_id_mapper_->IdToName(schema->second[i].property_id), SchemaTypeToString(schema->second[i].type), SchemaTypeToString(*property_schema_type))); @@ -78,7 +78,7 @@ std::optional SchemaValidator::ValidatePropertyUpdate(const LabelId [property_id](const auto &schema_property) { return property_id == schema_property.property_id; }); schema_property != schema->second.end()) { return SHARD_ERROR( - ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY, + common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY, fmt::format("Cannot update primary property {} of schema on label :{}", name_id_mapper_->IdToName(schema_property->property_id), name_id_mapper_->IdToName(primary_label))); } @@ -88,7 +88,7 @@ std::optional SchemaValidator::ValidatePropertyUpdate(const LabelId std::optional SchemaValidator::ValidateLabelUpdate(const LabelId label) const { const auto *schema = schemas_->GetSchema(label); if (schema) { - return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL, + return SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL, fmt::format("Cannot add/remove primary label :{}", name_id_mapper_->IdToName(label))); } return std::nullopt; diff --git a/src/storage/v3/shard.cpp b/src/storage/v3/shard.cpp index 0fafe4bf6..bad7e8d0a 100644 --- a/src/storage/v3/shard.cpp +++ b/src/storage/v3/shard.cpp @@ -364,7 +364,7 @@ ShardResult Shard::Accessor::CreateVertexAndValidate( VertexAccessor vertex_acc{&it->vertex, transaction_, &shard_->indices_, config_, shard_->vertex_validator_}; if (!inserted) { - return SHARD_ERROR(ErrorCode::VERTEX_ALREADY_INSERTED); + return SHARD_ERROR(common::ErrorCode::VERTEX_ALREADY_INSERTED); } MG_ASSERT(it != acc.end(), "Invalid Vertex accessor!"); @@ -401,13 +401,14 @@ ShardResult> Shard::Accessor::DeleteVertex(VertexA "accessor when deleting a vertex!"); auto *vertex_ptr = vertex->vertex_; - if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); if (vertex_ptr->deleted) { return std::optional{}; } - if (!vertex_ptr->in_edges.empty() || !vertex_ptr->out_edges.empty()) return SHARD_ERROR(ErrorCode::VERTEX_HAS_EDGES); + if (!vertex_ptr->in_edges.empty() || !vertex_ptr->out_edges.empty()) + return SHARD_ERROR(common::ErrorCode::VERTEX_HAS_EDGES); CreateAndLinkDelta(transaction_, vertex_ptr, Delta::RecreateObjectTag()); vertex_ptr->deleted = true; @@ -429,7 +430,7 @@ ShardResult>>> std::vector out_edges; { - if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); if (vertex_ptr->deleted) return std::optional{}; @@ -444,7 +445,7 @@ ShardResult>>> EdgeAccessor e(edge, edge_type, from_vertex, vertex_id, transaction_, &shard_->indices_, config_); auto ret = DeleteEdge(e.FromVertex(), e.ToVertex(), e.Gid()); if (ret.HasError()) { - MG_ASSERT(ret.GetError().code == ErrorCode::SERIALIZATION_ERROR, "Invalid database state!"); + MG_ASSERT(ret.GetError().code == common::ErrorCode::SERIALIZATION_ERROR, "Invalid database state!"); return ret.GetError(); } @@ -457,7 +458,7 @@ ShardResult>>> EdgeAccessor e(edge, edge_type, vertex_id, to_vertex, transaction_, &shard_->indices_, config_); auto ret = DeleteEdge(e.FromVertex(), e.ToVertex(), e.Gid()); if (ret.HasError()) { - MG_ASSERT(ret.GetError().code == ErrorCode::SERIALIZATION_ERROR, "Invalid database state!"); + MG_ASSERT(ret.GetError().code == common::ErrorCode::SERIALIZATION_ERROR, "Invalid database state!"); return ret.GetError(); } @@ -470,7 +471,7 @@ ShardResult>>> // vertex. Some other transaction could have modified the vertex in the // meantime if we didn't have any edges to delete. - if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); MG_ASSERT(!vertex_ptr->deleted, "Invalid database state!"); @@ -507,12 +508,12 @@ ShardResult Shard::Accessor::CreateEdge(VertexId from_vertex_id, V } if (from_is_local) { - if (!PrepareForWrite(transaction_, from_vertex)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (from_vertex->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (!PrepareForWrite(transaction_, from_vertex)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (from_vertex->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); } if (to_is_local && to_vertex != from_vertex) { - if (!PrepareForWrite(transaction_, to_vertex)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (to_vertex->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (!PrepareForWrite(transaction_, to_vertex)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (to_vertex->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); } EdgeRef edge(gid); @@ -567,13 +568,13 @@ ShardResult> Shard::Accessor::DeleteEdge(VertexId fr if (from_is_local) { if (!PrepareForWrite(transaction_, from_vertex)) { - return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); } MG_ASSERT(!from_vertex->deleted, "Invalid database state!"); } if (to_is_local && to_vertex != from_vertex) { if (!PrepareForWrite(transaction_, to_vertex)) { - return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); } MG_ASSERT(!to_vertex->deleted, "Invalid database state!"); } diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 1ea22fa8f..1ff8e04b7 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -532,7 +532,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { auto vertex_to_update = acc.FindVertex(ConvertPropertyVector(std::move(vertex.primary_key)), View::OLD); if (!vertex_to_update) { action_successful = false; - shard_error.emplace(msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND}); + shard_error.emplace(msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND}); spdlog::debug("In transaction {} vertex could not be found while trying to update its properties.", req.transaction_id.logical_id); continue; @@ -564,7 +564,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::DeleteVerticesRequest &&req) { spdlog::debug("Error while trying to delete vertex. Vertex to delete does not exist. Transaction id: {}", req.transaction_id.logical_id); action_successful = false; - shard_error.emplace(msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND}); + shard_error.emplace(msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND}); spdlog::debug("In transaction {} vertex could not be found while trying to delete it.", req.transaction_id.logical_id); break; @@ -612,7 +612,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { if (!(shard_->IsVertexBelongToShard(from_vertex_id) || shard_->IsVertexBelongToShard(to_vertex_id))) { action_successful = false; - shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND, + shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND, "Error while trying to insert edge, none of the vertices belong to this shard"}; spdlog::debug("Error while trying to insert edge, none of the vertices belong to this shard. Transaction id: {}", req.transaction_id.logical_id); @@ -638,7 +638,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { } else { // TODO Code for this action_successful = false; - shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND}; + shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND}; spdlog::debug("Creating edge was not successful. Transaction id: {}", req.transaction_id.logical_id); break; } @@ -697,7 +697,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { auto vertex_acc = acc.FindVertex(ConvertPropertyVector(std::move(edge.src.second)), View::OLD); if (!vertex_acc) { action_successful = false; - shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND, "Source vertex was not found"}; + shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND, "Source vertex was not found"}; spdlog::debug("Encountered an error while trying to acquire VertexAccessor with transaction id: {}", req.transaction_id.logical_id); continue; @@ -733,7 +733,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { if (!edge_accessor_did_match) { // TODO(jbajic) Do we need this - shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND, "Edge was not found"}; + shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND, "Edge was not found"}; action_successful = false; spdlog::debug("Could not find the Edge with the specified Gid. Transaction id: {}", req.transaction_id.logical_id); @@ -785,7 +785,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { // Vertex is separated from the properties in the response. // Is it useful to return just a vertex without the properties? if (!found_props) { - shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND, "Requested properties were not found!"}; + shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND, "Requested properties were not found!"}; action_successful = false; } @@ -858,7 +858,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { // Get Vertex acc auto src_vertex_acc_opt = acc.FindVertex(ConvertPropertyVector((src_vertex.second)), View::NEW); if (!src_vertex_acc_opt) { - shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND, "Source vertex was not found."}; + shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND, "Source vertex was not found."}; action_successful = false; spdlog::debug("Encountered an error while trying to obtain VertexAccessor. Transaction id: {}", req.transaction_id.logical_id); @@ -878,7 +878,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { if (result.HasError()) { // Code Error - shard_error = msgs::ShardError{ErrorCode::OBJECT_NOT_FOUND, "Source vertex was not found."}; + shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND, "Source vertex was not found."}; action_successful = false; break; } diff --git a/src/storage/v3/vertex_accessor.cpp b/src/storage/v3/vertex_accessor.cpp index 6949c08cc..88f9aa965 100644 --- a/src/storage/v3/vertex_accessor.cpp +++ b/src/storage/v3/vertex_accessor.cpp @@ -83,9 +83,9 @@ bool VertexAccessor::IsVisible(View view) const { ShardResult VertexAccessor::AddLabel(LabelId label) { utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (vertex_->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); if (std::find(vertex_->labels.begin(), vertex_->labels.end(), label) != vertex_->labels.end()) return false; @@ -104,9 +104,9 @@ ShardResult VertexAccessor::AddLabelAndValidate(LabelId label) { } utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (vertex_->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); if (std::find(vertex_->labels.begin(), vertex_->labels.end(), label) != vertex_->labels.end()) return false; @@ -120,9 +120,9 @@ ShardResult VertexAccessor::AddLabelAndValidate(LabelId label) { } ShardResult VertexAccessor::RemoveLabel(LabelId label) { - if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (vertex_->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); auto it = std::find(vertex_->labels.begin(), vertex_->labels.end(), label); if (it == vertex_->labels.end()) return false; @@ -139,9 +139,9 @@ ShardResult VertexAccessor::RemoveLabelAndValidate(LabelId label) { return {*maybe_violation_error}; } - if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (vertex_->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); auto it = std::find(vertex_->labels.begin(), vertex_->labels.end(), label); if (it == vertex_->labels.end()) return false; @@ -197,8 +197,8 @@ ShardResult VertexAccessor::HasLabel(LabelId label, View view) const { break; } }); - if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); return has_label; } @@ -267,17 +267,17 @@ ShardResult> VertexAccessor::Labels(View view) const { break; } }); - if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); return std::move(labels); } ShardResult VertexAccessor::SetProperty(PropertyId property, const PropertyValue &value) { utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (vertex_->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); auto current_value = vertex_->properties.GetProperty(property); // We could skip setting the value if the previous one is the same to the new @@ -323,10 +323,10 @@ ShardResult VertexAccessor::CheckVertexExistence(View view) const { } }); if (!exists) { - return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); } if (!for_deleted_ && deleted) { - return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); } return {}; } @@ -338,11 +338,11 @@ ShardResult VertexAccessor::SetPropertyAndValidate(PropertyId pro utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; if (!PrepareForWrite(transaction_, vertex_)) { - return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); } if (vertex_->deleted) { - return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); } auto current_value = vertex_->properties.GetProperty(property); @@ -361,9 +361,9 @@ ShardResult VertexAccessor::SetPropertyAndValidate(PropertyId pro } ShardResult> VertexAccessor::ClearProperties() { - if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (vertex_->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); auto properties = vertex_->properties.Properties(); for (const auto &property : properties) { @@ -441,8 +441,8 @@ ShardResult VertexAccessor::GetProperty(PropertyId property, View break; } }); - if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); return std::move(value); } @@ -491,8 +491,8 @@ ShardResult> VertexAccessor::Properties(View break; } }); - if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); return std::move(properties); } @@ -563,8 +563,8 @@ ShardResult> VertexAccessor::InEdges(View view, const break; } }); - if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); - if (deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); + if (deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); std::vector ret; if (in_edges.empty()) { return ret; @@ -643,8 +643,8 @@ ShardResult> VertexAccessor::OutEdges(View view, const break; } }); - if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); - if (deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); + if (deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); std::vector ret; if (out_edges.empty()) { return ret; @@ -690,8 +690,8 @@ ShardResult VertexAccessor::InDegree(View view) const { break; } }); - if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); return degree; } @@ -727,8 +727,8 @@ ShardResult VertexAccessor::OutDegree(View view) const { break; } }); - if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); return degree; } diff --git a/tests/unit/storage_v3.cpp b/tests/unit/storage_v3.cpp index cd524cf41..a214ccac6 100644 --- a/tests/unit/storage_v3.cpp +++ b/tests/unit/storage_v3.cpp @@ -17,6 +17,7 @@ #include #include +#include "common/errors.hpp" #include "coordinator/hybrid_logical_clock.hpp" #include "io/time.hpp" #include "storage/v3/delta.hpp" @@ -579,7 +580,7 @@ TEST_P(StorageV3, VertexDeleteSerializationError) { EXPECT_EQ(CountVertices(acc2, View::NEW), 1U); auto res = acc2.DeleteVertex(&*vertex); ASSERT_TRUE(res.HasError()); - ASSERT_EQ(res.GetError(), SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR)); + ASSERT_EQ(res.GetError(), SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR)); EXPECT_EQ(CountVertices(acc2, View::OLD), 1U); EXPECT_EQ(CountVertices(acc2, View::NEW), 1U); acc2.AdvanceCommand(); @@ -710,20 +711,20 @@ TEST_P(StorageV3, VertexDeleteLabel) { // Check whether label 5 exists ASSERT_FALSE(vertex->HasLabel(label5, View::OLD).GetValue()); - ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); ASSERT_EQ(vertex->Labels(View::OLD)->size(), 0); - ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); // Try to add the label { auto ret = vertex->AddLabelAndValidate(label5); - AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + AssertShardErrorEqual(ret, SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); } // Try to remove the label { auto ret = vertex->RemoveLabelAndValidate(label5); - AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + AssertShardErrorEqual(ret, SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); } acc.Abort(); @@ -778,33 +779,33 @@ TEST_P(StorageV3, VertexDeleteLabel) { // Check whether label 5 exists ASSERT_TRUE(vertex->HasLabel(label5, View::OLD).GetValue()); - ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); { auto labels = vertex->Labels(View::OLD).GetValue(); ASSERT_EQ(labels.size(), 1); ASSERT_EQ(labels[0], label5); } - ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); // Advance command acc.AdvanceCommand(); // Check whether label 5 exists - ASSERT_EQ(vertex->HasLabel(label5, View::OLD).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex->Labels(View::OLD).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->HasLabel(label5, View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Labels(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); // Try to add the label { auto ret = vertex->AddLabelAndValidate(label5); - AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + AssertShardErrorEqual(ret, SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); } // Try to remove the label { auto ret = vertex->RemoveLabelAndValidate(label5); - AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + AssertShardErrorEqual(ret, SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); } acc.Abort(); @@ -854,14 +855,14 @@ TEST_P(StorageV3, VertexDeleteProperty) { // Check whether label 5 exists ASSERT_TRUE(vertex->GetProperty(property5, View::OLD)->IsNull()); - ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); ASSERT_EQ(vertex->Properties(View::OLD)->size(), 0); - ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); // Try to set the property5 { auto ret = vertex->SetPropertyAndValidate(property5, PropertyValue("haihai")); - AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + AssertShardErrorEqual(ret, SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); } acc.Abort(); @@ -917,27 +918,27 @@ TEST_P(StorageV3, VertexDeleteProperty) { // Check whether property 5 exists ASSERT_EQ(vertex->GetProperty(property5, View::OLD)->ValueString(), "nandare"); - ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); { auto properties = vertex->Properties(View::OLD).GetValue(); ASSERT_EQ(properties.size(), 1); ASSERT_EQ(properties[property5].ValueString(), "nandare"); } - ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); // Advance command acc.AdvanceCommand(); // Check whether property 5 exists - ASSERT_EQ(vertex->GetProperty(property5, View::OLD).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex->Properties(View::OLD).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->GetProperty(property5, View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Properties(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); // Try to set the property { auto ret = vertex->SetPropertyAndValidate(property5, PropertyValue("haihai")); - AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + AssertShardErrorEqual(ret, SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); } acc.Abort(); @@ -1370,7 +1371,7 @@ TEST_P(StorageV3, VertexLabelSerializationError) { { auto res = vertex->AddLabelAndValidate(label1); - AssertShardErrorEqual(res, SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR)); + AssertShardErrorEqual(res, SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR)); } } @@ -1864,7 +1865,7 @@ TEST_P(StorageV3, VertexPropertySerializationError) { { auto res = vertex->SetPropertyAndValidate(property2, PropertyValue("nandare")); - AssertShardErrorEqual(res, SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR)); + AssertShardErrorEqual(res, SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR)); } } @@ -2254,14 +2255,14 @@ TEST_P(StorageV3, VertexNonexistentLabelPropertyEdgeAPI) { auto vertex = CreateVertexAndValidate(acc, {}, PropertyValue{0}, {}); // Check state before (OLD view). - ASSERT_EQ(vertex.Labels(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.HasLabel(label1, View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.Properties(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.GetProperty(property1, View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.InEdges(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.OutEdges(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.InDegree(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.OutDegree(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.Labels(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.HasLabel(label1, View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.Properties(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.GetProperty(property1, View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.InEdges(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.OutEdges(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.InDegree(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.OutDegree(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); // Check state before (NEW view). ASSERT_EQ(vertex.Labels(View::NEW)->size(), 0); @@ -2281,14 +2282,14 @@ TEST_P(StorageV3, VertexNonexistentLabelPropertyEdgeAPI) { .HasValue()); // Check state after (OLD view). - ASSERT_EQ(vertex.Labels(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.HasLabel(label1, View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.Properties(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.GetProperty(property1, View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.InEdges(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.OutEdges(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.InDegree(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.OutDegree(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.Labels(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.HasLabel(label1, View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.Properties(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.GetProperty(property1, View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.InEdges(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.OutEdges(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.InDegree(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.OutDegree(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); // Check state after (NEW view). ASSERT_EQ(vertex.Labels(View::NEW)->size(), 1); @@ -2656,31 +2657,31 @@ TEST_P(StorageV3, TestCreateVertexAndValidate) { ASSERT_TRUE(vertex2.HasError()); auto error = vertex2.GetError(); - ASSERT_TRUE(error.code == storage::v3::ErrorCode::VERTEX_ALREADY_INSERTED); + ASSERT_TRUE(error.code == common::ErrorCode::VERTEX_ALREADY_INSERTED); } { auto acc = store.Access(GetNextHlc()); auto vertex = acc.CreateVertexAndValidate({primary_label}, {PropertyValue{0}}, {}); ASSERT_TRUE(vertex.HasError()); - EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { auto acc = store.Access(GetNextHlc()); auto vertex = acc.CreateVertexAndValidate({primary_label}, {PropertyValue{0}}, {}); ASSERT_TRUE(vertex.HasError()); - EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { auto acc = store.Access(GetNextHlc()); auto vertex = acc.CreateVertexAndValidate({}, {}, {}); ASSERT_TRUE(vertex.HasError()); - EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); } { auto acc = store.Access(GetNextHlc()); auto vertex = acc.CreateVertexAndValidate({}, {PropertyValue{"test"}}, {}); ASSERT_TRUE(vertex.HasError()); - EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } } } // namespace memgraph::storage::v3::tests diff --git a/tests/unit/storage_v3_edge.cpp b/tests/unit/storage_v3_edge.cpp index 64f7f4e2e..47327ca3e 100644 --- a/tests/unit/storage_v3_edge.cpp +++ b/tests/unit/storage_v3_edge.cpp @@ -3242,7 +3242,7 @@ TEST_P(StorageEdgeTest, VertexDetachDeleteSingleCommit) { { auto ret = acc.DeleteVertex(&vertex_from.value()); ASSERT_TRUE(ret.HasError()); - ASSERT_EQ(ret.GetError(), SHARD_ERROR(ErrorCode::VERTEX_HAS_EDGES)); + ASSERT_EQ(ret.GetError(), SHARD_ERROR(common::ErrorCode::VERTEX_HAS_EDGES)); } // Detach delete vertex @@ -3255,8 +3255,8 @@ TEST_P(StorageEdgeTest, VertexDetachDeleteSingleCommit) { // Check edges ASSERT_EQ(vertex_from->InEdges(View::OLD)->size(), 0); ASSERT_EQ(*vertex_from->InDegree(View::OLD), 0); - ASSERT_EQ(vertex_from->InEdges(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex_from->InDegree(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex_from->InEdges(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex_from->InDegree(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); { auto ret = vertex_from->OutEdges(View::OLD); ASSERT_TRUE(ret.HasValue()); @@ -3269,8 +3269,8 @@ TEST_P(StorageEdgeTest, VertexDetachDeleteSingleCommit) { ASSERT_EQ(e.FromVertex(), from_id); ASSERT_EQ(e.ToVertex(), to_id); } - ASSERT_EQ(vertex_from->OutEdges(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex_from->OutDegree(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex_from->OutEdges(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex_from->OutDegree(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); { auto ret = vertex_to->InEdges(View::OLD); ASSERT_TRUE(ret.HasValue()); diff --git a/tests/unit/storage_v3_schema.cpp b/tests/unit/storage_v3_schema.cpp index e5f651bf2..4ce611c98 100644 --- a/tests/unit/storage_v3_schema.cpp +++ b/tests/unit/storage_v3_schema.cpp @@ -18,6 +18,7 @@ #include #include +#include "common/errors.hpp" #include "common/types.hpp" #include "storage/v3/id_types.hpp" #include "storage/v3/property_value.hpp" @@ -187,43 +188,43 @@ TEST_F(SchemaValidatorTest, TestSchemaValidateVertexCreate) { { const auto schema_violation = schema_validator.ValidateVertexCreate(NameToLabel("test"), {}, {PropertyValue(1)}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); } // Validate wrong secondary label { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {label1}, {PropertyValue("test")}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {label2}, {PropertyValue("test")}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } // Validate wrong property type { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {}, {PropertyValue(1)}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {PropertyValue("test"), PropertyValue(12), PropertyValue(1)}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } { const auto wrong_prop = PropertyValue(TemporalData(TemporalType::Date, 1234)); const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {PropertyValue("test"), PropertyValue(12), wrong_prop}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } // Passing validations EXPECT_EQ(schema_validator.ValidateVertexCreate(label1, {}, {PropertyValue("test")}), std::nullopt); @@ -245,12 +246,12 @@ TEST_F(SchemaValidatorTest, TestSchemaValidatePropertyUpdate) { { const auto schema_violation = schema_validator.ValidatePropertyUpdate(label1, prop_string); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } { const auto schema_violation = schema_validator.ValidatePropertyUpdate(label2, prop_duration); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } EXPECT_EQ(schema_validator.ValidatePropertyUpdate(label1, prop_int), std::nullopt); EXPECT_EQ(schema_validator.ValidatePropertyUpdate(label1, prop_duration), std::nullopt); @@ -262,12 +263,12 @@ TEST_F(SchemaValidatorTest, TestSchemaValidatePropertyUpdateLabel) { { const auto schema_violation = schema_validator.ValidateLabelUpdate(label1); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } { const auto schema_violation = schema_validator.ValidateLabelUpdate(label2); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } EXPECT_EQ(schema_validator.ValidateLabelUpdate(NameToLabel("test")), std::nullopt); } diff --git a/tests/unit/storage_v3_vertex_accessors.cpp b/tests/unit/storage_v3_vertex_accessors.cpp index 3ba8def82..dd515983a 100644 --- a/tests/unit/storage_v3_vertex_accessors.cpp +++ b/tests/unit/storage_v3_vertex_accessors.cpp @@ -16,6 +16,7 @@ #include #include +#include "common/errors.hpp" #include "common/types.hpp" #include "storage/v3/delta.hpp" #include "storage/v3/id_types.hpp" @@ -76,7 +77,7 @@ TEST_F(StorageV3Accessor, TestPrimaryLabel) { ASSERT_TRUE(vertex.PrimaryLabel(View::OLD).HasError()); const auto error_primary_label = vertex.PrimaryLabel(View::OLD).GetError(); ASSERT_FALSE(vertex.PrimaryLabel(View::NEW).HasError()); - EXPECT_EQ(error_primary_label, SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + EXPECT_EQ(error_primary_label, SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); } { auto acc = storage.Access(GetNextHlc()); @@ -127,7 +128,7 @@ TEST_F(StorageV3Accessor, TestAddLabels) { const auto label1 = NameToLabelId("label"); auto vertex = acc.CreateVertexAndValidate({label1}, {PropertyValue{2}}, {}); ASSERT_TRUE(vertex.HasError()); - EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { auto acc = storage.Access(GetNextHlc()); @@ -136,7 +137,7 @@ TEST_F(StorageV3Accessor, TestAddLabels) { ASSERT_TRUE(vertex.HasValue()); const auto schema_violation = vertex->AddLabelAndValidate(label1); ASSERT_TRUE(schema_violation.HasError()); - EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } } @@ -180,7 +181,7 @@ TEST_F(StorageV3Accessor, TestRemoveLabels) { auto vertex = CreateVertexAndValidate(acc, {}, PropertyValue{2}); const auto res1 = vertex.RemoveLabelAndValidate(primary_label); ASSERT_TRUE(res1.HasError()); - EXPECT_EQ(res1.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); + EXPECT_EQ(res1.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } } @@ -199,14 +200,14 @@ TEST_F(StorageV3Accessor, TestSetKeysAndProperties) { auto vertex = CreateVertexAndValidate(acc, {}, PropertyValue{1}); const auto res = vertex.SetPropertyAndValidate(primary_property, PropertyValue(1)); ASSERT_TRUE(res.HasError()); - EXPECT_EQ(res.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); + EXPECT_EQ(res.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } { auto acc = storage.Access(GetNextHlc()); auto vertex = CreateVertexAndValidate(acc, {}, PropertyValue{2}); const auto res = vertex.SetPropertyAndValidate(primary_property, PropertyValue()); ASSERT_TRUE(res.HasError()); - EXPECT_EQ(res.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); + EXPECT_EQ(res.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } } From a17a6aea5a880ba144ea51d345e6080165269eca Mon Sep 17 00:00:00 2001 From: jeremy Date: Thu, 17 Nov 2022 12:27:12 +0100 Subject: [PATCH 058/103] rename variable vertice->vertex --- src/storage/v3/shard_rsm.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 5bb925fa8..88aa028e6 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -510,7 +510,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { // Can we do differently to avoid this? We need OrderByElements but currently it returns vector, so this // workaround is here to avoid more duplication later auto local_sorted_vertices = OrderByVertices( - acc, dba, vertex_accessors, req.order_by); // #NoCommit see whether we can avoid the extra std::transform + acc, dba, vertex_accessors, req.order_by); // #NoCommit see whether we can avoid the extra std::transform vertex_accessors.clear(); std::transform(local_sorted_vertices.begin(), local_sorted_vertices.end(), std::back_inserter(vertex_accessors), [](auto &vertex) { return vertex.object_acc; }); @@ -529,7 +529,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { break; } - msgs::VertexId src_vertice(msgs::Label{.id = *label_id}, conversions::ConvertValueVector(*primary_key)); + msgs::VertexId src_vertex(msgs::Label{.id = *label_id}, conversions::ConvertValueVector(*primary_key)); std::optional maybe_result; @@ -537,7 +537,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { auto schema = shard_->GetSchema(shard_->PrimaryLabel()); MG_ASSERT(schema); maybe_result = - GetExpandOneResult(acc, src_vertice, req, maybe_filter_based_on_edge_uniquness, edge_filler, *schema); + GetExpandOneResult(acc, src_vertex, req, maybe_filter_based_on_edge_uniquness, edge_filler, *schema); } else { auto [in_edge_accessors, out_edge_accessors] = GetEdgesFromVertex(src_vertex_acc, req.direction); @@ -554,7 +554,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { auto schema = shard_->GetSchema(shard_->PrimaryLabel()); MG_ASSERT(schema); maybe_result = - GetExpandOneResult(src_vertex_acc, src_vertice, req, in_edge_ordered_accessors, out_edge_ordered_accessors, + GetExpandOneResult(src_vertex_acc, src_vertex, req, in_edge_ordered_accessors, out_edge_ordered_accessors, maybe_filter_based_on_edge_uniquness, edge_filler, *schema); } From 68e51e73bacf66bb386dfb3edf66502a3933becd Mon Sep 17 00:00:00 2001 From: Jeremy B <97525434+42jeremy@users.noreply.github.com> Date: Thu, 17 Nov 2022 12:42:36 +0100 Subject: [PATCH 059/103] Update src/storage/v3/shard_rsm.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: János Benjamin Antal --- src/storage/v3/shard_rsm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 5bb925fa8..ac6e7ae66 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -563,7 +563,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { break; } - results.emplace_back(maybe_result.value()); + results.emplace_back(std::move(maybe_result.value())); if (batch_limit.has_value() && results.size() >= batch_limit.value()) { break; } From 2f554912717b77c9cd41550011064a44ed0ac6a0 Mon Sep 17 00:00:00 2001 From: jeremy Date: Thu, 17 Nov 2022 12:45:15 +0100 Subject: [PATCH 060/103] use std::SameAsAnyOf i.o. is_sale_v --- src/storage/v3/request_helper.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 035cc1ae5..9359bd52f 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -28,7 +28,7 @@ using EdgeFiller = std::function -concept ObjectAccessor = std::is_same_v || std::is_same_v; +concept ObjectAccessor = utils::SameAsAnyOf; inline bool TypedValueCompare(const TypedValue &a, const TypedValue &b) { // in ordering null comes after everything else From 38b0b308cedf9d9b80afb20842b54fbdf8e40cf6 Mon Sep 17 00:00:00 2001 From: jeremy Date: Thu, 17 Nov 2022 12:58:28 +0100 Subject: [PATCH 061/103] Remove unnecessary reserve --- src/storage/v3/request_helper.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 9359bd52f..309902645 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -133,7 +133,6 @@ std::vector> OrderByVertices(Shard::Accessor &acc, DbAcc }); std::vector> ordered; - ordered.reserve(acc.ApproximateVertexCount()); for (auto it = iterable.begin(); it != iterable.end(); ++it) { std::vector properties_order_by; properties_order_by.reserve(order_bys.size()); From 77ab07d99100b568fb00db32d6e19bcc654407d7 Mon Sep 17 00:00:00 2001 From: Jeremy B <97525434+42jeremy@users.noreply.github.com> Date: Thu, 17 Nov 2022 12:58:39 +0100 Subject: [PATCH 062/103] Update src/storage/v3/request_helper.hpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: János Benjamin Antal --- src/storage/v3/request_helper.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 035cc1ae5..bc622cc73 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -117,10 +117,11 @@ struct Element { TObjectAccessor object_acc; }; -template +template +concept IDontKnowAGoodNameForThis = utils::SameAsAnyOf> +template std::vector> OrderByVertices(Shard::Accessor &acc, DbAccessor &dba, TIterable &iterable, std::vector &order_bys) { - static_assert(std::is_same_v || std::is_same_v>); std::vector ordering; ordering.reserve(order_bys.size()); From 6e44c2295d289080267eb37a3fe6ec157c4e4261 Mon Sep 17 00:00:00 2001 From: jeremy Date: Thu, 17 Nov 2022 13:30:39 +0100 Subject: [PATCH 063/103] Remove template from OrderByEdges + move to impl file --- src/storage/v3/request_helper.cpp | 33 +++++++++++++++++++++++++ src/storage/v3/request_helper.hpp | 40 ++++--------------------------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index b5c7ea2dd..c59cadf9c 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -512,4 +512,37 @@ std::array, 2> GetEdgesFromVertex(const VertexAccessor return std::array, 2>{std::move(in_edges), std::move(out_edges)}; } +std::vector> OrderByEdges(DbAccessor &dba, std::vector &iterable, + std::vector &order_bys, + const VertexAccessor &vertex_acc) { + std::vector ordering; + ordering.reserve(order_bys.size()); + std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(ordering), [](const auto &order_by) { + if (memgraph::msgs::OrderingDirection::ASCENDING == order_by.direction) { + return Ordering::ASC; + } + MG_ASSERT(memgraph::msgs::OrderingDirection::DESCENDING == order_by.direction); + return Ordering::DESC; + }); + + std::vector> ordered; + for (auto it = iterable.begin(); it != iterable.end(); ++it) { + std::vector properties_order_by; + properties_order_by.reserve(order_bys.size()); + std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(properties_order_by), + [&dba, &vertex_acc, &it](const auto &order_by) { + return ComputeExpression(dba, vertex_acc, *it, order_by.expression.expression, + expr::identifier_node_symbol, expr::identifier_edge_symbol); + }); + + ordered.push_back({std::move(properties_order_by), *it}); + } + + auto compare_typed_values = TypedValueVectorCompare(ordering); + std::sort(ordered.begin(), ordered.end(), [compare_typed_values](const auto &pair1, const auto &pair2) { + return compare_typed_values(pair1.properties_order_by, pair2.properties_order_by); + }); + return ordered; +} + } // namespace memgraph::storage::v3 diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 5a065c3c9..37cc0e7ff 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -118,8 +118,8 @@ struct Element { }; template -concept IDontKnowAGoodNameForThis = utils::SameAsAnyOf> -template +concept VerticesIt = utils::SameAsAnyOf>; +template std::vector> OrderByVertices(Shard::Accessor &acc, DbAccessor &dba, TIterable &iterable, std::vector &order_bys) { std::vector ordering; @@ -153,41 +153,9 @@ std::vector> OrderByVertices(Shard::Accessor &acc, DbAcc return ordered; } -template -std::vector> OrderByEdges(DbAccessor &dba, TIterable &iterable, +std::vector> OrderByEdges(DbAccessor &dba, std::vector &iterable, std::vector &order_bys, - const VertexAccessor &vertex_acc) { - static_assert(std::is_same_v>); // Can be extended if needed - - std::vector ordering; - ordering.reserve(order_bys.size()); - std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(ordering), [](const auto &order_by) { - if (memgraph::msgs::OrderingDirection::ASCENDING == order_by.direction) { - return Ordering::ASC; - } - MG_ASSERT(memgraph::msgs::OrderingDirection::DESCENDING == order_by.direction); - return Ordering::DESC; - }); - - std::vector> ordered; - for (auto it = iterable.begin(); it != iterable.end(); ++it) { - std::vector properties_order_by; - properties_order_by.reserve(order_bys.size()); - std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(properties_order_by), - [&dba, &vertex_acc, &it](const auto &order_by) { - return ComputeExpression(dba, vertex_acc, *it, order_by.expression.expression, - expr::identifier_node_symbol, expr::identifier_edge_symbol); - }); - - ordered.push_back({std::move(properties_order_by), *it}); - } - - auto compare_typed_values = TypedValueVectorCompare(ordering); - std::sort(ordered.begin(), ordered.end(), [compare_typed_values](const auto &pair1, const auto &pair2) { - return compare_typed_values(pair1.properties_order_by, pair2.properties_order_by); - }); - return ordered; -} + const VertexAccessor &vertex_acc); VerticesIterable::Iterator GetStartVertexIterator(VerticesIterable &vertex_iterable, const std::vector &start_ids, View view); From 49652d0a61d54a8ac60b8904f358459173c06e09 Mon Sep 17 00:00:00 2001 From: Jeremy B <97525434+42jeremy@users.noreply.github.com> Date: Thu, 17 Nov 2022 13:32:48 +0100 Subject: [PATCH 064/103] Update src/storage/v3/shard_rsm.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: János Benjamin Antal --- src/storage/v3/shard_rsm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index ac6e7ae66..5b2afeda2 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -478,7 +478,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { bool action_successful = true; std::vector results; - auto batch_limit = req.limit; + const auto batch_limit = req.limit; auto dba = DbAccessor{&acc}; auto maybe_filter_based_on_edge_uniquness = InitializeEdgeUniqunessFunction(req.only_unique_neighbor_rows); From a499bf6dfdc33da69cc35b5b980a3e6034955a17 Mon Sep 17 00:00:00 2001 From: jeremy Date: Thu, 17 Nov 2022 13:33:11 +0100 Subject: [PATCH 065/103] Rename variable --- src/storage/v3/request_helper.cpp | 8 ++++---- src/storage/v3/request_helper.hpp | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index c59cadf9c..0222f38ad 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -456,10 +456,10 @@ std::optional GetExpandOneResult( } VerticesIterable::Iterator GetStartVertexIterator(VerticesIterable &vertex_iterable, - const std::vector &start_ids, const View view) { + const std::vector &primary_key, const View view) { auto it = vertex_iterable.begin(); while (it != vertex_iterable.end()) { - if (const auto &vertex = *it; start_ids <= vertex.PrimaryKey(view).GetValue()) { + if (const auto &vertex = *it; primary_key <= vertex.PrimaryKey(view).GetValue()) { break; } ++it; @@ -468,10 +468,10 @@ VerticesIterable::Iterator GetStartVertexIterator(VerticesIterable &vertex_itera } std::vector>::const_iterator GetStartOrderedElementsIterator( - const std::vector> &ordered_elements, const std::vector &start_ids, + const std::vector> &ordered_elements, const std::vector &primary_key, const View view) { for (auto it = ordered_elements.begin(); it != ordered_elements.end(); ++it) { - if (const auto &vertex = it->object_acc; start_ids <= vertex.PrimaryKey(view).GetValue()) { + if (const auto &vertex = it->object_acc; primary_key <= vertex.PrimaryKey(view).GetValue()) { return it; } } diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 37cc0e7ff..71bef4a20 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -158,10 +158,10 @@ std::vector> OrderByEdges(DbAccessor &dba, std::vector &start_ids, View view); + const std::vector &primary_key, View view); std::vector>::const_iterator GetStartOrderedElementsIterator( - const std::vector> &ordered_elements, const std::vector &start_ids, + const std::vector> &ordered_elements, const std::vector &primary_key, View view); std::array, 2> GetEdgesFromVertex(const VertexAccessor &vertex_accessor, From 5f88e7557158f07102dd0a12963df0ca0f0725ab Mon Sep 17 00:00:00 2001 From: jeremy Date: Thu, 17 Nov 2022 14:10:49 +0100 Subject: [PATCH 066/103] Remove double declaration --- src/storage/v3/request_helper.hpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 71bef4a20..94220f214 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -185,13 +185,6 @@ EdgeUniqunessFunction InitializeEdgeUniqunessFunction(bool only_unique_neighbor_ EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req); -std::optional> CollectSpecificPropertiesFromAccessor(const VertexAccessor &acc, - const std::vector &props, - View view); - -bool FilterOnVertex(DbAccessor &dba, const storage::v3::VertexAccessor &v_acc, const std::vector &filters, - const std::string_view node_name); - std::optional GetExpandOneResult( Shard::Accessor &acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, const EdgeUniqunessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, From fe03f5b206f601a66c675bfed0b11914de658cfd Mon Sep 17 00:00:00 2001 From: jeremy Date: Thu, 17 Nov 2022 14:11:25 +0100 Subject: [PATCH 067/103] Update include to full path add auto To variable declaration --- src/storage/v3/request_helper.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index 0222f38ad..f9cbf8886 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -13,8 +13,8 @@ #include -#include "pretty_print_ast_to_original_expression.hpp" #include "storage/v3/bindings/db_accessor.hpp" +#include "storage/v3/bindings/pretty_print_ast_to_original_expression.hpp" #include "storage/v3/expr.hpp" #include "storage/v3/value_conversions.hpp" @@ -387,8 +387,7 @@ std::optional GetExpandOneResult( return std::nullopt; } - std::optional> src_vertex_properties; - src_vertex_properties = FillUpSourceVertexProperties(v_acc, req, storage::v3::View::NEW, schema); + auto src_vertex_properties = FillUpSourceVertexProperties(v_acc, req, storage::v3::View::NEW, schema); if (!src_vertex_properties) { return std::nullopt; From 3840c148460e0073cb1a7220ac0ea9aeade70ca7 Mon Sep 17 00:00:00 2001 From: jeremy Date: Thu, 17 Nov 2022 14:33:08 +0100 Subject: [PATCH 068/103] Remove nocommit comment --- src/storage/v3/shard_rsm.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 9f739695e..2cf265205 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -509,8 +509,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { if (!req.order_by.empty()) { // Can we do differently to avoid this? We need OrderByElements but currently it returns vector, so this // workaround is here to avoid more duplication later - auto local_sorted_vertices = OrderByVertices( - acc, dba, vertex_accessors, req.order_by); // #NoCommit see whether we can avoid the extra std::transform + auto local_sorted_vertices = OrderByVertices(acc, dba, vertex_accessors, req.order_by); vertex_accessors.clear(); std::transform(local_sorted_vertices.begin(), local_sorted_vertices.end(), std::back_inserter(vertex_accessors), [](auto &vertex) { return vertex.object_acc; }); From 1b0db5289d6ffc75b6f74dc7e327fee0b38eba66 Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 21 Nov 2022 10:41:08 +0100 Subject: [PATCH 069/103] OrderByVertices only keeps OrderBy expression which corresponds to Vertices --- src/storage/v3/request_helper.hpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 94220f214..08094e8ba 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -121,7 +121,14 @@ template concept VerticesIt = utils::SameAsAnyOf>; template std::vector> OrderByVertices(Shard::Accessor &acc, DbAccessor &dba, TIterable &iterable, - std::vector &order_bys) { + std::vector &original_order_bys) { + auto order_bys = original_order_bys; + auto it_to_remove = std::remove_if(order_bys.begin(), order_bys.end(), [](const auto &order_by) { + // We only want to keep OrderBys not impliying edges-ordering + return std::string::npos != order_by.expression.expression.find(expr::identifier_edge_symbol); + }); + order_bys.erase(it_to_remove, order_bys.end()); + std::vector ordering; ordering.reserve(order_bys.size()); std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(ordering), [](const auto &order_by) { From 86e5b44c1c89a3f0a0606f6d682d7125bea4b48d Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 21 Nov 2022 10:42:15 +0100 Subject: [PATCH 070/103] Remove Shard::Accessor (unsued) --- src/storage/v3/request_helper.hpp | 2 +- src/storage/v3/shard_rsm.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 08094e8ba..44d012f1d 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -120,7 +120,7 @@ struct Element { template concept VerticesIt = utils::SameAsAnyOf>; template -std::vector> OrderByVertices(Shard::Accessor &acc, DbAccessor &dba, TIterable &iterable, +std::vector> OrderByVertices(DbAccessor &dba, TIterable &iterable, std::vector &original_order_bys) { auto order_bys = original_order_bys; auto it_to_remove = std::remove_if(order_bys.begin(), order_bys.end(), [](const auto &order_by) { diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 2cf265205..eaf760253 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -426,7 +426,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { uint64_t sample_counter{0}; auto vertex_iterable = acc.Vertices(view); if (!req.order_bys.empty()) { - const auto ordered = OrderByVertices(acc, dba, vertex_iterable, req.order_bys); + const auto ordered = OrderByVertices(dba, vertex_iterable, req.order_bys); // we are traversing Elements auto it = GetStartOrderedElementsIterator(ordered, start_id, View(req.storage_view)); for (; it != ordered.end(); ++it) { @@ -509,7 +509,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { if (!req.order_by.empty()) { // Can we do differently to avoid this? We need OrderByElements but currently it returns vector, so this // workaround is here to avoid more duplication later - auto local_sorted_vertices = OrderByVertices(acc, dba, vertex_accessors, req.order_by); + auto local_sorted_vertices = OrderByVertices(dba, vertex_accessors, req.order_by); vertex_accessors.clear(); std::transform(local_sorted_vertices.begin(), local_sorted_vertices.end(), std::back_inserter(vertex_accessors), [](auto &vertex) { return vertex.object_acc; }); From 2fc1aeb087d83a3f8a6125f691a1f671f83d4ec2 Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 21 Nov 2022 10:59:16 +0100 Subject: [PATCH 071/103] Remove unneeded using statements --- src/storage/v3/request_helper.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index f9cbf8886..ab863afdb 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -22,12 +22,9 @@ namespace memgraph::storage::v3 { using msgs::Label; using msgs::PropertyId; -using conversions::ConvertPropertyMap; using conversions::ConvertPropertyVector; -using conversions::ConvertValueVector; using conversions::FromPropertyValueToValue; using conversions::ToMsgsVertexId; -using conversions::ToPropertyValue; namespace { namespace msgs = msgs; From b2050d55ce4842c3b87a4e2491c5220ad9f2b8da Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 21 Nov 2022 10:59:38 +0100 Subject: [PATCH 072/103] Add const --- src/storage/v3/shard_rsm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index eaf760253..9c281b51f 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -550,7 +550,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { std::vector out_edge_ordered_accessors; std::transform(out_ordered_edges.begin(), out_ordered_edges.end(), std::back_inserter(out_edge_ordered_accessors), [](const auto &edge_element) { return edge_element.object_acc; }); - auto schema = shard_->GetSchema(shard_->PrimaryLabel()); + const auto schema = shard_->GetSchema(shard_->PrimaryLabel()); MG_ASSERT(schema); maybe_result = GetExpandOneResult(src_vertex_acc, src_vertex, req, in_edge_ordered_accessors, out_edge_ordered_accessors, From 4eb673c7b92e351bfc2c9418d7052899c19845ec Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 21 Nov 2022 11:31:22 +0100 Subject: [PATCH 073/103] Add const to variable --- src/storage/v3/shard_rsm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 9c281b51f..767fefed9 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -533,7 +533,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { std::optional maybe_result; if (req.order_by.empty()) { - auto schema = shard_->GetSchema(shard_->PrimaryLabel()); + const auto schema = shard_->GetSchema(shard_->PrimaryLabel()); MG_ASSERT(schema); maybe_result = GetExpandOneResult(acc, src_vertex, req, maybe_filter_based_on_edge_uniquness, edge_filler, *schema); From 1a67dec302ed85a8e330c2c21f69eea1328e9a3c Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 21 Nov 2022 11:45:35 +0100 Subject: [PATCH 074/103] Update test to use OrderBy and Limit on Expand --- tests/simulation/shard_rsm.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index b54f7a993..c1ef1079d 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -680,10 +680,12 @@ void AttemptToExpandOneWithUniqueEdges(ShardClient &client, uint64_t src_vertex_ } } -void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_val, EdgeTypeId edge_type_id) { +void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_val, uint64_t other_src_vertex_val, + EdgeTypeId edge_type_id) { // Source vertex msgs::Label label = {.id = get_primary_label()}; auto src_vertex = std::make_pair(label, GetPrimaryKey(src_vertex_val)); + auto other_src_vertex = std::make_pair(label, GetPrimaryKey(other_src_vertex_val)); // Edge type auto edge_type = msgs::EdgeType{}; @@ -699,8 +701,9 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ std::optional> edge_properties = {}; std::vector order_by = { - {msgs::Expression{"MG_SYMBOL_NODE.prop1"}, msgs::OrderingDirection::DESCENDING}}; - size_t limit = 3; + {msgs::Expression{"MG_SYMBOL_NODE.prop1"}, msgs::OrderingDirection::ASCENDING}, + {msgs::Expression{"MG_SYMBOL_EDGE.prop4"}, msgs::OrderingDirection::DESCENDING}}; + size_t limit = 1; std::vector filters = {"MG_SYMBOL_NODE.prop1 != -1"}; msgs::ExpandOneRequest expand_one_req{}; @@ -712,7 +715,7 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ expand_one_req.limit = limit; expand_one_req.order_by = order_by; expand_one_req.src_vertex_properties = src_vertex_properties; - expand_one_req.src_vertices = {src_vertex}; + expand_one_req.src_vertices = {src_vertex, other_src_vertex}; expand_one_req.transaction_id.logical_id = GetTransactionId(); while (true) { @@ -727,9 +730,14 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ // We check that we do not have more results than the limit. Based on the data in the graph, we know that we should // receive exactly limit responses. auto expected_number_of_rows = std::min(expand_one_req.src_vertices.size(), limit); - MG_ASSERT(expected_number_of_rows == 1); // We are sending a single vertex to expand + MG_ASSERT(expected_number_of_rows == 1); MG_ASSERT(write_response.result.size() == expected_number_of_rows); - const auto expected_number_of_edges = 10; // We know there are 10 out-going edges from V2->V3 + + // We know there are 1 out-going edges from V1->V2 + // We know there are 10 out-going edges from V2->V3 + // Since we sort on prop1 and limit 1, we will have a single response + // with two edges corresponding to V1->V2 and V1->V3 + const auto expected_number_of_edges = 2; MG_ASSERT(write_response.result[0].out_edges_with_all_properties.size() == expected_number_of_edges); MG_ASSERT(write_response.result[0] .out_edges_with_specific_properties.empty()); // We are not asking for specific properties @@ -1109,7 +1117,7 @@ void TestExpandOneGraphOne(ShardClient &client) { }); AttemptToExpandOneSimple(client, unique_prop_val_1, edge_type_id); - AttemptToExpandOneLimitAndOrderBy(client, unique_prop_val_2, edge_type_id); + AttemptToExpandOneLimitAndOrderBy(client, unique_prop_val_1, unique_prop_val_2, edge_type_id); AttemptToExpandOneWithWrongEdgeType(client, unique_prop_val_1, wrong_edge_type_id); AttemptToExpandOneWithSpecifiedSrcVertexProperties(client, unique_prop_val_1, edge_type_id); AttemptToExpandOneWithSpecifiedEdgeProperties(client, unique_prop_val_1, edge_type_id, edge_prop_id); From e9e42a0614d05cfaecfd9bebf0cf282af683af9b Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 21 Nov 2022 11:50:56 +0100 Subject: [PATCH 075/103] add * token to variable declaration --- src/storage/v3/shard_rsm.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 767fefed9..d3cf8e4c3 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -533,7 +533,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { std::optional maybe_result; if (req.order_by.empty()) { - const auto schema = shard_->GetSchema(shard_->PrimaryLabel()); + const auto *schema = shard_->GetSchema(shard_->PrimaryLabel()); MG_ASSERT(schema); maybe_result = GetExpandOneResult(acc, src_vertex, req, maybe_filter_based_on_edge_uniquness, edge_filler, *schema); @@ -550,7 +550,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { std::vector out_edge_ordered_accessors; std::transform(out_ordered_edges.begin(), out_ordered_edges.end(), std::back_inserter(out_edge_ordered_accessors), [](const auto &edge_element) { return edge_element.object_acc; }); - const auto schema = shard_->GetSchema(shard_->PrimaryLabel()); + const auto *schema = shard_->GetSchema(shard_->PrimaryLabel()); MG_ASSERT(schema); maybe_result = GetExpandOneResult(src_vertex_acc, src_vertex, req, in_edge_ordered_accessors, out_edge_ordered_accessors, From ce8bc522d048d478d045146b93ae7ce318bee524 Mon Sep 17 00:00:00 2001 From: jeremy Date: Mon, 21 Nov 2022 12:23:04 +0100 Subject: [PATCH 076/103] Clang warning --- src/storage/v3/request_helper.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 44d012f1d..5591ac11a 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -175,7 +175,7 @@ std::array, 2> GetEdgesFromVertex(const VertexAccessor msgs::EdgeDirection direction); bool FilterOnVertex(DbAccessor &dba, const storage::v3::VertexAccessor &v_acc, const std::vector &filters, - const std::string_view node_name); + std::string_view node_name); std::vector EvaluateVertexExpressions(DbAccessor &dba, const VertexAccessor &v_acc, const std::vector &expressions, From bffef1a653cd59e69b94c0a0d9a6c7079edec3d6 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 22 Nov 2022 11:13:23 +0100 Subject: [PATCH 077/103] Use experimental source location --- src/expr/interpret/eval.hpp | 8 ++++---- src/storage/v3/result.hpp | 10 ++++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/expr/interpret/eval.hpp b/src/expr/interpret/eval.hpp index 85911678d..758ea3797 100644 --- a/src/expr/interpret/eval.hpp +++ b/src/expr/interpret/eval.hpp @@ -100,7 +100,7 @@ class ExpressionEvaluator : public ExpressionVisitor { #undef BINARY_OPERATOR_VISITOR #undef UNARY_OPERATOR_VISITOR - void HandleShardError(Error &shard_error, const std::string_view accessed_object) { + void HandleObjectAccessError(Error &shard_error, const std::string_view accessed_object) { switch (shard_error) { case Error::DELETED_OBJECT: throw ExpressionRuntimeException("Trying to access {} on a deleted object.", accessed_object); @@ -418,7 +418,7 @@ class ExpressionEvaluator : public ExpressionVisitor { has_label = vertex.HasLabel(StorageView::NEW, GetLabel(label)); } if (has_label.HasError()) { - HandleShardError(has_label.GetError().code, "labels"); + HandleObjectAccessError(has_label.GetError().code, "labels"); } return *has_label; } @@ -756,7 +756,7 @@ class ExpressionEvaluator : public ExpressionVisitor { maybe_prop = record_accessor.GetProperty(StorageView::NEW, ctx_->properties[prop.ix]); } if (maybe_prop.HasError()) { - HandleShardError(maybe_prop.GetError().code, "property"); + HandleObjectAccessError(maybe_prop.GetError().code, "property"); } return conv_(*maybe_prop, ctx_->memory); } @@ -775,7 +775,7 @@ class ExpressionEvaluator : public ExpressionVisitor { maybe_prop = record_accessor.GetProperty(view_, dba_->NameToProperty(name)); } if (maybe_prop.HasError()) { - HandleShardError(maybe_prop.GetError().code, "property"); + HandleObjectAccessError(maybe_prop.GetError().code, "property"); } return conv_(*maybe_prop, ctx_->memory); } diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index c9c0c8a4b..b86ede6b4 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -12,6 +12,7 @@ #pragma once #include +#include #include #include #include @@ -24,10 +25,11 @@ namespace memgraph::storage::v3 { static_assert(std::is_same_v); struct ShardError { - ShardError(common::ErrorCode code, std::string message, std::string source) - : code{code}, message{std::move(message)}, source{std::move(source)} {} + ShardError(common::ErrorCode code, std::string message, const std::experimental::source_location location) + : code{code}, message{std::move(message)}, source{fmt::format("{}:{}", location.file_name(), location.line())} {} - ShardError(common::ErrorCode code, std::string source) : code{code}, source{std::move(source)} {} + ShardError(common::ErrorCode code, const std::experimental::source_location location) + : code{code}, source{fmt::format("{}:{}", location.file_name(), location.line())} {} common::ErrorCode code; // TODO Maybe add category @@ -38,7 +40,7 @@ struct ShardError { }; // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) -#define SHARD_ERROR(...) memgraph::storage::v3::ShardError(__VA_ARGS__, fmt::format("{}:{}", __FILE__, __LINE__)) +#define SHARD_ERROR(...) memgraph::storage::v3::ShardError(__VA_ARGS__, std::experimental::source_location::current()) template using ShardResult = utils::BasicResult; From 37f5fb29ea30134d6ee9e65dca21c01e9afc23fe Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 22 Nov 2022 13:15:32 +0100 Subject: [PATCH 078/103] Introduce BoltResult --- src/glue/v2/communication.cpp | 23 +++++++++++------------ src/glue/v2/communication.hpp | 28 ++++++++++++++++------------ src/query/v2/plan/operator.cpp | 28 ---------------------------- 3 files changed, 27 insertions(+), 52 deletions(-) diff --git a/src/glue/v2/communication.cpp b/src/glue/v2/communication.cpp index 0b185d19b..a977e0e29 100644 --- a/src/glue/v2/communication.cpp +++ b/src/glue/v2/communication.cpp @@ -71,9 +71,9 @@ query::v2::TypedValue ToTypedValue(const Value &value) { } } -storage::v3::ShardResult ToBoltVertex( - const query::v2::accessors::VertexAccessor &vertex, const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View /*view*/) { +BoltResult ToBoltVertex(const query::v2::accessors::VertexAccessor &vertex, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View /*view*/) { auto id = communication::bolt::Id::FromUint(0); auto labels = vertex.Labels(); @@ -91,9 +91,9 @@ storage::v3::ShardResult ToBoltVertex( return communication::bolt::Vertex{id, new_labels, new_properties}; } -storage::v3::ShardResult ToBoltEdge( - const query::v2::accessors::EdgeAccessor &edge, const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View /*view*/) { +BoltResult ToBoltEdge(const query::v2::accessors::EdgeAccessor &edge, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View /*view*/) { // TODO(jbajic) Fix bolt communication auto id = communication::bolt::Id::FromUint(0); auto from = communication::bolt::Id::FromUint(0); @@ -108,16 +108,15 @@ storage::v3::ShardResult ToBoltEdge( return communication::bolt::Edge{id, from, to, type, new_properties}; } -storage::v3::ShardResult ToBoltPath( - const query::v2::accessors::Path & /*edge*/, const msgs::ShardRequestManagerInterface * /*shard_request_manager*/, - storage::v3::View /*view*/) { +BoltResult ToBoltPath(const query::v2::accessors::Path & /*edge*/, + const msgs::ShardRequestManagerInterface * /*shard_request_manager*/, + storage::v3::View /*view*/) { // TODO(jbajic) Fix bolt communication return {SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)}; } -storage::v3::ShardResult ToBoltValue(const query::v2::TypedValue &value, - const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View view) { +BoltResult ToBoltValue(const query::v2::TypedValue &value, + const msgs::ShardRequestManagerInterface *shard_request_manager, storage::v3::View view) { switch (value.type()) { case query::v2::TypedValue::Type::Null: return Value(); diff --git a/src/glue/v2/communication.hpp b/src/glue/v2/communication.hpp index debc26adf..4ea05a61d 100644 --- a/src/glue/v2/communication.hpp +++ b/src/glue/v2/communication.hpp @@ -20,6 +20,7 @@ #include "storage/v3/result.hpp" #include "storage/v3/shard.hpp" #include "storage/v3/view.hpp" +#include "utils/result.hpp" namespace memgraph::storage::v3 { class EdgeAccessor; @@ -29,42 +30,45 @@ class VertexAccessor; namespace memgraph::glue::v2 { +template +using BoltResult = utils::BasicResult; + /// @param storage::v3::VertexAccessor for converting to /// communication::bolt::Vertex. /// @param msgs::ShardRequestManagerInterface *shard_request_manager getting label and property names. /// @param storage::v3::View for deciding which vertex attributes are visible. /// /// @throw std::bad_alloc -storage::v3::ShardResult ToBoltVertex( - const storage::v3::VertexAccessor &vertex, const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View view); +BoltResult ToBoltVertex(const storage::v3::VertexAccessor &vertex, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View view); /// @param storage::v3::EdgeAccessor for converting to communication::bolt::Edge. /// @param msgs::ShardRequestManagerInterface *shard_request_manager getting edge type and property names. /// @param storage::v3::View for deciding which edge attributes are visible. /// /// @throw std::bad_alloc -storage::v3::ShardResult ToBoltEdge( - const storage::v3::EdgeAccessor &edge, const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View view); +BoltResult ToBoltEdge(const storage::v3::EdgeAccessor &edge, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View view); /// @param query::v2::Path for converting to communication::bolt::Path. /// @param msgs::ShardRequestManagerInterface *shard_request_manager ToBoltVertex and ToBoltEdge. /// @param storage::v3::View for ToBoltVertex and ToBoltEdge. /// /// @throw std::bad_alloc -storage::v3::ShardResult ToBoltPath( - const query::v2::accessors::Path &path, const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View view); +BoltResult ToBoltPath(const query::v2::accessors::Path &path, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View view); /// @param query::v2::TypedValue for converting to communication::bolt::Value. /// @param msgs::ShardRequestManagerInterface *shard_request_manager ToBoltVertex and ToBoltEdge. /// @param storage::v3::View for ToBoltVertex and ToBoltEdge. /// /// @throw std::bad_alloc -storage::v3::ShardResult ToBoltValue( - const query::v2::TypedValue &value, const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View view); +BoltResult ToBoltValue(const query::v2::TypedValue &value, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View view); query::v2::TypedValue ToTypedValue(const communication::bolt::Value &value); diff --git a/src/query/v2/plan/operator.cpp b/src/query/v2/plan/operator.cpp index 59e1c6d68..095061ebb 100644 --- a/src/query/v2/plan/operator.cpp +++ b/src/query/v2/plan/operator.cpp @@ -564,34 +564,6 @@ UniqueCursorPtr ScanAllById::MakeCursor(utils::MemoryResource *mem) const { std::move(vertices), "ScanAllById"); } -namespace { - -template -auto UnwrapEdgesResult(storage::v3::ShardResult &&result) { - if (result.HasError()) { - switch (result.GetError().code) { - case common::ErrorCode::DELETED_OBJECT: - throw QueryRuntimeException("Trying to get relationships of a deleted node."); - case common::ErrorCode::NONEXISTENT_OBJECT: - throw query::v2::QueryRuntimeException("Trying to get relationships from a node that doesn't exist."); - case common::ErrorCode::VERTEX_HAS_EDGES: - case common::ErrorCode::SERIALIZATION_ERROR: - case common::ErrorCode::PROPERTIES_DISABLED: - throw QueryRuntimeException("Unexpected error when accessing relationships."); - case common::ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: - case common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: - case common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: - case common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: - case common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: - case common::ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: - throw QueryRuntimeException("SchemaViolation occurred when accessing relationships."); - } - } - return std::move(*result); -} - -} // namespace - Expand::Expand(const std::shared_ptr &input, Symbol input_symbol, Symbol node_symbol, Symbol edge_symbol, EdgeAtom::Direction direction, const std::vector &edge_types, bool existing_node, storage::v3::View view) From d080e260e63c5e50091659e6619083911ead9ae7 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 22 Nov 2022 13:27:02 +0100 Subject: [PATCH 079/103] Remove redundant code --- src/expr/interpret/eval.hpp | 6 +++--- src/query/v2/common.hpp | 14 ------------- src/query/v2/plan/operator.cpp | 36 ---------------------------------- src/storage/v3/result.hpp | 2 ++ 4 files changed, 5 insertions(+), 53 deletions(-) diff --git a/src/expr/interpret/eval.hpp b/src/expr/interpret/eval.hpp index 758ea3797..a7d027ede 100644 --- a/src/expr/interpret/eval.hpp +++ b/src/expr/interpret/eval.hpp @@ -407,7 +407,7 @@ class ExpressionEvaluator : public ExpressionVisitor { typename TReturnType = std::enable_if_t, bool>> TReturnType HasLabelImpl(const VertexAccessor &vertex, const LabelIx &label, StorageTag /*tag*/) { auto has_label = vertex.HasLabel(view_, GetLabel(label)); - if (has_label.HasError() && has_label.GetError().code == Error::NONEXISTENT_OBJECT) { + if (has_label.HasError() && has_label.GetError() == Error::NONEXISTENT_OBJECT) { // This is a very nasty and temporary hack in order to make MERGE // work. The old storage had the following logic when returning an // `OLD` view: `return old ? old : new`. That means that if the @@ -746,7 +746,7 @@ class ExpressionEvaluator : public ExpressionVisitor { class TReturnType = std::enable_if_t, TypedValue>> TypedValue GetProperty(const TRecordAccessor &record_accessor, PropertyIx prop) { auto maybe_prop = record_accessor.GetProperty(view_, ctx_->properties[prop.ix]); - if (maybe_prop.HasError() && maybe_prop.GetError().code == Error::NONEXISTENT_OBJECT) { + if (maybe_prop.HasError() && maybe_prop.GetError() == Error::NONEXISTENT_OBJECT) { // This is a very nasty and temporary hack in order to make MERGE work. // The old storage had the following logic when returning an `OLD` view: // `return old ? old : new`. That means that if the `OLD` view didn't @@ -765,7 +765,7 @@ class ExpressionEvaluator : public ExpressionVisitor { class TReturnType = std::enable_if_t, TypedValue>> TypedValue GetProperty(const TRecordAccessor &record_accessor, const std::string_view name) { auto maybe_prop = record_accessor.GetProperty(view_, dba_->NameToProperty(name)); - if (maybe_prop.HasError() && maybe_prop.GetError().code == Error::NONEXISTENT_OBJECT) { + if (maybe_prop.HasError() && maybe_prop.GetError() == Error::NONEXISTENT_OBJECT) { // This is a very nasty and temporary hack in order to make MERGE work. // The old storage had the following logic when returning an `OLD` view: // `return old ? old : new`. That means that if the `OLD` view didn't diff --git a/src/query/v2/common.hpp b/src/query/v2/common.hpp index 774a90a37..089f3a4f9 100644 --- a/src/query/v2/common.hpp +++ b/src/query/v2/common.hpp @@ -81,19 +81,5 @@ inline void ExpectType(const Symbol &symbol, const TypedValue &value, TypedValue throw QueryRuntimeException("Expected a {} for '{}', but got {}.", expected, symbol.name(), value.type()); } -template -concept AccessorWithSetProperty = requires(T accessor, const storage::v3::PropertyId key, - const storage::v3::PropertyValue new_value) { - { accessor.SetProperty(key, new_value) } -> std::same_as>; -}; - -template -concept AccessorWithSetPropertyAndValidate = requires(T accessor, const storage::v3::PropertyId key, - const storage::v3::PropertyValue new_value) { - { - accessor.SetPropertyAndValidate(key, new_value) - } -> std::same_as>; -}; - int64_t QueryTimestamp(); } // namespace memgraph::query::v2 diff --git a/src/query/v2/plan/operator.cpp b/src/query/v2/plan/operator.cpp index 095061ebb..d82d83034 100644 --- a/src/query/v2/plan/operator.cpp +++ b/src/query/v2/plan/operator.cpp @@ -816,45 +816,9 @@ std::vector SetProperties::ModifiedSymbols(const SymbolTable &table) con SetProperties::SetPropertiesCursor::SetPropertiesCursor(const SetProperties &self, utils::MemoryResource *mem) : self_(self), input_cursor_(self.input_->MakeCursor(mem)) {} -namespace { - -template -concept AccessorWithProperties = requires(T value, storage::v3::PropertyId property_id, - storage::v3::PropertyValue property_value) { - { - value.ClearProperties() - } -> std::same_as>>; - {value.SetProperty(property_id, property_value)}; -}; - -} // namespace - bool SetProperties::SetPropertiesCursor::Pull(Frame &frame, ExecutionContext &context) { SCOPED_PROFILE_OP("SetProperties"); return false; - // if (!input_cursor_->Pull(frame, context)) return false; - // - // TypedValue &lhs = frame[self_.input_symbol_]; - // - // // Set, just like Create needs to see the latest changes. - // ExpressionEvaluator evaluator(&frame, context.symbol_table, context.evaluation_context, context.db_accessor, - // storage::v3::View::NEW); - // TypedValue rhs = self_.rhs_->Accept(evaluator); - // - // switch (lhs.type()) { - // case TypedValue::Type::Vertex: - // SetPropertiesOnRecord(&lhs.ValueVertex(), rhs, self_.op_, &context); - // break; - // case TypedValue::Type::Edge: - // SetPropertiesOnRecord(&lhs.ValueEdge(), rhs, self_.op_, &context); - // break; - // case TypedValue::Type::Null: - // // Skip setting properties on Null (can occur in optional match). - // break; - // default: - // throw QueryRuntimeException("Properties can only be set on edges and vertices."); - // } - // return true; } void SetProperties::SetPropertiesCursor::Shutdown() { input_cursor_->Shutdown(); } diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index b86ede6b4..2b5b316bb 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -37,6 +37,8 @@ struct ShardError { std::string source; inline friend bool operator==(const ShardError &lhs, const ShardError &rhs) { return lhs.code == rhs.code; } + + inline friend bool operator==(const ShardError &lhs, const common::ErrorCode rhs) { return lhs.code == rhs; } }; // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) From 2a6dc7bb934f7de76a406d239e06f5aa5e2b3bf6 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 22 Nov 2022 13:49:12 +0100 Subject: [PATCH 080/103] Remove success --- src/query/v2/requests.hpp | 10 ---- src/query/v2/shard_request_manager.hpp | 10 ++-- src/storage/v3/shard_rsm.cpp | 58 +++++-------------- tests/simulation/shard_rsm.cpp | 26 ++++----- tests/simulation/test_cluster.hpp | 2 +- tests/unit/high_density_shard_create_scan.cpp | 2 +- tests/unit/machine_manager.cpp | 2 +- 7 files changed, 37 insertions(+), 73 deletions(-) diff --git a/src/query/v2/requests.hpp b/src/query/v2/requests.hpp index 62a4e93f8..f61275377 100644 --- a/src/query/v2/requests.hpp +++ b/src/query/v2/requests.hpp @@ -367,7 +367,6 @@ struct ScanResultRow { }; struct ScanVerticesResponse { - bool success; std::optional error; std::optional next_start_id; std::vector results; @@ -388,7 +387,6 @@ struct GetPropertiesRequest { }; struct GetPropertiesResponse { - bool success; std::optional error; }; @@ -454,7 +452,6 @@ struct ExpandOneResultRow { }; struct ExpandOneResponse { - bool success; std::optional error; std::vector result; }; @@ -489,7 +486,6 @@ struct CreateVerticesRequest { }; struct CreateVerticesResponse { - bool success; std::optional error; }; @@ -501,7 +497,6 @@ struct DeleteVerticesRequest { }; struct DeleteVerticesResponse { - bool success; std::optional error; }; @@ -511,7 +506,6 @@ struct UpdateVerticesRequest { }; struct UpdateVerticesResponse { - bool success; std::optional error; }; @@ -534,7 +528,6 @@ struct CreateExpandRequest { }; struct CreateExpandResponse { - bool success; std::optional error; }; @@ -544,7 +537,6 @@ struct DeleteEdgesRequest { }; struct DeleteEdgesResponse { - bool success; std::optional error; }; @@ -554,7 +546,6 @@ struct UpdateEdgesRequest { }; struct UpdateEdgesResponse { - bool success; std::optional error; }; @@ -564,7 +555,6 @@ struct CommitRequest { }; struct CommitResponse { - bool success; std::optional error; }; diff --git a/src/query/v2/shard_request_manager.hpp b/src/query/v2/shard_request_manager.hpp index a73201046..20bae7b97 100644 --- a/src/query/v2/shard_request_manager.hpp +++ b/src/query/v2/shard_request_manager.hpp @@ -206,7 +206,7 @@ class ShardRequestManager : public ShardRequestManagerInterface { } WriteResponses write_response_variant = commit_response.GetValue(); auto &response = std::get(write_response_variant); - if (!response.success) { + if (response.error) { throw std::runtime_error("Commit request did not succeed"); } } @@ -311,7 +311,7 @@ class ShardRequestManager : public ShardRequestManagerInterface { WriteResponses response_variant = write_response_result.GetValue(); CreateExpandResponse mapped_response = std::get(response_variant); - if (!mapped_response.success) { + if (mapped_response.error) { throw std::runtime_error("CreateExpand request did not succeed"); } responses.push_back(mapped_response); @@ -601,7 +601,7 @@ class ShardRequestManager : public ShardRequestManagerInterface { WriteResponses response_variant = poll_result->GetValue(); auto response = std::get(response_variant); - if (!response.success) { + if (response.error) { throw std::runtime_error("CreateVertices request did not succeed"); } responses.push_back(response); @@ -637,7 +637,7 @@ class ShardRequestManager : public ShardRequestManagerInterface { // Currently a boolean flag for signaling the overall success of the // ExpandOne request does not exist. But it should, so here we assume // that it is already in place. - if (!response.success) { + if (response.error) { throw std::runtime_error("ExpandOne request did not succeed"); } @@ -680,7 +680,7 @@ class ShardRequestManager : public ShardRequestManagerInterface { ReadResponses read_response_variant = await_result->GetValue(); auto response = std::get(read_response_variant); - if (!response.success) { + if (response.error) { throw std::runtime_error("ScanAll request did not succeed"); } diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 1ff8e04b7..7a128a78f 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -487,7 +487,6 @@ auto CreateErrorResponse(const ShardError &shard_error, const auto transaction_i msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); - bool action_successful = true; std::optional shard_error; for (auto &new_vertex : req.new_vertices) { @@ -510,28 +509,25 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { if (result_schema.HasError()) { shard_error.emplace(CreateErrorResponse(result_schema.GetError(), req.transaction_id, "creating vertices")); - action_successful = false; break; } } - return msgs::CreateVerticesResponse{action_successful, std::move(shard_error)}; + return msgs::CreateVerticesResponse{std::move(shard_error)}; } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); - bool action_successful = true; std::optional shard_error; for (auto &vertex : req.new_properties) { - if (!action_successful) { + if (shard_error) { break; } auto vertex_to_update = acc.FindVertex(ConvertPropertyVector(std::move(vertex.primary_key)), View::OLD); if (!vertex_to_update) { - action_successful = false; shard_error.emplace(msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND}); spdlog::debug("In transaction {} vertex could not be found while trying to update its properties.", req.transaction_id.logical_id); @@ -542,18 +538,16 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateVerticesRequest &&req) { auto result_schema = vertex_to_update->SetPropertyAndValidate(update_prop.first, ToPropertyValue(std::move(update_prop.second))); if (result_schema.HasError()) { - action_successful = false; shard_error.emplace(CreateErrorResponse(result_schema.GetError(), req.transaction_id, "updating vertices")); break; } } } - return msgs::UpdateVerticesResponse{action_successful, std::move(shard_error)}; + return msgs::UpdateVerticesResponse{std::move(shard_error)}; } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::DeleteVerticesRequest &&req) { - bool action_successful = true; std::optional shard_error; auto acc = shard_->Access(req.transaction_id); @@ -563,7 +557,6 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::DeleteVerticesRequest &&req) { if (!vertex_acc) { spdlog::debug("Error while trying to delete vertex. Vertex to delete does not exist. Transaction id: {}", req.transaction_id.logical_id); - action_successful = false; shard_error.emplace(msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND}); spdlog::debug("In transaction {} vertex could not be found while trying to delete it.", req.transaction_id.logical_id); @@ -576,7 +569,6 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::DeleteVerticesRequest &&req) { case msgs::DeleteVerticesRequest::DeletionType::DELETE: { auto result = acc.DeleteVertex(&vertex_acc.value()); if (result.HasError() || !(result.GetValue().has_value())) { - action_successful = false; shard_error.emplace(CreateErrorResponse(result.GetError(), req.transaction_id, "deleting vertices")); } break; @@ -584,23 +576,21 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::DeleteVerticesRequest &&req) { case msgs::DeleteVerticesRequest::DeletionType::DETACH_DELETE: { auto result = acc.DetachDeleteVertex(&vertex_acc.value()); if (result.HasError() || !(result.GetValue().has_value())) { - action_successful = false; shard_error.emplace(CreateErrorResponse(result.GetError(), req.transaction_id, "deleting vertices")); } break; } } - if (!action_successful) { + if (shard_error) { break; } } - return msgs::DeleteVerticesResponse{action_successful, std::move(shard_error)}; + return msgs::DeleteVerticesResponse{std::move(shard_error)}; } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { auto acc = shard_->Access(req.transaction_id); - bool action_successful = true; std::optional shard_error; for (auto &new_expand : req.new_expands) { @@ -611,7 +601,6 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { VertexId{new_expand.dest_vertex.first.id, ConvertPropertyVector(std::move(new_expand.dest_vertex.second))}; if (!(shard_->IsVertexBelongToShard(from_vertex_id) || shard_->IsVertexBelongToShard(to_vertex_id))) { - action_successful = false; shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND, "Error while trying to insert edge, none of the vertices belong to this shard"}; spdlog::debug("Error while trying to insert edge, none of the vertices belong to this shard. Transaction id: {}", @@ -625,19 +614,17 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { if (!new_expand.properties.empty()) { for (const auto &[property, value] : new_expand.properties) { if (const auto maybe_error = edge.SetProperty(property, ToPropertyValue(value)); maybe_error.HasError()) { - action_successful = false; shard_error.emplace( CreateErrorResponse(maybe_error.GetError(), req.transaction_id, "setting edge property")); break; } } - if (!action_successful) { + if (shard_error) { break; } } } else { // TODO Code for this - action_successful = false; shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND}; spdlog::debug("Creating edge was not successful. Transaction id: {}", req.transaction_id.logical_id); break; @@ -648,7 +635,6 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { for (auto &[edge_prop_key, edge_prop_val] : new_expand.properties) { auto set_result = edge_acc->SetProperty(edge_prop_key, ToPropertyValue(std::move(edge_prop_val))); if (set_result.HasError()) { - action_successful = false; shard_error.emplace(CreateErrorResponse(set_result.GetError(), req.transaction_id, "adding edge property")); break; } @@ -656,16 +642,15 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateExpandRequest &&req) { } } - return msgs::CreateExpandResponse{action_successful, std::move(shard_error)}; + return msgs::CreateExpandResponse{std::move(shard_error)}; } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::DeleteEdgesRequest &&req) { - bool action_successful = true; std::optional shard_error; auto acc = shard_->Access(req.transaction_id); for (auto &edge : req.edges) { - if (!action_successful) { + if (shard_error) { break; } @@ -673,30 +658,27 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::DeleteEdgesRequest &&req) { VertexId(edge.dst.first.id, ConvertPropertyVector(std::move(edge.dst.second))), Gid::FromUint(edge.id.gid)); if (edge_acc.HasError() || !edge_acc.HasValue()) { - action_successful = false; shard_error.emplace(CreateErrorResponse(edge_acc.GetError(), req.transaction_id, "delete edge")); continue; } } - return msgs::DeleteEdgesResponse{action_successful, std::move(shard_error)}; + return msgs::DeleteEdgesResponse{std::move(shard_error)}; } msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { // TODO(antaljanosbenjamin): handle when the vertex is the destination vertex auto acc = shard_->Access(req.transaction_id); - bool action_successful = true; std::optional shard_error; for (auto &edge : req.new_properties) { - if (!action_successful) { + if (shard_error) { break; } auto vertex_acc = acc.FindVertex(ConvertPropertyVector(std::move(edge.src.second)), View::OLD); if (!vertex_acc) { - action_successful = false; shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND, "Source vertex was not found"}; spdlog::debug("Encountered an error while trying to acquire VertexAccessor with transaction id: {}", req.transaction_id.logical_id); @@ -707,7 +689,6 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { // in the vertex's out-going edges auto edges_res = vertex_acc->OutEdges(View::OLD); if (edges_res.HasError()) { - action_successful = false; shard_error.emplace(CreateErrorResponse(edges_res.GetError(), req.transaction_id, "update edge")); continue; } @@ -734,19 +715,17 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::UpdateEdgesRequest &&req) { if (!edge_accessor_did_match) { // TODO(jbajic) Do we need this shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND, "Edge was not found"}; - action_successful = false; spdlog::debug("Could not find the Edge with the specified Gid. Transaction id: {}", req.transaction_id.logical_id); continue; } } - return msgs::UpdateEdgesResponse{action_successful, std::move(shard_error)}; + return msgs::UpdateEdgesResponse{std::move(shard_error)}; } msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { auto acc = shard_->Access(req.transaction_id); - bool action_successful = true; std::optional shard_error; std::vector results; @@ -786,7 +765,6 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { // Is it useful to return just a vertex without the properties? if (!found_props) { shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND, "Requested properties were not found!"}; - action_successful = false; } results.emplace_back(msgs::ScanResultRow{.vertex = ConstructValueVertex(vertex, view).vertex_v, @@ -834,9 +812,8 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { } } - msgs::ScanVerticesResponse resp{.success = action_successful, .error = std::move(shard_error)}; - - if (action_successful) { + msgs::ScanVerticesResponse resp{.error = std::move(shard_error)}; + if (resp.error) { resp.next_start_id = next_start_id; resp.results = std::move(results); } @@ -846,7 +823,6 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { auto acc = shard_->Access(req.transaction_id); - bool action_successful = true; std::optional shard_error; std::vector results; @@ -859,7 +835,6 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { auto src_vertex_acc_opt = acc.FindVertex(ConvertPropertyVector((src_vertex.second)), View::NEW); if (!src_vertex_acc_opt) { shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND, "Source vertex was not found."}; - action_successful = false; spdlog::debug("Encountered an error while trying to obtain VertexAccessor. Transaction id: {}", req.transaction_id.logical_id); break; @@ -879,15 +854,14 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { if (result.HasError()) { // Code Error shard_error = msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND, "Source vertex was not found."}; - action_successful = false; break; } results.emplace_back(result.GetValue()); } - msgs::ExpandOneResponse resp{.success = action_successful, .error = std::move(shard_error)}; - if (action_successful) { + msgs::ExpandOneResponse resp{.error = std::move(shard_error)}; + if (!resp.error) { resp.result = std::move(results); } @@ -896,7 +870,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CommitRequest &&req) { shard_->Access(req.transaction_id).Commit(req.commit_timestamp); - return msgs::CommitResponse{true}; + return msgs::CommitResponse{}; }; // NOLINTNEXTLINE(readability-convert-member-functions-to-static) diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index 64d0a0861..b9511f024 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -137,7 +137,7 @@ void Commit(ShardClient &client, const coordinator::Hlc &transaction_timestamp) auto write_response_result = write_res.GetValue(); auto write_response = std::get(write_response_result); - MG_ASSERT(write_response.success, "Commit expected to be successful, but it is failed"); + MG_ASSERT(!write_response.error.has_value(), "Commit expected to be successful, but it is failed"); break; } @@ -156,7 +156,7 @@ bool AttemptToCreateVertex(ShardClient &client, int64_t value) { create_req.transaction_id.logical_id = GetTransactionId(); auto write_res = client.SendWriteRequest(create_req); - MG_ASSERT(write_res.HasValue() && std::get(write_res.GetValue()).success, + MG_ASSERT(write_res.HasValue() && std::get(write_res.GetValue()).error, "Unexpected failure"); Commit(client, create_req.transaction_id); @@ -179,7 +179,7 @@ bool AttemptToDeleteVertex(ShardClient &client, int64_t value) { auto write_response = std::get(write_response_result); Commit(client, delete_req.transaction_id); - return write_response.success; + return !write_response.error.has_value(); } } @@ -207,7 +207,7 @@ bool AttemptToUpdateVertex(ShardClient &client, int64_t value) { auto write_response = std::get(write_response_result); Commit(client, update_req.transaction_id); - return write_response.success; + return !write_response.error.has_value(); } } @@ -244,7 +244,7 @@ bool AttemptToAddEdge(ShardClient &client, int64_t value_of_vertex_1, int64_t va Commit(client, create_req.transaction_id); - return write_response.success; + return !write_response.error.has_value(); } return true; } @@ -276,7 +276,7 @@ bool AttemptToAddEdgeWithProperties(ShardClient &client, int64_t value_of_vertex create_req.transaction_id.logical_id = GetTransactionId(); auto write_res = client.SendWriteRequest(create_req); - MG_ASSERT(write_res.HasValue() && std::get(write_res.GetValue()).success, + MG_ASSERT(write_res.HasValue() && !std::get(write_res.GetValue()).error.has_value(), "Unexpected failure"); Commit(client, create_req.transaction_id); @@ -316,7 +316,7 @@ bool AttemptToDeleteEdge(ShardClient &client, int64_t value_of_vertex_1, int64_t auto write_response = std::get(write_response_result); Commit(client, delete_req.transaction_id); - return write_response.success; + return !write_response.error.has_value(); } } @@ -356,7 +356,7 @@ bool AttemptToUpdateEdge(ShardClient &client, int64_t value_of_vertex_1, int64_t auto write_response = std::get(write_response_result); Commit(client, update_req.transaction_id); - return write_response.success; + return !write_response.error.has_value(); } } @@ -379,7 +379,7 @@ std::tuple> AttemptToScanAllWithoutBatchLi auto write_response_result = read_res.GetValue(); auto write_response = std::get(write_response_result); - MG_ASSERT(write_response.success); + MG_ASSERT(write_response.error == std::nullopt); return {write_response.results.size(), write_response.next_start_id}; } @@ -405,7 +405,7 @@ std::tuple> AttemptToScanAllWithBatchLimit auto write_response_result = read_res.GetValue(); auto write_response = std::get(write_response_result); - MG_ASSERT(write_response.success); + MG_ASSERT(!write_response.error.has_value()); return {write_response.results.size(), write_response.next_start_id}; } @@ -439,7 +439,7 @@ std::tuple> AttemptToScanAllWithExpression auto write_response_result = read_res.GetValue(); auto write_response = std::get(write_response_result); - MG_ASSERT(write_response.success); + MG_ASSERT(!write_response.error.has_value()); MG_ASSERT(!write_response.results.empty(), "There are no results!"); MG_ASSERT(write_response.results[0].evaluated_vertex_expressions[0].int_v == 4); return {write_response.results.size(), write_response.next_start_id}; @@ -464,7 +464,7 @@ void AttemptToScanAllWithOrderByOnPrimaryProperty(ShardClient &client, msgs::Ver auto write_response_result = read_res.GetValue(); auto write_response = std::get(write_response_result); - MG_ASSERT(write_response.success); + MG_ASSERT(!write_response.error.has_value()); MG_ASSERT(write_response.results.size() == 5, "Expecting 5 results!"); for (int64_t i{0}; i < 5; ++i) { const auto expected_primary_key = std::vector{msgs::Value(1023 - i)}; @@ -494,7 +494,7 @@ void AttemptToScanAllWithOrderByOnSecondaryProperty(ShardClient &client, msgs::V auto write_response_result = read_res.GetValue(); auto write_response = std::get(write_response_result); - MG_ASSERT(write_response.success); + MG_ASSERT(!write_response.error.has_value()); MG_ASSERT(write_response.results.size() == 5, "Expecting 5 results!"); for (int64_t i{0}; i < 5; ++i) { const auto expected_prop4 = std::vector{msgs::Value(1023 - i)}; diff --git a/tests/simulation/test_cluster.hpp b/tests/simulation/test_cluster.hpp index 6a32a391d..ce304d1cc 100644 --- a/tests/simulation/test_cluster.hpp +++ b/tests/simulation/test_cluster.hpp @@ -173,7 +173,7 @@ void ExecuteOp(msgs::ShardRequestManager &shard_request_mana auto result = shard_request_manager.Request(state, std::move(new_vertices)); RC_ASSERT(result.size() == 1); - RC_ASSERT(result[0].success); + RC_ASSERT(!result[0].error.has_value()); correctness_model.emplace(std::make_pair(create_vertex.first, create_vertex.second)); } diff --git a/tests/unit/high_density_shard_create_scan.cpp b/tests/unit/high_density_shard_create_scan.cpp index 9c2d1cfd7..a0c0a0c28 100644 --- a/tests/unit/high_density_shard_create_scan.cpp +++ b/tests/unit/high_density_shard_create_scan.cpp @@ -187,7 +187,7 @@ void ExecuteOp(msgs::ShardRequestManager &shard_request_manager, auto result = shard_request_manager.Request(state, std::move(new_vertices)); MG_ASSERT(result.size() == 1); - MG_ASSERT(result[0].success); + MG_ASSERT(!result[0].error.has_value()); correctness_model.emplace(std::make_pair(create_vertex.first, create_vertex.second)); } diff --git a/tests/unit/machine_manager.cpp b/tests/unit/machine_manager.cpp index 1d69da5c5..7b57d61ea 100644 --- a/tests/unit/machine_manager.cpp +++ b/tests/unit/machine_manager.cpp @@ -151,7 +151,7 @@ void TestCreateExpand(msgs::ShardRequestManagerInterface &shard_request_manager) auto responses = shard_request_manager.Request(state, std::move(new_expands)); MG_ASSERT(responses.size() == 1); - MG_ASSERT(responses[0].success); + MG_ASSERT(!responses[0].error.has_value()); } void TestExpandOne(msgs::ShardRequestManagerInterface &shard_request_manager) { From 1101c2444c5bc8ec0140892d71eff5c885e11f12 Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 22 Nov 2022 14:01:16 +0100 Subject: [PATCH 081/103] Make ConvertPropertyMap expect ref and not rvalue --- src/storage/v3/shard_rsm.cpp | 2 +- src/storage/v3/value_conversions.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index d3cf8e4c3..97e5af703 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -66,7 +66,7 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::CreateVerticesRequest &&req) { /// TODO(gvolfing) Consider other methods than converting. Change either /// the way that the property map is stored in the messages, or the /// signature of CreateVertexAndValidate. - auto converted_property_map = ConvertPropertyMap(std::move(new_vertex.properties)); + auto converted_property_map = ConvertPropertyMap(new_vertex.properties); // TODO(gvolfing) make sure if this conversion is actually needed. std::vector converted_label_ids; diff --git a/src/storage/v3/value_conversions.hpp b/src/storage/v3/value_conversions.hpp index 80b6f02b9..53374e1ed 100644 --- a/src/storage/v3/value_conversions.hpp +++ b/src/storage/v3/value_conversions.hpp @@ -131,7 +131,7 @@ inline msgs::VertexId ToMsgsVertexId(const v3::VertexId &vertex_id) { } inline std::vector> ConvertPropertyMap( - std::vector> &&properties) { + std::vector> &properties) { std::vector> ret; ret.reserve(properties.size()); From 5717dfb165f23537d8fec566897aa2e3902b8af2 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 22 Nov 2022 14:05:04 +0100 Subject: [PATCH 082/103] Add ErrorCode to SHARD_ERROR macro --- src/glue/v2/communication.cpp | 2 +- src/storage/v3/edge_accessor.cpp | 20 ++--- src/storage/v3/result.hpp | 6 +- src/storage/v3/schema_validator.cpp | 8 +- src/storage/v3/shard.cpp | 23 +++--- src/storage/v3/vertex_accessor.cpp | 64 +++++++-------- tests/unit/storage_v3.cpp | 90 +++++++++++----------- tests/unit/storage_v3_edge.cpp | 10 +-- tests/unit/storage_v3_schema.cpp | 22 +++--- tests/unit/storage_v3_vertex_accessors.cpp | 12 +-- 10 files changed, 130 insertions(+), 127 deletions(-) diff --git a/src/glue/v2/communication.cpp b/src/glue/v2/communication.cpp index a977e0e29..bc8d2da09 100644 --- a/src/glue/v2/communication.cpp +++ b/src/glue/v2/communication.cpp @@ -112,7 +112,7 @@ BoltResult ToBoltPath(const query::v2::accessors::Pat const msgs::ShardRequestManagerInterface * /*shard_request_manager*/, storage::v3::View /*view*/) { // TODO(jbajic) Fix bolt communication - return {SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)}; + return {SHARD_ERROR(ErrorCode::DELETED_OBJECT)}; } BoltResult ToBoltValue(const query::v2::TypedValue &value, diff --git a/src/storage/v3/edge_accessor.cpp b/src/storage/v3/edge_accessor.cpp index 4438e0acb..95dd6875a 100644 --- a/src/storage/v3/edge_accessor.cpp +++ b/src/storage/v3/edge_accessor.cpp @@ -57,11 +57,11 @@ const VertexId &EdgeAccessor::ToVertex() const { return to_vertex_; } ShardResult EdgeAccessor::SetProperty(PropertyId property, const PropertyValue &value) { utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!config_.properties_on_edges) return SHARD_ERROR(common::ErrorCode::PROPERTIES_DISABLED); + if (!config_.properties_on_edges) return SHARD_ERROR(ErrorCode::PROPERTIES_DISABLED); - if (!PrepareForWrite(transaction_, edge_.ptr)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, edge_.ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (edge_.ptr->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (edge_.ptr->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto current_value = edge_.ptr->properties.GetProperty(property); // We could skip setting the value if the previous one is the same to the new @@ -77,11 +77,11 @@ ShardResult EdgeAccessor::SetProperty(PropertyId property, const } ShardResult> EdgeAccessor::ClearProperties() { - if (!config_.properties_on_edges) return SHARD_ERROR(common::ErrorCode::PROPERTIES_DISABLED); + if (!config_.properties_on_edges) return SHARD_ERROR(ErrorCode::PROPERTIES_DISABLED); - if (!PrepareForWrite(transaction_, edge_.ptr)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, edge_.ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (edge_.ptr->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (edge_.ptr->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto properties = edge_.ptr->properties.Properties(); for (const auto &property : properties) { @@ -129,8 +129,8 @@ ShardResult EdgeAccessor::GetProperty(PropertyId property, View v break; } }); - if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(value); } @@ -175,8 +175,8 @@ ShardResult> EdgeAccessor::Properties(View v break; } }); - if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(properties); } diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index 2b5b316bb..136ec0136 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -42,7 +42,11 @@ struct ShardError { }; // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) -#define SHARD_ERROR(...) memgraph::storage::v3::ShardError(__VA_ARGS__, std::experimental::source_location::current()) +#define SHARD_ERROR(...) \ + ({ \ + using ErrorCode = memgraph::common::ErrorCode; \ + memgraph::storage::v3::ShardError(__VA_ARGS__, std::experimental::source_location::current()); \ + }) template using ShardResult = utils::BasicResult; diff --git a/src/storage/v3/schema_validator.cpp b/src/storage/v3/schema_validator.cpp index 97c068524..7854dc389 100644 --- a/src/storage/v3/schema_validator.cpp +++ b/src/storage/v3/schema_validator.cpp @@ -31,14 +31,14 @@ std::optional SchemaValidator::ValidateVertexCreate( // Schema on primary label const auto *schema = schemas_->GetSchema(primary_label); if (schema == nullptr) { - return SHARD_ERROR(common::ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL, + return SHARD_ERROR(ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL, fmt::format("Schema not defined for label :{}", name_id_mapper_->IdToName(primary_label))); } // Is there another primary label among secondary labels for (const auto &secondary_label : labels) { if (schemas_->GetSchema(secondary_label)) { - return SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY, + return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY, fmt::format("Cannot add label :{}, since it is defined as a primary label", name_id_mapper_->IdToName(secondary_label))); } @@ -46,7 +46,7 @@ std::optional SchemaValidator::ValidateVertexCreate( // Quick size check if (schema->second.size() != primary_properties.size()) { - return SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED, + return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED, fmt::format("Not all primary properties have been specified for :{} vertex", name_id_mapper_->IdToName(primary_label))); } @@ -88,7 +88,7 @@ std::optional SchemaValidator::ValidatePropertyUpdate(const LabelId std::optional SchemaValidator::ValidateLabelUpdate(const LabelId label) const { const auto *schema = schemas_->GetSchema(label); if (schema) { - return SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL, + return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL, fmt::format("Cannot add/remove primary label :{}", name_id_mapper_->IdToName(label))); } return std::nullopt; diff --git a/src/storage/v3/shard.cpp b/src/storage/v3/shard.cpp index bad7e8d0a..4f25916c6 100644 --- a/src/storage/v3/shard.cpp +++ b/src/storage/v3/shard.cpp @@ -364,7 +364,7 @@ ShardResult Shard::Accessor::CreateVertexAndValidate( VertexAccessor vertex_acc{&it->vertex, transaction_, &shard_->indices_, config_, shard_->vertex_validator_}; if (!inserted) { - return SHARD_ERROR(common::ErrorCode::VERTEX_ALREADY_INSERTED); + return SHARD_ERROR(ErrorCode::VERTEX_ALREADY_INSERTED); } MG_ASSERT(it != acc.end(), "Invalid Vertex accessor!"); @@ -401,14 +401,13 @@ ShardResult> Shard::Accessor::DeleteVertex(VertexA "accessor when deleting a vertex!"); auto *vertex_ptr = vertex->vertex_; - if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); if (vertex_ptr->deleted) { return std::optional{}; } - if (!vertex_ptr->in_edges.empty() || !vertex_ptr->out_edges.empty()) - return SHARD_ERROR(common::ErrorCode::VERTEX_HAS_EDGES); + if (!vertex_ptr->in_edges.empty() || !vertex_ptr->out_edges.empty()) return SHARD_ERROR(ErrorCode::VERTEX_HAS_EDGES); CreateAndLinkDelta(transaction_, vertex_ptr, Delta::RecreateObjectTag()); vertex_ptr->deleted = true; @@ -430,7 +429,7 @@ ShardResult>>> std::vector out_edges; { - if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); if (vertex_ptr->deleted) return std::optional{}; @@ -471,7 +470,7 @@ ShardResult>>> // vertex. Some other transaction could have modified the vertex in the // meantime if we didn't have any edges to delete. - if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_ptr)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); MG_ASSERT(!vertex_ptr->deleted, "Invalid database state!"); @@ -508,12 +507,12 @@ ShardResult Shard::Accessor::CreateEdge(VertexId from_vertex_id, V } if (from_is_local) { - if (!PrepareForWrite(transaction_, from_vertex)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); - if (from_vertex->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (!PrepareForWrite(transaction_, from_vertex)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (from_vertex->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); } if (to_is_local && to_vertex != from_vertex) { - if (!PrepareForWrite(transaction_, to_vertex)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); - if (to_vertex->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (!PrepareForWrite(transaction_, to_vertex)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); + if (to_vertex->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); } EdgeRef edge(gid); @@ -568,13 +567,13 @@ ShardResult> Shard::Accessor::DeleteEdge(VertexId fr if (from_is_local) { if (!PrepareForWrite(transaction_, from_vertex)) { - return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); } MG_ASSERT(!from_vertex->deleted, "Invalid database state!"); } if (to_is_local && to_vertex != from_vertex) { if (!PrepareForWrite(transaction_, to_vertex)) { - return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); } MG_ASSERT(!to_vertex->deleted, "Invalid database state!"); } diff --git a/src/storage/v3/vertex_accessor.cpp b/src/storage/v3/vertex_accessor.cpp index 88f9aa965..6949c08cc 100644 --- a/src/storage/v3/vertex_accessor.cpp +++ b/src/storage/v3/vertex_accessor.cpp @@ -83,9 +83,9 @@ bool VertexAccessor::IsVisible(View view) const { ShardResult VertexAccessor::AddLabel(LabelId label) { utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); if (std::find(vertex_->labels.begin(), vertex_->labels.end(), label) != vertex_->labels.end()) return false; @@ -104,9 +104,9 @@ ShardResult VertexAccessor::AddLabelAndValidate(LabelId label) { } utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); if (std::find(vertex_->labels.begin(), vertex_->labels.end(), label) != vertex_->labels.end()) return false; @@ -120,9 +120,9 @@ ShardResult VertexAccessor::AddLabelAndValidate(LabelId label) { } ShardResult VertexAccessor::RemoveLabel(LabelId label) { - if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto it = std::find(vertex_->labels.begin(), vertex_->labels.end(), label); if (it == vertex_->labels.end()) return false; @@ -139,9 +139,9 @@ ShardResult VertexAccessor::RemoveLabelAndValidate(LabelId label) { return {*maybe_violation_error}; } - if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto it = std::find(vertex_->labels.begin(), vertex_->labels.end(), label); if (it == vertex_->labels.end()) return false; @@ -197,8 +197,8 @@ ShardResult VertexAccessor::HasLabel(LabelId label, View view) const { break; } }); - if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return has_label; } @@ -267,17 +267,17 @@ ShardResult> VertexAccessor::Labels(View view) const { break; } }); - if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(labels); } ShardResult VertexAccessor::SetProperty(PropertyId property, const PropertyValue &value) { utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; - if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto current_value = vertex_->properties.GetProperty(property); // We could skip setting the value if the previous one is the same to the new @@ -323,10 +323,10 @@ ShardResult VertexAccessor::CheckVertexExistence(View view) const { } }); if (!exists) { - return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); + return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); } if (!for_deleted_ && deleted) { - return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + return SHARD_ERROR(ErrorCode::DELETED_OBJECT); } return {}; } @@ -338,11 +338,11 @@ ShardResult VertexAccessor::SetPropertyAndValidate(PropertyId pro utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; if (!PrepareForWrite(transaction_, vertex_)) { - return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); } if (vertex_->deleted) { - return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + return SHARD_ERROR(ErrorCode::DELETED_OBJECT); } auto current_value = vertex_->properties.GetProperty(property); @@ -361,9 +361,9 @@ ShardResult VertexAccessor::SetPropertyAndValidate(PropertyId pro } ShardResult> VertexAccessor::ClearProperties() { - if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR); + if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); - if (vertex_->deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (vertex_->deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); auto properties = vertex_->properties.Properties(); for (const auto &property : properties) { @@ -441,8 +441,8 @@ ShardResult VertexAccessor::GetProperty(PropertyId property, View break; } }); - if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(value); } @@ -491,8 +491,8 @@ ShardResult> VertexAccessor::Properties(View break; } }); - if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return std::move(properties); } @@ -563,8 +563,8 @@ ShardResult> VertexAccessor::InEdges(View view, const break; } }); - if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); - if (deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); std::vector ret; if (in_edges.empty()) { return ret; @@ -643,8 +643,8 @@ ShardResult> VertexAccessor::OutEdges(View view, const break; } }); - if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); - if (deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); std::vector ret; if (out_edges.empty()) { return ret; @@ -690,8 +690,8 @@ ShardResult VertexAccessor::InDegree(View view) const { break; } }); - if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return degree; } @@ -727,8 +727,8 @@ ShardResult VertexAccessor::OutDegree(View view) const { break; } }); - if (!exists) return SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT); - if (!for_deleted_ && deleted) return SHARD_ERROR(common::ErrorCode::DELETED_OBJECT); + if (!exists) return SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT); + if (!for_deleted_ && deleted) return SHARD_ERROR(ErrorCode::DELETED_OBJECT); return degree; } diff --git a/tests/unit/storage_v3.cpp b/tests/unit/storage_v3.cpp index a214ccac6..cab463b7f 100644 --- a/tests/unit/storage_v3.cpp +++ b/tests/unit/storage_v3.cpp @@ -580,7 +580,7 @@ TEST_P(StorageV3, VertexDeleteSerializationError) { EXPECT_EQ(CountVertices(acc2, View::NEW), 1U); auto res = acc2.DeleteVertex(&*vertex); ASSERT_TRUE(res.HasError()); - ASSERT_EQ(res.GetError(), SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR)); + ASSERT_EQ(res.GetError(), SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR)); EXPECT_EQ(CountVertices(acc2, View::OLD), 1U); EXPECT_EQ(CountVertices(acc2, View::NEW), 1U); acc2.AdvanceCommand(); @@ -711,20 +711,20 @@ TEST_P(StorageV3, VertexDeleteLabel) { // Check whether label 5 exists ASSERT_FALSE(vertex->HasLabel(label5, View::OLD).GetValue()); - ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); ASSERT_EQ(vertex->Labels(View::OLD)->size(), 0); - ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); // Try to add the label { auto ret = vertex->AddLabelAndValidate(label5); - AssertShardErrorEqual(ret, SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); } // Try to remove the label { auto ret = vertex->RemoveLabelAndValidate(label5); - AssertShardErrorEqual(ret, SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); } acc.Abort(); @@ -779,33 +779,33 @@ TEST_P(StorageV3, VertexDeleteLabel) { // Check whether label 5 exists ASSERT_TRUE(vertex->HasLabel(label5, View::OLD).GetValue()); - ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); { auto labels = vertex->Labels(View::OLD).GetValue(); ASSERT_EQ(labels.size(), 1); ASSERT_EQ(labels[0], label5); } - ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); // Advance command acc.AdvanceCommand(); // Check whether label 5 exists - ASSERT_EQ(vertex->HasLabel(label5, View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex->Labels(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->HasLabel(label5, View::OLD).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->HasLabel(label5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Labels(View::OLD).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Labels(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); // Try to add the label { auto ret = vertex->AddLabelAndValidate(label5); - AssertShardErrorEqual(ret, SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); } // Try to remove the label { auto ret = vertex->RemoveLabelAndValidate(label5); - AssertShardErrorEqual(ret, SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); } acc.Abort(); @@ -855,14 +855,14 @@ TEST_P(StorageV3, VertexDeleteProperty) { // Check whether label 5 exists ASSERT_TRUE(vertex->GetProperty(property5, View::OLD)->IsNull()); - ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); ASSERT_EQ(vertex->Properties(View::OLD)->size(), 0); - ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); // Try to set the property5 { auto ret = vertex->SetPropertyAndValidate(property5, PropertyValue("haihai")); - AssertShardErrorEqual(ret, SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); } acc.Abort(); @@ -918,27 +918,27 @@ TEST_P(StorageV3, VertexDeleteProperty) { // Check whether property 5 exists ASSERT_EQ(vertex->GetProperty(property5, View::OLD)->ValueString(), "nandare"); - ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); { auto properties = vertex->Properties(View::OLD).GetValue(); ASSERT_EQ(properties.size(), 1); ASSERT_EQ(properties[property5].ValueString(), "nandare"); } - ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); // Advance command acc.AdvanceCommand(); // Check whether property 5 exists - ASSERT_EQ(vertex->GetProperty(property5, View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex->Properties(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->GetProperty(property5, View::OLD).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->GetProperty(property5, View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Properties(View::OLD).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex->Properties(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); // Try to set the property { auto ret = vertex->SetPropertyAndValidate(property5, PropertyValue("haihai")); - AssertShardErrorEqual(ret, SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + AssertShardErrorEqual(ret, SHARD_ERROR(ErrorCode::DELETED_OBJECT)); } acc.Abort(); @@ -1371,7 +1371,7 @@ TEST_P(StorageV3, VertexLabelSerializationError) { { auto res = vertex->AddLabelAndValidate(label1); - AssertShardErrorEqual(res, SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR)); + AssertShardErrorEqual(res, SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR)); } } @@ -1865,7 +1865,7 @@ TEST_P(StorageV3, VertexPropertySerializationError) { { auto res = vertex->SetPropertyAndValidate(property2, PropertyValue("nandare")); - AssertShardErrorEqual(res, SHARD_ERROR(common::ErrorCode::SERIALIZATION_ERROR)); + AssertShardErrorEqual(res, SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR)); } } @@ -2255,14 +2255,14 @@ TEST_P(StorageV3, VertexNonexistentLabelPropertyEdgeAPI) { auto vertex = CreateVertexAndValidate(acc, {}, PropertyValue{0}, {}); // Check state before (OLD view). - ASSERT_EQ(vertex.Labels(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.HasLabel(label1, View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.Properties(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.GetProperty(property1, View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.InEdges(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.OutEdges(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.InDegree(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.OutDegree(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.Labels(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.HasLabel(label1, View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.Properties(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.GetProperty(property1, View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.InEdges(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.OutEdges(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.InDegree(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.OutDegree(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); // Check state before (NEW view). ASSERT_EQ(vertex.Labels(View::NEW)->size(), 0); @@ -2282,14 +2282,14 @@ TEST_P(StorageV3, VertexNonexistentLabelPropertyEdgeAPI) { .HasValue()); // Check state after (OLD view). - ASSERT_EQ(vertex.Labels(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.HasLabel(label1, View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.Properties(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.GetProperty(property1, View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.InEdges(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.OutEdges(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.InDegree(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); - ASSERT_EQ(vertex.OutDegree(View::OLD).GetError(), SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.Labels(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.HasLabel(label1, View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.Properties(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.GetProperty(property1, View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.InEdges(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.OutEdges(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.InDegree(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); + ASSERT_EQ(vertex.OutDegree(View::OLD).GetError(), SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); // Check state after (NEW view). ASSERT_EQ(vertex.Labels(View::NEW)->size(), 1); @@ -2663,25 +2663,25 @@ TEST_P(StorageV3, TestCreateVertexAndValidate) { auto acc = store.Access(GetNextHlc()); auto vertex = acc.CreateVertexAndValidate({primary_label}, {PropertyValue{0}}, {}); ASSERT_TRUE(vertex.HasError()); - EXPECT_EQ(vertex.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { auto acc = store.Access(GetNextHlc()); auto vertex = acc.CreateVertexAndValidate({primary_label}, {PropertyValue{0}}, {}); ASSERT_TRUE(vertex.HasError()); - EXPECT_EQ(vertex.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { auto acc = store.Access(GetNextHlc()); auto vertex = acc.CreateVertexAndValidate({}, {}, {}); ASSERT_TRUE(vertex.HasError()); - EXPECT_EQ(vertex.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); } { auto acc = store.Access(GetNextHlc()); auto vertex = acc.CreateVertexAndValidate({}, {PropertyValue{"test"}}, {}); ASSERT_TRUE(vertex.HasError()); - EXPECT_EQ(vertex.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } } } // namespace memgraph::storage::v3::tests diff --git a/tests/unit/storage_v3_edge.cpp b/tests/unit/storage_v3_edge.cpp index 47327ca3e..64f7f4e2e 100644 --- a/tests/unit/storage_v3_edge.cpp +++ b/tests/unit/storage_v3_edge.cpp @@ -3242,7 +3242,7 @@ TEST_P(StorageEdgeTest, VertexDetachDeleteSingleCommit) { { auto ret = acc.DeleteVertex(&vertex_from.value()); ASSERT_TRUE(ret.HasError()); - ASSERT_EQ(ret.GetError(), SHARD_ERROR(common::ErrorCode::VERTEX_HAS_EDGES)); + ASSERT_EQ(ret.GetError(), SHARD_ERROR(ErrorCode::VERTEX_HAS_EDGES)); } // Detach delete vertex @@ -3255,8 +3255,8 @@ TEST_P(StorageEdgeTest, VertexDetachDeleteSingleCommit) { // Check edges ASSERT_EQ(vertex_from->InEdges(View::OLD)->size(), 0); ASSERT_EQ(*vertex_from->InDegree(View::OLD), 0); - ASSERT_EQ(vertex_from->InEdges(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex_from->InDegree(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex_from->InEdges(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex_from->InDegree(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); { auto ret = vertex_from->OutEdges(View::OLD); ASSERT_TRUE(ret.HasValue()); @@ -3269,8 +3269,8 @@ TEST_P(StorageEdgeTest, VertexDetachDeleteSingleCommit) { ASSERT_EQ(e.FromVertex(), from_id); ASSERT_EQ(e.ToVertex(), to_id); } - ASSERT_EQ(vertex_from->OutEdges(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); - ASSERT_EQ(vertex_from->OutDegree(View::NEW).GetError(), SHARD_ERROR(common::ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex_from->OutEdges(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); + ASSERT_EQ(vertex_from->OutDegree(View::NEW).GetError(), SHARD_ERROR(ErrorCode::DELETED_OBJECT)); { auto ret = vertex_to->InEdges(View::OLD); ASSERT_TRUE(ret.HasValue()); diff --git a/tests/unit/storage_v3_schema.cpp b/tests/unit/storage_v3_schema.cpp index 4ce611c98..4bc920313 100644 --- a/tests/unit/storage_v3_schema.cpp +++ b/tests/unit/storage_v3_schema.cpp @@ -188,43 +188,43 @@ TEST_F(SchemaValidatorTest, TestSchemaValidateVertexCreate) { { const auto schema_violation = schema_validator.ValidateVertexCreate(NameToLabel("test"), {}, {PropertyValue(1)}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); } // Validate wrong secondary label { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {label1}, {PropertyValue("test")}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {label2}, {PropertyValue("test")}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } // Validate wrong property type { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {}, {PropertyValue(1)}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {PropertyValue("test"), PropertyValue(12), PropertyValue(1)}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } { const auto wrong_prop = PropertyValue(TemporalData(TemporalType::Date, 1234)); const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {PropertyValue("test"), PropertyValue(12), wrong_prop}); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } // Passing validations EXPECT_EQ(schema_validator.ValidateVertexCreate(label1, {}, {PropertyValue("test")}), std::nullopt); @@ -246,12 +246,12 @@ TEST_F(SchemaValidatorTest, TestSchemaValidatePropertyUpdate) { { const auto schema_violation = schema_validator.ValidatePropertyUpdate(label1, prop_string); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } { const auto schema_violation = schema_validator.ValidatePropertyUpdate(label2, prop_duration); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } EXPECT_EQ(schema_validator.ValidatePropertyUpdate(label1, prop_int), std::nullopt); EXPECT_EQ(schema_validator.ValidatePropertyUpdate(label1, prop_duration), std::nullopt); @@ -263,12 +263,12 @@ TEST_F(SchemaValidatorTest, TestSchemaValidatePropertyUpdateLabel) { { const auto schema_violation = schema_validator.ValidateLabelUpdate(label1); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } { const auto schema_violation = schema_validator.ValidateLabelUpdate(label2); ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); + EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } EXPECT_EQ(schema_validator.ValidateLabelUpdate(NameToLabel("test")), std::nullopt); } diff --git a/tests/unit/storage_v3_vertex_accessors.cpp b/tests/unit/storage_v3_vertex_accessors.cpp index dd515983a..9a155efb4 100644 --- a/tests/unit/storage_v3_vertex_accessors.cpp +++ b/tests/unit/storage_v3_vertex_accessors.cpp @@ -77,7 +77,7 @@ TEST_F(StorageV3Accessor, TestPrimaryLabel) { ASSERT_TRUE(vertex.PrimaryLabel(View::OLD).HasError()); const auto error_primary_label = vertex.PrimaryLabel(View::OLD).GetError(); ASSERT_FALSE(vertex.PrimaryLabel(View::NEW).HasError()); - EXPECT_EQ(error_primary_label, SHARD_ERROR(common::ErrorCode::NONEXISTENT_OBJECT)); + EXPECT_EQ(error_primary_label, SHARD_ERROR(ErrorCode::NONEXISTENT_OBJECT)); } { auto acc = storage.Access(GetNextHlc()); @@ -128,7 +128,7 @@ TEST_F(StorageV3Accessor, TestAddLabels) { const auto label1 = NameToLabelId("label"); auto vertex = acc.CreateVertexAndValidate({label1}, {PropertyValue{2}}, {}); ASSERT_TRUE(vertex.HasError()); - EXPECT_EQ(vertex.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); + EXPECT_EQ(vertex.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { auto acc = storage.Access(GetNextHlc()); @@ -137,7 +137,7 @@ TEST_F(StorageV3Accessor, TestAddLabels) { ASSERT_TRUE(vertex.HasValue()); const auto schema_violation = vertex->AddLabelAndValidate(label1); ASSERT_TRUE(schema_violation.HasError()); - EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } } @@ -181,7 +181,7 @@ TEST_F(StorageV3Accessor, TestRemoveLabels) { auto vertex = CreateVertexAndValidate(acc, {}, PropertyValue{2}); const auto res1 = vertex.RemoveLabelAndValidate(primary_label); ASSERT_TRUE(res1.HasError()); - EXPECT_EQ(res1.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); + EXPECT_EQ(res1.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } } @@ -200,14 +200,14 @@ TEST_F(StorageV3Accessor, TestSetKeysAndProperties) { auto vertex = CreateVertexAndValidate(acc, {}, PropertyValue{1}); const auto res = vertex.SetPropertyAndValidate(primary_property, PropertyValue(1)); ASSERT_TRUE(res.HasError()); - EXPECT_EQ(res.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); + EXPECT_EQ(res.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } { auto acc = storage.Access(GetNextHlc()); auto vertex = CreateVertexAndValidate(acc, {}, PropertyValue{2}); const auto res = vertex.SetPropertyAndValidate(primary_property, PropertyValue()); ASSERT_TRUE(res.HasError()); - EXPECT_EQ(res.GetError(), SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); + EXPECT_EQ(res.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } } From a6f3937692d5a13506abf5d7fa45ca85003ac2cc Mon Sep 17 00:00:00 2001 From: Jeremy B <97525434+42jeremy@users.noreply.github.com> Date: Tue, 22 Nov 2022 14:10:02 +0100 Subject: [PATCH 083/103] Update src/storage/v3/request_helper.hpp Co-authored-by: Jure Bajic --- src/storage/v3/request_helper.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 5591ac11a..3735d1e29 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -13,7 +13,7 @@ #include -#include "ast/ast.hpp" +#include "storage/v3/bindings/ast/ast.hpp" #include "query/v2/requests.hpp" #include "storage/v3/bindings/pretty_print_ast_to_original_expression.hpp" #include "storage/v3/bindings/typed_value.hpp" From bbbd722eebf87f841dff594babc43ec69bcef68d Mon Sep 17 00:00:00 2001 From: Jeremy B <97525434+42jeremy@users.noreply.github.com> Date: Tue, 22 Nov 2022 14:11:29 +0100 Subject: [PATCH 084/103] Update src/storage/v3/request_helper.hpp Co-authored-by: Jure Bajic --- src/storage/v3/request_helper.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 3735d1e29..2ac8e05ae 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -119,6 +119,7 @@ struct Element { template concept VerticesIt = utils::SameAsAnyOf>; + template std::vector> OrderByVertices(DbAccessor &dba, TIterable &iterable, std::vector &original_order_bys) { From c0cb53e156c649ddeba00718dc912a2f740a35f6 Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 22 Nov 2022 14:20:22 +0100 Subject: [PATCH 085/103] Replace if by switch --- src/storage/v3/request_helper.hpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 2ac8e05ae..2f469ae5b 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -13,8 +13,8 @@ #include -#include "storage/v3/bindings/ast/ast.hpp" #include "query/v2/requests.hpp" +#include "storage/v3/bindings/ast/ast.hpp" #include "storage/v3/bindings/pretty_print_ast_to_original_expression.hpp" #include "storage/v3/bindings/typed_value.hpp" #include "storage/v3/edge_accessor.hpp" @@ -133,11 +133,14 @@ std::vector> OrderByVertices(DbAccessor &dba, TIterable std::vector ordering; ordering.reserve(order_bys.size()); std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(ordering), [](const auto &order_by) { - if (memgraph::msgs::OrderingDirection::ASCENDING == order_by.direction) { - return Ordering::ASC; + switch (order_by.direction) { + case memgraph::msgs::OrderingDirection::ASCENDING: + return Ordering::ASC; + case memgraph::msgs::OrderingDirection::DESCENDING: + return Ordering::DESC; + default: + LOG_FATAL("Unknown ordering direction"); } - MG_ASSERT(memgraph::msgs::OrderingDirection::DESCENDING == order_by.direction); - return Ordering::DESC; }); std::vector> ordered; From 662fa2e6d2f4a6dd60ec695e035cd49674481c2c Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 22 Nov 2022 14:22:19 +0100 Subject: [PATCH 086/103] Remove uneeded using statement --- src/storage/v3/request_helper.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index ab863afdb..9c0a3f21a 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -27,7 +27,6 @@ using conversions::FromPropertyValueToValue; using conversions::ToMsgsVertexId; namespace { -namespace msgs = msgs; using AllEdgePropertyDataSructure = std::map; using SpecificEdgePropertyDataSructure = std::vector; From 307cce9e2174e7331a6c57f7f2f6920b7bdda30e Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 22 Nov 2022 14:23:24 +0100 Subject: [PATCH 087/103] Remove unused struct --- src/storage/v3/request_helper.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index 9c0a3f21a..24c1f431f 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -59,8 +59,6 @@ std::optional> PrimaryKeysFromAccessor(const VertexA return ret; } -struct LocalError {}; - std::optional> FillUpSourceVertexSecondaryLabels(const std::optional &v_acc, const msgs::ExpandOneRequest &req) { auto secondary_labels = v_acc->Labels(View::NEW); From f1e360469a57724788cc7b6c0b0cfd2dbc9e2ec2 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 22 Nov 2022 14:44:28 +0100 Subject: [PATCH 088/103] Expand SHARD_ERROR with fmt format --- src/storage/v3/name_id_mapper.hpp | 4 ---- src/storage/v3/result.hpp | 8 +++---- src/storage/v3/schema_validator.cpp | 33 ++++++++++++++--------------- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/storage/v3/name_id_mapper.hpp b/src/storage/v3/name_id_mapper.hpp index 70b5c5ded..d238f1072 100644 --- a/src/storage/v3/name_id_mapper.hpp +++ b/src/storage/v3/name_id_mapper.hpp @@ -48,10 +48,6 @@ class NameIdMapper final { return kUnmappedId; } - const std::string &IdToName(const LabelId label_id) const { return IdToName(label_id.AsInt()); } - - const std::string &IdToName(const PropertyId property_id) const { return IdToName(property_id.AsInt()); } - const std::string &IdToName(const uint64_t id) const { auto it = id_to_name_.find(id); MG_ASSERT(it != id_to_name_.end(), "Id not know in mapper!"); diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index 136ec0136..fcec17548 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -42,10 +42,10 @@ struct ShardError { }; // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) -#define SHARD_ERROR(...) \ - ({ \ - using ErrorCode = memgraph::common::ErrorCode; \ - memgraph::storage::v3::ShardError(__VA_ARGS__, std::experimental::source_location::current()); \ +#define SHARD_ERROR(error, ...) \ + ({ \ + using ErrorCode = memgraph::common::ErrorCode; \ + memgraph::storage::v3::ShardError(error, GET_MESSAGE(__VA_ARGS__), std::experimental::source_location::current()); \ }) template diff --git a/src/storage/v3/schema_validator.cpp b/src/storage/v3/schema_validator.cpp index 7854dc389..3350531aa 100644 --- a/src/storage/v3/schema_validator.cpp +++ b/src/storage/v3/schema_validator.cpp @@ -31,35 +31,34 @@ std::optional SchemaValidator::ValidateVertexCreate( // Schema on primary label const auto *schema = schemas_->GetSchema(primary_label); if (schema == nullptr) { - return SHARD_ERROR(ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL, - fmt::format("Schema not defined for label :{}", name_id_mapper_->IdToName(primary_label))); + return SHARD_ERROR(ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL, "Schema not defined for label :{}", + name_id_mapper_->IdToName(primary_label.AsInt())); } // Is there another primary label among secondary labels for (const auto &secondary_label : labels) { if (schemas_->GetSchema(secondary_label)) { return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY, - fmt::format("Cannot add label :{}, since it is defined as a primary label", - name_id_mapper_->IdToName(secondary_label))); + "Cannot add label :{}, since it is defined as a primary label", + name_id_mapper_->IdToName(secondary_label.AsInt())); } } // Quick size check if (schema->second.size() != primary_properties.size()) { return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED, - fmt::format("Not all primary properties have been specified for :{} vertex", - name_id_mapper_->IdToName(primary_label))); + "Not all primary properties have been specified for :{} vertex", + name_id_mapper_->IdToName(primary_label.AsInt())); } // Check only properties defined by schema for (size_t i{0}; i < schema->second.size(); ++i) { // Check schema property type if (auto property_schema_type = PropertyTypeToSchemaType(primary_properties[i]); property_schema_type && *property_schema_type != schema->second[i].type) { - return SHARD_ERROR( - common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE, - fmt::format("Property {} is of wrong type, expected {}, actual {}", - name_id_mapper_->IdToName(schema->second[i].property_id), - SchemaTypeToString(schema->second[i].type), SchemaTypeToString(*property_schema_type))); + return SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE, + "Property {} is of wrong type, expected {}, actual {}", + name_id_mapper_->IdToName(schema->second[i].property_id.AsInt()), + SchemaTypeToString(schema->second[i].type), SchemaTypeToString(*property_schema_type)); } } @@ -77,10 +76,10 @@ std::optional SchemaValidator::ValidatePropertyUpdate(const LabelId schema->second, [property_id](const auto &schema_property) { return property_id == schema_property.property_id; }); schema_property != schema->second.end()) { - return SHARD_ERROR( - common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY, - fmt::format("Cannot update primary property {} of schema on label :{}", - name_id_mapper_->IdToName(schema_property->property_id), name_id_mapper_->IdToName(primary_label))); + return SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY, + "Cannot update primary property {} of schema on label :{}", + name_id_mapper_->IdToName(schema_property->property_id.AsInt()), + name_id_mapper_->IdToName(primary_label.AsInt())); } return std::nullopt; } @@ -88,8 +87,8 @@ std::optional SchemaValidator::ValidatePropertyUpdate(const LabelId std::optional SchemaValidator::ValidateLabelUpdate(const LabelId label) const { const auto *schema = schemas_->GetSchema(label); if (schema) { - return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL, - fmt::format("Cannot add/remove primary label :{}", name_id_mapper_->IdToName(label))); + return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL, "Cannot add/remove primary label :{}", + name_id_mapper_->IdToName(label.AsInt())); } return std::nullopt; } From 6801d6ff09979c092b7aca1281b76ba80ac26137 Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 22 Nov 2022 14:49:54 +0100 Subject: [PATCH 089/103] Remove duplicate using statement --- src/storage/v3/request_helper.cpp | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index 24c1f431f..73e6f86e5 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -173,17 +173,6 @@ std::optional, 2>> FillUpConnectingEdges( return std::array, 2>{std::move(in_edges), std::move(out_edges)}; } -using AllEdgePropertyDataSructure = std::map; -using SpecificEdgePropertyDataSructure = std::vector; - -using AllEdgeProperties = std::tuple; -using SpecificEdgeProperties = std::tuple; - -using SpecificEdgePropertiesVector = std::vector; -using AllEdgePropertiesVector = std::vector; - -using EdgeFiller = std::function; - template bool FillEdges(const std::vector &edges, msgs::ExpandOneResultRow &row, const EdgeFiller &edge_filler) { for (const auto &edge : edges) { From d82cfb349ea927ba9f3f6b50a55a7ee0e1f21e8e Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 22 Nov 2022 15:00:32 +0100 Subject: [PATCH 090/103] Use ShardResult --- src/storage/v3/schema_validator.cpp | 23 ++++----- src/storage/v3/schema_validator.hpp | 16 +++--- src/storage/v3/shard.cpp | 4 +- src/storage/v3/shard_rsm.cpp | 4 +- src/storage/v3/vertex_accessor.cpp | 14 ++--- tests/unit/storage_v3_schema.cpp | 80 +++++++++++++++-------------- 6 files changed, 71 insertions(+), 70 deletions(-) diff --git a/src/storage/v3/schema_validator.cpp b/src/storage/v3/schema_validator.cpp index 3350531aa..ea2522684 100644 --- a/src/storage/v3/schema_validator.cpp +++ b/src/storage/v3/schema_validator.cpp @@ -25,9 +25,8 @@ namespace memgraph::storage::v3 { SchemaValidator::SchemaValidator(Schemas &schemas, const NameIdMapper &name_id_mapper) : schemas_{&schemas}, name_id_mapper_{&name_id_mapper} {} -std::optional SchemaValidator::ValidateVertexCreate( - LabelId primary_label, const std::vector &labels, - const std::vector &primary_properties) const { +ShardResult SchemaValidator::ValidateVertexCreate(LabelId primary_label, const std::vector &labels, + const std::vector &primary_properties) const { // Schema on primary label const auto *schema = schemas_->GetSchema(primary_label); if (schema == nullptr) { @@ -62,11 +61,11 @@ std::optional SchemaValidator::ValidateVertexCreate( } } - return std::nullopt; + return {}; } -std::optional SchemaValidator::ValidatePropertyUpdate(const LabelId primary_label, - const PropertyId property_id) const { +ShardResult SchemaValidator::ValidatePropertyUpdate(const LabelId primary_label, + const PropertyId property_id) const { // Verify existence of schema on primary label const auto *schema = schemas_->GetSchema(primary_label); MG_ASSERT(schema, "Cannot validate against non existing schema!"); @@ -81,16 +80,16 @@ std::optional SchemaValidator::ValidatePropertyUpdate(const LabelId name_id_mapper_->IdToName(schema_property->property_id.AsInt()), name_id_mapper_->IdToName(primary_label.AsInt())); } - return std::nullopt; + return {}; } -std::optional SchemaValidator::ValidateLabelUpdate(const LabelId label) const { +ShardResult SchemaValidator::ValidateLabelUpdate(const LabelId label) const { const auto *schema = schemas_->GetSchema(label); if (schema) { return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL, "Cannot add/remove primary label :{}", name_id_mapper_->IdToName(label.AsInt())); } - return std::nullopt; + return {}; } const Schemas::Schema *SchemaValidator::GetSchema(LabelId label) const { return schemas_->GetSchema(label); } @@ -98,15 +97,15 @@ const Schemas::Schema *SchemaValidator::GetSchema(LabelId label) const { return VertexValidator::VertexValidator(const SchemaValidator &schema_validator, const LabelId primary_label) : schema_validator{&schema_validator}, primary_label_{primary_label} {} -std::optional VertexValidator::ValidatePropertyUpdate(PropertyId property_id) const { +ShardResult VertexValidator::ValidatePropertyUpdate(PropertyId property_id) const { return schema_validator->ValidatePropertyUpdate(primary_label_, property_id); }; -std::optional VertexValidator::ValidateAddLabel(LabelId label) const { +ShardResult VertexValidator::ValidateAddLabel(LabelId label) const { return schema_validator->ValidateLabelUpdate(label); } -std::optional VertexValidator::ValidateRemoveLabel(LabelId label) const { +ShardResult VertexValidator::ValidateRemoveLabel(LabelId label) const { return schema_validator->ValidateLabelUpdate(label); } diff --git a/src/storage/v3/schema_validator.hpp b/src/storage/v3/schema_validator.hpp index 329d4a582..637ca5f7e 100644 --- a/src/storage/v3/schema_validator.hpp +++ b/src/storage/v3/schema_validator.hpp @@ -11,7 +11,6 @@ #pragma once -#include #include #include "storage/v2/result.hpp" @@ -27,13 +26,12 @@ class SchemaValidator { public: explicit SchemaValidator(Schemas &schemas, const NameIdMapper &name_id_mapper); - [[nodiscard]] std::optional ValidateVertexCreate( - LabelId primary_label, const std::vector &labels, - const std::vector &primary_properties) const; + [[nodiscard]] ShardResult ValidateVertexCreate(LabelId primary_label, const std::vector &labels, + const std::vector &primary_properties) const; - [[nodiscard]] std::optional ValidatePropertyUpdate(LabelId primary_label, PropertyId property_id) const; + [[nodiscard]] ShardResult ValidatePropertyUpdate(LabelId primary_label, PropertyId property_id) const; - [[nodiscard]] std::optional ValidateLabelUpdate(LabelId label) const; + [[nodiscard]] ShardResult ValidateLabelUpdate(LabelId label) const; const Schemas::Schema *GetSchema(LabelId label) const; @@ -45,11 +43,11 @@ class SchemaValidator { struct VertexValidator { explicit VertexValidator(const SchemaValidator &schema_validator, LabelId primary_label); - [[nodiscard]] std::optional ValidatePropertyUpdate(PropertyId property_id) const; + [[nodiscard]] ShardResult ValidatePropertyUpdate(PropertyId property_id) const; - [[nodiscard]] std::optional ValidateAddLabel(LabelId label) const; + [[nodiscard]] ShardResult ValidateAddLabel(LabelId label) const; - [[nodiscard]] std::optional ValidateRemoveLabel(LabelId label) const; + [[nodiscard]] ShardResult ValidateRemoveLabel(LabelId label) const; const SchemaValidator *schema_validator; diff --git a/src/storage/v3/shard.cpp b/src/storage/v3/shard.cpp index 4f25916c6..dbf1f043f 100644 --- a/src/storage/v3/shard.cpp +++ b/src/storage/v3/shard.cpp @@ -353,8 +353,8 @@ ShardResult Shard::Accessor::CreateVertexAndValidate( auto maybe_schema_violation = GetSchemaValidator().ValidateVertexCreate(shard_->primary_label_, labels, primary_properties); - if (maybe_schema_violation) { - return {std::move(*maybe_schema_violation)}; + if (maybe_schema_violation.HasError()) { + return {std::move(maybe_schema_violation.GetError())}; } auto acc = shard_->vertices_.access(); diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 7a128a78f..aac045fe4 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -330,11 +330,11 @@ ShardResult GetExpandOneResult( auto v_acc = acc.FindVertex(primary_key, View::NEW); msgs::Vertex source_vertex = {.id = src_vertex}; - const auto maybe_secondary_labels = FillUpSourceVertexSecondaryLabels(v_acc, req); + auto maybe_secondary_labels = FillUpSourceVertexSecondaryLabels(v_acc, req); if (maybe_secondary_labels.HasError()) { return maybe_secondary_labels.GetError(); } - source_vertex.labels = *maybe_secondary_labels; + source_vertex.labels = std::move(*maybe_secondary_labels); auto src_vertex_properties = FillUpSourceVertexProperties(v_acc, req, storage::v3::View::NEW, schema); diff --git a/src/storage/v3/vertex_accessor.cpp b/src/storage/v3/vertex_accessor.cpp index 6949c08cc..1b0ba6340 100644 --- a/src/storage/v3/vertex_accessor.cpp +++ b/src/storage/v3/vertex_accessor.cpp @@ -99,8 +99,8 @@ ShardResult VertexAccessor::AddLabel(LabelId label) { } ShardResult VertexAccessor::AddLabelAndValidate(LabelId label) { - if (const auto maybe_violation_error = vertex_validator_->ValidateAddLabel(label); maybe_violation_error) { - return {*maybe_violation_error}; + if (const auto maybe_violation_error = vertex_validator_->ValidateAddLabel(label); maybe_violation_error.HasError()) { + return {maybe_violation_error.GetError()}; } utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; @@ -135,8 +135,9 @@ ShardResult VertexAccessor::RemoveLabel(LabelId label) { } ShardResult VertexAccessor::RemoveLabelAndValidate(LabelId label) { - if (const auto maybe_violation_error = vertex_validator_->ValidateRemoveLabel(label); maybe_violation_error) { - return {*maybe_violation_error}; + if (const auto maybe_violation_error = vertex_validator_->ValidateRemoveLabel(label); + maybe_violation_error.HasError()) { + return {maybe_violation_error.GetError()}; } if (!PrepareForWrite(transaction_, vertex_)) return SHARD_ERROR(ErrorCode::SERIALIZATION_ERROR); @@ -332,8 +333,9 @@ ShardResult VertexAccessor::CheckVertexExistence(View view) const { } ShardResult VertexAccessor::SetPropertyAndValidate(PropertyId property, const PropertyValue &value) { - if (auto maybe_violation_error = vertex_validator_->ValidatePropertyUpdate(property); maybe_violation_error) { - return {*maybe_violation_error}; + if (auto maybe_violation_error = vertex_validator_->ValidatePropertyUpdate(property); + maybe_violation_error.HasError()) { + return {maybe_violation_error.GetError()}; } utils::MemoryTracker::OutOfMemoryExceptionEnabler oom_exception; diff --git a/tests/unit/storage_v3_schema.cpp b/tests/unit/storage_v3_schema.cpp index 4bc920313..2c5515958 100644 --- a/tests/unit/storage_v3_schema.cpp +++ b/tests/unit/storage_v3_schema.cpp @@ -14,7 +14,6 @@ #include #include -#include #include #include @@ -187,89 +186,92 @@ TEST_F(SchemaValidatorTest, TestSchemaValidateVertexCreate) { // Validate against secondary label { const auto schema_violation = schema_validator.ValidateVertexCreate(NameToLabel("test"), {}, {PropertyValue(1)}); - ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL)); + ASSERT_FALSE(schema_violation.HasError()); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {}); - ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); + ASSERT_FALSE(schema_violation.HasError()); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); } // Validate wrong secondary label { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {label1}, {PropertyValue("test")}); - ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); + ASSERT_FALSE(schema_violation.HasError()); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {label2}, {PropertyValue("test")}); - ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); + ASSERT_FALSE(schema_violation.HasError()); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } // Validate wrong property type { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {}, {PropertyValue(1)}); - ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); + ASSERT_FALSE(schema_violation.HasError()); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {PropertyValue("test"), PropertyValue(12), PropertyValue(1)}); - ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); + ASSERT_FALSE(schema_violation.HasError()); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } { const auto wrong_prop = PropertyValue(TemporalData(TemporalType::Date, 1234)); const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {PropertyValue("test"), PropertyValue(12), wrong_prop}); - ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); + ASSERT_FALSE(schema_violation.HasError()); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } // Passing validations - EXPECT_EQ(schema_validator.ValidateVertexCreate(label1, {}, {PropertyValue("test")}), std::nullopt); - EXPECT_EQ(schema_validator.ValidateVertexCreate(label1, {NameToLabel("label3"), NameToLabel("label4")}, - {PropertyValue("test")}), - std::nullopt); - EXPECT_EQ(schema_validator.ValidateVertexCreate( - label2, {}, - {PropertyValue("test"), PropertyValue(122), PropertyValue(TemporalData(TemporalType::Duration, 1234))}), - std::nullopt); - EXPECT_EQ(schema_validator.ValidateVertexCreate(label2, {NameToLabel("label5"), NameToLabel("label6")}, - {PropertyValue("test123"), PropertyValue(122221), - PropertyValue(TemporalData(TemporalType::Duration, 12344321))}), - std::nullopt); + EXPECT_FALSE(schema_validator.ValidateVertexCreate(label1, {}, {PropertyValue("test")}).HasError()); + EXPECT_FALSE( + schema_validator + .ValidateVertexCreate(label1, {NameToLabel("label3"), NameToLabel("label4")}, {PropertyValue("test")}) + .HasError()); + EXPECT_FALSE(schema_validator + .ValidateVertexCreate(label2, {}, + {PropertyValue("test"), PropertyValue(122), + PropertyValue(TemporalData(TemporalType::Duration, 1234))}) + .HasError()); + EXPECT_FALSE(schema_validator + .ValidateVertexCreate(label2, {NameToLabel("label5"), NameToLabel("label6")}, + {PropertyValue("test123"), PropertyValue(122221), + PropertyValue(TemporalData(TemporalType::Duration, 12344321))}) + .HasError()); } TEST_F(SchemaValidatorTest, TestSchemaValidatePropertyUpdate) { // Validate updating of primary key { const auto schema_violation = schema_validator.ValidatePropertyUpdate(label1, prop_string); - ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); + ASSERT_FALSE(schema_violation.HasError()); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } { const auto schema_violation = schema_validator.ValidatePropertyUpdate(label2, prop_duration); - ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); + ASSERT_FALSE(schema_violation.HasError()); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } - EXPECT_EQ(schema_validator.ValidatePropertyUpdate(label1, prop_int), std::nullopt); - EXPECT_EQ(schema_validator.ValidatePropertyUpdate(label1, prop_duration), std::nullopt); - EXPECT_EQ(schema_validator.ValidatePropertyUpdate(label2, NameToProperty("test")), std::nullopt); + EXPECT_FALSE(schema_validator.ValidatePropertyUpdate(label1, prop_int).HasError()); + EXPECT_FALSE(schema_validator.ValidatePropertyUpdate(label1, prop_duration).HasError()); + EXPECT_FALSE(schema_validator.ValidatePropertyUpdate(label2, NameToProperty("test")).HasError()); } TEST_F(SchemaValidatorTest, TestSchemaValidatePropertyUpdateLabel) { // Validate adding primary label { const auto schema_violation = schema_validator.ValidateLabelUpdate(label1); - ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); + ASSERT_FALSE(schema_violation.HasError()); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } { const auto schema_violation = schema_validator.ValidateLabelUpdate(label2); - ASSERT_NE(schema_violation, std::nullopt); - EXPECT_EQ(*schema_violation, SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); + ASSERT_FALSE(schema_violation.HasError()); + EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } - EXPECT_EQ(schema_validator.ValidateLabelUpdate(NameToLabel("test")), std::nullopt); + EXPECT_FALSE(schema_validator.ValidateLabelUpdate(NameToLabel("test")).HasError()); } } // namespace memgraph::storage::v3::tests From cd7b33f23fb3e6b7c566af11cb328634177e0331 Mon Sep 17 00:00:00 2001 From: jbajic Date: Tue, 22 Nov 2022 16:22:42 +0100 Subject: [PATCH 091/103] use comparison between ErrorCode and ShardError --- src/storage/v3/shard.cpp | 4 ++-- src/storage/v3/shard_rsm.cpp | 16 ++++------------ tests/unit/storage_v3.cpp | 2 +- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/storage/v3/shard.cpp b/src/storage/v3/shard.cpp index dbf1f043f..79093449c 100644 --- a/src/storage/v3/shard.cpp +++ b/src/storage/v3/shard.cpp @@ -444,7 +444,7 @@ ShardResult>>> EdgeAccessor e(edge, edge_type, from_vertex, vertex_id, transaction_, &shard_->indices_, config_); auto ret = DeleteEdge(e.FromVertex(), e.ToVertex(), e.Gid()); if (ret.HasError()) { - MG_ASSERT(ret.GetError().code == common::ErrorCode::SERIALIZATION_ERROR, "Invalid database state!"); + MG_ASSERT(ret.GetError() == common::ErrorCode::SERIALIZATION_ERROR, "Invalid database state!"); return ret.GetError(); } @@ -457,7 +457,7 @@ ShardResult>>> EdgeAccessor e(edge, edge_type, vertex_id, to_vertex, transaction_, &shard_->indices_, config_); auto ret = DeleteEdge(e.FromVertex(), e.ToVertex(), e.Gid()); if (ret.HasError()) { - MG_ASSERT(ret.GetError().code == common::ErrorCode::SERIALIZATION_ERROR, "Invalid database state!"); + MG_ASSERT(ret.GetError() == common::ErrorCode::SERIALIZATION_ERROR, "Invalid database state!"); return ret.GetError(); } diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index aac045fe4..62f46b944 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -355,16 +355,11 @@ ShardResult GetExpandOneResult( result_row.src_vertex_properties = std::move(*src_vertex_properties); static constexpr bool kInEdges = true; static constexpr bool kOutEdges = false; - if (!in_edges.empty()) { - if (const auto fill_edges_res = FillEdges(in_edges, result_row, edge_filler); fill_edges_res.HasError()) { - return fill_edges_res.GetError(); - } + if (const auto fill_edges_res = FillEdges(in_edges, result_row, edge_filler); fill_edges_res.HasError()) { + return fill_edges_res.GetError(); } - if (!out_edges.empty()) { - if (const auto fill_edges_res = FillEdges(out_edges, result_row, edge_filler); - fill_edges_res.HasError()) { - return fill_edges_res.GetError(); - } + if (const auto fill_edges_res = FillEdges(out_edges, result_row, edge_filler); fill_edges_res.HasError()) { + return fill_edges_res.GetError(); } return result_row; @@ -447,7 +442,6 @@ EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { return {}; }; } else { - // TODO(gvolfing) - do we want to set the action_successful here? edge_filler = [&req](const EdgeAccessor &edge, const bool is_in_edge, msgs::ExpandOneResultRow &result_row) -> ShardResult { std::vector value_properties; @@ -555,8 +549,6 @@ msgs::WriteResponses ShardRsm::ApplyWrite(msgs::DeleteVerticesRequest &&req) { auto vertex_acc = acc.FindVertex(ConvertPropertyVector(std::move(propval)), View::OLD); if (!vertex_acc) { - spdlog::debug("Error while trying to delete vertex. Vertex to delete does not exist. Transaction id: {}", - req.transaction_id.logical_id); shard_error.emplace(msgs::ShardError{common::ErrorCode::OBJECT_NOT_FOUND}); spdlog::debug("In transaction {} vertex could not be found while trying to delete it.", req.transaction_id.logical_id); diff --git a/tests/unit/storage_v3.cpp b/tests/unit/storage_v3.cpp index cab463b7f..6f6902d1c 100644 --- a/tests/unit/storage_v3.cpp +++ b/tests/unit/storage_v3.cpp @@ -2657,7 +2657,7 @@ TEST_P(StorageV3, TestCreateVertexAndValidate) { ASSERT_TRUE(vertex2.HasError()); auto error = vertex2.GetError(); - ASSERT_TRUE(error.code == common::ErrorCode::VERTEX_ALREADY_INSERTED); + ASSERT_TRUE(error == common::ErrorCode::VERTEX_ALREADY_INSERTED); } { auto acc = store.Access(GetNextHlc()); From 3a171376d717f1fa9664bbdb6e6c7b2e5fabe643 Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 22 Nov 2022 16:47:25 +0100 Subject: [PATCH 092/103] OrderBy in Expand has two members to differ vertices Vs edges --- src/query/v2/requests.hpp | 4 ++- src/storage/v3/request_helper.cpp | 10 +++---- src/storage/v3/request_helper.hpp | 36 +++++++++++-------------- src/storage/v3/shard_rsm.cpp | 10 +++---- tests/simulation/shard_rsm.cpp | 45 ++++++++++++++++++++----------- 5 files changed, 58 insertions(+), 47 deletions(-) diff --git a/src/query/v2/requests.hpp b/src/query/v2/requests.hpp index 971977246..888265bb4 100644 --- a/src/query/v2/requests.hpp +++ b/src/query/v2/requests.hpp @@ -403,7 +403,9 @@ struct ExpandOneRequest { std::vector vertex_expressions; std::vector edge_expressions; - std::vector order_by; + std::vector order_by_vertices; + std::vector order_by_edges; + // Limit the edges or the vertices? std::optional limit; std::vector filters; diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index 73e6f86e5..cca506088 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -495,11 +495,11 @@ std::array, 2> GetEdgesFromVertex(const VertexAccessor } std::vector> OrderByEdges(DbAccessor &dba, std::vector &iterable, - std::vector &order_bys, + std::vector &order_by_edges, const VertexAccessor &vertex_acc) { std::vector ordering; - ordering.reserve(order_bys.size()); - std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(ordering), [](const auto &order_by) { + ordering.reserve(order_by_edges.size()); + std::transform(order_by_edges.begin(), order_by_edges.end(), std::back_inserter(ordering), [](const auto &order_by) { if (memgraph::msgs::OrderingDirection::ASCENDING == order_by.direction) { return Ordering::ASC; } @@ -510,8 +510,8 @@ std::vector> OrderByEdges(DbAccessor &dba, std::vector> ordered; for (auto it = iterable.begin(); it != iterable.end(); ++it) { std::vector properties_order_by; - properties_order_by.reserve(order_bys.size()); - std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(properties_order_by), + properties_order_by.reserve(order_by_edges.size()); + std::transform(order_by_edges.begin(), order_by_edges.end(), std::back_inserter(properties_order_by), [&dba, &vertex_acc, &it](const auto &order_by) { return ComputeExpression(dba, vertex_acc, *it, order_by.expression.expression, expr::identifier_node_symbol, expr::identifier_edge_symbol); diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index 2f469ae5b..b43b2d24a 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -122,33 +122,27 @@ concept VerticesIt = utils::SameAsAnyOf std::vector> OrderByVertices(DbAccessor &dba, TIterable &iterable, - std::vector &original_order_bys) { - auto order_bys = original_order_bys; - auto it_to_remove = std::remove_if(order_bys.begin(), order_bys.end(), [](const auto &order_by) { - // We only want to keep OrderBys not impliying edges-ordering - return std::string::npos != order_by.expression.expression.find(expr::identifier_edge_symbol); - }); - order_bys.erase(it_to_remove, order_bys.end()); - + std::vector &order_by_vertices) { std::vector ordering; - ordering.reserve(order_bys.size()); - std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(ordering), [](const auto &order_by) { - switch (order_by.direction) { - case memgraph::msgs::OrderingDirection::ASCENDING: - return Ordering::ASC; - case memgraph::msgs::OrderingDirection::DESCENDING: - return Ordering::DESC; - default: - LOG_FATAL("Unknown ordering direction"); - } - }); + ordering.reserve(order_by_vertices.size()); + std::transform(order_by_vertices.begin(), order_by_vertices.end(), std::back_inserter(ordering), + [](const auto &order_by) { + switch (order_by.direction) { + case memgraph::msgs::OrderingDirection::ASCENDING: + return Ordering::ASC; + case memgraph::msgs::OrderingDirection::DESCENDING: + return Ordering::DESC; + default: + LOG_FATAL("Unknown ordering direction"); + } + }); std::vector> ordered; for (auto it = iterable.begin(); it != iterable.end(); ++it) { std::vector properties_order_by; - properties_order_by.reserve(order_bys.size()); + properties_order_by.reserve(order_by_vertices.size()); - std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(properties_order_by), + std::transform(order_by_vertices.begin(), order_by_vertices.end(), std::back_inserter(properties_order_by), [&dba, &it](const auto &order_by) { return ComputeExpression(dba, *it, std::nullopt /*e_acc*/, order_by.expression.expression, expr::identifier_node_symbol, expr::identifier_edge_symbol); diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 97e5af703..2d536fe42 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -506,10 +506,10 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { vertex_accessors.emplace_back(src_vertex_acc_opt.value()); } - if (!req.order_by.empty()) { + if (!req.order_by_vertices.empty()) { // Can we do differently to avoid this? We need OrderByElements but currently it returns vector, so this // workaround is here to avoid more duplication later - auto local_sorted_vertices = OrderByVertices(dba, vertex_accessors, req.order_by); + auto local_sorted_vertices = OrderByVertices(dba, vertex_accessors, req.order_by_vertices); vertex_accessors.clear(); std::transform(local_sorted_vertices.begin(), local_sorted_vertices.end(), std::back_inserter(vertex_accessors), [](auto &vertex) { return vertex.object_acc; }); @@ -532,7 +532,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { std::optional maybe_result; - if (req.order_by.empty()) { + if (req.order_by_vertices.empty()) { const auto *schema = shard_->GetSchema(shard_->PrimaryLabel()); MG_ASSERT(schema); maybe_result = @@ -540,8 +540,8 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { } else { auto [in_edge_accessors, out_edge_accessors] = GetEdgesFromVertex(src_vertex_acc, req.direction); - const auto in_ordered_edges = OrderByEdges(dba, in_edge_accessors, req.order_by, src_vertex_acc); - const auto out_ordered_edges = OrderByEdges(dba, out_edge_accessors, req.order_by, src_vertex_acc); + const auto in_ordered_edges = OrderByEdges(dba, in_edge_accessors, req.order_by_edges, src_vertex_acc); + const auto out_ordered_edges = OrderByEdges(dba, out_edge_accessors, req.order_by_edges, src_vertex_acc); std::vector in_edge_ordered_accessors; std::transform(in_ordered_edges.begin(), in_ordered_edges.end(), std::back_inserter(in_edge_ordered_accessors), diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index c1ef1079d..c668a43a2 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -531,7 +531,8 @@ void AttemptToExpandOneWithWrongEdgeType(ShardClient &client, uint64_t src_verte std::optional> edge_properties = {}; std::vector expressions; - std::vector order_by = {}; + std::vector order_by_vertices = {}; + std::vector order_by_edges = {}; std::optional limit = {}; std::vector filter = {}; @@ -543,7 +544,8 @@ void AttemptToExpandOneWithWrongEdgeType(ShardClient &client, uint64_t src_verte expand_one_req.vertex_expressions = expressions; expand_one_req.filters = filter; expand_one_req.limit = limit; - expand_one_req.order_by = order_by; + expand_one_req.order_by_vertices = order_by_vertices; + expand_one_req.order_by_edges = order_by_edges; expand_one_req.src_vertex_properties = src_vertex_properties; expand_one_req.src_vertices = {src_vertex}; expand_one_req.transaction_id.logical_id = GetTransactionId(); @@ -586,7 +588,8 @@ void AttemptToExpandOneSimple(ShardClient &client, uint64_t src_vertex_val, Edge std::optional> edge_properties = {}; std::vector expressions; - std::vector order_by = {}; + std::vector order_by_vertices = {}; + std::vector order_by_edges = {}; std::optional limit = {}; std::vector filter = {}; @@ -598,7 +601,8 @@ void AttemptToExpandOneSimple(ShardClient &client, uint64_t src_vertex_val, Edge expand_one_req.vertex_expressions = expressions; expand_one_req.filters = filter; expand_one_req.limit = limit; - expand_one_req.order_by = order_by; + expand_one_req.order_by_vertices = order_by_vertices; + expand_one_req.order_by_edges = order_by_edges; expand_one_req.src_vertex_properties = src_vertex_properties; expand_one_req.src_vertices = {src_vertex}; expand_one_req.transaction_id.logical_id = GetTransactionId(); @@ -642,7 +646,8 @@ void AttemptToExpandOneWithUniqueEdges(ShardClient &client, uint64_t src_vertex_ std::optional> edge_properties = {}; std::vector expressions; - std::vector order_by = {}; + std::vector order_by_vertices = {}; + std::vector order_by_edges = {}; std::optional limit = {}; std::vector filter = {}; @@ -654,7 +659,8 @@ void AttemptToExpandOneWithUniqueEdges(ShardClient &client, uint64_t src_vertex_ expand_one_req.vertex_expressions = expressions; expand_one_req.filters = filter; expand_one_req.limit = limit; - expand_one_req.order_by = order_by; + expand_one_req.order_by_vertices = order_by_vertices; + expand_one_req.order_by_edges = order_by_edges; expand_one_req.src_vertex_properties = src_vertex_properties; expand_one_req.src_vertices = {src_vertex}; expand_one_req.only_unique_neighbor_rows = true; @@ -700,9 +706,11 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ // Edge properties to look for std::optional> edge_properties = {}; - std::vector order_by = { - {msgs::Expression{"MG_SYMBOL_NODE.prop1"}, msgs::OrderingDirection::ASCENDING}, + std::vector order_by_vertices = { + {msgs::Expression{"MG_SYMBOL_NODE.prop1"}, msgs::OrderingDirection::ASCENDING}}; + std::vector order_by_edges = { {msgs::Expression{"MG_SYMBOL_EDGE.prop4"}, msgs::OrderingDirection::DESCENDING}}; + size_t limit = 1; std::vector filters = {"MG_SYMBOL_NODE.prop1 != -1"}; @@ -713,7 +721,8 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_ expand_one_req.edge_types = {edge_type}; expand_one_req.filters = filters; expand_one_req.limit = limit; - expand_one_req.order_by = order_by; + expand_one_req.order_by_vertices = order_by_vertices; + expand_one_req.order_by_edges = order_by_edges; expand_one_req.src_vertex_properties = src_vertex_properties; expand_one_req.src_vertices = {src_vertex, other_src_vertex}; expand_one_req.transaction_id.logical_id = GetTransactionId(); @@ -780,7 +789,8 @@ void AttemptToExpandOneWithSpecifiedSrcVertexProperties(ShardClient &client, uin std::optional> edge_properties = {}; std::vector expressions; - std::vector order_by = {}; + std::vector order_by_vertices = {}; + std::vector order_by_edges = {}; std::optional limit = {}; std::vector filter = {}; @@ -792,7 +802,8 @@ void AttemptToExpandOneWithSpecifiedSrcVertexProperties(ShardClient &client, uin expand_one_req.vertex_expressions = expressions; expand_one_req.filters = filter; expand_one_req.limit = limit; - expand_one_req.order_by = order_by; + expand_one_req.order_by_vertices = order_by_vertices; + expand_one_req.order_by_edges = order_by_edges; expand_one_req.src_vertex_properties = src_vertex_properties; expand_one_req.src_vertices = {src_vertex}; expand_one_req.transaction_id.logical_id = GetTransactionId(); @@ -840,7 +851,8 @@ void AttemptToExpandOneWithSpecifiedEdgeProperties(ShardClient &client, uint64_t std::optional> edge_properties = {specified_edge_prop}; std::vector expressions; - std::vector order_by = {}; + std::vector order_by_vertices = {}; + std::vector order_by_edges = {}; std::optional limit = {}; std::vector filter = {}; @@ -852,7 +864,8 @@ void AttemptToExpandOneWithSpecifiedEdgeProperties(ShardClient &client, uint64_t expand_one_req.vertex_expressions = expressions; expand_one_req.filters = filter; expand_one_req.limit = limit; - expand_one_req.order_by = order_by; + expand_one_req.order_by_vertices = order_by_vertices; + expand_one_req.order_by_edges = order_by_edges; expand_one_req.src_vertex_properties = src_vertex_properties; expand_one_req.src_vertices = {src_vertex}; expand_one_req.transaction_id.logical_id = GetTransactionId(); @@ -899,7 +912,8 @@ void AttemptToExpandOneWithFilters(ShardClient &client, uint64_t src_vertex_val, std::optional> edge_properties = {}; std::vector expressions; - std::vector order_by = {}; + std::vector order_by_vertices = {}; + std::vector order_by_edges = {}; std::optional limit = {}; std::vector filter = {}; @@ -911,7 +925,8 @@ void AttemptToExpandOneWithFilters(ShardClient &client, uint64_t src_vertex_val, expand_one_req.vertex_expressions = expressions; expand_one_req.filters = {filter_expr1}; expand_one_req.limit = limit; - expand_one_req.order_by = order_by; + expand_one_req.order_by_vertices = order_by_vertices; + expand_one_req.order_by_edges = order_by_edges; expand_one_req.src_vertex_properties = src_vertex_properties; expand_one_req.src_vertices = {src_vertex}; expand_one_req.transaction_id.logical_id = GetTransactionId(); From 85034ddcbeed4300de620c9ad17c2c25572d3e4c Mon Sep 17 00:00:00 2001 From: jeremy Date: Tue, 22 Nov 2022 17:29:02 +0100 Subject: [PATCH 093/103] Rename variable un function def --- src/storage/v3/request_helper.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index b43b2d24a..ebf4f1d0e 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -159,7 +159,7 @@ std::vector> OrderByVertices(DbAccessor &dba, TIterable } std::vector> OrderByEdges(DbAccessor &dba, std::vector &iterable, - std::vector &order_bys, + std::vector &order_by_edges, const VertexAccessor &vertex_acc); VerticesIterable::Iterator GetStartVertexIterator(VerticesIterable &vertex_iterable, From 9fade5ebac55a8e345b3db135da94eb541f61b6a Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 23 Nov 2022 10:19:27 +0100 Subject: [PATCH 094/103] Fix tests --- src/storage/v3/shard_rsm.cpp | 2 +- tests/simulation/shard_rsm.cpp | 2 +- tests/unit/machine_manager.cpp | 1 + tests/unit/storage_v3_schema.cpp | 22 +++++++++++----------- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 62f46b944..a027140e2 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -805,7 +805,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ScanVerticesRequest &&req) { } msgs::ScanVerticesResponse resp{.error = std::move(shard_error)}; - if (resp.error) { + if (!resp.error) { resp.next_start_id = next_start_id; resp.results = std::move(results); } diff --git a/tests/simulation/shard_rsm.cpp b/tests/simulation/shard_rsm.cpp index b9511f024..b197f114d 100644 --- a/tests/simulation/shard_rsm.cpp +++ b/tests/simulation/shard_rsm.cpp @@ -156,7 +156,7 @@ bool AttemptToCreateVertex(ShardClient &client, int64_t value) { create_req.transaction_id.logical_id = GetTransactionId(); auto write_res = client.SendWriteRequest(create_req); - MG_ASSERT(write_res.HasValue() && std::get(write_res.GetValue()).error, + MG_ASSERT(write_res.HasValue() && !std::get(write_res.GetValue()).error.has_value(), "Unexpected failure"); Commit(client, create_req.transaction_id); diff --git a/tests/unit/machine_manager.cpp b/tests/unit/machine_manager.cpp index 7b57d61ea..110220eda 100644 --- a/tests/unit/machine_manager.cpp +++ b/tests/unit/machine_manager.cpp @@ -131,6 +131,7 @@ void TestCreateVertices(msgs::ShardRequestManagerInterface &shard_request_manage auto result = shard_request_manager.Request(state, std::move(new_vertices)); EXPECT_EQ(result.size(), 1); + EXPECT_FALSE(result[0].error.has_value()) << result[0].error->message; } void TestCreateExpand(msgs::ShardRequestManagerInterface &shard_request_manager) { diff --git a/tests/unit/storage_v3_schema.cpp b/tests/unit/storage_v3_schema.cpp index 2c5515958..df84fd70c 100644 --- a/tests/unit/storage_v3_schema.cpp +++ b/tests/unit/storage_v3_schema.cpp @@ -186,43 +186,43 @@ TEST_F(SchemaValidatorTest, TestSchemaValidateVertexCreate) { // Validate against secondary label { const auto schema_violation = schema_validator.ValidateVertexCreate(NameToLabel("test"), {}, {PropertyValue(1)}); - ASSERT_FALSE(schema_violation.HasError()); + ASSERT_TRUE(schema_violation.HasError()); EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {}); - ASSERT_FALSE(schema_violation.HasError()); + ASSERT_TRUE(schema_violation.HasError()); EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED)); } // Validate wrong secondary label { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {label1}, {PropertyValue("test")}); - ASSERT_FALSE(schema_violation.HasError()); + ASSERT_TRUE(schema_violation.HasError()); EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {label2}, {PropertyValue("test")}); - ASSERT_FALSE(schema_violation.HasError()); + ASSERT_TRUE(schema_violation.HasError()); EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY)); } // Validate wrong property type { const auto schema_violation = schema_validator.ValidateVertexCreate(label1, {}, {PropertyValue(1)}); - ASSERT_FALSE(schema_violation.HasError()); + ASSERT_TRUE(schema_violation.HasError()); EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } { const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {PropertyValue("test"), PropertyValue(12), PropertyValue(1)}); - ASSERT_FALSE(schema_violation.HasError()); + ASSERT_TRUE(schema_violation.HasError()); EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } { const auto wrong_prop = PropertyValue(TemporalData(TemporalType::Date, 1234)); const auto schema_violation = schema_validator.ValidateVertexCreate(label2, {}, {PropertyValue("test"), PropertyValue(12), wrong_prop}); - ASSERT_FALSE(schema_violation.HasError()); + ASSERT_TRUE(schema_violation.HasError()); EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE)); } // Passing validations @@ -247,12 +247,12 @@ TEST_F(SchemaValidatorTest, TestSchemaValidatePropertyUpdate) { // Validate updating of primary key { const auto schema_violation = schema_validator.ValidatePropertyUpdate(label1, prop_string); - ASSERT_FALSE(schema_violation.HasError()); + ASSERT_TRUE(schema_violation.HasError()); EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } { const auto schema_violation = schema_validator.ValidatePropertyUpdate(label2, prop_duration); - ASSERT_FALSE(schema_violation.HasError()); + ASSERT_TRUE(schema_violation.HasError()); EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY)); } EXPECT_FALSE(schema_validator.ValidatePropertyUpdate(label1, prop_int).HasError()); @@ -264,12 +264,12 @@ TEST_F(SchemaValidatorTest, TestSchemaValidatePropertyUpdateLabel) { // Validate adding primary label { const auto schema_violation = schema_validator.ValidateLabelUpdate(label1); - ASSERT_FALSE(schema_violation.HasError()); + ASSERT_TRUE(schema_violation.HasError()); EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } { const auto schema_violation = schema_validator.ValidateLabelUpdate(label2); - ASSERT_FALSE(schema_violation.HasError()); + ASSERT_TRUE(schema_violation.HasError()); EXPECT_EQ(schema_violation.GetError(), SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL)); } EXPECT_FALSE(schema_validator.ValidateLabelUpdate(NameToLabel("test")).HasError()); From accf015dcfb180c8a67f3207a30291bfa0483a09 Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 23 Nov 2022 11:03:17 +0100 Subject: [PATCH 095/103] Fix clang-tidy errors --- src/storage/v3/schema_validator.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/storage/v3/schema_validator.cpp b/src/storage/v3/schema_validator.cpp index ea2522684..d21043868 100644 --- a/src/storage/v3/schema_validator.cpp +++ b/src/storage/v3/schema_validator.cpp @@ -54,7 +54,7 @@ ShardResult SchemaValidator::ValidateVertexCreate(LabelId primary_label, c // Check schema property type if (auto property_schema_type = PropertyTypeToSchemaType(primary_properties[i]); property_schema_type && *property_schema_type != schema->second[i].type) { - return SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE, + return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE, "Property {} is of wrong type, expected {}, actual {}", name_id_mapper_->IdToName(schema->second[i].property_id.AsInt()), SchemaTypeToString(schema->second[i].type), SchemaTypeToString(*property_schema_type)); @@ -75,7 +75,7 @@ ShardResult SchemaValidator::ValidatePropertyUpdate(const LabelId primary_ schema->second, [property_id](const auto &schema_property) { return property_id == schema_property.property_id; }); schema_property != schema->second.end()) { - return SHARD_ERROR(common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY, + return SHARD_ERROR(ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY, "Cannot update primary property {} of schema on label :{}", name_id_mapper_->IdToName(schema_property->property_id.AsInt()), name_id_mapper_->IdToName(primary_label.AsInt())); From ce0f1a09f7d661463586390365f2f40e3b06f96c Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 23 Nov 2022 11:32:53 +0100 Subject: [PATCH 096/103] Remove unused methods from accessors --- src/query/v2/common.hpp | 1 - src/query/v2/db_accessor.hpp | 43 ------------------------------------ 2 files changed, 44 deletions(-) diff --git a/src/query/v2/common.hpp b/src/query/v2/common.hpp index 089f3a4f9..0ee33c650 100644 --- a/src/query/v2/common.hpp +++ b/src/query/v2/common.hpp @@ -20,7 +20,6 @@ #include "query/v2/bindings/symbol.hpp" #include "query/v2/bindings/typed_value.hpp" -#include "query/v2/db_accessor.hpp" #include "query/v2/exceptions.hpp" #include "query/v2/frontend/ast/ast.hpp" #include "query/v2/path.hpp" diff --git a/src/query/v2/db_accessor.hpp b/src/query/v2/db_accessor.hpp index dfa23376f..e97b47f63 100644 --- a/src/query/v2/db_accessor.hpp +++ b/src/query/v2/db_accessor.hpp @@ -69,19 +69,6 @@ class EdgeAccessor final { return impl_.GetProperty(key, view); } - storage::v3::ShardResult SetProperty(storage::v3::PropertyId key, - const storage::v3::PropertyValue &value) { - return impl_.SetProperty(key, value); - } - - storage::v3::ShardResult RemoveProperty(storage::v3::PropertyId key) { - return SetProperty(key, storage::v3::PropertyValue()); - } - - storage::v3::ShardResult> ClearProperties() { - return impl_.ClearProperties(); - } - VertexAccessor To() const; VertexAccessor From() const; @@ -113,18 +100,6 @@ class VertexAccessor final { auto PrimaryKey(storage::v3::View view) const { return impl_.PrimaryKey(view); } - storage::v3::ShardResult AddLabel(storage::v3::LabelId label) { return impl_.AddLabelAndValidate(label); } - - storage::v3::ShardResult AddLabelAndValidate(storage::v3::LabelId label) { - return impl_.AddLabelAndValidate(label); - } - - storage::v3::ShardResult RemoveLabel(storage::v3::LabelId label) { return impl_.RemoveLabelAndValidate(label); } - - storage::v3::ShardResult RemoveLabelAndValidate(storage::v3::LabelId label) { - return impl_.RemoveLabelAndValidate(label); - } - storage::v3::ShardResult HasLabel(storage::v3::View view, storage::v3::LabelId label) const { return impl_.HasLabel(label, view); } @@ -136,24 +111,6 @@ class VertexAccessor final { return impl_.GetProperty(key, view); } - storage::v3::ShardResult SetProperty(storage::v3::PropertyId key, - const storage::v3::PropertyValue &value) { - return impl_.SetPropertyAndValidate(key, value); - } - - storage::v3::ShardResult SetPropertyAndValidate(storage::v3::PropertyId key, - const storage::v3::PropertyValue &value) { - return impl_.SetPropertyAndValidate(key, value); - } - - storage::v3::ShardResult RemovePropertyAndValidate(storage::v3::PropertyId key) { - return SetPropertyAndValidate(key, storage::v3::PropertyValue{}); - } - - storage::v3::ShardResult> ClearProperties() { - return impl_.ClearProperties(); - } - auto InEdges(storage::v3::View view, const std::vector &edge_types) const -> storage::v3::ShardResult { auto maybe_edges = impl_.InEdges(view, edge_types); From 9b19dd57d3d3c9347be8b00879e30abaf4668ccf Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 23 Nov 2022 13:19:25 +0100 Subject: [PATCH 097/103] Remove storage include --- tests/unit/storage_v3_shard_rsm.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/storage_v3_shard_rsm.cpp b/tests/unit/storage_v3_shard_rsm.cpp index a45a60db4..6f8e31d03 100644 --- a/tests/unit/storage_v3_shard_rsm.cpp +++ b/tests/unit/storage_v3_shard_rsm.cpp @@ -28,7 +28,6 @@ #include "storage/v3/schema_validator.hpp" #include "storage/v3/schemas.hpp" #include "storage/v3/shard_rsm.hpp" -#include "storage/v3/storage.hpp" #include "storage/v3/temporal.hpp" #include "storage/v3/vertex_id.hpp" From 56e2ad4546e1adf4fdb3c6fd635f7f531cd7fa33 Mon Sep 17 00:00:00 2001 From: Jeremy B <97525434+42jeremy@users.noreply.github.com> Date: Wed, 23 Nov 2022 13:24:35 +0100 Subject: [PATCH 098/103] Update src/storage/v3/shard_rsm.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: János Benjamin Antal --- src/storage/v3/shard_rsm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 26fba10cf..7e90b11bd 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -531,7 +531,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) { std::optional maybe_result; - if (req.order_by_vertices.empty()) { + if (req.order_by_edges.empty()) { const auto *schema = shard_->GetSchema(shard_->PrimaryLabel()); MG_ASSERT(schema); maybe_result = From 6d86801be0bd4e7f964d2ad318bc87d455c0cba7 Mon Sep 17 00:00:00 2001 From: jeremy Date: Wed, 23 Nov 2022 13:54:54 +0100 Subject: [PATCH 099/103] Extract logic to convert ORderingDirection to Ordering --- src/storage/v3/request_helper.cpp | 9 ++------- src/storage/v3/request_helper.hpp | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index cca506088..bfc217b81 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -499,13 +499,8 @@ std::vector> OrderByEdges(DbAccessor &dba, std::vector ordering; ordering.reserve(order_by_edges.size()); - std::transform(order_by_edges.begin(), order_by_edges.end(), std::back_inserter(ordering), [](const auto &order_by) { - if (memgraph::msgs::OrderingDirection::ASCENDING == order_by.direction) { - return Ordering::ASC; - } - MG_ASSERT(memgraph::msgs::OrderingDirection::DESCENDING == order_by.direction); - return Ordering::DESC; - }); + std::transform(order_by_edges.begin(), order_by_edges.end(), std::back_inserter(ordering), + [](const auto &order_by) { return ConvertMsgsOrderByToOrdering(order_by.direction); }); std::vector> ordered; for (auto it = iterable.begin(); it != iterable.end(); ++it) { diff --git a/src/storage/v3/request_helper.hpp b/src/storage/v3/request_helper.hpp index ebf4f1d0e..dce555d88 100644 --- a/src/storage/v3/request_helper.hpp +++ b/src/storage/v3/request_helper.hpp @@ -84,6 +84,17 @@ inline bool TypedValueCompare(const TypedValue &a, const TypedValue &b) { } } +inline Ordering ConvertMsgsOrderByToOrdering(msgs::OrderingDirection ordering) { + switch (ordering) { + case memgraph::msgs::OrderingDirection::ASCENDING: + return memgraph::storage::v3::Ordering::ASC; + case memgraph::msgs::OrderingDirection::DESCENDING: + return memgraph::storage::v3::Ordering::DESC; + default: + LOG_FATAL("Unknown ordering direction"); + } +} + class TypedValueVectorCompare final { public: explicit TypedValueVectorCompare(const std::vector &ordering) : ordering_(ordering) {} @@ -126,16 +137,7 @@ std::vector> OrderByVertices(DbAccessor &dba, TIterable std::vector ordering; ordering.reserve(order_by_vertices.size()); std::transform(order_by_vertices.begin(), order_by_vertices.end(), std::back_inserter(ordering), - [](const auto &order_by) { - switch (order_by.direction) { - case memgraph::msgs::OrderingDirection::ASCENDING: - return Ordering::ASC; - case memgraph::msgs::OrderingDirection::DESCENDING: - return Ordering::DESC; - default: - LOG_FATAL("Unknown ordering direction"); - } - }); + [](const auto &order_by) { return ConvertMsgsOrderByToOrdering(order_by.direction); }); std::vector> ordered; for (auto it = iterable.begin(); it != iterable.end(); ++it) { From ab5fc05fd76acf94004865fe0537727806fff99d Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 23 Nov 2022 15:32:28 +0100 Subject: [PATCH 100/103] Address review comments --- src/glue/v2/communication.cpp | 50 ++++++++++++++----------------- src/glue/v2/communication.hpp | 27 ++++++++--------- src/memgraph.cpp | 48 ++++------------------------- src/storage/v3/name_id_mapper.hpp | 1 - src/storage/v3/result.hpp | 1 - 5 files changed, 40 insertions(+), 87 deletions(-) diff --git a/src/glue/v2/communication.cpp b/src/glue/v2/communication.cpp index bc8d2da09..cd3f41255 100644 --- a/src/glue/v2/communication.cpp +++ b/src/glue/v2/communication.cpp @@ -71,9 +71,9 @@ query::v2::TypedValue ToTypedValue(const Value &value) { } } -BoltResult ToBoltVertex(const query::v2::accessors::VertexAccessor &vertex, - const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View /*view*/) { +communication::bolt::Vertex ToBoltVertex(const query::v2::accessors::VertexAccessor &vertex, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View /*view*/) { auto id = communication::bolt::Id::FromUint(0); auto labels = vertex.Labels(); @@ -91,9 +91,9 @@ BoltResult ToBoltVertex(const query::v2::accessors: return communication::bolt::Vertex{id, new_labels, new_properties}; } -BoltResult ToBoltEdge(const query::v2::accessors::EdgeAccessor &edge, - const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View /*view*/) { +communication::bolt::Edge ToBoltEdge(const query::v2::accessors::EdgeAccessor &edge, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View /*view*/) { // TODO(jbajic) Fix bolt communication auto id = communication::bolt::Id::FromUint(0); auto from = communication::bolt::Id::FromUint(0); @@ -108,15 +108,16 @@ BoltResult ToBoltEdge(const query::v2::accessors::Edg return communication::bolt::Edge{id, from, to, type, new_properties}; } -BoltResult ToBoltPath(const query::v2::accessors::Path & /*edge*/, - const msgs::ShardRequestManagerInterface * /*shard_request_manager*/, - storage::v3::View /*view*/) { +communication::bolt::Path ToBoltPath(const query::v2::accessors::Path & /*edge*/, + const msgs::ShardRequestManagerInterface * /*shard_request_manager*/, + storage::v3::View /*view*/) { // TODO(jbajic) Fix bolt communication - return {SHARD_ERROR(ErrorCode::DELETED_OBJECT)}; + MG_ASSERT(false, "Path is unimplemented!"); + return {}; } -BoltResult ToBoltValue(const query::v2::TypedValue &value, - const msgs::ShardRequestManagerInterface *shard_request_manager, storage::v3::View view) { +Value ToBoltValue(const query::v2::TypedValue &value, const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View view) { switch (value.type()) { case query::v2::TypedValue::Type::Null: return Value(); @@ -132,35 +133,30 @@ BoltResult ToBoltValue(const query::v2::TypedValue &value, std::vector values; values.reserve(value.ValueList().size()); for (const auto &v : value.ValueList()) { - auto maybe_value = ToBoltValue(v, shard_request_manager, view); - if (maybe_value.HasError()) return maybe_value.GetError(); - values.emplace_back(std::move(*maybe_value)); + auto value = ToBoltValue(v, shard_request_manager, view); + values.emplace_back(std::move(value)); } return Value(std::move(values)); } case query::v2::TypedValue::Type::Map: { std::map map; for (const auto &kv : value.ValueMap()) { - auto maybe_value = ToBoltValue(kv.second, shard_request_manager, view); - if (maybe_value.HasError()) return maybe_value.GetError(); - map.emplace(kv.first, std::move(*maybe_value)); + auto value = ToBoltValue(kv.second, shard_request_manager, view); + map.emplace(kv.first, std::move(value)); } return Value(std::move(map)); } case query::v2::TypedValue::Type::Vertex: { - auto maybe_vertex = ToBoltVertex(value.ValueVertex(), shard_request_manager, view); - if (maybe_vertex.HasError()) return maybe_vertex.GetError(); - return Value(std::move(*maybe_vertex)); + auto vertex = ToBoltVertex(value.ValueVertex(), shard_request_manager, view); + return Value(std::move(vertex)); } case query::v2::TypedValue::Type::Edge: { - auto maybe_edge = ToBoltEdge(value.ValueEdge(), shard_request_manager, view); - if (maybe_edge.HasError()) return maybe_edge.GetError(); - return Value(std::move(*maybe_edge)); + auto edge = ToBoltEdge(value.ValueEdge(), shard_request_manager, view); + return Value(std::move(edge)); } case query::v2::TypedValue::Type::Path: { - auto maybe_path = ToBoltPath(value.ValuePath(), shard_request_manager, view); - if (maybe_path.HasError()) return maybe_path.GetError(); - return Value(std::move(*maybe_path)); + auto path = ToBoltPath(value.ValuePath(), shard_request_manager, view); + return Value(std::move(path)); } case query::v2::TypedValue::Type::Date: return Value(value.ValueDate()); diff --git a/src/glue/v2/communication.hpp b/src/glue/v2/communication.hpp index 4ea05a61d..a20162176 100644 --- a/src/glue/v2/communication.hpp +++ b/src/glue/v2/communication.hpp @@ -30,45 +30,42 @@ class VertexAccessor; namespace memgraph::glue::v2 { -template -using BoltResult = utils::BasicResult; - /// @param storage::v3::VertexAccessor for converting to /// communication::bolt::Vertex. /// @param msgs::ShardRequestManagerInterface *shard_request_manager getting label and property names. /// @param storage::v3::View for deciding which vertex attributes are visible. /// /// @throw std::bad_alloc -BoltResult ToBoltVertex(const storage::v3::VertexAccessor &vertex, - const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View view); +communication::bolt::Vertex ToBoltVertex(const storage::v3::VertexAccessor &vertex, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View view); /// @param storage::v3::EdgeAccessor for converting to communication::bolt::Edge. /// @param msgs::ShardRequestManagerInterface *shard_request_manager getting edge type and property names. /// @param storage::v3::View for deciding which edge attributes are visible. /// /// @throw std::bad_alloc -BoltResult ToBoltEdge(const storage::v3::EdgeAccessor &edge, - const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View view); +communication::bolt::Edge ToBoltEdge(const storage::v3::EdgeAccessor &edge, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View view); /// @param query::v2::Path for converting to communication::bolt::Path. /// @param msgs::ShardRequestManagerInterface *shard_request_manager ToBoltVertex and ToBoltEdge. /// @param storage::v3::View for ToBoltVertex and ToBoltEdge. /// /// @throw std::bad_alloc -BoltResult ToBoltPath(const query::v2::accessors::Path &path, - const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View view); +communication::bolt::Path ToBoltPath(const query::v2::accessors::Path &path, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View view); /// @param query::v2::TypedValue for converting to communication::bolt::Value. /// @param msgs::ShardRequestManagerInterface *shard_request_manager ToBoltVertex and ToBoltEdge. /// @param storage::v3::View for ToBoltVertex and ToBoltEdge. /// /// @throw std::bad_alloc -BoltResult ToBoltValue(const query::v2::TypedValue &value, - const msgs::ShardRequestManagerInterface *shard_request_manager, - storage::v3::View view); +communication::bolt::Value ToBoltValue(const query::v2::TypedValue &value, + const msgs::ShardRequestManagerInterface *shard_request_manager, + storage::v3::View view); query::v2::TypedValue ToTypedValue(const communication::bolt::Value &value); diff --git a/src/memgraph.cpp b/src/memgraph.cpp index 23582e9ea..5903e65ad 100644 --- a/src/memgraph.cpp +++ b/src/memgraph.cpp @@ -481,27 +481,9 @@ class BoltSession final : public memgraph::communication::bolt::Session decoded_summary; for (const auto &kv : summary) { - auto maybe_value = memgraph::glue::v2::ToBoltValue(kv.second, interpreter_.GetShardRequestManager(), - memgraph::storage::v3::View::NEW); - if (maybe_value.HasError()) { - switch (maybe_value.GetError().code) { - case memgraph::common::ErrorCode::DELETED_OBJECT: - case memgraph::common::ErrorCode::SERIALIZATION_ERROR: - case memgraph::common::ErrorCode::VERTEX_HAS_EDGES: - case memgraph::common::ErrorCode::PROPERTIES_DISABLED: - case memgraph::common::ErrorCode::NONEXISTENT_OBJECT: - case memgraph::common::ErrorCode::VERTEX_ALREADY_INSERTED: - case memgraph::common::ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: - case memgraph::common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: - case memgraph::common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: - case memgraph::common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: - case memgraph::common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: - case memgraph::common::ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: - case memgraph::common::ErrorCode::OBJECT_NOT_FOUND: - throw memgraph::communication::bolt::ClientError("Unexpected storage error when streaming summary."); - } - } - decoded_summary.emplace(kv.first, std::move(*maybe_value)); + auto bolt_value = memgraph::glue::v2::ToBoltValue(kv.second, interpreter_.GetShardRequestManager(), + memgraph::storage::v3::View::NEW); + decoded_summary.emplace(kv.first, std::move(bolt_value)); } return decoded_summary; } catch (const memgraph::query::v2::QueryException &e) { @@ -522,28 +504,8 @@ class BoltSession final : public memgraph::communication::bolt::Session decoded_values; decoded_values.reserve(values.size()); for (const auto &v : values) { - auto maybe_value = memgraph::glue::v2::ToBoltValue(v, shard_request_manager_, memgraph::storage::v3::View::NEW); - if (maybe_value.HasError()) { - switch (maybe_value.GetError().code) { - case memgraph::common::ErrorCode::DELETED_OBJECT: - throw memgraph::communication::bolt::ClientError("Returning a deleted object as a result."); - case memgraph::common::ErrorCode::NONEXISTENT_OBJECT: - case memgraph::common::ErrorCode::OBJECT_NOT_FOUND: - throw memgraph::communication::bolt::ClientError("Returning a nonexistent object as a result."); - case memgraph::common::ErrorCode::VERTEX_HAS_EDGES: - case memgraph::common::ErrorCode::SERIALIZATION_ERROR: - case memgraph::common::ErrorCode::PROPERTIES_DISABLED: - case memgraph::common::ErrorCode::VERTEX_ALREADY_INSERTED: - case memgraph::common::ErrorCode::SCHEMA_NO_SCHEMA_DEFINED_FOR_LABEL: - case memgraph::common::ErrorCode::SCHEMA_VERTEX_PROPERTY_WRONG_TYPE: - case memgraph::common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_KEY: - case memgraph::common::ErrorCode::SCHEMA_VERTEX_UPDATE_PRIMARY_LABEL: - case memgraph::common::ErrorCode::SCHEMA_VERTEX_SECONDARY_LABEL_IS_PRIMARY: - case memgraph::common::ErrorCode::SCHEMA_VERTEX_PRIMARY_PROPERTIES_UNDEFINED: - throw memgraph::communication::bolt::ClientError("Unexpected storage error when streaming results."); - } - } - decoded_values.emplace_back(std::move(*maybe_value)); + auto bolt_value = memgraph::glue::v2::ToBoltValue(v, shard_request_manager_, memgraph::storage::v3::View::NEW); + decoded_values.emplace_back(std::move(bolt_value)); } encoder_->MessageRecord(decoded_values); } diff --git a/src/storage/v3/name_id_mapper.hpp b/src/storage/v3/name_id_mapper.hpp index d238f1072..f241bca54 100644 --- a/src/storage/v3/name_id_mapper.hpp +++ b/src/storage/v3/name_id_mapper.hpp @@ -17,7 +17,6 @@ #include #include -#include "storage/v3/id_types.hpp" #include "utils/logging.hpp" #include "utils/skip_list.hpp" diff --git a/src/storage/v3/result.hpp b/src/storage/v3/result.hpp index fcec17548..e3561fcc0 100644 --- a/src/storage/v3/result.hpp +++ b/src/storage/v3/result.hpp @@ -32,7 +32,6 @@ struct ShardError { : code{code}, source{fmt::format("{}:{}", location.file_name(), location.line())} {} common::ErrorCode code; - // TODO Maybe add category std::string message; std::string source; From d6b444c38bd8071cb3ab3ad846ef6d396560c2a2 Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 23 Nov 2022 18:02:00 +0100 Subject: [PATCH 101/103] Log transaction id --- src/storage/v3/shard_rsm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/v3/shard_rsm.cpp b/src/storage/v3/shard_rsm.cpp index 5b919402a..b13a5ad54 100644 --- a/src/storage/v3/shard_rsm.cpp +++ b/src/storage/v3/shard_rsm.cpp @@ -466,7 +466,7 @@ EdgeFiller InitializeEdgeFillerFunction(const msgs::ExpandOneRequest &req) { auto CreateErrorResponse(const ShardError &shard_error, const auto transaction_id, const std::string_view action) { msgs::ShardError message_shard_error{shard_error.code, shard_error.message}; - spdlog::debug("{} In transaction {} {} failed: {}: {}", shard_error.source, transaction_id, action, + spdlog::debug("{} In transaction {} {} failed: {}: {}", shard_error.source, transaction_id.logical_id, action, ErrorCodeToString(shard_error.code), shard_error.message); return message_shard_error; } From d820d0a9e52030c9aef7327d0dc511cfb4834a22 Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 23 Nov 2022 22:29:03 +0100 Subject: [PATCH 102/103] Fix clang tidy errors --- src/glue/v2/communication.cpp | 28 ++++++++++++++-------------- src/storage/v3/request_helper.cpp | 10 +++++----- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/glue/v2/communication.cpp b/src/glue/v2/communication.cpp index cd3f41255..eedf699bb 100644 --- a/src/glue/v2/communication.cpp +++ b/src/glue/v2/communication.cpp @@ -120,15 +120,15 @@ Value ToBoltValue(const query::v2::TypedValue &value, const msgs::ShardRequestMa storage::v3::View view) { switch (value.type()) { case query::v2::TypedValue::Type::Null: - return Value(); + return {}; case query::v2::TypedValue::Type::Bool: - return Value(value.ValueBool()); + return {value.ValueBool()}; case query::v2::TypedValue::Type::Int: - return Value(value.ValueInt()); + return {value.ValueInt()}; case query::v2::TypedValue::Type::Double: - return Value(value.ValueDouble()); + return {value.ValueDouble()}; case query::v2::TypedValue::Type::String: - return Value(std::string(value.ValueString())); + return {std::string(value.ValueString())}; case query::v2::TypedValue::Type::List: { std::vector values; values.reserve(value.ValueList().size()); @@ -136,7 +136,7 @@ Value ToBoltValue(const query::v2::TypedValue &value, const msgs::ShardRequestMa auto value = ToBoltValue(v, shard_request_manager, view); values.emplace_back(std::move(value)); } - return Value(std::move(values)); + return {std::move(values)}; } case query::v2::TypedValue::Type::Map: { std::map map; @@ -144,28 +144,28 @@ Value ToBoltValue(const query::v2::TypedValue &value, const msgs::ShardRequestMa auto value = ToBoltValue(kv.second, shard_request_manager, view); map.emplace(kv.first, std::move(value)); } - return Value(std::move(map)); + return {std::move(map)}; } case query::v2::TypedValue::Type::Vertex: { auto vertex = ToBoltVertex(value.ValueVertex(), shard_request_manager, view); - return Value(std::move(vertex)); + return {std::move(vertex)}; } case query::v2::TypedValue::Type::Edge: { auto edge = ToBoltEdge(value.ValueEdge(), shard_request_manager, view); - return Value(std::move(edge)); + return {std::move(edge)}; } case query::v2::TypedValue::Type::Path: { auto path = ToBoltPath(value.ValuePath(), shard_request_manager, view); - return Value(std::move(path)); + return {std::move(path)}; } case query::v2::TypedValue::Type::Date: - return Value(value.ValueDate()); + return {value.ValueDate()}; case query::v2::TypedValue::Type::LocalTime: - return Value(value.ValueLocalTime()); + return {value.ValueLocalTime()}; case query::v2::TypedValue::Type::LocalDateTime: - return Value(value.ValueLocalDateTime()); + return {value.ValueLocalDateTime()}; case query::v2::TypedValue::Type::Duration: - return Value(value.ValueDuration()); + return {value.ValueDuration()}; } } diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index 6a0d6da7e..dd94309ab 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -360,7 +360,7 @@ bool FilterOnVertex(DbAccessor &dba, const storage::v3::VertexAccessor &v_acc, c ShardResult GetExpandOneResult( Shard::Accessor &acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, - const EdgeUniquenessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, + const EdgeUniquenessFunction &maybe_filter_based_on_edge_uniqueness, const EdgeFiller &edge_filler, const Schemas::Schema &schema) { /// Fill up source vertex const auto primary_key = ConvertPropertyVector(src_vertex.second); @@ -379,7 +379,7 @@ ShardResult GetExpandOneResult( } /// Fill up connecting edges - auto fill_up_connecting_edges = FillUpConnectingEdges(v_acc, req, maybe_filter_based_on_edge_uniquness); + auto fill_up_connecting_edges = FillUpConnectingEdges(v_acc, req, maybe_filter_based_on_edge_uniqueness); if (fill_up_connecting_edges.HasError()) { return fill_up_connecting_edges.GetError(); } @@ -403,7 +403,7 @@ ShardResult GetExpandOneResult( ShardResult GetExpandOneResult( VertexAccessor v_acc, msgs::VertexId src_vertex, const msgs::ExpandOneRequest &req, std::vector in_edge_accessors, std::vector out_edge_accessors, - const EdgeUniquenessFunction &maybe_filter_based_on_edge_uniquness, const EdgeFiller &edge_filler, + const EdgeUniquenessFunction &maybe_filter_based_on_edge_uniqueness, const EdgeFiller &edge_filler, const Schemas::Schema &schema) { /// Fill up source vertex msgs::Vertex source_vertex = {.id = src_vertex}; @@ -420,8 +420,8 @@ ShardResult GetExpandOneResult( } /// Fill up connecting edges - auto in_edges = maybe_filter_based_on_edge_uniquness(std::move(in_edge_accessors), msgs::EdgeDirection::IN); - auto out_edges = maybe_filter_based_on_edge_uniquness(std::move(out_edge_accessors), msgs::EdgeDirection::OUT); + auto in_edges = maybe_filter_based_on_edge_uniqueness(std::move(in_edge_accessors), msgs::EdgeDirection::IN); + auto out_edges = maybe_filter_based_on_edge_uniqueness(std::move(out_edge_accessors), msgs::EdgeDirection::OUT); msgs::ExpandOneResultRow result_row; result_row.src_vertex = std::move(source_vertex); From c4327cfb001bfd6674083d38bad07d365a095142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Benjamin=20Antal?= Date: Thu, 24 Nov 2022 11:13:55 +0100 Subject: [PATCH 103/103] Make implicit-fallthrough a compilation error --- CMakeLists.txt | 3 ++- src/communication/bolt/v1/states/executing.hpp | 2 +- src/storage/v2/constraints.cpp | 4 ++-- src/storage/v3/request_helper.cpp | 3 +++ 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 916389424..36e648e8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -182,7 +182,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # c99-designator is disabled because of required mixture of designated and # non-designated initializers in Python Query Module code (`py_module.cpp`). set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall \ - -Werror=switch -Werror=switch-bool -Werror=return-type \ + -Werror=switch -Werror=switch-bool -Werror=implicit-fallthrough \ + -Werror=return-type \ -Werror=return-stack-address \ -Wno-c99-designator \ -DBOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT") diff --git a/src/communication/bolt/v1/states/executing.hpp b/src/communication/bolt/v1/states/executing.hpp index 54504985b..de4b2a00e 100644 --- a/src/communication/bolt/v1/states/executing.hpp +++ b/src/communication/bolt/v1/states/executing.hpp @@ -74,7 +74,7 @@ State RunHandlerV4(Signature signature, TSession &session, State state, Marker m } case Signature::Route: { if constexpr (bolt_minor >= 3) { - if (signature == Signature::Route) return HandleRoute(session); + return HandleRoute(session); } else { spdlog::trace("Supported only in bolt v4.3"); return State::Close; diff --git a/src/storage/v2/constraints.cpp b/src/storage/v2/constraints.cpp index fab6ee4c4..2a71ee2b0 100644 --- a/src/storage/v2/constraints.cpp +++ b/src/storage/v2/constraints.cpp @@ -97,15 +97,15 @@ bool LastCommittedVersionHasLabelProperty(const Vertex &vertex, LabelId label, c if (delta->label == label) { MG_ASSERT(!has_label, "Invalid database state!"); has_label = true; - break; } + break; } case Delta::Action::REMOVE_LABEL: { if (delta->label == label) { MG_ASSERT(has_label, "Invalid database state!"); has_label = false; - break; } + break; } case Delta::Action::ADD_IN_EDGE: case Delta::Action::ADD_OUT_EDGE: diff --git a/src/storage/v3/request_helper.cpp b/src/storage/v3/request_helper.cpp index 8288a6154..0f6132a1a 100644 --- a/src/storage/v3/request_helper.cpp +++ b/src/storage/v3/request_helper.cpp @@ -471,12 +471,14 @@ std::array, 2> GetEdgesFromVertex(const VertexAccessor if (edges.HasValue()) { in_edges = edges.GetValue(); } + break; } case memgraph::msgs::EdgeDirection::OUT: { auto edges = vertex_accessor.OutEdges(View::OLD); if (edges.HasValue()) { out_edges = edges.GetValue(); } + break; } case memgraph::msgs::EdgeDirection::BOTH: { auto maybe_in_edges = vertex_accessor.InEdges(View::OLD); @@ -488,6 +490,7 @@ std::array, 2> GetEdgesFromVertex(const VertexAccessor if (maybe_out_edges.HasValue()) { out_edges = maybe_out_edges.GetValue(); } + break; } }