Skip to content

Commit b6989ca

Browse files
Simplify Mutations & small bug fixes
1 parent f23b397 commit b6989ca

File tree

6 files changed

+11
-26
lines changed

6 files changed

+11
-26
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalStore.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ public LocalWriteResult writeLocally(List<Mutation> mutations) {
216216
() -> {
217217
// Load and apply all existing mutations. This lets us compute the current base state for
218218
// all non-idempotent transforms before applying any additional user-provided writes.
219-
ImmutableSortedMap<DocumentKey, Document> existingDocuments =
220-
localDocuments.getDocuments(keys);
219+
ImmutableSortedMap<DocumentKey, Document> documents = localDocuments.getDocuments(keys);
221220

222221
// For non-idempotent mutations (such as `FieldValue.increment()`), we record the base
223222
// state in a separate patch mutation. This is later used to guarantee consistent values
@@ -226,7 +225,7 @@ public LocalWriteResult writeLocally(List<Mutation> mutations) {
226225
List<Mutation> baseMutations = new ArrayList<>();
227226
for (Mutation mutation : mutations) {
228227
ObjectValue baseValue =
229-
mutation.extractTransformBaseValue(existingDocuments.get(mutation.getKey()));
228+
mutation.extractTransformBaseValue(documents.get(mutation.getKey()));
230229
if (baseValue != null) {
231230
// NOTE: The base state should only be applied if there's some existing
232231
// document to override, so use a Precondition of exists=true
@@ -241,9 +240,8 @@ public LocalWriteResult writeLocally(List<Mutation> mutations) {
241240

242241
MutationBatch batch =
243242
mutationQueue.addMutationBatch(localWriteTime, baseMutations, mutations);
244-
ImmutableSortedMap<DocumentKey, Document> changedDocuments =
245-
batch.applyToLocalDocumentSet(existingDocuments);
246-
return new LocalWriteResult(batch.getBatchId(), changedDocuments);
243+
batch.applyToLocalDocumentSet(documents);
244+
return new LocalWriteResult(batch.getBatchId(), documents);
247245
});
248246
}
249247

firebase-firestore/src/main/java/com/google/firebase/firestore/model/DatabaseId.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private DatabaseId(String projectId, String databaseId) {
4343
public static DatabaseId fromName(String name) {
4444
ResourcePath resourceName = ResourcePath.fromString(name);
4545
hardAssert(
46-
resourceName.length() >= 3
46+
resourceName.length() > 3
4747
&& resourceName.getSegment(0).equals("projects")
4848
&& resourceName.getSegment(2).equals("databases"),
4949
"Tried to parse an invalid resource name: %s",

firebase-firestore/src/main/java/com/google/firebase/firestore/model/DocumentKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static DocumentKey empty() {
5151
public static DocumentKey fromName(String name) {
5252
ResourcePath resourceName = ResourcePath.fromString(name);
5353
hardAssert(
54-
resourceName.length() >= 4
54+
resourceName.length() > 4
5555
&& resourceName.getSegment(0).equals("projects")
5656
&& resourceName.getSegment(2).equals("databases")
5757
&& resourceName.getSegment(4).equals("documents"),

firebase-firestore/src/main/java/com/google/firebase/firestore/model/Values.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,12 @@ private static void canonifyArray(StringBuilder builder, ArrayValue arrayValue)
386386
builder.append("]");
387387
}
388388

389-
/** Returns true if `value` is either a INTEGER_VALUE. */
389+
/** Returns true if `value` is a INTEGER_VALUE. */
390390
public static boolean isInteger(@Nullable Value value) {
391391
return value != null && value.getValueTypeCase() == Value.ValueTypeCase.INTEGER_VALUE;
392392
}
393393

394-
/** Returns true if `value` is either a DOUBLE_VALUE. */
394+
/** Returns true if `value` is a DOUBLE_VALUE. */
395395
public static boolean isDouble(@Nullable Value value) {
396396
return value != null && value.getValueTypeCase() == Value.ValueTypeCase.DOUBLE_VALUE;
397397
}

firebase-firestore/src/main/java/com/google/firebase/firestore/model/mutation/Mutation.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,7 @@ protected Map<FieldPath, Value> serverTransformResults(
170170
for (int i = 0; i < serverTransformResults.size(); i++) {
171171
FieldTransform fieldTransform = fieldTransforms.get(i);
172172
TransformOperation transform = fieldTransform.getOperation();
173-
174-
Value previousValue = null;
175-
if (mutableDocument.isFoundDocument()) {
176-
previousValue = mutableDocument.getField(fieldTransform.getFieldPath());
177-
}
178-
173+
Value previousValue = mutableDocument.getField(fieldTransform.getFieldPath());
179174
transformResults.put(
180175
fieldTransform.getFieldPath(),
181176
transform.applyToRemoteDocument(previousValue, serverTransformResults.get(i)));
@@ -196,12 +191,7 @@ protected Map<FieldPath, Value> localTransformResults(
196191
Map<FieldPath, Value> transformResults = new HashMap<>(fieldTransforms.size());
197192
for (FieldTransform fieldTransform : fieldTransforms) {
198193
TransformOperation transform = fieldTransform.getOperation();
199-
200-
Value previousValue = null;
201-
if (mutableDocument.isFoundDocument()) {
202-
previousValue = mutableDocument.getField(fieldTransform.getFieldPath());
203-
}
204-
194+
Value previousValue = mutableDocument.getField(fieldTransform.getFieldPath());
205195
transformResults.put(
206196
fieldTransform.getFieldPath(), transform.applyToLocalView(previousValue, localWriteTime));
207197
}

firebase-firestore/src/main/java/com/google/firebase/firestore/model/mutation/MutationBatch.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ public void applyToLocalView(MutableDocument document) {
118118
}
119119

120120
/** Computes the local view for all provided documents given the mutations in this batch. */
121-
public ImmutableSortedMap<DocumentKey, Document> applyToLocalDocumentSet(
122-
ImmutableSortedMap<DocumentKey, Document> documentMap) {
121+
public void applyToLocalDocumentSet(ImmutableSortedMap<DocumentKey, Document> documentMap) {
123122
// TODO(mrschmidt): This implementation is O(n^2). If we iterate through the mutations first
124123
// (as done in `applyToLocalView(MutableDocument d)`), we can reduce the complexity to
125124
// O(n).
@@ -131,9 +130,7 @@ public ImmutableSortedMap<DocumentKey, Document> applyToLocalDocumentSet(
131130
if (!document.isValidDocument()) {
132131
document.convertToNoDocument(SnapshotVersion.NONE);
133132
}
134-
documentMap = documentMap.insert(document.getKey(), document);
135133
}
136-
return documentMap;
137134
}
138135

139136
@Override

0 commit comments

Comments
 (0)