Search in sources :

Example 16 with Scanner

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

the class StorageIntegrationTestBase method scan_ScanWithPartitionGivenThreeTimes_ShouldRetrieveResultsProperlyEveryTime.

@Test
public void scan_ScanWithPartitionGivenThreeTimes_ShouldRetrieveResultsProperlyEveryTime() throws IOException, ExecutionException {
    // Arrange
    populateRecords();
    int pKey = 0;
    // Act
    Scan scan = new Scan(new Key(COL_NAME1, pKey));
    double t1 = System.currentTimeMillis();
    List<Result> actual = scanAll(scan);
    double t2 = System.currentTimeMillis();
    Scanner scanner = storage.scan(scan);
    scanner.close();
    double t3 = System.currentTimeMillis();
    scanner = storage.scan(scan);
    scanner.close();
    double t4 = System.currentTimeMillis();
    // Assert
    assertThat(actual.get(0).getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
    assertThat(actual.get(0).getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, 0)));
    System.err.println("first: " + (t2 - t1) + " (ms)");
    System.err.println("second: " + (t3 - t2) + " (ms)");
    System.err.println("third: " + (t4 - t3) + " (ms)");
}
Also used : Scanner(com.scalar.db.api.Scanner) Scan(com.scalar.db.api.Scan) IntValue(com.scalar.db.io.IntValue) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 17 with Scanner

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

the class StorageIntegrationTestBase method scannerIterator_AllAndIteratorCalled_ShouldRetrieveCorrectResults.

@Test
public void scannerIterator_AllAndIteratorCalled_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);
    List<Result> all = scanner.all();
    scanner.forEach(actual::add);
    scanner.close();
    // Assert
    assertThat(all.size()).isEqualTo(3);
    assertThat(all.get(0).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(all.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(all.get(0).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(all.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
    assertThat(all.get(1).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(all.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(all.get(1).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(all.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
    assertThat(all.get(2).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(all.get(2).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(all.get(2).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(all.get(2).getValue(COL_NAME4).get().getAsInt()).isEqualTo(2);
    assertThat(actual).isEmpty();
}
Also used : Scanner(com.scalar.db.api.Scanner) ArrayList(java.util.ArrayList) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 18 with Scanner

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

the class StorageWithReservedKeywordIntegrationTestBase method scan_ScanWithReservedKeywordAndPartitionKeyGivenAndResultsIteratedWithOne_ShouldReturnWhatsPut.

@Test
public void scan_ScanWithReservedKeywordAndPartitionKeyGivenAndResultsIteratedWithOne_ShouldReturnWhatsPut() throws ExecutionException, IOException {
    // Arrange
    populateRecords();
    int pKey = 0;
    // Act
    Scan scan = new Scan(new Key(columnName1, 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(columnName1).isPresent()).isTrue();
    assertThat(results.get(0).getValue(columnName1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(0).getValue(columnName4).isPresent()).isTrue();
    assertThat(results.get(0).getValue(columnName4).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(1).getValue(columnName1).isPresent()).isTrue();
    assertThat(results.get(1).getValue(columnName1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(1).getValue(columnName4).isPresent()).isTrue();
    assertThat(results.get(1).getValue(columnName4).get().getAsInt()).isEqualTo(1);
    assertThat(results.get(2).getValue(columnName1).isPresent()).isTrue();
    assertThat(results.get(2).getValue(columnName1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(2).getValue(columnName4).isPresent()).isTrue();
    assertThat(results.get(2).getValue(columnName4).get().getAsInt()).isEqualTo(2);
    scanner.close();
}
Also used : Scanner(com.scalar.db.api.Scanner) ArrayList(java.util.ArrayList) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 19 with Scanner

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

the class SnapshotTest method toSerializableWithExtraRead_ScanSetExtended_ShouldProcessWithoutExceptions.

@Test
public void toSerializableWithExtraRead_ScanSetExtended_ShouldProcessWithoutExceptions() 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().withProjection(Attribute.ID).withProjection(Attribute.VERSION);
    when(storage.scan(scanWithProjections)).thenReturn(scanner);
    // Act Assert
    assertThatThrownBy(() -> snapshot.toSerializableWithExtraRead(storage)).isInstanceOf(CommitConflictException.class);
    // Assert
    verify(storage).scan(scanWithProjections);
}
Also used : Scanner(com.scalar.db.api.Scanner) DistributedStorage(com.scalar.db.api.DistributedStorage) Scan(com.scalar.db.api.Scan) Put(com.scalar.db.api.Put) Test(org.junit.Test)

Example 20 with Scanner

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

the class SnapshotTest method toSerializableWithExtraRead_MultipleScansInScanSetExist_ShouldThrowCommitConflictException.

@Test
public void toSerializableWithExtraRead_MultipleScansInScanSetExist_ShouldThrowCommitConflictException() 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, Optional.of(new TextValue(ANY_NAME_1, ANY_TEXT_1)), ANY_NAME_2, Optional.of(new TextValue(ANY_NAME_2, ANY_TEXT_2)), Attribute.ID, Optional.of(Attribute.toIdValue("id1")), Attribute.VERSION, Optional.of(Attribute.toVersionValue(ANY_VERSION))), TABLE_METADATA));
    Result result2 = new TransactionResult(new ResultImpl(ImmutableMap.of(ANY_NAME_1, Optional.of(new TextValue(ANY_NAME_1, ANY_TEXT_2)), ANY_NAME_2, Optional.of(new TextValue(ANY_NAME_2, ANY_TEXT_1)), Attribute.ID, Optional.of(Attribute.toIdValue("id2")), Attribute.VERSION, Optional.of(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).withProjection(Attribute.ID).withProjection(Attribute.VERSION);
    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).withProjection(Attribute.ID).withProjection(Attribute.VERSION);
    when(storage.scan(scan2WithProjections)).thenReturn(scanner2);
    // Act Assert
    assertThatCode(() -> snapshot.toSerializableWithExtraRead(storage)).doesNotThrowAnyException();
}
Also used : Scanner(com.scalar.db.api.Scanner) DistributedStorage(com.scalar.db.api.DistributedStorage) TextValue(com.scalar.db.io.TextValue) Scan(com.scalar.db.api.Scan) ResultImpl(com.scalar.db.util.ResultImpl) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Aggregations

Scanner (com.scalar.db.api.Scanner)20 Scan (com.scalar.db.api.Scan)17 Result (com.scalar.db.api.Result)14 Key (com.scalar.db.io.Key)14 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)9 DistributedStorage (com.scalar.db.api.DistributedStorage)6 Put (com.scalar.db.api.Put)6 Test (org.junit.jupiter.api.Test)6 IOException (java.io.IOException)3 Get (com.scalar.db.api.Get)2 List (java.util.List)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Ordering (com.scalar.db.api.Scan.Ordering)1 ResultImpl (com.scalar.db.common.ResultImpl)1 IntValue (com.scalar.db.io.IntValue)1 TextValue (com.scalar.db.io.TextValue)1 ParallelExecutorTask (com.scalar.db.transaction.consensuscommit.ParallelExecutor.ParallelExecutorTask)1 ResultImpl (com.scalar.db.util.ResultImpl)1 HashMap (java.util.HashMap)1