Author: navssurtani
Date: 2008-06-26 07:02:27 -0400 (Thu, 26 Jun 2008)
New Revision: 6049
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java
Log:
Created QueryResultIteratorImpl and fixed CacheQueryImpl
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java 2008-06-26
02:21:26 UTC (rev 6048)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java 2008-06-26
11:02:27 UTC (rev 6049)
@@ -82,35 +82,41 @@
return null;
}
-
-
/**
* @return The result size of the query.
*/
public int getResultSize()
{
- if (resultSize == null) {
+ if (resultSize == null)
+ {
//get result size without object initialization
IndexSearcher searcher = buildSearcher(searchFactory);
- if (searcher == null) {
+ if (searcher == null)
+ {
resultSize = 0;
}
- else {
+ else
+ {
Hits hits;
- try {
+ try
+ {
hits = getHits(searcher);
resultSize = hits.length();
}
- catch (IOException e) {
+ catch (IOException e)
+ {
throw new HibernateException("Unable to query Lucene index",
e);
}
- finally {
+ finally
+ {
//searcher cannot be null
- try {
+ try
+ {
closeSearcher(searcher, searchFactory.getReaderProvider());
//searchFactoryImplementor.getReaderProvider().closeReader(
searcher.getIndexReader() );
}
- catch (SearchException e) {
+ catch (SearchException e)
+ {
log.warn("Unable to properly close searcher during lucene query:
" + e);
}
}
@@ -150,7 +156,8 @@
{
Set<IndexReader> indexReaders = getIndexReaders(searcher);
- for (IndexReader indexReader : indexReaders) {
+ for (IndexReader indexReader : indexReaders)
+ {
readerProvider.closeReader(indexReader);
}
}
@@ -163,7 +170,8 @@
*/
public FullTextFilter enableFullTextFilter(String name)
{
- if (filterDefinitions == null) {
+ if (filterDefinitions == null)
+ {
filterDefinitions = new HashMap<String, FullTextFilterImpl>();
}
FullTextFilterImpl filterDefinition = filterDefinitions.get(name);
@@ -172,7 +180,8 @@
filterDefinition = new FullTextFilterImpl();
filterDefinition.setName(name);
FilterDef filterDef = searchFactory.getFilterDefinition(name);
- if (filterDef == null) {
+ if (filterDef == null)
+ {
throw new SearchException("Unkown @FullTextFilter: " + name);
}
filterDefinitions.put(name, filterDefinition);
@@ -197,7 +206,8 @@
*/
public void setFirstResult(int firstResult)
{
- if (firstResult < 0) {
+ if (firstResult < 0)
+ {
throw new IllegalArgumentException("'first' pagination parameter
less than 0");
}
this.firstResult = firstResult;
@@ -207,11 +217,13 @@
{
List list;
IndexSearcher searcher = buildSearcher(searchFactory);
- if (searcher == null) {
+ if (searcher == null)
+ {
throw new NullPointerException("IndexSearcher instance is null.");
}
- try {
+ try
+ {
Hits hits = getHits(searcher);
int first = first();
int max = max(first, hits);
@@ -219,7 +231,8 @@
List<CacheEntityId> ids = new ArrayList<CacheEntityId>(size);
DocumentExtractor extractor = new DocumentExtractor(searchFactory,
indexProjection);
- for (int index = first; index <= max; index++) {
+ for (int index = first; index <= max; index++)
+ {
String documentId = (String) extractor.extract(hits, index).id;
CacheEntityId id = new CacheEntityId(documentId);
ids.add(id);
@@ -227,14 +240,16 @@
list = entityLoader.load(ids);
}
- catch (IOException e) {
+ catch (IOException e)
+ {
throw new HibernateException("Unable to query Lucene index", e);
}
- finally {
+ finally
+ {
- closeSearcher(searcher, searchFactory.getReaderProvider());
+ closeSearcher(searcher, searchFactory.getReaderProvider());
}
@@ -247,7 +262,8 @@
Hits hits;
- try {
+ try
+ {
hits = getHits(searcher);
int first = first();
int max = max(first, hits);
@@ -257,7 +273,8 @@
List<CacheEntityId> ids = new ArrayList<CacheEntityId>(size);
- for (int index = first; index <= max; index++) {
+ for (int index = first; index <= max; index++)
+ {
String documentId = (String) extractor.extract(hits, index).id;
CacheEntityId id = new CacheEntityId(documentId);
@@ -265,18 +282,17 @@
}
List<Object> list = entityLoader.load(ids);
-
+
}
- catch (IOException e) {
+ catch (IOException e)
+ {
throw new HibernateException("Unable to query Lucene index", e);
}
return null;
}
-
-
/**
* Returns a the results from the query as a List object.
*
@@ -291,7 +307,8 @@
Hits hits;
- try {
+ try
+ {
hits = getHits(searcher);
int first = first();
int max = max(first, hits);
@@ -300,30 +317,34 @@
List<CacheEntityId> ids = new ArrayList<CacheEntityId>(size);
DocumentExtractor extractor = new DocumentExtractor(searchFactory,
indexProjection);
- for (int index = first; index <= max; index++) {
+ for (int index = first; index <= max; index++)
+ {
String documentId = (String) extractor.extract(hits, index).id;
CacheEntityId id = new CacheEntityId(documentId);
ids.add(id);
}
List<Object> list = entityLoader.load(ids);
- if (resultTransformer == null) {
+ if (resultTransformer == null)
+ {
return list;
}
- else {
+ else
+ {
return resultTransformer.transformList(list);
}
}
- catch (IOException e) {
+ catch (IOException e)
+ {
throw new HibernateException("Unable to query Lucene index", e);
}
- finally {
- closeSearcher(searcher, searchFactory.getReaderProvider());
+ finally
+ {
+ closeSearcher(searcher, searchFactory.getReaderProvider());
-
}
}
@@ -371,24 +392,22 @@
// }
// }
-
private int max(int first, Hits hits)
{
return maxResults == null ?
- hits.length() - 1 :
- maxResults + first < hits.length() ?
- first + maxResults - 1 :
- hits.length() - 1;
+ hits.length() - 1 :
+ maxResults + first < hits.length() ?
+ first + maxResults - 1 :
+ hits.length() - 1;
}
private int first()
{
return firstResult != null ?
- firstResult :
- 0;
+ firstResult :
+ 0;
}
-
public int executeUpdate() throws HibernateException
{
throw new UnsupportedOperationException(" This method is not supported in
JBossCache Searchable");
@@ -396,7 +415,8 @@
public void setMaxResults(int maxResults)
{
- if (maxResults < 0) {
+ if (maxResults < 0)
+ {
throw new IllegalArgumentException("'max' pagination parameter less
than 0");
}
this.maxResults = maxResults;
@@ -404,7 +424,8 @@
public void setFetchSize(int fetchSize)
{
- if (fetchSize <= 0) {
+ if (fetchSize <= 0)
+ {
throw new IllegalArgumentException("'fetch size' parameter less
than or equals to 0");
}
this.fetchSize = fetchSize;
@@ -427,24 +448,29 @@
Similarity searcherSimilarity = null;
- if (classes == null || classes.length == 0) {
+ if (classes == null || classes.length == 0)
+ {
//no class means all classes
- for (DocumentBuilder builder : builders.values()) {
+ for (DocumentBuilder builder : builders.values())
+ {
searcherSimilarity = checkSimilarity(searcherSimilarity, builder);
final DirectoryProvider[] directoryProviders =
builder.getDirectoryProviderSelectionStrategy().getDirectoryProvidersForAllShards();
populateDirectories(directories, directoryProviders,
searchFactoryImplementor);
}
classesAndSubclasses = null;
}
- else {
+ else
+ {
Set<Class> involvedClasses = new HashSet<Class>(classes.length);
Collections.addAll(involvedClasses, classes);
- for (Class clazz : classes) {
+ for (Class clazz : classes)
+ {
DocumentBuilder builder = builders.get(clazz);
if (builder != null) involvedClasses.addAll(builder.getMappedSubclasses());
}
- for (Class clazz : involvedClasses) {
+ for (Class clazz : involvedClasses)
+ {
DocumentBuilder builder = builders.get(clazz);
if (builder == null)
throw new HibernateException("Not a mapped entity (don't forget
to add @Indexed): " + clazz);
@@ -458,14 +484,19 @@
//compute optimization needClassFilterClause
//if at least one DP contains one class that is not part of the targeted
classesAndSubclasses we can't optimize
- if (classesAndSubclasses != null) {
- for (DirectoryProvider dp : directories) {
+ if (classesAndSubclasses != null)
+ {
+ for (DirectoryProvider dp : directories)
+ {
final Set<Class> classesInDirectoryProvider =
searchFactoryImplementor.getClassesInDirectoryProvider(dp);
// if a DP contains only one class, we know for sure it's part of
classesAndSubclasses
- if (classesInDirectoryProvider.size() > 1) {
+ if (classesInDirectoryProvider.size() > 1)
+ {
//risk of needClassFilterClause
- for (Class clazz : classesInDirectoryProvider) {
- if (!classesAndSubclasses.contains(clazz)) {
+ for (Class clazz : classesInDirectoryProvider)
+ {
+ if (!classesAndSubclasses.contains(clazz))
+ {
this.needClassFilterClause = true;
break;
}
@@ -484,10 +515,12 @@
private Similarity checkSimilarity(Similarity similarity, DocumentBuilder builder)
{
- if (similarity == null) {
+ if (similarity == null)
+ {
similarity = builder.getSimilarity();
}
- else if (!similarity.getClass().equals(builder.getSimilarity().getClass())) {
+ else if (!similarity.getClass().equals(builder.getSimilarity().getClass()))
+ {
throw new HibernateException("Cannot perform search on two entities with
differing Similarity implementations (" + similarity.getClass().getName() + "
& " + builder.getSimilarity().getClass().getName() + ")");
}
@@ -497,8 +530,10 @@
private void populateDirectories(List<DirectoryProvider> directories,
DirectoryProvider[] directoryProviders,
SearchFactoryImplementor searchFactoryImplementor)
{
- for (DirectoryProvider provider : directoryProviders) {
- if (!directories.contains(provider)) {
+ for (DirectoryProvider provider : directoryProviders)
+ {
+ if (!directories.contains(provider))
+ {
directories.add(provider);
}
}
@@ -521,16 +556,19 @@
private org.apache.lucene.search.Query
filterQueryByClasses(org.apache.lucene.search.Query luceneQuery)
{
- if (!needClassFilterClause) {
+ if (!needClassFilterClause)
+ {
return luceneQuery;
}
- else {
+ else
+ {
//A query filter is more practical than a manual class filtering post query (esp
on scrollable resultsets)
//it also probably minimise the memory footprint
BooleanQuery classFilter = new BooleanQuery();
//annihilate the scoring impact of DocumentBuilder.CLASS_FIELDNAME
classFilter.setBoost(0);
- for (Class clazz : classesAndSubclasses) {
+ for (Class clazz : classesAndSubclasses)
+ {
Term t = new Term(DocumentBuilder.CLASS_FIELDNAME, clazz.getName());
TermQuery termQuery = new TermQuery(t);
classFilter.add(termQuery, BooleanClause.Occur.SHOULD);
@@ -544,27 +582,35 @@
private void buildFilters()
{
- if (filterDefinitions != null && filterDefinitions.size() > 0) {
+ if (filterDefinitions != null && filterDefinitions.size() > 0)
+ {
ChainedFilter chainedFilter = new ChainedFilter();
- for (FullTextFilterImpl filterDefinition : filterDefinitions.values()) {
+ for (FullTextFilterImpl filterDefinition : filterDefinitions.values())
+ {
FilterDef def =
searchFactory.getFilterDefinition(filterDefinition.getName());
Class implClass = def.getImpl();
Object instance;
- try {
+ try
+ {
instance = implClass.newInstance();
}
- catch (Exception e) {
+ catch (Exception e)
+ {
throw new SearchException("Unable to create @FullTextFilterDef:
" + def.getImpl(), e);
}
- for (Map.Entry<String, Object> entry :
filterDefinition.getParameters().entrySet()) {
+ for (Map.Entry<String, Object> entry :
filterDefinition.getParameters().entrySet())
+ {
def.invoke(entry.getKey(), instance, entry.getValue());
}
- if (def.isCache() && def.getKeyMethod() == null &&
filterDefinition.getParameters().size() > 0) {
+ if (def.isCache() && def.getKeyMethod() == null &&
filterDefinition.getParameters().size() > 0)
+ {
throw new SearchException("Filter with parameters and no @Key method:
" + filterDefinition.getName());
}
FilterKey key = null;
- if (def.isCache()) {
- if (def.getKeyMethod() == null) {
+ if (def.isCache())
+ {
+ if (def.getKeyMethod() == null)
+ {
key = new FilterKey()
{
public int hashCode()
@@ -580,54 +626,68 @@
}
};
}
- else {
- try {
+ else
+ {
+ try
+ {
key = (FilterKey) def.getKeyMethod().invoke(instance);
}
- catch (IllegalAccessException e) {
+ catch (IllegalAccessException e)
+ {
throw new SearchException("Unable to access @Key method:
"
- + def.getImpl().getName() + "." +
def.getKeyMethod().getName());
+ + def.getImpl().getName() + "." +
def.getKeyMethod().getName());
}
- catch (InvocationTargetException e) {
+ catch (InvocationTargetException e)
+ {
throw new SearchException("Unable to access @Key method:
"
- + def.getImpl().getName() + "." +
def.getKeyMethod().getName());
+ + def.getImpl().getName() + "." +
def.getKeyMethod().getName());
}
- catch (ClassCastException e) {
+ catch (ClassCastException e)
+ {
throw new SearchException("@Key method does not return
FilterKey: "
- + def.getImpl().getName() + "." +
def.getKeyMethod().getName());
+ + def.getImpl().getName() + "." +
def.getKeyMethod().getName());
}
}
key.setImpl(def.getImpl());
}
Filter filter = def.isCache() ?
- searchFactory.getFilterCachingStrategy().getCachedFilter(key) :
- null;
- if (filter == null) {
- if (def.getFactoryMethod() != null) {
- try {
+ searchFactory.getFilterCachingStrategy().getCachedFilter(key) :
+ null;
+ if (filter == null)
+ {
+ if (def.getFactoryMethod() != null)
+ {
+ try
+ {
filter = (Filter) def.getFactoryMethod().invoke(instance);
}
- catch (IllegalAccessException e) {
+ catch (IllegalAccessException e)
+ {
throw new SearchException("Unable to access @Factory method:
"
- + def.getImpl().getName() + "." +
def.getFactoryMethod().getName());
+ + def.getImpl().getName() + "." +
def.getFactoryMethod().getName());
}
- catch (InvocationTargetException e) {
+ catch (InvocationTargetException e)
+ {
throw new SearchException("Unable to access @Factory method:
"
- + def.getImpl().getName() + "." +
def.getFactoryMethod().getName());
+ + def.getImpl().getName() + "." +
def.getFactoryMethod().getName());
}
- catch (ClassCastException e) {
+ catch (ClassCastException e)
+ {
throw new SearchException("@Key method does not return a
org.apache.lucene.search.Filter class: "
- + def.getImpl().getName() + "." +
def.getFactoryMethod().getName());
+ + def.getImpl().getName() + "." +
def.getFactoryMethod().getName());
}
}
- else {
- try {
+ else
+ {
+ try
+ {
filter = (Filter) instance;
}
- catch (ClassCastException e) {
+ catch (ClassCastException e)
+ {
throw new SearchException("@Key method does not return a
org.apache.lucene.search.Filter class: "
- + def.getImpl().getName() + "." +
def.getFactoryMethod().getName());
+ + def.getImpl().getName() + "." +
def.getFactoryMethod().getName());
}
}
if (def.isCache())