ticlo-tsdb 0.1.0 → 0.3.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.
@@ -0,0 +1,20 @@
1
+ import type { ChunkSizeOptions, RollupTier, StorageLimitOptions } from './types.ts';
2
+ export declare function resolveChunkSizes(chunkSize?: ChunkSizeOptions, limits?: StorageLimitOptions): Required<ChunkSizeOptions>;
3
+ /**
4
+ * Max-min fair water-filling across populated rollup tiers so all active tiers
5
+ * converge toward equal byte budgets while smaller tiers keep 100% of their data.
6
+ */
7
+ export declare function computeTierTargets(tierBytes: Record<RollupTier, number>, targetTotalBytes: number): Record<RollupTier, number>;
8
+ export interface EvictionCandidate<T> {
9
+ owner: T;
10
+ id: number;
11
+ minKey: number;
12
+ maxKey: number;
13
+ byteSize: number;
14
+ isLastChunkOfOwner: boolean;
15
+ }
16
+ /**
17
+ * Plans whole-chunk eviction across owners in roughly aligned time order
18
+ * (`maxKey asc`, `minKey asc`) while protecting the live head chunk of active owners.
19
+ */
20
+ export declare function planChunkEviction<T>(candidates: ReadonlyArray<EvictionCandidate<T>>, currentBytes: number, targetBytes: number): Map<T, number[]>;
@@ -0,0 +1,101 @@
1
+ const DEFAULT_RAW_NUMERIC_CHUNK_BYTES = 65_536;
2
+ const DEFAULT_RAW_VALUE_CHUNK_BYTES = 65_536;
3
+ const DEFAULT_ROLLUP_CHUNK_SLOTS = 512;
4
+ const ROLLUP_HEADER_BYTES = 64;
5
+ const ROLLUP_SLOT_BYTES = 56;
6
+ const TIERS_ORDER = ['15s', '5m', '1h', '1d'];
7
+ export function resolveChunkSizes(chunkSize, limits) {
8
+ const rawNumericChunkBytes = chunkSize?.rawNumericChunkBytes !== undefined
9
+ ? Math.max(48, Math.floor(chunkSize.rawNumericChunkBytes))
10
+ : limits?.rawNumericMaxBytes !== undefined
11
+ ? Math.min(DEFAULT_RAW_NUMERIC_CHUNK_BYTES, Math.max(1024, Math.floor(limits.rawNumericMaxBytes / 16)))
12
+ : DEFAULT_RAW_NUMERIC_CHUNK_BYTES;
13
+ const rawValueChunkBytes = chunkSize?.rawValueChunkBytes !== undefined
14
+ ? Math.max(128, Math.floor(chunkSize.rawValueChunkBytes))
15
+ : limits?.rawValueMaxBytes !== undefined
16
+ ? Math.min(DEFAULT_RAW_VALUE_CHUNK_BYTES, Math.max(1024, Math.floor(limits.rawValueMaxBytes / 16)))
17
+ : DEFAULT_RAW_VALUE_CHUNK_BYTES;
18
+ const rollupChunkSlots = chunkSize?.rollupChunkSlots !== undefined
19
+ ? Math.max(1, Math.floor(chunkSize.rollupChunkSlots))
20
+ : limits?.rollupMaxBytes !== undefined
21
+ ? Math.max(4, Math.min(DEFAULT_ROLLUP_CHUNK_SLOTS, Math.floor((Math.floor(limits.rollupMaxBytes / 32) - ROLLUP_HEADER_BYTES) / ROLLUP_SLOT_BYTES)))
22
+ : DEFAULT_ROLLUP_CHUNK_SLOTS;
23
+ return {
24
+ rawNumericChunkBytes,
25
+ rawValueChunkBytes,
26
+ rollupChunkSlots,
27
+ };
28
+ }
29
+ /**
30
+ * Max-min fair water-filling across populated rollup tiers so all active tiers
31
+ * converge toward equal byte budgets while smaller tiers keep 100% of their data.
32
+ */
33
+ export function computeTierTargets(tierBytes, targetTotalBytes) {
34
+ const targets = {
35
+ '15s': 0,
36
+ '5m': 0,
37
+ '1h': 0,
38
+ '1d': 0,
39
+ };
40
+ let remainingBudget = Math.max(0, targetTotalBytes);
41
+ const unsettled = [];
42
+ for (let i = 0; i < TIERS_ORDER.length; ++i) {
43
+ const tier = TIERS_ORDER[i];
44
+ if (tierBytes[tier] > 0) {
45
+ unsettled.push(tier);
46
+ }
47
+ }
48
+ while (unsettled.length > 0) {
49
+ const fairShare = remainingBudget / unsettled.length;
50
+ let settledAny = false;
51
+ for (let i = unsettled.length - 1; i >= 0; --i) {
52
+ const tier = unsettled[i];
53
+ if (tierBytes[tier] <= fairShare) {
54
+ targets[tier] = tierBytes[tier];
55
+ remainingBudget -= tierBytes[tier];
56
+ unsettled.splice(i, 1);
57
+ settledAny = true;
58
+ }
59
+ }
60
+ if (!settledAny) {
61
+ const cappedShare = Math.floor(remainingBudget / unsettled.length);
62
+ for (let i = 0; i < unsettled.length; ++i) {
63
+ targets[unsettled[i]] = cappedShare;
64
+ }
65
+ break;
66
+ }
67
+ }
68
+ return targets;
69
+ }
70
+ /**
71
+ * Plans whole-chunk eviction across owners in roughly aligned time order
72
+ * (`maxKey asc`, `minKey asc`) while protecting the live head chunk of active owners.
73
+ */
74
+ export function planChunkEviction(candidates, currentBytes, targetBytes) {
75
+ const drops = new Map();
76
+ if (currentBytes <= targetBytes || candidates.length === 0) {
77
+ return drops;
78
+ }
79
+ let globalNewestMinKey = Number.NEGATIVE_INFINITY;
80
+ for (let i = 0; i < candidates.length; ++i) {
81
+ if (candidates[i].minKey > globalNewestMinKey) {
82
+ globalNewestMinKey = candidates[i].minKey;
83
+ }
84
+ }
85
+ const sorted = candidates.slice().sort((a, b) => a.maxKey - b.maxKey || a.minKey - b.minKey || a.id - b.id);
86
+ let remaining = currentBytes;
87
+ for (let i = 0; i < sorted.length && remaining > targetBytes; ++i) {
88
+ const c = sorted[i];
89
+ if (c.isLastChunkOfOwner && c.maxKey >= globalNewestMinKey) {
90
+ continue;
91
+ }
92
+ let list = drops.get(c.owner);
93
+ if (!list) {
94
+ list = [];
95
+ drops.set(c.owner, list);
96
+ }
97
+ list.push(c.id);
98
+ remaining -= c.byteSize;
99
+ }
100
+ return drops;
101
+ }
@@ -1,6 +1,6 @@
1
1
  import type { StorageBackend } from './backend.ts';
