Search in sources :

Example 36 with ScanProperties

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

the class KeyValueCursorTest method beginsWith.

@Test
public void beginsWith() {
    fdb.run(context -> {
        KeyValueCursor cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.allOf(Tuple.from(3))).setContinuation(null).setScanProperties(ScanProperties.FORWARD_SCAN).build();
        for (int j = 0; j < 5; j++) {
            KeyValue kv = cursor.getNext().get();
            assertArrayEquals(subspace.pack(Tuple.from(3, j)), kv.getKey());
            assertArrayEquals(Tuple.from(3, j).pack(), kv.getValue());
        }
        assertThat(cursor.getNext().hasNext(), is(false));
        cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.allOf(Tuple.from(3))).setContinuation(null).setScanProperties(new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(2).build())).build();
        assertEquals(2, (int) cursor.getCount().join());
        cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.allOf(Tuple.from(3))).setContinuation(cursor.getNext().getContinuation().toBytes()).setScanProperties(new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(3).build())).build();
        assertEquals(3, (int) cursor.getCount().join());
        return null;
    });
}
Also used : KeyValue(com.apple.foundationdb.KeyValue) ScanProperties(com.apple.foundationdb.record.ScanProperties) Test(org.junit.jupiter.api.Test)

Example 37 with ScanProperties

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

the class KeyValueCursorTest method all.

@Test
public void all() {
    fdb.run(context -> {
        KeyValueCursor cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.ALL).setContinuation(null).setScanProperties(ScanProperties.FORWARD_SCAN).build();
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                KeyValue kv = cursor.getNext().get();
                assertArrayEquals(subspace.pack(Tuple.from(i, j)), kv.getKey());
                assertArrayEquals(Tuple.from(i, j).pack(), kv.getValue());
            }
        }
        assertThat(cursor.getNext().hasNext(), is(false));
        cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.ALL).setContinuation(null).setScanProperties(new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(10).build())).build();
        assertEquals(10, (int) cursor.getCount().join());
        cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.ALL).setContinuation(cursor.getNext().getContinuation().toBytes()).setScanProperties(ScanProperties.FORWARD_SCAN).build();
        assertEquals(15, (int) cursor.getCount().join());
        return null;
    });
}
Also used : KeyValue(com.apple.foundationdb.KeyValue) ScanProperties(com.apple.foundationdb.record.ScanProperties) Test(org.junit.jupiter.api.Test)

Example 38 with ScanProperties

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

the class RankIndexTest method checkRankScan.

@Test
public void checkRankScan() throws Exception {
    TupleRange range = new TupleRange(Tuple.from(0L), Tuple.from(2L), EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_EXCLUSIVE);
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        int i = 0;
        try (RecordCursorIterator<FDBIndexedRecord<Message>> cursor = recordStore.scanIndexRecords("BasicRankedRecord$score", IndexScanType.BY_RANK, range, null, ScanProperties.FORWARD_SCAN).asIterator()) {
            while (cursor.hasNext()) {
                FDBIndexedRecord<Message> rec = cursor.next();
                TestRecordsRankProto.BasicRankedRecord.Builder myrec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
                myrec.mergeFrom(rec.getRecord());
                assertTrue(myrec.getScore() < 200);
                i++;
            }
        }
        assertEquals(2, i);
    }
    range = TupleRange.allOf(Tuple.from("M", 0L));
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        try (RecordCursor<FDBIndexedRecord<Message>> cursor = recordStore.scanIndexRecords("rank_by_gender", IndexScanType.BY_RANK, range, null, new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(1).build()))) {
            FDBIndexedRecord<Message> rec = cursor.getNext().get();
            TestRecordsRankProto.BasicRankedRecord.Builder myrec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
            myrec.mergeFrom(rec.getRecord());
            assertEquals("hector", myrec.getName());
            assertEquals(75, myrec.getScore());
        }
    }
}
Also used : FDBIndexedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBIndexedRecord) Message(com.google.protobuf.Message) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScanProperties(com.apple.foundationdb.record.ScanProperties) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test)

Example 39 with ScanProperties

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

