@bjornpagen/bumbledb-log 0.20.0 → 0.20.2

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 (76) hide show
  1. package/dist/braids.d.ts +28 -0
  2. package/dist/braids.d.ts.map +1 -0
  3. package/{src/braids.ts → dist/braids.js} +22 -29
  4. package/dist/braids.js.map +1 -0
  5. package/dist/bytes.d.ts +26 -0
  6. package/dist/bytes.d.ts.map +1 -0
  7. package/dist/bytes.js +86 -0
  8. package/dist/bytes.js.map +1 -0
  9. package/dist/chain.d.ts +56 -0
  10. package/dist/chain.d.ts.map +1 -0
  11. package/dist/chain.js +137 -0
  12. package/dist/chain.js.map +1 -0
  13. package/dist/codec.d.ts +71 -0
  14. package/dist/codec.d.ts.map +1 -0
  15. package/dist/codec.js +179 -0
  16. package/dist/codec.js.map +1 -0
  17. package/dist/descriptor.d.ts +61 -0
  18. package/dist/descriptor.d.ts.map +1 -0
  19. package/dist/descriptor.js +160 -0
  20. package/dist/descriptor.js.map +1 -0
  21. package/dist/errors.d.ts +202 -0
  22. package/dist/errors.d.ts.map +1 -0
  23. package/dist/errors.js +122 -0
  24. package/dist/errors.js.map +1 -0
  25. package/dist/index.d.ts +34 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +24 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/keys.d.ts +52 -0
  30. package/dist/keys.d.ts.map +1 -0
  31. package/dist/keys.js +166 -0
  32. package/dist/keys.js.map +1 -0
  33. package/dist/manifest.d.ts +42 -0
  34. package/dist/manifest.d.ts.map +1 -0
  35. package/dist/manifest.js +116 -0
  36. package/dist/manifest.js.map +1 -0
  37. package/dist/replica.d.ts +154 -0
  38. package/dist/replica.d.ts.map +1 -0
  39. package/dist/replica.js +820 -0
  40. package/dist/replica.js.map +1 -0
  41. package/dist/store-s3.d.ts +31 -0
  42. package/dist/store-s3.d.ts.map +1 -0
  43. package/dist/store-s3.js +299 -0
  44. package/dist/store-s3.js.map +1 -0
  45. package/dist/store.d.ts +129 -0
  46. package/dist/store.d.ts.map +1 -0
  47. package/dist/store.js +577 -0
  48. package/dist/store.js.map +1 -0
  49. package/dist/tenants.d.ts +56 -0
  50. package/dist/tenants.d.ts.map +1 -0
  51. package/dist/tenants.js +333 -0
  52. package/dist/tenants.js.map +1 -0
  53. package/dist/tilde-family.json +21 -0
  54. package/dist/vector.d.ts +31 -0
  55. package/dist/vector.d.ts.map +1 -0
  56. package/dist/vector.js +87 -0
  57. package/dist/vector.js.map +1 -0
  58. package/dist/writer.d.ts +106 -0
  59. package/dist/writer.d.ts.map +1 -0
  60. package/dist/writer.js +677 -0
  61. package/dist/writer.js.map +1 -0
  62. package/package.json +12 -7
  63. package/src/bytes.ts +0 -115
  64. package/src/chain.ts +0 -181
  65. package/src/codec.ts +0 -252
  66. package/src/descriptor.ts +0 -240
  67. package/src/errors.ts +0 -309
  68. package/src/index.ts +0 -57
  69. package/src/keys.ts +0 -218
  70. package/src/manifest.ts +0 -161
  71. package/src/replica.ts +0 -1061
  72. package/src/store-s3.ts +0 -377
  73. package/src/store.ts +0 -727
  74. package/src/tenants.ts +0 -414
  75. package/src/vector.ts +0 -103
  76. package/src/writer.ts +0 -977
@@ -0,0 +1,333 @@
1
+ /**
2
+ * Per-tenant replicas (50, case 4): an LRU of replicas keyed by tenant
3
+ * id — a tenant is a prefix (`<root>/t/<tenant>`), eviction closes and
4
+ * deletes the local dir (the disposable law), and `t/_shared` is
5
+ * pinned. Braids shard within a tenant; tenants shard the world. A
6
+ * tenant id is one StoreKey segment. `get` returns a live handle whose
7
+ * refcount pins it against eviction for the duration of the borrow
8
+ * (findings 38, 71) — the pin outlives `get` and drops only on
9
+ * `release`; a disposed handle is a distinct type. One fenced lease
10
+ * owns each replica directory and is CAS-renewed while the slot is
11
+ * open, so a second live replica on that dir is unrepresentable
12
+ * (findings 39, 48).
13
+ */
14
+ import * as fs from "node:fs/promises";
15
+ import * as path from "node:path";
16
+ import * as errors from "@superbuilders/errors";
17
+ import { regex } from "arkregex";
18
+ import { TEMP_NAMESPACE, tenantPrefix } from "#keys.ts";
19
+ import { openReplica } from "#replica.ts";
20
+ import { acquireFsLease, encodeLease, parseLease, releaseFsLease, sweepStaleTemps, syncedTemp } from "#store.ts";
21
+ const PINNED_TENANT = "_shared";
22
+ const TENANT_ID = regex("^[A-Za-z0-9._-]+$");
23
+ /** Directory-lease lifetime: one owner per replica directory. The pool
24
+ * CAS-extends `expires` at one-third this TTL while the slot is open,
25
+ * so expiry cannot mint a second owner under a live replica. */
26
+ const DIR_LEASE_MS = 300_000;
27
+ const liveHandleBrand = Symbol("liveHandle");
28
+ const disposedHandleBrand = Symbol("disposedHandle");
29
+ function codeOf(error) {
30
+ return error.code;
31
+ }
32
+ async function fsyncDir(dir) {
33
+ const handle = await fs.open(dir, "r");
34
+ const synced = await errors.try(handle.sync());
35
+ await handle.close();
36
+ if (synced.error) {
37
+ throw errors.wrap(synced.error, `fsync directory ${dir}`);
38
+ }
39
+ }
40
+ /** CAS-extend `expires` of a token we already hold. A new token would
41
+ * be a second owner; we rewrite the same path so identity does not
42
+ * change. */
43
+ async function renewDirLease(held, ttlMs) {
44
+ const read = await errors.try(fs.readFile(held.path, "utf8"));
45
+ if (read.error) {
46
+ if (codeOf(read.error) === "ENOENT") {
47
+ return;
48
+ }
49
+ throw read.error;
50
+ }
51
+ const lease = parseLease(read.data);
52
+ if (lease === null || lease.holder !== held.holder || lease.token !== held.token) {
53
+ throw errors.new("directory lease is no longer ours");
54
+ }
55
+ const body = encodeLease({ holder: held.holder, token: held.token, expires: BigInt(Date.now() + ttlMs) });
56
+ // The temp lives under the reserved `{root}/~tmp` namespace, where a
57
+ // crash strands sweepable litter — never beside the lease path.
58
+ const temp = await syncedTemp(held.root, body);
59
+ const replaced = await errors.try(fs.rename(temp, held.path));
60
+ if (replaced.error) {
61
+ await fs.rm(temp, { force: true });
62
+ if (codeOf(replaced.error) === "ENOENT") {
63
+ return;
64
+ }
65
+ throw replaced.error;
66
+ }
67
+ await fsyncDir(held.dir);
68
+ }
69
+ async function directoryBytes(dir) {
70
+ let total = 0;
71
+ const listed = await errors.try(fs.readdir(dir, { withFileTypes: true, recursive: true }));
72
+ if (listed.error) {
73
+ return 0;
74
+ }
75
+ for (const entry of listed.data) {
76
+ if (entry.isFile()) {
77
+ const stat = await errors.try(fs.stat(path.join(entry.parentPath, entry.name)));
78
+ if (stat.error === undefined) {
79
+ total += stat.data.size;
80
+ }
81
+ }
82
+ }
83
+ return total;
84
+ }
85
+ function checkTenantId(tenant) {
86
+ if (!TENANT_ID.test(tenant)) {
87
+ throw errors.new(`tenant id is not a single path segment: ${tenant}`);
88
+ }
89
+ }
90
+ function asDisposed() {
91
+ return { [disposedHandleBrand]: disposedHandleBrand };
92
+ }
93
+ async function disposeSlot(slot) {
94
+ await slot.replica[Symbol.asyncDispose]();
95
+ await fs.rm(slot.dir, { recursive: true, force: true });
96
+ await releaseFsLease(slot.lease);
97
+ }
98
+ function openTenants(options) {
99
+ const budgetBytes = options.budgetBytes ?? 400_000_000;
100
+ const maxOpen = options.maxOpen ?? 32;
101
+ const dirLeaseMs = options.dirLeaseMs ?? DIR_LEASE_MS;
102
+ if (dirLeaseMs <= 0) {
103
+ throw errors.new("dirLeaseMs must be positive");
104
+ }
105
+ const renewEveryMs = Math.max(1, Math.floor(dirLeaseMs / 3));
106
+ const open = new Map();
107
+ const opening = new Map();
108
+ /** The pool root's `~tmp` holds lease mint/renew/release temps; stale
109
+ * ones are crash litter swept once per pool open, best-effort. */
110
+ const swept = errors.try(sweepStaleTemps(path.join(options.dir, TEMP_NAMESPACE)));
111
+ let tick = 0;
112
+ let closed = false;
113
+ let renewTimer;
114
+ let leaseGate = Promise.resolve();
115
+ function withLeaseGate(body) {
116
+ const run = leaseGate.then(body, body);
117
+ leaseGate = run.then(function absorb() {
118
+ return undefined;
119
+ }, function absorbFailure() {
120
+ return undefined;
121
+ });
122
+ return run;
123
+ }
124
+ function dropBorrow(tenant) {
125
+ if (closed) {
126
+ throw errors.new("tenants pool is disposed");
127
+ }
128
+ const slot = open.get(tenant);
129
+ if (slot === undefined || slot.refs <= 0) {
130
+ throw errors.new(`no live borrow for tenant ${tenant}`);
131
+ }
132
+ slot.refs -= 1;
133
+ }
134
+ function brandLive(replica, tenant) {
135
+ const handle = replica;
136
+ Object.defineProperty(handle, liveHandleBrand, { value: liveHandleBrand });
137
+ Object.defineProperty(handle, "release", {
138
+ value: function release() {
139
+ dropBorrow(tenant);
140
+ },
141
+ enumerable: false
142
+ });
143
+ return handle;
144
+ }
145
+ function armRenew() {
146
+ if (renewTimer !== undefined) {
147
+ return;
148
+ }
149
+ renewTimer = setInterval(function tickRenew() {
150
+ void errors.try(renewAllOpen());
151
+ }, renewEveryMs);
152
+ }
153
+ function disarmRenew() {
154
+ if (open.size > 0 || renewTimer === undefined) {
155
+ return;
156
+ }
157
+ clearInterval(renewTimer);
158
+ renewTimer = undefined;
159
+ }
160
+ async function renewAllOpen() {
161
+ await withLeaseGate(async function renewHeld() {
162
+ for (const [tenant, slot] of open) {
163
+ const result = await errors.try(renewDirLease(slot.lease, dirLeaseMs));
164
+ if (result.error !== undefined && open.get(tenant) === slot) {
165
+ slot.leaseLost = result.error;
166
+ }
167
+ }
168
+ });
169
+ }
170
+ async function evictUntilWithin(keep) {
171
+ for (;;) {
172
+ let bytes = 0;
173
+ for (const slot of open.values()) {
174
+ bytes += slot.bytes;
175
+ }
176
+ if (open.size <= maxOpen && bytes <= budgetBytes) {
177
+ return;
178
+ }
179
+ let victim = null;
180
+ let oldest = Number.POSITIVE_INFINITY;
181
+ for (const [tenant, slot] of open) {
182
+ if (tenant === PINNED_TENANT || tenant === keep || slot.refs > 0) {
183
+ continue;
184
+ }
185
+ if (slot.lastUsed < oldest) {
186
+ oldest = slot.lastUsed;
187
+ victim = tenant;
188
+ }
189
+ }
190
+ if (victim === null) {
191
+ return;
192
+ }
193
+ const slot = open.get(victim);
194
+ open.delete(victim);
195
+ if (slot !== undefined) {
196
+ await withLeaseGate(function disposeVictim() {
197
+ return disposeSlot(slot);
198
+ });
199
+ }
200
+ }
201
+ }
202
+ /** Increment the borrow. The pin outlives `get` — a `finally`
203
+ * decrement here would make LRU/`evict` legal while the caller
204
+ * still holds the LiveHandle (finding 38). Decrement only if this
205
+ * `get` fails before the handle is in the caller's hands. */
206
+ async function pin(slot, tenant) {
207
+ if (slot.leaseLost !== undefined) {
208
+ throw slot.leaseLost;
209
+ }
210
+ slot.refs += 1;
211
+ slot.lastUsed = tick;
212
+ try {
213
+ await evictUntilWithin(tenant);
214
+ const kept = open.get(tenant);
215
+ if (kept === undefined) {
216
+ throw errors.new("tenants pool evicted the handle it is returning");
217
+ }
218
+ await withLeaseGate(function renewOnPin() {
219
+ return renewDirLease(kept.lease, dirLeaseMs);
220
+ });
221
+ return kept.replica;
222
+ }
223
+ catch (error) {
224
+ slot.refs -= 1;
225
+ throw error;
226
+ }
227
+ }
228
+ async function openOne(tenant) {
229
+ await swept;
230
+ const dir = path.join(options.dir, tenant);
231
+ /** The dir-lease mirrors Rust `acquire_named`: tokens live at
232
+ * `{options.dir}/~lease/{tenant}`, outside the disposable replica
233
+ * directory `{options.dir}/{tenant}` — the replica-open sweep
234
+ * rm-rfs the replica dir's own `~lease` and must not touch the
235
+ * held tenant lease. */
236
+ const lease = await acquireFsLease(options.dir, tenant, dirLeaseMs, "refuse");
237
+ let replica;
238
+ try {
239
+ replica = brandLive(await openReplica({
240
+ store: options.store,
241
+ prefix: tenantPrefix(options.root, tenant),
242
+ dir,
243
+ theory: options.theory
244
+ }), tenant);
245
+ }
246
+ catch (error) {
247
+ await releaseFsLease(lease);
248
+ throw error;
249
+ }
250
+ const slot = {
251
+ replica,
252
+ dir,
253
+ lease,
254
+ bytes: await directoryBytes(dir),
255
+ lastUsed: tick,
256
+ refs: 0
257
+ };
258
+ open.set(tenant, slot);
259
+ armRenew();
260
+ return await pin(slot, tenant);
261
+ }
262
+ return {
263
+ async get(tenant) {
264
+ if (closed) {
265
+ throw errors.new("tenants pool is disposed");
266
+ }
267
+ checkTenantId(tenant);
268
+ tick += 1;
269
+ const hit = open.get(tenant);
270
+ if (hit !== undefined) {
271
+ return await pin(hit, tenant);
272
+ }
273
+ const inflight = opening.get(tenant);
274
+ if (inflight !== undefined) {
275
+ const ran = await errors.try(inflight);
276
+ if (ran.error) {
277
+ throw ran.error;
278
+ }
279
+ const slot = open.get(tenant);
280
+ if (slot === undefined) {
281
+ throw errors.new("tenants pool dropped the tenant during open");
282
+ }
283
+ return await pin(slot, tenant);
284
+ }
285
+ const pending = openOne(tenant);
286
+ opening.set(tenant, pending);
287
+ const ran = await errors.try(pending);
288
+ opening.delete(tenant);
289
+ if (ran.error) {
290
+ throw ran.error;
291
+ }
292
+ return ran.data;
293
+ },
294
+ async evict(tenant) {
295
+ if (closed) {
296
+ throw errors.new("tenants pool is disposed");
297
+ }
298
+ checkTenantId(tenant);
299
+ if (tenant === PINNED_TENANT) {
300
+ return null;
301
+ }
302
+ const slot = open.get(tenant);
303
+ if (slot === undefined || slot.refs > 0) {
304
+ return null;
305
+ }
306
+ open.delete(tenant);
307
+ await withLeaseGate(function disposeEvicted() {
308
+ return disposeSlot(slot);
309
+ });
310
+ disarmRenew();
311
+ return asDisposed();
312
+ },
313
+ release(tenant) {
314
+ dropBorrow(tenant);
315
+ },
316
+ async [Symbol.asyncDispose]() {
317
+ closed = true;
318
+ if (renewTimer !== undefined) {
319
+ clearInterval(renewTimer);
320
+ renewTimer = undefined;
321
+ }
322
+ await withLeaseGate(async function disposePool() {
323
+ for (const slot of open.values()) {
324
+ await releaseFsLease(slot.lease);
325
+ await slot.replica[Symbol.asyncDispose]();
326
+ }
327
+ open.clear();
328
+ });
329
+ }
330
+ };
331
+ }
332
+ export { openTenants };
333
+ //# sourceMappingURL=tenants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tenants.js","sourceRoot":"","sources":["../src/tenants.ts"],"names":[],"mappings":"AAAA;;;;;;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0,0 +1,21 @@
1
+ {
2
+ "surface": "key-grammar/tilde-family",
3
+ "rule": "a segment whose first code point is in this set is reserved; the set is ASCII tilde, its lookalikes, and the NFKC preimage of U+007E, one closed table both drivers consume",
4
+ "codePoints": [
5
+ "U+007E",
6
+ "U+02DC",
7
+ "U+02F7",
8
+ "U+1FC0",
9
+ "U+2053",
10
+ "U+223C",
11
+ "U+223D",
12
+ "U+223F",
13
+ "U+2E1E",
14
+ "U+2E1F",
15
+ "U+301C",
16
+ "U+3030",
17
+ "U+FE4B",
18
+ "U+FE4F",
19
+ "U+FF5E"
20
+ ]
21
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The per-braid generation map. Sum, domination, checkpoint order, and
3
+ * apply's increment live here. Overflow is a refusal of `sum` and of
4
+ * nowhere else.
5
+ */
6
+ import type { Braid } from "./descriptor.js";
7
+ import type { Generation } from "./keys.js";
8
+ declare const Overflow: {
9
+ readonly tag: "overflow";
10
+ };
11
+ type Overflow = typeof Overflow;
12
+ type CheckpointOrder = "before" | "equal" | "after";
13
+ /** Applied counts keyed by braid. Any vector is a legal restore point. */
14
+ declare class Vector {
15
+ #private;
16
+ constructor(entries?: Iterable<readonly [Braid, bigint]>);
17
+ static from(map: ReadonlyMap<Braid, bigint>): Vector;
18
+ /** The wholeness arithmetic. The one overflow site. */
19
+ sum(): bigint | Overflow;
20
+ /** Pointwise `this[braid] >= other[braid]`; an absent braid is zero. */
21
+ dominates(other: Vector): boolean;
22
+ /** The total order the manifest CAS installs. */
23
+ order(other: Vector): CheckpointOrder;
24
+ /** The applied count for `braid`; absent is zero. */
25
+ at(braid: Braid): Generation;
26
+ /** Apply's one mutation: this braid's count advances by one. */
27
+ advance(braid: Braid): Vector;
28
+ }
29
+ export type { CheckpointOrder };
30
+ export { Overflow, Vector };
31
+ //# sourceMappingURL=vector.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vector.d.ts","sourceRoot":"","sources":["../src/vector.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAG1C,QAAA,MAAM,QAAQ;aAAK,GAAG,EAAE,UAAU;CAAW,CAAA;AAC7C,KAAK,QAAQ,GAAG,OAAO,QAAQ,CAAA;AAE/B,KAAK,eAAe,GAAG,QAAQ,GAAG,OAAO,GAAG,OAAO,CAAA;AAEnD,0EAA0E;AAC1E,cAAM,MAAM;;IAGX,YAAY,OAAO,GAAE,QAAQ,CAAC,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAM,EAc3D;IAED,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,MAAM,CAEnD;IAED,uDAAuD;IACvD,GAAG,IAAI,MAAM,GAAG,QAAQ,CAUvB;IAED,wEAAwE;IACxE,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAOhC;IAED,iDAAiD;IACjD,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,CAqBpC;IAED,qDAAqD;IACrD,EAAE,CAAC,KAAK,EAAE,KAAK,GAAG,UAAU,CAE3B;IAED,gEAAgE;IAChE,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,CAK5B;CACD;AAED,YAAY,EAAE,eAAe,EAAE,CAAA;AAC/B,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAA"}
package/dist/vector.js ADDED
@@ -0,0 +1,87 @@
1
+ /**
2
+ * The per-braid generation map. Sum, domination, checkpoint order, and
3
+ * apply's increment live here. Overflow is a refusal of `sum` and of
4
+ * nowhere else.
5
+ */
6
+ import { U64_MAX } from "#bytes.ts";
7
+ import { generation } from "#keys.ts";
8
+ const Overflow = { tag: "overflow" };
9
+ /** Applied counts keyed by braid. Any vector is a legal restore point. */
10
+ class Vector {
11
+ #counts;
12
+ constructor(entries = []) {
13
+ const counts = new Map();
14
+ for (const [braid, g] of [...entries].sort(function byBraid(left, right) {
15
+ if (left[0] < right[0]) {
16
+ return -1;
17
+ }
18
+ if (left[0] > right[0]) {
19
+ return 1;
20
+ }
21
+ return 0;
22
+ })) {
23
+ counts.set(braid, g);
24
+ }
25
+ this.#counts = counts;
26
+ }
27
+ static from(map) {
28
+ return new Vector(map);
29
+ }
30
+ /** The wholeness arithmetic. The one overflow site. */
31
+ sum() {
32
+ let acc = 0n;
33
+ for (const g of this.#counts.values()) {
34
+ const next = acc + g;
35
+ if (next > U64_MAX) {
36
+ return Overflow;
37
+ }
38
+ acc = next;
39
+ }
40
+ return acc;
41
+ }
42
+ /** Pointwise `this[braid] >= other[braid]`; an absent braid is zero. */
43
+ dominates(other) {
44
+ for (const [braid, g] of other.#counts) {
45
+ if (this.at(braid) < g) {
46
+ return false;
47
+ }
48
+ }
49
+ return true;
50
+ }
51
+ /** The total order the manifest CAS installs. */
52
+ order(other) {
53
+ const left = this.sum();
54
+ const right = other.sum();
55
+ const leftOverflow = typeof left !== "bigint";
56
+ const rightOverflow = typeof right !== "bigint";
57
+ if (leftOverflow && rightOverflow) {
58
+ return "equal";
59
+ }
60
+ if (leftOverflow) {
61
+ return "after";
62
+ }
63
+ if (rightOverflow) {
64
+ return "before";
65
+ }
66
+ if (left < right) {
67
+ return "before";
68
+ }
69
+ if (left > right) {
70
+ return "after";
71
+ }
72
+ return "equal";
73
+ }
74
+ /** The applied count for `braid`; absent is zero. */
75
+ at(braid) {
76
+ return generation(this.#counts.get(braid) ?? 0n);
77
+ }
78
+ /** Apply's one mutation: this braid's count advances by one. */
79
+ advance(braid) {
80
+ const next = new Map(this.#counts);
81
+ const current = next.get(braid) ?? 0n;
82
+ next.set(braid, current === U64_MAX ? U64_MAX : current + 1n);
83
+ return new Vector(next);
84
+ }
85
+ }
86
+ export { Overflow, Vector };
87
+ //# sourceMappingURL=vector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vector.js","s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,IAAI,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAClC,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,CAAA;QACrC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,GAAG,EAAE,CAAC,CAAA;QAC7D,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,CAAA;IACxB,CAAC;CACD;AAGD,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAA"}
@@ -0,0 +1,106 @@
1
+ /**
2
+ * The writer (60): a replica plus the right to create log objects. Role
3
+ * is a field on the handle. `openWriter(options)` births the store;
4
+ * `openWriter(replica)` wraps a born replica and settles an inherited
5
+ * pending without drawing id leases. A replica never births —
6
+ * ManifestMissing is its refusal. One commit path and one loss path: a
7
+ * lost slot's byte-equal occupant is an ambiguous PUT absorbed; anything
8
+ * else discards the local directory, re-opens through the replica to the
9
+ * current tip, and re-judges the recorded ops once — the verdict IS a
10
+ * serial execution, performed. Each loop iteration races once at the
11
+ * then-tip, so a historical loss is structurally uncountable, and bounded
12
+ * live-tip losses surface as ErrContention carrying the terminal
13
+ * re-judgment's own violation or the racing tip.
14
+ */
15
+ import { type Admission, type Fact, type FreshKeys, type FreshRange, type MemberRelation, type MutationReport, type SchemaRelations } from "@bjornpagen/bumbledb";
16
+ import type { Digest32 } from "./bytes.js";
17
+ import type { Braid, Theory } from "./descriptor.js";
18
+ import type { Generation } from "./keys.js";
19
+ import type { CheckpointFacts } from "./manifest.js";
20
+ import type { OpenReplicaOptions, Replica } from "./replica.js";
21
+ import type { ObjectStore } from "./store.js";
22
+ type Durability = "published" | "local-pending";
23
+ /** Where an accepted commit landed on its braid: the slot (the braid
24
+ * position — never the store-wide sum) and how durable the batch is. */
25
+ interface Landing {
26
+ readonly slot: Generation;
27
+ readonly durability: Durability;
28
+ }
29
+ /** The accepted payload of `commit`: the body's value plus the landing. */
30
+ interface CommitReceipt<R> extends Landing {
31
+ readonly value: R;
32
+ readonly braid: Braid;
33
+ }
34
+ /** The empty commit is not a commit: a body that records no ops names no
35
+ * braid, so the refusal is its own outcome — never a slot, never a
36
+ * thrown surprise. The body's value rides out, the way an abandoned
37
+ * write's payload does in the engine. */
38
+ interface EmptyCommit<R> {
39
+ readonly tag: "empty";
40
+ readonly value: R;
41
+ }
42
+ /** The engine's Admission sum carrying the log's receipt in its accepted
43
+ * arm; the rejected arm is the engine's violations, shell and payload. */
44
+ type Commit<Rels extends SchemaRelations, R> = Admission<Rels, CommitReceipt<R>> | EmptyCommit<R>;
45
+ /** One braid's verdict inside a split commit: the engine's Admission
46
+ * carrying the landing, beside the braid it judged. */
47
+ interface BraidOutcome<Rels extends SchemaRelations> {
48
+ readonly braid: Braid;
49
+ readonly admission: Admission<Rels, Landing>;
50
+ }
51
+ type CommitSplit<Rels extends SchemaRelations, R> = {
52
+ readonly tag: "split";
53
+ readonly value: R;
54
+ readonly outcomes: readonly BraidOutcome<Rels>[];
55
+ } | EmptyCommit<R>;
56
+ /**
57
+ * The recorder: the engine `WriteTx`'s write surface, verbatim — insert,
58
+ * delete, and reserve carry the engine's own signatures, so a write-only
59
+ * body typechecks against both dialects. `contains`/`get` are absent by
60
+ * law: the journaled dialect is a pure recorder, and change is judged at
61
+ * commit, so a report's `changed` is 0n at record time. Reservations
62
+ * never appear in the log; the resulting inserts carry concrete values.
63
+ */
64
+ interface Batch<Rels extends SchemaRelations> {
65
+ insert<Rel extends MemberRelation<Rels>>(relation: Rel, facts: Iterable<Fact<Rel>>): MutationReport;
66
+ delete<Rel extends MemberRelation<Rels>>(relation: Rel, facts: Iterable<Fact<Rel>>): MutationReport;
67
+ reserve<Rel extends MemberRelation<Rels>>(relation: Rel, field: FreshKeys<Rel> & string, count: bigint): FreshRange;
68
+ }
69
+ /** Ownership of a slot, read from the winner's header. */
70
+ interface Deposition {
71
+ readonly braid: Braid;
72
+ readonly slot: Generation;
73
+ readonly resident: bigint;
74
+ readonly usurper: bigint;
75
+ }
76
+ interface Writer<Rels extends SchemaRelations> {
77
+ readonly role: "writer";
78
+ readonly replica: Replica<Rels>;
79
+ deposition(): Deposition | null;
80
+ commit<R>(body: (batch: Batch<Rels>) => R | Promise<R>): Promise<Commit<Rels, R>>;
81
+ commitSplit<R>(body: (batch: Batch<Rels>) => R | Promise<R>): Promise<CommitSplit<Rels, R>>;
82
+ }
83
+ type PublishRefusal = "manifest-missing" | "manifest" | "checkpoint-doc-missing" | "checkpoint";
84
+ type Published = {
85
+ readonly tag: "replaced";
86
+ } | {
87
+ readonly tag: "kept";
88
+ readonly incumbent: Digest32;
89
+ } | {
90
+ readonly tag: "refused";
91
+ readonly reason: PublishRefusal;
92
+ };
93
+ /**
94
+ * Publishes `candidate` under the checkpoint order. The digest is the
95
+ * blake3 of the full bytes, `prev` included. A scratch lease
96
+ * `{dir}/~lease/ckpt-scratch` names the candidate for the publish
97
+ * window; a crash leaves that lease and a successor sweep reclaims
98
+ * it. `Kept` and every refused publish delete the candidate's `ckpt`
99
+ * pair.
100
+ */
101
+ declare function publishCheckpoint(store: ObjectStore, prefix: string, dir: string, theory: Theory, candidate: CheckpointFacts, mdb: Uint8Array): Promise<Published>;
102
+ declare function openWriter<Rels extends SchemaRelations>(replica: Replica<Rels>): Promise<Writer<Rels>>;
103
+ declare function openWriter<Rels extends SchemaRelations>(options: OpenReplicaOptions<Rels>): Promise<Writer<Rels>>;
104
+ export type { Batch, BraidOutcome, Commit, CommitReceipt, CommitSplit, Deposition, Durability, EmptyCommit, Landing, Writer };
105
+ export { openWriter, publishCheckpoint };
106
+ //# sourceMappingURL=writer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"writer.d.ts","sourceRoot":"","sources":["../src/writer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAKH,OAAO,EACN,KAAK,SAAS,EACd,KAAK,IAAI,EAET,KAAK,SAA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