From 15505f89912f72208d6cbefa3e3780904483e6b1 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Fri, 2 Dec 2016 17:41:29 -0800 Subject: [PATCH] DB path is now a constructor parameter of Index struct --- src/config.rs | 9 +++++++++ src/index.rs | 9 ++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index 6bc0e60..51e1557 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,6 +12,7 @@ use utils; use vfs::VfsConfig; const DEFAULT_CONFIG_FILE_NAME: &'static str = "polaris.toml"; +const INDEX_FILE_NAME: &'static str = "index.sqlite"; const CONFIG_SECRET: &'static str = "auth_secret"; const CONFIG_MOUNT_DIRS: &'static str = "mount_dirs"; const CONFIG_MOUNT_DIR_NAME: &'static str = "name"; @@ -29,6 +30,7 @@ const CONFIG_DDNS_PASSWORD: &'static str = "password"; #[derive(Debug)] pub enum ConfigError { IoError(io::Error), + CacheDirectoryError, ConfigDirectoryError, TOMLParseError, RegexError(regex::Error), @@ -98,6 +100,13 @@ impl Config { try!(config.parse_album_art_pattern(&parsed_config)); try!(config.parse_ddns(&parsed_config)); + let mut index_path = match utils::get_cache_root() { + Err(_) => return Err(ConfigError::CacheDirectoryError), + Ok(p) => p, + }; + index_path.push(INDEX_FILE_NAME); + config.index.path = index_path; + Ok(config) } diff --git a/src/index.rs b/src/index.rs index 2bb1292..36e907b 100644 --- a/src/index.rs +++ b/src/index.rs @@ -3,23 +3,22 @@ use regex::Regex; use sqlite; use sqlite::{Connection, State, Statement, Value}; use std::fs; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::sync::Arc; use std::thread; use std::time; use error::*; use metadata; -use utils; use vfs::Vfs; -const INDEX_FILE_NAME: &'static str = "index.sqlite"; 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 + pub path: PathBuf, } impl IndexConfig { @@ -27,6 +26,7 @@ impl IndexConfig { IndexConfig { sleep_duration: 60 * 30, // 30 minutes album_art_pattern: None, + path: Path::new(":memory:").to_path_buf(), } } } @@ -186,8 +186,7 @@ impl<'db> Drop for IndexBuilder<'db> { impl Index { pub fn new(vfs: Arc, config: &IndexConfig) -> Result { - let mut path = try!(utils::get_cache_root()); - path.push(INDEX_FILE_NAME); + let path = &config.path; println!("Reading or creating index from {}", path.to_string_lossy());