Finds the last valid commit in the segment using the offset index. It traverses the index in reverse order, starting from the last key. Returns `Ok((Metadata)` - If a valid commit is found containing the commit, It adds a default header, which should be replaced with the actual header. `Err` - If no valid commit is found or if the index is empty
(
min_tx_offset: u64,
header: Header,
reader: &mut R,
offset_index: &TxOffsetIndex,
)
| 690 | /// header, which should be replaced with the actual header. |
| 691 | /// * `Err` - If no valid commit is found or if the index is empty |
| 692 | fn find_valid_indexed_commit<R: io::Read + io::Seek>( |
| 693 | min_tx_offset: u64, |
| 694 | header: Header, |
| 695 | reader: &mut R, |
| 696 | offset_index: &TxOffsetIndex, |
| 697 | ) -> io::Result<Metadata> { |
| 698 | let mut candidate_last_key = TxOffset::MAX; |
| 699 | |
| 700 | while let Ok((key, byte_offset)) = offset_index.key_lookup(candidate_last_key) { |
| 701 | match Self::validate_commit_at_offset(reader, key, byte_offset) { |
| 702 | Ok(commit) => { |
| 703 | return Ok(Metadata { |
| 704 | header, |
| 705 | tx_range: Range { |
| 706 | start: min_tx_offset, |
| 707 | end: commit.tx_range.end, |
| 708 | }, |
| 709 | size_in_bytes: byte_offset + commit.size_in_bytes, |
| 710 | max_epoch: commit.epoch, |
| 711 | max_commit_offset: commit.tx_range.start, |
| 712 | max_commit: Some(commit), |
| 713 | }); |
| 714 | } |
| 715 | |
| 716 | // `TxOffset` at `byte_offset` is not valid, so try with previous entry |
| 717 | Err(_) => { |
| 718 | candidate_last_key = key.saturating_sub(1); |
| 719 | if candidate_last_key == 0 { |
| 720 | break; |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | Err(io::Error::new( |
| 727 | ErrorKind::InvalidData, |
| 728 | format!("No valid commit found in index up to key: {candidate_last_key}"), |
| 729 | )) |
| 730 | } |
| 731 | |
| 732 | /// Validates and decodes a commit at `byte_offset` in the segment. |
| 733 | /// |
nothing calls this directly
no test coverage detected