Author: rvesse
Date: Tue Jan 29 15:49:00 2013
New Revision: 1439954

URL: http://svn.apache.org/viewvc?rev=1439954&view=rev
Log:
Simplify FastAbbreviatingPrefixMap to use longestMatch() method of Trie, fix a 
bug in Trie related to longest matches, enabled abbreviation performance tests 
since with the Trie improvements in place performance difference is now much 
more marked

Modified:
    jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/lib/Trie.java
    
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/system/FastAbbreviatingPrefixMap.java
    
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/system/TS_RiotSystem.java
    
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/system/TestAbbreviationPerformance.java

Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/lib/Trie.java
URL: 
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/lib/Trie.java?rev=1439954&r1=1439953&r2=1439954&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/lib/Trie.java 
(original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/lib/Trie.java Tue 
Jan 29 15:49:00 2013
@@ -272,17 +272,17 @@ public final class Trie<T> {
             return current.getValue();
         } else {
             for (int i = 0; i < key.length(); i++) {
-                if (current == null)
-                    return value;
                 if (current.hasValue())
                     value = current.getValue();
                 current = current.getChild(key.charAt(i));
+                if (current == null)
+                    return value;
+            }
+            // If we reach here current is the complete key match
+            // so return its value if it has one
+            if (current.hasValue()) {
+                return current.getValue();
             }
-        }
-        // If we reach here current is the complete key match
-        // so return its value if it has one
-        if (current.hasValue()) {
-            return current.getValue();
         }
         return value;
     }

Modified: 
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/system/FastAbbreviatingPrefixMap.java
URL: 
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/system/FastAbbreviatingPrefixMap.java?rev=1439954&r1=1439953&r2=1439954&view=diff
==============================================================================
--- 
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/system/FastAbbreviatingPrefixMap.java
 (original)
+++ 
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/system/FastAbbreviatingPrefixMap.java
 Tue Jan 29 15:49:00 2013
@@ -79,23 +79,6 @@ public class FastAbbreviatingPrefixMap e
         this.putAll(pmap);
     }
 
-    /**
-     * Gets the key for abbreviation lookup from an IRI
-     * 
-     * @param iriString
-     *            IRI string
-     * @return Key or null
-     */
-    private String getAbbrevKey(String iriString) {
-        int index = iriString.lastIndexOf('#');
-        if (index > -1)
-            return iriString.substring(0, index + 1);
-        index = iriString.lastIndexOf('/');
-        if (index > -1)
-            return iriString.substring(0, index + 1);
-        return null;
-    }
-
     @Override
     public Map<String, IRI> getMapping() {
         return this.prefixesView;
@@ -214,27 +197,15 @@ public class FastAbbreviatingPrefixMap e
 
     @Override
     protected Pair<String, String> abbrev(Map<String, IRI> prefixes, String 
uriStr, boolean turtleSafe) {
-        // Try to use trie based lookup
-        String abbrevKey = this.getAbbrevKey(uriStr);
-        if (abbrevKey != null) {
-            // Suitable for trie based lookup
-            String prefix = this.abbrevs.get(abbrevKey);
-            if (prefix == null) {
-                // Try looking up the full IRI in case it is exactly a 
namespace IRI
-                prefix = this.abbrevs.get(uriStr);
-                if (prefix == null)
-                    return null;
-            }
-
-            String ln = 
uriStr.substring(this.prefixes.get(prefix).toString().length());
-            if (!turtleSafe || isTurtleSafe(ln))
-                return Pair.create(prefix, ln);
+        //Use longest match to find the longest possible match
+        String prefix = this.abbrevs.longestMatch(uriStr);
+        if (prefix == null)
             return null;
-        } else {
-            // Not suitable for trie based lookup so trie the brute force
-            // approach
-            return super.abbrev(prefixes, uriStr, turtleSafe);
-        }
+
+        String ln = 
uriStr.substring(this.prefixes.get(prefix).toString().length());
+        if (!turtleSafe || isTurtleSafe(ln))
+            return Pair.create(prefix, ln);
+        return null;
     }
 
     @Override

Modified: 
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/system/TS_RiotSystem.java
URL: 
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/system/TS_RiotSystem.java?rev=1439954&r1=1439953&r2=1439954&view=diff
==============================================================================
--- 
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/system/TS_RiotSystem.java
 (original)
+++ 
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/system/TS_RiotSystem.java
 Tue Jan 29 15:49:00 2013
@@ -33,9 +33,9 @@ import org.junit.runners.Suite.SuiteClas
     // Prefix Map implementations
     , TestPrefixMap.class
     , TestFastAbbreviatingPrefixMap.class
-    // May be subject to performance vagaries, often passes in Eclipse 
-    // and not on maven command line so excluded for now
-    //, TestAbbreviationPerformance.class
+    // May be subject to performance vagaries, with the improvements made
+    // to the fast implementation this should be fairly safe
+    , TestAbbreviationPerformance.class
 })
 
 public class TS_RiotSystem

Modified: 
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/system/TestAbbreviationPerformance.java
URL: 
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/system/TestAbbreviationPerformance.java?rev=1439954&r1=1439953&r2=1439954&view=diff
==============================================================================
--- 
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/system/TestAbbreviationPerformance.java
 (original)
+++ 
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/system/TestAbbreviationPerformance.java
 Tue Jan 29 15:49:00 2013
@@ -44,8 +44,8 @@ public class TestAbbreviationPerformance
             fPerf += run(fast, input, expected, 1000);
         }
         
-        //System.out.println("PrefixMap performance: " + nPerf + "ns");
-        //System.out.println("Fast Prefix Map performance: " + fPerf + "ns");
+        System.out.println("PrefixMap performance: " + nPerf + "ns");
+        System.out.println("Fast Prefix Map performance: " + fPerf + "ns");
 
         if (fastShouldWin) {
             if (fPerf > nPerf)
@@ -92,20 +92,6 @@ public class TestAbbreviationPerformance
     @Test
     public void prefixMap_abbrev_performance_02() {
         PrefixMap pmap = new PrefixMap();
-        populate(pmap, 2);
-        FastAbbreviatingPrefixMap fmap = new FastAbbreviatingPrefixMap();
-        populate(fmap, 2);
-
-        test_amalgamated_performance(pmap, fmap, 2, true);
-    }
-
-    /**
-     * Expect {@link FastAbbreviatingPrefixMap} to outperform {@link 
PrefixMap} as soon as
-     * there are a few namespaces
-     */
-    @Test
-    public void prefixMap_abbrev_performance_03() {
-        PrefixMap pmap = new PrefixMap();
         populate(pmap, 5);
         FastAbbreviatingPrefixMap fmap = new FastAbbreviatingPrefixMap();
         populate(fmap, 5);
@@ -118,7 +104,7 @@ public class TestAbbreviationPerformance
      * {@link PrefixMap} once there are a good number of namespaces
      */
     @Test
-    public void prefixMap_abbrev_performance_04() {
+    public void prefixMap_abbrev_performance_03() {
         PrefixMap pmap = new PrefixMap();
         populate(pmap, 20);
         FastAbbreviatingPrefixMap fmap = new FastAbbreviatingPrefixMap();
@@ -132,7 +118,7 @@ public class TestAbbreviationPerformance
      * {@link PrefixMap} once there are a good number of namespaces
      */
     @Test
-    public void prefixMap_abbrev_performance_05() {
+    public void prefixMap_abbrev_performance_04() {
         PrefixMap pmap = new PrefixMap();
         populate(pmap, 100);
         FastAbbreviatingPrefixMap fmap = new FastAbbreviatingPrefixMap();


Reply via email to