Kudu C++ client API
partial_row.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_COMMON_PARTIAL_ROW_H
18 #define KUDU_COMMON_PARTIAL_ROW_H
19 
20 #include <stdint.h>
21 #include <string>
22 #include <vector>
23 
24 #ifdef KUDU_HEADERS_NO_STUBS
25 #include "kudu/gutil/macros.h"
26 #include "kudu/gutil/port.h"
27 #include <gtest/gtest_prod.h>
28 #else
29 // This is a poor module interdependency, but the stubs are header-only and
30 // it's only for exported header builds, so we'll make an exception.
31 #include "kudu/client/stubs.h"
32 #endif
33 
34 #include "kudu/util/kudu_export.h"
35 #include "kudu/util/slice.h"
36 
38 namespace kudu {
39 class ColumnSchema;
40 namespace client {
41 class KuduWriteOperation;
42 template<typename KeyTypeWrapper> struct SliceKeysTestSetup;
43 template<typename KeyTypeWrapper> struct IntKeysTestSetup;
44 } // namespace client
46 
47 class Schema;
48 class PartialRowPB;
49 
55 class KUDU_EXPORT KuduPartialRow {
56  public:
60  explicit KuduPartialRow(const Schema* schema);
61 
62  virtual ~KuduPartialRow();
63 
68  KuduPartialRow(const KuduPartialRow& other);
69 
75  KuduPartialRow& operator=(KuduPartialRow other);
76 
88  Status SetBool(const Slice& col_name, bool val) WARN_UNUSED_RESULT;
89 
90  Status SetInt8(const Slice& col_name, int8_t val) WARN_UNUSED_RESULT;
91  Status SetInt16(const Slice& col_name, int16_t val) WARN_UNUSED_RESULT;
92  Status SetInt32(const Slice& col_name, int32_t val) WARN_UNUSED_RESULT;
93  Status SetInt64(const Slice& col_name, int64_t val) WARN_UNUSED_RESULT;
94  Status SetUnixTimeMicros(const Slice& col_name,
95  int64_t micros_since_utc_epoch) WARN_UNUSED_RESULT;
96 
97  Status SetFloat(const Slice& col_name, float val) WARN_UNUSED_RESULT;
98  Status SetDouble(const Slice& col_name, double val) WARN_UNUSED_RESULT;
100 
117  Status SetBool(int col_idx, bool val) WARN_UNUSED_RESULT;
118 
119  Status SetInt8(int col_idx, int8_t val) WARN_UNUSED_RESULT;
120  Status SetInt16(int col_idx, int16_t val) WARN_UNUSED_RESULT;
121  Status SetInt32(int col_idx, int32_t val) WARN_UNUSED_RESULT;
122  Status SetInt64(int col_idx, int64_t val) WARN_UNUSED_RESULT;
123  Status SetUnixTimeMicros(int col_idx, int64_t micros_since_utc_epoch) WARN_UNUSED_RESULT;
124 
125  Status SetFloat(int col_idx, float val) WARN_UNUSED_RESULT;
126  Status SetDouble(int col_idx, double val) WARN_UNUSED_RESULT;
128 
146  Status SetBinary(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT;
147  Status SetString(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT;
149 
172  Status SetBinary(int col_idx, const Slice& val) WARN_UNUSED_RESULT;
173  Status SetString(int col_idx, const Slice& val) WARN_UNUSED_RESULT;
175 
188  Status SetBinaryCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT;
189  Status SetStringCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT;
191 
209  Status SetStringCopy(int col_idx, const Slice& val) WARN_UNUSED_RESULT;
210  Status SetBinaryCopy(int col_idx, const Slice& val) WARN_UNUSED_RESULT;
212 
230  Status SetBinaryNoCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT;
231  Status SetStringNoCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT;
233 
256  Status SetBinaryNoCopy(int col_idx, const Slice& val) WARN_UNUSED_RESULT;
257  Status SetStringNoCopy(int col_idx, const Slice& val) WARN_UNUSED_RESULT;
259 
268  Status SetNull(const Slice& col_name) WARN_UNUSED_RESULT;
269 
278  Status SetNull(int col_idx) WARN_UNUSED_RESULT;
279 
287  Status Unset(const Slice& col_name) WARN_UNUSED_RESULT;
288 
296  Status Unset(int col_idx) WARN_UNUSED_RESULT;
297 
303  bool IsColumnSet(const Slice& col_name) const;
304 
310  bool IsColumnSet(int col_idx) const;
311 
317  bool IsNull(const Slice& col_name) const;
318 
324  bool IsNull(int col_idx) const;
325 
337  Status GetBool(const Slice& col_name, bool* val) const WARN_UNUSED_RESULT;
338 
339  Status GetInt8(const Slice& col_name, int8_t* val) const WARN_UNUSED_RESULT;
340  Status GetInt16(const Slice& col_name, int16_t* val) const WARN_UNUSED_RESULT;
341  Status GetInt32(const Slice& col_name, int32_t* val) const WARN_UNUSED_RESULT;
342  Status GetInt64(const Slice& col_name, int64_t* val) const WARN_UNUSED_RESULT;
343  Status GetUnixTimeMicros(const Slice& col_name,
344  int64_t* micros_since_utc_epoch) const WARN_UNUSED_RESULT;
345 
346  Status GetFloat(const Slice& col_name, float* val) const WARN_UNUSED_RESULT;
347  Status GetDouble(const Slice& col_name, double* val) const WARN_UNUSED_RESULT;
349 
368  Status GetBool(int col_idx, bool* val) const WARN_UNUSED_RESULT;
369 
370  Status GetInt8(int col_idx, int8_t* val) const WARN_UNUSED_RESULT;
371  Status GetInt16(int col_idx, int16_t* val) const WARN_UNUSED_RESULT;
372  Status GetInt32(int col_idx, int32_t* val) const WARN_UNUSED_RESULT;
373  Status GetInt64(int col_idx, int64_t* val) const WARN_UNUSED_RESULT;
374  Status GetUnixTimeMicros(int col_idx, int64_t* micros_since_utc_epoch) const WARN_UNUSED_RESULT;
375 
376  Status GetFloat(int col_idx, float* val) const WARN_UNUSED_RESULT;
377  Status GetDouble(int col_idx, double* val) const WARN_UNUSED_RESULT;
379 
397  Status GetString(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
398  Status GetBinary(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
400 
422  Status GetString(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
423  Status GetBinary(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
425 
426  //------------------------------------------------------------
427  // Key-encoding related functions
428  //------------------------------------------------------------
429 
441  Status EncodeRowKey(std::string* encoded_key) const;
442 
449  std::string ToEncodedRowKeyOrDie() const;
450 
451  //------------------------------------------------------------
452  // Utility code
453  //------------------------------------------------------------
454 
457  bool IsKeySet() const;
458 
460  bool AllColumnsSet() const;
461 
467  std::string ToString() const;
468 
470  const Schema* schema() const { return schema_; }
471 
472  private:
473  friend class client::KuduWriteOperation; // for row_data_.
474  friend class KeyUtilTest;
475  friend class PartitionSchema;
476  friend class RowOperationsPBDecoder;
477  friend class RowOperationsPBEncoder;
478  friend class TestScanSpec;
479  template<typename KeyTypeWrapper> friend struct client::SliceKeysTestSetup;
480  template<typename KeyTypeWrapper> friend struct client::IntKeysTestSetup;
481  FRIEND_TEST(TestPartitionPruner, TestPrimaryKeyRangePruning);
482  FRIEND_TEST(TestPartitionPruner, TestPartialPrimaryKeyRangePruning);
483 
484  template<typename T>
485  Status Set(const Slice& col_name, const typename T::cpp_type& val,
486  bool owned = false);
487 
488  template<typename T>
489  Status Set(int col_idx, const typename T::cpp_type& val,
490  bool owned = false);
491 
492  // Runtime version of the generic setter.
493  Status Set(int32_t column_idx, const uint8_t* val);
494 
495  template<typename T>
496  Status Get(const Slice& col_name, typename T::cpp_type* val) const;
497 
498  template<typename T>
499  Status Get(int col_idx, typename T::cpp_type* val) const;
500 
501  template<typename T>
502  Status SetSliceCopy(const Slice& col_name, const Slice& val);
503 
504  template<typename T>
505  Status SetSliceCopy(int col_idx, const Slice& val);
506 
507  // If the given column is a variable length column whose memory is owned by this instance,
508  // deallocates the value.
509  // NOTE: Does not mutate the isset bitmap.
510  // REQUIRES: col_idx must be a variable length column.
511  void DeallocateStringIfSet(int col_idx, const ColumnSchema& col);
512 
513  // Deallocate any string/binary values whose memory is managed by this object.
514  void DeallocateOwnedStrings();
515 
516  const Schema* schema_;
517 
518  // 1-bit set for any field which has been explicitly set. This is distinct
519  // from NULL -- an "unset" field will take the server-side default on insert,
520  // whereas a field explicitly set to NULL will override the default.
521  uint8_t* isset_bitmap_;
522 
523  // 1-bit set for any variable length columns whose memory is managed by this instance.
524  // These strings need to be deallocated whenever the value is reset,
525  // or when the instance is destructed.
526  uint8_t* owned_strings_bitmap_;
527 
528  // The normal "contiguous row" format row data. Any column whose data is unset
529  // or NULL can have undefined bytes.
530  uint8_t* row_data_;
531 };
532 
533 } // namespace kudu
534 #endif /* KUDU_COMMON_PARTIAL_ROW_H */
Definition: callbacks.h:28
const Schema * schema() const
Definition: partial_row.h:470
A row which may only contain values for a subset of the columns.
Definition: partial_row.h:55