@minnowdb/core 0.7.0 → 0.7.1

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 (95) hide show
  1. package/dist/block-format/block.js +254 -325
  2. package/dist/block-format/checksum.js +30 -52
  3. package/dist/block-format/codecs.js +112 -125
  4. package/dist/block-format/column.js +187 -205
  5. package/dist/block-format/index.js +5 -1
  6. package/dist/block-format/physical.js +371 -387
  7. package/dist/block-format/types.js +4 -1
  8. package/dist/block-format/unicode.js +30 -39
  9. package/dist/date-value.js +67 -78
  10. package/dist/engine/artifact-cache.js +106 -116
  11. package/dist/engine/batch.js +36 -44
  12. package/dist/engine/buffered-writer.js +156 -158
  13. package/dist/engine/byte-estimates.js +20 -22
  14. package/dist/engine/cache-limits.js +24 -25
  15. package/dist/engine/cancellation.js +5 -3
  16. package/dist/engine/catalog.js +64 -73
  17. package/dist/engine/client.js +715 -780
  18. package/dist/engine/database.js +15941 -19313
  19. package/dist/engine/defaults.js +104 -111
  20. package/dist/engine/errors.js +113 -128
  21. package/dist/engine/fts.js +233 -280
  22. package/dist/engine/group-index.js +287 -321
  23. package/dist/engine/index.js +23 -12
  24. package/dist/engine/join-index.js +177 -186
  25. package/dist/engine/keyed-live.js +168 -173
  26. package/dist/engine/live-api.js +0 -1
  27. package/dist/engine/live-equal.js +32 -37
  28. package/dist/engine/live.js +603 -647
  29. package/dist/engine/memory.js +109 -111
  30. package/dist/engine/optimizer.d.ts +19 -1
  31. package/dist/engine/optimizer.js +2633 -2934
  32. package/dist/engine/point-read.js +122 -182
  33. package/dist/engine/query-api.js +12 -3
  34. package/dist/engine/query-cache.js +61 -69
  35. package/dist/engine/query.d.ts +6 -0
  36. package/dist/engine/query.js +8503 -9980
  37. package/dist/engine/result-wire.js +221 -252
  38. package/dist/engine/schema-wire.js +97 -112
  39. package/dist/engine/schema.js +1099 -1254
  40. package/dist/engine/sort-keys.js +315 -389
  41. package/dist/engine/sql-domains.js +646 -769
  42. package/dist/engine/sql-driver.js +0 -1
  43. package/dist/engine/sql-functions.js +1031 -1125
  44. package/dist/engine/sql-json.js +199 -218
  45. package/dist/engine/sql-semantics.js +572 -639
  46. package/dist/engine/typed-live.js +271 -293
  47. package/dist/engine/vector.js +4484 -5145
  48. package/dist/engine/worker-host.d.ts +8 -63
  49. package/dist/engine/worker-host.js +28 -1130
  50. package/dist/engine/worker-indexeddb.d.ts +1 -0
  51. package/dist/engine/worker-indexeddb.js +3 -0
  52. package/dist/engine/worker-memory.d.ts +1 -0
  53. package/dist/engine/worker-memory.js +3 -0
  54. package/dist/engine/worker-opfs.d.ts +1 -0
  55. package/dist/engine/worker-opfs.js +3 -0
  56. package/dist/engine/worker-server.d.ts +93 -0
  57. package/dist/engine/worker-server.js +999 -0
  58. package/dist/engine/worker-store-indexeddb.d.ts +2 -0
  59. package/dist/engine/worker-store-indexeddb.js +11 -0
  60. package/dist/engine/worker-store-memory.d.ts +2 -0
  61. package/dist/engine/worker-store-memory.js +6 -0
  62. package/dist/engine/worker-store-opfs.d.ts +2 -0
  63. package/dist/engine/worker-store-opfs.js +9 -0
  64. package/dist/engine/worker.js +0 -12
  65. package/dist/engine/write-block-planner.js +93 -100
  66. package/dist/plan/index.js +15 -16
  67. package/dist/plan/model.d.ts +6 -0
  68. package/dist/plan/model.js +20 -27
  69. package/dist/storage/index.js +0 -12
  70. package/dist/storage/indexeddb.js +11776 -13171
  71. package/dist/storage/memory.js +1198 -1241
  72. package/dist/storage/opfs/files.js +238 -295
  73. package/dist/storage/opfs/index.js +9 -3
  74. package/dist/storage/opfs/leader.js +4386 -4856
  75. package/dist/storage/opfs/rpc.js +223 -259
  76. package/dist/storage/opfs/snapshot-ledger.js +219 -260
  77. package/dist/storage/opfs/store.js +884 -1003
  78. package/dist/storage/persistence.js +59 -74
  79. package/dist/storage/snapshot-stream.js +788 -826
  80. package/dist/storage/snapshot.js +36 -37
  81. package/dist/storage/toolkit/extents.js +444 -520
  82. package/dist/storage/toolkit/index.js +28 -29
  83. package/dist/storage/toolkit/record-core.js +5792 -6377
  84. package/dist/storage/toolkit/sync-file.js +34 -42
  85. package/dist/storage/toolkit/wal.js +92 -127
  86. package/dist/storage/toolkit/wire.js +232 -256
  87. package/dist/storage/types.js +2948 -3250
  88. package/dist/testing/block-store-conformance.js +1556 -1629
  89. package/dist/testing/index.js +310 -291
  90. package/dist/testing/opfs-shim.js +280 -312
  91. package/dist/testing/simulator.js +496 -534
  92. package/dist/testing/sqllogictest.js +425 -365
  93. package/dist/transactions/index.js +1464 -1712
  94. package/dist/worker-protocol/index.js +70 -69
  95. package/package.json +20 -2
