meilisearch/meilisearch-lib/src/index_controller/updates/message.rs

110 lines
2.8 KiB
Rust
Raw Normal View History

2021-09-22 17:52:29 +08:00
use std::path::PathBuf;
use tokio::sync::{mpsc, oneshot};
use uuid::Uuid;
2021-09-27 22:48:03 +08:00
use crate::index::Index;
2021-09-22 17:52:29 +08:00
use super::error::Result;
use super::{Update, UpdateStatus, UpdateStoreInfo};
2021-09-22 21:07:04 +08:00
#[derive(Debug)]
2021-09-22 17:52:29 +08:00
pub enum UpdateMsg {
Update {
uuid: Uuid,
update: Update,
ret: oneshot::Sender<Result<UpdateStatus>>,
},
ListUpdates {
uuid: Uuid,
ret: oneshot::Sender<Result<Vec<UpdateStatus>>>,
},
GetUpdate {
uuid: Uuid,
ret: oneshot::Sender<Result<UpdateStatus>>,
id: u64,
},
2021-09-29 00:10:09 +08:00
DeleteIndex {
2021-09-22 17:52:29 +08:00
uuid: Uuid,
ret: oneshot::Sender<Result<()>>,
},
Snapshot {
2021-09-27 22:48:03 +08:00
indexes: Vec<Index>,
2021-09-22 17:52:29 +08:00
path: PathBuf,
ret: oneshot::Sender<Result<()>>,
},
Dump {
2021-09-28 17:59:55 +08:00
indexes: Vec<Index>,
2021-09-22 17:52:29 +08:00
path: PathBuf,
ret: oneshot::Sender<Result<()>>,
},
GetInfo {
ret: oneshot::Sender<Result<UpdateStoreInfo>>,
},
}
impl UpdateMsg {
2021-09-27 22:48:03 +08:00
pub async fn snapshot(sender: &mpsc::Sender<Self>, path: PathBuf, indexes: Vec<Index>) -> Result<()> {
let (ret, rcv) = oneshot::channel();
let msg = Self::Snapshot { path, indexes, ret };
sender.send(msg).await?;
rcv.await?
}
2021-09-22 17:52:29 +08:00
pub async fn dump(
sender: &mpsc::Sender<Self>,
2021-09-28 17:59:55 +08:00
indexes: Vec<Index>,
2021-09-22 17:52:29 +08:00
path: PathBuf,
) -> Result<()> {
let (ret, rcv) = oneshot::channel();
2021-09-28 17:59:55 +08:00
let msg = Self::Dump { path, indexes, ret };
2021-09-22 17:52:29 +08:00
sender.send(msg).await?;
rcv.await?
}
pub async fn update(
sender: &mpsc::Sender<Self>,
uuid: Uuid,
update: Update,
) -> Result<UpdateStatus> {
let (ret, rcv) = oneshot::channel();
2021-09-27 22:48:03 +08:00
let msg = Self::Update { uuid, update, ret };
2021-09-22 17:52:29 +08:00
sender.send(msg).await?;
rcv.await?
}
pub async fn get_update(
sender: &mpsc::Sender<Self>,
uuid: Uuid,
id: u64,
) -> Result<UpdateStatus> {
let (ret, rcv) = oneshot::channel();
2021-09-27 22:48:03 +08:00
let msg = Self::GetUpdate { uuid, id, ret };
2021-09-22 17:52:29 +08:00
sender.send(msg).await?;
rcv.await?
}
pub async fn list_updates(
sender: &mpsc::Sender<Self>,
uuid: Uuid,
) -> Result<Vec<UpdateStatus>> {
let (ret, rcv) = oneshot::channel();
2021-09-27 22:48:03 +08:00
let msg = Self::ListUpdates { uuid, ret };
2021-09-22 17:52:29 +08:00
sender.send(msg).await?;
rcv.await?
}
2021-09-27 22:48:03 +08:00
pub async fn get_info(sender: &mpsc::Sender<Self>) -> Result<UpdateStoreInfo> {
2021-09-22 17:52:29 +08:00
let (ret, rcv) = oneshot::channel();
2021-09-27 22:48:03 +08:00
let msg = Self::GetInfo { ret };
2021-09-22 17:52:29 +08:00
sender.send(msg).await?;
rcv.await?
}
2021-09-29 00:10:09 +08:00
pub async fn delete(sender: &mpsc::Sender<Self>, uuid: Uuid) -> Result<()> {
let (ret, rcv) = oneshot::channel();
let msg = Self::DeleteIndex { ret, uuid };
sender.send(msg).await?;
rcv.await?
}
2021-09-22 17:52:29 +08:00
}