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();
}
});
}
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);
}
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)));
}
Aggregations