diff --git a/src/index/update.rs b/src/index/update.rs index d9aa7cc..1d08dd7 100644 --- a/src/index/update.rs +++ b/src/index/update.rs @@ -341,43 +341,29 @@ pub fn populate(db: &DB) -> Result<()> { Ok(()) } -fn insert_songs(receiver: Receiver, db: DB) { - let mut new_entries = Vec::new(); - new_entries.reserve_exact(INDEX_BUILDING_INSERT_BUFFER_SIZE); - - loop { - match receiver.recv() { - Ok(s) => { - new_entries.push(s); - if new_entries.len() >= INDEX_BUILDING_INSERT_BUFFER_SIZE { - if db.connect() - .and_then(|connection|{ - diesel::insert_into(songs::table) - .values(&new_entries) - .execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822 - .map_err(Error::new) - }) - .is_err() { - error!("Could not insert new songs in database"); - } - new_entries.clear(); - } - }, - Err(_) => break, - } +fn flush_directories(db: &DB, entries: &Vec) { + if db.connect() + .and_then(|connection|{ + diesel::insert_into(directories::table) + .values(entries) + .execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822 + .map_err(Error::new) + }) + .is_err() { + error!("Could not insert new directories in database"); } +} - if new_entries.len() > 0 { - if db.connect() - .and_then(|connection|{ - diesel::insert_into(songs::table) - .values(&new_entries) - .execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822 - .map_err(Error::new) - }) - .is_err() { - error!("Could not insert new songs in database"); - } +fn flush_songs(db: &DB, entries: &Vec) { + if db.connect() + .and_then(|connection|{ + diesel::insert_into(songs::table) + .values(entries) + .execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822 + .map_err(Error::new) + }) + .is_err() { + error!("Could not insert new songs in database"); } } @@ -390,16 +376,7 @@ fn insert_directories(receiver: Receiver, db: DB) { Ok(s) => { new_entries.push(s); if new_entries.len() >= INDEX_BUILDING_INSERT_BUFFER_SIZE { - if db.connect() - .and_then(|connection|{ - diesel::insert_into(directories::table) - .values(&new_entries) - .execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822 - .map_err(Error::new) - }) - .is_err() { - error!("Could not insert new directories in database"); - } + flush_directories(&db, &new_entries); new_entries.clear(); } }, @@ -408,15 +385,28 @@ fn insert_directories(receiver: Receiver, db: DB) { } if new_entries.len() > 0 { - if db.connect() - .and_then(|connection|{ - diesel::insert_into(directories::table) - .values(&new_entries) - .execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822 - .map_err(Error::new) - }) - .is_err() { - error!("Could not insert new directories in database"); - } + flush_directories(&db, &new_entries); + } +} + +fn insert_songs(receiver: Receiver, db: DB) { + let mut new_entries = Vec::new(); + new_entries.reserve_exact(INDEX_BUILDING_INSERT_BUFFER_SIZE); + + loop { + match receiver.recv() { + Ok(s) => { + new_entries.push(s); + if new_entries.len() >= INDEX_BUILDING_INSERT_BUFFER_SIZE { + flush_songs(&db, &new_entries); + new_entries.clear(); + } + }, + Err(_) => break, + } + } + + if new_entries.len() > 0 { + flush_songs(&db, &new_entries); } }