index hashes

This commit is contained in:
Athou
2013-04-14 18:28:48 +02:00
parent 0b96f5f95b
commit 86edd54a21
6 changed files with 60 additions and 4 deletions

View File

@@ -10,6 +10,8 @@ import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import com.commafeed.backend.model.Feed;
@@ -51,8 +53,12 @@ public class FeedDAO extends GenericDAO<Feed> {
}
public Feed findByUrl(String url) {
List<Feed> feeds = findByField(Feed_.url, url);
return Iterables.getFirst(feeds, null);
List<Feed> feeds = findByField(Feed_.urlHash, DigestUtils.sha1Hex(url));
Feed feed = Iterables.getFirst(feeds, null);
if (feed != null && StringUtils.equals(url, feed.getUrl())) {
return feed;
}
return null;
}
public Feed findByIdWithEntries(Long feedId, int offset, int limit) {

View File

@@ -7,9 +7,12 @@ import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.apache.commons.codec.digest.DigestUtils;
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedEntry_;
import com.google.api.client.util.Lists;
import com.uaihebert.model.EasyCriteria;
@Stateless
@@ -17,11 +20,23 @@ import com.uaihebert.model.EasyCriteria;
public class FeedEntryDAO extends GenericDAO<FeedEntry> {
public List<FeedEntry> findByGuids(List<String> guids) {
List<String> hashes = Lists.newArrayList();
for (String guid : guids) {
hashes.add(DigestUtils.sha1Hex(guid));
}
EasyCriteria<FeedEntry> criteria = createCriteria();
criteria.setDistinctTrue();
criteria.andStringIn(FeedEntry_.guid.getName(), guids);
criteria.andStringIn(FeedEntry_.guidHash.getName(), hashes);
criteria.leftJoinFetch(FeedEntry_.feeds.getName());
return criteria.getResultList();
List<FeedEntry> list = Lists.newArrayList();
for (FeedEntry entry : criteria.getResultList()) {
if (guids.contains(entry.getGuid())) {
list.add(entry);
}
}
return list;
}
public List<FeedEntry> findByFeed(Feed feed, int offset, int limit) {

View File

@@ -5,6 +5,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.jsoup.Jsoup;
@@ -49,6 +50,7 @@ public class FeedParser {
for (SyndEntry item : items) {
FeedEntry entry = new FeedEntry();
entry.setGuid(item.getUri());
entry.setGuidHash(DigestUtils.sha1Hex(item.getUri()));
entry.setUrl(item.getLink());
entry.setUpdated(getUpdateDate(item));

View File

@@ -21,12 +21,22 @@ import com.google.common.collect.Sets;
@SuppressWarnings("serial")
public class Feed extends AbstractModel {
/**
* The url of the feed
*/
@Column(length = 2048, nullable = false)
private String url;
@Column(length = 40, nullable = false)
@Index(name = "urlHash_index")
private String urlHash;
@Transient
private String title;
/**
* The url of the website, extracted from the feed
*/
@Column(length = 2048)
private String link;
@@ -129,4 +139,12 @@ public class Feed extends AbstractModel {
this.disabledUntil = disabledUntil;
}
public String getUrlHash() {
return urlHash;
}
public void setUrlHash(String urlHash) {
this.urlHash = urlHash;
}
}

View File

@@ -28,6 +28,10 @@ public class FeedEntry extends AbstractModel {
@Column(length = 2048, nullable = false)
private String guid;
@Column(length = 40, nullable = false)
@Index(name = "guidHash_index")
private String guidHash;
@ManyToMany
@JoinTable(name = "FEED_FEEDENTRIES", joinColumns = { @JoinColumn(name = "FEED_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "FEEDENTRY_ID", nullable = false, updatable = false) })
private Set<Feed> feeds = Sets.newHashSet();
@@ -106,4 +110,12 @@ public class FeedEntry extends AbstractModel {
this.content = content;
}
public String getGuidHash() {
return guidHash;
}
public void setGuidHash(String guidHash) {
this.guidHash = guidHash;
}
}

View File

@@ -7,6 +7,8 @@ import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.inject.Inject;
import org.apache.commons.codec.digest.DigestUtils;
import com.commafeed.backend.dao.FeedDAO;
import com.commafeed.backend.dao.FeedEntryDAO;
import com.commafeed.backend.dao.FeedEntryStatusDAO;
@@ -42,6 +44,7 @@ public class FeedSubscriptionService {
if (feed == null) {
feed = new Feed();
feed.setUrl(url);
feed.setUrlHash(DigestUtils.sha1Hex(url));
feedDAO.save(feed);
}