Make copy more efficient
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 14 Nov 2017 08:07:01 +0000 (09:07 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 14 Nov 2017 08:07:01 +0000 (09:07 +0100)
org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/commands/MergeWorkspaces.java
org.argeo.slc.repo/src/org/argeo/slc/repo/RepoSync.java

index 965e5348f1f9a1e5c2a88edf3d57b303c80bab5f..39d5495f1e695b3a95d549b529bdc2304fe429c7 100644 (file)
@@ -69,16 +69,12 @@ public class MergeWorkspaces extends AbstractHandler {
                try {
                        nodeSession = nodeRepository.login();
                        Node srcRepoNode = nodeSession.getNode(sourceRepoPath);
-                       Repository srcRepository = RepoUtils.getRepository(
-                                       repositoryFactory, keyring, srcRepoNode);
-                       Credentials srcCredentials = RepoUtils.getRepositoryCredentials(
-                                       keyring, srcRepoNode);
+                       Repository srcRepository = RepoUtils.getRepository(repositoryFactory, keyring, srcRepoNode);
+                       Credentials srcCredentials = RepoUtils.getRepositoryCredentials(keyring, srcRepoNode);
 
                        Node targetRepoNode = nodeSession.getNode(targetRepoPath);
-                       Repository targetRepository = RepoUtils.getRepository(
-                                       repositoryFactory, keyring, targetRepoNode);
-                       Credentials targetCredentials = RepoUtils.getRepositoryCredentials(
-                                       keyring, targetRepoNode);
+                       Repository targetRepository = RepoUtils.getRepository(repositoryFactory, keyring, targetRepoNode);
+                       Credentials targetCredentials = RepoUtils.getRepositoryCredentials(keyring, targetRepoNode);
 
                        // String msg = "Are you sure you want to merge distribution ["
                        // + sourceWkspName + "] in distribution [" + targetWkspName
@@ -91,35 +87,27 @@ public class MergeWorkspaces extends AbstractHandler {
                        // Open sessions here since the background thread
                        // won't necessarily be authenticated.
                        // Job should close the sessions.
-                       Session sourceSession = srcRepository.login(srcCredentials,
-                                       sourceWkspName);
+                       Session sourceSession = srcRepository.login(srcCredentials, sourceWkspName);
                        Session targetSession;
                        try {
-                               targetSession = targetRepository.login(targetCredentials,
-                                               targetWkspName);
+                               targetSession = targetRepository.login(targetCredentials, targetWkspName);
                        } catch (NoSuchWorkspaceException e) {
-                               Session defaultSession = targetRepository
-                                               .login(targetCredentials);
+                               Session defaultSession = targetRepository.login(targetCredentials);
                                try {
-                                       defaultSession.getWorkspace().createWorkspace(
-                                                       targetWkspName);
+                                       defaultSession.getWorkspace().createWorkspace(targetWkspName);
                                } catch (Exception e1) {
-                                       throw new SlcException("Cannot create new workspace "
-                                                       + targetWkspName, e);
+                                       throw new SlcException("Cannot create new workspace " + targetWkspName, e);
                                } finally {
                                        JcrUtils.logoutQuietly(defaultSession);
                                }
-                               targetSession = targetRepository.login(targetCredentials,
-                                               targetWkspName);
+                               targetSession = targetRepository.login(targetCredentials, targetWkspName);
                        }
 
-                       Job workspaceMergeJob = new WorkspaceMergeJob(sourceSession,
-                                       targetSession);
+                       Job workspaceMergeJob = new WorkspaceMergeJob(sourceSession, targetSession);
                        workspaceMergeJob.setUser(true);
                        workspaceMergeJob.schedule();
                } catch (RepositoryException re) {
-                       throw new SlcException(
-                                       "Unexpected error while merging workspaces.", re);
+                       throw new SlcException("Unexpected error while merging workspaces.", re);
                } finally {
                        JcrUtils.logoutQuietly(nodeSession);
                }
@@ -140,34 +128,28 @@ public class MergeWorkspaces extends AbstractHandler {
                protected IStatus run(IProgressMonitor eclipseMonitor) {
                        long begin = System.currentTimeMillis();
                        try {
-                               Query countQuery = sourceSession
-                                               .getWorkspace()
-                                               .getQueryManager()
-                                               .createQuery("select file from [nt:file] as file",
-                                                               Query.JCR_SQL2);
+                               Query countQuery = sourceSession.getWorkspace().getQueryManager()
+                                               .createQuery("select file from [nt:file] as file", Query.JCR_SQL2);
                                QueryResult result = countQuery.execute();
                                Long expectedCount = result.getNodes().getSize();
                                if (log.isDebugEnabled())
                                        log.debug("Will copy " + expectedCount + " files...");
 
                                JcrMonitor monitor = new EclipseJcrMonitor(eclipseMonitor);
-                               eclipseMonitor
-                                               .beginTask("Copy files", expectedCount.intValue());
+                               eclipseMonitor.beginTask("Copy files", expectedCount.intValue());
 
-                               Long count = JcrUtils.copyFiles(sourceSession.getRootNode(),
-                                               targetSession.getRootNode(), true, monitor);
+                               Long count = JcrUtils.copyFiles(sourceSession.getRootNode(), targetSession.getRootNode(), true, monitor,
+                                               true);
 
                                monitor.done();
                                long duration = (System.currentTimeMillis() - begin) / 1000;// in
                                                                                                                                                        // s
                                if (log.isDebugEnabled())
-                                       log.debug("Copied " + count + " files in "
-                                                       + (duration / 60) + "min " + (duration % 60) + "s");
+                                       log.debug("Copied " + count + " files in " + (duration / 60) + "min " + (duration % 60) + "s");
 
                                return Status.OK_STATUS;
                        } catch (RepositoryException e) {
-                               return new Status(IStatus.ERROR, DistPlugin.PLUGIN_ID,
-                                               "Cannot merge", e);
+                               return new Status(IStatus.ERROR, DistPlugin.PLUGIN_ID, "Cannot merge", e);
                        } finally {
                                JcrUtils.logoutQuietly(sourceSession);
                                JcrUtils.logoutQuietly(targetSession);
index ba6281917505dc631bdd7647206d2c77b9edba5c..38fc23a343af693af82f26eeb0854eb0757550c5 100644 (file)
@@ -213,16 +213,28 @@ public class RepoSync implements Runnable {
                        if (log.isDebugEnabled())
                                log.debug(msg);
 
-                       if (filesOnly) {
-                               JcrUtils.copyFiles(sourceSession.getRootNode(), targetSession.getRootNode(), true, monitor);
-                       } else {
-                               for (NodeIterator it = sourceSession.getRootNode().getNodes(); it.hasNext();) {
-                                       Node node = it.nextNode();
-                                       if (node.getName().equals("jcr:system"))
-                                               continue;
-                                       syncNode(node, targetSession);
-                               }
+                       for (NodeIterator it = sourceSession.getRootNode().getNodes(); it.hasNext();) {
+                               Node node = it.nextNode();
+                               if (node.getName().contains(":"))
+                                       continue;
+                               if (node.getName().equals("download"))
+                                       continue;
+                               if (!node.isNodeType(NodeType.NT_HIERARCHY_NODE))
+                                       continue;
+                               syncNode(node, targetSession);
                        }
+                       // if (filesOnly) {
+                       // JcrUtils.copyFiles(sourceSession.getRootNode(), targetSession.getRootNode(),
+                       // true, monitor);
+                       // } else {
+                       // for (NodeIterator it = sourceSession.getRootNode().getNodes(); it.hasNext();)
+                       // {
+                       // Node node = it.nextNode();
+                       // if (node.getName().equals("jcr:system"))
+                       // continue;
+                       // syncNode(node, targetSession);
+                       // }
+                       // }
                        if (log.isDebugEnabled())
                                log.debug("Synced " + sourceSession.getWorkspace().getName());
                } catch (Exception e) {
@@ -238,6 +250,15 @@ public class RepoSync implements Runnable {
        }
 
        protected void syncNode(Node sourceNode, Session targetSession) throws RepositoryException, SAXException {
+               if (filesOnly) {
+                       Node targetNode;
+                       if (targetSession.itemExists(sourceNode.getPath()))
+                               targetNode = targetSession.getNode(sourceNode.getPath());
+                       else
+                               targetNode = JcrUtils.mkdirs(targetSession, sourceNode.getPath(), NodeType.NT_FOLDER);
+                       JcrUtils.copyFiles(sourceNode, targetNode, true, monitor, true);
+                       return;
+               }
                // Boolean singleLevel = singleLevel(sourceNode);
                try {
                        if (monitor != null && monitor.isCanceled()) {
@@ -494,8 +515,7 @@ public class RepoSync implements Runnable {
        }
 
        /**
-        * Synchronises only one workspace, retrieved by name without changing its
-        * name.
+        * Synchronises only one workspace, retrieved by name without changing its name.
         */
        public void setSourceWksp(String sourceWksp) {
                if (sourceWksp != null && !sourceWksp.trim().equals("")) {
@@ -507,8 +527,8 @@ public class RepoSync implements Runnable {
 
        /**
         * Synchronises a map of workspaces that will be retrieved by name. If the
-        * target name is not defined (eg null or an empty string) for a given
-        * source workspace, we use the source name as target name.
+        * target name is not defined (eg null or an empty string) for a given source
+        * workspace, we use the source name as target name.
         */
        public void setWkspMap(Map<String, String> workspaceMap) {
                // clean the list to ease later use