Skip to content

Commit 0358d7f

Browse files
authored
feat: add pure virtual classes for Catalog, Table, etc. (#47)
# What are done in this PR - Added pure virtual classes of `Catalog`, `Table`, `TableOperations`, `Transaction` and `LocationProvider` with limited features like `AppendFiles` and `TableScan`. - Added basic definitions of `Namespace` and `TableIdentifier`. - Defined initial `Error` enum. # What are undecided - How to define an IO-less `FileIO`. - How to define `StructLike`.
1 parent dde3efa commit 0358d7f

File tree

15 files changed

+516
-21
lines changed

15 files changed

+516
-21
lines changed

example/demo_example.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
#include "iceberg/arrow/demo_arrow.h"
2323
#include "iceberg/avro/demo_avro.h"
24-
#include "iceberg/demo_table.h"
24+
#include "iceberg/demo.h"
2525
#include "iceberg/puffin/demo_puffin.h"
2626

2727
int main() {
28-
std::cout << iceberg::DemoTable().print() << std::endl;
28+
std::cout << iceberg::Demo().print() << std::endl;
2929
std::cout << iceberg::puffin::DemoPuffin().print() << std::endl;
3030
std::cout << iceberg::arrow::DemoArrow().print() << std::endl;
3131
std::cout << iceberg::avro::DemoAvro().print() << std::endl;

src/iceberg/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ set(ICEBERG_INCLUDES "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/src>"
1919
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>")
2020
set(ICEBERG_SOURCES
2121
arrow_c_data_internal.cc
22-
demo_table.cc
22+
demo.cc
2323
schema.cc
2424
schema_field.cc
2525
type.cc)

src/iceberg/arrow/demo_arrow.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@
2121

2222
#include <arrow/config.h>
2323

24-
#include "iceberg/demo_table.h"
24+
#include "iceberg/demo.h"
2525

2626
namespace iceberg::arrow {
2727

2828
std::string DemoArrow::print() const {
29-
return DemoTable().print() +
30-
", Arrow version: " + ::arrow::GetBuildInfo().version_string;
29+
return Demo().print() + ", Arrow version: " + ::arrow::GetBuildInfo().version_string;
3130
}
3231

3332
} // namespace iceberg::arrow

src/iceberg/arrow/demo_arrow.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
#include <string>
2323

2424
#include "iceberg/arrow/iceberg_arrow_export.h"
25-
#include "iceberg/table.h"
25+
#include "iceberg/demo.h"
2626

2727
namespace iceberg::arrow {
2828

29-
class ICEBERG_ARROW_EXPORT DemoArrow : public Table {
29+
class ICEBERG_ARROW_EXPORT DemoArrow : public Demo {
3030
public:
3131
DemoArrow() = default;
3232
~DemoArrow() override = default;

src/iceberg/avro/demo_avro.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include "avro/Compiler.hh"
2525
#include "avro/ValidSchema.hh"
26-
#include "iceberg/demo_table.h"
26+
#include "iceberg/demo.h"
2727

2828
namespace iceberg::avro {
2929

src/iceberg/catalog.h

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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

src/iceberg/demo_table.cc renamed to src/iceberg/demo.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
* under the License.
1818
*/
1919

20-
#include "iceberg/demo_table.h"
20+
#include "iceberg/demo.h"
2121

2222
#include "iceberg/avro.h" // include to export symbols
23+
#include "iceberg/catalog.h"
24+
#include "iceberg/location_provider.h"
2325
#include "iceberg/puffin.h"
26+
#include "iceberg/table.h"
27+
#include "iceberg/transaction.h"
2428

2529
namespace iceberg {
2630

27-
std::string DemoTable::print() const { return "DemoTable"; }
31+
std::string Demo::print() const { return "Demo"; }
2832

2933
} // namespace iceberg

src/iceberg/demo_table.h renamed to src/iceberg/demo.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919

2020
#pragma once
2121

22-
#include "iceberg/table.h"
22+
#include <string>
23+
24+
#include "iceberg/iceberg_export.h"
2325

2426
namespace iceberg {
2527

26-
class ICEBERG_EXPORT DemoTable : public Table {
28+
class ICEBERG_EXPORT Demo {
2729
public:
28-
DemoTable() = default;
29-
~DemoTable() override = default;
30+
Demo() = default;
31+
virtual ~Demo() = default;
3032

31-
std::string print() const override;
33+
virtual std::string print() const;
3234
};
3335

3436
} // namespace iceberg

src/iceberg/error.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 <string>
23+
24+
#include "iceberg/iceberg_export.h"
25+
26+
namespace iceberg {
27+
28+
/// \brief Error types for iceberg.
29+
/// TODO: add more and sort them based on some rules.
30+
enum class ErrorKind {
31+
kNoSuchNamespace,
32+
kAlreadyExists,
33+
kNoSuchTable,
34+
kCommitStateUnknown,
35+
};
36+
37+
/// \brief Error with a kind and a message.
38+
struct ICEBERG_EXPORT [[nodiscard]] Error {
39+
ErrorKind kind;
40+
std::string message;
41+
};
42+
43+
} // namespace iceberg

0 commit comments

Comments
 (0)