Search in sources :

Example 16 with RecordType

use of com.apple.foundationdb.record.metadata.RecordType in project fdb-record-layer by FoundationDB.

the class RecordQueryPlannerSubstitutionVisitor method removeIndexFetch.

@Nullable
public static RecordQueryPlan removeIndexFetch(@Nonnull RecordMetaData recordMetaData, @Nonnull PlannableIndexTypes indexTypes, @Nullable KeyExpression commonPrimaryKey, @Nonnull RecordQueryPlan plan, @Nonnull Set<KeyExpression> requiredFields) {
    if (plan instanceof RecordQueryPlanWithIndex) {
        RecordQueryPlanWithIndex indexPlan = (RecordQueryPlanWithIndex) plan;
        Index index = recordMetaData.getIndex(indexPlan.getIndexName());
        final Collection<RecordType> recordTypes = recordMetaData.recordTypesForIndex(index);
        if (recordTypes.size() != 1) {
            return null;
        }
        final RecordType recordType = Iterables.getOnlyElement(recordTypes);
        AvailableFields fieldsFromIndex = AvailableFields.fromIndex(recordType, index, indexTypes, commonPrimaryKey);
        Set<KeyExpression> fields = new HashSet<>(requiredFields);
        if (commonPrimaryKey != null) {
            // Need the primary key, even if it wasn't one of the explicit result fields.
            fields.addAll(commonPrimaryKey.normalizeKeyForPositions());
        }
        if (fieldsFromIndex.containsAll(fields)) {
            final IndexKeyValueToPartialRecord keyValueToPartialRecord = fieldsFromIndex.buildIndexKeyValueToPartialRecord(recordType).build();
            if (keyValueToPartialRecord != null) {
                return new RecordQueryCoveringIndexPlan(indexPlan, recordType.getName(), fieldsFromIndex, keyValueToPartialRecord);
            }
        }
    } else if (plan instanceof RecordQueryFetchFromPartialRecordPlan) {
        RecordQueryFetchFromPartialRecordPlan fetchPlan = (RecordQueryFetchFromPartialRecordPlan) plan;
        if (fetchPlan.getChild().getAvailableFields().containsAll(requiredFields)) {
            return ((RecordQueryFetchFromPartialRecordPlan) plan).getChild();
        }
    }
    return null;
}
Also used : AvailableFields(com.apple.foundationdb.record.query.plan.AvailableFields) RecordQueryFetchFromPartialRecordPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryFetchFromPartialRecordPlan) RecordType(com.apple.foundationdb.record.metadata.RecordType) IndexKeyValueToPartialRecord(com.apple.foundationdb.record.query.plan.IndexKeyValueToPartialRecord) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) RecordQueryPlanWithIndex(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithIndex) RecordQueryCoveringIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryCoveringIndexPlan) RecordQueryPlanWithIndex(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithIndex) Index(com.apple.foundationdb.record.metadata.Index) HashSet(java.util.HashSet) Nullable(javax.annotation.Nullable)

Example 17 with RecordType

use of com.apple.foundationdb.record.metadata.RecordType in project fdb-record-layer by FoundationDB.

the class LuceneIndexTest method searchForAutoCompleteAndAssert.

