Skip to content

Commit ec8fe08

Browse files
committed
Address comments.
1 parent eeb2243 commit ec8fe08

File tree

5 files changed

+45
-67
lines changed

5 files changed

+45
-67
lines changed

packages/firestore/src/local/local_documents_view.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import {
3030
MutableDocumentMap,
3131
newDocumentKeyMap,
3232
newMutationMap,
33-
newOverlayMap
33+
newOverlayMap,
34+
documentMap,
35+
mutableDocumentMap,
36+
documentKeySet
3437
} from '../model/collections';
3538
import { Document, MutableDocument } from '../model/document';
3639
import { DocumentKey } from '../model/document_key';
@@ -45,7 +48,6 @@ import { Overlay } from '../model/overlay';
4548
import { ResourcePath } from '../model/path';
4649
import { debugAssert } from '../util/assert';
4750
import { SortedMap } from '../util/sorted_map';
48-
import { SortedSet } from '../util/sorted_set';
4951

5052
import { DocumentOverlayCache } from './document_overlay_cache';
5153
import { IndexManager } from './index_manager';
@@ -111,11 +113,9 @@ export class LocalDocumentsView {
111113
return this.remoteDocumentCache
112114
.getEntries(transaction, keys)
113115
.next(docs =>
114-
this.getLocalViewOfDocuments(
115-
transaction,
116-
docs,
117-
new SortedSet(DocumentKey.comparator)
118-
).next(() => docs as DocumentMap)
116+
this.getLocalViewOfDocuments(transaction, docs, documentKeySet()).next(
117+
() => docs as DocumentMap
118+
)
119119
);
120120
}
121121

