LUCENE-4756 - release AE and CAS on #close

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1442876 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tommaso Teofili 2013-02-06 09:39:08 +00:00
parent 5b48346ed7
commit f1d80996fa
6 changed files with 90 additions and 20 deletions

View File

@ -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;
}
}
}

View File

@ -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);

View File

@ -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 {

View File

@ -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
*/

View File

@ -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>()));
}
}

View File

@ -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()));
}
}