org.apache.cassandra.db.filter.SliceQueryFilter Java Examples

The following examples show how to use org.apache.cassandra.db.filter.SliceQueryFilter. 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: 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 #2
Source File: QueryPagers.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Convenience method that count (live) cells/rows for a given slice of a row, but page underneath.
 */
public static int countPaged(String keyspace,
                             String columnFamily,
                             ByteBuffer key,
                             SliceQueryFilter filter,
                             ConsistencyLevel consistencyLevel,
                             ClientState cState,
                             final int pageSize,
                             long now) throws RequestValidationException, RequestExecutionException
{
    SliceFromReadCommand command = new SliceFromReadCommand(keyspace, key, columnFamily, now, filter);
    final SliceQueryPager pager = new SliceQueryPager(command, consistencyLevel, cState, false);

    ColumnCounter counter = filter.columnCounter(Schema.instance.getCFMetaData(keyspace, columnFamily).comparator, now);
    while (!pager.isExhausted())
    {
        List<Row> next = pager.fetchPage(pageSize);
        if (!next.isEmpty())
            counter.countAll(next.get(0).cf);
    }
    return counter.live();
}
 
Example #3
Source File: SliceQueryPager.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 RequestValidationException, RequestExecutionException
{
    // For some queries, such as a DISTINCT query on static columns, the limit for slice queries will be lower
    // than the page size (in the static example, it will be 1).  We use the min here to ensure we don't fetch
    // more rows than we're supposed to.  See CASSANDRA-8108 for more details.
    SliceQueryFilter filter = command.filter.withUpdatedCount(Math.min(command.filter.count, pageSize));
    if (lastReturned != null)
        filter = filter.withUpdatedStart(lastReturned, cfm.comparator);

    logger.debug("Querying next page of slice query; new filter: {}", filter);
    ReadCommand pageCmd = command.withUpdatedFilter(filter);
    return localQuery
         ? Collections.singletonList(pageCmd.getRow(Keyspace.open(command.ksName)))
         : StorageProxy.read(Collections.singletonList(pageCmd), consistencyLevel, cstate);
}
 
Example #4
Source File: CompositesSearcher.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private Composite makePrefix(CompositesIndex index, ByteBuffer key, ExtendedFilter filter, boolean isStart)
{
    if (key.remaining() == 0)
        return Composites.EMPTY;

    Composite prefix;
    IDiskAtomFilter columnFilter = filter.columnFilter(key);
    if (columnFilter instanceof SliceQueryFilter)
    {
        SliceQueryFilter sqf = (SliceQueryFilter)columnFilter;
        Composite columnName = isStart ? sqf.start() : sqf.finish();
        prefix = columnName.isEmpty() ? index.getIndexComparator().make(key) : index.makeIndexColumnPrefix(key, columnName);
    }
    else
    {
        prefix = index.getIndexComparator().make(key);
    }
    return isStart ? prefix.start() : prefix.end();
}
 
Example #5
Source File: SliceFromReadCommand.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Override
public ReadCommand maybeGenerateRetryCommand(RowDataResolver resolver, Row row)
{
    int maxLiveColumns = resolver.getMaxLiveCount();

    int count = filter.count;
    // We generate a retry if at least one node reply with count live columns but after merge we have less
    // than the total number of column we are interested in (which may be < count on a retry).
    // So in particular, if no host returned count live columns, we know it's not a short read.
    if (maxLiveColumns < count)
        return null;

    int liveCountInRow = row == null || row.cf == null ? 0 : filter.getLiveCount(row.cf, timestamp);
    if (liveCountInRow < getOriginalRequestedCount())
    {
        // We asked t (= count) live columns and got l (=liveCountInRow) ones.
        // From that, we can estimate that on this row, for x requested
        // columns, only l/t end up live after reconciliation. So for next
        // round we want to ask x column so that x * (l/t) == t, i.e. x = t^2/l.
        int retryCount = liveCountInRow == 0 ? count + 1 : ((count * count) / liveCountInRow) + 1;
        SliceQueryFilter newFilter = filter.withUpdatedCount(retryCount);
        return new RetriedSliceFromReadCommand(ksName, key, cfName, timestamp, newFilter, getOriginalRequestedCount());
    }

    return null;
}
 
