Search in sources :

Example 1 with DirectoryLayer

use of com.apple.foundationdb.directory.DirectoryLayer in project fdb-record-layer by FoundationDB.

the class ScopedDirectoryLayerTest method testDefaultResolverSeesPreviousDefaultDirectoryLayerEntries.

@Test
public void testDefaultResolverSeesPreviousDefaultDirectoryLayerEntries() {
    final DirectoryLayer directoryLayer = DirectoryLayer.getDefault();
    final List<String> names = IntStream.range(0, 5).mapToObj(number -> String.format("name-%d", number)).collect(Collectors.toList());
    Map<String, Long> values = new HashMap<>();
    try (FDBRecordContext context = database.openContext()) {
        for (String name : names) {
            values.put(name, directoryLayer.createOrOpen(context.ensureActive(), ImmutableList.of(name)).thenApply(subspace -> Tuple.fromBytes(subspace.getKey()).getLong(0)).join());
        }
        context.commit();
    }
    try (FDBRecordContext context = database.openContext()) {
        for (String name : names) {
            Long resolvedValue = globalScope.resolve(context.getTimer(), name).join();
            assertThat("resolver sees all mappings in directory layer", values.get(name), is(resolvedValue));
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MetadataHook(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook) Tags(com.apple.test.Tags) Matchers.not(org.hamcrest.Matchers.not) DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) HashMap(java.util.HashMap) CompletionException(java.util.concurrent.CompletionException) Disabled(org.junit.jupiter.api.Disabled) Collectors(java.util.stream.Collectors) Bytes(com.google.common.primitives.Bytes) Subspace(com.apple.foundationdb.subspace.Subspace) Test(org.junit.jupiter.api.Test) KeyType(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType) Tuple(com.apple.foundationdb.tuple.Tuple) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Is.is(org.hamcrest.core.Is.is) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) HashMap(java.util.HashMap) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Test(org.junit.jupiter.api.Test)

Example 2 with DirectoryLayer

use of com.apple.foundationdb.directory.DirectoryLayer in project fdb-record-layer by FoundationDB.

the class ExtendedDirectoryLayerTest method testCanAllocateSameKeysInParallelWithFDBDirectoryLayer.

@Test
public void testCanAllocateSameKeysInParallelWithFDBDirectoryLayer() {
    final DirectoryLayer directoryLayer = DirectoryLayer.getDefault();
    testParallelAllocation(true, database, key -> database.runAsync(context -> directoryLayer.createOrOpen(context.ensureActive(), Collections.singletonList(key)).thenApply(subspace -> Tuple.fromBytes(subspace.pack()).getLong(0))), key -> globalScope.resolve(key), ExtendedDirectoryLayer.global(database), ScopedDirectoryLayer.global(database));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MetadataHook(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Tuple(com.apple.foundationdb.tuple.Tuple) STRING(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType.STRING) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) BiMap(com.google.common.collect.BiMap) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) Test(org.junit.jupiter.api.Test)

Example 3 with DirectoryLayer

use of com.apple.foundationdb.directory.DirectoryLayer in project fdb-record-layer by FoundationDB.

the class ExtendedDirectoryLayerTest method testDefaultCanAllocateIndependentKeysInParallel.

@Test
public void testDefaultCanAllocateIndependentKeysInParallel() {
    final ScopedDirectoryLayer directoryLayer = ScopedDirectoryLayer.global(database);
    Map<String, Long> mappingsFromOld = new ConcurrentHashMap<>();
    Map<String, Long> mappingsFromNew = new ConcurrentHashMap<>();
    List<CompletableFuture<?>> operations = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        String oldDirLayerKey = i + "-old-dl-key";
        String newDirLayerKey = i + "-new-dl-key";
        operations.add(directoryLayer.resolve(oldDirLayerKey).thenApply(value -> mappingsFromOld.put(oldDirLayerKey, value)));
        operations.add(globalScope.resolve(newDirLayerKey).thenApply(value -> mappingsFromNew.put(newDirLayerKey, value)));
    }
    CompletableFuture.allOf(operations.toArray(new CompletableFuture<?>[0])).join();
    // Implicitly checks that there are no duplicate keys or values in the two maps
    BiMap<String, Long> completeMapping = ImmutableBiMap.<String, Long>builder().putAll(mappingsFromOld).putAll(mappingsFromNew).build();
    for (Map.Entry<String, Long> entry : completeMapping.entrySet()) {
        try (FDBRecordContext context = database.openContext()) {
            assertThat("the FDB directory layer sees the mapping", directoryLayer.mustResolve(context, entry.getKey()).join(), is(entry.getValue()));
            assertThat("the ScopedDirectoryLayer sees the mapping", globalScope.mustResolve(context, entry.getKey()).join(), is(entry.getValue()));
        }
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MetadataHook(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Tuple(com.apple.foundationdb.tuple.Tuple) STRING(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType.STRING) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) BiMap(com.google.common.collect.BiMap) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.jupiter.api.Test)

Example 4 with DirectoryLayer

use of com.apple.foundationdb.directory.DirectoryLayer in project fdb-record-layer by FoundationDB.

the class FDBReverseDirectoryCacheTest method testPutIfNotExistsNotVisibleUntilCommit.

@Test
public void testPutIfNotExistsNotVisibleUntilCommit() throws Exception {
    final ScopedDirectoryLayer scope = globalScope;
    final String name = "dir_" + Math.abs(new Random().nextInt());
    final FDBReverseDirectoryCache rdc = fdb.getReverseDirectoryCache();
    final ScopedValue<String> scopedName = scope.wrap(name);
    // Create a new directory layer entry, put it in the cache in the same transaction
    try (FDBRecordContext context = openContext()) {
        Transaction transaction = context.ensureActive();
        DirectoryLayer directoryLayerToUse = new DirectoryLayer(context.join(scope.getNodeSubspace(context)), scope.getContentSubspace());
        final byte[] rawDirectoryEntry = directoryLayerToUse.createOrOpen(transaction, Collections.singletonList(name)).get().getKey();
        final Long id = Tuple.fromBytes(rawDirectoryEntry).getLong(0);
        rdc.putIfNotExists(context, scopedName, id).get();
        // This happens in its own transaction so should not yet see the uncommitted directory and reverse
        // directory entries that are being created.
        Optional<String> result = rdc.get(scope.wrap(id)).join();
        assertFalse(result.isPresent(), "Should not have gotten a result from RDC lookup");
        commit(context);
    }
}
Also used : DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) ScopedDirectoryLayer(com.apple.foundationdb.record.provider.foundationdb.keyspace.ScopedDirectoryLayer) ScopedDirectoryLayer(com.apple.foundationdb.record.provider.foundationdb.keyspace.ScopedDirectoryLayer) Random(java.util.Random) Transaction(com.apple.foundationdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 5 with DirectoryLayer

use of com.apple.foundationdb.directory.DirectoryLayer in project bsv-components-library by bitcoin-sv.

the class BlockStoreFDB method initDirectoryStructure.

/* It creates the Directory Layer structure */
protected void initDirectoryStructure() {
    dirLayer = new DirectoryLayer();
    db.run(tr -> {
        blockchainDir = dirLayer.createOrOpen(tr, Arrays.asList(DIR_BLOCKCHAIN)).join();
        netDir = blockchainDir.createOrOpen(tr, Arrays.asList(config.getNetworkId())).join();
        blocksDir = netDir.createOrOpen(tr, Arrays.asList(DIR_BLOCKS)).join();
        blocksMetadataDir = blocksDir.createOrOpen(tr, Arrays.asList(DIR_METADATA)).join();
        txsDir = netDir.createOrOpen(tr, Arrays.asList(DIR_TXS)).join();
        // We print out the DB Structure and general info about the configuration:
        log.info("JCL-Store Configuration:");
        log.info(" - FoundationDB Implementation");
        log.info(" - Network : " + config.getNetworkId());
        return null;
    });
}
Also used : DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer)

Aggregations

DirectoryLayer (com.apple.foundationdb.directory.DirectoryLayer)8 Test (org.junit.jupiter.api.Test)7 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)6 MetadataHook (com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook)4 Tuple (com.apple.foundationdb.tuple.Tuple)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 Matchers.nullValue (org.hamcrest.Matchers.nullValue)4 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)4 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)3 FDBDatabase (com.apple.foundationdb.record.provider.foundationdb.FDBDatabase)3 FDBStoreTimer (com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer)3 STRING (com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType.STRING)3 BiMap (com.google.common.collect.BiMap)3 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)3 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 CompletableFuture (java.util.concurrent.CompletableFuture)3