nodedb-fts 0.6.1

Shared full-text search engine for NodeDB — inverted index, BM25 scoring, analyzers, fuzzy matching
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
// SPDX-License-Identifier: Apache-2.0

//! 128-document posting block with compressed storage.
//!
//! Each `PostingBlock` holds up to 128 postings. Data is stored as:
//! - Delta-encoded, bitpacked surrogate IDs (raw `u32` on disk; typed
//!   `Surrogate` in memory)
//! - Bitpacked term frequencies (u32)
//! - SmallFloat fieldnorm bytes (1 byte per doc)
//! - Per-doc position lists (variable length)
//!
//! This is the storage unit for BMW (Block-Max WAND): each block carries
//! `block_max_tf` and `block_min_fieldnorm` for skip-level pruning.
//!
//! `Surrogate` is the global, monotonically-allocated row identity shared
//! across every engine. The on-disk encoding stays raw `u32` for delta
//! compression and SIMD unpack; in-memory blocks expose typed `Surrogate`
//! values so cross-engine bitmap intersections stay type-safe.

use std::mem::size_of;

use nodedb_types::Surrogate;
use nodedb_types::decode_bounds::checked_decode_capacity;

use crate::codec::{bitpack, delta, smallfloat};

/// Maximum number of documents per posting block.
pub const BLOCK_SIZE: usize = 128;

/// A compact posting entry keyed on a `Surrogate` row identity.
#[derive(Debug, Clone)]
pub struct CompactPosting {
    pub doc_id: Surrogate,
    pub term_freq: u32,
    pub fieldnorm: u8,
    pub positions: Vec<u32>,
}

/// A block of up to 128 postings in compressed form.
///
/// Provides both in-memory access (via `decode`) and serialized byte
/// representation (via `to_bytes` / `from_bytes`).
#[derive(Debug, Clone)]
pub struct PostingBlock {
    /// Sorted surrogate IDs in this block.
    pub doc_ids: Vec<Surrogate>,
    /// Term frequencies, parallel to `doc_ids`.
    pub term_freqs: Vec<u32>,
    /// SmallFloat-encoded fieldnorms, parallel to `doc_ids`.
    pub fieldnorms: Vec<u8>,
    /// Per-document position lists, parallel to `doc_ids`.
    pub positions: Vec<Vec<u32>>,
}

/// Block-level metadata for BMW skip index.
#[derive(Debug, Clone, Copy)]
pub struct BlockMeta {
    /// Last (largest) surrogate ID in this block.
    pub last_doc_id: Surrogate,
    /// Number of documents in this block.
    pub doc_count: u16,
    /// Maximum term frequency in this block (for upper bound scoring).
    pub block_max_tf: u32,
    /// Minimum fieldnorm in this block (shortest doc — highest BM25 potential).
    pub block_min_fieldnorm: u8,
}

impl PostingBlock {
    /// Create a block from a slice of compact postings.
    ///
    /// Postings MUST be sorted by doc_id. Panics in debug if unsorted.
    /// Truncates to `BLOCK_SIZE` if more are provided.
    pub fn from_postings(postings: &[CompactPosting]) -> Self {
        let n = postings.len().min(BLOCK_SIZE);
        debug_assert!(
            postings[..n].windows(2).all(|w| w[0].doc_id <= w[1].doc_id),
            "postings must be sorted by surrogate"
        );

        Self {
            doc_ids: postings[..n].iter().map(|p| p.doc_id).collect(),
            term_freqs: postings[..n].iter().map(|p| p.term_freq).collect(),
            fieldnorms: postings[..n].iter().map(|p| p.fieldnorm).collect(),
            positions: postings[..n].iter().map(|p| p.positions.clone()).collect(),
        }
    }

    /// Number of documents in this block.
    pub fn len(&self) -> usize {
        self.doc_ids.len()
    }

    /// Whether the block is empty.
    pub fn is_empty(&self) -> bool {
        self.doc_ids.is_empty()
    }

    /// Compute block metadata for BMW skip index.
    pub fn meta(&self) -> BlockMeta {
        BlockMeta {
            last_doc_id: self.doc_ids.last().copied().unwrap_or(Surrogate::ZERO),
            doc_count: self.doc_ids.len() as u16,
            block_max_tf: self.term_freqs.iter().copied().max().unwrap_or(0),
            block_min_fieldnorm: self.fieldnorms.iter().copied().min().unwrap_or(0),
        }
    }

    /// Serialize the block to bytes.
    ///
    /// Layout:
    /// ```text
    /// [doc_count: u16 LE]
    /// [packed_doc_id_deltas: len u32 LE + bytes]
    /// [packed_term_freqs: len u32 LE + bytes]
    /// [fieldnorms: doc_count bytes]
    /// [position_data: for each doc, count u16 LE + packed positions]
    /// ```
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::new();

        // Doc count.
        buf.extend_from_slice(&(self.doc_ids.len() as u16).to_le_bytes());

        // Delta-encoded, bitpacked surrogate IDs (raw u32 on disk).
        let raw_ids: Vec<u32> = self.doc_ids.iter().map(|s| s.0).collect();
        let deltas = delta::encode(&raw_ids);
        let packed_ids = bitpack::pack(&deltas);
        buf.extend_from_slice(&(packed_ids.len() as u32).to_le_bytes());
        buf.extend_from_slice(&packed_ids);

        // Bitpacked term frequencies.
        let packed_freqs = bitpack::pack(&self.term_freqs);
        buf.extend_from_slice(&(packed_freqs.len() as u32).to_le_bytes());
        buf.extend_from_slice(&packed_freqs);

        // Fieldnorms (raw bytes, 1 per doc).
        buf.extend_from_slice(&self.fieldnorms);

        // Position data: for each doc, [count: u16 LE][packed positions].
        for positions in &self.positions {
            buf.extend_from_slice(&(positions.len() as u16).to_le_bytes());
            if !positions.is_empty() {
                let packed_pos = bitpack::pack(positions);
                buf.extend_from_slice(&(packed_pos.len() as u16).to_le_bytes());
                buf.extend_from_slice(&packed_pos);
            } else {
                buf.extend_from_slice(&0u16.to_le_bytes());
            }
        }

        buf
    }

    /// Deserialize a block from bytes. Returns `None` if malformed.
    pub fn from_bytes(buf: &[u8]) -> Option<Self> {
        let mut pos = 0;

        // Doc count.
        if buf.len() < 2 {
            return None;
        }
        let doc_count = u16::from_le_bytes([buf[pos], buf[pos + 1]]) as usize;
        pos += 2;

        if doc_count > BLOCK_SIZE {
            return None;
        }

        if doc_count == 0 {
            return Some(Self {
                doc_ids: Vec::new(),
                term_freqs: Vec::new(),
                fieldnorms: Vec::new(),
                positions: Vec::new(),
            });
        }

        // Packed doc ID deltas.
        if pos + 4 > buf.len() {
            return None;
        }
        let ids_len =
            u32::from_le_bytes([buf[pos], buf[pos + 1], buf[pos + 2], buf[pos + 3]]) as usize;
        pos += 4;
        if pos + ids_len > buf.len() {
            return None;
        }
        let packed_deltas = &buf[pos..pos + ids_len];
        let deltas = bitpack::unpack(packed_deltas);
        let raw_ids = delta::decode(&deltas);
        let doc_ids: Vec<Surrogate> = raw_ids.into_iter().map(Surrogate).collect();
        pos += ids_len;

        // Packed term frequencies.
        if pos + 4 > buf.len() {
            return None;
        }
        let freqs_len =
            u32::from_le_bytes([buf[pos], buf[pos + 1], buf[pos + 2], buf[pos + 3]]) as usize;
        pos += 4;
        if pos + freqs_len > buf.len() {
            return None;
        }
        let term_freqs = bitpack::unpack(&buf[pos..pos + freqs_len]);
        pos += freqs_len;

        // Fieldnorms.
        if pos + doc_count > buf.len() {
            return None;
        }
        let fieldnorms = buf[pos..pos + doc_count].to_vec();
        pos += doc_count;

        // Position data.
        let positions_capacity = checked_decode_capacity(
            doc_count,
            size_of::<Vec<u32>>(),
            buf.len().saturating_sub(pos),
            4,
            BLOCK_SIZE,
            BLOCK_SIZE * size_of::<Vec<u32>>(),
        )?;
        let mut positions = Vec::with_capacity(positions_capacity);
        for _ in 0..doc_count {
            if pos + 2 > buf.len() {
                return None;
            }
            let num_positions = u16::from_le_bytes([buf[pos], buf[pos + 1]]) as usize;
            pos += 2;

            if num_positions == 0 {
                if pos + 2 > buf.len() {
                    return None;
                }
                pos += 2; // Skip packed_len (0).
                positions.push(Vec::new());
            } else {
                if pos + 2 > buf.len() {
                    return None;
                }
                let packed_pos_len = u16::from_le_bytes([buf[pos], buf[pos + 1]]) as usize;
                pos += 2;
                if pos + packed_pos_len > buf.len() {
                    return None;
                }
                let doc_positions = bitpack::unpack(&buf[pos..pos + packed_pos_len]);
                pos += packed_pos_len;
                positions.push(doc_positions);
            }
        }

        if doc_ids.len() != doc_count || term_freqs.len() != doc_count {
            return None;
        }

        Some(Self {
            doc_ids,
            term_freqs,
            fieldnorms,
            positions,
        })
    }
}

/// Split a list of compact postings into 128-doc blocks.
pub fn into_blocks(mut postings: Vec<CompactPosting>) -> Vec<PostingBlock> {
    postings.sort_by_key(|p| p.doc_id);
    postings
        .chunks(BLOCK_SIZE)
        .map(PostingBlock::from_postings)
        .collect()
}

/// Encode a document length as a SmallFloat fieldnorm byte.
pub fn encode_fieldnorm(doc_length: u32) -> u8 {
    smallfloat::encode(doc_length)
}

/// Decode a SmallFloat fieldnorm byte back to approximate document length.
pub fn decode_fieldnorm(byte: u8) -> u32 {
    smallfloat::decode(byte)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_postings(n: usize) -> Vec<CompactPosting> {
        (0..n)
            .map(|i| CompactPosting {
                doc_id: Surrogate((i * 3) as u32),
                term_freq: (i % 5 + 1) as u32,
                fieldnorm: smallfloat::encode((i * 10 + 20) as u32),
                positions: vec![i as u32, (i + 5) as u32],
            })
            .collect()
    }

    #[test]
    fn block_roundtrip() {
        let postings = make_postings(50);
        let block = PostingBlock::from_postings(&postings);
        assert_eq!(block.len(), 50);

        let bytes = block.to_bytes();
        let restored = PostingBlock::from_bytes(&bytes).unwrap();

        assert_eq!(restored.doc_ids, block.doc_ids);
        assert_eq!(restored.term_freqs, block.term_freqs);
        assert_eq!(restored.fieldnorms, block.fieldnorms);
        assert_eq!(restored.positions, block.positions);
    }

    #[test]
    fn block_128_roundtrip() {
        let postings = make_postings(128);
        let block = PostingBlock::from_postings(&postings);
        assert_eq!(block.len(), 128);

        let bytes = block.to_bytes();
        let restored = PostingBlock::from_bytes(&bytes).unwrap();
        assert_eq!(restored.doc_ids, block.doc_ids);
    }

    #[test]
    fn block_meta() {
        let postings = make_postings(10);
        let block = PostingBlock::from_postings(&postings);
        let meta = block.meta();

        assert_eq!(meta.doc_count, 10);
        assert_eq!(meta.last_doc_id, Surrogate(27)); // (9 * 3)
        assert_eq!(meta.block_max_tf, 5); // max of (i%5+1) for i=0..9
    }

    #[test]
    fn into_blocks_splits() {
        let postings = make_postings(300);
        let blocks = into_blocks(postings);
        assert_eq!(blocks.len(), 3); // 128 + 128 + 44
        assert_eq!(blocks[0].len(), 128);
        assert_eq!(blocks[1].len(), 128);
        assert_eq!(blocks[2].len(), 44);
    }

    #[test]
    fn rejects_huge_doc_count_with_tiny_payload_before_allocation() {
        assert!(PostingBlock::from_bytes(&u16::MAX.to_le_bytes()).is_none());
    }

    #[test]
    fn empty_block() {
        let block = PostingBlock::from_postings(&[]);
        assert!(block.is_empty());
        let bytes = block.to_bytes();
        let restored = PostingBlock::from_bytes(&bytes).unwrap();
        assert!(restored.is_empty());
    }

    #[test]
    fn fieldnorm_encode_decode() {
        for len in [0, 1, 5, 8, 10, 20, 50, 100, 500, 1000, 10_000] {
            let encoded = encode_fieldnorm(len);
            let decoded = decode_fieldnorm(encoded);
            if len <= 8 {
                assert_eq!(decoded, len, "exact for small: len={len}");
            } else {
                assert!(decoded <= len, "decoded {decoded} > original {len}");
                let error = (len - decoded) as f64 / len as f64;
                assert!(
                    error < 0.25,
                    "len={len}, decoded={decoded}, error={error:.4}"
                );
            }
        }
    }

    #[test]
    fn compression_ratio() {
        // 128 postings with small deltas should compress well.
        let postings: Vec<CompactPosting> = (0..128)
            .map(|i| CompactPosting {
                doc_id: Surrogate(i),
                term_freq: 1,
                fieldnorm: smallfloat::encode(100),
                positions: vec![0],
            })
            .collect();
        let block = PostingBlock::from_postings(&postings);
        let bytes = block.to_bytes();

        // Uncompressed: 128 * (4 + 4 + 1 + ~8) = ~2176 bytes.
        // Compressed should be significantly smaller.
        // Position data is the bulk: 128 docs × (2 pos_count + 2 packed_len + 3 header + 1 data).
        // The IDs and freqs should be very compact (1-bit deltas, 1-bit freqs).
        // Total should be well under uncompressed 128*(4+4+1+8) = 2176 bytes.
        assert!(
            bytes.len() < 1500,
            "compressed block should be < 1500 bytes, got {}",
            bytes.len()
        );
    }
}