Search in sources :

Example 1 with RowLimitedCursor

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

the class ProbableIntersectionCursorTest method longLists.

@Test
public void longLists() {
    final Random r = new Random(0xba5eba11);
    for (int itr = 0; itr < 50; itr++) {
        long seed = r.nextLong();
        LOGGER.info(KeyValueLogMessage.of("running intersection with large lists", TestLogMessageKeys.SEED, seed, TestLogMessageKeys.ITERATION, itr));
        r.setSeed(seed);
        final List<List<Integer>> lists = Stream.generate(() -> IntStream.generate(() -> r.nextInt(500)).limit(1000).boxed().collect(Collectors.toList())).limit(5).collect(Collectors.toList());
        final List<Function<byte[], RecordCursor<Integer>>> cursorFuncs = lists.stream().map(list -> (Function<byte[], RecordCursor<Integer>>) ((byte[] continuation) -> new RowLimitedCursor<>(RecordCursor.fromList(list, continuation), r.nextInt(50) + 10))).collect(Collectors.toList());
        final List<Set<Integer>> sets = lists.stream().map(HashSet::new).collect(Collectors.toList());
        final Set<Integer> actualIntersection = new HashSet<>(sets.get(0));
        sets.forEach(actualIntersection::retainAll);
        Set<Integer> found = new HashSet<>();
        AtomicInteger falsePositives = new AtomicInteger();
        boolean done = false;
        byte[] continuation = null;
        while (!done) {
            RecordCursor<Integer> intersectionCursor = ProbableIntersectionCursor.create(Collections::singletonList, cursorFuncs, continuation, null);
            AsyncUtil.whileTrue(() -> intersectionCursor.onNext().thenApply(result -> {
                if (result.hasNext()) {
                    // Each value should be in at least one set and hopefully all
                    int value = result.get();
                    assertThat(sets.stream().anyMatch(set -> set.contains(value)), is(true));
                    if (!actualIntersection.contains(value)) {
                        falsePositives.incrementAndGet();
                    }
                    found.add(value);
                }
                return result.hasNext();
            }), intersectionCursor.getExecutor()).join();
            RecordCursorResult<Integer> result = intersectionCursor.getNext();
            assertThat(result.hasNext(), is(false));
            if (result.getNoNextReason().isSourceExhausted()) {
                done = true;
            } else {
                assertEquals(RecordCursor.NoNextReason.RETURN_LIMIT_REACHED, result.getNoNextReason());
            }
            continuation = result.getContinuation().toBytes();
        }
        assertThat(found.containsAll(actualIntersection), is(true));
        LOGGER.info(KeyValueLogMessage.of("intersection false positives", "false_positives", falsePositives.get(), "actual_intersection_size", actualIntersection.size(), "iteration", itr));
        assertThat(falsePositives.get(), lessThan(20));
    }
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) RowLimitedCursor(com.apple.foundationdb.record.cursors.RowLimitedCursor) LoggerFactory(org.slf4j.LoggerFactory) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Function(java.util.function.Function) Iterators(com.google.common.collect.Iterators) HashSet(java.util.HashSet) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) TestLogMessageKeys(com.apple.foundationdb.record.logging.TestLogMessageKeys) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FirableCursor(com.apple.foundationdb.record.cursors.FirableCursor) Matchers.lessThan(org.hamcrest.Matchers.lessThan) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) BloomFilter(com.google.common.hash.BloomFilter) Set(java.util.Set) Collectors(java.util.stream.Collectors) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Stream(java.util.stream.Stream) RecordCursorProto(com.apple.foundationdb.record.RecordCursorProto) RecordCursor(com.apple.foundationdb.record.RecordCursor) RecordCursorTest(com.apple.foundationdb.record.RecordCursorTest) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) HashSet(java.util.HashSet) Set(java.util.Set) RowLimitedCursor(com.apple.foundationdb.record.cursors.RowLimitedCursor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(java.util.function.Function) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) Collections(java.util.Collections) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) RecordCursorTest(com.apple.foundationdb.record.RecordCursorTest)

Aggregations

AsyncUtil (com.apple.foundationdb.async.AsyncUtil)1 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)1 RecordCursor (com.apple.foundationdb.record.RecordCursor)1 RecordCursorProto (com.apple.foundationdb.record.RecordCursorProto)1 RecordCursorResult (com.apple.foundationdb.record.RecordCursorResult)1 RecordCursorTest (com.apple.foundationdb.record.RecordCursorTest)1 FirableCursor (com.apple.foundationdb.record.cursors.FirableCursor)1 RowLimitedCursor (com.apple.foundationdb.record.cursors.RowLimitedCursor)1 KeyValueLogMessage (com.apple.foundationdb.record.logging.KeyValueLogMessage)1 TestLogMessageKeys (com.apple.foundationdb.record.logging.TestLogMessageKeys)1 FDBStoreTimer (com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer)1 Iterators (com.google.common.collect.Iterators)1 BloomFilter (com.google.common.hash.BloomFilter)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Random (java.util.Random)1