Kudu C++ client API
scan_batch.h
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 #ifndef KUDU_CLIENT_SCAN_BATCH_H
18 #define KUDU_CLIENT_SCAN_BATCH_H
19 
20 // NOTE: using stdint.h instead of cstdint because this file is supposed
21 // to be processed by a compiler lacking C++11 support.
22 #include <stdint.h>
23 
24 #include <cstddef>
25 #include <iterator>
26 #include <string>
27 
28 #ifdef KUDU_HEADERS_NO_STUBS
29 #include "kudu/gutil/macros.h"
30 #include "kudu/gutil/port.h"
31 #else
32 #include "kudu/client/stubs.h"
33 #endif
34 
35 #include "kudu/util/int128.h"
36 #include "kudu/util/kudu_export.h"
37 #include "kudu/util/slice.h"
38 #include "kudu/util/status.h"
39 
40 namespace kudu {
41 class Schema;
42 
43 namespace tools {
44 class ReplicaDumper;
45 class TableScanner;
46 } // namespace tools
47 
48 namespace client {
49 class KuduSchema;
50 
84 class KUDU_EXPORT KuduScanBatch {
85  public:
90  class RowPtr;
91 
98  class const_iterator;
99 
101  typedef RowPtr value_type;
102 
103  KuduScanBatch();
104  ~KuduScanBatch();
105 
107  int NumRows() const;
108 
116  KuduScanBatch::RowPtr Row(int idx) const;
117 
119  const_iterator begin() const;
121  const_iterator end() const;
122 
126  const KuduSchema* projection_schema() const;
127 
135  //
142  Slice direct_data() const;
143 
147  Slice indirect_data() const;
149 
150  private:
151  class KUDU_NO_EXPORT Data;
152  friend class KuduScanner;
153  friend class tools::ReplicaDumper;
154 
155  Data* data_;
156  DISALLOW_COPY_AND_ASSIGN(KuduScanBatch);
157 };
158 
159 class KUDU_EXPORT KuduScanBatch::RowPtr {
160  public:
163  RowPtr() : schema_(NULL), row_data_(NULL) {}
164 
167 
169  const RowPtr* operator->() const {
170  return this;
171  }
175  bool IsNull(const Slice& col_name) const;
176 
180  bool IsNull(int col_idx) const;
181 
188  Status IsDeleted(bool* val) const WARN_UNUSED_RESULT KUDU_NO_EXPORT;
189 
203  Status GetBool(const Slice& col_name, bool* val) const WARN_UNUSED_RESULT;
204 
205  Status GetInt8(const Slice& col_name, int8_t* val) const WARN_UNUSED_RESULT;
206  Status GetInt16(const Slice& col_name, int16_t* val) const WARN_UNUSED_RESULT;
207  Status GetInt32(const Slice& col_name, int32_t* val) const WARN_UNUSED_RESULT;
208  Status GetInt64(const Slice& col_name, int64_t* val) const WARN_UNUSED_RESULT;
209  Status GetUnixTimeMicros(const Slice& col_name, int64_t* micros_since_utc_epoch)
210  const WARN_UNUSED_RESULT;
211  Status GetDate(const Slice& col_name, int32_t* days_since_unix_epoch) const WARN_UNUSED_RESULT;
212 
213  Status GetFloat(const Slice& col_name, float* val) const WARN_UNUSED_RESULT;
214  Status GetDouble(const Slice& col_name, double* val) const WARN_UNUSED_RESULT;
215 
216 #if KUDU_INT128_SUPPORTED
217  Status GetUnscaledDecimal(const Slice& col_name, int128_t* val) const WARN_UNUSED_RESULT;
218 #endif
219 
239  Status GetBool(int col_idx, bool* val) const WARN_UNUSED_RESULT;
240 
241  Status GetInt8(int col_idx, int8_t* val) const WARN_UNUSED_RESULT;
242  Status GetInt16(int col_idx, int16_t* val) const WARN_UNUSED_RESULT;
243  Status GetInt32(int col_idx, int32_t* val) const WARN_UNUSED_RESULT;
244  Status GetInt64(int col_idx, int64_t* val) const WARN_UNUSED_RESULT;
245  Status GetUnixTimeMicros(int col_idx, int64_t* micros_since_utc_epoch) const WARN_UNUSED_RESULT;
246  Status GetDate(int col_idx, int32_t* days_since_unix_epoch) const WARN_UNUSED_RESULT;
247 
248  Status GetFloat(int col_idx, float* val) const WARN_UNUSED_RESULT;
249  Status GetDouble(int col_idx, double* val) const WARN_UNUSED_RESULT;
250 
251 #if KUDU_INT128_SUPPORTED
252  Status GetUnscaledDecimal(int col_idx, int128_t* val) const WARN_UNUSED_RESULT;
253 #endif
254 
273  Status GetString(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
274  Status GetBinary(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
275  Status GetVarchar(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
277 
299  Status GetString(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
300  Status GetBinary(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
301  Status GetVarchar(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
303 
310  const void* cell(int col_idx) const;
311 
313  std::string ToString() const;
314 
315  private:
316  friend class KuduScanBatch;
317  friend class tools::TableScanner;
318  template<typename KeyTypeWrapper> friend struct SliceKeysTestSetup;
319  template<typename KeyTypeWrapper> friend struct IntKeysTestSetup;
320 
321  // Only invoked by KuduScanner.
322  RowPtr(const Schema* schema,
323  const uint8_t* row_data)
324  : schema_(schema),
325  row_data_(row_data) {
326  }
327 
328  template<typename T>
329  Status Get(const Slice& col_name, typename T::cpp_type* val) const;
330 
331  template<typename T>
332  Status Get(int col_idx, typename T::cpp_type* val) const;
333 
334  const Schema* schema_;
335  const uint8_t* row_data_;
336 };
337 
338 class KUDU_EXPORT KuduScanBatch::const_iterator
339  : public std::iterator<std::forward_iterator_tag, KuduScanBatch::RowPtr> {
340  public:
341  ~const_iterator() {}
342 
344  KuduScanBatch::RowPtr operator*() const {
345  return batch_->Row(idx_);
346  }
347 
352 
354  KuduScanBatch::RowPtr operator->() const {
355  return batch_->Row(idx_);
356  }
357 
361  const_iterator& operator++() {
362  ++idx_;
363  return *this;
364  }
365 
369  const_iterator operator++(int) {
370  const_iterator tmp(batch_, idx_);
371  ++idx_;
372  return tmp;
373  }
374 
381  bool operator==(const const_iterator& other) const {
382  return (idx_ == other.idx_) && (batch_ == other.batch_);
383  }
384 
392  bool operator!=(const const_iterator& other) const {
393  return !(*this == other);
394  }
395 
396  private:
397  friend class KuduScanBatch;
398  const_iterator(const KuduScanBatch* b, int idx)
399  : batch_(b),
400  idx_(idx) {
401  }
402 
403  const KuduScanBatch* const batch_;
404  int idx_;
405 };
406 
407 
408 inline KuduScanBatch::const_iterator KuduScanBatch::begin() const {
409  return const_iterator(this, 0);
410 }
411 
412 inline KuduScanBatch::const_iterator KuduScanBatch::end() const {
413  return const_iterator(this, NumRows());
414 }
415 
416 } // namespace client
417 } // namespace kudu
418 
419 #endif
A representation of a table&#39;s schema.
Definition: schema.h:594
A representation of an operation&#39;s outcome.
Definition: status.h:165
Definition: callbacks.h:28
RowPtr value_type
A handy typedef for the RowPtr.
Definition: scan_batch.h:98
RowPtr()
Definition: scan_batch.h:163
KuduScanBatch::RowPtr operator*() const
Definition: scan_batch.h:344
A wrapper around externally allocated data.
Definition: slice.h:51
This class is a representation of a single scan.
Definition: client.h:2012
KuduScanBatch::RowPtr operator->() const
Definition: scan_batch.h:354
bool operator==(const const_iterator &other) const
Definition: scan_batch.h:381
const_iterator operator++(int)
Definition: scan_batch.h:369
bool operator!=(const const_iterator &other) const
Definition: scan_batch.h:392
const_iterator & operator++()
Definition: scan_batch.h:361
A batch of zero or more rows returned by a scan operation.
Definition: scan_batch.h:84
const RowPtr * operator->() const
Definition: scan_batch.h:169