From cf6ccc7a462d0f7eb7c5ef15c36300f8bb63c9df Mon Sep 17 00:00:00 2001 From: Brian Chen Date: Wed, 10 Jun 2020 17:17:30 -0700 Subject: [PATCH] fix: update delete to return sentinel --- .../google/cloud/firestore/BulkWriter.java | 7 ++++--- .../google/cloud/firestore/UpdateBuilder.java | 21 +++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/BulkWriter.java b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/BulkWriter.java index a42302f21..62bda8fd3 100644 --- a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/BulkWriter.java +++ b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/BulkWriter.java @@ -148,8 +148,8 @@ public ApiFuture delete(@Nonnull DocumentReference documentReferenc * * @param documentReference The DocumentReference to delete. * @param precondition Precondition to enforce for this delete. - * @return An ApiFuture containing the result of the delete. Contains an error if the delete - * fails. + * @return An ApiFuture containing a sentinel value (Timestamp(0)) for the delete operation. + * Contains an error if the delete fails. */ @Nonnull public ApiFuture delete( @@ -167,7 +167,8 @@ public ApiFuture delete( * * @param documentReference A reference to the document to be set. * @param fields A map of the fields and values for the document. - * @return An ApiFuture containing the result of the write. Contains an error if the write fails. + * @return An ApiFuture containing a sentinel value (Timestamp(0)) for the delete operation. + * Contains an error if the delete fails. */ @Nonnull public ApiFuture set( diff --git a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/UpdateBuilder.java b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/UpdateBuilder.java index fa98c8951..08a53aa3c 100644 --- a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/UpdateBuilder.java +++ b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/UpdateBuilder.java @@ -697,12 +697,21 @@ public List apply(BatchWriteResponse batchWriteResponse) { for (int i = 0; i < writeResults.size(); ++i) { com.google.firestore.v1.WriteResult writeResult = writeResults.get(i); com.google.rpc.Status status = statuses.get(i); - result.add( - new BatchWriteResult( - writeResult.hasUpdateTime() - ? Timestamp.fromProto(writeResult.getUpdateTime()) - : null, - Status.fromCodeValue(status.getCode()))); + Timestamp updateTime = null; + + // Since delete operations currently do not have write times, use a + // sentinel Timestamp value. + // TODO(b/158502664): Use actual delete timestamp. + boolean isSuccessfulDelete = + !writeResult.hasUpdateTime() + && Status.fromCodeValue(status.getCode()) == Status.OK; + Timestamp DELETE_TIMESTAMP_SENTINEL = Timestamp.ofTimeSecondsAndNanos(0, 0); + if (isSuccessfulDelete) { + updateTime = DELETE_TIMESTAMP_SENTINEL; + } else if (writeResult.hasUpdateTime()) { + updateTime = Timestamp.fromProto(writeResult.getUpdateTime()); + } + result.add(new BatchWriteResult(updateTime, Status.fromCodeValue(status.getCode()))); } return result;