Library rescans commit to the database
This commit is contained in:
parent
4d2ea77da9
commit
12fcdd45b6
@ -72,7 +72,7 @@ impl<LIB, DB> MusicHoard<LIB, DB> {
|
||||
}
|
||||
}
|
||||
|
||||
fn merge_collections(&mut self) {
|
||||
fn merge_collections(&mut self) -> Collection {
|
||||
let mut primary = self.library_cache.clone();
|
||||
for secondary_artist in self.database_cache.iter().cloned() {
|
||||
if let Some(ref mut primary_artist) = primary.get_mut(&secondary_artist.id) {
|
||||
@ -82,10 +82,10 @@ impl<LIB, DB> MusicHoard<LIB, DB> {
|
||||
}
|
||||
}
|
||||
|
||||
self.collection = primary.into_values().collect();
|
||||
Self::sort_artists(&mut self.collection);
|
||||
let mut collection: Collection = primary.into_values().collect();
|
||||
Self::sort_artists(&mut collection);
|
||||
|
||||
self.pre_commit = self.collection.clone();
|
||||
collection
|
||||
}
|
||||
|
||||
fn items_to_artists(items: Vec<Item>) -> Result<HashMap<ArtistId, Artist>, Error> {
|
||||
@ -190,17 +190,22 @@ impl<LIB: ILibrary> MusicHoard<LIB, NoDatabase> {
|
||||
database_cache: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// Rescan the library and merge with the in-memory collection.
|
||||
pub fn rescan_library(&mut self) -> Result<(), Error> {
|
||||
self.pre_commit = self.rescan_library_inner()?;
|
||||
self.collection = self.pre_commit.clone();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<LIB: ILibrary, DB> MusicHoard<LIB, DB> {
|
||||
/// Rescan the library and merge with the in-memory collection.
|
||||
pub fn rescan_library(&mut self) -> Result<(), Error> {
|
||||
fn rescan_library_inner(&mut self) -> Result<Collection, Error> {
|
||||
let items = self.library.list(&Query::new())?;
|
||||
self.library_cache = Self::items_to_artists(items)?;
|
||||
Self::sort_albums_and_tracks(self.library_cache.values_mut());
|
||||
|
||||
self.merge_collections();
|
||||
Ok(())
|
||||
Ok(self.merge_collections())
|
||||
}
|
||||
}
|
||||
|
||||
@ -226,7 +231,9 @@ impl<LIB, DB: IDatabase> MusicHoard<LIB, DB> {
|
||||
self.database_cache = self.database.load()?;
|
||||
Self::sort_albums_and_tracks(self.database_cache.iter_mut());
|
||||
|
||||
self.merge_collections();
|
||||
self.collection = self.merge_collections();
|
||||
self.pre_commit = self.collection.clone();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -381,6 +388,12 @@ impl<LIB: ILibrary, DB: IDatabase> MusicHoard<LIB, DB> {
|
||||
mh.reload_database()?;
|
||||
Ok(mh)
|
||||
}
|
||||
|
||||
/// Rescan the library and merge with the in-memory collection.
|
||||
pub fn rescan_library(&mut self) -> Result<(), Error> {
|
||||
self.pre_commit = self.rescan_library_inner()?;
|
||||
self.commit()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -672,7 +685,7 @@ mod tests {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
mh.merge_collections();
|
||||
mh.collection = mh.merge_collections();
|
||||
assert_eq!(expected, mh.collection);
|
||||
|
||||
// The merge is completely non-overlapping so it should be commutative.
|
||||
@ -686,7 +699,7 @@ mod tests {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
mh.merge_collections();
|
||||
mh.collection = mh.merge_collections();
|
||||
assert_eq!(expected, mh.collection);
|
||||
}
|
||||
|
||||
@ -710,7 +723,7 @@ mod tests {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
mh.merge_collections();
|
||||
mh.collection = mh.merge_collections();
|
||||
assert_eq!(expected, mh.collection);
|
||||
|
||||
// The merge does not overwrite any data so it should be commutative.
|
||||
@ -724,7 +737,7 @@ mod tests {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
mh.merge_collections();
|
||||
mh.collection = mh.merge_collections();
|
||||
assert_eq!(expected, mh.collection);
|
||||
}
|
||||
|
||||
@ -761,7 +774,7 @@ mod tests {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
mh.merge_collections();
|
||||
mh.collection = mh.merge_collections();
|
||||
assert_eq!(expected, mh.collection);
|
||||
|
||||
// The merge overwrites the sort data, but no data is erased so it should be commutative.
|
||||
@ -775,13 +788,14 @@ mod tests {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
mh.merge_collections();
|
||||
mh.collection = mh.merge_collections();
|
||||
assert_eq!(expected, mh.collection);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rescan_library_ordered() {
|
||||
let mut library = MockILibrary::new();
|
||||
let mut database = MockIDatabase::new();
|
||||
|
||||
let library_input = Query::new();
|
||||
let library_result = Ok(LIBRARY_ITEMS.to_owned());
|
||||
@ -792,7 +806,14 @@ mod tests {
|
||||
.times(1)
|
||||
.return_once(|_| library_result);
|
||||
|
||||
let mut music_hoard = MusicHoard::library(library);
|
||||
database.expect_load().times(1).returning(|| Ok(vec![]));
|
||||
database
|
||||
.expect_save()
|
||||
.with(predicate::eq(&*LIBRARY_COLLECTION))
|
||||
.times(1)
|
||||
.return_once(|_| Ok(()));
|
||||
|
||||
let mut music_hoard = MusicHoard::new(library, database).unwrap();
|
||||
|
||||
music_hoard.rescan_library().unwrap();
|
||||
assert_eq!(music_hoard.get_collection(), &*LIBRARY_COLLECTION);
|
||||
|
@ -13,7 +13,7 @@ pub trait IMusicHoard {
|
||||
// GRCOV_EXCL_START
|
||||
impl<LIB: ILibrary, DB: IDatabase> IMusicHoard for MusicHoard<LIB, DB> {
|
||||
fn rescan_library(&mut self) -> Result<(), musichoard::Error> {
|
||||
MusicHoard::rescan_library(self)
|
||||
MusicHoard::<LIB, DB>::rescan_library(self)
|
||||
}
|
||||
|
||||
fn reload_database(&mut self) -> Result<(), musichoard::Error> {
|
||||
|
Loading…
Reference in New Issue
Block a user