Search in sources :

Example 66 with Tuple

use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.

the class KeySpaceDirectoryTest method testDirectoryLayerDirectoryUsingLongs.

@Test
public void testDirectoryLayerDirectoryUsingLongs() throws Exception {
    KeySpace root = new KeySpace(new DirectoryLayerDirectory("cabinet", "cabinet").addSubdirectory(new DirectoryLayerDirectory("game")));
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    final Tuple senetTuple;
    final Tuple urTuple;
    try (FDBRecordContext context = database.openContext()) {
        senetTuple = root.path("cabinet").add("game", "senet").toTuple(context);
        urTuple = root.path("cabinet").add("game", "royal_game_of_ur").toTuple(context);
        context.commit();
    }
    try (FDBRecordContext context = database.openContext()) {
        // Verify that I can create the tuple again using the directory layer values.
        assertEquals(senetTuple, root.path("cabinet").add("game", senetTuple.getLong(1)).toTuple(context));
        assertEquals(urTuple, root.path("cabinet").add("game", urTuple.getLong(1)).toTuple(context));
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Example 67 with Tuple

use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.

the class KeySpaceDirectoryTest method listRangeTestCases.

private List<Pair<ValueRange<Object>, List<Tuple>>> listRangeTestCases(List<Tuple> values) {
    values.sort(null);
    // Size >= 1. It is very likely to be 5 (but can be smaller) expect when the key type is NULL or BOOLEAN.
    int size = values.size();
    List<Pair<ValueRange<Object>, List<Tuple>>> testCases = new LinkedList<>();
    testCases.add(Pair.of(null, new ArrayList<>(values)));
    testCases.add(newTestCase(values.get(0), null, EndpointType.RANGE_INCLUSIVE, EndpointType.TREE_END, new ArrayList<>(values)));
    testCases.add(newTestCase(values.get(0), null, EndpointType.RANGE_EXCLUSIVE, EndpointType.TREE_END, new ArrayList<>(values.subList(1, size))));
    testCases.add(newTestCase(null, values.get(size - 1), EndpointType.TREE_START, EndpointType.RANGE_INCLUSIVE, new ArrayList<>(values)));
    testCases.add(newTestCase(null, values.get(size - 1), EndpointType.TREE_START, EndpointType.RANGE_EXCLUSIVE, new ArrayList<>(values.subList(0, size - 1))));
    testCases.add(newTestCase(null, null, EndpointType.TREE_START, EndpointType.TREE_END, new ArrayList<>(values)));
    testCases.add(newTestCase(values.get(0), values.get(0), EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE, new ArrayList<>(values.subList(0, 1))));
    // Only test this for LONG because it might be tricky to modify values for some other types.
    if (KeyType.LONG.isMatch(values.get(0))) {
        Tuple first = values.get(0);
        Tuple justBeforeFirst = Tuple.from((Long) first.get(0) - 1);
        Tuple justAfterFirst = Tuple.from((Long) first.get(0) + 1);
        Tuple last = values.get(size - 1);
        Tuple justBeforeLast = Tuple.from((Long) last.get(0) - 1);
        Tuple justAfterLast = Tuple.from((Long) last.get(0) + 1);
        // Endpoint type does not matters to the values who are not in the collection.
        for (EndpointType endpointType : Arrays.asList(EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_EXCLUSIVE)) {
            testCases.add(newTestCase(justBeforeFirst, null, endpointType, EndpointType.TREE_END, new ArrayList<>(values.subList(0, size))));
            testCases.add(newTestCase(justAfterFirst, null, endpointType, EndpointType.TREE_END, new ArrayList<>(values.subList(1, size))));
            testCases.add(newTestCase(null, justBeforeLast, EndpointType.TREE_START, endpointType, new ArrayList<>(values.subList(0, size - 1))));
            testCases.add(newTestCase(null, justAfterLast, EndpointType.TREE_START, endpointType, new ArrayList<>(values.subList(0, size))));
        }
    }
    if (size >= 2) {
        Tuple first = values.get(0);
        Tuple last = values.get(size - 1);
        testCases.add(newTestCase(first, last, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE, new ArrayList<>(values.subList(0, size))));
        testCases.add(newTestCase(first, last, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_EXCLUSIVE, new ArrayList<>(values.subList(0, size - 1))));
        testCases.add(newTestCase(first, last, EndpointType.RANGE_EXCLUSIVE, EndpointType.RANGE_INCLUSIVE, new ArrayList<>(values.subList(1, size))));
        testCases.add(newTestCase(first, last, EndpointType.RANGE_EXCLUSIVE, EndpointType.RANGE_EXCLUSIVE, new ArrayList<>(values.subList(1, size - 1))));
    }
    if (size >= 4) {
        Tuple second = values.get(1);
        Tuple secondLast = values.get(size - 2);
        testCases.add(newTestCase(second, secondLast, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE, new ArrayList<>(values.subList(1, size - 1))));
        testCases.add(newTestCase(second, secondLast, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_EXCLUSIVE, new ArrayList<>(values.subList(1, size - 2))));
        testCases.add(newTestCase(second, secondLast, EndpointType.RANGE_EXCLUSIVE, EndpointType.RANGE_INCLUSIVE, new ArrayList<>(values.subList(2, size - 1))));
        testCases.add(newTestCase(second, secondLast, EndpointType.RANGE_EXCLUSIVE, EndpointType.RANGE_EXCLUSIVE, new ArrayList<>(values.subList(2, size - 2))));
    }
    return testCases;
}
Also used : ArrayList(java.util.ArrayList) EndpointType(com.apple.foundationdb.record.EndpointType) Tuple(com.apple.foundationdb.tuple.Tuple) LinkedList(java.util.LinkedList) Pair(org.apache.commons.lang3.tuple.Pair)

Example 68 with Tuple

use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.

the class KeySpaceDirectoryTest method testListReverse.

@Test
public void testListReverse() {
    KeySpace root = new KeySpace(new KeySpaceDirectory("root", KeyType.STRING, "root-" + random.nextInt(Integer.MAX_VALUE)).addSubdirectory(new KeySpaceDirectory("a", KeyType.LONG).addSubdirectory(new KeySpaceDirectory("b", KeyType.LONG))));
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    try (FDBRecordContext context = database.openContext()) {
        Transaction tr = context.ensureActive();
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 2; j++) {
                tr.set(root.path("root").add("a", i).add("b", j).toTuple(context).pack(), Tuple.from(i + j).pack());
            }
        }
        tr.commit().join();
    }
    final List<Tuple> results;
    try (FDBRecordContext context = database.openContext()) {
        ScanProperties props = new ScanProperties(ExecuteProperties.newBuilder().build(), true);
        results = root.path("root").listSubdirectoryAsync(context, "a", null, props).asList().join().stream().map(path -> path.toTuple()).collect(Collectors.toList());
    }
    assertEquals(5, results.size());
    for (int i = 0; i < 5; i++) {
        assertEquals(i, ((Long) results.get(4 - i).getLong(1)).intValue());
    }
}
Also used : Transaction(com.apple.foundationdb.Transaction) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScanProperties(com.apple.foundationdb.record.ScanProperties) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Example 69 with Tuple

use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.

the class ResolverCreateHooksTest method testPreWriteChecks.

@Test
void testPreWriteChecks() {
    // reads the key, and chooses the resolver based on the value
    final PreWriteCheck check = (context, providedResolver) -> {
        CompletableFuture<LocatableResolver> expectedResolverFuture = root().add("should-use-A").toTupleAsync(context).thenCompose(keyTuple -> context.ensureActive().get(keyTuple.pack())).thenApply(value -> {
            boolean useA = Tuple.fromBytes(value).getBoolean(0);
            return new ScopedInterningLayer(database, resolverPath(context, useA ? "A" : "B"));
        });
        return expectedResolverFuture.thenApply(expectedResolver -> expectedResolver.equals(providedResolver));
    };
    final ResolverCreateHooks hooks = new ResolverCreateHooks(check, ResolverCreateHooks.DEFAULT_HOOK);
    // use resolver A
    database.run(context -> root().add("should-use-A").toTupleAsync(context).thenAccept(tuple -> context.ensureActive().set(tuple.pack(), Tuple.from(true).pack())));
    try (FDBRecordContext context = database.openContext()) {
        LocatableResolver resolverA = new ScopedInterningLayer(database, resolverPath(context, "A"));
        LocatableResolver resolverB = new ScopedInterningLayer(database, resolverPath(context, "B"));
        assertChecks(context, resolverA, hooks, true);
        assertChecks(context, resolverB, hooks, false);
    }
    // use resolver B
    database.run(context -> root().add("should-use-A").toTupleAsync(context).thenAccept(tuple -> context.ensureActive().set(tuple.pack(), Tuple.from(false).pack())));
    // after migration
    try (FDBRecordContext context = database.openContext()) {
        LocatableResolver resolverA = new ScopedInterningLayer(database, resolverPath(context, "A"));
        LocatableResolver resolverB = new ScopedInterningLayer(database, resolverPath(context, "B"));
        assertChecks(context, resolverA, hooks, false);
        assertChecks(context, resolverB, hooks, true);
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Tags(com.apple.test.Tags) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) ScopedInterningLayer(com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer) Collectors(java.util.stream.Collectors) FDBTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBTestBase) Test(org.junit.jupiter.api.Test) Tuple(com.apple.foundationdb.tuple.Tuple) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PreWriteCheck(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.PreWriteCheck) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) FDBDatabaseFactory(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory) Tag(org.junit.jupiter.api.Tag) CompletableFuture(java.util.concurrent.CompletableFuture) PreWriteCheck(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.PreWriteCheck) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScopedInterningLayer(com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer) Test(org.junit.jupiter.api.Test)

Example 70 with Tuple

use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.

the class FDBLuceneQueryTest method assertTermIndexedOrNot.

private void assertTermIndexedOrNot(String term, boolean indexedExpected, boolean shouldDeferFetch) throws Exception {
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        final QueryComponent filter = new LuceneQueryComponent(term, Lists.newArrayList());
        // Query for full records
        RecordQuery query = RecordQuery.newBuilder().setRecordType(TextIndexTestUtils.SIMPLE_DOC).setFilter(filter).build();
        setDeferFetchAfterUnionAndIntersection(shouldDeferFetch);
        RecordQueryPlan plan = planner.plan(query);
        RecordCursor<FDBQueriedRecord<Message>> fdbQueriedRecordRecordCursor = recordStore.executeQuery(plan);
        RecordCursor<Tuple> map = fdbQueriedRecordRecordCursor.map(FDBQueriedRecord::getPrimaryKey);
        List<Long> primaryKeys = map.map(t -> t.getLong(0)).asList().get();
        if (indexedExpected) {
            assertEquals(ImmutableSet.of(1L), ImmutableSet.copyOf(primaryKeys), "Expected term not indexed");
        } else {
            assertThat("Unexpected term indexed", primaryKeys.isEmpty());
        }
    }
}
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) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) LuceneQueryComponent(com.apple.foundationdb.record.query.expressions.LuceneQueryComponent) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Tuple(com.apple.foundationdb.tuple.Tuple)

Aggregations

Tuple (com.apple.foundationdb.tuple.Tuple)163 Nonnull (javax.annotation.Nonnull)80 List (java.util.List)66 Test (org.junit.jupiter.api.Test)66 TupleRange (com.apple.foundationdb.record.TupleRange)62 ArrayList (java.util.ArrayList)57 Message (com.google.protobuf.Message)56 Nullable (javax.annotation.Nullable)50 IndexEntry (com.apple.foundationdb.record.IndexEntry)48 Index (com.apple.foundationdb.record.metadata.Index)47 Subspace (com.apple.foundationdb.subspace.Subspace)47 CompletableFuture (java.util.concurrent.CompletableFuture)46 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)45 ScanProperties (com.apple.foundationdb.record.ScanProperties)43 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)42 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)41 Collectors (java.util.stream.Collectors)41 Collections (java.util.Collections)40 Transaction (com.apple.foundationdb.Transaction)38 RecordCursor (com.apple.foundationdb.record.RecordCursor)38