Re-factored duplicated code

This commit is contained in:
Antoine Gersant 2020-01-19 01:01:43 -08:00
parent 2b30307488
commit 028633d0e6

View file

@ -341,43 +341,29 @@ pub fn populate(db: &DB) -> Result<()> {
Ok(())
}
fn insert_songs(receiver: Receiver<NewSong>, 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<NewDirectory>) {
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<NewSong>) {
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<NewDirectory>, 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<NewDirectory>, 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<NewSong>, 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);
}
}