am 5a8d2fd1: Merge "Cache UserHistoryDictionary for each language" into jb-dev

* commit '5a8d2fd1d353b2039f0d340301f9dac779cb4e64':
  Cache UserHistoryDictionary for each language
main
Satoshi Kataoka 2012-06-06 10:46:17 -07:00 committed by Android Git Automerger
commit e08cfda1eb
3 changed files with 216 additions and 127 deletions

View File

@ -497,7 +497,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// Note that the calling sequence of onCreate() and onCurrentInputMethodSubtypeChanged() // Note that the calling sequence of onCreate() and onCurrentInputMethodSubtypeChanged()
// is not guaranteed. It may even be called at the same time on a different thread. // is not guaranteed. It may even be called at the same time on a different thread.
if (null == mPrefs) mPrefs = PreferenceManager.getDefaultSharedPreferences(this); if (null == mPrefs) mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mUserHistoryDictionary = new UserHistoryDictionary( mUserHistoryDictionary = UserHistoryDictionary.getInstance(
this, localeStr, Suggest.DIC_USER_HISTORY, mPrefs); this, localeStr, Suggest.DIC_USER_HISTORY, mPrefs);
mSuggest.setUserHistoryDictionary(mUserHistoryDictionary); mSuggest.setUserHistoryDictionary(mUserHistoryDictionary);
} }

View File

@ -29,7 +29,9 @@ import android.util.Log;
import com.android.inputmethod.latin.UserHistoryForgettingCurveUtils.ForgettingCurveParams; import com.android.inputmethod.latin.UserHistoryForgettingCurveUtils.ForgettingCurveParams;
import java.lang.ref.SoftReference;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* Locally gathers stats about the words user types and various other signals like auto-correction * Locally gathers stats about the words user types and various other signals like auto-correction
@ -38,6 +40,7 @@ import java.util.HashMap;
public class UserHistoryDictionary extends ExpandableDictionary { public class UserHistoryDictionary extends ExpandableDictionary {
private static final String TAG = "UserHistoryDictionary"; private static final String TAG = "UserHistoryDictionary";
public static final boolean DBG_SAVE_RESTORE = false; public static final boolean DBG_SAVE_RESTORE = false;
public static final boolean PROFILE_SAVE_RESTORE = LatinImeLogger.sDBG;
/** Any pair being typed or picked */ /** Any pair being typed or picked */
private static final int FREQUENCY_FOR_TYPED = 2; private static final int FREQUENCY_FOR_TYPED = 2;
@ -77,13 +80,14 @@ public class UserHistoryDictionary extends ExpandableDictionary {
/** Locale for which this user history dictionary is storing words */ /** Locale for which this user history dictionary is storing words */
private final String mLocale; private final String mLocale;
private UserHistoryDictionaryBigramList mBigramList = private final UserHistoryDictionaryBigramList mBigramList =
new UserHistoryDictionaryBigramList(); new UserHistoryDictionaryBigramList();
private final Object mPendingWritesLock = new Object();
private static volatile boolean sUpdatingDB = false; private static volatile boolean sUpdatingDB = false;
private final SharedPreferences mPrefs; private final SharedPreferences mPrefs;
private final static HashMap<String, String> sDictProjectionMap; private final static HashMap<String, String> sDictProjectionMap;
private final static ConcurrentHashMap<String, SoftReference<UserHistoryDictionary>>
sLangDictCache = new ConcurrentHashMap<String, SoftReference<UserHistoryDictionary>>();
static { static {
sDictProjectionMap = new HashMap<String, String>(); sDictProjectionMap = new HashMap<String, String>();
@ -107,7 +111,26 @@ public class UserHistoryDictionary extends ExpandableDictionary {
sDeleteHistoryBigrams = deleteHistoryBigram; sDeleteHistoryBigrams = deleteHistoryBigram;
} }
public UserHistoryDictionary(final Context context, final String locale, final int dicTypeId, public synchronized static UserHistoryDictionary getInstance(
final Context context, final String locale,
final int dictTypeId, final SharedPreferences sp) {
if (sLangDictCache.containsKey(locale)) {
final SoftReference<UserHistoryDictionary> ref = sLangDictCache.get(locale);
final UserHistoryDictionary dict = ref == null ? null : ref.get();
if (dict != null) {
if (PROFILE_SAVE_RESTORE) {
Log.w(TAG, "Use cached UserHistoryDictionary for " + locale);
}
return dict;
}
}
final UserHistoryDictionary dict =
new UserHistoryDictionary(context, locale, dictTypeId, sp);
sLangDictCache.put(locale, new SoftReference<UserHistoryDictionary>(dict));
return dict;
}
private UserHistoryDictionary(final Context context, final String locale, final int dicTypeId,
SharedPreferences sp) { SharedPreferences sp) {
super(context, dicTypeId); super(context, dicTypeId);
mLocale = locale; mLocale = locale;
@ -123,12 +146,13 @@ public class UserHistoryDictionary extends ExpandableDictionary {
@Override @Override
public void close() { public void close() {
flushPendingWrites(); flushPendingWrites();
SettingsValues.setLastUserHistoryWriteTime(mPrefs, mLocale);
// Don't close the database as locale changes will require it to be reopened anyway // Don't close the database as locale changes will require it to be reopened anyway
// Also, the database is written to somewhat frequently, so it needs to be kept alive // Also, the database is written to somewhat frequently, so it needs to be kept alive
// throughout the life of the process. // throughout the life of the process.
// mOpenHelper.close(); // mOpenHelper.close();
super.close(); // Ignore close because we cache UserHistoryDictionary for each language. See getInstance()
// above.
// super.close();
} }
/** /**
@ -160,7 +184,7 @@ public class UserHistoryDictionary extends ExpandableDictionary {
} else { } else {
freq = super.setBigramAndGetFrequency(word1, word2, new ForgettingCurveParams(isValid)); freq = super.setBigramAndGetFrequency(word1, word2, new ForgettingCurveParams(isValid));
} }
synchronized (mPendingWritesLock) { synchronized (mBigramList) {
mBigramList.addBigram(word1, word2); mBigramList.addBigram(word1, word2);
} }
@ -168,7 +192,7 @@ public class UserHistoryDictionary extends ExpandableDictionary {
} }
public boolean cancelAddingUserHistory(String word1, String word2) { public boolean cancelAddingUserHistory(String word1, String word2) {
synchronized (mPendingWritesLock) { synchronized (mBigramList) {
if (mBigramList.removeBigram(word1, word2)) { if (mBigramList.removeBigram(word1, word2)) {
return super.removeBigram(word1, word2); return super.removeBigram(word1, word2);
} }
@ -180,19 +204,17 @@ public class UserHistoryDictionary extends ExpandableDictionary {
* Schedules a background thread to write any pending words to the database. * Schedules a background thread to write any pending words to the database.
*/ */
private void flushPendingWrites() { private void flushPendingWrites() {
synchronized (mPendingWritesLock) { synchronized (mBigramList) {
// Nothing pending? Return // Nothing pending? Return
if (mBigramList.isEmpty()) return; if (mBigramList.isEmpty()) return;
// Create a background thread to write the pending entries // Create a background thread to write the pending entries
new UpdateDbTask(sOpenHelper, mBigramList, mLocale, this).execute(); new UpdateDbTask(sOpenHelper, mBigramList, mLocale, this, mPrefs).execute();
// Create a new map for writing new entries into while the old one is written to db
mBigramList = new UserHistoryDictionaryBigramList();
} }
} }
/** Used for testing purpose **/ /** Used for testing purpose **/
void waitUntilUpdateDBDone() { void waitUntilUpdateDBDone() {
synchronized (mPendingWritesLock) { synchronized (mBigramList) {
while (sUpdatingDB) { while (sUpdatingDB) {
try { try {
Thread.sleep(100); Thread.sleep(100);
@ -205,6 +227,7 @@ public class UserHistoryDictionary extends ExpandableDictionary {
@Override @Override
public void loadDictionaryAsync() { public void loadDictionaryAsync() {
synchronized(mBigramList) {
final long last = SettingsValues.getLastUserHistoryWriteTime(mPrefs, mLocale); final long last = SettingsValues.getLastUserHistoryWriteTime(mPrefs, mLocale);
final long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
// Load the words that correspond to the current input locale // Load the words that correspond to the current input locale
@ -232,14 +255,18 @@ public class UserHistoryDictionary extends ExpandableDictionary {
super.setBigramAndGetFrequency( super.setBigramAndGetFrequency(
word1, word2, new ForgettingCurveParams(fc, now, last)); word1, word2, new ForgettingCurveParams(fc, now, last));
} }
synchronized(mPendingWritesLock) {
mBigramList.addBigram(word1, word2, (byte)fc); mBigramList.addBigram(word1, word2, (byte)fc);
}
cursor.moveToNext(); cursor.moveToNext();
} }
} }
} finally { } finally {
cursor.close(); cursor.close();
if (PROFILE_SAVE_RESTORE) {
final long diff = System.currentTimeMillis() - now;
Log.w(TAG, "PROF: Load User HistoryDictionary: "
+ mLocale + ", " + diff + "ms.");
}
}
} }
} }
@ -317,14 +344,16 @@ public class UserHistoryDictionary extends ExpandableDictionary {
private final DatabaseHelper mDbHelper; private final DatabaseHelper mDbHelper;
private final String mLocale; private final String mLocale;
private final UserHistoryDictionary mUserHistoryDictionary; private final UserHistoryDictionary mUserHistoryDictionary;
private final SharedPreferences mPrefs;
public UpdateDbTask( public UpdateDbTask(
DatabaseHelper openHelper, UserHistoryDictionaryBigramList pendingWrites, DatabaseHelper openHelper, UserHistoryDictionaryBigramList pendingWrites,
String locale, UserHistoryDictionary dict) { String locale, UserHistoryDictionary dict, SharedPreferences prefs) {
mBigramList = pendingWrites; mBigramList = pendingWrites;
mLocale = locale; mLocale = locale;
mDbHelper = openHelper; mDbHelper = openHelper;
mUserHistoryDictionary = dict; mUserHistoryDictionary = dict;
mPrefs = prefs;
} }
/** Prune any old data if the database is getting too big. */ /** Prune any old data if the database is getting too big. */
@ -363,6 +392,11 @@ public class UserHistoryDictionary extends ExpandableDictionary {
@Override @Override
protected Void doInBackground(Void... v) { protected Void doInBackground(Void... v) {
synchronized(mBigramList) {
final long now = PROFILE_SAVE_RESTORE ? System.currentTimeMillis() : 0;
int profTotal = 0;
int profInsert = 0;
int profDelete = 0;
SQLiteDatabase db = null; SQLiteDatabase db = null;
try { try {
db = mDbHelper.getWritableDatabase(); db = mDbHelper.getWritableDatabase();
@ -382,11 +416,24 @@ public class UserHistoryDictionary extends ExpandableDictionary {
for (String word1 : mBigramList.keySet()) { for (String word1 : mBigramList.keySet()) {
final HashMap<String, Byte> word1Bigrams = mBigramList.getBigrams(word1); final HashMap<String, Byte> word1Bigrams = mBigramList.getBigrams(word1);
for (String word2 : word1Bigrams.keySet()) { for (String word2 : word1Bigrams.keySet()) {
// Get new frequency. Do not insert shortcuts/bigrams which freq is "-1". if (PROFILE_SAVE_RESTORE) {
++profTotal;
}
// Get new frequency. Do not insert unigrams/bigrams which freq is "-1".
final int freq; // -1, or 0~255 final int freq; // -1, or 0~255
if (word1 == null) { if (word1 == null) { // unigram
freq = FREQUENCY_FOR_TYPED; freq = FREQUENCY_FOR_TYPED;
} else { final byte prevFc = word1Bigrams.get(word2);
if (prevFc == FREQUENCY_FOR_TYPED) {
// No need to update since we found no changes for this entry.
// Just skip to the next entry.
if (DBG_SAVE_RESTORE) {
Log.d(TAG, "Skip update user history: " + word1 + "," + word2
+ "," + prevFc);
}
continue;
}
} else { // bigram
final NextWord nw = mUserHistoryDictionary.getBigramWord(word1, word2); final NextWord nw = mUserHistoryDictionary.getBigramWord(word1, word2);
if (nw != null) { if (nw != null) {
final ForgettingCurveParams fcp = nw.getFcParams(); final ForgettingCurveParams fcp = nw.getFcParams();
@ -397,8 +444,8 @@ public class UserHistoryDictionary extends ExpandableDictionary {
// No need to update since we found no changes for this entry. // No need to update since we found no changes for this entry.
// Just skip to the next entry. // Just skip to the next entry.
if (DBG_SAVE_RESTORE) { if (DBG_SAVE_RESTORE) {
Log.d(TAG, "Skip update user history: " + word1 + "," + word2 Log.d(TAG, "Skip update user history: " + word1 + ","
+ "," + prevFc); + word2 + "," + prevFc);
} }
continue; continue;
} else if (UserHistoryForgettingCurveUtils. } else if (UserHistoryForgettingCurveUtils.
@ -431,6 +478,9 @@ public class UserHistoryDictionary extends ExpandableDictionary {
final int pairId; final int pairId;
if (c.moveToFirst()) { if (c.moveToFirst()) {
if (PROFILE_SAVE_RESTORE) {
++profDelete;
}
// Delete existing pair // Delete existing pair
pairId = c.getInt(c.getColumnIndex(MAIN_COLUMN_ID)); pairId = c.getInt(c.getColumnIndex(MAIN_COLUMN_ID));
db.delete(FREQ_TABLE_NAME, FREQ_COLUMN_PAIR_ID + "=?", db.delete(FREQ_TABLE_NAME, FREQ_COLUMN_PAIR_ID + "=?",
@ -442,6 +492,9 @@ public class UserHistoryDictionary extends ExpandableDictionary {
pairId = pairIdLong.intValue(); pairId = pairIdLong.intValue();
} }
if (freq > 0) { if (freq > 0) {
if (PROFILE_SAVE_RESTORE) {
++profInsert;
}
if (DBG_SAVE_RESTORE) { if (DBG_SAVE_RESTORE) {
Log.d(TAG, "--- Save user history: " + word1 + ", " + word2 Log.d(TAG, "--- Save user history: " + word1 + ", " + word2
+ mLocale + "," + this); + mLocale + "," + this);
@ -449,6 +502,9 @@ public class UserHistoryDictionary extends ExpandableDictionary {
// Insert new frequency // Insert new frequency
db.insert(FREQ_TABLE_NAME, null, db.insert(FREQ_TABLE_NAME, null,
getFrequencyContentValues(pairId, freq)); getFrequencyContentValues(pairId, freq));
// Update an existing bigram entry in mBigramList too in order to
// synchronize the SQL DB and mBigramList.
mBigramList.updateBigram(word1, word2, (byte)freq);
} }
} finally { } finally {
if (c != null) { if (c != null) {
@ -459,9 +515,17 @@ public class UserHistoryDictionary extends ExpandableDictionary {
} }
checkPruneData(db); checkPruneData(db);
// Save the timestamp after we finish writing the SQL DB.
SettingsValues.setLastUserHistoryWriteTime(mPrefs, mLocale);
sUpdatingDB = false; sUpdatingDB = false;
if (PROFILE_SAVE_RESTORE) {
final long diff = System.currentTimeMillis() - now;
Log.w(TAG, "PROF: Write User HistoryDictionary: " + mLocale + ", "+ diff
+ "ms. Total: " + profTotal + ". Insert: " + profInsert + ". Delete: "
+ profDelete);
}
return null; return null;
} // synchronized
} }
private static ContentValues getContentValues(String word1, String word2, String locale) { private static ContentValues getContentValues(String word1, String word2, String locale) {

View File

@ -39,13 +39,19 @@ public class UserHistoryDictionaryBigramList {
mBigramMap.clear(); mBigramMap.clear();
} }
/**
* Called when the user typed a word.
*/
public void addBigram(String word1, String word2) { public void addBigram(String word1, String word2) {
addBigram(word1, word2, FORGETTING_CURVE_INITIAL_VALUE); addBigram(word1, word2, FORGETTING_CURVE_INITIAL_VALUE);
} }
/**
* Called when loaded from the SQL DB.
*/
public void addBigram(String word1, String word2, byte fcValue) { public void addBigram(String word1, String word2, byte fcValue) {
if (UserHistoryDictionary.DBG_SAVE_RESTORE) { if (UserHistoryDictionary.DBG_SAVE_RESTORE) {
Log.d(TAG, "--- add bigram: " + word1 + ", " + word2); Log.d(TAG, "--- add bigram: " + word1 + ", " + word2 + ", " + fcValue);
} }
final HashMap<String, Byte> map; final HashMap<String, Byte> map;
if (mBigramMap.containsKey(word1)) { if (mBigramMap.containsKey(word1)) {
@ -60,6 +66,25 @@ public class UserHistoryDictionaryBigramList {
} }
} }
/**
* Called when inserted to the SQL DB.
*/
public void updateBigram(String word1, String word2, byte fcValue) {
if (UserHistoryDictionary.DBG_SAVE_RESTORE) {
Log.d(TAG, "--- update bigram: " + word1 + ", " + word2 + ", " + fcValue);
}
final HashMap<String, Byte> map;
if (mBigramMap.containsKey(word1)) {
map = mBigramMap.get(word1);
} else {
return;
}
if (!map.containsKey(word2)) {
return;
}
map.put(word2, fcValue);
}
public int size() { public int size() {
return mSize; return mSize;
} }