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/10/18 23:26:39 UTC

[sling-org-apache-sling-nosql-couchbase-resourceprovider] 33/42: 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 branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-nosql-couchbase-resourceprovider.git

commit 0e85925feee2725657be395e9b7a628ff59bf328
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@1725576 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  4 +--
 .../impl/CouchbaseNoSqlAdapter.java                | 36 +++++++++++++++++++---
 2 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/pom.xml b/pom.xml
index 0355b76..e8e074f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
     </parent>
 
     <artifactId>org.apache.sling.nosql.couchbase-resourceprovider</artifactId>
-    <version>1.0.1-SNAPSHOT</version>
+    <version>1.1.0-SNAPSHOT</version>
     <packaging>bundle</packaging>
   
     <name>Apache Sling NoSQL Couchbase Resource Provider</name>
@@ -56,7 +56,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.nosql.generic</artifactId>
-            <version>1.0.0</version>
+            <version>1.1.0-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
     
diff --git a/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/CouchbaseNoSqlAdapter.java b/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/CouchbaseNoSqlAdapter.java
index 12302fa..2b0dd6c 100644
--- a/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/CouchbaseNoSqlAdapter.java
+++ b/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/CouchbaseNoSqlAdapter.java
@@ -26,12 +26,15 @@ import java.util.Iterator;
 import java.util.regex.Pattern;
 
 import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.nosql.couchbase.client.CouchbaseClient;
 import org.apache.sling.nosql.couchbase.client.CouchbaseKey;
 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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.couchbase.client.java.Bucket;
 import com.couchbase.client.java.document.JsonDocument;
@@ -57,14 +60,11 @@ public final class CouchbaseNoSqlAdapter extends AbstractNoSqlAdapter {
     
     private static final N1qlParams N1QL_PARAMS = N1qlParams.build().consistency(ScanConsistency.REQUEST_PLUS);
 
+    private static final Logger log = LoggerFactory.getLogger(CouchbaseNoSqlAdapter.class);
+
     public CouchbaseNoSqlAdapter(CouchbaseClient couchbaseClient, String cacheKeyPrefix) {
         this.couchbaseClient = couchbaseClient;
         this.cacheKeyPrefix = cacheKeyPrefix;
-        
-        // make sure primary index and index on parentPath is present - ignore error if it is already present
-        Bucket bucket = couchbaseClient.getBucket();
-        bucket.query(N1qlQuery.simple("CREATE PRIMARY INDEX ON `" + couchbaseClient.getBucketName() + "`"));
-        bucket.query(N1qlQuery.simple("CREATE INDEX " + PN_PARENT_PATH + " ON `" + couchbaseClient.getBucketName() + "`(" + PN_PARENT_PATH + ")"));
     }
 
     @Override
@@ -183,4 +183,30 @@ public final class CouchbaseNoSqlAdapter extends AbstractNoSqlAdapter {
         }
     }
 
+    @Override
+    public void checkConnection() throws LoginException {
+        // try to access root element to check connection
+        try {
+            Bucket bucket = couchbaseClient.getBucket();
+            String cacheKey = CouchbaseKey.build("/", cacheKeyPrefix);
+            bucket.exists(cacheKey);
+        }
+        catch (Throwable ex) {
+            throw new LoginException(ex);
+        }
+    }
+
+    @Override
+    public void createIndexDefinitions() {
+        // make sure primary index and index on parentPath is present - ignore error if it is already present
+        try {
+            Bucket bucket = couchbaseClient.getBucket();
+            bucket.query(N1qlQuery.simple("CREATE PRIMARY INDEX ON `" + couchbaseClient.getBucketName() + "`"));
+            bucket.query(N1qlQuery.simple("CREATE INDEX " + PN_PARENT_PATH + " ON `" + couchbaseClient.getBucketName() + "`(" + PN_PARENT_PATH + ")"));
+        }
+        catch (Throwable ex) {
+            log.debug("Unable to create/validate couchbase index definitions: " + ex.getMessage(), ex);
+        }
+    }
+
 }

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