|                                                                                                              1
 5   package org.enhydra.snapper.wrapper.lucene;
 6
 7   import java.io.File
  ; 8   import java.io.IOException
  ; 9   import java.util.Iterator
  ; 10  import java.util.Map
  ; 11  import java.util.TreeSet
  ; 12  import java.util.Vector
  ; 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
 25
 26
 27  public class LuceneReader implements Reader {
 28      IndexReader reader;
 29      FilterIndexReader freader;
 30      String
  dir; 31
 32
 39
 40
 41
 44      public void setUpReader(String
  dir) throws IOException  { 45          this.dir = dir;
 46          reader = IndexReader.open(dir);
 47              }
 49
 50
 53      public boolean fileExists(File
  fileToCheck) throws Exception  { 54          String
  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
  path) throws Exception  { 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
 81      public boolean checkLastModified(File
  fileToCheck) throws Exception  { 82
 83          long indexMod = IndexReader.lastModified(reader.directory());
 84          if (fileToCheck.lastModified() > indexMod){
 85              String
  path = fileToCheck.getPath(); 86              path = replace(path, "\\",".");
 87              path = replace(path, "/",".");
 88              if (path.startsWith("ftp"))
 89                  path = path.replaceFirst(":.", "://");
 90
 92              reader.delete(new Term("pt", path));
 93                          return true;
 95
 96          }
 97          return false;
 98      }
 99
 100     public boolean checkLastModified(String
  path, long modified) throws Exception  { 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
  fileToCheck, long timestamp) throws Exception  { 115
 116         long indexMod = IndexReader.lastModified(reader.directory());
 117         if (timestamp > indexMod){
 118             String
  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
 139     public void deleteFile(File
  file) throws Exception  { 140
 141     }
 142
 143
 146     public void closeReader() throws Exception
  { 147         reader.close();
 148
 149     }
 150
 151     String
  replace(String  s, String  one, String  another) { 152        if (s.equals("")) return "";
 154        String
  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);         return res;
 164      }
 165
 166     public void checkDeleted(TreeSet
  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
  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
  e) { 190             LuceneIndexerFactory.logger.error("Could not get last modified");
 191             return 0;
 192         }
 193
 194
 195     }
 196
 197     public void removeDocuments(Map
  data) throws Exception  { 198
 199         for (Iterator
  iterator = data.entrySet().iterator(); iterator.hasNext();){ 200             Map.Entry
  entry = (Map.Entry  ) iterator.next(); 201
 202             try{
 203                 String
  path = (String  )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
  ex) { 213                 ex.printStackTrace();
 214             }
 215         }
 216     }
 217
 218
 219 }
 220
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |