Apache Solr open-source search software
LUCENE-4756 - release AE and CAS on #close
Tommaso Teofilif1d80996faca
diff --git a/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/BaseUIMATokenizer.java b/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/BaseUIMATokenizer.javaindex 212a40d23f9..36a9591842b 100644--- a/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/BaseUIMATokenizer.java+++ b/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/BaseUIMATokenizer.java@@ -28,7 +28,6 @@ import org.apache.uima.resource.ResourceInitializationException;import java.io.IOException;import java.io.Reader;-import java.util.HashMap;import java.util.Map;/**@@ -38,17 +37,17 @@ import java.util.Map;public abstract class BaseUIMATokenizer extends Tokenizer {protected FSIterator<AnnotationFS> iterator;- protected final AnalysisEngine ae;- protected final CAS cas;++ private final String descriptorPath;+ private final Map<String, Object> configurationParameters;++ protected AnalysisEngine ae;+ protected CAS cas;protected BaseUIMATokenizer(Reader reader, String descriptorPath, Map<String, Object> configurationParameters) {super(reader);- try {- ae = AEProviderFactory.getInstance().getAEProvider(null, descriptorPath, configurationParameters).getAE();- cas = ae.newCAS();- } catch (ResourceInitializationException e) {- throw new RuntimeException(e);- }+ this.descriptorPath = descriptorPath;+ this.configurationParameters = configurationParameters;}/**@@ -58,8 +57,15 @@ public abstract class BaseUIMATokenizer extends Tokenizer {** @throws IOException If there is a low-level I/O error.*/- protected void analyzeInput() throws AnalysisEngineProcessException, IOException {- cas.reset();+ protected void analyzeInput() throws ResourceInitializationException, AnalysisEngineProcessException, IOException {+ if (ae == null) {+ ae = AEProviderFactory.getInstance().getAEProvider(null, descriptorPath, configurationParameters).getAE();+ }+ if (cas == null) {+ cas = ae.newCAS();+ } else {+ cas.reset();+ }cas.setDocumentText(toString(input));ae.process(cas);}@@ -90,5 +96,18 @@ public abstract class BaseUIMATokenizer extends Tokenizer {iterator = null;}+ @Override+ public void close() throws IOException {+ super.close();+ // release resources and ease GC+ if (ae != null) {+ ae.destroy();+ ae = null;+ }+ if (cas != null) {+ cas.release();+ cas = null;+ }+ }}diff --git a/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMAAnnotationsTokenizer.java b/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMAAnnotationsTokenizer.javaindex 19f8c780424..cf3af6cbba0 100644--- a/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMAAnnotationsTokenizer.java+++ b/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMAAnnotationsTokenizer.java@@ -23,6 +23,7 @@ import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;import org.apache.uima.analysis_engine.AnalysisEngineProcessException;import org.apache.uima.cas.Type;import org.apache.uima.cas.text.AnnotationFS;+import org.apache.uima.resource.ResourceInitializationException;import java.io.IOException;import java.io.Reader;@@ -54,6 +55,8 @@ public final class UIMAAnnotationsTokenizer extends BaseUIMATokenizer {analyzeInput();} catch (AnalysisEngineProcessException e) {throw new IOException(e);+ } catch (ResourceInitializationException e) {+ throw new IOException(e);}finalOffset = correctOffset(cas.getDocumentText().length());Type tokenType = cas.getTypeSystem().getType(tokenTypeString);diff --git a/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMATypeAwareAnnotationsTokenizer.java b/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMATypeAwareAnnotationsTokenizer.javaindex abdcb84bd9b..8fb33c0494a 100644--- a/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMATypeAwareAnnotationsTokenizer.java+++ b/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMATypeAwareAnnotationsTokenizer.java@@ -26,6 +26,7 @@ import org.apache.uima.cas.CASException;import org.apache.uima.cas.FeaturePath;import org.apache.uima.cas.Type;import org.apache.uima.cas.text.AnnotationFS;+import org.apache.uima.resource.ResourceInitializationException;import java.io.IOException;import java.io.Reader;@@ -66,6 +67,8 @@ public final class UIMATypeAwareAnnotationsTokenizer extends BaseUIMATokenizer {analyzeInput();} catch (AnalysisEngineProcessException e) {throw new IOException(e);+ } catch (ResourceInitializationException e) {+ throw new IOException(e);}featurePath = cas.createFeaturePath();try {diff --git a/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/ae/AEProviderFactory.java b/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/ae/AEProviderFactory.javaindex be671136660..d08c85044d9 100644--- a/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/ae/AEProviderFactory.java+++ b/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/ae/AEProviderFactory.java@@ -25,7 +25,7 @@ import java.util.Map;*/public class AEProviderFactory {- private static AEProviderFactory instance;+ private static final AEProviderFactory instance = new AEProviderFactory();private final Map<String, AEProvider> providerCache = new HashMap<String, AEProvider>();@@ -34,15 +34,12 @@ public class AEProviderFactory {}public static AEProviderFactory getInstance() {- if (instance == null) {- instance = new AEProviderFactory();- }return instance;}/**- * @param keyPrefix a prefix of the key used to cache the AEProvider- * @param aePath the AnalysisEngine descriptor path+ * @param keyPrefix a prefix of the key used to cache the AEProvider+ * @param aePath the AnalysisEngine descriptor path* @param runtimeParameters map of runtime parameters to configure inside the AnalysisEngine* @return AEProvider*/@@ -69,7 +66,7 @@ public class AEProviderFactory {}/**- * @param aePath the AnalysisEngine descriptor path+ * @param aePath the AnalysisEngine descriptor path* @param runtimeParameters map of runtime parameters to configure inside the AnalysisEngine* @return AEProvider*/diff --git a/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/AEProviderFactoryTest.java b/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/AEProviderFactoryTest.javanew file mode 100644index 00000000000..66f15c24dba--- /dev/null+++ b/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/AEProviderFactoryTest.java@@ -0,0 +1,44 @@+package org.apache.lucene.analysis.uima.ae;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.junit.Test;++import java.util.HashMap;++import static org.junit.Assert.assertTrue;++/**+ * Testcase for {@link AEProviderFactory}+ */+public class AEProviderFactoryTest {++ @Test+ public void testCorrectCaching() throws Exception {+ AEProvider aeProvider = AEProviderFactory.getInstance().getAEProvider("/uima/TestAggregateSentenceAE.xml");+ assertTrue(aeProvider == AEProviderFactory.getInstance().getAEProvider("/uima/TestAggregateSentenceAE.xml"));+ }++ @Test+ public void testCorrectCachingWithParameters() throws Exception {+ AEProvider aeProvider = AEProviderFactory.getInstance().getAEProvider("prefix", "/uima/TestAggregateSentenceAE.xml",+ new HashMap<String, Object>());+ assertTrue(aeProvider == AEProviderFactory.getInstance().getAEProvider("prefix", "/uima/TestAggregateSentenceAE.xml",+ new HashMap<String, Object>()));+ }+}diff --git a/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/OverridingParamsAEProviderTest.java b/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/OverridingParamsAEProviderTest.javaindex c3419253a2a..0922184a273 100644--- a/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/OverridingParamsAEProviderTest.java+++ b/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/OverridingParamsAEProviderTest.java@@ -24,7 +24,9 @@ import org.junit.Test;import java.util.HashMap;import java.util.Map;-import static org.junit.Assert.*;+import static org.junit.Assert.assertEquals;+import static org.junit.Assert.assertNotNull;+import static org.junit.Assert.fail;/*** TestCase for {@link OverridingParamsAEProvider}@@ -56,6 +58,8 @@ public class OverridingParamsAEProviderTest {AEProvider aeProvider = new OverridingParamsAEProvider("/uima/AggregateSentenceAE.xml", runtimeParameters);AnalysisEngine analysisEngine = aeProvider.getAE();assertNotNull(analysisEngine);- assertEquals(analysisEngine.getConfigParameterValue("ngramsize"), 3);+ Object parameterValue = analysisEngine.getConfigParameterValue("ngramsize");+ assertNotNull(parameterValue);+ assertEquals(Integer.valueOf(3), Integer.valueOf(parameterValue.toString()));}}