am 9b5965d4: am 1eb1af75: Merge "Fix looping logic bugs."

* commit '9b5965d452d55fd271dc14f2328b44da08c3cb67':
  Fix looping logic bugs.
main
Kurt Partridge 2013-05-15 12:46:48 -07:00 committed by Android Git Automerger
commit 8facddba7d
2 changed files with 31 additions and 25 deletions

View File

@ -65,6 +65,7 @@ public class FixedLogBuffer extends LogBuffer {
final int numWordsIncoming = newLogUnit.getNumWords(); final int numWordsIncoming = newLogUnit.getNumWords();
if (mNumActualWords >= mWordCapacity) { if (mNumActualWords >= mWordCapacity) {
// Give subclass a chance to handle the buffer full condition by shifting out logUnits. // Give subclass a chance to handle the buffer full condition by shifting out logUnits.
// TODO: Tell onBufferFull() how much space it needs to make to avoid forced eviction.
onBufferFull(); onBufferFull();
// If still full, evict. // If still full, evict.
if (mNumActualWords >= mWordCapacity) { if (mNumActualWords >= mWordCapacity) {
@ -119,21 +120,19 @@ public class FixedLogBuffer extends LogBuffer {
/** /**
* Remove LogUnits from the front of the LogBuffer until {@code numWords} have been removed. * Remove LogUnits from the front of the LogBuffer until {@code numWords} have been removed.
* *
* If there are less than {@code numWords} word-containing {@link LogUnit}s, shifts out * If there are less than {@code numWords} in the buffer, shifts out all {@code LogUnit}s.
* all {@code LogUnit}s in the buffer.
* *
* @param numWords the minimum number of word-containing {@link LogUnit}s to shift out * @param numWords the minimum number of words in {@link LogUnit}s to shift out
* @return the number of actual {@code LogUnit}s shifted out * @return the number of actual words LogUnit}s shifted out
*/ */
protected int shiftOutWords(final int numWords) { protected int shiftOutWords(final int numWords) {
int numWordContainingLogUnitsShiftedOut = 0; int numWordsShiftedOut = 0;
for (LogUnit logUnit = shiftOut(); logUnit != null do {
&& numWordContainingLogUnitsShiftedOut < numWords; logUnit = shiftOut()) { final LogUnit logUnit = shiftOut();
if (logUnit.hasOneOrMoreWords()) { if (logUnit == null) break;
numWordContainingLogUnitsShiftedOut += logUnit.getNumWords(); numWordsShiftedOut += logUnit.getNumWords();
} } while (numWordsShiftedOut < numWords);
} return numWordsShiftedOut;
return numWordContainingLogUnitsShiftedOut;
} }
public void shiftOutAll() { public void shiftOutAll() {

View File

@ -190,22 +190,30 @@ public abstract class MainLogBuffer extends FixedLogBuffer {
} }
protected final void publishLogUnitsAtFrontOfBuffer() { protected final void publishLogUnitsAtFrontOfBuffer() {
// TODO: Refactor this method to require fewer passes through the LogUnits. Should really
// require only one pass.
ArrayList<LogUnit> logUnits = peekAtFirstNWords(N_GRAM_SIZE); ArrayList<LogUnit> logUnits = peekAtFirstNWords(N_GRAM_SIZE);
if (isSafeNGram(logUnits, N_GRAM_SIZE)) { if (isSafeNGram(logUnits, N_GRAM_SIZE)) {
// Good n-gram at the front of the buffer. Publish it, disclosing details. // Good n-gram at the front of the buffer. Publish it, disclosing details.
publish(logUnits, true /* canIncludePrivateData */); publish(logUnits, true /* canIncludePrivateData */);
shiftOutWords(N_GRAM_SIZE); shiftOutWords(N_GRAM_SIZE);
mNumWordsUntilSafeToSample = mNumWordsBetweenNGrams; mNumWordsUntilSafeToSample = mNumWordsBetweenNGrams;
} else { return;
// No good n-gram at front, and buffer is full. Shift out up through the first logUnit
// with associated words (or if there is none, all the existing logUnits).
logUnits.clear();
for (LogUnit logUnit = shiftOut(); logUnit != null && !logUnit.hasOneOrMoreWords();
logUnit = shiftOut()) {
logUnits.add(logUnit);
}
publish(logUnits, false /* canIncludePrivateData */);
} }
// No good n-gram at front, and buffer is full. Shift out up through the first logUnit
// with associated words (or if there is none, all the existing logUnits).
logUnits.clear();
LogUnit logUnit = shiftOut();
while (logUnit != null) {
logUnits.add(logUnit);
final int numWords = logUnit.getNumWords();
if (numWords > 0) {
mNumWordsUntilSafeToSample = Math.max(0, mNumWordsUntilSafeToSample - numWords);
break;
}
logUnit = shiftOut();
}
publish(logUnits, false /* canIncludePrivateData */);
} }
/** /**
@ -222,12 +230,11 @@ public abstract class MainLogBuffer extends FixedLogBuffer {
@Override @Override
protected int shiftOutWords(final int numWords) { protected int shiftOutWords(final int numWords) {
final int numWordContainingLogUnitsShiftedOut = super.shiftOutWords(numWords); final int numWordsShiftedOut = super.shiftOutWords(numWords);
mNumWordsUntilSafeToSample = Math.max(0, mNumWordsUntilSafeToSample mNumWordsUntilSafeToSample = Math.max(0, mNumWordsUntilSafeToSample - numWordsShiftedOut);
- numWordContainingLogUnitsShiftedOut);
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "wordsUntilSafeToSample now at " + mNumWordsUntilSafeToSample); Log.d(TAG, "wordsUntilSafeToSample now at " + mNumWordsUntilSafeToSample);
} }
return numWordContainingLogUnitsShiftedOut; return numWordsShiftedOut;
} }
} }