@effect-dynamodb/schema 1.44.0 → 1.45.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/DynamoSchema.d.ts +9 -0
- package/dist/DynamoSchema.d.ts.map +1 -1
- package/dist/DynamoSchema.js.map +1 -1
- package/dist/Entity.d.ts +62 -5
- package/dist/Entity.d.ts.map +1 -1
- package/dist/Entity.js +44 -6
- package/dist/Entity.js.map +1 -1
- package/dist/KeyComposer.d.ts +190 -11
- package/dist/KeyComposer.d.ts.map +1 -1
- package/dist/KeyComposer.js +489 -53
- package/dist/KeyComposer.js.map +1 -1
- package/dist/ShardLayouts.d.ts +197 -0
- package/dist/ShardLayouts.d.ts.map +1 -0
- package/dist/ShardLayouts.js +191 -0
- package/dist/ShardLayouts.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/SharedKeyAttribute.d.ts.map +1 -1
- package/dist/internal/SharedKeyAttribute.js +24 -5
- package/dist/internal/SharedKeyAttribute.js.map +1 -1
- package/package.json +1 -1
package/dist/KeyComposer.js
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* template strings ("USER#${userId}"). The DynamoSchema module handles
|
|
6
6
|
* the key format; KeyComposer extracts attribute values and delegates.
|
|
7
7
|
*/
|
|
8
|
-
import { DateTime } from "effect";
|
|
9
|
-
import { applyCasing, composeClusteredSortKey, composeCollectionKey, composeIsolatedSortKey, composeKey, KEY_DELIMITER, } from "./DynamoSchema.js";
|
|
8
|
+
import { Context, DateTime, Fiber } from "effect";
|
|
9
|
+
import { applyCasing, composeClusteredSortKey, composeCollectionKey, composeIsolatedSortKey, composeKey, KEY_DELIMITER, make as makeDynamoSchema, } from "./DynamoSchema.js";
|
|
10
10
|
import { encodeBigInt, encodeNumber, isStoredNaN, KeyValueError } from "./internal/NumericKey.js";
|
|
11
11
|
import { judgeObjectKeyText } from "./internal/ObjectKeyText.js";
|
|
12
12
|
// A record composed from may be a caller's key or query composites: a composite's value is read as
|
|
@@ -16,8 +16,11 @@ import { MAX_SHARD_COUNT, ShardSourceError, shardMarker, shardOf } from "./inter
|
|
|
16
16
|
import { zonesAsStored } from "./internal/StoredForm.js";
|
|
17
17
|
import { concatBytes, interleave, prefixSegment, toBytes, } from "./internal/ZOrderCurve.js";
|
|
18
18
|
import { declareDimension } from "./internal/ZOrderDimension.js";
|
|
19
|
+
import { ShardLayouts } from "./ShardLayouts.js";
|
|
19
20
|
/** Is this sort key half a Z-order one? */
|
|
20
21
|
export const isZOrderHalf = (part) => "zorderHalf" in part && part.zorderHalf !== undefined;
|
|
22
|
+
/** The layouts of `shards` (#287): as declared, or a static one's `count` and `previous`. */
|
|
23
|
+
export const shardLayoutsOf = (shards) => shards.layouts ?? { default: { count: shards.count, previous: shards.previous }, values: [] };
|
|
21
24
|
/** The casing an index's keys are composed with: its own, else the schema's. */
|
|
22
25
|
export const effectiveCasing = (schema, index) => index.casing ?? schema.casing;
|
|
23
26
|
/**
|
|
@@ -37,39 +40,174 @@ export const assertCollectionCasingAgreement = (collectionName, members) => {
|
|
|
37
40
|
/**
|
|
38
41
|
* Fail when a collection's members would read different partitions (#183). They
|
|
39
42
|
* share one physical index and one logical key, so they must agree on how many
|
|
40
|
-
* partitions it is spread over and on the layout being migrated from
|
|
41
|
-
*
|
|
43
|
+
* partitions it is spread over and on the layout being migrated from: with layouts
|
|
44
|
+
* per value (#287), their tables, compared by content: equal defaults, and the same
|
|
45
|
+
* layout for every key text an entry composes, under the member's `schema` (the
|
|
46
|
+
* default casing without one). A static member is a table with only a default; a
|
|
47
|
+
* member without shards agrees only with members without them. `by` may differ:
|
|
48
|
+
* each member hashes its own source, into the same partitions. A member whose table
|
|
49
|
+
* is supplied at run time and not carried by its `schema` is compared when it is.
|
|
42
50
|
*/
|
|
43
51
|
export const assertCollectionShardsAgreement = (collectionName, members) => {
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
52
|
+
const fallback = makeDynamoSchema({ name: "collection", version: 1 });
|
|
53
|
+
const layoutText = ({ count, previous }) => `count ${count}${previous === undefined ? "" : `, previous ${previous}`}`;
|
|
54
|
+
const layoutOf = ({ count, previous }) => [count, previous ?? null];
|
|
55
|
+
/**
|
|
56
|
+
* A member's layouts: `content` is what two members must agree on, structured, so no
|
|
57
|
+
* key text can spell another table's (a value may hold any text when keys aren't
|
|
58
|
+
* escaped); `shown` is the same for the message. `undefined`: not known yet.
|
|
59
|
+
*/
|
|
60
|
+
const contentOf = (member) => {
|
|
61
|
+
const shards = member.index.shards;
|
|
62
|
+
if (shards === undefined)
|
|
63
|
+
return { content: "unsharded", shown: "unsharded" };
|
|
64
|
+
const schema = member.schema ?? fallback;
|
|
65
|
+
if (shardLayoutsOf(shards) === "provided" &&
|
|
66
|
+
schema.shardTables?.get(shards) === undefined &&
|
|
67
|
+
ambientShardTable(shards) === undefined) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
const table = shardTableOf(schema, member.index);
|
|
71
|
+
const entries = [...textIndexOf(schema, member.key, member.index, table)].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
72
|
+
return {
|
|
73
|
+
content: JSON.stringify([
|
|
74
|
+
layoutOf(table.default),
|
|
75
|
+
entries.map(([text, layout]) => [text, ...layoutOf(layout)]),
|
|
76
|
+
]),
|
|
77
|
+
shown: entries.length === 0
|
|
78
|
+
? layoutText(table.default)
|
|
79
|
+
: `default ${layoutText(table.default)}; ` +
|
|
80
|
+
entries.map(([text, layout]) => `${text} → ${layoutText(layout)}`).join("; "),
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
const contents = members.map((member) => ({ key: member.key, of: contentOf(member) }));
|
|
84
|
+
const known = contents.flatMap((c) => (c.of === undefined ? [] : [c.of.content]));
|
|
85
|
+
const shardedness = new Set(contents.map((c) => c.of?.content === "unsharded"));
|
|
86
|
+
if (new Set(known).size > 1 || shardedness.size > 1) {
|
|
87
|
+
const shown = contents.map((c) => `${c.key}: ${c.of?.shown ?? "a table supplied at run time"}`);
|
|
50
88
|
throw new Error(`[EDD-9103] Collection "${collectionName}" members spread its index differently ` +
|
|
51
89
|
`(${shown.join("; ")}). They share one logical partition, so a query has to read one ` +
|
|
52
|
-
`set of partitions: give the collection's index the same \`shards\` count and
|
|
53
|
-
|
|
90
|
+
`set of partitions: give the collection's index the same \`shards\` layouts (count and ` +
|
|
91
|
+
`\`previous\`, or the same table) on every member.`);
|
|
54
92
|
}
|
|
55
93
|
};
|
|
56
94
|
const shardsMalformed = (index, why) => new Error(`[EDD-9099] Index "${index}" declares malformed shards: ${why}`);
|
|
95
|
+
const isPlainObject = (u) => typeof u === "object" && u !== null && !Array.isArray(u);
|
|
96
|
+
const refuseUnknownKeys = (index, where, value, allowed) => {
|
|
97
|
+
const unknownKeys = Object.keys(value).filter((key) => !allowed.includes(key));
|
|
98
|
+
if (unknownKeys.length > 0) {
|
|
99
|
+
throw shardsMalformed(index, `unknown option ${unknownKeys.map((key) => `"${key}"`).join(", ")} in ${where}: it takes ` +
|
|
100
|
+
`${allowed.join(", ")}.`);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const isShardCount = (u, least) => typeof u === "number" && Number.isInteger(u) && u >= least && u <= MAX_SHARD_COUNT;
|
|
104
|
+
/**
|
|
105
|
+
* Check one layout (#287): a count from 1 to 256, or `{ count, previous? }` with
|
|
106
|
+
* `previous` from 1 to 256 and not `count`. `where` names it in the message.
|
|
107
|
+
*/
|
|
108
|
+
export const normalizeShardLayout = (index, where, declared) => {
|
|
109
|
+
const layout = typeof declared === "number" ? { count: declared } : isPlainObject(declared) ? declared : {};
|
|
110
|
+
if (typeof declared !== "number" && !isPlainObject(declared)) {
|
|
111
|
+
throw shardsMalformed(index, `${where} must be a count, or { count, previous? }.`);
|
|
112
|
+
}
|
|
113
|
+
if (isPlainObject(declared))
|
|
114
|
+
refuseUnknownKeys(index, where, declared, ["count", "previous"]);
|
|
115
|
+
const { count, previous } = layout;
|
|
116
|
+
if (!isShardCount(count, 1)) {
|
|
117
|
+
throw shardsMalformed(index, `${where}: count ${JSON.stringify(count)} must be an integer from 1 to ${MAX_SHARD_COUNT}.`);
|
|
118
|
+
}
|
|
119
|
+
if (previous !== undefined) {
|
|
120
|
+
if (typeof previous !== "number" || !Number.isInteger(previous) || previous < 1) {
|
|
121
|
+
throw shardsMalformed(index, `${where}: previous ${JSON.stringify(previous)} must be a positive integer: 1 for the ` +
|
|
122
|
+
`unsharded key, or the old count.`);
|
|
123
|
+
}
|
|
124
|
+
if (previous === count) {
|
|
125
|
+
throw shardsMalformed(index, `${where}: previous ${previous} equals count: it names the layout being migrated from, ` +
|
|
126
|
+
`which has to differ.`);
|
|
127
|
+
}
|
|
128
|
+
if (previous > MAX_SHARD_COUNT) {
|
|
129
|
+
throw shardsMalformed(index, `${where}: previous ${previous} is above ${MAX_SHARD_COUNT}.`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return { count, previous: previous };
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Check a table's layouts and the shape of its entries (#287). What needs the
|
|
136
|
+
* model (each `match`'s names and values) is `Entity.make`'s, through
|
|
137
|
+
* {@link bindShardTable}. Used for a declared table and for a supplied one.
|
|
138
|
+
*/
|
|
139
|
+
export const normalizeShardTable = (index, declared) => {
|
|
140
|
+
if (!isPlainObject(declared)) {
|
|
141
|
+
throw shardsMalformed(index, `layouts must be "provided", or a table { default, values? }; got ${JSON.stringify(declared)}.`);
|
|
142
|
+
}
|
|
143
|
+
refuseUnknownKeys(index, "layouts", declared, ["default", "values"]);
|
|
144
|
+
if (declared.default === undefined) {
|
|
145
|
+
throw shardsMalformed(index, `layouts has no default: give the layout of every value the table doesn't list (1 for ` +
|
|
146
|
+
`the unsharded key).`);
|
|
147
|
+
}
|
|
148
|
+
const values = declared.values ?? [];
|
|
149
|
+
if (!Array.isArray(values)) {
|
|
150
|
+
throw shardsMalformed(index, "layouts.values must be an array of { match, layout }.");
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
default: normalizeShardLayout(index, "layouts.default", declared.default),
|
|
154
|
+
values: values.map((entry, i) => {
|
|
155
|
+
const where = `layouts.values[${i}]`;
|
|
156
|
+
if (!isPlainObject(entry)) {
|
|
157
|
+
throw shardsMalformed(index, `${where} must be { match, layout }.`);
|
|
158
|
+
}
|
|
159
|
+
refuseUnknownKeys(index, where, entry, ["match", "layout"]);
|
|
160
|
+
if (!isPlainObject(entry.match)) {
|
|
161
|
+
throw shardsMalformed(index, `${where}.match must name every partition key composite of the index, with its value.`);
|
|
162
|
+
}
|
|
163
|
+
if (entry.layout === undefined)
|
|
164
|
+
throw shardsMalformed(index, `${where} has no layout.`);
|
|
165
|
+
return {
|
|
166
|
+
match: { ...entry.match },
|
|
167
|
+
keyRecord: undefined,
|
|
168
|
+
layout: normalizeShardLayout(index, `${where}.layout`, entry.layout),
|
|
169
|
+
};
|
|
170
|
+
}),
|
|
171
|
+
};
|
|
172
|
+
};
|
|
57
173
|
/** Normalise and check a declared `shards` (the rest is `Entity.make`'s, which has the model). */
|
|
58
174
|
const normalizeShards = (index, declared) => {
|
|
59
|
-
const config = typeof declared === "number"
|
|
60
|
-
|
|
61
|
-
|
|
175
|
+
const config = typeof declared === "number"
|
|
176
|
+
? { count: declared }
|
|
177
|
+
: isPlainObject(declared)
|
|
178
|
+
? declared
|
|
179
|
+
: undefined;
|
|
180
|
+
if (config === undefined) {
|
|
181
|
+
throw shardsMalformed(index, "pass a count, { count, by?, previous? }, or { by?, layouts } for a layout per value.");
|
|
62
182
|
}
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
183
|
+
const by = config.by;
|
|
184
|
+
const normalizedBy = by === undefined ? undefined : [...by];
|
|
185
|
+
if ("layouts" in config) {
|
|
186
|
+
const stray = ["count", "previous"].filter((key) => key in config);
|
|
187
|
+
if (stray.length > 0) {
|
|
188
|
+
throw shardsMalformed(index, `${stray.map((key) => `"${key}"`).join(" and ")} can't stand beside layouts: each ` +
|
|
189
|
+
`layout carries its own count and previous.`);
|
|
190
|
+
}
|
|
191
|
+
refuseUnknownKeys(index, "shards", config, ["by", "layouts"]);
|
|
192
|
+
const layouts = config.layouts === "provided" ? "provided" : normalizeShardTable(index, config.layouts);
|
|
193
|
+
return {
|
|
194
|
+
count: layouts === "provided" ? 0 : layouts.default.count,
|
|
195
|
+
previous: layouts === "provided" ? undefined : layouts.default.previous,
|
|
196
|
+
by: normalizedBy,
|
|
197
|
+
source: undefined,
|
|
198
|
+
layouts,
|
|
199
|
+
};
|
|
66
200
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
throw shardsMalformed(index, `count ${JSON.stringify(count)} must be an integer from 2 to ${MAX_SHARD_COUNT}
|
|
201
|
+
refuseUnknownKeys(index, "shards", config, ["count", "by", "previous"]);
|
|
202
|
+
const { count, previous } = config;
|
|
203
|
+
// A bare count of 1 says "sharded" and means nothing; `{ count: 1, previous }`
|
|
204
|
+
// moves an index back to its unsharded key (#287).
|
|
205
|
+
if (!isShardCount(count, previous === undefined ? 2 : 1)) {
|
|
206
|
+
throw shardsMalformed(index, `count ${JSON.stringify(count)} must be an integer from 2 to ${MAX_SHARD_COUNT}` +
|
|
207
|
+
(previous === undefined
|
|
208
|
+
? ""
|
|
209
|
+
: " (or 1 beside previous, to move back to the unsharded key)") +
|
|
210
|
+
".");
|
|
73
211
|
}
|
|
74
212
|
if (previous !== undefined) {
|
|
75
213
|
if (typeof previous !== "number" || !Number.isInteger(previous) || previous < 1) {
|
|
@@ -82,11 +220,13 @@ const normalizeShards = (index, declared) => {
|
|
|
82
220
|
throw shardsMalformed(index, `previous ${previous} is above ${MAX_SHARD_COUNT}.`);
|
|
83
221
|
}
|
|
84
222
|
}
|
|
223
|
+
const layout = { count, previous: previous };
|
|
85
224
|
return {
|
|
86
225
|
count,
|
|
87
|
-
by:
|
|
88
|
-
previous,
|
|
226
|
+
by: normalizedBy,
|
|
227
|
+
previous: layout.previous,
|
|
89
228
|
source: undefined,
|
|
229
|
+
layouts: { default: layout, values: [] },
|
|
90
230
|
};
|
|
91
231
|
};
|
|
92
232
|
/** Normalize a GsiConfig (entity input) to an IndexDefinition (internal format). */
|
|
@@ -376,7 +516,7 @@ export const extractKeyComposites = (schema, index, composite, record, half) =>
|
|
|
376
516
|
*/
|
|
377
517
|
export const composePk = (schema, entityType, index, record) => {
|
|
378
518
|
const composites = extractKeyComposites(schema, index, index.pk.composite, record, "pk");
|
|
379
|
-
return composePkText(schema, entityType, index, composites, index.pk.composite, composedShardMarker(entityType, index, record));
|
|
519
|
+
return composePkText(schema, entityType, index, composites, index.pk.composite, composedShardMarker(schema, entityType, index, record, () => composePkText(schema, entityType, index, composites, index.pk.composite, undefined)));
|
|
380
520
|
};
|
|
381
521
|
/**
|
|
382
522
|
* The text of a partition key: the entity type or collection name, the marker
|
|
@@ -397,11 +537,17 @@ const composePkText = (schema, entityType, index, composites, names, marker) =>
|
|
|
397
537
|
: composeCollectionKey(schema, spelled, composites, options);
|
|
398
538
|
};
|
|
399
539
|
/**
|
|
400
|
-
* The marker of the shard `record` hashes to; `undefined` on an unsharded index
|
|
401
|
-
*
|
|
402
|
-
*
|
|
540
|
+
* The marker of the shard `record` hashes to; `undefined` on an unsharded index,
|
|
541
|
+
* and for a value whose layout has a count of 1 (#287): that value's key is the
|
|
542
|
+
* unsharded text, never `!1.0`. Throws `[EDD-9100]` when the source is missing,
|
|
543
|
+
* whatever the value's count: a sharded index never composes an unsharded
|
|
544
|
+
* partition key for want of it, so a write can't go wrong by forgetting it.
|
|
545
|
+
*
|
|
546
|
+
* `unsharded` composes the value's unsharded partition key text, which its
|
|
547
|
+
* layout is looked up by; a truncated key has none (`undefined`), and takes the
|
|
548
|
+
* table's default: no entry composes truncated text.
|
|
403
549
|
*/
|
|
404
|
-
const composedShardMarker = (entityType, index, record) => {
|
|
550
|
+
const composedShardMarker = (schema, entityType, index, record, unsharded) => {
|
|
405
551
|
const shards = index.shards;
|
|
406
552
|
if (shards === undefined)
|
|
407
553
|
return undefined;
|
|
@@ -415,7 +561,243 @@ const composedShardMarker = (entityType, index, record) => {
|
|
|
415
561
|
`Include ${absent.length === 1 ? "it" : "them"} in the write.`,
|
|
416
562
|
});
|
|
417
563
|
}
|
|
418
|
-
|
|
564
|
+
const table = shardTableOf(schema, index);
|
|
565
|
+
const { count } = unsharded === undefined
|
|
566
|
+
? table.default
|
|
567
|
+
: shardLayoutFor(schema, entityType, index, table, unsharded);
|
|
568
|
+
if (count === 1)
|
|
569
|
+
return undefined;
|
|
570
|
+
return shardMarker(count, shardOf(source.map((attr) => serializeValue(given(record, attr))), count));
|
|
571
|
+
};
|
|
572
|
+
// ---------------------------------------------------------------------------
|
|
573
|
+
// Shard tables (#287, docs/designs/shard-layouts.md)
|
|
574
|
+
// ---------------------------------------------------------------------------
|
|
575
|
+
/**
|
|
576
|
+
* The table of a sharded index (#287): the one it declares, or, for an index
|
|
577
|
+
* declared `layouts: "provided"`, the one the schema an operation composes under
|
|
578
|
+
* carries ({@link withShardTables}), else the one the `ShardLayouts` service of the
|
|
579
|
+
* context it runs in supplies. The one way every composer and reader
|
|
580
|
+
* obtains a table, so a path that has none can't fall back to a default: it
|
|
581
|
+
* throws `[EDD-9181]`.
|
|
582
|
+
*/
|
|
583
|
+
export const shardTableOf = (schema, index) => {
|
|
584
|
+
const shards = index.shards;
|
|
585
|
+
if (shards === undefined) {
|
|
586
|
+
throw new Error(`KeyComposer.shardTableOf: index "${index.index}" is not sharded.`);
|
|
587
|
+
}
|
|
588
|
+
const layouts = shardLayoutsOf(shards);
|
|
589
|
+
if (layouts !== "provided")
|
|
590
|
+
return layouts;
|
|
591
|
+
const supplied = schema.shardTables?.get(shards) ?? ambientShardTable(shards);
|
|
592
|
+
if (supplied === undefined) {
|
|
593
|
+
throw new Error(`[EDD-9181] Index "${index.index}" declares shards: { layouts: "provided" }, but no ` +
|
|
594
|
+
`ShardLayouts layer supplies its table. Add ShardLayouts.layer(...) to the application ` +
|
|
595
|
+
`layer.`);
|
|
596
|
+
}
|
|
597
|
+
return supplied;
|
|
598
|
+
};
|
|
599
|
+
/**
|
|
600
|
+
* The table a `ShardLayouts` service supplies for a provided index (#287): the
|
|
601
|
+
* service of the context the composition runs in. A bound operation runs in its
|
|
602
|
+
* binding's context, so it composes with the table `DynamoClient.make` read; an
|
|
603
|
+
* unbound one with the caller's. `undefined` outside a fiber, or without the
|
|
604
|
+
* service or the entry.
|
|
605
|
+
*/
|
|
606
|
+
const ambientShardTable = (shards) => {
|
|
607
|
+
const fiber = Fiber.getCurrent();
|
|
608
|
+
return fiber === undefined
|
|
609
|
+
? undefined
|
|
610
|
+
: Context.getOrUndefined(fiber.context, ShardLayouts)?.tables.get(shards);
|
|
611
|
+
};
|
|
612
|
+
/**
|
|
613
|
+
* `schema` carrying the tables supplied for `"provided"` indexes (#287), keyed by
|
|
614
|
+
* each index's normalised `shards`: what an operation composes under, so
|
|
615
|
+
* {@link shardTableOf} finds them. Every other composition is unchanged; index
|
|
616
|
+
* definitions never change identity.
|
|
617
|
+
*/
|
|
618
|
+
export const withShardTables = (schema, tables) => tables.size === 0
|
|
619
|
+
? schema
|
|
620
|
+
: {
|
|
621
|
+
...schema,
|
|
622
|
+
shardTables: new Map([...(schema.shardTables ?? new Map()), ...tables]),
|
|
623
|
+
};
|
|
624
|
+
/** The `[EDD-9181]` message for provided indexes with no table (#287). */
|
|
625
|
+
export const missingShardTablesMessage = (where, missing) => `[EDD-9181] ${where}: ${missing.map(({ entity, index }) => `${entity}.${index}`).join(", ")} ` +
|
|
626
|
+
`declare${missing.length === 1 ? "s" : ""} shards: { layouts: "provided" }, but no ShardLayouts ` +
|
|
627
|
+
`layer supplies ${missing.length === 1 ? "its table" : "their tables"}. Add ` +
|
|
628
|
+
`ShardLayouts.layer([ShardLayouts.of(...)]) to the application layer.`;
|
|
629
|
+
/**
|
|
630
|
+
* The provided indexes of an entity (#287) that have no table where an operation
|
|
631
|
+
* runs: none under `schema`'s tables, nor from the `ShardLayouts` service of the
|
|
632
|
+
* current fiber (or of `registry`, when given).
|
|
633
|
+
*/
|
|
634
|
+
export const missingShardTables = (schema, indexes, registry) => Object.entries(indexes).flatMap(([name, def]) => {
|
|
635
|
+
const shards = def.shards;
|
|
636
|
+
if (shards === undefined || shardLayoutsOf(shards) !== "provided")
|
|
637
|
+
return [];
|
|
638
|
+
const found = schema?.shardTables?.get(shards) ??
|
|
639
|
+
(registry === undefined ? ambientShardTable(shards) : registry.tables.get(shards));
|
|
640
|
+
return found === undefined ? [name] : [];
|
|
641
|
+
});
|
|
642
|
+
/**
|
|
643
|
+
* Throw `[EDD-9181]` when an entity's provided index has no table where the
|
|
644
|
+
* operation runs (#287): checked when an operation without a bound client starts,
|
|
645
|
+
* so nothing is sent, whether or not this write would have composed the index.
|
|
646
|
+
*/
|
|
647
|
+
export const requireShardTables = (where, schema, entityType, indexes) => {
|
|
648
|
+
const missing = missingShardTables(schema, indexes);
|
|
649
|
+
if (missing.length > 0) {
|
|
650
|
+
throw new Error(missingShardTablesMessage(where, missing.map((index) => ({ entity: entityType, index }))));
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
/** Does any index of these have its table supplied at run time (#287)? */
|
|
654
|
+
export const hasProvidedShards = (indexes) => Object.values(indexes).some((def) => def.shards !== undefined && shardLayoutsOf(def.shards) === "provided");
|
|
655
|
+
/** Does `index` have a sharded partition key whose layout can vary by value? */
|
|
656
|
+
export const hasShardTable = (index) => index.shards !== undefined &&
|
|
657
|
+
(shardLayoutsOf(index.shards) === "provided" ||
|
|
658
|
+
shardLayoutsOf(index.shards).values.length > 0);
|
|
659
|
+
/** Memoised text indexes: per table, per key text settings (design §3.3). */
|
|
660
|
+
const textIndexes = new WeakMap();
|
|
661
|
+
/**
|
|
662
|
+
* What a key's text depends on besides its values: the schema's key text settings
|
|
663
|
+
* (its own casing spells its name, whatever the index's), the index's casing and the
|
|
664
|
+
* label.
|
|
665
|
+
*/
|
|
666
|
+
const keyTextSettings = (schema, entityType, index) => JSON.stringify([
|
|
667
|
+
schema.name,
|
|
668
|
+
schema.version,
|
|
669
|
+
schema.casing,
|
|
670
|
+
effectiveCasing(schema, index),
|
|
671
|
+
schema.escapeKeys === true,
|
|
672
|
+
schema.numericKeyText === true,
|
|
673
|
+
schema.text136 === true,
|
|
674
|
+
index.collection === undefined ? entityType : null,
|
|
675
|
+
]);
|
|
676
|
+
/** What a table entry is called in a message. */
|
|
677
|
+
const describeEntry = (entry, i) => `layouts.values[${i}] (match ${JSON.stringify(entry.match, (_, v) => typeof v === "bigint" ? `${v}n` : v)})`;
|
|
678
|
+
/**
|
|
679
|
+
* The text index of `table` under these key text settings: each entry's unsharded
|
|
680
|
+
* partition key text to its layout. Built once per table and settings; two entries
|
|
681
|
+
* composing one key are `[EDD-9099]`.
|
|
682
|
+
*/
|
|
683
|
+
const textIndexOf = (schema, entityType, index, table) => {
|
|
684
|
+
let bySettings = textIndexes.get(table);
|
|
685
|
+
if (bySettings === undefined) {
|
|
686
|
+
bySettings = new Map();
|
|
687
|
+
textIndexes.set(table, bySettings);
|
|
688
|
+
}
|
|
689
|
+
const settings = keyTextSettings(schema, entityType, index);
|
|
690
|
+
const known = bySettings.get(settings);
|
|
691
|
+
if (known !== undefined)
|
|
692
|
+
return known;
|
|
693
|
+
// The entries are the table's, never a write's own values: composed as a read composes.
|
|
694
|
+
const { objectKeys: _o, shardTables: _t, ...plain } = schema;
|
|
695
|
+
const texts = new Map();
|
|
696
|
+
const owners = new Map();
|
|
697
|
+
table.values.forEach((entry, i) => {
|
|
698
|
+
if (entry.keyRecord === undefined) {
|
|
699
|
+
throw new Error(`KeyComposer: the shard table of index "${index.index}" was never bound to an entity — ` +
|
|
700
|
+
`Entity.make puts each entry's match into key form.`);
|
|
701
|
+
}
|
|
702
|
+
let text;
|
|
703
|
+
try {
|
|
704
|
+
text = composePkText(plain, entityType, index, extractKeyComposites(plain, index, index.pk.composite, entry.keyRecord), index.pk.composite, undefined);
|
|
705
|
+
}
|
|
706
|
+
catch (cause) {
|
|
707
|
+
throw shardsMalformed(index.index ?? "primary", `${describeEntry(entry, i)} composes no key: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
708
|
+
}
|
|
709
|
+
const first = owners.get(text);
|
|
710
|
+
if (first !== undefined) {
|
|
711
|
+
throw shardsMalformed(index.index ?? "primary", `${describeEntry(table.values[first], first)} and ${describeEntry(entry, i)} compose one ` +
|
|
712
|
+
`key, ${JSON.stringify(text)}, under the casing "${effectiveCasing(schema, index)}": one ` +
|
|
713
|
+
`value can have one layout. Remove one of them.`);
|
|
714
|
+
}
|
|
715
|
+
owners.set(text, i);
|
|
716
|
+
texts.set(text, entry.layout);
|
|
717
|
+
});
|
|
718
|
+
bySettings.set(settings, texts);
|
|
719
|
+
return texts;
|
|
720
|
+
};
|
|
721
|
+
/**
|
|
722
|
+
* The layout of the value whose unsharded partition key text `unsharded` composes
|
|
723
|
+
* (#287): its entry's, else the default. Composed only for a table with entries.
|
|
724
|
+
* Nothing caches a value's layout: the text index is the compiled table.
|
|
725
|
+
*/
|
|
726
|
+
export const shardLayoutFor = (schema, entityType, index, table, unsharded) => table.values.length === 0
|
|
727
|
+
? table.default
|
|
728
|
+
: (textIndexOf(schema, entityType, index, table).get(unsharded()) ?? table.default);
|
|
729
|
+
/**
|
|
730
|
+
* Check every declared shard table of an entity's indexes under `schema`, and build
|
|
731
|
+
* its text index (#287): two entries composing one key are `[EDD-9099]`. `Table.make`
|
|
732
|
+
* calls it, which knows the casing an index leaves to the schema. A provided index's
|
|
733
|
+
* table is checked where it's supplied, by passing it as `tables`.
|
|
734
|
+
*/
|
|
735
|
+
export const checkShardTables = (schema, entityType, indexes, tables) => {
|
|
736
|
+
for (const index of Object.values(indexes)) {
|
|
737
|
+
const shards = index.shards;
|
|
738
|
+
if (shards === undefined)
|
|
739
|
+
continue;
|
|
740
|
+
const layouts = shardLayoutsOf(shards);
|
|
741
|
+
const table = layouts === "provided" ? tables?.get(shards) : layouts;
|
|
742
|
+
if (table !== undefined)
|
|
743
|
+
textIndexOf(schema, entityType, index, table);
|
|
744
|
+
}
|
|
745
|
+
};
|
|
746
|
+
/**
|
|
747
|
+
* Bind a table to its entity's index (#287): check each entry's `match` names
|
|
748
|
+
* exactly the index's partition key composites, and put it into key form with
|
|
749
|
+
* `keyForm` (the entity's domain key form, which accepts a domain value or its
|
|
750
|
+
* stored form, as an accessor's pins do). `[EDD-9099]` for a malformed entry; a
|
|
751
|
+
* value with no key text is refused here, so a bound table always composes.
|
|
752
|
+
*/
|
|
753
|
+
export const bindShardTable = (indexName, index, table, keyForm) => {
|
|
754
|
+
if (table.values.length === 0)
|
|
755
|
+
return table;
|
|
756
|
+
const composites = index.pk.composite;
|
|
757
|
+
if (composites.length === 0) {
|
|
758
|
+
throw shardsMalformed(indexName, `layouts.values on an index whose partition key has no composites: every value composes ` +
|
|
759
|
+
`the one key, so only the default applies. Remove values.`);
|
|
760
|
+
}
|
|
761
|
+
const probe = makeDynamoSchema({ name: "shards", version: 1 });
|
|
762
|
+
return {
|
|
763
|
+
default: table.default,
|
|
764
|
+
values: table.values.map((entry, i) => {
|
|
765
|
+
const where = describeEntry(entry, i);
|
|
766
|
+
const names = Object.keys(entry.match);
|
|
767
|
+
const missing = composites.filter((attr) => !names.includes(attr));
|
|
768
|
+
const extra = names.filter((name) => !composites.includes(name));
|
|
769
|
+
if (missing.length > 0 || extra.length > 0) {
|
|
770
|
+
throw shardsMalformed(indexName, `${where} must name exactly the partition key composites of the index ` +
|
|
771
|
+
`(${composites.join(", ")})` +
|
|
772
|
+
(missing.length > 0 ? `; missing ${missing.map((a) => `"${a}"`).join(", ")}` : "") +
|
|
773
|
+
(extra.length > 0
|
|
774
|
+
? `; not a composite: ${extra.map((a) => `"${a}"`).join(", ")}`
|
|
775
|
+
: "") +
|
|
776
|
+
".");
|
|
777
|
+
}
|
|
778
|
+
let keyRecord;
|
|
779
|
+
try {
|
|
780
|
+
const absent = composites.filter((attr) => !hasValue(given(entry.match, attr)));
|
|
781
|
+
if (absent.length > 0) {
|
|
782
|
+
throw new Error(`no value for ${absent.map((a) => `"${a}"`).join(", ")}`);
|
|
783
|
+
}
|
|
784
|
+
keyRecord = keyForm(entry.match);
|
|
785
|
+
const objects = composites.filter((attr) => {
|
|
786
|
+
const value = given(keyRecord, attr);
|
|
787
|
+
return typeof value === "object" || typeof value === "function";
|
|
788
|
+
});
|
|
789
|
+
if (objects.length > 0) {
|
|
790
|
+
throw new Error(`${objects.map((a) => `"${a}"`).join(", ")} is not a value a key can hold`);
|
|
791
|
+
}
|
|
792
|
+
// A value with no key text (`NaN`) is refused here, under any schema.
|
|
793
|
+
extractKeyComposites(probe, index, composites, keyRecord);
|
|
794
|
+
}
|
|
795
|
+
catch (cause) {
|
|
796
|
+
throw shardsMalformed(indexName, `${where} composes no key: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
797
|
+
}
|
|
798
|
+
return { match: entry.match, keyRecord, layout: entry.layout };
|
|
799
|
+
}),
|
|
800
|
+
};
|
|
419
801
|
};
|
|
420
802
|
/**
|
|
421
803
|
* `index` as its unsharded layout: what a `previous: 1` migration reads, and the
|
|
@@ -469,11 +851,57 @@ export const tryComposeStructuralKeys = (schema, entityType, entityVersion, inde
|
|
|
469
851
|
};
|
|
470
852
|
/**
|
|
471
853
|
* Does stored partition key text lie in `index`'s current layout (#183): shard
|
|
472
|
-
* marker with this count, whatever shard
|
|
473
|
-
* can only be left alone when
|
|
854
|
+
* marker with this count, whatever shard, or the unsharded text for a count of 1
|
|
855
|
+
* (#287)? A row whose values can't compose its key can only be left alone when
|
|
856
|
+
* this holds.
|
|
857
|
+
*
|
|
858
|
+
* The layout is the row's resolved one (#287), from its key-form `record`: the
|
|
859
|
+
* value's own when every partition key composite is present; for a row with a hole
|
|
860
|
+
* in them, any of the default's and those of the entries agreeing with the
|
|
861
|
+
* composites it holds. Without `record`, the default's: a static declaration's
|
|
862
|
+
* only layout.
|
|
474
863
|
*/
|
|
475
|
-
export const isCurrentShardLayout = (schema, entityType, index, stored) =>
|
|
476
|
-
|
|
864
|
+
export const isCurrentShardLayout = (schema, entityType, index, stored, record) => {
|
|
865
|
+
if (index.shards === undefined)
|
|
866
|
+
return false;
|
|
867
|
+
const table = shardTableOf(schema, index);
|
|
868
|
+
const head = composePkText(schema, entityType, index, [], [], undefined);
|
|
869
|
+
const inLayout = ({ count }) => count === 1
|
|
870
|
+
? stored === head ||
|
|
871
|
+
(stored.startsWith(`${head}${KEY_DELIMITER}`) &&
|
|
872
|
+
!stored.startsWith(`${head}${KEY_DELIMITER}!`))
|
|
873
|
+
: stored.startsWith(composePkText(schema, entityType, index, [], [], `!${count}.`));
|
|
874
|
+
return candidateLayouts(schema, entityType, index, table, record).some(inLayout);
|
|
875
|
+
};
|
|
876
|
+
/** The layouts a row may be in, for {@link isCurrentShardLayout}. */
|
|
877
|
+
const candidateLayouts = (schema, entityType, index, table, record) => {
|
|
878
|
+
if (record === undefined || table.values.length === 0)
|
|
879
|
+
return [table.default];
|
|
880
|
+
const composites = index.pk.composite;
|
|
881
|
+
const textOf = (attr, from) => {
|
|
882
|
+
try {
|
|
883
|
+
return extractKeyComposites(schema, index, [attr], from)[0];
|
|
884
|
+
}
|
|
885
|
+
catch {
|
|
886
|
+
return undefined;
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
if (composites.every((attr) => hasValue(given(record, attr)))) {
|
|
890
|
+
try {
|
|
891
|
+
const own = extractKeyComposites(schema, index, composites, record);
|
|
892
|
+
return [
|
|
893
|
+
shardLayoutFor(schema, entityType, index, table, () => composePkText(schema, entityType, index, own, composites, undefined)),
|
|
894
|
+
];
|
|
895
|
+
}
|
|
896
|
+
catch {
|
|
897
|
+
return [table.default];
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
const present = composites.filter((attr) => hasValue(given(record, attr)));
|
|
901
|
+
const agreeing = table.values.filter((entry) => entry.keyRecord !== undefined &&
|
|
902
|
+
present.every((attr) => textOf(attr, record) === textOf(attr, entry.keyRecord)));
|
|
903
|
+
return [table.default, ...agreeing.map((entry) => entry.layout)];
|
|
904
|
+
};
|
|
477
905
|
const boundShardSource = (index, shards) => {
|
|
478
906
|
if (shards.source === undefined) {
|
|
479
907
|
throw new Error(`KeyComposer: sharded index "${index.index}" was never bound to an entity — Entity.make ` +
|
|
@@ -505,6 +933,10 @@ const pinsShardSource = (index, source, record) => {
|
|
|
505
933
|
* then the `previous` layout's, which are never pruned: the old source isn't
|
|
506
934
|
* recorded. Every read path uses this; {@link composePk} composes the one key a
|
|
507
935
|
* write stores.
|
|
936
|
+
*
|
|
937
|
+
* The layout is the pinned value's (#287). A layout of count 1 reads the unsharded
|
|
938
|
+
* key: alone, it is a plain query (no fan-out), exactly what an index without
|
|
939
|
+
* shards reads.
|
|
508
940
|
*/
|
|
509
941
|
export const composeReadPartitions = (schema, entityType, index, record,
|
|
510
942
|
/** `false`: read every shard, whatever is pinned (a collection's members may differ). */
|
|
@@ -514,32 +946,34 @@ options) => {
|
|
|
514
946
|
return { partitions: [composePk(schema, entityType, index, record)], fanOut: undefined };
|
|
515
947
|
}
|
|
516
948
|
const source = boundShardSource(index, shards);
|
|
949
|
+
const table = shardTableOf(schema, index);
|
|
517
950
|
const composites = extractKeyComposites(schema, index, index.pk.composite, record);
|
|
951
|
+
const unsharded = composePkText(schema, entityType, index, composites, index.pk.composite, undefined);
|
|
952
|
+
const resolved = shardLayoutFor(schema, entityType, index, table, () => unsharded);
|
|
518
953
|
const partition = (count, shard) => composePkText(schema, entityType, index, composites, index.pk.composite, shardMarker(count, shard));
|
|
519
|
-
const layout = (count) => Array.from({ length: count }, (_, i) => partition(count, i));
|
|
520
|
-
const
|
|
954
|
+
const layout = (count) => count === 1 ? [unsharded] : Array.from({ length: count }, (_, i) => partition(count, i));
|
|
955
|
+
const { count, previous } = resolved;
|
|
956
|
+
if (count === 1 && previous === undefined) {
|
|
957
|
+
return { partitions: [unsharded], fanOut: undefined, layout: resolved };
|
|
958
|
+
}
|
|
959
|
+
// A count of 1 has nothing to prune.
|
|
960
|
+
const pruned = count > 1 && options?.prune !== false && pinsShardSource(index, source, record);
|
|
521
961
|
const current = pruned
|
|
522
962
|
? [
|
|
523
|
-
partition(
|
|
963
|
+
partition(count, shardOf(source.map((attr) => serializeValue(given(record, attr))), count)),
|
|
524
964
|
]
|
|
525
|
-
: layout(
|
|
526
|
-
const
|
|
527
|
-
? composePkText(schema, entityType, index, composites, index.pk.composite, undefined)
|
|
528
|
-
: undefined;
|
|
529
|
-
const previous = unsharded !== undefined
|
|
530
|
-
? [unsharded]
|
|
531
|
-
: shards.previous === undefined
|
|
532
|
-
? []
|
|
533
|
-
: layout(shards.previous);
|
|
965
|
+
: layout(count);
|
|
966
|
+
const before = previous === undefined ? [] : layout(previous);
|
|
534
967
|
return {
|
|
535
|
-
partitions: [...current, ...
|
|
968
|
+
partitions: [...current, ...before],
|
|
536
969
|
fanOut: {
|
|
537
|
-
count
|
|
970
|
+
count,
|
|
538
971
|
source,
|
|
539
|
-
previous
|
|
972
|
+
previous,
|
|
540
973
|
pruned,
|
|
541
|
-
everyPartition: pruned ? [...layout(
|
|
974
|
+
everyPartition: pruned ? [...layout(count), ...before] : undefined,
|
|
542
975
|
},
|
|
976
|
+
layout: resolved,
|
|
543
977
|
};
|
|
544
978
|
};
|
|
545
979
|
/**
|
|
@@ -731,7 +1165,9 @@ export const composeAllKeys = (schema, entityType, entityVersion, indexes, recor
|
|
|
731
1165
|
*/
|
|
732
1166
|
const composePkPrefixUpTo = (schema, entityType, index, record, stopBefore) => {
|
|
733
1167
|
const slice = index.pk.composite.slice(0, stopBefore);
|
|
734
|
-
return composePkText(schema, entityType, index, extractKeyComposites(schema, index, slice, record, "pk"), slice,
|
|
1168
|
+
return composePkText(schema, entityType, index, extractKeyComposites(schema, index, slice, record, "pk"), slice,
|
|
1169
|
+
// Truncated text, which no entry composes: the default's layout (#287).
|
|
1170
|
+
composedShardMarker(schema, entityType, index, record, undefined));
|
|
735
1171
|
};
|
|
736
1172
|
/**
|
|
737
1173
|
* Compose a sort key from the leading prefix `[sk_0, ..., sk_(stopBefore-1)]`
|