Author: schor
Date: Thu Oct 13 15:51:57 2016
New Revision: 1764717

URL: http://svn.apache.org/viewvc?rev=1764717&view=rev
Log:
[UIMA-5115] updates to Select framework, after writing the docs for it, so it 
matches the docs.

Modified:
    
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java
    
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIndexRepositoryImpl.java
    
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/LowLevelIndex.java
    
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java

Modified: 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java
URL: 
http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java?rev=1764717&r1=1764716&r2=1764717&view=diff
==============================================================================
--- 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java
 (original)
+++ 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java
 Thu Oct 13 15:51:57 2016
@@ -36,19 +36,19 @@ import org.apache.uima.jcas.cas.TOP;
  */
 public interface SelectFSs<T extends FeatureStructure> extends Iterable<T>, 
Stream<T> {
   
-  // If not specified, defaults to all FSs (unordered) unless AnnotationIndex 
implied
-    // Methods take their generic type from the variable to which they are 
assigned except for
-    // index(class) which takes it from its argument.
-  <N extends FeatureStructure> SelectFSs<N> index(String indexName);  
-  <N extends FeatureStructure> SelectFSs<N> index(FSIndex<N> index);
-
-  // If not specified defaults to the index's uppermost type.
-  // Methods take their generic type from the variable to which they are 
assigned except for
-  // type(class) which takes it from its argument.
-  <N extends FeatureStructure> SelectFSs<N> type(Type uimaType);
-  <N extends FeatureStructure> SelectFSs<N> type(String 
fullyQualifiedTypeName);
-  <N extends FeatureStructure> SelectFSs<N> type(int jcasClass_dot_type);
-  <N extends FeatureStructure> SelectFSs<N> type(Class<N> jcasClass_dot_class);
+//  // If not specified, defaults to all FSs (unordered) unless 
AnnotationIndex implied
+//    // Methods take their generic type from the variable to which they are 
assigned except for
+//    // index(class) which takes it from its argument.
+//  <N extends FeatureStructure> SelectFSs<N> index(String indexName);  
+//  <N extends FeatureStructure> SelectFSs<N> index(FSIndex<N> index);
+//
+//  // If not specified defaults to the index's uppermost type.
+//  // Methods take their generic type from the variable to which they are 
assigned except for
+//  // type(class) which takes it from its argument.
+//  <N extends FeatureStructure> SelectFSs<N> type(Type uimaType);
+//  <N extends FeatureStructure> SelectFSs<N> type(String 
fullyQualifiedTypeName);
+//  <N extends FeatureStructure> SelectFSs<N> type(int jcasClass_dot_type);
+//  <N extends FeatureStructure> SelectFSs<N> type(Class<N> 
jcasClass_dot_class);
     
 //  SelectFSs<T> shift(int amount); // incorporated into startAt 
   
@@ -163,8 +163,8 @@ public interface SelectFSs<T extends Fea
   // ---------------------------------
   FSIterator<T> fsIterator();
   Iterator<T> iterator();
-  List<T> asList(Class<T> clazz);
-  T[] asArray(Class<T> clazz);
+  <N extends T> List<N> asList();
+  <N extends T> N[] asArray(Class<N> clazz);
   Spliterator<T> spliterator();
   
   // returning one item
@@ -190,15 +190,27 @@ public interface SelectFSs<T extends Fea
   T single(int begin, int end, int offset);       // throws if not exactly 1 
element
   T singleOrNull(int begin, int end, int offset); // throws if more than 1 
element, returns single or null
   
-  // ---------------------------------
-  // The methods below are alternatives 
-  // to the methods above, that combine
-  // frequently used patterns into more
-  // concise forms using positional arguments
-  // ---------------------------------
   
