001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/search/post/RebuildPostIndexTask.java,v 1.21 2008/01/14 08:58:47 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.21 $
005: * $Date: 2008/01/14 08:58:47 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: * @author: Dejan Krsmanovic dejan_krsmanovic@yahoo.com
040: */
041: package com.mvnforum.search.post;
042:
043: import java.io.IOException;
044: import java.util.*;
045:
046: import net.myvietnam.mvncore.exception.DatabaseException;
047:
048: import org.apache.commons.logging.Log;
049: import org.apache.commons.logging.LogFactory;
050: import org.apache.lucene.index.IndexWriter;
051: import org.apache.lucene.store.Directory;
052:
053: import com.mvnforum.db.DAOFactory;
054: import com.mvnforum.db.PostBean;
055: import com.mvnforum.service.MvnForumServiceFactory;
056: import com.mvnforum.service.SearchService;
057:
058: /**
059: * Rebuilding index task. This task do indexing of all documents
060: */
061: public class RebuildPostIndexTask extends TimerTask {
062:
063: private static Log log = LogFactory
064: .getLog(RebuildPostIndexTask.class);
065:
066: public static final int POSTS_PER_FETCH = 200;
067:
068: public static final int MERGE_FACTOR = 20;
069:
070: private int maxPostID = 0;
071:
072: private static boolean isRebuilding = false;
073:
074: public static boolean isRebuilding() {
075: return isRebuilding;
076: }
077:
078: /*
079: * Constructor with default access, prevent new an instance from outside package
080: */
081: RebuildPostIndexTask(int maxPostID) {
082: this .maxPostID = maxPostID;
083: }
084:
085: /**
086: * Create new index. If anything exist already - delete it
087: */
088: public void run() {
089: isRebuilding = true;
090: long start = System.currentTimeMillis();
091:
092: Directory directory = null;
093: IndexWriter writer = null;
094: try {
095: SearchService service = MvnForumServiceFactory
096: .getMvnForumService().getSearchService();
097: directory = service.getSearchPostIndexDir();
098: writer = PostIndexer.getIndexWriter(directory, true);
099: writer.setMergeFactor(MERGE_FACTOR);
100: // note that the maxPostID is got at the beginning of the method
101: // so that it will index only these posts. Later while indexing,
102: // if new posts are added, then other task will take care it
103: if (maxPostID <= 0) {
104: maxPostID = DAOFactory.getPostDAO().getMaxPostID();
105: }
106: int count = 0;
107:
108: for (int fromID = 0; fromID <= maxPostID /* <= is correct */; fromID += POSTS_PER_FETCH) {
109: int toID = fromID + POSTS_PER_FETCH - 1;
110: if (toID > maxPostID) {
111: toID = maxPostID;
112: }
113: Collection posts = DAOFactory.getPostDAO()
114: .getPosts_fromIDRange(fromID, toID);
115:
116: for (Iterator iter = posts.iterator(); iter.hasNext();) {
117: PostBean post = (PostBean) iter.next();
118: PostIndexer.doIndexPost(post, writer);
119: count++;
120: }
121: } //end for
122:
123: writer.optimize();
124: log.info("Rebuilt index finished successfully! " + count
125: + " post(s) indexed.");
126: } catch (DatabaseException ex) {
127: log
128: .error(
129: "RebuildPostIndexTask.run : cannot get posts from database for indexing",
130: ex);
131: } catch (Throwable e) {
132: log.error("Error while rebuilding index", e);
133: } finally {
134: if (writer != null) {
135: try {
136: writer.close();
137: } catch (IOException e) {
138: log.debug("Error closing Lucene IndexWriter", e);
139: }
140: }
141: if (directory != null) {
142: try {
143: directory.close();
144: } catch (IOException e) {
145: log.debug("Cannot close directory.", e);
146: }
147: }
148: }
149: log.info("RebuildPostIndexTask took "
150: + (System.currentTimeMillis() - start) + " ms");
151: isRebuilding = false;
152: }
153:
154: }
|