Skip to content

Commit c353581

Browse files
Backport to branch(3.11) : Fix delete behavior in Cosmos adapter (#2346)
Co-authored-by: Toshihiro Suzuki <[email protected]>
1 parent fae31ed commit c353581

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
@@ -1178,6 +1178,17 @@ public void delete_MultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProper
11781178
assertThat(results.size()).isEqualTo(0);
11791179
}
11801180

1181+
@Test
1182+
public void delete_ForNonExistingRecord_ShouldDoNothing() throws ExecutionException {
1183+
// Arrange
1184+
1185+
// Act Assert
1186+
assertThatCode(() -> storage.delete(prepareDeletes().get(0))).doesNotThrowAnyException();
1187+
1188+
Optional<Result> result = storage.get(prepareGet(0, 0));
1189+
assertThat(result).isNotPresent();
1190+
}
1191+
11811192
@Test
11821193
public void mutate_MultiplePutGiven_ShouldStoreProperly() throws ExecutionException, IOException {
11831194
// Arrange

0 commit comments

Comments
 (0)