gitoria

jukebox

All repositories: gitoria

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

8.0 KB

  1. // lib/browse.hl — READING THE DATABASE FAST: pages of songs in the three orders, artists, albums, search, shuffle, stats.
  2. // The order lists (song ids with their play count, sorted by title / artist / album) are made once from the sorted
  3. // indexes of the songs table and kept in memory until a scan changes something; a page is a slice of such a list and
  4. // one lookup per row. Nothing here returns more than `maxLimit` rows. No writes (lib/library.hl and lib/catalog.hl write).
  5. import { random } from 'hl:math'
  6. import { songsTable, albumsTable, artistsTable, coversTable } from './db.hl'
  7. import { floor } from 'hl:math'
  8. import { fold } from './util.hl'
  9. import OrderCache from './order-cache.hl'
  10. static maxLimit = 200
  11. static defaultLimit = 50
  12. static cache = new OrderCache()
  13. static dropOrderCache = () => {
  14. cache.lists = {}
  15. cache.stats = null
  16. }
  17. static sortField = (sort) => {
  18. if (sort == 'title') { return 'sortTitle' }
  19. if (sort == 'album') { return 'sortAlbum' }
  20. return 'sortArtist'
  21. }
  22. // the lean rows { id, pc } of all songs in one order
  23. static orderList = (sort) => {
  24. field = sortField(sort)
  25. list = cache.lists[field]
  26. if (list != null) { return list }
  27. out = []
  28. for (r of songsTable.range(field, null, null)) { out.push({ id = r.id pc = r.playCount }) }
  29. lists = cache.lists
  30. lists[field] = out
  31. cache.lists = lists
  32. return out
  33. }
  34. // ---- what the API shows of a song / album / artist --------------------------------------------------------------
  35. static publicSong = (s) => {
  36. return {
  37. id = s.id title = s.title artist = s.artist albumArtist = s.albumArtist album = s.album track = s.track trackTotal = s.trackTotal
  38. disc = s.disc year = s.year genre = s.genre duration = s.duration format = s.format kind = s.kind size = s.size
  39. playCount = s.playCount rating = s.rating stars = s.stars popm = s.popm coverId = s.coverId path = s.path addedAt = s.addedAt
  40. }
  41. }
  42. static publicAlbum = (a) => {
  43. return { id = a.id name = a.name albumArtist = a.albumArtist year = a.year songCount = a.songCount duration = a.duration playCount = a.playCount coverId = a.coverId folder = a.folder }
  44. }
  45. static publicArtist = (a) => {
  46. return { id = a.id name = a.name songCount = a.songCount albumCount = a.albumCount playCount = a.playCount coverId = a.coverId }
  47. }
  48. static clampLimit = (limit) => {
  49. if (limit == null || limit < 1) { return defaultLimit }
  50. return limit > maxLimit ? maxLimit : limit
  51. }
  52. static clampOffset = (offset) => { return offset == null || offset < 0 ? 0 : offset }
  53. // rows ordered by one string field (insertion sort; small lists only: one album, one artist)
  54. static orderedBy = (rows, field) => {
  55. out = []
  56. for (r of rows) {
  57. let j = out.length
  58. out.push(r)
  59. while (j > 0 && out[j - 1][field] > r[field]) {
  60. out[j] = out[j - 1]
  61. j = j - 1
  62. }
  63. out[j] = r
  64. }
  65. return out
  66. }
  67. static page = (total, offset, limit, items) => { return { total = total offset = offset limit = limit items = items } }
  68. // ---- songs ------------------------------------------------------------------------------------------------------
  69. // params: sort (title | artist | album), offset, limit, minPlays, artist (an artist id), album (an album id)
  70. static songsPage = (sort, offset, limit, minPlays, artistId, albumId) => {
  71. off = clampOffset(offset)
  72. lim = clampLimit(limit)
  73. if (albumId != null) {
  74. a = albumsTable.fetch(albumId)
  75. if (a == null) { return page(0, off, lim, []) }
  76. return slicePage(orderedBy(songsTable.find('albumKey', a.key), 'sortAlbum'), off, lim, minPlays)
  77. }
  78. if (artistId != null) {
  79. a = artistsTable.fetch(artistId)
  80. if (a == null) { return page(0, off, lim, []) }
  81. return slicePage(orderedBy(songsTable.find('artistKey', a.key), 'sortArtist'), off, lim, minPlays)
  82. }
  83. list = orderList(sort)
  84. min = minPlays == null ? 0 : minPlays
  85. items = []
  86. let total = 0
  87. for (r of list) {
  88. if (r.pc < min) { continue }
  89. if (total >= off && items.length < lim) {
  90. s = songsTable.fetch(r.id)
  91. if (s != null) { items.push(publicSong(s)) }
  92. }
  93. total = total + 1
  94. }
  95. return page(total, off, lim, items)
  96. }
  97. // a page out of full rows already in order (an album, an artist)
  98. static slicePage = (rows, off, lim, minPlays) => {
  99. min = minPlays == null ? 0 : minPlays
  100. items = []
  101. let total = 0
  102. for (s of rows) {
  103. if (s.playCount < min) { continue }
  104. if (total >= off && items.length < lim) { items.push(publicSong(s)) }
  105. total = total + 1
  106. }
  107. return page(total, off, lim, items)
  108. }
  109. // `count` random songs (no repeats) among those played at least `minPlays` times
  110. static shuffle = (minPlays, count) => {
  111. lim = clampLimit(count)
  112. min = minPlays == null ? 0 : minPlays
  113. pool = []
  114. for (r of orderList('title')) {
  115. if (r.pc >= min) { pool.push(r.id) }
  116. }
  117. items = []
  118. let left = pool.length
  119. while (items.length < lim && left > 0) {
  120. k = floor(random() * left)
  121. id = pool[k]
  122. pool[k] = pool[left - 1]
  123. left = left - 1
  124. s = songsTable.fetch(id)
  125. if (s != null) { items.push(publicSong(s)) }
  126. }
  127. return page(pool.length, 0, lim, items)
  128. }
  129. // ---- artists and albums -----------------------------------------------------------------------------------------
  130. static artistsPage = (offset, limit) => {
  131. off = clampOffset(offset)
  132. lim = clampLimit(limit)
  133. rows = artistsTable.range('sortName', null, null)
  134. items = []
  135. let i = off
  136. while (i < rows.length && items.length < lim) {
  137. items.push(publicArtist(rows[i]))
  138. i = i + 1
  139. }
  140. return page(rows.length, off, lim, items)
  141. }
  142. static albumsPage = (offset, limit, artistId) => {
  143. off = clampOffset(offset)
  144. lim = clampLimit(limit)
  145. let rows = []
  146. if (artistId != null) {
  147. a = artistsTable.fetch(artistId)
  148. if (a != null) { rows = orderedBy(albumsTable.find('artistKey', a.key), 'sortName') }
  149. } else {
  150. rows = albumsTable.range('sortName', null, null)
  151. }
  152. items = []
  153. let i = off
  154. while (i < rows.length && items.length < lim) {
  155. items.push(publicAlbum(rows[i]))
  156. i = i + 1
  157. }
  158. return page(rows.length, off, lim, items)
  159. }
  160. // ---- search -----------------------------------------------------------------------------------------------------
  161. // every word of q must be in the song's folded artist / title / album / genre text
  162. static searchAll = (q, offset, limit) => {
  163. off = clampOffset(offset)
  164. lim = clampLimit(limit)
  165. terms = []
  166. for (t of fold(q).split(' ')) {
  167. if (t != '') { terms.push(t) }
  168. }
  169. if (terms.length == 0) { return { q = q songs = page(0, off, lim, []) artists = [] albums = [] } }
  170. items = []
  171. let total = 0
  172. for (s of songsTable.iterate(null, null)) {
  173. let ok = true
  174. for (t of terms) {
  175. if (!s.search.includes(t)) {
  176. ok = false
  177. break
  178. }
  179. }
  180. if (!ok) { continue }
  181. if (total >= off && items.length < lim) { items.push(publicSong(s)) }
  182. total = total + 1
  183. }
  184. artists = []
  185. for (a of artistsTable.range('sortName', null, null)) {
  186. if (artists.length >= 10) { break }
  187. let ok = true
  188. for (t of terms) {
  189. if (!a.sortName.includes(t)) { ok = false }
  190. }
  191. if (ok) { artists.push(publicArtist(a)) }
  192. }
  193. albums = []
  194. for (a of albumsTable.range('sortName', null, null)) {
  195. if (albums.length >= 10) { break }
  196. let ok = true
  197. for (t of terms) {
  198. if (!a.sortName.includes(t)) { ok = false }
  199. }
  200. if (ok) { albums.push(publicAlbum(a)) }
  201. }
  202. return { q = q songs = page(total, off, lim, items) artists = artists albums = albums }
  203. }
  204. // ---- stats ------------------------------------------------------------------------------------------------------
  205. static libraryStats = () => {
  206. if (cache.stats != null) { return cache.stats }
  207. let songs = 0
  208. let seconds = 0
  209. let bytes = 0
  210. let played1 = 0
  211. let played5 = 0
  212. let withCover = 0
  213. let videos = 0
  214. for (s of songsTable.iterate(null, null)) {
  215. songs = songs + 1
  216. seconds = seconds + (s.duration == null ? 0 : s.duration)
  217. bytes = bytes + (s.size == null ? 0 : s.size)
  218. if (s.playCount >= 1) { played1 = played1 + 1 }
  219. if (s.playCount >= 5) { played5 = played5 + 1 }
  220. if (s.coverId != null) { withCover = withCover + 1 }
  221. if (s.kind == 'video') { videos = videos + 1 }
  222. }
  223. st = { songs = songs artists = artistsTable.count() albums = albumsTable.count() covers = coversTable.count() songsWithCover = withCover
  224. videos = videos playedAtLeast1 = played1 playedAtLeast5 = played5 totalSeconds = seconds totalBytes = bytes }
  225. cache.stats = st
  226. return st
  227. }

Branches

Latest commits

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