gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit9c3971d09c3971d0tests/persons-skip-count.hlmre9c3971d0/lib/shortids.hl

8.9 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 { env } from 'hl:proc'
  24. import { readFile, exists } from 'hl:fs'
  25. import { randomBytes } from 'hl:crypto'
  26. import { hexDigits } from './util.hl'
  27. import { showsTable, personsTable, titlePath, liveShowOf } from './shows.hl'
  28. static shortIdChars = 'abcdefghijklmnopqrstuvwxyz0123456789'
  29. // short id → { kind ('show' | 'person'), id }
  30. static owners = {}
  31. // the old tracker's ids: oldId → short id, and every one of them → true (reserved)
  32. static oldShortIds = {}
  33. static reserved = {}
  34. // the records still without a short id, [{ kind, id }], worked off from `state.queuePos`. Filled in place (push) and never
  35. // reassigned (a static cannot be) — kept OUT of `state`: reading `state.queue[i]` copied the whole 25k list on every access
  36. // (measured: ~3 ms per record instead of ~0.2)
  37. static queue = []
  38. static state = { built = false queuePos = 0 oldLoaded = 0 scanned = 0 existing = 0 duplicates = 0 ms = 0 assigned = 0 oldKept = 0 }
  39. // the file of old ids: TRACKER_OLD_SHORT_IDS, else data/old-short-ids.json beside project.hl (relative paths resolve against the
  40. // ENTRY script: a tool in tools/ — sync-tmdb.hl creates people since #28, mission 062 — finds it one folder up)
  41. static oldFile = () => {
  42. f = env('TRACKER_OLD_SHORT_IDS')
  43. if (f != null && f.trim() != '') { return f.trim() }
  44. return !exists('./data/old-short-ids.json') && exists('../data/old-short-ids.json') ? '../data/old-short-ids.json' : './data/old-short-ids.json'
  45. }
  46. // is this a short id at all (5 characters, a–z, 0–9)?
  47. static isShortId = (&s) => {
  48. if (s == null || hlTypeName(s) != 'String' || s.length != 5) { return false }
  49. let i = 0
  50. while (i < 5) {
  51. if (!shortIdChars.includes(s[i])) { return false }
  52. i = i + 1
  53. }
  54. return true
  55. }
  56. // 5 random characters a–z0–9, not taken and not an old one. Bytes ≥ 252 are skipped (252 = 7 × 36), so every character
  57. // is equally likely.
  58. static randomShortId = () => {
  59. while (true) {
  60. let hex = randomBytes(16, 'hex')
  61. let out = ''
  62. let i = 0
  63. while (i < hex.length && out.length < 5) {
  64. let b = hexDigits.indexOf(hex[i]) * 16 + hexDigits.indexOf(hex[i + 1])
  65. if (b < 252) { out = out + shortIdChars[b % 36] }
  66. i = i + 2
  67. }
  68. if (out.length == 5 && owners[out] == null && reserved[out] != true) { return out }
  69. }
  70. }
  71. // 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
  72. // one — taken in `owners` at once
  73. static claimShortId = (kind, id, &oldId) => {
  74. ensureShortIds()
  75. let sid = oldId != null ? oldShortIds['' + oldId] : null
  76. if (sid != null && owners[sid] == null) { state.oldKept = state.oldKept + 1 } else { sid = randomShortId() }
  77. owners[sid] = { kind = kind id = id }
  78. return sid
  79. }
  80. // tracker#31 (merge.hl picks the keeper of duplicates): is this record's short id the one the old tracker gave it?
  81. static hasOldShortId = (&r) => {
  82. ensureShortIds()
  83. return r.oldId != null && r.shortId != null && oldShortIds['' + r.oldId] == r.shortId
  84. }
  85. // does this record own its short id? (null / malformed / held by another record → no)
  86. static ownsShortId = (&kind, &r) => {
  87. if (!isShortId(r.shortId)) { return false }
  88. o = owners[r.shortId]
  89. return o != null && o.kind == kind && o.id == r.id
  90. }
  91. // one record into `owners`; a record without a (valid, own) short id into the queue
  92. static noteShortIdRow = (kind, &r) => {
  93. state.scanned = state.scanned + 1
  94. if (isShortId(r.shortId) && owners[r.shortId] == null) {
  95. owners[r.shortId] = { kind = kind id = r.id }
  96. state.existing = state.existing + 1
  97. } else {
  98. if (r.shortId != null) {
  99. state.duplicates = state.duplicates + 1
  100. console.log('short ids: ' + kind + ' ' + r.id + ' has "' + r.shortId + '", ' + (isShortId(r.shortId) ? 'taken by ' + owners[r.shortId].kind + ' ' + owners[r.shortId].id : 'not a short id') + ' — gets a new one')
  101. }
  102. queue.push({ kind = kind id = r.id })
  103. }
  104. return true
  105. }
  106. // one table's rows into `owners` (streamed, mission 043)
  107. static scanTable = (kind, &table) => {
  108. for (r of table.iterate(null, null)) { noteShortIdRow(kind, r) }
  109. return true
  110. }
  111. // the old tracker's ids (data/old-short-ids.json) into oldShortIds + reserved
  112. static loadOldShortIds = () => {
  113. f = oldFile()
  114. if (exists(f)) {
  115. m = JSON.parse(readFile(f))
  116. let n = 0
  117. for (k of m.keys()) {
  118. let sid = m[k]
  119. if (isShortId(sid)) { oldShortIds[k] = sid reserved[sid] = true n = n + 1 }
  120. }
  121. state.oldLoaded = n
  122. } else { console.log('short ids: no old ids (' + f + ' missing)') }
  123. return true
  124. }
  125. // the scan is done (bootscan.hl streams the tables itself): the log line
  126. static finishShortIds = (&t0) => {
  127. state.built = true
  128. state.ms = now() - t0
  129. console.log('short ids: ' + state.scanned + ' records, ' + state.existing + ' with a short id, ' + queue.length + ' without, ' + state.oldLoaded + ' old ids, ' + state.ms + ' ms')
  130. return true
  131. }
  132. // 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
  133. // queue of records without one
  134. static ensureShortIds = () => {
  135. if (state.built) { return true }
  136. state.built = true
  137. t0 = now()
  138. loadOldShortIds()
  139. scanTable('show', showsTable)
  140. scanTable('person', personsTable)
  141. finishShortIds(t0)
  142. return true
  143. }
  144. static tableOf = (&kind) => { return kind == 'person' ? personsTable : showsTable }
  145. // the backfill, one batch: records from the queue until `budgetMs` is used up → { done, assigned, left }
  146. static shortIdStep = (&budgetMs) => {
  147. ensureShortIds()
  148. t0 = now()
  149. let n = 0
  150. while (state.queuePos < queue.length && now() - t0 < budgetMs) {
  151. let item = queue[state.queuePos]
  152. state.queuePos = state.queuePos + 1
  153. let table = tableOf(item.kind)
  154. let r = table.fetch(item.id)
  155. if (r != null && !ownsShortId(item.kind, r)) {
  156. let x = r
  157. x.shortId = claimShortId(item.kind, x.id, x.oldId)
  158. if (table.update(x.id, x) == null) { console.log('short ids: ' + item.kind + ' ' + x.id + ' not stored: ' + table.lastError()) } else { n = n + 1 }
  159. }
  160. }
  161. state.assigned = state.assigned + n
  162. done = state.queuePos >= queue.length
  163. return { done = done assigned = n left = queue.length - state.queuePos }
  164. }
  165. static shortIdStats = () => { return state }
  166. static persistShortIds = () => {
  167. showsTable.persist()
  168. personsTable.persist()
  169. return true
  170. }
  171. static shortIdsLeft = () => { return queue.length - state.queuePos }
  172. // `/<shortId>` (project.hl): the page's path, or null
  173. static shortIdPath = (&sid) => {
  174. if (!isShortId(sid)) { return null }
  175. ensureShortIds()
  176. o = owners[sid]
  177. if (o == null) { return null }
  178. // tracker#31: a merged tombstone's short id leads to its keeper
  179. r = o.kind == 'person' ? personsTable.fetch(o.id) : liveShowOf(showsTable.fetch(o.id))
  180. if (r == null || r.urlSegment == null) { return null }
  181. // a title's own page (/movies/, /series/, /shows/<slug>, tracker.worldapi.org#21)
  182. return o.kind == 'person' ? '/person/' + r.urlSegment : titlePath(r)
  183. }

Branches

Latest commits

  • 9c3971d0tests/persons-skip-count.hlmre
  • 4d72e365mission 045mre
  • b9cae4c9slices: skip and count non-row values from range(); merge persons pass falls back to iterate; guard testmre
  • 3db7dc31merge w44-jobs-slices into w43-trkmemmre
  • 593dab6fmission 043: report, LOGmre
  • 1a5a4818boot scans back to iterate (slices measured slower: titles 114 s, episodes 141 s, persons failed); lambda per rowmre
  • 03d29893boot scans read primary-key slices (lib/slices.hl from mission 044): iterate is eager too; people view log line back; allShows/allEpisodes stay for toolsmre
  • 23375c58mission 044: report, LOG, STATUSmre
  • c6fd928emission 044: 4096 slices for tables over 200 K rows, 15 ms budget, TRACKER_SLICE_DEPTH for testsmre
  • fb057680mission 044: jobs.hl imports mergeLeft (gate found it), mission file, slices benchmre
  • 0d6293ebmission 043: mission filemre
  • d25bd14amission 044: sliced merge prepare + sliced collections prepare; jobs-dump old/new check + bulk fixturemre
  • 633ee210mission 044: sliced table scans (slices.hl, pk-range slices) for the adult/kind/status/art/credits/details-repair queuesmre
  • 55bbd619boot: one streamed pass per table (lib/bootscan.hl) instead of find(null,null) per module; follows/watches/franchises scans streammre
  • 1d465d3ere-vendor plugins to Hybriel master f9614007 (mpackdb #202 copy fix, libmpackdb sha 6fb3b9d6)mre
  • 6648ba44mission 042: real-browser E2E on ce6f2e0a re-vendor 69/0 at 390 + 1280mre
  • 3ed3a0efmission 042: gates old/new equal (browser #41 route-table regex accepts unescaped hl:web output), real-data TTFB, E2E blocked by lisbeth lockmre
  • d5b61e90LOG 042mre
  • 4acdbaccmission 042: memory comparison old vs ce6f2e0a (stopped: warm VmHWM higher), reportmre
  • d0f620d6mission 042: re-vendor Hybriel ce6f2e0a (plugins incl. mpackdb; bin runtime built from the same archive, git-ignored), compact=false keptmre