From 9218c1b130dbac831d3fd567185a2cf7558ad403 Mon Sep 17 00:00:00 2001 From: Maximilian Bauer Date: Tue, 30 Mar 2021 22:05:14 +0200 Subject: [PATCH] Subsonic: increase lib sync performance --- .../Api/Subsonic/SsAlbumParserDelegate.swift | 22 ++++++------------- .../Api/Subsonic/SsArtistParserDelegate.swift | 15 +++++-------- .../SsPlaylistSongsParserDelegate.swift | 9 ++++---- .../Api/Subsonic/SsSongParserDelegate.swift | 13 +++++++---- .../SubsonicLibraryBackgroundSyncer.swift | 2 ++ .../Api/Subsonic/SubsonicLibrarySyncer.swift | 14 +++++++----- 6 files changed, 36 insertions(+), 39 deletions(-) diff --git a/Amperfy/Api/Subsonic/SsAlbumParserDelegate.swift b/Amperfy/Api/Subsonic/SsAlbumParserDelegate.swift index f9977a71..ba22b75c 100644 --- a/Amperfy/Api/Subsonic/SsAlbumParserDelegate.swift +++ b/Amperfy/Api/Subsonic/SsAlbumParserDelegate.swift @@ -7,7 +7,6 @@ class SsAlbumParserDelegate: GenericXmlLibParser { private var subsonicUrlCreator: SubsonicUrlCreator private var albumBuffer: Album? - var songCountOfAlbum = 0 init(libraryStorage: LibraryStorage, syncWave: SyncWave, subsonicUrlCreator: SubsonicUrlCreator, parseNotifier: ParsedObjectNotifiable? = nil) { self.subsonicUrlCreator = subsonicUrlCreator @@ -18,15 +17,9 @@ class SsAlbumParserDelegate: GenericXmlLibParser { buffer = "" if(elementName == "album") { - guard let albumId = attributeDict["id"], - let attributeAlbumtName = attributeDict["name"], - let artistId = attributeDict["artistId"], - let attributeSongCount = attributeDict["songCount"], - let songCount = Int(attributeSongCount) else { - return - } + guard let albumId = attributeDict["id"] else { return } - if !syncWave.isInitialWave, let fetchedAlbum = libraryStorage.getAlbum(id: albumId) { + if let fetchedAlbum = libraryStorage.getAlbum(id: albumId) { albumBuffer = fetchedAlbum } else { albumBuffer = libraryStorage.createAlbum() @@ -34,20 +27,20 @@ class SsAlbumParserDelegate: GenericXmlLibParser { albumBuffer?.syncInfo = syncWave albumBuffer?.id = albumId - albumBuffer?.name = attributeAlbumtName + if let attributeAlbumtName = attributeDict["name"] { + albumBuffer?.name = attributeAlbumtName + } albumBuffer?.artwork?.url = subsonicUrlCreator.getArtUrlString(forArtistId: albumId) if let attributeYear = attributeDict["year"], let year = Int(attributeYear) { albumBuffer?.year = year } - if let artist = libraryStorage.getArtist(id: artistId) { + if let artistId = attributeDict["artistId"], let artist = libraryStorage.getArtist(id: artistId) { albumBuffer?.artist = artist } else { - os_log("Found album id %s with unknown artist id %s", log: log, type: .error, albumId, artistId) + os_log("Found album id %s with unknown artist", log: log, type: .error, albumId) } - - songCountOfAlbum += songCount } } @@ -55,7 +48,6 @@ class SsAlbumParserDelegate: GenericXmlLibParser { switch(elementName) { case "album": parsedCount += 1 - parseNotifier?.notifyParsedObject() albumBuffer = nil default: break diff --git a/Amperfy/Api/Subsonic/SsArtistParserDelegate.swift b/Amperfy/Api/Subsonic/SsArtistParserDelegate.swift index 0272ed6a..ae1a5684 100644 --- a/Amperfy/Api/Subsonic/SsArtistParserDelegate.swift +++ b/Amperfy/Api/Subsonic/SsArtistParserDelegate.swift @@ -6,7 +6,6 @@ class SsArtistParserDelegate: GenericXmlLibParser { private var subsonicUrlCreator: SubsonicUrlCreator private var artistBuffer: Artist? - var albumCountOfAllArtists = 0 init(libraryStorage: LibraryStorage, syncWave: SyncWave, subsonicUrlCreator: SubsonicUrlCreator, parseNotifier: ParsedObjectNotifiable? = nil) { self.subsonicUrlCreator = subsonicUrlCreator @@ -17,22 +16,19 @@ class SsArtistParserDelegate: GenericXmlLibParser { buffer = "" if(elementName == "artist") { - guard let artistId = attributeDict["id"], - let attributeArtistName = attributeDict["name"], - let attributeAlbumCount = attributeDict["albumCount"], - let albumCount = Int(attributeAlbumCount) else { - return - } + guard let artistId = attributeDict["id"] else { return } + if !syncWave.isInitialWave, let fetchedArtist = libraryStorage.getArtist(id: artistId) { artistBuffer = fetchedArtist } else { artistBuffer = libraryStorage.createArtist() artistBuffer?.syncInfo = syncWave artistBuffer?.id = artistId - artistBuffer?.name = attributeArtistName + if let attributeArtistName = attributeDict["name"] { + artistBuffer?.name = attributeArtistName + } artistBuffer?.artwork?.url = subsonicUrlCreator.getArtUrlString(forArtistId: artistId) } - albumCountOfAllArtists += albumCount } } @@ -40,7 +36,6 @@ class SsArtistParserDelegate: GenericXmlLibParser { switch(elementName) { case "artist": parsedCount += 1 - parseNotifier?.notifyParsedObject() artistBuffer = nil default: break diff --git a/Amperfy/Api/Subsonic/SsPlaylistSongsParserDelegate.swift b/Amperfy/Api/Subsonic/SsPlaylistSongsParserDelegate.swift index 40fd5785..a423f154 100644 --- a/Amperfy/Api/Subsonic/SsPlaylistSongsParserDelegate.swift +++ b/Amperfy/Api/Subsonic/SsPlaylistSongsParserDelegate.swift @@ -19,13 +19,12 @@ class SsPlaylistSongsParserDelegate: GenericXmlParser { buffer = "" if elementName == "playlist" { - guard let playlistId = attributeDict["id"], - let attributePlaylistName = attributeDict["name"] else { - return - } + guard let playlistId = attributeDict["id"] else { return } playlistHasBeenDetected = true playlist.id = playlistId - playlist.name = attributePlaylistName + if let attributePlaylistName = attributeDict["name"] { + playlist.name = attributePlaylistName + } } if elementName == "entry" { diff --git a/Amperfy/Api/Subsonic/SsSongParserDelegate.swift b/Amperfy/Api/Subsonic/SsSongParserDelegate.swift index 5ae30703..4052a4ba 100644 --- a/Amperfy/Api/Subsonic/SsSongParserDelegate.swift +++ b/Amperfy/Api/Subsonic/SsSongParserDelegate.swift @@ -7,6 +7,8 @@ class SsSongParserDelegate: GenericXmlLibParser { private var subsonicUrlCreator: SubsonicUrlCreator private var songBuffer: Song? + var guessedArtist: Artist? + var guessedAlbum: Album? init(libraryStorage: LibraryStorage, syncWave: SyncWave, subsonicUrlCreator: SubsonicUrlCreator, parseNotifier: ParsedObjectNotifiable? = nil) { self.subsonicUrlCreator = subsonicUrlCreator @@ -19,7 +21,7 @@ class SsSongParserDelegate: GenericXmlLibParser { if(elementName == "song") { guard let songId = attributeDict["id"] else { return } - if !syncWave.isInitialWave, let fetchedSong = libraryStorage.getSong(id: songId) { + if let fetchedSong = libraryStorage.getSong(id: songId) { songBuffer = fetchedSong } else { songBuffer = libraryStorage.createSong() @@ -42,7 +44,9 @@ class SsSongParserDelegate: GenericXmlLibParser { } if let artistId = attributeDict["artistId"] { - if let artist = libraryStorage.getArtist(id: artistId) { + if let guessedArtist = guessedArtist, guessedArtist.id == artistId { + songBuffer?.artist = guessedArtist + } else if let artist = libraryStorage.getArtist(id: artistId) { songBuffer?.artist = artist } else { os_log("Found song id %s with unknown artist id %s", log: log, type: .error, songId, artistId) @@ -52,7 +56,9 @@ class SsSongParserDelegate: GenericXmlLibParser { } if let albumId = attributeDict["albumId"] { - if let album = libraryStorage.getAlbum(id: albumId) { + if let guessedAlbum = guessedAlbum, guessedAlbum.id == albumId { + songBuffer?.album = guessedAlbum + } else if let album = libraryStorage.getAlbum(id: albumId) { songBuffer?.album = album songBuffer?.artwork?.url = subsonicUrlCreator.getArtUrlString(forArtistId: albumId) } else { @@ -68,7 +74,6 @@ class SsSongParserDelegate: GenericXmlLibParser { switch(elementName) { case "song": parsedCount += 1 - parseNotifier?.notifyParsedObject() songBuffer = nil default: break diff --git a/Amperfy/Api/Subsonic/SubsonicLibraryBackgroundSyncer.swift b/Amperfy/Api/Subsonic/SubsonicLibraryBackgroundSyncer.swift index 731b7237..33a8dff2 100644 --- a/Amperfy/Api/Subsonic/SubsonicLibraryBackgroundSyncer.swift +++ b/Amperfy/Api/Subsonic/SubsonicLibraryBackgroundSyncer.swift @@ -73,6 +73,8 @@ class SubsonicLibraryBackgroundSyncer: GenericLibraryBackgroundSyncer, Backgroun os_log("Lib resync: Songs parsing start", log: log, type: .info) for album in albumsLeftSorted { let songDelegate = SsSongParserDelegate(libraryStorage: libraryStorage, syncWave: syncWave, subsonicUrlCreator: subsonicServerApi) + songDelegate.guessedArtist = album.artist + songDelegate.guessedAlbum = album subsonicServerApi.requestAlbum(parserDelegate: songDelegate, id: album.id) syncWave.syncIndexToContinue = album.id if(!isRunning) { break } diff --git a/Amperfy/Api/Subsonic/SubsonicLibrarySyncer.swift b/Amperfy/Api/Subsonic/SubsonicLibrarySyncer.swift index a134c928..0a3a9d06 100644 --- a/Amperfy/Api/Subsonic/SubsonicLibrarySyncer.swift +++ b/Amperfy/Api/Subsonic/SubsonicLibrarySyncer.swift @@ -22,21 +22,25 @@ class SubsonicLibrarySyncer: LibrarySyncer { statusNotifyier?.notifyArtistSyncStarted() let artistParser = SsArtistParserDelegate(libraryStorage: libraryStorage, syncWave: syncWave, subsonicUrlCreator: subsonicServerApi, parseNotifier: statusNotifyier) subsonicServerApi.requestArtists(parserDelegate: artistParser) - albumCount = artistParser.albumCountOfAllArtists - - statusNotifyier?.notifyAlbumsSyncStarted() + let artists = libraryStorage.getArtists() + albumCount = artists.count + statusNotifyier?.notifyAlbumsSyncStarted() for artist in artists { let albumDelegate = SsAlbumParserDelegate(libraryStorage: libraryStorage, syncWave: syncWave, subsonicUrlCreator: subsonicServerApi, parseNotifier: statusNotifyier) subsonicServerApi.requestArtist(parserDelegate: albumDelegate, id: artist.id) - songCount += albumDelegate.songCountOfAlbum + statusNotifyier?.notifyParsedObject() } - statusNotifyier?.notifySongsSyncStarted() let albums = libraryStorage.getAlbums() + songCount = albums.count + statusNotifyier?.notifySongsSyncStarted() for album in albums { let songDelegate = SsSongParserDelegate(libraryStorage: libraryStorage, syncWave: syncWave, subsonicUrlCreator: subsonicServerApi, parseNotifier: statusNotifyier) + songDelegate.guessedArtist = album.artist + songDelegate.guessedAlbum = album subsonicServerApi.requestAlbum(parserDelegate: songDelegate, id: album.id) + statusNotifyier?.notifyParsedObject() } statusNotifyier?.notifyPlaylistSyncStarted()