LuceneReindexer.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 6k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  * 
  5.  * Redistribution and use in source and binary forms, 
  6.  * with or without modification, are permitted provided 
  7.  * that the following conditions are met:
  8.  * 
  9.  * 1) Redistributions of source code must retain the above 
  10.  * copyright notice, this list of conditions and the 
  11.  * following  disclaimer.
  12.  * 2)  Redistributions in binary form must reproduce the 
  13.  * above copyright notice, this list of conditions and 
  14.  * the following disclaimer in the documentation and/or 
  15.  * other materials provided with the distribution.
  16.  * 3) Neither the name of "Rafael Steil" nor 
  17.  * the names of its contributors may be used to endorse 
  18.  * or promote products derived from this software without 
  19.  * specific prior written permission.
  20.  * 
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
  22.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
  24.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  26.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
  27.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  29.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  31.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  32.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  33.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
  34.  * IN CONTRACT, STRICT LIABILITY, OR TORT 
  35.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  37.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  38.  * 
  39.  * Created on 06/08/2007 15:20:23
  40.  * 
  41.  * The JForum Project
  42.  * http://www.jforum.net
  43.  */
  44. package net.jforum.search;
  45. import java.io.IOException;
  46. import java.util.Iterator;
  47. import java.util.List;
  48. import net.jforum.JForumExecutionContext;
  49. import net.jforum.dao.DataAccessDriver;
  50. import net.jforum.dao.LuceneDAO;
  51. import net.jforum.entities.Post;
  52. import net.jforum.exceptions.ForumException;
  53. import net.jforum.util.preferences.ConfigKeys;
  54. import net.jforum.util.preferences.SystemGlobals;
  55. import org.apache.lucene.search.IndexSearcher;
  56. /**
  57.  * @author Rafael Steil
  58.  * @version $Id: LuceneReindexer.java,v 1.5 2007/09/09 16:43:55 rafaelsteil Exp $
  59.  */
  60. public class LuceneReindexer
  61. {
  62. private LuceneSettings settings;
  63. private LuceneReindexArgs args;
  64. private boolean recreate;
  65. public LuceneReindexer(LuceneSettings settings, LuceneReindexArgs args, boolean recreate)
  66. {
  67. this.args = args;
  68. this.recreate = recreate;
  69. this.settings = settings;
  70. }
  71. public void startProcess()
  72. {
  73. this.reindex();
  74. }
  75. public void startBackgroundProcess()
  76. {
  77. Runnable indexingJob = new Runnable() {
  78. public void run() {
  79. reindex();
  80. }
  81. };
  82. SystemGlobals.setValue(ConfigKeys.LUCENE_CURRENTLY_INDEXING, "1");
  83. Thread thread = new Thread(indexingJob);
  84. thread.start();
  85. }
  86. private void reindex()
  87. {
  88. try {
  89. if (recreate) {
  90. this.settings.createIndexDirectory(SystemGlobals.getValue(ConfigKeys.LUCENE_INDEX_WRITE_PATH));
  91. }
  92. }
  93. catch (IOException e) {
  94. throw new ForumException(e);
  95. }
  96. LuceneDAO dao = DataAccessDriver.getInstance().newLuceneDAO();
  97. IndexSearcher searcher = null;
  98. LuceneSearch luceneSearch = ((LuceneManager)SearchFacade.manager()).luceneSearch();
  99. LuceneIndexer luceneIndexer = ((LuceneManager)SearchFacade.manager()).luceneIndexer();
  100. int fetchCount = SystemGlobals.getIntValue(ConfigKeys.LUCENE_INDEXER_DB_FETCH_COUNT);
  101. try {
  102. if (!recreate) {
  103. searcher = new IndexSearcher(this.settings.directory());
  104. }
  105. boolean hasMorePosts = true;
  106. long processStart = System.currentTimeMillis();
  107. int firstPostId = args.filterByMessage()
  108. ? args.getFirstPostId()
  109. : dao.firstPostIdByDate(args.getFromDate());
  110. int lastPostId = args.filterByMessage()
  111. ? args.getLastPostId()
  112. : dao.lastPostIdByDate(args.getToDate());
  113. int counter = 1;
  114. int indexTotal = 0;
  115. long indexRangeStart = System.currentTimeMillis();
  116. while (hasMorePosts) {
  117. boolean contextFinished = false;
  118. int toPostId = firstPostId + fetchCount < lastPostId
  119. ? firstPostId + fetchCount
  120. : lastPostId;
  121. try {
  122. JForumExecutionContext ex = JForumExecutionContext.get();
  123. JForumExecutionContext.set(ex);
  124. List l = dao.getPostsToIndex(firstPostId, toPostId);
  125. if (counter >= 5000) {
  126. long end = System.currentTimeMillis();
  127. System.out.println("Indexed ~5000 documents in " 
  128. + (end - indexRangeStart) + " ms (" + indexTotal + " so far)");
  129. indexRangeStart = end;
  130. counter = 0;
  131. }
  132. JForumExecutionContext.finish();
  133. contextFinished = true;
  134. for (Iterator iter = l.iterator(); iter.hasNext(); ) {
  135. if ("0".equals(SystemGlobals.getValue(ConfigKeys.LUCENE_CURRENTLY_INDEXING))) {
  136. hasMorePosts = false;
  137. break;
  138. }
  139. Post post = (Post)iter.next();
  140. if (!recreate && args.avoidDuplicatedRecords()) {
  141. if (luceneSearch.findDocumentByPostId(post.getId()) != null) {
  142. continue;
  143. }
  144. }
  145. luceneIndexer.batchCreate(post);
  146. counter++;
  147. indexTotal++;
  148. }
  149. firstPostId += fetchCount;
  150. hasMorePosts = hasMorePosts && l.size() > 0;
  151. }
  152. finally {
  153. if (!contextFinished) {
  154. JForumExecutionContext.finish();
  155. }
  156. }
  157. }
  158. long end = System.currentTimeMillis();
  159. System.out.println("**** Total: " + (end - processStart) + " ms");
  160. }
  161. catch (IOException e) {
  162. throw new ForumException(e);
  163. }
  164. finally {
  165. SystemGlobals.setValue(ConfigKeys.LUCENE_CURRENTLY_INDEXING, "0");
  166. luceneIndexer.flushRAMDirectory();
  167. if (searcher != null) {
  168. try { searcher.close(); }
  169. catch (Exception e) {}
  170. }
  171. }
  172. }
  173. }