Use onFinishInput to flush dictionary state

This commit is contained in:
Trevor Terris 2021-05-05 17:21:25 -04:00 committed by Daniele Laudani
parent 206fc18ec3
commit ddbed48ce9
4 changed files with 11 additions and 22 deletions

View file

@ -160,6 +160,10 @@ public abstract class Dictionary {
// empty base implementation
}
public void onFinishInput() {
//empty base implementation
}
/**
* Subclasses may override to indicate that this Dictionary is not yet properly initialized.
*/

View file

@ -225,7 +225,10 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
@Override
public void onFinishInput(Context context) {
flushDictionaries();
for (final String dictType : ALL_DICTIONARY_TYPES) {
Dictionary dict = mDictionaryGroup.getDict(dictType);
if (dict != null) dict.onFinishInput();
}
}
@Override
@ -460,18 +463,6 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
}
}
public void flushDictionaries() {
final DictionaryGroup dictionaryGroupToClose;
synchronized (mLock) {
dictionaryGroupToClose = mDictionaryGroup;
mDictionaryGroup = new DictionaryGroup();
}
for (final String dictType : ALL_DICTIONARY_TYPES) {
ExpandableBinaryDictionary dict = dictionaryGroupToClose.getSubDict(dictType);
if (dict != null) dict.asyncFlushBinaryDictionary();
}
}
@UsedForTesting
public ExpandableBinaryDictionary getSubDictForTesting(final String dictName) {
return mDictionaryGroup.getSubDict(dictName);

View file

@ -632,7 +632,8 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
/**
* Flush binary dictionary to dictionary file.
*/
public void asyncFlushBinaryDictionary() {
@Override
public void onFinishInput() {
asyncExecuteTaskWithWriteLock(new Runnable() {
@Override
public void run() {
@ -641,7 +642,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
return;
}
if (binaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
binaryDictionary.flushWithGC();
binaryDictionary.flushWithGCIfHasUpdated();
} else {
binaryDictionary.flush();
}

View file

@ -86,13 +86,6 @@ public class UserHistoryDictionary extends ExpandableBinaryDictionary {
isValid, 1 /* count */, timestamp);
}
@Override
public void close() {
// Flush pending writes.
asyncFlushBinaryDictionary();
super.close();
}
@Override
protected Map<String, String> getHeaderAttributeMap() {
final Map<String, String> attributeMap = super.getHeaderAttributeMap();