Skip to content

Apache Lucene open-source search software

LUCENE-1186: add Analyzer.close() to close the internal ThreadLocal

Michael McCandlessdcd63a35e773
diff --git a/CHANGES.txt b/CHANGES.txt
index e7d13adee68..9bde26bd806 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -56,6 +56,9 @@ API Changes
IndexReader. This matches IndexWriter's commit methods. (Jason
Rutherglen via Mike McCandless)
+9. LUCENE-1186: Add Analyzer.close() to free internal ThreadLocal
+ resources. (Christian Kohlsch�tter via Mike McCandless)
+
Bug fixes
1. LUCENE-1415: MultiPhraseQuery has incorrect hashCode() and equals()
@@ -81,6 +84,9 @@ Bug fixes
5. LUCENE-1544: Fix deadlock in IndexWriter.addIndexes(IndexReader[]).
(Mike McCandless via Doug Sale)
+6. LUCENE-1186: Add Analyzer.close() to free internal ThreadLocal
+ resources. (Christian Kohlsch�tter via Mike McCandless)
+
New features
1. LUCENE-1411: Added expert API to open an IndexWriter on a prior
diff --git a/src/java/org/apache/lucene/analysis/Analyzer.java b/src/java/org/apache/lucene/analysis/Analyzer.java
index 8120cc522b8..2af9ba2d82f 100644
--- a/src/java/org/apache/lucene/analysis/Analyzer.java
+++ b/src/java/org/apache/lucene/analysis/Analyzer.java
@@ -20,6 +20,9 @@ package org.apache.lucene.analysis;
import java.io.Reader;
import java.io.IOException;
+import org.apache.lucene.util.CloseableThreadLocal;
+import org.apache.lucene.store.AlreadyClosedException;
+
/** An Analyzer builds TokenStreams, which analyze text. It thus represents a
* policy for extracting index terms from text.
* <p>
@@ -44,20 +47,36 @@ public abstract class Analyzer {
return tokenStream(fieldName, reader);
}
- private ThreadLocal tokenStreams = new ThreadLocal();
+ private CloseableThreadLocal tokenStreams = new CloseableThreadLocal();
/** Used by Analyzers that implement reusableTokenStream
* to retrieve previously saved TokenStreams for re-use
* by the same thread. */
protected Object getPreviousTokenStream() {
- return tokenStreams.get();
+ try {
+ return tokenStreams.get();
+ } catch (NullPointerException npe) {
+ if (tokenStreams == null) {
+ throw new AlreadyClosedException("this Analyzer is closed");
+ } else {
+ throw npe;
+ }
+ }
}
/** Used by Analyzers that implement reusableTokenStream
* to save a TokenStream for later re-use by the same
* thread. */
protected void setPreviousTokenStream(Object obj) {
- tokenStreams.set(obj);
+ try {
+ tokenStreams.set(obj);
+ } catch (NullPointerException npe) {
+ if (tokenStreams == null) {
+ throw new AlreadyClosedException("this Analyzer is closed");
+ } else {
+ throw npe;
+ }
+ }
}
@@ -78,4 +97,10 @@ public abstract class Analyzer {
{
return 0;
}
+
+ /** Frees persistent resources used by this Analyzer */
+ public void close() {
+ tokenStreams.close();
+ tokenStreams = null;
+ }
}