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;
}
}

View File

@@ -1,17 +1,13 @@
package com.commafeed.frontend.rest.resources;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import org.apache.commons.lang.ObjectUtils;
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedEntry;
@@ -19,8 +15,10 @@ import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.frontend.model.Entries;
import com.commafeed.frontend.model.Entry;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@Path("entries")
public class EntriesREST extends AbstractREST {
@@ -58,33 +56,28 @@ public class EntriesREST extends AbstractREST {
buildEntries(subscription, offset, limit, unreadOnly));
} else {
FeedCategory feedCategory = ALL.equals(id) ? null
: feedCategoryService.findById(getUser(), Long.valueOf(id));
Collection<FeedSubscription> subscriptions = ALL.equals(id) ? feedSubscriptionService
.findAll(getUser()) : feedSubscriptionService
.findWithCategory(getUser(), feedCategory);
FeedCategory feedCategory = null;
if (!ALL.equals(id)) {
feedCategory = new FeedCategory();
feedCategory.setId(Long.valueOf(id));
}
List<FeedCategory> childrenCategories = feedCategoryService
.findAllChildrenCategories(getUser(), feedCategory);
Map<Long, FeedSubscription> subMapping = Maps.uniqueIndex(
feedSubscriptionService.findAll(getUser()),
new Function<FeedSubscription, Long>() {
public Long apply(FeedSubscription sub) {
return sub.getFeed().getId();
}
});
entries.setName(ALL.equals(id) ? ALL : feedCategory.getName());
for (FeedSubscription subscription : subscriptions) {
entries.getEntries().addAll(
buildEntries(subscription, offset, limit, unreadOnly));
}
entries.getEntries().addAll(
buildEntries(childrenCategories, subMapping, offset, limit,
unreadOnly));
}
Collections.sort(entries.getEntries(), new Comparator<Entry>() {
@Override
public int compare(Entry e1, Entry e2) {
return ObjectUtils.compare(e2.getDate(), e1.getDate());
}
});
if (limit > -1) {
int size = entries.getEntries().size();
int to = Math.min(size, limit);
List<Entry> subList = entries.getEntries().subList(0, to);
entries.setEntries(Lists.newArrayList(subList));
}
return entries;
}
@@ -96,22 +89,39 @@ public class EntriesREST extends AbstractREST {
List<FeedEntry> unreadEntries = feedEntryService.getEntries(
subscription.getFeed(), getUser(), true, offset, limit);
for (FeedEntry feedEntry : unreadEntries) {
Entry entry = buildEntry(feedEntry);
entry.setFeedName(subscription.getTitle());
entry.setFeedId(String.valueOf(subscription.getId()));
entry.setRead(true);
entries.add(entry);
entries.add(populateEntry(buildEntry(feedEntry), subscription,
true));
}
}
List<FeedEntry> readEntries = feedEntryService.getEntries(
subscription.getFeed(), getUser(), false, offset, limit);
for (FeedEntry feedEntry : readEntries) {
Entry entry = buildEntry(feedEntry);
entry.setFeedName(subscription.getTitle());
entry.setFeedId(String.valueOf(subscription.getId()));
entry.setRead(false);
entries.add(entry);
entries.add(populateEntry(buildEntry(feedEntry), subscription,
false));
}
return entries;
}
private List<Entry> buildEntries(List<FeedCategory> categories,
Map<Long, FeedSubscription> subMapping, int offset, int limit,
boolean unreadOnly) {
List<Entry> entries = Lists.newArrayList();
if (!unreadOnly) {
List<FeedEntry> unreadEntries = feedEntryService.getEntries(
categories, getUser(), true, offset, limit);
for (FeedEntry feedEntry : unreadEntries) {
entries.add(populateEntry(buildEntry(feedEntry),
subMapping.get(feedEntry.getFeed().getId()), true));
}
}
List<FeedEntry> readEntries = feedEntryService.getEntries(categories,
getUser(), false, offset, limit);
for (FeedEntry feedEntry : readEntries) {
entries.add(populateEntry(buildEntry(feedEntry),
subMapping.get(feedEntry.getFeed().getId()), false));
}
return entries;
}
@@ -127,6 +137,13 @@ public class EntriesREST extends AbstractREST {
return entry;
}
private Entry populateEntry(Entry entry, FeedSubscription sub, boolean read) {
entry.setFeedName(sub.getTitle());
entry.setFeedId(String.valueOf(sub.getId()));
entry.setRead(read);
return entry;
}
@Path("mark")
@GET
public void mark(@QueryParam("type") Type type,