Search in sources :

Example 11 with FDBDatabase

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

the class KeySpaceDirectoryTest method testPathToString.

@Test
public void testPathToString() throws Exception {
    KeySpace root = new KeySpace("foo", new KeySpaceDirectory("root", KeyType.LONG).addSubdirectory(new KeySpaceDirectory("dir1", KeyType.STRING).addSubdirectory(new KeySpaceDirectory("dir2", KeyType.BYTES))).addSubdirectory(new KeySpaceDirectory("dir3", KeyType.LONG).addSubdirectory(new DirectoryLayerDirectory("dir4"))));
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    final Long fooValue;
    final Long barValue;
    try (FDBRecordContext context = database.openContext()) {
        List<Long> entries = resolveBatch(context, "_foo", "_bar");
        context.commit();
        fooValue = entries.get(0);
        barValue = entries.get(1);
    }
    assertEquals("/foo/root/dir3/dir4", root.getDirectory("root").getSubdirectory("dir3").getSubdirectory("dir4").toPathString());
    try (FDBRecordContext context = database.openContext()) {
        assertEquals("/root:4/dir1:\"hi\"/dir2:0x4142+(\"blah\")", root.resolveFromKey(context, Tuple.from(4L, "hi", new byte[] { 0x41, 0x42 }, "blah")).toString());
        assertEquals("/root:11", root.resolveFromKey(context, Tuple.from(11L)).toString());
        assertEquals("/root:14/dir3:4/dir4:\"_bar\"[" + barValue + "]", root.resolveFromKey(context, Tuple.from(14L, 4L, barValue)).toString());
        assertEquals("/root:11/dir3:17/dir4:" + fooValue, root.path("root", 11L).add("dir3", 17L).add("dir4", fooValue).toString());
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test)

Example 12 with FDBDatabase

use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase 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 13 with FDBDatabase

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

the class KeySpaceDirectoryTest method testSeesMetadataUpdates.

@Test
public void testSeesMetadataUpdates() {
    String testRoot = "test-root-" + random.nextInt();
    Function<FDBRecordContext, CompletableFuture<LocatableResolver>> generator = getGenerator();
    KeySpace root = rootForMetadataTests(testRoot, generator);
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    database.setResolverStateRefreshTimeMillis(100);
    String dir = "test-string-" + random.nextInt();
    try (FDBRecordContext context = database.openContext()) {
        KeySpacePath path1 = root.path(testRoot).add("dir_with_metadata_name", dir);
        DirWithMetadataWrapper wrapped = (DirWithMetadataWrapper) path1;
        assertThat("there's no metadata", wrapped.metadata(context).join(), is(nullValue()));
    }
    try (FDBRecordContext context = database.openContext()) {
        generator.apply(context).thenCompose(scope -> scope.updateMetadataAndVersion(dir, Tuple.from("new-metadata").pack())).join();
    }
    eventually("we see the new metadata for the path", () -> {
        try (FDBRecordContext context = database.openContext()) {
            KeySpacePath path1 = root.path(testRoot).add("dir_with_metadata_name", dir);
            return ((DirWithMetadataWrapper) path1).metadata(context).join();
        }
    }, is(Tuple.from("new-metadata").pack()), 120, 10);
}
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) CompletableFuture(java.util.concurrent.CompletableFuture) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test)

Example 14 with FDBDatabase

use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project lionrock by panghy.

the class RemoteFDBDatabaseFactory method getDatabase.

@Override
@Nonnull
public synchronized FDBDatabase getDatabase(@Nullable String clusterFile) {
    FDBDatabase database = databases.get(clusterFile);
    if (database == null) {
        database = new FDBDatabase(this, clusterFile);
        database.setDirectoryCacheSize(getDirectoryCacheSize());
        database.setTrackLastSeenVersion(getTrackLastSeenVersion());
        database.setResolverStateRefreshTimeMillis(getStateRefreshTimeMillis());
        database.setDatacenterId(getDatacenterId());
        database.setStoreStateCache(storeStateCacheFactory.getCache(database));
        databases.put(clusterFile, database);
    }
    return database;
}
Also used : FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Nonnull(javax.annotation.Nonnull)

Example 15 with FDBDatabase

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

the class ChainedCursorTest method testObeysTimeLimit.

@Test
public void testObeysTimeLimit() {
    FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    try (FDBRecordContext context = database.openContext()) {
        ScanProperties props = new ScanProperties(ExecuteProperties.newBuilder().setTimeLimit(4L).setFailOnScanLimitReached(false).build());
        RecordCursorIterator<Long> cursor = new ChainedCursor<>(context, (lastKey) -> nextKey(lastKey).thenApply(value -> {
            sleep(1L);
            return value;
        }), (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(cursor.getNoNextReason(), RecordCursor.NoNextReason.TIME_LIMIT_REACHED);
        assertTrue(count < 5, "Too many values returned");
    }
}
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) 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