mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
added feeds cleanup method
This commit is contained in:
@@ -8,16 +8,33 @@ import javax.inject.Inject;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.commafeed.backend.dao.FeedDAO;
|
||||||
import com.commafeed.backend.dao.FeedEntryDAO;
|
import com.commafeed.backend.dao.FeedEntryDAO;
|
||||||
|
|
||||||
public class DatabaseCleaner {
|
public class DatabaseCleaner {
|
||||||
|
|
||||||
private static Logger log = LoggerFactory.getLogger(DatabaseCleaner.class);
|
private static Logger log = LoggerFactory.getLogger(DatabaseCleaner.class);
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
FeedDAO feedDAO;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
FeedEntryDAO feedEntryDAO;
|
FeedEntryDAO feedEntryDAO;
|
||||||
|
|
||||||
public long cleanOlderThan(long value, TimeUnit unit) {
|
public long cleanFeedsWithoutSubscriptions() {
|
||||||
|
|
||||||
|
long total = 0;
|
||||||
|
int deleted = -1;
|
||||||
|
do {
|
||||||
|
deleted = feedDAO.deleteWithoutSubscriptions(100);
|
||||||
|
total += deleted;
|
||||||
|
log.info("removed {} feeds without subscriptions", total);
|
||||||
|
} while (deleted != 0);
|
||||||
|
log.info("cleanup done: {} feeds without subscriptions deleted", total);
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long cleanEntriesOlderThan(long value, TimeUnit unit) {
|
||||||
Calendar cal = Calendar.getInstance();
|
Calendar cal = Calendar.getInstance();
|
||||||
cal.add(Calendar.MINUTE, -1 * (int) unit.toMinutes(value));
|
cal.add(Calendar.MINUTE, -1 * (int) unit.toMinutes(value));
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,15 @@ import javax.persistence.criteria.CriteriaQuery;
|
|||||||
import javax.persistence.criteria.JoinType;
|
import javax.persistence.criteria.JoinType;
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
import javax.persistence.criteria.Root;
|
import javax.persistence.criteria.Root;
|
||||||
|
import javax.persistence.criteria.SetJoin;
|
||||||
|
|
||||||
import org.apache.commons.codec.digest.DigestUtils;
|
import org.apache.commons.codec.digest.DigestUtils;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.commons.lang.time.DateUtils;
|
import org.apache.commons.lang.time.DateUtils;
|
||||||
|
|
||||||
import com.commafeed.backend.model.Feed;
|
import com.commafeed.backend.model.Feed;
|
||||||
|
import com.commafeed.backend.model.FeedSubscription;
|
||||||
|
import com.commafeed.backend.model.FeedSubscription_;
|
||||||
import com.commafeed.backend.model.Feed_;
|
import com.commafeed.backend.model.Feed_;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
@@ -92,4 +95,22 @@ public class FeedDAO extends GenericDAO<Feed> {
|
|||||||
public List<Feed> findByTopic(String topic) {
|
public List<Feed> findByTopic(String topic) {
|
||||||
return findByField(Feed_.pushTopicHash, DigestUtils.sha1Hex(topic));
|
return findByField(Feed_.pushTopicHash, DigestUtils.sha1Hex(topic));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int deleteWithoutSubscriptions(int max) {
|
||||||
|
CriteriaQuery<Feed> query = builder.createQuery(getType());
|
||||||
|
Root<Feed> root = query.from(getType());
|
||||||
|
|
||||||
|
SetJoin<Feed, FeedSubscription> join = root.join(Feed_.subscriptions,
|
||||||
|
JoinType.LEFT);
|
||||||
|
query.where(builder.isNull(join.get(FeedSubscription_.id)));
|
||||||
|
|
||||||
|
TypedQuery<Feed> q = em.createQuery(query);
|
||||||
|
q.setMaxResults(max);
|
||||||
|
|
||||||
|
List<Feed> list = q.getResultList();
|
||||||
|
int deleted = list.size();
|
||||||
|
delete(list);
|
||||||
|
return deleted;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.commafeed.backend.model;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.persistence.CascadeType;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.ManyToMany;
|
import javax.persistence.ManyToMany;
|
||||||
@@ -97,7 +98,7 @@ public class Feed extends AbstractModel {
|
|||||||
@Column(length = 40)
|
@Column(length = 40)
|
||||||
private String lastContentHash;
|
private String lastContentHash;
|
||||||
|
|
||||||
@ManyToMany(mappedBy = "feeds")
|
@ManyToMany(mappedBy = "feeds", cascade = CascadeType.REMOVE)
|
||||||
private Set<FeedEntry> entries = Sets.newHashSet();
|
private Set<FeedEntry> entries = Sets.newHashSet();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "feed")
|
@OneToMany(mappedBy = "feed")
|
||||||
|
|||||||
@@ -195,9 +195,13 @@ public class AdminREST extends AbstractResourceREST {
|
|||||||
|
|
||||||
@Path("/cleanup")
|
@Path("/cleanup")
|
||||||
@GET
|
@GET
|
||||||
public Response cleanup() {
|
public Response cleanup(@QueryParam("days") @DefaultValue("30") int days) {
|
||||||
long deleted = cleaner.cleanOlderThan(30, TimeUnit.DAYS);
|
Map<String, Long> map = Maps.newHashMap();
|
||||||
return Response.ok("ok: " + deleted).build();
|
map.put("feeds_without_subscriptions",
|
||||||
|
cleaner.cleanFeedsWithoutSubscriptions());
|
||||||
|
map.put("old entries",
|
||||||
|
cleaner.cleanEntriesOlderThan(days, TimeUnit.DAYS));
|
||||||
|
return Response.ok(map).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user