Search in sources :

Example 36 with FDBDatabase

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

the class KeySpaceDirectoryTest method testListPreservesWrapper.

@Test
public void testListPreservesWrapper() {
    KeySpace keySpace = new KeySpace(new KeySpaceDirectory("a", KeyType.STRING, PathA::new).addSubdirectory(new KeySpaceDirectory("b", KeyType.STRING, PathB::new)));
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    try (FDBRecordContext context = database.openContext()) {
        Transaction tr = context.ensureActive();
        PathA root = (PathA) keySpace.path("a", "foo");
        tr.set(root.add("b", "one").toTuple(context).pack(), TupleHelpers.EMPTY.pack());
        tr.set(root.add("b", "two").toTuple(context).pack(), TupleHelpers.EMPTY.pack());
        tr.set(root.add("b", "three").toTuple(context).pack(), TupleHelpers.EMPTY.pack());
        tr.commit();
    }
    try (FDBRecordContext context = database.openContext()) {
        List<ResolvedKeySpacePath> paths = keySpace.path("a", "foo").listSubdirectory(context, "b");
        for (ResolvedKeySpacePath path : paths) {
            assertThat("Path should be PathB", path, instanceOf(PathB.class));
            assertThat("parent should be PathA", path.getParent(), instanceOf(PathA.class));
        }
    }
}
Also used : Transaction(com.apple.foundationdb.Transaction) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test)

Example 37 with FDBDatabase

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

the class KeySpaceDirectoryTest method testInvalidPath.

@Test
public void testInvalidPath() throws Exception {
    KeySpace root = new KeySpace(new KeySpaceDirectory("root", KeyType.STRING, "production").addSubdirectory(new KeySpaceDirectory("a", KeyType.STRING).addSubdirectory(new KeySpaceDirectory("b", KeyType.STRING))));
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    try (FDBRecordContext context = database.openContext()) {
        // Building a tuple in the correct order works
        Tuple tuple = root.path("root").add("a", "foo").add("b", "bar").toTuple(context);
        assertEquals(Tuple.from("production", "foo", "bar"), tuple);
        // Walking in the wrong order fails
        assertThrows(NoSuchDirectoryException.class, () -> root.path("foo").add("a", "bar").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 38 with FDBDatabase

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

the class KeySpaceDirectoryTest method testDirectoryLayerDirectoryValidation.

@Test
public void testDirectoryLayerDirectoryValidation() throws Exception {
    KeySpace root = new KeySpace(new DirectoryLayerDirectory("school", "school").addSubdirectory(new DirectoryLayerDirectory("school_name").addSubdirectory(new DirectoryLayerDirectory("teachers", "teachers")).addSubdirectory(new DirectoryLayerDirectory("students", "students"))));
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    final Tuple teachersTuple;
    final Tuple studentsTuple;
    try (FDBRecordContext context = database.openContext()) {
        teachersTuple = root.path("school").add("school_name", "Football Tech").add("teachers").toTuple(context);
        studentsTuple = root.path("school").add("school_name", "Football Tech").add("students").toTuple(context);
        context.commit();
    }
    try (FDBRecordContext context = database.openContext()) {
        // Use the wrong directory layer value for the students and teachers
        assertThrows(RecordCoreArgumentException.class, () -> root.path("school").add("school_name", "Football Tech").add("teachers", studentsTuple.getLong(1)).toTuple(context));
        assertThrows(RecordCoreArgumentException.class, () -> root.path("school").add("school_name", "Football Tech").add("students", teachersTuple.getLong(1)).toTuple(context));
        // Use a value that does not exist in the directory layer as the school name.
        assertThrows(NoSuchElementException.class, () -> root.path("school").add("school_name", -746464638L).add("teachers").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 39 with FDBDatabase

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

the class KeySpaceDirectoryTest method testListAcrossTransactions.

@Test
public void testListAcrossTransactions() throws Exception {
    KeySpace root = new KeySpace(new KeySpaceDirectory("a", KeyType.LONG, random.nextLong()).addSubdirectory(new KeySpaceDirectory("b", KeyType.STRING)));
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    final List<String> directoryEntries = IntStream.range(0, 10).boxed().map(i -> "val_" + i).collect(Collectors.toList());
    final KeySpacePath rootPath = root.path("a");
    try (final FDBRecordContext context = database.openContext()) {
        final Transaction tr = context.ensureActive();
        directoryEntries.forEach(name -> tr.set(rootPath.add("b", name).toTuple(context).pack(), TupleHelpers.EMPTY.pack()));
        context.commit();
    }
    byte[] continuation = null;
    int idx = 0;
    do {
        try (final FDBRecordContext context = database.openContext()) {
            final RecordCursor<ResolvedKeySpacePath> cursor = rootPath.listSubdirectoryAsync(context, "b", continuation, new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(2).build()));
            List<ResolvedKeySpacePath> subdirs = context.asyncToSync(FDBStoreTimer.Waits.WAIT_KEYSPACE_LIST, cursor.asList());
            if (!subdirs.isEmpty()) {
                assertEquals(2, subdirs.size(), "Wrong number of path entries returned");
                assertEquals("val_" + idx, subdirs.get(0).getResolvedValue());
                assertEquals("val_" + (idx + 1), subdirs.get(1).getResolvedValue());
                idx += 2;
                continuation = cursor.getNext().getContinuation().toBytes();
                System.out.println(continuation == null ? "null" : Tuple.fromBytes(continuation));
            } else {
                continuation = cursor.getNext().getContinuation().toBytes();
                assertNull(continuation);
            }
        }
    } while (continuation != null);
    assertEquals(directoryEntries.size(), idx);
}
Also used : Arrays(java.util.Arrays) IsInstanceOf.instanceOf(org.hamcrest.core.IsInstanceOf.instanceOf) BiFunction(java.util.function.BiFunction) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Random(java.util.Random) Transaction(com.apple.foundationdb.Transaction) Tuple(com.apple.foundationdb.tuple.Tuple) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) Pair(org.apache.commons.lang3.tuple.Pair) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Tag(org.junit.jupiter.api.Tag) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) UUID(java.util.UUID) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) Collectors(java.util.stream.Collectors) Matchers.startsWith(org.hamcrest.Matchers.startsWith) Test(org.junit.jupiter.api.Test) List(java.util.List) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) Matchers.is(org.hamcrest.Matchers.is) DEFAULT_CHECK(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.DEFAULT_CHECK) IntStream(java.util.stream.IntStream) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Function(java.util.function.Function) Supplier(java.util.function.Supplier) FDBTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBTestBase) ArrayList(java.util.ArrayList) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) KeyType(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType) TestHelpers.eventually(com.apple.foundationdb.record.TestHelpers.eventually) FDBRecordStore(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStore) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) EndpointType(com.apple.foundationdb.record.EndpointType) ScanProperties(com.apple.foundationdb.record.ScanProperties) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Iterator(java.util.Iterator) Tags(com.apple.test.Tags) ScopedInterningLayer(com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer) TimeUnit(java.util.concurrent.TimeUnit) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) TestHelpers.assertThrows(com.apple.foundationdb.record.TestHelpers.assertThrows) FDBDatabaseFactory(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory) RecordCursor(com.apple.foundationdb.record.RecordCursor) ValueRange(com.apple.foundationdb.record.ValueRange) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Transaction(com.apple.foundationdb.Transaction) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScanProperties(com.apple.foundationdb.record.ScanProperties) Test(org.junit.jupiter.api.Test)