-  static <N extends FeatureStructure> SelectFSs<N> sselect(FSIndex<N> index) {
+  /**
+   * static methods that more effectively capture the generic argument
+   */
+  /**
+   * @param index
+   * @return
+   */
+  static <U extends FeatureStructure, V extends U> SelectFSs<V> 
sselect(FSIndex<U> index) {
     return index.select();    
   } 
+  
+//  /**
+//   * DON'T USE THIS, use index.select(XXX.class) instead
+//   * @param index the index to use
+//   * @param clazz the JCas class
+//   * @return a select instance for this index and type
+//   */
+//  static <U extends FeatureStructure, V extends U> SelectFSs<V> 
sselect(FSIndex<U> index, Class<V> clazz) {
+//    return index.select(clazz);
+//  }
+ 
 
 }

Modified: 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIndexRepositoryImpl.java
URL: 
http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIndexRepositoryImpl.java?rev=1764717&r1=1764716&r2=1764717&view=diff
==============================================================================
--- 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIndexRepositoryImpl.java
 (original)
+++ 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIndexRepositoryImpl.java
 Thu Oct 13 15:51:57 2016
@@ -350,7 +350,18 @@ public class FSIndexRepositoryImpl imple
 
   // one bit per typeCode, indexed by typeCode
   //   This is only to speed up the test to skip adding an index to the set of 
"used" ones if it already is used.
-  final private boolean[] isUsed;
+  final private BitSet isUsed;
+  
+//  /**
+//   * Used for maintaining collection of all used iicp's for indexes
+//   * package scope for setting in index impl flush
+//   */
+//  boolean isUsedChanged = true;
+//  
+//  /**
+//   * iicps for all FSs
+//   */
+//  private List<FsIndex_iicp<?>> iicps4allFSs = null;
   
   // Monitor which indexes are iterated over, to allow resetting flatIndexes
 //  final private List<FsIndex_iicp<? extends FeatureStructure>> 
iteratedSortedIndexes = 
@@ -409,6 +420,8 @@ public class FSIndexRepositoryImpl imple
     this.indexUpdateOperation = null;
     this.usedIndexes = null;
     this.isUsed = null;
+//    this.isUsedChanged = true;
+//    this.iicps4allFSs = null;
   }
 
   /**
@@ -432,7 +445,9 @@ public class FSIndexRepositoryImpl imple
     this.logProcessed = false;
     this.indexArray = new IndexesForType[this.sii.tsi.getNumberOfTypes() + 1];
     this.usedIndexes = new IntVector();
-    this.isUsed = new boolean[numTypes];
+    this.isUsed = new BitSet(numTypes);
+//    this.isUsedChanged = true;
+//    this.iicps4allFSs = new ArrayList<>();
     init();
   }
 
@@ -460,8 +475,9 @@ public class FSIndexRepositoryImpl imple
     this.logProcessed = false;
     this.indexArray = new IndexesForType[numTypes];
     this.usedIndexes = new IntVector();
-    this.isUsed = new boolean[numTypes];
-
+    this.isUsed = new BitSet(numTypes);
+//    this.isUsedChanged = true;
+//    this.iicps4allFSs = new ArrayList<>();
     init();
     // cant do this here because need to have the CAS's ref to this instance 
set before this is done.
 //    baseIndexRepo.name2indexMap.keySet().stream().forEach(key -> 
createIndex(baseIndexRepo, key));
@@ -623,10 +639,11 @@ public class FSIndexRepositoryImpl imple
     }
     
     annotationIndexes.clear();
-    
+    isUsed.clear();
+//    isUsedChanged = true;
+//    iicps4allFSs.clear();
     for (int i = 0; i < usedIndexes.size(); i++) {
       int used = this.usedIndexes.get(i);
-      isUsed[used] = false;
       for (FsIndex_iicp<?> iicp : indexArray[used].indexesForType) {
         iicp.fsIndex_singletype.flush();
       }
@@ -1318,9 +1335,10 @@ public class FSIndexRepositoryImpl imple
       ((FsIndex_singletype<T>)(indexes.get(indexes.size() - 
1)).fsIndex_singletype).insert(fs);
     }
 
-    if (!this.isUsed[typeCode]) {
+    if (!this.isUsed.get(typeCode)) {
       // mark this type as being in some indexes
-      this.isUsed[typeCode] = true;
+      this.isUsed.set(typeCode);
+//      this.isUsedChanged = true;
       this.usedIndexes.add(typeCode);
     }
   }
@@ -1329,6 +1347,13 @@ public class FSIndexRepositoryImpl imple
     return "_" + type.getName() + "_DefaultBagGeneratedIndex";
   }
 
+  /**
+   * Common remove FS code; all remove operations call this, except bulk 
remove (flush and removeall...)
+   * Removes FS from all indexes in this view (except bag if skipBagIndexes is 
true)
+   * @param fs the fs to remove
+   * @param skipBagIndexes set true by protect-indexes style of temporary 
removal
+   * @return true if it was removed
+   */
   boolean removeFS_ret(TOP fs, boolean skipBagIndexes) {
     if (skipBagIndexes && !fs._inSetSortedIndex()) {
       return false;
@@ -1358,6 +1383,9 @@ public class FSIndexRepositoryImpl imple
       }
     }
     
+    /**
+     * Actual remove loop over all indexes for this type in this view
+     */
     for (FsIndex_iicp<TOP> iicp : indexes4type) {
       FsIndex_singletype<TOP> st = iicp.fsIndex_singletype;
       if (skipBagIndexes && !st.isSetOrSorted()) {
@@ -1377,10 +1405,13 @@ public class FSIndexRepositoryImpl imple
       if (this.cas.getCurrentMark() != null) {
         logIndexOperation(fs, ITEM_REMOVED_FROM_INDEX);
       }
-      if (skipBagIndexes ||              // means called for remove from all 
corruptable indexes 
-          fs instanceof AnnotationBase) {  // means only indexed in this index
-        fs._resetInSetSortedIndex();
-      } 
+      
+   // oops, might still be indexed in other views if not instance of 
AnnotationBase
+   // reset in caller (removeFromIndexAnyView)
+//      if (skipBagIndexes ||              // means called for remove only 
from all corruptable indexes 
+//          fs instanceof AnnotationBase) {  // means only indexed in this 
view 
+//        fs._resetInSetSortedIndex();     
+//      } 
     }
     return wasRemoved;    
   }
@@ -1396,7 +1427,25 @@ public class FSIndexRepositoryImpl imple
    */
   public <T extends FeatureStructure> LowLevelIterator<T> getAllIndexedFS(Type 
type) {
     final ArrayList<LowLevelIterator<T>> iteratorList = new ArrayList<>();
-    getAllIndexedFS(type, iteratorList);
+    
+//    TypeImpl ti = (TypeImpl) type;
+//    if (!isUsedChanged && ti.isTopType()) {  
+//      // reuse previously computed iicps4allFSs
+//      for (FsIndex_iicp<?> iicp : iicps4allFSs) {
+//        if (iicp.cachedSubFsLeafIndexes[0].size() != 0) {
+//          LowLevelIterator<T> it = (iicp.getIndexingStrategy() == 
FSIndex.SORTED_INDEX) 
+//              ? (LowLevelIterator<T>)iicp.iteratorUnordered()
+//              : (LowLevelIterator<T>)iicp.iterator();
+//          iteratorList.add(it);
+//        }
+//      }
+//    } else {
+//      iicps4allFSs.clear();
+      getAllIndexedFS(type, iteratorList);
+//      this.isUsedChanged = false; // above call recomputed the cache  
+//    }    
+
+    
     final int iteratorListSize = iteratorList.size();
     if (iteratorListSize == 0) {
       return (LowLevelIterator<T>) 
LowLevelIterator.FS_ITERATOR_LOW_LEVEL_EMPTY;
@@ -1422,11 +1471,14 @@ public class FSIndexRepositoryImpl imple
     //   Note that a default bag index is guaranteed to exist if any FS of 
Type type were added to the indexes
     //     and only a SET index was defined, see 
https://issues.apache.org/jira/browse/UIMA-4111
 
-    // get all indexes for this type
+    
+    // get all indexes for this type and compute iicps4allFSs
+
     TypeImpl ti = (TypeImpl)type;
-    if (isUsed[ti.getCode()]) {
+    if (isUsed.get(ti.getCode())) {
       FsIndex_iicp<TOP> iicp = 
getIndexesForType(ti.getCode()).getNonSetIndex();  
       
+//      iicps4allFSs.add(iicp);
       if (null != iicp && !iicp.isEmpty()) {
         LowLevelIterator<T> it = (iicp.getIndexingStrategy() == 
FSIndex.SORTED_INDEX) 
                                     ? 
(LowLevelIterator<T>)iicp.iteratorUnordered()

Modified: 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/LowLevelIndex.java
URL: 
http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/LowLevelIndex.java?rev=1764717&r1=1764716&r2=1764717&view=diff
==============================================================================
--- 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/LowLevelIndex.java
 (original)
+++ 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/LowLevelIndex.java
 Thu Oct 13 15:51:57 2016
@@ -121,28 +121,28 @@ public interface LowLevelIndex<T extends
   
   
   @Override
-  default SelectFSs<T> select() {
-    return getCasImpl().select().index(this);
+  default <N extends FeatureStructure> SelectFSs<N> select() {
+    return ((SelectFSs_impl)getCasImpl().select()).index(this);
   }
 
   @Override
   default <N extends FeatureStructure> SelectFSs<N> select(Type type) {
-    return select().type(type);
+    return ((SelectFSs_impl)select()).type(type);
   }
 
   @Override
   default <N extends FeatureStructure> SelectFSs<N> select(Class<N> clazz) {
-    return select().type(clazz);
+    return ((SelectFSs_impl)select()).type(clazz);
   }
 
   @Override
   default <N extends FeatureStructure> SelectFSs<N> select(int jcasType) {
-    return select().type(jcasType);
+    return ((SelectFSs_impl)select()).type(jcasType);
   }
 
   @Override
   default <N extends FeatureStructure> SelectFSs<N> select(String 
fullyQualifiedTypeName) {
-    return select().type(fullyQualifiedTypeName);
+    return ((SelectFSs_impl)select()).type(fullyQualifiedTypeName);
   }
   
 }

Modified: 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java
URL: 
http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java?rev=1764717&r1=1764716&r2=1764717&view=diff
==============================================================================
--- 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java
 (original)
+++ 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java
 Thu Oct 13 15:51:57 2016
@@ -20,11 +20,13 @@
 package org.apache.uima.cas.impl;
 
 import java.lang.reflect.Array;
+import java.util.AbstractSequentialList;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
+import java.util.ListIterator;
 import java.util.Optional;
 import java.util.Spliterator;
 import java.util.function.BiConsumer;
@@ -134,13 +136,12 @@ public class SelectFSs_impl <T extends F
    * INDEX
    * If not specified, defaults to all FSs (unordered) unless AnnotationIndex 
implied
    */
-  @Override
+
   public <N extends FeatureStructure> SelectFSs_impl<N> index(String 
indexName) {
     this.index = view.indexRepository.getIndex(indexName);
     return (SelectFSs_impl<N>) this;
   }
   
-  @Override
   public <N extends FeatureStructure> SelectFSs_impl<N> index(FSIndex<N> 
aIndex) {
     this.index = (FSIndex<T>) aIndex;
     return (SelectFSs_impl<N>) this;
@@ -150,22 +151,22 @@ public class SelectFSs_impl <T extends F
    * TYPE
    * if not specified defaults to the index's uppermost type.  
    */
-  @Override
+  
   public <N extends FeatureStructure> SelectFSs_impl<N> type(Type uimaType) {
     this.ti = (TypeImpl) uimaType;
     return (SelectFSs_impl<N>) this;
   }
-  @Override
+  
   public <N extends FeatureStructure> SelectFSs_impl<N> type(String 
fullyQualifiedTypeName) {
     this.ti = view.getTypeSystemImpl().getType(fullyQualifiedTypeName);
     return (SelectFSs_impl<N>) this;
   }
-  @Override
+
   public <N extends FeatureStructure> SelectFSs_impl<N> type(int 
jcasClass_dot_type) {
     this.ti = (TypeImpl) view.getJCas().getCasType(jcasClass_dot_type);
     return (SelectFSs_impl<N>) this;
   }
-  @Override
+
   public <N extends FeatureStructure> SelectFSs_impl<N> type(Class<N> 
jcasClass_dot_class) {
     this.ti = (TypeImpl) view.getJCasImpl().getCasType(jcasClass_dot_class);
     return (SelectFSs_impl<N>) this;
@@ -471,8 +472,7 @@ public class SelectFSs_impl <T extends F
         ti = (TypeImpl) index.getType();
       }
     } else {
-      if (index != null &&
-          ((TypeImpl)index.getType()).subsumes(ti)) {
+      if (index != null && ((TypeImpl)index.getType()).subsumes(ti)) {
         index = ((LowLevelIndex)index).getSubIndex(ti);
       }
     }
@@ -618,6 +618,8 @@ public class SelectFSs_impl <T extends F
       if (isAltSource) {
         return altSourceIterator();
       } else {
+        // idx is null after prepareTerminalOp has been called.
+        // guaranteed not annotationindex or any op that requires that
         return v.indexRepository.getAllIndexedFS(ti);
       }
     }
@@ -692,44 +694,52 @@ public class SelectFSs_impl <T extends F
   public Iterator<T> iterator() {
     return fsIterator();
   }
-  
-  
-  /**
-   * Initial simple impl, creates reified list
-   *   Better impl would be to use underlying fsIterator
-   * @return
-   */
+    
   @Override
-  public List<T> asList(Class<T> clazz) {
-    T[] a = asArray(clazz);
-    return Arrays.asList(a);
+  public <N extends T> List<N> asList() {
+    prepareTerminalOp();
+    
+    return new AbstractSequentialList<N>() {
+      
+      @Override
+      public ListIterator<N> listIterator(int index) {
+        return (ListIterator<N>) fsIterator();
+      }
+
+      @Override
+      public int size() {
+        return (index == null) ? -1 : index.size();
+      }
+      
+    };
   }
   
   /* (non-Javadoc)
    * @see org.apache.uima.cas.SelectFSs#asArray()
    */
   @Override
-  public T[] asArray(Class<T> clazz) {
+  public <N extends T> N[] asArray(Class<N> clazz) {
     return asArray(fsIterator(), clazz);
   }
 
-  private T[] asArray(FSIterator<T> it, Class<T> clazz) {
-    List<T> al = new ArrayList<>();
+  private <N extends T> N[] asArray(FSIterator<T> it, Class<N> clazz) {
+    List<N> al = asArray1(it);    
+    N[] r = (N[]) Array.newInstance(clazz, al.size());
+    return al.toArray(r);    
+  }
+  
+  private <N extends T> List<N> asArray1(FSIterator<T> it) {
+    List<N> al = new ArrayList<>();
     while (it.isValid()) {
-      al.add(it.getNvc());  // limit iterator might cause it to become invalid
+      al.add((N) it.getNvc());  // limit iterator might cause it to become 
invalid
       it.moveToNext();
     }    
-    T[] r = (T[]) Array.newInstance(clazz, al.size());
-    return al.toArray(r);    
+    return al;    
   }
   
   private FeatureStructure[] asArray(FSIterator<T> it) {
-    List<? super T> al = new ArrayList<>();
-    while (it.isValid()) {
-      al.add(it.getNvc());
-      it.moveToNext(); // limit iterator might cause it to become invalid
-    }    
-    FeatureStructure[] r = (FeatureStructure[]) 
Array.newInstance(FeatureStructure.class,  al.size());
+    List<? super T> al = asArray1(it);
+    FeatureStructure[] r = (FeatureStructure[]) 
Array.newInstance(FeatureStructure.class, al.size());
     return al.toArray(r);
   }
   
@@ -844,7 +854,7 @@ public class SelectFSs_impl <T extends F
     if (it.isValid()) {
       return it.getNvc();
     }
-    if ((!isDoSetTest || isNullOkSpecified) && !isNullOK) {
+    if ((!isDoSetTest || isNullOkSpecified) && !isNullOK) {  // if not 
specified, isNullOK == false
       throw new 
CASRuntimeException(CASRuntimeException.SELECT_GET_NO_INSTANCES, ti.getName(), 
maybeMsgPosition());
     }
     return null;


Reply via email to