laurus 0.12.0

Unified search library for lexical, vector, and semantic retrieval
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! In-memory reader and iterator over the BlockSection of a `.dict` file.
//!
//! A [`BlockReader`] points to the bytes of a single block within the
//! BlockSection. It can perform an in-block lookup (linear front-coded
//! scan) or yield each `(term_bytes, TermInfo)` pair sequentially.
//!
//! [`BlockSectionIter`] walks the entire BlockSection, lazily decoding
//! one block at a time so that `iter()` does not need to materialise
//! the whole dictionary into memory.
//!
//! # Block layout (within `BlockSection`)
//!
//! ```text
//! [term_count:                varint (u16 bounded)]
//! [front_coded_terms_len:     varint]
//! [front_coded_terms_bytes:   u8 × front_coded_terms_len]
//! [FixedTermInfoBlock serialized]
//!   block_min_posting_offset:    u64 LE  (8 bytes)
//!   block_min_posting_length:    u64 LE  (8 bytes)
//!   block_min_doc_frequency:     u64 LE  (8 bytes)
//!   block_min_total_frequency:   u64 LE  (8 bytes)
//!   ref_max_score_factor:        f32 LE  (4 bytes)
//!   posting_offset_nbits:        u8
//!   posting_length_nbits:        u8
//!   doc_frequency_nbits:         u8
//!   total_frequency_nbits:       u8
//!   max_score_factor_size:       u8
//!   payload_len:                 u32 LE  (4 bytes)
//!   payload:                     u8 × payload_len
//! [BlockMaxData serialized]
//!   bm_offsets:                  u32 LE × (term_count + 1)
//!   bm_data_len:                 u32 LE  (4 bytes)
//!   bm_data:                     u8 × bm_data_len
//! ```

// Skeleton wiring: these `pub(super)` items are consumed by
// `builder` (Phase 6). Until then, `cargo check` flags some of them
// as dead code. Remove this allow once the integration is complete.
#![allow(dead_code)]

use crate::error::{LaurusError, Result};
use crate::lexical::index::structures::dictionary::TermInfo;

use super::block_max_data::BlockMaxData;
use super::front_coding::FrontCodingDecoder;
use super::term_info_block::FixedTermInfoBlock;

/// Reader for a single block parsed out of the BlockSection.
pub(super) struct BlockReader<'a> {
    /// Number of terms in this block (`1..=BLOCK_TERM_COUNT`).
    pub(super) block_term_count: u16,
    /// Front-coded term bytes section of the block (sub-slice of the
    /// BlockSection).
    pub(super) term_bytes: &'a [u8],
    /// Bit-packed fixed-size `TermInfo` portion (payload bytes copied
    /// from the BlockSection — see module-level note about
    /// zero-copy).
    pub(super) term_info_block: FixedTermInfoBlock,
    /// Variable-length `block_max` storage (offsets + bytes copied
    /// from the BlockSection).
    pub(super) block_max_data: BlockMaxData,
}

impl<'a> BlockReader<'a> {
    /// Parse a block starting at `cursor` within the BlockSection
    /// `bytes`. Returns the parsed reader and the cursor advanced past
    /// the block.
    pub(super) fn parse(bytes: &'a [u8], cursor: usize) -> Result<(Self, usize)> {
        let mut c = cursor;

        let term_count_u64 = read_varint(bytes, &mut c)?;
        let term_count = u16::try_from(term_count_u64).map_err(|_| {
            LaurusError::index(format!(
                "BlockReader: term_count {term_count_u64} exceeds u16::MAX"
            ))
        })?;
        if term_count == 0 {
            return Err(LaurusError::index("BlockReader: empty block"));
        }

        let fc_len = read_varint(bytes, &mut c)? as usize;
        if c + fc_len > bytes.len() {
            return Err(LaurusError::index(
                "BlockReader: front-coded section overruns BlockSection",
            ));
        }
        let term_bytes = &bytes[c..c + fc_len];
        c += fc_len;

        // FixedTermInfoBlock fixed header (36 bytes block_min + ref f32,
        // 5 bytes nbits + size, 4 bytes payload_len = 45 bytes).
        const FTIB_FIXED_HEADER_BYTES: usize = 8 + 8 + 8 + 8 + 4 + 5 + 4;
        if c + FTIB_FIXED_HEADER_BYTES > bytes.len() {
            return Err(LaurusError::index(
                "BlockReader: FixedTermInfoBlock header overruns BlockSection",
            ));
        }

        let block_min_posting_offset = read_u64_le(bytes, &mut c);
        let block_min_posting_length = read_u64_le(bytes, &mut c);
        let block_min_doc_frequency = read_u64_le(bytes, &mut c);
        let block_min_total_frequency = read_u64_le(bytes, &mut c);
        let ref_max_score_factor = read_f32_le(bytes, &mut c);
        let posting_offset_nbits = read_u8(bytes, &mut c);
        let posting_length_nbits = read_u8(bytes, &mut c);
        let doc_frequency_nbits = read_u8(bytes, &mut c);
        let total_frequency_nbits = read_u8(bytes, &mut c);
        let max_score_factor_size = read_u8(bytes, &mut c);
        let payload_len = read_u32_le(bytes, &mut c) as usize;

        if c + payload_len > bytes.len() {
            return Err(LaurusError::index(
                "BlockReader: FixedTermInfoBlock payload overruns BlockSection",
            ));
        }
        let payload = bytes[c..c + payload_len].to_vec();
        c += payload_len;

        let term_info_block = FixedTermInfoBlock {
            term_count,
            block_min_posting_offset,
            block_min_posting_length,
            block_min_doc_frequency,
            block_min_total_frequency,
            ref_max_score_factor,
            posting_offset_nbits,
            posting_length_nbits,
            doc_frequency_nbits,
            total_frequency_nbits,
            max_score_factor_size,
            payload,
        };

        // BlockMaxData: (term_count + 1) u32 offsets, then bm_data_len + bytes.
        let offsets_count = term_count as usize + 1;
        let offsets_bytes = offsets_count * 4;
        if c + offsets_bytes + 4 > bytes.len() {
            return Err(LaurusError::index(
                "BlockReader: BlockMaxData header overruns BlockSection",
            ));
        }
        let mut offsets = Vec::with_capacity(offsets_count);
        for _ in 0..offsets_count {
            offsets.push(read_u32_le(bytes, &mut c));
        }
        let bm_data_len = read_u32_le(bytes, &mut c) as usize;
        if c + bm_data_len > bytes.len() {
            return Err(LaurusError::index(
                "BlockReader: BlockMaxData data overruns BlockSection",
            ));
        }
        let bm_data = bytes[c..c + bm_data_len].to_vec();
        c += bm_data_len;

        let block_max_data = BlockMaxData {
            offsets,
            data: bm_data,
        };

        Ok((
            BlockReader {
                block_term_count: term_count,
                term_bytes,
                term_info_block,
                block_max_data,
            },
            c,
        ))
    }

    /// Look up `target` within this block. Returns `Some(TermInfo)` on
    /// hit, `None` if not present.
    ///
    /// Performs a linear scan over front-coded term bytes, comparing
    /// each decoded term to `target`. Stops early if a decoded term
    /// is greater than `target` (terms are sorted within a block).
    pub(super) fn lookup(&self, target: &[u8]) -> Option<TermInfo> {
        let mut decoder = FrontCodingDecoder::new(self.term_bytes, self.block_term_count as u32);
        let mut inner: usize = 0;
        while let Some(term) = decoder.next() {
            match term.cmp(target) {
                std::cmp::Ordering::Equal => {
                    return Some(self.materialise_term_info(inner));
                }
                std::cmp::Ordering::Greater => return None,
                std::cmp::Ordering::Less => {
                    inner += 1;
                }
            }
        }
        None
    }

    /// Iterate `(term_bytes_owned, TermInfo)` pairs in sorted order.
    pub(super) fn iter(&self) -> BlockReaderIter<'_, 'a> {
        BlockReaderIter {
            decoder: FrontCodingDecoder::new(self.term_bytes, self.block_term_count as u32),
            term_info_block: &self.term_info_block,
            block_max_data: &self.block_max_data,
            inner: 0,
        }
    }

    /// Build a [`TermInfo`] for the entry at `inner_offset` by
    /// combining the bit-packed fixed fields with the variable-length
    /// `block_max` array.
    fn materialise_term_info(&self, inner_offset: usize) -> TermInfo {
        let fti = self.term_info_block.decode_at(inner_offset);
        let block_max = self.block_max_data.get(inner_offset);
        TermInfo {
            posting_offset: fti.posting_offset,
            posting_length: fti.posting_length,
            doc_frequency: fti.doc_frequency,
            total_frequency: fti.total_frequency,
            max_score_factor: fti.max_score_factor,
            block_max,
        }
    }
}

/// Iterator yielded by [`BlockReader::iter`]. Yields fresh `Vec<u8>`
/// term bytes (copy of the decoder's reusable buffer) so the caller
/// can move the term independently of the iterator.
pub(super) struct BlockReaderIter<'r, 'a: 'r> {
    decoder: FrontCodingDecoder<'a>,
    term_info_block: &'r FixedTermInfoBlock,
    block_max_data: &'r BlockMaxData,
    inner: usize,
}

impl<'r, 'a: 'r> Iterator for BlockReaderIter<'r, 'a> {
    type Item = (Vec<u8>, TermInfo);

    fn next(&mut self) -> Option<Self::Item> {
        let term = self.decoder.next()?;
        let term_owned = term.to_vec();
        let fti = self.term_info_block.decode_at(self.inner);
        let block_max = self.block_max_data.get(self.inner);
        let info = TermInfo {
            posting_offset: fti.posting_offset,
            posting_length: fti.posting_length,
            doc_frequency: fti.doc_frequency,
            total_frequency: fti.total_frequency,
            max_score_factor: fti.max_score_factor,
            block_max,
        };
        self.inner += 1;
        Some((term_owned, info))
    }
}

/// Streaming iterator over the entire BlockSection, yielding
/// `(term_string, TermInfo)` pairs in sorted order.
///
/// Internally walks block-by-block using [`BlockReader`], decoding
/// front-coded term bytes lazily so memory usage stays at one block
/// + one decode buffer regardless of dictionary size.
pub(super) struct BlockSectionIter<'a> {
    /// BlockSection bytes (full).
    bytes: &'a [u8],
    /// Cursor into `bytes` for the next block to parse.
    cursor: usize,
    /// Number of blocks remaining to be parsed (excluding the current
    /// in-flight one).
    blocks_remaining: u32,
    /// Currently-active block reader (or `None` if exhausted / not yet
    /// loaded).
    current: Option<CurrentBlock<'a>>,
}

/// Owned per-block state held by [`BlockSectionIter`].
///
/// Holds a stateful [`FrontCodingDecoder`] alongside the parsed block
/// metadata so consecutive `next()` calls advance through the block in
/// `O(1)` per term instead of re-decoding the front-coded prefix from
/// the start of the block each time. Both `decoder` and the
/// `term_info_block` / `block_max_data` borrows live for `'a`, the
/// lifetime of the underlying BlockSection bytes — there is no
/// self-reference between fields.
struct CurrentBlock<'a> {
    block_term_count: u16,
    decoder: FrontCodingDecoder<'a>,
    term_info_block: FixedTermInfoBlock,
    block_max_data: BlockMaxData,
    inner: usize,
}

impl<'a> BlockSectionIter<'a> {
    /// Create a new iterator over `bytes` covering exactly `block_count`
    /// blocks.
    pub(super) fn new(bytes: &'a [u8], block_count: u32) -> Self {
        BlockSectionIter {
            bytes,
            cursor: 0,
            blocks_remaining: block_count,
            current: None,
        }
    }

    /// Advance into the next block, populating `self.current`. Returns
    /// `Ok(false)` when no further blocks remain.
    fn advance_block(&mut self) -> Result<bool> {
        if self.blocks_remaining == 0 {
            self.current = None;
            return Ok(false);
        }
        let (reader, next_cursor) = BlockReader::parse(self.bytes, self.cursor)?;
        self.cursor = next_cursor;
        self.blocks_remaining -= 1;

        let decoder = FrontCodingDecoder::new(reader.term_bytes, reader.block_term_count as u32);
        self.current = Some(CurrentBlock {
            block_term_count: reader.block_term_count,
            decoder,
            term_info_block: reader.term_info_block,
            block_max_data: reader.block_max_data,
            inner: 0,
        });
        Ok(true)
    }
}

impl<'a> Iterator for BlockSectionIter<'a> {
    type Item = (String, TermInfo);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.current.is_none() {
                match self.advance_block() {
                    Ok(true) => {}
                    Ok(false) => return None,
                    Err(_) => return None, // corruption: end iteration silently
                }
            }
            // SAFETY of unwrap: just set in advance_block.
            let cb = self.current.as_mut().unwrap();
            if cb.inner >= cb.block_term_count as usize {
                self.current = None;
                continue;
            }

            // Single decoder advance per call. Buffer is reused across
            // calls, so per-step cost is the front-coding decode
            // (≈ 5–10 ns) rather than a `O(N²)` full-block re-walk.
            let term_bytes = match cb.decoder.next() {
                Some(b) => b,
                None => {
                    // Decoder exhausted unexpectedly — treat as
                    // corruption and end iteration silently.
                    self.current = None;
                    return None;
                }
            };
            let term_string = String::from_utf8_lossy(term_bytes).into_owned();
            let fti = cb.term_info_block.decode_at(cb.inner);
            let block_max = cb.block_max_data.get(cb.inner);
            let info = TermInfo {
                posting_offset: fti.posting_offset,
                posting_length: fti.posting_length,
                doc_frequency: fti.doc_frequency,
                total_frequency: fti.total_frequency,
                max_score_factor: fti.max_score_factor,
                block_max,
            };
            cb.inner += 1;
            return Some((term_string, info));
        }
    }
}

// ---------- byte-level helpers ----------

/// Read an unsigned LEB128 varint at `*cursor`, advancing the cursor.
fn read_varint(bytes: &[u8], cursor: &mut usize) -> Result<u64> {
    let mut result: u64 = 0;
    let mut shift: u32 = 0;
    loop {
        if *cursor >= bytes.len() {
            return Err(LaurusError::index("BlockReader: truncated varint"));
        }
        let byte = bytes[*cursor];
        *cursor += 1;
        result |= u64::from(byte & 0x7F) << shift;
        if byte & 0x80 == 0 {
            return Ok(result);
        }
        shift += 7;
        if shift >= 64 {
            return Err(LaurusError::index("BlockReader: varint overflow"));
        }
    }
}

fn read_u64_le(bytes: &[u8], cursor: &mut usize) -> u64 {
    let v = u64::from_le_bytes(
        bytes[*cursor..*cursor + 8]
            .try_into()
            .expect("8-byte slice"),
    );
    *cursor += 8;
    v
}

fn read_u32_le(bytes: &[u8], cursor: &mut usize) -> u32 {
    let v = u32::from_le_bytes(
        bytes[*cursor..*cursor + 4]
            .try_into()
            .expect("4-byte slice"),
    );
    *cursor += 4;
    v
}

fn read_f32_le(bytes: &[u8], cursor: &mut usize) -> f32 {
    let v = f32::from_le_bytes(
        bytes[*cursor..*cursor + 4]
            .try_into()
            .expect("4-byte slice"),
    );
    *cursor += 4;
    v
}

fn read_u8(bytes: &[u8], cursor: &mut usize) -> u8 {
    let v = bytes[*cursor];
    *cursor += 1;
    v
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lexical::index::structures::dictionary::BlockMax;

    use super::super::front_coding::encode_block_terms;
    use super::super::term_info_block::FixedTermInfo;

    fn fti(po: u64, pl: u64, df: u64, tf: u64, msf: f32) -> FixedTermInfo {
        FixedTermInfo {
            posting_offset: po,
            posting_length: pl,
            doc_frequency: df,
            total_frequency: tf,
            max_score_factor: msf,
        }
    }

    fn sample_block_max(doc: u64, factor: f32) -> BlockMax {
        BlockMax {
            last_doc_id: doc,
            max_factor: factor,
        }
    }

    /// Encode one block's bytes (BlockHeader + TermBytes +
    /// FixedTermInfoBlock + BlockMaxData) into `out`. Used by tests
    /// to build a synthetic BlockSection without depending on the
    /// (Phase 6) builder.
    fn encode_block_into(
        out: &mut Vec<u8>,
        terms: &[&[u8]],
        infos: &[(FixedTermInfo, Vec<BlockMax>)],
    ) {
        assert_eq!(terms.len(), infos.len());
        let term_count = terms.len();
        // Term count varint.
        write_varint_into(out, term_count as u64);
        // Front-coded term bytes.
        let fc = encode_block_terms(terms);
        write_varint_into(out, fc.len() as u64);
        out.extend_from_slice(&fc);
        // FixedTermInfoBlock.
        let fixed: Vec<FixedTermInfo> = infos.iter().map(|(f, _)| *f).collect();
        let ftib = FixedTermInfoBlock::encode(&fixed);
        out.extend_from_slice(&ftib.block_min_posting_offset.to_le_bytes());
        out.extend_from_slice(&ftib.block_min_posting_length.to_le_bytes());
        out.extend_from_slice(&ftib.block_min_doc_frequency.to_le_bytes());
        out.extend_from_slice(&ftib.block_min_total_frequency.to_le_bytes());
        out.extend_from_slice(&ftib.ref_max_score_factor.to_le_bytes());
        out.push(ftib.posting_offset_nbits);
        out.push(ftib.posting_length_nbits);
        out.push(ftib.doc_frequency_nbits);
        out.push(ftib.total_frequency_nbits);
        out.push(ftib.max_score_factor_size);
        out.extend_from_slice(&(ftib.payload.len() as u32).to_le_bytes());
        out.extend_from_slice(&ftib.payload);
        // BlockMaxData.
        let per_term: Vec<Vec<BlockMax>> = infos.iter().map(|(_, bm)| bm.clone()).collect();
        let bmd = BlockMaxData::encode(&per_term);
        for off in &bmd.offsets {
            out.extend_from_slice(&off.to_le_bytes());
        }
        out.extend_from_slice(&(bmd.data.len() as u32).to_le_bytes());
        out.extend_from_slice(&bmd.data);
    }

    fn write_varint_into(buf: &mut Vec<u8>, mut value: u64) {
        while value >= 0x80 {
            buf.push(((value as u8) & 0x7F) | 0x80);
            value >>= 7;
        }
        buf.push(value as u8);
    }

    /// Build an in-memory BlockSection containing two blocks for the
    /// integration tests below. Returns the bytes plus the test
    /// fixtures so tests can verify lookups.
    fn build_two_block_section() -> (Vec<u8>, Vec<(Vec<u8>, TermInfo)>) {
        // Block 0: "alpha", "beta", "gamma" (sorted).
        // Block 1: "delta", "epsilon", "zeta" — note "delta" < "gamma"
        //          would violate the global sort order, so we use
        //          "kappa", "lambda", "mu" instead.
        let block0_terms: Vec<&[u8]> = vec![b"alpha", b"beta", b"gamma"];
        let block0_infos = vec![
            (
                fti(0, 100, 5, 50, 1.5),
                vec![sample_block_max(10, 1.5), sample_block_max(20, 2.5)],
            ),
            (fti(100, 80, 3, 40, 1.5), Vec::new()),
            (fti(180, 200, 8, 100, 2.0), vec![sample_block_max(30, 3.5)]),
        ];

        let block1_terms: Vec<&[u8]> = vec![b"kappa", b"lambda", b"mu"];
        let block1_infos = vec![
            (fti(380, 50, 1, 1, 0.5), Vec::new()),
            (fti(430, 70, 2, 5, 0.5), Vec::new()),
            (fti(500, 90, 4, 8, 0.5), vec![sample_block_max(99, 9.9)]),
        ];

        let mut section = Vec::new();
        encode_block_into(&mut section, &block0_terms, &block0_infos);
        encode_block_into(&mut section, &block1_terms, &block1_infos);

        let mut expected: Vec<(Vec<u8>, TermInfo)> = Vec::new();
        for (term, (f, bm)) in block0_terms.iter().zip(block0_infos.iter()) {
            expected.push((
                term.to_vec(),
                TermInfo {
                    posting_offset: f.posting_offset,
                    posting_length: f.posting_length,
                    doc_frequency: f.doc_frequency,
                    total_frequency: f.total_frequency,
                    max_score_factor: f.max_score_factor,
                    block_max: bm.clone(),
                },
            ));
        }
        for (term, (f, bm)) in block1_terms.iter().zip(block1_infos.iter()) {
            expected.push((
                term.to_vec(),
                TermInfo {
                    posting_offset: f.posting_offset,
                    posting_length: f.posting_length,
                    doc_frequency: f.doc_frequency,
                    total_frequency: f.total_frequency,
                    max_score_factor: f.max_score_factor,
                    block_max: bm.clone(),
                },
            ));
        }
        (section, expected)
    }

    #[test]
    fn parse_single_block_round_trip() {
        let terms: Vec<&[u8]> = vec![b"apple", b"banana", b"cherry"];
        let infos = vec![
            (fti(0, 50, 1, 10, 1.0), Vec::new()),
            (fti(50, 60, 2, 20, 1.0), vec![sample_block_max(5, 0.5)]),
            (fti(110, 70, 3, 30, 1.0), Vec::new()),
        ];
        let mut section = Vec::new();
        encode_block_into(&mut section, &terms, &infos);

        let (reader, next_cursor) = BlockReader::parse(&section, 0).unwrap();
        assert_eq!(reader.block_term_count, 3);
        assert_eq!(next_cursor, section.len());

        // Lookup hits.
        let info = reader.lookup(b"banana").unwrap();
        assert_eq!(info.posting_offset, 50);
        assert_eq!(info.block_max.len(), 1);
        assert_eq!(info.block_max[0].last_doc_id, 5);

        // Lookup miss (not in block, alphabetically before).
        assert!(reader.lookup(b"aardvark").is_none());
        // Lookup miss (alphabetically between).
        assert!(reader.lookup(b"blueberry").is_none());
        // Lookup miss (alphabetically after).
        assert!(reader.lookup(b"date").is_none());
    }

    #[test]
    fn block_reader_iter_yields_in_order() {
        let terms: Vec<&[u8]> = vec![b"alpha", b"beta", b"gamma"];
        let infos = vec![
            (fti(0, 100, 5, 50, 1.5), Vec::new()),
            (fti(100, 80, 3, 40, 1.5), Vec::new()),
            (fti(180, 200, 8, 100, 2.0), Vec::new()),
        ];
        let mut section = Vec::new();
        encode_block_into(&mut section, &terms, &infos);

        let (reader, _) = BlockReader::parse(&section, 0).unwrap();
        let collected: Vec<(Vec<u8>, TermInfo)> = reader.iter().collect();
        assert_eq!(collected.len(), 3);
        assert_eq!(collected[0].0, b"alpha");
        assert_eq!(collected[1].0, b"beta");
        assert_eq!(collected[2].0, b"gamma");
    }

    #[test]
    fn block_section_iter_walks_two_blocks() {
        let (section, expected) = build_two_block_section();
        let iter = BlockSectionIter::new(&section, 2);
        let collected: Vec<(String, TermInfo)> = iter.collect();
        assert_eq!(collected.len(), expected.len());
        for ((got_term, got_info), (exp_term, exp_info)) in collected.iter().zip(expected.iter()) {
            assert_eq!(got_term.as_bytes(), exp_term.as_slice());
            assert_eq!(got_info.posting_offset, exp_info.posting_offset);
            assert_eq!(got_info.posting_length, exp_info.posting_length);
            assert_eq!(got_info.doc_frequency, exp_info.doc_frequency);
            assert_eq!(got_info.total_frequency, exp_info.total_frequency);
            assert_eq!(
                got_info.max_score_factor.to_bits(),
                exp_info.max_score_factor.to_bits()
            );
            assert_eq!(got_info.block_max.len(), exp_info.block_max.len());
        }
    }

    #[test]
    fn parse_rejects_truncated_section() {
        let terms: Vec<&[u8]> = vec![b"alpha", b"beta"];
        let infos = vec![
            (fti(0, 50, 1, 10, 1.0), Vec::new()),
            (fti(50, 60, 2, 20, 1.0), Vec::new()),
        ];
        let mut section = Vec::new();
        encode_block_into(&mut section, &terms, &infos);
        let truncated = &section[..section.len() - 5];
        assert!(BlockReader::parse(truncated, 0).is_err());
    }

    #[test]
    fn empty_section_iter() {
        let iter = BlockSectionIter::new(&[], 0);
        let collected: Vec<(String, TermInfo)> = iter.collect();
        assert!(collected.is_empty());
    }

    #[test]
    fn varied_term_lengths_in_block() {
        // A block with a short and a 100-byte term in sorted order,
        // exercising the front-coding decoder buffer growth path.
        // "long_aaaa..." sorts before "short" (l < s).
        let mut long_term = b"long_".to_vec();
        long_term.extend(std::iter::repeat_n(b'a', 100));
        let terms: Vec<&[u8]> = vec![long_term.as_slice(), b"short"];
        let infos = vec![
            (fti(0, 50, 1, 1, 0.0), Vec::new()),
            (fti(50, 60, 2, 2, 0.0), Vec::new()),
        ];
        let mut section = Vec::new();
        encode_block_into(&mut section, &terms, &infos);
        let (reader, _) = BlockReader::parse(&section, 0).unwrap();
        assert!(reader.lookup(&long_term).is_some());
        assert!(reader.lookup(b"short").is_some());
        assert!(reader.lookup(b"medium").is_none());
    }

    #[test]
    fn lookup_miss_after_last_block_term() {
        // Target alphabetically greater than every term in the block —
        // must return None without panicking.
        let terms: Vec<&[u8]> = vec![b"alpha", b"beta"];
        let infos = vec![
            (fti(0, 50, 1, 1, 0.0), Vec::new()),
            (fti(50, 60, 2, 2, 0.0), Vec::new()),
        ];
        let mut section = Vec::new();
        encode_block_into(&mut section, &terms, &infos);
        let (reader, _) = BlockReader::parse(&section, 0).unwrap();
        assert!(reader.lookup(b"zulu").is_none());
    }
}