Search in sources :

Example 91 with Get

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

the class CrudHandlerTest method get_KeyNeitherExistsInSnapshotNorInStorage_ShouldReturnEmpty.

@Test
public void get_KeyNeitherExistsInSnapshotNorInStorage_ShouldReturnEmpty() throws CrudException, ExecutionException {
    // Arrange
    Get get = prepareGet();
    when(snapshot.get(new Snapshot.Key(get))).thenReturn(Optional.empty());
    when(storage.get(get)).thenReturn(Optional.empty());
    // Act
    Optional<Result> result = handler.get(get);
    // Assert
    assertThat(result.isPresent()).isFalse();
}
Also used : Get(com.scalar.db.api.Get) Result(com.scalar.db.api.Result) Test(org.junit.jupiter.api.Test)

Example 92 with Get

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

the class CrudHandlerTest 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_NAME).forTable(ANY_TABLE_NAME);
}
Also used : Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key)

Example 93 with Get

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

the class CrudHandlerTest method scan_GetCalledAfterScan_ShouldReturnFromSnapshot.

@Test
public void scan_GetCalledAfterScan_ShouldReturnFromSnapshot() throws ExecutionException, CrudException {
    // Arrange
    Scan scan = prepareScan();
    result = prepareResult(TransactionState.COMMITTED);
    doNothing().when(snapshot).put(any(Snapshot.Key.class), ArgumentMatchers.<Optional<TransactionResult>>any());
    when(scanner.iterator()).thenReturn(Collections.singletonList(result).iterator());
    when(storage.scan(scan)).thenReturn(scanner);
    Snapshot.Key key = new Snapshot.Key(scan, result);
    when(snapshot.get(scan)).thenReturn(Optional.empty());
    when(snapshot.containsKeyInReadSet(key)).thenReturn(false).thenReturn(true);
    when(snapshot.get(key)).thenReturn(Optional.of((TransactionResult) result));
    // Act
    List<Result> results = handler.scan(scan);
    Optional<Result> result = handler.get(prepareGet());
    // Assert
    verify(storage, never()).get(any(Get.class));
    assertThat(results.get(0)).isEqualTo(result.get());
}
Also used : Get(com.scalar.db.api.Get) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.jupiter.api.Test)

Example 94 with Get

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

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

the class RollbackMutationComposerTest method add_GetAndDeletedResultByThisGiven_ShouldComposePut.

@Test
public void add_GetAndDeletedResultByThisGiven_ShouldComposePut() throws ExecutionException {
    // Arrange
    composer = new RollbackMutationComposer(ANY_ID_2, storage, tableMetadataManager, mutations);
    TransactionResult result = prepareResult(TransactionState.DELETED);
    Get get = prepareGet();
    // Act
    composer.add(get, result);
    // Assert
    Put actual = (Put) mutations.get(0);
    Put expected = new Put(get.getPartitionKey(), get.getClusteringKey().orElse(null)).forNamespace(get.forNamespace().get()).forTable(get.forTable().get());
    expected.withConsistency(Consistency.LINEARIZABLE);
    expected.withCondition(new PutIf(new ConditionalExpression(ID, toIdValue(ANY_ID_2), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.DELETED), Operator.EQ)));
    expected.withValues(extractAfterValues(prepareInitialResult(ANY_ID_1, TransactionState.COMMITTED)));
    assertThat(actual).isEqualTo(expected);
}
Also used : PutIf(com.scalar.db.api.PutIf) Get(com.scalar.db.api.Get) ConditionalExpression(com.scalar.db.api.ConditionalExpression) 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