dbtreewalker.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 217 行
JAVA
217 行
/** * $RCSfile: DbTreeWalker.java,v $ * $Revision: 1.5 $ * $Date: 2002/05/13 19:41:23 $ * * Copyright (C) 1999-2002 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.sql.*;import java.util.Iterator;import java.io.*;import com.jivesoftware.forum.*;import com.jivesoftware.util.*;/** * Database implementation of the TreeWalker interface. It caches the tree * structure in memory for fastest access.<p> * * One important caveat is the following: we assumes that no child message * will have a creation date that is earlier in time than its parent message. * The assumption lets us make some big optimization gains and is safe as long * as all the data is created in Jive Forums. The only possible problem is if * data is imported from other forum apps that don't follow this behavior (not * very likely). * * @author Matt Tucker */public class DbTreeWalker implements TreeWalker, Serializable { /** DATABASE QUERIES **/ private static final String MESSAGE_COUNT = "SELECT count(*) FROM jiveMessage WHERE threadID=?"; private static final String GET_MESSAGES = "SELECT messageID, parentMessageID, creationDate FROM jiveMessage " + "WHERE threadID=? AND parentMessageID IS NOT NULL " + "ORDER BY creationDate ASC"; private long threadID; private LongTree tree; private transient DbForumFactory factory; /** * Creates a new TreeWalker instance. */ public DbTreeWalker(DbForumThread thread) { this.threadID = thread.getID(); factory = DbForumFactory.getInstance(); // Now, build the rest of the tree. Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); // First, get the count. pstmt = con.prepareStatement(MESSAGE_COUNT); pstmt.setLong(1, thread.getID()); ResultSet rs = pstmt.executeQuery(); rs.next(); int numMessages = rs.getInt(1); pstmt.close(); // Create the tree, set the root. tree = new LongTree(thread.getRootMessage().getID(), numMessages); // Now, load the actual results. pstmt = con.prepareStatement(GET_MESSAGES); pstmt.setLong(1, thread.getID()); rs = pstmt.executeQuery(); int count = 1; // Keep reading items into the tree until we run out of db // results, or we hit the max number of elements that we defined // for the tree. Only very rarely should the count change while // reading in the tree. while(rs.next() && count < numMessages) { count++; long messageID = rs.getLong(1); long parentMessageID = rs.getLong(2); tree.addChild(parentMessageID, messageID); } } catch (SQLException sqle) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); factory = DbForumFactory.getInstance(); } //FROM THE TREEWALKER INTERFACE// public ForumMessage getRoot() { ForumThread thread = null; try { thread = factory.cacheManager.getForumThread(threadID); } catch (Exception e) { return null; } return thread.getRootMessage(); } public int getChildCount(ForumMessage parent) { return tree.getChildCount(parent.getID()); } public ForumMessage getParent(ForumMessage message) throws ForumMessageNotFoundException { long parentID = tree.getParent(message.getID()); if (parentID == -1) { throw new ForumMessageNotFoundException(); } else { ForumThread thread = null; try { thread = factory.cacheManager.getForumThread(threadID); } catch (Exception e) { throw new ForumMessageNotFoundException(); } return thread.getMessage(parentID); } } public ForumMessage getChild(ForumMessage message, int index) throws ForumMessageNotFoundException { long childID = tree.getChild(message.getID(), index); if (childID == -1) { throw new ForumMessageNotFoundException(); } else { ForumThread thread = null; try { thread = factory.cacheManager.getForumThread(threadID); } catch (Exception e) { throw new ForumMessageNotFoundException(); } return thread.getMessage(childID); } } public Iterator children(ForumMessage parent) { long [] children = tree.getChildren(parent.getID()); return new DatabaseObjectIterator(JiveGlobals.MESSAGE, children, parent.getForumThread()); } public Iterator recursiveChildren(ForumMessage parent) { long [] messages = tree.getRecursiveChildren(parent.getID()); return new DatabaseObjectIterator(JiveGlobals.MESSAGE, messages, parent.getForumThread()); } public int getMessageDepth(ForumMessage message) { int depth = tree.getDepth(message.getID()); if (depth == -1) { throw new IllegalArgumentException("Message " + message.getID() + " does not belong to this thread."); } return depth; } public int getRecursiveChildCount(ForumMessage parent) { //Call the recursive method. return getRecursiveChildCount(parent.getID()); } public int getIndexOfChild(ForumMessage parent, ForumMessage child) { return tree.getIndexOfChild(parent.getID(), child.getID()); } public boolean isLeaf(ForumMessage message) { return tree.isLeaf(message.getID()); } //OTHER METHODS// public int getSize() { int size = 0; size += CacheSizes.sizeOfObject(); size += tree.getCachedSize(); size += CacheSizes.sizeOfLong(); size += CacheSizes.sizeOfLong(); size += CacheSizes.sizeOfObject(); return size; } private int getRecursiveChildCount(long parentID) { int numChildren = 0; int num = tree.getChildCount(parentID); numChildren += num; for (int i=0; i<num; i++) { long childID = tree.getChild(parentID, i); if (childID != -1) { numChildren += getRecursiveChildCount(childID); } } return numChildren; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?