Advances the `segment` reader to the position corresponding to the `start_tx_offset` using the `index_file` for efficient seeking. Input: - `segment` - segment reader - `min_tx_offset` - minimum transaction offset in the segment - `start_tx_offset` - transaction offset to advance to Returns the byte position `segment` is at after seeking.
(
mut segment: &mut R,
index_file: &TxOffsetIndex,
start_tx_offset: u64,
)
| 483 | /// |
| 484 | /// Returns the byte position `segment` is at after seeking. |
| 485 | pub fn seek_to_offset<R: io::Read + io::Seek>( |
| 486 | mut segment: &mut R, |
| 487 | index_file: &TxOffsetIndex, |
| 488 | start_tx_offset: u64, |
| 489 | ) -> Result<u64, IndexError> { |
| 490 | let (index_key, byte_offset) = index_file.key_lookup(start_tx_offset)?; |
| 491 | |
| 492 | // If the index_key is 0, it means the index file is empty, return error without seeking |
| 493 | if index_key == 0 { |
| 494 | return Err(IndexError::KeyNotFound); |
| 495 | } |
| 496 | debug!("index lookup for key={start_tx_offset}: found key={index_key} at byte-offset={byte_offset}"); |
| 497 | // returned `index_key` should never be greater than `start_tx_offset` |
| 498 | debug_assert!(index_key <= start_tx_offset); |
| 499 | |
| 500 | // Check if the offset index is pointing to the right commit. |
| 501 | let hdr = validate_commit_header(&mut segment, byte_offset)?; |
| 502 | if hdr.min_tx_offset == index_key { |
| 503 | // Advance the segment Seek if expected commit is found. |
| 504 | segment.seek(SeekFrom::Start(byte_offset)) |
| 505 | } else { |
| 506 | Err(io::Error::new( |
| 507 | io::ErrorKind::InvalidData, |
| 508 | "mismatched key in offset index file", |
| 509 | )) |
| 510 | } |
| 511 | .map_err(Into::into) |
| 512 | } |
| 513 | |
| 514 | /// Try to extract the commit header from the asked position without advancing seek. |
| 515 | /// `IndexFileMut` fsync asynchoronously, which makes it important for reader to verify its entry |
no test coverage detected
searching dependent graphs…