WZhuo commented on code in PR #345:
URL: https://github.com/apache/iceberg-cpp/pull/345#discussion_r2570307633
##########
src/iceberg/table_metadata.cc:
##########
@@ -239,7 +250,38 @@ struct TableMetadataBuilder::Impl {
// Constructor from existing metadata
explicit Impl(const TableMetadata* base_metadata)
- : base(base_metadata), metadata(*base_metadata) {}
+ : base(base_metadata), metadata(*base_metadata) {
+ // Initialize index maps from base metadata
+ for (const auto& schema : metadata.schemas) {
+ if (schema->schema_id().has_value()) {
+ schemas_by_id.emplace(schema->schema_id().value(), schema);
+ }
+ }
+
+ for (const auto& spec : metadata.partition_specs) {
+ specs_by_id.emplace(spec->spec_id(), spec);
+ }
+
+ for (const auto& order : metadata.sort_orders) {
+ sort_orders_by_id.emplace(order->order_id(), order);
+ }
+ }
+
+ int32_t reuseOrCreateNewSortOrderId(const SortOrder& new_order) {
+ if (new_order.is_unsorted()) {
+ return SortOrder::kUnsortedOrderId;
+ }
+ // determine the next order id
+ int32_t new_order_id = SortOrder::kInitialSortOrderId;
+ for (const auto& order : metadata.sort_orders) {
+ if (order->SameOrder(new_order)) {
+ return order->order_id();
+ } else if (new_order_id <= order->order_id()) {
+ new_order_id = order->order_id() + 1;
+ }
+ }
+ return new_order_id;
Review Comment:
can optimize if metadata.sort_orders is sorted by order_id
```suggestion
for (const auto& order : metadata.sort_orders) {
if (order->SameOrder(new_order)) {
return order->order_id();
}
}
return metadata.sort_orders.empty() ? SortOrder::kInitialSortOrderId :
metadata.sort_orders.back()->order_id() + 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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]