Apache Lucene open-source search software
LUCENE-5611: fix the crazy getAttribute API to prevent double lookups and extra code everywhere
Robert Muirf0414ff3af79
diff --git a/lucene/core/src/java/org/apache/lucene/analysis/TokenStream.java b/lucene/core/src/java/org/apache/lucene/analysis/TokenStream.javaindex 54f4c7f0642..0d10d6c1c57 100644--- a/lucene/core/src/java/org/apache/lucene/analysis/TokenStream.java+++ b/lucene/core/src/java/org/apache/lucene/analysis/TokenStream.java@@ -171,8 +171,9 @@ public abstract class TokenStream extends AttributeSource implements Closeable {*/public void end() throws IOException {clearAttributes(); // LUCENE-3849: don't consume dirty atts- if (hasAttribute(PositionIncrementAttribute.class)) {- getAttribute(PositionIncrementAttribute.class).setPositionIncrement(0);+ PositionIncrementAttribute posIncAtt = getAttribute(PositionIncrementAttribute.class);+ if (posIncAtt != null) {+ posIncAtt.setPositionIncrement(0);}}diff --git a/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java b/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.javaindex 47bd7675cf2..6d65a9789fb 100644--- a/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java+++ b/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java@@ -547,18 +547,10 @@ final class DefaultIndexingChain extends DocConsumer {if (invertState.attributeSource != stream) {// EmptyTokenStream gets angry otherwise:- if (stream.hasAttribute(TermToBytesRefAttribute.class)) {- invertState.termAttribute = stream.getAttribute(TermToBytesRefAttribute.class);- } else {- invertState.termAttribute = null;- }+ invertState.termAttribute = stream.getAttribute(TermToBytesRefAttribute.class);invertState.posIncrAttribute = stream.addAttribute(PositionIncrementAttribute.class);invertState.offsetAttribute = stream.addAttribute(OffsetAttribute.class);- if (stream.hasAttribute(PayloadAttribute.class)) {- invertState.payloadAttribute = stream.getAttribute(PayloadAttribute.class);- } else {- invertState.payloadAttribute = null;- }+ invertState.payloadAttribute = stream.getAttribute(PayloadAttribute.class);invertState.attributeSource = stream;}diff --git a/lucene/core/src/java/org/apache/lucene/util/AttributeSource.java b/lucene/core/src/java/org/apache/lucene/util/AttributeSource.javaindex 83d87fa4f89..ea943cf76bd 100644--- a/lucene/core/src/java/org/apache/lucene/util/AttributeSource.java+++ b/lucene/core/src/java/org/apache/lucene/util/AttributeSource.java@@ -287,22 +287,20 @@ public class AttributeSource {}/**- * The caller must pass in a Class<? extends Attribute> value.* Returns the instance of the passed in Attribute contained in this AttributeSource+ * <p>+ * The caller must pass in a Class<? extends Attribute> value.*- * @throws IllegalArgumentException if this AttributeSource does not contain the- * Attribute. It is recommended to always use {@link #addAttribute} even in consumers- * of TokenStreams, because you cannot know if a specific TokenStream really uses- * a specific Attribute. {@link #addAttribute} will automatically make the attribute- * available. If you want to only use the attribute, if it is available (to optimize- * consuming), use {@link #hasAttribute}.+ * @returns instance of the passed in Attribute, or {@code null} if this AttributeSource+ * does not contain the Attribute. It is recommended to always use+ * {@link #addAttribute} even in consumers of TokenStreams, because you cannot+ * know if a specific TokenStream really uses a specific Attribute.+ * {@link #addAttribute} will automatically make the attribute available.+ * If you want to only use the attribute, if it is available (to optimize+ * consuming), use {@link #hasAttribute}.*/public final <T extends Attribute> T getAttribute(Class<T> attClass) {- AttributeImpl attImpl = attributes.get(attClass);- if (attImpl == null) {- throw new IllegalArgumentException("This AttributeSource does not have the attribute '" + attClass.getName() + "'.");- }- return attClass.cast(attImpl);+ return attClass.cast(attributes.get(attClass));}private State getCurrentState() {diff --git a/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.java b/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.javaindex 9e6ae7d8049..2d0e2d211f7 100644--- a/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.java+++ b/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.java@@ -207,12 +207,8 @@ public class QueryBuilder {buffer = new CachingTokenFilter(source);buffer.reset();- if (buffer.hasAttribute(TermToBytesRefAttribute.class)) {- termAtt = buffer.getAttribute(TermToBytesRefAttribute.class);- }- if (buffer.hasAttribute(PositionIncrementAttribute.class)) {- posIncrAtt = buffer.getAttribute(PositionIncrementAttribute.class);- }+ termAtt = buffer.getAttribute(TermToBytesRefAttribute.class);+ posIncrAtt = buffer.getAttribute(PositionIncrementAttribute.class);if (termAtt != null) {try {diff --git a/lucene/core/src/test/org/apache/lucene/analysis/TestNumericTokenStream.java b/lucene/core/src/test/org/apache/lucene/analysis/TestNumericTokenStream.javaindex 0c9cb49ba44..28ce77a8872 100644--- a/lucene/core/src/test/org/apache/lucene/analysis/TestNumericTokenStream.java+++ b/lucene/core/src/test/org/apache/lucene/analysis/TestNumericTokenStream.java@@ -31,10 +31,12 @@ public class TestNumericTokenStream extends BaseTokenStreamTestCase {public void testLongStream() throws Exception {final NumericTokenStream stream=new NumericTokenStream().setLongValue(lvalue);- // use getAttribute to test if attributes really exist, if not an IAE will be throwedfinal TermToBytesRefAttribute bytesAtt = stream.getAttribute(TermToBytesRefAttribute.class);+ assertNotNull(bytesAtt);final TypeAttribute typeAtt = stream.getAttribute(TypeAttribute.class);+ assertNotNull(typeAtt);final NumericTokenStream.NumericTermAttribute numericAtt = stream.getAttribute(NumericTokenStream.NumericTermAttribute.class);+ assertNotNull(numericAtt);final BytesRef bytes = bytesAtt.getBytesRef();stream.reset();assertEquals(64, numericAtt.getValueSize());@@ -53,10 +55,12 @@ public class TestNumericTokenStream extends BaseTokenStreamTestCase {public void testIntStream() throws Exception {final NumericTokenStream stream=new NumericTokenStream().setIntValue(ivalue);- // use getAttribute to test if attributes really exist, if not an IAE will be throwedfinal TermToBytesRefAttribute bytesAtt = stream.getAttribute(TermToBytesRefAttribute.class);+ assertNotNull(bytesAtt);final TypeAttribute typeAtt = stream.getAttribute(TypeAttribute.class);+ assertNotNull(typeAtt);final NumericTokenStream.NumericTermAttribute numericAtt = stream.getAttribute(NumericTokenStream.NumericTermAttribute.class);+ assertNotNull(numericAtt);final BytesRef bytes = bytesAtt.getBytesRef();stream.reset();assertEquals(32, numericAtt.getValueSize());diff --git a/lucene/core/src/test/org/apache/lucene/index/TestStressIndexing2.java b/lucene/core/src/test/org/apache/lucene/index/TestStressIndexing2.javaindex 2ec4903ca5d..a644d1ca349 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestStressIndexing2.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestStressIndexing2.java@@ -639,8 +639,8 @@ public class TestStressIndexing2 extends LuceneTestCase {int freq1 = dpEnum1.freq();int freq2 = dpEnum2.freq();assertEquals(freq1, freq2);- OffsetAttribute offsetAtt1 = dpEnum1.attributes().hasAttribute(OffsetAttribute.class) ? dpEnum1.attributes().getAttribute(OffsetAttribute.class) : null;- OffsetAttribute offsetAtt2 = dpEnum2.attributes().hasAttribute(OffsetAttribute.class) ? dpEnum2.attributes().getAttribute(OffsetAttribute.class) : null;+ OffsetAttribute offsetAtt1 = dpEnum1.attributes().getAttribute(OffsetAttribute.class);+ OffsetAttribute offsetAtt2 = dpEnum2.attributes().getAttribute(OffsetAttribute.class);if (offsetAtt1 != null) {assertNotNull(offsetAtt2);diff --git a/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.java b/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.javaindex 1d890c47d67..747ba03d14b 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.java@@ -90,7 +90,9 @@ public class TestAttributeSource extends LuceneTestCase {assertFalse("No more attributes", it.hasNext());final FlagsAttribute flagsAtt2 = clone.getAttribute(FlagsAttribute.class);+ assertNotNull(flagsAtt2);final TypeAttribute typeAtt2 = clone.getAttribute(TypeAttribute.class);+ assertNotNull(typeAtt2);assertNotSame("FlagsAttribute of original and clone must be different instances", flagsAtt2, flagsAtt);assertNotSame("TypeAttribute of original and clone must be different instances", typeAtt2, typeAtt);assertEquals("FlagsAttribute of original and clone must be equal", flagsAtt2, flagsAtt);diff --git a/lucene/test-framework/src/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.javaindex 1f123041cbf..2f51452dfe0 100644--- a/lucene/test-framework/src/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java@@ -685,11 +685,11 @@ public abstract class BaseTokenStreamTestCase extends LuceneTestCase {int remainder = random.nextInt(10);Reader reader = new StringReader(text);TokenStream ts = a.tokenStream("dummy", useCharFilter ? new MockCharFilter(reader, remainder) : reader);- CharTermAttribute termAtt = ts.hasAttribute(CharTermAttribute.class) ? ts.getAttribute(CharTermAttribute.class) : null;- OffsetAttribute offsetAtt = ts.hasAttribute(OffsetAttribute.class) ? ts.getAttribute(OffsetAttribute.class) : null;- PositionIncrementAttribute posIncAtt = ts.hasAttribute(PositionIncrementAttribute.class) ? ts.getAttribute(PositionIncrementAttribute.class) : null;- PositionLengthAttribute posLengthAtt = ts.hasAttribute(PositionLengthAttribute.class) ? ts.getAttribute(PositionLengthAttribute.class) : null;- TypeAttribute typeAtt = ts.hasAttribute(TypeAttribute.class) ? ts.getAttribute(TypeAttribute.class) : null;+ CharTermAttribute termAtt = ts.getAttribute(CharTermAttribute.class);+ OffsetAttribute offsetAtt = ts.getAttribute(OffsetAttribute.class);+ PositionIncrementAttribute posIncAtt = ts.getAttribute(PositionIncrementAttribute.class);+ PositionLengthAttribute posLengthAtt = ts.getAttribute(PositionLengthAttribute.class);+ TypeAttribute typeAtt = ts.getAttribute(TypeAttribute.class);List<String> tokens = new ArrayList<>();List<String> types = new ArrayList<>();List<Integer> positions = new ArrayList<>();diff --git a/lucene/test-framework/src/java/org/apache/lucene/analysis/ValidatingTokenFilter.java b/lucene/test-framework/src/java/org/apache/lucene/analysis/ValidatingTokenFilter.javaindex bd406430246..88218fb1ca0 100644--- a/lucene/test-framework/src/java/org/apache/lucene/analysis/ValidatingTokenFilter.java+++ b/lucene/test-framework/src/java/org/apache/lucene/analysis/ValidatingTokenFilter.java@@ -47,23 +47,14 @@ public final class ValidatingTokenFilter extends TokenFilter {private final Map<Integer,Integer> posToStartOffset = new HashMap<>();private final Map<Integer,Integer> posToEndOffset = new HashMap<>();- private final PositionIncrementAttribute posIncAtt = getAttrIfExists(PositionIncrementAttribute.class);- private final PositionLengthAttribute posLenAtt = getAttrIfExists(PositionLengthAttribute.class);- private final OffsetAttribute offsetAtt = getAttrIfExists(OffsetAttribute.class);- private final CharTermAttribute termAtt = getAttrIfExists(CharTermAttribute.class);+ private final PositionIncrementAttribute posIncAtt = getAttribute(PositionIncrementAttribute.class);+ private final PositionLengthAttribute posLenAtt = getAttribute(PositionLengthAttribute.class);+ private final OffsetAttribute offsetAtt = getAttribute(OffsetAttribute.class);+ private final CharTermAttribute termAtt = getAttribute(CharTermAttribute.class);private final boolean offsetsAreCorrect;private final String name;- // Returns null if the attr wasn't already added- private <A extends Attribute> A getAttrIfExists(Class<A> att) {- if (hasAttribute(att)) {- return getAttribute(att);- } else {- return null;- }- }-/** The name arg is used to identify this stage when* throwing exceptions (useful if you have more than one* instance in your chain). */diff --git a/solr/core/src/java/org/apache/solr/schema/JsonPreAnalyzedParser.java b/solr/core/src/java/org/apache/solr/schema/JsonPreAnalyzedParser.javaindex accce1d8d7a..f3928291e5b 100644--- a/solr/core/src/java/org/apache/solr/schema/JsonPreAnalyzedParser.java+++ b/solr/core/src/java/org/apache/solr/schema/JsonPreAnalyzedParser.java@@ -230,10 +230,10 @@ public class JsonPreAnalyzedParser implements PreAnalyzedParser {Map<String,Object> tok = new TreeMap<>();while (it.hasNext()) {Class<? extends Attribute> cl = it.next();- if (!ts.hasAttribute(cl)) {+ Attribute att = ts.getAttribute(cl);+ if (att == null) {continue;}- Attribute att = ts.getAttribute(cl);if (cl.isAssignableFrom(CharTermAttribute.class)) {CharTermAttribute catt = (CharTermAttribute)att;cTerm = new String(catt.buffer(), 0, catt.length());diff --git a/solr/core/src/java/org/apache/solr/schema/SimplePreAnalyzedParser.java b/solr/core/src/java/org/apache/solr/schema/SimplePreAnalyzedParser.javaindex 1e588245a51..8f841f41352 100644--- a/solr/core/src/java/org/apache/solr/schema/SimplePreAnalyzedParser.java+++ b/solr/core/src/java/org/apache/solr/schema/SimplePreAnalyzedParser.java@@ -482,10 +482,10 @@ public final class SimplePreAnalyzedParser implements PreAnalyzedParser {String tTerm = null;while (it.hasNext()) {Class<? extends Attribute> cl = it.next();- if (!ts.hasAttribute(cl)) {+ Attribute att = ts.getAttribute(cl);+ if (att == null) {continue;}- Attribute att = ts.getAttribute(cl);if (cl.isAssignableFrom(CharTermAttribute.class)) {CharTermAttribute catt = (CharTermAttribute)att;cTerm = escape(catt.buffer(), catt.length());