|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/table.h" |
| 21 | + |
| 22 | +#include <iostream> |
| 23 | + |
| 24 | +#include "iceberg/exception.h" |
| 25 | +#include "iceberg/partition_spec.h" |
| 26 | +#include "iceberg/result.h" |
| 27 | +#include "iceberg/schema.h" |
| 28 | +#include "iceberg/snapshot.h" |
| 29 | +#include "iceberg/sort_order.h" |
| 30 | +#include "iceberg/table_metadata.h" |
| 31 | + |
| 32 | +namespace iceberg { |
| 33 | + |
| 34 | +BaseTable::BaseTable(std::string name, std::shared_ptr<TableMetadata> metadata) |
| 35 | + : name_(std::move(name)), metadata_(std::move(metadata)) { |
| 36 | + if (!metadata_) { |
| 37 | + throw std::invalid_argument("Table metadata cannot be null"); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +void BaseTable::InitSchema() const { |
| 42 | + for (const auto& schema : metadata_->schemas) { |
| 43 | + if (schema->schema_id() != std::nullopt) { |
| 44 | + schemas_map_.emplace(schema->schema_id().value(), schema); |
| 45 | + if (schema->schema_id().value() == metadata_->current_schema_id) { |
| 46 | + schema_ = schema; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + // compatible with V1 table schema |
| 51 | + if (!schema_ && metadata_->schemas.size() == 1UL) { |
| 52 | + schema_ = metadata_->schemas.front(); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void BaseTable::InitPartitionSpec() const { |
| 57 | + for (const auto& spec : metadata_->partition_specs) { |
| 58 | + partition_spec_map_[spec->spec_id()] = spec; |
| 59 | + if (spec->spec_id() == metadata_->default_spec_id) { |
| 60 | + partition_spec_ = spec; |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +void BaseTable::InitSortOrder() const { |
| 66 | + for (const auto& order : metadata_->sort_orders) { |
| 67 | + sort_orders_map_[order->order_id()] = order; |
| 68 | + if (order->order_id() == metadata_->default_sort_order_id) { |
| 69 | + sort_order_ = order; |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void BaseTable::InitSnapshot() const { |
| 75 | + auto snapshots = metadata_->snapshots; |
| 76 | + for (const auto& snapshot : snapshots) { |
| 77 | + if (snapshot->snapshot_id == metadata_->current_snapshot_id) { |
| 78 | + current_snapshot_ = snapshot; |
| 79 | + } |
| 80 | + snapshots_map_[snapshot->snapshot_id] = snapshot; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +const std::string& BaseTable::uuid() const { return metadata_->table_uuid; } |
| 85 | + |
| 86 | +const std::shared_ptr<Schema>& BaseTable::schema() const { |
| 87 | + std::call_once(init_schema_once_, [this]() { InitSchema(); }); |
| 88 | + if (!schema_) { |
| 89 | + throw IcebergError("Current schema is not defined for this table"); |
| 90 | + } |
| 91 | + return schema_; |
| 92 | +} |
| 93 | + |
| 94 | +const std::unordered_map<int32_t, std::shared_ptr<Schema>>& BaseTable::schemas() const { |
| 95 | + std::call_once(init_schema_once_, [this]() { InitSchema(); }); |
| 96 | + return schemas_map_; |
| 97 | +} |
| 98 | + |
| 99 | +const std::shared_ptr<PartitionSpec>& BaseTable::spec() const { |
| 100 | + std::call_once(init_partition_spec_once_, [this]() { InitPartitionSpec(); }); |
| 101 | + return partition_spec_; |
| 102 | +} |
| 103 | + |
| 104 | +const std::unordered_map<int32_t, std::shared_ptr<PartitionSpec>>& BaseTable::specs() |
| 105 | + const { |
| 106 | + std::call_once(init_partition_spec_once_, [this]() { InitPartitionSpec(); }); |
| 107 | + return partition_spec_map_; |
| 108 | +} |
| 109 | + |
| 110 | +const std::shared_ptr<SortOrder>& BaseTable::sort_order() const { |
| 111 | + std::call_once(init_sort_order_once_, [this]() { InitSortOrder(); }); |
| 112 | + return sort_order_; |
| 113 | +} |
| 114 | + |
| 115 | +const std::unordered_map<int32_t, std::shared_ptr<SortOrder>>& BaseTable::sort_orders() |
| 116 | + const { |
| 117 | + std::call_once(init_sort_order_once_, [this]() { InitSortOrder(); }); |
| 118 | + return sort_orders_map_; |
| 119 | +} |
| 120 | + |
| 121 | +const std::unordered_map<std::string, std::string>& BaseTable::properties() const { |
| 122 | + return metadata_->properties; |
| 123 | +} |
| 124 | + |
| 125 | +const std::string& BaseTable::location() const { return metadata_->location; } |
| 126 | + |
| 127 | +const std::shared_ptr<Snapshot>& BaseTable::current_snapshot() const { |
| 128 | + std::call_once(init_snapshot_once_, [this]() { InitSnapshot(); }); |
| 129 | + if (!current_snapshot_) { |
| 130 | + throw IcebergError("Current snapshot is not defined for this table"); |
| 131 | + } |
| 132 | + return current_snapshot_; |
| 133 | +} |
| 134 | + |
| 135 | +Result<std::shared_ptr<Snapshot>> BaseTable::snapshot(int64_t snapshot_id) const { |
| 136 | + std::call_once(init_snapshot_once_, [this]() { InitSnapshot(); }); |
| 137 | + auto iter = snapshots_map_.find(snapshot_id); |
| 138 | + if (iter == snapshots_map_.end()) { |
| 139 | + return NotFound("Snapshot with ID {} not found", snapshot_id); |
| 140 | + } |
| 141 | + return iter->second; |
| 142 | +} |
| 143 | + |
| 144 | +const std::vector<std::shared_ptr<Snapshot>>& BaseTable::snapshots() const { |
| 145 | + return metadata_->snapshots; |
| 146 | +} |
| 147 | + |
| 148 | +const std::vector<std::shared_ptr<HistoryEntry>>& BaseTable::history() const { |
| 149 | + // TODO: Implement history retrieval |
| 150 | + throw IcebergError("history is not supported for BaseTable now"); |
| 151 | +} |
| 152 | + |
| 153 | +Status StaticTable::Refresh() { |
| 154 | + throw IcebergError("Refresh is not supported for StaticTable"); |
| 155 | +} |
| 156 | + |
| 157 | +std::unique_ptr<TableScan> StaticTable::NewScan() const { |
| 158 | + throw IcebergError("NewScan is not supported for StaticTable"); |
| 159 | +} |
| 160 | + |
| 161 | +std::shared_ptr<AppendFiles> StaticTable::NewAppend() { |
| 162 | + throw IcebergError("NewAppend is not supported for StaticTable"); |
| 163 | +} |
| 164 | + |
| 165 | +std::unique_ptr<Transaction> StaticTable::NewTransaction() { |
| 166 | + throw IcebergError("NewTransaction is not supported for StaticTable"); |
| 167 | +} |
| 168 | + |
| 169 | +std::unique_ptr<LocationProvider> StaticTable::location_provider() const { |
| 170 | + throw IcebergError("location_provider is not supported for StaticTable"); |
| 171 | +} |
| 172 | + |
| 173 | +} // namespace iceberg |
0 commit comments