Skip to content

Remove special handling in Mutations #2993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.firebase.Timestamp;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.MutableDocument;
import com.google.firebase.firestore.model.SnapshotVersion;

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

if (getPrecondition().isValidFor(document)) {
document.convertToNoDocument(SnapshotVersion.NONE);
document.convertToNoDocument(document.getVersion()).setHasLocalMutations();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.firebase.firestore.model.FieldPath;
import com.google.firebase.firestore.model.MutableDocument;
import com.google.firebase.firestore.model.ObjectValue;
import com.google.firebase.firestore.model.SnapshotVersion;
import com.google.firestore.v1.Value;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -136,19 +135,6 @@ void verifyKeyMatches(MutableDocument document) {
"Can only apply a mutation to a document with the same key");
}

/**
* Returns the version from the given document for use as the result of a mutation. Mutations are
* defined to return the version of the base document only if it is an existing document. Deleted
* and unknown documents have a post-mutation version of {@code SnapshotVersion.NONE}.
*/
static SnapshotVersion getPostMutationVersion(MutableDocument document) {
if (document.isFoundDocument()) {
return document.getVersion();
} else {
return SnapshotVersion.NONE;
}
}

/**
* Creates a list of "transform results" (a transform result is a field value representing the
* result of applying a transform) for use after a mutation containing transforms has been
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void applyToLocalView(MutableDocument document, Timestamp localWriteTime)
value.setAll(getPatch());
value.setAll(transformResults);
document
.convertToFoundDocument(getPostMutationVersion(document), document.getData())
.convertToFoundDocument(document.getVersion(), document.getData())
.setHasLocalMutations();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ public void applyToLocalView(MutableDocument document, Timestamp localWriteTime)
Map<FieldPath, Value> transformResults = localTransformResults(localWriteTime, document);
ObjectValue localValue = value.clone();
localValue.setAll(transformResults);
document
.convertToFoundDocument(getPostMutationVersion(document), localValue)
.setHasLocalMutations();
document.convertToFoundDocument(document.getVersion(), localValue).setHasLocalMutations();
}

/** Returns the object value to use when setting the document. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,9 @@ public void testHandlesDeletedDocumentThenSetMutationThenAck() {
}

writeMutation(setMutation("foo/bar", map("foo", "bar")));
assertChanged(doc("foo/bar", 0, map("foo", "bar")).setHasLocalMutations());
assertContains(doc("foo/bar", 0, map("foo", "bar")).setHasLocalMutations());
int expectedVersion = garbageCollectorIsEager() ? 0 : 2;
assertChanged(doc("foo/bar", expectedVersion, map("foo", "bar")).setHasLocalMutations());
assertContains(doc("foo/bar", expectedVersion, map("foo", "bar")).setHasLocalMutations());

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

applyRemoteEvent(updateRemoteEvent(deletedDoc("foo/bar", 2), asList(targetId), emptyList()));
assertChanged(doc("foo/bar", 0, map("foo", "bar")).setHasLocalMutations());
assertContains(doc("foo/bar", 0, map("foo", "bar")).setHasLocalMutations());
assertChanged(doc("foo/bar", 2, map("foo", "bar")).setHasLocalMutations());
assertContains(doc("foo/bar", 2, map("foo", "bar")).setHasLocalMutations());
}

@Test
Expand Down Expand Up @@ -566,7 +567,7 @@ public void testHandlesPatchMutationThenAckThenDocument() {
public void testHandlesDeleteMutationThenAck() {
writeMutation(deleteMutation("foo/bar"));
assertRemoved("foo/bar");
assertContains(deletedDoc("foo/bar", 0));
assertContains(deletedDoc("foo/bar", 0).setHasLocalMutations());

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

writeMutation(deleteMutation("foo/bar"));
assertRemoved("foo/bar");
assertContains(deletedDoc("foo/bar", 0));
assertContains(deletedDoc("foo/bar", 1).setHasLocalMutations());

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

applyRemoteEvent(
updateRemoteEvent(doc("foo/bar", 1, map("it", "base")), asList(targetId), emptyList()));
assertRemoved("foo/bar");
assertContains(deletedDoc("foo/bar", 0));
assertContains(deletedDoc("foo/bar", 1).setHasLocalMutations());

releaseTarget(targetId);
acknowledgeMutation(2);
Expand Down Expand Up @@ -731,11 +732,11 @@ public void testHandlesSetMutationsAndPatchMutationOfJustOneTogether() {
public void testHandlesDeleteMutationThenPatchMutationThenAckThenAck() {
writeMutation(deleteMutation("foo/bar"));
assertRemoved("foo/bar");
assertContains(deletedDoc("foo/bar", 0));
assertContains(deletedDoc("foo/bar", 0).setHasLocalMutations());

writeMutation(patchMutation("foo/bar", map("foo", "bar")));
assertRemoved("foo/bar");
assertContains(deletedDoc("foo/bar", 0));
assertContains(deletedDoc("foo/bar", 0).setHasLocalMutations());

acknowledgeMutation(2); // delete mutation
assertRemoved("foo/bar");
Expand Down Expand Up @@ -798,17 +799,17 @@ public void testCollectsGarbageAfterAcknowledgedMutation() {
writeMutation(deleteMutation("foo/baz"));
assertContains(doc("foo/bar", 1, map("foo", "bar")).setHasLocalMutations());
assertContains(doc("foo/bah", 0, map("foo", "bah")).setHasLocalMutations());
assertContains(deletedDoc("foo/baz", 0));
assertContains(deletedDoc("foo/baz", 0).setHasLocalMutations());

acknowledgeMutation(3);
assertNotContains("foo/bar");
assertContains(doc("foo/bah", 0, map("foo", "bah")).setHasLocalMutations());
assertContains(deletedDoc("foo/baz", 0));
assertContains(deletedDoc("foo/baz", 0).setHasLocalMutations());

acknowledgeMutation(4);
assertNotContains("foo/bar");
assertNotContains("foo/bah");
assertContains(deletedDoc("foo/baz", 0));
assertContains(deletedDoc("foo/baz", 0).setHasLocalMutations());

acknowledgeMutation(5);
assertNotContains("foo/bar");
Expand All @@ -831,17 +832,17 @@ public void testCollectsGarbageAfterRejectedMutation() {
writeMutation(deleteMutation("foo/baz"));
assertContains(doc("foo/bar", 1, map("foo", "bar")).setHasLocalMutations());
assertContains(doc("foo/bah", 0, map("foo", "bah")).setHasLocalMutations());
assertContains(deletedDoc("foo/baz", 0));
assertContains(deletedDoc("foo/baz", 0).setHasLocalMutations());

rejectMutation(); // patch mutation
assertNotContains("foo/bar");
assertContains(doc("foo/bah", 0, map("foo", "bah")).setHasLocalMutations());
assertContains(deletedDoc("foo/baz", 0));
assertContains(deletedDoc("foo/baz", 0).setHasLocalMutations());

rejectMutation(); // set mutation
assertNotContains("foo/bar");
assertNotContains("foo/bah");
assertContains(deletedDoc("foo/baz", 0));
assertContains(deletedDoc("foo/baz", 0).setHasLocalMutations());

rejectMutation(); // delete mutation
assertNotContains("foo/bar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ public void testDeleteDeletes() {

Mutation delete = deleteMutation("collection/key");
delete.applyToLocalView(deletedDoc, Timestamp.now());
assertEquals(deletedDoc("collection/key", 0), deletedDoc);
assertEquals(deletedDoc("collection/key", 0).setHasLocalMutations(), deletedDoc);
}

@Test
Expand Down