Search in sources :

Example 86 with RecordQuery

use of com.apple.foundationdb.record.query.RecordQuery 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())));
    }
}
Also used : IndexEntry(com.apple.foundationdb.record.IndexEntry) Arrays(java.util.Arrays) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Disabled(org.junit.jupiter.api.Disabled) PlanMatchers.bounds(com.apple.foundationdb.record.query.plan.match.PlanMatchers.bounds) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Tag(org.junit.jupiter.api.Tag) MethodSource(org.junit.jupiter.params.provider.MethodSource) Query(com.apple.foundationdb.record.query.expressions.Query) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Matchers.allOf(org.hamcrest.Matchers.allOf) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) List(java.util.List) Stream(java.util.stream.Stream) PlanMatchers.indexName(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexName) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RecordTypeKeyComparison(com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Matchers.anyOf(org.hamcrest.Matchers.anyOf) FDBRecordStoreQueryTestBase(com.apple.foundationdb.record.provider.foundationdb.query.FDBRecordStoreQueryTestBase) IntStream(java.util.stream.IntStream) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) PlanMatchers.indexScan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexScan) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ScanProperties(com.apple.foundationdb.record.ScanProperties) Expressions.empty(com.apple.foundationdb.record.metadata.Key.Expressions.empty) PlanMatchers.scan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.scan) 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) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) Tags(com.apple.test.Tags) Matchers(org.hamcrest.Matchers) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Expressions.recordType(com.apple.foundationdb.record.metadata.Key.Expressions.recordType) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Index(com.apple.foundationdb.record.metadata.Index) Assumptions(org.junit.jupiter.api.Assumptions) PlanMatchers.unbounded(com.apple.foundationdb.record.query.plan.match.PlanMatchers.unbounded) TestHelpers.assertThrows(com.apple.foundationdb.record.TestHelpers.assertThrows) Message(com.google.protobuf.Message) Collections(java.util.Collections) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 87 with RecordQuery

use of com.apple.foundationdb.record.query.RecordQuery 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]")))));
    }
}
Also used : IndexEntry(com.apple.foundationdb.record.IndexEntry) Arrays(java.util.Arrays) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Disabled(org.junit.jupiter.api.Disabled) PlanMatchers.bounds(com.apple.foundationdb.record.query.plan.match.PlanMatchers.bounds) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Tag(org.junit.jupiter.api.Tag) MethodSource(org.junit.jupiter.params.provider.MethodSource) Query(com.apple.foundationdb.record.query.expressions.Query) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Matchers.allOf(org.hamcrest.Matchers.allOf) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) List(java.util.List) Stream(java.util.stream.Stream) PlanMatchers.indexName(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexName) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RecordTypeKeyComparison(com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Matchers.anyOf(org.hamcrest.Matchers.anyOf) FDBRecordStoreQueryTestBase(com.apple.foundationdb.record.provider.foundationdb.query.FDBRecordStoreQueryTestBase) IntStream(java.util.stream.IntStream) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) PlanMatchers.indexScan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexScan) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ScanProperties(com.apple.foundationdb.record.ScanProperties) Expressions.empty(com.apple.foundationdb.record.metadata.Key.Expressions.empty) PlanMatchers.scan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.scan) 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) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) Tags(com.apple.test.Tags) Matchers(org.hamcrest.Matchers) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Expressions.recordType(com.apple.foundationdb.record.metadata.Key.Expressions.recordType) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Index(com.apple.foundationdb.record.metadata.Index) Assumptions(org.junit.jupiter.api.Assumptions) PlanMatchers.unbounded(com.apple.foundationdb.record.query.plan.match.PlanMatchers.unbounded) TestHelpers.assertThrows(com.apple.foundationdb.record.TestHelpers.assertThrows) Message(com.google.protobuf.Message) Collections(java.util.Collections) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Message(com.google.protobuf.Message) Index(com.apple.foundationdb.record.metadata.Index) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource) Disabled(org.junit.jupiter.api.Disabled)

Example 88 with RecordQuery

use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.

the class OnlineIndexerBuildValueIndexTest method valueRebuild.

private void valueRebuild(@Nonnull List<TestRecords1Proto.MySimpleRecord> records, @Nullable List<TestRecords1Proto.MySimpleRecord> recordsWhileBuilding, int agents, boolean overlap, boolean splitLongRecords) {
    Index index = new Index("newIndex", field("num_value_2"));
    Function<FDBQueriedRecord<Message>, Integer> projection = rec -> {
        TestRecords1Proto.MySimpleRecord simple = TestRecords1Proto.MySimpleRecord.newBuilder().mergeFrom(rec.getRecord()).build();
        if (simple.hasNumValue2()) {
            return simple.getNumValue2();
        } else {
            return Integer.MIN_VALUE;
        }
    };
    List<RecordQuery> queries = records.stream().map(record -> {
        Integer value2 = (record.hasNumValue2()) ? record.getNumValue2() : null;
        return RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(value2 != null ? Query.field("num_value_2").equalsValue(record.getNumValue2()) : Query.field("num_value_2").isNull()).build();
    }).collect(Collectors.toList());
    Function<TestRecords1Proto.MySimpleRecord, Integer> indexValue = msg -> msg.hasNumValue2() ? msg.getNumValue2() : null;
    Map<Integer, List<Message>> valueMap = group(records, indexValue);
    Runnable beforeBuild = () -> {
        try (FDBRecordContext context = openContext()) {
            for (int i = 0; i < queries.size(); i++) {
                Integer value2 = (records.get(i).hasNumValue2()) ? records.get(i).getNumValue2() : null;
                String planString = "Scan(<,>) | [MySimpleRecord] | " + ((value2 == null) ? "num_value_2 IS_NULL" : "num_value_2 EQUALS " + value2);
                executeQuery(queries.get(i), planString, valueMap.get(value2));
            }
            context.commit();
        }
    };
    List<TestRecords1Proto.MySimpleRecord> updatedRecords;
    List<RecordQuery> updatedQueries;
    Map<Integer, List<Message>> updatedValueMap;
    if (recordsWhileBuilding == null || recordsWhileBuilding.size() == 0) {
        updatedRecords = records;
        updatedQueries = queries;
        updatedValueMap = valueMap;
    } else {
        updatedRecords = updated(records, recordsWhileBuilding);
        updatedQueries = updatedRecords.stream().map(record -> {
            Integer value2 = (record.hasNumValue2()) ? record.getNumValue2() : null;
            return RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(value2 != null ? Query.field("num_value_2").equalsValue(record.getNumValue2()) : Query.field("num_value_2").isNull()).build();
        }).collect(Collectors.toList());
        updatedValueMap = group(updatedRecords, indexValue);
    }
    Runnable afterBuild = new Runnable() {

        @SuppressWarnings("try")
        @Override
        public void run() {
            try (FDBRecordContext context = openContext()) {
                // The build job shouldn't affect the reads.
                for (int i = 0; i < updatedQueries.size(); i++) {
                    Integer value2 = (updatedRecords.get(i).hasNumValue2()) ? updatedRecords.get(i).getNumValue2() : null;
                    String planString = "Scan(<,>) | [MySimpleRecord] | " + ((value2 == null) ? "num_value_2 IS_NULL" : "num_value_2 EQUALS " + value2);
                    executeQuery(updatedQueries.get(i), planString, updatedValueMap.get(value2));
                }
            }
        }
    };
    Runnable afterReadable = () -> {
        try (FDBRecordContext context = openContext()) {
            for (int i = 0; i < updatedQueries.size(); i++) {
                Integer value2 = (updatedRecords.get(i).hasNumValue2()) ? updatedRecords.get(i).getNumValue2() : null;
                executeQuery(updatedQueries.get(i), "Index(newIndex [[" + value2 + "],[" + value2 + "]])", updatedValueMap.get(value2));
            }
            RecordQuery sortQuery = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setSort(field("num_value_2")).build();
            executeQuery(sortQuery, "Index(newIndex <,>)", updatedRecords.stream().map(msg -> (msg.hasNumValue2()) ? msg.getNumValue2() : Integer.MIN_VALUE).sorted().collect(Collectors.toList()), projection);
            context.commit();
        }
    };
    singleRebuild(records, recordsWhileBuilding, agents, overlap, splitLongRecords, index, beforeBuild, afterBuild, afterReadable);
}
Also used : IntStream(java.util.stream.IntStream) Query(com.apple.foundationdb.record.query.expressions.Query) Arrays(java.util.Arrays) LongStream(java.util.stream.LongStream) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Tags(com.apple.test.Tags) Random(java.util.Random) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Strings(com.google.common.base.Strings) List(java.util.List) Stream(java.util.stream.Stream) Index(com.apple.foundationdb.record.metadata.Index) Map(java.util.Map) Message(com.google.protobuf.Message) Tag(org.junit.jupiter.api.Tag) Comparator(java.util.Comparator) Nonnull(javax.annotation.Nonnull) Collections(java.util.Collections) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) Nullable(javax.annotation.Nullable) Index(com.apple.foundationdb.record.metadata.Index) List(java.util.List) RecordQuery(com.apple.foundationdb.record.query.RecordQuery)

