Java Code Examples for org.apache.cassandra.service.StorageProxy#getRangeSlice()

The following examples show how to use org.apache.cassandra.service.StorageProxy#getRangeSlice() . 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: SelectStatement.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private ResultMessage.Rows execute(Pageable command, QueryOptions options, int limit, long now, QueryState state) throws RequestValidationException, RequestExecutionException
{
    List<Row> rows;
    if (command == null)
    {
        rows = Collections.<Row>emptyList();
    }
    else
    {
        rows = command instanceof Pageable.ReadCommands
             ? StorageProxy.read(((Pageable.ReadCommands)command).commands, options.getConsistency(), state.getClientState())
             : StorageProxy.getRangeSlice((RangeSliceCommand)command, options.getConsistency());
    }

    return processResults(rows, options, limit, now);
}
 
Example 2
Source File: RangeSliceQueryPager.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
protected List<Row> queryNextPage(int pageSize, ConsistencyLevel consistencyLevel, boolean localQuery)
throws RequestExecutionException
{
    SliceQueryFilter sf = (SliceQueryFilter)columnFilter;
    AbstractBounds<RowPosition> keyRange = lastReturnedKey == null ? command.keyRange : makeIncludingKeyBounds(lastReturnedKey);
    Composite start = lastReturnedName == null ? sf.start() : lastReturnedName;
    PagedRangeCommand pageCmd = new PagedRangeCommand(command.keyspace,
                                                      command.columnFamily,
                                                      command.timestamp,
                                                      keyRange,
                                                      sf,
                                                      start,
                                                      sf.finish(),
                                                      command.rowFilter,
                                                      pageSize,
                                                      command.countCQL3Rows);

    return localQuery
         ? pageCmd.executeLocally()
         : StorageProxy.getRangeSlice(pageCmd, consistencyLevel);
}
 
Example 3
Source File: RangeNamesQueryPager.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
protected List<Row> queryNextPage(int pageSize, ConsistencyLevel consistencyLevel, boolean localQuery)
throws RequestExecutionException
{
    AbstractRangeCommand pageCmd = command.withUpdatedLimit(pageSize);
    if (lastReturnedKey != null)
        pageCmd = pageCmd.forSubRange(makeExcludingKeyBounds(lastReturnedKey));

    return localQuery
         ? pageCmd.executeLocally()
         : StorageProxy.getRangeSlice(pageCmd, consistencyLevel);
}
 
Example 4
Source File: CassandraEmbeddedKeyColumnValueStore.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
/**
 * Create a RangeSliceCommand and run it against the StorageProxy.
 * <p>
 * To match the behavior of the standard Cassandra thrift API endpoint, the
 * {@code nowMillis} argument should be the number of milliseconds since the
 * UNIX Epoch (e.g. System.currentTimeMillis() or equivalent obtained
 * through a {@link TimestampProvider}). This is per
 * {@link org.apache.cassandra.thrift.CassandraServer#get_range_slices(ColumnParent, SlicePredicate, KeyRange, ConsistencyLevel)},
 * which passes the server's System.currentTimeMillis() to the
 * {@code RangeSliceCommand} constructor.
 */
private List<Row> getKeySlice(Token start,
                              Token end,
                              @Nullable SliceQuery sliceQuery,
                              int pageSize,
                              long nowMillis) throws BackendException {
    IPartitioner partitioner = StorageService.getPartitioner();

    SliceRange columnSlice = new SliceRange();
    if (sliceQuery == null) {
        columnSlice.setStart(ArrayUtils.EMPTY_BYTE_ARRAY)
                .setFinish(ArrayUtils.EMPTY_BYTE_ARRAY)
                .setCount(5);
    } else {
        columnSlice.setStart(sliceQuery.getSliceStart().asByteBuffer())
                .setFinish(sliceQuery.getSliceEnd().asByteBuffer())
                .setCount(sliceQuery.hasLimit() ? sliceQuery.getLimit() : Integer.MAX_VALUE);
    }
    /* Note: we need to fetch columns for each row as well to remove "range ghosts" */
    SlicePredicate predicate = new SlicePredicate().setSlice_range(columnSlice);

    // DAVID CASSANDRA
    // Old cassandra code did not use partitioner anyway in this call...so new code removed it as a parmaeter
    // RowPosition startPosition = start.minKeyBound(partitioner);
    RowPosition startPosition = start.minKeyBound();
    // DAVID CASSANDRA
    // RowPosition endPosition = end.minKeyBound(partitioner);
    RowPosition endPosition = end.minKeyBound();

    List<Row> rows;

    try {
        CFMetaData cfm = Schema.instance.getCFMetaData(keyspace, columnFamily);
        IDiskAtomFilter filter = ThriftValidation.asIFilter(predicate, cfm, null);

        RangeSliceCommand cmd = new RangeSliceCommand(keyspace, columnFamily, nowMillis, filter, new Bounds<RowPosition>(startPosition, endPosition), pageSize);

        rows = StorageProxy.getRangeSlice(cmd, ConsistencyLevel.QUORUM);
    } catch (Exception e) {
        throw new PermanentBackendException(e);
    }

    return rows;
}
 
