2013-06-16 11:48:23 +02:00
|
|
|
package com.commafeed.backend;
|
|
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
2013-07-25 11:02:49 +02:00
|
|
|
import java.util.Date;
|
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-08-11 11:45:32 +02:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2013-06-18 13:28:15 +02:00
|
|
|
|
2013-06-19 17:01:28 +02:00
|
|
|
import com.commafeed.backend.dao.FeedDAO;
|
2013-07-24 15:39:20 +02:00
|
|
|
import com.commafeed.backend.dao.FeedEntryContentDAO;
|
2013-06-19 13:29:56 +02:00
|
|
|
import com.commafeed.backend.dao.FeedEntryDAO;
|
2013-07-25 11:02:49 +02:00
|
|
|
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
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-07-10 11:01:24 +02:00
|
|
|
import com.commafeed.backend.services.ApplicationSettingsService;
|
2013-06-16 11:48:23 +02:00
|
|
|
|
2013-07-26 16:00:02 +02:00
|
|
|
/**
|
|
|
|
|
* Contains utility methods for cleaning the database
|
|
|
|
|
*
|
|
|
|
|
*/
|
2013-08-11 11:45:32 +02:00
|
|
|
@Slf4j
|
2013-06-16 11:48:23 +02:00
|
|
|
public class DatabaseCleaner {
|
|
|
|
|
|
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-07-25 09:17:33 +02:00
|
|
|
|
2013-07-24 15:39:20 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedEntryContentDAO feedEntryContentDAO;
|
2013-07-02 16:10:59 +02:00
|
|
|
|
2013-07-25 11:02:49 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedEntryStatusDAO feedEntryStatusDAO;
|
|
|
|
|
|
2013-07-10 11:01:24 +02:00
|
|
|
@Inject
|
|
|
|
|
ApplicationSettingsService applicationSettingsService;
|
|
|
|
|
|
2013-06-19 17:01:28 +02:00
|
|
|
public long cleanFeedsWithoutSubscriptions() {
|
|
|
|
|
|
|
|
|
|
long total = 0;
|
|
|
|
|
int deleted = -1;
|
|
|
|
|
do {
|
2013-07-05 17:00:32 +02:00
|
|
|
deleted = feedDAO.deleteWithoutSubscriptions(10);
|
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-09 08:06:32 +02:00
|
|
|
|
2013-07-24 15:39:20 +02:00
|
|
|
public long cleanContentsWithoutEntries() {
|
2013-07-04 21:50:39 +02:00
|
|
|
|
|
|
|
|
long total = 0;
|
|
|
|
|
int deleted = -1;
|
|
|
|
|
do {
|
2013-07-24 15:39:20 +02:00
|
|
|
deleted = feedEntryContentDAO.deleteWithoutEntries(10);
|
2013-07-04 21:50:39 +02:00
|
|
|
total += deleted;
|
2013-07-24 15:39:20 +02:00
|
|
|
log.info("removed {} feeds without subscriptions", total);
|
2013-07-04 21:50:39 +02:00
|
|
|
} while (deleted != 0);
|
2013-07-24 15:39:20 +02:00
|
|
|
log.info("cleanup done: {} feeds without subscriptions deleted", total);
|
2013-07-04 21:50:39 +02:00
|
|
|
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-07-25 11:02:49 +02:00
|
|
|
|
|
|
|
|
public void cleanStatusesOlderThan(Date olderThan) {
|
|
|
|
|
log.info("cleaning old read statuses");
|
|
|
|
|
int deleted = feedEntryStatusDAO.deleteOldStatuses(olderThan);
|
|
|
|
|
log.info("cleaned {} read statuses", deleted);
|
|
|
|
|
}
|
2013-06-16 11:48:23 +02:00
|
|
|
}
|