lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Rhoden <jrho...@unimelb.edu.au>
Subject How to safely close Index objects?
Date Wed, 11 Nov 2009 05:13:33 GMT
Hi Guys,

Given a class with two static variables, is the following safe? ie If  
I call "close" while something else is using the objects, do the  
objects simply hold a flag saying they need to be destroyed once the  
objects are finished being used, or do they not track if anything is  
currently using the object and simply blow up if I try to close them?

public static void rebuildIndex() throws IOException, ParseException {

	if(dir==null)
		dir = new NIOFSDirectory(new  
File(Configuration.getAttachmentFolder()), null);

	IndexWriter w = new IndexWriter(dir, analyzer, true,  
IndexWriter.MaxFieldLength.UNLIMITED);
	.... do the building here   ...
	w.optimize();
	w.commit();
	w.close();

	// Remember the current search objects
	IndexReader oldIndexReader = indexReader;
	IndexSearcher oldIndexSearcher = searcher;

	// Allocate new search objects
	indexReader = IndexReader.open(dir,true);
	searcher = new IndexSearcher(indexReader);

	// Cleanup on the old search objects
	try {
		if(oldIndexSearcher!=null)
			oldIndexSearcher.close();
		if(oldIndexReader!=null)
			oldIndexReader.close();
	} catch(IOException e) {
		// There's nothing we can do if a file was not able to be closed  
correctly.
	}
}

I am not sure if I need to create and remember about both the index  
reader and index searcher, the javadocs are a bit unclear on that.

How does one get the early edition of the lucene book? I couldn't work  
out how from the website. I am assuming I am missing something obvious  
(:

Thanks,
Jacob

____________________________________
Information Technology Services,
The University of Melbourne

Email: jrhoden@unimelb.edu.au
Phone: +61 3 8344 2884
Mobile: +61 4 1095 7575


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message