Search in sources :

Example 36 with Transaction

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);
    }
}
Also used : Random(java.util.Random) Transaction(com.apple.foundationdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 37 with Transaction

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));
}
Also used : Random(java.util.Random) Transaction(com.apple.foundationdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 38 with Transaction

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();
    }
}
Also used : Transaction(com.apple.foundationdb.Transaction)

Example 39 with Transaction

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();
}
Also used : Transaction(com.apple.foundationdb.Transaction)

Example 40 with Transaction

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();
}
Also used : Transaction(com.apple.foundationdb.Transaction)

Aggregations

Transaction (com.apple.foundationdb.Transaction)84 ReadTransaction (com.apple.foundationdb.ReadTransaction)34 Tuple (com.apple.foundationdb.tuple.Tuple)34 Test (org.junit.jupiter.api.Test)33 Nonnull (javax.annotation.Nonnull)28 ArrayList (java.util.ArrayList)26 List (java.util.List)26 CompletableFuture (java.util.concurrent.CompletableFuture)26 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)22 Subspace (com.apple.foundationdb.subspace.Subspace)21 Collectors (java.util.stream.Collectors)19 Nullable (javax.annotation.Nullable)19 KeyValue (com.apple.foundationdb.KeyValue)18 Map (java.util.Map)18 KeyValueLogMessage (com.apple.foundationdb.record.logging.KeyValueLogMessage)17 Range (com.apple.foundationdb.Range)16 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 Collections (java.util.Collections)15 RecordCursor (com.apple.foundationdb.record.RecordCursor)14