Package org.easycassandra.persistence.cassandra

Source Code of org.easycassandra.persistence.cassandra.CountQuery

/*
*  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.easycassandra.persistence.cassandra;

import org.easycassandra.ClassInformation;
import org.easycassandra.ClassInformation.KeySpaceInformation;
import org.easycassandra.ClassInformations;

import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;

/**
* Class to mount and execute a query to return the number of column in a family
* column.
* @author otaviojava
*/
class CountQuery {

    private String keySpace;

    public CountQuery(String keySpace) {
        this.keySpace = keySpace;
    }

    /**
     * return the number of row in a column family.
     * @param bean - column family
     * @param session
     * @return number of register in a column family
     */
    public Long count(Class<?> bean, Session session,
            ConsistencyLevel consistency) {
        Select select = prepareCount(bean, consistency);
        ResultSet resultSet = session.execute(select);
        return resultSet.all().get(0).getLong(0);
    }

    protected Select prepareCount(Class<?> bean, ConsistencyLevel consistency) {
        ClassInformation classInformation = ClassInformations.INSTACE.getClass(bean);
        KeySpaceInformation key = classInformation.getKeySpace(keySpace);
        Select select = QueryBuilder.select().countAll()
                .from(key.getKeySpace(), key.getColumnFamily());
        select.setConsistencyLevel(consistency);
        return select;
    }
}
TOP

Related Classes of org.easycassandra.persistence.cassandra.CountQuery

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.