Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3bee80a6 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #1890 from k9mail/fix-fts-oom

Fix OutOfMemoryError during database migration
parents 50d81f70 b0574804
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -896,6 +896,42 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
        }
    }

    public List<String> getAllMessageUids() 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 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<>();
+6 −10
Original line number Diff line number Diff line
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();
                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");