Open way modify
This commit is contained in:
@@ -38,6 +38,7 @@ export class IndexProtocol {
|
||||
buf.writeBigUInt64LE(0n, 28); // dataFileSize
|
||||
buf.writeUInt32LE(0, 36); // latestSequence
|
||||
buf.writeUInt8(autoIncrementSequence ? 1 : 0, 40); // autoIncrementSequence
|
||||
buf.writeUInt32LE(0, 41); // flags
|
||||
return buf;
|
||||
}
|
||||
|
||||
@@ -52,10 +53,15 @@ export class IndexProtocol {
|
||||
dataFileSize: buf.readBigUInt64LE(28),
|
||||
latestSequence: buf.readUInt32LE(36),
|
||||
autoIncrementSequence: buf.readUInt8(40) === 1,
|
||||
reserved: buf.subarray(41, 64),
|
||||
flags: buf.readUInt32LE(41),
|
||||
reserved: buf.subarray(45, 64),
|
||||
};
|
||||
}
|
||||
|
||||
static writeFlags(buf: Buffer, flags: number): void {
|
||||
buf.writeUInt32LE(flags, 41);
|
||||
}
|
||||
|
||||
static updateHeaderCounts(
|
||||
buf: Buffer,
|
||||
writtenCnt: number,
|
||||
|
||||
@@ -7,14 +7,14 @@ export class IndexReader {
|
||||
private buffer: Buffer | null = null;
|
||||
private header: IndexHeader | null = null;
|
||||
|
||||
readonly path: string;
|
||||
private path: string | null = null;
|
||||
|
||||
constructor(path: string) {
|
||||
this.path = path;
|
||||
constructor() {
|
||||
}
|
||||
|
||||
open(): void {
|
||||
open(idxFilePath: string): void {
|
||||
// Read entire file into buffer (simpler than mmap for read-only access)
|
||||
this.path = idxFilePath;
|
||||
this.buffer = fs.readFileSync(this.path);
|
||||
this.header = IndexProtocol.readHeader(this.buffer);
|
||||
}
|
||||
@@ -24,6 +24,11 @@ export class IndexReader {
|
||||
return this.header;
|
||||
}
|
||||
|
||||
getFlags(): number {
|
||||
if (!this.header) throw new Error('Index file not opened');
|
||||
return this.header.flags;
|
||||
}
|
||||
|
||||
getEntry(index: number): IndexEntry | null {
|
||||
if (!this.buffer || !this.header) throw new Error('Index file not opened');
|
||||
if (index < 0 || index >= this.header.entryCount) return null;
|
||||
|
||||
@@ -9,6 +9,7 @@ export interface IndexHeader {
|
||||
dataFileSize: bigint;
|
||||
latestSequence: number;
|
||||
autoIncrementSequence: boolean;
|
||||
flags: number;
|
||||
reserved: Buffer;
|
||||
}
|
||||
|
||||
|
||||
@@ -151,6 +151,9 @@ export class IndexWriter {
|
||||
this.dataFileSize = newDataEnd;
|
||||
}
|
||||
|
||||
// Sync immediately after write
|
||||
this.writeHeader();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -158,7 +161,7 @@ export class IndexWriter {
|
||||
return this.latestSequence;
|
||||
}
|
||||
|
||||
syncHeader(): void {
|
||||
writeHeader(): void {
|
||||
if (!this.headerBuf || this.fd === null) return;
|
||||
|
||||
// Update header counts
|
||||
@@ -177,7 +180,7 @@ export class IndexWriter {
|
||||
if (this.fd === null) return;
|
||||
|
||||
// Sync header first
|
||||
this.syncHeader();
|
||||
this.writeHeader();
|
||||
|
||||
// Sync all file changes to disk
|
||||
fs.fsyncSync(this.fd);
|
||||
@@ -198,6 +201,16 @@ export class IndexWriter {
|
||||
this.entryBuf = null;
|
||||
}
|
||||
|
||||
writeFlags(flags: number): void {
|
||||
if (!this.headerBuf || this.fd === null) {
|
||||
throw new Error('Index file not opened');
|
||||
}
|
||||
|
||||
IndexProtocol.writeFlags(this.headerBuf, flags);
|
||||
fs.writeSync(this.fd, this.headerBuf, 41, 4, 41);
|
||||
fs.fsyncSync(this.fd);
|
||||
}
|
||||
|
||||
getStats() {
|
||||
return {
|
||||
path: this.path,
|
||||
|
||||
Reference in New Issue
Block a user