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 
255  const std::string &name,
256  DataType type,
257  bool is_nullable = false,
258  const void* default_value = NULL, //NOLINT(modernize-use-nullptr)
259  const KuduColumnStorageAttributes& storage_attributes = KuduColumnStorageAttributes(),
260  const KuduColumnTypeAttributes& type_attributes = KuduColumnTypeAttributes());
261 
262  // Owned.
263  ColumnSchema* col_;
264 };
265 
274 class KUDU_EXPORT KuduColumnSpec {
275  public:
287  KuduColumnSpec* Default(KuduValue* value);
288 
295 
304 
324  KuduColumnSpec* BlockSize(int32_t block_size);
325 
342  KuduColumnSpec* Precision(int8_t precision);
343 
357  KuduColumnSpec* Scale(int8_t scale);
359 
372  KuduColumnSpec* PrimaryKey();
373 
379  KuduColumnSpec* NotNull();
380 
386  KuduColumnSpec* Nullable();
387 
397 
407  KuduColumnSpec* RemoveDefault();
408 
414  KuduColumnSpec* RenameTo(const std::string& new_name);
416 
417  private:
418  class KUDU_NO_EXPORT Data;
419  friend class KuduSchemaBuilder;
420  friend class KuduTableAlterer;
421 
422  // This class should always be owned and deleted by one of its friends,
423  // not the user.
424  ~KuduColumnSpec();
425 
426  explicit KuduColumnSpec(const std::string& col_name);
427 
428  Status ToColumnSchema(KuduColumnSchema* col) const;
429 
430  Status ToColumnSchemaDelta(ColumnSchemaDelta* col_delta) const;
431 
432  Slice DefaultValueAsSlice() const;
433 
434  // Owned.
435  Data* data_;
436 };
437 
459 class KUDU_EXPORT KuduSchemaBuilder {
460  public:
463 
470  KuduColumnSpec* AddColumn(const std::string& name);
471 
479  KuduSchemaBuilder* SetPrimaryKey(const std::vector<std::string>& key_col_names);
480 
490  Status Build(KuduSchema* schema);
491 
492  private:
493  class KUDU_NO_EXPORT Data;
494  // Owned.
495  Data* data_;
496 };
497 
499 class KUDU_EXPORT KuduSchema {
500  public:
501  KuduSchema();
502 
507  KuduSchema(const KuduSchema& other);
508  ~KuduSchema();
509 
516  KuduSchema& operator=(const KuduSchema& other);
517  void CopyFrom(const KuduSchema& other);
519 
529  Status Reset(const std::vector<KuduColumnSchema>& columns, int key_columns)
530  ATTRIBUTE_DEPRECATED("this method will be removed in a future release")
531  WARN_UNUSED_RESULT;
532 
539  bool Equals(const KuduSchema& other) const;
540 
544  KuduColumnSchema Column(size_t idx) const;
545 
547  size_t num_columns() const;
548 
557  void GetPrimaryKeyColumnIndexes(std::vector<int>* indexes) const;
558 
566  KuduPartialRow* NewRow() const;
567 
571  std::string ToString() const;
572 
573  private:
574  friend class ClientTest;
575  friend class KuduClient;
576  friend class KuduScanner;
577  friend class KuduScanToken;
578  friend class KuduScanTokenBuilder;
579  friend class KuduSchemaBuilder;
580  friend class KuduTable;
581  friend class KuduTableCreator;
582  friend class KuduWriteOperation;
583  friend class ScanConfiguration;
584  friend class internal::GetTableSchemaRpc;
585  friend class internal::LookupRpc;
586  friend class internal::MetaCache;
587  friend class internal::MetaCacheEntry;
588  friend class internal::WriteRpc;
589  friend class tools::RemoteKsckCluster;
590  friend class tools::ReplicaDumper;
591 
592  friend KuduSchema KuduSchemaFromSchema(const Schema& schema);
593  friend Schema SchemaFromKuduSchema(const KuduSchema& schema);
594 
595  // For use by kudu tests.
596  explicit KuduSchema(const Schema& schema);
597 
598  // Private since we don't want users to rely on the first N columns
599  // being the keys.
600  size_t num_key_columns() const;
601 
602  // Owned.
603  Schema* schema_;
604 };
605 
606 } // namespace client
607 } // namespace kudu
608 #endif // KUDU_CLIENT_SCHEMA_H
A row which may only contain values for a subset of the columns.
Definition: partial_row.h:64
const EncodingType encoding() const
Definition: schema.h:156
Definition: callbacks.h:28
A helper class to create a new table with the desired options.
Definition: client.h:704
A constant cell value with a specific type.
Definition: value.h:35
Alters an existing table based on the provided steps.
Definition: client.h:1076
Builds scan tokens for a table.
Definition: client.h:2263
Builder API for specifying or altering a column within a table schema.
Definition: schema.h:274
Representation of column storage attributes.
Definition: schema.h:108
DataType
Supported data types for columns.
Definition: schema.h:178
Builder API for constructing a KuduSchema object.
Definition: schema.h:459
A handle for a connection to a cluster.
Definition: client.h:314
A representation of a table&#39;s schema.
Definition: schema.h:499
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 wrapper around externally allocated data.
Definition: slice.h:50
A representation of a table on a particular cluster.
Definition: client.h:920
A representation of an operation&#39;s outcome.
Definition: status.h:145
A scan descriptor limited to a single physical contiguous location.
Definition: client.h:2203
A single-row write operation to be sent to a Kudu table.
Definition: write_op.h:65
Representation of column type attributes.
Definition: schema.h:65
Representation of the column schema.
Definition: schema.h:175
EncodingType
Column encoding types.
Definition: schema.h:111
This class is a representation of a single scan.
Definition: client.h:1762
CompressionType
Column compression types.
Definition: schema.h:125
const CompressionType compression() const
Definition: schema.h:161