forked from Archives/Athou_commafeed
faster existing entry detection
This commit is contained in:
@@ -6,54 +6,41 @@ import java.util.List;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.JoinType;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.persistence.criteria.SetJoin;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.hibernate.Hibernate;
|
||||
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedEntry_;
|
||||
import com.commafeed.backend.model.Feed_;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
@Stateless
|
||||
public class FeedEntryDAO extends GenericDAO<FeedEntry> {
|
||||
|
||||
public List<FeedEntry> findByGuid(String guid) {
|
||||
String hash = DigestUtils.sha1Hex(guid);
|
||||
public static class EntryWithFeed {
|
||||
public FeedEntry entry;
|
||||
public Feed feed;
|
||||
|
||||
CriteriaQuery<FeedEntry> query = builder.createQuery(getType());
|
||||
Root<FeedEntry> root = query.from(getType());
|
||||
|
||||
query.where(builder.equal(root.get(FeedEntry_.guidHash), hash));
|
||||
|
||||
TypedQuery<FeedEntry> q = em.createQuery(query);
|
||||
List<FeedEntry> list = q.getResultList();
|
||||
return list;
|
||||
public EntryWithFeed(FeedEntry entry, Feed feed) {
|
||||
this.entry = entry;
|
||||
this.feed = feed;
|
||||
}
|
||||
}
|
||||
|
||||
public List<FeedEntry> findByGuids(List<String> guids) {
|
||||
List<String> hashes = Lists.newArrayList();
|
||||
for (String guid : guids) {
|
||||
hashes.add(DigestUtils.sha1Hex(guid));
|
||||
}
|
||||
public EntryWithFeed findExisting(String guid, String url, Long feedId) {
|
||||
|
||||
CriteriaQuery<FeedEntry> query = builder.createQuery(getType());
|
||||
Root<FeedEntry> root = query.from(getType());
|
||||
TypedQuery<EntryWithFeed> q = em.createNamedQuery(
|
||||
"EntryStatus.existing", EntryWithFeed.class);
|
||||
q.setParameter("guidHash", DigestUtils.sha1Hex(guid));
|
||||
q.setParameter("url", url);
|
||||
q.setParameter("feedId", feedId);
|
||||
|
||||
query.distinct(true);
|
||||
query.where(root.get(FeedEntry_.guidHash).in(hashes));
|
||||
root.fetch(FeedEntry_.feeds, JoinType.LEFT);
|
||||
|
||||
TypedQuery<FeedEntry> q = em.createQuery(query);
|
||||
List<FeedEntry> list = q.getResultList();
|
||||
for (FeedEntry entry : list) {
|
||||
Hibernate.initialize(entry.getFeeds());
|
||||
}
|
||||
return list;
|
||||
List<EntryWithFeed> resultList = q.getResultList();
|
||||
EntryWithFeed ewf = Iterables.getFirst(resultList, null);
|
||||
return ewf;
|
||||
}
|
||||
|
||||
public List<FeedEntry> findByFeed(Feed feed, int offset, int limit) {
|
||||
|
||||
@@ -6,10 +6,9 @@ import java.util.List;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
|
||||
import com.commafeed.backend.MetricsBean;
|
||||
import com.commafeed.backend.dao.FeedEntryDAO;
|
||||
import com.commafeed.backend.dao.FeedEntryDAO.EntryWithFeed;
|
||||
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
||||
import com.commafeed.backend.feeds.FeedUtils;
|
||||
@@ -38,11 +37,11 @@ public class FeedUpdateService {
|
||||
public void updateEntry(Feed feed, FeedEntry entry,
|
||||
List<FeedSubscription> subscriptions) {
|
||||
|
||||
FeedEntry foundEntry = FeedUtils.findEntry(
|
||||
feedEntryDAO.findByGuid(entry.getGuid()), entry);
|
||||
EntryWithFeed existing = feedEntryDAO.findExisting(entry.getGuid(),
|
||||
entry.getUrl(), feed.getId());
|
||||
|
||||
FeedEntry update = null;
|
||||
if (foundEntry == null) {
|
||||
if (existing == null) {
|
||||
FeedEntryContent content = entry.getContent();
|
||||
content.setTitle(FeedUtils.truncate(
|
||||
FeedUtils.handleContent(content.getTitle(), feed.getLink()),
|
||||
@@ -54,12 +53,9 @@ public class FeedUpdateService {
|
||||
entry.getFeeds().add(feed);
|
||||
|
||||
update = entry;
|
||||
} else {
|
||||
Hibernate.initialize(foundEntry.getFeeds());
|
||||
if (FeedUtils.findFeed(foundEntry.getFeeds(), feed) == null) {
|
||||
foundEntry.getFeeds().add(feed);
|
||||
update = foundEntry;
|
||||
}
|
||||
} else if (existing.feed == null) {
|
||||
existing.entry.getFeeds().add(feed);
|
||||
update = existing.entry;
|
||||
}
|
||||
|
||||
if (update != null) {
|
||||
|
||||
Reference in New Issue
Block a user