gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitad529ac1ad529ac1log: m572 verdictmread529ac1/lib/catalog.hl

15.2 KB

  1. // catalog.hl — THE PUBLIC LISTS (tracker.worldapi.org#13): every movie, newest release first, and every series, the one
  2. // whose newest RELEASED episode is newest first — the homepage's rows (10 each) and /movies, /series, /shows (24 per page).
  3. // Movies and series are both rows of `shows` (`type` 'movie' / 'series', the old tracker's shape).
  4. //
  5. // BUILT ONCE when the app starts — one scan of shows and of episodes (real data: 9,453 shows, 232k episodes, ~1 s) — and
  6. // kept in memory as two sorted lists of { id, key }. A request only slices its page and fetches those 24 shows: no scan.
  7. // Kept up to date without a rebuild:
  8. // - the daily TMDB sync (jobs.hl syncTick) calls `refreshCatalogShow(id)` after each show: its entry is recomputed
  9. // from its own seasons/episodes (new episodes, moved release dates) and moved to its new place;
  10. // - a NEW DAY: an episode upcoming yesterday may be out today — each series keeps the release dates of its upcoming
  11. // episodes (`upcoming`), and the first read of a new day moves the ones that are out into `latest` and re-sorts.
  12. // tools/sync-tmdb.hl runs with the app stopped (hybriel#21): the next start builds the lists afresh.
  13. //
  14. // ADULT TITLES (mission 054): only a title with `adult == false` (shows.hl isPublicTitle) is in the two lists — unknown is
  15. // hidden. The adult backfill (jobs.hl backfillTick) calls `refreshCatalogShow(id)` per title, so the lists fill as the
  16. // flags arrive. The series' release maps (latest/upcoming/release) hold every series, shown or not.
  17. //
  18. // ORDER: a movie by its `release` ('YYYY-MM-DD'; none → last). A series by its newest released episode (release ≤
  19. // today, UTC like the rest of the app); a series with none released yet comes after all that have one, by its own
  20. // `release`. Ties keep the table's order. `key` is one string so one comparison sorts: series '1<date>' / '0<date>'.
  21. //
  22. // SERIES AND SHOWS (tracker.worldapi.org#21, mission 068): the TV titles are two lists by their kind (shows.hl `titleKind`) —
  23. // /series and the homepage's Series row, /shows and its Shows row — both in the order above. `refreshCatalogShow` puts a title
  24. // into the list of its CURRENT kind (the kind backfill, jobs.hl kindTick, calls it when TMDB's type moves a title).
  25. //
  26. // GENRES (mission 058, tracker#25, /genres/<genre>): per genre id one list of its public movies AND series, newest first by
  27. // ONE date for both kinds: a movie's `release`, a series' newest released episode (none → its own `release`). Built in the
  28. // same scans; `refreshCatalogShow` moves the title in the lists of its old genres (`genresOf`) and its current ones.
  29. import { todayStr, dateOr, sortDesc } from './util.hl'
  30. import { warm } from './warm.hl'
  31. import { genresTable, showById, seasonsOfShow, episodesOfSeason, showYear, posterUrlOf, isPublicTitle, titleKind, titlePath, cardOf, posterUrlOfName, refreshTitleCaches } from './shows.hl'
  32. static pageSize = 24
  33. static seriesKey = (&latest, &release) => { return latest != null && latest != '' ? '1' + latest : '0' + release }
  34. // a series in a genre list: the plain date (comparable with a movie's release) — its newest released episode, else its release
  35. static genreKey = (&latest, &release) => { return latest != null && latest != '' ? latest : release }
  36. // ---- the lists' entries (sorted by util.hl sortDesc) -----------------------------------------------------------
  37. // SL1 (stall 2026-11-03): the lists hold every public title (movies 150 K, a genre up to 100 K) and a refresh used to rebuild the
  38. // WHOLE list per title (`placed` / `without` answered a new list, copied once more on the assignment): 120 ms per movie list, 190 ms for
  39. // its genres, per title a person fill or the details repair stored — 40 titles of a filmography blocked the loop for 10 s. Now the list
  40. // is changed IN PLACE through `&`: the title's old entry is found and cut out (one scan, no copy), the new one is put in at the place a
  41. // binary search finds (the lists are sorted by key, largest first). The place is the one the linear form gave: before the first entry
  42. // with a smaller key, so after the entries with an equal key (ties keep their order).
  43. static indexOfId = (&list, &id) => {
  44. let i = 0
  45. for (x of list) {
  46. if (x.id == id) { return i }
  47. i = i + 1
  48. }
  49. return -1
  50. }
  51. // one entry out of the list (a title that is not public, mission 054; or before it is put in again)
  52. static takeOut = (&list, &id) => {
  53. at = indexOfId(&list, &id)
  54. if (at >= 0) { list.splice(at, 1) }
  55. return null
  56. }
  57. // one entry moved to its place: out of the list, then in before the first entry with a smaller key. `absent` = the caller knows the
  58. // list has no entry of this id (a new title; the genre lists after refreshGenres cut the old entries): the scan is left out (it is
  59. // 30 ms for 150 K entries in the real process, the rest of the work a few ms)
  60. static placeIn = (&list, entry, absent) => {
  61. if (absent != true) { takeOut(&list, &entry.id) }
  62. let lo = 0
  63. let hi = list.length
  64. while (lo < hi) {
  65. mid = (lo + hi - (lo + hi) % 2) / 2
  66. if (list[mid].key >= entry.key) { lo = mid + 1 } else { hi = mid }
  67. }
  68. list.splice(lo, 0, entry)
  69. return null
  70. }
  71. // ---- the index -------------------------------------------------------------------------------------------------
  72. // mission 043: filled by ONE streamed pass per table (bootscan.hl: every show → noteIndexShow, every episode → noteIndexEpisode, then
  73. // finishIndex) — never a whole table in memory. `index` is filled in place (a static cannot be reassigned); `pending` holds what the
  74. // show pass collects for the finish.
  75. static index = { day = '' movies = [] series = [] shows = [] latest = {} upcoming = {} release = {} genres = {} genresOf = {} }
  76. static pending = { seriesIds = [] showIds = [] genreTitles = [] }
  77. static beginIndex = () => {
  78. index.day = todayStr()
  79. return null
  80. }
  81. static noteIndexShow = (&s) => {
  82. if (s.type == 'movie') { if (isPublicTitle(&s)) { index.movies.push({ id = s.id key = dateOr(&s.release) }) } }
  83. else if (s.type == 'series') {
  84. if (isPublicTitle(&s)) { if (titleKind(&s) == 'show') { pending.showIds.push(s.id) } else { pending.seriesIds.push(s.id) } }
  85. index.release[s.id] = dateOr(&s.release)
  86. }
  87. if ((s.type == 'movie' || s.type == 'series') && isPublicTitle(&s) && s.genres != null && s.genres.length > 0) {
  88. pending.genreTitles.push({ id = s.id movie = s.type == 'movie' release = dateOr(&s.release) genres = s.genres })
  89. }
  90. return null
  91. }
  92. static noteIndexEpisode = (&e) => {
  93. // tracker#31: a merged episode is its keeper episode's twin (same date) — not counted twice
  94. if (e.show != null && e.mergedInto == null && dateOr(&e.release) != '') {
  95. if (e.release <= index.day) {
  96. let l = index.latest[e.show]
  97. if (l == null || e.release > l) { index.latest[e.show] = e.release }
  98. } else {
  99. // mission 037: pushed IN PLACE — `let u = upcoming[x]; u.push(…); upcoming[x] = u` copies the list twice per episode
  100. // (plain data copies on assignment), quadratic in a list's length
  101. if (index.upcoming[e.show] == null) { index.upcoming[e.show] = [] }
  102. index.upcoming[e.show].push(e.release)
  103. }
  104. }
  105. return null
  106. }
  107. static finishIndex = () => {
  108. for (id of pending.seriesIds) { index.series.push({ id = id key = seriesKey(&index.latest[id], &index.release[id]) }) }
  109. for (id of pending.showIds) { index.shows.push({ id = id key = seriesKey(&index.latest[id], &index.release[id]) }) }
  110. byGenre = {}
  111. // mission 037: each genre's list grows IN PLACE. The copy-push-store form (`let list = byGenre[g]` … `byGenre[g] = list`)
  112. // copied the list twice per title — quadratic: with the filmography titles (Drama 25,364 of 59,696 public titles, 2026-10-19)
  113. // the boot spent over 20 minutes here, on the old vendored binary as on the new one, before the first log line
  114. for (t of pending.genreTitles) {
  115. let key = t.movie ? t.release : genreKey(&index.latest[t.id], &t.release)
  116. for (g of t.genres) {
  117. if (byGenre[g] == null) { byGenre[g] = [] }
  118. byGenre[g].push({ id = t.id key = key })
  119. }
  120. index.genresOf[t.id] = t.genres
  121. }
  122. for (g of byGenre.keys()) { index.genres[g] = sortDesc(&byGenre[g]) }
  123. index.movies = sortDesc(&index.movies)
  124. index.series = sortDesc(&index.series)
  125. index.shows = sortDesc(&index.shows)
  126. pending.seriesIds = []
  127. pending.showIds = []
  128. pending.genreTitles = []
  129. return null
  130. }
  131. // a TV list with every key from the current release maps, sorted again
  132. static rekeyed = (&list) => {
  133. out = []
  134. for (x of list) { out.push({ id = x.id key = seriesKey(&index.latest[x.id], &index.release[x.id]) }) }
  135. return sortDesc(&out)
  136. }
  137. // a new day: upcoming episodes that are out now become their series' newest released one
  138. static rollDay = () => {
  139. // mission T17: not while the start-up scan is still reading the episodes (it classifies them by `index.day`)
  140. if (warm.running && !warm.catalog) { return null }
  141. today = todayStr()
  142. if (index.day == today) { return null }
  143. let moved = false
  144. for (id of index.upcoming.keys()) {
  145. let rest = []
  146. let l = index.latest[id]
  147. for (d of index.upcoming[id]) {
  148. if (d <= today) {
  149. if (l == null || d > l) { l = d }
  150. moved = true
  151. } else { rest.push(d) }
  152. }
  153. index.upcoming[id] = rest
  154. if (l != null) { index.latest[id] = l }
  155. }
  156. if (moved) {
  157. index.series = rekeyed(&index.series)
  158. index.shows = rekeyed(&index.shows)
  159. // the genre lists too: a series' key is its newest released episode (movies keep theirs)
  160. for (g of index.genres.keys()) {
  161. let list = []
  162. for (x of index.genres[g]) { list.push({ id = x.id key = index.release[x.id] != null ? genreKey(&index.latest[x.id], &index.release[x.id]) : x.key }) }
  163. index.genres[g] = sortDesc(&list)
  164. }
  165. }
  166. index.day = today
  167. return null
  168. }
  169. // one title out of the lists of the genres it was in, then (when public) into those of its current genres
  170. static refreshGenres = (&show, &shown, key) => {
  171. old = index.genresOf[show.id]
  172. if (old != null) {
  173. for (g of old) { if (index.genres[g] != null) { takeOut(&index.genres[g], &show.id) } }
  174. }
  175. index.genresOf[show.id] = null
  176. if (!shown || show.genres == null || show.genres.length == 0) { return null }
  177. for (g of show.genres) {
  178. if (index.genres[g] == null) { index.genres[g] = [] }
  179. placeIn(&index.genres[g], { id = show.id key = key }, true)
  180. }
  181. index.genresOf[show.id] = show.genres
  182. return null
  183. }
  184. // after the sync (or the adult backfill) touched a show: its entry from its own data again (only that show's
  185. // seasons/episodes are read) — or out of the list when it is not public (adult or unknown, mission 054)
  186. // `isNew` = the title was stored just now (a person fill's filmography): it is in no list yet, so nothing is looked for in them
  187. static refreshCatalogShow = (&showId, isNew) => {
  188. rollDay()
  189. show = showById(&showId)
  190. if (show == null) { return null }
  191. // tracker#30: the lists' slim cache of this title (shows.hl cardOf / episodeRowsOf) from the changed record
  192. refreshTitleCaches(&show)
  193. shown = isPublicTitle(&show)
  194. if (show.type == 'movie') {
  195. if (shown) { placeIn(&index.movies, { id = show.id key = dateOr(&show.release) }, isNew == true) } else if (isNew != true) { takeOut(&index.movies, &show.id) }
  196. refreshGenres(&show, &shown, dateOr(&show.release))
  197. return null
  198. }
  199. if (show.type != 'series') { return null }
  200. today = index.day
  201. let l = null
  202. up = []
  203. for (s of seasonsOfShow(&show)) {
  204. for (e of episodesOfSeason(&s)) {
  205. if (dateOr(&e.release) != '') {
  206. if (e.release <= today) { if (l == null || e.release > l) { l = e.release } } else { up.push(e.release) }
  207. }
  208. }
  209. }
  210. index.latest[show.id] = l
  211. index.upcoming[show.id] = up
  212. index.release[show.id] = dateOr(&show.release)
  213. // in the list of its kind (#21), out of the other one
  214. entry = { id = show.id key = seriesKey(&l, &index.release[show.id]) }
  215. isShow = titleKind(&show) == 'show'
  216. if (shown && !isShow) { placeIn(&index.series, entry, isNew == true) } else if (isNew != true) { takeOut(&index.series, &show.id) }
  217. if (shown && isShow) { placeIn(&index.shows, entry, isNew == true) } else if (isNew != true) { takeOut(&index.shows, &show.id) }
  218. refreshGenres(&show, &shown, genreKey(&l, &index.release[show.id]))
  219. return null
  220. }
  221. // ---- what the pages read ---------------------------------------------------------------------------------------
  222. // one poster tile: poster, title, year, the title's page (/movies/, /series/ or /shows/<slug>, shows.hl titlePath — one
  223. // component for all three, with its Follow button). tracker#30 (mission 025): from the slim cached card (shows.hl cardOf),
  224. // not the whole record with its cast and crew
  225. static tilesOf = (&entries) => {
  226. out = []
  227. for (x of entries) {
  228. let c = cardOf(&x.id)
  229. if (c != null) { out.push({ id = c.id title = c.title year = c.year href = c.href posterUrl = posterUrlOfName(&c.posterName) }) }
  230. }
  231. return out
  232. }
  233. // the homepage: the n latest RELEASED movies (none dated after today) and the n series with the newest released episode
  234. static latestMovies = (&n) => {
  235. rollDay()
  236. want = []
  237. for (x of index.movies) {
  238. if (want.length < n && x.key != '' && x.key <= index.day) { want.push(x) }
  239. if (want.length >= n) { return tilesOf(&want) }
  240. }
  241. return tilesOf(&want)
  242. }
  243. static latestSeries = (&n) => { rollDay() return latestTv(&index.series, &n) }
  244. static latestShows = (&n) => { rollDay() return latestTv(&index.shows, &n) }
  245. static latestTv = (&list, &n) => {
  246. want = []
  247. for (x of list) {
  248. if (want.length < n && x.key.slice(0, 1) == '1') { want.push(x) }
  249. if (want.length >= n) { return tilesOf(&want) }
  250. }
  251. return tilesOf(&want)
  252. }
  253. // a page number from the address (`/movies/page/:page`, a String) — anything that is not a whole number ≥ 1 is page 1;
  254. // past the last page is the last page
  255. static pageNumberOf = (&raw, &pages) => {
  256. if (raw == null) { return 1 }
  257. s = '' + raw
  258. if (s == '' || s.length > 6) { return 1 }
  259. let i = 0
  260. while (i < s.length) {
  261. let c = s.charCodeAt(i)
  262. if (c < 48 || c > 57) { return 1 }
  263. i = i + 1
  264. }
  265. p = toNumber(s)
  266. if (p < 1) { return 1 }
  267. return p > pages ? pages : p
  268. }
  269. static pagesOf = (&count) => {
  270. let p = 1
  271. while (p * pageSize < count) { p = p + 1 }
  272. return p
  273. }
  274. // { page, pages, tiles } of one list
  275. static pageOf = (&list, &raw) => {
  276. pages = pagesOf(&list.length)
  277. let page = pageNumberOf(&raw, &pages)
  278. return { page = page pages = pages tiles = tilesOf(list.slice((page - 1) * pageSize, page * pageSize)) }
  279. }
  280. static moviesPage = (&raw) => { rollDay() return pageOf(&index.movies, &raw) }
  281. static seriesPage = (&raw) => { rollDay() return pageOf(&index.series, &raw) }
  282. static showsPage = (&raw) => { rollDay() return pageOf(&index.shows, &raw) }
  283. static catalogCounts = () => { return { movies = index.movies.length series = index.series.length shows = index.shows.length } }
  284. // /genres/<genre> (mission 058): one genre's movies and series, newest first, 24 per page; how many there are
  285. static genrePage = (&genreId, &raw) => {
  286. rollDay()
  287. list = index.genres[genreId]
  288. return pageOf(list != null ? list : [], &raw)
  289. }
  290. static genreCount = (&genreId) => {
  291. list = index.genres[genreId]
  292. return list != null ? list.length : 0
  293. }
  294. // /genres (tracker#32, the main menu's Genres): every genre that has public titles, A–Z, with how many — the genres table is 27 rows
  295. static genreList = () => {
  296. keyed = []
  297. for (g of genresTable.find(null, null)) {
  298. let n = genreCount(&g.id)
  299. if (n > 0 && g.name != null && g.urlSegment != null) { keyed.push({ key = ('' + g.name).toLowerCase() name = '' + g.name href = '/genres/' + g.urlSegment count = n }) }
  300. }
  301. desc = sortDesc(&keyed)
  302. out = []
  303. let i = desc.length - 1
  304. while (i >= 0) { out.push(desc[i]) i = i - 1 }
  305. return out
  306. }

Branches

Latest commits

  • ad529ac1log: m572 verdictmre
  • 1b37d404RV39 on hold: hybriel#308mre
  • f9127c68Revert "Merge branch 'rv39'"mre
  • cd39f3d3log: hybriel#308mre
  • 51341d6elog: RV39 deploy startedmre
  • c9e29e06Merge branch 'rv39'mre
  • 0f8d1fdclog: RV39 report, browser 387/2mre
  • 50249b0bRV39: reportmre
  • 10ed9581compose: HL_GC_STATS=1mre
  • 955da19bRV39: re-vendor Hybriel set day39 (source 51e921d7, bin 6b481aa7)mre
  • 64b8d1dclog: RV39 startedmre
  • 1f2df72amission RV39mre
  • 6c1936f9log+status: GC planmre
  • 2846a840log: m569 result, GC hypothesismre
  • b24cd2d5log: Anton on hl:fetch, mission 569mre
  • 7d42cac1log+status: SL1 measured, fetch stalls remainmre
  • 8e5f2ceflog: SL1 deployedmre
  • 29f34212log: SL1 deploy startedmre
  • 529dd741Merge sl1-slices: slice details repair and person fill (SL1)mre
  • 8a83316blog: SL1 result, session drain slowermre