niyue commented on a change in pull request #11486: URL: https://github.com/apache/arrow/pull/11486#discussion_r740216701
########## File path: cpp/src/arrow/ipc/reader.cc ########## @@ -1358,6 +1398,62 @@ Future<std::shared_ptr<RecordBatchFileReader>> RecordBatchFileReader::OpenAsync( .Then([=]() -> Result<std::shared_ptr<RecordBatchFileReader>> { return result; }); } +Result<int64_t> IoRecordedRandomAccessFile::GetSize() { return file_size_; } + +Result<int64_t> IoRecordedRandomAccessFile::ReadAt(int64_t position, int64_t nbytes, + void* out) { + auto num_bytes_read = std::min(file_size_, position + nbytes) - position; + + if (!recorded_reads_.empty() && + position == recorded_reads_.back().offset + recorded_reads_.back().length) { + // merge continuous IOs into one if possible + recorded_reads_.back().length += num_bytes_read; + } else { + // no real IO is performed, it is only saved into a vector for replaying later + recorded_reads_.emplace_back(io::ReadRange{position, num_bytes_read}); + } + return num_bytes_read; +} + +Result<std::shared_ptr<Buffer>> IoRecordedRandomAccessFile::ReadAt(int64_t position, + int64_t nbytes) { + std::shared_ptr<Buffer> out; + auto result = ReadAt(position, nbytes, &out); + return out; +} + +Status IoRecordedRandomAccessFile::Close() { + closed_ = true; + return Status::OK(); +} + +Status IoRecordedRandomAccessFile::Abort() { return Status::OK(); } + +Result<int64_t> IoRecordedRandomAccessFile::Tell() const { return position_; } + +bool IoRecordedRandomAccessFile::closed() const { return closed_; } + +Status IoRecordedRandomAccessFile::Seek(int64_t position) { + position_ = position; + return Status::OK(); +} + +Result<int64_t> IoRecordedRandomAccessFile::Read(int64_t nbytes, void* out) { + return ReadAt(position_, nbytes, out); +} + +Result<std::shared_ptr<Buffer>> IoRecordedRandomAccessFile::Read(int64_t nbytes) { + return ReadAt(position_, nbytes); +} + +const io::IOContext& IoRecordedRandomAccessFile::io_context() const { + return io_context_; +} + +const std::vector<io::ReadRange>& IoRecordedRandomAccessFile::GetRecordedReads() const { Review comment: I find arrow has a class `io::ReadRange` with offset/length in it, and its semantics fit this API very well, so I re-use this class in the API instead of `std::pair<int64_t, int64_t>` -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org