jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1407119 [3/9] - in /jackrabbit/oak/trunk: ./ oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/ oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/ oak-mk-perf/ oak-mk-perf/src/main/java/org/apache/jackrabbit/mk/tasks/ oak-...
Date Thu, 08 Nov 2012 15:18:02 GMT
Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/api/model/Commit.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/api/model/Commit.java?rev=1407119&r1=1407118&r2=1407119&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/api/model/Commit.java (original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/api/model/Commit.java Thu Nov  8 15:17:50 2012
@@ -18,22 +18,53 @@ package org.apache.jackrabbit.mongomk.ap
 
 import java.util.List;
 
+import org.apache.jackrabbit.mongomk.api.instruction.Instruction;
+
 /**
  * A higher level object representing a commit.
- *
- * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
  */
 public interface Commit {
 
     /**
-     * Returns the <a href="http://wiki.apache.org/jackrabbit/Jsop">JSOP</a> diff of this commit.
+     * Returns the paths affected by the commit.
+     *
+     * @return The paths affected by the commit.
+     */
+    List<String> getAffectedPaths();
+
+    /**
+     * Returns the base revision id the commit is based on.
+     *
+     * @return The base revision id the commit is based on.
+     */
+    Long getBaseRevisionId();
+
+    /**
+     * Returns the private branch id the commit is under or {@code null} if the
+     * commit is in the public branch.
+     *
+     * @return The private branch id or {@code null}
+     */
+    String getBranchId();
+
+    /**
+     * Returns the <a href="http://wiki.apache.org/jackrabbit/Jsop">JSOP</a>
+     * diff of this commit.
      *
      * @return The {@link String} representing the diff.
      */
     String getDiff();
 
     /**
-     * Returns the {@link List} of {@link Instruction}s which were created from the diff.
+     * Determines whether the commit failed or not.
+     *
+     * @return True if the commit failed.
+     */
+    boolean isFailed();
+
+    /**
+     * Returns the {@link List} of {@link Instruction}s which were created from
+     * the diff.
      *
      * @see #getDiff()
      *
@@ -56,10 +87,9 @@ public interface Commit {
     String getPath();
 
     /**
-     * Returns the revision id of this commit if known already, else this will return {@code null}.
-     * The revision id will be determined only after the commit has been successfully performed.
-     *
-     * @see #setRevisionId(Long)
+     * Returns the revision id of this commit if known already, else this will
+     * return {@code null}. The revision id will be determined only after the
+     * commit has been successfully performed.
      *
      * @return The revision id of this commit or {@code null}.
      */
@@ -68,17 +98,14 @@ public interface Commit {
     /**
      * Sets the revision id of this commit.
      *
-     * @see #getRevisionId()
-     *
      * @param revisionId The revision id to set.
      */
     void setRevisionId(Long revisionId);
 
-
     /**
      * Returns the timestamp of this commit.
      *
      * @return The timestamp of this commit.
      */
-    long getTimestamp();
-}
+    Long getTimestamp();
+}
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/api/model/Node.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/api/model/Node.java?rev=1407119&r1=1407118&r2=1407119&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/api/model/Node.java (original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/api/model/Node.java Thu Nov  8 15:17:50 2012
@@ -18,51 +18,59 @@ package org.apache.jackrabbit.mongomk.ap
 
 import java.util.Iterator;
 import java.util.Map;
-import java.util.Set;
+
+import org.apache.jackrabbit.mk.model.NodeDiffHandler;
 
 /**
  * A higher level object representing a node.
- *
- * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
  */
 public interface Node {
 
     /**
+     * Returns the descendant node entry (descendant)
+     *
+     * @param name Name of the descendant.
+     * @return Descendant node.
+     */
+    Node getChildNodeEntry(String name);
+
+    /**
+     *
      * Returns the total number of children of this node.
      *
      * <p>
-     * <strong>This is not necessarily equal to the number of children returned by {@link #getChildren()} since this
-     * {@code Node} might be created with only a subset of children.</strong>
+     * <strong>This is not necessarily equal to the number of children returned by
+     * {@link #getChildren()} since this {@code Node} might be created with only
+     * a subset of children.</strong>
      * </p>
      *
      * @return The total number of children.
      */
-    long getChildCount();
+    int getChildNodeCount();
 
     /**
-     * Returns the children this {@code Node} was created with.
+     * Returns the children iterator for the supplied offset and count.
      *
-     * @return The children.
+     * @param offset The offset to return the children from.
+     * @param count The number of children to return.
+     * @return Iterator with child entries.
      */
-    Set<Node> getChildren(); // TODO Replace Set with Collection
-
-    // TODO - [Mete] Document.
-    Iterator<Node> getChildEntries(int offset, int count);
+    Iterator<Node> getChildNodeEntries(int offset, int count);
 
     /**
-     * Returns the descendants (children, children of the children, etc) this {@code Node} was created with.
+     * Returns the properties this {@code Node} was created with.
      *
-     * @param includeThis Flag indicating whether this {@code Node} should be included in the list.
-     * @return The descendants.
+     * @return The properties.
      */
-    Set<Node> getDescendants(boolean includeThis);
+    Map<String, String> getProperties();
 
     /**
-     * Returns the name of this {@code Node}.
+     * Diffs this node with the other node and calls the passed in diff handler.
      *
-     * @return The name.
+     * @param otherNode Other node.
+     * @param nodeDiffHandler Diff handler.
      */
-    String getName();
+    void diff(Node otherNode, NodeDiffHandler nodeDiffHandler);
 
     /**
      * Returns the path of this {@code Node}.
@@ -72,29 +80,11 @@ public interface Node {
     String getPath();
 
     /**
-     * Returns the properties this {@code Node} was created with.
-     *
-     * @return The properties.
-     */
-    Map<String, Object> getProperties();
-
-    /**
      * Returns the revision id of this node if known already, else this will return {@code null}.
      * The revision id will be determined only after the commit has been successfully
      * performed or the node has been read as part of an existing revision.
      *
-     * @see #setRevisionId(Long)
-     *
      * @return The revision id of this commit or {@code null}.
      */
     Long getRevisionId();
-
-    /**
-     * Sets the revision id of this node.
-     *
-     * @see #getRevisionId()
-     *
-     * @param revisionId The revision id to set.
-     */
-    void setRevisionId(Long revisionId);
-}
+}
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoConnection.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoConnection.java?rev=1407119&r1=1407118&r2=1407119&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoConnection.java (original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoConnection.java Thu Nov  8 15:17:50 2012
@@ -16,27 +16,15 @@
  */
 package org.apache.jackrabbit.mongomk.impl;
 
-import org.apache.jackrabbit.mongomk.model.CommitMongo;
-import org.apache.jackrabbit.mongomk.model.HeadMongo;
-import org.apache.jackrabbit.mongomk.model.NodeMongo;
-
 import com.mongodb.DB;
-import com.mongodb.DBCollection;
 import com.mongodb.Mongo;
-import com.mongodb.gridfs.GridFS;
 
 /**
- * The {@code MongoConnection} contains connection properties for the {@code MongoDB}.
- *
- * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ * The {@code MongoConnection} abstracts connection to the {@code MongoDB}.
  */
 public class MongoConnection {
 
-    public static final String COLLECTION_COMMITS = "commits";
-    public static final String COLLECTION_HEAD = "head";
-    public static final String COLLECTION_NODES = "nodes";
     private final DB db;
-    private final GridFS gridFS;
     private final Mongo mongo;
 
     /**
@@ -50,18 +38,6 @@ public class MongoConnection {
     public MongoConnection(String host, int port, String database) throws Exception {
         mongo = new Mongo(host, port);
         db = mongo.getDB(database);
-        gridFS = new GridFS(db);
-    }
-
-    /**
-     * Returns the commit {@link DBCollection}.
-     *
-     * @return The commit {@link DBCollection}.
-     */
-    public DBCollection getCommitCollection() {
-        DBCollection commitCollection = db.getCollection(COLLECTION_COMMITS);
-        commitCollection.setObjectClass(CommitMongo.class);
-        return commitCollection;
     }
 
     /**
@@ -74,50 +50,10 @@ public class MongoConnection {
     }
 
     /**
-     * Returns the {@link GridFS}.
-     *
-     * @return The {@link GridFS}.
-     */
-    public GridFS getGridFS() {
-        return gridFS;
-    }
-
-    /**
-     * Returns the {@link Mongo}.
-     *
-     * @return The {@link Mongo}.
-     */
-    public Mongo getMongo() {
-        return mongo;
-    }
-
-    /**
-     * Returns the head {@link DBCollection}.
-     *
-     * @return The head {@link DBCollection}.
-     */
-    public DBCollection getHeadCollection() {
-        DBCollection headCollection = db.getCollection(COLLECTION_HEAD);
-        headCollection.setObjectClass(HeadMongo.class);
-        return headCollection;
-    }
-
-    /**
-     * Returns the node {@link DBCollection}.
-     *
-     * @return The node {@link DBCollection}.
-     */
-    public DBCollection getNodeCollection() {
-        DBCollection nodeCollection = db.getCollection(COLLECTION_NODES);
-        nodeCollection.setObjectClass(NodeMongo.class);
-        return nodeCollection;
-    }
-
-    /**
      * Closes the underlying Mongo instance
      */
     public void close(){
-        if(mongo != null){
+        if (mongo != null){
             mongo.close();
         }
     }

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoMicroKernel.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoMicroKernel.java?rev=1407119&r1=1407118&r2=1407119&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoMicroKernel.java (original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoMicroKernel.java Thu Nov  8 15:17:50 2012
@@ -17,117 +17,148 @@
 package org.apache.jackrabbit.mongomk.impl;
 
 import java.io.InputStream;
+import java.util.UUID;
 
 import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.mk.api.MicroKernelException;
+import org.apache.jackrabbit.mk.blobs.BlobStore;
+import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.util.NodeFilter;
-import org.apache.jackrabbit.mongomk.api.BlobStore;
 import org.apache.jackrabbit.mongomk.api.NodeStore;
 import org.apache.jackrabbit.mongomk.api.model.Commit;
 import org.apache.jackrabbit.mongomk.api.model.Node;
-import org.apache.jackrabbit.mongomk.impl.builder.CommitBuilder;
 import org.apache.jackrabbit.mongomk.impl.json.JsonUtil;
+import org.apache.jackrabbit.mongomk.impl.model.CommitBuilder;
+import org.apache.jackrabbit.mongomk.impl.model.MongoCommit;
+import org.apache.jackrabbit.mongomk.impl.model.tree.MongoNodeState;
 
 /**
  * The {@code MongoDB} implementation of the {@link MicroKernel}.
  *
  * <p>
- * This class will transform and delegate to instances of {@link NodeStore} and {@link BlobStore}.
+ * This class will transform and delegate to instances of {@link NodeStore} and
+ * {@link BlobStore}.
  * </p>
- *
- * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
  */
 public class MongoMicroKernel implements MicroKernel {
 
+    private final MongoConnection mongoConnection;
     private final BlobStore blobStore;
     private final NodeStore nodeStore;
 
     /**
      * Constructs a new {@code MongoMicroKernel}.
      *
+     * @param mongoConnection Connection to MongoDB.
      * @param nodeStore The {@link NodeStore}.
      * @param blobStore The {@link BlobStore}.
      */
-    public MongoMicroKernel(NodeStore nodeStore, BlobStore blobStore) {
+    public MongoMicroKernel(MongoConnection mongoConnection, NodeStore nodeStore,
+            BlobStore blobStore) {
+        this.mongoConnection = mongoConnection;
         this.nodeStore = nodeStore;
         this.blobStore = blobStore;
     }
 
+    public void dispose() {
+        mongoConnection.close();
+    }
+
+    /**
+     * Returns the underlying blob store.
+     *
+     * @return Blob store.
+     */
+    public BlobStore getBlobStore() {
+        return blobStore;
+    }
+
+    /**
+     * Returns the underlying node store.
+     *
+     * @return Node store.
+     */
+    public NodeStore getNodeStore() {
+        return nodeStore;
+    }
+
     @Override
     public String branch(String trunkRevisionId) throws MicroKernelException {
-        throw new UnsupportedOperationException("Branch is currently not supported.");
+        String revId = trunkRevisionId == null ? getHeadRevision() : trunkRevisionId;
+
+        try {
+            MongoCommit commit = (MongoCommit)CommitBuilder.build("", "", revId,
+                    MongoNodeStore.INITIAL_COMMIT_MESSAGE);
+            commit.setBranchId(UUID.randomUUID().toString());
+            return nodeStore.commit(commit);
+        } catch (Exception e) {
+            throw new MicroKernelException(e);
+        }
     }
 
     @Override
     public String commit(String path, String jsonDiff, String revisionId, String message) throws MicroKernelException {
-        String newRevisionId = null;
-
         try {
-            Commit commit = CommitBuilder.build(path, jsonDiff, message);
-            newRevisionId = nodeStore.commit(commit);
+            Commit commit = CommitBuilder.build(path, jsonDiff, revisionId, message);
+            return nodeStore.commit(commit);
         } catch (Exception e) {
             throw new MicroKernelException(e);
         }
-
-        return newRevisionId;
     }
 
     @Override
-    public String diff(String fromRevisionId, String toRevisionId, String filter,
+    public String diff(String fromRevisionId, String toRevisionId, String path,
             int depth) throws MicroKernelException {
-        throw new UnsupportedOperationException("Diff is currently not supported.");
+        try {
+            return nodeStore.diff(fromRevisionId, toRevisionId, path, depth);
+        } catch (Exception e) {
+            throw new MicroKernelException(e);
+        }
     }
 
     @Override
-    public long getChildNodeCount(String path, String revisionId) throws MicroKernelException {
-        long childNodeCount = 0L;
-
+    public long getChildNodeCount(String path, String revisionId)
+            throws MicroKernelException {
+        Node node;
         try {
-            String revId = null;
-            if (revisionId != null) {
-                revId = new String(revisionId);
-            }
-            Node rootOfPath = nodeStore.getNodes(path, revId, 0, 0, -1, null);
-            if (rootOfPath != null) {
-                childNodeCount = rootOfPath.getChildCount();
-            }
+            node = nodeStore.getNodes(path, revisionId, 0, 0, -1, null);
         } catch (Exception e) {
             throw new MicroKernelException(e);
         }
-
-        return childNodeCount;
+        if (node != null) {
+            return node.getChildNodeCount();
+        } else {
+            throw new MicroKernelException("Path " + path + " not found in revision "
+                    + revisionId);
+        }
     }
 
     @Override
     public String getHeadRevision() throws MicroKernelException {
-        String headRevisionId = null;
-
         try {
-            headRevisionId = nodeStore.getHeadRevision();
+            return nodeStore.getHeadRevision();
         } catch (Exception e) {
             throw new MicroKernelException(e);
         }
-
-        return headRevisionId;
     }
 
     @Override
     public String getJournal(String fromRevisionId, String toRevisionId,
             String path) throws MicroKernelException {
-        return nodeStore.getJournal(fromRevisionId, toRevisionId, path);
+        try {
+            return nodeStore.getJournal(fromRevisionId, toRevisionId, path);
+        } catch (Exception e) {
+            throw new MicroKernelException(e);
+        }
     }
 
     @Override
     public long getLength(String blobId) throws MicroKernelException {
-        long length = -1;
-
         try {
-            length = blobStore.getBlobLength(blobId);
+            return blobStore.getBlobLength(blobId);
         } catch (Exception e) {
             throw new MicroKernelException(e);
         }
-
-        return length;
     }
 
     @Override
@@ -141,74 +172,75 @@ public class MongoMicroKernel implements
         }
 
         try {
-            // FIXME [Mete] Should filter, offset, and maxChildNodes be handled in Mongo instead?
+            // FIXME Should filter, offset, and maxChildNodes be handled in Mongo instead?
             Node rootNode = nodeStore.getNodes(path, revisionId, depth, offset, maxChildNodes, filter);
             if (rootNode == null) {
                 return null;
             }
-            return JsonUtil.convertToJson(rootNode, depth, (int)offset, maxChildNodes, true, nodeFilter);
+
+            JsopBuilder builder = new JsopBuilder().object();
+            JsonUtil.toJson(builder, new MongoNodeState(rootNode), depth, (int)offset, maxChildNodes, true, nodeFilter);
+            return builder.endObject().toString();
         } catch (Exception e) {
             throw new MicroKernelException(e);
         }
     }
 
     @Override
-    public String getRevisionHistory(long since, int maxEntries, String path) throws MicroKernelException {
-        return nodeStore.getRevisionHistory(since, maxEntries, path);
+    public String getRevisionHistory(long since, int maxEntries, String path)
+            throws MicroKernelException {
+        try {
+            return nodeStore.getRevisionHistory(since, maxEntries, path);
+        } catch (Exception e) {
+            throw new MicroKernelException(e);
+        }
     }
 
     @Override
-    public String merge(String branchRevisionId, String message) throws MicroKernelException {
-        throw new UnsupportedOperationException("Merge is currently not supported.");
+    public String merge(String branchRevisionId, String message)
+            throws MicroKernelException {
+        try {
+            return nodeStore.merge(branchRevisionId, message);
+        } catch (Exception e) {
+            throw new MicroKernelException(e);
+        }
     }
 
     @Override
     public boolean nodeExists(String path, String revisionId) throws MicroKernelException {
-        boolean exists = false;
-
         try {
-            String revId = null;
-            if (revisionId != null) {
-                revId = new String(revisionId);
-            }
-
-            exists = nodeStore.nodeExists(path, revId);
+            return nodeStore.nodeExists(path, revisionId);
         } catch (Exception e) {
             throw new MicroKernelException(e);
         }
-
-        return exists;
     }
 
     @Override
-    public int read(String blobId, long pos, byte[] buff, int off, int length) throws MicroKernelException {
-        int totalBytes = -1;
-
+    public int read(String blobId, long pos, byte[] buff, int off, int length)
+            throws MicroKernelException {
         try {
-            totalBytes = blobStore.readBlob(blobId, pos, buff, off, length);
+            return blobStore.readBlob(blobId, pos, buff, off, length);
         } catch (Exception e) {
             throw new MicroKernelException(e);
         }
-
-        return totalBytes;
     }
 
     @Override
-    public String waitForCommit(String oldHeadRevisionId, long timeout) throws MicroKernelException,
-            InterruptedException {
-        return nodeStore.waitForCommit(oldHeadRevisionId, timeout);
+    public String waitForCommit(String oldHeadRevisionId, long timeout)
+            throws MicroKernelException, InterruptedException {
+        try {
+            return nodeStore.waitForCommit(oldHeadRevisionId, timeout);
+        } catch (Exception e) {
+            throw new MicroKernelException(e);
+        }
     }
 
     @Override
     public String write(InputStream in) throws MicroKernelException {
-        String blobId = null;
-
         try {
-            blobId = blobStore.writeBlob(in);
+            return blobStore.writeBlob(in);
         } catch (Exception e) {
             throw new MicroKernelException(e);
         }
-
-        return blobId;
     }
 }
\ No newline at end of file

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoNodeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoNodeStore.java?rev=1407119&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoNodeStore.java (added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoNodeStore.java Thu Nov  8 15:17:50 2012
@@ -0,0 +1,239 @@
+/*
+ * 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.mongomk.impl;
+
+import java.util.Arrays;
+
+import org.apache.jackrabbit.mongomk.api.NodeStore;
+import org.apache.jackrabbit.mongomk.api.command.Command;
+import org.apache.jackrabbit.mongomk.api.command.CommandExecutor;
+import org.apache.jackrabbit.mongomk.api.model.Commit;
+import org.apache.jackrabbit.mongomk.api.model.Node;
+import org.apache.jackrabbit.mongomk.impl.action.FetchCommitAction;
+import org.apache.jackrabbit.mongomk.impl.command.CommitCommand;
+import org.apache.jackrabbit.mongomk.impl.command.DefaultCommandExecutor;
+import org.apache.jackrabbit.mongomk.impl.command.DiffCommand;
+import org.apache.jackrabbit.mongomk.impl.command.GetHeadRevisionCommand;
+import org.apache.jackrabbit.mongomk.impl.command.GetJournalCommand;
+import org.apache.jackrabbit.mongomk.impl.command.GetNodesCommand;
+import org.apache.jackrabbit.mongomk.impl.command.GetRevisionHistoryCommand;
+import org.apache.jackrabbit.mongomk.impl.command.MergeCommand;
+import org.apache.jackrabbit.mongomk.impl.command.NodeExistsCommand;
+import org.apache.jackrabbit.mongomk.impl.command.WaitForCommitCommand;
+import org.apache.jackrabbit.mongomk.impl.model.MongoCommit;
+import org.apache.jackrabbit.mongomk.impl.model.MongoNode;
+import org.apache.jackrabbit.mongomk.impl.model.MongoSync;
+import org.apache.jackrabbit.mongomk.util.MongoUtil;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
+
+/**
+ * Implementation of {@link NodeStore} for the {@code MongoDB}.
+ */
+public class MongoNodeStore implements NodeStore {
+
+    public static final String INITIAL_COMMIT_MESSAGE = "This is an autogenerated initial commit";
+    public static final String INITIAL_COMMIT_PATH = "";
+    public static final String INITIAL_COMMIT_DIFF = "+\"/\" : {}";
+
+    public static final String COLLECTION_COMMITS = "commits";
+    public static final String COLLECTION_NODES = "nodes";
+    public static final String COLLECTION_SYNC = "sync";
+
+    private final CommandExecutor commandExecutor;
+    private final DB db;
+
+    /**
+     * Constructs a new {@code NodeStoreMongo}.
+     *
+     * @param db Mongo DB.
+     */
+    public MongoNodeStore(DB db) {
+        commandExecutor = new DefaultCommandExecutor();
+        this.db = db;
+        init();
+    }
+
+    @Override
+    public String commit(Commit commit) throws Exception {
+        Command<Long> command = new CommitCommand(this, commit);
+        Long revisionId = commandExecutor.execute(command);
+        return MongoUtil.fromMongoRepresentation(revisionId);
+    }
+
+    @Override
+    public String diff(String fromRevision, String toRevision, String path, int depth)
+            throws Exception {
+        Command<String> command = new DiffCommand(this, fromRevision, toRevision, path, depth);
+        return commandExecutor.execute(command);
+    }
+
+    @Override
+    public String getHeadRevision() throws Exception {
+        GetHeadRevisionCommand command = new GetHeadRevisionCommand(this);
+        long revisionId = commandExecutor.execute(command);
+        return MongoUtil.fromMongoRepresentation(revisionId);
+    }
+
+    @Override
+    public Node getNodes(String path, String revisionId, int depth, long offset,
+            int maxChildNodes, String filter) throws Exception {
+        GetNodesCommand command = new GetNodesCommand(this, path,
+                MongoUtil.toMongoRepresentation(revisionId));
+        command.setBranchId(getBranchId(revisionId));
+        command.setDepth(depth);
+        return commandExecutor.execute(command);
+    }
+
+    @Override
+    public String merge(String branchRevisionId, String message) throws Exception {
+        MergeCommand command = new MergeCommand(this, branchRevisionId, message);
+        return commandExecutor.execute(command);
+    }
+
+    @Override
+    public boolean nodeExists(String path, String revisionId) throws Exception {
+        NodeExistsCommand command = new NodeExistsCommand(this, path,
+                MongoUtil.toMongoRepresentation(revisionId));
+        String branchId = getBranchId(revisionId);
+        command.setBranchId(branchId);
+        return commandExecutor.execute(command);
+    }
+
+    @Override
+    public String getJournal(String fromRevisionId, String toRevisionId, String path)
+            throws Exception {
+        GetJournalCommand command = new GetJournalCommand(this, fromRevisionId, toRevisionId, path);
+        return commandExecutor.execute(command);
+    }
+
+    @Override
+    public String getRevisionHistory(long since, int maxEntries, String path)
+            throws Exception {
+        GetRevisionHistoryCommand command = new GetRevisionHistoryCommand(this,
+                since, maxEntries, path);
+        return commandExecutor.execute(command);
+    }
+
+    @Override
+    public String waitForCommit(String oldHeadRevisionId, long timeout) throws Exception {
+        WaitForCommitCommand command = new WaitForCommitCommand(this,
+                oldHeadRevisionId, timeout);
+        long revisionId = commandExecutor.execute(command);
+        return MongoUtil.fromMongoRepresentation(revisionId);
+    }
+
+    /**
+     * Returns the commit {@link DBCollection}.
+     *
+     * @return The commit {@link DBCollection}.
+     */
+    public DBCollection getCommitCollection() {
+        DBCollection commitCollection = db.getCollection(COLLECTION_COMMITS);
+        commitCollection.setObjectClass(MongoCommit.class);
+        return commitCollection;
+    }
+
+    /**
+     * Returns the sync {@link DBCollection}.
+     *
+     * @return The sync {@link DBCollection}.
+     */
+    public DBCollection getSyncCollection() {
+        DBCollection syncCollection = db.getCollection(COLLECTION_SYNC);
+        syncCollection.setObjectClass(MongoSync.class);
+        return syncCollection;
+    }
+
+    /**
+     * Returns the node {@link DBCollection}.
+     *
+     * @return The node {@link DBCollection}.
+     */
+    public DBCollection getNodeCollection() {
+        DBCollection nodeCollection = db.getCollection(COLLECTION_NODES);
+        nodeCollection.setObjectClass(MongoNode.class);
+        return nodeCollection;
+    }
+
+    private void init() {
+        initCommitCollection();
+        initNodeCollection();
+        initSyncCollection();
+    }
+
+    private void initCommitCollection() {
+        if (db.collectionExists(COLLECTION_COMMITS)){
+            return;
+        }
+        DBCollection commitCollection = getCommitCollection();
+        DBObject index = new BasicDBObject();
+        index.put(MongoCommit.KEY_REVISION_ID, 1L);
+        DBObject options = new BasicDBObject();
+        options.put("unique", Boolean.TRUE);
+        commitCollection.ensureIndex(index, options);
+        MongoCommit commit = new MongoCommit();
+        commit.setAffectedPaths(Arrays.asList(new String[] { "/" }));
+        commit.setBaseRevisionId(0L);
+        commit.setDiff(INITIAL_COMMIT_DIFF);
+        commit.setMessage(INITIAL_COMMIT_MESSAGE);
+        commit.setRevisionId(0L);
+        commit.setPath(INITIAL_COMMIT_PATH);
+        commitCollection.insert(commit);
+    }
+
+    private void initNodeCollection() {
+        if (db.collectionExists(COLLECTION_NODES)){
+            return;
+        }
+        DBCollection nodeCollection = getNodeCollection();
+        DBObject index = new BasicDBObject();
+        index.put(MongoNode.KEY_PATH, 1L);
+        index.put(MongoNode.KEY_REVISION_ID, 1L);
+        DBObject options = new BasicDBObject();
+        options.put("unique", Boolean.TRUE);
+        nodeCollection.ensureIndex(index, options);
+        MongoNode root = new MongoNode();
+        root.setRevisionId(0L);
+        root.setPath("/");
+        nodeCollection.insert(root);
+    }
+
+    private void initSyncCollection() {
+        if (db.collectionExists(COLLECTION_SYNC)){
+            return;
+        }
+        DBCollection headCollection = getSyncCollection();
+        MongoSync headMongo = new MongoSync();
+        headMongo.setHeadRevisionId(0L);
+        headMongo.setNextRevisionId(1L);
+        headCollection.insert(headMongo);
+    }
+
+    private String getBranchId(String revisionId) throws Exception {
+        if (revisionId == null) {
+            return null;
+        }
+
+        MongoCommit baseCommit = new FetchCommitAction(this,
+                MongoUtil.toMongoRepresentation(revisionId)).execute();
+        return baseCommit.getBranchId();
+    }
+}
\ No newline at end of file

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoNodeStore.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoNodeStore.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Copied: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/BaseAction.java (from r1407101, jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/AbstractQuery.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/BaseAction.java?p2=jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/BaseAction.java&p1=jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/AbstractQuery.java&r1=1407101&r2=1407119&rev=1407119&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/AbstractQuery.java (original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/BaseAction.java Thu Nov  8 15:17:50 2012
@@ -14,39 +14,33 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.jackrabbit.mongomk.query;
+package org.apache.jackrabbit.mongomk.impl.action;
 
-import org.apache.jackrabbit.mongomk.impl.MongoConnection;
+import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
 
 /**
- * An abstract base class for queries performed with {@code MongoDB}.
+ * An abstract base class for actions performed against {@code MongoDB}.
  *
- * @param <T>
- *            The result type of the query.
- *
- * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ * @param <T> The result type of the query.
  */
-public abstract class AbstractQuery<T> {
+public abstract class BaseAction<T> {
 
-    /** The {@link MongoConnection}. */
-    protected MongoConnection mongoConnection;
+    protected MongoNodeStore nodeStore;
 
     /**
-     * Constructs a new {@code AbstractQuery}.
+     * Constructs a new {@code AbstractAction}.
      *
-     * @param mongoConnection
-     *            The {@link MongoConnection}.
+     * @param nodeStore Node store.
      */
-    protected AbstractQuery(MongoConnection mongoConnection) {
-        this.mongoConnection = mongoConnection;
+    public BaseAction(MongoNodeStore nodeStore) {
+        this.nodeStore = nodeStore;
     }
 
     /**
-     * Executes this query.
+     * Executes this action.
      *
-     * @return The result of the query.
-     * @throws Exception
-     *             If an error occurred while executing the query.
+     * @return The result of the action.
+     * @throws Exception If an error occurred while executing the action.
      */
     public abstract T execute() throws Exception;
 }

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/BaseAction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/BaseAction.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchBranchBaseRevisionIdAction.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchBranchBaseRevisionIdAction.java?rev=1407119&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchBranchBaseRevisionIdAction.java (added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchBranchBaseRevisionIdAction.java Thu Nov  8 15:17:50 2012
@@ -0,0 +1,70 @@
+/*
+ * 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.mongomk.impl.action;
+
+import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
+import org.apache.jackrabbit.mongomk.impl.model.MongoCommit;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBCollection;
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import com.mongodb.QueryBuilder;
+
+/**
+ * An action for fetching the base (trunk) revision id that the branch is based on.
+ */
+public class FetchBranchBaseRevisionIdAction extends BaseAction<Long> {
+
+    private final String branchId;
+
+    /**
+     * Constructs a new {@code FetchBranchBaseRevisionIdAction}.
+     *
+     * @param nodeStore Node store.
+     * @param branchId The branch id. It should not be null.
+     */
+    public FetchBranchBaseRevisionIdAction(MongoNodeStore nodeStore, String branchId) {
+        super(nodeStore);
+        this.branchId = branchId;
+    }
+
+    @Override
+    public Long execute() {
+        if (branchId == null) {
+            throw new IllegalArgumentException("Branch id cannot be null");
+        }
+
+        DBCollection commitCollection = nodeStore.getCommitCollection();
+        QueryBuilder queryBuilder = QueryBuilder.start(MongoCommit.KEY_FAILED)
+                .notEquals(Boolean.TRUE)
+                .and(MongoCommit.KEY_BRANCH_ID).is(branchId);
+        DBObject query = queryBuilder.get();
+
+        BasicDBObject filter = new BasicDBObject();
+        filter.put(MongoCommit.KEY_BASE_REVISION_ID, 1);
+
+        BasicDBObject orderBy = new BasicDBObject(MongoCommit.KEY_BASE_REVISION_ID, 1);
+
+        DBCursor dbCursor = commitCollection.find(query, filter).sort(orderBy).limit(1);
+        if (dbCursor.hasNext()) {
+            MongoCommit commitMongo = (MongoCommit)dbCursor.next();
+            return commitMongo.getBaseRevisionId();
+        }
+        return 0L;
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchBranchBaseRevisionIdAction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchBranchBaseRevisionIdAction.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchCommitAction.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchCommitAction.java?rev=1407119&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchCommitAction.java (added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchCommitAction.java Thu Nov  8 15:17:50 2012
@@ -0,0 +1,64 @@
+/*
+ * 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.mongomk.impl.action;
+
+import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
+import org.apache.jackrabbit.mongomk.impl.model.MongoCommit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
+import com.mongodb.QueryBuilder;
+
+/**
+ * An action for fetching a commit. An exception is thrown if a commit with the
+ * revision id does not exist.
+ */
+public class FetchCommitAction extends BaseAction<MongoCommit> {
+
+    private static final Logger LOG = LoggerFactory.getLogger(FetchCommitAction.class);
+
+    private final long revisionId;
+
+    /**
+     * Constructs a new {@link FetchCommitAction}
+     *
+     * @param nodeStore Node store.
+     * @param revisionId Revision id.
+     */
+    public FetchCommitAction(MongoNodeStore nodeStore, long revisionId) {
+        super(nodeStore);
+        this.revisionId = revisionId;
+    }
+
+    @Override
+    public MongoCommit execute() throws Exception {
+        DBCollection commitCollection = nodeStore.getCommitCollection();
+        DBObject query = QueryBuilder.start(MongoCommit.KEY_FAILED).notEquals(Boolean.TRUE)
+                .and(MongoCommit.KEY_REVISION_ID).is(revisionId)
+                .get();
+
+        LOG.debug(String.format("Executing query: %s", query));
+
+        DBObject dbObject = commitCollection.findOne(query);
+        if (dbObject == null) {
+            throw new Exception(String.format("Commit with revision %d could not be found", revisionId));
+        }
+        return (MongoCommit)dbObject;
+    }
+}
\ No newline at end of file

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchCommitAction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchCommitAction.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchCommitsAction.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchCommitsAction.java?rev=1407119&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchCommitsAction.java (added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchCommitsAction.java Thu Nov  8 15:17:50 2012
@@ -0,0 +1,165 @@
+/*
+ * 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.mongomk.impl.action;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
+import org.apache.jackrabbit.mongomk.impl.model.MongoCommit;
+import org.apache.jackrabbit.mongomk.impl.model.MongoNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBCollection;
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import com.mongodb.QueryBuilder;
+
+/**
+ * An action for fetching valid commits.
+ */
+public class FetchCommitsAction extends BaseAction<List<MongoCommit>> {
+
+    private static final int LIMITLESS = -1;
+    private static final Logger LOG = LoggerFactory.getLogger(FetchCommitsAction.class);
+
+    private long fromRevisionId = -1;
+    private long toRevisionId = -1;
+    private int maxEntries = LIMITLESS;
+    private boolean includeBranchCommits = true;
+
+    /**
+     * Constructs a new {@link FetchCommitsAction}
+     *
+     * @param nodeStore Node store.
+     * @param toRevisionId To revision id.
+     */
+    public FetchCommitsAction(MongoNodeStore nodeStore) {
+        this(nodeStore, -1L, -1L);
+    }
+
+    /**
+     * Constructs a new {@link FetchCommitsAction}
+     *
+     * @param nodeStore Node store.
+     * @param toRevisionId To revision id.
+     */
+    public FetchCommitsAction(MongoNodeStore nodeStore, long toRevisionId) {
+        this(nodeStore, -1L, toRevisionId);
+    }
+
+    /**
+     * Constructs a new {@link FetchCommitsAction}
+     *
+     * @param nodeStore Node store.
+     * @param fromRevisionId From revision id.
+     * @param toRevisionId To revision id.
+     */
+    public FetchCommitsAction(MongoNodeStore nodeStore, long fromRevisionId,
+            long toRevisionId) {
+        super(nodeStore);
+        this.fromRevisionId = fromRevisionId;
+        this.toRevisionId = toRevisionId;
+    }
+
+    /**
+     * Sets the max number of entries that should be fetched.
+     *
+     * @param maxEntries The max number of entries.
+     */
+    public void setMaxEntries(int maxEntries) {
+        this.maxEntries = maxEntries;
+    }
+
+    /**
+     * Sets whether the branch commits are included in the query.
+     *
+     * @param includeBranchCommits Whether the branch commits are included.
+     */
+    public void includeBranchCommits(boolean includeBranchCommits) {
+        this.includeBranchCommits = includeBranchCommits;
+    }
+
+    @Override
+    public List<MongoCommit> execute() {
+        if (maxEntries == 0) {
+            return Collections.emptyList();
+        }
+        DBCursor dbCursor = fetchListOfValidCommits();
+        return convertToCommits(dbCursor);
+    }
+
+    private DBCursor fetchListOfValidCommits() {
+        DBCollection commitCollection = nodeStore.getCommitCollection();
+        QueryBuilder queryBuilder = QueryBuilder.start(MongoCommit.KEY_FAILED).notEquals(Boolean.TRUE);
+        if (toRevisionId > -1) {
+            queryBuilder = queryBuilder.and(MongoCommit.KEY_REVISION_ID).lessThanEquals(toRevisionId);
+        }
+
+        if (!includeBranchCommits) {
+            queryBuilder = queryBuilder.and(new BasicDBObject(MongoNode.KEY_BRANCH_ID,
+                    new BasicDBObject("$exists", false)));
+        }
+
+        DBObject query = queryBuilder.get();
+
+        LOG.debug(String.format("Executing query: %s", query));
+
+        return maxEntries > 0? commitCollection.find(query).limit(maxEntries) : commitCollection.find(query);
+    }
+
+    private List<MongoCommit> convertToCommits(DBCursor dbCursor) {
+        Map<Long, MongoCommit> commits = new HashMap<Long, MongoCommit>();
+        while (dbCursor.hasNext()) {
+            MongoCommit commitMongo = (MongoCommit) dbCursor.next();
+            commits.put(commitMongo.getRevisionId(), commitMongo);
+        }
+
+        List<MongoCommit> validCommits = new LinkedList<MongoCommit>();
+        if (commits.isEmpty()) {
+            return validCommits;
+        }
+
+        Set<Long> revisions = commits.keySet();
+        long currentRevision = (toRevisionId != -1 && revisions.contains(toRevisionId)) ?
+                toRevisionId : Collections.max(revisions);
+
+        while (true) {
+            MongoCommit commitMongo = commits.get(currentRevision);
+            if (commitMongo == null) {
+                break;
+            }
+            validCommits.add(commitMongo);
+            long baseRevision = commitMongo.getBaseRevisionId();
+            if (currentRevision == 0L || baseRevision < fromRevisionId) {
+                break;
+            }
+            currentRevision = baseRevision;
+        }
+
+        LOG.debug(String.format("Found list of valid revisions for max revision %s: %s",
+                toRevisionId, validCommits));
+
+        return validCommits;
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchCommitsAction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchCommitsAction.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchHeadRevisionIdAction.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchHeadRevisionIdAction.java?rev=1407119&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchHeadRevisionIdAction.java (added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchHeadRevisionIdAction.java Thu Nov  8 15:17:50 2012
@@ -0,0 +1,69 @@
+/*
+ * 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.mongomk.impl.action;
+
+import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
+import org.apache.jackrabbit.mongomk.impl.model.MongoCommit;
+import org.apache.jackrabbit.mongomk.impl.model.MongoSync;
+
+import com.mongodb.DBCollection;
+
+/**
+ * An action for fetching the head revision.
+ */
+public class FetchHeadRevisionIdAction extends BaseAction<Long> {
+
+    private boolean includeBranchCommits = true;
+
+    /**
+     * Constructs a new {@code FetchHeadRevisionIdAction}.
+     *
+     * @param nodeStore Node store.
+     */
+    public FetchHeadRevisionIdAction(MongoNodeStore nodeStore) {
+        super(nodeStore);
+    }
+
+    /**
+     * Sets whether the branch commits are included in the query.
+     *
+     * @param includeBranchCommits Whether the branch commits are included.
+     */
+    public void includeBranchCommits(boolean includeBranchCommits) {
+        this.includeBranchCommits = includeBranchCommits;
+    }
+
+    @Override
+    public Long execute() throws Exception {
+        DBCollection headCollection = nodeStore.getSyncCollection();
+        MongoSync syncMongo = (MongoSync)headCollection.findOne();
+        long headRevisionId = syncMongo.getHeadRevisionId();
+        if (includeBranchCommits) {
+            return headRevisionId;
+        }
+
+        // Otherwise, find the first revision id that's not part of a branch.
+        long revisionId = headRevisionId;
+        while (true) {
+            MongoCommit commitMongo = new FetchCommitAction(nodeStore, revisionId).execute();
+            if (commitMongo.getBranchId() == null) {
+                return revisionId;
+            }
+            revisionId = commitMongo.getBaseRevisionId();
+        }
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchHeadRevisionIdAction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchHeadRevisionIdAction.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchNodesAction.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchNodesAction.java?rev=1407119&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchNodesAction.java (added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchNodesAction.java Thu Nov  8 15:17:50 2012
@@ -0,0 +1,226 @@
+/*
+ * 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.mongomk.impl.action;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
+import org.apache.jackrabbit.mongomk.impl.model.MongoCommit;
+import org.apache.jackrabbit.mongomk.impl.model.MongoNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import com.mongodb.QueryBuilder;
+
+/**
+ * An action for fetching nodes.
+ */
+public class FetchNodesAction extends BaseAction<List<MongoNode>> {
+
+    public static final int LIMITLESS_DEPTH = -1;
+    private static final Logger LOG = LoggerFactory.getLogger(FetchNodesAction.class);
+
+    private final Set<String> paths;
+    private final long revisionId;
+
+    private String branchId;
+    private int depth = LIMITLESS_DEPTH;
+    private boolean fetchDescendants;
+
+    /**
+     * Constructs a new {@code FetchNodesAction} to fetch a node and optionally
+     * its descendants under the specified path.
+     *
+     * @param nodeStore Node store.
+     * @param path The path.
+     * @param fetchDescendants Determines whether the descendants of the path
+     * will be fetched as well.
+     * @param revisionId The revision id.
+     */
+    public FetchNodesAction(MongoNodeStore nodeStore, String path,
+            boolean fetchDescendants, long revisionId) {
+        super(nodeStore);
+        paths = new HashSet<String>();
+        paths.add(path);
+        this.fetchDescendants = fetchDescendants;
+        this.revisionId = revisionId;
+    }
+
+    /**
+     * Constructs a new {@code FetchNodesAction} to fetch nodes with the exact
+     * specified paths.
+     *
+     * @param nodeStore Node store.
+     * @param path The exact paths to fetch nodes for.
+     * @param revisionId The revision id.
+     */
+    public FetchNodesAction(MongoNodeStore nodeStore,  Set<String> paths,
+            long revisionId) {
+        super(nodeStore);
+        this.paths = paths;
+        this.revisionId = revisionId;
+    }
+
+    /**
+     * Sets the branchId for the query.
+     *
+     * @param branchId Branch id.
+     */
+    public void setBranchId(String branchId) {
+        this.branchId = branchId;
+    }
+
+    /**
+     * Sets the depth for the command. Only used when fetchDescendants is enabled.
+     *
+     * @param depth The depth for the command or -1 for limitless depth.
+     */
+    public void setDepth(int depth) {
+        this.depth = depth;
+    }
+
+    @Override
+    public List<MongoNode> execute() {
+        if (paths.isEmpty()) {
+            return Collections.emptyList();
+        }
+        DBCursor dbCursor = performQuery();
+        List<MongoCommit> validCommits = new FetchCommitsAction(nodeStore, revisionId).execute();
+        return getMostRecentValidNodes(dbCursor, validCommits);
+    }
+
+    private DBCursor performQuery() {
+        QueryBuilder queryBuilder = QueryBuilder.start(MongoNode.KEY_PATH);
+        if (paths.size() > 1) {
+            queryBuilder = queryBuilder.in(paths);
+        } else {
+            String path = paths.toArray(new String[0])[0];
+            if (fetchDescendants) {
+                Pattern pattern = createPrefixRegExp(path);
+                queryBuilder = queryBuilder.regex(pattern);
+            } else {
+                queryBuilder = queryBuilder.is(path);
+            }
+        }
+
+        if (revisionId > 0) {
+            queryBuilder = queryBuilder.and(MongoNode.KEY_REVISION_ID).lessThanEquals(revisionId);
+        }
+
+        if (branchId == null) {
+            DBObject query = new BasicDBObject(MongoNode.KEY_BRANCH_ID, new BasicDBObject("$exists", false));
+            queryBuilder = queryBuilder.and(query);
+        } else {
+            // Not only return nodes in the branch but also nodes in the trunk
+            // before the branch was created.
+            FetchBranchBaseRevisionIdAction action = new FetchBranchBaseRevisionIdAction(nodeStore, branchId);
+            long headBranchRevisionId = action.execute();
+
+            DBObject branchQuery = QueryBuilder.start().or(
+                    QueryBuilder.start(MongoNode.KEY_BRANCH_ID).is(branchId).get(),
+                    QueryBuilder.start(MongoNode.KEY_REVISION_ID).lessThanEquals(headBranchRevisionId).get()
+            ).get();
+            queryBuilder = queryBuilder.and(branchQuery);
+        }
+
+        DBObject query = queryBuilder.get();
+        LOG.debug(String.format("Executing query: %s", query));
+
+        return nodeStore.getNodeCollection().find(query);
+    }
+
+    private Pattern createPrefixRegExp(String path) {
+        StringBuilder sb = new StringBuilder();
+
+        if (depth < 0) {
+            sb.append("^");
+            sb.append(path);
+        } else if (depth == 0) {
+            sb.append("^");
+            sb.append(path);
+            sb.append("$");
+        } else if (depth > 0) {
+            sb.append("^");
+            if (!"/".equals(path)) {
+                sb.append(path);
+            }
+            sb.append("(/[^/]*)");
+            sb.append("{0,");
+            sb.append(depth);
+            sb.append("}$");
+        }
+
+        return Pattern.compile(sb.toString());
+    }
+
+    private List<MongoNode> getMostRecentValidNodes(DBCursor dbCursor,
+            List<MongoCommit> validCommits) {
+        List<Long> validRevisions = extractRevisionIds(validCommits);
+        Map<String, MongoNode> nodeMongos = new HashMap<String, MongoNode>();
+
+        while (dbCursor.hasNext()) {
+            MongoNode nodeMongo = (MongoNode) dbCursor.next();
+
+            String path = nodeMongo.getPath();
+            long revisionId = nodeMongo.getRevisionId();
+
+            LOG.debug(String.format("Converting node %s (%d)", path, revisionId));
+
+            if (!validRevisions.contains(revisionId)) {
+                LOG.debug(String.format("Node will not be converted as it is not a valid commit %s (%d)",
+                        path, revisionId));
+                continue;
+            }
+
+            MongoNode existingNodeMongo = nodeMongos.get(path);
+            if (existingNodeMongo != null) {
+                long existingRevId = existingNodeMongo.getRevisionId();
+
+                if (revisionId > existingRevId) {
+                    nodeMongos.put(path, nodeMongo);
+                    LOG.debug(String.format("Converted nodes was put into map and replaced %s (%d)", path, revisionId));
+                } else {
+                    LOG.debug(String.format("Converted nodes was not put into map because a newer version"
+                            + " is available %s (%d)", path, revisionId));
+                }
+            } else {
+                nodeMongos.put(path, nodeMongo);
+                LOG.debug("Converted node was put into map");
+            }
+        }
+
+        return new ArrayList<MongoNode>(nodeMongos.values());
+    }
+
+    private List<Long> extractRevisionIds(List<MongoCommit> validCommits) {
+        List<Long> validRevisions = new ArrayList<Long>(validCommits.size());
+        for (MongoCommit commitMongo : validCommits) {
+            validRevisions.add(commitMongo.getRevisionId());
+        }
+        return validRevisions;
+    }
+}
\ No newline at end of file

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchNodesAction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/FetchNodesAction.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Copied: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/ReadAndIncHeadRevisionAction.java (from r1407101, jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/ReadAndIncHeadRevisionQuery.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/ReadAndIncHeadRevisionAction.java?p2=jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/ReadAndIncHeadRevisionAction.java&p1=jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/ReadAndIncHeadRevisionQuery.java&r1=1407101&r2=1407119&rev=1407119&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/ReadAndIncHeadRevisionQuery.java (original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/ReadAndIncHeadRevisionAction.java Thu Nov  8 15:17:50 2012
@@ -14,44 +14,41 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.jackrabbit.mongomk.query;
+package org.apache.jackrabbit.mongomk.impl.action;
 
-import org.apache.jackrabbit.mongomk.impl.MongoConnection;
-import org.apache.jackrabbit.mongomk.model.HeadMongo;
+import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
+import org.apache.jackrabbit.mongomk.impl.model.MongoSync;
 
 import com.mongodb.BasicDBObject;
 import com.mongodb.DBCollection;
 import com.mongodb.DBObject;
 
 /**
- * An query for reading and incrementing the head revisio id.
- *
- * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ * An action for reading and incrementing the head revision id.
  */
-public class ReadAndIncHeadRevisionQuery extends AbstractQuery<HeadMongo> {
+public class ReadAndIncHeadRevisionAction extends BaseAction<MongoSync> {
 
     /**
      * Constructs a new {@code ReadAndIncHeadRevisionQuery}.
      *
-     * @param mongoConnection
-     *            The {@link MongoConnection}.
+     * @param nodeStore Node store.
      */
-    public ReadAndIncHeadRevisionQuery(MongoConnection mongoConnection) {
-        super(mongoConnection);
+    public ReadAndIncHeadRevisionAction(MongoNodeStore nodeStore) {
+        super(nodeStore);
     }
 
     @Override
-    public HeadMongo execute() throws Exception {
+    public MongoSync execute() throws Exception {
         DBObject query = new BasicDBObject();
-        DBObject inc = new BasicDBObject(HeadMongo.KEY_NEXT_REVISION_ID, 1L);
+        DBObject inc = new BasicDBObject(MongoSync.KEY_NEXT_REVISION_ID, 1L);
         DBObject update = new BasicDBObject("$inc", inc);
-        DBCollection headCollection = mongoConnection.getHeadCollection();
+        DBCollection headCollection = nodeStore.getSyncCollection();
 
         DBObject dbObject = headCollection.findAndModify(query, null, null, false, update, true, false);
-        // Not sure why but sometimes dbObject is null. Simply retry for now.
+        // FIXME - Not sure why but sometimes dbObject is null. Simply retry for now.
         while (dbObject == null) {
             dbObject = headCollection.findAndModify(query, null, null, false, update, true, false);
         }
-        return HeadMongo.fromDBObject(dbObject);
+        return MongoSync.fromDBObject(dbObject);
     }
 }

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/ReadAndIncHeadRevisionAction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/ReadAndIncHeadRevisionAction.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Copied: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveAndSetHeadRevisionAction.java (from r1407101, jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveAndSetHeadRevisionQuery.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveAndSetHeadRevisionAction.java?p2=jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveAndSetHeadRevisionAction.java&p1=jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveAndSetHeadRevisionQuery.java&r1=1407101&r2=1407119&rev=1407119&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveAndSetHeadRevisionQuery.java (original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveAndSetHeadRevisionAction.java Thu Nov  8 15:17:50 2012
@@ -14,10 +14,10 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.jackrabbit.mongomk.query;
+package org.apache.jackrabbit.mongomk.impl.action;
 
-import org.apache.jackrabbit.mongomk.impl.MongoConnection;
-import org.apache.jackrabbit.mongomk.model.HeadMongo;
+import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
+import org.apache.jackrabbit.mongomk.impl.model.MongoSync;
 
 import com.mongodb.BasicDBObject;
 import com.mongodb.DBCollection;
@@ -25,43 +25,33 @@ import com.mongodb.DBObject;
 import com.mongodb.QueryBuilder;
 
 /**
- * An query for saveing and setting the head revision id.
- *
- * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ * An action for saving and setting the head revision id.
  */
-public class SaveAndSetHeadRevisionQuery extends AbstractQuery<HeadMongo> {
+public class SaveAndSetHeadRevisionAction extends BaseAction<MongoSync> {
 
     private final long newHeadRevision;
     private final long oldHeadRevision;
 
     /**
-     * Constructs a new {@code SaveAndSetHeadRevisionQuery}.
+     * Constructs a new {@code SaveAndSetHeadRevisionAction}.
      *
-     * @param mongoConnection
-     *            The {@link MongoConnection}.
-     * @param oldHeadRevision
-     * @param newHeadRevision
+     * @param nodeStore Node store.
+     * @param oldHeadRevision Old head revision.
+     * @param newHeadRevision New head revision.
      */
-    public SaveAndSetHeadRevisionQuery(MongoConnection mongoConnection, long oldHeadRevision, long newHeadRevision) {
-        super(mongoConnection);
-
+    public SaveAndSetHeadRevisionAction(MongoNodeStore nodeStore,
+            long oldHeadRevision, long newHeadRevision) {
+        super(nodeStore);
         this.oldHeadRevision = oldHeadRevision;
         this.newHeadRevision = newHeadRevision;
     }
 
     @Override
-    public HeadMongo execute() throws Exception {
-        HeadMongo headMongo = null;
-
-        DBCollection headCollection = mongoConnection.getHeadCollection();
-        DBObject query = QueryBuilder.start(HeadMongo.KEY_HEAD_REVISION_ID).is(oldHeadRevision).get();
-        DBObject update = new BasicDBObject("$set", new BasicDBObject(HeadMongo.KEY_HEAD_REVISION_ID, newHeadRevision));
-
+    public MongoSync execute() throws Exception {
+        DBCollection headCollection = nodeStore.getSyncCollection();
+        DBObject query = QueryBuilder.start(MongoSync.KEY_HEAD_REVISION_ID).is(oldHeadRevision).get();
+        DBObject update = new BasicDBObject("$set", new BasicDBObject(MongoSync.KEY_HEAD_REVISION_ID, newHeadRevision));
         DBObject dbObject = headCollection.findAndModify(query, null, null, false, update, true, false);
-        if (dbObject != null) {
-            headMongo = HeadMongo.fromDBObject(dbObject);
-        }
-
-        return headMongo;
+        return MongoSync.fromDBObject(dbObject);
     }
 }

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveAndSetHeadRevisionAction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveAndSetHeadRevisionAction.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Copied: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveCommitAction.java (from r1407101, jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveCommitQuery.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveCommitAction.java?p2=jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveCommitAction.java&p1=jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveCommitQuery.java&r1=1407101&r2=1407119&rev=1407119&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveCommitQuery.java (original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveCommitAction.java Thu Nov  8 15:17:50 2012
@@ -14,45 +14,39 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.jackrabbit.mongomk.query;
+package org.apache.jackrabbit.mongomk.impl.action;
 
-import org.apache.jackrabbit.mongomk.impl.MongoConnection;
-import org.apache.jackrabbit.mongomk.model.CommitMongo;
+import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
+import org.apache.jackrabbit.mongomk.impl.model.MongoCommit;
 
 import com.mongodb.DBCollection;
 import com.mongodb.WriteResult;
 
 /**
- * An query for saving a commit.
- *
- * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ * An action for saving a commit.
  */
-public class SaveCommitQuery extends AbstractQuery<Boolean> {
+public class SaveCommitAction extends BaseAction<Boolean> {
 
-    private final CommitMongo commitMongo;
+    private final MongoCommit commitMongo;
 
     /**
-     * Constructs a new {@code SaveCommitQuery}.
+     * Constructs a new {@code SaveCommitAction}.
      *
-     * @param mongoConnection
-     *            The {@link MongoConnection}.
-     * @param commitMongo
-     *            The {@link CommitMongo} to save.
+     * @param nodeStore Node store.
+     * @param commitMongo The {@link MongoCommit} to save.
      */
-    public SaveCommitQuery(MongoConnection mongoConnection, CommitMongo commitMongo) {
-        super(mongoConnection);
-
+    public SaveCommitAction(MongoNodeStore nodeStore, MongoCommit commitMongo) {
+        super(nodeStore);
         this.commitMongo = commitMongo;
     }
 
     @Override
     public Boolean execute() throws Exception {
-        DBCollection commitCollection = mongoConnection.getCommitCollection();
+        DBCollection commitCollection = nodeStore.getCommitCollection();
         WriteResult writeResult = commitCollection.insert(commitMongo);
         if (writeResult.getError() != null) {
             throw new Exception(String.format("Insertion wasn't successful: %s", writeResult));
         }
-
         return Boolean.TRUE;
     }
 }

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveCommitAction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveCommitAction.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Copied: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveNodesAction.java (from r1407101, jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveNodesQuery.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveNodesAction.java?p2=jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveNodesAction.java&p1=jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveNodesQuery.java&r1=1407101&r2=1407119&rev=1407119&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveNodesQuery.java (original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveNodesAction.java Thu Nov  8 15:17:50 2012
@@ -14,12 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.jackrabbit.mongomk.query;
+package org.apache.jackrabbit.mongomk.impl.action;
 
 import java.util.Collection;
 
-import org.apache.jackrabbit.mongomk.impl.MongoConnection;
-import org.apache.jackrabbit.mongomk.model.NodeMongo;
+import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
+import org.apache.jackrabbit.mongomk.impl.model.MongoNode;
 
 import com.mongodb.DBCollection;
 import com.mongodb.DBObject;
@@ -27,37 +27,31 @@ import com.mongodb.WriteConcern;
 import com.mongodb.WriteResult;
 
 /**
- * An query for saving a list of nodes.
- *
- * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ * An action for saving a list of nodes.
  */
-public class SaveNodesQuery extends AbstractQuery<Boolean> {
+public class SaveNodesAction extends BaseAction<Boolean> {
 
-    private final Collection<NodeMongo> nodeMongos;
+    private final Collection<MongoNode> nodeMongos;
 
     /**
-     * Constructs a new {@code SaveNodesQuery}.
+     * Constructs a new {@code SaveNodesAction}.
      *
-     * @param mongoConnection
-     *            The {@link MongoConnection}.
-     * @param nodeMongos
-     *            The list of {@link NodeMongo}s.
+     * @param nodeStore Node store.
+     * @param nodeMongos The list of {@link MongoNode}s.
      */
-    public SaveNodesQuery(MongoConnection mongoConnection, Collection<NodeMongo> nodeMongos) {
-        super(mongoConnection);
-
+    public SaveNodesAction(MongoNodeStore nodeStore, Collection<MongoNode> nodeMongos) {
+        super(nodeStore);
         this.nodeMongos = nodeMongos;
     }
 
     @Override
     public Boolean execute() throws Exception {
-        DBCollection nodeCollection = mongoConnection.getNodeCollection();
+        DBCollection nodeCollection = nodeStore.getNodeCollection();
         DBObject[] temp = nodeMongos.toArray(new DBObject[nodeMongos.size()]);
         WriteResult writeResult = nodeCollection.insert(temp, WriteConcern.SAFE);
         if ((writeResult != null) && (writeResult.getError() != null)) {
             throw new Exception(String.format("Insertion wasn't successful: %s", writeResult));
         }
-
         return Boolean.TRUE;
     }
 }

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveNodesAction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/action/SaveNodesAction.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/blob/MongoBlobStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/blob/MongoBlobStore.java?rev=1407119&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/blob/MongoBlobStore.java (added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/blob/MongoBlobStore.java Thu Nov  8 15:17:50 2012
@@ -0,0 +1,160 @@
+/*
+ * 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.mongomk.impl.blob;
+
+import org.apache.jackrabbit.mk.blobs.AbstractBlobStore;
+import org.apache.jackrabbit.mk.blobs.BlobStore;
+import org.apache.jackrabbit.mk.util.StringUtils;
+import org.apache.jackrabbit.mongomk.impl.model.MongoBlob;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
+import com.mongodb.QueryBuilder;
+import com.mongodb.WriteResult;
+
+/**
+ * Implementation of {@link BlobStore} for the {@code MongoDB} extending from
+ * {@link AbstractBlobStore}. Unlike {@link MongoGridFSBlobStore}, it saves blobs
+ * into a separate collection in {@link MongoDB} instead of GridFS.
+ *
+ * FIXME:
+ * -Do we need to create commands for retry etc.?
+ * -Implement GC
+ */
+public class MongoBlobStore extends AbstractBlobStore {
+
+    public static final String COLLECTION_BLOBS = "blobs";
+
+    private final DB db;
+    private long minLastModified;
+
+    /**
+     * Constructs a new {@code MongoBlobStore}
+     *
+     * @param db The DB.
+     */
+    public MongoBlobStore(DB db) {
+        this.db = db;
+        initBlobCollection();
+    }
+
+    @Override
+    protected void storeBlock(byte[] digest, int level, byte[] data) throws Exception {
+        String id = StringUtils.convertBytesToHex(digest);
+        // Check if it already exists?
+        MongoBlob mongoBlob = new MongoBlob();
+        mongoBlob.setId(id);
+        mongoBlob.setData(data);
+        mongoBlob.setLevel(level);
+        getBlobCollection().insert(mongoBlob);
+    }
+
+    @Override
+    protected byte[] readBlockFromBackend(BlockId blockId) throws Exception {
+        String id = StringUtils.convertBytesToHex(blockId.getDigest());
+        MongoBlob blobMongo = getBlob(id, 0);
+        byte[] data = blobMongo.getData();
+
+        if (blockId.getPos() == 0) {
+            return data;
+        }
+
+        int len = (int) (data.length - blockId.getPos());
+        if (len < 0) {
+            return new byte[0];
+        }
+        byte[] d2 = new byte[len];
+        System.arraycopy(data, (int) blockId.getPos(), d2, 0, len);
+        return d2;
+    }
+
+    @Override
+    public void startMark() throws Exception {
+        minLastModified = System.currentTimeMillis();
+        markInUse();
+    }
+
+    @Override
+    protected boolean isMarkEnabled() {
+        return minLastModified != 0;
+    }
+
+    @Override
+    protected void mark(BlockId blockId) throws Exception {
+        if (minLastModified == 0) {
+            return;
+        }
+        String id = StringUtils.convertBytesToHex(blockId.getDigest());
+        DBObject query = getBlobQuery(id, minLastModified);
+        DBObject update = new BasicDBObject("$set",
+                new BasicDBObject(MongoBlob.KEY_LAST_MOD, System.currentTimeMillis()));
+        WriteResult writeResult = getBlobCollection().update(query, update);
+        if (writeResult.getError() != null) {
+            // Handle
+        }
+    }
+
+    @Override
+    public int sweep() throws Exception {
+        DBObject query = getBlobQuery(null, minLastModified);
+        long beforeCount = getBlobCollection().count(query);
+        WriteResult writeResult = getBlobCollection().remove(query);
+        if (writeResult.getError() != null) {
+            // Handle
+        }
+
+        long afterCount = getBlobCollection().count(query);
+        minLastModified = 0;
+        return (int)(beforeCount - afterCount);
+    }
+
+    private DBCollection getBlobCollection() {
+        DBCollection collection = db.getCollection(COLLECTION_BLOBS);
+        collection.setObjectClass(MongoBlob.class);
+        return collection;
+    }
+
+    private void initBlobCollection() {
+        if (db.collectionExists(COLLECTION_BLOBS)) {
+            return;
+        }
+        DBCollection collection = getBlobCollection();
+        DBObject index = new BasicDBObject();
+        index.put(MongoBlob.KEY_ID, 1L);
+        DBObject options = new BasicDBObject();
+        options.put("unique", Boolean.TRUE);
+        collection.ensureIndex(index, options);
+    }
+
+    private MongoBlob getBlob(String id, long lastMod) {
+        DBObject query = getBlobQuery(id, lastMod);
+        return (MongoBlob)getBlobCollection().findOne(query);
+    }
+
+    private DBObject getBlobQuery(String id, long lastMod) {
+        QueryBuilder queryBuilder = new QueryBuilder();
+        if (id != null) {
+            queryBuilder = queryBuilder.and(MongoBlob.KEY_ID).is(id);
+        }
+        if (lastMod > 0) {
+            queryBuilder = queryBuilder.and(MongoBlob.KEY_LAST_MOD).lessThan(lastMod);
+        }
+        return queryBuilder.get();
+    }
+}
\ No newline at end of file

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/blob/MongoBlobStore.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/blob/MongoBlobStore.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Copied: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/blob/MongoGridFsBlobStore.java (from r1407101, jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/BlobStoreMongo.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/blob/MongoGridFsBlobStore.java?p2=jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/blob/MongoGridFsBlobStore.java&p1=jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/BlobStoreMongo.java&r1=1407101&r2=1407119&rev=1407119&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/BlobStoreMongo.java (original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/blob/MongoGridFsBlobStore.java Thu Nov  8 15:17:50 2012
@@ -14,43 +14,55 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.jackrabbit.mongomk.impl;
+package org.apache.jackrabbit.mongomk.impl.blob;
 
 import java.io.InputStream;
 
-import org.apache.jackrabbit.mongomk.api.BlobStore;
+import org.apache.jackrabbit.mk.blobs.BlobStore;
 import org.apache.jackrabbit.mongomk.api.command.Command;
 import org.apache.jackrabbit.mongomk.api.command.CommandExecutor;
-import org.apache.jackrabbit.mongomk.command.GetBlobLengthCommandMongo;
-import org.apache.jackrabbit.mongomk.command.ReadBlobCommandMongo;
-import org.apache.jackrabbit.mongomk.command.WriteBlobCommandMongo;
-import org.apache.jackrabbit.mongomk.impl.command.CommandExecutorImpl;
+import org.apache.jackrabbit.mongomk.impl.command.DefaultCommandExecutor;
+import org.apache.jackrabbit.mongomk.impl.command.blob.GetBlobLengthCommandGridFS;
+import org.apache.jackrabbit.mongomk.impl.command.blob.ReadBlobCommandGridFS;
+import org.apache.jackrabbit.mongomk.impl.command.blob.WriteBlobCommandGridFS;
 
-public class BlobStoreMongo implements BlobStore {
+import com.mongodb.DB;
+import com.mongodb.gridfs.GridFS;
+
+/**
+ * Implementation of {@link BlobStore} for the {@code MongoDB} using GridFS.
+ */
+public class MongoGridFSBlobStore implements BlobStore {
 
-    private final MongoConnection mongoConnection;
     private final CommandExecutor commandExecutor;
+    private final GridFS gridFS;
 
-    public BlobStoreMongo(MongoConnection mongoConnection) {
-        this.mongoConnection = mongoConnection;
-        this.commandExecutor = new CommandExecutorImpl();
+    /**
+     * Constructs a new {@code BlobStoreMongoGridFS}
+     *
+     * @param db The DB.
+     */
+    public MongoGridFSBlobStore(DB db) {
+        commandExecutor = new DefaultCommandExecutor();
+        gridFS = new GridFS(db);
     }
 
     @Override
     public long getBlobLength(String blobId) throws Exception {
-        Command<Long> command = new GetBlobLengthCommandMongo(mongoConnection, blobId);
+        Command<Long> command = new GetBlobLengthCommandGridFS(gridFS, blobId);
         return commandExecutor.execute(command);
     }
 
     @Override
     public int readBlob(String blobId, long blobOffset, byte[] buffer, int bufferOffset, int length) throws Exception {
-        Command<Integer> command = new ReadBlobCommandMongo(mongoConnection, blobId, blobOffset, buffer, bufferOffset, length);
+        Command<Integer> command = new ReadBlobCommandGridFS(gridFS, blobId, blobOffset,
+                buffer, bufferOffset, length);
         return commandExecutor.execute(command);
     }
 
     @Override
     public String writeBlob(InputStream is) throws Exception {
-        Command<String> command = new WriteBlobCommandMongo(mongoConnection, is);
+        Command<String> command = new WriteBlobCommandGridFS(gridFS, is);
         return commandExecutor.execute(command);
     }
 }



Mime
View raw message