Package org.sf.mustru.ui

Source Code of org.sf.mustru.ui.RunIndex

package org.sf.mustru.ui;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.sf.mustru.crawl.BuildIndex;

/**
* The thread to run the BuildIndex task
*
*/
public class RunIndex implements IRunnableWithProgress
{
  private boolean jobRan = true;
  private String[] args = null;
 
  public RunIndex(String[] args)
   { super(); this.args = new String[args.length];
     for (int i = 0; i < args.length; i++) this.args[i] = args[i];
   }
 
  public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
   {
   //*-- create a thread to build the index and start it.
   monitor.beginTask("Building Index.... please be patient...may take a while", 100);
   BuildIndex buildIndex = new BuildIndex();
   buildIndex.copyArgs(args);
   buildIndex.start();
   boolean done = false; long indexStart = new Date().getTime();
   int currentBarVal = 0
   double avgTimePerDoc = 0.0; double prevAvgTimePerDoc = 0.0;
   while (!done)
   {
    //*-- wait a second
    done = true;
    try { Thread.sleep(1000); catch (InterruptedException e) { }

    //*-- compute the bar increment for the initial scanning of index directories to build the task file
    if (buildIndex.isScanning())
    { int numSeconds = buildIndex.getTimeScanning();
      monitor.subTask("Scanning directories for files to index: " + numSeconds + " seconds elapsed.");
      if ( monitor.isCanceled())  
       { buildIndex.setRunning(false); done = true; monitor.done(); setJobRan(false); }
      else done = false;
    }
    //*-- check the progress of the indexing task
    else
    { int numFiles = buildIndex.getNumFiles(); int numFilesProcessed = buildIndex.getNumFilesProcessed();
      int increment = ( (int) Math.floor(numFilesProcessed * 100.0) / (numFiles + 1) )  - currentBarVal;
      monitor.worked(increment); currentBarVal += increment;

      //*-- display a progress message
      long elapsedTime = new Date().getTime() - indexStart;
      avgTimePerDoc = ( elapsedTime / (numFilesProcessed + 1.0) ) + prevAvgTimePerDoc / 2.0;
      long timeLeft = (long) ( avgTimePerDoc * (numFiles - numFilesProcessed) );
      int percentDone = (int) Math.floor(numFilesProcessed * 100.0 / numFiles);
      monitor.subTask( (percentDone < 100) ? "Finished indexing " + numFilesProcessed + " out of " + numFiles + " documents (" +
      percentDone + "%). Time left: " + formatTime(timeLeft): "Cleaning up......" );
      if (buildIndex.isRunning()) done = false;
   
      if ( monitor.isCanceled())  
       { buildIndex.setRunning(false); done = true; monitor.done(); setJobRan(false); }
     
      prevAvgTimePerDoc = avgTimePerDoc;
    } //*-- end of if
   
   } //*-- end of while

   monitor.done();
  }

  //*-- build a timestamp to show the milliseconds in hh:mm:ss format
  private String formatTime(long etime)
  { etime /= 1000;
  long hours = (long) Math.floor(etime / 3600.0);
  long rem = (etime % 3600);
  long mins = (long) Math.floor(rem / 60.0);
  long secs = (rem % 60);
  return  ( (hours < 10) ? "0" + hours: "" + hours) + ":" +
          ( (mins < 10) ? "0" + mins: "" + mins) + ":" +
          ( (secs < 10) ? "0" + secs: "" + secs);
  }

  public boolean isJobRan()
  { return jobRan; }

  public void setJobRan(boolean jobRan)
  { this.jobRan = jobRan; }

}
TOP

Related Classes of org.sf.mustru.ui.RunIndex

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.