[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/forum ...
Christian Bauer
christian at hibernate.org
Thu Jan 3 04:06:04 EST 2008
User: cbauer
Date: 08/01/03 04:06:04
Modified: examples/wiki/src/main/org/jboss/seam/wiki/plugin/forum
ForumQueries.hbm.xml ReplyHome.java ForumDAO.java
TopicInfo.java
Log:
Forum queries performance optimization
Revision Changes Path
1.4 +20 -15 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/forum/ForumQueries.hbm.xml
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ForumQueries.hbm.xml
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/forum/ForumQueries.hbm.xml,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- ForumQueries.hbm.xml 25 Dec 2007 13:06:30 -0000 1.3
+++ ForumQueries.hbm.xml 3 Jan 2008 09:06:04 -0000 1.4
@@ -130,13 +130,20 @@
and c.createdOn > :lastLoginDate
]]></query>
+ <query name="forumTopicsCount">
+ select count(t) from WikiDocument t
+ where t.parent = :parentDir
+ and (t.headerMacrosString like '%forumPosting%' or t.headerMacrosString like '%forumStickyPosting%')
+ </query>
- <!-- TODO: HQL doesn't support CASE...WHEN -->
+
+ <!-- TODO: HQL doesn't support CASE...WHEN or arbitrary OUTER JOINs -->
<!-- TODO: We could optimize this a little if we'd also retrieve the CREATED_BY_USER_ID guy but
we'll hit the 2nd level cache anyway or we load them in batches... -->
<sql-query name="forumTopics">
<return class="org.jboss.seam.wiki.core.model.WikiDocument"/>
<return-scalar column="STICKY" type="integer"/>
+ <return-scalar column="HAS_REPLIES" type="boolean"/>
<return-scalar column="LAST_POST" type="timestamp"/>
<![CDATA[
select distinct
@@ -167,22 +174,20 @@
doc0.FOOTER_MACROS as FOOTER_MACROS,
case when (doc0.HEADER_MACROS like '%forumStickyPosting%') then 1 else 0 end as STICKY,
- case
- when
- (select max(c3.CREATED_ON) from WIKI_COMMENT c2 inner join WIKI_NODE c3 on c2.NODE_ID = c3.NODE_ID where c2.NS_THREAD in
- (select c4.NS_THREAD from WIKI_COMMENT c4 inner join WIKI_NODE c5 on c4.NODE_ID = c5.NODE_ID where c5.PARENT_NODE_ID = doc0.NODE_ID)
- ) is null
- then doc2.CREATED_ON
- else
- (select max(c3.CREATED_ON) from WIKI_COMMENT c2 inner join WIKI_NODE c3 on c2.NODE_ID = c3.NODE_ID where c2.NS_THREAD in
- (select c4.NS_THREAD from WIKI_COMMENT c4 inner join WIKI_NODE c5 on c4.NODE_ID = c5.NODE_ID where c5.PARENT_NODE_ID = doc0.NODE_ID)
- )
- end as LAST_POST
+ case when c0.CREATED_ON is null then false else true end as HAS_REPLIES,
+ case when c0.CREATED_ON is null then doc2.CREATED_ON else c0.CREATED_ON end as LAST_POST
+
from
- WIKI_DOCUMENT doc0 inner join WIKI_FILE doc1 on doc0.NODE_ID=doc1.NODE_ID inner join WIKI_NODE doc2 on doc0.NODE_ID=doc2.NODE_ID,
- WIKI_COMMENT c0 inner join WIKI_NODE c1 on c0.NODE_ID = c1.NODE_ID
+ WIKI_DOCUMENT doc0 inner join WIKI_FILE doc1 on doc0.NODE_ID=doc1.NODE_ID inner join WIKI_NODE doc2 on doc0.NODE_ID=doc2.NODE_ID
+ left outer join WIKI_NODE c0
+ on c0.NODE_ID in (select c1.NODE_ID from WIKI_COMMENT c1 where c1.NS_THREAD in
+ (select c2.NS_THREAD from WIKI_COMMENT c2 inner join WIKI_NODE c3 on c3.NODE_ID = c2.NODE_ID where c3.PARENT_NODE_ID = doc0.NODE_ID)
+ )
+ and c0.CREATED_ON = (select max(c5.CREATED_ON) from WIKI_COMMENT c4 inner join WIKI_NODE c5 on c4.NODE_ID = c5.NODE_ID where c4.NS_THREAD in
+ (select c6.NS_THREAD from WIKI_COMMENT c6 inner join WIKI_NODE c7 on c6.NODE_ID = c7.NODE_ID where c7.PARENT_NODE_ID = doc0.NODE_ID)
+ )
where
- doc2.PARENT_NODE_ID= :parentNodeId
+ doc2.PARENT_NODE_ID = :parentNodeId
and (doc0.HEADER_MACROS like '%forumPosting%' or doc0.HEADER_MACROS like '%forumStickyPosting%')
order
by STICKY desc, LAST_POST desc
1.4 +10 -7 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/forum/ReplyHome.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ReplyHome.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/forum/ReplyHome.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- ReplyHome.java 19 Dec 2007 04:29:25 -0000 1.3
+++ ReplyHome.java 3 Jan 2008 09:06:04 -0000 1.4
@@ -19,13 +19,7 @@
@Override
public void create() {
super.create();
-
- if (!getCurrentUser().isAdmin() && !getCurrentUser().isGuest()) {
- getLog().debug("### adding to read topics, forum id: "
- + documentHome.getParentNode().getId() + " topic id: " + documentHome.getInstance().getId());
- ForumTopicReadManager forumTopicReadManager = (ForumTopicReadManager)Component.getInstance("forumTopicReadManager");
- forumTopicReadManager.addTopicId(documentHome.getParentNode().getId(), documentHome.getInstance().getId());
- }
+ markTopicRead();
}
@Begin(flushMode = FlushModeType.MANUAL, join = true)
@@ -99,4 +93,13 @@
);
}
+ private void markTopicRead() {
+ if (!getCurrentUser().isAdmin() && !getCurrentUser().isGuest()) {
+ getLog().debug("adding to read topics, forum id: "
+ + documentHome.getParentNode().getId() + " topic id: " + documentHome.getInstance().getId());
+ ForumTopicReadManager forumTopicReadManager = (ForumTopicReadManager)Component.getInstance("forumTopicReadManager");
+ forumTopicReadManager.addTopicId(documentHome.getParentNode().getId(), documentHome.getInstance().getId());
+ }
+ }
+
}
1.4 +14 -12 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/forum/ForumDAO.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ForumDAO.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/forum/ForumDAO.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- ForumDAO.java 25 Dec 2007 13:06:30 -0000 1.3
+++ ForumDAO.java 3 Jan 2008 09:06:04 -0000 1.4
@@ -174,16 +174,11 @@
}
public Long findTopicCount(WikiDirectory forum) {
- ScrollableResults cursor =
- getSession(true).getNamedQuery("forumTopics")
- .setParameter("parentNodeId", forum.getId())
- .setComment("Counting forum topics")
- .scroll();
- cursor.last();
- Long count = cursor.getRowNumber() + 1l;
- cursor.close();
-
- return count;
+ return (Long)getSession(true).getNamedQuery("forumTopicsCount")
+ .setParameter("parentDir", forum)
+ .setComment("Retrieving forum topics count")
+ .setCacheable(true)
+ .uniqueResult();
}
public Map<Long, TopicInfo> findTopics(WikiDirectory forum, long firstResult, long maxResults) {
@@ -199,7 +194,7 @@
public Object transformTuple(Object[] result, String[] strings) {
topicInfoMap.put(
((WikiDocument)result[0]).getId(),
- new TopicInfo( (WikiDocument)result[0], (Integer)result[1])
+ new TopicInfo( (WikiDocument)result[0], (Integer)result[1], (Boolean)result[2])
);
return null;
}
@@ -207,8 +202,15 @@
}
)
.list();
+
+ List<Long> topicIdsWithReplies = new ArrayList<Long>();
+ for (Map.Entry<Long, TopicInfo> entry : topicInfoMap.entrySet()) {
+ if (entry.getValue().isReplies()) topicIdsWithReplies.add(entry.getKey());
+ }
+ if (topicIdsWithReplies.size() == 0) return topicInfoMap; // Early exit possible
+
getSession(true).getNamedQuery("forumTopicsReplies")
- .setParameterList("topicIds", topicInfoMap.keySet())
+ .setParameterList("topicIds", topicIdsWithReplies)
.setComment("Retrieving forum topic replies")
.setResultTransformer(
new ResultTransformer() {
1.2 +11 -1 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/forum/TopicInfo.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: TopicInfo.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/forum/TopicInfo.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- TopicInfo.java 19 Dec 2007 04:29:25 -0000 1.1
+++ TopicInfo.java 3 Jan 2008 09:06:04 -0000 1.2
@@ -8,12 +8,14 @@
private WikiDocument topic;
private boolean unread;
private boolean sticky;
+ private boolean replies;
private long numOfReplies;
private WikiComment lastComment;
- public TopicInfo(WikiDocument topic, Integer sticky) {
+ public TopicInfo(WikiDocument topic, Integer sticky, Boolean replies) {
this.topic = topic;
this.sticky = sticky != 0;
+ this.replies = replies;
}
public WikiDocument getTopic() {
@@ -36,6 +38,14 @@
this.sticky = sticky;
}
+ public boolean isReplies() {
+ return replies;
+ }
+
+ public void setReplies(boolean replies) {
+ this.replies = replies;
+ }
+
public long getNumOfReplies() {
return numOfReplies;
}
More information about the jboss-cvs-commits
mailing list