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:
parent
2067f15b27
commit
9727dc505c
@ -391,7 +391,6 @@ public class UpdateService extends IntentService implements ProgressListener {
|
|||||||
if (!changes) {
|
if (!changes) {
|
||||||
Utils.debugLog(TAG, "Not checking app details or compatibility, because all repos were up to date.");
|
Utils.debugLog(TAG, "Not checking app details or compatibility, because all repos were up to date.");
|
||||||
} else {
|
} else {
|
||||||
sendStatus(this, STATUS_INFO, getString(R.string.status_checking_compatibility));
|
|
||||||
notifyContentProviders();
|
notifyContentProviders();
|
||||||
|
|
||||||
if (prefs.getBoolean(Preferences.PREF_UPD_NOTIFY, true)) {
|
if (prefs.getBoolean(Preferences.PREF_UPD_NOTIFY, true)) {
|
||||||
|
@ -78,8 +78,7 @@ public class RepoPersister {
|
|||||||
|
|
||||||
public void commit(ContentValues repoDetailsToSave) throws RepoUpdater.UpdateException {
|
public void commit(ContentValues repoDetailsToSave) throws RepoUpdater.UpdateException {
|
||||||
flushBufferToDb();
|
flushBufferToDb();
|
||||||
TempAppProvider.Helper.commit(context);
|
TempAppProvider.Helper.commitAppsAndApks(context);
|
||||||
TempApkProvider.Helper.commit(context);
|
|
||||||
RepoProvider.Helper.update(context, repo, repoDetailsToSave);
|
RepoProvider.Helper.update(context, repo, repoDetailsToSave);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,26 +17,23 @@ public class TempApkProvider extends ApkProvider {
|
|||||||
|
|
||||||
private static final String PROVIDER_NAME = "TempApkProvider";
|
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_INIT = "init";
|
||||||
private static final String PATH_COMMIT = "commit";
|
|
||||||
|
|
||||||
private static final int CODE_INIT = 10000;
|
private static final int CODE_INIT = 10000;
|
||||||
private static final int CODE_COMMIT = CODE_INIT + 1;
|
|
||||||
|
|
||||||
private static final UriMatcher matcher = new UriMatcher(-1);
|
private static final UriMatcher matcher = new UriMatcher(-1);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
matcher.addURI(getAuthority(), PATH_INIT, CODE_INIT);
|
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_APK + "/#/*", CODE_SINGLE);
|
||||||
matcher.addURI(getAuthority(), PATH_REPO_APK + "/#/*", CODE_REPO_APK);
|
matcher.addURI(getAuthority(), PATH_REPO_APK + "/#/*", CODE_REPO_APK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getTableName() {
|
protected String getTableName() {
|
||||||
return "temp_" + super.getTableName();
|
return TABLE_TEMP_APK;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getAuthority() {
|
public static String getAuthority() {
|
||||||
@ -76,15 +73,6 @@ public class TempApkProvider extends ApkProvider {
|
|||||||
context.getContentResolver().insert(uri, new ContentValues());
|
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
|
@Override
|
||||||
@ -93,9 +81,6 @@ public class TempApkProvider extends ApkProvider {
|
|||||||
case CODE_INIT:
|
case CODE_INIT:
|
||||||
initTable();
|
initTable();
|
||||||
return null;
|
return null;
|
||||||
case CODE_COMMIT:
|
|
||||||
commitTable();
|
|
||||||
return null;
|
|
||||||
default:
|
default:
|
||||||
return super.insert(uri, values);
|
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);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,12 @@ package org.fdroid.fdroid.data;
|
|||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.UriMatcher;
|
import android.content.UriMatcher;
|
||||||
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.fdroid.fdroid.Utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class does all of its operations in a temporary sqlite table.
|
* 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 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_INIT = "init";
|
||||||
private static final String PATH_COMMIT = "commit";
|
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
|
* 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.
|
* 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);
|
Uri uri = Uri.withAppendedPath(getContentUri(), PATH_COMMIT);
|
||||||
context.getContentResolver().insert(uri, new ContentValues());
|
context.getContentResolver().insert(uri, new ContentValues());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -115,9 +117,25 @@ public class TempAppProvider extends AppProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void commitTable() {
|
private void commitTable() {
|
||||||
Log.i(TAG, "Deleting all apks from " + DBHelper.TABLE_APP + " so they can be copied from " + getTableName());
|
final SQLiteDatabase db = write();
|
||||||
write().execSQL("DELETE FROM " + DBHelper.TABLE_APP);
|
try {
|
||||||
write().execSQL("INSERT INTO " + DBHelper.TABLE_APP + " SELECT * FROM " + getTableName());
|
db.beginTransaction();
|
||||||
getContext().getContentResolver().notifyChange(AppProvider.getContentUri(), null);
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user