Final2, I swear, when dealing with Arc/rc always use references.

This commit is contained in:
Jordan Petridis
2017-10-22 04:51:34 +03:00
parent cbe50c9163
commit 6ffacaa6d8
9 changed files with 49 additions and 54 deletions
+10 -11
View File
@@ -1,6 +1,5 @@
#![allow(dead_code)]
#![cfg_attr(feature = "cargo-clippy", allow(clone_on_ref_ptr))]
#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
use diesel::prelude::*;
use diesel;
@@ -82,12 +81,12 @@ fn insert_return_episode(con: &SqliteConnection, ep: &NewEpisode) -> Result<Epis
Ok(dbqueries::load_episode(con, ep.uri.unwrap())?)
}
pub fn index_loop(db: Arc<Mutex<SqliteConnection>>, force: bool) -> Result<()> {
let mut f = fetch_feeds(db.clone(), force)?;
pub fn index_loop(db: &Arc<Mutex<SqliteConnection>>, force: bool) -> Result<()> {
let mut f = fetch_feeds(db, force)?;
f.par_iter_mut()
.for_each(|&mut Feed(ref mut req, ref source)| {
let e = complete_index_from_source(req, source, db.clone());
let e = complete_index_from_source(req, source, db);
if e.is_err() {
error!("Error While trying to update the database.");
error!("Error msg: {}", e.unwrap_err());
@@ -100,7 +99,7 @@ pub fn index_loop(db: Arc<Mutex<SqliteConnection>>, force: bool) -> Result<()> {
pub fn complete_index_from_source(
req: &mut reqwest::Response,
source: &Source,
mutex: Arc<Mutex<SqliteConnection>>,
mutex: &Arc<Mutex<SqliteConnection>>,
) -> Result<()> {
use std::io::Read;
use std::str::FromStr;
@@ -115,7 +114,7 @@ pub fn complete_index_from_source(
}
fn complete_index(
connection: Arc<Mutex<SqliteConnection>>,
connection: &Arc<Mutex<SqliteConnection>>,
chan: &rss::Channel,
parent: &Source,
) -> Result<()> {
@@ -137,7 +136,7 @@ fn index_channel(db: &SqliteConnection, chan: &rss::Channel, parent: &Source) ->
Ok(pd)
}
fn index_channel_items(connection: Arc<Mutex<SqliteConnection>>, it: &[rss::Item], pd: &Podcast) {
fn index_channel_items(connection: &Arc<Mutex<SqliteConnection>>, it: &[rss::Item], pd: &Podcast) {
it.par_iter()
.map(|x| feedparser::parse_episode(x, pd.id()))
.for_each(|x| {
@@ -152,7 +151,7 @@ fn index_channel_items(connection: Arc<Mutex<SqliteConnection>>, it: &[rss::Item
}
// Maybe this can be refactored into an Iterator for lazy evaluation.
pub fn fetch_feeds(connection: Arc<Mutex<SqliteConnection>>, force: bool) -> Result<Vec<Feed>> {
pub fn fetch_feeds(connection: &Arc<Mutex<SqliteConnection>>, force: bool) -> Result<Vec<Feed>> {
let tempdb = connection.lock().unwrap();
let mut feeds = dbqueries::get_sources(&tempdb)?;
drop(tempdb);
@@ -272,10 +271,10 @@ mod tests {
index_source(&tempdb, &NewSource::new_with_uri(feed)).unwrap()
});
index_loop(db.clone(), true).unwrap();
index_loop(&db, true).unwrap();
// Run again to cover Unique constrains erros.
index_loop(db.clone(), true).unwrap();
index_loop(&db, true).unwrap();
}
#[test]
@@ -316,7 +315,7 @@ mod tests {
let chan = rss::Channel::read_from(BufReader::new(feed)).unwrap();
// Index the channel
complete_index(m.clone(), &chan, &s).unwrap();
complete_index(&m, &chan, &s).unwrap();
});
// Assert the index rows equal the controlled results