|
| 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/data/puffin_dv_register.h" |
| 21 | + |
| 22 | +#include <optional> |
| 23 | +#include <string_view> |
| 24 | +#include <utility> |
| 25 | + |
| 26 | +#include "iceberg/data/deletion_vector_writer.h" |
| 27 | +#include "iceberg/data/dv_util_internal.h" |
| 28 | +#include "iceberg/deletes/position_delete_index.h" |
| 29 | +#include "iceberg/manifest/manifest_entry.h" |
| 30 | +#include "iceberg/util/macros.h" |
| 31 | + |
| 32 | +namespace iceberg { |
| 33 | +namespace { |
| 34 | + |
| 35 | +class DataPuffinDVIO final : public PuffinDVIO { |
| 36 | + public: |
| 37 | + Result<std::vector<std::shared_ptr<DataFile>>> MergeAndWriteDVs( |
| 38 | + std::span<const DeletionVectorMergeGroup> groups, std::string_view output_path, |
| 39 | + const std::shared_ptr<FileIO>& io) override { |
| 40 | + if (groups.empty()) { |
| 41 | + return std::vector<std::shared_ptr<DataFile>>{}; |
| 42 | + } |
| 43 | + |
| 44 | + ICEBERG_ASSIGN_OR_RAISE( |
| 45 | + auto writer, |
| 46 | + DeletionVectorWriter::Make(DeletionVectorWriterOptions{ |
| 47 | + .path = std::string(output_path), |
| 48 | + .io = io, |
| 49 | + .load_previous_deletes = [](std::string_view) |
| 50 | + -> Result<std::optional<PositionDeleteIndex>> { return std::nullopt; }})); |
| 51 | + |
| 52 | + for (const auto& group : groups) { |
| 53 | + PositionDeleteIndex merged; |
| 54 | + for (const auto& delete_file : group.delete_files) { |
| 55 | + ICEBERG_ASSIGN_OR_RAISE(auto index, DVUtil::ReadDV(delete_file, io)); |
| 56 | + merged.Merge(index); |
| 57 | + } |
| 58 | + ICEBERG_RETURN_UNEXPECTED(writer->Delete(group.referenced_data_file, merged, |
| 59 | + group.spec, group.partition)); |
| 60 | + } |
| 61 | + |
| 62 | + ICEBERG_RETURN_UNEXPECTED(writer->Close()); |
| 63 | + ICEBERG_ASSIGN_OR_RAISE(auto metadata, writer->Metadata()); |
| 64 | + return std::move(metadata.data_files); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +} // namespace |
| 69 | + |
| 70 | +std::shared_ptr<PuffinDVIO> MakePuffinDVIO() { |
| 71 | + return std::make_shared<DataPuffinDVIO>(); |
| 72 | +} |
| 73 | + |
| 74 | +void RegisterPuffinDVIO() { |
| 75 | + static PuffinDVIORegistry registry( |
| 76 | + []() -> Result<std::shared_ptr<PuffinDVIO>> { return MakePuffinDVIO(); }); |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace iceberg |
0 commit comments