2
2
  import type { RollupTierStore } from './store.ts';
3
- import type { RollupTier } from './types.ts';
3
+ import type { RollupTier, StorageChunkInfo } from './types.ts';
4
4
  export declare const ROLLUP_HEADER_BYTES = 64;
5
5
  export declare const ROLLUP_SLOT_FLOATS = 7;
6
6
  export declare const ROLLUP_SLOT_BYTES: number;
@@ -14,30 +14,49 @@ export declare const SLOT_FINAL = 6;
14
14
  export declare function makeGapSlotsBuffer(numSlots: number): Float64Array;
15
15
  export declare class RollupFile implements RollupTierStore {
16
16
  readonly tier: RollupTier;
17
- private readonly relPath;
17
+ private readonly relPrefix;
18
18
  private readonly backend;
19
- private handle;
20
- baseSlotId: number;
21
- slotCount: number;
22
- hasData: boolean;
19
+ private slotsPerChunk;
20
+ private chunks;
21
+ private nextChunkId;
22
+ private activeHandle;
23
+ private activeChunkId;
24
+ private activeDirtyForManifest;
25
+ private _byteSize;
26
+ private opened;
23
27
  private readonly headerBuf;
24
28
  private readonly headerU8;
25
29
  private readonly headerView;
26
- constructor(backend: StorageBackend, relPath: string, tier: RollupTier);
30
+ constructor(backend: StorageBackend, relPrefix: string, tier: RollupTier, slotsPerChunk?: number);
31
+ get hasData(): boolean;
32
+ get baseSlotId(): number;
27
33
  get endSlotId(): number;
34
+ get slotCount(): number;
35
+ get byteSize(): number;
36
+ private get manifestPath();
37
+ private chunkPath;
38
+ private chunkBucket;
28
39
  open(): Promise<void>;
29
- private writeHeader;
40
+ private writeManifest;
41
+ private readChunkHeader;
42
+ private writeChunkHeader;
43
+ private ensureActiveHandle;
44
+ private findChunkForBucket;
30
45
  readLastSlot(): Promise<{
31
46
  slotId: number;
32
47
  data: Float64Array;
33
48
  } | null>;
34
49
  writeSlots(startSlotId: number, data: Float64Array, numSlots: number): Promise<void>;
50
+ private writeIntoChunk;
35
51
  private writeAlignedFloats;
52
+ private readChunkSlice;
36
53
  readSlotRange(fromSlotId: number, toSlotIdInclusive: number): Promise<{
37
54
  firstSlotId: number;
38
55
  data: Float64Array;
39
56
  count: number;
40
57
  }>;
58
+ listChunks(): ReadonlyArray<StorageChunkInfo>;
59
+ dropChunks(chunkIds: ReadonlyArray<number>): Promise<void>;
41
60
  compact(keepFromSlotId: number): Promise<void>;
42
61
  close(): Promise<void>;
43
62
  }