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

com.netflix.astyanax.thrift.AbstractThriftCqlQuery Maven / Gradle / Ivy

package com.netflix.astyanax.thrift;

import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.Callable;

import org.apache.cassandra.thrift.Cassandra.Client;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.SchemaDisagreementException;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;

import com.google.common.util.concurrent.ListenableFuture;
import com.netflix.astyanax.CassandraOperationType;
import com.netflix.astyanax.connectionpool.ConnectionContext;
import com.netflix.astyanax.connectionpool.OperationResult;
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.model.ConsistencyLevel;
import com.netflix.astyanax.model.CqlResult;
import com.netflix.astyanax.query.AbstractPreparedCqlQuery;
import com.netflix.astyanax.query.CqlQuery;
import com.netflix.astyanax.query.PreparedCqlQuery;
import com.netflix.astyanax.thrift.model.ThriftCqlResultImpl;
import com.netflix.astyanax.thrift.model.ThriftCqlRowsImpl;

public abstract class AbstractThriftCqlQuery implements CqlQuery {
    boolean useCompression = false;
    ThriftColumnFamilyQueryImpl cfQuery;
    String cql;
    ConsistencyLevel cl = ConsistencyLevel.CL_ONE;
    
    AbstractThriftCqlQuery(ThriftColumnFamilyQueryImpl cfQuery, String cql) {
        this.cfQuery = cfQuery;
        this.cql = cql;
        this.cl = cfQuery.consistencyLevel;
    }
    
    @Override
    public OperationResult> execute() throws ConnectionException {
        return cfQuery.keyspace.connectionPool.executeWithFailover(
                new AbstractKeyspaceOperationImpl>(cfQuery.keyspace.tracerFactory.newTracer(
                        CassandraOperationType.CQL, cfQuery.columnFamily), cfQuery.pinnedHost, cfQuery.keyspace.getKeyspaceName()) {
                    @Override
                    public CqlResult internalExecute(Client client, ConnectionContext context) throws Exception {
                        org.apache.cassandra.thrift.CqlResult res = execute_cql_query(client);
                        switch (res.getType()) {
                        case ROWS:
                            return new ThriftCqlResultImpl(new ThriftCqlRowsImpl(res.getRows(),
                                    cfQuery.columnFamily.getKeySerializer(), cfQuery.columnFamily.getColumnSerializer()));
                        case INT:
                            return new ThriftCqlResultImpl(res.getNum());
                        default:
                            return null;
                        }
                    }
                }, cfQuery.retry);
    }

    @Override
    public ListenableFuture>> executeAsync() throws ConnectionException {
        return cfQuery.keyspace.executor.submit(new Callable>>() {
            @Override
            public OperationResult> call() throws Exception {
                return execute();
            }
        });
    }

    @Override
    public CqlQuery useCompression() {
        useCompression = true;
        return this;
    }

    @Override
    public PreparedCqlQuery asPreparedStatement() {
        return new AbstractPreparedCqlQuery() {
            @Override
            public OperationResult> execute() throws ConnectionException {
                return cfQuery.keyspace.connectionPool.executeWithFailover(
                        new AbstractKeyspaceOperationImpl>(cfQuery.keyspace.tracerFactory.newTracer(
                                CassandraOperationType.CQL, cfQuery.columnFamily), cfQuery.pinnedHost, cfQuery.keyspace.getKeyspaceName()) {
                            @Override
                            public CqlResult internalExecute(Client client, ConnectionContext state) throws Exception {
                                Integer id = (Integer)state.getMetadata(cql);
                                if (id == null) {
                                    org.apache.cassandra.thrift.CqlPreparedResult res = prepare_cql_query(client);
                                    id = res.getItemId();
                                    state.setMetadata(cql, id);
                                }

                                org.apache.cassandra.thrift.CqlResult res = execute_prepared_cql_query(client, id, getValues());
                                switch (res.getType()) {
                                case ROWS:
                                    return new ThriftCqlResultImpl(new ThriftCqlRowsImpl(res.getRows(),
                                            cfQuery.columnFamily.getKeySerializer(), cfQuery.columnFamily.getColumnSerializer()));
                                case INT:
                                    return new ThriftCqlResultImpl(res.getNum());
                                    
                                default:
                                    return null;
                                }
                            }
                        }, cfQuery.retry);
            }

            @Override
            public ListenableFuture>> executeAsync() throws ConnectionException {
                return cfQuery.executor.submit(new Callable>>() {
                    @Override
                    public OperationResult> call() throws Exception {
                        return execute();
                    }
                });
            }
        };
    }
    
    public CqlQuery withConsistencyLevel(ConsistencyLevel cl) {
        this.cl = cl;
        return this;
    }
    
    protected abstract org.apache.cassandra.thrift.CqlPreparedResult prepare_cql_query(Client client) 
            throws InvalidRequestException, TException;
    
    protected abstract org.apache.cassandra.thrift.CqlResult execute_prepared_cql_query(Client client, int id, List values) 
            throws InvalidRequestException, UnavailableException, TimedOutException, SchemaDisagreementException, TException;
    
    protected abstract org.apache.cassandra.thrift.CqlResult execute_cql_query(Client client) 
            throws InvalidRequestException, UnavailableException, TimedOutException, SchemaDisagreementException, TException;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy