@gusto/baerly-storage 0.3.0 → 0.4.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.
- package/README.md +157 -92
- package/dist/API.md +64 -32
- package/dist/CHANGELOG.md +301 -5
- package/dist/RECIPES.md +125 -0
- package/dist/THIRD-PARTY-LICENSES.txt +22 -96
- package/dist/{app-config-BIzyAKzy.d.ts → app-config-DyiFK3_k.d.ts} +43 -11
- package/dist/app-config.d.ts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.js +13 -13
- package/dist/auth.js.map +1 -1
- package/dist/aws4fetch.esm-p7eEzLCs.js +1702 -0
- package/dist/aws4fetch.esm-p7eEzLCs.js.map +1 -0
- package/dist/baerly.js +35 -15
- package/dist/baerly.js.map +1 -1
- package/dist/baerly.spec.json +342 -0
- package/dist/{client-BJXF3XJA.d.ts → client-Fe8CwB73.d.ts} +3 -3
- package/dist/client-react.d.ts +3 -3
- package/dist/client-react.js +4 -4
- package/dist/client-react.js.map +1 -1
- package/dist/client.d.ts +4 -4
- package/dist/client.js +3 -3
- package/dist/{cloudflare-Cn2ibODt.js → cloudflare-DzKBrUlg.js} +2 -2
- package/dist/{cloudflare-Cn2ibODt.js.map → cloudflare-DzKBrUlg.js.map} +1 -1
- package/dist/cloudflare.d.ts +49 -6
- package/dist/cloudflare.js +55 -21
- package/dist/cloudflare.js.map +1 -1
- package/dist/config-1tv0-Yzm.js +2693 -0
- package/dist/config-1tv0-Yzm.js.map +1 -0
- package/dist/constants-CfeMWQWk.js +222 -0
- package/dist/constants-CfeMWQWk.js.map +1 -0
- package/dist/{cost-BZ_MDBmn.js → cost-DSMIVqzL.js} +42 -43
- package/dist/cost-DSMIVqzL.js.map +1 -0
- package/dist/{current-json-DsN7FNY1.js → current-json-C6nhPOm1.js} +6 -204
- package/dist/current-json-C6nhPOm1.js.map +1 -0
- package/dist/{db-CCT6tDfY.d.ts → db-DsPkOVEH.d.ts} +3 -3
- package/dist/{deploy-BKKyGlSJ.js → deploy-DonTr-CV.js} +4 -4
- package/dist/{deploy-BKKyGlSJ.js.map → deploy-DonTr-CV.js.map} +1 -1
- package/dist/dev-vite.d.ts +9 -5
- package/dist/dev-vite.js +5 -4
- package/dist/dev-vite.js.map +1 -1
- package/dist/dev.d.ts +1 -1
- package/dist/dev.js +1 -1
- package/dist/{doctor-BVU4GNXN.js → doctor-DSIyCDg_.js} +5 -8
- package/dist/{doctor-BVU4GNXN.js.map → doctor-DSIyCDg_.js.map} +1 -1
- package/dist/{dump-DPEAKub6.js → dump-Bh5k26VQ.js} +9 -32
- package/dist/dump-Bh5k26VQ.js.map +1 -0
- package/dist/{errors-DOdL95kK.js → errors-Dfa3p78c.js} +25 -5
- package/dist/errors-Dfa3p78c.js.map +1 -0
- package/dist/{export-BcuCLFFe.js → export-DtF5tDIM.js} +9 -32
- package/dist/{export-BcuCLFFe.js.map → export-DtF5tDIM.js.map} +1 -1
- package/dist/{fsck-RXgLOaJG.js → fsck-D_djMO5D.js} +10 -33
- package/dist/fsck-D_djMO5D.js.map +1 -0
- package/dist/{http-Bd4byR4n.js → http-DxM9MflO.js} +123 -35
- package/dist/http-DxM9MflO.js.map +1 -0
- package/dist/http.d.ts +42 -26
- package/dist/http.js +3 -2
- package/dist/{index-DnBGSeuU.d.ts → index-0rZcMBwy.d.ts} +2 -2
- package/dist/{index-Cmsxx-ZN.d.ts → index-B6oexfOB.d.ts} +7 -7
- package/dist/{index-BDceQQi-.d.ts → index-BNRh23K9.d.ts} +4 -4
- package/dist/{index-DW7GT6M2.d.ts → index-BYk9oCNf.d.ts} +193 -163
- package/dist/index.d.ts +6 -6
- package/dist/index.js +4 -4
- package/dist/{indexes-Cx7uMXm0.d.ts → indexes-Dn9hbGQG.d.ts} +2 -2
- package/dist/{inspect-DhI49PSi.js → inspect-6WZ-pC7-.js} +10 -33
- package/dist/inspect-6WZ-pC7-.js.map +1 -0
- package/dist/key-Di6yyxFu.js +35 -0
- package/dist/key-Di6yyxFu.js.map +1 -0
- package/dist/{log-tail-DXeDkWEp.js → log-tail-D98U1JBo.js} +31 -7
- package/dist/log-tail-D98U1JBo.js.map +1 -0
- package/dist/{maintenance-CWBQpZ2y.js → maintenance-B4Dki6Ig.js} +77 -22
- package/dist/maintenance-B4Dki6Ig.js.map +1 -0
- package/dist/{maintenance-BkhUr72A.d.ts → maintenance-B_e0T99L.d.ts} +28 -5
- package/dist/{maintenance-C_KSkVjd.js → maintenance-C7coQp4c.js} +17 -16
- package/dist/maintenance-C7coQp4c.js.map +1 -0
- package/dist/maintenance.d.ts +3 -3
- package/dist/maintenance.js +3 -3
- package/dist/node.d.ts +168 -65
- package/dist/node.js +3 -2
- package/dist/{normalize-XWY25_sG.js → normalize-rjnZLwS4.js} +2 -2
- package/dist/normalize-rjnZLwS4.js.map +1 -0
- package/dist/{observability-BYd-RzEx.js → observability-wzs3-BL_.js} +233 -71
- package/dist/observability-wzs3-BL_.js.map +1 -0
- package/dist/observability.d.ts +2 -2
- package/dist/observability.js +1 -1
- package/dist/{plan-DIu_B6fy.js → plan-a7PJHBCy.js} +2 -2
- package/dist/{plan-DIu_B6fy.js.map → plan-a7PJHBCy.js.map} +1 -1
- package/dist/{query-sQNO5vU_.js → query-rAtXNQQB.js} +96 -207
- package/dist/query-rAtXNQQB.js.map +1 -0
- package/dist/{rebuild-index-DP4CqSSs.js → rebuild-index-BVzQSZlm.js} +9 -32
- package/dist/rebuild-index-BVzQSZlm.js.map +1 -0
- package/dist/{request-BEF_OvMh.js → request-u0M51mhJ.js} +3 -3
- package/dist/{request-BEF_OvMh.js.map → request-u0M51mhJ.js.map} +1 -1
- package/dist/{restore-DpQYef6r.js → restore-R-4rbciV.js} +73 -100
- package/dist/restore-R-4rbciV.js.map +1 -0
- package/dist/runtime-spec-_GTdeSMo.js +391 -0
- package/dist/runtime-spec-_GTdeSMo.js.map +1 -0
- package/dist/s3-http-B-z8th6O.d.ts +60 -0
- package/dist/s3.d.ts +47 -0
- package/dist/s3.js +45 -0
- package/dist/s3.js.map +1 -0
- package/dist/{shared-secret-BJdSwpgw.js → shared-secret-DgEicd3x.js} +13 -5
- package/dist/shared-secret-DgEicd3x.js.map +1 -0
- package/dist/{src-Db4-qVtM.js → src-CYHFDrKz.js} +51 -8
- package/dist/src-CYHFDrKz.js.map +1 -0
- package/dist/{src-CWpkSbvJ.js → src-DYZ4H1aF.js} +6 -5
- package/dist/src-DYZ4H1aF.js.map +1 -0
- package/dist/src-D__mHQmQ.js +2271 -0
- package/dist/src-D__mHQmQ.js.map +1 -0
- package/dist/{subcommand-CP9NY2J4.js → subcommand-DoDA-K7d.js} +44 -6
- package/dist/subcommand-DoDA-K7d.js.map +1 -0
- package/dist/time-DUqvFMHq.js +119 -0
- package/dist/time-DUqvFMHq.js.map +1 -0
- package/dist/{usage-CTfDq9HR.js → usage-DBV1eQHC.js} +7 -22
- package/dist/usage-DBV1eQHC.js.map +1 -0
- package/dist/wire-izfGZDvr.js.map +1 -1
- package/package.json +51 -27
- package/dist/config-CGyBopy3.js +0 -5326
- package/dist/config-CGyBopy3.js.map +0 -1
- package/dist/cost-BZ_MDBmn.js.map +0 -1
- package/dist/current-json-DsN7FNY1.js.map +0 -1
- package/dist/dump-DPEAKub6.js.map +0 -1
- package/dist/errors-DOdL95kK.js.map +0 -1
- package/dist/fsck-RXgLOaJG.js.map +0 -1
- package/dist/http-Bd4byR4n.js.map +0 -1
- package/dist/inspect-DhI49PSi.js.map +0 -1
- package/dist/log-tail-DXeDkWEp.js.map +0 -1
- package/dist/maintenance-CWBQpZ2y.js.map +0 -1
- package/dist/maintenance-C_KSkVjd.js.map +0 -1
- package/dist/normalize-XWY25_sG.js.map +0 -1
- package/dist/observability-BYd-RzEx.js.map +0 -1
- package/dist/query-sQNO5vU_.js.map +0 -1
- package/dist/rebuild-index-DP4CqSSs.js.map +0 -1
- package/dist/restore-DpQYef6r.js.map +0 -1
- package/dist/shared-secret-BJdSwpgw.js.map +0 -1
- package/dist/src-CWpkSbvJ.js.map +0 -1
- package/dist/src-D24wDpzR.js +0 -6247
- package/dist/src-D24wDpzR.js.map +0 -1
- package/dist/src-Db4-qVtM.js.map +0 -1
- package/dist/subcommand-CP9NY2J4.js.map +0 -1
- package/dist/usage-CTfDq9HR.js.map +0 -1
- /package/dist/{chunk-EAsCxrDo.js → rolldown-runtime-EAsCxrDo.js} +0 -0
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { a as decodeJsonBytes, i as readCurrentJson, n as createCurrentJson, o as encodeJsonBytes, r as logSeqStartOf } from "./current-json-C6nhPOm1.js";
|
|
2
|
+
import { l as MAINTENANCE_PROFILE_CF_FREE, m as MAX_KEY_BYTES, r as FORBIDDEN_MERGE_KEYS, s as LOG_FORWARD_PROBE_CAP } from "./constants-CfeMWQWk.js";
|
|
3
|
+
import { t as BaerlyError } from "./errors-Dfa3p78c.js";
|
|
4
|
+
import { A as deepEqualDocumentValue, C as findLogTail, D as validateWire, E as versionFromContent, O as assertWireSatisfiable, S as assertNameNotReserved, T as logObjectKey, _ as encodeIndexValue, b as loadSnapshotAsMap, g as allIndexKeysFor, h as walkLogRange, i as dispatchInlineAwaited, k as matchesWire, l as shouldFireMaintenance, m as readLogEntry, p as foldLogEntriesOnto, s as runBoundedMaintenance, v as validateIndexDefinition, w as probeTailFrom } from "./maintenance-B4Dki6Ig.js";
|
|
5
|
+
import { t as normalizePredicateArg } from "./normalize-rjnZLwS4.js";
|
|
6
|
+
import { a as uuid, n as timestamp, o as uuidv7, r as countKey } from "./time-DUqvFMHq.js";
|
|
5
7
|
import { i as noopMetricsRecorder, n as getCurrentContext } from "./context-DRZ3tZ5l.js";
|
|
6
8
|
//#region packages/protocol/src/json.ts
|
|
7
9
|
const isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -79,128 +81,13 @@ const mergePredicateWires = (a, b) => {
|
|
|
79
81
|
assertWireSatisfiable(merged);
|
|
80
82
|
return merged;
|
|
81
83
|
};
|
|
82
|
-
//#endregion
|
|
83
|
-
//#region packages/protocol/src/types.ts
|
|
84
|
-
/**
|
|
85
|
-
* Mint a fresh {@link UUID}. The cast lives here so callers don't sprinkle
|
|
86
|
-
* `<UUID>crypto.randomUUID()` throughout the codebase.
|
|
87
|
-
*/
|
|
88
|
-
const uuid = () => crypto.randomUUID();
|
|
89
|
-
/**
|
|
90
|
-
* Mint a UUIDv7 — 48-bit Unix-millis prefix + 74 bits of randomness,
|
|
91
|
-
* formatted as a standard 8-4-4-4-12 UUID string per RFC 9562 §5.7.
|
|
92
|
-
* Lex-sortable by mint time, so it pairs well with object-storage
|
|
93
|
-
* list semantics. Returns a branded {@link UUID}.
|
|
94
|
-
*
|
|
95
|
-
* Uses `crypto.getRandomValues` which is universally available
|
|
96
|
-
* (Node 19+, Workerd, Bun, browsers).
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* ```ts
|
|
100
|
-
* const id = uuidv7();
|
|
101
|
-
* // e.g. "01956cad-4cc8-7abc-9def-0123456789ab"
|
|
102
|
-
* ```
|
|
103
|
-
*/
|
|
104
|
-
const uuidv7 = () => {
|
|
105
|
-
const millis = Date.now();
|
|
106
|
-
const rand = new Uint8Array(10);
|
|
107
|
-
crypto.getRandomValues(rand);
|
|
108
|
-
rand[0] = rand[0] & 15 | 112;
|
|
109
|
-
rand[2] = rand[2] & 63 | 128;
|
|
110
|
-
const millisHex = millis.toString(16).padStart(12, "0");
|
|
111
|
-
const b = Array.from(rand, (n) => n.toString(16).padStart(2, "0")).join("");
|
|
112
|
-
return `${millisHex.slice(0, 8)}-${millisHex.slice(8, 12)}-${b.slice(0, 4)}-${b.slice(4, 8)}-${b.slice(8, 20)}`;
|
|
113
|
-
};
|
|
114
|
-
/**
|
|
115
|
-
* Mint the trailing seq segment of an LSN (`<base32-time>_<sess>_<seq>`).
|
|
116
|
-
* Uses `COUNT_BIT_WIDTH` (see `packages/protocol/src/constants.ts`) to
|
|
117
|
-
* produce an 11-char descending base-32 string with a domain of
|
|
118
|
-
* 0 .. `Number.MAX_SAFE_INTEGER` (2^53 − 1).
|
|
119
|
-
*
|
|
120
|
-
* Used together with {@link timestamp} (the descending base-32
|
|
121
|
-
* encoding of millis-since-epoch). See {@link uint2strDesc} for the
|
|
122
|
-
* lex-reversal property both rely on.
|
|
123
|
-
*/
|
|
124
|
-
const countKey = (number) => uint2strDesc(number, 53);
|
|
125
|
-
const uint2str = (num, bits) => {
|
|
126
|
-
const maxBase32Length = Math.ceil(bits / 5);
|
|
127
|
-
return num.toString(32).padStart(maxBase32Length, "0");
|
|
128
|
-
};
|
|
129
|
-
/**
|
|
130
|
-
* Lex-reversed base-32 encoder for an unsigned integer in `[0, 2^bits)`.
|
|
131
|
-
* Pads to `Math.ceil(bits/5)` chars so all values in the domain compare
|
|
132
|
-
* lex against each other.
|
|
133
|
-
*
|
|
134
|
-
* **Order-reversal property:** for any pair of integers `a < b` (with
|
|
135
|
-
* `a, b ∈ [0, 2^bits)`), `uint2strDesc(a, bits) > uint2strDesc(b, bits)`
|
|
136
|
-
* lexicographically. Composed into an LSN of shape
|
|
137
|
-
* `<base32-time>_<session>_<seq>` (where both `<base32-time>` and
|
|
138
|
-
* `<seq>` use this encoder), a forward `Storage.list(prefix)`
|
|
139
|
-
* returns log entries in reverse-causal order — newer entries first.
|
|
140
|
-
*
|
|
141
|
-
* This is the structural property the read path leans on:
|
|
142
|
-
* `ListObjectsV2` is forward-only on S3, but the descending encoding
|
|
143
|
-
* makes "fetch the K newest entries" a single bounded LIST with
|
|
144
|
-
* `maxKeys=K` instead of a full-population LIST + in-memory reverse.
|
|
145
|
-
*
|
|
146
|
-
* Behaviourally verified across randomized populations in
|
|
147
|
-
* `packages/protocol/src/lsn-reverse-list.test.ts`. Quantified
|
|
148
|
-
* bytes-listed reduction is in
|
|
149
|
-
* `docs/spec/attachments/lsn-reverse-walk-baseline.json`
|
|
150
|
-
* (run `pnpm bench:lsn-reverse-walk` to reproduce). Spec rationale:
|
|
151
|
-
* `docs/spec/sync-protocol.md` §"LSNs, wall clocks, and downstream
|
|
152
|
-
* consumers".
|
|
153
|
-
*/
|
|
154
|
-
const uint2strDesc = (num, bits) => {
|
|
155
|
-
return uint2str(Math.pow(2, bits) - 1 - num, bits);
|
|
156
|
-
};
|
|
157
|
-
const str2uintDesc = (str, bits) => {
|
|
158
|
-
return Math.pow(2, bits) - 1 - parseInt(str, 32);
|
|
159
|
-
};
|
|
160
|
-
//#endregion
|
|
161
|
-
//#region packages/protocol/src/time.ts
|
|
162
|
-
/**
|
|
163
|
-
* Encode an epoch-millis instant as a lexicographically-DEScending
|
|
164
|
-
* base-32 key (newer sorts first). The load-bearing LSN ordering
|
|
165
|
-
* primitive — see {@link uint2strDesc}.
|
|
166
|
-
*
|
|
167
|
-
* `epoch` is REQUIRED and validated. There is no default: an argless
|
|
168
|
-
* call used to encode `0`, which descending-sorts as the most-ancient
|
|
169
|
-
* instant and would silently invert ordering. A non-finite, negative,
|
|
170
|
-
* or out-of-range `epoch` (e.g. a broken `Date.now()` returning `NaN`)
|
|
171
|
-
* would corrupt the key silently rather than fail loud, so it throws
|
|
172
|
-
* `Internal` instead — this is an invariant violation, not caller config
|
|
173
|
-
* (the only legitimate argument is `Date.now()`).
|
|
174
|
-
*/
|
|
175
|
-
const timestamp = (epoch) => {
|
|
176
|
-
if (!Number.isInteger(epoch) || epoch < 0 || epoch >= 2 ** 42) throw new BaerlyError("Internal", `timestamp(): epoch must be a non-negative integer < 2^42 (got ${epoch})`);
|
|
177
|
-
return uint2strDesc(epoch, 42);
|
|
178
|
-
};
|
|
179
|
-
/**
|
|
180
|
-
* Delay for `ms` milliseconds, with optional cancellation. If
|
|
181
|
-
* `signal` aborts before the delay elapses, the returned promise
|
|
182
|
-
* rejects with `signal.reason`. Used by `S3HttpStorage`'s retry
|
|
183
|
-
* loop so shutdown can interrupt an in-flight backoff instead of
|
|
184
|
-
* waiting for it to complete.
|
|
185
|
-
*/
|
|
186
|
-
const delay = (ms, signal) => new Promise((resolve, reject) => {
|
|
187
|
-
if (signal?.aborted) {
|
|
188
|
-
reject(signal.reason);
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
const timer = setTimeout(resolve, ms);
|
|
192
|
-
signal?.addEventListener("abort", () => {
|
|
193
|
-
clearTimeout(timer);
|
|
194
|
-
reject(signal.reason);
|
|
195
|
-
}, { once: true });
|
|
196
|
-
});
|
|
197
84
|
const utf8$1 = new TextEncoder();
|
|
198
85
|
const CONTROL_CHARS = /[\u0000-\u001f\u007f-\u009f]/;
|
|
199
86
|
/**
|
|
200
87
|
* The single validation rule for every caller-controlled key segment
|
|
201
88
|
* (`_id`, `collection`, `app`, `tenant`) before it becomes a written
|
|
202
89
|
* key segment or `LocalFsStorage` path. Rejects empty / `"/"` /
|
|
203
|
-
* `"."`|`".."` / C0-C1 control / leading `"_"` (ADR-
|
|
90
|
+
* `"."`|`".."` / C0-C1 control / leading `"_"` (ADR-003) / `> MAX_SEGMENT_BYTES`,
|
|
204
91
|
* all as `BaerlyError{code:"InvalidConfig"}`.
|
|
205
92
|
*
|
|
206
93
|
* @param role human label for the segment ("_id" | "collection" | "app" | "tenant").
|
|
@@ -222,7 +109,7 @@ const assertPathSegment = (value, role, verb = "") => {
|
|
|
222
109
|
* (index keys, log `doc_id`) or filesystem path (`LocalFsStorage`). The
|
|
223
110
|
* `_id` analogue of `assertKeySegment` (`db.ts`). Routes through the
|
|
224
111
|
* single shared {@link assertPathSegment} rule (empty / `"/"` /
|
|
225
|
-
* `"."`|`".."` / C0-C1 control chars / leading `"_"` (ADR-
|
|
112
|
+
* `"."`|`".."` / C0-C1 control chars / leading `"_"` (ADR-003) / overlong,
|
|
226
113
|
* all as `BaerlyError{code:"InvalidConfig"}`).
|
|
227
114
|
*/
|
|
228
115
|
const assertDocId = (id) => {
|
|
@@ -748,10 +635,6 @@ const assertKeyWithinLimit = (key) => {
|
|
|
748
635
|
* @see The soundness invariant in this module's header docstring.
|
|
749
636
|
*/
|
|
750
637
|
const tryAdoptOwnSessionLogEntry = (ctx) => {
|
|
751
|
-
if (ctx.batchSize !== 1) return {
|
|
752
|
-
adopt: false,
|
|
753
|
-
reason: "batch"
|
|
754
|
-
};
|
|
755
638
|
if (ctx.existing.session !== ctx.self.session) return {
|
|
756
639
|
adopt: false,
|
|
757
640
|
reason: "foreign-session"
|
|
@@ -781,8 +664,8 @@ const tryAdoptOwnSessionLogEntry = (ctx) => {
|
|
|
781
664
|
* point.** The writer writes NOTHING to `current.json` on the commit
|
|
782
665
|
* path; `tail_hint` advances outside ordinary commits via compaction
|
|
783
666
|
* folds, write-tick refreshes, and explicit operator/import paths. The
|
|
784
|
-
* commit model and its invariants are recorded in ADR-
|
|
785
|
-
* (`docs/adr/
|
|
667
|
+
* commit model and its invariants are recorded in ADR-004
|
|
668
|
+
* (`docs/adr/004-single-write-commit.md`). The
|
|
786
669
|
* optional integrity walk is gated by
|
|
787
670
|
* {@link WriterOptions.verifyLogIntegrityOnCommit} (default off).
|
|
788
671
|
*
|
|
@@ -802,7 +685,7 @@ const tryAdoptOwnSessionLogEntry = (ctx) => {
|
|
|
802
685
|
* `matchesWire` (read path) drops. Emitting `newKeys` before the commit
|
|
803
686
|
* means a committed row is ALWAYS index-findable; the stale-key DELETE
|
|
804
687
|
* stays after the commit so a crash can never de-index a committed
|
|
805
|
-
* doc. See `index-emit-order.test.ts` and ADR-
|
|
688
|
+
* doc. See `index-emit-order.test.ts` and ADR-004 Q4.
|
|
806
689
|
*
|
|
807
690
|
* **`LogEntry` shape.** Emitted entries follow the contract in
|
|
808
691
|
* `packages/protocol/src/log.ts` (see {@link LogEntry}) and
|
|
@@ -814,6 +697,7 @@ const tryAdoptOwnSessionLogEntry = (ctx) => {
|
|
|
814
697
|
* loop preserves.
|
|
815
698
|
* @see docs/spec/log-entry-shape.md for the per-field contract.
|
|
816
699
|
*/
|
|
700
|
+
const CAS_CONFLICT_RESOLUTION = "Re-read and retry.";
|
|
817
701
|
const ctxMetrics = () => getCurrentContext()?.recorder ?? noopMetricsRecorder;
|
|
818
702
|
const DEFAULT_INITIAL_BACKOFF_MS = 25;
|
|
819
703
|
const MAX_BACKOFF_MS = 1500;
|
|
@@ -825,7 +709,7 @@ const APPLICATION_JSON = "application/json";
|
|
|
825
709
|
* Pre-allocated module-level constant so every index PUT shares one
|
|
826
710
|
* zero-length buffer.
|
|
827
711
|
*/
|
|
828
|
-
const EMPTY_BODY = new Uint8Array(0);
|
|
712
|
+
const EMPTY_BODY = /* @__PURE__ */ new Uint8Array(0);
|
|
829
713
|
/**
|
|
830
714
|
* Stateless write engine for the multi-instance core.
|
|
831
715
|
*
|
|
@@ -918,7 +802,7 @@ var Writer = class {
|
|
|
918
802
|
for (let attempt = 1; attempt <= this.#maxRetries; attempt++) {
|
|
919
803
|
let success;
|
|
920
804
|
try {
|
|
921
|
-
success = await this.#singleAttemptCommit(
|
|
805
|
+
success = await this.#singleAttemptCommit(input, session, logPrefix, "Writer");
|
|
922
806
|
} catch (error) {
|
|
923
807
|
if (!isPreconditionFailed(error)) throw error;
|
|
924
808
|
await this.#backoff(attempt);
|
|
@@ -931,7 +815,7 @@ var Writer = class {
|
|
|
931
815
|
attempts: attempt
|
|
932
816
|
};
|
|
933
817
|
}
|
|
934
|
-
throw new BaerlyError("Conflict", `Writer: CAS conflict on ${this.#currentJsonKey} after ${this.#maxRetries} attempts
|
|
818
|
+
throw new BaerlyError("Conflict", `Writer: CAS conflict on ${this.#currentJsonKey} after ${this.#maxRetries} attempts`, void 0, void 0, void 0, CAS_CONFLICT_RESOLUTION, true);
|
|
935
819
|
}
|
|
936
820
|
/**
|
|
937
821
|
* One full commit attempt — the body of {@link commit}. Reads
|
|
@@ -941,9 +825,8 @@ var Writer = class {
|
|
|
941
825
|
* (single-write commit; no `current.json` CAS follows it). Stale index
|
|
942
826
|
* keys are DELETE'd after the commit. Returns the success payload.
|
|
943
827
|
*
|
|
944
|
-
*
|
|
945
|
-
*
|
|
946
|
-
* one entry.
|
|
828
|
+
* Commits exactly one {@link CommitInput} — the numbered log create is
|
|
829
|
+
* the single linearization point per call.
|
|
947
830
|
*
|
|
948
831
|
* A log-create 412 is disambiguated by session read-back: own session
|
|
949
832
|
* / own seq ⇒ our crashed-or-lost-ack commit is already durable ⇒
|
|
@@ -963,7 +846,7 @@ var Writer = class {
|
|
|
963
846
|
* inverse — committed log entry with missing content, or a de-indexed
|
|
964
847
|
* committed doc — is never produced.
|
|
965
848
|
*/
|
|
966
|
-
async #singleAttemptCommit(
|
|
849
|
+
async #singleAttemptCommit(input, session, logPrefix, errorPrefix) {
|
|
967
850
|
const read = await readCurrentJson(this.#storage, this.#currentJsonKey) ?? await this.#provisionCurrentJson();
|
|
968
851
|
const current = read.json;
|
|
969
852
|
const baseEtag = read.etag;
|
|
@@ -974,29 +857,41 @@ var Writer = class {
|
|
|
974
857
|
const commitNowMs = Date.now();
|
|
975
858
|
const commitTs = new Date(commitNowMs).toISOString();
|
|
976
859
|
const lsnTimestamp = timestamp(commitNowMs);
|
|
977
|
-
const input0 = inputs[0];
|
|
978
860
|
const mintEntry = (atSeq) => ({
|
|
979
861
|
lsn: `${lsnTimestamp}_${session}_${countKey(atSeq)}`,
|
|
980
862
|
commit_ts: commitTs,
|
|
981
|
-
op:
|
|
982
|
-
collection:
|
|
983
|
-
doc_id:
|
|
863
|
+
op: input.op,
|
|
864
|
+
collection: input.collection,
|
|
865
|
+
doc_id: input.docId,
|
|
984
866
|
session,
|
|
985
867
|
seq: atSeq,
|
|
986
|
-
...
|
|
987
|
-
...
|
|
868
|
+
...input.op !== "D" && input.body !== void 0 ? { after: input.body } : {},
|
|
869
|
+
...input.origin !== void 0 ? { origin: input.origin } : {}
|
|
988
870
|
});
|
|
989
871
|
let entry = mintEntry(seq);
|
|
990
872
|
let contentPutCount = 0;
|
|
991
873
|
let newKeysClassA = 0;
|
|
992
874
|
const parallelPuts = [];
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
875
|
+
if (input.op !== "D" && input.body !== void 0) {
|
|
876
|
+
contentPutCount++;
|
|
877
|
+
const bytes = encodeJsonBytes(input.body);
|
|
878
|
+
const contentKey = `${logPrefix}/content/${await versionFromContent(bytes)}.json`;
|
|
879
|
+
assertKeyWithinLimit(contentKey);
|
|
880
|
+
parallelPuts.push(this.#storage.put(contentKey, bytes, {
|
|
881
|
+
ifNoneMatch: "*",
|
|
882
|
+
contentType: APPLICATION_JSON
|
|
883
|
+
}).catch((error) => {
|
|
884
|
+
this.#observe429(error, input.collection);
|
|
885
|
+
if (isPreconditionFailed(error)) return;
|
|
886
|
+
throw error;
|
|
887
|
+
}));
|
|
888
|
+
}
|
|
889
|
+
if (this.#indexes.length > 0 && input.op !== "D") {
|
|
890
|
+
const newKeys = allIndexKeysFor(logPrefix, this.#indexes, input.body, input.docId);
|
|
891
|
+
for (const k of newKeys) {
|
|
892
|
+
assertKeyWithinLimit(k);
|
|
893
|
+
newKeysClassA++;
|
|
894
|
+
parallelPuts.push(this.#storage.put(k, EMPTY_BODY, {
|
|
1000
895
|
ifNoneMatch: "*",
|
|
1001
896
|
contentType: APPLICATION_JSON
|
|
1002
897
|
}).catch((error) => {
|
|
@@ -1005,21 +900,6 @@ var Writer = class {
|
|
|
1005
900
|
throw error;
|
|
1006
901
|
}));
|
|
1007
902
|
}
|
|
1008
|
-
if (this.#indexes.length > 0 && input.op !== "D") {
|
|
1009
|
-
const newKeys = allIndexKeysFor(logPrefix, this.#indexes, input.body, input.docId);
|
|
1010
|
-
for (const k of newKeys) {
|
|
1011
|
-
assertKeyWithinLimit(k);
|
|
1012
|
-
newKeysClassA++;
|
|
1013
|
-
parallelPuts.push(this.#storage.put(k, EMPTY_BODY, {
|
|
1014
|
-
ifNoneMatch: "*",
|
|
1015
|
-
contentType: APPLICATION_JSON
|
|
1016
|
-
}).catch((error) => {
|
|
1017
|
-
this.#observe429(error, input.collection);
|
|
1018
|
-
if (isPreconditionFailed(error)) return;
|
|
1019
|
-
throw error;
|
|
1020
|
-
}));
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
903
|
}
|
|
1024
904
|
await Promise.all(parallelPuts);
|
|
1025
905
|
let committedEntry = entry;
|
|
@@ -1055,8 +935,7 @@ var Writer = class {
|
|
|
1055
935
|
const existing = await readLogEntry(this.#storage, logEntryKey);
|
|
1056
936
|
const decision = tryAdoptOwnSessionLogEntry({
|
|
1057
937
|
self: entry,
|
|
1058
|
-
existing
|
|
1059
|
-
batchSize: inputs.length
|
|
938
|
+
existing
|
|
1060
939
|
});
|
|
1061
940
|
if (decision.adopt) {
|
|
1062
941
|
committedEntry = decision.entry;
|
|
@@ -1068,12 +947,12 @@ var Writer = class {
|
|
|
1068
947
|
}
|
|
1069
948
|
const committedEntries = [committedEntry];
|
|
1070
949
|
const staleKeysClassA = await this.#cleanupStaleIndexKeysBestEffort({
|
|
1071
|
-
|
|
950
|
+
input,
|
|
1072
951
|
logPrefix,
|
|
1073
952
|
seq
|
|
1074
953
|
});
|
|
1075
954
|
const indexClassA = newKeysClassA + staleKeysClassA;
|
|
1076
|
-
if (this.#indexes.length > 0 && indexClassA > 0) ctxMetrics().histogram("db.write.index_ops_per_logical_write", indexClassA, { collection:
|
|
955
|
+
if (this.#indexes.length > 0 && indexClassA > 0) ctxMetrics().histogram("db.write.index_ops_per_logical_write", indexClassA, { collection: input.collection });
|
|
1077
956
|
const prevSeq = preCommitTail;
|
|
1078
957
|
const observedTail = seq + 1;
|
|
1079
958
|
const maint = getCurrentContext()?.maintenance;
|
|
@@ -1139,40 +1018,34 @@ var Writer = class {
|
|
|
1139
1018
|
}
|
|
1140
1019
|
async #cleanupStaleIndexKeysBestEffort(args) {
|
|
1141
1020
|
if (this.#indexes.length === 0) return 0;
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
} else if (input.op === "D") {
|
|
1154
|
-
const preImage = inBatchImage.has(input.docId) ? inBatchImage.get(input.docId) : await this.#readPreImage(args.logPrefix, input.collection, input.docId, args.seq);
|
|
1155
|
-
staleKeys = allIndexKeysFor(args.logPrefix, this.#indexes, preImage, input.docId);
|
|
1156
|
-
}
|
|
1157
|
-
} catch {
|
|
1158
|
-
ctxMetrics().counter("db.write.index_cleanup_errors_total", 1, {
|
|
1159
|
-
collection: input.collection,
|
|
1160
|
-
step: "pre-image-read"
|
|
1161
|
-
});
|
|
1162
|
-
inBatchImage.set(input.docId, input.op === "D" ? void 0 : input.body);
|
|
1163
|
-
continue;
|
|
1021
|
+
const { input } = args;
|
|
1022
|
+
let staleKeys = [];
|
|
1023
|
+
try {
|
|
1024
|
+
if (input.op === "U") {
|
|
1025
|
+
const preImage = await this.#readPreImage(args.logPrefix, input.collection, input.docId, args.seq);
|
|
1026
|
+
const oldKeys = allIndexKeysFor(args.logPrefix, this.#indexes, preImage, input.docId);
|
|
1027
|
+
const newSet = new Set(allIndexKeysFor(args.logPrefix, this.#indexes, input.body, input.docId));
|
|
1028
|
+
staleKeys = oldKeys.filter((k) => !newSet.has(k));
|
|
1029
|
+
} else if (input.op === "D") {
|
|
1030
|
+
const preImage = await this.#readPreImage(args.logPrefix, input.collection, input.docId, args.seq);
|
|
1031
|
+
staleKeys = allIndexKeysFor(args.logPrefix, this.#indexes, preImage, input.docId);
|
|
1164
1032
|
}
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
staleKeysClassA += staleKeys.length;
|
|
1172
|
-
inBatchImage.set(input.docId, input.op === "D" ? void 0 : input.body);
|
|
1033
|
+
} catch {
|
|
1034
|
+
ctxMetrics().counter("db.write.index_cleanup_errors_total", 1, {
|
|
1035
|
+
collection: input.collection,
|
|
1036
|
+
step: "pre-image-read"
|
|
1037
|
+
});
|
|
1038
|
+
return 0;
|
|
1173
1039
|
}
|
|
1040
|
+
const indexDeletes = [];
|
|
1041
|
+
for (const k of staleKeys) indexDeletes.push(this.#storage.delete(k).catch(() => {
|
|
1042
|
+
ctxMetrics().counter("db.write.index_cleanup_errors_total", 1, {
|
|
1043
|
+
collection: input.collection,
|
|
1044
|
+
step: "delete"
|
|
1045
|
+
});
|
|
1046
|
+
}));
|
|
1174
1047
|
await Promise.all(indexDeletes);
|
|
1175
|
-
return
|
|
1048
|
+
return staleKeys.length;
|
|
1176
1049
|
}
|
|
1177
1050
|
/**
|
|
1178
1051
|
* Read the pre-image content body for a doc by walking the live log
|
|
@@ -1318,15 +1191,31 @@ const is429 = (err) => {
|
|
|
1318
1191
|
*
|
|
1319
1192
|
* @see ../../../docs/spec/sync-protocol.md
|
|
1320
1193
|
*/
|
|
1194
|
+
const DUPLICATE_ID_RESOLUTION = "Use different `_id` or omit it.";
|
|
1321
1195
|
/**
|
|
1322
|
-
* Serialise a {@link CurrentJson} head into
|
|
1323
|
-
* `
|
|
1324
|
-
*
|
|
1325
|
-
*
|
|
1196
|
+
* Serialise a {@link CurrentJson} head into an opaque wire cursor.
|
|
1197
|
+
* `tail` is the DISCOVERED tail (defaults to `json.tail_hint` when no
|
|
1198
|
+
* probe has run). Physical snapshot keys feed the digest but never
|
|
1199
|
+
* cross the HTTP boundary.
|
|
1326
1200
|
*
|
|
1327
1201
|
* @internal — exported for `query.test.ts`.
|
|
1328
1202
|
*/
|
|
1329
|
-
const
|
|
1203
|
+
const manifestPointerDigest = (seed) => {
|
|
1204
|
+
let hash = 14695981039346656037n;
|
|
1205
|
+
for (let i = 0; i < seed.length; i++) {
|
|
1206
|
+
hash ^= BigInt(seed.charCodeAt(i));
|
|
1207
|
+
hash = hash * 1099511628211n & 18446744073709551615n;
|
|
1208
|
+
}
|
|
1209
|
+
return hash.toString(36).padStart(13, "0");
|
|
1210
|
+
};
|
|
1211
|
+
const serializeManifestPointer = (json, tail = json.tail_hint) => `m1:${manifestPointerDigest([
|
|
1212
|
+
json.snapshot ?? "none",
|
|
1213
|
+
json.log_seq_start,
|
|
1214
|
+
json.tail_hint,
|
|
1215
|
+
json.snapshot_bytes,
|
|
1216
|
+
json.snapshot_rows,
|
|
1217
|
+
tail
|
|
1218
|
+
].join("|"))}`;
|
|
1330
1219
|
/**
|
|
1331
1220
|
* Returns the id iff `wire` carries exactly one clause and that
|
|
1332
1221
|
* clause is `{op:"eq", field:"_id", value:<string>}` — the shape
|
|
@@ -1509,7 +1398,7 @@ const runInsert = async (ctx, doc) => {
|
|
|
1509
1398
|
}] },
|
|
1510
1399
|
order: void 0,
|
|
1511
1400
|
limit: 1
|
|
1512
|
-
})).rows.length > 0) throw new BaerlyError("Conflict", `Query.insert: _id ${JSON.stringify(_id)} already exists in collection ${JSON.stringify(ctx.collectionName)}
|
|
1401
|
+
})).rows.length > 0) throw new BaerlyError("Conflict", `Query.insert: _id ${JSON.stringify(_id)} already exists in collection ${JSON.stringify(ctx.collectionName)}`, void 0, void 0, void 0, DUPLICATE_ID_RESOLUTION, false);
|
|
1513
1402
|
await writerFor(ctx).commit({
|
|
1514
1403
|
op: "I",
|
|
1515
1404
|
collection: ctx.collectionName,
|
|
@@ -1649,7 +1538,7 @@ const runRead = async (ctx, state) => {
|
|
|
1649
1538
|
const fresh = true;
|
|
1650
1539
|
if (head === null) return {
|
|
1651
1540
|
rows: [],
|
|
1652
|
-
manifestPointer:
|
|
1541
|
+
manifestPointer: "m1:0000000000000",
|
|
1653
1542
|
fresh
|
|
1654
1543
|
};
|
|
1655
1544
|
const plan = planQuery(state.wire, ctx.indexes);
|
|
@@ -1879,6 +1768,6 @@ const sortByOrderSpec = (rows, spec) => {
|
|
|
1879
1768
|
});
|
|
1880
1769
|
};
|
|
1881
1770
|
//#endregion
|
|
1882
|
-
export { runReplaceById as a,
|
|
1771
|
+
export { runReplaceById as a, runInsert as i, runAllWithMeta as n, validateOrThrow as o, runByIdWithMeta as r, assertPathSegment as s, makeQuery as t };
|
|
1883
1772
|
|
|
1884
|
-
//# sourceMappingURL=query-
|
|
1773
|
+
//# sourceMappingURL=query-rAtXNQQB.js.map
|