@dxos/index-core 0.8.4-main.ef1bc66f44 → 0.8.4-main.f466a3d56e
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/LICENSE +102 -5
- package/README.md +1 -1
- package/dist/lib/neutral/index.mjs +198 -66
- package/dist/lib/neutral/index.mjs.map +3 -3
- package/dist/lib/neutral/meta.json +1 -1
- package/dist/types/src/index-engine.d.ts +38 -9
- package/dist/types/src/index-engine.d.ts.map +1 -1
- package/dist/types/src/index-tracker.d.ts +3 -3
- package/dist/types/src/index-tracker.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/indexes/fts-index.d.ts +2 -1
- package/dist/types/src/indexes/fts-index.d.ts.map +1 -1
- package/dist/types/src/indexes/interface.d.ts +9 -0
- package/dist/types/src/indexes/interface.d.ts.map +1 -1
- package/dist/types/src/indexes/object-meta-index.d.ts +40 -6
- package/dist/types/src/indexes/object-meta-index.d.ts.map +1 -1
- package/dist/types/src/indexes/reverse-ref-index.d.ts +2 -2
- package/dist/types/src/indexes/reverse-ref-index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +13 -13
- package/src/index-engine.test.ts +168 -8
- package/src/index-engine.ts +116 -28
- package/src/index-tracker.ts +9 -0
- package/src/index.ts +7 -1
- package/src/indexes/fts-index.test.ts +153 -3
- package/src/indexes/fts-index.ts +25 -8
- package/src/indexes/interface.ts +10 -0
- package/src/indexes/object-meta-index.test.ts +199 -20
- package/src/indexes/object-meta-index.ts +184 -32
- package/src/indexes/reverse-ref-index.test.ts +31 -17
- package/src/indexes/reverse-ref-index.ts +11 -12
package/LICENSE
CHANGED
|
@@ -1,8 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
Copyright (c) 2022 DXOS
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
## Abbreviation
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
FSL-1.1-Apache-2.0
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 DXOS
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
package/README.md
CHANGED
|
@@ -18,4 +18,4 @@ pnpm i @dxos/index-core
|
|
|
18
18
|
|
|
19
19
|
Your ideas, issues, and code are most welcome. Please take a look at our [community code of conduct](https://github.com/dxos/dxos/blob/main/CODE_OF_CONDUCT.md), the [issue guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-issues), and the [PR contribution guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-prs).
|
|
20
20
|
|
|
21
|
-
License: [
|
|
21
|
+
License: [FSL-1.1-Apache-2.0](./LICENSE) Copyright 2022 © DXOS
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/index-engine.ts
|
|
2
2
|
import * as Effect5 from "effect/Effect";
|
|
3
|
+
import { ATTR_TYPE as ATTR_TYPE2 } from "@dxos/echo/internal";
|
|
3
4
|
import * as SqlTransaction from "@dxos/sql-sqlite/SqlTransaction";
|
|
4
5
|
|
|
5
6
|
// src/index-tracker.ts
|
|
@@ -31,6 +32,9 @@ var IndexCursor = Schema.Struct({
|
|
|
31
32
|
*/
|
|
32
33
|
cursor: Schema.Union(Schema.Number, Schema.String)
|
|
33
34
|
});
|
|
35
|
+
var DEPRECATED_INDEX_NAMES = [
|
|
36
|
+
"fts"
|
|
37
|
+
];
|
|
34
38
|
var IndexTracker = class {
|
|
35
39
|
migrate = Effect.fn("IndexTracker.migrate")(function* () {
|
|
36
40
|
const sql = yield* SqlClient.SqlClient;
|
|
@@ -42,6 +46,9 @@ var IndexTracker = class {
|
|
|
42
46
|
cursor,
|
|
43
47
|
PRIMARY KEY (indexName, spaceId, sourceName, resourceId)
|
|
44
48
|
)`;
|
|
49
|
+
yield* Effect.forEach(DEPRECATED_INDEX_NAMES, (indexName) => {
|
|
50
|
+
return sql`DELETE FROM indexCursor WHERE indexName = ${indexName}`;
|
|
51
|
+
});
|
|
45
52
|
});
|
|
46
53
|
queryCursors = Effect.fn("IndexTracker.queryCursors")((query) => Effect.gen(function* () {
|
|
47
54
|
const sql = yield* SqlClient.SqlClient;
|
|
@@ -82,6 +89,7 @@ var IndexTracker = class {
|
|
|
82
89
|
// src/indexes/fts-index.ts
|
|
83
90
|
import * as SqlClient3 from "@effect/sql/SqlClient";
|
|
84
91
|
import * as Effect2 from "effect/Effect";
|
|
92
|
+
var SQL_CHUNK_SIZE = 500;
|
|
85
93
|
var escapeFts5Query = (text) => {
|
|
86
94
|
return text.split(/\s+/).filter(Boolean).map((term) => `"${term.replace(/"/g, '""')}"`).join(" ");
|
|
87
95
|
};
|
|
@@ -143,6 +151,7 @@ var FtsIndex = class {
|
|
|
143
151
|
/**
|
|
144
152
|
* Query snapshots by recordIds.
|
|
145
153
|
* Returns the parsed JSON snapshots for queue objects.
|
|
154
|
+
* RecordIds not present in the FTS index are silently omitted from the result.
|
|
146
155
|
*/
|
|
147
156
|
querySnapshotsJSON(recordIds) {
|
|
148
157
|
return Effect2.gen(function* () {
|
|
@@ -150,11 +159,21 @@ var FtsIndex = class {
|
|
|
150
159
|
return [];
|
|
151
160
|
}
|
|
152
161
|
const sql = yield* SqlClient3.SqlClient;
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
162
|
+
const chunks = [];
|
|
163
|
+
for (let i = 0; i < recordIds.length; i += SQL_CHUNK_SIZE) {
|
|
164
|
+
chunks.push(recordIds.slice(i, i + SQL_CHUNK_SIZE));
|
|
165
|
+
}
|
|
166
|
+
const allResults = [];
|
|
167
|
+
for (const chunk of chunks) {
|
|
168
|
+
const rows = yield* sql`SELECT rowid, snapshot FROM ftsIndex WHERE rowid IN ${sql.in(chunk)}`;
|
|
169
|
+
for (const r of rows) {
|
|
170
|
+
allResults.push({
|
|
171
|
+
recordId: r.rowid,
|
|
172
|
+
snapshot: JSON.parse(r.snapshot)
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return allResults;
|
|
158
177
|
});
|
|
159
178
|
}
|
|
160
179
|
update = Effect2.fn("FtsIndex.update")((objects) => Effect2.gen(function* () {
|
|
@@ -190,19 +209,42 @@ var ObjectMeta = Schema2.Struct({
|
|
|
190
209
|
recordId: Schema2.Number,
|
|
191
210
|
objectId: Schema2.String,
|
|
192
211
|
queueId: Schema2.String,
|
|
212
|
+
/** Queue subspace namespace (e.g. 'data', 'trace'). Empty string for non-queue objects. */
|
|
213
|
+
queueNamespace: Schema2.String,
|
|
193
214
|
spaceId: Schema2.String,
|
|
194
215
|
documentId: Schema2.String,
|
|
195
216
|
entityKind: Schema2.String,
|
|
196
217
|
/** The versioned DXN of the type of the object. */
|
|
197
|
-
|
|
218
|
+
typeDXN: Schema2.String,
|
|
198
219
|
deleted: Schema2.Boolean,
|
|
199
220
|
source: Schema2.NullOr(Schema2.String),
|
|
200
221
|
target: Schema2.NullOr(Schema2.String),
|
|
201
222
|
/** Parent object id (nullable). */
|
|
202
223
|
parent: Schema2.NullOr(Schema2.String),
|
|
203
224
|
/** Monotonically increasing sequence number assigned on insert/update for tracking indexing order. */
|
|
204
|
-
version: Schema2.Number
|
|
225
|
+
version: Schema2.Number,
|
|
226
|
+
/** Unix ms timestamp when the object was first indexed. */
|
|
227
|
+
createdAt: Schema2.NullOr(Schema2.Number),
|
|
228
|
+
/** Unix ms timestamp when the object was last re-indexed. */
|
|
229
|
+
updatedAt: Schema2.NullOr(Schema2.Number)
|
|
205
230
|
});
|
|
231
|
+
var buildSourceCondition = (sql, spaceIds, includeAllQueues, queueIds) => {
|
|
232
|
+
const conditions = [];
|
|
233
|
+
if (spaceIds.length > 0) {
|
|
234
|
+
if (includeAllQueues) {
|
|
235
|
+
conditions.push(sql`${sql.in("spaceId", spaceIds)}`);
|
|
236
|
+
} else {
|
|
237
|
+
conditions.push(sql`(${sql.in("spaceId", spaceIds)} AND queueId = '')`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (queueIds && queueIds.length > 0) {
|
|
241
|
+
conditions.push(sql`${sql.in("queueId", queueIds)}`);
|
|
242
|
+
}
|
|
243
|
+
if (conditions.length === 0) {
|
|
244
|
+
return sql`1 = 0`;
|
|
245
|
+
}
|
|
246
|
+
return sql.or(conditions);
|
|
247
|
+
};
|
|
206
248
|
var ObjectMetaIndex = class {
|
|
207
249
|
migrate = Effect3.fn("ObjectMetaIndex.runMigrations")(function* () {
|
|
208
250
|
const sql = yield* SqlClient5.SqlClient;
|
|
@@ -210,44 +252,53 @@ var ObjectMetaIndex = class {
|
|
|
210
252
|
recordId INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
211
253
|
objectId TEXT NOT NULL,
|
|
212
254
|
queueId TEXT NOT NULL DEFAULT '',
|
|
255
|
+
queueNamespace TEXT NOT NULL DEFAULT '',
|
|
213
256
|
spaceId TEXT NOT NULL,
|
|
214
257
|
documentId TEXT NOT NULL DEFAULT '',
|
|
215
258
|
entityKind TEXT NOT NULL,
|
|
216
|
-
|
|
259
|
+
typeDXN TEXT NOT NULL,
|
|
217
260
|
deleted INTEGER NOT NULL,
|
|
218
261
|
source TEXT,
|
|
219
262
|
target TEXT,
|
|
220
263
|
parent TEXT,
|
|
221
|
-
version INTEGER NOT NULL
|
|
264
|
+
version INTEGER NOT NULL,
|
|
265
|
+
createdAt INTEGER,
|
|
266
|
+
updatedAt INTEGER
|
|
222
267
|
)`;
|
|
223
268
|
yield* Effect3.catchAll(sql`ALTER TABLE objectMeta ADD COLUMN parent TEXT`, () => Effect3.void);
|
|
269
|
+
yield* Effect3.catchAll(sql`ALTER TABLE objectMeta ADD COLUMN createdAt INTEGER`, () => Effect3.void);
|
|
270
|
+
yield* Effect3.catchAll(sql`ALTER TABLE objectMeta ADD COLUMN updatedAt INTEGER`, () => Effect3.void);
|
|
271
|
+
yield* Effect3.catchAll(sql`ALTER TABLE objectMeta ADD COLUMN queueNamespace TEXT NOT NULL DEFAULT ''`, () => Effect3.void);
|
|
224
272
|
yield* sql`CREATE INDEX IF NOT EXISTS idx_object_index_objectId ON objectMeta(spaceId, objectId)`;
|
|
225
|
-
yield* sql`CREATE INDEX IF NOT EXISTS
|
|
273
|
+
yield* sql`CREATE INDEX IF NOT EXISTS idx_object_index_typeDXN ON objectMeta(spaceId, typeDXN)`;
|
|
226
274
|
yield* sql`CREATE INDEX IF NOT EXISTS idx_object_index_version ON objectMeta(version)`;
|
|
227
275
|
yield* sql`CREATE INDEX IF NOT EXISTS idx_object_index_parent ON objectMeta(spaceId, parent)`;
|
|
276
|
+
yield* sql`CREATE INDEX IF NOT EXISTS idx_object_index_updatedAt ON objectMeta(updatedAt)`;
|
|
277
|
+
yield* sql`CREATE INDEX IF NOT EXISTS idx_object_index_createdAt ON objectMeta(createdAt)`;
|
|
228
278
|
});
|
|
229
279
|
query = Effect3.fn("ObjectMetaIndex.query")((query) => Effect3.gen(function* () {
|
|
230
280
|
const sql = yield* SqlClient5.SqlClient;
|
|
231
|
-
const parsedType = DXN.tryParse(query.
|
|
232
|
-
const rows = parsedType && parsedType.version === void 0 ? yield* sql`SELECT * FROM objectMeta WHERE spaceId = ${query.spaceId} AND (
|
|
281
|
+
const parsedType = DXN.tryParse(query.typeDXN)?.asTypeDXN();
|
|
282
|
+
const rows = parsedType && parsedType.version === void 0 ? yield* sql`SELECT * FROM objectMeta WHERE spaceId = ${query.spaceId} AND (typeDXN = ${query.typeDXN} OR typeDXN LIKE ${_escapeLikePrefix(query.typeDXN)} ESCAPE '\\')` : yield* sql`SELECT * FROM objectMeta WHERE spaceId = ${query.spaceId} AND typeDXN = ${query.typeDXN}`;
|
|
233
283
|
return rows.map((row) => ({
|
|
234
284
|
...row,
|
|
235
285
|
deleted: !!row.deleted
|
|
236
286
|
}));
|
|
237
287
|
}));
|
|
238
288
|
queryAll = Effect3.fn("ObjectMetaIndex.queryAll")((query) => Effect3.gen(function* () {
|
|
239
|
-
if (query.spaceIds.length === 0) {
|
|
289
|
+
if (query.spaceIds.length === 0 && (!query.queueIds || query.queueIds.length === 0)) {
|
|
240
290
|
return [];
|
|
241
291
|
}
|
|
242
292
|
const sql = yield* SqlClient5.SqlClient;
|
|
243
|
-
const
|
|
293
|
+
const sourceCondition = buildSourceCondition(sql, query.spaceIds, query.includeAllQueues ?? false, query.queueIds ?? null);
|
|
294
|
+
const rows = yield* sql`SELECT * FROM objectMeta WHERE ${sourceCondition}`;
|
|
244
295
|
return rows.map((row) => ({
|
|
245
296
|
...row,
|
|
246
297
|
deleted: !!row.deleted
|
|
247
298
|
}));
|
|
248
299
|
}));
|
|
249
|
-
queryTypes = Effect3.fn("ObjectMetaIndex.queryTypes")(({ spaceIds, typeDxns, inverted = false }) => Effect3.gen(function* () {
|
|
250
|
-
if (spaceIds.length === 0) {
|
|
300
|
+
queryTypes = Effect3.fn("ObjectMetaIndex.queryTypes")(({ spaceIds, typeDxns, inverted = false, includeAllQueues = false, queueIds = null }) => Effect3.gen(function* () {
|
|
301
|
+
if (spaceIds.length === 0 && (!queueIds || queueIds.length === 0)) {
|
|
251
302
|
return [];
|
|
252
303
|
}
|
|
253
304
|
if (typeDxns.length === 0) {
|
|
@@ -255,22 +306,23 @@ var ObjectMetaIndex = class {
|
|
|
255
306
|
return [];
|
|
256
307
|
}
|
|
257
308
|
const sql2 = yield* SqlClient5.SqlClient;
|
|
258
|
-
const
|
|
309
|
+
const sourceCondition2 = buildSourceCondition(sql2, spaceIds, includeAllQueues, queueIds);
|
|
310
|
+
const rows2 = yield* sql2`SELECT * FROM objectMeta WHERE ${sourceCondition2}`;
|
|
259
311
|
return rows2.map((row) => ({
|
|
260
312
|
...row,
|
|
261
313
|
deleted: !!row.deleted
|
|
262
314
|
}));
|
|
263
315
|
}
|
|
264
316
|
const sql = yield* SqlClient5.SqlClient;
|
|
265
|
-
const
|
|
266
|
-
const typeWhere = sql.or(typeDxns.map((
|
|
267
|
-
const parsedType = DXN.tryParse(
|
|
317
|
+
const sourceCondition = buildSourceCondition(sql, spaceIds, includeAllQueues, queueIds);
|
|
318
|
+
const typeWhere = sql.or(typeDxns.map((typeDXN) => {
|
|
319
|
+
const parsedType = DXN.tryParse(typeDXN)?.asTypeDXN();
|
|
268
320
|
return parsedType && parsedType.version === void 0 ? sql.or([
|
|
269
|
-
sql`
|
|
270
|
-
sql`
|
|
271
|
-
]) : sql`
|
|
321
|
+
sql`typeDXN = ${typeDXN}`,
|
|
322
|
+
sql`typeDXN LIKE ${_escapeLikePrefix(typeDXN)} ESCAPE '\\'`
|
|
323
|
+
]) : sql`typeDXN = ${typeDXN}`;
|
|
272
324
|
}));
|
|
273
|
-
const rows = inverted ? yield* sql`SELECT * FROM objectMeta WHERE ${
|
|
325
|
+
const rows = inverted ? yield* sql`SELECT * FROM objectMeta WHERE ${sourceCondition} AND NOT ${typeWhere}` : yield* sql`SELECT * FROM objectMeta WHERE ${sourceCondition} AND ${typeWhere}`;
|
|
274
326
|
return rows.map((row) => ({
|
|
275
327
|
...row,
|
|
276
328
|
deleted: !!row.deleted
|
|
@@ -292,7 +344,7 @@ var ObjectMetaIndex = class {
|
|
|
292
344
|
update = Effect3.fn("ObjectMetaIndex.update")((objects) => Effect3.gen(function* () {
|
|
293
345
|
const sql = yield* SqlClient5.SqlClient;
|
|
294
346
|
yield* Effect3.forEach(objects, (object) => Effect3.gen(function* () {
|
|
295
|
-
const { spaceId, queueId, documentId, data } = object;
|
|
347
|
+
const { spaceId, queueId, queueNamespace, documentId, data } = object;
|
|
296
348
|
const castData = data;
|
|
297
349
|
const objectId = castData.id;
|
|
298
350
|
let existing;
|
|
@@ -307,32 +359,37 @@ var ObjectMetaIndex = class {
|
|
|
307
359
|
const [{ v }] = result;
|
|
308
360
|
const version = (v ?? 0) + 1;
|
|
309
361
|
const entityKind = castData[ATTR_RELATION_SOURCE] ? "relation" : "object";
|
|
310
|
-
const
|
|
362
|
+
const typeDXN = castData[ATTR_TYPE] ? String(castData[ATTR_TYPE]) : "type";
|
|
311
363
|
const deleted = castData[ATTR_DELETED] ? 1 : 0;
|
|
312
364
|
const source = entityKind === "relation" ? castData[ATTR_RELATION_SOURCE] ?? null : null;
|
|
313
365
|
const target = entityKind === "relation" ? castData[ATTR_RELATION_TARGET] ?? null : null;
|
|
314
366
|
const parent = castData[ATTR_PARENT] ?? null;
|
|
367
|
+
const sourceTimestamp = object.updatedAt;
|
|
315
368
|
if (existing.length > 0) {
|
|
316
369
|
yield* sql`
|
|
317
370
|
UPDATE objectMeta SET
|
|
318
371
|
version = ${version},
|
|
372
|
+
queueNamespace = ${queueNamespace ?? ""},
|
|
319
373
|
entityKind = ${entityKind},
|
|
320
|
-
|
|
374
|
+
typeDXN = ${typeDXN},
|
|
321
375
|
deleted = ${deleted},
|
|
322
376
|
source = ${source},
|
|
323
377
|
target = ${target},
|
|
324
|
-
parent = ${parent}
|
|
378
|
+
parent = ${parent},
|
|
379
|
+
updatedAt = ${sourceTimestamp}
|
|
325
380
|
WHERE recordId = ${existing[0].recordId}
|
|
326
381
|
`;
|
|
327
382
|
} else {
|
|
328
383
|
yield* sql`
|
|
329
384
|
INSERT INTO objectMeta (
|
|
330
|
-
objectId, queueId, spaceId, documentId,
|
|
331
|
-
entityKind,
|
|
385
|
+
objectId, queueId, queueNamespace, spaceId, documentId,
|
|
386
|
+
entityKind, typeDXN, deleted, source, target, parent, version,
|
|
387
|
+
createdAt, updatedAt
|
|
332
388
|
) VALUES (
|
|
333
|
-
${objectId}, ${queueId ?? ""}, ${spaceId}, ${documentId ?? ""},
|
|
334
|
-
${entityKind}, ${
|
|
335
|
-
${source}, ${target}, ${parent}, ${version}
|
|
389
|
+
${objectId}, ${queueId ?? ""}, ${queueNamespace ?? ""}, ${spaceId}, ${documentId ?? ""},
|
|
390
|
+
${entityKind}, ${typeDXN}, ${deleted},
|
|
391
|
+
${source}, ${target}, ${parent}, ${version},
|
|
392
|
+
${sourceTimestamp}, ${sourceTimestamp}
|
|
336
393
|
)
|
|
337
394
|
`;
|
|
338
395
|
}
|
|
@@ -378,14 +435,61 @@ var ObjectMetaIndex = class {
|
|
|
378
435
|
}));
|
|
379
436
|
}));
|
|
380
437
|
/**
|
|
438
|
+
* Look up object metadata by objectId, spaceId, and queueId.
|
|
439
|
+
*/
|
|
440
|
+
lookupByObjectId = Effect3.fn("ObjectMetaIndex.lookupByObjectId")((query) => Effect3.gen(function* () {
|
|
441
|
+
const sql = yield* SqlClient5.SqlClient;
|
|
442
|
+
const rows = yield* sql`SELECT * FROM objectMeta WHERE spaceId = ${query.spaceId} AND queueId = ${query.queueId} AND objectId = ${query.objectId} LIMIT 1`;
|
|
443
|
+
if (rows.length === 0) {
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
return {
|
|
447
|
+
...rows[0],
|
|
448
|
+
deleted: !!rows[0].deleted
|
|
449
|
+
};
|
|
450
|
+
}));
|
|
451
|
+
/**
|
|
452
|
+
* Query objects by timestamp range.
|
|
453
|
+
*/
|
|
454
|
+
queryByTimeRange = Effect3.fn("ObjectMetaIndex.queryByTimeRange")((query) => Effect3.gen(function* () {
|
|
455
|
+
if (query.spaceIds.length === 0 && (!query.queueIds || query.queueIds.length === 0)) {
|
|
456
|
+
return [];
|
|
457
|
+
}
|
|
458
|
+
const sql = yield* SqlClient5.SqlClient;
|
|
459
|
+
const sourceCondition = buildSourceCondition(sql, query.spaceIds, query.includeAllQueues ?? false, query.queueIds ?? null);
|
|
460
|
+
const timeConditions = [];
|
|
461
|
+
if (query.updatedAfter != null) {
|
|
462
|
+
timeConditions.push(sql`updatedAt >= ${query.updatedAfter}`);
|
|
463
|
+
}
|
|
464
|
+
if (query.updatedBefore != null) {
|
|
465
|
+
timeConditions.push(sql`updatedAt <= ${query.updatedBefore}`);
|
|
466
|
+
}
|
|
467
|
+
if (query.createdAfter != null) {
|
|
468
|
+
timeConditions.push(sql`createdAt >= ${query.createdAfter}`);
|
|
469
|
+
}
|
|
470
|
+
if (query.createdBefore != null) {
|
|
471
|
+
timeConditions.push(sql`createdAt <= ${query.createdBefore}`);
|
|
472
|
+
}
|
|
473
|
+
const rows = timeConditions.length > 0 ? yield* sql`SELECT * FROM objectMeta WHERE ${sourceCondition} AND ${sql.and(timeConditions)}` : yield* sql`SELECT * FROM objectMeta WHERE ${sourceCondition}`;
|
|
474
|
+
return rows.map((row) => ({
|
|
475
|
+
...row,
|
|
476
|
+
deleted: !!row.deleted
|
|
477
|
+
}));
|
|
478
|
+
}));
|
|
479
|
+
/**
|
|
381
480
|
* Query children by parent object ids.
|
|
481
|
+
* Matches both:
|
|
482
|
+
* - Objects whose `parent` field references one of the given parent ids (standard parent/child hierarchy).
|
|
483
|
+
* - Queue items whose `queueId` equals one of the parent ids (e.g. items inside a Feed, since a feed's queue
|
|
484
|
+
* DXN uses the feed's object id as its queue id — see `Feed.getQueueDxn`).
|
|
382
485
|
*/
|
|
383
486
|
queryChildren = Effect3.fn("ObjectMetaIndex.queryChildren")((query) => Effect3.gen(function* () {
|
|
384
487
|
if (query.parentIds.length === 0) {
|
|
385
488
|
return [];
|
|
386
489
|
}
|
|
387
490
|
const sql = yield* SqlClient5.SqlClient;
|
|
388
|
-
const
|
|
491
|
+
const parentDxns = query.parentIds.map((id) => DXN.fromLocalObjectId(id).toString());
|
|
492
|
+
const rows = yield* sql`SELECT * FROM objectMeta WHERE ${sql.in("spaceId", query.spaceId)} AND (${sql.in("parent", parentDxns)} OR ${sql.in("queueId", query.parentIds)})`;
|
|
389
493
|
return rows.map((row) => ({
|
|
390
494
|
...row,
|
|
391
495
|
deleted: !!row.deleted
|
|
@@ -414,15 +518,7 @@ var EscapedPropPath = class extends Schema3.String.annotations({
|
|
|
414
518
|
let current = "";
|
|
415
519
|
for (let i = 0; i < path.length; i++) {
|
|
416
520
|
if (path[i] === "\\") {
|
|
417
|
-
invariant(i + 1 < path.length && (path[i + 1] === "." || path[i + 1] === "\\"), "Malformed escaping.", {
|
|
418
|
-
F: __dxlog_file,
|
|
419
|
-
L: 34,
|
|
420
|
-
S: this,
|
|
421
|
-
A: [
|
|
422
|
-
"i + 1 < path.length && (path[i + 1] === '.' || path[i + 1] === '\\\\')",
|
|
423
|
-
"'Malformed escaping.'"
|
|
424
|
-
]
|
|
425
|
-
});
|
|
521
|
+
invariant(i + 1 < path.length && (path[i + 1] === "." || path[i + 1] === "\\"), "Malformed escaping.", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 25, S: this, A: ["i + 1 < path.length && (path[i + 1] === '.' || path[i + 1] === '\\\\')", "'Malformed escaping.'"] });
|
|
426
522
|
current = current + path[i + 1];
|
|
427
523
|
i++;
|
|
428
524
|
} else if (path[i] === ".") {
|
|
@@ -449,7 +545,7 @@ var extractReferences = (data) => {
|
|
|
449
545
|
}
|
|
450
546
|
refs.push({
|
|
451
547
|
path,
|
|
452
|
-
|
|
548
|
+
targetDXN: dxn.toString()
|
|
453
549
|
});
|
|
454
550
|
} else if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
455
551
|
for (const [key, v] of Object.entries(value)) {
|
|
@@ -472,7 +568,7 @@ var extractReferences = (data) => {
|
|
|
472
568
|
};
|
|
473
569
|
var ReverseRef = Schema4.Struct({
|
|
474
570
|
recordId: Schema4.Number,
|
|
475
|
-
|
|
571
|
+
targetDXN: Schema4.String,
|
|
476
572
|
/**
|
|
477
573
|
* Escaped property path within an object.
|
|
478
574
|
*
|
|
@@ -489,18 +585,18 @@ var ReverseRefIndex = class {
|
|
|
489
585
|
const sql = yield* SqlClient7.SqlClient;
|
|
490
586
|
yield* sql`CREATE TABLE IF NOT EXISTS reverseRef (
|
|
491
587
|
recordId INTEGER NOT NULL,
|
|
492
|
-
|
|
588
|
+
targetDXN TEXT NOT NULL,
|
|
493
589
|
propPath TEXT NOT NULL,
|
|
494
|
-
PRIMARY KEY (recordId,
|
|
590
|
+
PRIMARY KEY (recordId, targetDXN, propPath)
|
|
495
591
|
)`;
|
|
496
|
-
yield* sql`CREATE INDEX IF NOT EXISTS idx_reverse_ref_target ON reverseRef(
|
|
592
|
+
yield* sql`CREATE INDEX IF NOT EXISTS idx_reverse_ref_target ON reverseRef(targetDXN)`;
|
|
497
593
|
});
|
|
498
594
|
/**
|
|
499
595
|
* Query all references pointing to a target DXN.
|
|
500
596
|
*/
|
|
501
|
-
query = Effect4.fn("ReverseRefIndex.query")(({
|
|
597
|
+
query = Effect4.fn("ReverseRefIndex.query")(({ targetDXN }) => Effect4.gen(function* () {
|
|
502
598
|
const sql = yield* SqlClient7.SqlClient;
|
|
503
|
-
const rows = yield* sql`SELECT * FROM reverseRef WHERE
|
|
599
|
+
const rows = yield* sql`SELECT * FROM reverseRef WHERE targetDXN = ${targetDXN}`;
|
|
504
600
|
return rows;
|
|
505
601
|
}));
|
|
506
602
|
update = Effect4.fn("ReverseRefIndex.update")((objects) => Effect4.gen(function* () {
|
|
@@ -512,7 +608,7 @@ var ReverseRefIndex = class {
|
|
|
512
608
|
}
|
|
513
609
|
yield* sql`DELETE FROM reverseRef WHERE recordId = ${recordId}`;
|
|
514
610
|
const refs = extractReferences(data);
|
|
515
|
-
yield* Effect4.forEach(refs, (ref) => sql`INSERT INTO reverseRef (recordId,
|
|
611
|
+
yield* Effect4.forEach(refs, (ref) => sql`INSERT INTO reverseRef (recordId, targetDXN, propPath) VALUES (${recordId}, ${ref.targetDXN}, ${EscapedPropPath.escape(ref.path)})`, {
|
|
516
612
|
discard: true
|
|
517
613
|
});
|
|
518
614
|
}), {
|
|
@@ -522,6 +618,33 @@ var ReverseRefIndex = class {
|
|
|
522
618
|
};
|
|
523
619
|
|
|
524
620
|
// src/index-engine.ts
|
|
621
|
+
var makeEmptyIndexingResult = () => ({
|
|
622
|
+
updated: 0,
|
|
623
|
+
done: true,
|
|
624
|
+
spaces: /* @__PURE__ */ new Set(),
|
|
625
|
+
queues: /* @__PURE__ */ new Set(),
|
|
626
|
+
documents: /* @__PURE__ */ new Set(),
|
|
627
|
+
types: /* @__PURE__ */ new Set(),
|
|
628
|
+
objects: /* @__PURE__ */ new Set()
|
|
629
|
+
});
|
|
630
|
+
var accumulateIndexingResult = (acc, objects) => {
|
|
631
|
+
for (const obj of objects) {
|
|
632
|
+
acc.spaces.add(obj.spaceId);
|
|
633
|
+
if (obj.queueId) {
|
|
634
|
+
acc.queues.add(obj.queueId);
|
|
635
|
+
}
|
|
636
|
+
if (obj.documentId) {
|
|
637
|
+
acc.documents.add(obj.documentId);
|
|
638
|
+
}
|
|
639
|
+
const t = obj.data[ATTR_TYPE2];
|
|
640
|
+
if (t) {
|
|
641
|
+
acc.types.add(String(t));
|
|
642
|
+
}
|
|
643
|
+
if (obj.data.id) {
|
|
644
|
+
acc.objects.add(obj.data.id);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
};
|
|
525
648
|
var IndexEngine = class {
|
|
526
649
|
#tracker;
|
|
527
650
|
#objectMetaIndex;
|
|
@@ -574,31 +697,38 @@ var IndexEngine = class {
|
|
|
574
697
|
queryTypes(query) {
|
|
575
698
|
return this.#objectMetaIndex.queryTypes(query);
|
|
576
699
|
}
|
|
700
|
+
queryByTimeRange(query) {
|
|
701
|
+
return this.#objectMetaIndex.queryByTimeRange(query);
|
|
702
|
+
}
|
|
577
703
|
queryRelations(query) {
|
|
578
704
|
return this.#objectMetaIndex.queryRelations(query);
|
|
579
705
|
}
|
|
580
706
|
lookupByRecordIds(recordIds) {
|
|
581
707
|
return this.#objectMetaIndex.lookupByRecordIds(recordIds);
|
|
582
708
|
}
|
|
583
|
-
|
|
709
|
+
lookupByObjectId(query) {
|
|
710
|
+
return this.#objectMetaIndex.lookupByObjectId(query);
|
|
711
|
+
}
|
|
712
|
+
update(ctx, dataSource, opts) {
|
|
584
713
|
return Effect5.gen(this, function* () {
|
|
585
|
-
|
|
586
|
-
const { updated: updatedFtsIndex, done: doneFtsIndex } = yield* this.#update(this.#ftsIndex, dataSource, {
|
|
587
|
-
indexName: "
|
|
714
|
+
const result = makeEmptyIndexingResult();
|
|
715
|
+
const { updated: updatedFtsIndex, done: doneFtsIndex, objects: ftsObjects } = yield* this.#update(ctx, this.#ftsIndex, dataSource, {
|
|
716
|
+
indexName: "fts5",
|
|
588
717
|
spaceId: opts.spaceId,
|
|
589
718
|
limit: opts.limit
|
|
590
719
|
});
|
|
591
|
-
updated += updatedFtsIndex;
|
|
592
|
-
|
|
720
|
+
result.updated += updatedFtsIndex;
|
|
721
|
+
result.done = result.done && doneFtsIndex;
|
|
722
|
+
accumulateIndexingResult(result, ftsObjects);
|
|
723
|
+
const { updated: updatedReverseRefIndex, done: doneReverseRefIndex, objects: reverseRefObjects } = yield* this.#update(ctx, this.#reverseRefIndex, dataSource, {
|
|
593
724
|
indexName: "reverseRef",
|
|
594
725
|
spaceId: opts.spaceId,
|
|
595
726
|
limit: opts.limit
|
|
596
727
|
});
|
|
597
|
-
updated += updatedReverseRefIndex;
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
};
|
|
728
|
+
result.updated += updatedReverseRefIndex;
|
|
729
|
+
result.done = result.done && doneReverseRefIndex;
|
|
730
|
+
accumulateIndexingResult(result, reverseRefObjects);
|
|
731
|
+
return result;
|
|
602
732
|
}).pipe(Effect5.withSpan("IndexEngine.update"));
|
|
603
733
|
}
|
|
604
734
|
/**
|
|
@@ -610,7 +740,7 @@ var IndexEngine = class {
|
|
|
610
740
|
* 4. Enriches objects with recordIds.
|
|
611
741
|
* 5. Updates the dependent index.
|
|
612
742
|
*/
|
|
613
|
-
#update(index, source, opts) {
|
|
743
|
+
#update(ctx, index, source, opts) {
|
|
614
744
|
return Effect5.gen(this, function* () {
|
|
615
745
|
const sqlTransaction = yield* SqlTransaction.SqlTransaction;
|
|
616
746
|
return yield* sqlTransaction.withTransaction(Effect5.gen(this, function* () {
|
|
@@ -620,13 +750,14 @@ var IndexEngine = class {
|
|
|
620
750
|
// Pass undefined to get all cursors when spaceId is null.
|
|
621
751
|
spaceId: opts.spaceId ?? void 0
|
|
622
752
|
});
|
|
623
|
-
const { objects, cursors: updatedCursors } = yield* source.getChangedObjects(cursors, {
|
|
753
|
+
const { objects, cursors: updatedCursors } = yield* source.getChangedObjects(ctx, cursors, {
|
|
624
754
|
limit: opts.limit
|
|
625
755
|
});
|
|
626
756
|
if (objects.length === 0) {
|
|
627
757
|
return {
|
|
628
758
|
updated: 0,
|
|
629
|
-
done: true
|
|
759
|
+
done: true,
|
|
760
|
+
objects: []
|
|
630
761
|
};
|
|
631
762
|
}
|
|
632
763
|
yield* this.#objectMetaIndex.update(objects);
|
|
@@ -641,7 +772,8 @@ var IndexEngine = class {
|
|
|
641
772
|
})));
|
|
642
773
|
return {
|
|
643
774
|
updated: objects.length,
|
|
644
|
-
done: false
|
|
775
|
+
done: false,
|
|
776
|
+
objects
|
|
645
777
|
};
|
|
646
778
|
}));
|
|
647
779
|
}).pipe(Effect5.withSpan("IndexEngine.#update"));
|