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

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Fri Sep 19 05:38:13 EDT 2008


Author: navssurtani
Date: 2008-09-19 05:38:13 -0400 (Fri, 19 Sep 2008)
New Revision: 6755

Modified:
   searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java
   searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java
   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/CacheQuery.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java	2008-09-18 17:18:38 UTC (rev 6754)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java	2008-09-19 09:38:13 UTC (rev 6755)
@@ -56,14 +56,24 @@
    QueryResultIterator iterator();
 
    /**
-    * Lazily loads the results from the Query as a {@link org.jboss.cache.search.QueryResultIterator}
+    * Lazily loads the results from the Query as a {@link org.jboss.cache.search.QueryResultIterator} with a given
+    * integer parameter - the fetchSize.
     *
+    * @param fetchSize integer to be passed into the lazy implementation of {@link org.jboss.cache.search.QueryResultIterator}
     * @return a QueryResultIterator which can be used to <B>lazily</B> iterate through results.
     */
 
+   QueryResultIterator lazyIterator(int fetchSize);
+
+   /**
+    * Calls the {@link CacheQuery#lazyIterator(int fetchSize)} method but passes in a default 1 as a parameter.
+    *
+    * @return a QueryResultIterator which can be used to <B>lazily</B> iterate through results.
+    */
+
+
    QueryResultIterator lazyIterator();
 
-
    /**
     * Sets a result with a given index to the first result.
     *

Modified: searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java	2008-09-18 17:18:38 UTC (rev 6754)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java	2008-09-19 09:38:13 UTC (rev 6755)
@@ -248,6 +248,11 @@
 
    public QueryResultIterator lazyIterator()
    {
+      return lazyIterator(1);
+   }
+
+   public QueryResultIterator lazyIterator(int fetchSize)
+   {
       IndexSearcher searcher = buildSearcher(searchFactory);
 
       try
@@ -258,7 +263,7 @@
 
          DocumentExtractor extractor = new DocumentExtractor(luceneQuery, searcher, searchFactory, indexProjection);
 
-         return new LazyQueryResultIterator(extractor, entityLoader, hits, searcher, searchFactory, first, max);
+         return new LazyQueryResultIterator(extractor, entityLoader, hits, searcher, searchFactory, first, max, fetchSize);
       }
       catch (IOException e)
       {
@@ -271,14 +276,9 @@
          throw new HibernateException( "Unable to query Lucene index", e );
 
       }
+
    }
 
-   /**
-    * Returns a the results from the query as a List object.
-    *
-    * @return List of results.
-    * @throws HibernateException
-    */
    public List<Object> list() throws HibernateException
    {
       IndexSearcher searcher = buildSearcher(searchFactory);

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-18 17:18:38 UTC (rev 6754)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/LazyQueryResultIterator.java	2008-09-19 09:38:13 UTC (rev 6755)
@@ -18,7 +18,7 @@
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */ 
+ */
 
 package org.jboss.cache.search;
 
@@ -26,8 +26,11 @@
 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.io.IOException;
 
 /**
@@ -40,25 +43,37 @@
 public class LazyQueryResultIterator implements QueryResultIterator
 {
    private int index = 0;
+   private int oldIndex;
    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.
    private CacheEntityLoader entityLoader;
    private DocumentExtractor extractor;
    private Hits hits;
    private IndexSearcher searcher;
    private SearchFactoryImplementor searchFactory;
+   private List<Object> bufferObjects;
 
    public LazyQueryResultIterator(DocumentExtractor extractor, CacheEntityLoader entityLoader, Hits hits,
-                                  IndexSearcher searcher, SearchFactoryImplementor searchFactory, int first, int max)
+                                  IndexSearcher searcher, SearchFactoryImplementor searchFactory, int first, int max, int fetchSize)
    {
+      if (fetchSize < 1)
+      {
+         throw new IllegalArgumentException("Incorrect value for fetchsize passed. Your fetchSize is less than 1");
+      }
+
       this.extractor = extractor;
       this.entityLoader = entityLoader;
       index = first;
       this.first = first;
       this.max = max;
+      this.fetchSize = fetchSize + first;
       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);
    }
 
    public void jumpToResult(int index) throws IndexOutOfBoundsException
@@ -125,11 +140,35 @@
    public Object next()
    {
       Object toReturn = null;
+      int bufferSize = fetchSize - 1;
+
+      if (index <= oldIndex + bufferSize)
+      {
+         oldIndex = index;
+         index++;
+         return bufferObjects.get(oldIndex);         
+      }
+
       try
       {
          String documentId = (String) extractor.extract(hits, index).id;
          CacheEntityId id = new CacheEntityId(documentId);
          toReturn = entityLoader.load(id);
+
+         //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 the oldIndex so that there is no stale data.
+            bufferObjects.clear();
+            oldIndex = 0;
+
+            String bufferDocumentId = (String) extractor.extract(hits, index + i).id;
+            CacheEntityId bufferId = new CacheEntityId(bufferDocumentId);
+            Object toBuffer = entityLoader.load(bufferId);
+            bufferObjects.add(toBuffer);
+
+         }
+         oldIndex = index;
          index++;
       }
       catch (IOException e)
@@ -147,12 +186,37 @@
 
    public Object previous()
    {
+      int bufferSize = fetchSize - 1;
       Object toReturn = null;
+
+      if (index >= oldIndex - bufferSize)
+      {
+         oldIndex = index;
+         index--;
+         return bufferObjects.get(oldIndex);
+      }
+
       try
       {
-         String documentId = (String) extractor.extract (hits, index).id;
+         String documentId = (String) extractor.extract(hits, index).id;
          CacheEntityId id = new CacheEntityId(documentId);
          toReturn = entityLoader.load(id);
+
+         //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);
+
+         }
+
+         oldIndex = index;
          index--;
       }
       catch (IOException e)
@@ -204,4 +268,5 @@
    {
       throw new UnsupportedOperationException("Not supported as you are trying to change something in the cache");
    }
+
 }




More information about the jbosscache-commits mailing list