Merge "Always close binary dicts for personalized dicts."

main
Keisuke Kuroyanagi 2014-04-03 09:19:49 +00:00 committed by Android (Google) Code Review
commit b600ffdb25
4 changed files with 32 additions and 13 deletions

View File

@ -89,6 +89,8 @@ public final class BinaryDictionary extends Dictionary {
private final long mDictSize; private final long mDictSize;
private final String mDictFilePath; private final String mDictFilePath;
private final boolean mIsUpdatable; private final boolean mIsUpdatable;
private boolean mHasUpdated;
private final int[] mInputCodePoints = new int[MAX_WORD_LENGTH]; private final int[] mInputCodePoints = new int[MAX_WORD_LENGTH];
private final int[] mOutputSuggestionCount = new int[1]; private final int[] mOutputSuggestionCount = new int[1];
private final int[] mOutputCodePoints = new int[MAX_WORD_LENGTH * MAX_RESULTS]; private final int[] mOutputCodePoints = new int[MAX_WORD_LENGTH * MAX_RESULTS];
@ -138,6 +140,7 @@ public final class BinaryDictionary extends Dictionary {
mDictSize = length; mDictSize = length;
mDictFilePath = filename; mDictFilePath = filename;
mIsUpdatable = isUpdatable; mIsUpdatable = isUpdatable;
mHasUpdated = false;
mNativeSuggestOptions.setUseFullEditDistance(useFullEditDistance); mNativeSuggestOptions.setUseFullEditDistance(useFullEditDistance);
loadDictionary(filename, offset, length, isUpdatable); loadDictionary(filename, offset, length, isUpdatable);
} }
@ -185,6 +188,7 @@ public final class BinaryDictionary extends Dictionary {
// TODO: Move native dict into session // TODO: Move native dict into session
private final void loadDictionary(final String path, final long startOffset, private final void loadDictionary(final String path, final long startOffset,
final long length, final boolean isUpdatable) { final long length, final boolean isUpdatable) {
mHasUpdated = false;
mNativeDict = openNative(path, startOffset, length, isUpdatable); mNativeDict = openNative(path, startOffset, length, isUpdatable);
} }
@ -401,6 +405,7 @@ public final class BinaryDictionary extends Dictionary {
StringUtils.toCodePointArray(shortcutTarget) : null; StringUtils.toCodePointArray(shortcutTarget) : null;
addUnigramWordNative(mNativeDict, codePoints, probability, shortcutTargetCodePoints, addUnigramWordNative(mNativeDict, codePoints, probability, shortcutTargetCodePoints,
shortcutProbability, isNotAWord, isBlacklisted, timestamp); shortcutProbability, isNotAWord, isBlacklisted, timestamp);
mHasUpdated = true;
} }
// Add a bigram entry to binary dictionary with timestamp in native code. // Add a bigram entry to binary dictionary with timestamp in native code.
@ -412,6 +417,7 @@ public final class BinaryDictionary extends Dictionary {
final int[] codePoints0 = StringUtils.toCodePointArray(word0); final int[] codePoints0 = StringUtils.toCodePointArray(word0);
final int[] codePoints1 = StringUtils.toCodePointArray(word1); final int[] codePoints1 = StringUtils.toCodePointArray(word1);
addBigramWordsNative(mNativeDict, codePoints0, codePoints1, probability, timestamp); addBigramWordsNative(mNativeDict, codePoints0, codePoints1, probability, timestamp);
mHasUpdated = true;
} }
// Remove a bigram entry form binary dictionary in native code. // Remove a bigram entry form binary dictionary in native code.
@ -422,6 +428,7 @@ public final class BinaryDictionary extends Dictionary {
final int[] codePoints0 = StringUtils.toCodePointArray(word0); final int[] codePoints0 = StringUtils.toCodePointArray(word0);
final int[] codePoints1 = StringUtils.toCodePointArray(word1); final int[] codePoints1 = StringUtils.toCodePointArray(word1);
removeBigramWordsNative(mNativeDict, codePoints0, codePoints1); removeBigramWordsNative(mNativeDict, codePoints0, codePoints1);
mHasUpdated = true;
} }
public void addMultipleDictionaryEntries(final LanguageModelParam[] languageModelParams) { public void addMultipleDictionaryEntries(final LanguageModelParam[] languageModelParams) {
@ -433,6 +440,7 @@ public final class BinaryDictionary extends Dictionary {
} }
processedParamCount = addMultipleDictionaryEntriesNative(mNativeDict, processedParamCount = addMultipleDictionaryEntriesNative(mNativeDict,
languageModelParams, processedParamCount); languageModelParams, processedParamCount);
mHasUpdated = true;
if (processedParamCount <= 0) { if (processedParamCount <= 0) {
return; return;
} }
@ -451,8 +459,10 @@ public final class BinaryDictionary extends Dictionary {
public void flush() { public void flush() {
if (!isValidDictionary()) return; if (!isValidDictionary()) return;
flushNative(mNativeDict, mDictFilePath); if (mHasUpdated) {
reopen(); flushNative(mNativeDict, mDictFilePath);
reopen();
}
} }
public void flushWithGC() { public void flushWithGC() {

View File

@ -100,10 +100,6 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
}); });
} }
public void reopen(final Context context) {
registerObserver(context);
}
@Override @Override
public synchronized void close() { public synchronized void close() {
if (mObserver != null) { if (mObserver != null) {

View File

@ -262,6 +262,9 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() { ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
@Override @Override
public void run() { public void run() {
if (mBinaryDictionary == null) {
return;
}
runGCAfterAllPrioritizedTasksIfRequiredLocked(mindsBlockByGC); runGCAfterAllPrioritizedTasksIfRequiredLocked(mindsBlockByGC);
} }
}); });
@ -274,9 +277,6 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
} }
private void runGCAfterAllPrioritizedTasksIfRequiredLocked(final boolean mindsBlockByGC) { private void runGCAfterAllPrioritizedTasksIfRequiredLocked(final boolean mindsBlockByGC) {
if (mBinaryDictionary == null) {
return;
}
// needsToRunGC() have to be called with lock. // needsToRunGC() have to be called with lock.
if (mBinaryDictionary.needsToRunGC(mindsBlockByGC)) { if (mBinaryDictionary.needsToRunGC(mindsBlockByGC)) {
if (setProcessingLargeTaskIfNot()) { if (setProcessingLargeTaskIfNot()) {
@ -301,9 +301,13 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
protected void addWordDynamically(final String word, final int frequency, protected void addWordDynamically(final String word, final int frequency,
final String shortcutTarget, final int shortcutFreq, final boolean isNotAWord, final String shortcutTarget, final int shortcutFreq, final boolean isNotAWord,
final boolean isBlacklisted, final int timestamp) { final boolean isBlacklisted, final int timestamp) {
reloadDictionaryIfRequired();
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() { ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
@Override @Override
public void run() { public void run() {
if (mBinaryDictionary == null) {
return;
}
runGCAfterAllPrioritizedTasksIfRequiredLocked(true /* mindsBlockByGC */); runGCAfterAllPrioritizedTasksIfRequiredLocked(true /* mindsBlockByGC */);
addWordDynamicallyLocked(word, frequency, shortcutTarget, shortcutFreq, addWordDynamicallyLocked(word, frequency, shortcutTarget, shortcutFreq,
isNotAWord, isBlacklisted, timestamp); isNotAWord, isBlacklisted, timestamp);
@ -323,9 +327,13 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
*/ */
protected void addBigramDynamically(final String word0, final String word1, protected void addBigramDynamically(final String word0, final String word1,
final int frequency, final int timestamp) { final int frequency, final int timestamp) {
reloadDictionaryIfRequired();
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() { ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
@Override @Override
public void run() { public void run() {
if (mBinaryDictionary == null) {
return;
}
runGCAfterAllPrioritizedTasksIfRequiredLocked(true /* mindsBlockByGC */); runGCAfterAllPrioritizedTasksIfRequiredLocked(true /* mindsBlockByGC */);
addBigramDynamicallyLocked(word0, word1, frequency, timestamp); addBigramDynamicallyLocked(word0, word1, frequency, timestamp);
} }
@ -341,9 +349,13 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
* Dynamically remove a word bigram in the dictionary. * Dynamically remove a word bigram in the dictionary.
*/ */
protected void removeBigramDynamically(final String word0, final String word1) { protected void removeBigramDynamically(final String word0, final String word1) {
reloadDictionaryIfRequired();
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() { ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
@Override @Override
public void run() { public void run() {
if (mBinaryDictionary == null) {
return;
}
runGCAfterAllPrioritizedTasksIfRequiredLocked(true /* mindsBlockByGC */); runGCAfterAllPrioritizedTasksIfRequiredLocked(true /* mindsBlockByGC */);
mBinaryDictionary.removeBigramWords(word0, word1); mBinaryDictionary.removeBigramWords(word0, word1);
} }
@ -360,14 +372,15 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
protected void addMultipleDictionaryEntriesDynamically( protected void addMultipleDictionaryEntriesDynamically(
final ArrayList<LanguageModelParam> languageModelParams, final ArrayList<LanguageModelParam> languageModelParams,
final AddMultipleDictionaryEntriesCallback callback) { final AddMultipleDictionaryEntriesCallback callback) {
reloadDictionaryIfRequired();
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() { ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
@Override @Override
public void run() { public void run() {
if (mBinaryDictionary == null) {
return;
}
final boolean locked = setProcessingLargeTaskIfNot(); final boolean locked = setProcessingLargeTaskIfNot();
try { try {
if (mBinaryDictionary == null) {
return;
}
mBinaryDictionary.addMultipleDictionaryEntries( mBinaryDictionary.addMultipleDictionaryEntries(
languageModelParams.toArray( languageModelParams.toArray(
new LanguageModelParam[languageModelParams.size()])); new LanguageModelParam[languageModelParams.size()]));

View File

@ -66,7 +66,7 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
} }
// Flush pending writes. // Flush pending writes.
flush(); flush();
// TODO: Quit depending on finalize() and really close the dictionary file. super.close();
} }
public void flush() { public void flush() {