Search in sources :

Example 1 with CrudException

use of com.scalar.db.exception.transaction.CrudException in project scalardb by scalar-labs.

the class DistributedTransactionServiceWithJdbcTransactionIntegrationTest method populateRecords.

private void populateRecords() throws TransactionException {
    GrpcTransaction 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);
        try {
            transaction.put(put);
        } catch (CrudException e) {
            throw new RuntimeException(e);
        }
    }));
    transaction.commit();
}
Also used : IntStream(java.util.stream.IntStream) IntValue(com.scalar.db.io.IntValue) BeforeClass(org.junit.BeforeClass) 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) 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) GrpcTransactionManager(com.scalar.db.transaction.rpc.GrpcTransactionManager) DataType(com.scalar.db.io.DataType) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Before(org.junit.Before) GrpcConfig(com.scalar.db.storage.rpc.GrpcConfig) Key(com.scalar.db.io.Key) AfterClass(org.junit.AfterClass) IOException(java.io.IOException) Test(org.junit.Test) Put(com.scalar.db.api.Put) Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) List(java.util.List) Scan(com.scalar.db.api.Scan) Optional(java.util.Optional) TransactionException(com.scalar.db.exception.transaction.TransactionException) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) CrudException(com.scalar.db.exception.transaction.CrudException)

Example 2 with CrudException

use of com.scalar.db.exception.transaction.CrudException in project scalardb by scalar-labs.

the class CrudHandlerTest method get_KeyNotExistsInCrudSetAndExceptionThrownInStorage_ShouldThrowCrudException.

@Test
public void get_KeyNotExistsInCrudSetAndExceptionThrownInStorage_ShouldThrowCrudException() throws CrudException, ExecutionException {
    // Arrange
    Get get = prepareGet();
    when(snapshot.get(new Snapshot.Key(get))).thenReturn(Optional.empty());
    ExecutionException toThrow = mock(ExecutionException.class);
    when(storage.get(get)).thenThrow(toThrow);
    // Act Assert
    assertThatThrownBy(() -> handler.get(get)).isInstanceOf(CrudException.class).hasCause(toThrow);
}
Also used : Get(com.scalar.db.api.Get) ExecutionException(com.scalar.db.exception.storage.ExecutionException) CrudException(com.scalar.db.exception.transaction.CrudException) Test(org.junit.jupiter.api.Test)

Example 3 with CrudException

use of com.scalar.db.exception.transaction.CrudException in project scalardb by scalar-labs.

the class CrudHandlerTest method get_KeyNotExistsInSnapshotAndRecordInStorageCommitted_ShouldReturnFromStorageAndUpdateSnapshot.

@Test
public void get_KeyNotExistsInSnapshotAndRecordInStorageCommitted_ShouldReturnFromStorageAndUpdateSnapshot() throws CrudException, ExecutionException {
    // Arrange
    Get get = prepareGet();
    Optional<Result> expected = Optional.of(prepareResult(TransactionState.COMMITTED));
    Snapshot.Key key = new Snapshot.Key(get);
    when(snapshot.containsKeyInReadSet(key)).thenReturn(false);
    doNothing().when(snapshot).put(any(Snapshot.Key.class), ArgumentMatchers.<Optional<TransactionResult>>any());
    when(storage.get(get)).thenReturn(expected);
    when(snapshot.get(key)).thenReturn(expected.map(e -> (TransactionResult) e));
    // Act
    Optional<Result> result = handler.get(get);
    // Assert
    assertThat(result).isEqualTo(Optional.of(new FilteredResult(expected.get(), Collections.emptyList(), TABLE_METADATA)));
    verify(storage).get(get);
    verify(snapshot).put(key, Optional.of((TransactionResult) expected.get()));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ExecutionException(com.scalar.db.exception.storage.ExecutionException) HashMap(java.util.HashMap) TableMetadata(com.scalar.db.api.TableMetadata) CrudException(com.scalar.db.exception.transaction.CrudException) TextColumn(com.scalar.db.io.TextColumn) Result(com.scalar.db.api.Result) ScalarDbUtils(com.scalar.db.util.ScalarDbUtils) MockitoAnnotations(org.mockito.MockitoAnnotations) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Map(java.util.Map) ResultImpl(com.scalar.db.common.ResultImpl) DataType(com.scalar.db.io.DataType) InjectMocks(org.mockito.InjectMocks) Scanner(com.scalar.db.api.Scanner) Key(com.scalar.db.io.Key) ImmutableMap(com.google.common.collect.ImmutableMap) Column(com.scalar.db.io.Column) TransactionState(com.scalar.db.api.TransactionState) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) Mockito.verify(org.mockito.Mockito.verify) DistributedStorage(com.scalar.db.api.DistributedStorage) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) List(java.util.List) Scan(com.scalar.db.api.Scan) Optional(java.util.Optional) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.jupiter.api.Test)

Example 4 with CrudException

use of com.scalar.db.exception.transaction.CrudException in project scalardb by scalar-labs.

the class TwoPhaseCommitTransactionIntegrationTestBase method populateRecords.

private void populateRecords() throws TransactionException {
    TwoPhaseCommitTransaction 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).withIntValue(BALANCE, INITIAL_BALANCE).withIntValue(SOME_COLUMN, i * j);
        try {
            transaction.put(put);
        } catch (CrudException e) {
            throw new RuntimeException(e);
        }
    }));
    transaction.prepare();
    transaction.validate();
    transaction.commit();
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) IntColumn(com.scalar.db.io.IntColumn) IntValue(com.scalar.db.io.IntValue) Arrays(java.util.Arrays) TestUtils.assertResultsAreASubsetOf(com.scalar.db.util.TestUtils.assertResultsAreASubsetOf) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ExecutionException(com.scalar.db.exception.storage.ExecutionException) Value(com.scalar.db.io.Value) TestUtils(com.scalar.db.util.TestUtils) CrudException(com.scalar.db.exception.transaction.CrudException) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) ImmutableList(com.google.common.collect.ImmutableList) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) CommitException(com.scalar.db.exception.transaction.CommitException) DataType(com.scalar.db.io.DataType) Key(com.scalar.db.io.Key) Properties(java.util.Properties) ExpectedResultBuilder(com.scalar.db.util.TestUtils.ExpectedResult.ExpectedResultBuilder) TestUtils.assertResultsContainsExactlyInAnyOrder(com.scalar.db.util.TestUtils.assertResultsContainsExactlyInAnyOrder) TransactionFactory(com.scalar.db.service.TransactionFactory) Test(org.junit.jupiter.api.Test) List(java.util.List) ExpectedResult(com.scalar.db.util.TestUtils.ExpectedResult) Ordering(com.scalar.db.api.Scan.Ordering) PreparationConflictException(com.scalar.db.exception.transaction.PreparationConflictException) Optional(java.util.Optional) TransactionException(com.scalar.db.exception.transaction.TransactionException) Collections(java.util.Collections) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) Key(com.scalar.db.io.Key) CrudException(com.scalar.db.exception.transaction.CrudException)

Example 5 with CrudException

use of com.scalar.db.exception.transaction.CrudException in project scalardb by scalar-labs.

the class JdbcTransaction method delete.

@Override
public void delete(Delete delete) throws CrudException {
    delete = copyAndSetTargetToIfNot(delete);
    // Ignore the condition in the delete
    if (delete.getCondition().isPresent()) {
        LOGGER.warn("ignoring the condition of the mutation: {}", delete);
        delete.withCondition(null);
    }
    try {
        jdbcService.delete(delete, connection);
    } catch (SQLException e) {
        throw createCrudException(e, "delete operation failed");
    } catch (ExecutionException e) {
        throw new CrudException("delete operation failed", e);
    }
}
Also used : SQLException(java.sql.SQLException) ExecutionException(com.scalar.db.exception.storage.ExecutionException) CrudException(com.scalar.db.exception.transaction.CrudException)

Aggregations

CrudException (com.scalar.db.exception.transaction.CrudException)14 ExecutionException (com.scalar.db.exception.storage.ExecutionException)12 List (java.util.List)7 Optional (java.util.Optional)7 Get (com.scalar.db.api.Get)6 TransactionException (com.scalar.db.exception.transaction.TransactionException)6 Result (com.scalar.db.api.Result)5 Scan (com.scalar.db.api.Scan)5 TableMetadata (com.scalar.db.api.TableMetadata)5 DataType (com.scalar.db.io.DataType)5 Key (com.scalar.db.io.Key)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 CommitException (com.scalar.db.exception.transaction.CommitException)4 CrudConflictException (com.scalar.db.exception.transaction.CrudConflictException)4 IntValue (com.scalar.db.io.IntValue)4 Value (com.scalar.db.io.Value)4 ArrayList (java.util.ArrayList)4 IntStream (java.util.stream.IntStream)4 Delete (com.scalar.db.api.Delete)3 Test (org.junit.jupiter.api.Test)3