Kudu C++ client API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 TsAdminClient;
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 
119  private:
120  class KUDU_NO_EXPORT Data;
121  friend class KuduScanner;
122  friend class kudu::tools::TsAdminClient;
123 
124  Data* data_;
125  DISALLOW_COPY_AND_ASSIGN(KuduScanBatch);
126 };
127 
128 class KUDU_EXPORT KuduScanBatch::RowPtr {
129  public:
132  RowPtr() : schema_(NULL), row_data_(NULL) {}
133 
137  bool IsNull(const Slice& col_name) const;
138 
142  bool IsNull(int col_idx) const;
143 
157  Status GetBool(const Slice& col_name, bool* val) const WARN_UNUSED_RESULT;
158 
159  Status GetInt8(const Slice& col_name, int8_t* val) const WARN_UNUSED_RESULT;
160  Status GetInt16(const Slice& col_name, int16_t* val) const WARN_UNUSED_RESULT;
161  Status GetInt32(const Slice& col_name, int32_t* val) const WARN_UNUSED_RESULT;
162  Status GetInt64(const Slice& col_name, int64_t* val) const WARN_UNUSED_RESULT;
163  Status GetTimestamp(const Slice& col_name, int64_t* micros_since_utc_epoch)
164  const WARN_UNUSED_RESULT;
165 
166  Status GetFloat(const Slice& col_name, float* val) const WARN_UNUSED_RESULT;
167  Status GetDouble(const Slice& col_name, double* val) const WARN_UNUSED_RESULT;
169 
188  Status GetBool(int col_idx, bool* val) const WARN_UNUSED_RESULT;
189 
190  Status GetInt8(int col_idx, int8_t* val) const WARN_UNUSED_RESULT;
191  Status GetInt16(int col_idx, int16_t* val) const WARN_UNUSED_RESULT;
192  Status GetInt32(int col_idx, int32_t* val) const WARN_UNUSED_RESULT;
193  Status GetInt64(int col_idx, int64_t* val) const WARN_UNUSED_RESULT;
194  Status GetTimestamp(int col_idx, int64_t* micros_since_utc_epoch) const WARN_UNUSED_RESULT;
195 
196  Status GetFloat(int col_idx, float* val) const WARN_UNUSED_RESULT;
197  Status GetDouble(int col_idx, double* val) const WARN_UNUSED_RESULT;
199 
217  Status GetString(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
218  Status GetBinary(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
220 
242  Status GetString(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
243  Status GetBinary(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
245 
252  const void* cell(int col_idx) const;
253 
255  std::string ToString() const;
256 
257  private:
258  friend class KuduScanBatch;
259  template<typename KeyTypeWrapper> friend struct SliceKeysTestSetup;
260  template<typename KeyTypeWrapper> friend struct IntKeysTestSetup;
261 
262  // Only invoked by KuduScanner.
263  RowPtr(const Schema* schema,
264  const uint8_t* row_data)
265  : schema_(schema),
266  row_data_(row_data) {
267  }
268 
269  template<typename T>
270  Status Get(const Slice& col_name, typename T::cpp_type* val) const;
271 
272  template<typename T>
273  Status Get(int col_idx, typename T::cpp_type* val) const;
274 
275  const Schema* schema_;
276  const uint8_t* row_data_;
277 };
278 
279 class KUDU_EXPORT KuduScanBatch::const_iterator
280  : public std::iterator<std::forward_iterator_tag, KuduScanBatch::RowPtr> {
281  public:
282  ~const_iterator() {}
283 
286  return batch_->Row(idx_);
287  }
288 
292  const_iterator& operator++() {
293  ++idx_;
294  return *this;
295  }
296 
300  const_iterator operator++(int) {
301  const_iterator tmp(batch_, idx_);
302  ++idx_;
303  return tmp;
304  }
305 
312  bool operator==(const const_iterator& other) const {
313  return (idx_ == other.idx_) && (batch_ == other.batch_);
314  }
315 
323  bool operator!=(const const_iterator& other) const {
324  return !(*this == other);
325  }
326 
327  private:
328  friend class KuduScanBatch;
329  const_iterator(const KuduScanBatch* b, int idx)
330  : batch_(b),
331  idx_(idx) {
332  }
333 
334  const KuduScanBatch* const batch_;
335  int idx_;
336 };
337 
338 
339 inline KuduScanBatch::const_iterator KuduScanBatch::begin() const {
340  return const_iterator(this, 0);
341 }
342 
343 inline KuduScanBatch::const_iterator KuduScanBatch::end() const {
344  return const_iterator(this, NumRows());
345 }
346 
347 } // namespace client
348 } // namespace kudu
349 
350 #endif
A representation of a table's schema.
Definition: schema.h:409
A representation of an operation's outcome.
Definition: status.h:106
const_iterator begin() const
Definition: scan_batch.h:339
RowPtr value_type
A handy typedef for the RowPtr.
Definition: scan_batch.h:89
RowPtr()
Definition: scan_batch.h:132
Definition: partial_row.h:42
KuduScanBatch::RowPtr operator*() const
Definition: scan_batch.h:285
Definition: partial_row.h:41
A wrapper around externally allocated data.
Definition: slice.h:43
This class is a representation of a single scan.
Definition: client.h:1299
bool operator==(const const_iterator &other) const
Definition: scan_batch.h:312
const_iterator operator++(int)
Definition: scan_batch.h:300
bool operator!=(const const_iterator &other) const
Definition: scan_batch.h:323
const_iterator & operator++()
Definition: scan_batch.h:292
const_iterator end() const
Definition: scan_batch.h:343
A batch of zero or more rows returned by a scan operation.
Definition: scan_batch.h:75