use of com.scalar.db.api.Scanner in project scalardb by scalar-labs.
the class SnapshotTest method toSerializableWithExtraRead_MultipleScansInScanSetExist_ShouldProcessWithoutExceptions.
@Test
public void toSerializableWithExtraRead_MultipleScansInScanSetExist_ShouldProcessWithoutExceptions() throws ExecutionException {
// Arrange
snapshot = prepareSnapshot(Isolation.SERIALIZABLE, SerializableStrategy.EXTRA_READ);
Scan scan1 = new Scan(new Key(ANY_NAME_1, ANY_TEXT_1)).withStart(new Key(ANY_NAME_2, ANY_TEXT_2)).withConsistency(Consistency.LINEARIZABLE).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME);
Scan scan2 = new Scan(new Key(ANY_NAME_1, ANY_TEXT_2)).withStart(new Key(ANY_NAME_2, ANY_TEXT_1)).withConsistency(Consistency.LINEARIZABLE).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME);
Result result1 = new TransactionResult(new ResultImpl(ImmutableMap.of(ANY_NAME_1, TextColumn.of(ANY_NAME_1, ANY_TEXT_1), ANY_NAME_2, TextColumn.of(ANY_NAME_2, ANY_TEXT_2), Attribute.ID, ScalarDbUtils.toColumn(Attribute.toIdValue("id1")), Attribute.VERSION, ScalarDbUtils.toColumn(Attribute.toVersionValue(ANY_VERSION))), TABLE_METADATA));
Result result2 = new TransactionResult(new ResultImpl(ImmutableMap.of(ANY_NAME_1, TextColumn.of(ANY_NAME_1, ANY_TEXT_2), ANY_NAME_2, TextColumn.of(ANY_NAME_2, ANY_TEXT_1), Attribute.ID, ScalarDbUtils.toColumn(Attribute.toIdValue("id2")), Attribute.VERSION, ScalarDbUtils.toColumn(Attribute.toVersionValue(ANY_VERSION))), TABLE_METADATA));
Snapshot.Key key1 = new Snapshot.Key(scan1, result1);
Snapshot.Key key2 = new Snapshot.Key(scan2, result2);
snapshot.put(scan1, Collections.singletonList(key1));
snapshot.put(scan2, Collections.singletonList(key2));
snapshot.put(key1, Optional.of(new TransactionResult(result1)));
snapshot.put(key2, Optional.of(new TransactionResult(result2)));
DistributedStorage storage = mock(DistributedStorage.class);
Scanner scanner1 = mock(Scanner.class);
when(scanner1.iterator()).thenReturn(Collections.singletonList(result1).iterator());
Scan scan1WithProjections = new Scan(new Key(ANY_NAME_1, ANY_TEXT_1)).withStart(new Key(ANY_NAME_2, ANY_TEXT_2)).withConsistency(Consistency.LINEARIZABLE).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME).withProjections(Arrays.asList(Attribute.ID, Attribute.VERSION, ANY_NAME_1, ANY_NAME_2));
when(storage.scan(scan1WithProjections)).thenReturn(scanner1);
Scanner scanner2 = mock(Scanner.class);
when(scanner2.iterator()).thenReturn(Collections.singletonList(result2).iterator());
Scan scan2WithProjections = new Scan(new Key(ANY_NAME_1, ANY_TEXT_2)).withStart(new Key(ANY_NAME_2, ANY_TEXT_1)).withConsistency(Consistency.LINEARIZABLE).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME).withProjections(Arrays.asList(Attribute.ID, Attribute.VERSION, ANY_NAME_1, ANY_NAME_2));
when(storage.scan(scan2WithProjections)).thenReturn(scanner2);
// Act Assert
assertThatCode(() -> snapshot.toSerializableWithExtraRead(storage)).doesNotThrowAnyException();
}
use of com.scalar.db.api.Scanner in project scalardb by scalar-labs.
the class SnapshotTest method toSerializableWithExtraRead_ScanSetNotChanged_ShouldProcessWithoutExceptions.
@Test
public void toSerializableWithExtraRead_ScanSetNotChanged_ShouldProcessWithoutExceptions() throws ExecutionException {
// Arrange
snapshot = prepareSnapshot(Isolation.SERIALIZABLE, SerializableStrategy.EXTRA_READ);
Scan scan = prepareScan();
Put put = preparePut();
TransactionResult txResult = prepareResult(ANY_ID);
Snapshot.Key key = new Snapshot.Key(scan, txResult);
snapshot.put(key, Optional.of(txResult));
snapshot.put(scan, Collections.singletonList(key));
snapshot.put(new Snapshot.Key(put), put);
DistributedStorage storage = mock(DistributedStorage.class);
Scanner scanner = mock(Scanner.class);
when(scanner.iterator()).thenReturn(Collections.singletonList((Result) txResult).iterator());
Scan scanWithProjections = prepareScan().withProjections(Arrays.asList(Attribute.ID, Attribute.VERSION, ANY_NAME_1, ANY_NAME_2));
when(storage.scan(scanWithProjections)).thenReturn(scanner);
// Act Assert
assertThatCode(() -> snapshot.toSerializableWithExtraRead(storage)).doesNotThrowAnyException();
// Assert
verify(storage).scan(scanWithProjections);
}
use of com.scalar.db.api.Scanner in project scalardb by scalar-labs.
the class SnapshotTest method toSerializableWithExtraRead_ScanSetExtended_ShouldThrowCommitConflictException.
@Test
public void toSerializableWithExtraRead_ScanSetExtended_ShouldThrowCommitConflictException() throws ExecutionException {
// Arrange
snapshot = prepareSnapshot(Isolation.SERIALIZABLE, SerializableStrategy.EXTRA_READ);
Scan scan = prepareScan();
Put put = preparePut();
TransactionResult result = prepareResult(ANY_ID + "x");
snapshot.put(scan, Collections.emptyList());
snapshot.put(new Snapshot.Key(put), put);
DistributedStorage storage = mock(DistributedStorage.class);
TransactionResult txResult = new TransactionResult(result);
Scanner scanner = mock(Scanner.class);
when(scanner.iterator()).thenReturn(Collections.singletonList((Result) txResult).iterator());
Scan scanWithProjections = prepareScan().withProjections(Arrays.asList(Attribute.ID, Attribute.VERSION, ANY_NAME_1, ANY_NAME_2));
when(storage.scan(scanWithProjections)).thenReturn(scanner);
// Act Assert
assertThatThrownBy(() -> snapshot.toSerializableWithExtraRead(storage)).isInstanceOf(CommitConflictException.class);
// Assert
verify(storage).scan(scanWithProjections);
}
use of com.scalar.db.api.Scanner in project scalardb by scalar-labs.
the class StorageIntegrationTestBase method scan_ScanWithPartitionKeyGivenAndResultsIteratedWithOne_ShouldReturnWhatsPut.
@Test
public void scan_ScanWithPartitionKeyGivenAndResultsIteratedWithOne_ShouldReturnWhatsPut() throws ExecutionException, IOException {
// Arrange
populateRecords();
int pKey = 0;
// Act
Scan scan = new Scan(new Key(COL_NAME1, pKey));
Scanner scanner = storage.scan(scan);
// Assert
List<Result> results = new ArrayList<>();
Optional<Result> result = scanner.one();
assertThat(result.isPresent()).isTrue();
results.add(result.get());
result = scanner.one();
assertThat(result.isPresent()).isTrue();
results.add(result.get());
result = scanner.one();
assertThat(result.isPresent()).isTrue();
results.add(result.get());
result = scanner.one();
assertThat(result.isPresent()).isFalse();
assertThat(results.size()).isEqualTo(3);
assertThat(results.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
assertThat(results.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
assertThat(results.get(2).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(2).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(2).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(2).getValue(COL_NAME4).get().getAsInt()).isEqualTo(2);
scanner.close();
}
use of com.scalar.db.api.Scanner in project scalardb by scalar-labs.
the class StorageIntegrationTestBase method scannerIterator_IteratorCalledMultipleTimes_ShouldRetrieveCorrectResults.
@Test
public void scannerIterator_IteratorCalledMultipleTimes_ShouldRetrieveCorrectResults() throws ExecutionException, IOException {
// Arrange
populateRecords();
int pKey = 0;
// Act
Scan scan = new Scan(new Key(COL_NAME1, pKey));
List<Result> actual = new ArrayList<>();
Scanner scanner = storage.scan(scan);
actual.add(scanner.iterator().next());
actual.add(scanner.iterator().next());
actual.add(scanner.iterator().next());
scanner.close();
// Assert
assertThat(actual.size()).isEqualTo(3);
assertThat(actual.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
assertThat(actual.get(2).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(2).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(2).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(2).getValue(COL_NAME4).get().getAsInt()).isEqualTo(2);
}
Aggregations