use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testListConstantValue.
@Test
public void testListConstantValue() throws Exception {
// Create a root directory called "a" with subdirs of every type and a constant value
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, kv.generator.get()));
}
KeySpace root = new KeySpace(dirA);
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
Transaction tr = context.ensureActive();
for (KeyTypeValue kv : valueOfEveryType) {
KeySpaceDirectory dir = root.getDirectory("a").getSubdirectory(kv.keyType.name());
for (int i = 0; i < 5; i++) {
tr.set(Tuple.from(rootValue, dir.getValue(), i).pack(), Tuple.from(i).pack());
}
}
context.commit();
}
try (FDBRecordContext context = database.openContext()) {
for (KeyTypeValue kv : valueOfEveryType) {
KeySpaceDirectory dir = root.getDirectory("a").getSubdirectory(kv.keyType.name());
List<ResolvedKeySpacePath> paths = root.path("a").listSubdirectory(context, kv.keyType.toString());
assertEquals(1, paths.size());
if (dir.getKeyType() == KeyType.BYTES) {
assertTrue(Arrays.equals((byte[]) dir.getValue(), paths.get(0).toTuple().getBytes(1)));
} else {
assertEquals(dir.getValue(), paths.get(0).toTuple().get(1));
}
}
}
}
use of com.apple.foundationdb.Transaction 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());
}
}
use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class FDBDatabaseTest method testCommitLatencyInjection.
@Test
public void testCommitLatencyInjection() throws Exception {
testLatencyInjection(FDBLatencySource.COMMIT_ASYNC, 300L, context -> {
final Transaction tr = context.ensureActive();
tr.clear(new byte[] { (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef });
context.commit();
});
}
use of com.apple.foundationdb.Transaction 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.Transaction in project fdb-record-layer by FoundationDB.
the class FDBReverseDirectoryCacheTest method testPutIfNotExistsWrongValue.
@Test
public void testPutIfNotExistsWrongValue() throws Exception {
final Random random = new Random();
final String name = "dir_" + Math.abs(random.nextInt());
final Long id;
ScopedValue<String> scopedName = globalScope.wrap(name);
try (FDBRecordContext context = openContext()) {
Transaction tr = context.ensureActive();
// need to use the FDB DirectoryLayer to bypass LocatableResolver which populates the reverse directory cache automatically
id = Tuple.fromBytes(DirectoryLayer.getDefault().createOrOpen(tr, PathUtil.from(name)).get().pack()).getLong(0);
commit(context);
}
FDBReverseDirectoryCache rdc = fdb.getReverseDirectoryCache();
// Store the correct value
try (FDBRecordContext context = openContext()) {
rdc.putIfNotExists(context, scopedName, id).get();
// The put should be considered a hard miss because it had to write to the cache
assertEquals(0L, rdc.getPersistentCacheHitCount());
assertEquals(1L, rdc.getPersistentCacheMissCount());
assertEquals(0L, context.getTimer().getCount(FDBStoreTimer.Counts.REVERSE_DIR_PERSISTENT_CACHE_HIT_COUNT));
assertEquals(1L, context.getTimer().getCount(FDBStoreTimer.Counts.REVERSE_DIR_PERSISTENT_CACHE_MISS_COUNT));
commit(context);
}
// Try again with a different value
try (FDBRecordContext context = openContext()) {
rdc.putIfNotExists(context, globalScope.wrap(name + "_x"), id).get();
commit(context);
fail("Should have thrown an exception due to wrong value");
} catch (ExecutionException e) {
// This will throw if we didn't get the exception we wanted
RecordCoreException yay = (RecordCoreException) e.getCause();
}
}
Aggregations