ferritin-common 0.4.0

library for rustdoc navigation and search
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
use fieldwork::Fieldwork;
use rkyv::rancor::Error;
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
use rustc_hash::FxHashMap;
use rustc_hash::FxHasher;
use rustdoc_types::{Item, ItemEnum, StructKind, Trait};
use std::cmp::Reverse;
use std::collections::BTreeMap;
use std::fs::File;
use std::fs::OpenOptions;
use std::hash::{Hash, Hasher};
use std::io::{Read, Write};
use std::path::Path;
use std::time::SystemTime;

use crate::{
    doc_ref::DocRef,
    navigator::{Navigator, Suggestion},
};
use std::collections::HashMap;

// Newtypes for clarity
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Archive,
    RkyvSerialize,
    RkyvDeserialize,
)]
#[rkyv(derive(PartialEq, Eq, PartialOrd, Ord))]
#[repr(transparent)]
struct TermHash(u64);

#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Archive,
    RkyvSerialize,
    RkyvDeserialize,
)]
#[repr(transparent)]
struct DocumentId(usize);

#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Archive, RkyvSerialize, RkyvDeserialize,
)]
#[repr(transparent)]
struct DocumentTermCount(usize);

#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Archive, RkyvSerialize, RkyvDeserialize,
)]
#[repr(transparent)]
struct DocumentLength(usize);

#[derive(Debug, Clone, PartialEq, Eq, Archive, RkyvSerialize, RkyvDeserialize)]
struct ItemPath(Vec<u32>);

#[derive(Debug, Clone, Copy, Archive, RkyvSerialize, RkyvDeserialize)]
struct Posting {
    document: DocumentId,
    count: DocumentTermCount,
}

#[derive(Debug, Clone, Archive, RkyvSerialize, RkyvDeserialize)]
struct DocumentInfo {
    path: ItemPath,
    length: DocumentLength,
}

#[derive(Default, Debug, Clone)]
struct Terms<'a> {
    term_docs: BTreeMap<TermHash, BTreeMap<(u64, u32), DocumentTermCount>>,
    shortest_paths: BTreeMap<(u64, u32), Vec<u32>>,
    document_lengths: BTreeMap<(u64, u32), DocumentLength>,
    crate_hashes: FxHashMap<&'a str, TermHash>,
}

impl<'a> Terms<'a> {
    fn add(&mut self, word: &str, count: DocumentTermCount, id: (u64, u32)) {
        let term_hash = hash_term(word);
        let entry = self
            .term_docs
            .entry(term_hash)
            .or_default()
            .entry(id)
            .or_insert(DocumentTermCount(0));
        entry.0 += count.0;
    }

    fn finalize(self) -> SearchableTerms {
        let mut documents = vec![];
        let mut id_set = BTreeMap::new();
        let mut total_document_length = 0;

        for (id, id_path) in self.shortest_paths {
            let doc_length = self
                .document_lengths
                .get(&id)
                .copied()
                .unwrap_or(DocumentLength(0));
            total_document_length += doc_length.0;
            id_set.insert(id, documents.len());
            documents.push(DocumentInfo {
                path: ItemPath(id_path),
                length: doc_length,
            });
        }

        let terms = self
            .term_docs
            .into_iter()
            .map(|(term_hash, doc_counts)| {
                // Store raw counts, not TF-IDF
                let mut postings: Vec<_> = doc_counts
                    .into_iter()
                    .filter_map(|(doc_id, count)| {
                        id_set.get(&doc_id).map(|&id| Posting {
                            document: DocumentId(id),
                            count,
                        })
                    })
                    .collect();

                // Sort by count (descending) for faster retrieval of top results
                postings.sort_by_key(|b| Reverse(b.count.0));

                (term_hash, postings)
            })
            .collect();

        SearchableTerms {
            terms,
            documents,
            total_document_length,
        }
    }

    fn recurse(&mut self, item: DocRef<'a, Item>, ids: &[u32], add_id: bool) {
        let mut ids = ids.to_owned();
        if add_id {
            ids.push(item.id.0);
        }
        let crate_name = item.crate_docs().name();

        let crate_hash = self
            .crate_hashes
            .entry(crate_name)
            .or_insert_with(|| hash_term(crate_name));

        let id = (crate_hash.0, *ids.last().unwrap_or(&item.id.0));

        if let Some(existing_path) = self.shortest_paths.get_mut(&id) {
            if ids.len() < existing_path.len() {
                *existing_path = ids;
            }
            return;
        }

        self.add_for_item(item, id);

        match item.inner() {
            ItemEnum::Struct(struct_item) => match &struct_item.kind {
                StructKind::Unit => {}
                StructKind::Tuple(field_ids) => {
                    for field in field_ids.iter().flatten().filter_map(|id| item.get(id)) {
                        self.add_for_item(field, id);
                    }
                }
                StructKind::Plain { fields, .. } => {
                    for field in item.id_iter(fields) {
                        self.add_for_item(field, id);
                    }
                }
            },
            ItemEnum::Trait(Trait { items, .. }) => {
                for field in item.id_iter(items) {
                    self.recurse(field, &ids, false);
                }
            }
            _ => {}
        };

        for child in item.child_items().with_use() {
            self.recurse(child, &ids, true)
        }

        self.shortest_paths.insert(id, ids);
    }

