use std::path::PathBuf; use tokio::sync::{mpsc, oneshot}; use uuid::Uuid; use super::error::Result; use crate::index::{Checked, Document, SearchQuery, SearchResult, Settings}; use crate::index_controller::updates::status::{Failed, Processed, Processing}; use crate::index_controller::{IndexSettings, IndexStats}; use super::IndexMeta; #[allow(clippy::large_enum_variant)] #[derive(Debug)] pub enum IndexMsg { CreateIndex { uuid: Uuid, primary_key: Option, ret: oneshot::Sender>, }, Update { uuid: Uuid, meta: Processing, ret: oneshot::Sender>>, }, Search { uuid: Uuid, query: SearchQuery, ret: oneshot::Sender>, }, Settings { uuid: Uuid, ret: oneshot::Sender>>, }, Documents { uuid: Uuid, attributes_to_retrieve: Option>, offset: usize, limit: usize, ret: oneshot::Sender>>, }, Document { uuid: Uuid, attributes_to_retrieve: Option>, doc_id: String, ret: oneshot::Sender>, }, Delete { uuid: Uuid, ret: oneshot::Sender>, }, GetMeta { uuid: Uuid, ret: oneshot::Sender>, }, UpdateIndex { uuid: Uuid, index_settings: IndexSettings, ret: oneshot::Sender>, }, Snapshot { uuid: Uuid, path: PathBuf, ret: oneshot::Sender>, }, Dump { uuid: Uuid, path: PathBuf, ret: oneshot::Sender>, }, GetStats { uuid: Uuid, ret: oneshot::Sender>, }, } impl IndexMsg { pub async fn search( sender: &mpsc::Sender, uuid: Uuid, query: SearchQuery, ) -> Result { let (ret, rcv) = oneshot::channel(); let msg = Self::Search { ret, uuid, query, }; sender.send(msg).await?; rcv.await? } pub async fn update_index( sender: &mpsc::Sender, uuid: Uuid, index_settings: IndexSettings, ) -> Result { let (ret, rcv) = oneshot::channel(); let msg = Self::UpdateIndex { ret, uuid, index_settings, }; sender.send(msg).await?; rcv.await? } pub async fn create_index( sender: &mpsc::Sender, uuid: Uuid, primary_key: Option, ) -> Result { let (ret, rcv) = oneshot::channel(); let msg = Self::CreateIndex { ret, uuid, primary_key, }; sender.send(msg).await?; rcv.await? } pub async fn index_meta(sender: &mpsc::Sender, uuid: Uuid) -> Result { let (ret, rcv) = oneshot::channel(); let msg = Self::GetMeta { ret, uuid }; sender.send(msg).await?; rcv.await? } pub async fn index_stats(sender: &mpsc::Sender, uuid: Uuid) -> Result { let (ret, rcv) = oneshot::channel(); let msg = Self::GetStats { ret, uuid }; sender.send(msg).await?; rcv.await? } pub async fn settings(sender: &mpsc::Sender, uuid: Uuid) -> Result> { let (ret, rcv) = oneshot::channel(); let msg = Self::Settings { ret, uuid }; sender.send(msg).await?; rcv.await? } pub async fn documents( sender: &mpsc::Sender, uuid: Uuid, offset: usize, limit: usize, attributes_to_retrieve: Option>, ) -> Result> { let (ret, rcv) = oneshot::channel(); let msg = Self::Documents { ret, uuid, attributes_to_retrieve, offset, limit, }; sender.send(msg).await?; rcv.await? } pub async fn document( sender: &mpsc::Sender, uuid: Uuid, attributes_to_retrieve: Option>, doc_id: String, ) -> Result { let (ret, rcv) = oneshot::channel(); let msg = Self::Document { ret, uuid, attributes_to_retrieve, doc_id, }; sender.send(msg).await?; rcv.await? } pub async fn update(sender: &mpsc::Sender, uuid: Uuid, meta: Processing) -> Result> { let (ret, rcv) = oneshot::channel(); let msg = Self::Update { ret, uuid, meta, }; sender.send(msg).await?; rcv.await? } pub async fn snapshot(sender: &mpsc::Sender, uuid: Uuid, path: PathBuf) -> Result<()> { let (ret, rcv) = oneshot::channel(); let msg = Self::Snapshot { uuid, path, ret, }; sender.send(msg).await?; rcv.await? } pub async fn dump(sender: &mpsc::Sender, uuid: Uuid, path: PathBuf) -> Result<()> { let (ret, rcv) = oneshot::channel(); let msg = Self::Dump { uuid, ret, path, }; sender.send(msg).await?; rcv.await? } }