You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2010/03/09 12:19:01 UTC

svn commit: r920806 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core: RepositoryChecker.java RepositoryImpl.java

Author: jukka
Date: Tue Mar  9 11:19:01 2010
New Revision: 920806

URL: http://svn.apache.org/viewvc?rev=920806&view=rev
Log:
JCR-2551: Recovery from a lost version history

Add a very simple recovery mechanims that's triggered by setting the org.apache.jackrabbit.version.recovery system property to true. Work in progress...

Added:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryChecker.java   (with props)
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryChecker.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryChecker.java?rev=920806&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryChecker.java (added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryChecker.java Tue Mar  9 11:19:01 2010
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core;
+
+import static org.apache.jackrabbit.spi.commons.name.NameConstants.JCR_BASEVERSION;
+import static org.apache.jackrabbit.spi.commons.name.NameConstants.JCR_PREDECESSORS;
+import static org.apache.jackrabbit.spi.commons.name.NameConstants.JCR_VERSIONHISTORY;
+import static org.apache.jackrabbit.spi.commons.name.NameConstants.MIX_VERSIONABLE;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.jcr.RepositoryException;
+
+import org.apache.jackrabbit.core.id.NodeId;
+import org.apache.jackrabbit.core.id.PropertyId;
+import org.apache.jackrabbit.core.persistence.PersistenceManager;
+import org.apache.jackrabbit.core.state.ChangeLog;
+import org.apache.jackrabbit.core.state.ChildNodeEntry;
+import org.apache.jackrabbit.core.state.ItemStateException;
+import org.apache.jackrabbit.core.state.NodeState;
+import org.apache.jackrabbit.core.version.InternalVersionManager;
+import org.apache.jackrabbit.spi.Name;
+
+/**
+ * Tool for checking for and optionally fixing consistency issues in a
+ * repository. Currently this class only contains a simple versioning
+ * recovery feature for
+ * <a href="https://issues.apache.org/jira/browse/JCR-2551">JCR-2551</a>.
+ */
+class RepositoryChecker {
+
+    private final PersistenceManager workspace;
+
+    private final ChangeLog workspaceChanges;
+
+    private final InternalVersionManager versionManager;
+
+    public RepositoryChecker(
+            PersistenceManager workspace,
+            InternalVersionManager versionManager) {
+        this.workspace = workspace;
+        this.workspaceChanges = new ChangeLog();
+        this.versionManager = versionManager;
+    }
+
+    public void check(NodeId id, boolean recurse)
+            throws RepositoryException {
+        try {
+            NodeState state = workspace.load(id);
+            checkVersionHistory(state);
+
+            if (recurse) {
+                for (ChildNodeEntry child : state.getChildNodeEntries()) {
+                    if (child.getId() != RepositoryImpl.SYSTEM_ROOT_NODE_ID) {
+                        check(child.getId(), recurse);
+                    }
+                }
+            }
+        } catch (ItemStateException e) {
+            throw new RepositoryException("Unable to access node " + id, e);
+        }
+    }
+
+    public void fix() throws RepositoryException {
+        System.out.println("Fixing workspace issues");
+        if (workspaceChanges.hasUpdates()) {
+            System.out.println("... workspace has issues");
+            try {
+                workspace.store(workspaceChanges);
+            } catch (ItemStateException e) {
+                e.printStackTrace();
+                throw new RepositoryException(
+                        "Failed to fix workspace inconsistencies", e);
+            }
+        }
+    }
+
+    private void checkVersionHistory(NodeState node) {
+        if (node.hasPropertyName(JCR_VERSIONHISTORY)) {
+            System.out.println("Checking version history of node " + node.getNodeId());
+            try {
+                versionManager.getVersionHistoryOfNode(node.getNodeId());
+            } catch (Exception e) {
+                e.printStackTrace();
+                removeVersionHistoryReferences(node);
+            }
+        }
+    }
+
+    private void removeVersionHistoryReferences(NodeState node) {
+        NodeState modified =
+            new NodeState(node, NodeState.STATUS_EXISTING_MODIFIED, true);
+
+        Set<Name> mixins = new HashSet<Name>(node.getMixinTypeNames());
+        if (mixins.remove(MIX_VERSIONABLE)) {
+            modified.setMixinTypeNames(mixins);
+        }
+
+        removeProperty(modified, JCR_VERSIONHISTORY);
+        removeProperty(modified, JCR_BASEVERSION);
+        removeProperty(modified, JCR_PREDECESSORS);
+
+        workspaceChanges.modified(modified);
+    }
+
+    private void removeProperty(NodeState node, Name name) {
+        if (node.hasPropertyName(name)) {
+            node.removePropertyName(name);
+            try {
+                workspaceChanges.deleted(workspace.load(
+                        new PropertyId(node.getNodeId(), name)));
+            } catch (ItemStateException ignoe) {
+            }
+        }
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryChecker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java?rev=920806&r1=920805&r2=920806&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java Tue Mar  9 11:19:01 2010
@@ -2016,6 +2016,14 @@ public class RepositoryImpl extends Abst
                     ntReg,
                     dataStore);
 
+            // JCR-2551: Recovery from a lost version history
+            if (Boolean.getBoolean("org.apache.jackrabbit.version.recovery")) {
+                RepositoryChecker checker =
+                    new RepositoryChecker(persistMgr, vMgr);
+                checker.check(ROOT_NODE_ID, true);
+                checker.fix();
+            }
+
             ISMLocking ismLocking = config.getISMLocking();
 
             // create item state manager