2024-04-12 10:21:41 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-04-26 09:44:07 +08:00
|
|
|
use super::notifier::{SearchNotifier, SearchResultChanged, SearchResultReceiverRunner};
|
|
|
|
use crate::entities::{SearchFilterPB, SearchResultNotificationPB, SearchResultPB};
|
2024-04-12 10:21:41 +02:00
|
|
|
use flowy_error::FlowyResult;
|
|
|
|
use lib_dispatch::prelude::af_spawn;
|
2024-06-13 01:37:19 +02:00
|
|
|
use lib_infra::async_trait::async_trait;
|
|
|
|
use tokio::sync::broadcast;
|
|
|
|
|
2024-04-12 10:21:41 +02:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
|
|
|
pub enum SearchType {
|
|
|
|
Folder,
|
2024-06-13 01:37:19 +02:00
|
|
|
Document,
|
2024-04-12 10:21:41 +02:00
|
|
|
}
|
|
|
|
|
2024-06-13 01:37:19 +02:00
|
|
|
#[async_trait]
|
2024-04-12 10:21:41 +02:00
|
|
|
pub trait SearchHandler: Send + Sync + 'static {
|
|
|
|
/// returns the type of search this handler is responsible for
|
|
|
|
fn search_type(&self) -> SearchType;
|
2024-06-13 01:37:19 +02:00
|
|
|
|
2024-04-12 10:21:41 +02:00
|
|
|
/// performs a search and returns the results
|
2024-06-13 01:37:19 +02:00
|
|
|
async fn perform_search(
|
2024-04-23 15:46:57 +02:00
|
|
|
&self,
|
|
|
|
query: String,
|
|
|
|
filter: Option<SearchFilterPB>,
|
|
|
|
) -> FlowyResult<Vec<SearchResultPB>>;
|
2024-06-13 01:37:19 +02:00
|
|
|
|
2024-04-12 10:21:41 +02:00
|
|
|
/// returns the number of indexed objects
|
|
|
|
fn index_count(&self) -> u64;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The [SearchManager] is used to inject multiple [SearchHandler]'s
|
|
|
|
/// to delegate a search to all relevant handlers, and stream the result
|
|
|
|
/// to the client until the query has been fully completed.
|
|
|
|
///
|
|
|
|
pub struct SearchManager {
|
|
|
|
pub handlers: HashMap<SearchType, Arc<dyn SearchHandler>>,
|
|
|
|
notifier: SearchNotifier,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SearchManager {
|
|
|
|
pub fn new(handlers: Vec<Arc<dyn SearchHandler>>) -> Self {
|
|
|
|
let handlers: HashMap<SearchType, Arc<dyn SearchHandler>> = handlers
|
|
|
|
.into_iter()
|
|
|
|
.map(|handler| (handler.search_type(), handler))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Initialize Search Notifier
|
|
|
|
let (notifier, _) = broadcast::channel(100);
|
|
|
|
af_spawn(SearchResultReceiverRunner(Some(notifier.subscribe())).run());
|
|
|
|
|
|
|
|
Self { handlers, notifier }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_handler(&self, search_type: SearchType) -> Option<&Arc<dyn SearchHandler>> {
|
|
|
|
self.handlers.get(&search_type)
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:46:57 +02:00
|
|
|
pub fn perform_search(
|
|
|
|
&self,
|
|
|
|
query: String,
|
|
|
|
filter: Option<SearchFilterPB>,
|
|
|
|
channel: Option<String>,
|
|
|
|
) {
|
2024-04-12 10:21:41 +02:00
|
|
|
let max: usize = self.handlers.len();
|
|
|
|
let handlers = self.handlers.clone();
|
|
|
|
for (_, handler) in handlers {
|
|
|
|
let q = query.clone();
|
2024-04-23 15:46:57 +02:00
|
|
|
let f = filter.clone();
|
|
|
|
let ch = channel.clone();
|
2024-04-12 10:21:41 +02:00
|
|
|
let notifier = self.notifier.clone();
|
|
|
|
|
2024-06-13 01:37:19 +02:00
|
|
|
af_spawn(async move {
|
2024-06-25 21:56:58 +02:00
|
|
|
let res = handler.perform_search(q.clone(), f).await;
|
2024-04-12 10:21:41 +02:00
|
|
|
|
|
|
|
let items = res.unwrap_or_default();
|
2024-06-13 01:37:19 +02:00
|
|
|
|
2024-04-12 10:21:41 +02:00
|
|
|
let notification = SearchResultNotificationPB {
|
|
|
|
items,
|
2024-06-13 01:37:19 +02:00
|
|
|
sends: max as u64,
|
2024-04-23 15:46:57 +02:00
|
|
|
channel: ch,
|
2024-06-25 21:56:58 +02:00
|
|
|
query: q,
|
2024-04-12 10:21:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let _ = notifier.send(SearchResultChanged::SearchResultUpdate(notification));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|