use byteorder::{LittleEndian, WriteBytesExt};
use std::io::{self, Read, Write};
use super::opt_p4d::{find_optimal_bit_width, pack_with_exceptions, unpack_with_exceptions};
use super::posting_common::{read_vint, write_vint};
use crate::DocId;
use crate::directories::OwnedBytes;
use crate::structures::simd;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PostingCodec {
#[default]
Rounded = 0,
Packed = 1,
Pfor = 2,
}
impl PostingCodec {
const HEADER_SHIFT: u32 = 6;
const WIDTH_MASK: u8 = 0x3F;
fn from_header_byte(doc_bits: u8) -> io::Result<(Self, u8)> {
let width = doc_bits & Self::WIDTH_MASK;
let codec = match doc_bits >> Self::HEADER_SHIFT {
0 => PostingCodec::Rounded,
1 => PostingCodec::Packed,
2 => PostingCodec::Pfor,
other => {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"posting block uses unknown codec id {other}; the index was written by a \
newer Hermes"
),
));
}
};
if width > 32 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("posting block doc-id width {width} exceeds 32 bits"),
));
}
Ok((codec, width))
}
fn header_byte(self, width: u8) -> u8 {
((self as u8) << Self::HEADER_SHIFT) | width
}
pub fn parse(s: &str) -> Option<Self> {
match s.to_ascii_lowercase().as_str() {
"rounded" | "default" => Some(PostingCodec::Rounded),
"packed" | "bp128" | "exact" => Some(PostingCodec::Packed),
"pfor" | "optp4d" | "patched" => Some(PostingCodec::Pfor),
_ => None,
}
}
}
impl std::fmt::Display for PostingCodec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
PostingCodec::Rounded => "rounded",
PostingCodec::Packed => "packed",
PostingCodec::Pfor => "pfor",
})
}
}
#[inline]
fn packed_bytes(count: usize, width: u8) -> usize {
(count * width as usize).div_ceil(8)
}
fn pack_bits(values: &[u32], width: u8, out: &mut Vec<u8>) {
if width == 0 || values.is_empty() {
return;
}
if width == 32 {
for &v in values {
out.extend_from_slice(&v.to_le_bytes());
}
return;
}
let start = out.len();
out.resize(start + packed_bytes(values.len(), width), 0);
let dst = &mut out[start..];
let mut bit_pos = 0usize;
for &v in values {
let mut acc = (v as u64) << (bit_pos & 7);
let mut byte = bit_pos >> 3;
let mut remaining = (bit_pos & 7) + width as usize;
while remaining > 0 {
dst[byte] |= acc as u8;
acc >>= 8;
byte += 1;
remaining = remaining.saturating_sub(8);
}
bit_pos += width as usize;
}
}
fn unpack_bits(input: &[u8], width: u8, out: &mut [u32], count: usize) {
match width {
0 => out[..count].fill(0),
8 => simd::unpack_8bit(input, out, count),
16 => simd::unpack_16bit(input, out, count),
32 => simd::unpack_32bit(input, out, count),
_ => {
let mask = (1u64 << width) - 1;
let mut bit_pos = 0usize;
for slot in out[..count].iter_mut() {
let byte = bit_pos >> 3;
let word = if byte + 8 <= input.len() {
u64::from_le_bytes(input[byte..byte + 8].try_into().unwrap())
} else {
let mut word = 0u64;
for (i, &b) in input[byte..].iter().enumerate() {
word |= (b as u64) << (i * 8);
}
word
};
*slot = ((word >> (bit_pos & 7)) & mask) as u32;
bit_pos += width as usize;
}
}
}
}
fn pack_pfor(values: &[u32], out: &mut Vec<u8>) -> u8 {
let (width, _, _) = find_optimal_bit_width(values);
let (packed, exceptions) = pack_with_exceptions(values, width);
out.push(exceptions.len() as u8);
out.extend_from_slice(&packed);
for (pos, high) in exceptions {
out.push(pos);
out.extend_from_slice(&high.to_le_bytes());
}
width
}
fn pfor_payload_len(input: &[u8], count: usize, width: u8) -> io::Result<usize> {
let n_exceptions = *input
.first()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "posting block truncated"))?
as usize;
Ok(1 + packed_bytes(count, width) + n_exceptions * 5)
}
fn unpack_pfor(input: &[u8], width: u8, out: &mut [u32], count: usize) -> io::Result<()> {
let n_exceptions = *input
.first()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "posting block truncated"))?
as usize;
let packed_len = packed_bytes(count, width);
let table_at = 1 + packed_len;
if input.len() < table_at + n_exceptions * 5 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"posting block exception table truncated",
));
}
let packed = &input[1..table_at];
let mut exceptions: [(u8, u32); 128] = [(0, 0); 128];
for (i, entry) in input[table_at..table_at + n_exceptions * 5]
.chunks_exact(5)
.enumerate()
.take(128)
{
exceptions[i] = (
entry[0],
u32::from_le_bytes([entry[1], entry[2], entry[3], entry[4]]),
);
}
if width == 0 {
out[..count].fill(0);
for &(pos, value) in &exceptions[..n_exceptions.min(128)] {
if (pos as usize) < count {
out[pos as usize] = value;
}
}
return Ok(());
}
unpack_with_exceptions(
packed,
width,
&exceptions[..n_exceptions.min(128)],
count,
out,
);
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Posting {
pub doc_id: DocId,
pub term_freq: u32,
}
#[derive(Debug, Clone, Default)]
pub struct PostingList {
postings: Vec<Posting>,
}
impl PostingList {
pub fn new() -> Self {
Self::default()
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
postings: Vec::with_capacity(capacity),
}
}
pub fn push(&mut self, doc_id: DocId, term_freq: u32) {
debug_assert!(
self.postings.is_empty() || self.postings.last().unwrap().doc_id < doc_id,
"Postings must be added in sorted order"
);
self.postings.push(Posting { doc_id, term_freq });
}
pub fn add(&mut self, doc_id: DocId, term_freq: u32) {
if let Some(last) = self.postings.last_mut()
&& last.doc_id == doc_id
{
last.term_freq += term_freq;
return;
}
self.postings.push(Posting { doc_id, term_freq });
}
pub fn doc_count(&self) -> u32 {
self.postings.len() as u32
}
pub fn len(&self) -> usize {
self.postings.len()
}
pub fn is_empty(&self) -> bool {
self.postings.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = &Posting> {
self.postings.iter()
}
pub fn serialize<W: Write>(&self, writer: &mut W) -> io::Result<()> {
write_vint(writer, self.postings.len() as u64)?;
let mut prev_doc_id = 0u32;
for posting in &self.postings {
let delta = posting.doc_id - prev_doc_id;
write_vint(writer, delta as u64)?;
write_vint(writer, posting.term_freq as u64)?;
prev_doc_id = posting.doc_id;
}
Ok(())
}
pub fn deserialize<R: Read>(reader: &mut R) -> io::Result<Self> {
let count = read_vint(reader)? as usize;
let mut postings = Vec::with_capacity(count);
let mut prev_doc_id = 0u32;
for _ in 0..count {
let delta = read_vint(reader)? as u32;
let term_freq = read_vint(reader)? as u32;
let doc_id = prev_doc_id + delta;
postings.push(Posting { doc_id, term_freq });
prev_doc_id = doc_id;
}
Ok(Self { postings })
}
}
pub struct PostingListIterator<'a> {
postings: &'a [Posting],
position: usize,
}
impl<'a> PostingListIterator<'a> {
pub fn new(posting_list: &'a PostingList) -> Self {
Self {
postings: &posting_list.postings,
position: 0,
}
}
pub fn doc(&self) -> DocId {
if self.position < self.postings.len() {
self.postings[self.position].doc_id
} else {
TERMINATED
}
}
pub fn term_freq(&self) -> u32 {
if self.position < self.postings.len() {
self.postings[self.position].term_freq
} else {
0
}
}
pub fn advance(&mut self) -> DocId {
self.position += 1;
self.doc()
}
pub fn seek(&mut self, target: DocId) -> DocId {
let remaining = &self.postings[self.position..];
let offset = remaining.partition_point(|p| p.doc_id < target);
self.position += offset;
self.doc()
}
pub fn size_hint(&self) -> usize {
self.postings.len().saturating_sub(self.position)
}
}
pub const TERMINATED: DocId = DocId::MAX;
pub const BLOCK_SIZE: usize = 128;
const L1_INTERVAL: usize = 8;
const L0_SIZE: usize = 16;
const L1_SIZE: usize = 4;
const FOOTER_SIZE: usize = 24;
const FOOTER_V2_SIZE: usize = FOOTER_SIZE + 20;
const FOOTER_MAGIC: u32 = 0x324C_5042;
const FLAG_POS_CURSORS: u32 = 1;
const FLAG_LEN_BOUNDS: u32 = 2;
const FLAG_L1_BOUNDS: u32 = 4;
fn group_bounds_from_l0(l0: &[u8], l0_count: usize) -> Vec<u32> {
let mut groups = Vec::with_capacity(l0_count.div_ceil(L1_INTERVAL));
let mut idx = 0;
while idx < l0_count {
let end = (idx + L1_INTERVAL).min(l0_count);
let mut max_tf = 0u32;
let mut min_len = u32::MAX;
for block in idx..end {
let (_, _, _, word) = read_l0(l0, block);
let (tf, len) = unpack_bounds(word, true);
max_tf = max_tf.max(tf);
min_len = min_len.min(len.unwrap_or(1));
}
groups.push(pack_bounds(max_tf, min_len));
idx = end;
}
groups
}
#[inline]
fn pack_bounds(max_tf: u32, min_len: u32) -> u32 {
max_tf.min(u16::MAX as u32) | (min_len.min(u16::MAX as u32) << 16)
}
#[inline]
fn unpack_bounds(word: u32, packed: bool) -> (u32, Option<u32>) {
if packed {
(word & 0xFFFF, Some(word >> 16))
} else {
(f32::from_bits(word) as u32, None)
}
}
const CURSOR_SIZE: usize = 8;
struct Footer {
stream_len: usize,
l0_count: usize,
l1_count: usize,
doc_count: u32,
max_tf: u32,
total_positions: u64,
has_cursors: bool,
len_bounds: bool,
l1_bounds: bool,
min_len: u32,
}
impl Footer {
fn parse(raw: &[u8]) -> io::Result<Self> {
if raw.len() < FOOTER_SIZE {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"posting data too short",
));
}
let extended = raw.len() >= FOOTER_V2_SIZE
&& u32::from_le_bytes(raw[raw.len() - 4..].try_into().unwrap()) == FOOTER_MAGIC;
let f = raw.len()
- if extended {
FOOTER_V2_SIZE
} else {
FOOTER_SIZE
};
let stream_len = u64::from_le_bytes(raw[f..f + 8].try_into().unwrap()) as usize;
let l0_count = u32::from_le_bytes(raw[f + 8..f + 12].try_into().unwrap()) as usize;
let l1_count = u32::from_le_bytes(raw[f + 12..f + 16].try_into().unwrap()) as usize;
let doc_count = u32::from_le_bytes(raw[f + 16..f + 20].try_into().unwrap());
let max_tf = u32::from_le_bytes(raw[f + 20..f + 24].try_into().unwrap());
let (total_positions, flags, min_len) = if extended {
let total = u64::from_le_bytes(raw[f + 24..f + 32].try_into().unwrap());
let flags = u32::from_le_bytes(raw[f + 32..f + 36].try_into().unwrap());
let min_len = u32::from_le_bytes(raw[f + 36..f + 40].try_into().unwrap());
(total, flags, min_len)
} else {
(0, 0, 0)
};
let footer = Self {
stream_len,
l0_count,
l1_count,
doc_count,
max_tf,
total_positions,
has_cursors: flags & FLAG_POS_CURSORS != 0,
len_bounds: flags & FLAG_LEN_BOUNDS != 0,
l1_bounds: flags & FLAG_L1_BOUNDS != 0,
min_len,
};
if footer.cursors_end() > f {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"posting list sections exceed the footer offset",
));
}
Ok(footer)
}
fn l0_start(&self) -> usize {
self.stream_len
}
fn l0_end(&self) -> usize {
self.l0_start() + self.l0_count * L0_SIZE
}
fn l1_end(&self) -> usize {
self.l0_end() + self.l1_count * L1_SIZE
}
fn l1_bounds_end(&self) -> usize {
self.l1_end() + if self.l1_bounds { self.l1_count * 4 } else { 0 }
}
fn cursors_end(&self) -> usize {
self.l1_bounds_end()
+ if self.has_cursors {
self.l0_count * CURSOR_SIZE
} else {
0
}
}
}
#[inline]
fn read_l0(bytes: &[u8], idx: usize) -> (u32, u32, u32, u32) {
let b = &bytes[idx * L0_SIZE..][..L0_SIZE];
let first_doc = u32::from_le_bytes([b[0], b[1], b[2], b[3]]);
let last_doc = u32::from_le_bytes([b[4], b[5], b[6], b[7]]);
let offset = u32::from_le_bytes([b[8], b[9], b[10], b[11]]);
let bounds = u32::from_le_bytes([b[12], b[13], b[14], b[15]]);
(first_doc, last_doc, offset, bounds)
}
#[inline]
fn write_l0(buf: &mut Vec<u8>, first_doc: u32, last_doc: u32, offset: u32, bounds: u32) {
buf.extend_from_slice(&first_doc.to_le_bytes());
buf.extend_from_slice(&last_doc.to_le_bytes());
buf.extend_from_slice(&offset.to_le_bytes());
buf.extend_from_slice(&bounds.to_le_bytes());
}
#[inline]
fn block_len_from_l0(l0_bytes: &[u8], l0_count: usize, stream_len: usize, idx: usize) -> usize {
let (_, _, offset, _) = read_l0(l0_bytes, idx);
let end = if idx + 1 < l0_count {
read_l0(l0_bytes, idx + 1).2 as usize
} else {
stream_len
};
end.saturating_sub(offset as usize)
}
struct EncodedBlock {
doc_bits: u8,
tf_bits: u8,
}
fn encode_block_arrays(
codec: PostingCodec,
deltas: &[u32],
tfs: &[u32],
stream: &mut Vec<u8>,
) -> EncodedBlock {
match codec {
PostingCodec::Rounded => {
let max_delta = deltas.iter().copied().max().unwrap_or(0);
let doc_bits = simd::round_bit_width(simd::bits_needed(max_delta));
let max_tf = tfs.iter().copied().max().unwrap_or(0);
let tf_bits = simd::round_bit_width(simd::bits_needed(max_tf));
if !deltas.is_empty() {
let rounded = simd::RoundedBitWidth::from_u8(doc_bits);
let start = stream.len();
stream.resize(start + deltas.len() * rounded.bytes_per_value(), 0);
simd::pack_rounded(deltas, rounded, &mut stream[start..]);
}
{
let rounded = simd::RoundedBitWidth::from_u8(tf_bits);
let start = stream.len();
stream.resize(start + tfs.len() * rounded.bytes_per_value(), 0);
simd::pack_rounded(tfs, rounded, &mut stream[start..]);
}
EncodedBlock {
doc_bits: codec.header_byte(doc_bits),
tf_bits,
}
}
PostingCodec::Packed => {
let max_delta = deltas.iter().copied().max().unwrap_or(0);
let doc_bits = simd::bits_needed(max_delta);
let max_tf = tfs.iter().copied().max().unwrap_or(0);
let tf_bits = simd::bits_needed(max_tf);
pack_bits(deltas, doc_bits, stream);
pack_bits(tfs, tf_bits, stream);
EncodedBlock {
doc_bits: codec.header_byte(doc_bits),
tf_bits,
}
}
PostingCodec::Pfor => {
let doc_bits = if deltas.is_empty() {
0
} else {
pack_pfor(deltas, stream)
};
let tf_bits = pack_pfor(tfs, stream);
EncodedBlock {
doc_bits: codec.header_byte(doc_bits),
tf_bits,
}
}
}
}
#[derive(Debug, Clone)]
pub struct BlockPostingList {
stream: OwnedBytes,
l0_bytes: OwnedBytes,
l0_count: usize,
l1_docs: Vec<u32>,
l1_bounds: Vec<u32>,
doc_count: u32,
max_tf: u32,
pos_cursors: Option<OwnedBytes>,
total_positions: u64,
len_bounds: bool,
min_len: u32,
}
impl BlockPostingList {
#[inline]
fn read_l0_entry(&self, idx: usize) -> (u32, u32, u32, u32) {
read_l0(&self.l0_bytes, idx)
}
pub fn from_posting_list(list: &PostingList) -> io::Result<Self> {
Self::build(list, false, None, PostingCodec::Rounded)
}
pub fn from_posting_list_with_codec(
list: &PostingList,
codec: PostingCodec,
) -> io::Result<Self> {
Self::build(list, false, None, codec)
}
pub fn from_posting_list_with_positions(list: &PostingList) -> io::Result<Self> {
Self::build(list, true, None, PostingCodec::Rounded)
}
pub fn from_posting_list_with(
list: &PostingList,
with_positions: bool,
length_of: Option<&dyn Fn(DocId) -> u32>,
) -> io::Result<Self> {
Self::build(list, with_positions, length_of, PostingCodec::Rounded)
}
pub fn from_posting_list_with_options(
list: &PostingList,
with_positions: bool,
length_of: Option<&dyn Fn(DocId) -> u32>,
codec: PostingCodec,
) -> io::Result<Self> {
Self::build(list, with_positions, length_of, codec)
}
fn build(
list: &PostingList,
with_positions: bool,
length_of: Option<&dyn Fn(DocId) -> u32>,
codec: PostingCodec,
) -> io::Result<Self> {
let mut stream: Vec<u8> = Vec::new();
let mut l0_buf: Vec<u8> = Vec::new();
let mut l1_docs: Vec<u32> = Vec::new();
let mut cursors: Vec<u8> = Vec::new();
let mut positions_so_far = 0u64;
let mut l0_count = 0usize;
let mut max_tf = 0u32;
let mut list_min_len = u32::MAX;
let postings = &list.postings;
let mut i = 0;
let mut deltas = Vec::with_capacity(BLOCK_SIZE);
let mut tf_buf = Vec::with_capacity(BLOCK_SIZE);
while i < postings.len() {
if stream.len() > u32::MAX as usize {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"posting list stream exceeds u32::MAX bytes",
));
}
let block_start = stream.len() as u32;
let block_end = (i + BLOCK_SIZE).min(postings.len());
let block = &postings[i..block_end];
let count = block.len();
let block_max_tf = block.iter().map(|p| p.term_freq).max().unwrap_or(0);
max_tf = max_tf.max(block_max_tf);
let base_doc_id = block.first().unwrap().doc_id;
let last_doc_id = block.last().unwrap().doc_id;
deltas.clear();
let mut prev = base_doc_id;
for posting in block.iter().skip(1) {
deltas.push(posting.doc_id - prev);
prev = posting.doc_id;
}
tf_buf.clear();
tf_buf.extend(block.iter().map(|p| p.term_freq));
stream.write_u16::<LittleEndian>(count as u16)?;
stream.write_u32::<LittleEndian>(base_doc_id)?;
let header_at = stream.len();
stream.push(0);
stream.push(0);
let encoded = encode_block_arrays(codec, &deltas, &tf_buf, &mut stream);
stream[header_at] = encoded.doc_bits;
stream[header_at + 1] = encoded.tf_bits;
let block_min_len = length_of.map_or(1, |length_of| {
block
.iter()
.map(|p| length_of(p.doc_id).max(1))
.min()
.unwrap_or(1)
});
list_min_len = list_min_len.min(block_min_len);
write_l0(
&mut l0_buf,
base_doc_id,
last_doc_id,
block_start,
pack_bounds(block_max_tf, block_min_len),
);
l0_count += 1;
if with_positions {
cursors.extend_from_slice(&positions_so_far.to_le_bytes());
positions_so_far += block.iter().map(|p| p.term_freq as u64).sum::<u64>();
}
if l0_count.is_multiple_of(L1_INTERVAL) {
l1_docs.push(last_doc_id);
}
i = block_end;
}
if !l0_count.is_multiple_of(L1_INTERVAL) && l0_count > 0 {
let (_, last_doc, _, _) = read_l0(&l0_buf, l0_count - 1);
l1_docs.push(last_doc);
}
let l1_bounds = group_bounds_from_l0(&l0_buf, l0_count);
Ok(Self {
stream: OwnedBytes::new(stream),
l0_bytes: OwnedBytes::new(l0_buf),
l0_count,
l1_docs,
l1_bounds,
doc_count: postings.len() as u32,
max_tf,
pos_cursors: with_positions.then(|| OwnedBytes::new(cursors)),
total_positions: positions_so_far,
len_bounds: true,
min_len: if list_min_len == u32::MAX {
1
} else {
list_min_len
},
})
}
pub fn serialize<W: Write>(&self, writer: &mut W) -> io::Result<()> {
writer.write_all(&self.stream)?;
writer.write_all(&self.l0_bytes)?;
for &doc in &self.l1_docs {
writer.write_u32::<LittleEndian>(doc)?;
}
for &bounds in &self.l1_bounds {
writer.write_u32::<LittleEndian>(bounds)?;
}
if let Some(cursors) = &self.pos_cursors {
writer.write_all(cursors)?;
}
Self::write_footer(
writer,
self.stream.len() as u64,
self.l0_count,
self.l1_docs.len(),
self.doc_count,
self.max_tf,
self.total_positions,
self.pos_cursors.is_some(),
self.len_bounds.then_some(self.min_len),
!self.l1_bounds.is_empty(),
)
}
#[allow(clippy::too_many_arguments)]
fn write_footer<W: Write>(
writer: &mut W,
stream_len: u64,
l0_count: usize,
l1_count: usize,
doc_count: u32,
max_tf: u32,
total_positions: u64,
has_cursors: bool,
min_len: Option<u32>,
l1_bounds: bool,
) -> io::Result<()> {
writer.write_u64::<LittleEndian>(stream_len)?;
writer.write_u32::<LittleEndian>(l0_count as u32)?;
writer.write_u32::<LittleEndian>(l1_count as u32)?;
writer.write_u32::<LittleEndian>(doc_count)?;
writer.write_u32::<LittleEndian>(max_tf)?;
writer.write_u64::<LittleEndian>(total_positions)?;
let mut flags = 0u32;
if has_cursors {
flags |= FLAG_POS_CURSORS;
}
if min_len.is_some() {
flags |= FLAG_LEN_BOUNDS;
}
if l1_bounds {
flags |= FLAG_L1_BOUNDS;
}
writer.write_u32::<LittleEndian>(flags)?;
writer.write_u32::<LittleEndian>(min_len.unwrap_or(0))?;
writer.write_u32::<LittleEndian>(FOOTER_MAGIC)?;
Ok(())
}
pub fn deserialize(raw: &[u8]) -> io::Result<Self> {
Self::deserialize_zero_copy(OwnedBytes::new(raw.to_vec()))
}
pub fn deserialize_zero_copy(raw: OwnedBytes) -> io::Result<Self> {
let footer = Footer::parse(raw.as_slice())?;
let l1_docs = Self::extract_l1_docs(&raw[footer.l0_end()..], footer.l1_count);
let l1_bounds = if footer.l1_bounds {
Self::extract_l1_docs(&raw[footer.l1_end()..], footer.l1_count)
} else {
Vec::new()
};
let pos_cursors = footer
.has_cursors
.then(|| raw.slice(footer.l1_bounds_end()..footer.cursors_end()));
Ok(Self {
stream: raw.slice(0..footer.stream_len),
l0_bytes: raw.slice(footer.l0_start()..footer.l0_end()),
l0_count: footer.l0_count,
l1_docs,
l1_bounds,
doc_count: footer.doc_count,
max_tf: footer.max_tf,
pos_cursors,
total_positions: footer.total_positions,
len_bounds: footer.len_bounds,
min_len: footer.min_len,
})
}
pub fn min_len(&self) -> Option<u32> {
self.len_bounds.then_some(self.min_len)
}
#[inline]
pub fn block_bounds(&self, block_idx: usize) -> Option<(u32, Option<u32>)> {
if block_idx >= self.l0_count {
return None;
}
let (_, _, _, word) = self.read_l0_entry(block_idx);
Some(unpack_bounds(word, self.len_bounds))
}
#[inline]
pub fn group_bounds(&self, block_idx: usize) -> Option<(u32, u32)> {
if block_idx >= self.l0_count {
return None;
}
let word = *self.l1_bounds.get(block_idx / L1_INTERVAL)?;
let (max_tf, min_len) = unpack_bounds(word, true);
Some((max_tf, min_len.unwrap_or(1)))
}
#[inline]
pub fn group_last_doc(&self, block_idx: usize) -> Option<DocId> {
self.l1_docs.get(block_idx / L1_INTERVAL).copied()
}
#[inline]
pub fn is_group_start(&self, block_idx: usize) -> bool {
block_idx.is_multiple_of(L1_INTERVAL)
}
#[inline]
pub fn next_group_block(&self, block_idx: usize) -> usize {
((block_idx / L1_INTERVAL + 1) * L1_INTERVAL).min(self.l0_count)
}
pub fn has_cursors_bytes(raw: &[u8]) -> bool {
Footer::parse(raw).is_ok_and(|footer| footer.has_cursors)
}
pub fn has_position_cursors(&self) -> bool {
self.pos_cursors.is_some()
}
pub fn total_positions(&self) -> u64 {
self.total_positions
}
#[inline]
pub fn pos_cursor(&self, block_idx: usize) -> Option<u64> {
let cursors = self.pos_cursors.as_ref()?;
let p = block_idx * CURSOR_SIZE;
cursors
.get(p..p + CURSOR_SIZE)
.map(|b| u64::from_le_bytes(b.try_into().unwrap()))
}
fn extract_l1_docs(bytes: &[u8], count: usize) -> Vec<u32> {
let mut docs = Vec::with_capacity(count);
for i in 0..count {
let p = i * L1_SIZE;
docs.push(u32::from_le_bytes(bytes[p..p + 4].try_into().unwrap()));
}
docs
}
pub fn doc_count(&self) -> u32 {
self.doc_count
}
pub fn max_tf(&self) -> u32 {
self.max_tf
}
pub fn num_blocks(&self) -> usize {
self.l0_count
}
pub fn block_max_tf(&self, block_idx: usize) -> Option<u32> {
self.block_bounds(block_idx).map(|(max_tf, _)| max_tf)
}
pub fn concatenate_blocks(sources: &[(BlockPostingList, u32)]) -> io::Result<Self> {
let mut stream: Vec<u8> = Vec::new();
let mut l0_buf: Vec<u8> = Vec::new();
let mut l1_docs: Vec<u32> = Vec::new();
let mut l0_count = 0usize;
let mut total_docs = 0u32;
let mut max_tf = 0u32;
let all_cursors = sources.iter().all(|(s, _)| s.has_position_cursors());
if !all_cursors && sources.iter().any(|(s, _)| s.has_position_cursors()) {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"cannot concatenate posting lists with and without position cursors",
));
}
let mut cursors: Vec<u8> = Vec::new();
let mut positions_before = 0u64;
let mut min_len = u32::MAX;
for (source, doc_offset) in sources {
max_tf = max_tf.max(source.max_tf);
min_len = min_len.min(source.min_len().unwrap_or(1));
for block_idx in 0..source.num_blocks() {
if all_cursors {
let cursor = source.pos_cursor(block_idx).unwrap_or(0) + positions_before;
cursors.extend_from_slice(&cursor.to_le_bytes());
}
let (first_doc, last_doc, offset, word) = source.read_l0_entry(block_idx);
let (block_max_tf, block_min_len) = unpack_bounds(word, source.len_bounds);
let bounds = pack_bounds(block_max_tf, block_min_len.unwrap_or(1));
let blk_size = source.block_len(block_idx);
let block_bytes = &source.stream[offset as usize..offset as usize + blk_size];
let count = u16::from_le_bytes(block_bytes[0..2].try_into().unwrap());
if stream.len() > u32::MAX as usize {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"posting list stream exceeds u32::MAX bytes during concatenation",
));
}
let new_offset = stream.len() as u32;
stream.write_u16::<LittleEndian>(count)?;
stream.write_u32::<LittleEndian>(first_doc + doc_offset)?;
stream.extend_from_slice(&block_bytes[6..]);
let new_last = last_doc + doc_offset;
write_l0(
&mut l0_buf,
first_doc + doc_offset,
new_last,
new_offset,
bounds,
);
l0_count += 1;
total_docs += count as u32;
if l0_count.is_multiple_of(L1_INTERVAL) {
l1_docs.push(new_last);
}
}
positions_before += source.total_positions;
}
if !l0_count.is_multiple_of(L1_INTERVAL) && l0_count > 0 {
let (_, last_doc, _, _) = read_l0(&l0_buf, l0_count - 1);
l1_docs.push(last_doc);
}
let l1_bounds = group_bounds_from_l0(&l0_buf, l0_count);
Ok(Self {
stream: OwnedBytes::new(stream),
l0_bytes: OwnedBytes::new(l0_buf),
l0_count,
l1_docs,
l1_bounds,
doc_count: total_docs,
max_tf,
pos_cursors: all_cursors.then(|| OwnedBytes::new(cursors)),
total_positions: if all_cursors { positions_before } else { 0 },
len_bounds: true,
min_len: if min_len == u32::MAX { 1 } else { min_len },
})
}
pub fn concatenate_streaming<W: Write>(
sources: &[(&[u8], u32)], writer: &mut W,
) -> crate::Result<(u32, usize)> {
let mut metas: Vec<Footer> = Vec::with_capacity(sources.len());
let mut total_docs = 0u32;
let mut merged_max_tf = 0u32;
let mut merged_min_len = u32::MAX;
for (source_index, (raw, _)) in sources.iter().enumerate() {
let footer = Footer::parse(raw).map_err(|e| {
crate::Error::Corruption(format!(
"posting list source {source_index} has an invalid footer: {e}"
))
})?;
total_docs += footer.doc_count;
merged_max_tf = merged_max_tf.max(footer.max_tf);
merged_min_len = merged_min_len.min(if footer.len_bounds { footer.min_len } else { 1 });
metas.push(footer);
}
if sources.len() == 1 && sources[0].1 == 0 {
writer.write_all(sources[0].0)?;
return Ok((metas[0].doc_count, sources[0].0.len()));
}
let all_cursors = metas.iter().all(|m| m.has_cursors);
if !all_cursors && metas.iter().any(|m| m.has_cursors) {
return Err(crate::Error::Corruption(
"cannot concatenate posting lists with and without position cursors".into(),
));
}
let mut out_l0: Vec<u8> = Vec::new();
let mut out_l1_docs: Vec<u32> = Vec::new();
let mut out_cursors: Vec<u8> = Vec::new();
let mut positions_before = 0u64;
let mut out_l0_count = 0usize;
let mut stream_written = 0u64;
let mut patch_buf = [0u8; 8];
for (src_idx, meta) in metas.iter().enumerate() {
let (raw, doc_offset) = &sources[src_idx];
let l0_base = meta.l0_start(); let src_stream = &raw[..meta.stream_len];
let cursors_base = meta.l1_bounds_end();
for i in 0..meta.l0_count {
let (first_doc, last_doc, offset, word) = read_l0(&raw[l0_base..], i);
let (block_max_tf, block_min_len) = unpack_bounds(word, meta.len_bounds);
let bounds = pack_bounds(block_max_tf, block_min_len.unwrap_or(1));
if all_cursors {
let p = cursors_base + i * CURSOR_SIZE;
let cursor = u64::from_le_bytes(raw[p..p + CURSOR_SIZE].try_into().unwrap());
out_cursors.extend_from_slice(&(cursor + positions_before).to_le_bytes());
}
let blk_size =
block_len_from_l0(&raw[l0_base..], meta.l0_count, meta.stream_len, i);
let block = &src_stream[offset as usize..offset as usize + blk_size];
let new_last = last_doc + doc_offset;
if stream_written > u32::MAX as u64 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"posting list stream exceeds u32::MAX bytes during streaming merge",
)
.into());
}
write_l0(
&mut out_l0,
first_doc + doc_offset,
new_last,
stream_written as u32,
bounds,
);
out_l0_count += 1;
if out_l0_count.is_multiple_of(L1_INTERVAL) {
out_l1_docs.push(new_last);
}
patch_buf.copy_from_slice(&block[0..8]);
let blk_first = u32::from_le_bytes(patch_buf[2..6].try_into().unwrap());
patch_buf[2..6].copy_from_slice(&(blk_first + doc_offset).to_le_bytes());
writer.write_all(&patch_buf)?;
writer.write_all(&block[8..])?;
stream_written += blk_size as u64;
}
positions_before += meta.total_positions;
}
if !out_l0_count.is_multiple_of(L1_INTERVAL) && out_l0_count > 0 {
let (_, last_doc, _, _) = read_l0(&out_l0, out_l0_count - 1);
out_l1_docs.push(last_doc);
}
let out_l1_bounds = group_bounds_from_l0(&out_l0, out_l0_count);
writer.write_all(&out_l0)?;
for &doc in &out_l1_docs {
writer.write_u32::<LittleEndian>(doc)?;
}
for &bounds in &out_l1_bounds {
writer.write_u32::<LittleEndian>(bounds)?;
}
writer.write_all(&out_cursors)?;
Self::write_footer(
writer,
stream_written,
out_l0_count,
out_l1_docs.len(),
total_docs,
merged_max_tf,
if all_cursors { positions_before } else { 0 },
all_cursors,
Some(if merged_min_len == u32::MAX {
1
} else {
merged_min_len
}),
true,
)?;
let l1_bytes_len = out_l1_docs.len() * L1_SIZE + out_l1_bounds.len() * 4;
let total_bytes = stream_written as usize
+ out_l0.len()
+ l1_bytes_len
+ out_cursors.len()
+ FOOTER_V2_SIZE;
Ok((total_docs, total_bytes))
}
pub fn decode_block_into(
&self,
block_idx: usize,
doc_ids: &mut Vec<u32>,
tfs: &mut Vec<u32>,
) -> bool {
if let Some((offset, tf_start, count)) = self.decode_block_doc_ids_only(block_idx, doc_ids)
{
self.decode_block_tfs_deferred(offset, tf_start, count, tfs);
true
} else {
false
}
}
pub fn decode_block_doc_ids_only(
&self,
block_idx: usize,
doc_ids: &mut Vec<u32>,
) -> Option<(usize, usize, usize)> {
if block_idx >= self.l0_count {
return None;
}
let (_, _, offset, _) = self.read_l0_entry(block_idx);
let pos = offset as usize;
let blk_size = self.block_len(block_idx);
let block_data = &self.stream[pos..pos + blk_size];
let count = u16::from_le_bytes(block_data[0..2].try_into().unwrap()) as usize;
let first_doc = u32::from_le_bytes(block_data[2..6].try_into().unwrap());
let (codec, doc_width) = PostingCodec::from_header_byte(block_data[6]).ok()?;
doc_ids.clear();
doc_ids.resize(count, 0);
doc_ids[0] = first_doc;
let payload = &block_data[8..];
let deltas_bytes = if count > 1 {
match codec {
PostingCodec::Rounded => {
let rounded = simd::RoundedBitWidth::from_u8(doc_width);
let bytes = (count - 1) * rounded.bytes_per_value();
simd::unpack_rounded(&payload[..bytes], rounded, &mut doc_ids[1..], count - 1);
bytes
}
PostingCodec::Packed => {
let bytes = packed_bytes(count - 1, doc_width);
unpack_bits(&payload[..bytes], doc_width, &mut doc_ids[1..], count - 1);
bytes
}
PostingCodec::Pfor => {
let bytes = pfor_payload_len(payload, count - 1, doc_width).ok()?;
unpack_pfor(&payload[..bytes], doc_width, &mut doc_ids[1..], count - 1).ok()?;
bytes
}
}
} else {
0
};
for i in 1..count {
doc_ids[i] = doc_ids[i].wrapping_add(doc_ids[i - 1]);
}
let tfs_start = 8 + deltas_bytes;
Some((pos, tfs_start, count))
}
pub fn decode_block_tfs_deferred(
&self,
block_offset: usize,
tf_start: usize,
count: usize,
tfs: &mut Vec<u32>,
) {
let block_data = &self.stream[block_offset..];
let codec = PostingCodec::from_header_byte(block_data[6])
.map(|(codec, _)| codec)
.unwrap_or_default();
let tf_bits = block_data[7];
tfs.clear();
tfs.resize(count, 0);
let payload = &block_data[tf_start..];
match codec {
PostingCodec::Rounded => {
let rounded = simd::RoundedBitWidth::from_u8(tf_bits);
simd::unpack_rounded(
&payload[..count * rounded.bytes_per_value()],
rounded,
tfs,
count,
);
}
PostingCodec::Packed => {
unpack_bits(
&payload[..packed_bytes(count, tf_bits)],
tf_bits,
tfs,
count,
);
}
PostingCodec::Pfor => {
if let Ok(len) = pfor_payload_len(payload, count, tf_bits) {
let _ = unpack_pfor(&payload[..len], tf_bits, tfs, count);
}
}
}
}
#[inline]
fn block_len(&self, block_idx: usize) -> usize {
block_len_from_l0(&self.l0_bytes, self.l0_count, self.stream.len(), block_idx)
}
pub fn block_codec(&self, block_idx: usize) -> Option<PostingCodec> {
if block_idx >= self.l0_count {
return None;
}
let (_, _, offset, _) = self.read_l0_entry(block_idx);
PostingCodec::from_header_byte(self.stream[offset as usize + 6])
.ok()
.map(|(codec, _)| codec)
}
#[inline]
pub fn block_first_doc(&self, block_idx: usize) -> Option<DocId> {
if block_idx >= self.l0_count {
return None;
}
let (first_doc, _, _, _) = self.read_l0_entry(block_idx);
Some(first_doc)
}
#[inline]
pub fn block_last_doc(&self, block_idx: usize) -> Option<DocId> {
if block_idx >= self.l0_count {
return None;
}
let (_, last_doc, _, _) = self.read_l0_entry(block_idx);
Some(last_doc)
}
pub fn seek_block(&self, target: DocId, from_block: usize) -> Option<usize> {
if from_block >= self.l0_count {
return None;
}
let from_l1 = from_block / L1_INTERVAL;
let l1_idx = if !self.l1_docs.is_empty() {
let idx = from_l1 + simd::find_first_ge_u32(&self.l1_docs[from_l1..], target);
if idx >= self.l1_docs.len() {
return None;
}
idx
} else {
return None;
};
let start = (l1_idx * L1_INTERVAL).max(from_block);
let end = ((l1_idx + 1) * L1_INTERVAL).min(self.l0_count);
let count = end - start;
let mut last_docs = [u32::MAX; L1_INTERVAL];
for (j, idx) in (start..end).enumerate() {
let (_, ld, _, _) = read_l0(&self.l0_bytes, idx);
last_docs[j] = ld;
}
let within = simd::find_first_ge_u32(&last_docs[..count], target);
let block_idx = start + within;
if block_idx < self.l0_count {
Some(block_idx)
} else {
None
}
}
pub fn iterator(&self) -> BlockPostingIterator<'_> {
BlockPostingIterator::new(self)
}
pub fn into_iterator(self) -> BlockPostingIterator<'static> {
BlockPostingIterator::owned(self)
}
}
pub struct BlockPostingIterator<'a> {
block_list: std::borrow::Cow<'a, BlockPostingList>,
current_block: usize,
block_doc_ids: Vec<u32>,
block_tfs: Vec<u32>,
position_in_block: usize,
tf_prefix: u64,
exhausted: bool,
}
impl<'a> BlockPostingIterator<'a> {
fn new(block_list: &'a BlockPostingList) -> Self {
let exhausted = block_list.l0_count == 0;
let mut iter = Self {
block_list: std::borrow::Cow::Borrowed(block_list),
current_block: 0,
block_doc_ids: Vec::with_capacity(BLOCK_SIZE),
block_tfs: Vec::with_capacity(BLOCK_SIZE),
position_in_block: 0,
tf_prefix: 0,
exhausted,
};
if !iter.exhausted {
iter.load_block(0);
}
iter
}
fn owned(block_list: BlockPostingList) -> BlockPostingIterator<'static> {
let exhausted = block_list.l0_count == 0;
let mut iter = BlockPostingIterator {
block_list: std::borrow::Cow::Owned(block_list),
current_block: 0,
block_doc_ids: Vec::with_capacity(BLOCK_SIZE),
block_tfs: Vec::with_capacity(BLOCK_SIZE),
position_in_block: 0,
tf_prefix: 0,
exhausted,
};
if !iter.exhausted {
iter.load_block(0);
}
iter
}
fn load_block(&mut self, block_idx: usize) {
if block_idx >= self.block_list.l0_count {
self.exhausted = true;
return;
}
self.current_block = block_idx;
self.position_in_block = 0;
self.tf_prefix = 0;
self.block_list
.decode_block_into(block_idx, &mut self.block_doc_ids, &mut self.block_tfs);
}
#[inline]
pub fn position_cursor(&self) -> u64 {
self.block_list.pos_cursor(self.current_block).unwrap_or(0) + self.tf_prefix
}
pub fn doc(&self) -> DocId {
if self.exhausted {
TERMINATED
} else if self.position_in_block < self.block_doc_ids.len() {
self.block_doc_ids[self.position_in_block]
} else {
TERMINATED
}
}
pub fn term_freq(&self) -> u32 {
if self.exhausted || self.position_in_block >= self.block_tfs.len() {
0
} else {
self.block_tfs[self.position_in_block]
}
}
pub fn advance(&mut self) -> DocId {
if self.exhausted {
return TERMINATED;
}
if let Some(&tf) = self.block_tfs.get(self.position_in_block) {
self.tf_prefix += tf as u64;
}
self.position_in_block += 1;
if self.position_in_block >= self.block_doc_ids.len() {
self.load_block(self.current_block + 1);
}
self.doc()
}
pub fn seek(&mut self, target: DocId) -> DocId {
if self.exhausted {
return TERMINATED;
}
let block_idx = match self.block_list.seek_block(target, self.current_block) {
Some(idx) => idx,
None => {
self.exhausted = true;
return TERMINATED;
}
};
if block_idx != self.current_block {
self.load_block(block_idx);
}
let remaining = &self.block_doc_ids[self.position_in_block..];
let pos = crate::structures::simd::find_first_ge_u32(remaining, target);
self.tf_prefix += self.block_tfs[self.position_in_block..self.position_in_block + pos]
.iter()
.map(|&tf| tf as u64)
.sum::<u64>();
self.position_in_block += pos;
if self.position_in_block >= self.block_doc_ids.len() {
self.load_block(self.current_block + 1);
}
self.doc()
}
pub fn skip_to_next_block(&mut self) -> DocId {
if self.exhausted {
return TERMINATED;
}
self.load_block(self.current_block + 1);
self.doc()
}
#[inline]
pub fn current_block_idx(&self) -> usize {
self.current_block
}
#[inline]
pub fn num_blocks(&self) -> usize {
self.block_list.l0_count
}
#[inline]
pub fn current_block_max_tf(&self) -> u32 {
if self.exhausted || self.current_block >= self.block_list.l0_count {
0
} else {
self.block_list
.block_max_tf(self.current_block)
.unwrap_or(0)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_posting_list_basic() {
let mut list = PostingList::new();
list.push(1, 2);
list.push(5, 1);
list.push(10, 3);
assert_eq!(list.len(), 3);
let mut iter = PostingListIterator::new(&list);
assert_eq!(iter.doc(), 1);
assert_eq!(iter.term_freq(), 2);
assert_eq!(iter.advance(), 5);
assert_eq!(iter.term_freq(), 1);
assert_eq!(iter.advance(), 10);
assert_eq!(iter.term_freq(), 3);
assert_eq!(iter.advance(), TERMINATED);
}
#[test]
fn test_posting_list_serialization() {
let mut list = PostingList::new();
for i in 0..100 {
list.push(i * 3, (i % 5) + 1);
}
let mut buffer = Vec::new();
list.serialize(&mut buffer).unwrap();
let deserialized = PostingList::deserialize(&mut &buffer[..]).unwrap();
assert_eq!(deserialized.len(), list.len());
for (a, b) in list.iter().zip(deserialized.iter()) {
assert_eq!(a, b);
}
}
#[test]
fn test_posting_list_seek() {
let mut list = PostingList::new();
for i in 0..100 {
list.push(i * 2, 1);
}
let mut iter = PostingListIterator::new(&list);
assert_eq!(iter.seek(50), 50);
assert_eq!(iter.seek(51), 52);
assert_eq!(iter.seek(200), TERMINATED);
}
#[test]
fn test_block_posting_list() {
let mut list = PostingList::new();
for i in 0..500 {
list.push(i * 2, (i % 10) + 1);
}
let block_list = BlockPostingList::from_posting_list(&list).unwrap();
assert_eq!(block_list.doc_count(), 500);
let mut iter = block_list.iterator();
assert_eq!(iter.doc(), 0);
assert_eq!(iter.term_freq(), 1);
assert_eq!(iter.seek(500), 500);
assert_eq!(iter.seek(998), 998);
assert_eq!(iter.seek(1000), TERMINATED);
}
#[test]
fn test_block_posting_list_serialization() {
let mut list = PostingList::new();
for i in 0..300 {
list.push(i * 3, i + 1);
}
let block_list = BlockPostingList::from_posting_list(&list).unwrap();
let mut buffer = Vec::new();
block_list.serialize(&mut buffer).unwrap();
let deserialized = BlockPostingList::deserialize(&buffer[..]).unwrap();
assert_eq!(deserialized.doc_count(), block_list.doc_count());
let mut iter1 = block_list.iterator();
let mut iter2 = deserialized.iterator();
while iter1.doc() != TERMINATED {
assert_eq!(iter1.doc(), iter2.doc());
assert_eq!(iter1.term_freq(), iter2.term_freq());
iter1.advance();
iter2.advance();
}
assert_eq!(iter2.doc(), TERMINATED);
}
fn collect_postings(bpl: &BlockPostingList) -> Vec<(u32, u32)> {
let mut result = Vec::new();
let mut it = bpl.iterator();
while it.doc() != TERMINATED {
result.push((it.doc(), it.term_freq()));
it.advance();
}
result
}
fn build_bpl(postings: &[(u32, u32)]) -> BlockPostingList {
let mut pl = PostingList::new();
for &(doc_id, tf) in postings {
pl.push(doc_id, tf);
}
BlockPostingList::from_posting_list(&pl).unwrap()
}
fn serialize_bpl(bpl: &BlockPostingList) -> Vec<u8> {
let mut buf = Vec::new();
bpl.serialize(&mut buf).unwrap();
buf
}
#[test]
fn test_concatenate_blocks_two_segments() {
let a: Vec<(u32, u32)> = (0..100).map(|i| (i * 2, i + 1)).collect();
let bpl_a = build_bpl(&a);
let b: Vec<(u32, u32)> = (0..100).map(|i| (i * 3, i + 2)).collect();
let bpl_b = build_bpl(&b);
let merged =
BlockPostingList::concatenate_blocks(&[(bpl_a.clone(), 0), (bpl_b.clone(), 200)])
.unwrap();
assert_eq!(merged.doc_count(), 200);
let postings = collect_postings(&merged);
assert_eq!(postings.len(), 200);
for (i, p) in postings.iter().enumerate().take(100) {
assert_eq!(*p, (i as u32 * 2, i as u32 + 1));
}
for i in 0..100 {
assert_eq!(postings[100 + i], (i as u32 * 3 + 200, i as u32 + 2));
}
}
#[test]
fn test_concatenate_streaming_matches_blocks() {
let seg_a: Vec<(u32, u32)> = (0..250).map(|i| (i * 2, (i % 7) + 1)).collect();
let seg_b: Vec<(u32, u32)> = (0..180).map(|i| (i * 5, (i % 3) + 1)).collect();
let seg_c: Vec<(u32, u32)> = (0..90).map(|i| (i * 10, (i % 11) + 1)).collect();
let bpl_a = build_bpl(&seg_a);
let bpl_b = build_bpl(&seg_b);
let bpl_c = build_bpl(&seg_c);
let offset_b = 1000u32;
let offset_c = 2000u32;
let ref_merged = BlockPostingList::concatenate_blocks(&[
(bpl_a.clone(), 0),
(bpl_b.clone(), offset_b),
(bpl_c.clone(), offset_c),
])
.unwrap();
let mut ref_buf = Vec::new();
ref_merged.serialize(&mut ref_buf).unwrap();
let bytes_a = serialize_bpl(&bpl_a);
let bytes_b = serialize_bpl(&bpl_b);
let bytes_c = serialize_bpl(&bpl_c);
let sources: Vec<(&[u8], u32)> =
vec![(&bytes_a, 0), (&bytes_b, offset_b), (&bytes_c, offset_c)];
let mut stream_buf = Vec::new();
let (doc_count, bytes_written) =
BlockPostingList::concatenate_streaming(&sources, &mut stream_buf).unwrap();
assert_eq!(doc_count, 520); assert_eq!(bytes_written, stream_buf.len());
let ref_postings = collect_postings(&BlockPostingList::deserialize(&ref_buf).unwrap());
let stream_postings =
collect_postings(&BlockPostingList::deserialize(&stream_buf).unwrap());
assert_eq!(ref_postings.len(), stream_postings.len());
for (i, (r, s)) in ref_postings.iter().zip(stream_postings.iter()).enumerate() {
assert_eq!(r, s, "mismatch at posting {}", i);
}
}
#[test]
fn test_concatenate_streaming_short_source_returns_corruption() {
let seg_a: Vec<(u32, u32)> = (0..250).map(|i| (i * 2, (i % 7) + 1)).collect();
let seg_c: Vec<(u32, u32)> = (0..90).map(|i| (i * 10, (i % 11) + 1)).collect();
let bytes_a = serialize_bpl(&build_bpl(&seg_a));
let bytes_c = serialize_bpl(&build_bpl(&seg_c));
let short = vec![0u8; FOOTER_SIZE - 1];
let sources: Vec<(&[u8], u32)> = vec![(&bytes_a, 0), (&short, 1000), (&bytes_c, 2000)];
let mut out = Vec::new();
let result = BlockPostingList::concatenate_streaming(&sources, &mut out);
assert!(
matches!(result, Err(crate::Error::Corruption(_))),
"short/corrupt source must be a Corruption error, not silently skipped: {:?}",
result.map(|r| r.0)
);
}
#[test]
fn test_multi_round_merge() {
let segments: Vec<Vec<(u32, u32)>> = (0..4)
.map(|seg| (0..200).map(|i| (i * 3, (i + seg * 7) % 10 + 1)).collect())
.collect();
let bpls: Vec<BlockPostingList> = segments.iter().map(|s| build_bpl(s)).collect();
let serialized: Vec<Vec<u8>> = bpls.iter().map(serialize_bpl).collect();
let mut merged_01 = Vec::new();
let sources_01: Vec<(&[u8], u32)> = vec![(&serialized[0], 0), (&serialized[1], 600)];
let (dc_01, _) =
BlockPostingList::concatenate_streaming(&sources_01, &mut merged_01).unwrap();
assert_eq!(dc_01, 400);
let mut merged_23 = Vec::new();
let sources_23: Vec<(&[u8], u32)> = vec![(&serialized[2], 0), (&serialized[3], 600)];
let (dc_23, _) =
BlockPostingList::concatenate_streaming(&sources_23, &mut merged_23).unwrap();
assert_eq!(dc_23, 400);
let mut final_merged = Vec::new();
let sources_final: Vec<(&[u8], u32)> = vec![(&merged_01, 0), (&merged_23, 1200)];
let (dc_final, _) =
BlockPostingList::concatenate_streaming(&sources_final, &mut final_merged).unwrap();
assert_eq!(dc_final, 800);
let final_bpl = BlockPostingList::deserialize(&final_merged).unwrap();
let postings = collect_postings(&final_bpl);
assert_eq!(postings.len(), 800);
assert_eq!(postings[0].0, 0); assert_eq!(postings[199].0, 597); assert_eq!(postings[200].0, 600); assert_eq!(postings[399].0, 1197); assert_eq!(postings[400].0, 1200); assert_eq!(postings[799].0, 2397);
for seg in 0u32..4 {
for i in 0u32..200 {
let idx = (seg * 200 + i) as usize;
assert_eq!(
postings[idx].1,
(i + seg * 7) % 10 + 1,
"seg{} tf[{}]",
seg,
i
);
}
}
let mut it = final_bpl.iterator();
assert_eq!(it.seek(600), 600);
assert_eq!(it.seek(1200), 1200);
assert_eq!(it.seek(2397), 2397);
assert_eq!(it.seek(2398), TERMINATED);
}
#[test]
fn test_large_scale_merge() {
let num_segments = 5;
let docs_per_segment = 2000;
let docs_gap = 3;
let segments: Vec<Vec<(u32, u32)>> = (0..num_segments)
.map(|seg| {
(0..docs_per_segment)
.map(|i| (i as u32 * docs_gap, (i as u32 + seg as u32) % 20 + 1))
.collect()
})
.collect();
let bpls: Vec<BlockPostingList> = segments.iter().map(|s| build_bpl(s)).collect();
for bpl in &bpls {
assert!(
bpl.num_blocks() >= 15,
"expected >=15 blocks, got {}",
bpl.num_blocks()
);
}
let serialized: Vec<Vec<u8>> = bpls.iter().map(serialize_bpl).collect();
let max_doc_per_seg = (docs_per_segment as u32 - 1) * docs_gap;
let offsets: Vec<u32> = (0..num_segments)
.map(|i| i as u32 * (max_doc_per_seg + 1))
.collect();
let sources: Vec<(&[u8], u32)> = serialized
.iter()
.zip(offsets.iter())
.map(|(b, o)| (b.as_slice(), *o))
.collect();
let mut merged = Vec::new();
let (doc_count, _) =
BlockPostingList::concatenate_streaming(&sources, &mut merged).unwrap();
assert_eq!(doc_count, (num_segments * docs_per_segment) as u32);
let merged_bpl = BlockPostingList::deserialize(&merged).unwrap();
let postings = collect_postings(&merged_bpl);
assert_eq!(postings.len(), num_segments * docs_per_segment);
for i in 1..postings.len() {
assert!(
postings[i].0 > postings[i - 1].0 || (i % docs_per_segment == 0), "doc_id not increasing at {}: {} vs {}",
i,
postings[i - 1].0,
postings[i].0,
);
}
let mut it = merged_bpl.iterator();
for (seg, &expected_first) in offsets.iter().enumerate() {
assert_eq!(
it.seek(expected_first),
expected_first,
"seek to segment {} start",
seg
);
}
}
#[test]
fn test_merge_edge_cases() {
let bpl_a = build_bpl(&[(0, 5)]);
let bpl_b = build_bpl(&[(0, 3)]);
let merged =
BlockPostingList::concatenate_blocks(&[(bpl_a.clone(), 0), (bpl_b.clone(), 1)])
.unwrap();
assert_eq!(merged.doc_count(), 2);
let p = collect_postings(&merged);
assert_eq!(p, vec![(0, 5), (1, 3)]);
let exact_block: Vec<(u32, u32)> = (0..BLOCK_SIZE as u32).map(|i| (i, i % 5 + 1)).collect();
let bpl_exact = build_bpl(&exact_block);
assert_eq!(bpl_exact.num_blocks(), 1);
let bytes = serialize_bpl(&bpl_exact);
let mut out = Vec::new();
let sources: Vec<(&[u8], u32)> = vec![(&bytes, 0), (&bytes, BLOCK_SIZE as u32)];
let (dc, _) = BlockPostingList::concatenate_streaming(&sources, &mut out).unwrap();
assert_eq!(dc, BLOCK_SIZE as u32 * 2);
let merged = BlockPostingList::deserialize(&out).unwrap();
let postings = collect_postings(&merged);
assert_eq!(postings.len(), BLOCK_SIZE * 2);
assert_eq!(postings[BLOCK_SIZE].0, BLOCK_SIZE as u32);
let over_block: Vec<(u32, u32)> = (0..BLOCK_SIZE as u32 + 1).map(|i| (i * 2, 1)).collect();
let bpl_over = build_bpl(&over_block);
assert_eq!(bpl_over.num_blocks(), 2);
}
#[test]
fn test_streaming_roundtrip_single_source() {
let docs: Vec<(u32, u32)> = (0..500).map(|i| (i * 7, i % 15 + 1)).collect();
let bpl = build_bpl(&docs);
let direct = serialize_bpl(&bpl);
let sources: Vec<(&[u8], u32)> = vec![(&direct, 0)];
let mut streamed = Vec::new();
BlockPostingList::concatenate_streaming(&sources, &mut streamed).unwrap();
let p1 = collect_postings(&BlockPostingList::deserialize(&direct).unwrap());
let p2 = collect_postings(&BlockPostingList::deserialize(&streamed).unwrap());
assert_eq!(p1, p2);
}
#[test]
fn test_max_tf_preserved_through_merge() {
let mut a = Vec::new();
for i in 0..200 {
a.push((i * 2, if i == 100 { 50 } else { 1 }));
}
let bpl_a = build_bpl(&a);
assert_eq!(bpl_a.max_tf(), 50);
let mut b = Vec::new();
for i in 0..200 {
b.push((i * 2, if i == 50 { 30 } else { 2 }));
}
let bpl_b = build_bpl(&b);
assert_eq!(bpl_b.max_tf(), 30);
let bytes_a = serialize_bpl(&bpl_a);
let bytes_b = serialize_bpl(&bpl_b);
let sources: Vec<(&[u8], u32)> = vec![(&bytes_a, 0), (&bytes_b, 1000)];
let mut out = Vec::new();
BlockPostingList::concatenate_streaming(&sources, &mut out).unwrap();
let merged = BlockPostingList::deserialize(&out).unwrap();
assert_eq!(merged.max_tf(), 50);
assert_eq!(merged.doc_count(), 400);
}
#[test]
fn test_l0_l1_counts() {
let bpl = build_bpl(&(0..50u32).map(|i| (i, 1)).collect::<Vec<_>>());
assert_eq!(bpl.num_blocks(), 1);
assert_eq!(bpl.l1_docs.len(), 1);
let n = BLOCK_SIZE * L1_INTERVAL;
let bpl = build_bpl(&(0..n as u32).map(|i| (i * 2, 1)).collect::<Vec<_>>());
assert_eq!(bpl.num_blocks(), L1_INTERVAL);
assert_eq!(bpl.l1_docs.len(), 1);
let n = BLOCK_SIZE * L1_INTERVAL + 1;
let bpl = build_bpl(&(0..n as u32).map(|i| (i * 2, 1)).collect::<Vec<_>>());
assert_eq!(bpl.num_blocks(), L1_INTERVAL + 1);
assert_eq!(bpl.l1_docs.len(), 2);
let n = BLOCK_SIZE * L1_INTERVAL * 3;
let bpl = build_bpl(&(0..n as u32).map(|i| (i, 1)).collect::<Vec<_>>());
assert_eq!(bpl.num_blocks(), L1_INTERVAL * 3);
assert_eq!(bpl.l1_docs.len(), 3);
}
#[test]
fn test_l1_last_doc_values() {
let n = BLOCK_SIZE * 20;
let docs: Vec<(u32, u32)> = (0..n as u32).map(|i| (i * 3, 1)).collect();
let bpl = build_bpl(&docs);
assert_eq!(bpl.num_blocks(), 20);
assert_eq!(bpl.l1_docs.len(), 3);
let expected_l1_0 = bpl.block_last_doc(7).unwrap();
assert_eq!(bpl.l1_docs[0], expected_l1_0);
let expected_l1_1 = bpl.block_last_doc(15).unwrap();
assert_eq!(bpl.l1_docs[1], expected_l1_1);
let expected_l1_2 = bpl.block_last_doc(19).unwrap();
assert_eq!(bpl.l1_docs[2], expected_l1_2);
}
#[test]
fn test_seek_block_basic() {
let n = BLOCK_SIZE * 20;
let docs: Vec<(u32, u32)> = (0..n as u32).map(|i| (i * 10, 1)).collect();
let bpl = build_bpl(&docs);
assert_eq!(bpl.seek_block(0, 0), Some(0));
for blk in 0..20 {
let first = bpl.block_first_doc(blk).unwrap();
assert_eq!(
bpl.seek_block(first, 0),
Some(blk),
"seek to block {} first_doc",
blk
);
}
for blk in 0..20 {
let last = bpl.block_last_doc(blk).unwrap();
assert_eq!(
bpl.seek_block(last, 0),
Some(blk),
"seek to block {} last_doc",
blk
);
}
let max_doc = bpl.block_last_doc(19).unwrap();
assert_eq!(bpl.seek_block(max_doc + 1, 0), None);
let mid_doc = bpl.block_first_doc(10).unwrap();
assert_eq!(bpl.seek_block(mid_doc, 10), Some(10));
assert_eq!(
bpl.seek_block(mid_doc, 11),
Some(11).or(bpl.seek_block(mid_doc, 11))
);
}
#[test]
fn test_seek_block_across_l1_boundaries() {
let n = BLOCK_SIZE * 24;
let docs: Vec<(u32, u32)> = (0..n as u32).map(|i| (i * 5, 1)).collect();
let bpl = build_bpl(&docs);
assert_eq!(bpl.l1_docs.len(), 3);
for group in 0..3 {
let blk = group * L1_INTERVAL;
let target = bpl.block_first_doc(blk).unwrap();
assert_eq!(
bpl.seek_block(target, 0),
Some(blk),
"seek to group {} block {}",
group,
blk
);
}
let target = bpl.block_first_doc(20).unwrap() + 1;
assert_eq!(bpl.seek_block(target, 0), Some(20));
}
#[test]
fn block_len_matches_l0_offsets() {
let bpl = build_bpl(&(0..1000).map(|i| (i * 3, 1 + i % 4)).collect::<Vec<_>>());
let mut total = 0usize;
for b in 0..bpl.num_blocks() {
let (_, _, offset, _) = bpl.read_l0_entry(b);
assert_eq!(offset as usize, total, "block {b} offset");
total += bpl.block_len(b);
}
assert_eq!(total, bpl.stream.len());
}
#[test]
fn every_codec_round_trips_and_seeks() {
let mut postings: Vec<(u32, u32)> = Vec::new();
let mut doc = 0u32;
for i in 0..5000u32 {
doc += if i % 97 == 0 { 100_000 } else { 1 + i % 7 };
let tf = if i % 131 == 0 { 5000 } else { 1 + i % 3 };
postings.push((doc, tf));
}
let mut list = PostingList::new();
for &(d, tf) in &postings {
list.push(d, tf);
}
let rounded = BlockPostingList::from_posting_list(&list).unwrap();
let mut sizes = Vec::new();
for codec in [
PostingCodec::Rounded,
PostingCodec::Packed,
PostingCodec::Pfor,
] {
let bpl = BlockPostingList::from_posting_list_with_codec(&list, codec).unwrap();
assert_eq!(collect_postings(&bpl), postings, "{codec}");
for b in 0..bpl.num_blocks() {
assert_eq!(bpl.block_codec(b), Some(codec));
assert_eq!(bpl.block_max_tf(b), rounded.block_max_tf(b));
}
let bytes = serialize_bpl(&bpl);
let back = BlockPostingList::deserialize(&bytes).unwrap();
assert_eq!(collect_postings(&back), postings, "{codec} deserialize");
let back =
BlockPostingList::deserialize_zero_copy(OwnedBytes::new(bytes.clone())).unwrap();
assert_eq!(collect_postings(&back), postings, "{codec} zero-copy");
let mut a = rounded.iterator();
let mut b = back.iterator();
for target in (0..postings.last().unwrap().0 + 10).step_by(2_003) {
assert_eq!(a.seek(target), b.seek(target), "{codec} seek {target}");
assert_eq!(a.term_freq(), b.term_freq());
}
sizes.push((codec, bytes.len()));
}
let rounded_bytes = serialize_bpl(&rounded);
assert_eq!(
serialize_bpl(
&BlockPostingList::from_posting_list_with_codec(&list, PostingCodec::Rounded)
.unwrap()
),
rounded_bytes,
"Rounded must stay byte-identical"
);
assert!(matches!(rounded.stream[6], 0 | 8 | 16 | 32));
let size = |c: PostingCodec| sizes.iter().find(|(k, _)| *k == c).unwrap().1;
assert!(size(PostingCodec::Packed) < size(PostingCodec::Rounded));
assert!(size(PostingCodec::Pfor) < size(PostingCodec::Packed));
}
#[test]
fn mixed_codec_sources_concatenate() {
let a: Vec<(u32, u32)> = (0..300u32).map(|i| (i * 5, 1 + i % 9)).collect();
let b: Vec<(u32, u32)> = (0..300u32).map(|i| (i * 11 + 3, 2 + i % 5)).collect();
let list_a = {
let mut l = PostingList::new();
a.iter().for_each(|&(d, t)| l.push(d, t));
BlockPostingList::from_posting_list_with_codec(&l, PostingCodec::Pfor).unwrap()
};
let list_b = {
let mut l = PostingList::new();
b.iter().for_each(|&(d, t)| l.push(d, t));
BlockPostingList::from_posting_list_with_codec(&l, PostingCodec::Packed).unwrap()
};
let offset_b = a.last().unwrap().0 + 1;
let expected: Vec<(u32, u32)> = a
.iter()
.copied()
.chain(b.iter().map(|&(d, t)| (d + offset_b, t)))
.collect();
let merged = BlockPostingList::concatenate_blocks(&[
(list_a.clone(), 0),
(list_b.clone(), offset_b),
])
.unwrap();
assert_eq!(collect_postings(&merged), expected);
let bytes_a = serialize_bpl(&list_a);
let bytes_b = serialize_bpl(&list_b);
let mut out = Vec::new();
let (docs, written) = BlockPostingList::concatenate_streaming(
&[(bytes_a.as_slice(), 0), (bytes_b.as_slice(), offset_b)],
&mut out,
)
.unwrap();
assert_eq!(docs, 600);
assert_eq!(written, out.len());
let streamed = BlockPostingList::deserialize(&out).unwrap();
assert_eq!(collect_postings(&streamed), expected);
assert_eq!(streamed.block_codec(0), Some(PostingCodec::Pfor));
assert_eq!(
streamed.block_codec(streamed.num_blocks() - 1),
Some(PostingCodec::Packed)
);
}
#[test]
fn test_l0_entry_roundtrip() {
let docs: Vec<(u32, u32)> = (0..1000u32).map(|i| (i * 3, (i % 10) + 1)).collect();
let bpl = build_bpl(&docs);
let bytes = serialize_bpl(&bpl);
let bpl2 = BlockPostingList::deserialize(&bytes).unwrap();
assert_eq!(bpl.num_blocks(), bpl2.num_blocks());
for blk in 0..bpl.num_blocks() {
assert_eq!(
bpl.read_l0_entry(blk),
bpl2.read_l0_entry(blk),
"L0 entry mismatch at block {}",
blk
);
}
assert_eq!(bpl.l1_docs, bpl2.l1_docs);
}
#[test]
fn test_zero_copy_deserialize_matches() {
let docs: Vec<(u32, u32)> = (0..2000u32).map(|i| (i * 2, (i % 5) + 1)).collect();
let bpl = build_bpl(&docs);
let bytes = serialize_bpl(&bpl);
let copied = BlockPostingList::deserialize(&bytes).unwrap();
let zero_copy =
BlockPostingList::deserialize_zero_copy(OwnedBytes::new(bytes.clone())).unwrap();
assert_eq!(copied.l0_count, zero_copy.l0_count);
assert_eq!(copied.l1_docs, zero_copy.l1_docs);
assert_eq!(copied.doc_count, zero_copy.doc_count);
assert_eq!(copied.max_tf, zero_copy.max_tf);
let p1 = collect_postings(&copied);
let p2 = collect_postings(&zero_copy);
assert_eq!(p1, p2);
}
#[test]
fn test_l1_preserved_through_streaming_merge() {
let seg_a = build_bpl(&(0..1000u32).map(|i| (i * 2, 1)).collect::<Vec<_>>());
let seg_b = build_bpl(&(0..800u32).map(|i| (i * 3, 2)).collect::<Vec<_>>());
let seg_c = build_bpl(&(0..500u32).map(|i| (i * 5, 3)).collect::<Vec<_>>());
let bytes_a = serialize_bpl(&seg_a);
let bytes_b = serialize_bpl(&seg_b);
let bytes_c = serialize_bpl(&seg_c);
let sources: Vec<(&[u8], u32)> = vec![(&bytes_a, 0), (&bytes_b, 10000), (&bytes_c, 20000)];
let mut out = Vec::new();
BlockPostingList::concatenate_streaming(&sources, &mut out).unwrap();
let merged = BlockPostingList::deserialize(&out).unwrap();
let expected_l1_count = merged.num_blocks().div_ceil(L1_INTERVAL);
assert_eq!(merged.l1_docs.len(), expected_l1_count);
for (i, &l1_doc) in merged.l1_docs.iter().enumerate() {
let last_block_in_group = ((i + 1) * L1_INTERVAL - 1).min(merged.num_blocks() - 1);
let expected = merged.block_last_doc(last_block_in_group).unwrap();
assert_eq!(l1_doc, expected, "L1[{}] mismatch", i);
}
for blk in 0..merged.num_blocks() {
let first = merged.block_first_doc(blk).unwrap();
assert_eq!(merged.seek_block(first, 0), Some(blk));
}
}
#[test]
fn test_seek_block_single_block() {
let bpl = build_bpl(&[(0, 1), (10, 2), (20, 3)]);
assert_eq!(bpl.num_blocks(), 1);
assert_eq!(bpl.l1_docs.len(), 1);
assert_eq!(bpl.seek_block(0, 0), Some(0));
assert_eq!(bpl.seek_block(10, 0), Some(0));
assert_eq!(bpl.seek_block(20, 0), Some(0));
assert_eq!(bpl.seek_block(21, 0), None);
}
#[test]
fn test_footer_size() {
let docs: Vec<(u32, u32)> = (0..500u32).map(|i| (i * 2, 1)).collect();
let bpl = build_bpl(&docs);
let bytes = serialize_bpl(&bpl);
let expected = bpl.stream.len()
+ bpl.l0_count * L0_SIZE
+ bpl.l1_docs.len() * (L1_SIZE + 4)
+ FOOTER_V2_SIZE;
assert_eq!(bytes.len(), expected);
}
fn build_bpl_with_positions(postings: &[(u32, u32)]) -> BlockPostingList {
let mut list = PostingList::new();
for &(doc, tf) in postings {
list.push(doc, tf);
}
BlockPostingList::from_posting_list_with_positions(&list).unwrap()
}
fn expected_cursors(postings: &[(u32, u32)]) -> Vec<u64> {
let mut acc = 0u64;
postings
.iter()
.map(|&(_, tf)| {
let c = acc;
acc += tf as u64;
c
})
.collect()
}
fn iterator_cursors(bpl: &BlockPostingList) -> Vec<u64> {
let mut it = bpl.iterator();
let mut out = Vec::new();
while it.doc() != TERMINATED {
out.push(it.position_cursor());
it.advance();
}
out
}
#[test]
fn position_cursors_survive_serialization_and_seeks() {
let docs: Vec<(u32, u32)> = (0..700u32).map(|i| (i * 3, i % 5 + 1)).collect();
let bpl = build_bpl_with_positions(&docs);
assert!(bpl.has_position_cursors());
assert_eq!(
bpl.total_positions(),
docs.iter().map(|&(_, tf)| tf as u64).sum::<u64>()
);
assert_eq!(bpl.pos_cursor(0), Some(0));
assert_eq!(
bpl.pos_cursor(1),
Some(docs[..128].iter().map(|&(_, tf)| tf as u64).sum::<u64>())
);
assert_eq!(iterator_cursors(&bpl), expected_cursors(&docs));
let bytes = serialize_bpl(&bpl);
assert_eq!(
bytes.len(),
bpl.stream.len()
+ bpl.l0_count * (L0_SIZE + CURSOR_SIZE)
+ bpl.l1_docs.len() * (L1_SIZE + 4)
+ FOOTER_V2_SIZE
);
assert!(BlockPostingList::has_cursors_bytes(&bytes));
let decoded =
BlockPostingList::deserialize_zero_copy(OwnedBytes::new(bytes.clone())).unwrap();
assert_eq!(iterator_cursors(&decoded), expected_cursors(&docs));
assert_eq!(decoded.total_positions(), bpl.total_positions());
let mut it = decoded.iterator();
let expected = expected_cursors(&docs);
for (i, &(doc, _)) in docs.iter().enumerate().step_by(37) {
assert_eq!(it.seek(doc), doc);
assert_eq!(it.position_cursor(), expected[i], "cursor at doc {doc}");
}
let mut it = decoded.iterator();
assert_eq!(it.seek(docs[600].0 + 1), docs[601].0);
assert_eq!(it.position_cursor(), expected[601]);
let plain = build_bpl(&docs);
assert!(!plain.has_position_cursors());
assert_eq!(plain.pos_cursor(0), None);
assert_eq!(plain.total_positions(), 0);
}
#[test]
fn length_bounds_are_packed_per_block_and_survive_merges() {
let docs: Vec<(u32, u32)> = (0..300u32).map(|i| (i, i % 3 + 1)).collect();
let length_of = |doc: u32| 10 + (doc % 50) * 7;
let mut list = PostingList::new();
for &(doc, tf) in &docs {
list.push(doc, tf);
}
let bpl = BlockPostingList::from_posting_list_with(&list, true, Some(&length_of)).unwrap();
assert_eq!(bpl.min_len(), Some(10));
assert_eq!(bpl.block_bounds(0), Some((3, Some(10))));
assert_eq!(bpl.block_bounds(2), Some((3, Some(52))));
assert_eq!(bpl.block_max_tf(2), Some(3));
let bytes = serialize_bpl(&bpl);
let decoded = BlockPostingList::deserialize(&bytes).unwrap();
assert_eq!(decoded.min_len(), Some(10));
assert_eq!(decoded.block_bounds(2), Some((3, Some(52))));
assert_eq!(decoded.group_bounds(0), Some((3, 10)));
assert_eq!(decoded.group_bounds(2), Some((3, 10)));
assert_eq!(decoded.group_bounds(3), None);
assert_eq!(decoded.group_last_doc(1), Some(299));
assert_eq!(decoded.next_group_block(1), 3);
let plain = build_bpl(&docs);
assert_eq!(plain.min_len(), Some(1));
assert_eq!(plain.block_bounds(0), Some((3, Some(1))));
let mut out = Vec::new();
BlockPostingList::concatenate_streaming(&[(&bytes, 0), (&bytes, 1000)], &mut out).unwrap();
let merged = BlockPostingList::deserialize(&out).unwrap();
assert_eq!(merged.min_len(), Some(10));
assert_eq!(merged.block_bounds(2), Some((3, Some(52))));
assert_eq!(merged.block_bounds(3), Some((3, Some(10))));
assert_eq!(merged.block_max_tf(5), Some(3));
assert_eq!(merged.group_bounds(5), Some((3, 10)));
assert_eq!(merged.group_last_doc(5), Some(1299));
assert_eq!(merged.next_group_block(5), 6);
}
#[test]
fn legacy_footer_without_magic_still_deserializes() {
let docs: Vec<(u32, u32)> = (0..300u32).map(|i| (i * 2, 1 + i % 3)).collect();
let bpl = build_bpl(&docs);
let bytes = serialize_bpl(&bpl);
let legacy = bytes[..bytes.len() - (FOOTER_V2_SIZE - FOOTER_SIZE)].to_vec();
assert!(!BlockPostingList::has_cursors_bytes(&legacy));
let decoded = BlockPostingList::deserialize(&legacy).unwrap();
assert_eq!(collect_postings(&decoded), docs);
assert_eq!(decoded.max_tf(), 3);
assert!(!decoded.has_position_cursors());
assert_eq!(decoded.min_len(), None);
assert_eq!(decoded.group_bounds(0), None);
let mut out = Vec::new();
let (count, written) =
BlockPostingList::concatenate_streaming(&[(&legacy, 0), (&legacy, 1000)], &mut out)
.unwrap();
assert_eq!(count, 600);
assert_eq!(written, out.len());
let merged = BlockPostingList::deserialize(&out).unwrap();
assert_eq!(merged.doc_count(), 600);
assert!(!merged.has_position_cursors());
}
#[test]
fn streaming_merge_rebases_position_cursors() {
let a: Vec<(u32, u32)> = (0..200u32).map(|i| (i, i % 4 + 1)).collect();
let b: Vec<(u32, u32)> = (0..150u32).map(|i| (i * 2, 2)).collect();
let bytes_a = serialize_bpl(&build_bpl_with_positions(&a));
let bytes_b = serialize_bpl(&build_bpl_with_positions(&b));
let mut out = Vec::new();
let (count, written) =
BlockPostingList::concatenate_streaming(&[(&bytes_a, 0), (&bytes_b, 1000)], &mut out)
.unwrap();
assert_eq!(count, 350);
assert_eq!(written, out.len());
let merged = BlockPostingList::deserialize(&out).unwrap();
assert!(merged.has_position_cursors());
let all: Vec<(u32, u32)> = a
.iter()
.copied()
.chain(b.iter().map(|&(d, tf)| (d + 1000, tf)))
.collect();
assert_eq!(collect_postings(&merged), all);
assert_eq!(iterator_cursors(&merged), expected_cursors(&all));
assert_eq!(
merged.total_positions(),
all.iter().map(|&(_, tf)| tf as u64).sum::<u64>()
);
let reference = BlockPostingList::concatenate_blocks(&[
(build_bpl_with_positions(&a), 0),
(build_bpl_with_positions(&b), 1000),
])
.unwrap();
assert_eq!(iterator_cursors(&reference), expected_cursors(&all));
let plain = serialize_bpl(&build_bpl(&b));
assert!(
BlockPostingList::concatenate_streaming(
&[(&bytes_a, 0), (&plain, 1000)],
&mut Vec::new()
)
.is_err()
);
}
#[test]
fn test_seek_block_from_block_skips_earlier() {
let n = BLOCK_SIZE * 16;
let docs: Vec<(u32, u32)> = (0..n as u32).map(|i| (i * 3, 1)).collect();
let bpl = build_bpl(&docs);
let target_in_5 = bpl.block_first_doc(5).unwrap() + 1;
let result = bpl.seek_block(target_in_5, 8);
assert!(result.is_some());
assert!(result.unwrap() >= 8);
}
}