ClickHouse/src/IO/VarInt.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

233 lines
4.9 KiB
C++
Raw Normal View History

#pragma once
2010-03-01 16:59:51 +00:00
#include <iostream>
2021-10-02 07:13:14 +00:00
#include <base/types.h>
#include <base/defines.h>
#include <IO/ReadBuffer.h>
#include <IO/WriteBuffer.h>
2010-03-01 16:59:51 +00:00
namespace DB
{
/// Variable-Length Quantity (VLQ) Base-128 compression, also known as Variable Byte (VB) or Varint encoding.
[[noreturn]] void throwReadAfterEOF();
[[noreturn]] void throwValueTooLargeForVarIntEncoding(UInt64 x);
2010-03-01 16:59:51 +00:00
2023-07-06 14:44:06 +00:00
/// NOTE: Due to historical reasons, only values up to 1<<63-1 can be safely encoded/decoded (bigger values are not idempotent under
/// encoding/decoding). This cannot be changed without breaking backward compatibility (some drivers, e.g. clickhouse-rs (Rust), have the
/// same limitation, others support the full 1<<64 range, e.g. clickhouse-driver (Python))
constexpr UInt64 VAR_UINT_MAX = (1ULL<<63) - 1;
inline void writeVarUInt(UInt64 x, WriteBuffer & ostr)
2011-03-22 20:36:01 +00:00
{
2023-07-06 14:44:06 +00:00
if (x > VAR_UINT_MAX) [[unlikely]]
throwValueTooLargeForVarIntEncoding(x);
2011-03-22 20:36:01 +00:00
2023-07-06 14:44:06 +00:00
for (size_t i = 0; i < 9; ++i)
{
uint8_t byte = x & 0x7F;
if (x > 0x7F)
byte |= 0x80;
2010-03-01 16:59:51 +00:00
2023-07-06 14:44:06 +00:00
ostr.nextIfAtEnd();
*ostr.position() = byte;
++ostr.position();
x >>= 7;
if (!x)
return;
}
2010-03-01 16:59:51 +00:00
}
2023-07-06 14:44:06 +00:00
inline void writeVarUInt(UInt64 x, std::ostream & ostr)
2011-03-22 20:36:01 +00:00
{
2023-07-06 14:44:06 +00:00
if (x > VAR_UINT_MAX) [[unlikely]]
throwValueTooLargeForVarIntEncoding(x);
2011-03-22 20:36:01 +00:00
2023-07-06 14:44:06 +00:00
for (size_t i = 0; i < 9; ++i)
{
uint8_t byte = x & 0x7F;
if (x > 0x7F)
byte |= 0x80;
2010-03-01 16:59:51 +00:00
2023-07-06 14:44:06 +00:00
ostr.put(byte);
2023-07-06 14:44:06 +00:00
x >>= 7;
if (!x)
return;
}
2010-03-01 16:59:51 +00:00
}
2023-07-06 14:44:06 +00:00
inline char * writeVarUInt(UInt64 x, char * ostr)
{
2023-07-06 14:44:06 +00:00
if (x > VAR_UINT_MAX) [[unlikely]]
throwValueTooLargeForVarIntEncoding(x);
for (size_t i = 0; i < 9; ++i)
{
uint8_t byte = x & 0x7F;
if (x > 0x7F)
byte |= 0x80;
*ostr = byte;
++ostr;
x >>= 7;
if (!x)
return ostr;
}
return ostr;
}
2023-07-06 14:44:06 +00:00
template <typename Out>
inline void writeVarInt(Int64 x, Out & ostr)
{
2023-07-06 14:44:06 +00:00
writeVarUInt(static_cast<UInt64>((x << 1) ^ (x >> 63)), ostr);
}
2023-07-06 14:44:06 +00:00
inline char * writeVarInt(Int64 x, char * ostr)
{
2023-07-06 14:44:06 +00:00
return writeVarUInt(static_cast<UInt64>((x << 1) ^ (x >> 63)), ostr);
}
2023-07-06 14:44:06 +00:00
namespace impl
{
2023-07-06 14:47:40 +00:00
template <bool check_eof>
2023-07-06 14:44:06 +00:00
inline void readVarUInt(UInt64 & x, ReadBuffer & istr)
{
x = 0;
for (size_t i = 0; i < 9; ++i)
{
2023-07-06 14:47:40 +00:00
if constexpr (check_eof)
if (istr.eof()) [[unlikely]]
throwReadAfterEOF();
UInt64 byte = *istr.position();
++istr.position();
2016-08-04 05:19:37 +00:00
x |= (byte & 0x7F) << (7 * i);
if (!(byte & 0x80))
return;
}
}
2023-07-06 14:44:06 +00:00
}
inline void readVarUInt(UInt64 & x, ReadBuffer & istr)
{
if (istr.buffer().end() - istr.position() >= 9)
2023-07-06 14:47:40 +00:00
return impl::readVarUInt<false>(x, istr);
return impl::readVarUInt<true>(x, istr);
}
inline void readVarUInt(UInt64 & x, std::istream & istr)
{
2016-08-04 05:19:37 +00:00
x = 0;
for (size_t i = 0; i < 9; ++i)
{
UInt64 byte = istr.get();
2016-08-04 05:19:37 +00:00
x |= (byte & 0x7F) << (7 * i);
if (!(byte & 0x80))
return;
}
}
inline const char * readVarUInt(UInt64 & x, const char * istr, size_t size)
{
const char * end = istr + size;
x = 0;
for (size_t i = 0; i < 9; ++i)
{
if (istr == end) [[unlikely]]
throwReadAfterEOF();
UInt64 byte = *istr;
++istr;
x |= (byte & 0x7F) << (7 * i);
if (!(byte & 0x80))
return istr;
}
return istr;
}
2023-07-06 14:44:06 +00:00
template <typename In>
inline void readVarInt(Int64 & x, In & istr)
{
2023-07-06 14:44:06 +00:00
readVarUInt(*reinterpret_cast<UInt64*>(&x), istr);
x = (static_cast<UInt64>(x) >> 1) ^ -(x & 1);
}
2023-07-06 14:44:06 +00:00
inline const char * readVarInt(Int64 & x, const char * istr, size_t size)
{
2023-07-06 14:44:06 +00:00
const char * res = readVarUInt(*reinterpret_cast<UInt64*>(&x), istr, size);
x = (static_cast<UInt64>(x) >> 1) ^ -(x & 1);
return res;
}
2023-07-06 14:44:06 +00:00
inline void readVarUInt(UInt32 & x, ReadBuffer & istr)
{
2023-07-06 14:44:06 +00:00
UInt64 tmp;
readVarUInt(tmp, istr);
x = static_cast<UInt32>(tmp);
}
2016-08-04 05:19:37 +00:00
2023-07-06 14:44:06 +00:00
inline void readVarInt(Int32 & x, ReadBuffer & istr)
{
Int64 tmp;
readVarInt(tmp, istr);
x = static_cast<Int32>(tmp);
}
2016-08-04 05:19:37 +00:00
2023-07-06 14:44:06 +00:00
inline void readVarUInt(UInt16 & x, ReadBuffer & istr)
{
UInt64 tmp;
readVarUInt(tmp, istr);
x = tmp;
}
2023-07-06 14:44:06 +00:00
inline void readVarInt(Int16 & x, ReadBuffer & istr)
{
Int64 tmp;
readVarInt(tmp, istr);
x = tmp;
}
2023-07-06 14:44:06 +00:00
template <typename T>
requires (!std::is_same_v<T, UInt64>)
inline void readVarUInt(T & x, ReadBuffer & istr)
{
UInt64 tmp;
readVarUInt(tmp, istr);
x = tmp;
}
inline size_t getLengthOfVarUInt(UInt64 x)
{
return x < (1ULL << 7) ? 1
: (x < (1ULL << 14) ? 2
: (x < (1ULL << 21) ? 3
: (x < (1ULL << 28) ? 4
: (x < (1ULL << 35) ? 5
: (x < (1ULL << 42) ? 6
: (x < (1ULL << 49) ? 7
: (x < (1ULL << 56) ? 8
: 9)))))));
}
inline size_t getLengthOfVarInt(Int64 x)
{
return getLengthOfVarUInt(static_cast<UInt64>((x << 1) ^ (x >> 63)));
}
}