You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "WZhuo (via GitHub)" <gi...@apache.org> on 2025/12/03 10:10:20 UTC
[PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
WZhuo opened a new pull request, #386:
URL: https://github.com/apache/iceberg-cpp/pull/386
(no comment)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "WZhuo (via GitHub)" <gi...@apache.org>.
WZhuo commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2597422210
##########
src/iceberg/catalog/memory/in_memory_catalog.cc:
##########
@@ -394,7 +411,32 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable(
const TableIdentifier& identifier,
const std::vector<std::unique_ptr<TableRequirement>>& requirements,
const std::vector<std::unique_ptr<TableUpdate>>& updates) {
- return NotImplemented("update table");
+ std::unique_lock lock(mutex_);
+ ICEBERG_ASSIGN_OR_RAISE(auto metadata_location,
+ root_namespace_->GetTableMetadataLocation(identifier));
+
+ ICEBERG_ASSIGN_OR_RAISE(auto base,
+ TableMetadataUtil::Read(*file_io_, metadata_location));
+ base->metadata_file_location = metadata_location;
Review Comment:
Fix it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "HuaHuaY (via GitHub)" <gi...@apache.org>.
HuaHuaY commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2593323488
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +290,95 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata* metadata) {
+ ICEBERG_CHECK(metadata != nullptr, "The metadata is nullptr.");
+
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(*metadata, version + 1));
Review Comment:
Should we plus one when parsing version from location?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "WZhuo (via GitHub)" <gi...@apache.org>.
WZhuo commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2597540704
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +290,95 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata* metadata) {
+ ICEBERG_CHECK(metadata != nullptr, "The metadata is nullptr.");
+
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(*metadata, version + 1));
Review Comment:
The ParseVersionFromLocation method only gets the version of a specified file, not the next version. I think this makes the function's semantics clearer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "WZhuo (via GitHub)" <gi...@apache.org>.
WZhuo commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2597403301
##########
src/iceberg/catalog/memory/in_memory_catalog.cc:
##########
@@ -394,7 +411,32 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable(
const TableIdentifier& identifier,
const std::vector<std::unique_ptr<TableRequirement>>& requirements,
const std::vector<std::unique_ptr<TableUpdate>>& updates) {
- return NotImplemented("update table");
+ std::unique_lock lock(mutex_);
Review Comment:
I think it can be optimized with ReadWriteLock later
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "HuaHuaY (via GitHub)" <gi...@apache.org>.
HuaHuaY commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2597472131
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +290,95 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
Review Comment:
You can turn `metadata` into reference.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac merged PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "WZhuo (via GitHub)" <gi...@apache.org>.
WZhuo commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2618425126
##########
src/iceberg/table_metadata.cc:
##########
@@ -220,30 +226,57 @@ Result<TableMetadataCache::SnapshotsMap> TableMetadataCache::InitSnapshotMap(
std::ranges::to<SnapshotsMap>();
}
-// TableMetadataUtil implementation
+Result<MetadataFileCodecType> CodecTypeUtil::CodecFromString(
Review Comment:
How about redefine it in the TableMetadataUtil, TableMetadataUtil::Codec
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "WZhuo (via GitHub)" <gi...@apache.org>.
WZhuo commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2597388446
##########
src/iceberg/catalog/memory/in_memory_catalog.cc:
##########
@@ -314,6 +324,13 @@ Result<std::string> InMemoryNamespace::GetTableMetadataLocation(
return it->second;
}
+Status InMemoryNamespace::UpdateTableMetadataLocation(
+ TableIdentifier const& table_ident, std::string const& metadata_location) {
Review Comment:
Fix all function declaration style in this file.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "WZhuo (via GitHub)" <gi...@apache.org>.
WZhuo commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2597382191
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +290,95 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
Review Comment:
The base can be nullptr, indicating that this is creating a new table.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "HuaHuaY (via GitHub)" <gi...@apache.org>.
HuaHuaY commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2597464575
##########
src/iceberg/table_metadata.h:
##########
@@ -458,14 +467,41 @@ enum class ICEBERG_EXPORT MetadataFileCodecType {
kGzip,
};
-/// \brief Utility class for table metadata
-struct ICEBERG_EXPORT TableMetadataUtil {
+struct ICEBERG_EXPORT CodecTypeUtil {
+ /// \brief Returns the MetadataFileCodecType corresponding to the given string.
+ ///
+ /// \param name The string to parse.
+ /// \return The MetadataFileCodecType corresponding to the given string.
+ static Result<MetadataFileCodecType> CodecFromString(const std::string_view& name);
+
/// \brief Get the codec type from the table metadata file name.
///
/// \param file_name The name of the table metadata file.
/// \return The codec type of the table metadata file.
static Result<MetadataFileCodecType> CodecFromFileName(std::string_view file_name);
+ /// \brief Get the file extension from the codec type.
+ /// \param codec The codec name.
+ /// \return The file extension of the codec.
+ static Result<std::string> CodecNameToFileExtension(const std::string_view& codec);
+
+ /// \brief Get the file extension from the codec type.
+ /// \param codec The codec type.
+ /// \return The file extension of the codec.
+ static std::string CodecTypeToFileExtension(MetadataFileCodecType codec);
+
+ inline static constexpr std::string_view kTableMetadataFileSuffix = ".metadata.json";
Review Comment:
Sorry that I have no time last week so I didn't review your previous PR. This project is under at least C++23, so we don't need inline to avoid ODR violations.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "HuaHuaY (via GitHub)" <gi...@apache.org>.
HuaHuaY commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2593272962
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +290,95 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
Review Comment:
How about using reference instead of raw pointer?
##########
src/iceberg/catalog/memory/in_memory_catalog.cc:
##########
@@ -394,7 +411,32 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable(
const TableIdentifier& identifier,
const std::vector<std::unique_ptr<TableRequirement>>& requirements,
const std::vector<std::unique_ptr<TableUpdate>>& updates) {
- return NotImplemented("update table");
+ std::unique_lock lock(mutex_);
Review Comment:
```suggestion
std::lock_guard guard(mutex_);
```
Use `std::lock_guard` instead of `std::unique_lock` if we don't need to move the guard. Anyone interested can help modify the other `std::unique_lock` in this file.
##########
src/iceberg/table_metadata.h:
##########
@@ -458,14 +467,41 @@ enum class ICEBERG_EXPORT MetadataFileCodecType {
kGzip,
};
-/// \brief Utility class for table metadata
-struct ICEBERG_EXPORT TableMetadataUtil {
+struct ICEBERG_EXPORT CodecTypeUtil {
+ /// \brief Returns the MetadataFileCodecType corresponding to the given string.
+ ///
+ /// \param name The string to parse.
+ /// \return The MetadataFileCodecType corresponding to the given string.
+ static Result<MetadataFileCodecType> CodecFromString(const std::string_view& name);
+
/// \brief Get the codec type from the table metadata file name.
///
/// \param file_name The name of the table metadata file.
/// \return The codec type of the table metadata file.
static Result<MetadataFileCodecType> CodecFromFileName(std::string_view file_name);
+ /// \brief Get the file extension from the codec type.
+ /// \param codec The codec name.
+ /// \return The file extension of the codec.
+ static Result<std::string> CodecNameToFileExtension(const std::string_view& codec);
+
+ /// \brief Get the file extension from the codec type.
+ /// \param codec The codec type.
+ /// \return The file extension of the codec.
+ static std::string CodecTypeToFileExtension(MetadataFileCodecType codec);
+
+ inline static constexpr std::string_view kTableMetadataFileSuffix = ".metadata.json";
Review Comment:
`static constexpr` is implicitly `inline` and we can remove `inline` here.
##########
src/iceberg/catalog/memory/in_memory_catalog.cc:
##########
@@ -314,6 +324,13 @@ Result<std::string> InMemoryNamespace::GetTableMetadataLocation(
return it->second;
}
+Status InMemoryNamespace::UpdateTableMetadataLocation(
+ TableIdentifier const& table_ident, std::string const& metadata_location) {
Review Comment:
```suggestion
const TableIdentifier& table_ident, const std::string& metadata_location) {
```
I think it's better to keep the same style `const T&` as function declaration.
##########
src/iceberg/table_metadata.cc:
##########
@@ -664,7 +781,32 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataBuilder::Build() {
std::chrono::system_clock::now().time_since_epoch())};
}
- // 4. Create and return the TableMetadata
+ // 4. Buildup metadata_log from base metadata
+ int32_t max_metadata_log_size = TableProperties::kMetadataPreviousVersionsMax.value();
+ if (auto iter = impl_->metadata.properties.find(
+ TableProperties::kMetadataPreviousVersionsMax.key());
+ iter != impl_->metadata.properties.end()) {
+ try {
+ max_metadata_log_size = std::stoi(iter->second);
Review Comment:
ditto
##########
src/iceberg/catalog/memory/in_memory_catalog.cc:
##########
@@ -394,7 +411,32 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable(
const TableIdentifier& identifier,
const std::vector<std::unique_ptr<TableRequirement>>& requirements,
const std::vector<std::unique_ptr<TableUpdate>>& updates) {
- return NotImplemented("update table");
+ std::unique_lock lock(mutex_);
+ ICEBERG_ASSIGN_OR_RAISE(auto metadata_location,
+ root_namespace_->GetTableMetadataLocation(identifier));
+
+ ICEBERG_ASSIGN_OR_RAISE(auto base,
+ TableMetadataUtil::Read(*file_io_, metadata_location));
+ base->metadata_file_location = metadata_location;
Review Comment:
```suggestion
base->metadata_file_location = std::move(metadata_location);
```
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +290,95 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata* metadata) {
+ ICEBERG_CHECK(metadata != nullptr, "The metadata is nullptr.");
+
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(*metadata, version + 1));
+ ICEBERG_RETURN_UNEXPECTED(Write(io, new_file_location, *metadata));
+ metadata->metadata_file_location = std::move(new_file_location);
+ return {};
+}
+
Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
const TableMetadata& metadata) {
auto json = ToJson(metadata);
ICEBERG_ASSIGN_OR_RAISE(auto json_string, ToJsonString(json));
return io.WriteFile(location, json_string);
}
+void TableMetadataUtil::DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
+ const TableMetadata* metadata) {
+ if (!base) {
+ return;
+ }
+
+ bool delete_after_commit = TableProperties::kMetadataDeleteAfterCommitEnabled.value();
+ if (auto it = metadata->properties.find(
+ TableProperties::kMetadataDeleteAfterCommitEnabled.key());
+ it != metadata->properties.end()) {
+ delete_after_commit = StringUtils::ToLower(it->second) == "true" || it->second == "1";
Review Comment:
```suggestion
delete_after_commit = StringUtils::EqualsIgnoreCase(it->second, "true") || it->second == "1";
```
`StringUtils::ToLower` allocates new memory and `StringUtils::EqualsIgnoreCase` doesn't.
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +290,95 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata* metadata) {
+ ICEBERG_CHECK(metadata != nullptr, "The metadata is nullptr.");
+
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(*metadata, version + 1));
Review Comment:
How about using `0` as init value and removing `+ 1`?
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +290,95 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata* metadata) {
+ ICEBERG_CHECK(metadata != nullptr, "The metadata is nullptr.");
+
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(*metadata, version + 1));
+ ICEBERG_RETURN_UNEXPECTED(Write(io, new_file_location, *metadata));
+ metadata->metadata_file_location = std::move(new_file_location);
+ return {};
+}
+
Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
const TableMetadata& metadata) {
auto json = ToJson(metadata);
ICEBERG_ASSIGN_OR_RAISE(auto json_string, ToJsonString(json));
return io.WriteFile(location, json_string);
}
+void TableMetadataUtil::DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
+ const TableMetadata* metadata) {
+ if (!base) {
+ return;
+ }
+
+ bool delete_after_commit = TableProperties::kMetadataDeleteAfterCommitEnabled.value();
+ if (auto it = metadata->properties.find(
+ TableProperties::kMetadataDeleteAfterCommitEnabled.key());
+ it != metadata->properties.end()) {
+ delete_after_commit = StringUtils::ToLower(it->second) == "true" || it->second == "1";
+ }
+
+ if (delete_after_commit) {
+ std::ranges::for_each(
+ base->metadata_log |
+ std::views::filter(
+ [current_files =
+ metadata->metadata_log |
+ std::ranges::to<std::unordered_set<MetadataLogEntry,
+ MetadataLogEntry::Hasher>>()](
+ const auto& entry) { return !current_files.contains(entry); }),
+ [&io](const auto& entry) { auto status = io.DeleteFile(entry.metadata_file); });
Review Comment:
```suggestion
auto current_files =
metadata->metadata_log |
std::ranges::to<std::unordered_set<MetadataLogEntry, MetadataLogEntry::Hasher>>();
std::ranges::for_each(
base->metadata_log | std::views::filter([¤t_files](const auto& entry) {
return !current_files.contains(entry);
}),
[&io](const auto& entry) { std::ignore = io.DeleteFile(entry.metadata_file); });
```
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +290,95 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata* metadata) {
+ ICEBERG_CHECK(metadata != nullptr, "The metadata is nullptr.");
+
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(*metadata, version + 1));
+ ICEBERG_RETURN_UNEXPECTED(Write(io, new_file_location, *metadata));
+ metadata->metadata_file_location = std::move(new_file_location);
+ return {};
+}
+
Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
const TableMetadata& metadata) {
auto json = ToJson(metadata);
ICEBERG_ASSIGN_OR_RAISE(auto json_string, ToJsonString(json));
return io.WriteFile(location, json_string);
}
+void TableMetadataUtil::DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
+ const TableMetadata* metadata) {
+ if (!base) {
+ return;
+ }
+
+ bool delete_after_commit = TableProperties::kMetadataDeleteAfterCommitEnabled.value();
+ if (auto it = metadata->properties.find(
+ TableProperties::kMetadataDeleteAfterCommitEnabled.key());
+ it != metadata->properties.end()) {
+ delete_after_commit = StringUtils::ToLower(it->second) == "true" || it->second == "1";
+ }
+
+ if (delete_after_commit) {
+ std::ranges::for_each(
+ base->metadata_log |
+ std::views::filter(
+ [current_files =
+ metadata->metadata_log |
+ std::ranges::to<std::unordered_set<MetadataLogEntry,
+ MetadataLogEntry::Hasher>>()](
+ const auto& entry) { return !current_files.contains(entry); }),
+ [&io](const auto& entry) { auto status = io.DeleteFile(entry.metadata_file); });
+ }
+}
+
+int TableMetadataUtil::ParseVersionFromLocation(const std::string& metadata_location) {
+ size_t version_start = metadata_location.find_last_of('/') + 1;
+ size_t version_end = metadata_location.find('-', version_start);
+
+ if (version_end == std::string::npos) {
+ // found filesystem table's metadata
+ return -1;
+ }
+
+ try {
+ return std::stoi(
+ metadata_location.substr(version_start, version_end - version_start));
+ } catch (const std::exception& e) {
+ // Unable to parse version from metadata location
+ return -1;
+ }
+}
+
+Result<std::string> TableMetadataUtil::NewTableMetadataFilePath(const TableMetadata& meta,
+ int new_version) {
+ std::string codec_name = "none";
Review Comment:
```suggestion
std::string_view codec_name = "none";
```
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +290,95 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata* metadata) {
+ ICEBERG_CHECK(metadata != nullptr, "The metadata is nullptr.");
+
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(*metadata, version + 1));
+ ICEBERG_RETURN_UNEXPECTED(Write(io, new_file_location, *metadata));
+ metadata->metadata_file_location = std::move(new_file_location);
+ return {};
+}
+
Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
const TableMetadata& metadata) {
auto json = ToJson(metadata);
ICEBERG_ASSIGN_OR_RAISE(auto json_string, ToJsonString(json));
return io.WriteFile(location, json_string);
}
+void TableMetadataUtil::DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
+ const TableMetadata* metadata) {
+ if (!base) {
+ return;
+ }
+
+ bool delete_after_commit = TableProperties::kMetadataDeleteAfterCommitEnabled.value();
+ if (auto it = metadata->properties.find(
+ TableProperties::kMetadataDeleteAfterCommitEnabled.key());
+ it != metadata->properties.end()) {
+ delete_after_commit = StringUtils::ToLower(it->second) == "true" || it->second == "1";
+ }
+
+ if (delete_after_commit) {
+ std::ranges::for_each(
+ base->metadata_log |
+ std::views::filter(
+ [current_files =
+ metadata->metadata_log |
+ std::ranges::to<std::unordered_set<MetadataLogEntry,
+ MetadataLogEntry::Hasher>>()](
+ const auto& entry) { return !current_files.contains(entry); }),
+ [&io](const auto& entry) { auto status = io.DeleteFile(entry.metadata_file); });
+ }
+}
+
+int TableMetadataUtil::ParseVersionFromLocation(const std::string& metadata_location) {
+ size_t version_start = metadata_location.find_last_of('/') + 1;
+ size_t version_end = metadata_location.find('-', version_start);
+
+ if (version_end == std::string::npos) {
+ // found filesystem table's metadata
+ return -1;
+ }
+
+ try {
+ return std::stoi(
Review Comment:
If we ensure there are only numbers here, we can use `std::from_chars` here. It doesn't need to use `substr` to create a new string and is faster than `std::stoi`.
```cpp
#include <charconv>
int result;
if (std::from_chars(metadata_location.data() + version_start,
metadata_location.data() + version_end, result)
.ec == std::errc()) {
return result;
} else {
return -1;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "zhjwpku (via GitHub)" <gi...@apache.org>.
zhjwpku commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2592630513
##########
src/iceberg/util/location_util.h:
##########
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <string>
+
+#include "iceberg/iceberg_export.h"
+
+namespace iceberg {
+
+class ICEBERG_EXPORT LocationUtil {
+ public:
+ static std::string StripTrailingSlash(const std::string& path) {
+ if (path.empty()) {
+ return "";
+ }
+
+ std::string_view result = path;
+ while (!result.ends_with("://") && result.ends_with("/")) {
Review Comment:
```suggestion
while (result.ends_with("/") && !result.ends_with("://")) {
```
This might short circuit more quickly, not a strong opinion though.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "WZhuo (via GitHub)" <gi...@apache.org>.
WZhuo commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2597423970
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +290,95 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata* metadata) {
+ ICEBERG_CHECK(metadata != nullptr, "The metadata is nullptr.");
+
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(*metadata, version + 1));
+ ICEBERG_RETURN_UNEXPECTED(Write(io, new_file_location, *metadata));
+ metadata->metadata_file_location = std::move(new_file_location);
+ return {};
+}
+
Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
const TableMetadata& metadata) {
auto json = ToJson(metadata);
ICEBERG_ASSIGN_OR_RAISE(auto json_string, ToJsonString(json));
return io.WriteFile(location, json_string);
}
+void TableMetadataUtil::DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
+ const TableMetadata* metadata) {
+ if (!base) {
+ return;
+ }
+
+ bool delete_after_commit = TableProperties::kMetadataDeleteAfterCommitEnabled.value();
+ if (auto it = metadata->properties.find(
+ TableProperties::kMetadataDeleteAfterCommitEnabled.key());
+ it != metadata->properties.end()) {
+ delete_after_commit = StringUtils::ToLower(it->second) == "true" || it->second == "1";
Review Comment:
Fix it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "WZhuo (via GitHub)" <gi...@apache.org>.
WZhuo commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2597377101
##########
src/iceberg/table_metadata.h:
##########
@@ -458,14 +467,41 @@ enum class ICEBERG_EXPORT MetadataFileCodecType {
kGzip,
};
-/// \brief Utility class for table metadata
-struct ICEBERG_EXPORT TableMetadataUtil {
+struct ICEBERG_EXPORT CodecTypeUtil {
+ /// \brief Returns the MetadataFileCodecType corresponding to the given string.
+ ///
+ /// \param name The string to parse.
+ /// \return The MetadataFileCodecType corresponding to the given string.
+ static Result<MetadataFileCodecType> CodecFromString(const std::string_view& name);
+
/// \brief Get the codec type from the table metadata file name.
///
/// \param file_name The name of the table metadata file.
/// \return The codec type of the table metadata file.
static Result<MetadataFileCodecType> CodecFromFileName(std::string_view file_name);
+ /// \brief Get the file extension from the codec type.
+ /// \param codec The codec name.
+ /// \return The file extension of the codec.
+ static Result<std::string> CodecNameToFileExtension(const std::string_view& codec);
+
+ /// \brief Get the file extension from the codec type.
+ /// \param codec The codec type.
+ /// \return The file extension of the codec.
+ static std::string CodecTypeToFileExtension(MetadataFileCodecType codec);
+
+ inline static constexpr std::string_view kTableMetadataFileSuffix = ".metadata.json";
Review Comment:
It's implicitly inline in c++20+, It's recommended to explicitly define as inline in my previous PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "HuaHuaY (via GitHub)" <gi...@apache.org>.
HuaHuaY commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2597562926
##########
src/iceberg/catalog/memory/in_memory_catalog.cc:
##########
@@ -394,7 +411,32 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable(
const TableIdentifier& identifier,
const std::vector<std::unique_ptr<TableRequirement>>& requirements,
const std::vector<std::unique_ptr<TableUpdate>>& updates) {
- return NotImplemented("update table");
+ std::unique_lock lock(mutex_);
Review Comment:
I agree that we may be able to replace the lock type with `std::shared_lock`. If you don't do that in this PR, I hope that you can replace with `std::lock_guard` first. Because we have merged a PR https://github.com/apache/iceberg-cpp/pull/398 which replaced the other `unique_lock` in this file.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2614660575
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +291,88 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata& metadata) {
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(metadata, version + 1));
+ ICEBERG_RETURN_UNEXPECTED(Write(io, new_file_location, metadata));
+ metadata.metadata_file_location = std::move(new_file_location);
+ return {};
+}
+
Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
const TableMetadata& metadata) {
auto json = ToJson(metadata);
ICEBERG_ASSIGN_OR_RAISE(auto json_string, ToJsonString(json));
return io.WriteFile(location, json_string);
}
+void TableMetadataUtil::DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
+ const TableMetadata& metadata) {
+ if (!base) {
+ return;
+ }
+
+ bool delete_after_commit = TableProperties::kMetadataDeleteAfterCommitEnabled.value();
+ if (auto it = metadata.properties.find(
Review Comment:
Created https://github.com/apache/iceberg-cpp/pull/407
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#issuecomment-3632442523
This PR seems have carried other unrelated changes. Could you fix it?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "WZhuo (via GitHub)" <gi...@apache.org>.
WZhuo commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2618385210
##########
src/iceberg/table_metadata.h:
##########
@@ -76,6 +83,8 @@ struct ICEBERG_EXPORT TableMetadata {
static constexpr int64_t kInvalidSequenceNumber = -1;
static constexpr int64_t kInitialRowId = 0;
+ /// The location of the table metadata file
+ std::string metadata_file_location;
Review Comment:
If seperate metadata_file_locatin field from TableMetadata, we need to convert more fields in some function
```
Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base, TableMetadata& metadata)
=>
Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base, const std::string& base_location, TableMetadata& metadata, std::string& new_location)
```
This filed is highly correlative with TableMetadata.BTW, it's also a filed of TableMetadata in Java impl.
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +291,88 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata& metadata) {
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(metadata, version + 1));
+ ICEBERG_RETURN_UNEXPECTED(Write(io, new_file_location, metadata));
+ metadata.metadata_file_location = std::move(new_file_location);
+ return {};
+}
+
Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
const TableMetadata& metadata) {
auto json = ToJson(metadata);
ICEBERG_ASSIGN_OR_RAISE(auto json_string, ToJsonString(json));
return io.WriteFile(location, json_string);
}
+void TableMetadataUtil::DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
+ const TableMetadata& metadata) {
+ if (!base) {
+ return;
+ }
+
+ bool delete_after_commit = TableProperties::kMetadataDeleteAfterCommitEnabled.value();
+ if (auto it = metadata.properties.find(
+ TableProperties::kMetadataDeleteAfterCommitEnabled.key());
+ it != metadata.properties.end()) {
+ delete_after_commit =
+ StringUtils::EqualsIgnoreCase(it->second, "true") || it->second == "1";
+ }
+
+ if (delete_after_commit) {
+ auto current_files =
+ metadata.metadata_log |
+ std::ranges::to<std::unordered_set<MetadataLogEntry, MetadataLogEntry::Hasher>>();
+ std::ranges::for_each(
+ base->metadata_log | std::views::filter([¤t_files](const auto& entry) {
+ return !current_files.contains(entry);
+ }),
+ [&io](const auto& entry) { auto status = io.DeleteFile(entry.metadata_file); });
+ }
+}
+
+int TableMetadataUtil::ParseVersionFromLocation(
+ const std::string_view& metadata_location) {
+ size_t version_start = metadata_location.find_last_of('/') + 1;
Review Comment:
It's Ok if '/' does not exist, `version_end` will be std::string::npos, and return -1.
##########
src/iceberg/table_metadata.h:
##########
@@ -58,6 +59,12 @@ struct ICEBERG_EXPORT MetadataLogEntry {
friend bool operator==(const MetadataLogEntry& lhs, const MetadataLogEntry& rhs) {
return lhs.timestamp_ms == rhs.timestamp_ms && lhs.metadata_file == rhs.metadata_file;
}
+
+ struct Hasher {
Review Comment:
SnapshotLogEntry not used currently. Has leave a TODO in TableMetadataBuilder::Build
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2613048404
##########
src/iceberg/table_metadata.h:
##########
@@ -458,14 +467,40 @@ enum class ICEBERG_EXPORT MetadataFileCodecType {
kGzip,
};
-/// \brief Utility class for table metadata
-struct ICEBERG_EXPORT TableMetadataUtil {
+struct ICEBERG_EXPORT CodecTypeUtil {
+ /// \brief Returns the MetadataFileCodecType corresponding to the given string.
+ ///
+ /// \param name The string to parse.
+ /// \return The MetadataFileCodecType corresponding to the given string.
+ static Result<MetadataFileCodecType> CodecFromString(const std::string_view& name);
Review Comment:
```suggestion
static Result<MetadataFileCodecType> CodecFromString(std::string_view name);
```
It is recommended to pass string_view by value.
##########
src/iceberg/table_metadata.cc:
##########
@@ -220,30 +226,57 @@ Result<TableMetadataCache::SnapshotsMap> TableMetadataCache::InitSnapshotMap(
std::ranges::to<SnapshotsMap>();
}
-// TableMetadataUtil implementation
+Result<MetadataFileCodecType> CodecTypeUtil::CodecFromString(
Review Comment:
Perhaps `TableMetadataUtil` should be renamed to `TableMetadataFileUtil` so that we don't need to split a `CodecTypeUtil`? `CodecTypeUtil` is also confusing because it is tight to codec supported by only metadata file not those general codecs supported by data files (e.g. parquet).
##########
src/iceberg/table_metadata.cc:
##########
@@ -48,6 +52,8 @@ const TimePointMs kInvalidLastUpdatedMs = TimePointMs::min();
constexpr int32_t kLastAdded = -1;
} // namespace
+static constexpr std::string_view kMetadataFolderName = "metadata";
Review Comment:
Can we move it to the anonymous namespace above?
##########
src/iceberg/table_metadata.h:
##########
@@ -76,6 +83,8 @@ struct ICEBERG_EXPORT TableMetadata {
static constexpr int64_t kInvalidSequenceNumber = -1;
static constexpr int64_t kInitialRowId = 0;
+ /// The location of the table metadata file
+ std::string metadata_file_location;
Review Comment:
I'm hesitant to introduce this field to the `TableMetadata` because `TableMetadata` aims to carry fields that only exist in the metadata json. Can we revert this?
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +291,88 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata& metadata) {
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(metadata, version + 1));
+ ICEBERG_RETURN_UNEXPECTED(Write(io, new_file_location, metadata));
+ metadata.metadata_file_location = std::move(new_file_location);
+ return {};
+}
+
Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
const TableMetadata& metadata) {
auto json = ToJson(metadata);
ICEBERG_ASSIGN_OR_RAISE(auto json_string, ToJsonString(json));
return io.WriteFile(location, json_string);
}
+void TableMetadataUtil::DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
+ const TableMetadata& metadata) {
+ if (!base) {
+ return;
+ }
+
+ bool delete_after_commit = TableProperties::kMetadataDeleteAfterCommitEnabled.value();
+ if (auto it = metadata.properties.find(
+ TableProperties::kMetadataDeleteAfterCommitEnabled.key());
+ it != metadata.properties.end()) {
+ delete_after_commit =
+ StringUtils::EqualsIgnoreCase(it->second, "true") || it->second == "1";
+ }
+
+ if (delete_after_commit) {
+ auto current_files =
+ metadata.metadata_log |
+ std::ranges::to<std::unordered_set<MetadataLogEntry, MetadataLogEntry::Hasher>>();
+ std::ranges::for_each(
+ base->metadata_log | std::views::filter([¤t_files](const auto& entry) {
+ return !current_files.contains(entry);
+ }),
+ [&io](const auto& entry) { auto status = io.DeleteFile(entry.metadata_file); });
Review Comment:
```suggestion
[&io](const auto& entry) { std::ignore = io.DeleteFile(entry.metadata_file); });
```
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +291,88 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata& metadata) {
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(metadata, version + 1));
+ ICEBERG_RETURN_UNEXPECTED(Write(io, new_file_location, metadata));
+ metadata.metadata_file_location = std::move(new_file_location);
+ return {};
+}
+
Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
const TableMetadata& metadata) {
auto json = ToJson(metadata);
ICEBERG_ASSIGN_OR_RAISE(auto json_string, ToJsonString(json));
return io.WriteFile(location, json_string);
}
+void TableMetadataUtil::DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
+ const TableMetadata& metadata) {
+ if (!base) {
+ return;
+ }
+
+ bool delete_after_commit = TableProperties::kMetadataDeleteAfterCommitEnabled.value();
+ if (auto it = metadata.properties.find(
+ TableProperties::kMetadataDeleteAfterCommitEnabled.key());
+ it != metadata.properties.end()) {
+ delete_after_commit =
+ StringUtils::EqualsIgnoreCase(it->second, "true") || it->second == "1";
+ }
+
+ if (delete_after_commit) {
+ auto current_files =
+ metadata.metadata_log |
+ std::ranges::to<std::unordered_set<MetadataLogEntry, MetadataLogEntry::Hasher>>();
+ std::ranges::for_each(
+ base->metadata_log | std::views::filter([¤t_files](const auto& entry) {
+ return !current_files.contains(entry);
+ }),
+ [&io](const auto& entry) { auto status = io.DeleteFile(entry.metadata_file); });
+ }
+}
+
+int TableMetadataUtil::ParseVersionFromLocation(
Review Comment:
```suggestion
int32_t TableMetadataUtil::ParseVersionFromLocation(
```
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +291,88 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata& metadata) {
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(metadata, version + 1));
+ ICEBERG_RETURN_UNEXPECTED(Write(io, new_file_location, metadata));
+ metadata.metadata_file_location = std::move(new_file_location);
+ return {};
+}
+
Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
const TableMetadata& metadata) {
auto json = ToJson(metadata);
ICEBERG_ASSIGN_OR_RAISE(auto json_string, ToJsonString(json));
return io.WriteFile(location, json_string);
}
+void TableMetadataUtil::DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
+ const TableMetadata& metadata) {
+ if (!base) {
+ return;
+ }
+
+ bool delete_after_commit = TableProperties::kMetadataDeleteAfterCommitEnabled.value();
+ if (auto it = metadata.properties.find(
Review Comment:
I was thinking to change `TableMetadata::properties` to directly use `std::shared_ptr<TableProperties>` then downstream can easily check any property. I will put up a PR for this.
##########
src/iceberg/table_metadata.cc:
##########
@@ -258,13 +291,88 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
return TableMetadataFromJson(json);
}
+Status TableMetadataUtil::Write(FileIO& io, const TableMetadata* base,
+ TableMetadata& metadata) {
+ int version = -1;
+ if (base != nullptr && !base->metadata_file_location.empty()) {
+ // parse current version from location
+ version = ParseVersionFromLocation(base->metadata_file_location);
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(std::string new_file_location,
+ NewTableMetadataFilePath(metadata, version + 1));
+ ICEBERG_RETURN_UNEXPECTED(Write(io, new_file_location, metadata));
+ metadata.metadata_file_location = std::move(new_file_location);
+ return {};
+}
+
Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
const TableMetadata& metadata) {
auto json = ToJson(metadata);
ICEBERG_ASSIGN_OR_RAISE(auto json_string, ToJsonString(json));
return io.WriteFile(location, json_string);
}
+void TableMetadataUtil::DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
+ const TableMetadata& metadata) {
+ if (!base) {
+ return;
+ }
+
+ bool delete_after_commit = TableProperties::kMetadataDeleteAfterCommitEnabled.value();
+ if (auto it = metadata.properties.find(
+ TableProperties::kMetadataDeleteAfterCommitEnabled.key());
+ it != metadata.properties.end()) {
+ delete_after_commit =
+ StringUtils::EqualsIgnoreCase(it->second, "true") || it->second == "1";
+ }
+
+ if (delete_after_commit) {
+ auto current_files =
+ metadata.metadata_log |
+ std::ranges::to<std::unordered_set<MetadataLogEntry, MetadataLogEntry::Hasher>>();
+ std::ranges::for_each(
+ base->metadata_log | std::views::filter([¤t_files](const auto& entry) {
+ return !current_files.contains(entry);
+ }),
+ [&io](const auto& entry) { auto status = io.DeleteFile(entry.metadata_file); });
+ }
+}
+
+int TableMetadataUtil::ParseVersionFromLocation(
+ const std::string_view& metadata_location) {
+ size_t version_start = metadata_location.find_last_of('/') + 1;
Review Comment:
Is this correct? What if `/` does not exist?
##########
src/iceberg/table_metadata.h:
##########
@@ -458,14 +467,40 @@ enum class ICEBERG_EXPORT MetadataFileCodecType {
kGzip,
};
-/// \brief Utility class for table metadata
-struct ICEBERG_EXPORT TableMetadataUtil {
+struct ICEBERG_EXPORT CodecTypeUtil {
+ /// \brief Returns the MetadataFileCodecType corresponding to the given string.
+ ///
+ /// \param name The string to parse.
+ /// \return The MetadataFileCodecType corresponding to the given string.
+ static Result<MetadataFileCodecType> CodecFromString(const std::string_view& name);
+
/// \brief Get the codec type from the table metadata file name.
///
/// \param file_name The name of the table metadata file.
/// \return The codec type of the table metadata file.
static Result<MetadataFileCodecType> CodecFromFileName(std::string_view file_name);
+ /// \brief Get the file extension from the codec type.
+ /// \param codec The codec name.
+ /// \return The file extension of the codec.
+ static Result<std::string> CodecNameToFileExtension(const std::string_view& codec);
Review Comment:
```suggestion
static Result<std::string> CodecNameToFileExtension(std::string_view codec);
```
##########
src/iceberg/table_metadata.h:
##########
@@ -58,6 +59,12 @@ struct ICEBERG_EXPORT MetadataLogEntry {
friend bool operator==(const MetadataLogEntry& lhs, const MetadataLogEntry& rhs) {
return lhs.timestamp_ms == rhs.timestamp_ms && lhs.metadata_file == rhs.metadata_file;
}
+
+ struct Hasher {
Review Comment:
Why do we need to add this? Why not do this for `SnapshotLogEntry`?
##########
src/iceberg/table_metadata.h:
##########
@@ -476,13 +511,56 @@ struct ICEBERG_EXPORT TableMetadataUtil {
class FileIO& io, const std::string& location,
std::optional<size_t> length = std::nullopt);
+ /// \brief Write a new metadata file to storage.
+ ///
+ /// Serializes the table metadata to JSON and writes it to a new metadata
+ /// file. If no location is specified in the metadata, generates a new
+ /// file path based on the version number.
+ ///
+ /// \param io The FileIO instance for writing files
+ /// \param base The base metadata (can be null for new tables)
+ /// \param metadata The metadata to write, which will be updated with the new location
+ static Status Write(FileIO& io, const TableMetadata* base, TableMetadata& metadata);
+
+ /// \brief Delete removed metadata files based on retention policy.
+ ///
+ /// Removes obsolete metadata files that are no longer referenced in the
+ /// current metadata log, based on the metadata.delete-after-commit.enabled
+ /// property.
+ ///
+ /// \param io The FileIO instance for deleting files
+ /// \param base The previous metadata version
+ /// \param metadata The current metadata containing the updated log
+ static void DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
+ const TableMetadata& metadata);
+
/// \brief Write the table metadata to a file.
///
/// \param io The file IO to use to write the table metadata.
/// \param location The location of the table metadata file.
/// \param metadata The table metadata to write.
static Status Write(FileIO& io, const std::string& location,
const TableMetadata& metadata);
+
+ private:
+ /// \brief Parse the version number from a metadata file location.
+ ///
+ /// Extracts the version number from a metadata file path which follows
+ /// the format: vvvvv-uuid.metadata.json where vvvvv is the zero-padded
+ /// version number.
+ ///
+ /// \param metadata_location The metadata file location string
+ /// \return The parsed version number, or -1 if parsing fails or the
+ /// location doesn't contain a version
+ static int ParseVersionFromLocation(const std::string_view& metadata_location);
Review Comment:
```suggestion
static int ParseVersionFromLocation(std::string_view metadata_location);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "WZhuo (via GitHub)" <gi...@apache.org>.
WZhuo commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2597421557
##########
src/iceberg/util/location_util.h:
##########
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <string>
+
+#include "iceberg/iceberg_export.h"
+
+namespace iceberg {
+
+class ICEBERG_EXPORT LocationUtil {
+ public:
+ static std::string StripTrailingSlash(const std::string& path) {
+ if (path.empty()) {
+ return "";
+ }
+
+ std::string_view result = path;
+ while (!result.ends_with("://") && result.ends_with("/")) {
Review Comment:
Ok, I got it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: Impl InMemoryCatalog's UpdateTable [iceberg-cpp]
Posted by "HuaHuaY (via GitHub)" <gi...@apache.org>.
HuaHuaY commented on code in PR #386:
URL: https://github.com/apache/iceberg-cpp/pull/386#discussion_r2593258828
##########
src/iceberg/catalog/memory/in_memory_catalog.cc:
##########
@@ -394,7 +411,32 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable(
const TableIdentifier& identifier,
const std::vector<std::unique_ptr<TableRequirement>>& requirements,
const std::vector<std::unique_ptr<TableUpdate>>& updates) {
- return NotImplemented("update table");
+ std::unique_lock lock(mutex_);
Review Comment:
```suggestion
std::lock_guard guard(mutex_);
```
Use `std::lock_guard` instead of `std::unique_lock` if we don't need to move the guard.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org