Move some indexing functions into methods of insertable models.
This commit is contained in:
parent
8eca46871d
commit
282a29e7dd
@ -53,6 +53,7 @@ impl Feed {
|
|||||||
|
|
||||||
let conn = db.lock().unwrap();
|
let conn = db.lock().unwrap();
|
||||||
let e = conn.transaction::<(), Error, _>(|| {
|
let e = conn.transaction::<(), Error, _>(|| {
|
||||||
|
// TODO:
|
||||||
episodes.iter().for_each(|x| {
|
episodes.iter().for_each(|x| {
|
||||||
let e = index_episode(&conn, x);
|
let e = index_episode(&conn, x);
|
||||||
if let Err(err) = e {
|
if let Err(err) = e {
|
||||||
@ -68,28 +69,6 @@ impl Feed {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn index_source(con: &SqliteConnection, foo: &NewSource) {
|
|
||||||
use schema::source::dsl::*;
|
|
||||||
|
|
||||||
// Throw away the result like `insert or ignore`
|
|
||||||
// Diesel deos not support `insert or ignore` yet.
|
|
||||||
let _ = diesel::insert_into(source).values(foo).execute(con);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn index_podcast(con: &SqliteConnection, pd: &NewPodcast) -> Result<()> {
|
|
||||||
use schema::podcast::dsl::*;
|
|
||||||
|
|
||||||
match dbqueries::get_podcast_from_title(con, &pd.title) {
|
|
||||||
Ok(foo) => if foo.link() != pd.link || foo.description() != pd.description {
|
|
||||||
diesel::replace_into(podcast).values(pd).execute(con)?;
|
|
||||||
},
|
|
||||||
Err(_) => {
|
|
||||||
diesel::insert_into(podcast).values(pd).execute(con)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Currently using diesel from master git.
|
// TODO: Currently using diesel from master git.
|
||||||
// Watch out for v0.99.0 beta and change the toml.
|
// Watch out for v0.99.0 beta and change the toml.
|
||||||
fn index_episode(con: &SqliteConnection, ep: &NewEpisode) -> QueryResult<()> {
|
fn index_episode(con: &SqliteConnection, ep: &NewEpisode) -> QueryResult<()> {
|
||||||
@ -203,8 +182,9 @@ mod tests {
|
|||||||
];
|
];
|
||||||
|
|
||||||
inpt.iter().for_each(|feed| {
|
inpt.iter().for_each(|feed| {
|
||||||
let tempdb = db.lock().unwrap();
|
NewSource::new_with_uri(feed)
|
||||||
index_source(&tempdb, &NewSource::new_with_uri(feed));
|
.into_source(&db.clone())
|
||||||
|
.unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
full_index_loop(&db).unwrap();
|
full_index_loop(&db).unwrap();
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
use diesel;
|
||||||
|
|
||||||
use schema::{episode, podcast, source};
|
use schema::{episode, podcast, source};
|
||||||
use models::{Podcast, Source};
|
use models::{Podcast, Source};
|
||||||
use index_feed::Database;
|
use index_feed::Database;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
use index_feed;
|
// use index_feed;
|
||||||
use dbqueries;
|
use dbqueries;
|
||||||
|
|
||||||
#[derive(Insertable)]
|
#[derive(Insertable)]
|
||||||
@ -26,10 +27,20 @@ impl<'a> NewSource<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn index(&self, db: &Database) {
|
||||||
|
use schema::source::dsl::*;
|
||||||
|
|
||||||
|
let tempdb = db.lock().unwrap();
|
||||||
|
// Throw away the result like `insert or ignore`
|
||||||
|
// Diesel deos not support `insert or ignore` yet.
|
||||||
|
let _ = diesel::insert_into(source).values(self).execute(&*tempdb);
|
||||||
|
}
|
||||||
|
|
||||||
// Look out for when tryinto lands into stable.
|
// Look out for when tryinto lands into stable.
|
||||||
pub fn into_source(self, db: &Database) -> QueryResult<Source> {
|
pub fn into_source(self, db: &Database) -> QueryResult<Source> {
|
||||||
|
self.index(db);
|
||||||
|
|
||||||
let tempdb = db.lock().unwrap();
|
let tempdb = db.lock().unwrap();
|
||||||
index_feed::index_source(&tempdb, &self);
|
|
||||||
dbqueries::get_source_from_uri(&tempdb, self.uri)
|
dbqueries::get_source_from_uri(&tempdb, self.uri)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,9 +73,30 @@ pub struct NewPodcast {
|
|||||||
impl NewPodcast {
|
impl NewPodcast {
|
||||||
// Look out for when tryinto lands into stable.
|
// Look out for when tryinto lands into stable.
|
||||||
pub fn into_podcast(self, db: &Database) -> Result<Podcast> {
|
pub fn into_podcast(self, db: &Database) -> Result<Podcast> {
|
||||||
|
self.index(db)?;
|
||||||
let tempdb = db.lock().unwrap();
|
let tempdb = db.lock().unwrap();
|
||||||
index_feed::index_podcast(&tempdb, &self)?;
|
|
||||||
|
|
||||||
Ok(dbqueries::get_podcast_from_title(&tempdb, &self.title)?)
|
Ok(dbqueries::get_podcast_from_title(&tempdb, &self.title)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn index(&self, db: &Database) -> Result<()> {
|
||||||
|
use schema::podcast::dsl::*;
|
||||||
|
let pd = {
|
||||||
|
let tempdb = db.lock().unwrap();
|
||||||
|
dbqueries::get_podcast_from_title(&tempdb, &self.title)
|
||||||
|
};
|
||||||
|
|
||||||
|
match pd {
|
||||||
|
Ok(foo) => if foo.link() != self.link {
|
||||||
|
let tempdb = db.lock().unwrap();
|
||||||
|
diesel::replace_into(podcast)
|
||||||
|
.values(self)
|
||||||
|
.execute(&*tempdb)?;
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
|
let tempdb = db.lock().unwrap();
|
||||||
|
diesel::insert_into(podcast).values(self).execute(&*tempdb)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user