jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r486099 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core: ./ query/ query/lucene/
Date Tue, 12 Dec 2006 11:18:39 GMT
Author: mreutegg
Date: Tue Dec 12 03:18:37 2006
New Revision: 486099

URL: http://svn.apache.org/viewvc?view=rev&rev=486099
Log:
JCR-669: Move NamespaceMappings/Index from lucene to namespace registry

Added:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/FileBasedNamespaceMappings.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NSRegistryBasedNamespaceMappings.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SearchManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryHandlerContext.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NamespaceMappings.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SearchManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SearchManager.java?view=diff&rev=486099&r1=486098&r2=486099
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SearchManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SearchManager.java
Tue Dec 12 03:18:37 2006
@@ -30,8 +30,6 @@
 import org.apache.jackrabbit.core.state.ItemStateManager;
 import org.apache.jackrabbit.core.state.NodeState;
 import org.apache.jackrabbit.core.state.NodeStateIterator;
-import org.apache.jackrabbit.name.AbstractNamespaceResolver;
-import org.apache.jackrabbit.name.NamespaceResolver;
 import org.apache.jackrabbit.name.NoPrefixDeclaredException;
 import org.apache.jackrabbit.name.Path;
 import org.apache.jackrabbit.name.PathFormat;
@@ -39,7 +37,6 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.jcr.NamespaceException;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.observation.Event;
@@ -150,9 +147,9 @@
     private final QueryHandler parentHandler;
 
     /**
-     * Namespace resolver that is based on the namespace registry itself.
+     * The namespace registry of the repository.
      */
-    private final NamespaceResolver nsResolver;
+    private final NamespaceRegistryImpl nsReg;
 
     /**
      * ID of the node that should be excluded from indexing or <code>null</code>
@@ -223,27 +220,11 @@
         }
         this.config = config;
         this.ntReg = ntReg;
+        this.nsReg = nsReg;
         this.itemMgr = itemMgr;
         this.rootNodeId = rootNodeId;
         this.parentHandler = (parentMgr != null) ? parentMgr.handler : null;
         this.excludedNodeId = excludedNodeId;
-        this.nsResolver = new AbstractNamespaceResolver() {
-            public String getURI(String prefix) throws NamespaceException {
-                try {
-                    return nsReg.getURI(prefix);
-                } catch (RepositoryException e) {
-                    throw new NamespaceException(e.getMessage());
-                }
-            }
-
-            public String getPrefix(String uri) throws NamespaceException {
-                try {
-                    return nsReg.getPrefix(uri);
-                } catch (RepositoryException e) {
-                    throw new NamespaceException(e.getMessage());
-                }
-            }
-        };
 
         // register namespaces
         nsReg.safeRegisterNamespace(NS_XS_PREFIX, NS_XS_URI);
@@ -259,7 +240,7 @@
         }
 
         if (excludedNodeId != null) {
-            HierarchyManagerImpl hmgr = new HierarchyManagerImpl(rootNodeId, itemMgr, nsResolver);
+            HierarchyManagerImpl hmgr = new HierarchyManagerImpl(rootNodeId, itemMgr, nsReg);
             excludePath = hmgr.getPath(excludedNodeId);
         }
 
@@ -363,7 +344,7 @@
         String exclude = "";
         if (excludePath != null) {
             try {
-                exclude = PathFormat.format(excludePath, nsResolver);
+                exclude = PathFormat.format(excludePath, nsReg);
             } catch (NoPrefixDeclaredException e) {
                 log.error("Error filtering events.", e);
             }
@@ -519,7 +500,7 @@
             handler = (QueryHandler) config.newInstance();
             QueryHandlerContext context
                     = new QueryHandlerContext(fs, itemMgr, rootNodeId,
-                            ntReg, parentHandler, excludedNodeId);
+                            ntReg, nsReg, parentHandler, excludedNodeId);
             handler.init(context);
         } catch (Exception e) {
             throw new RepositoryException(e.getMessage(), e);

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryHandlerContext.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryHandlerContext.java?view=diff&rev=486099&r1=486098&r2=486099
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryHandlerContext.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryHandlerContext.java
Tue Dec 12 03:18:37 2006
@@ -20,6 +20,7 @@
 import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
 import org.apache.jackrabbit.core.state.ItemStateManager;
 import org.apache.jackrabbit.core.NodeId;
+import org.apache.jackrabbit.core.NamespaceRegistryImpl;
 
 /**
  * Acts as an argument for the {@link QueryHandler} to keep the interface
@@ -44,6 +45,11 @@
     private final NodeTypeRegistry ntRegistry;
 
     /**
+     * The namespace registry of the repository.
+     */
+    private final NamespaceRegistryImpl nsRegistry;
+
+    /**
      * The id of the root node.
      */
     private NodeId rootId;
@@ -73,6 +79,7 @@
      * @param stateMgr         provides persistent item states.
      * @param rootId           the id of the root node.
      * @param ntRegistry       the node type registry.
+     * @param nsRegistry       the namespace registry.
      * @param parentHandler    the parent query handler or <code>null</code>
it
      *                         there is no parent handler.
      * @param excludedNodeId   id of the node that should be excluded from
@@ -83,12 +90,14 @@
                                ItemStateManager stateMgr,
                                NodeId rootId,
                                NodeTypeRegistry ntRegistry,
+                               NamespaceRegistryImpl nsRegistry,
                                QueryHandler parentHandler,
                                NodeId excludedNodeId) {
         this.fs = fs;
         this.stateMgr = stateMgr;
         this.rootId = rootId;
         this.ntRegistry = ntRegistry;
+        this.nsRegistry = nsRegistry;
         propRegistry = new PropertyTypeRegistry(ntRegistry);
         this.parentHandler = parentHandler;
         this.excludedNodeId = excludedNodeId;
@@ -140,6 +149,14 @@
      */
     public NodeTypeRegistry getNodeTypeRegistry() {
         return ntRegistry;
+    }
+
+    /**
+     * Returns the NamespaceRegistryImpl for this repository.
+     * @return the NamespaceRegistryImpl for this repository.
+     */
+    public NamespaceRegistryImpl getNamespaceRegistry() {
+        return nsRegistry;
     }
 
     /**

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/FileBasedNamespaceMappings.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/FileBasedNamespaceMappings.java?view=auto&rev=486099
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/FileBasedNamespaceMappings.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/FileBasedNamespaceMappings.java
Tue Dec 12 03:18:37 2006
@@ -0,0 +1,218 @@
+/*
+ * 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.query.lucene;
+
+import org.apache.jackrabbit.name.IllegalNameException;
+import org.apache.jackrabbit.name.NamespaceResolver;
+import org.apache.jackrabbit.name.NoPrefixDeclaredException;
+import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.name.UnknownPrefixException;
+import org.apache.jackrabbit.name.AbstractNamespaceResolver;
+import org.apache.jackrabbit.name.NameFormat;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.jcr.NamespaceException;
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * The class <code>NamespaceMappings</code> implements a {@link
+ * org.apache.jackrabbit.core.NamespaceResolver} that holds a namespace
+ * mapping that is used internally in the search index. Storing paths with the
+ * full uri of a namespace would require too much space in the search index.
+ * <p/>
+ * Whenever a yet unknown namespace uri to prefix mapping is requested, a new
+ * prefix is created on the fly and associated with the namespace. Known
+ * namespace mappings are stored in a properties file.
+ */
+public class FileBasedNamespaceMappings
+        extends AbstractNamespaceResolver
+        implements NamespaceMappings {
+
+    /**
+     * Default logger instance for this class
+     */
+    private static Logger log = LoggerFactory.getLogger(NamespaceMappings.class);
+
+    /**
+     * Location of the file that persists the uri / prefix mappings
+     */
+    private final File storage;
+
+    /**
+     * Map of uris indexed by prefixes
+     */
+    private Map prefixToURI = new HashMap();
+
+    /**
+     * Map of prefixes indexed by uris
+     */
+    private Map uriToPrefix = new HashMap();
+
+    /**
+     * Current prefix count.
+     */
+    private int prefixCount;
+
+    /**
+     * Creates <code>NamespaceMappings</code> instance. Initial mappings are
+     * loaded from <code>file</code>.
+     *
+     * @param file the <code>File</code> to load initial mappings.
+     * @throws IOException if an error occurs while reading initial namespace
+     *                     mappings from <code>file</code>.
+     */
+    public FileBasedNamespaceMappings(File file) throws IOException {
+        storage = file;
+        load();
+    }
+
+    /**
+     * Returns a namespace uri for a <code>prefix</code>.
+     *
+     * @param prefix the namespace prefix.
+     * @return the namespace uri.
+     * @throws NamespaceException if no namespace uri is registered for
+     *                            <code>prefix</code>.
+     */
+    public synchronized String getURI(String prefix) throws NamespaceException {
+        if (!prefixToURI.containsKey(prefix)) {
+            throw new NamespaceException(prefix + ": is not a registered namespace prefix.");
+        }
+        return (String) prefixToURI.get(prefix);
+    }
+
+    /**
+     * Returns a prefix for the namespace <code>uri</code>. If a namespace
+     * mapping exists, the already known prefix is returned; otherwise a new
+     * prefix is created and assigned to the namespace uri.
+     *
+     * @param uri the namespace uri.
+     * @return the prefix for the namespace uri.
+     * @throws NamespaceException if an yet unknown namespace uri / prefix
+     *                            mapping could not be stored.
+     */
+    public synchronized String getPrefix(String uri) throws NamespaceException {
+        String prefix = (String) uriToPrefix.get(uri);
+        if (prefix == null) {
+            // make sure prefix is not taken
+            while (prefixToURI.get(String.valueOf(prefixCount)) != null) {
+                prefixCount++;
+            }
+            prefix = String.valueOf(prefixCount);
+            prefixToURI.put(prefix, uri);
+            uriToPrefix.put(uri, prefix);
+            log.debug("adding new namespace mapping: " + prefix + " -> " + uri);
+            try {
+                store();
+            } catch (IOException e) {
+                throw new NamespaceException("Could not obtain a prefix for uri: " + uri,
e);
+            }
+        }
+        return prefix;
+    }
+
+    //----------------------------< NamespaceMappings >-------------------------
+
+    /**
+     * Translates a property name from a session local namespace mapping
+     * into a search index private namespace mapping.
+     *
+     * @param name     the property name to translate
+     * @param resolver the <code>NamespaceResolver</code> of the local session.
+     * @return the translated property name
+     */
+    public String translatePropertyName(String name, NamespaceResolver resolver)
+            throws IllegalNameException, UnknownPrefixException {
+        QName qName = NameFormat.parse(name, resolver);
+        try {
+            return NameFormat.format(qName, this);
+        } catch (NoPrefixDeclaredException e) {
+            // should never happen actually, because we create yet unknown
+            // uri mappings on the fly.
+            throw new IllegalNameException("Internal error.", e);
+        }
+    }
+
+    //-----------------------< internal >---------------------------------------
+
+    /**
+     * Loads currently known mappings from a .properties file.
+     *
+     * @throws IOException if an error occurs while reading from the file.
+     */
+    private void load() throws IOException {
+        if (storage.exists()) {
+            InputStream in = new FileInputStream(storage);
+            try {
+                Properties props = new Properties();
+                log.debug("loading namespace mappings...");
+                props.load(in);
+
+                // read mappings from properties
+                Iterator iter = props.keySet().iterator();
+                while (iter.hasNext()) {
+                    String prefix = (String) iter.next();
+                    String uri = props.getProperty(prefix);
+                    log.debug(prefix + " -> " + uri);
+                    prefixToURI.put(prefix, uri);
+                    uriToPrefix.put(uri, prefix);
+                }
+                prefixCount = props.size();
+                log.debug("namespace mappings loaded.");
+            } finally {
+                in.close();
+            }
+        }
+    }
+
+    /**
+     * Writes the currently known mappings into a .properties file.
+     *
+     * @throws IOException if an error occurs while writing the file.
+     */
+    private void store() throws IOException {
+        Properties props = new Properties();
+
+        // store mappings in properties
+        Iterator iter = prefixToURI.keySet().iterator();
+        while (iter.hasNext()) {
+            String prefix = (String) iter.next();
+            String uri = (String) prefixToURI.get(prefix);
+            props.setProperty(prefix, uri);
+        }
+
+        OutputStream out = new FileOutputStream(storage);
+        try {
+            out = new BufferedOutputStream(out);
+            props.store(out, null);
+        } finally {
+            // make sure stream is closed
+            out.close();
+        }
+    }
+}

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

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NSRegistryBasedNamespaceMappings.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NSRegistryBasedNamespaceMappings.java?view=auto&rev=486099
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NSRegistryBasedNamespaceMappings.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NSRegistryBasedNamespaceMappings.java
Tue Dec 12 03:18:37 2006
@@ -0,0 +1,90 @@
+/*
+ * 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.query.lucene;
+
+import org.apache.jackrabbit.name.AbstractNamespaceResolver;
+import org.apache.jackrabbit.name.NamespaceResolver;
+import org.apache.jackrabbit.name.IllegalNameException;
+import org.apache.jackrabbit.name.UnknownPrefixException;
+import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.name.NameFormat;
+import org.apache.jackrabbit.name.NoPrefixDeclaredException;
+import org.apache.jackrabbit.core.NamespaceRegistryImpl;
+
+import javax.jcr.NamespaceException;
+
+/**
+ * <code>NSRegistryBasedNamespaceMappings</code> implements a namespace mapping
+ * based on the stable index prefix provided by the namespace registry.
+ */
+public class NSRegistryBasedNamespaceMappings
+        extends AbstractNamespaceResolver
+        implements NamespaceMappings {
+
+    /**
+     * The namespace registry.
+     */
+    private final NamespaceRegistryImpl nsReg;
+
+    /**
+     * Creates a new <code>NSRegistryBasedNamespaceMappings</code>.
+     *
+     * @param nsReg the namespace registry of the repository.
+     */
+    NSRegistryBasedNamespaceMappings(NamespaceRegistryImpl nsReg) {
+        this.nsReg = nsReg;
+    }
+
+    //-------------------------------< NamespaceResolver >----------------------
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getURI(String prefix) throws NamespaceException {
+        try {
+            int index = Integer.parseInt(prefix);
+            return nsReg.getURI(index);
+        } catch (NumberFormatException e) {
+            throw new NamespaceException("Unknown prefix: " + prefix);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getPrefix(String uri) throws NamespaceException {
+        return String.valueOf(nsReg.getURIIndex(uri));
+    }
+
+    //-------------------------------< NamespaceMappings >----------------------
+
+    /**
+     * {@inheritDoc}
+     */
+    public String translatePropertyName(String name,
+                                        NamespaceResolver resolver)
+            throws IllegalNameException, UnknownPrefixException {
+        QName qName = NameFormat.parse(name, resolver);
+        try {
+            return NameFormat.format(qName, this);
+        } catch (NoPrefixDeclaredException e) {
+            // should never happen actually, there is always a stable index
+            // prefix for a known namespace uri
+            throw new IllegalNameException("Internal error.", e);
+        }
+    }
+}

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

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NamespaceMappings.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NamespaceMappings.java?view=diff&rev=486099&r1=486098&r2=486099
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NamespaceMappings.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NamespaceMappings.java
Tue Dec 12 03:18:37 2006
@@ -18,197 +18,24 @@
 
 import org.apache.jackrabbit.name.IllegalNameException;
 import org.apache.jackrabbit.name.NamespaceResolver;
-import org.apache.jackrabbit.name.NoPrefixDeclaredException;
-import org.apache.jackrabbit.name.QName;
 import org.apache.jackrabbit.name.UnknownPrefixException;
-import org.apache.jackrabbit.name.AbstractNamespaceResolver;
-import org.apache.jackrabbit.name.NameFormat;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.jcr.NamespaceException;
-import java.io.BufferedOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Properties;
 
 /**
- * The class <code>NamespaceMappings</code> implements a {@link
- * org.apache.jackrabbit.core.NamespaceResolver} that holds a namespace
- * mapping that is used internally in the search index. Storing paths with the
- * full uri of a namespace would require too much space in the search index.
- * <p/>
- * Whenever a yet unknown namespace uri to prefix mapping is requested, a new
- * prefix is created on the fly and associated with the namespace. Known
- * namespace mappings are stored in a properties file.
+ * The class <code>NamespaceMappings</code> holds a namespace mapping that is
+ * used internally in the search index. Storing paths with the full uri of a
+ * namespace would require too much space in the search index.
  */
-public class NamespaceMappings extends AbstractNamespaceResolver {
-
-    /**
-     * Default logger instance for this class
-     */
-    private static Logger log = LoggerFactory.getLogger(NamespaceMappings.class);
-
-    /**
-     * Location of the file that persists the uri / prefix mappings
-     */
-    private final File storage;
-
-    /**
-     * Map of uris indexed by prefixes
-     */
-    private Map prefixToURI = new HashMap();
-
-    /**
-     * Map of prefixes indexed by uris
-     */
-    private Map uriToPrefix = new HashMap();
-
-    /**
-     * Current prefix count.
-     */
-    private int prefixCount;
-
-    /**
-     * Creates <code>NamespaceMappings</code> instance. Initial mappings are
-     * loaded from <code>file</code>.
-     *
-     * @param file the <code>File</code> to load initial mappings.
-     * @throws IOException if an error occurs while reading initial namespace
-     *                     mappings from <code>file</code>.
-     */
-    public NamespaceMappings(File file) throws IOException {
-        storage = file;
-        load();
-    }
+public interface NamespaceMappings extends NamespaceResolver {
 
     /**
-     * Returns a namespace uri for a <code>prefix</code>.
-     *
-     * @param prefix the namespace prefix.
-     * @return the namespace uri.
-     * @throws NamespaceException if no namespace uri is registered for
-     *                            <code>prefix</code>.
-     */
-    public synchronized String getURI(String prefix) throws NamespaceException {
-        if (!prefixToURI.containsKey(prefix)) {
-            throw new NamespaceException(prefix + ": is not a registered namespace prefix.");
-        }
-        return (String) prefixToURI.get(prefix);
-    }
-
-    /**
-     * Returns a prefix for the namespace <code>uri</code>. If a namespace
-     * mapping exists, the already known prefix is returned; otherwise a new
-     * prefix is created and assigned to the namespace uri.
-     *
-     * @param uri the namespace uri.
-     * @return the prefix for the namespace uri.
-     * @throws NamespaceException if an yet unknown namespace uri / prefix
-     *                            mapping could not be stored.
-     */
-    public synchronized String getPrefix(String uri) throws NamespaceException {
-        String prefix = (String) uriToPrefix.get(uri);
-        if (prefix == null) {
-            // make sure prefix is not taken
-            while (prefixToURI.get(String.valueOf(prefixCount)) != null) {
-                prefixCount++;
-            }
-            prefix = String.valueOf(prefixCount);
-            prefixToURI.put(prefix, uri);
-            uriToPrefix.put(uri, prefix);
-            log.debug("adding new namespace mapping: " + prefix + " -> " + uri);
-            try {
-                store();
-            } catch (IOException e) {
-                throw new NamespaceException("Could not obtain a prefix for uri: " + uri,
e);
-            }
-        }
-        return prefix;
-    }
-
-    /**
-     * Translates a property name from a session local namespace mapping
-     * into a search index private namespace mapping.
+     * Translates a property name from a session local namespace mapping into a
+     * search index private namespace mapping.
      *
      * @param name     the property name to translate
      * @param resolver the <code>NamespaceResolver</code> of the local session.
      * @return the translated property name
      */
-    public String translatePropertyName(String name, NamespaceResolver resolver)
-            throws IllegalNameException, UnknownPrefixException {
-        QName qName = NameFormat.parse(name, resolver);
-        try {
-            return NameFormat.format(qName, this);
-        } catch (NoPrefixDeclaredException e) {
-            // should never happen actually, because we create yet unknown
-            // uri mappings on the fly.
-            throw new IllegalNameException("Internal error.", e);
-        }
-    }
-
-    //-----------------------< internal >---------------------------------------
-
-    /**
-     * Loads currently known mappings from a .properties file.
-     *
-     * @throws IOException if an error occurs while reading from the file.
-     */
-    private void load() throws IOException {
-        if (storage.exists()) {
-            InputStream in = new FileInputStream(storage);
-            try {
-                Properties props = new Properties();
-                log.debug("loading namespace mappings...");
-                props.load(in);
-
-                // read mappings from properties
-                Iterator iter = props.keySet().iterator();
-                while (iter.hasNext()) {
-                    String prefix = (String) iter.next();
-                    String uri = props.getProperty(prefix);
-                    log.debug(prefix + " -> " + uri);
-                    prefixToURI.put(prefix, uri);
-                    uriToPrefix.put(uri, prefix);
-                }
-                prefixCount = props.size();
-                log.debug("namespace mappings loaded.");
-            } finally {
-                in.close();
-            }
-        }
-    }
-
-    /**
-     * Writes the currently known mappings into a .properties file.
-     *
-     * @throws IOException if an error occurs while writing the file.
-     */
-    private void store() throws IOException {
-        Properties props = new Properties();
-
-        // store mappings in properties
-        Iterator iter = prefixToURI.keySet().iterator();
-        while (iter.hasNext()) {
-            String prefix = (String) iter.next();
-            String uri = (String) prefixToURI.get(prefix);
-            props.setProperty(prefix, uri);
-        }
-
-        OutputStream out = new FileOutputStream(storage);
-        try {
-            out = new BufferedOutputStream(out);
-            props.store(out, null);
-        } finally {
-            // make sure stream is closed
-            out.close();
-        }
-    }
+    public String translatePropertyName(String name,
+                                        NamespaceResolver resolver)
+            throws IllegalNameException, UnknownPrefixException;
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java?view=diff&rev=486099&r1=486098&r2=486099
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java
Tue Dec 12 03:18:37 2006
@@ -230,7 +230,16 @@
         } else {
             // read local namespace mappings
             File mapFile = new File(indexDir, NS_MAPPING_FILE);
-             nsMappings = new NamespaceMappings(mapFile);
+            if (mapFile.exists()) {
+                // be backward compatible and use ns_mappings.properties from
+                // index folder
+                nsMappings = new FileBasedNamespaceMappings(mapFile);
+            } else {
+                // otherwise use repository wide stable index prefix from
+                // namespace registry
+                nsMappings = new NSRegistryBasedNamespaceMappings(
+                        context.getNamespaceRegistry());
+            }
         }
 
         index = new MultiIndex(indexDir, this, context.getItemStateManager(),



Mime
View raw message