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);
}
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());
}
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));
}
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());
}
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);
}
Aggregations