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