use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.
the class PrepareMutationComposerTest method delete_DeleteAndNullResultGiven_ShouldComposePutWithPutIfNotExistsCondition.
@Test
public void delete_DeleteAndNullResultGiven_ShouldComposePutWithPutIfNotExistsCondition() {
// Arrange
Delete delete = prepareDelete();
// Act
composer.add(delete, null);
// Assert
Put actual = (Put) mutations.get(0);
Put expected = new Put(delete.getPartitionKey(), delete.getClusteringKey().orElse(null)).forNamespace(delete.forNamespace().get()).forTable(delete.forTable().get());
expected.withConsistency(Consistency.LINEARIZABLE);
expected.withCondition(new PutIfNotExists());
expected.withValue(Attribute.toPreparedAtValue(ANY_TIME_5));
expected.withValue(Attribute.toIdValue(ANY_ID_3));
expected.withValue(Attribute.toStateValue(TransactionState.DELETED));
expected.withValue(Attribute.toVersionValue(1));
assertThat(actual).isEqualTo(expected);
}
use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.
the class PutStatementHandlerTest method handle_PutIfNotExistsGiven_ShouldCallStoredProcedure.
@Test
public void handle_PutIfNotExistsGiven_ShouldCallStoredProcedure() {
// Arrange
when(container.getScripts()).thenReturn(cosmosScripts);
when(cosmosScripts.getStoredProcedure(anyString())).thenReturn(storedProcedure);
when(storedProcedure.execute(anyList(), any(CosmosStoredProcedureRequestOptions.class))).thenReturn(spResponse);
when(spResponse.getResponseAsString()).thenReturn("true");
Put put = preparePut().withCondition(new PutIfNotExists());
CosmosMutation cosmosMutation = new CosmosMutation(put, metadata);
Record record = cosmosMutation.makeRecord();
String query = cosmosMutation.makeConditionalQuery();
// Act Assert
assertThatCode(() -> handler.handle(put)).doesNotThrowAnyException();
// Assert
verify(cosmosScripts).getStoredProcedure("mutate.js");
verify(storedProcedure).execute(captor.capture(), any(CosmosStoredProcedureRequestOptions.class));
assertThat(captor.getValue().get(0)).isEqualTo(1);
assertThat(captor.getValue().get(1)).isEqualTo(CosmosMutation.MutationType.PUT_IF_NOT_EXISTS.ordinal());
assertThat(captor.getValue().get(2)).isEqualTo(record);
assertThat(captor.getValue().get(3)).isEqualTo(query);
}
use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.
the class BatchHandlerTest method handle_TransactionCanceledExceptionWithConditionCheckFailed_ShouldThrowNoMutationException.
@Test
public void handle_TransactionCanceledExceptionWithConditionCheckFailed_ShouldThrowNoMutationException() {
TransactionCanceledException toThrow = TransactionCanceledException.builder().cancellationReasons(CancellationReason.builder().code("ConditionalCheckFailed").build()).build();
doThrow(toThrow).when(client).transactWriteItems(any(TransactWriteItemsRequest.class));
Put put = preparePut().withCondition(new PutIfNotExists());
Delete delete = prepareDelete().withCondition(new DeleteIfExists());
// Act Assert
assertThatThrownBy(() -> handler.handle(Arrays.asList(put, delete))).isInstanceOf(NoMutationException.class);
}
use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.
the class BatchHandlerTest method handle_TransactionCanceledExceptionWithTransactionConflict_ShouldThrowRetriableExecutionException.
@Test
public void handle_TransactionCanceledExceptionWithTransactionConflict_ShouldThrowRetriableExecutionException() {
TransactionCanceledException toThrow = TransactionCanceledException.builder().cancellationReasons(CancellationReason.builder().code("TransactionConflict").build(), CancellationReason.builder().code("None").build()).build();
doThrow(toThrow).when(client).transactWriteItems(any(TransactWriteItemsRequest.class));
Put put = preparePut().withCondition(new PutIfNotExists());
Delete delete = prepareDelete().withCondition(new DeleteIfExists());
// Act Assert
assertThatThrownBy(() -> handler.handle(Arrays.asList(put, delete))).isInstanceOf(RetriableExecutionException.class);
}
use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.
the class CosmosMutation method getMutationType.
@Nonnull
public MutationType getMutationType() {
Mutation mutation = (Mutation) getOperation();
if (!mutation.getCondition().isPresent()) {
if (mutation instanceof Put) {
return MutationType.PUT;
} else {
return MutationType.DELETE_IF;
}
}
MutationCondition condition = mutation.getCondition().get();
if (condition instanceof PutIfNotExists) {
return MutationType.PUT_IF_NOT_EXISTS;
} else if (condition instanceof PutIfExists || condition instanceof PutIf) {
return MutationType.PUT_IF;
} else {
return MutationType.DELETE_IF;
}
}
Aggregations