Skip to content

Commit 882f0f2

Browse files
committed
refactor: consolidate result type
1 parent 22adac2 commit 882f0f2

File tree

14 files changed

+82
-77
lines changed

14 files changed

+82
-77
lines changed

src/iceberg/arrow/arrow_error_transform_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
#include <arrow/result.h>
2323
#include <arrow/status.h>
2424

25-
#include "iceberg/error.h"
2625
#include "iceberg/expected.h"
26+
#include "iceberg/result.h"
2727

2828
namespace iceberg::arrow {
2929

src/iceberg/arrow/arrow_fs_file_io.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
namespace iceberg::arrow {
2727

2828
/// \brief Read the content of the file at the given location.
29-
expected<std::string, Error> ArrowFileSystemFileIO::ReadFile(
30-
const std::string& file_location, std::optional<size_t> length) {
29+
Result<std::string> ArrowFileSystemFileIO::ReadFile(const std::string& file_location,
30+
std::optional<size_t> length) {
3131
::arrow::fs::FileInfo file_info(file_location);
3232
if (length.has_value()) {
3333
file_info.set_size(length.value());
@@ -52,8 +52,8 @@ expected<std::string, Error> ArrowFileSystemFileIO::ReadFile(
5252
}
5353

5454
/// \brief Write the given content to the file at the given location.
55-
expected<void, Error> ArrowFileSystemFileIO::WriteFile(const std::string& file_location,
56-
std::string_view content) {
55+
Result<void> ArrowFileSystemFileIO::WriteFile(const std::string& file_location,
56+
std::string_view content) {
5757
ICEBERG_ARROW_ASSIGN_OR_RETURN(auto file, arrow_fs_->OpenOutputStream(file_location));
5858
ICEBERG_ARROW_RETURN_NOT_OK(file->Write(content.data(), content.size()));
5959
ICEBERG_ARROW_RETURN_NOT_OK(file->Flush());
@@ -62,8 +62,7 @@ expected<void, Error> ArrowFileSystemFileIO::WriteFile(const std::string& file_l
6262
}
6363

6464
/// \brief Delete a file at the given location.
65-
expected<void, Error> ArrowFileSystemFileIO::DeleteFile(
66-
const std::string& file_location) {
65+
Result<void> ArrowFileSystemFileIO::DeleteFile(const std::string& file_location) {
6766
ICEBERG_ARROW_RETURN_NOT_OK(arrow_fs_->DeleteFile(file_location));
6867
return {};
6968
}

src/iceberg/arrow/arrow_fs_file_io.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ class ICEBERG_BUNDLE_EXPORT ArrowFileSystemFileIO : public FileIO {
3737
~ArrowFileSystemFileIO() override = default;
3838

3939
/// \brief Read the content of the file at the given location.
40-
expected<std::string, Error> ReadFile(const std::string& file_location,
41-
std::optional<size_t> length) override;
40+
Result<std::string> ReadFile(const std::string& file_location,
41+
std::optional<size_t> length) override;
4242

4343
/// \brief Write the given content to the file at the given location.
44-
expected<void, Error> WriteFile(const std::string& file_location,
45-
std::string_view content) override;
44+
Result<void> WriteFile(const std::string& file_location,
45+
std::string_view content) override;
4646

4747
/// \brief Delete a file at the given location.
48-
expected<void, Error> DeleteFile(const std::string& file_location) override;
48+
Result<void> DeleteFile(const std::string& file_location) override;
4949

5050
private:
5151
std::shared_ptr<::arrow::fs::FileSystem> arrow_fs_;

src/iceberg/catalog.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919

2020
#pragma once
2121

22-
#include <map>
2322
#include <memory>
2423
#include <string>
2524
#include <string_view>
25+
#include <unordered_map>
2626
#include <vector>
2727

28-
#include "iceberg/error.h"
29-
#include "iceberg/expected.h"
28+
#include "iceberg/result.h"
3029
#include "iceberg/table_identifier.h"
3130
#include "iceberg/type_fwd.h"
3231

@@ -48,8 +47,7 @@ class ICEBERG_EXPORT Catalog {
4847
/// \param ns a namespace
4948
/// \return a list of identifiers for tables or ErrorKind::kNoSuchNamespace
5049
/// if the namespace does not exist
51-
virtual expected<std::vector<TableIdentifier>, Error> ListTables(
52-
const Namespace& ns) const = 0;
50+
virtual Result<std::vector<TableIdentifier>> ListTables(const Namespace& ns) const = 0;
5351

5452
/// \brief Create a table
5553
///
@@ -59,18 +57,18 @@ class ICEBERG_EXPORT Catalog {
5957
/// \param location a location for the table; leave empty if unspecified
6058
/// \param properties a string map of table properties
6159
/// \return a Table instance or ErrorKind::kAlreadyExists if the table already exists
62-
virtual expected<std::unique_ptr<Table>, Error> CreateTable(
60+
virtual Result<std::unique_ptr<Table>> CreateTable(
6361
const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec,
6462
const std::string& location,
65-
const std::map<std::string, std::string>& properties) = 0;
63+
const std::unordered_map<std::string, std::string>& properties) = 0;
6664

6765
/// \brief Update a table
6866
///
6967
/// \param identifier a table identifier
7068
/// \param requirements a list of table requirements
7169
/// \param updates a list of table updates
7270
/// \return a Table instance or ErrorKind::kAlreadyExists if the table already exists
73-
virtual expected<std::unique_ptr<Table>, Error> UpdateTable(
71+
virtual Result<std::unique_ptr<Table>> UpdateTable(
7472
const TableIdentifier& identifier,
7573
const std::vector<std::unique_ptr<UpdateRequirement>>& requirements,
7674
const std::vector<std::unique_ptr<MetadataUpdate>>& updates) = 0;
@@ -84,10 +82,10 @@ class ICEBERG_EXPORT Catalog {
8482
/// \param properties a string map of table properties
8583
/// \return a Transaction to create the table or ErrorKind::kAlreadyExists if the table
8684
/// already exists
87-
virtual expected<std::shared_ptr<Transaction>, Error> StageCreateTable(
85+
virtual Result<std::shared_ptr<Transaction>> StageCreateTable(
8886
const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec,
8987
const std::string& location,
90-
const std::map<std::string, std::string>& properties) = 0;
88+
const std::unordered_map<std::string, std::string>& properties) = 0;
9189

9290
/// \brief Check whether table exists
9391
///
@@ -110,15 +108,15 @@ class ICEBERG_EXPORT Catalog {
110108
/// \param identifier a table identifier
111109
/// \return instance of Table implementation referred to by identifier or
112110
/// ErrorKind::kNoSuchTable if the table does not exist
113-
virtual expected<std::shared_ptr<Table>, Error> LoadTable(
111+
virtual Result<std::shared_ptr<Table>> LoadTable(
114112
const TableIdentifier& identifier) const = 0;
115113

116114
/// \brief Register a table with the catalog if it does not exist
117115
///
118116
/// \param identifier a table identifier
119117
/// \param metadata_file_location the location of a metadata file
120118
/// \return a Table instance or ErrorKind::kAlreadyExists if the table already exists
121-
virtual expected<std::shared_ptr<Table>, Error> RegisterTable(
119+
virtual Result<std::shared_ptr<Table>> RegisterTable(
122120
const TableIdentifier& identifier, const std::string& metadata_file_location) = 0;
123121

124122
/// \brief Initialize a catalog given a custom name and a map of catalog properties
@@ -129,8 +127,9 @@ class ICEBERG_EXPORT Catalog {
129127
///
130128
/// \param name a custom name for the catalog
131129
/// \param properties catalog properties
132-
virtual void Initialize(const std::string& name,
133-
const std::map<std::string, std::string>& properties) = 0;
130+
virtual void Initialize(
131+
const std::string& name,
132+
const std::unordered_map<std::string, std::string>& properties) = 0;
134133

135134
/// \brief Instantiate a builder to either create a table or start a create/replace
136135
/// transaction
@@ -169,7 +168,7 @@ class ICEBERG_EXPORT Catalog {
169168
/// \param properties key/value properties
170169
/// \return this for method chaining
171170
virtual TableBuilder& WithProperties(
172-
const std::map<std::string, std::string>& properties) = 0;
171+
const std::unordered_map<std::string, std::string>& properties) = 0;
173172

174173
/// \brief Adds a key/value property to the table
175174
///

src/iceberg/file_io.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
#include <string>
2424
#include <string_view>
2525

26-
#include "iceberg/error.h"
27-
#include "iceberg/expected.h"
2826
#include "iceberg/iceberg_export.h"
27+
#include "iceberg/result.h"
2928

3029
namespace iceberg {
3130

@@ -50,8 +49,8 @@ class ICEBERG_EXPORT FileIO {
5049
/// the length to read, e.g. S3 `GetObject` has a Range parameter.
5150
/// \return The content of the file if the read succeeded, an error code if the read
5251
/// failed.
53-
virtual expected<std::string, Error> ReadFile(const std::string& file_location,
54-
std::optional<size_t> length) {
52+
virtual Result<std::string> ReadFile(const std::string& file_location,
53+
std::optional<size_t> length) {
5554
// We provide a default implementation to avoid Windows linker error LNK2019.
5655
return unexpected<Error>{
5756
{.kind = ErrorKind::kNotImplemented, .message = "ReadFile not implemented"}};
@@ -64,8 +63,8 @@ class ICEBERG_EXPORT FileIO {
6463
/// \param overwrite If true, overwrite the file if it exists. If false, fail if the
6564
/// file exists.
6665
/// \return void if the write succeeded, an error code if the write failed.
67-
virtual expected<void, Error> WriteFile(const std::string& file_location,
68-
std::string_view content) {
66+
virtual Result<void> WriteFile(const std::string& file_location,
67+
std::string_view content) {
6968
return unexpected<Error>{
7069
{.kind = ErrorKind::kNotImplemented, .message = "WriteFile not implemented"}};
7170
}
@@ -74,7 +73,7 @@ class ICEBERG_EXPORT FileIO {
7473
///
7574
/// \param file_location The location of the file to delete.
7675
/// \return void if the delete succeeded, an error code if the delete failed.
77-
virtual expected<void, Error> DeleteFile(const std::string& file_location) {
76+
virtual Result<void> DeleteFile(const std::string& file_location) {
7877
return unexpected<Error>{
7978
{.kind = ErrorKind::kNotImplemented, .message = "DeleteFile not implemented"}};
8079
}

src/iceberg/json_internal.cc

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
#include <nlohmann/json.hpp>
2626

27-
#include "iceberg/error.h"
2827
#include "iceberg/expected.h"
28+
#include "iceberg/result.h"
2929
#include "iceberg/schema.h"
3030
#include "iceberg/schema_internal.h"
3131
#include "iceberg/sort_order.h"
@@ -69,7 +69,7 @@ constexpr std::string_view kElementRequired = "element-required";
6969
constexpr std::string_view kValueRequired = "value-required";
7070

7171
template <typename T>
72-
expected<T, Error> GetJsonValue(const nlohmann::json& json, std::string_view key) {
72+
Result<T> GetJsonValue(const nlohmann::json& json, std::string_view key) {
7373
if (!json.contains(key)) {
7474
return unexpected<Error>({
7575
.kind = ErrorKind::kJsonParseError,
@@ -109,8 +109,7 @@ nlohmann::json ToJson(const SortOrder& sort_order) {
109109
return json;
110110
}
111111

112-
expected<std::unique_ptr<SortField>, Error> SortFieldFromJson(
113-
const nlohmann::json& json) {
112+
Result<std::unique_ptr<SortField>> SortFieldFromJson(const nlohmann::json& json) {
114113
ICEBERG_ASSIGN_OR_RAISE(auto source_id, GetJsonValue<int32_t>(json, kSourceId));
115114
ICEBERG_ASSIGN_OR_RAISE(
116115
auto transform,
@@ -125,8 +124,7 @@ expected<std::unique_ptr<SortField>, Error> SortFieldFromJson(
125124
null_order);
126125
}
127126

128-
expected<std::unique_ptr<SortOrder>, Error> SortOrderFromJson(
129-
const nlohmann::json& json) {
127+
Result<std::unique_ptr<SortOrder>> SortOrderFromJson(const nlohmann::json& json) {
130128
ICEBERG_ASSIGN_OR_RAISE(auto order_id, GetJsonValue<int32_t>(json, kOrderId));
131129
ICEBERG_ASSIGN_OR_RAISE(auto fields, GetJsonValue<nlohmann::json>(json, kFields));
132130

@@ -232,7 +230,7 @@ nlohmann::json SchemaToJson(const Schema& schema) {
232230

233231
namespace {
234232

235-
expected<std::unique_ptr<Type>, Error> StructTypeFromJson(const nlohmann::json& json) {
233+
Result<std::unique_ptr<Type>> StructTypeFromJson(const nlohmann::json& json) {
236234
ICEBERG_ASSIGN_OR_RAISE(auto json_fields, GetJsonValue<nlohmann::json>(json, kFields));
237235

238236
std::vector<SchemaField> fields;
@@ -244,7 +242,7 @@ expected<std::unique_ptr<Type>, Error> StructTypeFromJson(const nlohmann::json&
244242
return std::make_unique<StructType>(std::move(fields));
245243
}
246244

247-
expected<std::unique_ptr<Type>, Error> ListTypeFromJson(const nlohmann::json& json) {
245+
Result<std::unique_ptr<Type>> ListTypeFromJson(const nlohmann::json& json) {
248246
ICEBERG_ASSIGN_OR_RAISE(auto element_type, TypeFromJson(json[kElement]));
249247
ICEBERG_ASSIGN_OR_RAISE(auto element_id, GetJsonValue<int32_t>(json, kElementId));
250248
ICEBERG_ASSIGN_OR_RAISE(auto element_required,
@@ -255,7 +253,7 @@ expected<std::unique_ptr<Type>, Error> ListTypeFromJson(const nlohmann::json& js
255253
std::move(element_type), !element_required));
256254
}
257255

258-
expected<std::unique_ptr<Type>, Error> MapTypeFromJson(const nlohmann::json& json) {
256+
Result<std::unique_ptr<Type>> MapTypeFromJson(const nlohmann::json& json) {
259257
ICEBERG_ASSIGN_OR_RAISE(
260258
auto key_type, GetJsonValue<nlohmann::json>(json, kKey).and_then(TypeFromJson));
261259
ICEBERG_ASSIGN_OR_RAISE(
@@ -274,7 +272,7 @@ expected<std::unique_ptr<Type>, Error> MapTypeFromJson(const nlohmann::json& jso
274272

275273
} // namespace
276274

277-
expected<std::unique_ptr<Type>, Error> TypeFromJson(const nlohmann::json& json) {
275+
Result<std::unique_ptr<Type>> TypeFromJson(const nlohmann::json& json) {
278276
if (json.is_string()) {
279277
std::string type_str = json.get<std::string>();
280278
if (type_str == "boolean") {
@@ -346,7 +344,7 @@ expected<std::unique_ptr<Type>, Error> TypeFromJson(const nlohmann::json& json)
346344
}
347345
}
348346

349-
expected<std::unique_ptr<SchemaField>, Error> FieldFromJson(const nlohmann::json& json) {
347+
Result<std::unique_ptr<SchemaField>> FieldFromJson(const nlohmann::json& json) {
350348
ICEBERG_ASSIGN_OR_RAISE(
351349
auto type, GetJsonValue<nlohmann::json>(json, kType).and_then(TypeFromJson));
352350
ICEBERG_ASSIGN_OR_RAISE(auto field_id, GetJsonValue<int32_t>(json, kId));
@@ -357,7 +355,7 @@ expected<std::unique_ptr<SchemaField>, Error> FieldFromJson(const nlohmann::json
357355
!required);
358356
}
359357

360-
expected<std::unique_ptr<Schema>, Error> SchemaFromJson(const nlohmann::json& json) {
358+
Result<std::unique_ptr<Schema>> SchemaFromJson(const nlohmann::json& json) {
361359
ICEBERG_ASSIGN_OR_RAISE(auto schema_id, GetJsonValue<int32_t>(json, kSchemaId));
362360
ICEBERG_ASSIGN_OR_RAISE(auto type, TypeFromJson(json));
363361

src/iceberg/json_internal.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
#include <nlohmann/json_fwd.hpp>
2626

27-
#include "iceberg/error.h"
2827
#include "iceberg/expected.h"
28+
#include "iceberg/result.h"
2929
#include "iceberg/type_fwd.h"
3030

3131
namespace iceberg {
@@ -59,7 +59,7 @@ nlohmann::json ToJson(const SortOrder& sort_order);
5959
/// \param json The JSON object representing a `SortField`.
6060
/// \return An `expected` value containing either a `SortField` object or an error. If the
6161
/// JSON is malformed or missing expected fields, an error will be returned.
62-
expected<std::unique_ptr<SortField>, Error> SortFieldFromJson(const nlohmann::json& json);
62+
Result<std::unique_ptr<SortField>> SortFieldFromJson(const nlohmann::json& json);
6363

6464
/// \brief Deserializes a JSON object into a `SortOrder` object.
6565
///
@@ -70,7 +70,7 @@ expected<std::unique_ptr<SortField>, Error> SortFieldFromJson(const nlohmann::js
7070
/// \param json The JSON object representing a `SortOrder`.
7171
/// \return An `expected` value containing either a `SortOrder` object or an error. If the
7272
/// JSON is malformed or missing expected fields, an error will be returned.
73-
expected<std::unique_ptr<SortOrder>, Error> SortOrderFromJson(const nlohmann::json& json);
73+
Result<std::unique_ptr<SortOrder>> SortOrderFromJson(const nlohmann::json& json);
7474

7575
/// \brief Convert an Iceberg Schema to JSON.
7676
///
@@ -94,18 +94,18 @@ nlohmann::json FieldToJson(const SchemaField& field);
9494
///
9595
/// \param[in] json The JSON representation of the schema.
9696
/// \return The Iceberg schema or an error if the conversion fails.
97-
expected<std::unique_ptr<Schema>, Error> SchemaFromJson(const nlohmann::json& json);
97+
Result<std::unique_ptr<Schema>> SchemaFromJson(const nlohmann::json& json);
9898

9999
/// \brief Convert JSON to an Iceberg Type.
100100
///
101101
/// \param[in] json The JSON representation of the type.
102102
/// \return The Iceberg type or an error if the conversion fails.
103-
expected<std::unique_ptr<Type>, Error> TypeFromJson(const nlohmann::json& json);
103+
Result<std::unique_ptr<Type>> TypeFromJson(const nlohmann::json& json);
104104

105105
/// \brief Convert JSON to an Iceberg SchemaField.
106106
///
107107
/// \param[in] json The JSON representation of the field.
108108
/// \return The Iceberg field or an error if the conversion fails.
109-
expected<std::unique_ptr<SchemaField>, Error> FieldFromJson(const nlohmann::json& json);
109+
Result<std::unique_ptr<SchemaField>> FieldFromJson(const nlohmann::json& json);
110110

111111
} // namespace iceberg

src/iceberg/error.h renamed to src/iceberg/result.h

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

2222
#include <string>
2323

24+
#include "iceberg/expected.h"
2425
#include "iceberg/iceberg_export.h"
2526

2627
namespace iceberg {
@@ -47,4 +48,14 @@ struct ICEBERG_EXPORT [[nodiscard]] Error {
4748
std::string message;
4849
};
4950

51+
/// /brief Default error trait
52+
template <typename T>
53+
struct DefaultError {
54+
using type = Error;
55+
};
56+
57+
/// \brief Result alias
58+
template <typename T, typename E = typename DefaultError<T>::type>
59+
using Result = expected<T, E>;
60+
5061
} // namespace iceberg

0 commit comments

Comments
 (0)