You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by mr...@apache.org on 2017/04/26 16:05:46 UTC

svn commit: r1792760 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/document/mongo/ test/java/org/apache/jackrabbit/oak/plugins/document/mongo/

Author: mreutegg
Date: Wed Apr 26 16:05:46 2017
New Revision: 1792760

URL: http://svn.apache.org/viewvc?rev=1792760&view=rev
Log:
OAK-5989: Create a partial index on _deletedOnce instead of sparse index

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatus.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtils.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java?rev=1792760&r1=1792759&r2=1792760&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java Wed Apr 26 16:05:46 2017
@@ -108,9 +108,12 @@ import static com.google.common.collect.
 import static com.google.common.collect.Maps.filterKeys;
 import static com.google.common.collect.Maps.filterValues;
 import static com.google.common.collect.Sets.difference;
+import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.DELETED_ONCE;
+import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.MODIFIED_IN_SECS;
 import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.SD_MAX_REV_TIME_IN_SECS;
 import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.SD_TYPE;
 import static org.apache.jackrabbit.oak.plugins.document.mongo.MongoUtils.createIndex;
+import static org.apache.jackrabbit.oak.plugins.document.mongo.MongoUtils.createPartialIndex;
 import static org.apache.jackrabbit.oak.plugins.document.mongo.MongoUtils.hasIndex;
 
 /**
@@ -277,7 +280,14 @@ public class MongoDocumentStore implemen
         createIndex(nodes, NodeDocument.HAS_BINARY_FLAG, true, false, true);
 
         // index on _deleted for fast lookup of potentially garbage
-        createIndex(nodes, NodeDocument.DELETED_ONCE, true, false, true);
+        // depending on the MongoDB version, create a partial index
+        if (mongoStatus.isVersion(3, 2)
+                && initialDocsCount == 0) {
+            createPartialIndex(nodes, new String[]{DELETED_ONCE, MODIFIED_IN_SECS},
+                    new boolean[]{true, true}, "{" + DELETED_ONCE + ":true}");
+        } else {
+            createIndex(nodes, NodeDocument.DELETED_ONCE, true, false, true);
+        }
 
         // compound index on _sdType and _sdMaxRevTime
         if (initialDocsCount == 0) {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatus.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatus.java?rev=1792760&r1=1792759&r2=1792760&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatus.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatus.java Wed Apr 26 16:05:46 2017
@@ -141,7 +141,7 @@ public class MongoStatus {
         return version;
     }
 
-    private boolean isVersion(int requiredMajor, int requiredMinor) {
+    boolean isVersion(int requiredMajor, int requiredMinor) {
         String v = getVersion();
         Matcher m = Pattern.compile("^(\\d+)\\.(\\d+)\\..*").matcher(v);
         if (!m.matches()) {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtils.java?rev=1792760&r1=1792759&r2=1792760&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtils.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtils.java Wed Apr 26 16:05:46 2017
@@ -84,6 +84,31 @@ class MongoUtils {
     }
 
     /**
+     * Forces creation of a partial index on a set of fields, if one does not
+     * already exist.
+     *
+     * @param collection the collection.
+     * @param fields the name of the fields.
+     * @param ascending {@code true} for an ascending, {@code false} for a
+     *                              descending index.
+     * @param filter the filter expression for the partial index.
+     * @throws MongoException if the operation fails.
+     */
+    static void createPartialIndex(DBCollection collection,
+                                   String[] fields,
+                                   boolean[] ascending,
+                                   String filter) throws MongoException {
+        checkArgument(fields.length == ascending.length);
+        DBObject index = new BasicDBObject();
+        for (int i = 0; i < fields.length; i++) {
+            index.put(fields[i], ascending[i] ? 1 : -1);
+        }
+        DBObject options = new BasicDBObject();
+        options.put("partialFilterExpression", BasicDBObject.parse(filter));
+        collection.createIndex(index, options);
+    }
+
+    /**
      * Returns {@code true} if there is an index on the given fields,
      * {@code false} otherwise. If multiple fields are passed, this method
      * check if there a compound index on those field. This method does not

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java?rev=1792760&r1=1792759&r2=1792760&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java Wed Apr 26 16:05:46 2017
@@ -53,7 +53,11 @@ public class MongoDocumentStoreTest exte
         assertTrue(hasIndex(store.getDBCollection(Collection.NODES), Document.ID));
         assertFalse(hasIndex(store.getDBCollection(Collection.NODES), NodeDocument.SD_TYPE));
         assertTrue(hasIndex(store.getDBCollection(Collection.NODES), NodeDocument.SD_TYPE, NodeDocument.SD_MAX_REV_TIME_IN_SECS));
-        assertTrue(hasIndex(store.getDBCollection(Collection.NODES), NodeDocument.DELETED_ONCE));
+        if (new MongoStatus(mongoConnection.getDB()).isVersion(3, 2)) {
+            assertTrue(hasIndex(store.getDBCollection(Collection.NODES), NodeDocument.DELETED_ONCE, NodeDocument.MODIFIED_IN_SECS));
+        } else {
+            assertTrue(hasIndex(store.getDBCollection(Collection.NODES), NodeDocument.DELETED_ONCE));
+        }
         assertTrue(hasIndex(store.getDBCollection(Collection.NODES), NodeDocument.HAS_BINARY_FLAG));
         assertTrue(hasIndex(store.getDBCollection(Collection.NODES), NodeDocument.MODIFIED_IN_SECS, Document.ID));
         assertFalse(hasIndex(store.getDBCollection(Collection.NODES), NodeDocument.MODIFIED_IN_SECS));

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java?rev=1792760&r1=1792759&r2=1792760&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java Wed Apr 26 16:05:46 2017
@@ -28,6 +28,7 @@ import org.junit.Test;
 import static org.apache.jackrabbit.oak.plugins.document.MongoUtils.isAvailable;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assume.assumeTrue;
 
@@ -74,6 +75,37 @@ public class MongoUtilsTest {
             }
         }
 
+        c.getDB().dropDatabase();
+    }
+
+    @Test
+    public void createPartialIndex() {
+        MongoConnection c = connectionFactory.getConnection();
+        c.getDB().dropDatabase();
+        MongoStatus status = new MongoStatus(c.getDB());
+        assumeTrue(status.isVersion(3, 2));
+
+        DBCollection collection = c.getDB().getCollection("test");
+
+        MongoUtils.createPartialIndex(collection, new String[]{"foo", "bar"},
+                new boolean[]{true, true}, "{foo:true}");
+        assertTrue(MongoUtils.hasIndex(collection, "_id"));
+        assertTrue(MongoUtils.hasIndex(collection, "foo", "bar"));
+
+        assertEquals(2, collection.getIndexInfo().size());
+        for (DBObject info : collection.getIndexInfo()) {
+            DBObject key = (DBObject) info.get("key");
+            assertNotNull(key);
+            if (key.keySet().contains("foo")) {
+                assertEquals(2, key.keySet().size());
+                assertEquals(1, key.get("foo"));
+                assertEquals(1, key.get("bar"));
+                DBObject filter = (DBObject) info.get("partialFilterExpression");
+                assertNotNull(filter);
+                assertEquals(Boolean.TRUE, filter.get("foo"));
+            }
+        }
+
         c.getDB().dropDatabase();
     }