2013-06-16 11:48:23 +02:00
|
|
|
package com.commafeed.backend;
|
|
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
2013-07-02 16:10:59 +02:00
|
|
|
import java.util.List;
|
2013-06-16 11:48:23 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
2013-06-18 13:28:15 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2013-06-19 17:01:28 +02:00
|
|
|
import com.commafeed.backend.dao.FeedDAO;
|
2013-06-19 13:29:56 +02:00
|
|
|
import com.commafeed.backend.dao.FeedEntryDAO;
|
2013-07-02 16:10:59 +02:00
|
|
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
|
|
|
|
import com.commafeed.backend.model.Feed;
|
|
|
|
|
import com.commafeed.backend.model.FeedSubscription;
|
2013-06-16 11:48:23 +02:00
|
|
|
|
|
|
|
|
public class DatabaseCleaner {
|
|
|
|
|
|
2013-06-18 13:28:15 +02:00
|
|
|
private static Logger log = LoggerFactory.getLogger(DatabaseCleaner.class);
|
|
|
|
|
|
2013-06-19 17:01:28 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedDAO feedDAO;
|
|
|
|
|
|
2013-06-16 11:48:23 +02:00
|
|
|
@Inject
|
2013-06-19 13:29:56 +02:00
|
|
|
FeedEntryDAO feedEntryDAO;
|
2013-06-16 11:48:23 +02:00
|
|
|
|
2013-07-02 16:10:59 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedSubscriptionDAO feedSubscriptionDAO;
|
|
|
|
|
|
2013-06-19 17:01:28 +02:00
|
|
|
public long cleanFeedsWithoutSubscriptions() {
|
|
|
|
|
|
|
|
|
|
long total = 0;
|
|
|
|
|
int deleted = -1;
|
|
|
|
|
do {
|
2013-07-05 00:23:00 +02:00
|
|
|
deleted = feedDAO.deleteWithoutSubscriptions(1);
|
2013-06-19 17:01:28 +02:00
|
|
|
total += deleted;
|
|
|
|
|
log.info("removed {} feeds without subscriptions", total);
|
|
|
|
|
} while (deleted != 0);
|
|
|
|
|
log.info("cleanup done: {} feeds without subscriptions deleted", total);
|
|
|
|
|
return total;
|
|
|
|
|
}
|
2013-07-04 21:50:39 +02:00
|
|
|
|
|
|
|
|
public long cleanEntriesWithoutFeeds() {
|
|
|
|
|
|
|
|
|
|
long total = 0;
|
|
|
|
|
int deleted = -1;
|
|
|
|
|
do {
|
|
|
|
|
deleted = feedEntryDAO.deleteWithoutFeeds(100);
|
|
|
|
|
total += deleted;
|
|
|
|
|
log.info("removed {} entries without feeds", total);
|
|
|
|
|
} while (deleted != 0);
|
|
|
|
|
log.info("cleanup done: {} entries without feeds deleted", total);
|
|
|
|
|
return total;
|
|
|
|
|
}
|
2013-06-19 17:01:28 +02:00
|
|
|
|
|
|
|
|
public long cleanEntriesOlderThan(long value, TimeUnit unit) {
|
2013-06-16 11:48:23 +02:00
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
cal.add(Calendar.MINUTE, -1 * (int) unit.toMinutes(value));
|
|
|
|
|
|
2013-06-18 13:00:03 +02:00
|
|
|
long total = 0;
|
|
|
|
|
int deleted = -1;
|
|
|
|
|
do {
|
2013-06-19 13:29:56 +02:00
|
|
|
deleted = feedEntryDAO.delete(cal.getTime(), 100);
|
2013-06-18 13:00:03 +02:00
|
|
|
total += deleted;
|
2013-06-19 15:14:15 +02:00
|
|
|
log.info("removed {} entries", total);
|
2013-06-18 13:00:03 +02:00
|
|
|
} while (deleted != 0);
|
2013-06-19 15:14:15 +02:00
|
|
|
log.info("cleanup done: {} entries deleted", total);
|
2013-06-18 13:00:03 +02:00
|
|
|
return total;
|
2013-06-16 11:48:23 +02:00
|
|
|
}
|
2013-07-02 16:10:59 +02:00
|
|
|
|
|
|
|
|
public void mergeFeeds(Feed into, List<Feed> feeds) {
|
|
|
|
|
for (Feed feed : feeds) {
|
|
|
|
|
if (into.getId().equals(feed.getId())) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
List<FeedSubscription> subs = feedSubscriptionDAO.findByFeed(feed);
|
|
|
|
|
for (FeedSubscription sub : subs) {
|
|
|
|
|
sub.setFeed(into);
|
|
|
|
|
}
|
|
|
|
|
feedSubscriptionDAO.saveOrUpdate(subs);
|
|
|
|
|
feedDAO.delete(feed);
|
|
|
|
|
}
|
|
|
|
|
feedDAO.saveOrUpdate(into);
|
|
|
|
|
}
|
2013-06-16 11:48:23 +02:00
|
|
|
}
|