@agentshouse/mdruntime 0.1.0-cb1f56e16291b → 0.1.0-cb3bb3b233fa1
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 +302 -0
- package/dist/adapter.d.ts +113 -81
- 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 +345 -0
- package/dist/bootstrap.d.ts +9 -5
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +204 -85
- 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 +5 -0
- 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 +30 -22
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +39 -20
- package/dist/collection.d.ts +26 -4
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +19 -2
- 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 +9 -7
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +252 -318
- package/dist/connection.d.ts +5 -141
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +111 -5282
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +1 -1
- package/dist/conversation-live.d.ts +150 -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 +62 -43
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +987 -902
- 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 +10 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +150 -0
- package/dist/durability.d.ts +19 -0
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +4 -0
- package/dist/entry.d.ts +2 -2
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +18 -9
- 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 +6 -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 +20 -4
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +72 -28
- 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 +32 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -5
- 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 +282 -0
- package/dist/listing.d.ts +52 -0
- package/dist/listing.d.ts.map +1 -0
- package/dist/listing.js +152 -0
- package/dist/live.d.ts +8 -6
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +110 -31
- package/dist/loaded.d.ts +5 -0
- package/dist/loaded.d.ts.map +1 -0
- package/dist/loaded.js +40 -0
- package/dist/locks.d.ts +7 -0
- package/dist/locks.d.ts.map +1 -0
- package/dist/locks.js +32 -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 +6 -1
- package/dist/media.d.ts.map +1 -1
- package/dist/media.js +68 -17
- package/dist/memory.d.ts +11 -3
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +132 -33
- package/dist/messages.d.ts +33 -3
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +98 -27
- package/dist/model.d.ts +2 -10
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +0 -14
- package/dist/models.d.ts +10 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +153 -0
- package/dist/observation.d.ts +27 -12
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +184 -85
- 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 +53 -0
- package/dist/outcome.d.ts +17 -2
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +35 -2
- package/dist/overlay.d.ts +7 -2
- 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 +4 -0
- package/dist/parts.d.ts.map +1 -0
- package/dist/parts.js +13 -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 +41 -0
- package/dist/preparation.d.ts.map +1 -0
- package/dist/preparation.js +314 -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 +308 -0
- package/dist/query.d.ts +13 -213
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -524
- package/dist/questions.d.ts +30 -0
- package/dist/questions.d.ts.map +1 -0
- package/dist/questions.js +116 -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 +10 -10
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +235 -120
- package/dist/react.d.ts +17 -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 +9 -4
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +3 -5
- package/dist/reads.d.ts +39 -0
- package/dist/reads.d.ts.map +1 -0
- package/dist/reads.js +178 -0
- package/dist/recovery.d.ts +3 -1
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +38 -8
- package/dist/refresh.d.ts +1 -1
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +10 -22
- 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 +11 -16
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +66 -35
- 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 +32 -9
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +273 -66
- package/dist/room-drafts.d.ts +24 -0
- package/dist/room-drafts.d.ts.map +1 -0
- package/dist/room-drafts.js +220 -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 -23
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +49 -191
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +1 -0
- package/dist/save.d.ts +42 -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 +437 -0
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +1 -1
- 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 -6
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +13 -95
- package/dist/shown.d.ts +33 -0
- package/dist/shown.d.ts.map +1 -0
- package/dist/shown.js +309 -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 +38 -0
- package/dist/socket/session.d.ts.map +1 -0
- package/dist/socket/session.js +23 -0
- package/dist/socket.d.ts +67 -0
- package/dist/socket.d.ts.map +1 -0
- package/dist/socket.js +164 -0
- package/dist/source.d.ts +59 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +93 -0
- package/dist/status.d.ts +0 -2
- 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 +223 -0
- package/dist/store/records.d.ts.map +1 -0
- package/dist/store/records.js +465 -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 +115 -152
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +171 -395
- package/dist/stored.d.ts +72 -67
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +317 -166
- package/dist/surface.d.ts +90 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +225 -0
- package/dist/sync.d.ts +19 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +536 -0
- package/dist/tables.d.ts +112 -89
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +197 -283
- package/dist/tallies.d.ts +18 -0
- package/dist/tallies.d.ts.map +1 -0
- package/dist/tallies.js +128 -0
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +9 -12
- 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 +68 -7
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +23 -4
- 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/job.d.ts +0 -14
- package/dist/job.d.ts.map +0 -1
- package/dist/job.js +0 -28
- package/dist/log.d.ts +0 -5
- package/dist/log.d.ts.map +0 -1
- package/dist/log.js +0 -14
- package/dist/phrase.d.ts +0 -3
- package/dist/phrase.d.ts.map +0 -1
- package/dist/phrase.js +0 -8
package/dist/stored.js
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
+
import { instantKey, sql } from '@agentshouse/mdmodel';
|
|
1
2
|
import { composedOf } from './collection.js';
|
|
2
|
-
import { described, derived } from './rows.js';
|
|
3
|
+
import { described, derived, heldOf } from './rows.js';
|
|
3
4
|
export const REPLICA_TABLES = [
|
|
4
5
|
'CREATE TABLE replica_keys (key INTEGER PRIMARY KEY AUTOINCREMENT, locator TEXT NOT NULL, transfer TEXT NOT NULL, fingerprint TEXT NOT NULL, rederiving INTEGER)',
|
|
5
|
-
'CREATE
|
|
6
|
-
'CREATE TABLE
|
|
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)",
|
|
7
12
|
'CREATE UNIQUE INDEX replica_entries_path ON replica_entries (key, layer, path)',
|
|
8
13
|
'CREATE INDEX replica_entries_visible ON replica_entries (key, visible, present, path, binary)',
|
|
9
14
|
'CREATE INDEX replica_entries_folder ON replica_entries (key, folder, path)',
|
|
10
|
-
'CREATE INDEX replica_entries_type ON replica_entries (key, type, visible, present,
|
|
15
|
+
'CREATE INDEX replica_entries_type ON replica_entries (key, type, visible, present, folder)',
|
|
11
16
|
'CREATE INDEX replica_entries_key ON replica_entries (key)',
|
|
12
|
-
|
|
17
|
+
sql.VALUE_TABLE,
|
|
13
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',
|
|
14
19
|
'CREATE INDEX replica_links_target ON replica_links (key, target, kind, keys)',
|
|
15
|
-
'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',
|
|
16
21
|
'CREATE TABLE replica_erasing (key INTEGER PRIMARY KEY, locator TEXT NOT NULL)',
|
|
17
|
-
'CREATE TABLE replica_opened (locator TEXT NOT NULL, path TEXT NOT NULL, PRIMARY KEY (locator, path)) WITHOUT ROWID',
|
|
18
22
|
'CREATE TABLE replica_facts (name TEXT PRIMARY KEY, value TEXT NOT NULL) WITHOUT ROWID',
|
|
19
23
|
'CREATE TABLE replica_presentation (app TEXT PRIMARY KEY, value TEXT NOT NULL) WITHOUT ROWID',
|
|
20
24
|
];
|
|
@@ -26,11 +30,9 @@ const ENTRY_WEIGHT = 256;
|
|
|
26
30
|
const ABSENT = {
|
|
27
31
|
type: null,
|
|
28
32
|
declared: null,
|
|
29
|
-
queried: false,
|
|
30
33
|
folder: '',
|
|
31
34
|
fields: null,
|
|
32
|
-
|
|
33
|
-
links: [],
|
|
35
|
+
links: null,
|
|
34
36
|
};
|
|
35
37
|
function text(value) {
|
|
36
38
|
return typeof value === 'string' ? value : null;
|
|
@@ -51,17 +53,11 @@ function dropDerived(connection, doc) {
|
|
|
51
53
|
connection.run('DELETE FROM replica_values WHERE doc = ?', [doc]);
|
|
52
54
|
connection.run('DELETE FROM replica_links WHERE doc = ?', [doc]);
|
|
53
55
|
}
|
|
54
|
-
function
|
|
55
|
-
|
|
56
|
-
return 'Infinity';
|
|
57
|
-
if (value === -Infinity)
|
|
58
|
-
return '-Infinity';
|
|
59
|
-
return JSON.stringify(value);
|
|
60
|
-
}
|
|
61
|
-
function writeDerived(connection, doc, key, made) {
|
|
56
|
+
function writeDerived(connection, doc, key, path, held, composition) {
|
|
57
|
+
const made = derived(path, held, composition);
|
|
62
58
|
if (made.values.length > 0) {
|
|
63
|
-
const
|
|
64
|
-
connection.run(
|
|
59
|
+
const inserted = sql.insertedValues(doc, made.values);
|
|
60
|
+
connection.run(inserted.sql, inserted.bind);
|
|
65
61
|
}
|
|
66
62
|
if (made.links.length > 0) {
|
|
67
63
|
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 FROM json_each(?)', [
|
|
@@ -82,25 +78,28 @@ function removeEntry(connection, key, layer, path) {
|
|
|
82
78
|
dropDerived(connection, id);
|
|
83
79
|
connection.run('DELETE FROM replica_entries WHERE id = ?', [id]);
|
|
84
80
|
}
|
|
85
|
-
function insertEntry(connection, key, layer, entry, present, made) {
|
|
86
|
-
connection.run('INSERT INTO replica_entries (key, layer, path, visible, present,
|
|
81
|
+
function insertEntry(connection, key, layer, entry, present, made, part = null) {
|
|
82
|
+
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) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [
|
|
87
83
|
key,
|
|
88
84
|
layer,
|
|
89
85
|
entry.path,
|
|
90
86
|
flag(present && layer !== REFRESHED),
|
|
91
87
|
flag(present),
|
|
92
|
-
flag(made.queried),
|
|
93
88
|
entry.binary === null ? entry.content : null,
|
|
94
|
-
entry.binary === null && entry.outline !== null ? JSON.stringify(entry.outline) : null,
|
|
95
89
|
entry.binary === null ? null : JSON.stringify(entry.binary),
|
|
96
90
|
entry.revision,
|
|
97
91
|
entry.fileId,
|
|
92
|
+
entry.created,
|
|
93
|
+
instantKey(entry.created),
|
|
98
94
|
entry.change === null ? null : JSON.stringify(entry.change),
|
|
95
|
+
instantKey(entry.change?.time),
|
|
99
96
|
entry.written,
|
|
100
97
|
made.type,
|
|
101
98
|
made.declared,
|
|
102
99
|
made.folder,
|
|
103
100
|
made.fields,
|
|
101
|
+
made.links,
|
|
102
|
+
part,
|
|
104
103
|
]);
|
|
105
104
|
}
|
|
106
105
|
function textOf(entry) {
|
|
@@ -109,16 +108,15 @@ function textOf(entry) {
|
|
|
109
108
|
function putEntry(connection, key, layer, entry, present, composition) {
|
|
110
109
|
removeEntry(connection, key, layer, entry.path);
|
|
111
110
|
const content = textOf(entry);
|
|
112
|
-
|
|
113
|
-
insertEntry(connection, key, layer, entry, present, made);
|
|
111
|
+
insertEntry(connection, key, layer, entry, present, present ? described(entry.path, content, entry.facts) : ABSENT);
|
|
114
112
|
if (!present)
|
|
115
113
|
return;
|
|
116
114
|
const id = entryId(connection, key, layer, entry.path);
|
|
117
|
-
writeDerived(connection, id, key,
|
|
115
|
+
writeDerived(connection, id, key, entry.path, heldOf(entry.path, content, entry.facts), composition);
|
|
118
116
|
}
|
|
119
|
-
function placeEntry(connection, key, entry) {
|
|
117
|
+
function placeEntry(connection, key, entry, part) {
|
|
120
118
|
removeEntry(connection, key, CONFIRMED, entry.path);
|
|
121
|
-
insertEntry(connection, key, CONFIRMED, entry, true, described(entry.path, textOf(entry), entry.
|
|
119
|
+
insertEntry(connection, key, CONFIRMED, entry, true, described(entry.path, textOf(entry), entry.facts), part);
|
|
122
120
|
}
|
|
123
121
|
function revealed(connection, key, touched) {
|
|
124
122
|
if (touched.length === 0)
|
|
@@ -145,17 +143,83 @@ export function entered(connection, locator, composition, rows) {
|
|
|
145
143
|
return key;
|
|
146
144
|
}
|
|
147
145
|
export function stage(connection, request) {
|
|
146
|
+
if (request.key === null) {
|
|
147
|
+
for (const row of connection.rows(STAGED_KEYS, [request.locator])) {
|
|
148
|
+
erasing(connection, number(row.key), request.locator);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
148
151
|
const key = request.key ??
|
|
149
152
|
newKey(connection, request.locator, request.transfer, request.fingerprint, false);
|
|
150
153
|
recomposed(connection, key, request.fingerprint);
|
|
151
|
-
connection
|
|
154
|
+
owing(connection, key);
|
|
152
155
|
for (const entry of request.entries)
|
|
153
|
-
placeEntry(connection, key, entry);
|
|
156
|
+
placeEntry(connection, key, entry, request.part);
|
|
154
157
|
for (const path of request.folders) {
|
|
155
|
-
connection.run('INSERT OR REPLACE INTO replica_folders (key, layer, path, present) VALUES (?, ?, ?, 1)', [key, CONFIRMED, path]);
|
|
158
|
+
connection.run('INSERT OR REPLACE INTO replica_folders (key, layer, path, present, part) VALUES (?, ?, ?, 1, ?)', [key, CONFIRMED, path, request.part]);
|
|
156
159
|
}
|
|
157
160
|
return key;
|
|
158
161
|
}
|
|
162
|
+
function owing(connection, key) {
|
|
163
|
+
connection.run('UPDATE replica_keys SET rederiving = coalesce(rederiving, last) FROM (SELECT coalesce(max(id), 0) AS last FROM replica_entries) WHERE key = ?', [key]);
|
|
164
|
+
}
|
|
165
|
+
const STAGED_KEYS = `SELECT k.key, k.transfer FROM replica_keys k WHERE k.locator = ?
|
|
166
|
+
AND NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
167
|
+
AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)
|
|
168
|
+
ORDER BY k.key DESC`;
|
|
169
|
+
const UNPARTED = `EXISTS (SELECT 1 FROM replica_entries e WHERE e.key = k.key AND e.part IS NULL)
|
|
170
|
+
OR EXISTS (SELECT 1 FROM replica_folders f WHERE f.key = k.key AND f.part IS NULL)`;
|
|
171
|
+
function partsOf(connection, key, layer) {
|
|
172
|
+
const scoped = layer === null ? '' : ` AND layer = ${layer}`;
|
|
173
|
+
return new Set(connection
|
|
174
|
+
.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])
|
|
175
|
+
.map((row) => text(row.part)));
|
|
176
|
+
}
|
|
177
|
+
function resumable(connection, locator) {
|
|
178
|
+
let kept = null;
|
|
179
|
+
for (const row of connection.rows(STAGED_KEYS, [locator])) {
|
|
180
|
+
const key = number(row.key);
|
|
181
|
+
const parted = kept === null &&
|
|
182
|
+
connection.rows(`SELECT 1 FROM replica_keys k WHERE k.key = ? AND NOT (${UNPARTED})`, [key])
|
|
183
|
+
.length > 0;
|
|
184
|
+
if (parted)
|
|
185
|
+
kept = { key, transfer: text(row.transfer) };
|
|
186
|
+
else
|
|
187
|
+
erasing(connection, key, locator);
|
|
188
|
+
}
|
|
189
|
+
return kept;
|
|
190
|
+
}
|
|
191
|
+
export function resume(connection, request) {
|
|
192
|
+
const wanted = new Set(request.parts);
|
|
193
|
+
const held = resumable(connection, request.locator);
|
|
194
|
+
const staged = held === null ? new Set() : partsOf(connection, held.key, null);
|
|
195
|
+
const dropped = [...staged].filter((part) => !wanted.has(part));
|
|
196
|
+
if (held !== null && dropped.length > 0) {
|
|
197
|
+
const named = JSON.stringify(dropped);
|
|
198
|
+
const rows = `SELECT id FROM replica_entries WHERE key = ? AND part IN (SELECT value FROM json_each(?))`;
|
|
199
|
+
for (const table of ['replica_values', 'replica_links']) {
|
|
200
|
+
connection.run(`DELETE FROM ${table} WHERE doc IN (${rows})`, [held.key, named]);
|
|
201
|
+
}
|
|
202
|
+
connection.run(`DELETE FROM replica_entries WHERE id IN (${rows})`, [held.key, named]);
|
|
203
|
+
connection.run('DELETE FROM replica_folders WHERE key = ? AND part IN (SELECT value FROM json_each(?))', [held.key, named]);
|
|
204
|
+
}
|
|
205
|
+
const kept = [...staged].filter((part) => wanted.has(part));
|
|
206
|
+
const publishedParts = request.published === null
|
|
207
|
+
? new Set()
|
|
208
|
+
: partsOf(connection, request.published, CONFIRMED);
|
|
209
|
+
const copied = request.parts.filter((part) => !staged.has(part) && publishedParts.has(part));
|
|
210
|
+
if (held === null && copied.length === 0) {
|
|
211
|
+
return { key: null, transfer: request.transfer, staged: [] };
|
|
212
|
+
}
|
|
213
|
+
const key = held?.key ?? newKey(connection, request.locator, request.transfer, request.fingerprint, false);
|
|
214
|
+
recomposed(connection, key, request.fingerprint);
|
|
215
|
+
if (copied.length > 0) {
|
|
216
|
+
owing(connection, key);
|
|
217
|
+
const named = JSON.stringify(copied);
|
|
218
|
+
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]);
|
|
219
|
+
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]);
|
|
220
|
+
}
|
|
221
|
+
return { key, transfer: held?.transfer ?? request.transfer, staged: [...kept, ...copied] };
|
|
222
|
+
}
|
|
159
223
|
export function refreshing(connection, request) {
|
|
160
224
|
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]);
|
|
161
225
|
if (held.length === 0)
|
|
@@ -169,12 +233,13 @@ export function refreshing(connection, request) {
|
|
|
169
233
|
putEntry(connection, request.key, REFRESHED, {
|
|
170
234
|
path,
|
|
171
235
|
content: null,
|
|
172
|
-
outline: null,
|
|
173
236
|
binary: null,
|
|
174
237
|
revision: null,
|
|
175
238
|
fileId: null,
|
|
239
|
+
created: null,
|
|
176
240
|
change: null,
|
|
177
241
|
written: null,
|
|
242
|
+
facts: null,
|
|
178
243
|
}, false, composition);
|
|
179
244
|
}
|
|
180
245
|
for (const [list, present] of [
|
|
@@ -231,15 +296,18 @@ function counted(connection, key, roots) {
|
|
|
231
296
|
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)]);
|
|
232
297
|
return number(row.held);
|
|
233
298
|
}
|
|
234
|
-
export function counts(connection, rooms) {
|
|
235
|
-
return rooms.map(({ key, roots }) => counted(connection, key, roots));
|
|
236
|
-
}
|
|
237
299
|
function erasing(connection, key, locator) {
|
|
238
300
|
connection.run('INSERT OR REPLACE INTO replica_erasing (key, locator) VALUES (?, ?)', [
|
|
239
301
|
key,
|
|
240
302
|
locator,
|
|
241
303
|
]);
|
|
242
304
|
}
|
|
305
|
+
function saved(connection, key, entry, at) {
|
|
306
|
+
if (!entry.present || entry.created !== null)
|
|
307
|
+
return entry;
|
|
308
|
+
const [held] = connection.rows(`SELECT created FROM replica_entries WHERE key = ? AND layer = ${OVERLAY} AND path = ? AND present = 1`, [key, entry.path]);
|
|
309
|
+
return { ...entry, created: text(held?.created) ?? at };
|
|
310
|
+
}
|
|
243
311
|
function commitRoom(connection, room) {
|
|
244
312
|
const touched = new Set();
|
|
245
313
|
recomposed(connection, room.key, room.composed.fingerprint);
|
|
@@ -261,8 +329,9 @@ function commitRoom(connection, room) {
|
|
|
261
329
|
removeEntry(connection, room.key, CONFIRMED, path);
|
|
262
330
|
touched.add(path);
|
|
263
331
|
}
|
|
332
|
+
const savedAt = new Date().toISOString();
|
|
264
333
|
for (const entry of room.overlay?.set ?? []) {
|
|
265
|
-
putEntry(connection, room.key, OVERLAY, entry, entry.present, composition);
|
|
334
|
+
putEntry(connection, room.key, OVERLAY, saved(connection, room.key, entry, savedAt), entry.present, composition);
|
|
266
335
|
touched.add(entry.path);
|
|
267
336
|
}
|
|
268
337
|
for (const path of room.overlay?.cleared ?? []) {
|
|
@@ -272,40 +341,68 @@ function commitRoom(connection, room) {
|
|
|
272
341
|
const changed = [...touched];
|
|
273
342
|
revealed(connection, room.key, changed);
|
|
274
343
|
if (room.record !== undefined) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
]);
|
|
344
|
+
const replaced = heldModel(connection, room.locator);
|
|
345
|
+
connection.run(KEEP_MODEL, [room.record, room.record, room.record, room.record]);
|
|
346
|
+
connection.run(`INSERT OR REPLACE INTO replica_rooms (locator, key, record) VALUES (?, ?, ${MODEL_LEFT})`, [room.locator, room.key, room.record, room.record, room.record]);
|
|
347
|
+
letGo(connection, replaced);
|
|
280
348
|
}
|
|
281
349
|
return room.published === undefined ? changed : null;
|
|
282
350
|
}
|
|
351
|
+
const SCHEMA = '$.model.snapshot.schema';
|
|
352
|
+
const KEEP_MODEL = `INSERT OR IGNORE INTO replica_models (identity, version, schema)
|
|
353
|
+
SELECT json_extract(?, '$.model.snapshot.identity'), json_extract(?, '$.model.snapshot.version'),
|
|
354
|
+
json_extract(?, '${SCHEMA}')
|
|
355
|
+
WHERE json_type(?, '${SCHEMA}') = 'text'`;
|
|
356
|
+
const MODEL_LEFT = `CASE WHEN json_type(?, '${SCHEMA}') = 'text' THEN json_set(?, '${SCHEMA}', json('null')) ELSE ? END`;
|
|
357
|
+
const HELD_MODEL = `SELECT json_extract(record, '$.model.snapshot.identity') AS identity,
|
|
358
|
+
json_extract(record, '$.model.snapshot.version') AS version
|
|
359
|
+
FROM replica_rooms WHERE locator = ?`;
|
|
360
|
+
const UNHELD_MODEL = `DELETE FROM replica_models WHERE identity = ? AND version = ? AND NOT EXISTS (
|
|
361
|
+
SELECT 1 FROM replica_rooms r
|
|
362
|
+
WHERE json_extract(r.record, '$.model.snapshot.identity') = replica_models.identity
|
|
363
|
+
AND json_extract(r.record, '$.model.snapshot.version') = replica_models.version
|
|
364
|
+
)`;
|
|
365
|
+
function heldModel(connection, locator) {
|
|
366
|
+
return connection.rows(HELD_MODEL, [locator])[0];
|
|
367
|
+
}
|
|
368
|
+
function letGo(connection, model) {
|
|
369
|
+
if (model !== undefined)
|
|
370
|
+
connection.run(UNHELD_MODEL, [model.identity, model.version]);
|
|
371
|
+
}
|
|
372
|
+
const HELD_RECORDS = `SELECT r.locator, r.key, CASE WHEN m.schema IS NULL THEN r.record
|
|
373
|
+
ELSE json_set(r.record, '${SCHEMA}', m.schema) END AS record
|
|
374
|
+
FROM replica_rooms r
|
|
375
|
+
LEFT JOIN replica_models m
|
|
376
|
+
ON m.identity = json_extract(r.record, '$.model.snapshot.identity')
|
|
377
|
+
AND m.version = json_extract(r.record, '$.model.snapshot.version')`;
|
|
283
378
|
export function commit(connection, rooms) {
|
|
284
|
-
const counted = {};
|
|
285
379
|
const changed = {};
|
|
286
380
|
const indexed = {};
|
|
287
381
|
for (const room of rooms) {
|
|
288
382
|
changed[room.locator] = commitRoom(connection, room);
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
383
|
+
connection.run('UPDATE replica_rooms SET documents = ? WHERE locator = ? AND key = ?', [
|
|
384
|
+
counted(connection, room.key, room.roots ?? rootsOf(connection, room.locator)),
|
|
385
|
+
room.locator,
|
|
386
|
+
room.key,
|
|
387
|
+
]);
|
|
292
388
|
const [held] = connection.rows('SELECT rederiving FROM replica_keys WHERE key = ?', [room.key]);
|
|
293
389
|
indexed[room.locator] = held !== undefined && held.rederiving === null;
|
|
294
390
|
}
|
|
295
|
-
return {
|
|
391
|
+
return { changed, indexed };
|
|
296
392
|
}
|
|
297
393
|
function rootsOf(connection, locator) {
|
|
298
394
|
const [row] = connection.rows('SELECT record FROM replica_rooms WHERE locator = ?', [locator]);
|
|
299
395
|
const record = json(row?.record);
|
|
300
|
-
return record?.typedSelection?.roots ?? [];
|
|
396
|
+
return record?.declaration.typedSelection?.roots ?? [];
|
|
301
397
|
}
|
|
302
398
|
export function fenceRooms(connection, locators) {
|
|
303
399
|
for (const locator of locators) {
|
|
304
400
|
for (const row of connection.rows('SELECT key FROM replica_keys WHERE locator = ? UNION SELECT key FROM replica_rooms WHERE locator = ?', [locator, locator])) {
|
|
305
401
|
erasing(connection, number(row.key), locator);
|
|
306
402
|
}
|
|
403
|
+
const replaced = heldModel(connection, locator);
|
|
307
404
|
connection.run('DELETE FROM replica_rooms WHERE locator = ?', [locator]);
|
|
308
|
-
connection
|
|
405
|
+
letGo(connection, replaced);
|
|
309
406
|
}
|
|
310
407
|
return null;
|
|
311
408
|
}
|
|
@@ -359,17 +456,20 @@ export function rederive(connection, key, composed, bytes, ms, floor) {
|
|
|
359
456
|
let built = 0;
|
|
360
457
|
let read = false;
|
|
361
458
|
reading: for (;;) {
|
|
362
|
-
const rows = connection.rows('SELECT id, path,
|
|
459
|
+
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]);
|
|
363
460
|
for (const row of rows) {
|
|
364
461
|
const id = number(row.id);
|
|
365
462
|
const path = text(row.path);
|
|
366
|
-
const
|
|
367
|
-
const made = derived(path, content, json(row.outline), composition);
|
|
463
|
+
const size = row.size === null ? 0 : number(row.size);
|
|
368
464
|
dropDerived(connection, id);
|
|
369
|
-
writeDerived(connection, id, key,
|
|
370
|
-
|
|
465
|
+
writeDerived(connection, id, key, path, {
|
|
466
|
+
type: text(row.type),
|
|
467
|
+
fields: json(row.fields),
|
|
468
|
+
links: json(row.links) ?? [],
|
|
469
|
+
}, composition);
|
|
470
|
+
built += size;
|
|
371
471
|
last = id;
|
|
372
|
-
spent += ENTRY_WEIGHT +
|
|
472
|
+
spent += ENTRY_WEIGHT + size;
|
|
373
473
|
if (spent >= bytes || (spent >= floor && performance.now() - started >= ms))
|
|
374
474
|
break reading;
|
|
375
475
|
}
|
|
@@ -381,63 +481,31 @@ export function rederive(connection, key, composed, bytes, ms, floor) {
|
|
|
381
481
|
connection.run('UPDATE replica_keys SET rederiving = ? WHERE key = ?', [read ? null : last, key]);
|
|
382
482
|
return { done: read, text: built };
|
|
383
483
|
}
|
|
384
|
-
function outlinedAgain(connection, row, key, content, composition) {
|
|
385
|
-
const id = number(row.id);
|
|
386
|
-
const path = text(row.path);
|
|
387
|
-
const made = derived(path, content, json(row.outline), composition);
|
|
388
|
-
connection.run('UPDATE replica_entries SET content = ?, type = ?, declared = ?, queried = ?, folder = ?, fields = ? WHERE id = ?', [content, made.type, made.declared, flag(made.queried), made.folder, made.fields, id]);
|
|
389
|
-
dropDerived(connection, id);
|
|
390
|
-
writeDerived(connection, id, key, made);
|
|
391
|
-
}
|
|
392
|
-
export function holdTexts(connection, request) {
|
|
393
|
-
const held = [];
|
|
394
|
-
for (const { path, revision, content } of request.texts) {
|
|
395
|
-
const [row] = connection.rows(`SELECT id, path, outline FROM replica_entries WHERE key = ? AND layer = ${CONFIRMED} AND path = ? AND revision = ? AND content IS NULL AND outline IS NOT NULL`, [request.key, path, revision]);
|
|
396
|
-
if (row === undefined)
|
|
397
|
-
continue;
|
|
398
|
-
outlinedAgain(connection, row, request.key, content, request.composed.composition);
|
|
399
|
-
connection.run('INSERT OR IGNORE INTO replica_opened (locator, path) VALUES (?, ?)', [
|
|
400
|
-
request.locator,
|
|
401
|
-
path,
|
|
402
|
-
]);
|
|
403
|
-
held.push(path);
|
|
404
|
-
}
|
|
405
|
-
return held;
|
|
406
|
-
}
|
|
407
|
-
export function unholdTexts(connection, request) {
|
|
408
|
-
connection.run('DELETE FROM replica_opened WHERE locator = ?', [request.locator]);
|
|
409
|
-
const rows = connection.rows(`SELECT id, path, outline, octet_length(content) AS bytes FROM replica_entries WHERE key = ? AND layer = ${CONFIRMED} AND visible = 1 AND content IS NOT NULL AND outline IS NOT NULL`, [request.key]);
|
|
410
|
-
for (const row of rows) {
|
|
411
|
-
outlinedAgain(connection, row, request.key, null, request.composed.composition);
|
|
412
|
-
}
|
|
413
|
-
return {
|
|
414
|
-
paths: rows.map((row) => text(row.path)),
|
|
415
|
-
bytes: rows.reduce((total, row) => total + number(row.bytes), 0),
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
export function remembered(connection, request) {
|
|
419
|
-
connection.run(`INSERT OR IGNORE INTO replica_opened (locator, path) SELECT ?, path FROM replica_entries WHERE key = ? AND layer = ${CONFIRMED} AND content IS NOT NULL AND binary IS NULL AND path IN (SELECT value FROM json_each(?))`, [request.locator, request.key, paths(request.paths)]);
|
|
420
|
-
return null;
|
|
421
|
-
}
|
|
422
|
-
export function wanted(connection, request) {
|
|
423
|
-
return connection
|
|
424
|
-
.rows(`SELECT e.path, e.revision FROM replica_opened o JOIN replica_entries e ON e.key = ? AND e.layer = ${CONFIRMED} AND e.path = o.path WHERE o.locator = ? AND e.content IS NULL AND e.outline IS NOT NULL AND e.revision IS NOT NULL`, [request.key, request.locator])
|
|
425
|
-
.map((row) => ({ path: text(row.path), revision: text(row.revision) }));
|
|
426
|
-
}
|
|
427
484
|
function heldEntry(row) {
|
|
428
485
|
return {
|
|
429
486
|
path: text(row.path),
|
|
430
487
|
content: text(row.content),
|
|
431
|
-
outline: json(row.outline),
|
|
432
488
|
binary: json(row.binary),
|
|
433
489
|
revision: text(row.revision),
|
|
434
490
|
fileId: text(row.file_id),
|
|
491
|
+
created: text(row.created),
|
|
435
492
|
change: json(row.change),
|
|
436
493
|
written: text(row.written),
|
|
437
494
|
type: text(row.type),
|
|
495
|
+
facts: factsHeld(row),
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
function factsHeld(row) {
|
|
499
|
+
const type = text(row.declared);
|
|
500
|
+
if (type === null)
|
|
501
|
+
return null;
|
|
502
|
+
return {
|
|
503
|
+
type,
|
|
504
|
+
fields: json(row.fields) ?? {},
|
|
505
|
+
links: json(row.links) ?? [],
|
|
438
506
|
};
|
|
439
507
|
}
|
|
440
|
-
const ENTRY_COLUMNS = 'e.path, e.content, e.
|
|
508
|
+
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';
|
|
441
509
|
function unrefreshed(alias) {
|
|
442
510
|
return `NOT EXISTS (SELECT 1 FROM replica_entries r WHERE r.key = ${alias}.key AND r.layer = ${REFRESHED} AND r.path = ${alias}.path)`;
|
|
443
511
|
}
|
|
@@ -448,22 +516,6 @@ function viewed(view, alias) {
|
|
|
448
516
|
return `${alias}.layer = ${CONFIRMED}`;
|
|
449
517
|
return `${alias}.present = 1 AND (${alias}.layer = ${REFRESHED} OR (${alias}.layer = ${CONFIRMED} AND ${unrefreshed(alias)}))`;
|
|
450
518
|
}
|
|
451
|
-
function layers(view) {
|
|
452
|
-
if (view !== 'refreshed')
|
|
453
|
-
return [viewed(view, 'e')];
|
|
454
|
-
return [
|
|
455
|
-
`e.layer = ${REFRESHED} AND e.present = 1`,
|
|
456
|
-
`e.layer = ${CONFIRMED} AND e.present = 1 AND ${unrefreshed('e')}`,
|
|
457
|
-
];
|
|
458
|
-
}
|
|
459
|
-
function pathOrdered(connection, columns, key, view, condition, after, limit) {
|
|
460
|
-
const beyond = after === null ? '' : ' AND e.path > ?';
|
|
461
|
-
const parts = layers(view).map((layer) => `SELECT ${columns} FROM replica_entries e WHERE e.key = ? AND ${layer}${condition}${beyond}`);
|
|
462
|
-
return connection.rows(`${parts.join(' UNION ALL ')} ORDER BY path LIMIT ?`, [
|
|
463
|
-
...parts.flatMap(() => (after === null ? [key] : [key, after])),
|
|
464
|
-
limit,
|
|
465
|
-
]);
|
|
466
|
-
}
|
|
467
519
|
export function entries(connection, key, view, list) {
|
|
468
520
|
if (list.length === 0)
|
|
469
521
|
return [];
|
|
@@ -482,68 +534,167 @@ export function typed(connection, key, view, list) {
|
|
|
482
534
|
binary: number(row.binary) === 1,
|
|
483
535
|
}));
|
|
484
536
|
}
|
|
485
|
-
export function referrers(connection, key, view, targets
|
|
537
|
+
export function referrers(connection, key, view, targets) {
|
|
486
538
|
if (targets.length === 0)
|
|
487
539
|
return [];
|
|
488
540
|
return connection
|
|
489
|
-
.rows(`SELECT DISTINCT e.path, l.target
|
|
490
|
-
.map((row) => ({
|
|
491
|
-
path: text(row.path),
|
|
492
|
-
target: text(row.target),
|
|
493
|
-
kind: text(row.kind),
|
|
494
|
-
}));
|
|
495
|
-
}
|
|
496
|
-
export function files(connection, key, view, after, limit) {
|
|
497
|
-
return pathOrdered(connection, 'e.path, e.revision', key, view, ' AND e.content IS NOT NULL AND e.revision IS NOT NULL', after, limit).map((row) => ({ path: text(row.path), revision: text(row.revision) }));
|
|
541
|
+
.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)])
|
|
542
|
+
.map((row) => ({ path: text(row.path), target: text(row.target) }));
|
|
498
543
|
}
|
|
499
|
-
export function folders(connection, key,
|
|
500
|
-
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(
|
|
544
|
+
export function folders(connection, key, list) {
|
|
545
|
+
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);
|
|
501
546
|
}
|
|
502
547
|
export function typedWithin(connection, key, view, roots, within) {
|
|
503
548
|
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);
|
|
504
549
|
}
|
|
550
|
+
const ORPHANS = `SELECT k.key FROM replica_keys k
|
|
551
|
+
WHERE NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
552
|
+
AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)
|
|
553
|
+
AND (${UNPARTED} OR NOT EXISTS (
|
|
554
|
+
SELECT 1 FROM replica_entries e WHERE e.key = k.key
|
|
555
|
+
UNION ALL SELECT 1 FROM replica_folders f WHERE f.key = k.key
|
|
556
|
+
))`;
|
|
557
|
+
const STAGED = `SELECT DISTINCT k.locator FROM replica_keys k
|
|
558
|
+
WHERE NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
559
|
+
AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)
|
|
560
|
+
AND k.key NOT IN (${ORPHANS})`;
|
|
505
561
|
export function opened(connection) {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
562
|
+
return {
|
|
563
|
+
orphans: connection.rows(ORPHANS).map((row) => number(row.key)),
|
|
564
|
+
erasing: connection.rows('SELECT 1 FROM replica_erasing LIMIT 1').length > 0,
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
const ROOM_ROWS = `SELECT h.locator, h.key, h.record, r.documents, k.rederiving IS NULL AS indexed
|
|
568
|
+
FROM (${HELD_RECORDS}) h
|
|
569
|
+
JOIN replica_rooms r ON r.locator = h.locator
|
|
570
|
+
LEFT JOIN replica_keys k ON k.key = h.key`;
|
|
571
|
+
function roomRow(row) {
|
|
572
|
+
return {
|
|
573
|
+
locator: text(row.locator),
|
|
574
|
+
key: number(row.key),
|
|
575
|
+
record: text(row.record),
|
|
576
|
+
indexed: number(row.indexed) === 1,
|
|
577
|
+
documents: number(row.documents),
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
export function heldRooms(connection, locators) {
|
|
581
|
+
return (locators === null
|
|
582
|
+
? connection.rows(`${ROOM_ROWS} ORDER BY h.locator`)
|
|
583
|
+
: connection.rows(`${ROOM_ROWS} WHERE h.locator IN (SELECT value FROM json_each(?)) ORDER BY h.locator`, [JSON.stringify(locators)])).map(roomRow);
|
|
584
|
+
}
|
|
585
|
+
export function heldRoom(connection, locator) {
|
|
586
|
+
return heldRooms(connection, [locator])[0] ?? null;
|
|
587
|
+
}
|
|
588
|
+
export function overlaid(connection, key) {
|
|
589
|
+
return connection
|
|
590
|
+
.rows(`SELECT path FROM replica_entries WHERE key = ? AND layer = ${OVERLAY}`, [key])
|
|
591
|
+
.map((row) => text(row.path));
|
|
592
|
+
}
|
|
593
|
+
const COVER_ROWS = `SELECT r.locator, r.key, r.documents, json_extract(r.record, '$.transfer') AS transfer,
|
|
594
|
+
json_extract(r.record, '$.roomVersion') AS room_version,
|
|
595
|
+
coalesce(json_extract(r.record, '$.inventory.bytes'), 0) AS bytes,
|
|
596
|
+
json_extract(r.record, '$.declaration.replica.offline') AS offline,
|
|
597
|
+
k.rederiving IS NULL AS indexed
|
|
598
|
+
FROM replica_rooms r LEFT JOIN replica_keys k ON k.key = r.key`;
|
|
599
|
+
const COVER_MARK = `UPDATE cover_clock SET at = at + 1;
|
|
600
|
+
INSERT OR REPLACE INTO cover_changes (locator, at) SELECT`;
|
|
601
|
+
const COVER_LOG = [
|
|
602
|
+
'CREATE TEMP TABLE cover_clock (token TEXT NOT NULL, at INTEGER NOT NULL)',
|
|
603
|
+
'CREATE TEMP TABLE cover_changes (locator TEXT PRIMARY KEY, at INTEGER NOT NULL) WITHOUT ROWID',
|
|
604
|
+
'CREATE INDEX temp.cover_changes_at ON cover_changes (at)',
|
|
605
|
+
'INSERT INTO temp.cover_clock (token, at) VALUES (lower(hex(randomblob(8))), 0)',
|
|
606
|
+
`CREATE TEMP TRIGGER cover_room_added AFTER INSERT ON replica_rooms BEGIN
|
|
607
|
+
${COVER_MARK} NEW.locator, at FROM cover_clock; END`,
|
|
608
|
+
`CREATE TEMP TRIGGER cover_room_changed AFTER UPDATE ON replica_rooms BEGIN
|
|
609
|
+
${COVER_MARK} OLD.locator, at FROM cover_clock;
|
|
610
|
+
INSERT OR REPLACE INTO cover_changes (locator, at) SELECT NEW.locator, at FROM cover_clock; END`,
|
|
611
|
+
`CREATE TEMP TRIGGER cover_room_removed AFTER DELETE ON replica_rooms BEGIN
|
|
612
|
+
${COVER_MARK} OLD.locator, at FROM cover_clock; END`,
|
|
613
|
+
`CREATE TEMP TRIGGER cover_key_changed AFTER UPDATE OF rederiving ON replica_keys BEGIN
|
|
614
|
+
${COVER_MARK} r.locator, c.at FROM replica_rooms r, cover_clock c WHERE r.key = NEW.key; END`,
|
|
615
|
+
`CREATE TEMP TRIGGER cover_key_removed AFTER DELETE ON replica_keys BEGIN
|
|
616
|
+
${COVER_MARK} r.locator, c.at FROM replica_rooms r, cover_clock c WHERE r.key = OLD.key; END`,
|
|
617
|
+
];
|
|
618
|
+
function coveredRow(row) {
|
|
619
|
+
return {
|
|
620
|
+
locator: text(row.locator),
|
|
621
|
+
key: number(row.key),
|
|
622
|
+
transfer: text(row.transfer),
|
|
623
|
+
roomVersion: text(row.room_version),
|
|
624
|
+
indexed: number(row.indexed) === 1,
|
|
625
|
+
documents: number(row.documents),
|
|
626
|
+
bytes: number(row.bytes),
|
|
627
|
+
offline: text(row.offline),
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
function coverLog(connection) {
|
|
631
|
+
const logged = connection.rows("SELECT 1 FROM sqlite_temp_schema WHERE type = 'table' AND name = 'cover_clock'");
|
|
632
|
+
if (logged.length === 0)
|
|
633
|
+
return null;
|
|
634
|
+
const [clock] = connection.rows('SELECT token, at FROM temp.cover_clock');
|
|
635
|
+
return { token: text(clock.token), at: number(clock.at) };
|
|
636
|
+
}
|
|
637
|
+
export function covering(connection, since) {
|
|
638
|
+
let log = coverLog(connection);
|
|
639
|
+
if (log === null) {
|
|
640
|
+
for (const statement of COVER_LOG)
|
|
641
|
+
connection.run(statement);
|
|
642
|
+
log = coverLog(connection);
|
|
643
|
+
}
|
|
644
|
+
if (since === null || since.token !== log.token) {
|
|
645
|
+
return {
|
|
646
|
+
log,
|
|
647
|
+
whole: true,
|
|
648
|
+
rooms: connection.rows(`${COVER_ROWS} ORDER BY r.locator`).map(coveredRow),
|
|
649
|
+
removed: [],
|
|
650
|
+
};
|
|
509
651
|
}
|
|
510
652
|
return {
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
.rows(
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
locator: text(row.locator),
|
|
521
|
-
transfer: text(row.transfer),
|
|
522
|
-
rederiving: cursor(row.rederiving),
|
|
523
|
-
})),
|
|
524
|
-
facts,
|
|
525
|
-
presentation: connection.rows('SELECT app, value FROM replica_presentation').map((row) => ({
|
|
526
|
-
app: text(row.app),
|
|
527
|
-
value: text(row.value),
|
|
528
|
-
})),
|
|
529
|
-
overlaid: connection
|
|
530
|
-
.rows(`SELECT key, path FROM replica_entries WHERE layer = ${OVERLAY}`)
|
|
531
|
-
.map((row) => ({ key: number(row.key), path: text(row.path) })),
|
|
532
|
-
orphans: connection
|
|
533
|
-
.rows(`SELECT k.key FROM replica_keys k
|
|
534
|
-
WHERE NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
535
|
-
AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)`)
|
|
536
|
-
.map((row) => number(row.key)),
|
|
537
|
-
erasing: connection.rows('SELECT key, locator FROM replica_erasing').map((row) => ({
|
|
538
|
-
key: number(row.key),
|
|
539
|
-
locator: text(row.locator),
|
|
540
|
-
})),
|
|
653
|
+
log,
|
|
654
|
+
whole: false,
|
|
655
|
+
rooms: connection
|
|
656
|
+
.rows(`${COVER_ROWS} WHERE r.locator IN (SELECT locator FROM temp.cover_changes WHERE at > ?) ORDER BY r.locator`, [since.at])
|
|
657
|
+
.map(coveredRow),
|
|
658
|
+
removed: connection
|
|
659
|
+
.rows(`SELECT c.locator FROM temp.cover_changes c WHERE c.at > ?
|
|
660
|
+
AND NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.locator = c.locator)`, [since.at])
|
|
661
|
+
.map((row) => text(row.locator)),
|
|
541
662
|
};
|
|
542
663
|
}
|
|
543
|
-
export function
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
664
|
+
export function offlineMode(connection, locator) {
|
|
665
|
+
const [held] = connection.rows("SELECT json_extract(record, '$.declaration.replica.offline') AS offline FROM replica_rooms WHERE locator = ?", [locator]);
|
|
666
|
+
return text(held?.offline);
|
|
667
|
+
}
|
|
668
|
+
export function stagedLocators(connection) {
|
|
669
|
+
return connection.rows(STAGED).map((row) => text(row.locator));
|
|
670
|
+
}
|
|
671
|
+
export function heldLocators(connection) {
|
|
672
|
+
return connection
|
|
673
|
+
.rows(`SELECT locator FROM replica_rooms UNION ${STAGED}`)
|
|
674
|
+
.map((row) => text(row.locator));
|
|
675
|
+
}
|
|
676
|
+
export function demoted(connection) {
|
|
677
|
+
connection.run(`UPDATE replica_rooms SET record = json_set(record, '$.model.status', 'cached')
|
|
678
|
+
WHERE json_extract(record, '$.model.status') = 'current'`);
|
|
679
|
+
return null;
|
|
680
|
+
}
|
|
681
|
+
export function facts(connection, names) {
|
|
682
|
+
const held = {};
|
|
683
|
+
for (const row of names === null
|
|
684
|
+
? connection.rows('SELECT name, value FROM replica_facts')
|
|
685
|
+
: connection.rows('SELECT name, value FROM replica_facts WHERE name IN (SELECT value FROM json_each(?))', [JSON.stringify(names)])) {
|
|
686
|
+
held[text(row.name)] = text(row.value);
|
|
548
687
|
}
|
|
688
|
+
return held;
|
|
689
|
+
}
|
|
690
|
+
export function presentation(connection, app) {
|
|
691
|
+
const [held] = connection.rows('SELECT value FROM replica_presentation WHERE app = ?', [app]);
|
|
692
|
+
return text(held?.value);
|
|
693
|
+
}
|
|
694
|
+
export function tablesOf(statements) {
|
|
695
|
+
return statements.flatMap((statement) => /^CREATE TABLE (\w+)/u.exec(statement)?.[1] ?? []);
|
|
696
|
+
}
|
|
697
|
+
export function ended(connection) {
|
|
698
|
+
for (const table of tablesOf(REPLICA_TABLES))
|
|
699
|
+
connection.run(`DELETE FROM ${table}`);
|
|
549
700
|
}
|