Search in sources :

Example 96 with FDBRecordContext

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

the class RankIndexTest method countNotPossibleWithoutUniqueIndex.

@Test
public void countNotPossibleWithoutUniqueIndex() throws Exception {
    assertThrows(RecordCoreException.class, () -> {
        try (FDBRecordContext context = openContext()) {
            openRecordStore(context);
            recordStore.evaluateAggregateFunction(Collections.singletonList("BasicRankedRecord"), IndexFunctionHelper.count(Key.Expressions.field("gender")), Key.Evaluated.scalar("M"), IsolationLevel.SERIALIZABLE);
        }
    });
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Test(org.junit.jupiter.api.Test)

Example 97 with FDBRecordContext

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

the class TextIndexTest method backwardsRangeScanRaceCondition.

// An older implementation did reverse range scan to find the keys before and after in order
// to find where insertions should go. This was able to reproduce an error where two keys could
// be returned after the scan that were both greater than the map key due to a race condition.
// This was able to reproduce the error when run alone.
@Test
public void backwardsRangeScanRaceCondition() throws Exception {
    final Random r = new Random(0x5ca1ab1e);
    final List<String> lexicon = Arrays.asList(TextSamples.ROMEO_AND_JULIET_PROLOGUE.split(" "));
    final SimpleDocument bigDocument = getRandomRecords(r, 1, lexicon, 100, 0).get(0);
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context, metaDataBuilder -> metaDataBuilder.setSplitLongRecords(true));
        LOGGER.info(KeyValueLogMessage.of("saving document", LogMessageKeys.DOCUMENT, bigDocument));
        recordStore.saveRecord(bigDocument);
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context, metaDataBuilder -> metaDataBuilder.setSplitLongRecords(true));
        recordStore.deleteRecord(Tuple.from(bigDocument.getDocId()));
        recordStore.saveRecord(bigDocument);
    // do not commit
    } catch (RuntimeException e) {
        Throwable err = e;
        while (!(err instanceof LoggableException) && err != null) {
            err = err.getCause();
        }
        if (err != null) {
            LoggableException logE = (LoggableException) err;
            LOGGER.error(KeyValueLogMessage.build("unable to save record").addKeysAndValues(logE.getLogInfo()).toString(), err);
            throw logE;
        } else {
            throw e;
        }
    }
}
Also used : Random(java.util.Random) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) SimpleDocument(com.apple.foundationdb.record.TestRecordsTextProto.SimpleDocument) Matchers.containsString(org.hamcrest.Matchers.containsString) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) LoggableException(com.apple.foundationdb.util.LoggableException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 98 with FDBRecordContext

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

the class VersionIndexTest method updateWithinContext.

@ParameterizedTest(name = "updateWithinContext [formatVersion = {0}, splitLongRecords = {1}]")
@MethodSource("formatVersionArguments")
@SuppressWarnings("try")
public void updateWithinContext(int testFormatVersion, boolean testSplitLongRecords) {
    formatVersion = testFormatVersion;
    splitLongRecords = testSplitLongRecords;
    MySimpleRecord record1 = MySimpleRecord.newBuilder().setRecNo(1066L).setNumValue2(42).setNumValue3Indexed(1).build();
    MySimpleRecord record2 = MySimpleRecord.newBuilder().setRecNo(1066L).setNumValue2(42).setNumValue3Indexed(2).build();
    MySimpleRecord record3 = MySimpleRecord.newBuilder().setRecNo(1066L).setNumValue2(43).setNumValue3Indexed(2).build();
    MySimpleRecord record4 = MySimpleRecord.newBuilder().setRecNo(1776L).setNumValue2(42).setNumValue3Indexed(1).build();
    byte[] versionstamp;
    try (FDBRecordContext context = openContext(simpleVersionHook)) {
        FDBRecordVersion version = FDBRecordVersion.incomplete(context.claimLocalVersion());
        FDBStoredRecord<?> stored1 = recordStore.saveRecord(record1, version);
        assertTrue(stored1.hasVersion());
        assertEquals(0, stored1.getVersion().getLocalVersion());
        assertFalse(stored1.getVersion().isComplete());
        // Save same again. Should be idempotent.
        FDBStoredRecord<?> stored1a = recordStore.saveRecord(record1, version);
        assertTrue(stored1a.hasVersion());
        assertEquals(0, stored1a.getVersion().getLocalVersion());
        assertFalse(stored1a.getVersion().isComplete());
        assertEquals(stored1, stored1a);
        // Save record. Shouldn't update version information.
        FDBStoredRecord<?> stored2 = recordStore.saveRecord(record2, version);
        assertTrue(stored1.hasVersion());
        assertEquals(0, stored2.getVersion().getLocalVersion());
        assertFalse(stored2.getVersion().isComplete());
        assertEquals(stored1.getPrimaryKey(), stored2.getPrimaryKey());
        assertEquals(stored1.getVersion(), stored2.getVersion());
        // Save record. Shouldn't update version information
        FDBStoredRecord<?> stored3 = recordStore.saveRecord(record3, version);
        assertTrue(stored3.hasVersion());
        assertEquals(0, stored3.getVersion().getLocalVersion());
        assertFalse(stored3.getVersion().isComplete());
        assertEquals(stored1.getPrimaryKey(), stored3.getPrimaryKey());
        assertEquals(stored1.getVersion(), stored3.getVersion());
        // New record.
        FDBStoredRecord<?> stored4 = recordStore.saveRecord(record4);
        assertTrue(stored4.hasVersion());
        assertEquals(1, stored4.getVersion().getLocalVersion());
        assertFalse(stored4.getVersion().isComplete());
        // Same record. New version.
        FDBStoredRecord<?> stored4a = recordStore.saveRecord(record4);
        assertTrue(stored4a.hasVersion());
        assertEquals(2, stored4a.getVersion().getLocalVersion());
        assertFalse(stored4a.getVersion().isComplete());
        context.commit();
        versionstamp = context.getVersionStamp();
        assertEquals(3, context.claimLocalVersion());
    }
    try (FDBRecordContext context = openContext(simpleVersionHook)) {
        Optional<FDBRecordVersion> storedVersionOptional = recordStore.loadRecordVersion(Tuple.from(1066L));
        assertTrue(storedVersionOptional.isPresent());
        assertEquals(FDBRecordVersion.complete(versionstamp, 0), storedVersionOptional.get());
        Optional<FDBRecordVersion> storedVersionOptional2 = recordStore.loadRecordVersion(Tuple.from(1776L));
        assertTrue(storedVersionOptional2.isPresent());
        assertEquals(FDBRecordVersion.complete(versionstamp, 2), storedVersionOptional2.get());
        // Verify that there are only two entries in the index.
        assertEquals(Arrays.asList(Tuple.from(FDBRecordVersion.complete(versionstamp, 0).toVersionstamp(), 1066L), Tuple.from(FDBRecordVersion.complete(versionstamp, 2).toVersionstamp(), 1776L)), recordStore.scanIndex(metaData.getIndex("globalVersion"), IndexScanType.BY_VALUE, TupleRange.ALL, null, ScanProperties.FORWARD_SCAN).map(IndexEntry::getKey).asList().join());
        assertEquals(Arrays.asList(Tuple.from(42, FDBRecordVersion.complete(versionstamp, 2).toVersionstamp(), 1776L), Tuple.from(43, FDBRecordVersion.complete(versionstamp, 0).toVersionstamp(), 1066L)), recordStore.scanIndex(metaData.getIndex("MySimpleRecord$num2-version"), IndexScanType.BY_VALUE, TupleRange.ALL, null, ScanProperties.FORWARD_SCAN).map(IndexEntry::getKey).asList().join());
    }
}
Also used : MySimpleRecord(com.apple.foundationdb.record.TestRecords1Proto.MySimpleRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBRecordVersion(com.apple.foundationdb.record.provider.foundationdb.FDBRecordVersion) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 99 with FDBRecordContext

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

the class VersionIndexTest method assertMaxVersionWithExtraColumn.

@SuppressWarnings("try")
private void assertMaxVersionWithExtraColumn(int column, @Nonnull FDBRecordVersion recordVersion) {
    try (FDBRecordContext context = openContext(maxEverVersionWithExtraColumnHook)) {
        Index index = metaData.getIndex("max_ever_version_with_extra_column");
        IndexEntry entry = new IndexEntry(index, Key.Evaluated.EMPTY, Key.Evaluated.concatenate(column, recordVersion));
        assertMaxVersionEntries(index, Collections.singletonList(entry));
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) IndexEntry(com.apple.foundationdb.record.IndexEntry) Index(com.apple.foundationdb.record.metadata.Index)

Example 100 with FDBRecordContext

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

the class VersionIndexTest method scanWithIncompleteVersion.

@ParameterizedTest(name = "saveLoadWithRepeatedVersion [formatVersion = {0}, splitLongRecords = {1}]")
@MethodSource("formatVersionArguments")
@SuppressWarnings("try")
public void scanWithIncompleteVersion(int testFormatVersion, boolean testSplitLongRecords) {
    formatVersion = testFormatVersion;
    splitLongRecords = testSplitLongRecords;
    MySimpleRecord record1 = MySimpleRecord.newBuilder().setRecNo(1066L).build();
    MySimpleRecord record2 = MySimpleRecord.newBuilder().setRecNo(1415L).build();
    byte[] globalVersion;
    List<FDBStoredRecord<Message>> savedRecords;
    try (FDBRecordContext context = openContext(simpleVersionHook)) {
        FDBStoredRecord<Message> storedRecord1 = recordStore.saveRecord(record1);
        FDBStoredRecord<Message> storedRecord2 = recordStore.saveRecord(record2);
        List<FDBStoredRecord<Message>> scannedRecords = recordStore.scanRecords(null, ScanProperties.FORWARD_SCAN).asList().join();
        assertEquals(Arrays.asList(storedRecord1, storedRecord2), scannedRecords);
        scannedRecords = recordStore.scanRecords(null, ScanProperties.REVERSE_SCAN).asList().join();
        assertEquals(Arrays.asList(storedRecord2, storedRecord1), scannedRecords);
        context.commit();
        globalVersion = context.getVersionStamp();
        assertNotNull(globalVersion);
        assertNotNull(storedRecord1.getVersion());
        assertNotNull(storedRecord2.getVersion());
        savedRecords = Arrays.asList(storedRecord1.withVersion(FDBRecordVersion.complete(globalVersion, storedRecord1.getVersion().getLocalVersion())), storedRecord2.withVersion(FDBRecordVersion.complete(globalVersion, storedRecord2.getVersion().getLocalVersion())));
    }
    try (FDBRecordContext context = openContext(simpleVersionHook)) {
        List<FDBStoredRecord<Message>> scannedRecords = recordStore.scanRecords(null, ScanProperties.FORWARD_SCAN).asList().join();
        assertEquals(savedRecords, scannedRecords);
        scannedRecords = recordStore.scanRecords(null, ScanProperties.REVERSE_SCAN).asList().join();
        assertEquals(Lists.reverse(savedRecords), scannedRecords);
        context.commit();
    }
}
Also used : MySimpleRecord(com.apple.foundationdb.record.TestRecords1Proto.MySimpleRecord) FDBStoredRecord(com.apple.foundationdb.record.provider.foundationdb.FDBStoredRecord) Message(com.google.protobuf.Message) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)542 Test (org.junit.jupiter.api.Test)365 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)226 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)215 Message (com.google.protobuf.Message)187 FDBQueriedRecord (com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord)170 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)165 Tuple (com.apple.foundationdb.tuple.Tuple)147 Tag (org.junit.jupiter.api.Tag)136 Tags (com.apple.test.Tags)129 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)123 List (java.util.List)121 Index (com.apple.foundationdb.record.metadata.Index)119 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)114 ArrayList (java.util.ArrayList)112 Collections (java.util.Collections)100 Query (com.apple.foundationdb.record.query.expressions.Query)97 RecordQueryPlanner (com.apple.foundationdb.record.query.plan.RecordQueryPlanner)96 Arrays (java.util.Arrays)94 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)93