T-154: M2.3l — libs/data/questdb: pure TypeScript ILP v2 batch writer for bars_1m / fx_1m (ilp-v2-writer.ts, binary doubles + microsecond timestamps over an injected sink, byte-parity with tools/curate-etl/src/ilp.rs; no new deps, no live QuestDB)
This commit is contained in:
parent
18f1e6e471
commit
8ebc442512
2 changed files with 116 additions and 8 deletions
|
|
@ -481,19 +481,48 @@ export function createIlpV2BatchWriter(options: IlpV2BatchWriterOptions): IlpV2B
|
|||
}
|
||||
}
|
||||
|
||||
function snapshotBar1mRow(row: Bar1mIlpV2Row): Bar1mIlpV2Row {
|
||||
return {
|
||||
listing_id: row.listing_id,
|
||||
ts: row.ts,
|
||||
known_at: row.known_at,
|
||||
open: row.open,
|
||||
high: row.high,
|
||||
low: row.low,
|
||||
close: row.close,
|
||||
volume: row.volume,
|
||||
session: row.session,
|
||||
source: row.source,
|
||||
};
|
||||
}
|
||||
|
||||
function snapshotFx1mRow(row: Fx1mIlpV2Row): Fx1mIlpV2Row {
|
||||
return {
|
||||
pair: row.pair,
|
||||
source: row.source,
|
||||
open: row.open,
|
||||
high: row.high,
|
||||
low: row.low,
|
||||
close: row.close,
|
||||
ts: row.ts,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
writeBar1m(row: Bar1mIlpV2Row): Promise<void> {
|
||||
const snapshot = snapshotBar1mRow(row);
|
||||
return enqueue(async () => {
|
||||
assertHealthyForMutation('writeBar1m');
|
||||
const encoded = encodeBar1mIlpV2(row);
|
||||
const encoded = encodeBar1mIlpV2(snapshot);
|
||||
await enqueueEncodedRow(encoded);
|
||||
});
|
||||
},
|
||||
|
||||
writeFx1m(row: Fx1mIlpV2Row): Promise<void> {
|
||||
const snapshot = snapshotFx1mRow(row);
|
||||
return enqueue(async () => {
|
||||
assertHealthyForMutation('writeFx1m');
|
||||
const encoded = encodeFx1mIlpV2(row);
|
||||
const encoded = encodeFx1mIlpV2(snapshot);
|
||||
await enqueueEncodedRow(encoded);
|
||||
});
|
||||
},
|
||||
|
|
|
|||
|
|
@ -506,7 +506,7 @@ describe('createIlpV2BatchWriter batching, boundaries, and option validation', (
|
|||
expect(batches.every(b => b[b.byteLength - 1] === 0x0a)).toBe(true);
|
||||
}
|
||||
|
||||
// 2. Exact byte limit (2 * rowLen): flushes after every 2 rows.
|
||||
// 2. Exact byte limit (2 * rowLen): flushes immediately when the 2nd and 4th rows hit the limit.
|
||||
{
|
||||
const { sink, batches } = createMemorySink();
|
||||
const writer = createIlpV2BatchWriter({
|
||||
|
|
@ -514,14 +514,21 @@ describe('createIlpV2BatchWriter batching, boundaries, and option validation', (
|
|||
maxBatchBytes: 2 * rowLen,
|
||||
maxBatchRows: 100,
|
||||
});
|
||||
for (let i = 0; i < 5; i++) {
|
||||
await writer.writeBar1m(row);
|
||||
}
|
||||
await writer.writeBar1m(row);
|
||||
expect(batches).toHaveLength(0);
|
||||
await writer.writeBar1m(row);
|
||||
expect(batches).toHaveLength(1);
|
||||
expect(batches[0]).toEqual(concatBytes([oneLine, oneLine]));
|
||||
await writer.writeBar1m(row);
|
||||
expect(batches).toHaveLength(1);
|
||||
await writer.writeBar1m(row);
|
||||
expect(batches).toHaveLength(2);
|
||||
expect(batches[1]).toEqual(concatBytes([oneLine, oneLine]));
|
||||
await writer.writeBar1m(row);
|
||||
expect(batches).toHaveLength(2);
|
||||
const stats = await writer.finish();
|
||||
expect(stats).toEqual({ rows: 5, batches: 3, bytes: 5 * rowLen });
|
||||
expect(batches).toHaveLength(3);
|
||||
expect(batches[0]).toEqual(concatBytes([oneLine, oneLine]));
|
||||
expect(batches[1]).toEqual(concatBytes([oneLine, oneLine]));
|
||||
expect(batches[2]).toEqual(oneLine);
|
||||
}
|
||||
|
||||
|
|
@ -684,6 +691,78 @@ describe('createIlpV2BatchWriter serialization and fail-closed sink errors', ()
|
|||
]);
|
||||
});
|
||||
|
||||
it('snapshots row field values at submission when a caller reuses mutable row objects across overlapping writes', async () => {
|
||||
const { sink, batches } = createMemorySink();
|
||||
const writer = createIlpV2BatchWriter({ sink, maxBatchRows: 1, maxBatchBytes: 100_000 });
|
||||
|
||||
const mutableBar = sampleBar({
|
||||
listing_id: 10,
|
||||
ts: 1_700_000_000_000_000n,
|
||||
known_at: 1_700_000_060_000_000n,
|
||||
open: 100.0,
|
||||
high: 101.0,
|
||||
low: 99.5,
|
||||
close: 100.5,
|
||||
volume: 1_000,
|
||||
session: 'PRE',
|
||||
source: 'bar_one',
|
||||
});
|
||||
const expectedBar1 = encodeBar1mIlpV2(mutableBar);
|
||||
|
||||
const mutableFx = sampleFx({
|
||||
pair: 'USD.CAD',
|
||||
source: 'fx_one',
|
||||
open: 1.35,
|
||||
high: 1.36,
|
||||
low: 1.34,
|
||||
close: 1.355,
|
||||
ts: 1_700_000_000_000_000n,
|
||||
});
|
||||
const expectedFx1 = encodeFx1mIlpV2(mutableFx);
|
||||
|
||||
// Submit first bar and FX row without awaiting, then mutate both objects in place.
|
||||
const pBar1 = writer.writeBar1m(mutableBar);
|
||||
const pFx1 = writer.writeFx1m(mutableFx);
|
||||
|
||||
mutableBar.listing_id = 11;
|
||||
mutableBar.ts = 1_700_000_060_000_000n;
|
||||
mutableBar.known_at = 1_700_000_120_000_000n;
|
||||
mutableBar.open = 200.0;
|
||||
mutableBar.high = 202.0;
|
||||
mutableBar.low = 199.0;
|
||||
mutableBar.close = 201.5;
|
||||
mutableBar.volume = 2_000;
|
||||
mutableBar.session = 'RTH';
|
||||
mutableBar.source = 'bar_two';
|
||||
const expectedBar2 = encodeBar1mIlpV2(mutableBar);
|
||||
|
||||
mutableFx.pair = 'EUR.USD';
|
||||
mutableFx.source = 'fx_two';
|
||||
mutableFx.open = 1.08;
|
||||
mutableFx.high = 1.09;
|
||||
mutableFx.low = 1.07;
|
||||
mutableFx.close = 1.085;
|
||||
mutableFx.ts = 1_700_000_060_000_000n;
|
||||
const expectedFx2 = encodeFx1mIlpV2(mutableFx);
|
||||
|
||||
const pBar2 = writer.writeBar1m(mutableBar);
|
||||
const pFx2 = writer.writeFx1m(mutableFx);
|
||||
|
||||
await Promise.all([pBar1, pFx1, pBar2, pFx2]);
|
||||
const stats = await writer.finish();
|
||||
|
||||
expect(stats).toEqual({
|
||||
rows: 4,
|
||||
batches: 4,
|
||||
bytes:
|
||||
expectedBar1.byteLength +
|
||||
expectedFx1.byteLength +
|
||||
expectedBar2.byteLength +
|
||||
expectedFx2.byteLength,
|
||||
});
|
||||
expect(batches).toEqual([expectedBar1, expectedFx1, expectedBar2, expectedFx2]);
|
||||
});
|
||||
|
||||
it('propagates sink rejection and fails closed for queued and subsequent calls with zero extra sink writes', async () => {
|
||||
const boom = new Error('simulated sink EPIPE');
|
||||
let sinkCalls = 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue