Search in sources :

Example 6 with FDBRecordVersion

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

the class VersionIndexTest method removeWithVersion.

@ParameterizedTest(name = "removeWithVersion [formatVersion = {0}, splitLongRecords = {1}]")
@MethodSource("formatVersionArguments")
public void removeWithVersion(int testFormatVersion, boolean testSplitLongRecords) {
    formatVersion = testFormatVersion;
    splitLongRecords = testSplitLongRecords;
    MySimpleRecord record = MySimpleRecord.newBuilder().setRecNo(1066L).setNumValue2(42).build();
    FDBStoredRecord<Message> storedRecord;
    byte[] versionstamp;
    try (FDBRecordContext context = openContext(simpleVersionHook)) {
        storedRecord = recordStore.saveRecord(record);
        context.commit();
        versionstamp = context.getVersionStamp();
        assertEquals(1, context.claimLocalVersion());
    }
    try (FDBRecordContext context = openContext(simpleVersionHook)) {
        RecordFunction<FDBRecordVersion> function = Query.version().getFunction();
        FDBRecordVersion version = recordStore.evaluateRecordFunction(function, storedRecord).join();
        assertNotNull(version);
        assertArrayEquals(versionstamp, version.getGlobalVersion());
        assertEquals(0, version.getLocalVersion());
        Optional<FDBRecordVersion> versionOptional = recordStore.loadRecordVersion(storedRecord.getPrimaryKey());
        assertTrue(versionOptional.isPresent());
        assertEquals(version, versionOptional.get());
        // Remove record saved within previous transaction
        assertTrue(recordStore.deleteRecord(Tuple.from(1066L)));
        version = recordStore.evaluateRecordFunction(function, storedRecord).join();
        assertNull(version);
        versionOptional = recordStore.loadRecordVersion(Tuple.from(1066L));
        assertFalse(versionOptional.isPresent());
        assertFalse(recordStore.deleteRecord(Tuple.from(1066L)));
        version = recordStore.evaluateRecordFunction(function, storedRecord).join();
        assertNull(version);
        versionOptional = recordStore.loadRecordVersion(Tuple.from(1066L));
        assertFalse(versionOptional.isPresent());
        // Save a new record and verify version removed with it after it is deleted
        MySimpleRecord record2 = record.toBuilder().setRecNo(1415L).build();
        FDBStoredRecord<Message> storedRecord2 = recordStore.saveRecord(record2);
        assertTrue(storedRecord2.hasVersion());
        assertFalse(storedRecord2.getVersion().isComplete());
        version = recordStore.evaluateRecordFunction(function, storedRecord2).join();
        assertNotNull(version);
        assertEquals(storedRecord2.getVersion(), version);
        versionOptional = recordStore.loadRecordVersion(Tuple.from(1415L));
        assertTrue(versionOptional.isPresent());
        assertEquals(version, versionOptional.get());
        assertTrue(recordStore.deleteRecord(Tuple.from(1415L)));
        version = recordStore.evaluateRecordFunction(function, storedRecord2).join();
        assertNull(version);
        versionOptional = recordStore.loadRecordVersion(Tuple.from(1415L));
        assertFalse(versionOptional.isPresent());
        context.commit();
    }
    try (FDBRecordContext context = openContext(simpleVersionHook)) {
        // Verify that the version added in the second record wasn't actually committed during the
        // pre-commit hook that writes all the versioned keys and values
        Optional<FDBRecordVersion> versionOptional = recordStore.loadRecordVersion(Tuple.from(1415L));
        assertFalse(versionOptional.isPresent());
        context.commit();
    }
}
Also used : MySimpleRecord(com.apple.foundationdb.record.TestRecords1Proto.MySimpleRecord) Message(com.google.protobuf.Message) 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 7 with FDBRecordVersion

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

the class VersionIndexTest method saveLoadWithRepeatedAndCompoundVersion.

@ParameterizedTest(name = "saveLoadWithRepeatedAndCompoundVersion [formatVersion = {0}, splitLongRecords = {1}]")
@MethodSource("formatVersionArguments")
@SuppressWarnings("try")
public void saveLoadWithRepeatedAndCompoundVersion(int testFormatVersion, boolean testSplitLongRecords) {
    formatVersion = testFormatVersion;
    splitLongRecords = testSplitLongRecords;
    MySimpleRecord simpleRecord1 = MySimpleRecord.newBuilder().setRecNo(1066L).addRepeater(1).addRepeater(2).addRepeater(3).build();
    MySimpleRecord simpleRecord2 = MySimpleRecord.newBuilder().setRecNo(1729L).addRepeater(1).build();
    MySimpleRecord simpleRecord3 = MySimpleRecord.newBuilder().setRecNo(1776L).build();
    byte[] versionstamp;
    try (FDBRecordContext context = openContext(repeatedAndCompoundVersionHook)) {
        recordStore.saveRecord(simpleRecord1);
        recordStore.saveRecord(simpleRecord2);
        recordStore.saveRecord(simpleRecord3);
        context.commit();
        versionstamp = context.getVersionStamp();
        assertEquals(3, context.claimLocalVersion());
    }
    try (FDBRecordContext context = openContext(repeatedAndCompoundVersionHook)) {
        FDBStoredRecord<Message> stored1 = recordStore.loadRecord(Tuple.from(1066L));
        assertTrue(stored1.hasVersion());
        FDBRecordVersion version1 = stored1.getVersion();
        assertNotNull(version1);
        assertArrayEquals(versionstamp, version1.getGlobalVersion());
        assertEquals(0, version1.getLocalVersion());
        FDBStoredRecord<Message> stored2 = recordStore.loadRecord(Tuple.from(1729L));
        assertTrue(stored2.hasVersion());
        FDBRecordVersion version2 = stored2.getVersion();
        assertNotNull(version2);
        assertArrayEquals(versionstamp, version2.getGlobalVersion());
        assertEquals(1, version2.getLocalVersion());
        FDBStoredRecord<Message> stored3 = recordStore.loadRecord(Tuple.from(1776L));
        assertTrue(stored3.hasVersion());
        FDBRecordVersion version3 = stored3.getVersion();
        assertNotNull(version3);
        assertArrayEquals(versionstamp, version3.getGlobalVersion());
        assertEquals(2, version3.getLocalVersion());
    }
}
Also used : MySimpleRecord(com.apple.foundationdb.record.TestRecords1Proto.MySimpleRecord) Message(com.google.protobuf.Message) 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 8 with FDBRecordVersion

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

the class VersionIndexTest method validateUsingOlderVersionFormat.

