lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael McCandless <>
Subject Re: Refreshing RAMDirectory
Date Wed, 12 Dec 2007 00:36:36 GMT

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 =;
>                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.

> 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?

> Am I going about things the right way?  Is there a better  
> implementation of what I'm looking to do?  Is there perhaps some  
> function I'm not seeing which will let me know if the indexreader  
> is closed?

Your 2nd line above is the right way I think.


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

View raw message