Skip to content

Commit

Permalink
Helper to get last element from posting list
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildKite committed Jan 2, 2025
1 parent cb0e042 commit 9963c99
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions rs/index/src/posting_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ impl<'a> PostingList<'a> {
let mut elem_count = 0;
let elem_size_in_bytes = size_of::<u64>();
for slice in &slices {
if slice.is_empty() {
return Err(anyhow!("Posting list slice should not be empty"));
}
if slice.len() % elem_size_in_bytes != 0 {
return Err(anyhow!(
"Invalid slice length {}: should be multiple of u64's size in bytes",
Expand All @@ -53,6 +56,23 @@ impl<'a> PostingList<'a> {
current_index: 0,
}
}

pub fn last(&self) -> Option<u64> {
if self.slices.is_empty() {
return None;
}

// Get the last slice (guaranteed to exist and be non-empty at this point)
let last_slice = self.slices.last().unwrap();

// Calculate the index of the last u64 in the slice
let last_index = last_slice.len() - size_of::<u64>();

// Extract and return the last u64
Some(u64::from_le_bytes(
last_slice[last_index..].try_into().unwrap(),
))
}
}

impl<'a> Iterator for PostingListIterator<'a> {
Expand Down

0 comments on commit 9963c99

Please sign in to comment.