KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > search > post > RebuildPostIndexTask


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/search/post/RebuildPostIndexTask.java,v 1.10 2006/04/14 17:05:27 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.10 $
5  * $Date: 2006/04/14 17:05:27 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Dejan Krsmanovic dejan_krsmanovic@yahoo.com
40  */

41 package com.mvnforum.search.post;
42
43 import java.io.IOException JavaDoc;
44 import java.util.*;
45
46 import com.mvnforum.MVNForumConfig;
47 import com.mvnforum.db.DAOFactory;
48 import com.mvnforum.db.PostBean;
49 import net.myvietnam.mvncore.exception.DatabaseException;
50 import org.apache.commons.logging.Log;
51 import org.apache.commons.logging.LogFactory;
52 import org.apache.lucene.index.IndexWriter;
53 import org.apache.lucene.store.Directory;
54
55 /**
56  * Rebuilding indices task. This task do indexing of all documents
57  */

58 public class RebuildPostIndexTask extends TimerTask
59 {
60     private static Log log = LogFactory.getLog(RebuildPostIndexTask.class);
61
62     public static final int POSTS_PER_FETCH = 200;
63
64     public static final int MERGE_FACTOR = 20;
65
66     private int maxPostID = 0;
67
68     private static boolean isRebuilding = false;
69
70     public static boolean isRebuilding() {
71         return isRebuilding;
72     }
73
74     /*
75      * Contructor with default access, prevent new an instance from outside package
76      */

77     RebuildPostIndexTask(int maxPostID) {
78         this.maxPostID = maxPostID;
79     }
80
81     /**
82      * Create new index. If anything exist already - delete it
83      */

84     public void run() {
85         isRebuilding = true;
86         long start = System.currentTimeMillis();
87
88         Directory directory = null;
89         IndexWriter writer = null;
90         try {
91             directory = MVNForumConfig.getSearchPostIndexDir();
92             writer = PostIndexer.getIndexWriter(directory, true);
93             writer.mergeFactor = MERGE_FACTOR;
94             // note that the maxPostID is get at the begining of the method
95
// so that it will index only these posts. Later while indexing,
96
// if new posts are added, then other task will take care it
97
if (maxPostID <= 0) {
98                 maxPostID = DAOFactory.getPostDAO().getMaxPostID();
99             }
100             int count = 0;
101
102             for (int fromID = 0; fromID <= maxPostID /* <= is correct */; fromID += POSTS_PER_FETCH) {
103                 int toID = fromID + POSTS_PER_FETCH - 1;
104                 if (toID > maxPostID) {
105                     toID = maxPostID;
106                 }
107                 Collection posts = DAOFactory.getPostDAO().getPosts_fromIDRange(fromID, toID);
108
109                 for (Iterator iter = posts.iterator(); iter.hasNext(); ) {
110                     PostBean post = (PostBean) iter.next();
111                     PostIndexer.doIndexPost(post, writer);
112                     count++;
113                 }
114             } //end for
115

116             writer.optimize();
117             log.info("Rebuilt index finished successfully! " + count + " post(s) indexed.");
118         } catch (DatabaseException ex) {
119             log.error("RebuildPostIndexTask.run : cannot get posts from database for indexing", ex);
120         } catch (Exception JavaDoc e) {
121             log.error("Error while rebuilding index", e);
122         } finally {
123             if (writer != null) {
124                 try {
125                     writer.close();
126                 } catch (IOException JavaDoc e) {
127                     log.debug("Error closing Lucene IndexWriter", e);
128                 }
129             }
130             if (directory != null) {
131                 try {
132                     directory.close();
133                 } catch (IOException JavaDoc e) {
134                     log.debug("Cannot close directory.", e);
135                 }
136             }
137         }
138         log.info("RebuildPostIndexTask took " + (System.currentTimeMillis() - start) + " ms");
139         isRebuilding = false;
140     }
141 }
142
Popular Tags