From 5f8d0747529c098e588ec28194a9dc9a1e553cc0 Mon Sep 17 00:00:00 2001 From: Boris Petrov Date: Fri, 30 Dec 2016 14:28:25 +0200 Subject: [PATCH] Prevent memory leaks in PerFieldAnalyzerWrapper By overriding the 'close' method. Otherwise nothing will close the defaultAnalyzer as well as all the fieldAnalyzers. --- .../analysis/miscellaneous/PerFieldAnalyzerWrapper.java | 7 +++++++ .../miscellaneous/TestPerFieldAnalyzerWrapper.java | 6 +----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/PerFieldAnalyzerWrapper.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/PerFieldAnalyzerWrapper.java index e49a929ab27e..ccbd18ca99eb 100644 --- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/PerFieldAnalyzerWrapper.java +++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/PerFieldAnalyzerWrapper.java @@ -90,4 +90,11 @@ protected Analyzer getWrappedAnalyzer(String fieldName) { public String toString() { return "PerFieldAnalyzerWrapper(" + fieldAnalyzers + ", default=" + defaultAnalyzer + ")"; } + + @Override + public void close() { + defaultAnalyzer.close(); + fieldAnalyzers.values().forEach(Analyzer::close); + super.close(); + } } diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestPerFieldAnalyzerWrapper.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestPerFieldAnalyzerWrapper.java index 11eff953006a..67c2d9f7eba4 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestPerFieldAnalyzerWrapper.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestPerFieldAnalyzerWrapper.java @@ -68,10 +68,7 @@ public void testPerField() throws Exception { assertFalse(tokenStream.incrementToken()); tokenStream.end(); } - // TODO: fix this about PFAW, this is crazy analyzer.close(); - defaultAnalyzer.close(); - IOUtils.close(analyzerPerField.values()); } public void testReuseWrapped() throws Exception { @@ -127,7 +124,7 @@ protected TokenStreamComponents wrapComponents(String fieldName, TokenStreamComp ts4 = wrapper3.tokenStream("moreSpecial", text); assertSame(ts3, ts4); assertSame(ts2, ts3); - IOUtils.close(wrapper3, wrapper2, wrapper1, specialAnalyzer, defaultAnalyzer); + IOUtils.close(wrapper3, wrapper2, wrapper1); } public void testCharFilters() throws Exception { @@ -157,6 +154,5 @@ protected Reader initReader(String fieldName, Reader reader) { new int[] { 2 } ); p.close(); - a.close(); // TODO: fix this about PFAW, its a trap } }