@@ -0,0 +1,2 @@
1
+ import { type WorkerStoreFactory } from "./worker-server.js";
2
+ export declare const indexedDbWorkerStore: WorkerStoreFactory;
@@ -0,0 +1,11 @@
1
+ import { IndexedDbBlockStore } from "../storage/indexeddb.js";
2
+ import { singleStoreFactory } from "./worker-server.js";
3
+ const indexedDbWorkerStore = singleStoreFactory("indexeddb", (descriptor, options) => IndexedDbBlockStore.open({
4
+ name: descriptor.name,
5
+ ...descriptor.durability === void 0 ? {} : { durability: descriptor.durability },
6
+ ...descriptor.uniqueKeyCacheBytes === void 0 ? {} : { uniqueKeyCacheBytes: descriptor.uniqueKeyCacheBytes },
7
+ ...options.indexedDB === void 0 ? {} : { indexedDB: options.indexedDB }
8
+ }));
9
+ export {
10
+ indexedDbWorkerStore
11
+ };
@@ -0,0 +1,2 @@
1
+ import { type WorkerStoreFactory } from "./worker-server.js";
2
+ export declare const memoryWorkerStore: WorkerStoreFactory;
@@ -0,0 +1,6 @@
1
+ import { MemoryBlockStore } from "../storage/memory.js";
2
+ import { singleStoreFactory } from "./worker-server.js";
3
+ const memoryWorkerStore = singleStoreFactory("memory", () => new MemoryBlockStore());
4
+ export {
5
+ memoryWorkerStore
6
+ };
@@ -0,0 +1,2 @@
1
+ import { type WorkerStoreFactory } from "./worker-server.js";
2
+ export declare const opfsWorkerStore: WorkerStoreFactory;
@@ -0,0 +1,9 @@
1
+ import { OpfsBlockStore } from "../storage/opfs/index.js";
2
+ import { singleStoreFactory } from "./worker-server.js";
3
+ const opfsWorkerStore = singleStoreFactory("opfs", (descriptor) => OpfsBlockStore.open({
4
+ name: descriptor.name,
5
+ ...descriptor.durability === void 0 ? {} : { durability: descriptor.durability }
6
+ }));
7
+ export {
8
+ opfsWorkerStore
9
+ };
@@ -1,14 +1,2 @@
1
- /// <reference lib="webworker" />
2
- /**
3
- * Ready-made dedicated-worker entry, published as `@minnowdb/core/worker`. Point a
4
- * module worker at it and connect a MinnowDatabaseClient — the client's init frame carries the
5
- * store descriptor and options, so this entry needs no configuration of its own:
6
- *
7
- * // db-worker.ts: import "@minnowdb/core/worker";
8
- * new Worker(new URL("./db-worker.ts", import.meta.url), { type: "module" })
9
- *
10
- * For non-cloneable construction options (custom store, `now`, `createId`), write your own entry
11
- * and import `exposeDatabase()` from `@minnowdb/core/worker-host` instead.
12
- */
13
1
  import { attachDatabaseWorker } from "./worker-host.js";
