📄 dbsearchindexer.java
字号:
/**
* Copyright (C) 2001 Yasna.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
* Yasna.com (http://www.yasna.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Yazd" and "Yasna.com" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact yazd@yasna.com.
*
* 5. Products derived from this software may not be called "Yazd",
* nor may "Yazd" appear in their name, without prior written
* permission of Yasna.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 YASNA.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 Yasna.com. For more information
* on Yasna.com, please see <http://www.yasna.com>.
*/
/**
* 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.Yasna.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.Yasna.forum.*;
import com.Yasna.util.*;
/**
* Database implementation of SearchIndexer using the Lucene search package.
*
* Search indexes are stored in the "search" subdirectory of directory pointed to
* by the Yazd property "yazdHome".
*/
public class DbSearchIndexer extends Thread implements SearchIndexer{
/** DATABASE QUERIES **/
//private static final String MESSAGES_BEFORE_DATE =
// "SELECT messageID FROM yazdMessage WHERE modifiedDate < ?";
private static final String MESSAGES_BEFORE_DATE =
"SELECT messageID, userID, yazdMessage.threadID, forumID, " +
"subject, body, yazdMessage.creationDate " +
"FROM yazdMessage, yazdThread WHERE yazdMessage.threadID=yazdThread.threadID " +
"AND yazdMessage.modifiedDate < ?";
private static final String MESSAGES_BEFORE_DATE_COUNT =
"SELECT count(messageID) FROM yazdMessage WHERE modifiedDate < ?";
private static final String MESSAGES_SINCE_DATE =
"SELECT messageID FROM yazdMessage WHERE modifiedDate > ? " +
"AND modifiedDate < ?";
private static final String MESSAGES_SINCE_DATE_COUNT =
"SELECT count(messageID) FROM yazdMessage WHERE modifiedDate > ? " +
"AND modifiedDate < ?";
private static final String LOAD_MESSAGE =
"SELECT subject, body, userID, yazdMessage.threadID, forumID, " +
"yazdMessage.creationDate FROM yazdMessage, yazdThread WHERE " +
"yazdMessage.threadID=yazdThread.threadID AND yazdMessage.messageID=?";
/**
* 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
* Yazd servers all pointed at the same db, only one indexer should be
* running once.
*/
private Object indexLock = new Object();
private static Analyzer analyzer = new StopAnalyzer();
/**
* Creates a new DbSearchIndexer. It attempts to load properties for
* the update interval and when the last index occured from the Yazd
* 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 Yazd 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(writer, message.getID(),
message.getUnfilteredSubject(), message.getUnfilteredBody(),
message.getUser().getID(), message.getForumThread().getID(),
message.getForumThread().getForum().getID(),
message.getCreationDate()
);
}
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 Yazd 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 Yazd property.
PropertyManager.setProperty("DbSearchIndexer.lastIndexed",
"" + lastIndexed);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -