Refactor to use Arc's and a mutex instead of plain Rc<&Sqlcon>.

This commit is contained in:
Jordan Petridis 2017-10-18 04:14:09 +03:00
parent 5d01b735d6
commit eccbbf0fc1
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
3 changed files with 32 additions and 29 deletions

View File

@ -83,13 +83,9 @@ pub fn index_loop(db: SqliteConnection, force: bool) -> Result<()> {
let mut f = fetch_feeds(m.clone(), force)?; let mut f = fetch_feeds(m.clone(), force)?;
// f.par_iter_mut().for_each(|&mut (ref mut req, ref source)| { f.par_iter_mut().for_each(|&mut (ref mut req, ref source)| {
// TODO: Once for_each is stable, uncomment above line and delete collect.
let _: Vec<_> = f.par_iter_mut()
.map(|&mut (ref mut req, ref source)| {
complete_index_from_source(req, source, m.clone()).unwrap(); complete_index_from_source(req, source, m.clone()).unwrap();
}) });
.collect();
Ok(()) Ok(())
} }

View File

@ -8,7 +8,7 @@ use std::path::Path;
use errors::*; use errors::*;
use hammond_data::dbqueries; use hammond_data::dbqueries;
use hammond_data::models::{Episode, Podcast}; use hammond_data::models::Episode;
use hammond_data::{DL_DIR, HAMMOND_CACHE}; use hammond_data::{DL_DIR, HAMMOND_CACHE};
// Adapted from https://github.com/mattgathu/rget . // Adapted from https://github.com/mattgathu/rget .

View File

