2021-10-17 11:46:56 +00:00
|
|
|
use std::{any::type_name, collections::HashMap, sync::{Arc, Mutex, atomic::{AtomicUsize, Ordering}}};
|
2021-09-12 14:33:52 +00:00
|
|
|
|
2021-10-17 11:46:56 +00:00
|
|
|
use log::debug;
|
|
|
|
use rayon::{iter::{IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator, IntoParallelRefMutIterator, ParallelIterator}, slice::ParallelSliceMut};
|
2021-10-03 21:07:19 +00:00
|
|
|
|
2021-10-14 19:29:16 +00:00
|
|
|
use super::matcher::PrefixMatcher;
|
|
|
|
|
|
|
|
pub type SourceRef = Box<dyn Source>;
|
|
|
|
pub type MatcherRef = Box<dyn Matcher>;
|
|
|
|
|
2021-10-03 21:07:19 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2021-09-12 14:33:52 +00:00
|
|
|
pub struct Entry {
|
2021-10-03 21:07:19 +00:00
|
|
|
pub source: &'static str,
|
2021-09-12 14:33:52 +00:00
|
|
|
pub name: String,
|
|
|
|
pub description: String,
|
2021-10-03 21:07:19 +00:00
|
|
|
pub identifier: u64,
|
2021-09-12 14:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Action {
|
|
|
|
Primary,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Source: Send + Sync {
|
2021-10-03 21:07:19 +00:00
|
|
|
fn name(&self) -> &'static str;
|
|
|
|
fn entries(&mut self) -> Vec<Entry>;
|
2021-09-12 14:33:52 +00:00
|
|
|
fn action(&self, entry: &Entry, action: Action);
|
|
|
|
}
|
|
|
|
|
2021-10-14 19:29:16 +00:00
|
|
|
pub trait Matcher: Send + Sync {
|
|
|
|
fn try_match(&self, haystack: &str, needle: &str) -> Option<(f64, Vec<usize>)>;
|
|
|
|
}
|
|
|
|
|
2021-09-12 14:33:52 +00:00
|
|
|
pub struct Roftl {
|
2021-10-14 19:29:16 +00:00
|
|
|
sources: Vec<SourceRef>,
|
|
|
|
matcher: MatcherRef,
|
2021-09-12 14:33:52 +00:00
|
|
|
entries: Vec<Entry>,
|
2021-10-03 21:07:19 +00:00
|
|
|
narrow_map: Mutex<HashMap<usize, usize>>
|
2021-09-12 14:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Roftl {
|
2021-10-14 19:29:16 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Roftl {
|
|
|
|
sources: vec![],
|
|
|
|
matcher: PrefixMatcher::new(),
|
|
|
|
entries: vec![],
|
|
|
|
narrow_map: Mutex::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_source(mut self, source: SourceRef) -> Self {
|
2021-10-03 21:07:19 +00:00
|
|
|
if self.sources.par_iter().any(|s| s.name().eq(source.name())) {
|
2021-10-14 19:29:16 +00:00
|
|
|
panic! {"Source with name '{}' already exists", source.name()}
|
2021-09-12 14:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.sources.push(source);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-10-14 19:29:16 +00:00
|
|
|
pub fn with_matcher(mut self, matcher: MatcherRef) -> Self {
|
|
|
|
self.matcher = matcher;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn source(&mut self) {
|
2021-09-12 14:33:52 +00:00
|
|
|
self.entries = self
|
|
|
|
.sources
|
2021-10-03 21:07:19 +00:00
|
|
|
.par_iter_mut()
|
2021-10-14 19:29:16 +00:00
|
|
|
.flat_map(|s| s.entries())
|
2021-09-12 14:33:52 +00:00
|
|
|
.collect();
|
2021-10-03 21:07:19 +00:00
|
|
|
|
|
|
|
println!("Entries {:?}", self.entries);
|
2021-09-12 14:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn narrow(&self, input: &str) -> Vec<&Entry> {
|
2021-10-14 19:29:16 +00:00
|
|
|
let count = Arc::new(AtomicUsize::new(0));
|
2021-10-03 21:07:19 +00:00
|
|
|
self.narrow_map.lock().unwrap().clear();
|
|
|
|
|
2021-10-17 11:46:56 +00:00
|
|
|
let mut scored_entries: Vec<(f64, usize, &Entry, Vec<usize>)> = self.entries
|
2021-10-03 21:07:19 +00:00
|
|
|
.par_iter()
|
|
|
|
.enumerate()
|
2021-10-17 11:46:56 +00:00
|
|
|
.filter_map(|(idx, entry)| {
|
|
|
|
let match_result = self.matcher.try_match(&entry.name, input);
|
|
|
|
return match_result.map(|(score, indices)| (score, idx, entry, indices) )
|
2021-10-03 21:07:19 +00:00
|
|
|
})
|
2021-10-17 11:46:56 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
scored_entries
|
|
|
|
.par_sort_by(|e1, e2| e1.0.partial_cmp(&e2.0).unwrap());
|
|
|
|
|
|
|
|
scored_entries
|
|
|
|
.par_iter()
|
|
|
|
.for_each(|se| {
|
|
|
|
self.narrow_map.lock().unwrap().insert(count.fetch_add(1, Ordering::Relaxed), se.1);
|
|
|
|
});
|
|
|
|
|
|
|
|
scored_entries
|
|
|
|
.par_iter()
|
|
|
|
.map(|se| se.2)
|
2021-09-12 14:33:52 +00:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2021-10-03 21:07:19 +00:00
|
|
|
pub fn action(&self, entry_id: i32, action: Action) {
|
|
|
|
println!("NarrowMap {:?}", self.narrow_map.lock().unwrap());
|
|
|
|
|
|
|
|
let real_id = *self.narrow_map.lock().unwrap().get(&((entry_id-1) as usize)).unwrap();
|
|
|
|
let entry = self.entries.get(real_id).unwrap();
|
|
|
|
|
|
|
|
let source = self
|
|
|
|
.sources
|
|
|
|
.iter()
|
|
|
|
.find(|&s| s.name().eq(entry.source))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
source.action(entry, action);
|
2021-09-12 14:33:52 +00:00
|
|
|
}
|
|
|
|
}
|