Skip to content

Commit a1755d3

Browse files
Remove special handling in Mutations
1 parent 90efb98 commit a1755d3

File tree

6 files changed

+21
-37
lines changed

6 files changed

+21
-37
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.google.firebase.Timestamp;
2020
import com.google.firebase.firestore.model.DocumentKey;
2121
import com.google.firebase.firestore.model.MutableDocument;
22-
import com.google.firebase.firestore.model.SnapshotVersion;
2322

2423
/** Represents a Delete operation */
2524
public final class DeleteMutation extends Mutation {
@@ -72,7 +71,7 @@ public void applyToLocalView(MutableDocument document, Timestamp localWriteTime)
7271
verifyKeyMatches(document);
7372

7473
if (getPrecondition().isValidFor(document)) {
75-
document.convertToNoDocument(SnapshotVersion.NONE);
74+
document.convertToNoDocument(document.getVersion()).setHasLocalMutations();
7675
}
7776
}
7877
}

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.firebase.firestore.model.FieldPath;
2323
import com.google.firebase.firestore.model.MutableDocument;
2424
import com.google.firebase.firestore.model.ObjectValue;
25-
import com.google.firebase.firestore.model.SnapshotVersion;
2625
import com.google.firestore.v1.Value;
2726
import java.util.ArrayList;
2827
import java.util.HashMap;
@@ -136,19 +135,6 @@ void verifyKeyMatches(MutableDocument document) {
136135
"Can only apply a mutation to a document with the same key");
137136
}
138137

139-
/**
140-
* Returns the version from the given document for use as the result of a mutation. Mutations are
141-
* defined to return the version of the base document only if it is an existing document. Deleted
142-
* and unknown documents have a post-mutation version of {@code SnapshotVersion.NONE}.
143-
*/
144-
static SnapshotVersion getPostMutationVersion(MutableDocument document) {
145-
if (document.isFoundDocument()) {
146-
return document.getVersion();
147-
} else {
148-
return SnapshotVersion.NONE;
149-
}
150-
}
151-
152138
/**
153139
* Creates a list of "transform results" (a transform result is a field value representing the
154140
* result of applying a transform) for use after a mutation containing transforms has been

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void applyToLocalView(MutableDocument document, Timestamp localWriteTime)
138138
value.setAll(getPatch());
139139
value.setAll(transformResults);
140140
document
141-
.convertToFoundDocument(getPostMutationVersion(document), document.getData())
141+
.convertToFoundDocument(document.getVersion(), document.getData())
142142
.setHasLocalMutations();
143143
}
144144

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ public void applyToLocalView(MutableDocument document, Timestamp localWriteTime)
9696
Map<FieldPath, Value> transformResults = localTransformResults(localWriteTime, document);
9797
ObjectValue localValue = value.clone();
9898
localValue.setAll(transformResults);
99-
document
100-
.convertToFoundDocument(getPostMutationVersion(document), localValue)
101-
.setHasLocalMutations();
99+
document.convertToFoundDocument(document.getVersion(), localValue).setHasLocalMutations();
102100
}
103101

104102
/** Returns the object value to use when setting the document. */

firebase-firestore/src/test/java/com/google/firebase/firestore/local/LocalStoreTestCase.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,9 @@ public void testHandlesDeletedDocumentThenSetMutationThenAck() {
444444
}
445445

446446
writeMutation(setMutation("foo/bar", map("foo", "bar")));
447-
assertChanged(doc("foo/bar", 0, map("foo", "bar")).setHasLocalMutations());
448-
assertContains(doc("foo/bar", 0, map("foo", "bar")).setHasLocalMutations());
447+
int expectedVersion = garbageCollectorIsEager() ? 0 : 2;
448+
assertChanged(doc("foo/bar", expectedVersion, map("foo", "bar")).setHasLocalMutations());
449+
assertContains(doc("foo/bar", expectedVersion, map("foo", "bar")).setHasLocalMutations());
449450

450451
releaseTarget(targetId);
451452
acknowledgeMutation(3);
@@ -464,8 +465,8 @@ public void testHandlesSetMutationThenDeletedDocument() {
464465
assertChanged(doc("foo/bar", 0, map("foo", "bar")).setHasLocalMutations());
465466

466467
applyRemoteEvent(updateRemoteEvent(deletedDoc("foo/bar", 2), asList(targetId), emptyList()));
467-
assertChanged(doc("foo/bar", 0, map("foo", "bar")).setHasLocalMutations());
468-
assertContains(doc("foo/bar", 0, map("foo", "bar")).setHasLocalMutations());
468+
assertChanged(doc("foo/bar", 2, map("foo", "bar")).setHasLocalMutations());
469+
assertContains(doc("foo/bar", 2, map("foo", "bar")).setHasLocalMutations());
469470
}
470471

