LUCENE-3502: move .has/getArray into PackedInts.Reader and make the direct impls package private

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1181070 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2011-10-10 17:00:42 +00:00
parent 3088c013ae
commit 9e027695c8
10 changed files with 134 additions and 61 deletions

View File

@ -26,9 +26,6 @@ import org.apache.lucene.search.cache.*;
import org.apache.lucene.search.cache.CachedArray.*; import org.apache.lucene.search.cache.CachedArray.*;
import org.apache.lucene.util.Bits; import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.packed.Direct16;
import org.apache.lucene.util.packed.Direct32;
import org.apache.lucene.util.packed.Direct8;
import org.apache.lucene.util.packed.PackedInts; import org.apache.lucene.util.packed.PackedInts;
import java.io.IOException; import java.io.IOException;
@ -1222,14 +1219,21 @@ public abstract class FieldComparator<T> {
final int docBase = context.docBase; final int docBase = context.docBase;
termsIndex = FieldCache.DEFAULT.getTermsIndex(context.reader, field); termsIndex = FieldCache.DEFAULT.getTermsIndex(context.reader, field);
final PackedInts.Reader docToOrd = termsIndex.getDocToOrd(); final PackedInts.Reader docToOrd = termsIndex.getDocToOrd();
FieldComparator perSegComp; FieldComparator perSegComp = null;
if (docToOrd instanceof Direct8) { if (docToOrd.hasArray()) {
perSegComp = new ByteOrdComparator(((Direct8) docToOrd).getArray(), termsIndex, docBase); final Object arr = docToOrd.getArray();
} else if (docToOrd instanceof Direct16) { if (arr instanceof byte[]) {
perSegComp = new ShortOrdComparator(((Direct16) docToOrd).getArray(), termsIndex, docBase); perSegComp = new ByteOrdComparator((byte[]) arr, termsIndex, docBase);
} else if (docToOrd instanceof Direct32) { } else if (arr instanceof short[]) {
perSegComp = new IntOrdComparator(((Direct32) docToOrd).getArray(), termsIndex, docBase); perSegComp = new ShortOrdComparator((short[]) arr, termsIndex, docBase);
} else { } else if (arr instanceof int[]) {
perSegComp = new IntOrdComparator((int[]) arr, termsIndex, docBase);
}
// Don't specialize the long[] case since it's not
// possible, ie, worse case is MAX_INT-1 docs with
// every one having a unique value.
}
if (perSegComp == null) {
perSegComp = new AnyOrdComparator(docToOrd, termsIndex, docBase); perSegComp = new AnyOrdComparator(docToOrd, termsIndex, docBase);
} }

View File

@ -28,7 +28,7 @@ import java.util.Arrays;
* @lucene.internal * @lucene.internal
*/ */
public class Direct16 extends PackedInts.ReaderImpl class Direct16 extends PackedInts.ReaderImpl
implements PackedInts.Mutable { implements PackedInts.Mutable {
private short[] values; private short[] values;
private static final int BITS_PER_VALUE = 16; private static final int BITS_PER_VALUE = 16;
@ -68,10 +68,6 @@ public class Direct16 extends PackedInts.ReaderImpl
this.values = values; this.values = values;
} }
public short[] getArray() {
return values;
}
public long get(final int index) { public long get(final int index) {
return 0xFFFFL & values[index]; return 0xFFFFL & values[index];
} }
@ -88,4 +84,14 @@ public class Direct16 extends PackedInts.ReaderImpl
public void clear() { public void clear() {
Arrays.fill(values, (short)0); Arrays.fill(values, (short)0);
} }
@Override
public Object getArray() {
return values;
}
@Override
public boolean hasArray() {
return true;
}
} }

View File

@ -28,7 +28,7 @@ import java.util.Arrays;
* @lucene.internal * @lucene.internal
*/ */
public class Direct32 extends PackedInts.ReaderImpl class Direct32 extends PackedInts.ReaderImpl
implements PackedInts.Mutable { implements PackedInts.Mutable {
private int[] values; private int[] values;
private static final int BITS_PER_VALUE = 32; private static final int BITS_PER_VALUE = 32;
@ -64,10 +64,6 @@ public class Direct32 extends PackedInts.ReaderImpl
this.values = values; this.values = values;
} }
public int[] getArray() {
return values;
}
public long get(final int index) { public long get(final int index) {
return 0xFFFFFFFFL & values[index]; return 0xFFFFFFFFL & values[index];
} }
@ -84,4 +80,14 @@ public class Direct32 extends PackedInts.ReaderImpl
public void clear() { public void clear() {
Arrays.fill(values, 0); Arrays.fill(values, 0);
} }
@Override
public int[] getArray() {
return values;
}
@Override
public boolean hasArray() {
return true;
}
} }

View File

@ -28,7 +28,7 @@ import java.util.Arrays;
* @lucene.internal * @lucene.internal
*/ */
public class Direct64 extends PackedInts.ReaderImpl class Direct64 extends PackedInts.ReaderImpl
implements PackedInts.Mutable { implements PackedInts.Mutable {
private long[] values; private long[] values;
private static final int BITS_PER_VALUE = 64; private static final int BITS_PER_VALUE = 64;
@ -48,10 +48,6 @@ public class Direct64 extends PackedInts.ReaderImpl
this.values = values; this.values = values;
} }
public long[] getArray() {
return values;
}
/** /**
* Creates an array backed by the given values. * Creates an array backed by the given values.
* </p><p> * </p><p>
@ -80,4 +76,14 @@ public class Direct64 extends PackedInts.ReaderImpl
public void clear() { public void clear() {
Arrays.fill(values, 0L); Arrays.fill(values, 0L);
} }
@Override
public long[] getArray() {
return values;
}
@Override
public boolean hasArray() {
return true;
}
} }

View File

@ -28,7 +28,7 @@ import java.util.Arrays;
* @lucene.internal * @lucene.internal
*/ */
public class Direct8 extends PackedInts.ReaderImpl class Direct8 extends PackedInts.ReaderImpl
implements PackedInts.Mutable { implements PackedInts.Mutable {
private byte[] values; private byte[] values;
private static final int BITS_PER_VALUE = 8; private static final int BITS_PER_VALUE = 8;
@ -69,10 +69,6 @@ public class Direct8 extends PackedInts.ReaderImpl
this.values = values; this.values = values;
} }
public byte[] getArray() {
return values;
}
public long get(final int index) { public long get(final int index) {
return 0xFFL & values[index]; return 0xFFL & values[index];
} }
@ -88,4 +84,14 @@ public class Direct8 extends PackedInts.ReaderImpl
public void clear() { public void clear() {
Arrays.fill(values, (byte)0); Arrays.fill(values, (byte)0);
} }
@Override
public Object getArray() {
return values;
}
@Override
public boolean hasArray() {
return true;
}
} }

View File

