FileDocCategorySizeDatePackage
CachedFilterBuilder.javaAPI DocApache Lucene 2.1.04440Wed Feb 14 10:46:20 GMT 2007org.apache.lucene.xmlparser.builders

CachedFilterBuilder

public class CachedFilterBuilder extends Object implements org.apache.lucene.xmlparser.FilterBuilder
Filters are cached in an LRU Cache keyed on the contained query or filter object. Using this will speed up overall performance for repeated uses of the same expensive query/filter. The sorts of queries/filters likely to benefit from caching need not necessarily be complex - e.g. simple TermQuerys with a large DF (document frequency) can be expensive on large indexes. A good example of this might be a term query on a field with only 2 possible values - "true" or "false". In a large index, querying or filtering on this field requires reading millions of document ids from disk which can more usefully be cached as a filter bitset. For Queries/Filters to be cached and reused the object must implement hashcode and equals methods correctly so that duplicate queries/filters can be detected in the cache. The CoreParser.maxNumCachedFilters property can be used to control the size of the LRU Cache established during the construction of CoreParser instances.
author
maharwood

Fields Summary
private org.apache.lucene.xmlparser.QueryBuilderFactory
queryFactory
private org.apache.lucene.xmlparser.FilterBuilderFactory
filterFactory
private LRUCache
filterCache
private int
cacheSize
Constructors Summary
public CachedFilterBuilder(org.apache.lucene.xmlparser.QueryBuilderFactory queryFactory, org.apache.lucene.xmlparser.FilterBuilderFactory filterFactory, int cacheSize)


	   
			  
	
		this.queryFactory=queryFactory;
		this.filterFactory=filterFactory;
		this.cacheSize=cacheSize;
	
Methods Summary
public org.apache.lucene.search.FiltergetFilter(org.w3c.dom.Element e)


		Element childElement = DOMUtils.getFirstChildOrFail(e);

		if (filterCache == null)
		{
			filterCache = new LRUCache(cacheSize);
		}

		// Test to see if child Element is a query or filter that needs to be
		// cached
		QueryBuilder qb = queryFactory.getQueryBuilder(childElement
				.getNodeName());
		Object cacheKey = null;
		Query q = null;
		Filter f = null;
		if (qb != null)
		{
			q = qb.getQuery(childElement);
			cacheKey = q;
		} else
		{
			f = filterFactory.getFilter(childElement);
			cacheKey = f;
		}
		Filter cachedFilter = null;
		synchronized (filterCache)
		{ // check cache
			cachedFilter = (Filter) filterCache.get(cacheKey);
			if (cachedFilter != null)
			{
				return cachedFilter; // cache hit
			}
		}
		
		//cache miss
		if (qb != null)
		{
			cachedFilter = new QueryFilter(q);
		} else
		{
			cachedFilter = new CachingWrapperFilter(f);
		}

		synchronized (filterCache)
		{ // update cache
			filterCache.put(cacheKey, cachedFilter);
		}
		return cachedFilter;