Define SearchActor
This commit is contained in:
		
							parent
							
								
									cfe097edf4
								
							
						
					
					
						commit
						0a148721de
					
				
							
								
								
									
										58
									
								
								plume-models/src/search/actor.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								plume-models/src/search/actor.rs
									
									
									
									
									
										Normal file
									
								
							| @ -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<Searcher>, | ||||
|     conn: DbPool, | ||||
| } | ||||
| 
 | ||||
| impl Actor for SearchActor { | ||||
|     type Msg = PostEvent; | ||||
| 
 | ||||
|     fn pre_start(&mut self, ctx: &Context<Self::Msg>) { | ||||
|         &POST_CHAN.tell( | ||||
|             Subscribe { | ||||
|                 actor: Box::new(ctx.myself()), | ||||
|                 topic: "*".into(), | ||||
|             }, | ||||
|             None, | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|     fn recv(&mut self, _ctx: &Context<Self::Msg>, 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<Searcher>, DbPool)> for SearchActor { | ||||
|     fn create_args((searcher, conn): (Arc<Searcher>, DbPool)) -> Self { | ||||
|         Self { searcher, conn } | ||||
|     } | ||||
| } | ||||
| @ -1,3 +1,4 @@ | ||||
| pub mod actor; | ||||
| mod query; | ||||
| mod searcher; | ||||
| mod tokenizer; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user