effect-dynamodb 1.42.2 → 1.44.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.
- package/dist/Aggregate.d.ts +157 -20
- package/dist/Aggregate.d.ts.map +1 -1
- package/dist/Aggregate.js +1219 -286
- package/dist/Aggregate.js.map +1 -1
- package/dist/Batch.d.ts +17 -4
- package/dist/Batch.d.ts.map +1 -1
- package/dist/Batch.js +129 -62
- package/dist/Batch.js.map +1 -1
- package/dist/BlobGc.d.ts +243 -0
- package/dist/BlobGc.d.ts.map +1 -0
- package/dist/BlobGc.js +961 -0
- package/dist/BlobGc.js.map +1 -0
- package/dist/BlobStore.d.ts +83 -0
- package/dist/BlobStore.d.ts.map +1 -0
- package/dist/BlobStore.js +70 -0
- package/dist/BlobStore.js.map +1 -0
- package/dist/Collection.d.ts +88 -8
- package/dist/Collection.d.ts.map +1 -1
- package/dist/Collection.js +22 -26
- package/dist/Collection.js.map +1 -1
- package/dist/Collections.d.ts +0 -7
- package/dist/Collections.d.ts.map +1 -1
- package/dist/Collections.js +0 -16
- package/dist/Collections.js.map +1 -1
- package/dist/DynamoClient.d.ts +68 -35
- package/dist/DynamoClient.d.ts.map +1 -1
- package/dist/DynamoClient.js +58 -12
- package/dist/DynamoClient.js.map +1 -1
- package/dist/Entity.d.ts +403 -104
- package/dist/Entity.d.ts.map +1 -1
- package/dist/Entity.js +1771 -382
- package/dist/Entity.js.map +1 -1
- package/dist/EventStore.d.ts +323 -77
- package/dist/EventStore.d.ts.map +1 -1
- package/dist/EventStore.js +473 -96
- package/dist/EventStore.js.map +1 -1
- package/dist/Offload.d.ts +199 -0
- package/dist/Offload.d.ts.map +1 -0
- package/dist/Offload.js +120 -0
- package/dist/Offload.js.map +1 -0
- package/dist/Query.d.ts +132 -59
- package/dist/Query.d.ts.map +1 -1
- package/dist/Query.js +95 -31
- package/dist/Query.js.map +1 -1
- package/dist/StorageEncoding.d.ts +62 -15
- package/dist/StorageEncoding.d.ts.map +1 -1
- package/dist/StorageEncoding.js +108 -24
- package/dist/StorageEncoding.js.map +1 -1
- package/dist/Table.d.ts +1 -1
- package/dist/Table.d.ts.map +1 -1
- package/dist/Transaction.d.ts +16 -3
- package/dist/Transaction.d.ts.map +1 -1
- package/dist/Transaction.js +28 -9
- package/dist/Transaction.js.map +1 -1
- package/dist/blob/S3BlobStore.d.ts +178 -0
- package/dist/blob/S3BlobStore.d.ts.map +1 -0
- package/dist/blob/S3BlobStore.js +770 -0
- package/dist/blob/S3BlobStore.js.map +1 -0
- package/dist/blob/s3.d.ts +122 -0
- package/dist/blob/s3.d.ts.map +1 -0
- package/dist/blob/s3.js +143 -0
- package/dist/blob/s3.js.map +1 -0
- package/dist/index.d.ts +9 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/internal/AggregateWrite.d.ts +130 -0
- package/dist/internal/AggregateWrite.d.ts.map +1 -0
- package/dist/internal/AggregateWrite.js +341 -0
- package/dist/internal/AggregateWrite.js.map +1 -0
- package/dist/internal/BlobCalls.d.ts +70 -0
- package/dist/internal/BlobCalls.d.ts.map +1 -0
- package/dist/internal/BlobCalls.js +105 -0
- package/dist/internal/BlobCalls.js.map +1 -0
- package/dist/internal/BlobKeys.d.ts +100 -0
- package/dist/internal/BlobKeys.d.ts.map +1 -0
- package/dist/internal/BlobKeys.js +135 -0
- package/dist/internal/BlobKeys.js.map +1 -0
- package/dist/internal/BlobLedger.d.ts +323 -0
- package/dist/internal/BlobLedger.d.ts.map +1 -0
- package/dist/internal/BlobLedger.js +1012 -0
- package/dist/internal/BlobLedger.js.map +1 -0
- package/dist/internal/BlobMetadata.d.ts +109 -0
- package/dist/internal/BlobMetadata.d.ts.map +1 -0
- package/dist/internal/BlobMetadata.js +249 -0
- package/dist/internal/BlobMetadata.js.map +1 -0
- package/dist/internal/BlobResolver.d.ts +146 -0
- package/dist/internal/BlobResolver.d.ts.map +1 -0
- package/dist/internal/BlobResolver.js +417 -0
- package/dist/internal/BlobResolver.js.map +1 -0
- package/dist/internal/BlobStoreContract.d.ts +214 -0
- package/dist/internal/BlobStoreContract.d.ts.map +1 -0
- package/dist/internal/BlobStoreContract.js +181 -0
- package/dist/internal/BlobStoreContract.js.map +1 -0
- package/dist/internal/BoundCrud.d.ts +153 -53
- package/dist/internal/BoundCrud.d.ts.map +1 -1
- package/dist/internal/BoundCrud.js +27 -5
- package/dist/internal/BoundCrud.js.map +1 -1
- package/dist/internal/BoundQuery.d.ts +49 -33
- package/dist/internal/BoundQuery.d.ts.map +1 -1
- package/dist/internal/BoundQuery.js +8 -2
- package/dist/internal/BoundQuery.js.map +1 -1
- package/dist/internal/BoundVectorQuery.d.ts +59 -25
- package/dist/internal/BoundVectorQuery.d.ts.map +1 -1
- package/dist/internal/BoundVectorQuery.js +38 -17
- package/dist/internal/BoundVectorQuery.js.map +1 -1
- package/dist/internal/Capacity.d.ts +17 -2
- package/dist/internal/Capacity.d.ts.map +1 -1
- package/dist/internal/Capacity.js +47 -9
- package/dist/internal/Capacity.js.map +1 -1
- package/dist/internal/ConditionHolds.d.ts.map +1 -1
- package/dist/internal/ConditionHolds.js +2 -8
- package/dist/internal/ConditionHolds.js.map +1 -1
- package/dist/internal/CredentialRefusal.d.ts.map +1 -1
- package/dist/internal/CredentialRefusal.js +4 -0
- package/dist/internal/CredentialRefusal.js.map +1 -1
- package/dist/internal/Digest.d.ts +4 -3
- package/dist/internal/Digest.d.ts.map +1 -1
- package/dist/internal/Digest.js +25 -82
- package/dist/internal/Digest.js.map +1 -1
- package/dist/internal/EntityCombinators.d.ts +93 -45
- package/dist/internal/EntityCombinators.d.ts.map +1 -1
- package/dist/internal/EntityCombinators.js +42 -7
- package/dist/internal/EntityCombinators.js.map +1 -1
- package/dist/internal/EntityOps.d.ts +47 -6
- package/dist/internal/EntityOps.d.ts.map +1 -1
- package/dist/internal/EntityOps.js +30 -10
- package/dist/internal/EntityOps.js.map +1 -1
- package/dist/internal/EventProjection.d.ts +99 -25
- package/dist/internal/EventProjection.d.ts.map +1 -1
- package/dist/internal/EventProjection.js +124 -33
- package/dist/internal/EventProjection.js.map +1 -1
- package/dist/internal/ExpressionLimits.d.ts +22 -3
- package/dist/internal/ExpressionLimits.d.ts.map +1 -1
- package/dist/internal/ExpressionLimits.js +46 -7
- package/dist/internal/ExpressionLimits.js.map +1 -1
- package/dist/internal/GcSupport.d.ts +258 -0
- package/dist/internal/GcSupport.d.ts.map +1 -0
- package/dist/internal/GcSupport.js +284 -0
- package/dist/internal/GcSupport.js.map +1 -0
- package/dist/internal/ItemSize.d.ts +7 -1
- package/dist/internal/ItemSize.d.ts.map +1 -1
- package/dist/internal/ItemSize.js +13 -5
- package/dist/internal/ItemSize.js.map +1 -1
- package/dist/internal/MemoryBlobStore.d.ts +203 -0
- package/dist/internal/MemoryBlobStore.d.ts.map +1 -0
- package/dist/internal/MemoryBlobStore.js +623 -0
- package/dist/internal/MemoryBlobStore.js.map +1 -0
- package/dist/internal/MultiProjection.d.ts +56 -28
- package/dist/internal/MultiProjection.d.ts.map +1 -1
- package/dist/internal/MultiProjection.js +9 -3
- package/dist/internal/MultiProjection.js.map +1 -1
- package/dist/internal/Multipart.d.ts +96 -0
- package/dist/internal/Multipart.d.ts.map +1 -0
- package/dist/internal/Multipart.js +143 -0
- package/dist/internal/Multipart.js.map +1 -0
- package/dist/internal/OffloadChange.d.ts +187 -0
- package/dist/internal/OffloadChange.d.ts.map +1 -0
- package/dist/internal/OffloadChange.js +391 -0
- package/dist/internal/OffloadChange.js.map +1 -0
- package/dist/internal/OffloadInput.d.ts +141 -0
- package/dist/internal/OffloadInput.d.ts.map +1 -0
- package/dist/internal/OffloadInput.js +236 -0
- package/dist/internal/OffloadInput.js.map +1 -0
- package/dist/internal/OffloadRead.d.ts +140 -0
- package/dist/internal/OffloadRead.d.ts.map +1 -0
- package/dist/internal/OffloadRead.js +369 -0
- package/dist/internal/OffloadRead.js.map +1 -0
- package/dist/internal/OffloadTypes.d.ts +182 -0
- package/dist/internal/OffloadTypes.d.ts.map +1 -0
- package/dist/internal/OffloadTypes.js +15 -0
- package/dist/internal/OffloadTypes.js.map +1 -0
- package/dist/internal/OffloadValues.d.ts +87 -0
- package/dist/internal/OffloadValues.d.ts.map +1 -0
- package/dist/internal/OffloadValues.js +114 -0
- package/dist/internal/OffloadValues.js.map +1 -0
- package/dist/internal/OffloadWrite.d.ts +267 -0
- package/dist/internal/OffloadWrite.d.ts.map +1 -0
- package/dist/internal/OffloadWrite.js +766 -0
- package/dist/internal/OffloadWrite.js.map +1 -0
- package/dist/internal/Packed.d.ts +58 -23
- package/dist/internal/Packed.d.ts.map +1 -1
- package/dist/internal/Packed.js +108 -33
- package/dist/internal/Packed.js.map +1 -1
- package/dist/internal/ProjectionEngine.d.ts +28 -3
- package/dist/internal/ProjectionEngine.d.ts.map +1 -1
- package/dist/internal/ProjectionEngine.js +47 -9
- package/dist/internal/ProjectionEngine.js.map +1 -1
- package/dist/internal/RangePlanner.d.ts +3 -6
- package/dist/internal/RangePlanner.d.ts.map +1 -1
- package/dist/internal/RangePlanner.js +3 -29
- package/dist/internal/RangePlanner.js.map +1 -1
- package/dist/internal/Rekey.d.ts +16 -0
- package/dist/internal/Rekey.d.ts.map +1 -1
- package/dist/internal/Rekey.js +31 -2
- package/dist/internal/Rekey.js.map +1 -1
- package/dist/internal/StoredPointer.d.ts +19 -0
- package/dist/internal/StoredPointer.d.ts.map +1 -0
- package/dist/internal/StoredPointer.js +59 -0
- package/dist/internal/StoredPointer.js.map +1 -0
- package/dist/internal/TransactPlan.d.ts +18 -1
- package/dist/internal/TransactPlan.d.ts.map +1 -1
- package/dist/internal/TransactPlan.js +50 -10
- package/dist/internal/TransactPlan.js.map +1 -1
- package/dist/internal/TransactWriteOps.d.ts +33 -12
- package/dist/internal/TransactWriteOps.d.ts.map +1 -1
- package/dist/internal/TransactWriteOps.js +49 -19
- package/dist/internal/TransactWriteOps.js.map +1 -1
- package/dist/internal/TransactableOps.d.ts +58 -11
- package/dist/internal/TransactableOps.d.ts.map +1 -1
- package/dist/internal/TransactableOps.js +95 -19
- package/dist/internal/TransactableOps.js.map +1 -1
- package/dist/internal/Utf8Order.d.ts +21 -0
- package/dist/internal/Utf8Order.d.ts.map +1 -0
- package/dist/internal/Utf8Order.js +44 -0
- package/dist/internal/Utf8Order.js.map +1 -0
- package/dist/internal/WriteDiagnosis.d.ts +7 -1
- package/dist/internal/WriteDiagnosis.d.ts.map +1 -1
- package/dist/internal/WriteDiagnosis.js +12 -16
- package/dist/internal/WriteDiagnosis.js.map +1 -1
- package/package.json +15 -4
|
@@ -0,0 +1,766 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal The write side of offloaded fields (#179, design §6.1–§6.5,
|
|
3
|
+
* §6.8, §6.13): which values of a row go to the blob store (per-field
|
|
4
|
+
* thresholds, then the item budget), their uploads through the operation's
|
|
5
|
+
* ledger, and the pointers that replace them in the request. (A write's
|
|
6
|
+
* result reads what it stored through `OffloadRead`, its own uploads from the
|
|
7
|
+
* ledger's read cache.)
|
|
8
|
+
*
|
|
9
|
+
* Everything here works on **stored** attribute names, after renames, sparse
|
|
10
|
+
* flattening and packing: an overflow value arrives as its inline stored form
|
|
11
|
+
* (a JSON string, or a v1 header byte + compressed bytes) and leaves as a `B`
|
|
12
|
+
* pointer; a binary value arrives as its bytes and leaves as `~edbp:` text.
|
|
13
|
+
* A binary value given as a stream, a handle or `Offload.source(...)` arrives
|
|
14
|
+
* as its placeholder (`OffloadInput`), which uploads what it stands for.
|
|
15
|
+
*
|
|
16
|
+
* Not part of the public API.
|
|
17
|
+
*/
|
|
18
|
+
import { ValidationError } from "@effect-dynamodb/schema/Errors.js";
|
|
19
|
+
import { decode as decodePointer, decodeText as decodePointerText, encode as encodePointer, encodeText as encodePointerText, isPointerHeader, isPointerText, make as makePointer, } from "@effect-dynamodb/schema/internal/BlobPointer.js";
|
|
20
|
+
import { crc64nvme } from "@effect-dynamodb/schema/internal/Crc64Nvme.js";
|
|
21
|
+
import { ownValue, setOwn } from "@effect-dynamodb/schema/internal/OwnNames.js";
|
|
22
|
+
import { parseHeader } from "@effect-dynamodb/schema/internal/PackedCodec.js";
|
|
23
|
+
import { Cause, Context, Effect, Exit, Option } from "effect";
|
|
24
|
+
import { BlobStore } from "../BlobStore.js";
|
|
25
|
+
import { toAttributeMap, toAttributeValue } from "../Marshaller.js";
|
|
26
|
+
import { blobError } from "./BlobCalls.js";
|
|
27
|
+
import { objectKey } from "./BlobKeys.js";
|
|
28
|
+
import * as BlobLedger from "./BlobLedger.js";
|
|
29
|
+
import * as BlobMetadata from "./BlobMetadata.js";
|
|
30
|
+
import { attributeBytes, itemBytes, utf8Bytes } from "./ItemSize.js";
|
|
31
|
+
import * as OffloadInput from "./OffloadInput.js";
|
|
32
|
+
import { currentRegistry } from "./Packed.js";
|
|
33
|
+
/** The stored attributes of a member that may hold a pointer. */
|
|
34
|
+
export const offloadedAttributes = (info) => [
|
|
35
|
+
...Object.keys(info.overflow),
|
|
36
|
+
...Object.keys(info.binary),
|
|
37
|
+
];
|
|
38
|
+
/** True iff `attribute` is one of the member's offloaded attributes. */
|
|
39
|
+
export const isOffloaded = (info, attribute) => Object.hasOwn(info.overflow, attribute) || Object.hasOwn(info.binary, attribute);
|
|
40
|
+
/** A key attribute value of a stored-name item (its composed `S` value, as a rule). */
|
|
41
|
+
export const keyValueOf = (value) => toAttributeValue(value);
|
|
42
|
+
const plainKeyValue = (value) => {
|
|
43
|
+
const v = value;
|
|
44
|
+
return v.S ?? (v.N !== undefined ? Number(v.N) : v.B);
|
|
45
|
+
};
|
|
46
|
+
/** The row of a stored-name item whose key attributes are `pkAttribute` / `skAttribute`. */
|
|
47
|
+
export const rowOf = (item, keys) => ({
|
|
48
|
+
table: keys.table,
|
|
49
|
+
pkAttribute: keys.pkAttribute,
|
|
50
|
+
pk: keyValueOf(item[keys.pkAttribute]),
|
|
51
|
+
skAttribute: keys.skAttribute,
|
|
52
|
+
sk: keyValueOf(item[keys.skAttribute]),
|
|
53
|
+
operation: keys.operation,
|
|
54
|
+
escapeKeys: keys.escapeKeys,
|
|
55
|
+
});
|
|
56
|
+
/**
|
|
57
|
+
* The `[EDD-9146]` text: a member offloads on a schema without `escapeKeys:
|
|
58
|
+
* true`. `what` names the member and its fields; `where` the bind site or the
|
|
59
|
+
* write.
|
|
60
|
+
*/
|
|
61
|
+
export const unescapedKeysText = (where, what) => `[EDD-9146] ${where}: ${what}, but its table's schema doesn't set escapeKeys: true, which ` +
|
|
62
|
+
"offloading requires. An offloaded value's object key is named from its row's key text and " +
|
|
63
|
+
"S3 has no rename, so a row holding a pointer must be written only at its final key text and " +
|
|
64
|
+
"never moved by rekey. Set DynamoSchema.make({ ..., escapeKeys: true }) — over existing rows, " +
|
|
65
|
+
"run db.tables.<Table>.rekey() before serving traffic (see the offloaded storage guide).";
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
// Store
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
const notProvided = (info, attribute, operation) => blobError({ entityType: info.entityType, attribute, operation }, "put", "NotProvided", undefined, "no BlobStore is in context: provide one (DynamoClient.make({ blobStore }), BlobStore.layer, " +
|
|
70
|
+
"or the S3 store's layer) where the operation runs or the client is bound");
|
|
71
|
+
/** The store in context, if any (`Effect.serviceOption`: no requirement). */
|
|
72
|
+
const currentStore = Effect.map(Effect.serviceOption(BlobStore), (store) => Option.getOrUndefined(store));
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
74
|
+
// Content
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
const JSON_CONTENT_TYPE = "application/json";
|
|
77
|
+
const OCTET_STREAM = "application/octet-stream";
|
|
78
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
79
|
+
const invalid = (info, attribute, operation, message) => new ValidationError({
|
|
80
|
+
entityType: info.entityType,
|
|
81
|
+
operation,
|
|
82
|
+
cause: new Error(`Offloaded attribute "${attribute}": ${message}`),
|
|
83
|
+
});
|
|
84
|
+
/**
|
|
85
|
+
* The upload of an overflow attribute's inline stored value: its JSON text
|
|
86
|
+
* (codec none, `application/json`), or the compressed payload after the
|
|
87
|
+
* header byte (`application/octet-stream`, the content its decompression).
|
|
88
|
+
* `contentSize` is left `undefined` for a compressed value when `measure`
|
|
89
|
+
* only (routing): the pointer bound then counts the largest content size.
|
|
90
|
+
*/
|
|
91
|
+
const overflowUpload = (info, attribute, stored, registry, operation) => {
|
|
92
|
+
if (typeof stored === "string") {
|
|
93
|
+
const bytes = utf8Bytes(stored);
|
|
94
|
+
return {
|
|
95
|
+
kind: "json",
|
|
96
|
+
content: { kind: "json", text: stored },
|
|
97
|
+
codec: "none",
|
|
98
|
+
body: undefined,
|
|
99
|
+
contentType: JSON_CONTENT_TYPE,
|
|
100
|
+
pointerContentType: undefined,
|
|
101
|
+
objectSize: bytes,
|
|
102
|
+
contentSize: bytes,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const header = stored.length === 0 ? "the stored value is empty" : parseHeader(stored[0]);
|
|
106
|
+
if (typeof header === "string")
|
|
107
|
+
return invalid(info, attribute, operation, header);
|
|
108
|
+
if (header.version !== 1) {
|
|
109
|
+
return invalid(info, attribute, operation, "the value to upload is already a blob pointer");
|
|
110
|
+
}
|
|
111
|
+
const body = stored.slice(1);
|
|
112
|
+
if (registry === undefined) {
|
|
113
|
+
return {
|
|
114
|
+
kind: "json",
|
|
115
|
+
content: { kind: "json", text: "" },
|
|
116
|
+
codec: header.codec,
|
|
117
|
+
body,
|
|
118
|
+
contentType: header.codec === "none" ? JSON_CONTENT_TYPE : OCTET_STREAM,
|
|
119
|
+
pointerContentType: undefined,
|
|
120
|
+
objectSize: body.length,
|
|
121
|
+
contentSize: undefined,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
let payload = body;
|
|
125
|
+
if (header.codec !== "none") {
|
|
126
|
+
const compressor = registry.get(header.codec);
|
|
127
|
+
if (compressor === undefined) {
|
|
128
|
+
return invalid(info, attribute, operation, `no "${header.codec}" compressor is registered`);
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
payload = compressor.decompress(body);
|
|
132
|
+
}
|
|
133
|
+
catch (cause) {
|
|
134
|
+
return new ValidationError({
|
|
135
|
+
entityType: info.entityType,
|
|
136
|
+
operation,
|
|
137
|
+
cause: new Error(`Offloaded attribute "${attribute}": the "${header.codec}" decompressor failed`, { cause }),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
let text;
|
|
142
|
+
try {
|
|
143
|
+
text = decoder.decode(payload);
|
|
144
|
+
}
|
|
145
|
+
catch (cause) {
|
|
146
|
+
return new ValidationError({
|
|
147
|
+
entityType: info.entityType,
|
|
148
|
+
operation,
|
|
149
|
+
cause: new Error(`Offloaded attribute "${attribute}": the payload is not UTF-8`, { cause }),
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
kind: "json",
|
|
154
|
+
content: { kind: "json", text },
|
|
155
|
+
codec: header.codec,
|
|
156
|
+
body: header.codec === "none" ? undefined : body,
|
|
157
|
+
contentType: header.codec === "none" ? JSON_CONTENT_TYPE : OCTET_STREAM,
|
|
158
|
+
pointerContentType: undefined,
|
|
159
|
+
objectSize: body.length,
|
|
160
|
+
contentSize: payload.length,
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
/** Largest LEB128 content size a pointer can record (8 groups, every safe integer). */
|
|
164
|
+
const MAX_CONTENT_SIZE = Number.MAX_SAFE_INTEGER;
|
|
165
|
+
/**
|
|
166
|
+
* The upload of a binary attribute's value (design §3.8, §4.4): its bytes, or
|
|
167
|
+
* what its placeholder stands for — a stream (single-use), a handle's content
|
|
168
|
+
* to copy, or `Offload.source` bytes. The content type is the source's, else
|
|
169
|
+
* the field's `External({ contentType })`.
|
|
170
|
+
*/
|
|
171
|
+
const binaryUpload = (config, bytes) => {
|
|
172
|
+
const prepared = OffloadInput.preparedOf(bytes);
|
|
173
|
+
if (prepared?._tag === "Stream") {
|
|
174
|
+
const contentType = prepared.contentType ?? config.contentType;
|
|
175
|
+
return {
|
|
176
|
+
kind: "bytes",
|
|
177
|
+
content: {
|
|
178
|
+
kind: "stream",
|
|
179
|
+
stream: prepared.stream,
|
|
180
|
+
identity: prepared.identity,
|
|
181
|
+
contentLength: prepared.contentLength,
|
|
182
|
+
},
|
|
183
|
+
codec: "none",
|
|
184
|
+
body: undefined,
|
|
185
|
+
contentType,
|
|
186
|
+
pointerContentType: contentType,
|
|
187
|
+
objectSize: prepared.contentLength ?? MAX_CONTENT_SIZE,
|
|
188
|
+
contentSize: prepared.contentLength,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
if (prepared?._tag === "Handle") {
|
|
192
|
+
return {
|
|
193
|
+
kind: "bytes",
|
|
194
|
+
content: { kind: "copy", handle: prepared.handle },
|
|
195
|
+
codec: "none",
|
|
196
|
+
body: undefined,
|
|
197
|
+
contentType: config.contentType,
|
|
198
|
+
pointerContentType: config.contentType,
|
|
199
|
+
objectSize: prepared.handle.size,
|
|
200
|
+
contentSize: prepared.handle.size,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
const contentType = prepared?.contentType ?? config.contentType;
|
|
204
|
+
return {
|
|
205
|
+
kind: "bytes",
|
|
206
|
+
content: { kind: "bytes", bytes },
|
|
207
|
+
codec: "none",
|
|
208
|
+
body: undefined,
|
|
209
|
+
contentType,
|
|
210
|
+
pointerContentType: contentType,
|
|
211
|
+
objectSize: bytes.length,
|
|
212
|
+
contentSize: bytes.length,
|
|
213
|
+
};
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* The bytes the attribute's pointer can take at most (design §6.3): the real
|
|
217
|
+
* key extended by the store's `maxKeySuffixBytes`, a version of
|
|
218
|
+
* `maxVersionBytes`, the real sizes (the largest content size when not yet
|
|
219
|
+
* known) and the content type — in the attribute's stored form (`B` bytes for
|
|
220
|
+
* an overflow value, the `~edbp:` text's UTF-8 bytes for a binary one).
|
|
221
|
+
*/
|
|
222
|
+
const pointerBound = (store, key, upload) => {
|
|
223
|
+
const pointer = makePointer({
|
|
224
|
+
kind: upload.kind,
|
|
225
|
+
codec: upload.codec,
|
|
226
|
+
ref: {
|
|
227
|
+
key: key + "k".repeat(store.limits.maxKeySuffixBytes),
|
|
228
|
+
version: "v".repeat(store.limits.maxVersionBytes),
|
|
229
|
+
},
|
|
230
|
+
objectSize: upload.objectSize,
|
|
231
|
+
contentSize: upload.contentSize ?? MAX_CONTENT_SIZE,
|
|
232
|
+
checksum: { algorithm: "crc64nvme", value: new Uint8Array(8) },
|
|
233
|
+
contentType: Option.fromNullishOr(upload.pointerContentType),
|
|
234
|
+
});
|
|
235
|
+
if (typeof pointer === "string") {
|
|
236
|
+
throw new Error(`OffloadWrite: no pointer bound for "${key}": ${pointer}`);
|
|
237
|
+
}
|
|
238
|
+
return upload.kind === "json"
|
|
239
|
+
? encodePointer(pointer).length
|
|
240
|
+
: utf8Bytes(encodePointerText(pointer));
|
|
241
|
+
};
|
|
242
|
+
// ---------------------------------------------------------------------------
|
|
243
|
+
// Upload
|
|
244
|
+
// ---------------------------------------------------------------------------
|
|
245
|
+
const keyOf = (info, store, row, attribute) => objectKey(info.prefix ?? store.prefix, {
|
|
246
|
+
table: row.table,
|
|
247
|
+
pk: row.pk,
|
|
248
|
+
familySk: row.sk,
|
|
249
|
+
rowSk: row.rowSk ?? row.sk,
|
|
250
|
+
attribute,
|
|
251
|
+
});
|
|
252
|
+
/** The stored form of `pointer` on the attribute: `B` (overflow) or `~edbp:` text (binary). */
|
|
253
|
+
export const storedPointer = (pointer) => pointer.kind === "json" ? encodePointer(pointer) : encodePointerText(pointer);
|
|
254
|
+
const uploadOne = (info, store, row, attribute, upload) => {
|
|
255
|
+
const rowSk = row.rowSk ?? row.sk;
|
|
256
|
+
return BlobLedger.upload({
|
|
257
|
+
store,
|
|
258
|
+
key: keyOf(info, store, row, attribute),
|
|
259
|
+
content: upload.content,
|
|
260
|
+
codec: upload.codec,
|
|
261
|
+
body: upload.body,
|
|
262
|
+
contentType: upload.contentType,
|
|
263
|
+
pointerContentType: upload.pointerContentType,
|
|
264
|
+
// Built from the ledger's own checksum of the content: computed once.
|
|
265
|
+
metadata: (checksum) => BlobMetadata.build({
|
|
266
|
+
table: row.table,
|
|
267
|
+
entity: info.entityType,
|
|
268
|
+
attribute,
|
|
269
|
+
pk: row.pk,
|
|
270
|
+
sk: rowSk,
|
|
271
|
+
kind: upload.kind,
|
|
272
|
+
codec: upload.codec,
|
|
273
|
+
checksum,
|
|
274
|
+
}),
|
|
275
|
+
target: {
|
|
276
|
+
table: row.table,
|
|
277
|
+
entityType: info.entityType,
|
|
278
|
+
attribute,
|
|
279
|
+
rowKey: {
|
|
280
|
+
[row.pkAttribute]: plainKeyValue(row.pk),
|
|
281
|
+
[row.skAttribute]: plainKeyValue(rowSk),
|
|
282
|
+
},
|
|
283
|
+
operation: row.operation,
|
|
284
|
+
copiesPointers: info.copiesPointers,
|
|
285
|
+
},
|
|
286
|
+
});
|
|
287
|
+
};
|
|
288
|
+
/** The inline stored value of an offloaded attribute that can be uploaded, if any. */
|
|
289
|
+
const uploadable = (info, attribute, value) => {
|
|
290
|
+
if (Object.hasOwn(info.binary, attribute))
|
|
291
|
+
return value instanceof Uint8Array;
|
|
292
|
+
if (!Object.hasOwn(info.overflow, attribute))
|
|
293
|
+
return false;
|
|
294
|
+
if (typeof value === "string")
|
|
295
|
+
return true;
|
|
296
|
+
return value instanceof Uint8Array && value.length > 0 && !isPointerHeader(value[0]);
|
|
297
|
+
};
|
|
298
|
+
const storedBytes = (value) => typeof value === "string" ? utf8Bytes(value) : value.length;
|
|
299
|
+
/** An overflow value at or above its threshold, or any binary value: offloaded whatever the item. */
|
|
300
|
+
const overThreshold = (info, attribute, value) => Object.hasOwn(info.binary, attribute) ||
|
|
301
|
+
storedBytes(value) >= info.overflow[attribute].external.threshold;
|
|
302
|
+
/**
|
|
303
|
+
* Run `f` over `items` concurrently and fail only once every one has settled
|
|
304
|
+
* (design §6.2): an upload already sent is let finish rather than interrupted
|
|
305
|
+
* by a sibling's failure, so the version it writes is registered with the
|
|
306
|
+
* ledger and rolled back at exit — an interrupted one would leave a version no
|
|
307
|
+
* entry names. Fails with the first failure in `items` order, preferring a
|
|
308
|
+
* failure over an interruption.
|
|
309
|
+
*/
|
|
310
|
+
export const forEachSettled = (items, f) => Effect.flatMap(Effect.forEach(items, (item) => Effect.exit(f(item)), { concurrency: "unbounded" }), (exits) => {
|
|
311
|
+
let interrupted;
|
|
312
|
+
for (const exit of exits) {
|
|
313
|
+
if (Exit.isSuccess(exit))
|
|
314
|
+
continue;
|
|
315
|
+
if (!Cause.hasInterruptsOnly(exit.cause))
|
|
316
|
+
return Effect.failCause(exit.cause);
|
|
317
|
+
interrupted ??= exit.cause;
|
|
318
|
+
}
|
|
319
|
+
return interrupted === undefined ? Effect.void : Effect.failCause(interrupted);
|
|
320
|
+
});
|
|
321
|
+
/**
|
|
322
|
+
* Route and upload a whole item (design §6.3), in place: every offloaded
|
|
323
|
+
* attribute of `only` (default: all of them) at or above its threshold, every
|
|
324
|
+
* binary one, then — when the member has an `itemBudget` and the item, with
|
|
325
|
+
* those as pointers at their upper bound, is still over it — the remaining
|
|
326
|
+
* overflow attributes by stored size, largest first (ties by name), until it
|
|
327
|
+
* fits. The item may still exceed the budget (DynamoDB's own limit is then
|
|
328
|
+
* diagnosed on failure). Each upload goes through the operation's ledger and
|
|
329
|
+
* its pointer replaces the value. Attributes outside `only` keep what they
|
|
330
|
+
* hold (a read-merge update's untouched pointers). Every upload is built
|
|
331
|
+
* before any starts (a value that cannot be uploaded sends nothing to the
|
|
332
|
+
* store), and a failed one fails the write only once the others have settled
|
|
333
|
+
* ({@link forEachSettled}).
|
|
334
|
+
*
|
|
335
|
+
* Fails with `BlobStoreError { reason: "NotProvided" }` when a value must go
|
|
336
|
+
* to the store and none is in context.
|
|
337
|
+
*/
|
|
338
|
+
export const route = (info, item, row, only) => Effect.gen(function* () {
|
|
339
|
+
const { out, store } = yield* decide(info, item, row, only);
|
|
340
|
+
if (out.size === 0)
|
|
341
|
+
return { uploaded: out };
|
|
342
|
+
if (store === undefined) {
|
|
343
|
+
return yield* notProvided(info, [...out][0], row.operation);
|
|
344
|
+
}
|
|
345
|
+
// A measurement of the write (`measureAppend`'s additional items): each
|
|
346
|
+
// value takes its pointer's upper bound, and nothing is uploaded.
|
|
347
|
+
if (yield* Measuring) {
|
|
348
|
+
const placed = placeholders(info, item, row, out, store);
|
|
349
|
+
if (placed instanceof ValidationError)
|
|
350
|
+
return yield* placed;
|
|
351
|
+
return { uploaded: out };
|
|
352
|
+
}
|
|
353
|
+
const registry = yield* currentRegistry;
|
|
354
|
+
const uploads = [];
|
|
355
|
+
for (const attribute of out) {
|
|
356
|
+
const value = ownValue(item, attribute);
|
|
357
|
+
const upload = Object.hasOwn(info.binary, attribute)
|
|
358
|
+
? binaryUpload(info.binary[attribute], value)
|
|
359
|
+
: overflowUpload(info, attribute, value, registry, row.operation);
|
|
360
|
+
if (upload instanceof ValidationError)
|
|
361
|
+
return yield* upload;
|
|
362
|
+
uploads.push([attribute, upload]);
|
|
363
|
+
}
|
|
364
|
+
// Single-use streams (design §3.8): refused before any upload starts.
|
|
365
|
+
const ledger = yield* BlobLedger.current;
|
|
366
|
+
const streams = new Set();
|
|
367
|
+
for (const [attribute, upload] of uploads) {
|
|
368
|
+
if (upload.content.kind !== "stream")
|
|
369
|
+
continue;
|
|
370
|
+
const { identity } = upload.content;
|
|
371
|
+
const why = streams.has(identity)
|
|
372
|
+
? "it is also the content of another field of this item"
|
|
373
|
+
: BlobLedger.streamRefusal(ledger, identity, store, keyOf(info, store, row, attribute));
|
|
374
|
+
if (why !== undefined) {
|
|
375
|
+
return yield* BlobLedger.singleUseError(info.entityType, row.operation, attribute, why);
|
|
376
|
+
}
|
|
377
|
+
streams.add(identity);
|
|
378
|
+
}
|
|
379
|
+
yield* forEachSettled(uploads, ([attribute, upload]) => Effect.map(uploadOne(info, store, row, attribute, upload), (pointer) => {
|
|
380
|
+
setOwn(item, attribute, storedPointer(pointer));
|
|
381
|
+
}));
|
|
382
|
+
return { uploaded: out };
|
|
383
|
+
});
|
|
384
|
+
/**
|
|
385
|
+
* Which attributes of `item` {@link route} sends to the store (design §6.3),
|
|
386
|
+
* and the store in context: the routing decision alone, shared by `route`
|
|
387
|
+
* and {@link measure} so a measurement routes exactly as the write would.
|
|
388
|
+
*/
|
|
389
|
+
const decide = (info, item, row, only) => Effect.gen(function* () {
|
|
390
|
+
// Offloading requires escaped key text (design §11.10, `[EDD-9146]`). A
|
|
391
|
+
// bound member was refused at bind; an unbound write is refused here,
|
|
392
|
+
// before anything is uploaded or sent (a measurement, before anything is
|
|
393
|
+
// measured).
|
|
394
|
+
if (!row.escapeKeys) {
|
|
395
|
+
return yield* new ValidationError({
|
|
396
|
+
entityType: info.entityType,
|
|
397
|
+
operation: row.operation,
|
|
398
|
+
cause: unescapedKeysText(row.operation, `${info.entityType} offloads ${offloadedAttributes(info)
|
|
399
|
+
.map((attribute) => `"${info.fieldOf[attribute] ?? attribute}"`)
|
|
400
|
+
.join(", ")}`),
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
const candidates = offloadedAttributes(info).filter((attribute) => (only === undefined || only.has(attribute)) &&
|
|
404
|
+
uploadable(info, attribute, ownValue(item, attribute)));
|
|
405
|
+
if (candidates.length === 0)
|
|
406
|
+
return { out: new Set(), store: undefined };
|
|
407
|
+
const out = new Set(candidates.filter((attribute) => overThreshold(info, attribute, ownValue(item, attribute))));
|
|
408
|
+
const store = yield* currentStore;
|
|
409
|
+
const budget = info.itemBudget;
|
|
410
|
+
if (budget !== undefined) {
|
|
411
|
+
const marshalled = toAttributeMap(item);
|
|
412
|
+
const measured = new Map();
|
|
413
|
+
const measureOne = (attribute) => {
|
|
414
|
+
const known = measured.get(attribute);
|
|
415
|
+
if (known !== undefined)
|
|
416
|
+
return known;
|
|
417
|
+
const value = ownValue(item, attribute);
|
|
418
|
+
const upload = Object.hasOwn(info.binary, attribute)
|
|
419
|
+
? binaryUpload(info.binary[attribute], value)
|
|
420
|
+
: overflowUpload(info, attribute, value, undefined, row.operation);
|
|
421
|
+
if (!(upload instanceof ValidationError))
|
|
422
|
+
measured.set(attribute, upload);
|
|
423
|
+
return upload;
|
|
424
|
+
};
|
|
425
|
+
const bound = (attribute) => {
|
|
426
|
+
if (store === undefined)
|
|
427
|
+
return Effect.fail(notProvided(info, attribute, row.operation));
|
|
428
|
+
const upload = measureOne(attribute);
|
|
429
|
+
if (upload instanceof ValidationError)
|
|
430
|
+
return Effect.fail(upload);
|
|
431
|
+
return Effect.succeed(pointerBound(store, keyOf(info, store, row, attribute), upload));
|
|
432
|
+
};
|
|
433
|
+
const inline = (attribute) => {
|
|
434
|
+
const value = marshalled[attribute];
|
|
435
|
+
return value === undefined ? 0 : attributeBytes(value);
|
|
436
|
+
};
|
|
437
|
+
let size = itemBytes(marshalled);
|
|
438
|
+
for (const attribute of out)
|
|
439
|
+
size += (yield* bound(attribute)) - inline(attribute);
|
|
440
|
+
const rest = candidates
|
|
441
|
+
.filter((attribute) => !out.has(attribute))
|
|
442
|
+
.sort((a, b) => inline(b) - inline(a) || (a < b ? -1 : a > b ? 1 : 0));
|
|
443
|
+
for (const attribute of rest) {
|
|
444
|
+
if (size <= budget)
|
|
445
|
+
break;
|
|
446
|
+
out.add(attribute);
|
|
447
|
+
size += (yield* bound(attribute)) - inline(attribute);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return { out, store };
|
|
451
|
+
});
|
|
452
|
+
/**
|
|
453
|
+
* Measure, in place, what {@link route} would do to `item` without uploading
|
|
454
|
+
* anything (design §6.10, `measureAppend` / `measureSnapshot`): each attribute
|
|
455
|
+
* the write would offload is replaced by a placeholder of its pointer's upper
|
|
456
|
+
* bound (§6.3) — so the item measures at most what the write would send — and
|
|
457
|
+
* reported with the bytes its object would hold. Fails as `route` does when a
|
|
458
|
+
* value must go to the store and none is in context (the bound needs its
|
|
459
|
+
* limits).
|
|
460
|
+
*/
|
|
461
|
+
export const measure = (info, item, row) => Effect.gen(function* () {
|
|
462
|
+
const { out, store } = yield* decide(info, item, row, undefined);
|
|
463
|
+
if (out.size === 0)
|
|
464
|
+
return [];
|
|
465
|
+
if (store === undefined)
|
|
466
|
+
return yield* notProvided(info, [...out][0], row.operation);
|
|
467
|
+
const placed = placeholders(info, item, row, out, store);
|
|
468
|
+
if (placed instanceof ValidationError)
|
|
469
|
+
return yield* placed;
|
|
470
|
+
return placed;
|
|
471
|
+
});
|
|
472
|
+
/**
|
|
473
|
+
* Replace each of `out` in `item` by a placeholder of its pointer's upper
|
|
474
|
+
* bound (§6.3), uploading nothing: what {@link measure} and a measuring
|
|
475
|
+
* {@link route} ({@link Measuring}) leave for the item to be measured with.
|
|
476
|
+
*/
|
|
477
|
+
const placeholders = (info, item, row, out, store) => {
|
|
478
|
+
const measured = [];
|
|
479
|
+
for (const attribute of out) {
|
|
480
|
+
const value = ownValue(item, attribute);
|
|
481
|
+
const upload = Object.hasOwn(info.binary, attribute)
|
|
482
|
+
? binaryUpload(info.binary[attribute], value)
|
|
483
|
+
: overflowUpload(info, attribute, value, undefined, row.operation);
|
|
484
|
+
if (upload instanceof ValidationError)
|
|
485
|
+
return upload;
|
|
486
|
+
const bound = pointerBound(store, keyOf(info, store, row, attribute), upload);
|
|
487
|
+
// `B` bytes for an overflow pointer, `~edbp:` text for a binary one.
|
|
488
|
+
setOwn(item, attribute, upload.kind === "json" ? new Uint8Array(bound) : "~".repeat(bound));
|
|
489
|
+
measured.push({ attribute, objectBytes: upload.objectSize });
|
|
490
|
+
}
|
|
491
|
+
return measured;
|
|
492
|
+
};
|
|
493
|
+
/**
|
|
494
|
+
* @internal Set while a write is only being measured (`measureAppend` builds
|
|
495
|
+
* the transaction `append` would send, its `additionalItems` included):
|
|
496
|
+
* {@link route} then gives each value it would upload a placeholder of its
|
|
497
|
+
* pointer's upper bound, so the item measures at most what the write would
|
|
498
|
+
* send, and uploads nothing.
|
|
499
|
+
*/
|
|
500
|
+
export const Measuring = Context.Reference("effect-dynamodb/internal/OffloadWrite/Measuring", { defaultValue: () => false });
|
|
501
|
+
/**
|
|
502
|
+
* Upload, in place, the offloaded values an `UpdateItem` `SET`s (thresholds
|
|
503
|
+
* only — the whole item is not in hand, §6.3): each binary value and each
|
|
504
|
+
* overflow value at or above its threshold becomes, in `values`, the pointer
|
|
505
|
+
* of a fresh upload; anything else stays as given. One {@link route} of all of
|
|
506
|
+
* them, so every upload is built before any starts and a failed one fails the
|
|
507
|
+
* write only once the others have settled.
|
|
508
|
+
*/
|
|
509
|
+
export const setValues = (info, sets, values, row) => {
|
|
510
|
+
const item = {};
|
|
511
|
+
const valKeys = new Map();
|
|
512
|
+
for (const { valKey, attr } of sets) {
|
|
513
|
+
const stored = values[valKey];
|
|
514
|
+
if (stored === undefined || !isOffloaded(info, attr))
|
|
515
|
+
continue;
|
|
516
|
+
setOwn(item, attr, stored.S ?? stored.B);
|
|
517
|
+
valKeys.set(attr, valKey);
|
|
518
|
+
}
|
|
519
|
+
if (valKeys.size === 0)
|
|
520
|
+
return Effect.void;
|
|
521
|
+
return Effect.map(route({ ...info, itemBudget: undefined }, item, row, new Set(valKeys.keys())), ({ uploaded }) => {
|
|
522
|
+
for (const attr of uploaded)
|
|
523
|
+
values[valKeys.get(attr)] = toAttributeValue(ownValue(item, attr));
|
|
524
|
+
});
|
|
525
|
+
};
|
|
526
|
+
// ---------------------------------------------------------------------------
|
|
527
|
+
// Stored pointers
|
|
528
|
+
// ---------------------------------------------------------------------------
|
|
529
|
+
/** The pointer an offloaded attribute's stored value is, if it is a well-formed one. */
|
|
530
|
+
export const pointerOf = (info, attribute, value) => {
|
|
531
|
+
if (Object.hasOwn(info.overflow, attribute)) {
|
|
532
|
+
if (!(value instanceof Uint8Array) || value.length === 0 || !isPointerHeader(value[0])) {
|
|
533
|
+
return undefined;
|
|
534
|
+
}
|
|
535
|
+
const pointer = decodePointer(value);
|
|
536
|
+
return typeof pointer === "string" || pointer.kind !== "json" ? undefined : pointer;
|
|
537
|
+
}
|
|
538
|
+
if (Object.hasOwn(info.binary, attribute)) {
|
|
539
|
+
if (typeof value !== "string" || !isPointerText(value))
|
|
540
|
+
return undefined;
|
|
541
|
+
const pointer = decodePointerText(value);
|
|
542
|
+
return pointer === undefined || typeof pointer === "string" || pointer.kind !== "bytes"
|
|
543
|
+
? undefined
|
|
544
|
+
: pointer;
|
|
545
|
+
}
|
|
546
|
+
return undefined;
|
|
547
|
+
};
|
|
548
|
+
/** No reuse, no pins. */
|
|
549
|
+
export const NO_PINS = [];
|
|
550
|
+
/** Sort an object's keys, recursively — key order does not change a JSON value. */
|
|
551
|
+
const sortKeys = (value) => {
|
|
552
|
+
if (Array.isArray(value))
|
|
553
|
+
return value.map((element) => sortKeys(element));
|
|
554
|
+
if (value === null || typeof value !== "object")
|
|
555
|
+
return value;
|
|
556
|
+
// A null-prototype copy: on a plain object, assigning an own `__proto__`
|
|
557
|
+
// key (which `JSON.parse` creates) would set the prototype instead, and the
|
|
558
|
+
// key and its value would drop out of the text.
|
|
559
|
+
const out = Object.create(null);
|
|
560
|
+
for (const key of Object.keys(value).sort()) {
|
|
561
|
+
out[key] = sortKeys(value[key]);
|
|
562
|
+
}
|
|
563
|
+
return out;
|
|
564
|
+
};
|
|
565
|
+
/** A JSON value's text with every object's keys sorted: equal iff the values are. */
|
|
566
|
+
export const canonicalJson = (value) => JSON.stringify(sortKeys(value)) ?? "null";
|
|
567
|
+
/**
|
|
568
|
+
* The pointers a stored-name row holds on the member's offloaded attributes,
|
|
569
|
+
* before it is loaded (the load replaces them with their values).
|
|
570
|
+
*/
|
|
571
|
+
export const pointersIn = (info, row) => offloadedAttributes(info).flatMap((attribute) => {
|
|
572
|
+
const stored = row[attribute];
|
|
573
|
+
const pointer = pointerOf(info, attribute, stored);
|
|
574
|
+
return pointer === undefined
|
|
575
|
+
? []
|
|
576
|
+
: [{ attribute, pointer, stored: stored }];
|
|
577
|
+
});
|
|
578
|
+
/**
|
|
579
|
+
* The {@link ReadPointers} of a row whose `pointers` ({@link pointersIn}) were
|
|
580
|
+
* just loaded eagerly: each attribute holds its fetched value — the JSON wire
|
|
581
|
+
* form (overflow) or the bytes (binary) — which is snapshotted.
|
|
582
|
+
*/
|
|
583
|
+
export const snapshotPointers = (info, pointers, loaded) => {
|
|
584
|
+
const out = new Map();
|
|
585
|
+
for (const { attribute, pointer, stored } of pointers) {
|
|
586
|
+
const value = loaded[attribute];
|
|
587
|
+
if (Object.hasOwn(info.binary, attribute)) {
|
|
588
|
+
if (value instanceof Uint8Array) {
|
|
589
|
+
out.set(attribute, { pointer, stored, content: new Uint8Array(value) });
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
else if (value !== undefined) {
|
|
593
|
+
out.set(attribute, { pointer, stored, content: canonicalJson(value) });
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
return out;
|
|
597
|
+
};
|
|
598
|
+
/**
|
|
599
|
+
* Keep, in place, the read pointer of every offloaded attribute of a packed,
|
|
600
|
+
* stored-name item whose new value has the content that pointer named when
|
|
601
|
+
* the row was read (design §6.11) — content, not checksum: a CRC is no proof
|
|
602
|
+
* of equality. The attribute then holds the pointer, which {@link route}
|
|
603
|
+
* leaves as is, and the returned {@link Pin}s must condition the write on the
|
|
604
|
+
* row still holding each one ({@link withPins}), so a concurrent write of the
|
|
605
|
+
* row fails it. Each reused value goes into the ledger's read cache, so the
|
|
606
|
+
* write's result decodes it without a fetch. A value that differs, or an
|
|
607
|
+
* attribute the item no longer holds, is left to {@link route}.
|
|
608
|
+
*/
|
|
609
|
+
export const reuse = (info, item, read, operation) => read.size === 0
|
|
610
|
+
? Effect.succeed(NO_PINS)
|
|
611
|
+
: Effect.gen(function* () {
|
|
612
|
+
const registry = yield* currentRegistry;
|
|
613
|
+
const ledger = yield* BlobLedger.current;
|
|
614
|
+
const pins = [];
|
|
615
|
+
for (const [attribute, previous] of read) {
|
|
616
|
+
const value = ownValue(item, attribute);
|
|
617
|
+
if (!uploadable(info, attribute, value))
|
|
618
|
+
continue;
|
|
619
|
+
let content;
|
|
620
|
+
if (Object.hasOwn(info.binary, attribute)) {
|
|
621
|
+
// A stream's or a handle's placeholder holds no content to compare;
|
|
622
|
+
// a source naming another content type is a new object.
|
|
623
|
+
const prepared = OffloadInput.preparedOf(value);
|
|
624
|
+
if ((prepared !== undefined && prepared._tag !== "Bytes") ||
|
|
625
|
+
(prepared?.contentType !== undefined &&
|
|
626
|
+
prepared.contentType !== Option.getOrUndefined(previous.pointer.contentType)) ||
|
|
627
|
+
!(previous.content instanceof Uint8Array) ||
|
|
628
|
+
!sameBytes(value, previous.content)) {
|
|
629
|
+
continue;
|
|
630
|
+
}
|
|
631
|
+
content = { kind: "bytes", bytes: previous.content };
|
|
632
|
+
}
|
|
633
|
+
else {
|
|
634
|
+
const upload = overflowUpload(info, attribute, value, registry, operation);
|
|
635
|
+
if (upload instanceof ValidationError)
|
|
636
|
+
return yield* upload;
|
|
637
|
+
const text = upload.content.text;
|
|
638
|
+
let parsed;
|
|
639
|
+
try {
|
|
640
|
+
parsed = JSON.parse(text);
|
|
641
|
+
}
|
|
642
|
+
catch {
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
if (previous.content !== canonicalJson(parsed))
|
|
646
|
+
continue;
|
|
647
|
+
content = { kind: "json", text };
|
|
648
|
+
}
|
|
649
|
+
setOwn(item, attribute, previous.stored);
|
|
650
|
+
pins.push({ attribute, stored: previous.stored });
|
|
651
|
+
BlobLedger.remember(ledger, previous.pointer.ref, content);
|
|
652
|
+
}
|
|
653
|
+
return pins;
|
|
654
|
+
});
|
|
655
|
+
/**
|
|
656
|
+
* `condition` ANDed with `#f = :P` for every pin (design §6.11): the write
|
|
657
|
+
* succeeds only while the row still holds each reused pointer. `condition`
|
|
658
|
+
* itself when there are none.
|
|
659
|
+
*/
|
|
660
|
+
export const withPins = (condition, pins) => {
|
|
661
|
+
if (pins.length === 0)
|
|
662
|
+
return condition;
|
|
663
|
+
const names = { ...condition?.names };
|
|
664
|
+
const values = { ...condition?.values };
|
|
665
|
+
const parts = [];
|
|
666
|
+
pins.forEach((pin, i) => {
|
|
667
|
+
names[`#eddPin${i}`] = pin.attribute;
|
|
668
|
+
values[`:eddPin${i}`] = toAttributeValue(pin.stored);
|
|
669
|
+
parts.push(`#eddPin${i} = :eddPin${i}`);
|
|
670
|
+
});
|
|
671
|
+
const pinned = parts.join(" AND ");
|
|
672
|
+
return {
|
|
673
|
+
expression: condition === undefined ? pinned : `(${condition.expression}) AND ${pinned}`,
|
|
674
|
+
names,
|
|
675
|
+
values,
|
|
676
|
+
};
|
|
677
|
+
};
|
|
678
|
+
// ---------------------------------------------------------------------------
|
|
679
|
+
// Immutables
|
|
680
|
+
// ---------------------------------------------------------------------------
|
|
681
|
+
const sameBytes = (a, b) => a.length === b.length && a.every((byte, i) => byte === b[i]);
|
|
682
|
+
/**
|
|
683
|
+
* Whether `content` (a restated value's JSON text, or its bytes) has the
|
|
684
|
+
* pointer's content checksum and size — the fast path of an immutable
|
|
685
|
+
* assertion (design §6.5): a match passes; a mismatch is confirmed by
|
|
686
|
+
* fetching the stored value.
|
|
687
|
+
*/
|
|
688
|
+
export const matchesPointer = (pointer, content) => {
|
|
689
|
+
const bytes = typeof content === "string" ? new TextEncoder().encode(content) : content;
|
|
690
|
+
return matchesChecksum(pointer, { algorithm: "crc64nvme", value: crc64nvme(bytes) }, bytes.length);
|
|
691
|
+
};
|
|
692
|
+
/** Whether `bytes` (a value stored natively) have `checksum` and `size` (a `BlobHandle`'s). */
|
|
693
|
+
export const bytesMatch = (bytes, checksum, size) => bytes.length === size &&
|
|
694
|
+
checksum.algorithm === "crc64nvme" &&
|
|
695
|
+
sameBytes(crc64nvme(bytes), checksum.value);
|
|
696
|
+
/**
|
|
697
|
+
* Whether content of `size` bytes with `checksum` (a `BlobHandle`'s,
|
|
698
|
+
* restating an immutable binary field) has the pointer's content checksum
|
|
699
|
+
* and size.
|
|
700
|
+
*/
|
|
701
|
+
export const matchesChecksum = (pointer, checksum, size) => size === pointer.contentSize &&
|
|
702
|
+
pointer.checksum.algorithm === "crc64nvme" &&
|
|
703
|
+
checksum.algorithm === "crc64nvme" &&
|
|
704
|
+
sameBytes(checksum.value, pointer.checksum.value);
|
|
705
|
+
/** Whether a member's schema (`_schema`, configured by `Table.make`) sets `escapeKeys: true`. */
|
|
706
|
+
const escapeKeysOf = (schema) => typeof schema === "object" && schema !== null
|
|
707
|
+
? schema.escapeKeys === true
|
|
708
|
+
: undefined;
|
|
709
|
+
/** The store requirement of an entity (`entity.offload`), if it offloads anything. */
|
|
710
|
+
export const entityStoreRequirements = (owner, entity) => {
|
|
711
|
+
const member = entity;
|
|
712
|
+
const offload = member?.offload;
|
|
713
|
+
if (offload === undefined)
|
|
714
|
+
return [];
|
|
715
|
+
return [
|
|
716
|
+
{
|
|
717
|
+
owner,
|
|
718
|
+
fields: [...offload.overflow, ...offload.binary],
|
|
719
|
+
escapeKeys: escapeKeysOf(member?._schema),
|
|
720
|
+
},
|
|
721
|
+
];
|
|
722
|
+
};
|
|
723
|
+
/** The store requirements of an aggregate's edge entities (`_offloadByType`). */
|
|
724
|
+
export const aggregateStoreRequirements = (owner, aggregate) => {
|
|
725
|
+
const member = aggregate;
|
|
726
|
+
const byType = member?._offloadByType;
|
|
727
|
+
if (byType === undefined)
|
|
728
|
+
return [];
|
|
729
|
+
return [...byType].map(([edgeType, fields]) => ({
|
|
730
|
+
owner: `${owner} (edge "${edgeType}")`,
|
|
731
|
+
fields,
|
|
732
|
+
escapeKeys: escapeKeysOf(member?._keySchema),
|
|
733
|
+
}));
|
|
734
|
+
};
|
|
735
|
+
/**
|
|
736
|
+
* The `[EDD-9146]` configuration error for members that offload on a schema
|
|
737
|
+
* without `escapeKeys: true` (design §11.10), or `undefined` when none does.
|
|
738
|
+
* Checked before the store ({@link missingStoreError}): a store doesn't make
|
|
739
|
+
* the key text right. `where` names the binding.
|
|
740
|
+
*/
|
|
741
|
+
export const unescapedKeysError = (where, requirements) => {
|
|
742
|
+
const unescaped = requirements.filter((r) => r.escapeKeys === false);
|
|
743
|
+
if (unescaped.length === 0)
|
|
744
|
+
return undefined;
|
|
745
|
+
return new Error(unescapedKeysText(where, unescaped
|
|
746
|
+
.map((r) => `${r.owner} offloads ${r.fields.map((field) => `"${field}"`).join(", ")}`)
|
|
747
|
+
.join("; ")));
|
|
748
|
+
};
|
|
749
|
+
/**
|
|
750
|
+
* The `[EDD-9115]` configuration error for members bound with no store
|
|
751
|
+
* resolvable (`store` undefined), or `undefined` when there is one or nothing
|
|
752
|
+
* offloads. `where` names the binding — `DynamoClient.make`, `Entity.bind`, ….
|
|
753
|
+
*/
|
|
754
|
+
export const missingStoreError = (where, requirements, store) => {
|
|
755
|
+
if (store !== undefined || requirements.length === 0)
|
|
756
|
+
return undefined;
|
|
757
|
+
const what = requirements
|
|
758
|
+
.map((r) => `${r.owner} offloads ${r.fields.map((field) => `"${field}"`).join(", ")}`)
|
|
759
|
+
.join("; ");
|
|
760
|
+
return new Error(`[EDD-9115] ${where}: ${what}, but no BlobStore is resolvable. Every write of an ` +
|
|
761
|
+
"offloaded field uploads it to the blob store and every read resolves its pointer there, " +
|
|
762
|
+
"so provide one where the client is bound: DynamoClient.make({ blobStore }), or " +
|
|
763
|
+
"BlobStore.layer(...) / the S3 store's layer merged into the application layer (see the " +
|
|
764
|
+
"offloaded storage guide).");
|
|
765
|
+
};
|
|
766
|
+
//# sourceMappingURL=OffloadWrite.js.map
|