Skip to content

Commit ef6bab3

Browse files
author
shuxu.li
committed
feat: Refresh method support for table
1 parent 323aebe commit ef6bab3

File tree

5 files changed

+30
-37
lines changed

5 files changed

+30
-37
lines changed

src/iceberg/catalog/in_memory_catalog.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,4 @@ std::unique_ptr<TableBuilder> InMemoryCatalog::BuildTable(
466466
throw IcebergError("not implemented");
467467
}
468468

469-
Status InMemoryCatalog::UpdateTableMetaLocationInternal(
470-
const TableIdentifier& identifier, const std::string& metadata_location) {
471-
std::unique_lock lock(mutex_);
472-
return root_namespace_->UpdateTableMetaLocation(identifier, metadata_location);
473-
}
474-
475469
} // namespace iceberg

src/iceberg/catalog/in_memory_catalog.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ class ICEBERG_EXPORT InMemoryCatalog
8484

8585
Status DropTable(const TableIdentifier& identifier, bool purge) override;
8686

87-
Result<std::unique_ptr<Table>> LoadTable(const TableIdentifier& identifier) override;
87+
virtual Result<std::unique_ptr<Table>> LoadTable(
88+
const TableIdentifier& identifier) override;
8889

8990
Result<std::shared_ptr<Table>> RegisterTable(
9091
const TableIdentifier& identifier,
@@ -93,10 +94,6 @@ class ICEBERG_EXPORT InMemoryCatalog
9394
std::unique_ptr<iceberg::TableBuilder> BuildTable(const TableIdentifier& identifier,
9495
const Schema& schema) const override;
9596

96-
protected:
97-
Status UpdateTableMetaLocationInternal(const TableIdentifier& identifier,
98-
const std::string& metadata_location);
99-
10097
private:
10198
std::string catalog_name_;
10299
std::unordered_map<std::string, std::string> properties_;

src/iceberg/table.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const std::string& Table::uuid() const { return metadata_->table_uuid; }
3535

3636
Status Table::Refresh() {
3737
if (!catalog_) {
38-
return InvalidArgument("Refresh is not supported for table without a catalog");
38+
return NotSupported("Refresh is not supported for table without a catalog");
3939
}
4040

4141
ICEBERG_ASSIGN_OR_RAISE(auto refreshed_table, catalog_->LoadTable(identifier_));

test/in_memory_catalog_test.cc

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,32 +117,38 @@ TEST_F(InMemoryCatalogTest, RegisterTable) {
117117
TEST_F(InMemoryCatalogTest, RefreshTable) {
118118
TableIdentifier tableIdent{.ns = {}, .name = "t1"};
119119

120-
std::unique_ptr<TableMetadata> metadata;
121-
ASSERT_NO_FATAL_FAILURE(ReadTableMetadata("TableMetadataV2Valid.json", &metadata));
122-
123-
metadata->current_snapshot_id = 3051729675574597004;
124120
auto table_location = GenerateTestTableLocation(tableIdent.name);
125-
auto metadata_location = std::format("{}v0.metadata.json", table_location);
126-
auto status = TableMetadataUtil::Write(*file_io_, metadata_location, *metadata);
127-
EXPECT_THAT(status, IsOk());
128-
121+
auto buildTable = [this, &tableIdent, &table_location](
122+
int64_t snapshot_id, int64_t version) -> std::unique_ptr<Table> {
123+
std::unique_ptr<TableMetadata> metadata;
124+
125+
ReadTableMetadata("TableMetadataV2Valid.json", &metadata);
126+
metadata->current_snapshot_id = snapshot_id;
127+
128+
auto metadata_location = std::format("{}v{}.metadata.json", table_location, version);
129+
auto status = TableMetadataUtil::Write(*file_io_, metadata_location, *metadata);
130+
EXPECT_THAT(status, IsOk());
131+
132+
return std::make_unique<Table>(tableIdent, std::move(metadata), metadata_location,
133+
file_io_, std::static_pointer_cast<Catalog>(catalog_));
134+
};
135+
136+
auto table_v0 = buildTable(3051729675574597004, 0);
137+
auto table_v1 = buildTable(3055729675574597004, 1);
138+
EXPECT_CALL(*catalog_, LoadTable(::testing::_))
139+
.WillOnce(::testing::Return(
140+
::testing::ByMove(Result<std::unique_ptr<Table>>(std::move(table_v0)))))
141+
.WillOnce(::testing::Return(
142+
::testing::ByMove(Result<std::unique_ptr<Table>>(std::move(table_v1)))));
143+
144+
auto metadata_location = std::format("{}v{}.metadata.json", table_location, 0);
129145
auto table = catalog_->RegisterTable(tableIdent, metadata_location);
130146
EXPECT_THAT(table, IsOk());
131147
ASSERT_TRUE(table.value()->current_snapshot().has_value());
132148
ASSERT_EQ(table.value()->current_snapshot().value()->snapshot_id, 3051729675574597004);
133149

134-
// update table version to 3055729675574597004
135-
metadata_location = std::format("{}v1.metadata.json", table_location);
136-
metadata->current_snapshot_id = 3055729675574597004;
137-
status = TableMetadataUtil::Write(*file_io_, metadata_location, *metadata);
138-
EXPECT_THAT(status, IsOk());
139-
140-
// update table metadata location in catalog
141-
status = catalog_->UpdateTableMetaLocation(tableIdent, metadata_location);
142-
EXPECT_THAT(status, IsOk());
143-
144150
// same version
145-
status = table.value()->Refresh();
151+
auto status = table.value()->Refresh();
146152
EXPECT_THAT(status, IsOk());
147153
ASSERT_TRUE(table.value()->current_snapshot().has_value());
148154
ASSERT_EQ(table.value()->current_snapshot().value()->snapshot_id, 3055729675574597004);

test/mock_catalog.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ class ICEBERG_EXPORT MockInMemoryCatalog : public InMemoryCatalog {
3030
std::unordered_map<std::string, std::string> const& properties)
3131
: InMemoryCatalog(name, file_io, warehouse_location, properties) {}
3232

33-
/// \brief Directly sets the metadata location of a table.
34-
/// \note This should only be used in unit tests.
35-
Status UpdateTableMetaLocation(const TableIdentifier& identifier,
36-
const std::string& metadata_location) {
37-
return UpdateTableMetaLocationInternal(identifier, metadata_location);
38-
}
33+
MOCK_METHOD(Result<std::unique_ptr<Table>>, LoadTable, (const TableIdentifier&),
34+
(override));
3935
};
4036
} // namespace iceberg

0 commit comments

Comments
 (0)