Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a Lucene IndexWriter instance is valid/open?

Sorry for the simple question, but there doesn't seem to be any obvious way. According to the documentation, it is recommended to keep a single instance of IndexWriter in memory that can be used again and again for updates, as opposed to opening and closing one for each change (which is much more costly).

However, the documentation also states that the IndexWriter should be closed if an exception occurs (e.g. OutOfMemoryException).

Therefore, I need some way to check if my instance of IndexWriter is valid or not, assuming it could be closed by some other operation.

I realize I will get an exception if I try to use it when it's closed, but rather than fail, I would like to check to see if I need to create a new IndexWriter before I need it so no failure occurs.

It seems to me a simple IsOpen property/method would be such an obvious addition.

Thoughts:

I suppose that if I could ensure that my class is the only thing capable of closing the IndexWriter, I could simply set it to null when it was closed and just check to make sure it's not null whenever I go to use it, however this would not handle cases where the IndexWriter closes itself (if such a case is possible now or in the future).

Another thought is that I could wrap an attempt to use it in a try/catch block and use the exception to indicate that it needs to be reopened, but this doesn't seem very efficient/elegant. What method would I use to test the validity of the IndexWriter?

like image 254
devios1 Avatar asked Oct 13 '22 17:10

devios1


1 Answers

If you get an out of memory exception (or any kind of exception, really), that's potentially a big problem. The changes you were trying to write probably won't get written, and your index may even be corrupt, depending on when/how the exception occurred.

So an IndexWriter being in a faulted state is an exceptional circumstance, and I would say it certainly warrants using exceptions, i.e. try/catch.

(I don't think your catch would be "reopen the writer." Depending on the exception, you might need to reindex stuff, possibly from scratch. Certainly you shouldn't expect to get exceptions in your writer as a regular occurrence.)

like image 109
Xodarap Avatar answered Dec 27 '22 13:12

Xodarap