@ -19,8 +19,8 @@ use hammond_data::dbqueries;
use hammond_data::models::Episode; use hammond_data::models::Episode;
use hammond_downloader::downloader; use hammond_downloader::downloader;
use std::rc;
use std::thread; use std::thread;
use std::sync::{Arc, Mutex};
use gtk::prelude::*; use gtk::prelude::*;
use gio::ApplicationExt; use gio::ApplicationExt;
@ -79,7 +79,7 @@ fn create_and_fill_list_store(
} }
fn podcast_widget( fn podcast_widget(
connection: &SqliteConnection, connection: Arc<Mutex<SqliteConnection>>,
title: Option<&str>, title: Option<&str>,
description: Option<&str>, description: Option<&str>,
image: Option<Pixbuf>, image: Option<Pixbuf>,
@ -96,7 +96,7 @@ fn podcast_widget(
if let Some(t) = title { if let Some(t) = title {
title_label.set_text(t); title_label.set_text(t);
let listbox = episodes_listbox(connection, t); let listbox = episodes_listbox(connection.clone(), t);
view.add(&listbox); view.add(&listbox);
} }
@ -112,7 +112,11 @@ fn podcast_widget(
pd_widget pd_widget
} }
fn epidose_widget(connection: &SqliteConnection, episode: &mut Episode, pd_title: &str) -> gtk::Box { fn epidose_widget(
connection: &SqliteConnection,
episode: &mut Episode,
pd_title: &str,
) -> gtk::Box {
// This is just a prototype and will be reworked probably. // This is just a prototype and will be reworked probably.
let builder = include_str!("../gtk/EpisodeWidget.ui"); let builder = include_str!("../gtk/EpisodeWidget.ui");
let builder = gtk::Builder::new_from_string(builder); let builder = gtk::Builder::new_from_string(builder);
@ -160,13 +164,17 @@ fn epidose_widget(connection: &SqliteConnection, episode: &mut Episode, pd_title
ep ep
} }
fn episodes_listbox(connection: &SqliteConnection, pd_title: &str) -> gtk::ListBox { fn episodes_listbox(connection: Arc<Mutex<SqliteConnection>>, pd_title: &str) -> gtk::ListBox {
let pd = dbqueries::load_podcast(connection, pd_title).unwrap(); let m = connection.lock().unwrap();
let mut episodes = dbqueries::get_pd_episodes(connection, &pd).unwrap(); let pd = dbqueries::load_podcast(&m, pd_title).unwrap();
let mut episodes = dbqueries::get_pd_episodes(&m, &pd).unwrap();
drop(m);
let list = gtk::ListBox::new(); let list = gtk::ListBox::new();
episodes.iter_mut().for_each(|ep| { episodes.iter_mut().for_each(|ep| {
let w = epidose_widget(connection, ep, pd_title); let m = connection.clone();
let db = m.lock().unwrap();
let w = epidose_widget(&db, ep, pd_title);
list.add(&w) list.add(&w)
}); });
@ -189,8 +197,8 @@ fn build_ui() {
// Get the Stack // Get the Stack
let stack: gtk::Stack = builder.get_object("stack1").unwrap(); let stack: gtk::Stack = builder.get_object("stack1").unwrap();
let db = hammond_data::establish_connection(); let db = Arc::new(Mutex::new(hammond_data::establish_connection()));
let pd_widget = podcast_widget(&db, None, None, None); let pd_widget = podcast_widget(db.clone(), None, None, None);
stack.add_named(&pd_widget, "pdw"); stack.add_named(&pd_widget, "pdw");
// Get the headerbar // Get the headerbar
let header: gtk::HeaderBar = header_build.get_object("headerbar1").unwrap(); let header: gtk::HeaderBar = header_build.get_object("headerbar1").unwrap();
@ -237,18 +245,20 @@ fn build_ui() {
let refresh_button: gtk::Button = header_build.get_object("refbutton").unwrap(); let refresh_button: gtk::Button = header_build.get_object("refbutton").unwrap();
// FIXME: There appears to be a memmory leak here. // FIXME: There appears to be a memmory leak here.
let db_clone = db.clone();
refresh_button.connect_clicked(move |_| { refresh_button.connect_clicked(move |_| {
thread::spawn(|| { let _db_clone = db_clone.clone();
let db = hammond_data::establish_connection(); thread::spawn(move || {
hammond_data::index_feed::index_loop(db, false).unwrap(); // let tempdb = db_clone.lock().unwrap();
// hammond_data::index_feed::index_loop(*tempdb, false).unwrap();
}); });
}); });
// let pd_model = create_and_fill_tree_store(&db, &builder); let tempdb = db.lock().unwrap();
let pd_model = create_and_fill_list_store(&db, &builder); let pd_model = create_and_fill_list_store(&tempdb, &builder);
drop(tempdb);
let iter = pd_model.get_iter_first().unwrap(); let iter = pd_model.get_iter_first().unwrap();
let db = rc::Rc::new(db);
// this will iterate over the episodes. // this will iterate over the episodes.
// let iter = pd_model.iter_children(&iter).unwrap(); // let iter = pd_model.iter_children(&iter).unwrap();
loop { loop {
@ -256,10 +266,7 @@ fn build_ui() {
let description = pd_model.get_value(&iter, 2).get::<String>().unwrap(); let description = pd_model.get_value(&iter, 2).get::<String>().unwrap();
let image_uri = pd_model.get_value(&iter, 4).get::<String>(); let image_uri = pd_model.get_value(&iter, 4).get::<String>();
let imgpath = downloader::cache_image( let imgpath = downloader::cache_image(&title, image_uri.as_ref().map(|s| s.as_str()));
&title,
image_uri.as_ref().map(|s| s.as_str()),
);
let pixbuf = if let Some(i) = imgpath { let pixbuf = if let Some(i) = imgpath {
Pixbuf::new_from_file_at_scale(&i, 200, 200, true).ok() Pixbuf::new_from_file_at_scale(&i, 200, 200, true).ok()
@ -274,7 +281,7 @@ fn build_ui() {
let pdw = stack_clone.get_child_by_name("pdw").unwrap(); let pdw = stack_clone.get_child_by_name("pdw").unwrap();
stack_clone.remove(&pdw); stack_clone.remove(&pdw);
let pdw = podcast_widget( let pdw = podcast_widget(
&db_clone, db_clone.clone(),
Some(title.as_str()), Some(title.as_str()),
Some(description.as_str()), Some(description.as_str()),
pixbuf.clone(), pixbuf.clone(),