Home | About | Sematext search-lucene.com search-hadoop.com
 Search Lucene and all its subprojects:

Switch to Threaded View
Lucene, mail # user - Refreshing RAMDirectory


Copy link to this message
-
Re: Refreshing RAMDirectory
Ruslan Sivak 2007-12-12, 05:19
Michael McCandless wrote:
>
> Ruslan Sivak wrote:
>
>> I have an index of about 10mb.  Since it's so small, I would like to
>> keep it loaded in memory, and reload it about every minute or so,
>> assuming that it has changed on disk.  I have the following code,
>> which works, except it doesn't reload the changes.
>> protected String indexName;
>> protected IndexReader reader;
>> private long lastCheck=0;
>> ...
>> protected IndexReader getReader() throws CorruptIndexException,
>> IOException
>>    {
>>        if (reader==null || System.currentTimeMillis() > lastCheck+60000)
>>        {
>>            lastCheck=System.currentTimeMillis();
>>            if (reader==null || !reader.isCurrent())
>>            {
>>                if (reader!=null)
>>                    reader.close();
>>                              Directory dir = new
>> RAMDirectory(indexName);
>>                reader = IndexReader.open(dir);
>>                searcher = new IndexSearcher(reader);
>>            }
>>        }
>>        return reader;
>> }
>>
>>
>> Apparently reader.isCurrent() won't tell you if the underlying
>> FSDirectory has changed.
>
> That's right: your reader is only searching the RAMDirectory; it has
> no idea that your RAMDirectory was copied from an FSDirectory that has
> now changed.  (That ctor for RAMDirectory makes a full copy of what's
> currently in the FSDirectory and thereafter maintains no link to that
> FSDirectory).
>
>> I also had the following code before:
>> instead of
>> if (reader==null || !reader.isCurrent())
>> I had
>> if (reader==null || reader.getVersion() !=
>> IndexReader.getCurrentVersion(indexName))
>
> That 2nd line seems like it should have worked.  What version of
> Lucene are you using?  Are you really sure it's not showing the
> changes?  Can you print the two versions?  Every commit to the index
> (by IndexWriter) should increment that version number.
>
The 2nd line was working fine, however I was getting errors in other
places saying that the indexReader is closed.
>> I was getting a bunch of this indexreader is closed errors, and I'm
>> not sure why there's no method like reader.isClosed().
>
> That's spooky: can you explain why you're accidentally using a closed
> reader?  Your code above seems to replace reader after closing it.  
> Are there other threads that are using the reader while you are doing
> this re-opening?
>
There could be other threads using this, and there are other places in
the code that open and close readers.  My main problem I guess is that I
can't tell when a reader is closed.  Is there some method I can use?  I
basically want to do something like this.
if (reader==null || reader.isClosed || reader.getVersion() !=
IndexReader.getCurrentVersion(indexName))

Is reader threadsafe?  Should each invocation open it's own reader?

Russ

---------------------------------------------------------------------