    fn add_for_item(&mut self, item: DocRef<'a, Item>, id: (u64, u32)) {
        let mut doc_length = 0;

        if let Some(name) = item.name() {
            doc_length += self.add_terms(name, id, 2);
        }

        if let Some(docs) = &item.docs {
            doc_length += self.add_terms(docs, id, 1);
        }

        self.document_lengths.insert(id, DocumentLength(doc_length));
    }

    fn add_terms(&mut self, text: &str, id: (u64, u32), weight: usize) -> usize {
        let words = tokenize(text);
        let doc_length = words.len();

        // Count word frequencies in this document
        let mut word_counts: BTreeMap<&str, usize> = BTreeMap::new();
        for word in &words {
            *word_counts.entry(word).or_insert(0) += 1;
        }

        // Add each unique word to the index with weighted count
        for (word, count) in word_counts {
            let weighted_count = count * weight;
            self.add(word, DocumentTermCount(weighted_count), id);
        }

        doc_length
    }
}

#[derive(Debug, Clone, Archive, RkyvSerialize, RkyvDeserialize)]
struct SearchableTerms {
    terms: BTreeMap<TermHash, Vec<Posting>>,
    documents: Vec<DocumentInfo>,
    total_document_length: usize,
}

/// A search index for a single crate
#[derive(Debug, Clone, Fieldwork)]
pub struct SearchIndex {
    #[field(get)]
    crate_name: String,
    terms: SearchableTerms,
}

impl SearchableTerms {
    fn search<'a>(&self, query: &'a str) -> SearchResults<'a> {
        let tokens = tokenize(query);

        // Build lookup from hash to original token
        let token_map: HashMap<TermHash, &'a str> = tokens
            .iter()
            .map(|&token| (hash_term(token), token))
            .collect();

        // Collect posting lists for each query term
        let mut term_postings: HashMap<TermHash, &Vec<Posting>> = HashMap::new();
        for &token in &tokens {
            let term_hash = hash_term(token);
            if let Some(postings) = self.terms.get(&term_hash) {
                term_postings.insert(term_hash, postings);
            }
        }

        // Build document frequency map (in borrowed strings for public API)
        let term_doc_freqs: HashMap<&'a str, usize> = term_postings
            .iter()
            .map(|(term_hash, postings)| {
                let term_str = token_map.get(term_hash).unwrap();
                (*term_str, postings.len())
            })
            .collect();

        // Collect all matching documents and aggregate term counts
        let mut doc_term_counts: BTreeMap<DocumentId, HashMap<&'a str, usize>> = BTreeMap::new();
        for (term_hash, postings) in term_postings {
            let term_str = token_map.get(&term_hash).unwrap();
            for posting in postings.iter() {
                doc_term_counts
                    .entry(posting.document)
                    .or_default()
                    .insert(term_str, posting.count.0);
            }
        }

        // Convert to results vec
        let results: Vec<SearchResult<'a>> = doc_term_counts
            .into_iter()
            .filter_map(|(doc_id, term_counts)| {
                self.documents.get(doc_id.0).map(|doc_info| SearchResult {
                    id_path: doc_info.path.0.clone(),
                    doc_length: doc_info.length.0,
                    term_counts,
                })
            })
            .collect();

        SearchResults {
            total_docs: self.documents.len(),
            total_doc_length: self.total_document_length,
            term_doc_freqs,
            results,
        }
    }
}

