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

Commit f09f2d40 authored by cketti's avatar cketti
Browse files

Move LocalStore.compact() to MessageStore

parent baa94b84
Loading
Loading
Loading
Loading
+4 −7
Original line number Diff line number Diff line
@@ -2509,16 +2509,13 @@ public class MessagingController {
            @Override
            public void run() {
                try {
                    LocalStore localStore = localStoreProvider.getInstance(account);
                    long oldSize = localStore.getSize();
                    localStore.compact();
                    long newSize = localStore.getSize();
                    MessageStore messageStore = messageStoreManager.getMessageStore(account);
                    long oldSize = messageStore.getSize();
                    messageStore.compact();
                    long newSize = messageStore.getSize();
                    for (MessagingListener l : getListeners(ml)) {
                        l.accountSizeChanged(account, oldSize, newSize);
                    }
                } catch (UnavailableStorageException e) {
                    Timber.i("Failed to compact account because storage is not available - trying again later.");
                    throw new UnavailableAccountException(e);
                } catch (Exception e) {
                    Timber.e(e, "Failed to compact account %s", account.getDescription());
                }
+0 −45
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ import android.text.TextUtils;
import com.fsck.k9.Account;
import com.fsck.k9.Clock;
import com.fsck.k9.DI;
import com.fsck.k9.K9;
import com.fsck.k9.controller.MessageCounts;
import com.fsck.k9.Preferences;
import com.fsck.k9.controller.MessagingControllerCommands.PendingCommand;
@@ -235,50 +234,6 @@ public class LocalStore {
        return outboxStateRepository;
    }

    public long getSize() throws MessagingException {

        final StorageManager storageManager = StorageManager.getInstance(context);

        final File attachmentDirectory = storageManager.getAttachmentDirectory(account.getUuid(),
                                         database.getStorageProviderId());

        return database.execute(false, new DbCallback<Long>() {
            @Override
            public Long doDbWork(final SQLiteDatabase db) {
                final File[] files = attachmentDirectory.listFiles();
                long attachmentLength = 0;
                if (files != null) {
                    for (File file : files) {
                        if (file.exists()) {
                            attachmentLength += file.length();
                        }
                    }
                }

                final File dbFile = storageManager.getDatabase(account.getUuid(), database.getStorageProviderId());
                return dbFile.length() + attachmentLength;
            }
        });
    }

    public void compact() throws MessagingException {
        if (K9.isDebugLoggingEnabled()) {
            Timber.i("Before compaction size = %d", getSize());
        }

        database.execute(false, new DbCallback<Void>() {
            @Override
            public Void doDbWork(final SQLiteDatabase db) throws WrappedException {
                db.execSQL("VACUUM");
                return null;
            }
        });

        if (K9.isDebugLoggingEnabled()) {
            Timber.i("After compaction size = %d", getSize());
        }
    }

    public LocalFolder getFolder(String serverId) {
        return new LocalFolder(this, serverId);
    }
+10 −0
Original line number Diff line number Diff line
@@ -125,6 +125,11 @@ interface MessageStore {
     */
    fun getLastUid(folderId: Long): Long?

    /**
     * Return the size of this message store in bytes.
     */
    fun getSize(): Long

    /**
     * Remove messages from the store.
     */
@@ -267,4 +272,9 @@ interface MessageStore {
     * Create or update a number property associated with the given folder.
     */
    fun setFolderExtraNumber(folderId: Long, name: String, value: Long)

    /**
     * Optimize the message store with the goal of using the minimal amount of disk space.
     */
    fun compact()
}
+40 −0
Original line number Diff line number Diff line
package com.fsck.k9.storage.messages

import com.fsck.k9.mailstore.LockableDatabase
import com.fsck.k9.mailstore.StorageManager
import timber.log.Timber

internal class DatabaseOperations(
    private val lockableDatabase: LockableDatabase,
    val storageManager: StorageManager,
    val accountUuid: String
) {
    fun getSize(): Long {
        val storageProviderId = lockableDatabase.storageProviderId
        val attachmentDirectory = storageManager.getAttachmentDirectory(accountUuid, storageProviderId)

        return lockableDatabase.execute(false) {
            val attachmentFiles = attachmentDirectory.listFiles() ?: emptyArray()
            val attachmentsSize = attachmentFiles.asSequence()
                .filter { file -> file.exists() }
                .fold(initial = 0L) { accumulatedSize, file ->
                    accumulatedSize + file.length()
                }

            val databaseFile = storageManager.getDatabase(accountUuid, storageProviderId)
            val databaseSize = databaseFile.length()

            databaseSize + attachmentsSize
        }
    }

    fun compact() {
        Timber.i("Before compaction size = %d", getSize())

        lockableDatabase.execute(false) { database ->
            database.execSQL("VACUUM")
        }

        Timber.i("After compaction size = %d", getSize())
    }
}
+9 −0
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ class K9MessageStore(
    private val updateFolderOperations = UpdateFolderOperations(database)
    private val deleteFolderOperations = DeleteFolderOperations(database, attachmentFileManager)
    private val keyValueStoreOperations = KeyValueStoreOperations(database)
    private val databaseOperations = DatabaseOperations(database, storageManager, accountUuid)

    override fun saveRemoteMessage(folderId: Long, messageServerId: String, messageData: SaveMessageData) {
        saveMessageOperations.saveRemoteMessage(folderId, messageServerId, messageData)
@@ -133,6 +134,10 @@ class K9MessageStore(
        return retrieveFolderOperations.getFolderId(folderServerId)
    }

    override fun getSize(): Long {
        return databaseOperations.getSize()
    }

    override fun changeFolder(folderServerId: String, name: String, type: FolderType) {
        updateFolderOperations.changeFolder(folderServerId, name, type)
    }
@@ -208,4 +213,8 @@ class K9MessageStore(
    override fun setFolderExtraNumber(folderId: Long, name: String, value: Long) {
        return keyValueStoreOperations.setFolderExtraNumber(folderId, name, value)
    }

    override fun compact() {
        return databaseOperations.compact()
    }
}