Search in sources :

Example 36 with RecordMetaData

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);
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Message(com.google.protobuf.Message) Test(org.junit.jupiter.api.Test)

Example 37 with RecordMetaData

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));
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 38 with RecordMetaData

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);
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 39 with RecordMetaData

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());
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Descriptors(com.google.protobuf.Descriptors) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 40 with RecordMetaData

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());
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Aggregations

RecordMetaData (com.apple.foundationdb.record.RecordMetaData)168 Test (org.junit.jupiter.api.Test)130 RecordMetaDataBuilder (com.apple.foundationdb.record.RecordMetaDataBuilder)81 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)66 Nonnull (javax.annotation.Nonnull)47 Descriptors (com.google.protobuf.Descriptors)44 RecordMetaDataProto (com.apple.foundationdb.record.RecordMetaDataProto)39 MetaDataException (com.apple.foundationdb.record.metadata.MetaDataException)39 MetaDataProtoTest (com.apple.foundationdb.record.metadata.MetaDataProtoTest)38 Tuple (com.apple.foundationdb.tuple.Tuple)36 List (java.util.List)33 ArrayList (java.util.ArrayList)31 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)30 Index (com.apple.foundationdb.record.metadata.Index)30 DescriptorProtos (com.google.protobuf.DescriptorProtos)30 Collectors (java.util.stream.Collectors)30 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)27 ByteString (com.google.protobuf.ByteString)27 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)27 RecordMetaDataOptionsProto (com.apple.foundationdb.record.RecordMetaDataOptionsProto)26