Search in sources :

Example 6 with Ordering

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

the class DistributedStorageMultipleClusteringKeyScanIntegrationTestBase 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) Key(com.scalar.db.io.Key)

Example 7 with Ordering

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

the class StorageWithReservedKeywordIntegrationTestBase 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) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 8 with Ordering

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

the class SelectStatementHandler method executeQuery.

private List<Map<String, AttributeValue>> executeQuery(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 empty list
        return new ArrayList<>();
    }
    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()) {
        projectionExpression(builder, scan);
    }
    if (scan.getConsistency() != Consistency.EVENTUAL) {
        builder.consistentRead(true);
    }
    QueryResponse queryResponse = client.query(builder.build());
    return new ArrayList<>(queryResponse.items());
}
Also used : QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse) ArrayList(java.util.ArrayList) 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