Skip to content

Commit 9b68086

Browse files
Feedback
1 parent 7f51b2d commit 9b68086

File tree

3 files changed

+76
-51
lines changed

3 files changed

+76
-51
lines changed

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import {
4242
DbRemoteDocumentGlobalStore,
4343
DbRemoteDocumentKey,
4444
DbRemoteDocumentReadTimeIndex,
45-
DbRemoteDocumentStore
45+
DbRemoteDocumentStore,
46+
DbTimestampKey
4647
} from './indexeddb_sentinels';
4748
import { getStore } from './indexeddb_transaction';
4849
import {
@@ -108,13 +109,7 @@ class IndexedDbRemoteDocumentCacheImpl implements IndexedDbRemoteDocumentCache {
108109
readTime: SnapshotVersion
109110
): PersistencePromise<void> {
110111
const store = remoteDocumentsStore(transaction);
111-
const path = documentKey.path.toArray();
112-
return store.delete([
113-
path.slice(0, path.length - 2),
114-
path[path.length - 2],
115-
toDbTimestampKey(readTime),
116-
path[path.length - 1]
117-
]);
112+
return store.delete(dbReadTimeKey(documentKey, readTime));
118113
}
119114

120115
/**
@@ -327,21 +322,10 @@ class IndexedDbRemoteDocumentCacheImpl implements IndexedDbRemoteDocumentCache {
327322
limit: number
328323
): PersistencePromise<MutableDocumentMap> {
329324
debugAssert(limit > 0, 'Limit should be at least 1');
330-
331-
const path = offset.documentKey.path.toArray();
332-
const startKey = [
333-
collectionGroup,
334-
toDbTimestampKey(offset.readTime),
335-
path.length > 0 ? path.slice(0, path.length - 2) : [],
336-
path.length > 0 ? path[path.length - 1] : ''
337-
];
338-
const endKey = [
339-
collectionGroup,
340-
[Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER],
341-
[],
342-
''
343-
];
344325
let results = mutableDocumentMap();
326+
327+
const startKey = dbCollectionGroupKey(collectionGroup, offset);
328+
const endKey = dbCollectionGroupKey(collectionGroup, IndexOffset.max());
345329
return remoteDocumentsStore(transaction)
346330
.iterate(
347331
{
@@ -661,12 +645,45 @@ function remoteDocumentsStore(
661645
function dbKey(documentKey: DocumentKey): [string[], string, string] {
662646
const path = documentKey.path.toArray();
663647
return [
664-
path.slice(0, path.length - 2),
665-
path[path.length - 2],
666-
path[path.length - 1]
648+
/* prefix path */ path.slice(0, path.length - 2),
649+
/* collection id */ path[path.length - 2],
650+
/* document id */ path[path.length - 1]
667651
];
668652
}
669653

