Rename temp tables rather than DELETE + INSERT SELECT.

This requires renaming the old app/apk tables to be deleted and
the temp ones to be renamed. This is done in a transaction to
ensure we always have at least `fdroid_app` and `fdroid_apk`.
Well, two transactions, one for renaming the `fdroid_app` table
and one for `fdroid_apk`.
This commit is contained in:
Peter Serwylo 2015-12-13 09:14:40 +11:00
parent 2067f15b27
commit 9727dc505c
4 changed files with 28 additions and 33 deletions

View File

@ -391,7 +391,6 @@ public class UpdateService extends IntentService implements ProgressListener {
if (!changes) {
Utils.debugLog(TAG, "Not checking app details or compatibility, because all repos were up to date.");
} else {
sendStatus(this, STATUS_INFO, getString(R.string.status_checking_compatibility));
notifyContentProviders();
if (prefs.getBoolean(Preferences.PREF_UPD_NOTIFY, true)) {

View File

@ -78,8 +78,7 @@ public class RepoPersister {
public void commit(ContentValues repoDetailsToSave) throws RepoUpdater.UpdateException {
flushBufferToDb();
TempAppProvider.Helper.commit(context);
TempApkProvider.Helper.commit(context);
TempAppProvider.Helper.commitAppsAndApks(context);
RepoProvider.Helper.update(context, repo, repoDetailsToSave);
}

View File

@ -17,26 +17,23 @@ public class TempApkProvider extends ApkProvider {
private static final String PROVIDER_NAME = "TempApkProvider";
static final String TABLE_TEMP_APK = "temp_fdroid_apk";
static final String TABLE_TEMP_APK = "temp_" + DBHelper.TABLE_APK;
private static final String PATH_INIT = "init";
private static final String PATH_COMMIT = "commit";
private static final int CODE_INIT = 10000;
private static final int CODE_COMMIT = CODE_INIT + 1;
private static final UriMatcher matcher = new UriMatcher(-1);
static {
matcher.addURI(getAuthority(), PATH_INIT, CODE_INIT);
matcher.addURI(getAuthority(), PATH_COMMIT, CODE_COMMIT);
matcher.addURI(getAuthority(), PATH_APK + "/#/*", CODE_SINGLE);
matcher.addURI(getAuthority(), PATH_REPO_APK + "/#/*", CODE_REPO_APK);
}
@Override
protected String getTableName() {
return "temp_" + super.getTableName();
return TABLE_TEMP_APK;
}
public static String getAuthority() {
@ -76,15 +73,6 @@ public class TempApkProvider extends ApkProvider {
context.getContentResolver().insert(uri, new ContentValues());
}
/**
* Saves data from the temp table to the apk table, by removing _EVERYTHING_ from the real
* apk table and inserting all of the records from here. The temporary table is then removed.
*/
public static void commit(Context context) {
Uri uri = Uri.withAppendedPath(getContentUri(), PATH_COMMIT);
context.getContentResolver().insert(uri, new ContentValues());
}
}
@Override
@ -93,9 +81,6 @@ public class TempApkProvider extends ApkProvider {
case CODE_INIT:
initTable();
return null;
case CODE_COMMIT:
commitTable();
return null;
default:
return super.insert(uri, values);
}
@ -140,10 +125,4 @@ public class TempApkProvider extends ApkProvider {
write().execSQL("CREATE TABLE " + getTableName() + " AS SELECT * FROM " + DBHelper.TABLE_APK);
}
private void commitTable() {
Log.i(TAG, "Deleting all apks from " + DBHelper.TABLE_APK + " so they can be copied from " + getTableName());
write().execSQL("DELETE FROM " + DBHelper.TABLE_APK);
write().execSQL("INSERT INTO " + DBHelper.TABLE_APK + " SELECT * FROM " + getTableName());
getContext().getContentResolver().notifyChange(ApkProvider.getContentUri(), null);
}
}

View File

@ -3,9 +3,12 @@ package org.fdroid.fdroid.data;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;
import org.fdroid.fdroid.Utils;
/**
* This class does all of its operations in a temporary sqlite table.
*/
@ -15,7 +18,7 @@ public class TempAppProvider extends AppProvider {
private static final String PROVIDER_NAME = "TempAppProvider";
private static final String TABLE_TEMP_APP = "temp_fdroid_app";
private static final String TABLE_TEMP_APP = "temp_" + DBHelper.TABLE_APP;
private static final String PATH_INIT = "init";
private static final String PATH_COMMIT = "commit";
@ -63,11 +66,10 @@ public class TempAppProvider extends AppProvider {
* Saves data from the temp table to the apk table, by removing _EVERYTHING_ from the real
* apk table and inserting all of the records from here. The temporary table is then removed.
*/
public static void commit(Context context) {
public static void commitAppsAndApks(Context context) {
Uri uri = Uri.withAppendedPath(getContentUri(), PATH_COMMIT);
context.getContentResolver().insert(uri, new ContentValues());
}
}
@Override
@ -115,9 +117,25 @@ public class TempAppProvider extends AppProvider {
}
private void commitTable() {
Log.i(TAG, "Deleting all apks from " + DBHelper.TABLE_APP + " so they can be copied from " + getTableName());
write().execSQL("DELETE FROM " + DBHelper.TABLE_APP);
write().execSQL("INSERT INTO " + DBHelper.TABLE_APP + " SELECT * FROM " + getTableName());
getContext().getContentResolver().notifyChange(AppProvider.getContentUri(), null);
final SQLiteDatabase db = write();
try {
db.beginTransaction();
Log.i(TAG, "Renaming " + TABLE_TEMP_APP + " to " + DBHelper.TABLE_APP);
db.execSQL("DROP TABLE " + DBHelper.TABLE_APP);
db.execSQL("ALTER TABLE " + TABLE_TEMP_APP + " RENAME TO " + DBHelper.TABLE_APP);
Log.i(TAG, "Renaming " + TempApkProvider.TABLE_TEMP_APK + " to " + DBHelper.TABLE_APK);
db.execSQL("DROP TABLE " + DBHelper.TABLE_APK);
db.execSQL("ALTER TABLE " + TempApkProvider.TABLE_TEMP_APK + " RENAME TO " + DBHelper.TABLE_APK);
Utils.debugLog(TAG, "Successfully renamed both tables, will commit transaction");
db.setTransactionSuccessful();
getContext().getContentResolver().notifyChange(AppProvider.getContentUri(), null);
getContext().getContentResolver().notifyChange(ApkProvider.getContentUri(), null);
} finally {
db.endTransaction();
}
}
}