Example #6
Source File: ColumnFamilyStoreTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void findRowGetSlicesAndAssertColsFound(ColumnFamilyStore cfs, SliceQueryFilter filter, String rowKey,
        String... colNames)
{
    List<Row> rows = cfs.getRangeSlice(new Bounds<RowPosition>(rp(rowKey), rp(rowKey)),
                                       null,
                                       filter,
                                       Integer.MAX_VALUE,
                                       System.currentTimeMillis(),
                                       false,
                                       false);
    assertSame("unexpected number of rows ", 1, rows.size());
    Row row = rows.get(0);
    Collection<Cell> cols = !filter.isReversed() ? row.cf.getSortedColumns() : row.cf.getReverseSortedColumns();
    // printRow(cfs, new String(row.key.key.array()), cols);
    String[] returnedColsNames = Iterables.toArray(Iterables.transform(cols, new Function<Cell, String>()
    {
        public String apply(Cell arg0)
        {
            return Util.string(arg0.name().toByteBuffer());
        }
    }), String.class);

    assertTrue(
            "Columns did not match. Expected: " + Arrays.toString(colNames) + " but got:"
                    + Arrays.toString(returnedColsNames), Arrays.equals(colNames, returnedColsNames));
    int i = 0;
    for (Cell col : cols)
    {
        assertEquals(colNames[i++], Util.string(col.name().toByteBuffer()));
    }
}
 
Example #7
Source File: Util.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static List<Row> getRangeSlice(ColumnFamilyStore cfs, ByteBuffer superColumn)
{
    IDiskAtomFilter filter = superColumn == null
                           ? new IdentityQueryFilter()
                           : new SliceQueryFilter(SuperColumns.startOf(superColumn), SuperColumns.endOf(superColumn), false, Integer.MAX_VALUE);

    Token min = StorageService.getPartitioner().getMinimumToken();
    return cfs.getRangeSlice(new Bounds<Token>(min, min).toRowBounds(), null, filter, 10000);
}
 
Example #8
Source File: ColumnFamilyStore.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Allows generic range paging with the slice column filter.
 * Typically, suppose we have rows A, B, C ... Z having each some columns in [1, 100].
 * And suppose we want to page through the query that for all rows returns the columns
 * within [25, 75]. For that, we need to be able to do a range slice starting at (row r, column c)
 * and ending at (row Z, column 75), *but* that only return columns in [25, 75].
 * That is what this method allows. The columnRange is the "window" of  columns we are interested
 * in each row, and columnStart (resp. columnEnd) is the start (resp. end) for the first
 * (resp. last) requested row.
 */
public ExtendedFilter makeExtendedFilter(AbstractBounds<RowPosition> keyRange,
                                         SliceQueryFilter columnRange,
                                         Composite columnStart,
                                         Composite columnStop,
                                         List<IndexExpression> rowFilter,
                                         int maxResults,
                                         boolean countCQL3Rows,
                                         long now)
{
    DataRange dataRange = new DataRange.Paging(keyRange, columnRange, columnStart, columnStop, metadata.comparator);
    return ExtendedFilter.create(this, dataRange, rowFilter, maxResults, countCQL3Rows, now);
}
 
Example #9
Source File: AbstractCType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
protected AbstractCType(boolean isByteOrderComparable)
{
    reverseComparator = new Comparator<Composite>()
    {
        public int compare(Composite c1, Composite c2)
        {
            return AbstractCType.this.compare(c2, c1);
        }
    };
    indexComparator = new Comparator<IndexInfo>()
    {
        public int compare(IndexInfo o1, IndexInfo o2)
        {
            return AbstractCType.this.compare(o1.lastName, o2.lastName);
        }
    };
    indexReverseComparator = new Comparator<IndexInfo>()
    {
        public int compare(IndexInfo o1, IndexInfo o2)
        {
            return AbstractCType.this.compare(o1.firstName, o2.firstName);
        }
    };

    serializer = new Serializer(this);

    indexSerializer = new IndexInfo.Serializer(this);
    sliceSerializer = new ColumnSlice.Serializer(this);
    sliceQueryFilterSerializer = new SliceQueryFilter.Serializer(this);
    deletionInfoSerializer = new DeletionInfo.Serializer(this);
    rangeTombstoneSerializer = new RangeTombstone.Serializer(this);
    rowIndexEntrySerializer = new RowIndexEntry.Serializer(this);
    this.isByteOrderComparable = isByteOrderComparable;
}
 
