You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by je...@apache.org on 2018/10/17 18:22:21 UTC

[sling-whiteboard] 02/02: added recursive deletion

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

jeb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit c979af9539a3e55f5b919d4f976c3c49e6472a46
Author: JE Bailey <ja...@sas.com>
AuthorDate: Wed Oct 17 14:21:53 2018 -0400

    added recursive deletion
---
 .../apache/sling/mvresource/impl/MvResource.java   |  6 ++-
 .../mvresource/impl/MvStoreResourceProvider.java   | 43 +++++++++++++++++-----
 2 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/mvresourceprovider/src/main/java/org/apache/sling/mvresource/impl/MvResource.java b/mvresourceprovider/src/main/java/org/apache/sling/mvresource/impl/MvResource.java
index 6addbd4..5d031fc 100644
--- a/mvresourceprovider/src/main/java/org/apache/sling/mvresource/impl/MvResource.java
+++ b/mvresourceprovider/src/main/java/org/apache/sling/mvresource/impl/MvResource.java
@@ -48,7 +48,11 @@ public class MvResource extends AbstractResource {
         if (properties.isEmpty()) {
             return null;
         }
-        return (String) properties.get("sling:resourceType");
+        String type = (String) properties.get("sling:resourceType");
+        if (type == null) {
+            type = (String) properties.get("jcr:primaryType");
+        }
+        return type;
     }
 
     @Override
diff --git a/mvresourceprovider/src/main/java/org/apache/sling/mvresource/impl/MvStoreResourceProvider.java b/mvresourceprovider/src/main/java/org/apache/sling/mvresource/impl/MvStoreResourceProvider.java
index 2c7c666..e468365 100644
--- a/mvresourceprovider/src/main/java/org/apache/sling/mvresource/impl/MvStoreResourceProvider.java
+++ b/mvresourceprovider/src/main/java/org/apache/sling/mvresource/impl/MvStoreResourceProvider.java
@@ -20,7 +20,9 @@ package org.apache.sling.mvresource.impl;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Deque;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
@@ -113,16 +115,13 @@ public class MvStoreResourceProvider extends ResourceProvider<Object> implements
         LOG.info("CREATE  {} ", path);
         try {
             String parent = parentPath(path);
-            String child = currentName(path);
             MVMap<String, String[]> parentResource = store.openMap("_children");
             String[] children = parentResource.getOrDefault(parent, new String[] {});
             String[] newChildren = Arrays.copyOf(children, children.length + 1);
-            newChildren[children.length] = child;
+            newChildren[children.length] = path;
             parentResource.put(parent, newChildren);
             MVMap<String, Object> data = store.openMap(path);
-            
             data.putAll(properties);
-            store.commit();
             return new MvResource(ctx.getResourceResolver(), path, data);
         } catch (Exception e) {
             LOG.error("Error occured in creation {}", e);
@@ -131,7 +130,7 @@ public class MvStoreResourceProvider extends ResourceProvider<Object> implements
 
     }
 
-    private String currentName(String path) {
+    public String currentName(String path) {
         LOG.info("CURRENT NAME  {} ", path);
         int index = path.lastIndexOf('/');
         return path.substring(index + 1, path.length());
@@ -151,12 +150,36 @@ public class MvStoreResourceProvider extends ResourceProvider<Object> implements
     @Override
     public void delete(ResolveContext<Object> ctx, Resource resource) throws PersistenceException {
         LOG.info("DELETE  {} ", resource.getName());
-        if (!(resource instanceof MVMap)) {
-            throw new PersistenceException();
+        if (!(resource instanceof MvResource)) {
+            throw new PersistenceException("can not delete resource of type" + resource.getClass());
         }
-        MVMap<String, Object> map = ((MvResource) resource).getMVMap();
-        store.removeMap(map);
-        store.commit();
+        MVMap<String, String[]> parentResource = store.openMap("_children");
+        String parentPath = parentPath(resource.getPath());
+        String[] childNames = parentResource.get(parentPath);
+        String[] newChildren = new String[childNames.length - 1];
+        int newIndex = 0;
+        for (int index = 0; index < childNames.length ; ++index) {
+            if (!childNames[index].equals(resource.getName())) {
+                newChildren[newIndex++] = childNames[index];
+            }
+        }
+        parentResource.put(parentPath, newChildren);
+        Deque<String> resourceToDelete = new LinkedList<>();
+        resourceToDelete.add(resource.getPath());
+        deleteDescendents(parentResource, resourceToDelete);
+    }
+    
+    private void deleteDescendents(MVMap<String, String[]> parentMap, Deque<String> pathsToDelete ) {
+        if (pathsToDelete.isEmpty()) {
+            return;
+        }
+        String currentPath = pathsToDelete.pop();
+        String[] children = parentMap.getOrDefault(currentPath, new String[] {});
+        store.removeMap(store.openMap(currentPath));
+        for (int i = 0; i < children.length ; ++i) {
+            pathsToDelete.add(children[i]);
+        }
+        deleteDescendents(parentMap,pathsToDelete);
     }
 
     @Override