Search in sources :

Example 21 with PutIf

use of com.scalar.db.api.PutIf in project scalardb by scalar-labs.

the class RollbackMutationComposerTest method add_PutAndResultFromSnapshotGivenAndDeletedResultGivenFromStorage_ShouldComposePut.

@Test
public void add_PutAndResultFromSnapshotGivenAndDeletedResultGivenFromStorage_ShouldComposePut() throws ExecutionException {
    // Arrange
    composer = new RollbackMutationComposer(ANY_ID_2, storage, tableMetadataManager, mutations);
    TransactionResult resultInSnapshot = prepareInitialResult(ANY_ID_1, TransactionState.COMMITTED);
    TransactionResult result = prepareResult(TransactionState.DELETED);
    when(storage.get(any(Get.class))).thenReturn(Optional.of(result));
    Put put = preparePut();
    // Act
    composer.add(put, resultInSnapshot);
    // Assert
    Put actual = (Put) mutations.get(0);
    Put expected = new Put(put.getPartitionKey(), put.getClusteringKey().orElse(null)).forNamespace(put.forNamespace().get()).forTable(put.forTable().get());
    expected.withConsistency(Consistency.LINEARIZABLE);
    expected.withCondition(new PutIf(new ConditionalExpression(ID, toIdValue(ANY_ID_2), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.DELETED), Operator.EQ)));
    expected.withValues(extractAfterValues(resultInSnapshot));
    assertThat(actual).isEqualTo(expected);
    verify(storage).get(any(Get.class));
}
Also used : PutIf(com.scalar.db.api.PutIf) Get(com.scalar.db.api.Get) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 22 with PutIf

use of com.scalar.db.api.PutIf in project scalardb by scalar-labs.

the class CommitMutationComposerTest method add_SelectionAndPreparedResultGiven_ShouldComposePutForRollforward.

@Test
public void add_SelectionAndPreparedResultGiven_ShouldComposePutForRollforward() {
    // Arrange
    Get get = prepareGet();
    TransactionResult result = prepareResult(TransactionState.PREPARED);
    // Act
    composer.add(get, result);
    // Assert
    Put actual = (Put) mutations.get(0);
    Put expected = new Put(get.getPartitionKey(), get.getClusteringKey().orElse(null)).forNamespace(get.forNamespace().get()).forTable(get.forTable().get());
    expected.withConsistency(Consistency.LINEARIZABLE);
    expected.withCondition(new PutIf(new ConditionalExpression(ID, toIdValue(ANY_ID), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.PREPARED), Operator.EQ)));
    expected.withValue(Attribute.toCommittedAtValue(ANY_TIME_2));
    expected.withValue(Attribute.toStateValue(TransactionState.COMMITTED));
    assertThat(actual).isEqualTo(expected);
}
Also used : PutIf(com.scalar.db.api.PutIf) Get(com.scalar.db.api.Get) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 23 with PutIf

use of com.scalar.db.api.PutIf in project scalardb by scalar-labs.

the class PrepareMutationComposerTest method add_PutAndResultGiven_ShouldComposePutWithPutIfCondition.

@Test
public void add_PutAndResultGiven_ShouldComposePutWithPutIfCondition() {
    // Arrange
    Put put = preparePut();
    TransactionResult result = prepareResult();
    // Act
    composer.add(put, result);
    // Assert
    Put actual = (Put) mutations.get(0);
    put.withConsistency(Consistency.LINEARIZABLE);
    put.withCondition(new PutIf(new ConditionalExpression(VERSION, toVersionValue(2), Operator.EQ), new ConditionalExpression(ID, toIdValue(ANY_ID_2), Operator.EQ)));
    put.withValue(Attribute.toPreparedAtValue(ANY_TIME_5));
    put.withValue(Attribute.toIdValue(ANY_ID_3));
    put.withValue(Attribute.toStateValue(TransactionState.PREPARED));
    put.withValue(Attribute.toVersionValue(3));
    put.withValue(Attribute.toBeforePreparedAtValue(ANY_TIME_3));
    put.withValue(Attribute.toBeforeCommittedAtValue(ANY_TIME_4));
    put.withValue(Attribute.toBeforeIdValue(ANY_ID_2));
    put.withValue(Attribute.toBeforeStateValue(TransactionState.COMMITTED));
    put.withValue(Attribute.toBeforeVersionValue(2));
    put.withValue(Attribute.BEFORE_PREFIX + ANY_NAME_3, ANY_INT_2);
    assertThat(actual).isEqualTo(put);
}
Also used : PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 24 with PutIf

