From edfaa010c0d7034207ed9b84da7aa74440bafafa Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 23 Jul 2019 08:28:32 +0200 Subject: [PATCH] Limit VACUUM to once a day --- app/src/main/java/eu/faircode/email/DB.java | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DB.java b/app/src/main/java/eu/faircode/email/DB.java index 77e065ee28..eae7b229c9 100644 --- a/app/src/main/java/eu/faircode/email/DB.java +++ b/app/src/main/java/eu/faircode/email/DB.java @@ -24,6 +24,7 @@ import org.json.JSONObject; import java.io.File; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Set; import java.util.concurrent.ExecutorService; @@ -100,6 +101,7 @@ public abstract class DB extends RoomDatabase { Runtime.getRuntime().availableProcessors(), Helper.backgroundThreadFactory); private static final String DB_NAME = "fairemail"; + private static final long VACUUM_INTERVAL = 24 * 3600 * 1000L; public static synchronized DB getInstance(Context context) { if (sInstance == null) { @@ -108,12 +110,19 @@ public abstract class DB extends RoomDatabase { sInstance = migrate(acontext, getBuilder(acontext)); // https://www.sqlite.org/lang_vacuum.html - try { - Log.i("Running VACUUM"); - sInstance.getOpenHelper().getWritableDatabase().execSQL("VACUUM;"); - } catch (Throwable ex) { - Log.e(ex); - } + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + long last_vacuum = prefs.getLong("last_vacuum", 0); + Log.i("Last VACUUM at " + new Date(last_vacuum)); + + long now = new Date().getTime(); + if (last_vacuum + VACUUM_INTERVAL < now) + try { + Log.i("Running VACUUM"); + sInstance.getOpenHelper().getWritableDatabase().execSQL("VACUUM;"); + prefs.edit().putLong("last_vacuum", now).apply(); + } catch (Throwable ex) { + Log.e(ex); + } sInstance.getInvalidationTracker().addObserver(new InvalidationTracker.Observer( EntityAccount.TABLE_NAME,