FileDocCategorySizeDatePackage
FilterManager.javaAPI DocApache Lucene 2.2.07020Sat Jun 16 22:20:34 BST 2007org.apache.lucene.search

FilterManager

public class FilterManager extends Object
Filter caching singleton. It can be used by {@link org.apache.lucene.search.RemoteCachingWrapperFilter} or just to save filters locally for reuse. This class makes it possble to cache Filters even when using RMI, as it keeps the cache on the seaercher side of the RMI connection. Also could be used as a persistent storage for any filter as long as the filter provides a proper hashCode(), as that is used as the key in the cache. The cache is periodically cleaned up from a separate thread to ensure the cache doesn't exceed the maximum size.
author
Matt Ericson

Fields Summary
protected static FilterManager
manager
protected static final int
DEFAULT_CACHE_CLEAN_SIZE
The default maximum number of Filters in the cache
protected static final long
DEFAULT_CACHE_SLEEP_TIME
The default frequency of cache clenup
protected Map
cache
The cache itself
protected int
cacheCleanSize
Maximum allowed cache size
protected long
cleanSleepTime
Cache cleaning frequency
protected FilterCleaner
filterCleaner
Cache cleaner that runs in a separate thread
Constructors Summary
protected FilterManager()
Sets up the FilterManager singleton.

    cache            = new HashMap();
    cacheCleanSize   = DEFAULT_CACHE_CLEAN_SIZE; // Let the cache get to 100 items
    cleanSleepTime   = DEFAULT_CACHE_SLEEP_TIME; // 10 minutes between cleanings

    filterCleaner   = new FilterCleaner();
    Thread fcThread = new Thread(filterCleaner);
    // setto be a Daemon so it doesn't have to be stopped
    fcThread.setDaemon(true);
    fcThread.start();
  
Methods Summary
public org.apache.lucene.search.FiltergetFilter(org.apache.lucene.search.Filter filter)
Returns the cached version of the filter. Allows the caller to pass up a small filter but this will keep a persistent version around and allow the caching filter to do its job.

param
filter The input filter
return
The cached version of the filter

    synchronized(cache) {
      FilterItem fi = null;
      fi = (FilterItem)cache.get(new Integer(filter.hashCode()));
      if (fi != null) {
        fi.timestamp = new Date().getTime();
        return fi.filter;
      }
      cache.put(new Integer(filter.hashCode()), new FilterItem(filter));
      return filter;
    }
  
public static synchronized org.apache.lucene.search.FilterManagergetInstance()


       
    if (manager == null) {
      manager = new FilterManager();
    }
    return manager;
  
public voidsetCacheSize(int cacheCleanSize)
Sets the max size that cache should reach before it is cleaned up

param
cacheCleanSize maximum allowed cache size

    this.cacheCleanSize = cacheCleanSize;
  
public voidsetCleanThreadSleepTime(long cleanSleepTime)
Sets the cache cleaning frequency in milliseconds.

param
cleanSleepTime cleaning frequency in millioseconds

    this.cleanSleepTime  = cleanSleepTime;