private <M extends Message> void validateUsingOlderVersionFormat(@Nonnull List<FDBStoredRecord<M>> storedRecords) {
    // Make sure all of the records have versions in the old keyspace
    final Subspace legacyVersionSubspace = recordStore.getLegacyVersionSubspace();
    RecordCursorIterator<Pair<Tuple, FDBRecordVersion>> versionKeyPairs = KeyValueCursor.Builder.withSubspace(legacyVersionSubspace).setContext(recordStore.getRecordContext()).setScanProperties(ScanProperties.FORWARD_SCAN).build().map(kv -> Pair.of(legacyVersionSubspace.unpack(kv.getKey()), FDBRecordVersion.fromBytes(kv.getValue()))).asIterator();
    for (FDBStoredRecord<M> storedRecord : storedRecords) {
        assertTrue(versionKeyPairs.hasNext());
        Pair<Tuple, FDBRecordVersion> versionPair = versionKeyPairs.next();
        assertEquals(storedRecord.getPrimaryKey(), versionPair.getLeft());
        assertEquals(storedRecord.getVersion(), versionPair.getRight());
    }
    assertFalse(versionKeyPairs.hasNext());
    // Validate that no value in the record subspace begins with the type code for versionstamps
    final Subspace recordsSubspace = recordStore.recordsSubspace();
    KeyValueCursor.Builder.withSubspace(recordsSubspace).setContext(recordStore.getRecordContext()).setScanProperties(ScanProperties.FORWARD_SCAN).build().forEach(kv -> assertNotEquals(VERSIONSTAMP_CODE, kv.getValue()[0])).join();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) FDBRecord(com.apple.foundationdb.record.provider.foundationdb.FDBRecord) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Subspace(com.apple.foundationdb.subspace.Subspace) IndexScanType(com.apple.foundationdb.record.IndexScanType) Pair(org.apache.commons.lang3.tuple.Pair) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) VersionKeyExpression(com.apple.foundationdb.record.metadata.expressions.VersionKeyExpression) Map(java.util.Map) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Tag(org.junit.jupiter.api.Tag) Query(com.apple.foundationdb.record.query.expressions.Query) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) Matchers.allOf(org.hamcrest.Matchers.allOf) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Arguments(org.junit.jupiter.params.provider.Arguments) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) TupleRange(com.apple.foundationdb.record.TupleRange) Stream(java.util.stream.Stream) PlanMatchers.indexName(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexName) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Expressions.function(com.apple.foundationdb.record.metadata.Key.Expressions.function) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) FunctionKeyExpression(com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) MySimpleRecord(com.apple.foundationdb.record.TestRecords1Proto.MySimpleRecord) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) FDBTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBTestBase) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Matchers.lessThan(org.hamcrest.Matchers.lessThan) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nullable(javax.annotation.Nullable) FDBStoredRecord(com.apple.foundationdb.record.provider.foundationdb.FDBStoredRecord) Tags(com.apple.test.Tags) ExecutionException(java.util.concurrent.ExecutionException) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) TestRecords2Proto(com.apple.foundationdb.record.TestRecords2Proto) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Index(com.apple.foundationdb.record.metadata.Index) Assumptions(org.junit.jupiter.api.Assumptions) TreeMap(java.util.TreeMap) AutoService(com.google.auto.service.AutoService) IndexEntry(com.apple.foundationdb.record.IndexEntry) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBRecordVersion(com.apple.foundationdb.record.provider.foundationdb.FDBRecordVersion) PlanMatchers.bounds(com.apple.foundationdb.record.query.plan.match.PlanMatchers.bounds) RecordQueryPlanner(com.apple.foundationdb.record.query.plan.RecordQueryPlanner) Tuple(com.apple.foundationdb.tuple.Tuple) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) FDBRecordStoreBase(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase) MethodSource(org.junit.jupiter.params.provider.MethodSource) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) KeyValueCursor(com.apple.foundationdb.record.provider.foundationdb.KeyValueCursor) List(java.util.List) ObjectPlanHash(com.apple.foundationdb.record.ObjectPlanHash) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Optional(java.util.Optional) SortedMap(java.util.SortedMap) IntStream(java.util.stream.IntStream) PlanMatchers.indexScan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexScan) HashMap(java.util.HashMap) TestKeySpace(com.apple.foundationdb.record.provider.foundationdb.TestKeySpace) PlanHashable(com.apple.foundationdb.record.PlanHashable) Key(com.apple.foundationdb.record.metadata.Key) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) FDBRecordStore(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStore) ScanProperties(com.apple.foundationdb.record.ScanProperties) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) Nonnull(javax.annotation.Nonnull) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) Versionstamp(com.apple.foundationdb.tuple.Versionstamp) Iterator(java.util.Iterator) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) RecordMetaDataHook(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreTestBase.RecordMetaDataHook) FDBDatabaseFactory(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory) Message(com.google.protobuf.Message) RecordCursor(com.apple.foundationdb.record.RecordCursor) RecordFunction(com.apple.foundationdb.record.RecordFunction) Collections(java.util.Collections) Subspace(com.apple.foundationdb.subspace.Subspace) Tuple(com.apple.foundationdb.tuple.Tuple) FDBRecordVersion(com.apple.foundationdb.record.provider.foundationdb.FDBRecordVersion) Pair(org.apache.commons.lang3.tuple.Pair)

Example 9 with FDBRecordVersion

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

the class VersionIndexTest method maxEverVersionWithExtraColumn.

@ParameterizedTest(name = "maxEverVersionWithExtraColumn [formatVersion = {0}, splitLongRecords = {1}]")
@MethodSource("formatVersionArguments")
public void maxEverVersionWithExtraColumn(int testFormatVersion, boolean testSplitLongRecords) {
    formatVersion = testFormatVersion;
    splitLongRecords = testSplitLongRecords;
    // Save a record with a fresh version
    FDBRecordVersion expectedMaxVersion;
    try (FDBRecordContext context = openContext(maxEverVersionWithExtraColumnHook)) {
        MySimpleRecord record1 = MySimpleRecord.newBuilder().setRecNo(1066L).setNumValue2(0).build();
        recordStore.saveRecord(record1);
        context.commit();
        assertNotNull(context.getVersionStamp());
        expectedMaxVersion = FDBRecordVersion.complete(context.getVersionStamp(), 0);
    }
    assertMaxVersionWithExtraColumn(0, expectedMaxVersion);
    // Should be no change
    try (FDBRecordContext context = openContext(maxEverVersionWithExtraColumnHook)) {
        MySimpleRecord record2 = MySimpleRecord.newBuilder().setRecNo(1415L).setNumValue2(-1).build();
        recordStore.saveRecord(record2, getBiggerVersion(expectedMaxVersion));
        context.commit();
    }
    assertMaxVersionWithExtraColumn(0, expectedMaxVersion);
    // Add a record with a larger value for the first column and a smaller value for the version
    // Should update to the new version
    FDBRecordVersion smallerVersion;
    try (FDBRecordContext context = openContext(maxEverVersionWithExtraColumnHook)) {
        MySimpleRecord record3 = MySimpleRecord.newBuilder().setRecNo(1863L).setNumValue2(1).build();
        smallerVersion = getSmallerVersion(expectedMaxVersion);
        recordStore.saveRecord(record3, smallerVersion);
        context.commit();
    }
    assertMaxVersionWithExtraColumn(1, smallerVersion);
    // the update even if that causes the extra column to go down.
    try (FDBRecordContext context = openContext(maxEverVersionWithExtraColumnHook)) {
        MySimpleRecord record4 = MySimpleRecord.newBuilder().setRecNo(1455L).setNumValue2(0).build();
        recordStore.saveRecord(record4);
        context.commit();
        assertNotNull(context.getVersionStamp());
        expectedMaxVersion = FDBRecordVersion.complete(context.getVersionStamp(), 0);
    }
    assertMaxVersionWithExtraColumn(0, expectedMaxVersion);
}
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 10 with FDBRecordVersion

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

the class VersionIndexTest method saveLoadWithRepeatedVersion.

@ParameterizedTest(name = "saveLoadWithRepeatedVersion [formatVersion = {0}, splitLongRecords = {1}]")
@MethodSource("formatVersionArguments")
@SuppressWarnings("try")
public void saveLoadWithRepeatedVersion(int testFormatVersion, boolean testSplitLongRecords) {
    formatVersion = testFormatVersion;
    splitLongRecords = testSplitLongRecords;
    MySimpleRecord simpleRecord1 = MySimpleRecord.newBuilder().setRecNo(1066L).addRepeater(1).addRepeater(2).addRepeater(3).build();
    MySimpleRecord simpleRecord2 = MySimpleRecord.newBuilder().setRecNo(1729L).addRepeater(1).build();
    MySimpleRecord simpleRecord3 = MySimpleRecord.newBuilder().setRecNo(1776L).build();
    byte[] versionstamp;
    try (FDBRecordContext context = openContext(repeatedVersionHook)) {
        recordStore.saveRecord(simpleRecord1);
        recordStore.saveRecord(simpleRecord2);
        recordStore.saveRecord(simpleRecord3);
        context.commit();
        versionstamp = context.getVersionStamp();
        assertEquals(3, context.claimLocalVersion());
    }
    try (FDBRecordContext context = openContext(repeatedVersionHook)) {
        FDBStoredRecord<Message> stored1 = recordStore.loadRecord(Tuple.from(1066L));
        assertTrue(stored1.hasVersion());
        FDBRecordVersion version1 = stored1.getVersion();
        assertNotNull(version1);
        assertArrayEquals(versionstamp, version1.getGlobalVersion());
        assertEquals(0, version1.getLocalVersion());
        FDBStoredRecord<Message> stored2 = recordStore.loadRecord(Tuple.from(1729L));
        assertTrue(stored2.hasVersion());
        FDBRecordVersion version2 = stored2.getVersion();
        assertNotNull(version2);
        assertArrayEquals(versionstamp, version2.getGlobalVersion());
        assertEquals(1, version2.getLocalVersion());
        FDBStoredRecord<Message> stored3 = recordStore.loadRecord(Tuple.from(1776L));
        assertTrue(stored3.hasVersion());
        FDBRecordVersion version3 = stored3.getVersion();
        assertNotNull(version3);
        assertArrayEquals(versionstamp, version3.getGlobalVersion());
        assertEquals(2, version3.getLocalVersion());
    }
}
Also used : MySimpleRecord(com.apple.foundationdb.record.TestRecords1Proto.MySimpleRecord) Message(com.google.protobuf.Message) 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)

Aggregations

FDBRecordVersion (com.apple.foundationdb.record.provider.foundationdb.FDBRecordVersion)25 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)23 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)21 MethodSource (org.junit.jupiter.params.provider.MethodSource)21 MySimpleRecord (com.apple.foundationdb.record.TestRecords1Proto.MySimpleRecord)20 Message (com.google.protobuf.Message)13 FDBRecordStore (com.apple.foundationdb.record.provider.foundationdb.FDBRecordStore)10 Tuple (com.apple.foundationdb.tuple.Tuple)9 TreeMap (java.util.TreeMap)9 IndexEntry (com.apple.foundationdb.record.IndexEntry)8 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)8 Index (com.apple.foundationdb.record.metadata.Index)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 SortedMap (java.util.SortedMap)8 PlanHashable (com.apple.foundationdb.record.PlanHashable)7 RecordCursorIterator (com.apple.foundationdb.record.RecordCursorIterator)7 IndexTypes (com.apple.foundationdb.record.metadata.IndexTypes)7 Tags (com.apple.test.Tags)6 Arrays (java.util.Arrays)6