diff --git a/plume-models/src/search/actor.rs b/plume-models/src/search/actor.rs new file mode 100644 index 00000000..9a939f93 --- /dev/null +++ b/plume-models/src/search/actor.rs @@ -0,0 +1,58 @@ +use super::Searcher; +use crate::{db_conn::DbPool, posts::PostEvent, POST_CHAN}; +use riker::actors::{Actor, ActorFactoryArgs, Context, Sender, Subscribe, Tell}; +use std::sync::Arc; +use tracing::error; + +pub struct SearchActor { + searcher: Arc, + conn: DbPool, +} + +impl Actor for SearchActor { + type Msg = PostEvent; + + fn pre_start(&mut self, ctx: &Context) { + &POST_CHAN.tell( + Subscribe { + actor: Box::new(ctx.myself()), + topic: "*".into(), + }, + None, + ); + } + + fn recv(&mut self, _ctx: &Context, msg: Self::Msg, _sender: Sender) { + use PostEvent::*; + + match msg { + PostPublished(post) => { + let conn = self.conn.get(); + if conn.is_ok() { + self.searcher + .add_document(&conn.unwrap(), &post) + .unwrap_or_else(|e| error!("{:?}", e)); + } else { + error!("Failed to get database connection"); + } + } + PostUpdated(post) => { + let conn = self.conn.get(); + if conn.is_ok() { + self.searcher + .update_document(&conn.unwrap(), &post) + .unwrap_or_else(|e| error!("{:?}", e)); + } else { + error!("Failed to get database connection"); + } + } + PostDeleted(post) => self.searcher.delete_document(&post), + } + } +} + +impl ActorFactoryArgs<(Arc, DbPool)> for SearchActor { + fn create_args((searcher, conn): (Arc, DbPool)) -> Self { + Self { searcher, conn } + } +} diff --git a/plume-models/src/search/mod.rs b/plume-models/src/search/mod.rs index 83b9bf62..33de8d49 100644 --- a/plume-models/src/search/mod.rs +++ b/plume-models/src/search/mod.rs @@ -1,3 +1,4 @@ +pub mod actor; mod query; mod searcher; mod tokenizer;