You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by Robert Muir <rc...@gmail.com> on 2013/02/06 12:45:29 UTC

Re: svn commit: r1442876 - in /lucene/dev/trunk/lucene/analysis/uima/src: java/org/apache/lucene/analysis/uima/ java/org/apache/lucene/analysis/uima/ae/ test/org/apache/lucene/analysis/uima/ae/

is it really better to make these new for each and every document?

this is what releasing in close() will effectively do here...

On Wed, Feb 6, 2013 at 4:39 AM,  <to...@apache.org> wrote:
> Author: tommaso
> Date: Wed Feb  6 09:39:08 2013
> New Revision: 1442876
>
> URL: http://svn.apache.org/viewvc?rev=1442876&view=rev
> Log:
> LUCENE-4756 - release AE and CAS on #close
>
> Added:
>     lucene/dev/trunk/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/AEProviderFactoryTest.java   (with props)
> Modified:
>     lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/BaseUIMATokenizer.java
>     lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMAAnnotationsTokenizer.java
>     lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMATypeAwareAnnotationsTokenizer.java
>     lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/ae/AEProviderFactory.java
>     lucene/dev/trunk/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/OverridingParamsAEProviderTest.java
>
> Modified: lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/BaseUIMATokenizer.java
> URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/BaseUIMATokenizer.java?rev=1442876&r1=1442875&r2=1442876&view=diff
> ==============================================================================
> --- lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/BaseUIMATokenizer.java (original)
> +++ lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/BaseUIMATokenizer.java Wed Feb  6 09:39:08 2013
> @@ -28,7 +28,6 @@ import org.apache.uima.resource.Resource
>
>  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
>     *
>     * @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
>      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;
> +    }
> +  }
>  }
>
> Modified: lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMAAnnotationsTokenizer.java
> URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMAAnnotationsTokenizer.java?rev=1442876&r1=1442875&r2=1442876&view=diff
> ==============================================================================
> --- lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMAAnnotationsTokenizer.java (original)
> +++ lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMAAnnotationsTokenizer.java Wed Feb  6 09:39:08 2013
> @@ -23,6 +23,7 @@ import org.apache.lucene.analysis.tokena
>  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 UIMAAnnotationsTokeni
>        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);
>
> Modified: lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMATypeAwareAnnotationsTokenizer.java
> URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMATypeAwareAnnotationsTokenizer.java?rev=1442876&r1=1442875&r2=1442876&view=diff
> ==============================================================================
> --- lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMATypeAwareAnnotationsTokenizer.java (original)
> +++ lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/UIMATypeAwareAnnotationsTokenizer.java Wed Feb  6 09:39:08 2013
> @@ -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 UIMATypeAwareAnnotati
>        analyzeInput();
>      } catch (AnalysisEngineProcessException e) {
>        throw new IOException(e);
> +    } catch (ResourceInitializationException e) {
> +      throw new IOException(e);
>      }
>      featurePath = cas.createFeaturePath();
>      try {
>
> Modified: lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/ae/AEProviderFactory.java
> URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/ae/AEProviderFactory.java?rev=1442876&r1=1442875&r2=1442876&view=diff
> ==============================================================================
> --- lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/ae/AEProviderFactory.java (original)
> +++ lucene/dev/trunk/lucene/analysis/uima/src/java/org/apache/lucene/analysis/uima/ae/AEProviderFactory.java Wed Feb  6 09:39:08 2013
> @@ -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
>     */
>
> Added: lucene/dev/trunk/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/AEProviderFactoryTest.java
> URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/AEProviderFactoryTest.java?rev=1442876&view=auto
> ==============================================================================
> --- lucene/dev/trunk/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/AEProviderFactoryTest.java (added)
> +++ lucene/dev/trunk/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/AEProviderFactoryTest.java Wed Feb  6 09:39:08 2013
> @@ -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>()));
> +  }
> +}
>
> Modified: lucene/dev/trunk/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/OverridingParamsAEProviderTest.java
> URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/OverridingParamsAEProviderTest.java?rev=1442876&r1=1442875&r2=1442876&view=diff
> ==============================================================================
> --- lucene/dev/trunk/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/OverridingParamsAEProviderTest.java (original)
> +++ lucene/dev/trunk/lucene/analysis/uima/src/test/org/apache/lucene/analysis/uima/ae/OverridingParamsAEProviderTest.java Wed Feb  6 09:39:08 2013
> @@ -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 OverridingParamsAEProviderT
>      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()));
>    }
>  }
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org