/*
 * ManagedSearcher.java
 *
 * Created on 2002. április 6., 23:21
 */

package iac;
import org.apache.lucene.search.*;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import java.io.IOException;
/**
 * The Searcher object used in conjuction with the IndexAccessControl component.
 *
 * @author  Peter Halacsy
 * @version
 */
public class ManagedSearcher extends Searcher {
    protected Searcher m_searcher;
    
    protected long m_lastUsed;
    
    /** object that is interested in the fact that this searcher is closed */
    protected SearcherListener m_manager;
    
    /** Creates new ManagedSearcher */
    public ManagedSearcher(iac.SearcherListener manager, Searcher searcher) {
        System.out.println(Thread.currentThread().getName() + " new ManagedSearcher created");
        m_manager = manager;
        m_searcher = searcher;
        touch();
    }
    
    Searcher getRealSearcher() {
        return m_searcher;
    }
    /** Notify listener  */
    public void close() throws IOException {
        // do not close the searcher but notify listener
        m_manager.searcherClosed(new SearcherEvent(this));
    }
    
    /** helper method to save the time the delegated Searcher last was used */
    private final void touch() {
        m_lastUsed = System.currentTimeMillis();
    }
    /** returns milliseconds from when this object was not used */
    public final long getAge() {
        return System.currentTimeMillis() - m_lastUsed;
    }
    
    
    // implementing methods of Searcher
    
    /** For use by {@link HitCollector} implementations.  */
    public Document doc(int i) throws IOException {
        touch();
        return doc(i);
    }

    public void search(Query query, Filter filter, HitCollector results) throws IOException {
        touch();
        m_searcher.search(query, filter, results);
    }
    public TopDocs search(Query query, Filter filter, int n) throws IOException {
        touch();
        return m_searcher.search(query, filter, n);
    }
    
    public int docFreq(Term term) throws IOException {
        touch();
        return m_searcher.docFreq(term);
    }
    
    public int maxDoc() throws IOException {
        touch();
        return m_searcher.maxDoc();
    }
    
}
