@bjornpagen/bumbledb 1.0.1 → 1.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.
Files changed (172) hide show
  1. package/COOKBOOK.md +176 -36
  2. package/README.md +70 -26
  3. package/dist/capacity.d.ts +15 -55
  4. package/dist/capacity.d.ts.map +1 -1
  5. package/dist/capacity.js +63 -12
  6. package/dist/capacity.js.map +1 -1
  7. package/dist/changes.d.ts +28 -6
  8. package/dist/changes.d.ts.map +1 -1
  9. package/dist/changes.js +119 -71
  10. package/dist/changes.js.map +1 -1
  11. package/dist/closed.d.ts +23 -51
  12. package/dist/closed.d.ts.map +1 -1
  13. package/dist/closed.js +110 -168
  14. package/dist/closed.js.map +1 -1
  15. package/dist/codec.d.ts +8 -14
  16. package/dist/codec.d.ts.map +1 -1
  17. package/dist/codec.js +113 -188
  18. package/dist/codec.js.map +1 -1
  19. package/dist/compile.d.ts +17 -18
  20. package/dist/compile.d.ts.map +1 -1
  21. package/dist/compile.js +41 -20
  22. package/dist/compile.js.map +1 -1
  23. package/dist/db-native.d.ts +76 -41
  24. package/dist/db-native.d.ts.map +1 -1
  25. package/dist/db-native.js +8 -0
  26. package/dist/db-native.js.map +1 -1
  27. package/dist/db.d.ts +61 -26
  28. package/dist/db.d.ts.map +1 -1
  29. package/dist/db.js +152 -85
  30. package/dist/db.js.map +1 -1
  31. package/dist/errors.d.ts +11 -3
  32. package/dist/errors.d.ts.map +1 -1
  33. package/dist/errors.js +13 -1
  34. package/dist/errors.js.map +1 -1
  35. package/dist/face.d.ts +19 -25
  36. package/dist/face.d.ts.map +1 -1
  37. package/dist/face.js +30 -24
  38. package/dist/face.js.map +1 -1
  39. package/dist/fields.d.ts +13 -26
  40. package/dist/fields.d.ts.map +1 -1
  41. package/dist/fields.js +89 -116
  42. package/dist/fields.js.map +1 -1
  43. package/dist/immutable.d.ts +10 -0
  44. package/dist/immutable.d.ts.map +1 -0
  45. package/dist/immutable.js +75 -0
  46. package/dist/immutable.js.map +1 -0
  47. package/dist/index.d.ts +16 -12
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +6 -4
  50. package/dist/index.js.map +1 -1
  51. package/dist/internal/log.d.ts +6 -4
  52. package/dist/internal/log.d.ts.map +1 -1
  53. package/dist/internal/log.js +3 -1
  54. package/dist/internal/log.js.map +1 -1
  55. package/dist/law.d.ts +11 -11
  56. package/dist/law.d.ts.map +1 -1
  57. package/dist/law.js +1 -1
  58. package/dist/law.js.map +1 -1
  59. package/dist/lower.d.ts.map +1 -1
  60. package/dist/lower.js +11 -6
  61. package/dist/lower.js.map +1 -1
  62. package/dist/migration.d.ts +2 -3
  63. package/dist/migration.d.ts.map +1 -1
  64. package/dist/migration.js +5 -5
  65. package/dist/migration.js.map +1 -1
  66. package/dist/native.d.ts +2 -2
  67. package/dist/native.d.ts.map +1 -1
  68. package/dist/native.js.map +1 -1
  69. package/dist/query/atom.d.ts +1 -1
  70. package/dist/query/atom.d.ts.map +1 -1
  71. package/dist/query/atom.js.map +1 -1
  72. package/dist/query/description.d.ts +33 -0
  73. package/dist/query/description.d.ts.map +1 -0
  74. package/dist/query/description.js +302 -0
  75. package/dist/query/description.js.map +1 -0
  76. package/dist/query/lower.d.ts +60 -4
  77. package/dist/query/lower.d.ts.map +1 -1
  78. package/dist/query/lower.js +126 -161
  79. package/dist/query/lower.js.map +1 -1
  80. package/dist/query/parse-ir.d.ts +7 -2
  81. package/dist/query/parse-ir.d.ts.map +1 -1
  82. package/dist/query/parse-ir.js +240 -53
  83. package/dist/query/parse-ir.js.map +1 -1
  84. package/dist/query/run.d.ts.map +1 -1
  85. package/dist/query/run.js +8 -10
  86. package/dist/query/run.js.map +1 -1
  87. package/dist/query/scope.d.ts +3 -4
  88. package/dist/query/scope.d.ts.map +1 -1
  89. package/dist/query/scope.js +5 -10
  90. package/dist/query/scope.js.map +1 -1
  91. package/dist/relation.d.ts +22 -48
  92. package/dist/relation.d.ts.map +1 -1
  93. package/dist/relation.js +30 -82
  94. package/dist/relation.js.map +1 -1
  95. package/dist/result.d.ts +10 -25
  96. package/dist/result.d.ts.map +1 -1
  97. package/dist/result.js +7 -12
  98. package/dist/result.js.map +1 -1
  99. package/dist/rows.d.ts +11 -14
  100. package/dist/rows.d.ts.map +1 -1
  101. package/dist/rows.js +49 -237
  102. package/dist/rows.js.map +1 -1
  103. package/dist/runtime-codes.d.ts +1 -1
  104. package/dist/runtime-codes.d.ts.map +1 -1
  105. package/dist/runtime-codes.js +1 -2
  106. package/dist/runtime-codes.js.map +1 -1
  107. package/dist/runtime-errors.d.ts +27 -15
  108. package/dist/runtime-errors.d.ts.map +1 -1
  109. package/dist/runtime-errors.js +44 -8
  110. package/dist/runtime-errors.js.map +1 -1
  111. package/dist/runtime-native.d.ts +16 -34
  112. package/dist/runtime-native.d.ts.map +1 -1
  113. package/dist/runtime-native.js.map +1 -1
  114. package/dist/runtime.d.ts +11 -28
  115. package/dist/runtime.d.ts.map +1 -1
  116. package/dist/runtime.js +14 -54
  117. package/dist/runtime.js.map +1 -1
  118. package/dist/schema.d.ts +9 -1
  119. package/dist/schema.d.ts.map +1 -1
  120. package/dist/schema.js +85 -24
  121. package/dist/schema.js.map +1 -1
  122. package/dist/selection.d.ts +27 -0
  123. package/dist/selection.d.ts.map +1 -0
  124. package/dist/selection.js +104 -0
  125. package/dist/selection.js.map +1 -0
  126. package/dist/shape.d.ts +6 -9
  127. package/dist/shape.d.ts.map +1 -1
  128. package/dist/statements.d.ts +17 -52
  129. package/dist/statements.d.ts.map +1 -1
  130. package/dist/statements.js +92 -111
  131. package/dist/statements.js.map +1 -1
  132. package/dist/values.d.ts +25 -0
  133. package/dist/values.d.ts.map +1 -0
  134. package/dist/values.js +203 -0
  135. package/dist/values.js.map +1 -0
  136. package/pack-provenance.json +3 -3
  137. package/package.json +4 -4
  138. package/src/capacity.ts +86 -76
  139. package/src/changes.ts +225 -94
  140. package/src/closed.ts +161 -314
  141. package/src/codec.ts +123 -213
  142. package/src/compile.ts +61 -38
  143. package/src/db-native.ts +73 -68
  144. package/src/db.ts +268 -165
  145. package/src/errors.ts +16 -1
  146. package/src/face.ts +44 -74
  147. package/src/fields.ts +100 -146
  148. package/src/immutable.ts +76 -0
  149. package/src/index.ts +38 -31
  150. package/src/internal/log.ts +4 -5
  151. package/src/law.ts +14 -14
  152. package/src/lower.ts +13 -10
  153. package/src/migration.ts +5 -12
  154. package/src/native.ts +2 -1
  155. package/src/query/atom.ts +1 -0
  156. package/src/query/description.ts +394 -0
  157. package/src/query/lower.ts +139 -173
  158. package/src/query/parse-ir.ts +247 -54
  159. package/src/query/run.ts +15 -9
  160. package/src/query/scope.ts +10 -18
  161. package/src/relation.ts +58 -142
  162. package/src/result.ts +16 -45
  163. package/src/rows.ts +57 -250
  164. package/src/runtime-codes.ts +1 -2
  165. package/src/runtime-errors.ts +48 -8
  166. package/src/runtime-native.ts +13 -46
  167. package/src/runtime.ts +51 -119
  168. package/src/schema.ts +99 -29
  169. package/src/selection.ts +124 -0
  170. package/src/shape.ts +9 -9
  171. package/src/statements.ts +138 -189
  172. package/src/values.ts +223 -0
package/src/db-native.ts CHANGED
@@ -2,22 +2,23 @@
2
2
  * Private database bridge over worker-owned core snapshots and results.
3
3
  * The implementation lives in `ts/crate/src/`; this is not a public API.
4
4
  * Every verb runs on the shared bounded executor, registers under its
5
- * operation accounting, takes a `PolicyWire` converted once at admission,
6
- * and completes through the registered callback; `runtimeCancel` cancels
5
+ * operation ownership and completes through the registered callback; `runtimeCancel` cancels
7
6
  * and joins any of them. Close verbs report the real drain outcome through
8
7
  * `CloseWire`.
9
8
  */
9
+
10
+ import type { ChangeCounts } from "#changes.ts"
10
11
  import type { DbHandle, ParsedQuery, QueryParam, SealedDescriptor, Violation } from "#native.ts"
11
12
  import { native } from "#native.ts"
12
13
  import type { CellValue } from "#rows.ts"
13
- import type { CloseWire, OperationHandle, PolicyWire, RuntimeHandle } from "#runtime-native.ts"
14
+ import type { CloseWire, OperationHandle, RuntimeHandle } from "#runtime-native.ts"
14
15
  import type { SchemaSpec } from "#spec.ts"
15
16
 
16
17
  export interface SnapshotHandle {
17
18
  readonly __snapshot: unique symbol
18
19
  }
19
- export interface SessionHandle {
20
- readonly __session: unique symbol
20
+ export interface PreparedHandle {
21
+ readonly __prepared: unique symbol
21
22
  }
22
23
  export interface ResultHandle {
23
24
  readonly __result: unique symbol
@@ -31,6 +32,9 @@ export interface DraftHandle {
31
32
  export interface ChangesHandle {
32
33
  readonly __changes: unique symbol
33
34
  }
35
+ export interface ChangesCursorHandle {
36
+ readonly __changesCursor: unique symbol
37
+ }
34
38
 
35
39
  /** The core-local witness: catalog/store identity plus generation, never a StateStamp. */
36
40
  export interface WitnessWire {
@@ -48,13 +52,20 @@ export type ApplyOutcomeWire =
48
52
  | { readonly tag: "invariant-rejected"; readonly violations: readonly Violation[] }
49
53
  | { readonly tag: "moved"; readonly witnessed: WitnessWire; readonly current: WitnessWire }
50
54
 
55
+ export type JudgeOutcomeWire =
56
+ | { readonly tag: "admitted"; readonly base: WitnessWire; readonly changes: ChangeCounts }
57
+ | {
58
+ readonly tag: "invariant-rejected"
59
+ readonly base: WitnessWire
60
+ readonly changes: ChangeCounts
61
+ readonly violations: readonly Violation[]
62
+ }
63
+ | { readonly tag: "moved"; readonly witnessed: WitnessWire; readonly current: WitnessWire }
64
+
51
65
  /** Bounded database diagnostics: measurements, never retained row payloads. */
52
66
  export interface DbInspectionWire {
53
67
  readonly generation: bigint
54
- readonly mapBytes: bigint
55
- readonly populatedBytes: bigint
56
- readonly diskBytes: bigint
57
- readonly residentEstimateBytes: bigint
68
+ readonly storage: import("#db.ts").StorageInspection
58
69
  readonly retainedOperations: bigint
59
70
  }
60
71
 
@@ -66,6 +77,14 @@ export interface SnapshotWire {
66
77
  export interface ChangesWire {
67
78
  readonly changes: ChangesHandle
68
79
  readonly fingerprint: string
80
+ readonly counts: ChangeCounts
81
+ readonly byteLength: bigint
82
+ }
83
+
84
+ export interface ChangeRecordWire {
85
+ readonly relation: number
86
+ readonly kind: "add" | "remove"
87
+ readonly values: readonly CellValue[]
69
88
  }
70
89
 
71
90
  export interface MutationReportWire {
@@ -74,29 +93,24 @@ export interface MutationReportWire {
74
93
  }
75
94
 
76
95
  interface DbBridge {
77
- /** Charged schema admission/compilation; take yields detached descriptor data. */
78
- runtimeSchemaCompile(
79
- runtime: RuntimeHandle,
80
- policy: PolicyWire,
81
- spec: SchemaSpec,
82
- callback: () => void
83
- ): OperationHandle
96
+ /** Schema admission/compilation; take yields detached descriptor data. */
97
+ runtimeSchemaCompile(runtime: RuntimeHandle, spec: SchemaSpec, callback: () => void): OperationHandle
84
98
  runtimeSchemaTake(operation: OperationHandle): SealedDescriptor
85
99
 
86
100
  /** Coherent owned snapshot acquisition off a managed database. */
87
- runtimeDbSnapshot(db: DbHandle, policy: PolicyWire, callback: () => void): OperationHandle
101
+ runtimeDbSnapshot(db: DbHandle, callback: () => void): OperationHandle
88
102
  runtimeSnapshotTake(operation: OperationHandle): SnapshotWire
89
103
  runtimeSnapshotClose(snapshot: SnapshotHandle, callback: (report: CloseWire) => void): void
90
104
 
91
- /** Reusable snapshot-bound execution session (worker-affine natively). */
92
- runtimeSnapshotSession(snapshot: SnapshotHandle, policy: PolicyWire, callback: () => void): OperationHandle
93
- runtimeSessionTake(operation: OperationHandle): SessionHandle
94
- runtimeSessionClose(session: SessionHandle, callback: (report: CloseWire) => void): void
105
+ /** One compiled query, with an independent share of the snapshot pin. */
106
+ runtimeSnapshotPrepare(snapshot: SnapshotHandle, query: ParsedQuery, callback: () => void): OperationHandle
107
+ runtimePreparedTake(operation: OperationHandle): PreparedHandle
108
+ runtimePreparedClose(prepared: PreparedHandle, callback: (report: CloseWire) => void): void
109
+ runtimePreparedReleaseMemory(prepared: PreparedHandle, callback: () => void): OperationHandle
95
110
 
96
111
  /** Exact-key point read; take yields one owned row or null (absent). */
97
112
  runtimeSnapshotGet(
98
113
  snapshot: SnapshotHandle,
99
- policy: PolicyWire,
100
114
  relationId: number,
101
115
  keyStatementId: number,
102
116
  keyCells: readonly CellValue[],
@@ -105,34 +119,25 @@ interface DbBridge {
105
119
  runtimeRowTake(operation: OperationHandle): readonly CellValue[] | null
106
120
 
107
121
  /**
108
- * Complete bounded execution. The snapshot variant owns an internal
109
- * one-shot session and closes it before publishing the result; the
110
- * session variant reuses the caller's session. Either way the result is
122
+ * Complete execution. One-shot preparation drops before publication;
123
+ * explicit preparation reuses its compiled plan and scratch. The result is
111
124
  * sealed and independent only after ALL evaluation succeeded.
112
125
  */
113
126
  runtimeSnapshotExecute(
114
127
  snapshot: SnapshotHandle,
115
- policy: PolicyWire,
116
- query: ParsedQuery,
117
- params: readonly QueryParam[],
118
- callback: () => void
119
- ): OperationHandle
120
- runtimeSessionExecute(
121
- session: SessionHandle,
122
- policy: PolicyWire,
123
128
  query: ParsedQuery,
124
129
  params: readonly QueryParam[],
125
130
  callback: () => void
126
131
  ): OperationHandle
132
+ runtimePreparedExecute(prepared: PreparedHandle, params: readonly QueryParam[], callback: () => void): OperationHandle
127
133
  runtimeResultTake(operation: OperationHandle): ResultHandle
128
134
  runtimeResultClose(result: ResultHandle, callback: (report: CloseWire) => void): void
129
135
 
130
136
  /**
131
- * Bounded total materialization: refuses (ResourceLimit) BEFORE
132
- * allocating past `policy.resultBytes`; a cap failure leaves the sealed
133
- * backing available. Take yields owned row arrays.
137
+ * Explicit full materialization into final native row arrays.
138
+ * Failure leaves the sealed backing available.
134
139
  */
135
- runtimeResultCollect(result: ResultHandle, policy: PolicyWire, callback: () => void): OperationHandle
140
+ runtimeResultCollect(result: ResultHandle, callback: () => void): OperationHandle
136
141
  runtimeRowsTake(operation: OperationHandle): readonly (readonly CellValue[])[]
137
142
 
138
143
  /**
@@ -140,25 +145,24 @@ interface DbBridge {
140
145
  * private cursor. A second transfer, or transfer racing collect,
141
146
  * refuses (SpentHandle) before touching the backing.
142
147
  */
143
- runtimeResultCursor(result: ResultHandle, policy: PolicyWire, callback: () => void): OperationHandle
148
+ runtimeResultCursor(result: ResultHandle, callback: () => void): OperationHandle
144
149
  runtimeCursorTake(operation: OperationHandle): CursorHandle
145
- /** One owned page bounded by `policy.resultBytes`; null is EOF (cursor storage reclaimed). */
146
- runtimeCursorNext(cursor: CursorHandle, policy: PolicyWire, callback: () => void): OperationHandle
150
+ /** One bounded delivery batch; null is EOF. Execution is already complete. */
151
+ runtimeCursorNext(cursor: CursorHandle, callback: () => void): OperationHandle
147
152
  runtimePageTake(operation: OperationHandle): readonly (readonly CellValue[])[] | null
148
153
  runtimeCursorClose(cursor: CursorHandle, callback: (report: CloseWire) => void): void
149
154
 
150
155
  /** Database-free draft acquisition (schema compiled/checked on the executor). */
151
- runtimeDraftOpen(runtime: RuntimeHandle, policy: PolicyWire, spec: SchemaSpec, callback: () => void): OperationHandle
156
+ runtimeDraftOpen(runtime: RuntimeHandle, spec: SchemaSpec, callback: () => void): OperationHandle
152
157
  runtimeDraftTake(operation: OperationHandle): DraftHandle
153
158
  /**
154
159
  * One bounded ingestion chunk (rows × arity cells, row-major, sealed
155
- * field order). Chunks share the draft's cumulative aggregate budget
156
- * they never reset it. Failure spends the draft and starts tracked
160
+ * field order). Rows are owned by the draft until finish. Failure spends
161
+ * the draft and starts tracked
157
162
  * drain natively.
158
163
  */
159
164
  runtimeDraftInsert(
160
165
  draft: DraftHandle,
161
- policy: PolicyWire,
162
166
  relationId: number,
163
167
  rows: bigint,
164
168
  cells: readonly CellValue[],
@@ -166,7 +170,6 @@ interface DbBridge {
166
170
  ): OperationHandle
167
171
  runtimeDraftDelete(
168
172
  draft: DraftHandle,
169
- policy: PolicyWire,
170
173
  relationId: number,
171
174
  rows: bigint,
172
175
  cells: readonly CellValue[],
@@ -174,28 +177,41 @@ interface DbBridge {
174
177
  ): OperationHandle
175
178
  runtimeReportTake(operation: OperationHandle): MutationReportWire
176
179
  /** Consumes the draft into an immutable schema-bound ChangeSet (one command, add-wins normalization). */
177
- runtimeDraftFinish(draft: DraftHandle, policy: PolicyWire, callback: () => void): OperationHandle
180
+ runtimeDraftFinish(draft: DraftHandle, callback: () => void): OperationHandle
178
181
  runtimeChangesTake(operation: OperationHandle): ChangesWire
179
182
  runtimeDraftClose(draft: DraftHandle, callback: (report: CloseWire) => void): void
180
183
  runtimeChangesClose(changes: ChangesHandle, callback: (report: CloseWire) => void): void
181
-
182
- /** One immutable final-state admission/commit under the managed owner. */
183
- runtimeDbApply(
184
- db: DbHandle,
185
- policy: PolicyWire,
186
- changes: ChangesHandle,
187
- expected: ExpectedWire,
184
+ /** Owned immutable bytes; parsing checks canonical order and the supplied schema. */
185
+ runtimeChangesBytes(changes: ChangesHandle, callback: () => void): OperationHandle
186
+ runtimeChangesParse(
187
+ runtime: RuntimeHandle,
188
+ spec: SchemaSpec,
189
+ bytes: Uint8Array,
188
190
  callback: () => void
189
191
  ): OperationHandle
192
+ /** One command's add-wins composition, not sequential application. */
193
+ runtimeChangesCompose(left: ChangesHandle, right: ChangesHandle, callback: () => void): OperationHandle
194
+ /** Independent bounded traversal; never spends the immutable source. */
195
+ runtimeChangesCursor(changes: ChangesHandle, callback: () => void): OperationHandle
196
+ runtimeChangesCursorTake(operation: OperationHandle): ChangesCursorHandle
197
+ runtimeChangesCursorNext(cursor: ChangesCursorHandle, callback: () => void): OperationHandle
198
+ runtimeChangePageTake(operation: OperationHandle): readonly ChangeRecordWire[] | null
199
+ runtimeChangesCursorClose(cursor: ChangesCursorHandle, callback: (report: CloseWire) => void): void
200
+
201
+ /** One immutable final-state admission/commit under the managed owner. */
202
+ runtimeDbApply(db: DbHandle, changes: ChangesHandle, expected: ExpectedWire, callback: () => void): OperationHandle
190
203
  runtimeApplyTake(operation: OperationHandle): ApplyOutcomeWire
204
+ /** Same candidate admission as apply, aborted on its worker before returning. */
205
+ runtimeDbJudge(db: DbHandle, changes: ChangesHandle, expected: ExpectedWire, callback: () => void): OperationHandle
206
+ runtimeJudgeTake(operation: OperationHandle): JudgeOutcomeWire
191
207
 
192
- runtimeDbInspect(db: DbHandle, policy: PolicyWire, callback: () => void): OperationHandle
208
+ runtimeDbInspect(db: DbHandle, callback: () => void): OperationHandle
209
+ runtimeDbClearCache(db: DbHandle, callback: () => void): OperationHandle
193
210
  runtimeDbInspectTake(operation: OperationHandle): DbInspectionWire
194
211
 
195
212
  /** Shared canonical row codec (also the log/migration encoding). */
196
213
  runtimeEncodeRows(
197
214
  runtime: RuntimeHandle,
198
- policy: PolicyWire,
199
215
  spec: SchemaSpec,
200
216
  relationId: number,
201
217
  rows: bigint,
@@ -205,7 +221,6 @@ interface DbBridge {
205
221
  runtimeBytesTake(operation: OperationHandle): Uint8Array
206
222
  runtimeDecodeRows(
207
223
  runtime: RuntimeHandle,
208
- policy: PolicyWire,
209
224
  spec: SchemaSpec,
210
225
  relationId: number,
211
226
  bytes: Uint8Array,
@@ -219,18 +234,8 @@ interface DbBridge {
219
234
  * cancellable operation. Neither verb opens, initializes, freezes or
220
235
  * migrates a database.
221
236
  */
222
- runtimeMigrationSchema(
223
- runtime: RuntimeHandle,
224
- policy: PolicyWire,
225
- spec: SchemaSpec,
226
- callback: () => void
227
- ): OperationHandle
228
- runtimeMigrationRead(
229
- runtime: RuntimeHandle,
230
- policy: PolicyWire,
231
- request: Uint8Array,
232
- callback: () => void
233
- ): OperationHandle
237
+ runtimeMigrationSchema(runtime: RuntimeHandle, spec: SchemaSpec, callback: () => void): OperationHandle
238
+ runtimeMigrationRead(runtime: RuntimeHandle, request: Uint8Array, callback: () => void): OperationHandle
234
239
  }
235
240
 
236
241
  // The fresh-addon roster test pins this private declaration exactly as it