Search in sources :

Example 1 with RemovalCause

use of com.google.common.cache.RemovalCause in project jackrabbit-oak by apache.

the class CacheTest method evictionCallbackRandomized.

@Test
public void evictionCallbackRandomized() throws ExecutionException {
    final HashMap<Integer, Integer> evictedMap = new HashMap<Integer, Integer>();
    final HashSet<Integer> evictedNonResidentSet = new HashSet<Integer>();
    CacheLIRS<Integer, Integer> cache = CacheLIRS.<Integer, Integer>newBuilder().maximumSize(10).evictionCallback(new EvictionCallback<Integer, Integer>() {

        @Override
        public void evicted(Integer key, Integer value, RemovalCause cause) {
            if (value == null) {
                assertTrue(evictedNonResidentSet.add(key));
            } else {
                assertEquals(null, evictedMap.put(key, value));
            }
        }
    }).build();
    Random r = new Random(1);
    for (int k = 0; k < 10000; k++) {
        if (r.nextInt(20) == 0) {
            evictedMap.clear();
            evictedNonResidentSet.clear();
            long size = cache.size();
            long sizeNonResident = cache.sizeNonResident();
            cache.invalidateAll();
            assertEquals(evictedMap.size(), size);
            assertEquals(evictedNonResidentSet.size(), sizeNonResident);
        }
        evictedMap.clear();
        evictedNonResidentSet.clear();
        int key = r.nextInt(20);
        if (r.nextBoolean()) {
            cache.put(key, k);
        } else {
            cache.get(key);
        }
        for (Entry<Integer, Integer> ev : evictedMap.entrySet()) {
            int ek = ev.getKey();
            if (ek == key) {
            // the same key was inserted just now
            } else {
                assertFalse(cache.containsKey(ek));
            }
        }
        for (Entry<Integer, Integer> ev : evictedMap.entrySet()) {
            int ek = ev.getKey();
            Integer v = ev.getValue();
            // an old value
            assertTrue(v < k);
            if (ek == key) {
            // the same key was inserted just now
            } else {
                assertFalse(cache.containsKey(ek));
            }
        }
    }
}
Also used : Random(java.util.Random) HashMap(java.util.HashMap) EvictionCallback(org.apache.jackrabbit.oak.cache.CacheLIRS.EvictionCallback) RemovalCause(com.google.common.cache.RemovalCause) HashSet(java.util.HashSet) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Test(org.junit.Test)

Example 2 with RemovalCause

use of com.google.common.cache.RemovalCause in project jackrabbit-oak by apache.

the class AsyncQueueTest method setup.

@Before
public void setup() throws IOException {
    FileUtils.deleteDirectory(new File("target/cacheTest"));
    pCache = new PersistentCache("target/cacheTest");
    final AtomicReference<NodeCache<PathRev, StringValue>> nodeCacheRef = new AtomicReference<NodeCache<PathRev, StringValue>>();
    CacheLIRS<PathRev, StringValue> cache = new CacheLIRS.Builder<PathRev, StringValue>().maximumSize(1).evictionCallback(new CacheLIRS.EvictionCallback<PathRev, StringValue>() {

        @Override
        public void evicted(@Nonnull PathRev key, @Nullable StringValue value, @Nonnull RemovalCause cause) {
            if (nodeCacheRef.get() != null) {
                nodeCacheRef.get().evicted(key, value, cause);
            }
        }
    }).build();
    nodeCache = (NodeCache<PathRev, StringValue>) pCache.wrap(builderProvider.newBuilder().getNodeStore(), null, cache, CacheType.NODE);
    nodeCacheRef.set(nodeCache);
    CacheWriteQueueWrapper writeQueue = new CacheWriteQueueWrapper(nodeCache.writeQueue);
    nodeCache.writeQueue = writeQueue;
    this.putActions = writeQueue.putActions;
    this.invalidateActions = writeQueue.invalidateActions;
    this.id = 0;
}
Also used : Nonnull(javax.annotation.Nonnull) AtomicReference(java.util.concurrent.atomic.AtomicReference) CacheLIRS(org.apache.jackrabbit.oak.cache.CacheLIRS) PathRev(org.apache.jackrabbit.oak.plugins.document.PathRev) StringValue(org.apache.jackrabbit.oak.plugins.document.util.StringValue) File(java.io.File) Nullable(javax.annotation.Nullable) RemovalCause(com.google.common.cache.RemovalCause) Before(org.junit.Before)

Aggregations

RemovalCause (com.google.common.cache.RemovalCause)2 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 CacheLIRS (org.apache.jackrabbit.oak.cache.CacheLIRS)1 EvictionCallback (org.apache.jackrabbit.oak.cache.CacheLIRS.EvictionCallback)1 PathRev (org.apache.jackrabbit.oak.plugins.document.PathRev)1 StringValue (org.apache.jackrabbit.oak.plugins.document.util.StringValue)1 Before (org.junit.Before)1 Test (org.junit.Test)1