Search in sources :

Example 1 with MergeCursorState

use of com.apple.foundationdb.record.provider.foundationdb.cursors.MergeCursorState in project fdb-record-layer by FoundationDB.

the class ComposedBitmapIndexCursor method computeNextResultStates.

@Nonnull
@Override
protected CompletableFuture<List<MergeCursorState<IndexEntry>>> computeNextResultStates() {
    final List<MergeCursorState<IndexEntry>> cursorStates = getCursorStates();
    return whenAll(cursorStates).thenApply(vignore -> {
        boolean anyHasNext = false;
        for (MergeCursorState<IndexEntry> cursorState : cursorStates) {
            if (cursorState.getResult().hasNext()) {
                anyHasNext = true;
            } else if (cursorState.getResult().getNoNextReason().isLimitReached()) {
                // Stop if any has reached limit.
                return Collections.emptyList();
            }
        }
        if (anyHasNext) {
            // Result states are all those that share the minimum next position,
            // whose bitmaps need to be merged to produce the next stream element.
            final List<MergeCursorState<IndexEntry>> resultStates = new ArrayList<>();
            long nextPosition = Long.MAX_VALUE;
            for (MergeCursorState<IndexEntry> cursorState : cursorStates) {
                if (cursorState.getResult().hasNext()) {
                    final IndexEntry indexEntry = cursorState.getResult().get();
                    final Tuple indexKey = indexEntry.getKey();
                    final long position = indexKey.getLong(indexKey.size() - 1);
                    if (nextPosition > position) {
                        resultStates.clear();
                        nextPosition = position;
                    }
                    if (nextPosition == position) {
                        resultStates.add(cursorState);
                    }
                }
            }
            return resultStates;
        } else {
            return Collections.emptyList();
        }
    });
}
Also used : MergeCursorState(com.apple.foundationdb.record.provider.foundationdb.cursors.MergeCursorState) ArrayList(java.util.ArrayList) IndexEntry(com.apple.foundationdb.record.IndexEntry) Tuple(com.apple.foundationdb.tuple.Tuple) Nonnull(javax.annotation.Nonnull)

Example 2 with MergeCursorState

use of com.apple.foundationdb.record.provider.foundationdb.cursors.MergeCursorState in project fdb-record-layer by FoundationDB.

the class ComposedBitmapIndexCursor method create.

@Nonnull
public static ComposedBitmapIndexCursor create(@Nonnull List<Function<byte[], RecordCursor<IndexEntry>>> cursorFunctions, @Nonnull Composer composer, @Nullable byte[] byteContinuation, @Nullable FDBStoreTimer timer) {
    if (cursorFunctions.size() < 2) {
        throw new RecordCoreArgumentException("not enough child cursors provided to ComposedBitmapIndexCursor").addLogInfo(LogMessageKeys.CHILD_COUNT, cursorFunctions.size());
    }
    final List<MergeCursorState<IndexEntry>> cursorStates = new ArrayList<>(cursorFunctions.size());
    final ComposedBitmapIndexContinuation continuation = ComposedBitmapIndexContinuation.from(byteContinuation, cursorFunctions.size());
    int i = 0;
    for (Function<byte[], RecordCursor<IndexEntry>> cursorFunction : cursorFunctions) {
        cursorStates.add(MergeCursorState.from(cursorFunction, continuation.getContinuation(i)));
        i++;
    }
    return new ComposedBitmapIndexCursor(cursorStates, timer, composer);
}
Also used : MergeCursorState(com.apple.foundationdb.record.provider.foundationdb.cursors.MergeCursorState) RecordCursor(com.apple.foundationdb.record.RecordCursor) ArrayList(java.util.ArrayList) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) Nonnull(javax.annotation.Nonnull)

Example 3 with MergeCursorState

use of com.apple.foundationdb.record.provider.foundationdb.cursors.MergeCursorState in project fdb-record-layer by FoundationDB.

the class ComposedBitmapIndexCursor method getNextResult.

@Nonnull
@Override
protected IndexEntry getNextResult(@Nonnull List<MergeCursorState<IndexEntry>> resultStates) {
    final List<MergeCursorState<IndexEntry>> cursorStates = getCursorStates();
    final IndexEntry firstEntry = resultStates.get(0).getResult().get();
    final int size = firstEntry.getValue().getBytes(0).length;
    final List<byte[]> bitmaps = new ArrayList<>(cursorStates.size());
    for (MergeCursorState<IndexEntry> cursorState : cursorStates) {
        if (resultStates.contains(cursorState)) {
            byte[] bitmap = cursorState.getResult().get().getValue().getBytes(0);
            if (bitmap.length != size) {
                throw new RecordCoreException("Index bitmaps are not all the same size");
            }
            bitmaps.add(bitmap);
        } else {
            bitmaps.add(null);
        }
    }
    final byte[] composed = composer.compose(bitmaps, size);
    return new IndexEntry(firstEntry.getIndex(), firstEntry.getKey(), Tuple.fromList(Collections.singletonList(composed)));
}
Also used : MergeCursorState(com.apple.foundationdb.record.provider.foundationdb.cursors.MergeCursorState) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) ArrayList(java.util.ArrayList) IndexEntry(com.apple.foundationdb.record.IndexEntry) Nonnull(javax.annotation.Nonnull)

Aggregations

MergeCursorState (com.apple.foundationdb.record.provider.foundationdb.cursors.MergeCursorState)3 ArrayList (java.util.ArrayList)3 Nonnull (javax.annotation.Nonnull)3 IndexEntry (com.apple.foundationdb.record.IndexEntry)2 RecordCoreArgumentException (com.apple.foundationdb.record.RecordCoreArgumentException)1 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)1 RecordCursor (com.apple.foundationdb.record.RecordCursor)1 Tuple (com.apple.foundationdb.tuple.Tuple)1