Search in sources :

Example 31 with ScanProperties

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

the class FDBRecordStoreSplitRecordsTest method saveAndCheckSplitSimpleRecord.

private void saveAndCheckSplitSimpleRecord(long recno, String strValue, int numValue) {
    FDBStoredRecord<Message> savedRecord = saveAndSplitSimpleRecord(recno, strValue, numValue);
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, TEST_SPLIT_HOOK);
        FDBStoredRecord<Message> loadedRecord = recordStore.loadRecord(Tuple.from(recno));
        assertEquals(savedRecord, loadedRecord);
        List<FDBStoredRecord<Message>> scannedRecords = recordStore.scanRecords(null, ScanProperties.FORWARD_SCAN).asList().join();
        assertEquals(Collections.singletonList(savedRecord), scannedRecords);
        List<FDBStoredRecord<Message>> scanOneRecord = recordStore.scanRecords(null, new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(1).setIsolationLevel(IsolationLevel.SERIALIZABLE).build())).asList().join();
        assertEquals(Collections.singletonList(savedRecord), scanOneRecord);
        List<FDBStoredRecord<Message>> reversedScannedRecords = recordStore.scanRecords(null, ScanProperties.REVERSE_SCAN).asList().join();
        assertEquals(Collections.singletonList(savedRecord), reversedScannedRecords);
        List<FDBStoredRecord<Message>> reversedScannedOneRecord = recordStore.scanRecords(null, new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(1).setIsolationLevel(IsolationLevel.SERIALIZABLE).build(), true)).asList().join();
        assertEquals(Collections.singletonList(savedRecord), reversedScannedOneRecord);
        commit(context);
    }
}
Also used : Message(com.google.protobuf.Message) ScanProperties(com.apple.foundationdb.record.ScanProperties)

Example 32 with ScanProperties

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

the class ChainedCursorTest method testObeysTimeLimit.

@Test
public void testObeysTimeLimit() {
    FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    try (FDBRecordContext context = database.openContext()) {
        ScanProperties props = new ScanProperties(ExecuteProperties.newBuilder().setTimeLimit(4L).setFailOnScanLimitReached(false).build());
        RecordCursorIterator<Long> cursor = new ChainedCursor<>(context, (lastKey) -> nextKey(lastKey).thenApply(value -> {
            sleep(1L);
            return value;
        }), (key) -> Tuple.from(key).pack(), (prevContinuation) -> Tuple.fromBytes(prevContinuation).getLong(0), null, props).asIterator();
        int count = 0;
        while (cursor.hasNext()) {
            assertEquals(Long.valueOf(count), cursor.next());
            ++count;
        }
        assertEquals(cursor.getNoNextReason(), RecordCursor.NoNextReason.TIME_LIMIT_REACHED);
        assertTrue(count < 5, "Too many values returned");
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Tags(com.apple.test.Tags) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) CompletableFuture(java.util.concurrent.CompletableFuture) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) FDBTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBTestBase) Test(org.junit.jupiter.api.Test) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) Tuple(com.apple.foundationdb.tuple.Tuple) ScanProperties(com.apple.foundationdb.record.ScanProperties) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) FDBDatabaseFactory(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory) RecordCursor(com.apple.foundationdb.record.RecordCursor) Optional(java.util.Optional) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScanProperties(com.apple.foundationdb.record.ScanProperties) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test)

Example 33 with ScanProperties

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

the class ChainedCursorTest method testHatesReverse.

@Test
public void testHatesReverse() {
    // The chained cursor cannot implement a reverse scan
    assertThrows(RecordCoreArgumentException.class, () -> {
        FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
        try (FDBRecordContext context = database.openContext()) {
            ScanProperties props = new ScanProperties(ExecuteProperties.newBuilder().build(), true);
            new ChainedCursor<>(context, (lastKey) -> CompletableFuture.completedFuture(Optional.of(10L)), (key) -> new byte[0], (prevContinuation) -> 10L, null, props);
        }
    });
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScanProperties(com.apple.foundationdb.record.ScanProperties) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test)

Example 34 with ScanProperties

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

the class FDBRecordStoreSplitRecordsTest method testSplitContinuation.

@Test
public void testSplitContinuation() {
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, TEST_SPLIT_HOOK);
        commit(context);
    }
    final String bigValue = Strings.repeat("X", SplitHelper.SPLIT_RECORD_SIZE + 10);
    final String smallValue = Strings.repeat("Y", 5);
    final List<FDBStoredRecord<Message>> createdRecords = new ArrayList<>();
    createdRecords.add(saveAndSplitSimpleRecord(1L, smallValue, 1));
    createdRecords.add(saveAndSplitSimpleRecord(2L, smallValue, 2));
    createdRecords.add(saveAndSplitSimpleRecord(3L, bigValue, 3));
    createdRecords.add(saveAndSplitSimpleRecord(4L, smallValue, 4));
    createdRecords.add(saveAndSplitSimpleRecord(5L, bigValue, 5));
    createdRecords.add(saveAndSplitSimpleRecord(6L, bigValue, 6));
    createdRecords.add(saveAndSplitSimpleRecord(7L, smallValue, 7));
    createdRecords.add(saveAndSplitSimpleRecord(8L, smallValue, 8));
    createdRecords.add(saveAndSplitSimpleRecord(9L, smallValue, 9));
    // Scan one record at a time using continuations
    final List<FDBStoredRecord<Message>> scannedRecords = new ArrayList<>();
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, TEST_SPLIT_HOOK);
        RecordCursorIterator<FDBStoredRecord<Message>> messageCursor = recordStore.scanRecords(null, new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(1).setIsolationLevel(IsolationLevel.SERIALIZABLE).build())).asIterator();
        while (messageCursor.hasNext()) {
            scannedRecords.add(messageCursor.next());
            messageCursor = recordStore.scanRecords(messageCursor.getContinuation(), new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(1).setIsolationLevel(IsolationLevel.SERIALIZABLE).build())).asIterator();
        }
        commit(context);
    }
    assertEquals(createdRecords, scannedRecords);
}
Also used : ScanProperties(com.apple.foundationdb.record.ScanProperties) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 35 with ScanProperties

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

the class SplitHelperTest method scanSingleRecord.

private FDBRawRecord scanSingleRecord(@Nonnull FDBRecordContext context, boolean reverse, @Nonnull Tuple key, @Nullable FDBStoredSizes expectedSizes, @Nullable byte[] expectedContents, @Nullable FDBRecordVersion version) {
    final ScanProperties scanProperties = reverse ? ScanProperties.REVERSE_SCAN : ScanProperties.FORWARD_SCAN;
    KeyValueCursor kvCursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.allOf(key)).setScanProperties(scanProperties).build();
    SplitHelper.KeyValueUnsplitter kvUnsplitter = new SplitHelper.KeyValueUnsplitter(context, subspace, kvCursor, false, null, scanProperties);
    RecordCursorResult<FDBRawRecord> result = kvUnsplitter.getNext();
    if (expectedSizes == null || expectedContents == null) {
        assertThat(result.hasNext(), is(false));
        assertEquals(RecordCursor.NoNextReason.SOURCE_EXHAUSTED, result.getNoNextReason());
        return null;
    } else {
        assertThat(result.hasNext(), is(true));
        final FDBRawRecord rawRecord = result.get();
        result = kvUnsplitter.getNext();
        assertThat(result.hasNext(), is(false));
        assertEquals(RecordCursor.NoNextReason.SOURCE_EXHAUSTED, result.getNoNextReason());
        assertNotNull(rawRecord);
        assertEquals(key, rawRecord.getPrimaryKey());
        assertArrayEquals(expectedContents, rawRecord.getRawRecord());
        assertEquals(expectedSizes.getKeyCount(), rawRecord.getKeyCount());
        assertEquals(expectedSizes.getKeySize(), rawRecord.getKeySize());
        assertEquals(expectedSizes.getValueSize(), rawRecord.getValueSize());
        boolean isSplit = rawRecord.getKeyCount() - (rawRecord.isVersionedInline() ? 1 : 0) != 1;
        assertEquals(rawRecord.getKeyCount() - (rawRecord.isVersionedInline() ? 1 : 0) != 1, expectedSizes.isSplit());
        assertEquals(version != null, expectedSizes.isVersionedInline());
        return rawRecord;
    }
}
Also used : ScanProperties(com.apple.foundationdb.record.ScanProperties)

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