-
Notifications
You must be signed in to change notification settings - Fork 67
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 4 commits
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 |
---|---|---|
|
@@ -46,19 +46,13 @@ | |
*/ | ||
public abstract class UpdateBuilder<T extends UpdateBuilder> { | ||
|
||
private static class Mutation { | ||
Write.Builder document; | ||
Write.Builder transform; | ||
com.google.firestore.v1.Precondition precondition; | ||
} | ||
|
||
final FirestoreImpl firestore; | ||
private final List<Mutation> mutations; | ||
private final List<Write.Builder> writes; | ||
private boolean committed; | ||
|
||
UpdateBuilder(FirestoreImpl firestore) { | ||
this.firestore = firestore; | ||
this.mutations = new ArrayList<>(); | ||
this.writes = new ArrayList<>(); | ||
} | ||
|
||
/** | ||
|
@@ -123,27 +117,23 @@ private T performCreate( | |
DocumentTransform.fromFieldPathMap( | ||
documentReference, convertToFieldPaths(fields, /* splitOnDots= */ false)); | ||
|
||
Mutation mutation = addMutation(); | ||
mutation.precondition = Precondition.exists(false).toPb(); | ||
|
||
if (!documentSnapshot.isEmpty() || documentTransform.isEmpty()) { | ||
mutation.document = documentSnapshot.toPb(); | ||
} | ||
verifyNotCommitted(); | ||
Write.Builder write = documentSnapshot.toPb(); | ||
write.setCurrentDocument(Precondition.exists(false).toPb()); | ||
|
||
if (!documentTransform.isEmpty()) { | ||
mutation.transform = documentTransform.toPb(); | ||
write.addAllUpdateTransforms(documentTransform.toPb()); | ||
} | ||
|
||
writes.add(write); | ||
|
||
return (T) this; | ||
} | ||
|
||
/** Adds a new mutation to the batch. */ | ||
private Mutation addMutation() { | ||
/** Adds a new write to the batch. */ | ||
private void verifyNotCommitted() { | ||
Preconditions.checkState( | ||
!committed, "Cannot modify a WriteBatch that has already been committed."); | ||
Mutation mutation = new Mutation(); | ||
mutations.add(mutation); | ||
return mutation; | ||
} | ||
|
||
/** | ||
|
@@ -248,31 +238,26 @@ 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()); | ||
verifyNotCommitted(); | ||
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. This feels out of place now. Move to top of method? 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. |
||
Write.Builder write = documentSnapshot.toPb(); | ||
if (!documentTransform.isEmpty()) { | ||
write.addAllUpdateTransforms(documentTransform.toPb()); | ||
} | ||
|
||
if (!documentTransform.isEmpty()) { | ||
mutation.transform = documentTransform.toPb(); | ||
if (options.isMerge() || options.getFieldMask() != null) { | ||
write.setUpdateMask(documentMask.toPb()); | ||
} | ||
|
||
writes.add(write); | ||
|
||
return (T) this; | ||
} | ||
|
||
|
@@ -532,17 +517,15 @@ public boolean allowTransform() { | |
fieldPaths.removeAll(documentTransform.getFields()); | ||
FieldMask fieldMask = new FieldMask(fieldPaths); | ||
|
||
Mutation mutation = addMutation(); | ||
mutation.precondition = precondition.toPb(); | ||
|
||
if (!documentSnapshot.isEmpty() || !fieldMask.isEmpty()) { | ||
mutation.document = documentSnapshot.toPb(); | ||
mutation.document.setUpdateMask(fieldMask.toPb()); | ||
} | ||
verifyNotCommitted(); | ||
Write.Builder write = documentSnapshot.toPb(); | ||
write.setCurrentDocument(precondition.toPb()); | ||
write.setUpdateMask(fieldMask.toPb()); | ||
|
||
if (!documentTransform.isEmpty()) { | ||
mutation.transform = documentTransform.toPb(); | ||
write.addAllUpdateTransforms(documentTransform.toPb()); | ||
} | ||
writes.add(write); | ||
|
||
return (T) this; | ||
} | ||
|
@@ -573,12 +556,13 @@ public T delete(@Nonnull DocumentReference documentReference) { | |
|
||
private T performDelete( | ||
@Nonnull DocumentReference documentReference, @Nonnull Precondition precondition) { | ||
Mutation mutation = addMutation(); | ||
mutation.document = Write.newBuilder().setDelete(documentReference.getName()); | ||
verifyNotCommitted(); | ||
Write.Builder write = Write.newBuilder().setDelete(documentReference.getName()); | ||
|
||
if (!precondition.isEmpty()) { | ||
mutation.precondition = precondition.toPb(); | ||
write.setCurrentDocument(precondition.toPb()); | ||
} | ||
writes.add(write); | ||
|
||
return (T) this; | ||
} | ||
|
@@ -589,28 +573,13 @@ ApiFuture<List<WriteResult>> commit(@Nullable ByteString transactionId) { | |
.getCurrentSpan() | ||
.addAnnotation( | ||
"CloudFirestore.Commit", | ||
ImmutableMap.of("numDocuments", AttributeValue.longAttributeValue(mutations.size()))); | ||
ImmutableMap.of("numDocuments", AttributeValue.longAttributeValue(writes.size()))); | ||
|
||
final CommitRequest.Builder request = CommitRequest.newBuilder(); | ||
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); | ||
} | ||
|
||
if (mutation.transform != null) { | ||
request.addWrites(mutation.transform); | ||
} | ||
for (Write.Builder write : writes) { | ||
request.addWrites(write); | ||
} | ||
|
||
if (transactionId != null) { | ||
|
@@ -632,27 +601,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<Write.Builder> writeIterator = writes.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(); | ||
} | ||
|
||
while (writeIterator.hasNext()) { | ||
writeIterator.next(); | ||
result.add( | ||
WriteResult.fromProto(responseIterator.next(), commitResponse.getCommitTime())); | ||
} | ||
|
@@ -665,11 +619,11 @@ public List<WriteResult> apply(CommitResponse commitResponse) { | |
|
||
/** Checks whether any updates have been queued. */ | ||
boolean isEmpty() { | ||
return mutations.isEmpty(); | ||
return writes.isEmpty(); | ||
} | ||
|
||
/** Get the number of mutations. */ | ||
/** Get the number of writes. */ | ||
public int getMutationsSize() { | ||
return mutations.size(); | ||
return writes.size(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,13 +19,13 @@ | |
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 org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.fail; | ||
|
@@ -40,6 +40,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 +199,9 @@ 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 |
||
update(Collections.<String, Value>emptyMap(), new ArrayList<String>()), | ||
transform("array", arrayUnion(SINGLE_FIELD_VALUE))); | ||
CommitRequest actualRequest = commitCapture.getValue(); | ||
assertEquals(expectedRequest, actualRequest); | ||
} | ||
|
@@ -212,7 +217,9 @@ 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( | ||
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.
Incorrect javadoc now that the mutation has been removed.
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.
Removed the javadoc since the method is pretty straightforward now.