Add storage module to fb

Differential Revision: D2584243

fb-gh-sync-id: 50dece06820aa754741b560cae5eb3318c1926bd
This commit is contained in:
Andrei Coman 2015-10-26 18:26:25 -07:00 committed by facebook-github-bot-9
parent 787e421815
commit 6a7567e742
2 changed files with 50 additions and 23 deletions

View File

@ -44,7 +44,7 @@ public final class AsyncStorageModule
public AsyncStorageModule(ReactApplicationContext reactContext) { public AsyncStorageModule(ReactApplicationContext reactContext) {
super(reactContext); super(reactContext);
mReactDatabaseSupplier = new ReactDatabaseSupplier(reactContext); mReactDatabaseSupplier = ReactDatabaseSupplier.getInstance(reactContext);
} }
@Override @Override
@ -68,23 +68,7 @@ public final class AsyncStorageModule
// Clear local storage. If fails, crash, since the app is potentially in a bad state and could // Clear local storage. If fails, crash, since the app is potentially in a bad state and could
// cause a privacy violation. We're still not recovering from this well, but at least the error // cause a privacy violation. We're still not recovering from this well, but at least the error
// will be reported to the server. // will be reported to the server.
clear( mReactDatabaseSupplier.clearAndCloseDatabase();
new Callback() {
@Override
public void invoke(Object... args) {
if (args.length == 0) {
FLog.d(ReactConstants.TAG, "Cleaned AsyncLocalStorage.");
return;
}
// Clearing the database has failed, delete it instead.
if (mReactDatabaseSupplier.deleteDatabase()) {
FLog.d(ReactConstants.TAG, "Deleted Local Database AsyncLocalStorage.");
return;
}
// Everything failed, crash the app
throw new RuntimeException("Clearing and deleting database failed: " + args[0]);
}
});
} }
/** /**
@ -353,7 +337,7 @@ public final class AsyncStorageModule
return; return;
} }
try { try {
mReactDatabaseSupplier.get().delete(TABLE_CATALYST, null, null); mReactDatabaseSupplier.clear();
callback.invoke(); callback.invoke();
} catch (Exception e) { } catch (Exception e) {
FLog.w(ReactConstants.TAG, e.getMessage(), e); FLog.w(ReactConstants.TAG, e.getMessage(), e);

View File

@ -16,7 +16,13 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
// VisibleForTesting import com.facebook.common.logging.FLog;
import com.facebook.react.common.ReactConstants;
/**
* Database supplier of the database used by react native. This creates, opens and deletes the
* database as necessary.
*/
public class ReactDatabaseSupplier extends SQLiteOpenHelper { public class ReactDatabaseSupplier extends SQLiteOpenHelper {
// VisibleForTesting // VisibleForTesting
@ -38,12 +44,20 @@ public class ReactDatabaseSupplier extends SQLiteOpenHelper {
private Context mContext; private Context mContext;
private @Nullable SQLiteDatabase mDb; private @Nullable SQLiteDatabase mDb;
private static @Nullable ReactDatabaseSupplier mReactDatabaseSupplierInstance;
public ReactDatabaseSupplier(Context context) { private ReactDatabaseSupplier(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION); super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context; mContext = context;
} }
public static ReactDatabaseSupplier getInstance(Context context) {
if (mReactDatabaseSupplierInstance == null) {
mReactDatabaseSupplierInstance = new ReactDatabaseSupplier(context);
}
return mReactDatabaseSupplierInstance;
}
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
db.execSQL(VERSION_TABLE_CREATE); db.execSQL(VERSION_TABLE_CREATE);
@ -102,11 +116,40 @@ public class ReactDatabaseSupplier extends SQLiteOpenHelper {
return mDb; return mDb;
} }
/* package */ synchronized boolean deleteDatabase() { public synchronized void clearAndCloseDatabase() throws RuntimeException {
try {
clear();
closeDatabase();
FLog.d(ReactConstants.TAG, "Cleaned " + DATABASE_NAME);
} catch (Exception e) {
// Clearing the database has failed, delete it instead.
if (deleteDatabase()) {
FLog.d(ReactConstants.TAG, "Deleted Local Database " + DATABASE_NAME);
return;
}
// Everything failed, throw
throw new RuntimeException("Clearing and deleting database " + DATABASE_NAME + " failed");
}
}
/* package */ synchronized void clear() {
get().delete(TABLE_CATALYST, null, null);
}
private synchronized boolean deleteDatabase() {
closeDatabase();
return mContext.deleteDatabase(DATABASE_NAME);
}
private synchronized void closeDatabase() {
if (mDb != null && mDb.isOpen()) { if (mDb != null && mDb.isOpen()) {
mDb.close(); mDb.close();
mDb = null; mDb = null;
} }
return mContext.deleteDatabase(DATABASE_NAME); }
// For testing purposes only!
public static void deleteInstance() {
mReactDatabaseSupplierInstance = null;
} }
} }