Optimize thread ID DB query.

This commit is contained in:
Nicholas 2023-11-08 09:53:55 -05:00 committed by Cody Henthorne
parent 39fbbe896f
commit d6fd6cb5a3

View file

@ -3,7 +3,6 @@ package org.thoughtcrime.securesms.database
import android.content.Context import android.content.Context
import android.database.Cursor import android.database.Cursor
import androidx.core.content.contentValuesOf import androidx.core.content.contentValuesOf
import org.signal.core.util.delete
import org.signal.core.util.logging.Log import org.signal.core.util.logging.Log
import org.signal.core.util.readToList import org.signal.core.util.readToList
import org.signal.core.util.requireLong import org.signal.core.util.requireLong
@ -13,7 +12,6 @@ import org.thoughtcrime.securesms.database.RemappedRecordTables.SharedColumns.ID
import org.thoughtcrime.securesms.database.RemappedRecordTables.SharedColumns.NEW_ID import org.thoughtcrime.securesms.database.RemappedRecordTables.SharedColumns.NEW_ID
import org.thoughtcrime.securesms.database.RemappedRecordTables.SharedColumns.OLD_ID import org.thoughtcrime.securesms.database.RemappedRecordTables.SharedColumns.OLD_ID
import org.thoughtcrime.securesms.recipients.RecipientId import org.thoughtcrime.securesms.recipients.RecipientId
import java.util.HashMap
/** /**
* The backing datastore for [RemappedRecords]. See that class for more details. * The backing datastore for [RemappedRecords]. See that class for more details.
@ -55,8 +53,6 @@ class RemappedRecordTables internal constructor(context: Context?, databaseHelpe
} }
fun getAllRecipientMappings(): Map<RecipientId, RecipientId> { fun getAllRecipientMappings(): Map<RecipientId, RecipientId> {
clearInvalidRecipientMappings()
val recipientMap: MutableMap<RecipientId, RecipientId> = HashMap() val recipientMap: MutableMap<RecipientId, RecipientId> = HashMap()
readableDatabase.withinTransaction { db -> readableDatabase.withinTransaction { db ->
@ -72,8 +68,6 @@ class RemappedRecordTables internal constructor(context: Context?, databaseHelpe
} }
fun getAllThreadMappings(): Map<Long, Long> { fun getAllThreadMappings(): Map<Long, Long> {
clearInvalidThreadMappings()
val threadMap: MutableMap<Long, Long> = HashMap() val threadMap: MutableMap<Long, Long> = HashMap()
readableDatabase.withinTransaction { db -> readableDatabase.withinTransaction { db ->
@ -95,7 +89,6 @@ class RemappedRecordTables internal constructor(context: Context?, databaseHelpe
} }
fun getAllRecipients(): Cursor { fun getAllRecipients(): Cursor {
clearInvalidRecipientMappings()
return readableDatabase return readableDatabase
.select() .select()
.from(Recipients.TABLE_NAME) .from(Recipients.TABLE_NAME)
@ -103,7 +96,6 @@ class RemappedRecordTables internal constructor(context: Context?, databaseHelpe
} }
fun getAllThreads(): Cursor { fun getAllThreads(): Cursor {
clearInvalidThreadMappings()
return readableDatabase return readableDatabase
.select() .select()
.from(Threads.TABLE_NAME) .from(Threads.TABLE_NAME)
@ -130,33 +122,5 @@ class RemappedRecordTables internal constructor(context: Context?, databaseHelpe
databaseHelper.signalWritableDatabase.insert(table, null, values) databaseHelper.signalWritableDatabase.insert(table, null, values)
} }
/**
* The old_id should never exist -- this class is intended to remap from IDs that were deleted.
*/
private fun clearInvalidRecipientMappings() {
val count = writableDatabase
.delete(Recipients.TABLE_NAME)
.where("$OLD_ID IN (SELECT ${RecipientTable.ID} FROM ${RecipientTable.TABLE_NAME})")
.run()
if (count > 0) {
Log.w(TAG, "Deleted $count invalid recipient mappings!", true)
}
}
/**
* The old_id should never exist -- this class is intended to remap from IDs that were deleted.
*/
private fun clearInvalidThreadMappings() {
val count = writableDatabase
.delete(Threads.TABLE_NAME)
.where("$OLD_ID IN (SELECT ${ThreadTable.ID} FROM ${ThreadTable.TABLE_NAME})")
.run()
if (count > 0) {
Log.w(TAG, "Deleted $count invalid thread mappings!", true)
}
}
private class Mapping(val oldId: Long, val newId: Long) private class Mapping(val oldId: Long, val newId: Long)
} }