Merge "Limit the number of waiting UpdateBinaryTask to at most 1."

main
Ken Wakasa 2013-08-16 11:58:57 +00:00 committed by Android (Google) Code Review
commit c2f2d0d211
3 changed files with 62 additions and 50 deletions

View File

@ -43,6 +43,7 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
/** /**
@ -75,6 +76,8 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona
private final ArrayList<PersonalizationDictionaryUpdateSession> mSessions = private final ArrayList<PersonalizationDictionaryUpdateSession> mSessions =
CollectionUtils.newArrayList(); CollectionUtils.newArrayList();
private final AtomicReference<AsyncTask<Void, Void, Void>> mWaitingTask;
// Should always be false except when we use this class for test // Should always be false except when we use this class for test
@UsedForTesting boolean mIsTest = false; @UsedForTesting boolean mIsTest = false;
@ -83,6 +86,7 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona
super(context, dictionaryType); super(context, dictionaryType);
mLocale = locale; mLocale = locale;
mPrefs = sp; mPrefs = sp;
mWaitingTask = new AtomicReference<AsyncTask<Void, Void, Void>>();
if (mLocale != null && mLocale.length() > 1) { if (mLocale != null && mLocale.length() > 1) {
loadDictionary(); loadDictionary();
} }
@ -174,7 +178,11 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona
*/ */
private void flushPendingWrites() { private void flushPendingWrites() {
// Create a background thread to write the pending entries // Create a background thread to write the pending entries
new UpdateBinaryTask(mBigramList, mLocale, this, mPrefs, getContext()).execute(); final AsyncTask<Void, Void, Void> old = mWaitingTask.getAndSet(new UpdateBinaryTask(
mBigramList, mLocale, this, mPrefs, getContext()).execute());
if (old != null) {
old.cancel(false);
}
} }
@Override @Override
@ -287,6 +295,7 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona
@Override @Override
protected Void doInBackground(final Void... v) { protected Void doInBackground(final Void... v) {
if (isCancelled()) return null;
if (mDynamicPredictionDictionary.mIsTest) { if (mDynamicPredictionDictionary.mIsTest) {
// If mIsTest == true, wait until the lock is released. // If mIsTest == true, wait until the lock is released.
mDynamicPredictionDictionary.mBigramListLock.lock(); mDynamicPredictionDictionary.mBigramListLock.lock();
@ -306,6 +315,9 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona
} }
private void doWriteTaskLocked() { private void doWriteTaskLocked() {
if (isCancelled()) return;
mDynamicPredictionDictionary.mWaitingTask.compareAndSet(this, null);
if (DBG_STRESS_TEST) { if (DBG_STRESS_TEST) {
try { try {
Log.w(TAG, "Start stress in closing: " + mLocale); Log.w(TAG, "Start stress in closing: " + mLocale);

View File

@ -26,7 +26,8 @@ import android.content.SharedPreferences;
* cancellation or manual picks. This allows the keyboard to adapt to the typist over time. * cancellation or manual picks. This allows the keyboard to adapt to the typist over time.
*/ */
public class UserHistoryPredictionDictionary extends DynamicPredictionDictionaryBase { public class UserHistoryPredictionDictionary extends DynamicPredictionDictionaryBase {
private static final String NAME = UserHistoryPredictionDictionary.class.getSimpleName(); /* package for tests */ static final String NAME =
UserHistoryPredictionDictionary.class.getSimpleName();
/* package */ UserHistoryPredictionDictionary(final Context context, final String locale, /* package */ UserHistoryPredictionDictionary(final Context context, final String locale,
final SharedPreferences sp) { final SharedPreferences sp) {
super(context, locale, sp, Dictionary.TYPE_USER_HISTORY); super(context, locale, sp, Dictionary.TYPE_USER_HISTORY);

View File

@ -29,6 +29,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit;
/** /**
* Unit tests for UserHistoryDictionary * Unit tests for UserHistoryDictionary
@ -43,6 +44,8 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
}; };
private static final int MIN_USER_HISTORY_DICTIONARY_FILE_SIZE = 1000;
@Override @Override
public void setUp() { public void setUp() {
mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
@ -78,43 +81,42 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
} }
} }
private void addAndWriteRandomWords(final String locale, final int numberOfWords,
final Random random) {
final List<String> words = generateWords(numberOfWords, random);
final UserHistoryPredictionDictionary dict =
PersonalizationDictionaryHelper.getUserHistoryPredictionDictionary(getContext(),
locale, mPrefs);
// Add random words to the user history dictionary.
addToDict(dict, words);
// write to file.
dict.close();
}
public void testRandomWords() { public void testRandomWords() {
File dictFile = null; File dictFile = null;
try {
Log.d(TAG, "This test can be used for profiling."); Log.d(TAG, "This test can be used for profiling.");
Log.d(TAG, "Usage: please set UserHistoryDictionary.PROFILE_SAVE_RESTORE to true."); Log.d(TAG, "Usage: please set UserHistoryDictionary.PROFILE_SAVE_RESTORE to true.");
final String locale = "testRandomWords" + System.currentTimeMillis();
final int numberOfWords = 1000; final int numberOfWords = 1000;
final Random random = new Random(123456); final Random random = new Random(123456);
List<String> words = generateWords(numberOfWords, random);
final String locale = "testRandomWords";
final String fileName = "UserHistoryDictionary." + locale + ".dict";
dictFile = new File(getContext().getFilesDir(), fileName);
final UserHistoryPredictionDictionary dict =
PersonalizationDictionaryHelper.getUserHistoryPredictionDictionary(
getContext(), locale, mPrefs);
dict.mIsTest = true;
addToDict(dict, words);
try { try {
Log.d(TAG, "waiting for adding the word ..."); addAndWriteRandomWords(locale, numberOfWords, random);
Thread.sleep(2000); } finally {
} catch (InterruptedException e) {
Log.d(TAG, "InterruptedException: " + e);
}
// write to file
dict.close();
try { try {
Log.d(TAG, "waiting for writing ..."); Log.d(TAG, "waiting for writing ...");
Thread.sleep(5000); Thread.sleep(TimeUnit.MILLISECONDS.convert(5L, TimeUnit.SECONDS));
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.d(TAG, "InterruptedException: " + e); Log.d(TAG, "InterruptedException: " + e);
} }
} finally {
final String fileName = UserHistoryPredictionDictionary.NAME + "." + locale + ".dict";
dictFile = new File(getContext().getFilesDir(), fileName);
if (dictFile != null) { if (dictFile != null) {
assertTrue(dictFile.exists());
assertTrue(dictFile.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE);
dictFile.delete(); dictFile.delete();
} }
} }
@ -122,49 +124,46 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
public void testStressTestForSwitchingLanguagesAndAddingWords() { public void testStressTestForSwitchingLanguagesAndAddingWords() {
final int numberOfLanguages = 2; final int numberOfLanguages = 2;
final int numberOfLanguageSwitching = 100; final int numberOfLanguageSwitching = 80;
final int numberOfWordsIntertedForEachLanguageSwitch = 100; final int numberOfWordsInsertedForEachLanguageSwitch = 100;
final File dictFiles[] = new File[numberOfLanguages]; final File dictFiles[] = new File[numberOfLanguages];
try { try {
final Random random = new Random(123456); final Random random = new Random(123456);
// Create locales for this test. // Create filename suffixes for this test.
String locales[] = new String[numberOfLanguages]; String testFilenameSuffixes[] = new String[numberOfLanguages];
for (int i = 0; i < numberOfLanguages; i++) { for (int i = 0; i < numberOfLanguages; i++) {
locales[i] = "testSwitchingLanguages" + i; testFilenameSuffixes[i] = "testSwitchingLanguages" + i;
final String fileName = "UserHistoryDictionary." + locales[i] + ".dict"; final String fileName = "UserHistoryDictionary." + testFilenameSuffixes[i]
+ ".dict";
dictFiles[i] = new File(getContext().getFilesDir(), fileName); dictFiles[i] = new File(getContext().getFilesDir(), fileName);
} }
final long now = System.currentTimeMillis(); final long start = System.currentTimeMillis();
for (int i = 0; i < numberOfLanguageSwitching; i++) { for (int i = 0; i < numberOfLanguageSwitching; i++) {
final int index = i % numberOfLanguages; final int index = i % numberOfLanguages;
// Switch languages to locales[index]. // Switch languages to testFilenameSuffixes[index].
final UserHistoryPredictionDictionary dict = addAndWriteRandomWords(testFilenameSuffixes[index],
PersonalizationDictionaryHelper.getUserHistoryPredictionDictionary( numberOfWordsInsertedForEachLanguageSwitch, random);
getContext(), locales[index], mPrefs);
final List<String> words = generateWords(
numberOfWordsIntertedForEachLanguageSwitch, random);
// Add random words to the user history dictionary.
addToDict(dict, words);
// write to file
dict.close();
} }
final long end = System.currentTimeMillis(); final long end = System.currentTimeMillis();
Log.d(TAG, "testStressTestForSwitchingLanguageAndAddingWords took " Log.d(TAG, "testStressTestForSwitchingLanguageAndAddingWords took "
+ (end - now) + " ms"); + (end - start) + " ms");
} finally {
Log.d(TAG, "waiting for writing ...");
try { try {
Log.d(TAG, "waiting for writing ..."); Log.d(TAG, "waiting for writing ...");
Thread.sleep(5000); Thread.sleep(TimeUnit.MILLISECONDS.convert(5L, TimeUnit.SECONDS));
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.d(TAG, "InterruptedException: " + e); Log.d(TAG, "InterruptedException: " + e);
} }
} finally {
for (final File file : dictFiles) { for (final File file : dictFiles) {
if (file != null) { if (file != null) {
assertTrue(file.exists());
assertTrue(file.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE);
file.delete(); file.delete();
} }
} }