MeiliSearch/meilisearch-lib/src/index_controller/dump_actor/handle_impl.rs

58 lines
1.6 KiB
Rust
Raw Normal View History

2021-05-31 16:03:39 +02:00
use std::path::Path;
2021-09-24 11:53:11 +02:00
use std::sync::Arc;
2021-05-31 16:40:59 +02:00
2021-05-10 20:25:09 +02:00
use tokio::sync::{mpsc, oneshot};
2021-09-24 11:53:11 +02:00
use crate::index_controller::index_resolver::HardStateIndexResolver;
2021-09-22 10:49:59 +02:00
use super::error::Result;
2021-06-15 17:39:07 +02:00
use super::{DumpActor, DumpActorHandle, DumpInfo, DumpMsg};
2021-05-31 16:40:59 +02:00
2021-05-10 20:25:09 +02:00
#[derive(Clone)]
pub struct DumpActorHandleImpl {
sender: mpsc::Sender<DumpMsg>,
}
#[async_trait::async_trait]
impl DumpActorHandle for DumpActorHandleImpl {
async fn create_dump(&self) -> Result<DumpInfo> {
2021-05-10 20:25:09 +02:00
let (ret, receiver) = oneshot::channel();
let msg = DumpMsg::CreateDump { ret };
let _ = self.sender.send(msg).await;
receiver.await.expect("IndexActor has been killed")
}
async fn dump_info(&self, uid: String) -> Result<DumpInfo> {
2021-05-10 20:25:09 +02:00
let (ret, receiver) = oneshot::channel();
let msg = DumpMsg::DumpInfo { ret, uid };
let _ = self.sender.send(msg).await;
receiver.await.expect("IndexActor has been killed")
}
}
impl DumpActorHandleImpl {
pub fn new(
path: impl AsRef<Path>,
analytics_path: impl AsRef<Path>,
2021-09-24 11:53:11 +02:00
index_resolver: Arc<HardStateIndexResolver>,
2021-09-22 11:52:29 +02:00
update: crate::index_controller::updates::UpdateSender,
2021-05-31 16:40:59 +02:00
index_db_size: usize,
update_db_size: usize,
2021-06-15 17:39:07 +02:00
) -> anyhow::Result<Self> {
2021-05-10 20:25:09 +02:00
let (sender, receiver) = mpsc::channel(10);
2021-05-31 16:03:39 +02:00
let actor = DumpActor::new(
receiver,
2021-09-24 11:53:11 +02:00
index_resolver,
2021-05-31 16:03:39 +02:00
update,
path,
analytics_path,
2021-05-31 16:03:39 +02:00
index_db_size,
update_db_size,
);
2021-05-10 20:25:09 +02:00
tokio::task::spawn(actor.run());
2021-05-26 20:42:09 +02:00
2021-05-10 20:25:09 +02:00
Ok(Self { sender })
}
}