jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1532056 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/Namespaces.java oak-upgrade/src/main/java/org/apache/jackrabbit/oak/upgrade/RepositoryUpgrade.java
Date Mon, 14 Oct 2013 20:17:24 GMT
Author: jukka
Date: Mon Oct 14 20:17:24 2013
New Revision: 1532056

URL: http://svn.apache.org/r1532056
Log:
OAK-806: Content migration from Jackrabbit to Oak

Copy the namespace registry directly instead of going through the JCR API.
This way we can capture the stored index values and later on avoid extra lookups.

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/Namespaces.java
    jackrabbit/oak/trunk/oak-upgrade/src/main/java/org/apache/jackrabbit/oak/upgrade/RepositoryUpgrade.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/Namespaces.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/Namespaces.java?rev=1532056&r1=1532055&r2=1532056&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/Namespaces.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/Namespaces.java
Mon Oct 14 20:17:24 2013
@@ -16,6 +16,7 @@
 */
 package org.apache.jackrabbit.oak.plugins.name;
 
+import static com.google.common.base.Preconditions.checkState;
 import static javax.jcr.NamespaceRegistry.NAMESPACE_EMPTY;
 import static javax.jcr.NamespaceRegistry.NAMESPACE_JCR;
 import static javax.jcr.NamespaceRegistry.NAMESPACE_MIX;
@@ -58,11 +59,16 @@ public class Namespaces implements Names
     }
 
     public static void setupNamespaces(NodeBuilder system) {
-        if (system.hasChildNode(REP_NAMESPACES)) {
-            return;
+        if (!system.hasChildNode(REP_NAMESPACES)) {
+            NodeBuilder namespaces = createStandardMappings(system);
+            buildIndexNode(namespaces); // index node for faster lookup
         }
+    }
+
+    public static NodeBuilder createStandardMappings(NodeBuilder system) {
+        checkState(!system.hasChildNode(REP_NAMESPACES));
 
-        NodeBuilder namespaces = system.child(REP_NAMESPACES);
+        NodeBuilder namespaces = system.setChildNode(REP_NAMESPACES);
         namespaces.setProperty(JCR_PRIMARYTYPE, NT_UNSTRUCTURED, NAME);
 
         // Standard namespace specified by JCR (default one not included)
@@ -76,11 +82,35 @@ public class Namespaces implements Names
         namespaces.setProperty(PREFIX_SV, NAMESPACE_SV);
         namespaces.setProperty(PREFIX_REP, NAMESPACE_REP);
 
-        // index node for faster lookup
-        buildIndexNode(namespaces);
+        return namespaces;
+    }
+
+    public static String addCustomMapping(
+            NodeBuilder namespaces, String uri, String prefixHint) {
+        // first look for an existing mapping for the given URI
+        for (PropertyState property : namespaces.getProperties()) {
+            if (property.getType() == STRING) {
+                String prefix = unescapePropertyKey(property.getName());
+                if (isValidPrefix(prefix)
+                        && uri.equals(property.getValue(STRING))) {
+                    return prefix;
+                }
+            }
+        }
+
+        // no existing mapping found for the URI, make sure prefix is unique
+        String prefix = prefixHint;
+        int iteration = 1;
+        while (namespaces.hasProperty(escapePropertyKey(prefix))) {
+            prefix = prefixHint + ++iteration;
+        }
+
+        // add the new mapping with its unique prefix
+        namespaces.setProperty(escapePropertyKey(prefix), uri);
+        return prefix;
     }
 
-    static void buildIndexNode(NodeBuilder namespaces) {
+    public static void buildIndexNode(NodeBuilder namespaces) {
         Set<String> prefixes = new HashSet<String>();
         Set<String> uris = new HashSet<String>();
         Map<String, String> reverse = new HashMap<String, String>();

Modified: jackrabbit/oak/trunk/oak-upgrade/src/main/java/org/apache/jackrabbit/oak/upgrade/RepositoryUpgrade.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-upgrade/src/main/java/org/apache/jackrabbit/oak/upgrade/RepositoryUpgrade.java?rev=1532056&r1=1532055&r2=1532056&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-upgrade/src/main/java/org/apache/jackrabbit/oak/upgrade/RepositoryUpgrade.java
(original)
+++ jackrabbit/oak/trunk/oak-upgrade/src/main/java/org/apache/jackrabbit/oak/upgrade/RepositoryUpgrade.java
Mon Oct 14 20:17:24 2013
@@ -16,7 +16,9 @@
  */
 package org.apache.jackrabbit.oak.upgrade;
 
+import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.collect.Lists.newArrayListWithCapacity;
+import static com.google.common.collect.Maps.newHashMap;
 import static java.util.Arrays.asList;
 import static org.apache.jackrabbit.JcrConstants.JCR_AUTOCREATED;
 import static org.apache.jackrabbit.JcrConstants.JCR_CHILDNODEDEFINITION;
@@ -55,26 +57,28 @@ import static org.apache.jackrabbit.spi.
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.List;
-import java.util.Set;
+import java.util.Map;
+import java.util.Properties;
 
 import javax.jcr.NamespaceException;
-import javax.jcr.NamespaceRegistry;
 import javax.jcr.RepositoryException;
 import javax.jcr.version.OnParentVersionAction;
 
 import org.apache.jackrabbit.core.RepositoryContext;
 import org.apache.jackrabbit.core.RepositoryImpl;
 import org.apache.jackrabbit.core.config.RepositoryConfig;
+import org.apache.jackrabbit.core.fs.FileSystem;
+import org.apache.jackrabbit.core.fs.FileSystemException;
 import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
 import org.apache.jackrabbit.oak.api.Type;
-import org.apache.jackrabbit.oak.plugins.name.NamespaceConstants;
+import org.apache.jackrabbit.oak.plugins.name.Namespaces;
 import org.apache.jackrabbit.oak.plugins.nodetype.RegistrationEditorProvider;
 import org.apache.jackrabbit.oak.spi.commit.EditorHook;
 import org.apache.jackrabbit.oak.spi.commit.PostCommitHook;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeStore;
-import org.apache.jackrabbit.oak.spi.state.NodeStoreBranch;
 import org.apache.jackrabbit.spi.Name;
 import org.apache.jackrabbit.spi.QItemDefinition;
 import org.apache.jackrabbit.spi.QNodeDefinition;
@@ -85,8 +89,6 @@ import org.apache.jackrabbit.spi.QValueC
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.ImmutableSet;
-
 public class RepositoryUpgrade {
 
     /**
@@ -170,10 +172,10 @@ public class RepositoryUpgrade {
         try {
             NodeBuilder builder = target.getRoot().builder();
 
-            copyNamespaces(builder);
+            Map<Integer, String> idxToPrefix = copyNamespaces(builder);
             copyNodeTypes(builder);
-            copyVersionStore(builder);
-            copyWorkspaces(builder);
+            copyVersionStore(builder, idxToPrefix);
+            copyWorkspaces(builder, idxToPrefix);
 
             target.merge(builder, new EditorHook(new RegistrationEditorProvider()), PostCommitHook.EMPTY);
// TODO: default hooks?
         } catch (Exception e) {
@@ -191,25 +193,79 @@ public class RepositoryUpgrade {
         }
     }
 
-    private void copyNamespaces(NodeBuilder root) throws RepositoryException {
-        NamespaceRegistry sourceRegistry = source.getNamespaceRegistry();
+    /**
+     * Copies the registered namespaces to the target repository, and returns
+     * the internal namespace index mapping used in bundle serialization.
+     *
+     * @param root root builder
+     * @return index to prefix mapping
+     * @throws RepositoryException
+     */
+    private Map<Integer, String> copyNamespaces(NodeBuilder root)
+            throws RepositoryException {
+        Map<Integer, String> idxToPrefix = newHashMap();
+
         NodeBuilder system = root.child(JCR_SYSTEM);
-        NodeBuilder namespaces = system.child("rep:namespaces");
+        NodeBuilder namespaces = Namespaces.createStandardMappings(system);
 
-        Set<String> defaults = ImmutableSet.of(
-                NamespaceRegistry.NAMESPACE_EMPTY,
-                NamespaceRegistry.NAMESPACE_JCR,
-                NamespaceRegistry.NAMESPACE_MIX,
-                NamespaceRegistry.NAMESPACE_NT,
-                NamespaceRegistry.NAMESPACE_XML,
-                NamespaceConstants.NAMESPACE_SV,
-                NamespaceConstants.NAMESPACE_REP);
-        logger.info("Copying registered namespaces");
-        for (String uri : sourceRegistry.getURIs()) {
-            if (!defaults.contains(uri)) {
-                namespaces.setProperty(sourceRegistry.getPrefix(uri), uri);
+        Properties registry = loadProperties("/namespaces/ns_reg.properties");
+        Properties indexes  = loadProperties("/namespaces/ns_idx.properties");
+
+        for (String prefixHint : registry.stringPropertyNames()) {
+            String uri = registry.getProperty(prefixHint);
+            if (".empty.key".equals(prefixHint)) {
+                prefixHint = "";
+            }
+
+            String prefix =
+                    Namespaces.addCustomMapping(namespaces, uri, prefixHint);
+
+            String index = null;
+            if (uri.isEmpty()) {
+                index = indexes.getProperty(".empty.key");
+            }
+            if (index == null) {
+                index = indexes.getProperty(uri);
             }
+
+            Integer idx;
+            if (index != null) {
+                idx = Integer.decode(index);
+            } else {
+                int i = 0;
+                do {
+                    idx = (uri.hashCode() + i++) & 0x00ffffff;
+                } while (idxToPrefix.containsKey(idx));
+            }
+
+            checkState(idxToPrefix.put(idx, prefix) == null);
         }
+
+        Namespaces.buildIndexNode(namespaces);
+
+        return idxToPrefix;
+    }
+
+    private Properties loadProperties(String path) throws RepositoryException {
+        Properties properties = new Properties();
+
+        FileSystem filesystem = source.getFileSystem();
+        try {
+            if (filesystem.exists(path)) {
+                InputStream stream = filesystem.getInputStream(path);
+                try {
+                    properties.load(stream);
+                } finally {
+                    stream.close();
+                }
+            }
+        } catch (FileSystemException e) {
+            throw new RepositoryException(e);
+        } catch (IOException e) {
+            throw new RepositoryException(e);
+        }
+
+        return properties;
     }
 
     private void copyNodeTypes(NodeBuilder root) throws RepositoryException {
@@ -354,7 +410,8 @@ public class RepositoryUpgrade {
         builder.setProperty(JCR_PROTECTED, def.isProtected());
     }
 
-    private void copyVersionStore(NodeBuilder root)
+    private void copyVersionStore(
+            NodeBuilder root, Map<Integer, String> idxToPrefix)
             throws RepositoryException, IOException {
         logger.info("Copying version histories");
         NodeBuilder system = root.child(JCR_SYSTEM);
@@ -368,7 +425,8 @@ public class RepositoryUpgrade {
         copier.copy(RepositoryImpl.ACTIVITIES_NODE_ID, activities);
     }   
 
-    private void copyWorkspaces(NodeBuilder root)
+    private void copyWorkspaces(
+            NodeBuilder root, Map<Integer, String> idxToPrefix)
             throws RepositoryException, IOException {
         logger.info("Copying default workspace");
 



Mime
View raw message