the class TextIndexTest method scanWithZeroScanRecordLimit.

private void scanWithZeroScanRecordLimit(@Nonnull Index index, @Nonnull String token, boolean reverse) throws Exception {
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        ScanProperties scanProperties = ExecuteProperties.newBuilder().setScannedRecordsLimit(0).build().asScanProperties(reverse);
        RecordCursor<IndexEntry> cursor = recordStore.scanIndex(index, BY_TEXT_TOKEN, TupleRange.allOf(Tuple.from(token)), null, scanProperties);
        RecordCursorResult<IndexEntry> result = cursor.getNext();
        if (!result.hasNext()) {
            assertEquals(SOURCE_EXHAUSTED, result.getNoNextReason());
            return;
        }
        result = cursor.getNext();
        assertThat(result.hasNext(), is(false));
        assertEquals(SCAN_LIMIT_REACHED, result.getNoNextReason());
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScanProperties(com.apple.foundationdb.record.ScanProperties) IndexEntry(com.apple.foundationdb.record.IndexEntry)

Example 40 with ScanProperties

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

the class FDBRecordStoreByteLimitTest method testHitExhaustedDuringSplit.

@BooleanSource
@ParameterizedTest
public void testHitExhaustedDuringSplit(boolean reverse) throws Exception {
    deleteSimpleRecords();
    // Insert a single large record
    final FDBStoredRecord<Message> createdRecord = saveAndSplitSimpleRecord(1L, Strings.repeat("Z", SplitHelper.SPLIT_RECORD_SIZE + 10), 1);
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, TEST_SPLIT_HOOK);
        // Create a limit that will be hit while reading the first record
        ScanProperties properties = new ScanProperties(ExecuteProperties.newBuilder().setScannedBytesLimit(1).setIsolationLevel(IsolationLevel.SERIALIZABLE).build(), reverse);
        final RecordCursor<FDBStoredRecord<Message>> messageCursor = recordStore.scanRecords(null, properties);
        RecordCursorResult<FDBStoredRecord<Message>> result = messageCursor.getNext();
        assertTrue(result.hasNext());
        assertEquals(createdRecord, result.get());
        // Limit hit and also exhausted. Either one is a valid response, but the data should be internally
        // consistent (i.e., it should not return a non-end continuation if the source is exhausted)
        result = messageCursor.getNext();
        assertFalse(result.hasNext());
        if (result.getNoNextReason().isSourceExhausted()) {
            assertTrue(result.getContinuation().isEnd(), "second result should be at the end");
            assertNull(result.getContinuation().toBytes());
        } else {
            assertEquals(RecordCursor.NoNextReason.BYTE_LIMIT_REACHED, result.getNoNextReason());
            assertFalse(result.getContinuation().isEnd());
            assertNotNull(result.getContinuation().toBytes());
        }
    }
}
Also used : Message(com.google.protobuf.Message) FDBStoredRecord(com.apple.foundationdb.record.provider.foundationdb.FDBStoredRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScanProperties(com.apple.foundationdb.record.ScanProperties) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Aggregations

ScanProperties (com.apple.foundationdb.record.ScanProperties)54 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)29 Nonnull (javax.annotation.Nonnull)29 Tuple (com.apple.foundationdb.tuple.Tuple)26 Message (com.google.protobuf.Message)24 Test (org.junit.jupiter.api.Test)24 RecordCursor (com.apple.foundationdb.record.RecordCursor)22 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)22 TupleRange (com.apple.foundationdb.record.TupleRange)21 List (java.util.List)21 ArrayList (java.util.ArrayList)20 IndexEntry (com.apple.foundationdb.record.IndexEntry)19 CompletableFuture (java.util.concurrent.CompletableFuture)19 Nullable (javax.annotation.Nullable)18 Index (com.apple.foundationdb.record.metadata.Index)17 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)16 TupleHelpers (com.apple.foundationdb.tuple.TupleHelpers)16 LogMessageKeys (com.apple.foundationdb.record.logging.LogMessageKeys)15 Arrays (java.util.Arrays)15 Collectors (java.util.stream.Collectors)15