Loosen coupling between db and other modules

This commit is contained in:
Antoine Gersant 2017-07-01 12:26:14 -07:00
parent f9562b5c8c
commit c314f7a452
3 changed files with 38 additions and 24 deletions

View file

@ -12,7 +12,7 @@ use config::{MiscSettings, UserConfig};
use ddns::{DDNSConfigSource, DDNSConfig}; use ddns::{DDNSConfigSource, DDNSConfig};
use errors::*; use errors::*;
use user::*; use user::*;
use vfs::{MountPoint, Vfs}; use vfs::{MountPoint, Vfs, VFSSource};
mod schema; mod schema;
@ -24,6 +24,10 @@ pub use self::schema::*;
const DB_MIGRATIONS_PATH: &'static str = "src/db/migrations"; const DB_MIGRATIONS_PATH: &'static str = "src/db/migrations";
embed_migrations!("src/db/migrations"); embed_migrations!("src/db/migrations");
pub trait ConnectionSource {
fn get_connection(&self) -> Arc<Mutex<SqliteConnection>>;
}
pub struct DB { pub struct DB {
connection: Arc<Mutex<SqliteConnection>>, connection: Arc<Mutex<SqliteConnection>>,
} }
@ -47,10 +51,6 @@ impl DB {
Ok(()) Ok(())
} }
pub fn get_connection(&self) -> Arc<Mutex<SqliteConnection>> {
self.connection.clone()
}
#[allow(dead_code)] #[allow(dead_code)]
fn migrate_down(&self) -> Result<()> { fn migrate_down(&self) -> Result<()> {
let connection = self.connection.lock().unwrap(); let connection = self.connection.lock().unwrap();
@ -127,20 +127,6 @@ impl DB {
index::update_loop(self); index::update_loop(self);
} }
pub fn get_vfs(&self) -> Result<Vfs> {
use self::mount_points::dsl::*;
let mut vfs = Vfs::new();
let connection = self.connection.lock().unwrap();
let connection = connection.deref();
let points: Vec<MountPoint> = mount_points
.select((source, name))
.get_results(connection)?;
for point in points {
vfs.mount(&Path::new(&point.source), &point.name)?;
}
Ok(vfs)
}
fn virtualize_song(&self, vfs: &Vfs, mut song: Song) -> Option<Song> { fn virtualize_song(&self, vfs: &Vfs, mut song: Song) -> Option<Song> {
song.path = match vfs.real_to_virtual(Path::new(&song.path)) { song.path = match vfs.real_to_virtual(Path::new(&song.path)) {
Ok(p) => p.to_string_lossy().into_owned(), Ok(p) => p.to_string_lossy().into_owned(),
@ -267,6 +253,12 @@ impl DB {
} }
} }
impl ConnectionSource for DB {
fn get_connection(&self) -> Arc<Mutex<SqliteConnection>> {
self.connection.clone()
}
}
impl DDNSConfigSource for DB { impl DDNSConfigSource for DB {
fn get_ddns_config(&self) -> Result<DDNSConfig> { fn get_ddns_config(&self) -> Result<DDNSConfig> {
use self::ddns_config::dsl::*; use self::ddns_config::dsl::*;
@ -278,6 +270,22 @@ impl DDNSConfigSource for DB {
} }
} }
impl VFSSource for DB {
fn get_vfs(&self) -> Result<Vfs> {
use self::mount_points::dsl::*;
let mut vfs = Vfs::new();
let connection = self.connection.lock().unwrap();
let connection = connection.deref();
let points: Vec<MountPoint> = mount_points
.select((source, name))
.get_results(connection)?;
for point in points {
vfs.mount(&Path::new(&point.source), &point.name)?;
}
Ok(vfs)
}
}
fn _get_test_db(name: &str) -> DB { fn _get_test_db(name: &str) -> DB {
let config_path = Path::new("test/config.toml"); let config_path = Path::new("test/config.toml");
let config = UserConfig::parse(&config_path).unwrap(); let config = UserConfig::parse(&config_path).unwrap();

View file

@ -10,8 +10,10 @@ use std::thread;
use std::time; use std::time;
use config::{MiscSettings, UserConfig}; use config::{MiscSettings, UserConfig};
use db::ConnectionSource;
use db::DB; use db::DB;
use db::{directories, misc_settings, songs}; use db::{directories, misc_settings, songs};
use vfs::VFSSource;
use errors::*; use errors::*;
use metadata; use metadata;
@ -277,7 +279,7 @@ impl<'conn> IndexBuilder<'conn> {
} }
} }
fn clean(db: &DB) -> Result<()> { fn clean<T>(db: &T) -> Result<()> where T: ConnectionSource + VFSSource {
let vfs = db.get_vfs()?; let vfs = db.get_vfs()?;
{ {
@ -342,7 +344,7 @@ fn clean(db: &DB) -> Result<()> {
Ok(()) Ok(())
} }
fn populate(db: &DB) -> Result<()> { fn populate<T>(db: &T) -> Result<()> where T: ConnectionSource + VFSSource {
let vfs = db.get_vfs()?; let vfs = db.get_vfs()?;
let mount_points = vfs.get_mount_points(); let mount_points = vfs.get_mount_points();
let connection = db.get_connection(); let connection = db.get_connection();
@ -364,7 +366,7 @@ fn populate(db: &DB) -> Result<()> {
Ok(()) Ok(())
} }
pub fn update(db: &DB) -> Result<()> { pub fn update<T>(db: &T) -> Result<()> where T: ConnectionSource + VFSSource {
let start = time::Instant::now(); let start = time::Instant::now();
println!("Beginning library index update"); println!("Beginning library index update");
clean(db)?; clean(db)?;
@ -374,7 +376,7 @@ pub fn update(db: &DB) -> Result<()> {
Ok(()) Ok(())
} }
pub fn update_loop(db: &DB) { pub fn update_loop<T>(db: &T) where T: ConnectionSource + VFSSource {
loop { loop {
if let Err(e) = update(db) { if let Err(e) = update(db) {
println!("Error while updating index: {}", e); println!("Error while updating index: {}", e);

View file

@ -5,6 +5,10 @@ use std::path::Path;
use db::mount_points; use db::mount_points;
use errors::*; use errors::*;
pub trait VFSSource {
fn get_vfs(&self) -> Result<Vfs>;
}
#[derive(Debug, Deserialize, Insertable, Queryable)] #[derive(Debug, Deserialize, Insertable, Queryable)]
#[table_name="mount_points"] #[table_name="mount_points"]
pub struct MountPoint { pub struct MountPoint {