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:25:08 UTC

[sling-org-apache-sling-mongodb] 20/36: SLING-3002 : Make the Mongo Resource Provider easier to subclass. Apply patch from Dan McWeeney

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-mongodb.git

commit 4c0625f22ed94ff9be09c044858e20de047e2f73
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Aug 9 06:13:24 2013 +0000

    SLING-3002 : Make the Mongo Resource Provider easier to subclass. Apply patch from Dan McWeeney
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1512146 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/mongodb/impl/ChangeableValueMap.java     |  5 ++
 .../apache/sling/mongodb/impl/MongoDBContext.java  |  2 +-
 .../apache/sling/mongodb/impl/MongoDBResource.java |  4 +-
 .../mongodb/impl/MongoDBResourceProvider.java      | 64 ++++++++++++++++------
 .../impl/MongoDBResourceProviderFactory.java       | 23 +++++++-
 .../sling/mongodb/impl/ReadableValueMap.java       |  3 +
 .../OSGI-INF/metatype/metatype.properties          |  8 ++-
 7 files changed, 85 insertions(+), 24 deletions(-)

diff --git a/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java b/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java
index 5cc08b5..2061171 100644
--- a/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java
+++ b/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java
@@ -19,6 +19,7 @@ package org.apache.sling.mongodb.impl;
 import java.util.Map;
 
 import org.apache.sling.api.resource.ModifiableValueMap;
+import org.apache.sling.api.resource.Resource;
 
 public class ChangeableValueMap
     extends ReadableValueMap
@@ -79,4 +80,8 @@ public class ChangeableValueMap
         }
         return result;
     }
+    
+    protected MongoDBResource getResource() {
+        return this.resource;
+    }
 }
diff --git a/src/main/java/org/apache/sling/mongodb/impl/MongoDBContext.java b/src/main/java/org/apache/sling/mongodb/impl/MongoDBContext.java
index cd8ccc1..0c2e08a 100644
--- a/src/main/java/org/apache/sling/mongodb/impl/MongoDBContext.java
+++ b/src/main/java/org/apache/sling/mongodb/impl/MongoDBContext.java
@@ -116,5 +116,5 @@ public class MongoDBContext {
         props.put("event.distribute", "");
         final Event event = new Event(SlingConstants.TOPIC_RESOURCE_CHANGED, props);
         this.eventAdmin.postEvent(event);
-    }
+    }    
 }
