gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commited4cbab2ed4cbab2status timemreed4cbab2/plugins/mpackdb/tools/check-index-keys.mjs

5.7 KB

  1. #!/usr/bin/env node
  2. // Usage: node check-index-keys.mjs <directory>
  3. //
  4. // READ ONLY. For every <name>.mpack in the directory (not below it) this lists the live
  5. // records whose indexed text field holds a comma, a newline, a carriage return or starts with
  6. // a double quote, and says which of them are MISSING from that field's index file
  7. // <name>.<field>.txt (mission 486: the engine before the fix dropped such entries from the
  8. // index when it was reopened). The indexed fields are the <name>.<field>.txt files beside
  9. // the .mpack; nothing is written, locked or created. Exit status 1 when any entry is missing.
  10. //
  11. // A missing entry is repaired by opening the table with the fixed hl:mpackdb: an index whose
  12. // idxstate carries no "idxFmt":2 mark is rebuilt from the data once, the data itself holds
  13. // every record.
  14. import { readFileSync, readdirSync, existsSync } from 'node:fs'
  15. import { join } from 'node:path'
  16. const dir = process.argv[2]
  17. if (!dir) {
  18. console.error('usage: node check-index-keys.mjs <directory>')
  19. process.exit(2)
  20. }
  21. // --- a small MessagePack reader (the subset the engine writes: maps, arrays, strings, numbers)
  22. function decode(buf, start) {
  23. let p = start
  24. const val = () => {
  25. const b = buf[p++]
  26. if (b <= 0x7f) return b
  27. if (b >= 0xe0) return b - 256
  28. if ((b & 0xe0) === 0xa0) return str(b & 0x1f)
  29. if ((b & 0xf0) === 0x80) return map(b & 0x0f)
  30. if ((b & 0xf0) === 0x90) return arr(b & 0x0f)
  31. switch (b) {
  32. case 0xc0: return null
  33. case 0xc2: return false
  34. case 0xc3: return true
  35. case 0xc4: return bin(buf[p++])
  36. case 0xc5: { const n = buf.readUInt16BE(p); p += 2; return bin(n) }
  37. case 0xc6: { const n = buf.readUInt32BE(p); p += 4; return bin(n) }
  38. case 0xca: { const v = buf.readFloatBE(p); p += 4; return v }
  39. case 0xcb: { const v = buf.readDoubleBE(p); p += 8; return v }
  40. case 0xcc: return buf[p++]
  41. case 0xcd: { const v = buf.readUInt16BE(p); p += 2; return v }
  42. case 0xce: { const v = buf.readUInt32BE(p); p += 4; return v }
  43. case 0xcf: { const v = Number(buf.readBigUInt64BE(p)); p += 8; return v }
  44. case 0xd0: return buf.readInt8(p++)
  45. case 0xd1: { const v = buf.readInt16BE(p); p += 2; return v }
  46. case 0xd2: { const v = buf.readInt32BE(p); p += 4; return v }
  47. case 0xd3: { const v = Number(buf.readBigInt64BE(p)); p += 8; return v }
  48. case 0xd9: return str(buf[p++])
  49. case 0xda: { const n = buf.readUInt16BE(p); p += 2; return str(n) }
  50. case 0xdb: { const n = buf.readUInt32BE(p); p += 4; return str(n) }
  51. case 0xdc: { const n = buf.readUInt16BE(p); p += 2; return arr(n) }
  52. case 0xdd: { const n = buf.readUInt32BE(p); p += 4; return arr(n) }
  53. case 0xde: { const n = buf.readUInt16BE(p); p += 2; return map(n) }
  54. case 0xdf: { const n = buf.readUInt32BE(p); p += 4; return map(n) }
  55. default: throw new Error('unsupported msgpack byte 0x' + b.toString(16))
  56. }
  57. }
  58. const str = (n) => { const s = buf.toString('utf8', p, p + n); p += n; return s }
  59. const bin = (n) => { p += n; return null }
  60. const arr = (n) => { const a = []; for (let i = 0; i < n; i++) a.push(val()); return a }
  61. const map = (n) => { const o = {}; for (let i = 0; i < n; i++) { const k = val(); o[k] = val() } return o }
  62. return val()
  63. }
  64. // --- an index file: the offsets it names (a quoted key is a JSON string, else split from the right)
  65. function indexOffsets(text) {
  66. const offsets = new Set()
  67. for (const line of text.split('\n')) {
  68. if (!line) continue
  69. if (line[0] === '"') {
  70. let rest = null
  71. for (let i = 1; i < line.length; i++) {
  72. if (line[i] === '\\') { i++; continue }
  73. if (line[i] === '"') { rest = line.slice(i + 1); break }
  74. }
  75. if (rest !== null && /^,\d+,\d+$/.test(rest)) { offsets.add(Number(rest.split(',')[1])); continue }
  76. }
  77. const c2 = line.lastIndexOf(',')
  78. const c1 = c2 > 0 ? line.lastIndexOf(',', c2 - 1) : -1
  79. if (c1 < 0) continue
  80. const off = Number(line.slice(c1 + 1, c2))
  81. if (Number.isInteger(off)) offsets.add(off)
  82. }
  83. return offsets
  84. }
  85. const risky = (v) => typeof v === 'string' && (/[,\n\r]/.test(v) || v[0] === '"')
  86. let missingTotal = 0
  87. const all = readdirSync(dir)
  88. const names = all.filter((f) => f.endsWith('.mpack')).map((f) => f.slice(0, -'.mpack'.length)).sort()
  89. for (const name of names) {
  90. const data = readFileSync(join(dir, name + '.mpack'))
  91. let deleted = new Set()
  92. const metaPath = join(dir, name + '.meta.json')
  93. if (existsSync(metaPath)) {
  94. try { deleted = new Set(JSON.parse(readFileSync(metaPath, 'utf8')).deleted || []) } catch { /* no usable meta */ }
  95. }
  96. const fields = all
  97. .filter((f) => f.startsWith(name + '.') && f.endsWith('.txt'))
  98. .map((f) => f.slice(name.length + 1, -4))
  99. .filter((f) => f && !f.includes('.'))
  100. const indexes = new Map(fields.map((f) => [f, indexOffsets(readFileSync(join(dir, name + '.' + f + '.txt'), 'utf8'))]))
  101. let live = 0, riskyCount = 0, missing = 0
  102. for (let pos = 0; pos + 4 <= data.length;) {
  103. const size = data.readUInt32LE(pos)
  104. if (size < 5 || pos + size > data.length) { console.log(`${name}.mpack: record frame at ${pos} is damaged or cut short; stopped there`); break }
  105. if (!deleted.has(pos)) {
  106. live++
  107. let rec = null
  108. try { rec = decode(data, pos + 4) } catch (e) { console.log(`${name}.mpack: record at ${pos} cannot be read: ${e.message}`) }
  109. if (rec && typeof rec === 'object') {
  110. for (const [field, offsets] of indexes) {
  111. if (!risky(rec[field])) continue
  112. riskyCount++
  113. const lost = !offsets.has(pos)
  114. if (lost) { missing++; missingTotal++ }
  115. console.log(`${name}.${field}: record at offset ${pos} key ${JSON.stringify(rec[field])} ${lost ? 'MISSING from the index file' : 'present'}`)
  116. }
  117. }
  118. }
  119. pos += size
  120. }
  121. console.log(`${name}: ${live} live records, indexed fields [${fields.join(', ')}], ${riskyCount} with a comma/newline/CR/leading quote, ${missing} missing from their index`)
  122. }
  123. if (!names.length) console.log('no .mpack files in ' + dir)
  124. process.exit(missingTotal ? 1 : 0)

