jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1528590 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment: AbstractStore.java SegmentCache.java file/FileStore.java memory/MemoryStore.java mongo/MongoStore.java
Date Wed, 02 Oct 2013 18:48:40 GMT
Author: jukka
Date: Wed Oct  2 18:48:40 2013
New Revision: 1528590

URL: http://svn.apache.org/r1528590
Log:
OAK-1031: SegmentMK: Fewer segment lookups

Push the segment cache down to AbstractStore to avoid duplication and to simplify further
refactoring

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/AbstractStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentCache.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/AbstractStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/AbstractStore.java?rev=1528590&r1=1528589&r2=1528590&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/AbstractStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/AbstractStore.java
Wed Oct  2 18:48:40 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.oak.plugins.segment;
 
+import java.util.UUID;
 import java.util.concurrent.Callable;
 
 import org.apache.jackrabbit.oak.cache.CacheLIRS;
@@ -24,17 +25,49 @@ import com.google.common.cache.Cache;
 
 public abstract class AbstractStore implements SegmentStore {
 
+    private final SegmentCache cache;
+
     private final Cache<RecordId, Object> records =
             CacheLIRS.newBuilder().maximumSize(1000).build();
 
     private final SegmentWriter writer = new SegmentWriter(this);
 
+    protected AbstractStore(int cacheSize) {
+        this.cache = SegmentCache.create(cacheSize);
+    }
+
+    protected abstract Segment loadSegment(UUID id) throws Exception;
+
     @Override
     public SegmentWriter getWriter() {
         return writer;
     }
 
     @Override
+    public Segment readSegment(final UUID id) {
+        try {
+            Segment segment = getWriter().getCurrentSegment(id);
+            if (segment == null) {
+                segment = cache.getSegment(id, new Callable<Segment>() {
+                    @Override
+                    public Segment call() throws Exception {
+                        return loadSegment(id);
+                    }
+                });
+            }
+            return segment;
+        } catch (IllegalStateException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public void deleteSegment(UUID segmentId) {
+    }
+
+    @Override
     public <T> T getRecord(RecordId id, Callable<T> loader) {
         @SuppressWarnings("unchecked")
         T record = (T) records.getIfPresent(id);
@@ -50,4 +83,9 @@ public abstract class AbstractStore impl
         return record;
     }
 
+    @Override
+    public void close() {
+        records.invalidateAll();
+    }
+
 }

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=1528590&r1=1528589&r2=1528590&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 Oct  2 18:48:40 2013
@@ -34,8 +34,6 @@ import org.apache.jackrabbit.oak.cache.C
  */
 public class SegmentCache {
 
-    private static final int DEFAULT_MEMORY_CACHE_SIZE = 1 << 28; // 256MB
-
     protected final int maximumSize;
     
     protected int currentSize;
@@ -59,10 +57,6 @@ public class SegmentCache {
         this.maximumSize = maximumSize;
     }
 
-    public static SegmentCache create() {
-        return create(DEFAULT_MEMORY_CACHE_SIZE);
-    }
-
     public static SegmentCache create(int maximumSize) {
         if (Boolean.parseBoolean(System.getProperty("oak.segmentCacheLIRS", "false"))) {
             return new SegmentCacheLIRS(maximumSize);

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java?rev=1528590&r1=1528589&r2=1528590&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java
Wed Oct  2 18:48:40 2013
@@ -30,19 +30,19 @@ import java.nio.ByteBuffer;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.concurrent.Callable;
 import java.util.UUID;
 
 import org.apache.jackrabbit.oak.plugins.segment.AbstractStore;
 import org.apache.jackrabbit.oak.plugins.segment.Journal;
 import org.apache.jackrabbit.oak.plugins.segment.RecordId;
 import org.apache.jackrabbit.oak.plugins.segment.Segment;
-import org.apache.jackrabbit.oak.plugins.segment.SegmentCache;
 import org.apache.jackrabbit.oak.plugins.segment.SegmentNodeState;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 
 public class FileStore extends AbstractStore {
 
+    private static final int DEFAULT_MEMORY_CACHE_SIZE = 1 << 28; // 256MB
+
     private static final long SEGMENT_MAGIC = 0x4f616b0a527845ddL;
 
     private static final long JOURNAL_MAGIC = 0xdf36544212c0cb24L;
@@ -61,10 +61,9 @@ public class FileStore extends AbstractS
 
     private final Map<String, Journal> journals = newHashMap();
 
-    private final SegmentCache cache = SegmentCache.create();
-
     public FileStore(File directory, int maxFileSize, boolean memoryMapping)
             throws IOException {
+        super(DEFAULT_MEMORY_CACHE_SIZE);
         checkNotNull(directory).mkdirs();
         this.directory = directory;
         this.maxFileSize = maxFileSize;
@@ -109,11 +108,11 @@ public class FileStore extends AbstractS
     @Override
     public synchronized void close() {
         try {
+            super.close();
             for (TarFile file : files) {
                 file.close();
             }
             files.clear();
-            cache.clear();
             System.gc(); // for any memory-mappings that are no longer used
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -131,26 +130,7 @@ public class FileStore extends AbstractS
     }
 
     @Override
-    public Segment readSegment(final UUID id) {
-        try {
-            Segment segment = getWriter().getCurrentSegment(id);
-            if (segment == null) {
-                segment = cache.getSegment(id, new Callable<Segment>() {
-                    @Override
-                    public Segment call() throws Exception {
-                        return loadSegment(id);
-                    }
-                });
-            }
-            return segment;
-        } catch (IllegalStateException e) {
-            throw e;
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
-    }
-
-    private Segment loadSegment(UUID id) throws Exception {
+    protected Segment loadSegment(UUID id) throws Exception {
         for (TarFile file : files) {
             ByteBuffer buffer = file.readEntry(id);
             if (buffer != null) {
@@ -221,11 +201,7 @@ public class FileStore extends AbstractS
     @Override
     public void deleteSegment(UUID segmentId) {
         // TODO: implement
-        try {
-            cache.removeSegment(segmentId);
-        } catch (InterruptedException e) {
-            throw new RuntimeException(e);
-        }
+        super.deleteSegment(segmentId);
     }
 
     synchronized void writeJournals() throws IOException {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java?rev=1528590&r1=1528589&r2=1528590&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java
Wed Oct  2 18:48:40 2013
@@ -41,6 +41,7 @@ public class MemoryStore extends Abstrac
             Maps.newConcurrentMap();
 
     public MemoryStore(NodeState root) {
+        super(1024 * 1024);
         NodeBuilder builder = EMPTY_NODE.builder();
         builder.setChildNode("root", root);
         journals.put("root", new MemoryJournal(this, builder.getNodeState()));
@@ -65,11 +66,8 @@ public class MemoryStore extends Abstrac
     }
 
     @Override
-    public Segment readSegment(UUID id) {
-        Segment segment = getWriter().getCurrentSegment(id);
-        if (segment == null) {
-            segment = segments.get(id);
-        }
+    protected Segment loadSegment(UUID id) {
+        Segment segment = segments.get(id);
         if (segment != null) {
             return segment;
         } else {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java?rev=1528590&r1=1528589&r2=1528590&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java
Wed Oct  2 18:48:40 2013
@@ -27,12 +27,10 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
-import java.util.concurrent.Callable;
 
 import org.apache.jackrabbit.oak.plugins.segment.AbstractStore;
 import org.apache.jackrabbit.oak.plugins.segment.Journal;
 import org.apache.jackrabbit.oak.plugins.segment.Segment;
-import org.apache.jackrabbit.oak.plugins.segment.SegmentCache;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 
 import com.google.common.collect.Lists;
@@ -54,12 +52,10 @@ public class MongoStore extends Abstract
 
     private final Map<String, Journal> journals = Maps.newHashMap();
 
-    private final SegmentCache cache;
-
     public MongoStore(DB db, int cacheSize) {
+        super(cacheSize);
         this.db = checkNotNull(db);
         this.segments = db.getCollection("segments");
-        this.cache = SegmentCache.create(cacheSize);
         NodeBuilder builder = EMPTY_NODE.builder();
         builder.child("root");
         journals.put("root", new MongoJournal(
@@ -87,24 +83,6 @@ public class MongoStore extends Abstract
     }
 
     @Override
-    public Segment readSegment(final UUID segmentId) {
-        try {
-            Segment segment = getWriter().getCurrentSegment(segmentId);
-            if (segment == null) {
-                segment = cache.getSegment(segmentId, new Callable<Segment>() {
-                    @Override
-                    public Segment call() throws Exception {
-                        return findSegment(segmentId);
-                    }
-                });
-            }
-            return segment;
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
-    }
-
-    @Override
     public void writeSegment(
             UUID segmentId, byte[] data, int offset, int length,
             List<UUID> referencedSegmentIds) {
@@ -113,7 +91,8 @@ public class MongoStore extends Abstract
         insertSegment(segmentId, d, referencedSegmentIds);
     }
 
-    private Segment findSegment(UUID segmentId) {
+    @Override
+    protected Segment loadSegment(UUID segmentId) {
         DBObject id = new BasicDBObject("_id", segmentId.toString());
         DBObject fields = new BasicDBObject(of("data", 1, "uuids", 1));
 
@@ -152,11 +131,7 @@ public class MongoStore extends Abstract
     @Override
     public void deleteSegment(UUID segmentId) {
         segments.remove(new BasicDBObject("_id", segmentId.toString()));
-        try {
-            cache.removeSegment(segmentId);
-        } catch (InterruptedException e) {
-            throw new RuntimeException(e);
-        }
+        super.deleteSegment(segmentId);;
     }
 
 }



Mime
View raw message