use of com.apple.foundationdb.record.TestHelpers in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreIndexTest method buildNewIndex.
/**
* Verify that building an index works for a single-type index.
*/
@Test
public void buildNewIndex() throws Exception {
try (FDBRecordContext context = openContext()) {
uncheckedOpenSimpleRecordStore(context);
recordStore.checkVersion(null, FDBRecordStoreBase.StoreExistenceCheck.ERROR_IF_EXISTS).join();
for (int i = 0; i < 10; i++) {
TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
recBuilder.setRecNo(i);
recBuilder.setNumValue2(i % 10);
recordStore.saveRecord(recBuilder.build());
}
commit(context);
}
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.field("num_value_2").equalsValue(6)).build();
assertEquals(Arrays.asList(6L), fetchResultValues(planner.plan(query), TestRecords1Proto.MySimpleRecord.REC_NO_FIELD_NUMBER, this::uncheckedOpenSimpleRecordStore, context -> assertDiscardedAtLeast(9, context)));
// Adds an index for that query.
RecordMetaDataHook hook = metaData -> metaData.addIndex("MySimpleRecord", "new_index", "num_value_2");
Opener openWithNewIndex = context -> uncheckedOpenSimpleRecordStore(context, hook);
try (FDBRecordContext context = openContext()) {
uncheckedOpenSimpleRecordStore(context, hook);
for (int i = 10; i < 20; i++) {
TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
recBuilder.setRecNo(i);
recBuilder.setNumValue2(i % 10);
recordStore.saveRecord(recBuilder.build());
}
commit(context);
}
// Only sees new record added after index.
assertEquals(Arrays.asList(16L), fetchResultValues(planner.plan(query), TestRecords1Proto.MySimpleRecord.REC_NO_FIELD_NUMBER, openWithNewIndex, TestHelpers::assertDiscardedNone));
try (FDBRecordContext context = openContext()) {
uncheckedOpenSimpleRecordStore(context, hook);
recordStore.checkVersion(null, FDBRecordStoreBase.StoreExistenceCheck.NONE).join();
assertEquals(1, timer.getCount(FDBStoreTimer.Events.REBUILD_INDEX), "should build new index");
commit(context);
}
// Now see both records from indexed query.
assertEquals(Arrays.asList(6L, 16L), fetchResultValues(planner.plan(query), TestRecords1Proto.MySimpleRecord.REC_NO_FIELD_NUMBER, openWithNewIndex, TestHelpers::assertDiscardedNone));
}
Aggregations