Removed code duplication and combined refresh_feed and refresh_db.

This commit is contained in:
Jordan Petridis 2017-11-04 13:36:25 +02:00
parent 7e8f39119d
commit 7a1b272d9e
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
8 changed files with 34 additions and 45 deletions

1
Cargo.lock generated
View File

@ -588,6 +588,7 @@ dependencies = [
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"loggerv 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "loggerv 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"open 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "open 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]

View File

@ -89,9 +89,15 @@ fn insert_return_episode(con: &SqliteConnection, ep: &NewEpisode) -> Result<Epis
Ok(dbqueries::load_episode_from_uri(con, ep.uri.unwrap())?) Ok(dbqueries::load_episode_from_uri(con, ep.uri.unwrap())?)
} }
pub fn index_loop(db: &Database) -> Result<()> { pub fn full_index_loop(db: &Database) -> Result<()> {
let mut f = fetch_feeds(db)?; let mut f = fetch_feeds(db)?;
index_feed(&db, &mut f);
info!("Indexing done.");
Ok(())
}
pub fn index_feed(db: &Database, f: &mut [Feed]) {
f.par_iter_mut() f.par_iter_mut()
.for_each(|&mut Feed(ref mut req, ref source)| { .for_each(|&mut Feed(ref mut req, ref source)| {
let e = complete_index_from_source(req, source, db); let e = complete_index_from_source(req, source, db);
@ -100,8 +106,6 @@ pub fn index_loop(db: &Database) -> Result<()> {
error!("Error msg: {}", e.unwrap_err()); error!("Error msg: {}", e.unwrap_err());
}; };
}); });
info!("Indexing done.");
Ok(())
} }
pub fn complete_index_from_source( pub fn complete_index_from_source(
@ -274,10 +278,10 @@ mod tests {
index_source(&tempdb, &NewSource::new_with_uri(feed)).unwrap() index_source(&tempdb, &NewSource::new_with_uri(feed)).unwrap()
}); });
index_loop(&db).unwrap(); full_index_loop(&db).unwrap();
// Run again to cover Unique constrains erros. // Run again to cover Unique constrains erros.
index_loop(&db).unwrap(); full_index_loop(&db).unwrap();
} }
#[test] #[test]

View File

@ -15,6 +15,7 @@ loggerv = "0.4"
log = "0.3" log = "0.3"
open = "1.2" open = "1.2"
dissolve = "0.2" dissolve = "0.2"
rayon = "0.8"
hammond-data = {path = "../hammond-data"} hammond-data = {path = "../hammond-data"}
hammond-downloader = {path = "../hammond-downloader"} hammond-downloader = {path = "../hammond-downloader"}

View File

@ -49,7 +49,7 @@ pub fn get_headerbar(db: &Database, stack: &gtk::Stack) -> gtk::HeaderBar {
// FIXME: There appears to be a memmory leak here. // FIXME: There appears to be a memmory leak here.
refresh_button.connect_clicked(clone!(stack, db => move |_| { refresh_button.connect_clicked(clone!(stack, db => move |_| {
utils::refresh_db(&db, &stack); utils::refresh_feed(&db, &stack, None);
})); }));
header header
@ -62,9 +62,9 @@ fn on_add_bttn_clicked(db: &Database, stack: &gtk::Stack, url: &str) {
}; };
info!("{:?} feed added", url); info!("{:?} feed added", url);
if let Ok(mut s) = source { if let Ok(s) = source {
// update the db // update the db
utils::refresh_feed(db, stack, &mut s); utils::refresh_feed(db, stack, Some(Box::new(vec![s])));
} else { } else {
error!("Expected Error, feed probably already exists."); error!("Expected Error, feed probably already exists.");
error!("Error: {:?}", source.unwrap_err()); error!("Error: {:?}", source.unwrap_err());

View File

@ -12,6 +12,7 @@ extern crate hammond_downloader;
extern crate log; extern crate log;
extern crate loggerv; extern crate loggerv;
extern crate open; extern crate open;
extern crate rayon;
use log::LogLevel; use log::LogLevel;
use hammond_data::dbcheckup; use hammond_data::dbcheckup;

View File

@ -1,9 +1,8 @@
use glib; use glib;
use gtk; use gtk;
// use gtk::prelude::*; use rayon::prelude::*;
use hammond_data; use hammond_data::index_feed;
use hammond_data::index_feed::Feed;
use hammond_data::models::Source; use hammond_data::models::Source;
use hammond_data::index_feed::Database; use hammond_data::index_feed::Database;
@ -19,49 +18,32 @@ thread_local!(
gtk::Stack, gtk::Stack,
Receiver<bool>)>> = RefCell::new(None)); Receiver<bool>)>> = RefCell::new(None));
pub fn refresh_db(db: &Database, stack: &gtk::Stack) { pub fn refresh_feed(db: &Database, stack: &gtk::Stack, source: Option<Box<Vec<Source>>>) {
// Create a async channel.
let (sender, receiver) = channel(); let (sender, receiver) = channel();
// Pass the desired arguments into the Local Thread Storage.
GLOBAL.with(clone!(db, stack => move |global| { GLOBAL.with(clone!(db, stack => move |global| {
*global.borrow_mut() = Some((db, stack, receiver)); *global.borrow_mut() = Some((db, stack, receiver));
})); }));
// The implementation of how this is done is probably terrible but it works!.
// TODO: add timeout option and error reporting. // TODO: add timeout option and error reporting.
thread::spawn(clone!(db => move || { thread::spawn(clone!(db => move || {
let t = hammond_data::index_feed::index_loop(&db); let feeds = {
if t.is_err() { if let Some(mut boxed_vec) = source {
error!("Error While trying to update the database."); let f = boxed_vec
error!("Error msg: {}", t.unwrap_err()); .par_iter_mut()
}; .filter_map(|mut s| {
sender.send(true).expect("Couldn't send data to channel");; index_feed::refresh_source(&db, &mut s).ok()
})
// http://gtk-rs.org/docs/glib/source/fn.idle_add.html .collect();
glib::idle_add(refresh_podcasts_view); Ok(f)
})); } else {
index_feed::fetch_feeds(&db)
} }
pub fn refresh_feed(db: &Database, stack: &gtk::Stack, source: &mut Source) {
let (sender, receiver) = channel();
GLOBAL.with(clone!(db, stack => move |global| {
*global.borrow_mut() = Some((db, stack, receiver));
}));
// TODO: add timeout option and error reporting.
thread::spawn(clone!(db, source => move || {
let foo_ = hammond_data::index_feed::refresh_source(&db, &mut source.clone());
if let Ok(x) = foo_ {
let Feed(mut req, s) = x;
let s = hammond_data::index_feed::complete_index_from_source(&mut req, &s, &db);
if s.is_err() {
error!("Error While trying to update the database.");
error!("Error msg: {}", s.unwrap_err());
}; };
if let Ok(mut x) = feeds {
index_feed::index_feed(&db, &mut x);
sender.send(true).expect("Couldn't send data to channel");; sender.send(true).expect("Couldn't send data to channel");;
glib::idle_add(refresh_podcasts_view); glib::idle_add(refresh_podcasts_view);
}; };

View File

@ -199,7 +199,7 @@ fn receive() -> glib::Continue {
pub fn episodes_listbox(db: &Database, pd: &Podcast) -> Result<gtk::ListBox> { pub fn episodes_listbox(db: &Database, pd: &Podcast) -> Result<gtk::ListBox> {
let conn = db.lock().unwrap(); let conn = db.lock().unwrap();
let mut episodes = dbqueries::get_pd_episodes(&conn, &pd)?; let mut episodes = dbqueries::get_pd_episodes(&conn, pd)?;
drop(conn); drop(conn);
let list = gtk::ListBox::new(); let list = gtk::ListBox::new();

View File

@ -30,7 +30,7 @@ fn podcast_widget(db: &Database, stack: &gtk::Stack, pd: &Podcast) -> gtk::Box {
})); }));
title_label.set_text(pd.title()); title_label.set_text(pd.title());
let listbox = episodes_listbox(db, &pd); let listbox = episodes_listbox(db, pd);
if let Ok(l) = listbox { if let Ok(l) = listbox {
view.add(&l); view.add(&l);
} }