Search in sources :

Example 36 with Put

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

the class JdbcTransactionIntegrationTest method preparePut.

private Put preparePut(int id, int type, String namespace, String table) {
    Key partitionKey = new Key(ACCOUNT_ID, id);
    Key clusteringKey = new Key(ACCOUNT_TYPE, type);
    return new Put(partitionKey, clusteringKey).forNamespace(namespace).forTable(table).withConsistency(Consistency.LINEARIZABLE);
}
Also used : Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put)

Example 37 with Put

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

the class ConsensusCommitIntegrationTestBase method preparePut.

private Put preparePut(int id, int type, String namespace, String table) {
    Key partitionKey = new Key(ACCOUNT_ID, id);
    Key clusteringKey = new Key(ACCOUNT_TYPE, type);
    return new Put(partitionKey, clusteringKey).forNamespace(namespace).forTable(table).withConsistency(Consistency.LINEARIZABLE);
}
Also used : Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put)

Example 38 with Put

use of com.scalar.db.api.Put 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 39 with Put

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

the class ConsensusCommitIntegrationTestBase method populateRecords.

private void populateRecords(String namespace, String table) throws CommitException, UnknownTransactionStatusException {
    ConsensusCommit transaction = manager.start();
    IntStream.range(0, NUM_ACCOUNTS).forEach(i -> IntStream.range(0, NUM_TYPES).forEach(j -> {
        Key partitionKey = new Key(ACCOUNT_ID, i);
        Key clusteringKey = new Key(ACCOUNT_TYPE, j);
        Put put = new Put(partitionKey, clusteringKey).forNamespace(namespace).forTable(table).withValue(BALANCE, INITIAL_BALANCE);
        transaction.put(put);
    }));
    transaction.commit();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) DatabaseConfig(com.scalar.db.config.DatabaseConfig) IntValue(com.scalar.db.io.IntValue) Arrays(java.util.Arrays) Consistency(com.scalar.db.api.Consistency) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ExecutionException(com.scalar.db.exception.storage.ExecutionException) Value(com.scalar.db.io.Value) StorageFactory(com.scalar.db.service.StorageFactory) Mockito.spy(org.mockito.Mockito.spy) DistributedStorageAdmin(com.scalar.db.api.DistributedStorageAdmin) TableMetadata(com.scalar.db.api.TableMetadata) CrudException(com.scalar.db.exception.transaction.CrudException) ArrayList(java.util.ArrayList) Result(com.scalar.db.api.Result) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) TestUtils(com.scalar.db.storage.TestUtils) Map(java.util.Map) Assertions(org.assertj.core.api.Assertions) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) CommitException(com.scalar.db.exception.transaction.CommitException) DataType(com.scalar.db.io.DataType) CommitConflictException(com.scalar.db.exception.transaction.CommitConflictException) Before(org.junit.Before) Key(com.scalar.db.io.Key) AfterClass(org.junit.AfterClass) NoMutationException(com.scalar.db.exception.storage.NoMutationException) TransactionState(com.scalar.db.api.TransactionState) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Put(com.scalar.db.api.Put) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) Mockito.verify(org.mockito.Mockito.verify) DistributedStorage(com.scalar.db.api.DistributedStorage) Mockito.never(org.mockito.Mockito.never) List(java.util.List) Selection(com.scalar.db.api.Selection) Scan(com.scalar.db.api.Scan) UnknownTransactionStatusException(com.scalar.db.exception.transaction.UnknownTransactionStatusException) Optional(java.util.Optional) Collections(java.util.Collections) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put)

Example 40 with Put

use of com.scalar.db.api.Put 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)

Aggregations

Put (com.scalar.db.api.Put)374 Key (com.scalar.db.io.Key)216 Test (org.junit.jupiter.api.Test)209 Result (com.scalar.db.api.Result)108 Get (com.scalar.db.api.Get)93 Test (org.junit.Test)67 Delete (com.scalar.db.api.Delete)64 IntValue (com.scalar.db.io.IntValue)63 TextValue (com.scalar.db.io.TextValue)48 Scan (com.scalar.db.api.Scan)44 Value (com.scalar.db.io.Value)37 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)35 BooleanValue (com.scalar.db.io.BooleanValue)33 ConditionalExpression (com.scalar.db.api.ConditionalExpression)30 PutIfNotExists (com.scalar.db.api.PutIfNotExists)29 PutIf (com.scalar.db.api.PutIf)28 DoubleValue (com.scalar.db.io.DoubleValue)26 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)19 ExecutionException (com.scalar.db.exception.storage.ExecutionException)17 Mutation (com.scalar.db.api.Mutation)16