Improve thread delete performance.

This commit is contained in:
Cody Henthorne
2024-10-02 10:19:34 -04:00
committed by Greyson Parrelli
parent 93609106b0
commit 66e6b5506e
6 changed files with 183 additions and 35 deletions

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2024 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.thoughtcrime.securesms.jobs
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.database.SignalDatabase.Companion.attachments
import org.thoughtcrime.securesms.dependencies.AppDependencies
import org.thoughtcrime.securesms.jobmanager.Job
import kotlin.time.Duration.Companion.days
/**
* Deletes attachment files that are no longer referenced in the database.
*/
class DeleteAbandonedAttachmentsJob private constructor(parameters: Parameters) : Job(parameters) {
companion object {
private val TAG = Log.tag(DeleteAbandonedAttachmentsJob::class)
const val KEY = "DeleteAbandonedAttachmentsJob"
fun enqueue() {
AppDependencies.jobManager.add(DeleteAbandonedAttachmentsJob())
}
}
constructor() : this(
parameters = Parameters.Builder()
.setMaxInstancesForFactory(2)
.setLifespan(1.days.inWholeMilliseconds)
.build()
)
override fun serialize(): ByteArray? = null
override fun getFactoryKey(): String = KEY
override fun onFailure() = Unit
override fun run(): Result {
val deletes = attachments.deleteAbandonedAttachmentFiles()
Log.i(TAG, "Deleted $deletes abandoned attachments.")
return Result.success()
}
class Factory : Job.Factory<DeleteAbandonedAttachmentsJob> {
override fun create(parameters: Parameters, serializedData: ByteArray?): DeleteAbandonedAttachmentsJob {
return DeleteAbandonedAttachmentsJob(parameters)
}
}
}

View File

@@ -140,6 +140,7 @@ public final class JobManagerFactories {
put(ConversationShortcutUpdateJob.KEY, new ConversationShortcutUpdateJob.Factory());
put(CopyAttachmentToArchiveJob.KEY, new CopyAttachmentToArchiveJob.Factory());
put(CreateReleaseChannelJob.KEY, new CreateReleaseChannelJob.Factory());
put(DeleteAbandonedAttachmentsJob.KEY, new DeleteAbandonedAttachmentsJob.Factory());
put(DirectoryRefreshJob.KEY, new DirectoryRefreshJob.Factory());
put(DonationReceiptRedemptionJob.KEY, new DonationReceiptRedemptionJob.Factory());
put(DownloadLatestEmojiDataJob.KEY, new DownloadLatestEmojiDataJob.Factory());