* Repack the WAL by recovering all valid records and rewriting cleanly. * Removes corrupted entries and ensures clean formatting. * Updates the recovery state (accessible via getStats). * @param out - Output path (defaults to current file)
(out = this.#file)
| 223 | * @param out - Output path (defaults to current file) |
| 224 | */ |
| 225 | repack(out = this.#file) { |
| 226 | this.close(); |
| 227 | const r = this.recover(); |
| 228 | if (r.errors.length > 0) { |
| 229 | // eslint-disable-next-line no-console |
| 230 | console.log('WAL repack encountered decode errors'); |
| 231 | } |
| 232 | |
| 233 | // Check if any records are invalid entries (from tolerant codec) |
| 234 | const hasInvalidEntries = r.records.some( |
| 235 | rec => typeof rec === 'object' && rec != null && '__invalid' in rec, |
| 236 | ); |
| 237 | if (hasInvalidEntries) { |
| 238 | // eslint-disable-next-line no-console |
| 239 | console.log('Found invalid entries during WAL repack'); |
| 240 | } |
| 241 | const recordsToWrite = hasInvalidEntries |
| 242 | ? filterValidRecords(r.records) |
| 243 | : (r.records as T[]); |
| 244 | ensureDirectoryExistsSync(path.dirname(out)); |
| 245 | fs.writeFileSync(out, `${recordsToWrite.map(this.#encode).join('\n')}\n`); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Get comprehensive statistics about the WAL file state. |
no test coverage detected