|
| 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 | +#pragma once |
| 21 | + |
| 22 | +/// \file iceberg/manifest/rolling_manifest_writer.h |
| 23 | +/// Rolling manifest writer that can produce multiple manifest files. |
| 24 | + |
| 25 | +#include <functional> |
| 26 | +#include <memory> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +#include "iceberg/iceberg_export.h" |
| 30 | +#include "iceberg/manifest/manifest_entry.h" |
| 31 | +#include "iceberg/manifest/manifest_list.h" |
| 32 | +#include "iceberg/manifest/manifest_writer.h" |
| 33 | +#include "iceberg/result.h" |
| 34 | + |
| 35 | +namespace iceberg { |
| 36 | + |
| 37 | +/// \brief A rolling manifest writer that can produce multiple manifest files. |
| 38 | +class ICEBERG_EXPORT RollingManifestWriter { |
| 39 | + public: |
| 40 | + /// \brief Factory function type for creating ManifestWriter instances. |
| 41 | + using ManifestWriterFactory = std::function<Result<std::unique_ptr<ManifestWriter>>()>; |
| 42 | + |
| 43 | + /// \brief Construct a rolling manifest writer. |
| 44 | + /// \param manifest_writer_factory Factory function to create new ManifestWriter |
| 45 | + /// instances. |
| 46 | + /// \param target_file_size_in_bytes Target file size in bytes. When the current |
| 47 | + /// file reaches this size (and row count is a multiple of 250), a new file |
| 48 | + /// will be created. |
| 49 | + RollingManifestWriter(ManifestWriterFactory manifest_writer_factory, |
| 50 | + int64_t target_file_size_in_bytes); |
| 51 | + |
| 52 | + ~RollingManifestWriter(); |
| 53 | + |
| 54 | + /// \brief Add an added entry for a file. |
| 55 | + /// |
| 56 | + /// \param file a data file |
| 57 | + /// \return Status indicating success or failure |
| 58 | + /// \note The entry's snapshot ID will be this manifest's snapshot ID. The |
| 59 | + /// entry's data sequence number will be the provided data sequence number. |
| 60 | + /// The entry's file sequence number will be assigned at commit. |
| 61 | + Status WriteAddedEntry(std::shared_ptr<DataFile> file, |
| 62 | + std::optional<int64_t> data_sequence_number = std::nullopt); |
| 63 | + |
| 64 | + /// \brief Add an existing entry for a file. |
| 65 | + /// |
| 66 | + /// \param file an existing data file |
| 67 | + /// \param file_snapshot_id snapshot ID when the data file was added to the table |
| 68 | + /// \param data_sequence_number a data sequence number of the file (assigned when |
| 69 | + /// the file was added) |
| 70 | + /// \param file_sequence_number a file sequence number (assigned when the file |
| 71 | + /// was added) |
| 72 | + /// \return Status indicating success or failure |
| 73 | + /// \note The original data and file sequence numbers, snapshot ID, which were |
| 74 | + /// assigned at commit, must be preserved when adding an existing entry. |
| 75 | + Status WriteExistingEntry(std::shared_ptr<DataFile> file, int64_t file_snapshot_id, |
| 76 | + int64_t data_sequence_number, |
| 77 | + std::optional<int64_t> file_sequence_number = std::nullopt); |
| 78 | + |
| 79 | + /// \brief Add a delete entry for a file. |
| 80 | + /// |
| 81 | + /// \param file a deleted data file |
| 82 | + /// \param data_sequence_number a data sequence number of the file (assigned when |
| 83 | + /// the file was added) |
| 84 | + /// \param file_sequence_number a file sequence number (assigned when the file |
| 85 | + /// was added) |
| 86 | + /// \return Status indicating success or failure |
| 87 | + /// \note The entry's snapshot ID will be this manifest's snapshot ID. However, |
| 88 | + /// the original data and file sequence numbers of the file must be preserved |
| 89 | + /// when the file is marked as deleted. |
| 90 | + Status WriteDeletedEntry(std::shared_ptr<DataFile> file, int64_t data_sequence_number, |
| 91 | + std::optional<int64_t> file_sequence_number = std::nullopt); |
| 92 | + |
| 93 | + /// \brief Close the rolling manifest writer. |
| 94 | + Status Close(); |
| 95 | + |
| 96 | + /// \brief Get the list of manifest files produced by this writer. |
| 97 | + /// \return A vector of ManifestFile objects |
| 98 | + /// \note Only valid after the writer is closed. |
| 99 | + Result<std::vector<ManifestFile>> ToManifestFiles() const; |
| 100 | + |
| 101 | + private: |
| 102 | + /// \brief Get or create the current writer, rolling to a new file if needed. |
| 103 | + /// \return The current ManifestWriter, or an error if creation fails |
| 104 | + Result<ManifestWriter*> CurrentWriter(); |
| 105 | + |
| 106 | + /// \brief Check if we should roll to a new file. |
| 107 | + /// |
| 108 | + /// This method checks if the current file has reached the target size |
| 109 | + /// or the number of rows has reached the threshold. If so, it rolls to a new file. |
| 110 | + bool ShouldRollToNewFile() const; |
| 111 | + |
| 112 | + /// \brief Close the current writer and add its ManifestFile to the list. |
| 113 | + Status CloseCurrentWriter(); |
| 114 | + |
| 115 | + /// \brief The number of rows after which to consider rolling to a new file. |
| 116 | + /// \note This aligned with Iceberg's Java impl. |
| 117 | + static constexpr int64_t kRowsDivisor = 250; |
| 118 | + |
| 119 | + ManifestWriterFactory manifest_writer_factory_; |
| 120 | + int64_t target_file_size_in_bytes_; |
| 121 | + std::vector<ManifestFile> manifest_files_; |
| 122 | + |
| 123 | + int64_t current_file_rows_{0}; |
| 124 | + std::unique_ptr<ManifestWriter> current_writer_{nullptr}; |
| 125 | + bool closed_{false}; |
| 126 | +}; |
| 127 | + |
| 128 | +} // namespace iceberg |
0 commit comments