dd_sds/scanner/regex_rule/
regex_cache_store.rs1use crate::scanner::regex_rule::regex_store::{RegexCacheKey, SharedRegex};
2use crate::SharedPool;
3use lazy_static::lazy_static;
4use regex_automata::meta::Regex as MetaRegex;
5use slotmap::SecondaryMap;
6use std::sync::Arc;
7extern crate num_cpus;
8
9lazy_static! {
10 static ref REGEX_CACHE_STORE: Arc<SharedPool<Box<RegexCaches>>> = Arc::new(SharedPool::new(
11 Box::new(|| Box::new(RegexCaches::new())),
12 num_cpus::get()
13 ));
14}
15
16pub fn access_regex_caches<T>(func: impl FnOnce(&mut RegexCaches) -> T) -> T {
17 let mut caches = REGEX_CACHE_STORE.get();
20 func(caches.get_ref())
21}
22
23pub struct RegexCaches {
24 map: SecondaryMap<RegexCacheKey, regex_automata::meta::Cache>,
25}
26
27impl RegexCaches {
28 pub fn new() -> Self {
29 Self {
30 map: SecondaryMap::new(),
31 }
32 }
33
34 pub fn get(&mut self, shared_regex: &SharedRegex) -> &mut regex_automata::meta::Cache {
35 self.raw_get(shared_regex.cache_key, &shared_regex.regex)
36 }
37
38 pub(super) fn raw_get(
39 &mut self,
40 key: RegexCacheKey,
41 regex: &MetaRegex,
42 ) -> &mut regex_automata::meta::Cache {
43 self.map
44 .entry(key)
45 .unwrap()
46 .or_insert_with(|| regex.create_cache())
47 }
48}