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

Commit f614de62 authored by Keisuke Kuroyanagi's avatar Keisuke Kuroyanagi Committed by Android (Google) Code Review
Browse files

Merge "Cleanup ExpandableBinaryDictionary."

parents c207955d b451f270
Loading
Loading
Loading
Loading
+14 −27
Original line number Original line Diff line number Diff line
@@ -45,10 +45,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
/**
 * Abstract base class for an expandable dictionary that can be created and updated dynamically
 * Abstract base class for an expandable dictionary that can be created and updated dynamically
 * during runtime. When updated it automatically generates a new binary dictionary to handle future
 * during runtime. When updated it automatically generates a new binary dictionary to handle future
 * queries in native code. This binary dictionary is written to internal storage, and potentially
 * queries in native code. This binary dictionary is written to internal storage.
 * shared across multiple ExpandableBinaryDictionary instances. Updates to each dictionary filename
 * are controlled across multiple instances to ensure that only one instance can update the same
 * dictionary at the same time.
 */
 */
abstract public class ExpandableBinaryDictionary extends Dictionary {
abstract public class ExpandableBinaryDictionary extends Dictionary {


@@ -82,8 +79,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {


    /**
    /**
     * The name of this dictionary, used as a part of the filename for storing the binary
     * The name of this dictionary, used as a part of the filename for storing the binary
     * dictionary. Multiple dictionary instances with the same name is supported, with access
     * dictionary.
     * controlled by DictionaryUpdateController.
     */
     */
    private final String mDictName;
    private final String mDictName;


@@ -112,7 +108,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
    /**
    /**
     * Indicates that the source dictionary contents have changed and a rebuild of the binary file
     * Indicates that the source dictionary contents have changed and a rebuild of the binary file
     * is required. If it returns false, the next reload will only read the current binary
     * is required. If it returns false, the next reload will only read the current binary
     * dictionary from file. Note that the shared binary dictionary is locked when this is called.
     * dictionary from file.
     */
     */
    protected abstract boolean haveContentsChanged();
    protected abstract boolean haveContentsChanged();


@@ -484,17 +480,6 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
        mBinaryDictionary.flushWithGCIfHasUpdated();
        mBinaryDictionary.flushWithGCIfHasUpdated();
    }
    }


    private void flushDictionaryLocked() {
        if (mBinaryDictionary == null) {
            return;
        }
        if (mBinaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
            mBinaryDictionary.flushWithGC();
        } else {
            mBinaryDictionary.flush();
        }
    }

    /**
    /**
     * Marks that the dictionary needs to be reloaded.
     * Marks that the dictionary needs to be reloaded.
     *
     *
@@ -531,9 +516,9 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
                @Override
                @Override
                public void run() {
                public void run() {
                    try {
                    try {
                        if (!dictionaryFileExists() || haveContentsChanged()) {
                        if (!mDictFile.exists() || haveContentsChanged()) {
                            // If the shared dictionary file does not exist or contents have been
                            // If the dictionary file does not exist or contents have been updated,
                            // updated, generate a new one.
                            // generate a new one.
                            createNewDictionaryLocked();
                            createNewDictionaryLocked();
                        } else if (mBinaryDictionary == null) {
                        } else if (mBinaryDictionary == null) {
                            // Otherwise, load the existing dictionary.
                            // Otherwise, load the existing dictionary.
@@ -556,11 +541,6 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
        }
        }
    }
    }


    // TODO: cache the file's existence so that we avoid doing a disk access each time.
    private boolean dictionaryFileExists() {
        return mDictFile.exists();
    }

    /**
    /**
     * Flush binary dictionary to dictionary file.
     * Flush binary dictionary to dictionary file.
     */
     */
@@ -568,7 +548,14 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
        asyncExecuteTaskWithWriteLock(new Runnable() {
        asyncExecuteTaskWithWriteLock(new Runnable() {
            @Override
            @Override
            public void run() {
            public void run() {
                flushDictionaryLocked();
                if (mBinaryDictionary == null) {
                    return;
                }
                if (mBinaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
                    mBinaryDictionary.flushWithGC();
                } else {
                    mBinaryDictionary.flush();
                }
            }
            }
        });
        });
    }
    }