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 2015/02/13 22:45:00 UTC

svn commit: r1659696 - in /uima/uimaj/trunk/uimaj-core/src: main/java/org/apache/uima/cas/impl/FSIndexRepositoryImpl.java test/java/org/apache/uima/cas/test/IteratorTest.java

Author: schor
Date: Fri Feb 13 21:44:59 2015
New Revision: 1659696

URL: http://svn.apache.org/r1659696
Log:
[UIMA-4248] fix snapshot iterators for index sizes < 16

Modified:
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIndexRepositoryImpl.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/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=1659696&r1=1659695&r2=1659696&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 Feb 13 21:44:59 2015
@@ -235,7 +235,12 @@ public class FSIndexRepositoryImpl imple
   IntPointerIterator createPointerIterator(IndexIteratorCachePair iicp, boolean is_unordered) {
     iicp.createIndexIteratorCache();
     if (iicp.iteratorCache.size() > 1) {
-      if (iicp.index.getIndexingStrategy() == FSIndex.BAG_INDEX || is_unordered) {
+      final int strat = iicp.index.getIndexingStrategy();
+      if (strat == FSIndex.BAG_INDEX || 
+          /*
+          strat == FSIndex.SET_INDEX ||  // because set indexes do not enforce ordering
+          */  // not added for 2.7.0 release, may break existing user code
+          is_unordered) {
         return new PointerIteratorUnordered(iicp);
       } else {
         return new PointerIterator(iicp);
@@ -885,6 +890,10 @@ public class FSIndexRepositoryImpl imple
 
     /* (non-Javadoc)
      * @see org.apache.uima.cas.impl.FSIndexRepositoryImpl.PointerIterator#moveTo(int)
+     * 
+     * NOTE: This logic is for the "Unordered" iterator case
+     * For unordered iterating, it doesn't make sense to find one not equal, any will do...
+     * but if one is exactly equal, we return that (backwards compatibility)
      */
     @Override
     public void moveTo(int fs) {
@@ -1052,6 +1061,7 @@ public class FSIndexRepositoryImpl imple
 
     final private FSIntArrayIndex sortedLeafIndex;
     final private int[] snapshot;
+    final private int size;   // can't get from snapshot.length - that might have extra space  https://issues.apache.org/jira/browse/UIMA-4248
     private int pos = 0;
       
     @Override
@@ -1067,9 +1077,10 @@ public class FSIndexRepositoryImpl imple
       FSLeafIndexImpl leafIndex = iicp0.index;
       FSIndexComparator comp = leafIndex.getComparator();
       
-      final int size = iicp0.size();
+      final int size = iicp0.size();  // adds up all the sizes of the indexes
       sortedLeafIndex = (FSIntArrayIndex) addNewIndexCore(comp, size, FSIndex.SORTED_INDEX);
       snapshot = sortedLeafIndex.getVector().getArray();
+      this.size = size;
       flattenCopy(iicp0, isRootOnly);
       sortedLeafIndex.getVector().setSize(size);
       moveToFirst();      
@@ -1102,11 +1113,11 @@ public class FSIndexRepositoryImpl imple
     }
        
     public boolean isValid() {
-      return (0 <= pos) && (pos < snapshot.length);
+      return (0 <= pos) && (pos < size);
     }
     
     public void moveToLast() {
-      pos = snapshot.length - 1;
+      pos = size - 1;
     }
 
     public void moveToFirst() {
@@ -1139,7 +1150,7 @@ public class FSIndexRepositoryImpl imple
       if (sortedLeafIndex.getComparator().getNumberOfKeys() == 0) {
         // use identity, search from beginning to get "left-most"
         int i = 0;
-        for (; i < snapshot.length; i++) {
+        for (; i < size; i++) {
           if (fs == snapshot[i]) {
             break;
           }
@@ -1174,7 +1185,7 @@ public class FSIndexRepositoryImpl imple
     }
     
     public int ll_indexSize() {
-      return snapshot.length;
+      return size;
     }
     
     public Object copy() {
@@ -2420,7 +2431,7 @@ public class FSIndexRepositoryImpl imple
    * This is used to see if a FS which has a key feature being modified
    * could corrupt an index in this view.  It returns true if found 
    * (sometimes it returns true, even if strictly speaking, there is 
-   * no chance of corruption - seel below)
+   * no chance of corruption - see below)
    * 
    * It does this by seeing if this FS is indexed by one or more Set or Sorted
    * indexes.

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=1659696&r1=1659695&r2=1659696&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 Feb 13 21:44:59 2015
@@ -25,6 +25,7 @@ import java.util.ArrayList;
 import java.util.ConcurrentModificationException;
 import java.util.Iterator;
 import java.util.List;
+import java.util.NoSuchElementException;
 
 import junit.framework.TestCase;
 
@@ -223,6 +224,29 @@ public class IteratorTest extends TestCa
 
   }
   
+  public void testEmptySnapshotIterator() {
+    setupindexes();
+    FSIterator<FeatureStructure> it = sortedIndex.iterator();
+    assertFalse(it.isValid());
+    boolean ok = false;
+    try {
+      it.get();
+    } catch (NoSuchElementException e) {
+      ok = true;
+    }
+    assertTrue(ok);
+    
+    it = ssSortedIndex.iterator();
+    assertFalse(it.isValid());
+    ok = false;
+    try {
+      it.get();
+    } catch (NoSuchElementException e) {
+      ok = true;
+    }
+    assertTrue(ok);    
+  }
+  
   public void testGetIndexes() {
     Iterator<FSIndex<FeatureStructure>> it = this.cas.getIndexRepository().getIndexes();
     while (it.hasNext()) {
@@ -359,8 +383,10 @@ public class IteratorTest extends TestCa
     
     System.out.println("test multicore iterator with " + numberOfCores + " threads");
     Thread[] threads = new Thread[numberOfCores];
-    final Throwable[] tthrowable = new Throwable[1];
+    
+    final Throwable[] tthrowable = new Throwable[1];  // trick to get a return value in a parameter
     tthrowable[0] = null;
+    
     for (int r = 0; r < 10; r++) {
       for (int i = 0; i < numberOfCores; i++) {
         final int finalI = i;
@@ -460,8 +486,13 @@ public class IteratorTest extends TestCa
     while (it.isValid()) {
       a = (AnnotationFS) it.get();
       if (b != null) {
-        // note compare may be equal for two items of different types 
-        assertTrue(setIndex.compare(b, a) <= 0);
+        // note compare may be equal for two items of different types
+        // because setIndex is not using type priorities (I think) (2/2015)
+        int compareResult = setIndex.compare(b, a);
+        if (compareResult == 0) {
+          // types must be different
+          assertFalse(a.getType().getName().equals(b.getType().getName()));
+        }
         if (a.hashCode() == b.hashCode()) {
           System.err.format("set Iterator: should not have 2 identical elements%n%s%n", it);
           assertTrue(false);