jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Fabián Mandelbaum <fmandelb...@gmail.com>
Subject Re: removing versionable nodes permanently (so the data store gc could do its job)
Date Fri, 20 Aug 2010 16:42:41 GMT
Hello Robert, you can try something like the following. I've copied
code from my app verbatim, adding comments for you, look for /*
ROBERT: ... */

If my app-specific code (for example the Workspace class, some DAOs,
etc.) confuse you, please let me know and I'll clean it up for you,
leaving only the code you are interested into.

This is tested and working on JackRabbit 1.6.2. There shouldn't be
differences with JR 2.x or even 1.4.x. Hope this helps.

    /**
     * WorkspaceDeleter deletes from the content repository:
     * <ul>
     *   <li>All files and their revisions</li>
     *   <li>The workspace itself (with all its classifications,
associations, etc.</li>
     * </ul>
     */
    class WorkspaceDeleter extends Thread {
        Workspace w;
        String wname;

        public WorkspaceDeleter(Workspace workspace) {
            this.w = workspace;
            this.wname = workspace.getName();
            /**
             * Avoid overloading the server. Useful when the workspace has
             * lots of files and/or revisions stored
             */
            setPriority(MIN_PRIORITY);
        }

        @Override
        public void run() {
            long lstart = System.currentTimeMillis();
            Session session = null;
            try {
                session = repoMgr.getSessionRW();
                // Intl files+revisions
                /* ROBERT: This is the part you are interested in */
                NodeIterator nit =
session.getWorkspace().getQueryManager().createQuery("/jcr:root/workspaces/"
+ wname + "/content/*[@jcr:primaryType='nt:file']",
Query.XPATH).execute().getNodes();
                while (nit.hasNext()) {
                    Node node = nit.nextNode();
                    String href = node.getPath();
                    getLogger().fine(String.format("Processing file
stored at %s ...", href));
                    VersionHistory vh =
node.getNode("jcr:content").getVersionHistory();
                    node.remove();
                    session.save();
                    for (VersionIterator vit = vh.getAllVersions();
vit.hasNext(); ) {
                        Version v = (Version)vit.next();
                        String vname = v.getName();
                        if (!"jcr:rootVersion".equals(vname)) {
                            vh.removeVersion(vname);
                            getLogger().fine(String.format("Deleted
version %s", vname));
                        }
                    }
                }
                // Specific lang files+revisions
                /* ROBERT: This is the part you are interested in,
same as above but for different files */
                nit =
session.getWorkspace().getQueryManager().createQuery("/jcr:root/workspaces/"
+ wname + "/content/*/*[@jcr:primaryType='nt:file']",
Query.XPATH).execute().getNodes();
                while (nit.hasNext()) {
                    Node node = nit.nextNode();
                    String href = node.getPath();
                    getLogger().fine(String.format("Processing file
stored at %s ...", href));
                    VersionHistory vh =
node.getNode("jcr:content").getVersionHistory();
                    node.remove();
                    session.save();
                    for (VersionIterator vit = vh.getAllVersions();
vit.hasNext(); ) {
                        Version v = (Version)vit.next();
                        String vname = v.getName();
                        if (!"jcr:rootVersion".equals(vname)) {
                            vh.removeVersion(vname);
                            getLogger().fine(String.format("Deleted
version %s", vname));
                        }
                    }
                }
                // Wipe workspace
                /* ROBERT: This deletes the container of those files,
which makes sure that the root version is also deleted because its
references is gone */
                WorkspaceDAO dao = new WorkspaceDAO(session);
                dao.remove(w);
            } catch (Exception e) {
                getLogger().warning(String.format("Caught exception
when deleting workspace %s: %s", wname, e));
            } finally {
                repoMgr.releaseSessionRW(session);
                long lend = System.currentTimeMillis();
                getLogger().info(String.format("Workspace %s deleted
in %2.3f sec.", wname, (lend - lstart) / 1000.0));
            }
        }
    }

On Fri, Aug 20, 2010 at 1:21 PM, Seidel. Robert <Robert.Seidel@aeb.de> wrote:
> Hi,
>
> how can I remove a versionable node permanently, so the data store garbage collector
can grab the referenced large binary and delete it?
>
> I've tried to remove the versions (but I can't remove the root version) -> didn't
work
> I've tried to simply remove the node -> didn't work
>
> Kindly Regards,
>
> Robert Seidel
>



-- 
Fabián Mandelbaum
IS Engineer

Mime
View raw message