Copy collected acc(maxFreqs) into empty acc, rather than merge them. (#12846)

This commit is contained in:
zhouhui 2024-01-11 21:44:11 +08:00 committed by GitHub
parent 701619d35a
commit 4b1180372e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 1 deletions

View File

@ -93,6 +93,18 @@ public final class CompetitiveImpactAccumulator {
assert assertConsistent();
}
/** Replace the content of this {@code acc} with the provided {@code acc}. */
public void copy(CompetitiveImpactAccumulator acc) {
int[] maxFreqs = this.maxFreqs;
int[] otherMaxFreqs = acc.maxFreqs;
System.arraycopy(otherMaxFreqs, 0, maxFreqs, 0, maxFreqs.length);
otherFreqNormPairs.clear();
otherFreqNormPairs.addAll(acc.otherFreqNormPairs);
assert assertConsistent();
}
/** Get the set of competitive freq and norm pairs, ordered by increasing freq and norm. */
public Collection<Impact> getCompetitiveFreqNormPairs() {
List<Impact> impacts = new ArrayList<>();

View File

@ -166,7 +166,7 @@ public final class Lucene99SkipWriter extends MultiLevelSkipListWriter {
this.curPayPointer = payFP;
this.curPosBufferUpto = posBufferUpto;
this.curPayloadByteUpto = payloadByteUpto;
this.curCompetitiveFreqNorms[0].addAll(competitiveFreqNorms);
this.curCompetitiveFreqNorms[0].copy(competitiveFreqNorms);
bufferSkip(numDocs);
}

View File

@ -83,6 +83,71 @@ public class TestCompetitiveFreqNormAccumulator extends LuceneTestCase {
assertEquals(List.copyOf(expected), List.copyOf(acc.getCompetitiveFreqNormPairs()));
}
public void testCopy() {
CompetitiveImpactAccumulator acc = new CompetitiveImpactAccumulator();
CompetitiveImpactAccumulator copiedAcc = new CompetitiveImpactAccumulator();
CompetitiveImpactAccumulator mergedAcc = new CompetitiveImpactAccumulator();
acc.add(3, 5);
copiedAcc.copy(acc);
assertEquals(
List.copyOf(copiedAcc.getCompetitiveFreqNormPairs()),
List.copyOf(acc.getCompetitiveFreqNormPairs()));
mergedAcc.addAll(acc);
assertEquals(
List.copyOf(copiedAcc.getCompetitiveFreqNormPairs()),
List.copyOf(mergedAcc.getCompetitiveFreqNormPairs()));
acc.add(10, 10000);
copiedAcc.copy(acc);
assertEquals(
List.copyOf(copiedAcc.getCompetitiveFreqNormPairs()),
List.copyOf(acc.getCompetitiveFreqNormPairs()));
mergedAcc.clear();
mergedAcc.addAll(acc);
assertEquals(
List.copyOf(copiedAcc.getCompetitiveFreqNormPairs()),
List.copyOf(mergedAcc.getCompetitiveFreqNormPairs()));
acc.add(5, 200);
copiedAcc.copy(acc);
assertEquals(
List.copyOf(copiedAcc.getCompetitiveFreqNormPairs()),
List.copyOf(acc.getCompetitiveFreqNormPairs()));
mergedAcc.clear();
mergedAcc.addAll(acc);
assertEquals(
List.copyOf(copiedAcc.getCompetitiveFreqNormPairs()),
List.copyOf(mergedAcc.getCompetitiveFreqNormPairs()));
acc.add(20, -100);
copiedAcc.copy(acc);
assertEquals(
List.copyOf(copiedAcc.getCompetitiveFreqNormPairs()),
List.copyOf(acc.getCompetitiveFreqNormPairs()));
mergedAcc.clear();
mergedAcc.addAll(acc);
assertEquals(
List.copyOf(copiedAcc.getCompetitiveFreqNormPairs()),
List.copyOf(mergedAcc.getCompetitiveFreqNormPairs()));
acc.add(30, -3);
copiedAcc.copy(acc);
assertEquals(
List.copyOf(copiedAcc.getCompetitiveFreqNormPairs()),
List.copyOf(acc.getCompetitiveFreqNormPairs()));
mergedAcc.clear();
mergedAcc.addAll(acc);
assertEquals(
List.copyOf(copiedAcc.getCompetitiveFreqNormPairs()),
List.copyOf(mergedAcc.getCompetitiveFreqNormPairs()));
}
public void testOmitFreqs() {
CompetitiveImpactAccumulator acc = new CompetitiveImpactAccumulator();