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>
|
|
|
|
|
|
|
|
#include "database/graph_db_accessor.hpp"
|
|
|
|
|
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:
|
2018-07-02 21:34:33 +08:00
|
|
|
return query::TypedValue::Null;
|
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));
|
|
|
|
return query::TypedValue(list);
|
|
|
|
}
|
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));
|
|
|
|
return query::TypedValue(map);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:11:18 +08:00
|
|
|
Value ToBoltValue(const query::TypedValue &value) {
|
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:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(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()) {
|
2018-07-24 21:11:18 +08:00
|
|
|
values.push_back(ToBoltValue(v));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(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()) {
|
2018-07-24 21:11:18 +08:00
|
|
|
map.emplace(kv.first, ToBoltValue(kv.second));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(map);
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
case query::TypedValue::Type::Vertex:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(ToBoltVertex(value.ValueVertex()));
|
2018-07-02 21:34:33 +08:00
|
|
|
case query::TypedValue::Type::Edge:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(ToBoltEdge(value.ValueEdge()));
|
2018-07-02 21:34:33 +08:00
|
|
|
case query::TypedValue::Type::Path:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(ToBoltPath(value.ValuePath()));
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:11:18 +08:00
|
|
|
communication::bolt::Vertex ToBoltVertex(const VertexAccessor &vertex) {
|
2018-07-02 21:34:33 +08:00
|
|
|
auto id = communication::bolt::Id::FromUint(vertex.gid());
|
|
|
|
std::vector<std::string> labels;
|
|
|
|
labels.reserve(vertex.labels().size());
|
|
|
|
for (const auto &label : vertex.labels()) {
|
|
|
|
labels.push_back(vertex.db_accessor().LabelName(label));
|
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
std::map<std::string, Value> properties;
|
2018-07-02 21:34:33 +08:00
|
|
|
for (const auto &prop : vertex.Properties()) {
|
|
|
|
properties[vertex.db_accessor().PropertyName(prop.first)] =
|
2018-07-24 21:11:18 +08:00
|
|
|
ToBoltValue(prop.second);
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
return communication::bolt::Vertex{id, labels, properties};
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
|
2018-07-24 21:11:18 +08:00
|
|
|
communication::bolt::Edge ToBoltEdge(const EdgeAccessor &edge) {
|
2018-07-02 21:34:33 +08:00
|
|
|
auto id = communication::bolt::Id::FromUint(edge.gid());
|
|
|
|
auto from = communication::bolt::Id::FromUint(edge.from().gid());
|
|
|
|
auto to = communication::bolt::Id::FromUint(edge.to().gid());
|
|
|
|
auto type = edge.db_accessor().EdgeTypeName(edge.EdgeType());
|
2018-07-24 21:11:18 +08:00
|
|
|
std::map<std::string, Value> properties;
|
2018-07-02 21:34:33 +08:00
|
|
|
for (const auto &prop : edge.Properties()) {
|
|
|
|
properties[edge.db_accessor().PropertyName(prop.first)] =
|
2018-07-24 21:11:18 +08:00
|
|
|
ToBoltValue(prop.second);
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
return communication::bolt::Edge{id, from, to, type, properties};
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
|
2018-07-24 21:11:18 +08:00
|
|
|
communication::bolt::Path ToBoltPath(const query::Path &path) {
|
|
|
|
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()) {
|
2018-07-24 21:11:18 +08:00
|
|
|
vertices.push_back(ToBoltVertex(v));
|
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()) {
|
2018-07-24 21:11:18 +08:00
|
|
|
edges.push_back(ToBoltEdge(e));
|
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
|
|
|
}
|
|
|
|
|
2018-07-24 21:11:18 +08:00
|
|
|
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:
|
2018-07-02 21:34:33 +08:00
|
|
|
return PropertyValue::Null;
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Bool:
|
2018-07-02 21:34:33 +08:00
|
|
|
return PropertyValue(value.ValueBool());
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Int:
|
2018-07-02 21:34:33 +08:00
|
|
|
return PropertyValue(value.ValueInt());
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Double:
|
2018-07-02 21:34:33 +08:00
|
|
|
return PropertyValue(value.ValueDouble());
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::String:
|
2018-07-02 21:34:33 +08:00
|
|
|
return PropertyValue(value.ValueString());
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::List: {
|
2018-07-02 21:34:33 +08:00
|
|
|
std::vector<PropertyValue> vec;
|
|
|
|
vec.reserve(value.ValueList().size());
|
|
|
|
for (const auto &value : value.ValueList())
|
|
|
|
vec.emplace_back(ToPropertyValue(value));
|
|
|
|
return PropertyValue(std::move(vec));
|
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
case Value::Type::Map: {
|
2018-07-02 21:34:33 +08:00
|
|
|
std::map<std::string, PropertyValue> map;
|
|
|
|
for (const auto &kv : value.ValueMap())
|
|
|
|
map.emplace(kv.first, ToPropertyValue(kv.second));
|
|
|
|
return PropertyValue(std::move(map));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:11:18 +08:00
|
|
|
Value ToBoltValue(const PropertyValue &value) {
|
2018-07-02 21:34:33 +08:00
|
|
|
switch (value.type()) {
|
|
|
|
case PropertyValue::Type::Null:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value();
|
2018-07-02 21:34:33 +08:00
|
|
|
case PropertyValue::Type::Bool:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(value.Value<bool>());
|
2018-07-02 21:34:33 +08:00
|
|
|
case PropertyValue::Type::Int:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(value.Value<int64_t>());
|
2018-07-02 21:34:33 +08:00
|
|
|
break;
|
|
|
|
case PropertyValue::Type::Double:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(value.Value<double>());
|
2018-07-02 21:34:33 +08:00
|
|
|
case PropertyValue::Type::String:
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(value.Value<std::string>());
|
2018-07-02 21:34:33 +08:00
|
|
|
case PropertyValue::Type::List: {
|
|
|
|
const auto &values = value.Value<std::vector<PropertyValue>>();
|
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
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(vec);
|
2018-07-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
case PropertyValue::Type::Map: {
|
|
|
|
const auto &map = value.Value<std::map<std::string, PropertyValue>>();
|
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
|
|
|
}
|
2018-07-24 21:11:18 +08:00
|
|
|
return Value(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
|