Search in sources :

Example 16 with PutIfNotExists

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);
}
Also used : Delete(com.scalar.db.api.Delete) PutIfNotExists(com.scalar.db.api.PutIfNotExists) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 17 with PutIfNotExists

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);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 18 with PutIfNotExists

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);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Delete(com.scalar.db.api.Delete) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 19 with PutIfNotExists

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);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Delete(com.scalar.db.api.Delete) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 20 with PutIfNotExists

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;
    }
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) PutIf(com.scalar.db.api.PutIf) MutationCondition(com.scalar.db.api.MutationCondition) Mutation(com.scalar.db.api.Mutation) Put(com.scalar.db.api.Put) PutIfExists(com.scalar.db.api.PutIfExists) Nonnull(javax.annotation.Nonnull)

Aggregations

PutIfNotExists (com.scalar.db.api.PutIfNotExists)37 Put (com.scalar.db.api.Put)27 Test (org.junit.jupiter.api.Test)26 Key (com.scalar.db.io.Key)15 Delete (com.scalar.db.api.Delete)8 Test (org.junit.Test)7 DeleteIfExists (com.scalar.db.api.DeleteIfExists)6 MutationCondition (com.scalar.db.api.MutationCondition)6 Result (com.scalar.db.api.Result)6 TextValue (com.scalar.db.io.TextValue)6 PutIf (com.scalar.db.api.PutIf)5 Scan (com.scalar.db.api.Scan)5 IntValue (com.scalar.db.io.IntValue)5 Value (com.scalar.db.io.Value)5 ConditionalExpression (com.scalar.db.api.ConditionalExpression)4 BooleanValue (com.scalar.db.io.BooleanValue)4 CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)3 DoubleValue (com.scalar.db.io.DoubleValue)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 TransactWriteItemsRequest (software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest)3