471472
@Test
@@ -566,7 +567,7 @@ public void testHandlesPatchMutationThenAckThenDocument() {
566567
public void testHandlesDeleteMutationThenAck() {
567568
writeMutation(deleteMutation("foo/bar"));
568569
assertRemoved("foo/bar");
569-
assertContains(deletedDoc("foo/bar", 0));
570+
assertContains(deletedDoc("foo/bar", 0).setHasLocalMutations());
570571

571572
acknowledgeMutation(1);
572573
assertRemoved("foo/bar");
@@ -589,7 +590,7 @@ public void testHandlesDocumentThenDeleteMutationThenAck() {
589590

590591
writeMutation(deleteMutation("foo/bar"));
591592
assertRemoved("foo/bar");
592-
assertContains(deletedDoc("foo/bar", 0));
593+
assertContains(deletedDoc("foo/bar", 1).setHasLocalMutations());
593594

594595
// Remove the target so only the mutation is pinning the document.
595596
releaseTarget(targetId);
@@ -608,12 +609,12 @@ public void testHandlesDeleteMutationThenDocumentThenAck() {
608609
int targetId = allocateQuery(query);
609610
writeMutation(deleteMutation("foo/bar"));
610611
assertRemoved("foo/bar");
611-
assertContains(deletedDoc("foo/bar", 0));
612+
assertContains(deletedDoc("foo/bar", 0).setHasLocalMutations());
612613

613614
applyRemoteEvent(
614615
updateRemoteEvent(doc("foo/bar", 1, map("it", "base")), asList(targetId), emptyList()));
615616
assertRemoved("foo/bar");
616-
assertContains(deletedDoc("foo/bar", 0));
617+
assertContains(deletedDoc("foo/bar", 1).setHasLocalMutations());
617618

618619
releaseTarget(targetId);
619620
acknowledgeMutation(2);
@@ -731,11 +732,11 @@ public void testHandlesSetMutationsAndPatchMutationOfJustOneTogether() {
731732
public void testHandlesDeleteMutationThenPatchMutationThenAckThenAck() {
732733
writeMutation(deleteMutation("foo/bar"));
733734
assertRemoved("foo/bar");
734-
assertContains(deletedDoc("foo/bar", 0));
735+
assertContains(deletedDoc("foo/bar", 0).setHasLocalMutations());
735736

736737
writeMutation(patchMutation("foo/bar", map("foo", "bar")));
737738
assertRemoved("foo/bar");
738-
assertContains(deletedDoc("foo/bar", 0));
739+
assertContains(deletedDoc("foo/bar", 0).setHasLocalMutations());
739740

740741
acknowledgeMutation(2); // delete mutation
741742
assertRemoved("foo/bar");
@@ -798,17 +799,17 @@ public void testCollectsGarbageAfterAcknowledgedMutation() {
798799
writeMutation(deleteMutation("foo/baz"));
799800
assertContains(doc("foo/bar", 1, map("foo", "bar")).setHasLocalMutations());
800801
assertContains(doc("foo/bah", 0, map("foo", "bah")).setHasLocalMutations());
801-
assertContains(deletedDoc("foo/baz", 0));
802+
assertContains(deletedDoc("foo/baz", 0).setHasLocalMutations());
802803

803804
acknowledgeMutation(3);
804805
assertNotContains("foo/bar");
805806
assertContains(doc("foo/bah", 0, map("foo", "bah")).setHasLocalMutations());
806-
assertContains(deletedDoc("foo/baz", 0));
807+
assertContains(deletedDoc("foo/baz", 0).setHasLocalMutations());
807808

808809
acknowledgeMutation(4);
809810
assertNotContains("foo/bar");
810811
assertNotContains("foo/bah");
811-
assertContains(deletedDoc("foo/baz", 0));
812+
assertContains(deletedDoc("foo/baz", 0).setHasLocalMutations());
812813

813814
acknowledgeMutation(5);
814815
assertNotContains("foo/bar");
@@ -831,17 +832,17 @@ public void testCollectsGarbageAfterRejectedMutation() {
831832
writeMutation(deleteMutation("foo/baz"));
832833
assertContains(doc("foo/bar", 1, map("foo", "bar")).setHasLocalMutations());
833834
assertContains(doc("foo/bah", 0, map("foo", "bah")).setHasLocalMutations());
834-
assertContains(deletedDoc("foo/baz", 0));
835+
assertContains(deletedDoc("foo/baz", 0).setHasLocalMutations());
835836

836837
rejectMutation(); // patch mutation
837838
assertNotContains("foo/bar");
838839
assertContains(doc("foo/bah", 0, map("foo", "bah")).setHasLocalMutations());
839-
assertContains(deletedDoc("foo/baz", 0));
840+
assertContains(deletedDoc("foo/baz", 0).setHasLocalMutations());
840841

841842
rejectMutation(); // set mutation
842843
assertNotContains("foo/bar");
843844
assertNotContains("foo/bah");
844-
assertContains(deletedDoc("foo/baz", 0));
845+
assertContains(deletedDoc("foo/baz", 0).setHasLocalMutations());
845846

846847
rejectMutation(); // delete mutation
847848
assertNotContains("foo/bar");

firebase-firestore/src/test/java/com/google/firebase/firestore/model/MutationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public void testDeleteDeletes() {
526526

527527
Mutation delete = deleteMutation("collection/key");
528528
delete.applyToLocalView(deletedDoc, Timestamp.now());
529-
assertEquals(deletedDoc("collection/key", 0), deletedDoc);
529+
assertEquals(deletedDoc("collection/key", 0).setHasLocalMutations(), deletedDoc);
530530
}
531531

532532
@Test

0 commit comments

Comments
 (0)