@ -60,6 +60,16 @@ public class GrowableWriter implements PackedInts.Mutable {
return current; return current;
} }
@Override
public Object getArray() {
return current.getArray();
}
@Override
public boolean hasArray() {
return current.hasArray();
}
public void set(int index, long value) { public void set(int index, long value) {
if (value >= currentMaxValue) { if (value >= currentMaxValue) {
int bpv = getBitsPerValue(); int bpv = getBitsPerValue();

View File

@ -65,6 +65,25 @@ public class PackedInts {
* @return the number of values. * @return the number of values.
*/ */
int size(); int size();
/**
* Expert: if the bit-width of this reader matches one of
* java's native types, returns the underlying array
* (ie, byte[], short[], int[], long[]); else, returns
* null. Note that when accessing the array you must
* upgrade the type (bitwise AND with all ones), to
* interpret the full value as unsigned. Ie,
* bytes[idx]&0xFF, shorts[idx]&0xFFFF, etc.
*/
Object getArray();
/**
* Returns true if this implementation is backed by a
* native java array.
*
* @see #getArray
*/
boolean hasArray();
} }
/** /**
@ -137,6 +156,14 @@ public class PackedInts {
public long getMaxValue() { // Convenience method public long getMaxValue() { // Convenience method
return maxValue(bitsPerValue); return maxValue(bitsPerValue);
} }
public Object getArray() {
return null;
}
public boolean hasArray() {
return false;
}
} }
/** A write-once Writer. /** A write-once Writer.

View File

@ -28,9 +28,6 @@ import org.apache.lucene.util.PriorityQueue;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.ReaderUtil; import org.apache.lucene.util.ReaderUtil;
import org.apache.lucene.util.UnicodeUtil; import org.apache.lucene.util.UnicodeUtil;
import org.apache.lucene.util.packed.Direct16;
import org.apache.lucene.util.packed.Direct32;
import org.apache.lucene.util.packed.Direct8;
import org.apache.lucene.util.packed.PackedInts; import org.apache.lucene.util.packed.PackedInts;
import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.FacetParams; import org.apache.solr.common.params.FacetParams;
@ -268,8 +265,15 @@ class PerSegmentSingleValuedFaceting {
PackedInts.Reader ordReader = si.getDocToOrd(); PackedInts.Reader ordReader = si.getDocToOrd();
int doc; int doc;
if (ordReader instanceof Direct32) { final Object arr;
int[] ords = ((Direct32)ordReader).getArray(); if (ordReader.hasArray()) {
arr = ordReader.getArray();
} else {
arr = null;
}
if (arr instanceof int[]) {
int[] ords = (int[]) arr;
if (prefix==null) { if (prefix==null) {
while ((doc = iter.nextDoc()) < DocIdSetIterator.NO_MORE_DOCS) { while ((doc = iter.nextDoc()) < DocIdSetIterator.NO_MORE_DOCS) {
counts[ords[doc]]++; counts[ords[doc]]++;
@ -281,8 +285,8 @@ class PerSegmentSingleValuedFaceting {
if (arrIdx>=0 && arrIdx<nTerms) counts[arrIdx]++; if (arrIdx>=0 && arrIdx<nTerms) counts[arrIdx]++;
} }
} }
} else if (ordReader instanceof Direct16) { } else if (arr instanceof short[]) {
short[] ords = ((Direct16)ordReader).getArray(); short[] ords = (short[]) arr;
if (prefix==null) { if (prefix==null) {
while ((doc = iter.nextDoc()) < DocIdSetIterator.NO_MORE_DOCS) { while ((doc = iter.nextDoc()) < DocIdSetIterator.NO_MORE_DOCS) {
counts[ords[doc] & 0xffff]++; counts[ords[doc] & 0xffff]++;
@ -294,8 +298,8 @@ class PerSegmentSingleValuedFaceting {
if (arrIdx>=0 && arrIdx<nTerms) counts[arrIdx]++; if (arrIdx>=0 && arrIdx<nTerms) counts[arrIdx]++;
} }
} }
} else if (ordReader instanceof Direct8) { } else if (arr instanceof byte[]) {
byte[] ords = ((Direct8)ordReader).getArray(); byte[] ords = (byte[]) arr;
if (prefix==null) { if (prefix==null) {
while ((doc = iter.nextDoc()) < DocIdSetIterator.NO_MORE_DOCS) { while ((doc = iter.nextDoc()) < DocIdSetIterator.NO_MORE_DOCS) {
counts[ords[doc] & 0xff]++; counts[ords[doc] & 0xff]++;
@ -322,7 +326,6 @@ class PerSegmentSingleValuedFaceting {
} }
} }
} }
} }
} }
} }

View File

@ -22,9 +22,6 @@ import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.*; import org.apache.lucene.search.*;
import org.apache.lucene.search.grouping.AbstractAllGroupHeadsCollector; import org.apache.lucene.search.grouping.AbstractAllGroupHeadsCollector;
import org.apache.lucene.util.*; import org.apache.lucene.util.*;
import org.apache.lucene.util.packed.Direct16;
import org.apache.lucene.util.packed.Direct32;
import org.apache.lucene.util.packed.Direct8;
import org.apache.lucene.util.packed.PackedInts; import org.apache.lucene.util.packed.PackedInts;
import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode; import org.apache.solr.common.SolrException.ErrorCode;
@ -450,8 +447,15 @@ public class SimpleFacets {
DocIterator iter = docs.iterator(); DocIterator iter = docs.iterator();
PackedInts.Reader ordReader = si.getDocToOrd(); PackedInts.Reader ordReader = si.getDocToOrd();
if (ordReader instanceof Direct32) { final Object arr;
int[] ords = ((Direct32)ordReader).getArray(); if (ordReader.hasArray()) {
arr = ordReader.getArray();
} else {
arr = null;
}
if (arr instanceof int[]) {
int[] ords = (int[]) arr;
if (prefix==null) { if (prefix==null) {
while (iter.hasNext()) { while (iter.hasNext()) {
counts[ords[iter.nextDoc()]]++; counts[ords[iter.nextDoc()]]++;
@ -463,8 +467,8 @@ public class SimpleFacets {
if (arrIdx>=0 && arrIdx<nTerms) counts[arrIdx]++; if (arrIdx>=0 && arrIdx<nTerms) counts[arrIdx]++;
} }
} }
} else if (ordReader instanceof Direct16) { } else if (arr instanceof short[]) {
short[] ords = ((Direct16)ordReader).getArray(); short[] ords = (short[]) arr;
if (prefix==null) { if (prefix==null) {
while (iter.hasNext()) { while (iter.hasNext()) {
counts[ords[iter.nextDoc()] & 0xffff]++; counts[ords[iter.nextDoc()] & 0xffff]++;
@ -476,8 +480,8 @@ public class SimpleFacets {
if (arrIdx>=0 && arrIdx<nTerms) counts[arrIdx]++; if (arrIdx>=0 && arrIdx<nTerms) counts[arrIdx]++;
} }
} }
} else if (ordReader instanceof Direct8) { } else if (arr instanceof byte[]) {
byte[] ords = ((Direct8)ordReader).getArray(); byte[] ords = (byte[]) arr;
if (prefix==null) { if (prefix==null) {
while (iter.hasNext()) { while (iter.hasNext()) {
counts[ords[iter.nextDoc()] & 0xff]++; counts[ords[iter.nextDoc()] & 0xff]++;

View File

@ -24,9 +24,6 @@ import org.apache.lucene.search.FieldComparator;
import org.apache.lucene.search.FieldComparatorSource; import org.apache.lucene.search.FieldComparatorSource;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.UnicodeUtil; import org.apache.lucene.util.UnicodeUtil;
import org.apache.lucene.util.packed.Direct16;
import org.apache.lucene.util.packed.Direct32;
import org.apache.lucene.util.packed.Direct8;
import org.apache.lucene.util.packed.PackedInts; import org.apache.lucene.util.packed.PackedInts;
import java.io.IOException; import java.io.IOException;
@ -438,15 +435,19 @@ class TermOrdValComparator_SML extends FieldComparator<Comparable> {
public static FieldComparator createComparator(IndexReader reader, TermOrdValComparator_SML parent) throws IOException { public static FieldComparator createComparator(IndexReader reader, TermOrdValComparator_SML parent) throws IOException {
parent.termsIndex = FieldCache.DEFAULT.getTermsIndex(reader, parent.field); parent.termsIndex = FieldCache.DEFAULT.getTermsIndex(reader, parent.field);
final PackedInts.Reader docToOrd = parent.termsIndex.getDocToOrd(); final PackedInts.Reader docToOrd = parent.termsIndex.getDocToOrd();
PerSegmentComparator perSegComp; PerSegmentComparator perSegComp = null;
if (docToOrd.hasArray()) {
final Object arr = docToOrd.getArray();
if (arr instanceof byte[]) {
perSegComp = new ByteOrdComparator((byte[]) arr, parent);
} else if (arr instanceof short[]) {
perSegComp = new ShortOrdComparator((short[]) arr, parent);
} else if (arr instanceof int[]) {
perSegComp = new IntOrdComparator((int[]) arr, parent);
}
}
if (docToOrd instanceof Direct8) { if (perSegComp == null) {
perSegComp = new ByteOrdComparator(((Direct8) docToOrd).getArray(), parent);
} else if (docToOrd instanceof Direct16) {
perSegComp = new ShortOrdComparator(((Direct16) docToOrd).getArray(), parent);
} else if (docToOrd instanceof Direct32) {
perSegComp = new IntOrdComparator(((Direct32) docToOrd).getArray(), parent);
} else {
perSegComp = new AnyOrdComparator(docToOrd, parent); perSegComp = new AnyOrdComparator(docToOrd, parent);
} }