001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/search/attachment/RebuildAttachmentIndexTask.java,v 1.9 2008/01/14 08:58:46 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.9 $
005: * $Date: 2008/01/14 08:58:46 $
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: */
040: package com.mvnforum.search.attachment;
041:
042: import java.io.IOException;
043: import java.util.Collection;
044: import java.util.Iterator;
045: import java.util.TimerTask;
046:
047: import net.myvietnam.mvncore.exception.DatabaseException;
048:
049: import org.apache.commons.logging.Log;
050: import org.apache.commons.logging.LogFactory;
051: import org.apache.lucene.index.IndexWriter;
052: import org.apache.lucene.store.Directory;
053:
054: import com.mvnforum.db.AttachmentBean;
055: import com.mvnforum.db.DAOFactory;
056: import com.mvnforum.service.MvnForumServiceFactory;
057: import com.mvnforum.service.SearchService;
058:
059: /**
060: * Rebuilding index task. This task do indexing of all documents
061: */
062: public class RebuildAttachmentIndexTask extends TimerTask {
063:
064: private static Log log = LogFactory
065: .getLog(RebuildAttachmentIndexTask.class);
066:
067: public static final int ATTACHMENTS_PER_FETCH = 200;
068:
069: public static final int MERGE_FACTOR = 20;
070:
071: private int maxAttachmentID = 0;
072:
073: private static boolean isRebuilding = false;
074:
075: public static boolean isRebuilding() {
076: return isRebuilding;
077: }
078:
079: /*
080: * Constructor with default access, prevent new an instance from outside package
081: */
082: RebuildAttachmentIndexTask(int maxAttachmentID) {
083: this .maxAttachmentID = maxAttachmentID;
084: }
085:
086: /**
087: * Create new index. If anything exist already - delete it
088: */
089: public void run() {
090: isRebuilding = true;
091: long start = System.currentTimeMillis();
092:
093: Directory directory = null;
094: IndexWriter writer = null;
095: try {
096: SearchService service = MvnForumServiceFactory
097: .getMvnForumService().getSearchService();
098: directory = service.getSearchAttachmentIndexDir();
099: writer = AttachmentIndexer.getIndexWriter(directory, true);
100: writer.setMergeFactor(MERGE_FACTOR);
101: // note that the maxAttachmentID is got at the beginning of the method
102: // so that it will index only these attachments. Later while indexing,
103: // if new attachments are added, then other task will take care it
104: if (maxAttachmentID <= 0) {
105: maxAttachmentID = DAOFactory.getAttachmentDAO()
106: .getMaxAttachmentID();
107: }
108: int count = 0;
109:
110: for (int fromID = 0; fromID <= maxAttachmentID /* <= is correct */; fromID += ATTACHMENTS_PER_FETCH) {
111: int toID = fromID + ATTACHMENTS_PER_FETCH - 1;
112: if (toID > maxAttachmentID) {
113: toID = maxAttachmentID;
114: }
115: Collection attachments = DAOFactory.getAttachmentDAO()
116: .getAttachments_fromIDRange(fromID, toID);
117:
118: for (Iterator iter = attachments.iterator(); iter
119: .hasNext();) {
120: AttachmentBean attachment = (AttachmentBean) iter
121: .next();
122: AttachmentIndexer.doIndexAttachment(attachment,
123: writer);
124: count++;
125: }
126: } //end for
127:
128: writer.optimize();
129: log.info("Rebuilt index finished successfully! " + count
130: + " attachment(s) indexed.");
131: } catch (DatabaseException ex) {
132: log
133: .error(
134: "RebuildAttachmentIndexTask.run : cannot get attachments from database for indexing",
135: ex);
136: } catch (Throwable e) {
137: log.error("Error while rebuilding index", e);
138: } finally {
139: if (writer != null) {
140: try {
141: writer.close();
142: } catch (IOException e) {
143: log.debug("Error closing Lucene IndexWriter", e);
144: }
145: }
146: if (directory != null) {
147: try {
148: directory.close();
149: } catch (IOException e) {
150: log.debug("Cannot close directory.", e);
151: }
152: }
153: }
154: log.info("RebuildAttachmentIndexTask took "
155: + (System.currentTimeMillis() - start) + " ms");
156: isRebuilding = false;
157: }
158:
159: }
|