Interface modified mmap to fd

This commit is contained in:
Eli-Class
2026-01-29 09:24:48 +00:00
parent abc47d4909
commit c6e3eae22e
11 changed files with 727 additions and 1733 deletions

View File

@@ -1,213 +1,215 @@
// src/data-file/reader.ts
import * as fs from 'node:fs';
import mmap from '@elilee/mmap-native';
import { DATA_HEADER_SIZE } from './constants.js';
import { DataProtocol, DataHeader } from './protocol.js';
import { IndexReader } from '../idx/index.js';
import type { Serializer, DataEntry } from './types.js';
export class DataReader<T> {
private fd: number | null = null;
private buffer: Buffer | null = null;
private header: DataHeader | null = null;
private fd: number | null = null;
private header: DataHeader | null = null;
private indexReader: IndexReader;
private serializer: Serializer<T>;
private indexReader: IndexReader;
private serializer: Serializer<T>;
readonly dataPath: string;
readonly indexPath: string;
readonly dataPath: string;
readonly indexPath: string;
constructor(basePath: string, serializer: Serializer<T>) {
this.dataPath = `${basePath}.dat`;
this.indexPath = `${basePath}.idx`;
this.serializer = serializer;
this.indexReader = new IndexReader(this.indexPath);
}
constructor(basePath: string, serializer: Serializer<T>) {
this.dataPath = `${basePath}.dat`;
this.indexPath = `${basePath}.idx`;
this.serializer = serializer;
this.indexReader = new IndexReader(this.indexPath);
}
open(): void {
const stats = fs.statSync(this.dataPath);
this.fd = fs.openSync(this.dataPath, 'r');
open(): void {
this.fd = fs.openSync(this.dataPath, 'r');
this.buffer = mmap.map(
stats.size,
mmap.PROT_READ,
mmap.MAP_SHARED,
this.fd,
0
);
// Read header only
const headerBuf = Buffer.alloc(DATA_HEADER_SIZE);
fs.readSync(this.fd, headerBuf, 0, DATA_HEADER_SIZE, 0);
this.header = DataProtocol.readHeader(headerBuf);
this.header = DataProtocol.readHeader(this.buffer);
this.indexReader.open();
}
this.indexReader.open();
}
getHeader(): DataHeader {
if (!this.header) throw new Error('Data file not opened');
return this.header;
}
private readRecord(offset: bigint, length: number): Buffer {
if (this.fd === null) throw new Error('Data file not opened');
getBySequence(sequence: number): DataEntry<T> | null {
if (!this.buffer) throw new Error('Data file not opened');
const buf = Buffer.alloc(length);
fs.readSync(this.fd, buf, 0, length, Number(offset));
return buf;
}
const found = this.indexReader.binarySearchBySequence(sequence);
if (!found) return null;
getHeader(): DataHeader {
if (!this.header) throw new Error('Data file not opened');
return this.header;
}
const result = DataProtocol.deserializeRecord(
this.buffer,
Number(found.entry.offset),
this.serializer
);
if (!result) return null;
getBySequence(sequence: number): DataEntry<T> | null {
if (this.fd === null) throw new Error('Data file not opened');
return {
sequence: found.entry.sequence,
timestamp: found.entry.timestamp,
data: result.data,
};
}
const found = this.indexReader.binarySearchBySequence(sequence);
if (!found) return null;
getByIndex(index: number): DataEntry<T> | null {
if (!this.buffer) throw new Error('Data file not opened');
const entry = this.indexReader.getEntry(index);
if (!entry) return null;
const result = DataProtocol.deserializeRecord(
this.buffer,
Number(entry.offset),
this.serializer
);
if (!result) return null;
return {
sequence: entry.sequence,
timestamp: entry.timestamp,
data: result.data,
};
}
getBulkData(startSeq: number, endSeq: number): DataEntry<T>[] {
if (!this.buffer) throw new Error('Data file not opened');
const results: DataEntry<T>[] = [];
const indexHeader = this.indexReader.getHeader();
let startIdx = this.findStartIndex(startSeq, indexHeader.validCount);
for (let i = startIdx; i < indexHeader.validCount; i++) {
const entry = this.indexReader.getEntry(i);
if (!entry) continue;
if (entry.sequence > endSeq) break;
if (entry.sequence >= startSeq) {
const buf = this.readRecord(found.entry.offset, found.entry.length);
const result = DataProtocol.deserializeRecord(
this.buffer,
Number(entry.offset),
this.serializer
buf,
0,
this.serializer
);
if (result) {
results.push({
if (!result) return null;
return {
sequence: found.entry.sequence,
timestamp: found.entry.timestamp,
data: result.data,
};
}
getByIndex(index: number): DataEntry<T> | null {
if (this.fd === null) throw new Error('Data file not opened');
const entry = this.indexReader.getEntry(index);
if (!entry) return null;
const buf = this.readRecord(entry.offset, entry.length);
const result = DataProtocol.deserializeRecord(
buf,
0,
this.serializer
);
if (!result) return null;
return {
sequence: entry.sequence,
timestamp: entry.timestamp,
data: result.data,
});
};
}
getBulkData(startSeq: number, endSeq: number): DataEntry<T>[] {
if (this.fd === null) throw new Error('Data file not opened');
const results: DataEntry<T>[] = [];
const indexHeader = this.indexReader.getHeader();
let startIdx = this.findStartIndex(startSeq, indexHeader.writtenCnt);
for (let i = startIdx; i < indexHeader.writtenCnt; i++) {
const entry = this.indexReader.getEntry(i);
if (!entry) continue;
if (entry.sequence > endSeq) break;
if (entry.sequence >= startSeq) {
const buf = this.readRecord(entry.offset, entry.length);
const result = DataProtocol.deserializeRecord(
buf,
0,
this.serializer
);
if (result) {
results.push({
sequence: entry.sequence,
timestamp: entry.timestamp,
data: result.data,
});
}
}
}
}
return results;
}
return results;
}
private findStartIndex(targetSeq: number, writtenCnt: number): number {
let left = 0;
let right = writtenCnt - 1;
let result = 0;
private findStartIndex(targetSeq: number, validCount: number): number {
let left = 0;
let right = validCount - 1;
let result = 0;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const entry = this.indexReader.getEntry(mid);
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const entry = this.indexReader.getEntry(mid);
if (!entry) {
right = mid - 1;
continue;
}
if (!entry) {
right = mid - 1;
continue;
}
if (entry.sequence >= targetSeq) {
result = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
if (entry.sequence >= targetSeq) {
result = mid;
right = mid - 1;
} else {
left = mid + 1;
}
return result;
}
return result;
}
getBulkDataByTime(startTs: bigint, endTs: bigint): DataEntry<T>[] {
if (this.fd === null) throw new Error('Data file not opened');
getBulkDataByTime(startTs: bigint, endTs: bigint): DataEntry<T>[] {
if (!this.buffer) throw new Error('Data file not opened');
const indexResults = this.indexReader.findByTimeRange(startTs, endTs);
const results: DataEntry<T>[] = [];
const indexResults = this.indexReader.findByTimeRange(startTs, endTs);
const results: DataEntry<T>[] = [];
for (const { entry } of indexResults) {
const buf = this.readRecord(entry.offset, entry.length);
const result = DataProtocol.deserializeRecord(
buf,
0,
this.serializer
);
if (result) {
results.push({
sequence: entry.sequence,
timestamp: entry.timestamp,
data: result.data,
});
}
}
for (const { entry } of indexResults) {
const result = DataProtocol.deserializeRecord(
this.buffer,
Number(entry.offset),
this.serializer
);
if (result) {
results.push({
sequence: entry.sequence,
timestamp: entry.timestamp,
data: result.data,
});
}
return results;
}
return results;
}
getAllData(): DataEntry<T>[] {
if (this.fd === null) throw new Error('Data file not opened');
getAllData(): DataEntry<T>[] {
if (!this.buffer) throw new Error('Data file not opened');
const entries = this.indexReader.getAllEntries();
const results: DataEntry<T>[] = [];
const entries = this.indexReader.getAllEntries();
const results: DataEntry<T>[] = [];
for (const entry of entries) {
const buf = this.readRecord(entry.offset, entry.length);
const result = DataProtocol.deserializeRecord(
buf,
0,
this.serializer
);
if (result) {
results.push({
sequence: entry.sequence,
timestamp: entry.timestamp,
data: result.data,
});
}
}
for (const entry of entries) {
const result = DataProtocol.deserializeRecord(
this.buffer,
Number(entry.offset),
this.serializer
);
if (result) {
results.push({
sequence: entry.sequence,
timestamp: entry.timestamp,
data: result.data,
});
}
return results;
}
return results;
}
getRecordCount(): number {
return this.indexReader.getHeader().validCount;
}
getLastSequence(): number {
return this.indexReader.getHeader().lastSequence;
}
close(): void {
if (this.buffer) {
mmap.unmap(this.buffer);
this.buffer = null;
getRecordCount(): number {
return this.indexReader.getHeader().writtenCnt;
}
if (this.fd !== null) {
fs.closeSync(this.fd);
this.fd = null;
getLastSequence(): number {
return this.indexReader.getHeader().latestSequence;
}
this.header = null;
this.indexReader.close();
}
}
close(): void {
if (this.fd !== null) {
fs.closeSync(this.fd);
this.fd = null;
}
this.header = null;
this.indexReader.close();
}
}

View File

@@ -1,16 +1,19 @@
import { IndexFileOptions } from "../idx/types.js";
// src/data-file/types.ts
export interface Serializer<T> {
serialize(data: T): Buffer;
deserialize(buf: Buffer): T;
serialize(data: T): Buffer;
deserialize(buf: Buffer): T;
}
export interface DataEntry<T> {
sequence: number;
timestamp: bigint;
data: T;
sequence: number;
timestamp: bigint;
data: T;
}
export interface DataFileOptions<T> {
serializer: Serializer<T>;
maxEntries?: number;
}
serializer: Serializer<T>;
forceTruncate?: boolean;
indexFileOpt: IndexFileOptions;
}

View File

@@ -2,119 +2,189 @@
import * as fs from 'node:fs';
import { DATA_HEADER_SIZE } from './constants.js';
import { DataProtocol } from './protocol.js';
import { IndexWriter } from '../idx/index.js';
import { IndexWriter, IndexFileOptionsRequired } from '../idx/index.js';
import type { Serializer, DataFileOptions } from './types.js';
export class DataWriter<T> {
private fd: number | null = null;
private headerBuf: Buffer | null = null;
private currentOffset: bigint = BigInt(DATA_HEADER_SIZE);
private recordCount = 0;
private fd: number | null = null;
private headerBuf: Buffer | null = null;
private currentOffset: bigint = BigInt(DATA_HEADER_SIZE);
private recordCount = 0;
private indexWriter: IndexWriter;
private serializer: Serializer<T>;
private indexWriter: IndexWriter;
readonly dataPath: string;
readonly indexPath: string;
// See DataFileOptions
private readonly serializer: Serializer<T>;
private readonly forceTruncate: boolean;
constructor(basePath: string, options: DataFileOptions<T>) {
this.dataPath = `${basePath}.dat`;
this.indexPath = `${basePath}.idx`;
this.serializer = options.serializer;
private latestSequence: number = 0;
const maxEntries = options.maxEntries ?? 10_000_000;
this.indexWriter = new IndexWriter(this.indexPath, { maxEntries });
}
private readonly indexFileOpt: IndexFileOptionsRequired;
open(): void {
const isNew = !fs.existsSync(this.dataPath);
private dataPath: string | null = null;
private indexPath: string | null = null;
this.fd = fs.openSync(this.dataPath, isNew ? 'w+' : 'r+');
this.headerBuf = Buffer.alloc(DATA_HEADER_SIZE);
if (isNew) {
const header = DataProtocol.createHeader();
fs.writeSync(this.fd, header, 0, DATA_HEADER_SIZE, 0);
this.currentOffset = BigInt(DATA_HEADER_SIZE);
this.recordCount = 0;
} else {
fs.readSync(this.fd, this.headerBuf, 0, DATA_HEADER_SIZE, 0);
const header = DataProtocol.readHeader(this.headerBuf);
this.currentOffset = header.fileSize;
this.recordCount = header.recordCount;
constructor(options: DataFileOptions<T>) {
this.serializer = options.serializer;
this.forceTruncate = options.forceTruncate ?? false;
this.indexFileOpt = {
maxEntries: options.indexFileOpt.maxEntries ?? 10_000_000,
autoIncrementSequence: options.indexFileOpt.autoIncrementSequence ?? false
}
this.indexWriter = new IndexWriter(this.indexFileOpt);
}
this.indexWriter.open();
}
open(basePath: string): void {
this.dataPath = `${basePath}.dat`;
this.indexPath = `${basePath}.idx`;
append(data: T, timestamp?: bigint): number {
if (this.fd === null) throw new Error('Data file not opened');
// Index file 을 Open 함으로써 파일을 시작 할 수 있는지 검증 (Throw 로써)
// Open index file with maxEntries and autoIncrementSequence
const writtenCount = this.indexWriter.open(this.indexPath, this.forceTruncate);
const isNew = !fs.existsSync(this.dataPath);
const buf = DataProtocol.serializeRecord(data, this.serializer);
const offset = this.currentOffset;
// Index file 은 초기화인데, 신규파일 혹은 강제 클리어가 아니라면
if (writtenCount === 0 && !(isNew || this.forceTruncate)) {
throw new Error(`Index file & Data File is invalid ${this.indexPath} is initial but ${this.dataPath} is exists`);
}
if (writtenCount > 0 && isNew) {
throw new Error(`Index file & Data File is invalid data of ${this.indexPath} | ${writtenCount} is exists but ${this.dataPath} is not exists`);
}
fs.writeSync(this.fd, buf, 0, buf.length, Number(offset));
// Warn if forceTruncate will delete existing data
if (this.forceTruncate && !isNew) {
const stats = fs.statSync(this.dataPath);
const sizeMB = (stats.size / 1024 / 1024).toFixed(2);
console.warn(
`[DataWriter] forceTruncate enabled: Deleting ${sizeMB} MB of existing data\n` +
` Index: ${this.indexPath} (${writtenCount} records)\n` +
` Data: ${this.dataPath}`
);
}
const sequence = this.indexWriter.getNextSequence();
const ts = timestamp ?? BigInt(Date.now()) * 1000000n;
this.fd = fs.openSync(this.dataPath,
isNew || this.forceTruncate ? 'w+' : 'r+');
this.indexWriter.append(offset, buf.length, ts);
try {
this.headerBuf = Buffer.alloc(DATA_HEADER_SIZE);
this.currentOffset += BigInt(buf.length);
this.recordCount++;
if (isNew || this.forceTruncate) {
const header = DataProtocol.createHeader();
fs.writeSync(this.fd, header, 0, DATA_HEADER_SIZE, 0);
this.currentOffset = BigInt(DATA_HEADER_SIZE);
this.recordCount = 0;
this.latestSequence = 0;
} else {
fs.readSync(this.fd, this.headerBuf, 0, DATA_HEADER_SIZE, 0);
const header = DataProtocol.readHeader(this.headerBuf);
return sequence;
}
// Validate: Data file recordCount must match Index file writtenCnt
if (header.recordCount !== writtenCount) {
throw new Error(
`Data file record count mismatch: Data has ${header.recordCount} but Index has ${writtenCount}`
);
}
appendBulk(records: T[], timestamp?: bigint): number[] {
const sequences: number[] = [];
const ts = timestamp ?? BigInt(Date.now()) * 1000000n;
for (const record of records) {
const seq = this.append(record, ts);
sequences.push(seq);
this.currentOffset = header.fileSize;
this.recordCount = header.recordCount;
this.latestSequence = this.indexWriter.getLatestSequence();
}
} catch (error) {
// Clean up resources on error
if (this.fd !== null) {
fs.closeSync(this.fd);
this.fd = null;
}
this.headerBuf = null;
throw error;
}
}
return sequences;
}
append(data: T, sequence?: number, timestamp?: bigint): number {
if (this.fd === null) throw new Error('Data file not opened');
getLastSequence(): number {
return this.indexWriter.getLastSequence();
}
const buf = DataProtocol.serializeRecord(data, this.serializer);
const offset = this.currentOffset;
getNextSequence(): number {
return this.indexWriter.getNextSequence();
}
fs.writeSync(this.fd, buf, 0, buf.length, Number(offset));
sync(): void {
if (this.fd === null || !this.headerBuf) return;
// Write to index file
this.indexWriter.write(offset, buf.length, sequence, timestamp);
DataProtocol.updateHeader(this.headerBuf, this.currentOffset, this.recordCount);
fs.writeSync(this.fd, this.headerBuf, 0, DATA_HEADER_SIZE, 0);
fs.fsyncSync(this.fd);
// Update latestSequence to the most recent sequence
this.latestSequence = this.indexWriter.getLatestSequence();
this.indexWriter.syncAll();
}
this.currentOffset += BigInt(buf.length);
++this.recordCount;
close(): void {
this.sync();
if (this.fd !== null) {
fs.closeSync(this.fd);
this.fd = null;
return this.latestSequence;
}
this.indexWriter.close();
this.headerBuf = null;
}
/*
appendBulk(records: T[], sequences?: number[], timestamp?: bigint): number[] {
// Runtime check: sequences required when autoIncrementSequence is false
if (!this.autoIncrementSequence) {
if (!sequences) {
throw new Error('sequences is required when autoIncrementSequence is false');
}
if (sequences.length !== records.length) {
throw new Error(`sequences length (${sequences.length}) must match records length (${records.length})`);
}
}
getStats() {
return {
dataPath: this.dataPath,
indexPath: this.indexPath,
currentOffset: this.currentOffset,
recordCount: this.recordCount,
lastSequence: this.indexWriter.getLastSequence(),
};
}
}
const resultSequences: number[] = [];
const ts = timestamp ?? BigInt(Date.now()) * 1000000n;
for (let i = 0; i < records.length; i++) {
const seq = sequences?.[i];
const resultSeq = this.append(records[i], seq, ts);
resultSequences.push(resultSeq);
}
return resultSequences;
}
*/
getLatestSequence(): number {
return this.latestSequence;
}
getNextSequence(): number {
return this.latestSequence + 1;
}
sync(): void {
if (this.fd === null || !this.headerBuf) return;
DataProtocol.updateHeader(this.headerBuf, this.currentOffset, this.recordCount);
fs.writeSync(this.fd, this.headerBuf, 0, DATA_HEADER_SIZE, 0);
fs.fsyncSync(this.fd);
this.indexWriter.syncAll();
}
close(): void {
this.sync();
if (this.fd !== null) {
fs.closeSync(this.fd);
this.fd = null;
}
this.indexWriter.close();
this.headerBuf = null;
}
getStats() {
return {
dataPath: this.dataPath,
indexPath: this.indexPath,
currentOffset: this.currentOffset,
recordCount: this.recordCount,
latestSequence: this.indexWriter.getLatestSequence(),
};
}
}