gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd13987dfd13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemred13987df/lib/catalog.hl

2.4 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 + folder); 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 } from './util.hl'
  7. static idOf = (key) => { return textDigest(key).slice(0, 16) }
  8. // Rebuild both tables from scratch (one pass over the songs, then the old rows go and the new come). Answers
  9. // { albums, artists } counts.
  10. static rebuildCatalog = () => {
  11. albums = {}
  12. artists = {}
  13. for (s of songsTable.iterate(null, null)) {
  14. let a = albums[s.albumKey]
  15. if (a == null) {
  16. a = { id = idOf(s.albumKey) key = s.albumKey name = s.album albumArtist = s.albumArtist artistKey = s.artistKey folder = s.folder
  17. year = s.year songCount = 0 duration = 0 playCount = 0 coverId = null sortName = fold(s.album) + '|' + s.artistKey }
  18. }
  19. a.songCount = a.songCount + 1
  20. a.duration = a.duration + (s.duration == null ? 0 : s.duration)
  21. a.playCount = a.playCount + s.playCount
  22. if (a.coverId == null && s.coverId != null) { a.coverId = s.coverId }
  23. if (a.year == null && s.year != null) { a.year = s.year }
  24. albums[s.albumKey] = a
  25. let r = artists[s.artistKey]
  26. if (r == null) {
  27. r = { id = idOf('artist:' + s.artistKey) key = s.artistKey name = s.albumArtist songCount = 0 albumCount = 0 playCount = 0 coverId = null
  28. sortName = s.artistKey seenAlbums = {} }
  29. }
  30. r.songCount = r.songCount + 1
  31. r.playCount = r.playCount + s.playCount
  32. if (r.seenAlbums[s.albumKey] == null) {
  33. r.seenAlbums[s.albumKey] = true
  34. r.albumCount = r.albumCount + 1
  35. }
  36. if (r.coverId == null && s.coverId != null) { r.coverId = s.coverId }
  37. artists[s.artistKey] = r
  38. }
  39. // the old rows out
  40. for (old of albumsTable.find(null, null)) { albumsTable.delete(old.id) }
  41. for (old of artistsTable.find(null, null)) { artistsTable.delete(old.id) }
  42. let na = 0
  43. for (k in albums) {
  44. albumsTable.put(albums[k])
  45. na = na + 1
  46. }
  47. let nr = 0
  48. for (k in artists) {
  49. r = artists[k]
  50. 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 })
  51. nr = nr + 1
  52. }
  53. return { albums = na artists = nr }
  54. }

Branches

Latest commits

  • d13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemre