lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ehatc...@apache.org
Subject cvs commit: jakarta-lucene/src/java/org/apache/lucene/search DateFilter.java Hits.java QueryFilter.java
Date Sat, 13 Sep 2003 23:40:30 GMT
ehatcher    2003/09/13 16:40:30

  Modified:    src/java/org/apache/lucene/search DateFilter.java Hits.java
                        QueryFilter.java
  Log:
  Elements of Java Style #1: Adhere to the style of the original.  I wholeheartedly agree.
 Reverting my previous infraction of this rule
  
  Revision  Changes    Path
  1.8       +107 -107  jakarta-lucene/src/java/org/apache/lucene/search/DateFilter.java
  
  Index: DateFilter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/DateFilter.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- DateFilter.java	13 Sep 2003 10:44:24 -0000	1.7
  +++ DateFilter.java	13 Sep 2003 23:40:29 -0000	1.8
  @@ -71,116 +71,116 @@
    * {@link DateField}.
    */
   public class DateFilter extends Filter {
  -    String field;
  +  String field;
   
  -    String start = DateField.MIN_DATE_STRING();
  -    String end = DateField.MAX_DATE_STRING();
  -
  -    private DateFilter(String f) {
  -        field = f;
  -    }
  -
  -    /**
  -     * Constructs a filter for field <code>f</code> matching dates
  -     * between <code>from</code> and <code>to</code> inclusively.
  -     */
  -    public DateFilter(String f, Date from, Date to) {
  -        field = f;
  -        start = DateField.dateToString(from);
  -        end = DateField.dateToString(to);
  -    }
  -
  -    /**
  -     * Constructs a filter for field <code>f</code> matching times
  -     * between <code>from</code> and <code>to</code> inclusively.
  -     */
  -    public DateFilter(String f, long from, long to) {
  -        field = f;
  -        start = DateField.timeToString(from);
  -        end = DateField.timeToString(to);
  -    }
  -
  -    /**
  -     * Constructs a filter for field <code>f</code> matching
  -     * dates on or before before <code>date</code>.
  -     */
  -    public static DateFilter Before(String field, Date date) {
  -        DateFilter result = new DateFilter(field);
  -        result.end = DateField.dateToString(date);
  -        return result;
  -    }
  -
  -    /**
  -     * Constructs a filter for field <code>f</code> matching times
  -     * on or before <code>time</code>.
  -     */
  -    public static DateFilter Before(String field, long time) {
  -        DateFilter result = new DateFilter(field);
  -        result.end = DateField.timeToString(time);
  -        return result;
  -    }
  -
  -    /**
  -     * Constructs a filter for field <code>f</code> matching
  -     * dates on or after <code>date</code>.
  -     */
  -    public static DateFilter After(String field, Date date) {
  -        DateFilter result = new DateFilter(field);
  -        result.start = DateField.dateToString(date);
  -        return result;
  -    }
  -
  -    /**
  -     * Constructs a filter for field <code>f</code> matching
  -     * times on or after <code>time</code>.
  -     */
  -    public static DateFilter After(String field, long time) {
  -        DateFilter result = new DateFilter(field);
  -        result.start = DateField.timeToString(time);
  -        return result;
  -    }
  -
  -    /**
  -     * Returns a BitSet with true for documents which should be
  -     * permitted in search results, and false for those that should
  -     * not.
  -     */
  -    public BitSet bits(IndexReader reader) throws IOException {
  -        BitSet bits = new BitSet(reader.maxDoc());
  -        TermEnum enumerator = reader.terms(new Term(field, start));
  -        TermDocs termDocs = reader.termDocs();
  -        if (enumerator.term() == null) {
  -            return bits;
  -        }
  +  String start = DateField.MIN_DATE_STRING();
  +  String end = DateField.MAX_DATE_STRING();
   
  +  private DateFilter(String f) {
  +    field = f;
  +  }
  +
  +  /**
  +   * Constructs a filter for field <code>f</code> matching dates
  +   * between <code>from</code> and <code>to</code> inclusively.
  +   */
  +  public DateFilter(String f, Date from, Date to) {
  +    field = f;
  +    start = DateField.dateToString(from);
  +    end = DateField.dateToString(to);
  +  }
  +
  +  /**
  +   * Constructs a filter for field <code>f</code> matching times
  +   * between <code>from</code> and <code>to</code> inclusively.
  +   */
  +  public DateFilter(String f, long from, long to) {
  +    field = f;
  +    start = DateField.timeToString(from);
  +    end = DateField.timeToString(to);
  +  }
  +
  +  /**
  +   * Constructs a filter for field <code>f</code> matching
  +   * dates on or before before <code>date</code>.
  +   */
  +  public static DateFilter Before(String field, Date date) {
  +    DateFilter result = new DateFilter(field);
  +    result.end = DateField.dateToString(date);
  +    return result;
  +  }
  +
  +  /**
  +   * Constructs a filter for field <code>f</code> matching times
  +   * on or before <code>time</code>.
  +   */
  +  public static DateFilter Before(String field, long time) {
  +    DateFilter result = new DateFilter(field);
  +    result.end = DateField.timeToString(time);
  +    return result;
  +  }
  +
  +  /**
  +   * Constructs a filter for field <code>f</code> matching
  +   * dates on or after <code>date</code>.
  +   */
  +  public static DateFilter After(String field, Date date) {
  +    DateFilter result = new DateFilter(field);
  +    result.start = DateField.dateToString(date);
  +    return result;
  +  }
  +
  +  /**
  +   * Constructs a filter for field <code>f</code> matching
  +   * times on or after <code>time</code>.
  +   */
  +  public static DateFilter After(String field, long time) {
  +    DateFilter result = new DateFilter(field);
  +    result.start = DateField.timeToString(time);
  +    return result;
  +  }
  +
  +  /**
  +   * Returns a BitSet with true for documents which should be
  +   * permitted in search results, and false for those that should
  +   * not.
  +   */
  +  public BitSet bits(IndexReader reader) throws IOException {
  +    BitSet bits = new BitSet(reader.maxDoc());
  +    TermEnum enumerator = reader.terms(new Term(field, start));
  +    TermDocs termDocs = reader.termDocs();
  +    if (enumerator.term() == null) {
  +      return bits;
  +    }
  +
  +    try {
  +      Term stop = new Term(field, end);
  +      while (enumerator.term().compareTo(stop) <= 0) {
  +        termDocs.seek(enumerator.term());
           try {
  -            Term stop = new Term(field, end);
  -            while (enumerator.term().compareTo(stop) <= 0) {
  -                termDocs.seek(enumerator.term());
  -                try {
  -                    while (termDocs.next()) {
  -                        bits.set(termDocs.doc());
  -                    }
  -                } finally {
  -                    termDocs.close();
  -                }
  -                if (!enumerator.next()) {
  -                    break;
  -                }
  -            }
  +          while (termDocs.next()) {
  +            bits.set(termDocs.doc());
  +          }
           } finally {
  -            enumerator.close();
  +          termDocs.close();
           }
  -        return bits;
  -    }
  -
  -    public String toString() {
  -        StringBuffer buffer = new StringBuffer();
  -        buffer.append(field);
  -        buffer.append(":");
  -        buffer.append(DateField.stringToDate(start).toString());
  -        buffer.append("-");
  -        buffer.append(DateField.stringToDate(end).toString());
  -        return buffer.toString();
  -    }
  +        if (!enumerator.next()) {
  +          break;
  +        }
  +      }
  +    } finally {
  +      enumerator.close();
  +    }
  +    return bits;
  +  }
  +
  +  public String toString() {
  +    StringBuffer buffer = new StringBuffer();
  +    buffer.append(field);
  +    buffer.append(":");
  +    buffer.append(DateField.stringToDate(start).toString());
  +    buffer.append("-");
  +    buffer.append(DateField.stringToDate(end).toString());
  +    return buffer.toString();
  +  }
   }
  
  
  
  1.8       +109 -107  jakarta-lucene/src/java/org/apache/lucene/search/Hits.java
  
  Index: Hits.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/Hits.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Hits.java	13 Sep 2003 10:46:25 -0000	1.7
  +++ Hits.java	13 Sep 2003 23:40:29 -0000	1.8
  @@ -61,144 +61,146 @@
   
   /** A ranked list of documents, used to hold search results. */
   public final class Hits {
  -    private Query query;
  -    private Searcher searcher;
  -    private Filter filter = null;
  +  private Query query;
  +  private Searcher searcher;
  +  private Filter filter = null;
   
  -    private int length;				  // the total number of hits
  -    private Vector hitDocs = new Vector();	  // cache of hits retrieved
  +  private int length;				  // the total number of hits
  +  private Vector hitDocs = new Vector();	  // cache of hits retrieved
   
  -    private HitDoc first;				  // head of LRU cache
  -    private HitDoc last;				  // tail of LRU cache
  -    private int numDocs = 0;			  // number cached
  -    private int maxDocs = 200;			  // max to cache
  +  private HitDoc first;         // head of LRU cache
  +  private HitDoc last;          // tail of LRU cache
  +  private int numDocs = 0;      // number cached
  +  private int maxDocs = 200;    // max to cache
   
  -    Hits(Searcher s, Query q, Filter f) throws IOException {
  -        query = q;
  -        searcher = s;
  -        filter = f;
  -        getMoreDocs(50);				  // retrieve 100 initially
  -    }
  -
  -    // Tries to add new documents to hitDocs.
  -    // Ensures that the hit numbered <code>min</code> has been retrieved.
  -    private final void getMoreDocs(int min) throws IOException {
  -        if (hitDocs.size() > min) {
  -            min = hitDocs.size();
  -        }
  +  Hits(Searcher s, Query q, Filter f) throws IOException {
  +    query = q;
  +    searcher = s;
  +    filter = f;
  +    getMoreDocs(50); // retrieve 100 initially
  +  }
   
  -        int n = min * 2;				  // double # retrieved
  -        TopDocs topDocs = searcher.search(query, filter, n);
  -        length = topDocs.totalHits;
  -        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
  +  /**
  +   * Tries to add new documents to hitDocs.
  +   * Ensures that the hit numbered <code>min</code> has been retrieved.
  +   */
  +  private final void getMoreDocs(int min) throws IOException {
  +    if (hitDocs.size() > min) {
  +      min = hitDocs.size();
  +    }
   
  -        float scoreNorm = 1.0f;
  -        if (length > 0 && scoreDocs[0].score > 1.0f) {
  -            scoreNorm = 1.0f / scoreDocs[0].score;
  -        }
  +    int n = min * 2;				  // double # retrieved
  +    TopDocs topDocs = searcher.search(query, filter, n);
  +    length = topDocs.totalHits;
  +    ScoreDoc[] scoreDocs = topDocs.scoreDocs;
   
  -        int end = scoreDocs.length < length ? scoreDocs.length : length;
  -        for (int i = hitDocs.size(); i < end; i++) {
  -            hitDocs.addElement(new HitDoc(scoreDocs[i].score * scoreNorm,
  -                                          scoreDocs[i].doc));
  -        }
  +    float scoreNorm = 1.0f;
  +    if (length > 0 && scoreDocs[0].score > 1.0f) {
  +      scoreNorm = 1.0f / scoreDocs[0].score;
       }
   
  -    /** Returns the total number of hits available in this set. */
  -    public final int length() {
  -        return length;
  +    int end = scoreDocs.length < length ? scoreDocs.length : length;
  +    for (int i = hitDocs.size(); i < end; i++) {
  +      hitDocs.addElement(new HitDoc(scoreDocs[i].score * scoreNorm,
  +                                    scoreDocs[i].doc));
       }
  +  }
   
  -    /** Returns the nth document in this set.
  -     <p>Documents are cached, so that repeated requests for the same element may
  -     return the same Document object. */
  -    public final Document doc(int n) throws IOException {
  -        HitDoc hitDoc = hitDoc(n);
  +  /** Returns the total number of hits available in this set. */
  +  public final int length() {
  +    return length;
  +  }
   
  -        // Update LRU cache of documents
  -        remove(hitDoc);               // remove from list, if there
  -        addToFront(hitDoc);           // add to front of list
  -        if (numDocs > maxDocs) {      // if cache is full
  -            HitDoc oldLast = last;
  -            remove(last);             // flush last
  -            oldLast.doc = null;       // let doc get gc'd
  -        }
  +  /** Returns the nth document in this set.
  +   <p>Documents are cached, so that repeated requests for the same element may
  +   return the same Document object. */
  +  public final Document doc(int n) throws IOException {
  +    HitDoc hitDoc = hitDoc(n);
   
  -        if (hitDoc.doc == null) {
  -            hitDoc.doc = searcher.doc(hitDoc.id);  // cache miss: read document
  -        }
  -
  -        return hitDoc.doc;
  +    // Update LRU cache of documents
  +    remove(hitDoc);               // remove from list, if there
  +    addToFront(hitDoc);           // add to front of list
  +    if (numDocs > maxDocs) {      // if cache is full
  +      HitDoc oldLast = last;
  +      remove(last);             // flush last
  +      oldLast.doc = null;       // let doc get gc'd
       }
   
  -    /** Returns the score for the nth document in this set. */
  -    public final float score(int n) throws IOException {
  -        return hitDoc(n).score;
  +    if (hitDoc.doc == null) {
  +      hitDoc.doc = searcher.doc(hitDoc.id);  // cache miss: read document
       }
   
  -    /** Returns the id for the nth document in this set. */
  -    public final int id(int n) throws IOException {
  -        return hitDoc(n).id;
  -    }
  +    return hitDoc.doc;
  +  }
   
  +  /** Returns the score for the nth document in this set. */
  +  public final float score(int n) throws IOException {
  +    return hitDoc(n).score;
  +  }
   
  -    private final HitDoc hitDoc(int n) throws IOException {
  -        if (n >= length) {
  -            throw new IndexOutOfBoundsException("Not a valid hit number: " + n);
  -        }
  +  /** Returns the id for the nth document in this set. */
  +  public final int id(int n) throws IOException {
  +    return hitDoc(n).id;
  +  }
   
  -        if (n >= hitDocs.size()) {
  -            getMoreDocs(n);
  -        }
   
  -        return (HitDoc) hitDocs.elementAt(n);
  +  private final HitDoc hitDoc(int n) throws IOException {
  +    if (n >= length) {
  +      throw new IndexOutOfBoundsException("Not a valid hit number: " + n);
       }
   
  -    private final void addToFront(HitDoc hitDoc) {  // insert at front of cache
  -        if (first == null) {
  -            last = hitDoc;
  -        } else {
  -            first.prev = hitDoc;
  -        }
  +    if (n >= hitDocs.size()) {
  +      getMoreDocs(n);
  +    }
   
  -        hitDoc.next = first;
  -        first = hitDoc;
  -        hitDoc.prev = null;
  +    return (HitDoc) hitDocs.elementAt(n);
  +  }
   
  -        numDocs++;
  +  private final void addToFront(HitDoc hitDoc) {  // insert at front of cache
  +    if (first == null) {
  +      last = hitDoc;
  +    } else {
  +      first.prev = hitDoc;
       }
   
  -    private final void remove(HitDoc hitDoc) {	  // remove from cache
  -        if (hitDoc.doc == null) {     // it's not in the list
  -            return;					  // abort
  -        }
  +    hitDoc.next = first;
  +    first = hitDoc;
  +    hitDoc.prev = null;
   
  -        if (hitDoc.next == null) {
  -            last = hitDoc.prev;
  -        } else {
  -            hitDoc.next.prev = hitDoc.prev;
  -        }
  +    numDocs++;
  +  }
   
  -        if (hitDoc.prev == null) {
  -            first = hitDoc.next;
  -        } else {
  -            hitDoc.prev.next = hitDoc.next;
  -        }
  +  private final void remove(HitDoc hitDoc) {	  // remove from cache
  +    if (hitDoc.doc == null) {     // it's not in the list
  +      return;					  // abort
  +    }
  +
  +    if (hitDoc.next == null) {
  +      last = hitDoc.prev;
  +    } else {
  +      hitDoc.next.prev = hitDoc.prev;
  +    }
   
  -        numDocs--;
  +    if (hitDoc.prev == null) {
  +      first = hitDoc.next;
  +    } else {
  +      hitDoc.prev.next = hitDoc.next;
       }
  +
  +    numDocs--;
  +  }
   }
   
   final class HitDoc {
  -    float score;
  -    int id;
  -    Document doc = null;
  -
  -    HitDoc next;  // in doubly-linked cache
  -    HitDoc prev;  // in doubly-linked cache
  -
  -    HitDoc(float s, int i) {
  -        score = s;
  -        id = i;
  -    }
  +  float score;
  +  int id;
  +  Document doc = null;
  +
  +  HitDoc next;  // in doubly-linked cache
  +  HitDoc prev;  // in doubly-linked cache
  +
  +  HitDoc(float s, int i) {
  +    score = s;
  +    id = i;
  +  }
   }
  
  
  
  1.3       +27 -26    jakarta-lucene/src/java/org/apache/lucene/search/QueryFilter.java
  
  Index: QueryFilter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/QueryFilter.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- QueryFilter.java	13 Sep 2003 10:46:25 -0000	1.2
  +++ QueryFilter.java	13 Sep 2003 23:40:29 -0000	1.3
  @@ -70,37 +70,38 @@
    * once per day.
    */
   public class QueryFilter extends Filter {
  -    private Query query;
  -    private transient WeakHashMap cache = new WeakHashMap();
  +  private Query query;
  +  private transient WeakHashMap cache = new WeakHashMap();
   
  -    /** Constructs a filter which only matches documents matching
  -     * <code>query</code>.
  -     */
  -    public QueryFilter(Query query) {
  -        this.query = query;
  +  /** Constructs a filter which only matches documents matching
  +   * <code>query</code>.
  +   */
  +  public QueryFilter(Query query) {
  +    this.query = query;
  +  }
  +
  +  public BitSet bits(IndexReader reader) throws IOException {
  +
  +    synchronized (cache) {  // check cache
  +      BitSet cached = (BitSet) cache.get(reader);
  +      if (cached != null) {
  +        return cached;
  +      }
       }
   
  -    public BitSet bits(IndexReader reader) throws IOException {
  +    final BitSet bits = new BitSet(reader.maxDoc());
   
  -        synchronized (cache) {  // check cache
  -            BitSet cached = (BitSet) cache.get(reader);
  -            if (cached != null)
  -                return cached;
  -        }
  +    new IndexSearcher(reader).search(query, new HitCollector() {
  +      public final void collect(int doc, float score) {
  +        bits.set(doc);  // set bit for hit
  +      }
  +    });
   
  -        final BitSet bits = new BitSet(reader.maxDoc());
   
  -        new IndexSearcher(reader).search(query, new HitCollector() {
  -            public final void collect(int doc, float score) {
  -                bits.set(doc);  // set bit for hit
  -            }
  -        });
  -
  -
  -        synchronized (cache) {  // update cache
  -            cache.put(reader, bits);
  -        }
  -
  -        return bits;
  +    synchronized (cache) {  // update cache
  +      cache.put(reader, bits);
       }
  +
  +    return bits;
  +  }
   }
  
  
  

Mime
View raw message