mirror of
https://github.com/agersant/polaris
synced 2024-12-02 17:49:10 +00:00
Loosen coupling between db and other modules
This commit is contained in:
parent
f9562b5c8c
commit
c314f7a452
3 changed files with 38 additions and 24 deletions
|
@ -12,7 +12,7 @@ use config::{MiscSettings, UserConfig};
|
|||
use ddns::{DDNSConfigSource, DDNSConfig};
|
||||
use errors::*;
|
||||
use user::*;
|
||||
use vfs::{MountPoint, Vfs};
|
||||
use vfs::{MountPoint, Vfs, VFSSource};
|
||||
|
||||
mod schema;
|
||||
|
||||
|
@ -24,6 +24,10 @@ pub use self::schema::*;
|
|||
const DB_MIGRATIONS_PATH: &'static str = "src/db/migrations";
|
||||
embed_migrations!("src/db/migrations");
|
||||
|
||||
pub trait ConnectionSource {
|
||||
fn get_connection(&self) -> Arc<Mutex<SqliteConnection>>;
|
||||
}
|
||||
|
||||
pub struct DB {
|
||||
connection: Arc<Mutex<SqliteConnection>>,
|
||||
}
|
||||
|
@ -47,10 +51,6 @@ impl DB {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_connection(&self) -> Arc<Mutex<SqliteConnection>> {
|
||||
self.connection.clone()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn migrate_down(&self) -> Result<()> {
|
||||
let connection = self.connection.lock().unwrap();
|
||||
|
@ -127,20 +127,6 @@ impl DB {
|
|||
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> {
|
||||
song.path = match vfs.real_to_virtual(Path::new(&song.path)) {
|
||||
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 {
|
||||
fn get_ddns_config(&self) -> Result<DDNSConfig> {
|
||||
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 {
|
||||
let config_path = Path::new("test/config.toml");
|
||||
let config = UserConfig::parse(&config_path).unwrap();
|
||||
|
|
12
src/index.rs
12
src/index.rs
|
@ -10,8 +10,10 @@ use std::thread;
|
|||
use std::time;
|
||||
|
||||
use config::{MiscSettings, UserConfig};
|
||||
use db::ConnectionSource;
|
||||
use db::DB;
|
||||
use db::{directories, misc_settings, songs};
|
||||
use vfs::VFSSource;
|
||||
use errors::*;
|
||||
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()?;
|
||||
|
||||
{
|
||||
|
@ -342,7 +344,7 @@ fn clean(db: &DB) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn populate(db: &DB) -> Result<()> {
|
||||
fn populate<T>(db: &T) -> Result<()> where T: ConnectionSource + VFSSource {
|
||||
let vfs = db.get_vfs()?;
|
||||
let mount_points = vfs.get_mount_points();
|
||||
let connection = db.get_connection();
|
||||
|
@ -364,7 +366,7 @@ fn populate(db: &DB) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update(db: &DB) -> Result<()> {
|
||||
pub fn update<T>(db: &T) -> Result<()> where T: ConnectionSource + VFSSource {
|
||||
let start = time::Instant::now();
|
||||
println!("Beginning library index update");
|
||||
clean(db)?;
|
||||
|
@ -374,7 +376,7 @@ pub fn update(db: &DB) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_loop(db: &DB) {
|
||||
pub fn update_loop<T>(db: &T) where T: ConnectionSource + VFSSource {
|
||||
loop {
|
||||
if let Err(e) = update(db) {
|
||||
println!("Error while updating index: {}", e);
|
||||
|
@ -400,7 +402,7 @@ pub fn update_loop(db: &DB) {
|
|||
}
|
||||
}
|
||||
|
||||
fn _get_test_db(name: &str) -> DB {
|
||||
fn _get_test_db(name: &str) -> DB {
|
||||
let config_path = Path::new("test/config.toml");
|
||||
let config = UserConfig::parse(&config_path).unwrap();
|
||||
|
||||
|
|
|
@ -5,6 +5,10 @@ use std::path::Path;
|
|||
use db::mount_points;
|
||||
use errors::*;
|
||||
|
||||
pub trait VFSSource {
|
||||
fn get_vfs(&self) -> Result<Vfs>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Insertable, Queryable)]
|
||||
#[table_name="mount_points"]
|
||||
pub struct MountPoint {
|
||||
|
|
Loading…
Reference in a new issue