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));
}
}
}
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));
}
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()));
}
}
}
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);
}
}
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;
});
}
Aggregations