| 3001 | Result<int64_t> IoRecordedRandomAccessFile::GetSize() { return file_size_; } |
| 3002 | |
| 3003 | Result<int64_t> IoRecordedRandomAccessFile::ReadAt(int64_t position, int64_t nbytes, |
| 3004 | bool allow_short_read, void* out) { |
| 3005 | auto num_bytes_read = std::min(file_size_, position + nbytes) - position; |
| 3006 | if (!allow_short_read && num_bytes_read != nbytes) { |
| 3007 | return Status::IOError("File too short: expected to be able to read ", nbytes, |
| 3008 | " bytes, got ", num_bytes_read); |
| 3009 | } |
| 3010 | |
| 3011 | if (!read_ranges_.empty() && |
| 3012 | position == read_ranges_.back().offset + read_ranges_.back().length) { |
| 3013 | // merge continuous IOs into one if possible |
| 3014 | read_ranges_.back().length += num_bytes_read; |
| 3015 | } else { |
| 3016 | // no real IO is performed, it is only saved into a vector for replaying later |
| 3017 | read_ranges_.emplace_back(io::ReadRange{position, num_bytes_read}); |
| 3018 | } |
| 3019 | return num_bytes_read; |
| 3020 | } |
| 3021 | |
| 3022 | Result<std::shared_ptr<Buffer>> IoRecordedRandomAccessFile::ReadAt( |
| 3023 | int64_t position, int64_t nbytes, bool allow_short_read) { |