Author: navssurtani
Date: 2008-08-07 08:04:30 -0400 (Thu, 07 Aug 2008)
New Revision: 6537
Added:
searchable/trunk/src/main/java/org/jboss/cache/search/IndexSearcherCloser.java
searchable/trunk/src/main/java/org/jboss/cache/search/LazyQueryResultIterator.java
Modified:
searchable/trunk/TODO.txt
searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIterator.java
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIteratorImpl.java
Log:
Wrote up lazyIterator() in CacheQueryImpl. Still needs to be tested
Modified: searchable/trunk/TODO.txt
===================================================================
--- searchable/trunk/TODO.txt 2008-08-07 04:21:36 UTC (rev 6536)
+++ searchable/trunk/TODO.txt 2008-08-07 12:04:30 UTC (rev 6537)
@@ -10,4 +10,6 @@
Think.
-5 - Mock objects.
\ No newline at end of file
+5 - Mock objects.
+
+6 - Think about removing the Hibernate Exceptions.
\ No newline at end of file
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java 2008-08-07
04:21:36 UTC (rev 6536)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java 2008-08-07
12:04:30 UTC (rev 6537)
@@ -14,7 +14,6 @@
import org.hibernate.search.engine.SearchFactoryImplementor;
import org.hibernate.search.filter.ChainedFilter;
import org.hibernate.search.filter.FilterKey;
-import org.hibernate.search.impl.SearchFactoryImpl;
import org.hibernate.search.query.FullTextFilterImpl;
import org.hibernate.search.reader.ReaderProvider;
import static org.hibernate.search.reader.ReaderProviderHelper.getIndexReaders;
@@ -114,7 +113,7 @@
//searcher cannot be null
try
{
- closeSearcher(searcher, searchFactory.getReaderProvider());
+ IndexSearcherCloser.closeSearcher(searcher,
searchFactory.getReaderProvider());
//searchFactoryImplementor.getReaderProvider().closeReader(
searcher.getIndexReader() );
}
catch (SearchException e)
@@ -133,16 +132,6 @@
}
- private void closeSearcher(Searcher searcher, ReaderProvider readerProvider)
- {
- Set<IndexReader> indexReaders = getIndexReaders(searcher);
-
- for (IndexReader indexReader : indexReaders)
- {
- readerProvider.closeReader(indexReader);
- }
- }
-
/**
* Enable a given filter by its name.
*
@@ -232,7 +221,7 @@
finally
{
- closeSearcher(searcher, searchFactory.getReaderProvider());
+ IndexSearcherCloser.closeSearcher(searcher, searchFactory.getReaderProvider());
}
@@ -250,14 +239,20 @@
int max = max(first, hits);
DocumentExtractor extractor = new DocumentExtractor(luceneQuery, searcher,
searchFactory, indexProjection);
-
+
+ return new LazyQueryResultIterator(extractor, entityLoader, hits, searcher,
searchFactory, first, max);
}
catch (IOException e)
{
- e.printStackTrace(); //To change body of catch statement use File | Settings |
File Templates.
- }
+ try {
+ IndexSearcherCloser.closeSearcher( searcher,
searchFactory.getReaderProvider() );
+ }
+ catch (SearchException ee) {
+ //we have the initial issue already
+ }
+ throw new HibernateException( "Unable to query Lucene index", e );
- return null;
+ }
}
/**
@@ -312,7 +307,7 @@
}
finally
{
- closeSearcher(searcher, searchFactory.getReaderProvider());
+ IndexSearcherCloser.closeSearcher(searcher, searchFactory.getReaderProvider());
}
Added: searchable/trunk/src/main/java/org/jboss/cache/search/IndexSearcherCloser.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/IndexSearcherCloser.java
(rev 0)
+++
searchable/trunk/src/main/java/org/jboss/cache/search/IndexSearcherCloser.java 2008-08-07
12:04:30 UTC (rev 6537)
@@ -0,0 +1,25 @@
+package org.jboss.cache.search;
+
+import org.hibernate.search.engine.SearchFactoryImplementor;
+import org.hibernate.search.reader.ReaderProvider;
+import static org.hibernate.search.reader.ReaderProviderHelper.getIndexReaders;
+import org.apache.lucene.search.Searcher;
+import org.apache.lucene.index.IndexReader;
+
+import java.util.Set;
+
+/**
+ * @author Navin Surtani (<a
href="mailto:nsurtani@redhat.com">nsurtani@redhat.com</a>)
+ */
+public class IndexSearcherCloser
+{
+ static void closeSearcher(Searcher searcher, ReaderProvider readerProvider)
+ {
+ Set<IndexReader> indexReaders = getIndexReaders(searcher);
+
+ for (IndexReader indexReader : indexReaders)
+ {
+ readerProvider.closeReader(indexReader);
+ }
+ }
+}
Added: searchable/trunk/src/main/java/org/jboss/cache/search/LazyQueryResultIterator.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/LazyQueryResultIterator.java
(rev 0)
+++
searchable/trunk/src/main/java/org/jboss/cache/search/LazyQueryResultIterator.java 2008-08-07
12:04:30 UTC (rev 6537)
@@ -0,0 +1,182 @@
+package org.jboss.cache.search;
+
+import org.jboss.cache.search.CacheEntityLoader;
+import org.hibernate.search.engine.DocumentExtractor;
+import org.hibernate.search.engine.SearchFactoryImplementor;
+import org.apache.lucene.search.Hits;
+import org.apache.lucene.search.IndexSearcher;
+
+import java.util.NoSuchElementException;
+import java.io.IOException;
+
+/**
+ * @author Navin Surtani (<a
href="mailto:nsurtani@redhat.com">nsurtani@redhat.com</a>)
+ */
+public class LazyQueryResultIterator implements QueryResultIterator
+{
+ private int index = 0;
+ private int max = 0;
+ private int first = 0;
+ private CacheEntityLoader entityLoader;
+ private DocumentExtractor extractor;
+ private Hits hits;
+ private IndexSearcher searcher;
+ private SearchFactoryImplementor searchFactory;
+
+ public LazyQueryResultIterator(DocumentExtractor extractor, CacheEntityLoader
entityLoader, Hits hits,
+ IndexSearcher searcher, SearchFactoryImplementor
searchFactory, int first, int max)
+ {
+ this.extractor = extractor;
+ this.entityLoader = entityLoader;
+ index = first;
+ this.first = first;
+ this.max = max;
+ this.hits = hits;
+ this.searcher = searcher;
+ this.searchFactory = searchFactory;
+ }
+
+ public void jumpToResult(int index) throws IndexOutOfBoundsException
+ {
+ if (index < first || index > max)
+ {
+ throw new IndexOutOfBoundsException("The given index is incorrect. Please
check and try again.");
+ }
+
+ this.index = first + index;
+
+ }
+
+ public void first()
+ {
+ index = first;
+ }
+
+ public void last()
+ {
+ index = max;
+ }
+
+ public void afterFirst()
+ {
+ index = first + 1;
+ }
+
+ public void beforeLast()
+ {
+ index = max - 1;
+ }
+
+ public boolean isFirst()
+ {
+ return index == first;
+ }
+
+ public boolean isLast()
+ {
+ return index == max;
+ }
+
+ public boolean isAfterFirst()
+ {
+ return index == first + 1;
+ }
+
+ public boolean isBeforeLast()
+ {
+ return index == max - 1;
+ }
+
+ public void close()
+ {
+ IndexSearcherCloser.closeSearcher(searcher, searchFactory.getReaderProvider());
+ }
+
+ public boolean hasNext()
+ {
+ return index <= max;
+ }
+
+ public Object next()
+ {
+ Object toReturn = null;
+ try
+ {
+ String documentId = (String) extractor.extract(hits, index).id;
+ CacheEntityId id = new CacheEntityId(documentId);
+ toReturn = entityLoader.load(id);
+ index++;
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+
+ return toReturn;
+ }
+
+ public boolean hasPrevious()
+ {
+ return index >= first;
+ }
+
+ public Object previous()
+ {
+ Object toReturn = null;
+ try
+ {
+ String documentId = (String) extractor.extract (hits, index).id;
+ CacheEntityId id = new CacheEntityId(documentId);
+ toReturn = entityLoader.load(id);
+ index--;
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ return toReturn;
+ }
+
+ public int nextIndex()
+ {
+ if (!hasNext()) throw new NoSuchElementException("Out of boundaries");
+ return index + 1;
+ }
+
+ public int previousIndex()
+ {
+ if (!hasPrevious()) throw new NoSuchElementException("Out of
boundaries.");
+ return index - 1;
+ }
+
+ /**
+ * This method is not supported and should not be used. Use cache.remove() instead.
+ */
+ public void remove()
+ {
+ throw new UnsupportedOperationException("Not supported as you are trying to
change something in the cache");
+ }
+
+ /**
+ * This method is not supported in and should not be called. Use cache.put() instead.
+ *
+ * @param o
+ * @throws UnsupportedOperationException
+ */
+ public void set(Object o) throws UnsupportedOperationException
+ {
+ throw new UnsupportedOperationException("Not supported as you are trying to
change something in the cache");
+ }
+
+ /**
+ * This method is not supported in and should not be called. Use cache.put() instead.
+ *
+ * @param o
+ * @throws UnsupportedOperationException
+ */
+
+ public void add(Object o)
+ {
+ throw new UnsupportedOperationException("Not supported as you are trying to
change something in the cache");
+ }
+}
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIterator.java
===================================================================
---
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIterator.java 2008-08-07
04:21:36 UTC (rev 6536)
+++
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIterator.java 2008-08-07
12:04:30 UTC (rev 6537)
@@ -56,4 +56,10 @@
* @return true if the current result is one before the last
*/
boolean isBeforeLast();
+
+ /**
+ * This method must be called on your iterator once you have finished so that Lucene
resources can be freed up.
+ */
+
+ void close();
}
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIteratorImpl.java
===================================================================
---
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIteratorImpl.java 2008-08-07
04:21:36 UTC (rev 6536)
+++
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIteratorImpl.java 2008-08-07
12:04:30 UTC (rev 6537)
@@ -116,6 +116,12 @@
return idList.get(index) == idList.get(idList.size() - 2);
}
+ public void close()
+ {
+ // This method does not need to do anything for this type of iterator as when an
instace of it is
+ // created, the iterator() method in CacheQueryImpl closes everything that needs to
be closed.
+ }
+
/**
* Returns true if the list has more elements when traversing the list in the forward
direction.
*
@@ -219,4 +225,5 @@
{
throw new UnsupportedOperationException("Not supported as you are trying to
change something in the cache");
}
+
}