@dxos/echo-pipeline 0.8.4-main.a4bbb77 → 0.8.4-main.ae835ea
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 → chunk-FR6GJ4S4.mjs} +339 -425
- package/dist/lib/{node-esm/chunk-CHMJJ4DG.mjs.map → browser/chunk-FR6GJ4S4.mjs.map} +2 -2
- package/dist/lib/browser/{chunk-BVR7OOZK.mjs → chunk-WJJXJTNS.mjs} +7 -18
- package/dist/lib/browser/chunk-WJJXJTNS.mjs.map +7 -0
- package/dist/lib/browser/filter/index.mjs +1 -1
- package/dist/lib/browser/index.mjs +682 -750
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +65 -106
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/{chunk-CHMJJ4DG.mjs → chunk-JQCCVDLT.mjs} +339 -425
- package/dist/lib/{browser/chunk-XGG76KKU.mjs.map → node-esm/chunk-JQCCVDLT.mjs.map} +2 -2
- package/dist/lib/node-esm/{chunk-L3ZEKVDR.mjs → chunk-LEQ77KAT.mjs} +7 -18
- package/dist/lib/node-esm/chunk-LEQ77KAT.mjs.map +7 -0
- package/dist/lib/node-esm/filter/index.mjs +1 -1
- package/dist/lib/node-esm/index.mjs +682 -750
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +65 -106
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
- package/dist/types/src/automerge/collection-synchronizer.d.ts +2 -1
- package/dist/types/src/automerge/collection-synchronizer.d.ts.map +1 -1
- package/dist/types/src/db-host/query-service.d.ts.map +1 -1
- package/dist/types/src/edge/echo-edge-replicator.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/query/query-executor.d.ts +9 -0
- package/dist/types/src/query/query-executor.d.ts.map +1 -1
- package/dist/types/src/testing/test-data.d.ts.map +1 -1
- package/dist/types/src/testing/test-schema.d.ts +1 -1
- package/dist/types/src/testing/test-schema.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +35 -36
- package/src/automerge/automerge-host.ts +8 -5
- package/src/automerge/collection-synchronizer.test.ts +4 -3
- package/src/automerge/collection-synchronizer.ts +14 -9
- package/src/db-host/query-service.ts +2 -1
- package/src/edge/echo-edge-replicator.ts +30 -9
- package/src/filter/filter-match.test.ts +1 -1
- package/src/filter/filter-match.ts +6 -19
- package/src/query/query-executor.ts +116 -36
- package/src/testing/test-data.ts +1 -1
- package/src/testing/test-schema.ts +1 -1
- package/dist/lib/browser/chunk-BVR7OOZK.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-L3ZEKVDR.mjs.map +0 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/echo-pipeline",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.ae835ea",
|
|
4
4
|
"description": "ECHO database.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -44,50 +44,49 @@
|
|
|
44
44
|
],
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@automerge/automerge": "3.1.2",
|
|
47
|
-
"@automerge/automerge-repo": "2.
|
|
47
|
+
"@automerge/automerge-repo": "2.4.0",
|
|
48
48
|
"crc-32": "^1.2.2",
|
|
49
49
|
"effect": "3.18.3",
|
|
50
50
|
"level-transcoder": "^1.0.1",
|
|
51
51
|
"lodash.isequal": "^4.5.0",
|
|
52
|
-
"@dxos/
|
|
53
|
-
"@dxos/
|
|
54
|
-
"@dxos/
|
|
55
|
-
"@dxos/
|
|
56
|
-
"@dxos/
|
|
57
|
-
"@dxos/
|
|
58
|
-
"@dxos/echo": "0.8.4-main.
|
|
59
|
-
"@dxos/echo
|
|
60
|
-
"@dxos/
|
|
61
|
-
"@dxos/
|
|
62
|
-
"@dxos/
|
|
63
|
-
"@dxos/
|
|
64
|
-
"@dxos/
|
|
65
|
-
"@dxos/
|
|
66
|
-
"@dxos/
|
|
67
|
-
"@dxos/
|
|
68
|
-
"@dxos/
|
|
69
|
-
"@dxos/
|
|
70
|
-
"@dxos/
|
|
71
|
-
"@dxos/
|
|
72
|
-
"@dxos/protocols": "0.8.4-main.
|
|
73
|
-
"@dxos/
|
|
74
|
-
"@dxos/
|
|
75
|
-
"@dxos/teleport-extension-automerge-replicator": "0.8.4-main.
|
|
76
|
-
"@dxos/
|
|
77
|
-
"@dxos/teleport-extension-
|
|
78
|
-
"@dxos/
|
|
79
|
-
"@dxos/teleport
|
|
80
|
-
"@dxos/
|
|
81
|
-
"@dxos/
|
|
82
|
-
"@dxos/typings": "0.8.4-main.
|
|
83
|
-
"@dxos/util": "0.8.4-main.
|
|
84
|
-
"@dxos/teleport": "0.8.4-main.a4bbb77"
|
|
52
|
+
"@dxos/codec-protobuf": "0.8.4-main.ae835ea",
|
|
53
|
+
"@dxos/async": "0.8.4-main.ae835ea",
|
|
54
|
+
"@dxos/context": "0.8.4-main.ae835ea",
|
|
55
|
+
"@dxos/credentials": "0.8.4-main.ae835ea",
|
|
56
|
+
"@dxos/crypto": "0.8.4-main.ae835ea",
|
|
57
|
+
"@dxos/debug": "0.8.4-main.ae835ea",
|
|
58
|
+
"@dxos/echo-protocol": "0.8.4-main.ae835ea",
|
|
59
|
+
"@dxos/echo": "0.8.4-main.ae835ea",
|
|
60
|
+
"@dxos/edge-client": "0.8.4-main.ae835ea",
|
|
61
|
+
"@dxos/errors": "0.8.4-main.ae835ea",
|
|
62
|
+
"@dxos/feed-store": "0.8.4-main.ae835ea",
|
|
63
|
+
"@dxos/hypercore": "0.8.4-main.ae835ea",
|
|
64
|
+
"@dxos/invariant": "0.8.4-main.ae835ea",
|
|
65
|
+
"@dxos/keyring": "0.8.4-main.ae835ea",
|
|
66
|
+
"@dxos/keys": "0.8.4-main.ae835ea",
|
|
67
|
+
"@dxos/kv-store": "0.8.4-main.ae835ea",
|
|
68
|
+
"@dxos/indexing": "0.8.4-main.ae835ea",
|
|
69
|
+
"@dxos/log": "0.8.4-main.ae835ea",
|
|
70
|
+
"@dxos/messaging": "0.8.4-main.ae835ea",
|
|
71
|
+
"@dxos/network-manager": "0.8.4-main.ae835ea",
|
|
72
|
+
"@dxos/protocols": "0.8.4-main.ae835ea",
|
|
73
|
+
"@dxos/node-std": "0.8.4-main.ae835ea",
|
|
74
|
+
"@dxos/random-access-storage": "0.8.4-main.ae835ea",
|
|
75
|
+
"@dxos/teleport-extension-automerge-replicator": "0.8.4-main.ae835ea",
|
|
76
|
+
"@dxos/teleport-extension-gossip": "0.8.4-main.ae835ea",
|
|
77
|
+
"@dxos/teleport-extension-object-sync": "0.8.4-main.ae835ea",
|
|
78
|
+
"@dxos/timeframe": "0.8.4-main.ae835ea",
|
|
79
|
+
"@dxos/teleport": "0.8.4-main.ae835ea",
|
|
80
|
+
"@dxos/tracing": "0.8.4-main.ae835ea",
|
|
81
|
+
"@dxos/teleport-extension-replicator": "0.8.4-main.ae835ea",
|
|
82
|
+
"@dxos/typings": "0.8.4-main.ae835ea",
|
|
83
|
+
"@dxos/util": "0.8.4-main.ae835ea"
|
|
85
84
|
},
|
|
86
85
|
"devDependencies": {
|
|
87
86
|
"@types/lodash.isequal": "^4.5.0",
|
|
88
87
|
"fast-check": "^3.19.0",
|
|
89
88
|
"get-port-please": "^3.1.1",
|
|
90
|
-
"@dxos/test-utils": "0.8.4-main.
|
|
89
|
+
"@dxos/test-utils": "0.8.4-main.ae835ea"
|
|
91
90
|
},
|
|
92
91
|
"publishConfig": {
|
|
93
92
|
"access": "public"
|
|
@@ -378,10 +378,6 @@ export class AutomergeHost extends Resource {
|
|
|
378
378
|
// NOTE: If both peers return sharePolicy=false the replication will not happen
|
|
379
379
|
// https://github.com/automerge/automerge-repo/pull/292
|
|
380
380
|
private async _sharePolicy(peerId: PeerId, documentId?: DocumentId): Promise<boolean> {
|
|
381
|
-
if (peerId.startsWith('client-')) {
|
|
382
|
-
return false; // Only send docs to clients if they are requested.
|
|
383
|
-
}
|
|
384
|
-
|
|
385
381
|
if (!documentId) {
|
|
386
382
|
return false;
|
|
387
383
|
}
|
|
@@ -505,6 +501,10 @@ export class AutomergeHost extends Resource {
|
|
|
505
501
|
},
|
|
506
502
|
);
|
|
507
503
|
await this._repo.flush(loadedDocuments);
|
|
504
|
+
|
|
505
|
+
// Ensure that document verions have propagated accross the system.
|
|
506
|
+
// This is important for the case where we are doing flush and then waiting for sync to happen.
|
|
507
|
+
await this._onHeadsChangedTask?.runBlocking();
|
|
508
508
|
}
|
|
509
509
|
|
|
510
510
|
async getHeads(documentIds: DocumentId[]): Promise<(Heads | undefined)[]> {
|
|
@@ -795,10 +795,13 @@ export class AutomergeHost extends Resource {
|
|
|
795
795
|
|
|
796
796
|
for (const collectionId of this._collectionSynchronizer.getRegisteredCollectionIds()) {
|
|
797
797
|
const state = this._collectionSynchronizer.getLocalCollectionState(collectionId);
|
|
798
|
+
if (!state) {
|
|
799
|
+
continue;
|
|
800
|
+
}
|
|
798
801
|
let newState: CollectionState | undefined;
|
|
799
802
|
|
|
800
803
|
for (const [documentId, heads] of docHeads) {
|
|
801
|
-
if (state
|
|
804
|
+
if (documentId in state.documents) {
|
|
802
805
|
if (!newState) {
|
|
803
806
|
newState = structuredClone(state);
|
|
804
807
|
}
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import type
|
|
5
|
+
import { type Heads } from '@automerge/automerge';
|
|
6
|
+
import type { DocumentId, PeerId } from '@automerge/automerge-repo';
|
|
6
7
|
import { describe, expect, onTestFinished, test } from 'vitest';
|
|
7
8
|
|
|
8
9
|
import { sleep } from '@dxos/async';
|
|
@@ -84,7 +85,7 @@ const STATE_1: CollectionState = {
|
|
|
84
85
|
a: ['1'],
|
|
85
86
|
b: ['2'],
|
|
86
87
|
c: ['3'],
|
|
87
|
-
},
|
|
88
|
+
} as Record<DocumentId, Heads>,
|
|
88
89
|
};
|
|
89
90
|
|
|
90
91
|
const STATE_2: CollectionState = {
|
|
@@ -92,5 +93,5 @@ const STATE_2: CollectionState = {
|
|
|
92
93
|
a: ['1'],
|
|
93
94
|
b: ['4'],
|
|
94
95
|
d: ['3'],
|
|
95
|
-
},
|
|
96
|
+
} as Record<DocumentId, Heads>,
|
|
96
97
|
};
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { next as
|
|
5
|
+
import { next as A, type Heads } from '@automerge/automerge';
|
|
6
6
|
import type { DocumentId, PeerId } from '@automerge/automerge-repo';
|
|
7
|
+
import * as Array from 'effect/Array';
|
|
8
|
+
import * as Record from 'effect/Record';
|
|
7
9
|
|
|
8
10
|
import { Event, asyncReturn, scheduleTask, scheduleTaskInterval } from '@dxos/async';
|
|
9
11
|
import { type Context, Resource } from '@dxos/context';
|
|
@@ -223,7 +225,7 @@ export type CollectionState = {
|
|
|
223
225
|
/**
|
|
224
226
|
* DocumentId -> Heads.
|
|
225
227
|
*/
|
|
226
|
-
documents: Record<
|
|
228
|
+
documents: Record<DocumentId, Heads>;
|
|
227
229
|
};
|
|
228
230
|
|
|
229
231
|
export type CollectionStateDiff = {
|
|
@@ -233,18 +235,21 @@ export type CollectionStateDiff = {
|
|
|
233
235
|
};
|
|
234
236
|
|
|
235
237
|
export const diffCollectionState = (local: CollectionState, remote: CollectionState): CollectionStateDiff => {
|
|
236
|
-
const
|
|
238
|
+
const localDocuments = Record.filter(local.documents, (heads) => heads.length > 0);
|
|
239
|
+
const remoteDocuments = Record.filter(remote.documents, (heads) => heads.length > 0);
|
|
240
|
+
// NOTE: Using `Array.union` is slow.
|
|
241
|
+
const allDocuments = [...new Set([...Record.keys(localDocuments), ...Record.keys(remoteDocuments)])] as DocumentId[];
|
|
237
242
|
|
|
238
243
|
const missingOnRemote: DocumentId[] = [];
|
|
239
244
|
const missingOnLocal: DocumentId[] = [];
|
|
240
245
|
const different: DocumentId[] = [];
|
|
241
246
|
for (const documentId of allDocuments) {
|
|
242
|
-
if (!
|
|
243
|
-
missingOnLocal.push(documentId
|
|
244
|
-
} else if (!
|
|
245
|
-
missingOnRemote.push(documentId
|
|
246
|
-
} else if (!
|
|
247
|
-
different.push(documentId
|
|
247
|
+
if (!localDocuments[documentId]) {
|
|
248
|
+
missingOnLocal.push(documentId);
|
|
249
|
+
} else if (!remoteDocuments[documentId]) {
|
|
250
|
+
missingOnRemote.push(documentId);
|
|
251
|
+
} else if (!A.equals(local.documents[documentId], remote.documents[documentId])) {
|
|
252
|
+
different.push(documentId);
|
|
248
253
|
}
|
|
249
254
|
}
|
|
250
255
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { getHeads } from '@automerge/automerge';
|
|
6
6
|
import { type DocHandle, type DocumentId } from '@automerge/automerge-repo';
|
|
7
|
-
import
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
8
8
|
|
|
9
9
|
import { DeferredTask, scheduleMicroTask, synchronized } from '@dxos/async';
|
|
10
10
|
import { Stream } from '@dxos/codec-protobuf/stream';
|
|
@@ -56,6 +56,7 @@ type ActiveQuery = {
|
|
|
56
56
|
|
|
57
57
|
@trace.resource()
|
|
58
58
|
export class QueryServiceImpl extends Resource implements QueryService {
|
|
59
|
+
// TODO(dmaretskyi): We need to implement query deduping. Idle composer has 80 queries with only 10 being unique.
|
|
59
60
|
private readonly _queries = new Set<ActiveQuery>();
|
|
60
61
|
|
|
61
62
|
private _updateQueries!: DeferredTask;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import * as Automerge from '@automerge/automerge';
|
|
5
6
|
import { type DocumentId, type Heads, cbor } from '@automerge/automerge-repo';
|
|
6
7
|
|
|
7
8
|
import { Mutex, scheduleMicroTask, scheduleTask } from '@dxos/async';
|
|
@@ -25,7 +26,7 @@ import {
|
|
|
25
26
|
type Message as RouterMessage,
|
|
26
27
|
MessageSchema as RouterMessageSchema,
|
|
27
28
|
} from '@dxos/protocols/buf/dxos/edge/messenger_pb';
|
|
28
|
-
import { bufferToArray } from '@dxos/util';
|
|
29
|
+
import { bufferToArray, setDeep } from '@dxos/util';
|
|
29
30
|
|
|
30
31
|
import {
|
|
31
32
|
type EchoReplicator,
|
|
@@ -204,6 +205,7 @@ const MAX_INFLIGHT_REQUESTS = 5;
|
|
|
204
205
|
const MAX_RATE_LIMIT_WAIT_TIME_MS = 3000;
|
|
205
206
|
|
|
206
207
|
class EdgeReplicatorConnection extends Resource implements ReplicatorConnection {
|
|
208
|
+
private readonly _connectionId = randomUUID();
|
|
207
209
|
private readonly _edgeConnection: EdgeConnection;
|
|
208
210
|
private readonly _edgeHttpClient: EdgeHttpClient;
|
|
209
211
|
private readonly _remotePeerId: string | null = null;
|
|
@@ -214,6 +216,7 @@ class EdgeReplicatorConnection extends Resource implements ReplicatorConnection
|
|
|
214
216
|
private readonly _onRemoteConnected: () => Promise<void>;
|
|
215
217
|
private readonly _onRemoteDisconnected: () => Promise<void>;
|
|
216
218
|
private readonly _onRestartRequested: () => void;
|
|
219
|
+
private _sequence = 0;
|
|
217
220
|
|
|
218
221
|
private _requestLimiter = new InflightRequestLimiter({
|
|
219
222
|
maxInflightRequests: MAX_INFLIGHT_REQUESTS,
|
|
@@ -244,7 +247,7 @@ class EdgeReplicatorConnection extends Resource implements ReplicatorConnection
|
|
|
244
247
|
// This way automerge-repo will have separate sync states for every connection.
|
|
245
248
|
// This is important because the previous connection might have had some messages that failed to deliver
|
|
246
249
|
// abd if we don't clear the sync-state, automerge will not attempt to deliver them again.
|
|
247
|
-
this._remotePeerId = `${EdgeService.AUTOMERGE_REPLICATOR}:${spaceId}-${
|
|
250
|
+
this._remotePeerId = `${EdgeService.AUTOMERGE_REPLICATOR}:${spaceId}-${this._connectionId}`;
|
|
248
251
|
this._targetServiceId = `${EdgeService.AUTOMERGE_REPLICATOR}:${spaceId}`;
|
|
249
252
|
this._sharedPolicyEnabled = sharedPolicyEnabled;
|
|
250
253
|
this._onRemoteConnected = onRemoteConnected;
|
|
@@ -349,8 +352,7 @@ class EdgeReplicatorConnection extends Resource implements ReplicatorConnection
|
|
|
349
352
|
|
|
350
353
|
const payload = cbor.decode(message.payload!.value) as AutomergeProtocolMessage;
|
|
351
354
|
log.verbose('received', {
|
|
352
|
-
|
|
353
|
-
documentId: payload.type === 'sync' && payload.documentId,
|
|
355
|
+
...getMessageInfo(payload),
|
|
354
356
|
remoteId: this._remotePeerId,
|
|
355
357
|
});
|
|
356
358
|
|
|
@@ -384,7 +386,7 @@ class EdgeReplicatorConnection extends Resource implements ReplicatorConnection
|
|
|
384
386
|
// There's a race between the credentials being replicated that are needed for access control and the data replication.
|
|
385
387
|
// AutomergeReplicator might return a Forbidden error if the credentials are not yet replicated.
|
|
386
388
|
// We restart the connection with some delay to account for that.
|
|
387
|
-
if (
|
|
389
|
+
if (isErrorMessage(message)) {
|
|
388
390
|
this._onRestartRequested();
|
|
389
391
|
return;
|
|
390
392
|
}
|
|
@@ -398,9 +400,12 @@ class EdgeReplicatorConnection extends Resource implements ReplicatorConnection
|
|
|
398
400
|
// Fix the peer id.
|
|
399
401
|
(message as any).targetId = this._targetServiceId as PeerId;
|
|
400
402
|
|
|
403
|
+
// Note: This is used on EDGE to detect out-of-order messages per connection.
|
|
404
|
+
setDeep(message, ['metadata', 'dxos_sequence'], this._getSequence());
|
|
405
|
+
setDeep(message, ['metadata', 'dxos_connectionId'], this._connectionId);
|
|
406
|
+
|
|
401
407
|
log.verbose('sending...', {
|
|
402
|
-
|
|
403
|
-
documentId: message.type === 'sync' && message.documentId,
|
|
408
|
+
...getMessageInfo(message),
|
|
404
409
|
remoteId: this._remotePeerId,
|
|
405
410
|
});
|
|
406
411
|
|
|
@@ -421,10 +426,26 @@ class EdgeReplicatorConnection extends Resource implements ReplicatorConnection
|
|
|
421
426
|
log.error('failed to send message', { err });
|
|
422
427
|
}
|
|
423
428
|
}
|
|
429
|
+
|
|
430
|
+
private _getSequence(): number {
|
|
431
|
+
return this._sequence++;
|
|
432
|
+
}
|
|
424
433
|
}
|
|
425
434
|
|
|
426
435
|
/**
|
|
427
436
|
* This message is sent by EDGE AutomergeReplicator when the authorization is denied.
|
|
428
437
|
*/
|
|
429
|
-
const
|
|
430
|
-
|
|
438
|
+
const isErrorMessage = (message: AutomergeProtocolMessage) => message.type === 'error';
|
|
439
|
+
|
|
440
|
+
const getMessageInfo = (msg: AutomergeProtocolMessage) => {
|
|
441
|
+
const { have, heads, need, changes } = msg.type === 'sync' ? Automerge.decodeSyncMessage(msg.data) : {};
|
|
442
|
+
return {
|
|
443
|
+
type: msg.type,
|
|
444
|
+
documentId: 'documentId' in msg ? msg.documentId : undefined,
|
|
445
|
+
have,
|
|
446
|
+
heads,
|
|
447
|
+
need,
|
|
448
|
+
changes: changes?.length,
|
|
449
|
+
sequence: msg.metadata?.dxos_sequence,
|
|
450
|
+
};
|
|
451
|
+
};
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
import { describe, expect, test } from 'vitest';
|
|
6
6
|
|
|
7
7
|
import { Filter } from '@dxos/echo';
|
|
8
|
+
import { EXPANDO_TYPENAME, Expando, Ref } from '@dxos/echo/internal';
|
|
8
9
|
import { ObjectStructure } from '@dxos/echo-protocol';
|
|
9
|
-
import { EXPANDO_TYPENAME, Expando, Ref } from '@dxos/echo-schema';
|
|
10
10
|
import { DXN, ObjectId, SpaceId } from '@dxos/keys';
|
|
11
11
|
|
|
12
12
|
import { type MatchedObject, filterMatchObject } from './filter-match';
|
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { ATTR_META, EXPANDO_TYPENAME, type ObjectJSON } from '@dxos/echo/internal';
|
|
6
|
+
import { ObjectStructure, type QueryAST, decodeReference, isEncodedReference } from '@dxos/echo-protocol';
|
|
7
7
|
import { DXN, type ObjectId, type SpaceId } from '@dxos/keys';
|
|
8
|
-
import { getDeep } from '@dxos/util';
|
|
9
8
|
|
|
10
9
|
export type MatchedObject = {
|
|
11
10
|
id: ObjectId;
|
|
@@ -67,14 +66,8 @@ export const filterMatchObject = (filter: QueryAST.Filter, obj: MatchedObject):
|
|
|
67
66
|
}
|
|
68
67
|
|
|
69
68
|
case 'tag': {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return (
|
|
73
|
-
Array.isArray(tags) &&
|
|
74
|
-
tags?.some((tag) => {
|
|
75
|
-
return typeof tag === 'object' && tag.label === filter.tag;
|
|
76
|
-
})
|
|
77
|
-
);
|
|
69
|
+
const tags = ObjectStructure.getTags(obj.doc);
|
|
70
|
+
return tags.some((tag) => tag === filter.tag);
|
|
78
71
|
}
|
|
79
72
|
|
|
80
73
|
case 'text-search': {
|
|
@@ -154,14 +147,8 @@ export const filterMatchObjectJSON = (filter: QueryAST.Filter, obj: ObjectJSON):
|
|
|
154
147
|
}
|
|
155
148
|
|
|
156
149
|
case 'tag': {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
return (
|
|
160
|
-
Array.isArray(tags) &&
|
|
161
|
-
tags?.some((tag) => {
|
|
162
|
-
return typeof tag === 'object' && tag.label === filter.tag;
|
|
163
|
-
})
|
|
164
|
-
);
|
|
150
|
+
const tags = obj[ATTR_META]?.tags ?? [];
|
|
151
|
+
return tags.some((tag) => tag === filter.tag);
|
|
165
152
|
}
|
|
166
153
|
|
|
167
154
|
case 'text-search': {
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import type { AutomergeUrl, DocumentId } from '@automerge/automerge-repo';
|
|
6
|
-
import
|
|
6
|
+
import * as Match from 'effect/Match';
|
|
7
|
+
import * as Predicate from 'effect/Predicate';
|
|
7
8
|
|
|
8
9
|
import { Context, ContextDisposedError, LifecycleState, Resource } from '@dxos/context';
|
|
9
10
|
import { DatabaseDirectory, ObjectStructure, type QueryAST, isEncodedReference } from '@dxos/echo-protocol';
|
|
@@ -63,6 +64,9 @@ export type ExecutionTrace = {
|
|
|
63
64
|
documentsLoaded: number;
|
|
64
65
|
indexHits: number;
|
|
65
66
|
|
|
67
|
+
beginTs: number;
|
|
68
|
+
endTs: number;
|
|
69
|
+
|
|
66
70
|
executionTime: number;
|
|
67
71
|
indexQueryTime: number;
|
|
68
72
|
documentLoadTime: number;
|
|
@@ -77,11 +81,42 @@ export const ExecutionTrace = Object.freeze({
|
|
|
77
81
|
objectCount: 0,
|
|
78
82
|
documentsLoaded: 0,
|
|
79
83
|
indexHits: 0,
|
|
84
|
+
beginTs: 0,
|
|
85
|
+
endTs: 0,
|
|
80
86
|
indexQueryTime: 0,
|
|
81
87
|
documentLoadTime: 0,
|
|
82
88
|
executionTime: 0,
|
|
83
89
|
children: [],
|
|
84
90
|
}),
|
|
91
|
+
markEnd: (trace: ExecutionTrace) => {
|
|
92
|
+
trace.endTs = performance.now();
|
|
93
|
+
trace.executionTime = trace.endTs - trace.beginTs;
|
|
94
|
+
},
|
|
95
|
+
putOnPerformanceTimeline: (trace: ExecutionTrace) => {
|
|
96
|
+
performance.measure(trace.name, {
|
|
97
|
+
start: trace.beginTs,
|
|
98
|
+
end: trace.endTs,
|
|
99
|
+
detail: {
|
|
100
|
+
devtools: {
|
|
101
|
+
dataType: 'track-entry',
|
|
102
|
+
track: 'Query Execution',
|
|
103
|
+
trackGroup: 'ECHO', // Group related tracks together
|
|
104
|
+
color: 'tertiary-dark',
|
|
105
|
+
properties: [
|
|
106
|
+
['objectCount', trace.objectCount],
|
|
107
|
+
['documentsLoaded', trace.documentsLoaded],
|
|
108
|
+
['index hits', trace.indexHits],
|
|
109
|
+
['indexQueryTime', trace.indexQueryTime],
|
|
110
|
+
['documentLoadTime', trace.documentLoadTime],
|
|
111
|
+
],
|
|
112
|
+
tooltipText: trace.details,
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
for (const child of trace.children) {
|
|
117
|
+
ExecutionTrace.putOnPerformanceTimeline(child);
|
|
118
|
+
}
|
|
119
|
+
},
|
|
85
120
|
format: (trace: ExecutionTrace): string => {
|
|
86
121
|
const go = (trace: ExecutionTrace, indent: number): string => {
|
|
87
122
|
return [
|
|
@@ -191,6 +226,9 @@ export class QueryExecutor extends Resource {
|
|
|
191
226
|
workingSet[index].documentId !== item.documentId,
|
|
192
227
|
);
|
|
193
228
|
|
|
229
|
+
// Disabled because concurrent queries don't print hierarchies correctly.
|
|
230
|
+
// ExecutionTrace.putOnPerformanceTimeline(trace);
|
|
231
|
+
|
|
194
232
|
if (TRACE_QUERY_EXECUTION) {
|
|
195
233
|
console.log(ExecutionTrace.format(trace));
|
|
196
234
|
}
|
|
@@ -213,7 +251,7 @@ export class QueryExecutor extends Resource {
|
|
|
213
251
|
trace.children.push(result.trace);
|
|
214
252
|
}
|
|
215
253
|
trace.objectCount = workingSet.length;
|
|
216
|
-
|
|
254
|
+
ExecutionTrace.markEnd(trace);
|
|
217
255
|
return { workingSet, trace };
|
|
218
256
|
}
|
|
219
257
|
|
|
@@ -253,7 +291,8 @@ export class QueryExecutor extends Resource {
|
|
|
253
291
|
default:
|
|
254
292
|
throw new Error(`Unknown step type: ${(step as any)._tag}`);
|
|
255
293
|
}
|
|
256
|
-
trace.
|
|
294
|
+
trace.beginTs = begin;
|
|
295
|
+
ExecutionTrace.markEnd(trace);
|
|
257
296
|
|
|
258
297
|
return { workingSet: newWorkingSet, trace };
|
|
259
298
|
}
|
|
@@ -265,6 +304,7 @@ export class QueryExecutor extends Resource {
|
|
|
265
304
|
...ExecutionTrace.makeEmpty(),
|
|
266
305
|
name: 'Select',
|
|
267
306
|
details: JSON.stringify(step.selector),
|
|
307
|
+
beginTs: performance.now(),
|
|
268
308
|
};
|
|
269
309
|
|
|
270
310
|
switch (step.selector._tag) {
|
|
@@ -597,17 +637,7 @@ export class QueryExecutor extends Resource {
|
|
|
597
637
|
}
|
|
598
638
|
|
|
599
639
|
private async _execOrderStep(step: QueryPlan.OrderStep, workingSet: QueryItem[]): Promise<StepExecutionResult> {
|
|
600
|
-
const
|
|
601
|
-
step.order.reduce((comparison, order) => {
|
|
602
|
-
// If we already have a definitive result, return it
|
|
603
|
-
if (comparison !== 0) {
|
|
604
|
-
return comparison;
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
return this._compareByOrder(a, b, order);
|
|
608
|
-
}, 0);
|
|
609
|
-
|
|
610
|
-
const sortedWorkingSet = [...workingSet].sort(compareItems);
|
|
640
|
+
const sortedWorkingSet = [...workingSet].sort((a, b) => this._compareMultiOrder(a, b, step.order));
|
|
611
641
|
|
|
612
642
|
return {
|
|
613
643
|
workingSet: sortedWorkingSet,
|
|
@@ -620,34 +650,73 @@ export class QueryExecutor extends Resource {
|
|
|
620
650
|
};
|
|
621
651
|
}
|
|
622
652
|
|
|
653
|
+
private _compareMultiOrder(a: QueryItem, b: QueryItem, orders: readonly QueryAST.Order[]): number {
|
|
654
|
+
// Short circuit for common cases.
|
|
655
|
+
if (orders.length === 0) {
|
|
656
|
+
return 0;
|
|
657
|
+
} else if (orders.length === 1) {
|
|
658
|
+
return this._compareByOrder(a, b, orders[0]);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
for (const order of orders) {
|
|
662
|
+
const comparison = this._compareByOrder(a, b, order);
|
|
663
|
+
if (comparison !== 0) {
|
|
664
|
+
return comparison;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
return 0;
|
|
668
|
+
}
|
|
669
|
+
|
|
623
670
|
private _compareByOrder(a: QueryItem, b: QueryItem, order: QueryAST.Order): number {
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
const comparison = this._compareByProperty(a, b, property);
|
|
629
|
-
return direction === 'desc' ? -comparison : comparison;
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
|
|
671
|
+
switch (order.kind) {
|
|
672
|
+
case 'natural':
|
|
673
|
+
return a.objectId.localeCompare(b.objectId);
|
|
674
|
+
case 'property': {
|
|
675
|
+
const comparison = this._compareByProperty(a, b, order.property);
|
|
676
|
+
return order.direction === 'desc' ? -comparison : comparison;
|
|
677
|
+
}
|
|
678
|
+
default:
|
|
679
|
+
// Should never reach here with proper TypeScript types
|
|
680
|
+
return 0;
|
|
681
|
+
}
|
|
633
682
|
}
|
|
634
683
|
|
|
635
684
|
private _compareByProperty(a: QueryItem, b: QueryItem, property: string): number {
|
|
636
685
|
const aValue = a.doc.data[property];
|
|
637
686
|
const bValue = b.doc.data[property];
|
|
638
687
|
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
688
|
+
// Both null or undefined
|
|
689
|
+
if (aValue == null && bValue == null) {
|
|
690
|
+
return 0;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// Only a is null/undefined
|
|
694
|
+
if (aValue == null) {
|
|
695
|
+
return 1;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// Only b is null/undefined
|
|
699
|
+
if (bValue == null) {
|
|
700
|
+
return -1;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
// Both strings
|
|
704
|
+
if (typeof aValue === 'string' && typeof bValue === 'string') {
|
|
705
|
+
return aValue.localeCompare(bValue);
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
// Both numbers
|
|
709
|
+
if (typeof aValue === 'number' && typeof bValue === 'number') {
|
|
710
|
+
return aValue - bValue;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// Both booleans
|
|
714
|
+
if (typeof aValue === 'boolean' && typeof bValue === 'boolean') {
|
|
715
|
+
return aValue === bValue ? 0 : aValue ? 1 : -1;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
// Fallback: convert to strings and compare
|
|
719
|
+
return String(aValue).localeCompare(String(bValue));
|
|
651
720
|
}
|
|
652
721
|
|
|
653
722
|
private async _loadDocumentsAfterIndexQuery(indexHits: FindResult[]): Promise<(QueryItem | null)[]> {
|
|
@@ -658,6 +727,11 @@ export class QueryExecutor extends Resource {
|
|
|
658
727
|
);
|
|
659
728
|
}
|
|
660
729
|
|
|
730
|
+
/**
|
|
731
|
+
* Space key hex -> SpaceId.
|
|
732
|
+
*/
|
|
733
|
+
private readonly _spaceIdCache = new Map<string, SpaceId>();
|
|
734
|
+
|
|
661
735
|
private async _loadFromIndexHit(hit: FindResult): Promise<QueryItem | null> {
|
|
662
736
|
const { objectId, documentId, spaceKey: spaceKeyInIndex } = objectPointerCodec.decode(hit.id);
|
|
663
737
|
|
|
@@ -672,6 +746,12 @@ export class QueryExecutor extends Resource {
|
|
|
672
746
|
return null;
|
|
673
747
|
}
|
|
674
748
|
|
|
749
|
+
let spaceId = this._spaceIdCache.get(spaceKey);
|
|
750
|
+
if (!spaceId) {
|
|
751
|
+
spaceId = await createIdFromSpaceKey(PublicKey.from(spaceKey));
|
|
752
|
+
this._spaceIdCache.set(spaceKey, spaceId);
|
|
753
|
+
}
|
|
754
|
+
|
|
675
755
|
const object = DatabaseDirectory.getInlineObject(doc, objectId);
|
|
676
756
|
if (!object) {
|
|
677
757
|
return null;
|
|
@@ -680,7 +760,7 @@ export class QueryExecutor extends Resource {
|
|
|
680
760
|
return {
|
|
681
761
|
objectId,
|
|
682
762
|
documentId: documentId as DocumentId,
|
|
683
|
-
spaceId
|
|
763
|
+
spaceId,
|
|
684
764
|
doc: object,
|
|
685
765
|
};
|
|
686
766
|
}
|
package/src/testing/test-data.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { getSchemaDXN } from '@dxos/echo/internal';
|
|
5
6
|
import { DatabaseDirectory, ObjectStructure } from '@dxos/echo-protocol';
|
|
6
|
-
import { getSchemaDXN } from '@dxos/echo-schema';
|
|
7
7
|
import { DXN, ObjectId, PublicKey } from '@dxos/keys';
|
|
8
8
|
|
|
9
9
|
import * as TestSchema from './test-schema';
|