Clover coverage report -
Coverage timestamp: Fri Nov 19 2004 13:41:51 PST
file stats: LOC: 127   Methods: 6
NCLOC: 106   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Indexer.java 92.9% 95.8% 100% 95.6%
coverage coverage
 1    package photospace.search;
 2   
 3    import java.io.*;
 4    import java.util.*;
 5    import org.apache.commons.logging.*;
 6    import org.apache.lucene.index.*;
 7    import photospace.meta.*;
 8   
 9    public class Indexer
 10    implements Serializable
 11    {
 12    private static final Log log = LogFactory.getLog(Indexer.class);
 13   
 14    private SearchIndex index;
 15    private DocumentFactory factory = new DocumentFactory();
 16   
 17  1 public void init() throws IOException
 18    {
 19  1 try
 20    {
 21  1 index.getReader();
 22    }
 23    catch (Exception e)
 24    {
 25  1 log.info("Initializing search index.");
 26  1 index(new ArrayList(), true);
 27    }
 28    }
 29   
 30  15 public int index(Collection docs, boolean fromScratch) throws IOException
 31    {
 32  15 Date start = new Date();
 33   
 34  15 Set metas = new HashSet(docs);
 35   
 36  15 File destination;
 37  14 if (fromScratch) destination = new File(System.getProperty("java.io.tmpdir"), "photospace-index");
 38  1 else destination = index.getIndex();
 39   
 40  0 if (log.isDebugEnabled()) log.debug("Writing index to " + destination);
 41   
 42  14 if (!destination.exists()) destination.mkdirs();
 43   
 44  15 IndexWriter writer = new IndexWriter(destination, new MetaAnalyzer(), fromScratch);
 45   
 46  15 int initialDocs = writer.docCount();
 47   
 48  15 try
 49    {
 50  15 writer.mergeFactor = 1000;
 51  15 writer.maxMergeDocs = 10000;
 52  15 writer.setUseCompoundFile(true);
 53   
 54  15 addDocuments((Meta[]) metas.toArray(new Meta[] {}), writer);
 55   
 56  15 writer.optimize();
 57    }
 58    finally
 59    {
 60  15 writer.close();
 61    }
 62   
 63  15 index.closeReader();
 64  14 if (fromScratch) index.update(destination);
 65   
 66  15 int indexedDocs = writer.docCount() - initialDocs;
 67  15 log.info("Indexed " + indexedDocs + " documents in " + (new Date().getTime() - start.getTime()) + " milliseconds");
 68  15 return indexedDocs;
 69    }
 70   
 71  25 private void addDocuments(Meta[] metas, IndexWriter writer)
 72    throws IOException
 73    {
 74  25 for (int i = 0; i < metas.length; i++)
 75    {
 76  43 Meta meta = metas[i];
 77  43 try
 78    {
 79  43 writer.addDocument(factory.createDocument(meta));
 80    }
 81    catch (Exception e)
 82    {
 83  0 log.error("Exception creating document for " + meta, e);
 84    }
 85  43 if (meta instanceof FolderMeta)
 86    {
 87  10 addDocuments(((FolderMeta) meta).getFiles(), writer);
 88    }
 89    }
 90    }
 91   
 92  1 public void merge(Collection metas)
 93    throws Exception
 94    {
 95  1 delete(metas);
 96  1 index(metas, false);
 97    }
 98   
 99  1 public void delete(Collection metas)
 100    throws IOException
 101    {
 102  1 Date start = new Date();
 103  1 synchronized (index)
 104    {
 105  1 IndexReader reader = index.getReader();
 106  1 try
 107    {
 108  1 for (Iterator i = metas.iterator(); i.hasNext();)
 109    {
 110  1 Meta meta = (Meta) i.next();
 111  1 reader.delete(new Term("path", meta.getPath()));
 112    }
 113    }
 114    finally
 115    {
 116  1 index.closeReader();
 117    }
 118    }
 119  1 log.info("Deleted " + metas.size() + " documents in " + (new Date().getTime() - start.getTime()) + " milliseconds");
 120    }
 121   
 122  13 public void setIndex(SearchIndex index)
 123    {
 124  13 this.index = index;
 125    }
 126   
 127    }