Example 40 with FDBDatabase

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

the class KeySpaceDirectoryTest method testAllTypesAnyValues.

@Test
public void testAllTypesAnyValues() throws Exception {
    KeySpaceDirectory rootDir = new KeySpaceDirectory("root", KeyType.LONG, 1L);
    for (KeyTypeValue kv : valueOfEveryType) {
        rootDir.addSubdirectory(new KeySpaceDirectory(kv.keyType.toString(), kv.keyType));
    }
    KeySpace root = new KeySpace(rootDir);
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    try (FDBRecordContext context = database.openContext()) {
        for (KeyTypeValue kv : valueOfEveryType) {
            // Test that we can set a good type and get the right tuple back
            final Object value = kv.generator.get();
            assertEquals(Tuple.from(1L, value), root.path("root").add(kv.keyType.toString(), value).toTuple(context));
            final Object badValue = pickDifferentType(kv.keyType).generator.get();
            assertThrows(RecordCoreArgumentException.class, () -> root.path("root").add(kv.keyType.toString(), badValue).toTuple(context));
        }
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test)

Aggregations

FDBDatabase (com.apple.foundationdb.record.provider.foundationdb.FDBDatabase)42 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)39 Test (org.junit.jupiter.api.Test)38 Tuple (com.apple.foundationdb.tuple.Tuple)19 FDBStoreTimer (com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer)14 Transaction (com.apple.foundationdb.Transaction)12 FDBDatabaseFactory (com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory)11 ScanProperties (com.apple.foundationdb.record.ScanProperties)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 List (java.util.List)8 CompletableFuture (java.util.concurrent.CompletableFuture)8 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)8 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)7 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)7 RecordCoreArgumentException (com.apple.foundationdb.record.RecordCoreArgumentException)7 RecordCursor (com.apple.foundationdb.record.RecordCursor)7 FDBTestBase (com.apple.foundationdb.record.provider.foundationdb.FDBTestBase)7 Tags (com.apple.test.Tags)7