Skip to content

Commit 5a073b9

Browse files
committed
Fix delete behavior in Cosmos adapter (#2341)
1 parent 69b2c11 commit 5a073b9

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

core/src/main/java/com/scalar/db/storage/cosmos/DeleteStatementHandler.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.azure.cosmos.CosmosClient;
44
import com.azure.cosmos.CosmosException;
5+
import com.azure.cosmos.implementation.NotFoundException;
56
import com.azure.cosmos.models.CosmosItemRequestOptions;
67
import com.azure.cosmos.models.PartitionKey;
78
import com.scalar.db.api.Delete;
@@ -42,7 +43,11 @@ private void execute(Mutation mutation, TableMetadata tableMetadata) throws Cosm
4243
PartitionKey partitionKey = cosmosMutation.getCosmosPartitionKey();
4344
CosmosItemRequestOptions options = new CosmosItemRequestOptions();
4445

45-
getContainer(mutation).deleteItem(id, partitionKey, options);
46+
try {
47+
getContainer(mutation).deleteItem(id, partitionKey, options);
48+
} catch (NotFoundException ignored) {
49+
// don't throw an exception if the item is not found
50+
}
4651
} else {
4752
// clustering key is not fully specified
4853
executeStoredProcedure(mutation, tableMetadata);

core/src/test/java/com/scalar/db/storage/cosmos/DeleteStatementHandlerTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.azure.cosmos.CosmosException;
1919
import com.azure.cosmos.CosmosScripts;
2020
import com.azure.cosmos.CosmosStoredProcedure;
21+
import com.azure.cosmos.implementation.NotFoundException;
2122
import com.azure.cosmos.models.CosmosItemRequestOptions;
2223
import com.azure.cosmos.models.CosmosItemResponse;
2324
import com.azure.cosmos.models.CosmosStoredProcedureRequestOptions;
@@ -121,6 +122,19 @@ public void handle_DeleteWithoutConditionsCosmosExceptionThrown_ShouldThrowExecu
121122
.hasCause(toThrow);
122123
}
123124

125+
@Test
126+
public void handle_DeleteWithoutConditionsNotFoundExceptionThrown_ShouldNotThrowAnyException() {
127+
// Arrange
128+
doThrow(NotFoundException.class)
129+
.when(container)
130+
.deleteItem(anyString(), any(PartitionKey.class), any(CosmosItemRequestOptions.class));
131+
132+
Delete delete = prepareDelete();
133+
134+
// Act Assert
135+
assertThatCode(() -> handler.handle(delete)).doesNotThrowAnyException();
136+
}
137+
124138
@Test
125139
public void handle_DeleteWithoutClusteringKeyGiven_ShouldCallStoredProcedure() {
126140
// Arrange

integration-test/src/main/java/com/scalar/db/api/DistributedStorageIntegrationTestBase.java

+11
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,17 @@ public void delete_MultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProper
14991499
assertThat(results.size()).isEqualTo(0);
15001500
}
15011501

1502+
@Test
1503+
public void delete_ForNonExistingRecord_ShouldDoNothing() throws ExecutionException {
1504+
// Arrange
1505+
1506+
// Act Assert
1507+
assertThatCode(() -> storage.delete(prepareDeletes().get(0))).doesNotThrowAnyException();
1508+
1509+
Optional<Result> result = storage.get(prepareGet(0, 0));
1510+
assertThat(result).isNotPresent();
1511+
}
1512+
15021513
@Test
15031514
public void mutate_MultiplePutGiven_ShouldStoreProperly() throws ExecutionException, IOException {
15041515
// Arrange

0 commit comments

Comments
 (0)