Search in sources :

Example 41 with FDBQueriedRecord

use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.

the class SortCursorTests method memorySortContinuations.

@Test
public void memorySortContinuations() throws Exception {
    final Function<byte[], RecordCursor<FDBQueriedRecord<Message>>> scanRecords = continuation -> {
        final ExecuteProperties executeProperties = ExecuteProperties.newBuilder().setScannedRecordsLimit(20).build();
        return recordStore.scanRecords(null, null, EndpointType.TREE_START, EndpointType.TREE_END, continuation, new ScanProperties(executeProperties)).map(FDBQueriedRecord::stored);
    };
    final MemoryAdapterBase adapter = new MemoryAdapterBase() {

        @Override
        public int getMaxRecordCountInMemory() {
            return 10;
        }
    };
    List<Integer> resultNums = new ArrayList<>();
    byte[] continuation = null;
    int transactionCount = 0;
    do {
        try (FDBRecordContext context = openContext()) {
            openSimpleRecordStore(context);
            try (RecordCursor<FDBQueriedRecord<Message>> cursor = MemorySortCursor.create(adapter, scanRecords, timer, continuation)) {
                while (true) {
                    RecordCursorResult<FDBQueriedRecord<Message>> result = cursor.getNext();
                    if (result.hasNext()) {
                        int num2 = TestRecords1Proto.MySimpleRecord.newBuilder().mergeFrom(result.get().getRecord()).getNumValue2();
                        resultNums.add(num2);
                    } else {
                        continuation = result.getContinuation().toBytes();
                        break;
                    }
                }
            }
            transactionCount++;
        }
    } while (continuation != null);
    assertEquals(110, transactionCount);
    assertEquals(sortedNums, resultNums);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) FileSortAdapter(com.apple.foundationdb.record.sorting.FileSortAdapter) MemorySortAdapter(com.apple.foundationdb.record.sorting.MemorySortAdapter) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Random(java.util.Random) Function(java.util.function.Function) KeyGenerator(javax.crypto.KeyGenerator) ArrayList(java.util.ArrayList) Key(com.apple.foundationdb.record.metadata.Key) SecureRandom(java.security.SecureRandom) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) FileSortCursor(com.apple.foundationdb.record.sorting.FileSortCursor) Tuple(com.apple.foundationdb.tuple.Tuple) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) EndpointType(com.apple.foundationdb.record.EndpointType) ScanProperties(com.apple.foundationdb.record.ScanProperties) SortedRecordSerializer(com.apple.foundationdb.record.provider.foundationdb.SortedRecordSerializer) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) CodedOutputStream(com.google.protobuf.CodedOutputStream) Nullable(javax.annotation.Nullable) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) MemorySortCursor(com.apple.foundationdb.record.sorting.MemorySortCursor) Tags(com.apple.test.Tags) IOException(java.io.IOException) CipherPool(com.apple.foundationdb.record.provider.common.CipherPool) File(java.io.File) FDBRecordStoreTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreTestBase) Test(org.junit.jupiter.api.Test) List(java.util.List) CodedInputStream(com.google.protobuf.CodedInputStream) MemorySorter(com.apple.foundationdb.record.sorting.MemorySorter) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) Message(com.google.protobuf.Message) RecordCursor(com.apple.foundationdb.record.RecordCursor) SecretKey(javax.crypto.SecretKey) DynamicMessageRecordSerializer(com.apple.foundationdb.record.provider.common.DynamicMessageRecordSerializer) Collections(java.util.Collections) RecordCursor(com.apple.foundationdb.record.RecordCursor) Message(com.google.protobuf.Message) ArrayList(java.util.ArrayList) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScanProperties(com.apple.foundationdb.record.ScanProperties) Test(org.junit.jupiter.api.Test)

Example 42 with FDBQueriedRecord

use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.

the class SortCursorTests method memorySort.

