fetch all tags at once

This commit is contained in:
Athou
2024-07-01 08:18:29 +02:00
parent c2f2780c3f
commit 82bf8cd807
2 changed files with 25 additions and 15 deletions

View File

@@ -3,6 +3,7 @@ package com.commafeed.backend.dao;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
@@ -73,10 +74,13 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
return status; return status;
} }
private FeedEntryStatus fetchTags(User user, FeedEntryStatus status) { private void fetchTags(User user, List<FeedEntryStatus> statuses) {
List<FeedEntryTag> tags = feedEntryTagDAO.findByEntry(user, status.getEntry()); Map<Long, List<FeedEntryTag>> tagsByEntryIds = feedEntryTagDAO.findByEntries(user,
status.setTags(tags); statuses.stream().map(FeedEntryStatus::getEntry).toList());
return status; for (FeedEntryStatus status : statuses) {
List<FeedEntryTag> tags = tagsByEntryIds.get(status.getEntry().getId());
status.setTags(tags == null ? List.of() : tags);
}
} }
public List<FeedEntryStatus> findStarred(User user, Instant newerThan, int offset, int limit, ReadingOrder order, public List<FeedEntryStatus> findStarred(User user, Instant newerThan, int offset, int limit, ReadingOrder order,
@@ -107,12 +111,9 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
setTimeout(query, config.getApplicationSettings().getQueryTimeout()); setTimeout(query, config.getApplicationSettings().getQueryTimeout());
List<FeedEntryStatus> statuses = query.fetch(); List<FeedEntryStatus> statuses = query.fetch();
for (FeedEntryStatus status : statuses) { statuses.forEach(s -> s.setMarkable(true));
status.setMarkable(true); if (includeContent) {
fetchTags(user, statuses);
if (includeContent) {
fetchTags(user, status);
}
} }
return statuses; return statuses;
@@ -190,14 +191,13 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
FeedEntry e = tuple.get(entry); FeedEntry e = tuple.get(entry);
FeedSubscription sub = tuple.get(subscription); FeedSubscription sub = tuple.get(subscription);
FeedEntryStatus s = handleStatus(user, tuple.get(status), sub, e); FeedEntryStatus s = handleStatus(user, tuple.get(status), sub, e);
if (includeContent) {
fetchTags(user, s);
}
statuses.add(s); statuses.add(s);
} }
if (includeContent) {
fetchTags(user, statuses);
}
return statuses; return statuses;
} }

View File

@@ -1,6 +1,8 @@
package com.commafeed.backend.dao; package com.commafeed.backend.dao;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
@@ -29,4 +31,12 @@ public class FeedEntryTagDAO extends GenericDAO<FeedEntryTag> {
public List<FeedEntryTag> findByEntry(User user, FeedEntry entry) { public List<FeedEntryTag> findByEntry(User user, FeedEntry entry) {
return query().selectFrom(tag).where(tag.user.eq(user), tag.entry.eq(entry)).fetch(); return query().selectFrom(tag).where(tag.user.eq(user), tag.entry.eq(entry)).fetch();
} }
public Map<Long, List<FeedEntryTag>> findByEntries(User user, List<FeedEntry> entries) {
return query().selectFrom(tag)
.where(tag.user.eq(user), tag.entry.in(entries))
.fetch()
.stream()
.collect(Collectors.groupingBy(t -> t.getEntry().getId()));
}
} }