gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc41587d2c41587d2Merge branch 'bs2' into mainTrackerc41587d2/lib/catalog.hl

18.6 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 as UTF-8 ID/key bytes with numeric boundaries. A request decodes 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 CatalogSort from './catalog-sort.hl'
  30. import { now } from 'hl:time'
  31. import CatalogList from './catalog-list.hl'
  32. import CatalogMap from './catalog-map.hl'
  33. import { sortEntries, appendEntry, entryAt } from './catalog-entries.hl'
  34. import { todayStr, dateOr, sortDesc } from './util.hl'
  35. import { warm } from './warm.hl'
  36. import { genresTable, showById, seasonsOfShow, episodesOfSeason, showYear, posterUrlOf, isPublicTitle, titleKind, titlePath, cardOf, posterUrlOfName, refreshTitleCaches, genreBySlug } from './shows.hl'
  37. static pageSize = 24
  38. static seriesKey = (&latest, &release) => { return latest != null && latest != '' ? '1' + latest : '0' + release }
  39. // a series in a genre list: the plain date (comparable with a movie's release) — its newest released episode, else its release
  40. static genreKey = (&latest, &release) => { return latest != null && latest != '' ? latest : release }
  41. // ---- the lists' entries (UTF-8 ID/key bytes and numeric boundaries, sorted by catalog-entries.hl) -----------------------------------------------------------
  42. // 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
  43. // WHOLE list per title (`placed` / `without` answered a new list, copied once more on the assignment): 120 ms per movie list, 190 ms for
  44. // 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
  45. // 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
  46. // binary search finds (the lists are sorted by key, largest first). The place is the one the linear form gave: before the first entry
  47. // with a smaller key, so after the entries with an equal key (ties keep their order).
  48. static indexOfId = (&list, &id) => {
  49. let i = 0
  50. while (i < list.length) {
  51. if (entryAt(&list, i) == id) { return i }
  52. i = i + 2
  53. }
  54. return -1
  55. }
  56. // one entry out of the list (a title that is not public, mission 054; or before it is put in again)
  57. static takeOut = (&list, &id) => {
  58. at = indexOfId(&list, &id)
  59. if (at >= 0) { list.remove(at) }
  60. return null
  61. }
  62. // 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
  63. // 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
  64. // 30 ms for 150 K entries in the real process, the rest of the work a few ms)
  65. static placeIn = (&list, &id, &key, absent) => {
  66. if (absent != true) { takeOut(&list, &id) }
  67. let lo = 0
  68. let hi = list.length / 2
  69. while (lo < hi) {
  70. mid = (lo + hi - (lo + hi) % 2) / 2
  71. if (entryAt(&list, mid * 2 + 1) >= key) { lo = mid + 1 } else { hi = mid }
  72. }
  73. list.insert(lo * 2, id, key)
  74. return null
  75. }
  76. // ---- the index -------------------------------------------------------------------------------------------------
  77. // mission 043: filled by ONE streamed pass per table (bootscan.hl: every show → noteIndexShow, every episode → noteIndexEpisode, then
  78. // finishIndex) — never a whole table in memory. `index` is filled in place (a static cannot be reassigned); `pending` holds what the
  79. // show pass collects for the finish.
  80. static index = { day = '' movies = new CatalogList() series = new CatalogList() shows = new CatalogList() latest = new CatalogMap() upcoming = new CatalogMap() release = new CatalogMap() genres = {} genresOf = new CatalogMap() }
  81. // Boot-only maps accept streamed episode appends; finishIndex packs and releases them.
  82. static pending = { seriesIds = [] showIds = [] genreTitles = [] latest = {} upcoming = {} release = {} }
  83. static beginIndex = () => {
  84. index.day = todayStr()
  85. return null
  86. }
  87. static noteIndexShow = (&s) => {
  88. if (s.type == 'movie') { if (isPublicTitle(&s)) { appendEntry(&index.movies, &s.id, dateOr(&s.release)) } }
  89. else if (s.type == 'series') {
  90. if (isPublicTitle(&s)) { if (titleKind(&s) == 'show') { pending.showIds.push(s.id) } else { pending.seriesIds.push(s.id) } }
  91. pending.release[s.id] = dateOr(&s.release)
  92. }
  93. if ((s.type == 'movie' || s.type == 'series') && isPublicTitle(&s) && s.genres != null && s.genres.length > 0) {
  94. pending.genreTitles.push({ id = s.id movie = s.type == 'movie' release = dateOr(&s.release) genres = s.genres })
  95. }
  96. return null
  97. }
  98. static noteIndexEpisode = (&e) => {
  99. // tracker#31: a merged episode is its keeper episode's twin (same date) — not counted twice
  100. if (e.show != null && e.mergedInto == null && dateOr(&e.release) != '') {
  101. if (e.release <= index.day) {
  102. l = pending.latest[e.show]
  103. if (l == null || e.release > l) { pending.latest[e.show] = e.release }
  104. } else {
  105. // mission 037: pushed IN PLACE — `let u = upcoming[x]; u.push(…); upcoming[x] = u` copies the list twice per episode
  106. // (plain data copies on assignment), quadratic in a list's length
  107. if (pending.upcoming[e.show] == null) { pending.upcoming[e.show] = [] }
  108. pending.upcoming[e.show].push(e.release)
  109. }
  110. }
  111. return null
  112. }
  113. // BS2: the closing pass is part of warm start too. Keep the catalog unavailable
  114. // until all maps, lists and stable sorts are complete, yielding on the same clock.
  115. static closing = { phase = 'latest' keys = null pos = 0 genres = {} sorter = null }
  116. static finishIndexStep = (Number budgetMs) => {
  117. deadline = now() + budgetMs
  118. let n = 0
  119. while (closing.phase != 'done') {
  120. phase = closing.phase
  121. if (phase == 'latest' || phase == 'release' || phase == 'upcoming') {
  122. if (closing.keys == null) { closing.keys = pending[phase].keys() }
  123. if (closing.pos < closing.keys.length) {
  124. id = closing.keys[closing.pos]
  125. index[phase].put(id, &pending[phase][id])
  126. closing.pos = closing.pos + 1
  127. } else {
  128. pending[phase] = {}
  129. closing.keys = null
  130. closing.pos = 0
  131. closing.phase = phase == 'latest' ? 'release' : (phase == 'release' ? 'upcoming' : 'series')
  132. }
  133. } else if (phase == 'series' || phase == 'shows') {
  134. ids = phase == 'series' ? 'seriesIds' : 'showIds'
  135. if (closing.pos < pending[ids].length) {
  136. id = pending[ids][closing.pos]
  137. appendEntry(&index[phase], &id, seriesKey(index.latest.fetch(id), index.release.fetch(id)))
  138. closing.pos = closing.pos + 1
  139. } else {
  140. pending[ids] = []
  141. closing.pos = 0
  142. closing.phase = phase == 'series' ? 'shows' : 'genres'
  143. }
  144. } else if (phase == 'genres') {
  145. if (closing.pos < pending.genreTitles.length) {
  146. t = pending.genreTitles[closing.pos]
  147. key = t.movie ? t.release : genreKey(index.latest.fetch(t.id), &t.release)
  148. for (g of t.genres) {
  149. if (closing.genres[g] == null) { closing.genres[g] = new CatalogList() }
  150. appendEntry(&closing.genres[g], &t.id, &key)
  151. }
  152. index.genresOf.put(t.id, t.genres)
  153. closing.pos = closing.pos + 1
  154. } else {
  155. pending.genreTitles = []
  156. closing.keys = closing.genres.keys()
  157. closing.pos = 0
  158. closing.phase = 'sortGenres'
  159. }
  160. } else if (phase == 'sortGenres') {
  161. if (closing.pos >= closing.keys.length) {
  162. closing.genres = {}
  163. closing.keys = null
  164. closing.phase = 'sortMovies'
  165. } else {
  166. g = closing.keys[closing.pos]
  167. if (closing.sorter == null) { closing.sorter = new CatalogSort(source = closing.genres[g]) }
  168. if (!closing.sorter.step(deadline)) { return false }
  169. index.genres[g] = closing.sorter.out
  170. closing.sorter = null
  171. closing.pos = closing.pos + 1
  172. }
  173. } else {
  174. list = phase == 'sortMovies' ? 'movies' : (phase == 'sortSeries' ? 'series' : 'shows')
  175. if (closing.sorter == null) { closing.sorter = new CatalogSort(source = index[list]) }
  176. if (!closing.sorter.step(deadline)) { return false }
  177. index[list] = closing.sorter.out
  178. closing.sorter = null
  179. closing.phase = phase == 'sortMovies' ? 'sortSeries' : (phase == 'sortSeries' ? 'sortShows' : 'done')
  180. }
  181. n = n + 1
  182. if (n % 32 == 0 && now() >= deadline) { return closing.phase == 'done' }
  183. }
  184. return true
  185. }
  186. // Synchronous callers (tools) retain the same interface and result.
  187. static finishIndex = () => {
  188. while (!finishIndexStep(2000000000)) { }
  189. return null
  190. }
  191. // a TV list with every key from the current release maps, sorted again
  192. static rekeyed = (&list) => {
  193. out = new CatalogList()
  194. for (let i = 0; i < list.length; i = i + 2) {
  195. appendEntry(&out, entryAt(&list, i), seriesKey(index.latest.fetch(entryAt(&list, i)), index.release.fetch(entryAt(&list, i))))
  196. }
  197. return sortEntries(&out)
  198. }
  199. // a new day: upcoming episodes that are out now become their series' newest released one
  200. static rollDay = () => {
  201. // mission T17: not while the start-up scan is still reading the episodes (it classifies them by `index.day`)
  202. if (warm.running && !warm.catalog) { return null }
  203. today = todayStr()
  204. if (index.day == today) { return null }
  205. let moved = false
  206. for (id of index.upcoming.keys()) {
  207. rest = []
  208. let l = index.latest.fetch(id)
  209. for (d of index.upcoming.fetch(id)) {
  210. if (d <= today) {
  211. if (l == null || d > l) { l = d }
  212. moved = true
  213. } else { rest.push(d) }
  214. }
  215. index.upcoming.put(id, rest)
  216. if (l != null) { index.latest.put(id, l) }
  217. }
  218. if (moved) {
  219. index.series = rekeyed(&index.series)
  220. index.shows = rekeyed(&index.shows)
  221. // the genre lists too: a series' key is its newest released episode (movies keep theirs)
  222. for (g of index.genres.keys()) {
  223. list = new CatalogList()
  224. for (let i = 0; i < index.genres[g].length; i = i + 2) {
  225. id = entryAt(&index.genres[g], i)
  226. appendEntry(&list, &id, index.release.fetch(id) != null ? genreKey(index.latest.fetch(id), index.release.fetch(id)) : entryAt(&index.genres[g], i + 1))
  227. }
  228. index.genres[g] = sortEntries(&list)
  229. }
  230. }
  231. index.day = today
  232. return null
  233. }
  234. // one title out of the lists of the genres it was in, then (when public) into those of its current genres
  235. static refreshGenres = (&show, &shown, key) => {
  236. old = index.genresOf.fetch(show.id)
  237. if (old != null) {
  238. for (g of old) { if (index.genres[g] != null) { takeOut(&index.genres[g], &show.id) } }
  239. }
  240. index.genresOf.put(show.id, null)
  241. if (!shown || show.genres == null || show.genres.length == 0) { return null }
  242. for (g of show.genres) {
  243. if (index.genres[g] == null) { index.genres[g] = new CatalogList() }
  244. placeIn(&index.genres[g], &show.id, &key, true)
  245. }
  246. index.genresOf.put(show.id, show.genres)
  247. return null
  248. }
  249. // after the sync (or the adult backfill) touched a show: its entry from its own data again (only that show's
  250. // seasons/episodes are read) — or out of the list when it is not public (adult or unknown, mission 054)
  251. // `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
  252. static refreshCatalogShow = (&showId, isNew) => {
  253. rollDay()
  254. show = showById(&showId)
  255. if (show == null) { return null }
  256. // tracker#30: the lists' slim cache of this title (shows.hl cardOf / episodeRowsOf) from the changed record
  257. refreshTitleCaches(&show)
  258. shown = isPublicTitle(&show)
  259. if (show.type == 'movie') {
  260. if (shown) { placeIn(&index.movies, &show.id, dateOr(&show.release), isNew == true) } else if (isNew != true) { takeOut(&index.movies, &show.id) }
  261. refreshGenres(&show, &shown, dateOr(&show.release))
  262. return null
  263. }
  264. if (show.type != 'series') { return null }
  265. today = index.day
  266. let l = null
  267. up = []
  268. for (s of seasonsOfShow(&show)) {
  269. for (e of episodesOfSeason(&s)) {
  270. if (dateOr(&e.release) != '') {
  271. if (e.release <= today) { if (l == null || e.release > l) { l = e.release } } else { up.push(e.release) }
  272. }
  273. }
  274. }
  275. index.latest.put(show.id, l)
  276. index.upcoming.put(show.id, up)
  277. index.release.put(show.id, dateOr(&show.release))
  278. // in the list of its kind (#21), out of the other one
  279. key = seriesKey(&l, index.release.fetch(show.id))
  280. isShow = titleKind(&show) == 'show'
  281. if (shown && !isShow) { placeIn(&index.series, &show.id, &key, isNew == true) } else if (isNew != true) { takeOut(&index.series, &show.id) }
  282. if (shown && isShow) { placeIn(&index.shows, &show.id, &key, isNew == true) } else if (isNew != true) { takeOut(&index.shows, &show.id) }
  283. refreshGenres(&show, &shown, genreKey(&l, index.release.fetch(show.id)))
  284. return null
  285. }
  286. // ---- what the pages read ---------------------------------------------------------------------------------------
  287. // one poster tile: poster, title, year, the title's page (/movies/, /series/ or /shows/<slug>, shows.hl titlePath — one
  288. // component for all three, with its Follow button). tracker#30 (mission 025): from the slim cached card (shows.hl cardOf),
  289. // not the whole record with its cast and crew
  290. static tilesOf = (&entries) => {
  291. out = []
  292. for (let i = 0; i < entries.length; i = i + 2) {
  293. c = cardOf(entryAt(&entries, i))
  294. if (c != null) { out.push({ id = c.id title = c.title year = c.year href = c.href posterUrl = posterUrlOfName(&c.posterName) }) }
  295. }
  296. return out
  297. }
  298. // the homepage: the n latest RELEASED movies (none dated after today) and the n series with the newest released episode
  299. static latestMovies = (&n) => {
  300. rollDay()
  301. want = new CatalogList()
  302. for (let i = 0; i < index.movies.length; i = i + 2) {
  303. if (want.length < n * 2 && entryAt(&index.movies, i + 1) != '' && entryAt(&index.movies, i + 1) <= index.day) { appendEntry(&want, entryAt(&index.movies, i), entryAt(&index.movies, i + 1)) }
  304. if (want.length >= n * 2) { return tilesOf(&want) }
  305. }
  306. return tilesOf(&want)
  307. }
  308. static latestSeries = (&n) => { rollDay() return latestTv(&index.series, &n) }
  309. static latestShows = (&n) => { rollDay() return latestTv(&index.shows, &n) }
  310. static latestTv = (&list, &n) => {
  311. want = new CatalogList()
  312. for (let i = 0; i < list.length; i = i + 2) {
  313. if (want.length < n * 2 && entryAt(&list, i + 1).slice(0, 1) == '1') { appendEntry(&want, entryAt(&list, i), entryAt(&list, i + 1)) }
  314. if (want.length >= n * 2) { return tilesOf(&want) }
  315. }
  316. return tilesOf(&want)
  317. }
  318. // a page number from the address (`/movies/page/:page`, a String) — anything that is not a whole number ≥ 1 is page 1;
  319. // past the last page is the last page
  320. static pageNumberOf = (&raw, &pages) => {
  321. if (raw == null) { return 1 }
  322. s = '' + raw
  323. if (s == '' || s.length > 6) { return 1 }
  324. let i = 0
  325. while (i < s.length) {
  326. c = s.charCodeAt(i)
  327. if (c < 48 || c > 57) { return 1 }
  328. i = i + 1
  329. }
  330. p = toNumber(s)
  331. if (p < 1) { return 1 }
  332. return p > pages ? pages : p
  333. }
  334. static pagesOf = (&count) => {
  335. let p = 1
  336. while (p * pageSize < count) { p = p + 1 }
  337. return p
  338. }
  339. // { page, pages, tiles } of one list
  340. static pageOf = (&list, &raw) => {
  341. pages = pagesOf(list.length / 2)
  342. page = pageNumberOf(&raw, &pages)
  343. selected = new CatalogList()
  344. for (let i = (page - 1) * pageSize * 2; i < page * pageSize * 2 && i < list.length; i = i + 2) {
  345. appendEntry(&selected, entryAt(&list, i), entryAt(&list, i + 1))
  346. }
  347. return { page = page pages = pages tiles = tilesOf(&selected) }
  348. }
  349. static moviesPage = (&raw) => { rollDay() return pageOf(&index.movies, &raw) }
  350. static seriesPage = (&raw) => { rollDay() return pageOf(&index.series, &raw) }
  351. static showsPage = (&raw) => { rollDay() return pageOf(&index.shows, &raw) }
  352. static catalogCounts = () => { return { movies = index.movies.length / 2 series = index.series.length / 2 shows = index.shows.length / 2 } }
  353. // /genres/<genre> (mission 058): one genre's movies and series, newest first, 24 per page; how many there are
  354. static genrePage = (&genreId, &raw) => {
  355. rollDay()
  356. list = index.genres[genreId]
  357. return pageOf(list != null ? list : new CatalogList(), &raw)
  358. }
  359. static genreCount = (&genreId) => {
  360. list = index.genres[genreId]
  361. return list != null ? list.length / 2 : 0
  362. }
  363. // /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
  364. static genreList = () => {
  365. keyed = []
  366. for (g of genresTable.find(null, null)) {
  367. n = genreCount(&g.id)
  368. if (n > 0 && g.name != null && g.urlSegment != null) { keyed.push({ key = ('' + g.name).toLowerCase() name = '' + g.name href = '/genres/' + g.urlSegment count = n }) }
  369. }
  370. desc = sortDesc(&keyed)
  371. out = []
  372. let i = desc.length - 1
  373. while (i >= 0) { out.push(desc[i]) i = i - 1 }
  374. return out
  375. }
  376. // tracker#59: genre page rows.
  377. static genreOf = (&slug) => { return genreBySlug(&slug) }
  378. static listingOf = (&g, &raw) => { return g != null ? genrePage(&g.id, &raw) : { page = 1 pages = 1 tiles = [] } }
  379. static countOf = (&g) => { return g != null ? genreCount(&g.id) : 0 }

