Regularly analyze database tables to improve index usage.

This commit is contained in:
Greyson Parrelli
2024-04-09 16:55:25 -04:00
committed by GitHub
parent 713298109a
commit 982f602178
6 changed files with 169 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package org.thoughtcrime.securesms.service
import android.content.Context
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
import org.thoughtcrime.securesms.jobs.AnalyzeDatabaseJob
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.thoughtcrime.securesms.util.toMillis
import java.time.LocalDateTime
/**
* Schedules database analysis to happen everyday at 3am.
*/
class AnalyzeDatabaseAlarmListener : PersistentAlarmManagerListener() {
companion object {
@JvmStatic
fun schedule(context: Context?) {
AnalyzeDatabaseAlarmListener().onReceive(context, getScheduleIntent())
}
}
override fun shouldScheduleExact(): Boolean {
return true
}
override fun getNextScheduledExecutionTime(context: Context): Long {
var nextTime = SignalStore.misc().nextDatabaseAnalysisTime
if (nextTime == 0L) {
nextTime = getNextTime()
SignalStore.misc().nextDatabaseAnalysisTime = nextTime
}
return nextTime
}
override fun onAlarm(context: Context, scheduledTime: Long): Long {
ApplicationDependencies.getJobManager().add(AnalyzeDatabaseJob())
val nextTime = getNextTime()
SignalStore.misc().nextDatabaseAnalysisTime = nextTime
return nextTime
}
private fun getNextTime(): Long {
return LocalDateTime
.now()
.plusDays(1)
.withHour(3)
.withMinute(0)
.withSecond(0)
.toMillis()
}
}