use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class FDBCollateQueryTest method compareParameter.
@Test
public void compareParameter() throws Exception {
final KeyExpression key = function(collateFunctionName, concat(NAME_FIELD, value("de_DE")));
final RecordMetaDataHook hook = md -> {
md.removeIndex("MySimpleRecord$str_value_indexed");
md.addIndex("MySimpleRecord", "collated_name", key);
};
loadNames(hook);
final RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.keyExpression(key).equalsParameter("name")).setRequiredResults(Arrays.asList(NAME_FIELD)).build();
final List<String> actual = queryNames(query, hook, "name", "gauss");
final List<String> expected = Arrays.asList("Gauß");
assertEquals(expected, actual);
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, indexScan(allOf(indexName("collated_name"), bounds(hasTupleString(String.format("[EQUALS %s($name)]", collateFunctionName))))));
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class PermutedMinMaxIndexTest method deleteWhere.
@Test
public void deleteWhere() {
final RecordMetaDataHook hook = md -> {
final KeyExpression pkey = Key.Expressions.concatenateFields("num_value_2", "num_value_3_indexed", "rec_no");
md.getRecordType("MySimpleRecord").setPrimaryKey(pkey);
md.getRecordType("MyOtherRecord").setPrimaryKey(pkey);
md.removeIndex("MySimpleRecord$str_value_indexed");
md.removeIndex("MySimpleRecord$num_value_3_indexed");
md.removeIndex("MySimpleRecord$num_value_unique");
md.removeIndex(COUNT_INDEX.getName());
md.removeIndex(COUNT_UPDATES_INDEX.getName());
md.addIndex("MySimpleRecord", new Index(INDEX_NAME, Key.Expressions.concatenateFields("num_value_2", "num_value_3_indexed", "str_value_indexed", "num_value_unique").group(1), IndexTypes.PERMUTED_MAX, Collections.singletonMap(IndexOptions.PERMUTED_SIZE_OPTION, "2")));
};
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
saveRecord(100, "yes", 1, 1);
saveRecord(150, "yes", 1, 1);
saveRecord(200, "no", 1, 1);
saveRecord(300, "yes", 1, 2);
saveRecord(400, "no", 1, 2);
saveRecord(500, "maybe", 2, 1);
assertEquals(Arrays.asList(Tuple.from(1, 150, 1, "yes"), Tuple.from(1, 200, 1, "no"), Tuple.from(1, 300, 2, "yes"), Tuple.from(1, 400, 2, "no"), Tuple.from(2, 500, 1, "maybe")), scanGroup(Tuple.from(), false));
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
recordStore.deleteRecordsWhere(Query.field("num_value_2").equalsValue(2));
assertEquals(Arrays.asList(Tuple.from(1, 150, 1, "yes"), Tuple.from(1, 200, 1, "no"), Tuple.from(1, 300, 2, "yes"), Tuple.from(1, 400, 2, "no")), scanGroup(Tuple.from(), false));
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertThrows(Query.InvalidExpressionException.class, () -> {
recordStore.deleteRecordsWhere(Query.and(Query.field("num_value_2").equalsValue(2), Query.field("num_value_3_indexed").equalsValue(1)));
});
}
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression 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.expressions.KeyExpression 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.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class RecordTypeKeyTest method testSortOnIndexWithComparisonOnSecondColumn.
@ParameterizedTest(name = "testSortOnSingleRecordType [sortExpr = {0}, reverse = {1}]")
@MethodSource("sortArgs")
@Disabled
public void testSortOnIndexWithComparisonOnSecondColumn(@Nonnull KeyExpression sortExpr, boolean reverse) throws Exception {
final Index index = new Index("recno-type", concat(field("num_value_2"), recordType()));
RecordMetaDataHook hook = metaData -> {
BASIC_HOOK.apply(metaData);
metaData.addUniversalIndex(index);
};
// Save an additional record which matches the predicate in order to ensure the reverse parameter of the
// query is honored (as the additional record allows forward and reverse scans to be distinguished)
final List<FDBStoredRecord<Message>> recs = saveSomeRecords(hook);
FDBStoredRecord<Message> additionalRecord;
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
TestRecords1Proto.MySimpleRecord record = TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(1066).setNumValue2(2).build();
FDBStoredRecord<Message> storedRecord = recordStore.saveRecord(record);
commit(context);
additionalRecord = storedRecord.withCommittedVersion(context.getVersionStamp());
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.field("num_value_2").equalsValue(2)).setSort(sortExpr, reverse).build();
final RecordQueryPlan plan = planner.plan(query);
List<FDBStoredRecord<Message>> expectedResults = Arrays.asList(recs.get(1), additionalRecord);
if (reverse) {
expectedResults = Lists.reverse(expectedResults);
}
assertEquals(expectedResults, recordStore.executeQuery(query).map(FDBQueriedRecord::getStoredRecord).asList().join());
assertThat(plan, indexScan(allOf(indexName(index.getName()), bounds(hasTupleString("[EQUALS 2, IS MySimpleRecord]")))));
}
}
Aggregations