Optimize bug fix
This commit is contained in:
@@ -163,32 +163,24 @@ export class DataReader<T> {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
getAllData(): DataEntry<T>[] {
|
getAllData(): DataEntry<T>[] {
|
||||||
if (this.fd === null) throw new Error('Data file not opened');
|
if (this.fd === null) throw new Error('Data file not opened');
|
||||||
|
|
||||||
const entries = this.indexReader.getAllEntries();
|
|
||||||
const results: DataEntry<T>[] = [];
|
const results: DataEntry<T>[] = [];
|
||||||
|
let first = this.getByIndex(0);
|
||||||
for (const entry of entries) {
|
if (first == null) {
|
||||||
const buf = this.readRecordAt(this.fd, entry.offset, entry.length);
|
return [];
|
||||||
const result = DataProtocol.deserializeRecord(
|
|
||||||
buf,
|
|
||||||
0,
|
|
||||||
this.serializer
|
|
||||||
);
|
|
||||||
if (result) {
|
|
||||||
results.push({
|
|
||||||
sequence: entry.sequence,
|
|
||||||
timestamp: entry.timestamp,
|
|
||||||
data: result.data,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
results.push(first);
|
||||||
|
|
||||||
|
do {
|
||||||
|
const entry = this.readNext();
|
||||||
|
if (!entry) break;
|
||||||
|
results.push(entry);
|
||||||
|
} while (true);
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
getRecordCount(): number {
|
getRecordCount(): number {
|
||||||
return this.indexReader.getHeader().writtenCnt;
|
return this.indexReader.getHeader().writtenCnt;
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ export class IndexReader {
|
|||||||
private path: string | null = null;
|
private path: string | null = null;
|
||||||
|
|
||||||
// Sequential read state
|
// Sequential read state
|
||||||
private currentIndex: number = 0;
|
private currentIndex: number = -1;
|
||||||
private currentSequence: number = 0;
|
private currentSequence: number = -1;
|
||||||
|
|
||||||
// Reusable buffer for reading entries
|
// Reusable buffer for reading entries
|
||||||
private entryBuffer: Buffer = Buffer.alloc(INDEX_ENTRY_SIZE);
|
private entryBuffer: Buffer = Buffer.alloc(INDEX_ENTRY_SIZE);
|
||||||
@@ -49,22 +49,6 @@ export class IndexReader {
|
|||||||
return this.readEntryAt(this.fd, index);
|
return this.readEntryAt(this.fd, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
findBySequenceRange(startSeq: number, endSeq: number): { index: number; entry: IndexEntry }[] {
|
|
||||||
if (this.fd === null || !this.header) throw new Error('Index file not opened');
|
|
||||||
|
|
||||||
const results: { index: number; entry: IndexEntry }[] = [];
|
|
||||||
const first = this.searchSequenceLowerBound(this.fd, this.header.writtenCnt, startSeq);
|
|
||||||
if (first == null) return [];
|
|
||||||
|
|
||||||
results.push(first);
|
|
||||||
for (let i = this.currentIndex; i < this.header.writtenCnt; i++) {
|
|
||||||
const entry = this.readNextEntry(this.fd);
|
|
||||||
if (entry == null || entry.sequence > endSeq) break;
|
|
||||||
results.push({ index: this.currentIndex, entry });
|
|
||||||
}
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
getAllEntries(): IndexEntry[] {
|
getAllEntries(): IndexEntry[] {
|
||||||
if (this.fd === null || !this.header) throw new Error('Index file not opened');
|
if (this.fd === null || !this.header) throw new Error('Index file not opened');
|
||||||
@@ -78,15 +62,39 @@ export class IndexReader {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
findBySequenceRange(startSeq: number, endSeq: number): { index: number; entry: IndexEntry }[] {
|
||||||
|
if (this.fd === null || !this.header) throw new Error('Index file not opened');
|
||||||
|
|
||||||
|
const results: { index: number; entry: IndexEntry }[] = [];
|
||||||
|
const currIdx = this.currentIndex;
|
||||||
|
const first = this.searchSequenceLowerBound(this.fd, this.header.writtenCnt, startSeq);
|
||||||
|
if (first === null) {
|
||||||
|
this.currentIndex = currIdx;
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
results.push(first);
|
||||||
|
while (this.currentIndex < this.header.writtenCnt - 1) {
|
||||||
|
const entry = this.readNextEntry(this.fd);
|
||||||
|
if (entry == null || entry.sequence > endSeq) break;
|
||||||
|
results.push({ index: this.currentIndex, entry });
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
findByTimeRange(startTs: bigint, endTs: bigint): { index: number; entry: IndexEntry }[] {
|
findByTimeRange(startTs: bigint, endTs: bigint): { index: number; entry: IndexEntry }[] {
|
||||||
if (this.fd === null || !this.header) throw new Error('Index file not opened');
|
if (this.fd === null || !this.header) throw new Error('Index file not opened');
|
||||||
|
|
||||||
const results: { index: number; entry: IndexEntry }[] = [];
|
const results: { index: number; entry: IndexEntry }[] = [];
|
||||||
|
const currIdx = this.currentIndex;
|
||||||
const first = this.searchTimestampLowerBound(this.fd, this.header.writtenCnt, startTs);
|
const first = this.searchTimestampLowerBound(this.fd, this.header.writtenCnt, startTs);
|
||||||
if (first === null) return [];
|
if (first === null) {
|
||||||
|
this.currentIndex = currIdx;
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
results.push(first);
|
results.push(first);
|
||||||
for (let i = this.currentIndex; i < this.header.writtenCnt; i++) {
|
while (this.currentIndex < this.header.writtenCnt - 1) {
|
||||||
const entry = this.readNextEntry(this.fd);
|
const entry = this.readNextEntry(this.fd);
|
||||||
if (entry == null || entry.timestamp > endTs) break;
|
if (entry == null || entry.timestamp > endTs) break;
|
||||||
results.push({ index: this.currentIndex, entry });
|
results.push({ index: this.currentIndex, entry });
|
||||||
|
|||||||
Reference in New Issue
Block a user