Skip to content

Commit

Permalink
optimize query for #280
Browse files Browse the repository at this point in the history
  • Loading branch information
Nandaka committed Sep 17, 2018
1 parent 1014935 commit b2e7865
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,25 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
holder.imgprogressBar.setVisibility(View.VISIBLE);

holder.position = position;
if (novels.get(novel.getTitle()) == null) {
new NovelLoader(position, holder).execute(novel);
} else {
populate(novels.get(novel.getTitle()), holder);

if(false) {
if (novels.get(novel.getTitle()) == null) {
new NovelLoader(position, holder).execute(novel);
} else {
populate(novels.get(novel.getTitle()), holder);
}
}
else {
String txtVolume = novel.getVolumes() + " Volume" + (novel.getVolumes() > 1 ? "s" : "");
String txtCategories = "";
for (String category : novel.getCategories()) {
if (category.contains("Project")) {
txtCategories = " | " + category.substring(0, category.indexOf("Project")).replace("Category:", "");
break;
}
}
holder.txtStatusVol.setText(txtVolume + txtCategories);
holder.imgprogressBar.setVisibility(View.INVISIBLE);
}

return row;
Expand Down
43 changes: 29 additions & 14 deletions app/src/main/java/com/erakk/lnreader/helper/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,22 +405,26 @@ public ArrayList<PageModel> getAllAlternative(SQLiteDatabase db, boolean alphOrd
private String getNovelListQuery(boolean alphOrder, boolean isQuickLoad) {
String sql = "";
if (isQuickLoad) {
sql = "SELECT p.*" +
" , CASE WHEN c." + COLUMN_CATEGORY + " IS NULL THEN 0 " +
" ELSE 1 END as " + COLUMN_IS_COMPLETED +
sql = "SELECT p.* " +
" , CASE WHEN instr(group_concat(c.category), 'Category:Completed Project') THEN 1 " +
" ELSE 0 END as is_completed " +
" , COUNT(b.page) as volumes " +
" , group_concat(c.category) as categories " +
" FROM " + TABLE_PAGE + " p " +
" LEFT JOIN " + TABLE_PAGE_CATEGORIES + " c ON p." + COLUMN_PAGE + " = c." + COLUMN_PAGE +
" AND c." + COLUMN_CATEGORY + " = 'Category:Completed Project' " +
" LEFT JOIN novel_books b ON p.page = b.page " +
" WHERE " + COLUMN_PARENT + " = ? ";
}
else {
sql = "SELECT p.*" +
" , CASE WHEN c." + COLUMN_CATEGORY + " IS NULL THEN 0 " +
" ELSE 1 END as " + COLUMN_IS_COMPLETED +
sql = "SELECT p.* " +
" , CASE WHEN instr(group_concat(c.category), 'Category:Completed Project') THEN 1 " +
" ELSE 0 END as is_completed " +
" , COUNT(b.page) as volumes " +
" , group_concat(c.category) as categories " +
" , r.* " +
" FROM " + TABLE_PAGE + " p " +
" LEFT JOIN " + TABLE_PAGE_CATEGORIES + " c ON p." + COLUMN_PAGE + " = c." + COLUMN_PAGE +
" AND c." + COLUMN_CATEGORY + " = 'Category:Completed Project' " +
" LEFT JOIN novel_books b ON p.page = b.page " +
" LEFT JOIN ( SELECT " + COLUMN_PAGE + ", SUM(UPDATESCOUNT) " +
" FROM ( SELECT d." + COLUMN_PAGE +
" , CASE WHEN p2." + COLUMN_LAST_UPDATE + " > ct." + COLUMN_LAST_UPDATE +
Expand All @@ -433,11 +437,14 @@ private String getNovelListQuery(boolean alphOrder, boolean isQuickLoad) {
" ) r ON p." + COLUMN_PAGE + " = r." + COLUMN_PAGE +
" WHERE p." + COLUMN_PARENT + " = ? ";
}
sql += " GROUP BY p.page ";
if (alphOrder)
sql += " ORDER BY " + COLUMN_TITLE;
else
sql += " ORDER BY " + COLUMN_IS_WATCHED + " DESC, " + COLUMN_TITLE;

Log.d(TAG, sql);

return sql;
}

Expand All @@ -456,22 +463,26 @@ public ArrayList<PageModel> getAllWatchedNovel(SQLiteDatabase db, boolean alphOr
String sql = "";
if (isQuickLoad) {
sql = "SELECT p.* " +
" , CASE WHEN c." + COLUMN_CATEGORY + " IS NULL THEN 0 " +
" ELSE 1 END as " + COLUMN_IS_COMPLETED +
" , CASE WHEN instr(group_concat(c.category), 'Category:Completed Project') THEN 1 " +
" ELSE 0 END as is_completed " +
" , COUNT(b.page) as volumes " +
" , group_concat(c.category) as categories " +
" FROM " + TABLE_PAGE + " p " +
" LEFT JOIN " + TABLE_PAGE_CATEGORIES + " c ON p." + COLUMN_PAGE + " = c." + COLUMN_PAGE +
" AND c." + COLUMN_CATEGORY + " = 'Category:Completed Project' " +
" LEFT JOIN novel_books b ON p.page = b.page " +
" WHERE p. " + COLUMN_PARENT + " IN (" + Util.join(parents, ", ") + ") " +
" AND p." + COLUMN_IS_WATCHED + " = ? ";
}
else {
sql = "SELECT p.* " +
" , CASE WHEN c." + COLUMN_CATEGORY + " IS NULL THEN 0 " +
" ELSE 1 END as " + COLUMN_IS_COMPLETED +
" , CASE WHEN instr(group_concat(c.category), 'Category:Completed Project') THEN 1 " +
" ELSE 0 END as is_completed " +
" , COUNT(b.page) as volumes " +
" , group_concat(c.category) as categories " +
" , r.* " +
" FROM " + TABLE_PAGE + " p " +
" LEFT JOIN " + TABLE_PAGE_CATEGORIES + " c ON p." + COLUMN_PAGE + " = c." + COLUMN_PAGE +
" AND c." + COLUMN_CATEGORY + " = 'Category:Completed Project' " +
" LEFT JOIN novel_books b ON p.page = b.page " +
" LEFT JOIN ( SELECT " + COLUMN_PAGE + ", SUM(UPDATESCOUNT) " +
" FROM ( SELECT d." + COLUMN_PAGE +
" , case when p2." + COLUMN_LAST_UPDATE + " > ct." + COLUMN_LAST_UPDATE +
Expand All @@ -487,11 +498,15 @@ public ArrayList<PageModel> getAllWatchedNovel(SQLiteDatabase db, boolean alphOr
" AND p." + COLUMN_IS_WATCHED + " = ? ";
}

sql += " GROUP BY p.page ";
if (alphOrder)
sql += " ORDER BY " + COLUMN_TITLE;
else
sql += " ORDER BY " + COLUMN_IS_WATCHED + " DESC, " + COLUMN_TITLE;


Log.d(TAG, sql);

Cursor cursor = rawQuery(db, sql, new String[] { "1" });
try {
cursor.moveToFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@ public static PageModel cursorToPageModel(Cursor cursor) {
if (cursor.getColumnCount() >= 16) {
page.setCompleted(cursor.getInt(15) == 1);
}
// Update Sums
// volume count
if (cursor.getColumnCount() >= 17) {
page.setVolumes(cursor.getInt(16));
}
// categories
if (cursor.getColumnCount() >= 18) {
page.setUpdateCount(cursor.getInt(17));
page.setCategoriesStr(cursor.getString(17));
}
// Update Sums
if (cursor.getColumnCount() >= 20) {
page.setUpdateCount(cursor.getInt(19));
}
return page;
}
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/erakk/lnreader/model/PageModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class PageModel {
private int wikiId = -1;
private ArrayList<String> categories;
private boolean isCompleted = false;
private int volumes = 0;
private String categoriesStr = "";

private String redirectedTo;

Expand Down Expand Up @@ -359,4 +361,26 @@ public boolean isCompleted() {
public void setCompleted(boolean completed) {
isCompleted = completed;
}

public int getVolumes() {
return volumes;
}

public void setVolumes(int volumes) {
this.volumes = volumes;
}

public String getCategoriesStr() {
return categoriesStr;
}

public void setCategoriesStr(String categoriesStr) {
this.categoriesStr = categoriesStr;
this.categories = new ArrayList<String>();
if(categoriesStr != null) {
for (String str : categoriesStr.split(",")) {
this.categories.add(str);
}
}
}
}

0 comments on commit b2e7865

Please sign in to comment.