Batch delete log entries

pull/193/head
M66B 4 years ago
parent d4400bca9d
commit 212a78e638

@ -42,6 +42,7 @@ public interface DaoLog {
long insertLog(EntityLog log); long insertLog(EntityLog log);
@Query("DELETE FROM log" + @Query("DELETE FROM log" +
" WHERE time < :before") " WHERE id IN (SELECT id FROM log" +
int deleteLogs(long before); " WHERE time < :before ORDER BY time LIMIT :limit)")
int deleteLogs(long before, int limit);
} }

@ -136,7 +136,13 @@ public class FragmentLogs extends FragmentBase {
protected Void onExecute(Context context, Bundle args) { protected Void onExecute(Context context, Bundle args) {
DB db = DB.getInstance(context); 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; return null;
} }

@ -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_CONTACTS_DURATION = 180 * 24 * 3600 * 1000L; // milliseconds
private static final long KEEP_LOG_DURATION = 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) { public WorkerCleanup(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams); super(context, workerParams);
Log.i("Instance " + getName()); Log.i("Instance " + getName());
@ -297,8 +299,13 @@ public class WorkerCleanup extends Worker {
Log.i("Deleted contacts=" + contacts); Log.i("Deleted contacts=" + contacts);
Log.i("Cleanup log"); Log.i("Cleanup log");
int logs = db.log().deleteLogs(now - KEEP_LOG_DURATION); long before = now - KEEP_LOG_DURATION;
Log.i("Deleted logs=" + logs); 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) { if (BuildConfig.DEBUG) {
// https://sqlite.org/lang_analyze.html // https://sqlite.org/lang_analyze.html

Loading…
Cancel
Save