use of com.scalar.db.api.PutIf in project scalardb by scalar-labs.

the class ConditionExpressionBuilderTest method build_PutIfAcceptCalled_ShouldReturnCondition.

@Test
public void build_PutIfAcceptCalled_ShouldReturnCondition() {
    // Arrange
    PutIf condition = ConditionBuilder.putIf(ConditionBuilder.column(ANY_NAME_1).isEqualToInt(ANY_INT_VALUE.getAsInt())).and(ConditionBuilder.column(ANY_NAME_2).isGreaterThanInt(ANY_INT_VALUE.getAsInt())).and(ConditionBuilder.column(ANY_NAME_3).isNullInt()).and(ConditionBuilder.column(ANY_NAME_4).isNotNullInt()).build();
    ConditionExpressionBuilder builder = new ConditionExpressionBuilder(DynamoOperation.CONDITION_COLUMN_NAME_ALIAS, DynamoOperation.CONDITION_VALUE_ALIAS);
    // Act
    condition.accept(builder);
    String actual = builder.build();
    // Assert
    assertThat(actual).isEqualTo("#ccol0 = :cval0 AND #ccol1 > :cval1 " + "AND (attribute_not_exists(#ccol2) OR #ccol2 = :cval2) " + "AND (attribute_exists(#ccol3) AND NOT #ccol3 = :cval3)");
}
Also used : PutIf(com.scalar.db.api.PutIf) Test(org.junit.jupiter.api.Test)

Example 25 with PutIf

use of com.scalar.db.api.PutIf in project scalardb by scalar-labs.

the class DynamoMutationTest method getConditionColumnMap_PutGiven_ShouldReturnCondition.

@Test
public void getConditionColumnMap_PutGiven_ShouldReturnCondition() {
    // Arrange
    PutIf conditions = new PutIf(new ConditionalExpression(ANY_NAME_3, ANY_INT_VALUE, Operator.EQ), new ConditionalExpression(ANY_NAME_4, ANY_INT_VALUE, Operator.GT));
    Put put = preparePut().withCondition(conditions);
    Map<String, String> expected = new HashMap<>();
    expected.put(DynamoOperation.CONDITION_COLUMN_NAME_ALIAS + "0", ANY_NAME_3);
    expected.put(DynamoOperation.CONDITION_COLUMN_NAME_ALIAS + "1", ANY_NAME_4);
    DynamoMutation dynamoMutation = new DynamoMutation(put, metadata);
    // Act
    Map<String, String> conditionColumnMap = dynamoMutation.getConditionColumnMap();
    // Assert
    assertThat(conditionColumnMap).isEqualTo(expected);
}
Also used : PutIf(com.scalar.db.api.PutIf) HashMap(java.util.HashMap) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Aggregations

PutIf (com.scalar.db.api.PutIf)34 ConditionalExpression (com.scalar.db.api.ConditionalExpression)30 Put (com.scalar.db.api.Put)26 Test (org.junit.jupiter.api.Test)25 Get (com.scalar.db.api.Get)8 IntValue (com.scalar.db.io.IntValue)8 TextValue (com.scalar.db.io.TextValue)7 Key (com.scalar.db.io.Key)6 PutIfNotExists (com.scalar.db.api.PutIfNotExists)5 Result (com.scalar.db.api.Result)5 Test (org.junit.Test)5 Scan (com.scalar.db.api.Scan)4 MutationCondition (com.scalar.db.api.MutationCondition)3 Value (com.scalar.db.io.Value)3 HashMap (java.util.HashMap)3 Delete (com.scalar.db.api.Delete)2 Attribute.toIdValue (com.scalar.db.transaction.consensuscommit.Attribute.toIdValue)2 Mutation (com.scalar.db.api.Mutation)1 PutIfExists (com.scalar.db.api.PutIfExists)1 BooleanValue (com.scalar.db.io.BooleanValue)1