dbforumthread.java

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

JAVA
1,412 行
字号
            // Delete the data from the jiveMessage table.            pstmt = con.prepareStatement(DELETE_MESSAGE);            pstmt.setLong(1, message.getID());            pstmt.execute();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }        }        // Now, delete the message from the cache.        factory.cacheManager.messageCache.remove(new Long(message.getID()));    }    /**     * Returns the number of reward points for the thread.     */    protected int getRewardPoints() {        return rewardPoints;    }    /**     * Sets the number of reward points for the thread.     */    protected void setRewardPoints(int rewardPoints, Connection con)            throws SQLException    {        // Save old point value in case something goes wrong.        int oldPoints = this.rewardPoints;        this.rewardPoints = rewardPoints;        try {            saveToDb(con);        }        catch (SQLException sqle) {            // Switch back to old point value.            this.rewardPoints = oldPoints;            throw sqle;        }        // Re-add thread to cache.        factory.cacheManager.threadCache.put(new Long(id), this);    }    /**     * Returns the SQL statement corresponding to a ResultFilter for messages.     */    private String getMessageListSQL(ResultFilter resultFilter, boolean countQuery) {        int sortField = resultFilter.getSortField();        // Make sure the sort field is valid.        if (!countQuery && !( sortField == JiveGlobals.MODIFIED_DATE ||                sortField == JiveGlobals.CREATION_DATE ||                sortField == JiveGlobals.MESSAGE_SUBJECT ||                ( sortField == JiveGlobals.EXTENDED_PROPERTY &&                  resultFilter.getSortPropertyName() != null                )              )            )        {            throw new IllegalArgumentException("The specified sort field is not valid.");        }        // We'll accumlate the query in a StringBuffer.        StringBuffer query = new StringBuffer(160);        if (!countQuery) {            query.append("SELECT jiveMessage.messageID");        }        else {            query.append("SELECT count(1)");        }        boolean filterUser = resultFilter.getUserID() != ResultFilter.NULL_INT;        boolean filterCreationDate = resultFilter.getCreationDateRangeMin() != null ||                                     resultFilter.getCreationDateRangeMax() != null;        boolean filterModifiedDate = resultFilter.getModifiedDateRangeMin() != null ||                                     resultFilter.getModifiedDateRangeMax() != null;        int propertyCount = resultFilter.getPropertyCount();        // SELECT -- need to add value that we sort on        if(!countQuery) {            switch(sortField) {                case JiveGlobals.MESSAGE_SUBJECT:                    query.append(", jiveMessage.subject");                    break;                case JiveGlobals.MODIFIED_DATE:                    query.append(", jiveMessage.modifiedDate");                    break;                case JiveGlobals.CREATION_DATE:                    query.append(", jiveMessage.creationDate");                    break;                case JiveGlobals.EXTENDED_PROPERTY:                    query.append(", propTable.propValue");                    break;            }        }        // FROM -- values (in addition to jiveThread)        query.append(" FROM jiveMessage");        for (int i=0; i<propertyCount; i++) {            query.append(", jiveMessageProp p").append(i);        }        if (resultFilter.getSortField() == JiveGlobals.EXTENDED_PROPERTY) {            query.append(", jiveMessageProp propTable");        }        // WHERE BLOCK        query.append(" WHERE jiveMessage.threadID=").append(this.id);        // User        if (filterUser) {            query.append(" AND jiveMessage.userID=").append(resultFilter.getUserID());        }        // Properties        for (int i=0; i<propertyCount; i++) {            query.append(" AND jiveMessage.messageID=p").append(i).append(".messageID");            query.append(" AND p").append(i).append(".name='");            query.append(resultFilter.getPropertyName(i));            query.append("' AND p").append(i).append(".propValue='");            query.append(resultFilter.getPropertyValue(i)).append("'");        }        // Sort on properties        if (sortField == JiveGlobals.EXTENDED_PROPERTY) {            query.append(" AND jiveMessage.messageID=propTable.messageID");            query.append(" AND propTable.name='");            query.append(resultFilter.getSortPropertyName()).append("'");        }        // Creation date range        if (filterCreationDate) {            if (resultFilter.getCreationDateRangeMin() != null) {                query.append(" AND jiveMessage.creationDate >= '");                query.append(StringUtils.dateToMillis(                        resultFilter.getCreationDateRangeMin()));                query.append("'");            }            if (resultFilter.getCreationDateRangeMax() != null) {                query.append(" AND jiveMessage.creationDate <= '");                query.append(StringUtils.dateToMillis(                        resultFilter.getCreationDateRangeMax()));                query.append("'");            }        }        // Modified date range        if (filterModifiedDate) {            if (resultFilter.getModifiedDateRangeMin() != null) {                query.append(" AND jiveMessage.modifiedDate >= '");                query.append(StringUtils.dateToMillis(                        resultFilter.getModifiedDateRangeMin()));                query.append("'");            }            if (resultFilter.getModifiedDateRangeMax() != null) {                query.append(" AND jiveMessage.modifiedDate <= '");                query.append(StringUtils.dateToMillis(                        resultFilter.getModifiedDateRangeMax()));                query.append("'");            }        }        // Moderation range (but only if moderation hasn't been disabled at        // the database level for performance reasons.        if (!factory.moderationDisabled()) {            int moderationRangeMin = resultFilter.getModerationRangeMin();            // If the minimum moderation value was not set, it should default            // to the minimum threshold for the forum.            if (moderationRangeMin == ResultFilter.NULL_INT) {                moderationRangeMin = getForum().getModerationMinMessageValue();            }            int moderationRangeMax = resultFilter.getModerationRangeMax();            if (moderationRangeMin == moderationRangeMax) {                query.append(" AND jiveMessage.modValue = ");                query.append(moderationRangeMin);            }            else {                // Ignore any min less than negative 1 million.                if (moderationRangeMin > -1000000) {                    query.append(" AND jiveMessage.modValue >= ");                    query.append(moderationRangeMin);                }                // Use a max value if it's defined.                if (moderationRangeMax != ResultFilter.NULL_INT) {                    query.append(" AND jiveMessage.modValue <= ");                    query.append(moderationRangeMax);                }            }        }        // ORDER BY        if (!countQuery) {            switch(sortField) {                case JiveGlobals.MESSAGE_SUBJECT:                    query.append(" ORDER BY jiveMessage.subject");                    break;                case JiveGlobals.MODIFIED_DATE:                    query.append(" ORDER BY jiveMessage.modifiedDate");                    break;                case JiveGlobals.CREATION_DATE:                    query.append(" ORDER BY jiveMessage.creationDate");                    break;                case JiveGlobals.EXTENDED_PROPERTY:                    query.append(" ORDER BY propTable.propValue");                    break;            }            if (resultFilter.getSortOrder() == ResultFilter.DESCENDING) {                query.append(" DESC");            }            else {                query.append(" ASC");            }        }        return query.toString();    }    /**     * Clears caches related to the object.     */    protected void clearCache() {        synchronized (queryKeys) {            // Clear out query cache lists and counts.            Object [] listElements = queryKeys.toArray();            queryKeys.clear();            for (int i=0; i<listElements.length; i++) {                Object key = listElements[i];                // If the short-term query cache is enabled, move the object                // there.                if (factory.cacheManager.isShortTermQueryCacheEnabled()) {                    Object value = factory.cacheManager.queryCache.get(key);                    // Add the object to the short-term cache and then remove it                    // from the normal cache.                    if (value != null) {                        factory.cacheManager.shortTermQueryCache.put(key, value);                    }                }                factory.cacheManager.queryCache.remove(key);            }        }        // Re-add thread to cache.        factory.cacheManager.threadCache.put(new Long(this.id), this);    }    /**     * Returns a block of objectID's (threads or messages) from a query and     * performs transparent caching of those blocks. The two parameters specify     * a database query and a startIndex for the results in that query.     *     * @param query the SQL message list query to cache blocks from.     * @param startIndex the startIndex in the list to get a block for.     */    protected long [] getBlock(String query, int startIndex) {        // First, find what block number the results will be in.        int blockID = startIndex / DbForum.BLOCK_SIZE;        int blockStart = blockID * DbForum.BLOCK_SIZE;        // Now, check cache to see if the block is already cached.        QueryCacheKey key = new QueryCacheKey(JiveGlobals.THREAD, id, query, blockID);        // Synchronize on the sql query so that only one thread can try to load a        // block at once. This should help to prevent the database from        // becoming overloaded on busy sites.        StringBuffer internKey = new StringBuffer(query.length()+4).append(query).append(blockID);        synchronized (internKey.toString().intern()) {            // First, look in long-term cache.            long [] objectIDs = (long [])factory.cacheManager.queryCache.get(key);            // If lookup in long-term cache failed, look in short-term cache.            if (objectIDs == null && factory.cacheManager.isShortTermQueryCacheEnabled()) {                objectIDs = (long [])factory.cacheManager.shortTermQueryCache.get(key);            }            // If already in cache, return the block.            if (objectIDs != null) {                /**                 * The actual block may be smaller than BLOCK_SIZE. If                 * that's the case, it means two things:                 *  1) We're at the end boundary of all the results.                 *  2) If the start index is greater than the length of the current                 *     block, than there aren't really any results to return.                 */                if (startIndex >= blockStart + objectIDs.length) {                    // Return an empty array                    return EMPTY_BLOCK;                }                else {                    return objectIDs;                }            }            // Otherwise, we have to load up the block from the database.            else {                LongList objectList = new LongList(DbForum.BLOCK_SIZE);                Connection con = null;                Statement stmt = null;                try {                    con = ConnectionManager.getConnection();                    stmt = ConnectionManager.createScrollableStatement(con);                    // Set the maximum number of rows to end at the end of this block.                    ConnectionManager.setMaxRows(stmt, DbForum.BLOCK_SIZE * (blockID+1));                    ResultSet rs = stmt.executeQuery(query);                    // Grab BLOCK_SIZE rows at a time.                    ConnectionManager.setFetchSize(rs, DbForum.BLOCK_SIZE);                    // Position the cursor right before the first row that we're insterested in.                    ConnectionManager.scrollResultSet(rs, blockStart);                    // Keep reading results until the result set is exhausted or                    // we come to the end of the block.                    int count = 0;                    while (rs.next() && count < DbForum.BLOCK_SIZE) {                        objectList.add(rs.getLong(1));                        count++;                    }                }                catch( SQLException sqle ) {                    sqle.printStackTrace();                }                finally {                    try {  stmt.close(); }                    catch (Exception e) { e.printStackTrace(); }                    try {  con.close();   }                    catch (Exception e) { e.printStackTrace(); }                }                objectIDs = objectList.toArray();                // Add the block to cache                factory.cacheManager.queryCache.put(key, objectIDs);                addQueryKey(key);                /**                 * The actual block may be smaller than BLOCK_SIZE. If                 * that's the case, it means two things:                 *  1) We're at the end boundary of all the results.                 *  2) If the start index is greater than the length of the current                 *     block, than there aren't really any results to return.                 */                if (startIndex >= blockStart + objectIDs.length) {                    // Return an empty array                    return EMPTY_BLOCK;                }                else {                    return objectIDs;                }            }        }    }    /**     * Returns the count associated with a ResultFilter query.     *     * @param key the key associted with the query.     */    private int getCount(QueryCacheKey key) {        // Synchronize on the sql query so that only one thread can try to load the        // count at once. This should help to prevent the database from        // becoming overloaded on busy sites.        StringBuffer internKey = new StringBuffer(key.getSql().length()+4);        internKey.append(key.getSql()).append(key.getBlockID());        synchronized (internKey.toString().intern()) {            Integer count = (Integer)factory.cacheManager.queryCache.get(key);            // Check in short-term cache if not in the long-term cache.            if (count == null && factory.cacheManager.isShortTermQueryCacheEnabled()) {                count = (Integer)factory.cacheManager.shortTermQueryCache.get(key);            }

⌨️ 快捷键说明

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