Author: tolusha
Date: 2011-12-28 10:47:47 -0500 (Wed, 28 Dec 2011)
New Revision: 5380
Modified:
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/distribution/impl/DataDistributionByName.java
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/hierarchy/impl/NodeHierarchyCreatorImpl.java
Log:
EXOJCR-1688: Upgrading JCR user directory structure doesnot consumes a lot of memory
Modified:
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/distribution/impl/DataDistributionByName.java
===================================================================
---
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/distribution/impl/DataDistributionByName.java 2011-12-28
15:45:30 UTC (rev 5379)
+++
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/distribution/impl/DataDistributionByName.java 2011-12-28
15:47:47 UTC (rev 5380)
@@ -21,6 +21,7 @@
import org.exoplatform.services.jcr.impl.core.NodeImpl;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -45,17 +46,17 @@
*/
public class DataDistributionByName extends AbstractDataDistributionType
{
-
+
/**
* The level of depth used by the algorithm
*/
private int depth = 4;
-
+
/**
* The suffix used by the algorithm to indicate that they are sub nodes inside
*/
private String suffix = "___";
-
+
/**
* {@inheritDoc}
*/
@@ -98,57 +99,48 @@
public void migrate(Node rootNode, String nodeType, List<String> mixinTypes,
Map<String, String[]> permissions)
throws RepositoryException
{
- // try to detect if migration is needed
- NodeIterator iter = ((NodeImpl)rootNode).getNodesLazily(1);
+ NodeIterator iter = ((NodeImpl)rootNode).getNodesLazily();
while (iter.hasNext())
{
- String userName = iter.nextNode().getName();
- if (userName.length() == 1)
- {
- continue;
- }
- else if (userName.endsWith(suffix) || (!iter.hasNext()))
- {
- return;
- }
- else
- {
- break;
- }
- }
-
- iter = ((NodeImpl)rootNode).getNodesLazily();
- while (iter.hasNext())
- {
Node userNode = iter.nextNode();
- if (userNode.getName().length() == 1)
- {
- continue;
- }
- List<String> ancestors = getAncestors(userNode.getName());
- Node node = rootNode;
-
- for (int i = 0, length = ancestors.size() - 1; i < length; i++)
+ if (!alreadyMigrated(userNode))
{
- String nodeName = ancestors.get(i);
- try
+ Node ancestorNode = rootNode;
+
+ Iterator<String> ancestors =
getAncestors(userNode.getName()).iterator();
+ while (ancestors.hasNext())
{
- node = node.getNode(nodeName);
- continue;
+ String ancestorName = ancestors.next();
+
+ if (ancestors.hasNext())
+ {
+ try
+ {
+ ancestorNode = ancestorNode.getNode(ancestorName);
+ continue;
+ }
+ catch (PathNotFoundException e)
+ {
+ ancestorNode =
+ createNode(ancestorNode, ancestorName, nodeType, mixinTypes,
permissions, false, false);
+ }
+ }
+ else
+ {
+ rootNode.getSession().move(userNode.getPath(), ancestorNode.getPath() +
"/" + ancestorName);
+ }
}
- catch (PathNotFoundException e)
- {
- // ignore me
- }
- // The node doesn't exist we need to create it
- node = createNode(node, nodeName, nodeType, mixinTypes, permissions, false,
false);
+ rootNode.getSession().save();
}
-
- userNode.getSession().move(userNode.getPath(), node.getPath() + "/" +
ancestors.get(ancestors.size() - 1));
}
+ }
- rootNode.getSession().save();
+ private boolean alreadyMigrated(Node userNode) throws RepositoryException
+ {
+ String nodeName = userNode.getName();
+
+ return nodeName.length() == 1 || nodeName.endsWith(suffix);
}
}
Modified:
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/hierarchy/impl/NodeHierarchyCreatorImpl.java
===================================================================
---
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/hierarchy/impl/NodeHierarchyCreatorImpl.java 2011-12-28
15:45:30 UTC (rev 5379)
+++
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/hierarchy/impl/NodeHierarchyCreatorImpl.java 2011-12-28
15:47:47 UTC (rev 5380)
@@ -122,14 +122,17 @@
log.error("An error occurs while processing the plugins", e);
}
- try
+ if (isNeededToMigrate())
{
- checkForUpgrade();
+ try
+ {
+ migrate();
+ }
+ catch (RepositoryException e)
+ {
+ log.error("An error occurs while upgrading JCR structure", e);
+ }
}
- catch (RepositoryException e)
- {
- log.error("An error occurs while upgrading JCR structure", e);
- }
}
/**
@@ -309,9 +312,10 @@
*/
public Node getUserNode(SessionProvider sessionProvider, String userName) throws
Exception
{
- String userPath = getJcrPath(USERS_PATH);
Session session = getSession(sessionProvider);
- Node usersNode = (Node)session.getItem(userPath);
+
+ Node usersNode = getRootOfUsersNodes(session);
+
DataDistributionType type =
dataDistributionManager_.getDataDistributionType(oldDistribution ?
DataDistributionMode.NONE
: DataDistributionMode.READABLE);
@@ -323,11 +327,11 @@
*/
public void removeUserNode(SessionProvider sessionProvider, String userName) throws
Exception
{
- String userPath = getJcrPath(USERS_PATH);
Session session = getSession(sessionProvider);
try
{
- Node usersNode = (Node)session.getItem(userPath);
+ Node usersNode = getRootOfUsersNodes(session);
+
DataDistributionType type =
dataDistributionManager_.getDataDistributionType(oldDistribution ?
DataDistributionMode.NONE
: DataDistributionMode.READABLE);
@@ -375,26 +379,29 @@
}
}
- private void checkForUpgrade() throws RepositoryException
+ private boolean isNeededToMigrate()
{
- if (!oldDistribution && autoMigrate)
+ return !oldDistribution && autoMigrate;
+ }
+
+ private void migrate() throws RepositoryException
+ {
+ Session session = getSession(SessionProvider.createSystemProvider());
+
+ try
{
- ManageableRepository repo = jcrService_.getCurrentRepository();
- Session session =
repo.getSystemSession(repo.getConfiguration().getDefaultWorkspaceName());
- try
- {
- String userPath = getJcrPath(USERS_PATH);
- Node usersNode = (Node)session.getItem(userPath);
-
-
dataDistributionManager_.getDataDistributionType(DataDistributionMode.READABLE).migrate(usersNode);
- }
- finally
- {
- if (session != null)
- {
- session.logout();
- }
- }
+ Node rootNode = getRootOfUsersNodes(session);
+
dataDistributionManager_.getDataDistributionType(DataDistributionMode.READABLE).migrate(rootNode);
}
+ finally
+ {
+ session.logout();
+ }
}
+
+ private Node getRootOfUsersNodes(Session session) throws PathNotFoundException,
RepositoryException
+ {
+ String usersPath = getJcrPath(USERS_PATH);
+ return (Node)session.getItem(usersPath);
+ }
}
Show replies by date