Skip to main content

hermes_core/query/
global_stats.rs

1//! Lazy global statistics for cross-segment IDF computation
2//!
3//! Provides lazily computed and cached statistics across multiple segments for:
4//! - Sparse vector dimensions (for sparse vector queries)
5//! - Full-text terms (for BM25/TF-IDF scoring)
6//!
7//! Key design principles:
8//! - **Lazy computation**: IDF values computed on first access, not upfront
9//! - **Per-term caching**: Each term/dimension's IDF is cached independently
10//! - **Bound to Searcher**: Stats tied to segment snapshot lifetime
11
12use std::sync::Arc;
13
14use parking_lot::RwLock;
15use rustc_hash::FxHashMap;
16
17use crate::dsl::Field;
18use crate::segment::SegmentReader;
19
20/// Lazy global statistics bound to a fixed set of segments
21///
22/// Computes IDF values lazily on first access and caches them.
23/// Lifetime is bound to the Searcher that created it, ensuring
24/// statistics always match the current segment set.
25pub struct LazyGlobalStats {
26    /// Segment readers (Arc for shared ownership with Searcher)
27    segments: Vec<Arc<SegmentReader>>,
28    /// Total documents (computed once on construction)
29    total_docs: u64,
30    /// Cached sparse IDF values: field_id -> (dim_id -> idf)
31    sparse_idf_cache: RwLock<FxHashMap<u32, FxHashMap<u32, f32>>>,
32    /// Cached text IDF values: field_id -> (term -> idf)
33    text_idf_cache: RwLock<FxHashMap<u32, FxHashMap<String, f32>>>,
34    /// Cached average field lengths: field_id -> avg_len
35    avg_field_len_cache: RwLock<FxHashMap<u32, f32>>,
36}
37
38impl LazyGlobalStats {
39    /// Create new lazy stats bound to a set of segments
40    pub fn new(segments: Vec<Arc<SegmentReader>>) -> Self {
41        let total_docs: u64 = segments.iter().map(|s| s.num_docs() as u64).sum();
42        Self {
43            segments,
44            total_docs,
45            sparse_idf_cache: RwLock::new(FxHashMap::default()),
46            text_idf_cache: RwLock::new(FxHashMap::default()),
47            avg_field_len_cache: RwLock::new(FxHashMap::default()),
48        }
49    }
50
51    /// Total documents across all segments
52    #[inline]
53    pub fn total_docs(&self) -> u64 {
54        self.total_docs
55    }
56
57    /// Get or compute IDF for a sparse vector dimension (lazy + cached)
58    ///
59    /// IDF = ln(N / df) where N = total docs, df = docs containing dimension
60    pub fn sparse_idf(&self, field: Field, dim_id: u32) -> f32 {
61        // Fast path: check cache
62        {
63            let cache = self.sparse_idf_cache.read();
64            if let Some(field_cache) = cache.get(&field.0)
65                && let Some(&idf) = field_cache.get(&dim_id)
66            {
67                return idf;
68            }
69        }
70
71        // Slow path: compute and cache
72        let df = self.compute_sparse_df(field, dim_id);
73        // Use total_vectors for proper IDF with multi-valued fields
74        // This ensures df <= N even when documents have multiple sparse vectors
75        let total_vectors = self.compute_sparse_total_vectors(field);
76        let n = total_vectors.max(self.total_docs);
77        let idf = if df > 0 && n > 0 {
78            (n as f32 / df as f32).ln().max(0.0)
79        } else {
80            0.0
81        };
82
83        // Cache the result
84        {
85            let mut cache = self.sparse_idf_cache.write();
86            cache.entry(field.0).or_default().insert(dim_id, idf);
87        }
88
89        idf
90    }
91
92    /// Compute IDF weights for multiple sparse dimensions (batch, uses cache)
93    pub fn sparse_idf_weights(&self, field: Field, dim_ids: &[u32]) -> Vec<f32> {
94        dim_ids.iter().map(|&d| self.sparse_idf(field, d)).collect()
95    }
96
97    /// Get or compute IDF for a full-text term (lazy + cached)
98    ///
99    /// IDF = ln((N - df + 0.5) / (df + 0.5) + 1) (BM25 variant)
100    pub fn text_idf(&self, field: Field, term: &str) -> f32 {
101        // Fast path: check cache
102        {
103            let cache = self.text_idf_cache.read();
104            if let Some(field_cache) = cache.get(&field.0)
105                && let Some(&idf) = field_cache.get(term)
106            {
107                return idf;
108            }
109        }
110
111        // Slow path: compute and cache
112        let df = self.compute_text_df(field, term);
113        let n = self.total_docs as f32;
114        let df_f = df as f32;
115        let idf = if df > 0 {
116            ((n - df_f + 0.5) / (df_f + 0.5) + 1.0).ln()
117        } else {
118            0.0
119        };
120
121        // Cache the result
122        {
123            let mut cache = self.text_idf_cache.write();
124            cache
125                .entry(field.0)
126                .or_default()
127                .insert(term.to_string(), idf);
128        }
129
130        idf
131    }
132
133    /// Get or compute average field length for BM25 (lazy + cached)
134    pub fn avg_field_len(&self, field: Field) -> f32 {
135        // Fast path: check cache
136        {
137            let cache = self.avg_field_len_cache.read();
138            if let Some(&avg) = cache.get(&field.0) {
139                return avg;
140            }
141        }
142
143        // Slow path: compute weighted average across segments
144        let mut weighted_sum = 0.0f64;
145        let mut total_weight = 0u64;
146
147        for segment in &self.segments {
148            let avg_len = segment.avg_field_len(field);
149            let doc_count = segment.num_docs() as u64;
150            if avg_len > 0.0 && doc_count > 0 {
151                weighted_sum += avg_len as f64 * doc_count as f64;
152                total_weight += doc_count;
153            }
154        }
155
156        let avg = if total_weight > 0 {
157            (weighted_sum / total_weight as f64) as f32
158        } else {
159            1.0
160        };
161
162        // Cache the result
163        {
164            let mut cache = self.avg_field_len_cache.write();
165            cache.insert(field.0, avg);
166        }
167
168        avg
169    }
170
171    /// Compute document frequency for a sparse dimension (not cached - internal)
172    fn compute_sparse_df(&self, field: Field, dim_id: u32) -> u64 {
173        let mut df = 0u64;
174        for segment in &self.segments {
175            if let Some(sparse_index) = segment.sparse_indexes().get(&field.0)
176                && let Some(Some(posting)) = sparse_index.postings.get(dim_id as usize)
177            {
178                df += posting.doc_count() as u64;
179            }
180        }
181        df
182    }
183
184    /// Compute total sparse vectors for a field across all segments
185    /// For multi-valued fields, this may exceed total_docs
186    fn compute_sparse_total_vectors(&self, field: Field) -> u64 {
187        let mut total = 0u64;
188        for segment in &self.segments {
189            if let Some(sparse_index) = segment.sparse_indexes().get(&field.0) {
190                total += sparse_index.total_vectors as u64;
191            }
192        }
193        total
194    }
195
196    /// Compute document frequency for a text term (not cached - internal)
197    ///
198    /// Note: This is expensive as it requires async term lookup.
199    /// For now, returns 0 - text IDF should be computed via term dictionary.
200    fn compute_text_df(&self, _field: Field, _term: &str) -> u64 {
201        // Text term lookup requires async access to term dictionary
202        // For now, this is a placeholder - actual implementation would
203        // need to be async or use pre-computed stats
204        0
205    }
206
207    /// Number of segments
208    pub fn num_segments(&self) -> usize {
209        self.segments.len()
210    }
211}
212
213impl std::fmt::Debug for LazyGlobalStats {
214    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
215        f.debug_struct("LazyGlobalStats")
216            .field("total_docs", &self.total_docs)
217            .field("num_segments", &self.segments.len())
218            .field("sparse_cache_fields", &self.sparse_idf_cache.read().len())
219            .field("text_cache_fields", &self.text_idf_cache.read().len())
220            .finish()
221    }
222}
223
224// Keep old types for backwards compatibility during transition
225
226/// Global statistics aggregated across all segments (legacy)
227#[derive(Debug)]
228pub struct GlobalStats {
229    /// Total documents across all segments
230    total_docs: u64,
231    /// Sparse vector statistics per field: field_id -> dimension stats
232    sparse_stats: FxHashMap<u32, SparseFieldStats>,
233    /// Full-text statistics per field: field_id -> term stats
234    text_stats: FxHashMap<u32, TextFieldStats>,
235    /// Generation counter for cache invalidation
236    generation: u64,
237}
238
239/// Statistics for a sparse vector field
240#[derive(Debug, Default)]
241pub struct SparseFieldStats {
242    /// Document frequency per dimension: dim_id -> doc_count
243    pub doc_freqs: FxHashMap<u32, u64>,
244}
245
246/// Statistics for a full-text field
247#[derive(Debug, Default)]
248pub struct TextFieldStats {
249    /// Document frequency per term: term -> doc_count
250    pub doc_freqs: FxHashMap<String, u64>,
251    /// Average field length (for BM25)
252    pub avg_field_len: f32,
253}
254
255impl GlobalStats {
256    /// Create empty stats
257    pub fn new() -> Self {
258        Self {
259            total_docs: 0,
260            sparse_stats: FxHashMap::default(),
261            text_stats: FxHashMap::default(),
262            generation: 0,
263        }
264    }
265
266    /// Total documents in the index
267    #[inline]
268    pub fn total_docs(&self) -> u64 {
269        self.total_docs
270    }
271
272    /// Compute IDF for a sparse vector dimension
273    #[inline]
274    pub fn sparse_idf(&self, field: Field, dim_id: u32) -> f32 {
275        if let Some(stats) = self.sparse_stats.get(&field.0)
276            && let Some(&df) = stats.doc_freqs.get(&dim_id)
277            && df > 0
278        {
279            return (self.total_docs as f32 / df as f32).ln();
280        }
281        0.0
282    }
283
284    /// Compute IDF weights for multiple sparse dimensions
285    pub fn sparse_idf_weights(&self, field: Field, dim_ids: &[u32]) -> Vec<f32> {
286        dim_ids.iter().map(|&d| self.sparse_idf(field, d)).collect()
287    }
288
289    /// Compute IDF for a full-text term
290    #[inline]
291    pub fn text_idf(&self, field: Field, term: &str) -> f32 {
292        if let Some(stats) = self.text_stats.get(&field.0)
293            && let Some(&df) = stats.doc_freqs.get(term)
294        {
295            let n = self.total_docs as f32;
296            let df = df as f32;
297            return ((n - df + 0.5) / (df + 0.5) + 1.0).ln();
298        }
299        0.0
300    }
301
302    /// Get average field length for BM25
303    #[inline]
304    pub fn avg_field_len(&self, field: Field) -> f32 {
305        self.text_stats
306            .get(&field.0)
307            .map(|s| s.avg_field_len)
308            .unwrap_or(1.0)
309    }
310
311    /// Current generation
312    #[inline]
313    pub fn generation(&self) -> u64 {
314        self.generation
315    }
316}
317
318impl Default for GlobalStats {
319    fn default() -> Self {
320        Self::new()
321    }
322}
323
324/// Builder for aggregating statistics from multiple segments
325pub struct GlobalStatsBuilder {
326    /// Total documents across all segments
327    pub total_docs: u64,
328    sparse_stats: FxHashMap<u32, SparseFieldStats>,
329    text_stats: FxHashMap<u32, TextFieldStats>,
330}
331
332impl GlobalStatsBuilder {
333    /// Create a new builder
334    pub fn new() -> Self {
335        Self {
336            total_docs: 0,
337            sparse_stats: FxHashMap::default(),
338            text_stats: FxHashMap::default(),
339        }
340    }
341
342    /// Add statistics from a segment reader
343    pub fn add_segment(&mut self, reader: &SegmentReader) {
344        self.total_docs += reader.num_docs() as u64;
345
346        // Aggregate sparse vector statistics
347        // Note: This requires access to sparse_indexes which may need to be exposed
348    }
349
350    /// Add sparse dimension document frequency
351    pub fn add_sparse_df(&mut self, field: Field, dim_id: u32, doc_count: u64) {
352        let stats = self.sparse_stats.entry(field.0).or_default();
353        *stats.doc_freqs.entry(dim_id).or_insert(0) += doc_count;
354    }
355
356    /// Add text term document frequency
357    pub fn add_text_df(&mut self, field: Field, term: String, doc_count: u64) {
358        let stats = self.text_stats.entry(field.0).or_default();
359        *stats.doc_freqs.entry(term).or_insert(0) += doc_count;
360    }
361
362    /// Set average field length for a text field
363    pub fn set_avg_field_len(&mut self, field: Field, avg_len: f32) {
364        let stats = self.text_stats.entry(field.0).or_default();
365        stats.avg_field_len = avg_len;
366    }
367
368    /// Build the final GlobalStats
369    pub fn build(self, generation: u64) -> GlobalStats {
370        GlobalStats {
371            total_docs: self.total_docs,
372            sparse_stats: self.sparse_stats,
373            text_stats: self.text_stats,
374            generation,
375        }
376    }
377}
378
379impl Default for GlobalStatsBuilder {
380    fn default() -> Self {
381        Self::new()
382    }
383}
384
385/// Cached global statistics with automatic invalidation
386///
387/// This is the main entry point for getting global IDF values.
388/// It caches statistics and rebuilds them when the segment list changes.
389pub struct GlobalStatsCache {
390    /// Cached statistics
391    stats: RwLock<Option<Arc<GlobalStats>>>,
392    /// Current generation (incremented when segments change)
393    generation: RwLock<u64>,
394}
395
396impl GlobalStatsCache {
397    /// Create a new cache
398    pub fn new() -> Self {
399        Self {
400            stats: RwLock::new(None),
401            generation: RwLock::new(0),
402        }
403    }
404
405    /// Invalidate the cache (call when segments are added/removed/merged)
406    pub fn invalidate(&self) {
407        let mut current_gen = self.generation.write();
408        *current_gen += 1;
409        let mut stats = self.stats.write();
410        *stats = None;
411    }
412
413    /// Get current generation
414    pub fn generation(&self) -> u64 {
415        *self.generation.read()
416    }
417
418    /// Get cached stats if valid, or None if needs rebuild
419    pub fn get(&self) -> Option<Arc<GlobalStats>> {
420        self.stats.read().clone()
421    }
422
423    /// Update the cache with new stats
424    pub fn set(&self, stats: GlobalStats) {
425        let mut cached = self.stats.write();
426        *cached = Some(Arc::new(stats));
427    }
428
429    /// Get or compute stats using the provided builder function (sync version)
430    ///
431    /// For basic stats that don't require async iteration.
432    pub fn get_or_compute<F>(&self, compute: F) -> Arc<GlobalStats>
433    where
434        F: FnOnce(&mut GlobalStatsBuilder),
435    {
436        // Fast path: return cached if available
437        if let Some(stats) = self.get() {
438            return stats;
439        }
440
441        // Slow path: compute new stats
442        let current_gen = self.generation();
443        let mut builder = GlobalStatsBuilder::new();
444        compute(&mut builder);
445        let stats = Arc::new(builder.build(current_gen));
446
447        // Cache the result
448        let mut cached = self.stats.write();
449        *cached = Some(Arc::clone(&stats));
450
451        stats
452    }
453
454    /// Check if stats need to be rebuilt
455    pub fn needs_rebuild(&self) -> bool {
456        self.stats.read().is_none()
457    }
458
459    /// Set pre-built stats (for async computation)
460    pub fn set_stats(&self, stats: GlobalStats) {
461        let mut cached = self.stats.write();
462        *cached = Some(Arc::new(stats));
463    }
464}
465
466impl Default for GlobalStatsCache {
467    fn default() -> Self {
468        Self::new()
469    }
470}
471
472#[cfg(test)]
473mod tests {
474    use super::*;
475
476    #[test]
477    fn test_sparse_idf_computation() {
478        let mut builder = GlobalStatsBuilder::new();
479        builder.total_docs = 1000;
480        builder.add_sparse_df(Field(0), 42, 100); // dim 42 appears in 100 docs
481        builder.add_sparse_df(Field(0), 43, 10); // dim 43 appears in 10 docs
482
483        let stats = builder.build(1);
484
485        // IDF = ln(N/df)
486        let idf_42 = stats.sparse_idf(Field(0), 42);
487        let idf_43 = stats.sparse_idf(Field(0), 43);
488
489        // dim 43 should have higher IDF (rarer)
490        assert!(idf_43 > idf_42);
491        assert!((idf_42 - (1000.0_f32 / 100.0).ln()).abs() < 0.001);
492        assert!((idf_43 - (1000.0_f32 / 10.0).ln()).abs() < 0.001);
493    }
494
495    #[test]
496    fn test_text_idf_computation() {
497        let mut builder = GlobalStatsBuilder::new();
498        builder.total_docs = 10000;
499        builder.add_text_df(Field(0), "common".to_string(), 5000);
500        builder.add_text_df(Field(0), "rare".to_string(), 10);
501
502        let stats = builder.build(1);
503
504        let idf_common = stats.text_idf(Field(0), "common");
505        let idf_rare = stats.text_idf(Field(0), "rare");
506
507        // Rare term should have higher IDF
508        assert!(idf_rare > idf_common);
509    }
510
511    #[test]
512    fn test_cache_invalidation() {
513        let cache = GlobalStatsCache::new();
514
515        // Initially no stats
516        assert!(cache.get().is_none());
517
518        // Compute stats
519        let stats = cache.get_or_compute(|builder| {
520            builder.total_docs = 100;
521        });
522        assert_eq!(stats.total_docs(), 100);
523
524        // Should be cached now
525        assert!(cache.get().is_some());
526
527        // Invalidate
528        cache.invalidate();
529        assert!(cache.get().is_none());
530    }
531}