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 #include <string>
22 #include <vector>
23 
24 #include "kudu/client/value.h"
25 #include "kudu/util/kudu_export.h"
26 
27 namespace kudu {
28 
29 class ColumnSchema;
30 struct ColumnSchemaDelta;
31 class KuduPartialRow;
32 class Schema;
33 class TestWorkload;
34 
35 namespace tools {
36 class RemoteKsckMaster;
37 class ReplicaDumper;
38 }
39 
40 namespace client {
41 class ClientTest;
42 
43 namespace internal {
44 class GetTableSchemaRpc;
45 class LookupRpc;
46 class MetaCacheEntry;
47 class WriteRpc;
48 } // namespace internal
49 
50 class KuduClient;
51 class KuduSchema;
52 class KuduSchemaBuilder;
53 class KuduWriteOperation;
54 
56 class KUDU_EXPORT KuduColumnStorageAttributes {
57  public:
59  enum EncodingType {
60  AUTO_ENCODING = 0,
61  PLAIN_ENCODING = 1,
62  PREFIX_ENCODING = 2,
63  RLE = 4,
64  DICT_ENCODING = 5,
65  BIT_SHUFFLE = 6,
66 
69  GROUP_VARINT = 3
70  };
71 
74  DEFAULT_COMPRESSION = 0,
75  NO_COMPRESSION = 1,
76  SNAPPY = 2,
77  LZ4 = 3,
78  ZLIB = 4,
79  };
80 
81 
94  EncodingType encoding = AUTO_ENCODING,
95  CompressionType compression = DEFAULT_COMPRESSION,
96  int32_t block_size = 0)
97  ATTRIBUTE_DEPRECATED("this constructor will be private in a future release")
98  : encoding_(encoding),
99  compression_(compression),
100  block_size_(block_size) {
101  }
102 
104  const EncodingType encoding() const {
105  return encoding_;
106  }
107 
109  const CompressionType compression() const {
110  return compression_;
111  }
112 
114  std::string ToString() const;
115 
116  private:
117  EncodingType encoding_;
118  CompressionType compression_;
119  int32_t block_size_;
120 };
121 
123 class KUDU_EXPORT KuduColumnSchema {
124  public:
126  enum DataType {
127  INT8 = 0,
128  INT16 = 1,
129  INT32 = 2,
130  INT64 = 3,
131  STRING = 4,
132  BOOL = 5,
133  FLOAT = 6,
134  DOUBLE = 7,
135  BINARY = 8,
136  UNIXTIME_MICROS = 9,
137  TIMESTAMP = UNIXTIME_MICROS
138  };
139 
143  static std::string DataTypeToString(DataType type);
144 
161  KuduColumnSchema(const std::string &name,
162  DataType type,
163  bool is_nullable = false,
164  const void* default_value = NULL,
166  ATTRIBUTE_DEPRECATED("use KuduSchemaBuilder instead");
167 
172  KuduColumnSchema(const KuduColumnSchema& other);
173  ~KuduColumnSchema();
174 
180  KuduColumnSchema& operator=(const KuduColumnSchema& other);
181 
186  void CopyFrom(const KuduColumnSchema& other);
187 
193  bool Equals(const KuduColumnSchema& other) const;
194 
201  const std::string& name() const;
202 
204  DataType type() const;
205 
207  bool is_nullable() const;
209 
210  private:
211  friend class KuduColumnSpec;
212  friend class KuduSchema;
213  friend class KuduSchemaBuilder;
214  // KuduTableAlterer::Data needs to be a friend. Friending the parent class
215  // is transitive to nested classes. See http://tiny.cloudera.com/jwtui
216  friend class KuduTableAlterer;
217 
219 
220  // Owned.
221  ColumnSchema* col_;
222 };
223 
232 class KUDU_EXPORT KuduColumnSpec {
233  public:
245  KuduColumnSpec* Default(KuduValue* value);
246 
252  KuduColumnSpec* Compression(KuduColumnStorageAttributes::CompressionType compression);
253 
261  KuduColumnSpec* Encoding(KuduColumnStorageAttributes::EncodingType encoding);
262 
282  KuduColumnSpec* BlockSize(int32_t block_size);
283 
296  KuduColumnSpec* PrimaryKey();
297 
303  KuduColumnSpec* NotNull();
304 
310  KuduColumnSpec* Nullable();
311 
319  KuduColumnSpec* Type(KuduColumnSchema::DataType type);
321 
331  KuduColumnSpec* RemoveDefault();
332 
338  KuduColumnSpec* RenameTo(const std::string& new_name);
340 
341  private:
342  class KUDU_NO_EXPORT Data;
343  friend class KuduSchemaBuilder;
344  friend class KuduTableAlterer;
345 
346  // This class should always be owned and deleted by one of its friends,
347  // not the user.
348  ~KuduColumnSpec();
349 
350  explicit KuduColumnSpec(const std::string& col_name);
351 
352  Status ToColumnSchema(KuduColumnSchema* col) const;
353 
354  Status ToColumnSchemaDelta(ColumnSchemaDelta* col_delta) const;
355 
356  Slice DefaultValueAsSlice() const;
357 
358  // Owned.
359  Data* data_;
360 };
361 
383 class KUDU_EXPORT KuduSchemaBuilder {
384  public:
387 
394  KuduColumnSpec* AddColumn(const std::string& name);
395 
403  KuduSchemaBuilder* SetPrimaryKey(const std::vector<std::string>& key_col_names);
404 
414  Status Build(KuduSchema* schema);
415 
416  private:
417  class KUDU_NO_EXPORT Data;
418  // Owned.
419  Data* data_;
420 };
421 
423 class KUDU_EXPORT KuduSchema {
424  public:
425  KuduSchema();
426 
431  KuduSchema(const KuduSchema& other);
432  ~KuduSchema();
433 
440  KuduSchema& operator=(const KuduSchema& other);
441  void CopyFrom(const KuduSchema& other);
443 
453  Status Reset(const std::vector<KuduColumnSchema>& columns, int key_columns)
454  ATTRIBUTE_DEPRECATED("this method will be removed in a future release")
455  WARN_UNUSED_RESULT;
456 
463  bool Equals(const KuduSchema& other) const;
464 
468  KuduColumnSchema Column(size_t idx) const;
469 
471  size_t num_columns() const;
472 
481  void GetPrimaryKeyColumnIndexes(std::vector<int>* indexes) const;
482 
490  KuduPartialRow* NewRow() const;
491 
492  private:
493  friend class ClientTest;
494  friend class KuduClient;
495  friend class KuduScanner;
496  friend class KuduScanToken;
497  friend class KuduScanTokenBuilder;
498  friend class KuduSchemaBuilder;
499  friend class KuduTable;
500  friend class KuduTableCreator;
501  friend class KuduWriteOperation;
502  friend class ScanConfiguration;
503  friend class internal::GetTableSchemaRpc;
504  friend class internal::LookupRpc;
505  friend class internal::MetaCacheEntry;
506  friend class internal::WriteRpc;
507  friend class tools::RemoteKsckMaster;
508  friend class tools::ReplicaDumper;
509 
510  friend KuduSchema KuduSchemaFromSchema(const Schema& schema);
511 
512 
513  // For use by kudu tests.
514  explicit KuduSchema(const Schema& schema);
515 
516  // Private since we don't want users to rely on the first N columns
517  // being the keys.
518  size_t num_key_columns() const;
519 
520  // Owned.
521  Schema* schema_;
522 };
523 
524 } // namespace client
525 } // namespace kudu
526 #endif // KUDU_CLIENT_SCHEMA_H
A representation of a table&#39;s schema.
Definition: schema.h:423
A representation of an operation&#39;s outcome.
Definition: status.h:130
A constant cell value with a specific type.
Definition: value.h:33
Definition: callbacks.h:28
Representation of column storage attributes.
Definition: schema.h:56
Builder API for specifying or altering a column within a table schema.
Definition: schema.h:232
Representation of the column schema.
Definition: schema.h:123
const EncodingType encoding() const
Definition: schema.h:104
Builds scan tokens for a table.
Definition: client.h:2173
Alters an existing table based on the provided steps.
Definition: client.h:1018
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:93
A handle for a connection to a cluster.
Definition: client.h:289
A wrapper around externally allocated data.
Definition: slice.h:43
A representation of a table on a particular cluster.
Definition: client.h:862
This class is a representation of a single scan.
Definition: client.h:1695
A single-row write operation to be sent to a Kudu table.
Definition: write_op.h:56
CompressionType
Column compression types.
Definition: schema.h:73
Builder API for constructing a KuduSchema object.
Definition: schema.h:383
DataType
Supported data types for columns.
Definition: schema.h:126
A scan descriptor limited to a single physical contiguous location.
Definition: client.h:2113
A helper class to create a new table with the desired options.
Definition: client.h:646
A row which may only contain values for a subset of the columns.
Definition: partial_row.h:61
const CompressionType compression() const
Definition: schema.h:109
EncodingType
Column encoding types.
Definition: schema.h:59