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

Commit 3b135ef2 authored by Tomasz Mikolajewski's avatar Tomasz Mikolajewski
Browse files

Move executor details to ReadableArchive.

WriteableArchive is going to use a different executor. Files have to
be written sequentially, so in only one thread in parallel. On the other
hand, ReadableArchive supports parallel reading.

Test: Refactoring only. Unit tests pass.
Bug: 20822019
Change-Id: Ie030c2e9478be576ae87f8f30b8d1e9388507cb2
parent d683f975
Loading
Loading
Loading
Loading
+0 −20
Original line number Diff line number Diff line
@@ -42,8 +42,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;

/**
@@ -67,7 +65,6 @@ public abstract class Archive implements Closeable {
    final Uri mArchiveUri;
    final int mArchiveMode;
    final Uri mNotificationUri;
    final ThreadPoolExecutor mExecutor;
    final Map<String, ZipEntry> mEntries;
    final Map<String, List<ZipEntry>> mTree;

@@ -81,12 +78,6 @@ public abstract class Archive implements Closeable {
        mArchiveMode = archiveMode;
        mNotificationUri = notificationUri;

        // At most 8 active threads. All threads idling for more than a minute will
        // be closed.
        mExecutor = new ThreadPoolExecutor(8, 8, 60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());
        mExecutor.allowCoreThreadTimeOut(true);

        mTree = new HashMap<>();
        mEntries = new HashMap<>();
    }
@@ -244,17 +235,6 @@ public abstract class Archive implements Closeable {
        return new ArchiveId(mArchiveUri, mArchiveMode, path);
    }

    /**
     * Closes an archive.
     *
     * <p>This method does not block until shutdown. Once called, other methods should not be
     * called. Any active pipes will be terminated.
     */
    @Override
    public void close() {
        mExecutor.shutdownNow();
    }

    void addCursorRow(MatrixCursor cursor, ZipEntry entry) {
        final MatrixCursor.RowBuilder row = cursor.newRow();
        final ArchiveId parsedId = createArchiveId(getEntryPath(entry));
+4 −1
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import com.android.internal.util.Preconditions;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
@@ -71,6 +72,8 @@ public class ArchivesProvider extends DocumentsProvider implements Closeable {
                    oldValue.getWriteLock().lock();
                    try {
                        oldValue.get().close();
                    } catch (IOException e) {
                        Log.e(TAG, "Closing archive failed.", e);
                    }finally {
                        oldValue.getWriteLock().unlock();
                    }
+17 −1
Original line number Diff line number Diff line
@@ -44,6 +44,9 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;

/**
@@ -55,6 +58,7 @@ import java.util.zip.ZipEntry;
public class ReadableArchive extends Archive {
    private static final String TAG = "Archive";

    final ThreadPoolExecutor mExecutor;
    private final StrictJarFile mZipFile;

    private ReadableArchive(
@@ -70,6 +74,12 @@ public class ReadableArchive extends Archive {
            throw new IllegalStateException("Unsupported access mode.");
        }

        // At most 8 active threads. All threads idling for more than a minute will
        // be closed.
        mExecutor = new ThreadPoolExecutor(8, 8, 60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());
        mExecutor.allowCoreThreadTimeOut(true);

        mZipFile = file != null ?
                new StrictJarFile(file.getPath(), false /* verify */,
                        false /* signatures */) :
@@ -335,9 +345,15 @@ public class ReadableArchive extends Archive {
                openDocument(documentId, "r", signal), 0, entry.getSize(), null);
    }

    /**
     * Closes an archive.
     *
     * <p>This method does not block until shutdown. Once called, other methods should not be
     * called. Any active pipes will be terminated.
     */
    @Override
    public void close() {
        super.close();
        mExecutor.shutdownNow();
        try {
            mZipFile.close();
        } catch (IOException e) {