Search in sources :

Example 6 with FDBRecordContext

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

the class ResolverMappingDigest method computeDigest.

public CompletableFuture<byte[]> computeDigest() {
    MessageDigest messageDigest;
    try {
        messageDigest = MessageDigest.getInstance(ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("invalid directory layer digest algorithm", e);
    }
    FDBRecordContext context = runner.openContext();
    return computeInternal(context, null, messageDigest).thenCompose(continuation -> {
        context.close();
        if (continuation != null) {
            return computeInternal(runner.openContext(), continuation, messageDigest);
        }
        return CompletableFuture.completedFuture(null);
    }).thenApply(ignore -> messageDigest.digest());
}
Also used : IsolationLevel(com.apple.foundationdb.record.IsolationLevel) KeyValue(com.apple.foundationdb.KeyValue) MessageDigest(java.security.MessageDigest) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) CompletableFuture(java.util.concurrent.CompletableFuture) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) KeyValueCursor(com.apple.foundationdb.record.provider.foundationdb.KeyValueCursor) Tuple(com.apple.foundationdb.tuple.Tuple) ScanProperties(com.apple.foundationdb.record.ScanProperties) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RecordCursor(com.apple.foundationdb.record.RecordCursor) API(com.apple.foundationdb.annotation.API) FDBDatabaseRunner(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseRunner) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 7 with FDBRecordContext

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

the class MetaDataVersionStampStoreStateCache method get.

@Nonnull
@Override
public CompletableFuture<FDBRecordStoreStateCacheEntry> get(@Nonnull FDBRecordStore recordStore, @Nonnull FDBRecordStoreBase.StoreExistenceCheck existenceCheck) {
    final FDBRecordContext context = recordStore.getContext();
    validateContext(context);
    if (context.hasDirtyStoreState()) {
        recordStore.increment(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS);
        return FDBRecordStoreStateCacheEntry.load(recordStore, existenceCheck);
    }
    final SubspaceProvider subspaceProvider = recordStore.getSubspaceProvider();
    final FDBRecordStoreStateCacheEntry existingEntry = cache.getIfPresent(subspaceProvider);
    if (existingEntry == null) {
        recordStore.increment(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS);
        return FDBRecordStoreStateCacheEntry.load(recordStore, existenceCheck).whenComplete((cacheEntry, err) -> {
            if (err == null && cacheEntry.getRecordStoreState().getStoreHeader().getCacheable()) {
                addToCache(subspaceProvider, cacheEntry);
            }
        });
    } else {
        return recordStore.getContext().getMetaDataVersionStampAsync(IsolationLevel.SNAPSHOT).thenCompose(metaDataVersionStamp -> {
            if (metaDataVersionStamp == null || existingEntry.getMetaDataVersionStamp() == null || ByteArrayUtil.compareUnsigned(metaDataVersionStamp, existingEntry.getMetaDataVersionStamp()) != 0) {
                recordStore.increment(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS);
                return FDBRecordStoreStateCacheEntry.load(recordStore, existenceCheck).whenComplete((cacheEntry, err) -> {
                    if (err == null && metaDataVersionStamp != null) {
                        if (cacheEntry.getRecordStoreState().getStoreHeader().getCacheable()) {
                            addToCache(subspaceProvider, cacheEntry);
                        } else {
                            invalidateOlderEntry(subspaceProvider, metaDataVersionStamp);
                        }
                    }
                });
            } else {
                recordStore.increment(FDBStoreTimer.Counts.STORE_STATE_CACHE_HIT);
                return existingEntry.handleCachedState(context, existenceCheck).thenApply(ignore -> existingEntry);
            }
        });
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) SubspaceProvider(com.apple.foundationdb.record.provider.foundationdb.SubspaceProvider) Nonnull(javax.annotation.Nonnull)

Example 8 with FDBRecordContext

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

the class ChainedCursorTest method limitBy.

private void limitBy(int returnedRowLimit, int recordScanLimit, RecordCursor.NoNextReason noNextReason) {
    // Note that this test only requires a database and a context because the ChainedCursor API requires
    // a context to be passed in when you are applying scan limits.
    FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    try (FDBRecordContext context = database.openContext()) {
        ScanProperties props = new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(returnedRowLimit).setScannedRecordsLimit(recordScanLimit).setFailOnScanLimitReached(false).build());
        RecordCursorIterator<Long> cursor = new ChainedCursor<>(context, ChainedCursorTest::nextKey, (key) -> Tuple.from(key).pack(), (prevContinuation) -> Tuple.fromBytes(prevContinuation).getLong(0), null, props).asIterator();
        int count = 0;
        while (cursor.hasNext()) {
            assertEquals(Long.valueOf(count), cursor.next());
            ++count;
        }
        assertEquals(Math.min(returnedRowLimit, recordScanLimit), count);
        assertEquals(cursor.getNoNextReason(), noNextReason);
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Tags(com.apple.test.Tags) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) CompletableFuture(java.util.concurrent.CompletableFuture) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) FDBTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBTestBase) Test(org.junit.jupiter.api.Test) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) Tuple(com.apple.foundationdb.tuple.Tuple) ScanProperties(com.apple.foundationdb.record.ScanProperties) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) FDBDatabaseFactory(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory) RecordCursor(com.apple.foundationdb.record.RecordCursor) Optional(java.util.Optional) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScanProperties(com.apple.foundationdb.record.ScanProperties) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase)

Example 9 with FDBRecordContext

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

the class FDBFilterCoalescingQueryTest method simpleRangeCoalesce.

