@agentshouse/mdruntime 0.1.0-c5d81e5d5cb58 → 0.1.0-c5e756efec46e
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 +21 -19
- package/dist/acceptance.d.ts +3 -0
- package/dist/acceptance.d.ts.map +1 -0
- package/dist/acceptance.js +299 -0
- package/dist/act-lanes.d.ts +30 -0
- package/dist/act-lanes.d.ts.map +1 -0
- package/dist/act-lanes.js +225 -0
- package/dist/actions.d.ts +26 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +321 -0
- package/dist/adapter.d.ts +207 -121
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +3 -11
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +20 -60
- package/dist/authored.d.ts +88 -0
- package/dist/authored.d.ts.map +1 -0
- package/dist/authored.js +347 -0
- package/dist/binary.d.ts +6 -2
- package/dist/binary.d.ts.map +1 -1
- package/dist/binary.js +10 -3
- package/dist/bootstrap.d.ts +15 -18
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +252 -158
- package/dist/cell.d.ts +6 -0
- package/dist/cell.d.ts.map +1 -0
- package/dist/cell.js +9 -0
- package/dist/chain.d.ts +2 -1
- package/dist/chain.d.ts.map +1 -1
- package/dist/change-set.d.ts +4 -0
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +6 -0
- package/dist/change.d.ts +7 -2
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +7 -2
- package/dist/chat-types.d.ts +3 -0
- package/dist/chat-types.d.ts.map +1 -0
- package/dist/chat-types.js +11 -0
- package/dist/client.d.ts +41 -25
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +58 -69
- package/dist/collection.d.ts +27 -7
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +29 -27
- package/dist/column.d.ts +5 -0
- package/dist/column.d.ts.map +1 -0
- package/dist/column.js +9 -0
- package/dist/compile.d.ts +8 -6
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +289 -333
- package/dist/connection.d.ts +6 -144
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +114 -5227
- package/dist/context.d.ts +2 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +7 -0
- package/dist/conversation-live.d.ts +149 -0
- package/dist/conversation-live.d.ts.map +1 -0
- package/dist/conversation-live.js +259 -0
- package/dist/conversation-row.d.ts +40 -0
- package/dist/conversation-row.d.ts.map +1 -0
- package/dist/conversation-row.js +66 -0
- package/dist/conversation.d.ts +74 -56
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +1015 -883
- package/dist/declaration.d.ts +10 -0
- package/dist/declaration.d.ts.map +1 -0
- package/dist/declaration.js +24 -0
- package/dist/discovery.d.ts +12 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +175 -0
- package/dist/download.d.ts +6 -0
- package/dist/download.d.ts.map +1 -0
- package/dist/download.js +42 -0
- package/dist/durability.d.ts +20 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +7 -0
- package/dist/entry.d.ts +17 -2
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +60 -21
- package/dist/expectation.d.ts +26 -13
- package/dist/expectation.d.ts.map +1 -1
- package/dist/expectation.js +72 -19
- package/dist/failure.d.ts +3 -0
- package/dist/failure.d.ts.map +1 -1
- package/dist/failure.js +8 -0
- package/dist/figures.d.ts +49 -0
- package/dist/figures.d.ts.map +1 -0
- package/dist/figures.js +199 -0
- package/dist/file.d.ts +1 -1
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +3 -2
- package/dist/holdings.d.ts +29 -0
- package/dist/holdings.d.ts.map +1 -0
- package/dist/holdings.js +95 -0
- package/dist/host.d.ts +25 -8
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +130 -56
- package/dist/house-query.d.ts +18 -0
- package/dist/house-query.d.ts.map +1 -0
- package/dist/house-query.js +54 -0
- package/dist/identity.d.ts +0 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +0 -3
- package/dist/index.d.ts +43 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -6
- package/dist/install.d.ts +60 -0
- package/dist/install.d.ts.map +1 -0
- package/dist/install.js +537 -0
- package/dist/lifecycle.d.ts +7 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +290 -0
- package/dist/listing.d.ts +60 -0
- package/dist/listing.d.ts.map +1 -0
- package/dist/listing.js +168 -0
- package/dist/live.d.ts +9 -6
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +118 -36
- package/dist/loaded.d.ts +7 -0
- package/dist/loaded.d.ts.map +1 -0
- package/dist/loaded.js +46 -0
- package/dist/locks.d.ts +7 -0
- package/dist/locks.d.ts.map +1 -0
- package/dist/locks.js +34 -0
- package/dist/lost.d.ts +5 -0
- package/dist/lost.d.ts.map +1 -0
- package/dist/lost.js +49 -0
- package/dist/media.d.ts +20 -4
- package/dist/media.d.ts.map +1 -1
- package/dist/media.js +183 -39
- package/dist/memory.d.ts +21 -8
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +262 -109
- package/dist/messages.d.ts +40 -5
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +118 -39
- package/dist/modal.d.ts +2 -0
- package/dist/modal.d.ts.map +1 -0
- package/dist/modal.js +11 -0
- package/dist/model.d.ts +6 -13
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +6 -23
- package/dist/models.d.ts +10 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +153 -0
- package/dist/name.d.ts +0 -1
- package/dist/name.d.ts.map +1 -1
- package/dist/name.js +3 -7
- package/dist/observation.d.ts +30 -13
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +218 -81
- package/dist/online.d.ts +7 -0
- package/dist/online.d.ts.map +1 -0
- package/dist/online.js +29 -0
- package/dist/opened.d.ts +1 -1
- package/dist/opened.d.ts.map +1 -1
- package/dist/opened.js +5 -5
- package/dist/opening.d.ts +4 -0
- package/dist/opening.d.ts.map +1 -0
- package/dist/opening.js +69 -0
- package/dist/outcome.d.ts +28 -8
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +36 -3
- package/dist/overlay.d.ts +10 -5
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +48 -24
- package/dist/page-metadata.d.ts +8 -0
- package/dist/page-metadata.d.ts.map +1 -0
- package/dist/page-metadata.js +19 -0
- package/dist/parts.d.ts +6 -0
- package/dist/parts.d.ts.map +1 -0
- package/dist/parts.js +36 -0
- package/dist/paths.d.ts +1 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +22 -0
- package/dist/preparation.d.ts +46 -0
- package/dist/preparation.d.ts.map +1 -0
- package/dist/preparation.js +344 -0
- package/dist/presence.d.ts +58 -0
- package/dist/presence.d.ts.map +1 -0
- package/dist/presence.js +231 -0
- package/dist/queries.d.ts +16 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +287 -0
- package/dist/query.d.ts +16 -212
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -505
- package/dist/questions.d.ts +30 -0
- package/dist/questions.d.ts.map +1 -0
- package/dist/questions.js +117 -0
- package/dist/queue.d.ts +40 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +480 -0
- package/dist/reach.d.ts +26 -15
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +350 -160
- package/dist/react.d.ts +21 -3
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +33 -1
- package/dist/read.d.ts +7 -0
- package/dist/read.d.ts.map +1 -0
- package/dist/read.js +17 -0
- package/dist/readiness.d.ts +23 -22
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +21 -20
- package/dist/reads.d.ts +38 -10
- package/dist/reads.d.ts.map +1 -1
- package/dist/reads.js +163 -85
- package/dist/recovery.d.ts +4 -0
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +59 -2
- package/dist/reference-forms.d.ts +4 -0
- package/dist/reference-forms.d.ts.map +1 -0
- package/dist/reference-forms.js +74 -0
- package/dist/refresh.d.ts +7 -5
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +37 -9
- package/dist/refusal.d.ts +5 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +55 -0
- package/dist/replica.d.ts +12 -18
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +68 -38
- package/dist/retried.d.ts +30 -0
- package/dist/retried.d.ts.map +1 -0
- package/dist/retried.js +72 -0
- package/dist/room-draft.d.ts +51 -12
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +368 -111
- package/dist/room-drafts.d.ts +24 -0
- package/dist/room-drafts.d.ts.map +1 -0
- package/dist/room-drafts.js +222 -0
- package/dist/room-list.d.ts +10 -0
- package/dist/room-list.d.ts.map +1 -0
- package/dist/room-list.js +45 -0
- package/dist/rows.d.ts +16 -22
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +51 -187
- package/dist/running.d.ts +5 -0
- package/dist/running.d.ts.map +1 -0
- package/dist/running.js +10 -0
- package/dist/runtime.d.ts +35 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +1 -0
- package/dist/save.d.ts +43 -86
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +332 -228
- package/dist/saves.d.ts +34 -0
- package/dist/saves.d.ts.map +1 -0
- package/dist/saves.js +457 -0
- package/dist/search.d.ts +12 -63
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +9 -52
- package/dist/seed.d.ts +11 -2
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +71 -1
- package/dist/shared.d.ts +1 -7
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +13 -105
- package/dist/shown.d.ts +35 -0
- package/dist/shown.d.ts.map +1 -0
- package/dist/shown.js +326 -0
- package/dist/socket/admission.d.ts +7 -0
- package/dist/socket/admission.d.ts.map +1 -0
- package/dist/socket/admission.js +39 -0
- package/dist/socket/answers.d.ts +6 -0
- package/dist/socket/answers.d.ts.map +1 -0
- package/dist/socket/answers.js +20 -0
- package/dist/socket/lanes.d.ts +28 -0
- package/dist/socket/lanes.d.ts.map +1 -0
- package/dist/socket/lanes.js +103 -0
- package/dist/socket/reads.d.ts +10 -0
- package/dist/socket/reads.d.ts.map +1 -0
- package/dist/socket/reads.js +31 -0
- package/dist/socket/session.d.ts +41 -0
- package/dist/socket/session.d.ts.map +1 -0
- package/dist/socket/session.js +26 -0
- package/dist/socket.d.ts +68 -0
- package/dist/socket.d.ts.map +1 -0
- package/dist/socket.js +165 -0
- package/dist/source.d.ts +61 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +95 -0
- package/dist/status.d.ts +0 -6
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +0 -6
- package/dist/store/installs.d.ts +30 -0
- package/dist/store/installs.d.ts.map +1 -0
- package/dist/store/installs.js +40 -0
- package/dist/store/readiness.d.ts +11 -0
- package/dist/store/readiness.d.ts.map +1 -0
- package/dist/store/readiness.js +68 -0
- package/dist/store/records.d.ts +227 -0
- package/dist/store/records.d.ts.map +1 -0
- package/dist/store/records.js +468 -0
- package/dist/store/room-held.d.ts +8 -0
- package/dist/store/room-held.d.ts.map +1 -0
- package/dist/store/room-held.js +17 -0
- package/dist/store/rooms-shown.d.ts +8 -0
- package/dist/store/rooms-shown.d.ts.map +1 -0
- package/dist/store/rooms-shown.js +13 -0
- package/dist/store/space.d.ts +11 -0
- package/dist/store/space.d.ts.map +1 -0
- package/dist/store/space.js +41 -0
- package/dist/store/watchers.d.ts +17 -0
- package/dist/store/watchers.d.ts.map +1 -0
- package/dist/store/watchers.js +56 -0
- package/dist/store/writes.d.ts +24 -0
- package/dist/store/writes.d.ts.map +1 -0
- package/dist/store/writes.js +125 -0
- package/dist/store.d.ts +125 -175
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +173 -419
- package/dist/stored.d.ts +81 -61
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +456 -295
- package/dist/surface.d.ts +94 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +255 -0
- package/dist/sync.d.ts +21 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +557 -0
- package/dist/tables.d.ts +119 -104
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +201 -328
- package/dist/tallies.d.ts +18 -0
- package/dist/tallies.d.ts.map +1 -0
- package/dist/tallies.js +128 -0
- package/dist/transfer.d.ts +0 -5
- package/dist/transfer.d.ts.map +1 -1
- package/dist/validation.d.ts +3 -4
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +42 -106
- package/dist/windows.d.ts +1 -12
- package/dist/windows.d.ts.map +1 -1
- package/dist/windows.js +2 -75
- package/dist/wire.d.ts +85 -14
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +37 -15
- package/package.json +3 -2
- package/dist/attempt.d.ts +0 -16
- package/dist/attempt.d.ts.map +0 -1
- package/dist/attempt.js +0 -70
- package/dist/fence.d.ts +0 -6
- package/dist/fence.d.ts.map +0 -1
- package/dist/fence.js +0 -10
- package/dist/fulltext.d.ts +0 -27
- package/dist/fulltext.d.ts.map +0 -1
- package/dist/fulltext.js +0 -368
- package/dist/log.d.ts +0 -5
- package/dist/log.d.ts.map +0 -1
- package/dist/log.js +0 -14
- package/dist/words.d.ts +0 -19
- package/dist/words.d.ts.map +0 -1
- package/dist/words.js +0 -156
package/dist/stored.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
+
import { instantKey, sql } from '@agentshouse/mdmodel';
|
|
1
2
|
import { composedOf } from './collection.js';
|
|
2
|
-
import {
|
|
3
|
-
import { described, derived } from './rows.js';
|
|
3
|
+
import { described, derived, heldOf } from './rows.js';
|
|
4
4
|
export const REPLICA_TABLES = [
|
|
5
|
-
'CREATE TABLE replica_keys (key INTEGER PRIMARY KEY AUTOINCREMENT, locator TEXT NOT NULL, transfer TEXT NOT NULL, fingerprint TEXT NOT NULL, rederiving INTEGER
|
|
6
|
-
'CREATE
|
|
7
|
-
'CREATE TABLE
|
|
8
|
-
'CREATE
|
|
9
|
-
|
|
10
|
-
'CREATE TABLE
|
|
5
|
+
'CREATE TABLE replica_keys (key INTEGER PRIMARY KEY AUTOINCREMENT, locator TEXT NOT NULL, transfer TEXT NOT NULL, fingerprint TEXT NOT NULL, rederiving INTEGER)',
|
|
6
|
+
'CREATE INDEX replica_keys_locator ON replica_keys (locator)',
|
|
7
|
+
'CREATE TABLE replica_rooms (locator TEXT PRIMARY KEY, key INTEGER NOT NULL, record TEXT NOT NULL, documents INTEGER NOT NULL DEFAULT 0) WITHOUT ROWID',
|
|
8
|
+
'CREATE INDEX replica_rooms_key ON replica_rooms (key)',
|
|
9
|
+
"CREATE INDEX replica_rooms_model ON replica_rooms (json_extract(record, '$.model.snapshot.identity'), json_extract(record, '$.model.snapshot.version'))",
|
|
10
|
+
'CREATE TABLE replica_models (identity TEXT NOT NULL, version TEXT NOT NULL, schema TEXT NOT NULL, PRIMARY KEY (identity, version)) WITHOUT ROWID',
|
|
11
|
+
"CREATE TABLE replica_entries (id INTEGER PRIMARY KEY, key INTEGER NOT NULL, layer INTEGER NOT NULL, path TEXT NOT NULL, visible INTEGER NOT NULL, present INTEGER NOT NULL, binary TEXT, revision TEXT, file_id TEXT, change TEXT, written TEXT, type TEXT, declared TEXT, folder TEXT NOT NULL, fields TEXT, links TEXT, part TEXT, created TEXT, created_instant TEXT, changed_instant TEXT, bytes INTEGER GENERATED ALWAYS AS (CASE WHEN binary IS NULL THEN length(CAST(content AS BLOB)) ELSE binary ->> '$.byteLength' END) STORED, content TEXT)",
|
|
11
12
|
'CREATE UNIQUE INDEX replica_entries_path ON replica_entries (key, layer, path)',
|
|
12
13
|
'CREATE INDEX replica_entries_visible ON replica_entries (key, visible, present, path, binary)',
|
|
13
14
|
'CREATE INDEX replica_entries_folder ON replica_entries (key, folder, path)',
|
|
15
|
+
'CREATE INDEX replica_entries_type ON replica_entries (key, type, visible, present, folder)',
|
|
14
16
|
'CREATE INDEX replica_entries_key ON replica_entries (key)',
|
|
15
|
-
|
|
17
|
+
sql.VALUE_TABLE,
|
|
16
18
|
'CREATE TABLE replica_links (doc INTEGER NOT NULL, key INTEGER NOT NULL, kind TEXT NOT NULL, keys TEXT NOT NULL, places TEXT NOT NULL, target TEXT NOT NULL, field TEXT NOT NULL, PRIMARY KEY (doc, kind, keys, places, target)) WITHOUT ROWID',
|
|
17
19
|
'CREATE INDEX replica_links_target ON replica_links (key, target, kind, keys)',
|
|
18
|
-
'CREATE TABLE replica_folders (key INTEGER NOT NULL, layer INTEGER NOT NULL, path TEXT NOT NULL, present INTEGER NOT NULL, PRIMARY KEY (key, layer, path)) WITHOUT ROWID',
|
|
20
|
+
'CREATE TABLE replica_folders (key INTEGER NOT NULL, layer INTEGER NOT NULL, path TEXT NOT NULL, present INTEGER NOT NULL, part TEXT, PRIMARY KEY (key, layer, path)) WITHOUT ROWID',
|
|
19
21
|
'CREATE TABLE replica_erasing (key INTEGER PRIMARY KEY, locator TEXT NOT NULL)',
|
|
20
|
-
'CREATE TABLE replica_received (key TEXT PRIMARY KEY, head TEXT NOT NULL) WITHOUT ROWID',
|
|
21
|
-
'CREATE TABLE replica_received_parts (key TEXT NOT NULL, part INTEGER NOT NULL, value TEXT NOT NULL, PRIMARY KEY (key, part)) WITHOUT ROWID',
|
|
22
22
|
'CREATE TABLE replica_facts (name TEXT PRIMARY KEY, value TEXT NOT NULL) WITHOUT ROWID',
|
|
23
23
|
'CREATE TABLE replica_presentation (app TEXT PRIMARY KEY, value TEXT NOT NULL) WITHOUT ROWID',
|
|
24
24
|
];
|
|
@@ -27,14 +27,45 @@ const OVERLAY = 1;
|
|
|
27
27
|
const REFRESHED = 2;
|
|
28
28
|
const DERIVED_ROWS = 64;
|
|
29
29
|
const ENTRY_WEIGHT = 256;
|
|
30
|
+
const INSERTED_COLUMNS = [
|
|
31
|
+
'key',
|
|
32
|
+
'layer',
|
|
33
|
+
'path',
|
|
34
|
+
'visible',
|
|
35
|
+
'present',
|
|
36
|
+
'content',
|
|
37
|
+
'binary',
|
|
38
|
+
'revision',
|
|
39
|
+
'file_id',
|
|
40
|
+
'created',
|
|
41
|
+
'created_instant',
|
|
42
|
+
'change',
|
|
43
|
+
'changed_instant',
|
|
44
|
+
'written',
|
|
45
|
+
'type',
|
|
46
|
+
'declared',
|
|
47
|
+
'folder',
|
|
48
|
+
'fields',
|
|
49
|
+
'links',
|
|
50
|
+
'part',
|
|
51
|
+
];
|
|
52
|
+
const INSERT_ENTRY = `INSERT INTO replica_entries (${INSERTED_COLUMNS.join(', ')}) VALUES (${INSERTED_COLUMNS.map(() => '?').join(', ')})`;
|
|
53
|
+
const INSERT_ENTRIES = `INSERT INTO replica_entries (${INSERTED_COLUMNS.join(', ')}) SELECT ${INSERTED_COLUMNS.map((_, at) => `value ->> ${at}`).join(', ')} FROM json_each(?)`;
|
|
54
|
+
const UNHELD = {
|
|
55
|
+
content: null,
|
|
56
|
+
binary: null,
|
|
57
|
+
revision: null,
|
|
58
|
+
fileId: null,
|
|
59
|
+
created: null,
|
|
60
|
+
change: null,
|
|
61
|
+
written: null,
|
|
62
|
+
};
|
|
30
63
|
const ABSENT = {
|
|
31
64
|
type: null,
|
|
32
65
|
declared: null,
|
|
33
|
-
queried: false,
|
|
34
66
|
folder: '',
|
|
35
67
|
fields: null,
|
|
36
|
-
|
|
37
|
-
links: [],
|
|
68
|
+
links: null,
|
|
38
69
|
};
|
|
39
70
|
function text(value) {
|
|
40
71
|
return typeof value === 'string' ? value : null;
|
|
@@ -55,87 +86,91 @@ function dropDerived(connection, doc) {
|
|
|
55
86
|
connection.run('DELETE FROM replica_values WHERE doc = ?', [doc]);
|
|
56
87
|
connection.run('DELETE FROM replica_links WHERE doc = ?', [doc]);
|
|
57
88
|
}
|
|
58
|
-
function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const rows = made.values.map((value) => `[${JSON.stringify(value.keys)},${JSON.stringify(value.places)},${value.item},${JSON.stringify(value.kind)},${scalarText(value.value)},${JSON.stringify(value.size)}]`);
|
|
68
|
-
connection.run('INSERT INTO replica_values (doc, keys, places, item, kind, value, size) SELECT ?, value ->> 0, value ->> 1, value ->> 2, value ->> 3, value ->> 4, value ->> 5 FROM json_each(?)', [doc, `[${rows.join(',')}]`]);
|
|
89
|
+
function writeDerived(connection, key, composition, documents) {
|
|
90
|
+
const made = documents.map(({ doc, path, held }) => ({
|
|
91
|
+
doc,
|
|
92
|
+
...derived(path, held, composition),
|
|
93
|
+
}));
|
|
94
|
+
const valued = made.filter(({ values }) => values.length > 0);
|
|
95
|
+
if (valued.length > 0) {
|
|
96
|
+
const inserted = sql.insertedValues(valued);
|
|
97
|
+
connection.run(inserted.sql, inserted.bind);
|
|
69
98
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
key,
|
|
74
|
-
JSON.stringify(made.links.map((link) => [link.kind, link.keys, link.places, link.target, link.field])),
|
|
75
|
-
]);
|
|
99
|
+
const links = made.flatMap(({ doc, links: held }) => held.map((link) => [doc, link.kind, link.keys, link.places, link.target, link.field]));
|
|
100
|
+
if (links.length > 0) {
|
|
101
|
+
connection.run('INSERT OR IGNORE INTO replica_links (doc, key, kind, keys, places, target, field) SELECT value ->> 0, ?, value ->> 1, value ->> 2, value ->> 3, value ->> 4, value ->> 5 FROM json_each(?)', [key, JSON.stringify(links)]);
|
|
76
102
|
}
|
|
77
103
|
}
|
|
78
104
|
function entryId(connection, key, layer, path) {
|
|
79
105
|
const [row] = connection.rows('SELECT id FROM replica_entries WHERE key = ? AND layer = ? AND path = ?', [key, layer, path]);
|
|
80
106
|
return row === undefined ? null : number(row.id);
|
|
81
107
|
}
|
|
82
|
-
function
|
|
83
|
-
return { dropped: [], made: [] };
|
|
84
|
-
}
|
|
85
|
-
function wordsWritten(connection, owed) {
|
|
86
|
-
unindexDocuments(connection, owed.dropped);
|
|
87
|
-
for (const { id, path, content } of owed.made)
|
|
88
|
-
indexDocument(connection, id, path, content);
|
|
89
|
-
}
|
|
90
|
-
function removeEntry(connection, key, layer, path, owed) {
|
|
108
|
+
function removeEntry(connection, key, layer, path) {
|
|
91
109
|
const id = entryId(connection, key, layer, path);
|
|
92
110
|
if (id === null)
|
|
93
111
|
return;
|
|
94
112
|
dropDerived(connection, id);
|
|
95
|
-
owed.dropped.push(id);
|
|
96
113
|
connection.run('DELETE FROM replica_entries WHERE id = ?', [id]);
|
|
97
114
|
}
|
|
98
|
-
function
|
|
99
|
-
return
|
|
100
|
-
}
|
|
101
|
-
function insertEntry(connection, key, layer, entry, present, made) {
|
|
102
|
-
connection.run('INSERT INTO replica_entries (key, layer, path, visible, present, queried, content, binary, revision, file_id, change, written, type, declared, folder, fields) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [
|
|
115
|
+
function entryRow(key, layer, { entry, present, made, part }) {
|
|
116
|
+
return [
|
|
103
117
|
key,
|
|
104
118
|
layer,
|
|
105
119
|
entry.path,
|
|
106
120
|
flag(present && layer !== REFRESHED),
|
|
107
121
|
flag(present),
|
|
108
|
-
flag(made.queried),
|
|
109
122
|
entry.binary === null ? entry.content : null,
|
|
110
123
|
entry.binary === null ? null : JSON.stringify(entry.binary),
|
|
111
124
|
entry.revision,
|
|
112
125
|
entry.fileId,
|
|
126
|
+
entry.created,
|
|
127
|
+
instantKey(entry.created),
|
|
113
128
|
entry.change === null ? null : JSON.stringify(entry.change),
|
|
129
|
+
instantKey(entry.change?.time),
|
|
114
130
|
entry.written,
|
|
115
131
|
made.type,
|
|
116
132
|
made.declared,
|
|
117
133
|
made.folder,
|
|
118
134
|
made.fields,
|
|
135
|
+
made.links,
|
|
136
|
+
part,
|
|
137
|
+
];
|
|
138
|
+
}
|
|
139
|
+
function insertEntry(connection, key, layer, inserted) {
|
|
140
|
+
connection.run(INSERT_ENTRY, entryRow(key, layer, inserted));
|
|
141
|
+
}
|
|
142
|
+
function insertEntries(connection, key, layer, inserted) {
|
|
143
|
+
connection.run(INSERT_ENTRIES, [
|
|
144
|
+
JSON.stringify(inserted.map((row) => entryRow(key, layer, row))),
|
|
119
145
|
]);
|
|
120
146
|
}
|
|
121
147
|
function textOf(entry) {
|
|
122
148
|
return entry.binary === null ? entry.content : null;
|
|
123
149
|
}
|
|
124
|
-
function putEntry(connection, key, layer, entry, present, composition
|
|
125
|
-
removeEntry(connection, key, layer, entry.path
|
|
150
|
+
function putEntry(connection, key, layer, entry, present, composition) {
|
|
151
|
+
removeEntry(connection, key, layer, entry.path);
|
|
126
152
|
const content = textOf(entry);
|
|
127
|
-
|
|
128
|
-
|
|
153
|
+
insertEntry(connection, key, layer, {
|
|
154
|
+
entry,
|
|
155
|
+
present,
|
|
156
|
+
made: present ? described(entry.path, content, entry.facts) : ABSENT,
|
|
157
|
+
part: null,
|
|
158
|
+
});
|
|
129
159
|
if (!present)
|
|
130
160
|
return;
|
|
131
161
|
const id = entryId(connection, key, layer, entry.path);
|
|
132
|
-
writeDerived(connection,
|
|
133
|
-
|
|
134
|
-
|
|
162
|
+
writeDerived(connection, key, composition, [
|
|
163
|
+
{ doc: id, path: entry.path, held: heldOf(entry.path, content, entry.facts) },
|
|
164
|
+
]);
|
|
135
165
|
}
|
|
136
|
-
function placeEntry(connection, key, entry,
|
|
137
|
-
removeEntry(connection, key, CONFIRMED, entry.path
|
|
138
|
-
insertEntry(connection, key, CONFIRMED,
|
|
166
|
+
function placeEntry(connection, key, entry, part) {
|
|
167
|
+
removeEntry(connection, key, CONFIRMED, entry.path);
|
|
168
|
+
insertEntry(connection, key, CONFIRMED, {
|
|
169
|
+
entry,
|
|
170
|
+
present: true,
|
|
171
|
+
made: described(entry.path, textOf(entry), entry.facts),
|
|
172
|
+
part,
|
|
173
|
+
});
|
|
139
174
|
}
|
|
140
175
|
function revealed(connection, key, touched) {
|
|
141
176
|
if (touched.length === 0)
|
|
@@ -144,7 +179,7 @@ function revealed(connection, key, touched) {
|
|
|
144
179
|
}
|
|
145
180
|
function newKey(connection, locator, transfer, fingerprint, built) {
|
|
146
181
|
const owed = built ? null : 0;
|
|
147
|
-
connection.run('INSERT INTO replica_keys (locator, transfer, fingerprint, rederiving
|
|
182
|
+
connection.run('INSERT INTO replica_keys (locator, transfer, fingerprint, rederiving) VALUES (?, ?, ?, ?)', [locator, transfer, fingerprint, owed]);
|
|
148
183
|
const [row] = connection.rows('SELECT last_insert_rowid() AS key');
|
|
149
184
|
return number(row.key);
|
|
150
185
|
}
|
|
@@ -155,54 +190,121 @@ function recomposed(connection, key, fingerprint) {
|
|
|
155
190
|
const held = connection.rows('SELECT 1 FROM replica_entries WHERE key = ? LIMIT 1', [key]);
|
|
156
191
|
connection.run(`UPDATE replica_keys SET fingerprint = ?${held.length === 0 ? '' : ', rederiving = 0'} WHERE key = ?`, [fingerprint, key]);
|
|
157
192
|
}
|
|
158
|
-
|
|
193
|
+
function enteredRows(connection, locator, composition, rows) {
|
|
159
194
|
const key = newKey(connection, locator, '', composedOf(composition).fingerprint, true);
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
195
|
+
insertEntries(connection, key, CONFIRMED, rows.map(({ entry, made }) => ({ entry, present: true, made, part: null })));
|
|
196
|
+
const ids = new Map(connection
|
|
197
|
+
.rows('SELECT id, path FROM replica_entries WHERE key = ?', [key])
|
|
198
|
+
.map((row) => [text(row.path), number(row.id)]));
|
|
199
|
+
writeDerived(connection, key, composition, rows.map(({ entry, held }) => ({ doc: ids.get(entry.path), path: entry.path, held })));
|
|
165
200
|
return key;
|
|
166
201
|
}
|
|
167
|
-
function
|
|
168
|
-
connection
|
|
169
|
-
|
|
202
|
+
export function entered(connection, locator, composition, entries) {
|
|
203
|
+
return enteredRows(connection, locator, composition, entries.map((entry) => {
|
|
204
|
+
const content = textOf(entry);
|
|
205
|
+
return {
|
|
206
|
+
entry,
|
|
207
|
+
held: heldOf(entry.path, content, entry.facts),
|
|
208
|
+
made: described(entry.path, content, entry.facts),
|
|
209
|
+
};
|
|
210
|
+
}));
|
|
211
|
+
}
|
|
212
|
+
export function enteredValues(connection, locator, composition, documents) {
|
|
213
|
+
return enteredRows(connection, locator, composition, documents.map(({ path, type, fields }) => {
|
|
214
|
+
const held = { type, fields, links: [] };
|
|
215
|
+
return {
|
|
216
|
+
entry: { ...UNHELD, path, facts: held },
|
|
217
|
+
held,
|
|
218
|
+
made: described(path, '', held),
|
|
219
|
+
};
|
|
220
|
+
}));
|
|
170
221
|
}
|
|
171
222
|
export function stage(connection, request) {
|
|
172
|
-
|
|
173
|
-
request.locator
|
|
174
|
-
|
|
223
|
+
if (request.key === null) {
|
|
224
|
+
for (const row of connection.rows(STAGED_KEYS, [request.locator])) {
|
|
225
|
+
erasing(connection, number(row.key), request.locator);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
175
228
|
const key = request.key ??
|
|
176
|
-
(
|
|
177
|
-
? newKey(connection, request.locator, request.transfer, request.fingerprint, false)
|
|
178
|
-
: number(held.key));
|
|
229
|
+
newKey(connection, request.locator, request.transfer, request.fingerprint, false);
|
|
179
230
|
recomposed(connection, key, request.fingerprint);
|
|
180
|
-
connection
|
|
181
|
-
connection.run('INSERT OR REPLACE INTO replica_transfers (locator, key, record) VALUES (?, ?, ?)', [request.locator, key, request.record]);
|
|
182
|
-
if (request.written !== undefined) {
|
|
183
|
-
connection.run('DELETE FROM replica_transfer_parts WHERE locator = ? AND token = ?', [
|
|
184
|
-
request.locator,
|
|
185
|
-
request.written,
|
|
186
|
-
]);
|
|
187
|
-
}
|
|
188
|
-
const owed = words();
|
|
231
|
+
owing(connection, key);
|
|
189
232
|
for (const entry of request.entries)
|
|
190
|
-
placeEntry(connection, key, entry,
|
|
233
|
+
placeEntry(connection, key, entry, request.part);
|
|
191
234
|
for (const path of request.folders) {
|
|
192
|
-
connection.run('INSERT OR REPLACE INTO replica_folders (key, layer, path, present) VALUES (?, ?, ?, 1)', [key, CONFIRMED, path]);
|
|
235
|
+
connection.run('INSERT OR REPLACE INTO replica_folders (key, layer, path, present, part) VALUES (?, ?, ?, 1, ?)', [key, CONFIRMED, path, request.part]);
|
|
193
236
|
}
|
|
194
|
-
wordsWritten(connection, owed);
|
|
195
237
|
return key;
|
|
196
238
|
}
|
|
239
|
+
function owing(connection, key) {
|
|
240
|
+
connection.run('UPDATE replica_keys SET rederiving = coalesce(rederiving, last) FROM (SELECT coalesce(max(id), 0) AS last FROM replica_entries) WHERE key = ?', [key]);
|
|
241
|
+
}
|
|
242
|
+
const STAGED_KEYS = `SELECT k.key, k.transfer FROM replica_keys k WHERE k.locator = ?
|
|
243
|
+
AND NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
244
|
+
AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)
|
|
245
|
+
ORDER BY k.key DESC`;
|
|
246
|
+
const UNPARTED = `EXISTS (SELECT 1 FROM replica_entries e WHERE e.key = k.key AND e.part IS NULL)
|
|
247
|
+
OR EXISTS (SELECT 1 FROM replica_folders f WHERE f.key = k.key AND f.part IS NULL)`;
|
|
248
|
+
function partsOf(connection, key, layer) {
|
|
249
|
+
const scoped = layer === null ? '' : ` AND layer = ${layer}`;
|
|
250
|
+
return new Set(connection
|
|
251
|
+
.rows(`SELECT part FROM replica_entries WHERE key = ?${scoped} AND part IS NOT NULL UNION SELECT part FROM replica_folders WHERE key = ?${scoped} AND part IS NOT NULL`, [key, key])
|
|
252
|
+
.map((row) => text(row.part)));
|
|
253
|
+
}
|
|
254
|
+
function resumable(connection, locator) {
|
|
255
|
+
let kept = null;
|
|
256
|
+
for (const row of connection.rows(STAGED_KEYS, [locator])) {
|
|
257
|
+
const key = number(row.key);
|
|
258
|
+
const parted = kept === null &&
|
|
259
|
+
connection.rows(`SELECT 1 FROM replica_keys k WHERE k.key = ? AND NOT (${UNPARTED})`, [key])
|
|
260
|
+
.length > 0;
|
|
261
|
+
if (parted)
|
|
262
|
+
kept = { key, transfer: text(row.transfer) };
|
|
263
|
+
else
|
|
264
|
+
erasing(connection, key, locator);
|
|
265
|
+
}
|
|
266
|
+
return kept;
|
|
267
|
+
}
|
|
268
|
+
export function resume(connection, request) {
|
|
269
|
+
const wanted = new Set(request.parts);
|
|
270
|
+
const held = resumable(connection, request.locator);
|
|
271
|
+
const staged = held === null ? new Set() : partsOf(connection, held.key, null);
|
|
272
|
+
const dropped = [...staged].filter((part) => !wanted.has(part));
|
|
273
|
+
if (held !== null && dropped.length > 0) {
|
|
274
|
+
const named = JSON.stringify(dropped);
|
|
275
|
+
const rows = `SELECT id FROM replica_entries WHERE key = ? AND part IN (SELECT value FROM json_each(?))`;
|
|
276
|
+
for (const table of ['replica_values', 'replica_links']) {
|
|
277
|
+
connection.run(`DELETE FROM ${table} WHERE doc IN (${rows})`, [held.key, named]);
|
|
278
|
+
}
|
|
279
|
+
connection.run(`DELETE FROM replica_entries WHERE id IN (${rows})`, [held.key, named]);
|
|
280
|
+
connection.run('DELETE FROM replica_folders WHERE key = ? AND part IN (SELECT value FROM json_each(?))', [held.key, named]);
|
|
281
|
+
}
|
|
282
|
+
const kept = [...staged].filter((part) => wanted.has(part));
|
|
283
|
+
const publishedParts = request.published === null
|
|
284
|
+
? new Set()
|
|
285
|
+
: partsOf(connection, request.published, CONFIRMED);
|
|
286
|
+
const copied = request.parts.filter((part) => !staged.has(part) && publishedParts.has(part));
|
|
287
|
+
if (held === null && copied.length === 0) {
|
|
288
|
+
return { key: null, transfer: request.transfer, staged: [] };
|
|
289
|
+
}
|
|
290
|
+
const key = held?.key ?? newKey(connection, request.locator, request.transfer, request.fingerprint, false);
|
|
291
|
+
recomposed(connection, key, request.fingerprint);
|
|
292
|
+
if (copied.length > 0) {
|
|
293
|
+
owing(connection, key);
|
|
294
|
+
const named = JSON.stringify(copied);
|
|
295
|
+
connection.run(`INSERT INTO replica_entries (key, layer, path, visible, present, content, binary, revision, file_id, created, created_instant, change, changed_instant, written, type, declared, folder, fields, links, part) SELECT ?, ${CONFIRMED}, path, 1, 1, content, binary, revision, file_id, created, created_instant, change, changed_instant, written, type, declared, folder, fields, links, part FROM replica_entries WHERE key = ? AND layer = ${CONFIRMED} AND present = 1 AND part IN (SELECT value FROM json_each(?)) ORDER BY path`, [key, request.published, named]);
|
|
296
|
+
connection.run(`INSERT OR REPLACE INTO replica_folders (key, layer, path, present, part) SELECT ?, ${CONFIRMED}, path, 1, part FROM replica_folders WHERE key = ? AND layer = ${CONFIRMED} AND present = 1 AND part IN (SELECT value FROM json_each(?))`, [key, request.published, named]);
|
|
297
|
+
}
|
|
298
|
+
return { key, transfer: held?.transfer ?? request.transfer, staged: [...kept, ...copied] };
|
|
299
|
+
}
|
|
197
300
|
export function refreshing(connection, request) {
|
|
198
|
-
const held = connection.rows('SELECT 1 FROM
|
|
301
|
+
const held = connection.rows('SELECT 1 FROM replica_keys k WHERE k.key = ? AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)', [request.key]);
|
|
199
302
|
if (held.length === 0)
|
|
200
303
|
return null;
|
|
201
304
|
recomposed(connection, request.key, request.composed.fingerprint);
|
|
202
305
|
const composition = request.composed.composition;
|
|
203
|
-
const owed = words();
|
|
204
306
|
for (const entry of request.entries) {
|
|
205
|
-
putEntry(connection, request.key, REFRESHED, entry, true, composition
|
|
307
|
+
putEntry(connection, request.key, REFRESHED, entry, true, composition);
|
|
206
308
|
}
|
|
207
309
|
for (const path of request.removals) {
|
|
208
310
|
putEntry(connection, request.key, REFRESHED, {
|
|
@@ -211,9 +313,11 @@ export function refreshing(connection, request) {
|
|
|
211
313
|
binary: null,
|
|
212
314
|
revision: null,
|
|
213
315
|
fileId: null,
|
|
316
|
+
created: null,
|
|
214
317
|
change: null,
|
|
215
318
|
written: null,
|
|
216
|
-
|
|
319
|
+
facts: null,
|
|
320
|
+
}, false, composition);
|
|
217
321
|
}
|
|
218
322
|
for (const [list, present] of [
|
|
219
323
|
[request.folders, 1],
|
|
@@ -223,11 +327,9 @@ export function refreshing(connection, request) {
|
|
|
223
327
|
connection.run('INSERT OR REPLACE INTO replica_folders (key, layer, path, present) VALUES (?, ?, ?, ?)', [request.key, REFRESHED, path, present]);
|
|
224
328
|
}
|
|
225
329
|
}
|
|
226
|
-
wordsWritten(connection, owed);
|
|
227
330
|
return null;
|
|
228
331
|
}
|
|
229
332
|
function dropLayer(connection, key, layer) {
|
|
230
|
-
unindexDocuments(connection, heldIds(connection, 'SELECT id FROM replica_entries WHERE key = ? AND layer = ?', [key, layer]));
|
|
231
333
|
for (const table of ['replica_values', 'replica_links']) {
|
|
232
334
|
connection.run(`DELETE FROM ${table} WHERE doc IN (SELECT id FROM replica_entries WHERE key = ? AND layer = ?)`, [key, layer]);
|
|
233
335
|
}
|
|
@@ -246,7 +348,6 @@ function refreshedPaths(connection, key) {
|
|
|
246
348
|
function applyRefresh(connection, key) {
|
|
247
349
|
const touched = refreshedPaths(connection, key);
|
|
248
350
|
const replaced = `SELECT c.id FROM replica_entries c JOIN replica_entries r ON r.key = c.key AND r.layer = ${REFRESHED} AND r.path = c.path WHERE c.key = ? AND c.layer = ${CONFIRMED}`;
|
|
249
|
-
unindexDocuments(connection, heldIds(connection, replaced, [key]));
|
|
250
351
|
for (const table of ['replica_values', 'replica_links']) {
|
|
251
352
|
connection.run(`DELETE FROM ${table} WHERE doc IN (${replaced})`, [key]);
|
|
252
353
|
}
|
|
@@ -265,111 +366,134 @@ function applyRefresh(connection, key) {
|
|
|
265
366
|
connection.run('DELETE FROM replica_folders WHERE key = ? AND layer = ?', [key, REFRESHED]);
|
|
266
367
|
return touched;
|
|
267
368
|
}
|
|
369
|
+
function beneathRoots(path) {
|
|
370
|
+
return `EXISTS (SELECT 1 FROM json_each(?) r WHERE ${path} = r.value OR (${path} >= r.value || '/' AND ${path} < r.value || '0'))`;
|
|
371
|
+
}
|
|
268
372
|
function counted(connection, key, roots) {
|
|
269
|
-
const
|
|
270
|
-
const bind = [key];
|
|
271
|
-
for (const root of roots)
|
|
272
|
-
bind.push(root, `${root}/`, `${root}0`);
|
|
273
|
-
const [row] = connection.rows(`SELECT count(*) AS held FROM replica_entries WHERE key = ? AND visible = 1 AND present = 1 AND (binary IS NOT NULL OR substr(path, -3) = '.md')${roots.length === 0 ? '' : ` AND NOT (${outside})`}`, bind);
|
|
373
|
+
const [row] = connection.rows(`SELECT count(*) AS held FROM replica_entries WHERE key = ? AND visible = 1 AND present = 1 AND (binary IS NOT NULL OR substr(path, -3) = '.md') AND NOT ${beneathRoots('replica_entries.path')}`, [key, JSON.stringify(roots)]);
|
|
274
374
|
return number(row.held);
|
|
275
375
|
}
|
|
276
|
-
export function counts(connection, rooms) {
|
|
277
|
-
return rooms.map(({ key, roots }) => counted(connection, key, roots));
|
|
278
|
-
}
|
|
279
376
|
function erasing(connection, key, locator) {
|
|
280
377
|
connection.run('INSERT OR REPLACE INTO replica_erasing (key, locator) VALUES (?, ?)', [
|
|
281
378
|
key,
|
|
282
379
|
locator,
|
|
283
380
|
]);
|
|
284
381
|
}
|
|
382
|
+
function saved(connection, key, entry, at) {
|
|
383
|
+
if (!entry.present || entry.created !== null)
|
|
384
|
+
return entry;
|
|
385
|
+
const [held] = connection.rows(`SELECT created FROM replica_entries WHERE key = ? AND layer = ${OVERLAY} AND path = ? AND present = 1`, [key, entry.path]);
|
|
386
|
+
return { ...entry, created: text(held?.created) ?? at };
|
|
387
|
+
}
|
|
285
388
|
function commitRoom(connection, room) {
|
|
286
389
|
const touched = new Set();
|
|
287
390
|
recomposed(connection, room.key, room.composed.fingerprint);
|
|
288
391
|
const composition = room.composed.composition;
|
|
289
|
-
if (room.published !== undefined
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
dropTransfer(connection, room.locator);
|
|
392
|
+
if (room.published !== undefined &&
|
|
393
|
+
room.published.from !== null &&
|
|
394
|
+
room.published.from !== room.key) {
|
|
395
|
+
erasing(connection, room.published.from, room.locator);
|
|
294
396
|
}
|
|
295
397
|
if (room.refreshed === true) {
|
|
296
398
|
for (const path of applyRefresh(connection, room.key))
|
|
297
399
|
touched.add(path);
|
|
298
400
|
}
|
|
299
|
-
const owed = words();
|
|
300
401
|
for (const entry of room.confirmed?.set ?? []) {
|
|
301
|
-
putEntry(connection, room.key, CONFIRMED, entry, true, composition
|
|
402
|
+
putEntry(connection, room.key, CONFIRMED, entry, true, composition);
|
|
302
403
|
touched.add(entry.path);
|
|
303
404
|
}
|
|
304
405
|
for (const path of room.confirmed?.removed ?? []) {
|
|
305
|
-
removeEntry(connection, room.key, CONFIRMED, path
|
|
406
|
+
removeEntry(connection, room.key, CONFIRMED, path);
|
|
306
407
|
touched.add(path);
|
|
307
408
|
}
|
|
409
|
+
const savedAt = new Date().toISOString();
|
|
308
410
|
for (const entry of room.overlay?.set ?? []) {
|
|
309
|
-
putEntry(connection, room.key, OVERLAY, entry, entry.present, composition
|
|
411
|
+
putEntry(connection, room.key, OVERLAY, saved(connection, room.key, entry, savedAt), entry.present, composition);
|
|
310
412
|
touched.add(entry.path);
|
|
311
413
|
}
|
|
312
414
|
for (const path of room.overlay?.cleared ?? []) {
|
|
313
|
-
removeEntry(connection, room.key, OVERLAY, path
|
|
415
|
+
removeEntry(connection, room.key, OVERLAY, path);
|
|
314
416
|
touched.add(path);
|
|
315
417
|
}
|
|
316
418
|
const changed = [...touched];
|
|
317
419
|
revealed(connection, room.key, changed);
|
|
318
420
|
if (room.record !== undefined) {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
]);
|
|
421
|
+
const replaced = heldModel(connection, room.locator);
|
|
422
|
+
connection.run(KEEP_MODEL, [room.record, room.record, room.record, room.record]);
|
|
423
|
+
connection.run(`INSERT OR REPLACE INTO replica_rooms (locator, key, record) VALUES (?, ?, ${MODEL_LEFT})`, [room.locator, room.key, room.record, room.record, room.record]);
|
|
424
|
+
letGo(connection, replaced);
|
|
324
425
|
}
|
|
325
|
-
wordsWritten(connection, owed);
|
|
326
426
|
return room.published === undefined ? changed : null;
|
|
327
427
|
}
|
|
428
|
+
const SCHEMA = '$.model.snapshot.schema';
|
|
429
|
+
const KEEP_MODEL = `INSERT OR IGNORE INTO replica_models (identity, version, schema)
|
|
430
|
+
SELECT json_extract(?, '$.model.snapshot.identity'), json_extract(?, '$.model.snapshot.version'),
|
|
431
|
+
json_extract(?, '${SCHEMA}')
|
|
432
|
+
WHERE json_type(?, '${SCHEMA}') = 'text'`;
|
|
433
|
+
const MODEL_LEFT = `CASE WHEN json_type(?, '${SCHEMA}') = 'text' THEN json_set(?, '${SCHEMA}', json('null')) ELSE ? END`;
|
|
434
|
+
const HELD_MODEL = `SELECT json_extract(record, '$.model.snapshot.identity') AS identity,
|
|
435
|
+
json_extract(record, '$.model.snapshot.version') AS version
|
|
436
|
+
FROM replica_rooms WHERE locator = ?`;
|
|
437
|
+
const UNHELD_MODEL = `DELETE FROM replica_models WHERE identity = ? AND version = ? AND NOT EXISTS (
|
|
438
|
+
SELECT 1 FROM replica_rooms r
|
|
439
|
+
WHERE json_extract(r.record, '$.model.snapshot.identity') = replica_models.identity
|
|
440
|
+
AND json_extract(r.record, '$.model.snapshot.version') = replica_models.version
|
|
441
|
+
)`;
|
|
442
|
+
function heldModel(connection, locator) {
|
|
443
|
+
return connection.rows(HELD_MODEL, [locator])[0];
|
|
444
|
+
}
|
|
445
|
+
function letGo(connection, model) {
|
|
446
|
+
if (model !== undefined)
|
|
447
|
+
connection.run(UNHELD_MODEL, [model.identity, model.version]);
|
|
448
|
+
}
|
|
449
|
+
const HELD_RECORDS = `SELECT r.locator, r.key, CASE WHEN m.schema IS NULL THEN r.record
|
|
450
|
+
ELSE json_set(r.record, '${SCHEMA}', m.schema) END AS record
|
|
451
|
+
FROM replica_rooms r
|
|
452
|
+
LEFT JOIN replica_models m
|
|
453
|
+
ON m.identity = json_extract(r.record, '$.model.snapshot.identity')
|
|
454
|
+
AND m.version = json_extract(r.record, '$.model.snapshot.version')`;
|
|
328
455
|
export function commit(connection, rooms) {
|
|
329
|
-
const counted = {};
|
|
330
456
|
const changed = {};
|
|
331
457
|
const indexed = {};
|
|
332
458
|
for (const room of rooms) {
|
|
333
459
|
changed[room.locator] = commitRoom(connection, room);
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
const [held] = connection.rows('SELECT rederiving, searching FROM replica_keys WHERE key = ?', [
|
|
460
|
+
connection.run('UPDATE replica_rooms SET documents = ? WHERE locator = ? AND key = ?', [
|
|
461
|
+
counted(connection, room.key, room.roots ?? rootsOf(connection, room.locator)),
|
|
462
|
+
room.locator,
|
|
338
463
|
room.key,
|
|
339
464
|
]);
|
|
340
|
-
|
|
341
|
-
|
|
465
|
+
const [held] = connection.rows('SELECT rederiving FROM replica_keys WHERE key = ?', [room.key]);
|
|
466
|
+
indexed[room.locator] = held !== undefined && held.rederiving === null;
|
|
342
467
|
}
|
|
343
|
-
return {
|
|
468
|
+
return { changed, indexed };
|
|
344
469
|
}
|
|
345
470
|
function rootsOf(connection, locator) {
|
|
346
471
|
const [row] = connection.rows('SELECT record FROM replica_rooms WHERE locator = ?', [locator]);
|
|
347
472
|
const record = json(row?.record);
|
|
348
|
-
return record?.typedSelection?.roots ?? [];
|
|
473
|
+
return record?.declaration.typedSelection?.roots ?? [];
|
|
349
474
|
}
|
|
350
475
|
export function fenceRooms(connection, locators) {
|
|
351
476
|
for (const locator of locators) {
|
|
352
|
-
for (const row of connection.rows('SELECT key FROM replica_keys WHERE locator = ? UNION SELECT key FROM replica_rooms WHERE locator = ?
|
|
477
|
+
for (const row of connection.rows('SELECT key FROM replica_keys WHERE locator = ? UNION SELECT key FROM replica_rooms WHERE locator = ?', [locator, locator])) {
|
|
353
478
|
erasing(connection, number(row.key), locator);
|
|
354
479
|
}
|
|
480
|
+
const replaced = heldModel(connection, locator);
|
|
355
481
|
connection.run('DELETE FROM replica_rooms WHERE locator = ?', [locator]);
|
|
356
|
-
|
|
482
|
+
letGo(connection, replaced);
|
|
357
483
|
}
|
|
358
484
|
return null;
|
|
359
485
|
}
|
|
360
|
-
export function abandon(connection, locator
|
|
361
|
-
const
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
return number(held.key);
|
|
486
|
+
export function abandon(connection, locator) {
|
|
487
|
+
for (const row of connection.rows(`SELECT k.key FROM replica_keys k WHERE k.locator = ?
|
|
488
|
+
AND NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
489
|
+
AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)`, [locator])) {
|
|
490
|
+
erasing(connection, number(row.key), locator);
|
|
491
|
+
}
|
|
492
|
+
return null;
|
|
368
493
|
}
|
|
369
494
|
export function release(connection, key) {
|
|
370
495
|
const [held] = connection.rows(`SELECT k.locator FROM replica_keys k WHERE k.key = ?
|
|
371
|
-
AND NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
372
|
-
AND NOT EXISTS (SELECT 1 FROM replica_transfers t WHERE t.key = k.key)`, [key]);
|
|
496
|
+
AND NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)`, [key]);
|
|
373
497
|
if (held !== undefined)
|
|
374
498
|
erasing(connection, key, text(held.locator));
|
|
375
499
|
return null;
|
|
@@ -380,7 +504,6 @@ export function erase(connection, locator, limit) {
|
|
|
380
504
|
return { remaining: false };
|
|
381
505
|
const key = number(held.key);
|
|
382
506
|
const batch = 'SELECT id FROM replica_entries WHERE key = ? ORDER BY id LIMIT ?';
|
|
383
|
-
unindexDocuments(connection, heldIds(connection, batch, [key, limit]));
|
|
384
507
|
for (const table of ['replica_values', 'replica_links']) {
|
|
385
508
|
connection.run(`DELETE FROM ${table} WHERE doc IN (${batch})`, [key, limit]);
|
|
386
509
|
}
|
|
@@ -400,53 +523,46 @@ function cursor(value) {
|
|
|
400
523
|
export function rederive(connection, key, composed, bytes, ms, floor) {
|
|
401
524
|
const started = performance.now();
|
|
402
525
|
recomposed(connection, key, composed.fingerprint);
|
|
403
|
-
const [held] = connection.rows('SELECT rederiving
|
|
404
|
-
key,
|
|
405
|
-
]);
|
|
526
|
+
const [held] = connection.rows('SELECT rederiving FROM replica_keys WHERE key = ?', [key]);
|
|
406
527
|
const deriving = cursor(held?.rederiving);
|
|
407
|
-
|
|
408
|
-
if (deriving === null && searching === null)
|
|
528
|
+
if (deriving === null)
|
|
409
529
|
return { done: true, text: 0 };
|
|
410
530
|
const composition = composed.composition;
|
|
411
|
-
let last =
|
|
531
|
+
let last = deriving;
|
|
412
532
|
let spent = 0;
|
|
413
533
|
let built = 0;
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const next = (from) => (from === null || done ? null : Math.max(from, last));
|
|
418
|
-
connection.run('UPDATE replica_keys SET rederiving = ?, searching = ? WHERE key = ?', [
|
|
419
|
-
next(deriving),
|
|
420
|
-
next(searching),
|
|
421
|
-
key,
|
|
422
|
-
]);
|
|
423
|
-
return { done, text: built };
|
|
424
|
-
};
|
|
425
|
-
for (;;) {
|
|
426
|
-
const rows = connection.rows('SELECT id, path, content FROM replica_entries WHERE key = ? AND present = 1 AND id > ? ORDER BY id LIMIT ?', [key, last, DERIVED_ROWS]);
|
|
534
|
+
let read = false;
|
|
535
|
+
reading: for (;;) {
|
|
536
|
+
const rows = connection.rows('SELECT id, path, type, fields, links, octet_length(content) AS size FROM replica_entries WHERE key = ? AND present = 1 AND id > ? ORDER BY id LIMIT ?', [key, last, DERIVED_ROWS]);
|
|
427
537
|
for (const row of rows) {
|
|
428
538
|
const id = number(row.id);
|
|
429
539
|
const path = text(row.path);
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
540
|
+
const size = row.size === null ? 0 : number(row.size);
|
|
541
|
+
dropDerived(connection, id);
|
|
542
|
+
writeDerived(connection, key, composition, [
|
|
543
|
+
{
|
|
544
|
+
doc: id,
|
|
545
|
+
path,
|
|
546
|
+
held: {
|
|
547
|
+
type: text(row.type),
|
|
548
|
+
fields: json(row.fields),
|
|
549
|
+
links: json(row.links) ?? [],
|
|
550
|
+
},
|
|
551
|
+
},
|
|
552
|
+
]);
|
|
553
|
+
built += size;
|
|
440
554
|
last = id;
|
|
441
|
-
|
|
442
|
-
spent
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
555
|
+
spent += ENTRY_WEIGHT + size;
|
|
556
|
+
if (spent >= bytes || (spent >= floor && performance.now() - started >= ms))
|
|
557
|
+
break reading;
|
|
558
|
+
}
|
|
559
|
+
if (rows.length < DERIVED_ROWS) {
|
|
560
|
+
read = true;
|
|
561
|
+
break;
|
|
446
562
|
}
|
|
447
|
-
if (rows.length < DERIVED_ROWS)
|
|
448
|
-
return reached(true);
|
|
449
563
|
}
|
|
564
|
+
connection.run('UPDATE replica_keys SET rederiving = ? WHERE key = ?', [read ? null : last, key]);
|
|
565
|
+
return { done: read, text: built };
|
|
450
566
|
}
|
|
451
567
|
function heldEntry(row) {
|
|
452
568
|
return {
|
|
@@ -455,12 +571,24 @@ function heldEntry(row) {
|
|
|
455
571
|
binary: json(row.binary),
|
|
456
572
|
revision: text(row.revision),
|
|
457
573
|
fileId: text(row.file_id),
|
|
574
|
+
created: text(row.created),
|
|
458
575
|
change: json(row.change),
|
|
459
576
|
written: text(row.written),
|
|
460
577
|
type: text(row.type),
|
|
578
|
+
facts: factsHeld(row),
|
|
461
579
|
};
|
|
462
580
|
}
|
|
463
|
-
|
|
581
|
+
function factsHeld(row) {
|
|
582
|
+
const type = text(row.declared);
|
|
583
|
+
if (type === null)
|
|
584
|
+
return null;
|
|
585
|
+
return {
|
|
586
|
+
type,
|
|
587
|
+
fields: json(row.fields) ?? {},
|
|
588
|
+
links: json(row.links) ?? [],
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
const ENTRY_COLUMNS = 'e.path, e.content, e.binary, e.revision, e.file_id, e.created, e.change, e.written, e.type, e.declared, e.fields, e.links';
|
|
464
592
|
function unrefreshed(alias) {
|
|
465
593
|
return `NOT EXISTS (SELECT 1 FROM replica_entries r WHERE r.key = ${alias}.key AND r.layer = ${REFRESHED} AND r.path = ${alias}.path)`;
|
|
466
594
|
}
|
|
@@ -471,22 +599,6 @@ function viewed(view, alias) {
|
|
|
471
599
|
return `${alias}.layer = ${CONFIRMED}`;
|
|
472
600
|
return `${alias}.present = 1 AND (${alias}.layer = ${REFRESHED} OR (${alias}.layer = ${CONFIRMED} AND ${unrefreshed(alias)}))`;
|
|
473
601
|
}
|
|
474
|
-
function layers(view) {
|
|
475
|
-
if (view !== 'refreshed')
|
|
476
|
-
return [viewed(view, 'e')];
|
|
477
|
-
return [
|
|
478
|
-
`e.layer = ${REFRESHED} AND e.present = 1`,
|
|
479
|
-
`e.layer = ${CONFIRMED} AND e.present = 1 AND ${unrefreshed('e')}`,
|
|
480
|
-
];
|
|
481
|
-
}
|
|
482
|
-
function pathOrdered(connection, columns, key, view, condition, after, limit) {
|
|
483
|
-
const beyond = after === null ? '' : ' AND e.path > ?';
|
|
484
|
-
const parts = layers(view).map((layer) => `SELECT ${columns} FROM replica_entries e WHERE e.key = ? AND ${layer}${condition}${beyond}`);
|
|
485
|
-
return connection.rows(`${parts.join(' UNION ALL ')} ORDER BY path LIMIT ?`, [
|
|
486
|
-
...parts.flatMap(() => (after === null ? [key] : [key, after])),
|
|
487
|
-
limit,
|
|
488
|
-
]);
|
|
489
|
-
}
|
|
490
602
|
export function entries(connection, key, view, list) {
|
|
491
603
|
if (list.length === 0)
|
|
492
604
|
return [];
|
|
@@ -505,120 +617,169 @@ export function typed(connection, key, view, list) {
|
|
|
505
617
|
binary: number(row.binary) === 1,
|
|
506
618
|
}));
|
|
507
619
|
}
|
|
508
|
-
export function referrers(connection, key, view, targets
|
|
620
|
+
export function referrers(connection, key, view, targets) {
|
|
509
621
|
if (targets.length === 0)
|
|
510
622
|
return [];
|
|
511
623
|
return connection
|
|
512
|
-
.rows(`SELECT DISTINCT e.path, l.target
|
|
513
|
-
.map((row) => ({
|
|
514
|
-
path: text(row.path),
|
|
515
|
-
target: text(row.target),
|
|
516
|
-
kind: text(row.kind),
|
|
517
|
-
}));
|
|
624
|
+
.rows(`SELECT DISTINCT e.path, l.target FROM replica_links l JOIN replica_entries e ON e.id = l.doc WHERE l.key = ? AND l.target IN (SELECT value FROM json_each(?)) AND ${viewed(view, 'e')}`, [key, paths(targets)])
|
|
625
|
+
.map((row) => ({ path: text(row.path), target: text(row.target) }));
|
|
518
626
|
}
|
|
519
|
-
export function
|
|
520
|
-
|
|
521
|
-
let spent = 0;
|
|
522
|
-
let from = after;
|
|
523
|
-
for (;;) {
|
|
524
|
-
const rows = pathOrdered(connection, ENTRY_COLUMNS, key, view, '', from, 64);
|
|
525
|
-
for (const row of rows) {
|
|
526
|
-
const entry = heldEntry(row);
|
|
527
|
-
found.push(entry);
|
|
528
|
-
spent += 256 + (entry.content?.length ?? 0) * 2;
|
|
529
|
-
if (spent >= bytes)
|
|
530
|
-
return { entries: found, next: entry.path };
|
|
531
|
-
}
|
|
532
|
-
if (rows.length < 64)
|
|
533
|
-
return { entries: found, next: null };
|
|
534
|
-
from = found.at(-1).path;
|
|
535
|
-
}
|
|
627
|
+
export function folders(connection, key, list) {
|
|
628
|
+
return list.filter((path) => connection.rows(`SELECT 1 FROM replica_folders f WHERE f.key = ? AND f.layer = ${CONFIRMED} AND f.present = 1 AND f.path = ? UNION ALL SELECT 1 FROM replica_entries e WHERE e.key = ? AND ${viewed('visible', 'e')} AND e.path >= ? AND e.path < ? LIMIT 1`, [key, path, key, `${path}/`, `${path}0`]).length > 0);
|
|
536
629
|
}
|
|
537
|
-
export function
|
|
538
|
-
return
|
|
630
|
+
export function typedWithin(connection, key, view, roots, within) {
|
|
631
|
+
return (connection.rows(`SELECT 1 FROM replica_entries e WHERE e.key = ? AND ${viewed(view, 'e')} AND e.declared IS NOT NULL AND e.declared <> 'any' AND ${within ? '' : 'NOT '}${beneathRoots('e.path')} LIMIT 1`, [key, JSON.stringify(roots)]).length > 0);
|
|
632
|
+
}
|
|
633
|
+
const ORPHANS = `SELECT k.key FROM replica_keys k
|
|
634
|
+
WHERE NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
635
|
+
AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)
|
|
636
|
+
AND (${UNPARTED} OR NOT EXISTS (
|
|
637
|
+
SELECT 1 FROM replica_entries e WHERE e.key = k.key
|
|
638
|
+
UNION ALL SELECT 1 FROM replica_folders f WHERE f.key = k.key
|
|
639
|
+
))`;
|
|
640
|
+
const STAGED = `SELECT DISTINCT k.locator FROM replica_keys k
|
|
641
|
+
WHERE NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
642
|
+
AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)
|
|
643
|
+
AND k.key NOT IN (${ORPHANS})`;
|
|
644
|
+
export function opened(connection) {
|
|
645
|
+
return {
|
|
646
|
+
orphans: connection.rows(ORPHANS).map((row) => number(row.key)),
|
|
647
|
+
erasing: connection.rows('SELECT 1 FROM replica_erasing LIMIT 1').length > 0,
|
|
648
|
+
};
|
|
539
649
|
}
|
|
540
|
-
|
|
541
|
-
|
|
650
|
+
const ROOM_ROWS = `SELECT h.locator, h.key, h.record, r.documents, k.rederiving IS NULL AS indexed
|
|
651
|
+
FROM (${HELD_RECORDS}) h
|
|
652
|
+
JOIN replica_rooms r ON r.locator = h.locator
|
|
653
|
+
LEFT JOIN replica_keys k ON k.key = h.key`;
|
|
654
|
+
function roomRow(row) {
|
|
655
|
+
return {
|
|
656
|
+
locator: text(row.locator),
|
|
657
|
+
key: number(row.key),
|
|
658
|
+
record: text(row.record),
|
|
659
|
+
indexed: number(row.indexed) === 1,
|
|
660
|
+
documents: number(row.documents),
|
|
661
|
+
};
|
|
542
662
|
}
|
|
543
|
-
export function
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
bind.push(root, `${root}/`, `${root}0`);
|
|
548
|
-
const placed = roots.length === 0 ? (within ? ' AND 0' : '') : ` AND ${within ? '' : 'NOT '}(${inside})`;
|
|
549
|
-
return (connection.rows(`SELECT 1 FROM replica_entries e WHERE e.key = ? AND ${viewed(view, 'e')} AND e.declared IS NOT NULL AND e.declared <> 'any'${placed} LIMIT 1`, bind).length > 0);
|
|
550
|
-
}
|
|
551
|
-
export function transferPart(connection, locator, token) {
|
|
552
|
-
const [held] = connection.rows('SELECT part FROM replica_transfer_parts WHERE locator = ? AND token = ?', [locator, token]);
|
|
553
|
-
return held?.part instanceof Uint8Array ? held.part : null;
|
|
554
|
-
}
|
|
555
|
-
export function keepParts(connection, parts) {
|
|
556
|
-
for (const { locator, token, part } of parts) {
|
|
557
|
-
connection.run('DELETE FROM replica_transfer_parts WHERE locator = ? AND token = ?', [
|
|
558
|
-
locator,
|
|
559
|
-
token,
|
|
560
|
-
]);
|
|
561
|
-
connection.run('INSERT INTO replica_transfer_parts (locator, token, part) SELECT ?, ?, ? WHERE EXISTS (SELECT 1 FROM replica_transfers WHERE locator = ?)', [locator, token, part, locator]);
|
|
562
|
-
}
|
|
563
|
-
return null;
|
|
663
|
+
export function heldRooms(connection, locators) {
|
|
664
|
+
return (locators === null
|
|
665
|
+
? connection.rows(`${ROOM_ROWS} ORDER BY h.locator`)
|
|
666
|
+
: connection.rows(`${ROOM_ROWS} WHERE h.locator IN (SELECT value FROM json_each(?)) ORDER BY h.locator`, [JSON.stringify(locators)])).map(roomRow);
|
|
564
667
|
}
|
|
565
|
-
export function
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
668
|
+
export function heldRoom(connection, locator) {
|
|
669
|
+
return heldRooms(connection, [locator])[0] ?? null;
|
|
670
|
+
}
|
|
671
|
+
export function overlaid(connection, key) {
|
|
672
|
+
return connection
|
|
673
|
+
.rows(`SELECT path FROM replica_entries WHERE key = ? AND layer = ${OVERLAY}`, [key])
|
|
674
|
+
.map((row) => text(row.path));
|
|
675
|
+
}
|
|
676
|
+
const COVER_ROWS = `SELECT r.locator, r.key, r.documents, json_extract(r.record, '$.transfer') AS transfer,
|
|
677
|
+
json_extract(r.record, '$.declaration.handle') AS handle,
|
|
678
|
+
json_extract(r.record, '$.roomVersion') AS room_version,
|
|
679
|
+
coalesce(json_extract(r.record, '$.inventory.bytes'), 0) AS bytes,
|
|
680
|
+
json_extract(r.record, '$.declaration.replica.offline') AS offline,
|
|
681
|
+
k.rederiving IS NULL AS indexed
|
|
682
|
+
FROM replica_rooms r LEFT JOIN replica_keys k ON k.key = r.key`;
|
|
683
|
+
const COVER_MARK = `UPDATE cover_clock SET at = at + 1;
|
|
684
|
+
INSERT OR REPLACE INTO cover_changes (locator, at) SELECT`;
|
|
685
|
+
const COVER_LOG = [
|
|
686
|
+
'CREATE TEMP TABLE cover_clock (token TEXT NOT NULL, at INTEGER NOT NULL)',
|
|
687
|
+
'CREATE TEMP TABLE cover_changes (locator TEXT PRIMARY KEY, at INTEGER NOT NULL) WITHOUT ROWID',
|
|
688
|
+
'CREATE INDEX temp.cover_changes_at ON cover_changes (at)',
|
|
689
|
+
'INSERT INTO temp.cover_clock (token, at) VALUES (lower(hex(randomblob(8))), 0)',
|
|
690
|
+
`CREATE TEMP TRIGGER cover_room_added AFTER INSERT ON replica_rooms BEGIN
|
|
691
|
+
${COVER_MARK} NEW.locator, at FROM cover_clock; END`,
|
|
692
|
+
`CREATE TEMP TRIGGER cover_room_changed AFTER UPDATE ON replica_rooms BEGIN
|
|
693
|
+
${COVER_MARK} OLD.locator, at FROM cover_clock;
|
|
694
|
+
INSERT OR REPLACE INTO cover_changes (locator, at) SELECT NEW.locator, at FROM cover_clock; END`,
|
|
695
|
+
`CREATE TEMP TRIGGER cover_room_removed AFTER DELETE ON replica_rooms BEGIN
|
|
696
|
+
${COVER_MARK} OLD.locator, at FROM cover_clock; END`,
|
|
697
|
+
`CREATE TEMP TRIGGER cover_key_changed AFTER UPDATE OF rederiving ON replica_keys BEGIN
|
|
698
|
+
${COVER_MARK} r.locator, c.at FROM replica_rooms r, cover_clock c WHERE r.key = NEW.key; END`,
|
|
699
|
+
`CREATE TEMP TRIGGER cover_key_removed AFTER DELETE ON replica_keys BEGIN
|
|
700
|
+
${COVER_MARK} r.locator, c.at FROM replica_rooms r, cover_clock c WHERE r.key = OLD.key; END`,
|
|
701
|
+
];
|
|
702
|
+
function coveredRow(row) {
|
|
703
|
+
return {
|
|
704
|
+
locator: text(row.locator),
|
|
705
|
+
handle: text(row.handle),
|
|
706
|
+
key: number(row.key),
|
|
707
|
+
transfer: text(row.transfer),
|
|
708
|
+
roomVersion: text(row.room_version),
|
|
709
|
+
indexed: number(row.indexed) === 1,
|
|
710
|
+
documents: number(row.documents),
|
|
711
|
+
bytes: number(row.bytes),
|
|
712
|
+
offline: text(row.offline),
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
function coverLog(connection) {
|
|
716
|
+
const logged = connection.rows("SELECT 1 FROM sqlite_temp_schema WHERE type = 'table' AND name = 'cover_clock'");
|
|
717
|
+
if (logged.length === 0)
|
|
718
|
+
return null;
|
|
719
|
+
const [clock] = connection.rows('SELECT token, at FROM temp.cover_clock');
|
|
720
|
+
return { token: text(clock.token), at: number(clock.at) };
|
|
721
|
+
}
|
|
722
|
+
export function covering(connection, since) {
|
|
723
|
+
let log = coverLog(connection);
|
|
724
|
+
if (log === null) {
|
|
725
|
+
for (const statement of COVER_LOG)
|
|
726
|
+
connection.run(statement);
|
|
727
|
+
log = coverLog(connection);
|
|
728
|
+
}
|
|
729
|
+
if (since === null || since.token !== log.token) {
|
|
730
|
+
return {
|
|
731
|
+
log,
|
|
732
|
+
whole: true,
|
|
733
|
+
rooms: connection.rows(`${COVER_ROWS} ORDER BY r.locator`).map(coveredRow),
|
|
734
|
+
removed: [],
|
|
735
|
+
};
|
|
569
736
|
}
|
|
570
737
|
return {
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
locator
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
})),
|
|
581
|
-
kept: connection
|
|
582
|
-
.rows('SELECT locator, token, length(part) AS bytes FROM replica_transfer_parts')
|
|
583
|
-
.map((row) => ({
|
|
584
|
-
locator: text(row.locator),
|
|
585
|
-
token: text(row.token),
|
|
586
|
-
bytes: number(row.bytes),
|
|
587
|
-
})),
|
|
588
|
-
keys: connection
|
|
589
|
-
.rows('SELECT key, locator, transfer, rederiving, searching FROM replica_keys')
|
|
590
|
-
.map((row) => ({
|
|
591
|
-
key: number(row.key),
|
|
592
|
-
locator: text(row.locator),
|
|
593
|
-
transfer: text(row.transfer),
|
|
594
|
-
rederiving: cursor(row.rederiving),
|
|
595
|
-
searching: cursor(row.searching),
|
|
596
|
-
})),
|
|
597
|
-
facts,
|
|
598
|
-
presentation: connection.rows('SELECT app, value FROM replica_presentation').map((row) => ({
|
|
599
|
-
app: text(row.app),
|
|
600
|
-
value: text(row.value),
|
|
601
|
-
})),
|
|
602
|
-
overlaid: connection
|
|
603
|
-
.rows(`SELECT key, path FROM replica_entries WHERE layer = ${OVERLAY}`)
|
|
604
|
-
.map((row) => ({ key: number(row.key), path: text(row.path) })),
|
|
605
|
-
orphans: connection
|
|
606
|
-
.rows(`SELECT k.key FROM replica_keys k
|
|
607
|
-
WHERE NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
608
|
-
AND NOT EXISTS (SELECT 1 FROM replica_transfers t WHERE t.key = k.key)
|
|
609
|
-
AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)`)
|
|
610
|
-
.map((row) => number(row.key)),
|
|
611
|
-
erasing: connection.rows('SELECT key, locator FROM replica_erasing').map((row) => ({
|
|
612
|
-
key: number(row.key),
|
|
613
|
-
locator: text(row.locator),
|
|
614
|
-
})),
|
|
738
|
+
log,
|
|
739
|
+
whole: false,
|
|
740
|
+
rooms: connection
|
|
741
|
+
.rows(`${COVER_ROWS} WHERE r.locator IN (SELECT locator FROM temp.cover_changes WHERE at > ?) ORDER BY r.locator`, [since.at])
|
|
742
|
+
.map(coveredRow),
|
|
743
|
+
removed: connection
|
|
744
|
+
.rows(`SELECT c.locator FROM temp.cover_changes c WHERE c.at > ?
|
|
745
|
+
AND NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.locator = c.locator)`, [since.at])
|
|
746
|
+
.map((row) => text(row.locator)),
|
|
615
747
|
};
|
|
616
748
|
}
|
|
617
|
-
export function
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
749
|
+
export function offlineMode(connection, locator) {
|
|
750
|
+
const [held] = connection.rows("SELECT json_extract(record, '$.declaration.replica.offline') AS offline FROM replica_rooms WHERE locator = ?", [locator]);
|
|
751
|
+
return text(held?.offline);
|
|
752
|
+
}
|
|
753
|
+
export function stagedLocators(connection) {
|
|
754
|
+
return connection.rows(STAGED).map((row) => text(row.locator));
|
|
755
|
+
}
|
|
756
|
+
export function heldLocators(connection) {
|
|
757
|
+
return connection
|
|
758
|
+
.rows(`SELECT locator FROM replica_rooms UNION ${STAGED}`)
|
|
759
|
+
.map((row) => text(row.locator));
|
|
760
|
+
}
|
|
761
|
+
export function demoted(connection) {
|
|
762
|
+
connection.run(`UPDATE replica_rooms SET record = json_set(record, '$.model.status', 'cached')
|
|
763
|
+
WHERE json_extract(record, '$.model.status') = 'current'`);
|
|
764
|
+
return null;
|
|
765
|
+
}
|
|
766
|
+
export function facts(connection, names) {
|
|
767
|
+
const held = {};
|
|
768
|
+
for (const row of names === null
|
|
769
|
+
? connection.rows('SELECT name, value FROM replica_facts')
|
|
770
|
+
: connection.rows('SELECT name, value FROM replica_facts WHERE name IN (SELECT value FROM json_each(?))', [JSON.stringify(names)])) {
|
|
771
|
+
held[text(row.name)] = text(row.value);
|
|
622
772
|
}
|
|
623
|
-
|
|
773
|
+
return held;
|
|
774
|
+
}
|
|
775
|
+
export function presentation(connection, app) {
|
|
776
|
+
const [held] = connection.rows('SELECT value FROM replica_presentation WHERE app = ?', [app]);
|
|
777
|
+
return text(held?.value);
|
|
778
|
+
}
|
|
779
|
+
export function tablesOf(statements) {
|
|
780
|
+
return statements.flatMap((statement) => /^CREATE TABLE (\w+)/u.exec(statement)?.[1] ?? []);
|
|
781
|
+
}
|
|
782
|
+
export function ended(connection) {
|
|
783
|
+
for (const table of tablesOf(REPLICA_TABLES))
|
|
784
|
+
connection.run(`DELETE FROM ${table}`);
|
|
624
785
|
}
|