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

IndexTask

public class IndexTask extends Object implements Runnable
Class to be used inside a {@link org.apache.lucene.gdata.search.index.GDataIndexer} to process the task queue. This class calls the commit method of the indexer if commit is scheduled.
author
Simon Willnauer

Fields Summary
private static final Log
INNERLOG
private AtomicBoolean
stopped
private final GDataIndexer
indexer
protected AtomicBoolean
commit
protected AtomicBoolean
optimize
protected final BlockingQueue
taskQueue
Constructors Summary
IndexTask(GDataIndexer indexer, BlockingQueue taskQueue)


      
               
        this.indexer = indexer;
        this.taskQueue = taskQueue;
    
Methods Summary
protected java.util.concurrent.FuturegetTask()

        return this.taskQueue.take();
    
protected booleanisStopped()

        return this.stopped.get();
    
private voidprocessDocument(IndexDocument document)

        /*
         * a null document is used for waking up the task if the indexer has
         * been destroyed to finish up and commit. should I change this?! -->
         * see TimedIndexTask#getTask() also!!
         */
        if (document == null) {
            INNERLOG.warn("Can not process document -- is null -- run commit");
            return;
        }
        if (document.isDelete()) {
            this.indexer.deleteDocument(document);
            return;
        } else if (document.isInsert()) {
            this.indexer.addDocument(document);
            return;
        } else if (document.isUpdate()) {
            this.indexer.updateDocument(document);
            return;
        }
        /*
         * that should not happen -- anyway skip the document and write it to
         * the log
         */
        INNERLOG.warn("IndexDocument has no Action " + document);

    
public voidrun()

see
java.lang.Runnable#run()


        while (!this.stopped.get() || this.taskQueue.size() != 0) {

            try {
                /*
                 * get the future from the queue and wait until processing has
                 * been done
                 */
                Future<IndexDocument> future = getTask();
                if (future != null) {
                    IndexDocument document = future.get();
                    setOptimize(document);
                    processDocument(document); 
                    /*
                     * the document contains the info for commit or optimize -->
                     * this comes from the controller
                     */
                    if (document == null || document.commitAfter())
                        this.indexer.commit(document == null ? false : this.optimize.getAndSet(false));
                }
                if (this.commit.getAndSet(false))
                    this.indexer.commit(this.optimize.getAndSet(false));

            } catch (InterruptedException e) {
                INNERLOG.warn("Queue is interrupted exiting IndexTask -- ", e);

            } catch (GdataIndexerException e) {
                /*
                 * 
                 * TODO fire callback here as well
                 */
                INNERLOG.error("can not retrieve Field from IndexDocument  ", e);
            } catch (ExecutionException e) {
                /*
                 * TODO callback for fail this exception is caused by an
                 * exception while processing the document. call back for failed
                 * docs should be placed here
                 */
                INNERLOG.error("Future throws execution exception ", e);

            } catch (IOException e) {
                INNERLOG.error("IOException thrown while processing document ",
                        e);

            } catch (Throwable e) {
                /*
                 * catch all to prevent the thread from dieing
                 */
                INNERLOG.error(
                        "Unexpected exception while processing document -- "
                                + e.getMessage(), e);
            }
        }
        try {
            this.indexer.commit(this.optimize.getAndSet(false));
        } catch (IOException e) {
            INNERLOG.warn("commit on going down failed - "+e.getMessage(),e);
            
        }
        this.stop();
    
protected voidsetOptimize(IndexDocument document)

        if(document == null)
            return;
        this.optimize.set(document.optimizeAfter());
    
protected voidstop()

        this.stopped.set(true);