ticlo-tsdb 0.1.0 → 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/README.md +13 -1
- package/dist/db.d.ts +22 -1
- package/dist/db.js +253 -9
- package/dist/index.d.ts +1 -1
- package/dist/rawFile.d.ts +55 -22
- package/dist/rawFile.js +882 -379
- package/dist/retention.d.ts +20 -0
- package/dist/retention.js +101 -0
- package/dist/rollupFile.d.ts +27 -8
- package/dist/rollupFile.js +406 -109
- package/dist/series.d.ts +12 -1
- package/dist/series.js +93 -5
- package/dist/store.d.ts +13 -2
- package/dist/store.js +7 -4
- package/dist/types.d.ts +35 -0
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@ Zero-dependency, performance-first embedded TypeScript time-series database desi
|
|
|
13
13
|
- **Millisecond precision** inclusive `[start, end]` queries (`...59.999`), `auto` interval selection (`> 64` buckets), and `final` returning next-bucket timestamps
|
|
14
14
|
- **Interruption-safe state**: data gaps reset `initial` to the resumed sample rather than carrying stale `final` values across downtime
|
|
15
15
|
- **Hybrid recent memory ring cache**: optionally serves/stitches recent fine-grained points when disk only retains coarser tiers (`1h`/`1d`)
|
|
16
|
+
- **Global byte size limits**: optional independent limits for numeric raw, non-numeric (`value`) raw, and combined rollups (`storageLimits`), deleting only whole chunks with cross-series time alignment and equal-size tier budgeting
|
|
16
17
|
- **Pluggable `ValueCodec` & `TsdbStore`**: pass an external codec (e.g. `jsonesc`) for non-numeric values while strings are stored unescaped via a per-record flag, or swap in a custom `TsdbStore` implementation
|
|
17
18
|
|
|
18
19
|
## Usage
|
|
@@ -20,7 +21,15 @@ Zero-dependency, performance-first embedded TypeScript time-series database desi
|
|
|
20
21
|
```ts
|
|
21
22
|
import {Tsdb} from 'ticlo-tsdb';
|
|
22
23
|
|
|
23
|
-
const db = await Tsdb.open({
|
|
24
|
+
const db = await Tsdb.open({
|
|
25
|
+
dir: './tsdb-data',
|
|
26
|
+
storageLimits: {
|
|
27
|
+
rawNumericMaxBytes: 512 * 1024 * 1024,
|
|
28
|
+
rawValueMaxBytes: 256 * 1024 * 1024,
|
|
29
|
+
rollupMaxBytes: 512 * 1024 * 1024,
|
|
30
|
+
trimTargetRatio: 0.7,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
24
33
|
|
|
25
34
|
await db.createSeries('sensor.temp', {
|
|
26
35
|
kind: 'number',
|
|
@@ -41,6 +50,9 @@ const res = await db.queryNumericColumns('sensor.temp', {
|
|
|
41
50
|
rollup: 'avg',
|
|
42
51
|
});
|
|
43
52
|
|
|
53
|
+
const usage = db.getStorageUsage();
|
|
54
|
+
await db.enforceStorageLimits();
|
|
55
|
+
|
|
44
56
|
await db.close();
|
|
45
57
|
```
|
|
46
58
|
|
package/dist/db.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ServerCalendar } from './time.ts';
|
|
2
|
-
import type { NumericColumnResult, NumericQueryOptions, NumericQueryResult, SeriesConfig, SeriesMeta, TimestampInput, TsdbOptions, ValueQueryOptions, ValueQueryResult } from './types.ts';
|
|
2
|
+
import type { NumericColumnResult, NumericQueryOptions, NumericQueryResult, SeriesConfig, SeriesMeta, StorageUsageStats, TimestampInput, TsdbOptions, ValueQueryOptions, ValueQueryResult } from './types.ts';
|
|
3
3
|
export declare class Tsdb {
|
|
4
4
|
readonly calendar: ServerCalendar;
|
|
5
5
|
private readonly store;
|
|
@@ -7,12 +7,27 @@ export declare class Tsdb {
|
|
|
7
7
|
private readonly options;
|
|
8
8
|
private readonly numericSeries;
|
|
9
9
|
private readonly valueSeries;
|
|
10
|
+
private readonly accountedNumeric;
|
|
11
|
+
private readonly accountedValue;
|
|
10
12
|
private readonly catalog;
|
|
13
|
+
private readonly trimTargetRatio;
|
|
14
|
+
private rawNumericBytes;
|
|
15
|
+
private rawValueBytes;
|
|
16
|
+
private readonly rollupTierBytes;
|
|
17
|
+
private nextRawNumericThreshold;
|
|
18
|
+
private nextRawValueThreshold;
|
|
19
|
+
private nextRollupThreshold;
|
|
20
|
+
private lastLimitCheckAtMs;
|
|
21
|
+
private enforceChain;
|
|
11
22
|
private flushTimer;
|
|
12
23
|
private closed;
|
|
13
24
|
private constructor();
|
|
14
25
|
static open(options: TsdbOptions): Promise<Tsdb>;
|
|
15
26
|
private loadSeriesInstance;
|
|
27
|
+
private syncNumericSeriesBytes;
|
|
28
|
+
private syncValueSeriesBytes;
|
|
29
|
+
private getTotalRollupBytes;
|
|
30
|
+
getStorageUsage(): StorageUsageStats;
|
|
16
31
|
createSeries(id: string, config: SeriesConfig): Promise<SeriesMeta>;
|
|
17
32
|
ensureSeries(id: string, config: SeriesConfig): Promise<SeriesMeta>;
|
|
18
33
|
hasSeries(id: string): boolean;
|
|
@@ -30,6 +45,12 @@ export declare class Tsdb {
|
|
|
30
45
|
queryNumeric(id: string, options: NumericQueryOptions): Promise<NumericQueryResult>;
|
|
31
46
|
queryValues(id: string, options: ValueQueryOptions): Promise<ValueQueryResult>;
|
|
32
47
|
subscribe(id: string, listener: (ts: number, value: unknown) => void): () => void;
|
|
48
|
+
private flushDirtySeries;
|
|
49
|
+
private needsLimitCheck;
|
|
50
|
+
private maybeEnforceStorageLimits;
|
|
51
|
+
private scheduleEnforce;
|
|
52
|
+
private runEnforceStorageLimits;
|
|
53
|
+
enforceStorageLimits(force?: boolean): Promise<StorageUsageStats>;
|
|
33
54
|
flush(): Promise<void>;
|
|
34
55
|
close(): Promise<void>;
|
|
35
56
|
}
|
package/dist/db.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { NodeFileBackend } from './backend.js';
|
|
2
|
+
import { computeTierTargets, planChunkEviction, resolveChunkSizes } from './retention.js';
|
|
2
3
|
import { normalizeSeriesConfig, NumericSeries, ValueSeries } from './series.js';
|
|
3
4
|
import { FileTsdbStore } from './store.js';
|
|
4
5
|
import { ServerCalendar, toEpochMs } from './time.js';
|
|
6
|
+
const ROLLUP_TIERS = ['15s', '5m', '1h', '1d'];
|
|
5
7
|
const defaultCodec = {
|
|
6
8
|
stringify: JSON.stringify,
|
|
7
9
|
parse: JSON.parse,
|
|
@@ -17,22 +19,52 @@ export class Tsdb {
|
|
|
17
19
|
options;
|
|
18
20
|
numericSeries = new Map();
|
|
19
21
|
valueSeries = new Map();
|
|
22
|
+
accountedNumeric = new Map();
|
|
23
|
+
accountedValue = new Map();
|
|
20
24
|
catalog;
|
|
25
|
+
trimTargetRatio;
|
|
26
|
+
rawNumericBytes = 0;
|
|
27
|
+
rawValueBytes = 0;
|
|
28
|
+
rollupTierBytes = {
|
|
29
|
+
'15s': 0,
|
|
30
|
+
'5m': 0,
|
|
31
|
+
'1h': 0,
|
|
32
|
+
'1d': 0,
|
|
33
|
+
};
|
|
34
|
+
nextRawNumericThreshold;
|
|
35
|
+
nextRawValueThreshold;
|
|
36
|
+
nextRollupThreshold;
|
|
37
|
+
lastLimitCheckAtMs = 0;
|
|
38
|
+
enforceChain = Promise.resolve({
|
|
39
|
+
rawNumericBytes: 0,
|
|
40
|
+
rawValueBytes: 0,
|
|
41
|
+
rollupBytes: 0,
|
|
42
|
+
rollupTierBytes: { '15s': 0, '5m': 0, '1h': 0, '1d': 0 },
|
|
43
|
+
});
|
|
21
44
|
flushTimer = null;
|
|
22
45
|
closed = false;
|
|
23
|
-
constructor(store, options, catalog) {
|
|
46
|
+
constructor(store, options, catalog, trimTargetRatio) {
|
|
24
47
|
this.store = store;
|
|
25
48
|
this.valueCodec = options.valueCodec ?? defaultCodec;
|
|
26
49
|
this.options = options;
|
|
27
50
|
this.catalog = catalog;
|
|
28
51
|
this.calendar = new ServerCalendar(catalog.timezone, catalog.weekStartsOn);
|
|
52
|
+
this.trimTargetRatio = trimTargetRatio;
|
|
53
|
+
this.nextRawNumericThreshold = options.storageLimits?.rawNumericMaxBytes ?? Number.POSITIVE_INFINITY;
|
|
54
|
+
this.nextRawValueThreshold = options.storageLimits?.rawValueMaxBytes ?? Number.POSITIVE_INFINITY;
|
|
55
|
+
this.nextRollupThreshold = options.storageLimits?.rollupMaxBytes ?? Number.POSITIVE_INFINITY;
|
|
29
56
|
}
|
|
30
57
|
static async open(options) {
|
|
58
|
+
const trimTargetRatio = options.storageLimits?.trimTargetRatio ?? 0.7;
|
|
59
|
+
if (!(trimTargetRatio > 0 && trimTargetRatio < 1)) {
|
|
60
|
+
throw new Error(`Invalid storageLimits.trimTargetRatio: ${trimTargetRatio} (must be between 0 and 1 exclusive)`);
|
|
61
|
+
}
|
|
62
|
+
const resolvedChunkSize = resolveChunkSizes(options.chunkSize, options.storageLimits);
|
|
31
63
|
const store = options.store ??
|
|
32
64
|
(options.backend
|
|
33
|
-
? new FileTsdbStore(options.backend)
|
|
65
|
+
? new FileTsdbStore(options.backend, resolvedChunkSize)
|
|
34
66
|
: options.dir
|
|
35
|
-
? new FileTsdbStore(new NodeFileBackend(options.dir))
|
|
67
|
+
? new FileTsdbStore(new NodeFileBackend(options.dir), resolvedChunkSize)
|
|
36
68
|
: null);
|
|
37
69
|
if (!store) {
|
|
38
70
|
throw new Error('Tsdb.open requires `dir`, `backend`, or `store`');
|
|
@@ -50,7 +82,7 @@ export class Tsdb {
|
|
|
50
82
|
};
|
|
51
83
|
await store.writeCatalog(catalog);
|
|
52
84
|
}
|
|
53
|
-
const db = new Tsdb(store, options, catalog);
|
|
85
|
+
const db = new Tsdb(store, options, catalog, trimTargetRatio);
|
|
54
86
|
for (const meta of Object.values(catalog.series)) {
|
|
55
87
|
await db.loadSeriesInstance(meta);
|
|
56
88
|
}
|
|
@@ -68,13 +100,60 @@ export class Tsdb {
|
|
|
68
100
|
const s = new ValueSeries(meta, this.store, this.calendar, this.valueCodec);
|
|
69
101
|
await s.open();
|
|
70
102
|
this.valueSeries.set(meta.id, s);
|
|
103
|
+
this.syncValueSeriesBytes(s);
|
|
71
104
|
return s;
|
|
72
105
|
}
|
|
73
106
|
const s = new NumericSeries(meta, this.store, this.calendar);
|
|
74
107
|
await s.open();
|
|
75
108
|
this.numericSeries.set(meta.id, s);
|
|
109
|
+
this.syncNumericSeriesBytes(s);
|
|
76
110
|
return s;
|
|
77
111
|
}
|
|
112
|
+
syncNumericSeriesBytes(num) {
|
|
113
|
+
let prev = this.accountedNumeric.get(num.meta.id);
|
|
114
|
+
if (!prev) {
|
|
115
|
+
prev = { 'raw': 0, '15s': 0, '5m': 0, '1h': 0, '1d': 0 };
|
|
116
|
+
this.accountedNumeric.set(num.meta.id, prev);
|
|
117
|
+
}
|
|
118
|
+
const curRaw = num.getRawByteSize();
|
|
119
|
+
if (curRaw !== prev.raw) {
|
|
120
|
+
this.rawNumericBytes += curRaw - prev.raw;
|
|
121
|
+
prev.raw = curRaw;
|
|
122
|
+
}
|
|
123
|
+
for (let i = 0; i < ROLLUP_TIERS.length; ++i) {
|
|
124
|
+
const tier = ROLLUP_TIERS[i];
|
|
125
|
+
const curTier = num.getRollupByteSize(tier);
|
|
126
|
+
if (curTier !== prev[tier]) {
|
|
127
|
+
this.rollupTierBytes[tier] += curTier - prev[tier];
|
|
128
|
+
prev[tier] = curTier;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
syncValueSeriesBytes(val) {
|
|
133
|
+
const prev = this.accountedValue.get(val.meta.id) ?? 0;
|
|
134
|
+
const cur = val.getByteSize();
|
|
135
|
+
if (cur !== prev) {
|
|
136
|
+
this.rawValueBytes += cur - prev;
|
|
137
|
+
this.accountedValue.set(val.meta.id, cur);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
getTotalRollupBytes() {
|
|
141
|
+
return (this.rollupTierBytes['15s'] + this.rollupTierBytes['5m'] + this.rollupTierBytes['1h'] + this.rollupTierBytes['1d']);
|
|
142
|
+
}
|
|
143
|
+
getStorageUsage() {
|
|
144
|
+
const rollupTierBytes = {
|
|
145
|
+
'15s': this.rollupTierBytes['15s'],
|
|
146
|
+
'5m': this.rollupTierBytes['5m'],
|
|
147
|
+
'1h': this.rollupTierBytes['1h'],
|
|
148
|
+
'1d': this.rollupTierBytes['1d'],
|
|
149
|
+
};
|
|
150
|
+
return {
|
|
151
|
+
rawNumericBytes: this.rawNumericBytes,
|
|
152
|
+
rawValueBytes: this.rawValueBytes,
|
|
153
|
+
rollupBytes: rollupTierBytes['15s'] + rollupTierBytes['5m'] + rollupTierBytes['1h'] + rollupTierBytes['1d'],
|
|
154
|
+
rollupTierBytes,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
78
157
|
async createSeries(id, config) {
|
|
79
158
|
if (this.closed)
|
|
80
159
|
throw new Error('Tsdb is closed');
|
|
@@ -122,11 +201,23 @@ export class Tsdb {
|
|
|
122
201
|
const num = this.numericSeries.get(id);
|
|
123
202
|
if (num) {
|
|
124
203
|
await num.close();
|
|
204
|
+
const prev = this.accountedNumeric.get(id);
|
|
205
|
+
if (prev) {
|
|
206
|
+
this.rawNumericBytes -= prev.raw;
|
|
207
|
+
for (let i = 0; i < ROLLUP_TIERS.length; ++i) {
|
|
208
|
+
const tier = ROLLUP_TIERS[i];
|
|
209
|
+
this.rollupTierBytes[tier] -= prev[tier];
|
|
210
|
+
}
|
|
211
|
+
this.accountedNumeric.delete(id);
|
|
212
|
+
}
|
|
125
213
|
this.numericSeries.delete(id);
|
|
126
214
|
}
|
|
127
215
|
const val = this.valueSeries.get(id);
|
|
128
216
|
if (val) {
|
|
129
217
|
await val.close();
|
|
218
|
+
const prev = this.accountedValue.get(id) ?? 0;
|
|
219
|
+
this.rawValueBytes -= prev;
|
|
220
|
+
this.accountedValue.delete(id);
|
|
130
221
|
this.valueSeries.delete(id);
|
|
131
222
|
}
|
|
132
223
|
delete this.catalog.series[id];
|
|
@@ -213,20 +304,169 @@ export class Tsdb {
|
|
|
213
304
|
return val.subscribe(listener);
|
|
214
305
|
throw new Error(`Unknown series: ${id}`);
|
|
215
306
|
}
|
|
216
|
-
async
|
|
307
|
+
async flushDirtySeries() {
|
|
217
308
|
const tasks = [];
|
|
218
309
|
for (const num of this.numericSeries.values()) {
|
|
219
|
-
if (num.hasDirtyData)
|
|
220
|
-
tasks.push(num.flush())
|
|
310
|
+
if (num.hasDirtyData) {
|
|
311
|
+
tasks.push(num.flush().then(() => {
|
|
312
|
+
this.syncNumericSeriesBytes(num);
|
|
313
|
+
}));
|
|
314
|
+
}
|
|
221
315
|
}
|
|
222
316
|
for (const val of this.valueSeries.values()) {
|
|
223
|
-
if (val.hasDirtyData)
|
|
224
|
-
tasks.push(val.flush())
|
|
317
|
+
if (val.hasDirtyData) {
|
|
318
|
+
tasks.push(val.flush().then(() => {
|
|
319
|
+
this.syncValueSeriesBytes(val);
|
|
320
|
+
}));
|
|
321
|
+
}
|
|
225
322
|
}
|
|
226
323
|
if (tasks.length > 0) {
|
|
227
324
|
await Promise.all(tasks);
|
|
228
325
|
}
|
|
229
326
|
}
|
|
327
|
+
needsLimitCheck() {
|
|
328
|
+
const limits = this.options.storageLimits;
|
|
329
|
+
if (!limits)
|
|
330
|
+
return false;
|
|
331
|
+
return ((limits.rawNumericMaxBytes !== undefined && this.rawNumericBytes > this.nextRawNumericThreshold) ||
|
|
332
|
+
(limits.rawValueMaxBytes !== undefined && this.rawValueBytes > this.nextRawValueThreshold) ||
|
|
333
|
+
(limits.rollupMaxBytes !== undefined && this.getTotalRollupBytes() > this.nextRollupThreshold));
|
|
334
|
+
}
|
|
335
|
+
async maybeEnforceStorageLimits() {
|
|
336
|
+
if (!this.needsLimitCheck())
|
|
337
|
+
return;
|
|
338
|
+
const checkInterval = this.options.storageLimits?.checkIntervalMs ?? 0;
|
|
339
|
+
if (checkInterval > 0 && Date.now() - this.lastLimitCheckAtMs < checkInterval) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
await this.scheduleEnforce(false);
|
|
343
|
+
}
|
|
344
|
+
scheduleEnforce(force) {
|
|
345
|
+
this.enforceChain = this.enforceChain.then(() => this.runEnforceStorageLimits(force));
|
|
346
|
+
return this.enforceChain;
|
|
347
|
+
}
|
|
348
|
+
async runEnforceStorageLimits(force) {
|
|
349
|
+
const limits = this.options.storageLimits;
|
|
350
|
+
if (!limits)
|
|
351
|
+
return this.getStorageUsage();
|
|
352
|
+
this.lastLimitCheckAtMs = Date.now();
|
|
353
|
+
if (limits.rawNumericMaxBytes !== undefined &&
|
|
354
|
+
this.rawNumericBytes > (force ? limits.rawNumericMaxBytes : this.nextRawNumericThreshold)) {
|
|
355
|
+
const target = Math.floor(limits.rawNumericMaxBytes * this.trimTargetRatio);
|
|
356
|
+
const candidates = [];
|
|
357
|
+
for (const num of this.numericSeries.values()) {
|
|
358
|
+
const chunks = num.listRawChunks();
|
|
359
|
+
for (let i = 0; i < chunks.length; ++i) {
|
|
360
|
+
const c = chunks[i];
|
|
361
|
+
candidates.push({
|
|
362
|
+
owner: num,
|
|
363
|
+
id: c.id,
|
|
364
|
+
minKey: c.minKey,
|
|
365
|
+
maxKey: c.maxKey,
|
|
366
|
+
byteSize: c.byteSize,
|
|
367
|
+
isLastChunkOfOwner: i === chunks.length - 1,
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
const drops = planChunkEviction(candidates, this.rawNumericBytes, target);
|
|
372
|
+
if (drops.size > 0) {
|
|
373
|
+
const tasks = [];
|
|
374
|
+
for (const [num, ids] of drops) {
|
|
375
|
+
tasks.push(num.dropRawChunks(ids).then(() => {
|
|
376
|
+
this.syncNumericSeriesBytes(num);
|
|
377
|
+
}));
|
|
378
|
+
}
|
|
379
|
+
await Promise.all(tasks);
|
|
380
|
+
}
|
|
381
|
+
this.nextRawNumericThreshold =
|
|
382
|
+
this.rawNumericBytes > limits.rawNumericMaxBytes
|
|
383
|
+
? this.rawNumericBytes + Math.max(4096, Math.floor(limits.rawNumericMaxBytes * 0.1))
|
|
384
|
+
: limits.rawNumericMaxBytes;
|
|
385
|
+
}
|
|
386
|
+
if (limits.rawValueMaxBytes !== undefined &&
|
|
387
|
+
this.rawValueBytes > (force ? limits.rawValueMaxBytes : this.nextRawValueThreshold)) {
|
|
388
|
+
const target = Math.floor(limits.rawValueMaxBytes * this.trimTargetRatio);
|
|
389
|
+
const candidates = [];
|
|
390
|
+
for (const val of this.valueSeries.values()) {
|
|
391
|
+
const chunks = val.listChunks();
|
|
392
|
+
for (let i = 0; i < chunks.length; ++i) {
|
|
393
|
+
const c = chunks[i];
|
|
394
|
+
candidates.push({
|
|
395
|
+
owner: val,
|
|
396
|
+
id: c.id,
|
|
397
|
+
minKey: c.minKey,
|
|
398
|
+
maxKey: c.maxKey,
|
|
399
|
+
byteSize: c.byteSize,
|
|
400
|
+
isLastChunkOfOwner: i === chunks.length - 1,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
const drops = planChunkEviction(candidates, this.rawValueBytes, target);
|
|
405
|
+
if (drops.size > 0) {
|
|
406
|
+
const tasks = [];
|
|
407
|
+
for (const [val, ids] of drops) {
|
|
408
|
+
tasks.push(val.dropChunks(ids).then(() => {
|
|
409
|
+
this.syncValueSeriesBytes(val);
|
|
410
|
+
}));
|
|
411
|
+
}
|
|
412
|
+
await Promise.all(tasks);
|
|
413
|
+
}
|
|
414
|
+
this.nextRawValueThreshold =
|
|
415
|
+
this.rawValueBytes > limits.rawValueMaxBytes
|
|
416
|
+
? this.rawValueBytes + Math.max(4096, Math.floor(limits.rawValueMaxBytes * 0.1))
|
|
417
|
+
: limits.rawValueMaxBytes;
|
|
418
|
+
}
|
|
419
|
+
if (limits.rollupMaxBytes !== undefined &&
|
|
420
|
+
this.getTotalRollupBytes() > (force ? limits.rollupMaxBytes : this.nextRollupThreshold)) {
|
|
421
|
+
const targetTotal = Math.floor(limits.rollupMaxBytes * this.trimTargetRatio);
|
|
422
|
+
const tierTargets = computeTierTargets(this.rollupTierBytes, targetTotal);
|
|
423
|
+
for (let ti = 0; ti < ROLLUP_TIERS.length; ++ti) {
|
|
424
|
+
const tier = ROLLUP_TIERS[ti];
|
|
425
|
+
const curTierBytes = this.rollupTierBytes[tier];
|
|
426
|
+
if (curTierBytes <= tierTargets[tier])
|
|
427
|
+
continue;
|
|
428
|
+
const candidates = [];
|
|
429
|
+
for (const num of this.numericSeries.values()) {
|
|
430
|
+
const chunks = num.listRollupChunks(tier);
|
|
431
|
+
for (let i = 0; i < chunks.length; ++i) {
|
|
432
|
+
const c = chunks[i];
|
|
433
|
+
candidates.push({
|
|
434
|
+
owner: num,
|
|
435
|
+
id: c.id,
|
|
436
|
+
minKey: c.minKey,
|
|
437
|
+
maxKey: c.maxKey,
|
|
438
|
+
byteSize: c.byteSize,
|
|
439
|
+
isLastChunkOfOwner: i === chunks.length - 1,
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
const drops = planChunkEviction(candidates, curTierBytes, tierTargets[tier]);
|
|
444
|
+
if (drops.size > 0) {
|
|
445
|
+
const tasks = [];
|
|
446
|
+
for (const [num, ids] of drops) {
|
|
447
|
+
tasks.push(num.dropRollupChunks(tier, ids).then(() => {
|
|
448
|
+
this.syncNumericSeriesBytes(num);
|
|
449
|
+
}));
|
|
450
|
+
}
|
|
451
|
+
await Promise.all(tasks);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
const afterTotal = this.getTotalRollupBytes();
|
|
455
|
+
this.nextRollupThreshold =
|
|
456
|
+
afterTotal > limits.rollupMaxBytes
|
|
457
|
+
? afterTotal + Math.max(4096, Math.floor(limits.rollupMaxBytes * 0.1))
|
|
458
|
+
: limits.rollupMaxBytes;
|
|
459
|
+
}
|
|
460
|
+
return this.getStorageUsage();
|
|
461
|
+
}
|
|
462
|
+
async enforceStorageLimits(force = true) {
|
|
463
|
+
await this.flushDirtySeries();
|
|
464
|
+
return this.scheduleEnforce(force);
|
|
465
|
+
}
|
|
466
|
+
async flush() {
|
|
467
|
+
await this.flushDirtySeries();
|
|
468
|
+
await this.maybeEnforceStorageLimits();
|
|
469
|
+
}
|
|
230
470
|
async close() {
|
|
231
471
|
if (this.closed)
|
|
232
472
|
return;
|
|
@@ -234,6 +474,10 @@ export class Tsdb {
|
|
|
234
474
|
clearInterval(this.flushTimer);
|
|
235
475
|
this.flushTimer = null;
|
|
236
476
|
}
|
|
477
|
+
if (this.options.storageLimits) {
|
|
478
|
+
await this.flush();
|
|
479
|
+
await this.enforceChain;
|
|
480
|
+
}
|
|
237
481
|
const closes = [];
|
|
238
482
|
for (const num of this.numericSeries.values())
|
|
239
483
|
closes.push(num.close());
|
package/dist/index.d.ts
CHANGED
|
@@ -5,4 +5,4 @@ export { FileTsdbStore } from './store.ts';
|
|
|
5
5
|
export type { NumericRawStore, RollupTierStore, TsdbStore, ValueRawStore } from './store.ts';
|
|
6
6
|
export { canTierServeInterval, parseInterval, selectAutoInterval, ServerCalendar, STANDARD_INTERVALS, TIER_STEP_MS, toEpochMs, } from './time.ts';
|
|
7
7
|
export type { ParsedInterval } from './time.ts';
|
|
8
|
-
export type { EncodedValueRecord, IntervalString, IntervalUnit, MemoryCacheConfig, NormalizedMemoryCacheConfig, NormalizedRawConfig, NormalizedSeriesConfig, NormalizedTierConfig, NumericColumnResult, NumericPoint, NumericQueryOptions, NumericQueryResult, RawOption, RollupTier, RollupType, SeriesConfig, SeriesKind, SeriesMeta, StandardInterval, TierOption, TimestampInput, TsdbOptions, ValueCodec, ValuePoint, ValueQueryOptions, ValueQueryResult, } from './types.ts';
|
|
8
|
+
export type { ChunkSizeOptions, EncodedValueRecord, IntervalString, IntervalUnit, MemoryCacheConfig, NormalizedMemoryCacheConfig, NormalizedRawConfig, NormalizedSeriesConfig, NormalizedTierConfig, NumericColumnResult, NumericPoint, NumericQueryOptions, NumericQueryResult, RawOption, RollupTier, RollupType, SeriesConfig, SeriesKind, SeriesMeta, StandardInterval, StorageChunkInfo, StorageLimitOptions, StorageUsageStats, TierOption, TimestampInput, TsdbOptions, ValueCodec, ValuePoint, ValueQueryOptions, ValueQueryResult, } from './types.ts';
|
package/dist/rawFile.d.ts
CHANGED
|
@@ -1,54 +1,87 @@
|
|
|
1
1
|
import type { StorageBackend } from './backend.ts';
|
|
2
2
|
import type { NumericRawStore, ValueRawStore } from './store.ts';
|
|
3
|
-
import type { EncodedValueRecord } from './types.ts';
|
|
3
|
+
import type { EncodedValueRecord, StorageChunkInfo } from './types.ts';
|
|
4
4
|
export declare class NumericRawFile implements NumericRawStore {
|
|
5
5
|
private readonly backend;
|
|
6
|
-
private readonly
|
|
7
|
-
private
|
|
8
|
-
|
|
9
|
-
private
|
|
10
|
-
private
|
|
11
|
-
private
|
|
6
|
+
private readonly relPrefix;
|
|
7
|
+
private readonly maxRecordsPerChunk;
|
|
8
|
+
private chunks;
|
|
9
|
+
private nextChunkId;
|
|
10
|
+
private activeHandle;
|
|
11
|
+
private activeChunkId;
|
|
12
|
+
private activeDirtyForManifest;
|
|
13
|
+
private _recordCount;
|
|
14
|
+
private _byteSize;
|
|
15
|
+
private opened;
|
|
12
16
|
private readonly headerBuf;
|
|
13
17
|
private readonly headerU8;
|
|
14
18
|
private readonly headerView;
|
|
15
|
-
constructor(backend: StorageBackend,
|
|
19
|
+
constructor(backend: StorageBackend, relPrefix: string, chunkBytes?: number);
|
|
20
|
+
get recordCount(): number;
|
|
21
|
+
get byteSize(): number;
|
|
22
|
+
private get manifestPath();
|
|
23
|
+
private chunkPath;
|
|
16
24
|
open(): Promise<void>;
|
|
17
|
-
private
|
|
18
|
-
private
|
|
19
|
-
private
|
|
20
|
-
private
|
|
25
|
+
private recomputeTotals;
|
|
26
|
+
private writeManifest;
|
|
27
|
+
private writeChunkHeader;
|
|
28
|
+
private writePairs;
|
|
29
|
+
private ensureActiveHandle;
|
|
30
|
+
private readChunkPairs;
|
|
21
31
|
appendBatch(interleaved: Float64Array, count: number): Promise<void>;
|
|
32
|
+
private findFirstChunkAtOrAfter;
|
|
22
33
|
readRange(startMs: number, endMs: number, limit?: number, stripNaN?: boolean): Promise<{
|
|
23
34
|
timestamps: Float64Array;
|
|
24
35
|
values: Float64Array;
|
|
25
36
|
length: number;
|
|
26
37
|
truncated: boolean;
|
|
27
38
|
}>;
|
|
39
|
+
private hasMatchingRecord;
|
|
28
40
|
readLastBefore(tsMs: number, inclusive?: boolean): Promise<{
|
|
29
41
|
ts: number;
|
|
30
42
|
value: number;
|
|
31
43
|
} | null>;
|
|
44
|
+
listChunks(): ReadonlyArray<StorageChunkInfo>;
|
|
45
|
+
dropChunks(chunkIds: ReadonlyArray<number>): Promise<void>;
|
|
32
46
|
compact(minTsKeep?: number, maxRecords?: number): Promise<void>;
|
|
33
47
|
close(): Promise<void>;
|
|
34
48
|
}
|
|
35
49
|
export declare class ValueRawFile implements ValueRawStore {
|
|
36
50
|
private readonly backend;
|
|
37
|
-
private readonly
|
|
38
|
-
private readonly
|
|
39
|
-
private
|
|
40
|
-
private
|
|
41
|
-
|
|
42
|
-
private
|
|
43
|
-
private
|
|
44
|
-
private
|
|
45
|
-
|
|
51
|
+
private readonly relPrefix;
|
|
52
|
+
private readonly targetChunkBytes;
|
|
53
|
+
private chunks;
|
|
54
|
+
private nextChunkId;
|
|
55
|
+
private activeHandle;
|
|
56
|
+
private activeChunkId;
|
|
57
|
+
private activeDirtyForManifest;
|
|
58
|
+
private _recordCount;
|
|
59
|
+
private _byteSize;
|
|
60
|
+
private opened;
|
|
61
|
+
private readonly headerBuf;
|
|
62
|
+
private readonly headerU8;
|
|
63
|
+
private readonly headerView;
|
|
64
|
+
constructor(backend: StorageBackend, relPrefix: string, targetChunkBytes?: number);
|
|
65
|
+
get recordCount(): number;
|
|
66
|
+
get byteSize(): number;
|
|
67
|
+
private get manifestPath();
|
|
68
|
+
private chunkPath;
|
|
46
69
|
open(): Promise<void>;
|
|
47
|
-
private
|
|
70
|
+
private recomputeTotals;
|
|
71
|
+
private writeManifest;
|
|
72
|
+
private writeChunkHeader;
|
|
73
|
+
private ensureActiveHandle;
|
|
48
74
|
appendBatch(entries: ReadonlyArray<EncodedValueRecord>): Promise<void>;
|
|
75
|
+
private findFirstChunkAtOrAfter;
|
|
76
|
+
private findLastChunkAtOrBefore;
|
|
77
|
+
private readChunkBody;
|
|
78
|
+
private decodeRecordAt;
|
|
49
79
|
readRange(startMs: number, endMs: number, limit?: number, order?: 'asc' | 'desc'): Promise<{
|
|
50
80
|
records: EncodedValueRecord[];
|
|
51
81
|
truncated: boolean;
|
|
52
82
|
}>;
|
|
83
|
+
listChunks(): ReadonlyArray<StorageChunkInfo>;
|
|
84
|
+
dropChunks(chunkIds: ReadonlyArray<number>): Promise<void>;
|
|
85
|
+
compact(minTsKeep?: number, maxRecords?: number): Promise<void>;
|
|
53
86
|
close(): Promise<void>;
|
|
54
87
|
}
|