From 996dc309f7cfdd2eb83b8e3ff5f00a19ca215eb3 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Wed, 6 Jan 2021 23:40:56 +0900 Subject: [PATCH] Make Searcher::open_or_create() accept path and tokenizers --- plume-models/src/search/searcher.rs | 17 ++++++++--------- src/main.rs | 5 ++++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/plume-models/src/search/searcher.rs b/plume-models/src/search/searcher.rs index 2aa5cf2b..61608bea 100644 --- a/plume-models/src/search/searcher.rs +++ b/plume-models/src/search/searcher.rs @@ -1,6 +1,6 @@ use crate::{ config::SearchTokenizerConfig, instance::Instance, posts::Post, schema::posts, - search::query::PlumeQuery, tags::Tag, Connection, Error, Result, CONFIG, + search::query::PlumeQuery, tags::Tag, Connection, Error, Result, }; use chrono::{Datelike, Utc}; use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl}; @@ -69,16 +69,15 @@ impl Searcher { schema_builder.build() } - pub fn open_or_recreate() -> Self { - let mut open_searcher = Self::open(&CONFIG.search_index, &CONFIG.search_tokenizers); + pub fn open_or_recreate(path: &dyn AsRef, tokenizers: &SearchTokenizerConfig) -> Self { + let mut open_searcher = Self::open(path, tokenizers); if let Err(Error::Search(SearcherError::InvalidIndexDataError)) = open_searcher { - if Self::create(&CONFIG.search_index, &CONFIG.search_tokenizers).is_err() { - let current_path = Path::new(&CONFIG.search_index); - let backup_path = format!("{}.{}", ¤t_path.display(), Utc::now().timestamp()); + if Self::create(path, tokenizers).is_err() { + let backup_path = format!("{}.{}", path.as_ref().display(), Utc::now().timestamp()); let backup_path = Path::new(&backup_path); - fs::rename(current_path, backup_path) + fs::rename(path, backup_path) .expect("main: error on backing up search index directory for recreating"); - if Self::create(&CONFIG.search_index, &CONFIG.search_tokenizers).is_ok() { + if Self::create(path, tokenizers).is_ok() { if fs::remove_dir_all(backup_path).is_err() { warn!( "error on removing backup directory: {}. it remains", @@ -89,7 +88,7 @@ impl Searcher { panic!("main: error on recreating search index in new index format. remove search index and run `plm search init` manually"); } } - open_searcher = Self::open(&CONFIG.search_index, &CONFIG.search_tokenizers); + open_searcher = Self::open(path, tokenizers); } #[allow(clippy::match_wild_err_arm)] let searcher = match open_searcher { diff --git a/src/main.rs b/src/main.rs index 6c548637..e6654ada 100755 --- a/src/main.rs +++ b/src/main.rs @@ -100,7 +100,10 @@ Then try to restart Plume. } let workpool = ScheduledThreadPool::with_name("worker {}", num_cpus::get()); // we want a fast exit here, so - let searcher = Arc::new(UnmanagedSearcher::open_or_recreate()); + let searcher = Arc::new(UnmanagedSearcher::open_or_recreate( + &CONFIG.search_index, + &CONFIG.search_tokenizers, + )); let commiter = searcher.clone(); workpool.execute_with_fixed_delay( Duration::from_secs(5),