From 267e4d06b0340c1ea0792623995b36b159dd63cc Mon Sep 17 00:00:00 2001 From: Britt Park Date: Tue, 31 Oct 2006 15:17:09 +0000 Subject: [PATCH] A more scalable implementation of getCommonAncestor. Performance is roughly proportional to the distance of the common ancestor from the compared nodes. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@4265 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../org/alfresco/repo/avm/AVMRepository.java | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/source/java/org/alfresco/repo/avm/AVMRepository.java b/source/java/org/alfresco/repo/avm/AVMRepository.java index f61bd2c879..5719aafb3c 100644 --- a/source/java/org/alfresco/repo/avm/AVMRepository.java +++ b/source/java/org/alfresco/repo/avm/AVMRepository.java @@ -1709,24 +1709,43 @@ public class AVMRepository throw new AVMNotFoundException("Node not found."); } List leftHistory = new ArrayList(); - while (lNode != null) - { - leftHistory.add(lNode); - lNode = lNode.getAncestor(); - } List rightHistory = new ArrayList(); - while (rNode != null) + while (lNode != null || rNode != null) { - rightHistory.add(rNode); - rNode = rNode.getAncestor(); - } - for (AVMNode l : leftHistory) - { - for (AVMNode r : rightHistory) + boolean checkRight = false; + if (lNode != null) { - if (l.equals(r)) + leftHistory.add(lNode); + checkRight = true; + lNode = lNode.getAncestor(); + } + boolean checkLeft = false; + if (rNode != null) + { + rightHistory.add(rNode); + checkLeft = true; + rNode = rNode.getAncestor(); + } + if (checkRight) + { + AVMNode check = leftHistory.get(leftHistory.size() - 1); + for (AVMNode node : rightHistory) { - return l.getDescriptor("", "", ""); + if (node.equals(check)) + { + return node.getDescriptor("", "", ""); + } + } + } + if (checkLeft) + { + AVMNode check = rightHistory.get(rightHistory.size() - 1); + for (AVMNode node : leftHistory) + { + if (node.equals(check)) + { + return node.getDescriptor("", "", ""); + } } } }