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:
parent
cabbf7805b
commit
1499881c36
|
@ -242,10 +242,8 @@ abstract class NestedFieldComparator extends FieldComparator {
|
||||||
int cmp1 = wrappedComparator.compareBottom(nestedDoc);
|
int cmp1 = wrappedComparator.compareBottom(nestedDoc);
|
||||||
if (cmp1 < 0) {
|
if (cmp1 < 0) {
|
||||||
return cmp1;
|
return cmp1;
|
||||||
} else {
|
} else if (cmp1 == 0) {
|
||||||
if (cmp1 == 0) {
|
cmp = 0;
|
||||||
cmp = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -279,131 +277,107 @@ abstract class NestedFieldComparator extends FieldComparator {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final static class Sum extends NestedFieldComparator {
|
static abstract class NumericNestedFieldComparatorBase extends NestedFieldComparator {
|
||||||
|
protected NumberComparatorBase numberComparator;
|
||||||
|
|
||||||
NumberComparatorBase wrappedComparator;
|
NumericNestedFieldComparatorBase(NumberComparatorBase wrappedComparator, Filter rootDocumentsFilter, Filter innerDocumentsFilter, int spareSlot) {
|
||||||
|
|
||||||
Sum(NumberComparatorBase wrappedComparator, Filter rootDocumentsFilter, Filter innerDocumentsFilter, int spareSlot) {
|
|
||||||
super(wrappedComparator, rootDocumentsFilter, innerDocumentsFilter, spareSlot);
|
super(wrappedComparator, rootDocumentsFilter, innerDocumentsFilter, spareSlot);
|
||||||
this.wrappedComparator = wrappedComparator;
|
this.numberComparator = wrappedComparator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareBottom(int rootDoc) throws IOException {
|
public final int compareBottom(int rootDoc) throws IOException {
|
||||||
if (rootDoc == 0 || rootDocuments == null || innerDocuments == null) {
|
if (rootDoc == 0 || rootDocuments == null || innerDocuments == null) {
|
||||||
return compareBottomMissing(wrappedComparator);
|
return compareBottomMissing(wrappedComparator);
|
||||||
}
|
}
|
||||||
|
|
||||||
int prevRootDoc = rootDocuments.prevSetBit(rootDoc - 1);
|
final int prevRootDoc = rootDocuments.prevSetBit(rootDoc - 1);
|
||||||
int nestedDoc = innerDocuments.nextSetBit(prevRootDoc + 1);
|
int nestedDoc = innerDocuments.nextSetBit(prevRootDoc + 1);
|
||||||
if (nestedDoc >= rootDoc || nestedDoc == -1) {
|
if (nestedDoc >= rootDoc || nestedDoc == -1) {
|
||||||
return compareBottomMissing(wrappedComparator);
|
return compareBottomMissing(wrappedComparator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int counter = 1;
|
||||||
wrappedComparator.copy(spareSlot, nestedDoc);
|
wrappedComparator.copy(spareSlot, nestedDoc);
|
||||||
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
|
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
|
||||||
while (nestedDoc > prevRootDoc && nestedDoc < rootDoc) {
|
while (nestedDoc > prevRootDoc && nestedDoc < rootDoc) {
|
||||||
wrappedComparator.add(spareSlot, nestedDoc);
|
onNested(spareSlot, nestedDoc);
|
||||||
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
|
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
|
||||||
|
counter++;
|
||||||
}
|
}
|
||||||
|
afterNested(spareSlot, counter);
|
||||||
return compare(bottomSlot, spareSlot);
|
return compare(bottomSlot, spareSlot);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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) {
|
if (rootDoc == 0 || rootDocuments == null || innerDocuments == null) {
|
||||||
copyMissing(wrappedComparator, slot);
|
copyMissing(wrappedComparator, slot);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int prevRootDoc = rootDocuments.prevSetBit(rootDoc - 1);
|
final int prevRootDoc = rootDocuments.prevSetBit(rootDoc - 1);
|
||||||
int nestedDoc = innerDocuments.nextSetBit(prevRootDoc + 1);
|
int nestedDoc = innerDocuments.nextSetBit(prevRootDoc + 1);
|
||||||
if (nestedDoc >= rootDoc || nestedDoc == -1) {
|
if (nestedDoc >= rootDoc || nestedDoc == -1) {
|
||||||
copyMissing(wrappedComparator, slot);
|
copyMissing(wrappedComparator, slot);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
int counter = 1;
|
||||||
wrappedComparator.copy(slot, nestedDoc);
|
wrappedComparator.copy(slot, nestedDoc);
|
||||||
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
|
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
|
||||||
while (nestedDoc > prevRootDoc && nestedDoc < rootDoc) {
|
while (nestedDoc > prevRootDoc && nestedDoc < rootDoc) {
|
||||||
wrappedComparator.add(slot, nestedDoc);
|
onNested(slot, nestedDoc);
|
||||||
nestedDoc = innerDocuments.nextSetBit(nestedDoc + 1);
|
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
|
@Override
|
||||||
public FieldComparator setNextReader(AtomicReaderContext context) throws IOException {
|
protected void onNested(int slot, int nestedDoc) {
|
||||||
super.setNextReader(context);
|
numberComparator.add(slot, nestedDoc);
|
||||||
wrappedComparator = (NumberComparatorBase) super.wrappedComparator;
|
}
|
||||||
return this;
|
|
||||||
|
@Override
|
||||||
|
protected void afterNested(int slot, int count) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final static class Avg extends NestedFieldComparator {
|
final static class Avg extends NumericNestedFieldComparatorBase {
|
||||||
|
|
||||||
NumberComparatorBase wrappedComparator;
|
|
||||||
|
|
||||||
Avg(NumberComparatorBase wrappedComparator, Filter rootDocumentsFilter, Filter innerDocumentsFilter, int spareSlot) {
|
Avg(NumberComparatorBase wrappedComparator, Filter rootDocumentsFilter, Filter innerDocumentsFilter, int spareSlot) {
|
||||||
super(wrappedComparator, rootDocumentsFilter, innerDocumentsFilter, spareSlot);
|
super(wrappedComparator, rootDocumentsFilter, innerDocumentsFilter, spareSlot);
|
||||||
this.wrappedComparator = wrappedComparator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareBottom(int rootDoc) throws IOException {
|
protected void onNested(int slot, int nestedDoc) {
|
||||||
if (rootDoc == 0 || rootDocuments == null || innerDocuments == null) {
|
numberComparator.add(slot, nestedDoc);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void copy(int slot, int rootDoc) throws IOException {
|
protected void afterNested(int slot, int count) {
|
||||||
if (rootDoc == 0 || rootDocuments == null || innerDocuments == null) {
|
numberComparator.divide(slot, count);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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) {
|
static final void copyMissing(FieldComparator<?> comparator, int slot) {
|
||||||
|
|
Loading…
Reference in New Issue