Search in sources :

Example 61 with Get

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

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

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

the class TwoPhaseConsensusCommitTest method prepareGet.

private Get prepareGet() {
    Key partitionKey = new Key(ANY_NAME_1, ANY_TEXT_1);
    Key clusteringKey = new Key(ANY_NAME_2, ANY_TEXT_2);
    return new Get(partitionKey, clusteringKey).forNamespace(ANY_NAMESPACE).forTable(ANY_TABLE_NAME);
}
Also used : Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key)

Example 64 with Get

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

the class TwoPhaseConsensusCommitTest method get_GetForUncommittedRecordGiven_ShouldRecoverRecord.

@Test
public void get_GetForUncommittedRecordGiven_ShouldRecoverRecord() throws CrudException {
    // Arrange
    Get get = prepareGet();
    TwoPhaseConsensusCommit transaction = new TwoPhaseConsensusCommit(crud, commit, recovery, false);
    TransactionResult result = mock(TransactionResult.class);
    UncommittedRecordException toThrow = mock(UncommittedRecordException.class);
    when(crud.get(get)).thenThrow(toThrow);
    when(crud.getSnapshot()).thenReturn(snapshot);
    when(toThrow.getResults()).thenReturn(Collections.singletonList(result));
    // Act
    assertThatThrownBy(() -> transaction.get(get)).isInstanceOf(UncommittedRecordException.class);
    // Assert
    verify(recovery).recover(get, result);
}
Also used : Get(com.scalar.db.api.Get) Test(org.junit.jupiter.api.Test)

Example 65 with Get

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

the class JdbcTransactionManagerTest method whenCommitFails_shouldThrowCommitExceptionAndRollback.

@Test
public void whenCommitFails_shouldThrowCommitExceptionAndRollback() throws Exception {
    // Arrange
    doThrow(sqlException).when(connection).commit();
    // Act Assert
    assertThatThrownBy(() -> {
        JdbcTransaction transaction = manager.start();
        Get get = new Get(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
        transaction.get(get);
        Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").forNamespace(NAMESPACE).forTable(TABLE);
        transaction.put(put);
        transaction.commit();
    }).isInstanceOf(CommitException.class);
    verify(connection).rollback();
    verify(connection).close();
}
Also used : Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Aggregations

Get (com.scalar.db.api.Get)286 Test (org.junit.jupiter.api.Test)130 Result (com.scalar.db.api.Result)129 Key (com.scalar.db.io.Key)126 Test (org.junit.Test)88 Put (com.scalar.db.api.Put)86 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)32 Delete (com.scalar.db.api.Delete)28 IntValue (com.scalar.db.io.IntValue)28 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)21 TextValue (com.scalar.db.io.TextValue)16 ConditionalExpression (com.scalar.db.api.ConditionalExpression)13 PartitionKey (com.azure.cosmos.models.PartitionKey)12 TableMetadata (com.scalar.db.api.TableMetadata)10 BooleanValue (com.scalar.db.io.BooleanValue)10 PutIf (com.scalar.db.api.PutIf)8 BigIntValue (com.scalar.db.io.BigIntValue)7 HashMap (java.util.HashMap)7 BlobValue (com.scalar.db.io.BlobValue)6 DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet (com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet)6