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