Skip to content

Commit

Permalink
Subsonic: increase lib sync performance
Browse files Browse the repository at this point in the history
  • Loading branch information
BLeeEZ committed Mar 30, 2021
1 parent f2c3fb0 commit 9218c1b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 39 deletions.
22 changes: 7 additions & 15 deletions Amperfy/Api/Subsonic/SsAlbumParserDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,44 +17,37 @@ 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()
}

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
}
}

func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
switch(elementName) {
case "album":
parsedCount += 1
parseNotifier?.notifyParsedObject()
albumBuffer = nil
default:
break
Expand Down
15 changes: 5 additions & 10 deletions Amperfy/Api/Subsonic/SsArtistParserDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,30 +16,26 @@ 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
}
}

func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
switch(elementName) {
case "artist":
parsedCount += 1
parseNotifier?.notifyParsedObject()
artistBuffer = nil
default:
break
Expand Down
9 changes: 4 additions & 5 deletions Amperfy/Api/Subsonic/SsPlaylistSongsParserDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
13 changes: 9 additions & 4 deletions Amperfy/Api/Subsonic/SsSongParserDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -68,7 +74,6 @@ class SsSongParserDelegate: GenericXmlLibParser {
switch(elementName) {
case "song":
parsedCount += 1
parseNotifier?.notifyParsedObject()
songBuffer = nil
default:
break
Expand Down
2 changes: 2 additions & 0 deletions Amperfy/Api/Subsonic/SubsonicLibraryBackgroundSyncer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
14 changes: 9 additions & 5 deletions Amperfy/Api/Subsonic/SubsonicLibrarySyncer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9218c1b

Please sign in to comment.