mindoodb 0.2.4 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README-server.md +4 -3
- package/dist/browser/cache/IndexedDBLocalCacheStore.d.ts +1 -0
- package/dist/browser/cache/IndexedDBLocalCacheStore.d.ts.map +1 -1
- package/dist/browser/cache/IndexedDBLocalCacheStore.js +18 -0
- package/dist/browser/cache/IndexedDBLocalCacheStore.js.map +1 -1
- package/dist/browser/index.mjs +21 -0
- package/dist/cloudflare/http.d.ts +1 -0
- package/dist/cloudflare/http.d.ts.map +1 -1
- package/dist/cloudflare/http.js +3 -4
- package/dist/cloudflare/http.js.map +1 -1
- package/dist/core/BaseMindooDB.d.ts +102 -39
- package/dist/core/BaseMindooDB.d.ts.map +1 -1
- package/dist/core/BaseMindooDB.js +585 -280
- package/dist/core/BaseMindooDB.js.map +1 -1
- package/dist/core/BaseMindooTenant.d.ts +2 -0
- package/dist/core/BaseMindooTenant.d.ts.map +1 -1
- package/dist/core/BaseMindooTenant.js +22 -1
- package/dist/core/BaseMindooTenant.js.map +1 -1
- package/dist/core/appendonlystores/rpc/RpcContentAddressedStore.d.ts +7 -0
- package/dist/core/appendonlystores/rpc/RpcContentAddressedStore.d.ts.map +1 -1
- package/dist/core/appendonlystores/rpc/RpcContentAddressedStore.js +7 -1
- package/dist/core/appendonlystores/rpc/RpcContentAddressedStore.js.map +1 -1
- package/dist/core/appendonlystores/rpc/SqlRpc.d.ts +116 -0
- package/dist/core/appendonlystores/rpc/SqlRpc.d.ts.map +1 -0
- package/dist/core/appendonlystores/rpc/SqlRpc.js +276 -0
- package/dist/core/appendonlystores/rpc/SqlRpc.js.map +1 -0
- package/dist/core/appendonlystores/rpc/StoreRpcServer.d.ts +6 -2
- package/dist/core/appendonlystores/rpc/StoreRpcServer.d.ts.map +1 -1
- package/dist/core/appendonlystores/rpc/StoreRpcServer.js +1 -1
- package/dist/core/appendonlystores/rpc/StoreRpcServer.js.map +1 -1
- package/dist/core/appendonlystores/sqlite/SqliteDriver.d.ts +0 -6
- package/dist/core/appendonlystores/sqlite/SqliteDriver.d.ts.map +1 -1
- package/dist/core/appendonlystores/sqlite/SqliteDriver.js +16 -1
- package/dist/core/appendonlystores/sqlite/SqliteDriver.js.map +1 -1
- package/dist/core/appendonlystores/sqlite/sqliteSchema.d.ts +59 -0
- package/dist/core/appendonlystores/sqlite/sqliteSchema.d.ts.map +1 -0
- package/dist/core/appendonlystores/sqlite/sqliteSchema.js +64 -0
- package/dist/core/appendonlystores/sqlite/sqliteSchema.js.map +1 -0
- package/dist/core/cache/DocStateTable.d.ts +144 -0
- package/dist/core/cache/DocStateTable.d.ts.map +1 -0
- package/dist/core/cache/DocStateTable.js +398 -0
- package/dist/core/cache/DocStateTable.js.map +1 -0
- package/dist/core/cache/EncryptedLocalCacheStore.d.ts +11 -0
- package/dist/core/cache/EncryptedLocalCacheStore.d.ts.map +1 -1
- package/dist/core/cache/EncryptedLocalCacheStore.js +20 -0
- package/dist/core/cache/EncryptedLocalCacheStore.js.map +1 -1
- package/dist/core/cache/FulltextTable.d.ts +89 -0
- package/dist/core/cache/FulltextTable.d.ts.map +1 -0
- package/dist/core/cache/FulltextTable.js +225 -0
- package/dist/core/cache/FulltextTable.js.map +1 -0
- package/dist/core/cache/LocalCacheStore.d.ts +16 -0
- package/dist/core/cache/LocalCacheStore.d.ts.map +1 -1
- package/dist/core/cache/LocalCacheStore.js +11 -0
- package/dist/core/cache/LocalCacheStore.js.map +1 -1
- package/dist/core/cache/SqliteLocalCacheStore.d.ts +34 -0
- package/dist/core/cache/SqliteLocalCacheStore.d.ts.map +1 -0
- package/dist/core/cache/SqliteLocalCacheStore.js +104 -0
- package/dist/core/cache/SqliteLocalCacheStore.js.map +1 -0
- package/dist/core/cache/SummaryTable.d.ts +140 -0
- package/dist/core/cache/SummaryTable.d.ts.map +1 -0
- package/dist/core/cache/SummaryTable.js +374 -0
- package/dist/core/cache/SummaryTable.js.map +1 -0
- package/dist/core/cache/createSqliteCacheStore.d.ts +27 -0
- package/dist/core/cache/createSqliteCacheStore.d.ts.map +1 -0
- package/dist/core/cache/createSqliteCacheStore.js +26 -0
- package/dist/core/cache/createSqliteCacheStore.js.map +1 -0
- package/dist/core/collation/collationKey.d.ts +41 -0
- package/dist/core/collation/collationKey.d.ts.map +1 -0
- package/dist/core/collation/collationKey.js +475 -0
- package/dist/core/collation/collationKey.js.map +1 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +34 -3
- package/dist/core/index.js.map +1 -1
- package/dist/core/indexing/ChangefeedIndex.d.ts +98 -0
- package/dist/core/indexing/ChangefeedIndex.d.ts.map +1 -0
- package/dist/core/indexing/ChangefeedIndex.js +284 -0
- package/dist/core/indexing/ChangefeedIndex.js.map +1 -0
- package/dist/core/indexing/fulltext/DocumentFullTextIndex.d.ts +26 -2
- package/dist/core/indexing/fulltext/DocumentFullTextIndex.d.ts.map +1 -1
- package/dist/core/indexing/fulltext/DocumentFullTextIndex.js +143 -3
- package/dist/core/indexing/fulltext/DocumentFullTextIndex.js.map +1 -1
- package/dist/core/indexing/fulltext/TableSearchEngine.d.ts +60 -0
- package/dist/core/indexing/fulltext/TableSearchEngine.d.ts.map +1 -0
- package/dist/core/indexing/fulltext/TableSearchEngine.js +150 -0
- package/dist/core/indexing/fulltext/TableSearchEngine.js.map +1 -0
- package/dist/core/indexing/summary/DocumentSummaryStore.d.ts +112 -1
- package/dist/core/indexing/summary/DocumentSummaryStore.d.ts.map +1 -1
- package/dist/core/indexing/summary/DocumentSummaryStore.js +435 -12
- package/dist/core/indexing/summary/DocumentSummaryStore.js.map +1 -1
- package/dist/core/indexing/summary/SummaryVirtualViewDataProvider.d.ts.map +1 -1
- package/dist/core/indexing/summary/SummaryVirtualViewDataProvider.js +3 -1
- package/dist/core/indexing/summary/SummaryVirtualViewDataProvider.js.map +1 -1
- package/dist/core/indexing/summary/summarySortKey.d.ts +12 -0
- package/dist/core/indexing/summary/summarySortKey.d.ts.map +1 -0
- package/dist/core/indexing/summary/summarySortKey.js +67 -0
- package/dist/core/indexing/summary/summarySortKey.js.map +1 -0
- package/dist/core/indexing/summary/summarySql.d.ts +72 -0
- package/dist/core/indexing/summary/summarySql.d.ts.map +1 -0
- package/dist/core/indexing/summary/summarySql.js +377 -0
- package/dist/core/indexing/summary/summarySql.js.map +1 -0
- package/dist/core/indexing/summary/types.d.ts +20 -0
- package/dist/core/indexing/summary/types.d.ts.map +1 -1
- package/dist/core/indexing/summary/types.js +25 -1
- package/dist/core/indexing/summary/types.js.map +1 -1
- package/dist/core/query/executeQuery.d.ts +2 -0
- package/dist/core/query/executeQuery.d.ts.map +1 -1
- package/dist/core/query/executeQuery.js +126 -9
- package/dist/core/query/executeQuery.js.map +1 -1
- package/dist/core/query/executeQueryInclude.d.ts.map +1 -1
- package/dist/core/query/executeQueryInclude.js +2 -0
- package/dist/core/query/executeQueryInclude.js.map +1 -1
- package/dist/core/types.d.ts +31 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/userkeys/UserKeyManager.d.ts +8 -0
- package/dist/core/userkeys/UserKeyManager.d.ts.map +1 -1
- package/dist/core/userkeys/UserKeyManager.js +12 -0
- package/dist/core/userkeys/UserKeyManager.js.map +1 -1
- package/dist/node/index.d.ts +1 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +5 -1
- package/dist/node/index.js.map +1 -1
- package/dist/node/server/MindooDBServer.d.ts +0 -2
- package/dist/node/server/MindooDBServer.d.ts.map +1 -1
- package/dist/node/server/MindooDBServer.js +6 -17
- package/dist/node/server/MindooDBServer.js.map +1 -1
- package/dist/server/corsOrigins.d.ts +15 -0
- package/dist/server/corsOrigins.d.ts.map +1 -0
- package/dist/server/corsOrigins.js +36 -0
- package/dist/server/corsOrigins.js.map +1 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +4 -1
- package/dist/server/index.js.map +1 -1
- package/dist/sqlite/index.d.ts +2 -0
- package/dist/sqlite/index.d.ts.map +1 -1
- package/dist/sqlite/index.js +11 -1
- package/dist/sqlite/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DocumentSummaryStore = void 0;
|
|
4
4
|
const feedCursor_1 = require("../../utils/feedCursor");
|
|
5
|
+
const SummaryTable_1 = require("../../cache/SummaryTable");
|
|
5
6
|
const idGeneration_1 = require("../../utils/idGeneration");
|
|
6
7
|
const types_1 = require("./types");
|
|
7
8
|
const extractSummaryFields_1 = require("./extractSummaryFields");
|
|
@@ -11,6 +12,8 @@ const extractSummaryFields_1 = require("./extractSummaryFields");
|
|
|
11
12
|
*/
|
|
12
13
|
const SCAN = { persistLoadedDocuments: false };
|
|
13
14
|
const DEFAULT_BUCKET_COUNT = 64;
|
|
15
|
+
/** Rows read per call when the summary is restored from a SummaryTable. */
|
|
16
|
+
const TABLE_READ_PAGE = 5000;
|
|
14
17
|
/** Keep at most this many removals for delta readers (older readers rescan). */
|
|
15
18
|
const MAX_TOMBSTONES = 5000;
|
|
16
19
|
/** Sequence jump on restore (see lostSeqRanges). */
|
|
@@ -23,6 +26,12 @@ const LOST_SEQ_JUMP = 2 ** 30;
|
|
|
23
26
|
* long catch-up. Idle flushes and forced (shutdown) flushes are not deferred.
|
|
24
27
|
*/
|
|
25
28
|
const DEFAULT_MIN_FLUSH_INTERVAL_DURING_UPDATE_MS = 30000;
|
|
29
|
+
/**
|
|
30
|
+
* Most documents prepared from local writes (see prepareDocument) kept until
|
|
31
|
+
* an update run takes them. A larger backlog (auto-update off, a long bulk
|
|
32
|
+
* import) drops the oldest: those are loaded from the store as before.
|
|
33
|
+
*/
|
|
34
|
+
const MAX_PREPARED_DOCUMENTS = 20000;
|
|
26
35
|
/** Simple deterministic string hash (djb2) used for bucket assignment. */
|
|
27
36
|
function hashDocId(docId) {
|
|
28
37
|
let hash = 5381;
|
|
@@ -71,6 +80,31 @@ class DocumentSummaryStore {
|
|
|
71
80
|
// --- persistence state ---
|
|
72
81
|
this.cacheManager = null;
|
|
73
82
|
this.cachePrefix = null;
|
|
83
|
+
/**
|
|
84
|
+
* Whether the summary may keep its rows in the cache store's SummaryTable
|
|
85
|
+
* (see attachCache). With a table, flushes write changed rows instead of
|
|
86
|
+
* bucket snapshots and journal segments, and queries filter and sort in SQL.
|
|
87
|
+
*/
|
|
88
|
+
this.tableAllowed = false;
|
|
89
|
+
/** The table holds exactly the entries in memory as of the last table write. */
|
|
90
|
+
this.tableInSync = false;
|
|
91
|
+
/** JSON of the sort fields the table rows carry keys for (`null`: none usable). */
|
|
92
|
+
this.tableSortFields = null;
|
|
93
|
+
/** Bucket/journal records of this summary may still exist (removed after the first full table write). */
|
|
94
|
+
this.legacyRecordsMayExist = true;
|
|
95
|
+
/** Table writes, one after the other. */
|
|
96
|
+
this.tableWrites = Promise.resolve();
|
|
97
|
+
/**
|
|
98
|
+
* Restored from a SummaryTable without reading the rows: `entries` holds
|
|
99
|
+
* only the entries written since, `lazyRemoved` the documents removed
|
|
100
|
+
* since. Queries read the rows they need from the table; whatever iterates
|
|
101
|
+
* every entry calls {@link ensureAllEntries} first.
|
|
102
|
+
*/
|
|
103
|
+
this.lazy = false;
|
|
104
|
+
this.lazyRemoved = new Set();
|
|
105
|
+
/** Number of rows in the table, as of the last table write (lazy mode; the table counts them). */
|
|
106
|
+
this.lazyCount = 0;
|
|
107
|
+
this.loadAllPromise = null;
|
|
74
108
|
this.bucketCount = DEFAULT_BUCKET_COUNT;
|
|
75
109
|
/** Buckets changed since the last snapshot (rewritten by the next snapshot). */
|
|
76
110
|
this.dirtyBuckets = new Set();
|
|
@@ -100,6 +134,15 @@ class DocumentSummaryStore {
|
|
|
100
134
|
this.backfillGeneration = 0;
|
|
101
135
|
// --- single-flight update ---
|
|
102
136
|
this.updatePromise = null;
|
|
137
|
+
/** Documents written locally, extracted at write time (see prepareDocument). */
|
|
138
|
+
this.prepared = new Map();
|
|
139
|
+
/** Changefeed hook: the prepared entry for this document state, if any. */
|
|
140
|
+
this.preparedLookup = (docId, changeSeq) => {
|
|
141
|
+
const entry = this.prepared.get(docId);
|
|
142
|
+
return entry && entry.changeSeq === changeSeq && entry.configFingerprint === this.configFingerprint
|
|
143
|
+
? entry
|
|
144
|
+
: undefined;
|
|
145
|
+
};
|
|
103
146
|
this.db = db;
|
|
104
147
|
this.minFlushIntervalDuringUpdateMs = Math.max(0, options?.minFlushIntervalDuringUpdateMs ?? DEFAULT_MIN_FLUSH_INTERVAL_DURING_UPDATE_MS);
|
|
105
148
|
this.hasExplicitConfig = config !== undefined;
|
|
@@ -139,6 +182,11 @@ class DocumentSummaryStore {
|
|
|
139
182
|
if (!scope) {
|
|
140
183
|
return;
|
|
141
184
|
}
|
|
185
|
+
if (this.lazy) {
|
|
186
|
+
// Rows outside the scope may be in the table: the next table write
|
|
187
|
+
// reads every row first (ensureAllEntries prunes) and rewrites them.
|
|
188
|
+
this.tableInSync = false;
|
|
189
|
+
}
|
|
142
190
|
for (const docId of [...this.entries.keys()]) {
|
|
143
191
|
if (!(0, idGeneration_1.matchesAnyDocIdPrefix)(docId, scope)) {
|
|
144
192
|
this.entries.delete(docId);
|
|
@@ -164,10 +212,17 @@ class DocumentSummaryStore {
|
|
|
164
212
|
applyConfig(config) {
|
|
165
213
|
const resolved = (0, types_1.resolveSummaryConfig)(config);
|
|
166
214
|
const fingerprint = (0, types_1.computeSummaryConfigFingerprint)(resolved);
|
|
215
|
+
const indexesChanged = JSON.stringify(resolved.indexes) !== JSON.stringify(this.config.indexes);
|
|
216
|
+
this.config = resolved;
|
|
217
|
+
if (indexesChanged) {
|
|
218
|
+
// Indexes never re-extract documents; changed sort fields rewrite the
|
|
219
|
+
// rows on the next table write (see tableNeedsFullWrite).
|
|
220
|
+
this.metaDirty = true;
|
|
221
|
+
this.cacheManager?.markDirty();
|
|
222
|
+
}
|
|
167
223
|
if (fingerprint === this.configFingerprint) {
|
|
168
224
|
return;
|
|
169
225
|
}
|
|
170
|
-
this.config = resolved;
|
|
171
226
|
this.configFingerprint = fingerprint;
|
|
172
227
|
this.pruneOutOfScope();
|
|
173
228
|
if (this.entries.size > 0 || this.cursor !== null) {
|
|
@@ -238,14 +293,109 @@ class DocumentSummaryStore {
|
|
|
238
293
|
// ---------------------------------------------------------------------------
|
|
239
294
|
// Reads
|
|
240
295
|
// ---------------------------------------------------------------------------
|
|
296
|
+
/**
|
|
297
|
+
* The entry of a document. After a restore from a SummaryTable it is only
|
|
298
|
+
* known once {@link ensureAllEntries} ran (or when written since): use
|
|
299
|
+
* {@link getEntries} for documents that may only be in the table.
|
|
300
|
+
*/
|
|
241
301
|
getEntry(docId) {
|
|
242
302
|
return this.entries.get(docId);
|
|
243
303
|
}
|
|
304
|
+
/** Every entry. After a restore from a SummaryTable, call {@link ensureAllEntries} first. */
|
|
244
305
|
getAllEntries() {
|
|
306
|
+
if (this.lazy) {
|
|
307
|
+
throw new Error("The summary entries are still in the summary table: await ensureAllEntries() before iterating them.");
|
|
308
|
+
}
|
|
245
309
|
return this.entries.values();
|
|
246
310
|
}
|
|
311
|
+
/**
|
|
312
|
+
* Number of entries. After a restore from a SummaryTable (until
|
|
313
|
+
* {@link ensureAllEntries}), the number of rows as of the last table
|
|
314
|
+
* write.
|
|
315
|
+
*/
|
|
247
316
|
getSize() {
|
|
248
|
-
return this.entries.size;
|
|
317
|
+
return this.lazy ? this.lazyCount : this.entries.size;
|
|
318
|
+
}
|
|
319
|
+
/** Whether the entries are all in memory (see {@link ensureAllEntries}). */
|
|
320
|
+
hasAllEntries() {
|
|
321
|
+
return !this.lazy;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* The entries of `docIds` that exist, read from the table where they are
|
|
325
|
+
* not in memory.
|
|
326
|
+
*/
|
|
327
|
+
async getEntries(docIds) {
|
|
328
|
+
const result = new Map();
|
|
329
|
+
const missing = [];
|
|
330
|
+
for (const docId of docIds) {
|
|
331
|
+
const entry = this.entries.get(docId);
|
|
332
|
+
if (entry)
|
|
333
|
+
result.set(docId, entry);
|
|
334
|
+
else if (this.lazy && !this.lazyRemoved.has(docId))
|
|
335
|
+
missing.push(docId);
|
|
336
|
+
}
|
|
337
|
+
const table = missing.length > 0 ? this.getTable() : null;
|
|
338
|
+
if (table) {
|
|
339
|
+
for (const entry of await table.readEntriesByIds(this.getCachePrefix(), missing)) {
|
|
340
|
+
// Written or removed while the rows were read: memory is newer.
|
|
341
|
+
if (!this.entries.has(entry.docId) && !this.lazyRemoved.has(entry.docId)) {
|
|
342
|
+
result.set(entry.docId, entry);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return result;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Read every row of the summary table into memory (after a restore from
|
|
350
|
+
* the table), for readers that iterate every entry: views, include
|
|
351
|
+
* lookups and queries no index answers.
|
|
352
|
+
*/
|
|
353
|
+
async ensureAllEntries() {
|
|
354
|
+
await this.ensureRestored();
|
|
355
|
+
if (!this.lazy)
|
|
356
|
+
return;
|
|
357
|
+
if (!this.loadAllPromise) {
|
|
358
|
+
this.loadAllPromise = this.loadAllFromTable().finally(() => {
|
|
359
|
+
this.loadAllPromise = null;
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
return this.loadAllPromise;
|
|
363
|
+
}
|
|
364
|
+
async loadAllFromTable() {
|
|
365
|
+
const table = this.getTable();
|
|
366
|
+
const prefix = this.getCachePrefix();
|
|
367
|
+
if (table) {
|
|
368
|
+
let after = null;
|
|
369
|
+
for (;;) {
|
|
370
|
+
const page = await table.readEntries(prefix, after, TABLE_READ_PAGE);
|
|
371
|
+
if (!this.lazy)
|
|
372
|
+
return;
|
|
373
|
+
for (const entry of page) {
|
|
374
|
+
// Entries written or removed since the restore are newer.
|
|
375
|
+
if (!this.entries.has(entry.docId) && !this.lazyRemoved.has(entry.docId)) {
|
|
376
|
+
this.entries.set(entry.docId, this.interner.internEntry(entry));
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (page.length < TABLE_READ_PAGE)
|
|
380
|
+
break;
|
|
381
|
+
after = page[page.length - 1].docId;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (!this.lazy)
|
|
385
|
+
return;
|
|
386
|
+
this.lazy = false;
|
|
387
|
+
this.lazyRemoved.clear();
|
|
388
|
+
this.rebuildSeqLog();
|
|
389
|
+
this.pruneOutOfScope();
|
|
390
|
+
}
|
|
391
|
+
/** Remove a document's entry; returns whether there may have been one. */
|
|
392
|
+
deleteEntry(docId) {
|
|
393
|
+
const deleted = this.entries.delete(docId);
|
|
394
|
+
if (!this.lazy)
|
|
395
|
+
return deleted;
|
|
396
|
+
// The row may be in the table.
|
|
397
|
+
this.lazyRemoved.add(docId);
|
|
398
|
+
return true;
|
|
249
399
|
}
|
|
250
400
|
getCursor() {
|
|
251
401
|
return this.cursor;
|
|
@@ -314,8 +464,16 @@ class DocumentSummaryStore {
|
|
|
314
464
|
return;
|
|
315
465
|
}
|
|
316
466
|
// Pass 1: incremental changefeed consumption from the main cursor.
|
|
317
|
-
|
|
318
|
-
|
|
467
|
+
// Documents written locally since the last run were extracted when they
|
|
468
|
+
// were written: the feed hands back that result instead of loading them.
|
|
469
|
+
const pass1Options = { ...this.scanOptions(), preparedDocument: this.preparedLookup };
|
|
470
|
+
for await (const { doc, cursor, prepared } of this.db.iterateChangesSince(this.cursor, pass1Options)) {
|
|
471
|
+
if (prepared) {
|
|
472
|
+
this.applyPrepared(doc.getId(), prepared, cursor);
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
this.applyDocument(doc.getId(), doc, cursor);
|
|
476
|
+
}
|
|
319
477
|
this.cursor = cursor;
|
|
320
478
|
processed++;
|
|
321
479
|
processedInBatch++;
|
|
@@ -377,14 +535,14 @@ class DocumentSummaryStore {
|
|
|
377
535
|
this.applyConfig(config);
|
|
378
536
|
}
|
|
379
537
|
// Drop a stale entry left over from before the setup doc was skipped.
|
|
380
|
-
if (this.
|
|
538
|
+
if (this.deleteEntry(docId)) {
|
|
381
539
|
this.recordRemoval(docId);
|
|
382
540
|
this.markBucketDirty(docId);
|
|
383
541
|
}
|
|
384
542
|
return;
|
|
385
543
|
}
|
|
386
544
|
if (doc.isDeleted()) {
|
|
387
|
-
if (this.
|
|
545
|
+
if (this.deleteEntry(docId)) {
|
|
388
546
|
this.recordRemoval(docId);
|
|
389
547
|
this.markBucketDirty(docId);
|
|
390
548
|
}
|
|
@@ -396,19 +554,70 @@ class DocumentSummaryStore {
|
|
|
396
554
|
if (scope && !(0, idGeneration_1.matchesAnyDocIdPrefix)(docId, scope)) {
|
|
397
555
|
return;
|
|
398
556
|
}
|
|
557
|
+
this.prepared.delete(docId);
|
|
558
|
+
this.setEntry(docId, (0, extractSummaryFields_1.extractSummaryFields)(doc.getData(), this.config), doc.getLastModified(), cursor.changeSeq ?? 0, doc.getDecryptionKeyId());
|
|
559
|
+
}
|
|
560
|
+
/** Apply a document extracted at write time (see prepareDocument). */
|
|
561
|
+
applyPrepared(docId, prepared, cursor) {
|
|
562
|
+
if (this.prepared.get(docId) === prepared) {
|
|
563
|
+
this.prepared.delete(docId);
|
|
564
|
+
}
|
|
565
|
+
const scope = this.config.idPrefixes;
|
|
566
|
+
if (scope && !(0, idGeneration_1.matchesAnyDocIdPrefix)(docId, scope)) {
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
// A configuration change in between schedules a backfill, which
|
|
570
|
+
// extracts the document again with the new configuration.
|
|
571
|
+
this.setEntry(docId, prepared.fields, prepared.lastModified, cursor.changeSeq ?? 0, prepared.decryptionKeyId);
|
|
572
|
+
}
|
|
573
|
+
setEntry(docId, fields, lastModified, changeSeq, decryptionKeyId) {
|
|
399
574
|
const updateSeq = ++this.updateSeq;
|
|
575
|
+
this.lazyRemoved.delete(docId);
|
|
400
576
|
this.entries.set(docId, this.interner.internEntry({
|
|
401
577
|
docId,
|
|
402
|
-
fields
|
|
403
|
-
lastModified
|
|
404
|
-
changeSeq
|
|
405
|
-
decryptionKeyId
|
|
578
|
+
fields,
|
|
579
|
+
lastModified,
|
|
580
|
+
changeSeq,
|
|
581
|
+
decryptionKeyId,
|
|
406
582
|
updateSeq,
|
|
407
583
|
}));
|
|
408
584
|
this.seqLogSeqs.push(updateSeq);
|
|
409
585
|
this.seqLogDocIds.push(docId);
|
|
410
586
|
this.markBucketDirty(docId);
|
|
411
587
|
}
|
|
588
|
+
/**
|
|
589
|
+
* Extract the summary fields of a document the user just wrote, while it
|
|
590
|
+
* is still in memory. The next update run takes the result from here
|
|
591
|
+
* instead of loading the document again: a bulk import writes far more
|
|
592
|
+
* documents than the document cache holds, so without this the summary
|
|
593
|
+
* would materialize each one a second time.
|
|
594
|
+
*
|
|
595
|
+
* `changeSeq` is the document's changefeed position after the write; a
|
|
596
|
+
* later change of the document makes the prepared state stale (the run
|
|
597
|
+
* then loads it as usual).
|
|
598
|
+
*/
|
|
599
|
+
prepareDocument(docId, doc, changeSeq) {
|
|
600
|
+
if (docId === types_1.DB_SETUP_DOC_ID) {
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
const scope = this.config.idPrefixes;
|
|
604
|
+
if (scope && !(0, idGeneration_1.matchesAnyDocIdPrefix)(docId, scope)) {
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
this.prepared.delete(docId);
|
|
608
|
+
this.prepared.set(docId, {
|
|
609
|
+
changeSeq,
|
|
610
|
+
configFingerprint: this.configFingerprint,
|
|
611
|
+
fields: (0, extractSummaryFields_1.extractSummaryFields)(doc.getData(), this.config),
|
|
612
|
+
lastModified: doc.getLastModified(),
|
|
613
|
+
decryptionKeyId: doc.getDecryptionKeyId(),
|
|
614
|
+
});
|
|
615
|
+
if (this.prepared.size > MAX_PREPARED_DOCUMENTS) {
|
|
616
|
+
const oldest = this.prepared.keys().next().value;
|
|
617
|
+
if (oldest !== undefined)
|
|
618
|
+
this.prepared.delete(oldest);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
412
621
|
recordRemoval(docId) {
|
|
413
622
|
const seq = ++this.updateSeq;
|
|
414
623
|
this.tombstones.push([seq, docId]);
|
|
@@ -511,7 +720,8 @@ class DocumentSummaryStore {
|
|
|
511
720
|
* update run).
|
|
512
721
|
*/
|
|
513
722
|
removeDocument(docId) {
|
|
514
|
-
|
|
723
|
+
this.prepared.delete(docId);
|
|
724
|
+
if (this.deleteEntry(docId)) {
|
|
515
725
|
this.recordRemoval(docId);
|
|
516
726
|
this.markBucketDirty(docId);
|
|
517
727
|
this.cacheManager?.markDirty();
|
|
@@ -520,6 +730,9 @@ class DocumentSummaryStore {
|
|
|
520
730
|
/** Drop all entries and cursors (e.g. after the underlying store was cleared). */
|
|
521
731
|
reset() {
|
|
522
732
|
this.entries.clear();
|
|
733
|
+
this.lazy = false;
|
|
734
|
+
this.lazyRemoved.clear();
|
|
735
|
+
this.prepared.clear();
|
|
523
736
|
this.cursor = null;
|
|
524
737
|
// Delta readers must rescan: start a new modification generation.
|
|
525
738
|
this.generation = (0, idGeneration_1.generateRandomUuid)();
|
|
@@ -532,6 +745,7 @@ class DocumentSummaryStore {
|
|
|
532
745
|
this.lostSeqRanges = [];
|
|
533
746
|
this.needsBackfill = false;
|
|
534
747
|
this.backfillCursor = null;
|
|
748
|
+
this.tableInSync = false;
|
|
535
749
|
for (let i = 0; i < this.bucketCount; i++) {
|
|
536
750
|
this.dirtyBuckets.add(i);
|
|
537
751
|
}
|
|
@@ -551,15 +765,73 @@ class DocumentSummaryStore {
|
|
|
551
765
|
* CacheManager for periodic flushing; the persisted state is restored
|
|
552
766
|
* lazily on the first `update()` call.
|
|
553
767
|
*/
|
|
554
|
-
attachCache(cacheManager, cachePrefix) {
|
|
768
|
+
attachCache(cacheManager, cachePrefix, options) {
|
|
555
769
|
this.cacheManager = cacheManager;
|
|
556
770
|
this.cachePrefix = cachePrefix;
|
|
771
|
+
this.tableAllowed = options?.useTable === true;
|
|
557
772
|
cacheManager.register(this);
|
|
558
773
|
}
|
|
774
|
+
/**
|
|
775
|
+
* Fields whose sort keys the table rows carry: the fields of the declared
|
|
776
|
+
* indexes (`_lastModified` has a column of its own).
|
|
777
|
+
*/
|
|
778
|
+
getSortFields() {
|
|
779
|
+
const fields = new Set();
|
|
780
|
+
for (const index of this.config.indexes) {
|
|
781
|
+
for (const field of index) {
|
|
782
|
+
if (field !== "_lastModified")
|
|
783
|
+
fields.add(field);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
return [...fields].sort();
|
|
787
|
+
}
|
|
788
|
+
/** Whether the next table write must replace every row. */
|
|
789
|
+
tableNeedsFullWrite() {
|
|
790
|
+
return !this.tableInSync || this.tableSortFields !== JSON.stringify(this.getSortFields());
|
|
791
|
+
}
|
|
792
|
+
/** The SummaryTable the rows are kept in, or `null` for bucket snapshots. */
|
|
793
|
+
getTable(store) {
|
|
794
|
+
if (!this.tableAllowed)
|
|
795
|
+
return null;
|
|
796
|
+
return (0, SummaryTable_1.getSummaryTable)(store ?? this.cacheManager?.getStore());
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Bring the SummaryTable up to date with the entries in memory and return
|
|
800
|
+
* it, for queries that filter and sort in SQL. `null` when the summary has
|
|
801
|
+
* no table (or the write failed): the caller scans the entries instead.
|
|
802
|
+
*
|
|
803
|
+
* @internal Used by executeQuery.
|
|
804
|
+
*/
|
|
805
|
+
async syncTable() {
|
|
806
|
+
await this.ensureRestored();
|
|
807
|
+
const table = this.getTable();
|
|
808
|
+
if (!table)
|
|
809
|
+
return null;
|
|
810
|
+
try {
|
|
811
|
+
if (this.lazy && this.tableNeedsFullWrite())
|
|
812
|
+
await this.ensureAllEntries();
|
|
813
|
+
if (this.tableNeedsFullWrite() || this.journalPending.size > 0) {
|
|
814
|
+
await this.writeTable(table, false);
|
|
815
|
+
if (this.lazy)
|
|
816
|
+
this.lazyCount = await table.count(this.getCachePrefix(), { where: null, params: [], orderBy: null });
|
|
817
|
+
}
|
|
818
|
+
else {
|
|
819
|
+
await this.tableWrites;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
catch {
|
|
823
|
+
return null;
|
|
824
|
+
}
|
|
825
|
+
return this.tableNeedsFullWrite() ? null : table;
|
|
826
|
+
}
|
|
559
827
|
getCachePrefix() {
|
|
560
828
|
return this.cachePrefix ?? "summary";
|
|
561
829
|
}
|
|
562
830
|
hasDirtyState() {
|
|
831
|
+
if (this.getTable()) {
|
|
832
|
+
// Before the restore, an out-of-sync table only means "not read yet".
|
|
833
|
+
return this.journalPending.size > 0 || this.metaDirty || (this.restored && this.tableNeedsFullWrite());
|
|
834
|
+
}
|
|
563
835
|
return this.journalPending.size > 0 || this.metaDirty || (!this.ownSnapshot && this.dirtyBuckets.size > 0);
|
|
564
836
|
}
|
|
565
837
|
clearDirty() {
|
|
@@ -602,6 +874,16 @@ class DocumentSummaryStore {
|
|
|
602
874
|
this.cacheManager?.markDirty();
|
|
603
875
|
return 0;
|
|
604
876
|
}
|
|
877
|
+
const table = this.getTable(store);
|
|
878
|
+
if (table) {
|
|
879
|
+
// A full write before the restore (or while the rows are only in the
|
|
880
|
+
// table) would replace the stored rows with the few entries changed
|
|
881
|
+
// since the start.
|
|
882
|
+
await this.ensureRestored();
|
|
883
|
+
if (this.lazy && this.tableNeedsFullWrite())
|
|
884
|
+
await this.ensureAllEntries();
|
|
885
|
+
return this.writeTable(table, true, store);
|
|
886
|
+
}
|
|
605
887
|
// Snapshot + journal: normally only the entries changed since the last
|
|
606
888
|
// flush are appended as a small segment (`summaryj`); the dirty buckets
|
|
607
889
|
// and meta (the snapshot) are rewritten when this instance has no
|
|
@@ -694,6 +976,107 @@ class DocumentSummaryStore {
|
|
|
694
976
|
this.lastFlushAt = Date.now();
|
|
695
977
|
return written;
|
|
696
978
|
}
|
|
979
|
+
/**
|
|
980
|
+
* Write the entries changed since the last write to the SummaryTable (all
|
|
981
|
+
* entries, replacing the stored rows, when the table is not in sync), with
|
|
982
|
+
* the meta record in the same transaction.
|
|
983
|
+
*/
|
|
984
|
+
writeTable(table, fromCacheManager, store) {
|
|
985
|
+
// Take the state synchronously: rows and meta must describe the same
|
|
986
|
+
// state, and changes made while the write is in flight stay pending.
|
|
987
|
+
const replaceAll = this.tableNeedsFullWrite();
|
|
988
|
+
if (replaceAll && this.lazy) {
|
|
989
|
+
// Callers read every row first (ensureAllEntries): never replace the
|
|
990
|
+
// table with the entries written since the restore.
|
|
991
|
+
return Promise.reject(new Error("summary table: full write while the rows are not in memory"));
|
|
992
|
+
}
|
|
993
|
+
const pending = this.journalPending;
|
|
994
|
+
const tombstones = this.pendingTombstones;
|
|
995
|
+
this.journalPending = new Map();
|
|
996
|
+
this.pendingTombstones = [];
|
|
997
|
+
this.dirtyBuckets.clear();
|
|
998
|
+
this.metaDirty = false;
|
|
999
|
+
if (fromCacheManager)
|
|
1000
|
+
this.flushHandledDirty = true;
|
|
1001
|
+
const upserts = [];
|
|
1002
|
+
const deletes = [];
|
|
1003
|
+
if (replaceAll) {
|
|
1004
|
+
for (const entry of this.entries.values())
|
|
1005
|
+
upserts.push(entry);
|
|
1006
|
+
}
|
|
1007
|
+
else {
|
|
1008
|
+
for (const [docId, entry] of pending) {
|
|
1009
|
+
if (entry)
|
|
1010
|
+
upserts.push(entry);
|
|
1011
|
+
else
|
|
1012
|
+
deletes.push(docId);
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
const meta = {
|
|
1016
|
+
cursor: this.cursor,
|
|
1017
|
+
configFingerprint: this.configFingerprint,
|
|
1018
|
+
bucketCount: this.bucketCount,
|
|
1019
|
+
needsBackfill: this.needsBackfill,
|
|
1020
|
+
backfillCursor: this.backfillCursor,
|
|
1021
|
+
generation: this.generation,
|
|
1022
|
+
updateSeq: this.updateSeq,
|
|
1023
|
+
tombstoneHorizon: this.tombstoneHorizon,
|
|
1024
|
+
tombstones: this.tombstones,
|
|
1025
|
+
lostSeqRanges: this.lostSeqRanges,
|
|
1026
|
+
};
|
|
1027
|
+
const prefix = this.getCachePrefix();
|
|
1028
|
+
const sortFields = this.getSortFields();
|
|
1029
|
+
// A failed write of the full table must be retried in full.
|
|
1030
|
+
if (replaceAll) {
|
|
1031
|
+
this.tableInSync = true;
|
|
1032
|
+
this.tableSortFields = JSON.stringify(sortFields);
|
|
1033
|
+
}
|
|
1034
|
+
const write = this.tableWrites.then(async () => {
|
|
1035
|
+
try {
|
|
1036
|
+
await table.write(prefix, {
|
|
1037
|
+
metaJson: JSON.stringify(meta),
|
|
1038
|
+
upserts,
|
|
1039
|
+
deletes,
|
|
1040
|
+
replaceAll,
|
|
1041
|
+
sortFields,
|
|
1042
|
+
indexes: this.config.indexes,
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
catch (error) {
|
|
1046
|
+
if (replaceAll)
|
|
1047
|
+
this.tableInSync = false;
|
|
1048
|
+
this.restorePending(pending, tombstones);
|
|
1049
|
+
this.metaDirty = true;
|
|
1050
|
+
if (fromCacheManager)
|
|
1051
|
+
this.flushHandledDirty = false;
|
|
1052
|
+
throw error;
|
|
1053
|
+
}
|
|
1054
|
+
if (replaceAll && this.legacyRecordsMayExist) {
|
|
1055
|
+
this.legacyRecordsMayExist = false;
|
|
1056
|
+
await this.deleteLegacyRecords(store ?? this.cacheManager?.getStore() ?? null, prefix);
|
|
1057
|
+
}
|
|
1058
|
+
this.lastFlushAt = Date.now();
|
|
1059
|
+
return upserts.length + deletes.length + 1;
|
|
1060
|
+
});
|
|
1061
|
+
this.tableWrites = write.catch(() => undefined);
|
|
1062
|
+
return write;
|
|
1063
|
+
}
|
|
1064
|
+
/** Remove bucket snapshots and journal segments the table replaces. */
|
|
1065
|
+
async deleteLegacyRecords(store, prefix) {
|
|
1066
|
+
if (!store)
|
|
1067
|
+
return;
|
|
1068
|
+
try {
|
|
1069
|
+
for (const type of ["summary", "summaryj"]) {
|
|
1070
|
+
for (const id of await store.list(type)) {
|
|
1071
|
+
if (id.startsWith(`${prefix}/`))
|
|
1072
|
+
await store.delete(type, id);
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
catch {
|
|
1077
|
+
// Left-over records are ignored: the table's meta wins on restore.
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
697
1080
|
/** Append the entries changed since the last flush as one journal segment. */
|
|
698
1081
|
async flushJournalSegment(store, prefix) {
|
|
699
1082
|
const pending = this.journalPending;
|
|
@@ -772,6 +1155,10 @@ class DocumentSummaryStore {
|
|
|
772
1155
|
return;
|
|
773
1156
|
}
|
|
774
1157
|
const prefix = this.getCachePrefix();
|
|
1158
|
+
const table = this.getTable(store);
|
|
1159
|
+
if (table && (await this.restoreFromTable(table, prefix))) {
|
|
1160
|
+
return;
|
|
1161
|
+
}
|
|
775
1162
|
let meta;
|
|
776
1163
|
try {
|
|
777
1164
|
const metaBytes = await store.get("summary", `${prefix}/meta`);
|
|
@@ -852,6 +1239,42 @@ class DocumentSummaryStore {
|
|
|
852
1239
|
// This instance continues with its own chain: its first flush writes a
|
|
853
1240
|
// snapshot (the buckets the replay touched plus meta).
|
|
854
1241
|
this.ownSnapshot = false;
|
|
1242
|
+
this.applyRestoredMeta(meta);
|
|
1243
|
+
}
|
|
1244
|
+
/**
|
|
1245
|
+
* Restore entries and meta from the SummaryTable. Returns false when the
|
|
1246
|
+
* table holds nothing for this summary (first start with a table: the
|
|
1247
|
+
* bucket snapshot is restored and the first flush writes every row).
|
|
1248
|
+
*/
|
|
1249
|
+
async restoreFromTable(table, prefix) {
|
|
1250
|
+
let meta;
|
|
1251
|
+
let sortFields;
|
|
1252
|
+
try {
|
|
1253
|
+
const stored = await table.readMeta(prefix);
|
|
1254
|
+
if (!stored)
|
|
1255
|
+
return false;
|
|
1256
|
+
meta = JSON.parse(stored.metaJson);
|
|
1257
|
+
sortFields = stored.sortFields;
|
|
1258
|
+
// The rows stay in the table until a reader needs all of them.
|
|
1259
|
+
this.lazyCount = await table.count(prefix, { where: null, params: [], orderBy: null });
|
|
1260
|
+
}
|
|
1261
|
+
catch {
|
|
1262
|
+
// An unreadable table only loses cached summaries: rebuild from the
|
|
1263
|
+
// changefeed and rewrite every row.
|
|
1264
|
+
this.entries.clear();
|
|
1265
|
+
this.cursor = null;
|
|
1266
|
+
this.tableInSync = false;
|
|
1267
|
+
return true;
|
|
1268
|
+
}
|
|
1269
|
+
this.tableInSync = true;
|
|
1270
|
+
this.tableSortFields = sortFields ? JSON.stringify(sortFields) : null;
|
|
1271
|
+
this.legacyRecordsMayExist = false;
|
|
1272
|
+
this.lazy = true;
|
|
1273
|
+
this.applyRestoredMeta(meta);
|
|
1274
|
+
return true;
|
|
1275
|
+
}
|
|
1276
|
+
/** Continue from a restored meta record (sequences, cursor, configuration). */
|
|
1277
|
+
applyRestoredMeta(meta) {
|
|
855
1278
|
this.cursor = meta.cursor ?? null;
|
|
856
1279
|
// Rescan once: the cursor may belong to an older numbering of the feed.
|
|
857
1280
|
if ((0, feedCursor_1.isPreEpochFeedCursor)(this.cursor)) {
|