tracker
All repositories: gitoria
5.7 KB
#!/usr/bin/env node// Usage: node check-index-keys.mjs <directory>//// READ ONLY. For every <name>.mpack in the directory (not below it) this lists the live// records whose indexed text field holds a comma, a newline, a carriage return or starts with// a double quote, and says which of them are MISSING from that field's index file// <name>.<field>.txt (mission 486: the engine before the fix dropped such entries from the// index when it was reopened). The indexed fields are the <name>.<field>.txt files beside// the .mpack; nothing is written, locked or created. Exit status 1 when any entry is missing.//// A missing entry is repaired by opening the table with the fixed hl:mpackdb: an index whose// idxstate carries no "idxFmt":2 mark is rebuilt from the data once, the data itself holds// every record.import { readFileSync, readdirSync, existsSync } from 'node:fs'import { join } from 'node:path'const dir = process.argv[2]if (!dir) {console.error('usage: node check-index-keys.mjs <directory>')process.exit(2)}// --- a small MessagePack reader (the subset the engine writes: maps, arrays, strings, numbers)function decode(buf, start) {let p = startconst val = () => {const b = buf[p++]if (b <= 0x7f) return bif (b >= 0xe0) return b - 256if ((b & 0xe0) === 0xa0) return str(b & 0x1f)if ((b & 0xf0) === 0x80) return map(b & 0x0f)if ((b & 0xf0) === 0x90) return arr(b & 0x0f)switch (b) {case 0xc0: return nullcase 0xc2: return falsecase 0xc3: return truecase 0xc4: return bin(buf[p++])case 0xc5: { const n = buf.readUInt16BE(p); p += 2; return bin(n) }case 0xc6: { const n = buf.readUInt32BE(p); p += 4; return bin(n) }case 0xca: { const v = buf.readFloatBE(p); p += 4; return v }case 0xcb: { const v = buf.readDoubleBE(p); p += 8; return v }case 0xcc: return buf[p++]case 0xcd: { const v = buf.readUInt16BE(p); p += 2; return v }case 0xce: { const v = buf.readUInt32BE(p); p += 4; return v }case 0xcf: { const v = Number(buf.readBigUInt64BE(p)); p += 8; return v }case 0xd0: return buf.readInt8(p++)case 0xd1: { const v = buf.readInt16BE(p); p += 2; return v }case 0xd2: { const v = buf.readInt32BE(p); p += 4; return v }case 0xd3: { const v = Number(buf.readBigInt64BE(p)); p += 8; return v }case 0xd9: return str(buf[p++])case 0xda: { const n = buf.readUInt16BE(p); p += 2; return str(n) }case 0xdb: { const n = buf.readUInt32BE(p); p += 4; return str(n) }case 0xdc: { const n = buf.readUInt16BE(p); p += 2; return arr(n) }case 0xdd: { const n = buf.readUInt32BE(p); p += 4; return arr(n) }case 0xde: { const n = buf.readUInt16BE(p); p += 2; return map(n) }case 0xdf: { const n = buf.readUInt32BE(p); p += 4; return map(n) }default: throw new Error('unsupported msgpack byte 0x' + b.toString(16))}}const str = (n) => { const s = buf.toString('utf8', p, p + n); p += n; return s }const bin = (n) => { p += n; return null }const arr = (n) => { const a = []; for (let i = 0; i < n; i++) a.push(val()); return a }const map = (n) => { const o = {}; for (let i = 0; i < n; i++) { const k = val(); o[k] = val() } return o }return val()}// --- an index file: the offsets it names (a quoted key is a JSON string, else split from the right)function indexOffsets(text) {const offsets = new Set()for (const line of text.split('\n')) {if (!line) continueif (line[0] === '"') {let rest = nullfor (let i = 1; i < line.length; i++) {if (line[i] === '\\') { i++; continue }if (line[i] === '"') { rest = line.slice(i + 1); break }}if (rest !== null && /^,\d+,\d+$/.test(rest)) { offsets.add(Number(rest.split(',')[1])); continue }}const c2 = line.lastIndexOf(',')const c1 = c2 > 0 ? line.lastIndexOf(',', c2 - 1) : -1if (c1 < 0) continueconst off = Number(line.slice(c1 + 1, c2))if (Number.isInteger(off)) offsets.add(off)}return offsets}const risky = (v) => typeof v === 'string' && (/[,\n\r]/.test(v) || v[0] === '"')let missingTotal = 0const all = readdirSync(dir)const names = all.filter((f) => f.endsWith('.mpack')).map((f) => f.slice(0, -'.mpack'.length)).sort()for (const name of names) {const data = readFileSync(join(dir, name + '.mpack'))let deleted = new Set()const metaPath = join(dir, name + '.meta.json')if (existsSync(metaPath)) {try { deleted = new Set(JSON.parse(readFileSync(metaPath, 'utf8')).deleted || []) } catch { /* no usable meta */ }}const fields = all.filter((f) => f.startsWith(name + '.') && f.endsWith('.txt')).map((f) => f.slice(name.length + 1, -4)).filter((f) => f && !f.includes('.'))const indexes = new Map(fields.map((f) => [f, indexOffsets(readFileSync(join(dir, name + '.' + f + '.txt'), 'utf8'))]))let live = 0, riskyCount = 0, missing = 0for (let pos = 0; pos + 4 <= data.length;) {const size = data.readUInt32LE(pos)if (size < 5 || pos + size > data.length) { console.log(`${name}.mpack: record frame at ${pos} is damaged or cut short; stopped there`); break }if (!deleted.has(pos)) {live++let rec = nulltry { rec = decode(data, pos + 4) } catch (e) { console.log(`${name}.mpack: record at ${pos} cannot be read: ${e.message}`) }if (rec && typeof rec === 'object') {for (const [field, offsets] of indexes) {if (!risky(rec[field])) continueriskyCount++const lost = !offsets.has(pos)if (lost) { missing++; missingTotal++ }console.log(`${name}.${field}: record at offset ${pos} key ${JSON.stringify(rec[field])} ${lost ? 'MISSING from the index file' : 'present'}`)}}}pos += size}console.log(`${name}: ${live} live records, indexed fields [${fields.join(', ')}], ${riskyCount} with a comma/newline/CR/leading quote, ${missing} missing from their index`)}if (!names.length) console.log('no .mpack files in ' + dir)process.exit(missingTotal ? 1 : 0)
Branches
Latest commits
- 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
- 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