1use crate::io::docs::LocalDocuments;
2use crate::io::server::Server;
3pub mod content;
4pub mod path;
5
6use std::collections::{HashMap, HashSet};
7use std::ops::Range;
8use uuid::Uuid;
9
10use crate::model::errors::LbResult;
11use crate::model::file::File;
12
13pub use content::ContentSearcher;
14pub use path::PathSearcher;
15
16#[derive(Debug, Clone, Default)]
18pub struct SearchResult {
19 pub id: Uuid,
20 pub filename: String,
21 pub parent_path: String,
22 pub is_folder: bool,
23 pub path_indices: Vec<u32>,
25 pub path_matches: Vec<ContentMatch>,
26 pub content_matches: Vec<ContentMatch>,
28}
29
30#[derive(Debug, Clone)]
32pub enum SearchFilter {
33 Path(String),
35}
36
37#[derive(Debug, Clone)]
39pub struct ContentMatch {
40 pub range: Range<usize>,
42 pub exact: bool,
44}
45
46use crate::Lb;
47
48const MAX_SEARCHED_FILES: usize = 100;
49
50impl<S: Server, D: LocalDocuments> Lb<S, D> {
51 pub async fn path_searcher(&self) -> PathSearcher {
52 PathSearcher::new(self).await
53 }
54
55 #[instrument(level = "debug", skip(self), err(Debug))]
56 pub async fn search_accepted(&self, id: Uuid) -> LbResult<()> {
57 let mut tx = self.begin_tx().await;
58 let db = tx.db();
59
60 let mut searched: Vec<Uuid> = db
61 .searched_files
62 .as_slice()
63 .iter()
64 .filter(|searched| **searched != id)
65 .copied()
66 .collect();
67 searched.push(id);
68 let dropped = searched.len().saturating_sub(MAX_SEARCHED_FILES);
69
70 db.searched_files.clear()?;
71 for id in &searched[dropped..] {
72 db.searched_files.push(*id)?;
73 }
74 tx.end();
75
76 Ok(())
77 }
78}
79
80pub(crate) fn build_descendants(files: &[File]) -> HashMap<Uuid, Vec<Uuid>> {
83 let parent_of: HashMap<Uuid, Uuid> = files.iter().map(|f| (f.id, f.parent)).collect();
84
85 let mut descendants: HashMap<Uuid, Vec<Uuid>> = HashMap::new();
86 for f in files {
87 let mut node = f.id;
88 while let Some(&parent) = parent_of.get(&node) {
89 if parent == node {
90 break; }
92 descendants.entry(parent).or_default().push(f.id);
93 node = parent;
94 }
95 }
96
97 descendants
98}
99
100pub(crate) fn resolve_filter(
103 filter: Option<SearchFilter>, path_to_id: &HashMap<String, Uuid>,
104 descendants: &HashMap<Uuid, Vec<Uuid>>,
105) -> Option<HashSet<Uuid>> {
106 filter.map(|SearchFilter::Path(path)| {
107 path_to_id
108 .get(&path)
109 .and_then(|id| descendants.get(id))
110 .map(|ids| ids.iter().copied().collect())
111 .unwrap_or_default()
112 })
113}