Author: mduerig
Date: Mon Jul 25 14:22:47 2016
New Revision: 1754016

URL: http://svn.apache.org/viewvc?rev=1754016&view=rev
Log:
OAK-4277: Finalise de-duplication caches
RecordCache tests, correcting off by one error in cache size, minor cleanup

Added:
    
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/RecordCacheTest.java
Modified:
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCache.java
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/WriterCacheManager.java

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCache.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCache.java?rev=1754016&r1=1754015&r2=1754016&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCache.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCache.java
 Mon Jul 25 14:22:47 2016
@@ -50,14 +50,14 @@ public abstract class RecordCache<T> {
     @Nonnull
     public static <T> Supplier<RecordCache<T>> factory(int size) {
         if (size <= 0) {
-            return Empty.supplier();
+            return Empty.emptyFactory();
         } else {
-            return Default.supplier(size);
+            return Default.defaultFactory(size);
         }
     }
 
     private static class Empty<T> extends RecordCache<T> {
-        static final <T> Supplier<RecordCache<T>> supplier() {
+        static final <T> Supplier<RecordCache<T>> emptyFactory() {
             return  new Supplier<RecordCache<T>>() {
                 @Override
                 public RecordCache<T> get() {
@@ -76,7 +76,7 @@ public abstract class RecordCache<T> {
     private static class Default<T> extends RecordCache<T> {
         private final Map<T, RecordId> records;
 
-        static final <T> Supplier<RecordCache<T>> supplier(final int size) {
+        static final <T> Supplier<RecordCache<T>> defaultFactory(final int 
size) {
             return new Supplier<RecordCache<T>>() {
                 @Override
                 public RecordCache<T> get() {
@@ -89,7 +89,7 @@ public abstract class RecordCache<T> {
             records = new LinkedHashMap<T, RecordId>(size * 4 / 3, 0.75f, 
true) {
                 @Override
                 protected boolean removeEldestEntry(Map.Entry<T, RecordId> 
eldest) {
-                    return size() >= size;
+                    return size() > size;
                 }
             };
         }

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/WriterCacheManager.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/WriterCacheManager.java?rev=1754016&r1=1754015&r2=1754016&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/WriterCacheManager.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/WriterCacheManager.java
 Mon Jul 25 14:22:47 2016
@@ -156,7 +156,7 @@ public abstract class WriterCacheManager
             return nodeCaches.getGeneration(generation);
         }
 
-        protected void evictCaches(Predicate<Integer> generations) {
+        protected final void evictCaches(Predicate<Integer> generations) {
             stringCaches.evictGenerations(generations);
             templateCaches.evictGenerations(generations);
             nodeCaches.evictGenerations(generations);

Added: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/RecordCacheTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/RecordCacheTest.java?rev=1754016&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/RecordCacheTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/RecordCacheTest.java
 Mon Jul 25 14:22:47 2016
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.segment;
+
+import static com.google.common.collect.Maps.newLinkedHashMap;
+import static org.apache.jackrabbit.oak.segment.RecordCache.newRecordCache;
+import static org.apache.jackrabbit.oak.segment.TestUtils.newRecordId;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Random;
+
+import org.apache.jackrabbit.oak.segment.memory.MemoryStore;
+import org.junit.Test;
+
+public class RecordCacheTest {
+    private final Random rnd = new Random();
+    private final MemoryStore store = new MemoryStore();
+
+    public RecordCacheTest() throws IOException {}
+
+    @Test
+    public void emptyCache() {
+        RecordCache<String> cache = newRecordCache(0);
+        assertNull(cache.get("any"));
+
+        cache.put("key", (newRecordId(store, rnd)));
+        assertNull(cache.get("key"));
+    }
+
+    @Test
+    public void putAndGet() {
+        RecordCache<String> cache = newRecordCache(10);
+        assertNull(cache.get("any"));
+
+        RecordId value = newRecordId(store, rnd);
+        cache.put("key", value);
+        assertEquals(value, cache.get("key"));
+    }
+
+    @Test
+    public void invalidate() {
+        RecordCache<String> cache = newRecordCache(10);
+        Map<String, RecordId> keys = newLinkedHashMap();
+        for (int k = 0; k < 10; k ++) {
+            String key = "key-" + k;
+            RecordId value = newRecordId(store, rnd);
+            keys.put(key, value);
+            cache.put(key, value);
+        }
+
+        for (Entry<String, RecordId> entry : keys.entrySet()) {
+            assertEquals(entry.getKey() + " should be in the cache",
+                    entry.getValue(), cache.get(entry.getKey()));
+        }
+
+        RecordId value = newRecordId(store, rnd);
+        // This should invalidate "key-0", which is the least recently 
accessed key
+        cache.put("key", value);
+        keys.put("key", value);
+        keys.remove("key-0");
+        for (Entry<String, RecordId> entry : keys.entrySet()) {
+            assertEquals(entry.getKey() + " should be in the cache",
+                    entry.getValue(), cache.get(entry.getKey()));
+        }
+    }
+
+}


Reply via email to