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 <gtest/gtest_prod.h>
31 
32 #include "kudu/gutil/port.h"
33 #else
34 #include "kudu/client/stubs.h"
35 #endif
36 
37 #include "kudu/util/kudu_export.h"
38 #include "kudu/util/status.h"
39 
40 namespace kudu {
41 
42 class ColumnSchema;
43 class KuduPartialRow;
44 class Schema;
45 class Slice;
46 struct ColumnSchemaDelta;
47 
48 namespace tools {
49 class RemoteKsckCluster;
50 class ReplicaDumper;
51 }
52 
53 namespace client {
54 
55 namespace internal {
56 class GetTableSchemaRpc;
57 class LookupRpc;
58 class MetaCache;
59 class MetaCacheEntry;
60 class WriteRpc;
61 } // namespace internal
62 
63 class KuduSchema;
64 class KuduValue;
65 
67 class KUDU_EXPORT KuduColumnTypeAttributes {
68  public:
70 
76 
83  KuduColumnTypeAttributes(int8_t precision, int8_t scale);
84 
89  explicit KuduColumnTypeAttributes(uint16_t length);
90 
92 
99  KuduColumnTypeAttributes& operator=(const KuduColumnTypeAttributes& other);
100  void CopyFrom(const KuduColumnTypeAttributes& other);
102 
104  int8_t precision() const;
105 
107  int8_t scale() const;
108 
110  uint16_t length() const;
111 
112  private:
113  friend class KuduColumnSchema;
114  friend class KuduColumnSpec;
115  friend class KuduSchema;
116 
117  KuduColumnTypeAttributes(int8_t precision, int8_t scale, uint16_t length);
118 
119  class KUDU_NO_EXPORT Data;
120 
121  // Owned.
122  Data* data_;
123 };
124 
126 class KUDU_EXPORT KuduColumnStorageAttributes {
127  public:
130  AUTO_ENCODING = 0,
131  PLAIN_ENCODING = 1,
132  PREFIX_ENCODING = 2,
133  RLE = 4,
134  DICT_ENCODING = 5,
135  BIT_SHUFFLE = 6,
136 
139  GROUP_VARINT = 3
140  };
141 
144  DEFAULT_COMPRESSION = 0,
145  NO_COMPRESSION = 1,
146  SNAPPY = 2,
147  LZ4 = 3,
148  ZLIB = 4,
149  };
150 
151 
164  EncodingType encoding = AUTO_ENCODING,
165  CompressionType compression = DEFAULT_COMPRESSION,
166  int32_t block_size = 0)
167  ATTRIBUTE_DEPRECATED("this constructor will be private in a future release")
168  : encoding_(encoding),
169  compression_(compression),
170  block_size_(block_size) {
171  }
172 
174  const EncodingType encoding() const {
175  return encoding_;
176  }
177 
179  const CompressionType compression() const {
180  return compression_;
181  }
182 
184  std::string ToString() const;
185 
192  static Status StringToEncodingType(const std::string& encoding,
193  EncodingType* type);
194 
201  static Status StringToCompressionType(const std::string& compression,
202  CompressionType* type);
203 
204  private:
205  EncodingType encoding_;
206  CompressionType compression_;
207  int32_t block_size_;
208 };
209 
211 class KUDU_EXPORT KuduColumnSchema {
212  public:
214  enum DataType {
215  INT8 = 0,
216  INT16 = 1,
217  INT32 = 2,
218  INT64 = 3,
219  STRING = 4,
220  BOOL = 5,
221  FLOAT = 6,
222  DOUBLE = 7,
223  BINARY = 8,
224  UNIXTIME_MICROS = 9,
225  DECIMAL = 10,
226  VARCHAR = 11,
227  TIMESTAMP = UNIXTIME_MICROS,
228  DATE = 12
229  };
230 
234  static std::string DataTypeToString(DataType type);
235 
241  static Status StringToDataType(const std::string& type_str, DataType* type);
242 
247  KuduColumnSchema(const KuduColumnSchema& other);
248  ~KuduColumnSchema();
249 
255  KuduColumnSchema& operator=(const KuduColumnSchema& other);
256 
261  void CopyFrom(const KuduColumnSchema& other);
262 
268  bool Equals(const KuduColumnSchema& other) const;
269 
276  const std::string& name() const;
277 
279  DataType type() const;
280 
282  bool is_nullable() const;
284 
286  KuduColumnTypeAttributes type_attributes() const;
287 
291  const std::string& comment() const;
292 
293  private:
294  friend class KuduColumnSpec;
295  friend class KuduSchema;
296  friend class KuduSchemaBuilder;
297  // KuduTableAlterer::Data needs to be a friend. Friending the parent class
298  // is transitive to nested classes. See https://s.apache.org/inner-class-friends
299  friend class KuduTableAlterer;
300 
301 #ifdef KUDU_HEADERS_NO_STUBS
302  FRIEND_TEST(KuduColumnSchemaTest, TestEquals);
303 #endif
304 
306 
307 #if defined(__clang__) || \
308  (defined(__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40600)
309 #pragma GCC diagnostic push
310 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
311 #endif
314  const std::string &name,
315  DataType type,
316  bool is_nullable = false,
317  const void* default_value = NULL, //NOLINT(modernize-use-nullptr)
318  const KuduColumnStorageAttributes& storage_attributes = KuduColumnStorageAttributes(),
319  const KuduColumnTypeAttributes& type_attributes = KuduColumnTypeAttributes(),
320  const std::string& comment = "");
321 #if defined(__clang__) || \
322  (defined(__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40600)
323 #pragma GCC diagnostic pop
324 #endif
325 
326  // Owned.
327  ColumnSchema* col_;
328 };
329 
338 class KUDU_EXPORT KuduColumnSpec {
339  public:
351  KuduColumnSpec* Default(KuduValue* value);
352 
359 
368 
388  KuduColumnSpec* BlockSize(int32_t block_size);
389 
408  KuduColumnSpec* Precision(int8_t precision);
409 
425  KuduColumnSpec* Scale(int8_t scale);
427 
443  KuduColumnSpec* Length(uint16_t length);
445 
458  KuduColumnSpec* PrimaryKey();
459 
465  KuduColumnSpec* NotNull();
466 
472  KuduColumnSpec* Nullable();
473 
483 
493  KuduColumnSpec* RemoveDefault();
494 
500  KuduColumnSpec* RenameTo(const std::string& new_name);
502 
508  KuduColumnSpec* Comment(const std::string& comment);
509 
510  private:
511  class KUDU_NO_EXPORT Data;
512 
513  friend class KuduSchemaBuilder;
514  friend class KuduTableAlterer;
515 
516  // This class should always be owned and deleted by one of its friends,
517  // not the user.
518  ~KuduColumnSpec();
519 
520  explicit KuduColumnSpec(const std::string& col_name);
521 
522  Status ToColumnSchema(KuduColumnSchema* col) const;
523 
524  Status ToColumnSchemaDelta(ColumnSchemaDelta* col_delta) const;
525 
526  Slice DefaultValueAsSlice() const;
527 
528  // Owned.
529  Data* data_;
530 };
531 
553 class KUDU_EXPORT KuduSchemaBuilder {
554  public:
557 
564  KuduColumnSpec* AddColumn(const std::string& name);
565 
573  KuduSchemaBuilder* SetPrimaryKey(const std::vector<std::string>& key_col_names);
574 
584  Status Build(KuduSchema* schema);
585 
586  private:
587  class KUDU_NO_EXPORT Data;
588 
589  // Owned.
590  Data* data_;
591 };
592 
594 class KUDU_EXPORT KuduSchema {
595  public:
596  KuduSchema();
597 
602  KuduSchema(const KuduSchema& other);
603  ~KuduSchema();
604 
611  KuduSchema& operator=(const KuduSchema& other);
612  void CopyFrom(const KuduSchema& other);
614 
624  Status Reset(const std::vector<KuduColumnSchema>& columns, int key_columns)
625  ATTRIBUTE_DEPRECATED("this method will be removed in a future release")
626  WARN_UNUSED_RESULT;
627 
634  bool Equals(const KuduSchema& other) const;
635 
639  KuduColumnSchema Column(size_t idx) const;
640 
646  bool HasColumn(const std::string& col_name, KuduColumnSchema* col_schema) const;
647 
649  size_t num_columns() const;
650 
659  void GetPrimaryKeyColumnIndexes(std::vector<int>* indexes) const;
660 
668  KuduPartialRow* NewRow() const;
669 
673  std::string ToString() const;
674 
676 
684  static KuduSchema FromSchema(const Schema& schema) KUDU_NO_EXPORT;
685 
693  static Schema ToSchema(const KuduSchema& kudu_schema) KUDU_NO_EXPORT;
694 
696 
697  private:
698  friend class ClientTest;
699  friend class KuduClient;
700  friend class KuduScanner;
701  friend class KuduScanToken;
702  friend class KuduScanTokenBuilder;
703  friend class KuduSchemaBuilder;
704  friend class KuduTable;
705  friend class KuduTableCreator;
706  friend class KuduWriteOperation;
707  friend class ScanConfiguration;
708  friend class internal::GetTableSchemaRpc;
709  friend class internal::LookupRpc;
710  friend class internal::MetaCache;
711  friend class internal::MetaCacheEntry;
712  friend class internal::WriteRpc;
713  friend class tools::RemoteKsckCluster;
714  friend class tools::ReplicaDumper;
715 
716  // For use by KuduSchema::FromSchema.
717  explicit KuduSchema(const Schema& schema);
718 #if __cplusplus >= 201103
719  explicit KuduSchema(Schema&& schema);
720 #endif
721 
722  // Private since we don't want users to rely on the first N columns
723  // being the keys.
724  size_t num_key_columns() const;
725 
726  // Owned.
727  Schema* schema_;
728 };
729 
730 } // namespace client
731 } // namespace kudu
732 #endif // KUDU_CLIENT_SCHEMA_H
A representation of a table&#39;s schema.
Definition: schema.h:594
Representation of column type attributes.
Definition: schema.h:67
A representation of an operation&#39;s outcome.
Definition: status.h:165
A constant cell value with a specific type.
Definition: value.h:35
Definition: callbacks.h:28
Representation of column storage attributes.
Definition: schema.h:126
Builder API for specifying or altering a column within a table schema.
Definition: schema.h:338
Representation of the column schema.
Definition: schema.h:211
const EncodingType encoding() const
Definition: schema.h:174
Builds scan tokens for a table.
Definition: client.h:2564
Alters an existing table based on the provided steps.
Definition: client.h:1268
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:163
A handle for a connection to a cluster.
Definition: client.h:327
A wrapper around externally allocated data.
Definition: slice.h:51
A representation of a table on a particular cluster.
Definition: client.h:1020
This class is a representation of a single scan.
Definition: client.h:2012
A single-row write operation to be sent to a Kudu table.
Definition: write_op.h:66
CompressionType
Column compression types.
Definition: schema.h:143
Builder API for constructing a KuduSchema object.
Definition: schema.h:553
DataType
Supported data types for columns.
Definition: schema.h:214
A scan descriptor limited to a single physical contiguous location.
Definition: client.h:2504
A helper class to create a new table with the desired options.
Definition: client.h:748
A row which may only contain values for a subset of the columns.
Definition: partial_row.h:72
const CompressionType compression() const
Definition: schema.h:179
EncodingType
Column encoding types.
Definition: schema.h:129