use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordVersion in project fdb-record-layer by FoundationDB.
the class VersionIndexTest method maxEverVersionWithinTransaction.
@ParameterizedTest(name = "maxEverVersionWithinTransaction [formatVersion = {0}, splitLongRecords = {1}]")
@MethodSource("formatVersionArguments")
public void maxEverVersionWithinTransaction(int testFormatVersion, boolean testSplitLongRecords) {
formatVersion = testFormatVersion;
splitLongRecords = testSplitLongRecords;
// Add two records in the same commit with different local versions to ensure the one with the higher one is written
FDBRecordVersion expectedMaxVersion;
try (FDBRecordContext context = openContext(maxEverVersionHook)) {
MySimpleRecord record1 = MySimpleRecord.newBuilder().setRecNo(1215L).build();
MySimpleRecord record2 = MySimpleRecord.newBuilder().setRecNo(1815L).build();
recordStore.saveRecord(record1, FDBRecordVersion.incomplete(42));
recordStore.saveRecord(record2, FDBRecordVersion.incomplete(13));
context.commit();
assertNotNull(context.getVersionStamp());
expectedMaxVersion = FDBRecordVersion.complete(context.getVersionStamp(), 42);
}
assertMaxVersion(expectedMaxVersion);
// but the incomplete one is chosen anyway.
try (FDBRecordContext context = openContext(maxEverVersionHook)) {
MySimpleRecord record3 = MySimpleRecord.newBuilder().setRecNo(1066L).build();
MySimpleRecord record4 = MySimpleRecord.newBuilder().setRecNo(1415L).build();
FDBStoredRecord<?> storedRecord3 = recordStore.saveRecord(record3);
assertNotNull(storedRecord3.getVersion());
FDBRecordVersion version4 = getBiggerVersion(expectedMaxVersion);
FDBStoredRecord<?> storedRecord4 = recordStore.saveRecord(record4, version4);
context.commit();
assertNotNull(context.getVersionStamp());
FDBRecordVersion version3 = storedRecord3.getVersion().withCommittedVersion(context.getVersionStamp());
Assumptions.assumeTrue(version3.compareTo(version4) < 0, "committed version should be less than incremented version");
expectedMaxVersion = version3;
}
assertMaxVersion(expectedMaxVersion);
// Same as above, but write the record that should have the maximum version first as it shouldn't matter.
try (FDBRecordContext context = openContext(maxEverVersionHook)) {
MySimpleRecord record5 = MySimpleRecord.newBuilder().setRecNo(1564L).build();
MySimpleRecord record6 = MySimpleRecord.newBuilder().setRecNo(1455L).build();
FDBRecordVersion version5 = getBiggerVersion(expectedMaxVersion);
recordStore.saveRecord(record5, version5);
FDBStoredRecord<?> storedRecord6 = recordStore.saveRecord(record6);
assertNotNull(storedRecord6.getVersion());
context.commit();
assertNotNull(context.getVersionStamp());
FDBRecordVersion version6 = storedRecord6.getVersion().withCommittedVersion(context.getVersionStamp());
Assumptions.assumeTrue(version6.compareTo(version5) < 0, "committed version should be less than incremented version");
expectedMaxVersion = version6;
}
assertMaxVersion(expectedMaxVersion);
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordVersion in project fdb-record-layer by FoundationDB.
the class VersionIndexTest method withMetaDataRebuilds.
@ParameterizedTest(name = "withMetaDataRebuilds [formatVersion = {0}, splitLongRecords = {1}]")
@MethodSource("formatVersionArguments")
@SuppressWarnings("try")
public void withMetaDataRebuilds(int testFormatVersion, boolean testSplitLongRecords) {
formatVersion = testFormatVersion;
splitLongRecords = testSplitLongRecords;
RecordMetaDataHook firstHook = metaDataBuilder -> {
metaDataBuilder.setSplitLongRecords(splitLongRecords);
metaDataBuilder.addUniversalIndex(new Index("globalCount", new GroupingKeyExpression(EmptyKeyExpression.EMPTY, 0), IndexTypes.COUNT));
metaDataBuilder.addUniversalIndex(new Index("globalVersion", VersionKeyExpression.VERSION, IndexTypes.VERSION));
};
RecordMetaDataHook secondHook = metaDataBuilder -> {
firstHook.apply(metaDataBuilder);
metaDataBuilder.removeIndex("globalVersion");
metaDataBuilder.setStoreRecordVersions(false);
};
RecordMetaDataHook thirdHook = metaDataBuilder -> {
secondHook.apply(metaDataBuilder);
metaDataBuilder.setStoreRecordVersions(true);
metaDataBuilder.addUniversalIndex(new Index("globalVersion2", VersionKeyExpression.VERSION, IndexTypes.VERSION));
};
MySimpleRecord record1 = MySimpleRecord.newBuilder().setRecNo(1066L).build();
FDBRecordVersion version1;
MySimpleRecord record2 = MySimpleRecord.newBuilder().setRecNo(1776L).build();
MySimpleRecord record3 = MySimpleRecord.newBuilder().setRecNo(1955L).build();
FDBRecordVersion version3;
RecordQuery query = RecordQuery.newBuilder().setSort(VersionKeyExpression.VERSION).build();
// First with version on.
try (FDBRecordContext context = openContext(firstHook)) {
FDBStoredRecord<?> storedRecord = recordStore.saveRecord(record1);
assertTrue(storedRecord.hasVersion());
context.commit();
version1 = FDBRecordVersion.complete(context.getVersionStamp(), storedRecord.getVersion().getLocalVersion());
}
try (FDBRecordContext context = openContext(firstHook)) {
FDBStoredRecord<?> loadedRecord = recordStore.loadRecord(Tuple.from(1066L));
assertNotNull(loadedRecord);
assertTrue(loadedRecord.hasVersion());
assertEquals(version1, loadedRecord.getVersion());
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, indexScan("globalVersion"));
List<FDBQueriedRecord<Message>> records = recordStore.executeQuery(plan).asList().join();
assertEquals(1, records.size());
FDBQueriedRecord<Message> queriedRecord = records.get(0);
assertEquals(Tuple.from(1066L), queriedRecord.getPrimaryKey());
assertTrue(queriedRecord.hasVersion());
assertEquals(version1, queriedRecord.getVersion());
}
// Now with version off.
try (FDBRecordContext context = openContext(secondHook)) {
FDBStoredRecord<?> storedRecord = recordStore.saveRecord(record2);
assertFalse(storedRecord.hasVersion());
context.commit();
}
try (FDBRecordContext context = openContext(secondHook)) {
FDBStoredRecord<?> loadedRecord1 = recordStore.loadRecord(Tuple.from(1066L));
assertNotNull(loadedRecord1);
assertEquals(testFormatVersion >= FDBRecordStore.SAVE_VERSION_WITH_RECORD_FORMAT_VERSION, loadedRecord1.hasVersion());
FDBStoredRecord<?> loadedRecord2 = recordStore.loadRecord(Tuple.from(1776L));
assertNotNull(loadedRecord2);
assertFalse(loadedRecord2.hasVersion());
assertThrows(RecordCoreException.class, () -> {
RecordQueryPlan plan = planner.plan(query);
fail("Came up with plan " + plan.toString() + " when it should be impossible");
});
}
// Now with version back on.
try (FDBRecordContext context = openContext(thirdHook)) {
FDBStoredRecord<?> storedRecord = recordStore.saveRecord(record3);
assertTrue(storedRecord.hasVersion());
context.commit();
version3 = FDBRecordVersion.complete(context.getVersionStamp(), storedRecord.getVersion().getLocalVersion());
}
try (FDBRecordContext context = openContext(thirdHook)) {
FDBStoredRecord<?> loadedRecord1 = recordStore.loadRecord(Tuple.from(1066L));
assertEquals(testFormatVersion >= FDBRecordStore.SAVE_VERSION_WITH_RECORD_FORMAT_VERSION, loadedRecord1.hasVersion());
FDBStoredRecord<?> loadedRecord2 = recordStore.loadRecord(Tuple.from(1776L));
assertFalse(loadedRecord2.hasVersion());
FDBStoredRecord<?> loadedRecord3 = recordStore.loadRecord(Tuple.from(1955L));
assertTrue(loadedRecord3.hasVersion());
assertEquals(version3, loadedRecord3.getVersion());
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, indexScan("globalVersion2"));
List<FDBQueriedRecord<Message>> records = recordStore.executeQuery(plan).asList().join();
assertEquals(3, records.size());
if (testFormatVersion < FDBRecordStore.SAVE_VERSION_WITH_RECORD_FORMAT_VERSION) {
FDBQueriedRecord<Message> queriedRecord1 = records.get(0);
assertEquals(Tuple.from(1066L), queriedRecord1.getPrimaryKey());
assertFalse(queriedRecord1.hasVersion());
FDBQueriedRecord<Message> queriedRecord2 = records.get(1);
assertEquals(Tuple.from(1776L), queriedRecord2.getPrimaryKey());
assertFalse(queriedRecord2.hasVersion());
} else {
FDBQueriedRecord<Message> queriedRecord1 = records.get(0);
assertEquals(Tuple.from(1776L), queriedRecord1.getPrimaryKey());
assertFalse(queriedRecord1.hasVersion());
FDBQueriedRecord<Message> queriedRecord2 = records.get(1);
assertEquals(Tuple.from(1066L), queriedRecord2.getPrimaryKey());
assertTrue(queriedRecord2.hasVersion());
assertEquals(version1, queriedRecord2.getVersion());
}
FDBQueriedRecord<Message> queriedRecord3 = records.get(2);
assertEquals(Tuple.from(1955L), queriedRecord3.getPrimaryKey());
assertTrue(queriedRecord3.hasVersion());
assertEquals(version3, queriedRecord3.getVersion());
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordVersion in project fdb-record-layer by FoundationDB.
the class VersionIndexTest method upgradeFormatVersions.
@ParameterizedTest(name = "upgradeFormatVersions [formatVersion = {0}, splitLongRecords = {1}]")
@MethodSource("formatVersionArguments")
@SuppressWarnings("try")
public void upgradeFormatVersions(int testFormatVersion, boolean splitLongRecords) {
formatVersion = testFormatVersion;
final RecordMetaDataHook hook = metaDataBuilder -> {
simpleVersionHook.apply(metaDataBuilder);
metaDataBuilder.setSplitLongRecords(splitLongRecords);
};
final List<Message> records = Arrays.asList(MySimpleRecord.newBuilder().setRecNo(1066L).setNumValue2(1).build(), MySimpleRecord.newBuilder().setRecNo(1415L).setNumValue2(1).build(), MySimpleRecord.newBuilder().setRecNo(1776L).setNumValue2(2).build());
List<FDBStoredRecord<Message>> storedRecords;
try (FDBRecordContext context = openContext(hook)) {
List<FDBStoredRecord<Message>> storedRecordsWithIncompletes = records.stream().map(recordStore::saveRecord).collect(Collectors.toList());
context.commit();
byte[] globalVersion = context.getVersionStamp();
storedRecords = storedRecordsWithIncompletes.stream().map(record -> record.withCommittedVersion(globalVersion)).collect(Collectors.toList());
}
try (FDBRecordContext context = openContext(hook)) {
if (testFormatVersion < FDBRecordStore.SAVE_VERSION_WITH_RECORD_FORMAT_VERSION) {
validateUsingOlderVersionFormat(storedRecords);
} else {
validateUsingNewerVersionFormat(storedRecords);
}
}
// Update to the current format version
formatVersion = FDBRecordStore.MAX_SUPPORTED_FORMAT_VERSION;
if (testFormatVersion < FDBRecordStore.SAVE_VERSION_WITH_RECORD_FORMAT_VERSION && (splitLongRecords || testFormatVersion >= FDBRecordStore.SAVE_UNSPLIT_WITH_SUFFIX_FORMAT_VERSION)) {
// After format version upgrade, each record should now store that it has the version inlined
storedRecords = storedRecords.stream().map(record -> new FDBStoredRecord<>(record.getPrimaryKey(), record.getRecordType(), record.getRecord(), record.getKeyCount() + 1, record.getKeySize() * 2 + 1, record.getValueSize() + 1 + FDBRecordVersion.VERSION_LENGTH, record.isSplit(), true, record.getVersion())).collect(Collectors.toList());
}
try (FDBRecordContext context = openContext(hook)) {
if (!splitLongRecords && testFormatVersion < FDBRecordStore.SAVE_UNSPLIT_WITH_SUFFIX_FORMAT_VERSION) {
validateUsingOlderVersionFormat(storedRecords);
} else {
validateUsingNewerVersionFormat(storedRecords);
}
for (FDBStoredRecord<Message> storedRecord : storedRecords) {
Optional<FDBRecordVersion> loadedVersion = recordStore.loadRecordVersion(storedRecord.getPrimaryKey());
assertTrue(loadedVersion.isPresent());
assertEquals(storedRecord.getVersion(), loadedVersion.get());
RecordQuery query = RecordQuery.newBuilder().setFilter(Query.version().equalsValue(storedRecord.getVersion())).build();
RecordQueryPlan plan = planner.plan(query);
final String endpointString = "[" + storedRecord.getVersion().toVersionstamp(false).toString() + "]";
assertThat(plan, indexScan(allOf(indexName("globalVersion"), bounds(hasTupleString("[" + endpointString + "," + endpointString + "]")))));
List<FDBStoredRecord<Message>> queriedRecords = recordStore.executeQuery(plan).map(FDBQueriedRecord::getStoredRecord).asList().join();
assertEquals(Collections.singletonList(storedRecord), queriedRecords);
}
assertTrue(recordStore.deleteRecord(storedRecords.get(0).getPrimaryKey()));
final List<FDBStoredRecord<Message>> fewerRecords = storedRecords.subList(1, storedRecords.size());
if (!splitLongRecords && testFormatVersion < FDBRecordStore.SAVE_UNSPLIT_WITH_SUFFIX_FORMAT_VERSION) {
validateUsingOlderVersionFormat(fewerRecords);
} else {
validateUsingNewerVersionFormat(fewerRecords);
}
recordStore.saveRecord(storedRecords.get(0).getRecord());
if (!splitLongRecords && testFormatVersion < FDBRecordStore.SAVE_UNSPLIT_WITH_SUFFIX_FORMAT_VERSION) {
validateUsingOlderVersionFormat(fewerRecords);
} else {
validateUsingNewerVersionFormat(fewerRecords);
}
// do not commit (so we can do a second upgrade)
}
final Index newValueIndex = new Index("MySimpleRecord$num2", field("num_value_2"), IndexTypes.VALUE);
final Index newVersionIndex = new Index("MySimpleRecord$version-num2", concat(VersionKeyExpression.VERSION, field("num_value_2")), IndexTypes.VERSION);
final RecordMetaDataHook hookWithNewIndexes = metaDataBuilder -> {
hook.apply(metaDataBuilder);
metaDataBuilder.addIndex("MySimpleRecord", newValueIndex);
metaDataBuilder.addIndex("MySimpleRecord", newVersionIndex);
};
final List<FDBStoredRecord<Message>> newStoredRecords;
try (FDBRecordContext context = openContext(hookWithNewIndexes)) {
assertTrue(recordStore.getRecordStoreState().isReadable(newValueIndex));
boolean performedMigration = testFormatVersion < FDBRecordStore.SAVE_VERSION_WITH_RECORD_FORMAT_VERSION && (splitLongRecords || testFormatVersion >= FDBRecordStore.SAVE_UNSPLIT_WITH_SUFFIX_FORMAT_VERSION);
assertNotEquals(performedMigration, recordStore.getRecordStoreState().isReadable(newVersionIndex));
if (recordStore.getRecordStoreState().isReadable(newVersionIndex)) {
// Validate versions are the same for all records in index and in primary store
List<Pair<Tuple, FDBRecordVersion>> recordScannedValues = recordStore.scanRecords(null, ScanProperties.FORWARD_SCAN).map(record -> Pair.of(record.getPrimaryKey(), record.getVersion())).asList().join();
List<Pair<Tuple, FDBRecordVersion>> indexedScannedValues = recordStore.scanIndex(newVersionIndex, IndexScanType.BY_VALUE, TupleRange.ALL, null, ScanProperties.FORWARD_SCAN).map(indexEntry -> Pair.of(TupleHelpers.subTuple(indexEntry.getKey(), 2, indexEntry.getKey().size()), FDBRecordVersion.fromVersionstamp(indexEntry.getKey().getVersionstamp(0), false))).asList().join();
assertEquals(recordScannedValues, indexedScannedValues);
}
// Save record at newer version
assertTrue(recordStore.deleteRecord(storedRecords.get(0).getPrimaryKey()));
FDBStoredRecord<Message> newRecord0 = recordStore.saveRecord(storedRecords.get(0).getRecord());
FDBStoredRecord<Message> newRecord2 = recordStore.saveRecord(storedRecords.get(2).getRecord());
assertEquals(newRecord0.getVersion(), recordStore.loadRecordVersion(newRecord0.getPrimaryKey()).get());
assertEquals(newRecord2.getVersion(), recordStore.loadRecordVersion(newRecord2.getPrimaryKey()).get());
context.commit();
byte[] versionstamp = context.getVersionStamp();
newStoredRecords = Arrays.asList(newRecord0.withVersion(FDBRecordVersion.complete(versionstamp, 0)), storedRecords.get(1), newRecord2.withVersion(FDBRecordVersion.complete(versionstamp, 1)));
}
try (FDBRecordContext context = openContext(hookWithNewIndexes)) {
if (!splitLongRecords && testFormatVersion < FDBRecordStore.SAVE_UNSPLIT_WITH_SUFFIX_FORMAT_VERSION) {
validateUsingOlderVersionFormat(newStoredRecords);
} else {
validateUsingNewerVersionFormat(newStoredRecords);
}
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordVersion in project fdb-record-layer by FoundationDB.
the class VersionIndexTest method saveLoadWithVersion.
@ParameterizedTest(name = "saveLoadWithVersion [formatVersion = {0}, splitLongRecords = {1}]")
@MethodSource("formatVersionArguments")
@SuppressWarnings("try")
public void saveLoadWithVersion(int testFormatVersion, boolean testSplitLongRecords) {
formatVersion = testFormatVersion;
splitLongRecords = testSplitLongRecords;
MySimpleRecord record1 = MySimpleRecord.newBuilder().setRecNo(1066L).setNumValue2(42).build();
TestRecords1Proto.MyOtherRecord record2 = TestRecords1Proto.MyOtherRecord.newBuilder().setRecNo(1776L).setNumValue2(1729).build();
byte[] versionstamp;
try (FDBRecordContext context = openContext(simpleVersionHook)) {
recordStore.saveRecord(record1);
recordStore.saveRecord(record2);
context.commit();
versionstamp = context.getVersionStamp();
assertEquals(2, context.claimLocalVersion());
}
FDBRecordVersion version1;
FDBRecordVersion version2;
try (FDBRecordContext context = openContext(simpleVersionHook)) {
FDBStoredRecord<Message> stored1 = recordStore.loadRecord(Tuple.from(1066L));
assertTrue(stored1.hasVersion());
version1 = stored1.getVersion();
assertNotNull(version1);
assertTrue(version1.isComplete());
assertArrayEquals(versionstamp, version1.getGlobalVersion());
assertEquals(0, version1.getLocalVersion());
FDBStoredRecord<Message> stored2 = recordStore.loadRecord(Tuple.from(1776L));
assertTrue(stored2.hasVersion());
version2 = stored2.getVersion();
assertNotNull(version2);
assertTrue(version2.isComplete());
assertArrayEquals(versionstamp, version2.getGlobalVersion());
assertEquals(1, version2.getLocalVersion());
}
// Saving them again should change the versions.
try (FDBRecordContext context = openContext(simpleVersionHook)) {
recordStore.saveRecord(record1);
recordStore.saveRecord(record2);
context.commit();
versionstamp = context.getVersionStamp();
assertEquals(2, context.claimLocalVersion());
}
try (FDBRecordContext context = openContext(simpleVersionHook)) {
FDBStoredRecord<Message> stored1 = recordStore.loadRecord(Tuple.from(1066L));
assertTrue(stored1.hasVersion());
FDBRecordVersion version1Prime = stored1.getVersion();
assertNotNull(version1Prime);
assertTrue(version1Prime.isComplete());
assertEquals(0, version1Prime.getLocalVersion());
assertArrayEquals(versionstamp, version1Prime.getGlobalVersion());
assertFalse(Arrays.equals(version1.getGlobalVersion(), version1Prime.getGlobalVersion()));
assertNotEquals(version1, version1Prime);
FDBStoredRecord<Message> stored2 = recordStore.loadRecord(Tuple.from(1776L));
assertTrue(stored1.hasVersion());
FDBRecordVersion version2Prime = stored2.getVersion();
assertNotNull(version2Prime);
assertTrue(version2Prime.isComplete());
assertEquals(1, version2Prime.getLocalVersion());
assertArrayEquals(versionstamp, version2Prime.getGlobalVersion());
assertFalse(Arrays.equals(version2.getGlobalVersion(), version2Prime.getGlobalVersion()));
assertNotEquals(version2, version2Prime);
}
// Saving them again with an explicit version should keep the version the same.
try (FDBRecordContext context = openContext(simpleVersionHook)) {
recordStore.saveRecord(record1, version1);
recordStore.saveRecord(record2, version2);
context.commit();
versionstamp = context.getVersionStamp();
assertEquals(0, context.claimLocalVersion());
}
try (FDBRecordContext context = openContext(simpleVersionHook)) {
FDBStoredRecord<Message> stored1 = recordStore.loadRecord(Tuple.from(1066L));
assertTrue(stored1.hasVersion());
FDBRecordVersion version1Prime = stored1.getVersion();
assertNotNull(version1Prime);
assertNotSame(version1, version1Prime);
assertTrue(version1Prime.isComplete());
assertEquals(0, version1Prime.getLocalVersion());
assertFalse(Arrays.equals(versionstamp, version1Prime.getGlobalVersion()));
assertArrayEquals(version1.getGlobalVersion(), version1Prime.getGlobalVersion());
assertEquals(version1, version1Prime);
FDBStoredRecord<Message> stored2 = recordStore.loadRecord(Tuple.from(1776L));
assertTrue(stored2.hasVersion());
FDBRecordVersion version2Prime = stored2.getVersion();
assertNotNull(version2Prime);
assertNotSame(version2, version2Prime);
assertTrue(version2Prime.isComplete());
assertEquals(1, version2Prime.getLocalVersion());
assertFalse(Arrays.equals(versionstamp, version2Prime.getGlobalVersion()));
assertArrayEquals(version2.getGlobalVersion(), version2Prime.getGlobalVersion());
assertEquals(version2, version2Prime);
}
// Saving new records with an explicit NO_VERSION behavior, should set the new records version to null
MySimpleRecord record3 = MySimpleRecord.newBuilder().setRecNo(3066L).setNumValue2(42).build();
TestRecords1Proto.MyOtherRecord record4 = TestRecords1Proto.MyOtherRecord.newBuilder().setRecNo(4776L).setNumValue2(1729).build();
try (FDBRecordContext context = openContext(simpleVersionHook)) {
assertThrows(RecordCoreException.class, () -> {
recordStore.saveRecord(record3, version2, FDBRecordStoreBase.VersionstampSaveBehavior.NO_VERSION);
fail("Save record with NO_VERSION behavior should throw an exception if the supplied version isn't null");
});
}
try (FDBRecordContext context = openContext(simpleVersionHook)) {
recordStore.saveRecord(record3, null, FDBRecordStoreBase.VersionstampSaveBehavior.NO_VERSION);
recordStore.saveRecord(record4, null, FDBRecordStoreBase.VersionstampSaveBehavior.NO_VERSION);
context.commit();
}
try (FDBRecordContext context = openContext(simpleVersionHook)) {
FDBStoredRecord<Message> stored3 = recordStore.loadRecord(Tuple.from(3066L));
assertFalse(stored3.hasVersion());
FDBStoredRecord<Message> stored4 = recordStore.loadRecord(Tuple.from(4776L));
assertFalse(stored4.hasVersion());
}
// default behavior in this case).
try (FDBRecordContext context = openContext(simpleVersionHook)) {
recordStore.saveRecord(record3, null, FDBRecordStoreBase.VersionstampSaveBehavior.WITH_VERSION);
recordStore.saveRecord(record4, null, FDBRecordStoreBase.VersionstampSaveBehavior.WITH_VERSION);
context.commit();
versionstamp = context.getVersionStamp();
assertEquals(2, context.claimLocalVersion());
}
try (FDBRecordContext context = openContext(simpleVersionHook)) {
FDBStoredRecord<Message> stored3 = recordStore.loadRecord(Tuple.from(3066L));
assertTrue(stored3.hasVersion());
FDBRecordVersion version3Prime = stored3.getVersion();
assertNotNull(version3Prime);
assertTrue(version3Prime.isComplete());
assertEquals(0, version3Prime.getLocalVersion());
assertArrayEquals(versionstamp, version3Prime.getGlobalVersion());
FDBStoredRecord<Message> stored4 = recordStore.loadRecord(Tuple.from(4776L));
assertTrue(stored4.hasVersion());
FDBRecordVersion version4Prime = stored4.getVersion();
assertNotNull(version4Prime);
assertTrue(version4Prime.isComplete());
assertEquals(1, version4Prime.getLocalVersion());
assertArrayEquals(versionstamp, version4Prime.getGlobalVersion());
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordVersion 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());
}
}
Aggregations