clean up these slow-wrapper impls a little bit

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene4547@1442170 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2013-02-04 16:37:33 +00:00
parent 5775a408ca
commit 563c6fa093
3 changed files with 87 additions and 64 deletions

View File

@ -36,10 +36,12 @@ public abstract class BinaryDocValues {
*/ */
public static final byte[] MISSING = new byte[0]; public static final byte[] MISSING = new byte[0];
/** An empty BinaryDocValues which returns empty bytes for every document */ /** An empty BinaryDocValues which returns {@link #MISSING} for every document */
public static final BinaryDocValues EMPTY = new BinaryDocValues() { public static final BinaryDocValues EMPTY = new BinaryDocValues() {
@Override @Override
public void get(int docID, BytesRef result) { public void get(int docID, BytesRef result) {
result.bytes = MISSING;
result.offset = 0;
result.length = 0; result.length = 0;
} }
}; };

View File

@ -51,61 +51,82 @@ public class MultiDocValues {
/** No instantiation */ /** No instantiation */
private MultiDocValues() {} private MultiDocValues() {}
/** returns a NumericDocValues for a reader's norms (potentially merging on-the-fly) */ /** Returns a NumericDocValues for a reader's norms (potentially merging on-the-fly).
// moved to src/java so SlowWrapper can use it... uggggggh * <p>
* This is a slow way to access normalization values. Instead, access them per-segment
* with {@link AtomicReader#getNormValues(String)}
* </p>
*/
public static NumericDocValues getNormValues(final IndexReader r, final String field) throws IOException { public static NumericDocValues getNormValues(final IndexReader r, final String field) throws IOException {
final List<AtomicReaderContext> leaves = r.leaves(); final List<AtomicReaderContext> leaves = r.leaves();
if (leaves.size() == 1) { final int size = leaves.size();
if (size == 0) {
return null;
} else if (size == 1) {
return leaves.get(0).reader().getNormValues(field); return leaves.get(0).reader().getNormValues(field);
} }
FieldInfo fi = MultiFields.getMergedFieldInfos(r).fieldInfo(field); FieldInfo fi = MultiFields.getMergedFieldInfos(r).fieldInfo(field);
if (fi == null || fi.hasNorms() == false) { if (fi == null || fi.hasNorms() == false) {
return null; return null;
} }
boolean anyReal = false;
for(AtomicReaderContext ctx : leaves) {
NumericDocValues norms = ctx.reader().getNormValues(field);
if (norms != null) { boolean anyReal = false;
final NumericDocValues[] values = new NumericDocValues[size];
final int[] starts = new int[size+1];
for (int i = 0; i < size; i++) {
AtomicReaderContext context = leaves.get(i);
NumericDocValues v = context.reader().getNormValues(field);
if (v == null) {
v = NumericDocValues.EMPTY;
} else {
anyReal = true; anyReal = true;
} }
values[i] = v;
starts[i] = context.docBase;
} }
starts[size] = r.maxDoc();
assert anyReal; assert anyReal;
return new NumericDocValues() { return new NumericDocValues() {
@Override @Override
public long get(int docID) { public long get(int docID) {
int subIndex = ReaderUtil.subIndex(docID, leaves); int subIndex = ReaderUtil.subIndex(docID, starts);
NumericDocValues norms; return values[subIndex].get(docID - starts[subIndex]);
try {
norms = leaves.get(subIndex).reader().getNormValues(field);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
if (norms == null) {
return 0;
} else {
return norms.get(docID - leaves.get(subIndex).docBase);
}
} }
}; };
} }
/** returns a NumericDocValues for a reader's docvalues (potentially merging on-the-fly) */ /** Returns a NumericDocValues for a reader's docvalues (potentially merging on-the-fly)
* <p>
* This is a slow way to access numeric values. Instead, access them per-segment
* with {@link AtomicReader#getNumericDocValues(String)}
* </p>
* */
public static NumericDocValues getNumericValues(final IndexReader r, final String field) throws IOException { public static NumericDocValues getNumericValues(final IndexReader r, final String field) throws IOException {
final List<AtomicReaderContext> leaves = r.leaves(); final List<AtomicReaderContext> leaves = r.leaves();
if (leaves.size() == 1) { final int size = leaves.size();
if (size == 0) {
return null;
} else if (size == 1) {
return leaves.get(0).reader().getNumericDocValues(field); return leaves.get(0).reader().getNumericDocValues(field);
} }
boolean anyReal = false;
for(AtomicReaderContext ctx : leaves) {
NumericDocValues values = ctx.reader().getNumericDocValues(field);
if (values != null) { boolean anyReal = false;
final NumericDocValues[] values = new NumericDocValues[size];
final int[] starts = new int[size+1];
for (int i = 0; i < size; i++) {
AtomicReaderContext context = leaves.get(i);
NumericDocValues v = context.reader().getNumericDocValues(field);
if (v == null) {
v = NumericDocValues.EMPTY;
} else {
anyReal = true; anyReal = true;
} }
values[i] = v;
starts[i] = context.docBase;
} }
starts[size] = r.maxDoc();
if (!anyReal) { if (!anyReal) {
return null; return null;
@ -113,66 +134,64 @@ public class MultiDocValues {
return new NumericDocValues() { return new NumericDocValues() {
@Override @Override
public long get(int docID) { public long get(int docID) {
int subIndex = ReaderUtil.subIndex(docID, leaves); int subIndex = ReaderUtil.subIndex(docID, starts);
NumericDocValues values; return values[subIndex].get(docID - starts[subIndex]);
try {
values = leaves.get(subIndex).reader().getNumericDocValues(field);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
if (values == null) {
return 0;
} else {
return values.get(docID - leaves.get(subIndex).docBase);
}
} }
}; };
} }
} }
/** returns a BinaryDocValues for a reader's docvalues (potentially merging on-the-fly) */ /** Returns a BinaryDocValues for a reader's docvalues (potentially merging on-the-fly)
* <p>
* This is a slow way to access binary values. Instead, access them per-segment
* with {@link AtomicReader#getBinaryDocValues(String)}
* </p>
*/
public static BinaryDocValues getBinaryValues(final IndexReader r, final String field) throws IOException { public static BinaryDocValues getBinaryValues(final IndexReader r, final String field) throws IOException {
final List<AtomicReaderContext> leaves = r.leaves(); final List<AtomicReaderContext> leaves = r.leaves();
if (leaves.size() == 1) { final int size = leaves.size();
if (size == 0) {
return null;
} else if (size == 1) {
return leaves.get(0).reader().getBinaryDocValues(field); return leaves.get(0).reader().getBinaryDocValues(field);
} }
boolean anyReal = false; boolean anyReal = false;
final BinaryDocValues[] values = new BinaryDocValues[size];
for(AtomicReaderContext ctx : leaves) { final int[] starts = new int[size+1];
BinaryDocValues values = ctx.reader().getBinaryDocValues(field); for (int i = 0; i < size; i++) {
AtomicReaderContext context = leaves.get(i);
if (values != null) { BinaryDocValues v = context.reader().getBinaryDocValues(field);
if (v == null) {
v = BinaryDocValues.EMPTY;
} else {
anyReal = true; anyReal = true;
} }
values[i] = v;
starts[i] = context.docBase;
} }
starts[size] = r.maxDoc();
if (!anyReal) { if (!anyReal) {
return null; return null;
} else { } else {
return new BinaryDocValues() { return new BinaryDocValues() {
@Override @Override
public void get(int docID, BytesRef result) { public void get(int docID, BytesRef result) {
int subIndex = ReaderUtil.subIndex(docID, leaves); int subIndex = ReaderUtil.subIndex(docID, starts);
BinaryDocValues values; values[subIndex].get(docID - starts[subIndex], result);
try {
values = leaves.get(subIndex).reader().getBinaryDocValues(field);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
if (values != null) {
values.get(docID - leaves.get(subIndex).docBase, result);
} else {
result.length = 0;
result.offset = 0;
result.bytes = BinaryDocValues.MISSING;
}
} }
}; };
} }
} }
/** returns a SortedDocValues for a reader's docvalues (potentially doing extremely slow things) */ /** Returns a SortedDocValues for a reader's docvalues (potentially doing extremely slow things).
* <p>
* This is an extremely slow way to access sorted values. Instead, access them per-segment
* with {@link AtomicReader#getSortedDocValues(String)}
* </p>
*/
public static SortedDocValues getSortedValues(final IndexReader r, final String field) throws IOException { public static SortedDocValues getSortedValues(final IndexReader r, final String field) throws IOException {
final List<AtomicReaderContext> leaves = r.leaves(); final List<AtomicReaderContext> leaves = r.leaves();
if (leaves.size() == 1) { if (leaves.size() == 1) {

View File

@ -67,7 +67,7 @@ public abstract class SortedDocValues extends BinaryDocValues {
} }
} }
/** An empty SortedDocValues which returns empty bytes for every document */ /** An empty SortedDocValues which returns {@link #MISSING} for every document */
public static final SortedDocValues EMPTY = new SortedDocValues() { public static final SortedDocValues EMPTY = new SortedDocValues() {
@Override @Override
public int getOrd(int docID) { public int getOrd(int docID) {
@ -76,6 +76,8 @@ public abstract class SortedDocValues extends BinaryDocValues {
@Override @Override
public void lookupOrd(int ord, BytesRef result) { public void lookupOrd(int ord, BytesRef result) {
result.bytes = MISSING;
result.offset = 0;
result.length = 0; result.length = 0;
} }