use of com.scalar.db.api.Scan.Ordering in project scalardb by scalar-labs.
the class StorageMultipleClusteringKeyScanIntegrationTestBase method getScan.
private Scan getScan(DataType firstClusteringKeyType, Order firstClusteringOrder, DataType secondClusteringKeyType, Order secondClusteringOrder, @Nullable ClusteringKey startClusteringKey, @Nullable Boolean startInclusive, @Nullable ClusteringKey endClusteringKey, @Nullable Boolean endInclusive, OrderingType orderingType, int limit) {
Scan scan = new Scan(getPartitionKey()).forNamespace(getNamespaceName(firstClusteringKeyType)).forTable(getTableName(firstClusteringKeyType, firstClusteringOrder, secondClusteringKeyType, secondClusteringOrder));
if (startClusteringKey != null && startInclusive != null) {
Key key;
if (startClusteringKey.second != null) {
key = new Key(startClusteringKey.first, startClusteringKey.second);
} else {
key = new Key(startClusteringKey.first);
}
scan.withStart(key, startInclusive);
}
if (endClusteringKey != null && endInclusive != null) {
Key key;
if (endClusteringKey.second != null) {
key = new Key(endClusteringKey.first, endClusteringKey.second);
} else {
key = new Key(endClusteringKey.first);
}
scan.withEnd(key, endInclusive);
}
switch(orderingType) {
case BOTH_SPECIFIED:
scan.withOrdering(new Ordering(FIRST_CLUSTERING_KEY, firstClusteringOrder)).withOrdering(new Ordering(SECOND_CLUSTERING_KEY, secondClusteringOrder));
break;
case ONLY_FIRST_SPECIFIED:
scan.withOrdering(new Ordering(FIRST_CLUSTERING_KEY, firstClusteringOrder));
break;
case BOTH_SPECIFIED_AND_REVERSED:
scan.withOrdering(new Ordering(FIRST_CLUSTERING_KEY, TestUtils.reverseOrder(firstClusteringOrder))).withOrdering(new Ordering(SECOND_CLUSTERING_KEY, TestUtils.reverseOrder(secondClusteringOrder)));
break;
case ONLY_FIRST_SPECIFIED_AND_REVERSED:
scan.withOrdering(new Ordering(FIRST_CLUSTERING_KEY, TestUtils.reverseOrder(firstClusteringOrder)));
break;
case NOTHING:
break;
default:
throw new AssertionError();
}
if (limit > 0) {
scan.withLimit(limit);
}
return scan;
}
use of com.scalar.db.api.Scan.Ordering 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.Scan.Ordering in project scalardb by scalar-labs.
the class DistributedStorageIntegrationTestBase 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).withBlobValue(COL_NAME6, new byte[5000]));
}
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.Scan.Ordering in project scalardb by scalar-labs.
the class DistributedStorageWithReservedKeywordIntegrationTestBase method scan_WithReservedKeywordAndClusteringKeyRange_ShouldReturnProperResult.
@Test
public void scan_WithReservedKeywordAndClusteringKeyRange_ShouldReturnProperResult() throws ExecutionException, IOException {
// Arrange
populateRecords();
Scan scan = new Scan(new Key(columnName1, 1)).withStart(new Key(columnName4, 1), false).withEnd(new Key(columnName4, 3), false).withOrdering(new Ordering(columnName4, Order.DESC)).forNamespace(namespace).forTable(tableName);
List<Integer> expected = ImmutableList.of(2);
// Act
List<Result> actual = scanAll(scan);
// Assert
assertThat(actual.size()).isEqualTo(1);
assertThat(actual.get(0).getValue(columnName4).isPresent()).isTrue();
assertThat(actual.get(0).getValue(columnName4).get().getAsInt()).isEqualTo(expected.get(0));
}
use of com.scalar.db.api.Scan.Ordering in project scalardb by scalar-labs.
the class SelectStatementHandler method executeScan.
private Scanner executeScan(Scan scan, TableMetadata tableMetadata) {
DynamoOperation dynamoOperation = new DynamoOperation(scan, tableMetadata);
QueryRequest.Builder builder = QueryRequest.builder().tableName(dynamoOperation.getTableName());
if (!setConditions(builder, scan, tableMetadata)) {
// if setConditions() fails, return an empty scanner
return new EmptyScanner();
}
if (!scan.getOrderings().isEmpty()) {
Ordering ordering = scan.getOrderings().get(0);
if (ordering.getOrder() != tableMetadata.getClusteringOrder(ordering.getColumnName())) {
// reverse scan
builder.scanIndexForward(false);
}
}
if (scan.getLimit() > 0) {
builder.limit(scan.getLimit());
}
if (!scan.getProjections().isEmpty()) {
Map<String, String> expressionAttributeNames = new HashMap<>();
projectionExpression(builder, scan, expressionAttributeNames);
builder.expressionAttributeNames(expressionAttributeNames);
}
if (scan.getConsistency() != Consistency.EVENTUAL) {
builder.consistentRead(true);
}
com.scalar.db.storage.dynamo.request.QueryRequest queryRequest = new com.scalar.db.storage.dynamo.request.QueryRequest(client, builder.build());
return new QueryScanner(queryRequest, new ResultInterpreter(scan.getProjections(), tableMetadata));
}
Aggregations