2016-08-08 04:19:04 +08:00
|
|
|
#pragma once
|
|
|
|
|
2016-08-11 11:47:30 +08:00
|
|
|
#include "communication/bolt/v1/packing/codes.hpp"
|
2016-08-13 06:01:39 +08:00
|
|
|
#include "communication/bolt/v1/transport/bolt_encoder.hpp"
|
2016-08-08 04:19:04 +08:00
|
|
|
|
|
|
|
#include "storage/edge_accessor.hpp"
|
2016-08-13 06:01:39 +08:00
|
|
|
#include "storage/vertex_accessor.hpp"
|
2016-08-08 04:19:04 +08:00
|
|
|
|
|
|
|
#include "storage/model/properties/all.hpp"
|
2016-08-13 06:01:39 +08:00
|
|
|
#include "storage/model/properties/properties.hpp"
|
2016-08-08 04:19:04 +08:00
|
|
|
|
|
|
|
namespace bolt
|
|
|
|
{
|
|
|
|
|
|
|
|
template <class Stream>
|
|
|
|
class BoltSerializer
|
|
|
|
{
|
|
|
|
friend class Property;
|
|
|
|
|
2016-08-11 11:47:30 +08:00
|
|
|
// TODO: here shoud be friend but it doesn't work
|
|
|
|
// template <class Handler>
|
|
|
|
// friend void accept(const Property &property, Handler &h);
|
|
|
|
|
2016-08-08 04:19:04 +08:00
|
|
|
public:
|
2016-08-13 06:01:39 +08:00
|
|
|
BoltSerializer(Stream &stream) : encoder(stream) {}
|
2016-08-08 04:19:04 +08:00
|
|
|
|
|
|
|
/* Serializes the vertex accessor into the packstream format
|
|
|
|
*
|
|
|
|
* struct[size = 3] Vertex [signature = 0x4E] {
|
|
|
|
* Integer node_id;
|
|
|
|
* List<String> labels;
|
|
|
|
* Map<String, Value> properties;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
2016-08-25 22:29:45 +08:00
|
|
|
void write(const VertexAccessor &vertex)
|
2016-08-08 04:19:04 +08:00
|
|
|
{
|
|
|
|
// write signatures for the node struct and node data type
|
|
|
|
encoder.write_struct_header(3);
|
|
|
|
encoder.write(underlying_cast(pack::Node));
|
|
|
|
|
|
|
|
// write the identifier for the node
|
|
|
|
encoder.write_integer(vertex.id());
|
|
|
|
|
|
|
|
// write the list of labels
|
|
|
|
auto labels = vertex.labels();
|
|
|
|
|
|
|
|
encoder.write_list_header(labels.size());
|
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
for (auto &label : labels)
|
2016-08-08 04:19:04 +08:00
|
|
|
encoder.write_string(label.get());
|
|
|
|
|
|
|
|
// write the property map
|
|
|
|
auto props = vertex.properties();
|
|
|
|
|
|
|
|
encoder.write_map_header(props.size());
|
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
for (auto &prop : props) {
|
2016-08-20 01:40:04 +08:00
|
|
|
write(prop.first.family_name());
|
2016-08-11 11:47:30 +08:00
|
|
|
write(*prop.second);
|
|
|
|
}
|
2016-08-08 04:19:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Serializes the vertex accessor into the packstream format
|
|
|
|
*
|
|
|
|
* struct[size = 5] Edge [signature = 0x52] {
|
|
|
|
* Integer edge_id;
|
|
|
|
* Integer start_node_id;
|
|
|
|
* Integer end_node_id;
|
|
|
|
* String type;
|
|
|
|
* Map<String, Value> properties;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
2016-08-29 04:10:13 +08:00
|
|
|
void write(const EdgeAccessor &edge);
|
2016-08-08 04:19:04 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
void write(const Property &prop) { accept(prop, *this); }
|
2016-08-08 04:19:04 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
void write_null() { encoder.write_null(); }
|
2016-08-08 04:19:04 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
void write(const Bool &prop) { encoder.write_bool(prop.value()); }
|
2016-08-08 04:19:04 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
void write(const Float &prop) { encoder.write_double(prop.value); }
|
2016-08-08 04:19:04 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
void write(const Double &prop) { encoder.write_double(prop.value); }
|
2016-08-08 04:19:04 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
void write(const Int32 &prop) { encoder.write_integer(prop.value); }
|
2016-08-08 04:19:04 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
void write(const Int64 &prop) { encoder.write_integer(prop.value); }
|
2016-08-08 04:19:04 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
void write(const std::string &value) { encoder.write_string(value); }
|
2016-08-11 11:47:30 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
void write(const String &prop) { encoder.write_string(prop.value); }
|
2016-08-08 04:19:04 +08:00
|
|
|
|
|
|
|
template <class T>
|
2016-08-13 06:01:39 +08:00
|
|
|
void handle(const T &prop)
|
2016-08-08 04:19:04 +08:00
|
|
|
{
|
|
|
|
write(prop);
|
|
|
|
}
|
2016-08-11 11:47:30 +08:00
|
|
|
|
|
|
|
protected:
|
2016-08-13 06:01:39 +08:00
|
|
|
Stream &encoder;
|
2016-08-08 04:19:04 +08:00
|
|
|
};
|
|
|
|
}
|