modified
This commit is contained in:
parent
c774e1e849
commit
14e4f554a3
|
@ -1,15 +1,20 @@
|
|||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.jcr.Node;
|
||||
import javax.jcr.NodeIterator;
|
||||
import javax.jcr.PathNotFoundException;
|
||||
import javax.jcr.Session;
|
||||
|
||||
import org.apache.jackrabbit.api.JackrabbitSession;
|
||||
import org.gcube.common.storagehub.model.Excludes;
|
||||
import org.gcube.common.storagehub.model.Path;
|
||||
import org.gcube.common.storagehub.model.Paths;
|
||||
import org.gcube.common.storagehub.model.items.AbstractFileItem;
|
||||
import org.gcube.common.storagehub.model.items.FolderItem;
|
||||
import org.gcube.common.storagehub.model.items.Item;
|
||||
import org.gcube.common.storagehub.model.items.TrashItem;
|
||||
import org.gcube.data.access.storagehub.scripting.AbstractScript;
|
||||
|
@ -31,7 +36,7 @@ public class CleanTrash implements AbstractScript{
|
|||
Node node = session.getNode("/Home");
|
||||
|
||||
NodeIterator it = node.getNodes();
|
||||
|
||||
long total = 0;
|
||||
while (it.hasNext()) {
|
||||
Node home = it.nextNode();
|
||||
|
||||
|
@ -44,7 +49,7 @@ public class CleanTrash implements AbstractScript{
|
|||
|
||||
Node trashNode = null;
|
||||
try {
|
||||
if (home.getProperty("hl:version").getLong()>0)
|
||||
if (home.hasProperty("hl:version") && home.getProperty("hl:version").getLong()>0)
|
||||
trashNode = session.getNode(Paths.append(Paths.getPath(home.getPath()), "Trash").toPath());
|
||||
else
|
||||
trashNode = session.getNode(Paths.append(workspacePath, "Trash").toPath());
|
||||
|
@ -57,16 +62,21 @@ public class CleanTrash implements AbstractScript{
|
|||
|
||||
List<Item> items = scriptUtil.getChildren(null, trashNode, null, true, TrashItem.class);
|
||||
|
||||
long totalUser = 0;
|
||||
for (Item item: items) {
|
||||
TrashItem trashItem = (TrashItem) item;
|
||||
stringBuilder.append("\n").append("removed node ").append(trashItem.getTitle());
|
||||
//stringBuilder.append("\n").append("removed node ").append(trashItem.getTitle());
|
||||
if (System.currentTimeMillis() - trashItem.getDeletedTime().getTimeInMillis() > 2629800000l ) {
|
||||
scriptUtil.removeNodes(session, Collections.singletonList(trashItem));
|
||||
//scriptUtil.removeNodes(session, Collections.singletonList(trashItem));
|
||||
Map<String, Long> calc = calculateDiskSpace(trashItem, scriptUtil, session);
|
||||
totalUser+= calc.get("space");
|
||||
}
|
||||
}
|
||||
stringBuilder.append("\n").append("cleaned trash of ").append(home.getName()).append(" in ").append(System.currentTimeMillis()-start).append(" millis");
|
||||
|
||||
total += totalUser;
|
||||
//stringBuilder.append("\n").append("cleaned trash of ").append(home.getName()).append(" in ").append(System.currentTimeMillis()-start).append(" millis");
|
||||
stringBuilder.append("\n").append("cleaned trash of ").append(home.getName()).append(" space freed ").append(totalUser).append(" Byte");
|
||||
}
|
||||
stringBuilder.append("\n total space freed ").append(total).append(" Byte");
|
||||
}catch (Exception e) {
|
||||
StringWriter writer = new StringWriter();
|
||||
PrintWriter sw = new PrintWriter(writer, true);
|
||||
|
@ -75,5 +85,33 @@ public class CleanTrash implements AbstractScript{
|
|||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
private Map<String, Long> calculateDiskSpace(Item item, ScriptUtil scriptUtil, Session session ) throws Exception {
|
||||
Map<String, Long> val = new HashMap<>();
|
||||
val.put("space", 0l);
|
||||
val.put("items", 0l);
|
||||
|
||||
if (item instanceof AbstractFileItem) {
|
||||
val.put("space", ((AbstractFileItem) item).getContent().getSize());
|
||||
val.put("items", 1l);
|
||||
return val;
|
||||
}else if (item instanceof FolderItem || item instanceof TrashItem) {
|
||||
|
||||
Node node = session.getNodeByIdentifier(item.getId());
|
||||
List<? extends Item> items = scriptUtil.getChildren(null, node, Excludes.GET_ONLY_CONTENT, true, null);
|
||||
for (Item child : items)
|
||||
try {
|
||||
Map<String, Long> retVal = calculateDiskSpace(child, scriptUtil, session);
|
||||
long space = retVal.get("space")+val.get("space");
|
||||
long itemsCount = retVal.get("items")+val.get("items");
|
||||
val.put("space", space);
|
||||
val.put("items", itemsCount);
|
||||
}catch (Exception e) {
|
||||
log.error("cannot calculate disk space for item id {}",child.getId(),e);
|
||||
}
|
||||
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue