summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorByteHamster <ByteHamster@users.noreply.github.com>2022-09-02 19:43:22 +0200
committerGitHub <noreply@github.com>2022-09-02 19:43:22 +0200
commit45e625d988f1c5326fc6272d2fc82f49a050922d (patch)
treee80ef9211b4f53d906fd41fbe1f98fc07012ac28
parent4c88a1aa69b698284ab0f70e55f059bcd514e654 (diff)
parent12076607874520a654558bc1f599aaee86479b15 (diff)
downloadAntennaPod-45e625d988f1c5326fc6272d2fc82f49a050922d.zip
Merge pull request #6053 from ByteHamster/database-optimizations
Database optimizations
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/export/favorites/FavoritesWriter.java26
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java23
-rw-r--r--storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java34
3 files changed, 22 insertions, 61 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/export/favorites/FavoritesWriter.java b/core/src/main/java/de/danoeh/antennapod/core/export/favorites/FavoritesWriter.java
index 29de6ca80..092329229 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/export/favorites/FavoritesWriter.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/export/favorites/FavoritesWriter.java
@@ -3,13 +3,13 @@ package de.danoeh.antennapod.core.export.favorites;
import android.content.Context;
import android.util.Log;
+import de.danoeh.antennapod.model.feed.FeedItemFilter;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@@ -22,9 +22,6 @@ import de.danoeh.antennapod.core.storage.DBReader;
/** Writes saved favorites to file. */
public class FavoritesWriter implements ExportWriter {
private static final String TAG = "FavoritesWriter";
-
- private static final int PAGE_LIMIT = 100;
-
private static final String FAVORITE_TEMPLATE = "html-export-favorites-item-template.html";
private static final String FEED_TEMPLATE = "html-export-feed-template.html";
private static final String UTF_8 = "UTF-8";
@@ -45,7 +42,9 @@ public class FavoritesWriter implements ExportWriter {
InputStream feedTemplateStream = context.getAssets().open(FEED_TEMPLATE);
String feedTemplate = IOUtils.toString(feedTemplateStream, UTF_8);
- Map<Long, List<FeedItem>> favoriteByFeed = getFeedMap(getFavorites());
+ List<FeedItem> allFavorites = DBReader.getRecentlyPublishedEpisodes(0, Integer.MAX_VALUE,
+ new FeedItemFilter(FeedItemFilter.IS_FAVORITE));
+ Map<Long, List<FeedItem>> favoriteByFeed = getFeedMap(allFavorites);
writer.append(templateParts[0]);
@@ -66,23 +65,6 @@ public class FavoritesWriter implements ExportWriter {
Log.d(TAG, "Finished writing document");
}
- private List<FeedItem> getFavorites() {
- int page = 0;
-
- List<FeedItem> favoritesList = new ArrayList<>();
- List<FeedItem> favoritesPage;
- do {
- favoritesPage = DBReader.getFavoriteItemsList(page * PAGE_LIMIT, PAGE_LIMIT);
- favoritesList.addAll(favoritesPage);
- ++page;
- } while (!favoritesPage.isEmpty() && favoritesPage.size() == PAGE_LIMIT);
-
- // sort in descending order
- Collections.sort(favoritesList, (lhs, rhs) -> rhs.getPubDate().compareTo(lhs.getPubDate()));
-
- return favoritesList;
- }
-
/**
* Group favorite episodes by feed, sorting them by publishing date in descending order.
*
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java
index 8241a2ca5..899852131 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java
@@ -337,33 +337,12 @@ public final class DBReader {
}
}
- /**
- * Loads a list of favorite items.
- *
- * @param offset The first episode that should be loaded.
- * @param limit The maximum number of episodes that should be loaded.
- * @return A list of FeedItems that are marked as favorite.
- */
- public static List<FeedItem> getFavoriteItemsList(int offset, int limit) {
- Log.d(TAG, "getFavoriteItemsList() called");
-
- PodDBAdapter adapter = PodDBAdapter.getInstance();
- adapter.open();
- try (Cursor cursor = adapter.getFavoritesCursor(offset, limit)) {
- List<FeedItem> items = extractItemlistFromCursor(adapter, cursor);
- loadAdditionalFeedItemListData(items);
- return items;
- } finally {
- adapter.close();
- }
- }
-
private static LongList getFavoriteIDList() {
Log.d(TAG, "getFavoriteIDList() called");
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
- try (Cursor cursor = adapter.getFavoritesCursor(0, Integer.MAX_VALUE)) {
+ try (Cursor cursor = adapter.getFavoritesIdsCursor(0, Integer.MAX_VALUE)) {
LongList favoriteIDs = new LongList(cursor.getCount());
while (cursor.moveToNext()) {
favoriteIDs.add(cursor.getLong(0));
diff --git a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java
index 42517e972..bd0024042 100644
--- a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java
+++ b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java
@@ -971,9 +971,11 @@ public class PodDBAdapter {
* cursor uses the FEEDITEM_SEL_FI_SMALL selection.
*/
public final Cursor getQueueCursor() {
- final String query = SELECT_FEED_ITEMS_AND_MEDIA
- + " INNER JOIN " + TABLE_NAME_QUEUE
+ final String query = "SELECT " + KEYS_FEED_ITEM_WITHOUT_DESCRIPTION + ", " + KEYS_FEED_MEDIA
+ + " FROM " + TABLE_NAME_QUEUE
+ + " INNER JOIN " + TABLE_NAME_FEED_ITEMS
+ " ON " + SELECT_KEY_ITEM_ID + " = " + TABLE_NAME_QUEUE + "." + KEY_FEEDITEM
+ + JOIN_FEED_ITEM_AND_MEDIA
+ " ORDER BY " + TABLE_NAME_QUEUE + "." + KEY_ID;
return db.rawQuery(query, null);
}
@@ -983,9 +985,11 @@ public class PodDBAdapter {
}
public Cursor getNextInQueue(final FeedItem item) {
- final String query = SELECT_FEED_ITEMS_AND_MEDIA
- + "INNER JOIN " + TABLE_NAME_QUEUE
+ final String query = "SELECT " + KEYS_FEED_ITEM_WITHOUT_DESCRIPTION + ", " + KEYS_FEED_MEDIA
+ + " FROM " + TABLE_NAME_QUEUE
+ + " INNER JOIN " + TABLE_NAME_FEED_ITEMS
+ " ON " + SELECT_KEY_ITEM_ID + " = " + TABLE_NAME_QUEUE + "." + KEY_FEEDITEM
+ + JOIN_FEED_ITEM_AND_MEDIA
+ " WHERE Queue.ID > (SELECT Queue.ID FROM Queue WHERE Queue.FeedItem = "
+ item.getId()
+ ")"
@@ -996,36 +1000,32 @@ public class PodDBAdapter {
public final Cursor getPausedQueueCursor(int limit) {
//playback position > 0 (paused), rank by last played, then rest of queue
- final String query = SELECT_FEED_ITEMS_AND_MEDIA
- + " INNER JOIN " + TABLE_NAME_QUEUE
+ final String query = "SELECT " + KEYS_FEED_ITEM_WITHOUT_DESCRIPTION + ", " + KEYS_FEED_MEDIA
+ + " FROM " + TABLE_NAME_QUEUE
+ + " INNER JOIN " + TABLE_NAME_FEED_ITEMS
+ " ON " + SELECT_KEY_ITEM_ID + " = " + TABLE_NAME_QUEUE + "." + KEY_FEEDITEM
+ + JOIN_FEED_ITEM_AND_MEDIA
+ " ORDER BY " + TABLE_NAME_FEED_MEDIA + "." + KEY_POSITION + ">0 DESC , "
+ TABLE_NAME_FEED_MEDIA + "." + KEY_LAST_PLAYED_TIME + " DESC , " + TABLE_NAME_QUEUE + "." + KEY_ID
+ " LIMIT " + limit;
return db.rawQuery(query, null);
}
- public final Cursor getFavoritesCursor(int offset, int limit) {
- final String query = SELECT_FEED_ITEMS_AND_MEDIA
+ public final Cursor getFavoritesIdsCursor(int offset, int limit) {
+ // Way faster than selecting all columns
+ final String query = "SELECT " + TABLE_NAME_FEED_ITEMS + "." + KEY_ID
+ + " FROM " + TABLE_NAME_FEED_ITEMS
+ " INNER JOIN " + TABLE_NAME_FAVORITES
- + " ON " + SELECT_KEY_ITEM_ID + " = " + TABLE_NAME_FAVORITES + "." + KEY_FEEDITEM
+ + " ON " + TABLE_NAME_FEED_ITEMS + "." + KEY_ID + " = " + TABLE_NAME_FAVORITES + "." + KEY_FEEDITEM
+ " ORDER BY " + TABLE_NAME_FEED_ITEMS + "." + KEY_PUBDATE + " DESC"
+ " LIMIT " + offset + ", " + limit;
return db.rawQuery(query, null);
}
- public void setFeedItems(int state) {
- setFeedItems(Integer.MIN_VALUE, state, 0);
- }
-
public void setFeedItems(int oldState, int newState) {
setFeedItems(oldState, newState, 0);
}
- public void setFeedItems(int state, long feedId) {
- setFeedItems(Integer.MIN_VALUE, state, feedId);
- }
-
public void setFeedItems(int oldState, int newState, long feedId) {
String sql = "UPDATE " + TABLE_NAME_FEED_ITEMS + " SET " + KEY_READ + "=" + newState;
if (feedId > 0) {