use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreOpeningTest method sizeBasedUserVersionChecker.
@Test
public void sizeBasedUserVersionChecker() {
final Index universalCountIndex = new Index("countIndex", new GroupingKeyExpression(EmptyKeyExpression.EMPTY, 0), IndexTypes.COUNT);
final RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
metaDataBuilder.addUniversalIndex(universalCountIndex);
final RecordMetaData metaData1 = metaDataBuilder.getRecordMetaData();
// Open the store and make sure estimate size path is used
try (FDBRecordContext context = openContext()) {
final SizeBasedUserVersionChecker userVersionChecker = new SizeBasedUserVersionChecker(IndexState.READABLE);
final FDBRecordStore recordStore = storeBuilder(context, metaData1).setUserVersionChecker(userVersionChecker).create();
assertThat("should have checked the size at least once", userVersionChecker.getCheckSizeCount(), greaterThan(0L));
assertEquals(1, timer.getCount(FDBStoreTimer.Events.ESTIMATE_SIZE));
assertTrue(recordStore.getRecordStoreState().allIndexesReadable(), "all indexes should be readable on new store opening");
commit(context);
}
// Re-open the store and make sure the estimate is never used
try (FDBRecordContext context = openContext()) {
final SizeBasedUserVersionChecker userVersionChecker = new SizeBasedUserVersionChecker(IndexState.DISABLED);
final FDBRecordStore recordStore = storeBuilder(context, metaData1).setUserVersionChecker(userVersionChecker).open();
assertEquals(0, userVersionChecker.getCheckSizeCount(), "should not have checked the size on already created store");
assertEquals(0, timer.getCount(FDBStoreTimer.Events.ESTIMATE_SIZE), "should not have estimated the size on already created store");
assertTrue(recordStore.getRecordStoreState().allIndexesReadable(), "all indexes should be readable on already created store opening");
commit(context);
}
// Add two indexes
final String indexName1 = "index1";
final String indexName2 = "index2";
metaDataBuilder.addIndex("MySimpleRecord", indexName1, "num_value_2");
metaDataBuilder.addIndex("MySimpleRecord", indexName2, "num_value_2");
final RecordMetaData metaData2 = metaDataBuilder.getRecordMetaData();
try (FDBRecordContext context = openContext()) {
final SizeBasedUserVersionChecker userVersionChecker = new SizeBasedUserVersionChecker(IndexState.DISABLED);
final FDBRecordStore recordStore = storeBuilder(context, metaData2).setUserVersionChecker(userVersionChecker).open();
assertEquals(2, userVersionChecker.getCheckSizeCount(), "should have checked the size once for each index");
assertEquals(1, timer.getCount(FDBStoreTimer.Events.ESTIMATE_SIZE), "should have only checked the database for the size once");
assertEquals(ImmutableSet.of(indexName1, indexName2), recordStore.getRecordStoreState().getDisabledIndexNames());
commit(context);
}
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class RecordTypeKeyTest method testExplicitKeys.
@Test
public void testExplicitKeys() throws Exception {
RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
final RecordTypeBuilder t1 = metaDataBuilder.getRecordType("MySimpleRecord");
final RecordTypeBuilder t2 = metaDataBuilder.getRecordType("MyOtherRecord");
final KeyExpression pkey = concat(recordType(), field("rec_no"));
t1.setPrimaryKey(pkey);
t1.setRecordTypeKey("t1");
t2.setPrimaryKey(pkey);
RecordMetaData metaData = metaDataBuilder.getRecordMetaData();
assertEquals("t1", metaData.getRecordType("MySimpleRecord").getExplicitRecordTypeKey());
assertNull(metaData.getRecordType("MyOtherRecord").getExplicitRecordTypeKey());
metaData = RecordMetaData.build(metaData.toProto());
assertEquals("t1", metaData.getRecordType("MySimpleRecord").getExplicitRecordTypeKey());
assertNull(metaData.getRecordType("MyOtherRecord").getExplicitRecordTypeKey());
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class OnlineIndexerTest method openMetaData.
private void openMetaData(@Nonnull Descriptors.FileDescriptor descriptor, @Nonnull RecordMetaDataHook hook) {
RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(descriptor);
hook.apply(metaDataBuilder);
metaData = metaDataBuilder.getRecordMetaData();
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreQueryTestBase method openDoublyNestedRecordStore.
protected void openDoublyNestedRecordStore(FDBRecordContext context) throws Exception {
RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords5Proto.getDescriptor());
metaDataBuilder.addUniversalIndex(COUNT_INDEX);
metaDataBuilder.addIndex("CalendarEvent", "alarm_start", field("alarmIndex").nest(field("recurrence", FanType.FanOut).nest("start")));
metaDataBuilder.addIndex("CalendarEvent", "event_start", field("eventIndex").nest(field("recurrence", FanType.FanOut).nest("start")));
createOrOpenRecordStore(context, metaDataBuilder.getRecordMetaData());
}
use of com.apple.foundationdb.record.RecordMetaDataBuilder in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreStateCacheTest method conflictWhenCachedChanged.
/**
* Make sure that if one transaction changes the store header then an open store in another transaction that
* loaded the store state from cache will fail at commit time with conflict.
*/
@ParameterizedTest(name = "conflictWhenCachedChanged (test context = {0})")
@MethodSource("testContextSource")
public void conflictWhenCachedChanged(@Nonnull StateCacheTestContext testContext) throws Exception {
FDBRecordStoreStateCache origStoreStateCache = fdb.getStoreStateCache();
try {
fdb.setStoreStateCache(testContext.getCache(fdb));
RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
metaDataBuilder.addIndex("MySimpleRecord", "num_value_2");
RecordMetaData metaData2 = metaDataBuilder.getRecordMetaData();
assertThat(metaData1.getVersion(), lessThan(metaData2.getVersion()));
FDBRecordStore.Builder storeBuilder;
// Initialize the record store with a meta-data store
try (FDBRecordContext context = openContext()) {
context.getTimer().reset();
FDBRecordStore recordStore = FDBRecordStore.newBuilder().setContext(context).setMetaDataProvider(metaData1).setKeySpacePath(path).create();
assertEquals(1, context.getTimer().getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS));
assertEquals(metaData1.getVersion(), recordStore.getRecordStoreState().getStoreHeader().getMetaDataversion());
commit(context);
storeBuilder = recordStore.asBuilder();
}
// Load the record store state into the cache.
try (FDBRecordContext context1 = testContext.getCachedContext(fdb, storeBuilder);
FDBRecordContext context2 = testContext.getCachedContext(fdb, storeBuilder)) {
context1.setTimer(new FDBStoreTimer());
context2.setTimer(new FDBStoreTimer());
FDBRecordStore recordStore1 = storeBuilder.copyBuilder().setContext(context1).setMetaDataProvider(metaData1).open();
assertEquals(1, context1.getTimer().getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_HIT));
assertEquals(metaData1.getVersion(), recordStore1.getRecordMetaData().getVersion());
assertEquals(metaData1.getVersion(), recordStore1.getRecordStoreState().getStoreHeader().getMetaDataversion());
// Update the meta-data in the second transaction
FDBRecordStore recordStore2 = storeBuilder.copyBuilder().setContext(context2).setMetaDataProvider(metaData2).open();
assertEquals(1, context2.getTimer().getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_HIT));
assertEquals(Collections.singletonList(recordStore2.getRecordMetaData().getRecordType("MySimpleRecord")), recordStore2.getRecordMetaData().recordTypesForIndex(recordStore2.getRecordMetaData().getIndex("MySimpleRecord$num_value_2")));
assertEquals(metaData2.getVersion(), recordStore2.getRecordMetaData().getVersion());
assertEquals(metaData2.getVersion(), recordStore2.getRecordStoreState().getStoreHeader().getMetaDataversion());
context2.commit();
// Add a write to context1 so that the conflict ranges actually get checked.
recordStore1.saveRecord(TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(1066).setNumValue2(1415).build());
// Should conflict on store header even though not actually read in this transaction
assertThrows(FDBExceptions.FDBStoreTransactionConflictException.class, context1::commit);
}
// New transaction should now see the new meta-data version
try (FDBRecordContext context = openContext()) {
context.getTimer().reset();
// Trying to load with the old meta-data should fail
assertThrows(RecordStoreStaleMetaDataVersionException.class, () -> storeBuilder.copyBuilder().setContext(context).setMetaDataProvider(metaData1).open());
assertEquals(1, context.getTimer().getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS));
// Trying to load with the new meta-data should succeed
FDBRecordStore recordStore = storeBuilder.copyBuilder().setContext(context).setMetaDataProvider(metaData2).open();
assertEquals(1, context.getTimer().getCount(FDBStoreTimer.Counts.STORE_STATE_CACHE_HIT));
assertEquals(metaData2.getVersion(), recordStore.getRecordStoreState().getStoreHeader().getMetaDataversion());
}
} finally {
fdb.setStoreStateCache(origStoreStateCache);
}
}
Aggregations