📄 dbsearchindexer.java
字号:
/**
* DbSearchIndexer.java * August 29, 2000 * * 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.util.*;
//JDK1.1// import com.sun.java.util.collections.*;import java.sql.*;import com.coolservlets.forum.*;
import com.coolservlets.util.*;
/**
* Right now this creates a thread that updates the database with the new
* words from any messages added in a given amount of time specified by the
* user with the setter methods of this class
* Default update rate will be once every two hours.
* Note: will want to allow updates on a per-message basis as well
*/
public class DbSearchIndexer extends Thread implements SearchIndexer{
/** DATABASE QUERIES **/
private static final String INSERT_WORD =
"INSERT INTO jiveWord(wordID,word) VALUES(?,?)";
private static final String INSERT_INDEX_RECORD =
"INSERT INTO jiveMessageWord(messageID, wordID, wordCount, forumID) " +
"VALUES(?,?,?,?)";
private static final String DELETE_INDEX1 =
"DELETE FROM jiveMessageWord";
private static final String DELETE_INDEX2 =
"DELETE FROM jiveWord";
private static final String DELETE_PARTIAL_INDEX =
"DELETE FROM jiveMessageWord WHERE jiveMessageWord.messageID=?";
private static final String FIND_WORD_ID =
"SELECT wordID FROM jiveWord WHERE word=?";
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 < ?";
/**
* Constant for a MINUTE (in milleseconds)
*/
private static final long MINUTE = 1000 * 60;
/**
* Constant for an HOUR (in milleseconds)
*/
private static final long HOUR = MINUTE * 60;
/**
* Local cache for the word ID's. This cache is reset after every index
* operation to save space. However, using it during large indexing tasks
* can yield a huge performance win since we avoid numerous database
* lookups. However, this cache should be changed to a fixed-size data
* structure at some point in the future. Otherwise, it's possible that it
* will grow larger than memory if too large of an indexing task is
* started.
*/
private Map wordCache;
/**
* 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();
/**
* 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;
wordCache = new HashMap();
//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 */ } wordCache = new HashMap(); //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; } //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 indexMessage(ForumMessage message) {
//acquire the index lock so that no other indexing operations
//are performed.
synchronized (indexLock) {
Connection con = null;
con = DbConnectionManager.getConnection();
indexMessage(message, con);
try { con.close(); }
catch (Exception e) { e.printStackTrace(); } //Clear out word cache until next operation wordCache.clear();
wordCache = new HashMap(); }
}
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 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -