Search in sources :

Example 11 with PutIf

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

the class PrepareMutationComposerTest method delete_DeleteAndResultGiven_ShouldComposePutWithPutIfCondition.

@Test
public void delete_DeleteAndResultGiven_ShouldComposePutWithPutIfCondition() {
    // Arrange
    Delete delete = prepareDelete();
    TransactionResult result = prepareResult();
    // Act
    composer.add(delete, result);
    // 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 PutIf(new ConditionalExpression(VERSION, toVersionValue(2), Operator.EQ), new ConditionalExpression(ID, toIdValue(ANY_ID_2), Operator.EQ)));
    expected.withValue(Attribute.toPreparedAtValue(ANY_TIME_5));
    expected.withValue(Attribute.toIdValue(ANY_ID_3));
    expected.withValue(Attribute.toStateValue(TransactionState.DELETED));
    expected.withValue(Attribute.toVersionValue(3));
    expected.withValue(Attribute.toBeforePreparedAtValue(ANY_TIME_3));
    expected.withValue(Attribute.toBeforeCommittedAtValue(ANY_TIME_4));
    expected.withValue(Attribute.toBeforeIdValue(ANY_ID_2));
    expected.withValue(Attribute.toBeforeStateValue(TransactionState.COMMITTED));
    expected.withValue(Attribute.toBeforeVersionValue(2));
    expected.withValue(Attribute.BEFORE_PREFIX + ANY_NAME_3, ANY_INT_2);
    assertThat(actual).isEqualTo(expected);
}
Also used : Delete(com.scalar.db.api.Delete) 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 12 with PutIf

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

the class RollbackMutationComposerTest method add_GetAndDeletedResultByThisGiven_ShouldComposePut.

@Test
public void add_GetAndDeletedResultByThisGiven_ShouldComposePut() throws ExecutionException {
    // Arrange
    composer = new RollbackMutationComposer(ANY_ID_2, storage, tableMetadataManager, mutations);
    TransactionResult result = prepareResult(TransactionState.DELETED);
    Get get = prepareGet();
    // 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_2), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.DELETED), Operator.EQ)));
    expected.withValues(extractAfterValues(prepareInitialResult(ANY_ID_1, 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 13 with PutIf

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

the class RollbackMutationComposerTest method add_GetAndPreparedResultByThisGiven_ShouldComposePut.

@Test
public void add_GetAndPreparedResultByThisGiven_ShouldComposePut() throws ExecutionException {
    // Arrange
    composer = new RollbackMutationComposer(ANY_ID_2, storage, tableMetadataManager, mutations);
    TransactionResult result = prepareResult(TransactionState.PREPARED);
    when(storage.get(any(Get.class))).thenReturn(Optional.of(result));
    Get get = prepareGet();
    // 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_2), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.PREPARED), Operator.EQ)));
    expected.withValues(extractAfterValues(prepareInitialResult(ANY_ID_1, 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 14 with PutIf

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

the class RollbackMutationComposerTest method add_ScanAndDeletedResultByThisGiven_ShouldComposePut.

@Test
public void add_ScanAndDeletedResultByThisGiven_ShouldComposePut() throws ExecutionException {
    // Arrange
    composer = new RollbackMutationComposer(ANY_ID_2, storage, tableMetadataManager, mutations);
    TransactionResult result = prepareResult(TransactionState.DELETED);
    Scan scan = prepareScan();
    // Act
    composer.add(scan, result);
    // Assert
    Put actual = (Put) mutations.get(0);
    Put expected = new Put(scan.getPartitionKey(), result.getClusteringKey().orElse(null)).forNamespace(scan.forNamespace().get()).forTable(scan.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(prepareInitialResult(ANY_ID_1, TransactionState.COMMITTED)));
    assertThat(actual).isEqualTo(expected);
}
Also used : PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Scan(com.scalar.db.api.Scan) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 15 with PutIf

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

the class RollbackMutationComposerTest method add_ScanAndPreparedResultByThisGiven_ShouldComposePut.

@Test
public void add_ScanAndPreparedResultByThisGiven_ShouldComposePut() throws ExecutionException {
    // Arrange
    composer = new RollbackMutationComposer(ANY_ID_2, storage, tableMetadataManager, mutations);
    TransactionResult result = prepareResult(TransactionState.PREPARED);
    Scan scan = prepareScan();
    // Act
    composer.add(scan, result);
    // Assert
    Put actual = (Put) mutations.get(0);
    Put expected = new Put(scan.getPartitionKey(), result.getClusteringKey().orElse(null)).forNamespace(scan.forNamespace().get()).forTable(scan.forTable().get());
    expected.withConsistency(Consistency.LINEARIZABLE);
    expected.withCondition(new PutIf(new ConditionalExpression(ID, toIdValue(ANY_ID_2), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.PREPARED), Operator.EQ)));
    expected.withValues(extractAfterValues(prepareInitialResult(ANY_ID_1, TransactionState.COMMITTED)));
    assertThat(actual).isEqualTo(expected);
}
Also used : PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Scan(com.scalar.db.api.Scan) 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