You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2016/03/29 23:37:24 UTC

svn commit: r1737062 - /uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/Id2FS.java

Author: schor
Date: Tue Mar 29 21:37:24 2016
New Revision: 1737062

URL: http://svn.apache.org/viewvc?rev=1737062&view=rev
Log:
[UIMA-4663] support for backwards compatibility a mode where weak refs are bypassed, preventing (disabling) Garbage Collection for FeatureStructures.  When Weak refs are being used, replace them with strong refs if ll_createFS kinds of APIs are used to create the FSs.

Modified:
    uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/Id2FS.java

Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/Id2FS.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/Id2FS.java?rev=1737062&r1=1737061&r2=1737062&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/Id2FS.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/Id2FS.java Tue Mar 29 21:37:24 2016
@@ -21,13 +21,11 @@ package org.apache.uima.cas.impl;
 
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
 import java.util.function.Consumer;
 
 import org.apache.uima.cas.CASRuntimeException;
 import org.apache.uima.jcas.cas.TOP;
+import org.apache.uima.util.Misc;
 
 /**
  * A map from ints representing FS id's (or "addresses") to those FSs
@@ -45,8 +43,17 @@ import org.apache.uima.jcas.cas.TOP;
  * Alternative: a map based on sorted arrays, searched by binary search
  */
 public class Id2FS {
-   
-  final private ArrayList<WeakReference<TOP>> id2fsw;
+  
+  /**
+   * Set this JVM property to true for backwards compatibility, where an application retains
+   * some references to Feature Structures held only using the low-level references (which are ints)..
+   */
+  public static final String DISABLE_FS_GC = "uima.disable_feature_structure_garbage_collection";
+  
+  private static final boolean IS_DISABLE_FS_GC = true || // debug
+      Misc.getNoValueSystemProperty(DISABLE_FS_GC);
+  
+  final private ArrayList<Object> id2fsw;
   
   public Id2FS(int initialHeapSize) {  
     id2fsw = new ArrayList<>(initialHeapSize >> 4);  
@@ -57,22 +64,41 @@ public class Id2FS {
    * @param fs -
    */
   public void add(TOP fs) {
-    id2fsw.add(new WeakReference<TOP>(fs));
+    id2fsw.add(IS_DISABLE_FS_GC ? fs : new WeakReference<TOP>(fs));
+  }
+  
+  public void setStrongRef(TOP fs, int i) {
+    id2fsw.set(i, fs);
+  }
+  
+  public void replaceWithStrongRef(TOP fs) {
+    id2fsw.set(fs._id, fs);  
   }
  
   public <T extends TOP> T get(int id) {
     if (id < 1 || id >= id2fsw.size()) {
       /** The Feature Structure ID {0} is invalid.*/
       throw new CASRuntimeException(CASRuntimeException.INVALID_FS_ID, id);
-    }    
-    return (T) id2fsw.get(id).get();  // could return null if fs is gc'd
+    }  
+    return getNoCheck(id);
   }
   
   public <T extends TOP> T getWithMissingIsNull(int id) {
     if (id < 1 || id >= id2fsw.size()) {
       return null;
     }    
-    return (T) id2fsw.get(id).get();  // could return null if fs is gc'd
+    return getNoCheck(id);  // could return null if fs is gc'd
+  }
+  
+  private <T extends TOP> T getNoCheck(int id) {
+    Object o = id2fsw.get(id);
+    if (o == null) { 
+      return null;
+    }
+    if (o instanceof TOP) {
+      return (T) o; 
+    }
+    return (T) ((WeakReference)o).get();  // could return null if fs is gc'd    
   }
     
   int size() {
@@ -128,8 +154,10 @@ public class Id2FS {
 //      }
 //    }
     // in this impl, the id is the index.
-    for (WeakReference<TOP> wr : id2fsw.subList(fromId, id2fsw.size())) {
-      TOP fs = wr.get();
+    for (Object o : id2fsw.subList(fromId, id2fsw.size())) {
+      TOP fs = (o == null) ? null
+                           : (o instanceof TOP) ? (TOP) o 
+                                                : (TOP) ((WeakReference)o).get(); 
       if (null != fs) {
         action.accept(fs);
       }