From 43ca02592cd6e5a7c96046e9e84426b694b3dbb9 Mon Sep 17 00:00:00 2001 From: kshah Date: Wed, 24 Jul 2024 15:22:51 +0530 Subject: [PATCH] Some Optimization for NodeSize Calculation. --- .../rest/api/impl/FolderSizeImpl.java | 5 +- .../api/nodes/NodeFolderSizeRelation.java | 59 ++++++++----------- .../RelationshipResourceAction.java | 3 + .../executer/NodeSizeActionExecuter.java | 10 ++-- 4 files changed, 37 insertions(+), 40 deletions(-) diff --git a/remote-api/src/main/java/org/alfresco/rest/api/impl/FolderSizeImpl.java b/remote-api/src/main/java/org/alfresco/rest/api/impl/FolderSizeImpl.java index 131e0e248f..34ec79390d 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/impl/FolderSizeImpl.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/impl/FolderSizeImpl.java @@ -42,6 +42,7 @@ public class FolderSizeImpl { private ActionService actionService; private static final Logger LOG = LoggerFactory.getLogger(FolderSizeImpl.class); + private static final String IN_PROGRESS = "IN-PROGRESS"; public Map executingAsynchronousFolderAction(final int maxItems, final NodeRef nodeRef, final Map result, final SimpleCache simpleCache) { @@ -49,9 +50,11 @@ public class FolderSizeImpl { folderSizeAction.setTrackStatus(true); folderSizeAction.setExecuteAsynchronously(true); folderSizeAction.setParameterValue(NodeSizeActionExecuter.PAGE_SIZE, maxItems); - simpleCache.put(folderSizeAction.getId(),"IN-PROGRESS"); + simpleCache.put(folderSizeAction.getId(),IN_PROGRESS); actionService.executeAction(folderSizeAction, nodeRef, false, true); + LOG.info("Executing NodeSizeActionExecuter from executingAsynchronousFolderAction method"); + result.putIfAbsent("executionId",folderSizeAction.getId()); return result; } diff --git a/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeFolderSizeRelation.java b/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeFolderSizeRelation.java index 35ad51726b..0e3a2a9089 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeFolderSizeRelation.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/nodes/NodeFolderSizeRelation.java @@ -37,6 +37,7 @@ import org.alfresco.rest.framework.WebApiParam; import org.alfresco.rest.framework.WebApiParameters; import org.alfresco.rest.framework.core.exceptions.InvalidNodeTypeException; import org.alfresco.rest.framework.core.exceptions.NotFoundException; +import org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException; import org.alfresco.rest.framework.resource.RelationshipResource; import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction.CalculateSize; import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction.ReadById; @@ -68,6 +69,12 @@ import java.util.Map; @RelationshipResource(name = "calculateSize", entityResource = NodesEntityResource.class, title = "Calculate size") public class NodeFolderSizeRelation implements CalculateSize>, ReadById>, InitializingBean { + + private static final Logger LOG = LoggerFactory.getLogger(NodeFolderSizeRelation.class); + + private static final String INVALID_NODEID = "Invalid parameter: value of nodeId is invalid"; + private static final String EXECUTIONID_NOT_FOUND = "Searched ExecutionId does not exist"; + private Nodes nodes; private SearchService searchService; private ServiceRegistry serviceRegistry; @@ -75,21 +82,8 @@ public class NodeFolderSizeRelation implements CalculateSize private NodeService nodeService; private ActionService actionService; private ActionTrackingService actionTrackingService; - private String invalidMessage = "Invalid parameter: value of nodeId is invalid"; - private String notFoundMessage = "Searched ExecutionId does not exist"; - - /** the simple cache that will hold action data */ private SimpleCache simpleCache; - /** - * The logger - */ - private static final Logger LOG = LoggerFactory.getLogger(NodeFolderSizeRelation.class); - - /** - * The class that wraps the ReST APIs from core. - */ - public void setNodes(Nodes nodes) { this.nodes = nodes; @@ -116,12 +110,6 @@ public class NodeFolderSizeRelation implements CalculateSize this.actionService = actionService; } - @Override - public void afterPropertiesSet() - { - ParameterCheck.mandatory("nodes", this.nodes); - } - public void setActionTrackingService(ActionTrackingService actionTrackingService) { this.actionTrackingService = actionTrackingService; @@ -132,6 +120,11 @@ public class NodeFolderSizeRelation implements CalculateSize this.simpleCache = simpleCache; } + @Override + public void afterPropertiesSet() + { + ParameterCheck.mandatory("nodes", this.nodes); + } /** * Folder Size - returns size of a folder. * @@ -151,12 +144,11 @@ public class NodeFolderSizeRelation implements CalculateSize int maxItems = Math.min(params.getPaging().getMaxItems(), 1000); QName qName = nodeService.getType(nodeRef); Map result = new HashMap<>(); - validatePermissions(nodeRef, nodeId); - if(!"folder".equals(qName.getLocalName())) + if(!FOLDER.equalsIgnoreCase(qName.getLocalName())) { - throw new InvalidNodeTypeException(invalidMessage); + throw new InvalidNodeTypeException(INVALID_NODEID); } try @@ -164,33 +156,30 @@ public class NodeFolderSizeRelation implements CalculateSize FolderSizeImpl folderSizeImpl = new FolderSizeImpl(actionService); return folderSizeImpl.executingAsynchronousFolderAction(maxItems, nodeRef, result, simpleCache); } - catch (Exception ex) + catch (Exception alfrescoRuntimeError) { - LOG.error("Exception occurred in NodeFolderSizeRelation:createById {}", ex.getMessage()); - throw new AlfrescoRuntimeException("Exception occurred in NodeFolderSizeRelation:createById",ex); + LOG.error("Exception occurred in NodeFolderSizeRelation:createById {}", alfrescoRuntimeError.getMessage()); + throw new AlfrescoRuntimeException("Exception occurred in NodeFolderSizeRelation:createById",alfrescoRuntimeError); } } @Override @WebApiDescription(title = "Returns Folder Node Size", description = "Returning a Folder Node Size") @WebApiParameters({@WebApiParam(name = "executionId", title = "The unique id of Execution Job", description = "A single execution id")}) - public Map readById(String executionId, String id, Parameters parameters) + public Map readById(String executionId, String nodeId, Parameters parameters) { - Map result; - try { LOG.info("Retrieving OUTPUT from NodeSizeActionExecutor - NodeFolderSizeRelation:readById"); Object cachedResult = simpleCache.get(executionId); if(cachedResult != null) { - result = getResult(cachedResult); + return getResult(cachedResult); } else { - throw new NotFoundException(notFoundMessage); + throw new NotFoundException(EXECUTIONID_NOT_FOUND); } - return result; } catch (Exception ex) { @@ -198,6 +187,7 @@ public class NodeFolderSizeRelation implements CalculateSize throw ex; // Rethrow with original stack trace } } + private Map getResult(Object outputResult) { Map result = new HashMap<>(); @@ -205,12 +195,12 @@ public class NodeFolderSizeRelation implements CalculateSize if (outputResult instanceof Map) { Map mapResult = (Map) outputResult; - mapResult.put("status", "COMPLETED"); + mapResult.put(STATUS, COMPLETED); result = mapResult; } - else + else if(outputResult instanceof String) { - result.put("status", "IN-PROGRESS"); + result.put(STATUS, outputResult); } return result; } @@ -226,5 +216,4 @@ public class NodeFolderSizeRelation implements CalculateSize throw new AccessDeniedException("permissions.err_access_denied"); } } - } \ No newline at end of file diff --git a/remote-api/src/main/java/org/alfresco/rest/framework/resource/actions/interfaces/RelationshipResourceAction.java b/remote-api/src/main/java/org/alfresco/rest/framework/resource/actions/interfaces/RelationshipResourceAction.java index 40abf6a2f5..10c0e297b3 100644 --- a/remote-api/src/main/java/org/alfresco/rest/framework/resource/actions/interfaces/RelationshipResourceAction.java +++ b/remote-api/src/main/java/org/alfresco/rest/framework/resource/actions/interfaces/RelationshipResourceAction.java @@ -166,6 +166,9 @@ public interface RelationshipResourceAction interface CalculateSize extends ResourceAction { + String STATUS = "status"; + String COMPLETED = "Completed"; + String FOLDER = "folder"; /** * Calculate the size of Folder Node. * diff --git a/repository/src/main/java/org/alfresco/repo/action/executer/NodeSizeActionExecuter.java b/repository/src/main/java/org/alfresco/repo/action/executer/NodeSizeActionExecuter.java index d947ffdb81..abdea17083 100644 --- a/repository/src/main/java/org/alfresco/repo/action/executer/NodeSizeActionExecuter.java +++ b/repository/src/main/java/org/alfresco/repo/action/executer/NodeSizeActionExecuter.java @@ -64,13 +64,10 @@ public class NodeSizeActionExecuter extends ActionExecuterAbstractBase public static final String PAGE_SIZE = "page-size"; public static final String RESULT = "size-result"; public static final String ERROR = "exception"; - private SimpleCache simpleCache; - /** - * The node service - */ private NodeService nodeService; private SearchService searchService; + private SimpleCache simpleCache; /** * The logger @@ -97,6 +94,11 @@ public class NodeSizeActionExecuter extends ActionExecuterAbstractBase this.searchService = searchService; } + /** + * Set the simpleCache service + * + * @param simpleCache the cache service + */ public void setSimpleCache(SimpleCache simpleCache) { this.simpleCache = simpleCache;