Search in sources :

Example 1 with IndexKeyEntries

use of com.hazelcast.query.impl.IndexKeyEntries in project hazelcast by hazelcast.

the class MapFetchIndexOperation method runInternalSorted.

@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:NPathComplexity", "checkstyle:MethodLength" })
private MapFetchIndexOperationResult runInternalSorted(InternalIndex index) {
    List<QueryableEntry<?, ?>> entries = new ArrayList<>(sizeLimit);
    int partitionCount = getNodeEngine().getPartitionService().getPartitionCount();
    for (int i = 0; i < pointers.length; i++) {
        IndexIterationPointer pointer = pointers[i];
        Data lastEntryKeyData = pointer.getLastEntryKeyData();
        Comparator<Data> comparator = OrderedIndexStore.DATA_COMPARATOR;
        if (isDescendingEntryKey(pointer)) {
            comparator = comparator.reversed();
        }
        Iterator<IndexKeyEntries> entryIterator = getEntryIterator(index, pointer);
        while (entryIterator.hasNext()) {
            IndexKeyEntries indexKeyEntries = entryIterator.next();
            @SuppressWarnings({ "rawtypes" }) Iterator<QueryableEntry> keyEntries = indexKeyEntries.getEntries();
            // Skip until the entry last read
            if (lastEntryKeyData != null) {
                while (keyEntries.hasNext()) {
                    QueryableEntry<?, ?> entry = keyEntries.next();
                    int comparison = comparator.compare(entry.getKeyData(), lastEntryKeyData);
                    if (comparison >= 0) {
                        if (comparison > 0 && isInPartitionSet(entry, partitionIdSet, partitionCount)) {
                            entries.add(entry);
                            lastEntryKeyData = entry.getKeyData();
                        }
                        break;
                    }
                }
            }
            // Read and add until size limit is reached or iterator ends
            while (keyEntries.hasNext() && entries.size() < sizeLimit) {
                QueryableEntry<?, ?> entry = keyEntries.next();
                if (isInPartitionSet(entry, partitionIdSet, partitionCount)) {
                    entries.add(entry);
                    lastEntryKeyData = entry.getKeyData();
                }
            }
            if (!keyEntries.hasNext()) {
                lastEntryKeyData = null;
            }
            if (entries.size() >= sizeLimit) {
                IndexIterationPointer[] newPointers;
                if (entryIterator.hasNext() || lastEntryKeyData != null) {
                    Comparable<?> currentIndexKey = indexKeyEntries.getIndexKey();
                    newPointers = new IndexIterationPointer[pointers.length - i];
                    if (lastEntryKeyData != null) {
                        newPointers[0] = IndexIterationPointer.create(pointer.isDescending() ? pointer.getFrom() : currentIndexKey, !pointer.isDescending() || pointer.isFromInclusive(), pointer.isDescending() ? currentIndexKey : pointer.getTo(), pointer.isDescending() || pointer.isToInclusive(), pointer.isDescending(), lastEntryKeyData);
                    } else {
                        newPointers[0] = IndexIterationPointer.create(pointer.isDescending() ? pointer.getFrom() : currentIndexKey, pointer.isDescending() && pointer.isFromInclusive(), pointer.isDescending() ? currentIndexKey : pointer.getTo(), !pointer.isDescending() && pointer.isToInclusive(), pointer.isDescending(), null);
                    }
                    System.arraycopy(pointers, i + 1, newPointers, 1, newPointers.length - 1);
                } else {
                    newPointers = new IndexIterationPointer[pointers.length - i - 1];
                    System.arraycopy(pointers, i + 1, newPointers, 0, newPointers.length);
                }
                return new MapFetchIndexOperationResult(entries, newPointers);
            }
        }
    }
    return new MapFetchIndexOperationResult(entries, new IndexIterationPointer[0]);
}
Also used : IndexKeyEntries(com.hazelcast.query.impl.IndexKeyEntries) IndexIterationPointer(com.hazelcast.internal.iteration.IndexIterationPointer) ArrayList(java.util.ArrayList) Data(com.hazelcast.internal.serialization.Data) QueryableEntry(com.hazelcast.query.impl.QueryableEntry)

Aggregations

IndexIterationPointer (com.hazelcast.internal.iteration.IndexIterationPointer)1 Data (com.hazelcast.internal.serialization.Data)1 IndexKeyEntries (com.hazelcast.query.impl.IndexKeyEntries)1 QueryableEntry (com.hazelcast.query.impl.QueryableEntry)1 ArrayList (java.util.ArrayList)1