home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/search/company/RebuildCompanyIndexTask.java,v 1.3.2.1 2005/05/20 08:52:55 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.3.2.1 $
 * $Date: 2005/05/20 08:52:55 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2005 by MyVietnam.net
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * All copyright notices regarding mvnForum MUST remain intact
 * in the scripts and in the outputted HTML.
 * The "powered by" text/logo with a link back to
 * http://www.mvnForum.com and http://www.MyVietnam.net in the
 * footer of the pages MUST remain visible when the pages
 * are viewed on the internet or intranet.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Support can be obtained from support forums at:
 * http://www.mvnForum.com/mvnforum/index
 *
 * Correspondence and Marketing Questions can be sent to:
 * info@MyVietnam.net
 *
 * @author: Minh Nguyen  minhnn@MyVietnam.net
 * @author: Dejan Krsmanovic dejan_krsmanovic@yahoo.com
 */
package com.mvnforum.search.company;

import java.io.IOException;
import java.util.*;

import com.mvnforum.db.CompanyBean;
import com.mvnforum.db.DAOFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.index.IndexWriter;

/**
 * Rebuilding indices task. This task do indexing of all documents
 */
public class RebuildCompanyIndexTask extends TimerTask
{
    private static Log log = LogFactory.getLog(RebuildCompanyIndexTask.class);

    public static final int MERGE_FACTOR  = 20;

    /*
     * Contructor with default access, prevent new an instance from outside package
     */
    RebuildCompanyIndexTask() {
    }

    /**
     * Create new index. If anything exist already - delete it
     */
    public void run() {
        long start = System.currentTimeMillis();

        Collection companies = null;
        try {
            companies = DAOFactory.getCompanyDAO().getCompanies();
        } catch (Exception ex) {
            log.error("RebuildCompanyIndexTask.run : cannot get all companies from database for indexing", ex);
        }

        if (companies == null) return;

        // we index it even when there are not any companies, this will
        // create an empty search index
        //if (posts.size() == 0) return;

        IndexWriter writer = null;
        try {
            writer = CompanyIndexer.getIndexWriter(true);
            writer.mergeFactor = MERGE_FACTOR;

            int i = 0;
            for (Iterator iter = companies.iterator(); iter.hasNext(); ) {
                CompanyBean companyBean = (CompanyBean) iter.next();
                CompanyIndexer.doIndexCompany(companyBean, writer);
                i++;
            }
            writer.optimize();
            log.info("Rebuilding index finished successfully! " + i + " company(s) indexed");
        } catch (Exception e) {
            log.error("Error while rebuilding index", e);
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException ignore) {
            }
        }
        log.info("RebuildCompanyIndexTask took " + (System.currentTimeMillis() - start) + " ms");
    }
}


my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.