use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class FDBReverseDirectoryCacheTest method testPutIfNotExists.
@Test
public void testPutIfNotExists() throws Exception {
final Random random = new Random();
final String name = "dir_" + Math.abs(random.nextInt());
final Long id;
ScopedValue<String> scopedName = globalScope.wrap(name);
FDBReverseDirectoryCache rdc = fdb.getReverseDirectoryCache();
try (FDBRecordContext context = openContext()) {
Transaction tr = context.ensureActive();
id = Tuple.fromBytes(DirectoryLayer.getDefault().createOrOpen(tr, PathUtil.from(name)).get().pack()).getLong(0);
commit(context);
}
// Initial store should be considered a hard miss
try (FDBRecordContext context = openContext()) {
rdc.putIfNotExists(context, scopedName, id).get();
// A few more calls to make sure that we only put it in once
rdc.putIfNotExists(context, scopedName, id).get();
rdc.putIfNotExists(context, scopedName, id).get();
rdc.putIfNotExists(context, scopedName, id).get();
assertEquals(3L, rdc.getPersistentCacheHitCount());
assertEquals(1L, rdc.getPersistentCacheMissCount());
assertEquals(3L, 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);
}
// Second attempt should miss the in-memory cache but hit the persistent cache
rdc.clearStats();
try (FDBRecordContext context = openContext()) {
rdc.putIfNotExists(context, scopedName, id).get();
assertEquals(1L, rdc.getPersistentCacheHitCount());
assertEquals(0L, rdc.getPersistentCacheMissCount());
assertEquals(1L, context.getTimer().getCount(FDBStoreTimer.Counts.REVERSE_DIR_PERSISTENT_CACHE_HIT_COUNT));
assertEquals(0L, context.getTimer().getCount(FDBStoreTimer.Counts.REVERSE_DIR_PERSISTENT_CACHE_MISS_COUNT));
commit(context);
}
}
use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class FDBReverseDirectoryCacheTest method testGetInReverseCacheSubspace.
@Test
public void testGetInReverseCacheSubspace() {
final Random random = new Random();
final String name = "dir_" + Math.abs(random.nextInt());
final Long id;
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, Collections.singletonList(name)).join().getKey()).getLong(0);
commit(context);
}
ScopedValue<Long> scopedId = globalScope.wrap(id);
FDBStoreTimer timer = new FDBStoreTimer();
assertThat("the lookup does not return a value", fdb.getReverseDirectoryCache().getInReverseDirectoryCacheSubspace(timer, scopedId).join(), is(Optional.empty()));
assertEquals(fdb.getReverseDirectoryCache().getPersistentCacheMissCount(), 1);
assertEquals(fdb.getReverseDirectoryCache().getPersistentCacheHitCount(), 0);
assertThat("it does not scan the directory layer", timer.getCount(FDBStoreTimer.DetailEvents.RD_CACHE_DIRECTORY_SCAN), is(0));
// assert that the last lookup did not populate the cache
assertThat("the lookup still does not return a value", fdb.getReverseDirectoryCache().getInReverseDirectoryCacheSubspace(timer, scopedId).join(), is(Optional.empty()));
assertEquals(fdb.getReverseDirectoryCache().getPersistentCacheMissCount(), 2);
assertEquals(fdb.getReverseDirectoryCache().getPersistentCacheHitCount(), 0);
assertThat("it does not scan the directory layer", timer.getCount(FDBStoreTimer.DetailEvents.RD_CACHE_DIRECTORY_SCAN), is(0));
}
use of com.apple.foundationdb.Transaction in project lionrock by panghy.
the class RemoteDatabase method run.
public <T> T run(String name, Function<? super Transaction, T> retryable, Executor e) {
Transaction t = this.createTransaction(name, e);
try {
while (true) {
try {
T returnVal = retryable.apply(t);
t.commit().join();
return returnVal;
} catch (RuntimeException err) {
t = t.onError(err).join();
}
}
} finally {
t.close();
}
}
use of com.apple.foundationdb.Transaction in project lionrock by panghy.
the class AbstractFoundationDBClientTests method setKeyAndCommit.
long setKeyAndCommit(Database db, byte[] key, byte[] value) {
Transaction tx = db.createTransaction();
tx.set(key, value);
tx.commit().join();
return tx.getCommittedVersion();
}
use of com.apple.foundationdb.Transaction in project lionrock by panghy.
the class AbstractFoundationDBClientTests method clearRangeAndCommit.
long clearRangeAndCommit(Database db, byte[] start, byte[] end) {
Transaction tx = db.createTransaction();
tx.clear(start, end);
tx.commit().join();
return tx.getCommittedVersion();
}
Aggregations