WIP: Refactor the IDatabase calls to write directly to the database #271
@ -65,7 +65,6 @@ impl MusicHoard<NoDatabase, NoLibrary> {
|
|||||||
filter: CollectionFilter::default(),
|
filter: CollectionFilter::default(),
|
||||||
filtered: vec![],
|
filtered: vec![],
|
||||||
collection: vec![],
|
collection: vec![],
|
||||||
pre_commit: vec![],
|
|
||||||
database: NoDatabase,
|
database: NoDatabase,
|
||||||
library: NoLibrary,
|
library: NoLibrary,
|
||||||
library_cache: vec![],
|
library_cache: vec![],
|
||||||
@ -87,7 +86,6 @@ impl<Library: ILibrary> MusicHoard<NoDatabase, Library> {
|
|||||||
filter: CollectionFilter::default(),
|
filter: CollectionFilter::default(),
|
||||||
filtered: vec![],
|
filtered: vec![],
|
||||||
collection: vec![],
|
collection: vec![],
|
||||||
pre_commit: vec![],
|
|
||||||
database: NoDatabase,
|
database: NoDatabase,
|
||||||
library,
|
library,
|
||||||
library_cache: vec![],
|
library_cache: vec![],
|
||||||
@ -109,7 +107,6 @@ impl<Database: IDatabase> MusicHoard<Database, NoLibrary> {
|
|||||||
filter: CollectionFilter::default(),
|
filter: CollectionFilter::default(),
|
||||||
filtered: vec![],
|
filtered: vec![],
|
||||||
collection: vec![],
|
collection: vec![],
|
||||||
pre_commit: vec![],
|
|
||||||
database,
|
database,
|
||||||
library: NoLibrary,
|
library: NoLibrary,
|
||||||
library_cache: vec![],
|
library_cache: vec![],
|
||||||
@ -131,7 +128,6 @@ impl<Database: IDatabase, Library: ILibrary> MusicHoard<Database, Library> {
|
|||||||
filter: CollectionFilter::default(),
|
filter: CollectionFilter::default(),
|
||||||
filtered: vec![],
|
filtered: vec![],
|
||||||
collection: vec![],
|
collection: vec![],
|
||||||
pre_commit: vec![],
|
|
||||||
database,
|
database,
|
||||||
library,
|
library,
|
||||||
library_cache: vec![],
|
library_cache: vec![],
|
||||||
|
@ -122,8 +122,6 @@ impl<Database: IDatabase, Library> IMusicHoardDatabase for MusicHoard<Database,
|
|||||||
self.collection = self.merge_collections(database_cache);
|
self.collection = self.merge_collections(database_cache);
|
||||||
self.filtered = self.filter_collection();
|
self.filtered = self.filter_collection();
|
||||||
|
|
||||||
self.pre_commit = self.collection.clone();
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -365,7 +363,6 @@ pub trait IMusicHoardDatabasePrivate {
|
|||||||
|
|
||||||
impl<Library> IMusicHoardDatabasePrivate for MusicHoard<NoDatabase, Library> {
|
impl<Library> IMusicHoardDatabasePrivate for MusicHoard<NoDatabase, Library> {
|
||||||
fn commit(&mut self) -> Result<(), Error> {
|
fn commit(&mut self) -> Result<(), Error> {
|
||||||
self.collection = self.pre_commit.clone();
|
|
||||||
self.filtered = self.filter_collection();
|
self.filtered = self.filter_collection();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -373,14 +370,11 @@ impl<Library> IMusicHoardDatabasePrivate for MusicHoard<NoDatabase, Library> {
|
|||||||
|
|
||||||
impl<Database: IDatabase, Library> IMusicHoardDatabasePrivate for MusicHoard<Database, Library> {
|
impl<Database: IDatabase, Library> IMusicHoardDatabasePrivate for MusicHoard<Database, Library> {
|
||||||
fn commit(&mut self) -> Result<(), Error> {
|
fn commit(&mut self) -> Result<(), Error> {
|
||||||
if self.collection != self.pre_commit {
|
if let Err(err) = self.database.save(&self.collection) {
|
||||||
if let Err(err) = self.database.save(&self.pre_commit) {
|
self.reload_database()?;
|
||||||
self.pre_commit = self.collection.clone();
|
|
||||||
return Err(err.into());
|
return Err(err.into());
|
||||||
}
|
}
|
||||||
self.collection = self.pre_commit.clone();
|
|
||||||
self.filtered = self.filter_collection();
|
self.filtered = self.filter_collection();
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -390,7 +384,7 @@ impl<Database: IDatabase, Library> MusicHoard<Database, Library> {
|
|||||||
where
|
where
|
||||||
FnColl: FnOnce(&mut Collection),
|
FnColl: FnOnce(&mut Collection),
|
||||||
{
|
{
|
||||||
fn_coll(&mut self.pre_commit);
|
fn_coll(&mut self.collection);
|
||||||
self.commit()
|
self.commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -404,7 +398,7 @@ impl<Database: IDatabase, Library> MusicHoard<Database, Library> {
|
|||||||
FnArtist: FnOnce(&mut Artist),
|
FnArtist: FnOnce(&mut Artist),
|
||||||
FnColl: FnOnce(&mut Collection),
|
FnColl: FnOnce(&mut Collection),
|
||||||
{
|
{
|
||||||
let artist = Self::get_artist_mut_or_err(&mut self.pre_commit, artist_id)?;
|
let artist = Self::get_artist_mut_or_err(&mut self.collection, artist_id)?;
|
||||||
fn_artist(artist);
|
fn_artist(artist);
|
||||||
self.update_collection(fn_coll)
|
self.update_collection(fn_coll)
|
||||||
}
|
}
|
||||||
@ -431,7 +425,7 @@ impl<Database: IDatabase, Library> MusicHoard<Database, Library> {
|
|||||||
FnAlbum: FnOnce(&mut Album),
|
FnAlbum: FnOnce(&mut Album),
|
||||||
FnArtist: FnOnce(&mut Artist),
|
FnArtist: FnOnce(&mut Artist),
|
||||||
{
|
{
|
||||||
let artist = Self::get_artist_mut_or_err(&mut self.pre_commit, artist_id)?;
|
let artist = Self::get_artist_mut_or_err(&mut self.collection, artist_id)?;
|
||||||
let album = Self::get_album_mut_or_err(artist, album_id)?;
|
let album = Self::get_album_mut_or_err(artist, album_id)?;
|
||||||
fn_album(album);
|
fn_album(album);
|
||||||
fn_artist(artist);
|
fn_artist(artist);
|
||||||
@ -493,7 +487,7 @@ mod tests {
|
|||||||
.returning(|| Ok(FULL_COLLECTION.to_owned()));
|
.returning(|| Ok(FULL_COLLECTION.to_owned()));
|
||||||
database
|
database
|
||||||
.expect_save()
|
.expect_save()
|
||||||
.times(1)
|
.times(3)
|
||||||
.in_sequence(&mut seq)
|
.in_sequence(&mut seq)
|
||||||
.with(predicate::eq(with_artist.clone()))
|
.with(predicate::eq(with_artist.clone()))
|
||||||
.returning(|_| Ok(()));
|
.returning(|_| Ok(()));
|
||||||
@ -789,7 +783,7 @@ mod tests {
|
|||||||
.returning(|| Ok(FULL_COLLECTION.to_owned()));
|
.returning(|| Ok(FULL_COLLECTION.to_owned()));
|
||||||
database
|
database
|
||||||
.expect_save()
|
.expect_save()
|
||||||
.times(1)
|
.times(3)
|
||||||
.in_sequence(&mut seq)
|
.in_sequence(&mut seq)
|
||||||
.with(predicate::eq(with_album.clone()))
|
.with(predicate::eq(with_album.clone()))
|
||||||
.returning(|_| Ok(()));
|
.returning(|_| Ok(()));
|
||||||
@ -1018,11 +1012,21 @@ mod tests {
|
|||||||
|
|
||||||
let database_result = Err(database::SaveError::IoError(String::from("I/O error")));
|
let database_result = Err(database::SaveError::IoError(String::from("I/O error")));
|
||||||
|
|
||||||
database.expect_load().return_once(|| Ok(vec![]));
|
let mut seq = Sequence::new();
|
||||||
|
database
|
||||||
|
.expect_load()
|
||||||
|
.times(1)
|
||||||
|
.in_sequence(&mut seq)
|
||||||
|
.return_once(|| Ok(vec![]));
|
||||||
database
|
database
|
||||||
.expect_save()
|
.expect_save()
|
||||||
.times(1)
|
.times(1)
|
||||||
.return_once(|_: &Collection| database_result);
|
.return_once(|_: &Collection| database_result);
|
||||||
|
database
|
||||||
|
.expect_load()
|
||||||
|
.times(1)
|
||||||
|
.in_sequence(&mut seq)
|
||||||
|
.return_once(|| Ok(vec![]));
|
||||||
|
|
||||||
let mut music_hoard = MusicHoard::database(database);
|
let mut music_hoard = MusicHoard::database(database);
|
||||||
music_hoard.reload_database().unwrap();
|
music_hoard.reload_database().unwrap();
|
||||||
|
@ -23,7 +23,7 @@ pub trait IMusicHoardLibrary {
|
|||||||
|
|
||||||
impl<Library: ILibrary> IMusicHoardLibrary for MusicHoard<NoDatabase, Library> {
|
impl<Library: ILibrary> IMusicHoardLibrary for MusicHoard<NoDatabase, Library> {
|
||||||
fn rescan_library(&mut self) -> Result<(), Error> {
|
fn rescan_library(&mut self) -> Result<(), Error> {
|
||||||
self.pre_commit = self.rescan_library_inner(vec![])?;
|
self.collection = self.rescan_library_inner(vec![])?;
|
||||||
self.commit()
|
self.commit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -33,7 +33,7 @@ impl<Database: IDatabase, Library: ILibrary> IMusicHoardLibrary for MusicHoard<D
|
|||||||
let mut database_cache = self.database.load()?;
|
let mut database_cache = self.database.load()?;
|
||||||
Self::sort_albums_and_tracks(database_cache.iter_mut());
|
Self::sort_albums_and_tracks(database_cache.iter_mut());
|
||||||
|
|
||||||
self.pre_commit = self.rescan_library_inner(database_cache)?;
|
self.collection = self.rescan_library_inner(database_cache)?;
|
||||||
self.commit()
|
self.commit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,6 @@ pub struct MusicHoard<Database, Library> {
|
|||||||
filter: CollectionFilter,
|
filter: CollectionFilter,
|
||||||
filtered: Collection,
|
filtered: Collection,
|
||||||
collection: Collection,
|
collection: Collection,
|
||||||
pre_commit: Collection,
|
|
||||||
database: Database,
|
database: Database,
|
||||||
library: Library,
|
library: Library,
|
||||||
library_cache: Collection,
|
library_cache: Collection,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user