WIP: Refactor the IDatabase calls to write directly to the database #271

Draft
wojtek wants to merge 12 commits from 268---refactor-the-idatabase-calls-to-write-directly-to-the-database into main
2 changed files with 23 additions and 34 deletions
Showing only changes of commit 6a80c05909 - Show all commits

View File

@ -30,7 +30,6 @@ impl<Database, Library> IMusicHoardBase for MusicHoard<Database, Library> {
} }
pub trait IMusicHoardBasePrivate { pub trait IMusicHoardBasePrivate {
fn sort_artists(collection: &mut [Artist]);
fn sort_albums_and_tracks<'a, C: Iterator<Item = &'a mut Artist>>(collection: C); fn sort_albums_and_tracks<'a, C: Iterator<Item = &'a mut Artist>>(collection: C);
fn merge_collections<It: IntoIterator<Item = Artist>>(&self, database: It) -> Collection; fn merge_collections<It: IntoIterator<Item = Artist>>(&self, database: It) -> Collection;
@ -55,10 +54,6 @@ pub trait IMusicHoardBasePrivate {
} }
impl<Database, Library> IMusicHoardBasePrivate for MusicHoard<Database, Library> { impl<Database, Library> IMusicHoardBasePrivate for MusicHoard<Database, Library> {
fn sort_artists(collection: &mut [Artist]) {
collection.sort_unstable();
}
fn sort_albums_and_tracks<'a, COL: Iterator<Item = &'a mut Artist>>(collection: COL) { fn sort_albums_and_tracks<'a, COL: Iterator<Item = &'a mut Artist>>(collection: COL) {
for artist in collection { for artist in collection {
artist.albums.sort_unstable(); artist.albums.sort_unstable();

View File

@ -86,19 +86,11 @@ impl<Database: IDatabase, Library> IMusicHoardDatabase for MusicHoard<Database,
artist_id: Id, artist_id: Id,
mb_ref: ArtistMbRef, mb_ref: ArtistMbRef,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.update_artist_and( self.update_artist(artist_id.as_ref(), |artist| artist.meta.set_mb_ref(mb_ref))
artist_id.as_ref(),
|artist| artist.meta.set_mb_ref(mb_ref),
|collection| Self::sort_artists(collection),
)
} }
fn clear_artist_mb_ref<Id: AsRef<ArtistId>>(&mut self, artist_id: Id) -> Result<(), Error> { fn clear_artist_mb_ref<Id: AsRef<ArtistId>>(&mut self, artist_id: Id) -> Result<(), Error> {
self.update_artist_and( self.update_artist(artist_id.as_ref(), |artist| artist.meta.clear_mb_ref())
artist_id.as_ref(),
|artist| artist.meta.clear_mb_ref(),
|collection| Self::sort_artists(collection),
)
} }
fn merge_artist_info<Id: AsRef<ArtistId>>( fn merge_artist_info<Id: AsRef<ArtistId>>(
@ -157,12 +149,9 @@ impl<Database: IDatabase, Library> IMusicHoardDatabase for MusicHoard<Database,
album_id: AlbumIdRef, album_id: AlbumIdRef,
mb_ref: AlbumMbRef, mb_ref: AlbumMbRef,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.update_album_and( self.update_album(artist_id.as_ref(), album_id.as_ref(), |album| {
artist_id.as_ref(), album.meta.set_mb_ref(mb_ref)
album_id.as_ref(), })
|album| album.meta.set_mb_ref(mb_ref),
|artist| artist.albums.sort_unstable(),
)
} }
fn clear_album_mb_ref<ArtistIdRef: AsRef<ArtistId>, AlbumIdRef: AsRef<AlbumId>>( fn clear_album_mb_ref<ArtistIdRef: AsRef<ArtistId>, AlbumIdRef: AsRef<AlbumId>>(
@ -170,12 +159,9 @@ impl<Database: IDatabase, Library> IMusicHoardDatabase for MusicHoard<Database,
artist_id: ArtistIdRef, artist_id: ArtistIdRef,
album_id: AlbumIdRef, album_id: AlbumIdRef,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.update_album_and( self.update_album(artist_id.as_ref(), album_id.as_ref(), |album| {
artist_id.as_ref(), album.meta.clear_mb_ref()
album_id.as_ref(), })
|album| album.meta.clear_mb_ref(),
|artist| artist.albums.sort_unstable(),
)
} }
fn merge_album_info<Id: AsRef<ArtistId>, AlbumIdRef: AsRef<AlbumId>>( fn merge_album_info<Id: AsRef<ArtistId>, AlbumIdRef: AsRef<AlbumId>>(
@ -184,10 +170,15 @@ impl<Database: IDatabase, Library> IMusicHoardDatabase for MusicHoard<Database,
album_id: AlbumIdRef, album_id: AlbumIdRef,
mut info: AlbumInfo, mut info: AlbumInfo,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.update_album(artist_id.as_ref(), album_id.as_ref(), |album| { self.update_album_and(
mem::swap(&mut album.meta.info, &mut info); artist_id.as_ref(),
album.meta.info.merge_in_place(info); album_id.as_ref(),
}) |album| {
mem::swap(&mut album.meta.info, &mut info);
album.meta.info.merge_in_place(info);
},
|artist| artist.albums.sort_unstable(),
)
} }
fn clear_album_info<Id: AsRef<ArtistId>, AlbumIdRef: AsRef<AlbumId>>( fn clear_album_info<Id: AsRef<ArtistId>, AlbumIdRef: AsRef<AlbumId>>(
@ -195,9 +186,12 @@ impl<Database: IDatabase, Library> IMusicHoardDatabase for MusicHoard<Database,
artist_id: Id, artist_id: Id,
album_id: AlbumIdRef, album_id: AlbumIdRef,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.update_album(artist_id.as_ref(), album_id.as_ref(), |album| { self.update_album_and(
album.meta.info = AlbumInfo::default() artist_id.as_ref(),
}) album_id.as_ref(),
|album| album.meta.info = AlbumInfo::default(),
|artist| artist.albums.sort_unstable(),
)
} }
} }