Search in sources :

Example 1 with Scanner

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

the class StorageIntegrationTestBase method scannerIterator_ScanWithPartitionKeyGiven_ShouldRetrieveCorrectResults.

@Test
public void scannerIterator_ScanWithPartitionKeyGiven_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);
    scanner.forEach(actual::add);
    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);
}
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 2 with Scanner

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

the class StorageIntegrationTestBase method scan_ScanLargeDataWithOrdering_ShouldRetrieveExpectedValues.

@Test
public void scan_ScanLargeDataWithOrdering_ShouldRetrieveExpectedValues() throws ExecutionException, IOException {
    // Arrange
    Key partitionKey = new Key(COL_NAME1, 1);
    for (int i = 0; i < 345; i++) {
        Key clusteringKey = new Key(COL_NAME4, i);
        storage.put(new Put(partitionKey, clusteringKey));
    }
    Scan scan = new Scan(partitionKey).withOrdering(new Ordering(COL_NAME4, Order.ASC));
    // Act
    List<Result> results = new ArrayList<>();
    try (Scanner scanner = storage.scan(scan)) {
        Iterator<Result> iterator = scanner.iterator();
        for (int i = 0; i < 234; i++) {
            results.add(iterator.next());
        }
    }
    // Assert
    assertThat(results.size()).isEqualTo(234);
    for (int i = 0; i < 234; i++) {
        assertThat(results.get(i).getPartitionKey().isPresent()).isTrue();
        assertThat(results.get(i).getClusteringKey().isPresent()).isTrue();
        assertThat(results.get(i).getPartitionKey().get().get().get(0).getAsInt()).isEqualTo(1);
        assertThat(results.get(i).getClusteringKey().get().get().get(0).getAsInt()).isEqualTo(i);
    }
}
Also used : Scanner(com.scalar.db.api.Scanner) Ordering(com.scalar.db.api.Scan.Ordering) ArrayList(java.util.ArrayList) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 3 with Scanner

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

the class StorageIntegrationTestBase method scannerIterator_OneAndIteratorCalled_ShouldRetrieveCorrectResults.

@Test
public void scannerIterator_OneAndIteratorCalled_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);
    Optional<Result> result = scanner.one();
    scanner.forEach(actual::add);
    scanner.close();
    // Assert
    assertThat(result.isPresent()).isTrue();
    assertThat(result.get().getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(result.get().getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(result.get().getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(result.get().getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
    assertThat(actual.size()).isEqualTo(2);
    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(1);
    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(2);
}
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 4 with Scanner

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

the class AdminIntegrationTestBase method truncateTable_ShouldTruncateProperly.

@Test
public void truncateTable_ShouldTruncateProperly() throws ExecutionException, IOException {
    // Arrange
    Key partitionKey = new Key(COL_NAME2, "aaa", COL_NAME1, 1);
    Key clusteringKey = new Key(COL_NAME4, 2, COL_NAME3, "bbb");
    storage.put(new Put(partitionKey, clusteringKey).withValue(COL_NAME5, 3).withValue(COL_NAME6, "ccc").withValue(COL_NAME7, 4L).withValue(COL_NAME8, 1.0f).withValue(COL_NAME9, 1.0d).withValue(COL_NAME10, true).withValue(COL_NAME11, "ddd".getBytes(StandardCharsets.UTF_8)).forNamespace(namespace1).forTable(TABLE1));
    // Act
    admin.truncateTable(namespace1, TABLE1);
    // Assert
    Scanner scanner = storage.scan(new Scan(partitionKey).forNamespace(namespace1).forTable(TABLE1));
    assertThat(scanner.all()).isEmpty();
    scanner.close();
}
Also used : Scanner(com.scalar.db.api.Scanner) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.Test)

Example 5 with Scanner

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

the class Snapshot method toSerializableWithExtraRead.

@VisibleForTesting
void toSerializableWithExtraRead(DistributedStorage storage) throws ExecutionException, CommitConflictException {
    if (!isExtraReadEnabled()) {
        return;
    }
    List<ParallelExecutorTask> tasks = new ArrayList<>();
    // Read set by scan is re-validated to check if there is no anti-dependency
    for (Map.Entry<Scan, List<Key>> entry : scanSet.entrySet()) {
        tasks.add(() -> {
            Map<Key, TransactionResult> currentReadMap = new HashMap<>();
            Set<Key> validatedReadSet = new HashSet<>();
            Scanner scanner = null;
            try {
                Scan scan = entry.getKey();
                // only get tx_id and tx_version columns because we use only them to compare
                scan.clearProjections();
                scan.withProjection(Attribute.ID).withProjection(Attribute.VERSION);
                ScalarDbUtils.addProjectionsForKeys(scan, getTableMetadata(scan));
                scanner = storage.scan(scan);
                for (Result result : scanner) {
                    TransactionResult transactionResult = new TransactionResult(result);
                    // Ignore records that this transaction has prepared (and that are in the write set)
                    if (transactionResult.getId().equals(id)) {
                        continue;
                    }
                    currentReadMap.put(new Key(scan, result), transactionResult);
                }
            } finally {
                if (scanner != null) {
                    try {
                        scanner.close();
                    } catch (IOException e) {
                        LOGGER.warn("failed to close the scanner", e);
                    }
                }
            }
            for (Key key : entry.getValue()) {
                if (writeSet.containsKey(key) || deleteSet.containsKey(key)) {
                    continue;
                }
                // Check if read records are not changed
                TransactionResult latestResult = currentReadMap.get(key);
                if (isChanged(Optional.of(latestResult), readSet.get(key))) {
                    throwExceptionDueToAntiDependency();
                }
                validatedReadSet.add(key);
            }
            // Check if the size of a read set by scan is not changed
            if (currentReadMap.size() != validatedReadSet.size()) {
                throwExceptionDueToAntiDependency();
            }
        });
    }
    // Calculate read set validated by scan
    Set<Key> validatedReadSetByScan = new HashSet<>();
    for (List<Key> values : scanSet.values()) {
        validatedReadSetByScan.addAll(values);
    }
    // Read set by get is re-validated to check if there is no anti-dependency
    for (Map.Entry<Key, Optional<TransactionResult>> entry : readSet.entrySet()) {
        Key key = entry.getKey();
        if (writeSet.containsKey(key) || deleteSet.containsKey(key) || validatedReadSetByScan.contains(key)) {
            continue;
        }
        tasks.add(() -> {
            // only get tx_id and tx_version columns because we use only them to compare
            Get get = new Get(key.getPartitionKey(), key.getClusteringKey().orElse(null)).withProjection(Attribute.ID).withProjection(Attribute.VERSION).withConsistency(Consistency.LINEARIZABLE).forNamespace(key.getNamespace()).forTable(key.getTable());
            Optional<TransactionResult> latestResult = storage.get(get).map(TransactionResult::new);
            // Check if a read record is not changed
            if (isChanged(latestResult, entry.getValue())) {
                throwExceptionDueToAntiDependency();
            }
        });
    }
    parallelExecutor.validate(tasks);
}
Also used : Scanner(com.scalar.db.api.Scanner) Optional(java.util.Optional) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ParallelExecutorTask(com.scalar.db.transaction.consensuscommit.ParallelExecutor.ParallelExecutorTask) Result(com.scalar.db.api.Result) Get(com.scalar.db.api.Get) Scan(com.scalar.db.api.Scan) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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