From 9c61ecdef7dd001b3218764b6977ee4e7027729e Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Thu, 23 Dec 2010 15:23:18 +0000 Subject: [PATCH] SOLR-2275: mm param parsing optimization git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1052299 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/solr/util/SolrPluginUtils.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/solr/src/java/org/apache/solr/util/SolrPluginUtils.java b/solr/src/java/org/apache/solr/util/SolrPluginUtils.java index 8560a456f0c..ada37e072eb 100644 --- a/solr/src/java/org/apache/solr/util/SolrPluginUtils.java +++ b/solr/src/java/org/apache/solr/util/SolrPluginUtils.java @@ -608,21 +608,27 @@ public class SolrPluginUtils { } } + // private static Pattern spaceAroundLessThanPattern = Pattern.compile("\\s*<\\s*"); + private static Pattern spaceAroundLessThanPattern = Pattern.compile("(\\s+<)|(<\\s+)|(\\s+<\\s+)"); + private static Pattern spacePattern = Pattern.compile(" "); + private static Pattern lessThanPattern = Pattern.compile("<"); + /** * helper exposed for UnitTests * @see #setMinShouldMatch */ - static int calculateMinShouldMatch(int optionalClauseCount, String specIn) { + static int calculateMinShouldMatch(int optionalClauseCount, String spec) { int result = optionalClauseCount; + spec = spec.trim(); - String spec = specIn.replaceAll("\\s*<\\s*", "<"); if (-1 < spec.indexOf("<")) { /* we have conditional spec(s) */ + spec = spaceAroundLessThanPattern.matcher(spec).replaceAll("<"); - for (String s : spec.trim().split(" ")) { - String[] parts = s.split("<"); - int upperBound = (new Integer(parts[0].trim())).intValue(); + for (String s : spacePattern.split(spec)) { + String[] parts = lessThanPattern.split(s); + int upperBound = Integer.parseInt(parts[0]); if (optionalClauseCount <= upperBound) { return result; } else { @@ -635,13 +641,14 @@ public class SolrPluginUtils { /* otherwise, simple expresion */ - if (-1 < spec.indexOf("%")) { - /* percentage */ - int percent = new Integer(spec.trim().replace("%","")).intValue(); - float calc = (result * percent) / 100f; + if (-1 < spec.indexOf('%')) { + /* percentage - assume the % was the last char. If not, let Integer.parseInt fail. */ + spec = spec.substring(0,spec.length()-1); + int percent = Integer.parseInt(spec); + float calc = (result * percent) * (1/100f); result = calc < 0 ? result + (int)calc : (int)calc; } else { - int calc = (new Integer(spec.trim())).intValue(); + int calc = Integer.parseInt(spec); result = calc < 0 ? result + calc : calc; }