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)",
"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)",
"rayon 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[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())?)
}
pub fn index_loop(db: &Database) -> Result<()> {
pub fn full_index_loop(db: &Database) -> Result<()> {
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()
.for_each(|&mut Feed(ref mut req, ref source)| {
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());
};
});
info!("Indexing done.");
Ok(())
}
pub fn complete_index_from_source(
@ -274,10 +278,10 @@ mod tests {
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.
index_loop(&db).unwrap();
full_index_loop(&db).unwrap();
}
#[test]

View File

@ -15,6 +15,7 @@ loggerv = "0.4"
log = "0.3"
open = "1.2"
dissolve = "0.2"
rayon = "0.8"
hammond-data = {path = "../hammond-data"}
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.
refresh_button.connect_clicked(clone!(stack, db => move |_| {
utils::refresh_db(&db, &stack);
utils::refresh_feed(&db, &stack, None);
}));
header
@ -62,9 +62,9 @@ fn on_add_bttn_clicked(db: &Database, stack: &gtk::Stack, url: &str) {
};
info!("{:?} feed added", url);
if let Ok(mut s) = source {
if let Ok(s) = source {
// update the db
utils::refresh_feed(db, stack, &mut s);
utils::refresh_feed(db, stack, Some(Box::new(vec![s])));
} else {
error!("Expected Error, feed probably already exists.");
error!("Error: {:?}", source.unwrap_err());

View File

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

View File

@ -1,9 +1,8 @@
use glib;
use gtk;
// use gtk::prelude::*;
use rayon::prelude::*;
use hammond_data;
use hammond_data::index_feed::Feed;
use hammond_data::index_feed;
use hammond_data::models::Source;
use hammond_data::index_feed::Database;
@ -19,48 +18,31 @@ thread_local!(
gtk::Stack,
Receiver<bool>)>> = RefCell::new(None));
pub fn refresh_db(db: &Database, stack: &gtk::Stack) {
// Create a async channel.
pub fn refresh_feed(db: &Database, stack: &gtk::Stack, source: Option<Box<Vec<Source>>>) {
let (sender, receiver) = channel();
// Pass the desired arguments into the Local Thread Storage.
GLOBAL.with(clone!(db, stack => move |global| {
*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.
thread::spawn(clone!(db => move || {
let t = hammond_data::index_feed::index_loop(&db);
if t.is_err() {
error!("Error While trying to update the database.");
error!("Error msg: {}", t.unwrap_err());
let feeds = {
if let Some(mut boxed_vec) = source {
let f = boxed_vec
.par_iter_mut()
.filter_map(|mut s| {
index_feed::refresh_source(&db, &mut s).ok()
})
.collect();
Ok(f)
} else {
index_feed::fetch_feeds(&db)
}
};
sender.send(true).expect("Couldn't send data to channel");;
// http://gtk-rs.org/docs/glib/source/fn.idle_add.html
glib::idle_add(refresh_podcasts_view);
}));
}
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");;
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> {
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);
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());
let listbox = episodes_listbox(db, &pd);
let listbox = episodes_listbox(db, pd);
if let Ok(l) = listbox {
view.add(&l);
}