EN: Run cleanup in IO thread, improve performance

This commit is contained in:
Marvin W 2020-09-09 00:50:58 +02:00
parent a016feba35
commit f9cc9249d5
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
2 changed files with 23 additions and 7 deletions

View File

@ -8,6 +8,9 @@ package org.microg.gms.nearby.exposurenotification
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import androidx.lifecycle.LifecycleService import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.microg.gms.common.ForegroundServiceContext import org.microg.gms.common.ForegroundServiceContext
class CleanupService : LifecycleService() { class CleanupService : LifecycleService() {
@ -15,12 +18,18 @@ class CleanupService : LifecycleService() {
ForegroundServiceContext.completeForegroundService(this, intent, TAG) ForegroundServiceContext.completeForegroundService(this, intent, TAG)
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
if (isNeeded(this)) { if (isNeeded(this)) {
ExposureDatabase.with(this@CleanupService) { lifecycleScope.launchWhenStarted {
it.dailyCleanup() launch(Dispatchers.IO) {
ExposureDatabase.with(this@CleanupService) {
it.dailyCleanup()
}
ExposurePreferences(this@CleanupService).lastCleanup = System.currentTimeMillis()
}
stopSelf()
} }
ExposurePreferences(this).lastCleanup = System.currentTimeMillis() } else {
stopSelf()
} }
stopSelf()
return START_NOT_STICKY return START_NOT_STICKY
} }

View File

@ -62,6 +62,9 @@ class ExposureDatabase private constructor(private val context: Context) : SQLit
if (oldVersion < 3) { if (oldVersion < 3) {
db.execSQL("CREATE TABLE $TABLE_APP_PERMS(package TEXT NOT NULL, sig TEXT NOT NULL, perm TEXT NOT NULL, timestamp INTEGER NOT NULL);") db.execSQL("CREATE TABLE $TABLE_APP_PERMS(package TEXT NOT NULL, sig TEXT NOT NULL, perm TEXT NOT NULL, timestamp INTEGER NOT NULL);")
} }
if (oldVersion < 4) {
db.execSQL("CREATE INDEX IF NOT EXISTS index_${TABLE_DIAGNOSIS}_tcid ON $TABLE_DIAGNOSIS(tcid);")
}
} }
fun SQLiteDatabase.delete(table: String, whereClause: String, args: LongArray): Int = fun SQLiteDatabase.delete(table: String, whereClause: String, args: LongArray): Int =
@ -75,11 +78,15 @@ class ExposureDatabase private constructor(private val context: Context) : SQLit
try { try {
val rollingStartTime = currentRollingStartNumber * ROLLING_WINDOW_LENGTH * 1000 - TimeUnit.DAYS.toMillis(KEEP_DAYS.toLong()) val rollingStartTime = currentRollingStartNumber * ROLLING_WINDOW_LENGTH * 1000 - TimeUnit.DAYS.toMillis(KEEP_DAYS.toLong())
val advertisements = delete(TABLE_ADVERTISEMENTS, "timestamp < ?", longArrayOf(rollingStartTime)) val advertisements = delete(TABLE_ADVERTISEMENTS, "timestamp < ?", longArrayOf(rollingStartTime))
Log.d(TAG, "Deleted on daily cleanup: $advertisements adv")
val appLogEntries = delete(TABLE_APP_LOG, "timestamp < ?", longArrayOf(rollingStartTime)) val appLogEntries = delete(TABLE_APP_LOG, "timestamp < ?", longArrayOf(rollingStartTime))
Log.d(TAG, "Deleted on daily cleanup: $appLogEntries applogs")
val temporaryExposureKeys = delete(TABLE_TEK, "(rollingStartNumber + rollingPeriod) < ?", longArrayOf(rollingStartTime / ROLLING_WINDOW_LENGTH_MS)) val temporaryExposureKeys = delete(TABLE_TEK, "(rollingStartNumber + rollingPeriod) < ?", longArrayOf(rollingStartTime / ROLLING_WINDOW_LENGTH_MS))
val checkedTemporaryExposureKeys = delete(TABLE_TEK_CHECK, "(rollingStartNumber + rollingPeriod) < ?", longArrayOf(rollingStartTime / ROLLING_WINDOW_LENGTH_MS)) Log.d(TAG, "Deleted on daily cleanup: $temporaryExposureKeys teks")
val checkedTemporaryExposureKeys = delete(TABLE_TEK_CHECK, "rollingStartNumber < ?", longArrayOf(rollingStartTime / ROLLING_WINDOW_LENGTH_MS - ROLLING_PERIOD))
Log.d(TAG, "Deleted on daily cleanup: $checkedTemporaryExposureKeys cteks")
val appPerms = delete(TABLE_APP_PERMS, "timestamp < ?", longArrayOf(System.currentTimeMillis() - CONFIRM_PERMISSION_VALIDITY)) val appPerms = delete(TABLE_APP_PERMS, "timestamp < ?", longArrayOf(System.currentTimeMillis() - CONFIRM_PERMISSION_VALIDITY))
Log.d(TAG, "Deleted on daily cleanup: $advertisements adv, $appLogEntries applogs, $temporaryExposureKeys teks, $checkedTemporaryExposureKeys cteks, $appPerms perms") Log.d(TAG, "Deleted on daily cleanup: $appPerms perms")
setTransactionSuccessful() setTransactionSuccessful()
} finally { } finally {
endTransaction() endTransaction()
@ -553,7 +560,7 @@ class ExposureDatabase private constructor(private val context: Context) : SQLit
companion object { companion object {
private const val DB_NAME = "exposure.db" private const val DB_NAME = "exposure.db"
private const val DB_VERSION = 3 private const val DB_VERSION = 4
private const val TABLE_ADVERTISEMENTS = "advertisements" private const val TABLE_ADVERTISEMENTS = "advertisements"
private const val TABLE_APP_LOG = "app_log" private const val TABLE_APP_LOG = "app_log"
private const val TABLE_TEK = "tek" private const val TABLE_TEK = "tek"