Return-Path: Delivered-To: apmail-lucene-java-user-archive@www.apache.org Received: (qmail 31973 invoked from network); 12 Nov 2009 00:33:38 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 12 Nov 2009 00:33:38 -0000 Received: (qmail 87924 invoked by uid 500); 12 Nov 2009 00:33:36 -0000 Delivered-To: apmail-lucene-java-user-archive@lucene.apache.org Received: (qmail 87837 invoked by uid 500); 12 Nov 2009 00:33:36 -0000 Mailing-List: contact java-user-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: java-user@lucene.apache.org Delivered-To: mailing list java-user@lucene.apache.org Received: (qmail 87827 invoked by uid 99); 12 Nov 2009 00:33:36 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Nov 2009 00:33:36 +0000 X-ASF-Spam-Status: No, hits=-2.6 required=5.0 tests=BAYES_00,HTML_MESSAGE X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [72.249.82.150] (HELO uptecs.net) (72.249.82.150) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Nov 2009 00:33:33 +0000 Received: from wanyin.its.unimelb.edu.au (wanyin.its.unimelb.edu.au [128.250.18.111]) (Authenticated sender: jacob) by uptecs.net (Postfix) with ESMTPA id 1100A7C1DF for ; Thu, 12 Nov 2009 00:33:09 +0000 (UTC) Message-Id: <6EE19AEC-1763-413D-BE80-6B4E2D932E12@unimelb.edu.au> From: Jacob Rhoden To: java-user@lucene.apache.org In-Reply-To: <359a92830911111453o1adfd89cy5a2bc3fd9e62273a@mail.gmail.com> Content-Type: multipart/alternative; boundary=Apple-Mail-2-505914640 Mime-Version: 1.0 (Apple Message framework v936) Subject: Re: Wrapping IndexSearcher so that it is safe? Date: Thu, 12 Nov 2009 11:33:08 +1100 References: <359a92830911111453o1adfd89cy5a2bc3fd9e62273a@mail.gmail.com> X-Mailer: Apple Mail (2.936) --Apple-Mail-2-505914640 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit The source code for SearcherManager is even downloadable for free: http://www.manning.com/hatcher3/LIAsourcecode.zip The example source code does some things that is beyond my level of understanding of lucene. ie: 1) To me it looks like an IndexSearcher never gets closed. 2) I don't understand what happens if the indexreader is reopened while a thread in the middle of a search using an indexsearcher. So I am going for something a bit simpler: If a thread wants to use the "SafeIndexSearcher", it first calls retain() and then calls release() when its done. If a thread wants to close the "SafeIndexSearcher" , the close is deferred until all threads have called release(): public class SafeIndexSearcher { private boolean finish = false; private int retainCount = 0; private IndexSearcher searcher; public SafeIndexSearcher(IndexSearcher searcher) { this.searcher = searcher; } public TopDocs search(Query query, int limit) throws IOException { TopDocs result = searcher.search(query, limit); return result; } public Document doc(int doc) throws CorruptIndexException, IOException { return searcher.doc(doc); } public synchronized void close() { finish = true; } public synchronized SafeIndexSearcher retain() throws IOException { if(finish) throw new IOException("SafeIndexSearcher used after close has been called."); retainCount++; return this; } public synchronized SafeIndexSearcher release() { retainCount--; if(finish && retainCount==0) try { searcher.close(); } catch (IOException e) { System.err.println("IndexSearcher.close() unexpected error: " + e.getMessage()); } return this; } } On 12/11/2009, at 9:53 AM, Erick Erickson wrote: > If you want to spend a few bucks, here's part of a reply to a similar > question > from Mike McCandless a day or so ago.... > > <<< > You can get the book here http://www.manning.com/hatcher3 (NOTE: I'm > one of the authors!). > > Chapter 11 in the book has a class called SearcherManager, that > handles the details of reopen/closing the IndexReader while queries > are still in flight, that might be useful here. >>>> > > The book is Lucene In Action II. Manning has an "early access program" > (MEAP) that lets you get a PDF version. That class is considerably > more > extensive and handles the edge cases as I remember it.... > > Best > Erick > > > On Wed, Nov 11, 2009 at 5:41 PM, Jacob Rhoden > wrote: > >> I knew I would have overlooked something, thanks for the help! >> >> On 12/11/2009, at 9:21 AM, Uwe Schindler wrote: >> >> ....simply do not catch and rethrow the IOException, instead put >> release >>> in a >>> >>> finally block and let the IOException automatically go upwards. >>> >>> this.retain(); >>>> try { >>>> TopDocs result = searcher.search(query, >>>> limit); >>>> return result; >>>> } finally { >>>> this.release(); >>>> } >>>> >>> >>> Less code more secure and effective :-) >>> >> >> >> ____________________________________ >> Information Technology Services, >> The University of Melbourne >> >> Email: jrhoden@unimelb.edu.au >> Phone: +61 3 8344 2884 >> Mobile: +61 4 1095 7575 >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org >> For additional commands, e-mail: java-user-help@lucene.apache.org >> >> ____________________________________ Information Technology Services, The University of Melbourne Email: jrhoden@unimelb.edu.au Phone: +61 3 8344 2884 Mobile: +61 4 1095 7575 --Apple-Mail-2-505914640--