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() );
}
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() );
}
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()
);
}
IndexReader outOfDateReader = reader;
reader = replaceActiveReader( outOfDateReader, directoryProviderLock, directoryProvider, readers );
}
else {
if ( trace ) {
log.trace( "Valid shared IndexReader: {}" + directoryProvider.getDirectory() );
}
directoryProviderLock.lock();
try {
//read the latest active one, the current one could be out of date and closed already
//the latest active is guaranteed to be active because it's protected by the dp lock