- 自己動手寫分布式搜索引擎
- 羅剛
- 368字
- 2020-11-28 15:52:47
3.2.12 多線程寫索引
Lucene默認只使用一個線程寫索引,而且一個索引只能由一個進程打開。
public class ThreadedIndexWriter extends IndexWriter { private ExecutorService threadPool; private Analyzer defaultAnalyzer; private class Job implements Runnable { //保留要加入索引的一個文檔 Document doc; Analyzer analyzer; Term delTerm; public Job(Document doc, Term delTerm, Analyzer analyzer) { this.doc = doc; this.analyzer = analyzer; this.delTerm = delTerm; } public void run() { //實際增加和更新文檔 try { if (delTerm ! = null) { ThreadedIndexWriter.super.updateDocument(delTerm, doc, analyzer); } else { ThreadedIndexWriter.super.addDocument(doc, analyzer); } } catch (IOException ioe) { throw new RuntimeException(ioe); } } } public ThreadedIndexWriter(Directory dir, Analyzer a, boolean create, int numThreads, int maxQueueSize, IndexWriter.MaxFieldLength mfl) throws CorruptIndexException, IOException { super(dir, a, create, mfl); defaultAnalyzer = a; threadPool = new ThreadPoolExecutor( //創建線程池 numThreads, numThreads, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(maxQueueSize, false), new ThreadPoolExecutor.CallerRunsPolicy()); } public void addDocument(Document doc) { //讓線程池增加文檔 threadPool.execute(new Job(doc, null, defaultAnalyzer)); } public void addDocument(Document doc, Analyzer a) { //讓線程池增加文檔 threadPool.execute(new Job(doc, null, a)); } public void updateDocument(Term term, Document doc) { //讓線程池更新文檔 threadPool.execute(new Job(doc, term, defaultAnalyzer)); } //讓線程池更新文檔 public void updateDocument(Term term, Document doc, Analyzer a) { threadPool.execute(new Job(doc, term, a)); } public void close() throws CorruptIndexException, IOException { finish(); super.close(); } public void close(boolean doWait) throws CorruptIndexException, IOException { finish(); super.close(doWait); } public void rollback() throws CorruptIndexException, IOException { finish(); super.rollback(); } private void finish() { //關閉線程池 threadPool.shutdown(); while(true) { try { if (threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS)) { break; } } catch (InterruptedException ie) { Thread.currentThread().interrupt(); throw new RuntimeException(ie); } } } }
如果是多線程寫索引,則讓每個線程使用一個不同的Document,不要在多個線程之間共享同一個Document。
推薦閱讀
- 常用工具軟件案例教程
- Word論文排版之道
- Microsoft Visual C++ Windows Applications by Example
- SolidWorks 2021中文版機械設計從入門到精通
- Oracle VM Manager 2.1.2
- UG NX 完全實例解析
- SketchUp/Piranesi印象彩繪表現項目實踐
- Photoshop網店圖片處理實訓教程
- Photoshop CC摳圖+修圖+調色+合成+特效實戰視頻教程
- 攝影師的后期必修課(調色篇)
- Flash CC動畫制作與應用(第3版)
- Altium Designer 21實戰從入門到精通
- 中文版Corel DRAW X5案例實訓教材
- Moodle 1.9 for Design and Technology
- 跟著視頻學Excel數據處理:函數篇