From 2e51ef9f66b09a880d184301b7c226f2fe4e3ea8 Mon Sep 17 00:00:00 2001 From: Teon Banek Date: Wed, 4 Sep 2019 14:45:49 +0200 Subject: [PATCH] Use storage/v2/view in glue/communication Summary: Switching to Storage V2 API will require passing storage::View when serializing VertexAccessor and EdgeAccessor, so this is just the first step in adapting the code. Reviewers: mferencevic, ipaljak Reviewed By: mferencevic Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D2352 --- src/durability/single_node/snapshooter.cpp | 5 ++- src/durability/single_node_ha/snapshooter.cpp | 5 ++- src/glue/communication.cpp | 43 ++++++++++++++----- src/glue/communication.hpp | 13 ++++-- src/memgraph_init.cpp | 6 ++- tests/unit/bolt_encoder.cpp | 6 +-- 6 files changed, 54 insertions(+), 24 deletions(-) diff --git a/src/durability/single_node/snapshooter.cpp b/src/durability/single_node/snapshooter.cpp index 32e318781..a0f556b60 100644 --- a/src/durability/single_node/snapshooter.cpp +++ b/src/durability/single_node/snapshooter.cpp @@ -10,6 +10,7 @@ #include "durability/single_node/paths.hpp" #include "durability/single_node/version.hpp" #include "glue/communication.hpp" +#include "storage/v2/view.hpp" #include "utils/file.hpp" namespace fs = std::filesystem; @@ -69,11 +70,11 @@ bool Encode(const fs::path &snapshot_file, database::GraphDb &db, } for (const auto &vertex : dba.Vertices(false)) { - encoder.WriteVertex(glue::ToBoltVertex(vertex)); + encoder.WriteVertex(glue::ToBoltVertex(vertex, storage::View::OLD)); vertex_num++; } for (const auto &edge : dba.Edges(false)) { - encoder.WriteEdge(glue::ToBoltEdge(edge)); + encoder.WriteEdge(glue::ToBoltEdge(edge, storage::View::OLD)); edge_num++; } buffer.WriteValue(vertex_num); diff --git a/src/durability/single_node_ha/snapshooter.cpp b/src/durability/single_node_ha/snapshooter.cpp index bbc644798..232c3049e 100644 --- a/src/durability/single_node_ha/snapshooter.cpp +++ b/src/durability/single_node_ha/snapshooter.cpp @@ -10,6 +10,7 @@ #include "durability/single_node_ha/paths.hpp" #include "durability/single_node_ha/version.hpp" #include "glue/communication.hpp" +#include "storage/v2/view.hpp" #include "utils/file.hpp" namespace fs = std::filesystem; @@ -43,11 +44,11 @@ bool Encode(const fs::path &snapshot_file, database::GraphDb &db, } for (const auto &vertex : dba.Vertices(false)) { - encoder.WriteVertex(glue::ToBoltVertex(vertex)); + encoder.WriteVertex(glue::ToBoltVertex(vertex, storage::View::OLD)); vertex_num++; } for (const auto &edge : dba.Edges(false)) { - encoder.WriteEdge(glue::ToBoltEdge(edge)); + encoder.WriteEdge(glue::ToBoltEdge(edge, storage::View::OLD)); edge_num++; } buffer.WriteValue(vertex_num); diff --git a/src/glue/communication.cpp b/src/glue/communication.cpp index 1d4c827c6..8f23bb410 100644 --- a/src/glue/communication.cpp +++ b/src/glue/communication.cpp @@ -43,7 +43,7 @@ query::TypedValue ToTypedValue(const Value &value) { } } -Value ToBoltValue(const query::TypedValue &value) { +Value ToBoltValue(const query::TypedValue &value, storage::View view) { switch (value.type()) { case query::TypedValue::Type::Null: return Value(); @@ -59,27 +59,37 @@ Value ToBoltValue(const query::TypedValue &value) { std::vector values; values.reserve(value.ValueList().size()); for (const auto &v : value.ValueList()) { - values.push_back(ToBoltValue(v)); + values.push_back(ToBoltValue(v, view)); } return Value(std::move(values)); } case query::TypedValue::Type::Map: { std::map map; for (const auto &kv : value.ValueMap()) { - map.emplace(kv.first, ToBoltValue(kv.second)); + map.emplace(kv.first, ToBoltValue(kv.second, view)); } return Value(std::move(map)); } case query::TypedValue::Type::Vertex: - return Value(ToBoltVertex(value.ValueVertex())); + return Value(ToBoltVertex(value.ValueVertex(), view)); case query::TypedValue::Type::Edge: - return Value(ToBoltEdge(value.ValueEdge())); + return Value(ToBoltEdge(value.ValueEdge(), view)); case query::TypedValue::Type::Path: - return Value(ToBoltPath(value.ValuePath())); + return Value(ToBoltPath(value.ValuePath(), view)); } } -communication::bolt::Vertex ToBoltVertex(const VertexAccessor &vertex) { +communication::bolt::Vertex ToBoltVertex(const VertexAccessor &vertex, + storage::View view) { + // NOTE: This hack will be removed when we switch to storage v2 API. + switch (view) { + case storage::View::OLD: + const_cast(vertex).SwitchOld(); + break; + case storage::View::NEW: + const_cast(vertex).SwitchNew(); + break; + } auto id = communication::bolt::Id::FromUint(vertex.gid().AsUint()); std::vector labels; labels.reserve(vertex.labels().size()); @@ -95,7 +105,17 @@ communication::bolt::Vertex ToBoltVertex(const VertexAccessor &vertex) { std::move(properties)}; } -communication::bolt::Edge ToBoltEdge(const EdgeAccessor &edge) { +communication::bolt::Edge ToBoltEdge(const EdgeAccessor &edge, + storage::View view) { + // NOTE: This hack will be removed when we switch to storage v2 API. + switch (view) { + case storage::View::OLD: + const_cast(edge).SwitchOld(); + break; + case storage::View::NEW: + const_cast(edge).SwitchNew(); + break; + } auto id = communication::bolt::Id::FromUint(edge.gid().AsUint()); auto from = communication::bolt::Id::FromUint(edge.from().gid().AsUint()); auto to = communication::bolt::Id::FromUint(edge.to().gid().AsUint()); @@ -108,16 +128,17 @@ communication::bolt::Edge ToBoltEdge(const EdgeAccessor &edge) { return communication::bolt::Edge{id, from, to, type, std::move(properties)}; } -communication::bolt::Path ToBoltPath(const query::Path &path) { +communication::bolt::Path ToBoltPath(const query::Path &path, + storage::View view) { std::vector vertices; vertices.reserve(path.vertices().size()); for (const auto &v : path.vertices()) { - vertices.push_back(ToBoltVertex(v)); + vertices.push_back(ToBoltVertex(v, view)); } std::vector edges; edges.reserve(path.edges().size()); for (const auto &e : path.edges()) { - edges.push_back(ToBoltEdge(e)); + edges.push_back(ToBoltEdge(e, view)); } return communication::bolt::Path(vertices, edges); } diff --git a/src/glue/communication.hpp b/src/glue/communication.hpp index 21e7b425b..5b3dbedb0 100644 --- a/src/glue/communication.hpp +++ b/src/glue/communication.hpp @@ -4,16 +4,21 @@ #include "communication/bolt/v1/value.hpp" #include "query/typed_value.hpp" #include "storage/common/types/property_value.hpp" +#include "storage/v2/view.hpp" namespace glue { -communication::bolt::Vertex ToBoltVertex(const VertexAccessor &vertex); +communication::bolt::Vertex ToBoltVertex(const VertexAccessor &vertex, + storage::View view); -communication::bolt::Edge ToBoltEdge(const EdgeAccessor &edge); +communication::bolt::Edge ToBoltEdge(const EdgeAccessor &edge, + storage::View view); -communication::bolt::Path ToBoltPath(const query::Path &path); +communication::bolt::Path ToBoltPath(const query::Path &path, + storage::View view); -communication::bolt::Value ToBoltValue(const query::TypedValue &value); +communication::bolt::Value ToBoltValue(const query::TypedValue &value, + storage::View view); query::TypedValue ToTypedValue(const communication::bolt::Value &value); diff --git a/src/memgraph_init.cpp b/src/memgraph_init.cpp index 82c378997..8df44cb5e 100644 --- a/src/memgraph_init.cpp +++ b/src/memgraph_init.cpp @@ -7,6 +7,7 @@ #include "glue/communication.hpp" #include "query/exceptions.hpp" #include "requests/requests.hpp" +#include "storage/v2/view.hpp" #include "utils/signals.hpp" #include "utils/sysinfo/memory.hpp" #include "utils/terminate_handler.hpp" @@ -81,7 +82,8 @@ std::map BoltSession::PullAll( const auto &summary = transaction_engine_.PullAll(&stream); std::map decoded_summary; for (const auto &kv : summary) { - decoded_summary.emplace(kv.first, glue::ToBoltValue(kv.second)); + decoded_summary.emplace(kv.first, + glue::ToBoltValue(kv.second, storage::View::NEW)); } return decoded_summary; } catch (const query::QueryException &e) { @@ -112,7 +114,7 @@ void BoltSession::TypedValueResultStream::Result( std::vector decoded_values; decoded_values.reserve(values.size()); for (const auto &v : values) { - decoded_values.push_back(glue::ToBoltValue(v)); + decoded_values.push_back(glue::ToBoltValue(v, storage::View::NEW)); } encoder_->MessageRecord(decoded_values); } diff --git a/tests/unit/bolt_encoder.cpp b/tests/unit/bolt_encoder.cpp index 52adc41db..4870708bf 100644 --- a/tests/unit/bolt_encoder.cpp +++ b/tests/unit/bolt_encoder.cpp @@ -189,9 +189,9 @@ TEST(BoltEncoder, VertexAndEdge) { // check everything std::vector vals; - vals.push_back(glue::ToBoltValue(query::TypedValue(va1))); - vals.push_back(glue::ToBoltValue(query::TypedValue(va2))); - vals.push_back(glue::ToBoltValue(query::TypedValue(ea))); + vals.push_back(glue::ToBoltValue(query::TypedValue(va1), storage::View::NEW)); + vals.push_back(glue::ToBoltValue(query::TypedValue(va2), storage::View::NEW)); + vals.push_back(glue::ToBoltValue(query::TypedValue(ea), storage::View::NEW)); bolt_encoder.MessageRecord(vals); // The vertexedge_encoded testdata has hardcoded zeros for IDs,