impl SearchIndex {
    pub fn load_or_build<'a>(
        navigator: &'a Navigator,
        crate_name: &str,
    ) -> Result<Self, Vec<Suggestion<'a>>> {
        let mut suggestions = vec![];

        let item = navigator
            .resolve_path(crate_name, &mut suggestions)
            .ok_or(suggestions)?;

        let crate_docs = item.crate_docs();
        let crate_name = crate_docs.name().to_string();

        let mtime = crate_docs
            .fs_path()
            .metadata()
            .ok()
            .and_then(|m| m.modified().ok());

        let mut path = crate_docs.fs_path().to_path_buf();
        path.set_extension("index");

        if let Some(terms) = Self::load(&path, mtime) {
            log::debug!("Loaded cached index from disk for {crate_name}");
            Ok(Self { crate_name, terms })
        } else {
            log::debug!("Building new index for {crate_name}");
            let mut terms = Terms::default();
            terms.recurse(item, &[], false);
            let terms = terms.finalize();
            log::debug!("Finished building index for {crate_name}");
            Self::store(&terms, &path);
            Ok(Self { terms, crate_name })
        }
    }

    fn store(terms: &SearchableTerms, path: &Path) {
        if let Ok(mut file) = OpenOptions::new().create_new(true).write(true).open(path) {
            match rkyv::to_bytes::<Error>(terms) {
                Ok(bytes) => {
                    if file.write_all(&bytes).is_err() {
                        let _ = std::fs::remove_file(path);
                    }
                }
                Err(_) => {
                    let _ = std::fs::remove_file(path);
                }
            }
        }
    }

    fn load(path: &Path, mtime: Option<SystemTime>) -> Option<SearchableTerms> {
        let mut file = File::open(path).ok()?;
        let index_mtime = file.metadata().ok().and_then(|m| m.modified().ok())?;

        let mtime = mtime?;
        if index_mtime.duration_since(mtime).is_ok() {
            let mut bytes = Vec::new();
            file.read_to_end(&mut bytes).ok()?;
            match rkyv::from_bytes::<SearchableTerms, Error>(&bytes) {
                Ok(terms) => Some(terms),
                Err(_) => {
                    let _ = std::fs::remove_file(path);
                    None
                }
            }
        } else {
            let _ = std::fs::remove_file(path);
            None
        }
    }

    pub fn len(&self) -> usize {
        self.terms.documents.len()
    }

    pub fn is_empty(&self) -> bool {
        self.terms.documents.is_empty()
    }

    /// Search for items containing the given term
    /// Returns components needed for BM25 scoring across multiple crates
    pub fn search<'a>(&self, query: &'a str) -> SearchResults<'a> {
        self.terms.search(query)
    }
}

// Public API types for BM25 scoring

/// Results from searching a single crate
pub struct SearchResults<'a> {
    /// Total number of documents in this crate's index
    pub total_docs: usize,
    /// Sum of all document lengths (for calculating average)
    pub total_doc_length: usize,
    /// How many documents contain each query term
    pub term_doc_freqs: HashMap<&'a str, usize>,
    /// Matching documents with their term counts
    pub results: Vec<SearchResult<'a>>,
}

/// A single document that matches the search query
pub struct SearchResult<'a> {
    /// Path to the item (rustdoc IDs)
    pub id_path: Vec<u32>,
    /// Length of this document in tokens
    pub doc_length: usize,
    /// Which query terms matched and their weighted counts
    pub term_counts: HashMap<&'a str, usize>,
}

/// A scored search result from BM25 scoring
pub struct ScoredResult<'a> {
    /// Which crate this result is from
    pub crate_name: &'a str,
    /// Path to the item (rustdoc IDs)
    pub id_path: Vec<u32>,
    /// BM25 score
    pub score: f32,
}

/// BM25 scorer for combining results from multiple crates
pub struct BM25Scorer<'a> {
    k1: f32,
    b: f32,
    crate_results: Vec<(&'a str, SearchResults<'a>)>,
}

impl<'a> BM25Scorer<'a> {
    /// Create a new BM25 scorer with default parameters
    pub fn new() -> Self {
        Self {
            k1: 1.2,
            b: 0.75,
            crate_results: Vec::new(),
        }
    }

    /// Add search results from a crate
    pub fn add(&mut self, crate_name: &'a str, results: SearchResults<'a>) {
        self.crate_results.push((crate_name, results));
    }

    /// Compute BM25 scores for all results and return them sorted by score
    pub fn score(self) -> Vec<ScoredResult<'a>> {
        log::debug!("Computing global statistics");

        // Aggregate global statistics
        let global_total_docs: usize = self.crate_results.iter().map(|(_, r)| r.total_docs).sum();
        let global_total_length: usize = self
            .crate_results
            .iter()
            .map(|(_, r)| r.total_doc_length)
            .sum();

        if global_total_docs == 0 {
            return vec![];
        }

        let avgdl = global_total_length as f32 / global_total_docs as f32;

        // Aggregate document frequencies across all crates
        let mut global_term_doc_freqs: HashMap<&str, usize> = HashMap::new();
        for (_, results) in &self.crate_results {
            for (term, doc_freq) in &results.term_doc_freqs {
                *global_term_doc_freqs.entry(term).or_default() += doc_freq;
            }
        }

        log::debug!(
            "Computing global IDF for {} terms",
            global_term_doc_freqs.len()
        );

