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

The following examples show how to use com.datastax.driver.core.Statement#setConsistencyLevel() . 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: CassandraAbstractModelDao.java    From iotplatform with Apache License 2.0 6 votes vote down vote up
protected ListenableFuture<List<D>> findListByStatementAsync(Statement statement) {
    if (statement != null) {
        statement.setConsistencyLevel(cluster.getDefaultReadConsistencyLevel());
        ResultSetFuture resultSetFuture = getSession().executeAsync(statement);
        return Futures.transform(resultSetFuture, new Function<ResultSet, List<D>>() {
            @Nullable
            @Override
            public List<D> apply(@Nullable ResultSet resultSet) {
                Result<E> result = getMapper().map(resultSet);
                if (result != null) {
                    List<E> entities = result.all();
                    return DaoUtil.convertDataList(entities);
                } else {
                    return Collections.emptyList();
                }
            }
        });
    }
    return Futures.immediateFuture(Collections.emptyList());
}
 
Example 2
Source File: CassandraAbstractModelDao.java    From iotplatform with Apache License 2.0 6 votes vote down vote up
protected ListenableFuture<D> findOneByStatementAsync(Statement statement) {
    if (statement != null) {
        statement.setConsistencyLevel(cluster.getDefaultReadConsistencyLevel());
        ResultSetFuture resultSetFuture = getSession().executeAsync(statement);
        return Futures.transform(resultSetFuture, new Function<ResultSet, D>() {
            @Nullable
            @Override
            public D apply(@Nullable ResultSet resultSet) {
                Result<E> result = getMapper().map(resultSet);
                if (result != null) {
                    E entity = result.one();
                    return DaoUtil.getData(entity);
                } else {
                    return null;
                }
            }
        });
    }
    return Futures.immediateFuture(null);
}
 
Example 3
Source File: CassandraStorage.java    From cassandra-reaper with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<RepairSegment> getSegmentsWithState(UUID runId, State segmentState) {
  Collection<RepairSegment> segments = Lists.newArrayList();

  Statement statement = null != getRepairSegmentsByRunIdAndStatePrepStmt
      ? getRepairSegmentsByRunIdAndStatePrepStmt.bind(runId, segmentState.ordinal())
      // legacy mode for Cassandra-2 backends
      : getRepairSegmentsByRunIdPrepStmt.bind(runId);

  if (State.STARTED == segmentState) {
    statement = statement.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
  }
  for (Row segmentRow : session.execute(statement)) {
    if (segmentRow.getInt("segment_state") == segmentState.ordinal()) {
      segments.add(createRepairSegmentFromRow(segmentRow));
    }
  }
  return segments;
}
 
Example 4
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 5
Source File: AuthorizationGrantDAOImpl.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public void removeForPlace(UUID placeId) {
   try(Context ctxt = removeForPlaceTimer.time()) {
      List<AuthorizationGrant> grants = findForPlace(placeId);
      Statement statement = QueryBuilder.delete().from(AUTHORIZATION_GRANT_TABLE)
            .where(QueryBuilder.in(Cols.ENTITY_ID, grants.stream().map(AuthorizationGrant::getEntityId).collect(Collectors.toList())))
            .and(QueryBuilder.eq(Cols.PLACE_ID, placeId));
      statement.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
      BatchStatement batch = new BatchStatement();
      batch.add(statement);
      batch.add(new BoundStatement(removeForPlace).bind(placeId));
      session.execute(batch);
   }
}
 
Example 6
Source File: CassandraAbstractDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
private ResultSet execute(Statement statement, ConsistencyLevel level) {
    log.debug("Execute cassandra statement {}", statement);
    if (statement.getConsistencyLevel() == null) {
        statement.setConsistencyLevel(level);
    }
    return getSession().execute(statement);
}
 
Example 7
Source File: CassandraAbstractDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
private ResultSetFuture executeAsync(Statement statement, ConsistencyLevel level) {
    log.debug("Execute cassandra async statement {}", statement);
    if (statement.getConsistencyLevel() == null) {
        statement.setConsistencyLevel(level);
    }
    return getSession().executeAsync(statement);
}
 
Example 8
Source File: CassandraAbstractModelDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
protected List<E> findListByStatement(Statement statement) {
    List<E> list = Collections.emptyList();
    if (statement != null) {
        statement.setConsistencyLevel(cluster.getDefaultReadConsistencyLevel());
        ResultSet resultSet = getSession().execute(statement);
        Result<E> result = getMapper().map(resultSet);
        if (result != null) {
            list = result.all();
        }
    }
    return list;
}
 
Example 9
Source File: CassandraAbstractModelDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
protected E findOneByStatement(Statement statement) {
    E object = null;
    if (statement != null) {
        statement.setConsistencyLevel(cluster.getDefaultReadConsistencyLevel());
        ResultSet resultSet = getSession().execute(statement);
        Result<E> result = getMapper().map(resultSet);
        if (result != null) {
            object = result.one();
        }
    }
    return object;
}
 
Example 10
Source File: CassandraAbstractModelDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
protected EntityResultSet<E> saveWithResult(E entity) {
    log.debug("Save entity {}", entity);
    if (entity.getId() == null) {
        entity.setId(UUIDs.timeBased());
    } else if (isDeleteOnSave()) {
        removeById(entity.getId());
    }
    Statement saveStatement = getSaveQuery(entity);
    saveStatement.setConsistencyLevel(cluster.getDefaultWriteConsistencyLevel());
    ResultSet resultSet = executeWrite(saveStatement);
    return new EntityResultSet<>(resultSet, entity);
}
 
Example 11
Source File: Session.java    From glowroot with Apache License 2.0 5 votes vote down vote up
public ListenableFuture<?> writeAsync(Statement statement) throws Exception {
    if (statement.getConsistencyLevel() == null && writeConsistencyLevel != null) {
        statement.setConsistencyLevel(writeConsistencyLevel);
    }
    return throttleWrite(() -> {
        // for now, need to record metrics in the same method because CassandraWriteMetrics
        // relies on some thread locals
        cassandraWriteMetrics.recordMetrics(statement);
        return wrappedSession.executeAsync(statement);
    });
}