Example #10
Source File: SliceFromReadCommand.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ReadCommand deserialize(DataInput in, int version) throws IOException
{
    boolean isDigest = in.readBoolean();
    String keyspaceName = in.readUTF();
    ByteBuffer key = ByteBufferUtil.readWithShortLength(in);
    String cfName = in.readUTF();
    long timestamp = in.readLong();
    CFMetaData metadata = Schema.instance.getCFMetaData(keyspaceName, cfName);
    SliceQueryFilter filter = metadata.comparator.sliceQueryFilterSerializer().deserialize(in, version);
    return new SliceFromReadCommand(keyspaceName, key, cfName, timestamp, filter).setIsDigestQuery(isDigest);
}
 
Example #11
Source File: ReadCommand.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static ReadCommand create(String ksName, ByteBuffer key, String cfName, long timestamp, IDiskAtomFilter filter)
{
    if (filter instanceof SliceQueryFilter)
        return new SliceFromReadCommand(ksName, key, cfName, timestamp, (SliceQueryFilter)filter);
    else
        return new SliceByNamesReadCommand(ksName, key, cfName, timestamp, (NamesQueryFilter)filter);
}
 
Example #12
Source File: CassandraServer.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public IDiskAtomFilter readFilter()
{
    return expected == null || expected.isEmpty()
         ? new SliceQueryFilter(ColumnSlice.ALL_COLUMNS_ARRAY, false, 1)
         : new NamesQueryFilter(ImmutableSortedSet.copyOf(expected.getComparator(), expected.getColumnNames()));
}
 
Example #13
Source File: SliceFromReadCommand.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public SliceFromReadCommand withUpdatedFilter(SliceQueryFilter newFilter)
{
    return new SliceFromReadCommand(ksName, key, cfName, timestamp, newFilter);
}
 
Example #14
Source File: ColumnFamilyStoreTest.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMultiRangeSomeEmptyNoIndex() throws Throwable
{
    // in order not to change thrift interfaces at this stage we build SliceQueryFilter
    // directly instead of using QueryFilter to build it for us
    ColumnSlice[] ranges = new ColumnSlice[] {
            new ColumnSlice(Composites.EMPTY, cellname("colA")),
            new ColumnSlice(cellname("colC"), cellname("colE")),
            new ColumnSlice(cellname("colF"), cellname("colF")),
            new ColumnSlice(cellname("colG"), cellname("colG")),
            new ColumnSlice(cellname("colI"), Composites.EMPTY) };

    ColumnSlice[] rangesReversed = new ColumnSlice[] {
            new ColumnSlice(Composites.EMPTY, cellname("colI")),
            new ColumnSlice(cellname("colG"), cellname("colG")),
            new ColumnSlice(cellname("colF"), cellname("colF")),
            new ColumnSlice(cellname("colE"), cellname("colC")),
            new ColumnSlice(cellname("colA"), Composites.EMPTY) };

    String tableName = "Keyspace1";
    String cfName = "Standard1";
    Keyspace table = Keyspace.open(tableName);
    ColumnFamilyStore cfs = table.getColumnFamilyStore(cfName);
    cfs.clearUnsafe();

    String[] letters = new String[] { "a", "b", "c", "d", "i" };
    Cell[] cols = new Cell[letters.length];
    for (int i = 0; i < cols.length; i++)
    {
        cols[i] = new BufferCell(cellname("col" + letters[i].toUpperCase()),
                ByteBuffer.wrap(new byte[1]), 1);
    }

    putColsStandard(cfs, dk("a"), cols);

    cfs.forceBlockingFlush();

    SliceQueryFilter multiRangeForward = new SliceQueryFilter(ranges, false, 100);
    SliceQueryFilter multiRangeForwardWithCounting = new SliceQueryFilter(ranges, false, 3);
    SliceQueryFilter multiRangeReverse = new SliceQueryFilter(rangesReversed, true, 100);
    SliceQueryFilter multiRangeReverseWithCounting = new SliceQueryFilter(rangesReversed, true, 3);

    findRowGetSlicesAndAssertColsFound(cfs, multiRangeForward, "a", "colA", "colC", "colD", "colI");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeForwardWithCounting, "a", "colA", "colC", "colD");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeReverse, "a", "colI", "colD", "colC", "colA");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeReverseWithCounting, "a", "colI", "colD", "colC");
}
 
Example #15
Source File: ColumnFamilyStoreTest.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMultiRangeSomeEmptyIndexed() throws Throwable
{
    // in order not to change thrift interfaces at this stage we build SliceQueryFilter
    // directly instead of using QueryFilter to build it for us
    ColumnSlice[] ranges = new ColumnSlice[] {
            new ColumnSlice(Composites.EMPTY, cellname("colA")),
            new ColumnSlice(cellname("colC"), cellname("colE")),
            new ColumnSlice(cellname("colF"), cellname("colF")),
            new ColumnSlice(cellname("colG"), cellname("colG")),
            new ColumnSlice(cellname("colI"), Composites.EMPTY) };

    ColumnSlice[] rangesReversed = new ColumnSlice[] {
            new ColumnSlice(Composites.EMPTY,  cellname("colI")),
            new ColumnSlice(cellname("colG"), cellname("colG")),
            new ColumnSlice(cellname("colF"), cellname("colF")),
            new ColumnSlice(cellname("colE"), cellname("colC")),
            new ColumnSlice(cellname("colA"), Composites.EMPTY) };

    String tableName = "Keyspace1";
    String cfName = "Standard1";
    Keyspace table = Keyspace.open(tableName);
    ColumnFamilyStore cfs = table.getColumnFamilyStore(cfName);
    cfs.clearUnsafe();

    String[] letters = new String[] { "a", "b", "c", "d", "i" };
    Cell[] cols = new Cell[letters.length];
    for (int i = 0; i < cols.length; i++)
    {
        cols[i] = new BufferCell(cellname("col" + letters[i].toUpperCase()),
                ByteBuffer.wrap(new byte[1366]), 1);
    }

    putColsStandard(cfs, dk("a"), cols);

    cfs.forceBlockingFlush();

    SliceQueryFilter multiRangeForward = new SliceQueryFilter(ranges, false, 100);
    SliceQueryFilter multiRangeForwardWithCounting = new SliceQueryFilter(ranges, false, 3);
    SliceQueryFilter multiRangeReverse = new SliceQueryFilter(rangesReversed, true, 100);
    SliceQueryFilter multiRangeReverseWithCounting = new SliceQueryFilter(rangesReversed, true, 3);

    findRowGetSlicesAndAssertColsFound(cfs, multiRangeForward, "a", "colA", "colC", "colD", "colI");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeForwardWithCounting, "a", "colA", "colC", "colD");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeReverse, "a", "colI", "colD", "colC", "colA");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeReverseWithCounting, "a", "colI", "colD", "colC");
}
 
Example #16
Source File: ColumnFamilyStoreTest.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMultiRangeContiguousNoIndex() throws Throwable
{
    // in order not to change thrift interfaces at this stage we build SliceQueryFilter
    // directly instead of using QueryFilter to build it for us
    ColumnSlice[] ranges = new ColumnSlice[] {
            new ColumnSlice(Composites.EMPTY, cellname("colA")),
            new ColumnSlice(cellname("colC"), cellname("colE")),
            new ColumnSlice(cellname("colF"), cellname("colF")),
            new ColumnSlice(cellname("colG"), cellname("colG")),
            new ColumnSlice(cellname("colI"), Composites.EMPTY) };

    ColumnSlice[] rangesReversed = new ColumnSlice[] {
            new ColumnSlice(Composites.EMPTY, cellname("colI")),
            new ColumnSlice(cellname("colG"), cellname("colG")),
            new ColumnSlice(cellname("colF"), cellname("colF")),
            new ColumnSlice(cellname("colE"), cellname("colC")),
            new ColumnSlice(cellname("colA"), Composites.EMPTY) };

    String tableName = "Keyspace1";
    String cfName = "Standard1";
    Keyspace table = Keyspace.open(tableName);
    ColumnFamilyStore cfs = table.getColumnFamilyStore(cfName);
    cfs.clearUnsafe();

    String[] letters = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i" };
    Cell[] cols = new Cell[letters.length];
    for (int i = 0; i < cols.length; i++)
    {
        cols[i] = new BufferCell(cellname("col" + letters[i].toUpperCase()),
                ByteBuffer.wrap(new byte[1]), 1);
    }

    putColsStandard(cfs, dk("a"), cols);

    cfs.forceBlockingFlush();

    SliceQueryFilter multiRangeForward = new SliceQueryFilter(ranges, false, 100);
    SliceQueryFilter multiRangeForwardWithCounting = new SliceQueryFilter(ranges, false, 3);
    SliceQueryFilter multiRangeReverse = new SliceQueryFilter(rangesReversed, true, 100);
    SliceQueryFilter multiRangeReverseWithCounting = new SliceQueryFilter(rangesReversed, true, 3);

    findRowGetSlicesAndAssertColsFound(cfs, multiRangeForward, "a", "colA", "colC", "colD", "colE", "colF", "colG", "colI");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeForwardWithCounting, "a", "colA", "colC", "colD");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeReverse, "a", "colI", "colG", "colF", "colE", "colD", "colC", "colA");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeReverseWithCounting, "a", "colI", "colG", "colF");

}
 
Example #17
Source File: ColumnFamilyStoreTest.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMultiRangeContiguousIndexed() throws Throwable
{
    // in order not to change thrift interfaces at this stage we build SliceQueryFilter
    // directly instead of using QueryFilter to build it for us
    ColumnSlice[] ranges = new ColumnSlice[] {
            new ColumnSlice(Composites.EMPTY, cellname("colA")),
            new ColumnSlice(cellname("colC"), cellname("colE")),
            new ColumnSlice(cellname("colF"), cellname("colF")),
            new ColumnSlice(cellname("colG"), cellname("colG")),
            new ColumnSlice(cellname("colI"), Composites.EMPTY) };

    ColumnSlice[] rangesReversed = new ColumnSlice[] {
            new ColumnSlice(Composites.EMPTY, cellname("colI")),
            new ColumnSlice(cellname("colG"), cellname("colG")),
            new ColumnSlice(cellname("colF"), cellname("colF")),
            new ColumnSlice(cellname("colE"), cellname("colC")),
            new ColumnSlice(cellname("colA"), Composites.EMPTY) };

    String tableName = "Keyspace1";
    String cfName = "Standard1";
    Keyspace table = Keyspace.open(tableName);
    ColumnFamilyStore cfs = table.getColumnFamilyStore(cfName);
    cfs.clearUnsafe();

    String[] letters = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i" };
    Cell[] cols = new Cell[letters.length];
    for (int i = 0; i < cols.length; i++)
    {
        cols[i] = new BufferCell(cellname("col" + letters[i].toUpperCase()),
                ByteBuffer.wrap(new byte[1366]), 1);
    }

    putColsStandard(cfs, dk("a"), cols);

    cfs.forceBlockingFlush();

    SliceQueryFilter multiRangeForward = new SliceQueryFilter(ranges, false, 100);
    SliceQueryFilter multiRangeForwardWithCounting = new SliceQueryFilter(ranges, false, 3);
    SliceQueryFilter multiRangeReverse = new SliceQueryFilter(rangesReversed, true, 100);
    SliceQueryFilter multiRangeReverseWithCounting = new SliceQueryFilter(rangesReversed, true, 3);

    findRowGetSlicesAndAssertColsFound(cfs, multiRangeForward, "a", "colA", "colC", "colD", "colE", "colF", "colG", "colI");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeForwardWithCounting, "a", "colA", "colC", "colD");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeReverse, "a", "colI", "colG", "colF", "colE", "colD", "colC", "colA");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeReverseWithCounting, "a", "colI", "colG", "colF");

}
 
