From dbc35b69fa0256920b3b3608c84b6ff844bca4b4 Mon Sep 17 00:00:00 2001 From: Andrei Coman Date: Mon, 14 Dec 2015 12:04:38 -0800 Subject: [PATCH] Make it possible to set DB size Reviewed By: oli Differential Revision: D2749219 fb-gh-sync-id: 2165ed8a89c48687ad82cd1facf2b875d31ca997 --- .../modules/storage/ReactDatabaseSupplier.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/storage/ReactDatabaseSupplier.java b/ReactAndroid/src/main/java/com/facebook/react/modules/storage/ReactDatabaseSupplier.java index 620d23146..87f9daf5c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/storage/ReactDatabaseSupplier.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/storage/ReactDatabaseSupplier.java @@ -30,7 +30,6 @@ public class ReactDatabaseSupplier extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final int SLEEP_TIME_MS = 30; - private static final long DEFAULT_MAX_DB_SIZE = 6L * 1024L * 1024L; // 6 MB in bytes static final String TABLE_CATALYST = "catalystLocalStorage"; static final String KEY_COLUMN = "key"; @@ -46,6 +45,7 @@ public class ReactDatabaseSupplier extends SQLiteOpenHelper { private Context mContext; private @Nullable SQLiteDatabase mDb; + private long mMaximumDatabaseSize = 6L * 1024L * 1024L; // 6 MB in bytes private ReactDatabaseSupplier(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); @@ -105,7 +105,7 @@ public class ReactDatabaseSupplier extends SQLiteOpenHelper { // This is a sane limit to protect the user from the app storing too much data in the database. // This also protects the database from filling up the disk cache and becoming malformed // (endTransaction() calls will throw an exception, not rollback, and leave the db malformed). - mDb.setMaximumSize(DEFAULT_MAX_DB_SIZE); + mDb.setMaximumSize(mMaximumDatabaseSize); return true; } @@ -137,6 +137,17 @@ public class ReactDatabaseSupplier extends SQLiteOpenHelper { get().delete(TABLE_CATALYST, null, null); } + /** + * Sets the maximum size the database will grow to. The maximum size cannot + * be set below the current size. + */ + public synchronized void setMaximumSize(long size) { + mMaximumDatabaseSize = size; + if (mDb != null) { + mDb.setMaximumSize(mMaximumDatabaseSize); + } + } + private synchronized boolean deleteDatabase() { closeDatabase(); return mContext.deleteDatabase(DATABASE_NAME);