public void start() {
int readCurrentState = current; //Unneeded value, but ensure visibility of state protected by memory barrier
int currentToBe = 0;
try {
directory1 = DirectoryProviderHelper.createFSIndex( new File( indexDir, "1" ) );
directory2 = DirectoryProviderHelper.createFSIndex( new File( indexDir, "2" ) );
File currentMarker = new File( indexDir, "current1" );
File current2Marker = new File( indexDir, "current2" );
if ( currentMarker.exists() ) {
currentToBe = 1;
if ( current2Marker.exists() ) {
current2Marker.delete(); //TODO or throw an exception?
}
}
else if ( current2Marker.exists() ) {
currentToBe = 2;
}
else {
//no default
log.debug( "Setting directory 1 as current" );
currentToBe = 1;
File destinationFile = new File( indexDir, Integer.valueOf( readCurrentState ).toString() );
int sourceCurrent;
if ( new File( sourceIndexDir, "current1" ).exists() ) {
sourceCurrent = 1;
}
else if ( new File( sourceIndexDir, "current2" ).exists() ) {
sourceCurrent = 2;
}
else {
throw new AssertionFailure( "No current file marker found in source directory: " + sourceIndexDir.getPath() );
}
try {
FileHelper.synchronize(
new File( sourceIndexDir, String.valueOf( sourceCurrent ) ),
destinationFile, true, copyChunkSize
);
}
catch ( IOException e ) {
throw new SearchException( "Unable to synchronize directory: " + indexName, e );
}
if ( !currentMarker.createNewFile() ) {
throw new SearchException( "Unable to create the directory marker file: " + indexName );
}
}
log.debug( "Current directory: {}", currentToBe );
}
catch ( IOException e ) {
throw new SearchException( "Unable to initialize index: " + directoryProviderName, e );
}
TimerTask task = new TriggerTask( sourceIndexDir, indexDir );
long period = DirectoryProviderHelper.getRefreshPeriod( properties, directoryProviderName );
timer.scheduleAtFixedRate( task, period, period );
this.current = currentToBe;
}
|