From 3e875b05c93736ecfe6cfea8014627bae6f394b9 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Fri, 2 Apr 2021 22:34:38 +0300 Subject: [PATCH] refactor(http): use macros to generate actor handlers --- .../index_actor/handle_impl.rs | 143 ++++-------------- .../update_actor/handle_impl.rs | 86 ++++------- .../uuid_resolver/handle_impl.rs | 78 +++------- 3 files changed, 85 insertions(+), 222 deletions(-) diff --git a/meilisearch-http/src/index_controller/index_actor/handle_impl.rs b/meilisearch-http/src/index_controller/index_actor/handle_impl.rs index dba0f9e6..1af8220c 100644 --- a/meilisearch-http/src/index_controller/index_actor/handle_impl.rs +++ b/meilisearch-http/src/index_controller/index_actor/handle_impl.rs @@ -3,126 +3,20 @@ use std::path::{Path, PathBuf}; use tokio::sync::{mpsc, oneshot}; use uuid::Uuid; -use super::{ - IndexActor, IndexActorHandle, IndexMeta, IndexMsg, MapIndexStore, Result, UpdateResult, -}; use crate::index::{Document, SearchQuery, SearchResult, Settings}; use crate::index_controller::IndexSettings; use crate::index_controller::{updates::Processing, UpdateMeta}; +use super::{ + IndexActor, IndexActorHandle, IndexMeta, IndexMsg, MapIndexStore, Result, UpdateResult, +}; + #[derive(Clone)] pub struct IndexActorHandleImpl { read_sender: mpsc::Sender, write_sender: mpsc::Sender, } -#[async_trait::async_trait] -impl IndexActorHandle for IndexActorHandleImpl { - async fn create_index(&self, uuid: Uuid, primary_key: Option) -> Result { - let (ret, receiver) = oneshot::channel(); - let msg = IndexMsg::CreateIndex { - ret, - uuid, - primary_key, - }; - let _ = self.read_sender.send(msg).await; - receiver.await.expect("IndexActor has been killed") - } - - async fn update( - &self, - meta: Processing, - data: std::fs::File, - ) -> anyhow::Result { - let (ret, receiver) = oneshot::channel(); - let msg = IndexMsg::Update { ret, meta, data }; - let _ = self.read_sender.send(msg).await; - Ok(receiver.await.expect("IndexActor has been killed")?) - } - - async fn search(&self, uuid: Uuid, query: SearchQuery) -> Result { - let (ret, receiver) = oneshot::channel(); - let msg = IndexMsg::Search { uuid, query, ret }; - let _ = self.read_sender.send(msg).await; - Ok(receiver.await.expect("IndexActor has been killed")?) - } - - async fn settings(&self, uuid: Uuid) -> Result { - let (ret, receiver) = oneshot::channel(); - let msg = IndexMsg::Settings { uuid, ret }; - let _ = self.read_sender.send(msg).await; - Ok(receiver.await.expect("IndexActor has been killed")?) - } - - async fn documents( - &self, - uuid: Uuid, - offset: usize, - limit: usize, - attributes_to_retrieve: Option>, - ) -> Result> { - let (ret, receiver) = oneshot::channel(); - let msg = IndexMsg::Documents { - uuid, - ret, - offset, - attributes_to_retrieve, - limit, - }; - let _ = self.read_sender.send(msg).await; - Ok(receiver.await.expect("IndexActor has been killed")?) - } - - async fn document( - &self, - uuid: Uuid, - doc_id: String, - attributes_to_retrieve: Option>, - ) -> Result { - let (ret, receiver) = oneshot::channel(); - let msg = IndexMsg::Document { - uuid, - ret, - doc_id, - attributes_to_retrieve, - }; - let _ = self.read_sender.send(msg).await; - Ok(receiver.await.expect("IndexActor has been killed")?) - } - - async fn delete(&self, uuid: Uuid) -> Result<()> { - let (ret, receiver) = oneshot::channel(); - let msg = IndexMsg::Delete { uuid, ret }; - let _ = self.read_sender.send(msg).await; - Ok(receiver.await.expect("IndexActor has been killed")?) - } - - async fn get_index_meta(&self, uuid: Uuid) -> Result { - let (ret, receiver) = oneshot::channel(); - let msg = IndexMsg::GetMeta { uuid, ret }; - let _ = self.read_sender.send(msg).await; - Ok(receiver.await.expect("IndexActor has been killed")?) - } - - async fn update_index(&self, uuid: Uuid, index_settings: IndexSettings) -> Result { - let (ret, receiver) = oneshot::channel(); - let msg = IndexMsg::UpdateIndex { - uuid, - index_settings, - ret, - }; - let _ = self.read_sender.send(msg).await; - Ok(receiver.await.expect("IndexActor has been killed")?) - } - - async fn snapshot(&self, uuid: Uuid, path: PathBuf) -> Result<()> { - let (ret, receiver) = oneshot::channel(); - let msg = IndexMsg::Snapshot { uuid, path, ret }; - let _ = self.read_sender.send(msg).await; - Ok(receiver.await.expect("IndexActor has been killed")?) - } -} - impl IndexActorHandleImpl { pub fn new(path: impl AsRef, index_size: usize) -> anyhow::Result { let (read_sender, read_receiver) = mpsc::channel(100); @@ -137,3 +31,32 @@ impl IndexActorHandleImpl { }) } } + +macro_rules! handler { + ($({$fn_name:ident, $message:ident, [$($arg:ident: $arg_type:ty),*], $return:ty}),*) => { + #[async_trait::async_trait] + impl IndexActorHandle for IndexActorHandleImpl { + $( + async fn $fn_name(&self, $($arg: $arg_type, )*) -> $return { + let (ret, receiver) = oneshot::channel(); + let msg = IndexMsg::$message { $($arg,)* ret }; + let _ = self.read_sender.send(msg).await; + Ok(receiver.await.expect("IndexActor has been killed")?) + } + )* + } + }; +} + +handler!( + {update, Update, [meta: Processing, data: std::fs::File], anyhow::Result}, + {create_index, CreateIndex, [uuid: Uuid, primary_key: Option], Result}, + {search, Search, [uuid: Uuid, query: SearchQuery], Result}, + {settings, Settings, [uuid: Uuid], Result}, + {documents, Documents, [uuid: Uuid, offset: usize, limit: usize, attributes_to_retrieve: Option>], Result>}, + {document, Document, [uuid: Uuid, doc_id: String, attributes_to_retrieve: Option>], Result}, + {delete, Delete, [uuid: Uuid], Result<()>}, + {get_index_meta, GetMeta, [uuid: Uuid], Result}, + {update_index, UpdateIndex, [uuid: Uuid, index_settings: IndexSettings], Result}, + {snapshot, Snapshot, [uuid: Uuid, path: PathBuf], Result<()>} +); diff --git a/meilisearch-http/src/index_controller/update_actor/handle_impl.rs b/meilisearch-http/src/index_controller/update_actor/handle_impl.rs index 59f67fbe..a84b8c05 100644 --- a/meilisearch-http/src/index_controller/update_actor/handle_impl.rs +++ b/meilisearch-http/src/index_controller/update_actor/handle_impl.rs @@ -3,11 +3,12 @@ use std::path::{Path, PathBuf}; use tokio::sync::{mpsc, oneshot}; use uuid::Uuid; +use crate::index_controller::IndexActorHandle; + use super::{ MapUpdateStoreStore, PayloadData, Result, UpdateActor, UpdateActorHandle, UpdateMeta, UpdateMsg, UpdateStatus, }; -use crate::index_controller::IndexActorHandle; #[derive(Clone)] pub struct UpdateActorHandleImpl { @@ -36,61 +37,32 @@ where Ok(Self { sender }) } } -#[async_trait::async_trait] -impl UpdateActorHandle for UpdateActorHandleImpl -where - D: AsRef<[u8]> + Sized + 'static + Sync + Send, -{ - type Data = D; - - async fn update( - &self, - meta: UpdateMeta, - data: mpsc::Receiver>, - uuid: Uuid, - ) -> Result { - 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.") - } - async fn get_all_updates_status(&self, uuid: Uuid) -> Result> { - 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 { - 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.") - } - - async fn create(&self, uuid: Uuid) -> Result<()> { - let (ret, receiver) = oneshot::channel(); - let msg = UpdateMsg::Create { uuid, ret }; - let _ = self.sender.send(msg).await; - receiver.await.expect("update actor killed.") - } - - async fn snapshot(&self, uuid: Uuid, path: PathBuf) -> Result<()> { - let (ret, receiver) = oneshot::channel(); - let msg = UpdateMsg::Snapshot { uuid, path, ret }; - let _ = self.sender.send(msg).await; - receiver.await.expect("update actor killed.") - } +macro_rules! handler { + ($({$fn_name:ident, $message:ident, [$($arg:ident: $arg_type:ty),*], $return:ty}),*) => { + #[async_trait::async_trait] + impl UpdateActorHandle for UpdateActorHandleImpl + where + D: AsRef<[u8]> + Sized + 'static + Sync + Send, + { + type Data = D; + $( + async fn $fn_name(&self, $($arg: $arg_type, )*) -> $return { + let (ret, receiver) = oneshot::channel(); + let msg = UpdateMsg::$message { $($arg,)* ret }; + let _ = self.sender.send(msg).await; + Ok(receiver.await.expect("UpdateActor has been killed")?) + } + )* + } + }; } + +handler!( + {update, Update, [meta: UpdateMeta, data: mpsc::Receiver>, uuid: Uuid], Result}, + {get_all_updates_status, ListUpdates, [uuid: Uuid], Result>}, + {update_status, GetUpdate, [uuid: Uuid, id: u64], Result}, + {delete, Delete, [uuid: Uuid], Result<()>}, + {create, Create, [uuid: Uuid], Result<()>}, + {snapshot, Snapshot, [uuid: Uuid, path: PathBuf], Result<()>} +); diff --git a/meilisearch-http/src/index_controller/uuid_resolver/handle_impl.rs b/meilisearch-http/src/index_controller/uuid_resolver/handle_impl.rs index f8625b37..024cab21 100644 --- a/meilisearch-http/src/index_controller/uuid_resolver/handle_impl.rs +++ b/meilisearch-http/src/index_controller/uuid_resolver/handle_impl.rs @@ -20,59 +20,27 @@ impl UuidResolverHandleImpl { } } -#[async_trait::async_trait] -impl UuidResolverHandle for UuidResolverHandleImpl { - async fn get(&self, name: String) -> Result { - let (ret, receiver) = oneshot::channel(); - let msg = UuidResolveMsg::Get { uid: name, ret }; - let _ = self.sender.send(msg).await; - Ok(receiver - .await - .expect("Uuid resolver actor has been killed")?) - } - - async fn create(&self, name: String) -> anyhow::Result { - let (ret, receiver) = oneshot::channel(); - let msg = UuidResolveMsg::Create { uid: name, ret }; - let _ = self.sender.send(msg).await; - Ok(receiver - .await - .expect("Uuid resolver actor has been killed")?) - } - - async fn delete(&self, name: String) -> anyhow::Result { - let (ret, receiver) = oneshot::channel(); - let msg = UuidResolveMsg::Delete { uid: name, ret }; - let _ = self.sender.send(msg).await; - Ok(receiver - .await - .expect("Uuid resolver actor has been killed")?) - } - - async fn list(&self) -> anyhow::Result> { - let (ret, receiver) = oneshot::channel(); - let msg = UuidResolveMsg::List { ret }; - let _ = self.sender.send(msg).await; - Ok(receiver - .await - .expect("Uuid resolver actor has been killed")?) - } - - async fn insert(&self, name: String, uuid: Uuid) -> anyhow::Result<()> { - let (ret, receiver) = oneshot::channel(); - let msg = UuidResolveMsg::Insert { ret, name, uuid }; - let _ = self.sender.send(msg).await; - Ok(receiver - .await - .expect("Uuid resolver actor has been killed")?) - } - - async fn snapshot(&self, path: PathBuf) -> Result> { - 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")?) - } +macro_rules! handler { + ($({$fn_name:ident, $message:ident, [$($arg:ident: $arg_type:ty),*], $return:ty}),*) => { + #[async_trait::async_trait] + impl UuidResolverHandle for UuidResolverHandleImpl { + $( + async fn $fn_name(&self, $($arg: $arg_type, )*) -> $return { + let (ret, receiver) = oneshot::channel(); + let msg = UuidResolveMsg::$message { $($arg,)* ret }; + let _ = self.sender.send(msg).await; + Ok(receiver.await.expect("UuidResolverActor has been killed")?) + } + )* + } + }; } + +handler!( + {get, Get, [uid: String], Result}, + {create, Create, [uid: String], anyhow::Result}, + {delete, Delete, [uid: String], anyhow::Result}, + {list, List, [], anyhow::Result>}, + {insert, Insert, [name: String, uuid: Uuid], anyhow::Result<()>}, + {snapshot, SnapshotRequest, [path: PathBuf], Result>} +);