Lucene并发连接实现 - ConcurrentLuceneConnection

Lucene并发连接实现代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import

 edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
import

 edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
import

 edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
import

 java.io.File;
import

 java.io.IOException;
import

 org.apache.log4j.Logger;
import

 org.apache.lucene.analysis.Analyzer;
import

 org.apache.lucene.index.IndexReader;
import

 org.apache.lucene.index.IndexWriter;
import

 org.apache.lucene.search.DelayCloseIndexSearcher;
import

 org.apache.lucene.search.IndexSearcher;
import

 org.apache.lucene.store.*;
 
public

 class

 ConcurrentLuceneConnection
 implements

 ILuceneConnection
{

 
 private

 static

 Logger log;
 private

 transient

 Directory directory;
 private

 final

 Analyzer analyzerForIndexing;
 private

 final

 ILuceneConnection.Configuration configuration;
 private

 final

 Lock indexWriteLock;
 private

 final

 Lock searcherCreationLock;
 private

 final

 AtomicBoolean batchMode;
 private

 DelayCloseIndexSearcher currentSearcher;
 
 public

 ConcurrentLuceneConnection(Directory directory, Analyzer analyzer, ILuceneConnection.Configuration configuration)
 {

     indexWriteLock = new

 ReentrantLock();
     searcherCreationLock = new

 ReentrantLock();
     batchMode = new

 AtomicBoolean(false

);
     this.directory = directory;
     analyzerForIndexing = analyzer;
     this.configuration = configuration;
 }

 
 public

 ConcurrentLuceneConnection(File path, Analyzer analyzer, ILuceneConnection.Configuration configuration)
 {

     this

(getDirectory(path), analyzer, configuration);
 }

 
 public

 int

 getNumDocs()
 {

     return

 ((Integer)withReader(new

 ILuceneConnection.ReaderAction() {

 
         public

 Object perform(IndexReader reader)
         {

             return

 new

 Integer(reader.numDocs());
         }

 
     }
)).intValue();
 }

 
 public

 boolean

 isIndexCreated()
 {

	 try

{

     return

 IndexReader.indexExists(directory);
	 }
catch

(IOException e){

     throw

 new

 LuceneException(e);
	 }

 }

 
 public

 IndexSearcher leakSearcher()
 {

	 try

{

     return

 getOpenedSearcher();
	 }
catch

(Throwable e){

     flipCurrentSearcher();
     throwLuceneException(e);
     throw

 new

 IllegalStateException("Exception should have been thrown."
);
	 }

 }

 
 public

 void

 optimize()
     throws

 LuceneException
 {

     withWriter(new

 ILuceneConnection.WriterAction() {

 
         public

 void

 perform(IndexWriter writer)
             throws

 IOException
         {

             writer.optimize();
         }

     }
);
 }

 
 public

 void

 recreateIndexDirectory()
 {

     indexWriteLock.lock();
     try


     {

         directory.close();
         if

(directory instanceof

 FSDirectory)
             directory = FSDirectory.getDirectory(((FSDirectory)directory).getFile(), true

);
         else


         if

(directory instanceof

 RAMDirectory)
             directory = new

 RAMDirectory();
         (new

 IndexWriter(directory, null, true

)).close();
     }

     catch

(IOException e)
     {

         throw

 new

 LuceneException("Cannot create index directory."
, e);
     }

     flipCurrentSearcher();
     indexWriteLock.unlock();
 }

 
 public

 void

 close()
 {

     flipCurrentSearcher();
 }

 
 public

 void

 withSearch(ILuceneConnection.SearcherAction action)
     throws

 LuceneException
 {

	 try

{

     IndexSearcher searcher = getOpenedSearcher();
     boolean

 b = action.perform(searcher);
     if

(!b)
         throw

 new

 UnsupportedOperationException("Searchers are always closed. The searcherAction should always return true, we do not allow them to control closing of the searchers"
);
     closeSearcher(searcher);
	 }
catch

(Throwable e){

     flipCurrentSearcher();
     throwLuceneException(e);
	 }

 }

 
 public

 Object withReader(ILuceneConnection.ReaderAction action)
     throws

 LuceneException
 {

	 try

{

     IndexSearcher searcher = getOpenedSearcher();
     Object obj = action.perform(searcher.getIndexReader());
     closeSearcher(searcher);
     return

 obj;
	 }
catch

(Throwable e){

     flipCurrentSearcher();
     throwLuceneException(e);
     return

 null

;
	 }

 }

 
 public

 void

 withReaderAndDeletes(ILuceneConnection.ReaderAction action)
     throws

 LuceneException
 {

     IndexReader deleter;
     indexWriteLock.lock();
     deleter = null

;
     try


     {

         deleter = constructIndexDeleter();
         action.perform(deleter);
         flipCurrentSearcher();
     }

     catch

(IOException e)
     {

         throw

 new

 LuceneException(e);
     }

     closeReader(deleter);
     indexWriteLock.unlock();
 }

 
 public

 void

 withWriter(ILuceneConnection.WriterAction action)
     throws

 LuceneException
 {

     IndexWriter writer;
     indexWriteLock.lock();
     writer = null

;
     try


     {

         writer = constructIndexWriter();
         action.perform(writer);
         flipCurrentSearcher();
     }

     catch

(IOException e)
     {

         throw

 new

 LuceneException(e);
     }

     closeWriter(writer);
     indexWriteLock.unlock();
 }

 
 public

 void

 withDeleteAndWrites(ILuceneConnection.ReaderAction readerAction, ILuceneConnection.WriterAction writerAction)
     throws

 LuceneException
 {

     indexWriteLock.lock();
     withReaderAndDeletes(readerAction);
     withWriter(writerAction);
     indexWriteLock.unlock();
 }

 
 public

 void

 withBatchUpdate(ILuceneConnection.BatchUpdateAction action)
 {

	 try

{

     indexWriteLock.lock();
     batchMode.set(true

);
     action.perform();
     batchMode.set(false

);
     indexWriteLock.unlock();
	 }
catch

(Exception e){

		 throwLuceneException(e);
	 }

 }

 
 public

 void

 flipCurrentSearcher()
 {

     if

(log.isDebugEnabled())
         log.debug("Closing current searcher.."
);
     searcherCreationLock.lock();
     if

(currentSearcher != null

)
     {

    	 try

{

     currentSearcher.closeWhenDone();
     currentSearcher = null

;
    	 }
catch

(Exception e){

    		 log.error(e);
    		 currentSearcher = null

;
    	 }

     }

     searcherCreationLock.unlock();
 }

 
 private

 DelayCloseIndexSearcher getOpenedSearcher()
     throws

 IOException
 {

     searcherCreationLock.lock();
     DelayCloseIndexSearcher delaycloseindexsearcher;
     if

(currentSearcher == null

)
         currentSearcher = new

 DelayCloseIndexSearcher(directory);
     currentSearcher.open();
     delaycloseindexsearcher = currentSearcher;
     searcherCreationLock.unlock();
     return

 delaycloseindexsearcher;
 }

 
 private

 IndexReader constructIndexDeleter()
 {

	 try

{

     return

 IndexReader.open(directory);
	 }
catch

(IOException e){

     throw

 new

 LuceneException(e);
	 }

 }

 
 private

 void

 closeReader(IndexReader reader)
 {

     try


     {

         if

(reader != null

)
         {

             if

(log.isDebugEnabled())
                 log.debug(Thread.currentThread().getName() + "## closing reader"
);
             reader.close();
         }

     }

     catch

(IOException e)
     {

         log.error("Error closing reader. "
 + e, e);
     }

 }

 
 private

 void

 closeSearcher(IndexSearcher searcher)
 {

     try


     {

         if

(searcher != null

)
             searcher.close();
     }

     catch

(IOException e)
     {

         log.error("Error occurred while closing searcher."
, e);
     }

 }

 
 private

 void

 closeWriter(IndexWriter writer)
 {

     try


     {

         if

(writer != null

)
         {

             if

(log.isDebugEnabled())
                 log.debug("## closing writer"
);
             writer.close();
         }
 else


         {

             log.warn("## trying to close null writer."
);
         }

     }

     catch

(IOException e)
     {

         log.error("Error closing writer. "
 + e, e);
     }

 }

 
 private

 IndexWriter constructIndexWriter()
     throws

 LuceneException
 {

     IndexWriter indexWriter;
     if

(log.isDebugEnabled())
         log.debug(Thread.currentThread().getName() + "## opening writer"
);
     try

{

     indexWriter = new

 IndexWriter(directory, analyzerForIndexing, false

);
     if

(batchMode.get())
     {

         indexWriter.setMaxBufferedDocs(configuration.getBatchMaxBufferedDocs());
         indexWriter.setMaxMergeDocs(configuration.getBatchMaxMergeDocs());
         indexWriter.setMergeFactor(configuration.getBatchMergeFactor());
     }
 else


     {

         indexWriter.setMaxBufferedDocs(configuration.getInteractiveMaxBufferedDocs());
         indexWriter.setMaxMergeDocs(configuration.getInteractiveMaxMergeDocs());
         indexWriter.setMergeFactor(configuration.getInteractiveMergeFactor());
     }

     indexWriter.setMaxFieldLength(configuration.getMaxFieldLength());
     return

 indexWriter;
     }
catch

(IOException e){

     throw

 new

 LuceneException(e);
     }

 }

 
 private

 void

 throwLuceneException(Throwable e)
 {

     if

(e instanceof

 Error)
         throw

 (Error)e;
     if

(e instanceof

 RuntimeException)
         throw

 (RuntimeException)e;
     else


         throw

 new

 LuceneException(e);
 }

 
 private

 static

 Directory getDirectory(File path)
 {

	 try

{

     return

 FSDirectory.getDirectory(path, false

);
	 }
catch

(IOException e){

     throw

 new

 LuceneException(e);
	 }

 }

 
 static

 
 {

     log = Logger.getLogger(ConcurrentLuceneConnection.class);
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值