All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.astyanax.entitystore.NativeQuery Maven / Gradle / Ivy

package com.netflix.astyanax.entitystore;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.netflix.astyanax.model.Equality;
import com.netflix.astyanax.query.ColumnPredicate;

/**
 * SQL'ish like fluent API for defining a query.  This is mainly used by the various entity mappers
 * to query for a subset of columns.  Each entity mapper stores data differently and will use the
 * predicates here to make the correct lower level query.
 * 
 * @author elandau
 *
 * @param 
 * @param 
 */
public abstract class NativeQuery {
    protected List                 ids = Lists.newArrayList();
    protected Collection      columnNames;
    protected List   predicates;
    protected int                     columnLimit = Integer.MAX_VALUE;
    
    /**
     * Refine query for row key
     * @author elandau
     *
     */
    public class NativeIdQuery {
        public NativeQuery in(Collection keys) {
            ids.addAll(keys);
            return NativeQuery.this;
        }
        
        public NativeQuery in(K... keys) {
            ids.addAll(Lists.newArrayList(keys));
            return NativeQuery.this;
        }
        
        public NativeQuery equal(K key) {
            ids.add(key);
            return NativeQuery.this;
        }
    }
    
    /**
     * Refine query for column range or slice
     * @author elandau
     *
     */
    public class NativeColumnQuery {
        private ColumnPredicate predicate = new ColumnPredicate();
        
        public NativeColumnQuery(String name) {
            predicate.setName(name);
        }
        
        public NativeQuery in(Collection names) {
            columnNames = names;
            return NativeQuery.this;
        }

        public NativeQuery equal(Object value) {
            return addPredicate(predicate.setOp(Equality.EQUAL).setValue(value));
        }
        
        public NativeQuery greaterThan(Object value) {
            return addPredicate(predicate.setOp(Equality.GREATER_THAN).setValue(value));
        }
        
        public NativeQuery lessThan(Object value) {
            return addPredicate(predicate.setOp(Equality.LESS_THAN).setValue(value));
        }
        
        public NativeQuery greaterThanEqual(Object value) {
            return addPredicate(predicate.setOp(Equality.GREATER_THAN_EQUALS).setValue(value));
        }
        
        public NativeQuery lessThanEqual(Object value) {
            return addPredicate(predicate.setOp(Equality.LESS_THAN_EQUALS).setValue(value));
        }
        
    }
    
    public NativeIdQuery whereId() {
        return new NativeIdQuery();
    }
    
    public NativeColumnQuery whereColumn(String name) {
        return new NativeColumnQuery(name);
    }

    public NativeQuery limit(int columnLimit) {
        this.columnLimit = columnLimit;
        return this;
    }
    
    private NativeQuery addPredicate(ColumnPredicate predicate) {
        if (predicates == null) {
            predicates = Lists.newArrayList();
        }
        
        predicates.add(predicate);
        return this;
    }

    /**
     * Return a single entity (or first) response
     * @return
     * @throws Exception
     */
    public abstract T getSingleResult() throws Exception;
    
    /**
     * Return a result set of entities
     * @return
     * @throws Exception
     */
    public abstract Collection getResultSet() throws Exception;
    
    /**
     * Get the result set as a mapping of the id field to a collection of entities. This
     * is useful for a multi-get scenario where it is desirable to group all the 'entities'
     * within a row.
     * 
     * @return
     * @throws Exception
     */
    public abstract Map> getResultSetById() throws Exception;

    /**
     * Get the column count for each id in the query without sending data back
     * to the client.
     * @return
     * @throws Excerption
     */
    public abstract Map getResultSetCounts() throws Exception;
}