|
| 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/file_writer.h |
| 23 | +/// Writer interface for file formats like Parquet, Avro and ORC. |
| 24 | + |
| 25 | +#include <functional> |
| 26 | +#include <memory> |
| 27 | +#include <optional> |
| 28 | + |
| 29 | +#include "iceberg/arrow_c_data.h" |
| 30 | +#include "iceberg/file_format.h" |
| 31 | +#include "iceberg/result.h" |
| 32 | +#include "iceberg/type_fwd.h" |
| 33 | + |
| 34 | +namespace iceberg { |
| 35 | + |
| 36 | +/// \brief Options for creating a writer. |
| 37 | +struct ICEBERG_EXPORT WriterOptions { |
| 38 | + /// \brief The path to the file to write. |
| 39 | + std::string path; |
| 40 | + /// \brief The schema of the data to write. |
| 41 | + ArrowSchema schema; |
| 42 | + /// \brief FileIO instance to open the file. Writer implementations should down cast it |
| 43 | + /// to the specific FileIO implementation. By default, the `iceberg-bundle` library uses |
| 44 | + /// `ArrowFileSystemFileIO` as the default implementation. |
| 45 | + std::shared_ptr<class FileIO> io; |
| 46 | + /// \brief Format-specific or implementation-specific properties. |
| 47 | + std::unordered_map<std::string, std::string> properties; |
| 48 | +}; |
| 49 | + |
| 50 | +/// \brief Base writer class to write data from different file formats. |
| 51 | +class ICEBERG_EXPORT Writer { |
| 52 | + public: |
| 53 | + virtual ~Writer() = default; |
| 54 | + Writer() = default; |
| 55 | + Writer(const Writer&) = delete; |
| 56 | + Writer& operator=(const Writer&) = delete; |
| 57 | + |
| 58 | + /// \brief Open the writer. |
| 59 | + virtual Status Open(const struct WriterOptions& options) = 0; |
| 60 | + |
| 61 | + /// \brief Close the writer. |
| 62 | + virtual Status Close() = 0; |
| 63 | + |
| 64 | + /// \brief Read next data from file. |
| 65 | + /// |
| 66 | + /// \return Status of write results. |
| 67 | + virtual Status Write(const ArrowArray& data) = 0; |
| 68 | +}; |
| 69 | + |
| 70 | +/// \brief Factory function to create a writer of a specific file format. |
| 71 | +using WriterFactory = std::function<Result<std::unique_ptr<Writer>>()>; |
| 72 | + |
| 73 | +/// \brief Registry of writer factories for different file formats. |
| 74 | +struct ICEBERG_EXPORT WriterFactoryRegistry { |
| 75 | + /// \brief Register a factory function for a specific file format. |
| 76 | + WriterFactoryRegistry(FileFormatType format_type, WriterFactory factory); |
| 77 | + |
| 78 | + /// \brief Get the factory function for a specific file format. |
| 79 | + static WriterFactory& GetFactory(FileFormatType format_type); |
| 80 | + |
| 81 | + /// \brief Open a writer for a specific file format. |
| 82 | + static Result<std::unique_ptr<Writer>> Open(FileFormatType format_type, |
| 83 | + const WriterOptions& options); |
| 84 | +}; |
| 85 | + |
| 86 | +} // namespace iceberg |
0 commit comments