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 } // namespace tools
46 
47 namespace client {
48 class KuduSchema;
49 
83 class KUDU_EXPORT KuduScanBatch {
84  public:
89  class RowPtr;
90 
97  class const_iterator;
98 
100  typedef RowPtr value_type;
101 
102  KuduScanBatch();
103  ~KuduScanBatch();
104 
106  int NumRows() const;
107 
115  KuduScanBatch::RowPtr Row(int idx) const;
116 
118  const_iterator begin() const;
120  const_iterator end() const;
121 
125  const KuduSchema* projection_schema() const;
126 
134  //
141  Slice direct_data() const;
142 
146  Slice indirect_data() const;
148 
149  private:
150  class KUDU_NO_EXPORT Data;
151  friend class KuduScanner;
152  friend class tools::ReplicaDumper;
153 
154  Data* data_;
155  DISALLOW_COPY_AND_ASSIGN(KuduScanBatch);
156 };
157 
158 class KUDU_EXPORT KuduScanBatch::RowPtr {
159  public:
162  RowPtr() : schema_(NULL), row_data_(NULL) {}
163 
167  bool IsNull(const Slice& col_name) const;
168 
172  bool IsNull(int col_idx) const;
173 
187  Status GetBool(const Slice& col_name, bool* val) const WARN_UNUSED_RESULT;
188 
189  Status GetInt8(const Slice& col_name, int8_t* val) const WARN_UNUSED_RESULT;
190  Status GetInt16(const Slice& col_name, int16_t* val) const WARN_UNUSED_RESULT;
191  Status GetInt32(const Slice& col_name, int32_t* val) const WARN_UNUSED_RESULT;
192  Status GetInt64(const Slice& col_name, int64_t* val) const WARN_UNUSED_RESULT;
193  Status GetUnixTimeMicros(const Slice& col_name, int64_t* micros_since_utc_epoch)
194  const WARN_UNUSED_RESULT;
195 
196  Status GetFloat(const Slice& col_name, float* val) const WARN_UNUSED_RESULT;
197  Status GetDouble(const Slice& col_name, double* val) const WARN_UNUSED_RESULT;
198 
199 #if KUDU_INT128_SUPPORTED
200  Status GetUnscaledDecimal(const Slice& col_name, int128_t* val) const WARN_UNUSED_RESULT;
201 #endif
202 
222  Status GetBool(int col_idx, bool* val) const WARN_UNUSED_RESULT;
223 
224  Status GetInt8(int col_idx, int8_t* val) const WARN_UNUSED_RESULT;
225  Status GetInt16(int col_idx, int16_t* val) const WARN_UNUSED_RESULT;
226  Status GetInt32(int col_idx, int32_t* val) const WARN_UNUSED_RESULT;
227  Status GetInt64(int col_idx, int64_t* val) const WARN_UNUSED_RESULT;
228  Status GetUnixTimeMicros(int col_idx, int64_t* micros_since_utc_epoch) const WARN_UNUSED_RESULT;
229 
230  Status GetFloat(int col_idx, float* val) const WARN_UNUSED_RESULT;
231  Status GetDouble(int col_idx, double* val) const WARN_UNUSED_RESULT;
232 
233 #if KUDU_INT128_SUPPORTED
234  Status GetUnscaledDecimal(int col_idx, int128_t* val) const WARN_UNUSED_RESULT;
235 #endif
236 
255  Status GetString(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
256  Status GetBinary(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
258 
280  Status GetString(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
281  Status GetBinary(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
283 
290  const void* cell(int col_idx) const;
291 
293  std::string ToString() const;
294 
295  private:
296  friend class KuduScanBatch;
297  template<typename KeyTypeWrapper> friend struct SliceKeysTestSetup;
298  template<typename KeyTypeWrapper> friend struct IntKeysTestSetup;
299 
300  // Only invoked by KuduScanner.
301  RowPtr(const Schema* schema,
302  const uint8_t* row_data)
303  : schema_(schema),
304  row_data_(row_data) {
305  }
306 
307  template<typename T>
308  Status Get(const Slice& col_name, typename T::cpp_type* val) const;
309 
310  template<typename T>
311  Status Get(int col_idx, typename T::cpp_type* val) const;
312 
313  const Schema* schema_;
314  const uint8_t* row_data_;
315 };
316 
317 class KUDU_EXPORT KuduScanBatch::const_iterator
318  : public std::iterator<std::forward_iterator_tag, KuduScanBatch::RowPtr> {
319  public:
320  ~const_iterator() {}
321 
323  KuduScanBatch::RowPtr operator*() const {
324  return batch_->Row(idx_);
325  }
326 
330  const_iterator& operator++() {
331  ++idx_;
332  return *this;
333  }
334 
338  const_iterator operator++(int) {
339  const_iterator tmp(batch_, idx_);
340  ++idx_;
341  return tmp;
342  }
343 
350  bool operator==(const const_iterator& other) const {
351  return (idx_ == other.idx_) && (batch_ == other.batch_);
352  }
353 
361  bool operator!=(const const_iterator& other) const {
362  return !(*this == other);
363  }
364 
365  private:
366  friend class KuduScanBatch;
367  const_iterator(const KuduScanBatch* b, int idx)
368  : batch_(b),
369  idx_(idx) {
370  }
371 
372  const KuduScanBatch* const batch_;
373  int idx_;
374 };
375 
376 
377 inline KuduScanBatch::const_iterator KuduScanBatch::begin() const {
378  return const_iterator(this, 0);
379 }
380 
381 inline KuduScanBatch::const_iterator KuduScanBatch::end() const {
382  return const_iterator(this, NumRows());
383 }
384 
385 } // namespace client
386 } // namespace kudu
387 
388 #endif
Definition: callbacks.h:28
A batch of zero or more rows returned by a scan operation.
Definition: scan_batch.h:83
const_iterator operator++(int)
Definition: scan_batch.h:338
KuduScanBatch::RowPtr operator*() const
Definition: scan_batch.h:323
A representation of a table&#39;s schema.
Definition: schema.h:499
bool operator==(const const_iterator &other) const
Definition: scan_batch.h:350
A wrapper around externally allocated data.
Definition: slice.h:50
A representation of an operation&#39;s outcome.
Definition: status.h:145
RowPtr()
Definition: scan_batch.h:162
const_iterator & operator++()
Definition: scan_batch.h:330
bool operator!=(const const_iterator &other) const
Definition: scan_batch.h:361
This class is a representation of a single scan.
Definition: client.h:1762
RowPtr value_type
A handy typedef for the RowPtr.
Definition: scan_batch.h:97