Bug 715796 - Part 0: extract SQLiteOpenHelper cache from AndroidBrowserHistoryDataExtender. r=rnewman

This commit is contained in:
Marina Samuel 2012-03-02 17:36:15 -08:00
parent eaf63ec593
commit 3d8e721d81
4 changed files with 58 additions and 42 deletions

View File

@ -46,9 +46,8 @@ import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class AndroidBrowserHistoryDataExtender extends SQLiteOpenHelper {
public class AndroidBrowserHistoryDataExtender extends CachedSQLiteOpenHelper {
public static final String LOG_TAG = "SyncHistoryVisits";
@ -77,44 +76,6 @@ public class AndroidBrowserHistoryDataExtender extends SQLiteOpenHelper {
db.execSQL(createTableSql);
}
// Cache these so we don't have to track them across cursors. Call `close`
// when you're done.
private static SQLiteDatabase readableDatabase;
private static SQLiteDatabase writableDatabase;
protected SQLiteDatabase getCachedReadableDatabase() {
if (AndroidBrowserHistoryDataExtender.readableDatabase == null) {
if (AndroidBrowserHistoryDataExtender.writableDatabase == null) {
AndroidBrowserHistoryDataExtender.readableDatabase = this.getReadableDatabase();
return AndroidBrowserHistoryDataExtender.readableDatabase;
} else {
return AndroidBrowserHistoryDataExtender.writableDatabase;
}
} else {
return AndroidBrowserHistoryDataExtender.readableDatabase;
}
}
protected SQLiteDatabase getCachedWritableDatabase() {
if (AndroidBrowserHistoryDataExtender.writableDatabase == null) {
AndroidBrowserHistoryDataExtender.writableDatabase = this.getWritableDatabase();
}
return AndroidBrowserHistoryDataExtender.writableDatabase;
}
@Override
public void close() {
if (AndroidBrowserHistoryDataExtender.readableDatabase != null) {
AndroidBrowserHistoryDataExtender.readableDatabase.close();
AndroidBrowserHistoryDataExtender.readableDatabase = null;
}
if (AndroidBrowserHistoryDataExtender.writableDatabase != null) {
AndroidBrowserHistoryDataExtender.writableDatabase.close();
AndroidBrowserHistoryDataExtender.writableDatabase = null;
}
super.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// For now we'll just drop and recreate the tables.

View File

@ -0,0 +1,55 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.sync.repositories.android;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public abstract class CachedSQLiteOpenHelper extends SQLiteOpenHelper {
public CachedSQLiteOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
// Cache these so we don't have to track them across cursors. Call `close`
// when you're done.
private static SQLiteDatabase readableDatabase;
private static SQLiteDatabase writableDatabase;
synchronized protected SQLiteDatabase getCachedReadableDatabase() {
if (readableDatabase == null) {
if (writableDatabase == null) {
readableDatabase = this.getReadableDatabase();
return readableDatabase;
} else {
return writableDatabase;
}
} else {
return readableDatabase;
}
}
synchronized protected SQLiteDatabase getCachedWritableDatabase() {
if (writableDatabase == null) {
writableDatabase = this.getWritableDatabase();
}
return writableDatabase;
}
synchronized public void close() {
if (readableDatabase != null) {
readableDatabase.close();
readableDatabase = null;
}
if (writableDatabase != null) {
writableDatabase.close();
writableDatabase = null;
}
super.close();
}
}

View File

@ -154,7 +154,7 @@ public class SyncAuthenticatorService extends Service {
try {
String username = KeyBundle.usernameFromAccount(account.name);
Logger.pii(LOG_TAG, "Account " + account.name + " hashes to " + username);
Logger.info(LOG_TAG, "Setting username. Null?" + (username == null));
Logger.info(LOG_TAG, "Setting username. Null? " + (username == null));
result.putString(Constants.OPTION_USERNAME, username);
} catch (NoSuchAlgorithmException e) {
// Do nothing. Calling code must check for missing value.

File diff suppressed because one or more lines are too long