jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1452886 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MongoStore.java
Date Tue, 05 Mar 2013 16:37:57 GMT
Author: jukka
Date: Tue Mar  5 16:37:57 2013
New Revision: 1452886

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

Reduce the number of getHead() calls that hit the underlying MongoDB

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

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MongoStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MongoStore.java?rev=1452886&r1=1452885&r2=1452886&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MongoStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MongoStore.java
Tue Mar  5 16:37:57 2013
@@ -19,6 +19,7 @@ package org.apache.jackrabbit.oak.plugin
 import java.util.List;
 import java.util.UUID;
 import java.util.concurrent.Callable;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeState;
 
@@ -32,6 +33,9 @@ import com.mongodb.Mongo;
 
 public class MongoStore implements SegmentStore {
 
+    private static final long UPDATE_INTERVAL =
+            TimeUnit.NANOSECONDS.convert(10, TimeUnit.MILLISECONDS);
+
     private final DBCollection segments;
 
     private final DBCollection journals;
@@ -65,19 +69,39 @@ public class MongoStore implements Segme
     @Override
     public Journal getJournal(final String name) {
         return new Journal() {
+
+            private long nextUpdate = System.nanoTime() - 2 * UPDATE_INTERVAL;
+
+            private RecordId head = getHead();
+
             @Override
-            public RecordId getHead() {
-                DBObject journal = journals.findOne(new BasicDBObject("_id", name));
-                return RecordId.fromString(journal.get("head").toString());
+            public synchronized RecordId getHead() {
+                long now = System.nanoTime();
+                if (now >= nextUpdate) {
+                    DBObject journal = journals.findOne(new BasicDBObject("_id", name));
+                    head = RecordId.fromString(journal.get("head").toString());
+                    nextUpdate = now + UPDATE_INTERVAL;
+                }
+                return head;
             }
+
             @Override
             public boolean setHead(RecordId base, RecordId head) {
                 DBObject baseObject = new BasicDBObject(
                         ImmutableMap.of("_id", name, "head", base.toString()));
                 DBObject headObject = new BasicDBObject(
                         ImmutableMap.of("_id", name, "head", head.toString()));
-                return journals.findAndModify(baseObject, headObject) != null;
+                if (journals.findAndModify(baseObject, headObject) != null) {
+                    this.head = head;
+                    nextUpdate = System.nanoTime() + UPDATE_INTERVAL;
+                    return true;
+                } else if (base.equals(this.head)) {
+                    // force an update at next getHead() call
+                    nextUpdate = System.nanoTime();
+                }
+                return false;
             }
+
             @Override
             public void merge() {
                 throw new UnsupportedOperationException();



Mime
View raw message