ass_editor/utils/search/
engine.rs1use super::options::{SearchOptions, SearchResult, SearchStats};
7
8#[cfg(feature = "std")]
9use std::collections::HashMap;
10
11#[cfg(not(feature = "std"))]
12use hashbrown::HashMap;
13
14#[cfg(not(feature = "std"))]
15use alloc::{format, string::String, vec::Vec};
16
17#[cfg(feature = "search-index")]
18use crate::core::Position;
19
20#[cfg(feature = "search-index")]
21use fst::Set;
22
23#[derive(Debug)]
25pub struct DocumentSearchImpl {
26 #[cfg(feature = "search-index")]
28 pub(super) word_index: Option<Set<Vec<u8>>>,
29
30 #[cfg(feature = "search-index")]
32 pub(super) word_positions: HashMap<String, Vec<Position>>,
33
34 pub(super) document_version: u64,
36
37 pub(super) result_cache: HashMap<String, Vec<SearchResult<'static>>>,
39
40 pub(super) max_cache_entries: usize,
42
43 pub(super) stats: SearchStats,
45
46 pub(super) cached_text: String,
48}
49
50impl DocumentSearchImpl {
51 pub fn new() -> Self {
62 Self {
63 #[cfg(feature = "search-index")]
64 word_index: None,
65 #[cfg(feature = "search-index")]
66 word_positions: HashMap::new(),
67 document_version: 0,
68 result_cache: HashMap::new(),
69 max_cache_entries: 100,
70 stats: SearchStats {
71 match_count: 0,
72 search_time_us: 0,
73 hit_limit: false,
74 index_size: 0,
75 },
76 cached_text: String::new(),
77 }
78 }
79
80 pub fn set_cache_size(&mut self, size: usize) {
82 self.max_cache_entries = size;
83 self.trim_cache();
84 }
85
86 fn trim_cache(&mut self) {
88 if self.result_cache.len() > self.max_cache_entries {
89 let keys_to_remove: Vec<String> = self
91 .result_cache
92 .keys()
93 .take(self.result_cache.len() / 2)
94 .cloned()
95 .collect();
96
97 for key in keys_to_remove {
98 self.result_cache.remove(&key);
99 }
100 }
101 }
102
103 pub(super) fn cache_key(&self, pattern: &str, options: &SearchOptions) -> String {
105 format!(
106 "{}|{}|{}|{}|{:?}",
107 pattern,
108 options.case_sensitive,
109 options.whole_words,
110 options.max_results,
111 options.scope
112 )
113 }
114}
115
116impl Default for DocumentSearchImpl {
117 fn default() -> Self {
118 Self::new()
119 }
120}