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

Commit d5c8c35f authored by cketti's avatar cketti
Browse files

Remove ImapFolder caching

The cache was causing problems when a folder was accessed in two threads at the same time. There was also no invalidation mechanism that removed folders that had been removed from the server.

The easy fix is to get rid of this cache. There's no state in ImapFolder that survives closing the folder anyway.
parent d25d4512
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -31,19 +31,13 @@ class ImapFolder internal constructor(
    val serverId: String,
    private val folderNameCodec: FolderNameCodec
) {
    @Volatile
    private var uidNext = -1L

    @Volatile
    private var connection: ImapConnection? = null

    @Volatile
    private var exists = false
    private var inSearch = false
    private var canCreateKeywords = false
    private var uidValidity: Long? = null

    @Volatile
    var messageCount = -1
        private set

+1 −18
Original line number Diff line number Diff line
@@ -53,14 +53,6 @@ public class ImapStore {
    private final Deque<ImapConnection> connections = new LinkedList<>();
    private FolderNameCodec folderNameCodec;

    /**
     * Cache of ImapFolder objects. ImapFolders are attached to a given folder on the server
     * and as long as their associated connection remains open they are reusable between
     * requests. This cache lets us make sure we always reuse, if possible, for a given
     * folder name.
     */
    private final Map<String, ImapFolder> folderCache = new HashMap<>();


    public ImapStore(ServerSettings serverSettings, ImapStoreConfig config,
            TrustedSocketFactory trustedSocketFactory, ConnectivityManager connectivityManager,
@@ -90,16 +82,7 @@ public class ImapStore {
    }

    public ImapFolder getFolder(String name) {
        ImapFolder folder;
        synchronized (folderCache) {
            folder = folderCache.get(name);
            if (folder == null) {
                folder = new ImapFolder(this, name);
                folderCache.put(name, folder);
            }
        }

        return folder;
        return new ImapFolder(this, name);
    }

    String getCombinedPrefix() {
+0 −10
Original line number Diff line number Diff line
@@ -60,16 +60,6 @@ public class ImapStoreTest {
        assertEquals(ImapFolder.class, result.getClass());
    }

    @Test
    public void getFolder_calledTwice_shouldReturnFirstInstance() throws Exception {
        String folderName = "Trash";
        ImapFolder imapFolder = imapStore.getFolder(folderName);

        ImapFolder result = imapStore.getFolder(folderName);

        assertEquals(imapFolder, result);
    }

    @Test
    public void checkSettings_shouldCreateImapConnectionAndCallOpen() throws Exception {
        ImapConnection imapConnection = mock(ImapConnection.class);