Branches

Latest commits

  • ed4cbab2status timemre
  • e982c26dlog: HM65 mergedmre
  • 6e72f9d1Merge hm65-fields: episode boot scan reads three fieldsmre
  • 07a5c883HM65 report measured field projection and complete validationmre
  • 0eab858fRead only catalog date fields during the episode boot scanmre
  • d3179273log: HM65 hl:heap vendoringmre
  • cb78a1a0HM65 report missing heap diagnostic package with repromre
  • 89606a5cHM65 briefmre
  • 096ce0d4log: DP69 live on day48mre
  • b3d8a80bDP69 brief, RV48 mergedmre
  • ce6d7d32Merge rv48: Hybriel day48mre
  • 3da2ea86RV48: report day48 cold starts, gates and cleanupmre
  • 8d9ecdfeRV48: re-vendor Hybriel set day48 (source b6a8f429, bin 660a9524)mre
  • 4a802ae7status: RV48 runningmre
  • 0a5412b6RV48 brief, day48 outmre
  • 56665efelog: CB69 mergedmre
  • 05448a98Merge cb69-imports: appTitle in a dependency-free topicmre
  • e55b379dKeep shared app title independent of the route import graphmre
  • 9a99659blog: hybriel#334, batch waits for day48mre
  • 85fa6c9blog: CB68 result, CB69 startedmre