Files
Athou_commafeed/src/main/java/com/commafeed/backend/service/DatabaseCleaningService.java

114 lines
3.7 KiB
Java
Raw Normal View History

package com.commafeed.backend.service;
2013-07-25 11:02:49 +02:00
import java.util.Date;
import java.util.List;
2014-08-17 14:16:30 +02:00
import javax.inject.Inject;
import javax.inject.Singleton;
2014-08-11 06:11:12 +02:00
import org.hibernate.SessionFactory;
2013-06-19 17:01:28 +02:00
import com.commafeed.backend.dao.FeedDAO;
import com.commafeed.backend.dao.FeedEntryContentDAO;
2013-06-19 13:29:56 +02:00
import com.commafeed.backend.dao.FeedEntryDAO;
import com.commafeed.backend.dao.FeedEntryDAO.FeedCapacity;
2013-07-25 11:02:49 +02:00
import com.commafeed.backend.dao.FeedEntryStatusDAO;
2014-08-11 06:11:12 +02:00
import com.commafeed.backend.dao.UnitOfWork;
2015-02-18 12:03:28 +01:00
import com.commafeed.backend.model.Feed;
2015-06-24 11:42:01 +02:00
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
2013-07-26 16:00:02 +02:00
/**
* Contains utility methods for cleaning the database
*
*/
2013-08-11 11:45:32 +02:00
@Slf4j
2015-06-24 11:42:01 +02:00
@RequiredArgsConstructor(onConstructor = @__({ @Inject }) )
2014-08-17 14:16:30 +02:00
@Singleton
public class DatabaseCleaningService {
2014-03-17 06:18:36 +01:00
2013-11-16 11:27:48 +01:00
private static final int BATCH_SIZE = 100;
2014-08-11 06:11:12 +02:00
private final SessionFactory sessionFactory;
private final FeedDAO feedDAO;
private final FeedEntryDAO feedEntryDAO;
private final FeedEntryContentDAO feedEntryContentDAO;
private final FeedEntryStatusDAO feedEntryStatusDAO;
2013-07-25 11:02:49 +02:00
2014-03-17 06:18:36 +01:00
public long cleanFeedsWithoutSubscriptions() {
log.info("cleaning feeds without subscriptions");
2013-11-16 07:30:01 +01:00
long total = 0;
int deleted = 0;
2015-02-20 08:51:33 +01:00
long entriesTotal = 0;
2013-11-16 07:30:01 +01:00
do {
2015-06-24 11:42:01 +02:00
List<Feed> feeds = UnitOfWork.call(sessionFactory, () -> feedDAO.findWithoutSubscriptions(1));
2015-02-18 12:03:28 +01:00
for (Feed feed : feeds) {
int entriesDeleted = 0;
do {
2015-06-24 11:42:01 +02:00
entriesDeleted = UnitOfWork.call(sessionFactory, () -> feedEntryDAO.delete(feed.getId(), BATCH_SIZE));
2015-02-20 08:51:33 +01:00
entriesTotal += entriesDeleted;
log.info("removed {} entries for feeds without subscriptions", entriesTotal);
} while (entriesDeleted > 0);
2015-02-18 12:03:28 +01:00
}
2015-06-24 11:42:01 +02:00
deleted = UnitOfWork.call(sessionFactory, () -> feedDAO.delete(feeds));
2014-03-17 06:18:36 +01:00
total += deleted;
2014-03-21 00:25:20 +01:00
log.info("removed {} feeds without subscriptions", total);
2013-11-16 07:30:01 +01:00
} while (deleted != 0);
2014-03-17 06:18:36 +01:00
log.info("cleanup done: {} feeds without subscriptions deleted", total);
2013-11-16 07:30:01 +01:00
return total;
}
public long cleanContentsWithoutEntries() {
2013-11-15 21:38:11 +01:00
log.info("cleaning contents without entries");
2013-07-04 21:50:39 +02:00
long total = 0;
2013-11-16 07:30:01 +01:00
int deleted = 0;
2013-07-04 21:50:39 +02:00
do {
2015-06-24 11:42:01 +02:00
deleted = UnitOfWork.call(sessionFactory, () -> feedEntryContentDAO.deleteWithoutEntries(BATCH_SIZE));
2013-07-04 21:50:39 +02:00
total += deleted;
2013-11-14 14:41:41 +01:00
log.info("removed {} contents without entries", total);
2013-07-04 21:50:39 +02:00
} while (deleted != 0);
2013-11-14 14:41:41 +01:00
log.info("cleanup done: {} contents without entries deleted", total);
2013-07-04 21:50:39 +02:00
return total;
}
2013-06-19 17:01:28 +02:00
public long cleanEntriesForFeedsExceedingCapacity(final int maxFeedCapacity) {
2013-06-18 13:00:03 +02:00
long total = 0;
while (true) {
2015-06-24 11:42:01 +02:00
List<FeedCapacity> feeds = UnitOfWork.call(sessionFactory,
2014-12-12 08:57:50 +01:00
() -> feedEntryDAO.findFeedsExceedingCapacity(maxFeedCapacity, BATCH_SIZE));
if (feeds.isEmpty()) {
break;
}
for (final FeedCapacity feed : feeds) {
long remaining = feed.getCapacity() - maxFeedCapacity;
do {
final long rem = remaining;
2015-06-24 11:42:01 +02:00
int deleted = UnitOfWork.call(sessionFactory,
2014-12-12 08:57:50 +01:00
() -> feedEntryDAO.deleteOldEntries(feed.getId(), Math.min(BATCH_SIZE, rem)));
total += deleted;
remaining -= deleted;
log.info("removed {} entries for feeds exceeding capacity", total);
} while (remaining > 0);
}
}
log.info("cleanup done: {} entries for feeds exceeding capacity deleted", total);
2013-06-18 13:00:03 +02:00
return total;
}
2014-08-11 06:11:12 +02:00
public long cleanStatusesOlderThan(final Date olderThan) {
2013-07-25 11:02:49 +02:00
log.info("cleaning old read statuses");
long total = 0;
2014-08-11 06:11:12 +02:00
int deleted = 0;
do {
2015-06-24 11:42:01 +02:00
deleted = UnitOfWork.call(sessionFactory,
2014-12-12 08:57:50 +01:00
() -> feedEntryStatusDAO.delete(feedEntryStatusDAO.getOldStatuses(olderThan, BATCH_SIZE)));
2014-08-11 06:11:12 +02:00
total += deleted;
2014-12-15 15:54:39 +01:00
log.info("removed {} old read statuses", total);
2014-08-11 06:11:12 +02:00
} while (deleted != 0);
log.info("cleanup done: {} old read statuses deleted", total);
return total;
2013-07-25 11:02:49 +02:00
}
}