From d3329504cd47197c7eb2902eb91083f82243269b Mon Sep 17 00:00:00 2001 From: Floens Date: Wed, 24 Jun 2015 23:18:07 +0200 Subject: [PATCH] Clean up DatabaseManager --- .../src/main/java/org/floens/chan/Chan.java | 2 +- .../chan/core/manager/BoardManager.java | 2 +- .../floens/chan/database/DatabaseManager.java | 154 +++++++++++------- .../DeveloperSettingsController.java | 11 -- 4 files changed, 99 insertions(+), 70 deletions(-) diff --git a/Clover/app/src/main/java/org/floens/chan/Chan.java b/Clover/app/src/main/java/org/floens/chan/Chan.java index 2538464b..40a43f4b 100644 --- a/Clover/app/src/main/java/org/floens/chan/Chan.java +++ b/Clover/app/src/main/java/org/floens/chan/Chan.java @@ -127,7 +127,7 @@ public class Chan extends Application { if (ChanBuild.DEVELOPER_MODE) { // refWatcher = LeakCanary.install(this); - StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); + StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectCustomSlowCalls().detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build()); } diff --git a/Clover/app/src/main/java/org/floens/chan/core/manager/BoardManager.java b/Clover/app/src/main/java/org/floens/chan/core/manager/BoardManager.java index bcd36854..4dde2ed7 100644 --- a/Clover/app/src/main/java/org/floens/chan/core/manager/BoardManager.java +++ b/Clover/app/src/main/java/org/floens/chan/core/manager/BoardManager.java @@ -84,7 +84,7 @@ public class BoardManager { } public void updateSavedBoards() { - Chan.getDatabaseManager().updateBoards(allBoards); + Chan.getDatabaseManager().setBoards(allBoards); notifyChanged(); } diff --git a/Clover/app/src/main/java/org/floens/chan/database/DatabaseManager.java b/Clover/app/src/main/java/org/floens/chan/database/DatabaseManager.java index 266bafc3..deb9926d 100644 --- a/Clover/app/src/main/java/org/floens/chan/database/DatabaseManager.java +++ b/Clover/app/src/main/java/org/floens/chan/database/DatabaseManager.java @@ -19,13 +19,15 @@ package org.floens.chan.database; import android.content.Context; +import com.j256.ormlite.dao.Dao; + import org.floens.chan.core.model.Board; import org.floens.chan.core.model.Pin; import org.floens.chan.core.model.SavedReply; import org.floens.chan.utils.Logger; -import org.floens.chan.utils.Time; import java.sql.SQLException; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.concurrent.Callable; @@ -37,30 +39,37 @@ public class DatabaseManager { private static final long SAVED_REPLY_TRIM_COUNT = 50; private final DatabaseHelper helper; - private List savedReplies; + + private List savedReplies = new ArrayList<>(); private HashSet savedRepliesIds = new HashSet<>(); public DatabaseManager(Context context) { helper = new DatabaseHelper(context); + initialize(); } + /** + * Save a reply to the savedreply table. + * @param saved the {@link SavedReply} to save + */ public void saveReply(SavedReply saved) { - Logger.d(TAG, "Saving " + saved.board + ", " + saved.no); - try { helper.savedDao.create(saved); } catch (SQLException e) { Logger.e(TAG, "Error saving reply", e); } - loadSavedReplies(); + savedReplies.add(saved); + savedRepliesIds.add(saved.no); } + /** + * Searches a saved reply. This is done through caching members, no database lookups. + * @param board board for the reply to search + * @param no no for the reply to search + * @return A {@link SavedReply} that matches {@code board} and {@code no}, or {@code null} + */ public SavedReply getSavedReply(String board, int no) { - if (savedReplies == null) { - loadSavedReplies(); - } - if (savedRepliesIds.contains(no)) { for (SavedReply r : savedReplies) { if (r.no == no && r.board.equals(board)) { @@ -72,38 +81,20 @@ public class DatabaseManager { return null; } + /** + * Searches if a saved reply exists. This is done through caching members, no database lookups. + * @param board board for the reply to search + * @param no no for the reply to search + * @return true if a {@link SavedReply} matched {@code board} and {@code no}, {@code false} otherwise + */ public boolean isSavedReply(String board, int no) { return getSavedReply(board, no) != null; } - private void loadSavedReplies() { - try { - savedReplies = helper.savedDao.queryForAll(); - savedRepliesIds.clear(); - for (SavedReply reply : savedReplies) { - savedRepliesIds.add(reply.no); - } - - long count = helper.savedDao.countOf(); - if (count >= SAVED_REPLY_TRIM_TRIGGER) { - trimSavedRepliesTable(SAVED_REPLY_TRIM_COUNT + (count - SAVED_REPLY_TRIM_TRIGGER)); - } - } catch (SQLException e) { - Logger.e(TAG, "Error loading saved replies", e); - } - } - - public void trimSavedRepliesTable(long limit) { - try { - Logger.i(TAG, "Trimming the length of the savedreply table for " + limit + " rows, was " + helper.savedDao.countOf()); - helper.savedDao.executeRaw("DELETE FROM savedreply WHERE id IN " + - "(SELECT id FROM savedreply ORDER BY id ASC LIMIT ?)", Long.toString(limit)); - Logger.i(TAG, "The savedreply table now has " + helper.savedDao.countOf() + " rows"); - } catch (SQLException e) { - Logger.e(TAG, "Error trimming saved replies table", e); - } - } - + /** + * Adds a {@link Pin} to the pin table. + * @param pin Pin to save + */ public void addPin(Pin pin) { try { helper.loadableDao.create(pin.loadable); @@ -113,6 +104,10 @@ public class DatabaseManager { } } + /** + * Deletes a {@link Pin} from the pin table. + * @param pin Pin to delete + */ public void removePin(Pin pin) { try { helper.pinDao.delete(pin); @@ -122,6 +117,10 @@ public class DatabaseManager { } } + /** + * Updates a {@link Pin} in the pin table. + * @param pin Pin to update + */ public void updatePin(Pin pin) { try { helper.pinDao.update(pin); @@ -131,6 +130,10 @@ public class DatabaseManager { } } + /** + * Updates all {@link Pin}s in the list to the pin table. + * @param pins Pins to update + */ public void updatePins(final List pins) { try { helper.pinDao.callBatchTasks(new Callable() { @@ -152,6 +155,10 @@ public class DatabaseManager { } } + /** + * Get a list of {@link Pin}s from the pin table. + * @return List of Pins + */ public List getPinned() { List list = null; try { @@ -166,6 +173,10 @@ public class DatabaseManager { return list; } + /** + * Create or updates these boards in the boards table. + * @param boards List of boards to create or update + */ public void setBoards(final List boards) { try { helper.boardsDao.callBatchTasks(new Callable() { @@ -183,26 +194,10 @@ public class DatabaseManager { } } - public void updateBoards(final List boards) { - try { - helper.boardsDao.callBatchTasks(new Callable() { - @Override - public Void call() throws SQLException { - long start = Time.get(); - for (Board b : boards) { - helper.boardsDao.update(b); - } - - Logger.d(TAG, "Update board took " + Time.get(start)); - - return null; - } - }); - } catch (Exception e) { - Logger.e(TAG, "Error updating boards in db", e); - } - } - + /** + * Get all boards from the boards table. + * @return all boards from the boards table + */ public List getBoards() { List boards = null; try { @@ -214,6 +209,10 @@ public class DatabaseManager { return boards; } + /** + * Summary of the database tables row count, for the developer screen. + * @return list of all tables and their row count. + */ public String getSummary() { String o = ""; @@ -229,8 +228,49 @@ public class DatabaseManager { return o; } + /** + * Reset all tables in the database. Used for the developer screen. + */ public void reset() { helper.reset(); + initialize(); + } + + private void initialize() { loadSavedReplies(); } + + private void loadSavedReplies() { + try { + trimTable(helper.savedDao, "savedreply", SAVED_REPLY_TRIM_TRIGGER, SAVED_REPLY_TRIM_COUNT); + + savedReplies.clear(); + savedReplies.addAll(helper.savedDao.queryForAll()); + savedRepliesIds.clear(); + for (SavedReply reply : savedReplies) { + savedRepliesIds.add(reply.no); + } + } catch (SQLException e) { + Logger.e(TAG, "Error loading saved replies", e); + } + } + + /** + * Trim a table with the specified trigger and trim count. + * @param dao {@link Dao} to use. + * @param table name of the table, used in the query (not escaped). + * @param trigger Trim if there are more rows than {@code trigger}. + * @param trim Count of rows to trim. + */ + private void trimTable(Dao dao, String table, long trigger, long trim) { + try { + long count = dao.countOf(); + if (count > trigger) { + dao.executeRaw("DELETE FROM " + table + " WHERE id IN (SELECT id FROM " + table + " ORDER BY id ASC LIMIT ?)", String.valueOf(trim)); + Logger.i(TAG, "Trimmed " + table + " from " + count + " to " + dao.countOf() + " rows"); + } + } catch (SQLException e) { + Logger.e(TAG, "Error trimming table " + table, e); + } + } } diff --git a/Clover/app/src/main/java/org/floens/chan/ui/controller/DeveloperSettingsController.java b/Clover/app/src/main/java/org/floens/chan/ui/controller/DeveloperSettingsController.java index 6b685018..7793b545 100644 --- a/Clover/app/src/main/java/org/floens/chan/ui/controller/DeveloperSettingsController.java +++ b/Clover/app/src/main/java/org/floens/chan/ui/controller/DeveloperSettingsController.java @@ -95,17 +95,6 @@ public class DeveloperSettingsController extends Controller { savedReplyDummyAdd.setText("Add test rows to savedReply"); wrapper.addView(savedReplyDummyAdd); - Button trimSavedReply = new Button(context); - trimSavedReply.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(final View v) { - Chan.getDatabaseManager().trimSavedRepliesTable(10); - setDbSummary(); - } - }); - trimSavedReply.setText("Trim savedreply table"); - wrapper.addView(trimSavedReply); - ScrollView scrollView = new ScrollView(context); scrollView.addView(wrapper); view = scrollView;