1 package com.atlassian.bonnie.index;
2
3 import com.atlassian.bonnie.LuceneConnection;
4 import com.atlassian.bonnie.LuceneException;
5 import org.apache.lucene.document.Document;
6 import org.apache.lucene.index.IndexWriter;
7
8 import java.io.IOException;
9
10
11
12
13 public class TempDirectoryDocumentWritingScheme extends BaseDocumentWritingScheme
14 {
15 protected final TempIndexWriter tempIndexWriter;
16
17 public TempDirectoryDocumentWritingScheme(BatchOpIndexer indexer)
18 {
19 this(indexer, null);
20 }
21
22 public TempDirectoryDocumentWritingScheme(BatchOpIndexer indexer, String tmpDir)
23 {
24 this.tempIndexWriter = new TempIndexWriter(indexer.getAnalyzer(), tmpDir);
25 }
26
27 public void write(final Document doc)
28 {
29 if (doc == null)
30 {
31 progress.incrementCounter();
32 return;
33 }
34
35 String key = getWriterKey(doc);
36 try
37 {
38 tempIndexWriter.addDocument(key, doc);
39 progress.incrementCounter("Indexed: " + getDocumentTitle(doc) + " - " + progress.progressAsString());
40 }
41 catch (IOException e)
42 {
43 progress.incrementCounter("Error indexing: " + getDocumentTitle(doc) + " (" + e.toString() + ") - " + progress.progressAsString());
44 BaseMultiThreadedIndexer.log.error("Error encountered", e);
45 }
46 }
47
48 protected String getWriterKey(Document doc)
49 {
50 return Thread.currentThread().getName();
51 }
52
53 protected String getDocumentTitle(Document doc)
54 {
55 return doc.get("title");
56 }
57
58 public void runComplete()
59 {
60
61 }
62
63
64
65
66
67
68
69 public void close(LuceneConnection luceneConnection) throws IOException
70 {
71 luceneConnection.withWriter(new LuceneConnection.WriterAction()
72 {
73 public void perform(IndexWriter writer) throws IOException
74 {
75 tempIndexWriter.merge(writer);
76 }
77 }, LuceneConnection.WRITER_BATCH);
78 tempIndexWriter.closeAll();
79 luceneConnection.close();
80 }
81 }