1 package com.atlassian.bonnie;
2
3 import java.io.IOException;
4
5 import org.apache.lucene.document.Document;
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.index.IndexWriter;
8 import org.apache.lucene.search.IndexSearcher;
9
10 public interface ILuceneConnection
11 {
12 public interface SearcherAction
13 {
14 /**
15 * Perform search.
16 *
17 * @param searcher
18 * @return true if searcher should be closed when action completes,
19 * false otherwise
20 * @throws IOException
21 */
22 public boolean perform(IndexSearcher searcher) throws IOException;
23 }
24
25 public interface ReaderAction
26 {
27 /**
28 * Perform index reading.
29 *
30 * @param reader
31 * @return result
32 * @throws IOException
33 */
34 public Object perform(IndexReader reader) throws IOException;
35 }
36
37 public interface WriterAction
38 {
39 /**
40 * Perform index writing.
41 *
42 * @param writer
43 * @throws IOException
44 */
45 public void perform(IndexWriter writer) throws IOException;
46 }
47
48 public interface BatchUpdateAction
49 {
50 /**
51 * Perform batch update operation. Use this action for multi-step
52 * operations where you want to acquire a write lock for the duration of
53 * the operation.
54 *
55 * @throws Exception
56 */
57 public void perform() throws Exception;
58 }
59
60 void withSearch(SearcherAction action) throws LuceneException;
61
62 /**
63 * Idempotent operation. Just for querying, do not delete documents with
64 * this action use {@link #withReaderAndDeletes(ReaderAction)} to perform
65 * index deletes.
66 */
67 Object withReader(ReaderAction action) throws LuceneException;
68
69 /**
70 * Delete stuff in the index.
71 */
72 void withReaderAndDeletes(ReaderAction action) throws LuceneException;
73
74 /**
75 * Add (write) documents to the index
76 */
77 void withWriter(WriterAction action) throws LuceneException;
78
79 /**
80 * Perform an Atomic delete and add. Shouldn't perform too many writes, for multiple writes use
81 * {@link #withBatchUpdate(com.atlassian.bonnie.ILuceneConnection.BatchUpdateAction)} as
82 * it will use the batch mode configuration, speeding up the writes.
83 */
84 void withDeleteAndWrites(ReaderAction readerAction, WriterAction action) throws LuceneException;
85
86 /**
87 * Perform multiple writes to the index. Holds a writeLock on the index for
88 * the whole time, and will use the {@link Configuration batch configuration}
89 * settings.
90 */
91 void withBatchUpdate(BatchUpdateAction action);
92
93 /**
94 * Perform an optimize on the index. Holds a writeLock and can take a long
95 * time (depending on the size of the index, how much optimization needs to
96 * be done... and whether a virus-checker is installed :) ).
97 */
98 void optimize() throws LuceneException;
99
100 /**
101 * Closes the reader and the writer.
102 */
103 void close();
104
105 /**
106 * Returns the number of {@link Document documents} in the index.
107 */
108 int getNumDocs();
109
110 /**
111 * Returns true if the index has been created. This means that the index
112 * directory itself exists AND has been initialised with the default
113 * required index files.
114 *
115 * @return true if the index exists, false otherwise.
116 */
117 boolean isIndexCreated();
118
119 /**
120 * Create or recreate the underlying directory the index is contained in.
121 * Blow away any indexes that currently live there.
122 */
123 void recreateIndexDirectory();
124
125 /**
126 * Get the current Searcher from the ILuceneConnection.
127 * <p/>
128 * Do not forget to call the Searcher's close() method once the searcher is no longer neeeded.
129 */
130 IndexSearcher leakSearcher();
131
132 /**
133 * Provide defaults for a Lucene IndexWriter. This is an alternative to the
134 * FLAGS passed to the
135 * {@link LuceneConnection#configureWriter(IndexWriter, int)} method.
136 */
137 public interface Configuration
138 {
139 int getInteractiveMergeFactor();
140 int getInteractiveMaxMergeDocs();
141 int getInteractiveMaxBufferedDocs();
142 int getBatchMergeFactor();
143 int getBatchMaxMergeDocs();
144 int getBatchMaxBufferedDocs();
145 int getMaxFieldLength();
146 boolean isCompoundIndexFileFormat();
147 }
148
149 /**
150 * The Bonnie default configuration. Preserves backward compatability for old Confluence settings.
151 */
152 static final Configuration DEFAULT_CONFIGURATION = new Configuration()
153 {
154 public int getBatchMergeFactor()
155 {
156 return 50;
157 }
158
159 public int getBatchMaxMergeDocs()
160 {
161 return Integer.MAX_VALUE;
162 }
163
164 public int getBatchMaxBufferedDocs()
165 {
166 return 300;
167 }
168
169 public int getInteractiveMergeFactor()
170 {
171 return 4;
172 }
173
174 public int getInteractiveMaxMergeDocs()
175 {
176 return 5000;
177 }
178
179 public int getInteractiveMaxBufferedDocs()
180 {
181 return 300;
182 }
183
184 /**
185 * 1million (the lucene default is 10,000).
186 * at (say) 10chars per token, that is a 10meg limit. Fair enough.
187 * [from Kelvin Tan]
188 */
189 public int getMaxFieldLength()
190 {
191 return 1000000;
192 }
193
194 public boolean isCompoundIndexFileFormat()
195 {
196 return true;
197 }
198 };
199 }