Java Code Examples for com.datastax.driver.core.Statement#setFetchSize()

The following examples show how to use com.datastax.driver.core.Statement#setFetchSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AdaptiveResultSet.java    From emodb with Apache License 2.0 6 votes vote down vote up
private static ListenableFuture<ResultSet> executeAdaptiveQueryAsync(Session session, Statement statement, int fetchSize,
                                                                     int remainingAdaptations) {

    statement.setFetchSize(fetchSize);

    ResultSetFuture rawFuture = session.executeAsync(statement);

    // Lazily wrap the result set from the async result with an AdaptiveResultSet
    ListenableFuture<ResultSet> adaptiveFuture = Futures.transform(rawFuture, new Function<ResultSet, ResultSet>() {
        @Override
        public ResultSet apply(ResultSet resultSet) {
            return new AdaptiveResultSet(session, resultSet, remainingAdaptations);
        }
    });

    return Futures.withFallback(adaptiveFuture, t -> {
        if (isAdaptiveException(t) && remainingAdaptations > 0 && fetchSize > MIN_FETCH_SIZE) {
            // Try again with half the fetch size
            int reducedFetchSize = Math.max(fetchSize / 2, MIN_FETCH_SIZE);
            _log.debug("Repeating previous query with fetch size {} due to {}", reducedFetchSize, t.getMessage());
            return executeAdaptiveQueryAsync(session, statement, reducedFetchSize, remainingAdaptations - 1);
        }
        throw Throwables.propagate(t);
    });
}
 
Example 2
Source File: AdaptiveResultSet.java    From emodb with Apache License 2.0 6 votes vote down vote up
/**
 * Executes a query sychronously, dynamically adjusting the fetch size down if necessary.
 */
public static ResultSet executeAdaptiveQuery(Session session, Statement statement, int fetchSize) {
    int remainingAdaptations = MAX_ADAPTATIONS;
    while (true) {
        try {
            statement.setFetchSize(fetchSize);
            ResultSet resultSet = session.execute(statement);
            return new AdaptiveResultSet(session, resultSet, remainingAdaptations);
        } catch (Throwable t) {
            if (isAdaptiveException(t) && --remainingAdaptations != 0 && fetchSize > MIN_FETCH_SIZE) {
                // Try again with half the fetch size
                fetchSize = Math.max(fetchSize / 2, MIN_FETCH_SIZE);
                _log.debug("Repeating previous query with fetch size {} due to {}", fetchSize, t.getMessage());
            } else {
                throw Throwables.propagate(t);
            }
        }
    }
}
 
Example 3
Source File: CassandraSessionImpl.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Tunes CQL statement execution options (consistency level, fetch option and etc.).
 *
 * @param statement Statement.
 * @return Modified statement.
 */
private Statement tuneStatementExecutionOptions(Statement statement) {
    String qry = "";

    if (statement instanceof BoundStatement)
        qry = ((BoundStatement)statement).preparedStatement().getQueryString().trim().toLowerCase();
    else if (statement instanceof PreparedStatement)
        qry = ((PreparedStatement)statement).getQueryString().trim().toLowerCase();

    boolean readStatement = qry.startsWith("select");
    boolean writeStatement = statement instanceof Batch || statement instanceof BatchStatement ||
        qry.startsWith("insert") || qry.startsWith("delete") || qry.startsWith("update");

    if (readStatement && readConsistency != null)
        statement.setConsistencyLevel(readConsistency);

    if (writeStatement && writeConsistency != null)
        statement.setConsistencyLevel(writeConsistency);

    if (fetchSize != null)
        statement.setFetchSize(fetchSize);

    return statement;
}
 
Example 4
Source File: StoreUtils.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
static <T> Observable<T> retrieve(Session session,
                                  ObjectMapper mapper,
                                  Statement fetchStatement,
                                  Class<T> type,
                                  EntitySanitizer entitySanitizer,
                                  boolean failOnError) {
    fetchStatement.setFetchSize(Integer.MAX_VALUE);
    return execute(session, fetchStatement)
            .flatMapIterable(resultSet -> {
                        List<Row> all = resultSet.all();
                        List<T> converted = new ArrayList<>();
                        all.forEach(row -> {
                            String text = row.getString(0);
                            try {
                                T value = ObjectMappers.readValue(mapper, text, type);
                                Set<ValidationError> violations = entitySanitizer.validate(value);
                                if (violations.isEmpty()) {
                                    converted.add(value);
                                } else {
                                    if (failOnError) {
                                        throw AgentStoreException.badData(value, violations);
                                    }
                                    logger.warn("Ignoring bad record of type {} due to validation constraint violations: record={}, violations={}", type, value, violations);
                                }
                            } catch (Exception e) {
                                if (failOnError) {
                                    throw e;
                                }
                                logger.warn("Ignoring bad record of type {}: content={}, jacksonError={}", type.getName(), text, e.getMessage());
                            }
                        });
                        return converted;
                    }
            );
}
 
Example 5
Source File: AdaptiveResultSet.java    From emodb with Apache License 2.0 5 votes vote down vote up
/**
 * Reduces the fetch size and retries the query.  Returns true if the query succeeded, false if the root cause
 * of the exception does not indicate a frame size issue, if the frame size cannot be adjusted down any further,
 * or if the retried query fails for an unrelated reason.
 */
private boolean reduceFetchSize(Throwable reason) {
    if (!isAdaptiveException(reason) || --_remainingAdaptations == 0) {
        return false;
    }

    ExecutionInfo executionInfo = _delegate.getExecutionInfo();
    Statement statement = executionInfo.getStatement();
    PagingState pagingState = executionInfo.getPagingState();
    int fetchSize = statement.getFetchSize();

    while (fetchSize > MIN_FETCH_SIZE) {
        fetchSize = Math.max(fetchSize / 2, MIN_FETCH_SIZE);
        _log.debug("Retrying query at next page with fetch size {} due to {}", fetchSize, reason.getMessage());
        statement.setFetchSize(fetchSize);
        statement.setPagingState(pagingState);
        try {
            _delegate = _session.execute(statement);
            return true;
        } catch (Throwable t) {
            // Exit the adaptation loop if the exception isn't one where adapting further may help
            if (!isAdaptiveException(t) || --_remainingAdaptations == 0) {
                return false;
            }
        }
    }

    return false;
}
 
Example 6
Source File: TransferLogSerializationImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public Result<TransferLog> getAllTransferLogs(PagingState pagingState, int fetchSize ) {

    Statement query = QueryBuilder.select().all().from(TABLE_TRANSFER_LOG);

    query.setFetchSize( fetchSize );
    if ( pagingState != null ) {
        query.setPagingState( pagingState );
    }

    ResultSet rs = cassandraClient.getApplicationSession().execute( query );
    final PagingState newPagingState = rs.getExecutionInfo().getPagingState();

    final List<TransferLog> transferLogs = new ArrayList<>();
    int numReturned = rs.getAvailableWithoutFetching();
    for ( int i=0; i<numReturned; i++ ) {
        Row row = rs.one();
        TransferLog tlog = new TransferLog(
                row.getString( COLUMN_QUEUE_NAME ),
                row.getString( COLUMN_SOURCE_REGION ),
                row.getString( COLUMN_DEST_REGION ),
                row.getUUID( COLUMN_MESSAGE_ID ),
                row.getLong( COLUMN_TRANSFER_TIME ));
        transferLogs.add( tlog );
    }

    return new Result<TransferLog>() {

        @Override
        public PagingState getPagingState() {
            return newPagingState;
        }

        @Override
        public List<TransferLog> getEntities() {
            return transferLogs;
        }
    };
}