lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rm...@apache.org
Subject svn commit: r932752 - in /lucene/dev/trunk/solr: CHANGES.txt src/java/org/apache/solr/analysis/PatternReplaceFilter.java
Date Sat, 10 Apr 2010 16:00:45 GMT
Author: rmuir
Date: Sat Apr 10 16:00:45 2010
New Revision: 932752

URL: http://svn.apache.org/viewvc?rev=932752&view=rev
Log:
SOLR-1874: Optimize PatternReplaceFilter

Modified:
    lucene/dev/trunk/solr/CHANGES.txt
    lucene/dev/trunk/solr/src/java/org/apache/solr/analysis/PatternReplaceFilter.java

Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=932752&r1=932751&r2=932752&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Sat Apr 10 16:00:45 2010
@@ -146,6 +146,8 @@ Optimizations
   are necessary for the current log level.
   (Fuad Efendi and hossman)
 
+* SOLR-1874: Optimize PatternReplaceFilter for better performance. (rmuir, uschindler)
+
 Bug Fixes
 ----------------------
 

Modified: lucene/dev/trunk/solr/src/java/org/apache/solr/analysis/PatternReplaceFilter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/java/org/apache/solr/analysis/PatternReplaceFilter.java?rev=932752&r1=932751&r2=932752&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/java/org/apache/solr/analysis/PatternReplaceFilter.java (original)
+++ lucene/dev/trunk/solr/src/java/org/apache/solr/analysis/PatternReplaceFilter.java Sat
Apr 10 16:00:45 2010
@@ -19,12 +19,11 @@ package org.apache.solr.analysis;
 
 import org.apache.lucene.analysis.TokenFilter;
 import org.apache.lucene.analysis.TokenStream;
-import org.apache.lucene.analysis.tokenattributes.TermAttribute;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
 
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
 import java.io.IOException;
-import java.nio.CharBuffer;
 
 /**
  * A TokenFilter which applies a Pattern to each token in the stream,
@@ -43,7 +42,9 @@ public final class PatternReplaceFilter 
   private final Pattern p;
   private final String replacement;
   private final boolean all;
-  private final TermAttribute termAtt;
+  private final CharTermAttribute termAtt;
+  private final Matcher m;
+
   /**
    * Constructs an instance to replace either the first, or all occurances
    *
@@ -63,20 +64,19 @@ public final class PatternReplaceFilter 
     this.p=p;
     this.replacement = (null == replacement) ? "" : replacement;
     this.all=all;
-    this.termAtt = addAttribute(TermAttribute.class);
+    this.termAtt = addAttribute(CharTermAttribute.class);
+    this.m = p.matcher(termAtt);
   }
 
   @Override
   public boolean incrementToken() throws IOException {
     if (!input.incrementToken()) return false;
     
-    CharSequence text = CharBuffer.wrap(termAtt.termBuffer(), 0, termAtt.termLength());
-    Matcher m = p.matcher(text);
-
-    if (all) {
-      termAtt.setTermBuffer(m.replaceAll(replacement));
-    } else {
-      termAtt.setTermBuffer(m.replaceFirst(replacement));
+    m.reset();
+    if (m.find()) {
+      // replaceAll/replaceFirst will reset() this previous find.
+      String transformed = all ? m.replaceAll(replacement) : m.replaceFirst(replacement);
+      termAtt.setEmpty().append(transformed);
     }
 
     return true;



Mime
View raw message