Example 89 with RecordQuery

use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.

the class OnlineIndexerBuildVersionIndexTest method versionRebuild.

private void versionRebuild(@Nonnull List<TestRecords1Proto.MySimpleRecord> records, @Nullable List<TestRecords1Proto.MySimpleRecord> recordsWhileBuilding, int agents, boolean overlap) {
    final Index index = new Index("newVersionIndex", concat(field("num_value_2"), VersionKeyExpression.VERSION), IndexTypes.VERSION);
    final Function<FDBQueriedRecord<Message>, Tuple> projection = rec -> {
        TestRecords1Proto.MySimpleRecord simple = TestRecords1Proto.MySimpleRecord.newBuilder().mergeFrom(rec.getRecord()).build();
        Integer numValue2 = (simple.hasNumValue2()) ? simple.getNumValue2() : null;
        FDBRecordVersion version = rec.hasVersion() ? rec.getVersion() : null;
        if (version != null) {
            assertTrue(version.isComplete());
        }
        return Tuple.from(numValue2, (version == null) ? null : version.toVersionstamp());
    };
    List<RecordQuery> queries = records.stream().map(record -> {
        Integer value2 = (record.hasNumValue2()) ? record.getNumValue2() : null;
        return RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(value2 != null ? Query.field("num_value_2").equalsValue(record.getNumValue2()) : Query.field("num_value_2").isNull()).setSort(VersionKeyExpression.VERSION).build();
    }).collect(Collectors.toList());
    Function<TestRecords1Proto.MySimpleRecord, Integer> indexValue = msg -> msg.hasNumValue2() ? msg.getNumValue2() : null;
    Map<Integer, List<Message>> valueMap = group(records, indexValue);
    Map<Long, FDBRecordVersion> versionMap = new HashMap<>(records.size() + (recordsWhileBuilding == null ? 0 : recordsWhileBuilding.size()));
    AtomicReference<FDBRecordVersion> greatestVersion = new AtomicReference<>(null);
    final Runnable beforeBuild = () -> {
        try (FDBRecordContext context = openContext()) {
            for (int i = 0; i < queries.size(); i++) {
                Integer value2 = (records.get(i).hasNumValue2()) ? records.get(i).getNumValue2() : null;
                try {
                    executeQuery(queries.get(i), "Index(newVersionIndex [[" + value2 + "],[" + value2 + "])", valueMap.get(value2));
                    fail("somehow executed query with new index before build");
                } catch (RecordCoreException e) {
                    assertEquals("Cannot sort without appropriate index: Version", e.getMessage());
                }
            }
            // Load all the version information for the records that were initially there.
            for (TestRecords1Proto.MySimpleRecord simple : records) {
                recordStore.loadRecordVersion(Tuple.from(simple.getRecNo())).ifPresent(version -> {
                    versionMap.put(simple.getRecNo(), version);
                    if (greatestVersion.get() == null || version.compareTo(greatestVersion.get()) > 0) {
                        greatestVersion.set(version);
                    }
                });
            }
            context.commit();
        }
    };
    List<TestRecords1Proto.MySimpleRecord> updatedRecords;
    List<RecordQuery> updatedQueries;
    Map<Integer, List<Message>> updatedValueMap;
    if (recordsWhileBuilding == null || recordsWhileBuilding.size() == 0) {
        updatedRecords = records;
        updatedQueries = queries;
        updatedValueMap = valueMap;
    } else {
        updatedRecords = updated(records, recordsWhileBuilding);
        updatedQueries = updatedRecords.stream().map(record -> {
            Integer value2 = (record.hasNumValue2()) ? record.getNumValue2() : null;
            return RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(value2 != null ? Query.field("num_value_2").equalsValue(record.getNumValue2()) : Query.field("num_value_2").isNull()).setSort(VersionKeyExpression.VERSION).build();
        }).collect(Collectors.toList());
        updatedValueMap = group(updatedRecords, indexValue);
    }
    Map<Long, FDBRecordVersion> updatedVersionMap = new HashMap<>(versionMap.size());
    Set<Long> newRecordKeys = (recordsWhileBuilding == null) ? Collections.emptySet() : recordsWhileBuilding.stream().map(TestRecords1Proto.MySimpleRecord::getRecNo).collect(Collectors.toSet());
    Runnable afterBuild = new Runnable() {

        @SuppressWarnings("try")
        @Override
        public void run() {
            try (FDBRecordContext context = openContext()) {
                // The build job shouldn't affect the reads.
                for (int i = 0; i < updatedQueries.size(); i++) {
                    Integer value2 = (updatedRecords.get(i).hasNumValue2()) ? updatedRecords.get(i).getNumValue2() : null;
                    try {
                        executeQuery(updatedQueries.get(i), "Index(newVersionIndex [[" + value2 + "],[" + value2 + "])", updatedValueMap.get(value2));
                        fail("somehow executed query with new index before readable");
                    } catch (RecordCoreException e) {
                        assertEquals("Cannot sort without appropriate index: Version", e.getMessage());
                    }
                }
                // Load all the version information for records that are there now and that values are sane.
                for (TestRecords1Proto.MySimpleRecord simple : updatedRecords) {
                    recordStore.loadRecordVersion(Tuple.from(simple.getRecNo())).ifPresent(version -> {
                        assertTrue(version.isComplete());
                        if (newRecordKeys.contains(simple.getRecNo())) {
                            assertThat(version, greaterThan(greatestVersion.get()));
                            if (versionMap.containsKey(simple.getRecNo())) {
                                assertThat(version, greaterThan(versionMap.get(simple.getRecNo())));
                            }
                        } else {
                            if (versionMap.containsKey(simple.getRecNo())) {
                                assertEquals(versionMap.get(simple.getRecNo()), version);
                            }
                        }
                        updatedVersionMap.put(simple.getRecNo(), version);
                    });
                }
            }
        }
    };
    Runnable afterReadable = () -> {
        Descriptors.FieldDescriptor recNoFieldDescriptor = TestRecords1Proto.MySimpleRecord.getDescriptor().findFieldByName("rec_no");
        try (FDBRecordContext context = openContext()) {
            for (int i = 0; i < updatedQueries.size(); i++) {
                Integer value2 = (updatedRecords.get(i).hasNumValue2()) ? updatedRecords.get(i).getNumValue2() : null;
                List<Tuple> sortedValues = updatedValueMap.get(value2).stream().map(msg -> {
                    FDBRecordVersion version = updatedVersionMap.get(((Number) msg.getField(recNoFieldDescriptor)).longValue());
                    return Tuple.from(value2, version == null ? null : version.toVersionstamp());
                }).sorted().collect(Collectors.toList());
                executeQuery(updatedQueries.get(i), "Index(newVersionIndex [[" + value2 + "],[" + value2 + "]])", sortedValues, projection);
            }
            context.commit();
        }
    };
    singleRebuild(records, recordsWhileBuilding, agents, overlap, false, index, beforeBuild, afterBuild, afterReadable);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Descriptors(com.google.protobuf.Descriptors) HashMap(java.util.HashMap) Random(java.util.Random) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) HashSet(java.util.HashSet) Tuple(com.apple.foundationdb.tuple.Tuple) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) VersionKeyExpression(com.apple.foundationdb.record.metadata.expressions.VersionKeyExpression) Map(java.util.Map) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) Tag(org.junit.jupiter.api.Tag) 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) Query(com.apple.foundationdb.record.query.expressions.Query) LongStream(java.util.stream.LongStream) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Tags(com.apple.test.Tags) Set(java.util.Set) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) Index(com.apple.foundationdb.record.metadata.Index) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Message(com.google.protobuf.Message) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Collections(java.util.Collections) HashMap(java.util.HashMap) Index(com.apple.foundationdb.record.metadata.Index) List(java.util.List) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) AtomicReference(java.util.concurrent.atomic.AtomicReference) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Tuple(com.apple.foundationdb.tuple.Tuple)

