Merge "Quit recording LastUserHistoryWriteTime."

This commit is contained in:
Keisuke Kuroyanagi 2013-12-02 10:10:32 +00:00 committed by Android (Google) Code Review
commit 545e4191b3
10 changed files with 34 additions and 169 deletions

View file

@ -53,6 +53,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
/** Whether to print debug output to log */
private static boolean DEBUG = false;
private static final boolean DBG_STRESS_TEST = false;
private static final int TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS = 100;
@ -496,6 +497,16 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
+ mFilenameDictionaryUpdateController.mLastUpdateRequestTime + " update="
+ mFilenameDictionaryUpdateController.mLastUpdateTime);
}
if (DBG_STRESS_TEST) {
// Test if this class does not cause problems when it takes long time to load binary
// dictionary.
try {
Log.w(TAG, "Start stress in loading: " + mFilename);
Thread.sleep(15000);
Log.w(TAG, "End stress in loading");
} catch (InterruptedException e) {
}
}
final File file = new File(mContext.getFilesDir(), mFilename
+ getFileNameExtentionToOpenDict());

View file

@ -649,14 +649,13 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
mUserHistoryDictionary = PersonalizationHelper.getUserHistoryDictionary(
this, localeStr, prefs);
mUserHistoryDictionary = PersonalizationHelper.getUserHistoryDictionary(this, localeStr);
newSuggest.setUserHistoryDictionary(mUserHistoryDictionary);
mPersonalizationDictionary = PersonalizationHelper
.getPersonalizationDictionary(this, localeStr, prefs);
mPersonalizationDictionary =
PersonalizationHelper.getPersonalizationDictionary(this, localeStr);
newSuggest.setPersonalizationDictionary(mPersonalizationDictionary);
mPersonalizationPredictionDictionary = PersonalizationHelper
.getPersonalizationPredictionDictionary(this, localeStr, prefs);
mPersonalizationPredictionDictionary =
PersonalizationHelper.getPersonalizationPredictionDictionary(this, localeStr);
newSuggest.setPersonalizationPredictionDictionary(mPersonalizationPredictionDictionary);
final Suggest oldSuggest = mSuggest;

View file