Branches

Latest commits

  • c41587d2Merge branch 'bs2' into mainTracker
  • 0dd538a6|||PR BS2: finish the boot index in slices so a booting instance keeps answeringTracker
  • 019d5cedMerge branch 'records-1110' into mainTracker
  • f88ae8b9|||PR records: day56 toggle live, reviewTracker
  • b6b3bb4dMerge branch 'ap56e' into mainTracker
  • 29a26da9|||PR AP56e: guard hidden show helpers during session syncTracker
  • 7c9ec236|||PR AP56e: re-vendor Hybriel set day56 (source 36ae83a8, bin c1f595a2)Tracker
  • 4b54f6f9Merge branch 'records-audit' into mainTracker
  • cab4a3bc|||PR records: branch audit, PR-onlyTracker
  • 641e1c4cMerge branch 'records-dp71' into mainTracker
  • 85a6e9f5|||PR records: DP71 checked in ChromeTracker
  • f5be5f18records: gitoria PR-only planTracker
  • 46a37036records: DP71 liveTracker
  • 3bcb7039Status/log: DP71 in progressTracker
  • 5b3a5693Log: SR1b merged via PR 2, DP71 startedTracker
  • 783957c2Merge branch 'sr1-search' into mainTracker
  • 792b9652README: search results come from the routeTracker
  • fd8570b1|||PR search: one server exchange per keystrokeTracker
  • e8f5cbcaLog: SR1 result, SR1bTracker
  • 8ad275a4Log/status: tracker 68, SR1Tracker