@thingmate/zekta-time-series 0.1.0 → 1.0.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/CONTRIBUTING.md +99 -43
  3. package/README.md +230 -2
  4. package/package.json +27 -41
  5. package/src/bucket.private/helpers.private/temp-files/temp-files.d.ts +15 -0
  6. package/src/bucket.private/helpers.private/temp-files/temp-files.js +47 -0
  7. package/src/bucket.private/helpers.private/temp-files/temp-files.js.map +1 -0
  8. package/src/bucket.private/zekta-time-bucket.d.ts +80 -0
  9. package/src/bucket.private/zekta-time-bucket.js +504 -0
  10. package/src/bucket.private/zekta-time-bucket.js.map +1 -0
  11. package/src/config/v1/zekta-time-series-config-v1-schema.d.ts +11 -0
  12. package/src/config/v1/zekta-time-series-config-v1-schema.js +12 -0
  13. package/src/config/v1/zekta-time-series-config-v1-schema.js.map +1 -0
  14. package/src/config/v1/zekta-time-series-config-v1.d.ts +11 -0
  15. package/src/config/v1/zekta-time-series-config-v1.js +2 -0
  16. package/src/config/v1/zekta-time-series-config-v1.js.map +1 -0
  17. package/src/config/zekta-time-series-config.d.ts +6 -0
  18. package/src/config/zekta-time-series-config.js +2 -0
  19. package/src/config/zekta-time-series-config.js.map +1 -0
  20. package/{helpers.private → src/helpers.private}/binary-search/binary-search.js +1 -2
  21. package/src/helpers.private/binary-search/binary-search.js.map +1 -0
  22. package/{helpers.private → src/helpers.private}/handle-promise-all-settled-result.d.ts +6 -0
  23. package/{helpers.private → src/helpers.private}/handle-promise-all-settled-result.js +6 -0
  24. package/src/helpers.private/handle-promise-all-settled-result.js.map +1 -0
  25. package/{helpers.private → src/helpers.private}/resizeable-buffer.d.ts +6 -0
  26. package/{helpers.private → src/helpers.private}/resizeable-buffer.js +7 -3
  27. package/src/helpers.private/resizeable-buffer.js.map +1 -0
  28. package/src/helpers.private/safe-write-file/safe-write-file.d.ts +13 -0
  29. package/src/helpers.private/safe-write-file/safe-write-file.js +29 -0
  30. package/src/helpers.private/safe-write-file/safe-write-file.js.map +1 -0
  31. package/src/helpers.private/throw-if-time-is-invalid/throw-if-time-is-invalid.d.ts +6 -0
  32. package/src/helpers.private/throw-if-time-is-invalid/throw-if-time-is-invalid.js +11 -0
  33. package/src/helpers.private/throw-if-time-is-invalid/throw-if-time-is-invalid.js.map +1 -0
  34. package/{index.d.ts → src/index.d.ts} +3 -1
  35. package/src/index.js +8 -0
  36. package/src/index.js.map +1 -0
  37. package/{specializations → src/specializations}/bytes/zekta-bytes-time-series.d.ts +21 -1
  38. package/src/specializations/bytes/zekta-bytes-time-series.js +152 -0
  39. package/src/specializations/bytes/zekta-bytes-time-series.js.map +1 -0
  40. package/src/specializations/number/zekta-number-time-series.d.ts +51 -0
  41. package/{specializations → src/specializations}/number/zekta-number-time-series.js +32 -11
  42. package/src/specializations/number/zekta-number-time-series.js.map +1 -0
  43. package/src/specializations/text/zekta-text-time-series.d.ts +36 -0
  44. package/{specializations → src/specializations}/text/zekta-text-time-series.js +20 -2
  45. package/src/specializations/text/zekta-text-time-series.js.map +1 -0
  46. package/src/zekta-time-series.d.ts +78 -0
  47. package/src/zekta-time-series.js +280 -0
  48. package/src/zekta-time-series.js.map +1 -0
  49. package/helpers.private/binary-search/binary-search.js.map +0 -1
  50. package/helpers.private/handle-promise-all-settled-result.js.map +0 -1
  51. package/helpers.private/resizeable-buffer.js.map +0 -1
  52. package/index.js +0 -6
  53. package/index.js.map +0 -1
  54. package/specializations/bytes/zekta-bytes-time-series.js +0 -87
  55. package/specializations/bytes/zekta-bytes-time-series.js.map +0 -1
  56. package/specializations/number/zekta-number-time-series.d.ts +0 -20
  57. package/specializations/number/zekta-number-time-series.js.map +0 -1
  58. package/specializations/text/zekta-text-time-series.d.ts +0 -16
  59. package/specializations/text/zekta-text-time-series.js.map +0 -1
  60. package/zekta-time-bucket.d.ts +0 -35
  61. package/zekta-time-bucket.js +0 -324
  62. package/zekta-time-bucket.js.map +0 -1
  63. package/zekta-time-series.d.ts +0 -25
  64. package/zekta-time-series.js +0 -194
  65. package/zekta-time-series.js.map +0 -1
  66. /package/{helpers.private → src/helpers.private}/binary-search/binary-search.d.ts +0 -0
@@ -0,0 +1,80 @@
1
+ import { TimeSeries, type TimeSeriesDeleteOptions, type TimeSeriesEntry, type TimeSeriesSelectOptions } from '@thingmate/time-series';
2
+ import { type PathInput } from '@xstd/path';
3
+ export interface ZektaTimeBucketFromFilePathOptions extends Omit<ZektaTimeBucketOptions, 'bucketsPath' | 'from' | 'to'> {
4
+ readonly filePath: PathInput;
5
+ readonly timeRange: number;
6
+ }
7
+ export interface ZektaTimeBucketFlushOptions {
8
+ /**
9
+ * When true, releases the in-memory data and stops the background timers:
10
+ * an explicit flush with "unload" is the signal that the bucket will not
11
+ * be used anymore.
12
+ */
13
+ readonly unload?: boolean;
14
+ }
15
+ export interface ZektaTimeBucketOptions {
16
+ readonly bucketsPath: PathInput;
17
+ readonly from: number;
18
+ readonly to: number;
19
+ readonly valueByteLength: number;
20
+ }
21
+ /**
22
+ * One persisted chunk of a "ZektaTimeSeries", covering a fixed time range
23
+ * and holding all its entries sorted by time in a single file.
24
+ *
25
+ * Changes are applied in memory and persisted to disk automatically, or
26
+ * immediately through "flush()". A failed persistence never loses the
27
+ * in-memory data: the next flush retries. Data written by an interrupted
28
+ * persistence is trimmed on the next load.
29
+ */
30
+ export declare class ZektaTimeBucket extends TimeSeries<Uint8Array> {
31
+ #private;
32
+ /**
33
+ * Creates the bucket corresponding to a bucket file path. Its name must be
34
+ * the canonical decimal form of the bucket start time, aligned on
35
+ * "timeRange"; otherwise the call throws (it indicates a corruption or a
36
+ * configuration change).
37
+ */
38
+ static fromFilePath({ filePath, timeRange, ...options }: ZektaTimeBucketFromFilePathOptions): ZektaTimeBucket;
39
+ static readonly sortFnc: (a: ZektaTimeBucket, b: ZektaTimeBucket) => number;
40
+ constructor({ bucketsPath, from, to, valueByteLength }: ZektaTimeBucketOptions);
41
+ get from(): number;
42
+ get to(): number;
43
+ get valueByteLength(): number;
44
+ isTimeInRange(time: number): boolean;
45
+ throwIfTimeOutOfRange(time: number): void;
46
+ /**
47
+ * Adds one entry: "time" must fall within the bucket time range and
48
+ * "value" must be exactly "valueByteLength" long, otherwise the call
49
+ * rejects and the bucket is left untouched.
50
+ */
51
+ push(time: number, value: Uint8Array): Promise<void>;
52
+ /**
53
+ * Adds many entries in one atomic operation: either all of them are
54
+ * added, or none of them are. Entries may be unsorted; entries sharing a
55
+ * time keep their relative order, after the existing ones.
56
+ */
57
+ insert(entries: readonly TimeSeriesEntry<Uint8Array>[]): Promise<void>;
58
+ /**
59
+ * Returns the entries within the requested time range, sorted by time
60
+ * (ascending by default).
61
+ */
62
+ select(options?: TimeSeriesSelectOptions): Promise<readonly TimeSeriesEntry<Uint8Array>[]>;
63
+ /**
64
+ * Removes the entries within the requested time range. Without options,
65
+ * removes every entry.
66
+ */
67
+ delete(options?: TimeSeriesDeleteOptions): Promise<void>;
68
+ /**
69
+ * Removes every entry of the bucket. The bucket stays usable afterwards.
70
+ */
71
+ drop(): Promise<void>;
72
+ /**
73
+ * Persists the pending changes to disk.
74
+ *
75
+ * Without options, only the pending data is persisted. With
76
+ * "{ unload: true }", the bucket also releases every resource it keeps
77
+ * alive: call it once you are done with the bucket.
78
+ */
79
+ flush({ unload }?: ZektaTimeBucketFlushOptions): Promise<void>;
80
+ }
@@ -0,0 +1,504 @@
1
+ import { normalizeTimeSeriesDeleteOptions, normalizeTimeSeriesSelectOptions, sortTimeSeriesEntries, TimeSeries, } from '@thingmate/time-series';
2
+ import { Path } from '@xstd/path';
3
+ import { mkdir, open, readFile, rm, truncate } from 'node:fs/promises';
4
+ import { binarySearch } from '../helpers.private/binary-search/binary-search.js';
5
+ import { ResizeableBuffer } from '../helpers.private/resizeable-buffer.js';
6
+ import { safeWriteFile } from '../helpers.private/safe-write-file/safe-write-file.js';
7
+ import { createBucketTempFileName } from './helpers.private/temp-files/temp-files.js';
8
+ /**
9
+ * One persisted chunk of a "ZektaTimeSeries", covering a fixed time range
10
+ * and holding all its entries sorted by time in a single file.
11
+ *
12
+ * Changes are applied in memory and persisted to disk automatically, or
13
+ * immediately through "flush()". A failed persistence never loses the
14
+ * in-memory data: the next flush retries. Data written by an interrupted
15
+ * persistence is trimmed on the next load.
16
+ */
17
+ export class ZektaTimeBucket extends TimeSeries {
18
+ static #autoFlushTime = 1000;
19
+ static #autoUnloadTime = 5000;
20
+ /**
21
+ * Creates the bucket corresponding to a bucket file path. Its name must be
22
+ * the canonical decimal form of the bucket start time, aligned on
23
+ * "timeRange"; otherwise the call throws (it indicates a corruption or a
24
+ * configuration change).
25
+ */
26
+ static fromFilePath({ filePath, timeRange, ...options }) {
27
+ const stem = Path.of(filePath).stemAndExt().stem;
28
+ const from = Number(stem);
29
+ if (!Number.isSafeInteger(from)) {
30
+ throw new Error(`Invalid from: ${from}`);
31
+ }
32
+ if (stem !== from.toString()) {
33
+ /* non-canonical names (leading zeros, "-0", ...) would allow several
34
+ files to resolve to the same bucket start time */
35
+ throw new Error(`Invalid bucket file name: "${stem}", expected: "${from}".`);
36
+ }
37
+ if (from % timeRange !== 0) {
38
+ throw new Error(`Misaligned bucket start time: ${from} is not a multiple of the bucket time range ${timeRange}.`);
39
+ }
40
+ return new ZektaTimeBucket({
41
+ ...options,
42
+ bucketsPath: Path.of(filePath).dirname(),
43
+ from,
44
+ to: from + timeRange,
45
+ });
46
+ }
47
+ static sortFnc = (a, b) => {
48
+ return a.#from - b.#from;
49
+ };
50
+ #path;
51
+ #from;
52
+ #to;
53
+ #valueByteLength;
54
+ #entryByteLength; // computed
55
+ #queue;
56
+ #data;
57
+ #flushRequired;
58
+ #persistedByteLength; // what the file on disk currently contains
59
+ #consistentPrefixByteLength; // memory prefix byte-identical to the file prefix
60
+ #autoFlushTimer;
61
+ #autoUnloadTimer;
62
+ constructor({ bucketsPath, from, to, valueByteLength }) {
63
+ super();
64
+ this.#path = Path.of(bucketsPath).concat(`${from}.bucket`);
65
+ this.#from = from;
66
+ this.#to = to;
67
+ this.#valueByteLength = valueByteLength;
68
+ this.#entryByteLength = timeByteLength + this.#valueByteLength;
69
+ this.#queue = Promise.resolve();
70
+ this.#flushRequired = false;
71
+ this.#persistedByteLength = 0;
72
+ this.#consistentPrefixByteLength = 0;
73
+ }
74
+ get from() {
75
+ return this.#from;
76
+ }
77
+ get to() {
78
+ return this.#to;
79
+ }
80
+ get valueByteLength() {
81
+ return this.#valueByteLength;
82
+ }
83
+ isTimeInRange(time) {
84
+ return this.#from <= time && time < this.#to;
85
+ }
86
+ throwIfTimeOutOfRange(time) {
87
+ if (!this.isTimeInRange(time)) {
88
+ throw new RangeError(`Time out-of-range: ${time}, expected: [${this.#from}, ${this.#to}[.`);
89
+ }
90
+ }
91
+ #throwIfValueLengthIsInvalid(value) {
92
+ if (value.length !== this.#valueByteLength) {
93
+ throw new Error(`Invalid value length: ${value.length}, expected: ${this.#valueByteLength}.`);
94
+ }
95
+ }
96
+ #run(task) {
97
+ const queuedTask = this.#queue.then(task, task);
98
+ this.#queue = queuedTask;
99
+ return queuedTask;
100
+ }
101
+ /* LOAD/SAVE DATA */
102
+ #startAutoFlushTimer() {
103
+ if (this.#autoFlushTimer !== undefined) {
104
+ /* a flush is already scheduled: keep its original deadline, so that
105
+ sustained activity cannot defer the flush indefinitely */
106
+ return;
107
+ }
108
+ this.#autoFlushTimer = setTimeout(() => {
109
+ this.#autoFlushTimer = undefined;
110
+ this.flush({ unload: false }).catch(reportError);
111
+ }, ZektaTimeBucket.#autoFlushTime);
112
+ }
113
+ #stopAutoFlushTimer() {
114
+ if (this.#autoFlushTimer !== undefined) {
115
+ clearTimeout(this.#autoFlushTimer);
116
+ this.#autoFlushTimer = undefined;
117
+ }
118
+ }
119
+ #startAutoUnloadTimer() {
120
+ this.#stopAutoUnloadTimer();
121
+ this.#autoUnloadTimer = setTimeout(() => {
122
+ this.#autoUnloadTimer = undefined;
123
+ this.flush({ unload: true }).catch(reportError);
124
+ }, ZektaTimeBucket.#autoUnloadTime);
125
+ }
126
+ #stopAutoUnloadTimer() {
127
+ if (this.#autoUnloadTimer !== undefined) {
128
+ clearTimeout(this.#autoUnloadTimer);
129
+ this.#autoUnloadTimer = undefined;
130
+ }
131
+ }
132
+ async #loadData() {
133
+ if (this.#data === undefined) {
134
+ let bytes;
135
+ try {
136
+ bytes = await readFile(this.#path.toString());
137
+ }
138
+ catch (error) {
139
+ if (error.code === 'ENOENT') {
140
+ this.#data = new ResizeableBuffer();
141
+ this.#persistedByteLength = 0;
142
+ this.#consistentPrefixByteLength = 0;
143
+ return;
144
+ }
145
+ else {
146
+ throw error;
147
+ }
148
+ }
149
+ let dataByteLength = bytes.byteLength;
150
+ if (bytes.byteLength % this.#entryByteLength !== 0) {
151
+ /* an interrupted in-place write (append) can leave a torn tail: keep the
152
+ complete entries and trim the tail on disk. The repair is idempotent, a
153
+ crash during the repair is redone on the next load */
154
+ dataByteLength = bytes.byteLength - (bytes.byteLength % this.#entryByteLength);
155
+ await truncate(this.#path.toString(), dataByteLength);
156
+ }
157
+ this.#data = new ResizeableBuffer(bytes.buffer, bytes.byteOffset, dataByteLength);
158
+ this.#persistedByteLength = dataByteLength;
159
+ this.#consistentPrefixByteLength = dataByteLength;
160
+ }
161
+ }
162
+ #runDataOperation(task) {
163
+ return this.#run(async () => {
164
+ /* only the auto-unload timer is reset per operation: unloading an active
165
+ bucket would cause reload storms, it must stay idle-based. The auto-flush
166
+ deadline is intentionally NOT reset: it is set by the first pending change */
167
+ this.#stopAutoUnloadTimer();
168
+ try {
169
+ await this.#loadData();
170
+ return await task(() => {
171
+ this.#flushRequired = true;
172
+ });
173
+ }
174
+ finally {
175
+ /* the auto-flush timer is only armed by changes still pending: a
176
+ read-only operation must not schedule a useless flush, nor keep the
177
+ event loop alive for nothing */
178
+ if (this.#flushRequired) {
179
+ this.#startAutoFlushTimer();
180
+ }
181
+ this.#startAutoUnloadTimer();
182
+ }
183
+ });
184
+ }
185
+ /* READ/WRITE DATA */
186
+ #readTime(entryByteOffset) {
187
+ return this.#data.view.getFloat64(entryByteOffset, true);
188
+ }
189
+ #writeTime(entryByteOffset, time) {
190
+ this.#data.view.setFloat64(entryByteOffset, time, true);
191
+ }
192
+ #readValue(entryByteOffset) {
193
+ const valueByteOffset = entryByteOffset + timeByteLength;
194
+ return this.#data.bytes.slice(valueByteOffset, valueByteOffset + this.#valueByteLength);
195
+ }
196
+ #writeValue(entryByteOffset, value) {
197
+ console.assert(value.length === this.#valueByteLength);
198
+ this.#data.bytes.set(value, entryByteOffset + timeByteLength);
199
+ }
200
+ #getInsertionByteOffset(time) {
201
+ console.assert(this.#data !== undefined);
202
+ if (this.#data.length === 0) {
203
+ return 0;
204
+ }
205
+ else {
206
+ const lastEntryByteOffset = this.#data.length - this.#entryByteLength;
207
+ if (time >= this.#readTime(lastEntryByteOffset) /* lastTime*/) {
208
+ // insert at the end
209
+ return this.#data.length;
210
+ }
211
+ else if (time <= this.#readTime(0) /* firstTime */) {
212
+ // insert at the beginning
213
+ return 0;
214
+ }
215
+ else {
216
+ return (binarySearch(this.#data.length / this.#entryByteLength, (index) => {
217
+ return this.#readTime(index * this.#entryByteLength) - time;
218
+ }) * this.#entryByteLength);
219
+ }
220
+ }
221
+ }
222
+ #getTimeRangeByteOffsets({ from, to /* included */ }) {
223
+ let fromEntryByteOffset = this.#getInsertionByteOffset(from);
224
+ while (fromEntryByteOffset >= this.#entryByteLength &&
225
+ from === this.#readTime(fromEntryByteOffset - this.#entryByteLength)) {
226
+ fromEntryByteOffset -= this.#entryByteLength;
227
+ }
228
+ let toEntryByteOffset = this.#getInsertionByteOffset(to);
229
+ while (toEntryByteOffset < this.#data.length && to === this.#readTime(toEntryByteOffset)) {
230
+ toEntryByteOffset += this.#entryByteLength;
231
+ }
232
+ return {
233
+ from: fromEntryByteOffset,
234
+ to: toEntryByteOffset /* excluded */,
235
+ };
236
+ }
237
+ /* OPERATIONS */
238
+ /**
239
+ * Adds one entry: "time" must fall within the bucket time range and
240
+ * "value" must be exactly "valueByteLength" long, otherwise the call
241
+ * rejects and the bucket is left untouched.
242
+ */
243
+ push(time, value) {
244
+ return this.#runDataOperation(async (requireFlush) => {
245
+ this.throwIfTimeOutOfRange(time);
246
+ this.#throwIfValueLengthIsInvalid(value);
247
+ const insertByteOffset = this.#getInsertionByteOffset(time);
248
+ /* bytes before the insertion point keep their content: the consistent
249
+ prefix cannot extend beyond it */
250
+ this.#consistentPrefixByteLength = Math.min(this.#consistentPrefixByteLength, insertByteOffset);
251
+ this.#data.resize(this.#data.length + this.#entryByteLength); // [f64, f64]
252
+ this.#data.bytes.copyWithin(insertByteOffset + this.#entryByteLength, insertByteOffset, this.#data.length);
253
+ this.#writeTime(insertByteOffset, time);
254
+ this.#writeValue(insertByteOffset, value);
255
+ requireFlush();
256
+ });
257
+ }
258
+ /**
259
+ * Adds many entries in one atomic operation: either all of them are
260
+ * added, or none of them are. Entries may be unsorted; entries sharing a
261
+ * time keep their relative order, after the existing ones.
262
+ */
263
+ insert(entries) {
264
+ return this.#runDataOperation((requireFlush) => {
265
+ if (entries.length === 0) {
266
+ return;
267
+ }
268
+ const sortedEntries = entries.toSorted(sortTimeSeriesEntries);
269
+ /* validate everything first: a failing insert must not leave any trace */
270
+ for (let i = 0; i < sortedEntries.length; i += 1) {
271
+ const { time, value } = sortedEntries[i];
272
+ this.throwIfTimeOutOfRange(time);
273
+ this.#throwIfValueLengthIsInvalid(value);
274
+ }
275
+ /* merge the sorted entries in a single pass: existing entries are
276
+ displaced at most once via block copies and inserted entries are written
277
+ once (O(n + k)) instead of one tail shift per entry (O(k · n)) */
278
+ const oldByteLength = this.#data.length;
279
+ const insertedByteLength = sortedEntries.length * this.#entryByteLength;
280
+ /* byte offset (in the old layout) where each inserted entry lands: the
281
+ existing entries with a time <= the inserted time stay before it (existing
282
+ entries win on equal times) */
283
+ const insertByteOffsets = new Array(sortedEntries.length);
284
+ const oldEntryCount = oldByteLength / this.#entryByteLength;
285
+ if (oldEntryCount === 0 ||
286
+ sortedEntries[0].time >=
287
+ this.#readTime(oldByteLength - this.#entryByteLength) /* lastTime */) {
288
+ /* fast path: every inserted entry lands after all the existing ones */
289
+ insertByteOffsets.fill(oldByteLength);
290
+ }
291
+ else {
292
+ /* the insert positions are monotonically increasing: they are located by
293
+ galloping forward from the previous one (logarithmic when insertions are
294
+ far apart, near-constant when they are adjacent) */
295
+ let oldCursor = 0;
296
+ for (let i = 0; i < sortedEntries.length; i += 1) {
297
+ const { time } = sortedEntries[i];
298
+ /* gallop forward until an existing entry with a greater time is found */
299
+ let probeStep = 1;
300
+ let probeIndex = oldCursor;
301
+ while (probeIndex < oldEntryCount &&
302
+ this.#readTime(probeIndex * this.#entryByteLength) <= time) {
303
+ probeIndex += probeStep;
304
+ probeStep *= 2;
305
+ }
306
+ /* the insert position lies between the last two probes: refine it with
307
+ a binary search */
308
+ let low = Math.max(oldCursor, probeIndex - (probeStep >> 1));
309
+ let high = Math.min(probeIndex, oldEntryCount);
310
+ while (low < high) {
311
+ const mid = (low + high) >>> 1;
312
+ if (this.#readTime(mid * this.#entryByteLength) <= time) {
313
+ low = mid + 1;
314
+ }
315
+ else {
316
+ high = mid;
317
+ }
318
+ }
319
+ oldCursor = low;
320
+ insertByteOffsets[i] = low * this.#entryByteLength;
321
+ }
322
+ /* the earliest displaced byte: everything before it is untouched */
323
+ this.#consistentPrefixByteLength = Math.min(this.#consistentPrefixByteLength, insertByteOffsets[0]);
324
+ }
325
+ this.#data.resize(oldByteLength + insertedByteLength);
326
+ /* move the existing segments located between consecutive insert positions,
327
+ from the last one to the first one: segment i (the existing entries between
328
+ the insert positions i-1 and i) is shifted by i entries */
329
+ let segmentEnd = oldByteLength;
330
+ for (let i = sortedEntries.length; i >= 1; i -= 1) {
331
+ const segmentStart = insertByteOffsets[i - 1];
332
+ if (segmentEnd > segmentStart) {
333
+ this.#data.bytes.copyWithin(segmentStart + i * this.#entryByteLength, segmentStart, segmentEnd);
334
+ }
335
+ segmentEnd = segmentStart;
336
+ }
337
+ for (let i = 0; i < sortedEntries.length; i += 1) {
338
+ const { time, value } = sortedEntries[i];
339
+ const entryByteOffset = insertByteOffsets[i] + i * this.#entryByteLength;
340
+ this.#writeTime(entryByteOffset, time);
341
+ this.#writeValue(entryByteOffset, value);
342
+ }
343
+ requireFlush();
344
+ });
345
+ }
346
+ /**
347
+ * Returns the entries within the requested time range, sorted by time
348
+ * (ascending by default).
349
+ */
350
+ async select(options) {
351
+ const { asc, from, to } = normalizeTimeSeriesSelectOptions(options);
352
+ if (from >= this.#to || to < this.#from) {
353
+ // out-of-range
354
+ return [];
355
+ }
356
+ return this.#runDataOperation(() => {
357
+ const { from: fromEntryByteOffset, to: toEntryByteOffset } = this.#getTimeRangeByteOffsets({
358
+ from,
359
+ to,
360
+ });
361
+ const entries = new Array((toEntryByteOffset - fromEntryByteOffset) / this.#entryByteLength);
362
+ if (asc) {
363
+ for (let entryIndex = 0, entryByteOffset = fromEntryByteOffset; entryIndex < entries.length; entryIndex += 1, entryByteOffset += this.#entryByteLength) {
364
+ entries[entryIndex] = {
365
+ time: this.#readTime(entryByteOffset),
366
+ value: this.#readValue(entryByteOffset),
367
+ };
368
+ }
369
+ }
370
+ else {
371
+ for (let entryIndex = 0, entryByteOffset = toEntryByteOffset - this.#entryByteLength; entryIndex < entries.length; entryIndex += 1, entryByteOffset -= this.#entryByteLength) {
372
+ entries[entryIndex] = {
373
+ time: this.#readTime(entryByteOffset),
374
+ value: this.#readValue(entryByteOffset),
375
+ };
376
+ }
377
+ }
378
+ return entries;
379
+ });
380
+ }
381
+ /**
382
+ * Removes the entries within the requested time range. Without options,
383
+ * removes every entry.
384
+ */
385
+ async delete(options) {
386
+ const { from, to } = normalizeTimeSeriesDeleteOptions(options);
387
+ if (from >= this.#to || to < this.#from) {
388
+ // out-of-range
389
+ return;
390
+ }
391
+ return this.#runDataOperation((requireFlush) => {
392
+ const { from: fromEntryByteOffset, to: toEntryByteOffset } = this.#getTimeRangeByteOffsets({
393
+ from,
394
+ to,
395
+ });
396
+ if (fromEntryByteOffset === toEntryByteOffset) {
397
+ return;
398
+ }
399
+ /* bytes before the deleted range keep their content */
400
+ this.#consistentPrefixByteLength = Math.min(this.#consistentPrefixByteLength, fromEntryByteOffset);
401
+ this.#data.bytes.copyWithin(fromEntryByteOffset, toEntryByteOffset, this.#data.length);
402
+ this.#data.resize(this.#data.length - (toEntryByteOffset - fromEntryByteOffset));
403
+ requireFlush();
404
+ });
405
+ }
406
+ /**
407
+ * Removes every entry of the bucket. The bucket stays usable afterwards.
408
+ */
409
+ drop() {
410
+ return this.#runDataOperation((requireFlush) => {
411
+ if (this.#data.length === 0) {
412
+ return;
413
+ }
414
+ this.#data.resize(0);
415
+ this.#consistentPrefixByteLength = 0;
416
+ requireFlush();
417
+ });
418
+ }
419
+ /* FLUSH */
420
+ /**
421
+ * Persists the pending changes to disk.
422
+ *
423
+ * Without options, only the pending data is persisted. With
424
+ * "{ unload: true }", the bucket also releases every resource it keeps
425
+ * alive: call it once you are done with the bucket.
426
+ */
427
+ flush({ unload = false } = {}) {
428
+ return this.#run(async () => {
429
+ if (this.#flushRequired) {
430
+ console.assert(this.#data !== undefined);
431
+ if (this.#data.length === 0) {
432
+ /* no data: the bucket file must not exist, nothing else to do */
433
+ await rm(this.#path.toString(), {
434
+ force: true,
435
+ });
436
+ this.#persistedByteLength = 0;
437
+ this.#consistentPrefixByteLength = 0;
438
+ }
439
+ else if (this.#persistedByteLength > 0 &&
440
+ this.#consistentPrefixByteLength === this.#persistedByteLength &&
441
+ this.#data.length > this.#persistedByteLength) {
442
+ /* the file ends exactly where the identical prefix does: only the
443
+ tail is new, append it in place. An interruption leaves a torn tail,
444
+ repaired (trimmed) on the next load */
445
+ const fileHandle = await open(this.#path.toString(), 'r+');
446
+ try {
447
+ await fileHandle.write(this.#data.bytes.subarray(this.#persistedByteLength, this.#data.length), 0, this.#data.length - this.#persistedByteLength, this.#persistedByteLength);
448
+ await fileHandle.sync();
449
+ }
450
+ finally {
451
+ await fileHandle.close();
452
+ }
453
+ this.#persistedByteLength = this.#data.length;
454
+ this.#consistentPrefixByteLength = this.#data.length;
455
+ }
456
+ else if (this.#consistentPrefixByteLength === this.#data.length &&
457
+ this.#persistedByteLength > this.#data.length) {
458
+ /* the memory is a strict prefix of the file: shrink the file in place */
459
+ const fileHandle = await open(this.#path.toString(), 'r+');
460
+ try {
461
+ await fileHandle.truncate(this.#data.length);
462
+ await fileHandle.sync();
463
+ }
464
+ finally {
465
+ await fileHandle.close();
466
+ }
467
+ this.#persistedByteLength = this.#data.length;
468
+ }
469
+ else {
470
+ /* creation (persisted length of 0), a mid-file change or a mix of
471
+ both: rewrite the whole content aside, then make it visible atomically */
472
+ const bucketsDirPath = this.#path.dirname();
473
+ /* the temp file lives in the parent directory: a leftover temp inside
474
+ the buckets directory would break "ZektaTimeSeries.open" (it parses every
475
+ file name as "<from>.bucket") */
476
+ const tempFilePath = bucketsDirPath.dirname().concat(createBucketTempFileName());
477
+ await mkdir(bucketsDirPath.toString(), {
478
+ recursive: true,
479
+ });
480
+ await safeWriteFile(this.#path, tempFilePath, this.#data.bytes.subarray(0, this.#data.length));
481
+ this.#persistedByteLength = this.#data.length;
482
+ this.#consistentPrefixByteLength = this.#data.length;
483
+ }
484
+ this.#flushRequired = false;
485
+ }
486
+ /* a completed flush makes the scheduled auto-flush redundant: stopping it
487
+ releases the timer while keeping the deadline semantics. On a write failure
488
+ nothing is stopped so that the next timer tick can retry */
489
+ this.#stopAutoFlushTimer();
490
+ if (unload) {
491
+ /* releasing the bucket is the "done" signal: the auto timers must not
492
+ keep anything alive after it */
493
+ this.#stopAutoUnloadTimer();
494
+ this.#data = undefined;
495
+ }
496
+ });
497
+ }
498
+ }
499
+ /* INTERNAL */
500
+ const timeByteLength = 8; // float64
501
+ function reportError(error) {
502
+ (globalThis.reportError ?? console.error)(error);
503
+ }
504
+ //# sourceMappingURL=zekta-time-bucket.js.map
@@ -0,0 +1 @@
1
+ {"version":3,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I,CAAC,mBAAmB,EAAE,CAAC;YAE3B,IAAI,MAAM,EAAE,CAAC;gBACX;+CAC+B;gBAC/B,IAAI,CAAC,oBAAoB,EAAE,CAAC;gBAC5B,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC;YACzB,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED,cAAc;AAEd,MAAM,cAAc,GAAW,CAAC,CAAC,CAAC,UAAU;AAE5C,SAAS,WAAW,CAAC,KAAc;IACjC,CAAC,UAAU,CAAC,WAAW,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;AACnD,CAAC"}
@@ -0,0 +1,11 @@
1
+ import * as z from 'zod';
2
+ /**
3
+ * Validates a version 1 "ZektaTimeSeries" configuration: unknown fields are
4
+ * rejected, and both "valueByteLength" and "bucketTimeRange" must be
5
+ * positive integers.
6
+ */
7
+ export declare const ZektaTimeSeriesConfigV1Schema: z.ZodObject<{
8
+ version: z.ZodLiteral<1>;
9
+ valueByteLength: z.ZodNumber;
10
+ bucketTimeRange: z.ZodNumber;
11
+ }, z.core.$strict>;
@@ -0,0 +1,12 @@
1
+ import * as z from 'zod';
2
+ /**
3
+ * Validates a version 1 "ZektaTimeSeries" configuration: unknown fields are
4
+ * rejected, and both "valueByteLength" and "bucketTimeRange" must be
5
+ * positive integers.
6
+ */
7
+ export const ZektaTimeSeriesConfigV1Schema = z.strictObject({
8
+ version: z.literal(1),
9
+ valueByteLength: z.number().int().positive(),
10
+ bucketTimeRange: z.number().int().positive(),
11
+ });
12
+ //# sourceMappingURL=zekta-time-series-config-v1-schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zekta-time-series-config-v1-schema.js","sourceRoot":"","sources":["../../../../src/config/v1/zekta-time-series-config-v1-schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB;;;;GAIG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,CAAC,YAAY,CAAC;IAC1D,OAAO,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;IACrB,eAAe,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE;IAC5C,eAAe,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE;CAC7C,CAAC,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { ZektaTimeSeriesConfig } from '../zekta-time-series-config.ts';
2
+ /**
3
+ * The version 1 configuration.
4
+ *
5
+ * "valueByteLength": the exact byte length of every stored value.
6
+ * "bucketTimeRange": the time span covered by one storage chunk.
7
+ */
8
+ export interface ZektaTimeSeriesConfigV1 extends ZektaTimeSeriesConfig<1> {
9
+ readonly valueByteLength: number;
10
+ readonly bucketTimeRange: number;
11
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=zekta-time-series-config-v1.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zekta-time-series-config-v1.js","sourceRoot":"","sources":["../../../../src/config/v1/zekta-time-series-config-v1.ts"],"names":[],"mappings":""}