mirror of
https://github.com/agersant/polaris
synced 2024-11-10 02:04:13 +00:00
Re-factored duplicated code
This commit is contained in:
parent
2b30307488
commit
028633d0e6
1 changed files with 45 additions and 55 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue