Search in sources :

Example 21 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method scan_DeleteCalledBefore_ShouldReturnEmpty.

@Test
public void scan_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();
    Scan scan = prepareScan(0, 0, 0, TABLE_1);
    List<Result> resultBefore = transaction1.scan(scan);
    transaction1.delete(prepareDelete(0, 0, TABLE_1));
    List<Result> resultAfter = transaction1.scan(scan);
    assertThatCode(transaction1::commit).doesNotThrowAnyException();
    // Assert
    assertThat(resultBefore.size()).isEqualTo(1);
    assertThat(resultAfter.size()).isEqualTo(0);
}
Also used : Scan(com.scalar.db.api.Scan) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 22 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method putAndCommit_PutGivenForNonExisting_ShouldCreateRecord.

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

Example 23 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method commit_DeleteGivenForExistingAfterRead_ShouldDeleteRecord.

@Test
public void commit_DeleteGivenForExistingAfterRead_ShouldDeleteRecord() throws TransactionException {
    // Arrange
    populateRecords(TABLE_1);
    Get get = prepareGet(0, 0, TABLE_1);
    Delete delete = prepareDelete(0, 0, TABLE_1);
    GrpcTransaction transaction = manager.start();
    // Act
    Optional<Result> result = transaction.get(get);
    transaction.delete(delete);
    transaction.commit();
    // Assert
    assertThat(result.isPresent()).isTrue();
    GrpcTransaction another = manager.start();
    assertThat(another.get(get).isPresent()).isFalse();
    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) Test(org.junit.Test)

Example 24 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method prepareDeletes.

private GrpcTransaction prepareDeletes(int one, String table, int another, String anotherTable) throws TransactionException {
    boolean differentTables = !table.equals(anotherTable);
    GrpcTransaction transaction = manager.start();
    List<Get> gets = prepareGets(table);
    List<Get> anotherGets = differentTables ? prepareGets(anotherTable) : gets;
    transaction.get(gets.get(one));
    transaction.get(anotherGets.get(another));
    List<Delete> deletes = prepareDeletes(table);
    List<Delete> anotherDeletes = differentTables ? prepareDeletes(anotherTable) : deletes;
    transaction.delete(deletes.get(one));
    transaction.delete(anotherDeletes.get(another));
    return transaction;
}
Also used : Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction)

Example 25 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method scan_NonOverlappingPutGivenBefore_ShouldScan.

@Test
public void scan_NonOverlappingPutGivenBefore_ShouldScan() throws TransactionException {
    // Arrange
    GrpcTransaction transaction = manager.start();
    transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
    // Act
    Scan scan = prepareScan(0, 1, 1, TABLE_1);
    Throwable thrown = catchThrowable(() -> transaction.scan(scan));
    transaction.commit();
    // Assert
    assertThat(thrown).doesNotThrowAnyException();
}
Also used : Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Scan(com.scalar.db.api.Scan) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Test(org.junit.Test)

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