Clean utils folder

Summary: The goal is to standardize utils folder.

Reviewers: teon.banek, mferencevic

Reviewed By: mferencevic

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1354
This commit is contained in:
Marko Budiselic 2018-04-06 09:31:00 +02:00
parent 4cbfc800b8
commit 20d667f8bf
11 changed files with 82 additions and 75 deletions

View File

@ -102,11 +102,11 @@ class Decoder {
return ReadEdge(marker, data);
default:
if ((value & 0xF0) == underlying_cast(Marker::TinyString)) {
if ((value & 0xF0) == utils::UnderlyingCast(Marker::TinyString)) {
return ReadString(marker, data);
} else if ((value & 0xF0) == underlying_cast(Marker::TinyList)) {
} else if ((value & 0xF0) == utils::UnderlyingCast(Marker::TinyList)) {
return ReadList(marker, data);
} else if ((value & 0xF0) == underlying_cast(Marker::TinyMap)) {
} else if ((value & 0xF0) == utils::UnderlyingCast(Marker::TinyMap)) {
return ReadMap(marker, data);
} else {
return ReadInt(marker, data);
@ -187,7 +187,7 @@ class Decoder {
}
bool ReadInt(const Marker &marker, DecodedValue *data) {
uint8_t value = underlying_cast(marker);
uint8_t value = utils::UnderlyingCast(marker);
int64_t ret;
VLOG(20) << "[ReadInt] Start";
if (value >= 240 || value <= 127) {
@ -227,7 +227,7 @@ class Decoder {
ret = bswap(ret);
} else {
DLOG(WARNING) << "[ReadInt] Received invalid marker "
<< underlying_cast(marker);
<< utils::UnderlyingCast(marker);
return false;
}
*data = DecodedValue(ret);
@ -253,8 +253,8 @@ class Decoder {
}
int64_t ReadTypeSize(const Marker &marker, const uint8_t type) {
uint8_t value = underlying_cast(marker);
if ((value & 0xF0) == underlying_cast(MarkerTiny[type])) {
uint8_t value = utils::UnderlyingCast(marker);
if ((value & 0xF0) == utils::UnderlyingCast(MarkerTiny[type])) {
VLOG(20) << "[ReadTypeSize] Found a TinyType";
return value & 0x0F;
} else if (marker == Marker8[type]) {
@ -285,7 +285,7 @@ class Decoder {
return tmp;
} else {
DLOG(WARNING) << "[ReadTypeSize] Received invalid marker "
<< underlying_cast(marker);
<< utils::UnderlyingCast(marker);
return -1;
}
}
@ -423,10 +423,10 @@ class Decoder {
// check header
if (marker != Marker::TinyStruct5) {
DLOG(WARNING) << "[ReadEdge] Received invalid marker "
<< (uint64_t)underlying_cast(marker);
<< (uint64_t)utils::UnderlyingCast(marker);
return false;
}
if (value != underlying_cast(Signature::Relationship)) {
if (value != utils::UnderlyingCast(Signature::Relationship)) {
DLOG(WARNING) << "[ReadEdge] Received invalid signature " << value;
return false;
}

View File

@ -38,8 +38,8 @@ class BaseEncoder : public PrimitiveEncoder<Buffer> {
}
void WriteVertex(const VertexAccessor &vertex) {
this->WriteRAW(underlying_cast(Marker::TinyStruct) + 3);
this->WriteRAW(underlying_cast(Signature::Node));
this->WriteRAW(utils::UnderlyingCast(Marker::TinyStruct) + 3);
this->WriteRAW(utils::UnderlyingCast(Signature::Node));
WriteUInt(vertex.gid());
// write labels
@ -58,9 +58,10 @@ class BaseEncoder : public PrimitiveEncoder<Buffer> {
}
void WriteEdge(const EdgeAccessor &edge, bool unbound = false) {
this->WriteRAW(underlying_cast(Marker::TinyStruct) + (unbound ? 3 : 5));
this->WriteRAW(underlying_cast(unbound ? Signature::UnboundRelationship
: Signature::Relationship));
this->WriteRAW(utils::UnderlyingCast(Marker::TinyStruct) +
(unbound ? 3 : 5));
this->WriteRAW(utils::UnderlyingCast(
unbound ? Signature::UnboundRelationship : Signature::Relationship));
WriteUInt(edge.gid());
if (!unbound) {
@ -113,8 +114,8 @@ class BaseEncoder : public PrimitiveEncoder<Buffer> {
}
// Write data.
this->WriteRAW(underlying_cast(Marker::TinyStruct) + 3);
this->WriteRAW(underlying_cast(Signature::Path));
this->WriteRAW(utils::UnderlyingCast(Marker::TinyStruct) + 3);
this->WriteRAW(utils::UnderlyingCast(Signature::Path));
this->WriteTypeSize(vertices.size(), MarkerList);
for (auto &v : vertices) WriteVertex(v);
this->WriteTypeSize(edges.size(), MarkerList);

View File

@ -40,8 +40,8 @@ class ClientEncoder : private BaseEncoder<Buffer> {
*/
bool MessageInit(const std::string client_name,
const std::map<std::string, query::TypedValue> &auth_token) {
WriteRAW(underlying_cast(Marker::TinyStruct2));
WriteRAW(underlying_cast(Signature::Init));
WriteRAW(utils::UnderlyingCast(Marker::TinyStruct2));
WriteRAW(utils::UnderlyingCast(Signature::Init));
WriteString(client_name);
WriteMap(auth_token);
return buffer_.Flush();
@ -64,8 +64,8 @@ class ClientEncoder : private BaseEncoder<Buffer> {
bool MessageRun(const std::string statement,
const std::map<std::string, query::TypedValue> &parameters,
bool flush = true) {
WriteRAW(underlying_cast(Marker::TinyStruct2));
WriteRAW(underlying_cast(Signature::Run));
WriteRAW(utils::UnderlyingCast(Marker::TinyStruct2));
WriteRAW(utils::UnderlyingCast(Signature::Run));
WriteString(statement);
WriteMap(parameters);
if (flush) {
@ -88,8 +88,8 @@ class ClientEncoder : private BaseEncoder<Buffer> {
* when flushing, false otherwise
*/
bool MessageDiscardAll() {
WriteRAW(underlying_cast(Marker::TinyStruct));
WriteRAW(underlying_cast(Signature::DiscardAll));
WriteRAW(utils::UnderlyingCast(Marker::TinyStruct));
WriteRAW(utils::UnderlyingCast(Signature::DiscardAll));
return buffer_.Flush();
}
@ -104,8 +104,8 @@ class ClientEncoder : private BaseEncoder<Buffer> {
* when flushing, false otherwise
*/
bool MessagePullAll() {
WriteRAW(underlying_cast(Marker::TinyStruct));
WriteRAW(underlying_cast(Signature::PullAll));
WriteRAW(utils::UnderlyingCast(Marker::TinyStruct));
WriteRAW(utils::UnderlyingCast(Signature::PullAll));
return buffer_.Flush();
}
@ -120,8 +120,8 @@ class ClientEncoder : private BaseEncoder<Buffer> {
* when flushing, false otherwise
*/
bool MessageAckFailure() {
WriteRAW(underlying_cast(Marker::TinyStruct));
WriteRAW(underlying_cast(Signature::AckFailure));
WriteRAW(utils::UnderlyingCast(Marker::TinyStruct));
WriteRAW(utils::UnderlyingCast(Signature::AckFailure));
return buffer_.Flush();
}
@ -136,8 +136,8 @@ class ClientEncoder : private BaseEncoder<Buffer> {
* when flushing, false otherwise
*/
bool MessageReset() {
WriteRAW(underlying_cast(Marker::TinyStruct));
WriteRAW(underlying_cast(Signature::Reset));
WriteRAW(utils::UnderlyingCast(Marker::TinyStruct));
WriteRAW(utils::UnderlyingCast(Signature::Reset));
return buffer_.Flush();
}
};

View File

@ -37,8 +37,8 @@ class Encoder : private BaseEncoder<Buffer> {
* @param values the fields list object that should be sent
*/
void MessageRecord(const std::vector<query::TypedValue> &values) {
WriteRAW(underlying_cast(Marker::TinyStruct1));
WriteRAW(underlying_cast(Signature::Record));
WriteRAW(utils::UnderlyingCast(Marker::TinyStruct1));
WriteRAW(utils::UnderlyingCast(Signature::Record));
WriteList(values);
buffer_.Chunk();
}
@ -58,8 +58,8 @@ class Encoder : private BaseEncoder<Buffer> {
*/
bool MessageSuccess(const std::map<std::string, query::TypedValue> &metadata,
bool flush = true) {
WriteRAW(underlying_cast(Marker::TinyStruct1));
WriteRAW(underlying_cast(Signature::Success));
WriteRAW(utils::UnderlyingCast(Marker::TinyStruct1));
WriteRAW(utils::UnderlyingCast(Signature::Success));
WriteMap(metadata);
if (flush) {
return buffer_.Flush();
@ -97,8 +97,8 @@ class Encoder : private BaseEncoder<Buffer> {
*/
bool MessageFailure(
const std::map<std::string, query::TypedValue> &metadata) {
WriteRAW(underlying_cast(Marker::TinyStruct1));
WriteRAW(underlying_cast(Signature::Failure));
WriteRAW(utils::UnderlyingCast(Marker::TinyStruct1));
WriteRAW(utils::UnderlyingCast(Signature::Failure));
WriteMap(metadata);
return buffer_.Flush();
}
@ -117,8 +117,8 @@ class Encoder : private BaseEncoder<Buffer> {
*/
bool MessageIgnored(
const std::map<std::string, query::TypedValue> &metadata) {
WriteRAW(underlying_cast(Marker::TinyStruct1));
WriteRAW(underlying_cast(Signature::Ignored));
WriteRAW(utils::UnderlyingCast(Marker::TinyStruct1));
WriteRAW(utils::UnderlyingCast(Signature::Ignored));
WriteMap(metadata);
return buffer_.Flush();
}
@ -132,8 +132,8 @@ class Encoder : private BaseEncoder<Buffer> {
* false otherwise
*/
bool MessageIgnored() {
WriteRAW(underlying_cast(Marker::TinyStruct));
WriteRAW(underlying_cast(Signature::Ignored));
WriteRAW(utils::UnderlyingCast(Marker::TinyStruct));
WriteRAW(utils::UnderlyingCast(Signature::Ignored));
return buffer_.Flush();
}
};

View File

@ -36,35 +36,35 @@ class PrimitiveEncoder {
WriteRAW(reinterpret_cast<const uint8_t *>(&value), sizeof(value));
}
void WriteNull() { WriteRAW(underlying_cast(Marker::Null)); }
void WriteNull() { WriteRAW(utils::UnderlyingCast(Marker::Null)); }
void WriteBool(const bool &value) {
if (value)
WriteRAW(underlying_cast(Marker::True));
WriteRAW(utils::UnderlyingCast(Marker::True));
else
WriteRAW(underlying_cast(Marker::False));
WriteRAW(utils::UnderlyingCast(Marker::False));
}
void WriteInt(const int64_t &value) {
if (value >= -16L && value < 128L) {
WriteRAW(static_cast<uint8_t>(value));
} else if (value >= -128L && value < -16L) {
WriteRAW(underlying_cast(Marker::Int8));
WriteRAW(utils::UnderlyingCast(Marker::Int8));
WriteRAW(static_cast<uint8_t>(value));
} else if (value >= -32768L && value < 32768L) {
WriteRAW(underlying_cast(Marker::Int16));
WriteRAW(utils::UnderlyingCast(Marker::Int16));
WriteValue(static_cast<int16_t>(value));
} else if (value >= -2147483648L && value < 2147483648L) {
WriteRAW(underlying_cast(Marker::Int32));
WriteRAW(utils::UnderlyingCast(Marker::Int32));
WriteValue(static_cast<int32_t>(value));
} else {
WriteRAW(underlying_cast(Marker::Int64));
WriteRAW(utils::UnderlyingCast(Marker::Int64));
WriteValue(value);
}
}
void WriteDouble(const double &value) {
WriteRAW(underlying_cast(Marker::Float64));
WriteRAW(utils::UnderlyingCast(Marker::Float64));
WriteValue(*reinterpret_cast<const int64_t *>(&value));
}
@ -72,18 +72,18 @@ class PrimitiveEncoder {
if (size <= 15) {
uint8_t len = size;
len &= 0x0F;
WriteRAW(underlying_cast(MarkerTiny[typ]) + len);
WriteRAW(utils::UnderlyingCast(MarkerTiny[typ]) + len);
} else if (size <= 255) {
uint8_t len = size;
WriteRAW(underlying_cast(Marker8[typ]));
WriteRAW(utils::UnderlyingCast(Marker8[typ]));
WriteRAW(len);
} else if (size <= 65535) {
uint16_t len = size;
WriteRAW(underlying_cast(Marker16[typ]));
WriteRAW(utils::UnderlyingCast(Marker16[typ]));
WriteValue(len);
} else {
uint32_t len = size;
WriteRAW(underlying_cast(Marker32[typ]));
WriteRAW(utils::UnderlyingCast(Marker32[typ]));
WriteValue(len);
}
}

View File

@ -26,7 +26,7 @@ State StateErrorRun(TSession &session, State state) {
}
DLOG(INFO) << fmt::format("Message signature is: 0x{:02X}",
underlying_cast(signature));
utils::UnderlyingCast(signature));
// Clear the data buffer if it has any leftover data.
session.encoder_buffer_.Clear();
@ -58,11 +58,11 @@ State StateErrorRun(TSession &session, State state) {
LOG(FATAL) << "Shouldn't happen";
}
} else {
uint8_t value = underlying_cast(marker);
uint8_t value = utils::UnderlyingCast(marker);
// All bolt client messages have less than 15 parameters so if we receive
// anything than a TinyStruct it's an error.
if ((value & 0xF0) != underlying_cast(Marker::TinyStruct)) {
if ((value & 0xF0) != utils::UnderlyingCast(Marker::TinyStruct)) {
DLOG(WARNING) << fmt::format(
"Expected TinyStruct marker, but received 0x{:02X}!", value);
return State::Close;

View File

@ -23,7 +23,7 @@ State HandleRun(TSession &session, State state, Marker marker) {
if (marker != Marker::TinyStruct2) {
DLOG(WARNING) << fmt::format(
"Expected TinyStruct2 marker, but received 0x{:02X}!",
underlying_cast(marker));
utils::UnderlyingCast(marker));
return State::Close;
}
@ -217,7 +217,7 @@ State HandlePullAll(Session &session, State state, Marker marker) {
if (marker != Marker::TinyStruct) {
DLOG(WARNING) << fmt::format(
"Expected TinyStruct marker, but received 0x{:02X}!",
underlying_cast(marker));
utils::UnderlyingCast(marker));
return State::Close;
}
@ -241,7 +241,7 @@ State HandleDiscardAll(Session &session, State state, Marker marker) {
if (marker != Marker::TinyStruct) {
DLOG(WARNING) << fmt::format(
"Expected TinyStruct marker, but received 0x{:02X}!",
underlying_cast(marker));
utils::UnderlyingCast(marker));
return State::Close;
}
@ -273,7 +273,7 @@ State HandleReset(Session &session, State, Marker marker) {
if (marker != Marker::TinyStruct) {
DLOG(WARNING) << fmt::format(
"Expected TinyStruct marker, but received 0x{:02X}!",
underlying_cast(marker));
utils::UnderlyingCast(marker));
return State::Close;
}
// clear all pending data and send a success message
@ -313,7 +313,7 @@ State StateExecutingRun(Session &session, State state) {
return HandleReset(session, state, marker);
} else {
DLOG(WARNING) << fmt::format("Unrecognized signature recieved (0x{:02X})!",
underlying_cast(signature));
utils::UnderlyingCast(signature));
return State::Close;
}
}

View File

@ -32,13 +32,13 @@ State StateInitRun(Session &session) {
if (UNLIKELY(signature != Signature::Init)) {
DLOG(WARNING) << fmt::format(
"Expected Init signature, but received 0x{:02X}!",
underlying_cast(signature));
utils::UnderlyingCast(signature));
return State::Close;
}
if (UNLIKELY(marker != Marker::TinyStruct2)) {
DLOG(WARNING) << fmt::format(
"Expected TinyStruct2 marker, but received 0x{:02X}!",
underlying_cast(marker));
utils::UnderlyingCast(marker));
DLOG(WARNING) << "The client sent malformed data, but we are continuing "
"because the official Neo4j Java driver sends malformed "
"data. D'oh!";

View File

@ -2,7 +2,11 @@
#include <type_traits>
namespace utils {
template <typename T>
constexpr typename std::underlying_type<T>::type underlying_cast(T e) {
constexpr typename std::underlying_type<T>::type UnderlyingCast(T e) {
return static_cast<typename std::underlying_type<T>::type>(e);
}
} // namespace utils

View File

@ -59,9 +59,10 @@ class SnapshotWriter {
void WriteNode(const Node &node,
const std::unordered_map<gid::Gid, Edge> &edges) {
encoder_.WriteRAW(underlying_cast(communication::bolt::Marker::TinyStruct) +
3);
encoder_.WriteRAW(underlying_cast(communication::bolt::Signature::Node));
encoder_.WriteRAW(
utils::UnderlyingCast(communication::bolt::Marker::TinyStruct) + 3);
encoder_.WriteRAW(
utils::UnderlyingCast(communication::bolt::Signature::Node));
encoder_.WriteInt(node.gid);
WriteList(node.labels);
@ -81,10 +82,10 @@ class SnapshotWriter {
}
void WriteEdge(const Edge &edge) {
encoder_.WriteRAW(underlying_cast(communication::bolt::Marker::TinyStruct) +
5);
encoder_.WriteRAW(
underlying_cast(communication::bolt::Signature::Relationship));
utils::UnderlyingCast(communication::bolt::Marker::TinyStruct) + 5);
encoder_.WriteRAW(
utils::UnderlyingCast(communication::bolt::Signature::Relationship));
encoder_.WriteInt(edge.gid);
encoder_.WriteInt(edge.from);
encoder_.WriteInt(edge.to);

View File

@ -162,9 +162,9 @@ std::vector<Field> ReadHeader(std::istream &stream) {
auto name_and_type = utils::Split(value, ":");
CHECK(name_and_type.size() == 1U || name_and_type.size() == 2U)
<< fmt::format(
"\nExpected a name and optionally a type, got '{}'.\nDid you "
"specify a correct CSV delimiter?",
value);
"\nExpected a name and optionally a type, got '{}'.\nDid you "
"specify a correct CSV delimiter?",
value);
auto name = name_and_type[0];
// When type is missing, default is string.
std::string type("string");
@ -390,8 +390,9 @@ void Convert(const std::vector<std::string> &nodes,
auto &vertex = vertex_pair.second;
// write node
encoder.WriteRAW(
underlying_cast(communication::bolt::Marker::TinyStruct) + 3);
encoder.WriteRAW(underlying_cast(communication::bolt::Signature::Node));
utils::UnderlyingCast(communication::bolt::Marker::TinyStruct) + 3);
encoder.WriteRAW(
utils::UnderlyingCast(communication::bolt::Signature::Node));
encoder.WriteInt(vertex.gid);
auto &labels = vertex.labels;
@ -420,9 +421,9 @@ void Convert(const std::vector<std::string> &nodes,
auto &edge = edge_pair.second;
// write relationship
encoder.WriteRAW(
underlying_cast(communication::bolt::Marker::TinyStruct) + 5);
utils::UnderlyingCast(communication::bolt::Marker::TinyStruct) + 5);
encoder.WriteRAW(
underlying_cast(communication::bolt::Signature::Relationship));
utils::UnderlyingCast(communication::bolt::Signature::Relationship));
encoder.WriteInt(edge.id);
encoder.WriteInt(edge.from);
encoder.WriteInt(edge.to);