📄 dbforum.java
字号:
catch (Exception e) { e.printStackTrace(); }
}
long [] threads = threadsList.toArray();
// Add the thread block to cache
threadListCache.add(key, new CacheableLongArray(threads));
/**
* The actual block may be smaller than THREAD_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 + threads.length) {
// Return an empty array
return EMPTY_BLOCK;
}
else {
return threads;
}
}
}
/**
* Returns a block of messageID's 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[] getMessageBlock(String query, int startIndex) {
// First, discover what block number the results will be in.
int blockID = startIndex / MESSAGE_BLOCK_SIZE;
int blockStart = blockID * MESSAGE_BLOCK_SIZE;
// Now, check cache to see if the block is already cached. The key is
// simply the query plus the blockID.
String key = query + blockID;
CacheableLongArray longArray = (CacheableLongArray)messageListCache.get(key);
// If already in cache, return the block.
if (longArray != null) {
/**
* The actual block may be smaller than MESSAGE_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.
*/
long [] messages = longArray.getLongArray();
if (startIndex >= blockStart + messages.length) {
// Return an empty array
return EMPTY_BLOCK;
}
else {
return messages;
}
}
// Otherwise, we have to load up the block from the database.
else {
LongList messagesList = new LongList(MESSAGE_BLOCK_SIZE);
Connection con = null;
Statement stmt = null;
try {
con = ConnectionManager.getConnection();
stmt = con.createStatement();
// Set the maxium number of rows to end at the end of this block.
ConnectionManager.setMaxRows(stmt, MESSAGE_BLOCK_SIZE * (blockID+1));
ResultSet rs = stmt.executeQuery(query);
// Grab MESSAGE_BLOCK_ROWS rows at a time.
ConnectionManager.setFetchSize(rs, MESSAGE_BLOCK_SIZE);
// Many JDBC drivers don't implement scrollable cursors the real
// way, but instead load all results into memory. Looping through
// the results ourselves is more efficient.
for (int i=0; i<blockStart; i++) {
rs.next();
}
// Keep reading results until the result set is exaughsted or
// we come to the end of the block.
int count = 0;
while (rs.next() && count < MESSAGE_BLOCK_SIZE) {
messagesList.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(); }
}
long [] messages = messagesList.toArray();
// Add the message block to cache
messageListCache.add(key, new CacheableLongArray(messages));
/**
* The actual block may be smaller than MESSAGE_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 + messages.length) {
//Return an empty array
return EMPTY_BLOCK;
}
else {
return messages;
}
}
}
/**
* Loads properties from the database.
*/
private synchronized void loadPropertiesFromDb() {
this.properties = new Hashtable();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setLong(1, id);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
String name = rs.getString(1);
String value = rs.getString(2);
properties.put(name, value);
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Saves all extended forum properties to the database.
*/
private synchronized void savePropertiesToDb() {
boolean abortTransaction = false;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getTransactionConnection();
// Delete all old values.
pstmt = con.prepareStatement(DELETE_PROPERTIES);
pstmt.setLong(1, id);
pstmt.execute();
pstmt.close();
// Now insert new values.
pstmt = con.prepareStatement(INSERT_PROPERTY);
Iterator iter = properties.keySet().iterator();
while (iter.hasNext()) {
String name = (String)iter.next();
String value = (String)properties.get(name);
pstmt.setLong(1, id);
pstmt.setString(2, name);
pstmt.setString(3, value);
pstmt.executeUpdate();
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
abortTransaction = true;
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
ConnectionManager.closeTransactionConnection(con, abortTransaction);
}
}
/**
* Deletes a property from the db.
*/
private synchronized void deletePropertyFromDb(String name) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(DELETE_PROPERTY);
pstmt.setLong(1, id);
pstmt.setString(2, name);
pstmt.execute();
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Loads forum data from the database.
*/
private void loadFromDb() throws ForumNotFoundException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_FORUM);
pstmt.setLong(1,id);
ResultSet rs = pstmt.executeQuery();
if( !rs.next() ) {
throw new ForumNotFoundException("Forum " + getID() +
" could not be loaded from the database.");
}
this.id = rs.getLong(1);
this.name = rs.getString(2);
this.description = rs.getString(3);
this.modDefaultThreadValue = rs.getInt(4);
this.modDefaultMessageValue = rs.getInt(5);
this.modMinThreadValue = rs.getInt(6);
this.modMinMessageValue = rs.getInt(7);
this.modifiedDate =
new java.util.Date(Long.parseLong(rs.getString(8).trim()));
this.creationDate =
new java.util.Date(Long.parseLong(rs.getString(9).trim()));
// Now, load any extended forum properties
if (!LAZY_PROP_LOADING) {
pstmt.close();
properties = new Hashtable();
pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setLong(1, id);
rs = pstmt.executeQuery();
while(rs.next()) {
String name = rs.getString(1);
String value = rs.getString(2);
properties.put(name, value);
}
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
throw new ForumNotFoundException("Forum " + getID() +
" could not be loaded from the database.");
}
catch (NumberFormatException nfe) {
System.err.println("WARNING: In DbForum.loadFromDb() -- there " +
"was an error parsing the dates returned from the database. Ensure " +
"that they're being stored correctly.");
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Inserts a new record into the database.
*/
private void insertIntoDb() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(ADD_FORUM);
pstmt.setLong(1,id);
pstmt.setString(2,name);
pstmt.setString(3,description);
pstmt.setInt(4, modDefaultThreadValue);
pstmt.setInt(5, modDefaultMessageValue);
pstmt.setInt(6, modMinThreadValue);
pstmt.setInt(7, modMinMessageValue);
pstmt.setString(8, StringUtils.dateToMillis(creationDate));
pstmt.setString(9, StringUtils.dateToMillis(modifiedDate));
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Saves forum data to the database.
*/
private synchronized void saveToDb() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(SAVE_FORUM);
pstmt.setString(1, name);
pstmt.setString(2, description);
pstmt.setInt(3, modDefaultThreadValue);
pstmt.setInt(4, modDefaultMessageValue);
pstmt.setInt(5, modMinThreadValue);
pstmt.setInt(6, modMinMessageValue);
pstmt.setString(7, StringUtils.dateToMillis(creationDate));
pstmt.setString(8, StringUtils.dateToMillis(modifiedDate));
pstmt.setLong(9, id);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -