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();
}
}
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());
}
}
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();
}
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);
}
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());
}
}
Aggregations