diff options
author | 2013-01-11 16:49:54 -0800 | |
---|---|---|
committer | 2013-01-21 10:50:32 -0800 | |
commit | 80685aa4b95173638c7982dbac723b282292a931 (patch) | |
tree | 1689a1ac389e61c367c3e96291a5b37404dada4a /java/src/com/android/inputmethod/research/FixedLogBuffer.java | |
parent | 345ef6762700cdb0fca25aa54b22ef83aaaac0ab (diff) | |
download | latinime-80685aa4b95173638c7982dbac723b282292a931.tar.gz latinime-80685aa4b95173638c7982dbac723b282292a931.tar.xz latinime-80685aa4b95173638c7982dbac723b282292a931.zip |
[Rlog78b] Make log privacy filtering decisions on n-grams
Previously, words were pushed out of a LogBuffer one at a time. The receiving code had to keep
state to know whether a n-gram was safe to log. This patch looks at the entire n-gram and makes a
single decision based on it alone.
mult-project commit with I3c40d7e02c77943d2668094ddb1d03efb942c74f
Change-Id: Id7d90bbd551b1a2f4e0e35f38852652f68f273f8
Diffstat (limited to 'java/src/com/android/inputmethod/research/FixedLogBuffer.java')
-rw-r--r-- | java/src/com/android/inputmethod/research/FixedLogBuffer.java | 81 |
1 files changed, 56 insertions, 25 deletions
diff --git a/java/src/com/android/inputmethod/research/FixedLogBuffer.java b/java/src/com/android/inputmethod/research/FixedLogBuffer.java index 777111947..52707303c 100644 --- a/java/src/com/android/inputmethod/research/FixedLogBuffer.java +++ b/java/src/com/android/inputmethod/research/FixedLogBuffer.java @@ -16,6 +16,7 @@ package com.android.inputmethod.research; +import java.util.ArrayList; import java.util.LinkedList; /** @@ -65,8 +66,13 @@ public class FixedLogBuffer extends LogBuffer { super.shiftIn(newLogUnit); return; } - if (mNumActualWords == mWordCapacity) { - shiftOutThroughFirstWord(); + if (mNumActualWords >= mWordCapacity) { + // Give subclass a chance to handle the buffer full condition by shifting out logUnits. + onBufferFull(); + // If still full, evict. + if (mNumActualWords >= mWordCapacity) { + shiftOutWords(1); + } } super.shiftIn(newLogUnit); mNumActualWords++; // Must be a word, or we wouldn't be here. @@ -81,18 +87,8 @@ public class FixedLogBuffer extends LogBuffer { return logUnit; } - public void shiftOutThroughFirstWord() { - final LinkedList<LogUnit> logUnits = getLogUnits(); - while (!logUnits.isEmpty()) { - final LogUnit logUnit = logUnits.removeFirst(); - onShiftOut(logUnit); - if (logUnit.hasWord()) { - // Successfully shifted out a word-containing LogUnit and made space for the new - // LogUnit. - mNumActualWords--; - break; - } - } + public int getNumWords() { + return mNumActualWords; } /** @@ -105,28 +101,63 @@ public class FixedLogBuffer extends LogBuffer { } /** - * Called when a LogUnit is removed from the LogBuffer as a result of a shiftIn. LogUnits are - * removed in the order entered. This method is not called when shiftOut is called directly. + * Called when the buffer has just shifted in one more word than its maximum, and its about to + * shift out LogUnits to bring it back down to the maximum. * * Base class does nothing; subclasses may override if they want to record non-privacy sensitive * events that fall off the end. */ - protected void onShiftOut(final LogUnit logUnit) { + protected void onBufferFull() { } - /** - * Called to deliberately remove the oldest LogUnit. Usually called when draining the - * LogBuffer. - */ @Override public LogUnit shiftOut() { - if (isEmpty()) { - return null; - } final LogUnit logUnit = super.shiftOut(); - if (logUnit.hasWord()) { + if (logUnit != null && logUnit.hasWord()) { mNumActualWords--; } return logUnit; } + + protected void shiftOutWords(final int numWords) { + final int targetNumWords = mNumActualWords - numWords; + final LinkedList<LogUnit> logUnits = getLogUnits(); + while (mNumActualWords > targetNumWords && !logUnits.isEmpty()) { + shiftOut(); + } + } + + public void shiftOutAll() { + final LinkedList<LogUnit> logUnits = getLogUnits(); + while (!logUnits.isEmpty()) { + shiftOut(); + } + mNumActualWords = 0; + } + + /** + * Returns a list of {@link LogUnit}s at the front of the buffer that have associated words. No + * more than {@code n} LogUnits will have words associated with them. If there are not enough + * LogUnits in the buffer to meet the word requirement, returns the all LogUnits. + * + * @param n The maximum number of {@link LogUnit}s with words to return. + * @return The list of the {@link LogUnit}s containing the first n words + */ + public ArrayList<LogUnit> peekAtFirstNWords(int n) { + final LinkedList<LogUnit> logUnits = getLogUnits(); + final int length = logUnits.size(); + // Allocate space for n*2 logUnits. There will be at least n, one for each word, and + // there may be additional for punctuation, between-word commands, etc. This should be + // enough that reallocation won't be necessary. + final ArrayList<LogUnit> list = new ArrayList<LogUnit>(n * 2); + for (int i = 0; i < length && n > 0; i++) { + final LogUnit logUnit = logUnits.get(i); + list.add(logUnit); + final String word = logUnit.getWord(); + if (word != null) { + n--; + } + } + return list; + } } |