private void searchForAutoCompleteAndAssert(String query, boolean matches, boolean highlight, int textSizeLimit) throws Exception {
    try (FDBRecordContext context = openContext(RecordLayerPropertyStorage.newBuilder().addProp(LuceneRecordContextProperties.LUCENE_AUTO_COMPLETE_TEXT_SIZE_UPPER_LIMIT, textSizeLimit))) {
        final RecordType recordType = addIndexAndSaveRecordForAutoComplete(context, highlight);
        List<IndexEntry> results = recordStore.scanIndex(highlight ? SIMPLE_TEXT_WITH_AUTO_COMPLETE_WITH_HIGHLIGHT : SIMPLE_TEXT_WITH_AUTO_COMPLETE, IndexScanType.BY_LUCENE_AUTO_COMPLETE, TupleRange.allOf(Tuple.from(query)), null, ScanProperties.FORWARD_SCAN).asList().get();
        if (!matches) {
            // Assert no suggestions
            assertTrue(results.isEmpty());
            assertEquals(0, context.getTimer().getCounter(FDBStoreTimer.Counts.LUCENE_SCAN_MATCHED_AUTO_COMPLETE_SUGGESTIONS).getCount());
            return;
        }
        // Assert the count of suggestions
        assertEquals(6, results.size());
        // Assert the suggestions' keys
        List<String> suggestions = results.stream().map(i -> (String) i.getKey().get(i.getKeySize() - 1)).collect(Collectors.toList());
        if (highlight) {
            assertEquals(ImmutableList.of("<b>good</b> evening", "<b>Good</b> night", "<b>Good</b> morning", "<b>Good</b> afternoon", "I'm <b>good</b>", "That's really <b>good</b>!"), suggestions);
        } else {
            assertEquals(ImmutableList.of("good evening", "Good night", "Good morning", "Good afternoon", "I'm good", "That's really good!"), suggestions);
        }
        // Assert the corresponding field for the suggestions
        List<String> fields = results.stream().map(i -> (String) i.getKey().get(i.getKeySize() - 2)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("text", "text", "text", "text", "text", "text"), fields);
        // Assert the suggestions are sorted according to their values, which are determined by the position of the term into the indexed text
        List<Long> values = results.stream().map(i -> (Long) i.getValue().get(0)).collect(Collectors.toList());
        List<Long> valuesSorted = new ArrayList<>(values);
        Collections.sort(valuesSorted, Collections.reverseOrder());
        assertEquals(valuesSorted, values);
        assertEquals(values.get(0), values.get(1));
        assertEquals(values.get(1), values.get(2));
        assertEquals(values.get(2), values.get(3));
        assertTrue(values.get(3) > values.get(4));
        assertTrue(values.get(4) > values.get(5));
        results.stream().forEach(i -> assertDocumentPartialRecordFromIndexEntry(recordType, i, (String) i.getKey().get(i.getKeySize() - 1), (String) i.getKey().get(i.getKeySize() - 2), IndexScanType.BY_LUCENE_AUTO_COMPLETE));
        assertEquals(6, context.getTimer().getCounter(FDBStoreTimer.Counts.LUCENE_SCAN_MATCHED_AUTO_COMPLETE_SUGGESTIONS).getCount());
        assertEntriesAndSegmentInfoStoredInCompoundFile(AutoCompleteSuggesterCommitCheckAsync.getSuggestionIndexSubspace(recordStore.indexSubspace(SIMPLE_TEXT_WITH_AUTO_COMPLETE), TupleHelpers.EMPTY), context, "_0.cfs", true);
    }
}
Also used : IndexEntry(com.apple.foundationdb.record.IndexEntry) Arrays(java.util.Arrays) StoreTimer(com.apple.foundationdb.record.provider.common.StoreTimer) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Random(java.util.Random) SynonymAnalyzer(com.apple.foundationdb.record.lucene.synonym.SynonymAnalyzer) EnglishSynonymMapConfig(com.apple.foundationdb.record.lucene.synonym.EnglishSynonymMapConfig) Subspace(com.apple.foundationdb.subspace.Subspace) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) Pair(org.apache.commons.lang3.tuple.Pair) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) COMPLEX_DOC(com.apple.foundationdb.record.provider.foundationdb.indexes.TextIndexTestUtils.COMPLEX_DOC) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Tag(org.junit.jupiter.api.Tag) SynonymMapConfig(com.apple.foundationdb.record.lucene.synonym.SynonymMapConfig) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) Collectors(java.util.stream.Collectors) FDBRecordContextConfig(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContextConfig) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) TestRecordsTextProto(com.apple.foundationdb.record.TestRecordsTextProto) List(java.util.List) NgramAnalyzer(com.apple.foundationdb.record.lucene.ngram.NgramAnalyzer) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RecordCursorProto(com.apple.foundationdb.record.RecordCursorProto) FDBDirectory(com.apple.foundationdb.record.lucene.directory.FDBDirectory) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Expressions.function(com.apple.foundationdb.record.metadata.Key.Expressions.function) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordLayerPropertyStorage(com.apple.foundationdb.record.provider.foundationdb.properties.RecordLayerPropertyStorage) Descriptors(com.google.protobuf.Descriptors) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) FDBRecordStore(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStore) IndexKeyValueToPartialRecord(com.apple.foundationdb.record.query.plan.IndexKeyValueToPartialRecord) ImmutableList(com.google.common.collect.ImmutableList) ScanProperties(com.apple.foundationdb.record.ScanProperties) FDBLuceneFileReference(com.apple.foundationdb.record.lucene.directory.FDBLuceneFileReference) TextIndexTestUtils(com.apple.foundationdb.record.provider.foundationdb.indexes.TextIndexTestUtils) 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) Nullable(javax.annotation.Nullable) SIMPLE_DOC(com.apple.foundationdb.record.provider.foundationdb.indexes.TextIndexTestUtils.SIMPLE_DOC) FDBStoredRecord(com.apple.foundationdb.record.provider.foundationdb.FDBStoredRecord) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Tags(com.apple.test.Tags) SequenceInputStream(java.io.SequenceInputStream) IndexFileNames(org.apache.lucene.index.IndexFileNames) IOException(java.io.IOException) FDBRecordStoreTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreTestBase) ExecutionException(java.util.concurrent.ExecutionException) RecordType(com.apple.foundationdb.record.metadata.RecordType) Index(com.apple.foundationdb.record.metadata.Index) AutoService(com.google.auto.service.AutoService) Message(com.google.protobuf.Message) Assertions(org.junit.jupiter.api.Assertions) RecordCursor(com.apple.foundationdb.record.RecordCursor) Collections(java.util.Collections) AllSuffixesTextTokenizer(com.apple.foundationdb.record.provider.common.text.AllSuffixesTextTokenizer) InputStream(java.io.InputStream) RecordType(com.apple.foundationdb.record.metadata.RecordType) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ArrayList(java.util.ArrayList) IndexEntry(com.apple.foundationdb.record.IndexEntry)

