gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit12a2623312a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre12a26233/lib/browse.hl

9.1 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, safeKey } from './util.hl'
  9. import { cache, dropOrderCache } from './state.hl'
  10. import { showOf } from './dups.hl'
  11. import { jbCount } from './plays.hl'
  12. static maxLimit = 200
  13. static defaultLimit = 50
  14. static sortField = (sort) => {
  15. if (sort == 'title') { return 'sortTitle' }
  16. if (sort == 'album') { return 'sortAlbum' }
  17. if (sort == 'plays') { return 'sortTitle' }
  18. return 'sortArtist'
  19. }
  20. // the lean rows { id, pc } of all songs in one order
  21. static orderList = (sort) => {
  22. field = sortField(sort)
  23. list = cache.lists[field]
  24. if (list != null) { return list }
  25. let out = []
  26. // a song with copies is listed once (its first copy), with the highest play count of the group
  27. for (r of songsTable.range(field, null, null)) {
  28. w = showOf(r.id, r.playCount)
  29. if (w.shown) { out.push({ id = r.id pc = w.plays }) }
  30. }
  31. // the most played first, by a counting sort over the play counts (the index of playCount sorts the numbers as text: 9 before 12)
  32. if (sort == 'plays') {
  33. let top = 0
  34. for (r of out) { if (r.pc > top) { top = r.pc } }
  35. buckets = []
  36. let b = 0
  37. while (b <= top) {
  38. buckets.push([])
  39. b = b + 1
  40. }
  41. for (r of out) { buckets[r.pc].push(r) }
  42. let sorted = []
  43. let k = top
  44. while (k >= 0) {
  45. for (r of buckets[k]) { sorted.push(r) }
  46. k = k - 1
  47. }
  48. out = sorted
  49. }
  50. lists = cache.lists
  51. lists[field] = out
  52. cache.lists = lists
  53. return out
  54. }
  55. // ---- what the API shows of a song / album / artist --------------------------------------------------------------
  56. static publicSong = (s) => {
  57. w = showOf(s.id, s.playCount)
  58. return {
  59. id = s.id title = s.title artist = s.artist albumArtist = s.albumArtist album = s.album track = s.track trackTotal = s.trackTotal
  60. disc = s.disc year = s.year genre = s.genre duration = s.duration format = s.format kind = s.kind size = s.size
  61. playCount = w.plays filePlays = s.playCount jukeboxPlays = jbCount(s.id) copies = w.copies rating = s.rating stars = s.stars popm = s.popm coverId = s.coverId path = s.path addedAt = s.addedAt
  62. }
  63. }
  64. static publicAlbum = (a) => {
  65. 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 }
  66. }
  67. static publicArtist = (a) => {
  68. return { id = a.id name = a.name songCount = a.songCount albumCount = a.albumCount playCount = a.playCount coverId = a.coverId }
  69. }
  70. static clampLimit = (limit) => {
  71. if (limit == null || limit < 1) { return defaultLimit }
  72. return limit > maxLimit ? maxLimit : limit
  73. }
  74. static clampOffset = (offset) => { return offset == null || offset < 0 ? 0 : offset }
  75. // rows ordered by one string field (insertion sort; small lists only: one album, one artist)
  76. static orderedBy = (rows, field) => {
  77. out = []
  78. for (r of rows) {
  79. let j = out.length
  80. out.push(r)
  81. while (j > 0 && out[j - 1][field] > r[field]) {
  82. out[j] = out[j - 1]
  83. j = j - 1
  84. }
  85. out[j] = r
  86. }
  87. return out
  88. }
  89. static page = (total, offset, limit, items) => { return { total = total offset = offset limit = limit items = items } }
  90. // ---- songs ------------------------------------------------------------------------------------------------------
  91. // params: sort (title | artist | album | plays), offset, limit, minPlays, artist (an artist id), album (an album id)
  92. static songsPage = (sort, offset, limit, minPlays, artistId, albumId) => {
  93. off = clampOffset(offset)
  94. lim = clampLimit(limit)
  95. if (albumId != null) {
  96. a = albumsTable.fetch(albumId)
  97. if (a == null) { return page(0, off, lim, []) }
  98. return slicePage(orderedBy(songsTable.find('albumKey', a.key), 'sortAlbum'), off, lim, minPlays)
  99. }
  100. if (artistId != null) {
  101. a = artistsTable.fetch(artistId)
  102. if (a == null) { return page(0, off, lim, []) }
  103. return slicePage(orderedBy(songsTable.find('artistKey', a.key), 'sortArtist'), off, lim, minPlays)
  104. }
  105. list = orderList(sort)
  106. min = minPlays == null ? 0 : minPlays
  107. items = []
  108. let total = 0
  109. for (r of list) {
  110. if (r.pc < min) { continue }
  111. if (total >= off && items.length < lim) {
  112. s = songsTable.fetch(r.id)
  113. if (s != null) { items.push(publicSong(s)) }
  114. }
  115. total = total + 1
  116. }
  117. return page(total, off, lim, items)
  118. }
  119. // a page out of full rows already in order (an album, an artist)
  120. static slicePage = (rows, off, lim, minPlays) => {
  121. min = minPlays == null ? 0 : minPlays
  122. items = []
  123. let total = 0
  124. for (s of rows) {
  125. w = showOf(s.id, s.playCount)
  126. if (!w.shown || w.plays < min) { continue }
  127. if (total >= off && items.length < lim) { items.push(publicSong(s)) }
  128. total = total + 1
  129. }
  130. return page(total, off, lim, items)
  131. }
  132. // `count` random songs (no repeats) among those played at least `minPlays` times
  133. static shuffle = (minPlays, count) => {
  134. lim = clampLimit(count)
  135. min = minPlays == null ? 0 : minPlays
  136. pool = []
  137. for (r of orderList('title')) {
  138. if (r.pc >= min) { pool.push(r.id) }
  139. }
  140. items = []
  141. let left = pool.length
  142. while (items.length < lim && left > 0) {
  143. k = floor(random() * left)
  144. id = pool[k]
  145. pool[k] = pool[left - 1]
  146. left = left - 1
  147. s = songsTable.fetch(id)
  148. if (s != null) { items.push(publicSong(s)) }
  149. }
  150. return page(pool.length, 0, lim, items)
  151. }
  152. // ---- artists and albums -----------------------------------------------------------------------------------------
  153. static artistsPage = (offset, limit) => {
  154. off = clampOffset(offset)
  155. lim = clampLimit(limit)
  156. rows = artistsTable.range('sortName', null, null)
  157. items = []
  158. let i = off
  159. while (i < rows.length && items.length < lim) {
  160. items.push(publicArtist(rows[i]))
  161. i = i + 1
  162. }
  163. return page(rows.length, off, lim, items)
  164. }
  165. static albumsPage = (offset, limit, artistId) => {
  166. off = clampOffset(offset)
  167. lim = clampLimit(limit)
  168. let rows = []
  169. if (artistId != null) {
  170. a = artistsTable.fetch(artistId)
  171. if (a != null) { rows = orderedBy(albumsTable.find('artistKey', a.key), 'sortName') }
  172. } else {
  173. rows = albumsTable.range('sortName', null, null)
  174. }
  175. items = []
  176. let i = off
  177. while (i < rows.length && items.length < lim) {
  178. items.push(publicAlbum(rows[i]))
  179. i = i + 1
  180. }
  181. return page(rows.length, off, lim, items)
  182. }
  183. // ---- search -----------------------------------------------------------------------------------------------------
  184. // every word of q must be in the song's folded artist / title / album / genre text
  185. static searchAll = (q, offset, limit) => {
  186. off = clampOffset(offset)
  187. lim = clampLimit(limit)
  188. terms = []
  189. let words = []
  190. for (t of fold(q).split(' ')) {
  191. if (t != '') { words.push(t) }
  192. }
  193. for (t of words) { terms.push(t) }
  194. // artists and albums are matched on their index keys, where a comma is character 2
  195. keyTerms = []
  196. for (t of words) { keyTerms.push(safeKey(t)) }
  197. if (terms.length == 0) { return { q = q songs = page(0, off, lim, []) artists = [] albums = [] } }
  198. items = []
  199. let total = 0
  200. for (s of songsTable.iterate(null, null)) {
  201. let ok = true
  202. for (t of terms) {
  203. if (!s.search.includes(t)) {
  204. ok = false
  205. break
  206. }
  207. }
  208. if (!ok) { continue }
  209. if (!showOf(s.id, s.playCount).shown) { continue }
  210. if (total >= off && items.length < lim) { items.push(publicSong(s)) }
  211. total = total + 1
  212. }
  213. artists = []
  214. for (a of artistsTable.range('sortName', null, null)) {
  215. if (artists.length >= 10) { break }
  216. let ok = true
  217. for (t of keyTerms) {
  218. if (!a.sortName.includes(t)) { ok = false }
  219. }
  220. if (ok) { artists.push(publicArtist(a)) }
  221. }
  222. albums = []
  223. for (a of albumsTable.range('sortName', null, null)) {
  224. if (albums.length >= 10) { break }
  225. let ok = true
  226. for (t of keyTerms) {
  227. if (!a.sortName.includes(t)) { ok = false }
  228. }
  229. if (ok) { albums.push(publicAlbum(a)) }
  230. }
  231. return { q = q songs = page(total, off, lim, items) artists = artists albums = albums }
  232. }
  233. // ---- stats ------------------------------------------------------------------------------------------------------
  234. static libraryStats = () => {
  235. if (cache.stats != null) { return cache.stats }
  236. let songs = 0
  237. let seconds = 0
  238. let bytes = 0
  239. let played1 = 0
  240. let played5 = 0
  241. let withCover = 0
  242. let videos = 0
  243. for (s of songsTable.iterate(null, null)) {
  244. songs = songs + 1
  245. seconds = seconds + (s.duration == null ? 0 : s.duration)
  246. bytes = bytes + (s.size == null ? 0 : s.size)
  247. if (s.playCount >= 1) { played1 = played1 + 1 }
  248. if (s.playCount >= 5) { played5 = played5 + 1 }
  249. if (s.coverId != null) { withCover = withCover + 1 }
  250. if (s.kind == 'video') { videos = videos + 1 }
  251. }
  252. st = { songs = songs artists = artistsTable.count() albums = albumsTable.count() covers = coversTable.count() songsWithCover = withCover
  253. videos = videos playedAtLeast1 = played1 playedAtLeast5 = played5 totalSeconds = seconds totalBytes = bytes }
  254. cache.stats = st
  255. return st
  256. }

Branches

Latest commits

  • 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
  • 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
  • 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
  • c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
  • 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre
  • 59c1373ejukebox JB3 (work in progress): albums without folder and with disc markers folded, compilations, duplicate songs, own play counter, server-side shuffle queuemre
  • 2a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre
  • 93a44212jukebox: report, numbers of the full scan, notes on the index key rulemre
  • bf5167dajukebox: index keys without commas (mpackdb loses them after a reopen), comma fixture and gate checksmre
  • 28a82fa2jukebox: README, STATUS, LOG, DECISIONSmre
  • d13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemre