2018-08-22 16:59:46 +08:00
|
|
|
#include "glue/communication.hpp"
|
2018-07-02 21:34:33 +08:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-09-05 16:25:27 +08:00
|
|
|
#include "storage/v2/edge_accessor.hpp"
|
|
|
|
#include "storage/v2/storage.hpp"
|
|
|
|
#include "storage/v2/vertex_accessor.hpp"
|
2018-07-02 21:34:33 +08:00
|
|
|
|
2018-07-24 21:11:18 +08:00
|
|
|
using communication::bolt::Value;
|
2018-07-02 21:34:33 +08:00
|
|
|
|
Extract communication to static library
Summary:
Session specifics have been move out of the Bolt `executing` state, and
are accessed via pure virtual Session type. Our server is templated on
the session and we are setting the concrete type, so there should be no
virtual call overhead. Abstract Session is used to indicate the
interface, this could have also been templated, but the explicit
interface definition makes it clearer.
Specific session implementation for running Memgraph is now implemented
in memgraph_bolt, which instantiates the concrete session type. This may
not be 100% appropriate place, but Memgraph specific session isn't
needed anywhere else.
Bolt/communication tests now use a dummy session and depend only on
communication, which significantly improves test run times.
All these changes make the communication a library which doesn't depend
on storage nor the database. Only shared connection points, which aren't
part of the base communication library are:
* glue/conversion -- which converts between storage and bolt types, and
* communication/result_stream_faker -- templated, but used in tests and query/repl
Depends on D1453
Reviewers: mferencevic, buda, mtomic, msantl
Reviewed By: mferencevic, mtomic
Subscribers: pullbot
Differential Revision: https://phabricator.memgraph.io/D1456
2018-07-10 22:18:19 +08:00
|
|
|
namespace glue {
|
2018-07-02 21:34:33 +08:00
|
|
|
|
2018-07-24 21:11:18 +08:00
|
|
|
query::TypedValue ToTypedValue(const Value &value) {
|
2018-07-02 21:34:33 +08:00
|
|
|
switch (value.type()) {
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Null:
|
2019-06-04 22:11:31 +08:00
|
|
|
return query::TypedValue();
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Bool:
|
2018-07-02 21:34:33 +08:00
|
|
|
return query::TypedValue(value.ValueBool());
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Int:
|
2018-07-02 21:34:33 +08:00
|
|
|
return query::TypedValue(value.ValueInt());
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Double:
|
2018-07-02 21:34:33 +08:00
|
|
|
return query::TypedValue(value.ValueDouble());
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::String:
|
2018-07-02 21:34:33 +08:00
|
|
|
return query::TypedValue(value.ValueString());
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::List: {
|
2018-07-02 21:34:33 +08:00
|
|
|
std::vector<query::TypedValue> list;
|
|
|
|
list.reserve(value.ValueList().size());
|
|
|
|
for (const auto &v : value.ValueList()) list.push_back(ToTypedValue(v));
|
2019-09-02 21:09:25 +08:00
|
|
|
return query::TypedValue(std::move(list));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Map: {
|
2018-07-02 21:34:33 +08:00
|
|
|
std::map<std::string, query::TypedValue> map;
|
|
|
|
for (const auto &kv : value.ValueMap())
|
|
|
|
map.emplace(kv.first, ToTypedValue(kv.second));
|
2019-09-02 21:09:25 +08:00
|
|
|
return query::TypedValue(std::move(map));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Vertex:
|
|
|
|
case Value::Type::Edge:
|
|
|
|
case Value::Type::UnboundedEdge:
|
|
|
|
case Value::Type::Path:
|
|
|
|
throw communication::bolt::ValueException(
|
|
|
|
"Unsupported conversion from Value to TypedValue");
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 22:10:53 +08:00
|
|
|
storage::Result<communication::bolt::Vertex> ToBoltVertex(
|
|
|
|
const query::VertexAccessor &vertex, const storage::Storage &db,
|
|
|
|
storage::View view) {
|
|
|
|
return ToBoltVertex(vertex.impl_, db, view);
|
|
|
|
}
|
|
|
|
|
|
|
|
storage::Result<communication::bolt::Edge> ToBoltEdge(
|
|
|
|
const query::EdgeAccessor &edge, const storage::Storage &db,
|
|
|
|
storage::View view) {
|
|
|
|
return ToBoltEdge(edge.impl_, db, view);
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:25:27 +08:00
|
|
|
storage::Result<Value> ToBoltValue(const query::TypedValue &value,
|
|
|
|
const storage::Storage &db,
|
|
|
|
storage::View view) {
|
2018-07-02 21:34:33 +08:00
|
|
|
switch (value.type()) {
|
|
|
|
case query::TypedValue::Type::Null:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value();
|
2018-07-02 21:34:33 +08:00
|
|
|
case query::TypedValue::Type::Bool:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(value.ValueBool());
|
2018-07-02 21:34:33 +08:00
|
|
|
case query::TypedValue::Type::Int:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(value.ValueInt());
|
2018-07-02 21:34:33 +08:00
|
|
|
case query::TypedValue::Type::Double:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(value.ValueDouble());
|
2018-07-02 21:34:33 +08:00
|
|
|
case query::TypedValue::Type::String:
|
2019-05-30 18:30:36 +08:00
|
|
|
return Value(std::string(value.ValueString()));
|
2018-07-02 21:34:33 +08:00
|
|
|
case query::TypedValue::Type::List: {
|
2018-07-24 21:11:18 +08:00
|
|
|
std::vector<Value> values;
|
2018-07-02 21:34:33 +08:00
|
|
|
values.reserve(value.ValueList().size());
|
|
|
|
for (const auto &v : value.ValueList()) {
|
2019-09-05 16:25:27 +08:00
|
|
|
auto maybe_value = ToBoltValue(v, db, view);
|
|
|
|
if (maybe_value.HasError()) return maybe_value.GetError();
|
|
|
|
values.emplace_back(std::move(*maybe_value));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2019-09-02 21:09:25 +08:00
|
|
|
return Value(std::move(values));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
case query::TypedValue::Type::Map: {
|
2018-07-24 21:11:18 +08:00
|
|
|
std::map<std::string, Value> map;
|
2018-07-02 21:34:33 +08:00
|
|
|
for (const auto &kv : value.ValueMap()) {
|
2019-09-05 16:25:27 +08:00
|
|
|
auto maybe_value = ToBoltValue(kv.second, db, view);
|
|
|
|
if (maybe_value.HasError()) return maybe_value.GetError();
|
|
|
|
map.emplace(kv.first, std::move(*maybe_value));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2019-09-02 21:09:25 +08:00
|
|
|
return Value(std::move(map));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
case query::TypedValue::Type::Vertex:
|
2019-09-05 16:25:27 +08:00
|
|
|
{
|
|
|
|
auto maybe_vertex = ToBoltVertex(value.ValueVertex(), db, view);
|
|
|
|
if (maybe_vertex.HasError()) return maybe_vertex.GetError();
|
|
|
|
return Value(std::move(*maybe_vertex));
|
|
|
|
}
|
2018-07-02 21:34:33 +08:00
|
|
|
case query::TypedValue::Type::Edge:
|
2019-09-05 16:25:27 +08:00
|
|
|
{
|
|
|
|
auto maybe_edge = ToBoltEdge(value.ValueEdge(), db, view);
|
|
|
|
if (maybe_edge.HasError()) return maybe_edge.GetError();
|
|
|
|
return Value(std::move(*maybe_edge));
|
|
|
|
}
|
2018-07-02 21:34:33 +08:00
|
|
|
case query::TypedValue::Type::Path:
|
2019-09-05 16:25:27 +08:00
|
|
|
{
|
|
|
|
auto maybe_path = ToBoltPath(value.ValuePath(), db, view);
|
|
|
|
if (maybe_path.HasError()) return maybe_path.GetError();
|
|
|
|
return Value(std::move(*maybe_path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
storage::Result<communication::bolt::Vertex> ToBoltVertex(
|
|
|
|
const storage::VertexAccessor &vertex, const storage::Storage &db,
|
|
|
|
storage::View view) {
|
|
|
|
auto id = communication::bolt::Id::FromUint(vertex.Gid().AsUint());
|
|
|
|
auto maybe_labels = vertex.Labels(view);
|
|
|
|
if (maybe_labels.HasError()) return maybe_labels.GetError();
|
|
|
|
std::vector<std::string> labels;
|
|
|
|
labels.reserve(maybe_labels->size());
|
|
|
|
for (const auto &label : *maybe_labels) {
|
|
|
|
labels.push_back(db.LabelToName(label));
|
|
|
|
}
|
|
|
|
auto maybe_properties = vertex.Properties(view);
|
|
|
|
if (maybe_properties.HasError()) return maybe_properties.GetError();
|
|
|
|
std::map<std::string, Value> properties;
|
|
|
|
for (const auto &prop : *maybe_properties) {
|
|
|
|
properties[db.PropertyToName(prop.first)] = ToBoltValue(prop.second);
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2019-09-05 16:25:27 +08:00
|
|
|
return communication::bolt::Vertex{id, labels, properties};
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
|
2019-09-05 16:25:27 +08:00
|
|
|
storage::Result<communication::bolt::Edge> ToBoltEdge(
|
|
|
|
const storage::EdgeAccessor &edge, const storage::Storage &db,
|
|
|
|
storage::View view) {
|
|
|
|
auto id = communication::bolt::Id::FromUint(edge.Gid().AsUint());
|
|
|
|
auto from =
|
|
|
|
communication::bolt::Id::FromUint(edge.FromVertex().Gid().AsUint());
|
|
|
|
auto to = communication::bolt::Id::FromUint(edge.ToVertex().Gid().AsUint());
|
|
|
|
auto type = db.EdgeTypeToName(edge.EdgeType());
|
|
|
|
auto maybe_properties = edge.Properties(view);
|
|
|
|
if (maybe_properties.HasError()) return maybe_properties.GetError();
|
|
|
|
std::map<std::string, Value> properties;
|
|
|
|
for (const auto &prop : *maybe_properties) {
|
|
|
|
properties[db.PropertyToName(prop.first)] = ToBoltValue(prop.second);
|
|
|
|
}
|
|
|
|
return communication::bolt::Edge{id, from, to, type, properties};
|
|
|
|
}
|
2018-07-02 21:34:33 +08:00
|
|
|
|
2019-09-05 16:25:27 +08:00
|
|
|
storage::Result<communication::bolt::Path> ToBoltPath(
|
|
|
|
const query::Path &path, const storage::Storage &db, storage::View view) {
|
2018-07-24 21:11:18 +08:00
|
|
|
std::vector<communication::bolt::Vertex> vertices;
|
2018-07-02 21:34:33 +08:00
|
|
|
vertices.reserve(path.vertices().size());
|
|
|
|
for (const auto &v : path.vertices()) {
|
2019-09-05 16:25:27 +08:00
|
|
|
auto maybe_vertex = ToBoltVertex(v, db, view);
|
|
|
|
if (maybe_vertex.HasError()) return maybe_vertex.GetError();
|
|
|
|
vertices.emplace_back(std::move(*maybe_vertex));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
std::vector<communication::bolt::Edge> edges;
|
2018-07-02 21:34:33 +08:00
|
|
|
edges.reserve(path.edges().size());
|
|
|
|
for (const auto &e : path.edges()) {
|
2019-09-05 16:25:27 +08:00
|
|
|
auto maybe_edge = ToBoltEdge(e, db, view);
|
|
|
|
if (maybe_edge.HasError()) return maybe_edge.GetError();
|
|
|
|
edges.emplace_back(std::move(*maybe_edge));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
return communication::bolt::Path(vertices, edges);
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
|
2020-01-22 23:20:13 +08:00
|
|
|
storage::PropertyValue ToPropertyValue(const Value &value) {
|
2018-07-02 21:34:33 +08:00
|
|
|
switch (value.type()) {
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Null:
|
2020-01-22 23:20:13 +08:00
|
|
|
return storage::PropertyValue();
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Bool:
|
2020-01-22 23:20:13 +08:00
|
|
|
return storage::PropertyValue(value.ValueBool());
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Int:
|
2020-01-22 23:20:13 +08:00
|
|
|
return storage::PropertyValue(value.ValueInt());
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Double:
|
2020-01-22 23:20:13 +08:00
|
|
|
return storage::PropertyValue(value.ValueDouble());
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::String:
|
2020-01-22 23:20:13 +08:00
|
|
|
return storage::PropertyValue(value.ValueString());
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::List: {
|
2020-01-22 23:20:13 +08:00
|
|
|
std::vector<storage::PropertyValue> vec;
|
2018-07-02 21:34:33 +08:00
|
|
|
vec.reserve(value.ValueList().size());
|
|
|
|
for (const auto &value : value.ValueList())
|
|
|
|
vec.emplace_back(ToPropertyValue(value));
|
2020-01-22 23:20:13 +08:00
|
|
|
return storage::PropertyValue(std::move(vec));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Map: {
|
2020-01-22 23:20:13 +08:00
|
|
|
std::map<std::string, storage::PropertyValue> map;
|
2018-07-02 21:34:33 +08:00
|
|
|
for (const auto &kv : value.ValueMap())
|
|
|
|
map.emplace(kv.first, ToPropertyValue(kv.second));
|
2020-01-22 23:20:13 +08:00
|
|
|
return storage::PropertyValue(std::move(map));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Vertex:
|
|
|
|
case Value::Type::Edge:
|
|
|
|
case Value::Type::UnboundedEdge:
|
|
|
|
case Value::Type::Path:
|
|
|
|
throw communication::bolt::ValueException(
|
|
|
|
"Unsupported conversion from Value to PropertyValue");
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 23:20:13 +08:00
|
|
|
Value ToBoltValue(const storage::PropertyValue &value) {
|
2018-07-02 21:34:33 +08:00
|
|
|
switch (value.type()) {
|
2020-01-22 23:20:13 +08:00
|
|
|
case storage::PropertyValue::Type::Null:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value();
|
2020-01-22 23:20:13 +08:00
|
|
|
case storage::PropertyValue::Type::Bool:
|
2019-08-22 20:50:57 +08:00
|
|
|
return Value(value.ValueBool());
|
2020-01-22 23:20:13 +08:00
|
|
|
case storage::PropertyValue::Type::Int:
|
2019-08-22 20:50:57 +08:00
|
|
|
return Value(value.ValueInt());
|
2018-07-02 21:34:33 +08:00
|
|
|
break;
|
2020-01-22 23:20:13 +08:00
|
|
|
case storage::PropertyValue::Type::Double:
|
2019-08-22 20:50:57 +08:00
|
|
|
return Value(value.ValueDouble());
|
2020-01-22 23:20:13 +08:00
|
|
|
case storage::PropertyValue::Type::String:
|
2019-08-22 20:50:57 +08:00
|
|
|
return Value(value.ValueString());
|
2020-01-22 23:20:13 +08:00
|
|
|
case storage::PropertyValue::Type::List: {
|
2019-08-22 20:50:57 +08:00
|
|
|
const auto &values = value.ValueList();
|
2018-07-24 21:11:18 +08:00
|
|
|
std::vector<Value> vec;
|
2018-07-02 21:34:33 +08:00
|
|
|
vec.reserve(values.size());
|
|
|
|
for (const auto &v : values) {
|
2018-07-24 21:11:18 +08:00
|
|
|
vec.push_back(ToBoltValue(v));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2019-09-02 21:09:25 +08:00
|
|
|
return Value(std::move(vec));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2020-01-22 23:20:13 +08:00
|
|
|
case storage::PropertyValue::Type::Map: {
|
2019-08-22 20:50:57 +08:00
|
|
|
const auto &map = value.ValueMap();
|
2018-07-24 21:11:18 +08:00
|
|
|
std::map<std::string, Value> dv_map;
|
2018-07-02 21:34:33 +08:00
|
|
|
for (const auto &kv : map) {
|
2018-07-24 21:11:18 +08:00
|
|
|
dv_map.emplace(kv.first, ToBoltValue(kv.second));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2019-09-02 21:09:25 +08:00
|
|
|
return Value(std::move(dv_map));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Extract communication to static library
Summary:
Session specifics have been move out of the Bolt `executing` state, and
are accessed via pure virtual Session type. Our server is templated on
the session and we are setting the concrete type, so there should be no
virtual call overhead. Abstract Session is used to indicate the
interface, this could have also been templated, but the explicit
interface definition makes it clearer.
Specific session implementation for running Memgraph is now implemented
in memgraph_bolt, which instantiates the concrete session type. This may
not be 100% appropriate place, but Memgraph specific session isn't
needed anywhere else.
Bolt/communication tests now use a dummy session and depend only on
communication, which significantly improves test run times.
All these changes make the communication a library which doesn't depend
on storage nor the database. Only shared connection points, which aren't
part of the base communication library are:
* glue/conversion -- which converts between storage and bolt types, and
* communication/result_stream_faker -- templated, but used in tests and query/repl
Depends on D1453
Reviewers: mferencevic, buda, mtomic, msantl
Reviewed By: mferencevic, mtomic
Subscribers: pullbot
Differential Revision: https://phabricator.memgraph.io/D1456
2018-07-10 22:18:19 +08:00
|
|
|
} // namespace glue
|