jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1386591 [5/7] - in /jackrabbit/oak/trunk: ./ oak-mongomk-api/ oak-mongomk-api/src/ oak-mongomk-api/src/main/ oak-mongomk-api/src/main/java/ oak-mongomk-api/src/main/java/org/ oak-mongomk-api/src/main/java/org/apache/ oak-mongomk-api/src/ma...
Date Mon, 17 Sep 2012 12:54:06 GMT
Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchNodeByPathQuery.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchNodeByPathQuery.java?rev=1386591&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchNodeByPathQuery.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchNodeByPathQuery.java
Mon Sep 17 12:54:01 2012
@@ -0,0 +1,103 @@
+/*
+ * 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.query;
+
+import java.util.List;
+
+import org.apache.jackrabbit.mongomk.MongoConnection;
+import org.apache.jackrabbit.mongomk.model.NodeMongo;
+
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
+import com.mongodb.QueryBuilder;
+
+/**
+ * An query for fetching a node by a given path.
+ *
+ * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ */
+public class FetchNodeByPathQuery extends AbstractQuery<NodeMongo> {
+
+    private final String path;
+    private long revisionId;
+    private boolean fetchAll;
+
+    /**
+     * Constructs a new {@code FetchNodeByPathQuery}.
+     *
+     * @param mongoConnection The {@link MongoConnection}.
+     * @param path The path.
+     */
+    public FetchNodeByPathQuery(MongoConnection mongoConnection, String path) {
+        this(mongoConnection, path, 0);
+    }
+
+    /**
+     * Constructs a new {@code FetchNodeByPathQuery}.
+     *
+     * @param mongoConnection
+     *            The {@link MongoConnection}.
+     * @param path
+     *            The path.
+     * @param revisionId
+     *            The revision id.
+     */
+    public FetchNodeByPathQuery(MongoConnection mongoConnection, String path, long revisionId)
{
+        super(mongoConnection);
+
+        this.path = path;
+        this.revisionId = revisionId;
+    }
+
+    public void setFetchAll(boolean fetchAll) {
+        this.fetchAll = fetchAll;
+    }
+
+    @Override
+    public NodeMongo execute() {
+        if (!revisionIdExists()) {
+            throw new RuntimeException("Revision id '" + revisionId + "' is not valid.");
+        }
+
+        DBCollection nodeCollection = mongoConnection.getNodeCollection();
+        QueryBuilder queryBuilder = QueryBuilder.start(NodeMongo.KEY_PATH).is(path);
+        if (revisionId > 0) {
+            queryBuilder = queryBuilder.and(NodeMongo.KEY_REVISION_ID).is(revisionId);
+        }
+        DBObject query = queryBuilder.get();
+
+        DBObject filter = null;
+        if (!fetchAll) {
+            QueryBuilder filterBuilder = QueryBuilder.start(NodeMongo.KEY_REVISION_ID).is(1);
+            filterBuilder.and(NodeMongo.KEY_CHILDREN).is(1);
+            filter = filterBuilder.get();
+        }
+
+        NodeMongo nodeMongo = (NodeMongo) nodeCollection.findOne(query, filter);
+
+        return nodeMongo;
+    }
+
+    private boolean revisionIdExists() {
+        if (revisionId == 0) {
+            return true;
+        }
+        FetchValidRevisionsQuery query = new FetchValidRevisionsQuery(mongoConnection, String.valueOf(Long.MAX_VALUE));
+        List<Long> revisionIds = query.execute();
+        return revisionIds.contains(revisionId);
+    }
+}

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

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchNodesByPathAndDepthQuery.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchNodesByPathAndDepthQuery.java?rev=1386591&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchNodesByPathAndDepthQuery.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchNodesByPathAndDepthQuery.java
Mon Sep 17 12:54:01 2012
@@ -0,0 +1,120 @@
+/*
+ * 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.query;
+
+import java.util.List;
+import java.util.regex.Pattern;
+
+import org.apache.jackrabbit.mongomk.MongoConnection;
+import org.apache.jackrabbit.mongomk.model.NodeMongo;
+import org.apache.jackrabbit.mongomk.util.MongoUtil;
+import org.apache.log4j.Logger;
+
+import com.mongodb.DBCollection;
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import com.mongodb.QueryBuilder;
+
+/**
+ * An query for fetching nodes by path and depth.
+ *
+ * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ */
+public class FetchNodesByPathAndDepthQuery extends AbstractQuery<List<NodeMongo>>
{
+    private static final Logger LOG = Logger.getLogger(FetchNodesByPathAndDepthQuery.class);
+    private final int depth;
+    private final String path;
+    private final String revisionId;
+
+    /**
+     * Constructs a new {@code FetchNodesByPathAndDepthQuery}.
+     *
+     * @param mongoConnection
+     *            The {@link MongoConnection}.
+     * @param path
+     *            The path.
+     * @param revisionId
+     *            The revision id.
+     * @param depth
+     *            The depth.
+     */
+    public FetchNodesByPathAndDepthQuery(MongoConnection mongoConnection, String path, String
revisionId, int depth) {
+        super(mongoConnection);
+        this.path = path;
+        this.revisionId = revisionId;
+        this.depth = depth;
+    }
+
+    @Override
+    public List<NodeMongo> execute() {
+        Pattern pattern = createPrefixRegExp();
+        List<Long> validRevisions = fetchValidRevisions(mongoConnection, revisionId);
+
+        DBCursor dbCursor = performQuery(pattern);
+        List<NodeMongo> nodes = QueryUtils.convertToNodes(dbCursor, validRevisions);
+
+        return nodes;
+    }
+
+    private Pattern createPrefixRegExp() {
+        StringBuilder sb = new StringBuilder();
+
+        if (depth < 0) {
+            sb.append("^");
+            sb.append(path);
+        }
+        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("}$");
+        }
+
+        Pattern pattern = Pattern.compile(sb.toString());
+
+        return pattern;
+    }
+
+    private List<Long> fetchValidRevisions(MongoConnection mongoConnection, String
revisionId) {
+        return new FetchValidRevisionsQuery(mongoConnection, revisionId).execute();
+    }
+
+    private DBCursor performQuery(Pattern pattern) {
+        DBCollection nodeCollection = mongoConnection.getNodeCollection();
+
+        QueryBuilder qb = QueryBuilder.start(NodeMongo.KEY_PATH).regex(pattern);
+        if (revisionId != null) {
+            qb = qb.and(NodeMongo.KEY_REVISION_ID).lessThanEquals(MongoUtil.toMongoRepresentation(revisionId));
+        }
+
+        DBObject query = qb.get();
+
+        LOG.debug(String.format("Executing query: %s", query));
+
+        DBCursor dbCursor = nodeCollection.find(query);
+
+        return dbCursor;
+    }
+}

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

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchNodesForRevisionQuery.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchNodesForRevisionQuery.java?rev=1386591&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchNodesForRevisionQuery.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchNodesForRevisionQuery.java
Mon Sep 17 12:54:01 2012
@@ -0,0 +1,99 @@
+/*
+ * 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.query;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.jackrabbit.mongomk.MongoConnection;
+import org.apache.jackrabbit.mongomk.model.NodeMongo;
+import org.apache.jackrabbit.mongomk.util.MongoUtil;
+import org.apache.log4j.Logger;
+
+import com.mongodb.DBCollection;
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import com.mongodb.QueryBuilder;
+
+/**
+ * An query for fetching nodes for a specific revision.
+ *
+ * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ */
+public class FetchNodesForRevisionQuery extends AbstractQuery<List<NodeMongo>>
{
+    private static final Logger LOG = Logger.getLogger(FetchNodesForRevisionQuery.class);
+    private final Set<String> paths;
+    private final String revisionId;
+
+    /**
+     * Constructs a new {@code FetchNodesForRevisionQuery}.
+     *
+     * @param mongoConnection
+     *            The {@link MongoConnection}.
+     * @param paths
+     *            The paths to fetch.
+     * @param revisionId
+     *            The revision id.
+     */
+    public FetchNodesForRevisionQuery(MongoConnection mongoConnection, Set<String>
paths, String revisionId) {
+        super(mongoConnection);
+        this.paths = paths;
+        this.revisionId = revisionId;
+    }
+
+    /**
+     * Constructs a new {@code FetchNodesForRevisionQuery}.
+     *
+     * @param mongoConnection
+     *            The {@link MongoConnection}.
+     * @param paths
+     *            The paths to fetch.
+     * @param revisionId
+     *            The revision id.
+     */
+    public FetchNodesForRevisionQuery(MongoConnection mongoConnection, String[] paths, String
revisionId) {
+        this(mongoConnection, new HashSet<String>(Arrays.asList(paths)), revisionId);
+    }
+
+    @Override
+    public List<NodeMongo> execute() {
+        List<Long> validRevisions = fetchValidRevisions(mongoConnection, revisionId);
+
+        DBCursor dbCursor = retrieveAllNodes();
+        List<NodeMongo> nodes = QueryUtils.convertToNodes(dbCursor, validRevisions);
+
+        return nodes;
+    }
+
+    private List<Long> fetchValidRevisions(MongoConnection mongoConnection, String
revisionId) {
+        return new FetchValidRevisionsQuery(mongoConnection, revisionId).execute();
+    }
+
+    private DBCursor retrieveAllNodes() {
+        DBCollection nodeCollection = mongoConnection.getNodeCollection();
+        DBObject query = QueryBuilder.start(NodeMongo.KEY_PATH).in(paths).and(NodeMongo.KEY_REVISION_ID)
+                .lessThanEquals(MongoUtil.toMongoRepresentation(revisionId)).get();
+
+        LOG.debug(String.format("Executing query: %s", query));
+
+        DBCursor dbCursor = nodeCollection.find(query);
+
+        return dbCursor;
+    }
+}

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

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchValidCommitsQuery.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchValidCommitsQuery.java?rev=1386591&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchValidCommitsQuery.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchValidCommitsQuery.java
Mon Sep 17 12:54:01 2012
@@ -0,0 +1,140 @@
+/*
+ * 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.query;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.jackrabbit.mongomk.MongoConnection;
+import org.apache.jackrabbit.mongomk.model.CommitMongo;
+import org.apache.jackrabbit.mongomk.util.MongoUtil;
+import org.apache.log4j.Logger;
+
+import com.mongodb.DBCollection;
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import com.mongodb.QueryBuilder;
+
+/**
+ * An query for fetching valid commits.
+ *
+ * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ */
+public class FetchValidCommitsQuery extends AbstractQuery<List<CommitMongo>>
{
+
+    private static final int LIMITLESS = 0;
+    private static final Logger LOG = Logger.getLogger(FetchValidCommitsQuery.class);
+
+    private final String fromRevisionId;
+    private String toRevisionId;
+    private int maxEntries = LIMITLESS;
+
+    /**
+     * Constructs a new {link FetchValidCommitsQuery}.
+     *
+     * @param mongoConnection Mongo connection.
+     * @param fromRevisionId From revision id.
+     */
+    public FetchValidCommitsQuery(MongoConnection mongoConnection, String toRevisionId) {
+        this(mongoConnection, null, toRevisionId);
+    }
+
+    /**
+     * Constructs a new {@link FetchValidCommitsQuery}
+     *
+     * @param mongoConnection Mongo connection.
+     * @param fromRevisionId From revision id.
+     * @param toRevisionId To revision id.
+     */
+    public FetchValidCommitsQuery(MongoConnection mongoConnection, String fromRevisionId,
+            String toRevisionId) {
+        super(mongoConnection);
+        this.fromRevisionId = fromRevisionId;
+        this.toRevisionId = toRevisionId;
+    }
+
+    /**
+     * FIXME - Maybe this should be removed.
+     *
+     * Constructs a new {@link FetchValidCommitsQuery}.
+     *
+     * @param mongoConnection The {@link MongoConnection}.
+     * @param maxEntries Max number of entries that should be fetched.
+     */
+    public FetchValidCommitsQuery(MongoConnection mongoConnection, int maxEntries) {
+        super(mongoConnection);
+        fromRevisionId = null;
+        toRevisionId = String.valueOf(Integer.MAX_VALUE);
+        this.maxEntries = maxEntries;
+    }
+
+    @Override
+    public List<CommitMongo> execute() {
+        DBCursor dbCursor = fetchListOfValidCommits();
+        List<CommitMongo> commits = convertToCommits(dbCursor);
+
+        return commits;
+    }
+
+    private List<CommitMongo> convertToCommits(DBCursor dbCursor) {
+        Map<Long, CommitMongo> revisions = new HashMap<Long, CommitMongo>();
+        while (dbCursor.hasNext()) {
+            CommitMongo commitMongo = (CommitMongo) dbCursor.next();
+            revisions.put(commitMongo.getRevisionId(), commitMongo);
+        }
+
+        List<CommitMongo> validCommits = new LinkedList<CommitMongo>();
+        if (revisions.isEmpty()) {
+            return validCommits;
+        }
+
+        Long currentRevision = MongoUtil.toMongoRepresentation(toRevisionId);
+        if (!revisions.containsKey(currentRevision)) {
+            currentRevision = Collections.max(revisions.keySet());
+        }
+
+        while (true) {
+            CommitMongo commitMongo = revisions.get(currentRevision);
+            validCommits.add(commitMongo);
+            Long baseRevision = commitMongo.getBaseRevisionId();
+            Long fromRevision = MongoUtil.toMongoRepresentation(fromRevisionId);
+            if ((currentRevision == 0L) || (baseRevision == null || baseRevision < fromRevision))
{
+                break;
+            }
+
+            currentRevision = baseRevision;
+        }
+
+        LOG.debug(String.format("Found list of valid revisions for max revision %s: %s",
toRevisionId, validCommits));
+
+        return validCommits;
+    }
+
+    private DBCursor fetchListOfValidCommits() {
+        DBCollection commitCollection = mongoConnection.getCommitCollection();
+        DBObject query = QueryBuilder.start(CommitMongo.KEY_FAILED).notEquals(Boolean.TRUE)
+                .and(CommitMongo.KEY_REVISION_ID).lessThanEquals(MongoUtil.toMongoRepresentation(toRevisionId))
+                .get();
+
+        LOG.debug(String.format("Executing query: %s", query));
+
+        return commitCollection.find(query).limit(maxEntries);
+    }
+}

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

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchValidRevisionsQuery.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchValidRevisionsQuery.java?rev=1386591&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchValidRevisionsQuery.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/FetchValidRevisionsQuery.java
Mon Sep 17 12:54:01 2012
@@ -0,0 +1,62 @@
+/*
+ * 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.query;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.jackrabbit.mongomk.MongoConnection;
+import org.apache.jackrabbit.mongomk.model.CommitMongo;
+
+
+/**
+ * An query for fetching valid revisions.
+ *
+ * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ */
+public class FetchValidRevisionsQuery extends AbstractQuery<List<Long>> {
+
+    private final String maxRevisionId;
+
+    /**
+     * Constructs a new {@code FetchValidRevisionsQuery}.
+     *
+     * @param mongoConnection
+     *            The {@link MongoConnection}.
+     * @param maxRevisionId
+     *            The max revision id which should be fetched.
+     */
+    public FetchValidRevisionsQuery(MongoConnection mongoConnection, String maxRevisionId)
{
+        super(mongoConnection);
+        this.maxRevisionId = maxRevisionId;
+    }
+
+    @Override
+    public List<Long> execute() {
+        List<CommitMongo> validCommits = fetchValidCommits();
+        List<Long> validRevisions = new ArrayList<Long>(validCommits.size());
+        for (CommitMongo commitMongo : validCommits) {
+            validRevisions.add(commitMongo.getRevisionId());
+        }
+
+        return validRevisions;
+    }
+
+    private List<CommitMongo> fetchValidCommits() {
+        return new FetchValidCommitsQuery(mongoConnection, maxRevisionId).execute();
+    }
+}

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

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/QueryUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/QueryUtils.java?rev=1386591&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/QueryUtils.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/QueryUtils.java
Mon Sep 17 12:54:01 2012
@@ -0,0 +1,90 @@
+/*
+ * 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.query;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.jackrabbit.mongomk.model.NodeMongo;
+import org.apache.log4j.Logger;
+
+import com.mongodb.DBCursor;
+
+/**
+ * Utility class for queries.
+ *
+ * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ */
+public class QueryUtils {
+
+    private static final Logger LOG = Logger.getLogger(QueryUtils.class);
+
+    /**
+     * Reads nodes from the given {@link DBCursor} and add them to the returned list if their
revision id is contained
+     * in the list of given valid revisions. If multiple nodes with the same path are read
by the cursor the one with
+     * the highest revision id will win.
+     *
+     * @param dbCursor
+     *            The {@code DBCursor} to read from.
+     * @param validRevisions
+     *            The list of valid revisions.
+     * @return The list containing the valid nodes.
+     */
+    static List<NodeMongo> convertToNodes(DBCursor dbCursor, List<Long> validRevisions)
{
+        Map<String, NodeMongo> nodeMongos = new HashMap<String, NodeMongo>();
+
+        while (dbCursor.hasNext()) {
+            NodeMongo nodeMongo = (NodeMongo) dbCursor.next();
+
+            String path = nodeMongo.getPath();
+            long revId = nodeMongo.getRevisionId();
+
+            LOG.debug(String.format("Converting node %s (%d)", path, revId));
+
+            if (!validRevisions.contains(revId)) {
+                LOG.debug(String.format("Node will not be converted b/c it is not a valid
commit %s (%d)", path, revId));
+
+                continue;
+            }
+
+            NodeMongo existingNodeMongo = nodeMongos.get(path);
+            if (existingNodeMongo != null) {
+                long existingRevId = existingNodeMongo.getRevisionId();
+
+                if (revId > existingRevId) {
+                    nodeMongos.put(path, nodeMongo);
+                    LOG.debug(String.format("Converted nodes was put into map and replaced
%s (%d)", path, revId));
+                } else {
+                    LOG.debug(String.format(
+                            "Converted nodes was not put into map because a newer version
is available %s (%d)", path,
+                            revId));
+                }
+            } else {
+                nodeMongos.put(path, nodeMongo);
+                LOG.debug("Converted node was put into map");
+            }
+        }
+
+        return new ArrayList<NodeMongo>(nodeMongos.values());
+    }
+
+    private QueryUtils() {
+        // no initialization
+    }
+}

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

Added: 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/query/ReadAndIncHeadRevisionQuery.java?rev=1386591&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/ReadAndIncHeadRevisionQuery.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/ReadAndIncHeadRevisionQuery.java
Mon Sep 17 12:54:01 2012
@@ -0,0 +1,57 @@
+/*
+ * 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.query;
+
+import org.apache.jackrabbit.mongomk.MongoConnection;
+import org.apache.jackrabbit.mongomk.model.HeadMongo;
+
+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>
+ */
+public class ReadAndIncHeadRevisionQuery extends AbstractQuery<HeadMongo> {
+
+    /**
+     * Constructs a new {@code ReadAndIncHeadRevisionQuery}.
+     *
+     * @param mongoConnection
+     *            The {@link MongoConnection}.
+     */
+    public ReadAndIncHeadRevisionQuery(MongoConnection mongoConnection) {
+        super(mongoConnection);
+    }
+
+    @Override
+    public HeadMongo execute() throws Exception {
+        DBObject query = new BasicDBObject();
+        DBObject inc = new BasicDBObject(HeadMongo.KEY_NEXT_REVISION_ID, Long.valueOf(1));
+        DBObject update = new BasicDBObject("$inc", inc);
+        DBCollection headCollection = mongoConnection.getHeadCollection();
+
+        DBObject dbObject = headCollection.findAndModify(query, null, null, false, update,
true, false);
+        // 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);
+    }
+}

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

Added: 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/query/SaveAndSetHeadRevisionQuery.java?rev=1386591&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveAndSetHeadRevisionQuery.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveAndSetHeadRevisionQuery.java
Mon Sep 17 12:54:01 2012
@@ -0,0 +1,67 @@
+/*
+ * 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.query;
+
+import org.apache.jackrabbit.mongomk.MongoConnection;
+import org.apache.jackrabbit.mongomk.model.HeadMongo;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBCollection;
+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>
+ */
+public class SaveAndSetHeadRevisionQuery extends AbstractQuery<HeadMongo> {
+
+    private final long newHeadRevision;
+    private final long oldHeadRevision;
+
+    /**
+     * Constructs a new {@code SaveAndSetHeadRevisionQuery}.
+     *
+     * @param mongoConnection
+     *            The {@link MongoConnection}.
+     * @param oldHeadRevision
+     * @param newHeadRevision
+     */
+    public SaveAndSetHeadRevisionQuery(MongoConnection mongoConnection, long oldHeadRevision,
long newHeadRevision) {
+        super(mongoConnection);
+
+        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));
+
+        DBObject dbObject = headCollection.findAndModify(query, null, null, false, update,
true, false);
+        if (dbObject != null) {
+            headMongo = HeadMongo.fromDBObject(dbObject);
+        }
+
+        return headMongo;
+    }
+}

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

Added: 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/query/SaveCommitQuery.java?rev=1386591&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveCommitQuery.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveCommitQuery.java
Mon Sep 17 12:54:01 2012
@@ -0,0 +1,58 @@
+/*
+ * 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.query;
+
+import org.apache.jackrabbit.mongomk.MongoConnection;
+import org.apache.jackrabbit.mongomk.model.CommitMongo;
+
+import com.mongodb.DBCollection;
+import com.mongodb.WriteResult;
+
+/**
+ * An query for saving a commit.
+ *
+ * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ */
+public class SaveCommitQuery extends AbstractQuery<Boolean> {
+
+    private final CommitMongo commitMongo;
+
+    /**
+     * Constructs a new {@code SaveCommitQuery}.
+     *
+     * @param mongoConnection
+     *            The {@link MongoConnection}.
+     * @param commitMongo
+     *            The {@link CommitMongo} to save.
+     */
+    public SaveCommitQuery(MongoConnection mongoConnection, CommitMongo commitMongo) {
+        super(mongoConnection);
+
+        this.commitMongo = commitMongo;
+    }
+
+    @Override
+    public Boolean execute() throws Exception {
+        DBCollection commitCollection = mongoConnection.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/query/SaveCommitQuery.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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/query/SaveNodesQuery.java?rev=1386591&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveNodesQuery.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/query/SaveNodesQuery.java
Mon Sep 17 12:54:01 2012
@@ -0,0 +1,63 @@
+/*
+ * 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.query;
+
+import java.util.Collection;
+
+import org.apache.jackrabbit.mongomk.MongoConnection;
+import org.apache.jackrabbit.mongomk.model.NodeMongo;
+
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
+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>
+ */
+public class SaveNodesQuery extends AbstractQuery<Boolean> {
+
+    private final Collection<NodeMongo> nodeMongos;
+
+    /**
+     * Constructs a new {@code SaveNodesQuery}.
+     *
+     * @param mongoConnection
+     *            The {@link MongoConnection}.
+     * @param nodeMongos
+     *            The list of {@link NodeMongo}s.
+     */
+    public SaveNodesQuery(MongoConnection mongoConnection, Collection<NodeMongo> nodeMongos)
{
+        super(mongoConnection);
+
+        this.nodeMongos = nodeMongos;
+    }
+
+    @Override
+    public Boolean execute() throws Exception {
+        DBCollection nodeCollection = mongoConnection.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/query/SaveNodesQuery.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/util/MongoUtil.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/util/MongoUtil.java?rev=1386591&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/util/MongoUtil.java
(added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/util/MongoUtil.java
Mon Sep 17 12:54:01 2012
@@ -0,0 +1,109 @@
+/*
+ * 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.util;
+
+import java.util.Arrays;
+
+import org.apache.jackrabbit.mongomk.MongoConnection;
+import org.apache.jackrabbit.mongomk.model.CommitMongo;
+import org.apache.jackrabbit.mongomk.model.HeadMongo;
+import org.apache.jackrabbit.mongomk.model.NodeMongo;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
+
+/**
+ * Utility classes for preparing the {@code MongoDB} environement.
+ *
+ * @author <a href="mailto:pmarx@adobe.com>Philipp Marx</a>
+ */
+@SuppressWarnings("javadoc")
+public class MongoUtil {
+
+    public static void clearCommitCollection(MongoConnection mongoConnection) {
+        DBCollection commitCollection = mongoConnection.getCommitCollection();
+        commitCollection.drop();
+    }
+
+    public static void clearDatabase(MongoConnection mongoConnection) {
+        clearNodeCollection(mongoConnection);
+        clearCommitCollection(mongoConnection);
+        clearHeadCollection(mongoConnection);
+    }
+
+    public static void clearHeadCollection(MongoConnection mongoConnection) {
+        DBCollection headCollection = mongoConnection.getHeadCollection();
+        headCollection.drop();
+    }
+
+    public static void clearNodeCollection(MongoConnection mongoConnection) {
+        DBCollection nodeCollection = mongoConnection.getNodeCollection();
+        nodeCollection.drop();
+    }
+
+    public static void initCommitCollection(MongoConnection mongoConnection) {
+        DBCollection commitCollection = mongoConnection.getCommitCollection();
+        DBObject index = new BasicDBObject();
+        index.put(CommitMongo.KEY_REVISION_ID, Long.valueOf(1));
+        DBObject options = new BasicDBObject();
+        options.put("unique", Boolean.TRUE);
+        commitCollection.ensureIndex(index, options);
+        CommitMongo commit = new CommitMongo();
+        commit.setAffectedPaths(Arrays.asList(new String[] { "/" }));
+        commit.setBaseRevId(0L);
+        commit.setDiff("+/ : {}");
+        commit.setMessage("This is an autogenerated initial commit");
+        commit.setPath("");
+        commit.setRevisionId(0L);
+        commitCollection.insert(commit);
+    }
+
+    public static void initDatabase(MongoConnection mongoConnection) {
+        clearDatabase(mongoConnection);
+
+        initNodeCollection(mongoConnection);
+        initCommitCollection(mongoConnection);
+        initHeadCollection(mongoConnection);
+    }
+
+    public static void initHeadCollection(MongoConnection mongoConnection) {
+        DBCollection headCollection = mongoConnection.getHeadCollection();
+        HeadMongo headMongo = new HeadMongo();
+        headMongo.setHeadRevisionId(0L);
+        headMongo.setNextRevisionId(1L);
+        headCollection.insert(headMongo);
+    }
+
+    public static void initNodeCollection(MongoConnection mongoConnection) {
+        DBCollection nodeCollection = mongoConnection.getNodeCollection();
+        DBObject index = new BasicDBObject();
+        index.put(NodeMongo.KEY_PATH, Long.valueOf(1));
+        index.put(NodeMongo.KEY_REVISION_ID, Long.valueOf(1));
+        DBObject options = new BasicDBObject();
+        options.put("unique", Boolean.TRUE);
+        nodeCollection.ensureIndex(index, options);
+        NodeMongo root = new NodeMongo();
+        root.setRevisionId(0L);
+        root.setPath("/");
+        nodeCollection.insert(root);
+    }
+
+    public static Long toMongoRepresentation(String revisionId) {
+        return revisionId != null? Long.parseLong(revisionId) : 0L;
+    }
+}

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



Mime
View raw message