Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/iceberg/file_writer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#pragma once

/// \file iceberg/file_writer.h
/// Writer interface for file formats like Parquet, Avro and ORC.

#include <functional>
#include <memory>
#include <optional>

#include "iceberg/arrow_c_data.h"
#include "iceberg/file_format.h"
#include "iceberg/result.h"
#include "iceberg/type_fwd.h"

namespace iceberg {

/// \brief Options for creating a writer.
struct ICEBERG_EXPORT WriterOptions {
/// \brief The path to the file to write.
std::string path;
/// \brief The schema of the data to write.
ArrowSchema schema;
/// \brief FileIO instance to open the file. Writer implementations should down cast it
/// to the specific FileIO implementation. By default, the `iceberg-bundle` library uses
/// `ArrowFileSystemFileIO` as the default implementation.
std::shared_ptr<class FileIO> io;
/// \brief Format-specific or implementation-specific properties.
std::unordered_map<std::string, std::string> properties;
};

/// \brief Base writer class to write data from different file formats.
class ICEBERG_EXPORT Writer {
public:
virtual ~Writer() = default;
Writer() = default;
Writer(const Writer&) = delete;
Writer& operator=(const Writer&) = delete;

/// \brief Open the writer.
virtual Status Open(const struct WriterOptions& options) = 0;

/// \brief Close the writer.
virtual Status Close() = 0;

/// \brief Write arrow data to the file.
///
/// \return Status of write results.
virtual Status Write(ArrowArray data) = 0;
};

/// \brief Factory function to create a writer of a specific file format.
using WriterFactory = std::function<Result<std::unique_ptr<Writer>>()>;

/// \brief Registry of writer factories for different file formats.
struct ICEBERG_EXPORT WriterFactoryRegistry {
/// \brief Register a factory function for a specific file format.
WriterFactoryRegistry(FileFormatType format_type, WriterFactory factory);

/// \brief Get the factory function for a specific file format.
static WriterFactory& GetFactory(FileFormatType format_type);

/// \brief Open a writer for a specific file format.
static Result<std::unique_ptr<Writer>> Open(FileFormatType format_type,
const WriterOptions& options);
};

} // namespace iceberg
64 changes: 64 additions & 0 deletions src/iceberg/manifest_writer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#pragma once

/// \file iceberg/manifest_writer.h
/// Data writer interface for manifest files.

#include <memory>
#include <vector>

#include "iceberg/file_writer.h"
#include "iceberg/iceberg_export.h"
#include "iceberg/type_fwd.h"

namespace iceberg {

/// \brief Write manifest entries to a manifest file.
class ICEBERG_EXPORT ManifestWriter {
public:
virtual ~ManifestWriter() = default;
virtual Status WriteManifestEntries(
const std::vector<ManifestEntry>& entries) const = 0;

/// \brief Creates a writer for a manifest file.
/// \param manifest_location Path to the manifest file.
/// \param file_io File IO implementation to use.
/// \return A Result containing the writer or an error.
static Result<std::unique_ptr<ManifestWriter>> MakeWriter(
std::string_view manifest_location, std::shared_ptr<FileIO> file_io,
std::shared_ptr<Schema> partition_schema);
};

/// \brief Write manifest files to a manifest list file.
class ICEBERG_EXPORT ManifestListWriter {
public:
virtual ~ManifestListWriter() = default;
virtual Status WriteManifestFiles(const std::vector<ManifestFile>& files) const = 0;

/// \brief Creates a writer for the manifest list.
/// \param manifest_list_location Path to the manifest list file.
/// \param file_io File IO implementation to use.
/// \return A Result containing the writer or an error.
static Result<std::unique_ptr<ManifestListWriter>> MakeWriter(
std::string_view manifest_list_location, std::shared_ptr<FileIO> file_io);
};

} // namespace iceberg
Loading