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

View File

@ -17,6 +17,7 @@
package com.android.inputmethod.latin;
import java.io.File;
import java.math.BigDecimal;
import java.util.Locale;
public class DictionaryStats {
@ -26,18 +27,54 @@ public class DictionaryStats {
public final String mDictName;
public final String mDictFilePath;
public final long mDictFileSize;
public final int mUnigramCount;
public final int mNgramCount;
// TODO: Add more members.
public final int mContentVersion;
public DictionaryStats(final Locale locale, final String dictName, final File dictFile,
final int unigramCount, final int ngramCount) {
final int contentVersion) {
mLocale = locale;
mDictName = dictName;
mDictFilePath = dictFile.getAbsolutePath();
mDictFileSize = dictFile.length();
mUnigramCount = unigramCount;
mNgramCount = ngramCount;
mDictFilePath = (dictFile == null) ? null : dictFile.getName();
mDictFileSize = (dictFile == null || !dictFile.exists()) ? 0 : dictFile.length();
mContentVersion = contentVersion;
}
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() {
reloadDictionaryIfRequired();
final String dictName = mDictName;
@ -664,22 +654,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
asyncExecuteTaskWithLock(mLock.readLock(), new Runnable() {
@Override
public void run() {
final BinaryDictionary binaryDictionary = getBinaryDictionary();
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));
result.set(new DictionaryStats(mLocale, dictName, dictFile, 0));
}
});
return result.get(null /* defaultValue */, TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS);