iterate messages by uid, don't keep messages around longer than necessary (fixes #1879)

This commit is contained in:
Vincent Breitmoser 2016-12-28 16:38:54 +01:00
parent 217b5bf9f4
commit e7653c4725
2 changed files with 43 additions and 10 deletions

View file

@ -896,6 +896,43 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
}
}
public List<String> getAllMessageUids(final boolean includeDeleted) throws MessagingException {
try {
return localStore.database.execute(false, new DbCallback<List<String>>() {
@Override
public List<String> doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
Cursor cursor = null;
ArrayList<String> result = new ArrayList<>();
try {
open(OPEN_MODE_RO);
cursor = db.rawQuery(
"SELECT uid " +
"FROM messages " +
"WHERE empty = 0 AND " +
(includeDeleted ? "" : "deleted = 0 AND ") +
"folder_id = ? ORDER BY date DESC",
new String[] { Long.toString(mFolderId) });
while (cursor.moveToNext()) {
String uid = cursor.getString(0);
result.add(uid);
}
} catch (MessagingException e) {
throw new WrappedException(e);
} finally {
Utility.closeQuietly(cursor);
}
return result;
}
});
} catch (WrappedException e) {
throw(MessagingException) e.getCause();
}
}
public List<LocalMessage> getMessagesByUids(@NonNull List<String> uids) throws MessagingException {
open(OPEN_MODE_RW);
List<LocalMessage> messages = new ArrayList<>();

View file

@ -1,9 +1,7 @@
package com.fsck.k9.mailstore.migrations;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import android.content.ContentValues;
@ -20,8 +18,8 @@ import com.fsck.k9.mailstore.LocalStore;
import com.fsck.k9.message.extractors.MessageFulltextCreator;
public class MigrationTo55 {
public static void createFtsSearchTable(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
class MigrationTo55 {
static void createFtsSearchTable(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
db.execSQL("CREATE VIRTUAL TABLE messages_fulltext USING fts4 (fulltext)");
LocalStore localStore = migrationsHelper.getLocalStore();
@ -33,13 +31,11 @@ public class MigrationTo55 {
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.BODY);
for (LocalFolder folder : folders) {
Iterator<LocalMessage> localMessages = new ArrayList<>(folder.getMessages(null, false)).iterator();
while (localMessages.hasNext()) {
LocalMessage localMessage = localMessages.next();
// The LocalMessage objects are heavy once they have been loaded, so we free them asap
localMessages.remove();
List<String> messageUids = folder.getAllMessageUids(false);
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)) {
Log.d(K9.LOG_TAG, "fulltext for msg id " + localMessage.getId() + " is " + fulltext.length() + " chars long");