Author: mreutegg
Date: Tue Feb 2 12:31:40 2016
New Revision: 1728102
URL: http://svn.apache.org/viewvc?rev=1728102&view=rev
Log:
OAK-3970: Utility methods for MongoDB indexes
Added:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtils.java
(with props)
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java
(with props)
Added:
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=1728102&view=auto
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtils.java
(added)
+++
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtils.java
Tue Feb 2 12:31:40 2016
@@ -0,0 +1,111 @@
+/*
+ * 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.oak.plugins.document.mongo;
+
+import java.util.Set;
+
+import com.google.common.collect.Sets;
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
+import com.mongodb.MongoException;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * Provides static utility methods for MongoDB.
+ */
+class MongoUtils {
+
+ /**
+ * Forces creation of an index on a field, if one does not already exist.
+ *
+ * @param collection the collection.
+ * @param field the name of the field.
+ * @param ascending {@code true} for an ascending, {@code false} for a
+ * descending index.
+ * @param unique whether values are unique.
+ * @param sparse whether the index should be sparse.
+ * @throws MongoException if the operation fails.
+ */
+ static void createIndex(DBCollection collection,
+ String field,
+ boolean ascending,
+ boolean unique,
+ boolean sparse)
+ throws MongoException {
+ createIndex(collection, new String[]{field},
+ new boolean[]{ascending}, unique, sparse);
+ }
+
+ /**
+ * Forces creation of an 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 unique whether values are unique.
+ * @param sparse whether the index should be sparse.
+ * @throws IllegalArgumentException if {@code fields} and {@code ascending}
+ * arrays have different lengths.
+ * @throws MongoException if the operation fails.
+ */
+ static void createIndex(DBCollection collection,
+ String[] fields,
+ boolean[] ascending,
+ boolean unique,
+ boolean sparse)
+ 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("unique", unique);
+ options.put("sparse", sparse);
+ 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
+ * check the sequence of fields for a compound index. That is, this method
+ * will return {@code true} as soon as it finds an index that covers the
+ * given fields, no matter their sequence in the compound index.
+ *
+ * @param collection the collection.
+ * @param fields the fields of an index.
+ * @return {@code true} if the index exists, {@code false} otherwise.
+ * @throws MongoException if the operation fails.
+ */
+ static boolean hasIndex(DBCollection collection, String... fields)
+ throws MongoException {
+ Set<String> uniqueFields = Sets.newHashSet(fields);
+ for (DBObject info : collection.getIndexInfo()) {
+ DBObject key = (DBObject) info.get("key");
+ Set<String> indexFields = Sets.newHashSet(key.keySet());
+ if (uniqueFields.equals(indexFields)) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
Propchange:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
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=1728102&view=auto
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java
(added)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java
Tue Feb 2 12:31:40 2016
@@ -0,0 +1,87 @@
+/*
+ * 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.oak.plugins.document.mongo;
+
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
+
+import org.apache.jackrabbit.oak.plugins.document.MongoConnectionFactory;
+import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+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.assertTrue;
+import static org.junit.Assume.assumeTrue;
+
+public class MongoUtilsTest {
+
+ @Rule
+ public MongoConnectionFactory connectionFactory = new
MongoConnectionFactory();
+
+ @BeforeClass
+ public static void mongoAvailable() {
+ assumeTrue(isAvailable());
+ }
+
+ @Test
+ public void createIndex() {
+ MongoConnection c = connectionFactory.getConnection();
+ c.getDB().dropDatabase();
+ DBCollection collection = c.getDB().getCollection("test");
+ MongoUtils.createIndex(collection, "foo", true, false, true);
+ MongoUtils.createIndex(collection, "bar", false, true, false);
+ MongoUtils.createIndex(collection, new String[]{"baz", "qux"},
+ new boolean[]{true, false}, false, false);
+
+ assertTrue(MongoUtils.hasIndex(collection, "_id"));
+ assertTrue(MongoUtils.hasIndex(collection, "foo"));
+ assertFalse(MongoUtils.hasIndex(collection, "foo", "bar"));
+ assertTrue(MongoUtils.hasIndex(collection, "baz", "qux"));
+
+ assertEquals(4, collection.getIndexInfo().size());
+ for (DBObject info : collection.getIndexInfo()) {
+ DBObject key = (DBObject) info.get("key");
+ if (key.keySet().contains("foo")) {
+ assertEquals(1, key.keySet().size());
+ assertEquals(1, key.get("foo"));
+ assertEquals(Boolean.TRUE, info.get("sparse"));
+ } else if (key.keySet().contains("bar")) {
+ assertEquals(1, key.keySet().size());
+ assertEquals(-1, key.get("bar"));
+ assertEquals(Boolean.TRUE, info.get("unique"));
+ } else if (key.keySet().contains("baz")) {
+ assertEquals(2, key.keySet().size());
+ assertEquals(1, key.get("baz"));
+ assertEquals(-1, key.get("qux"));
+ }
+ }
+
+ c.getDB().dropDatabase();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void checkArguments() {
+ MongoConnection c = connectionFactory.getConnection();
+ DBCollection collection = c.getDB().getCollection("test");
+ MongoUtils.createIndex(collection, new String[]{"foo", "bar"},
+ new boolean[]{true}, false, true);
+ }
+}
Propchange:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java
------------------------------------------------------------------------------
svn:eol-style = native