@agentshouse/mdruntime 0.1.0-c588e78cf6975 → 0.1.0-c59a5a6bd2f31

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.
Files changed (201) hide show
  1. package/README.md +2 -2
  2. package/dist/acceptance.d.ts +26 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +384 -0
  5. package/dist/adapter.d.ts +38 -25
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/attached-model.d.ts +3 -10
  8. package/dist/attached-model.d.ts.map +1 -1
  9. package/dist/attached-model.js +21 -56
  10. package/dist/bootstrap.d.ts +8 -4
  11. package/dist/bootstrap.d.ts.map +1 -1
  12. package/dist/bootstrap.js +209 -78
  13. package/dist/chain.d.ts +2 -1
  14. package/dist/chain.d.ts.map +1 -1
  15. package/dist/change.d.ts +1 -0
  16. package/dist/change.d.ts.map +1 -1
  17. package/dist/chat-types.d.ts +3 -0
  18. package/dist/chat-types.d.ts.map +1 -0
  19. package/dist/chat-types.js +2 -0
  20. package/dist/client.d.ts +14 -4
  21. package/dist/client.d.ts.map +1 -1
  22. package/dist/client.js +3 -0
  23. package/dist/collection.d.ts +2 -1
  24. package/dist/collection.d.ts.map +1 -1
  25. package/dist/compile.d.ts +7 -7
  26. package/dist/compile.d.ts.map +1 -1
  27. package/dist/compile.js +229 -297
  28. package/dist/connection.d.ts +19 -43
  29. package/dist/connection.d.ts.map +1 -1
  30. package/dist/connection.js +1325 -3586
  31. package/dist/context.d.ts +2 -0
  32. package/dist/context.d.ts.map +1 -0
  33. package/dist/context.js +7 -0
  34. package/dist/conversation-row.d.ts +33 -0
  35. package/dist/conversation-row.d.ts.map +1 -0
  36. package/dist/conversation-row.js +47 -0
  37. package/dist/conversation.d.ts +18 -9
  38. package/dist/conversation.d.ts.map +1 -1
  39. package/dist/conversation.js +375 -224
  40. package/dist/durability.d.ts +6 -0
  41. package/dist/durability.d.ts.map +1 -1
  42. package/dist/entry.d.ts +2 -2
  43. package/dist/entry.d.ts.map +1 -1
  44. package/dist/entry.js +17 -8
  45. package/dist/figures.d.ts +48 -0
  46. package/dist/figures.d.ts.map +1 -0
  47. package/dist/figures.js +197 -0
  48. package/dist/host.d.ts +4 -5
  49. package/dist/host.d.ts.map +1 -1
  50. package/dist/host.js +22 -21
  51. package/dist/identity.d.ts +0 -1
  52. package/dist/identity.d.ts.map +1 -1
  53. package/dist/identity.js +0 -3
  54. package/dist/index.d.ts +18 -8
  55. package/dist/index.d.ts.map +1 -1
  56. package/dist/index.js +5 -1
  57. package/dist/install.d.ts +86 -0
  58. package/dist/install.d.ts.map +1 -0
  59. package/dist/install.js +537 -0
  60. package/dist/job.d.ts +33 -0
  61. package/dist/job.d.ts.map +1 -0
  62. package/dist/job.js +54 -0
  63. package/dist/live.d.ts +5 -4
  64. package/dist/live.d.ts.map +1 -1
  65. package/dist/live.js +62 -20
  66. package/dist/log.d.ts +1 -0
  67. package/dist/log.d.ts.map +1 -1
  68. package/dist/memory.d.ts +4 -1
  69. package/dist/memory.d.ts.map +1 -1
  70. package/dist/memory.js +43 -4
  71. package/dist/messages.d.ts +27 -3
  72. package/dist/messages.d.ts.map +1 -1
  73. package/dist/messages.js +97 -19
  74. package/dist/model.d.ts +2 -9
  75. package/dist/model.d.ts.map +1 -1
  76. package/dist/model.js +0 -9
  77. package/dist/observation.d.ts +2 -2
  78. package/dist/observation.d.ts.map +1 -1
  79. package/dist/observation.js +7 -2
  80. package/dist/outcome.d.ts +1 -0
  81. package/dist/outcome.d.ts.map +1 -1
  82. package/dist/outcome.js +3 -0
  83. package/dist/overlay.d.ts +1 -0
  84. package/dist/overlay.d.ts.map +1 -1
  85. package/dist/overlay.js +11 -0
  86. package/dist/parts.d.ts +59 -0
  87. package/dist/parts.d.ts.map +1 -0
  88. package/dist/parts.js +118 -0
  89. package/dist/query.d.ts +13 -210
  90. package/dist/query.d.ts.map +1 -1
  91. package/dist/query.js +3 -508
  92. package/dist/questions.d.ts +32 -0
  93. package/dist/questions.d.ts.map +1 -0
  94. package/dist/questions.js +118 -0
  95. package/dist/reach.d.ts +11 -11
  96. package/dist/reach.d.ts.map +1 -1
  97. package/dist/reach.js +166 -113
  98. package/dist/react.d.ts +4 -2
  99. package/dist/react.d.ts.map +1 -1
  100. package/dist/read.d.ts +7 -0
  101. package/dist/read.d.ts.map +1 -0
  102. package/dist/read.js +17 -0
  103. package/dist/readiness.d.ts +0 -2
  104. package/dist/readiness.d.ts.map +1 -1
  105. package/dist/reads.d.ts +35 -0
  106. package/dist/reads.d.ts.map +1 -0
  107. package/dist/reads.js +128 -0
  108. package/dist/recovery.d.ts.map +1 -1
  109. package/dist/recovery.js +9 -1
  110. package/dist/refresh.d.ts +1 -1
  111. package/dist/refresh.d.ts.map +1 -1
  112. package/dist/refresh.js +8 -20
  113. package/dist/replica.d.ts +8 -8
  114. package/dist/replica.d.ts.map +1 -1
  115. package/dist/replica.js +50 -18
  116. package/dist/room-draft.d.ts +3 -2
  117. package/dist/room-draft.d.ts.map +1 -1
  118. package/dist/room-draft.js +4 -4
  119. package/dist/room-drafts.d.ts +29 -0
  120. package/dist/room-drafts.d.ts.map +1 -0
  121. package/dist/room-drafts.js +216 -0
  122. package/dist/rows.d.ts +14 -22
  123. package/dist/rows.d.ts.map +1 -1
  124. package/dist/rows.js +48 -189
  125. package/dist/save.d.ts.map +1 -1
  126. package/dist/save.js +11 -8
  127. package/dist/saves.d.ts +96 -0
  128. package/dist/saves.d.ts.map +1 -0
  129. package/dist/saves.js +999 -0
  130. package/dist/search.d.ts +12 -63
  131. package/dist/search.d.ts.map +1 -1
  132. package/dist/search.js +9 -52
  133. package/dist/seed.d.ts +16 -2
  134. package/dist/seed.d.ts.map +1 -1
  135. package/dist/seed.js +66 -0
  136. package/dist/socket/admission.d.ts +7 -0
  137. package/dist/socket/admission.d.ts.map +1 -0
  138. package/dist/socket/admission.js +39 -0
  139. package/dist/socket/answers.d.ts +6 -0
  140. package/dist/socket/answers.d.ts.map +1 -0
  141. package/dist/socket/answers.js +20 -0
  142. package/dist/socket/reads.d.ts +10 -0
  143. package/dist/socket/reads.d.ts.map +1 -0
  144. package/dist/socket/reads.js +31 -0
  145. package/dist/socket.d.ts +43 -0
  146. package/dist/socket.d.ts.map +1 -0
  147. package/dist/socket.js +96 -0
  148. package/dist/source.d.ts +61 -0
  149. package/dist/source.d.ts.map +1 -0
  150. package/dist/source.js +81 -0
  151. package/dist/status.d.ts +0 -2
  152. package/dist/status.d.ts.map +1 -1
  153. package/dist/status.js +0 -6
  154. package/dist/store/fences.d.ts +16 -0
  155. package/dist/store/fences.d.ts.map +1 -0
  156. package/dist/store/fences.js +26 -0
  157. package/dist/store/installs.d.ts +30 -0
  158. package/dist/store/installs.d.ts.map +1 -0
  159. package/dist/store/installs.js +34 -0
  160. package/dist/store/records.d.ts +212 -0
  161. package/dist/store/records.d.ts.map +1 -0
  162. package/dist/store/records.js +408 -0
  163. package/dist/store/room-held.d.ts +8 -0
  164. package/dist/store/room-held.d.ts.map +1 -0
  165. package/dist/store/room-held.js +17 -0
  166. package/dist/store/watchers.d.ts +15 -0
  167. package/dist/store/watchers.d.ts.map +1 -0
  168. package/dist/store/watchers.js +36 -0
  169. package/dist/store/writes.d.ts +24 -0
  170. package/dist/store/writes.d.ts.map +1 -0
  171. package/dist/store/writes.js +125 -0
  172. package/dist/store.d.ts +78 -156
  173. package/dist/store.d.ts.map +1 -1
  174. package/dist/store.js +117 -401
  175. package/dist/stored.d.ts +58 -65
  176. package/dist/stored.d.ts.map +1 -1
  177. package/dist/stored.js +264 -243
  178. package/dist/tables.d.ts +107 -51
  179. package/dist/tables.d.ts.map +1 -1
  180. package/dist/tables.js +193 -59
  181. package/dist/tallies.d.ts +18 -0
  182. package/dist/tallies.d.ts.map +1 -0
  183. package/dist/tallies.js +129 -0
  184. package/dist/validation.d.ts.map +1 -1
  185. package/dist/validation.js +7 -4
  186. package/dist/windows.d.ts +1 -12
  187. package/dist/windows.d.ts.map +1 -1
  188. package/dist/windows.js +2 -75
  189. package/dist/wire.d.ts +14 -0
  190. package/dist/wire.d.ts.map +1 -1
  191. package/dist/wire.js +5 -0
  192. package/package.json +2 -2
  193. package/dist/fence.d.ts +0 -6
  194. package/dist/fence.d.ts.map +0 -1
  195. package/dist/fence.js +0 -10
  196. package/dist/fulltext.d.ts +0 -27
  197. package/dist/fulltext.d.ts.map +0 -1
  198. package/dist/fulltext.js +0 -370
  199. package/dist/words.d.ts +0 -19
  200. package/dist/words.d.ts.map +0 -1
  201. package/dist/words.js +0 -156
package/dist/stored.js CHANGED
@@ -1,21 +1,21 @@
1
+ import { instantKey, sql } from '@agentshouse/mdmodel';
1
2
  import { composedOf } from './collection.js';
2
- import { indexDocument, unindexAll, unindexDocuments } from './fulltext.js';
3
- import { described, derived } from './rows.js';
3
+ import { described, derived, heldOf } from './rows.js';
4
4
  export const REPLICA_TABLES = [
5
- 'CREATE TABLE replica_keys (key INTEGER PRIMARY KEY AUTOINCREMENT, locator TEXT NOT NULL, transfer TEXT NOT NULL, fingerprint TEXT NOT NULL, rederiving INTEGER, searching INTEGER)',
6
- 'CREATE TABLE replica_rooms (locator TEXT PRIMARY KEY, key INTEGER NOT NULL, record TEXT NOT NULL) WITHOUT ROWID',
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, outline TEXT)',
5
+ 'CREATE TABLE replica_keys (key INTEGER PRIMARY KEY AUTOINCREMENT, locator TEXT NOT NULL, transfer TEXT NOT NULL, fingerprint TEXT NOT NULL, rederiving INTEGER)',
6
+ 'CREATE TABLE replica_rooms (locator TEXT PRIMARY KEY, key INTEGER NOT NULL, record TEXT NOT NULL, documents INTEGER NOT NULL DEFAULT 0) WITHOUT ROWID',
7
+ 'CREATE TABLE replica_models (identity TEXT NOT NULL, version TEXT NOT NULL, schema TEXT NOT NULL, PRIMARY KEY (identity, version)) WITHOUT ROWID',
8
+ "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, 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)",
8
9
  'CREATE UNIQUE INDEX replica_entries_path ON replica_entries (key, layer, path)',
9
10
  'CREATE INDEX replica_entries_visible ON replica_entries (key, visible, present, path, binary)',
10
11
  'CREATE INDEX replica_entries_folder ON replica_entries (key, folder, path)',
11
12
  'CREATE INDEX replica_entries_type ON replica_entries (key, type, visible, present, queried, folder)',
12
13
  'CREATE INDEX replica_entries_key ON replica_entries (key)',
13
- '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',
14
+ sql.VALUE_TABLE,
14
15
  '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',
15
16
  'CREATE INDEX replica_links_target ON replica_links (key, target, kind, keys)',
16
- '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',
17
+ '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',
17
18
  'CREATE TABLE replica_erasing (key INTEGER PRIMARY KEY, locator TEXT NOT NULL)',
18
- 'CREATE TABLE replica_opened (locator TEXT NOT NULL, path TEXT NOT NULL, PRIMARY KEY (locator, path)) WITHOUT ROWID',
19
19
  'CREATE TABLE replica_facts (name TEXT PRIMARY KEY, value TEXT NOT NULL) WITHOUT ROWID',
20
20
  'CREATE TABLE replica_presentation (app TEXT PRIMARY KEY, value TEXT NOT NULL) WITHOUT ROWID',
21
21
  ];
@@ -30,8 +30,7 @@ const ABSENT = {
30
30
  queried: false,
31
31
  folder: '',
32
32
  fields: null,
33
- values: [],
34
- links: [],
33
+ links: null,
35
34
  };
36
35
  function text(value) {
37
36
  return typeof value === 'string' ? value : null;
@@ -52,17 +51,11 @@ function dropDerived(connection, doc) {
52
51
  connection.run('DELETE FROM replica_values WHERE doc = ?', [doc]);
53
52
  connection.run('DELETE FROM replica_links WHERE doc = ?', [doc]);
54
53
  }
55
- function scalarText(value) {
56
- if (value === Infinity)
57
- return 'Infinity';
58
- if (value === -Infinity)
59
- return '-Infinity';
60
- return JSON.stringify(value);
61
- }
62
- function writeDerived(connection, doc, key, made) {
54
+ function writeDerived(connection, doc, key, path, held, composition) {
55
+ const made = derived(path, held, composition);
63
56
  if (made.values.length > 0) {
64
- 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)}]`);
65
- 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(',')}]`]);
57
+ const inserted = sql.insertedValues(doc, made.values);
58
+ connection.run(inserted.sql, inserted.bind);
66
59
  }
67
60
  if (made.links.length > 0) {
68
61
  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,27 +69,15 @@ function entryId(connection, key, layer, path) {
76
69
  const [row] = connection.rows('SELECT id FROM replica_entries WHERE key = ? AND layer = ? AND path = ?', [key, layer, path]);
77
70
  return row === undefined ? null : number(row.id);
78
71
  }
79
- function words() {
80
- return { dropped: [], made: [] };
81
- }
82
- function wordsWritten(connection, owed) {
83
- unindexDocuments(connection, owed.dropped);
84
- for (const { id, path, content } of owed.made)
85
- indexDocument(connection, id, path, content);
86
- }
87
- function removeEntry(connection, key, layer, path, owed) {
72
+ function removeEntry(connection, key, layer, path) {
88
73
  const id = entryId(connection, key, layer, path);
89
74
  if (id === null)
90
75
  return;
91
76
  dropDerived(connection, id);
92
- owed.dropped.push(id);
93
77
  connection.run('DELETE FROM replica_entries WHERE id = ?', [id]);
94
78
  }
95
- function heldIds(connection, sql, bind) {
96
- return connection.rows(sql, bind).map((row) => number(row.id));
97
- }
98
- function insertEntry(connection, key, layer, entry, present, made) {
99
- 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [
79
+ function insertEntry(connection, key, layer, entry, present, made, part = null) {
80
+ connection.run('INSERT INTO replica_entries (key, layer, path, visible, present, queried, content, binary, revision, file_id, created, created_instant, change, changed_instant, written, type, declared, folder, fields, links, part) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [
100
81
  key,
101
82
  layer,
102
83
  entry.path,
@@ -104,36 +85,37 @@ function insertEntry(connection, key, layer, entry, present, made) {
104
85
  flag(present),
105
86
  flag(made.queried),
106
87
  entry.binary === null ? entry.content : null,
107
- entry.binary === null && entry.outline !== null ? JSON.stringify(entry.outline) : null,
108
88
  entry.binary === null ? null : JSON.stringify(entry.binary),
109
89
  entry.revision,
110
90
  entry.fileId,
91
+ entry.created,
92
+ instantKey(entry.created),
111
93
  entry.change === null ? null : JSON.stringify(entry.change),
94
+ instantKey(entry.change?.time),
112
95
  entry.written,
113
96
  made.type,
114
97
  made.declared,
115
98
  made.folder,
116
99
  made.fields,
100
+ made.links,
101
+ part,
117
102
  ]);
118
103
  }
119
104
  function textOf(entry) {
120
105
  return entry.binary === null ? entry.content : null;
121
106
  }
122
- function putEntry(connection, key, layer, entry, present, composition, owed, searched = true) {
123
- removeEntry(connection, key, layer, entry.path, owed);
107
+ function putEntry(connection, key, layer, entry, present, composition) {
108
+ removeEntry(connection, key, layer, entry.path);
124
109
  const content = textOf(entry);
125
- const made = present ? derived(entry.path, content, entry.outline, composition) : ABSENT;
126
- insertEntry(connection, key, layer, entry, present, made);
110
+ insertEntry(connection, key, layer, entry, present, present ? described(entry.path, content, entry.facts) : ABSENT);
127
111
  if (!present)
128
112
  return;
129
113
  const id = entryId(connection, key, layer, entry.path);
130
- writeDerived(connection, id, key, made);
131
- if (searched && content !== null && made.queried)
132
- owed.made.push({ id, path: entry.path, content });
114
+ writeDerived(connection, id, key, entry.path, heldOf(entry.path, content, entry.facts), composition);
133
115
  }
134
- function placeEntry(connection, key, entry, owed) {
135
- removeEntry(connection, key, CONFIRMED, entry.path, owed);
136
- insertEntry(connection, key, CONFIRMED, entry, true, described(entry.path, textOf(entry), entry.outline));
116
+ function placeEntry(connection, key, entry, part) {
117
+ removeEntry(connection, key, CONFIRMED, entry.path);
118
+ insertEntry(connection, key, CONFIRMED, entry, true, described(entry.path, textOf(entry), entry.facts), part);
137
119
  }
138
120
  function revealed(connection, key, touched) {
139
121
  if (touched.length === 0)
@@ -142,7 +124,7 @@ function revealed(connection, key, touched) {
142
124
  }
143
125
  function newKey(connection, locator, transfer, fingerprint, built) {
144
126
  const owed = built ? null : 0;
145
- connection.run('INSERT INTO replica_keys (locator, transfer, fingerprint, rederiving, searching) VALUES (?, ?, ?, ?, ?)', [locator, transfer, fingerprint, owed, owed]);
127
+ connection.run('INSERT INTO replica_keys (locator, transfer, fingerprint, rederiving) VALUES (?, ?, ?, ?)', [locator, transfer, fingerprint, owed]);
146
128
  const [row] = connection.rows('SELECT last_insert_rowid() AS key');
147
129
  return number(row.key);
148
130
  }
@@ -155,48 +137,109 @@ function recomposed(connection, key, fingerprint) {
155
137
  }
156
138
  export function entered(connection, locator, composition, rows) {
157
139
  const key = newKey(connection, locator, '', composedOf(composition).fingerprint, true);
158
- const owed = words();
159
- for (const entry of rows) {
160
- putEntry(connection, key, CONFIRMED, entry, true, composition, owed, false);
161
- }
162
- wordsWritten(connection, owed);
140
+ for (const entry of rows)
141
+ putEntry(connection, key, CONFIRMED, entry, true, composition);
163
142
  return key;
164
143
  }
165
144
  export function stage(connection, request) {
145
+ if (request.key === null) {
146
+ for (const row of connection.rows(STAGED_KEYS, [request.locator])) {
147
+ erasing(connection, number(row.key), request.locator);
148
+ }
149
+ }
166
150
  const key = request.key ??
167
151
  newKey(connection, request.locator, request.transfer, request.fingerprint, false);
168
152
  recomposed(connection, key, request.fingerprint);
169
- connection.run('UPDATE replica_keys SET rederiving = coalesce(rederiving, last), searching = coalesce(searching, last) FROM (SELECT coalesce(max(id), 0) AS last FROM replica_entries) WHERE key = ?', [key]);
170
- const owed = words();
153
+ owing(connection, key);
171
154
  for (const entry of request.entries)
172
- placeEntry(connection, key, entry, owed);
155
+ placeEntry(connection, key, entry, request.part);
173
156
  for (const path of request.folders) {
174
- connection.run('INSERT OR REPLACE INTO replica_folders (key, layer, path, present) VALUES (?, ?, ?, 1)', [key, CONFIRMED, path]);
157
+ connection.run('INSERT OR REPLACE INTO replica_folders (key, layer, path, present, part) VALUES (?, ?, ?, 1, ?)', [key, CONFIRMED, path, request.part]);
175
158
  }
176
- wordsWritten(connection, owed);
177
159
  return key;
178
160
  }
161
+ function owing(connection, key) {
162
+ connection.run('UPDATE replica_keys SET rederiving = coalesce(rederiving, last) FROM (SELECT coalesce(max(id), 0) AS last FROM replica_entries) WHERE key = ?', [key]);
163
+ }
164
+ const STAGED_KEYS = `SELECT k.key, k.transfer FROM replica_keys k WHERE k.locator = ?
165
+ AND NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
166
+ AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)
167
+ ORDER BY k.key DESC`;
168
+ const UNPARTED = `EXISTS (SELECT 1 FROM replica_entries e WHERE e.key = k.key AND e.part IS NULL)
169
+ OR EXISTS (SELECT 1 FROM replica_folders f WHERE f.key = k.key AND f.part IS NULL)`;
170
+ function partsOf(connection, key, layer) {
171
+ const scoped = layer === null ? '' : ` AND layer = ${layer}`;
172
+ return new Set(connection
173
+ .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])
174
+ .map((row) => text(row.part)));
175
+ }
176
+ function resumable(connection, locator) {
177
+ let kept = null;
178
+ for (const row of connection.rows(STAGED_KEYS, [locator])) {
179
+ const key = number(row.key);
180
+ const parted = kept === null &&
181
+ connection.rows(`SELECT 1 FROM replica_keys k WHERE k.key = ? AND NOT (${UNPARTED})`, [key])
182
+ .length > 0;
183
+ if (parted)
184
+ kept = { key, transfer: text(row.transfer) };
185
+ else
186
+ erasing(connection, key, locator);
187
+ }
188
+ return kept;
189
+ }
190
+ export function resume(connection, request) {
191
+ const wanted = new Set(request.parts);
192
+ const held = resumable(connection, request.locator);
193
+ const staged = held === null ? new Set() : partsOf(connection, held.key, null);
194
+ const dropped = [...staged].filter((part) => !wanted.has(part));
195
+ if (held !== null && dropped.length > 0) {
196
+ const named = JSON.stringify(dropped);
197
+ const rows = `SELECT id FROM replica_entries WHERE key = ? AND part IN (SELECT value FROM json_each(?))`;
198
+ for (const table of ['replica_values', 'replica_links']) {
199
+ connection.run(`DELETE FROM ${table} WHERE doc IN (${rows})`, [held.key, named]);
200
+ }
201
+ connection.run(`DELETE FROM replica_entries WHERE id IN (${rows})`, [held.key, named]);
202
+ connection.run('DELETE FROM replica_folders WHERE key = ? AND part IN (SELECT value FROM json_each(?))', [held.key, named]);
203
+ }
204
+ const kept = [...staged].filter((part) => wanted.has(part));
205
+ const publishedParts = request.published === null
206
+ ? new Set()
207
+ : partsOf(connection, request.published, CONFIRMED);
208
+ const copied = request.parts.filter((part) => !staged.has(part) && publishedParts.has(part));
209
+ if (held === null && copied.length === 0) {
210
+ return { key: null, transfer: request.transfer, staged: [] };
211
+ }
212
+ const key = held?.key ?? newKey(connection, request.locator, request.transfer, request.fingerprint, false);
213
+ recomposed(connection, key, request.fingerprint);
214
+ if (copied.length > 0 && request.published !== null) {
215
+ owing(connection, key);
216
+ const named = JSON.stringify(copied);
217
+ connection.run(`INSERT INTO replica_entries (key, layer, path, visible, present, queried, content, binary, revision, file_id, created, created_instant, change, changed_instant, written, type, declared, folder, fields, links, part) SELECT ?, ${CONFIRMED}, path, 1, 1, queried, 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]);
218
+ 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]);
219
+ }
220
+ return { key, transfer: held?.transfer ?? request.transfer, staged: [...kept, ...copied] };
221
+ }
179
222
  export function refreshing(connection, request) {
180
223
  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]);
181
224
  if (held.length === 0)
182
225
  return null;
183
226
  recomposed(connection, request.key, request.composed.fingerprint);
184
227
  const composition = request.composed.composition;
185
- const owed = words();
186
228
  for (const entry of request.entries) {
187
- putEntry(connection, request.key, REFRESHED, entry, true, composition, owed);
229
+ putEntry(connection, request.key, REFRESHED, entry, true, composition);
188
230
  }
189
231
  for (const path of request.removals) {
190
232
  putEntry(connection, request.key, REFRESHED, {
191
233
  path,
192
234
  content: null,
193
- outline: null,
194
235
  binary: null,
195
236
  revision: null,
196
237
  fileId: null,
238
+ created: null,
197
239
  change: null,
198
240
  written: null,
199
- }, false, composition, owed);
241
+ facts: null,
242
+ }, false, composition);
200
243
  }
201
244
  for (const [list, present] of [
202
245
  [request.folders, 1],
@@ -206,11 +249,9 @@ export function refreshing(connection, request) {
206
249
  connection.run('INSERT OR REPLACE INTO replica_folders (key, layer, path, present) VALUES (?, ?, ?, ?)', [request.key, REFRESHED, path, present]);
207
250
  }
208
251
  }
209
- wordsWritten(connection, owed);
210
252
  return null;
211
253
  }
212
254
  function dropLayer(connection, key, layer) {
213
- unindexDocuments(connection, heldIds(connection, 'SELECT id FROM replica_entries WHERE key = ? AND layer = ?', [key, layer]));
214
255
  for (const table of ['replica_values', 'replica_links']) {
215
256
  connection.run(`DELETE FROM ${table} WHERE doc IN (SELECT id FROM replica_entries WHERE key = ? AND layer = ?)`, [key, layer]);
216
257
  }
@@ -229,7 +270,6 @@ function refreshedPaths(connection, key) {
229
270
  function applyRefresh(connection, key) {
230
271
  const touched = refreshedPaths(connection, key);
231
272
  const replaced = `SELECT c.id FROM replica_entries c JOIN replica_entries r ON r.key = c.key AND r.layer = ${REFRESHED} AND r.path = c.path WHERE c.key = ? AND c.layer = ${CONFIRMED}`;
232
- unindexDocuments(connection, heldIds(connection, replaced, [key]));
233
273
  for (const table of ['replica_values', 'replica_links']) {
234
274
  connection.run(`DELETE FROM ${table} WHERE doc IN (${replaced})`, [key]);
235
275
  }
@@ -255,15 +295,18 @@ function counted(connection, key, roots) {
255
295
  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)]);
256
296
  return number(row.held);
257
297
  }
258
- export function counts(connection, rooms) {
259
- return rooms.map(({ key, roots }) => counted(connection, key, roots));
260
- }
261
298
  function erasing(connection, key, locator) {
262
299
  connection.run('INSERT OR REPLACE INTO replica_erasing (key, locator) VALUES (?, ?)', [
263
300
  key,
264
301
  locator,
265
302
  ]);
266
303
  }
304
+ function saved(connection, key, entry, at) {
305
+ if (!entry.present || entry.created !== null)
306
+ return entry;
307
+ const [held] = connection.rows(`SELECT created FROM replica_entries WHERE key = ? AND layer = ${OVERLAY} AND path = ? AND present = 1`, [key, entry.path]);
308
+ return { ...entry, created: text(held?.created) ?? at };
309
+ }
267
310
  function commitRoom(connection, room) {
268
311
  const touched = new Set();
269
312
  recomposed(connection, room.key, room.composed.fingerprint);
@@ -277,51 +320,64 @@ function commitRoom(connection, room) {
277
320
  for (const path of applyRefresh(connection, room.key))
278
321
  touched.add(path);
279
322
  }
280
- const owed = words();
281
323
  for (const entry of room.confirmed?.set ?? []) {
282
- putEntry(connection, room.key, CONFIRMED, entry, true, composition, owed);
324
+ putEntry(connection, room.key, CONFIRMED, entry, true, composition);
283
325
  touched.add(entry.path);
284
326
  }
285
327
  for (const path of room.confirmed?.removed ?? []) {
286
- removeEntry(connection, room.key, CONFIRMED, path, owed);
328
+ removeEntry(connection, room.key, CONFIRMED, path);
287
329
  touched.add(path);
288
330
  }
331
+ const savedAt = new Date().toISOString();
289
332
  for (const entry of room.overlay?.set ?? []) {
290
- putEntry(connection, room.key, OVERLAY, entry, entry.present, composition, owed);
333
+ putEntry(connection, room.key, OVERLAY, saved(connection, room.key, entry, savedAt), entry.present, composition);
291
334
  touched.add(entry.path);
292
335
  }
293
336
  for (const path of room.overlay?.cleared ?? []) {
294
- removeEntry(connection, room.key, OVERLAY, path, owed);
337
+ removeEntry(connection, room.key, OVERLAY, path);
295
338
  touched.add(path);
296
339
  }
297
340
  const changed = [...touched];
298
341
  revealed(connection, room.key, changed);
299
342
  if (room.record !== undefined) {
300
- connection.run('INSERT OR REPLACE INTO replica_rooms (locator, key, record) VALUES (?, ?, ?)', [
301
- room.locator,
302
- room.key,
303
- room.record,
304
- ]);
343
+ connection.run(KEEP_MODEL, [room.record, room.record, room.record, room.record]);
344
+ connection.run(`INSERT OR REPLACE INTO replica_rooms (locator, key, record) VALUES (?, ?, ${MODEL_LEFT})`, [room.locator, room.key, room.record, room.record, room.record]);
305
345
  }
306
- wordsWritten(connection, owed);
307
346
  return room.published === undefined ? changed : null;
308
347
  }
348
+ const SCHEMA = '$.model.snapshot.schema';
349
+ const KEEP_MODEL = `INSERT OR IGNORE INTO replica_models (identity, version, schema)
350
+ SELECT json_extract(?, '$.model.snapshot.identity'), json_extract(?, '$.model.snapshot.version'),
351
+ json_extract(?, '${SCHEMA}')
352
+ WHERE json_type(?, '${SCHEMA}') = 'text'`;
353
+ const MODEL_LEFT = `CASE WHEN json_type(?, '${SCHEMA}') = 'text' THEN json_set(?, '${SCHEMA}', json('null')) ELSE ? END`;
354
+ const UNHELD_MODELS = `DELETE FROM replica_models WHERE NOT EXISTS (
355
+ SELECT 1 FROM replica_rooms r
356
+ WHERE json_extract(r.record, '$.model.snapshot.identity') = replica_models.identity
357
+ AND json_extract(r.record, '$.model.snapshot.version') = replica_models.version
358
+ )`;
359
+ const HELD_RECORDS = `SELECT r.locator, r.key, CASE WHEN m.schema IS NULL THEN r.record
360
+ ELSE json_set(r.record, '${SCHEMA}', m.schema) END AS record
361
+ FROM replica_rooms r
362
+ LEFT JOIN replica_models m
363
+ ON m.identity = json_extract(r.record, '$.model.snapshot.identity')
364
+ AND m.version = json_extract(r.record, '$.model.snapshot.version')`;
309
365
  export function commit(connection, rooms) {
310
- const counted = {};
311
366
  const changed = {};
312
367
  const indexed = {};
313
368
  for (const room of rooms) {
314
369
  changed[room.locator] = commitRoom(connection, room);
315
- counted[room.locator] = counts(connection, [
316
- { key: room.key, roots: room.roots ?? rootsOf(connection, room.locator) },
317
- ])[0];
318
- const [held] = connection.rows('SELECT rederiving, searching FROM replica_keys WHERE key = ?', [
370
+ connection.run('UPDATE replica_rooms SET documents = ? WHERE locator = ? AND key = ?', [
371
+ counted(connection, room.key, room.roots ?? rootsOf(connection, room.locator)),
372
+ room.locator,
319
373
  room.key,
320
374
  ]);
321
- indexed[room.locator] =
322
- held !== undefined && held.rederiving === null && held.searching === null;
375
+ const [held] = connection.rows('SELECT rederiving FROM replica_keys WHERE key = ?', [room.key]);
376
+ indexed[room.locator] = held !== undefined && held.rederiving === null;
323
377
  }
324
- return { counts: counted, changed, indexed };
378
+ if (rooms.some((room) => room.record !== undefined))
379
+ connection.run(UNHELD_MODELS);
380
+ return { changed, indexed };
325
381
  }
326
382
  function rootsOf(connection, locator) {
327
383
  const [row] = connection.rows('SELECT record FROM replica_rooms WHERE locator = ?', [locator]);
@@ -334,8 +390,8 @@ export function fenceRooms(connection, locators) {
334
390
  erasing(connection, number(row.key), locator);
335
391
  }
336
392
  connection.run('DELETE FROM replica_rooms WHERE locator = ?', [locator]);
337
- connection.run('DELETE FROM replica_opened WHERE locator = ?', [locator]);
338
393
  }
394
+ connection.run(UNHELD_MODELS);
339
395
  return null;
340
396
  }
341
397
  export function abandon(connection, locator) {
@@ -359,7 +415,6 @@ export function erase(connection, locator, limit) {
359
415
  return { remaining: false };
360
416
  const key = number(held.key);
361
417
  const batch = 'SELECT id FROM replica_entries WHERE key = ? ORDER BY id LIMIT ?';
362
- unindexDocuments(connection, heldIds(connection, batch, [key, limit]));
363
418
  for (const table of ['replica_values', 'replica_links']) {
364
419
  connection.run(`DELETE FROM ${table} WHERE doc IN (${batch})`, [key, limit]);
365
420
  }
@@ -379,41 +434,31 @@ function cursor(value) {
379
434
  export function rederive(connection, key, composed, bytes, ms, floor) {
380
435
  const started = performance.now();
381
436
  recomposed(connection, key, composed.fingerprint);
382
- const [held] = connection.rows('SELECT rederiving, searching FROM replica_keys WHERE key = ?', [
383
- key,
384
- ]);
437
+ const [held] = connection.rows('SELECT rederiving FROM replica_keys WHERE key = ?', [key]);
385
438
  const deriving = cursor(held?.rederiving);
386
- const searching = cursor(held?.searching);
387
- if (deriving === null && searching === null)
439
+ if (deriving === null)
388
440
  return { done: true, text: 0 };
389
441
  const composition = composed.composition;
390
- let last = Math.min(deriving ?? Infinity, searching ?? Infinity);
442
+ let last = deriving;
391
443
  let spent = 0;
392
444
  let built = 0;
393
445
  let read = false;
394
- const late = () => spent >= floor && performance.now() - started >= ms;
395
- const owed = [];
396
446
  reading: for (;;) {
397
- 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]);
447
+ 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]);
398
448
  for (const row of rows) {
399
449
  const id = number(row.id);
400
450
  const path = text(row.path);
401
- const content = text(row.content);
402
- const outline = json(row.outline);
403
- const owedRows = deriving !== null && id > deriving;
404
- const owedWords = searching !== null && id > searching && content !== null;
405
- if (owedRows) {
406
- const made = derived(path, content, outline, composition);
407
- dropDerived(connection, id);
408
- writeDerived(connection, id, key, made);
409
- }
410
- if (owedWords && described(path, content, outline).queried)
411
- owed.push({ id, path, content });
412
- else if (owedRows)
413
- built += content?.length ?? 0;
451
+ const size = row.size === null ? 0 : number(row.size);
452
+ dropDerived(connection, id);
453
+ writeDerived(connection, id, key, path, {
454
+ type: text(row.type),
455
+ fields: json(row.fields),
456
+ links: json(row.links) ?? [],
457
+ }, composition);
458
+ built += size;
414
459
  last = id;
415
- spent += ENTRY_WEIGHT + (content?.length ?? 0);
416
- if (spent >= bytes || late())
460
+ spent += ENTRY_WEIGHT + size;
461
+ if (spent >= bytes || (spent >= floor && performance.now() - started >= ms))
417
462
  break reading;
418
463
  }
419
464
  if (rows.length < DERIVED_ROWS) {
@@ -421,88 +466,34 @@ export function rederive(connection, key, composed, bytes, ms, floor) {
421
466
  break;
422
467
  }
423
468
  }
424
- let searched = last;
425
- for (const [at, { id, path, content }] of owed.entries()) {
426
- indexDocument(connection, id, path, content);
427
- built += content.length;
428
- const next = owed[at + 1];
429
- if (next !== undefined && late()) {
430
- searched = next.id - 1;
431
- break;
432
- }
433
- }
434
- const done = read && searched === last;
435
- connection.run('UPDATE replica_keys SET rederiving = ?, searching = ? WHERE key = ?', [
436
- deriving === null || read ? null : Math.max(deriving, last),
437
- searching === null || done ? null : Math.max(searching, searched),
438
- key,
439
- ]);
440
- return { done, text: built };
441
- }
442
- function outlinedAgain(connection, row, key, content, composition, owed) {
443
- const id = number(row.id);
444
- const path = text(row.path);
445
- const made = derived(path, content, json(row.outline), composition);
446
- 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]);
447
- dropDerived(connection, id);
448
- writeDerived(connection, id, key, made);
449
- owed.dropped.push(id);
450
- if (content !== null && made.queried)
451
- owed.made.push({ id, path, content });
452
- }
453
- export function holdTexts(connection, request) {
454
- const owed = words();
455
- const held = [];
456
- for (const { path, revision, content } of request.texts) {
457
- 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]);
458
- if (row === undefined)
459
- continue;
460
- outlinedAgain(connection, row, request.key, content, request.composed.composition, owed);
461
- connection.run('INSERT OR IGNORE INTO replica_opened (locator, path) VALUES (?, ?)', [
462
- request.locator,
463
- path,
464
- ]);
465
- held.push(path);
466
- }
467
- wordsWritten(connection, owed);
468
- return held;
469
- }
470
- export function unholdTexts(connection, request) {
471
- const owed = words();
472
- connection.run('DELETE FROM replica_opened WHERE locator = ?', [request.locator]);
473
- 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]);
474
- for (const row of rows) {
475
- outlinedAgain(connection, row, request.key, null, request.composed.composition, owed);
476
- }
477
- wordsWritten(connection, owed);
478
- return {
479
- paths: rows.map((row) => text(row.path)),
480
- bytes: rows.reduce((total, row) => total + number(row.bytes), 0),
481
- };
482
- }
483
- export function remembered(connection, request) {
484
- 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)]);
485
- return null;
486
- }
487
- export function wanted(connection, request) {
488
- return connection
489
- .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])
490
- .map((row) => ({ path: text(row.path), revision: text(row.revision) }));
469
+ connection.run('UPDATE replica_keys SET rederiving = ? WHERE key = ?', [read ? null : last, key]);
470
+ return { done: read, text: built };
491
471
  }
492
472
  function heldEntry(row) {
493
473
  return {
494
474
  path: text(row.path),
495
475
  content: text(row.content),
496
- outline: json(row.outline),
497
476
  binary: json(row.binary),
498
477
  revision: text(row.revision),
499
478
  fileId: text(row.file_id),
479
+ created: text(row.created),
500
480
  change: json(row.change),
501
481
  written: text(row.written),
502
482
  type: text(row.type),
483
+ facts: factsHeld(row),
484
+ };
485
+ }
486
+ function factsHeld(row) {
487
+ const type = text(row.declared);
488
+ if (type === null)
489
+ return null;
490
+ return {
491
+ type,
492
+ fields: json(row.fields) ?? {},
493
+ links: json(row.links) ?? [],
503
494
  };
504
495
  }
505
- const ENTRY_COLUMNS = 'e.path, e.content, e.outline, e.binary, e.revision, e.file_id, e.change, e.written, e.type';
496
+ 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';
506
497
  function unrefreshed(alias) {
507
498
  return `NOT EXISTS (SELECT 1 FROM replica_entries r WHERE r.key = ${alias}.key AND r.layer = ${REFRESHED} AND r.path = ${alias}.path)`;
508
499
  }
@@ -513,22 +504,6 @@ function viewed(view, alias) {
513
504
  return `${alias}.layer = ${CONFIRMED}`;
514
505
  return `${alias}.present = 1 AND (${alias}.layer = ${REFRESHED} OR (${alias}.layer = ${CONFIRMED} AND ${unrefreshed(alias)}))`;
515
506
  }
516
- function layers(view) {
517
- if (view !== 'refreshed')
518
- return [viewed(view, 'e')];
519
- return [
520
- `e.layer = ${REFRESHED} AND e.present = 1`,
521
- `e.layer = ${CONFIRMED} AND e.present = 1 AND ${unrefreshed('e')}`,
522
- ];
523
- }
524
- function pathOrdered(connection, columns, key, view, condition, after, limit) {
525
- const beyond = after === null ? '' : ' AND e.path > ?';
526
- const parts = layers(view).map((layer) => `SELECT ${columns} FROM replica_entries e WHERE e.key = ? AND ${layer}${condition}${beyond}`);
527
- return connection.rows(`${parts.join(' UNION ALL ')} ORDER BY path LIMIT ?`, [
528
- ...parts.flatMap(() => (after === null ? [key] : [key, after])),
529
- limit,
530
- ]);
531
- }
532
507
  export function entries(connection, key, view, list) {
533
508
  if (list.length === 0)
534
509
  return [];
@@ -547,19 +522,12 @@ export function typed(connection, key, view, list) {
547
522
  binary: number(row.binary) === 1,
548
523
  }));
549
524
  }
550
- export function referrers(connection, key, view, targets, kinds) {
525
+ export function referrers(connection, key, view, targets) {
551
526
  if (targets.length === 0)
552
527
  return [];
553
528
  return connection
554
- .rows(`SELECT DISTINCT e.path, l.target, l.kind 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 l.kind IN (SELECT value FROM json_each(?)) AND ${viewed(view, 'e')}`, [key, paths(targets), JSON.stringify(kinds)])
555
- .map((row) => ({
556
- path: text(row.path),
557
- target: text(row.target),
558
- kind: text(row.kind),
559
- }));
560
- }
561
- export function files(connection, key, view, after, limit) {
562
- 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) }));
529
+ .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)])
530
+ .map((row) => ({ path: text(row.path), target: text(row.target) }));
563
531
  }
564
532
  export function folders(connection, key, view, list) {
565
533
  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);
@@ -567,50 +535,103 @@ export function folders(connection, key, view, list) {
567
535
  export function typedWithin(connection, key, view, roots, within) {
568
536
  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);
569
537
  }
538
+ const ORPHANS = `SELECT k.key FROM replica_keys k
539
+ WHERE NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
540
+ AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)
541
+ AND (${UNPARTED} OR NOT EXISTS (
542
+ SELECT 1 FROM replica_entries e WHERE e.key = k.key
543
+ UNION ALL SELECT 1 FROM replica_folders f WHERE f.key = k.key
544
+ ))`;
545
+ const STAGED = `SELECT DISTINCT k.locator FROM replica_keys k
546
+ WHERE NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
547
+ AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)
548
+ AND k.key NOT IN (${ORPHANS})`;
570
549
  export function opened(connection) {
571
- const facts = {};
572
- for (const row of connection.rows('SELECT name, value FROM replica_facts')) {
573
- facts[text(row.name)] = text(row.value);
574
- }
575
550
  return {
576
- rooms: connection.rows('SELECT locator, key, record FROM replica_rooms').map((row) => ({
577
- locator: text(row.locator),
578
- key: number(row.key),
579
- record: text(row.record),
580
- })),
581
- keys: connection
582
- .rows('SELECT key, locator, transfer, rederiving, searching FROM replica_keys')
583
- .map((row) => ({
584
- key: number(row.key),
585
- locator: text(row.locator),
586
- transfer: text(row.transfer),
587
- rederiving: cursor(row.rederiving),
588
- searching: cursor(row.searching),
589
- })),
590
- facts,
591
- presentation: connection.rows('SELECT app, value FROM replica_presentation').map((row) => ({
592
- app: text(row.app),
593
- value: text(row.value),
594
- })),
595
- overlaid: connection
596
- .rows(`SELECT key, path FROM replica_entries WHERE layer = ${OVERLAY}`)
597
- .map((row) => ({ key: number(row.key), path: text(row.path) })),
598
- orphans: connection
599
- .rows(`SELECT k.key FROM replica_keys k
600
- WHERE NOT EXISTS (SELECT 1 FROM replica_rooms r WHERE r.key = k.key)
601
- AND NOT EXISTS (SELECT 1 FROM replica_erasing e WHERE e.key = k.key)`)
602
- .map((row) => number(row.key)),
603
- erasing: connection.rows('SELECT key, locator FROM replica_erasing').map((row) => ({
604
- key: number(row.key),
605
- locator: text(row.locator),
606
- })),
551
+ orphans: connection.rows(ORPHANS).map((row) => number(row.key)),
552
+ erasing: connection.rows('SELECT 1 FROM replica_erasing LIMIT 1').length > 0,
607
553
  };
608
554
  }
555
+ const ROOM_ROWS = `SELECT h.locator, h.key, h.record, r.documents, k.rederiving IS NULL AS indexed
556
+ FROM (${HELD_RECORDS}) h
557
+ JOIN replica_rooms r ON r.locator = h.locator
558
+ LEFT JOIN replica_keys k ON k.key = h.key`;
559
+ function roomRow(row) {
560
+ return {
561
+ locator: text(row.locator),
562
+ key: number(row.key),
563
+ record: text(row.record),
564
+ indexed: number(row.indexed) === 1,
565
+ documents: number(row.documents),
566
+ };
567
+ }
568
+ export function heldRooms(connection, locators) {
569
+ return (locators === null
570
+ ? connection.rows(`${ROOM_ROWS} ORDER BY h.locator`)
571
+ : connection.rows(`${ROOM_ROWS} WHERE h.locator IN (SELECT value FROM json_each(?)) ORDER BY h.locator`, [JSON.stringify(locators)])).map(roomRow);
572
+ }
573
+ export function heldRoom(connection, locator) {
574
+ return heldRooms(connection, [locator])[0] ?? null;
575
+ }
576
+ export function overlaid(connection, key) {
577
+ return connection
578
+ .rows(`SELECT path FROM replica_entries WHERE key = ? AND layer = ${OVERLAY}`, [key])
579
+ .map((row) => text(row.path));
580
+ }
581
+ export function covering(connection) {
582
+ return connection
583
+ .rows(`SELECT r.locator, r.key, r.documents, json_extract(r.record, '$.transfer') AS transfer,
584
+ json_extract(r.record, '$.roomVersion') AS room_version,
585
+ coalesce(json_extract(r.record, '$.inventory.bytes'), 0) AS bytes,
586
+ json_extract(r.record, '$.replica.offline') AS offline,
587
+ k.rederiving IS NULL AS indexed
588
+ FROM replica_rooms r LEFT JOIN replica_keys k ON k.key = r.key ORDER BY r.locator`)
589
+ .map((row) => ({
590
+ locator: text(row.locator),
591
+ key: number(row.key),
592
+ transfer: text(row.transfer),
593
+ roomVersion: text(row.room_version),
594
+ indexed: number(row.indexed) === 1,
595
+ documents: number(row.documents),
596
+ bytes: number(row.bytes),
597
+ offline: text(row.offline),
598
+ }));
599
+ }
600
+ export function offlineMode(connection, locator) {
601
+ const [held] = connection.rows("SELECT json_extract(record, '$.replica.offline') AS offline FROM replica_rooms WHERE locator = ?", [locator]);
602
+ return text(held?.offline);
603
+ }
604
+ export function stagedLocators(connection) {
605
+ return connection.rows(STAGED).map((row) => text(row.locator));
606
+ }
607
+ export function heldLocators(connection) {
608
+ return connection
609
+ .rows(`SELECT locator FROM replica_rooms
610
+ UNION SELECT locator FROM authored_room_drafts
611
+ UNION SELECT locator FROM authored_queues
612
+ UNION ${STAGED}`)
613
+ .map((row) => text(row.locator));
614
+ }
615
+ export function demoted(connection) {
616
+ connection.run(`UPDATE replica_rooms SET record = json_set(record, '$.model.status', 'cached')
617
+ WHERE json_extract(record, '$.model.status') = 'current'`);
618
+ return null;
619
+ }
620
+ export function facts(connection) {
621
+ const held = {};
622
+ for (const row of connection.rows('SELECT name, value FROM replica_facts')) {
623
+ held[text(row.name)] = text(row.value);
624
+ }
625
+ return held;
626
+ }
627
+ export function presentation(connection, app) {
628
+ const [held] = connection.rows('SELECT value FROM replica_presentation WHERE app = ?', [app]);
629
+ return text(held?.value);
630
+ }
609
631
  export function ended(connection) {
610
632
  for (const statement of REPLICA_TABLES) {
611
633
  const table = /^CREATE TABLE (\w+)/u.exec(statement)?.[1];
612
634
  if (table !== undefined)
613
635
  connection.run(`DELETE FROM ${table}`);
614
636
  }
615
- unindexAll(connection);
616
637
  }