mirror of
https://github.com/Athou/commafeed.git
synced 2026-09-25 05:24:20 +00:00
batch guid existence check on feed refresh
Instead of querying the database once per entry to check if it already exists, fetch all existing guids for a feed in a single query upfront and filter new entries in memory before the per-entry loop. The per-entry findExisting call inside addEntry is kept as a safety net for concurrent inserts, but is now only reached for new entries. Profiling in real usage showed a ~50% reduction in guidHash queries, with the check falling from the most expensive query to sixth in the ranking.
This commit is contained in:
@@ -18,6 +18,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import com.codahale.metrics.Meter;
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.commafeed.backend.Digests;
|
||||
import com.commafeed.backend.dao.FeedEntryDAO;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
||||
import com.commafeed.backend.dao.UnitOfWork;
|
||||
import com.commafeed.backend.feed.parser.FeedParserResult.Content;
|
||||
@@ -42,6 +43,7 @@ public class FeedRefreshUpdater {
|
||||
private final UnitOfWork unitOfWork;
|
||||
private final FeedService feedService;
|
||||
private final FeedEntryService feedEntryService;
|
||||
private final FeedEntryDAO feedEntryDAO;
|
||||
private final FeedSubscriptionDAO feedSubscriptionDAO;
|
||||
|
||||
private final Striped<Lock> locks;
|
||||
@@ -49,11 +51,12 @@ public class FeedRefreshUpdater {
|
||||
private final Meter feedUpdated;
|
||||
private final Meter entryInserted;
|
||||
|
||||
public FeedRefreshUpdater(UnitOfWork unitOfWork, FeedService feedService, FeedEntryService feedEntryService, MetricRegistry metrics,
|
||||
FeedSubscriptionDAO feedSubscriptionDAO) {
|
||||
public FeedRefreshUpdater(UnitOfWork unitOfWork, FeedService feedService, FeedEntryService feedEntryService, FeedEntryDAO feedEntryDAO,
|
||||
MetricRegistry metrics, FeedSubscriptionDAO feedSubscriptionDAO) {
|
||||
this.unitOfWork = unitOfWork;
|
||||
this.feedService = feedService;
|
||||
this.feedEntryService = feedEntryService;
|
||||
this.feedEntryDAO = feedEntryDAO;
|
||||
this.feedSubscriptionDAO = feedSubscriptionDAO;
|
||||
|
||||
locks = Striped.lazyWeakLock(100000);
|
||||
@@ -126,8 +129,11 @@ public class FeedRefreshUpdater {
|
||||
Map<FeedSubscription, List<FeedEntry>> insertedUnreadEntriesBySubscription = new HashMap<>();
|
||||
|
||||
if (!entries.isEmpty()) {
|
||||
Set<String> existingGuids = unitOfWork.call(() -> feedEntryDAO.findExistingGuids(feed));
|
||||
List<Entry> newEntries = entries.stream().filter(e -> !existingGuids.contains(Digests.sha1Hex(e.guid()))).toList();
|
||||
|
||||
List<FeedSubscription> subscriptions = null;
|
||||
for (Entry entry : entries) {
|
||||
for (Entry entry : newEntries) {
|
||||
if (subscriptions == null) {
|
||||
subscriptions = unitOfWork.call(() -> feedSubscriptionDAO.findByFeed(feed));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user