1 17 18 19 20 package org.apache.lenya.lucene.index; 21 22 import java.io.File ; 23 import java.io.FileFilter ; 24 import java.text.DateFormat ; 25 import java.text.SimpleDateFormat ; 26 import java.util.Calendar ; 27 import java.util.Date ; 28 import java.util.GregorianCalendar ; 29 30 import org.apache.log4j.Category; 31 import org.apache.lucene.index.IndexReader; 32 import org.apache.lucene.index.Term; 33 34 37 public class IndexInformation { 38 39 private static Category log = Category.getInstance(IndexInformation.class); 40 41 48 public IndexInformation(String index, File dumpDirectory, FileFilter filter, boolean create) { 49 log.info("Collecting index information for index '" + index + "'..."); 50 51 this.creating = create; 52 this.index = index; 53 collectFiles(dumpDirectory, filter, index); 54 this.startTime = new GregorianCalendar (); 55 56 log.info(this.length + " files to index"); 57 } 59 60 private String index; 61 62 protected String getIndex() { 63 return this.index; 64 } 65 66 private boolean creating; 67 68 protected boolean isCreating() { 69 return this.creating; 70 } 71 72 private int length = 0; 74 75 78 protected void addFile(File file) { 79 this.length++; 81 } 82 83 88 95 96 private int currentFile = 0; 97 98 101 public void increase() { 102 this.currentFile++; 103 } 104 105 110 public int getCurrentFile() { 111 return currentFile; 112 } 113 114 119 124 125 private Calendar startTime; 126 127 132 public Calendar getStartTime() { 133 return this.startTime; 134 } 135 136 141 public String printProgress() { 142 double percent = (double) this.currentFile / (double) this.length; 143 DateFormat format = new SimpleDateFormat ("HH:mm:ss"); 145 146 return "added document " + getCurrentFile() + " of " + this.length + " (" + 148 (int) (percent * 100) + "%" + ", remaining time: " + 149 format.format(getEstimatedTime().getTime()) + ")"; 150 } 151 152 155 protected Calendar getEstimatedTime() { 156 long elapsedMillis = new Date ().getTime() - getStartTime().getTime().getTime(); 157 158 double millisPerFile = (double) elapsedMillis / (double) this.currentFile; 159 long estimatedMillis = (long) (millisPerFile * this.length) - elapsedMillis; 160 162 GregorianCalendar estimatedCalendar = new GregorianCalendar (); 163 estimatedCalendar.setTimeInMillis(estimatedMillis); 164 estimatedCalendar.roll(Calendar.HOUR, false); 165 166 return estimatedCalendar; 167 } 168 169 172 protected void collectFiles(File dumpDirectory, FileFilter filter, String _index) { 173 IndexIterator iterator = new IndexIterator(_index, filter); 174 IndexIteratorHandler handler; 175 176 if (isCreating()) { 177 handler = new CreateHandler(); 178 } else { 179 handler = new UpdateHandler(); 180 } 181 182 iterator.addHandler(handler); 183 iterator.iterate(dumpDirectory); 184 } 185 186 189 public class CreateHandler extends AbstractIndexIteratorHandler { 190 193 public void handleFile(IndexReader reader, File file) { 194 IndexInformation.this.addFile(file); 195 } 196 } 197 198 201 public class UpdateHandler extends AbstractIndexIteratorHandler { 202 205 public void handleNewDocument(IndexReader reader, Term term, File file) { 206 IndexInformation.this.addFile(file); 207 } 208 } 209 } 210 | Popular Tags |