diff --git a/src/main/java/org/apache/sling/mongodb/impl/MongoDBResource.java b/src/main/java/org/apache/sling/mongodb/impl/MongoDBResource.java
index 6ad685c..0aad630 100644
--- a/src/main/java/org/apache/sling/mongodb/impl/MongoDBResource.java
+++ b/src/main/java/org/apache/sling/mongodb/impl/MongoDBResource.java
@@ -38,7 +38,7 @@ public class MongoDBResource extends AbstractResource {
     private final ResourceResolver resourceResolver;
 
     /** Metadata. */
-    private final ResourceMetadata metadata = new ResourceMetadata();
+    protected ResourceMetadata metadata = new ResourceMetadata();
 
     /** The db object. */
     private DBObject dbObject;
@@ -136,7 +136,7 @@ public class MongoDBResource extends AbstractResource {
 
     @Override
     public String toString() {
-        return "MongoDBResource [resourcePath=" + resourcePath + ", dbPath=" + this.dbObject.get(MongoDBResourceProvider.PROP_PATH) + ", collection=" + collection
+        return "MongoDBResource [resourcePath=" + resourcePath + ", dbPath=" + this.dbObject.get(provider.getPROP_PATH()) + ", collection=" + collection
                         + ", resourceResolver=" + resourceResolver + "]";
     }
 
diff --git a/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java b/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java
index b362e16..b1168ed 100644
--- a/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java
+++ b/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java
@@ -27,9 +27,11 @@ import javax.servlet.http.HttpServletRequest;
 
 import org.apache.sling.api.resource.ModifyingResourceProvider;
 import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.QueriableResourceProvider;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceProvider;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,13 +46,13 @@ import com.mongodb.QueryBuilder;
  * The MongoDB resource provider creates resources based on MongoDB entries.
  * The resources contain all properties stored in the MongoDB except those starting with a "_".
  */
-public class MongoDBResourceProvider implements ResourceProvider, ModifyingResourceProvider {
+public class MongoDBResourceProvider implements ResourceProvider, ModifyingResourceProvider, QueriableResourceProvider {
 
     /** The special path property containing the (relative) path of the resource in the tree. */
-    public static final String PROP_PATH = "_path";
+    private static final String PROP_PATH = "_path";
 
     /** The id property. */
-    public static final String PROP_ID = "_id";
+    private static final String PROP_ID = "_id";
 
     /** Logger. */
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -95,7 +97,7 @@ public class MongoDBResourceProvider implements ResourceProvider, ModifyingResou
                 throw new PersistenceException("Resource already exists at " + path, null, path, null);
             }
             final DBObject dbObj = new BasicDBObject();
-            dbObj.put(PROP_PATH, info[1]);
+            dbObj.put(getPROP_PATH(), info[1]);
             if ( properties != null ) {
                 for(Map.Entry<String, Object> entry : properties.entrySet()) {
                     final String key = propNameToKey(entry.getKey());
@@ -135,11 +137,11 @@ public class MongoDBResourceProvider implements ResourceProvider, ModifyingResou
                     final DBCollection col = this.getCollection(info[0]);
                     final String pattern = "^" + Pattern.quote(info[1]) + "/";
 
-                    final DBObject query = QueryBuilder.start(PROP_PATH).regex(Pattern.compile(pattern)).get();
+                    final DBObject query = QueryBuilder.start(getPROP_PATH()).regex(Pattern.compile(pattern)).get();
                     final DBCursor cur = col.find(query);
                     while ( cur.hasNext() ) {
                         final DBObject dbObj = cur.next();
-                        final String childPath = info[0] + '/' + dbObj.get(PROP_PATH);
+                        final String childPath = info[0] + '/' + dbObj.get(getPROP_PATH());
                         this.deletedResources.add(childPath);
                         this.changedResources.remove(childPath);
                     }
@@ -183,7 +185,7 @@ public class MongoDBResourceProvider implements ResourceProvider, ModifyingResou
                 // check if the collection still exists
                 final DBCollection col = this.getCollection(info[0]);
                 if ( col != null ) {
-                    if ( col.findAndRemove(QueryBuilder.start(PROP_PATH).is(info[1]).get()) != null ) {
+                    if ( col.findAndRemove(QueryBuilder.start(getPROP_PATH()).is(info[1]).get()) != null ) {
                         this.context.notifyRemoved(info);
                     }
                 }
@@ -193,10 +195,10 @@ public class MongoDBResourceProvider implements ResourceProvider, ModifyingResou
                 final DBCollection col = this.context.getDatabase().getCollection(changed.getCollection());
                 if ( col != null ) {
                     final String[] info = new String[] {changed.getCollection(),
-                            changed.getProperties().get(PROP_PATH).toString()};
+                            changed.getProperties().get(getPROP_PATH()).toString()};
                     // create or update?
                     if ( changed.getProperties().get(PROP_ID) != null ) {
-                        col.update(QueryBuilder.start(PROP_PATH).is(changed.getProperties().get(PROP_PATH)).get(),
+                        col.update(QueryBuilder.start(getPROP_PATH()).is(changed.getProperties().get(getPROP_PATH())).get(),
                                 changed.getProperties());
                         this.context.notifyUpdated(info);
                     } else {
@@ -283,9 +285,9 @@ public class MongoDBResourceProvider implements ResourceProvider, ModifyingResou
                     pattern = "^" + Pattern.quote(info[1]) + "/([^/])*$";
                 }
 
-                final DBObject query = QueryBuilder.start(PROP_PATH).regex(Pattern.compile(pattern)).get();
+                final DBObject query = QueryBuilder.start(getPROP_PATH()).regex(Pattern.compile(pattern)).get();
                 final DBCursor cur = col.find(query).
-                        sort(BasicDBObjectBuilder.start(PROP_PATH, 1).get());
+                        sort(BasicDBObjectBuilder.start(getPROP_PATH(), 1).get());
                 return new Iterator<Resource>() {
 
                     public boolean hasNext() {
@@ -294,7 +296,7 @@ public class MongoDBResourceProvider implements ResourceProvider, ModifyingResou
 
                     public Resource next() {
                         final DBObject obj = cur.next();
-                        final String objPath = obj.get(PROP_PATH).toString();
+                        final String objPath = obj.get(getPROP_PATH()).toString();
                         final int lastSlash = objPath.lastIndexOf('/');
                         final String name;
                         if (lastSlash == -1) {
@@ -332,7 +334,7 @@ public class MongoDBResourceProvider implements ResourceProvider, ModifyingResou
     /**
      * Extract info about collection and path
      */
-    private String[] extractResourceInfo(final String path) {
+    protected String[] extractResourceInfo(final String path) {
         if ( path.startsWith(this.context.getRootWithSlash()) ) {
             if ( path.length() == this.context.getRootWithSlash().length() ) {
                 // special resource - show all collections
@@ -358,16 +360,18 @@ public class MongoDBResourceProvider implements ResourceProvider, ModifyingResou
     /**
      * Check if a collection with a given name exists
      */
-    private boolean hasCollection(final String name) {
+    protected boolean hasCollection(final String name) {
+        logger.info("Mongo: Getting collection names");
         final Set<String> names = this.context.getDatabase().getCollectionNames();
         return names.contains(name) && !this.context.isFilterCollectionName(name);
     }
 
+    
     /**
      * Check if a collection with a given name exists and return it
      */
-    private DBCollection getCollection(final String name) {
-        if ( this.hasCollection(name) ) {
+    protected DBCollection getCollection(final String name) {
+        if ( this.hasCollection(name) ) {            
             return this.context.getDatabase().getCollection(name);
         }
         return null;
@@ -376,7 +380,7 @@ public class MongoDBResourceProvider implements ResourceProvider, ModifyingResou
     /**
      * Get a resource
      */
-    private Resource getResource(final ResourceResolver resourceResolver, final String path, final String[] info) {
+    protected Resource getResource(final ResourceResolver resourceResolver, final String path, final String[] info) {
         if ( info.length == 0 ) {
             // special resource : all collections
             return new MongoDBCollectionResource(resourceResolver, path);
@@ -390,7 +394,7 @@ public class MongoDBResourceProvider implements ResourceProvider, ModifyingResou
         logger.debug("Searching {} in {}", info[1], info[0]);
         final DBCollection col = this.getCollection(info[0]);
         if ( col != null ) {
-            final DBObject obj = col.findOne(QueryBuilder.start(PROP_PATH).is(info[1]).get());
+            final DBObject obj = col.findOne(QueryBuilder.start(getPROP_PATH()).is(info[1]).get());
             logger.debug("Found {}", obj);
             if ( obj != null ) {
                 return new MongoDBResource(resourceResolver,
@@ -414,4 +418,28 @@ public class MongoDBResourceProvider implements ResourceProvider, ModifyingResou
         }
         return dbObj;
     }
+    
+    protected Set<String> getDeletedResources() {
+        return this.deletedResources;
+    }
+    
+    protected Map<String, MongoDBResource> getChangedResources() {
+        return this.changedResources;
+    }
+    
+    protected MongoDBContext getContext() {
+        return this.context;
+    }
+    
+    protected String getPROP_PATH() {
+        return PROP_PATH;
+    }
+
+    public Iterator<Resource> findResources(ResourceResolver resolver, String query, String language) {
+        return null;
+    }
+
+    public Iterator<ValueMap> queryResources(ResourceResolver resolver, String query, String language) {
+        return null;
+    }
 }
diff --git a/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java b/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java
index 5eaeca0..9bcf562 100644
--- a/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java
+++ b/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java
@@ -35,7 +35,9 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.mongodb.DB;
+import com.mongodb.DBAddress;
 import com.mongodb.Mongo;
+import com.mongodb.MongoOptions;
 
 /**
  * The MongoDB resource provider factory allows to provided resources stored
@@ -66,10 +68,18 @@ public class MongoDBResourceProviderFactory implements ResourceProviderFactory {
 
     @Property(value=DEFAULT_DB)
     private static final String PROP_DB = "db";
-
+    
     @Property(unbounded=PropertyUnbounded.ARRAY, value="system.indexes")
     private static final String PROP_FILTER_COLLECTIONS = "filter.collections";
 
+    private static final int DEFAULT_NUMCONNECTIONS = 10;
+    @Property(intValue=DEFAULT_NUMCONNECTIONS)
+    private static final String PROP_NUM_CONNECTIONS = "numconnections";
+
+    private static final int DEFAULT_THREAD_MULTIPLIER= 5;
+    @Property(intValue=DEFAULT_THREAD_MULTIPLIER)
+    private static final String  PROP_THREAD_MULTIPLIER = "threadmultiplier";
+    
     /** Logger. */
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
@@ -96,8 +106,13 @@ public class MongoDBResourceProviderFactory implements ResourceProviderFactory {
         final String db = PropertiesUtil.toString(props.get(PROP_DB), DEFAULT_DB);
         logger.info("Starting MongoDB resource provider with host={}, port={}, db={}",
                 new Object[] {host, port, db});
+        final DBAddress address = new DBAddress(host, port, db);
+        final MongoOptions options = new MongoOptions();
+        
+        options.connectionsPerHost = PropertiesUtil.toInteger(props.get(PROP_NUM_CONNECTIONS), DEFAULT_NUMCONNECTIONS);
+        options.threadsAllowedToBlockForConnectionMultiplier = PropertiesUtil.toInteger(props.get(PROP_THREAD_MULTIPLIER), DEFAULT_THREAD_MULTIPLIER);
+        final Mongo m = new Mongo(address, options);
 
-        final Mongo m = new Mongo( host , port );
         final DB database = m.getDB( db );
         logger.info("Connected to database {}", database);
 
@@ -122,4 +137,8 @@ public class MongoDBResourceProviderFactory implements ResourceProviderFactory {
         // for now we allow anonymous access
         return new MongoDBResourceProvider(this.context);
     }
+    
+    protected MongoDBContext getContext() {
+        return this.context;
+    }
 }
diff --git a/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java b/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java
index c294d84..a363591 100644
--- a/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java
+++ b/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java
@@ -39,6 +39,9 @@ public class ReadableValueMap implements ValueMap {
 
     protected void createValueMap(final DBObject dbObject) {
         final Map<String, Object> map = new HashMap<String, Object>();
+        if (dbObject == null) {
+            this.valueMap = Collections.<String, Object> emptyMap();
+        }
         for(final String key : dbObject.keySet()) {
             final String name = MongoDBResourceProvider.keyToPropName(key);
             if ( name != null ) {
diff --git a/src/main/resources/OSGI-INF/metatype/metatype.properties b/src/main/resources/OSGI-INF/metatype/metatype.properties
index 4771886..22bcddd 100644
--- a/src/main/resources/OSGI-INF/metatype/metatype.properties
+++ b/src/main/resources/OSGI-INF/metatype/metatype.properties
@@ -43,4 +43,10 @@ db.name MongoDB Database
 db.description = The database to use.
 
 filter.collections.name = Filter Collections
-filter.collections.description = These collections are not available as resources.
\ No newline at end of file
+filter.collections.description = These collections are not available as resources.
+
+numconnections.name = Connections Per Host
+numconnections.description = MongoOptions.connectionsPerHost
+
+threadmultiplier.name = Thread Multiplier
+threadmultiplier.description = MongoOptions.threadsAllowedToBlockForConnectionMultiplier
\ No newline at end of file

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