Example #18
Source File: ColumnFamilyStoreTest.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMultiRangeIndexed() throws Throwable
{
    // in order not to change thrift interfaces at this stage we build SliceQueryFilter
    // directly instead of using QueryFilter to build it for us
    ColumnSlice[] ranges = new ColumnSlice[] {
            new ColumnSlice(Composites.EMPTY, cellname("colA")),
            new ColumnSlice(cellname("colC"), cellname("colE")),
            new ColumnSlice(cellname("colG"), cellname("colG")),
            new ColumnSlice(cellname("colI"), Composites.EMPTY) };

    ColumnSlice[] rangesReversed = new ColumnSlice[] {
            new ColumnSlice(Composites.EMPTY, cellname("colI")),
            new ColumnSlice(cellname("colG"), cellname("colG")),
            new ColumnSlice(cellname("colE"), cellname("colC")),
            new ColumnSlice(cellname("colA"), Composites.EMPTY) };

    String keyspaceName = "Keyspace1";
    String cfName = "Standard1";
    Keyspace keyspace = Keyspace.open(keyspaceName);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(cfName);
    cfs.clearUnsafe();

    String[] letters = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i" };
    Cell[] cols = new Cell[letters.length];
    for (int i = 0; i < cols.length; i++)
    {
        cols[i] = new BufferCell(cellname("col" + letters[i].toUpperCase()),
                // use 1366 so that three cols make an index segment
                ByteBuffer.wrap(new byte[1366]), 1);
    }

    putColsStandard(cfs, dk("a"), cols);

    cfs.forceBlockingFlush();

    // this setup should generate the following row (assuming indexes are of 4Kb each):
    // [colA, colB, colC, colD, colE, colF, colG, colH, colI]
    // indexed as:
    // index0 [colA, colC]
    // index1 [colD, colF]
    // index2 [colG, colI]
    // and we're looking for the ranges:
    // range0 [____, colA]
    // range1 [colC, colE]
    // range2 [colG, ColG]
    // range3 [colI, ____]

    SliceQueryFilter multiRangeForward = new SliceQueryFilter(ranges, false, 100);
    SliceQueryFilter multiRangeForwardWithCounting = new SliceQueryFilter(ranges, false, 3);
    SliceQueryFilter multiRangeReverse = new SliceQueryFilter(rangesReversed, true, 100);
    SliceQueryFilter multiRangeReverseWithCounting = new SliceQueryFilter(rangesReversed, true, 3);

    findRowGetSlicesAndAssertColsFound(cfs, multiRangeForward, "a", "colA", "colC", "colD", "colE", "colG", "colI");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeForwardWithCounting, "a", "colA", "colC", "colD");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeReverse, "a", "colI", "colG", "colE", "colD", "colC", "colA");
    findRowGetSlicesAndAssertColsFound(cfs, multiRangeReverseWithCounting, "a", "colI", "colG", "colE");

}
 
