ScanExpressionCountQuery.java

/*
 * Copyright © 2018 spring-data-dynamodb (https://github.com/prasanna0586/spring-data-dynamodb)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.socialsignin.spring.data.dynamodb.query;

import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;
import software.amazon.awssdk.enhanced.dynamodb.model.ScanEnhancedRequest;

/**
 * Query implementation for counting results of a DynamoDB scan expression.
 * @param <T> the entity type
 * @author Prasanna Kumar Ramachandran
 */
public class ScanExpressionCountQuery<T> extends AbstractSingleEntityQuery<Long> implements Query<Long> {

    private final ScanEnhancedRequest scanExpression;

    private final Class<T> domainClass;

    private final boolean pageQuery;

    /**
     * Creates a new count query for the given scan expression.
     * @param dynamoDBOperations the DynamoDB operations instance
     * @param clazz the entity class
     * @param scanExpression the scan expression to count results for
     * @param pageQuery whether this is for a page query
     */
    public ScanExpressionCountQuery(DynamoDBOperations dynamoDBOperations, Class<T> clazz,
            ScanEnhancedRequest scanExpression, boolean pageQuery) {
        super(dynamoDBOperations, Long.class);
        this.scanExpression = scanExpression;
        this.domainClass = clazz;
        this.pageQuery = pageQuery;
    }

    @NonNull
    @Override
    public Long getSingleResult() {
        assertScanCountEnabled(isScanCountEnabled());
        return (long) dynamoDBOperations.count(domainClass, scanExpression);
    }

    /**
     * Validates that scan count operations are enabled for this query.
     * @param scanCountEnabled whether scan count is enabled
     * @throws IllegalArgumentException if scan count is not enabled
     */
    public void assertScanCountEnabled(boolean scanCountEnabled) {
        if (pageQuery) {
            Assert.isTrue(scanCountEnabled, "Scanning for the total counts for this query is not enabled.  "
                    + "To enable annotate your repository method with @EnableScanCount, or "
                    + "enable scanning for all repository methods by annotating your repository interface with @EnableScanCount.  This total count is required to serve this Page query - if total counts are not desired an alternative approach could be to replace the Page query with a Slice query ");

        } else {
            Assert.isTrue(scanCountEnabled, "Scanning for counts for this query is not enabled.  "
                    + "To enable annotate your repository method with @EnableScanCount, or "
                    + "enable scanning for all repository methods by annotating your repository interface with @EnableScanCount");
        }
    }

}