Skip to content

Commit

Permalink
Add publish time index to the subscription-videos table
Browse files Browse the repository at this point in the history
  • Loading branch information
gzsombor committed Aug 18, 2024
1 parent e93c146 commit f2e9f12
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class LocalChannelTable {

static final String GET_ID_AND_CHANNEL_ID = String.format("SELECT %s, %s FROM %s", LocalChannelTable.COL_ID.name, LocalChannelTable.COL_CHANNEL_ID, LocalChannelTable.TABLE_NAME);

public static final String[] ALL_COLUMNS = new String[]{
private static final String[] ALL_COLUMNS = new String[]{
LocalChannelTable.COL_CHANNEL_ID,
LocalChannelTable.COL_TITLE,
LocalChannelTable.COL_DESCRIPTION,
Expand All @@ -62,11 +62,9 @@ public static String getCreateStatement(boolean withPk) {
}

public static final void addIdColumn(SQLiteDatabase db) {
db.execSQL("ALTER TABLE " + TABLE_NAME + " RENAME TO old_" + TABLE_NAME);
db.execSQL(getCreateStatement(true));
String allRowNames = Joiner.on(',').join(ALL_COLUMNS);
db.execSQL("insert into " + TABLE_NAME + " (_id," + allRowNames + ") select rowid," + allRowNames + " from old_" + TABLE_NAME);
db.execSQL("DROP TABLE old_" + TABLE_NAME);
final String allRowNames = Joiner.on(',').join(ALL_COLUMNS);
SQLiteOpenHelperEx.updateTableSchema(db, TABLE_NAME, getCreateStatement(true),
"insert into " + TABLE_NAME + " (_id," + allRowNames + ") select rowid," + allRowNames);
}

public static void updateLatestVideoTimestamp(SQLiteDatabase db, PersistentChannel persistentChannel, long latestPublishTimestamp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import com.google.common.base.Joiner;

import java.io.File;
import java.util.function.Supplier;

import free.rm.skytube.app.SkyTubeApp;
import free.rm.skytube.businessobjects.Logger;
Expand Down Expand Up @@ -160,6 +163,13 @@ public static void createTable(SQLiteDatabase db, String tableName, Column... co
}
}

public static void updateTableSchema(SQLiteDatabase db, String tableName, String newTableCreateStatement, String migration) {
db.execSQL("ALTER TABLE " + tableName + " RENAME TO old_" + tableName);
db.execSQL(newTableCreateStatement);
db.execSQL(migration + " from old_" + tableName);
db.execSQL("DROP TABLE old_" + tableName);
}

private static String listColumns(boolean justNames, final Column[] columns) {
boolean first = true;
final StringBuilder sql = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class SubscriptionsDb extends SQLiteOpenHelperEx {

private static volatile SubscriptionsDb subscriptionsDb = null;

private static final int DATABASE_VERSION = 15;
private static final int DATABASE_VERSION = 16;

private static final String DATABASE_NAME = "subs.db";

Expand All @@ -99,6 +99,7 @@ public static synchronized SubscriptionsDb getSubscriptionsDb() {
public void onCreate(SQLiteDatabase db) {
db.execSQL(SubscriptionsTable.getCreateStatement());
SubscriptionsVideosTable.addNewFlatTable(db);
SubscriptionsVideosTable.addPublishTimeIndex(db);
db.execSQL(LocalChannelTable.getCreateStatement(true));
db.execSQL(CategoriesTable.getCreateStatement());
new CategoryManagement(db).setupDefaultCategories();
Expand Down Expand Up @@ -165,6 +166,9 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (upgrade.executeStep(15)) {
SubscriptionsTable.cleanupTable(db);
}
if (upgrade.executeStep(16)) {
SubscriptionsVideosTable.addPublishTimeIndex(db);
}
}

private void normalizeSubscriptionVideosTable(final SQLiteDatabase db) {
Expand Down Expand Up @@ -623,20 +627,19 @@ public List<YouTubeVideo> getSubscriptionVideoPage(int limit, String videoId, lo
final String sortingColumn = SubscriptionsVideosTable.COL_PUBLISH_TIME.name;
final String[] selectionArguments;
if (videoId != null) {
selection = "(" + sortingColumn + " < ?) OR (" + sortingColumn + " = ? AND " + SubscriptionsVideosTable.COL_YOUTUBE_VIDEO_ID + " > ?)";
selection = "WHERE (" + sortingColumn + " < ?) OR (" + sortingColumn + " = ? AND " + SubscriptionsVideosTable.COL_YOUTUBE_VIDEO_ID + " > ?)";
String formatted = String.valueOf(beforeTimestamp);
selectionArguments = new String[]{ formatted, formatted, videoId };
} else {
selection = null;
selection = "";
selectionArguments = null;
}
Cursor cursor = getReadableDatabase().query(
SubscriptionsVideosTable.TABLE_NAME_V2,
SubscriptionsVideosTable.ALL_COLUMNS_FOR_EXTRACT,
selection, selectionArguments, null, null,
sortingColumn + " DESC, " + SubscriptionsVideosTable.COL_YOUTUBE_VIDEO_ID + " ASC",
String.valueOf(limit));
return extractVideos(cursor, true);
final String sorting = " ORDER BY " + sortingColumn + " DESC, " + SubscriptionsVideosTable.COL_YOUTUBE_VIDEO_ID + " ASC limit "+ limit;
String query = SubscriptionsVideosTable.BASE_QUERY + selection + sorting;
try (Stopwatch s = new Stopwatch("getVideos " + query + ",limit=" + limit + ", beforeTimestamp=" + beforeTimestamp+" videoid="+videoId)) {
Cursor cursor = getReadableDatabase().rawQuery(query, selectionArguments);
return extractVideos(cursor, true);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ public static String[] getLastCheckTimeColumn() {
}

public static void cleanupTable(SQLiteDatabase db) {
db.execSQL("ALTER TABLE " + TABLE_NAME + " RENAME TO old_" + TABLE_NAME);
db.execSQL(getCreateStatement());
db.execSQL("insert into " + TABLE_NAME + " (_id," + COL_CHANNEL_ID + "," + COL_CATEGORY_ID.name + "," + COL_LAST_VISIT_TIME + "," + COL_LAST_VIDEO_FETCH +
") select _id," + COL_CHANNEL_ID + "," + COL_CATEGORY_ID.name + "," + COL_LAST_VISIT_TIME + "," + COL_LAST_CHECK_TIME + " from old_" + TABLE_NAME);
db.execSQL("DROP TABLE old_" + TABLE_NAME);
SQLiteOpenHelperEx.updateTableSchema(db, TABLE_NAME, getCreateStatement(),
"insert into " + TABLE_NAME + " (_id," + COL_CHANNEL_ID + "," + COL_CATEGORY_ID.name + "," + COL_LAST_VISIT_TIME + "," + COL_LAST_VIDEO_FETCH +
") select _id," + COL_CHANNEL_ID + "," + COL_CATEGORY_ID.name + "," + COL_LAST_VISIT_TIME + "," + COL_LAST_CHECK_TIME );
}

public static void updateLastVideoFetchTimestamps(SQLiteDatabase db, PersistentChannel persistentChannel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public class SubscriptionsVideosTable {

private static final String IDX_PUBLISH_TS = "IDX_SubsVideo_Publish";
private static final String IDX_PUBLISH_TS_V2 = "IDX_subscription_videos_Publish";
private static final String IDX_PUBLISH_TIMESTAMP = "IDX_subscription_videos_PublishTime";

static final String[] ALL_COLUMNS_FOR_EXTRACT = new String[] {
COL_CHANNEL_ID_V2.name,
COL_CHANNEL_TITLE.name,
COL_YOUTUBE_VIDEO_ID_V2.name,
COL_CATEGORY_ID.name,
COL_TITLE.name,
Expand All @@ -73,6 +73,17 @@ public class SubscriptionsVideosTable {
COL_PUBLISH_TIME_EXACT.name,
};

static final String BASE_QUERY;
static {
StringBuilder s = new StringBuilder("select c.Title channel_title");
for (String col : ALL_COLUMNS_FOR_EXTRACT) {
s.append(",s.").append(col);
}
s.append(" from subscription_videos s left join Channel c on s.channel_pk = c._id ");
BASE_QUERY = s.toString();
}


private static final String ADD_COLUMN = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN ";

public static String getCreateStatement() {
Expand Down Expand Up @@ -138,4 +149,7 @@ static void addChannelPkColumn(SQLiteDatabase db) {
SQLiteOpenHelperEx.addColumn(db, TABLE_NAME_V2, COL_CHANNEL_PK);
}

static void addPublishTimeIndex(SQLiteDatabase db) {
SQLiteOpenHelperEx.createIndex(db, IDX_PUBLISH_TIMESTAMP, TABLE_NAME_V2, COL_PUBLISH_TIME);
}
}

0 comments on commit f2e9f12

Please sign in to comment.