Search in sources :

Example 1 with Ordering

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;
}
Also used : Ordering(com.scalar.db.api.Scan.Ordering) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key)

Example 2 with Ordering

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);
    }
}
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 Ordering

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);
    }
}
Also used : Ordering(com.scalar.db.api.Scan.Ordering) ArrayList(java.util.ArrayList) Key(com.scalar.db.io.Key) ExpectedResult(com.scalar.db.util.TestUtils.ExpectedResult) Test(org.junit.jupiter.api.Test)

Example 4 with Ordering

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));
}
Also used : Ordering(com.scalar.db.api.Scan.Ordering) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 5 with Ordering

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));
}
Also used : QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) HashMap(java.util.HashMap) Ordering(com.scalar.db.api.Scan.Ordering)

Aggregations

Ordering (com.scalar.db.api.Scan.Ordering)8 Key (com.scalar.db.io.Key)6 Scan (com.scalar.db.api.Scan)3 ArrayList (java.util.ArrayList)3 Result (com.scalar.db.api.Result)2 Test (org.junit.Test)2 Test (org.junit.jupiter.api.Test)2 QueryRequest (software.amazon.awssdk.services.dynamodb.model.QueryRequest)2 Put (com.scalar.db.api.Put)1 Scanner (com.scalar.db.api.Scanner)1 ExpectedResult (com.scalar.db.util.TestUtils.ExpectedResult)1 HashMap (java.util.HashMap)1 QueryResponse (software.amazon.awssdk.services.dynamodb.model.QueryResponse)1