This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-nosql-mongodb-resourceprovider.git

commit a147a8ead56a974966ce9a5df9dd33791203b238
Author: Stefan Seifert <[email protected]>
AuthorDate: Tue Nov 24 22:39:37 2015 +0000

    SLING-5078 Missing indexes on MongoDBResourceProvider (patch supplied by 
Norberto Leite)
    update path to ignore error when index already exists - log error if it 
could not be created
    do some refactoring on provided integration test to make sure the mongodb 
connection parameters passed via command line are used
    update to latest table mongodb java driver
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1716273 
13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  2 +-
 .../resourceprovider/impl/MongoDBNoSqlAdapter.java | 32 ++++++++++
 .../integration/IndexCreationIT.java               | 73 ++++++++++++++++++++++
 3 files changed, 106 insertions(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index eef1c26..d3fa27f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -56,7 +56,7 @@
         <dependency>
             <groupId>org.mongodb</groupId>
             <artifactId>mongo-java-driver</artifactId>
-            <version>3.0.4</version>
+            <version>3.1.1</version>
             <scope>provided</scope>
         </dependency>
 
diff --git 
a/src/main/java/org/apache/sling/nosql/mongodb/resourceprovider/impl/MongoDBNoSqlAdapter.java
 
b/src/main/java/org/apache/sling/nosql/mongodb/resourceprovider/impl/MongoDBNoSqlAdapter.java
index a25abd5..782021e 100644
--- 
a/src/main/java/org/apache/sling/nosql/mongodb/resourceprovider/impl/MongoDBNoSqlAdapter.java
+++ 
b/src/main/java/org/apache/sling/nosql/mongodb/resourceprovider/impl/MongoDBNoSqlAdapter.java
@@ -28,13 +28,17 @@ import 
org.apache.sling.nosql.generic.adapter.AbstractNoSqlAdapter;
 import org.apache.sling.nosql.generic.adapter.MultiValueMode;
 import org.apache.sling.nosql.generic.adapter.NoSqlData;
 import org.bson.Document;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
+import com.mongodb.DuplicateKeyException;
 import com.mongodb.MongoClient;
 import com.mongodb.client.FindIterable;
 import com.mongodb.client.MongoCollection;
 import com.mongodb.client.MongoCursor;
 import com.mongodb.client.MongoDatabase;
 import com.mongodb.client.model.Filters;
+import com.mongodb.client.model.IndexOptions;
 import com.mongodb.client.model.UpdateOptions;
 import com.mongodb.client.result.DeleteResult;
 import com.mongodb.client.result.UpdateResult;
@@ -50,6 +54,8 @@ public final class MongoDBNoSqlAdapter extends 
AbstractNoSqlAdapter {
     
     private final MongoCollection<Document> collection;
     
+    private static final Logger log = 
LoggerFactory.getLogger(MongoDBNoSqlAdapter.class);
+    
     /**
      * @param mongoClient MongoDB client
      * @param database MongoDB database
@@ -58,6 +64,32 @@ public final class MongoDBNoSqlAdapter extends 
AbstractNoSqlAdapter {
     public MongoDBNoSqlAdapter(MongoClient mongoClient, String database, 
String collection) {
         MongoDatabase db = mongoClient.getDatabase(database);
         this.collection = db.getCollection(collection);
+        
+        // create index on parent path field (if it does not exist yet)
+        try {
+            Document parenPathtIndex = new Document("_parentPath", 1);
+            this.collection.createIndex(parenPathtIndex);
+        }
+        catch (DuplicateKeyException ex) {
+            // index already exists, ignore
+        }
+        catch (Throwable ex) {
+            log.error("Unable to create index on _parentPath: " + 
ex.getMessage(), ex);
+        }
+        
+        // create unique index on path field (if it does not exist yet)
+        try {
+            Document pathIndex = new Document("_path", 1);
+            IndexOptions idxOptions = new IndexOptions();
+            idxOptions.unique(true);
+            this.collection.createIndex(pathIndex, idxOptions);
+        }
+        catch (DuplicateKeyException ex) {
+            // index already exists, ignore
+        }
+        catch (Throwable ex) {
+            log.error("Unable to create unique index on _path: " + 
ex.getMessage(), ex);
+        }
     }
 
     @Override
diff --git 
a/src/test/java/org/apache/sling/nosql/mongodb/resourceprovider/integration/IndexCreationIT.java
 
b/src/test/java/org/apache/sling/nosql/mongodb/resourceprovider/integration/IndexCreationIT.java
new file mode 100644
index 0000000..3ec88fe
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/nosql/mongodb/resourceprovider/integration/IndexCreationIT.java
@@ -0,0 +1,73 @@
+/*
+ * 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.sling.nosql.mongodb.resourceprovider.integration;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Arrays;
+
+import 
org.apache.sling.nosql.mongodb.resourceprovider.impl.MongoDBNoSqlAdapter;
+import org.bson.Document;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.mongodb.MongoClient;
+
+public class IndexCreationIT {
+
+       private MongoDBNoSqlAdapter obj;
+       
+       private MongoClient mongoClient;
+       private String database;
+       private String collection;
+               
+       @Before
+       public void setUp() throws Exception {
+           String connectionString = System.getProperty("connectionString", 
"localhost:27017");
+        database =  System.getProperty("database", "sling") + "_indextest";
+        collection = System.getProperty("collection", "resources");
+               mongoClient = new MongoClient(connectionString);
+               obj = new MongoDBNoSqlAdapter(mongoClient, database, 
collection);
+       }
+
+       @After
+       public void tearDown() throws Exception {
+               mongoClient.dropDatabase(database);
+               mongoClient.close();
+       }
+
+       @Test
+       public void testIndexesPresent() {
+               assertNotNull(obj);
+               
+               //expecting at least 3 indexes (_id, _path, _parentPath)
+               int expected = 3;
+               int actual = 0;
+               
+               final String[] expectedIndexesNames=  {"_id_", "_path_1", 
"_parentPath_1"};
+               
+               for( Document d : 
mongoClient.getDatabase(database).getCollection(collection).listIndexes()){
+                       assert Arrays.asList(expectedIndexesNames).contains( 
d.get("name") );
+                       actual++;
+               }
+               assert expected == actual;
+       }
+
+}

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to