|
| 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/deletion_vector_writer.h" |
| 21 | + |
| 22 | +#include <format> |
| 23 | +#include <map> |
| 24 | +#include <optional> |
| 25 | +#include <string> |
| 26 | +#include <utility> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +#include "iceberg/deletes/roaring_position_bitmap.h" |
| 30 | +#include "iceberg/file_io.h" |
| 31 | +#include "iceberg/manifest/manifest_entry.h" |
| 32 | +#include "iceberg/partition_spec.h" |
| 33 | +#include "iceberg/puffin/deletion_vector.h" |
| 34 | +#include "iceberg/puffin/file_metadata.h" |
| 35 | +#include "iceberg/puffin/puffin_writer.h" |
| 36 | +#include "iceberg/util/macros.h" |
| 37 | +#include "iceberg/version.h" |
| 38 | + |
| 39 | +namespace iceberg { |
| 40 | + |
| 41 | +class DeletionVectorWriter::Impl { |
| 42 | + public: |
| 43 | + explicit Impl(DeletionVectorWriterOptions options) : options_(std::move(options)) {} |
| 44 | + |
| 45 | + Status Delete(std::string_view referenced_data_file, int64_t pos) { |
| 46 | + ICEBERG_CHECK(!closed_, "Cannot delete after the writer is closed"); |
| 47 | + ICEBERG_PRECHECK(!referenced_data_file.empty(), |
| 48 | + "Deletion vector requires a non-empty referenced data file"); |
| 49 | + ICEBERG_PRECHECK(pos >= 0 && pos <= RoaringPositionBitmap::kMaxPosition, |
| 50 | + "Invalid deletion vector position: {}", pos); |
| 51 | + bitmaps_[std::string(referenced_data_file)].Add(pos); |
| 52 | + return {}; |
| 53 | + } |
| 54 | + |
| 55 | + Status Close() { |
| 56 | + if (closed_) { |
| 57 | + return {}; |
| 58 | + } |
| 59 | + |
| 60 | + // No deletes: skip creating an orphan Puffin file that no metadata |
| 61 | + // references. |
| 62 | + if (bitmaps_.empty()) { |
| 63 | + closed_ = true; |
| 64 | + return {}; |
| 65 | + } |
| 66 | + |
| 67 | + auto properties = options_.properties; |
| 68 | + properties.try_emplace(std::string(puffin::StandardPuffinProperties::kCreatedBy), |
| 69 | + std::format("iceberg-cpp/{}.{}.{}", ICEBERG_VERSION_MAJOR, |
| 70 | + ICEBERG_VERSION_MINOR, ICEBERG_VERSION_PATCH)); |
| 71 | + |
| 72 | + ICEBERG_ASSIGN_OR_RAISE(auto output_file, options_.io->NewOutputFile(options_.path)); |
| 73 | + ICEBERG_ASSIGN_OR_RAISE( |
| 74 | + auto writer, |
| 75 | + puffin::PuffinWriter::Make(std::move(output_file), std::move(properties))); |
| 76 | + |
| 77 | + // One blob per referenced data file, in deterministic (sorted) order. |
| 78 | + struct Entry { |
| 79 | + std::string referenced_data_file; |
| 80 | + int64_t offset; |
| 81 | + int64_t length; |
| 82 | + int64_t cardinality; |
| 83 | + }; |
| 84 | + std::vector<Entry> entries; |
| 85 | + entries.reserve(bitmaps_.size()); |
| 86 | + for (auto& [referenced_data_file, bitmap] : bitmaps_) { |
| 87 | + bitmap.Optimize(); // run-length encode before serializing |
| 88 | + ICEBERG_ASSIGN_OR_RAISE( |
| 89 | + auto blob, puffin::MakeDeletionVectorBlob(bitmap, referenced_data_file)); |
| 90 | + ICEBERG_ASSIGN_OR_RAISE(auto blob_metadata, writer->Write(blob)); |
| 91 | + entries.push_back(Entry{ |
| 92 | + .referenced_data_file = referenced_data_file, |
| 93 | + .offset = blob_metadata.offset, |
| 94 | + .length = blob_metadata.length, |
| 95 | + .cardinality = static_cast<int64_t>(bitmap.Cardinality()), |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + ICEBERG_RETURN_UNEXPECTED(writer->Finish()); |
| 100 | + ICEBERG_ASSIGN_OR_RAISE(file_size_, writer->FileSize()); |
| 101 | + |
| 102 | + const auto spec_id = |
| 103 | + options_.spec ? std::make_optional(options_.spec->spec_id()) : std::nullopt; |
| 104 | + |
| 105 | + for (auto& entry : entries) { |
| 106 | + data_files_.push_back(std::make_shared<DataFile>(DataFile{ |
| 107 | + .content = DataFile::Content::kPositionDeletes, |
| 108 | + .file_path = options_.path, |
| 109 | + .file_format = FileFormatType::kPuffin, |
| 110 | + .partition = options_.partition, |
| 111 | + .record_count = entry.cardinality, |
| 112 | + .file_size_in_bytes = file_size_, |
| 113 | + .referenced_data_file = std::move(entry.referenced_data_file), |
| 114 | + .content_offset = entry.offset, |
| 115 | + .content_size_in_bytes = entry.length, |
| 116 | + .partition_spec_id = spec_id, |
| 117 | + })); |
| 118 | + } |
| 119 | + |
| 120 | + closed_ = true; |
| 121 | + return {}; |
| 122 | + } |
| 123 | + |
| 124 | + Result<FileWriter::WriteResult> Metadata() { |
| 125 | + ICEBERG_CHECK(closed_, "Cannot get metadata before closing the writer"); |
| 126 | + FileWriter::WriteResult result; |
| 127 | + result.data_files = data_files_; |
| 128 | + return result; |
| 129 | + } |
| 130 | + |
| 131 | + private: |
| 132 | + DeletionVectorWriterOptions options_; |
| 133 | + // Ordered by referenced data file path for deterministic blob layout. |
| 134 | + std::map<std::string, RoaringPositionBitmap> bitmaps_; |
| 135 | + std::vector<std::shared_ptr<DataFile>> data_files_; |
| 136 | + int64_t file_size_ = -1; |
| 137 | + bool closed_ = false; |
| 138 | +}; |
| 139 | + |
| 140 | +DeletionVectorWriter::DeletionVectorWriter(std::unique_ptr<Impl> impl) |
| 141 | + : impl_(std::move(impl)) {} |
| 142 | + |
| 143 | +DeletionVectorWriter::~DeletionVectorWriter() = default; |
| 144 | + |
| 145 | +Result<std::unique_ptr<DeletionVectorWriter>> DeletionVectorWriter::Make( |
| 146 | + DeletionVectorWriterOptions options) { |
| 147 | + ICEBERG_PRECHECK(options.io != nullptr, "DeletionVectorWriter requires a FileIO"); |
| 148 | + ICEBERG_PRECHECK(!options.path.empty(), "DeletionVectorWriter requires a path"); |
| 149 | + return std::unique_ptr<DeletionVectorWriter>( |
| 150 | + new DeletionVectorWriter(std::make_unique<Impl>(std::move(options)))); |
| 151 | +} |
| 152 | + |
| 153 | +Status DeletionVectorWriter::Delete(std::string_view referenced_data_file, int64_t pos) { |
| 154 | + return impl_->Delete(referenced_data_file, pos); |
| 155 | +} |
| 156 | + |
| 157 | +Status DeletionVectorWriter::Close() { return impl_->Close(); } |
| 158 | + |
| 159 | +Result<FileWriter::WriteResult> DeletionVectorWriter::Metadata() { |
| 160 | + return impl_->Metadata(); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace iceberg |
0 commit comments