Example #19
Source File: RangeTombstoneTest.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@Test
public void rangeTombstoneFilteringTest() throws Exception
{
    CompactionManager.instance.disableAutoCompaction();
    Keyspace keyspace = Keyspace.open(KSNAME);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CFNAME);

    // Inserting data
    String key = "k111";
    Mutation rm;
    ColumnFamily cf;

    rm = new Mutation(KSNAME, ByteBufferUtil.bytes(key));
    for (int i = 0; i < 40; i += 2)
        add(rm, i, 0);
    rm.apply();

    rm = new Mutation(KSNAME, ByteBufferUtil.bytes(key));
    cf = rm.addOrGet(CFNAME);
    delete(cf, 5, 10, 1);
    rm.apply();

    rm = new Mutation(KSNAME, ByteBufferUtil.bytes(key));
    cf = rm.addOrGet(CFNAME);
    delete(cf, 15, 20, 2);
    rm.apply();

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(11), b(14), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    Collection<RangeTombstone> rt = rangeTombstones(cf);
    assertEquals(0, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(11), b(15), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(1, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(20), b(25), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(1, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(12), b(25), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(1, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(25), b(35), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(0, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(1), b(40), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(2, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(7), b(17), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(2, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(5), b(20), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(2, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(5), b(15), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(2, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(1), b(2), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(0, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(1), b(5), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(1, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(1), b(10), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(1, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(5), b(6), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(1, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(17), b(20), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(1, rt.size());

    cf = cfs.getColumnFamily(QueryFilter.getSliceFilter(dk(key), CFNAME, b(17), b(18), false, Integer.MAX_VALUE, System.currentTimeMillis()));
    rt = rangeTombstones(cf);
    assertEquals(1, rt.size());

    ColumnSlice[] slices = new ColumnSlice[]{new ColumnSlice( b(1), b(10)), new ColumnSlice( b(16), b(20))};
    IDiskAtomFilter sqf = new SliceQueryFilter(slices, false, Integer.MAX_VALUE);
    cf = cfs.getColumnFamily( new QueryFilter(dk(key), CFNAME, sqf, System.currentTimeMillis()) );
    rt = rangeTombstones(cf);
    assertEquals(2, rt.size());
}
 
Example #20
Source File: CassandraServer.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public List<ColumnOrSuperColumn> get_multi_slice(MultiSliceRequest request)
        throws InvalidRequestException, UnavailableException, TimedOutException
{
    if (startSessionIfRequested())
    {
        Map<String, String> traceParameters = ImmutableMap.of("key", ByteBufferUtil.bytesToHex(request.key),
                                                              "column_parent", request.column_parent.toString(),
                                                              "consistency_level", request.consistency_level.name(),
                                                              "count", String.valueOf(request.count),
                                                              "column_slices", request.column_slices.toString());
        Tracing.instance.begin("get_multi_slice", traceParameters);
    }
    else
    {
        logger.debug("get_multi_slice");
    }
    try
    {
        ClientState cState = state();
        String keyspace = cState.getKeyspace();
        state().hasColumnFamilyAccess(keyspace, request.getColumn_parent().column_family, Permission.SELECT);
        CFMetaData metadata = ThriftValidation.validateColumnFamily(keyspace, request.getColumn_parent().column_family);
        if (metadata.cfType == ColumnFamilyType.Super)
            throw new org.apache.cassandra.exceptions.InvalidRequestException("get_multi_slice does not support super columns");
        ThriftValidation.validateColumnParent(metadata, request.getColumn_parent());
        org.apache.cassandra.db.ConsistencyLevel consistencyLevel = ThriftConversion.fromThrift(request.getConsistency_level());
        consistencyLevel.validateForRead(keyspace);
        List<ReadCommand> commands = new ArrayList<>(1);
        ColumnSlice[] slices = new ColumnSlice[request.getColumn_slices().size()];
        for (int i = 0 ; i < request.getColumn_slices().size() ; i++)
        {
            fixOptionalSliceParameters(request.getColumn_slices().get(i));
            Composite start = metadata.comparator.fromByteBuffer(request.getColumn_slices().get(i).start);
            Composite finish = metadata.comparator.fromByteBuffer(request.getColumn_slices().get(i).finish);
            if (!start.isEmpty() && !finish.isEmpty())
            {
                int compare = metadata.comparator.compare(start, finish);
                if (!request.reversed && compare > 0)
                    throw new InvalidRequestException(String.format("Column slice at index %d had start greater than finish", i));
                else if (request.reversed && compare < 0)
                    throw new InvalidRequestException(String.format("Reversed column slice at index %d had start less than finish", i));
            }
            slices[i] = new ColumnSlice(start, finish);
        }

        ColumnSlice[] deoverlapped = ColumnSlice.deoverlapSlices(slices, request.reversed ? metadata.comparator.reverseComparator() : metadata.comparator);
        SliceQueryFilter filter = new SliceQueryFilter(deoverlapped, request.reversed, request.count);
        ThriftValidation.validateKey(metadata, request.key);
        commands.add(ReadCommand.create(keyspace, request.key, request.column_parent.getColumn_family(), System.currentTimeMillis(), filter));
        return getSlice(commands, request.column_parent.isSetSuper_column(), consistencyLevel, cState).entrySet().iterator().next().getValue();
    }
    catch (RequestValidationException e)
    {
        throw ThriftConversion.toThrift(e);
    }
    finally
    {
        Tracing.instance.stopSession();
    }
}
 
Example #21
Source File: RetriedSliceFromReadCommand.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public RetriedSliceFromReadCommand(String keyspaceName, ByteBuffer key, String cfName, long timestamp, SliceQueryFilter filter, int originalCount)
{
    super(keyspaceName, key, cfName, timestamp, filter);
    this.originalCount = originalCount;
}
 
Example #22
Source File: CassandraEmbeddedKeyColumnValueStore.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
@Override
public EntryList getSlice(KeySliceQuery query, StoreTransaction txh) throws BackendException {

    /**
     * This timestamp mimics the timestamp used by
     * {@link org.apache.cassandra.thrift.CassandraServer#get(ByteBuffer,ColumnPath,ConsistencyLevel)}.
     *
     * That method passes the server's System.currentTimeMillis() to
     * {@link ReadCommand#create(String, ByteBuffer, String, long, IDiskAtomFilter)}.
     * {@code create(...)} in turn passes that timestamp to the SliceFromReadCommand constructor.
     */
    final long nowMillis = times.getTime().toEpochMilli();
    Composite startComposite = CellNames.simpleDense(query.getSliceStart().asByteBuffer());
    Composite endComposite = CellNames.simpleDense(query.getSliceEnd().asByteBuffer());
    SliceQueryFilter sqf = new SliceQueryFilter(startComposite, endComposite,
            false, query.getLimit() + (query.hasLimit()?1:0));
    ReadCommand sliceCmd = new SliceFromReadCommand(keyspace, query.getKey().asByteBuffer(), columnFamily, nowMillis, sqf);

    List<Row> slice = read(sliceCmd, getTx(txh).getReadConsistencyLevel().getDB());

    if (null == slice || 0 == slice.size())
        return EntryList.EMPTY_LIST;

    int sliceSize = slice.size();
    if (1 < sliceSize)
        throw new PermanentBackendException("Received " + sliceSize + " rows for single key");

    Row r = slice.get(0);

    if (null == r) {
        log.warn("Null Row object retrieved from Cassandra StorageProxy");
        return EntryList.EMPTY_LIST;
    }

    ColumnFamily cf = r.cf;

    if (null == cf) {
        log.debug("null ColumnFamily (\"{}\")", columnFamily);
        return EntryList.EMPTY_LIST;
    }

    if (cf.isMarkedForDelete())
        return EntryList.EMPTY_LIST;

    return CassandraHelper.makeEntryList(
            Iterables.filter(cf.getSortedColumns(), new FilterDeletedColumns(nowMillis)),
            entryGetter,
            query.getSliceEnd(),
            query.getLimit());

}
 
Example #23
Source File: SliceFromReadCommand.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public SliceFromReadCommand(String keyspaceName, ByteBuffer key, String cfName, long timestamp, SliceQueryFilter filter)
{
    super(keyspaceName, key, cfName, timestamp, Type.GET_SLICES);
    this.filter = filter;
}
 
Example #24
Source File: AbstractCType.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public IVersionedSerializer<SliceQueryFilter> sliceQueryFilterSerializer()
{
    return sliceQueryFilterSerializer;
}
 
Example #25
Source File: ColumnFamilyStore.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public SliceQueryFilter readFilterForCache()
{
    // We create a new filter everytime before for now SliceQueryFilter is unfortunatly mutable.
    return new SliceQueryFilter(ColumnSlice.ALL_COLUMNS_ARRAY, false, metadata.getCaching().rowCache.rowsToCache, metadata.clusteringColumns().size());
}
 
Example #26
Source File: RangeSliceQueryPager.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
protected boolean isReversed()
{
    return ((SliceQueryFilter)command.predicate).reversed;
}
 
Example #27
Source File: QueryPlan.java    From sasi with Apache License 2.0 4 votes vote down vote up
private boolean isColumnSlice(DecoratedKey key)
{
    return filter.columnFilter(key.key) instanceof SliceQueryFilter;
}
 
Example #28
Source File: CType.java    From stratio-cassandra with Apache License 2.0 votes vote down vote up
public IVersionedSerializer<SliceQueryFilter> sliceQueryFilterSerializer();