use of com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression in project fdb-record-layer by FoundationDB.
the class FDBRestrictedIndexQueryTest method snapshotRecordCountForRecordTypeFiltered.
@Test
void snapshotRecordCountForRecordTypeFiltered() throws Exception {
Index onType = new Index("onType", emptyGroupedKeyExpression(), IndexTypes.COUNT);
Index byType = new Index("byType", new GroupingKeyExpression(Key.Expressions.recordType(), 0), IndexTypes.COUNT);
RecordMetaDataHook hook = metaData -> {
metaData.addUniversalIndex(byType);
metaData.addIndex("MySimpleRecord", onType);
};
try (FDBRecordContext context = openContext()) {
// test filtered
openSimpleRecordStore(context, hook);
recordStore.deleteAllRecords();
saveSimpleRecord(1066, 42);
saveSimpleRecord(1776, 100);
assertFilteredCount(2, filter -> recordStore.getSnapshotRecordCountForRecordType("MySimpleRecord", filter));
assertEquals(2, recordStore.getSnapshotRecordCountForRecordType("MySimpleRecord", createIndexFilter(17, index -> index.getName().equals("onType"))).get());
assertEquals(2, recordStore.getSnapshotRecordCountForRecordType("MySimpleRecord", createIndexFilter(18, index -> index.getName().equals("byType"))).get());
}
}
use of com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression in project fdb-record-layer by FoundationDB.
the class LuceneDocumentFromRecordTest method groupingMapWithExtra.
@Test
void groupingMapWithExtra() {
TestRecordsTextProto.MapDocument message = TestRecordsTextProto.MapDocument.newBuilder().setDocId(8).addEntry(TestRecordsTextProto.MapDocument.Entry.newBuilder().setKey("en").setValue("first").setSecondValue("second")).addEntry(TestRecordsTextProto.MapDocument.Entry.newBuilder().setKey("de").setValue("erste").setSecondValue("zweite")).setGroup(40).setText2("extra").build();
FDBRecord<Message> record = unstoredRecord(message);
KeyExpression index = new GroupingKeyExpression(concat(field("group"), field("entry", KeyExpression.FanType.FanOut).nest(concat(field("key"), function(LuceneFunctionNames.LUCENE_TEXT, field("value")), function(LuceneFunctionNames.LUCENE_TEXT, field("second_value")))), function(LuceneFunctionNames.LUCENE_TEXT, field("text2"))), 3);
assertEquals(ImmutableMap.of(Tuple.from(40, "en"), ImmutableList.of(textField("entry_value", "first"), textField("entry_second_value", "second"), textField("text2", "extra")), Tuple.from(40, "de"), ImmutableList.of(textField("entry_value", "erste"), textField("entry_second_value", "zweite"), textField("text2", "extra"))), LuceneDocumentFromRecord.getRecordFields(index, record));
// Build the partial record message for suggestion
Descriptors.Descriptor recordDescriptor = message.getDescriptorForType();
TestRecordsTextProto.MapDocument.Builder builder = TestRecordsTextProto.MapDocument.newBuilder();
LuceneIndexKeyValueToPartialRecordUtils.buildPartialRecord(index, recordDescriptor, builder, "entry_second_value", "suggestion", Tuple.from(40, "en"));
TestRecordsTextProto.MapDocument partialMsg = builder.build();
assertEquals(1, partialMsg.getEntryCount());
TestRecordsTextProto.MapDocument.Entry entry = partialMsg.getEntry(0);
// The suggestion is supposed to show up in second_value field within repeated entry sub-message
assertEquals("suggestion", entry.getSecondValue());
// The value field is not supposed to be populated
assertFalse(entry.hasValue());
// The key field within repeated entry sub-message is supposed to be populated because it is part of grouping key
assertEquals("en", entry.getKey());
// The group field is supposed to be populated because it is part of grouping key
assertEquals(40L, partialMsg.getGroup());
}
use of com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression in project fdb-record-layer by FoundationDB.
the class LuceneDocumentFromRecord method getRecordFields.
@Nonnull
protected static <M extends Message> Map<Tuple, List<DocumentField>> getRecordFields(@Nonnull KeyExpression root, @Nullable FDBRecord<M> rec) {
Map<Tuple, List<DocumentField>> result = new HashMap<>();
if (rec != null) {
// Grouping expression
if (root instanceof GroupingKeyExpression) {
GroupingKeyExpression group = (GroupingKeyExpression) root;
getGroupedFields(Collections.singletonList(group.getWholeKey()), 0, 0, group.getGroupingCount(), TupleHelpers.EMPTY, rec, rec.getRecord(), result, null);
} else {
result.put(TupleHelpers.EMPTY, getFields(root, rec, rec.getRecord(), null));
}
}
return result;
}
use of com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression in project fdb-record-layer by FoundationDB.
the class LuceneIndexExpressions method getFields.
/**
* Interpret the index key expression, either concretely for an actual record, or symbolically using meta-data.
* @param root the {@code LUCENE} index root expresison
* @param source the record / record meta-data
* @param destination the document / document meta-data
*/
@Nonnull
public static <T extends RecordSource<T>> void getFields(@Nonnull KeyExpression root, @Nonnull T source, @Nonnull DocumentDestination<T> destination, @Nullable String fieldNamePrefix) {
KeyExpression expression;
if (root instanceof GroupingKeyExpression) {
expression = ((GroupingKeyExpression) root).getGroupedSubKey();
} else {
expression = root;
}
getFieldsRecursively(expression, source, destination, fieldNamePrefix, 0, root instanceof GroupingKeyExpression ? ((GroupingKeyExpression) root).getGroupingCount() : 0, new ArrayList<>());
}
use of com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression in project fdb-record-layer by FoundationDB.
the class RecordQueryPlanner method planCoveringAggregateIndex.
@Nullable
public RecordQueryCoveringIndexPlan planCoveringAggregateIndex(@Nonnull RecordQuery query, @Nonnull String indexName) {
final Index index = metaData.getIndex(indexName);
KeyExpression indexExpr = index.getRootExpression();
if (indexExpr instanceof GroupingKeyExpression) {
indexExpr = ((GroupingKeyExpression) indexExpr).getGroupingSubKey();
} else {
indexExpr = EmptyKeyExpression.EMPTY;
}
return planCoveringAggregateIndex(query, index, indexExpr);
}
Aggregations