IndexReader[] readers = new IndexReader[length];
if ( trace ) log.trace( "Opening IndexReader for directoryProviders: " + length );
for (int index = 0; index < length; index++) {
DirectoryProvider directoryProvider = directoryProviders[index];
IndexReader reader;
Lock directoryProviderLock = perDirectoryProviderManipulationLocks.get( directoryProvider );
if ( trace ) log.trace( "Opening IndexReader from " + directoryProvider.getDirectory().toString() );
directoryProviderLock.lock(); //needed for same problem as the double-checked locking
try {
reader = activeSearchIndexReaders.get( directoryProvider );
}
finally {
directoryProviderLock.unlock();
}
if ( reader == null ) {
if ( trace )
log.trace( "No shared IndexReader, opening a new one: " + directoryProvider.getDirectory().toString() );
reader = replaceActiveReader( null, directoryProviderLock, directoryProvider, readers );
}
else {
boolean isCurrent;
try {
isCurrent = reader.isCurrent();
}
catch (IOException e) {
throw new SearchException( "Unable to read current status of Lucene IndexReader", e );
}
if ( !isCurrent ) {
if ( trace )
log.trace( "Out of date shared IndexReader found, opening a new one: " + directoryProvider.getDirectory().toString() );
IndexReader outOfDateReader = reader;
reader = replaceActiveReader( outOfDateReader, directoryProviderLock, directoryProvider, readers );
}
else {
if ( trace )
log.trace( "Valid shared IndexReader: " + directoryProvider.getDirectory().toString() );