-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3221 from k9mail/always_build_fts_table_last
Always build FTS table last
- Loading branch information
Showing
3 changed files
with
68 additions
and
45 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
k9mail/src/main/java/com/fsck/k9/mailstore/migrations/FullTextIndexer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.fsck.k9.mailstore.migrations | ||
|
||
|
||
import android.content.ContentValues | ||
import android.database.sqlite.SQLiteDatabase | ||
import com.fsck.k9.mail.FetchProfile | ||
import com.fsck.k9.mail.MessagingException | ||
import com.fsck.k9.mailstore.LocalFolder | ||
import com.fsck.k9.mailstore.LocalStore | ||
import timber.log.Timber | ||
|
||
|
||
internal class FullTextIndexer(val localStore: LocalStore, val database: SQLiteDatabase) { | ||
private val fulltextCreator = localStore.messageFulltextCreator | ||
private val fetchProfile = FetchProfile().apply { add(FetchProfile.Item.BODY) } | ||
|
||
fun indexAllMessages() { | ||
try { | ||
val folders = localStore.getPersonalNamespaces(true) | ||
for (folder in folders) { | ||
indexFolder(folder) | ||
} | ||
} catch (e: MessagingException) { | ||
Timber.e(e, "error indexing fulltext - skipping rest, fts index is incomplete!") | ||
} | ||
} | ||
|
||
private fun indexFolder(folder: LocalFolder) { | ||
val messageUids = folder.allMessageUids | ||
for (messageUid in messageUids) { | ||
indexMessage(folder, messageUid) | ||
} | ||
} | ||
|
||
private fun indexMessage(folder: LocalFolder, messageUid: String?) { | ||
val localMessage = folder.getMessage(messageUid) | ||
folder.fetch(listOf(localMessage), fetchProfile, null) | ||
|
||
val fulltext = fulltextCreator.createFulltext(localMessage) | ||
if (fulltext.isNullOrEmpty()) { | ||
Timber.d("no fulltext for msg id %d :(", localMessage.databaseId) | ||
} else { | ||
Timber.d("fulltext for msg id %d is %d chars long", localMessage.databaseId, fulltext.length) | ||
|
||
val values = ContentValues().apply { | ||
put("docid", localMessage.databaseId) | ||
put("fulltext", fulltext) | ||
} | ||
database.insert("messages_fulltext", null, values) | ||
} | ||
} | ||
} |
45 changes: 1 addition & 44 deletions
45
k9mail/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo55.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,11 @@ | ||
package com.fsck.k9.mailstore.migrations; | ||
|
||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import android.content.ContentValues; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.text.TextUtils; | ||
import timber.log.Timber; | ||
|
||
import com.fsck.k9.mail.FetchProfile; | ||
import com.fsck.k9.mail.MessagingException; | ||
import com.fsck.k9.mailstore.LocalFolder; | ||
import com.fsck.k9.mailstore.LocalMessage; | ||
import com.fsck.k9.mailstore.LocalStore; | ||
import com.fsck.k9.message.extractors.MessageFulltextCreator; | ||
|
||
|
||
class MigrationTo55 { | ||
static void createFtsSearchTable(SQLiteDatabase db, MigrationsHelper migrationsHelper) { | ||
static void createFtsSearchTable(SQLiteDatabase db) { | ||
db.execSQL("CREATE VIRTUAL TABLE messages_fulltext USING fts4 (fulltext)"); | ||
|
||
LocalStore localStore = migrationsHelper.getLocalStore(); | ||
MessageFulltextCreator fulltextCreator = localStore.getMessageFulltextCreator(); | ||
|
||
try { | ||
List<LocalFolder> folders = localStore.getPersonalNamespaces(true); | ||
ContentValues cv = new ContentValues(); | ||
FetchProfile fp = new FetchProfile(); | ||
fp.add(FetchProfile.Item.BODY); | ||
for (LocalFolder folder : folders) { | ||
List<String> messageUids = folder.getAllMessageUids(); | ||
for (String messageUid : messageUids) { | ||
LocalMessage localMessage = folder.getMessage(messageUid); | ||
folder.fetch(Collections.singletonList(localMessage), fp, null); | ||
|
||
String fulltext = fulltextCreator.createFulltext(localMessage); | ||
if (!TextUtils.isEmpty(fulltext)) { | ||
Timber.d("fulltext for msg id %d is %d chars long", localMessage.getDatabaseId(), fulltext.length()); | ||
cv.clear(); | ||
cv.put("docid", localMessage.getDatabaseId()); | ||
cv.put("fulltext", fulltext); | ||
db.insert("messages_fulltext", null, cv); | ||
} else { | ||
Timber.d("no fulltext for msg id %d :(", localMessage.getDatabaseId()); | ||
} | ||
} | ||
} | ||
} catch (MessagingException e) { | ||
Timber.e(e, "error indexing fulltext - skipping rest, fts index is incomplete!"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters