@dxos/echo-pipeline 0.8.4-main.84f28bd → 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-GBFX5J5B.mjs → chunk-FR6GJ4S4.mjs} +239 -162
- package/dist/lib/browser/chunk-FR6GJ4S4.mjs.map +7 -0
- package/dist/lib/browser/chunk-WJJXJTNS.mjs +240 -0
- package/dist/lib/browser/chunk-WJJXJTNS.mjs.map +7 -0
- package/dist/lib/browser/filter/index.mjs +3 -1
- package/dist/lib/browser/index.mjs +1094 -345
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +36 -14
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/{chunk-FQFKWA3X.mjs → chunk-JQCCVDLT.mjs} +239 -162
- package/dist/lib/node-esm/chunk-JQCCVDLT.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-LEQ77KAT.mjs +240 -0
- package/dist/lib/node-esm/chunk-LEQ77KAT.mjs.map +7 -0
- package/dist/lib/node-esm/filter/index.mjs +3 -1
- package/dist/lib/node-esm/index.mjs +1094 -345
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +36 -14
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/automerge/automerge-host.d.ts +15 -28
- package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
- package/dist/types/src/automerge/collection-synchronizer.d.ts +3 -2
- package/dist/types/src/automerge/collection-synchronizer.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-network-adapter.d.ts +8 -1
- package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-replicator.d.ts +21 -2
- package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
- package/dist/types/src/automerge/index.d.ts +1 -1
- package/dist/types/src/automerge/index.d.ts.map +1 -1
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts +1 -1
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts +1 -0
- package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
- package/dist/types/src/common/codec.d.ts +1 -1
- package/dist/types/src/common/codec.d.ts.map +1 -1
- package/dist/types/src/db-host/data-service.d.ts +2 -2
- 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 +2 -2
- package/dist/types/src/db-host/documents-synchronizer.d.ts.map +1 -1
- package/dist/types/src/db-host/echo-host.d.ts +2 -2
- package/dist/types/src/db-host/echo-host.d.ts.map +1 -1
- package/dist/types/src/db-host/query-service.d.ts +1 -1
- package/dist/types/src/db-host/query-service.d.ts.map +1 -1
- package/dist/types/src/db-host/space-state-manager.d.ts +1 -1
- package/dist/types/src/db-host/space-state-manager.d.ts.map +1 -1
- package/dist/types/src/edge/echo-edge-replicator.d.ts +4 -2
- package/dist/types/src/edge/echo-edge-replicator.d.ts.map +1 -1
- package/dist/types/src/filter/filter-match.d.ts +4 -1
- package/dist/types/src/filter/filter-match.d.ts.map +1 -1
- package/dist/types/src/metadata/metadata-store.d.ts +1 -1
- package/dist/types/src/metadata/metadata-store.d.ts.map +1 -1
- package/dist/types/src/pipeline/pipeline.d.ts +1 -1
- package/dist/types/src/pipeline/pipeline.d.ts.map +1 -1
- package/dist/types/src/query/errors.d.ts +24 -8
- package/dist/types/src/query/errors.d.ts.map +1 -1
- package/dist/types/src/query/plan.d.ts +8 -1
- package/dist/types/src/query/plan.d.ts.map +1 -1
- package/dist/types/src/query/query-executor.d.ts +13 -1
- package/dist/types/src/query/query-executor.d.ts.map +1 -1
- package/dist/types/src/query/query-planner.d.ts +2 -0
- 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/control-pipeline.d.ts +1 -1
- package/dist/types/src/space/control-pipeline.d.ts.map +1 -1
- package/dist/types/src/space/space-manager.d.ts +1 -1
- package/dist/types/src/space/space-manager.d.ts.map +1 -1
- package/dist/types/src/space/space-protocol.d.ts +1 -1
- package/dist/types/src/space/space-protocol.d.ts.map +1 -1
- package/dist/types/src/space/space.d.ts +1 -1
- package/dist/types/src/space/space.d.ts.map +1 -1
- package/dist/types/src/testing/test-agent-builder.d.ts +2 -2
- package/dist/types/src/testing/test-agent-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-data.d.ts.map +1 -1
- package/dist/types/src/testing/test-replicator.d.ts +1 -0
- package/dist/types/src/testing/test-replicator.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/src/util.d.ts +1 -1
- package/dist/types/src/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +40 -38
- package/src/automerge/automerge-host.test.ts +18 -8
- package/src/automerge/automerge-host.ts +273 -74
- package/src/automerge/automerge-repo.test.ts +67 -16
- package/src/automerge/collection-synchronizer.test.ts +6 -5
- package/src/automerge/collection-synchronizer.ts +16 -11
- package/src/automerge/echo-data-monitor.ts +1 -1
- package/src/automerge/echo-network-adapter.test.ts +3 -3
- package/src/automerge/echo-network-adapter.ts +38 -6
- package/src/automerge/echo-replicator.ts +23 -2
- package/src/automerge/index.ts +1 -1
- package/src/automerge/leveldb-storage-adapter.ts +7 -7
- package/src/automerge/mesh-echo-replicator-connection.ts +4 -0
- package/src/automerge/mesh-echo-replicator.ts +2 -1
- package/src/automerge/storage-adapter.test.ts +1 -1
- package/src/common/space-id.ts +1 -1
- package/src/db-host/data-service.ts +9 -17
- package/src/db-host/database-root.ts +2 -2
- package/src/db-host/documents-synchronizer.test.ts +1 -1
- package/src/db-host/documents-synchronizer.ts +39 -26
- package/src/db-host/echo-host.ts +14 -15
- package/src/db-host/query-service.ts +10 -2
- package/src/db-host/space-state-manager.ts +2 -2
- package/src/edge/echo-edge-replicator.test.ts +5 -3
- package/src/edge/echo-edge-replicator.ts +105 -27
- package/src/filter/filter-match.test.ts +33 -13
- package/src/filter/filter-match.ts +164 -10
- package/src/metadata/metadata-store.ts +3 -3
- package/src/pipeline/pipeline-stress.test.ts +4 -2
- package/src/pipeline/pipeline.test.ts +3 -2
- package/src/pipeline/pipeline.ts +8 -5
- package/src/query/errors.ts +2 -0
- package/src/query/plan.ts +12 -1
- package/src/query/query-executor.ts +154 -14
- package/src/query/query-planner.test.ts +146 -2
- package/src/query/query-planner.ts +81 -11
- package/src/space/admission-discovery-extension.ts +2 -2
- package/src/space/control-pipeline.test.ts +4 -3
- package/src/space/control-pipeline.ts +9 -6
- package/src/space/space-manager.browser.test.ts +1 -1
- package/src/space/space-manager.ts +5 -4
- package/src/space/space-protocol.browser.test.ts +2 -2
- package/src/space/space-protocol.test.ts +3 -2
- package/src/space/space-protocol.ts +6 -3
- package/src/space/space.test.ts +1 -1
- package/src/space/space.ts +3 -2
- package/src/testing/test-agent-builder.ts +4 -3
- package/src/testing/test-data.ts +1 -1
- package/src/testing/test-replicator.ts +4 -0
- package/src/testing/test-schema.ts +1 -1
- package/src/util.ts +1 -1
- package/dist/lib/browser/chunk-ANZAS5CC.mjs +0 -126
- package/dist/lib/browser/chunk-ANZAS5CC.mjs.map +0 -7
- package/dist/lib/browser/chunk-GBFX5J5B.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-2SAZ7CCF.mjs +0 -126
- package/dist/lib/node-esm/chunk-2SAZ7CCF.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-FQFKWA3X.mjs.map +0 -7
|
@@ -2,8 +2,8 @@
|
|
|
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
8
|
|
|
9
9
|
export type MatchedObject = {
|
|
@@ -14,15 +14,16 @@ export type MatchedObject = {
|
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Matches an object against a filter AST.
|
|
17
|
+
* @param obj object structure as stored in automerge.
|
|
17
18
|
*/
|
|
18
19
|
export const filterMatchObject = (filter: QueryAST.Filter, obj: MatchedObject): boolean => {
|
|
19
20
|
switch (filter.type) {
|
|
20
21
|
case 'object': {
|
|
21
|
-
// Check typename if specified
|
|
22
|
+
// Check typename if specified.
|
|
22
23
|
if (filter.typename !== null) {
|
|
23
24
|
// TODO(dmaretskyi): `system` is missing in some cases.
|
|
24
25
|
if (!obj.doc.system?.type?.['/']) {
|
|
25
|
-
// Objects with no type are considered to be expando objects
|
|
26
|
+
// Objects with no type are considered to be expando objects.
|
|
26
27
|
const expectedDXN = DXN.parse(filter.typename).asTypeDXN();
|
|
27
28
|
if (expectedDXN?.type !== EXPANDO_TYPENAME) {
|
|
28
29
|
return false;
|
|
@@ -30,19 +31,18 @@ export const filterMatchObject = (filter: QueryAST.Filter, obj: MatchedObject):
|
|
|
30
31
|
} else {
|
|
31
32
|
const actualDXN = DXN.parse(obj.doc.system.type['/']);
|
|
32
33
|
const expectedDXN = DXN.parse(filter.typename);
|
|
33
|
-
|
|
34
34
|
if (!compareTypename(expectedDXN, actualDXN)) {
|
|
35
35
|
return false;
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
// Check IDs if specified
|
|
40
|
+
// Check IDs if specified.
|
|
41
41
|
if (filter.id && filter.id.length > 0 && !filter.id.includes(obj.id)) {
|
|
42
42
|
return false;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
// Check properties
|
|
45
|
+
// Check properties.
|
|
46
46
|
if (filter.props) {
|
|
47
47
|
for (const [key, valueFilter] of Object.entries(filter.props)) {
|
|
48
48
|
const value = obj.doc.data[key];
|
|
@@ -52,7 +52,7 @@ export const filterMatchObject = (filter: QueryAST.Filter, obj: MatchedObject):
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
// Check foreign keys if specified
|
|
55
|
+
// Check foreign keys if specified.
|
|
56
56
|
if (filter.foreignKeys && filter.foreignKeys.length > 0) {
|
|
57
57
|
const hasMatchingKey = filter.foreignKeys.some((filterKey) =>
|
|
58
58
|
obj.doc.meta.keys.some((objKey) => objKey.source === filterKey.source && objKey.id === filterKey.id),
|
|
@@ -65,8 +65,13 @@ export const filterMatchObject = (filter: QueryAST.Filter, obj: MatchedObject):
|
|
|
65
65
|
return true;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
case 'tag': {
|
|
69
|
+
const tags = ObjectStructure.getTags(obj.doc);
|
|
70
|
+
return tags.some((tag) => tag === filter.tag);
|
|
71
|
+
}
|
|
72
|
+
|
|
68
73
|
case 'text-search': {
|
|
69
|
-
// TODO: Implement text search
|
|
74
|
+
// TODO(???): Implement text search.
|
|
70
75
|
return false;
|
|
71
76
|
}
|
|
72
77
|
|
|
@@ -87,6 +92,123 @@ export const filterMatchObject = (filter: QueryAST.Filter, obj: MatchedObject):
|
|
|
87
92
|
}
|
|
88
93
|
};
|
|
89
94
|
|
|
95
|
+
// TODO(burdon): Reconcile with filterMatchObject.
|
|
96
|
+
export const filterMatchObjectJSON = (filter: QueryAST.Filter, obj: ObjectJSON): boolean => {
|
|
97
|
+
switch (filter.type) {
|
|
98
|
+
case 'object': {
|
|
99
|
+
// Check typename if specified
|
|
100
|
+
if (filter.typename !== null) {
|
|
101
|
+
// TODO(dmaretskyi): `system` is missing in some cases.
|
|
102
|
+
if (!obj['@type']) {
|
|
103
|
+
// Objects with no type are considered to be expando objects
|
|
104
|
+
const expectedDXN = DXN.parse(filter.typename).asTypeDXN();
|
|
105
|
+
if (expectedDXN?.type !== EXPANDO_TYPENAME) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
} else {
|
|
109
|
+
const actualDXN = DXN.parse(obj['@type']);
|
|
110
|
+
const expectedDXN = DXN.parse(filter.typename);
|
|
111
|
+
if (!compareTypename(expectedDXN, actualDXN)) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Check IDs if specified
|
|
118
|
+
if (filter.id && filter.id.length > 0 && !filter.id.includes(obj.id)) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Check properties
|
|
123
|
+
if (filter.props) {
|
|
124
|
+
for (const [key, valueFilter] of Object.entries(filter.props)) {
|
|
125
|
+
if (key.startsWith('@')) {
|
|
126
|
+
// ignore meta properties
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const value = (obj as any)[key];
|
|
130
|
+
if (!filterMatchValue(valueFilter, value)) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Check foreign keys if specified
|
|
137
|
+
if (filter.foreignKeys && filter.foreignKeys.length > 0) {
|
|
138
|
+
const hasMatchingKey = filter.foreignKeys.some((filterKey) =>
|
|
139
|
+
obj['@meta']?.keys?.some((objKey) => objKey.source === filterKey.source && objKey.id === filterKey.id),
|
|
140
|
+
);
|
|
141
|
+
if (!hasMatchingKey) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
case 'tag': {
|
|
150
|
+
const tags = obj[ATTR_META]?.tags ?? [];
|
|
151
|
+
return tags.some((tag) => tag === filter.tag);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
case 'text-search': {
|
|
155
|
+
// TODO: Implement text search
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
case 'not': {
|
|
160
|
+
return !filterMatchObjectJSON(filter.filter, obj);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
case 'and': {
|
|
164
|
+
return filter.filters.every((f) => filterMatchObjectJSON(f, obj));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
case 'or': {
|
|
168
|
+
return filter.filters.some((f) => filterMatchObjectJSON(f, obj));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
default:
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Performs structural matching between a filter object and a target object.
|
|
178
|
+
* This handles nested object comparison for array matching scenarios.
|
|
179
|
+
*/
|
|
180
|
+
// TODO(wittjosiah): Add ast support for non-strict matching.
|
|
181
|
+
const structuralMatch = (filterObj: any, targetObj: any, strict = true): boolean => {
|
|
182
|
+
if (typeof filterObj !== 'object' || filterObj === null) {
|
|
183
|
+
return filterObj === targetObj;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (typeof targetObj !== 'object' || targetObj === null) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Prohibit extra keys in targetObj.
|
|
191
|
+
const filterKeys = Object.keys(filterObj);
|
|
192
|
+
const targetKeys = Object.keys(targetObj);
|
|
193
|
+
if (strict && filterKeys.length !== targetKeys.length) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return filterKeys.every((key) => {
|
|
198
|
+
if (!(key in targetObj)) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
const filterValue = filterObj[key];
|
|
202
|
+
const targetValue = targetObj[key];
|
|
203
|
+
|
|
204
|
+
if (typeof filterValue === 'object' && filterValue !== null) {
|
|
205
|
+
return structuralMatch(filterValue, targetValue);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return filterValue === targetValue;
|
|
209
|
+
});
|
|
210
|
+
};
|
|
211
|
+
|
|
90
212
|
export const filterMatchValue = (filter: QueryAST.Filter, value: unknown): boolean => {
|
|
91
213
|
switch (filter.type) {
|
|
92
214
|
case 'compare': {
|
|
@@ -110,12 +232,44 @@ export const filterMatchValue = (filter: QueryAST.Filter, value: unknown): boole
|
|
|
110
232
|
return (value as any) < compareValue;
|
|
111
233
|
case 'lte':
|
|
112
234
|
return (value as any) <= compareValue;
|
|
235
|
+
default:
|
|
236
|
+
return false;
|
|
113
237
|
}
|
|
114
|
-
|
|
238
|
+
}
|
|
239
|
+
case 'object': {
|
|
240
|
+
// Handle nested object filters for property matching
|
|
241
|
+
if (typeof value !== 'object' || value === null) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Check properties
|
|
246
|
+
if (filter.props) {
|
|
247
|
+
for (const [key, valueFilter] of Object.entries(filter.props)) {
|
|
248
|
+
const nestedValue = (value as any)[key];
|
|
249
|
+
if (!filterMatchValue(valueFilter, nestedValue)) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return true;
|
|
115
256
|
}
|
|
116
257
|
case 'in': {
|
|
117
258
|
return filter.values.includes(value);
|
|
118
259
|
}
|
|
260
|
+
case 'contains': {
|
|
261
|
+
if (!Array.isArray(value)) {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return value.some((element) => {
|
|
266
|
+
if (typeof filter.value === 'object' && filter.value !== null && !Array.isArray(filter.value)) {
|
|
267
|
+
return structuralMatch(filter.value, element);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return element === filter.value;
|
|
271
|
+
});
|
|
272
|
+
}
|
|
119
273
|
case 'range': {
|
|
120
274
|
return (value as any) >= filter.from && (value as any) <= filter.to;
|
|
121
275
|
}
|
|
@@ -16,11 +16,11 @@ import { Invitation, SpaceState } from '@dxos/protocols/proto/dxos/client/servic
|
|
|
16
16
|
import {
|
|
17
17
|
type ControlPipelineSnapshot,
|
|
18
18
|
type EchoMetadata,
|
|
19
|
-
type
|
|
19
|
+
type EdgeReplicationSetting,
|
|
20
20
|
type IdentityRecord,
|
|
21
|
-
type SpaceCache,
|
|
22
21
|
type LargeSpaceMetadata,
|
|
23
|
-
type
|
|
22
|
+
type SpaceCache,
|
|
23
|
+
type SpaceMetadata,
|
|
24
24
|
} from '@dxos/protocols/proto/dxos/echo/metadata';
|
|
25
25
|
import { type Directory, type File } from '@dxos/random-access-storage';
|
|
26
26
|
import { type Timeframe } from '@dxos/timeframe';
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import * as fc from 'fast-check';
|
|
6
5
|
import { inspect } from 'node:util';
|
|
6
|
+
|
|
7
|
+
import * as fc from 'fast-check';
|
|
7
8
|
import { describe, expect, test } from 'vitest';
|
|
8
9
|
|
|
9
10
|
import { asyncTimeout } from '@dxos/async';
|
|
@@ -15,9 +16,10 @@ import { type FeedMessage } from '@dxos/protocols/proto/dxos/echo/feed';
|
|
|
15
16
|
import { Timeframe } from '@dxos/timeframe';
|
|
16
17
|
import { range } from '@dxos/util';
|
|
17
18
|
|
|
18
|
-
import { Pipeline } from './pipeline';
|
|
19
19
|
import { TestFeedBuilder } from '../testing';
|
|
20
20
|
|
|
21
|
+
import { Pipeline } from './pipeline';
|
|
22
|
+
|
|
21
23
|
const NUM_AGENTS = 2;
|
|
22
24
|
const NUM_MESSAGES = 10;
|
|
23
25
|
|
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { describe, expect,
|
|
5
|
+
import { describe, expect, onTestFinished, test } from 'vitest';
|
|
6
6
|
|
|
7
7
|
import { Event, sleep } from '@dxos/async';
|
|
8
8
|
import { type FeedMessage } from '@dxos/protocols/proto/dxos/echo/feed';
|
|
9
9
|
import { Timeframe } from '@dxos/timeframe';
|
|
10
10
|
import { range } from '@dxos/util';
|
|
11
11
|
|
|
12
|
-
import { Pipeline } from './pipeline';
|
|
13
12
|
import { TestFeedBuilder } from '../testing';
|
|
14
13
|
|
|
14
|
+
import { Pipeline } from './pipeline';
|
|
15
|
+
|
|
15
16
|
const TEST_MESSAGE: FeedMessage = {
|
|
16
17
|
timeframe: new Timeframe(),
|
|
17
18
|
payload: {},
|
package/src/pipeline/pipeline.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Event, sleepWithContext, synchronized
|
|
5
|
+
import { Event, Trigger, sleepWithContext, synchronized } from '@dxos/async';
|
|
6
6
|
import { Context, rejectOnDispose } from '@dxos/context';
|
|
7
7
|
import { failUndefined } from '@dxos/debug';
|
|
8
8
|
import { FeedSetIterator, type FeedWrapper, type FeedWriter } from '@dxos/feed-store';
|
|
@@ -14,10 +14,11 @@ import type { FeedMessage } from '@dxos/protocols/proto/dxos/echo/feed';
|
|
|
14
14
|
import { Timeframe } from '@dxos/timeframe';
|
|
15
15
|
import { ComplexMap } from '@dxos/util';
|
|
16
16
|
|
|
17
|
-
import { createMessageSelector } from './message-selector';
|
|
18
|
-
import { mapFeedIndexesToTimeframe, startAfter, TimeframeClock } from './timeframe-clock';
|
|
19
17
|
import { createMappedFeedWriter } from '../common';
|
|
20
18
|
|
|
19
|
+
import { createMessageSelector } from './message-selector';
|
|
20
|
+
import { TimeframeClock, mapFeedIndexesToTimeframe, startAfter } from './timeframe-clock';
|
|
21
|
+
|
|
21
22
|
export type WaitUntilReachedTargetParams = {
|
|
22
23
|
/**
|
|
23
24
|
* For cancellation.
|
|
@@ -40,8 +41,10 @@ export class PipelineState {
|
|
|
40
41
|
*/
|
|
41
42
|
_ctx = new Context();
|
|
42
43
|
|
|
43
|
-
// TODO(dmaretskyi): Remove?.
|
|
44
|
-
public
|
|
44
|
+
// TODO(dmaretskyi): Remove?. Avoid accessing `_timeframeClock` before constructor initialization.
|
|
45
|
+
public get timeframeUpdate() {
|
|
46
|
+
return this._timeframeClock.update;
|
|
47
|
+
}
|
|
45
48
|
|
|
46
49
|
public readonly stalled = new Event();
|
|
47
50
|
|
package/src/query/errors.ts
CHANGED
package/src/query/plan.ts
CHANGED
|
@@ -32,7 +32,8 @@ export namespace QueryPlan {
|
|
|
32
32
|
| FilterDeletedStep
|
|
33
33
|
| TraverseStep
|
|
34
34
|
| UnionStep
|
|
35
|
-
| SetDifferenceStep
|
|
35
|
+
| SetDifferenceStep
|
|
36
|
+
| OrderStep;
|
|
36
37
|
|
|
37
38
|
/**
|
|
38
39
|
* Clear the current working set.
|
|
@@ -193,4 +194,14 @@ export namespace QueryPlan {
|
|
|
193
194
|
source: Plan;
|
|
194
195
|
exclude: Plan;
|
|
195
196
|
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Order the results of the plan.
|
|
200
|
+
*/
|
|
201
|
+
export type OrderStep = {
|
|
202
|
+
_tag: 'OrderStep';
|
|
203
|
+
|
|
204
|
+
// Defaults to natural order if empty.
|
|
205
|
+
order: readonly QueryAST.Order[];
|
|
206
|
+
};
|
|
196
207
|
}
|
|
@@ -3,10 +3,11 @@
|
|
|
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
|
-
import { DatabaseDirectory,
|
|
10
|
+
import { DatabaseDirectory, ObjectStructure, type QueryAST, isEncodedReference } from '@dxos/echo-protocol';
|
|
10
11
|
import { EscapedPropPath, type FindResult, type Indexer } from '@dxos/indexing';
|
|
11
12
|
import { invariant } from '@dxos/invariant';
|
|
12
13
|
import { DXN, type ObjectId, PublicKey, type SpaceId } from '@dxos/keys';
|
|
@@ -15,13 +16,16 @@ import { objectPointerCodec } from '@dxos/protocols';
|
|
|
15
16
|
import { type QueryReactivity, type QueryResult } from '@dxos/protocols/proto/dxos/echo/query';
|
|
16
17
|
import { getDeep, isNonNullable } from '@dxos/util';
|
|
17
18
|
|
|
18
|
-
import type { QueryPlan } from './plan';
|
|
19
|
-
import { QueryPlanner } from './query-planner';
|
|
20
19
|
import type { AutomergeHost } from '../automerge';
|
|
21
20
|
import { createIdFromSpaceKey } from '../common';
|
|
22
21
|
import type { SpaceStateManager } from '../db-host';
|
|
23
22
|
import { filterMatchObject } from '../filter';
|
|
24
23
|
|
|
24
|
+
import type { QueryPlan } from './plan';
|
|
25
|
+
import { QueryPlanner } from './query-planner';
|
|
26
|
+
|
|
27
|
+
const isNullable: Predicate.Refinement<unknown, null | undefined> = Predicate.isNullable;
|
|
28
|
+
|
|
25
29
|
type QueryExecutorOptions = {
|
|
26
30
|
indexer: Indexer;
|
|
27
31
|
automergeHost: AutomergeHost;
|
|
@@ -60,6 +64,9 @@ export type ExecutionTrace = {
|
|
|
60
64
|
documentsLoaded: number;
|
|
61
65
|
indexHits: number;
|
|
62
66
|
|
|
67
|
+
beginTs: number;
|
|
68
|
+
endTs: number;
|
|
69
|
+
|
|
63
70
|
executionTime: number;
|
|
64
71
|
indexQueryTime: number;
|
|
65
72
|
documentLoadTime: number;
|
|
@@ -74,11 +81,42 @@ export const ExecutionTrace = Object.freeze({
|
|
|
74
81
|
objectCount: 0,
|
|
75
82
|
documentsLoaded: 0,
|
|
76
83
|
indexHits: 0,
|
|
84
|
+
beginTs: 0,
|
|
85
|
+
endTs: 0,
|
|
77
86
|
indexQueryTime: 0,
|
|
78
87
|
documentLoadTime: 0,
|
|
79
88
|
executionTime: 0,
|
|
80
89
|
children: [],
|
|
81
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
|
+
},
|
|
82
120
|
format: (trace: ExecutionTrace): string => {
|
|
83
121
|
const go = (trace: ExecutionTrace, indent: number): string => {
|
|
84
122
|
return [
|
|
@@ -188,8 +226,10 @@ export class QueryExecutor extends Resource {
|
|
|
188
226
|
workingSet[index].documentId !== item.documentId,
|
|
189
227
|
);
|
|
190
228
|
|
|
229
|
+
// Disabled because concurrent queries don't print hierarchies correctly.
|
|
230
|
+
// ExecutionTrace.putOnPerformanceTimeline(trace);
|
|
231
|
+
|
|
191
232
|
if (TRACE_QUERY_EXECUTION) {
|
|
192
|
-
// eslint-disable-next-line no-console
|
|
193
233
|
console.log(ExecutionTrace.format(trace));
|
|
194
234
|
}
|
|
195
235
|
|
|
@@ -211,7 +251,7 @@ export class QueryExecutor extends Resource {
|
|
|
211
251
|
trace.children.push(result.trace);
|
|
212
252
|
}
|
|
213
253
|
trace.objectCount = workingSet.length;
|
|
214
|
-
|
|
254
|
+
ExecutionTrace.markEnd(trace);
|
|
215
255
|
return { workingSet, trace };
|
|
216
256
|
}
|
|
217
257
|
|
|
@@ -245,10 +285,14 @@ export class QueryExecutor extends Resource {
|
|
|
245
285
|
case 'TraverseStep':
|
|
246
286
|
({ workingSet: newWorkingSet, trace } = await this._execTraverseStep(step, workingSet));
|
|
247
287
|
break;
|
|
288
|
+
case 'OrderStep':
|
|
289
|
+
({ workingSet: newWorkingSet, trace } = await this._execOrderStep(step, workingSet));
|
|
290
|
+
break;
|
|
248
291
|
default:
|
|
249
292
|
throw new Error(`Unknown step type: ${(step as any)._tag}`);
|
|
250
293
|
}
|
|
251
|
-
trace.
|
|
294
|
+
trace.beginTs = begin;
|
|
295
|
+
ExecutionTrace.markEnd(trace);
|
|
252
296
|
|
|
253
297
|
return { workingSet: newWorkingSet, trace };
|
|
254
298
|
}
|
|
@@ -260,6 +304,7 @@ export class QueryExecutor extends Resource {
|
|
|
260
304
|
...ExecutionTrace.makeEmpty(),
|
|
261
305
|
name: 'Select',
|
|
262
306
|
details: JSON.stringify(step.selector),
|
|
307
|
+
beginTs: performance.now(),
|
|
263
308
|
};
|
|
264
309
|
|
|
265
310
|
switch (step.selector._tag) {
|
|
@@ -286,6 +331,7 @@ export class QueryExecutor extends Resource {
|
|
|
286
331
|
|
|
287
332
|
break;
|
|
288
333
|
}
|
|
334
|
+
|
|
289
335
|
case 'IdSelector': {
|
|
290
336
|
const beginLoad = performance.now();
|
|
291
337
|
const items = await Promise.all(
|
|
@@ -299,6 +345,7 @@ export class QueryExecutor extends Resource {
|
|
|
299
345
|
trace.objectCount = workingSet.length;
|
|
300
346
|
break;
|
|
301
347
|
}
|
|
348
|
+
|
|
302
349
|
case 'TypeSelector': {
|
|
303
350
|
const beginIndexQuery = performance.now();
|
|
304
351
|
const indexHits = await this._indexer.execQuery({
|
|
@@ -322,6 +369,7 @@ export class QueryExecutor extends Resource {
|
|
|
322
369
|
|
|
323
370
|
break;
|
|
324
371
|
}
|
|
372
|
+
|
|
325
373
|
case 'TextSelector': {
|
|
326
374
|
const beginIndexQuery = performance.now();
|
|
327
375
|
const indexHits = await this._indexer.execQuery({
|
|
@@ -352,6 +400,7 @@ export class QueryExecutor extends Resource {
|
|
|
352
400
|
trace.objectCount = workingSet.length;
|
|
353
401
|
break;
|
|
354
402
|
}
|
|
403
|
+
|
|
355
404
|
default:
|
|
356
405
|
throw new Error(`Unknown selector type: ${(step.selector as any)._tag}`);
|
|
357
406
|
}
|
|
@@ -367,6 +416,7 @@ export class QueryExecutor extends Resource {
|
|
|
367
416
|
doc: item.doc,
|
|
368
417
|
}),
|
|
369
418
|
);
|
|
419
|
+
|
|
370
420
|
return {
|
|
371
421
|
workingSet: result,
|
|
372
422
|
trace: {
|
|
@@ -382,10 +432,6 @@ export class QueryExecutor extends Resource {
|
|
|
382
432
|
step: QueryPlan.FilterDeletedStep,
|
|
383
433
|
workingSet: QueryItem[],
|
|
384
434
|
): Promise<StepExecutionResult> {
|
|
385
|
-
if (workingSet.length === 6) {
|
|
386
|
-
log.info('FilterDeletedStep', { step, workingSet });
|
|
387
|
-
}
|
|
388
|
-
|
|
389
435
|
const expected = step.mode === 'only-deleted';
|
|
390
436
|
const result = workingSet.filter((item) => ObjectStructure.isDeleted(item.doc) === expected);
|
|
391
437
|
return {
|
|
@@ -428,7 +474,7 @@ export class QueryExecutor extends Resource {
|
|
|
428
474
|
}
|
|
429
475
|
: null;
|
|
430
476
|
} catch {
|
|
431
|
-
log.warn('
|
|
477
|
+
log.warn('invalid reference', { ref: ref['/'] });
|
|
432
478
|
return null;
|
|
433
479
|
}
|
|
434
480
|
});
|
|
@@ -491,7 +537,7 @@ export class QueryExecutor extends Resource {
|
|
|
491
537
|
spaceId: item.spaceId,
|
|
492
538
|
};
|
|
493
539
|
} catch {
|
|
494
|
-
log.warn('
|
|
540
|
+
log.warn('invalid reference', { ref: ref['/'] });
|
|
495
541
|
return null;
|
|
496
542
|
}
|
|
497
543
|
})
|
|
@@ -590,6 +636,89 @@ export class QueryExecutor extends Resource {
|
|
|
590
636
|
};
|
|
591
637
|
}
|
|
592
638
|
|
|
639
|
+
private async _execOrderStep(step: QueryPlan.OrderStep, workingSet: QueryItem[]): Promise<StepExecutionResult> {
|
|
640
|
+
const sortedWorkingSet = [...workingSet].sort((a, b) => this._compareMultiOrder(a, b, step.order));
|
|
641
|
+
|
|
642
|
+
return {
|
|
643
|
+
workingSet: sortedWorkingSet,
|
|
644
|
+
trace: {
|
|
645
|
+
...ExecutionTrace.makeEmpty(),
|
|
646
|
+
name: 'Order',
|
|
647
|
+
details: JSON.stringify(step.order),
|
|
648
|
+
objectCount: sortedWorkingSet.length,
|
|
649
|
+
},
|
|
650
|
+
};
|
|
651
|
+
}
|
|
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
|
+
|
|
670
|
+
private _compareByOrder(a: QueryItem, b: QueryItem, order: QueryAST.Order): number {
|
|
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
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
private _compareByProperty(a: QueryItem, b: QueryItem, property: string): number {
|
|
685
|
+
const aValue = a.doc.data[property];
|
|
686
|
+
const bValue = b.doc.data[property];
|
|
687
|
+
|
|
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));
|
|
720
|
+
}
|
|
721
|
+
|
|
593
722
|
private async _loadDocumentsAfterIndexQuery(indexHits: FindResult[]): Promise<(QueryItem | null)[]> {
|
|
594
723
|
return Promise.all(
|
|
595
724
|
indexHits.map(async (hit): Promise<QueryItem | null> => {
|
|
@@ -598,6 +727,11 @@ export class QueryExecutor extends Resource {
|
|
|
598
727
|
);
|
|
599
728
|
}
|
|
600
729
|
|
|
730
|
+
/**
|
|
731
|
+
* Space key hex -> SpaceId.
|
|
732
|
+
*/
|
|
733
|
+
private readonly _spaceIdCache = new Map<string, SpaceId>();
|
|
734
|
+
|
|
601
735
|
private async _loadFromIndexHit(hit: FindResult): Promise<QueryItem | null> {
|
|
602
736
|
const { objectId, documentId, spaceKey: spaceKeyInIndex } = objectPointerCodec.decode(hit.id);
|
|
603
737
|
|
|
@@ -612,6 +746,12 @@ export class QueryExecutor extends Resource {
|
|
|
612
746
|
return null;
|
|
613
747
|
}
|
|
614
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
|
+
|
|
615
755
|
const object = DatabaseDirectory.getInlineObject(doc, objectId);
|
|
616
756
|
if (!object) {
|
|
617
757
|
return null;
|
|
@@ -620,7 +760,7 @@ export class QueryExecutor extends Resource {
|
|
|
620
760
|
return {
|
|
621
761
|
objectId,
|
|
622
762
|
documentId: documentId as DocumentId,
|
|
623
|
-
spaceId
|
|
763
|
+
spaceId,
|
|
624
764
|
doc: object,
|
|
625
765
|
};
|
|
626
766
|
}
|