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
-
Refreshing RAMDirectory
Ruslan Sivak 2007-12-11, 22:37
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.

I also had the following code before:
instead of
if (reader==null || !reader.isCurrent())
I had
if (reader==null || reader.getVersion() !=
IndexReader.getCurrentVersion(indexName))
I was getting a bunch of this indexreader is closed errors, and I'm not
sure why there's no method like reader.isClosed().

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?

Russ

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