Skip to content

Commit 40913c7

Browse files
authored
refactor: adapt table metadata to use table properties (#407)
1 parent 58abe49 commit 40913c7

File tree

10 files changed

+49
-26
lines changed

10 files changed

+49
-26
lines changed

src/iceberg/json_internal.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "iceberg/statistics_file.h"
4040
#include "iceberg/table_identifier.h"
4141
#include "iceberg/table_metadata.h"
42+
#include "iceberg/table_properties.h"
4243
#include "iceberg/transform.h"
4344
#include "iceberg/type.h"
4445
#include "iceberg/util/formatter.h" // IWYU pragma: keep
@@ -796,7 +797,9 @@ nlohmann::json ToJson(const TableMetadata& table_metadata) {
796797
json[kSortOrders] = ToJsonList(table_metadata.sort_orders);
797798

798799
// write properties map
799-
json[kProperties] = table_metadata.properties;
800+
if (table_metadata.properties) {
801+
json[kProperties] = table_metadata.properties->configs();
802+
}
800803

801804
if (std::ranges::find_if(table_metadata.snapshots, [&](const auto& snapshot) {
802805
return snapshot->snapshot_id == table_metadata.current_snapshot_id;
@@ -1037,7 +1040,10 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataFromJson(const nlohmann::jso
10371040
table_metadata->default_sort_order_id, table_metadata->sort_orders));
10381041

10391042
if (json.contains(kProperties)) {
1040-
ICEBERG_ASSIGN_OR_RAISE(table_metadata->properties, FromJsonMap(json, kProperties));
1043+
ICEBERG_ASSIGN_OR_RAISE(auto properties, FromJsonMap(json, kProperties));
1044+
table_metadata->properties = TableProperties::FromMap(std::move(properties));
1045+
} else {
1046+
table_metadata->properties = TableProperties::default_properties();
10411047
}
10421048

10431049
// This field is optional, but internally we set this to -1 when not set

src/iceberg/table.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ Table::Table(TableIdentifier identifier, std::shared_ptr<TableMetadata> metadata
4141
metadata_location_(std::move(metadata_location)),
4242
io_(std::move(io)),
4343
catalog_(std::move(catalog)),
44-
properties_(TableProperties::FromMap(metadata_->properties)),
4544
metadata_cache_(std::make_unique<TableMetadataCache>(metadata_.get())) {}
4645

4746
const std::string& Table::uuid() const { return metadata_->table_uuid; }
@@ -56,7 +55,6 @@ Status Table::Refresh() {
5655
metadata_ = std::move(refreshed_table->metadata_);
5756
metadata_location_ = std::move(refreshed_table->metadata_location_);
5857
io_ = std::move(refreshed_table->io_);
59-
properties_ = std::move(refreshed_table->properties_);
6058
metadata_cache_ = std::make_unique<TableMetadataCache>(metadata_.get());
6159
}
6260
return {};
@@ -89,7 +87,9 @@ Table::sort_orders() const {
8987
return metadata_cache_->GetSortOrdersById();
9088
}
9189

92-
const TableProperties& Table::properties() const { return *properties_; }
90+
const std::shared_ptr<TableProperties>& Table::properties() const {
91+
return metadata_->properties;
92+
}
9393

9494
const std::string& Table::location() const { return metadata_->location; }
9595

src/iceberg/table.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class ICEBERG_EXPORT Table {
8282
sort_orders() const;
8383

8484
/// \brief Return a map of string properties for this table
85-
const TableProperties& properties() const;
85+
const std::shared_ptr<TableProperties>& properties() const;
8686

8787
/// \brief Return the table's base location
8888
const std::string& location() const;
@@ -130,7 +130,6 @@ class ICEBERG_EXPORT Table {
130130
std::string metadata_location_;
131131
std::shared_ptr<FileIO> io_;
132132
std::shared_ptr<Catalog> catalog_;
133-
std::unique_ptr<TableProperties> properties_;
134133
std::unique_ptr<class TableMetadataCache> metadata_cache_;
135134
};
136135

src/iceberg/table_metadata.cc

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "iceberg/schema.h"
3939
#include "iceberg/snapshot.h"
4040
#include "iceberg/sort_order.h"
41+
#include "iceberg/table_properties.h"
4142
#include "iceberg/table_update.h"
4243
#include "iceberg/util/gzip_internal.h"
4344
#include "iceberg/util/macros.h"
@@ -141,6 +142,19 @@ bool SnapshotRefEquals(
141142
return true;
142143
}
143144

145+
bool TablePropertiesEquals(const std::shared_ptr<TableProperties>& lhs,
146+
const std::shared_ptr<TableProperties>& rhs) {
147+
bool left_is_empty = lhs == nullptr || lhs->configs().empty();
148+
bool right_is_empty = rhs == nullptr || rhs->configs().empty();
149+
if (left_is_empty && right_is_empty) {
150+
return true;
151+
}
152+
if (left_is_empty || right_is_empty) {
153+
return false;
154+
}
155+
return lhs->configs() == rhs->configs();
156+
}
157+
144158
} // namespace
145159

146160
bool operator==(const TableMetadata& lhs, const TableMetadata& rhs) {
@@ -153,7 +167,7 @@ bool operator==(const TableMetadata& lhs, const TableMetadata& rhs) {
153167
SharedPtrVectorEquals(lhs.schemas, rhs.schemas) &&
154168
lhs.default_spec_id == rhs.default_spec_id &&
155169
lhs.last_partition_id == rhs.last_partition_id &&
156-
lhs.properties == rhs.properties &&
170+
TablePropertiesEquals(lhs.properties, rhs.properties) &&
157171
lhs.current_snapshot_id == rhs.current_snapshot_id &&
158172
SharedPtrVectorEquals(lhs.snapshots, rhs.snapshots) &&
159173
lhs.snapshot_log == rhs.snapshot_log && lhs.metadata_log == rhs.metadata_log &&
@@ -300,6 +314,7 @@ struct TableMetadataBuilder::Impl {
300314
metadata.current_snapshot_id = Snapshot::kInvalidSnapshotId;
301315
metadata.default_sort_order_id = SortOrder::kInitialSortOrderId;
302316
metadata.next_row_id = TableMetadata::kInitialRowId;
317+
metadata.properties = TableProperties::default_properties();
303318
}
304319

305320
// Constructor from existing metadata
@@ -611,7 +626,7 @@ TableMetadataBuilder& TableMetadataBuilder::SetProperties(
611626

612627
// Add all updated properties to the metadata properties
613628
for (const auto& [key, value] : updated) {
614-
impl_->metadata.properties[key] = value;
629+
impl_->metadata.properties->mutable_configs()[key] = value;
615630
}
616631

617632
// Record the change
@@ -629,7 +644,7 @@ TableMetadataBuilder& TableMetadataBuilder::RemoveProperties(
629644

630645
// Remove each property from the metadata properties
631646
for (const auto& key : removed) {
632-
impl_->metadata.properties.erase(key);
647+
impl_->metadata.properties->mutable_configs().erase(key);
633648
}
634649

635650
// Record the change

src/iceberg/table_metadata.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ struct ICEBERG_EXPORT TableMetadata {
9999
/// The highest assigned partition field ID across all partition specs for the table
100100
int32_t last_partition_id;
101101
/// A string to string map of table properties
102-
std::unordered_map<std::string, std::string> properties;
102+
std::shared_ptr<TableProperties> properties;
103103
/// ID of the current table snapshot
104104
int64_t current_snapshot_id;
105105
/// A list of valid snapshots

src/iceberg/table_properties.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ std::unique_ptr<TableProperties> TableProperties::default_properties() {
3636
}
3737

3838
std::unique_ptr<TableProperties> TableProperties::FromMap(
39-
const std::unordered_map<std::string, std::string>& properties) {
39+
std::unordered_map<std::string, std::string> properties) {
4040
auto table_properties = std::unique_ptr<TableProperties>(new TableProperties());
41-
for (const auto& [key, value] : properties) { // NOLINT(modernize-type-traits)
42-
table_properties->configs_[key] = value;
43-
}
41+
table_properties->configs_ = std::move(properties);
4442
return table_properties;
4543
}
4644

src/iceberg/table_properties.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class ICEBERG_EXPORT TableProperties : public ConfigBase<TableProperties> {
296296
/// \param properties The map containing property key-value pairs
297297
/// \return A unique pointer to a TableProperties instance
298298
static std::unique_ptr<TableProperties> FromMap(
299-
const std::unordered_map<std::string, std::string>& properties);
299+
std::unordered_map<std::string, std::string> properties);
300300

301301
private:
302302
TableProperties() = default;

src/iceberg/test/metadata_io_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "iceberg/schema.h"
3131
#include "iceberg/snapshot.h"
3232
#include "iceberg/table_metadata.h"
33+
#include "iceberg/table_properties.h"
3334
#include "iceberg/test/matchers.h"
3435
#include "iceberg/test/temp_file_test_base.h"
3536

@@ -58,7 +59,7 @@ class MetadataIOTest : public TempFileTestBase {
5859
.current_schema_id = 1,
5960
.default_spec_id = 0,
6061
.last_partition_id = 0,
61-
.properties = {{"key", "value"}},
62+
.properties = TableProperties::FromMap({{"key", "value"}}),
6263
.current_snapshot_id = 3051729675574597004,
6364
.snapshots = {std::make_shared<Snapshot>(Snapshot{
6465
.snapshot_id = 3051729675574597004,

src/iceberg/test/table_metadata_builder_test.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "iceberg/sort_field.h"
3030
#include "iceberg/sort_order.h"
3131
#include "iceberg/table_metadata.h"
32+
#include "iceberg/table_properties.h"
3233
#include "iceberg/table_update.h"
3334
#include "iceberg/test/matchers.h"
3435
#include "iceberg/transform.h"
@@ -63,6 +64,7 @@ std::unique_ptr<TableMetadata> CreateBaseMetadata() {
6364
metadata->default_sort_order_id = SortOrder::kInitialSortOrderId;
6465
metadata->sort_orders.push_back(SortOrder::Unsorted());
6566
metadata->next_row_id = TableMetadata::kInitialRowId;
67+
metadata->properties = TableProperties::default_properties();
6668
return metadata;
6769
}
6870

@@ -145,19 +147,19 @@ TEST(TableMetadataBuilderTest, SetProperties) {
145147
builder->SetProperties({{"key1", "value1"}, {"key2", "value2"}});
146148

147149
ICEBERG_UNWRAP_OR_FAIL(auto metadata, builder->Build());
148-
EXPECT_EQ(metadata->properties.size(), 2);
149-
EXPECT_EQ(metadata->properties["key1"], "value1");
150-
EXPECT_EQ(metadata->properties["key2"], "value2");
150+
EXPECT_EQ(metadata->properties->configs().size(), 2);
151+
EXPECT_EQ(metadata->properties->configs().at("key1"), "value1");
152+
EXPECT_EQ(metadata->properties->configs().at("key2"), "value2");
151153

152154
// Update existing property and add new one
153155
builder = TableMetadataBuilder::BuildFromEmpty(2);
154156
builder->SetProperties({{"key1", "value1"}});
155157
builder->SetProperties({{"key1", "new_value1"}, {"key3", "value3"}});
156158

157159
ICEBERG_UNWRAP_OR_FAIL(metadata, builder->Build());
158-
EXPECT_EQ(metadata->properties.size(), 2);
159-
EXPECT_EQ(metadata->properties["key1"], "new_value1");
160-
EXPECT_EQ(metadata->properties["key3"], "value3");
160+
EXPECT_EQ(metadata->properties->configs().size(), 2);
161+
EXPECT_EQ(metadata->properties->configs().at("key1"), "new_value1");
162+
EXPECT_EQ(metadata->properties->configs().at("key3"), "value3");
161163
}
162164

163165
TEST(TableMetadataBuilderTest, RemoveProperties) {
@@ -166,9 +168,9 @@ TEST(TableMetadataBuilderTest, RemoveProperties) {
166168
builder->RemoveProperties({"key2", "key4"}); // key4 does not exist
167169

168170
ICEBERG_UNWRAP_OR_FAIL(auto metadata, builder->Build());
169-
EXPECT_EQ(metadata->properties.size(), 2);
170-
EXPECT_EQ(metadata->properties["key1"], "value1");
171-
EXPECT_EQ(metadata->properties["key3"], "value3");
171+
EXPECT_EQ(metadata->properties->configs().size(), 2);
172+
EXPECT_EQ(metadata->properties->configs().at("key1"), "value1");
173+
EXPECT_EQ(metadata->properties->configs().at("key3"), "value3");
172174
}
173175

174176
TEST(TableMetadataBuilderTest, UpgradeFormatVersion) {

src/iceberg/util/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class ConfigBase {
112112

113113
const std::unordered_map<std::string, std::string>& configs() const { return configs_; }
114114

115+
std::unordered_map<std::string, std::string>& mutable_configs() { return configs_; }
116+
115117
/// \brief Extracts the prefix from the configuration.
116118
/// \param prefix The prefix to extract.
117119
/// \return A map of entries that match the prefix with prefix removed.

0 commit comments

Comments
 (0)