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 2014/11/07 22:43:13 UTC

svn commit: r1637457 - in /uima/uimaj/trunk/uimaj-core/src: main/java/org/apache/uima/cas/ main/java/org/apache/uima/cas/impl/ test/java/org/apache/uima/cas/test/

Author: schor
Date: Fri Nov  7 21:43:12 2014
New Revision: 1637457

URL: http://svn.apache.org/r1637457
Log:
[UIMA-4094][UIMA-4095] Fixes to indexes, also switching to PostiveIntSet impl for bag indexes.  Add also new test cases. Update Javadocs to clarify that moveTo(fs) where fs is beyond the end, gives an isValid() == false result.

Modified:
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/FSIterator.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSBagIndex.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIndexRepositoryImpl.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntArrayIndex.java
    uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/IndexRepositoryTest.java
    uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/IteratorTest.java

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/FSIterator.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/FSIterator.java?rev=1637457&r1=1637456&r2=1637457&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/FSIterator.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/FSIterator.java Fri Nov  7 21:43:12 2014
@@ -112,6 +112,9 @@ public interface FSIterator<T extends Fe
    * such feature structure exists in the underlying collection, set the iterator to the "insertion
    * point" for <code>fs</code>, i.e., to a point where the current feature structure is greater
    * than <code>fs</code>, and the previous one is less than <code>fs</code>.