Example 18 with RecordType

use of com.apple.foundationdb.record.metadata.RecordType in project fdb-record-layer by FoundationDB.

the class FDBMetaDataStoreTest method updateRecordsWithNewUnionField.

@ParameterizedTest(name = "updateRecordsWithNewUnionField [reorderFields = {0}]")
@BooleanSource
public void updateRecordsWithNewUnionField(boolean reorderFields) {
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData oldMetaData = metaDataStore.getRecordMetaData();
        metaDataStore.mutateMetaData(metaDataProtoBuilder -> {
            final DescriptorProtos.FileDescriptorProto.Builder records = metaDataProtoBuilder.getRecordsBuilder();
            records.getMessageTypeBuilderList().stream().filter(message -> message.getName().equals(RecordMetaDataBuilder.DEFAULT_UNION_NAME)).forEach(unionMessage -> {
                unionMessage.getFieldBuilderList().stream().filter(field -> field.getName().equals("_MySimpleRecord")).forEach(field -> field.setName("_MySimpleRecord_old"));
                int newFieldNumber = unionMessage.getFieldBuilderList().stream().mapToInt(DescriptorProtos.FieldDescriptorProto.Builder::getNumber).max().orElse(0) + 1;
                DescriptorProtos.FieldDescriptorProto newField = DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE).setTypeName("." + TestRecords1Proto.MySimpleRecord.getDescriptor().getFullName()).setName("_MySimpleRecord_new").setNumber(newFieldNumber).build();
                if (reorderFields) {
                    List<DescriptorProtos.FieldDescriptorProto> fieldList = new ArrayList<>(unionMessage.getFieldBuilderList().size() + 1);
                    fieldList.add(newField);
                    fieldList.addAll(unionMessage.getFieldList());
                    unionMessage.clearField();
                    unionMessage.addAllField(fieldList);
                } else {
                    unionMessage.addField(newField);
                }
            });
        });
        RecordMetaData newMetaData = metaDataStore.getRecordMetaData();
        RecordType oldSimpleRecord = oldMetaData.getRecordType("MySimpleRecord");
        assertEquals(TestRecords1EvolvedProto.RecordTypeUnion._MYSIMPLERECORD_FIELD_NUMBER, oldMetaData.getUnionFieldForRecordType(oldSimpleRecord).getNumber());
        RecordType newSimpleRecord = newMetaData.getRecordType("MySimpleRecord");
        assertSame(newMetaData.getUnionDescriptor().findFieldByName("_MySimpleRecord_new"), newMetaData.getUnionFieldForRecordType(newSimpleRecord));
        assertThat(oldMetaData.getUnionFieldForRecordType(oldSimpleRecord).getNumber(), lessThan(newMetaData.getUnionFieldForRecordType(newSimpleRecord).getNumber()));
        assertEquals(oldSimpleRecord.getSinceVersion(), newSimpleRecord.getSinceVersion());
        context.commit();
    }
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaData metaData = metaDataStore.getRecordMetaData();
        RecordType simpleRecord = metaData.getRecordType("MySimpleRecord");
        assertEquals("_MySimpleRecord_new", metaData.getUnionFieldForRecordType(simpleRecord).getName());
        int newFieldNumber = TestRecords1Proto.RecordTypeUnion.getDescriptor().getFields().stream().mapToInt(Descriptors.FieldDescriptor::getNumber).max().orElse(0) + 1;
        assertEquals(newFieldNumber, metaData.getUnionFieldForRecordType(simpleRecord).getNumber());
    }
}
Also used : MetaDataEvolutionValidator(com.apple.foundationdb.record.metadata.MetaDataEvolutionValidator) DescriptorProtos(com.google.protobuf.DescriptorProtos) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) TestRecords3Proto(com.apple.foundationdb.record.TestRecords3Proto) Matchers.not(org.hamcrest.Matchers.not) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Transaction(com.apple.foundationdb.Transaction) TestRecordsMultiProto(com.apple.foundationdb.record.TestRecordsMultiProto) Tuple(com.apple.foundationdb.tuple.Tuple) TestRecordsDoubleNestedProto(com.apple.foundationdb.record.TestRecordsDoubleNestedProto) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) TestRecordsImplicitUsageProto(com.apple.foundationdb.record.TestRecordsImplicitUsageProto) RecordMetaDataProto(com.apple.foundationdb.record.RecordMetaDataProto) RecordMetaDataOptionsProto(com.apple.foundationdb.record.RecordMetaDataOptionsProto) TestNoUnionEvolvedRenamedRecordTypeProto(com.apple.foundationdb.record.TestNoUnionEvolvedRenamedRecordTypeProto) Tag(org.junit.jupiter.api.Tag) ProtoVersionSupplier(com.apple.foundationdb.record.ProtoVersionSupplier) TestRecords4Proto(com.apple.foundationdb.record.TestRecords4Proto) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) ImmutableSet(com.google.common.collect.ImmutableSet) KeyValue(com.apple.foundationdb.KeyValue) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) TestRecordsNestedAsRecord(com.apple.foundationdb.record.TestRecordsNestedAsRecord) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) TestRecordsImportedAndNewProto(com.apple.foundationdb.record.TestRecordsImportedAndNewProto) MetaDataProtoTest(com.apple.foundationdb.record.metadata.MetaDataProtoTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Optional(java.util.Optional) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) TestNoUnionEvolvedProto(com.apple.foundationdb.record.TestNoUnionEvolvedProto) Matchers.containsString(org.hamcrest.Matchers.containsString) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) TestNoUnionProto(com.apple.foundationdb.record.TestNoUnionProto) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) TestRecordsParentChildRelationshipProto(com.apple.foundationdb.record.TestRecordsParentChildRelationshipProto) TestRecords1EvolvedAgainProto(com.apple.foundationdb.record.TestRecords1EvolvedAgainProto) Descriptors(com.google.protobuf.Descriptors) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) TestRecordsOneOfProto(com.apple.foundationdb.record.TestRecordsOneOfProto) EnumSource(org.junit.jupiter.params.provider.EnumSource) ArrayList(java.util.ArrayList) Key(com.apple.foundationdb.record.metadata.Key) TestRecordsImplicitUsageNoUnionProto(com.apple.foundationdb.record.TestRecordsImplicitUsageNoUnionProto) TestNoUnionEvolvedIllegalProto(com.apple.foundationdb.record.TestNoUnionEvolvedIllegalProto) Matchers.lessThan(org.hamcrest.Matchers.lessThan) BooleanSource(com.apple.test.BooleanSource) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Tags(com.apple.test.Tags) TestRecords1EvolvedProto(com.apple.foundationdb.record.TestRecords1EvolvedProto) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) RecordType(com.apple.foundationdb.record.metadata.RecordType) Matchers.hasItem(org.hamcrest.Matchers.hasItem) TestRecordsImportProto(com.apple.foundationdb.record.TestRecordsImportProto) Index(com.apple.foundationdb.record.metadata.Index) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) ArrayList(java.util.ArrayList) DescriptorProtos(com.google.protobuf.DescriptorProtos) RecordType(com.apple.foundationdb.record.metadata.RecordType) Descriptors(com.google.protobuf.Descriptors) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Example 19 with RecordType

