jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1450714 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment: OffsetCache.java SegmentCache.java
Date Wed, 27 Feb 2013 10:31:46 GMT
Author: jukka
Date: Wed Feb 27 10:31:46 2013
New Revision: 1450714

URL: http://svn.apache.org/r1450714
Log:
OAK-593: Segment-based MK

Use soft references in OffsetCache to avoid wasting memory on unused bits

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/OffsetCache.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentCache.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/OffsetCache.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/OffsetCache.java?rev=1450714&r1=1450713&r2=1450714&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/OffsetCache.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/OffsetCache.java
Wed Feb 27 10:31:46 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.oak.plugins.segment;
 
+import java.lang.ref.SoftReference;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
@@ -31,13 +32,13 @@ abstract class OffsetCache<T> {
 
     private static final int[] NO_OFFSETS = new int[0];
 
-    private static final Object[] NO_VALUES = new Object[0];
+    private static final SoftReference<?>[] NO_VALUES = new SoftReference<?>[0];
 
     private int length = 0;
 
     private int[] offsets;
 
-    private Object[] values;
+    private SoftReference<?>[] values;
 
     OffsetCache() {
         offsets = NO_OFFSETS;
@@ -57,18 +58,25 @@ abstract class OffsetCache<T> {
 
         int n = list.size();
         offsets = new int[n];
-        values = new Object[n];
+        values = new SoftReference<?>[n];
         for (int i = 0; i < n; i++) {
             Entry<T, RecordId> entry = list.get(i);
             offsets[i] = entry.getValue().getOffset();
-            values[i] = entry.getKey();
+            values[i] = new SoftReference<T>(entry.getKey());
         }
     }
 
     @SuppressWarnings("unchecked")
     public synchronized T get(int offset) {
         int i = Arrays.binarySearch(offsets, 0, length, offset);
-        if (i < 0) {
+        if (i >= 0) {
+            T value = (T) values[i].get();
+            if (value == null) {
+                value = load(offset);
+                values[i] = new SoftReference<T>(value);
+            }
+            return value;
+        } else {
             i = ~i;
             if (length < offsets.length) {
                 System.arraycopy(offsets, i, offsets, i + 1, length - i);
@@ -78,15 +86,18 @@ abstract class OffsetCache<T> {
                 System.arraycopy(offsets, 0, newOffsets, 0, i);
                 System.arraycopy(offsets, i, newOffsets, i + 1, length - i);
                 offsets = newOffsets;
-                Object[] newValues = new Object[length + SIZE_INCREMENT];
+                SoftReference<?>[] newValues =
+                        new SoftReference<?>[length + SIZE_INCREMENT];
                 System.arraycopy(values, 0, newValues, 0, i);
                 System.arraycopy(values, i, newValues, i + 1, length - i);
                 values = newValues;
             }
+
+            T value = load(offset);
             offsets[i] = offset;
-            values[i] = load(offset);
+            values[i] = new SoftReference<T>(value);
+            return value;
         }
-        return (T) values[i];
     }
 
     protected abstract T load(int offset);

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentCache.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentCache.java?rev=1450714&r1=1450713&r2=1450714&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentCache.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentCache.java
Wed Feb 27 10:31:46 2013
@@ -16,18 +16,12 @@
  */
 package org.apache.jackrabbit.oak.plugins.segment;
 
-import java.io.File;
 import java.util.UUID;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
 
 import com.google.common.cache.Cache;
 import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-import com.google.common.cache.RemovalListener;
-import com.google.common.cache.RemovalNotification;
-import com.google.common.cache.Weigher;
 
 /**
  * Combined memory and disk cache for segments.



Mime
View raw message