dbquery.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 254 行

JAVA
254
字号
/** * $RCSfile: DbQuery.java,v $ * $Revision: 1.4 $ * $Date: 2002/07/07 17:20:26 $ * * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.database;import java.util.Date;import java.util.Iterator;import org.apache.lucene.document.*;import org.apache.lucene.search.*;import org.apache.lucene.queryParser.*;import com.jivesoftware.forum.*;/** * Database implementation of the Query interface using Lucene. The maximum * number of search results can be set using the Jive property * <tt>search.maxResultsSize</tt>. The default value is 1500. * * @author Matt Tucker */public class DbQuery implements com.jivesoftware.forum.Query {    /**     * The query String to use for searching. Set it the empty String by     * default so that if the user fails to set a query String, there won't     * be errors.     */    private String queryString = "";    private java.util.Date beforeDate = null;    private java.util.Date afterDate = null;    private User user = null;    private ForumThread thread = null;    /**     * The results of the query as an array of message ID's.     */    private long [] results = null;    private Forum [] forums;    private DbForumFactory factory;    /**     * The maximum number of results to return with a search.     */    private static final int MAX_RESULTS_SIZE;    static {        // Load a custom max results size value from the Jive property file or        // default to 1500.        int maxSize = 1500;        String maxResultsSize = JiveGlobals.getJiveProperty("search.maxResultsSize");        if (maxResultsSize != null) {            try {                maxSize = Integer.parseInt(maxResultsSize);            }            catch (NumberFormatException nfe) { }        }        MAX_RESULTS_SIZE = maxSize;    }    public DbQuery(Forum [] forums, DbForumFactory factory) {        this.forums = forums;        this.factory = factory;    }    public void setQueryString(String queryString) {        this.queryString = queryString;        // reset results        results = null;    }    public String getQueryString() {        return queryString;    }    public void setBeforeDate(java.util.Date beforeDate) {        this.beforeDate = beforeDate;        // reset results        results = null;    }    public java.util.Date getBeforeDate() {        return beforeDate;    }    public void setAfterDate(java.util.Date afterDate){        this.afterDate = afterDate;        // reset results        results = null;    }    public java.util.Date getAfterDate(){        return afterDate;    }    public User getFilteredUser() {        return user;    }    public void filterOnUser(User user) {        this.user = user;        results = null;    }    public ForumThread getFilteredThread() {        return thread;    }    public void filterOnThread(ForumThread thread) {        this.thread = thread;        results = null;    }    public int resultCount() {        if (results == null) {            executeQuery();        }        return results.length;    }    public Iterator results() {        if (results == null) {            executeQuery();        }        return new DatabaseObjectIterator(JiveGlobals.MESSAGE, results, factory);    }    public Iterator results(int startIndex, int numResults){        if (results == null) {            executeQuery();        }        int endIndex = startIndex + numResults - 1 ;        if (endIndex > results.length - 1) {            endIndex = results.length - 1;        }        int length = endIndex - startIndex + 1;        if (length < 0) {            return new DatabaseObjectIterator(JiveGlobals.MESSAGE, new long[0], factory);        }        else {            long [] resultsSubset = new long[length];            for (int i=0; i<length; i++) {                resultsSubset[i] = results[startIndex + i];            }            return new DatabaseObjectIterator(JiveGlobals.MESSAGE, resultsSubset, factory);        }    }    /**     * Execute the query and store the results in the results array.     */    private void executeQuery() {        try {            // Acquire a read lock on the searcher lock so we know the query will be completed            // correctly.            DbSearchManager.searcherLock.acquireReadLock();            Searcher searcher = DbSearchManager.getSearcher();            if (searcher == null) {                // Searcher can be null if the index doesn't exist.                results = new long[0];                return;            }            org.apache.lucene.search.Query bodyQuery =                QueryParser.parse(queryString, "body", DbSearchManager.analyzer);            org.apache.lucene.search.Query subjectQuery =                QueryParser.parse(queryString, "subject", DbSearchManager.analyzer);            BooleanQuery comboQuery = new BooleanQuery();            comboQuery.add(subjectQuery,false,false);            comboQuery.add(bodyQuery,false,false);            MultiFilter multiFilter = new MultiFilter(3);            int filterCount = 0;            // Forum filter -- we can ignore filtering if we are searching all            // forums in the system.            if (factory.getForumCount() != forums.length) {                String[] forumIDs = new String[forums.length];                for (int i=0; i<forumIDs.length; i++) {                    forumIDs[i] = Long.toString(forums[i].getID());                }                multiFilter.add(new FieldFilter("forumID", forumIDs));                filterCount++;            }            // Date filter            if (beforeDate != null || afterDate != null) {                if (beforeDate != null && afterDate != null) {                    multiFilter.add(new DateFilter("creationDate", beforeDate, afterDate));                    filterCount++;                }                else if (beforeDate == null) {                    multiFilter.add(DateFilter.After("creationDate", afterDate));                    filterCount++;                }                else {                    multiFilter.add(DateFilter.Before("creationDate", beforeDate));                    filterCount++;                }            }            // User filter            if (user != null) {                String userID = Long.toString(user.getID());                multiFilter.add(new FieldFilter("userID", userID));                filterCount++;            }            // Thread filter            if (thread != null) {                String threadID = Long.toString(thread.getID());                multiFilter.add(new FieldFilter("threadID", threadID));                filterCount++;            }            Hits hits;            // Only apply filters if any are defined.            if (filterCount > 0) {                hits = searcher.search(comboQuery, multiFilter);            }            else {                hits = searcher.search(comboQuery);            }            // Don't return more search results than the maximum number allowed.            int numResults = hits.length() < MAX_RESULTS_SIZE ?                    hits.length() : MAX_RESULTS_SIZE;            long [] messages = new long[numResults];            for (int i=0; i<numResults; i++) {                messages[i] = Long.parseLong( ((Document)hits.doc(i)).get("messageID") );            }            results = messages;        }        catch (Exception e) {            e.printStackTrace();            results = new long[0];        }        finally {            DbSearchManager.searcherLock.releaseReadLock();        }    }}

⌨️ 快捷键说明

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