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 #include <string>
21 
22 #ifdef KUDU_HEADERS_NO_STUBS
23 #include "kudu/gutil/macros.h"
24 #include "kudu/gutil/port.h"
25 #else
26 #include "kudu/client/stubs.h"
27 #endif
28 
29 #include "kudu/util/kudu_export.h"
30 #include "kudu/util/slice.h"
31 
32 namespace kudu {
33 class Schema;
34 
35 namespace tools {
36 class ReplicaDumper;
37 } // namespace tools
38 
39 namespace client {
40 class KuduSchema;
41 
75 class KUDU_EXPORT KuduScanBatch {
76  public:
81  class RowPtr;
82 
89  class const_iterator;
90 
92  typedef RowPtr value_type;
93 
94  KuduScanBatch();
95  ~KuduScanBatch();
96 
98  int NumRows() const;
99 
107  KuduScanBatch::RowPtr Row(int idx) const;
108 
110  const_iterator begin() const;
112  const_iterator end() const;
113 
117  const KuduSchema* projection_schema() const;
118 
126  //
133  Slice direct_data() const;
134 
138  Slice indirect_data() const;
140 
141  private:
142  class KUDU_NO_EXPORT Data;
143  friend class KuduScanner;
144  friend class tools::ReplicaDumper;
145 
146  Data* data_;
147  DISALLOW_COPY_AND_ASSIGN(KuduScanBatch);
148 };
149 
150 class KUDU_EXPORT KuduScanBatch::RowPtr {
151  public:
154  RowPtr() : schema_(NULL), row_data_(NULL) {}
155 
159  bool IsNull(const Slice& col_name) const;
160 
164  bool IsNull(int col_idx) const;
165 
179  Status GetBool(const Slice& col_name, bool* val) const WARN_UNUSED_RESULT;
180 
181  Status GetInt8(const Slice& col_name, int8_t* val) const WARN_UNUSED_RESULT;
182  Status GetInt16(const Slice& col_name, int16_t* val) const WARN_UNUSED_RESULT;
183  Status GetInt32(const Slice& col_name, int32_t* val) const WARN_UNUSED_RESULT;
184  Status GetInt64(const Slice& col_name, int64_t* val) const WARN_UNUSED_RESULT;
185  Status GetUnixTimeMicros(const Slice& col_name, int64_t* micros_since_utc_epoch)
186  const WARN_UNUSED_RESULT;
187 
188  Status GetFloat(const Slice& col_name, float* val) const WARN_UNUSED_RESULT;
189  Status GetDouble(const Slice& col_name, double* val) const WARN_UNUSED_RESULT;
191 
210  Status GetBool(int col_idx, bool* val) const WARN_UNUSED_RESULT;
211 
212  Status GetInt8(int col_idx, int8_t* val) const WARN_UNUSED_RESULT;
213  Status GetInt16(int col_idx, int16_t* val) const WARN_UNUSED_RESULT;
214  Status GetInt32(int col_idx, int32_t* val) const WARN_UNUSED_RESULT;
215  Status GetInt64(int col_idx, int64_t* val) const WARN_UNUSED_RESULT;
216  Status GetUnixTimeMicros(int col_idx, int64_t* micros_since_utc_epoch) const WARN_UNUSED_RESULT;
217 
218  Status GetFloat(int col_idx, float* val) const WARN_UNUSED_RESULT;
219  Status GetDouble(int col_idx, double* val) const WARN_UNUSED_RESULT;
221 
239  Status GetString(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
240  Status GetBinary(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
242 
264  Status GetString(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
265  Status GetBinary(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
267 
274  const void* cell(int col_idx) const;
275 
277  std::string ToString() const;
278 
279  private:
280  friend class KuduScanBatch;
281  template<typename KeyTypeWrapper> friend struct SliceKeysTestSetup;
282  template<typename KeyTypeWrapper> friend struct IntKeysTestSetup;
283 
284  // Only invoked by KuduScanner.
285  RowPtr(const Schema* schema,
286  const uint8_t* row_data)
287  : schema_(schema),
288  row_data_(row_data) {
289  }
290 
291  template<typename T>
292  Status Get(const Slice& col_name, typename T::cpp_type* val) const;
293 
294  template<typename T>
295  Status Get(int col_idx, typename T::cpp_type* val) const;
296 
297  const Schema* schema_;
298  const uint8_t* row_data_;
299 };
300 
301 class KUDU_EXPORT KuduScanBatch::const_iterator
302  : public std::iterator<std::forward_iterator_tag, KuduScanBatch::RowPtr> {
303  public:
304  ~const_iterator() {}
305 
307  KuduScanBatch::RowPtr operator*() const {
308  return batch_->Row(idx_);
309  }
310 
314  const_iterator& operator++() {
315  ++idx_;
316  return *this;
317  }
318 
322  const_iterator operator++(int) {
323  const_iterator tmp(batch_, idx_);
324  ++idx_;
325  return tmp;
326  }
327 
334  bool operator==(const const_iterator& other) const {
335  return (idx_ == other.idx_) && (batch_ == other.batch_);
336  }
337 
345  bool operator!=(const const_iterator& other) const {
346  return !(*this == other);
347  }
348 
349  private:
350  friend class KuduScanBatch;
351  const_iterator(const KuduScanBatch* b, int idx)
352  : batch_(b),
353  idx_(idx) {
354  }
355 
356  const KuduScanBatch* const batch_;
357  int idx_;
358 };
359 
360 
361 inline KuduScanBatch::const_iterator KuduScanBatch::begin() const {
362  return const_iterator(this, 0);
363 }
364 
365 inline KuduScanBatch::const_iterator KuduScanBatch::end() const {
366  return const_iterator(this, NumRows());
367 }
368 
369 } // namespace client
370 } // namespace kudu
371 
372 #endif
A representation of a table&#39;s schema.
Definition: schema.h:423
A representation of an operation&#39;s outcome.
Definition: status.h:130
Definition: callbacks.h:28
RowPtr value_type
A handy typedef for the RowPtr.
Definition: scan_batch.h:89
RowPtr()
Definition: scan_batch.h:154
KuduScanBatch::RowPtr operator*() const
Definition: scan_batch.h:307
A wrapper around externally allocated data.
Definition: slice.h:43
This class is a representation of a single scan.
Definition: client.h:1695
bool operator==(const const_iterator &other) const
Definition: scan_batch.h:334
const_iterator operator++(int)
Definition: scan_batch.h:322
bool operator!=(const const_iterator &other) const
Definition: scan_batch.h:345
const_iterator & operator++()
Definition: scan_batch.h:314
A batch of zero or more rows returned by a scan operation.
Definition: scan_batch.h:75