#pragma once #include #include #include #include #include #include #include #include namespace Coordination { using namespace DB; template requires is_arithmetic_v void write(T x, WriteBuffer & out) { writeBinaryBigEndian(x, out); } void write(OpNum x, WriteBuffer & out); void write(const std::string & s, WriteBuffer & out); void write(const ACL & acl, WriteBuffer & out); void write(const Stat & stat, WriteBuffer & out); void write(const Error & x, WriteBuffer & out); template void write(const std::array s, WriteBuffer & out) { write(int32_t(N), out); out.write(s.data(), N); } template void write(const std::vector & arr, WriteBuffer & out) { write(int32_t(arr.size()), out); for (const auto & elem : arr) write(elem, out); } template requires is_arithmetic_v size_t size(T x) { return sizeof(x); } size_t size(OpNum x); size_t size(const std::string & s); size_t size(const ACL & acl); size_t size(const Stat & stat); size_t size(const Error & x); template size_t size(const std::array) { return size(static_cast(N)) + N; } template size_t size(const std::vector & arr) { size_t total_size = size(static_cast(arr.size())); for (const auto & elem : arr) total_size += size(elem); return total_size; } template requires is_arithmetic_v void read(T & x, ReadBuffer & in) { readBinaryBigEndian(x, in); } void read(OpNum & x, ReadBuffer & in); void read(std::string & s, ReadBuffer & in); void read(ACL & acl, ReadBuffer & in); void read(Stat & stat, ReadBuffer & in); void read(Error & x, ReadBuffer & in); template void read(std::array & s, ReadBuffer & in) { int32_t size = 0; read(size, in); if (size != N) throw Exception::fromMessage(Error::ZMARSHALLINGERROR, "Unexpected array size while reading from ZooKeeper"); in.readStrict(s.data(), N); } template void read(std::vector & arr, ReadBuffer & in) { int32_t size = 0; read(size, in); if (size < 0) throw Exception::fromMessage(Error::ZMARSHALLINGERROR, "Negative size while reading array from ZooKeeper"); if (size > MAX_STRING_OR_ARRAY_SIZE) throw Exception::fromMessage(Error::ZMARSHALLINGERROR, "Too large array size while reading from ZooKeeper"); arr.resize(size); for (auto & elem : arr) read(elem, in); } }