gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit529dd741529dd741Merge sl1-slices: slice details repair and person fill (SL1)mre529dd741/lib/shortids.hl

9.5 KB

  1. // lib/shortids.hl — SHORT IDS (tracker.worldapi.org#27, mission 060): every movie, series and person has its own 5-character id
  2. // (`shortId`, like `a9s9a`), shown below its poster/photo; `/<shortId>` answers 301 to its page. Creator: "i wanted to give all
  3. // our movies, shows, series and persons own short ids like a9s9a … those ids show below the poster."
  4. //
  5. // OLD IDS: the old tracker had them on 702 of 9,453 shows (Show.jsonl field `id`, base62 counter '100jh'…'1012y', all of
  6. // them [a-z0-9]{5}). `data/old-short-ids.json` (made once by tools/old-short-ids.hl) maps the old Mongo `_id` (= our
  7. // `oldId`, tools/migrate.hl) → that id; such a show gets it back. Persons never had one.
  8. // NEW IDS: 5 random characters [a-z0-9] (36^5 = 60 M) — never one that is taken or one of the old ids (all 702 are
  9. // reserved, even where the show is gone).
  10. // UNIQUE ACROSS SHOWS AND PERSONS: one in-memory map `owners` (short id → { kind, id }) over BOTH tables — an mpackdb index
  11. // is per table and cannot span two. Built at start by `ensureShortIds` (one scan of each table); every id handed
  12. // out goes through it. A record whose short id another record already owns (never seen; logged) gets a new one.
  13. // NEW RECORDS get one when they are created: `claimShortId(kind, id, oldId)` in search.hl importTitle and people.hl
  14. // addMinimalTitle (the only places the app creates shows) and people.hl castPersonId (mission 062: the only place
  15. // the app creates persons — the cast/crew of #26/#28: completion, credits job, daily sync + tools/sync-tmdb.hl,
  16. // search import; and a guest star opened for the first time, people.hl guestPersonId).
  17. // THE BACKFILL: the records without one (at first every row: 9,453 shows + 15,157 persons) are given theirs in the
  18. // background, a time-boxed batch per clock tick (jobs.hl `shortIdTick` → `shortIdStep`), so pages are served
  19. // between two batches; after a restart it goes on with what is still missing. Real data: STATUS.md mission 060.
  20. //
  21. // ONE PROCESS PER STORAGE (hybriel#21): the map lives in the app's process; the tables are shows.hl's.
  22. import { now } from 'hl:time'
  23. import { warm } from './warm.hl'
  24. import { env } from 'hl:proc'
  25. import { readFile, exists } from 'hl:fs'
  26. import { randomBytes } from 'hl:crypto'
  27. import { hexDigits } from './util.hl'
  28. import { showsTable, personsTable, titlePath, liveShowOf } from './shows.hl'
  29. static shortIdChars = 'abcdefghijklmnopqrstuvwxyz0123456789'
  30. // short id → its owner as ONE string: 's<id>' (a show) or 'p<id>' (a person) — mission T17, boot time: an object { kind, id } per
  31. // record was 3 live objects each (795 K records), the string is one; ownerKind / ownerId read it
  32. static owners = {}
  33. static ownerValue = (&kind, &id) => { return (kind == 'person' ? 'p' : 's') + id }
  34. static ownerKind = (&v) => { return v.slice(0, 1) == 'p' ? 'person' : 'show' }
  35. static ownerId = (&v) => { return v.slice(1) }
  36. // the old tracker's ids: oldId → short id, and every one of them → true (reserved)
  37. static oldShortIds = {}
  38. static reserved = {}
  39. // the records still without a short id, [{ kind, id }], worked off from `state.queuePos`. Filled in place (push) and never
  40. // reassigned (a static cannot be) — kept OUT of `state`: reading `state.queue[i]` copied the whole 25k list on every access
  41. // (measured: ~3 ms per record instead of ~0.2)
  42. static queue = []
  43. static state = { built = false queuePos = 0 oldLoaded = 0 scanned = 0 existing = 0 duplicates = 0 ms = 0 assigned = 0 oldKept = 0 }
  44. // the file of old ids: TRACKER_OLD_SHORT_IDS, else data/old-short-ids.json beside project.hl (relative paths resolve against the
  45. // ENTRY script: a tool in tools/ — sync-tmdb.hl creates people since #28, mission 062 — finds it one folder up)
  46. static oldFile = () => {
  47. f = env('TRACKER_OLD_SHORT_IDS')
  48. if (f != null && f.trim() != '') { return f.trim() }
  49. return !exists('./data/old-short-ids.json') && exists('../data/old-short-ids.json') ? '../data/old-short-ids.json' : './data/old-short-ids.json'
  50. }
  51. // is this a short id at all (5 characters, a–z, 0–9)?
  52. static isShortId = (&s) => {
  53. if (s == null || hlTypeName(s) != 'String' || s.length != 5) { return false }
  54. let i = 0
  55. while (i < 5) {
  56. if (!shortIdChars.includes(s[i])) { return false }
  57. i = i + 1
  58. }
  59. return true
  60. }
  61. // 5 random characters a–z0–9, not taken and not an old one. Bytes ≥ 252 are skipped (252 = 7 × 36), so every character
  62. // is equally likely.
  63. static randomShortId = () => {
  64. while (true) {
  65. let hex = randomBytes(16, 'hex')
  66. let out = ''
  67. let i = 0
  68. while (i < hex.length && out.length < 5) {
  69. let b = hexDigits.indexOf(hex[i]) * 16 + hexDigits.indexOf(hex[i + 1])
  70. if (b < 252) { out = out + shortIdChars[b % 36] }
  71. i = i + 2
  72. }
  73. if (out.length == 5 && owners[out] == null && reserved[out] != true) { return out }
  74. }
  75. }
  76. // the short id for a record that has none: its old one when the old tracker had one (and nobody holds it), else a new
  77. // one — taken in `owners` at once
  78. static claimShortId = (kind, id, &oldId) => {
  79. ensureShortIds()
  80. let sid = oldId != null ? oldShortIds['' + oldId] : null
  81. if (sid != null && owners[sid] == null) { state.oldKept = state.oldKept + 1 } else { sid = randomShortId() }
  82. owners[sid] = ownerValue(&kind, &id)
  83. return sid
  84. }
  85. // tracker#31 (merge.hl picks the keeper of duplicates): is this record's short id the one the old tracker gave it?
  86. static hasOldShortId = (&r) => {
  87. ensureShortIds()
  88. return r.oldId != null && r.shortId != null && oldShortIds['' + r.oldId] == r.shortId
  89. }
  90. // does this record own its short id? (null / malformed / held by another record → no)
  91. static ownsShortId = (&kind, &r) => {
  92. if (!isShortId(&r.shortId)) { return false }
  93. o = owners[r.shortId]
  94. return o != null && o == ownerValue(&kind, &r.id)
  95. }
  96. // one record into `owners`; a record without a (valid, own) short id into the queue
  97. static noteShortIdRow = (kind, &r) => {
  98. state.scanned = state.scanned + 1
  99. if (isShortId(&r.shortId) && owners[r.shortId] == null) {
  100. owners[r.shortId] = ownerValue(&kind, &r.id)
  101. state.existing = state.existing + 1
  102. } else {
  103. if (r.shortId != null) {
  104. state.duplicates = state.duplicates + 1
  105. console.log('short ids: ' + kind + ' ' + r.id + ' has "' + r.shortId + '", ' + (isShortId(&r.shortId) ? 'taken by ' + ownerKind(&owners[r.shortId]) + ' ' + ownerId(&owners[r.shortId]) : 'not a short id') + ' — gets a new one')
  106. }
  107. queue.push({ kind = kind id = r.id })
  108. }
  109. return true
  110. }
  111. // one table's rows into `owners` (streamed, mission 043)
  112. static scanTable = (kind, &table) => {
  113. for (r of table.iterate(null, null)) { noteShortIdRow(kind, &r) }
  114. return true
  115. }
  116. // the old tracker's ids (data/old-short-ids.json) into oldShortIds + reserved
  117. static loadOldShortIds = () => {
  118. f = oldFile()
  119. if (exists(f)) {
  120. m = JSON.parse(readFile(f))
  121. let n = 0
  122. for (k of m.keys()) {
  123. let sid = m[k]
  124. if (isShortId(&sid)) { oldShortIds[k] = sid reserved[sid] = true n = n + 1 }
  125. }
  126. state.oldLoaded = n
  127. } else { console.log('short ids: no old ids (' + f + ' missing)') }
  128. return true
  129. }
  130. // the scan is done (bootscan.hl streams the tables itself): the log line
  131. static finishShortIds = (&t0) => {
  132. state.built = true
  133. state.ms = now() - t0
  134. console.log('short ids: ' + state.scanned + ' records, ' + state.existing + ' with a short id, ' + queue.length + ' without, ' + state.oldLoaded + ' old ids, ' + state.ms + ' ms')
  135. return true
  136. }
  137. // at start (bootscan.hl does it inside its passes; this is the lazy fallback): the old ids, the map of every short id in use, the
  138. // queue of records without one
  139. static ensureShortIds = () => {
  140. if (state.built) { return true }
  141. // mission T17: the progressive start is filling `owners` (bootscan.hl); nothing hands out an id before it is done (warm.hl warmBlocked)
  142. if (warm.running) { return false }
  143. state.built = true
  144. t0 = now()
  145. loadOldShortIds()
  146. scanTable('show', &showsTable)
  147. scanTable('person', &personsTable)
  148. finishShortIds(&t0)
  149. return true
  150. }
  151. static tableOf = (&kind) => { return kind == 'person' ? personsTable : showsTable }
  152. // the backfill, one batch: records from the queue until `budgetMs` is used up → { done, assigned, left }
  153. static shortIdStep = (&budgetMs) => {
  154. ensureShortIds()
  155. t0 = now()
  156. let n = 0
  157. while (state.queuePos < queue.length && now() - t0 < budgetMs) {
  158. let item = queue[state.queuePos]
  159. state.queuePos = state.queuePos + 1
  160. let table = tableOf(&item.kind)
  161. let r = table.fetch(item.id)
  162. if (r != null && !ownsShortId(&item.kind, &r)) {
  163. let x = r
  164. x.shortId = claimShortId(item.kind, x.id, &x.oldId)
  165. if (table.update(x.id, x) == null) { console.log('short ids: ' + item.kind + ' ' + x.id + ' not stored: ' + table.lastError()) } else { n = n + 1 }
  166. }
  167. }
  168. state.assigned = state.assigned + n
  169. done = state.queuePos >= queue.length
  170. return { done = done assigned = n left = queue.length - state.queuePos }
  171. }
  172. static shortIdStats = () => { return state }
  173. static persistShortIds = () => {
  174. showsTable.persist()
  175. personsTable.persist()
  176. return true
  177. }
  178. static shortIdsLeft = () => { return queue.length - state.queuePos }
  179. // `/<shortId>` (project.hl): the page's path, or null
  180. static shortIdPath = (&sid) => {
  181. if (!isShortId(&sid)) { return null }
  182. ensureShortIds()
  183. o = owners[sid]
  184. if (o == null) { return null }
  185. // tracker#31: a merged tombstone's short id leads to its keeper
  186. oKind = ownerKind(&o)
  187. oId = ownerId(&o)
  188. r = oKind == 'person' ? personsTable.fetch(oId) : liveShowOf(showsTable.fetch(oId))
  189. if (r == null || r.urlSegment == null) { return null }
  190. // a title's own page (/movies/, /series/, /shows/<slug>, tracker.worldapi.org#21)
  191. return oKind == 'person' ? '/person/' + r.urlSegment : titlePath(&r)
  192. }