@ -17,7 +17,6 @@
package com.android.inputmethod.latin.personalization;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting;
@ -25,11 +24,9 @@ import com.android.inputmethod.latin.BinaryDictionary.LanguageModelParam;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.makedict.DictDecoder;
import com.android.inputmethod.latin.makedict.FormatSpec;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils;
import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener;
@ -47,9 +44,7 @@ import java.util.Map;
public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableBinaryDictionary {
private static final String TAG = DecayingExpandableBinaryDictionaryBase.class.getSimpleName();
public static final boolean DBG_SAVE_RESTORE = false;
private static final boolean DBG_STRESS_TEST = false;
private static final boolean DBG_DUMP_ON_CLOSE = false;
private static final boolean PROFILE_SAVE_RESTORE = LatinImeLogger.sDBG;
/** Any pair being typed or picked */
public static final int FREQUENCY_FOR_TYPED = 2;
@ -64,8 +59,6 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
private final String mFileName;
private final SharedPreferences mPrefs;
private final ArrayList<PersonalizationDictionaryUpdateSession> mSessions =
CollectionUtils.newArrayList();
@ -73,12 +66,10 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
@UsedForTesting boolean mIsTest = false;
/* package */ DecayingExpandableBinaryDictionaryBase(final Context context,
final String locale, final SharedPreferences sp, final String dictionaryType,
final String fileName) {
final String locale, final String dictionaryType, final String fileName) {
super(context, fileName, dictionaryType, true);
mLocale = locale;
mFileName = fileName;
mPrefs = sp;
if (mLocale != null && mLocale.length() > 1) {
reloadDictionaryIfRequired();
}
@ -92,7 +83,6 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
// Flush pending writes.
// TODO: Remove after this class become to use a dynamic binary dictionary.
asyncFlashAllBinaryDictionary();
Settings.writeLastUserHistoryWriteTime(mPrefs, mLocale);
}
@Override
@ -171,67 +161,7 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
@Override
protected void loadDictionaryAsync() {
final int[] profTotalCount = { 0 };
final String locale = getLocale();
if (DBG_STRESS_TEST) {
try {
Log.w(TAG, "Start stress in loading: " + locale);
Thread.sleep(15000);
Log.w(TAG, "End stress in loading");
} catch (InterruptedException e) {
}
}
final long last = Settings.readLastUserHistoryWriteTime(mPrefs, locale);
final long now = System.currentTimeMillis();
final ExpandableBinaryDictionary dictionary = this;
final OnAddWordListener listener = new OnAddWordListener() {
@Override
public void setUnigram(final String word, final String shortcutTarget,
final int frequency, final int shortcutFreq) {
if (DBG_SAVE_RESTORE) {
Log.d(TAG, "load unigram: " + word + "," + frequency);
}
addWord(word, shortcutTarget, frequency, shortcutFreq, false /* isNotAWord */);
++profTotalCount[0];
}
@Override
public void setBigram(final String word0, final String word1, final int frequency) {
if (word0.length() < Constants.DICTIONARY_MAX_WORD_LENGTH
&& word1.length() < Constants.DICTIONARY_MAX_WORD_LENGTH) {
if (DBG_SAVE_RESTORE) {
Log.d(TAG, "load bigram: " + word0 + "," + word1 + "," + frequency);
}
++profTotalCount[0];
addBigram(word0, word1, frequency, last);
}
}
};
// Load the dictionary from binary file
final File dictFile = new File(mContext.getFilesDir(), mFileName);
final DictDecoder dictDecoder = FormatSpec.getDictDecoder(dictFile,
DictDecoder.USE_BYTEARRAY);
if (dictDecoder == null) {
// This is an expected condition: we don't have a user history dictionary for this
// language yet. It will be created sometime later.
return;
}
try {
dictDecoder.openDictBuffer();
UserHistoryDictIOUtils.readDictionaryBinary(dictDecoder, listener);
} catch (IOException e) {
Log.d(TAG, "IOException on opening a bytebuffer", e);
} catch (UnsupportedFormatException e) {
Log.d(TAG, "Unsupported format, can't read the dictionary", e);
} finally {
if (PROFILE_SAVE_RESTORE) {
final long diff = System.currentTimeMillis() - now;
Log.d(TAG, "PROF: Load UserHistoryDictionary: "
+ locale + ", " + diff + "ms. load " + profTotalCount[0] + "entries.");
}
}
// Never loaded to memory in Java side.
}
protected String getLocale() {

View file

@ -36,8 +36,7 @@ public class PersonalizationDictionary extends ExpandableBinaryDictionary {
/** Locale for which this user history dictionary is storing words */
private final String mLocale;
public PersonalizationDictionary(final Context context, final String locale,
final SharedPreferences prefs) {
public PersonalizationDictionary(final Context context, final String locale) {
// TODO: Make isUpdatable true.
super(context, getFilenameWithLocale(NAME, locale), Dictionary.TYPE_PERSONALIZATION,
false /* isUpdatable */);

View file

@ -41,7 +41,7 @@ public class PersonalizationHelper {
CollectionUtils.newConcurrentHashMap();
public static UserHistoryDictionary getUserHistoryDictionary(
final Context context, final String locale, final SharedPreferences sp) {
final Context context, final String locale) {
synchronized (sLangUserHistoryDictCache) {
if (sLangUserHistoryDictCache.containsKey(locale)) {
final SoftReference<UserHistoryDictionary> ref =
@ -55,7 +55,7 @@ public class PersonalizationHelper {
return dict;
}
}
final UserHistoryDictionary dict = new UserHistoryDictionary(context, locale, sp);
final UserHistoryDictionary dict = new UserHistoryDictionary(context, locale);
sLangUserHistoryDictCache.put(locale, new SoftReference<UserHistoryDictionary>(dict));
return dict;
}
@ -76,17 +76,14 @@ public class PersonalizationHelper {
public static void registerPersonalizationDictionaryUpdateSession(final Context context,
final PersonalizationDictionaryUpdateSession session, String locale) {
final PersonalizationPredictionDictionary predictionDictionary =
getPersonalizationPredictionDictionary(context, locale,
PreferenceManager.getDefaultSharedPreferences(context));
getPersonalizationPredictionDictionary(context, locale);
predictionDictionary.registerUpdateSession(session);
final PersonalizationDictionary dictionary =
getPersonalizationDictionary(context, locale,
PreferenceManager.getDefaultSharedPreferences(context));
final PersonalizationDictionary dictionary = getPersonalizationDictionary(context, locale);
dictionary.registerUpdateSession(session);
}
public static PersonalizationDictionary getPersonalizationDictionary(
final Context context, final String locale, final SharedPreferences sp) {
final Context context, final String locale) {
synchronized (sLangPersonalizationDictCache) {
if (sLangPersonalizationDictCache.containsKey(locale)) {
final SoftReference<PersonalizationDictionary> ref =
@ -99,8 +96,7 @@ public class PersonalizationHelper {
return dict;
}
}
final PersonalizationDictionary dict =
new PersonalizationDictionary(context, locale, sp);
final PersonalizationDictionary dict = new PersonalizationDictionary(context, locale);
sLangPersonalizationDictCache.put(
locale, new SoftReference<PersonalizationDictionary>(dict));
return dict;
@ -108,7 +104,7 @@ public class PersonalizationHelper {
}
public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary(
final Context context, final String locale, final SharedPreferences sp) {
final Context context, final String locale) {
synchronized (sLangPersonalizationPredictionDictCache) {
if (sLangPersonalizationPredictionDictCache.containsKey(locale)) {
final SoftReference<PersonalizationPredictionDictionary> ref =
@ -122,7 +118,7 @@ public class PersonalizationHelper {
}
}
final PersonalizationPredictionDictionary dict =
new PersonalizationPredictionDictionary(context, locale, sp);
new PersonalizationPredictionDictionary(context, locale);
sLangPersonalizationPredictionDictCache.put(
locale, new SoftReference<PersonalizationPredictionDictionary>(dict));
return dict;

View file

@ -20,14 +20,12 @@ import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import android.content.Context;
import android.content.SharedPreferences;
public class PersonalizationPredictionDictionary extends DecayingExpandableBinaryDictionaryBase {
private static final String NAME = PersonalizationPredictionDictionary.class.getSimpleName();
/* package */ PersonalizationPredictionDictionary(final Context context, final String locale,
final SharedPreferences sp) {
super(context, locale, sp, Dictionary.TYPE_PERSONALIZATION_PREDICTION_IN_JAVA,
/* package */ PersonalizationPredictionDictionary(final Context context, final String locale) {
super(context, locale, Dictionary.TYPE_PERSONALIZATION_PREDICTION_IN_JAVA,
getDictionaryFileName(locale));
}

View file

@ -20,7 +20,6 @@ import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import android.content.Context;
import android.content.SharedPreferences;
/**
* Locally gathers stats about the words user types and various other signals like auto-correction
@ -29,9 +28,8 @@ import android.content.SharedPreferences;
public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBase {
/* package for tests */ static final String NAME =
UserHistoryDictionary.class.getSimpleName();
/* package */ UserHistoryDictionary(final Context context, final String locale,
final SharedPreferences sp) {
super(context, locale, sp, Dictionary.TYPE_USER_HISTORY, getDictionaryFileName(locale));
/* package */ UserHistoryDictionary(final Context context, final String locale) {
super(context, locale, Dictionary.TYPE_USER_HISTORY, getDictionaryFileName(locale));
}
private static String getDictionaryFileName(final String locale) {

View file

@ -53,8 +53,6 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_AUTO_CORRECTION_THRESHOLD = "auto_correction_threshold";
public static final String PREF_SHOW_SUGGESTIONS_SETTING = "show_suggestions_setting";
public static final String PREF_MISC_SETTINGS = "misc_settings";
public static final String PREF_LAST_USER_DICTIONARY_WRITE_TIME =
"last_user_dictionary_write_time";
public static final String PREF_ADVANCED_SETTINGS = "pref_advanced_settings";
public static final String PREF_KEY_USE_CONTACTS_DICT = "pref_key_use_contacts_dict";
public static final String PREF_KEY_USE_DOUBLE_SPACE_PERIOD =
@ -333,25 +331,6 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
return prefs.getBoolean(DebugSettings.PREF_USABILITY_STUDY_MODE, true);
}
public static long readLastUserHistoryWriteTime(final SharedPreferences prefs,
final String locale) {
final String str = prefs.getString(PREF_LAST_USER_DICTIONARY_WRITE_TIME, "");
final HashMap<String, Long> map = LocaleUtils.localeAndTimeStrToHashMap(str);
if (map.containsKey(locale)) {
return map.get(locale);
}
return 0;
}
public static void writeLastUserHistoryWriteTime(final SharedPreferences prefs,
final String locale) {
final String oldStr = prefs.getString(PREF_LAST_USER_DICTIONARY_WRITE_TIME, "");
final HashMap<String, Long> map = LocaleUtils.localeAndTimeStrToHashMap(oldStr);
map.put(locale, System.currentTimeMillis());
final String newStr = LocaleUtils.localeAndTimeHashMapToStr(map);
prefs.edit().putString(PREF_LAST_USER_DICTIONARY_WRITE_TIME, newStr).apply();
}
public static boolean readUseFullscreenMode(final Resources res) {
return res.getBoolean(R.bool.config_use_fullscreen_mode);
}

View file

@ -30,9 +30,6 @@ import java.util.Locale;
* dictionary pack.
*/
public final class LocaleUtils {
private static final HashMap<String, Long> EMPTY_LT_HASH_MAP = CollectionUtils.newHashMap();
private static final String LOCALE_AND_TIME_STR_SEPARATER = ",";
private LocaleUtils() {
// Intentional empty constructor for utility class.
}
@ -188,38 +185,4 @@ public final class LocaleUtils {
return retval;
}
}
public static HashMap<String, Long> localeAndTimeStrToHashMap(String str) {
if (TextUtils.isEmpty(str)) {
return EMPTY_LT_HASH_MAP;
}
final String[] ss = str.split(LOCALE_AND_TIME_STR_SEPARATER);
final int N = ss.length;
if (N < 2 || N % 2 != 0) {
return EMPTY_LT_HASH_MAP;
}
final HashMap<String, Long> retval = CollectionUtils.newHashMap();
for (int i = 0; i < N / 2; ++i) {
final String localeStr = ss[i * 2];
final long time = Long.valueOf(ss[i * 2 + 1]);
retval.put(localeStr, time);
}
return retval;
}
public static String localeAndTimeHashMapToStr(HashMap<String, Long> map) {
if (map == null || map.isEmpty()) {
return "";
}
final StringBuilder builder = new StringBuilder();
for (String localeStr : map.keySet()) {
if (builder.length() > 0) {
builder.append(LOCALE_AND_TIME_STR_SEPARATER);
}
final Long time = map.get(localeStr);
builder.append(localeStr).append(LOCALE_AND_TIME_STR_SEPARATER);
builder.append(String.valueOf(time));
}
return builder.toString();
}
}

View file

@ -16,8 +16,6 @@
package com.android.inputmethod.latin.personalization;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
@ -38,7 +36,6 @@ import java.util.concurrent.TimeUnit;
@LargeTest
public class UserHistoryDictionaryTests extends AndroidTestCase {
private static final String TAG = UserHistoryDictionaryTests.class.getSimpleName();
private SharedPreferences mPrefs;
private static final String[] CHARACTERS = {
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
@ -48,11 +45,6 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
private static final int MIN_USER_HISTORY_DICTIONARY_FILE_SIZE = 1000;
private static final int WAIT_TERMINATING_IN_MILLISECONDS = 100;
@Override
public void setUp() {
mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
}
/**
* Generates a random word.
*/
@ -92,7 +84,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
final List<String> words = generateWords(numberOfWords, random);
final UserHistoryDictionary dict =
PersonalizationHelper.getUserHistoryDictionary(getContext(),
testFilenameSuffix /* locale */, mPrefs);
testFilenameSuffix /* locale */);
// Add random words to the user history dictionary.
addToDict(dict, words);
if (checkContents) {
@ -116,7 +108,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
private void clearHistory(final String testFilenameSuffix) {
final UserHistoryDictionary dict =
PersonalizationHelper.getUserHistoryDictionary(getContext(),
testFilenameSuffix /* locale */, mPrefs);
testFilenameSuffix /* locale */);
dict.clearAndFlushDictionary();
dict.close();
}
@ -129,7 +121,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
try {
final UserHistoryDictionary dict =
PersonalizationHelper.getUserHistoryDictionary(getContext(),
testFilenameSuffix, mPrefs);
testFilenameSuffix);
dict.shutdownExecutorForTests();
while (!dict.isTerminatedForTests()) {
Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS);