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
package/dist/BlobGc.js
ADDED
|
@@ -0,0 +1,961 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BlobGc — garbage collection of offloaded values (#179).
|
|
3
|
+
*
|
|
4
|
+
* Every write of an offloaded field (`DynamoModel.Packed({ external })`,
|
|
5
|
+
* `DynamoModel.External()`) uploads a new object version, so superseded
|
|
6
|
+
* versions accumulate until something deletes them. GC is opt-in:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const gc = yield* BlobGc.make({
|
|
10
|
+
* entities: { Documents }, aggregates: { Orders }, streams: { Ledger },
|
|
11
|
+
* reaper: { gracePeriod: "15 minutes", confirm: "auto" },
|
|
12
|
+
* sweep: { gracePeriod: "1 day" },
|
|
13
|
+
* })
|
|
14
|
+
* yield* gc.reap(event.Records) // a Lambda / Pipes batch (records name their table)
|
|
15
|
+
* yield* gc.reap(batch.records, { table: batch.streamArn }) // a change feed batch
|
|
16
|
+
* yield* gc.sweep("Documents", { docId: "d-1" }) // one item family
|
|
17
|
+
* yield* gc.sweepPartition("Orders", { orderId: "o-1" })
|
|
18
|
+
* yield* Stream.runDrain(gc.sweepStore()) // the whole store, resumable
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* - **Table-local.** A version is a member's own only when its key's
|
|
22
|
+
* partition and family hashes are the ones the member computes from its own
|
|
23
|
+
* table name and the row's keys: a restored or copied table never deletes
|
|
24
|
+
* its source's versions (they are `foreign`), nor the source its copy's.
|
|
25
|
+
* - **The reaper** (`reap`) reads each record as the members on the table it
|
|
26
|
+
* came from (its `eventSourceARN` or `tableName`, or the call's `table`)
|
|
27
|
+
* and frees each own pointer its `OldImage` holds that its `NewImage` does
|
|
28
|
+
* not, after a grace (default 15 minutes):
|
|
29
|
+
* directly for plain entities, aggregate edges and EventStore snapshots;
|
|
30
|
+
* for `retain`, `softDelete` and `timeSeries` entities (or every entity,
|
|
31
|
+
* with `confirm: "always"`) only once a confirm-read of the item family —
|
|
32
|
+
* its sort-key ranges bracketed by strongly consistent reads of the live
|
|
33
|
+
* row — finds no row still holding it.
|
|
34
|
+
* - **The sweeps** list a family's own versions and delete one only when a
|
|
35
|
+
* later pass finds it still unreferenced at least the sweep grace (default
|
|
36
|
+
* 24 hours) after an earlier pass marked it; marks are the store's
|
|
37
|
+
* unreferenced-since tags. `sweepStore` walks the store's listing and
|
|
38
|
+
* sweeps each own item family an object's metadata names.
|
|
39
|
+
*
|
|
40
|
+
* `BlobGc.make` resolves the `BlobStore`, `DynamoClient` and each member's
|
|
41
|
+
* `TableConfig` once; the collector's operations have no requirements.
|
|
42
|
+
*
|
|
43
|
+
* See `DESIGN.md §7 "Offloaded Storage"` and `docs/designs/blob-offload.md`
|
|
44
|
+
* §11.
|
|
45
|
+
*/
|
|
46
|
+
import { ValidationError } from "@effect-dynamodb/schema/Errors.js";
|
|
47
|
+
import { Clock, Context, Duration, Effect, Option, Stream } from "effect";
|
|
48
|
+
import { BlobStore } from "./BlobStore.js";
|
|
49
|
+
import { fromDefinition as entityFromDefinition } from "./Entity.js";
|
|
50
|
+
import { storeRequirementsOf } from "./EventStore.js";
|
|
51
|
+
import { attributeValueEquals } from "./internal/AttributeValueEquals.js";
|
|
52
|
+
import { call, refKey } from "./internal/BlobCalls.js";
|
|
53
|
+
import { familyHash, partitionHash, STREAM_FAMILY_SK, } from "./internal/BlobKeys.js";
|
|
54
|
+
import * as BlobMetadata from "./internal/BlobMetadata.js";
|
|
55
|
+
import * as Capacity from "./internal/Capacity.js";
|
|
56
|
+
import * as GcSupport from "./internal/GcSupport.js";
|
|
57
|
+
import { recordImages } from "./internal/OffloadChange.js";
|
|
58
|
+
import { aggregateStoreRequirements, entityStoreRequirements, missingStoreError, offloadedAttributes, unescapedKeysError, } from "./internal/OffloadWrite.js";
|
|
59
|
+
import { imageEntityType, normalizeImageSync } from "./internal/Packed.js";
|
|
60
|
+
import { compareUtf8 } from "./internal/Utf8Order.js";
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
// make
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
const DEFAULT_REAPER_GRACE = Duration.minutes(15);
|
|
65
|
+
const DEFAULT_SWEEP_GRACE = Duration.hours(24);
|
|
66
|
+
const MIN_SWEEP_GRACE = Duration.hours(1);
|
|
67
|
+
const OPTION_KEYS = new Set([
|
|
68
|
+
"entities",
|
|
69
|
+
"aggregates",
|
|
70
|
+
"streams",
|
|
71
|
+
"reaper",
|
|
72
|
+
"sweep",
|
|
73
|
+
]);
|
|
74
|
+
const REAPER_KEYS = new Set(["gracePeriod", "confirm"]);
|
|
75
|
+
const SWEEP_KEYS = new Set(["gracePeriod"]);
|
|
76
|
+
const SWEEP_STORE_KEYS = new Set(["prefix", "startAfter", "maxFamilies"]);
|
|
77
|
+
const REAP_KEYS = new Set(["table"]);
|
|
78
|
+
const show = (value) => {
|
|
79
|
+
try {
|
|
80
|
+
return typeof value === "string" ? JSON.stringify(value) : String(value);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return typeof value;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
/** A `[EDD-9118]` configuration error. */
|
|
87
|
+
const invalid = (where, message) => new Error(`[EDD-9118] ${where}: ${message}`);
|
|
88
|
+
const checkKeys = (where, what, value, keys) => {
|
|
89
|
+
if (value === undefined)
|
|
90
|
+
return;
|
|
91
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
92
|
+
throw invalid(where, `${what} must be an object, got ${show(value)}.`);
|
|
93
|
+
}
|
|
94
|
+
for (const key of Object.keys(value)) {
|
|
95
|
+
if (!keys.has(key)) {
|
|
96
|
+
throw invalid(where, `unknown option ${what === "options" ? "" : `${what}.`}${key}.`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const durationOf = (value) => {
|
|
101
|
+
try {
|
|
102
|
+
return Option.getOrUndefined(Duration.fromInput(value));
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
/** A table or stream ARN: `arn:<partition>:dynamodb:<region>:<account>:table/<name>[/…]`. */
|
|
109
|
+
const TABLE_ARN = /^arn:[^:]+:dynamodb:[^:]*:[^:]*:table\/([^/]+)(?:\/.*)?$/;
|
|
110
|
+
/**
|
|
111
|
+
* A table's name from its name, or a DynamoDB table or stream ARN;
|
|
112
|
+
* `undefined` for any other ARN (a Kinesis stream's, say).
|
|
113
|
+
*/
|
|
114
|
+
const tableNameOf = (value) => value.startsWith("arn:") ? TABLE_ARN.exec(value)?.[1] : value;
|
|
115
|
+
/**
|
|
116
|
+
* Make a garbage collector for the members' offloaded values. Resolves the
|
|
117
|
+
* `BlobStore` (`[EDD-9115]` without one), `DynamoClient` and each member's
|
|
118
|
+
* `TableConfig` once. A member on a table whose schema doesn't set
|
|
119
|
+
* `escapeKeys: true` is refused first (`[EDD-9146]`, a defect), as every
|
|
120
|
+
* binding refuses it.
|
|
121
|
+
*
|
|
122
|
+
* @throws `[EDD-9118]` (a defect) when an option is unknown or invalid — a
|
|
123
|
+
* reaper grace that is not a non-negative duration; a sweep grace under 1
|
|
124
|
+
* hour or under the store's `maxUploadAge` + 15 minutes; a `confirm` other
|
|
125
|
+
* than `"auto"` / `"always"` — or a member is not an effect-dynamodb
|
|
126
|
+
* member, is not registered on a table, has no offloaded field, or shares
|
|
127
|
+
* its name with a member `sweepPartition` also takes, or no member is given.
|
|
128
|
+
*/
|
|
129
|
+
export const make = (options) => Effect.gen(function* () {
|
|
130
|
+
const where = "BlobGc.make";
|
|
131
|
+
checkKeys(where, "options", options, OPTION_KEYS);
|
|
132
|
+
if (options === undefined || options === null)
|
|
133
|
+
throw invalid(where, "options are required.");
|
|
134
|
+
checkKeys(where, "reaper", options.reaper, REAPER_KEYS);
|
|
135
|
+
checkKeys(where, "sweep", options.sweep, SWEEP_KEYS);
|
|
136
|
+
const reaperGrace = options.reaper?.gracePeriod === undefined
|
|
137
|
+
? DEFAULT_REAPER_GRACE
|
|
138
|
+
: durationOf(options.reaper.gracePeriod);
|
|
139
|
+
if (reaperGrace === undefined ||
|
|
140
|
+
!Duration.isFinite(reaperGrace) ||
|
|
141
|
+
Duration.isLessThan(reaperGrace, Duration.zero)) {
|
|
142
|
+
throw invalid(where, `reaper.gracePeriod must be a non-negative duration, got ${show(options.reaper?.gracePeriod)}.`);
|
|
143
|
+
}
|
|
144
|
+
const confirm = options.reaper?.confirm ?? "auto";
|
|
145
|
+
if (confirm !== "auto" && confirm !== "always") {
|
|
146
|
+
throw invalid(where, `reaper.confirm must be "auto" or "always", got ${show(confirm)}.`);
|
|
147
|
+
}
|
|
148
|
+
const context = Capacity.detachContext(yield* Effect.context());
|
|
149
|
+
const store = Context.getOrUndefined(context, BlobStore);
|
|
150
|
+
const inFlight = store === undefined ? undefined : GcSupport.inFlightAge(store);
|
|
151
|
+
// Members.
|
|
152
|
+
const entities = [];
|
|
153
|
+
const aggregates = [];
|
|
154
|
+
const streams = [];
|
|
155
|
+
// What each member offloads, and whether its table's key text is escaped.
|
|
156
|
+
const requirements = [];
|
|
157
|
+
const tableOf = (kind, name, tag) => Effect.gen(function* () {
|
|
158
|
+
if (tag === undefined) {
|
|
159
|
+
throw invalid(where, `${kind} "${name}" is not registered on a table: register it with Table.make first.`);
|
|
160
|
+
}
|
|
161
|
+
return (yield* tag).name;
|
|
162
|
+
});
|
|
163
|
+
for (const [name, raw] of Object.entries(options.entities ?? {})) {
|
|
164
|
+
// A pure definition (`@effect-dynamodb/schema`'s `Entity.make`) is
|
|
165
|
+
// promoted, as `DynamoClient.make` promotes it.
|
|
166
|
+
const member = typeof raw.get !== "function" &&
|
|
167
|
+
typeof raw._config === "object" &&
|
|
168
|
+
typeof raw._data === "object"
|
|
169
|
+
? entityFromDefinition(raw)
|
|
170
|
+
: raw;
|
|
171
|
+
const support = GcSupport.supportOf(member);
|
|
172
|
+
if (support?.kind !== "entity") {
|
|
173
|
+
throw invalid(where, `entities.${name} is not an effect-dynamodb entity.`);
|
|
174
|
+
}
|
|
175
|
+
if (support.offload === undefined) {
|
|
176
|
+
throw invalid(where, `entity "${name}" has no offloaded field: GC has nothing to collect.`);
|
|
177
|
+
}
|
|
178
|
+
const table = yield* tableOf("entity", name, support.tableTag());
|
|
179
|
+
requirements.push(...entityStoreRequirements(`entity "${name}"`, member));
|
|
180
|
+
entities.push({
|
|
181
|
+
name,
|
|
182
|
+
table,
|
|
183
|
+
tableName: tableNameOf(table) ?? table,
|
|
184
|
+
support,
|
|
185
|
+
offload: support.offload,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
for (const [name, member] of Object.entries(options.aggregates ?? {})) {
|
|
189
|
+
const support = GcSupport.supportOf(member);
|
|
190
|
+
if (support?.kind !== "aggregate") {
|
|
191
|
+
throw invalid(where, `aggregates.${name} is not an effect-dynamodb aggregate.`);
|
|
192
|
+
}
|
|
193
|
+
if (support.edges.size === 0) {
|
|
194
|
+
throw invalid(where, `aggregate "${name}" has no offloading edge entity: GC has nothing to collect.`);
|
|
195
|
+
}
|
|
196
|
+
const table = yield* tableOf("aggregate", name, support.tableTag());
|
|
197
|
+
requirements.push(...aggregateStoreRequirements(`aggregate "${name}"`, member));
|
|
198
|
+
aggregates.push({ name, table, tableName: tableNameOf(table) ?? table, support });
|
|
199
|
+
}
|
|
200
|
+
for (const [name, member] of Object.entries(options.streams ?? {})) {
|
|
201
|
+
const support = GcSupport.supportOf(member);
|
|
202
|
+
if (support?.kind !== "stream") {
|
|
203
|
+
throw invalid(where, `streams.${name} is not an EventStore stream.`);
|
|
204
|
+
}
|
|
205
|
+
if (support.event === undefined && support.snapshot === undefined) {
|
|
206
|
+
throw invalid(where, `stream "${name}" offloads neither event data nor snapshot state: GC has nothing to collect.`);
|
|
207
|
+
}
|
|
208
|
+
const table = yield* tableOf("stream", name, support.tableTag());
|
|
209
|
+
requirements.push(...storeRequirementsOf(member));
|
|
210
|
+
streams.push({ name, table, tableName: tableNameOf(table) ?? table, support });
|
|
211
|
+
}
|
|
212
|
+
for (const { name } of aggregates) {
|
|
213
|
+
if (streams.some((stream) => stream.name === name)) {
|
|
214
|
+
throw invalid(where, `"${name}" names both an aggregate and a stream: sweepPartition takes one name each.`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (entities.length + aggregates.length + streams.length === 0) {
|
|
218
|
+
throw invalid(where, "no member is given: pass entities, aggregates or streams.");
|
|
219
|
+
}
|
|
220
|
+
// Offloading requires escaped key text (`[EDD-9146]`, design §11.10): GC
|
|
221
|
+
// finds a version's owner from the key text its object is named after, so
|
|
222
|
+
// it collects for members whose rows can be written nowhere else. Checked
|
|
223
|
+
// before the store, as every binding checks it.
|
|
224
|
+
const keysError = unescapedKeysError(where, requirements);
|
|
225
|
+
if (keysError !== undefined)
|
|
226
|
+
throw keysError;
|
|
227
|
+
const storeError = missingStoreError(where, requirements, store);
|
|
228
|
+
if (storeError !== undefined || store === undefined || inFlight === undefined) {
|
|
229
|
+
throw storeError ?? new Error("[EDD-9115] BlobGc.make: no BlobStore is resolvable.");
|
|
230
|
+
}
|
|
231
|
+
const sweepGrace = options.sweep?.gracePeriod === undefined
|
|
232
|
+
? Duration.max(DEFAULT_SWEEP_GRACE, inFlight)
|
|
233
|
+
: durationOf(options.sweep.gracePeriod);
|
|
234
|
+
if (sweepGrace === undefined ||
|
|
235
|
+
!Duration.isFinite(sweepGrace) ||
|
|
236
|
+
Duration.isLessThan(sweepGrace, MIN_SWEEP_GRACE) ||
|
|
237
|
+
Duration.isLessThan(sweepGrace, inFlight)) {
|
|
238
|
+
throw invalid(where, "sweep.gracePeriod must be a duration of at least 1 hour and at least the store's " +
|
|
239
|
+
`maxUploadAge + 15 minutes (${Duration.format(inFlight)}), got ` +
|
|
240
|
+
`${show(options.sweep?.gracePeriod)}.`);
|
|
241
|
+
}
|
|
242
|
+
const gc = makeCollector({
|
|
243
|
+
store,
|
|
244
|
+
entities,
|
|
245
|
+
aggregates,
|
|
246
|
+
streams,
|
|
247
|
+
reaperGrace,
|
|
248
|
+
confirm,
|
|
249
|
+
sweepGrace,
|
|
250
|
+
});
|
|
251
|
+
const provide = (effect) => Effect.provide(effect, context);
|
|
252
|
+
return {
|
|
253
|
+
reap: (records, reapOptions) => provide(Capacity.operation("BlobGc.reap", undefined, gc.reap(records, reapOptions))),
|
|
254
|
+
sweep: (member, key) => provide(Capacity.operation("BlobGc.sweep", { target: member }, gc.sweep(member, key))),
|
|
255
|
+
sweepPartition: (member, key) => provide(Capacity.operation("BlobGc.sweepPartition", { target: member }, gc.sweepPartition(member, key))),
|
|
256
|
+
sweepStore: (storeOptions) => Stream.unwrap(provide(Capacity.operationStream("BlobGc.sweepStore", undefined, gc.sweepStore(storeOptions)))).pipe(Stream.provideContext(context)),
|
|
257
|
+
};
|
|
258
|
+
});
|
|
259
|
+
const BUSY = {
|
|
260
|
+
referenced: 0,
|
|
261
|
+
listed: 0,
|
|
262
|
+
marked: 0,
|
|
263
|
+
deleted: 0,
|
|
264
|
+
retainedYoung: 0,
|
|
265
|
+
foreign: 0,
|
|
266
|
+
busy: true,
|
|
267
|
+
};
|
|
268
|
+
/** One attribute, or `"(several)"`, for the context of a call covering several. */
|
|
269
|
+
const oneOrSeveral = (values) => {
|
|
270
|
+
const distinct = new Set(values);
|
|
271
|
+
return distinct.size === 1 ? [...distinct][0] : "(several)";
|
|
272
|
+
};
|
|
273
|
+
const S = (value) => ({ S: value });
|
|
274
|
+
/** A stored attribute's string value (an unmarshalled image's, or `{ S }`). */
|
|
275
|
+
const stringOf = (value) => {
|
|
276
|
+
if (typeof value === "string")
|
|
277
|
+
return value;
|
|
278
|
+
const s = value?.S;
|
|
279
|
+
return typeof s === "string" ? s : undefined;
|
|
280
|
+
};
|
|
281
|
+
const SKIP = { _tag: "skip" };
|
|
282
|
+
/** The own and foreign versions `attributes` of `oldImage` hold that `newImage` does not. */
|
|
283
|
+
const superseded = (oldImage, newImage, attributes, segment, candidate) => {
|
|
284
|
+
const candidates = [];
|
|
285
|
+
const foreign = [];
|
|
286
|
+
for (const attribute of attributes) {
|
|
287
|
+
const before = GcSupport.pointerOf(oldImage[attribute]);
|
|
288
|
+
if (before === undefined)
|
|
289
|
+
continue;
|
|
290
|
+
const after = newImage === undefined ? undefined : GcSupport.pointerOf(newImage[attribute]);
|
|
291
|
+
if (after !== undefined && GcSupport.sameRef(before.ref, after.ref))
|
|
292
|
+
continue;
|
|
293
|
+
const own = segment !== undefined &&
|
|
294
|
+
GcSupport.leadingPrefix(before.ref.key, segment, GcSupport.FAMILY_REST) !== undefined;
|
|
295
|
+
if (own)
|
|
296
|
+
candidates.push(candidate(before.ref, attribute));
|
|
297
|
+
else
|
|
298
|
+
foreign.push(before.ref);
|
|
299
|
+
}
|
|
300
|
+
return { _tag: "act", candidates, foreign };
|
|
301
|
+
};
|
|
302
|
+
/**
|
|
303
|
+
* When a record was written, in epoch milliseconds: a `Date` (the SDK); a
|
|
304
|
+
* number in the unit `ApproximateCreationDateTimePrecision` names (Kinesis
|
|
305
|
+
* Data Streams: `MILLISECOND` / `MICROSECOND`), else by magnitude — epoch
|
|
306
|
+
* seconds (DynamoDB Streams JSON: Lambda, Pipes) below `1e11`, milliseconds
|
|
307
|
+
* below `1e14`, microseconds above. `now` when it does not say, and never
|
|
308
|
+
* later than `now`: a malformed time can delay a batch by the grace at most.
|
|
309
|
+
*/
|
|
310
|
+
const recordTime = (record, now) => {
|
|
311
|
+
const at = record.dynamodb?.ApproximateCreationDateTime;
|
|
312
|
+
let millis = now;
|
|
313
|
+
if (at instanceof Date) {
|
|
314
|
+
millis = at.getTime();
|
|
315
|
+
}
|
|
316
|
+
else if (typeof at === "number") {
|
|
317
|
+
const precision = record.dynamodb?.ApproximateCreationDateTimePrecision;
|
|
318
|
+
millis =
|
|
319
|
+
precision === "MICROSECOND"
|
|
320
|
+
? at / 1000
|
|
321
|
+
: precision === "MILLISECOND"
|
|
322
|
+
? at
|
|
323
|
+
: at < 1e11
|
|
324
|
+
? at * 1000
|
|
325
|
+
: at < 1e14
|
|
326
|
+
? at
|
|
327
|
+
: at / 1000;
|
|
328
|
+
}
|
|
329
|
+
return Number.isFinite(millis) ? Math.min(millis, now) : now;
|
|
330
|
+
};
|
|
331
|
+
/**
|
|
332
|
+
* The table a record names itself: a DynamoDB `eventSourceARN`'s (Lambda,
|
|
333
|
+
* Pipes) or its `tableName` (Kinesis Data Streams); a string saying why
|
|
334
|
+
* when the two disagree.
|
|
335
|
+
*/
|
|
336
|
+
const namedTable = (record) => {
|
|
337
|
+
const arn = record.eventSourceARN;
|
|
338
|
+
const fromArn = typeof arn === "string" ? tableNameOf(arn) : undefined;
|
|
339
|
+
const fromName = typeof record.tableName === "string" ? record.tableName : undefined;
|
|
340
|
+
if (fromArn !== undefined && fromName !== undefined && fromArn !== fromName) {
|
|
341
|
+
return {
|
|
342
|
+
conflict: `its eventSourceARN names table "${fromArn}" and its tableName "${fromName}"`,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
return { table: fromArn ?? fromName };
|
|
346
|
+
};
|
|
347
|
+
const makeCollector = (settings) => {
|
|
348
|
+
const { store } = settings;
|
|
349
|
+
const requireCapabilities = (operation, needs) => {
|
|
350
|
+
const missing = needs.filter((capability) => store[capability] === undefined);
|
|
351
|
+
return missing.length === 0
|
|
352
|
+
? Effect.void
|
|
353
|
+
: Effect.die(invalid(operation, `the store ${store.name} lacks ${missing.join(", ")}, which ${operation} needs ` +
|
|
354
|
+
`(${needs.join(", ")}).`));
|
|
355
|
+
};
|
|
356
|
+
// -------------------------------------------------------------------------
|
|
357
|
+
// reap (§11.3)
|
|
358
|
+
// -------------------------------------------------------------------------
|
|
359
|
+
const entitiesByType = new Map();
|
|
360
|
+
for (const m of settings.entities) {
|
|
361
|
+
const list = entitiesByType.get(m.support.entityType) ?? [];
|
|
362
|
+
list.push(m);
|
|
363
|
+
entitiesByType.set(m.support.entityType, list);
|
|
364
|
+
}
|
|
365
|
+
const readEntity = (m, eventName, oldImage, newImage) => {
|
|
366
|
+
const { support } = m;
|
|
367
|
+
const pk = stringOf(oldImage[support.pkField]);
|
|
368
|
+
const sk = stringOf(oldImage[support.skField]);
|
|
369
|
+
const liveSk = support.liveSkOfRow(oldImage);
|
|
370
|
+
// A retain update or delete snapshotted the old value in the same write.
|
|
371
|
+
if (support.lifecycle.retain && eventName === "MODIFY" && sk !== undefined && sk === liveSk) {
|
|
372
|
+
return SKIP;
|
|
373
|
+
}
|
|
374
|
+
const family = pk === undefined || liveSk === undefined ? undefined : { pk, liveSk };
|
|
375
|
+
const confirms = settings.confirm === "always" ||
|
|
376
|
+
support.lifecycle.retain ||
|
|
377
|
+
support.lifecycle.softDelete ||
|
|
378
|
+
support.lifecycle.timeSeries;
|
|
379
|
+
const ranges = settings.confirm === "always" ? GcSupport.ALL_RANGES : GcSupport.autoRanges(support.lifecycle);
|
|
380
|
+
return superseded(oldImage, newImage, offloadedAttributes(m.offload), family === undefined
|
|
381
|
+
? undefined
|
|
382
|
+
: GcSupport.familySegment(m.table, S(family.pk), S(family.liveSk)), (ref, attribute) => ({
|
|
383
|
+
ref,
|
|
384
|
+
entityType: support.entityType,
|
|
385
|
+
attribute,
|
|
386
|
+
confirm: confirms && family !== undefined ? { member: m, family, ranges } : undefined,
|
|
387
|
+
}));
|
|
388
|
+
};
|
|
389
|
+
const readAggregate = (m, info, oldImage, newImage) => {
|
|
390
|
+
const pk = stringOf(oldImage[m.support.pkField]);
|
|
391
|
+
const sk = stringOf(oldImage[m.support.skField]);
|
|
392
|
+
return superseded(oldImage, newImage, offloadedAttributes(info), pk === undefined || sk === undefined
|
|
393
|
+
? undefined
|
|
394
|
+
: GcSupport.familySegment(m.table, S(pk), S(sk)), (ref, attribute) => ({ ref, entityType: info.entityType, attribute, confirm: undefined }));
|
|
395
|
+
};
|
|
396
|
+
const readStream = (m, type, oldImage, newImage) => {
|
|
397
|
+
if (type === m.support.eventType)
|
|
398
|
+
return m.support.event === undefined ? undefined : SKIP;
|
|
399
|
+
const info = m.support.snapshot;
|
|
400
|
+
if (type !== m.support.snapshotType || info === undefined)
|
|
401
|
+
return undefined;
|
|
402
|
+
const pk = stringOf(oldImage[m.support.pkField]);
|
|
403
|
+
return superseded(oldImage, newImage, offloadedAttributes(info), pk === undefined ? undefined : GcSupport.familySegment(m.table, S(pk), STREAM_FAMILY_SK), (ref, attribute) => ({ ref, entityType: info.entityType, attribute, confirm: undefined }));
|
|
404
|
+
};
|
|
405
|
+
/**
|
|
406
|
+
* The reading of a record's images by every member on its table `table`
|
|
407
|
+
* whose type it carries, `[]` when it is no such member's. Only those: a
|
|
408
|
+
* member computes ownership from its own table and the record's keys, so a
|
|
409
|
+
* copied row read as another table's member would make the copy's pointers
|
|
410
|
+
* look like that member's own (§11.2).
|
|
411
|
+
*/
|
|
412
|
+
const readingsOf = (type, table, eventName, oldImage, newImage) => {
|
|
413
|
+
const readings = [];
|
|
414
|
+
for (const m of entitiesByType.get(type) ?? []) {
|
|
415
|
+
if (m.tableName === table)
|
|
416
|
+
readings.push(readEntity(m, eventName, oldImage, newImage));
|
|
417
|
+
}
|
|
418
|
+
for (const m of settings.aggregates) {
|
|
419
|
+
const info = m.support.edges.get(type);
|
|
420
|
+
const pk = stringOf(oldImage[m.support.pkField]);
|
|
421
|
+
// Only a row in the aggregate's own partition: an edge entity used standalone on the same
|
|
422
|
+
// table writes rows of the same type, whose history (snapshots, tombstones) the aggregate
|
|
423
|
+
// reading would never see — read as edge rows, they would be freed directly.
|
|
424
|
+
if (info !== undefined &&
|
|
425
|
+
m.tableName === table &&
|
|
426
|
+
pk !== undefined &&
|
|
427
|
+
m.support.ownsPartition(pk)) {
|
|
428
|
+
readings.push(readAggregate(m, info, oldImage, newImage));
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
for (const m of settings.streams) {
|
|
432
|
+
if (m.tableName !== table)
|
|
433
|
+
continue;
|
|
434
|
+
const reading = readStream(m, type, oldImage, newImage);
|
|
435
|
+
if (reading !== undefined)
|
|
436
|
+
readings.push(reading);
|
|
437
|
+
}
|
|
438
|
+
return readings;
|
|
439
|
+
};
|
|
440
|
+
/** The probe attribute that tells DynamoDB JSON from an unmarshalled image. */
|
|
441
|
+
const probeOf = (type) => entitiesByType.get(type)?.[0]?.support.pkField ??
|
|
442
|
+
settings.aggregates.find((m) => m.support.edges.has(type))?.support.pkField ??
|
|
443
|
+
"pk";
|
|
444
|
+
/** A member, on any table, declares the type. */
|
|
445
|
+
const isMemberType = (type) => entitiesByType.has(type) ||
|
|
446
|
+
settings.aggregates.some((m) => m.support.edges.has(type)) ||
|
|
447
|
+
settings.streams.some((m) => (type === m.support.eventType && m.support.event !== undefined) ||
|
|
448
|
+
(type === m.support.snapshotType && m.support.snapshot !== undefined));
|
|
449
|
+
/** The members' tables, by the name a record gives them. */
|
|
450
|
+
const memberTables = [
|
|
451
|
+
...new Set([...settings.entities, ...settings.aggregates, ...settings.streams].map((m) => m.tableName)),
|
|
452
|
+
].sort(compareUtf8);
|
|
453
|
+
const reap = (records, options) => Effect.gen(function* () {
|
|
454
|
+
const operation = "BlobGc.reap";
|
|
455
|
+
try {
|
|
456
|
+
checkKeys(operation, "options", options, REAP_KEYS);
|
|
457
|
+
}
|
|
458
|
+
catch (error) {
|
|
459
|
+
return yield* Effect.die(error);
|
|
460
|
+
}
|
|
461
|
+
const option = options?.table;
|
|
462
|
+
const given = typeof option === "string" ? tableNameOf(option) : undefined;
|
|
463
|
+
if (option !== undefined && (given === undefined || given.length === 0)) {
|
|
464
|
+
return yield* Effect.die(invalid(operation, `table must be a table name or a DynamoDB table or stream ARN, got ${show(option)}.`));
|
|
465
|
+
}
|
|
466
|
+
/** The table record `n` came from, or why it cannot be told. */
|
|
467
|
+
const tableOfRecord = (record, n) => {
|
|
468
|
+
const named = namedTable(record);
|
|
469
|
+
if ("conflict" in named)
|
|
470
|
+
return invalid(operation, `record ${n}: ${named.conflict}.`);
|
|
471
|
+
if (named.table !== undefined && given !== undefined && named.table !== given) {
|
|
472
|
+
return invalid(operation, `record ${n} comes from table "${named.table}", not the table option's "${given}".`);
|
|
473
|
+
}
|
|
474
|
+
const table = named.table ?? given ?? (memberTables.length === 1 ? memberTables[0] : undefined);
|
|
475
|
+
return table !== undefined
|
|
476
|
+
? table
|
|
477
|
+
: invalid(operation, `record ${n} does not name its table (no eventSourceARN or tableName) and the ` +
|
|
478
|
+
`collector's members are on several tables (${memberTables.join(", ")}): pass ` +
|
|
479
|
+
"reap(records, { table }).");
|
|
480
|
+
};
|
|
481
|
+
const all = [...records];
|
|
482
|
+
const now = yield* Clock.currentTimeMillis;
|
|
483
|
+
// 1. Check every record before acting on any (§11.3 step 1).
|
|
484
|
+
const candidates = new Map();
|
|
485
|
+
const foreign = new Map();
|
|
486
|
+
let skipped = 0;
|
|
487
|
+
let newest;
|
|
488
|
+
for (const [n, record] of all.entries()) {
|
|
489
|
+
const images = recordImages(record, operation);
|
|
490
|
+
if (typeof images === "string") {
|
|
491
|
+
if (images.startsWith("[EDD-9119]"))
|
|
492
|
+
return yield* Effect.die(new Error(images));
|
|
493
|
+
return yield* new ValidationError({
|
|
494
|
+
entityType: "BlobGc",
|
|
495
|
+
operation,
|
|
496
|
+
cause: new Error(`Malformed stream record: ${images}`),
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
const { eventName, oldImage, newImage } = images;
|
|
500
|
+
const type = oldImage === undefined ? undefined : imageEntityType(oldImage);
|
|
501
|
+
if (eventName === "INSERT" || oldImage === undefined || type === undefined) {
|
|
502
|
+
skipped++;
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
if (!isMemberType(type)) {
|
|
506
|
+
skipped++;
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
const table = tableOfRecord(record, n);
|
|
510
|
+
if (table instanceof Error)
|
|
511
|
+
return yield* Effect.die(table);
|
|
512
|
+
const ctx = { entityType: type, operation };
|
|
513
|
+
const probe = { probe: probeOf(type) };
|
|
514
|
+
const before = normalizeImageSync(oldImage, ctx, probe);
|
|
515
|
+
if (before instanceof ValidationError)
|
|
516
|
+
return yield* before;
|
|
517
|
+
const after = newImage === undefined ? undefined : normalizeImageSync(newImage, ctx, probe);
|
|
518
|
+
if (after instanceof ValidationError)
|
|
519
|
+
return yield* after;
|
|
520
|
+
const readings = readingsOf(type, table, eventName, before, after);
|
|
521
|
+
// A member reading that rules the record out wins: it frees nothing.
|
|
522
|
+
if (readings.length === 0 || readings.some((reading) => reading._tag === "skip")) {
|
|
523
|
+
skipped++;
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
const owned = new Set();
|
|
527
|
+
let contributes = false;
|
|
528
|
+
for (const reading of readings) {
|
|
529
|
+
if (reading._tag !== "act")
|
|
530
|
+
continue;
|
|
531
|
+
for (const candidate of reading.candidates) {
|
|
532
|
+
const k = refKey(candidate.ref);
|
|
533
|
+
owned.add(k);
|
|
534
|
+
contributes = true;
|
|
535
|
+
const known = candidates.get(k);
|
|
536
|
+
// A confirm-read wins over a direct free of the same version.
|
|
537
|
+
if (known === undefined ||
|
|
538
|
+
(known.confirm === undefined && candidate.confirm !== undefined)) {
|
|
539
|
+
candidates.set(k, candidate);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
for (const reading of readings) {
|
|
544
|
+
if (reading._tag !== "act")
|
|
545
|
+
continue;
|
|
546
|
+
for (const ref of reading.foreign) {
|
|
547
|
+
if (!owned.has(refKey(ref)))
|
|
548
|
+
foreign.set(refKey(ref), ref);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
if (contributes)
|
|
552
|
+
newest = Math.max(newest ?? 0, recordTime(record, now));
|
|
553
|
+
}
|
|
554
|
+
for (const k of candidates.keys())
|
|
555
|
+
foreign.delete(k);
|
|
556
|
+
// 2. Grace, from the newest record that superseded something (§11.3 step 3).
|
|
557
|
+
if (newest !== undefined) {
|
|
558
|
+
const due = newest + Duration.toMillis(settings.reaperGrace);
|
|
559
|
+
const current = yield* Clock.currentTimeMillis;
|
|
560
|
+
if (due > current)
|
|
561
|
+
yield* Effect.sleep(Duration.millis(due - current));
|
|
562
|
+
}
|
|
563
|
+
// 3. Free, by member rule (§11.3 step 4).
|
|
564
|
+
const freed = [];
|
|
565
|
+
let retained = 0;
|
|
566
|
+
const families = new Map();
|
|
567
|
+
for (const candidate of candidates.values()) {
|
|
568
|
+
if (candidate.confirm === undefined) {
|
|
569
|
+
freed.push(candidate);
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
const { member, family } = candidate.confirm;
|
|
573
|
+
const k = `${member.name}\u0000${family.pk}\u0000${family.liveSk}`;
|
|
574
|
+
const list = families.get(k) ?? [];
|
|
575
|
+
list.push(candidate);
|
|
576
|
+
families.set(k, list);
|
|
577
|
+
}
|
|
578
|
+
if (families.size > 0) {
|
|
579
|
+
const client = yield* Capacity.client;
|
|
580
|
+
for (const group of families.values()) {
|
|
581
|
+
const { member, family } = group[0].confirm;
|
|
582
|
+
const ranges = new Set();
|
|
583
|
+
for (const candidate of group)
|
|
584
|
+
for (const range of candidate.confirm.ranges)
|
|
585
|
+
ranges.add(range);
|
|
586
|
+
const held = yield* GcSupport.confirmRead({
|
|
587
|
+
client,
|
|
588
|
+
table: member.table,
|
|
589
|
+
liveKey: {
|
|
590
|
+
[member.support.pkField]: { S: family.pk },
|
|
591
|
+
[member.support.skField]: { S: family.liveSk },
|
|
592
|
+
},
|
|
593
|
+
readRows: member.support.readFamily(client, member.table, family, ranges),
|
|
594
|
+
});
|
|
595
|
+
if (Option.isNone(held)) {
|
|
596
|
+
retained += group.length;
|
|
597
|
+
yield* Effect.logWarning(`effect-dynamodb: BlobGc.reap kept ${group.length} superseded version(s) of a ` +
|
|
598
|
+
`${member.support.entityType} item: its live row changed while the reaper read ` +
|
|
599
|
+
`its family (${GcSupport.CONFIRM_ATTEMPTS} attempts). A later sweep collects them.`).pipe(Effect.annotateLogs({ table: member.table, pk: family.pk, sk: family.liveSk }));
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
for (const candidate of group) {
|
|
603
|
+
if (held.value.has(refKey(candidate.ref)))
|
|
604
|
+
retained++;
|
|
605
|
+
else
|
|
606
|
+
freed.push(candidate);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
// 4. One delete for the call (§11.3 step 5).
|
|
611
|
+
yield* GcSupport.deleteRefs(store, {
|
|
612
|
+
entityType: oneOrSeveral(freed.map((c) => c.entityType)),
|
|
613
|
+
attribute: oneOrSeveral(freed.map((c) => c.attribute)),
|
|
614
|
+
operation,
|
|
615
|
+
}, freed.map((c) => c.ref));
|
|
616
|
+
return {
|
|
617
|
+
records: all.length,
|
|
618
|
+
candidates: candidates.size,
|
|
619
|
+
freed: freed.length,
|
|
620
|
+
retained,
|
|
621
|
+
foreign: foreign.size,
|
|
622
|
+
skipped,
|
|
623
|
+
};
|
|
624
|
+
});
|
|
625
|
+
// -------------------------------------------------------------------------
|
|
626
|
+
// Sweeps (§11.4)
|
|
627
|
+
// -------------------------------------------------------------------------
|
|
628
|
+
/**
|
|
629
|
+
* Classify the refs the rows hold — own when they lie under `segment` —
|
|
630
|
+
* and the own prefixes to list: each `current` one, and every own family
|
|
631
|
+
* prefix under another leading prefix a held ref names.
|
|
632
|
+
*/
|
|
633
|
+
const classify = (held, segment, rest, current) => {
|
|
634
|
+
let own = 0;
|
|
635
|
+
let foreign = 0;
|
|
636
|
+
const prefixes = new Set();
|
|
637
|
+
for (const leading of current)
|
|
638
|
+
prefixes.add(`${leading}${segment}`);
|
|
639
|
+
for (const ref of held.values()) {
|
|
640
|
+
const leading = GcSupport.leadingPrefix(ref.key, segment, rest);
|
|
641
|
+
if (leading === undefined) {
|
|
642
|
+
foreign++;
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
own++;
|
|
646
|
+
prefixes.add(`${leading}${segment}`);
|
|
647
|
+
}
|
|
648
|
+
return { own, foreign, prefixes };
|
|
649
|
+
};
|
|
650
|
+
/** List `prefixes` and decide each version against `held` (§11.4 steps 2–4). */
|
|
651
|
+
const decide = (ctx, held, counts) => Effect.gen(function* () {
|
|
652
|
+
const listed = yield* GcSupport.listAll(store, ctx, counts.prefixes);
|
|
653
|
+
const now = yield* Clock.currentTimeMillis;
|
|
654
|
+
const decision = yield* GcSupport.sweepVersions({
|
|
655
|
+
store,
|
|
656
|
+
ctx,
|
|
657
|
+
listed,
|
|
658
|
+
referenced: held,
|
|
659
|
+
grace: settings.sweepGrace,
|
|
660
|
+
now,
|
|
661
|
+
});
|
|
662
|
+
return {
|
|
663
|
+
referenced: counts.own,
|
|
664
|
+
listed: listed.length,
|
|
665
|
+
marked: decision.marked,
|
|
666
|
+
deleted: decision.deleted,
|
|
667
|
+
retainedYoung: decision.retainedYoung,
|
|
668
|
+
foreign: counts.foreign,
|
|
669
|
+
busy: false,
|
|
670
|
+
};
|
|
671
|
+
});
|
|
672
|
+
const busy = (what, table, pk, sk) => Effect.as(Effect.logWarning(`effect-dynamodb: a sweep of ${what} left it untouched: its rows kept changing while ` +
|
|
673
|
+
`it read them (${GcSupport.CONFIRM_ATTEMPTS} attempts). Sweep it again later.`).pipe(Effect.annotateLogs({ table, pk, ...(sk === undefined ? {} : { sk }) })), BUSY);
|
|
674
|
+
/** Sweep an entity item family (§11.4): every range, bracketed. */
|
|
675
|
+
const sweepEntityFamily = (m, family, operation, listedPrefix) => Effect.gen(function* () {
|
|
676
|
+
const client = yield* Capacity.client;
|
|
677
|
+
const held = yield* GcSupport.confirmRead({
|
|
678
|
+
client,
|
|
679
|
+
table: m.table,
|
|
680
|
+
liveKey: {
|
|
681
|
+
[m.support.pkField]: { S: family.pk },
|
|
682
|
+
[m.support.skField]: { S: family.liveSk },
|
|
683
|
+
},
|
|
684
|
+
readRows: m.support.readFamily(client, m.table, family, GcSupport.ALL_RANGES),
|
|
685
|
+
});
|
|
686
|
+
if (Option.isNone(held)) {
|
|
687
|
+
return yield* busy(`a ${m.support.entityType} item`, m.table, family.pk, family.liveSk);
|
|
688
|
+
}
|
|
689
|
+
const segment = GcSupport.familySegment(m.table, S(family.pk), S(family.liveSk));
|
|
690
|
+
const counts = classify(held.value, segment, GcSupport.FAMILY_REST, [
|
|
691
|
+
m.offload.prefix ?? store.prefix,
|
|
692
|
+
]);
|
|
693
|
+
if (listedPrefix !== undefined)
|
|
694
|
+
counts.prefixes.add(listedPrefix);
|
|
695
|
+
return yield* decide({
|
|
696
|
+
entityType: m.support.entityType,
|
|
697
|
+
attribute: oneOrSeveral(offloadedAttributes(m.offload)),
|
|
698
|
+
operation,
|
|
699
|
+
}, held.value, counts);
|
|
700
|
+
});
|
|
701
|
+
/** Every row of a partition, one strongly consistent query (paginated). */
|
|
702
|
+
const readPartition = (client, table, pkField, pk) => Effect.gen(function* () {
|
|
703
|
+
const rows = [];
|
|
704
|
+
let start;
|
|
705
|
+
do {
|
|
706
|
+
const result = yield* client.query({
|
|
707
|
+
TableName: table,
|
|
708
|
+
KeyConditionExpression: "#pk = :pk",
|
|
709
|
+
ExpressionAttributeNames: { "#pk": pkField },
|
|
710
|
+
ExpressionAttributeValues: { ":pk": { S: pk } },
|
|
711
|
+
ConsistentRead: true,
|
|
712
|
+
ExclusiveStartKey: start,
|
|
713
|
+
});
|
|
714
|
+
rows.push(...(result.Items ?? []));
|
|
715
|
+
start = result.LastEvaluatedKey;
|
|
716
|
+
} while (start !== undefined);
|
|
717
|
+
return rows;
|
|
718
|
+
});
|
|
719
|
+
const sameRows = (a, b) => a.length === b.length &&
|
|
720
|
+
attributeValueEquals({ L: a.map((row) => ({ M: row })) }, { L: b.map((row) => ({ M: row })) });
|
|
721
|
+
/**
|
|
722
|
+
* An aggregate partition's refs, read twice: equal reads saw no
|
|
723
|
+
* transaction land in between, so no holder moved past them (another
|
|
724
|
+
* member's rows may share the partition — an entity whose lifecycle copies
|
|
725
|
+
* pointers between rows). `Option.none()` when they never agree.
|
|
726
|
+
*/
|
|
727
|
+
const readPartitionBracketed = (client, table, pkField, pk) => {
|
|
728
|
+
const attempt = (n) => n === GcSupport.CONFIRM_ATTEMPTS
|
|
729
|
+
? Effect.succeed(Option.none())
|
|
730
|
+
: Effect.gen(function* () {
|
|
731
|
+
const before = yield* readPartition(client, table, pkField, pk);
|
|
732
|
+
const after = yield* readPartition(client, table, pkField, pk);
|
|
733
|
+
if (!sameRows(before, after))
|
|
734
|
+
return yield* attempt(n + 1);
|
|
735
|
+
return Option.some(GcSupport.refsOfRows(after));
|
|
736
|
+
});
|
|
737
|
+
return attempt(0);
|
|
738
|
+
};
|
|
739
|
+
/** Sweep an aggregate's partition: every edge family under it, orphans included. */
|
|
740
|
+
const sweepAggregatePartition = (m, pk, operation) => Effect.gen(function* () {
|
|
741
|
+
const client = yield* Capacity.client;
|
|
742
|
+
const held = yield* readPartitionBracketed(client, m.table, m.support.pkField, pk);
|
|
743
|
+
if (Option.isNone(held))
|
|
744
|
+
return yield* busy(`aggregate "${m.name}"`, m.table, pk);
|
|
745
|
+
const segment = GcSupport.partitionSegment(m.table, S(pk));
|
|
746
|
+
const infos = [...m.support.edges.values()];
|
|
747
|
+
const counts = classify(held.value, segment, GcSupport.PARTITION_REST, infos.map((info) => info.prefix ?? store.prefix));
|
|
748
|
+
return yield* decide({
|
|
749
|
+
entityType: oneOrSeveral(infos.map((info) => info.entityType)),
|
|
750
|
+
attribute: oneOrSeveral(infos.flatMap((info) => offloadedAttributes(info))),
|
|
751
|
+
operation,
|
|
752
|
+
}, held.value, counts);
|
|
753
|
+
});
|
|
754
|
+
/**
|
|
755
|
+
* Sweep one aggregate edge item's family (`sweepStore`): its row, one
|
|
756
|
+
* strongly consistent read — edge rows never copy pointers, and no other
|
|
757
|
+
* row is in the family.
|
|
758
|
+
*/
|
|
759
|
+
const sweepEdgeFamily = (m, info, pk, sk, listedPrefix) => Effect.gen(function* () {
|
|
760
|
+
const client = yield* Capacity.client;
|
|
761
|
+
const result = yield* client.getItem({
|
|
762
|
+
TableName: m.table,
|
|
763
|
+
Key: { [m.support.pkField]: { S: pk }, [m.support.skField]: { S: sk } },
|
|
764
|
+
ConsistentRead: true,
|
|
765
|
+
});
|
|
766
|
+
const held = GcSupport.refsOfRows(result.Item === undefined ? [] : [result.Item]);
|
|
767
|
+
const segment = GcSupport.familySegment(m.table, S(pk), S(sk));
|
|
768
|
+
const counts = classify(held, segment, GcSupport.FAMILY_REST, [info.prefix ?? store.prefix]);
|
|
769
|
+
counts.prefixes.add(listedPrefix);
|
|
770
|
+
return yield* decide({
|
|
771
|
+
entityType: info.entityType,
|
|
772
|
+
attribute: oneOrSeveral(offloadedAttributes(info)),
|
|
773
|
+
operation: "BlobGc.sweepStore",
|
|
774
|
+
}, held, counts);
|
|
775
|
+
});
|
|
776
|
+
/**
|
|
777
|
+
* Sweep a stream's family: every row of its partition, one strongly
|
|
778
|
+
* consistent query — events are immutable and a snapshot write uploads
|
|
779
|
+
* afresh, so no holder of an existing version can appear.
|
|
780
|
+
*/
|
|
781
|
+
const sweepStreamPartition = (m, pk, operation, listedPrefix) => Effect.gen(function* () {
|
|
782
|
+
const client = yield* Capacity.client;
|
|
783
|
+
const held = GcSupport.refsOfRows(yield* readPartition(client, m.table, m.support.pkField, pk));
|
|
784
|
+
const segment = GcSupport.familySegment(m.table, S(pk), STREAM_FAMILY_SK);
|
|
785
|
+
const counts = classify(held, segment, GcSupport.FAMILY_REST, [
|
|
786
|
+
m.support.prefix ?? store.prefix,
|
|
787
|
+
]);
|
|
788
|
+
if (listedPrefix !== undefined)
|
|
789
|
+
counts.prefixes.add(listedPrefix);
|
|
790
|
+
return yield* decide({
|
|
791
|
+
entityType: oneOrSeveral([m.support.event, m.support.snapshot].flatMap((info) => info === undefined ? [] : [info.entityType])),
|
|
792
|
+
attribute: oneOrSeveral([
|
|
793
|
+
...(m.support.event === undefined ? [] : ["data"]),
|
|
794
|
+
...(m.support.snapshot === undefined ? [] : ["state"]),
|
|
795
|
+
]),
|
|
796
|
+
operation,
|
|
797
|
+
}, held, counts);
|
|
798
|
+
});
|
|
799
|
+
const unknownMember = (operation, kind, member) => Effect.die(invalid(operation, `${show(member)} is not ${kind} member of this collector.`));
|
|
800
|
+
const sweep = (member, key) => Effect.gen(function* () {
|
|
801
|
+
const operation = "BlobGc.sweep";
|
|
802
|
+
yield* requireCapabilities(operation, ["list", "marks"]);
|
|
803
|
+
const m = settings.entities.find((entry) => entry.name === member);
|
|
804
|
+
if (m === undefined)
|
|
805
|
+
return yield* unknownMember(operation, "an entity", member);
|
|
806
|
+
const family = yield* m.support.familyOfKey(key, operation);
|
|
807
|
+
return yield* sweepEntityFamily(m, family, operation);
|
|
808
|
+
});
|
|
809
|
+
const sweepPartition = (member, key) => Effect.gen(function* () {
|
|
810
|
+
const operation = "BlobGc.sweepPartition";
|
|
811
|
+
yield* requireCapabilities(operation, ["list", "marks"]);
|
|
812
|
+
const aggregate = settings.aggregates.find((entry) => entry.name === member);
|
|
813
|
+
if (aggregate !== undefined) {
|
|
814
|
+
const pk = yield* aggregate.support.partitionOfKey(key, operation);
|
|
815
|
+
return yield* sweepAggregatePartition(aggregate, pk, operation);
|
|
816
|
+
}
|
|
817
|
+
const stream = settings.streams.find((entry) => entry.name === member);
|
|
818
|
+
if (stream === undefined) {
|
|
819
|
+
return yield* unknownMember(operation, "an aggregate or stream", member);
|
|
820
|
+
}
|
|
821
|
+
const pk = yield* stream.support.partitionOfKey(key, operation);
|
|
822
|
+
return yield* sweepStreamPartition(stream, pk, operation);
|
|
823
|
+
});
|
|
824
|
+
/** `startAfter` for the keys after every key of a family prefix (`…P/F/` → `…P/F0`). */
|
|
825
|
+
const afterFamily = (familyPrefix) => familyPrefix.endsWith("/") ? `${familyPrefix.slice(0, -1)}0` : familyPrefix;
|
|
826
|
+
/** The store's prefix and every member's `offload.prefix`, in UTF-8 order. */
|
|
827
|
+
const knownPrefixes = (() => {
|
|
828
|
+
const all = new Set([store.prefix]);
|
|
829
|
+
for (const m of settings.entities)
|
|
830
|
+
all.add(m.offload.prefix ?? store.prefix);
|
|
831
|
+
for (const m of settings.aggregates) {
|
|
832
|
+
for (const info of m.support.edges.values())
|
|
833
|
+
all.add(info.prefix ?? store.prefix);
|
|
834
|
+
}
|
|
835
|
+
for (const m of settings.streams)
|
|
836
|
+
all.add(m.support.prefix ?? store.prefix);
|
|
837
|
+
return [...all].sort(compareUtf8);
|
|
838
|
+
})();
|
|
839
|
+
/** The prefixes to list by default: the known ones, none under another. */
|
|
840
|
+
const defaultRoots = () => knownPrefixes.filter((root) => !knownPrefixes.some((other) => other !== root && root.startsWith(other)));
|
|
841
|
+
/** The library families listed under `root`, resuming after `marker`. */
|
|
842
|
+
const familiesUnder = (root, marker, ctx) => {
|
|
843
|
+
let startAfter;
|
|
844
|
+
if (marker !== undefined && compareUtf8(marker, root) >= 0) {
|
|
845
|
+
if (!marker.startsWith(root))
|
|
846
|
+
return Stream.empty;
|
|
847
|
+
startAfter = marker;
|
|
848
|
+
}
|
|
849
|
+
const known = [...knownPrefixes, root];
|
|
850
|
+
const familyOf = (key) => GcSupport.listedFamily(key, root.length, known);
|
|
851
|
+
return GcSupport.listing(store, ctx, root, startAfter).pipe(Stream.groupAdjacentBy((version) => familyOf(version.ref.key)?.familyPrefix ?? ""), Stream.filter(([familyPrefix]) => familyPrefix !== ""), Stream.map(([, versions]) => ({ listed: familyOf(versions[0].ref.key), versions })));
|
|
852
|
+
};
|
|
853
|
+
/** The metadata of one of the family's versions; `undefined` when every one is gone. */
|
|
854
|
+
const headOf = (versions, ctx) => Effect.gen(function* () {
|
|
855
|
+
const head = store.head;
|
|
856
|
+
for (const version of versions) {
|
|
857
|
+
const metadata = yield* call(store, "head", ctx, { ref: version.ref }, () => head(version.ref)).pipe(Effect.map((found) => found), Effect.catchIf((error) => error.reason === "NotFound", () => Effect.succeed(undefined)));
|
|
858
|
+
if (metadata !== undefined)
|
|
859
|
+
return metadata;
|
|
860
|
+
}
|
|
861
|
+
return undefined;
|
|
862
|
+
});
|
|
863
|
+
/** Attribute a listed family to a member and sweep it (§11.5 steps 2–3). */
|
|
864
|
+
const sweepListed = (group, ctx) => Effect.gen(function* () {
|
|
865
|
+
const { listed, versions } = group;
|
|
866
|
+
const familyPrefix = listed.familyPrefix;
|
|
867
|
+
const foreign = (table) => ({
|
|
868
|
+
_tag: "Foreign",
|
|
869
|
+
familyPrefix,
|
|
870
|
+
table,
|
|
871
|
+
});
|
|
872
|
+
const metadata = yield* headOf(versions, ctx);
|
|
873
|
+
if (metadata === undefined)
|
|
874
|
+
return undefined;
|
|
875
|
+
const parsed = BlobMetadata.parse(metadata);
|
|
876
|
+
if (typeof parsed === "string")
|
|
877
|
+
return foreign(Option.none());
|
|
878
|
+
const table = Option.some(parsed.table);
|
|
879
|
+
const entityMembers = settings.entities.filter((m) => m.table === parsed.table && m.support.entityType === parsed.entity);
|
|
880
|
+
const aggregateMembers = settings.aggregates.filter((m) => m.table === parsed.table && m.support.edges.has(parsed.entity));
|
|
881
|
+
const streamMembers = settings.streams.filter((m) => m.table === parsed.table &&
|
|
882
|
+
((parsed.entity === m.support.eventType && m.support.event !== undefined) ||
|
|
883
|
+
(parsed.entity === m.support.snapshotType && m.support.snapshot !== undefined)));
|
|
884
|
+
if (entityMembers.length + aggregateMembers.length + streamMembers.length === 0) {
|
|
885
|
+
return foreign(table);
|
|
886
|
+
}
|
|
887
|
+
if (parsed.item === undefined) {
|
|
888
|
+
return {
|
|
889
|
+
_tag: "Unattributable",
|
|
890
|
+
familyPrefix,
|
|
891
|
+
versions: versions.length,
|
|
892
|
+
bytes: versions.reduce((sum, version) => sum + version.size, 0),
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
const pk = "S" in parsed.item.pk ? parsed.item.pk.S : undefined;
|
|
896
|
+
const rowSk = "S" in parsed.item.sk ? parsed.item.sk.S : undefined;
|
|
897
|
+
if (pk === undefined || rowSk === undefined)
|
|
898
|
+
return foreign(table);
|
|
899
|
+
const partition = partitionHash(parsed.table, S(pk));
|
|
900
|
+
if (partition !== listed.partition)
|
|
901
|
+
return foreign(table);
|
|
902
|
+
const swept = (member, report) => ({
|
|
903
|
+
_tag: "Swept",
|
|
904
|
+
familyPrefix,
|
|
905
|
+
member,
|
|
906
|
+
report,
|
|
907
|
+
});
|
|
908
|
+
for (const m of entityMembers) {
|
|
909
|
+
const liveSk = m.support
|
|
910
|
+
.liveSkCandidates(rowSk)
|
|
911
|
+
.find((candidate) => familyHash(partition, S(candidate)) === listed.family);
|
|
912
|
+
if (liveSk === undefined)
|
|
913
|
+
continue;
|
|
914
|
+
return swept(m.name, yield* sweepEntityFamily(m, { pk, liveSk }, "BlobGc.sweepStore", familyPrefix));
|
|
915
|
+
}
|
|
916
|
+
if (familyHash(partition, S(rowSk)) === listed.family) {
|
|
917
|
+
for (const m of aggregateMembers) {
|
|
918
|
+
if (!m.support.ownsPartition(pk))
|
|
919
|
+
continue;
|
|
920
|
+
const info = m.support.edges.get(parsed.entity);
|
|
921
|
+
return swept(m.name, yield* sweepEdgeFamily(m, info, pk, rowSk, familyPrefix));
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
if (familyHash(partition, STREAM_FAMILY_SK) === listed.family) {
|
|
925
|
+
for (const m of streamMembers) {
|
|
926
|
+
return swept(m.name, yield* sweepStreamPartition(m, pk, "BlobGc.sweepStore", familyPrefix));
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
return foreign(table);
|
|
930
|
+
});
|
|
931
|
+
const sweepStore = (options) => Stream.unwrap(Effect.gen(function* () {
|
|
932
|
+
const operation = "BlobGc.sweepStore";
|
|
933
|
+
yield* requireCapabilities(operation, ["list", "head", "marks"]);
|
|
934
|
+
try {
|
|
935
|
+
checkKeys(operation, "options", options, SWEEP_STORE_KEYS);
|
|
936
|
+
}
|
|
937
|
+
catch (error) {
|
|
938
|
+
return yield* Effect.die(error);
|
|
939
|
+
}
|
|
940
|
+
const prefix = options?.prefix;
|
|
941
|
+
const startAfter = options?.startAfter;
|
|
942
|
+
const maxFamilies = options?.maxFamilies;
|
|
943
|
+
if (prefix !== undefined && typeof prefix !== "string") {
|
|
944
|
+
return yield* Effect.die(invalid(operation, `prefix must be a string, got ${show(prefix)}.`));
|
|
945
|
+
}
|
|
946
|
+
if (startAfter !== undefined && typeof startAfter !== "string") {
|
|
947
|
+
return yield* Effect.die(invalid(operation, `startAfter must be a string, got ${show(startAfter)}.`));
|
|
948
|
+
}
|
|
949
|
+
if (maxFamilies !== undefined &&
|
|
950
|
+
(typeof maxFamilies !== "number" || !Number.isSafeInteger(maxFamilies) || maxFamilies < 1)) {
|
|
951
|
+
return yield* Effect.die(invalid(operation, `maxFamilies must be a positive integer, got ${show(maxFamilies)}.`));
|
|
952
|
+
}
|
|
953
|
+
const ctx = { entityType: "BlobGc", attribute: "(several)", operation };
|
|
954
|
+
const marker = startAfter === undefined ? undefined : afterFamily(startAfter);
|
|
955
|
+
const roots = prefix === undefined ? defaultRoots() : [prefix];
|
|
956
|
+
const families = Stream.fromIterable(roots).pipe(Stream.flatMap((root) => familiesUnder(root, marker, ctx)), Stream.mapEffect((group) => sweepListed(group, ctx)), Stream.filter((family) => family !== undefined));
|
|
957
|
+
return maxFamilies === undefined ? families : Stream.take(families, maxFamilies);
|
|
958
|
+
}));
|
|
959
|
+
return { reap, sweep, sweepPartition, sweepStore };
|
|
960
|
+
};
|
|
961
|
+
//# sourceMappingURL=BlobGc.js.map
|