wgtmac commented on code in PR #624: URL: https://github.com/apache/iceberg-cpp/pull/624#discussion_r3309200887
########## src/iceberg/puffin/puffin_writer.cc: ########## @@ -0,0 +1,176 @@ +/* + * 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. + */ + +#include "iceberg/puffin/puffin_writer.h" + +#include <array> + +#include "iceberg/file_io.h" +#include "iceberg/puffin/json_serde_internal.h" +#include "iceberg/puffin/puffin_format.h" +#include "iceberg/util/endian.h" +#include "iceberg/util/macros.h" + +namespace iceberg::puffin { + +PuffinWriter::PuffinWriter(std::unique_ptr<PositionOutputStream> stream, + std::unordered_map<std::string, std::string> properties, + PuffinCompressionCodec default_codec, bool compress_footer) + : stream_(std::move(stream)), + properties_(std::move(properties)), + default_codec_(default_codec), + compress_footer_(compress_footer) {} + +PuffinWriter::~PuffinWriter() = default; + +Result<std::unique_ptr<PuffinWriter>> PuffinWriter::Make( + std::unique_ptr<OutputFile> output_file, + std::unordered_map<std::string, std::string> properties, + PuffinCompressionCodec default_codec, bool compress_footer) { + if (!output_file) { + return InvalidArgument("output_file must not be null"); + } + ICEBERG_ASSIGN_OR_RAISE(auto stream, output_file->CreateOrOverwrite()); Review Comment: Java PuffinWriter opens the output with OutputFile.create(), which fails when the target already exists. This path uses CreateOrOverwrite(), so a caller can silently truncate an existing Puffin file instead of getting the Java-aligned failure. ########## src/iceberg/puffin/puffin_reader.cc: ########## @@ -0,0 +1,202 @@ +/* + * 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. + */ + +#include "iceberg/puffin/puffin_reader.h" + +#include <algorithm> +#include <array> +#include <cstdint> +#include <cstring> +#include <string_view> + +#include "iceberg/file_io.h" +#include "iceberg/puffin/json_serde_internal.h" +#include "iceberg/puffin/puffin_format.h" +#include "iceberg/util/endian.h" +#include "iceberg/util/macros.h" + +namespace iceberg::puffin { + +namespace { + +// Validate magic bytes in a buffer at offset 0. +Status CheckMagic(std::span<const std::byte> data) { + if (static_cast<int64_t>(data.size()) < PuffinFormat::kMagicLength) { + return Invalid("Invalid file: buffer too small for magic"); + } + auto* begin = reinterpret_cast<const uint8_t*>(data.data()); + if (!std::equal(PuffinFormat::kMagicV1.begin(), PuffinFormat::kMagicV1.end(), begin)) { + return Invalid( + "Invalid file: expected magic, got [{:#04x}, {:#04x}, {:#04x}, {:#04x}]", + begin[0], begin[1], begin[2], begin[3]); + } + return {}; +} + +// Validate that no unknown flag bits are set. +Status CheckUnknownFlags(std::span<const uint8_t, 4> flags) { + constexpr uint8_t kKnownBitsMask = 0x01; + if ((flags[0] & ~kKnownBitsMask) != 0 || flags[1] != 0 || flags[2] != 0 || + flags[3] != 0) { + return Invalid( + "Invalid file: unknown footer flags set [{:#04x}, {:#04x}, {:#04x}, {:#04x}]", + flags[0], flags[1], flags[2], flags[3]); + } + return {}; +} + +} // namespace + +PuffinReader::PuffinReader(std::unique_ptr<SeekableInputStream> stream, int64_t file_size, + std::optional<int64_t> known_footer_size) + : stream_(std::move(stream)), + file_size_(file_size), + known_footer_size_(known_footer_size) {} + +PuffinReader::~PuffinReader() = default; + +Result<std::unique_ptr<PuffinReader>> PuffinReader::Make( + std::unique_ptr<InputFile> input_file, std::optional<int64_t> footer_size) { + if (!input_file) { + return InvalidArgument("input_file must not be null"); + } + ICEBERG_ASSIGN_OR_RAISE(auto file_size, input_file->Size()); Review Comment: The reader accepts a known footer_size, but Make still always calls InputFile::Size() and ReadFileMetadata recomputes the footer size from the file tail. Java's ReadBuilder supports both withFileSize and withFooterSize and uses/validates the hints, so this API is not strictly aligned and loses the intended object-store optimization. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
