[jbosscache-commits] JBoss Cache SVN: r6763 - searchable/trunk/src/main/java/org/jboss/cache/search.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Fri Sep 19 12:55:20 EDT 2008


Author: navssurtani
Date: 2008-09-19 12:55:20 -0400 (Fri, 19 Sep 2008)
New Revision: 6763

Modified:
   searchable/trunk/src/main/java/org/jboss/cache/search/LazyQueryResultIterator.java
Log:
Implemented fetchSize changes in CQI and used the changes in LQRI	


Modified: searchable/trunk/src/main/java/org/jboss/cache/search/LazyQueryResultIterator.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/LazyQueryResultIterator.java	2008-09-19 14:41:57 UTC (rev 6762)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/LazyQueryResultIterator.java	2008-09-19 16:55:20 UTC (rev 6763)
@@ -26,11 +26,9 @@
 import org.hibernate.search.engine.SearchFactoryImplementor;
 import org.apache.lucene.search.Hits;
 import org.apache.lucene.search.IndexSearcher;
-import org.jboss.util.Objects;
 
 import java.util.NoSuchElementException;
-import java.util.List;
-import java.util.ArrayList;
+import java.util.Arrays;
 import java.io.IOException;
 
 /**
@@ -43,7 +41,7 @@
 public class LazyQueryResultIterator implements QueryResultIterator
 {
    private int index = 0;
-   private int oldIndex;
+   private int bufferIndex = -1; // this is the index at which the buffer was populated.  Hence, the first element of the buffer is at this index in the overall result set.
    private int max = 0;
    private int first = 0;
    private int fetchSize = 0;     // difference between fetchsize and index must always be the value for first. fetchSize has to be at least 1.
@@ -52,7 +50,7 @@
    private Hits hits;
    private IndexSearcher searcher;
    private SearchFactoryImplementor searchFactory;
-   private List<Object> bufferObjects;
+   private Object[] buffer;
 
    public LazyQueryResultIterator(DocumentExtractor extractor, CacheEntityLoader entityLoader, Hits hits,
                                   IndexSearcher searcher, SearchFactoryImplementor searchFactory, int first, int max, int fetchSize)
@@ -67,13 +65,13 @@
       index = first;
       this.first = first;
       this.max = max;
-      this.fetchSize = fetchSize + first;
+      this.fetchSize = fetchSize;
       this.hits = hits;
       this.searcher = searcher;
       this.searchFactory = searchFactory;
 
-      //Create an ArrayList with size fetchSize - 1 (which is the size of the required buffer.
-      bufferObjects = new ArrayList<Object>(this.fetchSize - 1);
+      //Create an buffer with size fetchSize (which is the size of the required buffer.
+      buffer = new Object[fetchSize];
    }
 
    public void jumpToResult(int index) throws IndexOutOfBoundsException
@@ -82,7 +80,7 @@
       {
          throw new IndexOutOfBoundsException("The given index is incorrect. Please check and try again.");
       }
-
+      
       this.index = first + index;
 
    }
@@ -140,42 +138,51 @@
    public Object next()
    {
       Object toReturn = null;
-      int bufferSize = fetchSize - 1;
+      int bufferSize = buffer.length;
 
-      if (index <= oldIndex + bufferSize)
+      // make sure the index we are after is in the buffer.  If it is, then index >= bufferIndex and index <= (bufferIndex + bufferSize).
+      if (bufferIndex >= 0                                       // buffer init check
+              && index >= bufferIndex                           // lower boundary
+              && index <= (bufferIndex + bufferSize))          // upper boundary
       {
-         oldIndex = index;
-         index++;
-         return bufferObjects.get(oldIndex);         
+         // now we can get this from the buffer.  Sweet!
+         int indexToReturn = index - bufferIndex;
+         toReturn = buffer[indexToReturn];
       }
-
-      try
+      else
       {
-         String documentId = (String) extractor.extract(hits, index).id;
-         CacheEntityId id = new CacheEntityId(documentId);
-         toReturn = entityLoader.load(id);
+         // else we need to populate the buffer and get what we need.
 
-         //now loop through bufferSize times to add the rest of the objects into the list.
-         for (int i = 1; i <= bufferSize; i++)
+         try
          {
-            //Wiping bufferObjects and the oldIndex so that there is no stale data.
-            bufferObjects.clear();
-            oldIndex = 0;
+            String documentId = (String) extractor.extract(hits, index).id;
+            CacheEntityId id = new CacheEntityId(documentId);
+            toReturn = entityLoader.load(id);
 
-            String bufferDocumentId = (String) extractor.extract(hits, index + i).id;
-            CacheEntityId bufferId = new CacheEntityId(bufferDocumentId);
-            Object toBuffer = entityLoader.load(bufferId);
-            bufferObjects.add(toBuffer);
+            //Wiping bufferObjects and the bufferIndex so that there is no stale data.
+            Arrays.fill(buffer, null);
+            buffer[0] = toReturn;
 
+            // we now need to buffer item at index "index", as well as the next "fetchsize - 1" elements.  I.e., a total of fetchsize elements will be buffered.
+            // ignore loop below, in needs fixing
+            //now loop through bufferSize times to add the rest of the objects into the list.
+
+            for (int i = 1; i <= bufferSize; i++)
+            {
+               String bufferDocumentId = (String) extractor.extract(hits, index + i).id;
+               CacheEntityId bufferId = new CacheEntityId(bufferDocumentId);
+               Object toBuffer = entityLoader.load(bufferId);
+               buffer[i] = toBuffer;
+            }
+            bufferIndex = index;
          }
-         oldIndex = index;
-         index++;
+         catch (IOException e)
+         {
+            e.printStackTrace();
+         }
       }
-      catch (IOException e)
-      {
-         e.printStackTrace();
-      }
 
+      index++;
       return toReturn;
    }
 
@@ -186,43 +193,47 @@
 
    public Object previous()
    {
-      int bufferSize = fetchSize - 1;
       Object toReturn = null;
+      int bufferSize = buffer.length;
 
-      if (index >= oldIndex - bufferSize)
+      // make sure the index we are after is in the buffer.  If it is, then index >= bufferIndex and index <= (bufferIndex + bufferSize).
+
+      if (bufferIndex >= 0 // buffer init check
+              && index <= bufferIndex // lower boundary
+              && index >= (bufferIndex + bufferSize)) // upper boundary
       {
-         oldIndex = index;
-         index--;
-         return bufferObjects.get(oldIndex);
+         // now we can get this from the buffer.  Sweet!
+         int indexToReturn = bufferIndex - index;        // Unlike next() we have to make sure that we are subtracting index from bufferIndex
+         toReturn = buffer[indexToReturn];
       }
 
       try
       {
+         //Wiping the buffer
+         Arrays.fill(buffer, null);
+
          String documentId = (String) extractor.extract(hits, index).id;
          CacheEntityId id = new CacheEntityId(documentId);
          toReturn = entityLoader.load(id);
 
+         buffer[0] = toReturn;
+
          //now loop through bufferSize times to add the rest of the objects into the list.
          for (int i = 1; i <= bufferSize; i++)
          {
-            //Wiping bufferObjects and oldIndex so that there is no stale data.
-            bufferObjects.clear();
-            oldIndex = 0;
-
             String bufferDocumentId = (String) extractor.extract(hits, index - i).id;    //In this case it has to be index - i because previous() is called.
             CacheEntityId bufferId = new CacheEntityId(bufferDocumentId);
             Object toBuffer = entityLoader.load(bufferId);
-            bufferObjects.add(toBuffer);
-
+            buffer[i] = toBuffer;
          }
 
-         oldIndex = index;
-         index--;
+         bufferIndex = index;
       }
       catch (IOException e)
       {
          e.printStackTrace();
       }
+      index--;
       return toReturn;
    }
 




More information about the jbosscache-commits mailing list