FileDocCategorySizeDatePackage
IndexLogWriter.javaAPI DocApache Lucene 2.1.03699Wed Feb 14 10:46:06 GMT 2007org.apache.lucene.gdata.search.index

IndexLogWriter

public class IndexLogWriter extends Object
author
Simon Willnauer

Fields Summary
private static final String
LINE_BREAK
private static final String
XMLHEADER
private static final String
CHARSET
private static final String
ROOT_BEGIN
private static final String
ROOT_END
private final BufferedWriter
writer
private final AtomicBoolean
isClosed
Constructors Summary
public IndexLogWriter(File file)

param
file
throws
IOException


              
         

        this.writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(file), CHARSET));
        this.writer.write(XMLHEADER);
        this.writer.write(ROOT_BEGIN);
        this.writer.flush();
        this.isClosed = new AtomicBoolean(false);

    
Methods Summary
private static java.lang.StringbuildElement(java.lang.String id, java.lang.String action)

        StringBuilder builder = new StringBuilder("\t<indexentry>")
                .append(LINE_BREAK);
        builder.append("\t\t<entryid>");
        builder.append(id);
        builder.append("</entryid>").append(LINE_BREAK);
        builder.append("\t\t<action>");
        builder.append(action);
        builder.append("</action>").append(LINE_BREAK);
        builder.append("\t</indexentry>").append(LINE_BREAK);
        return builder.toString();

    
synchronized voidclose()

        if (!this.isClosed.compareAndSet(false,true))
            throw new IllegalStateException("Writer is already closed");
        try {
            this.writer.write(ROOT_END);
            this.writer.flush();
        } finally {
            this.writer.close();
        }

    
static synchronized voidtryCloseRoot(java.io.File file)

        /*
         * try to append the Root element end
         * this happens if the server crashes.
         * If it dies while writing an entry the log file has to be fixed manually
         */
        RandomAccessFile raFile = new RandomAccessFile(file, "rw");
        raFile.seek(raFile.length());
        raFile.write(IndexLogWriter.ROOT_END.getBytes(CHARSET));
        raFile.close();
        

    
synchronized voidwriteAction(java.lang.String id, IndexAction action)

        if (this.isClosed.get())
            throw new IllegalStateException("Writer is already closed");
        this.writer.write(buildElement(id, action.name()));
        this.writer.flush();