Improve performance and memory usage of 'recent pubdate' feed sort option

This commit is contained in:
ByteHamster 2020-10-24 11:38:31 +02:00
parent 971df75a25
commit cd100aae15
2 changed files with 25 additions and 17 deletions

View File

@ -850,24 +850,11 @@ public final class DBReader {
}
};
} else {
final Map<Long, Long> recentPubDates = adapter.getMostRecentItemDates();
comparator = (lhs, rhs) -> {
if (lhs.getItems() == null || lhs.getItems().size() == 0) {
List<FeedItem> items = DBReader.getFeedItemList(lhs);
lhs.setItems(items);
}
if (rhs.getItems() == null || rhs.getItems().size() == 0) {
List<FeedItem> items = DBReader.getFeedItemList(rhs);
rhs.setItems(items);
}
if (lhs.getMostRecentItem() == null) {
return 1;
} else if (rhs.getMostRecentItem() == null) {
return -1;
} else {
Date d1 = lhs.getMostRecentItem().getPubDate();
Date d2 = rhs.getMostRecentItem().getPubDate();
return d2.compareTo(d1);
}
long dateLhs = recentPubDates.containsKey(lhs.getId()) ? recentPubDates.get(lhs.getId()) : 0;
long dateRhs = recentPubDates.containsKey(rhs.getId()) ? recentPubDates.get(rhs.getId()) : 0;
return Long.compare(dateRhs, dateLhs);
};
}

View File

@ -21,8 +21,10 @@ import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import de.danoeh.antennapod.core.feed.Chapter;
@ -1189,6 +1191,25 @@ public class PodDBAdapter {
return conditionalFeedCounterRead(whereRead, feedIds);
}
public final Map<Long, Long> getMostRecentItemDates() {
final String query = "SELECT " + KEY_FEED + ","
+ " MAX(" + TABLE_NAME_FEED_ITEMS + "." + KEY_PUBDATE + ") AS most_recent_pubdate"
+ " FROM " + TABLE_NAME_FEED_ITEMS
+ " GROUP BY " + KEY_FEED;
Cursor c = db.rawQuery(query, null);
Map<Long, Long> result = new HashMap<>();
if (c.moveToFirst()) {
do {
long feedId = c.getLong(0);
long date = c.getLong(1);
result.put(feedId, date);
} while (c.moveToNext());
}
c.close();
return result;
}
public final int getNumberOfDownloadedEpisodes() {
final String query = "SELECT COUNT(DISTINCT " + KEY_ID + ") AS count FROM " + TABLE_NAME_FEED_MEDIA +
" WHERE " + KEY_DOWNLOADED + " > 0";