public ForumThreadIterator threads(ResultFilter resultFilter) { //根据传入的resultFilter构造一个查询SQL语句(*注1) String query = getThreadListSQL(resultFilter, false); //返回一个确定返回的id列表,这里缺省大小是400个,例如,我要看1~15条纪录 // threadBlock的length是400,从1~400(*注2) long [] threadBlock = getBlock(query.toString(), resultFilter.getStartIndex()); int startIndex = resultFilter.getStartIndex(); int endIndex; // If number of results is set to inifinite, set endIndex to the total // number of threads in the forum. if (resultFilter.getNumResults() == ResultFilter.NULL_INT) { endIndex = (int)getThreadCount(resultFilter); } else { endIndex = resultFilter.getNumResults() + startIndex; } //嘿嘿,理解了上面几句代码先,然后来看这个iterator return new ForumThreadBlockIterator(threadBlock, query.toString(), startIndex, endIndex, this.id, factory); }
OK,关于(注1) 我分析了getThreadListSQL,这里是构造了一个SQL查询语句,这个语句前面的一些内容是: SELECT jiveThread.threadID FROM jiveThread,xxxxx 那些xxxxx无非是一些查询条件,是根据resultFilter中的查询条件而自动生成的。 原来,如果执行了这个SQL的作用只能取回满足条件的thread的ID号。
con = ConnectionManager.getConnection(); stmt = ConnectionManager.createScrollableStatement(con); // Set the maximum number of rows to end at the end of this block. ConnectionManager.setMaxRows(stmt, BLOCK_SIZE * (blockID+1)); ResultSet rs = stmt.executeQuery(query); // Grab BLOCK_SIZE rows at a time. ConnectionManager.setFetchSize(rs, BLOCK_SIZE); //一次最多取BLOCK_SIZE条,也就是400条 // Position the cursor right before the first row that we're insterested in. ConnectionManager.scrollResultSet(rs, blockStart);// 滚动游标到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 < BLOCK_SIZE) { objectList.add(rs.getLong(1)); count++; }