Search in sources :

Example 16 with IntValue

use of com.scalar.db.io.IntValue in project scalardb by scalar-labs.

the class ConsensusCommitIntegrationTestBase method putAndCommit_SinglePartitionMutationsGiven_ShouldAccessStorageOnceForPrepareAndCommit.

@Test
public void putAndCommit_SinglePartitionMutationsGiven_ShouldAccessStorageOnceForPrepareAndCommit() throws CommitException, UnknownTransactionStatusException, ExecutionException, CoordinatorException {
    // Arrange
    IntValue balance = new IntValue(BALANCE, INITIAL_BALANCE);
    List<Put> puts = preparePuts(namespace1, TABLE_1);
    puts.get(0).withValue(balance);
    puts.get(1).withValue(balance);
    ConsensusCommit transaction = manager.start();
    // Act
    transaction.put(puts.get(0));
    transaction.put(puts.get(1));
    transaction.commit();
    // Assert
    // one for prepare, one for commit
    verify(storage, times(2)).mutate(anyList());
    verify(coordinator).putState(any(Coordinator.State.class));
}
Also used : TransactionState(com.scalar.db.api.TransactionState) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Test(org.junit.Test)

Example 17 with IntValue

use of com.scalar.db.io.IntValue in project scalardb by scalar-labs.

the class ConsensusCommitIntegrationTestBase method prepareTransfer.

private ConsensusCommit prepareTransfer(int fromId, String fromNamespace, String fromTable, int toId, String toNamespace, String toTable, int amount) throws CrudException {
    boolean differentTables = toNamespace.equals(fromNamespace) || !toTable.equals(fromTable);
    ConsensusCommit transaction = manager.start();
    List<Get> fromGets = prepareGets(fromNamespace, fromTable);
    List<Get> toGets = differentTables ? prepareGets(toNamespace, toTable) : fromGets;
    Optional<Result> fromResult = transaction.get(fromGets.get(fromId));
    assertThat(fromResult).isPresent();
    IntValue fromBalance = new IntValue(BALANCE, getBalance(fromResult.get()) - amount);
    Optional<Result> toResult = transaction.get(toGets.get(toId));
    assertThat(toResult).isPresent();
    IntValue toBalance = new IntValue(BALANCE, getBalance(toResult.get()) + amount);
    List<Put> fromPuts = preparePuts(fromNamespace, fromTable);
    List<Put> toPuts = differentTables ? preparePuts(toNamespace, toTable) : fromPuts;
    fromPuts.get(fromId).withValue(fromBalance);
    toPuts.get(toId).withValue(toBalance);
    transaction.put(fromPuts.get(fromId));
    transaction.put(toPuts.get(toId));
    return transaction;
}
Also used : Get(com.scalar.db.api.Get) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result)

Example 18 with IntValue

use of com.scalar.db.io.IntValue in project scalardb by scalar-labs.

the class ConsensusCommitIntegrationTestBase method putAndCommit_TwoPartitionsMutationsGiven_ShouldAccessStorageTwiceForPrepareAndCommit.

@Test
public void putAndCommit_TwoPartitionsMutationsGiven_ShouldAccessStorageTwiceForPrepareAndCommit() throws CommitException, UnknownTransactionStatusException, ExecutionException, CoordinatorException {
    // Arrange
    IntValue balance = new IntValue(BALANCE, INITIAL_BALANCE);
    List<Put> puts = preparePuts(namespace1, TABLE_1);
    puts.get(0).withValue(balance);
    // next account
    puts.get(NUM_TYPES).withValue(balance);
    ConsensusCommit transaction = manager.start();
    // Act
    transaction.put(puts.get(0));
    transaction.put(puts.get(NUM_TYPES));
    transaction.commit();
    // Assert
    // twice for prepare, twice for commit
    verify(storage, times(4)).mutate(anyList());
    verify(coordinator).putState(any(Coordinator.State.class));
}
Also used : TransactionState(com.scalar.db.api.TransactionState) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Test(org.junit.Test)

Example 19 with IntValue

use of com.scalar.db.io.IntValue in project scalardb by scalar-labs.

the class JdbcTransactionIntegrationTest method prepareTransfer.

private JdbcTransaction prepareTransfer(int fromId, int toId, int amount) throws TransactionException {
    JdbcTransaction transaction = manager.start();
    List<Get> gets = prepareGets(NAMESPACE, TABLE);
    Optional<Result> fromResult = transaction.get(gets.get(fromId));
    assertThat(fromResult.isPresent()).isTrue();
    IntValue fromBalance = new IntValue(BALANCE, getBalance(fromResult.get()) - amount);
    Optional<Result> toResult = transaction.get(gets.get(toId));
    assertThat(toResult.isPresent()).isTrue();
    IntValue toBalance = new IntValue(BALANCE, getBalance(toResult.get()) + amount);
    List<Put> puts = preparePuts(NAMESPACE, TABLE);
    puts.get(fromId).withValue(fromBalance);
    puts.get(toId).withValue(toBalance);
    transaction.put(puts.get(fromId));
    transaction.put(puts.get(toId));
    return transaction;
}
Also used : Get(com.scalar.db.api.Get) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result)

Example 20 with IntValue

use of com.scalar.db.io.IntValue in project scalardb by scalar-labs.

the class ValueBinderTest method visit_AcceptCalledMultipleTimesWithNullValue_ShouldSkipNull.

@Test
public void visit_AcceptCalledMultipleTimesWithNullValue_ShouldSkipNull() {
    // Arrange
    IntValue value1 = new IntValue(ANY_NAME, ANY_INT);
    BlobValue value2 = new BlobValue(ANY_NAME, (byte[]) null);
    TextValue value3 = new TextValue(ANY_NAME, (String) null);
    IntValue value4 = new IntValue(ANY_NAME, ANY_INT);
    ValueBinder binder = new ValueBinder(bound);
    // Act
    value1.accept(binder);
    value2.accept(binder);
    value3.accept(binder);
    value4.accept(binder);
    // Assert
    verify(bound).setInt(0, ANY_INT);
    verify(bound, never()).setBytes(anyInt(), any(ByteBuffer.class));
    verify(bound, never()).setString(anyInt(), anyString());
    verify(bound).setInt(3, ANY_INT);
}
Also used : TextValue(com.scalar.db.io.TextValue) IntValue(com.scalar.db.io.IntValue) BigIntValue(com.scalar.db.io.BigIntValue) ByteBuffer(java.nio.ByteBuffer) BlobValue(com.scalar.db.io.BlobValue) Test(org.junit.Test)

Aggregations

IntValue (com.scalar.db.io.IntValue)108 Test (org.junit.jupiter.api.Test)65 TextValue (com.scalar.db.io.TextValue)63 Key (com.scalar.db.io.Key)62 Put (com.scalar.db.api.Put)55 BooleanValue (com.scalar.db.io.BooleanValue)48 DoubleValue (com.scalar.db.io.DoubleValue)38 Result (com.scalar.db.api.Result)35 Test (org.junit.Test)33 Get (com.scalar.db.api.Get)29 Value (com.scalar.db.io.Value)26 BigIntValue (com.scalar.db.io.BigIntValue)23 BlobValue (com.scalar.db.io.BlobValue)20 FloatValue (com.scalar.db.io.FloatValue)19 ExpectedResult (com.scalar.db.util.TestUtils.ExpectedResult)15 ConditionalExpression (com.scalar.db.api.ConditionalExpression)13 MutationCondition (com.scalar.db.api.MutationCondition)12 PutIf (com.scalar.db.api.PutIf)8 TransactionState (com.scalar.db.api.TransactionState)8 DeleteIf (com.scalar.db.api.DeleteIf)6