+   * <p>
+   * If the fs is greater than all of the entries in the index, the moveTo cannot set the iterator to an insertion point
+   * where the current feature structure is greater than fs, so it marks the iterator "invalid".
    * 
    * @param fs
    *          The feature structure the iterator should be set to.

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSBagIndex.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSBagIndex.java?rev=1637457&r1=1637456&r2=1637457&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSBagIndex.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSBagIndex.java Fri Nov  7 21:43:12 2014
@@ -37,7 +37,7 @@ import org.apache.uima.internal.util.Pos
  */
 public class FSBagIndex extends FSLeafIndexImpl {
   
-  private static boolean USE_POSITIVE_INT_SET = false;
+  private static boolean USE_POSITIVE_INT_SET = true;
 
   private class IntVectorIterator implements ComparableIntPointerIterator, LowLevelIterator {
 
@@ -127,6 +127,11 @@ public class FSBagIndex extends FSLeafIn
     }
 
     /**
+     * Strictly speaking, moving to a FS in a bag index isn't a valid operation because there are no keys
+     *   to define "equals".
+     *   
+     * However, we implement the following:  If the FS is == (identical), we return that position.
+     * If the FS is not found, we just move to the first item 
      * @see org.apache.uima.internal.util.IntPointerIterator#moveTo(int)
      */
     public void moveTo(int i) {
@@ -134,7 +139,7 @@ public class FSBagIndex extends FSLeafIn
       if (position >= 0) {
         this.itPos = position;
       } else {
-        this.itPos = -(position + 1);
+        moveToFirst();  
       }
     }
 
@@ -225,11 +230,11 @@ public class FSBagIndex extends FSLeafIn
 
   public final boolean insert(int fs) {
     if (USE_POSITIVE_INT_SET) {
-      indexP.add(fs);
+      return indexP.add(fs);
     } else {
       index.add(fs);
+      return true;  // supposed to return true if added, but can't tell, return value ignored anyways
     }
-    return true;
   }
 
   /*

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIndexRepositoryImpl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIndexRepositoryImpl.java?rev=1637457&r1=1637456&r2=1637457&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIndexRepositoryImpl.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIndexRepositoryImpl.java Fri Nov  7 21:43:12 2014
@@ -47,7 +47,6 @@ import org.apache.uima.internal.util.Int
 import org.apache.uima.internal.util.IntPointerIterator;
 import org.apache.uima.internal.util.IntSet;
 import org.apache.uima.internal.util.IntVector;
-import org.apache.uima.internal.util.PositiveIntSet_impl;
 
 public class FSIndexRepositoryImpl implements FSIndexRepositoryMgr, LowLevelIndexRepository {
 
@@ -663,20 +662,28 @@ public class FSIndexRepositoryImpl imple
       // configured to continue with forward iterations
       this.wentForward = true;
       this.lastValidIndex = lvi;
-      // Go back until we find a FS that is really smaller
-      while (true) {
-        moveToPrevious();
-        if (isValid()) {
-          int prev = get();
-          if (this.iicp.index.compare(prev, fs) != 0) {
-            moveToNext();
-            break;
-          }
-        } else {
-          moveToFirst();
-          break;
-        }
-      }
+     
+      // moved to leaf iterator
+//      if (!isValid()) {
+//        // this means the moveTo found the insert point at the end of the index
+//        // so just return invalid, since there's no way to return an insert point for a position
+//        // that satisfies the FS at that position is greater than fs  
+//        return;
+//      }
+//      // Go back until we find a FS that is really smaller
+//      while (true) {
+//        moveToPrevious();
+//        if (isValid()) {
+//          int prev = get();
+//          if (this.iicp.index.compare(prev, fs) != 0) {
+//            moveToNext();
+//            break;
+//          }
+//        } else {
+//          moveToFirst();
+//          break;
+//        }
+//      }
     }
 
     /*

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntArrayIndex.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntArrayIndex.java?rev=1637457&r1=1637456&r2=1637457&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntArrayIndex.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntArrayIndex.java Fri Nov  7 21:43:12 2014
@@ -114,11 +114,36 @@ public class FSIntArrayIndex<T extends F
      */
     public void moveTo(int i) {
       final int position = find(i);
+      boolean found = false;
       if (position >= 0) {
         this.itPos = position;
-      } else {
+        found = true;
+      } else {  // not found
         this.itPos = -(position + 1);
       }
+      
+      // https://issues.apache.org/jira/browse/UIMA-4094
+      // make sure you go to earliest one
+      if (!found || !isValid()) {
+        // this means the moveTo found the insert point at the end of the index
+        // so just return invalid, since there's no way to return an insert point for a position
+        // that satisfies the FS at that position is greater than fs  
+        return;
+      }    
+      // Go back until we find a FS that is really smaller
+      while (true) {
+        moveToPrevious();
+        if (isValid()) {
+          int prev = get();
+          if (compare(prev, i) != 0) {
+            moveToNext(); // go back
+            break;
+          }
+        } else {
+          moveToFirst();  // went to before first, so go back to 1st
+          break;
+        }
+      }
     }
 
     /*

Modified: uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/IndexRepositoryTest.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/IndexRepositoryTest.java?rev=1637457&r1=1637456&r2=1637457&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/IndexRepositoryTest.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/IndexRepositoryTest.java Fri Nov  7 21:43:12 2014
@@ -145,25 +145,43 @@ public class IndexRepositoryTest extends
   
   public void testRemovalSpeedBagAlone() throws Exception {
     // create an instance of an non-annotation type
-    Type fsType = this.typeSystem.getType(CASTestSetup.TOKEN_TYPE_TYPE);
-    FeatureStructure[] fsa = new FeatureStructure[NBR_ITEMS];
-    // create 40000 token-types
-    for (int i = 0; i < fsa.length; i++) {
-      fsa[i] = this.cas.createFS(fsType);
-    }
-    
+   
+
+    for (int iii = 0; iii < 3 /*10000*/; iii++) { // change to 10000 for iterations
+      
+//      this.cas = CASInitializer.initCas(new CASTestSetup());
+//      this.typeSystem = this.cas.getTypeSystem();
+//      this.indexRep = this.cas.getIndexRepository();
+      
+      // create 40000 token-types
+      Type fsType = this.typeSystem.getType(CASTestSetup.TOKEN_TYPE_TYPE);
+//      Feature beginFeat = typeSystem.getFeatureByFullName("Token:begin");
+      FeatureStructure[] fsa = new FeatureStructure[NBR_ITEMS];
+      for (int i = 0; i < fsa.length; i++) {
+        fsa[i] = this.cas.createFS(fsType);
+//        fsa[i].setIntValue(beginFeat,  i);
+      }
     // warmup and jit
     timeAdd2Indexes(fsa);
     timeRemoveFromIndexes(fsa);
-    
+//    timeAdd2Indexes(fsa);
+//    timeRemoveFromIndexes(fsa);
+    System.gc();
     long a2i = timeAdd2Indexes(fsa);
+//    Thread.currentThread().sleep(1000*60*60);  // for using yourkit to investigate memory sizes
     long rfi = timeRemoveFromIndexes(fsa);
     
     long a2i2 = timeAdd2Indexes(fsa);
     long rfir = timeRemoveFromIndexesReverse(fsa);
     
-    System.out.format("Timing add/remv from bag indexes: add1: %,d msec, add2: %,d msec, rmv: %,d msec, rmvReversed: %,d msec%n", 
-        a2i, a2i2, rfi, rfir);
+//    if (iii == 600) {
+//      System.out.println("debug stop");
+//    }
+    if (iii < 10 || (iii % 200) == 0) {
+    System.out.format("%,d Timing add/remv from bag indexes: add1: %,d msec, add2: %,d msec, rmv: %,d msec, rmvReversed: %,d msec%n", 
+        iii, a2i, a2i2, rfi, rfir);
+    }
+    }
   }
 
   private long timeAdd2Indexes (FeatureStructure[] fsa) {

Modified: uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/IteratorTest.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/IteratorTest.java?rev=1637457&r1=1637456&r2=1637457&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/IteratorTest.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/IteratorTest.java Fri Nov  7 21:43:12 2014
@@ -204,6 +204,52 @@ public class IteratorTest extends TestCa
     it.moveToPrevious();
     assertTrue(index.compare(match, it.get()) > 0);
   }
