incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1368189 - in /sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl: ChangeableValueMap.java MongoDBResourceProvider.java ReadableValueMap.java
Date Wed, 01 Aug 2012 18:42:29 GMT
Author: cziegeler
Date: Wed Aug  1 18:42:28 2012
New Revision: 1368189

URL: http://svn.apache.org/viewvc?rev=1368189&view=rev
Log:
New MongoDB resource provider

Modified:
    sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java
    sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java
    sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java

Modified: sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java?rev=1368189&r1=1368188&r2=1368189&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java
(original)
+++ sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java
Wed Aug  1 18:42:28 2012
@@ -43,12 +43,8 @@ public class ChangeableValueMap
      */
     public Object put(final String name, final Object value) {
         final Object oldValue = this.valueMap.get(name);
-        final String key;
-        if ( name.startsWith("_") ) {
-            key = "_" + name;
-        } else {
-            key = name;
-        }
+        final String key = MongoDBResourceProvider.propNameToKey(name);
+
         this.resource.getProperties().put(key, value);
 
         // update map and resource
@@ -73,12 +69,8 @@ public class ChangeableValueMap
     public Object remove(final Object name) {
         final Object result = this.valueMap.get(name);
         if ( result != null ) {
-            final String key;
-            if ( name.toString().startsWith("_") ) {
-                key = "_" + name;
-            } else {
-                key = name.toString();
-            }
+            final String key = MongoDBResourceProvider.propNameToKey(name.toString());
+
             this.resource.getProperties().removeField(key);
 
             // update map and resource

Modified: sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java?rev=1368189&r1=1368188&r2=1368189&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java
(original)
+++ sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java
Wed Aug  1 18:42:28 2012
@@ -66,6 +66,22 @@ public class MongoDBResourceProvider imp
         this.context = context;
     }
 
+    public static String propNameToKey(final String name) {
+        if ( name.startsWith("_") ) {
+            return "_" + name;
+        }
+        return name;
+    }
+
+    public static String keyToPropName(final String key) {
+        if ( key.startsWith("__") ) {
+            return key.substring(1);
+        } else if ( key.startsWith("_") ) {
+            return null;
+        }
+        return key;
+    }
+
     /**
      * @see org.apache.sling.api.resource.ModifyingResourceProvider#create(org.apache.sling.api.resource.ResourceResolver,
java.lang.String, java.util.Map)
      */
@@ -82,12 +98,7 @@ public class MongoDBResourceProvider imp
             dbObj.put(PROP_PATH, info[1]);
             if ( properties != null ) {
                 for(Map.Entry<String, Object> entry : properties.entrySet()) {
-                    final String key;
-                    if ( entry.getKey().startsWith("_") ) {
-                        key = "_" + entry.getKey();
-                    } else {
-                        key = entry.getKey();
-                    }
+                    final String key = propNameToKey(entry.getKey());
                     dbObj.put(key, entry.getValue());
                 }
             }
@@ -107,12 +118,33 @@ public class MongoDBResourceProvider imp
      */
     public void delete(final ResourceResolver resolver, final String path)
     throws PersistenceException {
-        final Resource rsrc = this.getResource(resolver, path);
-        if ( rsrc != null ) {
-            // TODO - delete all child resources!
-            this.deletedResources.add(path);
-            this.changedResources.remove(path);
+        if ( deletedResources.contains(path) ) {
+            return;
         }
+
+        final String[] info = this.extractResourceInfo(path);
+        if ( info != null ) {
+            final Resource rsrc = this.getResource(resolver, path, info);
+            if ( rsrc instanceof MongoDBResource ) {
+                this.deletedResources.add(path);
+                this.changedResources.remove(path);
+
+                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 DBCursor cur = col.find(query);
+                while ( cur.hasNext() ) {
+                    final DBObject dbObj = cur.next();
+                    final String childPath = info[0] + '/' + dbObj.get(PROP_PATH);
+                    this.deletedResources.add(childPath);
+                    this.changedResources.remove(childPath);
+                }
+                return;
+            }
+
+        }
+        throw new PersistenceException("Unable to delete resource at {}" + path, null, path,
null);
     }
 
     /**
@@ -131,12 +163,10 @@ public class MongoDBResourceProvider imp
             for(final String deleted : this.deletedResources) {
                 final String[] info = this.extractResourceInfo(deleted);
 
-                // check if the database still exists
-                if ( this.hasDatabase(info[0]) ) {
-                    final DBCollection col = this.context.getDatabase().getCollection(info[0]);
-                    if ( col != null ) {
-                        col.findAndRemove(QueryBuilder.start(PROP_PATH).is(info[1]).get());
-                    }
+                // check if the collection still exists
+                final DBCollection col = this.getCollection(info[0]);
+                if ( col != null ) {
+                    col.findAndRemove(QueryBuilder.start(PROP_PATH).is(info[1]).get());
                 }
             }
             for(final MongoDBResource changed : this.changedResources.values()) {
@@ -217,48 +247,46 @@ public class MongoDBResourceProvider imp
 
                 };
             }
-            if ( this.hasDatabase(info[0]) ) {
-                final DBCollection col = this.context.getDatabase().getCollection(info[0]);
-                if ( col != null ) {
-                    final String pattern;
-                    if ( info.length == 1 ) {
-                        pattern = "^([^/])*$";
-                    } else {
-                        pattern = "^" + Pattern.quote(info[1]) + "/([^/])*$";
-                    }
+            final DBCollection col = this.getCollection(info[0]);
+            if ( col != null ) {
+                final String pattern;
+                if ( info.length == 1 ) {
+                    pattern = "^([^/])*$";
+                } else {
+                    pattern = "^" + Pattern.quote(info[1]) + "/([^/])*$";
+                }
 
-                    final DBObject query = QueryBuilder.start(PROP_PATH).regex(Pattern.compile(pattern)).get();
-                    final DBCursor cur = col.find(query).
-                                    sort(BasicDBObjectBuilder.start(PROP_PATH, 1).get());
-                    return new Iterator<Resource>() {
+                final DBObject query = QueryBuilder.start(PROP_PATH).regex(Pattern.compile(pattern)).get();
+                final DBCursor cur = col.find(query).
+                                sort(BasicDBObjectBuilder.start(PROP_PATH, 1).get());
+                return new Iterator<Resource>() {
 
-                        public boolean hasNext() {
-                            return cur.hasNext();
-                        }
+                    public boolean hasNext() {
+                        return cur.hasNext();
+                    }
 
-                        public Resource next() {
-                            final DBObject obj = cur.next();
-                            final String objPath = obj.get(PROP_PATH).toString();
-                            final int lastSlash = objPath.lastIndexOf('/');
-                            final String name;
-                            if (lastSlash == -1) {
-                                name = objPath;
-                            } else {
-                                name = objPath.substring(lastSlash + 1);
-                            }
-                            return new MongoDBResource(parent.getResourceResolver(),
-                                            parent.getPath() + '/' + name,
-                                            info[0],
-                                            obj,
-                                            MongoDBResourceProvider.this);
+                    public Resource next() {
+                        final DBObject obj = cur.next();
+                        final String objPath = obj.get(PROP_PATH).toString();
+                        final int lastSlash = objPath.lastIndexOf('/');
+                        final String name;
+                        if (lastSlash == -1) {
+                            name = objPath;
+                        } else {
+                            name = objPath.substring(lastSlash + 1);
                         }
+                        return new MongoDBResource(parent.getResourceResolver(),
+                                        parent.getPath() + '/' + name,
+                                        info[0],
+                                        obj,
+                                        MongoDBResourceProvider.this);
+                    }
 
-                        public void remove() {
-                            throw new UnsupportedOperationException("remove");
-                        }
+                    public void remove() {
+                        throw new UnsupportedOperationException("remove");
+                    }
 
-                    };
-                }
+                };
             }
         }
         return null;
@@ -303,15 +331,25 @@ public class MongoDBResourceProvider imp
     }
 
     /**
-     * Check if a database with a given name exists
+     * Check if a collection with a given name exists
      */
-    private boolean hasDatabase(final String name) {
+    private boolean hasCollection(final String name) {
         final Set<String> names = this.context.getDatabase().getCollectionNames();
         return names.contains(name) && !this.context.isFilterCollectionName(name);
     }
 
     /**
-     * Create a resource
+     * Check if a collection with a given name exists and return it
+     */
+    private DBCollection getCollection(final String name) {
+        if ( this.hasCollection(name) ) {
+            return this.context.getDatabase().getCollection(name);
+        }
+        return null;
+    }
+
+    /**
+     * Get a resource
      */
     private Resource getResource(final ResourceResolver resourceResolver, final String path,
final String[] info) {
         if ( info.length == 0 ) {
@@ -319,26 +357,25 @@ public class MongoDBResourceProvider imp
             return new MongoDBCollectionResource(resourceResolver, path);
         } else if ( info.length == 1 ) {
             // special resource : collection
-            if ( this.hasDatabase(info[0]) ) {
+            if ( this.hasCollection(info[0]) ) {
                 return new MongoDBCollectionResource(resourceResolver, path);
             }
             return null;
         }
-        logger.info("Searching {} in {}", info[1], info[0]);
-        if ( this.hasDatabase(info[0]) ) {
-            final DBCollection col = this.context.getDatabase().getCollection(info[0]);
-            if ( col != null ) {
-                final DBObject obj = col.findOne(QueryBuilder.start(PROP_PATH).is(info[1]).get());
-                logger.info("Result={}", obj);
-                if ( obj != null ) {
-                    return new MongoDBResource(resourceResolver,
-                                    path,
-                                    info[0],
-                                    obj,
-                                    this);
-                }
+        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());
+            logger.debug("Found {}", obj);
+            if ( obj != null ) {
+                return new MongoDBResource(resourceResolver,
+                                path,
+                                info[0],
+                                obj,
+                                this);
             }
         }
+
         return null;
     }
 

Modified: sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java?rev=1368189&r1=1368188&r2=1368189&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java
(original)
+++ sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java
Wed Aug  1 18:42:28 2012
@@ -20,7 +20,6 @@ import java.lang.reflect.Array;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -39,16 +38,11 @@ public class ReadableValueMap implements
     }
 
     protected void createValueMap(final DBObject dbObject) {
-        @SuppressWarnings("unchecked")
-        final Map<String, Object> map = new HashMap<String, Object>(dbObject.toMap());
-        final Set<String> names = new HashSet<String>(map.keySet());
-        for(final String name : names) {
-            if ( name.startsWith("__") ) {
-                final Object value = map.remove(name);
-                map.put(name.substring(1), value);
-            } else if ( name.startsWith("_") ) {
-                // remove internal props, like _id, _path
-                map.remove(name);
+        final Map<String, Object> map = new HashMap<String, Object>();
+        for(final Map.Entry<String, Object> entry : map.entrySet()) {
+            final String name = MongoDBResourceProvider.keyToPropName(entry.getKey());
+            if ( name != null ) {
+                map.put(name, entry.getValue());
             }
         }
         this.valueMap = Collections.unmodifiableMap(map);



Mime
View raw message