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
fix: replace usages of transform proto with update_transform #213
Changes from 1 commit
2d6b08b
3c1ddae
56a6236
a1bbee4
6ef32c6
6dfd910
1aa5e44
3f8b653
069eba2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,12 +126,10 @@ private T performCreate( | |
Mutation mutation = addMutation(); | ||
mutation.precondition = Precondition.exists(false).toPb(); | ||
|
||
if (!documentSnapshot.isEmpty() || documentTransform.isEmpty()) { | ||
mutation.document = documentSnapshot.toPb(); | ||
} | ||
mutation.document = documentSnapshot.toPb(); | ||
|
||
if (!documentTransform.isEmpty()) { | ||
mutation.transform = documentTransform.toPb(); | ||
mutation.document.addAllUpdateTransforms(documentTransform.toPb()); | ||
} | ||
|
||
return (T) this; | ||
|
@@ -248,29 +246,23 @@ private T performSet( | |
DocumentTransform documentTransform = | ||
DocumentTransform.fromFieldPathMap(documentReference, documentData); | ||
|
||
if (options.isMerge()) { | ||
if (options.getFieldMask() != null) { | ||
List<FieldPath> fieldMask = new ArrayList<>(options.getFieldMask()); | ||
fieldMask.removeAll(documentTransform.getFields()); | ||
documentMask = new FieldMask(fieldMask); | ||
} else { | ||
documentMask = FieldMask.fromObject(fields); | ||
} | ||
if (options.getFieldMask() != null) { | ||
List<FieldPath> fieldMask = new ArrayList<>(options.getFieldMask()); | ||
fieldMask.removeAll(documentTransform.getFields()); | ||
documentMask = new FieldMask(fieldMask); | ||
} else if (options.isMerge()) { | ||
documentMask = FieldMask.fromObject(fields); | ||
} | ||
|
||
Mutation mutation = addMutation(); | ||
|
||
boolean hasDocumentData = !documentSnapshot.isEmpty() || !documentMask.isEmpty(); | ||
|
||
if (!options.isMerge()) { | ||
mutation.document = documentSnapshot.toPb(); | ||
} else if (hasDocumentData || documentTransform.isEmpty()) { | ||
mutation.document = documentSnapshot.toPb(); | ||
mutation.document.setUpdateMask(documentMask.toPb()); | ||
mutation.document = documentSnapshot.toPb(); | ||
if (!documentTransform.isEmpty()) { | ||
mutation.document.addAllUpdateTransforms(documentTransform.toPb()); | ||
} | ||
|
||
if (!documentTransform.isEmpty()) { | ||
mutation.transform = documentTransform.toPb(); | ||
if (options.isMerge() || options.getFieldMask() != null) { | ||
mutation.document.setUpdateMask(documentMask.toPb()); | ||
} | ||
|
||
return (T) this; | ||
|
@@ -535,13 +527,11 @@ public boolean allowTransform() { | |
Mutation mutation = addMutation(); | ||
mutation.precondition = precondition.toPb(); | ||
|
||
if (!documentSnapshot.isEmpty() || !fieldMask.isEmpty()) { | ||
mutation.document = documentSnapshot.toPb(); | ||
mutation.document.setUpdateMask(fieldMask.toPb()); | ||
} | ||
mutation.document = documentSnapshot.toPb(); | ||
mutation.document.setUpdateMask(fieldMask.toPb()); | ||
|
||
if (!documentTransform.isEmpty()) { | ||
mutation.transform = documentTransform.toPb(); | ||
mutation.document.addAllUpdateTransforms(documentTransform.toPb()); | ||
} | ||
|
||
return (T) this; | ||
|
@@ -595,22 +585,11 @@ ApiFuture<List<WriteResult>> commit(@Nullable ByteString transactionId) { | |
request.setDatabase(firestore.getDatabaseName()); | ||
|
||
for (Mutation mutation : mutations) { | ||
Preconditions.checkState( | ||
mutation.document != null || mutation.transform != null, | ||
"Either a write or transform must be set"); | ||
|
||
if (mutation.precondition != null) { | ||
(mutation.document != null ? mutation.document : mutation.transform) | ||
.setCurrentDocument(mutation.precondition); | ||
} | ||
|
||
if (mutation.document != null) { | ||
request.addWrites(mutation.document); | ||
mutation.document.setCurrentDocument(mutation.precondition); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we use the Write proto directly instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using the write proto directly now, but I can't see how to remove this line. Don't we always need to check if the precondition exists before setting it onto the write? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, sorry - I thought this was in "commit()". Looks good. |
||
} | ||
|
||
if (mutation.transform != null) { | ||
request.addWrites(mutation.transform); | ||
} | ||
request.addWrites(mutation.document); | ||
} | ||
|
||
if (transactionId != null) { | ||
|
@@ -632,27 +611,12 @@ public List<WriteResult> apply(CommitResponse commitResponse) { | |
|
||
List<WriteResult> result = new ArrayList<>(); | ||
|
||
Preconditions.checkState( | ||
request.getWritesCount() == writeResults.size(), | ||
"Expected one write result per operation, but got %s results for %s operations.", | ||
writeResults.size(), | ||
request.getWritesCount()); | ||
|
||
Iterator<Mutation> mutationIterator = mutations.iterator(); | ||
Iterator<com.google.firestore.v1.WriteResult> responseIterator = | ||
writeResults.iterator(); | ||
|
||
while (mutationIterator.hasNext()) { | ||
Mutation mutation = mutationIterator.next(); | ||
|
||
// Don't return both write results for a write that contains a transform, as the fact | ||
// that we have to split one write operation into two distinct write requests is an | ||
// implementation detail. | ||
if (mutation.document != null && mutation.transform != null) { | ||
// The document transform is always sent last and produces the latest update time. | ||
responseIterator.next(); | ||
} | ||
|
||
mutationIterator.next(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be simpler now if we just replace it with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried it with a for loop, intellij recommended I use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should iterator over There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After changing it to iterate through |
||
result.add( | ||
WriteResult.fromProto(responseIterator.next(), commitResponse.getCommitTime())); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,13 +19,14 @@ | |
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_OBJECT; | ||
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_PROTO; | ||
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_VALUE; | ||
import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATE_PRECONDITION; | ||
import static com.google.cloud.firestore.LocalFirestoreHelper.arrayRemove; | ||
import static com.google.cloud.firestore.LocalFirestoreHelper.arrayUnion; | ||
import static com.google.cloud.firestore.LocalFirestoreHelper.commit; | ||
import static com.google.cloud.firestore.LocalFirestoreHelper.commitResponse; | ||
import static com.google.cloud.firestore.LocalFirestoreHelper.getAllResponse; | ||
import static com.google.cloud.firestore.LocalFirestoreHelper.transform; | ||
import static com.google.cloud.firestore.LocalFirestoreHelper.update; | ||
import static com.google.cloud.firestore.LocalFirestoreHelper.writeWithTransform; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.fail; | ||
|
@@ -40,6 +41,9 @@ | |
import com.google.firestore.v1.CommitRequest; | ||
import com.google.firestore.v1.CommitResponse; | ||
import com.google.firestore.v1.ListCollectionIdsRequest; | ||
import com.google.firestore.v1.Value; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
import org.junit.Test; | ||
|
@@ -196,7 +200,10 @@ public void arrayUnionWithPojo() throws ExecutionException, InterruptedException | |
doc.update("array", FieldValue.arrayUnion(SINGLE_FIELD_OBJECT)).get(); | ||
|
||
CommitRequest expectedRequest = | ||
commit(transform(UPDATE_PRECONDITION, "array", arrayUnion(SINGLE_FIELD_VALUE))); | ||
commit( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of exposing an overload to commit?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. Got rid of |
||
writeWithTransform( | ||
update(Collections.<String, Value>emptyMap(), new ArrayList<String>()), | ||
transform("array", arrayUnion(SINGLE_FIELD_VALUE)))); | ||
CommitRequest actualRequest = commitCapture.getValue(); | ||
assertEquals(expectedRequest, actualRequest); | ||
} | ||
|
@@ -212,7 +219,10 @@ public void arrayRemoveWithPojo() throws ExecutionException, InterruptedExceptio | |
doc.update("array", FieldValue.arrayRemove(SINGLE_FIELD_OBJECT)).get(); | ||
|
||
CommitRequest expectedRequest = | ||
commit(transform(UPDATE_PRECONDITION, "array", arrayRemove(SINGLE_FIELD_VALUE))); | ||
commit( | ||
writeWithTransform( | ||
update(Collections.<String, Value>emptyMap(), new ArrayList<String>()), | ||
transform("array", arrayRemove(SINGLE_FIELD_VALUE)))); | ||
CommitRequest actualRequest = commitCapture.getValue(); | ||
assertEquals(expectedRequest, actualRequest); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can probably remove the
Mutation
type and replace it with the Proto type. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. I remember you did this for node and the code became much cleaner. Removed
Mutation
and renamed mutation related variables towrite
.