[Logo] JForum - Powering Communities
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Top Downloads] Top Downloads   [Groups] Back to home page 
[Register] Register /  [Login] Login 


The final version of JForum 2.7.0 has been released with lots of improvements. Read all about it here

Topics by forum RSS feed
Forum Index » Developer Forum
Author Message
rostek


Joined: 2012/6/20
Messages: 2
Offline
Hi, I downloaded JForum and started changing it to my needs. First was making urls more friendly, now they contain info about post/topic title but it's not important. When I tested friendly urls with pagination, I saw results on subsequent pages do not appear. I looked at code and my question is, what sense is in this method:

public static List<Topic> topicsByForum(int forumId, int start)
	{
		TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
		int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
		List<Topic> topics;
		
		// Try to get the first's page of topics from the cache
		if (SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) {
			topics = TopicRepository.getTopics(forumId);

			if (topics.isEmpty() || !TopicRepository.isLoaded(forumId)) {
				synchronized (MUTEXT) {
					if (topics.isEmpty() || !TopicRepository.isLoaded(forumId)) {
						topics = tm.selectAllByForum(forumId);
						TopicRepository.addAll(forumId, topics);
					}
				}
			}
		}
		else {
			topics = tm.selectAllByForumByLimit(forumId, start, topicsPerPage);
		}
		
		int size = topics.size();
		
		while (size < start) {
			start -= topicsPerPage;
		}
		if (start < 0) {
			start = 0;
		}
		
		return topics.subList(start, (size < start + topicsPerPage) ? size : start + topicsPerPage);
	}


in TopicsCommon class. What if in cache I have 20 results and go to records on 30th page? What is the subListing for, if I would not use cache and get exact list from database?

Thanks.
andowson


Joined: 2011/6/30
Messages: 223
Offline
TopicsCommon.topicsByForum(int forumId, int start) is used to fetch topics in forum forumId beginning from the index start.
For example:
TopicsCommon.topicsByForum(1, 0) returns topics start from index 0 in forum where forum_id=1.
How many topics returned is determined by topicsPerPage.

And if you enable cache of topics, JForum will look for them in the topic repository(cache) first. If the topic repository for the forum forumId is empty, JForum will load topics from database and cache it.
JForum try to load all the topics from database first, and then use subList to return only topicsPerPage line of records from the start index.

If topicsPerPage = 15, and you only have 20 topics in cache for the forum, the JForum will display the last 5 topics in the 2nd page.

If you disable cache of topics, JForum will fetch topics from the database directly. But only with limit number of records (topicsPerPage) returned.

rostek


Joined: 2012/6/20
Messages: 2
Offline
Thank you for the answer. Of course I understand the idea, but code does not do that.

// If the cache is full, remove the eldest element
				int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
				if (!contains && forumTopicsList.size() + 1 > topicsPerPage) {
					forumTopicsList.removeLast();


in TopicRepository:290, so cache can actually contain only ConfigKeys.TOPICS_PER_PAGE newest topics.
When you get topics = TopicRepository.getTopics(forumId); in TopicsCommon:101 you get newest topics from cache. How can you get topic for, for example start=1000?

andowson


Joined: 2011/6/30
Messages: 223
Offline
You are right. This is a bug.
The quick way to fix it is to remove this code fragment for checking with topicsPerPage limit.

I've committed a fix for it as revision 208.
The idea is to check if (start + topicsPerPage >= topicCacheSize) then reload the topics from database again.
topicCacheSize is a new configurable value in SystemGlobals.properties and the default value is 45:
topic.cache.size = 45

 
Forum Index » Developer Forum
Go to:   
Mobile view
Powered by JForum 2.7.0 © 2020 JForum Team • Maintained by Andowson Chang and Ulf Dittmer