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();