2021-03-23 18:00:50 +08:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
use tokio::sync::{mpsc, oneshot};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2021-04-02 19:44:35 +08:00
|
|
|
use crate::index_controller::IndexActorHandle;
|
|
|
|
|
2021-03-23 18:00:50 +08:00
|
|
|
use super::{
|
2021-04-14 23:53:12 +08:00
|
|
|
PayloadData, Result, UpdateActor, UpdateActorHandle, UpdateMeta, UpdateMsg, UpdateStatus,
|
2021-03-23 18:00:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct UpdateActorHandleImpl<D> {
|
|
|
|
sender: mpsc::Sender<UpdateMsg<D>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<D> UpdateActorHandleImpl<D>
|
|
|
|
where
|
|
|
|
D: AsRef<[u8]> + Sized + 'static + Sync + Send,
|
|
|
|
{
|
|
|
|
pub fn new<I>(
|
|
|
|
index_handle: I,
|
|
|
|
path: impl AsRef<Path>,
|
|
|
|
update_store_size: usize,
|
|
|
|
) -> anyhow::Result<Self>
|
|
|
|
where
|
2021-03-23 23:19:01 +08:00
|
|
|
I: IndexActorHandle + Clone + Send + Sync + 'static,
|
2021-03-23 18:00:50 +08:00
|
|
|
{
|
2021-04-14 23:53:12 +08:00
|
|
|
let path = path.as_ref().to_owned();
|
2021-03-23 18:00:50 +08:00
|
|
|
let (sender, receiver) = mpsc::channel(100);
|
2021-04-13 23:14:02 +08:00
|
|
|
let actor = UpdateActor::new(update_store_size, receiver, path, index_handle)?;
|
2021-03-23 18:00:50 +08:00
|
|
|
|
|
|
|
tokio::task::spawn(actor.run());
|
|
|
|
|
|
|
|
Ok(Self { sender })
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 19:44:35 +08:00
|
|
|
|
2021-03-23 18:00:50 +08:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<D> UpdateActorHandle for UpdateActorHandleImpl<D>
|
|
|
|
where
|
|
|
|
D: AsRef<[u8]> + Sized + 'static + Sync + Send,
|
|
|
|
{
|
|
|
|
type Data = D;
|
|
|
|
|
|
|
|
async fn get_all_updates_status(&self, uuid: Uuid) -> Result<Vec<UpdateStatus>> {
|
|
|
|
let (ret, receiver) = oneshot::channel();
|
|
|
|
let msg = UpdateMsg::ListUpdates { uuid, ret };
|
|
|
|
let _ = self.sender.send(msg).await;
|
|
|
|
receiver.await.expect("update actor killed.")
|
|
|
|
}
|
|
|
|
async fn update_status(&self, uuid: Uuid, id: u64) -> Result<UpdateStatus> {
|
|
|
|
let (ret, receiver) = oneshot::channel();
|
|
|
|
let msg = UpdateMsg::GetUpdate { uuid, id, ret };
|
|
|
|
let _ = self.sender.send(msg).await;
|
|
|
|
receiver.await.expect("update actor killed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn delete(&self, uuid: Uuid) -> Result<()> {
|
|
|
|
let (ret, receiver) = oneshot::channel();
|
|
|
|
let msg = UpdateMsg::Delete { uuid, ret };
|
|
|
|
let _ = self.sender.send(msg).await;
|
|
|
|
receiver.await.expect("update actor killed.")
|
|
|
|
}
|
|
|
|
|
2021-04-14 23:53:12 +08:00
|
|
|
async fn snapshot(&self, uuids: Vec<Uuid>, path: PathBuf) -> Result<()> {
|
2021-03-23 18:00:50 +08:00
|
|
|
let (ret, receiver) = oneshot::channel();
|
2021-04-14 23:53:12 +08:00
|
|
|
let msg = UpdateMsg::Snapshot { uuids, path, ret };
|
2021-03-23 18:00:50 +08:00
|
|
|
let _ = self.sender.send(msg).await;
|
|
|
|
receiver.await.expect("update actor killed.")
|
|
|
|
}
|
2021-04-02 19:44:35 +08:00
|
|
|
|
2021-04-14 23:53:12 +08:00
|
|
|
async fn get_size(&self) -> Result<u64> {
|
2021-04-09 20:41:24 +08:00
|
|
|
let (ret, receiver) = oneshot::channel();
|
2021-04-14 23:53:12 +08:00
|
|
|
let msg = UpdateMsg::GetSize { ret };
|
2021-04-09 20:41:24 +08:00
|
|
|
let _ = self.sender.send(msg).await;
|
|
|
|
receiver.await.expect("update actor killed.")
|
|
|
|
}
|
|
|
|
|
2021-04-02 19:44:35 +08:00
|
|
|
async fn update(
|
|
|
|
&self,
|
|
|
|
meta: UpdateMeta,
|
|
|
|
data: mpsc::Receiver<PayloadData<Self::Data>>,
|
|
|
|
uuid: Uuid,
|
|
|
|
) -> Result<UpdateStatus> {
|
|
|
|
let (ret, receiver) = oneshot::channel();
|
|
|
|
let msg = UpdateMsg::Update {
|
|
|
|
uuid,
|
|
|
|
data,
|
|
|
|
meta,
|
|
|
|
ret,
|
|
|
|
};
|
|
|
|
let _ = self.sender.send(msg).await;
|
|
|
|
receiver.await.expect("update actor killed.")
|
|
|
|
}
|
2021-03-23 18:00:50 +08:00
|
|
|
}
|