Search in sources :

Example 96 with Result

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method get_GetGivenForNonExisting_ShouldReturnEmpty.

@Test
public void get_GetGivenForNonExisting_ShouldReturnEmpty() throws TransactionException {
    // Arrange
    populate(TABLE_1);
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    // Act
    Optional<Result> result = transaction.get(prepareGet(0, 4, TABLE_1));
    transaction.prepare();
    transaction.commit();
    // Assert
    assertThat(result.isPresent()).isFalse();
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 97 with Result

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method commit_ConflictingPutAndDeleteGivenForExisting_ShouldCommitPutAndAbortDelete.

@Test
public void commit_ConflictingPutAndDeleteGivenForExisting_ShouldCommitPutAndAbortDelete() throws TransactionException {
    // Arrange
    int amount = 200;
    int fromId = 0;
    int fromType = 0;
    int toId = 1;
    int toType = 0;
    int anotherFromId = toId;
    int anotherFromType = toType;
    int anotherToId = 2;
    int anotherToType = 0;
    populate(TABLE_1);
    populate(TABLE_2);
    GrpcTwoPhaseCommitTransaction fromTx = manager.start();
    GrpcTwoPhaseCommitTransaction toTx = manager.join(fromTx.getId());
    fromTx.get(prepareGet(fromId, fromType, TABLE_1));
    fromTx.delete(prepareDelete(fromId, fromType, TABLE_1));
    toTx.get(prepareGet(toId, toType, TABLE_2));
    toTx.delete(prepareDelete(toId, toType, TABLE_2));
    // Act Assert
    assertThatCode(() -> {
        GrpcTwoPhaseCommitTransaction anotherFromTx = manager.start();
        GrpcTwoPhaseCommitTransaction anotherToTx = manager.join(anotherFromTx.getId());
        transfer(anotherFromId, anotherFromType, TABLE_2, anotherFromTx, anotherToId, anotherToType, TABLE_1, anotherToTx, amount);
        anotherFromTx.prepare();
        anotherToTx.prepare();
        anotherFromTx.commit();
        anotherToTx.commit();
    }).doesNotThrowAnyException();
    assertThatThrownBy(() -> {
        fromTx.prepare();
        toTx.prepare();
    }).isInstanceOf(PreparationException.class);
    fromTx.rollback();
    toTx.rollback();
    // Assert
    GrpcTwoPhaseCommitTransaction another = manager.start();
    Optional<Result> result = another.get(prepareGet(fromId, fromType, TABLE_1));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE);
    result = another.get(prepareGet(anotherFromId, anotherFromType, TABLE_2));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE - amount);
    result = another.get(prepareGet(anotherToId, anotherToType, TABLE_1));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE + amount);
    another.prepare();
    another.commit();
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 98 with Result

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method putAndCommit_PutGivenForExistingAndNeverRead_ShouldThrowPreparationException.

@Test
public void putAndCommit_PutGivenForExistingAndNeverRead_ShouldThrowPreparationException() throws TransactionException {
    // Arrange
    populate(TABLE_1);
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1100));
    // Act Assert
    assertThatThrownBy(transaction::prepare).isInstanceOf(PreparationException.class);
    transaction.rollback();
    // Assert
    GrpcTwoPhaseCommitTransaction another = manager.start();
    Optional<Result> result = another.get(prepareGet(0, 0, TABLE_1));
    another.prepare();
    another.commit();
    assertThat(result).isPresent();
    assertThat(getAccountId(result.get())).isEqualTo(0);
    assertThat(getAccountType(result.get())).isEqualTo(0);
    // a rolled back value
    assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE);
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 99 with Result

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method commit_ConflictingPutsGivenForExisting_ShouldCommitOneAndAbortTheOther.

@Test
public void commit_ConflictingPutsGivenForExisting_ShouldCommitOneAndAbortTheOther() throws TransactionException {
    // Arrange
    int amount1 = 100;
    int amount2 = 200;
    int fromId = 0;
    int fromType = 0;
    int toId = 1;
    int toType = 0;
    int anotherFromId = toId;
    int anotherFromType = toType;
    int anotherToId = 2;
    int anotherToType = 0;
    populate(TABLE_1);
    populate(TABLE_2);
    GrpcTwoPhaseCommitTransaction fromTx = manager.start();
    GrpcTwoPhaseCommitTransaction toTx = manager.join(fromTx.getId());
    transfer(fromId, fromType, TABLE_1, fromTx, toId, toType, TABLE_2, toTx, amount1);
    // Act Assert
    assertThatCode(() -> {
        GrpcTwoPhaseCommitTransaction anotherFromTx = manager.start();
        GrpcTwoPhaseCommitTransaction anotherToTx = manager.join(anotherFromTx.getId());
        transfer(anotherFromId, anotherFromType, TABLE_2, anotherFromTx, anotherToId, anotherToType, TABLE_1, anotherToTx, amount2);
        anotherFromTx.prepare();
        anotherToTx.prepare();
        anotherFromTx.commit();
        anotherToTx.commit();
    }).doesNotThrowAnyException();
    assertThatThrownBy(() -> {
        fromTx.prepare();
        toTx.prepare();
    }).isInstanceOf(PreparationException.class);
    fromTx.rollback();
    toTx.rollback();
    // Assert
    GrpcTwoPhaseCommitTransaction another = manager.start();
    Optional<Result> result = another.get(prepareGet(fromId, fromType, TABLE_1));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE);
    result = another.get(prepareGet(anotherFromId, anotherFromType, TABLE_2));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE - amount2);
    result = another.get(prepareGet(anotherToId, anotherToType, TABLE_1));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE + amount2);
    another.prepare();
    another.commit();
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 100 with Result

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method scan_ScanGivenForCommittedRecord_ShouldReturnRecord.

@Test
public void scan_ScanGivenForCommittedRecord_ShouldReturnRecord() throws TransactionException {
    // Arrange
    populate(TABLE_1);
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    // Act
    List<Result> results = transaction.scan(prepareScan(0, 0, 0, TABLE_1));
    transaction.prepare();
    transaction.commit();
    // Assert
    assertThat(results.size()).isEqualTo(1);
    assertThat(getAccountId(results.get(0))).isEqualTo(0);
    assertThat(getAccountType(results.get(0))).isEqualTo(0);
    assertThat(getBalance(results.get(0))).isEqualTo(INITIAL_BALANCE);
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Aggregations

Result (com.scalar.db.api.Result)324 Test (org.junit.Test)158 Get (com.scalar.db.api.Get)132 Scan (com.scalar.db.api.Scan)106 Put (com.scalar.db.api.Put)101 Key (com.scalar.db.io.Key)85 Test (org.junit.jupiter.api.Test)83 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)39 IntValue (com.scalar.db.io.IntValue)36 GrpcTwoPhaseCommitTransaction (com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction)32 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)29 Delete (com.scalar.db.api.Delete)28 TextValue (com.scalar.db.io.TextValue)24 Value (com.scalar.db.io.Value)19 BigIntValue (com.scalar.db.io.BigIntValue)16 Scanner (com.scalar.db.api.Scanner)15 ArrayList (java.util.ArrayList)14 BooleanValue (com.scalar.db.io.BooleanValue)11 ConditionalExpression (com.scalar.db.api.ConditionalExpression)10 ScanAll (com.scalar.db.api.ScanAll)9