Removed unecessary index object

This commit is contained in:
Antoine Gersant 2017-07-01 11:07:36 -07:00
parent ce90377b2d
commit 143e1f6761
3 changed files with 124 additions and 135 deletions

View file

@ -242,24 +242,7 @@ impl<'db> IndexBuilder<'db> {
} }
} }
pub struct Index {} fn clean(db: &DB) -> Result<()> {
impl Index {
pub fn new() -> Index {
Index {}
}
pub fn update_index(&self, db: &DB) -> Result<()> {
let start = time::Instant::now();
println!("Beginning library index update");
self.clean(db)?;
self.populate(db)?;
println!("Library index update took {} seconds",
start.elapsed().as_secs());
Ok(())
}
fn clean(&self, db: &DB) -> Result<()> {
let vfs = db.get_vfs()?; let vfs = db.get_vfs()?;
{ {
@ -322,9 +305,9 @@ impl Index {
} }
Ok(()) Ok(())
} }
fn populate(&self, db: &DB) -> Result<()> { fn populate(db: &DB) -> Result<()> {
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();
@ -344,11 +327,21 @@ impl Index {
builder.flush_songs()?; builder.flush_songs()?;
builder.flush_directories()?; builder.flush_directories()?;
Ok(()) Ok(())
} }
pub fn update_loop(&self, db: &DB) { pub fn update(db: &DB) -> Result<()> {
let start = time::Instant::now();
println!("Beginning library index update");
clean(db)?;
populate(db)?;
println!("Library index update took {} seconds",
start.elapsed().as_secs());
Ok(())
}
pub fn update_loop(db: &DB) {
loop { loop {
if let Err(e) = self.update_index(db) { if let Err(e) = update(db) {
println!("Error while updating index: {}", e); println!("Error while updating index: {}", e);
} }
{ {
@ -370,7 +363,6 @@ impl Index {
thread::sleep(time::Duration::from_secs(sleep_duration as u64)); thread::sleep(time::Duration::from_secs(sleep_duration as u64));
} }
} }
}
} }
fn _get_test_db(name: &str) -> DB { fn _get_test_db(name: &str) -> DB {
@ -394,9 +386,8 @@ fn test_populate() {
use db::models::*; use db::models::*;
let db = _get_test_db("populate.sqlite"); let db = _get_test_db("populate.sqlite");
let index = db.get_index(); update(&db).unwrap();
index.update_index(&db).unwrap(); update(&db).unwrap(); // Check that subsequent updates don't run into conflicts
index.update_index(&db).unwrap(); // Check that subsequent updates don't run into conflicts
let connection = db.get_connection(); let connection = db.get_connection();
let connection = connection.lock().unwrap(); let connection = connection.lock().unwrap();
@ -424,8 +415,7 @@ fn test_metadata() {
artwork_path.push("Folder.png"); artwork_path.push("Folder.png");
let db = _get_test_db("metadata.sqlite"); let db = _get_test_db("metadata.sqlite");
let index = db.get_index(); update(&db).unwrap();
index.update_index(&db).unwrap();
let connection = db.get_connection(); let connection = db.get_connection();
let connection = connection.lock().unwrap(); let connection = connection.lock().unwrap();

View file

@ -18,7 +18,6 @@ mod index;
mod models; mod models;
mod schema; mod schema;
pub use self::index::Index;
pub use self::models::*; pub use self::models::*;
#[allow(dead_code)] #[allow(dead_code)]
@ -27,7 +26,6 @@ embed_migrations!("src/db/migrations");
pub struct DB { pub struct DB {
connection: Arc<Mutex<SqliteConnection>>, connection: Arc<Mutex<SqliteConnection>>,
index: Index,
} }
impl DB { impl DB {
@ -35,10 +33,7 @@ impl DB {
println!("Database file path: {}", path.to_string_lossy()); println!("Database file path: {}", path.to_string_lossy());
let connection = let connection =
Arc::new(Mutex::new(SqliteConnection::establish(&path.to_string_lossy())?)); Arc::new(Mutex::new(SqliteConnection::establish(&path.to_string_lossy())?));
let db = DB { let db = DB { connection: connection.clone() };
connection: connection.clone(),
index: Index::new(),
};
db.init()?; db.init()?;
Ok(db) Ok(db)
} }
@ -56,10 +51,6 @@ impl DB {
self.connection.clone() self.connection.clone()
} }
pub fn get_index(&self) -> &Index {
&self.index
}
#[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();
@ -128,6 +119,14 @@ impl DB {
vfs.virtual_to_real(virtual_path) vfs.virtual_to_real(virtual_path)
} }
pub fn index_update(&self) -> Result<()> {
index::update(self)
}
pub fn index_update_loop(&self) {
index::update_loop(self);
}
fn get_vfs(&self) -> Result<Vfs> { fn get_vfs(&self) -> Result<Vfs> {
use self::mount_points::dsl::*; use self::mount_points::dsl::*;
let mut vfs = Vfs::new(); let mut vfs = Vfs::new();
@ -315,7 +314,7 @@ fn test_browse_top_level() {
root_path.push("root"); root_path.push("root");
let db = _get_test_db("browse_top_level.sqlite"); let db = _get_test_db("browse_top_level.sqlite");
db.get_index().update_index(&db).unwrap(); db.index_update().unwrap();
let results = db.browse(Path::new("")).unwrap(); let results = db.browse(Path::new("")).unwrap();
assert_eq!(results.len(), 1); assert_eq!(results.len(), 1);
@ -336,7 +335,7 @@ fn test_browse() {
tobokegao_path.push("Tobokegao"); tobokegao_path.push("Tobokegao");
let db = _get_test_db("browse.sqlite"); let db = _get_test_db("browse.sqlite");
db.get_index().update_index(&db).unwrap(); db.index_update().unwrap();
let results = db.browse(Path::new("root")).unwrap(); let results = db.browse(Path::new("root")).unwrap();
assert_eq!(results.len(), 2); assert_eq!(results.len(), 2);
@ -353,7 +352,7 @@ fn test_browse() {
#[test] #[test]
fn test_flatten() { fn test_flatten() {
let db = _get_test_db("flatten.sqlite"); let db = _get_test_db("flatten.sqlite");
db.get_index().update_index(&db).unwrap(); db.index_update().unwrap();
let results = db.flatten(Path::new("root")).unwrap(); let results = db.flatten(Path::new("root")).unwrap();
assert_eq!(results.len(), 12); assert_eq!(results.len(), 12);
} }
@ -361,7 +360,7 @@ fn test_flatten() {
#[test] #[test]
fn test_random() { fn test_random() {
let db = _get_test_db("random.sqlite"); let db = _get_test_db("random.sqlite");
db.get_index().update_index(&db).unwrap(); db.index_update().unwrap();
let results = db.get_random_albums(1).unwrap(); let results = db.get_random_albums(1).unwrap();
assert_eq!(results.len(), 1); assert_eq!(results.len(), 1);
} }
@ -369,7 +368,7 @@ fn test_random() {
#[test] #[test]
fn test_recent() { fn test_recent() {
let db = _get_test_db("recent.sqlite"); let db = _get_test_db("recent.sqlite");
db.get_index().update_index(&db).unwrap(); db.index_update().unwrap();
let results = db.get_recent_albums(2).unwrap(); let results = db.get_recent_albums(2).unwrap();
assert_eq!(results.len(), 2); assert_eq!(results.len(), 2);
assert!(results[0].date_added >= results[1].date_added); assert!(results[0].date_added >= results[1].date_added);

View file

@ -124,7 +124,7 @@ fn run() -> Result<()> {
let db_ref = db.clone(); let db_ref = db.clone();
std::thread::spawn(move || { std::thread::spawn(move || {
let db = db_ref.deref(); let db = db_ref.deref();
db.get_index().update_loop(db); db.index_update_loop();
}); });
// Mount API // Mount API