KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > lucene > index > IndexInformation


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: IndexInformation.java 152969 2005-02-09 01:01:05Z gregor $ */
19
20 package org.apache.lenya.lucene.index;
21
22 import java.io.File JavaDoc;
23 import java.io.FileFilter JavaDoc;
24 import java.text.DateFormat JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26 import java.util.Calendar JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.GregorianCalendar JavaDoc;
29
30 import org.apache.log4j.Category;
31 import org.apache.lucene.index.IndexReader;
32 import org.apache.lucene.index.Term;
33
34 /**
35  * Helper class to hold indexing information
36  */

37 public class IndexInformation {
38     
39     private static Category log = Category.getInstance(IndexInformation.class);
40     
41     /**
42      * Creates a new IndexInformation object.
43      * @param index DOCUMENT ME!
44      * @param dumpDirectory DOCUMENT ME!
45      * @param filter DOCUMENT ME!
46      * @param create DOCUMENT ME!
47      */

48     public IndexInformation(String JavaDoc index, File JavaDoc dumpDirectory, FileFilter JavaDoc 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 JavaDoc();
55
56         log.info(this.length + " files to index");
57         //log.info(getFileNumber() + " files to index");
58
}
59
60     private String JavaDoc index;
61
62     protected String JavaDoc getIndex() {
63         return this.index;
64     }
65
66     private boolean creating;
67
68     protected boolean isCreating() {
69         return this.creating;
70     }
71
72     //private List files = new ArrayList();
73
private int length = 0;
74
75     /**
76      *
77      */

78     protected void addFile(File JavaDoc file) {
79         //files.add(file);
80
this.length++;
81     }
82
83     /**
84      * DOCUMENT ME!
85      *
86      * @return DOCUMENT ME!
87      */

88 /*
89     public File[] getFiles() {
90         Collections.sort(files);
91
92         return (File[]) files.toArray(new File[files.size()]);
93     }
94 */

95
96     private int currentFile = 0;
97
98     /**
99      * DOCUMENT ME!
100      */

101     public void increase() {
102         this.currentFile++;
103     }
104
105     /**
106      * DOCUMENT ME!
107      *
108      * @return DOCUMENT ME!
109      */

110     public int getCurrentFile() {
111         return currentFile;
112     }
113
114     /**
115      * Get number of files to index
116      *
117      * @return number of files to index
118      */

119 /*
120     public int getFileNumber() {
121         return files.size();
122     }
123 */

124
125     private Calendar JavaDoc startTime;
126
127     /**
128      * DOCUMENT ME!
129      *
130      * @return DOCUMENT ME!
131      */

132     public Calendar JavaDoc getStartTime() {
133         return this.startTime;
134     }
135
136     /**
137      * Generate string which tells about the indexing progress
138      *
139      * @return indexing progress
140      */

141     public String JavaDoc printProgress() {
142         double percent = (double) this.currentFile / (double) this.length;
143         //double percent = (double) currentFile / (double) getFileNumber();
144
DateFormat JavaDoc format = new SimpleDateFormat JavaDoc("HH:mm:ss");
145
146         //return "added document " + getCurrentFile() + " of " + getFileNumber() + " (" +
147
return "added document " + getCurrentFile() + " of " + this.length + " (" +
148         (int) (percent * 100) + "%" + ", remaining time: " +
149         format.format(getEstimatedTime().getTime()) + ")";
150     }
151
152     /**
153      *
154      */

155     protected Calendar JavaDoc getEstimatedTime() {
156         long elapsedMillis = new Date JavaDoc().getTime() - getStartTime().getTime().getTime();
157
158         double millisPerFile = (double) elapsedMillis / (double) this.currentFile;
159         long estimatedMillis = (long) (millisPerFile * this.length) - elapsedMillis;
160         //long estimatedMillis = (long) (millisPerFile * getFileNumber()) - elapsedMillis;
161

162         GregorianCalendar JavaDoc estimatedCalendar = new GregorianCalendar JavaDoc();
163         estimatedCalendar.setTimeInMillis(estimatedMillis);
164         estimatedCalendar.roll(Calendar.HOUR, false);
165
166         return estimatedCalendar;
167     }
168
169     /**
170      * Collect files
171      */

172     protected void collectFiles(File JavaDoc dumpDirectory, FileFilter JavaDoc filter, String JavaDoc _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     /**
187      * DOCUMENT ME!
188      */

189     public class CreateHandler extends AbstractIndexIteratorHandler {
190         /** Handles a file.
191          *
192          */

193         public void handleFile(IndexReader reader, File JavaDoc file) {
194             IndexInformation.this.addFile(file);
195         }
196     }
197
198     /**
199      * DOCUMENT ME!
200      */

201     public class UpdateHandler extends AbstractIndexIteratorHandler {
202         /** Handles a new document.
203          *
204          */

205         public void handleNewDocument(IndexReader reader, Term term, File JavaDoc file) {
206             IndexInformation.this.addFile(file);
207         }
208     }
209 }
210
Popular Tags