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
- b7e6b48ePack normalized search text behind numeric offsetsmre
- 8dfe42c8mission DP62mre
- e347fef4Merge rv44: tracker#61 #62 on Hybriel day44mre
- 8274bcf1log: RV44 done, deploy held for the usage windowmre
- 1ef7a3b1RV44: report day44 gates and migration/restart proofsmre
- a5f62f6cRV44: re-vendor Hybriel set day44 (source 222edef7, bin 4f0f263e)mre
- 6159a248Merge branch 'hm62-personindex' into rv44mre
- a8923d93log: RV44 startedmre
- cf28cf77mission RV44mre
- 585782dfLog: tracker#60 Chrome look, STATUS rewrittenmre
- 147d0172conductor: caramboleyo is the user, not owner/creatorApollo
- 21dd0cfblog: #60 live measurement, reviewmre
- 7793cf94HM62b report: person indexes with fixed mpackdbmre
- b4c8d3a9Document tracker#62 fixed-index migration and GC proofmre
- 09d66ed5status: #60 livemre
- 4171672flog: #60 livemre
- f52862a2log: HM62b startedmre
- 88fde2c4mission HM62bmre
- a722e526log: #62 blocked by hybriel#317mre
- 074a7418log: #62 assigned to Tracker; pending rulemre