Branches

Latest commits

  • 529dd741Merge sl1-slices: slice details repair and person fill (SL1)mre
  • 8a83316blog: SL1 result, session drain slowermre
  • 35e93e8dSL1: slice the details repair and person fill: catalog lists changed in place, 40 ms fill steps, people created in steps, writes one table per tickmre
  • 72c9119flog+status: SL1 startedmre
  • d74620fcmission SL1: slice repair and person fillmre
  • 5d542d73log+status: ST1 resultmre
  • 3945eb81ST1: stall diagnosis reportmre
  • 404733aclog: ST1 startedmre
  • 3e3d5a55mission ST1: stall diagnosismre
  • 9c3180eblog+status: RV28d measured after deploymre
  • fdf10b1alog: RV28d deployed on day38bmre
  • 320005e9log: RV28d merged, deploy runningmre
  • a8fef27dRV28d: reportmre
  • 2bae8373RV28d: re-vendor Hybriel set day38b (source 1e554d75, bin 5ff2a46d)mre
  • 16b3354aMerge branch 'main' into rv28cmre
  • 6f4f27d8log: RV28d started (day38b)mre
  • e6cf03ffmission RV28d: rv28c onto day38bmre
  • 3ccaee19status: live sweep freeze, waiting for Apollomre
  • 49f76ecblog: watch toggle slowness = session sweep freeze (tracker#50)mre
  • 3daf6eddlog: day37 live, still waiting for day39mre