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
This commit is contained in:
Teon Banek 2019-09-04 14:45:49 +02:00
parent 9a94205a07
commit 2e51ef9f66
6 changed files with 54 additions and 24 deletions

View File

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

View File

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

View File

@ -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<Value> 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<std::string, Value> 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<VertexAccessor &>(vertex).SwitchOld();
break;
case storage::View::NEW:
const_cast<VertexAccessor &>(vertex).SwitchNew();
break;
}
auto id = communication::bolt::Id::FromUint(vertex.gid().AsUint());
std::vector<std::string> 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<EdgeAccessor &>(edge).SwitchOld();
break;
case storage::View::NEW:
const_cast<EdgeAccessor &>(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<communication::bolt::Vertex> 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<communication::bolt::Edge> 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);
}

View File

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

View File

@ -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<std::string, communication::bolt::Value> BoltSession::PullAll(
const auto &summary = transaction_engine_.PullAll(&stream);
std::map<std::string, communication::bolt::Value> 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<communication::bolt::Value> 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);
}

View File

@ -189,9 +189,9 @@ TEST(BoltEncoder, VertexAndEdge) {
// check everything
std::vector<Value> 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,