|
| 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 | + */ |
| 19 | + |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include <map> |
| 23 | +#include <memory> |
| 24 | +#include <string> |
| 25 | +#include <string_view> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +#include "iceberg/error.h" |
| 29 | +#include "iceberg/expected.h" |
| 30 | +#include "iceberg/table_identifier.h" |
| 31 | +#include "iceberg/type_fwd.h" |
| 32 | + |
| 33 | +namespace iceberg { |
| 34 | + |
| 35 | +/// \brief A Catalog API for table create, drop, and load operations. |
| 36 | +/// |
| 37 | +/// Note that these functions are named after the corresponding operationId |
| 38 | +/// specified by the Iceberg Rest Catalog API. |
| 39 | +class ICEBERG_EXPORT Catalog { |
| 40 | + public: |
| 41 | + virtual ~Catalog() = default; |
| 42 | + |
| 43 | + /// \brief Return the name for this catalog |
| 44 | + virtual std::string_view name() const = 0; |
| 45 | + |
| 46 | + /// \brief Return all the identifiers under this namespace |
| 47 | + /// |
| 48 | + /// \param ns a namespace |
| 49 | + /// \return a list of identifiers for tables or ErrorKind::kNoSuchNamespace |
| 50 | + /// if the namespace does not exist |
| 51 | + virtual expected<std::vector<TableIdentifier>, Error> ListTables( |
| 52 | + const Namespace& ns) const = 0; |
| 53 | + |
| 54 | + /// \brief Create a table |
| 55 | + /// |
| 56 | + /// \param identifier a table identifier |
| 57 | + /// \param schema a schema |
| 58 | + /// \param spec a partition spec |
| 59 | + /// \param location a location for the table; leave empty if unspecified |
| 60 | + /// \param properties a string map of table properties |
| 61 | + /// \return a Table instance or ErrorKind::kAlreadyExists if the table already exists |
| 62 | + virtual expected<std::unique_ptr<Table>, Error> CreateTable( |
| 63 | + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, |
| 64 | + const std::string& location, |
| 65 | + const std::map<std::string, std::string>& properties) = 0; |
| 66 | + |
| 67 | + /// \brief Update a table |
| 68 | + /// |
| 69 | + /// \param identifier a table identifier |
| 70 | + /// \param requirements a list of table requirements |
| 71 | + /// \param updates a list of table updates |
| 72 | + /// \return a Table instance or ErrorKind::kAlreadyExists if the table already exists |
| 73 | + virtual expected<std::unique_ptr<Table>, Error> UpdateTable( |
| 74 | + const TableIdentifier& identifier, |
| 75 | + const std::vector<std::unique_ptr<UpdateRequirement>>& requirements, |
| 76 | + const std::vector<std::unique_ptr<MetadataUpdate>>& updates) = 0; |
| 77 | + |
| 78 | + /// \brief Start a transaction to create a table |
| 79 | + /// |
| 80 | + /// \param identifier a table identifier |
| 81 | + /// \param schema a schema |
| 82 | + /// \param spec a partition spec |
| 83 | + /// \param location a location for the table; leave empty if unspecified |
| 84 | + /// \param properties a string map of table properties |
| 85 | + /// \return a Transaction to create the table or ErrorKind::kAlreadyExists if the table |
| 86 | + /// already exists |
| 87 | + virtual expected<std::shared_ptr<Transaction>, Error> StageCreateTable( |
| 88 | + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, |
| 89 | + const std::string& location, |
| 90 | + const std::map<std::string, std::string>& properties) = 0; |
| 91 | + |
| 92 | + /// \brief Check whether table exists |
| 93 | + /// |
| 94 | + /// \param identifier a table identifier |
| 95 | + /// \return true if the table exists, false otherwise |
| 96 | + virtual bool TableExists(const TableIdentifier& identifier) const = 0; |
| 97 | + |
| 98 | + /// \brief Drop a table; optionally delete data and metadata files |
| 99 | + /// |
| 100 | + /// If purge is set to true the implementation should delete all data and metadata |
| 101 | + /// files. |
| 102 | + /// |
| 103 | + /// \param identifier a table identifier |
| 104 | + /// \param purge if true, delete all data and metadata files in the table |
| 105 | + /// \return true if the table was dropped, false if the table did not exist |
| 106 | + virtual bool DropTable(const TableIdentifier& identifier, bool purge) = 0; |
| 107 | + |
| 108 | + /// \brief Load a table |
| 109 | + /// |
| 110 | + /// \param identifier a table identifier |
| 111 | + /// \return instance of Table implementation referred to by identifier or |
| 112 | + /// ErrorKind::kNoSuchTable if the table does not exist |
| 113 | + virtual expected<std::shared_ptr<Table>, Error> LoadTable( |
| 114 | + const TableIdentifier& identifier) const = 0; |
| 115 | + |
| 116 | + /// \brief Register a table with the catalog if it does not exist |
| 117 | + /// |
| 118 | + /// \param identifier a table identifier |
| 119 | + /// \param metadata_file_location the location of a metadata file |
| 120 | + /// \return a Table instance or ErrorKind::kAlreadyExists if the table already exists |
| 121 | + virtual expected<std::shared_ptr<Table>, Error> RegisterTable( |
| 122 | + const TableIdentifier& identifier, const std::string& metadata_file_location) = 0; |
| 123 | + |
| 124 | + /// \brief Initialize a catalog given a custom name and a map of catalog properties |
| 125 | + /// |
| 126 | + /// A custom Catalog implementation must have a default constructor. A compute engine |
| 127 | + /// will first initialize the catalog without any arguments, and then call this method |
| 128 | + /// to complete catalog initialization with properties passed into the engine. |
| 129 | + /// |
| 130 | + /// \param name a custom name for the catalog |
| 131 | + /// \param properties catalog properties |
| 132 | + virtual void Initialize(const std::string& name, |
| 133 | + const std::map<std::string, std::string>& properties) = 0; |
| 134 | + |
| 135 | + /// \brief Instantiate a builder to either create a table or start a create/replace |
| 136 | + /// transaction |
| 137 | + /// |
| 138 | + /// \param identifier a table identifier |
| 139 | + /// \param schema a schema |
| 140 | + /// \return the builder to create a table or start a create/replace transaction |
| 141 | + virtual std::unique_ptr<class TableBuilder> BuildTable( |
| 142 | + const TableIdentifier& identifier, const Schema& schema) const = 0; |
| 143 | + |
| 144 | + /// \brief A builder used to create valid tables or start create/replace transactions |
| 145 | + class TableBuilder { |
| 146 | + public: |
| 147 | + virtual ~TableBuilder() = default; |
| 148 | + |
| 149 | + /// \brief Sets a partition spec for the table |
| 150 | + /// |
| 151 | + /// \param spec a partition spec |
| 152 | + /// \return this for method chaining |
| 153 | + virtual TableBuilder& WithPartitionSpec(const PartitionSpec& spec) = 0; |
| 154 | + |
| 155 | + /// \brief Sets a sort order for the table |
| 156 | + /// |
| 157 | + /// \param sort_order a sort order |
| 158 | + /// \return this for method chaining |
| 159 | + virtual TableBuilder& WithSortOrder(const SortOrder& sort_order) = 0; |
| 160 | + |
| 161 | + /// \brief Sets a location for the table |
| 162 | + /// |
| 163 | + /// \param location a location |
| 164 | + /// \return this for method chaining |
| 165 | + virtual TableBuilder& WithLocation(const std::string& location) = 0; |
| 166 | + |
| 167 | + /// \brief Adds key/value properties to the table |
| 168 | + /// |
| 169 | + /// \param properties key/value properties |
| 170 | + /// \return this for method chaining |
| 171 | + virtual TableBuilder& WithProperties( |
| 172 | + const std::map<std::string, std::string>& properties) = 0; |
| 173 | + |
| 174 | + /// \brief Adds a key/value property to the table |
| 175 | + /// |
| 176 | + /// \param key a key |
| 177 | + /// \param value a value |
| 178 | + /// \return this for method chaining |
| 179 | + virtual TableBuilder& WithProperty(const std::string& key, |
| 180 | + const std::string& value) = 0; |
| 181 | + |
| 182 | + /// \brief Creates the table |
| 183 | + /// |
| 184 | + /// \return the created table |
| 185 | + virtual std::unique_ptr<Table> Create() = 0; |
| 186 | + |
| 187 | + /// \brief Starts a transaction to create the table |
| 188 | + /// |
| 189 | + /// \return the Transaction to create the table |
| 190 | + virtual std::unique_ptr<Transaction> StageCreate() = 0; |
| 191 | + }; |
| 192 | +}; |
| 193 | + |
| 194 | +} // namespace iceberg |
0 commit comments