mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
faster queries for categories
This commit is contained in:
@@ -11,6 +11,7 @@ import com.commafeed.backend.model.FeedCategory;
|
|||||||
import com.commafeed.backend.model.User;
|
import com.commafeed.backend.model.User;
|
||||||
import com.commafeed.frontend.utils.ModelFactory.MF;
|
import com.commafeed.frontend.utils.ModelFactory.MF;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import com.uaihebert.model.EasyCriteria;
|
import com.uaihebert.model.EasyCriteria;
|
||||||
|
|
||||||
@Stateless
|
@Stateless
|
||||||
@@ -47,6 +48,18 @@ public class FeedCategoryService extends GenericDAO<FeedCategory, Long> {
|
|||||||
return category;
|
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) {
|
public boolean isChild(FeedCategory child, FeedCategory parent) {
|
||||||
if (parent == null) {
|
if (parent == null) {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import javax.persistence.TypedQuery;
|
|||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
import com.commafeed.backend.model.Feed;
|
import com.commafeed.backend.model.Feed;
|
||||||
|
import com.commafeed.backend.model.FeedCategory;
|
||||||
import com.commafeed.backend.model.FeedEntry;
|
import com.commafeed.backend.model.FeedEntry;
|
||||||
import com.commafeed.backend.model.User;
|
import com.commafeed.backend.model.User;
|
||||||
import com.commafeed.frontend.utils.ModelFactory.MF;
|
import com.commafeed.frontend.utils.ModelFactory.MF;
|
||||||
@@ -85,4 +86,30 @@ public class FeedEntryService extends GenericDAO<FeedEntry, Long> {
|
|||||||
}
|
}
|
||||||
return query.getResultList();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,6 @@ import java.util.List;
|
|||||||
|
|
||||||
import javax.ejb.Stateless;
|
import javax.ejb.Stateless;
|
||||||
import javax.inject.Inject;
|
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.Feed;
|
||||||
import com.commafeed.backend.model.FeedCategory;
|
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.backend.model.User;
|
||||||
import com.commafeed.frontend.utils.ModelFactory.MF;
|
import com.commafeed.frontend.utils.ModelFactory.MF;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.uaihebert.factory.EasyCriteriaFactory;
|
import com.uaihebert.factory.EasyCriteriaFactory;
|
||||||
import com.uaihebert.model.EasyCriteria;
|
import com.uaihebert.model.EasyCriteria;
|
||||||
|
|
||||||
@@ -51,35 +47,4 @@ public class FeedSubscriptionService extends GenericDAO<FeedSubscription, Long>
|
|||||||
return criteria.getResultList();
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,13 @@
|
|||||||
package com.commafeed.frontend.rest.resources;
|
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.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.ws.rs.DefaultValue;
|
import javax.ws.rs.DefaultValue;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
|
|
||||||
import org.apache.commons.lang.ObjectUtils;
|
|
||||||
|
|
||||||
import com.commafeed.backend.model.Feed;
|
import com.commafeed.backend.model.Feed;
|
||||||
import com.commafeed.backend.model.FeedCategory;
|
import com.commafeed.backend.model.FeedCategory;
|
||||||
import com.commafeed.backend.model.FeedEntry;
|
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.backend.model.FeedSubscription;
|
||||||
import com.commafeed.frontend.model.Entries;
|
import com.commafeed.frontend.model.Entries;
|
||||||
import com.commafeed.frontend.model.Entry;
|
import com.commafeed.frontend.model.Entry;
|
||||||
|
import com.google.common.base.Function;
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
|
||||||
@Path("entries")
|
@Path("entries")
|
||||||
public class EntriesREST extends AbstractREST {
|
public class EntriesREST extends AbstractREST {
|
||||||
@@ -58,33 +56,28 @@ public class EntriesREST extends AbstractREST {
|
|||||||
buildEntries(subscription, offset, limit, unreadOnly));
|
buildEntries(subscription, offset, limit, unreadOnly));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
FeedCategory feedCategory = ALL.equals(id) ? null
|
FeedCategory feedCategory = null;
|
||||||
: feedCategoryService.findById(getUser(), Long.valueOf(id));
|
if (!ALL.equals(id)) {
|
||||||
Collection<FeedSubscription> subscriptions = ALL.equals(id) ? feedSubscriptionService
|
feedCategory = new FeedCategory();
|
||||||
.findAll(getUser()) : feedSubscriptionService
|
feedCategory.setId(Long.valueOf(id));
|
||||||
.findWithCategory(getUser(), feedCategory);
|
}
|
||||||
|
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());
|
entries.setName(ALL.equals(id) ? ALL : feedCategory.getName());
|
||||||
for (FeedSubscription subscription : subscriptions) {
|
entries.getEntries().addAll(
|
||||||
entries.getEntries().addAll(
|
buildEntries(childrenCategories, subMapping, offset, limit,
|
||||||
buildEntries(subscription, offset, limit, unreadOnly));
|
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;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,22 +89,39 @@ public class EntriesREST extends AbstractREST {
|
|||||||
List<FeedEntry> unreadEntries = feedEntryService.getEntries(
|
List<FeedEntry> unreadEntries = feedEntryService.getEntries(
|
||||||
subscription.getFeed(), getUser(), true, offset, limit);
|
subscription.getFeed(), getUser(), true, offset, limit);
|
||||||
for (FeedEntry feedEntry : unreadEntries) {
|
for (FeedEntry feedEntry : unreadEntries) {
|
||||||
Entry entry = buildEntry(feedEntry);
|
entries.add(populateEntry(buildEntry(feedEntry), subscription,
|
||||||
entry.setFeedName(subscription.getTitle());
|
true));
|
||||||
entry.setFeedId(String.valueOf(subscription.getId()));
|
|
||||||
entry.setRead(true);
|
|
||||||
entries.add(entry);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<FeedEntry> readEntries = feedEntryService.getEntries(
|
List<FeedEntry> readEntries = feedEntryService.getEntries(
|
||||||
subscription.getFeed(), getUser(), false, offset, limit);
|
subscription.getFeed(), getUser(), false, offset, limit);
|
||||||
for (FeedEntry feedEntry : readEntries) {
|
for (FeedEntry feedEntry : readEntries) {
|
||||||
Entry entry = buildEntry(feedEntry);
|
entries.add(populateEntry(buildEntry(feedEntry), subscription,
|
||||||
entry.setFeedName(subscription.getTitle());
|
false));
|
||||||
entry.setFeedId(String.valueOf(subscription.getId()));
|
}
|
||||||
entry.setRead(false);
|
return entries;
|
||||||
entries.add(entry);
|
}
|
||||||
|
|
||||||
|
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;
|
return entries;
|
||||||
}
|
}
|
||||||
@@ -127,6 +137,13 @@ public class EntriesREST extends AbstractREST {
|
|||||||
return entry;
|
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")
|
@Path("mark")
|
||||||
@GET
|
@GET
|
||||||
public void mark(@QueryParam("type") Type type,
|
public void mark(@QueryParam("type") Type type,
|
||||||
|
|||||||
@@ -6,12 +6,19 @@
|
|||||||
http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
|
http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
|
||||||
|
|
||||||
<named-query name="Entry.byGuids">
|
<named-query name="Entry.byGuids">
|
||||||
<query>select e from FeedEntry e where e.guid in :guids</query>
|
<query>select e from FeedEntry e where e.guid in :guids order by e.updated desc</query>
|
||||||
</named-query>
|
</named-query>
|
||||||
<named-query name="Entry.unreadByFeed">
|
<named-query name="Entry.unreadByFeed">
|
||||||
<query>select e from FeedEntry e where e.feed=:feed and not exists (select s from FeedEntryStatus s where s.entry = e and s.user =:user and s.read=true)</query>
|
<query>select e from FeedEntry e where e.feed=:feed and not exists (select s from FeedEntryStatus s where s.entry = e and s.user =:user and s.read=true) order by e.updated desc</query>
|
||||||
</named-query>
|
</named-query>
|
||||||
<named-query name="Entry.readByFeed">
|
<named-query name="Entry.readByFeed">
|
||||||
<query>select e from FeedEntry e where e.feed=:feed and exists (select s from FeedEntryStatus s where s.entry = e and s.user =:user and s.read=true)</query>
|
<query>select e from FeedEntry e where e.feed=:feed and exists (select s from FeedEntryStatus s where s.entry = e and s.user =:user and s.read=true) order by e.updated desc</query>
|
||||||
|
</named-query>
|
||||||
|
|
||||||
|
<named-query name="Entry.unreadByCategories">
|
||||||
|
<query>select e from FeedEntry e where exists (select s from FeedSubscription s where s.user=:user and s.feed = e.feed and s.category in :categories) and not exists (select s from FeedEntryStatus s where s.entry = e and s.user =:user and s.read=true) order by e.updated desc</query>
|
||||||
|
</named-query>
|
||||||
|
<named-query name="Entry.readByCategories">
|
||||||
|
<query>select e from FeedEntry e where exists (select s from FeedSubscription s where s.user=:user and s.feed = e.feed and s.category in :categories) and exists (select s from FeedEntryStatus s where s.entry = e and s.user =:user and s.read=true) order by e.updated desc</query>
|
||||||
</named-query>
|
</named-query>
|
||||||
</entity-mappings>
|
</entity-mappings>
|
||||||
@@ -102,7 +102,7 @@ module.controller('FeedListCtrl', function($scope, $routeParams, $http,
|
|||||||
$scope.refreshList();
|
$scope.refreshList();
|
||||||
});
|
});
|
||||||
|
|
||||||
$scope.limit = 10;
|
$scope.limit = 20;
|
||||||
$scope.busy = false;
|
$scope.busy = false;
|
||||||
$scope.hasMore = true;
|
$scope.hasMore = true;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user