        // Calculate global IDF for each term
        let global_idf: HashMap<&str, f32> = global_term_doc_freqs
            .iter()
            .map(|(term, doc_freq)| {
                // BM25 IDF formula
                let idf = ((global_total_docs as f32 - *doc_freq as f32 + 0.5)
                    / (*doc_freq as f32 + 0.5))
                    .ln();
                (*term, idf)
            })
            .collect();

        // Count total results to score
        let total_results: usize = self
            .crate_results
            .iter()
            .map(|(_, r)| r.results.len())
            .sum();
        log::debug!("Scoring {} results", total_results);

        // Score all results
        let mut scored: Vec<ScoredResult<'a>> = Vec::new();
        for (crate_name, results) in self.crate_results {
            for result in results.results {
                let doc_len_norm = result.doc_length as f32 / avgdl;

                let score: f32 = result
                    .term_counts
                    .iter()
                    .map(|(term, count)| {
                        let idf = global_idf.get(term).copied().unwrap_or(0.0);
                        let tf = *count as f32;
                        let numerator = tf * (self.k1 + 1.0);
                        let denominator = tf + self.k1 * (1.0 - self.b + self.b * doc_len_norm);
                        idf * (numerator / denominator)
                    })
                    .sum();

                scored.push(ScoredResult {
                    crate_name,
                    id_path: result.id_path,
                    score,
                });
            }
        }

        log::debug!("Sorting {} scored results", scored.len());

        // Sort by score descending
        scored.sort_by(|a, b| b.score.total_cmp(&a.score));

        scored
    }
}

impl<'a> Default for BM25Scorer<'a> {
    fn default() -> Self {
        Self::new()
    }
}

fn add_token<'a>(token: &'a str, tokens: &mut Vec<&'a str>) {
    tokens.push(token);
}

/// Simple tokenizer: split on whitespace and punctuation, lowercase, filter short words
fn tokenize(text: &str) -> Vec<&str> {
    let mut tokens = vec![];
    let min_chars = 2;
    let mut last_case = None;
    let mut word_start = 0;
    let mut subword_start = 0;
    let mut word_start_next_char = true;
    let mut subword_start_next_char = true;

    for (i, c) in text.char_indices() {
        if word_start_next_char {
            word_start = i;
            subword_start = i;
            word_start_next_char = false;
            subword_start_next_char = false;
        }

        if subword_start_next_char {
            subword_start = i;
            subword_start_next_char = false;
        }

        let current_case = c.is_alphabetic().then(|| c.is_uppercase());
        let case_change = last_case == Some(false) && current_case == Some(true);
        last_case = current_case;

        if c == '-' || c == '_' {
            if i.saturating_sub(subword_start) > min_chars {
                add_token(&text[subword_start..i], &mut tokens);
            }
            subword_start_next_char = true;
        } else if !c.is_alphabetic() {
            if i.saturating_sub(subword_start) > min_chars && subword_start != word_start {
                add_token(&text[subword_start..i], &mut tokens);
            }
            if i.saturating_sub(word_start) > min_chars {
                add_token(&text[word_start..i], &mut tokens);
            }
            word_start_next_char = true;
        } else if case_change {
            if i.saturating_sub(subword_start) > min_chars {
                add_token(&text[subword_start..i], &mut tokens);
            }
            subword_start = i;
        }
    }

    if !word_start_next_char {
        let last_subword = &text[subword_start..];

        if word_start != subword_start && last_subword.len() > min_chars {
            add_token(last_subword, &mut tokens);
        }

        let last_word = &text[word_start..];
        if last_word.len() > min_chars {
            add_token(last_word, &mut tokens);
        }
    }

    tokens
}

/// Hash a term for use as a map key (case-insensitive)
fn hash_term(term: &str) -> TermHash {
    let mut hasher = FxHasher::default();
    // Hash lowercased chars without allocating
    for c in term.chars() {
        for lower_c in c.to_lowercase() {
            lower_c.hash(&mut hasher);
        }
    }
    TermHash(hasher.finish())
}

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

    #[test]
    fn test_tokenize() {
        assert_eq!(
            tokenize("Hello, world! This is a test. CamelCase hyphenate-word snake_word"),
            vec![
                "Hello",
                "world",
                "This",
                "test",
                "Camel",
                "Case",
                "CamelCase",
                "hyphenate",
                "word",
                "hyphenate-word",
                "snake",
                "word",
                "snake_word"
            ]
        );
    }

    #[test]
    fn test_hash_term() {
        // Should be case insensitive
        assert_eq!(hash_term("Hello"), hash_term("HELLO"));
        assert_eq!(hash_term("Hello"), hash_term("hello"));
    }
}