ticlo-tsdb 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +373 -0
- package/README.md +64 -2
- package/dist/backend.d.ts +41 -0
- package/dist/backend.js +207 -0
- package/dist/db.d.ts +56 -0
- package/dist/db.js +490 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +4 -0
- package/dist/memoryCache.d.ts +25 -0
- package/dist/memoryCache.js +89 -0
- package/dist/rawFile.d.ts +87 -0
- package/dist/rawFile.js +1031 -0
- package/dist/retention.d.ts +20 -0
- package/dist/retention.js +101 -0
- package/dist/rollupFile.d.ts +62 -0
- package/dist/rollupFile.js +527 -0
- package/dist/series.d.ts +74 -0
- package/dist/series.js +1223 -0
- package/dist/store.d.ts +80 -0
- package/dist/store.js +42 -0
- package/dist/time.d.ts +39 -0
- package/dist/time.js +459 -0
- package/dist/types.d.ts +162 -0
- package/dist/types.js +1 -0
- package/package.json +36 -4
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
export const ROLLUP_HEADER_BYTES = 64;
|
|
2
|
+
export const ROLLUP_SLOT_FLOATS = 7;
|
|
3
|
+
export const ROLLUP_SLOT_BYTES = ROLLUP_SLOT_FLOATS * 8; // 56 bytes: count, sum, min, max, initial, first, final
|
|
4
|
+
export const SLOT_COUNT = 0;
|
|
5
|
+
export const SLOT_SUM = 1;
|
|
6
|
+
export const SLOT_MIN = 2;
|
|
7
|
+
export const SLOT_MAX = 3;
|
|
8
|
+
export const SLOT_INITIAL = 4;
|
|
9
|
+
export const SLOT_FIRST = 5;
|
|
10
|
+
export const SLOT_FINAL = 6;
|
|
11
|
+
const MAGIC = 0x504c5254; // 'TRLP' little-endian
|
|
12
|
+
const VERSION = 1;
|
|
13
|
+
const TIER_CODE = {
|
|
14
|
+
'15s': 1,
|
|
15
|
+
'5m': 2,
|
|
16
|
+
'1h': 3,
|
|
17
|
+
'1d': 4,
|
|
18
|
+
};
|
|
19
|
+
const IS_LITTLE_ENDIAN = (() => {
|
|
20
|
+
const buf = new ArrayBuffer(4);
|
|
21
|
+
new Uint32Array(buf)[0] = 0x01020304;
|
|
22
|
+
return new Uint8Array(buf)[0] === 0x04;
|
|
23
|
+
})();
|
|
24
|
+
export function makeGapSlotsBuffer(numSlots) {
|
|
25
|
+
const buf = new ArrayBuffer(numSlots * ROLLUP_SLOT_BYTES);
|
|
26
|
+
const f64 = new Float64Array(buf);
|
|
27
|
+
f64.fill(Number.NaN);
|
|
28
|
+
for (let i = 0; i < numSlots; ++i) {
|
|
29
|
+
f64[i * ROLLUP_SLOT_FLOATS + SLOT_COUNT] = 0;
|
|
30
|
+
}
|
|
31
|
+
return f64;
|
|
32
|
+
}
|
|
33
|
+
export class RollupFile {
|
|
34
|
+
tier;
|
|
35
|
+
relPrefix;
|
|
36
|
+
backend;
|
|
37
|
+
slotsPerChunk;
|
|
38
|
+
chunks = [];
|
|
39
|
+
nextChunkId = 1;
|
|
40
|
+
activeHandle = null;
|
|
41
|
+
activeChunkId = -1;
|
|
42
|
+
activeDirtyForManifest = false;
|
|
43
|
+
_byteSize = 0;
|
|
44
|
+
opened = false;
|
|
45
|
+
headerBuf = new ArrayBuffer(ROLLUP_HEADER_BYTES);
|
|
46
|
+
headerU8 = new Uint8Array(this.headerBuf);
|
|
47
|
+
headerView = new DataView(this.headerBuf);
|
|
48
|
+
constructor(backend, relPrefix, tier, slotsPerChunk = 512) {
|
|
49
|
+
this.backend = backend;
|
|
50
|
+
this.relPrefix = relPrefix;
|
|
51
|
+
this.tier = tier;
|
|
52
|
+
this.slotsPerChunk = Math.max(1, Math.floor(slotsPerChunk));
|
|
53
|
+
}
|
|
54
|
+
get hasData() {
|
|
55
|
+
return this.chunks.length > 0;
|
|
56
|
+
}
|
|
57
|
+
get baseSlotId() {
|
|
58
|
+
return this.chunks.length > 0 ? this.chunks[0].baseSlotId : 0;
|
|
59
|
+
}
|
|
60
|
+
get endSlotId() {
|
|
61
|
+
const last = this.chunks[this.chunks.length - 1];
|
|
62
|
+
return last ? last.baseSlotId + last.slotCount : 0;
|
|
63
|
+
}
|
|
64
|
+
get slotCount() {
|
|
65
|
+
return this.chunks.length > 0 ? this.endSlotId - this.baseSlotId : 0;
|
|
66
|
+
}
|
|
67
|
+
get byteSize() {
|
|
68
|
+
return this._byteSize;
|
|
69
|
+
}
|
|
70
|
+
get manifestPath() {
|
|
71
|
+
return `${this.relPrefix}.manifest.json`;
|
|
72
|
+
}
|
|
73
|
+
chunkPath(id) {
|
|
74
|
+
return `${this.relPrefix}_${id}.bin`;
|
|
75
|
+
}
|
|
76
|
+
chunkBucket(slotId) {
|
|
77
|
+
return Math.floor(slotId / this.slotsPerChunk);
|
|
78
|
+
}
|
|
79
|
+
async open() {
|
|
80
|
+
if (this.opened)
|
|
81
|
+
return;
|
|
82
|
+
const manifest = await this.backend.readJson(this.manifestPath);
|
|
83
|
+
if (manifest) {
|
|
84
|
+
if (manifest.version !== VERSION || manifest.tier !== this.tier) {
|
|
85
|
+
throw new Error(`Incompatible rollup manifest: ${this.manifestPath}`);
|
|
86
|
+
}
|
|
87
|
+
if (manifest.chunks.length > 0) {
|
|
88
|
+
this.slotsPerChunk = Math.max(1, manifest.slotsPerChunk);
|
|
89
|
+
}
|
|
90
|
+
this.nextChunkId = Math.max(1, manifest.nextChunkId);
|
|
91
|
+
this.chunks = manifest.chunks.map((c) => ({ ...c }));
|
|
92
|
+
this.chunks.sort((a, b) => a.baseSlotId - b.baseSlotId);
|
|
93
|
+
// Reconcile only the active (last) chunk from disk for crash safety.
|
|
94
|
+
const last = this.chunks[this.chunks.length - 1];
|
|
95
|
+
if (last) {
|
|
96
|
+
const handle = await this.backend.open(this.chunkPath(last.id));
|
|
97
|
+
const fileSize = await handle.size();
|
|
98
|
+
if (fileSize >= ROLLUP_HEADER_BYTES) {
|
|
99
|
+
const hdr = await this.readChunkHeader(handle, this.chunkPath(last.id));
|
|
100
|
+
const maxSlotsFromSize = Math.floor((fileSize - ROLLUP_HEADER_BYTES) / ROLLUP_SLOT_BYTES);
|
|
101
|
+
const reconciledCount = Math.min(hdr.slotCount, maxSlotsFromSize);
|
|
102
|
+
last.baseSlotId = hdr.baseSlotId;
|
|
103
|
+
last.slotCount = reconciledCount;
|
|
104
|
+
last.byteSize = ROLLUP_HEADER_BYTES + reconciledCount * ROLLUP_SLOT_BYTES;
|
|
105
|
+
this.activeHandle = handle;
|
|
106
|
+
this.activeChunkId = last.id;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
await handle.close();
|
|
110
|
+
await this.backend.remove(this.chunkPath(last.id));
|
|
111
|
+
this.chunks.pop();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
let totalBytes = 0;
|
|
116
|
+
for (let i = 0; i < this.chunks.length; ++i) {
|
|
117
|
+
totalBytes += this.chunks[i].byteSize;
|
|
118
|
+
}
|
|
119
|
+
this._byteSize = totalBytes;
|
|
120
|
+
this.activeDirtyForManifest = false;
|
|
121
|
+
this.opened = true;
|
|
122
|
+
}
|
|
123
|
+
async writeManifest() {
|
|
124
|
+
const data = {
|
|
125
|
+
version: VERSION,
|
|
126
|
+
tier: this.tier,
|
|
127
|
+
slotsPerChunk: this.slotsPerChunk,
|
|
128
|
+
nextChunkId: this.nextChunkId,
|
|
129
|
+
chunks: this.chunks,
|
|
130
|
+
};
|
|
131
|
+
await this.backend.writeJsonAtomic(this.manifestPath, data);
|
|
132
|
+
this.activeDirtyForManifest = false;
|
|
133
|
+
}
|
|
134
|
+
async readChunkHeader(handle, relPath) {
|
|
135
|
+
const readBytes = await handle.read(this.headerU8, 0, ROLLUP_HEADER_BYTES, 0);
|
|
136
|
+
if (readBytes < ROLLUP_HEADER_BYTES || this.headerView.getUint32(0, true) !== MAGIC) {
|
|
137
|
+
throw new Error(`Corrupt rollup file header: ${relPath}`);
|
|
138
|
+
}
|
|
139
|
+
const version = this.headerView.getUint16(4, true);
|
|
140
|
+
const tierCode = this.headerView.getUint16(6, true);
|
|
141
|
+
if (version !== VERSION || tierCode !== TIER_CODE[this.tier]) {
|
|
142
|
+
throw new Error(`Incompatible rollup file header: ${relPath}`);
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
baseSlotId: this.headerView.getFloat64(16, true),
|
|
146
|
+
slotCount: this.headerView.getFloat64(24, true),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
async writeChunkHeader(handle, baseSlotId, slotCount) {
|
|
150
|
+
this.headerU8.fill(0);
|
|
151
|
+
this.headerView.setUint32(0, MAGIC, true);
|
|
152
|
+
this.headerView.setUint16(4, VERSION, true);
|
|
153
|
+
this.headerView.setUint16(6, TIER_CODE[this.tier], true);
|
|
154
|
+
this.headerView.setUint32(8, ROLLUP_SLOT_BYTES, true);
|
|
155
|
+
this.headerView.setFloat64(16, baseSlotId, true);
|
|
156
|
+
this.headerView.setFloat64(24, slotCount, true);
|
|
157
|
+
await handle.write(this.headerU8, 0, ROLLUP_HEADER_BYTES, 0);
|
|
158
|
+
}
|
|
159
|
+
async ensureActiveHandle(chunkId) {
|
|
160
|
+
if (this.activeHandle && this.activeChunkId === chunkId) {
|
|
161
|
+
return this.activeHandle;
|
|
162
|
+
}
|
|
163
|
+
if (this.activeHandle) {
|
|
164
|
+
await this.activeHandle.close();
|
|
165
|
+
this.activeHandle = null;
|
|
166
|
+
this.activeChunkId = -1;
|
|
167
|
+
}
|
|
168
|
+
this.activeHandle = await this.backend.open(this.chunkPath(chunkId));
|
|
169
|
+
this.activeChunkId = chunkId;
|
|
170
|
+
return this.activeHandle;
|
|
171
|
+
}
|
|
172
|
+
findChunkForBucket(bucketIdx) {
|
|
173
|
+
// Fast path: check active/last chunk first.
|
|
174
|
+
const lastIdx = this.chunks.length - 1;
|
|
175
|
+
if (lastIdx >= 0 && this.chunkBucket(this.chunks[lastIdx].baseSlotId) === bucketIdx) {
|
|
176
|
+
return lastIdx;
|
|
177
|
+
}
|
|
178
|
+
let lo = 0;
|
|
179
|
+
let hi = lastIdx;
|
|
180
|
+
while (lo <= hi) {
|
|
181
|
+
const mid = (lo + hi) >>> 1;
|
|
182
|
+
const mb = this.chunkBucket(this.chunks[mid].baseSlotId);
|
|
183
|
+
if (mb === bucketIdx)
|
|
184
|
+
return mid;
|
|
185
|
+
if (mb < bucketIdx)
|
|
186
|
+
lo = mid + 1;
|
|
187
|
+
else
|
|
188
|
+
hi = mid - 1;
|
|
189
|
+
}
|
|
190
|
+
return -1;
|
|
191
|
+
}
|
|
192
|
+
async readLastSlot() {
|
|
193
|
+
if (!this.opened)
|
|
194
|
+
await this.open();
|
|
195
|
+
if (!this.hasData)
|
|
196
|
+
return null;
|
|
197
|
+
const last = this.chunks[this.chunks.length - 1];
|
|
198
|
+
if (last.slotCount <= 0)
|
|
199
|
+
return null;
|
|
200
|
+
const lastSlotId = last.baseSlotId + last.slotCount - 1;
|
|
201
|
+
const res = await this.readSlotRange(lastSlotId, lastSlotId);
|
|
202
|
+
if (res.count === 0)
|
|
203
|
+
return null;
|
|
204
|
+
return { slotId: lastSlotId, data: res.data };
|
|
205
|
+
}
|
|
206
|
+
async writeSlots(startSlotId, data, numSlots) {
|
|
207
|
+
if (numSlots <= 0)
|
|
208
|
+
return;
|
|
209
|
+
if (!this.opened)
|
|
210
|
+
await this.open();
|
|
211
|
+
let manifestDirty = false;
|
|
212
|
+
let cursor = startSlotId;
|
|
213
|
+
const end = startSlotId + numSlots;
|
|
214
|
+
let srcSlotOffset = 0;
|
|
215
|
+
while (cursor < end) {
|
|
216
|
+
const bucketIdx = this.chunkBucket(cursor);
|
|
217
|
+
const bucketEnd = (bucketIdx + 1) * this.slotsPerChunk;
|
|
218
|
+
const writeEnd = Math.min(end, bucketEnd);
|
|
219
|
+
const count = writeEnd - cursor;
|
|
220
|
+
const sliceData = srcSlotOffset === 0 && count === numSlots
|
|
221
|
+
? data
|
|
222
|
+
: data.subarray(srcSlotOffset * ROLLUP_SLOT_FLOATS, (srcSlotOffset + count) * ROLLUP_SLOT_FLOATS);
|
|
223
|
+
const chunkIdx = this.findChunkForBucket(bucketIdx);
|
|
224
|
+
if (chunkIdx >= 0) {
|
|
225
|
+
const chunk = this.chunks[chunkIdx];
|
|
226
|
+
const isLast = chunkIdx === this.chunks.length - 1;
|
|
227
|
+
const handle = isLast
|
|
228
|
+
? await this.ensureActiveHandle(chunk.id)
|
|
229
|
+
: await this.backend.open(this.chunkPath(chunk.id));
|
|
230
|
+
try {
|
|
231
|
+
const grew = await this.writeIntoChunk(handle, chunk, cursor, sliceData, count);
|
|
232
|
+
if (grew) {
|
|
233
|
+
if (isLast) {
|
|
234
|
+
this.activeDirtyForManifest = true;
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
manifestDirty = true;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
finally {
|
|
242
|
+
if (!isLast) {
|
|
243
|
+
await handle.close();
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
// Create a new chunk aligned to this bucket boundary.
|
|
249
|
+
const isNewLast = this.chunks.length === 0 || bucketIdx > this.chunkBucket(this.chunks[this.chunks.length - 1].baseSlotId);
|
|
250
|
+
if (isNewLast && this.activeHandle) {
|
|
251
|
+
await this.activeHandle.close();
|
|
252
|
+
this.activeHandle = null;
|
|
253
|
+
this.activeChunkId = -1;
|
|
254
|
+
}
|
|
255
|
+
const newId = this.nextChunkId++;
|
|
256
|
+
const handle = await this.backend.open(this.chunkPath(newId));
|
|
257
|
+
try {
|
|
258
|
+
const byteSize = ROLLUP_HEADER_BYTES + count * ROLLUP_SLOT_BYTES;
|
|
259
|
+
const chunk = {
|
|
260
|
+
id: newId,
|
|
261
|
+
baseSlotId: cursor,
|
|
262
|
+
slotCount: count,
|
|
263
|
+
byteSize,
|
|
264
|
+
};
|
|
265
|
+
await this.writeAlignedFloats(handle, ROLLUP_HEADER_BYTES, sliceData, count);
|
|
266
|
+
await this.writeChunkHeader(handle, chunk.baseSlotId, chunk.slotCount);
|
|
267
|
+
if (isNewLast) {
|
|
268
|
+
this.chunks.push(chunk);
|
|
269
|
+
this.activeHandle = handle;
|
|
270
|
+
this.activeChunkId = newId;
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
// Historical bucket insert: place in sorted order and close handle.
|
|
274
|
+
let insertAt = this.chunks.length;
|
|
275
|
+
for (let i = 0; i < this.chunks.length; ++i) {
|
|
276
|
+
if (this.chunks[i].baseSlotId > chunk.baseSlotId) {
|
|
277
|
+
insertAt = i;
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
this.chunks.splice(insertAt, 0, chunk);
|
|
282
|
+
}
|
|
283
|
+
this._byteSize += byteSize;
|
|
284
|
+
manifestDirty = true;
|
|
285
|
+
}
|
|
286
|
+
finally {
|
|
287
|
+
if (!isNewLast) {
|
|
288
|
+
await handle.close();
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
cursor = writeEnd;
|
|
293
|
+
srcSlotOffset += count;
|
|
294
|
+
}
|
|
295
|
+
if (manifestDirty) {
|
|
296
|
+
await this.writeManifest();
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
async writeIntoChunk(handle, chunk, startSlotId, data, numSlots) {
|
|
300
|
+
const oldEnd = chunk.baseSlotId + chunk.slotCount;
|
|
301
|
+
let headerChanged = false;
|
|
302
|
+
if (startSlotId < chunk.baseSlotId) {
|
|
303
|
+
const prependCount = chunk.baseSlotId - startSlotId;
|
|
304
|
+
const existingBytes = chunk.slotCount * ROLLUP_SLOT_BYTES;
|
|
305
|
+
if (existingBytes > 0) {
|
|
306
|
+
const existingBuf = new Uint8Array(new ArrayBuffer(existingBytes));
|
|
307
|
+
await handle.read(existingBuf, 0, existingBytes, ROLLUP_HEADER_BYTES);
|
|
308
|
+
await handle.write(existingBuf, 0, existingBytes, ROLLUP_HEADER_BYTES + prependCount * ROLLUP_SLOT_BYTES);
|
|
309
|
+
}
|
|
310
|
+
if (prependCount > numSlots) {
|
|
311
|
+
const gap = makeGapSlotsBuffer(prependCount - numSlots);
|
|
312
|
+
await this.writeAlignedFloats(handle, ROLLUP_HEADER_BYTES + numSlots * ROLLUP_SLOT_BYTES, gap, prependCount - numSlots);
|
|
313
|
+
}
|
|
314
|
+
chunk.baseSlotId = startSlotId;
|
|
315
|
+
chunk.slotCount += prependCount;
|
|
316
|
+
headerChanged = true;
|
|
317
|
+
}
|
|
318
|
+
else if (startSlotId > oldEnd) {
|
|
319
|
+
const gapCount = startSlotId - oldEnd;
|
|
320
|
+
const batchSlots = Math.min(gapCount, 512);
|
|
321
|
+
const gapChunk = makeGapSlotsBuffer(batchSlots);
|
|
322
|
+
let cursor = oldEnd;
|
|
323
|
+
let remaining = gapCount;
|
|
324
|
+
while (remaining > 0) {
|
|
325
|
+
const batch = Math.min(remaining, batchSlots);
|
|
326
|
+
const offset = ROLLUP_HEADER_BYTES + (cursor - chunk.baseSlotId) * ROLLUP_SLOT_BYTES;
|
|
327
|
+
await this.writeAlignedFloats(handle, offset, gapChunk, batch);
|
|
328
|
+
cursor += batch;
|
|
329
|
+
remaining -= batch;
|
|
330
|
+
}
|
|
331
|
+
chunk.slotCount = startSlotId - chunk.baseSlotId;
|
|
332
|
+
headerChanged = true;
|
|
333
|
+
}
|
|
334
|
+
const writeOffset = ROLLUP_HEADER_BYTES + (startSlotId - chunk.baseSlotId) * ROLLUP_SLOT_BYTES;
|
|
335
|
+
await this.writeAlignedFloats(handle, writeOffset, data, numSlots);
|
|
336
|
+
const newEnd = startSlotId + numSlots;
|
|
337
|
+
if (newEnd > chunk.baseSlotId + chunk.slotCount) {
|
|
338
|
+
chunk.slotCount = newEnd - chunk.baseSlotId;
|
|
339
|
+
headerChanged = true;
|
|
340
|
+
}
|
|
341
|
+
if (headerChanged) {
|
|
342
|
+
const newByteSize = ROLLUP_HEADER_BYTES + chunk.slotCount * ROLLUP_SLOT_BYTES;
|
|
343
|
+
this._byteSize += newByteSize - chunk.byteSize;
|
|
344
|
+
chunk.byteSize = newByteSize;
|
|
345
|
+
await this.writeChunkHeader(handle, chunk.baseSlotId, chunk.slotCount);
|
|
346
|
+
}
|
|
347
|
+
return headerChanged;
|
|
348
|
+
}
|
|
349
|
+
async writeAlignedFloats(handle, position, data, numSlots) {
|
|
350
|
+
const byteLen = numSlots * ROLLUP_SLOT_BYTES;
|
|
351
|
+
if (IS_LITTLE_ENDIAN) {
|
|
352
|
+
const bytes = new Uint8Array(data.buffer, data.byteOffset, byteLen);
|
|
353
|
+
await handle.write(bytes, 0, byteLen, position);
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
const scratch = new ArrayBuffer(byteLen);
|
|
357
|
+
const dv = new DataView(scratch);
|
|
358
|
+
const floats = numSlots * ROLLUP_SLOT_FLOATS;
|
|
359
|
+
for (let i = 0; i < floats; ++i) {
|
|
360
|
+
dv.setFloat64(i * 8, data[i], true);
|
|
361
|
+
}
|
|
362
|
+
await handle.write(new Uint8Array(scratch), 0, byteLen, position);
|
|
363
|
+
}
|
|
364
|
+
async readChunkSlice(handle, chunk, fromSlotId, count, out, outSlotOffset) {
|
|
365
|
+
const byteLen = count * ROLLUP_SLOT_BYTES;
|
|
366
|
+
const offset = ROLLUP_HEADER_BYTES + (fromSlotId - chunk.baseSlotId) * ROLLUP_SLOT_BYTES;
|
|
367
|
+
if (IS_LITTLE_ENDIAN) {
|
|
368
|
+
const bytes = new Uint8Array(out.buffer, out.byteOffset + outSlotOffset * ROLLUP_SLOT_BYTES, byteLen);
|
|
369
|
+
await handle.read(bytes, 0, byteLen, offset);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
const buf = new ArrayBuffer(byteLen);
|
|
373
|
+
const bytesRead = await handle.read(new Uint8Array(buf), 0, byteLen, offset);
|
|
374
|
+
const actualCount = Math.floor(bytesRead / ROLLUP_SLOT_BYTES);
|
|
375
|
+
const dv = new DataView(buf);
|
|
376
|
+
const floats = actualCount * ROLLUP_SLOT_FLOATS;
|
|
377
|
+
const baseFloat = outSlotOffset * ROLLUP_SLOT_FLOATS;
|
|
378
|
+
for (let i = 0; i < floats; ++i) {
|
|
379
|
+
out[baseFloat + i] = dv.getFloat64(i * 8, true);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
async readSlotRange(fromSlotId, toSlotIdInclusive) {
|
|
383
|
+
if (!this.opened)
|
|
384
|
+
await this.open();
|
|
385
|
+
if (!this.hasData || toSlotIdInclusive < this.baseSlotId || fromSlotId >= this.endSlotId) {
|
|
386
|
+
return { firstSlotId: 0, data: new Float64Array(0), count: 0 };
|
|
387
|
+
}
|
|
388
|
+
const clampedFrom = Math.max(fromSlotId, this.baseSlotId);
|
|
389
|
+
const clampedTo = Math.min(toSlotIdInclusive, this.endSlotId - 1);
|
|
390
|
+
const totalCount = clampedTo - clampedFrom + 1;
|
|
391
|
+
if (totalCount <= 0) {
|
|
392
|
+
return { firstSlotId: 0, data: new Float64Array(0), count: 0 };
|
|
393
|
+
}
|
|
394
|
+
const out = makeGapSlotsBuffer(totalCount);
|
|
395
|
+
// Binary search first chunk with chunkEnd > clampedFrom.
|
|
396
|
+
let lo = 0;
|
|
397
|
+
let hi = this.chunks.length - 1;
|
|
398
|
+
let startIdx = this.chunks.length;
|
|
399
|
+
while (lo <= hi) {
|
|
400
|
+
const mid = (lo + hi) >>> 1;
|
|
401
|
+
const c = this.chunks[mid];
|
|
402
|
+
if (c.baseSlotId + c.slotCount > clampedFrom) {
|
|
403
|
+
startIdx = mid;
|
|
404
|
+
hi = mid - 1;
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
lo = mid + 1;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
for (let i = startIdx; i < this.chunks.length; ++i) {
|
|
411
|
+
const chunk = this.chunks[i];
|
|
412
|
+
if (chunk.baseSlotId > clampedTo)
|
|
413
|
+
break;
|
|
414
|
+
const cFrom = Math.max(clampedFrom, chunk.baseSlotId);
|
|
415
|
+
const cTo = Math.min(clampedTo, chunk.baseSlotId + chunk.slotCount - 1);
|
|
416
|
+
const cCount = cTo - cFrom + 1;
|
|
417
|
+
if (cCount <= 0)
|
|
418
|
+
continue;
|
|
419
|
+
const isActive = chunk.id === this.activeChunkId && this.activeHandle !== null;
|
|
420
|
+
const handle = isActive ? this.activeHandle : await this.backend.open(this.chunkPath(chunk.id));
|
|
421
|
+
try {
|
|
422
|
+
await this.readChunkSlice(handle, chunk, cFrom, cCount, out, cFrom - clampedFrom);
|
|
423
|
+
}
|
|
424
|
+
finally {
|
|
425
|
+
if (!isActive) {
|
|
426
|
+
await handle.close();
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return { firstSlotId: clampedFrom, data: out, count: totalCount };
|
|
431
|
+
}
|
|
432
|
+
listChunks() {
|
|
433
|
+
return this.chunks.map((c) => ({
|
|
434
|
+
id: c.id,
|
|
435
|
+
minKey: c.baseSlotId,
|
|
436
|
+
maxKey: c.baseSlotId + c.slotCount - 1,
|
|
437
|
+
byteSize: c.byteSize,
|
|
438
|
+
count: c.slotCount,
|
|
439
|
+
}));
|
|
440
|
+
}
|
|
441
|
+
async dropChunks(chunkIds) {
|
|
442
|
+
if (!this.opened)
|
|
443
|
+
await this.open();
|
|
444
|
+
if (chunkIds.length === 0 || this.chunks.length === 0)
|
|
445
|
+
return;
|
|
446
|
+
const dropSet = new Set(chunkIds);
|
|
447
|
+
const remaining = [];
|
|
448
|
+
let droppedAny = false;
|
|
449
|
+
for (let i = 0; i < this.chunks.length; ++i) {
|
|
450
|
+
const c = this.chunks[i];
|
|
451
|
+
if (dropSet.has(c.id)) {
|
|
452
|
+
droppedAny = true;
|
|
453
|
+
if (c.id === this.activeChunkId && this.activeHandle) {
|
|
454
|
+
await this.activeHandle.close();
|
|
455
|
+
this.activeHandle = null;
|
|
456
|
+
this.activeChunkId = -1;
|
|
457
|
+
}
|
|
458
|
+
this._byteSize -= c.byteSize;
|
|
459
|
+
await this.backend.remove(this.chunkPath(c.id));
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
remaining.push(c);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (!droppedAny)
|
|
466
|
+
return;
|
|
467
|
+
this.chunks = remaining;
|
|
468
|
+
await this.writeManifest();
|
|
469
|
+
}
|
|
470
|
+
async compact(keepFromSlotId) {
|
|
471
|
+
if (!this.opened)
|
|
472
|
+
await this.open();
|
|
473
|
+
if (!this.hasData || keepFromSlotId <= this.baseSlotId)
|
|
474
|
+
return;
|
|
475
|
+
const wholeDrops = [];
|
|
476
|
+
for (let i = 0; i < this.chunks.length; ++i) {
|
|
477
|
+
const c = this.chunks[i];
|
|
478
|
+
if (c.baseSlotId + c.slotCount <= keepFromSlotId) {
|
|
479
|
+
wholeDrops.push(c.id);
|
|
480
|
+
}
|
|
481
|
+
else {
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
if (wholeDrops.length > 0) {
|
|
486
|
+
await this.dropChunks(wholeDrops);
|
|
487
|
+
}
|
|
488
|
+
if (this.chunks.length === 0)
|
|
489
|
+
return;
|
|
490
|
+
const head = this.chunks[0];
|
|
491
|
+
if (head.baseSlotId >= keepFromSlotId)
|
|
492
|
+
return;
|
|
493
|
+
const dropCount = keepFromSlotId - head.baseSlotId;
|
|
494
|
+
const keepCount = head.slotCount - dropCount;
|
|
495
|
+
const keepBytes = keepCount * ROLLUP_SLOT_BYTES;
|
|
496
|
+
const isActive = head.id === this.activeChunkId && this.activeHandle !== null;
|
|
497
|
+
const handle = isActive ? this.activeHandle : await this.backend.open(this.chunkPath(head.id));
|
|
498
|
+
try {
|
|
499
|
+
const buf = new Uint8Array(new ArrayBuffer(keepBytes));
|
|
500
|
+
await handle.read(buf, 0, keepBytes, ROLLUP_HEADER_BYTES + dropCount * ROLLUP_SLOT_BYTES);
|
|
501
|
+
await handle.write(buf, 0, keepBytes, ROLLUP_HEADER_BYTES);
|
|
502
|
+
head.baseSlotId = keepFromSlotId;
|
|
503
|
+
head.slotCount = keepCount;
|
|
504
|
+
const newByteSize = ROLLUP_HEADER_BYTES + keepBytes;
|
|
505
|
+
this._byteSize += newByteSize - head.byteSize;
|
|
506
|
+
head.byteSize = newByteSize;
|
|
507
|
+
await this.writeChunkHeader(handle, head.baseSlotId, head.slotCount);
|
|
508
|
+
await handle.truncate(newByteSize);
|
|
509
|
+
}
|
|
510
|
+
finally {
|
|
511
|
+
if (!isActive) {
|
|
512
|
+
await handle.close();
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
await this.writeManifest();
|
|
516
|
+
}
|
|
517
|
+
async close() {
|
|
518
|
+
if (this.activeDirtyForManifest) {
|
|
519
|
+
await this.writeManifest();
|
|
520
|
+
}
|
|
521
|
+
if (this.activeHandle) {
|
|
522
|
+
await this.activeHandle.close();
|
|
523
|
+
this.activeHandle = null;
|
|
524
|
+
this.activeChunkId = -1;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
package/dist/series.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { TsdbStore } from './store.ts';
|
|
2
|
+
import { ServerCalendar } from './time.ts';
|
|
3
|
+
import type { NormalizedSeriesConfig, NumericColumnResult, NumericQueryOptions, NumericQueryResult, RollupTier, SeriesConfig, SeriesMeta, StorageChunkInfo, ValueCodec, ValueQueryOptions, ValueQueryResult } from './types.ts';
|
|
4
|
+
export declare function normalizeSeriesConfig(config: SeriesConfig, defaults?: Partial<Omit<SeriesConfig, 'kind'>>): NormalizedSeriesConfig;
|
|
5
|
+
export declare class NumericSeries {
|
|
6
|
+
readonly meta: SeriesMeta;
|
|
7
|
+
private readonly store;
|
|
8
|
+
private readonly calendar;
|
|
9
|
+
private rawFile;
|
|
10
|
+
private readonly tiers;
|
|
11
|
+
private readonly activeTiers;
|
|
12
|
+
private readonly memoryCache;
|
|
13
|
+
private pendingRaw;
|
|
14
|
+
private pendingRawCount;
|
|
15
|
+
private continuous;
|
|
16
|
+
private lastTs;
|
|
17
|
+
private lastValue;
|
|
18
|
+
private flushChain;
|
|
19
|
+
private dirty;
|
|
20
|
+
private closed;
|
|
21
|
+
private readonly listeners;
|
|
22
|
+
constructor(meta: SeriesMeta, store: TsdbStore, calendar: ServerCalendar);
|
|
23
|
+
get hasDirtyData(): boolean;
|
|
24
|
+
getRawByteSize(): number;
|
|
25
|
+
getRollupByteSize(tier: RollupTier): number;
|
|
26
|
+
getActiveRollupTiers(): RollupTier[];
|
|
27
|
+
listRawChunks(): ReadonlyArray<StorageChunkInfo>;
|
|
28
|
+
listRollupChunks(tier: RollupTier): ReadonlyArray<StorageChunkInfo>;
|
|
29
|
+
dropRawChunks(chunkIds: ReadonlyArray<number>): Promise<void>;
|
|
30
|
+
dropRollupChunks(tier: RollupTier, chunkIds: ReadonlyArray<number>): Promise<void>;
|
|
31
|
+
open(): Promise<void>;
|
|
32
|
+
subscribe(listener: (ts: number, value: number | boolean) => void): () => void;
|
|
33
|
+
write(tsMs: number, input: number | boolean): void;
|
|
34
|
+
markInterruption(tsMs?: number): void;
|
|
35
|
+
flush(): Promise<void>;
|
|
36
|
+
private flushInternal;
|
|
37
|
+
private getTierMinSlotId;
|
|
38
|
+
private getTierMaxSlotId;
|
|
39
|
+
private readTierSlots;
|
|
40
|
+
private selectFallbackTier;
|
|
41
|
+
private getFinestEnabledTier;
|
|
42
|
+
private selectServingTier;
|
|
43
|
+
private readMergedRaw;
|
|
44
|
+
private aggregateRawIntoBuckets;
|
|
45
|
+
private queryTierAsBuckets;
|
|
46
|
+
queryColumns(options: NumericQueryOptions): Promise<NumericColumnResult>;
|
|
47
|
+
query(options: NumericQueryOptions): Promise<NumericQueryResult>;
|
|
48
|
+
close(): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
export declare class ValueSeries {
|
|
51
|
+
readonly meta: SeriesMeta;
|
|
52
|
+
private readonly store;
|
|
53
|
+
private readonly calendar;
|
|
54
|
+
private readonly codec;
|
|
55
|
+
private file;
|
|
56
|
+
private pending;
|
|
57
|
+
private lastTs;
|
|
58
|
+
private flushChain;
|
|
59
|
+
private closed;
|
|
60
|
+
private readonly listeners;
|
|
61
|
+
constructor(meta: SeriesMeta, store: TsdbStore, calendar: ServerCalendar, codec: ValueCodec);
|
|
62
|
+
get hasDirtyData(): boolean;
|
|
63
|
+
getByteSize(): number;
|
|
64
|
+
listChunks(): ReadonlyArray<StorageChunkInfo>;
|
|
65
|
+
dropChunks(chunkIds: ReadonlyArray<number>): Promise<void>;
|
|
66
|
+
open(): Promise<void>;
|
|
67
|
+
subscribe(listener: (ts: number, value: unknown) => void): () => void;
|
|
68
|
+
write(tsMs: number, value: unknown): void;
|
|
69
|
+
flush(): Promise<void>;
|
|
70
|
+
private flushInternal;
|
|
71
|
+
private decodeRecords;
|
|
72
|
+
query(options: ValueQueryOptions): Promise<ValueQueryResult>;
|
|
73
|
+
close(): Promise<void>;
|
|
74
|
+
}
|