jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r681121 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core: ./ persistence/bundle/ persistence/bundle/util/ query/lucene/ util/
Date Wed, 30 Jul 2008 16:54:23 GMT
Author: jukka
Date: Wed Jul 30 09:54:21 2008
New Revision: 681121

URL: http://svn.apache.org/viewvc?rev=681121&view=rev
Log:
JCR-1612: Reintroduce NamespaceStorage and namespace-caching

Make NamespaceRegistryImpl implement StringIndex and simplify the downstream classes accordingly.

To make this happen I modified the StringIndex contract a bit, allowing (and, for indexToString,
requiring) implementations to throw exceptions if an unknown index is requested. Modified
implementing classes accordingly.

Removed:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/NamespaceIndex.java
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/DbNameIndex.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/NGKDbNameIndex.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NSRegistryBasedNamespaceMappings.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/StringIndex.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java?rev=681121&r1=681120&r2=681121&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java
Wed Jul 30 09:54:21 2008
@@ -20,6 +20,7 @@
 import org.apache.jackrabbit.core.cluster.NamespaceEventListener;
 import org.apache.jackrabbit.core.fs.FileSystem;
 import org.apache.jackrabbit.core.fs.FileSystemResource;
+import org.apache.jackrabbit.core.util.StringIndex;
 import org.apache.jackrabbit.spi.commons.namespace.NamespaceResolver;
 import org.apache.jackrabbit.spi.Name;
 import org.apache.jackrabbit.util.XMLChar;
@@ -42,8 +43,9 @@
 /**
  * A <code>NamespaceRegistryImpl</code> ...
  */
-public class NamespaceRegistryImpl
-        implements NamespaceRegistry, NamespaceResolver, NamespaceEventListener {
+public class NamespaceRegistryImpl implements
+        NamespaceRegistry, NamespaceResolver,
+        NamespaceEventListener, StringIndex {
 
     private static Logger log = LoggerFactory.getLogger(NamespaceRegistryImpl.class);
 
@@ -286,32 +288,34 @@
         eventChannel.setListener(this);
     }
 
+    //-------------------------------------------------------< StringIndex >--
+
     /**
-     * Returns the index (i.e. stable prefix) for the given uri.
+     * Returns the index (i.e. stable prefix) for the given namespace URI.
      *
-     * @param uri the uri to retrieve the index for
-     * @return the index
-     * @throws NamespaceException if the URI is not registered.
+     * @param uri namespace URI
+     * @return namespace index
+     * @throws IllegalArgumentException if the namespace is not registered
      */
-    public int getURIIndex(String uri) throws NamespaceException {
+    public int stringToIndex(String uri) {
         Integer idx = (Integer) uriToIndex.get(uri);
         if (idx == null) {
-            throw new NamespaceException("URI " + uri + " is not registered.");
+            throw new IllegalArgumentException("Namespace not registered: " + uri);
         }
         return idx.intValue();
     }
 
     /**
-     * Returns the URI for a given index (i.e. stable prefix).
+     * Returns the namespace URI for a given index (i.e. stable prefix).
      *
-     * @param idx the index to retrieve the uri for.
-     * @return the uri
-     * @throws NamespaceException if the URI is not registered.
+     * @param idx namespace index
+     * @return namespace URI
+     * @throws IllegalArgumentException if the given index is invalid
      */
-    public String getURI(int idx) throws NamespaceException {
+    public String indexToString(int idx) {
         String uri = (String) indexToURI.get(new Integer(idx));
         if (uri == null) {
-            throw new NamespaceException("URI for index " + idx +  " not registered.");
+            throw new IllegalArgumentException("Invalid namespace index: " + idx);
         }
         return uri;
     }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java?rev=681121&r1=681120&r2=681121&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java
Wed Jul 30 09:54:21 2008
@@ -37,7 +37,6 @@
 import org.apache.jackrabbit.core.persistence.PMContext;
 import org.apache.jackrabbit.core.persistence.PersistenceManager;
 import org.apache.jackrabbit.core.util.StringIndex;
-import org.apache.jackrabbit.core.persistence.bundle.util.NamespaceIndex;
 import org.apache.jackrabbit.core.persistence.bundle.util.NodePropBundle;
 import org.apache.jackrabbit.core.persistence.bundle.util.BundleCache;
 import org.apache.jackrabbit.core.persistence.bundle.util.LRUNodeIdCache;
@@ -262,7 +261,7 @@
                 if (nsFile.exists()) {
                     nsIndex = new HashMapIndex(nsFile);
                 } else {
-                    nsIndex = new NamespaceIndex((NamespaceRegistryImpl) context.getNamespaceRegistry());
+                    nsIndex = (NamespaceRegistryImpl) context.getNamespaceRegistry();
                 }
             }
             return nsIndex;

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/DbNameIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/DbNameIndex.java?rev=681121&r1=681120&r2=681121&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/DbNameIndex.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/DbNameIndex.java
Wed Jul 30 09:54:21 2008
@@ -109,15 +109,12 @@
     /**
      * {@inheritDoc}
      */
-    public String indexToString(int idx) {
+    public String indexToString(int idx) throws IllegalArgumentException {
         // check cache
         Integer index = new Integer(idx);
         String s = (String) index2String.get(index);
         if (s == null) {
             s = getString(idx);
-            if (s == null) {
-                throw new IllegalStateException("String empty???");
-            }
             if (s.equals(" ")) {
                 s = "";
             }
@@ -135,21 +132,29 @@
      */
     protected int insertString(String string) {
         // assert index does not exist
-        ResultSet rs = null;
+        int result = -1;
         try {
-            Statement stmt = connectionManager.executeStmt(nameInsertSQL, new Object[]{string},
true, 0);
-            rs = stmt.getGeneratedKeys();
-            if (!rs.next()) {
-                return -1;
-            } else {
-                return rs.getInt(1);
+            Statement stmt = connectionManager.executeStmt(
+                    nameInsertSQL, new Object[] { string }, true, 0);
+            ResultSet rs = stmt.getGeneratedKeys();
+            try {
+                if (rs.next()) {
+                    result = rs.getInt(1);
+                }
+            } finally {
+                rs.close();
             }
         } catch (Exception e) {
-            IllegalStateException ise = new IllegalStateException("Unable to insert index
for string: " + string);
+            IllegalStateException ise = new IllegalStateException(
+                    "Unable to insert index for string: " + string);
             ise.initCause(e);
             throw ise;
-        } finally {
-            closeResultSet(rs);
+        }
+        if (result != -1) {
+            return result;
+        } else {
+            // Could not get the index with getGeneratedKeys, try with SELECT
+            return getIndex(string);
         }
     }
 
@@ -159,59 +164,57 @@
      * @return the index or -1 if not found.
      */
     protected int getIndex(String string) {
-        ResultSet rs = null;
         try {
-            Statement stmt = connectionManager.executeStmt(indexSelectSQL, new Object[]{string});
-            rs = stmt.getResultSet();
-            if (!rs.next()) {
-                return -1;
-            } else {
-                return rs.getInt(1);
+            Statement stmt = connectionManager.executeStmt(
+                    indexSelectSQL, new Object[] { string });
+            ResultSet rs = stmt.getResultSet();
+            try {
+                if (rs.next()) {
+                    return rs.getInt(1);
+                } else {
+                    return -1;
+                }
+            } finally {
+                rs.close();
             }
         } catch (Exception e) {
-            IllegalStateException ise = new IllegalStateException("Unable to read index for
string: " + string);
+            IllegalStateException ise = new IllegalStateException(
+                    "Unable to read index for string: " + string);
             ise.initCause(e);
             throw ise;
-        } finally {
-            closeResultSet(rs);
         }
     }
 
     /**
      * Retrieves the string from the database for the given index.
      * @param index the index to retrieve the string for.
-     * @return the string or <code>null</code> if not found.
+     * @return the string
+     * @throws IllegalArgumentException if the string is not found
      */
-    protected String getString(int index) {
-        ResultSet rs = null;
+    protected String getString(int index)
+            throws IllegalArgumentException, IllegalStateException {
+        String result = null;
         try {
-            Statement stmt = connectionManager.executeStmt(nameSelectSQL, new Object[]{new
Integer(index)});
-            rs = stmt.getResultSet();
-            if (!rs.next()) {
-                return null;
-            } else {
-                return rs.getString(1);
+            Statement stmt = connectionManager.executeStmt(
+                    nameSelectSQL, new Object[] { new Integer(index) });
+            ResultSet rs = stmt.getResultSet();
+            try {
+                if (rs.next()) {
+                    result = rs.getString(1);
+                }
+            } finally {
+                rs.close();
             }
         } catch (Exception e) {
-            IllegalStateException ise = new IllegalStateException("Unable to read name for
index: " + index);
+            IllegalStateException ise = new IllegalStateException(
+                    "Unable to read name for index: " + index);
             ise.initCause(e);
             throw ise;
-        } finally {
-            closeResultSet(rs);
         }
-    }
-
-    /**
-     * Closes the result set
-     * @param rs the result set.
-     */
-    protected void closeResultSet(ResultSet rs) {
-        if (rs != null) {
-            try {
-                rs.close();
-            } catch (SQLException se) {
-                // ignore
-            }
+        if (result == null) {
+            throw new IllegalArgumentException("Index not found: " + index);
         }
+        return result;
     }
+
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/NGKDbNameIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/NGKDbNameIndex.java?rev=681121&r1=681120&r2=681121&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/NGKDbNameIndex.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/NGKDbNameIndex.java
Wed Jul 30 09:54:21 2008
@@ -17,7 +17,6 @@
 package org.apache.jackrabbit.core.persistence.bundle.util;
 
 import java.sql.SQLException;
-import java.sql.ResultSet;
 
 /**
  * Same as {@link DbNameIndex} but does not make use of the
@@ -58,16 +57,15 @@
      */
     protected int insertString(String string) {
         // assert index does not exist
-        ResultSet rs = null;
         try {
-            connectionManager.executeStmt(nameInsertSQL, new Object[]{string});
-            return getIndex(string);
+            connectionManager.executeStmt(nameInsertSQL, new Object[] { string });
         } catch (Exception e) {
-            IllegalStateException ise = new IllegalStateException("Unable to insert index
for string: " + string);
+            IllegalStateException ise = new IllegalStateException(
+                    "Unable to insert index for string: " + string);
             ise.initCause(e);
             throw ise;
-        } finally {
-            closeResultSet(rs);
         }
+
+        return getIndex(string);
     }
 }

Modified: 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?rev=681121&r1=681120&r2=681121&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NSRegistryBasedNamespaceMappings.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/NSRegistryBasedNamespaceMappings.java
Wed Jul 30 09:54:21 2008
@@ -60,9 +60,10 @@
     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);
+            return nsReg.indexToString(index);
+        } catch (IllegalArgumentException e) {
+            throw new NamespaceException(
+                    "Unknown namespace prefix: " + prefix, e);
         }
     }
 
@@ -70,7 +71,12 @@
      * {@inheritDoc}
      */
     public String getPrefix(String uri) throws NamespaceException {
-        return String.valueOf(nsReg.getURIIndex(uri));
+        try {
+            return String.valueOf(nsReg.stringToIndex(uri));
+        } catch (IllegalArgumentException e) {
+            throw new NamespaceException(
+                    "Unknown namespace URI: " + uri, e);
+        }
     }
 
     //-------------------------------< NamespaceMappings >----------------------

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/StringIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/StringIndex.java?rev=681121&r1=681120&r2=681121&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/StringIndex.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/StringIndex.java
Wed Jul 30 09:54:21 2008
@@ -23,21 +23,24 @@
 public interface StringIndex {
 
     /**
-     * Returns the index for a given string. if the string does not exist in
-     * the underlying index map a new index needs to be created.
+     * Returns the index for a given string. If the given string is not
+     * already indexed, the implementation can either automatically index
+     * it or throw an exception.
      *
      * @param string the indexed (or to be indexed) string
      * @return index of the string
+     * @throws IllegalArgumentException if the string is not
+     *                                  (and can not be) indexed
      */
-    int stringToIndex(String string);
+    int stringToIndex(String string) throws IllegalArgumentException;
 
     /**
-     * Returns the string for a given index. If the index does not exist
-     * in the underlying index map, <code>null</code> is returned.
+     * Returns the string for a given index.
      *
      * @param idx index of a string
-     * @return the indexed string, or <code>null</code>
+     * @return the indexed string
+     * @throws IllegalArgumentException if the indexed string does not exist
      */
-    String indexToString(int idx);
+    String indexToString(int idx) throws IllegalArgumentException;
 
 }



Mime
View raw message