Skip to content

Commit

Permalink
Prevent thread trimming from gumming up the database.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Jan 27, 2023
1 parent 30c33fd commit 7348224
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3322,24 +3322,18 @@ int deleteMessagesInThreadBeforeDate(long threadId, long date) {
SQLiteDatabase db = databaseHelper.getSignalWritableDatabase();
String where = THREAD_ID + " = ? AND " + DATE_RECEIVED + " < " + date;

int count = db.delete(TABLE_NAME, where, SqlUtil.buildArgs(threadId));

if (count > 0) {
OptimizeMessageSearchIndexJob.enqueue();
}

return count;
return db.delete(TABLE_NAME, where, SqlUtil.buildArgs(threadId));
}

void deleteAbandonedMessages() {
int deleteAbandonedMessages() {
SQLiteDatabase db = databaseHelper.getSignalWritableDatabase();
String where = THREAD_ID + " NOT IN (SELECT _id FROM " + ThreadTable.TABLE_NAME + ")";

int deletes = db.delete(TABLE_NAME, where, null);
if (deletes > 0) {
Log.i(TAG, "Deleted " + deletes + " abandoned messages");
OptimizeMessageSearchIndexJob.enqueue();
}
return deletes;
}

public void deleteRemotelyDeletedStory(long messageId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import org.thoughtcrime.securesms.database.model.databaseprotos.BodyRangeList
import org.thoughtcrime.securesms.database.model.serialize
import org.thoughtcrime.securesms.groups.BadGroupIdException
import org.thoughtcrime.securesms.groups.GroupId
import org.thoughtcrime.securesms.jobs.OptimizeMessageSearchIndexJob
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.thoughtcrime.securesms.mms.SlideDeck
import org.thoughtcrime.securesms.mms.StickerSlide
Expand Down Expand Up @@ -311,6 +312,7 @@ class ThreadTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa

notifyAttachmentListeners()
notifyStickerPackListeners()
OptimizeMessageSearchIndexJob.enqueue()
}

fun trimThread(threadId: Long, length: Int, trimBeforeDate: Long) {
Expand All @@ -333,6 +335,7 @@ class ThreadTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa

notifyAttachmentListeners()
notifyStickerPackListeners()
OptimizeMessageSearchIndexJob.enqueue()
}

private fun trimThreadInternal(threadId: Long, length: Int, trimBeforeDate: Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.signal.core.util.ThreadUtil;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.database.SignalDatabase;
import org.thoughtcrime.securesms.jobmanager.impl.DefaultExecutorFactory;
import org.thoughtcrime.securesms.jobmanager.impl.JsonDataSerializer;
import org.thoughtcrime.securesms.jobmanager.persistence.JobSpec;
Expand Down Expand Up @@ -60,7 +61,12 @@ public class JobManager implements ConstraintObserver.Notifier {
public JobManager(@NonNull Application application, @NonNull Configuration configuration) {
this.application = application;
this.configuration = configuration;
this.executor = ThreadUtil.trace(new FilteredExecutor(configuration.getExecutorFactory().newSingleThreadExecutor("signal-JobManager"), ThreadUtil::isMainThread));
this.executor = ThreadUtil.trace(new FilteredExecutor(configuration.getExecutorFactory().newSingleThreadExecutor("signal-JobManager"), () -> {
if (SignalDatabase.inTransaction()) {
Log.w(TAG, "Enqueuing a job while in a transaction!", new Throwable());
}
return ThreadUtil.isMainThread();
}));
this.jobTracker = configuration.getJobTracker();
this.jobController = new JobController(application,
configuration.getJobStorage(),
Expand Down

0 comments on commit 7348224

Please sign in to comment.