654+
/**
655+
* Returns a key that can be used for document lookups via the primary key of
656+
* the DbRemoteDocument object store.
657+
*/
658+
function dbReadTimeKey(
659+
documentKey: DocumentKey,
660+
readTime: SnapshotVersion
661+
): DbRemoteDocumentKey {
662+
const path = documentKey.path.toArray();
663+
return [
664+
/* prefix path */ path.slice(0, path.length - 2),
665+
/* collection id */ path[path.length - 2],
666+
toDbTimestampKey(readTime),
667+
/* document id */ path[path.length - 1]
668+
];
669+
}
670+
671+
/**
672+
* Returns a key that can be used for document lookups on the
673+
* `DbRemoteDocumentDocumentCollectionGroupIndex` index.
674+
*/
675+
function dbCollectionGroupKey(
676+
collectionGroup: string,
677+
offset: IndexOffset
678+
): [string, DbTimestampKey, string[], string] {
679+
const path = offset.documentKey.path.toArray();
680+
return [
681+
/* collection id */ collectionGroup,
682+
toDbTimestampKey(offset.readTime),
683+
/* prefix path */ path.slice(0, path.length - 2),
684+
/* document id */ path.length > 0 ? path[path.length - 1] : ''
685+
];
686+
}
670687
/**
671688
* Comparator that compares document keys according to the primary key sorting
672689
* used by the `DbRemoteDocumentDocument` store (by collection path and then

packages/firestore/src/local/indexeddb_schema_converter.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,10 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
235235
}
236236

237237
if (fromVersion < 13 && toVersion >= 13) {
238-
p = p.next(() =>
239-
this.rewriteRemoteDocumentCache(db, simpleDbTransaction)
240-
);
238+
p = p
239+
.next(() => createRemoteDocumentCache(db))
240+
.next(() => this.rewriteRemoteDocumentCache(db, simpleDbTransaction))
241+
.next(() => db.deleteObjectStore(DbRemoteDocumentStoreLegacy));
241242
}
242243

243244
if (fromVersion < 14 && toVersion >= 14) {
@@ -433,23 +434,6 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
433434
DbRemoteDocumentLegacy
434435
>(DbRemoteDocumentStoreLegacy);
435436

436-
const remoteDocumentStore = db.createObjectStore(DbRemoteDocumentStore, {
437-
keyPath: DbRemoteDocumentKeyPath
438-
});
439-
remoteDocumentStore.createIndex(
440-
DbRemoteDocumentDocumentKeyIndex,
441-
DbRemoteDocumentDocumentKeyIndexPath
442-
);
443-
remoteDocumentStore.createIndex(
444-
DbRemoteDocumentReadTimeIndex,
445-
DbRemoteDocumentReadTimeIndexPath,
446-
{ unique: false }
447-
);
448-
remoteDocumentStore.createIndex(
449-
DbRemoteDocumentCollectionGroupIndex,
450-
DbRemoteDocumentCollectionGroupIndexPath
451-
);
452-
453437
const writes: Array<PersistencePromise<void>> = [];
454438
return legacyRemoteDocumentStore
455439
.iterate((_, legacyDocument) => {
@@ -471,11 +455,7 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
471455
};
472456
writes.push(remoteDocumentStore.put(dbRemoteDocument));
473457
})
474-
.next(() =>
475-
PersistencePromise.waitFor(writes).next(() =>
476-
db.deleteObjectStore(DbRemoteDocumentStoreLegacy)
477-
)
478-
);
458+
.next(() => PersistencePromise.waitFor(writes));
479459
}
480460
}
481461

@@ -544,6 +524,25 @@ function createLegacyRemoteDocumentCache(db: IDBDatabase): void {
544524
db.createObjectStore(DbRemoteDocumentStoreLegacy);
545525
}
546526

527+
function createRemoteDocumentCache(db: IDBDatabase): void {
528+
const remoteDocumentStore = db.createObjectStore(DbRemoteDocumentStore, {
529+
keyPath: DbRemoteDocumentKeyPath
530+
});
531+
remoteDocumentStore.createIndex(
532+
DbRemoteDocumentDocumentKeyIndex,
533+
DbRemoteDocumentDocumentKeyIndexPath
534+
);
535+
remoteDocumentStore.createIndex(
536+
DbRemoteDocumentReadTimeIndex,
537+
DbRemoteDocumentReadTimeIndexPath,
538+
{ unique: false }
539+
);
540+
remoteDocumentStore.createIndex(
541+
DbRemoteDocumentCollectionGroupIndex,
542+
DbRemoteDocumentCollectionGroupIndexPath
543+
);
544+
}
545+
547546
function createDocumentGlobalStore(db: IDBDatabase): void {
548547
db.createObjectStore(DbRemoteDocumentGlobalStore);
549548
}

packages/firestore/src/model/field_index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,23 @@ export class IndexOffset {
222222
readonly largestBatchId: number
223223
) {}
224224

225-
/** The state of an index that has not yet been backfilled. */
225+
/** Returns an offset that sorts before all regular offsets. */
226226
static min(): IndexOffset {
227227
return new IndexOffset(
228228
SnapshotVersion.min(),
229229
DocumentKey.empty(),
230230
INITIAL_LARGEST_BATCH_ID
231231
);
232232
}
233+
234+
/** Returns an offset that sorts after all regular offsets. */
235+
static max(): IndexOffset {
236+
return new IndexOffset(
237+
SnapshotVersion.max(),
238+
DocumentKey.empty(),
239+
INITIAL_LARGEST_BATCH_ID
240+
);
241+
}
233242
}
234243

235244
export function indexOffsetComparator(

0 commit comments

Comments
 (0)