MeiliSearch/meilisearch-http/src/index_controller/uuid_resolver/handle_impl.rs

88 lines
2.9 KiB
Rust
Raw Normal View History

2021-04-22 10:14:29 +02:00
use std::collections::HashSet;
2021-03-23 11:00:50 +01:00
use std::path::{Path, PathBuf};
use tokio::sync::{mpsc, oneshot};
use uuid::Uuid;
2021-03-24 11:29:11 +01:00
use super::{HeedUuidStore, Result, UuidResolveMsg, UuidResolverActor, UuidResolverHandle};
2021-03-23 11:00:50 +01:00
#[derive(Clone)]
pub struct UuidResolverHandleImpl {
sender: mpsc::Sender<UuidResolveMsg>,
}
impl UuidResolverHandleImpl {
pub fn new(path: impl AsRef<Path>) -> Result<Self> {
2021-03-23 11:00:50 +01:00
let (sender, reveiver) = mpsc::channel(100);
let store = HeedUuidStore::new(path)?;
let actor = UuidResolverActor::new(reveiver, store);
tokio::spawn(actor.run());
Ok(Self { sender })
}
}
#[async_trait::async_trait]
2021-03-24 11:29:11 +01:00
impl UuidResolverHandle for UuidResolverHandleImpl {
2021-03-25 14:21:05 +01:00
async fn get(&self, name: String) -> Result<Uuid> {
2021-03-23 11:00:50 +01:00
let (ret, receiver) = oneshot::channel();
2021-03-25 14:21:05 +01:00
let msg = UuidResolveMsg::Get { uid: name, ret };
2021-03-23 11:00:50 +01:00
let _ = self.sender.send(msg).await;
Ok(receiver
.await
.expect("Uuid resolver actor has been killed")?)
}
async fn delete(&self, name: String) -> Result<Uuid> {
2021-03-23 11:00:50 +01:00
let (ret, receiver) = oneshot::channel();
2021-03-25 14:21:05 +01:00
let msg = UuidResolveMsg::Delete { uid: name, ret };
2021-03-23 11:00:50 +01:00
let _ = self.sender.send(msg).await;
Ok(receiver
.await
.expect("Uuid resolver actor has been killed")?)
}
async fn list(&self) -> Result<Vec<(String, Uuid)>> {
2021-03-23 11:00:50 +01:00
let (ret, receiver) = oneshot::channel();
2021-03-25 14:21:05 +01:00
let msg = UuidResolveMsg::List { ret };
2021-03-23 11:00:50 +01:00
let _ = self.sender.send(msg).await;
Ok(receiver
.await
.expect("Uuid resolver actor has been killed")?)
}
async fn insert(&self, name: String, uuid: Uuid) -> Result<()> {
2021-03-23 11:00:50 +01:00
let (ret, receiver) = oneshot::channel();
2021-03-25 14:21:05 +01:00
let msg = UuidResolveMsg::Insert { ret, name, uuid };
2021-03-23 11:00:50 +01:00
let _ = self.sender.send(msg).await;
Ok(receiver
.await
.expect("Uuid resolver actor has been killed")?)
}
2021-04-22 10:14:29 +02:00
async fn snapshot(&self, path: PathBuf) -> Result<HashSet<Uuid>> {
2021-03-23 11:00:50 +01:00
let (ret, receiver) = oneshot::channel();
let msg = UuidResolveMsg::SnapshotRequest { path, ret };
let _ = self.sender.send(msg).await;
Ok(receiver
.await
.expect("Uuid resolver actor has been killed")?)
}
async fn get_size(&self) -> Result<u64> {
let (ret, receiver) = oneshot::channel();
let msg = UuidResolveMsg::GetSize { ret };
let _ = self.sender.send(msg).await;
Ok(receiver
.await
.expect("Uuid resolver actor has been killed")?)
}
2021-05-24 16:05:43 +02:00
async fn dump(&self, path: PathBuf) -> Result<HashSet<Uuid>> {
let (ret, receiver) = oneshot::channel();
let msg = UuidResolveMsg::DumpRequest { ret, path };
let _ = self.sender.send(msg).await;
Ok(receiver
.await
.expect("Uuid resolver actor has been killed")?)
}
2021-03-23 11:00:50 +01:00
}