Skip to content

Commit c3fbc23

Browse files
committed
Fix usages of Map/Set (switch to ObjectMap/SortedSet). Don't use documentMap, mutableDocumentMap, and documentKeySet.
1 parent d46c5b2 commit c3fbc23

File tree

5 files changed

+65
-38
lines changed

5 files changed

+65
-38
lines changed

packages/firestore/src/local/local_documents_view.ts

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,28 @@ import {
2424
} from '../core/query';
2525
import { Timestamp } from '../lite-api/timestamp';
2626
import {
27-
documentKeySet,
2827
DocumentKeySet,
28+
DocumentKeyToOverlayMap,
2929
DocumentMap,
30-
documentMap,
31-
mutableDocumentMap,
32-
MutableDocumentMap
30+
MutableDocumentMap,
31+
newDocumentKeyMap,
32+
newDocumentKeyToMutationMap,
33+
newDocumentKeyToOverlayMap
3334
} from '../model/collections';
3435
import { Document, MutableDocument } from '../model/document';
3536
import { DocumentKey } from '../model/document_key';
3637
import { IndexOffset } from '../model/field_index';
3738
import { FieldMask } from '../model/field_mask';
3839
import {
3940
calculateOverlayMutation,
40-
Mutation,
4141
mutationApplyToLocalView,
4242
PatchMutation
4343
} from '../model/mutation';
4444
import { Overlay } from '../model/overlay';
4545
import { ResourcePath } from '../model/path';
4646
import { debugAssert } from '../util/assert';
4747
import { SortedMap } from '../util/sorted_map';
48+
import { SortedSet } from '../util/sorted_set';
4849

4950
import { DocumentOverlayCache } from './document_overlay_cache';
5051
import { IndexManager } from './index_manager';
@@ -110,9 +111,11 @@ export class LocalDocumentsView {
110111
return this.remoteDocumentCache
111112
.getEntries(transaction, keys)
112113
.next(docs =>
113-
this.getLocalViewOfDocuments(transaction, docs, documentKeySet()).next(
114-
() => docs as DocumentMap
115-
)
114+
this.getLocalViewOfDocuments(
115+
transaction,
116+
docs,
117+
new SortedSet(DocumentKey.comparator)
118+
).next(() => docs as DocumentMap)
116119
);
117120
}
118121

@@ -134,7 +137,7 @@ export class LocalDocumentsView {
134137
return this.computeViews(
135138
transaction,
136139
docs,
137-
new Map<DocumentKey, Overlay>(),
140+
newDocumentKeyToOverlayMap(),
138141
existenceStateChanged
139142
);
140143
}
@@ -146,11 +149,13 @@ export class LocalDocumentsView {
146149
computeViews(
147150
transaction: PersistenceTransaction,
148151
docs: MutableDocumentMap,
149-
memoizedOverlays: Map<DocumentKey, Overlay>,
152+
memoizedOverlays: DocumentKeyToOverlayMap,
150153
existenceStateChanged: DocumentKeySet
151154
): PersistencePromise<DocumentMap> {
152-
let results = documentMap();
153-
let recalculateDocuments = mutableDocumentMap();
155+
let results = new SortedMap<DocumentKey, Document>(DocumentKey.comparator);
156+
let recalculateDocuments = new SortedMap<DocumentKey, MutableDocument>(
157+
DocumentKey.comparator
158+
);
154159
const promises: Array<PersistencePromise<void>> = [];
155160
docs.forEach((_, doc) => {
156161
const overlayPromise = memoizedOverlays.has(doc.key)
@@ -199,12 +204,12 @@ export class LocalDocumentsView {
199204
transaction: PersistenceTransaction,
200205
docs: MutableDocumentMap
201206
): PersistencePromise<void> {
202-
const masks = new Map<DocumentKey, FieldMask | null>();
207+
const masks = newDocumentKeyMap<FieldMask | null>();
203208
// A reverse lookup map from batch id to the documents within that batch.
204-
let documentsByBatchId = new SortedMap<number, Set<DocumentKey>>(
209+
let documentsByBatchId = new SortedMap<number, DocumentKeySet>(
205210
(key1: number, key2: number) => key1 - key2
206211
);
207-
const processed = new Set<DocumentKey>();
212+
let processed = new SortedSet(DocumentKey.comparator);
208213
return this.mutationQueue
209214
.getAllMutationBatchesAffectingDocumentKeys(transaction, docs)
210215
.next(batches => {
@@ -218,10 +223,11 @@ export class LocalDocumentsView {
218223
if (documentsByBatchId.get(batch.batchId) === null) {
219224
documentsByBatchId = documentsByBatchId.insert(
220225
batch.batchId,
221-
new Set<DocumentKey>()
226+
new SortedSet(DocumentKey.comparator)
222227
);
223228
}
224-
documentsByBatchId.get(batch.batchId)!.add(key);
229+
const newSet = documentsByBatchId.get(batch.batchId)!.add(key);
230+
documentsByBatchId.insert(batch.batchId, newSet);
225231
});
226232
});
227233
})
@@ -234,7 +240,7 @@ export class LocalDocumentsView {
234240
const entry = iter.getNext();
235241
const batchId = entry.key;
236242
const keys = entry.value;
237-
const overlays = new Map<DocumentKey, Mutation>();
243+
const overlays = newDocumentKeyToMutationMap();
238244
keys.forEach(key => {
239245
if (!processed.has(key)) {
240246
// TODO: Should we change `overlays` type to Map<DK, Mutation|null>
@@ -246,7 +252,7 @@ export class LocalDocumentsView {
246252
if (overlayMutation !== null) {
247253
overlays.set(key, overlayMutation);
248254
}
249-
processed.add(key);
255+
processed = processed.add(key);
250256
}
251257
});
252258
promises.push(
@@ -313,7 +319,9 @@ export class LocalDocumentsView {
313319
// Just do a simple document lookup.
314320
return this.getDocument(transaction, new DocumentKey(docPath)).next(
315321
document => {
316-
let result = documentMap();
322+
let result = new SortedMap<DocumentKey, Document>(
323+
DocumentKey.comparator
324+
);
317325
if (document.isFoundDocument()) {
318326
result = result.insert(document.key, document);
319327
}
@@ -332,7 +340,7 @@ export class LocalDocumentsView {
332340
'Currently we only support collection group queries at the root.'
333341
);
334342
const collectionId = query.collectionGroup!;
335-
let results = documentMap();
343+
let results = new SortedMap<DocumentKey, Document>(DocumentKey.comparator);
336344
return this.indexManager
337345
.getCollectionParents(transaction, collectionId)
338346
.next(parents => {
@@ -376,7 +384,7 @@ export class LocalDocumentsView {
376384
.next(overlays => {
377385
// As documents might match the query because of their overlay we need to
378386
// include documents for all overlays in the initial document set.
379-
overlays.forEach(overlay => {
387+
overlays.forEach((_, overlay) => {
380388
const key = overlay.getKey();
381389
if (remoteDocuments.get(key) === null) {
382390
remoteDocuments = remoteDocuments.insert(
@@ -387,7 +395,9 @@ export class LocalDocumentsView {
387395
});
388396

389397
// Apply the overlays and match against the query.
390-
let results = documentMap();
398+
let results = new SortedMap<DocumentKey, Document>(
399+
DocumentKey.comparator
400+
);
391401
remoteDocuments.forEach((key, document) => {
392402
const overlay = overlays.get(key);
393403
if (overlay !== undefined) {

packages/firestore/src/model/collections.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ export function newDocumentKeyToMutationMap(): DocumentKeyToMutationMap {
6666
);
6767
}
6868

69+
export type DocumentKeyMap<T> = ObjectMap<DocumentKey, T>;
70+
export function newDocumentKeyMap<T>(): DocumentKeyMap<T> {
71+
return new ObjectMap<DocumentKey, T>(
72+
key => key.toString(),
73+
(l, r) => l.isEqual(r)
74+
);
75+
}
76+
6977
export type DocumentVersionMap = SortedMap<DocumentKey, SnapshotVersion>;
7078
const EMPTY_DOCUMENT_VERSION_MAP = new SortedMap<DocumentKey, SnapshotVersion>(
7179
DocumentKey.comparator

packages/firestore/src/model/mutation.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Timestamp } from '../lite-api/timestamp';
2020
import { Value as ProtoValue } from '../protos/firestore_proto_api';
2121
import { debugAssert, hardAssert } from '../util/assert';
2222
import { arrayEquals } from '../util/misc';
23+
import { SortedSet } from '../util/sorted_set';
2324

2425
import { Document, MutableDocument } from './document';
2526
import { DocumentKey } from './document_key';
@@ -238,7 +239,7 @@ export function calculateOverlayMutation(
238239
} else {
239240
const docValue = doc.data;
240241
const patchValue = ObjectValue.empty();
241-
const maskSet = new Set<FieldPath>();
242+
let maskSet = new SortedSet<FieldPath>(FieldPath.comparator);
242243
mask.fields.forEach(path => {
243244
if (!maskSet.has(path)) {
244245
const value = docValue.field(path);
@@ -255,13 +256,13 @@ export function calculateOverlayMutation(
255256
path = path.popLast();
256257
}
257258
patchValue.set(path, docValue.field(path)!);
258-
maskSet.add(path);
259+
maskSet = maskSet.add(path);
259260
}
260261
});
261262
return new PatchMutation(
262263
doc.key,
263264
patchValue,
264-
new FieldMask(Array.from(maskSet)),
265+
new FieldMask(maskSet.toArray()),
265266
Precondition.none()
266267
);
267268
}
@@ -567,12 +568,17 @@ function patchMutationApplyToLocalView(
567568
return null;
568569
}
569570

570-
const mergedMaskSet = new Set<FieldPath>(previousMask.fields);
571-
mutation.fieldMask.fields.forEach(fieldPath => mergedMaskSet.add(fieldPath));
571+
let mergedMaskSet = new SortedSet<FieldPath>(FieldPath.comparator);
572+
previousMask.fields.forEach(
573+
fieldPath => (mergedMaskSet = mergedMaskSet.add(fieldPath))
574+
);
575+
mutation.fieldMask.fields.forEach(
576+
fieldPath => (mergedMaskSet = mergedMaskSet.add(fieldPath))
577+
);
572578
mutation.fieldTransforms
573579
.map(transform => transform.field)
574-
.forEach(fieldPath => mergedMaskSet.add(fieldPath));
575-
return new FieldMask(Array.from(mergedMaskSet));
580+
.forEach(fieldPath => (mergedMaskSet = mergedMaskSet.add(fieldPath)));
581+
return new FieldMask(mergedMaskSet.toArray());
576582
}
577583

578584
/**

packages/firestore/src/model/mutation_batch.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ import { arrayEquals } from '../util/misc';
2424
import {
2525
documentKeySet,
2626
DocumentKeySet,
27+
DocumentKeyToMutationMap,
2728
DocumentMap,
2829
DocumentVersionMap,
29-
documentVersionMap
30+
documentVersionMap,
31+
newDocumentKeyToMutationMap
3032
} from './collections';
3133
import { MutableDocument } from './document';
32-
import { DocumentKey } from './document_key';
3334
import { FieldMask } from './field_mask';
3435
import {
3536
calculateOverlayMutation,
@@ -148,13 +149,11 @@ export class MutationBatch {
148149
* this batch. Returns a `DocumentKey` to `Mutation` map which can be used to
149150
* replace all the mutation applications.
150151
*/
151-
applyToLocalDocumentSet(
152-
documentMap: DocumentMap
153-
): Map<DocumentKey, Mutation> {
152+
applyToLocalDocumentSet(documentMap: DocumentMap): DocumentKeyToMutationMap {
154153
// TODO(mrschmidt): This implementation is O(n^2). If we apply the mutations
155154
// directly (as done in `applyToLocalView()`), we can reduce the complexity
156155
// to O(n).
157-
const overlays = new Map<DocumentKey, Mutation>();
156+
const overlays = newDocumentKeyToMutationMap();
158157
this.mutations.forEach(m => {
159158
const document = documentMap.get(m.key)!;
160159
// TODO(mutabledocuments): This method should take a MutableDocumentMap

packages/firestore/test/unit/local/query_engine.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ import { PersistenceTransaction } from '../../../src/local/persistence_transacti
3232
import { QueryEngine } from '../../../src/local/query_engine';
3333
import { RemoteDocumentCache } from '../../../src/local/remote_document_cache';
3434
import { TargetCache } from '../../../src/local/target_cache';
35-
import { documentKeySet, DocumentMap } from '../../../src/model/collections';
35+
import {
36+
documentKeySet,
37+
DocumentMap,
38+
newDocumentKeyToMutationMap
39+
} from '../../../src/model/collections';
3640
import { Document, MutableDocument } from '../../../src/model/document';
3741
import { DocumentKey } from '../../../src/model/document_key';
3842
import { DocumentSet } from '../../../src/model/document_set';
@@ -133,7 +137,7 @@ describe('QueryEngine', () => {
133137
return mutationQueue
134138
.addMutationBatch(txn, Timestamp.now(), [], [mutation])
135139
.next(batch => {
136-
const overlayMap = new Map<DocumentKey, Mutation>();
140+
const overlayMap = newDocumentKeyToMutationMap();
137141
overlayMap.set(mutation.key, mutation);
138142
return documentOverlayCache.saveOverlays(
139143
txn,

0 commit comments

Comments
 (0)