]> source.dussan.org Git - jackcess.git/commitdiff
IndexCursor can early exit when searching based on indexed values, fixes 109
authorJames Ahlborn <jtahlborn@yahoo.com>
Sun, 21 Sep 2014 03:24:42 +0000 (03:24 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Sun, 21 Sep 2014 03:24:42 +0000 (03:24 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@881 f203690c-595d-4dc9-a70b-905162fa7fd2

src/changes/changes.xml
src/main/java/com/healthmarketscience/jackcess/impl/CursorImpl.java
src/main/java/com/healthmarketscience/jackcess/impl/IndexCursorImpl.java

index dba094afec6507971ea409f7753c9eeb18ca549f..affaa5c1d0f2fa6abf76dd6999375639bb120e77 100644 (file)
@@ -4,6 +4,11 @@
     <author email="javajedi@users.sf.net">Tim McCune</author>
   </properties>
   <body>
+    <release version="2.0.6" date="TBD">
+      <action dev="jahlborn" type="fix" system="SourceForge2" issue="109">
+        IndexCursor can early exit when searching based on indexed values.
+      </action>
+    </release>
     <release version="2.0.5" date="2014-09-17">
       <action dev="jahlborn" type="add">
         Add Cursor.findRow(RowId) for moving to a specific Table row using
index 1521e3b4c781ce7bd9f1ef59e86bd8e18e851afa..89b188364d56166403ffdee61d12679582adc99a 100644 (file)
@@ -315,7 +315,7 @@ public abstract class CursorImpl implements Cursor
    *         {@code null} if there is not another row in the given direction.
    */
   private Row getAnotherRow(Collection<String> columnNames,
-                                            boolean moveForward) 
+                            boolean moveForward) 
     throws IOException
   {
     if(moveToAnotherRow(moveForward)) {
@@ -485,7 +485,7 @@ public abstract class CursorImpl implements Cursor
         reset(moveForward);
       }
       found = findAnotherRowImpl(columnPattern, valuePattern, moveForward,
-                                 columnMatcher);
+                                 columnMatcher, null);
       return found;
     } finally {
       if(!found) {
@@ -521,7 +521,7 @@ public abstract class CursorImpl implements Cursor
       if(reset) {
         reset(moveForward);
       }
-      found = findAnotherRowImpl(rowPattern, moveForward, columnMatcher);
+      found = findAnotherRowImpl(rowPattern, moveForward, columnMatcher, null);
       return found;
     } finally {
       if(!found) {
@@ -598,13 +598,16 @@ public abstract class CursorImpl implements Cursor
    */
   protected boolean findAnotherRowImpl(
       ColumnImpl columnPattern, Object valuePattern, boolean moveForward,
-      ColumnMatcher columnMatcher)
+      ColumnMatcher columnMatcher, Object searchInfo)
     throws IOException
   {
     while(moveToAnotherRow(moveForward)) {
       if(currentRowMatchesImpl(columnPattern, valuePattern, columnMatcher)) {
         return true;
       }
+      if(!keepSearching(columnMatcher, searchInfo)) {
+        break;
+      }
     }
     return false;
   }
@@ -622,17 +625,32 @@ public abstract class CursorImpl implements Cursor
    */
   protected boolean findAnotherRowImpl(Map<String,?> rowPattern, 
                                        boolean moveForward, 
-                                       ColumnMatcher columnMatcher)
+                                       ColumnMatcher columnMatcher,
+                                       Object searchInfo)
     throws IOException
   {
     while(moveToAnotherRow(moveForward)) {
       if(currentRowMatchesImpl(rowPattern, columnMatcher)) {
         return true;
       }
+      if(!keepSearching(columnMatcher, searchInfo)) {
+        break;
+      }
     }
     return false;
   }  
 
+  /**
+   * Called by findAnotherRowImpl to determine if the search should continue
+   * after finding a row which does not match the current pattern.
+   */
+  protected boolean keepSearching(ColumnMatcher columnMatcher, 
+                                  Object searchInfo) 
+    throws IOException
+  {
+    return true;
+  }
+
   public int moveNextRows(int numRows) throws IOException
   {
     return moveSomeRows(numRows, MOVE_FORWARD);
index 68870d9b3483c45de837bb9c6788308804b448a3..4b8830a884246d5ec29959bcf0a55bcfd69d02a3 100644 (file)
@@ -246,23 +246,18 @@ public class IndexCursorImpl extends CursorImpl implements IndexCursor
   @Override
   protected boolean findAnotherRowImpl(
       ColumnImpl columnPattern, Object valuePattern, boolean moveForward,
-      ColumnMatcher columnMatcher)
+      ColumnMatcher columnMatcher, Object searchInfo)
     throws IOException
   {
-    if(!isAtBeginning(moveForward)) {
-      // use the default table scan for finding rows mid-cursor
-      return super.findAnotherRowImpl(columnPattern, valuePattern, moveForward,
-                                      columnMatcher);
-    }
-
     // searching for the first match
     Object[] rowValues = _entryCursor.getIndexData().constructIndexRow(
         columnPattern.getName(), valuePattern);
 
-    if(rowValues == null) {
-      // bummer, use the default table scan
+    if((rowValues == null) || !isAtBeginning(moveForward)) {
+      // use the default table scan if we don't have index data or we are
+      // mid-cursor
       return super.findAnotherRowImpl(columnPattern, valuePattern, moveForward,
-                                      columnMatcher);
+                                      columnMatcher, rowValues);
     }
       
     // sweet, we can use our index
@@ -300,23 +295,20 @@ public class IndexCursorImpl extends CursorImpl implements IndexCursor
   }
 
   @Override
-  protected boolean findAnotherRowImpl(Map<String,?> rowPattern, 
-                                       boolean moveForward,
-                                       ColumnMatcher columnMatcher)
+  protected boolean findAnotherRowImpl(
+      Map<String,?> rowPattern, boolean moveForward,
+      ColumnMatcher columnMatcher, Object searchInfo)
     throws IOException
   {
-    if(!isAtBeginning(moveForward)) {
-      // use the default table scan for finding rows mid-cursor
-      return super.findAnotherRowImpl(rowPattern, moveForward, columnMatcher);
-    }
-
     // searching for the first match
     IndexData indexData = _entryCursor.getIndexData();
     Object[] rowValues = indexData.constructIndexRow(rowPattern);
 
-    if(rowValues == null) {
-      // bummer, use the default table scan
-      return super.findAnotherRowImpl(rowPattern, moveForward, columnMatcher);
+    if((rowValues == null) || !isAtBeginning(moveForward)) {
+      // use the default table scan if we don't have index data or we are
+      // mid-cursor
+      return super.findAnotherRowImpl(rowPattern, moveForward, columnMatcher,
+                                      rowValues);
     }
 
     // sweet, we can use our index
@@ -398,6 +390,17 @@ public class IndexCursorImpl extends CursorImpl implements IndexCursor
     return true;
   }
 
+  @Override
+  protected boolean keepSearching(ColumnMatcher columnMatcher, 
+                                  Object searchInfo) 
+    throws IOException
+  {
+    if(searchInfo instanceof Object[]) {
+      return currentRowMatchesEntryImpl((Object[])searchInfo, columnMatcher);
+    }
+    return true;
+  }
+
   private Object[] toRowValues(Object[] entryValues)
   {
     return _entryCursor.getIndexData().constructIndexRowFromEntry(entryValues);