⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbsearchindexer.java

📁 Jive 是一个系统工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** * $RCSfile: DbSearchIndexer.java,v $ * $Revision: 1.6 $ * $Date: 2000/12/20 02:12:55 $ * * Copyright (C) 2000 CoolServlets.com. All rights reserved. * * =================================================================== * The Apache Software License, Version 1.1 * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *       "This product includes software developed by *        CoolServlets.com (http://www.coolservlets.com)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Jive" and "CoolServlets.com" must not be used to *    endorse or promote products derived from this software without *    prior written permission. For written permission, please *    contact webmaster@coolservlets.com. * * 5. Products derived from this software may not be called "Jive", *    nor may "Jive" appear in their name, without prior written *    permission of CoolServlets.com. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL COOLSERVLETS.COM OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of CoolServlets.com. For more information * on CoolServlets.com, please see <http://www.coolservlets.com>. */package com.coolservlets.forum.database;import java.sql.*;import java.util.*;import java.io.*;import com.lucene.document.*;import com.lucene.analysis.*;import com.lucene.analysis.standard.*;import com.lucene.index.*;import com.lucene.store.*;import com.coolservlets.forum.*;import com.coolservlets.util.*;/** * Database implementation of SearchIndexer using the Lucene search package. * * Search indexes are stored in the "search" subdirectory of directory pointed to * by the Jive property "jiveHome". */public class DbSearchIndexer extends Thread implements SearchIndexer{    /** DATABASE QUERIES **/    private static final String MESSAGES_BEFORE_DATE =        "SELECT messageID FROM jiveMessage WHERE modifiedDate < ?";    private static final String MESSAGES_BEFORE_DATE_COUNT =        "SELECT count(messageID) FROM jiveMessage WHERE modifiedDate < ?";    private static final String MESSAGES_SINCE_DATE =        "SELECT messageID FROM jiveMessage WHERE modifiedDate > ? " +        "AND modifiedDate < ?";    private static final String MESSAGES_SINCE_DATE_COUNT =        "SELECT count(messageID) FROM jiveMessage WHERE modifiedDate > ? " +        "AND modifiedDate < ?";    /**     * Path to where index is stored.     */    private static String indexPath = null;    /**     * Time constants (in milleseconds)     */    private static final long MINUTE = 1000 * 60;    private static final long HOUR = MINUTE * 60;    /**     * Maintains the amount of time that should elapse until the next index.     */    private long updateInterval;    /**     * Maintains the time that the last index took place.     */    private long lastIndexed;    /**     * Indicates whether auto-indexing should be on or off. When on, an update     * will be run at the "updateInterval".     */    private boolean autoIndex = true;    /**     * ForumFactory so that we can load message objects based on their ID.     */    private DbForumFactory factory;    /**     * Lock so that only one indexing function can be executed at once. Not     * locking could impact the database integrity. Therefore, in a cluster of     * Jive servers all pointed at the same db, only one indexer should be     * running once.     */    private Object indexLock = new Object();    private static Analyzer analyzer = new StandardAnalyzer();    /**     * Creates a new DbSearchIndexer. It attempts to load properties for     * the update interval and when the last index occured from the Jive     * properties then starts the indexing thread.     */    public DbSearchIndexer(DbForumFactory factory) {        this.factory = factory;        //Default to performing updates ever 10 minutes.        updateInterval = 10 * MINUTE;        //If the update interval property exists, use that        String updInterval = PropertyManager.getProperty("DbSearchIndexer.updateInterval");        try {            updateInterval = Long.parseLong(updInterval);        }        catch (Exception e) { /* ignore */ }        //Attempt to get the last updated time from the Jive properties        String lastInd = PropertyManager.getProperty("DbSearchIndexer.lastIndexed");        try {            lastIndexed = Long.parseLong(lastInd);        }        catch (Exception e) {            //Something went wrong. Therefore, set lastIndexed far into the past            //so that we'll do a full index.            lastIndexed = 0;        }        //Make this a daemon thread.        this.setDaemon(true);        //Start the indexing thread.        start();    }    public int getHoursUpdateInterval() {        return (int)(updateInterval / HOUR);    }    public int getMinutesUpdateInterval() {        return (int)((updateInterval - getHoursUpdateInterval()*HOUR) / MINUTE);    }    public void setUpdateInterval(int minutes, int hours) {        updateInterval = (minutes * MINUTE) + (hours * HOUR);        //Save it to the properties        PropertyManager.setProperty("DbSearchIndexer.updateInterval", ""+updateInterval);    }    public java.util.Date getLastIndexedDate() {        return new java.util.Date(lastIndexed);    }    public boolean isAutoIndexEnabled() {        return autoIndex;    }    public void setAutoIndexEnabled(boolean value) {        autoIndex = value;    }    public void addToIndex(ForumMessage message) {        //acquire the index lock so that no other indexing operations        //are performed.        synchronized (indexLock) {            IndexWriter writer = null;            try {                writer = getWriter(false);                addMessageToIndex(message, writer);            }            catch (IOException ioe) {                ioe.printStackTrace();            }            finally{                try { writer.close(); }                catch (Exception e) { }            }        }    }    public void removeFromIndex(ForumMessage message) {        //acquire the index lock so that no other indexing operations        //are performed.        synchronized (indexLock) {            try {                int [] toDelete = new int [] { message.getID() };                deleteMessagesFromIndex(toDelete);            }            catch (IOException ioe) {                ioe.printStackTrace();            }        }    }    public void updateIndex() {        //acquire the index lock so that no other indexing operations        //are performed.        synchronized (indexLock) {            long now = System.currentTimeMillis();            updateIndex(lastIndexed, now);            lastIndexed = now;            //Save the time as a Jive property.            PropertyManager.setProperty("DbSearchIndexer.lastIndexed",                "" + lastIndexed);        }    }    public void rebuildIndex() {        //acquire the index lock so that no other indexing operations        //are performed.        synchronized (indexLock) {            long now = System.currentTimeMillis();            rebuildIndex(now);            lastIndexed = now;            //Save the time as a Jive property.            PropertyManager.setProperty("DbSearchIndexer.lastIndexed",                "" + lastIndexed);        }    }    /**     * Indexing thread logic. It wakes up once a minute to see if any threaded     * action should take place.     */    public void run() {        while (true){            //If auto indexing is on            if (autoIndex) {                long now = System.currentTimeMillis();                //If we want to re-index everything.                if (lastIndexed == 0) {                    synchronized(indexLock) {                        rebuildIndex(now);                        lastIndexed = now;                        //Save the time as a Jive property.                        PropertyManager.setProperty("DbSearchIndexer.lastIndexed",                            "" + lastIndexed);                    }                }                //We only want to do an update.                else {                    long nextIndex = lastIndexed + updateInterval;                    if (now > nextIndex) {                        synchronized(indexLock) {                            updateIndex(lastIndexed, now);                            lastIndexed = now;                            //Save the time as a Jive property.                            PropertyManager.setProperty("DbSearchIndexer.lastIndexed",                                "" + lastIndexed);                        }                    }                }            }            //sleep for 1 minute and then check again.            try {                this.sleep(60000);            }            catch (Exception e) {                e.printStackTrace();            }        }    }   /**     * Indexes an indivual message. The connection is assumed to be open when     * passed in and will remain open after the method is done executing.     */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -