FileDocCategorySizeDatePackage
RecoverController.javaAPI DocApache Lucene 2.1.07074Wed Feb 14 10:46:04 GMT 2007org.apache.lucene.gdata.storage.lucenestorage.recover

RecoverController

public class RecoverController extends Object
author
Simon Willnauer

Fields Summary
private static final Log
LOG
private final File
recoverDirectory
private static final String
FILE_SUFFIX
private File
currentRecoverFile
private RecoverWriter
writer
private Writer
fileWriter
private BufferedReader
fileReader
private RecoverReader
reader
private Lock
lock
private final boolean
recover
private final boolean
keepRecoverFiles
Constructors Summary
public RecoverController(File recoverDirectory, boolean recover, boolean keepRecoverFiles)

param
recoverDirectory
param
recover
param
keepRecoverFiles


               
            
        if (recoverDirectory == null)
            throw new IllegalArgumentException("directory must not be null");
        if(!recoverDirectory.exists())
            recoverDirectory.mkdirs();
        if (!recoverDirectory.isDirectory())
            throw new IllegalStateException("the given File is not a directory -- "+recoverDirectory);
        this.recover = recover;
        this.keepRecoverFiles = keepRecoverFiles;
        this.recoverDirectory = recoverDirectory;
       
    
Methods Summary
public synchronized voiddestroy()

throws
RecoverException

        if (this.fileWriter != null) {
            this.lock.lock();
            try {
                
                this.fileWriter.flush();
                this.fileWriter.close();
                if(!this.keepRecoverFiles && this.currentRecoverFile != null)
                    this.currentRecoverFile.delete();
            } catch (IOException e) {
                throw new RecoverException("Can't close recover writer ", e);
            } finally {
                this.lock.unlock();
            }
        }
    
public synchronized voidinitialize()

throws
IOException

        if(this.recover)
            return;
        String filename = System.currentTimeMillis() + FILE_SUFFIX;
        this.currentRecoverFile = new File(this.recoverDirectory, filename);
        this.writer = new RecoverWriter();
        this.fileWriter = new BufferedWriter(new FileWriter(
                this.currentRecoverFile));

    
public booleanisRecovering()

return
true if the RecoverController is initialized in recover mode, otherwise false

        
        return this.recover;
    
public voidrecoverEntries(org.apache.lucene.gdata.storage.lucenestorage.StorageModifier modifier)

param
modifier

        // prevent deadlock either recovering or writing
        if(!this.recover){
            LOG.warn("Can't recover entries, Recovercontroller is initialized in write mode");
            return;
        }
        this.lock.lock();
        try{
            this.reader = new RecoverReader();
        File[] files = this.recoverDirectory.listFiles();
        for (int i = 0; i < files.length; i++) {
            if(!files[i].isDirectory()){
                try{
                LOG.info("Recover file -- "+files[i]);
                this.fileReader = new BufferedReader(new FileReader(files[i]));
                List<StorageEntryWrapper> entryList = this.reader.recoverEntries(this.fileReader);
                if(entryList.size() == 0)
                    continue;
                storeEntries(entryList,modifier);
                this.fileReader.close();
                if(!this.keepRecoverFiles){
                    LOG.info("Recovering file -- "+files[i]+" successful, delete file");
                    files[i].delete();
                }
                }catch (StorageException e) {
                    LOG.error("Can't store recover entries for file: "+files[i].getName()+" -- keep file "+e.getMessage(),e);
                }catch (IOException e) {
                    LOG.error("Can't recover entries for file: "+files[i].getName()+" -- keep file",e);
                }
            }
        }
        
        }finally{
            this.lock.unlock();
        }
    
public voidstorageModified(org.apache.lucene.gdata.storage.lucenestorage.StorageEntryWrapper wrapper)

param
wrapper
throws
RecoverException

        // prevent deadlock either recovering or writing
        if(this.recover){
            LOG.warn("Can't write entry, Recovercontroller is initialized in recover mode");
            return;
        }
        this.lock.lock();
        try {

            this.writer.writeEntry(wrapper, this.fileWriter);
        } catch (Exception e) {
            LOG.error("Writing entry failed -- create new recover file",e);
            throw new RecoverException(
                    "Writing entry failed -- create new recover file",e);

        } finally {
            this.lock.unlock();
        }
    
protected voidstoreEntries(java.util.List entries, org.apache.lucene.gdata.storage.lucenestorage.StorageModifier modifier)

        for (StorageEntryWrapper wrapper : entries) {
            if(wrapper.getOperation() == StorageOperation.DELETE)
                modifier.deleteEntry(wrapper);
            else if(wrapper.getOperation() == StorageOperation.INSERT)
                modifier.insertEntry(wrapper);
            else if(wrapper.getOperation() == StorageOperation.UPDATE)
                modifier.updateEntry(wrapper);
            modifier.forceWrite();
          
        }