Simplify NestedFieldComparators for numerics

The average and sum comparators basically share the same code which is
copy-past today. We can simplify this into a base class which reduces
code duplication and prevents copy-paste bugs.
This commit is contained in:
Simon Willnauer 2013-09-16 21:40:08 +02:00
parent cabbf7805b
commit 1499881c36
1 changed files with 49 additions and 75 deletions

View File

@ -242,10 +242,8 @@ abstract class NestedFieldComparator extends FieldComparator {
int cmp1 = wrappedComparator.compareBottom(nestedDoc);
if (cmp1 < 0) {
return cmp1;
} else {
if (cmp1 == 0) {
cmp = 0;
}
} else if (cmp1 == 0) {
cmp = 0;
}
}
}
@ -278,132 +276,108 @@ abstract class NestedFieldComparator extends FieldComparator {
}
}
static abstract class NumericNestedFieldComparatorBase extends NestedFieldComparator {
protected NumberComparatorBase numberComparator;
final static class Sum extends NestedFieldComparator {
NumberComparatorBase wrappedComparator;
Sum(NumberComparatorBase wrappedComparator, Filter rootDocumentsFilter, Filter innerDocumentsFilter, int spareSlot) {
NumericNestedFieldComparatorBase(NumberComparatorBase wrappedComparator, Filter rootDocumentsFilter, Filter innerDocumentsFilter, int spareSlot) {
super(wrappedComparator, rootDocumentsFilter, innerDocumentsFilter, spareSlot);
this.wrappedComparator = wrappedComparator;
this.numberComparator = wrappedComparator;
}
@Override
public int compareBottom(int rootDoc) throws IOException {
public final int compareBottom(int rootDoc) throws IOException {
if (rootDoc == 0 || rootDocuments == null || innerDocuments == null) {
return compareBottomMissing(wrappedComparator);
}
int prevRootDoc = rootDocuments.prevSetBit(rootDoc - 1);
final int prevRootDoc = rootDocuments.prevSetBit(rootDoc - 1);
int nestedDoc = innerDocuments.nextSetBit(prevRootDoc + 1);
if (nestedDoc >= rootDoc || nestedDoc == -1) {
return compareBottomMissing(wrappedComparator);
}
int counter = 1;
wrappedComparator.copy(spareSlot, nestedDoc);
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
while (nestedDoc > prevRootDoc && nestedDoc < rootDoc) {
wrappedComparator.add(spareSlot, nestedDoc);
onNested(spareSlot, nestedDoc);
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
counter++;
}
afterNested(spareSlot, counter);
return compare(bottomSlot, spareSlot);
}
@Override
public void copy(int slot, int rootDoc) throws IOException {
public final void copy(int slot, int rootDoc) throws IOException {
if (rootDoc == 0 || rootDocuments == null || innerDocuments == null) {
copyMissing(wrappedComparator, slot);
return;
}
int prevRootDoc = rootDocuments.prevSetBit(rootDoc - 1);
final int prevRootDoc = rootDocuments.prevSetBit(rootDoc - 1);
int nestedDoc = innerDocuments.nextSetBit(prevRootDoc + 1);
if (nestedDoc >= rootDoc || nestedDoc == -1) {
copyMissing(wrappedComparator, slot);
return;
}
int counter = 1;
wrappedComparator.copy(slot, nestedDoc);
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
while (nestedDoc > prevRootDoc && nestedDoc < rootDoc) {
wrappedComparator.add(slot, nestedDoc);
onNested(slot, nestedDoc);
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
counter++;
}
afterNested(slot, counter);
}
protected abstract void onNested(int slot, int nestedDoc);
protected abstract void afterNested(int slot, int count);
@Override
public final FieldComparator setNextReader(AtomicReaderContext context) throws IOException {
super.setNextReader(context);
numberComparator = (NumberComparatorBase) super.wrappedComparator;
return this;
}
}
final static class Sum extends NumericNestedFieldComparatorBase {
Sum(NumberComparatorBase wrappedComparator, Filter rootDocumentsFilter, Filter innerDocumentsFilter, int spareSlot) {
super(wrappedComparator, rootDocumentsFilter, innerDocumentsFilter, spareSlot);
}
@Override
public FieldComparator setNextReader(AtomicReaderContext context) throws IOException {
super.setNextReader(context);
wrappedComparator = (NumberComparatorBase) super.wrappedComparator;
return this;
protected void onNested(int slot, int nestedDoc) {
numberComparator.add(slot, nestedDoc);
}
@Override
protected void afterNested(int slot, int count) {
}
}
final static class Avg extends NestedFieldComparator {
NumberComparatorBase wrappedComparator;
final static class Avg extends NumericNestedFieldComparatorBase {
Avg(NumberComparatorBase wrappedComparator, Filter rootDocumentsFilter, Filter innerDocumentsFilter, int spareSlot) {
super(wrappedComparator, rootDocumentsFilter, innerDocumentsFilter, spareSlot);
this.wrappedComparator = wrappedComparator;
}
@Override
public int compareBottom(int rootDoc) throws IOException {
if (rootDoc == 0 || rootDocuments == null || innerDocuments == null) {
return 0;
}
int prevRootDoc = rootDocuments.prevSetBit(rootDoc - 1);
int nestedDoc = innerDocuments.nextSetBit(prevRootDoc + 1);
if (nestedDoc >= rootDoc || nestedDoc == -1) {
return compareBottomMissing(wrappedComparator);
}
int counter = 1;
wrappedComparator.copy(spareSlot, nestedDoc);
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
while (nestedDoc > prevRootDoc && nestedDoc < rootDoc) {
wrappedComparator.add(spareSlot, nestedDoc);
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
counter++;
}
wrappedComparator.divide(spareSlot, counter);
return compare(bottomSlot, spareSlot);
protected void onNested(int slot, int nestedDoc) {
numberComparator.add(slot, nestedDoc);
}
@Override
public void copy(int slot, int rootDoc) throws IOException {
if (rootDoc == 0 || rootDocuments == null || innerDocuments == null) {
copyMissing(wrappedComparator, slot);
return;
}
int prevRootDoc = rootDocuments.prevSetBit(rootDoc - 1);
int nestedDoc = innerDocuments.nextSetBit(prevRootDoc + 1);
if (nestedDoc >= rootDoc || nestedDoc == -1) {
copyMissing(wrappedComparator, slot);
return;
}
int counter = 1;
wrappedComparator.copy(slot, nestedDoc);
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
while (nestedDoc > prevRootDoc && nestedDoc < rootDoc) {
wrappedComparator.add(slot, nestedDoc);
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
counter++;
}
wrappedComparator.divide(slot, counter);
protected void afterNested(int slot, int count) {
numberComparator.divide(slot, count);
}
@Override
public FieldComparator setNextReader(AtomicReaderContext context) throws IOException {
super.setNextReader(context);
wrappedComparator = (NumberComparatorBase) super.wrappedComparator;
return this;
}
}
static final void copyMissing(FieldComparator<?> comparator, int slot) {