use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class FDBDatabaseRunnerTest method runDatabaseOperations.
@Test
public void runDatabaseOperations() {
try (FDBDatabaseRunner runner = database.newRunner()) {
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
testStoreAndRetrieveSimpleRecord(database, metaData);
runner.run(context -> {
FDBRecordStore store = FDBRecordStore.newBuilder().setMetaDataProvider(metaData).setContext(context).setKeySpacePath(TestKeySpace.getKeyspacePath(FDBRecordStoreTestBase.PATH_OBJECTS)).build();
store.deleteRecord(Tuple.from(1066L));
return null;
});
FDBStoredRecord<Message> retrieved2 = runner.run(context -> {
FDBRecordStore store = FDBRecordStore.newBuilder().setMetaDataProvider(metaData).setContext(context).setKeySpacePath(TestKeySpace.getKeyspacePath(FDBRecordStoreTestBase.PATH_OBJECTS)).build();
return store.loadRecord(Tuple.from(1066L));
});
assertNull(retrieved2);
}
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class FDBDatabaseTest method cachedVersionMaintenanceOnReadsTest.
@Test
public void cachedVersionMaintenanceOnReadsTest() throws Exception {
FDBDatabaseFactory factory = FDBDatabaseFactory.instance();
factory.setTrackLastSeenVersion(true);
FDBDatabase database = factory.getDatabase();
assertTrue(database.isTrackLastSeenVersionOnRead());
assertTrue(database.isTrackLastSeenVersionOnCommit());
// For the purpose of this test, commits will not change the cached read version
database.setTrackLastSeenVersionOnCommit(false);
assertFalse(database.isTrackLastSeenVersionOnCommit());
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
// First time, does a GRV from FDB
long readVersion1 = getReadVersion(database, 0L, 2000L);
// Store a record (advances future GRV, but not cached version)
testStoreAndRetrieveSimpleRecord(database, metaData);
// We're fine with any version obtained up to 2s ago, so will get readVersion
assertEquals(readVersion1, getReadVersion(database, 0L, 2000L));
Thread.sleep(10L);
// Set a short staleness bound, this will cause a GRV call to FDB and will update the cached version
long readVersion2 = getReadVersion(database, 0L, 11L);
assertTrue(readVersion1 < readVersion2);
// Store another record
testStoreAndRetrieveSimpleRecord(database, metaData);
assertEquals(readVersion2, getReadVersion(database, 0L, 2000L));
assertEquals(readVersion2, getReadVersion(database, readVersion2, 2000L));
// Now we want at least readVersion2 + 1, so this will cause a GRV
long readVersion3 = getReadVersion(database, readVersion2 + 1, 2000L);
assertTrue(readVersion2 < readVersion3);
// Store another record
testStoreAndRetrieveSimpleRecord(database, metaData);
// Don't use a stored version
assertTrue(readVersion3 < getReadVersion(database, null, null));
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class FDBDatabaseTest method cachedVersionMaintenanceOnCommitTest.
@Test
public void cachedVersionMaintenanceOnCommitTest() {
FDBDatabaseFactory factory = FDBDatabaseFactory.instance();
factory.setTrackLastSeenVersion(true);
FDBDatabase database = factory.getDatabase();
assertTrue(database.isTrackLastSeenVersionOnRead());
assertTrue(database.isTrackLastSeenVersionOnCommit());
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
// First time, does a GRV from FDB
long readVersion1 = getReadVersion(database, 0L, 2000L);
// Store a record (advances future GRV, but not cached version)
testStoreAndRetrieveSimpleRecord(database, metaData);
// We're fine with any version obtained up to 5s ago, but storing the record updated the cached version so we'll get a newer one
long readVersion2 = getReadVersion(database, 0L, 5000L);
assertTrue(readVersion1 < readVersion2);
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method localMetaDataWithRenamed.
@Test
public void localMetaDataWithRenamed() throws Descriptors.DescriptorValidationException {
// Rename a record type
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
RecordMetaDataProto.MetaData.Builder protoBuilder = metaData.toProto().toBuilder();
MetaDataProtoEditor.renameRecordType(protoBuilder, "MySimpleRecord", "MyNewSimpleRecord");
Descriptors.FileDescriptor updatedFile = Descriptors.FileDescriptor.buildFrom(protoBuilder.getRecords(), TestRecords1Proto.getDescriptor().getDependencies().toArray(new Descriptors.FileDescriptor[0]));
// Validate that the type name change happened
assertNull(updatedFile.findMessageTypeByName("MySimpleRecord"));
Descriptors.Descriptor newSimpleRecordDescriptor = updatedFile.findMessageTypeByName("MyNewSimpleRecord");
assertNotNull(newSimpleRecordDescriptor);
assertSame(newSimpleRecordDescriptor, updatedFile.findMessageTypeByName(RecordMetaDataBuilder.DEFAULT_UNION_NAME).findFieldByName("_MyNewSimpleRecord").getMessageType());
RecordMetaData metaData2 = RecordMetaData.newBuilder().setLocalFileDescriptor(updatedFile).setRecords(metaData.toProto()).build();
assertThrows(MetaDataException.class, () -> metaData2.getRecordType("MySimpleRecord"));
assertNotNull(metaData2.getRecordType("MyNewSimpleRecord"));
assertSame(newSimpleRecordDescriptor, metaData2.getRecordType("MyNewSimpleRecord").getDescriptor());
assertEquals(Collections.singletonList(metaData2.getRecordType("MyNewSimpleRecord")), metaData2.recordTypesForIndex(metaData2.getIndex("MySimpleRecord$str_value_indexed")));
assertSame(updatedFile.findMessageTypeByName(RecordMetaDataBuilder.DEFAULT_UNION_NAME), metaData2.getUnionDescriptor());
MetaDataException e = assertThrows(MetaDataException.class, metaData2::toProto);
assertEquals("cannot serialize meta-data with a local records descriptor to proto", e.getMessage());
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method indexOnPartialNestedPrimaryKey.
@ParameterizedTest(name = "indexOnPartialNestedPrimaryKey [indexCounterBasedSubspaceKey = {0}]")
@BooleanSource
public void indexOnPartialNestedPrimaryKey(final boolean indexCounterBasedSubspaceKey) {
RecordMetaDataBuilder builder = createBuilder(TestRecordsWithHeaderProto.getDescriptor(), indexCounterBasedSubspaceKey);
builder.getRecordType("MyRecord").setPrimaryKey(field("header").nest(concatenateFields("path", "rec_no")));
builder.addIndex("MyRecord", new Index("MyRecord$path_str", concat(field("header").nest("path"), field("str_value")), IndexTypes.VALUE));
RecordMetaData metaData = builder.getRecordMetaData();
Index index = metaData.getIndex("MyRecord$path_str");
assertNotNull(index);
assertNotNull(index.getPrimaryKeyComponentPositions());
assertArrayEquals(new int[] { 0, -1 }, index.getPrimaryKeyComponentPositions());
if (!indexCounterBasedSubspaceKey) {
assertEquals(index.getName(), index.getSubspaceKey());
} else {
assertEquals(1L, index.getSubspaceKey());
}
}
Aggregations