Skip to content

Commit

Permalink
Add 'state' and indexes to the subscription table
Browse files Browse the repository at this point in the history
  • Loading branch information
gzsombor committed Aug 18, 2024
1 parent fd52893 commit 2be2fd2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public class LocalChannelTable {
public static final String TABLE_NAME = "Channel";
public static final String COL_CHANNEL_ID = "Channel_Id";
public static final String COL_CHANNEL_ID_name = "Channel_Id";
public static final String COL_LAST_VIDEO_TS = "Last_Video_TS";
public static final String COL_LAST_CHECK_TS = "Last_Check_TS";
public static final String COL_TITLE = "Title";
Expand All @@ -36,11 +36,13 @@ public class LocalChannelTable {
public static final String COL_BANNER_URL = "Banner_Url";
public static final String COL_SUBSCRIBER_COUNT = "Subscriber_Count";
public static final Column COL_ID = new Column("_id", "integer", " primary key");
public static final Column COL_CHANNEL_ID = new Column(COL_CHANNEL_ID_name, "text", "UNIQUE NOT NULL");
public static final Column COL_STATE = new Column("state", "integer", "default 0");

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);
static final String GET_ID_AND_CHANNEL_ID = String.format("SELECT %s, %s FROM %s", LocalChannelTable.COL_ID.name(), LocalChannelTable.COL_CHANNEL_ID.name(), LocalChannelTable.TABLE_NAME);

private static final String[] ALL_COLUMNS = new String[]{
LocalChannelTable.COL_CHANNEL_ID,
LocalChannelTable.COL_CHANNEL_ID.name(),
LocalChannelTable.COL_TITLE,
LocalChannelTable.COL_DESCRIPTION,
LocalChannelTable.COL_BANNER_URL,
Expand All @@ -52,7 +54,8 @@ public class LocalChannelTable {
public static String getCreateStatement(boolean withPk) {
return "CREATE TABLE " + TABLE_NAME + " (" +
(withPk ? COL_ID.format() + "," : "") +
COL_CHANNEL_ID + " TEXT UNIQUE NOT NULL, " +
COL_CHANNEL_ID.format() + ", " +
COL_STATE.format() + ", " +
COL_TITLE + " TEXT, " +
COL_DESCRIPTION + " TEXT, " +
COL_THUMBNAIL_NORMAL_URL+ " TEXT, " +
Expand All @@ -73,4 +76,12 @@ public static void updateLatestVideoTimestamp(SQLiteDatabase db, PersistentChann
db.execSQL("update " + TABLE_NAME + " set " + COL_LAST_VIDEO_TS + " = max(?, coalesce(" + COL_LAST_VIDEO_TS + ",0)) where " + COL_ID.name() + " = ?", new Object[]{
latestPublishTimestamp, persistentChannel.subscriptionPk()});
}

public static void addChannelIdIndex(SQLiteDatabase db) {
SQLiteHelper.createIndex(db, "IDX_channel_channelId", TABLE_NAME, COL_CHANNEL_ID);
}

public static void addStateColumn(SQLiteDatabase db) {
SQLiteHelper.addColumn(db, TABLE_NAME, COL_STATE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class SubscriptionsDb extends SQLiteOpenHelperEx {
private static final String FIND_EMPTY_RETRIEVAL_TS = String.format("SELECT %s,%s FROM %s WHERE %s IS NULL",
SubscriptionsVideosTable.COL_YOUTUBE_VIDEO_ID, SubscriptionsVideosTable.COL_YOUTUBE_VIDEO, SubscriptionsVideosTable.TABLE_NAME, SubscriptionsVideosTable.COL_RETRIEVAL_TS);

private static final String SUBSCRIBED_CHANNEL_INFO = "SELECT c.Channel_Id,c.Title,c.Thumbnail_Normal_Url,s.Last_Visit_Time,(select max(publish_time) from subscription_videos videos where videos.Channel_Id = s.Channel_Id) as latest_video_ts FROM Subs s,Channel c where s.Channel_Id = c.Channel_Id ";
private static final String SUBSCRIBED_CHANNEL_INFO = "SELECT c.Channel_Id,c.Title,c.Thumbnail_Normal_Url,s.Last_Visit_Time,c.Last_Video_TS as latest_video_ts FROM Subs s,Channel c where s.channel_pk = c._Id ";
private static final String SUBSCRIBED_CHANNEL_INFO_ORDER_BY = " ORDER BY LOWER(" + LocalChannelTable.COL_TITLE + ") ASC";
private static final String SUBSCRIBED_CHANNEL_LIMIT_BY_TITLE = " and LOWER(c." +LocalChannelTable.COL_TITLE + ") like ?";

Expand All @@ -81,7 +81,7 @@ public class SubscriptionsDb extends SQLiteOpenHelperEx {

private static volatile SubscriptionsDb subscriptionsDb = null;

private static final int DATABASE_VERSION = 17;
private static final int DATABASE_VERSION = 18;

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

Expand All @@ -103,6 +103,7 @@ public void onCreate(SQLiteDatabase db) {
SubscriptionsVideosTable.addNewFlatTable(db, false);
SubscriptionsVideosTable.addPublishTimeIndex(db);
db.execSQL(LocalChannelTable.getCreateStatement(true));
LocalChannelTable.addChannelIdIndex(db);
db.execSQL(CategoriesTable.getCreateStatement());
new CategoryManagement(db).setupDefaultCategories();
}
Expand Down Expand Up @@ -173,6 +174,12 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
SubscriptionsVideosTable.removeChannelTitle(db);
SubscriptionsVideosTable.addPublishTimeIndex(db);
}
if (upgrade.executeStep(18)) {
Logger.w(this, "Optimize Channel table");
LocalChannelTable.addChannelIdIndex(db);
SubscriptionsTable.addChannelIdColumn(db);
LocalChannelTable.addStateColumn(db);
}
}

private void normalizeSubscriptionVideosTable(final SQLiteDatabase db) {
Expand Down Expand Up @@ -300,7 +307,7 @@ public DatabaseResult subscribe(PersistentChannel persistentChannel, Collection<
SkyTubeApp.nonUiThread();
saveChannelVideos(videos, persistentChannel, false);

return saveSubscription(persistentChannel.channel().getChannelId());
return saveSubscription(persistentChannel.channelPk(), persistentChannel.channel().getChannelId());
}

/**
Expand All @@ -310,11 +317,12 @@ public DatabaseResult subscribe(PersistentChannel persistentChannel, Collection<
*
* @return True if the operation was successful; false otherwise.
*/
private DatabaseResult saveSubscription(ChannelId channelId) {
private DatabaseResult saveSubscription(long channelPk, ChannelId channelId) {
SkyTubeApp.nonUiThread();

ContentValues values = new ContentValues();
values.put(SubscriptionsTable.COL_CHANNEL_ID, channelId.getRawId());
values.put(SubscriptionsTable.COL_CHANNEL_PK.name(), channelPk);

SQLiteDatabase db = getWritableDatabase();
try {
Expand Down Expand Up @@ -481,7 +489,7 @@ private List<YouTubeChannel> getSubscribedChannels(SQLiteDatabase db) throws IOE
List<YouTubeChannel> subsChannels = new ArrayList<>();

if (cursor.moveToNext()) {
final int colChannelIdNum = cursor.getColumnIndexOrThrow(LocalChannelTable.COL_CHANNEL_ID);
final int colChannelIdNum = cursor.getColumnIndexOrThrow(LocalChannelTable.COL_CHANNEL_ID.name());
final int colTitle = cursor.getColumnIndexOrThrow(LocalChannelTable.COL_TITLE);
final int colDescription = cursor.getColumnIndexOrThrow(LocalChannelTable.COL_DESCRIPTION);
final int colBanner = cursor.getColumnIndexOrThrow(LocalChannelTable.COL_BANNER_URL);
Expand Down Expand Up @@ -790,7 +798,7 @@ private PersistentChannel cacheChannel(SQLiteDatabase db, @Nullable PersistentCh
}
return new PersistentChannel(channel, channelPk, subPk);
}
values.put(LocalChannelTable.COL_CHANNEL_ID, channel.getChannelId().getRawId());
values.put(LocalChannelTable.COL_CHANNEL_ID.name(), channel.getChannelId().getRawId());
long newPk = db.insert(LocalChannelTable.TABLE_NAME, null, values);
return new PersistentChannel(channel, newPk, subPk);
}
Expand Down Expand Up @@ -821,7 +829,7 @@ private static ContentValues toContentValues(YouTubeChannel channel) {

public List<ChannelView> getSubscribedChannelsByText(String searchText, boolean sortChannelsAlphabetically) {
List<ChannelView> result = new ArrayList<>();
try (Cursor cursor = createSubscriptionCursor(searchText, sortChannelsAlphabetically)) {
try (Cursor cursor = createSubscriptionCursor(searchText, sortChannelsAlphabetically); Stopwatch s = new Stopwatch("search for "+searchText)) {
final int channelId = cursor.getColumnIndexOrThrow(SubscriptionsTable.COL_CHANNEL_ID);
final int title = cursor.getColumnIndexOrThrow(LocalChannelTable.COL_TITLE);
final int thumbnail = cursor.getColumnIndexOrThrow(LocalChannelTable.COL_THUMBNAIL_NORMAL_URL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class SubscriptionsTable {
private static final String COL_SUBSCRIBER_COUNT = "Subscriber_Count";
public static final Column COL_CATEGORY_ID = new Column("category_id", "INTEGER");
public static final String COL_LAST_VIDEO_FETCH = "last_video_fetch_time";
public static final Column COL_CHANNEL_PK = new Column("channel_pk", "integer");

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

Expand All @@ -50,6 +51,7 @@ public static String getCreateStatement() {
return "CREATE TABLE " + TABLE_NAME + " (" +
COL_ID + " INTEGER PRIMARY KEY ASC, " +
COL_CHANNEL_ID + " TEXT UNIQUE NOT NULL, " +
COL_CHANNEL_PK.format() + ", " +
COL_CATEGORY_ID.format() + ", " +
COL_LAST_VISIT_TIME + " TIMESTAMP DEFAULT (strftime('%s', 'now')), " +
COL_LAST_VIDEO_FETCH + " INTEGER " +
Expand Down Expand Up @@ -86,4 +88,11 @@ public static void updateLastVideoFetchTimestamps(SQLiteDatabase db, PersistentC
System.currentTimeMillis(), persistentChannel.subscriptionPk() });
}
}

public static void addChannelIdColumn(SQLiteDatabase db) {
SQLiteHelper.addColumn(db, TABLE_NAME, COL_CHANNEL_PK);
db.execSQL("update " + TABLE_NAME +
" set " + COL_CHANNEL_PK.name() +
" = (select " + LocalChannelTable.COL_ID.name() + " from " + LocalChannelTable.TABLE_NAME + " c where c." + LocalChannelTable.COL_CHANNEL_ID_name + " = " + TABLE_NAME + '.' + COL_CHANNEL_ID + ")");
}
}

0 comments on commit 2be2fd2

Please sign in to comment.