refactor(tracker/BatchWriter): explicit logic

This commit is contained in:
ShiKhu 2022-06-21 01:05:20 +02:00
parent 67b83deddb
commit 4a5093addf

View file

@ -16,11 +16,25 @@ export default class BatchWriter {
private timestamp: number,
private onBatch: (batch: Uint8Array) => void
) {
this.prepareBatchMeta()
this.prepare()
}
private prepareBatchMeta(): boolean {
return new BatchMeta(this.pageNo, this.nextIndex, this.timestamp).encode(this.writer)
private prepare(): void {
if (!this.writer.isEmpty()) {
//console.log("")
return
}
new BatchMeta(this.pageNo, this.nextIndex, this.timestamp).encode(this.writer)
}
private write(message: Message): boolean {
const wasWritten = message.encode(this.writer)
if (wasWritten) {
this.isEmpty = false
this.writer.checkpoint()
this.nextIndex++
}
return wasWritten
}
private beaconSizeLimit = 1e6
@ -28,41 +42,31 @@ export default class BatchWriter {
this.beaconSizeLimit = limit
}
// TODO: clear workflow
writeMessage(message: Message) {
if (message instanceof Timestamp) {
this.timestamp = (<any>message).timestamp
}
if (!message.encode(this.writer)) {
if (!this.isEmpty) {
this.onBatch(this.writer.flush())
this.prepareBatchMeta()
}
while (!message.encode(this.writer)) {
if (this.beaconSize === this.beaconSizeLimit) {
console.warn("OpenReplay: beacon size overflow. Skipping large message.");
this.writer.reset()
this.prepareBatchMeta()
this.isEmpty = true
return
}
// MBTODO: tempWriter for one message?
this.beaconSize = Math.min(this.beaconSize*2, this.beaconSizeLimit)
this.writer = new PrimitiveWriter(this.beaconSize)
this.prepareBatchMeta()
while (!this.write(message)) {
this.finaliseBatch()
if (this.beaconSize === this.beaconSizeLimit) {
console.warn("OpenReplay: beacon size overflow. Skipping large message.");
this.writer.reset()
this.prepare()
this.isEmpty = true
return
}
// MBTODO: tempWriter for one message?
this.beaconSize = Math.min(this.beaconSize*2, this.beaconSizeLimit)
this.writer = new PrimitiveWriter(this.beaconSize)
this.prepare()
this.isEmpty = true
}
this.writer.checkpoint()
this.nextIndex++
this.isEmpty = false
}
finaliseBatch() {
if (this.isEmpty) { return }
this.onBatch(this.writer.flush())
this.prepareBatchMeta()
this.prepare()
this.isEmpty = true
}