Search in sources :

Example 46 with GroupingKeyExpression

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());
    }
}
Also used : LogMessageKeys(com.apple.foundationdb.record.logging.LogMessageKeys) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) PlanMatchers.bounds(com.apple.foundationdb.record.query.plan.match.PlanMatchers.bounds) RecordQueryPlanner(com.apple.foundationdb.record.query.plan.RecordQueryPlanner) Tuple(com.apple.foundationdb.tuple.Tuple) TestHelpers(com.apple.foundationdb.record.TestHelpers) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Tag(org.junit.jupiter.api.Tag) Query(com.apple.foundationdb.record.query.expressions.Query) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) Predicate(java.util.function.Predicate) Matchers.allOf(org.hamcrest.Matchers.allOf) IndexQueryabilityFilter(com.apple.foundationdb.record.query.IndexQueryabilityFilter) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) List(java.util.List) PlanMatchers.indexName(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexName) RecordStoreState(com.apple.foundationdb.record.RecordStoreState) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) PlanMatchers.hasNoDescendant(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasNoDescendant) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AggregateFunctionNotSupportedException(com.apple.foundationdb.record.AggregateFunctionNotSupportedException) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Matchers.containsString(org.hamcrest.Matchers.containsString) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) FunctionNames(com.apple.foundationdb.record.FunctionNames) IndexAggregateFunction(com.apple.foundationdb.record.metadata.IndexAggregateFunction) PlanMatchers.indexScan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexScan) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) CompletableFuture(java.util.concurrent.CompletableFuture) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) RangeSet(com.apple.foundationdb.async.RangeSet) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) Function(java.util.function.Function) PlanHashable(com.apple.foundationdb.record.PlanHashable) Key(com.apple.foundationdb.record.metadata.Key) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) IsolationLevel(com.apple.foundationdb.record.IsolationLevel) Tags(com.apple.test.Tags) Index(com.apple.foundationdb.record.metadata.Index) Executable(org.junit.jupiter.api.function.Executable) Message(com.google.protobuf.Message) RecordCursor(com.apple.foundationdb.record.RecordCursor) PlanMatchers.descendant(com.apple.foundationdb.record.query.plan.match.PlanMatchers.descendant) Collections(java.util.Collections) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Index(com.apple.foundationdb.record.metadata.Index) Test(org.junit.jupiter.api.Test)

Example 47 with GroupingKeyExpression

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());
}
Also used : TestRecordsTextProto(com.apple.foundationdb.record.TestRecordsTextProto) Message(com.google.protobuf.Message) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Descriptors(com.google.protobuf.Descriptors) Test(org.junit.jupiter.api.Test)

Example 48 with GroupingKeyExpression

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;
}
Also used : HashMap(java.util.HashMap) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) ArrayList(java.util.ArrayList) List(java.util.List) Tuple(com.apple.foundationdb.tuple.Tuple) Nonnull(javax.annotation.Nonnull)

Example 49 with GroupingKeyExpression

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<>());
}
Also used : GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) LiteralKeyExpression(com.apple.foundationdb.record.metadata.expressions.LiteralKeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) Nonnull(javax.annotation.Nonnull)

Example 50 with GroupingKeyExpression

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);
}
Also used : GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) RecordTypeKeyExpression(com.apple.foundationdb.record.metadata.expressions.RecordTypeKeyExpression) VersionKeyExpression(com.apple.foundationdb.record.metadata.expressions.VersionKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) RecordQueryPlanWithIndex(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithIndex) Index(com.apple.foundationdb.record.metadata.Index) Nullable(javax.annotation.Nullable)

Aggregations

GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)57 Index (com.apple.foundationdb.record.metadata.Index)41 Test (org.junit.jupiter.api.Test)39 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)35 ArrayList (java.util.ArrayList)29 Nonnull (javax.annotation.Nonnull)25 IndexTypes (com.apple.foundationdb.record.metadata.IndexTypes)23 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)22 List (java.util.List)22 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)21 Message (com.google.protobuf.Message)21 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)21 RecordMetaDataBuilder (com.apple.foundationdb.record.RecordMetaDataBuilder)20 IndexAggregateFunction (com.apple.foundationdb.record.metadata.IndexAggregateFunction)20 Expressions.field (com.apple.foundationdb.record.metadata.Key.Expressions.field)20 EmptyKeyExpression (com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression)20 Query (com.apple.foundationdb.record.query.expressions.Query)20 Tuple (com.apple.foundationdb.tuple.Tuple)20 Tags (com.apple.test.Tags)20 Tag (org.junit.jupiter.api.Tag)20