Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
scanner-context.inline.h
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 
16 #ifndef IMPALA_EXEC_SCANNER_CONTEXT_INLINE_H
17 #define IMPALA_EXEC_SCANNER_CONTEXT_INLINE_H
18 
19 #include "exec/scanner-context.h"
20 #include "exec/read-write-util.h"
21 #include "runtime/string-buffer.h"
22 
23 using namespace impala;
24 
26 #define RETURN_IF_FALSE(x) if (UNLIKELY(!(x))) return false
27 
31 inline bool ScannerContext::Stream::GetBytes(int64_t requested_len, uint8_t** buffer,
32  int64_t* out_len, Status* status, bool peek) {
33  if (UNLIKELY(requested_len < 0)) {
34  *status = ReportInvalidRead(requested_len);
35  return false;
36  }
37  if (UNLIKELY(requested_len == 0)) {
38  *out_len = 0;
39  return true;
40  }
41  if (LIKELY(requested_len <= *output_buffer_bytes_left_)) {
42  *out_len = requested_len;
43  *buffer = *output_buffer_pos_;
44  if (LIKELY(!peek)) {
45  total_bytes_returned_ += *out_len;
46  *output_buffer_pos_ += *out_len;
47  *output_buffer_bytes_left_ -= *out_len;
48  }
49  return true;
50  }
51  DCHECK_GT(requested_len, 0);
52  *status = GetBytesInternal(requested_len, buffer, peek, out_len);
53  return status->ok();
54 }
55 
56 inline bool ScannerContext::Stream::ReadBytes(int64_t length, uint8_t** buf,
57  Status* status, bool peek) {
58  int64_t bytes_read;
59  RETURN_IF_FALSE(GetBytes(length, buf, &bytes_read, status, peek));
60  if (UNLIKELY(length != bytes_read)) {
61  DCHECK_LT(bytes_read, length);
62  *status = ReportIncompleteRead(length, bytes_read);
63  return false;
64  }
65  return true;
66 }
67 
70 inline bool ScannerContext::Stream::SkipBytes(int64_t length, Status* status) {
71  uint8_t* dummy_buf;
72  int64_t bytes_read;
73  RETURN_IF_FALSE(GetBytes(length, &dummy_buf, &bytes_read, status));
74  if (UNLIKELY(length != bytes_read)) {
75  DCHECK_LT(bytes_read, length);
76  *status = ReportIncompleteRead(length, bytes_read);
77  return false;
78  }
79  return true;
80 }
81 
83  uint8_t* dummy_buffer;
84  int64_t bytes_read;
85  return ReadText(&dummy_buffer, &bytes_read, status);
86 }
87 
88 inline bool ScannerContext::Stream::ReadText(uint8_t** buf, int64_t* len,
89  Status* status) {
90  RETURN_IF_FALSE(ReadVLong(len, status));
91  RETURN_IF_FALSE(ReadBytes(*len, buf, status));
92  return true;
93 }
94 
95 inline bool ScannerContext::Stream::ReadBoolean(bool* b, Status* status) {
96  uint8_t* val;
97  RETURN_IF_FALSE(ReadBytes(1, &val, status));
98  *b = (*val != 0);
99  return true;
100 }
101 
102 inline bool ScannerContext::Stream::ReadInt(int32_t* val, Status* status, bool peek) {
103  uint8_t* bytes;
104  RETURN_IF_FALSE(ReadBytes(sizeof(uint32_t), &bytes, status, peek));
105  *val = ReadWriteUtil::GetInt<uint32_t>(bytes);
106  return true;
107 }
108 
109 inline bool ScannerContext::Stream::ReadVInt(int32_t* value, Status* status) {
110  int64_t vlong;
111  RETURN_IF_FALSE(ReadVLong(&vlong, status));
112  *value = static_cast<int32_t>(vlong);
113  return true;
114 }
115 
116 inline bool ScannerContext::Stream::ReadVLong(int64_t* value, Status* status) {
117  int8_t* firstbyte;
118  uint8_t* bytes;
119 
120  RETURN_IF_FALSE(ReadBytes(1, reinterpret_cast<uint8_t**>(&firstbyte), status));
121 
122  int len = ReadWriteUtil::DecodeVIntSize(*firstbyte);
123  if (len > ReadWriteUtil::MAX_VINT_LEN) {
124  *status = Status("ReadVLong: size is too big");
125  return false;
126  }
127 
128  if (len == 1) {
129  *value = static_cast<int64_t>(*firstbyte);
130  return true;
131  }
132  --len;
133 
134  RETURN_IF_FALSE(ReadBytes(len, &bytes, status));
135 
136  *value = 0;
137 
138  for (int i = 0; i < len; i++) {
139  *value = (*value << 8) | (bytes[i] & 0xFF);
140  }
141 
142  if (ReadWriteUtil::IsNegativeVInt(*firstbyte)) {
143  *value = *value ^ (static_cast<int64_t>(-1));
144  }
145  return true;
146 }
147 
148 inline bool ScannerContext::Stream::ReadZLong(int64_t* value, Status* status) {
149  uint64_t zlong = 0;
150  int shift = 0;
151  uint8_t* byte;
152  do {
153  DCHECK_LE(shift, 64);
154  RETURN_IF_FALSE(ReadBytes(1, &byte, status));
155  zlong |= static_cast<uint64_t>(*byte & 0x7f) << shift;
156  shift += 7;
157  } while (*byte & 0x80);
158  *value = (zlong >> 1) ^ -(zlong & 1);
159  return true;
160 }
161 
162 #undef RETURN_IF_FALSE
163 
164 #endif
bool SkipText(Status *)
Skip this text object.
static int DecodeVIntSize(int8_t byte)
Determines the total length in bytes of a Writable VInt/VLong from the first byte.
bool ReadInt(int32_t *val, Status *, bool peek=false)
bool ReadBoolean(bool *boolean, Status *)
bool ReadVLong(int64_t *val, Status *)
bool ReadZLong(int64_t *val, Status *)
Read a zigzag encoded long.
bool ReadText(uint8_t **buf, int64_t *length, Status *)
#define RETURN_IF_FALSE(x)
Macro to return false if condition is false. Only defined for this file.
Status GetBytesInternal(int64_t requested_len, uint8_t **buffer, bool peek, int64_t *out_len)
bool GetBytes(int64_t requested_len, uint8_t **buffer, int64_t *out_len, Status *status, bool peek=false)
bool ReadVInt(int32_t *val, Status *)
bool SkipBytes(int64_t length, Status *)
Skip over the next length bytes in the specified HDFS file.
int64_t total_bytes_returned_
Total number of bytes returned from GetBytes()
static bool IsNegativeVInt(int8_t byte)
Determines the sign of a VInt/VLong from the first byte.
#define UNLIKELY(expr)
Definition: compiler-util.h:33
#define LIKELY(expr)
Definition: compiler-util.h:32
Status ReportInvalidRead(int64_t length)
bool ok() const
Definition: status.h:172
bool ReadBytes(int64_t length, uint8_t **buf, Status *, bool peek=false)
static const int MAX_VINT_LEN
Maximum length for Writeable VInt.