Kudu C++ client API
schema.h
1 //
2 // Licensed to the Apache Software Foundation (ASF) under one
3 // or more contributor license agreements. See the NOTICE file
4 // distributed with this work for additional information
5 // regarding copyright ownership. The ASF licenses this file
6 // to you under the Apache License, Version 2.0 (the
7 // "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing,
13 // software distributed under the License is distributed on an
14 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, either express or implied. See the License for the
16 // specific language governing permissions and limitations
17 // under the License.
18 #ifndef KUDU_CLIENT_SCHEMA_H
19 #define KUDU_CLIENT_SCHEMA_H
20 
21 // NOTE: using stdint.h instead of cstdint because this file is supposed
22 // to be processed by a compiler lacking C++11 support.
23 #include <stdint.h>
24 
25 #include <cstddef>
26 #include <string>
27 #include <vector>
28 
29 #ifdef KUDU_HEADERS_NO_STUBS
30 #include "kudu/gutil/port.h"
31 #else
32 #include "kudu/client/stubs.h"
33 #endif
34 
35 #include "kudu/util/kudu_export.h"
36 #include "kudu/util/status.h"
37 
38 namespace kudu {
39 
40 class ColumnSchema;
41 struct ColumnSchemaDelta;
42 class KuduPartialRow;
43 class Schema;
44 class Slice;
45 
46 namespace tools {
47 class RemoteKsckCluster;
48 class ReplicaDumper;
49 }
50 
51 namespace client {
52 
53 namespace internal {
54 class GetTableSchemaRpc;
55 class LookupRpc;
56 class MetaCache;
57 class MetaCacheEntry;
58 class WriteRpc;
59 } // namespace internal
60 
61 class KuduSchema;
62 class KuduValue;
63 
65 class KUDU_EXPORT KuduColumnTypeAttributes {
66  public:
68 
74 
81  KuduColumnTypeAttributes(int8_t precision, int8_t scale);
82 
84 
91  KuduColumnTypeAttributes& operator=(const KuduColumnTypeAttributes& other);
92  void CopyFrom(const KuduColumnTypeAttributes& other);
94 
96  int8_t precision() const;
97 
99  int8_t scale() const;
100 
101  private:
102  class KUDU_NO_EXPORT Data;
103  // Owned.
104  Data* data_;
105 };
106 
108 class KUDU_EXPORT KuduColumnStorageAttributes {
109  public:
112  AUTO_ENCODING = 0,
113  PLAIN_ENCODING = 1,
114  PREFIX_ENCODING = 2,
115  RLE = 4,
116  DICT_ENCODING = 5,
117  BIT_SHUFFLE = 6,
118 
121  GROUP_VARINT = 3
122  };
123 
126  DEFAULT_COMPRESSION = 0,
127  NO_COMPRESSION = 1,
128  SNAPPY = 2,
129  LZ4 = 3,
130  ZLIB = 4,
131  };
132 
133 
146  EncodingType encoding = AUTO_ENCODING,
147  CompressionType compression = DEFAULT_COMPRESSION,
148  int32_t block_size = 0)
149  ATTRIBUTE_DEPRECATED("this constructor will be private in a future release")
150  : encoding_(encoding),
151  compression_(compression),
152  block_size_(block_size) {
153  }
154 
156  const EncodingType encoding() const {
157  return encoding_;
158  }
159 
161  const CompressionType compression() const {
162  return compression_;
163  }
164 
166  std::string ToString() const;
167 
168  private:
169  EncodingType encoding_;
170  CompressionType compression_;
171  int32_t block_size_;
172 };
173 
175 class KUDU_EXPORT KuduColumnSchema {
176  public:
178  enum DataType {
179  INT8 = 0,
180  INT16 = 1,
181  INT32 = 2,
182  INT64 = 3,
183  STRING = 4,
184  BOOL = 5,
185  FLOAT = 6,
186  DOUBLE = 7,
187  BINARY = 8,
188  UNIXTIME_MICROS = 9,
189  DECIMAL = 10,
190  TIMESTAMP = UNIXTIME_MICROS
191  };
192 
196  static std::string DataTypeToString(DataType type);
197 
202  KuduColumnSchema(const KuduColumnSchema& other);
203  ~KuduColumnSchema();
204 
210  KuduColumnSchema& operator=(const KuduColumnSchema& other);
211 
216  void CopyFrom(const KuduColumnSchema& other);
217 
223  bool Equals(const KuduColumnSchema& other) const;
224 
231  const std::string& name() const;
232 
234  DataType type() const;
235 
237  bool is_nullable() const;
239 
241  KuduColumnTypeAttributes type_attributes() const;
242 
243  private:
244  friend class KuduColumnSpec;
245  friend class KuduSchema;
246  friend class KuduSchemaBuilder;
247  // KuduTableAlterer::Data needs to be a friend. Friending the parent class
248  // is transitive to nested classes. See https://s.apache.org/inner-class-friends
249  friend class KuduTableAlterer;
250 
252 
253 #if defined(__clang__) || \
254  (defined(__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40600)
255 #pragma GCC diagnostic push
256 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
257 #endif
260  const std::string &name,
261  DataType type,
262  bool is_nullable = false,
263  const void* default_value = NULL, //NOLINT(modernize-use-nullptr)
264  const KuduColumnStorageAttributes& storage_attributes = KuduColumnStorageAttributes(),
265  const KuduColumnTypeAttributes& type_attributes = KuduColumnTypeAttributes());
266 #if defined(__clang__) || \
267  (defined(__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40600)
268 #pragma GCC diagnostic pop
269 #endif
270 
271  // Owned.
272  ColumnSchema* col_;
273 };
274 
283 class KUDU_EXPORT KuduColumnSpec {
284  public:
296  KuduColumnSpec* Default(KuduValue* value);
297 
304 
313 
333  KuduColumnSpec* BlockSize(int32_t block_size);
334 
353  KuduColumnSpec* Precision(int8_t precision);
354 
370  KuduColumnSpec* Scale(int8_t scale);
372 
385  KuduColumnSpec* PrimaryKey();
386 
392  KuduColumnSpec* NotNull();
393 
399  KuduColumnSpec* Nullable();
400 
410 
420  KuduColumnSpec* RemoveDefault();
421 
427  KuduColumnSpec* RenameTo(const std::string& new_name);
429 
430  private:
431  class KUDU_NO_EXPORT Data;
432  friend class KuduSchemaBuilder;
433  friend class KuduTableAlterer;
434 
435  // This class should always be owned and deleted by one of its friends,
436  // not the user.
437  ~KuduColumnSpec();
438 
439  explicit KuduColumnSpec(const std::string& col_name);
440 
441  Status ToColumnSchema(KuduColumnSchema* col) const;
442 
443  Status ToColumnSchemaDelta(ColumnSchemaDelta* col_delta) const;
444 
445  Slice DefaultValueAsSlice() const;
446 
447  // Owned.
448  Data* data_;
449 };
450 
472 class KUDU_EXPORT KuduSchemaBuilder {
473  public:
476 
483  KuduColumnSpec* AddColumn(const std::string& name);
484 
492  KuduSchemaBuilder* SetPrimaryKey(const std::vector<std::string>& key_col_names);
493 
503  Status Build(KuduSchema* schema);
504 
505  private:
506  class KUDU_NO_EXPORT Data;
507  // Owned.
508  Data* data_;
509 };
510 
512 class KUDU_EXPORT KuduSchema {
513  public:
514  KuduSchema();
515 
520  KuduSchema(const KuduSchema& other);
521  ~KuduSchema();
522 
529  KuduSchema& operator=(const KuduSchema& other);
530  void CopyFrom(const KuduSchema& other);
532 
542  Status Reset(const std::vector<KuduColumnSchema>& columns, int key_columns)
543  ATTRIBUTE_DEPRECATED("this method will be removed in a future release")
544  WARN_UNUSED_RESULT;
545 
552  bool Equals(const KuduSchema& other) const;
553 
557  KuduColumnSchema Column(size_t idx) const;
558 
560  size_t num_columns() const;
561 
570  void GetPrimaryKeyColumnIndexes(std::vector<int>* indexes) const;
571 
579  KuduPartialRow* NewRow() const;
580 
584  std::string ToString() const;
585 
587 
595  static KuduSchema FromSchema(const Schema& schema) KUDU_NO_EXPORT;
596 
604  static Schema ToSchema(const KuduSchema& kudu_schema) KUDU_NO_EXPORT;
605 
607 
608  private:
609  friend class ClientTest;
610  friend class KuduClient;
611  friend class KuduScanner;
612  friend class KuduScanToken;
613  friend class KuduScanTokenBuilder;
614  friend class KuduSchemaBuilder;
615  friend class KuduTable;
616  friend class KuduTableCreator;
617  friend class KuduWriteOperation;
618  friend class ScanConfiguration;
619  friend class internal::GetTableSchemaRpc;
620  friend class internal::LookupRpc;
621  friend class internal::MetaCache;
622  friend class internal::MetaCacheEntry;
623  friend class internal::WriteRpc;
624  friend class tools::RemoteKsckCluster;
625  friend class tools::ReplicaDumper;
626 
627  // For use by KuduSchema::FromSchema.
628  explicit KuduSchema(const Schema& schema);
629 
630  // Private since we don't want users to rely on the first N columns
631  // being the keys.
632  size_t num_key_columns() const;
633 
634  // Owned.
635  Schema* schema_;
636 };
637 
638 } // namespace client
639 } // namespace kudu
640 #endif // KUDU_CLIENT_SCHEMA_H
A representation of a table&#39;s schema.
Definition: schema.h:512
Representation of column type attributes.
Definition: schema.h:65
A representation of an operation&#39;s outcome.
Definition: status.h:145
A constant cell value with a specific type.
Definition: value.h:35
Definition: callbacks.h:28
Representation of column storage attributes.
Definition: schema.h:108
Builder API for specifying or altering a column within a table schema.
Definition: schema.h:283
Representation of the column schema.
Definition: schema.h:175
const EncodingType encoding() const
Definition: schema.h:156
Builds scan tokens for a table.
Definition: client.h:2337
Alters an existing table based on the provided steps.
Definition: client.h:1138
KuduColumnStorageAttributes(EncodingType encoding=AUTO_ENCODING, CompressionType compression=DEFAULT_COMPRESSION, int32_t block_size=0) ATTRIBUTE_DEPRECATED("this const ructor will be private in a future release")
Definition: schema.h:145
A handle for a connection to a cluster.
Definition: client.h:316
A wrapper around externally allocated data.
Definition: slice.h:50
A representation of a table on a particular cluster.
Definition: client.h:967
This class is a representation of a single scan.
Definition: client.h:1830
A single-row write operation to be sent to a Kudu table.
Definition: write_op.h:65
CompressionType
Column compression types.
Definition: schema.h:125
Builder API for constructing a KuduSchema object.
Definition: schema.h:472
DataType
Supported data types for columns.
Definition: schema.h:178
A scan descriptor limited to a single physical contiguous location.
Definition: client.h:2277
A helper class to create a new table with the desired options.
Definition: client.h:751
A row which may only contain values for a subset of the columns.
Definition: partial_row.h:64
const CompressionType compression() const
Definition: schema.h:161
EncodingType
Column encoding types.
Definition: schema.h:111