@ipab/ipfs-store 0.1.0-rc.4 → 0.1.0-rc.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -17
- package/dist/dag.d.ts.map +1 -1
- package/dist/dag.js +8 -2
- package/dist/dag.js.map +1 -1
- package/dist/errors.d.ts +2 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/retention.d.ts +27 -0
- package/dist/retention.d.ts.map +1 -1
- package/dist/retention.js +296 -72
- package/dist/retention.js.map +1 -1
- package/dist/store.d.ts +18 -0
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +57 -2
- package/dist/store.js.map +1 -1
- package/package.json +1 -1
- package/src/dag.ts +13 -2
- package/src/errors.ts +1 -0
- package/src/index.ts +5 -1
- package/src/retention.ts +397 -80
- package/src/store.ts +139 -16
package/src/retention.ts
CHANGED
|
@@ -1,20 +1,45 @@
|
|
|
1
1
|
import { mkdirSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
3
|
import type { DatabaseSync } from "node:sqlite";
|
|
4
4
|
import type * as sqlite from "node:sqlite";
|
|
5
5
|
import { createRequire } from "node:module";
|
|
6
6
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
7
7
|
import { CID } from "multiformats/cid";
|
|
8
|
-
import { ContentError } from "./errors.js";
|
|
8
|
+
import { abortError, ContentError } from "./errors.js";
|
|
9
|
+
|
|
10
|
+
const coordinationQueues = new Map<string, Promise<void>>();
|
|
11
|
+
|
|
12
|
+
function queueKeyFor(mutexPath: string): string {
|
|
13
|
+
return resolve(mutexPath);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function waitForTurn(
|
|
17
|
+
previous: Promise<void>,
|
|
18
|
+
signal: AbortSignal | undefined,
|
|
19
|
+
): Promise<void> {
|
|
20
|
+
if (!signal) return previous;
|
|
21
|
+
return new Promise<void>((resolveTurn, rejectTurn) => {
|
|
22
|
+
const onAbort = () => {
|
|
23
|
+
rejectTurn(abortError(signal));
|
|
24
|
+
};
|
|
25
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
26
|
+
void previous.then(() => {
|
|
27
|
+
signal.removeEventListener("abort", onAbort);
|
|
28
|
+
resolveTurn();
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
}
|
|
9
32
|
|
|
10
33
|
interface RetentionPort {
|
|
11
34
|
prepare(root: string, signal?: AbortSignal): Promise<void>;
|
|
35
|
+
prepareDirect?: (root: string, signal?: AbortSignal) => Promise<void>;
|
|
12
36
|
}
|
|
13
37
|
/** The separate SQLite mutex preserves durable intents during content acquisition. */
|
|
14
38
|
export class Retention {
|
|
15
39
|
private readonly Database: typeof DatabaseSync;
|
|
16
40
|
private readonly db: DatabaseSync;
|
|
17
41
|
private readonly mutexPath: string;
|
|
42
|
+
private readonly queueKey: string;
|
|
18
43
|
private readonly pending = new Set<Promise<unknown>>();
|
|
19
44
|
private closed = false;
|
|
20
45
|
constructor(
|
|
@@ -27,21 +52,29 @@ export class Retention {
|
|
|
27
52
|
mkdirSync(directory, { recursive: true });
|
|
28
53
|
this.db = new this.Database(join(directory, "retention.sqlite"));
|
|
29
54
|
this.mutexPath = join(directory, "mutex.sqlite");
|
|
55
|
+
this.queueKey = queueKeyFor(this.mutexPath);
|
|
30
56
|
this.db.exec(`PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL;
|
|
31
57
|
CREATE TABLE IF NOT EXISTS owners (id TEXT PRIMARY KEY, roots TEXT NOT NULL);
|
|
32
58
|
CREATE TABLE IF NOT EXISTS stages (id TEXT NOT NULL, cid TEXT NOT NULL, PRIMARY KEY(id,cid));
|
|
33
59
|
CREATE TABLE IF NOT EXISTS intents (
|
|
34
60
|
id TEXT NOT NULL, cid TEXT NOT NULL, count INTEGER NOT NULL CHECK(count>0), PRIMARY KEY(id,cid));
|
|
35
|
-
CREATE TABLE IF NOT EXISTS pins (cid TEXT PRIMARY KEY, phase TEXT NOT NULL)
|
|
61
|
+
CREATE TABLE IF NOT EXISTS pins (cid TEXT PRIMARY KEY, phase TEXT NOT NULL);
|
|
62
|
+
CREATE TABLE IF NOT EXISTS direct_owners (id TEXT PRIMARY KEY, roots TEXT NOT NULL);
|
|
63
|
+
CREATE TABLE IF NOT EXISTS direct_intents (
|
|
64
|
+
id TEXT NOT NULL, cid TEXT NOT NULL, count INTEGER NOT NULL CHECK(count>0), PRIMARY KEY(id,cid));
|
|
65
|
+
CREATE TABLE IF NOT EXISTS direct_pins (cid TEXT PRIMARY KEY, phase TEXT NOT NULL);`);
|
|
36
66
|
}
|
|
37
67
|
private roots(values: readonly string[]): string[] {
|
|
38
68
|
return [
|
|
39
69
|
...new Set(values.map((value) => CID.parse(value).toV1().toString())),
|
|
40
70
|
].sort();
|
|
41
71
|
}
|
|
42
|
-
private readOwner(
|
|
72
|
+
private readOwner(
|
|
73
|
+
owner: string,
|
|
74
|
+
table: "owners" | "direct_owners" = "owners",
|
|
75
|
+
): string[] {
|
|
43
76
|
const row = this.db
|
|
44
|
-
.prepare(
|
|
77
|
+
.prepare(`SELECT roots FROM ${table} WHERE id=?`)
|
|
45
78
|
.get(owner);
|
|
46
79
|
if (!row) return [];
|
|
47
80
|
const roots: unknown = JSON.parse(String(row.roots));
|
|
@@ -53,17 +86,18 @@ export class Retention {
|
|
|
53
86
|
owner: string,
|
|
54
87
|
roots: readonly string[],
|
|
55
88
|
replace = false,
|
|
89
|
+
table: "owners" | "direct_owners" = "owners",
|
|
56
90
|
): void {
|
|
57
91
|
if (typeof owner !== "string" || !owner || owner.length > 4096)
|
|
58
92
|
throw new Error("A stable retention owner is required.");
|
|
59
93
|
const values = this.roots(
|
|
60
|
-
replace ? roots : [...this.readOwner(owner), ...roots],
|
|
94
|
+
replace ? roots : [...this.readOwner(owner, table), ...roots],
|
|
61
95
|
);
|
|
62
96
|
this.db
|
|
63
97
|
.prepare(
|
|
64
|
-
`INSERT INTO
|
|
98
|
+
`INSERT INTO ${table} VALUES (?,?)
|
|
65
99
|
ON CONFLICT(id) DO UPDATE SET roots=excluded.roots
|
|
66
|
-
WHERE
|
|
100
|
+
WHERE ${table}.roots<>excluded.roots`,
|
|
67
101
|
)
|
|
68
102
|
.run(owner, JSON.stringify(values));
|
|
69
103
|
}
|
|
@@ -83,6 +117,34 @@ export class Retention {
|
|
|
83
117
|
mutex.exec("BEGIN IMMEDIATE");
|
|
84
118
|
}
|
|
85
119
|
register(owner: string, roots: readonly string[]): void {
|
|
120
|
+
this.registerInTable(owner, roots, "owners");
|
|
121
|
+
}
|
|
122
|
+
registerDirect(owner: string, roots: readonly string[]): void {
|
|
123
|
+
this.registerInTable(owner, roots, "direct_owners");
|
|
124
|
+
}
|
|
125
|
+
registerAsync(
|
|
126
|
+
owner: string,
|
|
127
|
+
roots: readonly string[],
|
|
128
|
+
signal?: AbortSignal,
|
|
129
|
+
): Promise<void> {
|
|
130
|
+
return this.locked(signal, () => {
|
|
131
|
+
this.publish(owner, roots, false, "owners");
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
registerDirectAsync(
|
|
135
|
+
owner: string,
|
|
136
|
+
roots: readonly string[],
|
|
137
|
+
signal?: AbortSignal,
|
|
138
|
+
): Promise<void> {
|
|
139
|
+
return this.locked(signal, () => {
|
|
140
|
+
this.publish(owner, roots, false, "direct_owners");
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
private registerInTable(
|
|
144
|
+
owner: string,
|
|
145
|
+
roots: readonly string[],
|
|
146
|
+
table: "owners" | "direct_owners",
|
|
147
|
+
): void {
|
|
86
148
|
const mutex = new this.Database(this.mutexPath);
|
|
87
149
|
mutex.exec("PRAGMA busy_timeout=0");
|
|
88
150
|
try {
|
|
@@ -95,7 +157,7 @@ export class Retention {
|
|
|
95
157
|
);
|
|
96
158
|
}
|
|
97
159
|
try {
|
|
98
|
-
this.publish(owner, roots);
|
|
160
|
+
this.publish(owner, roots, false, table);
|
|
99
161
|
} finally {
|
|
100
162
|
mutex.exec("ROLLBACK");
|
|
101
163
|
mutex.close();
|
|
@@ -116,56 +178,84 @@ export class Retention {
|
|
|
116
178
|
signal: AbortSignal | undefined,
|
|
117
179
|
action: () => Promise<T> | T,
|
|
118
180
|
): Promise<T> {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
Date.now() >= deadline
|
|
132
|
-
)
|
|
133
|
-
throw new ContentError(
|
|
134
|
-
"retention_unfinished",
|
|
135
|
-
"Retention coordination is unavailable.",
|
|
136
|
-
);
|
|
137
|
-
await sleep(10, undefined, signal ? { signal } : {});
|
|
138
|
-
}
|
|
181
|
+
signal?.throwIfAborted();
|
|
182
|
+
const previous = coordinationQueues.get(this.queueKey) ?? Promise.resolve();
|
|
183
|
+
let releaseTurn!: () => void;
|
|
184
|
+
const current = new Promise<void>((resolveTurn) => {
|
|
185
|
+
releaseTurn = resolveTurn;
|
|
186
|
+
});
|
|
187
|
+
const tail = previous.then(() => current);
|
|
188
|
+
coordinationQueues.set(this.queueKey, tail);
|
|
189
|
+
const release = () => {
|
|
190
|
+
releaseTurn();
|
|
191
|
+
if (coordinationQueues.get(this.queueKey) === tail) {
|
|
192
|
+
coordinationQueues.delete(this.queueKey);
|
|
139
193
|
}
|
|
194
|
+
};
|
|
195
|
+
try {
|
|
196
|
+
await waitForTurn(previous, signal);
|
|
140
197
|
} catch (error) {
|
|
141
|
-
|
|
142
|
-
signal?.throwIfAborted();
|
|
198
|
+
release();
|
|
143
199
|
throw error;
|
|
144
200
|
}
|
|
145
201
|
try {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
202
|
+
const mutex = new this.Database(this.mutexPath);
|
|
203
|
+
mutex.exec("PRAGMA busy_timeout=0");
|
|
204
|
+
const deadline = Date.now() + 30_000;
|
|
205
|
+
try {
|
|
206
|
+
for (;;) {
|
|
207
|
+
signal?.throwIfAborted();
|
|
208
|
+
try {
|
|
209
|
+
this.begin(mutex);
|
|
210
|
+
break;
|
|
211
|
+
} catch (error) {
|
|
212
|
+
if (
|
|
213
|
+
(error as { errcode?: number }).errcode !== 5 ||
|
|
214
|
+
Date.now() >= deadline
|
|
215
|
+
)
|
|
216
|
+
throw new ContentError(
|
|
217
|
+
"retention_unfinished",
|
|
218
|
+
"Retention coordination is unavailable.",
|
|
219
|
+
);
|
|
220
|
+
await sleep(10, undefined, signal ? { signal } : {});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
} catch (error) {
|
|
224
|
+
mutex.close();
|
|
225
|
+
signal?.throwIfAborted();
|
|
226
|
+
throw error;
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
return await action();
|
|
230
|
+
} catch (error) {
|
|
231
|
+
if (signal?.aborted) throw signal.reason;
|
|
232
|
+
throw new ContentError(
|
|
233
|
+
"retention_unfinished",
|
|
234
|
+
`Retention hints are unfinished; durable intents remain for retry. ${
|
|
235
|
+
error instanceof ContentError
|
|
236
|
+
? `${error.code}: ${error.message}`
|
|
237
|
+
: error instanceof Error &&
|
|
238
|
+
["TimeoutError", "AbortError"].includes(error.name)
|
|
239
|
+
? error.name
|
|
240
|
+
: "verification_or_storage_failure"
|
|
241
|
+
}`,
|
|
242
|
+
{ cause: error },
|
|
243
|
+
);
|
|
244
|
+
} finally {
|
|
245
|
+
mutex.exec("ROLLBACK");
|
|
246
|
+
mutex.close();
|
|
247
|
+
}
|
|
161
248
|
} finally {
|
|
162
|
-
|
|
163
|
-
mutex.close();
|
|
249
|
+
release();
|
|
164
250
|
}
|
|
165
251
|
}
|
|
166
|
-
private phase(
|
|
252
|
+
private phase(
|
|
253
|
+
root: string,
|
|
254
|
+
phase: string,
|
|
255
|
+
table: "pins" | "direct_pins" = "pins",
|
|
256
|
+
): void {
|
|
167
257
|
this.db
|
|
168
|
-
.prepare(
|
|
258
|
+
.prepare(`INSERT OR REPLACE INTO ${table} VALUES (?,?)`)
|
|
169
259
|
.run(root, phase);
|
|
170
260
|
}
|
|
171
261
|
private async ensure(root: string, signal?: AbortSignal): Promise<void> {
|
|
@@ -173,10 +263,24 @@ export class Retention {
|
|
|
173
263
|
await this.port.prepare(root, signal);
|
|
174
264
|
this.phase(root, "pinned");
|
|
175
265
|
}
|
|
266
|
+
private async ensureDirect(
|
|
267
|
+
root: string,
|
|
268
|
+
signal?: AbortSignal,
|
|
269
|
+
): Promise<void> {
|
|
270
|
+
this.phase(root, "pin-pending", "direct_pins");
|
|
271
|
+
await (this.port.prepareDirect
|
|
272
|
+
? this.port.prepareDirect(root, signal)
|
|
273
|
+
: this.port.prepare(root, signal));
|
|
274
|
+
this.phase(root, "pinned", "direct_pins");
|
|
275
|
+
}
|
|
176
276
|
pin(root: string, signal?: AbortSignal): Promise<void> {
|
|
177
277
|
root = this.roots([root])[0]!;
|
|
178
278
|
return this.locked(signal, () => this.ensure(root, signal));
|
|
179
279
|
}
|
|
280
|
+
pinDirect(root: string, signal?: AbortSignal): Promise<void> {
|
|
281
|
+
root = this.roots([root])[0]!;
|
|
282
|
+
return this.locked(signal, () => this.ensureDirect(root, signal));
|
|
283
|
+
}
|
|
180
284
|
retain(
|
|
181
285
|
owner: string,
|
|
182
286
|
roots: readonly string[],
|
|
@@ -190,34 +294,82 @@ export class Retention {
|
|
|
190
294
|
for (const root of this.roots(roots)) await this.ensure(root, signal);
|
|
191
295
|
});
|
|
192
296
|
}
|
|
297
|
+
retainDirect(
|
|
298
|
+
owner: string,
|
|
299
|
+
roots: readonly string[],
|
|
300
|
+
signal?: AbortSignal,
|
|
301
|
+
): Promise<void> {
|
|
302
|
+
return this.locked(signal, async () => {
|
|
303
|
+
this.transaction(() => {
|
|
304
|
+
this.publish(owner, roots, false, "direct_owners");
|
|
305
|
+
for (const root of this.roots(roots))
|
|
306
|
+
this.intent(owner, root, "direct_intents");
|
|
307
|
+
});
|
|
308
|
+
for (const root of this.roots(roots))
|
|
309
|
+
await this.ensureDirect(root, signal);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
193
312
|
confirm(
|
|
194
313
|
owner: string,
|
|
195
314
|
roots: readonly string[],
|
|
196
315
|
signal?: AbortSignal,
|
|
316
|
+
): Promise<void> {
|
|
317
|
+
return this.confirmInTable(owner, roots, "owners", "intents", signal);
|
|
318
|
+
}
|
|
319
|
+
confirmDirect(
|
|
320
|
+
owner: string,
|
|
321
|
+
roots: readonly string[],
|
|
322
|
+
signal?: AbortSignal,
|
|
323
|
+
): Promise<void> {
|
|
324
|
+
return this.confirmInTable(
|
|
325
|
+
owner,
|
|
326
|
+
roots,
|
|
327
|
+
"direct_owners",
|
|
328
|
+
"direct_intents",
|
|
329
|
+
signal,
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
private decrementIntent(
|
|
333
|
+
owner: string,
|
|
334
|
+
root: string,
|
|
335
|
+
intentTable: "intents" | "direct_intents" = "intents",
|
|
336
|
+
): void {
|
|
337
|
+
const row = this.db
|
|
338
|
+
.prepare(`SELECT count FROM ${intentTable} WHERE id=? AND cid=?`)
|
|
339
|
+
.get(owner, root);
|
|
340
|
+
if (row && Number(row.count) > 1)
|
|
341
|
+
this.db
|
|
342
|
+
.prepare(`UPDATE ${intentTable} SET count=count-1 WHERE id=? AND cid=?`)
|
|
343
|
+
.run(owner, root);
|
|
344
|
+
else
|
|
345
|
+
this.db
|
|
346
|
+
.prepare(`DELETE FROM ${intentTable} WHERE id=? AND cid=?`)
|
|
347
|
+
.run(owner, root);
|
|
348
|
+
}
|
|
349
|
+
private confirmInTable(
|
|
350
|
+
owner: string,
|
|
351
|
+
roots: readonly string[],
|
|
352
|
+
ownerTable: "owners" | "direct_owners",
|
|
353
|
+
intentTable: "intents" | "direct_intents",
|
|
354
|
+
signal?: AbortSignal,
|
|
197
355
|
): Promise<void> {
|
|
198
356
|
return this.locked(signal, () => {
|
|
199
357
|
this.transaction(() => {
|
|
200
|
-
this.publish(owner, roots);
|
|
358
|
+
this.publish(owner, roots, false, ownerTable);
|
|
201
359
|
for (const root of this.roots(roots)) {
|
|
202
|
-
|
|
203
|
-
.prepare("SELECT count FROM intents WHERE id=? AND cid=?")
|
|
204
|
-
.get(owner, root);
|
|
205
|
-
if (row && Number(row.count) > 1)
|
|
206
|
-
this.db
|
|
207
|
-
.prepare("UPDATE intents SET count=count-1 WHERE id=? AND cid=?")
|
|
208
|
-
.run(owner, root);
|
|
209
|
-
else
|
|
210
|
-
this.db
|
|
211
|
-
.prepare("DELETE FROM intents WHERE id=? AND cid=?")
|
|
212
|
-
.run(owner, root);
|
|
360
|
+
this.decrementIntent(owner, root, intentTable);
|
|
213
361
|
}
|
|
214
362
|
});
|
|
215
363
|
});
|
|
216
364
|
}
|
|
217
|
-
private intent(
|
|
365
|
+
private intent(
|
|
366
|
+
owner: string,
|
|
367
|
+
root: string,
|
|
368
|
+
table: "intents" | "direct_intents" = "intents",
|
|
369
|
+
): void {
|
|
218
370
|
this.db
|
|
219
371
|
.prepare(
|
|
220
|
-
`INSERT INTO
|
|
372
|
+
`INSERT INTO ${table} VALUES (?,?,1)
|
|
221
373
|
ON CONFLICT(id,cid) DO UPDATE SET count=count+1`,
|
|
222
374
|
)
|
|
223
375
|
.run(owner, root);
|
|
@@ -252,35 +404,119 @@ export class Retention {
|
|
|
252
404
|
});
|
|
253
405
|
});
|
|
254
406
|
}
|
|
407
|
+
private async stageEntries(
|
|
408
|
+
session: string,
|
|
409
|
+
entries: readonly { root: string; write: () => Promise<unknown> }[],
|
|
410
|
+
signal?: AbortSignal,
|
|
411
|
+
): Promise<void> {
|
|
412
|
+
if (entries.length === 0) return;
|
|
413
|
+
const uniqueRoots = [...new Set(entries.map((entry) => entry.root))];
|
|
414
|
+
const alreadyPinned = new Set<string>();
|
|
415
|
+
for (const root of uniqueRoots) {
|
|
416
|
+
if (
|
|
417
|
+
this.db.prepare("SELECT phase FROM pins WHERE cid=?").get(root)
|
|
418
|
+
?.phase === "pinned"
|
|
419
|
+
) {
|
|
420
|
+
alreadyPinned.add(root);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
this.transaction(() => {
|
|
424
|
+
const insertStage = this.db.prepare(
|
|
425
|
+
"INSERT OR IGNORE INTO stages VALUES (?,?)",
|
|
426
|
+
);
|
|
427
|
+
for (const root of uniqueRoots) {
|
|
428
|
+
insertStage.run(session, root);
|
|
429
|
+
this.phase(root, "stage-pending");
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
const written = new Set<string>();
|
|
433
|
+
let failedRoot: string | undefined;
|
|
434
|
+
try {
|
|
435
|
+
for (const entry of entries) {
|
|
436
|
+
signal?.throwIfAborted();
|
|
437
|
+
failedRoot = entry.root;
|
|
438
|
+
written.delete(entry.root);
|
|
439
|
+
await entry.write();
|
|
440
|
+
written.add(entry.root);
|
|
441
|
+
failedRoot = undefined;
|
|
442
|
+
}
|
|
443
|
+
} catch (error) {
|
|
444
|
+
this.transaction(() => {
|
|
445
|
+
for (const root of written) {
|
|
446
|
+
this.phase(root, alreadyPinned.has(root) ? "pinned" : "staged");
|
|
447
|
+
}
|
|
448
|
+
for (const root of alreadyPinned) {
|
|
449
|
+
if (!written.has(root) && root !== failedRoot) {
|
|
450
|
+
this.phase(root, "pinned");
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
throw error;
|
|
455
|
+
}
|
|
456
|
+
this.transaction(() => {
|
|
457
|
+
for (const root of uniqueRoots) {
|
|
458
|
+
this.phase(root, alreadyPinned.has(root) ? "pinned" : "staged");
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
}
|
|
255
462
|
stage(
|
|
256
463
|
session: string,
|
|
257
464
|
root: string,
|
|
258
465
|
write: () => Promise<unknown>,
|
|
259
466
|
signal?: AbortSignal,
|
|
467
|
+
): Promise<void> {
|
|
468
|
+
root = this.roots([root])[0]!;
|
|
469
|
+
return this.locked(signal, () =>
|
|
470
|
+
this.stageEntries(session, [{ root, write }], signal),
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
stageBatch(
|
|
474
|
+
session: string,
|
|
475
|
+
entries: readonly { cid: string; write: () => Promise<unknown> }[],
|
|
476
|
+
signal?: AbortSignal,
|
|
477
|
+
): Promise<void> {
|
|
478
|
+
const normalized = entries.map((entry) => ({
|
|
479
|
+
root: this.roots([entry.cid])[0]!,
|
|
480
|
+
write: entry.write,
|
|
481
|
+
}));
|
|
482
|
+
return this.locked(signal, () =>
|
|
483
|
+
this.stageEntries(session, normalized, signal),
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
finish(
|
|
487
|
+
session: string,
|
|
488
|
+
root: string,
|
|
489
|
+
owner?: string,
|
|
490
|
+
signal?: AbortSignal,
|
|
260
491
|
): Promise<void> {
|
|
261
492
|
root = this.roots([root])[0]!;
|
|
262
493
|
return this.locked(signal, async () => {
|
|
263
|
-
|
|
264
|
-
this.
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
});
|
|
272
|
-
await write();
|
|
273
|
-
this.phase(root, alreadyPinned ? "pinned" : "staged");
|
|
494
|
+
if (owner) {
|
|
495
|
+
this.transaction(() => {
|
|
496
|
+
this.publish(owner, [root]);
|
|
497
|
+
this.intent(owner, root);
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
await this.ensure(root, signal);
|
|
501
|
+
this.db.prepare("DELETE FROM stages WHERE id=?").run(session);
|
|
274
502
|
});
|
|
275
503
|
}
|
|
276
|
-
|
|
504
|
+
importBatch(
|
|
277
505
|
session: string,
|
|
506
|
+
entries: readonly { cid: string; write: () => Promise<unknown> }[],
|
|
278
507
|
root: string,
|
|
279
508
|
owner?: string,
|
|
280
509
|
signal?: AbortSignal,
|
|
510
|
+
onRetained?: () => Promise<void> | void,
|
|
281
511
|
): Promise<void> {
|
|
282
512
|
root = this.roots([root])[0]!;
|
|
513
|
+
const normalized = entries.map((entry) => ({
|
|
514
|
+
root: this.roots([entry.cid])[0]!,
|
|
515
|
+
write: entry.write,
|
|
516
|
+
}));
|
|
283
517
|
return this.locked(signal, async () => {
|
|
518
|
+
await this.stageEntries(session, normalized, signal);
|
|
519
|
+
signal?.throwIfAborted();
|
|
284
520
|
if (owner) {
|
|
285
521
|
this.transaction(() => {
|
|
286
522
|
this.publish(owner, [root]);
|
|
@@ -289,16 +525,27 @@ export class Retention {
|
|
|
289
525
|
}
|
|
290
526
|
await this.ensure(root, signal);
|
|
291
527
|
this.db.prepare("DELETE FROM stages WHERE id=?").run(session);
|
|
528
|
+
if (owner && onRetained) {
|
|
529
|
+
signal?.throwIfAborted();
|
|
530
|
+
await onRetained();
|
|
531
|
+
this.transaction(() => {
|
|
532
|
+
this.decrementIntent(owner, root);
|
|
533
|
+
});
|
|
534
|
+
}
|
|
292
535
|
});
|
|
293
536
|
}
|
|
294
|
-
private remove(
|
|
537
|
+
private remove(
|
|
538
|
+
root: string,
|
|
539
|
+
signal?: AbortSignal,
|
|
540
|
+
table: "pins" | "direct_pins" = "pins",
|
|
541
|
+
): boolean {
|
|
295
542
|
const record = this.db
|
|
296
|
-
.prepare(
|
|
543
|
+
.prepare(`SELECT phase FROM ${table} WHERE cid=?`)
|
|
297
544
|
.get(root);
|
|
298
545
|
if (!record) return false;
|
|
299
|
-
this.phase(root, "unpin-pending");
|
|
546
|
+
this.phase(root, "unpin-pending", table);
|
|
300
547
|
signal?.throwIfAborted();
|
|
301
|
-
this.db.prepare(
|
|
548
|
+
this.db.prepare(`DELETE FROM ${table} WHERE cid=?`).run(root);
|
|
302
549
|
return true;
|
|
303
550
|
}
|
|
304
551
|
unpin(root: string, signal?: AbortSignal): Promise<void> {
|
|
@@ -309,6 +556,16 @@ export class Retention {
|
|
|
309
556
|
this.remove(root, signal);
|
|
310
557
|
});
|
|
311
558
|
}
|
|
559
|
+
unpinDirect(root: string, signal?: AbortSignal): Promise<void> {
|
|
560
|
+
root = this.roots([root])[0]!;
|
|
561
|
+
return this.locked(signal, () => {
|
|
562
|
+
if (this.allDirectRoots().includes(root))
|
|
563
|
+
throw new Error(
|
|
564
|
+
"A participating owner still retains this direct block.",
|
|
565
|
+
);
|
|
566
|
+
this.remove(root, signal, "direct_pins");
|
|
567
|
+
});
|
|
568
|
+
}
|
|
312
569
|
private allRoots(): string[] {
|
|
313
570
|
return this.roots([
|
|
314
571
|
...this.db
|
|
@@ -325,6 +582,18 @@ export class Retention {
|
|
|
325
582
|
.map((row) => String(row.cid)),
|
|
326
583
|
]);
|
|
327
584
|
}
|
|
585
|
+
private allDirectRoots(): string[] {
|
|
586
|
+
return this.roots([
|
|
587
|
+
...this.db
|
|
588
|
+
.prepare("SELECT id FROM direct_owners")
|
|
589
|
+
.all()
|
|
590
|
+
.flatMap((row) => this.readOwner(String(row.id), "direct_owners")),
|
|
591
|
+
...this.db
|
|
592
|
+
.prepare("SELECT cid FROM direct_intents")
|
|
593
|
+
.all()
|
|
594
|
+
.map((row) => String(row.cid)),
|
|
595
|
+
]);
|
|
596
|
+
}
|
|
328
597
|
reconcile(
|
|
329
598
|
owner: string,
|
|
330
599
|
roots: readonly string[] | (() => readonly string[]),
|
|
@@ -343,16 +612,47 @@ export class Retention {
|
|
|
343
612
|
return removed;
|
|
344
613
|
});
|
|
345
614
|
}
|
|
615
|
+
reconcileDirect(
|
|
616
|
+
owner: string,
|
|
617
|
+
roots: readonly string[] | (() => readonly string[]),
|
|
618
|
+
signal?: AbortSignal,
|
|
619
|
+
): Promise<number> {
|
|
620
|
+
return this.locked(signal, async () => {
|
|
621
|
+
this.publish(
|
|
622
|
+
owner,
|
|
623
|
+
typeof roots === "function" ? roots() : roots,
|
|
624
|
+
true,
|
|
625
|
+
"direct_owners",
|
|
626
|
+
);
|
|
627
|
+
const marked = new Set(this.allDirectRoots());
|
|
628
|
+
for (const root of marked) await this.ensureDirect(root, signal);
|
|
629
|
+
let removed = 0;
|
|
630
|
+
for (const row of this.db.prepare("SELECT cid FROM direct_pins").all()) {
|
|
631
|
+
const root = String(row.cid);
|
|
632
|
+
if (!marked.has(root) && this.remove(root, signal, "direct_pins"))
|
|
633
|
+
removed++;
|
|
634
|
+
}
|
|
635
|
+
return removed;
|
|
636
|
+
});
|
|
637
|
+
}
|
|
346
638
|
pins(): string[] {
|
|
347
639
|
return this.db
|
|
348
640
|
.prepare("SELECT cid FROM pins WHERE phase='pinned' ORDER BY cid")
|
|
349
641
|
.all()
|
|
350
642
|
.map((row) => String(row.cid));
|
|
351
643
|
}
|
|
644
|
+
directPins(): string[] {
|
|
645
|
+
return this.db
|
|
646
|
+
.prepare("SELECT cid FROM direct_pins WHERE phase='pinned' ORDER BY cid")
|
|
647
|
+
.all()
|
|
648
|
+
.map((row) => String(row.cid));
|
|
649
|
+
}
|
|
352
650
|
maintain<T>(
|
|
353
651
|
action: (references: {
|
|
354
652
|
pinned: readonly string[];
|
|
355
653
|
roots: readonly string[];
|
|
654
|
+
directPinned: readonly string[];
|
|
655
|
+
directRoots: readonly string[];
|
|
356
656
|
staged: readonly string[];
|
|
357
657
|
}) => Promise<T>,
|
|
358
658
|
signal?: AbortSignal,
|
|
@@ -376,6 +676,23 @@ export class Retention {
|
|
|
376
676
|
.all()
|
|
377
677
|
.map((row) => String(row.cid)),
|
|
378
678
|
]),
|
|
679
|
+
directPinned: this.directPins(),
|
|
680
|
+
directRoots: this.roots([
|
|
681
|
+
...this.db
|
|
682
|
+
.prepare("SELECT id FROM direct_owners")
|
|
683
|
+
.all()
|
|
684
|
+
.flatMap((row) => this.readOwner(String(row.id), "direct_owners")),
|
|
685
|
+
...this.db
|
|
686
|
+
.prepare("SELECT cid FROM direct_intents")
|
|
687
|
+
.all()
|
|
688
|
+
.map((row) => String(row.cid)),
|
|
689
|
+
...this.db
|
|
690
|
+
.prepare(
|
|
691
|
+
"SELECT cid FROM direct_pins WHERE phase NOT IN ('staged','stage-pending')",
|
|
692
|
+
)
|
|
693
|
+
.all()
|
|
694
|
+
.map((row) => String(row.cid)),
|
|
695
|
+
]),
|
|
379
696
|
// Import parents may arrive before their children; protect staged blocks individually.
|
|
380
697
|
staged: this.db
|
|
381
698
|
.prepare("SELECT DISTINCT cid FROM stages")
|