Example 5
Source File: QueryProcessor.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
private static List<org.apache.cassandra.db.Row> multiRangeSlice(CFMetaData metadata, SelectStatement select, List<ByteBuffer> variables, long now)
throws ReadTimeoutException, UnavailableException, InvalidRequestException
{
    IPartitioner p = StorageService.getPartitioner();

    AbstractType<?> keyType = Schema.instance.getCFMetaData(metadata.ksName, select.getColumnFamily()).getKeyValidator();

    ByteBuffer startKeyBytes = (select.getKeyStart() != null)
                               ? select.getKeyStart().getByteBuffer(keyType,variables)
                               : null;

    ByteBuffer finishKeyBytes = (select.getKeyFinish() != null)
                                ? select.getKeyFinish().getByteBuffer(keyType,variables)
                                : null;

    RowPosition startKey = RowPosition.ForKey.get(startKeyBytes, p), finishKey = RowPosition.ForKey.get(finishKeyBytes, p);
    if (startKey.compareTo(finishKey) > 0 && !finishKey.isMinimum(p))
    {
        if (p instanceof RandomPartitioner)
            throw new InvalidRequestException("Start key sorts after end key. This is not allowed; you probably should not specify end key at all, under RandomPartitioner");
        else
            throw new InvalidRequestException("Start key must sort before (or equal to) finish key in your partitioner!");
    }
    AbstractBounds<RowPosition> bounds = new Bounds<RowPosition>(startKey, finishKey);

    IDiskAtomFilter columnFilter = filterFromSelect(select, metadata, variables);
    validateFilter(metadata, columnFilter);

    List<Relation> columnRelations = select.getColumnRelations();
    List<IndexExpression> expressions = new ArrayList<IndexExpression>(columnRelations.size());
    for (Relation columnRelation : columnRelations)
    {
        // Left and right side of relational expression encoded according to comparator/validator.
        ByteBuffer entity = columnRelation.getEntity().getByteBuffer(metadata.comparator.asAbstractType(), variables);
        ByteBuffer value = columnRelation.getValue().getByteBuffer(metadata.getValueValidator(metadata.comparator.cellFromByteBuffer(entity)), variables);

        expressions.add(new IndexExpression(entity,
                                            Operator.valueOf(columnRelation.operator().name()),
                                            value));
    }

    int limit = select.isKeyRange() && select.getKeyStart() != null
              ? select.getNumRecords() + 1
              : select.getNumRecords();

    List<org.apache.cassandra.db.Row> rows = StorageProxy.getRangeSlice(new RangeSliceCommand(metadata.ksName,
                                                                                              select.getColumnFamily(),
                                                                                              now,
                                                                                              columnFilter,
                                                                                              bounds,
                                                                                              expressions,
                                                                                              limit),
                                                                        select.getConsistencyLevel());

    // if start key was set and relation was "greater than"
    if (select.getKeyStart() != null && !select.includeStartKey() && !rows.isEmpty())
    {
        if (rows.get(0).key.getKey().equals(startKeyBytes))
            rows.remove(0);
    }

    // if finish key was set and relation was "less than"
    if (select.getKeyFinish() != null && !select.includeFinishKey() && !rows.isEmpty())
    {
        int lastIndex = rows.size() - 1;
        if (rows.get(lastIndex).key.getKey().equals(finishKeyBytes))
            rows.remove(lastIndex);
    }

    return rows.subList(0, select.getNumRecords() < rows.size() ? select.getNumRecords() : rows.size());
}