Skip to content

Commit 5d08be6

Browse files
Backport to branch(3.14) : Fix delete behavior in Cosmos adapter (#2343)
Co-authored-by: Toshihiro Suzuki <[email protected]>
1 parent 721dbca commit 5d08be6

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
@@ -1293,6 +1293,17 @@ public void delete_MultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProper
12931293
assertThat(results.size()).isEqualTo(0);
12941294
}
12951295

1296+
@Test
1297+
public void delete_ForNonExistingRecord_ShouldDoNothing() throws ExecutionException {
1298+
// Arrange
1299+
1300+
// Act Assert
1301+
assertThatCode(() -> storage.delete(prepareDeletes().get(0))).doesNotThrowAnyException();
1302+
1303+
Optional<Result> result = storage.get(prepareGet(0, 0));
1304+
assertThat(result).isNotPresent();
1305+
}
1306+
12961307
@Test
12971308
public void mutate_MultiplePutGiven_ShouldStoreProperly() throws ExecutionException, IOException {
12981309
// Arrange

0 commit comments

Comments
 (0)