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 // 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 <string>
25 
26 #ifdef KUDU_HEADERS_NO_STUBS
27 #include <gtest/gtest_prod.h>
28 
29 #include "kudu/gutil/port.h"
30 #else
31 // This is a poor module interdependency, but the stubs are header-only and
32 // it's only for exported header builds, so we'll make an exception.
33 #include "kudu/client/stubs.h"
34 #endif
35 
36 #include "kudu/util/int128.h"
37 #include "kudu/util/kudu_export.h"
38 #include "kudu/util/slice.h"
39 #include "kudu/util/status.h"
40 
42 namespace kudu {
43 class ColumnSchema;
44 namespace client {
45 class KuduWriteOperation;
46 template<typename KeyTypeWrapper> struct SliceKeysTestSetup;// IWYU pragma: keep
47 template<typename KeyTypeWrapper> struct IntKeysTestSetup; // IWYU pragma: keep
48 } // namespace client
49 
50 namespace tablet {
51  template<typename KeyTypeWrapper> struct SliceTypeRowOps; // IWYU pragma: keep
52  template<typename KeyTypeWrapper> struct NumTypeRowOps; // IWYU pragma: keep
53 } // namespace tablet
54 
56 
57 class Schema;
58 
64 class KUDU_EXPORT KuduPartialRow {
65  public:
69  explicit KuduPartialRow(const Schema* schema);
70 
71  virtual ~KuduPartialRow();
72 
77  KuduPartialRow(const KuduPartialRow& other);
78 
84  KuduPartialRow& operator=(KuduPartialRow other);
85 
97  Status SetBool(const Slice& col_name, bool val) WARN_UNUSED_RESULT;
98 
99  Status SetInt8(const Slice& col_name, int8_t val) WARN_UNUSED_RESULT;
100  Status SetInt16(const Slice& col_name, int16_t val) WARN_UNUSED_RESULT;
101  Status SetInt32(const Slice& col_name, int32_t val) WARN_UNUSED_RESULT;
102  Status SetInt64(const Slice& col_name, int64_t val) WARN_UNUSED_RESULT;
103  Status SetUnixTimeMicros(const Slice& col_name,
104  int64_t micros_since_utc_epoch) WARN_UNUSED_RESULT;
105 
106  Status SetFloat(const Slice& col_name, float val) WARN_UNUSED_RESULT;
107  Status SetDouble(const Slice& col_name, double val) WARN_UNUSED_RESULT;
108 #if KUDU_INT128_SUPPORTED
109  Status SetUnscaledDecimal(const Slice& col_name, int128_t val) WARN_UNUSED_RESULT;
110 #endif
111 
129  Status SetBool(int col_idx, bool val) WARN_UNUSED_RESULT;
130 
131  Status SetInt8(int col_idx, int8_t val) WARN_UNUSED_RESULT;
132  Status SetInt16(int col_idx, int16_t val) WARN_UNUSED_RESULT;
133  Status SetInt32(int col_idx, int32_t val) WARN_UNUSED_RESULT;
134  Status SetInt64(int col_idx, int64_t val) WARN_UNUSED_RESULT;
135  Status SetUnixTimeMicros(int col_idx, int64_t micros_since_utc_epoch) WARN_UNUSED_RESULT;
136 
137  Status SetFloat(int col_idx, float val) WARN_UNUSED_RESULT;
138  Status SetDouble(int col_idx, double val) WARN_UNUSED_RESULT;
139 #if KUDU_INT128_SUPPORTED
140  Status SetUnscaledDecimal(int col_idx, int128_t val) WARN_UNUSED_RESULT;
141 #endif
142 
161  Status SetBinary(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT;
162  Status SetString(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT;
164 
187  Status SetBinary(int col_idx, const Slice& val) WARN_UNUSED_RESULT;
188  Status SetString(int col_idx, const Slice& val) WARN_UNUSED_RESULT;
190 
203  Status SetBinaryCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT;
204  Status SetStringCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT;
206 
224  Status SetStringCopy(int col_idx, const Slice& val) WARN_UNUSED_RESULT;
225  Status SetBinaryCopy(int col_idx, const Slice& val) WARN_UNUSED_RESULT;
227 
245  Status SetBinaryNoCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT;
246  Status SetStringNoCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT;
248 
271  Status SetBinaryNoCopy(int col_idx, const Slice& val) WARN_UNUSED_RESULT;
272  Status SetStringNoCopy(int col_idx, const Slice& val) WARN_UNUSED_RESULT;
274 
283  Status SetNull(const Slice& col_name) WARN_UNUSED_RESULT;
284 
293  Status SetNull(int col_idx) WARN_UNUSED_RESULT;
294 
302  Status Unset(const Slice& col_name) WARN_UNUSED_RESULT;
303 
311  Status Unset(int col_idx) WARN_UNUSED_RESULT;
312 
318  bool IsColumnSet(const Slice& col_name) const;
319 
325  bool IsColumnSet(int col_idx) const;
326 
332  bool IsNull(const Slice& col_name) const;
333 
339  bool IsNull(int col_idx) const;
340 
352  Status GetBool(const Slice& col_name, bool* val) const WARN_UNUSED_RESULT;
353 
354  Status GetInt8(const Slice& col_name, int8_t* val) const WARN_UNUSED_RESULT;
355  Status GetInt16(const Slice& col_name, int16_t* val) const WARN_UNUSED_RESULT;
356  Status GetInt32(const Slice& col_name, int32_t* val) const WARN_UNUSED_RESULT;
357  Status GetInt64(const Slice& col_name, int64_t* val) const WARN_UNUSED_RESULT;
358  Status GetUnixTimeMicros(const Slice& col_name,
359  int64_t* micros_since_utc_epoch) const WARN_UNUSED_RESULT;
360 
361  Status GetFloat(const Slice& col_name, float* val) const WARN_UNUSED_RESULT;
362  Status GetDouble(const Slice& col_name, double* val) const WARN_UNUSED_RESULT;
363 #if KUDU_INT128_SUPPORTED
364  // NOTE: The non-const version of this function is kept for backwards compatibility.
365  Status GetUnscaledDecimal(const Slice& col_name, int128_t* val) WARN_UNUSED_RESULT;
366  Status GetUnscaledDecimal(const Slice& col_name, int128_t* val) const WARN_UNUSED_RESULT;
367 #endif
368 
388  Status GetBool(int col_idx, bool* val) const WARN_UNUSED_RESULT;
389 
390  Status GetInt8(int col_idx, int8_t* val) const WARN_UNUSED_RESULT;
391  Status GetInt16(int col_idx, int16_t* val) const WARN_UNUSED_RESULT;
392  Status GetInt32(int col_idx, int32_t* val) const WARN_UNUSED_RESULT;
393  Status GetInt64(int col_idx, int64_t* val) const WARN_UNUSED_RESULT;
394  Status GetUnixTimeMicros(int col_idx, int64_t* micros_since_utc_epoch) const WARN_UNUSED_RESULT;
395 
396  Status GetFloat(int col_idx, float* val) const WARN_UNUSED_RESULT;
397  Status GetDouble(int col_idx, double* val) const WARN_UNUSED_RESULT;
398 #if KUDU_INT128_SUPPORTED
399  // NOTE: The non-const version of this function is kept for backwards compatibility.
400  Status GetUnscaledDecimal(int col_idx, int128_t* val) WARN_UNUSED_RESULT;
401  Status GetUnscaledDecimal(int col_idx, int128_t* val) const WARN_UNUSED_RESULT;
402 #endif
403 
422  Status GetString(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
423  Status GetBinary(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT;
425 
447  Status GetString(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
448  Status GetBinary(int col_idx, Slice* val) const WARN_UNUSED_RESULT;
450 
451  //------------------------------------------------------------
452  // Key-encoding related functions
453  //------------------------------------------------------------
454 
466  Status EncodeRowKey(std::string* encoded_key) const;
467 
474  std::string ToEncodedRowKeyOrDie() const;
475 
476  //------------------------------------------------------------
477  // Utility code
478  //------------------------------------------------------------
479 
482  bool IsKeySet() const;
483 
485  bool AllColumnsSet() const;
486 
492  std::string ToString() const;
493 
495  const Schema* schema() const { return schema_; }
496 
497  private:
498  friend class client::KuduWriteOperation; // for row_data_.
499  friend class KeyUtilTest;
500  friend class PartitionSchema;
501  friend class RowOperationsPBDecoder;
502  friend class RowOperationsPBEncoder;
503  friend class TestScanSpec;
504  template<typename KeyTypeWrapper> friend struct client::SliceKeysTestSetup;
505  template<typename KeyTypeWrapper> friend struct client::IntKeysTestSetup;
506  template<typename KeyTypeWrapper> friend struct tablet::SliceTypeRowOps;
507  template<typename KeyTypeWrapper> friend struct tablet::NumTypeRowOps;
508  FRIEND_TEST(KeyUtilTest, TestIncrementInt128PrimaryKey);
509  FRIEND_TEST(PartitionPrunerTest, TestIntPartialPrimaryKeyRangePruning);
510  FRIEND_TEST(PartitionPrunerTest, TestPartialPrimaryKeyRangePruning);
511  FRIEND_TEST(PartitionPrunerTest, TestPrimaryKeyRangePruning);
512 
513  template<typename T>
514  Status Set(const Slice& col_name, const typename T::cpp_type& val,
515  bool owned = false);
516 
517  template<typename T>
518  Status Set(int col_idx, const typename T::cpp_type& val,
519  bool owned = false);
520 
521  // Runtime version of the generic setter.
522  Status Set(int32_t column_idx, const uint8_t* val);
523 
524  template<typename T>
525  Status Get(const Slice& col_name, typename T::cpp_type* val) const;
526 
527  template<typename T>
528  Status Get(int col_idx, typename T::cpp_type* val) const;
529 
530  template<typename T>
531  Status SetSliceCopy(const Slice& col_name, const Slice& val);
532 
533  template<typename T>
534  Status SetSliceCopy(int col_idx, const Slice& val);
535 
536  // If the given column is a variable length column whose memory is owned by this instance,
537  // deallocates the value.
538  // NOTE: Does not mutate the isset bitmap.
539  // REQUIRES: col_idx must be a variable length column.
540  void DeallocateStringIfSet(int col_idx, const ColumnSchema& col);
541 
542  // Deallocate any string/binary values whose memory is managed by this object.
543  void DeallocateOwnedStrings();
544 
545  const Schema* schema_;
546 
547  // 1-bit set for any field which has been explicitly set. This is distinct
548  // from NULL -- an "unset" field will take the server-side default on insert,
549  // whereas a field explicitly set to NULL will override the default.
550  uint8_t* isset_bitmap_;
551 
552  // 1-bit set for any variable length columns whose memory is managed by this instance.
553  // These strings need to be deallocated whenever the value is reset,
554  // or when the instance is destructed.
555  uint8_t* owned_strings_bitmap_;
556 
557  // The normal "contiguous row" format row data. Any column whose data is unset
558  // or NULL can have undefined bytes.
559  uint8_t* row_data_;
560 };
561 
562 } // namespace kudu
563 #endif /* KUDU_COMMON_PARTIAL_ROW_H */
Definition: callbacks.h:28
const Schema * schema() const
Definition: partial_row.h:495
A row which may only contain values for a subset of the columns.
Definition: partial_row.h:64