14
2
  attachDatabaseWorker(self);
@@ -1,112 +1,105 @@
1
- import { MAX_BLOCK_ROW_COUNT, physicalColumnByteLength, wellFormedUtf8ByteLength, } from "../block-format/index.js";
2
- /**
3
- * Plans aligned row groups for every column in one segment. The row-wise accumulator visits each
4
- * string value once, plus at most one overflow recheck at a block boundary, so a late wide column
5
- * cannot make its siblings rescan the rest of the batch for every output block.
6
- */
7
- export function planAlignedWriteBlockRanges(columns, rowCount, maximumRows, targetBytes, measureString = wellFormedUtf8ByteLength) {
8
- if (rowCount === 0)
9
- return [];
10
- if (!Number.isSafeInteger(maximumRows) || maximumRows <= 0 || maximumRows > MAX_BLOCK_ROW_COUNT) {
11
- throw new RangeError("Maximum rows per write block exceeds the format limit");
1
+ import { MAX_BLOCK_ROW_COUNT, physicalColumnByteLength, wellFormedUtf8ByteLength } from "../block-format/index.js";
2
+ function planAlignedWriteBlockRanges(columns, rowCount, maximumRows, targetBytes, measureString = wellFormedUtf8ByteLength) {
3
+ if (rowCount === 0)
4
+ return [];
5
+ if (!Number.isSafeInteger(maximumRows) || maximumRows <= 0 || maximumRows > MAX_BLOCK_ROW_COUNT) {
6
+ throw new RangeError("Maximum rows per write block exceeds the format limit");
7
+ }
8
+ for (const column of columns) {
9
+ if (column.values.length !== rowCount) {
10
+ throw new RangeError("Column lengths must match while planning write blocks");
12
11
  }
13
- for (const column of columns) {
14
- if (column.values.length !== rowCount) {
15
- throw new RangeError("Column lengths must match while planning write blocks");
16
- }
12
+ }
13
+ let maximumRowsPerRange = maximumRows;
14
+ const stringColumns = [];
15
+ for (const column of columns) {
16
+ if (column.type === "string")
17
+ stringColumns.push(column);
18
+ else {
19
+ maximumRowsPerRange = Math.min(maximumRowsPerRange, maximumFixedWidthRows(column.type, maximumRows, targetBytes));
17
20
  }
18
- let maximumRowsPerRange = maximumRows;
19
- const stringColumns = [];
20
- for (const column of columns) {
21
- if (column.type === "string")
22
- stringColumns.push(column);
23
- else {
24
- maximumRowsPerRange = Math.min(maximumRowsPerRange, maximumFixedWidthRows(column.type, maximumRows, targetBytes));
25
- }
21
+ }
22
+ const ranges = [];
23
+ if (stringColumns.length === 0) {
24
+ for (let start = 0; start < rowCount; start += maximumRowsPerRange) {
25
+ ranges.push({ start, end: Math.min(rowCount, start + maximumRowsPerRange) });
26
26
  }
27
- const ranges = [];
28
- if (stringColumns.length === 0) {
29
- // Fixed-width sizes depend only on row count. Do not add a value-reading pass to numeric and
30
- // boolean writes: calculate the cap once and emit the arithmetic ranges directly.
31
- for (let start = 0; start < rowCount; start += maximumRowsPerRange) {
32
- ranges.push({ start, end: Math.min(rowCount, start + maximumRowsPerRange) });
27
+ return ranges;
28
+ }
29
+ for (let start = 0; start < rowCount; ) {
30
+ const rowLimit = Math.min(rowCount, start + maximumRowsPerRange);
31
+ const stringContentBytes = new Array(stringColumns.length).fill(0);
32
+ let end = start;
33
+ for (let row = start; row < rowLimit; row += 1) {
34
+ const count = row - start + 1;
35
+ let fitsTarget = true;
36
+ for (let columnIndex = 0; columnIndex < stringColumns.length; columnIndex += 1) {
37
+ const column = stringColumns[columnIndex];
38
+ if (column === void 0)
39
+ continue;
40
+ let contentBytes = stringContentBytes[columnIndex] ?? 0;
41
+ const value = column.values[row];
42
+ if (value !== null) {
43
+ if (typeof value !== "string") {
44
+ throw new TypeError("String column contains a non-string");
45
+ }
46
+ contentBytes += column.stringByteLengths?.[row] ?? measureString(value);
47
+ stringContentBytes[columnIndex] = contentBytes;
33
48
  }
34
- return ranges;
35
- }
36
- for (let start = 0; start < rowCount;) {
37
- const rowLimit = Math.min(rowCount, start + maximumRowsPerRange);
38
- const stringContentBytes = new Array(stringColumns.length).fill(0);
39
- let end = start;
40
- for (let row = start; row < rowLimit; row += 1) {
41
- const count = row - start + 1;
42
- let fitsTarget = true;
43
- for (let columnIndex = 0; columnIndex < stringColumns.length; columnIndex += 1) {
44
- const column = stringColumns[columnIndex];
45
- if (column === undefined)
46
- continue;
47
- let contentBytes = stringContentBytes[columnIndex] ?? 0;
48
- const value = column.values[row];
49
- if (value !== null) {
50
- if (typeof value !== "string") {
51
- throw new TypeError("String column contains a non-string");
52
- }
53
- contentBytes += column.stringByteLengths?.[row] ?? measureString(value);
54
- stringContentBytes[columnIndex] = contentBytes;
55
- }
56
- if (physicalColumnBytesUnchecked("string", count, contentBytes) > targetBytes) {
57
- fitsTarget = false;
58
- }
59
- }
60
- if (!fitsTarget && count > 1)
61
- break;
62
- if (!fitsTarget) {
63
- // The target is soft for one wide row, but the format ceiling is always hard. Validate
64
- // every string column's exact one-row size before allowing that row to make progress.
65
- for (let columnIndex = 0; columnIndex < stringColumns.length; columnIndex += 1) {
66
- physicalColumnByteLength("string", 1, stringContentBytes[columnIndex] ?? 0);
67
- }
68
- }
69
- end = row + 1;
70
- if (!fitsTarget)
71
- break;
49
+ if (physicalColumnBytesUnchecked("string", count, contentBytes) > targetBytes) {
50
+ fitsTarget = false;
72
51
  }
73
- if (end <= start)
74
- throw new Error("Write block planner made no progress");
75
- ranges.push({ start, end });
76
- start = end;
52
+ }
53
+ if (!fitsTarget && count > 1)
54
+ break;
55
+ if (!fitsTarget) {
56
+ for (let columnIndex = 0; columnIndex < stringColumns.length; columnIndex += 1) {
57
+ physicalColumnByteLength("string", 1, stringContentBytes[columnIndex] ?? 0);
58
+ }
59
+ }
60
+ end = row + 1;
61
+ if (!fitsTarget)
62
+ break;
77
63
  }
78
- return ranges;
64
+ if (end <= start)
65
+ throw new Error("Write block planner made no progress");
66
+ ranges.push({ start, end });
67
+ start = end;
68
+ }
69
+ return ranges;
79
70
  }
80
71
  function maximumFixedWidthRows(type, maximumRows, targetBytes) {
81
- if (physicalColumnBytesUnchecked(type, maximumRows, 0) <= targetBytes)
82
- return maximumRows;
83
- if (physicalColumnBytesUnchecked(type, 1, 0) > targetBytes)
84
- return 1;
85
- let low = 1;
86
- let high = maximumRows;
87
- while (low < high) {
88
- const middle = Math.ceil((low + high) / 2);
89
- if (physicalColumnBytesUnchecked(type, middle, 0) <= targetBytes)
90
- low = middle;
91
- else
92
- high = middle - 1;
93
- }
94
- return low;
72
+ if (physicalColumnBytesUnchecked(type, maximumRows, 0) <= targetBytes)
73
+ return maximumRows;
74
+ if (physicalColumnBytesUnchecked(type, 1, 0) > targetBytes)
75
+ return 1;
76
+ let low = 1;
77
+ let high = maximumRows;
78
+ while (low < high) {
79
+ const middle = Math.ceil((low + high) / 2);
80
+ if (physicalColumnBytesUnchecked(type, middle, 0) <= targetBytes)
81
+ low = middle;
82
+ else
83
+ high = middle - 1;
84
+ }
85
+ return low;
95
86
  }
96
- /** Conservative first estimate used by both physical compaction planners. */
97
- export function estimateCompactionRowsPerOutput(targetBlockBytes, maximumEncodedBytesPerRow) {
98
- return Math.max(1, Math.min(MAX_BLOCK_ROW_COUNT, Math.floor(targetBlockBytes / maximumEncodedBytesPerRow)));
87
+ function estimateCompactionRowsPerOutput(targetBlockBytes, maximumEncodedBytesPerRow) {
88
+ return Math.max(1, Math.min(MAX_BLOCK_ROW_COUNT, Math.floor(targetBlockBytes / maximumEncodedBytesPerRow)));
99
89
  }
100
- /** Exact physical size without applying the 64 MiB hard limit during range search. */
101
90
  function physicalColumnBytesUnchecked(type, rowCount, stringContentBytes) {
102
- const validityBytes = Math.ceil(rowCount / 8);
103
- switch (type) {
104
- case "boolean":
105
- return validityBytes * 2;
106
- case "number":
107
- case "datetime":
108
- return validityBytes + rowCount * 8;
109
- case "string":
110
- return validityBytes + (rowCount + 1) * 4 + stringContentBytes;
111
- }
91
+ const validityBytes = Math.ceil(rowCount / 8);
92
+ switch (type) {
93
+ case "boolean":
94
+ return validityBytes * 2;
95
+ case "number":
96
+ case "datetime":
97
+ return validityBytes + rowCount * 8;
98
+ case "string":
99
+ return validityBytes + (rowCount + 1) * 4 + stringContentBytes;
100
+ }
112
101
  }
102
+ export {
103
+ estimateCompactionRowsPerOutput,
104
+ planAlignedWriteBlockRanges
105
+ };
@@ -1,16 +1,15 @@
1
- /**
2
- * The plan-construction primitives, published so a typed query layer can be built externally.
3
- *
4
- * A query builder's whole job is to assemble the same logical plan the SQL parser assembles, and
5
- * then hand it to the engine. That requires the plan's types, the block-assembly functions the
6
- * parser itself ends in, and the validators that keep a hand-built plan as strict as a parsed one.
7
- * Exposing them here lets an external builder produce plans the engine treats as indistinguishable
8
- * from parsed SQL.
9
- *
10
- * The recursive logical model lives in the dependency-light `model` leaf; executable assembly
11
- * and validation stay with the compiler. This entry point keeps that distinction invisible to
12
- * consumers.
13
- */
14
- export { assembleSelectBlock, compoundSelectBlock, derivedTableSource, hasAggregate, splitCondition, validateLimit, validateOffset, } from "../engine/query.js";
15
- export { optimizePlan, renderPlan } from "../engine/optimizer.js";
16
- export { validateFtsQuery } from "../engine/fts.js";
1
+ import { assembleSelectBlock, compoundSelectBlock, derivedTableSource, hasAggregate, splitCondition, validateLimit, validateOffset } from "../engine/query.js";
2
+ import { optimizePlan, renderPlan } from "../engine/optimizer.js";
3
+ import { validateFtsQuery } from "../engine/fts.js";
4
+ export {
5
+ assembleSelectBlock,
6
+ compoundSelectBlock,
7
+ derivedTableSource,
8
+ hasAggregate,
9
+ optimizePlan,
10
+ renderPlan,
11
+ splitCondition,
12
+ validateFtsQuery,
13
+ validateLimit,
14
+ validateOffset
15
+ };
@@ -246,6 +246,12 @@ export interface CompiledQuery {
246
246
  pendingSetOrder?: true;
247
247
  /** Preserve `{ optimize: false }` after a deferred wildcard shape becomes concrete. */
248
248
  preserveUnoptimizedShape?: true;
249
+ /**
250
+ * Set by the optimizer on the plan it returns. Parameter binding applies the value-dependent
251
+ * rewrites (calendar equalities) only to optimized plans, so `{ optimize: false }` stays the
252
+ * untouched oracle for them.
253
+ */
254
+ optimized?: true;
249
255
  distinctWildcard?: boolean;
250
256
  limitParameter?: number;
251
257
  offsetParameter?: number;
@@ -1,29 +1,22 @@
1
- /**
2
- * A cross join rides the nested-loop inner-join path with a condition every row pair satisfies:
3
- * 1 = 1 over null literal key expressions. Producers build the shape with crossJoinPlan and
4
- * consumers recognize it with isCrossJoinPlan, so the encoding lives in exactly one place.
5
- */
6
- export function crossJoinPlan(source) {
7
- return {
8
- ...source,
9
- kind: "inner",
10
- left: { kind: "literal", value: null },
11
- right: { kind: "literal", value: null },
12
- on: {
13
- kind: "condition",
14
- operator: "=",
15
- left: { kind: "literal", value: 1 },
16
- right: { kind: "literal", value: 1 },
17
- },
18
- };
1
+ function crossJoinPlan(source) {
2
+ return {
3
+ ...source,
4
+ kind: "inner",
5
+ left: { kind: "literal", value: null },
6
+ right: { kind: "literal", value: null },
7
+ on: {
8
+ kind: "condition",
9
+ operator: "=",
10
+ left: { kind: "literal", value: 1 },
11
+ right: { kind: "literal", value: 1 }
12
+ }
13
+ };
19
14
  }
20
- export function isCrossJoinPlan(join) {
21
- const condition = join.on;
22
- return (join.kind === "inner" &&
23
- condition?.kind === "condition" &&
24
- condition.operator === "=" &&
25
- condition.left.kind === "literal" &&
26
- condition.left.value === 1 &&
27
- condition.right.kind === "literal" &&
28
- condition.right.value === 1);
15
+ function isCrossJoinPlan(join) {
16
+ const condition = join.on;
17
+ return join.kind === "inner" && condition?.kind === "condition" && condition.operator === "=" && condition.left.kind === "literal" && condition.left.value === 1 && condition.right.kind === "literal" && condition.right.value === 1;
29
18
  }
19
+ export {
20
+ crossJoinPlan,
21
+ isCrossJoinPlan
22
+ };
@@ -1,15 +1,3 @@
1
- /**
2
- * The storage surface, in three layers with enforced dependency directions
3
- * (contract-boundaries.test.ts):
4
- *
5
- * - **The contract** — `types.ts` and `snapshot.ts`: the `BlockStore` capability interfaces,
6
- * record types, and error classes. The engine depends on nothing else here.
7
- * - **The adapters** — `indexeddb.ts`, `memory.ts`, `opfs/`: implementations, each owning its
8
- * own low-level strategy.
9
- * - **The toolkit** — `toolkit/`, published as `@minnowdb/core/storage/toolkit` rather than
10
- * from this barrel: optional building blocks for writing new adapters. The contract never
11
- * references it.
12
- */
13
1
  export * from "./indexeddb.js";
14
2
  export * from "./memory.js";
15
3
  export * from "./opfs/index.js";