Skip to content

Commit 8e29a16

Browse files
author
shuxu.li
committed
feat: Refresh method support for table
1 parent 5bffdf6 commit 8e29a16

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/iceberg/table.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,36 @@
2121

2222
#include <algorithm>
2323

24+
#include "iceberg/catalog.h"
2425
#include "iceberg/partition_spec.h"
2526
#include "iceberg/schema.h"
2627
#include "iceberg/sort_order.h"
2728
#include "iceberg/table_metadata.h"
2829
#include "iceberg/table_scan.h"
30+
#include "iceberg/util/macros.h"
2931

3032
namespace iceberg {
3133

3234
const std::string& Table::uuid() const { return metadata_->table_uuid; }
3335

36+
Status Table::Refresh() {
37+
if (!catalog_) {
38+
return InvalidArgument("Cannot refresh table metadata without a catalog");
39+
}
40+
41+
ICEBERG_ASSIGN_OR_RAISE(auto fresh, catalog_->LoadTable(identifier_));
42+
if (metadata_location_ != fresh->metadata_location_) {
43+
metadata_ = std::move(fresh->metadata_);
44+
metadata_location_ = std::move(fresh->metadata_location_);
45+
io_ = std::move(fresh->io_);
46+
47+
schemas_map_.reset();
48+
partition_spec_map_.reset();
49+
sort_orders_map_.reset();
50+
}
51+
return {};
52+
}
53+
3454
Result<std::shared_ptr<Schema>> Table::schema() const { return metadata_->Schema(); }
3555

3656
const std::shared_ptr<std::unordered_map<int32_t, std::shared_ptr<Schema>>>&

src/iceberg/table.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class ICEBERG_EXPORT Table {
5757
/// \brief Returns the UUID of the table
5858
const std::string& uuid() const;
5959

60+
/// \brief Refresh the current table metadata
61+
Status Refresh();
62+
6063
/// \brief Return the schema for this table, return NotFoundError if not found
6164
Result<std::shared_ptr<Schema>> schema() const;
6265

@@ -114,9 +117,11 @@ class ICEBERG_EXPORT Table {
114117
const std::shared_ptr<FileIO>& io() const;
115118

116119
private:
120+
friend class Table;
121+
117122
const TableIdentifier identifier_;
118123
std::shared_ptr<TableMetadata> metadata_;
119-
const std::string metadata_location_;
124+
std::string metadata_location_;
120125
std::shared_ptr<FileIO> io_;
121126
std::shared_ptr<Catalog> catalog_;
122127

test/in_memory_catalog_test.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,30 @@ TEST_F(InMemoryCatalogTest, RegisterTable) {
115115
ASSERT_EQ(table.value()->location(), "s3://bucket/test/location");
116116
}
117117

118+
TEST_F(InMemoryCatalogTest, RefreshTable) {
119+
TableIdentifier tableIdent{.ns = {}, .name = "t1"};
120+
121+
std::unique_ptr<TableMetadata> metadata;
122+
ASSERT_NO_FATAL_FAILURE(ReadTableMetadata("TableMetadataV2Valid.json", &metadata));
123+
124+
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+
129+
auto table = catalog_->RegisterTable(tableIdent, metadata_location);
130+
EXPECT_THAT(table, IsOk());
131+
ASSERT_TRUE(table.value()->current_snapshot().has_value());
132+
ASSERT_EQ(table.value()->current_snapshot().value()->snapshot_id, 3055729675574597004);
133+
134+
// Now we don't support commit method in catalog, so here only test refresh with the
135+
// same version
136+
status = table.value()->Refresh();
137+
EXPECT_THAT(status, IsOk());
138+
ASSERT_TRUE(table.value()->current_snapshot().has_value());
139+
ASSERT_EQ(table.value()->current_snapshot().value()->snapshot_id, 3055729675574597004);
140+
}
141+
118142
TEST_F(InMemoryCatalogTest, DropTable) {
119143
TableIdentifier tableIdent{.ns = {}, .name = "t1"};
120144
auto result = catalog_->DropTable(tableIdent, false);

0 commit comments

Comments
 (0)