+  
+  public void testMoveToPastEnd() {  // https://issues.apache.org/jira/browse/UIMA-4094
+    this.cas.getIndexRepository().addFS(
+        this.cas.createAnnotation(this.annotationType, 1,2));
+    
+    AnnotationFS pastEnd = this.cas.createAnnotation(this.annotationType,  2,  3);
+    FSIndex<AnnotationFS> index = this.cas.getAnnotationIndex();
+    FSIterator<AnnotationFS> it = index.iterator();
+    it.moveTo(pastEnd);
+    assertFalse(it.isValid());
+  }
+  
+  public void testMoveToFirstOfEqualOneType() {
+    for (int i = 0; i < 2; i++) {
+      cas.reset();
+      if (i == 0) {
+        this.cas.getIndexRepository().addFS(
+            this.cas.createAnnotation(this.subsentenceType, 0, 1));        
+      }
+      this.cas.getIndexRepository().addFS(
+          this.cas.createAnnotation(this.subsentenceType, 1,2));
+      this.cas.getIndexRepository().addFS(
+          this.cas.createAnnotation(this.subsentenceType, 1,2));
+      this.cas.getIndexRepository().addFS(
+          this.cas.createAnnotation(this.subsentenceType, 1,3));
+      this.cas.getIndexRepository().addFS(
+          this.cas.createAnnotation(this.subsentenceType, 2,2));
+      this.cas.getIndexRepository().addFS(
+          this.cas.createAnnotation(this.subsentenceType, 2, 5));
+      
+      AnnotationFS testAnnot = this.cas.createAnnotation(this.subsentenceType, 1, 2);
+      
+      FSIndex<AnnotationFS> index = this.cas.getAnnotationIndex(this.subsentenceType);
+      FSIterator<AnnotationFS> it = index.iterator();
+      it.moveTo(testAnnot);
+      for (int j = 0; j < 2; j++) {
+        assertTrue(it.isValid());
+        AnnotationFS fs = it.get();
+        assertEquals(1, fs.getBegin()); 
+        assertEquals(2, fs.getEnd());
+        it.moveToNext();
+      }
+      assertTrue(it.isValid());
+    }
+  }
+  
 
   private void createFSs(int i) {
     this.cas.getIndexRepository().addFS(