Example 90 with RecordQuery

use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.

the class FDBRecordStoreQueryTest method nullableInt32.

@DualPlannerTest
void nullableInt32() throws Exception {
    try (FDBRecordContext context = openContext()) {
        final List<UUID> uuids = setupTupleFields(context);
        RecordQuery query = RecordQuery.newBuilder().setRecordType("MyFieldsRecord").setFilter(Query.field("fint32").isNull()).build();
        RecordQueryPlan plan = planner.plan(query);
        assertMatchesExactly(plan, indexPlan().where(indexName("MyFieldsRecord$fint32")).and(scanComparisons(range("[[null],[null]]"))));
        assertEquals(uuids.subList(3, 4), recordStore.executeQuery(plan).map(r -> r.getPrimaryKey().getUUID(0)).asList().join());
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) Arrays(java.util.Arrays) RecordQueryPlanMatchers.scanPlan(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers.scanPlan) Bindings(com.apple.foundationdb.record.Bindings) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) RecordQueryPlanMatchers.predicates(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers.predicates) RecordQueryPlanner(com.apple.foundationdb.record.query.plan.RecordQueryPlanner) TestHelpers.assertDiscardedNone(com.apple.foundationdb.record.TestHelpers.assertDiscardedNone) TestRecordsMultiProto(com.apple.foundationdb.record.TestRecordsMultiProto) Tuple(com.apple.foundationdb.tuple.Tuple) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) ListMatcher.only(com.apple.foundationdb.record.query.plan.temp.matchers.ListMatcher.only) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RecordQueryPlanMatchers.indexName(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers.indexName) Tag(org.junit.jupiter.api.Tag) Query(com.apple.foundationdb.record.query.expressions.Query) ImmutableSet(com.google.common.collect.ImmutableSet) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) RecordQueryPlanMatchers.recordTypes(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers.recordTypes) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) List(java.util.List) ScanComparisons.range(com.apple.foundationdb.record.query.plan.ScanComparisons.range) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TestRecordsEnumProto(com.apple.foundationdb.record.TestRecordsEnumProto) RecordQueryPlanComplexityException(com.apple.foundationdb.record.query.plan.RecordQueryPlanComplexityException) RecordQueryPlanMatchers.unionPlan(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers.unionPlan) RecordQueryPlanMatchers(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers) RecordQueryPlanMatchers.predicatesFilterPlan(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers.predicatesFilterPlan) IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ValueMatchers.anyValue(com.apple.foundationdb.record.query.plan.temp.matchers.ValueMatchers.anyValue) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) PrimitiveMatchers.equalsObject(com.apple.foundationdb.record.query.plan.temp.matchers.PrimitiveMatchers.equalsObject) QueryPredicateMatchers.queryComponentPredicate(com.apple.foundationdb.record.query.plan.temp.matchers.QueryPredicateMatchers.queryComponentPredicate) TestRecordsTupleFieldsProto(com.apple.foundationdb.record.TestRecordsTupleFieldsProto) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) PlanHashable(com.apple.foundationdb.record.PlanHashable) ArrayList(java.util.ArrayList) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) Lists(com.google.common.collect.Lists) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) RecordQueryPlanMatchers.scanComparisons(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers.scanComparisons) RecordQueryPlanMatchers.unorderedPrimaryKeyDistinctPlan(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers.unorderedPrimaryKeyDistinctPlan) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) FDBStoredRecord(com.apple.foundationdb.record.provider.foundationdb.FDBStoredRecord) IsolationLevel(com.apple.foundationdb.record.IsolationLevel) LongStream(java.util.stream.LongStream) ScanComparisons.unbounded(com.apple.foundationdb.record.query.plan.ScanComparisons.unbounded) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) TupleFieldsHelper(com.apple.foundationdb.record.metadata.expressions.TupleFieldsHelper) Tags(com.apple.test.Tags) QueryPredicateMatchers.valuePredicate(com.apple.foundationdb.record.query.plan.temp.matchers.QueryPredicateMatchers.valuePredicate) PrimitiveMatchers.containsAll(com.apple.foundationdb.record.query.plan.temp.matchers.PrimitiveMatchers.containsAll) RecordQueryPlanMatchers.queryComponents(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers.queryComponents) RecordQueryPlanMatchers.typeFilterPlan(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers.typeFilterPlan) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) RecordQueryPlanMatchers.filterPlan(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers.filterPlan) Index(com.apple.foundationdb.record.metadata.Index) RecordQueryPlanMatchers.indexPlan(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers.indexPlan) TestHelpers.assertDiscardedAtMost(com.apple.foundationdb.record.TestHelpers.assertDiscardedAtMost) Message(com.google.protobuf.Message) RecordCursor(com.apple.foundationdb.record.RecordCursor) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) ListMatcher.exactly(com.apple.foundationdb.record.query.plan.temp.matchers.ListMatcher.exactly) Collections(java.util.Collections) TestRecordsBytesProto(com.apple.foundationdb.record.TestRecordsBytesProto) ValueMatchers.fieldValue(com.apple.foundationdb.record.query.plan.temp.matchers.ValueMatchers.fieldValue) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) UUID(java.util.UUID) RecordQuery(com.apple.foundationdb.record.query.RecordQuery)

Aggregations

RecordQuery (com.apple.foundationdb.record.query.RecordQuery)334 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)250 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)222 Test (org.junit.jupiter.api.Test)205 Message (com.google.protobuf.Message)166 FDBQueriedRecord (com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord)163 Index (com.apple.foundationdb.record.metadata.Index)114 Query (com.apple.foundationdb.record.query.expressions.Query)114 Tags (com.apple.test.Tags)112 Tag (org.junit.jupiter.api.Tag)112 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)108 RecordQueryPlanner (com.apple.foundationdb.record.query.plan.RecordQueryPlanner)107 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)106 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)102 Arrays (java.util.Arrays)99 Expressions.field (com.apple.foundationdb.record.metadata.Key.Expressions.field)98 Collections (java.util.Collections)97 List (java.util.List)96 QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)95 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)93