You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:56:35 UTC

[sling-org-apache-sling-nosql-mongodb-resourceprovider] 10/15: SLING-5437 add connection check for couchbase resource provider define separate NoSqlAdapter methods for creating index definitions, to ensure they are only executed after connection test succeeds set versions to 1.1.0

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

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

commit 1265b050607757d743565fca3e7afa5dddac01a2
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Tue Jan 19 17:47:07 2016 +0000

    SLING-5437 add connection check for couchbase resource provider
    define separate NoSqlAdapter methods for creating index definitions, to ensure they are only executed after connection test succeeds
    set versions to 1.1.0
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/nosql/mongodb-resourceprovider@1725576 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  4 +-
 .../resourceprovider/impl/MongoDBNoSqlAdapter.java | 56 ++++++++++++----------
 .../integration/IndexCreationIT.java               | 23 ++++-----
 3 files changed, 44 insertions(+), 39 deletions(-)

diff --git a/pom.xml b/pom.xml
index ab83eae..1750693 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
     </parent>
 
     <artifactId>org.apache.sling.nosql.mongodb-resourceprovider</artifactId>
-    <version>1.0.1-SNAPSHOT</version>
+    <version>1.1.0-SNAPSHOT</version>
     <packaging>bundle</packaging>
   
     <name>Apache Sling NoSQL MongoDB Resource Provider</name>
@@ -49,7 +49,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.nosql.generic</artifactId>
-            <version>1.0.1-SNAPSHOT</version>
+            <version>1.1.0-SNAPSHOT</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 ab66fc1..9b993e4 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
@@ -66,32 +66,6 @@ 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(PN_PARENT_PATH, 1);
-            this.collection.createIndex(parenPathtIndex);
-        }
-        catch (DuplicateKeyException ex) {
-            // index already exists, ignore
-        }
-        catch (Throwable ex) {
-            log.error("Unable to create index on " + PN_PARENT_PATH + ": " + ex.getMessage(), ex);
-        }
-        
-        // create unique index on path field (if it does not exist yet)
-        try {
-            Document pathIndex = new Document(PN_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 " + PN_PATH + ": " + ex.getMessage(), ex);
-        }
     }
 
     @Override
@@ -156,4 +130,34 @@ public final class MongoDBNoSqlAdapter extends AbstractNoSqlAdapter {
             throw new LoginException(e);
         }
     }
+
+    @Override
+    public void createIndexDefinitions() {
+        // create index on parent path field (if it does not exist yet)
+        try {
+            Document parenPathtIndex = new Document(PN_PARENT_PATH, 1);
+            this.collection.createIndex(parenPathtIndex);
+        }
+        catch (DuplicateKeyException ex) {
+            // index already exists, ignore
+        }
+        catch (Throwable ex) {
+            log.error("Unable to create index on " + PN_PARENT_PATH + ": " + ex.getMessage(), ex);
+        }
+        
+        // create unique index on path field (if it does not exist yet)
+        try {
+            Document pathIndex = new Document(PN_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 " + PN_PATH + ": " + ex.getMessage(), ex);
+        }
+    }
+
 }
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
index e99ec8f..28f1256 100644
--- 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
@@ -21,7 +21,8 @@ package org.apache.sling.nosql.mongodb.resourceprovider.integration;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
-import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.sling.nosql.mongodb.resourceprovider.impl.MongoDBNoSqlAdapter;
 import org.bson.Document;
@@ -29,6 +30,7 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
 import com.mongodb.MongoClient;
 
 /**
@@ -43,12 +45,14 @@ public class IndexCreationIT {
 	private String collection;
 		
 	@Before
-	public void setUp() {
+	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);
 		underTest = new MongoDBNoSqlAdapter(mongoClient, database, collection);
+		underTest.checkConnection();
+		underTest.createIndexDefinitions();
 	}
 
 	@After
@@ -61,17 +65,14 @@ public class IndexCreationIT {
 	public void testIndexesPresent() {
 		assertNotNull(underTest);
 		
-		//expecting 2 indexes (_id, parentPath)
-		int expected = 2;
-		int actual = 0;
+		final List<String> expectedIndexNames= ImmutableList.<String>of("_id_", "parentPath_1");
 		
-		final String[] expectedIndexesNames=  {"_id_", "parentPath_1"};
-		
-		for (Document d : mongoClient.getDatabase(database).getCollection(collection).listIndexes()){
-			assert Arrays.asList(expectedIndexesNames).contains( d.get("name") );
-			actual++;
+		List<String> actualIndexNames = new ArrayList<String>();
+		for (Document d : mongoClient.getDatabase(database).getCollection(collection).listIndexes()) {
+		    actualIndexNames.add(d.get("name").toString());
 		}
-		assertEquals(expected, actual);
+		
+		assertEquals(expectedIndexNames, actualIndexNames);
 	}
 
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.