diff --git a/hammond-data/benches/bench.rs b/hammond-data/benches/bench.rs index 1f5e1ce..dc5e079 100644 --- a/hammond-data/benches/bench.rs +++ b/hammond-data/benches/bench.rs @@ -21,6 +21,7 @@ use tokio_core::reactor::Core; use hammond_data::FeedBuilder; use hammond_data::Source; use hammond_data::database::truncate_db; +use hammond_data::pipeline; // use hammond_data::errors::*; use std::io::BufReader; @@ -67,7 +68,7 @@ fn bench_pipeline(c: &mut Criterion) { c.bench_function("pipline", |b| { b.iter(|| { let sources = hammond_data::dbqueries::get_sources().unwrap(); - hammond_data::pipeline::pipeline(sources, true).unwrap(); + pipeline::run(sources, true).unwrap(); }) }); truncate_db().unwrap(); diff --git a/hammond-data/src/lib.rs b/hammond-data/src/lib.rs index f498821..399d119 100644 --- a/hammond-data/src/lib.rs +++ b/hammond-data/src/lib.rs @@ -65,6 +65,7 @@ mod schema; pub use feed::{Feed, FeedBuilder}; pub use models::{Episode, EpisodeWidgetQuery, Podcast, PodcastCoverQuery, Source}; +pub use models::Save; /// [XDG Base Direcotory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) Paths. #[allow(missing_debug_implementations)] diff --git a/hammond-data/src/models/episode.rs b/hammond-data/src/models/episode.rs index 5e4a5ab..948422c 100644 --- a/hammond-data/src/models/episode.rs +++ b/hammond-data/src/models/episode.rs @@ -5,7 +5,7 @@ use diesel::prelude::*; use database::connection; use errors::*; -use models::Podcast; +use models::{Podcast, Save}; use schema::episode; #[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)] @@ -31,6 +31,16 @@ pub struct Episode { podcast_id: i32, } +impl Save for Episode { + /// Helper method to easily save/"sync" current state of self to the Database. + fn save(&self) -> Result { + let db = connection(); + let tempdb = db.get()?; + + self.save_changes::(&*tempdb).map_err(From::from) + } +} + impl Episode { /// Get the value of the sqlite's `ROW_ID` pub fn rowid(&self) -> i32 { @@ -175,14 +185,6 @@ impl Episode { self.set_played(Some(epoch)); self.save().map(|_| ()) } - - /// Helper method to easily save/"sync" current state of self to the Database. - pub fn save(&self) -> Result { - let db = connection(); - let tempdb = db.get()?; - - self.save_changes::(&*tempdb).map_err(From::from) - } } #[derive(Queryable, AsChangeset, PartialEq)] @@ -221,6 +223,21 @@ impl From for EpisodeWidgetQuery { } } +impl Save for EpisodeWidgetQuery { + /// Helper method to easily save/"sync" current state of self to the Database. + fn save(&self) -> Result { + use schema::episode::dsl::*; + + let db = connection(); + let tempdb = db.get()?; + + diesel::update(episode.filter(rowid.eq(self.rowid))) + .set(self) + .execute(&*tempdb) + .map_err(From::from) + } +} + impl EpisodeWidgetQuery { /// Get the value of the sqlite's `ROW_ID` pub fn rowid(&self) -> i32 { @@ -330,19 +347,6 @@ impl EpisodeWidgetQuery { self.set_played(Some(epoch)); self.save().map(|_| ()) } - - /// Helper method to easily save/"sync" current state of self to the Database. - pub fn save(&self) -> Result { - use schema::episode::dsl::*; - - let db = connection(); - let tempdb = db.get()?; - - diesel::update(episode.filter(rowid.eq(self.rowid))) - .set(self) - .execute(&*tempdb) - .map_err(From::from) - } } #[derive(Queryable, AsChangeset, PartialEq)] @@ -357,6 +361,21 @@ pub struct EpisodeCleanerQuery { played: Option, } +impl Save for EpisodeCleanerQuery { + /// Helper method to easily save/"sync" current state of self to the Database. + fn save(&self) -> Result { + use schema::episode::dsl::*; + + let db = connection(); + let tempdb = db.get()?; + + diesel::update(episode.filter(rowid.eq(self.rowid))) + .set(self) + .execute(&*tempdb) + .map_err(From::from) + } +} + impl From for EpisodeCleanerQuery { fn from(e: Episode) -> EpisodeCleanerQuery { EpisodeCleanerQuery { @@ -397,19 +416,6 @@ impl EpisodeCleanerQuery { pub fn set_played(&mut self, value: Option) { self.played = value; } - - /// Helper method to easily save/"sync" current state of self to the Database. - pub fn save(&self) -> Result { - use schema::episode::dsl::*; - - let db = connection(); - let tempdb = db.get()?; - - diesel::update(episode.filter(rowid.eq(self.rowid()))) - .set(self) - .execute(&*tempdb) - .map_err(From::from) - } } #[derive(Queryable, AsChangeset, PartialEq)] diff --git a/hammond-data/src/models/mod.rs b/hammond-data/src/models/mod.rs index cdf87af..87efe30 100644 --- a/hammond-data/src/models/mod.rs +++ b/hammond-data/src/models/mod.rs @@ -43,3 +43,9 @@ pub trait Update { pub trait Index: Insert + Update { fn index(&self) -> Result<()>; } + +/// FIXME: DOCS +pub trait Save { + /// Helper method to easily save/"sync" current state of a diesel model to the Database. + fn save(&self) -> Result; +} diff --git a/hammond-data/src/models/new_podcast.rs b/hammond-data/src/models/new_podcast.rs index 1e08dd9..f6da37c 100644 --- a/hammond-data/src/models/new_podcast.rs +++ b/hammond-data/src/models/new_podcast.rs @@ -157,7 +157,7 @@ mod tests { use rss::Channel; use database::truncate_db; - use models::NewPodcastBuilder; + use models::{NewPodcastBuilder, Save}; use std::fs::File; use std::io::BufReader; diff --git a/hammond-data/src/models/podcast.rs b/hammond-data/src/models/podcast.rs index f75caee..fb6ab74 100644 --- a/hammond-data/src/models/podcast.rs +++ b/hammond-data/src/models/podcast.rs @@ -2,7 +2,7 @@ use diesel::SaveChangesDsl; use database::connection; use errors::*; -use models::Source; +use models::{Save, Source}; use schema::podcast; #[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)] @@ -23,6 +23,16 @@ pub struct Podcast { source_id: i32, } +impl Save for Podcast { + /// Helper method to easily save/"sync" current state of self to the Database. + fn save(&self) -> Result { + let db = connection(); + let tempdb = db.get()?; + + self.save_changes::(&*tempdb).map_err(From::from) + } +} + impl Podcast { /// Get the Feed `id`. pub fn id(&self) -> i32 { @@ -107,14 +117,6 @@ impl Podcast { pub fn source_id(&self) -> i32 { self.source_id } - - /// Helper method to easily save/"sync" current state of self to the Database. - pub fn save(&self) -> Result { - let db = connection(); - let tempdb = db.get()?; - - self.save_changes::(&*tempdb).map_err(From::from) - } } #[derive(Queryable, Debug, Clone)] diff --git a/hammond-data/src/models/source.rs b/hammond-data/src/models/source.rs index 33c5159..2f38b5d 100644 --- a/hammond-data/src/models/source.rs +++ b/hammond-data/src/models/source.rs @@ -16,7 +16,7 @@ use futures_cpupool::CpuPool; use database::connection; use errors::*; use feed::{Feed, FeedBuilder}; -use models::NewSource; +use models::{NewSource, Save}; use schema::source; use std::str::FromStr; @@ -33,6 +33,16 @@ pub struct Source { http_etag: Option, } +impl Save for Source { + /// Helper method to easily save/"sync" current state of self to the Database. + fn save(&self) -> Result { + let db = connection(); + let con = db.get()?; + + self.save_changes::(&con).map_err(From::from) + } +} + impl Source { /// Get the source `id` column. pub fn id(&self) -> i32 { @@ -74,14 +84,6 @@ impl Source { self.http_etag = value.map(|x| x.to_string()); } - /// Helper method to easily save/"sync" current state of self to the Database. - pub fn save(&self) -> Result { - let db = connection(); - let con = db.get()?; - - self.save_changes::(&con).map_err(From::from) - } - /// Extract Etag and LastModifier from res, and update self and the /// corresponding db row. fn update_etag(&mut self, res: &Response) -> Result<()> { diff --git a/hammond-data/src/utils.rs b/hammond-data/src/utils.rs index 2e152f9..97f762b 100644 --- a/hammond-data/src/utils.rs +++ b/hammond-data/src/utils.rs @@ -8,7 +8,7 @@ use url::{Position, Url}; use dbqueries; use errors::*; -use models::{EpisodeCleanerQuery, Podcast}; +use models::{EpisodeCleanerQuery, Podcast, Save}; use xdg_dirs::DL_DIR; use std::fs; diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index a700434..dd813b8 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -11,7 +11,7 @@ use std::path::Path; use std::sync::{Arc, Mutex}; use errors::*; -use hammond_data::{EpisodeWidgetQuery, PodcastCoverQuery}; +use hammond_data::{EpisodeWidgetQuery, PodcastCoverQuery, Save}; use hammond_data::xdg_dirs::HAMMOND_CACHE; // TODO: Replace path that are of type &str with std::path.