16 #ifndef IMPALA_EXEC_READ_WRITE_UTIL_H
17 #define IMPALA_EXEC_READ_WRITE_UTIL_H
19 #include <boost/cstdint.hpp>
27 #define RETURN_IF_FALSE(x) if (UNLIKELY(!(x))) return false
44 static int PutZInt(int32_t integer, uint8_t* buf);
47 static int PutZLong(int64_t longint, uint8_t* buf);
51 static T
GetInt(
const uint8_t* buffer);
56 static int GetVLong(uint8_t* buf, int64_t* vlong);
57 static int GetVInt(uint8_t* buf, int32_t* vint);
61 static int64_t
PutVLong(int64_t val, uint8_t* buf);
62 static int64_t
PutVInt(int32_t val, uint8_t* buf);
73 static void PutInt(uint8_t* buf, uint16_t integer);
74 static void PutInt(uint8_t* buf, uint32_t integer);
78 static std::string
HexDump(
const uint8_t* buf, int64_t length);
79 static std::string
HexDump(
const char* buf, int64_t length);
93 static int32_t
ReadZInt(uint8_t** buf);
103 static bool Read(uint8_t** buf,
int* buf_len, T* val,
Status* status);
106 static bool SkipBytes(uint8_t** buf,
int* buf_len,
int num_bytes,
Status* status);
111 return (buf[0] << 8) | buf[1];
116 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
121 uint64_t upper_half = GetInt<uint32_t>(buf);
122 uint64_t lower_half = GetInt<uint32_t>(buf + 4);
123 return lower_half | upper_half << 32;
127 buf[0] = integer >> 8;
133 memcpy(buf, &big_endian,
sizeof(uint32_t));
138 memcpy(buf, &big_endian,
sizeof(
uint64_t));
144 *vint =
static_cast<int32_t
>(vlong);
153 int8_t firstbyte = (int8_t) buf[0 + offset];
158 *vlong =
static_cast<int64_t
>(firstbyte);
164 for (
int i = 1; i < len; i++) {
165 *vlong = (*vlong << 8) | buf[i+offset];
169 *vlong = *vlong ^ ((int64_t) - 1);
177 if (val & 0xFF00000000000000llu)
return 8;
178 if (val & 0x00FF000000000000llu)
return 7;
179 if (val & 0x0000FF0000000000llu)
return 6;
180 if (val & 0x000000FF00000000llu)
return 5;
181 if (val & 0x00000000FF000000llu)
return 4;
182 if (val & 0x0000000000FF0000llu)
return 3;
183 if (val & 0x000000000000FF00llu)
return 2;
187 if (val < -112)
return 2;
194 if (num_bytes == 1) {
196 buf[0] =
static_cast<int8_t
>(val);
201 buf[0] = -119 + num_bytes;
204 for (
int i = 0; i < num_bytes; ++i) {
205 buf[i+1] = (val >> (8 * (num_bytes - i - 1))) & 0xFF;
209 return num_bytes + 1;
218 return static_cast<int32_t
>(zlong);
223 int val_len =
sizeof(T);
225 std::stringstream ss;
226 ss <<
"Cannot read " << val_len <<
" bytes, buffer length is " << *buf_len;
227 *status =
Status(ss.str());
230 *val = *
reinterpret_cast<T*
>(*buf);
238 DCHECK_GE(*buf_len, 0);
239 if (
UNLIKELY(num_bytes > *buf_len)) {
240 std::stringstream ss;
241 ss <<
"Cannot skip " << num_bytes <<
" bytes, buffer length is " << *buf_len;
242 *status =
Status(ss.str());
246 *buf_len -= num_bytes;
251 return byte < -120 || (byte >= -112 && byte < 0);
257 }
else if (byte < -120) {
static int DecodeVIntSize(int8_t byte)
Determines the total length in bytes of a Writable VInt/VLong from the first byte.
static const int MAX_ZINT_LEN
Maximum lengths for Zigzag encodings.
static int PutZInt(int32_t integer, uint8_t *buf)
Put a zigzag encoded integer into a buffer and return its length.
static bool SkipBytes(uint8_t **buf, int *buf_len, int num_bytes, Status *status)
Skip the next num_bytes bytes.
static int64_t ByteSwap(int64_t value)
Swaps the byte order (i.e. endianess)
static void PutInt(uint8_t *buf, uint16_t integer)
static int64_t PutVLong(int64_t val, uint8_t *buf)
static bool Read(uint8_t **buf, int *buf_len, T *val, Status *status)
static int64_t ReadZLong(uint8_t **buf)
static const int MAX_ZLONG_LEN
static int32_t ReadZInt(uint8_t **buf)
Read a zig-zag encoded int.
static int GetVInt(uint8_t *buf, int32_t *vint)
static bool IsNegativeVInt(int8_t byte)
Determines the sign of a VInt/VLong from the first byte.
static int GetVLong(uint8_t *buf, int64_t *vlong)
uint8_t offset[7 *64-sizeof(uint64_t)]
static std::string HexDump(const uint8_t *buf, int64_t length)
Dump the first length bytes of buf to a Hex string.
static int64_t PutVInt(int32_t val, uint8_t *buf)
static int VLongRequiredBytes(int64_t val)
returns size of the encoded long value, not including the 1 byte for length
static const int MAX_VINT_LEN
Maximum length for Writeable VInt.
static T GetInt(const uint8_t *buffer)
Get a big endian integer from a buffer. The buffer does not have to be word aligned.
static int PutZLong(int64_t longint, uint8_t *buf)
Put a zigzag encoded long integer into a buffer and return its length.