@minca/core 0.1.18 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/identity/abilities.d.ts +0 -2
- package/dist/identity/abilities.d.ts.map +1 -1
- package/dist/identity/abilities.js +2 -2
- package/dist/identity/abilities.js.map +1 -1
- package/dist/identity/account-vault.d.ts +0 -2
- package/dist/identity/account-vault.d.ts.map +1 -1
- package/dist/identity/account-vault.js +1 -1
- package/dist/identity/account-vault.js.map +1 -1
- package/dist/identity/crypto-p256.d.ts +1 -1
- package/dist/identity/crypto-p256.d.ts.map +1 -1
- package/dist/identity/ucan.d.ts +2 -1
- package/dist/identity/ucan.d.ts.map +1 -1
- package/dist/identity/ucan.js +1 -1
- package/dist/identity/ucan.js.map +1 -1
- package/dist/inbox/inbox.d.ts +0 -21
- package/dist/inbox/inbox.d.ts.map +1 -1
- package/dist/inbox/inbox.js +4 -4
- package/dist/inbox/inbox.js.map +1 -1
- package/dist/inbox/index.d.ts +4 -5
- package/dist/inbox/index.d.ts.map +1 -1
- package/dist/inbox/index.js +2 -3
- package/dist/inbox/index.js.map +1 -1
- package/dist/inbox/names.d.ts +0 -4
- package/dist/inbox/names.d.ts.map +1 -1
- package/dist/inbox/names.js +2 -2
- package/dist/inbox/names.js.map +1 -1
- package/dist/index.d.ts +45 -71
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +34 -53
- package/dist/index.js.map +1 -1
- package/dist/network/mesh.d.ts.map +1 -1
- package/dist/network/mesh.js +10 -0
- package/dist/network/mesh.js.map +1 -1
- package/dist/network/rendezvous.d.ts +2 -0
- package/dist/network/rendezvous.d.ts.map +1 -1
- package/dist/network/rendezvous.js +5 -0
- package/dist/network/rendezvous.js.map +1 -1
- package/dist/network/rtc-transport.d.ts.map +1 -1
- package/dist/network/rtc-transport.js +8 -3
- package/dist/network/rtc-transport.js.map +1 -1
- package/dist/node/carrier.d.ts +1 -33
- package/dist/node/carrier.d.ts.map +1 -1
- package/dist/node/carrier.js +38 -68
- package/dist/node/carrier.js.map +1 -1
- package/dist/node/index.d.ts +7 -14
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +4 -8
- package/dist/node/index.js.map +1 -1
- package/dist/node/node-carriers.d.ts.map +1 -1
- package/dist/node/node-carriers.js +21 -52
- package/dist/node/node-carriers.js.map +1 -1
- package/dist/node/node-context.d.ts +0 -2
- package/dist/node/node-context.d.ts.map +1 -1
- package/dist/node/node-context.js.map +1 -1
- package/dist/node/node-hosting.d.ts.map +1 -1
- package/dist/node/node-hosting.js +13 -4
- package/dist/node/node-hosting.js.map +1 -1
- package/dist/node/node-records.js +1 -1
- package/dist/node/node-records.js.map +1 -1
- package/dist/node/node.d.ts.map +1 -1
- package/dist/node/node.js +134 -36
- package/dist/node/node.js.map +1 -1
- package/dist/node/space-checks.d.ts +3 -1
- package/dist/node/space-checks.d.ts.map +1 -1
- package/dist/node/space-checks.js +7 -3
- package/dist/node/space-checks.js.map +1 -1
- package/dist/node/space-holding.d.ts +5 -0
- package/dist/node/space-holding.d.ts.map +1 -1
- package/dist/node/space-holding.js +90 -5
- package/dist/node/space-holding.js.map +1 -1
- package/dist/node/space-lines.d.ts +3 -0
- package/dist/node/space-lines.d.ts.map +1 -1
- package/dist/node/space-lines.js +13 -1
- package/dist/node/space-lines.js.map +1 -1
- package/dist/node/space-membership.js +4 -4
- package/dist/node/space-membership.js.map +1 -1
- package/dist/node/space-peers.d.ts +5 -2
- package/dist/node/space-peers.d.ts.map +1 -1
- package/dist/node/space-peers.js +3 -2
- package/dist/node/space-peers.js.map +1 -1
- package/dist/node/space-runtime.d.ts +3 -1
- package/dist/node/space-runtime.d.ts.map +1 -1
- package/dist/node/space-runtime.js +29 -8
- package/dist/node/space-runtime.js.map +1 -1
- package/dist/node/space-standing.d.ts +1 -1
- package/dist/node/space-standing.d.ts.map +1 -1
- package/dist/node/space-standing.js +11 -4
- package/dist/node/space-standing.js.map +1 -1
- package/dist/node/space-writing.d.ts.map +1 -1
- package/dist/node/space-writing.js +9 -0
- package/dist/node/space-writing.js.map +1 -1
- package/dist/node/stores.d.ts +9 -1
- package/dist/node/stores.d.ts.map +1 -1
- package/dist/node/stores.js +4 -2
- package/dist/node/stores.js.map +1 -1
- package/dist/node/types.d.ts +16 -5
- package/dist/node/types.d.ts.map +1 -1
- package/dist/node/worker.d.ts +14 -6
- package/dist/node/worker.d.ts.map +1 -1
- package/dist/node/worker.js +59 -20
- package/dist/node/worker.js.map +1 -1
- package/dist/node/writer-keys.d.ts +2 -2
- package/dist/node/writer-keys.d.ts.map +1 -1
- package/dist/node/writer-keys.js +12 -14
- package/dist/node/writer-keys.js.map +1 -1
- package/dist/privacy/direct.d.ts +0 -7
- package/dist/privacy/direct.d.ts.map +1 -1
- package/dist/privacy/direct.js +2 -2
- package/dist/privacy/direct.js.map +1 -1
- package/dist/query/filter.d.ts +0 -2
- package/dist/query/filter.d.ts.map +1 -1
- package/dist/query/filter.js +1 -1
- package/dist/query/filter.js.map +1 -1
- package/dist/query/types.d.ts +2 -6
- package/dist/query/types.d.ts.map +1 -1
- package/dist/query/types.js.map +1 -1
- package/dist/records/key.d.ts +2 -0
- package/dist/records/key.d.ts.map +1 -1
- package/dist/records/key.js +4 -0
- package/dist/records/key.js.map +1 -1
- package/dist/records/links.js +1 -1
- package/dist/schemas/direct.d.ts +70 -0
- package/dist/schemas/direct.d.ts.map +1 -0
- package/dist/schemas/direct.js +39 -0
- package/dist/schemas/direct.js.map +1 -0
- package/dist/schemas/index.d.ts +10 -73
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +8 -66
- package/dist/schemas/index.js.map +1 -1
- package/dist/session/account-hint.d.ts +2 -2
- package/dist/session/account-hint.d.ts.map +1 -1
- package/dist/session/account-hint.js +3 -3
- package/dist/session/account-hint.js.map +1 -1
- package/dist/session/connect.d.ts +12 -5
- package/dist/session/connect.d.ts.map +1 -1
- package/dist/session/connect.js +25 -21
- package/dist/session/connect.js.map +1 -1
- package/dist/session/connection.d.ts +26 -2
- package/dist/session/connection.d.ts.map +1 -1
- package/dist/session/connection.js +151 -11
- package/dist/session/connection.js.map +1 -1
- package/dist/session/hosting.d.ts +1 -7
- package/dist/session/hosting.d.ts.map +1 -1
- package/dist/session/hosting.js +3 -3
- package/dist/session/hosting.js.map +1 -1
- package/dist/session/index.d.ts +9 -14
- package/dist/session/index.d.ts.map +1 -1
- package/dist/session/index.js +8 -11
- package/dist/session/index.js.map +1 -1
- package/dist/session/stay-signed-in.d.ts +0 -1
- package/dist/session/stay-signed-in.d.ts.map +1 -1
- package/dist/session/stay-signed-in.js +1 -1
- package/dist/session/stay-signed-in.js.map +1 -1
- package/dist/space/account-registry.d.ts +0 -3
- package/dist/space/account-registry.d.ts.map +1 -1
- package/dist/space/account-registry.js.map +1 -1
- package/dist/space/own-automations.d.ts +51 -95
- package/dist/space/own-automations.d.ts.map +1 -1
- package/dist/space/own-automations.js +100 -295
- package/dist/space/own-automations.js.map +1 -1
- package/dist/space/pass.d.ts +8 -3
- package/dist/space/pass.d.ts.map +1 -1
- package/dist/space/pass.js +4 -3
- package/dist/space/pass.js.map +1 -1
- package/dist/space/space-manager.d.ts +1 -3
- package/dist/space/space-manager.d.ts.map +1 -1
- package/dist/space/space-manager.js +5 -7
- package/dist/space/space-manager.js.map +1 -1
- package/dist/storage/body-cache.d.ts +26 -0
- package/dist/storage/body-cache.d.ts.map +1 -0
- package/dist/storage/body-cache.js +41 -0
- package/dist/storage/body-cache.js.map +1 -0
- package/dist/storage/encrypted-adapter.d.ts +0 -5
- package/dist/storage/encrypted-adapter.d.ts.map +1 -1
- package/dist/storage/encrypted-adapter.js +1 -1
- package/dist/storage/encrypted-adapter.js.map +1 -1
- package/dist/storage/folder-adapter.d.ts +8 -2
- package/dist/storage/folder-adapter.d.ts.map +1 -1
- package/dist/storage/folder-adapter.js +43 -26
- package/dist/storage/folder-adapter.js.map +1 -1
- package/dist/storage/indexeddb-adapter.js +27 -2
- package/dist/storage/indexeddb-adapter.js.map +1 -1
- package/dist/storage/sqlite-adapter.d.ts +57 -0
- package/dist/storage/sqlite-adapter.d.ts.map +1 -0
- package/dist/storage/sqlite-adapter.js +213 -0
- package/dist/storage/sqlite-adapter.js.map +1 -0
- package/dist/storage/storage-provider.d.ts +21 -2
- package/dist/storage/storage-provider.d.ts.map +1 -1
- package/dist/storage/storage-provider.js +149 -56
- package/dist/storage/storage-provider.js.map +1 -1
- package/dist/sync/sync-engine.d.ts +10 -4
- package/dist/sync/sync-engine.d.ts.map +1 -1
- package/dist/sync/sync-engine.js +120 -38
- package/dist/sync/sync-engine.js.map +1 -1
- package/dist/sync/sync-messages.d.ts +4 -0
- package/dist/sync/sync-messages.d.ts.map +1 -1
- package/dist/sync/sync-messages.js.map +1 -1
- package/dist/types.d.ts +0 -7
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +0 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/step.d.ts +8 -0
- package/dist/utils/step.d.ts.map +1 -0
- package/dist/utils/step.js +15 -0
- package/dist/utils/step.js.map +1 -0
- package/docs/README.md +1 -1
- package/docs/actions.md +41 -2
- package/docs/agents.md +13 -10
- package/docs/automations.md +16 -48
- package/docs/blueprints-and-guides.md +2 -2
- package/docs/bots.md +3 -3
- package/docs/building-an-app.md +16 -0
- package/docs/collections.md +2 -4
- package/docs/computed.md +1 -2
- package/docs/node.md +112 -39
- package/docs/proposing-collections.md +3 -3
- package/docs/sign-in.md +45 -52
- package/docs/spaces.md +0 -5
- package/docs/standard-library.md +20 -8
- package/docs/storage.md +78 -12
- package/docs/usernames.md +3 -3
- package/package.json +7 -13
- package/dist/elements/auth-styles.d.ts +0 -18
- package/dist/elements/auth-styles.d.ts.map +0 -1
- package/dist/elements/auth-styles.js +0 -206
- package/dist/elements/auth-styles.js.map +0 -1
- package/dist/elements/dom.d.ts +0 -29
- package/dist/elements/dom.d.ts.map +0 -1
- package/dist/elements/dom.js +0 -91
- package/dist/elements/dom.js.map +0 -1
- package/dist/elements/index.d.ts +0 -13
- package/dist/elements/index.d.ts.map +0 -1
- package/dist/elements/index.js +0 -11
- package/dist/elements/index.js.map +0 -1
- package/dist/elements/minca-auth.d.ts +0 -55
- package/dist/elements/minca-auth.d.ts.map +0 -1
- package/dist/elements/minca-auth.js +0 -768
- package/dist/elements/minca-auth.js.map +0 -1
- package/dist/node/actions.d.ts +0 -85
- package/dist/node/actions.d.ts.map +0 -1
- package/dist/node/actions.js +0 -921
- package/dist/node/actions.js.map +0 -1
- package/dist/node/host.d.ts +0 -187
- package/dist/node/host.d.ts.map +0 -1
- package/dist/node/host.js +0 -818
- package/dist/node/host.js.map +0 -1
- package/dist/node/usage.d.ts +0 -32
- package/dist/node/usage.d.ts.map +0 -1
- package/dist/node/usage.js +0 -217
- package/dist/node/usage.js.map +0 -1
- package/dist/node/watch-notifications.d.ts +0 -19
- package/dist/node/watch-notifications.d.ts.map +0 -1
- package/dist/node/watch-notifications.js +0 -128
- package/dist/node/watch-notifications.js.map +0 -1
- package/dist/schemas/automations.d.ts +0 -390
- package/dist/schemas/automations.d.ts.map +0 -1
- package/dist/schemas/automations.js +0 -619
- package/dist/schemas/automations.js.map +0 -1
- package/dist/schemas/blueprints.d.ts +0 -122
- package/dist/schemas/blueprints.d.ts.map +0 -1
- package/dist/schemas/blueprints.js +0 -337
- package/dist/schemas/blueprints.js.map +0 -1
- package/dist/schemas/choosing.d.ts +0 -19
- package/dist/schemas/choosing.d.ts.map +0 -1
- package/dist/schemas/choosing.js +0 -69
- package/dist/schemas/choosing.js.map +0 -1
- package/dist/schemas/connectors.d.ts +0 -202
- package/dist/schemas/connectors.d.ts.map +0 -1
- package/dist/schemas/connectors.js +0 -140
- package/dist/schemas/connectors.js.map +0 -1
- package/dist/schemas/cron.d.ts +0 -14
- package/dist/schemas/cron.d.ts.map +0 -1
- package/dist/schemas/cron.js +0 -75
- package/dist/schemas/cron.js.map +0 -1
- package/dist/schemas/geo.d.ts +0 -63
- package/dist/schemas/geo.d.ts.map +0 -1
- package/dist/schemas/geo.js +0 -131
- package/dist/schemas/geo.js.map +0 -1
- package/dist/schemas/guides.d.ts +0 -26
- package/dist/schemas/guides.d.ts.map +0 -1
- package/dist/schemas/guides.js +0 -33
- package/dist/schemas/guides.js.map +0 -1
- package/dist/schemas/library/annotations.d.ts +0 -616
- package/dist/schemas/library/annotations.d.ts.map +0 -1
- package/dist/schemas/library/annotations.js +0 -156
- package/dist/schemas/library/annotations.js.map +0 -1
- package/dist/schemas/library/collections.d.ts +0 -211
- package/dist/schemas/library/collections.d.ts.map +0 -1
- package/dist/schemas/library/collections.js +0 -57
- package/dist/schemas/library/collections.js.map +0 -1
- package/dist/schemas/library/community.d.ts +0 -1136
- package/dist/schemas/library/community.d.ts.map +0 -1
- package/dist/schemas/library/community.js +0 -315
- package/dist/schemas/library/community.js.map +0 -1
- package/dist/schemas/library/life.d.ts +0 -1440
- package/dist/schemas/library/life.d.ts.map +0 -1
- package/dist/schemas/library/life.js +0 -202
- package/dist/schemas/library/life.js.map +0 -1
- package/dist/schemas/library/media.d.ts +0 -1002
- package/dist/schemas/library/media.d.ts.map +0 -1
- package/dist/schemas/library/media.js +0 -110
- package/dist/schemas/library/media.js.map +0 -1
- package/dist/schemas/library/money.d.ts +0 -1221
- package/dist/schemas/library/money.d.ts.map +0 -1
- package/dist/schemas/library/money.js +0 -168
- package/dist/schemas/library/money.js.map +0 -1
- package/dist/schemas/library/planning.d.ts +0 -1107
- package/dist/schemas/library/planning.d.ts.map +0 -1
- package/dist/schemas/library/planning.js +0 -214
- package/dist/schemas/library/planning.js.map +0 -1
- package/dist/schemas/library/publishing.d.ts +0 -1499
- package/dist/schemas/library/publishing.d.ts.map +0 -1
- package/dist/schemas/library/publishing.js +0 -285
- package/dist/schemas/library/publishing.js.map +0 -1
- package/dist/schemas/library/social.d.ts +0 -1257
- package/dist/schemas/library/social.d.ts.map +0 -1
- package/dist/schemas/library/social.js +0 -147
- package/dist/schemas/library/social.js.map +0 -1
- package/dist/schemas/standard.d.ts +0 -21
- package/dist/schemas/standard.d.ts.map +0 -1
- package/dist/schemas/standard.js +0 -39
- package/dist/schemas/standard.js.map +0 -1
- package/dist/session/auth.d.ts +0 -411
- package/dist/session/auth.d.ts.map +0 -1
- package/dist/session/auth.js +0 -1529
- package/dist/session/auth.js.map +0 -1
- package/dist/session/credentials.d.ts +0 -30
- package/dist/session/credentials.d.ts.map +0 -1
- package/dist/session/credentials.js +0 -63
- package/dist/session/credentials.js.map +0 -1
- package/dist/session/password-strength.d.ts +0 -22
- package/dist/session/password-strength.d.ts.map +0 -1
- package/dist/session/password-strength.js +0 -130
- package/dist/session/password-strength.js.map +0 -1
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module sqlite-adapter
|
|
3
|
+
* A node's stores in one SQLite database: every store's entries and version
|
|
4
|
+
* bodies, each store a path, every write one transaction.
|
|
5
|
+
*
|
|
6
|
+
* The connection is passed in, so this module never imports a driver and runs
|
|
7
|
+
* wherever one does: Node's `node:sqlite` (`DatabaseSync`), Bun's `bun:sqlite`
|
|
8
|
+
* (`Database`), or another with the same four methods.
|
|
9
|
+
*
|
|
10
|
+
* ```
|
|
11
|
+
* stores (id, path) one row per store a node opened
|
|
12
|
+
* entries (store, key, value) which version is current, what sync compares, space records
|
|
13
|
+
* versions (store, id, body) version bodies, as UTF-8 JSON
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* One process writes the database. Unlike a directory store it keeps nothing
|
|
17
|
+
* in memory between calls, so there is nothing to reload and nothing polls it.
|
|
18
|
+
*/
|
|
19
|
+
import { isStoredVersion } from '../utils/guards.js';
|
|
20
|
+
import { utf8Decode, utf8Encode } from '../utils/encoding.js';
|
|
21
|
+
import { createEncryptedAdapter } from './encrypted-adapter.js';
|
|
22
|
+
import { createBodyCache } from './body-cache.js';
|
|
23
|
+
/**
|
|
24
|
+
* Version bodies all the spaces' stores keep in memory together. Reading one
|
|
25
|
+
* from SQLite is cheap, so this is for the few read again and again, and the
|
|
26
|
+
* bodies a node keeps in memory stay the same however many spaces it holds.
|
|
27
|
+
*/
|
|
28
|
+
const CACHED_BODIES = 4_000;
|
|
29
|
+
/** Bytes per page: SQLite reads large values fastest at 8 or 16 KiB, and file chunks are 256 KiB */
|
|
30
|
+
const PAGE_SIZE = 8192;
|
|
31
|
+
/** Before any table: page size and auto-vacuum are fixed once the file has one */
|
|
32
|
+
const SCHEMA = `
|
|
33
|
+
PRAGMA page_size = ${PAGE_SIZE};
|
|
34
|
+
PRAGMA auto_vacuum = INCREMENTAL;
|
|
35
|
+
PRAGMA journal_mode = WAL;
|
|
36
|
+
PRAGMA synchronous = NORMAL;
|
|
37
|
+
CREATE TABLE IF NOT EXISTS stores (id INTEGER PRIMARY KEY, path TEXT NOT NULL UNIQUE);
|
|
38
|
+
CREATE TABLE IF NOT EXISTS entries (
|
|
39
|
+
store INTEGER NOT NULL, key TEXT NOT NULL, value BLOB NOT NULL, PRIMARY KEY (store, key));
|
|
40
|
+
CREATE TABLE IF NOT EXISTS versions (
|
|
41
|
+
store INTEGER NOT NULL, id TEXT NOT NULL, body BLOB NOT NULL, PRIMARY KEY (store, id));
|
|
42
|
+
`;
|
|
43
|
+
/**
|
|
44
|
+
* A node's stores in one SQLite database, for a node on a server or a
|
|
45
|
+
* computer. Opening a store again gives the same data; closing one leaves the
|
|
46
|
+
* database open, since the others share it: close the connection yourself
|
|
47
|
+
* when the node is done.
|
|
48
|
+
* @param options.localKey Seals the registry. Without it nothing is sealed.
|
|
49
|
+
*/
|
|
50
|
+
export function sqliteStores(db, options = {}) {
|
|
51
|
+
db.exec(SCHEMA);
|
|
52
|
+
const sql = {
|
|
53
|
+
findStore: db.prepare('SELECT id FROM stores WHERE path = ?'),
|
|
54
|
+
addStore: db.prepare('INSERT OR IGNORE INTO stores (path) VALUES (?)'),
|
|
55
|
+
paths: db.prepare(`SELECT path FROM stores s WHERE EXISTS (SELECT 1 FROM entries WHERE store = s.id)
|
|
56
|
+
OR EXISTS (SELECT 1 FROM versions WHERE store = s.id) ORDER BY path`),
|
|
57
|
+
get: db.prepare('SELECT value FROM entries WHERE store = ? AND key = ?'),
|
|
58
|
+
put: db.prepare('INSERT INTO entries (store, key, value) VALUES (?, ?, ?) ON CONFLICT (store, key) DO UPDATE SET value = excluded.value'),
|
|
59
|
+
delete: db.prepare('DELETE FROM entries WHERE store = ? AND key = ?'),
|
|
60
|
+
keysFrom: db.prepare('SELECT key FROM entries WHERE store = ? AND key >= ? ORDER BY key'),
|
|
61
|
+
keysBetween: db.prepare('SELECT key FROM entries WHERE store = ? AND key >= ? AND key < ? ORDER BY key'),
|
|
62
|
+
entriesFrom: db.prepare('SELECT key, value FROM entries WHERE store = ? AND key >= ? ORDER BY key'),
|
|
63
|
+
entriesBetween: db.prepare('SELECT key, value FROM entries WHERE store = ? AND key >= ? AND key < ? ORDER BY key'),
|
|
64
|
+
getVersion: db.prepare('SELECT body FROM versions WHERE store = ? AND id = ?'),
|
|
65
|
+
putVersion: db.prepare('INSERT INTO versions (store, id, body) VALUES (?, ?, ?) ON CONFLICT (store, id) DO UPDATE SET body = excluded.body'),
|
|
66
|
+
deleteVersion: db.prepare('DELETE FROM versions WHERE store = ? AND id = ?'),
|
|
67
|
+
entryBytes: db.prepare('SELECT coalesce(sum(length(CAST(key AS BLOB)) + length(value)), 0) AS n FROM entries WHERE store = ?'),
|
|
68
|
+
// A blob's length is in its row's header: nothing is read to sum them.
|
|
69
|
+
versionBytes: db.prepare('SELECT coalesce(sum(length(body)), 0) AS n FROM versions WHERE store = ?'),
|
|
70
|
+
};
|
|
71
|
+
const storeId = (path) => {
|
|
72
|
+
const row = sql.findStore.get(path);
|
|
73
|
+
return isRow(row) && typeof row.id === 'number' ? row.id : null;
|
|
74
|
+
};
|
|
75
|
+
/** Runs `work` in one transaction: all of it lands, or none */
|
|
76
|
+
const inTransaction = (work) => {
|
|
77
|
+
db.exec('BEGIN IMMEDIATE');
|
|
78
|
+
try {
|
|
79
|
+
work();
|
|
80
|
+
db.exec('COMMIT');
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
db.exec('ROLLBACK');
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const open = (path) => {
|
|
88
|
+
sql.addStore.run(path);
|
|
89
|
+
const id = storeId(path);
|
|
90
|
+
let closed = false;
|
|
91
|
+
const live = () => {
|
|
92
|
+
if (closed)
|
|
93
|
+
throw new Error(`The store ${path} is closed`);
|
|
94
|
+
return id;
|
|
95
|
+
};
|
|
96
|
+
const putVersion = (version) => void sql.putVersion.run(live(), version.id, utf8Encode(JSON.stringify(version)));
|
|
97
|
+
const put = (key, value) => void sql.put.run(live(), checkKey(key), value);
|
|
98
|
+
const apply = (ops) => {
|
|
99
|
+
for (const op of ops) {
|
|
100
|
+
if (op.type === 'put')
|
|
101
|
+
put(op.key, op.value);
|
|
102
|
+
else
|
|
103
|
+
sql.delete.run(live(), op.key);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const readVersion = (versionId) => {
|
|
107
|
+
const row = sql.getVersion.get(live(), versionId);
|
|
108
|
+
return isRow(row) && row.body instanceof Uint8Array ? parseVersion(row.body) : null;
|
|
109
|
+
};
|
|
110
|
+
return Object.freeze({
|
|
111
|
+
async get(key) {
|
|
112
|
+
const row = sql.get.get(live(), key);
|
|
113
|
+
return isRow(row) && row.value instanceof Uint8Array ? row.value : null;
|
|
114
|
+
},
|
|
115
|
+
async put(key, value) {
|
|
116
|
+
put(key, value);
|
|
117
|
+
},
|
|
118
|
+
async delete(key) {
|
|
119
|
+
sql.delete.run(live(), key);
|
|
120
|
+
},
|
|
121
|
+
async list(prefix = '') {
|
|
122
|
+
const end = after(prefix);
|
|
123
|
+
const rows = end === null ? sql.keysFrom.all(live(), prefix) : sql.keysBetween.all(live(), prefix, end);
|
|
124
|
+
return rows.flatMap((row) => (isRow(row) && typeof row.key === 'string' ? [row.key] : []));
|
|
125
|
+
},
|
|
126
|
+
async entries(prefix) {
|
|
127
|
+
const end = after(prefix);
|
|
128
|
+
const rows = end === null ? sql.entriesFrom.all(live(), prefix) : sql.entriesBetween.all(live(), prefix, end);
|
|
129
|
+
return rows.flatMap((row) => isRow(row) && typeof row.key === 'string' && row.value instanceof Uint8Array
|
|
130
|
+
? [[row.key, row.value]]
|
|
131
|
+
: []);
|
|
132
|
+
},
|
|
133
|
+
async putVersion(version) {
|
|
134
|
+
putVersion(version);
|
|
135
|
+
},
|
|
136
|
+
async getVersion(versionId) {
|
|
137
|
+
return readVersion(versionId);
|
|
138
|
+
},
|
|
139
|
+
async getVersions(ids) {
|
|
140
|
+
return ids.map(readVersion);
|
|
141
|
+
},
|
|
142
|
+
async deleteVersion(versionId) {
|
|
143
|
+
sql.deleteVersion.run(live(), versionId);
|
|
144
|
+
},
|
|
145
|
+
async batch(ops) {
|
|
146
|
+
inTransaction(() => apply(ops));
|
|
147
|
+
},
|
|
148
|
+
async commit(write) {
|
|
149
|
+
inTransaction(() => {
|
|
150
|
+
for (const version of write.store)
|
|
151
|
+
putVersion(version);
|
|
152
|
+
apply(write.ops);
|
|
153
|
+
for (const versionId of write.remove)
|
|
154
|
+
sql.deleteVersion.run(live(), versionId);
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
async close() {
|
|
158
|
+
closed = true;
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
const count = (row) => (isRow(row) ? Number(row.n ?? 0) : 0);
|
|
163
|
+
const factory = async (path, storeOptions) => {
|
|
164
|
+
const adapter = open(path);
|
|
165
|
+
return storeOptions?.seal && options.localKey
|
|
166
|
+
? createEncryptedAdapter(adapter, options.localKey)
|
|
167
|
+
: adapter;
|
|
168
|
+
};
|
|
169
|
+
return Object.assign(factory, {
|
|
170
|
+
cache: createBodyCache(CACHED_BODIES),
|
|
171
|
+
size(path) {
|
|
172
|
+
const id = storeId(path);
|
|
173
|
+
return id === null ? 0 : count(sql.entryBytes.get(id)) + count(sql.versionBytes.get(id));
|
|
174
|
+
},
|
|
175
|
+
paths() {
|
|
176
|
+
return sql.paths.all().flatMap((row) => (isRow(row) && typeof row.path === 'string' ? [row.path] : []));
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The first string after every string starting with `prefix`, or null when
|
|
182
|
+
* there is none. SQLite compares text as UTF-8 bytes, which sort as code
|
|
183
|
+
* points do, so `[prefix, after(prefix))` is exactly the strings it starts.
|
|
184
|
+
*/
|
|
185
|
+
export function after(prefix) {
|
|
186
|
+
const points = [...prefix];
|
|
187
|
+
while (points.length > 0) {
|
|
188
|
+
const last = points.pop().codePointAt(0);
|
|
189
|
+
if (last >= 0x10ffff)
|
|
190
|
+
continue;
|
|
191
|
+
// A surrogate is no code point of its own: the next one after them is U+E000.
|
|
192
|
+
const next = last + 1 >= 0xd800 && last + 1 <= 0xdfff ? 0xe000 : last + 1;
|
|
193
|
+
return points.join('') + String.fromCodePoint(next);
|
|
194
|
+
}
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
/** A key as SQLite keeps it whole: text read back stops at U+0000 */
|
|
198
|
+
function checkKey(key) {
|
|
199
|
+
if (key.includes('\0'))
|
|
200
|
+
throw new Error('A store key may not hold U+0000');
|
|
201
|
+
return key;
|
|
202
|
+
}
|
|
203
|
+
function parseVersion(body) {
|
|
204
|
+
try {
|
|
205
|
+
const value = JSON.parse(utf8Decode(body));
|
|
206
|
+
return isStoredVersion(value) ? value : null;
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const isRow = (row) => typeof row === 'object' && row !== null;
|
|
213
|
+
//# sourceMappingURL=sqlite-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite-adapter.js","sourceRoot":"","sources":["../../src/storage/sqlite-adapter.ts"],"names":[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,QAAQ,IAAI,GAAG,KAAK,IAAI,CAAC"}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* g/<key> → the record's first version (once it has been edited)
|
|
7
7
|
* h/<key>/<seq>/<id> → a superseded version, whole (only if marked `retain`)
|
|
8
8
|
* i/<collection>/<time>/<id> → every version kept, by collection — what sync compares
|
|
9
|
+
* t/<collection>/<time>/<id> → those in the shallow set — what a shallow exchange compares
|
|
9
10
|
* ```
|
|
10
11
|
*
|
|
11
12
|
* A version that arrives is compared with the one at `r/<key>` by the ordering
|
|
@@ -19,6 +20,7 @@
|
|
|
19
20
|
* writer's change reaches them through {@link StorageProvider.invalidate}.
|
|
20
21
|
*/
|
|
21
22
|
import type { StorageAdapter, Version } from '../types.js';
|
|
23
|
+
import { type BodyCache } from './body-cache.js';
|
|
22
24
|
import { ItemSet, type Sum } from '../sync/negentropy.js';
|
|
23
25
|
export interface StorageProvider {
|
|
24
26
|
/**
|
|
@@ -50,6 +52,10 @@ export interface StorageProvider {
|
|
|
50
52
|
items(collection: string): Promise<ItemSet>;
|
|
51
53
|
/** Every collection this store keeps anything in, and the sum of its version ids. */
|
|
52
54
|
sums(): Promise<ReadonlyMap<string, Sum>>;
|
|
55
|
+
/** {@link items} of a collection's shallow set ({@link inShallowSet}), as a shallow exchange compares it (05 §5). */
|
|
56
|
+
shallowItems(collection: string): Promise<ItemSet>;
|
|
57
|
+
/** {@link sums} of every collection's shallow set. */
|
|
58
|
+
shallowSums(): Promise<ReadonlyMap<string, Sum>>;
|
|
53
59
|
/**
|
|
54
60
|
* One fingerprint of everything kept, hex. Equal fingerprints mean the
|
|
55
61
|
* same versions kept — for status, and for tests.
|
|
@@ -64,11 +70,24 @@ export declare const CURRENT_PREFIX = "r/";
|
|
|
64
70
|
export declare const GENESIS_PREFIX = "g/";
|
|
65
71
|
export declare const HISTORY_PREFIX = "h/";
|
|
66
72
|
export declare const ITEM_PREFIX = "i/";
|
|
73
|
+
/** `t/<collection>/<time>/<id>`: the versions of {@link ITEM_PREFIX} in their collection's shallow set */
|
|
74
|
+
export declare const SHALLOW_PREFIX = "t/";
|
|
67
75
|
/**
|
|
68
76
|
* Where a version sorts for sync: its own time, in whole seconds. Only an
|
|
69
77
|
* order — a wrong clock makes a version slower to find, never lost.
|
|
70
78
|
*/
|
|
71
79
|
export declare function syncTime(version: Pick<Version, 'createdAt'>): number;
|
|
72
|
-
/**
|
|
73
|
-
|
|
80
|
+
/**
|
|
81
|
+
* Whether a kept version is in its collection's shallow set (05 §5): every
|
|
82
|
+
* version but an unretained stub past a record's first, outside a line, where
|
|
83
|
+
* a losing twin decides what counts. Decided by the version alone, so two
|
|
84
|
+
* stores keeping the same versions keep the same set.
|
|
85
|
+
*/
|
|
86
|
+
export declare const inShallowSet: (version: Version) => boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Creates a StorageProvider wrapping an adapter.
|
|
89
|
+
* @param cache Where it keeps version bodies in memory: shared with other
|
|
90
|
+
* stores, or by default one of its own of 50,000
|
|
91
|
+
*/
|
|
92
|
+
export declare function createStorageProvider(adapter: StorageAdapter, cache?: BodyCache): StorageProvider;
|
|
74
93
|
//# sourceMappingURL=storage-provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage-provider.d.ts","sourceRoot":"","sources":["../../src/storage/storage-provider.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"storage-provider.d.ts","sourceRoot":"","sources":["../../src/storage/storage-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAW,MAAM,aAAa,CAAC;AAQpE,OAAO,EAAmB,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAKL,OAAO,EAGP,KAAK,GAAG,EACT,MAAM,uBAAuB,CAAC;AAE/B,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,UAAU,CAAC,OAAO,EAAE,O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CAAC;IACnD,sDAAsD;IACtD,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;IACjD;;;OAGG;IACH,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/B,sFAAsF;IACtF,UAAU,IAAI,IAAI,CAAC;IACnB,iCAAiC;IACjC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,eAAO,MAAM,cAAc,OAAO,CAAC;AACnC,eAAO,MAAM,cAAc,OAAO,CAAC;AACnC,eAAO,MAAM,cAAc,OAAO,CAAC;AACnC,eAAO,MAAM,WAAW,OAAO,CAAC;AAChC,0GAA0G;AAC1G,eAAO,MAAM,cAAc,OAAO,CAAC;AAanC;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC,GAAG,MAAM,CAGpE;AAMD;;;;;GAKG;AACH,eAAO,MAAM,YAAY,YAAa,OAAO,KAAG,OACiD,CAAC;AAkHlG;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,cAAc,EAAE,KAAK,CAAC,EAAE,SAAS,GAAG,eAAe,CA2ajG"}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* g/<key> → the record's first version (once it has been edited)
|
|
7
7
|
* h/<key>/<seq>/<id> → a superseded version, whole (only if marked `retain`)
|
|
8
8
|
* i/<collection>/<time>/<id> → every version kept, by collection — what sync compares
|
|
9
|
+
* t/<collection>/<time>/<id> → those in the shallow set — what a shallow exchange compares
|
|
9
10
|
* ```
|
|
10
11
|
*
|
|
11
12
|
* A version that arrives is compared with the one at `r/<key>` by the ordering
|
|
@@ -19,17 +20,25 @@
|
|
|
19
20
|
* writer's change reaches them through {@link StorageProvider.invalidate}.
|
|
20
21
|
*/
|
|
21
22
|
import { byVersion, supersedes } from '../records/version.js';
|
|
23
|
+
import { hasLineKeyForm } from '../records/key.js';
|
|
22
24
|
import { isStub, stubOf } from '../schema/version.js';
|
|
23
25
|
import { utf8Encode, utf8Decode, bytesToHex } from '../utils/encoding.js';
|
|
24
26
|
import { cidDigest } from '../utils/hash.js';
|
|
25
27
|
import { getOrAdd } from '../utils/map.js';
|
|
26
28
|
import { queue } from '../utils/serial.js';
|
|
29
|
+
import { createBodyCache } from './body-cache.js';
|
|
27
30
|
import { addToSum, combineSums, EMPTY_SUM, fingerprintOf, ItemSet, removeFromSum, } from '../sync/negentropy.js';
|
|
28
31
|
export const CURRENT_PREFIX = 'r/';
|
|
29
32
|
export const GENESIS_PREFIX = 'g/';
|
|
30
33
|
export const HISTORY_PREFIX = 'h/';
|
|
31
34
|
export const ITEM_PREFIX = 'i/';
|
|
32
|
-
/**
|
|
35
|
+
/** `t/<collection>/<time>/<id>`: the versions of {@link ITEM_PREFIX} in their collection's shallow set */
|
|
36
|
+
export const SHALLOW_PREFIX = 't/';
|
|
37
|
+
/** Present once a store's `t/` entries are complete: one kept before them is indexed once, when first read */
|
|
38
|
+
const SHALLOW_INDEXED = 'shallow-indexed';
|
|
39
|
+
/** Versions read at a time while indexing a store kept before `t/`, between which other work runs */
|
|
40
|
+
const INDEX_CHUNK = 200;
|
|
41
|
+
/** Version bodies a store keeps in memory when it has a cache of its own, the one used longest ago let go first */
|
|
33
42
|
const MAX_BODIES = 50_000;
|
|
34
43
|
const currentKey = (key) => `${CURRENT_PREFIX}${key}`;
|
|
35
44
|
const genesisKey = (key) => `${GENESIS_PREFIX}${key}`;
|
|
@@ -44,8 +53,15 @@ export function syncTime(version) {
|
|
|
44
53
|
return Number.isFinite(ms) && ms > 0 ? Math.floor(ms / 1000) : 0;
|
|
45
54
|
}
|
|
46
55
|
/** Collection names may hold anything; the entry needs them free of `/`. */
|
|
47
|
-
const itemKey = (e) => `${
|
|
48
|
-
/**
|
|
56
|
+
const itemKey = (e, prefix = ITEM_PREFIX) => `${prefix}${encodeURIComponent(e.collection)}/${syncTime(e)}/${e.id}`;
|
|
57
|
+
/**
|
|
58
|
+
* Whether a kept version is in its collection's shallow set (05 §5): every
|
|
59
|
+
* version but an unretained stub past a record's first, outside a line, where
|
|
60
|
+
* a losing twin decides what counts. Decided by the version alone, so two
|
|
61
|
+
* stores keeping the same versions keep the same set.
|
|
62
|
+
*/
|
|
63
|
+
export const inShallowSet = (version) => !isStub(version) || version.seq === 0 || version.retain === true || hasLineKeyForm(version.key);
|
|
64
|
+
/** Reads an `i/` or `t/` entry back: null for anything malformed, or an id that is not a content id */
|
|
49
65
|
function parseItemKey(key) {
|
|
50
66
|
const [, collection, time, id, extra] = key.split('/');
|
|
51
67
|
if (collection === undefined || time === undefined || id === undefined || extra !== undefined)
|
|
@@ -84,10 +100,10 @@ function mergeInto(group, layer) {
|
|
|
84
100
|
group.toStore.set(id, version);
|
|
85
101
|
}
|
|
86
102
|
const decodeId = (bytes) => (bytes ? utf8Decode(bytes) : null);
|
|
87
|
-
/** Every `i/` entry, by collection, then by id */
|
|
88
|
-
async function readKept(adapter) {
|
|
103
|
+
/** Every `i/` entry (or `t/`), by collection, then by id */
|
|
104
|
+
async function readKept(adapter, prefix = ITEM_PREFIX) {
|
|
89
105
|
const kept = new Map();
|
|
90
|
-
for (const key of await adapter.list(
|
|
106
|
+
for (const key of await adapter.list(prefix)) {
|
|
91
107
|
const parsed = parseItemKey(key);
|
|
92
108
|
if (parsed)
|
|
93
109
|
getOrAdd(kept, parsed.collection, () => new Map()).set(parsed.id, parsed.item);
|
|
@@ -100,8 +116,40 @@ function sumOf(items) {
|
|
|
100
116
|
sum = addToSum(sum, item.id);
|
|
101
117
|
return sum;
|
|
102
118
|
}
|
|
103
|
-
|
|
104
|
-
|
|
119
|
+
const indexOf = (byCollection) => ({
|
|
120
|
+
byCollection,
|
|
121
|
+
totals: new Map([...byCollection].map(([collection, byId]) => [collection, sumOf(byId.values())])),
|
|
122
|
+
});
|
|
123
|
+
function addToIndex(index, e) {
|
|
124
|
+
const digest = cidDigest(e.id);
|
|
125
|
+
const byId = getOrAdd(index.byCollection, e.collection, () => new Map());
|
|
126
|
+
if (!digest || byId.has(e.id))
|
|
127
|
+
return;
|
|
128
|
+
byId.set(e.id, { timestamp: syncTime(e), id: digest });
|
|
129
|
+
index.totals.set(e.collection, addToSum(index.totals.get(e.collection) ?? EMPTY_SUM, digest));
|
|
130
|
+
}
|
|
131
|
+
function removeFromIndex(index, e) {
|
|
132
|
+
const item = index.byCollection.get(e.collection)?.get(e.id);
|
|
133
|
+
if (!item)
|
|
134
|
+
return;
|
|
135
|
+
index.byCollection.get(e.collection).delete(e.id);
|
|
136
|
+
index.totals.set(e.collection, removeFromSum(index.totals.get(e.collection) ?? EMPTY_SUM, item.id));
|
|
137
|
+
}
|
|
138
|
+
const isScheduler = (value) => typeof value === 'function';
|
|
139
|
+
/** Lets other work run: by `setImmediate` where there is one (Node, Bun), which no fake timer holds back */
|
|
140
|
+
const yieldToOthers = () => new Promise((resolve) => {
|
|
141
|
+
const immediate = Reflect.get(globalThis, 'setImmediate');
|
|
142
|
+
if (isScheduler(immediate))
|
|
143
|
+
immediate(resolve);
|
|
144
|
+
else
|
|
145
|
+
setTimeout(resolve, 0);
|
|
146
|
+
});
|
|
147
|
+
/**
|
|
148
|
+
* Creates a StorageProvider wrapping an adapter.
|
|
149
|
+
* @param cache Where it keeps version bodies in memory: shared with other
|
|
150
|
+
* stores, or by default one of its own of 50,000
|
|
151
|
+
*/
|
|
152
|
+
export function createStorageProvider(adapter, cache) {
|
|
105
153
|
// A change reads the current version, then writes: two interleaved would
|
|
106
154
|
// decide against the same one. So changes take turns.
|
|
107
155
|
const exclusively = queue();
|
|
@@ -113,17 +161,12 @@ export function createStorageProvider(adapter) {
|
|
|
113
161
|
// under its id; a pointer read while a change was landing is not kept.
|
|
114
162
|
/** `r/` and `g/` entries: the id they name, or null for none */
|
|
115
163
|
const pointers = new Map();
|
|
116
|
-
/** Version bodies by id, the
|
|
117
|
-
const bodies =
|
|
164
|
+
/** Version bodies by id, the one used longest ago let go first */
|
|
165
|
+
const bodies = (cache ?? createBodyCache(MAX_BODIES)).part();
|
|
118
166
|
/** Counts changes landed, so a read that raced one is not kept */
|
|
119
167
|
let landed = 0;
|
|
120
168
|
const isPointer = (key) => key.startsWith(CURRENT_PREFIX) || key.startsWith(GENESIS_PREFIX);
|
|
121
|
-
const remember = (version) =>
|
|
122
|
-
bodies.delete(version.id);
|
|
123
|
-
bodies.set(version.id, version);
|
|
124
|
-
if (bodies.size > MAX_BODIES)
|
|
125
|
-
bodies.delete(bodies.keys().next().value);
|
|
126
|
-
};
|
|
169
|
+
const remember = (version) => bodies.set(version);
|
|
127
170
|
/** Bodies the changes landing now will store: readable by the changes after them in the same group */
|
|
128
171
|
const staging = new Map();
|
|
129
172
|
async function body(id) {
|
|
@@ -136,42 +179,68 @@ export function createStorageProvider(adapter) {
|
|
|
136
179
|
return version;
|
|
137
180
|
}
|
|
138
181
|
// ─── What sync compares, in memory ─────────────────────────────────
|
|
139
|
-
/**
|
|
140
|
-
let
|
|
182
|
+
/** Every version kept, and the shallow sets' (`i/` and `t/`). Null until first needed. */
|
|
183
|
+
let indexes = null;
|
|
141
184
|
/** Sorted sets, built when a reconciliation asks for one and dropped when their collection changes */
|
|
142
185
|
const sets = new Map();
|
|
143
|
-
|
|
144
|
-
let totals = null;
|
|
186
|
+
const shallowSets = new Map();
|
|
145
187
|
/** Read in turn with changes, so none lands between the listing and the sets being kept up to date */
|
|
146
|
-
const
|
|
188
|
+
const loadIndexes = async () => indexes ??
|
|
147
189
|
exclusively(async () => {
|
|
148
|
-
if (
|
|
149
|
-
return
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
190
|
+
if (indexes)
|
|
191
|
+
return indexes;
|
|
192
|
+
const all = await readKept(adapter);
|
|
193
|
+
const shallow = (await adapter.get(SHALLOW_INDEXED))
|
|
194
|
+
? await readKept(adapter, SHALLOW_PREFIX)
|
|
195
|
+
: await indexShallow(all);
|
|
196
|
+
indexes = { all: indexOf(all), shallow: indexOf(shallow) };
|
|
197
|
+
return indexes;
|
|
154
198
|
});
|
|
199
|
+
const loadKept = async () => (await loadIndexes()).all.byCollection;
|
|
200
|
+
/**
|
|
201
|
+
* Writes the `t/` entries of a store kept before them, reading each version
|
|
202
|
+
* once, a chunk at a time so a large store doesn't hold up everything else.
|
|
203
|
+
* Run in turn with changes, so none lands meanwhile.
|
|
204
|
+
*/
|
|
205
|
+
async function indexShallow(all) {
|
|
206
|
+
const shallow = new Map();
|
|
207
|
+
for (const [collection, byId] of all) {
|
|
208
|
+
const ids = [...byId.keys()];
|
|
209
|
+
for (let from = 0; from < ids.length; from += INDEX_CHUNK) {
|
|
210
|
+
const ops = [];
|
|
211
|
+
for (const version of await load(ids.slice(from, from + INDEX_CHUNK))) {
|
|
212
|
+
if (!inShallowSet(version))
|
|
213
|
+
continue;
|
|
214
|
+
ops.push({ type: 'put', key: itemKey(version, SHALLOW_PREFIX), value: utf8Encode(version.id) });
|
|
215
|
+
getOrAdd(shallow, collection, () => new Map()).set(version.id, byId.get(version.id));
|
|
216
|
+
}
|
|
217
|
+
if (ops.length > 0)
|
|
218
|
+
await adapter.batch(ops);
|
|
219
|
+
await yieldToOthers();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
await adapter.put(SHALLOW_INDEXED, utf8Encode('1'));
|
|
223
|
+
return shallow;
|
|
224
|
+
}
|
|
155
225
|
/** Keeps the in-memory sets in step with a change just written */
|
|
156
226
|
const track = (added, removed) => {
|
|
157
|
-
for (const e of [...added, ...removed])
|
|
227
|
+
for (const e of [...added, ...removed]) {
|
|
158
228
|
sets.delete(e.collection);
|
|
159
|
-
|
|
229
|
+
shallowSets.delete(e.collection);
|
|
230
|
+
}
|
|
231
|
+
if (!indexes)
|
|
160
232
|
return;
|
|
161
233
|
for (const e of removed) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
continue;
|
|
165
|
-
kept.get(e.collection).delete(e.id);
|
|
166
|
-
totals.set(e.collection, removeFromSum(totals.get(e.collection) ?? EMPTY_SUM, item.id));
|
|
234
|
+
removeFromIndex(indexes.all, e);
|
|
235
|
+
removeFromIndex(indexes.shallow, e);
|
|
167
236
|
}
|
|
168
237
|
for (const e of added) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
238
|
+
addToIndex(indexes.all, e);
|
|
239
|
+
// Kept again as its stub once superseded: out of the shallow set, unless it is a first version or retained.
|
|
240
|
+
if (inShallowSet(e))
|
|
241
|
+
addToIndex(indexes.shallow, e);
|
|
242
|
+
else
|
|
243
|
+
removeFromIndex(indexes.shallow, e);
|
|
175
244
|
}
|
|
176
245
|
};
|
|
177
246
|
/** Changes that came in while others were landing: the next group */
|
|
@@ -270,11 +339,13 @@ export function createStorageProvider(adapter) {
|
|
|
270
339
|
staging.set(version.id, version);
|
|
271
340
|
}
|
|
272
341
|
layer.writes.set(itemKey(version), utf8Encode(version.id));
|
|
342
|
+
layer.writes.set(itemKey(version, SHALLOW_PREFIX), inShallowSet(version) ? utf8Encode(version.id) : null);
|
|
273
343
|
layer.added.set(version.id, version);
|
|
274
344
|
layer.dropped.delete(version.id);
|
|
275
345
|
},
|
|
276
346
|
drop: (version) => {
|
|
277
347
|
layer.writes.set(itemKey(version), null);
|
|
348
|
+
layer.writes.set(itemKey(version, SHALLOW_PREFIX), null);
|
|
278
349
|
layer.dropped.set(version.id, version);
|
|
279
350
|
layer.added.delete(version.id);
|
|
280
351
|
layer.toStore.delete(version.id);
|
|
@@ -320,19 +391,35 @@ export function createStorageProvider(adapter) {
|
|
|
320
391
|
await entries.keep(held && !isStub(held) ? held : version);
|
|
321
392
|
entries.set(historyKey(version), version.id);
|
|
322
393
|
}
|
|
323
|
-
/**
|
|
394
|
+
/**
|
|
395
|
+
* Versions by id, those held, in one read where the adapter can. What it
|
|
396
|
+
* read is returned, not read back from the cache, which may hold fewer.
|
|
397
|
+
*/
|
|
324
398
|
async function load(ids) {
|
|
325
399
|
const wanted = [...ids];
|
|
326
|
-
const
|
|
400
|
+
const found = new Map();
|
|
401
|
+
const missing = [];
|
|
402
|
+
for (const id of wanted) {
|
|
403
|
+
const held = bodies.get(id);
|
|
404
|
+
if (held)
|
|
405
|
+
found.set(id, held);
|
|
406
|
+
else
|
|
407
|
+
missing.push(id);
|
|
408
|
+
}
|
|
327
409
|
if (adapter.getVersions && missing.length > 1) {
|
|
328
|
-
for (const version of await adapter.getVersions(missing))
|
|
329
|
-
if (version)
|
|
330
|
-
|
|
410
|
+
for (const version of await adapter.getVersions(missing)) {
|
|
411
|
+
if (!version)
|
|
412
|
+
continue;
|
|
413
|
+
found.set(version.id, version);
|
|
414
|
+
remember(version);
|
|
415
|
+
}
|
|
331
416
|
}
|
|
332
417
|
else {
|
|
333
|
-
await Promise.all(missing.map((id) => body(id)))
|
|
418
|
+
for (const version of await Promise.all(missing.map((id) => body(id))))
|
|
419
|
+
if (version)
|
|
420
|
+
found.set(version.id, version);
|
|
334
421
|
}
|
|
335
|
-
return wanted.
|
|
422
|
+
return wanted.flatMap((id) => found.get(id) ?? []);
|
|
336
423
|
}
|
|
337
424
|
/** `[entry key, version id]` for every pointer under a prefix, in one read where the adapter can */
|
|
338
425
|
async function pointersUnder(prefix) {
|
|
@@ -375,18 +462,22 @@ export function createStorageProvider(adapter) {
|
|
|
375
462
|
const versionsOf = (held) => [...held].flatMap((id) => found.get(id) ?? []).sort(byVersion);
|
|
376
463
|
return new Map([...ids].map(([key, held]) => [key, versionsOf(held)]));
|
|
377
464
|
}
|
|
378
|
-
async function
|
|
379
|
-
const
|
|
465
|
+
async function itemsOf(collection, shallow) {
|
|
466
|
+
const cache = shallow ? shallowSets : sets;
|
|
467
|
+
const cached = cache.get(collection);
|
|
380
468
|
if (cached)
|
|
381
469
|
return cached;
|
|
382
|
-
const
|
|
383
|
-
|
|
470
|
+
const index = (await loadIndexes())[shallow ? 'shallow' : 'all'];
|
|
471
|
+
const set = new ItemSet(index.byCollection.get(collection)?.values() ?? []);
|
|
472
|
+
cache.set(collection, set);
|
|
384
473
|
return set;
|
|
385
474
|
}
|
|
386
|
-
async function
|
|
387
|
-
await
|
|
388
|
-
return new Map([...
|
|
475
|
+
async function sumsOf(shallow) {
|
|
476
|
+
const index = (await loadIndexes())[shallow ? 'shallow' : 'all'];
|
|
477
|
+
return new Map([...index.totals].filter(([, sum]) => sum.count > 0));
|
|
389
478
|
}
|
|
479
|
+
const items = (collection) => itemsOf(collection, false);
|
|
480
|
+
const sums = () => sumsOf(false);
|
|
390
481
|
return Object.freeze({
|
|
391
482
|
addVersion(incoming) {
|
|
392
483
|
return change(async (entries) => {
|
|
@@ -449,6 +540,8 @@ export function createStorageProvider(adapter) {
|
|
|
449
540
|
},
|
|
450
541
|
items,
|
|
451
542
|
sums,
|
|
543
|
+
shallowItems: (collection) => itemsOf(collection, true),
|
|
544
|
+
shallowSums: () => sumsOf(true),
|
|
452
545
|
async fingerprint() {
|
|
453
546
|
let total = EMPTY_SUM;
|
|
454
547
|
for (const sum of (await sums()).values())
|
|
@@ -456,9 +549,9 @@ export function createStorageProvider(adapter) {
|
|
|
456
549
|
return bytesToHex(await fingerprintOf(total));
|
|
457
550
|
},
|
|
458
551
|
invalidate() {
|
|
459
|
-
|
|
460
|
-
totals = null;
|
|
552
|
+
indexes = null;
|
|
461
553
|
sets.clear();
|
|
554
|
+
shallowSets.clear();
|
|
462
555
|
pointers.clear();
|
|
463
556
|
bodies.clear();
|
|
464
557
|
landed++;
|