Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Lucene: Java-User

Multiple threads under tomcat

 

 

Lucene java-user RSS feed   Index | Next | Previous | View Threaded


jrhoden at unimelb

Nov 10, 2009, 4:23 PM

Post #1 of 2 (390 views)
Permalink
Multiple threads under tomcat

Apologies if this info is already somewhere, but google cant find it
(: I am assuming the following code is completely thread safe:



// Called from a servlet when a user action results in the index
needing to be updated
public static void rebuildIndex() {
FSDirectory dir = new NIOFSDirectory(new
File(Configuration.getAttachmentFolder()), null);
IndexWriter w = new IndexWriter(dir, analyzer,
IndexWriter.MaxFieldLength.UNLIMITED);
.... build index ...
w.optimize();
w.commit();
w.close();
}

The following code is called from the search servlet:

// Called by the search servlet
public void search() {
FSDirectory dir = new NIOFSDirectory(new
File(Configuration.getAttachmentFolder()), null);
IndexReader indexReader = IndexReader.open(dir,true);
IndexSearcher searcher = new IndexSearcher(indexReader);
.... do the search ...
}

I am trying to work out the best/correct way to improve upon this:

1) Should the "FSDirectory dir" object be shared as some sort of
static variable?
2) Should the "IndexSearcher searcher" or the "IndexReader
indexReader" objects be stored as a static variable, and then simply
replaced when the index is rebuilt?


Thanks,
Jacob

____________________________________
Information Technology Services,
The University of Melbourne

Email: jrhoden [at] unimelb
Phone: +61 3 8344 2884
Mobile: +61 4 1095 7575


erickerickson at gmail

Nov 10, 2009, 6:02 PM

Post #2 of 2 (361 views)
Permalink
Re: Multiple threads under tomcat [In reply to]

1) Should the "FSDirectory dir" object be shared as some sort of static
variable?
I don't think it matters, do the simplest thing. The overhead in creating an
FSDirectory is so small in relation to the other operations that I don't
think you'd ever notice.

2) Should the "IndexSearcher searcher" or the "IndexReader indexReader"
objects be stored as a static variable, and then simply replaced when the
index is rebuilt?

Absolutely. Opening a searcher is expensive and they should only be opened
when necessary. However, if you have multiple threads using the same
searcher, you cannot close that searcher until all the threads are done with
their current search. There's an example of this in Lucene In Action II,
available from Manning in PDF format (it's still undergoing final editing).

Best
Erick


On Tue, Nov 10, 2009 at 7:23 PM, Jacob Rhoden <jrhoden [at] unimelb>wrote:

> Apologies if this info is already somewhere, but google cant find it (: I
> am assuming the following code is completely thread safe:
>
>
>
> // Called from a servlet when a user action results in the index needing to
> be updated
> public static void rebuildIndex() {
> FSDirectory dir = new NIOFSDirectory(new
> File(Configuration.getAttachmentFolder()), null);
> IndexWriter w = new IndexWriter(dir, analyzer,
> IndexWriter.MaxFieldLength.UNLIMITED);
> .... build index ...
> w.optimize();
> w.commit();
> w.close();
> }
>
> The following code is called from the search servlet:
>
> // Called by the search servlet
> public void search() {
> FSDirectory dir = new NIOFSDirectory(new
> File(Configuration.getAttachmentFolder()), null);
> IndexReader indexReader = IndexReader.open(dir,true);
> IndexSearcher searcher = new IndexSearcher(indexReader);
> .... do the search ...
> }
>
> I am trying to work out the best/correct way to improve upon this:
>
> 1) Should the "FSDirectory dir" object be shared as some sort of static
> variable?
> 2) Should the "IndexSearcher searcher" or the "IndexReader indexReader"
> objects be stored as a static variable, and then simply replaced when the
> index is rebuilt?
>
>
> Thanks,
> Jacob
>
> ____________________________________
> Information Technology Services,
> The University of Melbourne
>
> Email: jrhoden [at] unimelb
> Phone: +61 3 8344 2884
> Mobile: +61 4 1095 7575
>
>

Lucene java-user RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.