@@ -152,10 +152,8 @@ export class LocalDocumentsView {
152152
memoizedOverlays: OverlayMap,
153153
existenceStateChanged: DocumentKeySet
154154
): PersistencePromise<DocumentMap> {
155-
let results = new SortedMap<DocumentKey, Document>(DocumentKey.comparator);
156-
let recalculateDocuments = new SortedMap<DocumentKey, MutableDocument>(
157-
DocumentKey.comparator
158-
);
155+
let results = documentMap();
156+
let recalculateDocuments = mutableDocumentMap();
159157
const promises: Array<PersistencePromise<void>> = [];
160158
docs.forEach((_, doc) => {
161159
const overlayPromise = memoizedOverlays.has(doc.key)
@@ -209,21 +207,21 @@ export class LocalDocumentsView {
209207
let documentsByBatchId = new SortedMap<number, DocumentKeySet>(
210208
(key1: number, key2: number) => key1 - key2
211209
);
212-
let processed = new SortedSet(DocumentKey.comparator);
210+
let processed = documentKeySet();
213211
return this.mutationQueue
214212
.getAllMutationBatchesAffectingDocumentKeys(transaction, docs)
215213
.next(batches => {
216-
batches.forEach(batch => {
214+
for (const batch of batches) {
217215
batch.keys().forEach(key => {
218216
let mask: FieldMask | null = masks.has(key)
219217
? masks.get(key)!
220218
: FieldMask.empty();
221-
mask = batch.applyToLocalViewWithFieldMask(docs.get(key)!, mask);
219+
mask = batch.applyToLocalView(docs.get(key)!, mask);
222220
masks.set(key, mask);
223221
if (documentsByBatchId.get(batch.batchId) === null) {
224222
documentsByBatchId = documentsByBatchId.insert(
225223
batch.batchId,
226-
new SortedSet(DocumentKey.comparator)
224+
documentKeySet()
227225
);
228226
}
229227
const newSet = documentsByBatchId.get(batch.batchId)!.add(key);
@@ -232,7 +230,7 @@ export class LocalDocumentsView {
232230
newSet
233231
);
234232
});
235-
});
233+
}
236234
})
237235
.next(() => {
238236
const promises: Array<PersistencePromise<void>> = [];
@@ -246,8 +244,6 @@ export class LocalDocumentsView {
246244
const overlays = newMutationMap();
247245
keys.forEach(key => {
248246
if (!processed.has(key)) {
249-
// TODO: Should we change `overlays` type to Map<DK, Mutation|null>
250-
// and update `saveOverlays` to accept (and skip) null values?
251247
const overlayMutation = calculateOverlayMutation(
252248
docs.get(key)!,
253249
masks.get(key)!
@@ -322,9 +318,7 @@ export class LocalDocumentsView {
322318
// Just do a simple document lookup.
323319
return this.getDocument(transaction, new DocumentKey(docPath)).next(
324320
document => {
325-
let result = new SortedMap<DocumentKey, Document>(
326-
DocumentKey.comparator
327-
);
321+
let result = documentMap();
328322
if (document.isFoundDocument()) {
329323
result = result.insert(document.key, document);
330324
}
@@ -343,7 +337,7 @@ export class LocalDocumentsView {
343337
'Currently we only support collection group queries at the root.'
344338
);
345339
const collectionId = query.collectionGroup!;
346-
let results = new SortedMap<DocumentKey, Document>(DocumentKey.comparator);
340+
let results = documentMap();
347341
return this.indexManager
348342
.getCollectionParents(transaction, collectionId)
349343
.next(parents => {
@@ -398,9 +392,7 @@ export class LocalDocumentsView {
398392
});
399393

400394
// Apply the overlays and match against the query.
401-
let results = new SortedMap<DocumentKey, Document>(
402-
DocumentKey.comparator
403-
);
395+
let results = documentMap();
404396
remoteDocuments.forEach((key, document) => {
405397
const overlay = overlays.get(key);
406398
if (overlay !== undefined) {

packages/firestore/src/local/local_store_impl.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,9 @@ class LocalStoreImpl implements LocalStore {
218218
}
219219
}
220220

221-
class DocumentChangeResult {
222-
constructor(
223-
readonly changedDocuments: MutableDocumentMap,
224-
readonly existenceChangedKeys: DocumentKeySet
225-
) {}
221+
interface DocumentChangeResult {
222+
changedDocuments: MutableDocumentMap;
223+
existenceChangedKeys: DocumentKeySet;
226224
}
227225

228226
export function newLocalStore(
@@ -666,17 +664,10 @@ export function localStoreApplyRemoteEventToLocalCache(
666664
* Returns the document changes resulting from applying those documents, and
667665
* also a set of documents whose existence state are changed as a result.
668666
*
669-
* Note: this function will use `documentVersions` if it is defined;
670-
* when it is not defined, resorts to `globalVersion`.
671-
*
672667
* @param txn - Transaction to use to read existing documents from storage.
673668
* @param documentBuffer - Document buffer to collect the resulted changes to be
674669
* applied to storage.
675670
* @param documents - Documents to be applied.
676-
* @param globalVersion - A `SnapshotVersion` representing the read time if all
677-
* documents have the same read time.
678-
* @param documentVersions - A DocumentKey-to-SnapshotVersion map if documents
679-
* have their own read time.
680671
*/
681672
function populateDocumentChangeBuffer(
682673
txn: PersistenceTransaction,
@@ -730,7 +721,10 @@ function populateDocumentChangeBuffer(
730721
);
731722
}
732723
});
733-
return new DocumentChangeResult(changedDocs, conditionChanged);
724+
return {
725+
changedDocuments: changedDocs,
726+
existenceChangedKeys: conditionChanged
727+
};
734728
});
735729
}
736730

packages/firestore/src/model/mutation.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,11 @@ export function calculateOverlayMutation(
224224
doc: MutableDocument,
225225
mask: FieldMask | null
226226
): Mutation | null {
227-
if (!doc.hasLocalMutations || (mask !== null && mask!.fields.length === 0)) {
227+
if (!doc.hasLocalMutations || (mask && mask!.fields.length === 0)) {
228228
return null;
229229
}
230230

231-
// mask is null when there are Set or Delete being applied to get to the
232-
// current document.
231+
// mask is null when sets or deletes are applied to the current document.
233232
if (mask === null) {
234233
if (doc.isNoDocument()) {
235234
return new DeleteMutation(doc.key, Precondition.none());
@@ -240,7 +239,7 @@ export function calculateOverlayMutation(
240239
const docValue = doc.data;
241240
const patchValue = ObjectValue.empty();
242241
let maskSet = new SortedSet<FieldPath>(FieldPath.comparator);
243-
mask.fields.forEach(path => {
242+
for (let path of mask.fields) {
244243
if (!maskSet.has(path)) {
245244
let value = docValue.field(path);
246245
// If we are deleting a nested field, we take the immediate parent as
@@ -263,7 +262,7 @@ export function calculateOverlayMutation(
263262
}
264263
maskSet = maskSet.add(path);
265264
}
266-
});
265+
}
267266
return new PatchMutation(
268267
doc.key,
269268
patchValue,
@@ -489,8 +488,7 @@ function setMutationApplyToLocalView(
489488
document
490489
.convertToFoundDocument(document.version, newData)
491490
.setHasLocalMutations();
492-
// SetMutation overwrites all fields.
493-
return null;
491+
return null; // SetMutation overwrites all fields.
494492
}
495493

496494
/**
@@ -574,12 +572,12 @@ function patchMutationApplyToLocalView(
574572
}
575573

576574
let mergedMaskSet = new SortedSet<FieldPath>(FieldPath.comparator);
577-
previousMask.fields.forEach(
578-
fieldPath => (mergedMaskSet = mergedMaskSet.add(fieldPath))
579-
);
580-
mutation.fieldMask.fields.forEach(
581-
fieldPath => (mergedMaskSet = mergedMaskSet.add(fieldPath))
582-
);
575+
for (const fieldPath of previousMask.fields) {
576+
mergedMaskSet = mergedMaskSet.add(fieldPath);
577+
}
578+
for (const fieldPath of mutation.fieldMask.fields) {
579+
mergedMaskSet = mergedMaskSet.add(fieldPath);
580+
}
583581
mutation.fieldTransforms
584582
.map(transform => transform.field)
585583
.forEach(fieldPath => (mergedMaskSet = mergedMaskSet.add(fieldPath)));

packages/firestore/src/model/mutation_batch.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,6 @@ export class MutationBatch {
9393
}
9494
}
9595
}
96-
/**
97-
* Computes the local view of a document given all the mutations in this
98-
* batch.
99-
*
100-
* @param document - The document to apply mutations to.
101-
* @returns A `FieldMask` representing all the fields that are mutated.
102-
*/
103-
applyToLocalView(document: MutableDocument): FieldMask | null {
104-
return this.applyToLocalViewWithFieldMask(document, FieldMask.empty());
105-
}
10696

10797
/**
10898
* Computes the local view of a document given all the mutations in this
@@ -112,9 +102,9 @@ export class MutationBatch {
112102
* @param mutatedFields - Fields that have been updated before applying this mutation batch.
113103
* @returns A `FieldMask` representing all the fields that are mutated.
114104
*/
115-
applyToLocalViewWithFieldMask(
105+
applyToLocalView(
116106
document: MutableDocument,
117-
mutatedFields: FieldMask | null
107+
mutatedFields: FieldMask | null = FieldMask.empty()
118108
): FieldMask | null {
119109
// First, apply the base state. This allows us to apply non-idempotent
120110
// transform against a consistent set of values.

packages/firestore/test/util/helpers.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export function mergeMutation(
297297
return patchMutationHelper(keyStr, json, Precondition.none(), updateMask);
298298
}
299299

300-
export function patchMutationHelper(
300+
function patchMutationHelper(
301301
keyStr: string,
302302
json: JsonObject<unknown>,
303303
precondition: Precondition,
@@ -319,7 +319,7 @@ export function patchMutationHelper(
319319

320320
// `mergeMutation()` provides an update mask for the merged fields, whereas
321321
// `patchMutation()` requires the update mask to be parsed from the values.
322-
const mask = updateMask !== null ? updateMask : parsed.fieldMask.fields;
322+
const mask = updateMask ? updateMask : parsed.fieldMask.fields;
323323

324324
// We sort the fieldMaskPaths to make the order deterministic in tests.
325325
// (Otherwise, when we flatten a Set to a proto repeated field, we'll end up
@@ -1008,7 +1008,10 @@ export function forEachNumber<V>(
10081008
}
10091009
}
10101010

1011-
/** Returns all possible permutations of the given array. */
1011+
/**
1012+
* Returns all possible permutations of the given array.
1013+
* For `[a, b]`, this method returns `[[a, b], [b, a]]`.
1014+
*/
10121015
export function computePermutations<T>(input: T[]): T[][] {
10131016
if (input.length === 0) {
10141017
return [[]];
@@ -1033,7 +1036,8 @@ export function computePermutations<T>(input: T[]): T[][] {
10331036

10341037
/**
10351038
* Returns all possible combinations of the given array, including an empty
1036-
* array.
1039+
* array. For `[a, b, c]` this method returns
1040+
* `[[], [a], [a, b], [a, c], [b, c], [a, b, c]`.
10371041
*/
10381042
export function computeCombinations<T>(input: T[]): T[][] {
10391043
const computeNonEmptyCombinations = (input: T[]): T[][] => {

0 commit comments

Comments
 (0)