jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1143104 - in /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk: MicroKernelImpl.java api/MicroKernel.java mem/MemoryKernelImpl.java util/CommitGate.java
Date Tue, 05 Jul 2011 15:30:53 GMT
Author: thomasm
Date: Tue Jul  5 15:30:52 2011
New Revision: 1143104

URL: http://svn.apache.org/viewvc?rev=1143104&view=rev
Log:
New method waitForCommit to avoid polling delays.

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/CommitGate.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java?rev=1143104&r1=1143103&r2=1143104&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
Tue Jul  5 15:30:52 2011
@@ -25,6 +25,7 @@ import org.apache.jackrabbit.mk.store.Co
 import org.apache.jackrabbit.mk.store.Node;
 import org.apache.jackrabbit.mk.store.NodeDiffHandler;
 import org.apache.jackrabbit.mk.store.NodeUtils;
+import org.apache.jackrabbit.mk.util.CommitGate;
 import org.apache.jackrabbit.mk.util.PathUtils;
 
 import java.io.InputStream;
@@ -41,6 +42,7 @@ import java.util.Map;
 public class MicroKernelImpl implements MicroKernel {
 
     protected Repository rep;
+    private final CommitGate gate = new CommitGate();
 
     public MicroKernelImpl(String homeDir) throws MicroKernelException {
         init(homeDir);
@@ -56,6 +58,7 @@ public class MicroKernelImpl implements 
     }
 
     public void dispose() {
+        gate.commit("end");
         if (rep != null) {
             try {
                 rep.shutDown();
@@ -112,6 +115,10 @@ public class MicroKernelImpl implements 
         return buff.endArray().toString();
     }
 
+    public String waitForCommit(String oldHeadRevision, long maxWaitMillis) throws MicroKernelException
{
+        return gate.waitForCommit(oldHeadRevision, maxWaitMillis);
+    }
+
     public String getJournal(String fromRevisionId, String toRevisionId) throws MicroKernelException
{
         if (rep == null) {
             throw new IllegalStateException("this instance has already been disposed");
@@ -304,7 +311,9 @@ public class MicroKernelImpl implements 
                     throw new AssertionError("token type: " + t.getTokenType());
                 }
             }
-            return cb.doCommit();
+            String newHead = cb.doCommit();
+            gate.commit(newHead);
+            return newHead;
         } catch (Exception e) {
             throw new MicroKernelException(e);
         }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java?rev=1143104&r1=1143103&r2=1143104&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/api/MicroKernel.java
Tue Jul  5 15:30:52 2011
@@ -123,6 +123,21 @@ public interface MicroKernel {
             throws MicroKernelException;
 
     /**
+     * Wait for a commit to occur that is newer than the given revision number.
+     *
+     * This method is useful efficient polling. The method will return the current head revision
+     * if it is newer than the given old revision number, or wait until the given number
of
+     * milliseconds passed or a new head revision is available.
+     *
+     * @param maxWaitMillis the maximum number of milliseconds to wait (0 if the
+     *            method should not wait).
+     * @return the current head revision
+     * @throws MicroKernelException if an error occurs
+     */
+
+    String waitForCommit(String oldHeadRevision, long maxWaitMillis) throws MicroKernelException;
+
+    /**
      * Returns a revision journal, starting with <code>fromRevisionId</code>
      * and ending with <code>toRevisionId</code>.
      * <p/>

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java?rev=1143104&r1=1143103&r2=1143104&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
Tue Jul  5 15:30:52 2011
@@ -26,6 +26,7 @@ import java.util.TreeMap;
 import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
+import org.apache.jackrabbit.mk.util.CommitGate;
 import org.apache.jackrabbit.mk.util.NonDescendingClock;
 import org.apache.jackrabbit.mk.util.PathUtils;
 
@@ -43,6 +44,7 @@ public class MemoryKernelImpl implements
     private ArrayList<Revision> revisionList;
     private NodeImpl headRoot;
     private NonDescendingClock clock = new NonDescendingClock(System.currentTimeMillis());
+    private final CommitGate gate = new CommitGate();
 
     public synchronized static MemoryKernelImpl get(String name) {
         MemoryKernelImpl instance = INSTANCES.get(name);
@@ -85,10 +87,9 @@ public class MemoryKernelImpl implements
         headRevId++;
         jsonDiff = apply(path, jsonDiff);
         commit(jsonDiff, message);
-
-        System.out.println("commit " + getRevisionId(headRevId) + " = " + jsonDiff);
-
-        return getHeadRevision();
+        String head = getHeadRevision();
+        gate.commit(head);
+        return head;
     }
 
     private String apply(String rootPath, String jsonDiff) {
@@ -174,6 +175,10 @@ public class MemoryKernelImpl implements
         return buff.endArray().toString();
     }
 
+    public String waitForCommit(String oldHeadRevision, long maxWaitMillis) {
+        return gate.waitForCommit(oldHeadRevision, maxWaitMillis);
+    }
+
     public String getJournal(String fromRevisionId, String toRevisionId) {
         long fromRevId = parseRevisionId(fromRevisionId);
         long toRevId = parseRevisionId(toRevisionId);
@@ -245,7 +250,7 @@ public class MemoryKernelImpl implements
     }
 
     public void dispose() {
-        // nothing to do
+        gate.commit("end");
     }
 
 }

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/CommitGate.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/CommitGate.java?rev=1143104&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/CommitGate.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/CommitGate.java
Tue Jul  5 15:30:52 2011
@@ -0,0 +1,78 @@
+/*
+ * 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.mk.util;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * A gate where listeners can wait for a new commit.
+ */
+public class CommitGate {
+
+    private volatile String currentHead;
+    private volatile AtomicReference<CountDownLatch> latchRef = new AtomicReference<CountDownLatch>();
+
+    /**
+     * Wait for a new commit to occur. In very few cases, this method may return
+     * with the old head before the requested timeout.
+     *
+     * @param lastHead the last head
+     * @param millis the maximum number of milliseconds to wait (0 means don't wait)
+     * @return the new head (or old head, if no new commit occurred)
+     */
+    public String waitForCommit(String lastHead, long millis) {
+        if (millis == 0 || (currentHead != null && !currentHead.equals(lastHead)))
{
+            return currentHead;
+        }
+        CountDownLatch latch = latchRef.get();
+        if (latch == null) {
+            latch = new CountDownLatch(1);
+            CountDownLatch old = latchRef.getAndSet(latch);
+            if (old != null) {
+                // may cause a spurious release, but that's ok
+                old.countDown();
+            }
+        }
+        try {
+            latch.await(millis, TimeUnit.MILLISECONDS);
+        } catch (InterruptedException e) {
+            // ignore
+        }
+        return currentHead;
+    }
+
+    /**
+     * Commit a new head. Waiting threads are awoken.
+     *
+     * @param newHead the new head
+     */
+    public void commit(String newHead) {
+        currentHead = newHead;
+        CountDownLatch latch = latchRef.get();
+        if (latch != null) {
+            // may cause a spurious release, but that's ok
+            latch.countDown();
+            latch = latchRef.getAndSet(null);
+            if (latch != null) {
+                latch.countDown();
+            }
+        }
+    }
+
+}



Mime
View raw message