*/
final AbstractBounds<RowPosition> range = filter.dataRange.keyRange();
ByteBuffer startKey = range.left instanceof DecoratedKey ? ((DecoratedKey)range.left).key : ByteBufferUtil.EMPTY_BYTE_BUFFER;
ByteBuffer endKey = range.right instanceof DecoratedKey ? ((DecoratedKey)range.right).key : ByteBufferUtil.EMPTY_BYTE_BUFFER;
final CompositeType baseComparator = (CompositeType)baseCfs.getComparator();
final CompositeType indexComparator = (CompositeType)index.getIndexCfs().getComparator();
final ByteBuffer startPrefix = makePrefix(index, startKey, filter, true);
final ByteBuffer endPrefix = makePrefix(index, endKey, filter, false);
return new ColumnFamilyStore.AbstractScanIterator()
{
private ByteBuffer lastSeenPrefix = startPrefix;
private Deque<Column> indexColumns;
private int columnsRead = Integer.MAX_VALUE;
private int limit = filter.currentLimit();
private int columnsCount = 0;
private int meanColumns = Math.max(index.getIndexCfs().getMeanColumns(), 1);
// We shouldn't fetch only 1 row as this provides buggy paging in case the first row doesn't satisfy all clauses
private int rowsPerQuery = Math.max(Math.min(filter.maxRows(), filter.maxColumns() / meanColumns), 2);
public boolean needsFiltering()
{
return false;
}
private Row makeReturn(DecoratedKey key, ColumnFamily data)
{
if (data == null)
return endOfData();
assert key != null;
return new Row(key, data);
}
protected Row computeNext()
{
/*
* Our internal index code is wired toward internal rows. So we need to accumulate all results for a given
* row before returning from this method. Which unfortunately means that this method has to do what
* CFS.filter does for KeysIndex.
*/
DecoratedKey currentKey = null;
ColumnFamily data = null;
while (true)
{
// Did we get more columns that needed to respect the user limit?
// (but we still need to return what has been fetched already)
if (columnsCount >= limit)
return makeReturn(currentKey, data);
if (indexColumns == null || indexColumns.isEmpty())
{
if (columnsRead < rowsPerQuery)
{
logger.trace("Read only {} (< {}) last page through, must be done", columnsRead, rowsPerQuery);
return makeReturn(currentKey, data);
}
if (logger.isTraceEnabled())
logger.trace("Scanning index {} starting with {}",
index.expressionString(primary), indexComparator.getString(startPrefix));
QueryFilter indexFilter = QueryFilter.getSliceFilter(indexKey,
index.getIndexCfs().name,
lastSeenPrefix,
endPrefix,
false,
rowsPerQuery,
filter.timestamp);
ColumnFamily indexRow = index.getIndexCfs().getColumnFamily(indexFilter);
if (indexRow == null || indexRow.getColumnCount() == 0)
return makeReturn(currentKey, data);
Collection<Column> sortedColumns = indexRow.getSortedColumns();
columnsRead = sortedColumns.size();
indexColumns = new ArrayDeque<>(sortedColumns);
Column firstColumn = sortedColumns.iterator().next();
// Paging is racy, so it is possible the first column of a page is not the last seen one.
if (lastSeenPrefix != startPrefix && lastSeenPrefix.equals(firstColumn.name()))
{
// skip the row we already saw w/ the last page of results
indexColumns.poll();
logger.trace("Skipping {}", indexComparator.getString(firstColumn.name()));
}
}
while (!indexColumns.isEmpty() && columnsCount <= limit)
{
Column column = indexColumns.poll();
lastSeenPrefix = column.name();
if (column.isMarkedForDelete(filter.timestamp))
{
logger.trace("skipping {}", column.name());
continue;
}
CompositesIndex.IndexedEntry entry = index.decodeEntry(indexKey, column);
DecoratedKey dk = baseCfs.partitioner.decorateKey(entry.indexedKey);
// Are we done for this row?
if (currentKey == null)
{
currentKey = dk;
}
else if (!currentKey.equals(dk))
{
DecoratedKey previousKey = currentKey;
currentKey = dk;
// We're done with the previous row, return it if it had data, continue otherwise
indexColumns.addFirst(column);
if (data == null)
continue;
else
return makeReturn(previousKey, data);
}
if (!range.contains(dk))
{
// Either we're not yet in the range cause the range is start excluding, or we're
// past it.
if (!range.right.isMinimum(baseCfs.partitioner) && range.right.compareTo(dk) < 0)
{
logger.trace("Reached end of assigned scan range");
return endOfData();
}
else
{
logger.debug("Skipping entry {} before assigned scan range", dk.token);
continue;
}
}
// Check if this entry cannot be a hit due to the original column filter
ByteBuffer start = entry.indexedEntryStart();
if (!filter.columnFilter(dk.key).maySelectPrefix(baseComparator, start))
continue;
logger.trace("Adding index hit to current row for {}", indexComparator.getString(column.name()));
// We always query the whole CQL3 row. In the case where the original filter was a name filter this might be
// slightly wasteful, but this probably doesn't matter in practice and it simplify things.
ColumnSlice dataSlice = new ColumnSlice(start, entry.indexedEntryEnd());
ColumnSlice[] slices;