Skip to content

feat(puffin): add puffin file reader and writer#624

Open
zhaoxuan1994 wants to merge 1 commit intoapache:mainfrom
zhaoxuan1994:feat/puffin-format-utils
Open

feat(puffin): add puffin file reader and writer#624
zhaoxuan1994 wants to merge 1 commit intoapache:mainfrom
zhaoxuan1994:feat/puffin-format-utils

Conversation

@zhaoxuan1994
Copy link
Copy Markdown
Contributor

  • PuffinWriter: in-memory writer that builds complete Puffin files
    • Add() writes blobs with optional compression
    • Finish() serializes footer with JSON metadata
    • Tracks BlobMetadata for all written blobs
  • PuffinReader: in-memory reader that parses Puffin files
    • ReadFileMetadata() parses footer and validates magic bytes
    • ReadBlob() reads and decompresses individual blobs
    • ReadAll() reads all blobs from metadata
  • Expose Compress/Decompress as public API in puffin_format.h
  • Register new sources in CMake and Meson build systems
  • Add comprehensive tests

- PuffinWriter: in-memory writer that builds complete Puffin files
  - Add() writes blobs with optional compression
  - Finish() serializes footer with JSON metadata
  - Tracks BlobMetadata for all written blobs
- PuffinReader: in-memory reader that parses Puffin files
  - ReadFileMetadata() parses footer and validates magic bytes
  - ReadBlob() reads and decompresses individual blobs
  - ReadAll() reads all blobs from metadata
- Expose Compress/Decompress as public API in puffin_format.h
- Register new sources in CMake and Meson build systems
- Add comprehensive tests including Java binary compatibility
Copilot AI review requested due to automatic review settings April 23, 2026 13:01
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an in-memory Puffin file writer/reader to the Iceberg C++ Puffin module, exposes compression helpers as public API, wires new sources into the build, and introduces round-trip + Java-compatibility tests.

Changes:

  • Introduce PuffinWriter for building Puffin files in-memory (blob writing + footer serialization).
  • Introduce PuffinReader for parsing Puffin files in-memory (footer parsing + blob reading).
  • Export Compress/Decompress APIs and register new sources/tests in CMake + Meson.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/iceberg/puffin/puffin_writer.h Declares the in-memory Puffin writer API.
src/iceberg/puffin/puffin_writer.cc Implements Puffin file serialization (header, blobs, footer).
src/iceberg/puffin/puffin_reader.h Declares the in-memory Puffin reader API.
src/iceberg/puffin/puffin_reader.cc Implements footer parsing, magic validation, and blob reads.
src/iceberg/puffin/puffin_format.h Exposes Compress/Decompress as public APIs.
src/iceberg/puffin/puffin_format.cc Moves flag helpers and keeps compression stubs in the public namespace.
src/iceberg/puffin/meson.build Installs new public headers for Meson builds.
src/iceberg/meson.build Adds new Puffin reader/writer sources to the core library build.
src/iceberg/CMakeLists.txt Adds new Puffin reader/writer sources to the CMake build.
src/iceberg/test/puffin_reader_writer_test.cc Adds comprehensive writer/reader round-trip and Java-compat tests.
src/iceberg/test/meson.build Adds the new Puffin reader/writer test to Meson test target.
src/iceberg/test/CMakeLists.txt Adds the new Puffin reader/writer test to CMake test target.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

/// Parses a Puffin file from an in-memory buffer. Usage:
/// PuffinReader reader(file_data);
/// auto metadata = reader.ReadFileMetadata();
/// auto blob = reader.ReadBlob(metadata->blobs[0]);
Comment on lines +69 to +100
auto payload_size = ReadLittleEndian<int32_t>(
data_.data() + footer_struct_offset + PuffinFormat::kFooterStructPayloadSizeOffset);

// Calculate total footer size and validate
int64_t footer_size = PuffinFormat::kFooterStartMagicLength +
static_cast<int64_t>(payload_size) +
PuffinFormat::kFooterStructLength;
auto footer_offset = file_size - footer_size;
if (footer_offset < 0) {
return Invalid("Invalid file: footer size {} exceeds file size {}", footer_size,
file_size);
}

// Validate footer start magic
ICEBERG_RETURN_UNEXPECTED(CheckMagic(data_, footer_offset));

// Check flags for footer compression
std::array<uint8_t, 4> flags{};
std::memcpy(
flags.data(),
data_.data() + footer_struct_offset + PuffinFormat::kFooterStructFlagsOffset, 4);

PuffinCompressionCodec footer_compression = PuffinCompressionCodec::kNone;
if (IsFlagSet(flags, PuffinFlag::kFooterPayloadCompressed)) {
footer_compression = PuffinFormat::kDefaultFooterCompressionCodec;
}

// Extract footer payload
auto payload_offset = footer_offset + PuffinFormat::kFooterStartMagicLength;
std::span<const std::byte> payload_span(data_.data() + payload_offset, payload_size);
ICEBERG_ASSIGN_OR_RAISE(auto payload_bytes,
Decompress(footer_compression, payload_span));
Comment on lines +117 to +124
if (blob_metadata.offset < 0 ||
blob_metadata.offset + blob_metadata.length > file_size) {
return Invalid("Invalid blob: offset {} + length {} exceeds file size {}",
blob_metadata.offset, blob_metadata.length, file_size);
}

std::span<const std::byte> raw_data(data_.data() + blob_metadata.offset,
blob_metadata.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants