-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathindexeddb_remote_document_cache.ts
663 lines (611 loc) · 20.9 KB
/
indexeddb_remote_document_cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Query, queryMatches } from '../core/query';
import { SnapshotVersion } from '../core/snapshot_version';
import {
DocumentKeySet,
DocumentSizeEntries,
MutableDocumentMap,
mutableDocumentMap,
OverlayMap
} from '../model/collections';
import { MutableDocument } from '../model/document';
import { DocumentKey } from '../model/document_key';
import { IndexOffset } from '../model/field_index';
import { ResourcePath } from '../model/path';
import { debugAssert, hardAssert } from '../util/assert';
import { primitiveComparator } from '../util/misc';
import { ObjectMap } from '../util/obj_map';
import { SortedMap } from '../util/sorted_map';
import { SortedSet } from '../util/sorted_set';
import { IndexManager } from './index_manager';
import { dbDocumentSize } from './indexeddb_mutation_batch_impl';
import { DbRemoteDocument, DbRemoteDocumentGlobal } from './indexeddb_schema';
import {
DbRemoteDocumentCollectionGroupIndex,
DbRemoteDocumentDocumentKeyIndex,
DbRemoteDocumentGlobalKey,
DbRemoteDocumentGlobalStore,
DbRemoteDocumentKey,
DbRemoteDocumentStore,
DbTimestampKey
} from './indexeddb_sentinels';
import { getStore } from './indexeddb_transaction';
import {
fromDbRemoteDocument,
LocalSerializer,
toDbRemoteDocument,
toDbTimestampKey
} from './local_serializer';
import { PersistencePromise } from './persistence_promise';
import { PersistenceTransaction } from './persistence_transaction';
import { QueryContext } from './query_context';
import { RemoteDocumentCache } from './remote_document_cache';
import { RemoteDocumentChangeBuffer } from './remote_document_change_buffer';
import { SimpleDbStore } from './simple_db';
export interface DocumentSizeEntry {
document: MutableDocument;
size: number;
}
export interface IndexedDbRemoteDocumentCache extends RemoteDocumentCache {
// The IndexedDbRemoteDocumentCache doesn't implement any methods on top
// of RemoteDocumentCache. This class exists for consistency.
}
/**
* The RemoteDocumentCache for IndexedDb. To construct, invoke
* `newIndexedDbRemoteDocumentCache()`.
*/
class IndexedDbRemoteDocumentCacheImpl implements IndexedDbRemoteDocumentCache {
indexManager!: IndexManager;
constructor(readonly serializer: LocalSerializer) {}
setIndexManager(indexManager: IndexManager): void {
this.indexManager = indexManager;
}
/**
* Adds the supplied entries to the cache.
*
* All calls of `addEntry` are required to go through the RemoteDocumentChangeBuffer
* returned by `newChangeBuffer()` to ensure proper accounting of metadata.
*/
addEntry(
transaction: PersistenceTransaction,
key: DocumentKey,
doc: DbRemoteDocument
): PersistencePromise<void> {
const documentStore = remoteDocumentsStore(transaction);
return documentStore.put(doc);
}
/**
* Removes a document from the cache.
*
* All calls of `removeEntry` are required to go through the RemoteDocumentChangeBuffer
* returned by `newChangeBuffer()` to ensure proper accounting of metadata.
*/
removeEntry(
transaction: PersistenceTransaction,
documentKey: DocumentKey,
readTime: SnapshotVersion
): PersistencePromise<void> {
const store = remoteDocumentsStore(transaction);
return store.delete(dbReadTimeKey(documentKey, readTime));
}
/**
* Updates the current cache size.
*
* Callers to `addEntry()` and `removeEntry()` *must* call this afterwards to update the
* cache's metadata.
*/
updateMetadata(
transaction: PersistenceTransaction,
sizeDelta: number
): PersistencePromise<void> {
return this.getMetadata(transaction).next(metadata => {
metadata.byteSize += sizeDelta;
return this.setMetadata(transaction, metadata);
});
}
getEntry(
transaction: PersistenceTransaction,
documentKey: DocumentKey
): PersistencePromise<MutableDocument> {
let doc = MutableDocument.newInvalidDocument(documentKey);
return remoteDocumentsStore(transaction)
.iterate(
{
index: DbRemoteDocumentDocumentKeyIndex,
range: IDBKeyRange.only(dbKey(documentKey))
},
(_, dbRemoteDoc) => {
doc = this.maybeDecodeDocument(documentKey, dbRemoteDoc);
}
)
.next(() => doc);
}
/**
* Looks up an entry in the cache.
*
* @param documentKey - The key of the entry to look up.
* @returns The cached document entry and its size.
*/
getSizedEntry(
transaction: PersistenceTransaction,
documentKey: DocumentKey
): PersistencePromise<DocumentSizeEntry> {
let result = {
size: 0,
document: MutableDocument.newInvalidDocument(documentKey)
};
return remoteDocumentsStore(transaction)
.iterate(
{
index: DbRemoteDocumentDocumentKeyIndex,
range: IDBKeyRange.only(dbKey(documentKey))
},
(_, dbRemoteDoc) => {
result = {
document: this.maybeDecodeDocument(documentKey, dbRemoteDoc),
size: dbDocumentSize(dbRemoteDoc)
};
}
)
.next(() => result);
}
getEntries(
transaction: PersistenceTransaction,
documentKeys: DocumentKeySet
): PersistencePromise<MutableDocumentMap> {
let results = mutableDocumentMap();
return this.forEachDbEntry(
transaction,
documentKeys,
(key, dbRemoteDoc) => {
const doc = this.maybeDecodeDocument(key, dbRemoteDoc);
results = results.insert(key, doc);
}
).next(() => results);
}
/**
* Looks up several entries in the cache.
*
* @param documentKeys - The set of keys entries to look up.
* @returns A map of documents indexed by key and a map of sizes indexed by
* key (zero if the document does not exist).
*/
getSizedEntries(
transaction: PersistenceTransaction,
documentKeys: DocumentKeySet
): PersistencePromise<DocumentSizeEntries> {
let results = mutableDocumentMap();
let sizeMap = new SortedMap<DocumentKey, number>(DocumentKey.comparator);
return this.forEachDbEntry(
transaction,
documentKeys,
(key, dbRemoteDoc) => {
const doc = this.maybeDecodeDocument(key, dbRemoteDoc);
results = results.insert(key, doc);
sizeMap = sizeMap.insert(key, dbDocumentSize(dbRemoteDoc));
}
).next(() => {
return { documents: results, sizeMap };
});
}
private forEachDbEntry(
transaction: PersistenceTransaction,
documentKeys: DocumentKeySet,
callback: (key: DocumentKey, doc: DbRemoteDocument | null) => void
): PersistencePromise<void> {
if (documentKeys.isEmpty()) {
return PersistencePromise.resolve();
}
let sortedKeys = new SortedSet<DocumentKey>(dbKeyComparator);
documentKeys.forEach(e => (sortedKeys = sortedKeys.add(e)));
const range = IDBKeyRange.bound(
dbKey(sortedKeys.first()!),
dbKey(sortedKeys.last()!)
);
const keyIter = sortedKeys.getIterator();
let nextKey: DocumentKey | null = keyIter.getNext();
return remoteDocumentsStore(transaction)
.iterate(
{ index: DbRemoteDocumentDocumentKeyIndex, range },
(_, dbRemoteDoc, control) => {
const potentialKey = DocumentKey.fromSegments([
...dbRemoteDoc.prefixPath,
dbRemoteDoc.collectionGroup,
dbRemoteDoc.documentId
]);
// Go through keys not found in cache.
while (nextKey && dbKeyComparator(nextKey!, potentialKey) < 0) {
callback(nextKey!, null);
nextKey = keyIter.getNext();
}
if (nextKey && nextKey!.isEqual(potentialKey)) {
// Key found in cache.
callback(nextKey!, dbRemoteDoc);
nextKey = keyIter.hasNext() ? keyIter.getNext() : null;
}
// Skip to the next key (if there is one).
if (nextKey) {
control.skip(dbKey(nextKey));
} else {
control.done();
}
}
)
.next(() => {
// The rest of the keys are not in the cache. One case where `iterate`
// above won't go through them is when the cache is empty.
while (nextKey) {
callback(nextKey!, null);
nextKey = keyIter.hasNext() ? keyIter.getNext() : null;
}
});
}
getDocumentsMatchingQuery(
transaction: PersistenceTransaction,
query: Query,
offset: IndexOffset,
mutatedDocs: OverlayMap,
context?: QueryContext
): PersistencePromise<MutableDocumentMap> {
const collection = query.path;
const startKey = [
collection.popLast().toArray(),
collection.lastSegment(),
toDbTimestampKey(offset.readTime),
offset.documentKey.path.isEmpty()
? ''
: offset.documentKey.path.lastSegment()
];
const endKey: DbRemoteDocumentKey = [
collection.popLast().toArray(),
collection.lastSegment(),
[Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER],
''
];
return remoteDocumentsStore(transaction)
.loadAll(IDBKeyRange.bound(startKey, endKey, true))
.next(dbRemoteDocs => {
context?.incrementDocumentReadCount(dbRemoteDocs.length);
let results = mutableDocumentMap();
for (const dbRemoteDoc of dbRemoteDocs) {
const document = this.maybeDecodeDocument(
DocumentKey.fromSegments(
dbRemoteDoc.prefixPath.concat(
dbRemoteDoc.collectionGroup,
dbRemoteDoc.documentId
)
),
dbRemoteDoc
);
if (
document.isFoundDocument() &&
(queryMatches(query, document) || mutatedDocs.has(document.key))
) {
// Either the document matches the given query, or it is mutated.
results = results.insert(document.key, document);
}
}
return results;
});
}
getAllFromCollectionGroup(
transaction: PersistenceTransaction,
collectionGroup: string,
offset: IndexOffset,
limit: number
): PersistencePromise<MutableDocumentMap> {
debugAssert(limit > 0, 'Limit should be at least 1');
let results = mutableDocumentMap();
const startKey = dbCollectionGroupKey(collectionGroup, offset);
const endKey = dbCollectionGroupKey(collectionGroup, IndexOffset.max());
return remoteDocumentsStore(transaction)
.iterate(
{
index: DbRemoteDocumentCollectionGroupIndex,
range: IDBKeyRange.bound(startKey, endKey, true)
},
(_, dbRemoteDoc, control) => {
const document = this.maybeDecodeDocument(
DocumentKey.fromSegments(
dbRemoteDoc.prefixPath.concat(
dbRemoteDoc.collectionGroup,
dbRemoteDoc.documentId
)
),
dbRemoteDoc
);
results = results.insert(document.key, document);
if (results.size === limit) {
control.done();
}
}
)
.next(() => results);
}
newChangeBuffer(options?: {
trackRemovals: boolean;
}): RemoteDocumentChangeBuffer {
return new IndexedDbRemoteDocumentChangeBuffer(
this,
!!options && options.trackRemovals
);
}
getSize(txn: PersistenceTransaction): PersistencePromise<number> {
return this.getMetadata(txn).next(metadata => metadata.byteSize);
}
private getMetadata(
txn: PersistenceTransaction
): PersistencePromise<DbRemoteDocumentGlobal> {
return documentGlobalStore(txn)
.get(DbRemoteDocumentGlobalKey)
.next(metadata => {
hardAssert(!!metadata, 'Missing document cache metadata');
return metadata!;
});
}
private setMetadata(
txn: PersistenceTransaction,
metadata: DbRemoteDocumentGlobal
): PersistencePromise<void> {
return documentGlobalStore(txn).put(DbRemoteDocumentGlobalKey, metadata);
}
/**
* Decodes `dbRemoteDoc` and returns the document (or an invalid document if
* the document corresponds to the format used for sentinel deletes).
*/
private maybeDecodeDocument(
documentKey: DocumentKey,
dbRemoteDoc: DbRemoteDocument | null
): MutableDocument {
if (dbRemoteDoc) {
const doc = fromDbRemoteDocument(this.serializer, dbRemoteDoc);
// Whether the document is a sentinel removal and should only be used in the
// `getNewDocumentChanges()`
const isSentinelRemoval =
doc.isNoDocument() && doc.version.isEqual(SnapshotVersion.min());
if (!isSentinelRemoval) {
return doc;
}
}
return MutableDocument.newInvalidDocument(documentKey);
}
}
/** Creates a new IndexedDbRemoteDocumentCache. */
export function newIndexedDbRemoteDocumentCache(
serializer: LocalSerializer
): IndexedDbRemoteDocumentCache {
return new IndexedDbRemoteDocumentCacheImpl(serializer);
}
/**
* Handles the details of adding and updating documents in the IndexedDbRemoteDocumentCache.
*
* Unlike the MemoryRemoteDocumentChangeBuffer, the IndexedDb implementation computes the size
* delta for all submitted changes. This avoids having to re-read all documents from IndexedDb
* when we apply the changes.
*/
class IndexedDbRemoteDocumentChangeBuffer extends RemoteDocumentChangeBuffer {
// A map of document sizes and read times prior to applying the changes in
// this buffer.
protected documentStates: ObjectMap<
DocumentKey,
{ size: number; readTime: SnapshotVersion }
> = new ObjectMap(
key => key.toString(),
(l, r) => l.isEqual(r)
);
/**
* @param documentCache - The IndexedDbRemoteDocumentCache to apply the changes to.
* @param trackRemovals - Whether to create sentinel deletes that can be tracked by
* `getNewDocumentChanges()`.
*/
constructor(
private readonly documentCache: IndexedDbRemoteDocumentCacheImpl,
private readonly trackRemovals: boolean
) {
super();
}
protected applyChanges(
transaction: PersistenceTransaction
): PersistencePromise<void> {
const promises: Array<PersistencePromise<void>> = [];
let sizeDelta = 0;
let collectionParents = new SortedSet<ResourcePath>((l, r) =>
primitiveComparator(l.canonicalString(), r.canonicalString())
);
this.changes.forEach((key, documentChange) => {
const previousDoc = this.documentStates.get(key);
debugAssert(
previousDoc !== undefined,
`Cannot modify a document that wasn't read (for ${key})`
);
promises.push(
this.documentCache.removeEntry(transaction, key, previousDoc.readTime)
);
if (documentChange.isValidDocument()) {
debugAssert(
!documentChange.readTime.isEqual(SnapshotVersion.min()),
'Cannot add a document with a read time of zero'
);
const doc = toDbRemoteDocument(
this.documentCache.serializer,
documentChange
);
collectionParents = collectionParents.add(key.path.popLast());
const size = dbDocumentSize(doc);
sizeDelta += size - previousDoc.size;
promises.push(this.documentCache.addEntry(transaction, key, doc));
} else {
sizeDelta -= previousDoc.size;
if (this.trackRemovals) {
// In order to track removals, we store a "sentinel delete" in the
// RemoteDocumentCache. This entry is represented by a NoDocument
// with a version of 0 and ignored by `maybeDecodeDocument()` but
// preserved in `getNewDocumentChanges()`.
const deletedDoc = toDbRemoteDocument(
this.documentCache.serializer,
documentChange.convertToNoDocument(SnapshotVersion.min())
);
promises.push(
this.documentCache.addEntry(transaction, key, deletedDoc)
);
}
}
});
collectionParents.forEach(parent => {
promises.push(
this.documentCache.indexManager.addToCollectionParentIndex(
transaction,
parent
)
);
});
promises.push(this.documentCache.updateMetadata(transaction, sizeDelta));
return PersistencePromise.waitFor(promises);
}
protected getFromCache(
transaction: PersistenceTransaction,
documentKey: DocumentKey
): PersistencePromise<MutableDocument> {
// Record the size of everything we load from the cache so we can compute a delta later.
return this.documentCache
.getSizedEntry(transaction, documentKey)
.next(getResult => {
this.documentStates.set(documentKey, {
size: getResult.size,
readTime: getResult.document.readTime
});
return getResult.document;
});
}
protected getAllFromCache(
transaction: PersistenceTransaction,
documentKeys: DocumentKeySet
): PersistencePromise<MutableDocumentMap> {
// Record the size of everything we load from the cache so we can compute
// a delta later.
return this.documentCache
.getSizedEntries(transaction, documentKeys)
.next(({ documents, sizeMap }) => {
// Note: `getAllFromCache` returns two maps instead of a single map from
// keys to `DocumentSizeEntry`s. This is to allow returning the
// `MutableDocumentMap` directly, without a conversion.
sizeMap.forEach((documentKey, size) => {
this.documentStates.set(documentKey, {
size,
readTime: documents.get(documentKey)!.readTime
});
});
return documents;
});
}
}
function documentGlobalStore(
txn: PersistenceTransaction
): SimpleDbStore<DbRemoteDocumentGlobalKey, DbRemoteDocumentGlobal> {
return getStore<DbRemoteDocumentGlobalKey, DbRemoteDocumentGlobal>(
txn,
DbRemoteDocumentGlobalStore
);
}
/**
* Helper to get a typed SimpleDbStore for the remoteDocuments object store.
*/
function remoteDocumentsStore(
txn: PersistenceTransaction
): SimpleDbStore<DbRemoteDocumentKey, DbRemoteDocument> {
return getStore<DbRemoteDocumentKey, DbRemoteDocument>(
txn,
DbRemoteDocumentStore
);
}
/**
* Returns a key that can be used for document lookups on the
* `DbRemoteDocumentDocumentKeyIndex` index.
*/
function dbKey(documentKey: DocumentKey): [string[], string, string] {
const path = documentKey.path.toArray();
return [
/* prefix path */ path.slice(0, path.length - 2),
/* collection id */ path[path.length - 2],
/* document id */ path[path.length - 1]
];
}
/**
* Returns a key that can be used for document lookups via the primary key of
* the DbRemoteDocument object store.
*/
function dbReadTimeKey(
documentKey: DocumentKey,
readTime: SnapshotVersion
): DbRemoteDocumentKey {
const path = documentKey.path.toArray();
return [
/* prefix path */ path.slice(0, path.length - 2),
/* collection id */ path[path.length - 2],
toDbTimestampKey(readTime),
/* document id */ path[path.length - 1]
];
}
/**
* Returns a key that can be used for document lookups on the
* `DbRemoteDocumentDocumentCollectionGroupIndex` index.
*/
function dbCollectionGroupKey(
collectionGroup: string,
offset: IndexOffset
): [string, DbTimestampKey, string[], string] {
const path = offset.documentKey.path.toArray();
return [
/* collection id */ collectionGroup,
toDbTimestampKey(offset.readTime),
/* prefix path */ path.slice(0, path.length - 2),
/* document id */ path.length > 0 ? path[path.length - 1] : ''
];
}
/**
* Comparator that compares document keys according to the primary key sorting
* used by the `DbRemoteDocumentDocument` store (by prefix path, collection id
* and then document ID).
*
* Visible for testing.
*/
export function dbKeyComparator(l: DocumentKey, r: DocumentKey): number {
const left = l.path.toArray();
const right = r.path.toArray();
// The ordering is based on https://chromium.googlesource.com/chromium/blink/+/fe5c21fef94dae71c1c3344775b8d8a7f7e6d9ec/Source/modules/indexeddb/IDBKey.cpp#74
let cmp = 0;
for (let i = 0; i < left.length - 2 && i < right.length - 2; ++i) {
cmp = primitiveComparator(left[i], right[i]);
if (cmp) {
return cmp;
}
}
cmp = primitiveComparator(left.length, right.length);
if (cmp) {
return cmp;
}
cmp = primitiveComparator(left[left.length - 2], right[right.length - 2]);
if (cmp) {
return cmp;
}
// TODO(b/329441702): Document IDs should be sorted by UTF-8 encoded byte
// order, but IndexedDB sorts strings lexicographically. Document ID
// comparison here still relies on primitive comparison to avoid mismatches
// observed in snapshot listeners with Unicode characters in documentIds
return primitiveComparator(left[left.length - 1], right[right.length - 1]);
}