Skip to content

Commit

Permalink
make eclipse mars happy
Browse files Browse the repository at this point in the history
  • Loading branch information
Athou committed Jun 24, 2015
1 parent bae5c67 commit ddfd170
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 67 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/commafeed/backend/dao/UnitOfWork.java
Expand Up @@ -18,13 +18,13 @@ public static interface SessionRunnerReturningValue<T> {
} }


public static void run(SessionFactory sessionFactory, SessionRunner sessionRunner) { public static void run(SessionFactory sessionFactory, SessionRunner sessionRunner) {
run(sessionFactory, () -> { call(sessionFactory, () -> {
sessionRunner.runInSession(); sessionRunner.runInSession();
return null; return null;
}); });
} }


public static <T> T run(SessionFactory sessionFactory, SessionRunnerReturningValue<T> sessionRunner) { public static <T> T call(SessionFactory sessionFactory, SessionRunnerReturningValue<T> sessionRunner) {
final Session session = sessionFactory.openSession(); final Session session = sessionFactory.openSession();
if (ManagedSessionContext.hasBind(sessionFactory)) { if (ManagedSessionContext.hasBind(sessionFactory)) {
throw new IllegalStateException("Already in a unit of work!"); throw new IllegalStateException("Already in a unit of work!");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/commafeed/backend/feed/FeedQueues.java
Expand Up @@ -108,7 +108,7 @@ private void refill() {
// add feeds that are up to refresh from the database // add feeds that are up to refresh from the database
int count = batchSize - contexts.size(); int count = batchSize - contexts.size();
if (count > 0) { if (count > 0) {
List<Feed> feeds = UnitOfWork.run(sessionFactory, () -> feedDAO.findNextUpdatable(count, getLastLoginThreshold())); List<Feed> feeds = UnitOfWork.call(sessionFactory, () -> feedDAO.findNextUpdatable(count, getLastLoginThreshold()));
for (Feed feed : feeds) { for (Feed feed : feeds) {
contexts.add(new FeedRefreshContext(feed, false)); contexts.add(new FeedRefreshContext(feed, false));
} }
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/commafeed/backend/feed/FeedRefreshUpdater.java
@@ -1,7 +1,5 @@
package com.commafeed.backend.feed; package com.commafeed.backend.feed;


import io.dropwizard.lifecycle.Managed;

import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
Expand All @@ -14,8 +12,6 @@
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;


import lombok.extern.slf4j.Slf4j;

import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
Expand All @@ -39,6 +35,9 @@
import com.commafeed.backend.service.PubSubService; import com.commafeed.backend.service.PubSubService;
import com.google.common.util.concurrent.Striped; import com.google.common.util.concurrent.Striped;


import io.dropwizard.lifecycle.Managed;
import lombok.extern.slf4j.Slf4j;

@Slf4j @Slf4j
@Singleton @Singleton
public class FeedRefreshUpdater implements Managed { public class FeedRefreshUpdater implements Managed {
Expand Down Expand Up @@ -121,7 +120,7 @@ public void run() {
if (!lastEntries.contains(cacheKey)) { if (!lastEntries.contains(cacheKey)) {
log.debug("cache miss for {}", entry.getUrl()); log.debug("cache miss for {}", entry.getUrl());
if (subscriptions == null) { if (subscriptions == null) {
subscriptions = UnitOfWork.run(sessionFactory, () -> feedSubscriptionDAO.findByFeed(feed)); subscriptions = UnitOfWork.call(sessionFactory, () -> feedSubscriptionDAO.findByFeed(feed));
} }
ok &= addEntry(feed, entry, subscriptions); ok &= addEntry(feed, entry, subscriptions);
entryCacheMiss.mark(); entryCacheMiss.mark();
Expand Down Expand Up @@ -183,7 +182,7 @@ private boolean addEntry(final Feed feed, final FeedEntry entry, final List<Feed
locked1 = lock1.tryLock(1, TimeUnit.MINUTES); locked1 = lock1.tryLock(1, TimeUnit.MINUTES);
locked2 = lock2.tryLock(1, TimeUnit.MINUTES); locked2 = lock2.tryLock(1, TimeUnit.MINUTES);
if (locked1 && locked2) { if (locked1 && locked2) {
boolean inserted = UnitOfWork.run(sessionFactory, () -> feedUpdateService.addEntry(feed, entry, subscriptions)); boolean inserted = UnitOfWork.call(sessionFactory, () -> feedUpdateService.addEntry(feed, entry, subscriptions));
if (inserted) { if (inserted) {
entryInserted.mark(); entryInserted.mark();
} }
Expand Down
Expand Up @@ -6,9 +6,6 @@
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;


import com.commafeed.backend.dao.FeedDAO; import com.commafeed.backend.dao.FeedDAO;
Expand All @@ -19,12 +16,15 @@
import com.commafeed.backend.dao.UnitOfWork; import com.commafeed.backend.dao.UnitOfWork;
import com.commafeed.backend.model.Feed; import com.commafeed.backend.model.Feed;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/** /**
* Contains utility methods for cleaning the database * Contains utility methods for cleaning the database
* *
*/ */
@Slf4j @Slf4j
@RequiredArgsConstructor(onConstructor = @__({ @Inject })) @RequiredArgsConstructor(onConstructor = @__({ @Inject }) )
@Singleton @Singleton
public class DatabaseCleaningService { public class DatabaseCleaningService {


Expand All @@ -42,16 +42,16 @@ public long cleanFeedsWithoutSubscriptions() {
int deleted = 0; int deleted = 0;
long entriesTotal = 0; long entriesTotal = 0;
do { do {
List<Feed> feeds = UnitOfWork.run(sessionFactory, () -> feedDAO.findWithoutSubscriptions(1)); List<Feed> feeds = UnitOfWork.call(sessionFactory, () -> feedDAO.findWithoutSubscriptions(1));
for (Feed feed : feeds) { for (Feed feed : feeds) {
int entriesDeleted = 0; int entriesDeleted = 0;
do { do {
entriesDeleted = UnitOfWork.run(sessionFactory, () -> feedEntryDAO.delete(feed.getId(), BATCH_SIZE)); entriesDeleted = UnitOfWork.call(sessionFactory, () -> feedEntryDAO.delete(feed.getId(), BATCH_SIZE));
entriesTotal += entriesDeleted; entriesTotal += entriesDeleted;
log.info("removed {} entries for feeds without subscriptions", entriesTotal); log.info("removed {} entries for feeds without subscriptions", entriesTotal);
} while (entriesDeleted > 0); } while (entriesDeleted > 0);
} }
deleted = UnitOfWork.run(sessionFactory, () -> feedDAO.delete(feeds)); deleted = UnitOfWork.call(sessionFactory, () -> feedDAO.delete(feeds));
total += deleted; total += deleted;
log.info("removed {} feeds without subscriptions", total); log.info("removed {} feeds without subscriptions", total);
} while (deleted != 0); } while (deleted != 0);
Expand All @@ -64,7 +64,7 @@ public long cleanContentsWithoutEntries() {
long total = 0; long total = 0;
int deleted = 0; int deleted = 0;
do { do {
deleted = UnitOfWork.run(sessionFactory, () -> feedEntryContentDAO.deleteWithoutEntries(BATCH_SIZE)); deleted = UnitOfWork.call(sessionFactory, () -> feedEntryContentDAO.deleteWithoutEntries(BATCH_SIZE));
total += deleted; total += deleted;
log.info("removed {} contents without entries", total); log.info("removed {} contents without entries", total);
} while (deleted != 0); } while (deleted != 0);
Expand All @@ -75,7 +75,7 @@ public long cleanContentsWithoutEntries() {
public long cleanEntriesForFeedsExceedingCapacity(final int maxFeedCapacity) { public long cleanEntriesForFeedsExceedingCapacity(final int maxFeedCapacity) {
long total = 0; long total = 0;
while (true) { while (true) {
List<FeedCapacity> feeds = UnitOfWork.run(sessionFactory, List<FeedCapacity> feeds = UnitOfWork.call(sessionFactory,
() -> feedEntryDAO.findFeedsExceedingCapacity(maxFeedCapacity, BATCH_SIZE)); () -> feedEntryDAO.findFeedsExceedingCapacity(maxFeedCapacity, BATCH_SIZE));
if (feeds.isEmpty()) { if (feeds.isEmpty()) {
break; break;
Expand All @@ -85,7 +85,7 @@ public long cleanEntriesForFeedsExceedingCapacity(final int maxFeedCapacity) {
long remaining = feed.getCapacity() - maxFeedCapacity; long remaining = feed.getCapacity() - maxFeedCapacity;
do { do {
final long rem = remaining; final long rem = remaining;
int deleted = UnitOfWork.run(sessionFactory, int deleted = UnitOfWork.call(sessionFactory,
() -> feedEntryDAO.deleteOldEntries(feed.getId(), Math.min(BATCH_SIZE, rem))); () -> feedEntryDAO.deleteOldEntries(feed.getId(), Math.min(BATCH_SIZE, rem)));
total += deleted; total += deleted;
remaining -= deleted; remaining -= deleted;
Expand All @@ -102,7 +102,7 @@ public long cleanStatusesOlderThan(final Date olderThan) {
long total = 0; long total = 0;
int deleted = 0; int deleted = 0;
do { do {
deleted = UnitOfWork.run(sessionFactory, deleted = UnitOfWork.call(sessionFactory,
() -> feedEntryStatusDAO.delete(feedEntryStatusDAO.getOldStatuses(olderThan, BATCH_SIZE))); () -> feedEntryStatusDAO.delete(feedEntryStatusDAO.getOldStatuses(olderThan, BATCH_SIZE)));
total += deleted; total += deleted;
log.info("removed {} old read statuses", total); log.info("removed {} old read statuses", total);
Expand Down
29 changes: 14 additions & 15 deletions src/main/java/com/commafeed/backend/service/StartupService.java
@@ -1,25 +1,12 @@
package com.commafeed.backend.service; package com.commafeed.backend.service;


import io.dropwizard.lifecycle.Managed;

import java.sql.Connection; import java.sql.Connection;
import java.util.Arrays; import java.util.Arrays;


import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.sql.DataSource; import javax.sql.DataSource;


import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.core.PostgresDatabase;
import liquibase.database.jvm.JdbcConnection;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.ResourceAccessor;
import liquibase.structure.DatabaseObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl; import org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
Expand All @@ -31,8 +18,20 @@
import com.commafeed.backend.dao.UserDAO; import com.commafeed.backend.dao.UserDAO;
import com.commafeed.backend.model.UserRole.Role; import com.commafeed.backend.model.UserRole.Role;


import io.dropwizard.lifecycle.Managed;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.core.PostgresDatabase;
import liquibase.database.jvm.JdbcConnection;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.ResourceAccessor;
import liquibase.structure.DatabaseObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j @Slf4j
@RequiredArgsConstructor(onConstructor = @__({ @Inject })) @RequiredArgsConstructor(onConstructor = @__({ @Inject }) )
@Singleton @Singleton
public class StartupService implements Managed { public class StartupService implements Managed {


Expand All @@ -44,7 +43,7 @@ public class StartupService implements Managed {
@Override @Override
public void start() throws Exception { public void start() throws Exception {
updateSchema(); updateSchema();
long count = UnitOfWork.run(sessionFactory, () -> userDAO.count()); long count = UnitOfWork.call(sessionFactory, () -> userDAO.count());
if (count == 0) { if (count == 0) {
UnitOfWork.run(sessionFactory, () -> initialData()); UnitOfWork.run(sessionFactory, () -> initialData());
} }
Expand Down
Expand Up @@ -10,8 +10,6 @@
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;


import lombok.RequiredArgsConstructor;

import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;


import com.commafeed.backend.dao.UnitOfWork; import com.commafeed.backend.dao.UnitOfWork;
Expand All @@ -20,8 +18,10 @@
import com.commafeed.backend.model.UserSettings; import com.commafeed.backend.model.UserSettings;
import com.commafeed.frontend.session.SessionHelper; import com.commafeed.frontend.session.SessionHelper;


import lombok.RequiredArgsConstructor;

@SuppressWarnings("serial") @SuppressWarnings("serial")
@RequiredArgsConstructor(onConstructor = @__({ @Inject })) @RequiredArgsConstructor(onConstructor = @__({ @Inject }) )
@Singleton @Singleton
public class CustomCssServlet extends HttpServlet { public class CustomCssServlet extends HttpServlet {


Expand All @@ -37,7 +37,7 @@ protected void doGet(final HttpServletRequest req, HttpServletResponse resp) thr
return; return;
} }


UserSettings settings = UnitOfWork.run(sessionFactory, () -> userSettingsDAO.findByUser(user.get())); UserSettings settings = UnitOfWork.call(sessionFactory, () -> userSettingsDAO.findByUser(user.get()));
if (settings == null || settings.getCustomCss() == null) { if (settings == null || settings.getCustomCss() == null) {
return; return;
} }
Expand Down
54 changes: 26 additions & 28 deletions src/main/java/com/commafeed/frontend/servlet/NextUnreadServlet.java
Expand Up @@ -11,8 +11,6 @@
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;


import lombok.RequiredArgsConstructor;

import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;


Expand All @@ -31,8 +29,10 @@
import com.commafeed.frontend.session.SessionHelper; import com.commafeed.frontend.session.SessionHelper;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;


import lombok.RequiredArgsConstructor;

@SuppressWarnings("serial") @SuppressWarnings("serial")
@RequiredArgsConstructor(onConstructor = @__({ @Inject })) @RequiredArgsConstructor(onConstructor = @__({ @Inject }) )
@Singleton @Singleton
public class NextUnreadServlet extends HttpServlet { public class NextUnreadServlet extends HttpServlet {


Expand Down Expand Up @@ -63,31 +63,29 @@ protected void doGet(final HttpServletRequest req, HttpServletResponse resp) thr


final ReadingOrder order = StringUtils.equals(orderParam, "asc") ? ReadingOrder.asc : ReadingOrder.desc; final ReadingOrder order = StringUtils.equals(orderParam, "asc") ? ReadingOrder.asc : ReadingOrder.desc;


FeedEntryStatus status = UnitOfWork.run( FeedEntryStatus status = UnitOfWork.call(sessionFactory, () -> {
sessionFactory, FeedEntryStatus s = null;
() -> { if (StringUtils.isBlank(categoryId) || CategoryREST.ALL.equals(categoryId)) {
FeedEntryStatus s = null; List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user.get());
if (StringUtils.isBlank(categoryId) || CategoryREST.ALL.equals(categoryId)) { List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user.get(), subs, true, null, null, 0, 1, order,
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user.get()); true, false, null);
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user.get(), subs, true, null, null, 0, 1, s = Iterables.getFirst(statuses, null);
order, true, false, null); } else {
s = Iterables.getFirst(statuses, null); FeedCategory category = feedCategoryDAO.findById(user.get(), Long.valueOf(categoryId));
} else { if (category != null) {
FeedCategory category = feedCategoryDAO.findById(user.get(), Long.valueOf(categoryId)); List<FeedCategory> children = feedCategoryDAO.findAllChildrenCategories(user.get(), category);
if (category != null) { List<FeedSubscription> subscriptions = feedSubscriptionDAO.findByCategories(user.get(), children);
List<FeedCategory> children = feedCategoryDAO.findAllChildrenCategories(user.get(), category); List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user.get(), subscriptions, true, null, null, 0,
List<FeedSubscription> subscriptions = feedSubscriptionDAO.findByCategories(user.get(), children); 1, order, true, false, null);
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user.get(), subscriptions, true, null, s = Iterables.getFirst(statuses, null);
null, 0, 1, order, true, false, null); }
s = Iterables.getFirst(statuses, null); }
} if (s != null) {
} s.setRead(true);
if (s != null) { feedEntryStatusDAO.saveOrUpdate(s);
s.setRead(true); }
feedEntryStatusDAO.saveOrUpdate(s); return s;
} });
return s;
});


if (status == null) { if (status == null) {
resp.sendRedirect(resp.encodeRedirectURL(config.getApplicationSettings().getPublicUrl())); resp.sendRedirect(resp.encodeRedirectURL(config.getApplicationSettings().getPublicUrl()));
Expand Down

0 comments on commit ddfd170

Please sign in to comment.