Merge "Change DictionaryStats to report content version."

main
Dan Zivkovic 2015-03-24 19:20:26 +00:00 committed by Android (Google) Code Review
commit a8b14a4fdd
4 changed files with 53 additions and 41 deletions

View File

@ -17,7 +17,6 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import android.content.Context; import android.content.Context;
import android.util.Pair;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.Keyboard;
@ -28,6 +27,7 @@ import com.android.inputmethod.latin.utils.SuggestionResults;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -172,5 +172,5 @@ public interface DictionaryFacilitator {
void dumpDictionaryForDebug(final String dictName); void dumpDictionaryForDebug(final String dictName);
ArrayList<Pair<String, DictionaryStats>> getStatsOfEnabledSubDicts(); @Nonnull List<DictionaryStats> getDictionaryStats(final Context context);
} }

View File

@ -19,7 +19,6 @@ package com.android.inputmethod.latin;
import android.content.Context; import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.Keyboard;
@ -39,6 +38,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -644,12 +644,12 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
} }
@Override @Override
public ArrayList<Pair<String, DictionaryStats>> getStatsOfEnabledSubDicts() { @Nonnull public List<DictionaryStats> getDictionaryStats(final Context context) {
final ArrayList<Pair<String, DictionaryStats>> statsOfEnabledSubDicts = new ArrayList<>(); final ArrayList<DictionaryStats> statsOfEnabledSubDicts = new ArrayList<>();
for (final String dictType : DYNAMIC_DICTIONARY_TYPES) { for (final String dictType : DYNAMIC_DICTIONARY_TYPES) {
final ExpandableBinaryDictionary dictionary = mDictionaryGroup.getSubDict(dictType); final ExpandableBinaryDictionary dictionary = mDictionaryGroup.getSubDict(dictType);
if (dictionary == null) continue; if (dictionary == null) continue;
statsOfEnabledSubDicts.add(new Pair<>(dictType, dictionary.getDictionaryStats())); statsOfEnabledSubDicts.add(dictionary.getDictionaryStats());
} }
return statsOfEnabledSubDicts; return statsOfEnabledSubDicts;
} }

View File

@ -17,6 +17,7 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import java.io.File; import java.io.File;
import java.math.BigDecimal;
import java.util.Locale; import java.util.Locale;
public class DictionaryStats { public class DictionaryStats {
@ -26,18 +27,54 @@ public class DictionaryStats {
public final String mDictName; public final String mDictName;
public final String mDictFilePath; public final String mDictFilePath;
public final long mDictFileSize; public final long mDictFileSize;
public final int mContentVersion;
public final int mUnigramCount;
public final int mNgramCount;
// TODO: Add more members.
public DictionaryStats(final Locale locale, final String dictName, final File dictFile, public DictionaryStats(final Locale locale, final String dictName, final File dictFile,
final int unigramCount, final int ngramCount) { final int contentVersion) {
mLocale = locale; mLocale = locale;
mDictName = dictName; mDictName = dictName;
mDictFilePath = dictFile.getAbsolutePath(); mDictFilePath = (dictFile == null) ? null : dictFile.getName();
mDictFileSize = dictFile.length(); mDictFileSize = (dictFile == null || !dictFile.exists()) ? 0 : dictFile.length();
mUnigramCount = unigramCount; mContentVersion = contentVersion;
mNgramCount = ngramCount; }
public String getFileSizeString() {
if (mDictFileSize == 0) {
return "0";
}
BigDecimal bytes = new BigDecimal(mDictFileSize);
BigDecimal kb = bytes.divide(new BigDecimal(1024), 2, BigDecimal.ROUND_HALF_UP);
if (kb.longValue() == 0) {
return bytes.toString() + " bytes";
}
BigDecimal mb = kb.divide(new BigDecimal(1024), 2, BigDecimal.ROUND_HALF_UP);
if (mb.longValue() == 0) {
return kb.toString() + " kb";
}
return mb.toString() + " Mb";
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder(mDictName);
if (mDictName.equals(Dictionary.TYPE_MAIN)) {
builder.append(" (");
builder.append(mContentVersion);
builder.append(")");
}
builder.append(": ");
builder.append(mDictFilePath);
builder.append(" / ");
builder.append(getFileSizeString());
return builder.toString();
}
public static String toString(final Iterable<DictionaryStats> stats) {
final StringBuilder builder = new StringBuilder("LM Stats");
for (DictionaryStats stat : stats) {
builder.append("\n ");
builder.append(stat.toString());
}
return builder.toString();
} }
} }

View File

@ -646,16 +646,6 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
}); });
} }
static int parseEntryCount(final String entryCountStr) {
int entryCount;
try {
entryCount = Integer.parseInt(entryCountStr);
} catch (final NumberFormatException e) {
entryCount = DictionaryStats.NOT_AN_ENTRY_COUNT;
}
return entryCount;
}
public DictionaryStats getDictionaryStats() { public DictionaryStats getDictionaryStats() {
reloadDictionaryIfRequired(); reloadDictionaryIfRequired();
final String dictName = mDictName; final String dictName = mDictName;
@ -664,22 +654,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
asyncExecuteTaskWithLock(mLock.readLock(), new Runnable() { asyncExecuteTaskWithLock(mLock.readLock(), new Runnable() {
@Override @Override
public void run() { public void run() {
final BinaryDictionary binaryDictionary = getBinaryDictionary(); result.set(new DictionaryStats(mLocale, dictName, dictFile, 0));
if (binaryDictionary == null) {
result.set(new DictionaryStats(mLocale, dictName, dictFile,
DictionaryStats.NOT_AN_ENTRY_COUNT,
DictionaryStats.NOT_AN_ENTRY_COUNT));
return;
}
final int unigramCount = parseEntryCount(
binaryDictionary.getPropertyForGettingStats(
BinaryDictionary.MAX_UNIGRAM_COUNT_QUERY));
// TODO: Get dedicated entry counts for bigram, trigram, and so on.
final int ngramCount = parseEntryCount(binaryDictionary.getPropertyForGettingStats(
BinaryDictionary.MAX_BIGRAM_COUNT_QUERY));
// TODO: Get more information from dictionary.
result.set(new DictionaryStats(mLocale, dictName, dictFile, unigramCount,
ngramCount));
} }
}); });
return result.get(null /* defaultValue */, TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS); return result.get(null /* defaultValue */, TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS);