a3s-vec 0.1.8

Native Rust in-process vector database with zvec-compatible capabilities
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
//! Immutable, revisioned full-text postings with exact BM25 statistics.

mod document_lengths;
mod expression;
mod posting_list;
mod query_context;
mod term_dictionary;
mod trigram;

#[cfg(test)]
mod tests;

use super::ordinals::{OrdinalSet, OrdinalTable};
use crate::doc::DocumentMap;
use crate::error::{Error, Result};
use crate::query::{FtsDefaultOperator, SearchQuery};
use crate::schema::{CollectionSchema, FieldSchema, IndexParams};
use crate::stats::IndexStat;
use crate::text::{
    bm25_term_score, parse_fts_query, text_value, FtsTermMatcher, ParsedFtsQuery, Tokenizer,
};
use crate::types::IndexType;
use document_lengths::DocumentLengths;
use posting_list::PostingList;
use query_context::IndexedEvalContext;
use roaring::RoaringTreemap;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;
use term_dictionary::TermDictionary;
use trigram::{char_trigrams, TrigramTermIndex};

const DENSE_SCORE_MIN_VISITS: usize = 4_096;
const DENSE_SCORE_MAX_SPAN_FACTOR: usize = 8;

#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub(super) struct FtsIndexRegistry {
    source_revision: u64,
    indexes: BTreeMap<String, FtsIndex>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct FtsIndex {
    #[serde(with = "super::cache::index_params_serde")]
    params: IndexParams,
    tokenizer: Tokenizer,
    postings: TermDictionary,
    /// Character-trigram → term map used to prune wildcard/fuzzy expansion.
    trigrams: TrigramTermIndex,
    document_lengths: DocumentLengths,
    total_tokens: u64,
}

/// Co-locates both BM25 inputs so a posting hit does not need a second tree
/// lookup. A layout test guards the unchanged aligned B-tree entry footprint.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
struct PostingEntry {
    frequency: u32,
    document_length: u32,
}

impl FtsIndexRegistry {
    pub(super) fn build(
        schema: &CollectionSchema,
        docs: &DocumentMap,
        source_revision: u64,
        ordinals: &OrdinalTable,
    ) -> Result<Self> {
        let configured: Vec<_> = schema
            .fields
            .iter()
            .filter_map(|field| {
                field
                    .index_params
                    .as_ref()
                    .filter(|params| params.index_type == IndexType::Fts)
                    .map(|params| (field, params))
            })
            .collect();
        if configured.is_empty() {
            return Ok(Self {
                source_revision,
                ..Self::default()
            });
        }

        let mut indexes = BTreeMap::new();
        for (field, params) in configured {
            indexes.insert(
                field.name.clone(),
                FtsIndex::build(&field.name, params, docs, ordinals)?,
            );
        }
        Ok(Self {
            source_revision,
            indexes,
        })
    }

    pub(super) fn rebuild_field(
        &self,
        field: &FieldSchema,
        docs: &DocumentMap,
        source_revision: u64,
        ordinals: &OrdinalTable,
    ) -> Result<Self> {
        let params = field
            .index_params
            .as_ref()
            .filter(|params| params.index_type == IndexType::Fts)
            .ok_or_else(|| Error::internal("FTS rebuild requires an FTS index field"))?;
        let mut next = self.clone();
        next.source_revision = source_revision;
        next.indexes.insert(
            field.name.clone(),
            FtsIndex::build(&field.name, params, docs, ordinals)?,
        );
        Ok(next)
    }

    pub(super) fn apply_document_changes(
        &self,
        schema: &CollectionSchema,
        previous_docs: &DocumentMap,
        docs: &DocumentMap,
        source_revision: u64,
        changed_ids: &BTreeSet<String>,
        ordinals: &OrdinalTable,
    ) -> Result<Self> {
        if !self.matches_schema(schema) {
            return Self::build(schema, docs, source_revision, ordinals);
        }
        if previous_docs.is_empty() && !docs.is_empty() && changed_ids.len() == docs.len() {
            return Self::build(schema, docs, source_revision, ordinals);
        }
        if self.indexes.is_empty() {
            return Ok(Self {
                source_revision,
                ..Self::default()
            });
        }

        let mut indexes = BTreeMap::new();
        for field in &schema.fields {
            let Some(params) = field
                .index_params
                .as_ref()
                .filter(|params| params.index_type == IndexType::Fts)
            else {
                continue;
            };
            let Some(current) = self
                .indexes
                .get(&field.name)
                .filter(|index| index.params == *params)
            else {
                return Self::build(schema, docs, source_revision, ordinals);
            };
            let mut next = current.clone();
            for id in changed_ids {
                let previous = previous_docs
                    .get(id)
                    .and_then(|doc| text_value(doc, &field.name));
                let current = docs.get(id).and_then(|doc| text_value(doc, &field.name));
                if previous == current {
                    continue;
                }
                let ordinal = ordinals.ordinal(id).ok_or_else(|| {
                    Error::internal(format!("FTS ordinal is missing for document '{id}'"))
                })?;
                if let Some(text) = previous {
                    next.remove_text(text, ordinal)?;
                }
                if let Some(text) = current {
                    next.insert_text(text, ordinal)?;
                }
            }
            next.document_lengths.finish_changes()?;
            next.postings.finish_changes();
            indexes.insert(field.name.clone(), next);
        }
        Ok(Self {
            source_revision,
            indexes,
        })
    }

    pub(super) fn search(
        &self,
        source_revision: u64,
        query: &SearchQuery,
        candidates: Option<&OrdinalSet>,
        docs: &DocumentMap,
        ordinals: &OrdinalTable,
    ) -> Result<Option<Vec<(u64, f64)>>> {
        if self.source_revision != source_revision {
            return Ok(None);
        }
        let Some(index) = self.indexes.get(&query.field_name) else {
            return Ok(None);
        };
        let mut parsed = parse_fts_query(query, &index.tokenizer)?;
        index.expand_parsed_query(&mut parsed);
        let allowed = candidates.map(OrdinalSet::bitmap);
        let scores = if let Some((terms, operator)) = parsed.simple() {
            index.search(terms, allowed, operator)?
        } else {
            if allowed.is_none() && index.prefers_scan_for_expression(&parsed) {
                return Ok(None);
            }
            index.search_expression(&parsed, allowed, docs, ordinals, &query.field_name)?
        };
        Ok(Some(scores))
    }

    pub(super) fn stats(&self) -> Vec<IndexStat> {
        self.indexes
            .iter()
            .map(|(name, index)| IndexStat {
                name: name.clone(),
                index_type: IndexType::Fts,
                completeness: 1.0,
                source_revision: self.source_revision,
                document_count: u64::try_from(index.document_lengths.len()).unwrap_or(u64::MAX),
                estimated_payload_bytes: None,
                state: "ready".into(),
            })
            .collect()
    }

    pub(super) fn is_empty(&self) -> bool {
        self.indexes.is_empty()
    }

    pub(super) fn validates(
        &self,
        schema: &CollectionSchema,
        docs: &DocumentMap,
        source_revision: u64,
        ordinals: &OrdinalTable,
    ) -> bool {
        self.source_revision == source_revision
            && self.matches_schema(schema)
            && self
                .indexes
                .iter()
                .all(|(field_name, index)| index.validates(field_name, docs, ordinals))
    }

    fn matches_schema(&self, schema: &CollectionSchema) -> bool {
        let configured: Vec<_> = schema
            .fields
            .iter()
            .filter_map(|field| {
                field
                    .index_params
                    .as_ref()
                    .filter(|params| params.index_type == IndexType::Fts)
                    .map(|params| (&field.name, params))
            })
            .collect();
        configured.len() == self.indexes.len()
            && configured.into_iter().all(|(name, params)| {
                self.indexes
                    .get(name)
                    .is_some_and(|index| index.params == *params)
            })
    }
}

impl FtsIndex {
    fn build(
        field_name: &str,
        params: &IndexParams,
        docs: &DocumentMap,
        ordinals: &OrdinalTable,
    ) -> Result<Self> {
        let tokenizer = Tokenizer::from_index_params(Some(params))?;
        let mut postings = BTreeMap::<String, BTreeMap<u64, PostingEntry>>::new();
        let mut document_lengths = BTreeMap::<u64, u32>::new();
        let mut total_tokens = 0_u64;
        for (id, doc) in docs {
            let Some(text) = text_value(doc, field_name) else {
                continue;
            };
            let ordinal = ordinals.ordinal(id).ok_or_else(|| {
                Error::internal(format!("FTS ordinal is missing for document '{id}'"))
            })?;
            let tokens = tokenizer.tokenize(text);
            let length = u32::try_from(tokens.len())
                .map_err(|_| Error::resource_exhausted("FTS document has too many tokens"))?;
            if document_lengths.insert(ordinal, length).is_some() {
                return Err(Error::internal(format!(
                    "FTS document ordinal {ordinal} is already indexed"
                )));
            }
            total_tokens = total_tokens
                .checked_add(u64::from(length))
                .ok_or_else(|| Error::resource_exhausted("FTS token count overflow"))?;
            let mut frequencies = BTreeMap::<String, u32>::new();
            for token in tokens {
                let frequency = frequencies.entry(token).or_default();
                *frequency = frequency
                    .checked_add(1)
                    .ok_or_else(|| Error::resource_exhausted("FTS term frequency overflow"))?;
            }
            for (term, frequency) in frequencies {
                postings.entry(term).or_default().insert(
                    ordinal,
                    PostingEntry {
                        frequency,
                        document_length: length,
                    },
                );
            }
        }
        let trigrams = TrigramTermIndex::from_terms(postings.keys().map(String::as_str));
        Ok(Self {
            params: params.clone(),
            tokenizer,
            postings: TermDictionary::from_sorted_entries(postings.into_iter().map(
                |(term, posting)| (term, Arc::new(PostingList::from_sorted_entries(posting))),
            )),
            trigrams,
            document_lengths: DocumentLengths::from_sorted_entries(document_lengths)?,
            total_tokens,
        })
    }

    fn insert_text(&mut self, text: &str, ordinal: u64) -> Result<()> {
        let tokens = self.tokenizer.tokenize(text);
        let length = u32::try_from(tokens.len())
            .map_err(|_| Error::resource_exhausted("FTS document has too many tokens"))?;
        if self.document_lengths.contains_key(ordinal) {
            return Err(Error::internal(format!(
                "FTS document ordinal {ordinal} is already indexed"
            )));
        }
        self.total_tokens = self
            .total_tokens
            .checked_add(u64::from(length))
            .ok_or_else(|| Error::resource_exhausted("FTS token count overflow"))?;
        self.document_lengths.insert(ordinal, length)?;

        let mut frequencies = BTreeMap::<String, u32>::new();
        for token in tokens {
            let frequency = frequencies.entry(token).or_default();
            *frequency = frequency
                .checked_add(1)
                .ok_or_else(|| Error::resource_exhausted("FTS term frequency overflow"))?;
        }
        for (term, frequency) in frequencies {
            let entry = PostingEntry {
                frequency,
                document_length: length,
            };
            if let Some(posting) = self.postings.get(&term).cloned() {
                let mut posting = posting;
                Arc::make_mut(&mut posting).insert(ordinal, entry)?;
                self.postings.insert(term, posting)?;
            } else {
                self.trigrams.insert_term(&term);
                self.postings
                    .insert(term, Arc::new(PostingList::single(ordinal, entry)))?;
            }
        }
        Ok(())
    }

    fn remove_text(&mut self, text: &str, ordinal: u64) -> Result<()> {
        let length = self.document_lengths.remove(ordinal).ok_or_else(|| {
            Error::internal(format!("FTS document ordinal {ordinal} is not indexed"))
        })?;
        self.total_tokens = self
            .total_tokens
            .checked_sub(u64::from(length))
            .ok_or_else(|| Error::internal("FTS token count underflow"))?;

        let terms: BTreeSet<String> = self.tokenizer.tokenize(text).into_iter().collect();
        for term in terms {
            if let Some(posting) = self.postings.get(&term).cloned() {
                let mut posting = posting;
                Arc::make_mut(&mut posting).remove(ordinal);
                if posting.is_empty() {
                    self.trigrams.remove_term(&term);
                    self.postings.remove(&term);
                } else {
                    self.postings.insert(term, posting)?;
                }
            }
        }
        Ok(())
    }

    fn validates(&self, field_name: &str, docs: &DocumentMap, ordinals: &OrdinalTable) -> bool {
        if Tokenizer::from_index_params(Some(&self.params)).as_ref() != Ok(&self.tokenizer)
            || self
                .document_lengths
                .keys()
                .any(|ordinal| !ordinals.live().contains(ordinal))
        {
            return false;
        }
        let expected_documents = docs
            .iter()
            .filter(|(_, doc)| text_value(doc, field_name).is_some())
            .count();
        if self.document_lengths.len() != expected_documents
            || docs.iter().any(|(id, doc)| {
                let Some(ordinal) = ordinals.ordinal(id) else {
                    return true;
                };
                self.document_lengths.contains_key(ordinal) != text_value(doc, field_name).is_some()
            })
        {
            return false;
        }
        let total_tokens = self
            .document_lengths
            .values()
            .try_fold(0_u64, |total, length| total.checked_add(u64::from(length)));
        total_tokens == Some(self.total_tokens)
            && self
                .document_lengths
                .validates(ordinals.allocated_len(), ordinals.live())
            && self.postings.validates()
            && self.postings.iter().all(|(_, posting)| {
                !posting.is_empty() && posting.validates(&self.document_lengths)
            })
    }

    fn expand_parsed_query(&self, parsed: &mut ParsedFtsQuery) {
        parsed.expand_matchers(|matcher| self.expand_matcher(matcher));
    }

    fn expand_matcher(&self, matcher: &FtsTermMatcher) -> Vec<String> {
        match matcher {
            FtsTermMatcher::Wildcard(pattern) => {
                if let Some(required) = pattern.required_trigrams() {
                    let mut terms: Vec<String> = self
                        .trigrams
                        .terms_containing_all(&required)
                        .into_iter()
                        .filter(|term| matcher.matches(term))
                        .collect();
                    terms.sort();
                    terms
                } else {
                    self.expand_matcher_scan(matcher)
                }
            }
            FtsTermMatcher::Fuzzy { term, distance } => {
                // Each edit can destroy at most three overlapping trigrams.
                // When the bound drops to zero, fall back to a full scan.
                let grams = char_trigrams(term);
                let minimum = grams
                    .len()
                    .saturating_sub(3usize.saturating_mul(usize::from(*distance)));
                if minimum == 0 || grams.is_empty() {
                    return self.expand_matcher_scan(matcher);
                }
                let mut terms: Vec<String> = self
                    .trigrams
                    .terms_sharing_at_least(&grams, minimum)
                    .into_iter()
                    .filter(|candidate| matcher.matches(candidate))
                    .collect();
                terms.sort();
                terms
            }
            FtsTermMatcher::Range { .. } => self.expand_matcher_scan(matcher),
        }
    }

    fn expand_matcher_scan(&self, matcher: &FtsTermMatcher) -> Vec<String> {
        self.postings
            .iter()
            .filter(|(term, _)| matcher.matches(term))
            .map(|(term, _)| term.to_string())
            .collect()
    }

    fn search(
        &self,
        terms: &[String],
        allowed: Option<&RoaringTreemap>,
        operator: FtsDefaultOperator,
    ) -> Result<Vec<(u64, f64)>> {
        if self.document_lengths.is_empty() || allowed.is_some_and(RoaringTreemap::is_empty) {
            return Ok(Vec::new());
        }
        let document_count = count_to_f64(
            u64::try_from(self.document_lengths.len())
                .map_err(|_| Error::resource_exhausted("FTS document count exceeds u64"))?,
        );
        let average_length = count_to_f64(self.total_tokens) / document_count;
        if let [term] = terms {
            return self.search_single(term, allowed, document_count, average_length);
        }
        if operator == FtsDefaultOperator::And {
            return self.search_conjunctive(terms, allowed, document_count, average_length);
        }
        let posting_visits = terms.iter().fold(0_usize, |visits, term| {
            visits.saturating_add(self.postings.get(term).map_or(0, |posting| posting.len()))
        });
        let allowed_visits = allowed.map_or(posting_visits, |allowed| {
            usize::try_from(allowed.len())
                .unwrap_or(usize::MAX)
                .saturating_mul(terms.len())
                .min(posting_visits)
        });
        if allowed_visits >= DENSE_SCORE_MIN_VISITS {
            let ordinal_span = self
                .document_lengths
                .keys()
                .next_back()
                .and_then(|ordinal| usize::try_from(ordinal).ok())
                .and_then(|ordinal| ordinal.checked_add(1));
            if let Some(ordinal_span) = ordinal_span
                .filter(|ordinal_span| use_dense_score_scratch(allowed_visits, *ordinal_span))
            {
                return self.search_dense(
                    terms,
                    allowed,
                    document_count,
                    average_length,
                    allowed_visits,
                    ordinal_span,
                );
            }
        }
        self.search_sparse(terms, allowed, document_count, average_length)
    }

    fn search_expression(
        &self,
        query: &ParsedFtsQuery,
        allowed: Option<&RoaringTreemap>,
        docs: &DocumentMap,
        ordinals: &OrdinalTable,
        field_name: &str,
    ) -> Result<Vec<(u64, f64)>> {
        if self.document_lengths.is_empty() || allowed.is_some_and(RoaringTreemap::is_empty) {
            return Ok(Vec::new());
        }
        let mut candidates = self.expression_candidates(&query.root)?;
        if let Some(allowed) = allowed {
            candidates &= allowed;
        }
        if candidates.is_empty() {
            return Ok(Vec::new());
        }
        let document_count = count_to_f64(
            u64::try_from(self.document_lengths.len())
                .map_err(|_| Error::resource_exhausted("FTS document count exceeds u64"))?,
        );
        let average_length = count_to_f64(self.total_tokens) / document_count;
        let mut scores = Vec::new();
        scores
            .try_reserve_exact(usize::try_from(candidates.len()).unwrap_or(usize::MAX))
            .map_err(|_| {
                Error::resource_exhausted("FTS score result exceeds addressable memory")
            })?;
        for ordinal in candidates {
            let id = ordinals.id(ordinal).ok_or_else(|| {
                Error::internal(format!(
                    "FTS candidate ordinal {ordinal} has no primary key"
                ))
            })?;
            let text = docs
                .get(id)
                .and_then(|doc| text_value(doc, field_name))
                .ok_or_else(|| {
                    Error::internal(format!("FTS candidate document '{id}' has no indexed text"))
                })?;
            let mut context =
                IndexedEvalContext::new(self, ordinal, text, document_count, average_length);
            if let Some(score) = query.score(&mut context).filter(|score| *score > 0.0) {
                scores.push((ordinal, score));
            }
        }
        Ok(scores)
    }

    fn search_conjunctive(
        &self,
        terms: &[String],
        allowed: Option<&RoaringTreemap>,
        document_count: f64,
        average_length: f64,
    ) -> Result<Vec<(u64, f64)>> {
        let unique_terms: BTreeSet<&str> = terms.iter().map(String::as_str).collect();
        let mut required = Vec::with_capacity(unique_terms.len());
        for term in unique_terms {
            let Some(posting) = self.postings.get(term) else {
                return Ok(Vec::new());
            };
            required.push((term, posting));
        }
        required.sort_unstable_by(|left, right| {
            left.1
                .len()
                .cmp(&right.1.len())
                .then_with(|| left.0.cmp(right.0))
        });
        let Some((_, driver)) = required.first() else {
            return Ok(Vec::new());
        };
        let scoring: Vec<_> = terms
            .iter()
            .map(|term| {
                let posting = self.postings.get(term).ok_or_else(|| {
                    Error::internal(format!("FTS posting is missing for required term '{term}'"))
                })?;
                let document_frequency = count_to_f64(
                    u64::try_from(posting.len())
                        .map_err(|_| Error::resource_exhausted("FTS posting count exceeds u64"))?,
                );
                Ok((posting, document_frequency))
            })
            .collect::<Result<_>>()?;
        let capacity = allowed.map_or(driver.len(), |allowed| {
            driver
                .len()
                .min(usize::try_from(allowed.len()).unwrap_or(usize::MAX))
        });
        let mut scores = Vec::new();
        scores.try_reserve_exact(capacity).map_err(|_| {
            Error::resource_exhausted("FTS score result exceeds addressable memory")
        })?;
        for (ordinal, _) in driver.iter() {
            if allowed.is_some_and(|allowed| !allowed.contains(ordinal))
                || required
                    .iter()
                    .skip(1)
                    .any(|(_, posting)| posting.get(ordinal).is_none())
            {
                continue;
            }
            let mut score = 0.0_f64;
            for (posting, document_frequency) in &scoring {
                let entry = posting.get(ordinal).ok_or_else(|| {
                    Error::internal(format!(
                        "FTS posting is missing required document ordinal {ordinal}"
                    ))
                })?;
                score += bm25_term_score(
                    f64::from(entry.frequency),
                    *document_frequency,
                    document_count,
                    f64::from(entry.document_length),
                    average_length,
                );
            }
            if score > 0.0 {
                scores.push((ordinal, score));
            }
        }
        Ok(scores)
    }

    fn search_single(
        &self,
        term: &str,
        allowed: Option<&RoaringTreemap>,
        document_count: f64,
        average_length: f64,
    ) -> Result<Vec<(u64, f64)>> {
        let Some(posting) = self.postings.get(term) else {
            return Ok(Vec::new());
        };
        let document_frequency = count_to_f64(
            u64::try_from(posting.len())
                .map_err(|_| Error::resource_exhausted("FTS posting count exceeds u64"))?,
        );
        let capacity = allowed.map_or(posting.len(), |allowed| {
            posting
                .len()
                .min(usize::try_from(allowed.len()).unwrap_or(usize::MAX))
        });
        let mut scores = Vec::new();
        scores.try_reserve_exact(capacity).map_err(|_| {
            Error::resource_exhausted("FTS score result exceeds addressable memory")
        })?;
        for (ordinal, entry) in posting.iter() {
            if allowed.is_some_and(|allowed| !allowed.contains(ordinal)) {
                continue;
            }
            let score = bm25_term_score(
                f64::from(entry.frequency),
                document_frequency,
                document_count,
                f64::from(entry.document_length),
                average_length,
            );
            if score > 0.0 {
                scores.push((ordinal, score));
            }
        }
        Ok(scores)
    }

    fn search_sparse(
        &self,
        terms: &[String],
        allowed: Option<&RoaringTreemap>,
        document_count: f64,
        average_length: f64,
    ) -> Result<Vec<(u64, f64)>> {
        let mut scores = BTreeMap::<u64, f64>::new();
        for term in terms {
            let Some(posting) = self.postings.get(term) else {
                continue;
            };
            let document_frequency = count_to_f64(
                u64::try_from(posting.len())
                    .map_err(|_| Error::resource_exhausted("FTS posting count exceeds u64"))?,
            );
            for (ordinal, entry) in posting.iter() {
                if allowed.is_some_and(|allowed| !allowed.contains(ordinal)) {
                    continue;
                }
                let contribution = bm25_term_score(
                    f64::from(entry.frequency),
                    document_frequency,
                    document_count,
                    f64::from(entry.document_length),
                    average_length,
                );
                *scores.entry(ordinal).or_default() += contribution;
            }
        }
        Ok(scores
            .into_iter()
            .filter(|(_, score)| *score > 0.0)
            .collect())
    }

    fn search_dense(
        &self,
        terms: &[String],
        allowed: Option<&RoaringTreemap>,
        document_count: f64,
        average_length: f64,
        estimated_visits: usize,
        ordinal_span: usize,
    ) -> Result<Vec<(u64, f64)>> {
        let mut scores = Vec::new();
        scores.try_reserve_exact(ordinal_span).map_err(|_| {
            Error::resource_exhausted("FTS direct score scratch exceeds addressable memory")
        })?;
        scores.resize(ordinal_span, 0.0_f64);
        let mut touched = Vec::new();
        touched
            .try_reserve(estimated_visits.min(self.document_lengths.len()))
            .map_err(|_| {
                Error::resource_exhausted("FTS touched-ordinal scratch exceeds addressable memory")
            })?;
        for term in terms {
            let Some(posting) = self.postings.get(term) else {
                continue;
            };
            let document_frequency = count_to_f64(
                u64::try_from(posting.len())
                    .map_err(|_| Error::resource_exhausted("FTS posting count exceeds u64"))?,
            );
            for (ordinal, entry) in posting.iter() {
                if allowed.is_some_and(|allowed| !allowed.contains(ordinal)) {
                    continue;
                }
                let slot = usize::try_from(ordinal)
                    .ok()
                    .and_then(|ordinal| scores.get_mut(ordinal))
                    .ok_or_else(|| {
                        Error::internal(format!(
                            "FTS score slot is missing for document ordinal {ordinal}"
                        ))
                    })?;
                let contribution = bm25_term_score(
                    f64::from(entry.frequency),
                    document_frequency,
                    document_count,
                    f64::from(entry.document_length),
                    average_length,
                );
                if *slot == 0.0 {
                    touched.push(ordinal);
                }
                *slot += contribution;
            }
        }
        touched.sort_unstable();
        let mut output = Vec::new();
        output.try_reserve_exact(touched.len()).map_err(|_| {
            Error::resource_exhausted("FTS score result exceeds addressable memory")
        })?;
        for ordinal in touched {
            let score = usize::try_from(ordinal)
                .ok()
                .and_then(|ordinal| scores.get(ordinal).copied())
                .ok_or_else(|| {
                    Error::internal(format!(
                        "FTS score result is missing for document ordinal {ordinal}"
                    ))
                })?;
            if score > 0.0 {
                output.push((ordinal, score));
            }
        }
        Ok(output)
    }
}

fn use_dense_score_scratch(estimated_visits: usize, ordinal_span: usize) -> bool {
    estimated_visits >= DENSE_SCORE_MIN_VISITS
        && ordinal_span <= estimated_visits.saturating_mul(DENSE_SCORE_MAX_SPAN_FACTOR)
}

/// Storage limits keep document/token counts below f64's exact integer range.
#[allow(clippy::cast_precision_loss)]
fn count_to_f64(value: u64) -> f64 {
    value as f64
}

#[cfg(test)]
mod dense_scratch_tests {
    use super::{use_dense_score_scratch, DENSE_SCORE_MAX_SPAN_FACTOR, DENSE_SCORE_MIN_VISITS};

    #[test]
    fn dense_scratch_gate_requires_large_visits_and_bounded_span() {
        assert!(!use_dense_score_scratch(DENSE_SCORE_MIN_VISITS - 1, 1));
        assert!(use_dense_score_scratch(
            DENSE_SCORE_MIN_VISITS,
            DENSE_SCORE_MIN_VISITS
        ));
        assert!(!use_dense_score_scratch(
            DENSE_SCORE_MIN_VISITS,
            DENSE_SCORE_MIN_VISITS
                .saturating_mul(DENSE_SCORE_MAX_SPAN_FACTOR)
                .saturating_add(1)
        ));
    }
}