Kudu C++ client API
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 struct ColumnSchemaDelta;
44 class KuduPartialRow;
45 class Schema;
46 class Slice;
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 
86 
93  KuduColumnTypeAttributes& operator=(const KuduColumnTypeAttributes& other);
94  void CopyFrom(const KuduColumnTypeAttributes& other);
96 
98  int8_t precision() const;
99 
101  int8_t scale() const;
102 
103  private:
104  class KUDU_NO_EXPORT Data;
105  // Owned.
106  Data* data_;
107 };
108 
110 class KUDU_EXPORT KuduColumnStorageAttributes {
111  public:
114  AUTO_ENCODING = 0,
115  PLAIN_ENCODING = 1,
116  PREFIX_ENCODING = 2,
117  RLE = 4,
118  DICT_ENCODING = 5,
119  BIT_SHUFFLE = 6,
120 
123  GROUP_VARINT = 3
124  };
125 
128  DEFAULT_COMPRESSION = 0,
129  NO_COMPRESSION = 1,
130  SNAPPY = 2,
131  LZ4 = 3,
132  ZLIB = 4,
133  };
134 
135 
148  EncodingType encoding = AUTO_ENCODING,
149  CompressionType compression = DEFAULT_COMPRESSION,
150  int32_t block_size = 0)
151  ATTRIBUTE_DEPRECATED("this constructor will be private in a future release")
152  : encoding_(encoding),
153  compression_(compression),
154  block_size_(block_size) {
155  }
156 
158  const EncodingType encoding() const {
159  return encoding_;
160  }
161 
163  const CompressionType compression() const {
164  return compression_;
165  }
166 
168  std::string ToString() const;
169 
170  private:
171  EncodingType encoding_;
172  CompressionType compression_;
173  int32_t block_size_;
174 };
175 
177 class KUDU_EXPORT KuduColumnSchema {
178  public:
180  enum DataType {
181  INT8 = 0,
182  INT16 = 1,
183  INT32 = 2,
184  INT64 = 3,
185  STRING = 4,
186  BOOL = 5,
187  FLOAT = 6,
188  DOUBLE = 7,
189  BINARY = 8,
190  UNIXTIME_MICROS = 9,
191  DECIMAL = 10,
192  TIMESTAMP = UNIXTIME_MICROS
193  };
194 
198  static std::string DataTypeToString(DataType type);
199 
204  KuduColumnSchema(const KuduColumnSchema& other);
205  ~KuduColumnSchema();
206 
212  KuduColumnSchema& operator=(const KuduColumnSchema& other);
213 
218  void CopyFrom(const KuduColumnSchema& other);
219 
225  bool Equals(const KuduColumnSchema& other) const;
226 
233  const std::string& name() const;
234 
236  DataType type() const;
237 
239  bool is_nullable() const;
241 
243  KuduColumnTypeAttributes type_attributes() const;
244 
248  const std::string& comment() const;
249 
250  private:
251  friend class KuduColumnSpec;
252  friend class KuduSchema;
253  friend class KuduSchemaBuilder;
254  // KuduTableAlterer::Data needs to be a friend. Friending the parent class
255  // is transitive to nested classes. See https://s.apache.org/inner-class-friends
256  friend class KuduTableAlterer;
257 
258 #ifdef KUDU_HEADERS_NO_STUBS
259  FRIEND_TEST(KuduColumnSchemaTest, TestEquals);
260 #endif
261 
263 
264 #if defined(__clang__) || \
265  (defined(__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40600)
266 #pragma GCC diagnostic push
267 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
268 #endif
271  const std::string &name,
272  DataType type,
273  bool is_nullable = false,
274  const void* default_value = NULL, //NOLINT(modernize-use-nullptr)
275  const KuduColumnStorageAttributes& storage_attributes = KuduColumnStorageAttributes(),
276  const KuduColumnTypeAttributes& type_attributes = KuduColumnTypeAttributes(),
277  const std::string& comment = "");
278 #if defined(__clang__) || \
279  (defined(__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40600)
280 #pragma GCC diagnostic pop
281 #endif
282 
283  // Owned.
284  ColumnSchema* col_;
285 };
286 
295 class KUDU_EXPORT KuduColumnSpec {
296  public:
308  KuduColumnSpec* Default(KuduValue* value);
309 
316 
325 
345  KuduColumnSpec* BlockSize(int32_t block_size);
346 
365  KuduColumnSpec* Precision(int8_t precision);
366 
382  KuduColumnSpec* Scale(int8_t scale);
384 
397  KuduColumnSpec* PrimaryKey();
398 
404  KuduColumnSpec* NotNull();
405 
411  KuduColumnSpec* Nullable();
412 
422 
432  KuduColumnSpec* RemoveDefault();
433 
439  KuduColumnSpec* RenameTo(const std::string& new_name);
441 
447  KuduColumnSpec* Comment(const std::string& comment);
448 
449  private:
450  class KUDU_NO_EXPORT Data;
451  friend class KuduSchemaBuilder;
452  friend class KuduTableAlterer;
453 
454  // This class should always be owned and deleted by one of its friends,
455  // not the user.
456  ~KuduColumnSpec();
457 
458  explicit KuduColumnSpec(const std::string& col_name);
459 
460  Status ToColumnSchema(KuduColumnSchema* col) const;
461 
462  Status ToColumnSchemaDelta(ColumnSchemaDelta* col_delta) const;
463 
464  Slice DefaultValueAsSlice() const;
465 
466  // Owned.
467  Data* data_;
468 };
469 
491 class KUDU_EXPORT KuduSchemaBuilder {
492  public:
495 
502  KuduColumnSpec* AddColumn(const std::string& name);
503 
511  KuduSchemaBuilder* SetPrimaryKey(const std::vector<std::string>& key_col_names);
512 
522  Status Build(KuduSchema* schema);
523 
524  private:
525  class KUDU_NO_EXPORT Data;
526  // Owned.
527  Data* data_;
528 };
529 
531 class KUDU_EXPORT KuduSchema {
532  public:
533  KuduSchema();
534 
539  KuduSchema(const KuduSchema& other);
540  ~KuduSchema();
541 
548  KuduSchema& operator=(const KuduSchema& other);
549  void CopyFrom(const KuduSchema& other);
551 
561  Status Reset(const std::vector<KuduColumnSchema>& columns, int key_columns)
562  ATTRIBUTE_DEPRECATED("this method will be removed in a future release")
563  WARN_UNUSED_RESULT;
564 
571  bool Equals(const KuduSchema& other) const;
572 
576  KuduColumnSchema Column(size_t idx) const;
577 
579  size_t num_columns() const;
580 
589  void GetPrimaryKeyColumnIndexes(std::vector<int>* indexes) const;
590 
598  KuduPartialRow* NewRow() const;
599 
603  std::string ToString() const;
604 
606 
614  static KuduSchema FromSchema(const Schema& schema) KUDU_NO_EXPORT;
615 
623  static Schema ToSchema(const KuduSchema& kudu_schema) KUDU_NO_EXPORT;
624 
626 
627  private:
628  friend class ClientTest;
629  friend class KuduClient;
630  friend class KuduScanner;
631  friend class KuduScanToken;
632  friend class KuduScanTokenBuilder;
633  friend class KuduSchemaBuilder;
634  friend class KuduTable;
635  friend class KuduTableCreator;
636  friend class KuduWriteOperation;
637  friend class ScanConfiguration;
638  friend class internal::GetTableSchemaRpc;
639  friend class internal::LookupRpc;
640  friend class internal::MetaCache;
641  friend class internal::MetaCacheEntry;
642  friend class internal::WriteRpc;
643  friend class tools::RemoteKsckCluster;
644  friend class tools::ReplicaDumper;
645 
646  // For use by KuduSchema::FromSchema.
647  explicit KuduSchema(const Schema& schema);
648 
649  // Private since we don't want users to rely on the first N columns
650  // being the keys.
651  size_t num_key_columns() const;
652 
653  // Owned.
654  Schema* schema_;
655 };
656 
657 } // namespace client
658 } // namespace kudu
659 #endif // KUDU_CLIENT_SCHEMA_H
A representation of a table&#39;s schema.
Definition: schema.h:531
Representation of column type attributes.
Definition: schema.h:67
A representation of an operation&#39;s outcome.
Definition: status.h:145
A constant cell value with a specific type.
Definition: value.h:35
Representation of column storage attributes.
Definition: schema.h:110
Builder API for specifying or altering a column within a table schema.
Definition: schema.h:295
Representation of the column schema.
Definition: schema.h:177
const EncodingType encoding() const
Definition: schema.h:158
Builds scan tokens for a table.
Definition: client.h:2357
Alters an existing table based on the provided steps.
Definition: client.h:1129
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:147
A handle for a connection to a cluster.
Definition: client.h:319
A wrapper around externally allocated data.
Definition: slice.h:50
A representation of a table on a particular cluster.
Definition: client.h:954
This class is a representation of a single scan.
Definition: client.h:1832
A single-row write operation to be sent to a Kudu table.
Definition: write_op.h:65
CompressionType
Column compression types.
Definition: schema.h:127
Builder API for constructing a KuduSchema object.
Definition: schema.h:491
DataType
Supported data types for columns.
Definition: schema.h:180
A scan descriptor limited to a single physical contiguous location.
Definition: client.h:2297
A helper class to create a new table with the desired options.
Definition: client.h:729
A row which may only contain values for a subset of the columns.
Definition: partial_row.h:68
const CompressionType compression() const
Definition: schema.h:163
EncodingType
Column encoding types.
Definition: schema.h:113