@dxos/echo-pipeline 0.8.4-main.a4bbb77 → 0.8.4-main.abd8ff62ef
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/dist/lib/{browser/chunk-XGG76KKU.mjs → neutral/chunk-VIHYDCCC.mjs} +404 -963
- package/dist/lib/neutral/chunk-VIHYDCCC.mjs.map +7 -0
- package/dist/lib/{browser/chunk-BVR7OOZK.mjs → neutral/chunk-YBC6ONBZ.mjs} +23 -27
- package/dist/lib/neutral/chunk-YBC6ONBZ.mjs.map +7 -0
- package/dist/lib/{browser → neutral}/filter/index.mjs +1 -2
- package/dist/lib/{browser → neutral}/index.mjs +3143 -2206
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/{browser → neutral}/testing/index.mjs +103 -395
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/automerge/automerge-host.d.ts +43 -23
- package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
- package/dist/types/src/automerge/automerge-repo.test.d.ts +1 -1
- package/dist/types/src/automerge/automerge-repo.test.d.ts.map +1 -1
- package/dist/types/src/automerge/collection-synchronizer.d.ts +5 -3
- package/dist/types/src/automerge/collection-synchronizer.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-data-monitor.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-network-adapter.d.ts +17 -11
- package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-replicator.d.ts +20 -16
- package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
- package/dist/types/src/automerge/heads-store.d.ts +9 -2
- package/dist/types/src/automerge/heads-store.d.ts.map +1 -1
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts +4 -4
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts +8 -8
- package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator.d.ts +6 -5
- package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
- package/dist/types/src/automerge/network-protocol.d.ts.map +1 -1
- package/dist/types/src/automerge/space-collection.d.ts.map +1 -1
- package/dist/types/src/common/feeds.d.ts.map +1 -1
- package/dist/types/src/common/space-id.d.ts.map +1 -1
- package/dist/types/src/db-host/automerge-data-source.d.ts +35 -0
- package/dist/types/src/db-host/automerge-data-source.d.ts.map +1 -0
- package/dist/types/src/db-host/automerge-data-source.test.d.ts +2 -0
- package/dist/types/src/db-host/automerge-data-source.test.d.ts.map +1 -0
- package/dist/types/src/db-host/automerge-metrics.d.ts.map +1 -1
- package/dist/types/src/db-host/data-service.d.ts +7 -6
- package/dist/types/src/db-host/data-service.d.ts.map +1 -1
- package/dist/types/src/db-host/database-root.d.ts.map +1 -1
- package/dist/types/src/db-host/documents-synchronizer.d.ts +8 -7
- package/dist/types/src/db-host/documents-synchronizer.d.ts.map +1 -1
- package/dist/types/src/db-host/echo-host.d.ts +58 -28
- package/dist/types/src/db-host/echo-host.d.ts.map +1 -1
- package/dist/types/src/db-host/index.d.ts +2 -0
- package/dist/types/src/db-host/index.d.ts.map +1 -1
- package/dist/types/src/db-host/invalidation-hint.d.ts +19 -0
- package/dist/types/src/db-host/invalidation-hint.d.ts.map +1 -0
- package/dist/types/src/db-host/local-queue-service.d.ts +19 -0
- package/dist/types/src/db-host/local-queue-service.d.ts.map +1 -0
- package/dist/types/src/db-host/query-invalidation.test.d.ts +2 -0
- package/dist/types/src/db-host/query-invalidation.test.d.ts.map +1 -0
- package/dist/types/src/db-host/query-service.d.ts +18 -10
- package/dist/types/src/db-host/query-service.d.ts.map +1 -1
- package/dist/types/src/db-host/queue-data-source.d.ts +38 -0
- package/dist/types/src/db-host/queue-data-source.d.ts.map +1 -0
- package/dist/types/src/db-host/queue-service.test.d.ts +2 -0
- package/dist/types/src/db-host/queue-service.test.d.ts.map +1 -0
- package/dist/types/src/db-host/space-state-manager.d.ts +1 -0
- package/dist/types/src/db-host/space-state-manager.d.ts.map +1 -1
- package/dist/types/src/db-host/stub.d.ts +11 -0
- package/dist/types/src/db-host/stub.d.ts.map +1 -0
- package/dist/types/src/edge/echo-edge-replicator.d.ts +7 -6
- package/dist/types/src/edge/echo-edge-replicator.d.ts.map +1 -1
- package/dist/types/src/edge/inflight-request-limiter.d.ts.map +1 -1
- package/dist/types/src/filter/filter-match.d.ts +2 -2
- package/dist/types/src/filter/filter-match.d.ts.map +1 -1
- package/dist/types/src/metadata/metadata-store.d.ts.map +1 -1
- package/dist/types/src/pipeline/message-selector.d.ts.map +1 -1
- package/dist/types/src/pipeline/pipeline.d.ts +2 -2
- package/dist/types/src/pipeline/pipeline.d.ts.map +1 -1
- package/dist/types/src/pipeline/timeframe-clock.d.ts.map +1 -1
- package/dist/types/src/query/errors.d.ts +16 -20
- package/dist/types/src/query/errors.d.ts.map +1 -1
- package/dist/types/src/query/plan.d.ts +59 -8
- package/dist/types/src/query/plan.d.ts.map +1 -1
- package/dist/types/src/query/query-executor.d.ts +62 -10
- package/dist/types/src/query/query-executor.d.ts.map +1 -1
- package/dist/types/src/query/query-planner.d.ts +10 -1
- package/dist/types/src/query/query-planner.d.ts.map +1 -1
- package/dist/types/src/space/admission-discovery-extension.d.ts.map +1 -1
- package/dist/types/src/space/auth.d.ts +3 -3
- package/dist/types/src/space/auth.d.ts.map +1 -1
- package/dist/types/src/space/control-pipeline.d.ts +4 -3
- package/dist/types/src/space/control-pipeline.d.ts.map +1 -1
- package/dist/types/src/space/space-manager.d.ts +7 -7
- package/dist/types/src/space/space-manager.d.ts.map +1 -1
- package/dist/types/src/space/space-protocol.d.ts +9 -8
- package/dist/types/src/space/space-protocol.d.ts.map +1 -1
- package/dist/types/src/space/space.d.ts +5 -5
- package/dist/types/src/space/space.d.ts.map +1 -1
- package/dist/types/src/testing/change-metadata.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +0 -2
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/test-agent-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-feed-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-network-adapter.d.ts +1 -1
- package/dist/types/src/testing/test-network-adapter.d.ts.map +1 -1
- package/dist/types/src/testing/test-replicator.d.ts +13 -13
- package/dist/types/src/testing/test-replicator.d.ts.map +1 -1
- package/dist/types/src/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +51 -54
- package/src/automerge/automerge-host.test.ts +42 -23
- package/src/automerge/automerge-host.ts +163 -88
- package/src/automerge/automerge-repo.test.ts +149 -32
- package/src/automerge/collection-synchronizer.test.ts +19 -13
- package/src/automerge/collection-synchronizer.ts +69 -28
- package/src/automerge/echo-network-adapter.test.ts +2 -1
- package/src/automerge/echo-network-adapter.ts +40 -29
- package/src/automerge/echo-replicator.ts +21 -16
- package/src/automerge/heads-store.ts +42 -3
- package/src/automerge/leveldb-storage-adapter.ts +4 -4
- package/src/automerge/mesh-echo-replicator-connection.ts +8 -8
- package/src/automerge/mesh-echo-replicator.ts +14 -11
- package/src/db-host/automerge-data-source.test.ts +299 -0
- package/src/db-host/automerge-data-source.ts +186 -0
- package/src/db-host/data-service.ts +17 -10
- package/src/db-host/documents-synchronizer.test.ts +143 -14
- package/src/db-host/documents-synchronizer.ts +54 -51
- package/src/db-host/echo-host.ts +321 -86
- package/src/db-host/index.ts +2 -0
- package/src/db-host/invalidation-hint.ts +50 -0
- package/src/db-host/local-queue-service.ts +127 -0
- package/src/db-host/query-invalidation.test.ts +270 -0
- package/src/db-host/query-service.ts +110 -112
- package/src/db-host/queue-data-source.ts +159 -0
- package/src/db-host/queue-service.test.ts +145 -0
- package/src/db-host/space-state-manager.ts +5 -1
- package/src/db-host/stub.ts +33 -0
- package/src/edge/echo-edge-replicator.test.ts +9 -9
- package/src/edge/echo-edge-replicator.ts +76 -47
- package/src/filter/filter-match.test.ts +41 -25
- package/src/filter/filter-match.ts +33 -36
- package/src/metadata/metadata-store.ts +8 -5
- package/src/pipeline/pipeline-stress.test.ts +1 -3
- package/src/pipeline/pipeline.test.ts +0 -1
- package/src/pipeline/pipeline.ts +2 -3
- package/src/query/errors.ts +2 -2
- package/src/query/plan.ts +71 -8
- package/src/query/query-executor.ts +1170 -197
- package/src/query/query-planner.test.ts +854 -86
- package/src/query/query-planner.ts +408 -38
- package/src/space/admission-discovery-extension.ts +1 -1
- package/src/space/auth.ts +6 -6
- package/src/space/control-pipeline.test.ts +2 -2
- package/src/space/control-pipeline.ts +7 -7
- package/src/space/space-manager.ts +12 -17
- package/src/space/space-protocol.browser.test.ts +13 -12
- package/src/space/space-protocol.test.ts +13 -13
- package/src/space/space-protocol.ts +16 -15
- package/src/space/space.test.ts +2 -2
- package/src/space/space.ts +9 -10
- package/src/testing/index.ts +0 -2
- package/src/testing/test-agent-builder.ts +0 -1
- package/src/testing/test-network-adapter.ts +16 -3
- package/src/testing/test-replicator.ts +19 -18
- package/src/util.ts +7 -3
- package/dist/lib/browser/chunk-BVR7OOZK.mjs.map +0 -7
- package/dist/lib/browser/chunk-CGS2ULMK.mjs +0 -11
- package/dist/lib/browser/chunk-CGS2ULMK.mjs.map +0 -7
- package/dist/lib/browser/chunk-XGG76KKU.mjs.map +0 -7
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-CHMJJ4DG.mjs +0 -2230
- package/dist/lib/node-esm/chunk-CHMJJ4DG.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-L3ZEKVDR.mjs +0 -251
- package/dist/lib/node-esm/chunk-L3ZEKVDR.mjs.map +0 -7
- package/dist/lib/node-esm/filter/index.mjs +0 -13
- package/dist/lib/node-esm/filter/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -5560
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -805
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/db-host/documents-iterator.d.ts +0 -7
- package/dist/types/src/db-host/documents-iterator.d.ts.map +0 -1
- package/dist/types/src/testing/test-data.d.ts +0 -18
- package/dist/types/src/testing/test-data.d.ts.map +0 -1
- package/dist/types/src/testing/test-schema.d.ts +0 -37
- package/dist/types/src/testing/test-schema.d.ts.map +0 -1
- package/src/db-host/documents-iterator.ts +0 -74
- package/src/testing/test-data.ts +0 -127
- package/src/testing/test-schema.ts +0 -53
- /package/dist/lib/{browser → neutral}/filter/index.mjs.map +0 -0
package/src/db-host/echo-host.ts
CHANGED
|
@@ -2,67 +2,71 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from '@automerge/automerge-repo';
|
|
12
|
-
|
|
13
|
-
import { type Context, LifecycleState, Resource } from '@dxos/context';
|
|
5
|
+
import { type AnyDocumentId, type AutomergeUrl, type DocHandle, type DocumentId } from '@automerge/automerge-repo';
|
|
6
|
+
import * as SqlClient from '@effect/sql/SqlClient';
|
|
7
|
+
import * as Effect from 'effect/Effect';
|
|
8
|
+
|
|
9
|
+
import { asyncTimeout, DeferredTask, sleep } from '@dxos/async';
|
|
10
|
+
import { Context, LifecycleState, Resource } from '@dxos/context';
|
|
14
11
|
import { todo } from '@dxos/debug';
|
|
15
12
|
import { type DatabaseDirectory, SpaceDocVersion, createIdFromSpaceKey } from '@dxos/echo-protocol';
|
|
16
|
-
import {
|
|
13
|
+
import { RuntimeProvider } from '@dxos/effect';
|
|
14
|
+
import { FeedStore } from '@dxos/feed';
|
|
15
|
+
import { IndexEngine, type IndexingResult } from '@dxos/index-core';
|
|
17
16
|
import { invariant } from '@dxos/invariant';
|
|
18
|
-
import { type PublicKey, type SpaceId } from '@dxos/keys';
|
|
17
|
+
import { type ObjectId, type PublicKey, type SpaceId } from '@dxos/keys';
|
|
19
18
|
import { type LevelDB } from '@dxos/kv-store';
|
|
20
|
-
import {
|
|
19
|
+
import { log } from '@dxos/log';
|
|
20
|
+
import { type FeedProtocol } from '@dxos/protocols';
|
|
21
|
+
import type { SyncQueueRequest } from '@dxos/protocols/proto/dxos/client/services';
|
|
22
|
+
import type * as SqlTransaction from '@dxos/sql-sqlite/SqlTransaction';
|
|
21
23
|
import { trace } from '@dxos/tracing';
|
|
22
24
|
|
|
23
25
|
import {
|
|
24
26
|
AutomergeHost,
|
|
27
|
+
type AutomergeReplicator,
|
|
25
28
|
type CreateDocOptions,
|
|
26
29
|
EchoDataMonitor,
|
|
27
30
|
type EchoDataStats,
|
|
28
|
-
type EchoReplicator,
|
|
29
|
-
FIND_PARAMS,
|
|
30
31
|
type LoadDocOptions,
|
|
31
32
|
type PeerIdProvider,
|
|
32
33
|
type RootDocumentSpaceKeyProvider,
|
|
33
34
|
deriveCollectionIdFromSpaceId,
|
|
34
35
|
} from '../automerge';
|
|
35
|
-
|
|
36
|
+
import { AutomergeDataSource } from './automerge-data-source';
|
|
36
37
|
import { DataServiceImpl } from './data-service';
|
|
37
38
|
import { type DatabaseRoot } from './database-root';
|
|
38
|
-
import {
|
|
39
|
+
import { hintFromIndexingResult } from './invalidation-hint';
|
|
40
|
+
import { LocalQueueServiceImpl } from './local-queue-service';
|
|
39
41
|
import { QueryServiceImpl } from './query-service';
|
|
42
|
+
import { QueueDataSource } from './queue-data-source';
|
|
40
43
|
import { SpaceStateManager } from './space-state-manager';
|
|
41
44
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
* @default false
|
|
50
|
-
*/
|
|
51
|
-
vector: boolean;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const DEFAULT_INDEXING_CONFIG: EchoHostIndexingConfig = {
|
|
55
|
-
// TODO(dmaretskyi): Disabled by default since embedding generation is expensive.
|
|
56
|
-
fullText: false,
|
|
57
|
-
vector: false,
|
|
58
|
-
};
|
|
45
|
+
/**
|
|
46
|
+
* Executes "PRAGMA quick_check;" on SQLite database on startup.
|
|
47
|
+
*
|
|
48
|
+
* NOTE: Keep DISABLED in production, "quick" check is O(dbSize) which took 6 seconds for my relatively-small profile.
|
|
49
|
+
*
|
|
50
|
+
*/
|
|
51
|
+
const RUN_SQLITE_QUICK_CHECK_ON_STARTUP = false;
|
|
59
52
|
|
|
60
|
-
export type
|
|
53
|
+
export type EchoHostProps = {
|
|
61
54
|
kv: LevelDB;
|
|
62
55
|
peerIdProvider?: PeerIdProvider;
|
|
63
56
|
getSpaceKeyByRootDocumentId?: RootDocumentSpaceKeyProvider;
|
|
64
57
|
|
|
65
|
-
|
|
58
|
+
runtime: RuntimeProvider.RuntimeProvider<SqlClient.SqlClient | SqlTransaction.SqlTransaction>;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* This peer is allowed to assign positions (global-order) to items appended to the queue.
|
|
62
|
+
* @default false
|
|
63
|
+
*/
|
|
64
|
+
assignQueuePositions?: boolean;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Callback to run blocking queue sync.
|
|
68
|
+
*/
|
|
69
|
+
syncQueue?: (ctx: Context, request: SyncQueueRequest) => Promise<void>;
|
|
66
70
|
};
|
|
67
71
|
|
|
68
72
|
/**
|
|
@@ -72,51 +76,60 @@ export type EchoHostParams = {
|
|
|
72
76
|
* Can sync with pluggable data replicators.
|
|
73
77
|
*/
|
|
74
78
|
export class EchoHost extends Resource {
|
|
75
|
-
private readonly _indexMetadataStore: IndexMetadataStore;
|
|
76
|
-
private readonly _indexer: Indexer;
|
|
77
79
|
private readonly _automergeHost: AutomergeHost;
|
|
78
80
|
private readonly _queryService: QueryServiceImpl;
|
|
79
81
|
private readonly _dataService: DataServiceImpl;
|
|
80
82
|
private readonly _spaceStateManager = new SpaceStateManager();
|
|
81
83
|
private readonly _echoDataMonitor: EchoDataMonitor;
|
|
82
84
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
+
private readonly _automergeDataSource: AutomergeDataSource;
|
|
86
|
+
private readonly _indexEngine: IndexEngine;
|
|
87
|
+
private readonly _runtime: RuntimeProvider.RuntimeProvider<SqlClient.SqlClient | SqlTransaction.SqlTransaction>;
|
|
88
|
+
private readonly _feedStore: FeedStore;
|
|
89
|
+
private readonly _queueDataSource: QueueDataSource;
|
|
90
|
+
|
|
91
|
+
private _updateIndexes!: DeferredTask;
|
|
92
|
+
|
|
93
|
+
private _queuesService: FeedProtocol.QueueService;
|
|
94
|
+
|
|
95
|
+
private _indexesUpToDate = false;
|
|
85
96
|
|
|
86
|
-
|
|
97
|
+
constructor({
|
|
98
|
+
kv,
|
|
99
|
+
peerIdProvider,
|
|
100
|
+
getSpaceKeyByRootDocumentId,
|
|
101
|
+
runtime,
|
|
102
|
+
assignQueuePositions = false,
|
|
103
|
+
syncQueue,
|
|
104
|
+
}: EchoHostProps) {
|
|
105
|
+
super();
|
|
87
106
|
|
|
88
|
-
this._indexMetadataStore = new IndexMetadataStore({ db: kv.sublevel('index-metadata') });
|
|
89
107
|
this._echoDataMonitor = new EchoDataMonitor();
|
|
90
108
|
this._automergeHost = new AutomergeHost({
|
|
91
109
|
db: kv,
|
|
92
110
|
dataMonitor: this._echoDataMonitor,
|
|
93
|
-
indexMetadataStore: this._indexMetadataStore,
|
|
94
111
|
peerIdProvider,
|
|
95
112
|
getSpaceKeyByRootDocumentId,
|
|
96
113
|
});
|
|
97
114
|
|
|
98
|
-
this.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
enabled: true,
|
|
107
|
-
indexes: [
|
|
108
|
-
//
|
|
109
|
-
{ kind: IndexKind.Kind.SCHEMA_MATCH },
|
|
110
|
-
{ kind: IndexKind.Kind.GRAPH },
|
|
111
|
-
|
|
112
|
-
...(indexingConfig.fullText ? [{ kind: IndexKind.Kind.FULL_TEXT }] : []),
|
|
113
|
-
...(indexingConfig.vector ? [{ kind: IndexKind.Kind.VECTOR }] : []),
|
|
114
|
-
],
|
|
115
|
+
this._runtime = runtime;
|
|
116
|
+
this._automergeDataSource = new AutomergeDataSource(this._automergeHost);
|
|
117
|
+
|
|
118
|
+
this._feedStore = new FeedStore({ assignPositions: assignQueuePositions, localActorId: crypto.randomUUID() });
|
|
119
|
+
this._queueDataSource = new QueueDataSource({
|
|
120
|
+
feedStore: this._feedStore,
|
|
121
|
+
runtime: this._runtime,
|
|
122
|
+
getSpaceIds: () => this._spaceStateManager.spaceIds,
|
|
115
123
|
});
|
|
124
|
+
this._queuesService = new LocalQueueServiceImpl(runtime, this._feedStore, syncQueue);
|
|
125
|
+
|
|
126
|
+
// SQLite-based index engine for all queries.
|
|
127
|
+
this._indexEngine = new IndexEngine();
|
|
116
128
|
|
|
117
129
|
this._queryService = new QueryServiceImpl({
|
|
118
130
|
automergeHost: this._automergeHost,
|
|
119
|
-
|
|
131
|
+
indexEngine: this._indexEngine,
|
|
132
|
+
runtime: this._runtime,
|
|
120
133
|
spaceStateManager: this._spaceStateManager,
|
|
121
134
|
});
|
|
122
135
|
|
|
@@ -124,7 +137,9 @@ export class EchoHost extends Resource {
|
|
|
124
137
|
automergeHost: this._automergeHost,
|
|
125
138
|
spaceStateManager: this._spaceStateManager,
|
|
126
139
|
updateIndexes: async () => {
|
|
127
|
-
|
|
140
|
+
do {
|
|
141
|
+
await this._updateIndexes.runBlocking();
|
|
142
|
+
} while (!this._indexesUpToDate);
|
|
128
143
|
},
|
|
129
144
|
});
|
|
130
145
|
|
|
@@ -169,6 +184,10 @@ export class EchoHost extends Resource {
|
|
|
169
184
|
});
|
|
170
185
|
}
|
|
171
186
|
|
|
187
|
+
get spaceIds(): SpaceId[] {
|
|
188
|
+
return this._spaceStateManager.spaceIds;
|
|
189
|
+
}
|
|
190
|
+
|
|
172
191
|
get queryService(): QueryServiceImpl {
|
|
173
192
|
return this._queryService;
|
|
174
193
|
}
|
|
@@ -177,22 +196,56 @@ export class EchoHost extends Resource {
|
|
|
177
196
|
return this._dataService;
|
|
178
197
|
}
|
|
179
198
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
*/
|
|
183
|
-
get automergeRepo(): Repo {
|
|
184
|
-
return this._automergeHost.repo;
|
|
199
|
+
get queuesService(): FeedProtocol.QueueService {
|
|
200
|
+
return this._queuesService;
|
|
185
201
|
}
|
|
186
202
|
|
|
187
203
|
get roots(): ReadonlyMap<DocumentId, DatabaseRoot> {
|
|
188
204
|
return this._spaceStateManager.roots;
|
|
189
205
|
}
|
|
190
206
|
|
|
207
|
+
get feedStore(): FeedStore {
|
|
208
|
+
return this._feedStore;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Index engine for queries.
|
|
213
|
+
*/
|
|
214
|
+
get indexEngine(): IndexEngine {
|
|
215
|
+
return this._indexEngine;
|
|
216
|
+
}
|
|
217
|
+
|
|
191
218
|
protected override async _open(ctx: Context): Promise<void> {
|
|
192
|
-
|
|
193
|
-
await this.
|
|
219
|
+
log('echo-host: opening automerge host...');
|
|
220
|
+
await this._automergeHost.open(ctx);
|
|
221
|
+
log('echo-host: automerge host opened');
|
|
222
|
+
|
|
223
|
+
log('echo-host: opening query service...');
|
|
194
224
|
await this._queryService.open(ctx);
|
|
225
|
+
log('echo-host: query service opened');
|
|
226
|
+
|
|
227
|
+
log('echo-host: opening space state manager...');
|
|
195
228
|
await this._spaceStateManager.open(ctx);
|
|
229
|
+
log('echo-host: space state manager opened');
|
|
230
|
+
|
|
231
|
+
// Timeout needs to be outside effect runtime to catch the layer hanging on startup.
|
|
232
|
+
await asyncTimeout(
|
|
233
|
+
RuntimeProvider.runPromise(this._runtime)(testSqlite()),
|
|
234
|
+
15_000,
|
|
235
|
+
new Error('SQLite quick check timed out'),
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
log('echo-host: running index engine migration...');
|
|
239
|
+
await RuntimeProvider.runPromise(this._runtime)(this._indexEngine.migrate());
|
|
240
|
+
log('echo-host: index engine migration done');
|
|
241
|
+
this._updateIndexes = new DeferredTask(this._ctx, this._runUpdateIndexes);
|
|
242
|
+
|
|
243
|
+
log('echo-host: running feed store migration...');
|
|
244
|
+
await RuntimeProvider.runPromise(this._runtime)(this._feedStore.migrate());
|
|
245
|
+
log('echo-host: feed store migration done');
|
|
246
|
+
this._feedStore.onNewBlocks.on(this._ctx, () => {
|
|
247
|
+
this._updateIndexes.schedule();
|
|
248
|
+
});
|
|
196
249
|
|
|
197
250
|
this._spaceStateManager.spaceDocumentListUpdated.on(this._ctx, (e) => {
|
|
198
251
|
if (e.previousRootId) {
|
|
@@ -204,29 +257,32 @@ export class EchoHost extends Resource {
|
|
|
204
257
|
);
|
|
205
258
|
});
|
|
206
259
|
this._automergeHost.documentsSaved.on(this._ctx, () => {
|
|
207
|
-
this.
|
|
260
|
+
this._updateIndexes.schedule();
|
|
208
261
|
});
|
|
262
|
+
this._updateIndexes.schedule();
|
|
263
|
+
log('echo-host: open complete');
|
|
209
264
|
}
|
|
210
265
|
|
|
211
266
|
protected override async _close(ctx: Context): Promise<void> {
|
|
212
267
|
await this._queryService.close(ctx);
|
|
213
268
|
await this._spaceStateManager.close(ctx);
|
|
214
|
-
await this._indexer.close(ctx);
|
|
215
269
|
await this._automergeHost.close();
|
|
216
270
|
}
|
|
217
271
|
|
|
218
272
|
/**
|
|
219
273
|
* Flush all pending writes to the underlying storage.
|
|
220
274
|
*/
|
|
221
|
-
async flush(): Promise<void> {
|
|
222
|
-
await this._automergeHost.flush();
|
|
275
|
+
async flush(ctx: Context): Promise<void> {
|
|
276
|
+
await this._automergeHost.flush(ctx);
|
|
223
277
|
}
|
|
224
278
|
|
|
225
279
|
/**
|
|
226
280
|
* Perform any pending index updates.
|
|
227
281
|
*/
|
|
228
282
|
async updateIndexes(): Promise<void> {
|
|
229
|
-
|
|
283
|
+
do {
|
|
284
|
+
await this._updateIndexes.runBlocking();
|
|
285
|
+
} while (!this._indexesUpToDate);
|
|
230
286
|
}
|
|
231
287
|
|
|
232
288
|
/**
|
|
@@ -236,25 +292,25 @@ export class EchoHost extends Resource {
|
|
|
236
292
|
return await this._automergeHost.loadDoc(ctx, documentId, opts);
|
|
237
293
|
}
|
|
238
294
|
|
|
239
|
-
async exportDoc(
|
|
240
|
-
return await this._automergeHost.exportDoc(
|
|
295
|
+
async exportDoc(id: AnyDocumentId): Promise<Uint8Array> {
|
|
296
|
+
return await this._automergeHost.exportDoc(id);
|
|
241
297
|
}
|
|
242
298
|
|
|
243
299
|
/**
|
|
244
300
|
* Create new persisted document.
|
|
245
301
|
*/
|
|
246
|
-
createDoc<T>(initialValue?: T, opts?: CreateDocOptions): DocHandle<T
|
|
302
|
+
async createDoc<T>(initialValue?: T, opts?: CreateDocOptions): Promise<DocHandle<T>> {
|
|
247
303
|
return this._automergeHost.createDoc(initialValue, opts);
|
|
248
304
|
}
|
|
249
305
|
|
|
250
306
|
/**
|
|
251
307
|
* Create new space root.
|
|
252
308
|
*/
|
|
253
|
-
async createSpaceRoot(spaceKey: PublicKey): Promise<DatabaseRoot> {
|
|
309
|
+
async createSpaceRoot(ctx: Context, spaceKey: PublicKey): Promise<DatabaseRoot> {
|
|
254
310
|
invariant(this._lifecycleState === LifecycleState.OPEN);
|
|
255
311
|
const spaceId = await createIdFromSpaceKey(spaceKey);
|
|
256
312
|
|
|
257
|
-
const automergeRoot = this._automergeHost.createDoc<DatabaseDirectory>({
|
|
313
|
+
const automergeRoot = await this._automergeHost.createDoc<DatabaseDirectory>({
|
|
258
314
|
version: SpaceDocVersion.CURRENT,
|
|
259
315
|
access: { spaceKey: spaceKey.toHex() },
|
|
260
316
|
|
|
@@ -263,43 +319,222 @@ export class EchoHost extends Resource {
|
|
|
263
319
|
links: {},
|
|
264
320
|
});
|
|
265
321
|
|
|
266
|
-
await this._automergeHost.flush({ documentIds: [automergeRoot.documentId] });
|
|
322
|
+
await this._automergeHost.flush(ctx, { documentIds: [automergeRoot.documentId] });
|
|
267
323
|
|
|
268
|
-
return await this.openSpaceRoot(spaceId, automergeRoot.url);
|
|
324
|
+
return await this.openSpaceRoot(ctx, spaceId, automergeRoot.url);
|
|
269
325
|
}
|
|
270
326
|
|
|
271
327
|
// TODO(dmaretskyi): Change to document id.
|
|
272
|
-
async openSpaceRoot(spaceId: SpaceId, automergeUrl: AutomergeUrl): Promise<DatabaseRoot> {
|
|
328
|
+
async openSpaceRoot(ctx: Context, spaceId: SpaceId, automergeUrl: AutomergeUrl): Promise<DatabaseRoot> {
|
|
273
329
|
invariant(this._lifecycleState === LifecycleState.OPEN);
|
|
274
|
-
const handle = await this._automergeHost.
|
|
330
|
+
const handle = await this._automergeHost.loadDoc<DatabaseDirectory>(ctx, automergeUrl, {
|
|
331
|
+
fetchFromNetwork: true,
|
|
332
|
+
});
|
|
275
333
|
await handle.whenReady();
|
|
276
334
|
|
|
277
335
|
return this._spaceStateManager.assignRootToSpace(spaceId, handle);
|
|
278
336
|
}
|
|
279
337
|
|
|
280
338
|
// TODO(dmaretskyi): Change to document id.
|
|
281
|
-
async closeSpaceRoot(
|
|
339
|
+
async closeSpaceRoot(_automergeUrl: AutomergeUrl): Promise<void> {
|
|
282
340
|
todo();
|
|
283
341
|
}
|
|
284
342
|
|
|
285
343
|
/**
|
|
286
344
|
* Install data replicator.
|
|
287
345
|
*/
|
|
288
|
-
async addReplicator(replicator:
|
|
289
|
-
await this._automergeHost.addReplicator(replicator);
|
|
346
|
+
async addReplicator(ctx: Context, replicator: AutomergeReplicator): Promise<void> {
|
|
347
|
+
await this._automergeHost.addReplicator(ctx, replicator);
|
|
290
348
|
}
|
|
291
349
|
|
|
292
350
|
/**
|
|
293
351
|
* Remove data replicator.
|
|
294
352
|
*/
|
|
295
|
-
async removeReplicator(replicator:
|
|
353
|
+
async removeReplicator(replicator: AutomergeReplicator): Promise<void> {
|
|
296
354
|
await this._automergeHost.removeReplicator(replicator);
|
|
297
355
|
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Run collection sync for the given space.
|
|
359
|
+
* Does not wait for the sync to complete.
|
|
360
|
+
*/
|
|
361
|
+
async runCollectionSync(spaceId: SpaceId) {
|
|
362
|
+
const root = this._spaceStateManager.getRootBySpaceId(spaceId);
|
|
363
|
+
if (!root) {
|
|
364
|
+
throw new Error(`Space not found: ${spaceId}`);
|
|
365
|
+
}
|
|
366
|
+
this._automergeHost.refreshCollection(deriveCollectionIdFromSpaceId(spaceId, root.documentId));
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Get all feeds and their blocks for a space.
|
|
371
|
+
* Used for space archive export.
|
|
372
|
+
*/
|
|
373
|
+
async getAllFeedsForSpace(
|
|
374
|
+
spaceId: SpaceId,
|
|
375
|
+
): Promise<Array<{ feedId: string; feedNamespace: string; blocks: FeedProtocol.Block[] }>> {
|
|
376
|
+
return RuntimeProvider.runPromise(this._runtime)(this._feedStore.getAllFeedsForSpace({ spaceId }));
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
private _runUpdateIndexes = async (): Promise<void> => {
|
|
380
|
+
try {
|
|
381
|
+
const combinedResult = _makeEmptyMergedResult();
|
|
382
|
+
|
|
383
|
+
{
|
|
384
|
+
performance.mark('indexEngine.update.automerge:start');
|
|
385
|
+
const result = await this._indexEngine
|
|
386
|
+
.update(this._ctx, this._automergeDataSource, { spaceId: null, limit: 50 })
|
|
387
|
+
.pipe(RuntimeProvider.runPromise(this._runtime));
|
|
388
|
+
_mergeInto(combinedResult, result);
|
|
389
|
+
performance.measure('Index Automerge', {
|
|
390
|
+
start: 'indexEngine.update.automerge:start',
|
|
391
|
+
detail: {
|
|
392
|
+
devtools: {
|
|
393
|
+
dataType: 'track-entry',
|
|
394
|
+
track: 'Indexing',
|
|
395
|
+
trackGroup: 'ECHO', // Group related tracks together
|
|
396
|
+
color: 'tertiary-dark',
|
|
397
|
+
properties: [['count', result.updated]],
|
|
398
|
+
},
|
|
399
|
+
},
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
{
|
|
404
|
+
performance.mark('indexEngine.update.queue:start');
|
|
405
|
+
const result = await this._indexEngine
|
|
406
|
+
.update(this._ctx, this._queueDataSource, { spaceId: null, limit: 50 })
|
|
407
|
+
.pipe(RuntimeProvider.runPromise(this._runtime));
|
|
408
|
+
_mergeInto(combinedResult, result);
|
|
409
|
+
performance.measure('Index Queues', {
|
|
410
|
+
start: 'indexEngine.update.queue:start',
|
|
411
|
+
detail: {
|
|
412
|
+
devtools: {
|
|
413
|
+
dataType: 'track-entry',
|
|
414
|
+
track: 'Indexing',
|
|
415
|
+
trackGroup: 'ECHO',
|
|
416
|
+
color: 'tertiary-dark',
|
|
417
|
+
properties: [['count', result.updated]],
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
log.verbose('indexEngine update completed', {
|
|
424
|
+
updated: combinedResult.updated,
|
|
425
|
+
done: combinedResult.done,
|
|
426
|
+
spaces: combinedResult.spaces.size,
|
|
427
|
+
queues: combinedResult.queues.size,
|
|
428
|
+
documents: combinedResult.documents.size,
|
|
429
|
+
types: combinedResult.types.size,
|
|
430
|
+
objects: combinedResult.objects.size,
|
|
431
|
+
});
|
|
432
|
+
await sleep(1);
|
|
433
|
+
if (!combinedResult.done) {
|
|
434
|
+
this._indexesUpToDate = false;
|
|
435
|
+
this._updateIndexes!.schedule();
|
|
436
|
+
} else {
|
|
437
|
+
this._indexesUpToDate = true;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Invalidate queries after index update — the indexer is the sole invalidation source.
|
|
441
|
+
const hint = hintFromIndexingResult(combinedResult);
|
|
442
|
+
if (hint) {
|
|
443
|
+
this._queryService.invalidateQueries(hint);
|
|
444
|
+
}
|
|
445
|
+
} catch (err) {
|
|
446
|
+
log.catch(err);
|
|
447
|
+
// Failsafe: prevent queries from freezing if the indexer faults.
|
|
448
|
+
this._queryService.invalidateQueries();
|
|
449
|
+
throw err;
|
|
450
|
+
}
|
|
451
|
+
};
|
|
298
452
|
}
|
|
299
453
|
|
|
300
454
|
export type { EchoDataStats };
|
|
301
455
|
|
|
456
|
+
type MutableIndexingAccumulator = {
|
|
457
|
+
updated: number;
|
|
458
|
+
done: boolean;
|
|
459
|
+
spaces: Set<SpaceId>;
|
|
460
|
+
queues: Set<ObjectId>;
|
|
461
|
+
documents: Set<string>;
|
|
462
|
+
types: Set<string>;
|
|
463
|
+
objects: Set<ObjectId>;
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
const _makeEmptyMergedResult = (): MutableIndexingAccumulator => ({
|
|
467
|
+
updated: 0,
|
|
468
|
+
done: true,
|
|
469
|
+
spaces: new Set(),
|
|
470
|
+
queues: new Set(),
|
|
471
|
+
documents: new Set(),
|
|
472
|
+
types: new Set(),
|
|
473
|
+
objects: new Set(),
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
const _mergeInto = (acc: MutableIndexingAccumulator, r: IndexingResult): void => {
|
|
477
|
+
acc.updated += r.updated;
|
|
478
|
+
acc.done = acc.done && r.done;
|
|
479
|
+
for (const s of r.spaces) {
|
|
480
|
+
acc.spaces.add(s);
|
|
481
|
+
}
|
|
482
|
+
for (const q of r.queues) {
|
|
483
|
+
acc.queues.add(q);
|
|
484
|
+
}
|
|
485
|
+
for (const d of r.documents) {
|
|
486
|
+
acc.documents.add(d);
|
|
487
|
+
}
|
|
488
|
+
for (const t of r.types) {
|
|
489
|
+
acc.types.add(t);
|
|
490
|
+
}
|
|
491
|
+
for (const o of r.objects) {
|
|
492
|
+
acc.objects.add(o);
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
|
|
302
496
|
export type EchoStatsDiagnostic = {
|
|
303
497
|
loadedDocsCount: number;
|
|
304
498
|
dataStats: EchoDataStats;
|
|
305
499
|
};
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Sqlite health check on startup.
|
|
503
|
+
*
|
|
504
|
+
* Always runs a cheap schema-level probe (`PRAGMA schema_version` + a read from
|
|
505
|
+
* `sqlite_schema`). These do not walk user data, but they fail fast if the DB
|
|
506
|
+
* file header or schema root page is unreadable.
|
|
507
|
+
*
|
|
508
|
+
* Additionally runs `PRAGMA quick_check` when `RUN_SQLITE_QUICK_CHECK_ON_STARTUP`
|
|
509
|
+
* is enabled. That walks every page and is O(dbSize) — slow on OPFS.
|
|
510
|
+
*/
|
|
511
|
+
const testSqlite = () =>
|
|
512
|
+
Effect.gen(function* () {
|
|
513
|
+
log('begin sqlite health check');
|
|
514
|
+
const sql = yield* SqlClient.SqlClient;
|
|
515
|
+
const databases = yield* sql<{ seq: number; name: string; file: string }>`PRAGMA database_list`;
|
|
516
|
+
log('SQLite databases', { databases });
|
|
517
|
+
|
|
518
|
+
// Cheap header/schema probe: forces SQLite to read the file header and
|
|
519
|
+
// schema root page. Catches catastrophic corruption (bad header,
|
|
520
|
+
// unreadable schema) without walking user data.
|
|
521
|
+
const [schemaVersion] = yield* sql<{ schema_version: number }>`PRAGMA schema_version`;
|
|
522
|
+
const [schemaCount] = yield* sql<{ n: number }>`SELECT count(*) AS n FROM sqlite_schema`;
|
|
523
|
+
log('sqlite schema probe passed', {
|
|
524
|
+
schemaVersion: schemaVersion.schema_version,
|
|
525
|
+
objectCount: schemaCount.n,
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
if (RUN_SQLITE_QUICK_CHECK_ON_STARTUP) {
|
|
529
|
+
// NOTE: This is slow on non-trivial databases: O(dbSize).
|
|
530
|
+
log('starting sqlite quick_check');
|
|
531
|
+
const [result] = yield* sql<{ quick_check: string }>`PRAGMA quick_check`;
|
|
532
|
+
if (result.quick_check !== 'ok') {
|
|
533
|
+
throw new Error('SQLite quick check failed');
|
|
534
|
+
}
|
|
535
|
+
log('sqlite quick_check passed');
|
|
536
|
+
} else {
|
|
537
|
+
log('sqlite quick_check skipped');
|
|
538
|
+
}
|
|
539
|
+
log('sqlite health check complete');
|
|
540
|
+
});
|
package/src/db-host/index.ts
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
export * from './automerge-data-source';
|
|
5
6
|
export * from './data-service';
|
|
6
7
|
export * from './documents-synchronizer';
|
|
7
8
|
export * from './echo-host';
|
|
8
9
|
export * from './database-root';
|
|
10
|
+
export * from './invalidation-hint';
|
|
9
11
|
export * from './query-service';
|
|
10
12
|
export * from './space-state-manager';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type { IndexingResult } from '@dxos/index-core';
|
|
6
|
+
import type { ObjectId, SpaceId } from '@dxos/keys';
|
|
7
|
+
|
|
8
|
+
export type InvalidationHint = {
|
|
9
|
+
spaceIds?: ReadonlySet<SpaceId>;
|
|
10
|
+
queueIds?: ReadonlySet<ObjectId>;
|
|
11
|
+
typenames?: ReadonlySet<string>;
|
|
12
|
+
objectIds?: ReadonlySet<ObjectId>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Converts an IndexingResult to an InvalidationHint.
|
|
17
|
+
* Returns undefined when nothing was indexed (no need to invalidate).
|
|
18
|
+
*/
|
|
19
|
+
export const hintFromIndexingResult = (r: IndexingResult): InvalidationHint | undefined => {
|
|
20
|
+
if (r.updated === 0) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
spaceIds: r.spaces.size > 0 ? r.spaces : undefined,
|
|
25
|
+
queueIds: r.queues.size > 0 ? r.queues : undefined,
|
|
26
|
+
typenames: r.types.size > 0 ? r.types : undefined,
|
|
27
|
+
objectIds: r.objects.size > 0 ? r.objects : undefined,
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Merges two hints for the same tick.
|
|
33
|
+
* If either side leaves a dimension unconstrained (undefined), the merged result is also unconstrained on that dimension.
|
|
34
|
+
*/
|
|
35
|
+
export const mergeHints = (a: InvalidationHint, b: InvalidationHint): InvalidationHint => ({
|
|
36
|
+
spaceIds: unionOrUndefined(a.spaceIds, b.spaceIds),
|
|
37
|
+
queueIds: unionOrUndefined(a.queueIds, b.queueIds),
|
|
38
|
+
typenames: unionOrUndefined(a.typenames, b.typenames),
|
|
39
|
+
objectIds: unionOrUndefined(a.objectIds, b.objectIds),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const unionOrUndefined = <T>(
|
|
43
|
+
a: ReadonlySet<T> | undefined,
|
|
44
|
+
b: ReadonlySet<T> | undefined,
|
|
45
|
+
): ReadonlySet<T> | undefined => {
|
|
46
|
+
if (a === undefined || b === undefined) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
return new Set<T>([...a, ...b]);
|
|
50
|
+
};
|