faster queries for categories

This commit is contained in:
Jeremie Panzer
2013-03-26 09:54:59 +01:00
parent def27c5f9f
commit 9da424a9f0
6 changed files with 106 additions and 77 deletions

View File

@@ -11,6 +11,7 @@ import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.User;
import com.commafeed.frontend.utils.ModelFactory.MF;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.uaihebert.model.EasyCriteria;
@Stateless
@@ -47,6 +48,18 @@ public class FeedCategoryService extends GenericDAO<FeedCategory, Long> {
return category;
}
public List<FeedCategory> findAllChildrenCategories(User user,
FeedCategory parent) {
List<FeedCategory> list = Lists.newArrayList();
List<FeedCategory> all = findAll(user);
for (FeedCategory cat : all) {
if (isChild(cat, parent)) {
list.add(cat);
}
}
return list;
}
public boolean isChild(FeedCategory child, FeedCategory parent) {
if (parent == null) {
return true;

View File

@@ -11,6 +11,7 @@ import javax.persistence.TypedQuery;
import org.apache.commons.lang.StringUtils;
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.User;
import com.commafeed.frontend.utils.ModelFactory.MF;
@@ -85,4 +86,30 @@ public class FeedEntryService extends GenericDAO<FeedEntry, Long> {
}
return query.getResultList();
}
public List<FeedEntry> getEntries(List<FeedCategory> categories, User user,
boolean read) {
return getEntries(categories, user, read, -1, -1);
}
public List<FeedEntry> getEntries(List<FeedCategory> categories, User user,
boolean read, int offset, int limit) {
String queryName = null;
if (read) {
queryName = "Entry.readByCategories";
} else {
queryName = "Entry.unreadByCategories";
}
TypedQuery<FeedEntry> query = em.createNamedQuery(queryName,
FeedEntry.class);
query.setParameter("categories", categories);
query.setParameter("user", user);
if (offset > -1) {
query.setFirstResult(offset);
}
if (limit > -1) {
query.setMaxResults(limit);
}
return query.getResultList();
}
}

View File

@@ -4,9 +4,6 @@ import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.TypedQuery;
import org.apache.commons.lang.ObjectUtils;
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedCategory;
@@ -14,7 +11,6 @@ import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.User;
import com.commafeed.frontend.utils.ModelFactory.MF;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.uaihebert.factory.EasyCriteriaFactory;
import com.uaihebert.model.EasyCriteria;
@@ -51,35 +47,4 @@ public class FeedSubscriptionService extends GenericDAO<FeedSubscription, Long>
return criteria.getResultList();
}
public List<FeedSubscription> findWithCategory(User user,
FeedCategory category) {
List<FeedCategory> categories = Lists.newArrayList();
for (FeedCategory c : feedCategoryService.findAll(user)) {
if (isChild(c, category)) {
categories.add(c);
}
}
String query = "select s from FeedSubscription s where s.user=:user and s.category in :categories";
TypedQuery<FeedSubscription> typedQuery = em.createQuery(query,
FeedSubscription.class);
typedQuery.setParameter("user", user);
typedQuery.setParameter("categories", categories);
return typedQuery.getResultList();
}
private boolean isChild(FeedCategory c, FeedCategory category) {
boolean isChild = false;
while (c != null) {
if (ObjectUtils.equals(c.getId(), category.getId())) {
isChild = true;
break;
}
c = c.getParent();
}
return isChild;
}
}