From 05e646a640292dc6b9b9b1a18170a2daeb3709bd Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sun, 13 Nov 2016 22:08:04 -0800 Subject: [PATCH] Configurable re-index period --- res/default_config.toml | 3 +++ src/config.rs | 23 ++++++++++++++++++++--- src/index.rs | 22 +++++++++++++++++++--- src/main.rs | 2 +- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/res/default_config.toml b/res/default_config.toml index 52c2501..c13c98a 100644 --- a/res/default_config.toml +++ b/res/default_config.toml @@ -4,6 +4,9 @@ auth_secret = 'Something very secret' # Pattern used to match album art matching a file album_art_pattern = '^Folder\.(png|jpg|jpeg)$' +# How often the library should be re-indexed (in seconds) +reindex_every_n_seconds = 43200 + # Directories where your music is stored [[mount_dirs]] source = 'C:/Users/your_name/Music' # Location of the directory on your computer diff --git a/src/config.rs b/src/config.rs index 4617f92..3c4c3ad 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,6 +7,7 @@ use toml; use collection::User; use ddns::DDNSConfig; +use index::IndexConfig; use vfs::VfsConfig; const CONFIG_SECRET: &'static str = "auth_secret"; @@ -17,6 +18,7 @@ const CONFIG_USERS: &'static str = "users"; const CONFIG_USER_NAME: &'static str = "name"; const CONFIG_USER_PASSWORD: &'static str = "password"; const CONFIG_ALBUM_ART_PATTERN: &'static str = "album_art_pattern"; +const CONFIG_INDEX_SLEEP_DURATION: &'static str = "reindex_every_n_seconds"; const CONFIG_DDNS: &'static str = "ydns"; const CONFIG_DDNS_HOST: &'static str = "host"; const CONFIG_DDNS_USERNAME: &'static str = "username"; @@ -28,6 +30,7 @@ pub enum ConfigError { TOMLParseError, RegexError(regex::Error), SecretParseError, + SleepDurationParseError, AlbumArtPatternParseError, UsersParseError, MountDirsParseError, @@ -51,7 +54,7 @@ pub struct Config { pub secret: String, pub vfs: VfsConfig, pub users: Vec, - pub album_art_pattern: Option, + pub index: IndexConfig, pub ddns: Option, } @@ -67,11 +70,12 @@ impl Config { secret: String::new(), vfs: VfsConfig::new(), users: Vec::new(), - album_art_pattern: None, + index: IndexConfig::new(), ddns: None, }; try!(config.parse_secret(&parsed_config)); + try!(config.parse_index_sleep_duration(&parsed_config)); try!(config.parse_mount_points(&parsed_config)); try!(config.parse_users(&parsed_config)); try!(config.parse_album_art_pattern(&parsed_config)); @@ -87,6 +91,19 @@ impl Config { Ok(()) } + fn parse_index_sleep_duration(&mut self, source: &toml::Table) -> Result<(), ConfigError> { + let sleep_duration = match source.get(CONFIG_INDEX_SLEEP_DURATION) { + Some(s) => s, + None => return Ok(()), + }; + let sleep_duration = match sleep_duration { + &toml::Value::Integer(s) => s as u64, + _ => return Err(ConfigError::SleepDurationParseError), + }; + self.index.sleep_duration = sleep_duration; + Ok(()) + } + fn parse_album_art_pattern(&mut self, source: &toml::Table) -> Result<(), ConfigError> { let pattern = match source.get(CONFIG_ALBUM_ART_PATTERN) { Some(s) => s, @@ -96,7 +113,7 @@ impl Config { &toml::Value::String(ref s) => s, _ => return Err(ConfigError::AlbumArtPatternParseError), }; - self.album_art_pattern = Some(try!(regex::Regex::new(pattern))); + self.index.album_art_pattern = Some(try!(regex::Regex::new(pattern))); Ok(()) } diff --git a/src/index.rs b/src/index.rs index f19f4b0..e42de20 100644 --- a/src/index.rs +++ b/src/index.rs @@ -18,10 +18,25 @@ use vfs::Vfs; const INDEX_BUILDING_INSERT_BUFFER_SIZE: usize = 250; // Insertions in each transaction const INDEX_LOCK_TIMEOUT: usize = 1000; // In milliseconds +pub struct IndexConfig { + pub album_art_pattern: Option, + pub sleep_duration: u64, // in seconds +} + +impl IndexConfig { + pub fn new() -> IndexConfig { + IndexConfig { + sleep_duration: 60 * 30, // 30 minutes + album_art_pattern: None, + } + } +} + pub struct Index { path: String, vfs: Arc, album_art_pattern: Option, + sleep_duration: u64, } struct SongTags { @@ -257,13 +272,14 @@ impl<'db> Drop for IndexBuilder<'db> { impl Index { pub fn new(path: &Path, vfs: Arc, - album_art_pattern: &Option) + config: &IndexConfig) -> Result { let index = Index { path: path.to_string_lossy().deref().to_string(), vfs: vfs, - album_art_pattern: album_art_pattern.clone(), + album_art_pattern: config.album_art_pattern.clone(), + sleep_duration: config.sleep_duration, }; if path.exists() { @@ -500,7 +516,7 @@ impl Index { let db = self.connect(); self.update_index(&db); } - thread::sleep(time::Duration::from_secs(60 * 20)); // TODO expose in configuration + thread::sleep(time::Duration::from_secs(self.sleep_duration)); } } diff --git a/src/main.rs b/src/main.rs index de2cdc5..b4e79de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,7 +70,7 @@ fn main() { // Init index println!("Starting up index"); let index_path = path::Path::new(INDEX_FILE_NAME); - let index = Arc::new(index::Index::new(&index_path, vfs.clone(), &config.album_art_pattern) + let index = Arc::new(index::Index::new(&index_path, vfs.clone(), &config.index) .unwrap()); let index_ref = index.clone(); std::thread::spawn(move || index_ref.run());