Files
memgraph/src/communication/bolt/v1/encoder/result_stream.hpp
Matej Ferencevic f05fcd91c3 Refactored bolt session to use new decoder.
Summary:
Bolt buffer is now a template.

Communication worker now has a new interface.

Fixed network tests to use new interface.

Fixed bolt tests to use new interface.

Added more functions to bolt decoder.

Reviewers: dgleich, buda

Reviewed By: buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D256
2017-04-15 16:35:09 +02:00

78 lines
2.2 KiB
C++

#pragma once
#include "communication/bolt/v1/encoder/chunked_encoder_buffer.hpp"
#include "communication/bolt/v1/encoder/encoder.hpp"
#include "query/typed_value.hpp"
#include "logging/default.hpp"
namespace communication::bolt {
/**
* A high level API for streaming a Bolt response. Exposes
* functionalities used by the compiler and query plans (which
* should not use any lower level API).
*
* @tparam Encoder Encoder used.
*/
template <typename Encoder>
class ResultStream {
public:
ResultStream(Encoder &encoder) : encoder_(encoder) {}
/**
* Writes a header. Typically a header is something like:
* [ "Header1", "Header2", "Header3" ]
*
* @param fields the header fields that should be sent.
*/
void Header(const std::vector<std::string> &fields) {
std::vector<query::TypedValue> vec;
std::map<std::string, query::TypedValue> data;
for (auto &i : fields) vec.push_back(query::TypedValue(i));
data.insert(std::make_pair(std::string("fields"), query::TypedValue(vec)));
// this message shouldn't send directly to the client because if an error
// happened the client will receive two messages (success and failure)
// instead of only one
encoder_.MessageSuccess(data, false);
}
/**
* Writes a result. Typically a result is something like:
* [
* Value1,
* Value2,
* Value3
* ]
* NOTE: The result fields should be in the same ordering that the header
* fields were sent in.
*
* @param values the values that should be sent
*/
void Result(std::vector<query::TypedValue> &values) {
encoder_.MessageRecord(values);
}
/**
* Writes a summary. Typically a summary is something like:
* {
* "type" : "r" | "rw" | ...,
* "stats": {
* "nodes_created": 12,
* "nodes_deleted": 0
* }
* }
*
* @param summary the summary map object that should be sent
*/
void Summary(const std::map<std::string, query::TypedValue> &summary) {
// at this point message should not flush the socket so
// here is false because chunk has to be called instead of flush
encoder_.MessageSuccess(summary, false);
}
private:
Encoder &encoder_;
};
}