Search in sources :

Example 26 with FDBDatabase

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

the class KeySpaceDirectoryTest method testPathToAndFromTuple.

@Test
public void testPathToAndFromTuple() throws Exception {
    KeySpace root = new KeySpace(new DirectoryLayerDirectory("production", "production").addSubdirectory(new KeySpaceDirectory("userid", KeyType.LONG).addSubdirectory(new DirectoryLayerDirectory("application").addSubdirectory(new KeySpaceDirectory("dataStore", KeyType.NULL)).addSubdirectory(new DirectoryLayerDirectory("metadataStore", "S")))), new DirectoryLayerDirectory("test", "test").addSubdirectory(new KeySpaceDirectory("userid", KeyType.LONG).addSubdirectory(new DirectoryLayerDirectory("application").addSubdirectory(new KeySpaceDirectory("dataStore", KeyType.NULL)).addSubdirectory(new DirectoryLayerDirectory("metadataStore", "S")))));
    final KeySpacePath path1 = root.path("production").add("userid", 123456789L).add("application", "com.mybiz.application1").add("dataStore");
    final KeySpacePath path2 = root.path("test").add("userid", 987654321L).add("application", "com.mybiz.application2").add("metadataStore");
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    final Tuple path1Tuple;
    final Tuple path2Tuple;
    try (FDBRecordContext context = database.openContext()) {
        path1Tuple = path1.toTuple(context);
        path2Tuple = path2.toTuple(context);
        context.commit();
    }
    final Tuple path1ExpectedTuple;
    final Tuple path2ExpectedTuple;
    try (FDBRecordContext context = database.openContext()) {
        List<Long> entries = resolveBatch(context, "production", "test", "com.mybiz.application1", "com.mybiz.application2", "S");
        path1ExpectedTuple = Tuple.from(entries.get(0), 123456789L, entries.get(2), null);
        path2ExpectedTuple = Tuple.from(entries.get(1), 987654321L, entries.get(3), entries.get(4));
        assertEquals(path1ExpectedTuple, path1Tuple);
        assertEquals(path2ExpectedTuple, path2Tuple);
        // Now, make sure that we can take a tuple and turn it back into a keyspace path.
        List<ResolvedKeySpacePath> revPath1 = root.resolveFromKey(context, path1ExpectedTuple).flatten();
        assertEquals("production", revPath1.get(0).getDirectoryName());
        assertEquals(entries.get(0), revPath1.get(0).getResolvedValue());
        assertEquals("userid", revPath1.get(1).getDirectoryName());
        assertEquals(123456789L, revPath1.get(1).getResolvedValue());
        assertEquals("application", revPath1.get(2).getDirectoryName());
        assertEquals(entries.get(2), revPath1.get(2).getResolvedValue());
        assertEquals("dataStore", revPath1.get(3).getDirectoryName());
        assertEquals(null, revPath1.get(3).getResolvedValue());
        // Tack on extra value to make sure it is in the remainder.
        Tuple extendedPath2 = path2ExpectedTuple.add(10L);
        List<ResolvedKeySpacePath> revPath2 = root.resolveFromKey(context, extendedPath2).flatten();
        assertEquals("test", revPath2.get(0).getDirectoryName());
        assertEquals("userid", revPath2.get(1).getDirectoryName());
        assertEquals("application", revPath2.get(2).getDirectoryName());
        assertEquals("metadataStore", revPath2.get(3).getDirectoryName());
        assertEquals(Tuple.from(10L), revPath2.get(3).getRemainder());
    }
}
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 27 with FDBDatabase

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

the class KeySpaceDirectoryTest method flattenPreservesWrapper.

@Test
public void flattenPreservesWrapper() {
    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();
    List<KeySpacePath> path = keySpace.path("a", "foo").add("b", "bar").flatten();
    assertThat("a should be pathA", path.get(0), instanceOf(PathA.class));
    assertThat("b should be pathB", path.get(1), instanceOf(PathB.class));
}
Also used : FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test)

Example 28 with FDBDatabase

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

the class KeySpaceDirectoryTest method testCustomDirectoryResolver.

@Test
public void testCustomDirectoryResolver() throws Exception {
    KeySpace root = new KeySpace(new KeySpaceDirectory("root", KeyType.LONG, 1L).addSubdirectory(new ConstantResolvingKeySpaceDirectory("toString", KeyType.STRING, KeySpaceDirectory.ANY_VALUE, v -> "val" + v.toString())).addSubdirectory(new ConstantResolvingKeySpaceDirectory("toWrongType", KeyType.LONG, KeySpaceDirectory.ANY_VALUE, v -> "val" + v.toString())));
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    try (FDBRecordContext context = database.openContext()) {
        assertEquals(Tuple.from(1L, "val15"), root.path("root").add("toString", 15).toTuple(context));
        assertThrows(RecordCoreArgumentException.class, () -> root.path("root").add("toWrongType", 21L).toTuple(context));
    }
}
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) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test)

Example 29 with FDBDatabase

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

the class KeySpaceDirectoryTest method testListAnyValue.

@Test
public void testListAnyValue() throws Exception {
    // Create a root directory called "a" with subdirs of every type (no constants for now)
    Long rootValue = random.nextLong();
    KeySpaceDirectory dirA = new KeySpaceDirectory("a", KeyType.LONG, rootValue);
    for (KeyTypeValue kv : valueOfEveryType) {
        dirA.addSubdirectory(new KeySpaceDirectory(kv.keyType.toString(), kv.keyType));
    }
    KeySpace root = new KeySpace(dirA);
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    final Map<KeyType, List<Tuple>> valuesForType = new HashMap<>();
    // Create an entry in the keyspace with a row for every type that we support
    try (FDBRecordContext context = database.openContext()) {
        Transaction tr = context.ensureActive();
        for (KeyTypeValue kv : valueOfEveryType) {
            List<Tuple> values = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                Object value = kv.generator.get();
                Tuple tupleValue = Tuple.from(value);
                if (!values.contains(tupleValue)) {
                    values.add(tupleValue);
                    // final results.
                    for (int j = 0; j < 5; j++) {
                        tr.set(Tuple.from(rootValue, value, j).pack(), Tuple.from(i).pack());
                    }
                }
            }
            valuesForType.put(kv.keyType, values);
        }
        context.commit();
    }
    try (FDBRecordContext context = database.openContext()) {
        for (KeyTypeValue kv : valueOfEveryType) {
            if (kv.keyType != KeyType.NULL) {
                List<Tuple> values = valuesForType.get(kv.keyType);
                for (Pair<ValueRange<Object>, List<Tuple>> testCase : listRangeTestCases(values)) {
                    testListRange(testCase.getLeft(), testCase.getRight(), context, root, kv.keyType);
                }
            }
        }
    }
}
Also used : KeyType(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) ValueRange(com.apple.foundationdb.record.ValueRange) Transaction(com.apple.foundationdb.Transaction) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Example 30 with FDBDatabase

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

the class KeySpaceDirectoryTest method testListObeysReturnedRowAndScanLimits.

@Test
public void testListObeysReturnedRowAndScanLimits() {
    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();
    }
    doLimitedScan(database, root, 5, Integer.MAX_VALUE, RecordCursor.NoNextReason.RETURN_LIMIT_REACHED);
    doLimitedScan(database, root, Integer.MAX_VALUE, 5, RecordCursor.NoNextReason.SCAN_LIMIT_REACHED);
}
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)

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