2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Field.h>
|
2017-11-24 13:55:31 +00:00
|
|
|
#include <Common/FieldVisitors.h>
|
2015-10-12 07:05:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
inline void readBinary(Array & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
UInt8 type;
|
|
|
|
DB::readBinary(type, buf);
|
|
|
|
DB::readBinary(size, buf);
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t index = 0; index < size; ++index)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Field::Types::Null:
|
|
|
|
{
|
|
|
|
x.push_back(DB::Field());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::UInt64:
|
|
|
|
{
|
|
|
|
UInt64 value;
|
|
|
|
DB::readVarUInt(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
2017-07-04 10:42:53 +00:00
|
|
|
case Field::Types::UInt128:
|
|
|
|
{
|
|
|
|
UInt128 value;
|
|
|
|
DB::readBinary(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
case Field::Types::Int64:
|
|
|
|
{
|
|
|
|
Int64 value;
|
|
|
|
DB::readVarInt(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::Float64:
|
|
|
|
{
|
|
|
|
Float64 value;
|
|
|
|
DB::readFloatBinary(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::String:
|
|
|
|
{
|
|
|
|
std::string value;
|
|
|
|
DB::readStringBinary(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::Array:
|
|
|
|
{
|
|
|
|
Array value;
|
|
|
|
DB::readBinary(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::Tuple:
|
|
|
|
{
|
|
|
|
Tuple value;
|
|
|
|
DB::readBinary(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void writeBinary(const Array & x, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
UInt8 type = Field::Types::Null;
|
|
|
|
size_t size = x.size();
|
|
|
|
if (size)
|
|
|
|
type = x.front().getType();
|
|
|
|
DB::writeBinary(type, buf);
|
|
|
|
DB::writeBinary(size, buf);
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (Array::const_iterator it = x.begin(); it != x.end(); ++it)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Field::Types::Null: break;
|
|
|
|
case Field::Types::UInt64:
|
|
|
|
{
|
|
|
|
DB::writeVarUInt(get<UInt64>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
2017-07-04 10:42:53 +00:00
|
|
|
case Field::Types::UInt128:
|
|
|
|
{
|
|
|
|
DB::writeBinary(get<UInt128>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
case Field::Types::Int64:
|
|
|
|
{
|
|
|
|
DB::writeVarInt(get<Int64>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::Float64:
|
|
|
|
{
|
|
|
|
DB::writeFloatBinary(get<Float64>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::String:
|
|
|
|
{
|
|
|
|
DB::writeStringBinary(get<std::string>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::Array:
|
|
|
|
{
|
|
|
|
DB::writeBinary(get<Array>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::Tuple:
|
|
|
|
{
|
|
|
|
DB::writeBinary(get<Tuple>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void writeText(const Array & x, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
DB::String res = applyVisitor(DB::FieldVisitorToString(), DB::Field(x));
|
|
|
|
buf.write(res.data(), res.size());
|
|
|
|
}
|
2015-10-12 07:05:54 +00:00
|
|
|
}
|
2015-12-24 14:31:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
inline void readBinary(Tuple & x_def, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
auto & x = x_def.t;
|
|
|
|
size_t size;
|
|
|
|
DB::readBinary(size, buf);
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t index = 0; index < size; ++index)
|
|
|
|
{
|
|
|
|
UInt8 type;
|
|
|
|
DB::readBinary(type, buf);
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Field::Types::Null:
|
|
|
|
{
|
|
|
|
x.push_back(DB::Field());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::UInt64:
|
|
|
|
{
|
|
|
|
UInt64 value;
|
|
|
|
DB::readVarUInt(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
2017-07-04 10:42:53 +00:00
|
|
|
case Field::Types::UInt128:
|
|
|
|
{
|
|
|
|
UInt128 value;
|
|
|
|
DB::readBinary(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
case Field::Types::Int64:
|
|
|
|
{
|
|
|
|
Int64 value;
|
|
|
|
DB::readVarInt(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::Float64:
|
|
|
|
{
|
|
|
|
Float64 value;
|
|
|
|
DB::readFloatBinary(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::String:
|
|
|
|
{
|
|
|
|
std::string value;
|
|
|
|
DB::readStringBinary(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::Array:
|
|
|
|
{
|
|
|
|
Array value;
|
|
|
|
DB::readBinary(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::Tuple:
|
|
|
|
{
|
|
|
|
Tuple value;
|
|
|
|
DB::readBinary(value, buf);
|
|
|
|
x.push_back(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void writeBinary(const Tuple & x_def, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
auto & x = x_def.t;
|
|
|
|
const size_t size = x.size();
|
|
|
|
DB::writeBinary(size, buf);
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (auto it = x.begin(); it != x.end(); ++it)
|
|
|
|
{
|
|
|
|
const UInt8 type = it->getType();
|
|
|
|
DB::writeBinary(type, buf);
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Field::Types::Null: break;
|
|
|
|
case Field::Types::UInt64:
|
|
|
|
{
|
|
|
|
DB::writeVarUInt(get<UInt64>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
2017-07-04 10:42:53 +00:00
|
|
|
case Field::Types::UInt128:
|
|
|
|
{
|
|
|
|
DB::writeBinary(get<UInt128>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
case Field::Types::Int64:
|
|
|
|
{
|
|
|
|
DB::writeVarInt(get<Int64>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::Float64:
|
|
|
|
{
|
|
|
|
DB::writeFloatBinary(get<Float64>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::String:
|
|
|
|
{
|
|
|
|
DB::writeStringBinary(get<std::string>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::Array:
|
|
|
|
{
|
|
|
|
DB::writeBinary(get<Array>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Field::Types::Tuple:
|
|
|
|
{
|
|
|
|
DB::writeBinary(get<Tuple>(*it), buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void writeText(const Tuple & x, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
DB::String res = applyVisitor(DB::FieldVisitorToString(), DB::Field(x));
|
|
|
|
buf.write(res.data(), res.size());
|
|
|
|
}
|
2015-12-24 14:31:41 +00:00
|
|
|
}
|