lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ruslan Sivak <>
Subject Re: Refreshing RAMDirectory
Date Wed, 12 Dec 2007 22:44:09 GMT
Michael McCandless wrote:
> Ruslan Sivak wrote:
>> This seems to be problematic though.  There are other things that 
>> depend on the reader that is not so obvious.  For example,
>> IndexReader reader=getReader();
>> IndexSearcher searcher=new IndexSearcher(reader);
>> Hits;
>> searcher.close();
>> reader.close();
>> Iterator i=hits.iterator();
>> if (i.hasNext())
>>    Hit h=(Hit);
>> This, for example, would not work, as accessing the hits after the 
>> reader is closed will throw a "this reader is closed" exception.  You 
>> need to have the reader open for the hits to retrieve data.
> Right.  Reader must remain open so long as it's in-use (and iterating 
> through hits counts as using it).
>> Since my app would be multithreaded, there could be multiple threads 
>> accessing the reader, while i'm reloading it.  This means that if I 
>> close the reader, and another thread is using it, it might get an 
>> exception.
> The normal approach here is open a new reader, start sending new 
> searches to the new reader, and only once all existing searches (and, 
> possibly, search sessions, if for example you want paginating through 
> results to not suddenly change on the user)  are done with the old 
> reader do you close the old one.
How exactly would I do something like this?   I'm not sure where to 
start.  From what I understand, the reader will be auto closed when 
there are no more references to it.  What if I did somehting like this

Private IndexReader reader;

public IndexReader getReader()
   if (we are reloading)
      Directory dir = new RAMDirectory (indexName);
      reader =;
   return reader;
public Results search(String searchTerms)
   IndexReader r=getReader()
   //Do search and return results

This should work, right?  The local variable will hold a reference to a 
reader that it's using, and once it goes out of use, it should auto 
close, correct?  Is closing the reader even necessary?  Won't it just 
get collected by the GC once there are no more references to it? 


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message