Search in sources :

Example 86 with Scan

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

the class CrudHandlerTest method scan_CalledTwice_SecondTimeShouldReturnTheSameFromSnapshot.

@Test
public void scan_CalledTwice_SecondTimeShouldReturnTheSameFromSnapshot() 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()).thenReturn(Optional.of(Collections.singletonList(key)));
    when(snapshot.containsKeyInReadSet(key)).thenReturn(false).thenReturn(true);
    when(snapshot.get(key)).thenReturn(Optional.of((TransactionResult) result));
    // Act
    List<Result> results1 = handler.scan(scan);
    List<Result> results2 = handler.scan(scan);
    // Assert
    TransactionResult expected = new TransactionResult(result);
    verify(snapshot).put(key, Optional.of(expected));
    assertThat(results1.size()).isEqualTo(1);
    assertThat(results1.get(0)).isEqualTo(new FilteredResult(expected, Collections.emptyList(), TABLE_METADATA));
    assertThat(results1).isEqualTo(results2);
}
Also used : 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 87 with Scan

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

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

the class CrudHandlerTest method scan_CalledAfterDeleteUnderRealSnapshot_ShouldReturnResultsWithoutDeletedRecord.

@Test
public void scan_CalledAfterDeleteUnderRealSnapshot_ShouldReturnResultsWithoutDeletedRecord() throws ExecutionException, CrudException {
    // Arrange
    Scan scan = prepareScan();
    result = prepareResult(TransactionState.COMMITTED);
    ImmutableMap<String, Column<?>> columns = ImmutableMap.<String, Column<?>>builder().put(ANY_NAME_1, TextColumn.of(ANY_NAME_1, ANY_TEXT_1)).put(ANY_NAME_2, TextColumn.of(ANY_NAME_2, ANY_TEXT_3)).put(Attribute.ID, ScalarDbUtils.toColumn(Attribute.toIdValue(ANY_ID_2))).put(Attribute.STATE, ScalarDbUtils.toColumn(Attribute.toStateValue(TransactionState.COMMITTED))).put(Attribute.VERSION, ScalarDbUtils.toColumn(Attribute.toVersionValue(2))).put(Attribute.BEFORE_ID, ScalarDbUtils.toColumn(Attribute.toBeforeIdValue(ANY_ID_1))).put(Attribute.BEFORE_STATE, ScalarDbUtils.toColumn(Attribute.toBeforeStateValue(TransactionState.COMMITTED))).put(Attribute.BEFORE_VERSION, ScalarDbUtils.toColumn(Attribute.toBeforeVersionValue(1))).build();
    Result result2 = new ResultImpl(columns, TABLE_METADATA);
    Map<Snapshot.Key, Optional<TransactionResult>> readSet = new HashMap<>();
    Map<Snapshot.Key, Delete> deleteSet = new HashMap<>();
    snapshot = new Snapshot(ANY_TX_ID, Isolation.SNAPSHOT, null, tableMetadataManager, parallelExecutor, readSet, new HashMap<>(), new HashMap<>(), deleteSet);
    handler = new CrudHandler(storage, snapshot, tableMetadataManager);
    when(scanner.iterator()).thenReturn(Arrays.asList(result, result2).iterator());
    when(storage.scan(scan)).thenReturn(scanner);
    Delete delete = new Delete(new Key(ANY_NAME_1, ANY_TEXT_1), new Key(ANY_NAME_2, ANY_TEXT_3)).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME);
    // Act
    handler.delete(delete);
    List<Result> results = handler.scan(scan);
    // Assert
    assertThat(results.size()).isEqualTo(1);
    assertThat(results.get(0)).isEqualTo(new FilteredResult(result, Collections.emptyList(), TABLE_METADATA));
    // check the delete set
    assertThat(deleteSet.size()).isEqualTo(1);
    assertThat(deleteSet).containsKey(new Snapshot.Key(delete));
    // check if the scanned data is inserted correctly in the read set
    assertThat(readSet.size()).isEqualTo(2);
    Snapshot.Key key1 = new Snapshot.Key(scan, result);
    assertThat(readSet.get(key1).isPresent()).isTrue();
    assertThat(readSet.get(key1).get()).isEqualTo(new TransactionResult(result));
    Snapshot.Key key2 = new Snapshot.Key(scan, result2);
    assertThat(readSet.get(key2).isPresent()).isTrue();
    assertThat(readSet.get(key2).get()).isEqualTo(new TransactionResult(result2));
}
Also used : Delete(com.scalar.db.api.Delete) Optional(java.util.Optional) HashMap(java.util.HashMap) ResultImpl(com.scalar.db.common.ResultImpl) Result(com.scalar.db.api.Result) TextColumn(com.scalar.db.io.TextColumn) Column(com.scalar.db.io.Column) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 89 with Scan

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

the class CrudHandlerTest method scan_GetCalledAfterScanUnderRealSnapshot_ShouldReturnFromSnapshot.

@Test
public void scan_GetCalledAfterScanUnderRealSnapshot_ShouldReturnFromSnapshot() throws ExecutionException, CrudException {
    // Arrange
    Scan scan = prepareScan();
    result = prepareResult(TransactionState.COMMITTED);
    snapshot = new Snapshot(ANY_TX_ID, Isolation.SNAPSHOT, null, tableMetadataManager, parallelExecutor);
    handler = new CrudHandler(storage, snapshot, tableMetadataManager);
    when(scanner.iterator()).thenReturn(Collections.singletonList(result).iterator());
    when(storage.scan(scan)).thenReturn(scanner);
    // Act
    List<Result> results = handler.scan(scan);
    Optional<Result> result = handler.get(prepareGet());
    // Assert
    assertThat(results.get(0)).isEqualTo(result.get());
}
Also used : Scan(com.scalar.db.api.Scan) Result(com.scalar.db.api.Result) Test(org.junit.jupiter.api.Test)

Example 90 with Scan

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

the class RollbackMutationComposerTest method add_ScanAndDeletedResultByThisGiven_ShouldComposePut.

@Test
public void add_ScanAndDeletedResultByThisGiven_ShouldComposePut() throws ExecutionException {
    // Arrange
    composer = new RollbackMutationComposer(ANY_ID_2, storage, tableMetadataManager, mutations);
    TransactionResult result = prepareResult(TransactionState.DELETED);
    Scan scan = prepareScan();
    // Act
    composer.add(scan, result);
    // Assert
    Put actual = (Put) mutations.get(0);
    Put expected = new Put(scan.getPartitionKey(), result.getClusteringKey().orElse(null)).forNamespace(scan.forNamespace().get()).forTable(scan.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) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Scan(com.scalar.db.api.Scan) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Aggregations

Scan (com.scalar.db.api.Scan)241 Key (com.scalar.db.io.Key)137 Test (org.junit.jupiter.api.Test)125 Result (com.scalar.db.api.Result)105 Test (org.junit.Test)72 Put (com.scalar.db.api.Put)37 HashMap (java.util.HashMap)32 QueryRequest (software.amazon.awssdk.services.dynamodb.model.QueryRequest)32 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)31 ByteBuffer (java.nio.ByteBuffer)27 KeyBytesEncoder (com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder)26 Scanner (com.scalar.db.api.Scanner)17 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)14 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)13 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)13 PartitionKey (com.azure.cosmos.models.PartitionKey)11 ArrayList (java.util.ArrayList)11 Delete (com.scalar.db.api.Delete)9 Value (com.scalar.db.io.Value)8 ConditionalExpression (com.scalar.db.api.ConditionalExpression)7