You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by bu...@apache.org on 2016/08/21 22:26:28 UTC

svn commit: r1757094 - in /uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource: ./ impl/ metadata/impl/

Author: burn
Date: Sun Aug 21 22:26:28 2016
New Revision: 1757094

URL: http://svn.apache.org/viewvc?rev=1757094&view=rev
Log:
UIMA-5058 Revert to the faster check-descriptions approach and add a cache of imported URLs to match the cache of descriptions created from them

Modified:
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/CasManager_impl.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/FsIndexCollection_impl.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypePriorities_impl.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_impl.java

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java?rev=1757094&r1=1757093&r2=1757094&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java Sun Aug 21 22:26:28 2016
@@ -23,6 +23,7 @@ import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.uima.resource.metadata.ResourceManagerConfiguration;
 import org.apache.uima.util.XMLizable;
@@ -312,4 +313,11 @@ public interface ResourceManager {
    * @return A map from absolute URL to the XMLizable object that was parsed from that URL
    */
   public Map<String,XMLizable> getImportCache();
+  
+  /**
+   * Gets a cache of imported descriptor URLs from which the parsed objects in the above map
+   * were created, so that these URLs are not re-parsed if the same URL is imported again.
+   * @return A map from absolute URL to a set of URLs it imports.
+   */
+  public Map<String,Set<String>> getImportUrlsCache();
 }

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/CasManager_impl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/CasManager_impl.java?rev=1757094&r1=1757093&r2=1757094&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/CasManager_impl.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/CasManager_impl.java Sun Aug 21 22:26:28 2016
@@ -20,7 +20,6 @@
 package org.apache.uima.resource.impl;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -42,12 +41,8 @@ import org.apache.uima.resource.CasManag
 import org.apache.uima.resource.ResourceInitializationException;
 import org.apache.uima.resource.ResourceManager;
 import org.apache.uima.resource.metadata.FsIndexCollection;
-import org.apache.uima.resource.metadata.FsIndexDescription;
-import org.apache.uima.resource.metadata.MetaDataObject;
 import org.apache.uima.resource.metadata.ProcessingResourceMetaData;
-import org.apache.uima.resource.metadata.TypeDescription;
 import org.apache.uima.resource.metadata.TypePriorities;
-import org.apache.uima.resource.metadata.TypePriorityList;
 import org.apache.uima.resource.metadata.TypeSystemDescription;
 import org.apache.uima.util.CasCreationUtils;
 import org.apache.uima.util.CasPool;
@@ -101,99 +96,57 @@ public class CasManager_impl implements
    */
   public synchronized void addMetaData(ProcessingResourceMetaData aMetaData) {
     if (mCasDefinition != null) {
-      String error = containsSameTypeAndIndexInfo(aMetaData);  //UIMA-1249 UIMA-5058
-      if (error == null) { 
+      if (containsSameTypeAndIndexInfo(aMetaData)) { //UIMA-1249
         return;
       }
-      throw new UIMARuntimeException(UIMARuntimeException.ILLEGAL_ADDING_OF_NEW_META_INFO_AFTER_CAS_DEFINED, new Object[] {error});  // internal error  UIMA-1249    
+      throw new UIMARuntimeException(UIMARuntimeException.ILLEGAL_ADDING_OF_NEW_META_INFO_AFTER_CAS_DEFINED, 
+              new Object[] {aMetaData.getSourceUrlString()});  // internal error  UIMA-1249    
     }
     mMetaDataList.add(aMetaData);
+//    mCasDefinition = null; // marka this stale
+//    mCurrentTypeSystem = null; //this too
   }
   
   /**
-   * This check is needed to avoid throwing errors in the use case:
+   * This comparison is needed to avoid throwing errors in the use case:
    *   a) the pipeline has been fully initialized, and one or more CASes have been drawn from this pool
    *   b) Another instance of an annotator using the same resource manager is initialized.
    *   
    * In this case there is no change to the metadata, and we do not want to disturb anything.
    * 
    * @param md the metadata to see if its in the list already
-   * @return  null if the type system description, the type priority description, and the index collection are in the list already,
-   *          or the name of the failing item. UIMA-5058
+   * @return true if the type system description, the type priority description, and the index collection are
+   * in the list already.
    */
-  private String containsSameTypeAndIndexInfo(ProcessingResourceMetaData md) {
+  private boolean containsSameTypeAndIndexInfo(ProcessingResourceMetaData md) {
     final TypeSystemDescription tsd = md.getTypeSystem();
-    if (tsd != null) {
-      for (TypeDescription td : tsd.getTypes()) {
-        String missing = findMissingEntry(td);
-        if (missing != null) return missing;  // At least 1 type is new
-      }
-    }
-    final TypePriorities tp = md.getTypePriorities();
-    if (tp != null) {
-      for (TypePriorityList tpl : tp.getPriorityLists()) {
-        String missing = findMissingEntry(tpl);
-        if (missing != null) return missing;  // At least 1 priority-list is new
-      }
-    }
+    final TypePriorities tsp = md.getTypePriorities();
     final FsIndexCollection ic = md.getFsIndexCollection();
-    if (ic != null) {
-      for (FsIndexDescription fid : ic.getFsIndexes()) {
-        String missing = findMissingEntry(fid);
-        if (missing != null) return missing;   // At least 1 index is new
-      }
-    }
-    return null;  // All metadata already merged
-  }
-  
-  /*
-   * Check if the MetaDataObject is missing from the already merged CasDefinition
-   * Returns its name, or null if it is found.
-   */
-  private String findMissingEntry(MetaDataObject mdo) {
-    for (ProcessingResourceMetaData oldMd : mMetaDataList) {
-      MetaDataObject[] array = getMdArray(mdo, oldMd);
-      if (array != null) {
-        for (MetaDataObject oldMdo : array) {
-          if (mdo.equals(oldMdo)) return null;
-        }
+    
+    for (ProcessingResourceMetaData item : mMetaDataList) {
+      final TypeSystemDescription otsd = item.getTypeSystem();
+      final TypePriorities otsp = item.getTypePriorities();
+      final FsIndexCollection oic = item.getFsIndexCollection();
+      if (equalsWithNulls(tsp, otsp) &&
+          equalsWithNulls(tsd, otsd) &&
+          equalsWithNulls(ic , oic )) {
+        return true;
       }
     }
-    return getMdName(mdo);
+    return false;
   }
   
-  /*
-   * Return an array of TypeDescriptions or TypePriorities or FsIndexDescriptions
-   */
-  private MetaDataObject[] getMdArray(MetaDataObject type, ProcessingResourceMetaData md) {
-    if (type instanceof TypeDescription) {
-      TypeSystemDescription tsd = md.getTypeSystem();
-      return tsd==null ? null : tsd.getTypes();
-    } else if (type instanceof TypePriorityList) {
-      TypePriorities tp = md.getTypePriorities();
-      return tp==null ? null : tp.getPriorityLists();
-    } else if (type instanceof FsIndexDescription) {
-       FsIndexCollection ic = md.getFsIndexCollection();
-      return ic==null ? null : ic.getFsIndexes();
+  private boolean equalsWithNulls(Object a, Object b) {
+    if (a == null && b == null) {
+      return true;
     }
-    return null;
-  }
-  
-  /*
-   * Return the "name" of the MetaDataObject for an error message
-   */
-  private String getMdName(MetaDataObject type) {
-    String name = "?";
-    if (type instanceof TypeDescription) {
-      name = "Type: " + ((TypeDescription)type).getName();
-    } else if (type instanceof TypePriorityList) {
-      name = "TypePriority: " + Arrays.toString(((TypePriorityList)type).getTypes());
-    } else if (type instanceof FsIndexDescription) {
-      name = "FsIndex: " + ((FsIndexDescription)type).getLabel();
+    if (a != null) {
+      return a.equals(b);
+    } else {
+      return b.equals(a);
     }
-    return name + "@" + type.getSourceUrlString();
   }
-  
+
   /*
    * (non-Javadoc)
    * 

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java?rev=1757094&r1=1757093&r2=1757094&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java Sun Aug 21 22:26:28 2016
@@ -28,6 +28,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.uima.UIMAFramework;
@@ -144,6 +145,12 @@ public class ResourceManager_impl implem
   final private Map<String,XMLizable> importCache = Collections.synchronizedMap(new HashMap<String,XMLizable>());
   
   /**
+   * Cache of imported descriptor URLs from which the parsed objects in importCache
+   * were created, so that these URLs are not re-parsed if the same URL is imported again.
+   */
+  final private Map<String,Set<String>> importUrlsCache = Collections.synchronizedMap(new HashMap<String,Set<String>>());
+  
+  /**
    * Creates a new <code>ResourceManager_impl</code>.
    */
   public ResourceManager_impl() {
@@ -713,4 +720,7 @@ public class ResourceManager_impl implem
     return importCache;
   }
 
+  public Map<String, Set<String>> getImportUrlsCache() {
+    return importUrlsCache;
+  }
 }

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/FsIndexCollection_impl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/FsIndexCollection_impl.java?rev=1757094&r1=1757093&r2=1757094&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/FsIndexCollection_impl.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/FsIndexCollection_impl.java Sun Aug 21 22:26:28 2016
@@ -26,6 +26,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeSet;
 
 import org.apache.uima.UIMAFramework;
@@ -257,16 +258,24 @@ public class FsIndexCollection_impl exte
     FsIndexCollection desc;    
     String urlString = aURL.toString();
     Map<String, XMLizable> importCache = aResourceManager.getImportCache();
+    Map<String, Set<String>> importUrlsCache = aResourceManager.getImportUrlsCache();
     synchronized(importCache) {
       XMLizable cachedObject = importCache.get(urlString);
       if (cachedObject instanceof FsIndexCollection) {
         desc = (FsIndexCollection)cachedObject;
+        // Add the URLs parsed for this cached object to the list already-parsed (UIMA-5058)
+        aAlreadyImportedFsIndexCollectionURLs.addAll(importUrlsCache.get(urlString));
       } else {   
         XMLInputSource input;
         input = new XMLInputSource(aURL);
         desc = UIMAFramework.getXMLParser().parseFsIndexCollection(input);
+        TreeSet<String> previouslyImported = new TreeSet<String>(aAlreadyImportedFsIndexCollectionURLs);
         desc.resolveImports(aAlreadyImportedFsIndexCollectionURLs, aResourceManager);
         importCache.put(urlString, desc);
+        // Save the URLS parsed by this import 
+        TreeSet<String> locallyImported = new TreeSet<String>(aAlreadyImportedFsIndexCollectionURLs);
+        locallyImported.removeAll(previouslyImported);
+        importUrlsCache.put(urlString, locallyImported);
       }
     }
     aResults.addAll(Arrays.asList(desc.getFsIndexes()));

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypePriorities_impl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypePriorities_impl.java?rev=1757094&r1=1757093&r2=1757094&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypePriorities_impl.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypePriorities_impl.java Sun Aug 21 22:26:28 2016
@@ -26,6 +26,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeSet;
 
 import org.apache.uima.UIMAFramework;
@@ -266,16 +267,24 @@ public class TypePriorities_impl extends
     TypePriorities desc;    
     String urlString = aURL.toString();
     Map<String, XMLizable> importCache = aResourceManager.getImportCache();
+    Map<String, Set<String>> importUrlsCache = aResourceManager.getImportUrlsCache();
     synchronized(importCache) {
       XMLizable cachedObject = importCache.get(urlString);
       if (cachedObject instanceof TypePriorities) {
         desc = (TypePriorities)cachedObject;
+        // Add the URLs parsed for this cached object to the list already-parsed (UIMA-5058)
+        aAlreadyImportedTypePrioritiesURLs.addAll(importUrlsCache.get(urlString));
       } else {   
         XMLInputSource input;
         input = new XMLInputSource(aURL);
         desc = UIMAFramework.getXMLParser().parseTypePriorities(input);
+        TreeSet<String> previouslyImported = new TreeSet<String>(aAlreadyImportedTypePrioritiesURLs);
         desc.resolveImports(aAlreadyImportedTypePrioritiesURLs, aResourceManager);
         importCache.put(urlString, desc);
+        // Save the URLS parsed by this import 
+        TreeSet<String> locallyImported = new TreeSet<String>(aAlreadyImportedTypePrioritiesURLs);
+        locallyImported.removeAll(previouslyImported);
+        importUrlsCache.put(urlString, locallyImported);
       }
     }
     aResults.addAll(Arrays.asList(desc.getPriorityLists()));

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_impl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_impl.java?rev=1757094&r1=1757093&r2=1757094&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_impl.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_impl.java Sun Aug 21 22:26:28 2016
@@ -26,6 +26,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeSet;
 
 import org.apache.uima.UIMAFramework;
@@ -261,17 +262,26 @@ public class TypeSystemDescription_impl
     TypeSystemDescription desc;    
     String urlString = aURL.toString();
     Map<String, XMLizable> importCache = aResourceManager.getImportCache();
+    Map<String, Set<String>> importUrlsCache = aResourceManager.getImportUrlsCache();
     synchronized(importCache) {
       XMLizable cachedObject = importCache.get(urlString);
       if (cachedObject instanceof TypeSystemDescription) {
         desc = (TypeSystemDescription)cachedObject;
+        // Add the URLs parsed for this cached object to the list already-parsed (UIMA-5058)
+        aAlreadyImportedTypeSystemURLs.addAll(importUrlsCache.get(urlString));
       } else {   
         XMLInputSource input;
         input = new XMLInputSource(aURL);
         desc = UIMAFramework.getXMLParser().parseTypeSystemDescription(input);
+        TreeSet<String> previouslyImported = new TreeSet<String>(aAlreadyImportedTypeSystemURLs);
         desc.resolveImports(aAlreadyImportedTypeSystemURLs, aResourceManager);
         importCache.put(urlString, desc);
+        // Save the URLS parsed by this import 
+        TreeSet<String> locallyImported = new TreeSet<String>(aAlreadyImportedTypeSystemURLs);
+        locallyImported.removeAll(previouslyImported);
+        importUrlsCache.put(urlString, locallyImported);
       }
+      
     }
     aResults.addAll(Arrays.asList(desc.getTypes()));
   }