@agentshouse/mdruntime 0.1.0-c52f87bb83a70 → 0.1.0-c55c6f69061ee
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 +11 -9
- package/dist/adapter.d.ts +94 -63
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +2 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +28 -4
- package/dist/binary.d.ts +0 -5
- package/dist/binary.d.ts.map +1 -1
- package/dist/binary.js +1 -1
- package/dist/bootstrap.d.ts +16 -8
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +142 -94
- package/dist/change-set.d.ts +1 -7
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +1 -4
- package/dist/client.d.ts +10 -11
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +54 -8
- package/dist/collection.d.ts +5 -3
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +24 -19
- package/dist/compile.d.ts +7 -2
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +125 -48
- package/dist/connection.d.ts +10 -10
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1611 -1313
- package/dist/context.d.ts +2 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/{chat/context.js → context.js} +1 -4
- package/dist/conversation.d.ts +17 -14
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +262 -159
- package/dist/durability.d.ts +1 -5
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- package/dist/entry.d.ts +1 -0
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +2 -1
- package/dist/file.d.ts +2 -11
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +6 -4
- package/dist/host.d.ts +4 -7
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +52 -32
- 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 +13 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -3
- package/dist/job.d.ts +14 -0
- package/dist/job.d.ts.map +1 -0
- package/dist/job.js +28 -0
- package/dist/live.d.ts +2 -1
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +9 -5
- package/dist/media.d.ts +8 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +218 -0
- package/dist/memory.d.ts +10 -6
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +177 -100
- package/dist/messages.d.ts +28 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +89 -0
- package/dist/model.d.ts +6 -1
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +7 -10
- package/dist/observation.d.ts +3 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +8 -3
- package/dist/outcome.d.ts +13 -6
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +16 -5
- package/dist/overlay.js +1 -1
- package/dist/paths.d.ts +1 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +4 -0
- package/dist/phrase.d.ts +3 -0
- package/dist/phrase.d.ts.map +1 -0
- package/dist/phrase.js +8 -0
- package/dist/query.d.ts +14 -1
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +32 -2
- package/dist/reach.d.ts +13 -7
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +80 -64
- package/dist/react.d.ts +1 -0
- package/dist/react.d.ts.map +1 -1
- package/dist/readiness.d.ts +14 -17
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +2 -8
- package/dist/recovery.d.ts +1 -0
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +26 -0
- package/dist/refresh.d.ts +10 -4
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +104 -30
- package/dist/replica.d.ts +5 -5
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +7 -7
- package/dist/room-draft.d.ts +20 -3
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +131 -51
- package/dist/rows.d.ts +17 -7
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +120 -87
- package/dist/save.d.ts +2 -0
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +13 -7
- package/dist/search.d.ts +12 -95
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +10 -349
- package/dist/shared.d.ts +0 -1
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +0 -10
- package/dist/status.d.ts +4 -3
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +3 -1
- package/dist/store.d.ts +34 -60
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +71 -275
- package/dist/stored.d.ts +42 -23
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +198 -210
- package/dist/tables.d.ts +79 -46
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +101 -53
- 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 +35 -96
- package/dist/wire.d.ts +6 -9
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +3 -3
- package/package.json +14 -18
- package/AGENTS.md +0 -310
- package/dist/chat/acts.d.ts +0 -33
- package/dist/chat/acts.d.ts.map +0 -1
- package/dist/chat/acts.js +0 -36
- package/dist/chat/context.d.ts +0 -3
- package/dist/chat/context.d.ts.map +0 -1
- package/dist/chat/earlier.d.ts +0 -10
- package/dist/chat/earlier.d.ts.map +0 -1
- package/dist/chat/earlier.js +0 -54
- package/dist/chat/house.d.ts +0 -25
- package/dist/chat/house.d.ts.map +0 -1
- package/dist/chat/house.js +0 -38
- package/dist/chat/input.d.ts +0 -11
- package/dist/chat/input.d.ts.map +0 -1
- package/dist/chat/input.js +0 -92
- package/dist/chat/kit.d.ts +0 -61
- package/dist/chat/kit.d.ts.map +0 -1
- package/dist/chat/kit.js +0 -123
- package/dist/chat/lasting.d.ts +0 -6
- package/dist/chat/lasting.d.ts.map +0 -1
- package/dist/chat/lasting.js +0 -19
- package/dist/chat/list.d.ts +0 -28
- package/dist/chat/list.d.ts.map +0 -1
- package/dist/chat/list.js +0 -103
- package/dist/chat/markdown.d.ts +0 -21
- package/dist/chat/markdown.d.ts.map +0 -1
- package/dist/chat/markdown.js +0 -246
- package/dist/chat/message.d.ts +0 -7
- package/dist/chat/message.d.ts.map +0 -1
- package/dist/chat/message.js +0 -237
- package/dist/chat/messages.d.ts +0 -29
- package/dist/chat/messages.d.ts.map +0 -1
- package/dist/chat/messages.js +0 -190
- package/dist/chat/name.d.ts +0 -7
- package/dist/chat/name.d.ts.map +0 -1
- package/dist/chat/name.js +0 -37
- package/dist/chat/opened.d.ts +0 -12
- package/dist/chat/opened.d.ts.map +0 -1
- package/dist/chat/opened.js +0 -19
- package/dist/chat/overlay.d.ts +0 -32
- package/dist/chat/overlay.d.ts.map +0 -1
- package/dist/chat/overlay.js +0 -103
- package/dist/chat/parts.d.ts +0 -58
- package/dist/chat/parts.d.ts.map +0 -1
- package/dist/chat/parts.js +0 -120
- package/dist/chat/pieces.d.ts +0 -62
- package/dist/chat/pieces.d.ts.map +0 -1
- package/dist/chat/pieces.js +0 -1110
- package/dist/chat/process.d.ts +0 -20
- package/dist/chat/process.d.ts.map +0 -1
- package/dist/chat/process.js +0 -32
- package/dist/chat/provider.d.ts +0 -68
- package/dist/chat/provider.d.ts.map +0 -1
- package/dist/chat/provider.js +0 -619
- package/dist/chat/question.d.ts +0 -9
- package/dist/chat/question.d.ts.map +0 -1
- package/dist/chat/question.js +0 -69
- package/dist/chat/questions.d.ts +0 -31
- package/dist/chat/questions.d.ts.map +0 -1
- package/dist/chat/questions.js +0 -111
- package/dist/chat/read.d.ts +0 -5
- package/dist/chat/read.d.ts.map +0 -1
- package/dist/chat/read.js +0 -13
- package/dist/chat/ready.d.ts +0 -37
- package/dist/chat/ready.d.ts.map +0 -1
- package/dist/chat/ready.js +0 -93
- package/dist/chat/room-file.d.ts +0 -8
- package/dist/chat/room-file.d.ts.map +0 -1
- package/dist/chat/room-file.js +0 -72
- package/dist/chat/steady.d.ts +0 -3
- package/dist/chat/steady.d.ts.map +0 -1
- package/dist/chat/steady.js +0 -26
- package/dist/chat/style.d.ts +0 -4
- package/dist/chat/style.d.ts.map +0 -1
- package/dist/chat/style.js +0 -306
- package/dist/chat.d.ts +0 -15
- package/dist/chat.d.ts.map +0 -1
- package/dist/chat.js +0 -8
- package/dist/reads.d.ts +0 -11
- package/dist/reads.d.ts.map +0 -1
- package/dist/reads.js +0 -98
package/dist/stored.js
CHANGED
|
@@ -1,28 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { composedOf } from './collection.js';
|
|
2
|
+
import { described, derived } from './rows.js';
|
|
3
3
|
export const REPLICA_TABLES = [
|
|
4
|
-
'CREATE TABLE replica_keys (key INTEGER PRIMARY KEY AUTOINCREMENT, locator TEXT NOT NULL, transfer TEXT NOT NULL,
|
|
4
|
+
'CREATE TABLE replica_keys (key INTEGER PRIMARY KEY AUTOINCREMENT, locator TEXT NOT NULL, transfer TEXT NOT NULL, fingerprint TEXT NOT NULL, rederiving INTEGER)',
|
|
5
5
|
'CREATE TABLE replica_rooms (locator TEXT PRIMARY KEY, key INTEGER NOT NULL, record TEXT NOT NULL) WITHOUT ROWID',
|
|
6
|
-
'CREATE TABLE
|
|
7
|
-
'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, queried INTEGER NOT NULL, binary TEXT, revision TEXT, file_id TEXT, change TEXT, written TEXT, type TEXT, declared TEXT, folder TEXT NOT NULL, fields TEXT, content TEXT)',
|
|
6
|
+
'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, queried INTEGER NOT NULL, binary TEXT, revision TEXT, file_id TEXT, change TEXT, written TEXT, type TEXT, declared TEXT, folder TEXT NOT NULL, fields TEXT, content TEXT, outline TEXT)',
|
|
8
7
|
'CREATE UNIQUE INDEX replica_entries_path ON replica_entries (key, layer, path)',
|
|
9
|
-
'CREATE INDEX replica_entries_visible ON replica_entries (key, visible, path)',
|
|
8
|
+
'CREATE INDEX replica_entries_visible ON replica_entries (key, visible, present, path, binary)',
|
|
10
9
|
'CREATE INDEX replica_entries_folder ON replica_entries (key, folder, path)',
|
|
11
|
-
'CREATE
|
|
12
|
-
'CREATE INDEX
|
|
13
|
-
'CREATE TABLE
|
|
14
|
-
'CREATE
|
|
10
|
+
'CREATE INDEX replica_entries_type ON replica_entries (key, type, visible, present, queried, folder)',
|
|
11
|
+
'CREATE INDEX replica_entries_key ON replica_entries (key)',
|
|
12
|
+
'CREATE TABLE replica_values (doc INTEGER NOT NULL, keys TEXT NOT NULL, places TEXT NOT NULL, item INTEGER NOT NULL, kind TEXT NOT NULL, value, size INTEGER, PRIMARY KEY (doc, keys, places, item)) WITHOUT ROWID',
|
|
13
|
+
'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
|
+
'CREATE INDEX replica_links_target ON replica_links (key, target, kind, keys)',
|
|
15
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',
|
|
16
16
|
'CREATE TABLE replica_erasing (key INTEGER PRIMARY KEY, locator TEXT NOT NULL)',
|
|
17
|
-
'CREATE TABLE
|
|
18
|
-
'CREATE TABLE replica_received_parts (key TEXT NOT NULL, part INTEGER NOT NULL, value TEXT NOT NULL, PRIMARY KEY (key, part)) WITHOUT ROWID',
|
|
17
|
+
'CREATE TABLE replica_opened (locator TEXT NOT NULL, path TEXT NOT NULL, PRIMARY KEY (locator, path)) WITHOUT ROWID',
|
|
19
18
|
'CREATE TABLE replica_facts (name TEXT PRIMARY KEY, value TEXT NOT NULL) WITHOUT ROWID',
|
|
20
19
|
'CREATE TABLE replica_presentation (app TEXT PRIMARY KEY, value TEXT NOT NULL) WITHOUT ROWID',
|
|
21
20
|
];
|
|
22
21
|
const CONFIRMED = 0;
|
|
23
22
|
const OVERLAY = 1;
|
|
24
23
|
const REFRESHED = 2;
|
|
25
|
-
const
|
|
24
|
+
const DERIVED_ROWS = 64;
|
|
25
|
+
const ENTRY_WEIGHT = 256;
|
|
26
|
+
const ABSENT = {
|
|
27
|
+
type: null,
|
|
28
|
+
declared: null,
|
|
29
|
+
queried: false,
|
|
30
|
+
folder: '',
|
|
31
|
+
fields: null,
|
|
32
|
+
values: [],
|
|
33
|
+
links: [],
|
|
34
|
+
};
|
|
26
35
|
function text(value) {
|
|
27
36
|
return typeof value === 'string' ? value : null;
|
|
28
37
|
}
|
|
@@ -38,55 +47,29 @@ function flag(value) {
|
|
|
38
47
|
function paths(list) {
|
|
39
48
|
return JSON.stringify(list);
|
|
40
49
|
}
|
|
41
|
-
function compositionOf(connection, key) {
|
|
42
|
-
let held = compositions.get(connection);
|
|
43
|
-
if (held === undefined) {
|
|
44
|
-
held = new Map();
|
|
45
|
-
compositions.set(connection, held);
|
|
46
|
-
}
|
|
47
|
-
const [row] = connection.rows('SELECT composition FROM replica_keys WHERE key = ?', [key]);
|
|
48
|
-
const textual = text(row?.composition) ?? 'null';
|
|
49
|
-
const known = held.get(textual);
|
|
50
|
-
if (known !== undefined)
|
|
51
|
-
return known;
|
|
52
|
-
const parsed = JSON.parse(textual) ?? {
|
|
53
|
-
model: { status: 'unavailable', reason: 'not-acquired' },
|
|
54
|
-
selection: null,
|
|
55
|
-
};
|
|
56
|
-
held.clear();
|
|
57
|
-
held.set(textual, parsed);
|
|
58
|
-
return parsed;
|
|
59
|
-
}
|
|
60
|
-
export function fingerprint(composition) {
|
|
61
|
-
const shape = (model) => model.status === 'unavailable' ? null : model.types;
|
|
62
|
-
return JSON.stringify({
|
|
63
|
-
model: shape(composition.model),
|
|
64
|
-
selection: composition.selection === null
|
|
65
|
-
? null
|
|
66
|
-
: { roots: composition.selection.roots, model: shape(composition.selection.model) },
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
50
|
function dropDerived(connection, doc) {
|
|
70
51
|
connection.run('DELETE FROM replica_values WHERE doc = ?', [doc]);
|
|
71
52
|
connection.run('DELETE FROM replica_links WHERE doc = ?', [doc]);
|
|
72
53
|
}
|
|
54
|
+
function scalarText(value) {
|
|
55
|
+
if (value === Infinity)
|
|
56
|
+
return 'Infinity';
|
|
57
|
+
if (value === -Infinity)
|
|
58
|
+
return '-Infinity';
|
|
59
|
+
return JSON.stringify(value);
|
|
60
|
+
}
|
|
73
61
|
function writeDerived(connection, doc, key, made) {
|
|
74
|
-
|
|
75
|
-
|
|
62
|
+
if (made.values.length > 0) {
|
|
63
|
+
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)}]`);
|
|
64
|
+
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(',')}]`]);
|
|
65
|
+
}
|
|
66
|
+
if (made.links.length > 0) {
|
|
67
|
+
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(?)', [
|
|
76
68
|
doc,
|
|
77
69
|
key,
|
|
78
|
-
|
|
79
|
-
value.held,
|
|
80
|
-
value.item,
|
|
81
|
-
value.kind,
|
|
82
|
-
value.value,
|
|
83
|
-
value.size,
|
|
84
|
-
flag(value.single),
|
|
70
|
+
JSON.stringify(made.links.map((link) => [link.kind, link.keys, link.places, link.target, link.field])),
|
|
85
71
|
]);
|
|
86
72
|
}
|
|
87
|
-
for (const link of made.links) {
|
|
88
|
-
connection.run('INSERT OR IGNORE INTO replica_links (doc, key, kind, form, target, field) VALUES (?, ?, ?, ?, ?, ?)', [doc, key, link.kind, link.form, link.target, link.field]);
|
|
89
|
-
}
|
|
90
73
|
}
|
|
91
74
|
function entryId(connection, key, layer, path) {
|
|
92
75
|
const [row] = connection.rows('SELECT id FROM replica_entries WHERE key = ? AND layer = ? AND path = ?', [key, layer, path]);
|
|
@@ -99,20 +82,8 @@ function removeEntry(connection, key, layer, path) {
|
|
|
99
82
|
dropDerived(connection, id);
|
|
100
83
|
connection.run('DELETE FROM replica_entries WHERE id = ?', [id]);
|
|
101
84
|
}
|
|
102
|
-
function
|
|
103
|
-
|
|
104
|
-
const made = present
|
|
105
|
-
? derived(entry.path, entry.binary === null ? entry.content : null, composition)
|
|
106
|
-
: {
|
|
107
|
-
type: null,
|
|
108
|
-
declared: null,
|
|
109
|
-
queried: false,
|
|
110
|
-
folder: '',
|
|
111
|
-
fields: null,
|
|
112
|
-
values: [],
|
|
113
|
-
links: [],
|
|
114
|
-
};
|
|
115
|
-
connection.run('INSERT INTO replica_entries (key, layer, path, visible, present, queried, content, binary, revision, file_id, change, written, type, declared, folder, fields) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [
|
|
85
|
+
function insertEntry(connection, key, layer, entry, present, made) {
|
|
86
|
+
connection.run('INSERT INTO replica_entries (key, layer, path, visible, present, queried, content, outline, binary, revision, file_id, change, written, type, declared, folder, fields) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [
|
|
116
87
|
key,
|
|
117
88
|
layer,
|
|
118
89
|
entry.path,
|
|
@@ -120,6 +91,7 @@ function putEntry(connection, key, layer, entry, present, composition) {
|
|
|
120
91
|
flag(present),
|
|
121
92
|
flag(made.queried),
|
|
122
93
|
entry.binary === null ? entry.content : null,
|
|
94
|
+
entry.binary === null && entry.outline !== null ? JSON.stringify(entry.outline) : null,
|
|
123
95
|
entry.binary === null ? null : JSON.stringify(entry.binary),
|
|
124
96
|
entry.revision,
|
|
125
97
|
entry.fileId,
|
|
@@ -130,68 +102,66 @@ function putEntry(connection, key, layer, entry, present, composition) {
|
|
|
130
102
|
made.folder,
|
|
131
103
|
made.fields,
|
|
132
104
|
]);
|
|
105
|
+
}
|
|
106
|
+
function textOf(entry) {
|
|
107
|
+
return entry.binary === null ? entry.content : null;
|
|
108
|
+
}
|
|
109
|
+
function putEntry(connection, key, layer, entry, present, composition) {
|
|
110
|
+
removeEntry(connection, key, layer, entry.path);
|
|
111
|
+
const content = textOf(entry);
|
|
112
|
+
const made = present ? derived(entry.path, content, entry.outline, composition) : ABSENT;
|
|
113
|
+
insertEntry(connection, key, layer, entry, present, made);
|
|
133
114
|
if (!present)
|
|
134
115
|
return;
|
|
135
116
|
const id = entryId(connection, key, layer, entry.path);
|
|
136
117
|
writeDerived(connection, id, key, made);
|
|
137
118
|
}
|
|
119
|
+
function placeEntry(connection, key, entry) {
|
|
120
|
+
removeEntry(connection, key, CONFIRMED, entry.path);
|
|
121
|
+
insertEntry(connection, key, CONFIRMED, entry, true, described(entry.path, textOf(entry), entry.outline));
|
|
122
|
+
}
|
|
138
123
|
function revealed(connection, key, touched) {
|
|
139
124
|
if (touched.length === 0)
|
|
140
125
|
return;
|
|
141
126
|
connection.run(`UPDATE replica_entries SET visible = CASE WHEN layer = ${OVERLAY} THEN present ELSE NOT EXISTS (SELECT 1 FROM replica_entries o WHERE o.key = replica_entries.key AND o.layer = ${OVERLAY} AND o.path = replica_entries.path) END WHERE key = ? AND layer IN (${CONFIRMED}, ${OVERLAY}) AND path IN (SELECT value FROM json_each(?))`, [key, paths(touched)]);
|
|
142
127
|
}
|
|
143
|
-
function newKey(connection, locator, transfer,
|
|
144
|
-
|
|
128
|
+
function newKey(connection, locator, transfer, fingerprint, built) {
|
|
129
|
+
const owed = built ? null : 0;
|
|
130
|
+
connection.run('INSERT INTO replica_keys (locator, transfer, fingerprint, rederiving) VALUES (?, ?, ?, ?)', [locator, transfer, fingerprint, owed]);
|
|
145
131
|
const [row] = connection.rows('SELECT last_insert_rowid() AS key');
|
|
146
132
|
return number(row.key);
|
|
147
133
|
}
|
|
148
|
-
function recomposed(connection, key,
|
|
149
|
-
const [row] = connection.rows('SELECT
|
|
150
|
-
|
|
151
|
-
if (before !== null && fingerprint(before) === fingerprint(composition)) {
|
|
152
|
-
connection.run('UPDATE replica_keys SET composition = ? WHERE key = ?', [
|
|
153
|
-
JSON.stringify(composition),
|
|
154
|
-
key,
|
|
155
|
-
]);
|
|
134
|
+
function recomposed(connection, key, fingerprint) {
|
|
135
|
+
const [row] = connection.rows('SELECT fingerprint FROM replica_keys WHERE key = ?', [key]);
|
|
136
|
+
if (row === undefined || text(row.fingerprint) === fingerprint)
|
|
156
137
|
return;
|
|
157
|
-
|
|
158
|
-
connection.run(
|
|
159
|
-
JSON.stringify(composition),
|
|
160
|
-
key,
|
|
161
|
-
]);
|
|
138
|
+
const held = connection.rows('SELECT 1 FROM replica_entries WHERE key = ? LIMIT 1', [key]);
|
|
139
|
+
connection.run(`UPDATE replica_keys SET fingerprint = ?${held.length === 0 ? '' : ', rederiving = 0'} WHERE key = ?`, [fingerprint, key]);
|
|
162
140
|
}
|
|
163
141
|
export function entered(connection, locator, composition, rows) {
|
|
164
|
-
const key = newKey(connection, locator, '', composition);
|
|
142
|
+
const key = newKey(connection, locator, '', composedOf(composition).fingerprint, true);
|
|
165
143
|
for (const entry of rows)
|
|
166
144
|
putEntry(connection, key, CONFIRMED, entry, true, composition);
|
|
167
145
|
return key;
|
|
168
146
|
}
|
|
169
147
|
export function stage(connection, request) {
|
|
170
|
-
const [held] = connection.rows('SELECT key FROM replica_transfers WHERE locator = ?', [
|
|
171
|
-
request.locator,
|
|
172
|
-
]);
|
|
173
148
|
const key = request.key ??
|
|
174
|
-
(
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
connection.run('INSERT OR REPLACE INTO replica_transfers (locator, key, record) VALUES (?, ?, ?)', [request.locator, key, request.record]);
|
|
180
|
-
}
|
|
181
|
-
const composition = compositionOf(connection, key);
|
|
182
|
-
for (const entry of request.entries) {
|
|
183
|
-
putEntry(connection, key, CONFIRMED, entry, true, composition);
|
|
184
|
-
}
|
|
149
|
+
newKey(connection, request.locator, request.transfer, request.fingerprint, false);
|
|
150
|
+
recomposed(connection, key, request.fingerprint);
|
|
151
|
+
connection.run('UPDATE replica_keys SET rederiving = coalesce(rederiving, last) FROM (SELECT coalesce(max(id), 0) AS last FROM replica_entries) WHERE key = ?', [key]);
|
|
152
|
+
for (const entry of request.entries)
|
|
153
|
+
placeEntry(connection, key, entry);
|
|
185
154
|
for (const path of request.folders) {
|
|
186
155
|
connection.run('INSERT OR REPLACE INTO replica_folders (key, layer, path, present) VALUES (?, ?, ?, 1)', [key, CONFIRMED, path]);
|
|
187
156
|
}
|
|
188
157
|
return key;
|
|
189
158
|
}
|
|
190
159
|
export function refreshing(connection, request) {
|
|
191
|
-
const held = connection.rows('SELECT 1 FROM
|
|
160
|
+
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]);
|
|
192
161
|
if (held.length === 0)
|
|
193
162
|
return null;
|
|
194
|
-
|
|
163
|
+
recomposed(connection, request.key, request.composed.fingerprint);
|
|
164
|
+
const composition = request.composed.composition;
|
|
195
165
|
for (const entry of request.entries) {
|
|
196
166
|
putEntry(connection, request.key, REFRESHED, entry, true, composition);
|
|
197
167
|
}
|
|
@@ -199,6 +169,7 @@ export function refreshing(connection, request) {
|
|
|
199
169
|
putEntry(connection, request.key, REFRESHED, {
|
|
200
170
|
path,
|
|
201
171
|
content: null,
|
|
172
|
+
outline: null,
|
|
202
173
|
binary: null,
|
|
203
174
|
revision: null,
|
|
204
175
|
fileId: null,
|
|
@@ -253,12 +224,11 @@ function applyRefresh(connection, key) {
|
|
|
253
224
|
connection.run('DELETE FROM replica_folders WHERE key = ? AND layer = ?', [key, REFRESHED]);
|
|
254
225
|
return touched;
|
|
255
226
|
}
|
|
227
|
+
function beneathRoots(path) {
|
|
228
|
+
return `EXISTS (SELECT 1 FROM json_each(?) r WHERE ${path} = r.value OR (${path} >= r.value || '/' AND ${path} < r.value || '0'))`;
|
|
229
|
+
}
|
|
256
230
|
function counted(connection, key, roots) {
|
|
257
|
-
const
|
|
258
|
-
const bind = [key];
|
|
259
|
-
for (const root of roots)
|
|
260
|
-
bind.push(root, `${root}/`, `${root}0`);
|
|
261
|
-
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);
|
|
231
|
+
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)]);
|
|
262
232
|
return number(row.held);
|
|
263
233
|
}
|
|
264
234
|
export function counts(connection, rooms) {
|
|
@@ -272,14 +242,12 @@ function erasing(connection, key, locator) {
|
|
|
272
242
|
}
|
|
273
243
|
function commitRoom(connection, room) {
|
|
274
244
|
const touched = new Set();
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
connection.run('DELETE FROM replica_transfers WHERE locator = ?', [room.locator]);
|
|
245
|
+
recomposed(connection, room.key, room.composed.fingerprint);
|
|
246
|
+
const composition = room.composed.composition;
|
|
247
|
+
if (room.published !== undefined &&
|
|
248
|
+
room.published.from !== null &&
|
|
249
|
+
room.published.from !== room.key) {
|
|
250
|
+
erasing(connection, room.published.from, room.locator);
|
|
283
251
|
}
|
|
284
252
|
if (room.refreshed === true) {
|
|
285
253
|
for (const path of applyRefresh(connection, room.key))
|
|
@@ -315,16 +283,16 @@ function commitRoom(connection, room) {
|
|
|
315
283
|
export function commit(connection, rooms) {
|
|
316
284
|
const counted = {};
|
|
317
285
|
const changed = {};
|
|
318
|
-
const
|
|
286
|
+
const indexed = {};
|
|
319
287
|
for (const room of rooms) {
|
|
320
288
|
changed[room.locator] = commitRoom(connection, room);
|
|
321
289
|
counted[room.locator] = counts(connection, [
|
|
322
290
|
{ key: room.key, roots: room.roots ?? rootsOf(connection, room.locator) },
|
|
323
291
|
])[0];
|
|
324
292
|
const [held] = connection.rows('SELECT rederiving FROM replica_keys WHERE key = ?', [room.key]);
|
|
325
|
-
|
|
293
|
+
indexed[room.locator] = held !== undefined && held.rederiving === null;
|
|
326
294
|
}
|
|
327
|
-
return { counts: counted, changed,
|
|
295
|
+
return { counts: counted, changed, indexed };
|
|
328
296
|
}
|
|
329
297
|
function rootsOf(connection, locator) {
|
|
330
298
|
const [row] = connection.rows('SELECT record FROM replica_rooms WHERE locator = ?', [locator]);
|
|
@@ -333,27 +301,25 @@ function rootsOf(connection, locator) {
|
|
|
333
301
|
}
|
|
334
302
|
export function fenceRooms(connection, locators) {
|
|
335
303
|
for (const locator of locators) {
|
|
336
|
-
for (const row of connection.rows('SELECT key FROM replica_keys WHERE locator = ? UNION SELECT key FROM replica_rooms WHERE locator = ?
|
|
304
|
+
for (const row of connection.rows('SELECT key FROM replica_keys WHERE locator = ? UNION SELECT key FROM replica_rooms WHERE locator = ?', [locator, locator])) {
|
|
337
305
|
erasing(connection, number(row.key), locator);
|
|
338
306
|
}
|
|
339
307
|
connection.run('DELETE FROM replica_rooms WHERE locator = ?', [locator]);
|
|
340
|
-
connection.run('DELETE FROM
|
|
308
|
+
connection.run('DELETE FROM replica_opened WHERE locator = ?', [locator]);
|
|
341
309
|
}
|
|
342
310
|
return null;
|
|
343
311
|
}
|
|
344
|
-
export function abandon(connection, locator
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
return number(held.key);
|
|
312
|
+
export function abandon(connection, locator) {
|
|
313
|
+
for (const row of connection.rows(`SELECT k.key FROM replica_keys k WHERE k.locator = ?
|
|
314
|
+
AND NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
315
|
+
AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)`, [locator])) {
|
|
316
|
+
erasing(connection, number(row.key), locator);
|
|
317
|
+
}
|
|
318
|
+
return null;
|
|
352
319
|
}
|
|
353
320
|
export function release(connection, key) {
|
|
354
321
|
const [held] = connection.rows(`SELECT k.locator FROM replica_keys k WHERE k.key = ?
|
|
355
|
-
AND NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
356
|
-
AND NOT EXISTS (SELECT 1 FROM replica_transfers t WHERE t.key = k.key)`, [key]);
|
|
322
|
+
AND NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)`, [key]);
|
|
357
323
|
if (held !== undefined)
|
|
358
324
|
erasing(connection, key, text(held.locator));
|
|
359
325
|
return null;
|
|
@@ -377,33 +343,92 @@ export function erase(connection, locator, limit) {
|
|
|
377
343
|
const [more] = connection.rows(`SELECT 1 FROM replica_erasing${locator === null ? '' : ' WHERE locator = ?'} LIMIT 1`, locator === null ? [] : [locator]);
|
|
378
344
|
return { remaining: more !== undefined };
|
|
379
345
|
}
|
|
380
|
-
|
|
346
|
+
function cursor(value) {
|
|
347
|
+
return value === null || value === undefined ? null : number(value);
|
|
348
|
+
}
|
|
349
|
+
export function rederive(connection, key, composed, bytes, ms, floor) {
|
|
350
|
+
const started = performance.now();
|
|
351
|
+
recomposed(connection, key, composed.fingerprint);
|
|
381
352
|
const [held] = connection.rows('SELECT rederiving FROM replica_keys WHERE key = ?', [key]);
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
const composition =
|
|
386
|
-
|
|
353
|
+
const deriving = cursor(held?.rederiving);
|
|
354
|
+
if (deriving === null)
|
|
355
|
+
return { done: true, text: 0 };
|
|
356
|
+
const composition = composed.composition;
|
|
357
|
+
let last = deriving;
|
|
358
|
+
let spent = 0;
|
|
359
|
+
let built = 0;
|
|
360
|
+
let read = false;
|
|
361
|
+
reading: for (;;) {
|
|
362
|
+
const rows = connection.rows('SELECT id, path, content, outline FROM replica_entries WHERE key = ? AND present = 1 AND id > ? ORDER BY id LIMIT ?', [key, last, DERIVED_ROWS]);
|
|
363
|
+
for (const row of rows) {
|
|
364
|
+
const id = number(row.id);
|
|
365
|
+
const path = text(row.path);
|
|
366
|
+
const content = text(row.content);
|
|
367
|
+
const made = derived(path, content, json(row.outline), composition);
|
|
368
|
+
dropDerived(connection, id);
|
|
369
|
+
writeDerived(connection, id, key, made);
|
|
370
|
+
built += content?.length ?? 0;
|
|
371
|
+
last = id;
|
|
372
|
+
spent += ENTRY_WEIGHT + (content?.length ?? 0);
|
|
373
|
+
if (spent >= bytes || (spent >= floor && performance.now() - started >= ms))
|
|
374
|
+
break reading;
|
|
375
|
+
}
|
|
376
|
+
if (rows.length < DERIVED_ROWS) {
|
|
377
|
+
read = true;
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
connection.run('UPDATE replica_keys SET rederiving = ? WHERE key = ?', [read ? null : last, key]);
|
|
382
|
+
return { done: read, text: built };
|
|
383
|
+
}
|
|
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]);
|
|
387
410
|
for (const row of rows) {
|
|
388
|
-
|
|
389
|
-
const path = text(row.path);
|
|
390
|
-
const made = derived(path, text(row.content), composition);
|
|
391
|
-
dropDerived(connection, id);
|
|
392
|
-
connection.run('UPDATE replica_entries SET type = ?, declared = ?, queried = ?, fields = ? WHERE id = ?', [made.type, made.declared, flag(made.queried), made.fields, id]);
|
|
393
|
-
writeDerived(connection, id, key, made);
|
|
411
|
+
outlinedAgain(connection, row, request.key, null, request.composed.composition);
|
|
394
412
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
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) }));
|
|
402
426
|
}
|
|
403
427
|
function heldEntry(row) {
|
|
404
428
|
return {
|
|
405
429
|
path: text(row.path),
|
|
406
430
|
content: text(row.content),
|
|
431
|
+
outline: json(row.outline),
|
|
407
432
|
binary: json(row.binary),
|
|
408
433
|
revision: text(row.revision),
|
|
409
434
|
fileId: text(row.file_id),
|
|
@@ -412,13 +437,32 @@ function heldEntry(row) {
|
|
|
412
437
|
type: text(row.type),
|
|
413
438
|
};
|
|
414
439
|
}
|
|
415
|
-
const ENTRY_COLUMNS = 'e.path, e.content, e.binary, e.revision, e.file_id, e.change, e.written, e.type';
|
|
440
|
+
const ENTRY_COLUMNS = 'e.path, e.content, e.outline, e.binary, e.revision, e.file_id, e.change, e.written, e.type';
|
|
441
|
+
function unrefreshed(alias) {
|
|
442
|
+
return `NOT EXISTS (SELECT 1 FROM replica_entries r WHERE r.key = ${alias}.key AND r.layer = ${REFRESHED} AND r.path = ${alias}.path)`;
|
|
443
|
+
}
|
|
416
444
|
function viewed(view, alias) {
|
|
417
445
|
if (view === 'visible')
|
|
418
446
|
return `${alias}.visible = 1 AND ${alias}.present = 1`;
|
|
419
447
|
if (view === 'confirmed')
|
|
420
448
|
return `${alias}.layer = ${CONFIRMED}`;
|
|
421
|
-
return `${alias}.present = 1 AND (${alias}.layer = ${REFRESHED} OR (${alias}.layer = ${CONFIRMED} AND
|
|
449
|
+
return `${alias}.present = 1 AND (${alias}.layer = ${REFRESHED} OR (${alias}.layer = ${CONFIRMED} AND ${unrefreshed(alias)}))`;
|
|
450
|
+
}
|
|
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
|
+
]);
|
|
422
466
|
}
|
|
423
467
|
export function entries(connection, key, view, list) {
|
|
424
468
|
if (list.length === 0)
|
|
@@ -449,64 +493,14 @@ export function referrers(connection, key, view, targets, kinds) {
|
|
|
449
493
|
kind: text(row.kind),
|
|
450
494
|
}));
|
|
451
495
|
}
|
|
452
|
-
export function walk(connection, key, view, after, bytes) {
|
|
453
|
-
const found = [];
|
|
454
|
-
let spent = 0;
|
|
455
|
-
let from = after;
|
|
456
|
-
for (;;) {
|
|
457
|
-
const rows = connection.rows(`SELECT ${ENTRY_COLUMNS} FROM replica_entries e WHERE e.key = ? AND ${viewed(view, 'e')}${from === null ? '' : ' AND e.path > ?'} ORDER BY e.path LIMIT 64`, from === null ? [key] : [key, from]);
|
|
458
|
-
for (const row of rows) {
|
|
459
|
-
const entry = heldEntry(row);
|
|
460
|
-
found.push(entry);
|
|
461
|
-
spent += 256 + (entry.content?.length ?? 0) * 2;
|
|
462
|
-
if (spent >= bytes)
|
|
463
|
-
return { entries: found, next: entry.path };
|
|
464
|
-
}
|
|
465
|
-
if (rows.length < 64)
|
|
466
|
-
return { entries: found, next: null };
|
|
467
|
-
from = found.at(-1).path;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
496
|
export function files(connection, key, view, after, limit) {
|
|
471
|
-
return connection
|
|
472
|
-
|
|
473
|
-
|
|
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) }));
|
|
498
|
+
}
|
|
499
|
+
export function folders(connection, key, view, list) {
|
|
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(view, 'e')} AND e.path >= ? AND e.path < ? LIMIT 1`, [key, path, key, `${path}/`, `${path}0`]).length > 0);
|
|
474
501
|
}
|
|
475
502
|
export function typedWithin(connection, key, view, roots, within) {
|
|
476
|
-
|
|
477
|
-
const bind = [key];
|
|
478
|
-
for (const root of roots)
|
|
479
|
-
bind.push(root, `${root}/`, `${root}0`);
|
|
480
|
-
const placed = roots.length === 0 ? (within ? ' AND 0' : '') : ` AND ${within ? '' : 'NOT '}(${inside})`;
|
|
481
|
-
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);
|
|
482
|
-
}
|
|
483
|
-
export function searched(connection, key, locator, after, limit, only) {
|
|
484
|
-
const bind = [key];
|
|
485
|
-
if (only !== null)
|
|
486
|
-
bind.push(paths(only));
|
|
487
|
-
if (after !== null)
|
|
488
|
-
bind.push(after);
|
|
489
|
-
bind.push(limit);
|
|
490
|
-
const rows = connection.rows(`SELECT e.path, e.type, e.fields, e.content FROM replica_entries e WHERE e.key = ? AND e.visible = 1 AND e.present = 1 AND e.queried = 1 AND e.type IS NOT NULL${only === null ? '' : ' AND e.path IN (SELECT value FROM json_each(?))'}${after === null ? '' : ' AND e.path > ?'} ORDER BY e.path LIMIT ?`, bind);
|
|
491
|
-
const documents = [];
|
|
492
|
-
for (const row of rows) {
|
|
493
|
-
const path = text(row.path);
|
|
494
|
-
const read = parse(text(row.content) ?? '', path);
|
|
495
|
-
if (!read.ok || !markdownPath(path))
|
|
496
|
-
continue;
|
|
497
|
-
documents.push({
|
|
498
|
-
room: locator,
|
|
499
|
-
path,
|
|
500
|
-
type: text(row.type),
|
|
501
|
-
fields: json(row.fields) ?? {},
|
|
502
|
-
preamble: read.document.preamble,
|
|
503
|
-
sections: read.document.sections,
|
|
504
|
-
});
|
|
505
|
-
}
|
|
506
|
-
return {
|
|
507
|
-
documents,
|
|
508
|
-
next: rows.length < limit ? null : text(rows.at(-1).path),
|
|
509
|
-
};
|
|
503
|
+
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);
|
|
510
504
|
}
|
|
511
505
|
export function opened(connection) {
|
|
512
506
|
const facts = {};
|
|
@@ -519,18 +513,13 @@ export function opened(connection) {
|
|
|
519
513
|
key: number(row.key),
|
|
520
514
|
record: text(row.record),
|
|
521
515
|
})),
|
|
522
|
-
transfers: connection.rows('SELECT locator, key, record FROM replica_transfers').map((row) => ({
|
|
523
|
-
locator: text(row.locator),
|
|
524
|
-
key: number(row.key),
|
|
525
|
-
record: text(row.record),
|
|
526
|
-
})),
|
|
527
516
|
keys: connection
|
|
528
517
|
.rows('SELECT key, locator, transfer, rederiving FROM replica_keys')
|
|
529
518
|
.map((row) => ({
|
|
530
519
|
key: number(row.key),
|
|
531
520
|
locator: text(row.locator),
|
|
532
521
|
transfer: text(row.transfer),
|
|
533
|
-
rederiving:
|
|
522
|
+
rederiving: cursor(row.rederiving),
|
|
534
523
|
})),
|
|
535
524
|
facts,
|
|
536
525
|
presentation: connection.rows('SELECT app, value FROM replica_presentation').map((row) => ({
|
|
@@ -543,7 +532,6 @@ export function opened(connection) {
|
|
|
543
532
|
orphans: connection
|
|
544
533
|
.rows(`SELECT k.key FROM replica_keys k
|
|
545
534
|
WHERE NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
|
|
546
|
-
AND NOT EXISTS (SELECT 1 FROM replica_transfers t WHERE t.key = k.key)
|
|
547
535
|
AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)`)
|
|
548
536
|
.map((row) => number(row.key)),
|
|
549
537
|
erasing: connection.rows('SELECT key, locator FROM replica_erasing').map((row) => ({
|