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  /**
11   * A connection to a Lucene index. Supports reads, writes, searches, batch updates, and truncates.
12   */
13  public interface ILuceneConnection
14  {
15      public interface SearcherAction
16      {
17          /**
18           * Perform search. The searcher is managed which means you must not
19           * close the searcher.
20           */
21          public void perform(IndexSearcher searcher) throws IOException;
22      }
23  
24      public interface ReaderAction
25      {
26          /**
27           * Perform index reading.
28           */
29          public Object perform(IndexReader reader) throws IOException;
30      }
31  
32      public interface WriterAction
33      {
34          /**
35           * Perform index writing.
36           */
37          public void perform(IndexWriter writer) throws IOException;
38      }
39  
40      public interface BatchUpdateAction
41      {
42          /**
43           * Perform batch update operation. Use this action for multi-step
44           * operations where you want to acquire a write lock for the duration of
45           * the operation.
46           */
47          public void perform() throws Exception;
48      }
49  
50      void withSearch(SearcherAction action) throws LuceneException;
51  
52      /**
53       * Idempotent operation. Just for querying, do not delete documents with
54       * this action. Use {@link #withWriter(WriterAction)} to perform
55       * index deletes.
56       */
57      Object withReader(ReaderAction action) throws LuceneException;
58  
59      /**
60       * Add (write) documents to the index
61       */
62      void withWriter(WriterAction action) throws LuceneException;
63  
64      /**
65       * Perform multiple writes to the index. Holds a writeLock on the index for
66       * the whole time, and will use the {@link Configuration batch
67       * configuration} settings.
68       * <p/>
69       * Update actions performed within a batch update won't be visible to other
70       * readers or searchers until the batch is complete. Be aware this also
71       * applies to actions within a batch! That is, a read operation inside a
72       * batch will not see the changes made by earlier updates in that batch.
73       */
74      void withBatchUpdate(BatchUpdateAction action);
75  
76      /**
77       * Perform an optimize on the index. Holds a writeLock and can take a long
78       * time (depending on the size of the index, how much optimization needs to
79       * be done... and whether a virus-checker is installed :) ).
80       */
81      void optimize() throws LuceneException;
82  
83      /**
84       * Closes the reader and the writer. Calling any method on the API after
85       * closing the connection will throw {@link LuceneConnectionClosedException}.
86       */
87      void close() throws LuceneException;
88  
89      /**
90       * Returns the number of {@link Document documents} in the index.
91       */
92      int getNumDocs();
93  
94      /**
95       * Removes all documents from the index.
96       *
97       * @throws LuceneException if there was a problem removing the index
98       */
99      void truncateIndex() throws LuceneException;
100 
101     /**
102      * Provide configuration for the index writers used by implementations of this interface.
103      * 
104      * @see DefaultConfiguration
105      */
106     public interface Configuration
107     {
108         int getInteractiveMergeFactor();
109         int getInteractiveMaxMergeDocs();
110         int getInteractiveMaxBufferedDocs();
111         int getBatchMergeFactor();
112         int getBatchMaxMergeDocs();
113         int getBatchMaxBufferedDocs();
114         int getMaxFieldLength();
115         boolean isCompoundIndexFileFormat();
116     }
117 
118     /**
119      * The Bonnie default configuration. Preserves backward compatibility for old Confluence settings.
120      */
121     static final Configuration DEFAULT_CONFIGURATION = new DefaultConfiguration();
122 }