1
2
3
4
5 package net.sf.gumshoe.indexer;
6
7 import java.io.File;
8 import java.io.IOException;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11
12 import org.apache.lucene.index.IndexReader;
13 import org.apache.lucene.index.IndexWriter;
14 import org.apache.lucene.index.Term;
15 import org.apache.lucene.index.TermDocs;
16 import org.apache.lucene.store.Directory;
17 import org.apache.lucene.store.FSDirectory;
18 import org.apache.lucene.analysis.standard.StandardAnalyzer;
19 import org.apache.lucene.document.DateField;
20 import org.apache.lucene.document.Document;
21
22 /***
23 * @author Gabor
24 *
25 * TODO To change the template for this generated type comment go to
26 * Window - Preferences - Java - Code Style - Code Templates
27 */
28 public class DiskIndexer {
29 protected Logger logger=null;
30
31 public DiskIndexer(){
32 logger=Logger.getLogger(getClass().getName());
33 }
34
35 public void index(File indexDir, File dataDir, boolean recurse) throws IOException {
36 if (!dataDir.exists() || !dataDir.isDirectory()) {
37 throw new IOException(dataDir + " does not exist or is not a directory");
38 }
39
40 IndexWriter iw=null;
41 try {
42 iw = new IndexWriter(indexDir, new StandardAnalyzer(),
43 !IndexReader.indexExists(indexDir));
44 iw.close();
45 } catch (IOException ioe){
46 Directory fsDir = FSDirectory.getDirectory(indexDir, !IndexReader.indexExists(indexDir));
47 IndexReader.unlock(fsDir);
48 }
49 indexDirectory(indexDir, dataDir, recurse);
50 }
51
52 private void indexDirectory(File indexDir, File dir, boolean recurse) throws IOException {
53 if (indexDir.equals(dir)){
54 return;
55 }
56 File[] files = dir.listFiles();
57
58 for (int i=0; i < files.length; i++) {
59 File f = files[i];
60 if (f.isDirectory()) {
61 if (recurse){
62 indexDirectory(indexDir, f, recurse);
63 }
64 } else {
65 try {
66 indexFile(indexDir, f);
67 } catch (Throwable t){
68 logger.log(Level.SEVERE, "indexing failed for "+f.getCanonicalPath(), t);
69 }
70 }
71 }
72 }
73
74 private void indexFile(File indexDir, File f) throws Exception {
75 IndexReader ir = null;
76 try {
77 Directory fsDir = FSDirectory.getDirectory(indexDir, !IndexReader.indexExists(indexDir));
78 ir=IndexReader.open(fsDir);
79
80 TermDocs td = ir.termDocs(new Term(ContentReader.FILENAME, f.getCanonicalPath()));
81 boolean found=td.next();
82 if (found){
83 Document doc=ir.document(td.doc());
84 long prevModified=DateField.stringToTime(doc.get(ContentReader.MODIFIED));
85 if (f.lastModified()==prevModified){
86 logger.fine("match found for "+f.getCanonicalPath());
87 return;
88 } else {
89 ir.delete(td.doc());
90 }
91 }
92 } finally {
93 if (ir!=null){
94 ir.close();
95 }
96 }
97
98 IndexWriter iw = null;
99 try {
100 Document d=ContentReaderFactory.getInstance().getDocument(f);
101 iw=new IndexWriter(indexDir, new StandardAnalyzer(),
102 !IndexReader.indexExists(indexDir));
103 iw.addDocument(d);
104 logger.fine("indexed "+f.getCanonicalPath());
105 } finally {
106 if (iw!=null){
107 iw.close();
108 }
109 }
110 }
111 }