mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 00:59:49 +01:00
Improve reliability of rebuilding the search index.
This commit is contained in:
committed by
Alex Hart
parent
6682815663
commit
1aed8eefcd
@@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.database
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.database.Cursor
|
||||
import android.database.sqlite.SQLiteException
|
||||
import android.text.TextUtils
|
||||
import net.zetetic.database.sqlcipher.SQLiteDatabase
|
||||
import org.intellij.lang.annotations.Language
|
||||
@@ -141,34 +142,43 @@ class SearchTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa
|
||||
*
|
||||
* Warning: This is a potentially extremely-costly operation! It can take 10+ seconds on large installs and/or slow devices.
|
||||
* Be smart about where you call this.
|
||||
*
|
||||
* @return True if the rebuild was successful, otherwise false.
|
||||
*/
|
||||
fun rebuildIndex(batchSize: Long = 10_000L) {
|
||||
val maxId: Long = SignalDatabase.messages.getNextId()
|
||||
fun rebuildIndex(batchSize: Long = 10_000L): Boolean {
|
||||
try {
|
||||
val maxId: Long = SignalDatabase.messages.getNextId()
|
||||
|
||||
if (!SqlUtil.tableExists(readableDatabase, FTS_TABLE_NAME)) {
|
||||
Log.w(TAG, "FTS table does not exist. Rebuilding.")
|
||||
fullyResetTables()
|
||||
return
|
||||
Log.i(TAG, "Re-indexing. Operating on ID's 1-$maxId in steps of $batchSize.")
|
||||
|
||||
for (i in 1..maxId step batchSize) {
|
||||
Log.i(TAG, "Reindexing ID's [$i, ${i + batchSize})")
|
||||
|
||||
writableDatabase.withinTransaction { db ->
|
||||
db.execSQL(
|
||||
"""
|
||||
INSERT INTO $FTS_TABLE_NAME ($ID, $BODY)
|
||||
SELECT
|
||||
${MessageTable.ID},
|
||||
${MessageTable.BODY}
|
||||
FROM
|
||||
${MessageTable.TABLE_NAME}
|
||||
WHERE
|
||||
${MessageTable.ID} >= $i AND
|
||||
${MessageTable.ID} < ${i + batchSize}
|
||||
"""
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: SQLiteException) {
|
||||
Log.w(TAG, "Failed to rebuild index!", e)
|
||||
return false
|
||||
} catch (e: IllegalStateException) {
|
||||
Log.w(TAG, "Failed to rebuild index!", e)
|
||||
return false
|
||||
}
|
||||
|
||||
Log.i(TAG, "Re-indexing. Operating on ID's 1-$maxId in steps of $batchSize.")
|
||||
|
||||
for (i in 1..maxId step batchSize) {
|
||||
Log.i(TAG, "Reindexing ID's [$i, ${i + batchSize})")
|
||||
writableDatabase.execSQL(
|
||||
"""
|
||||
INSERT INTO $FTS_TABLE_NAME ($ID, $BODY)
|
||||
SELECT
|
||||
${MessageTable.ID},
|
||||
${MessageTable.BODY}
|
||||
FROM
|
||||
${MessageTable.TABLE_NAME}
|
||||
WHERE
|
||||
${MessageTable.ID} >= $i AND
|
||||
${MessageTable.ID} < ${i + batchSize}
|
||||
"""
|
||||
)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -249,11 +259,16 @@ class SearchTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa
|
||||
|
||||
try {
|
||||
Log.w(TAG, "[fullyResetTables] Dropping tables and triggers...")
|
||||
db.execSQL("DROP TABLE IF EXISTS $FTS_TABLE_NAME")
|
||||
db.execSQL("DROP TABLE IF EXISTS ${FTS_TABLE_NAME}_config")
|
||||
db.execSQL("DROP TABLE IF EXISTS ${FTS_TABLE_NAME}_content")
|
||||
db.execSQL("DROP TABLE IF EXISTS ${FTS_TABLE_NAME}_data")
|
||||
db.execSQL("DROP TABLE IF EXISTS ${FTS_TABLE_NAME}_idx")
|
||||
|
||||
// Due to issues we've had in the past, the delete sequence here is very particular. It mimics the "safe drop" process in the SQLite source code
|
||||
// that prevents weird vtable constructor issues when dropping potentially-corrupt tables. https://sqlite.org/src/info/4db9258a78?ln=1549-1592
|
||||
if (SqlUtil.tableExists(db, FTS_TABLE_NAME)) {
|
||||
db.execSQL("DELETE FROM ${FTS_TABLE_NAME}_data")
|
||||
db.execSQL("DELETE FROM ${FTS_TABLE_NAME}_config")
|
||||
db.execSQL("INSERT INTO ${FTS_TABLE_NAME}_data VALUES(10, X'0000000000')")
|
||||
db.execSQL("INSERT INTO ${FTS_TABLE_NAME}_config VALUES('version', 4)")
|
||||
db.execSQL("DROP TABLE $FTS_TABLE_NAME")
|
||||
}
|
||||
db.execSQL("DROP TRIGGER IF EXISTS $TRIGGER_AFTER_INSERT")
|
||||
db.execSQL("DROP TRIGGER IF EXISTS $TRIGGER_AFTER_DELETE")
|
||||
db.execSQL("DROP TRIGGER IF EXISTS $TRIGGER_AFTER_UPDATE")
|
||||
|
||||
Reference in New Issue
Block a user