Return-Path: Delivered-To: apmail-lucene-java-dev-archive@www.apache.org Received: (qmail 43786 invoked from network); 26 Apr 2005 02:36:50 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 26 Apr 2005 02:36:50 -0000 Received: (qmail 55250 invoked by uid 500); 26 Apr 2005 02:37:16 -0000 Delivered-To: apmail-lucene-java-dev-archive@lucene.apache.org Received: (qmail 55223 invoked by uid 500); 26 Apr 2005 02:37:15 -0000 Mailing-List: contact java-dev-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: java-dev@lucene.apache.org Delivered-To: mailing list java-dev@lucene.apache.org Received: (qmail 55209 invoked by uid 99); 26 Apr 2005 02:37:15 -0000 X-ASF-Spam-Status: No, hits=2.2 required=10.0 tests=FORGED_YAHOO_RCVD X-Spam-Check-By: apache.org Received-SPF: pass (hermes.apache.org: local policy) Received: from web31103.mail.mud.yahoo.com (HELO web31103.mail.mud.yahoo.com) (68.142.200.36) by apache.org (qpsmtpd/0.28) with SMTP; Mon, 25 Apr 2005 19:37:14 -0700 Received: (qmail 82868 invoked by uid 60001); 26 Apr 2005 02:36:36 -0000 Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; b=ZKNfSIWXCkTj0zkbZnRItMf0xJg4XrEh8oVwcaRyQyWmQJxW26dVK7g39J6UdbfNJMkOeb7tXBiQ5IVWSJU/jtxD2ATeviNJkhjStEP+RqZwq9dLQbWq+zQY8pf/Xvh/Zb+Sp9mZRhNytUdUh4KetzRgnDNlkl0c4xF5LC1gvD8= ; Message-ID: <20050426023636.82866.qmail@web31103.mail.mud.yahoo.com> Received: from [69.201.130.192] by web31103.mail.mud.yahoo.com via HTTP; Mon, 25 Apr 2005 19:36:36 PDT Date: Mon, 25 Apr 2005 19:36:36 -0700 (PDT) From: Otis Gospodnetic Subject: Re: svn commit: r164695 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/search/Hit.java src/java/org/apache/lucene/search/HitIterator.java src/java/org/apache/lucene/search/Hits.java src/test/org/apache/lucene/TestHitIterator.java To: java-dev@lucene.apache.org In-Reply-To: <20050426002155.94510.qmail@minotaur.apache.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Hello, Would it be better to explicitly check for out of bounds hitNumber instead of catching ArrayIndexOutOfBoundsException? if (hitNumber > hits.length()) { throw new NoSuchElementException(); } Also, is "a future for a hit" a typo, or does that actually mean something? This makes me think of Python's "future", but I'm not sure what this means in this context. Thanks, Otis --- ehatcher@apache.org wrote: > Author: ehatcher > Date: Mon Apr 25 17:21:53 2005 > New Revision: 164695 > > URL: http://svn.apache.org/viewcvs?rev=164695&view=rev > Log: > Add Hits.iterator and corresponding HitIterator and Hit classes. > Contributed by Jeremy Rayner > > Added: > lucene/java/trunk/src/java/org/apache/lucene/search/Hit.java > > lucene/java/trunk/src/java/org/apache/lucene/search/HitIterator.java > lucene/java/trunk/src/test/org/apache/lucene/TestHitIterator.java > Modified: > lucene/java/trunk/CHANGES.txt > lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java > > Modified: lucene/java/trunk/CHANGES.txt > URL: > http://svn.apache.org/viewcvs/lucene/java/trunk/CHANGES.txt?rev=164695&r1=164694&r2=164695&view=diff > ============================================================================== > --- lucene/java/trunk/CHANGES.txt (original) > +++ lucene/java/trunk/CHANGES.txt Mon Apr 25 17:21:53 2005 > @@ -91,6 +91,11 @@ > which lets the caller get the Lucene version information > specified in > the Lucene Jar. > (Doug Cutting via Otis) > + > +15. Added Hits.iterator() method and corresponding HitIterator and > Hit objects. > + This provides standard java.util.Iterator iteration over Hits. > + Each call to the iterator's next() method returns a Hit object. > + (Jeremy Rayner via Erik) > > API Changes > > > Added: lucene/java/trunk/src/java/org/apache/lucene/search/Hit.java > URL: > http://svn.apache.org/viewcvs/lucene/java/trunk/src/java/org/apache/lucene/search/Hit.java?rev=164695&view=auto > ============================================================================== > --- lucene/java/trunk/src/java/org/apache/lucene/search/Hit.java > (added) > +++ lucene/java/trunk/src/java/org/apache/lucene/search/Hit.java Mon > Apr 25 17:21:53 2005 > @@ -0,0 +1,125 @@ > +/** > + * Copyright 2005 The Apache Software Foundation > + * > + * Licensed under the Apache License, Version 2.0 (the "License"); > + * you may not use this file except in compliance with the License. > + * You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, > software > + * distributed under the License is distributed on an "AS IS" BASIS, > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or > implied. > + * See the License for the specific language governing permissions > and > + * limitations under the License. > + */ > + > +package org.apache.lucene.search; > + > +import java.io.IOException; > + > +import org.apache.lucene.document.Document; > + > +/** > + * a lazy future for a hit, useful for iterators over instances of > Hits > + * > + * @author Jeremy Rayner > + */ > +public class Hit implements java.io.Serializable { > + > + private float score; > + private int id; > + private Document doc = null; > + > + private boolean resolved = false; > + > + private Hits hits = null; > + private int hitNumber; > + > + /** > + * Constructed from {@link HitIterator} > + * @param hits Hits returned from a search > + * @param hitNumber Hit index in Hits > + */ > + Hit(Hits hits, int hitNumber) { > + this.hits = hits; > + this.hitNumber = hitNumber; > + } > + > + /** > + * Returns document for this hit. > + * > + * @see {@link Hits#doc(int)} > + */ > + public Document getDocument() throws IOException { > + if (!resolved) fetchTheHit(); > + return doc; > + } > + > + /** > + * Returns score for this hit. > + * > + * @see {@link Hits#score(int)} > + */ > + public float getScore() throws IOException { > + if (!resolved) fetchTheHit(); > + return score; > + } > + > + /** > + * Returns id for this hit. > + * > + * @see {@link Hits#id(int)} > + */ > + public int getId() throws IOException { > + if (!resolved) fetchTheHit(); > + return id; > + } > + > + private void fetchTheHit() throws IOException { > + doc = hits.doc(hitNumber); > + score = hits.score(hitNumber); > + id = hits.id(hitNumber); > + resolved = true; > + } > + > + // provide some of the Document style interface (the simple stuff) > + > + /** > + * Returns the boost factor for this hit on any field of the > underlying document. > + * > + * @see {@link Document#getBoost()} > + */ > + public float getBoost() throws IOException { > + return getDocument().getBoost(); > + } > + > + /** > + * Returns the string value of the field with the given name if > any exist in > + * this document, or null. If multiple fields exist with this > name, this > + * method returns the first value added. If only binary fields > with this name > + * exist, returns null. > + * > + * @see {@link Document#get(String)} > + */ > + public String get(String name) throws IOException { > + return getDocument().get(name); > + } > + > + /** > + * Prints the fields of the underlying document for human > consumption. > + *

> + * If an IOException occurs whilst getting the document, returns > null > + * > + * @see {@link Document#toString()} > + */ > + public String toString() { > + try { > + return getDocument().toString(); > + } catch (IOException e) { > + return null; > + } > + } > + > + > +} > > Added: > lucene/java/trunk/src/java/org/apache/lucene/search/HitIterator.java > URL: > http://svn.apache.org/viewcvs/lucene/java/trunk/src/java/org/apache/lucene/search/HitIterator.java?rev=164695&view=auto > ============================================================================== > --- > lucene/java/trunk/src/java/org/apache/lucene/search/HitIterator.java > (added) > +++ > lucene/java/trunk/src/java/org/apache/lucene/search/HitIterator.java > Mon Apr 25 17:21:53 2005 > @@ -0,0 +1,78 @@ > +/** > + * Copyright 2005 The Apache Software Foundation > + * > + * Licensed under the Apache License, Version 2.0 (the "License"); > + * you may not use this file except in compliance with the License. > + * You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, > software > + * distributed under the License is distributed on an "AS IS" BASIS, > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or > implied. > + * See the License for the specific language governing permissions > and > + * limitations under the License. > + */ > + > +package org.apache.lucene.search; > + > +import java.util.Iterator; > +import java.util.NoSuchElementException; > + > +/** > + * An iterator over {@link Hits} that provides lazy fetching of each > document. > + * {@link Hits#iterator()} returns an instance of this class. Calls > to {@link #next()} > + * return a {@link Hit} instance. > + * > + * @author Jeremy Rayner > + */ > +public class HitIterator implements Iterator { > + private Hits hits; > + private int hitNumber = 0; > + > + /** > + * Constructed from {@link Hits#iterator()}. > + */ > + HitIterator(Hits hits) { > + this.hits = hits; > + } > + > + /** > + * @return true if current hit is less than the total number of > {@link Hits}. > + */ > + public boolean hasNext() { > + return hitNumber < hits.length(); > + } > + > + /** > + * Returns a {@link Hit} instance representing the next hit in > {@link Hits}. > + * > + * @return Next {@link Hit}. > + */ > + public Object next() { > + try { > + Object next = new Hit(hits, hitNumber); > + hitNumber++; > + return next; > + } catch (IndexOutOfBoundsException e) { > + throw new NoSuchElementException(); > + } > + } > + > + /** > + * Unsupported operation. > + * > + * @throws UnsupportedOperationException > + */ > + public void remove() { > + throw new UnsupportedOperationException(); > + } > + > + /** > + * Returns the total number of hits. > + */ > + public int length() { > + return hits.length(); > + } > +} > + > > Modified: > lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java > URL: > http://svn.apache.org/viewcvs/lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java?rev=164695&r1=164694&r2=164695&view=diff > ============================================================================== > --- lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java > (original) > +++ lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java Mon > Apr 25 17:21:53 2005 > @@ -18,6 +18,7 @@ > > import java.io.IOException; > import java.util.Vector; > +import java.util.Iterator; > > import org.apache.lucene.document.Document; > > @@ -114,6 +115,13 @@ > return hitDoc(n).id; > } > > + /** > + * Returns an {@link Iterator} to navigate the Hits. Each item > returned > + * from {@link Iterator#next()} is a {@link Hit}. > + */ > + public Iterator iterator() { > + return new HitIterator(this); > + } > > private final HitDoc hitDoc(int n) throws IOException { > if (n >= length) { > > Added: > lucene/java/trunk/src/test/org/apache/lucene/TestHitIterator.java > URL: > http://svn.apache.org/viewcvs/lucene/java/trunk/src/test/org/apache/lucene/TestHitIterator.java?rev=164695&view=auto > ============================================================================== > --- lucene/java/trunk/src/test/org/apache/lucene/TestHitIterator.java > (added) > +++ lucene/java/trunk/src/test/org/apache/lucene/TestHitIterator.java > Mon Apr 25 17:21:53 2005 > @@ -0,0 +1,50 @@ > +package org.apache.lucene; > + > +import junit.framework.TestCase; > +import org.apache.lucene.store.RAMDirectory; > +import org.apache.lucene.index.IndexWriter; > +import org.apache.lucene.index.Term; > +import org.apache.lucene.analysis.WhitespaceAnalyzer; > +import org.apache.lucene.document.Document; > +import org.apache.lucene.document.Field; > +import org.apache.lucene.search.IndexSearcher; > +import org.apache.lucene.search.TermQuery; > +import org.apache.lucene.search.Hits; > +import org.apache.lucene.search.Hit; > +import org.apache.lucene.search.HitIterator; > + > +/** > + * This test intentionally not put in the search package in order > + * to test HitIterator and Hit package protection. > + */ > +public class TestHitIterator extends TestCase { > + public void testIterator() throws Exception { > + RAMDirectory directory = new RAMDirectory(); > + > + IndexWriter writer = new IndexWriter(directory, new > WhitespaceAnalyzer(), true); > + Document doc = new Document(); > + doc.add(new Field("field", "iterator test doc 1", > Field.Store.YES, Field.Index.TOKENIZED)); > + writer.addDocument(doc); > + > + doc = new Document(); > + doc.add(new Field("field", "iterator test doc 2", > Field.Store.YES, Field.Index.TOKENIZED)); > + writer.addDocument(doc); > + > + writer.close(); > + > + IndexSearcher searcher = new IndexSearcher(directory); > + Hits hits = searcher.search(new TermQuery(new Term("field", > "iterator"))); > + > + HitIterator iterator = (HitIterator) hits.iterator(); > + assertEquals(2, iterator.length()); > + assertTrue(iterator.hasNext()); > + Hit hit = (Hit) iterator.next(); > + assertEquals("iterator test doc 1", hit.get("field")); > + > + assertTrue(iterator.hasNext()); > + hit = (Hit) iterator.next(); > + assertEquals("iterator test doc 2", > hit.getDocument().get("field")); > + > + assertFalse(iterator.hasNext()); > + } > +} > > > --------------------------------------------------------------------- To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org For additional commands, e-mail: java-dev-help@lucene.apache.org