|
| 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 <memory> |
| 21 | +#include <optional> |
| 22 | + |
| 23 | +#include "iceberg/inheritable_metadata.h" |
| 24 | +#include "iceberg/manifest/manifest_entry.h" |
| 25 | +#include "iceberg/manifest/manifest_reader.h" |
| 26 | +#include "iceberg/manifest/manifest_util_internal.h" |
| 27 | +#include "iceberg/manifest/manifest_writer.h" |
| 28 | +#include "iceberg/result.h" |
| 29 | +#include "iceberg/schema.h" |
| 30 | +#include "iceberg/snapshot.h" |
| 31 | +#include "iceberg/util/macros.h" |
| 32 | + |
| 33 | +namespace iceberg { |
| 34 | + |
| 35 | +Result<ManifestFile> CopyAppendManifest( |
| 36 | + const ManifestFile& manifest, const std::shared_ptr<FileIO>& file_io, |
| 37 | + const std::shared_ptr<Schema>& schema, const std::shared_ptr<PartitionSpec>& spec, |
| 38 | + int64_t snapshot_id, const std::string& output_path, int8_t format_version, |
| 39 | + SnapshotSummaryBuilder* summary_builder) { |
| 40 | + // use metadata that will add the current snapshot's ID for the rewrite |
| 41 | + // read first_row_id as null because this copies the incoming manifest before commit |
| 42 | + ICEBERG_ASSIGN_OR_RAISE(auto inheritable_metadata, |
| 43 | + InheritableMetadataFactory::ForCopy(snapshot_id)); |
| 44 | + ICEBERG_ASSIGN_OR_RAISE( |
| 45 | + auto reader, |
| 46 | + ManifestReader::Make(manifest.manifest_path, manifest.manifest_length, file_io, |
| 47 | + schema, spec, std::move(inheritable_metadata), |
| 48 | + /*first_row_id=*/std::nullopt)); |
| 49 | + ICEBERG_ASSIGN_OR_RAISE(auto entries, reader->Entries()); |
| 50 | + |
| 51 | + // do not produce row IDs for the copy |
| 52 | + ICEBERG_ASSIGN_OR_RAISE( |
| 53 | + auto writer, ManifestWriter::MakeWriter( |
| 54 | + format_version, snapshot_id, output_path, file_io, spec, schema, |
| 55 | + ManifestContent::kData, /*first_row_id=*/std::nullopt)); |
| 56 | + |
| 57 | + for (auto& entry : entries) { |
| 58 | + ICEBERG_CHECK(entry.status == ManifestStatus::kAdded, |
| 59 | + "Manifest to copy must only contain added entries"); |
| 60 | + if (summary_builder != nullptr && entry.data_file != nullptr) { |
| 61 | + ICEBERG_RETURN_UNEXPECTED(summary_builder->AddedFile(*spec, *entry.data_file)); |
| 62 | + } |
| 63 | + |
| 64 | + ICEBERG_RETURN_UNEXPECTED(writer->WriteAddedEntry(entry)); |
| 65 | + } |
| 66 | + |
| 67 | + ICEBERG_RETURN_UNEXPECTED(writer->Close()); |
| 68 | + return writer->ToManifestFile(); |
| 69 | +} |
| 70 | + |
| 71 | +} // namespace iceberg |
0 commit comments