/**
 * Validate that a query for all values within a given range on an indexed field will be coalesced
 * into a single query on that field.
 */
@DualPlannerTest
public void simpleRangeCoalesce() throws Exception {
    complexQuerySetup(NO_HOOK);
    RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.and(Query.field("num_value_3_indexed").greaterThanOrEquals(0), Query.field("num_value_3_indexed").lessThanOrEquals(1))).build();
    // Index(MySimpleRecord$num_value_3_indexed [[0],[1]])
    RecordQueryPlan plan = planner.plan(query);
    assertThat(plan, indexScan(allOf(indexName("MySimpleRecord$num_value_3_indexed"), bounds(hasTupleString("[[0],[1]]")))));
    assertEquals(1869980849, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
    assertEquals(876021686, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
    assertEquals(-2083489995, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context);
        int i = 0;
        try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan).asIterator()) {
            while (cursor.hasNext()) {
                FDBQueriedRecord<Message> rec = cursor.next();
                TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
                myrec.mergeFrom(rec.getRecord());
                assertThat(myrec.getNumValue3Indexed(), allOf(greaterThanOrEqualTo(0), lessThanOrEqualTo(1)));
                assertThat(myrec.getRecNo() % 5, allOf(greaterThanOrEqualTo(0L), lessThanOrEqualTo(1L)));
                i++;
            }
        }
        assertEquals(40, i);
        assertDiscardedNone(context);
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) Message(com.google.protobuf.Message) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery)

Example 10 with FDBRecordContext

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

the class FDBFilterCoalescingQueryTest method duplicateFilters.

/**
 * Verify that the planner removes duplicate filters.
 * TODO We currently don't. Update this test when it gets implemented.
 * TODO: Some query plans include redundant filtering operations even when the index is a complete specification (https://github.com/FoundationDB/fdb-record-layer/issues/2)
 */
@Test
public void duplicateFilters() throws Exception {
    RecordMetaDataHook hook = metaData -> {
        metaData.addIndex("MySimpleRecord", new Index("multi_index", "str_value_indexed", "num_value_3_indexed"));
    };
    complexQuerySetup(hook);
    RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.and(Query.field("str_value_indexed").equalsValue("even"), Query.field("num_value_3_indexed").equalsValue(3), Query.field("num_value_3_indexed").equalsValue(3))).build();
    // Fetch(Covering(Index(multi_index [[even, 3],[even, 3]]) -> [num_value_3_indexed: KEY[1], rec_no: KEY[2], str_value_indexed: KEY[0]]) | num_value_3_indexed EQUALS 3)
    RecordQueryPlan plan = planner.plan(query);
    assertThat(plan, descendant(coveringIndexScan(indexScan(allOf(indexName("multi_index"), bounds(hasTupleString("[[even, 3],[even, 3]]")))))));
    assertEquals(-766201402, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
    assertEquals(-1632715349, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
    assertEquals(-1418679945, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        int i = 0;
        try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan).asIterator()) {
            while (cursor.hasNext()) {
                FDBQueriedRecord<Message> rec = cursor.next();
                TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
                myrec.mergeFrom(rec.getRecord());
                assertEquals("even", myrec.getStrValueIndexed());
                assertEquals(3, myrec.getNumValue3Indexed());
                assertEquals(3, myrec.getRecNo() % 5);
                i++;
            }
        }
        assertEquals(10, i);
        assertDiscardedNone(context);
    }
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) PlanMatchers.indexScan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexScan) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBRecordVersion(com.apple.foundationdb.record.provider.foundationdb.FDBRecordVersion) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Collections2(com.google.common.collect.Collections2) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) PlanMatchers.bounds(com.apple.foundationdb.record.query.plan.match.PlanMatchers.bounds) PlanHashable(com.apple.foundationdb.record.PlanHashable) TestHelpers.assertDiscardedNone(com.apple.foundationdb.record.TestHelpers.assertDiscardedNone) VersionKeyExpression(com.apple.foundationdb.record.metadata.expressions.VersionKeyExpression) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PlanMatchers.coveringIndexScan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.coveringIndexScan) Query(com.apple.foundationdb.record.query.expressions.Query) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Tags(com.apple.test.Tags) Matchers.allOf(org.hamcrest.Matchers.allOf) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) List(java.util.List) PlanMatchers.indexName(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexName) Index(com.apple.foundationdb.record.metadata.Index) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) Message(com.google.protobuf.Message) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) PlanMatchers.descendant(com.apple.foundationdb.record.query.plan.match.PlanMatchers.descendant) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.anyOf(org.hamcrest.Matchers.anyOf) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) Message(com.google.protobuf.Message) Index(com.apple.foundationdb.record.metadata.Index) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test)

Aggregations

FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)542 Test (org.junit.jupiter.api.Test)365 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)226 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)215 Message (com.google.protobuf.Message)187 FDBQueriedRecord (com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord)170 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)165 Tuple (com.apple.foundationdb.tuple.Tuple)147 Tag (org.junit.jupiter.api.Tag)136 Tags (com.apple.test.Tags)129 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)123 List (java.util.List)121 Index (com.apple.foundationdb.record.metadata.Index)119 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)114 ArrayList (java.util.ArrayList)112 Collections (java.util.Collections)100 Query (com.apple.foundationdb.record.query.expressions.Query)97 RecordQueryPlanner (com.apple.foundationdb.record.query.plan.RecordQueryPlanner)96 Arrays (java.util.Arrays)94 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)93