Search in sources :

Example 26 with BooleanSource

use of com.apple.test.BooleanSource in project fdb-record-layer by FoundationDB.

the class FDBLuceneQueryTest method misMatchQueryShouldReturnNoResult.

@ParameterizedTest
@BooleanSource
public void misMatchQueryShouldReturnNoResult(boolean shouldDeferFetch) throws Exception {
    initializeFlat();
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        final QueryComponent filter1 = new LuceneQueryComponent("doesNotExist", Lists.newArrayList("text"), true);
        // Query for full records
        RecordQuery query = RecordQuery.newBuilder().setRecordType(TextIndexTestUtils.SIMPLE_DOC).setFilter(filter1).build();
        setDeferFetchAfterUnionAndIntersection(shouldDeferFetch);
        RecordQueryPlan plan = planner.plan(query);
        Matcher<RecordQueryPlan> matcher = indexScan(allOf(indexScan("Complex$text_index"), indexScanType(IndexScanType.BY_LUCENE_FULL_TEXT), bounds(hasTupleString("[[doesNotExist],[doesNotExist]]"))));
        assertThat(plan, matcher);
        List<Long> primaryKeys = recordStore.executeQuery(plan).map(FDBQueriedRecord::getPrimaryKey).map(t -> t.getLong(0)).asList().get();
        assertEquals(ImmutableSet.of(), ImmutableSet.copyOf(primaryKeys));
        if (shouldDeferFetch) {
            assertLoadRecord(3, context);
        } else {
            assertLoadRecord(4, context);
        }
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) LuceneQueryComponent(com.apple.foundationdb.record.query.expressions.LuceneQueryComponent) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) LuceneQueryComponent(com.apple.foundationdb.record.query.expressions.LuceneQueryComponent) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Example 27 with BooleanSource

use of com.apple.test.BooleanSource in project fdb-record-layer by FoundationDB.

the class FDBLuceneQueryTest method simpleLuceneScansDocId.

@ParameterizedTest
@BooleanSource
public void simpleLuceneScansDocId(boolean shouldDeferFetch) throws Exception {
    initializeFlat();
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        // Query for full records
        RecordQuery query = RecordQuery.newBuilder().setRecordType(TextIndexTestUtils.SIMPLE_DOC).setFilter(Query.field("doc_id").equalsValue(1L)).build();
        setDeferFetchAfterUnionAndIntersection(shouldDeferFetch);
        Matcher<RecordQueryPlan> matcher = typeFilter(equalTo(Collections.singleton(TextIndexTestUtils.SIMPLE_DOC)), scan(bounds(hasTupleString("[[1],[1]]"))));
        RecordQueryPlan plan = planner.plan(query);
        assertThat(plan, matcher);
        List<Long> primaryKeys = recordStore.executeQuery(plan).map(FDBQueriedRecord::getPrimaryKey).map(t -> t.getLong(0)).asList().get();
        assertEquals(ImmutableSet.of(1L), ImmutableSet.copyOf(primaryKeys));
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Example 28 with BooleanSource

use of com.apple.test.BooleanSource 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 29 with BooleanSource

use of com.apple.test.BooleanSource in project fdb-record-layer by FoundationDB.

the class FDBMetaDataStoreTest method noUnionImplicitUsage.

@ParameterizedTest(name = "noUnionImplicitUsage [repeatSaveOrDoUpdate = {0}]")
@BooleanSource
public void noUnionImplicitUsage(boolean repeatSaveOrDoUpdate) {
    int version;
    // MyOtherRecord has no explicit usage. The proto file has a union and does not include MyOtherRecord.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        metaDataStore.saveRecordMetaData(TestRecordsImplicitUsageProto.getDescriptor());
        context.commit();
        version = metaDataStore.getRecordMetaData().getVersion();
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData().getRecordType("MyOtherRecord"));
        assertEquals(e.getMessage(), "Unknown record type MyOtherRecord");
    }
    // The evolved proto no longer has a union. The record types with no explicit usage should now automatically show up in the union.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
        if (repeatSaveOrDoUpdate) {
            metaDataStore.updateRecords(TestRecordsImplicitUsageNoUnionProto.getDescriptor());
        } else {
            metaDataStore.saveRecordMetaData(TestRecordsImplicitUsageNoUnionProto.getDescriptor());
        }
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyOtherRecord"));
        assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
        context.commit();
    }
}
Also used : MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Example 30 with BooleanSource

use of com.apple.test.BooleanSource in project fdb-record-layer by FoundationDB.

the class FunctionKeyIndexTest method testOneOfQueryCoveringValueIndex.

@ParameterizedTest
@BooleanSource
public void testOneOfQueryCoveringValueIndex(boolean negated) throws Exception {
    Index coveringIndex = new Index("int_str_index", "int_value", "str_value");
    Records records = Records.create();
    for (int i = 0; i < 10; i++) {
        String strValue = (char) ('a' + i) + "_" + (char) ('b' + i);
        records.add(i, strValue);
    }
    saveRecords(records, coveringIndex);
    QueryComponent funcFilter = Query.keyExpression(function("chars", field("str_value"))).oneOfThem().equalsValue("c");
    if (negated) {
        funcFilter = Query.not(funcFilter);
    }
    RecordQuery query = RecordQuery.newBuilder().setRecordType("TypesRecord").setFilter(Query.and(Query.field("int_value").greaterThan(1), funcFilter)).setRequiredResults(Collections.singletonList(field("int_value"))).build();
    // Covering(Index(int_str_index ([1],>) -> [int_value: KEY[0], long_value: KEY[2], str_value: KEY[1]]) | ANY chars(Field { 'str_value' None}) EQUALS c
    RecordQueryPlan plan = planner.plan(query);
    assertThat(plan, PlanMatchers.filter(funcFilter, coveringIndexScan(indexScan(allOf(indexName(coveringIndex.getName()), bounds(hasTupleString("([1],>")))))));
    if (negated) {
        assertEquals(-1785473858, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
        assertEquals(-842591344, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
        assertEquals(-1235952736, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
    } else {
        assertEquals(-1785473859, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
        assertEquals(-1937041998, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
        assertEquals(1964563906, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
    }
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context, coveringIndex);
        List<Integer> results = recordStore.executeQuery(plan).map(r -> fromMessage(r.getRecord()).getIntValue()).asList().join();
        // 0 - 1 indexed out; 2 returned; 3 - 9 filtered out; last two reversed when negated.
        if (negated) {
            assertEquals(Arrays.asList(3, 4, 5, 6, 7, 8, 9), results);
            assertDiscardedExactly(1, context);
        } else {
            assertEquals(Collections.singletonList(2), results);
            assertDiscardedExactly(10 - 2 - 1, context);
        }
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) Index(com.apple.foundationdb.record.metadata.Index) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) Matchers.containsString(org.hamcrest.Matchers.containsString) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Aggregations

BooleanSource (com.apple.test.BooleanSource)57 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)57 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)37 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)37 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)36 FDBQueriedRecord (com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord)24 Message (com.google.protobuf.Message)23 QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)14 RecordMetaDataBuilder (com.apple.foundationdb.record.RecordMetaDataBuilder)10 LuceneQueryComponent (com.apple.foundationdb.record.query.expressions.LuceneQueryComponent)10 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)9 CascadesPlanner (com.apple.foundationdb.record.query.plan.temp.CascadesPlanner)9 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)9 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)9 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)9 Test (org.junit.jupiter.api.Test)9 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)7 RecordCursorIterator (com.apple.foundationdb.record.RecordCursorIterator)6 Index (com.apple.foundationdb.record.metadata.Index)6 Tuple (com.apple.foundationdb.tuple.Tuple)6