@Test
public void memorySort() throws Exception {
    final Function<byte[], RecordCursor<FDBQueriedRecord<Message>>> scanRecords = continuation -> recordStore.scanRecords(null, null, EndpointType.TREE_START, EndpointType.TREE_END, continuation, ScanProperties.FORWARD_SCAN).map(FDBQueriedRecord::stored);
    final MemoryAdapterBase adapter = new MemoryAdapterBase() {

        @Override
        public int getMaxRecordCountInMemory() {
            return 20;
        }
    };
    List<Integer> resultNums;
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context);
        try (RecordCursor<FDBQueriedRecord<Message>> cursor = MemorySortCursor.create(adapter, scanRecords, timer, null)) {
            resultNums = cursor.map(r -> TestRecords1Proto.MySimpleRecord.newBuilder().mergeFrom(r.getRecord()).getNumValue2()).asList().get();
        }
    }
    assertEquals(sortedNums.subList(0, 20), resultNums);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) FileSortAdapter(com.apple.foundationdb.record.sorting.FileSortAdapter) MemorySortAdapter(com.apple.foundationdb.record.sorting.MemorySortAdapter) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Random(java.util.Random) Function(java.util.function.Function) KeyGenerator(javax.crypto.KeyGenerator) ArrayList(java.util.ArrayList) Key(com.apple.foundationdb.record.metadata.Key) SecureRandom(java.security.SecureRandom) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) FileSortCursor(com.apple.foundationdb.record.sorting.FileSortCursor) Tuple(com.apple.foundationdb.tuple.Tuple) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) EndpointType(com.apple.foundationdb.record.EndpointType) ScanProperties(com.apple.foundationdb.record.ScanProperties) SortedRecordSerializer(com.apple.foundationdb.record.provider.foundationdb.SortedRecordSerializer) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) CodedOutputStream(com.google.protobuf.CodedOutputStream) Nullable(javax.annotation.Nullable) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) MemorySortCursor(com.apple.foundationdb.record.sorting.MemorySortCursor) Tags(com.apple.test.Tags) IOException(java.io.IOException) CipherPool(com.apple.foundationdb.record.provider.common.CipherPool) File(java.io.File) FDBRecordStoreTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreTestBase) Test(org.junit.jupiter.api.Test) List(java.util.List) CodedInputStream(com.google.protobuf.CodedInputStream) MemorySorter(com.apple.foundationdb.record.sorting.MemorySorter) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) Message(com.google.protobuf.Message) RecordCursor(com.apple.foundationdb.record.RecordCursor) SecretKey(javax.crypto.SecretKey) DynamicMessageRecordSerializer(com.apple.foundationdb.record.provider.common.DynamicMessageRecordSerializer) Collections(java.util.Collections) RecordCursor(com.apple.foundationdb.record.RecordCursor) Message(com.google.protobuf.Message) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Test(org.junit.jupiter.api.Test)

Example 43 with FDBQueriedRecord

use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.

the class FDBRecordStoreQueryTest method testParameterQuery1.

/**
 * Verify that index lookups work with parameterized queries.
 */
@DualPlannerTest
void testParameterQuery1() throws Exception {
    RecordMetaDataHook hook = complexQuerySetupHook();
    complexQuerySetup(hook);
    RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.and(Query.field("str_value_indexed").equalsParameter("1"), Query.field("num_value_2").equalsParameter("2"))).build();
    // Index(multi_index [EQUALS $1, EQUALS $2])
    RecordQueryPlan plan = planner.plan(query);
    assertMatchesExactly(plan, indexPlan().where(indexName("multi_index")).and(scanComparisons(range("[EQUALS $1, EQUALS $2]"))));
    assertEquals(584809367, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
    assertEquals(1148926968, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
    assertEquals(1148926968, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        for (int attempt = 1; attempt <= 2; attempt++) {
            String strValue;
            int numValue2;
            switch(attempt) {
                case 1:
                    strValue = "even";
                    numValue2 = 1;
                    break;
                case 2:
                default:
                    strValue = "odd";
                    numValue2 = 2;
                    break;
            }
            Bindings.Builder bindings = Bindings.newBuilder();
            bindings.set("1", strValue);
            bindings.set("2", numValue2);
            EvaluationContext evaluationContext = EvaluationContext.forBindings(bindings.build());
            int i = 0;
            try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = plan.execute(recordStore, evaluationContext).asIterator()) {
                while (cursor.hasNext()) {
                    FDBQueriedRecord<Message> rec = cursor.next();
                    TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
                    myrec.mergeFrom(rec.getRecord());
                    assertEquals(strValue, myrec.getStrValueIndexed());
                    assertTrue((myrec.getNumValue2() % 3) == numValue2);
                    i++;
                }
            }
            assertEquals(16, i);
        }
        assertDiscardedNone(context);
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) Message(com.google.protobuf.Message) Bindings(com.apple.foundationdb.record.Bindings) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery)

Example 44 with FDBQueriedRecord

use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.

the class FDBRecordStoreQueryTestBase method querySimpleRecordStoreWithContinuation.

/**
 * A query execution utility that can handle continuations. This is very similar to the above {@link #querySimpleRecordStore}
 * with the additional support for {@link ExecuteProperties} and continuation.
 * This method returns the last result encountered. In the case where the row limit was encountered, this would be the one
 * result that contains the continuation that should be used on the next call.
 * @param recordMetaDataHook Metadata hook to invoke while opening store
 * @param plan the plan to execute
 * @param contextSupplier provider method to get execution context
 * @param continuation execution continuation
 * @param executeProperties execution properties to pass into the execute method
 * @param checkNumRecords Consumer that verifies correct number of records returned
 * @param checkRecord Consumer that asserts every record retrieved
 * @param checkDiscarded Consumer that asserts the number of discarded records
 * @return the last result from the cursor
 * @throws Throwable any thrown exception, or its cause if the exception is a {@link ExecutionException}
 */
protected RecordCursorResult<FDBQueriedRecord<Message>> querySimpleRecordStoreWithContinuation(@Nonnull RecordMetaDataHook recordMetaDataHook, @Nonnull RecordQueryPlan plan, @Nonnull Supplier<EvaluationContext> contextSupplier, @Nullable byte[] continuation, @Nonnull ExecuteProperties executeProperties, @Nonnull Consumer<Integer> checkNumRecords, @Nonnull Consumer<TestRecords1Proto.MySimpleRecord.Builder> checkRecord, @Nonnull Consumer<FDBRecordContext> checkDiscarded) throws Throwable {
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, recordMetaDataHook);
        AtomicInteger i = new AtomicInteger(0);
        CompletableFuture<RecordCursorResult<FDBQueriedRecord<Message>>> lastResult;
        RecordCursor<FDBQueriedRecord<Message>> cursor = plan.execute(recordStore, contextSupplier.get(), continuation, executeProperties);
        lastResult = cursor.forEachResult(result -> {
            TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
            myrec.mergeFrom(result.get().getRecord());
            checkRecord.accept(myrec);
            i.incrementAndGet();
        });
        lastResult.get();
        checkNumRecords.accept(i.get());
        checkDiscarded.accept(context);
        // TODO a hack until this gets refactored properly
        clearStoreCounter(context);
        return lastResult.get();
    } catch (ExecutionException ex) {
        throw ex.getCause();
    }
}
Also used : IntStream(java.util.stream.IntStream) Assertions.fail(org.junit.jupiter.api.Assertions.fail) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) TestRecords3Proto(com.apple.foundationdb.record.TestRecords3Proto) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) CompletableFuture(java.util.concurrent.CompletableFuture) TestRecordsTupleFieldsProto(com.apple.foundationdb.record.TestRecordsTupleFieldsProto) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) RecordQueryPlanner(com.apple.foundationdb.record.query.plan.RecordQueryPlanner) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) TestHelpers(com.apple.foundationdb.record.TestHelpers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) BiConsumer(java.util.function.BiConsumer) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) Nonnull(javax.annotation.Nonnull) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) Nullable(javax.annotation.Nullable) TestRecords4Proto(com.apple.foundationdb.record.TestRecords4Proto) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) TupleFieldsHelper(com.apple.foundationdb.record.metadata.expressions.TupleFieldsHelper) FanType(com.apple.foundationdb.record.metadata.expressions.KeyExpression.FanType) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) FDBRecordStoreTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreTestBase) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) List(java.util.List) BindingMatcher(com.apple.foundationdb.record.query.plan.temp.matchers.BindingMatcher) TestRecords5Proto(com.apple.foundationdb.record.TestRecords5Proto) Index(com.apple.foundationdb.record.metadata.Index) 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) Message(com.google.protobuf.Message) RecordCursor(com.apple.foundationdb.record.RecordCursor) TestRecordsWithHeaderProto(com.apple.foundationdb.record.TestRecordsWithHeaderProto) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Message(com.google.protobuf.Message) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) ExecutionException(java.util.concurrent.ExecutionException)

Example 45 with FDBQueriedRecord

use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.

the class FDBRecordStoreQueryTestBase method querySimpleRecordStore.

protected int querySimpleRecordStore(RecordMetaDataHook recordMetaDataHook, RecordQueryPlan plan, Supplier<EvaluationContext> contextSupplier, TestHelpers.DangerousConsumer<TestRecords1Proto.MySimpleRecord.Builder> checkRecord, TestHelpers.DangerousConsumer<FDBRecordContext> checkDiscarded) throws Exception {
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, recordMetaDataHook);
        int i = 0;
        try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = plan.execute(recordStore, contextSupplier.get()).asIterator()) {
            while (cursor.hasNext()) {
                FDBQueriedRecord<Message> rec = cursor.next();
                TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
                myrec.mergeFrom(rec.getRecord());
                checkRecord.accept(myrec);
                i++;
            }
        }
        checkDiscarded.accept(context);
        // TODO a hack until this gets refactored properly
        clearStoreCounter(context);
        return i;
    }
}
Also used : FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) Message(com.google.protobuf.Message) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)

Aggregations

FDBQueriedRecord (com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord)137 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)125 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)117 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)116 Message (com.google.protobuf.Message)108 Test (org.junit.jupiter.api.Test)65 Tags (com.apple.test.Tags)52 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)52 Tag (org.junit.jupiter.api.Tag)52 RecordQueryPlanner (com.apple.foundationdb.record.query.plan.RecordQueryPlanner)50 Query (com.apple.foundationdb.record.query.expressions.Query)47 List (java.util.List)47 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)46 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)45 Index (com.apple.foundationdb.record.metadata.Index)45 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)45 ArrayList (java.util.ArrayList)43 Collections (java.util.Collections)42 PlanHashable (com.apple.foundationdb.record.PlanHashable)41 Tuple (com.apple.foundationdb.tuple.Tuple)41