001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/search/member/RebuildMemberIndexTask.java,v 1.18 2008/01/14 08:58:45 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.18 $
005: * $Date: 2008/01/14 08:58:45 $
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.member;
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.MemberBean;
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 RebuildMemberIndexTask extends TimerTask {
062:
063: private static Log log = LogFactory
064: .getLog(RebuildMemberIndexTask.class);
065:
066: public static final int MEMBERS_PER_FETCH = 200;
067:
068: public static final int MERGE_FACTOR = 20;
069:
070: private int maxMemberID = 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: RebuildMemberIndexTask(int maxMemberID) {
082: this .maxMemberID = maxMemberID;
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.getSearchMemberIndexDir();
098: writer = MemberIndexer.getIndexWriter(directory, true);
099: writer.setMergeFactor(MERGE_FACTOR);
100: // note that the maxMemberID is got at the beginning of the method
101: // so that it will index only these members. Later while indexing,
102: // if new members are added, then other task will take care it
103: if (maxMemberID <= 0) {
104: maxMemberID = DAOFactory.getMemberDAO()
105: .getMaxMemberID();
106: }
107: // we index it even when there are not any members, this will
108: // create an empty search index
109: //if (members.size() == 0) return; ??
110: int count = 0;
111:
112: for (int fromID = 0; fromID <= maxMemberID /* <= is correct */; fromID += MEMBERS_PER_FETCH) {
113: int toID = fromID + MEMBERS_PER_FETCH - 1;
114: if (toID > maxMemberID) {
115: toID = maxMemberID;
116: }
117: Collection members = DAOFactory.getMemberDAO()
118: .getMembers_fromIDRange(fromID, toID);
119:
120: for (Iterator iter = members.iterator(); iter.hasNext();) {
121: MemberBean memberBean = (MemberBean) iter.next();
122: MemberIndexer.doIndexMember(memberBean, writer);
123: count++;
124: }
125: } //end for
126:
127: writer.optimize();
128: log.info("Rebuilt index finished successfully! " + count
129: + " member(s) indexed.");
130: } catch (DatabaseException e) {
131: log
132: .error(
133: "RebuildMemberIndexTask.run : cannot get members from database for indexing",
134: e);
135: } catch (Throwable e) {
136: log.error("Error while rebuilding index", e);
137: } finally {
138: if (writer != null) {
139: try {
140: writer.close();
141: } catch (IOException e) {
142: log.debug("Error closing Lucene IndexWriter", e);
143: }
144: }
145: if (directory != null) {
146: try {
147: directory.close();
148: } catch (IOException e) {
149: log.debug("Cannot close directory.", e);
150: }
151: }
152: }
153: log.info("RebuildMemberIndexTask took "
154: + (System.currentTimeMillis() - start) + " ms");
155: isRebuilding = false;
156: }
157:
158: }
|