Search in sources :

Example 36 with GrpcTransaction

use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method prepareTransfer.

private GrpcTransaction prepareTransfer(int fromId, String fromTable, int toId, String toTable, int amount) throws TransactionException {
    boolean differentTables = !toTable.equals(fromTable);
    GrpcTransaction transaction = manager.start();
    List<Get> fromGets = prepareGets(fromTable);
    List<Get> toGets = differentTables ? prepareGets(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(fromTable);
    List<Put> toPuts = differentTables ? preparePuts(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) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result)

Example 37 with GrpcTransaction

use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method putAndCommit_PutGivenForExistingAfterRead_ShouldUpdateRecord.

@Test
public void putAndCommit_PutGivenForExistingAfterRead_ShouldUpdateRecord() throws TransactionException {
    // Arrange
    populateRecords(TABLE_1);
    Get get = prepareGet(0, 0, TABLE_1);
    GrpcTransaction transaction = manager.start();
    // Act
    Optional<Result> result = transaction.get(get);
    assertThat(result).isPresent();
    int expected = getBalance(result.get()) + 100;
    Put put = preparePut(0, 0, TABLE_1).withValue(BALANCE, expected);
    transaction.put(put);
    transaction.commit();
    // Assert
    GrpcTransaction another = manager.start();
    Optional<Result> actual = another.get(get);
    assertThat(actual).isPresent();
    another.commit();
    assertThat(getBalance(actual.get())).isEqualTo(expected);
}
Also used : Get(com.scalar.db.api.Get) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 38 with GrpcTransaction

use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method commit_ConflictingPutAndDeleteGivenForExisting_ShouldCommitPutAndAbortDelete.

private void commit_ConflictingPutAndDeleteGivenForExisting_ShouldCommitPutAndAbortDelete(String table1, String table2) throws TransactionException {
    // Arrange
    boolean differentTables = !table1.equals(table2);
    int amount = 200;
    int from = 0;
    int to = NUM_TYPES;
    int anotherFrom = to;
    int anotherTo = NUM_TYPES * 2;
    populateRecords(table1);
    if (differentTables) {
        populateRecords(table2);
    }
    // Act
    GrpcTransaction transaction = manager.start();
    List<Get> gets1 = prepareGets(table1);
    List<Delete> deletes1 = prepareDeletes(table1);
    List<Get> gets2 = differentTables ? prepareGets(table2) : gets1;
    List<Delete> deletes2 = differentTables ? prepareDeletes(table2) : deletes1;
    transaction.get(gets1.get(from));
    transaction.delete(deletes1.get(from));
    transaction.get(gets2.get(to));
    transaction.delete(deletes2.get(to));
    assertThatCode(() -> prepareTransfer(anotherFrom, table2, anotherTo, table1, amount).commit()).doesNotThrowAnyException();
    assertThatThrownBy(transaction::commit).isInstanceOf(CommitException.class);
    // Assert
    GrpcTransaction another = manager.start();
    Optional<Result> fromResult = another.get(gets1.get(from));
    assertThat(fromResult).isPresent();
    assertThat(getBalance(fromResult.get())).isEqualTo(INITIAL_BALANCE);
    Optional<Result> toResult = another.get(gets2.get(to));
    assertThat(toResult).isPresent();
    assertThat(getBalance(toResult.get())).isEqualTo(INITIAL_BALANCE - amount);
    Optional<Result> anotherToResult = another.get(gets1.get(anotherTo));
    assertThat(anotherToResult).isPresent();
    assertThat(getBalance(anotherToResult.get())).isEqualTo(INITIAL_BALANCE + amount);
    another.commit();
}
Also used : Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Result(com.scalar.db.api.Result)

Example 39 with GrpcTransaction

use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method get_DeleteCalledBefore_ShouldReturnEmpty.

@Test
public void get_DeleteCalledBefore_ShouldReturnEmpty() throws TransactionException {
    // Arrange
    GrpcTransaction transaction = manager.start();
    transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
    transaction.commit();
    // Act
    GrpcTransaction transaction1 = manager.start();
    Get get = prepareGet(0, 0, TABLE_1);
    Optional<Result> resultBefore = transaction1.get(get);
    transaction1.delete(prepareDelete(0, 0, TABLE_1));
    Optional<Result> resultAfter = transaction1.get(get);
    assertThatCode(transaction1::commit).doesNotThrowAnyException();
    // Assert
    assertThat(resultBefore.isPresent()).isTrue();
    assertThat(resultAfter.isPresent()).isFalse();
}
Also used : Get(com.scalar.db.api.Get) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 40 with GrpcTransaction

use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method putAndCommit_GetsAndPutsGiven_ShouldCommitProperly.

private void putAndCommit_GetsAndPutsGiven_ShouldCommitProperly(String fromTable, String toTable) throws TransactionException {
    // Arrange
    boolean differentTables = !fromTable.equals(toTable);
    populateRecords(fromTable);
    if (differentTables) {
        populateRecords(toTable);
    }
    List<Get> fromGets = prepareGets(fromTable);
    List<Get> toGets = differentTables ? prepareGets(toTable) : fromGets;
    int amount = 100;
    int fromBalance = INITIAL_BALANCE - amount;
    int toBalance = INITIAL_BALANCE + amount;
    int from = 0;
    int to = NUM_TYPES;
    // Act
    prepareTransfer(from, fromTable, to, toTable, amount).commit();
    // Assert
    GrpcTransaction another = manager.start();
    Optional<Result> fromResult = another.get(fromGets.get(from));
    assertThat(fromResult).isPresent();
    assertThat(getBalance(fromResult.get())).isEqualTo(fromBalance);
    Optional<Result> toResult = another.get(toGets.get(to));
    assertThat(toResult).isPresent();
    assertThat(getBalance(toResult.get())).isEqualTo(toBalance);
    another.commit();
}
Also used : Get(com.scalar.db.api.Get) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Result(com.scalar.db.api.Result)

Aggregations

GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)52 Result (com.scalar.db.api.Result)41 Test (org.junit.Test)37 Get (com.scalar.db.api.Get)34 Put (com.scalar.db.api.Put)19 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)13 Scan (com.scalar.db.api.Scan)9 Delete (com.scalar.db.api.Delete)8 DistributedTransactionServiceWithConsensusCommitIntegrationTest.preparePut (com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.preparePut)7 TransactionState (com.scalar.db.api.TransactionState)4 IntValue (com.scalar.db.io.IntValue)4 DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet (com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet)4 Consistency (com.scalar.db.api.Consistency)2 DistributedStorageAdmin (com.scalar.db.api.DistributedStorageAdmin)2 TableMetadata (com.scalar.db.api.TableMetadata)2 ExecutionException (com.scalar.db.exception.storage.ExecutionException)2 TransactionException (com.scalar.db.exception.transaction.TransactionException)2 DataType (com.scalar.db.io.DataType)2 Key (com.scalar.db.io.Key)2 Value (com.scalar.db.io.Value)2