Improve routine around bulk attachment deletion.

This commit is contained in:
Cody Henthorne
2021-07-15 13:32:29 -04:00
committed by Greyson Parrelli
parent b04ca202f6
commit f65f4704c9
6 changed files with 83 additions and 8 deletions

View File

@@ -466,13 +466,12 @@ public class AttachmentDatabase extends Database {
public void trimAllAbandonedAttachments() {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
String selectAllMmsIds = "SELECT " + MmsDatabase.ID + " FROM " + MmsDatabase.TABLE_NAME;
String selectDataInUse = "SELECT DISTINCT " + DATA + " FROM " + TABLE_NAME + " WHERE " + QUOTE + " = 0 AND (" + MMS_ID + " IN (" + selectAllMmsIds + ") OR " + MMS_ID + " = " + PREUPLOAD_MESSAGE_ID + ")";
String where = MMS_ID + " NOT IN (" + selectAllMmsIds + ") AND " + DATA + " NOT IN (" + selectDataInUse + ")";
String where = MMS_ID + " != " + PREUPLOAD_MESSAGE_ID + " AND " + MMS_ID + " NOT IN (" + selectAllMmsIds + ")";
db.delete(TABLE_NAME, where, null);
}
public void deleteAbandonedAttachmentFiles() {
public int deleteAbandonedAttachmentFiles() {
Set<String> filesOnDisk = new HashSet<>();
Set<String> filesInDb = new HashSet<>();
@@ -495,6 +494,8 @@ public class AttachmentDatabase extends Database {
//noinspection ResultOfMethodCallIgnored
new File(filePath).delete();
}
return onDiskButNotInDatabase.size();
}
@SuppressWarnings("ResultOfMethodCallIgnored")

View File

@@ -255,6 +255,7 @@ public class ThreadDatabase extends Database {
GroupReceiptDatabase groupReceiptDatabase = DatabaseFactory.getGroupReceiptDatabase(context);
MmsSmsDatabase mmsSmsDatabase = DatabaseFactory.getMmsSmsDatabase(context);
MentionDatabase mentionDatabase = DatabaseFactory.getMentionDatabase(context);
int deletes = 0;
try (Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, new String[] { ID }, null, null, null, null, null)) {
while (cursor != null && cursor.moveToNext()) {
@@ -269,12 +270,15 @@ public class ThreadDatabase extends Database {
attachmentDatabase.trimAllAbandonedAttachments();
groupReceiptDatabase.deleteAbandonedRows();
mentionDatabase.deleteAbandonedMentions();
attachmentDatabase.deleteAbandonedAttachmentFiles();
deletes = attachmentDatabase.deleteAbandonedAttachmentFiles();
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (deletes > 0) {
Log.i(TAG, "Trim all threads caused " + deletes + " attachments to be deleted.");
}
notifyAttachmentListeners();
notifyStickerListeners();
@@ -291,6 +295,7 @@ public class ThreadDatabase extends Database {
GroupReceiptDatabase groupReceiptDatabase = DatabaseFactory.getGroupReceiptDatabase(context);
MmsSmsDatabase mmsSmsDatabase = DatabaseFactory.getMmsSmsDatabase(context);
MentionDatabase mentionDatabase = DatabaseFactory.getMentionDatabase(context);
int deletes = 0;
db.beginTransaction();
@@ -300,12 +305,15 @@ public class ThreadDatabase extends Database {
attachmentDatabase.trimAllAbandonedAttachments();
groupReceiptDatabase.deleteAbandonedRows();
mentionDatabase.deleteAbandonedMentions();
attachmentDatabase.deleteAbandonedAttachmentFiles();
deletes = attachmentDatabase.deleteAbandonedAttachmentFiles();
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (deletes > 0) {
Log.i(TAG, "Trim thread " + threadId + " caused " + deletes + " attachments to be deleted.");
}
notifyAttachmentListeners();
notifyStickerListeners();

View File

@@ -206,8 +206,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper implements SignalDatab
private static final int ABANDONED_MESSAGE_CLEANUP = 107;
private static final int THREAD_AUTOINCREMENT = 108;
private static final int MMS_AUTOINCREMENT = 109;
private static final int ABANDONED_ATTACHMENT_CLEANUP = 110;
private static final int DATABASE_VERSION = 109;
private static final int DATABASE_VERSION = 110;
private static final String DATABASE_NAME = "signal.db";
private final Context context;
@@ -1929,6 +1930,10 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper implements SignalDatab
smsStopwatch.stop(TAG);
}
if (oldVersion < ABANDONED_ATTACHMENT_CLEANUP) {
db.delete("part", "mid != -8675309 AND mid NOT IN (SELECT _id FROM mms)", null);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();

View File

@@ -31,6 +31,7 @@ import org.thoughtcrime.securesms.jobmanager.migrations.RetrieveProfileJobMigrat
import org.thoughtcrime.securesms.jobmanager.migrations.SendReadReceiptsJobMigration;
import org.thoughtcrime.securesms.migrations.AccountRecordMigrationJob;
import org.thoughtcrime.securesms.migrations.ApplyUnknownFieldsToSelfMigrationJob;
import org.thoughtcrime.securesms.migrations.AttachmentCleanupMigrationJob;
import org.thoughtcrime.securesms.migrations.AttributesMigrationJob;
import org.thoughtcrime.securesms.migrations.AvatarIdRemovalMigrationJob;
import org.thoughtcrime.securesms.migrations.AvatarMigrationJob;
@@ -166,6 +167,7 @@ public final class JobManagerFactories {
// Migrations
put(AccountRecordMigrationJob.KEY, new AccountRecordMigrationJob.Factory());
put(ApplyUnknownFieldsToSelfMigrationJob.KEY, new ApplyUnknownFieldsToSelfMigrationJob.Factory());
put(AttachmentCleanupMigrationJob.KEY, new AttachmentCleanupMigrationJob.Factory());
put(AttributesMigrationJob.KEY, new AttributesMigrationJob.Factory());
put(AvatarIdRemovalMigrationJob.KEY, new AvatarIdRemovalMigrationJob.Factory());
put(AvatarMigrationJob.KEY, new AvatarMigrationJob.Factory());

View File

@@ -77,9 +77,10 @@ public class ApplicationMigrations {
static final int SENDER_KEY = 35;
static final int SENDER_KEY_2 = 36;
static final int DB_AUTOINCREMENT = 37;
static final int ATTACHMENT_CLEANUP = 38;
}
public static final int CURRENT_VERSION = 37;
public static final int CURRENT_VERSION = 38;
/**
* This *must* be called after the {@link JobManager} has been instantiated, but *before* the call
@@ -322,7 +323,7 @@ public class ApplicationMigrations {
}
if (lastSeenVersion < Version.APPLY_UNIVERSAL_EXPIRE) {
jobs.put(Version.SMS_STORAGE_SYNC, new ApplyUnknownFieldsToSelfMigrationJob());
jobs.put(Version.APPLY_UNIVERSAL_EXPIRE, new ApplyUnknownFieldsToSelfMigrationJob());
}
if (lastSeenVersion < Version.SENDER_KEY) {
@@ -337,6 +338,10 @@ public class ApplicationMigrations {
jobs.put(Version.DB_AUTOINCREMENT, new DatabaseMigrationJob());
}
if (lastSeenVersion < Version.ATTACHMENT_CLEANUP) {
jobs.put(Version.ATTACHMENT_CLEANUP, new AttachmentCleanupMigrationJob());
}
return jobs;
}

View File

@@ -0,0 +1,54 @@
package org.thoughtcrime.securesms.migrations;
import androidx.annotation.NonNull;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.jobmanager.Job;
/**
* Check for abandoned attachments and delete them.
*/
public class AttachmentCleanupMigrationJob extends MigrationJob {
private static final String TAG = Log.tag(AttachmentCleanupMigrationJob.class);
public static final String KEY = "AttachmentCleanupMigrationJob";
AttachmentCleanupMigrationJob() {
this(new Parameters.Builder().build());
}
private AttachmentCleanupMigrationJob(@NonNull Parameters parameters) {
super(parameters);
}
@Override
public boolean isUiBlocking() {
return false;
}
@Override
public @NonNull String getFactoryKey() {
return KEY;
}
@Override
public void performMigration() {
int deletes = DatabaseFactory.getAttachmentDatabase(context).deleteAbandonedAttachmentFiles();
Log.i(TAG, "Deleted " + deletes + " abandoned attachments.");
}
@Override
boolean shouldRetry(@NonNull Exception e) {
return false;
}
public static class Factory implements Job.Factory<AttachmentCleanupMigrationJob> {
@Override
public @NonNull AttachmentCleanupMigrationJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new AttachmentCleanupMigrationJob(parameters);
}
}
}