KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > wrapper > lucene > LuceneReader


1 /*
2  * Created on Mar 22, 2005
3  *
4  */

5 package org.enhydra.snapper.wrapper.lucene;
6
7 import java.io.File JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.TreeSet JavaDoc;
12 import java.util.Vector JavaDoc;
13
14
15
16 import org.apache.lucene.index.FilterIndexReader;
17 import org.apache.lucene.index.IndexReader;
18 import org.apache.lucene.index.Term;
19 import org.enhydra.snapper.api.Reader;
20
21 /**
22  * @author Igor Smirnov
23  *
24  */

25
26
27 public class LuceneReader implements Reader {
28     IndexReader reader;
29     FilterIndexReader freader;
30     String JavaDoc dir;
31     
32     /*public void delete(int docNum) throws IOException{
33         reader.delete(docNum);
34     }
35     
36     public Document document(int n) throws IOException{
37         org.enhydra.snapper.wrapper.lucene.Document doc = reader.document(n);
38     }*/

39     
40     
41     /* (non-Javadoc)
42      * @see org.enhydra.snapper.api.Reader#setUpReader(java.lang.String)
43      */

44     public void setUpReader(String JavaDoc dir) throws IOException JavaDoc {
45         this.dir = dir;
46         reader = IndexReader.open(dir);
47         //freader = new FilterIndexReader(reader);
48
}
49
50     /* (non-Javadoc)
51      * @see org.enhydra.snapper.api.Reader#fileExists(java.io.File)
52      */

53     public boolean fileExists(File JavaDoc fileToCheck) throws Exception JavaDoc {
54         String JavaDoc str = fileToCheck.getPath();
55         str = replace(str, "\\",".");
56         str = replace(str, "/",".");
57         if (str.startsWith("ftp"))
58             str = str.replaceFirst(":.", "://");
59
60         if (reader.docFreq(new Term("pt", str)) > 0)
61             return true;
62         else
63             return false;
64     }
65     
66     public boolean fileExists(String JavaDoc path) throws Exception JavaDoc {
67         
68         path = replace(path, "\\",".");
69         path = replace(path, "/",".");
70         if (path.startsWith("ftp"))
71             path = path.replaceFirst(":..", "://");
72         if (reader.docFreq(new Term("pt", path)) > 0)
73             return true;
74         else
75             return false;
76     }
77
78     /* (non-Javadoc)
79      * @see org.enhydra.snapper.api.Reader#checkLastModified(java.io.File)
80      */

81     public boolean checkLastModified(File JavaDoc fileToCheck) throws Exception JavaDoc {
82         
83         long indexMod = IndexReader.lastModified(reader.directory());
84         if (fileToCheck.lastModified() > indexMod){
85             String JavaDoc path = fileToCheck.getPath();
86             path = replace(path, "\\",".");
87             path = replace(path, "/",".");
88             if (path.startsWith("ftp"))
89                 path = path.replaceFirst(":.", "://");
90             //LuceneIndexerFactory.logger.debug("1: " + reader.docFreq(new Term("pt", path)));
91

92             reader.delete(new Term("pt", path));
93             //LuceneIndexerFactory.logger.debug("2: " + reader.docFreq(new Term("pt", path)));
94
return true;
95             
96         }
97         return false;
98     }
99     
100     public boolean checkLastModified(String JavaDoc path, long modified) throws Exception JavaDoc {
101         
102         long indexMod = IndexReader.lastModified(reader.directory());
103         if (modified > indexMod){
104             path = replace(path, "\\",".");
105             path = replace(path, "/",".");
106             reader.delete(new Term("pt", path));
107             return true;
108             
109         }
110         return false;
111     }
112     
113     
114     public boolean checkLastModified(File JavaDoc fileToCheck, long timestamp) throws Exception JavaDoc {
115         
116         long indexMod = IndexReader.lastModified(reader.directory());
117         if (timestamp > indexMod){
118             String JavaDoc str = fileToCheck.getPath();
119             str = replace(str, "\\",".");
120             str = replace(str, "/",".");
121             if (str.startsWith("ftp")){
122                 str.replaceFirst(":.", "://");
123             }
124             reader.delete(new Term("pt", str));
125             return true;
126             
127         }
128         return false;
129     }
130     
131     
132     
133     
134     
135
136     /* (non-Javadoc)
137      * @see org.enhydra.snapper.api.Reader#deleteFile(java.io.File)
138      */

139     public void deleteFile(File JavaDoc file) throws Exception JavaDoc {
140
141     }
142
143     /* (non-Javadoc)
144      * @see org.enhydra.snapper.api.Reader#closeReader()
145      */

146     public void closeReader() throws Exception JavaDoc {
147         reader.close();
148
149     }
150     
151     String JavaDoc replace(String JavaDoc s, String JavaDoc one, String JavaDoc another) {
152 // In a string replace one substring with another
153
if (s.equals("")) return "";
154        String JavaDoc res = "";
155        int i = s.indexOf(one,0);
156        int lastpos = 0;
157        while (i != -1) {
158          res += s.substring(lastpos,i) + another;
159          lastpos = i + one.length();
160          i = s.indexOf(one,lastpos);
161        }
162        res += s.substring(lastpos); // the rest
163
return res;
164      }
165     
166     public void checkDeleted(TreeSet JavaDoc filesToCheck){
167         for (int i=0; i<reader.maxDoc(); i++){
168             try{
169                 if (!filesToCheck.contains(reader.document(i).getField("path").stringValue()))
170                 {
171                 reader.delete(i);
172     
173                 
174                 }
175             }
176             catch (Exception JavaDoc e) {
177                 LuceneIndexerFactory.logger.error("Could not delete from Indexer");
178             }
179         }
180     }
181     
182     public int getSize(){
183         return reader.maxDoc();
184     }
185     
186     public long lastModified(){
187         try{
188             return IndexReader.lastModified(reader.directory());
189         } catch (Exception JavaDoc e) {
190             LuceneIndexerFactory.logger.error("Could not get last modified");
191             return 0;
192         }
193         
194         
195     }
196     
197     public void removeDocuments(Map JavaDoc data) throws Exception JavaDoc {
198
199         for (Iterator JavaDoc iterator = data.entrySet().iterator(); iterator.hasNext();){
200             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
201             
202             try{
203                 String JavaDoc path = (String JavaDoc)entry.getKey();
204                 path = replace(path, "\\",".");
205                 path = replace(path, "/",".");
206                 if (path.startsWith("ftp")){
207                     path.replaceFirst(":.", "://");
208                 }
209                 reader.delete(new Term("pt", path));
210                 
211             }
212             catch (Exception JavaDoc ex) {
213                 ex.printStackTrace();
214             }
215         }
216     }
217
218
219 }
220
Popular Tags