zhjwpku commented on code in PR #30: URL: https://github.com/apache/iceberg-cpp/pull/30#discussion_r2011195005
########## src/iceberg/io/fs_file_reader.cc: ########## @@ -0,0 +1,80 @@ +/* + * 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/io/fs_file_reader.h" + +#include <format> + +#include "iceberg/exception.h" + +namespace iceberg::io { + +FsFileReader::FsFileReader(std::string file_path) : file_path_(std::move(file_path)) { + // Open the file in binary mode + input_file_.open(file_path_, std::ios::binary | std::ios::in); + if (!input_file_.is_open()) { + throw IcebergError(std::format("Failed to open file: {}", file_path_)); + } + + // Calculate the file size + input_file_.seekg(0, std::ios::end); + file_size_ = input_file_.tellg(); + input_file_.seekg(0, std::ios::beg); + + if (file_size_ < 0) { + throw IcebergError(std::format("Failed to determine file size: {}", file_path_)); + } +} + +FsFileReader::~FsFileReader() { + if (input_file_.is_open()) { + input_file_.close(); + } +} + +int64_t FsFileReader::read(ReadRange range, void* buffer) { + if (!input_file_.is_open()) { + throw IcebergError("File is not open for reading"); + } + + if (range.offset < 0 || range.offset + range.length > file_size_) { + throw IcebergError(std::format("Invalid read range: [{}, {})", range.offset, + range.offset + range.length)); + } + + // Seek to the starting position + input_file_.seekg(range.offset, std::ios::beg); + if (input_file_.fail()) { + throw IcebergError(std::format("Failed to seek to offset: {}", range.offset)); + } + + // Read the data into the buffer + input_file_.read(static_cast<char*>(buffer), range.length); + auto bytes_read = static_cast<int64_t>(input_file_.gcount()); + + return bytes_read; // Return actual bytes read +} + +std::future<int64_t> FsFileReader::readAsync(ReadRange range, void* buffer) { Review Comment: Agreed, we might also need a `Buffer` struct along with some BufferManager. I will create a issue to discuss this. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org