use of com.apple.foundationdb.record.metadata.RecordTypeBuilder 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.metadata.RecordTypeBuilder in project fdb-record-layer by FoundationDB.
the class RecordTypeKeyTest method testIndexScan.
@Test
public void testIndexScan() throws Exception {
// This means that some record types do not have a record type key, so an index scan will be better.
RecordMetaDataHook hook = metaData -> {
final RecordTypeBuilder t1 = metaData.getRecordType("MySimpleRecord");
final KeyExpression pkey = concat(recordType(), field("rec_no"));
t1.setPrimaryKey(pkey);
metaData.removeIndex(COUNT_INDEX.getName());
metaData.removeIndex(COUNT_UPDATES_INDEX.getName());
};
List<FDBStoredRecord<Message>> recs = saveSomeRecords(hook);
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").build();
RecordQueryPlan plan = planner.plan(query);
assertEquals(recs.subList(0, 2), recordStore.executeQuery(query).map(FDBQueriedRecord::getStoredRecord).asList().join());
assertThat(plan, indexScan(allOf(indexName("MySimpleRecord$str_value_indexed"), unbounded())));
}
}
use of com.apple.foundationdb.record.metadata.RecordTypeBuilder in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreIndexTest method minMaxTupleUngrouped.
@Test
public void minMaxTupleUngrouped() throws Exception {
final FieldKeyExpression fieldKey = field("num_value_3_indexed");
final GroupingKeyExpression indexKey = fieldKey.ungrouped();
final RecordMetaDataHook hook = md -> {
RecordTypeBuilder type = md.getRecordType("MySimpleRecord");
md.addIndex(type, new Index("min", indexKey, IndexTypes.MIN_EVER_TUPLE));
md.addIndex(type, new Index("max", indexKey, IndexTypes.MAX_EVER_TUPLE));
};
final IndexAggregateFunction min = new IndexAggregateFunction(FunctionNames.MIN_EVER, fieldKey, null);
final IndexAggregateFunction max = new IndexAggregateFunction(FunctionNames.MAX_EVER, fieldKey, null);
List<String> types = Collections.singletonList("MySimpleRecord");
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertNull(recordStore.evaluateAggregateFunction(types, min, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
assertNull(recordStore.evaluateAggregateFunction(types, max, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
for (int i = 0; i < 100; i++) {
TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
recBuilder.setRecNo(i);
recBuilder.setNumValue3Indexed(i * 10);
recordStore.saveRecord(recBuilder.build());
}
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertEquals(Tuple.from(0), recordStore.evaluateAggregateFunction(types, min, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
assertEquals(Tuple.from(990), recordStore.evaluateAggregateFunction(types, max, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
commit(context);
}
}
use of com.apple.foundationdb.record.metadata.RecordTypeBuilder in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreIndexTest method minMaxValueNegative.
@Test
public void minMaxValueNegative() throws Exception {
final FieldKeyExpression strValue = field("str_value_indexed");
final FieldKeyExpression numValue2 = field("num_value_2");
final FieldKeyExpression numValue3 = field("num_value_3_indexed");
final ThenKeyExpression compound = concat(strValue, numValue2, numValue3);
final RecordMetaDataHook hook = md -> {
RecordTypeBuilder type = md.getRecordType("MySimpleRecord");
md.addIndex(type, new Index("compound", compound, IndexTypes.VALUE));
};
List<String> types = Collections.singletonList("MySimpleRecord");
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
final IndexAggregateFunction minValue2 = new IndexAggregateFunction(FunctionNames.MIN, numValue2, null);
assertThrows(RecordCoreException.class, () -> {
recordStore.evaluateAggregateFunction(types, minValue2, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join();
});
final IndexAggregateFunction minValue3GroupedIncorrectly = new IndexAggregateFunction(FunctionNames.MIN, numValue3.groupBy(numValue2, strValue), null);
assertThrows(RecordCoreException.class, () -> {
recordStore.evaluateAggregateFunction(types, minValue3GroupedIncorrectly, Key.Evaluated.concatenate(1, "foo"), IsolationLevel.SNAPSHOT).join();
});
final IndexAggregateFunction minValue3GroupedTooMany = new IndexAggregateFunction(FunctionNames.MIN, concat(numValue3, numValue2).groupBy(strValue), null);
assertThrows(RecordCoreException.class, () -> {
recordStore.evaluateAggregateFunction(types, minValue3GroupedTooMany, Key.Evaluated.scalar("foo"), IsolationLevel.SNAPSHOT).join();
});
commit(context);
}
}
use of com.apple.foundationdb.record.metadata.RecordTypeBuilder in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreIndexTest method minMaxTupleGrouped.
@Test
public void minMaxTupleGrouped() throws Exception {
final ThenKeyExpression tupleKey = concat(field("str_value_indexed"), field("num_value_2"));
final GroupingKeyExpression byKey = tupleKey.groupBy(field("num_value_3_indexed"));
final RecordMetaDataHook hook = md -> {
RecordTypeBuilder type = md.getRecordType("MySimpleRecord");
md.addIndex(type, new Index("min", byKey, IndexTypes.MIN_EVER_TUPLE));
md.addIndex(type, new Index("max", byKey, IndexTypes.MAX_EVER_TUPLE));
};
final IndexAggregateFunction minOverall = new IndexAggregateFunction(FunctionNames.MIN_EVER, tupleKey, null);
final IndexAggregateFunction maxOverall = new IndexAggregateFunction(FunctionNames.MAX_EVER, tupleKey, null);
final IndexAggregateFunction minByKey = new IndexAggregateFunction(FunctionNames.MIN_EVER, byKey, null);
final IndexAggregateFunction maxByKey = new IndexAggregateFunction(FunctionNames.MAX_EVER, byKey, null);
List<String> types = Collections.singletonList("MySimpleRecord");
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertNull(recordStore.evaluateAggregateFunction(types, minOverall, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
assertNull(recordStore.evaluateAggregateFunction(types, maxOverall, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
assertNull(recordStore.evaluateAggregateFunction(types, minByKey, Key.Evaluated.scalar(1), IsolationLevel.SNAPSHOT).join());
assertNull(recordStore.evaluateAggregateFunction(types, maxByKey, Key.Evaluated.scalar(1), IsolationLevel.SNAPSHOT).join());
for (int i = 0; i < 100; i++) {
TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
recBuilder.setRecNo(i);
recBuilder.setNumValue3Indexed(i % 3);
recBuilder.setStrValueIndexed((i & 1) == 1 ? "odd" : "even");
recBuilder.setNumValue2(i / 2);
recordStore.saveRecord(recBuilder.build());
}
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertEquals(Tuple.from("even", 0), recordStore.evaluateAggregateFunction(types, minOverall, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
assertEquals(Tuple.from("odd", 49), recordStore.evaluateAggregateFunction(types, maxOverall, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
assertEquals(Tuple.from("even", 2), recordStore.evaluateAggregateFunction(types, minByKey, Key.Evaluated.scalar(1), IsolationLevel.SNAPSHOT).join());
assertEquals(Tuple.from("odd", 48), recordStore.evaluateAggregateFunction(types, maxByKey, Key.Evaluated.scalar(1), IsolationLevel.SNAPSHOT).join());
commit(context);
}
}
Aggregations