use of com.apple.foundationdb.record.metadata.RecordType in project fdb-record-layer by FoundationDB.

the class FDBMetaDataStoreTest method assertDeprecated.

private static void assertDeprecated(@Nonnull RecordMetaData metaData, @Nonnull String recordType) {
    RecordType recordTypeObj = metaData.getRecordType(recordType);
    assertTrue(metaData.getUnionFieldForRecordType(recordTypeObj).getOptions().getDeprecated());
}
Also used : RecordType(com.apple.foundationdb.record.metadata.RecordType)

Example 20 with RecordType

use of com.apple.foundationdb.record.metadata.RecordType in project fdb-record-layer by FoundationDB.

the class LuceneIndexTest method searchForAutoCompleteCrossingMultipleFields.

@Test
void searchForAutoCompleteCrossingMultipleFields() throws Exception {
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context, metaDataBuilder -> {
            metaDataBuilder.removeIndex(TextIndexTestUtils.SIMPLE_DEFAULT_NAME);
            metaDataBuilder.addIndex(COMPLEX_DOC, COMPLEX_MULTIPLE_TEXT_INDEXES_WITH_AUTO_COMPLETE);
        });
        // Write 8 texts and 6 of them contain the key "good"
        recordStore.saveRecord(createComplexDocument(1623L, "Good morning", "", 1));
        recordStore.saveRecord(createComplexDocument(1624L, "Good afternoon", "", 1));
        recordStore.saveRecord(createComplexDocument(1625L, "good evening", "", 1));
        recordStore.saveRecord(createComplexDocument(1626L, "Good night", "", 1));
        recordStore.saveRecord(createComplexDocument(1627L, "", "That's really good!", 1));
        recordStore.saveRecord(createComplexDocument(1628L, "", "I'm good", 1));
        recordStore.saveRecord(createComplexDocument(1629L, "", "Hello Record Layer", 1));
        RecordType recordType = recordStore.saveRecord(createComplexDocument(1630L, "", "Hello FoundationDB!", 1)).getRecordType();
        List<IndexEntry> results = recordStore.scanIndex(COMPLEX_MULTIPLE_TEXT_INDEXES_WITH_AUTO_COMPLETE, IndexScanType.BY_LUCENE_AUTO_COMPLETE, TupleRange.allOf(Tuple.from("good")), null, ScanProperties.FORWARD_SCAN).asList().get();
        results.stream().forEach(i -> assertDocumentPartialRecordFromIndexEntry(recordType, i, (String) i.getKey().get(i.getKeySize() - 1), (String) i.getKey().get(i.getKeySize() - 2), IndexScanType.BY_LUCENE_AUTO_COMPLETE));
        assertEquals(6, context.getTimer().getCounter(FDBStoreTimer.Counts.LUCENE_SCAN_MATCHED_AUTO_COMPLETE_SUGGESTIONS).getCount());
        assertEntriesAndSegmentInfoStoredInCompoundFile(AutoCompleteSuggesterCommitCheckAsync.getSuggestionIndexSubspace(recordStore.indexSubspace(COMPLEX_MULTIPLE_TEXT_INDEXES_WITH_AUTO_COMPLETE), TupleHelpers.EMPTY), context, "_0.cfs", true);
        // Assert the count of suggestions
        assertEquals(6, results.size());
        // Assert the suggestions' keys
        List<String> suggestions = results.stream().map(i -> (String) i.getKey().get(i.getKeySize() - 1)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("good evening", "Good night", "Good morning", "Good afternoon", "I'm good", "That's really good!"), suggestions);
        // Assert the corresponding field for the suggestions
        List<String> fields = results.stream().map(i -> (String) i.getKey().get(i.getKeySize() - 2)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("text", "text", "text", "text", "text2", "text2"), fields);
        // Assert the suggestions are sorted according to their values, which are determined by the position of the term into the indexed text
        List<Long> values = results.stream().map(i -> (Long) i.getValue().get(0)).collect(Collectors.toList());
        List<Long> valuesSorted = new ArrayList<>(values);
        Collections.sort(valuesSorted, Collections.reverseOrder());
        assertEquals(valuesSorted, values);
        assertEquals(values.get(0), values.get(1));
        assertEquals(values.get(1), values.get(2));
        assertEquals(values.get(2), values.get(3));
        assertTrue(values.get(3) > values.get(4));
        assertTrue(values.get(4) > values.get(5));
    }
}
Also used : IndexEntry(com.apple.foundationdb.record.IndexEntry) Arrays(java.util.Arrays) StoreTimer(com.apple.foundationdb.record.provider.common.StoreTimer) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Random(java.util.Random) SynonymAnalyzer(com.apple.foundationdb.record.lucene.synonym.SynonymAnalyzer) EnglishSynonymMapConfig(com.apple.foundationdb.record.lucene.synonym.EnglishSynonymMapConfig) Subspace(com.apple.foundationdb.subspace.Subspace) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) Pair(org.apache.commons.lang3.tuple.Pair) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) COMPLEX_DOC(com.apple.foundationdb.record.provider.foundationdb.indexes.TextIndexTestUtils.COMPLEX_DOC) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Tag(org.junit.jupiter.api.Tag) SynonymMapConfig(com.apple.foundationdb.record.lucene.synonym.SynonymMapConfig) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) Collectors(java.util.stream.Collectors) FDBRecordContextConfig(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContextConfig) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) TestRecordsTextProto(com.apple.foundationdb.record.TestRecordsTextProto) List(java.util.List) NgramAnalyzer(com.apple.foundationdb.record.lucene.ngram.NgramAnalyzer) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RecordCursorProto(com.apple.foundationdb.record.RecordCursorProto) FDBDirectory(com.apple.foundationdb.record.lucene.directory.FDBDirectory) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Expressions.function(com.apple.foundationdb.record.metadata.Key.Expressions.function) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordLayerPropertyStorage(com.apple.foundationdb.record.provider.foundationdb.properties.RecordLayerPropertyStorage) Descriptors(com.google.protobuf.Descriptors) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) FDBRecordStore(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStore) IndexKeyValueToPartialRecord(com.apple.foundationdb.record.query.plan.IndexKeyValueToPartialRecord) ImmutableList(com.google.common.collect.ImmutableList) ScanProperties(com.apple.foundationdb.record.ScanProperties) FDBLuceneFileReference(com.apple.foundationdb.record.lucene.directory.FDBLuceneFileReference) TextIndexTestUtils(com.apple.foundationdb.record.provider.foundationdb.indexes.TextIndexTestUtils) 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) Nullable(javax.annotation.Nullable) SIMPLE_DOC(com.apple.foundationdb.record.provider.foundationdb.indexes.TextIndexTestUtils.SIMPLE_DOC) FDBStoredRecord(com.apple.foundationdb.record.provider.foundationdb.FDBStoredRecord) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Tags(com.apple.test.Tags) SequenceInputStream(java.io.SequenceInputStream) IndexFileNames(org.apache.lucene.index.IndexFileNames) IOException(java.io.IOException) FDBRecordStoreTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreTestBase) ExecutionException(java.util.concurrent.ExecutionException) RecordType(com.apple.foundationdb.record.metadata.RecordType) Index(com.apple.foundationdb.record.metadata.Index) AutoService(com.google.auto.service.AutoService) Message(com.google.protobuf.Message) Assertions(org.junit.jupiter.api.Assertions) RecordCursor(com.apple.foundationdb.record.RecordCursor) Collections(java.util.Collections) AllSuffixesTextTokenizer(com.apple.foundationdb.record.provider.common.text.AllSuffixesTextTokenizer) InputStream(java.io.InputStream) RecordType(com.apple.foundationdb.record.metadata.RecordType) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ArrayList(java.util.ArrayList) IndexEntry(com.apple.foundationdb.record.IndexEntry) Test(org.junit.jupiter.api.Test)

Aggregations

RecordType (com.apple.foundationdb.record.metadata.RecordType)43 Nonnull (javax.annotation.Nonnull)29 Index (com.apple.foundationdb.record.metadata.Index)25 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)24 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)20 Nullable (javax.annotation.Nullable)20 Descriptors (com.google.protobuf.Descriptors)18 SyntheticRecordType (com.apple.foundationdb.record.metadata.SyntheticRecordType)17 ArrayList (java.util.ArrayList)17 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)16 Tuple (com.apple.foundationdb.tuple.Tuple)16 List (java.util.List)15 Collection (java.util.Collection)14 API (com.apple.foundationdb.annotation.API)13 IndexEntry (com.apple.foundationdb.record.IndexEntry)13 Collectors (java.util.stream.Collectors)13 RecordCoreArgumentException (com.apple.foundationdb.record.RecordCoreArgumentException)12 TupleRange (com.apple.foundationdb.record.TupleRange)12 MetaDataException (com.apple.foundationdb.record.metadata.MetaDataException)12 CompletableFuture (java.util.concurrent.CompletableFuture)12