Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
read-write-util.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "exec/read-write-util.h"
16 
17 #include "common/names.h"
18 
19 using namespace impala;
20 
21 // This function is not inlined because it can potentially cause LLVM to crash (see
22 // http://llvm.org/bugs/show_bug.cgi?id=19315), and inlining does not appear to have any
23 // performance impact.
24 int64_t ReadWriteUtil::ReadZLong(uint8_t** buf) {
25  uint64_t zlong = 0;
26  int shift = 0;
27  bool more;
28  do {
29  DCHECK_LE(shift, 64);
30  zlong |= static_cast<uint64_t>(**buf & 0x7f) << shift;
31  shift += 7;
32  more = (**buf & 0x80) != 0;
33  ++(*buf);
34  } while (more);
35  return (zlong >> 1) ^ -(zlong & 1);
36 }
37 
38 int ReadWriteUtil::PutZInt(int32_t integer, uint8_t* buf) {
39  // Move the sign bit to the first bit.
40  uint32_t uinteger = (integer << 1) ^ (integer >> 31);
41  const int mask = 0x7f;
42  const int cont = 0x80;
43  buf[0] = uinteger & mask;
44  int len = 1;
45  while ((uinteger >>= 7) != 0) {
46  // Set the continuation bit.
47  buf[len - 1] |= cont;
48  buf[len] = uinteger & mask;
49  ++len;
50  }
51 
52  return len;
53 }
54 
55 int ReadWriteUtil::PutZLong(int64_t longint, uint8_t* buf) {
56  // Move the sign bit to the first bit.
57  uint64_t ulongint = (longint << 1) ^ (longint >> 63);
58  const int mask = 0x7f;
59  const int cont = 0x80;
60  buf[0] = ulongint & mask;
61  int len = 1;
62  while ((ulongint >>= 7) != 0) {
63  // Set the continuation bit.
64  buf[len - 1] |= cont;
65  buf[len] = ulongint & mask;
66  ++len;
67  }
68 
69  return len;
70 }
71 
72 string ReadWriteUtil::HexDump(const uint8_t* buf, int64_t length) {
73  stringstream ss;
74  ss << std::hex;
75  for (int i = 0; i < length; ++i) {
76  ss << static_cast<int>(buf[i]) << " ";
77  }
78  return ss.str();
79 }
80 
81 string ReadWriteUtil::HexDump(const char* buf, int64_t length) {
82  return HexDump(reinterpret_cast<const uint8_t*>(buf), length);
83 }
static int PutZInt(int32_t integer, uint8_t *buf)
Put a zigzag encoded integer into a buffer and return its length.
static int64_t ReadZLong(uint8_t **buf)
static std::string HexDump(const uint8_t *buf, int64_t length)
Dump the first length bytes of buf to a Hex string.
static int PutZLong(int64_t longint, uint8_t *buf)
Put a zigzag encoded long integer into a buffer and return its length.