From 212a78e638adb3aa86098fe51f7de6bcd9098a28 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 17 Jan 2021 12:27:28 +0100 Subject: [PATCH] Batch delete log entries --- app/src/main/java/eu/faircode/email/DaoLog.java | 5 +++-- app/src/main/java/eu/faircode/email/FragmentLogs.java | 8 +++++++- .../main/java/eu/faircode/email/WorkerCleanup.java | 11 +++++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoLog.java b/app/src/main/java/eu/faircode/email/DaoLog.java index 7363b29420..d66d7c6367 100644 --- a/app/src/main/java/eu/faircode/email/DaoLog.java +++ b/app/src/main/java/eu/faircode/email/DaoLog.java @@ -42,6 +42,7 @@ public interface DaoLog { long insertLog(EntityLog log); @Query("DELETE FROM log" + - " WHERE time < :before") - int deleteLogs(long before); + " WHERE id IN (SELECT id FROM log" + + " WHERE time < :before ORDER BY time LIMIT :limit)") + int deleteLogs(long before, int limit); } diff --git a/app/src/main/java/eu/faircode/email/FragmentLogs.java b/app/src/main/java/eu/faircode/email/FragmentLogs.java index e54a31d443..bd18769b5a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentLogs.java +++ b/app/src/main/java/eu/faircode/email/FragmentLogs.java @@ -136,7 +136,13 @@ public class FragmentLogs extends FragmentBase { protected Void onExecute(Context context, Bundle args) { DB db = DB.getInstance(context); - db.log().deleteLogs(new Date().getTime()); + long before = new Date().getTime(); + while (true) { + int logs = db.log().deleteLogs(before, WorkerCleanup.LOG_DELETE_BATCH_SIZE); + Log.i("Deleted logs=" + logs); + if (logs < WorkerCleanup.LOG_DELETE_BATCH_SIZE) + break; + } return null; } diff --git a/app/src/main/java/eu/faircode/email/WorkerCleanup.java b/app/src/main/java/eu/faircode/email/WorkerCleanup.java index 2f12968403..c82bcdd2ff 100644 --- a/app/src/main/java/eu/faircode/email/WorkerCleanup.java +++ b/app/src/main/java/eu/faircode/email/WorkerCleanup.java @@ -53,6 +53,8 @@ public class WorkerCleanup extends Worker { private static final long KEEP_CONTACTS_DURATION = 180 * 24 * 3600 * 1000L; // milliseconds private static final long KEEP_LOG_DURATION = 24 * 3600 * 1000L; // milliseconds + static final int LOG_DELETE_BATCH_SIZE = 100; + public WorkerCleanup(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); Log.i("Instance " + getName()); @@ -297,8 +299,13 @@ public class WorkerCleanup extends Worker { Log.i("Deleted contacts=" + contacts); Log.i("Cleanup log"); - int logs = db.log().deleteLogs(now - KEEP_LOG_DURATION); - Log.i("Deleted logs=" + logs); + long before = now - KEEP_LOG_DURATION; + while (true) { + int logs = db.log().deleteLogs(before, LOG_DELETE_BATCH_SIZE); + Log.i("Deleted logs=" + logs + " before=" + new Date(before)); + if (logs < LOG_DELETE_BATCH_SIZE) + break; + } if (BuildConfig.DEBUG) { // https://sqlite.org/lang_analyze.html