订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
lucene

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);
 }
}


平均得分
(0 次评分)





文章来自: 本站原创
标签: 并发 lucene 连接 实现 
评论: 8 | 查看次数: 1611
  • 共有 8 条评论
游客 [2008-08-25 23:03:05]
lljhyp [2008-08-10 10:29:41]
你有lucene-2.3.2的API吗
游客 [2008-08-06 14:20:15]
游客 [2008-07-24 05:57:25]
游客 [2008-07-18 15:55:01]
Hey whats up guys New to the board.wow gold I'm in a bit of a dilema.wow gold welcome to my blog. mining and skinning as professions.world of warcraft gold I've made tons of with this guy.wow geld Enough money that I was able to buy.wow powerlevel my mount at level 40 with no problem at all.buy wow gold So now Im about to create an ALT toon and was wondering.wow which professions would compliment my.wow gold level 44 warrior and make me money at the same time.cheap wow gold One of my friends sayd I should go with disenchanting.serveur wow and blacksmithing but I don't know if there.wow europe is any money to be made in those.cheapest wow gold What do you guys think.wow power leveling Blacksmithing doesn't really pay off that well.wow powerleveling And I think that your warrior is geared quite well.gold wow so that the blacksmithing skill would have.mp3 players to be levelled quitea lot before you.mp3 player can make some nice and useful things.One crafting skill that compliments a warrior is alchemy.wow gold You can brew potions that he can consume.mp3 player and which are quite helpful in battle.zubehoer mp3 player There are potions to replenish lost health or rage.wow gold kaufen and there are battle and guardian elixirs to give you a boost.mp3 You could try enchanting to boost your warrior's gear.mp4 but then your other skill would have to be. mp4one where you can create green items (such as tailoring or leatherworking) to be disenchanted.
You can also send any useless greens you find with your warriorwow gold to your alt.but it's easier if you can also make your own.
游客 [2008-07-18 15:49:06]
Best place to start is in Bloodhoof Village in Mulgore.wow geld Go speak to Harn Longcast and buy Brilliant Smallfish him.mp3 players Now start fishing in Stonebull Lake.mp3 player Equip your rod and then apply the shiny bauble lure to it.wow level service as this will make catching fish easier.You'll want to catch about 60 Brilliant minutes.mp3 players You'll find that by the time you have Smallfish.mp3 player you'll also have about 30 Longjaw Mud Snappers.wow Once you have 60.mp3 players cook them.mp3 player You can own fire or jog back.wow gold to Harn Longcast and use the fire in front of him.wow schnell gold Cook the Brilliant Smallfish.gold für wow then at level 50 start cooking Longja.wow gold paypal Mud Snappers (after learning the recipe you bought earlier).You'll need to go and catch Longjaw.gold in wow Mud Snappers now and the place for these is.wow gold 1000 the pond in Orgrimmar by Lumark the Fishing trainer.wow power leveling (I know you've been catching quite a few where you are.wowgold but the "drop rate" is a lot better in Orgrimmar).Between levels 50 and 75 go and cooking.wowgold You'll now need to catch about 30 Longjaw 100.world of warcraft power leveling Now go to Mill in Hillsbrad and buy Bristle Whisker.wow golds Catfish recipe off of Derak Nightfall.cheapest wow gold Go to Mill and fish for Bristle Whisker Catfish.wow lvl Depending on your cooking level.world of warcraft power leveling Between levels 125 expert cooking from trainer.buy mp3 players You'll need and buy a book for your Fishing level.
Speak tobuy the book Expert Fishing: The Bass and You.Once your up to 175 cooking, go to Shadowprey Village in Desolace.
游客 [2008-07-18 15:45:09]
Hey whats up guys New to the board.wow gold I'm in a bit of a dilema.wow gold welcome to my blog. mining and skinning as professions.world of warcraft gold I've made tons of with this guy.wow geld Enough money that I was able to buy.wow powerlevel my mount at level 40 with no problem at all.buy wow gold So now Im about to create an ALT toon and was wondering.wow which professions would compliment my.wow gold level 44 warrior and make me money at the same time.cheap wow gold One of my friends sayd I should go with disenchanting.serveur wow and blacksmithing but I don't know if there.wow europe is any money to be made in those.cheapest wow gold What do you guys think.wow power leveling Blacksmithing doesn't really pay off that well.wow powerleveling And I think that your warrior is geared quite well.gold wow so that the blacksmithing skill would have.mp3 players to be levelled quitea lot before you.mp3 player can make some nice and useful things.One crafting skill that compliments a warrior is alchemy.wow gold You can brew potions that he can consume.mp3 player and which are quite helpful in battle.zubehoer mp3 player There are potions to replenish lost health or rage.wow gold kaufen and there are battle and guardian elixirs to give you a boost.mp3 You could try enchanting to boost your warrior's gear.mp4 but then your other skill would have to be. mp4one where you can create green items (such as tailoring or leatherworking) to be disenchanted.
You can also send any useless greens you find with your warriorwow gold to your alt.but it's easier if you can also make your own.
游客 [2008-07-18 15:41:31]
  • 共有 8 条评论
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启