gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf22f9aacf22f9aacmain: format styles with nested rules and bare queriesmref22f9aac/lib/catalog.hl

4.7 KB

  1. // lib/catalog.hl — THE DERIVED TABLES (albums, artists): built from the songs table after a scan that changed anything.
  2. // An album is one albumKey (album artist + album name without the CD marker; not the folder); a song with copies counts once; an artist is one artistKey. Both get a stable primary
  3. // key (a digest of the key), so an id from the API stays valid across rebuilds. All writes to those two tables are here.
  4. import { textDigest } from 'hl:fs'
  5. import { songsTable, albumsTable, artistsTable, countOfRows } from './db.hl'
  6. import { fold, safeKey, SEP } from './util.hl'
  7. import { albumKeyOf, sortKeys } from './albums.hl'
  8. import { showOf } from './dups.hl'
  9. import { dropOrderCache } from './state.hl'
  10. static variousName = 'Various Artists'
  11. // an album name that says nothing ("Unknown album", "Unbekannt", "Unbekannter Titel"): songs with it are never taken for one compilation
  12. static unknownAlbum = (name) => {
  13. f = fold(name)
  14. return f == '' || f.startsWith('unknown') || f.startsWith('unbekannt')
  15. }
  16. // A COMPILATION has no album artist tag, so each of its songs would be an album of its own. When three or more different artists share an album name in one folder
  17. // (and none of them has a real album artist), those songs get the album artist "Various Artists" and one album key. Their artist key stays their own, so an artist's page
  18. // still lists the songs. Idempotent; only songs whose keys change are written. Answers how many were changed.
  19. static fixCompilations = () => {
  20. groups = {}
  21. for (s of songsTable.iterate(null, null)) {
  22. if (unknownAlbum(s.album)) { continue }
  23. if (s.albumArtist != s.artist && s.albumArtist != variousName) { continue }
  24. k = fold(s.album) + SEP + s.folder
  25. let g = groups[k]
  26. if (g == null) { g = {} }
  27. g[fold(s.artist)] = true
  28. groups[k] = g
  29. }
  30. let changed = 0
  31. rows = []
  32. for (s of songsTable.iterate(null, null)) { rows.push(s) }
  33. for (s of rows) {
  34. if (unknownAlbum(s.album)) { continue }
  35. if (s.albumArtist != s.artist && s.albumArtist != variousName) { continue }
  36. g = groups[fold(s.album) + SEP + s.folder]
  37. let many = 0
  38. for (a in g) { many = many + 1 }
  39. want = many >= 3 ? variousName : s.artist
  40. if (want == s.albumArtist) { continue }
  41. keys = sortKeys(want, s.album, s.disc, s.track == null ? 0 : s.track, s.title)
  42. rec = {}
  43. for (f in s) {
  44. if (f != 'id') { rec[f] = s[f] }
  45. }
  46. rec.albumArtist = want
  47. rec.albumKey = albumKeyOf(want, s.album)
  48. rec.sortArtist = keys.sortArtist
  49. rec.sortAlbum = keys.sortAlbum
  50. songsTable.update(s.id, rec)
  51. changed = changed + 1
  52. }
  53. return changed
  54. }
  55. static idOf = (key) => { return textDigest(key).slice(0, 16) }
  56. // Rebuild both tables from scratch (one pass over the songs, then the old rows go and the new come). Answers
  57. // { albums, artists } counts.
  58. static rebuildCatalog = () => {
  59. fixCompilations()
  60. albums = {}
  61. artists = {}
  62. dropOrderCache()
  63. for (s of songsTable.iterate(null, null)) {
  64. w = showOf(s.id, s.playCount)
  65. if (!w.shown) { continue }
  66. let a = albums[s.albumKey]
  67. if (a == null) {
  68. a = { id = idOf(s.albumKey) key = s.albumKey name = s.album albumArtist = s.albumArtist artistKey = s.albumArtist == variousName ? safeKey(fold(variousName)) : s.artistKey folder = s.folder
  69. year = s.year songCount = 0 duration = 0 discs = 1 playCount = 0 coverId = null sortName = safeKey(fold(s.album) + '|' + s.artistKey) }
  70. }
  71. a.songCount = a.songCount + 1
  72. if (s.disc != null && s.disc > a.discs) { a.discs = s.disc }
  73. a.duration = a.duration + (s.duration == null ? 0 : s.duration)
  74. a.playCount = a.playCount + w.plays
  75. if (a.coverId == null && s.coverId != null) { a.coverId = s.coverId }
  76. if (a.year == null && s.year != null) { a.year = s.year }
  77. albums[s.albumKey] = a
  78. let r = artists[s.artistKey]
  79. if (r == null) {
  80. r = { id = idOf('artist:' + s.artistKey) key = s.artistKey name = s.albumArtist == variousName ? s.artist : s.albumArtist songCount = 0 albumCount = 0 playCount = 0 coverId = null
  81. sortName = s.artistKey seenAlbums = {} }
  82. }
  83. r.songCount = r.songCount + 1
  84. r.playCount = r.playCount + w.plays
  85. if (r.seenAlbums[s.albumKey] == null) {
  86. r.seenAlbums[s.albumKey] = true
  87. r.albumCount = r.albumCount + 1
  88. }
  89. if (r.coverId == null && s.coverId != null) { r.coverId = s.coverId }
  90. artists[s.artistKey] = r
  91. }
  92. // the old rows out
  93. for (old of albumsTable.find(null, null)) { albumsTable.delete(old.id) }
  94. for (old of artistsTable.find(null, null)) { artistsTable.delete(old.id) }
  95. let na = 0
  96. for (k in albums) {
  97. albumsTable.put(albums[k])
  98. na = na + 1
  99. }
  100. let nr = 0
  101. for (k in artists) {
  102. r = artists[k]
  103. artistsTable.put({ id = r.id key = r.key name = r.name songCount = r.songCount albumCount = r.albumCount playCount = r.playCount coverId = r.coverId sortName = r.sortName })
  104. nr = nr + 1
  105. }
  106. return { albums = na artists = nr }
  107. }

Branches

Latest commits

  • f22f9aacmain: format styles with nested rules and bare queriesmre
  • f56cd7c7Sync icons from 8176eb1 and verify mixed component sourcesmre
  • d56e5054Sync theme and shared tokens from layouts 17e0f03mre
  • b1362547Re-vendor to Hybriel set day35mre
  • 89e10f08Records: mission 002 rollback wordingmre
  • 9485dee7Records: mission 002 brief (live step)mre
  • 9ba68fe7Records: S1b reviewedmre
  • 193dfff1Records: S1 result, S1b startedmre
  • 028ed776Records: mission 001 continuation S1bmre
  • 1838d7e8Records: S1 startedmre
  • a43fca3fRecords: mission 001 decided pointsmre
  • 41773828Records: statusmre
  • a82c0805Records: mission 001 brief (Style form)mre
  • a4bce6d1Records: ticket push subscriptionmre
  • 14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre
  • 0433e9a8jukebox: records: gitoria remote added and pushedmre
  • 1722d707jukebox: records: repo creation handed to Gitoriamre
  • b42e7dfbjukebox: conductor records (CONDUCTOR.md, STATUS.md), old STATUS archivedmre
  • 1b0d0bc1jukebox: a startup error prints its message before exit 1 (hybriel#247)mre
  • b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre