diff --git a/core/src/main/java/com/scalar/db/storage/cosmos/DeleteStatementHandler.java b/core/src/main/java/com/scalar/db/storage/cosmos/DeleteStatementHandler.java index 302686ff5..3aa11b876 100644 --- a/core/src/main/java/com/scalar/db/storage/cosmos/DeleteStatementHandler.java +++ b/core/src/main/java/com/scalar/db/storage/cosmos/DeleteStatementHandler.java @@ -2,6 +2,7 @@ import com.azure.cosmos.CosmosClient; import com.azure.cosmos.CosmosException; +import com.azure.cosmos.implementation.NotFoundException; import com.azure.cosmos.models.CosmosItemRequestOptions; import com.azure.cosmos.models.PartitionKey; import com.scalar.db.api.Delete; @@ -42,7 +43,11 @@ private void execute(Mutation mutation, TableMetadata tableMetadata) throws Cosm PartitionKey partitionKey = cosmosMutation.getCosmosPartitionKey(); CosmosItemRequestOptions options = new CosmosItemRequestOptions(); - getContainer(mutation).deleteItem(id, partitionKey, options); + try { + getContainer(mutation).deleteItem(id, partitionKey, options); + } catch (NotFoundException ignored) { + // don't throw an exception if the item is not found + } } else { // clustering key is not fully specified executeStoredProcedure(mutation, tableMetadata); diff --git a/core/src/test/java/com/scalar/db/storage/cosmos/DeleteStatementHandlerTest.java b/core/src/test/java/com/scalar/db/storage/cosmos/DeleteStatementHandlerTest.java index fd8284f83..6c6651865 100644 --- a/core/src/test/java/com/scalar/db/storage/cosmos/DeleteStatementHandlerTest.java +++ b/core/src/test/java/com/scalar/db/storage/cosmos/DeleteStatementHandlerTest.java @@ -18,6 +18,7 @@ import com.azure.cosmos.CosmosException; import com.azure.cosmos.CosmosScripts; import com.azure.cosmos.CosmosStoredProcedure; +import com.azure.cosmos.implementation.NotFoundException; import com.azure.cosmos.models.CosmosItemRequestOptions; import com.azure.cosmos.models.CosmosItemResponse; import com.azure.cosmos.models.CosmosStoredProcedureRequestOptions; @@ -121,6 +122,19 @@ public void handle_DeleteWithoutConditionsCosmosExceptionThrown_ShouldThrowExecu .hasCause(toThrow); } + @Test + public void handle_DeleteWithoutConditionsNotFoundExceptionThrown_ShouldNotThrowAnyException() { + // Arrange + doThrow(NotFoundException.class) + .when(container) + .deleteItem(anyString(), any(PartitionKey.class), any(CosmosItemRequestOptions.class)); + + Delete delete = prepareDelete(); + + // Act Assert + assertThatCode(() -> handler.handle(delete)).doesNotThrowAnyException(); + } + @Test public void handle_DeleteWithoutClusteringKeyGiven_ShouldCallStoredProcedure() { // Arrange diff --git a/integration-test/src/main/java/com/scalar/db/api/DistributedStorageIntegrationTestBase.java b/integration-test/src/main/java/com/scalar/db/api/DistributedStorageIntegrationTestBase.java index e947eb736..40c30fa45 100644 --- a/integration-test/src/main/java/com/scalar/db/api/DistributedStorageIntegrationTestBase.java +++ b/integration-test/src/main/java/com/scalar/db/api/DistributedStorageIntegrationTestBase.java @@ -1293,6 +1293,17 @@ public void delete_MultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProper assertThat(results.size()).isEqualTo(0); } + @Test + public void delete_ForNonExistingRecord_ShouldDoNothing() throws ExecutionException { + // Arrange + + // Act Assert + assertThatCode(() -> storage.delete(prepareDeletes().get(0))).doesNotThrowAnyException(); + + Optional result = storage.get(prepareGet(0, 0)); + assertThat(result).isNotPresent(); + } + @Test public void mutate_MultiplePutGiven_ShouldStoreProperly() throws ExecutionException, IOException { // Arrange